[go: nahoru, domu]

blob: e1fa9d0b7ad778a6a5dfb155178ea5aa1fd617b0 [file] [log] [blame]
enne34f6084c2017-02-02 22:39:081// Copyright 2017 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CC_PAINT_PAINT_CANVAS_H_
6#define CC_PAINT_PAINT_CANVAS_H_
7
enned2501572017-03-09 19:59:178#include "base/compiler_specific.h"
enned2501572017-03-09 19:59:179#include "base/macros.h"
enneea15ddb2017-03-22 01:49:0810#include "base/memory/ref_counted.h"
enne7b64edf32017-02-16 20:10:0211#include "build/build_config.h"
enne34f6084c2017-02-02 22:39:0812#include "cc/paint/paint_export.h"
vmpstr94cfa882017-04-14 01:19:3513#include "cc/paint/paint_image.h"
enne34f6084c2017-02-02 22:39:0814#include "third_party/skia/include/core/SkCanvas.h"
enne34f6084c2017-02-02 22:39:0815
16namespace cc {
17
chrishtr2da019302017-03-20 23:35:0318class DisplayItemList;
enned2501572017-03-09 19:59:1719class PaintFlags;
enne59df29de2017-05-02 03:23:3720class PaintOpBuffer;
21
22using PaintRecord = PaintOpBuffer;
enne34f6084c2017-02-02 22:39:0823
enned2501572017-03-09 19:59:1724class CC_PAINT_EXPORT PaintCanvas {
enne34f6084c2017-02-02 22:39:0825 public:
enne59df29de2017-05-02 03:23:3726 PaintCanvas() {}
khushalsagarb74f9122017-03-22 04:37:4727 virtual ~PaintCanvas() {}
28
enne98c9f8052017-03-15 19:38:2229 virtual SkMetaData& getMetaData() = 0;
enne59df29de2017-05-02 03:23:3730
31 // TODO(enne): this only appears to mostly be used to determine if this is
32 // recording or not, so could be simplified or removed.
enne98c9f8052017-03-15 19:38:2233 virtual SkImageInfo imageInfo() const = 0;
enned2501572017-03-09 19:59:1734
enned2501572017-03-09 19:59:1735 // TODO(enne): It would be nice to get rid of flush() entirely, as it
36 // doesn't really make sense for recording. However, this gets used by
37 // SkCanvasVideoRenderer which takes a PaintCanvas to paint both
38 // software and hardware video. This is super entangled with ImageBuffer
39 // and canvas/video painting in Blink where the same paths are used for
40 // both recording and gpu work.
enne98c9f8052017-03-15 19:38:2241 virtual void flush() = 0;
enned2501572017-03-09 19:59:1742
enne98c9f8052017-03-15 19:38:2243 virtual int save() = 0;
44 virtual int saveLayer(const SkRect* bounds, const PaintFlags* flags) = 0;
enne59df29de2017-05-02 03:23:3745 virtual int saveLayerAlpha(const SkRect* bounds, uint8_t alpha) = 0;
enne98c9f8052017-03-15 19:38:2246
47 virtual void restore() = 0;
48 virtual int getSaveCount() const = 0;
49 virtual void restoreToCount(int save_count) = 0;
50 virtual void translate(SkScalar dx, SkScalar dy) = 0;
51 virtual void scale(SkScalar sx, SkScalar sy) = 0;
52 virtual void rotate(SkScalar degrees) = 0;
enne98c9f8052017-03-15 19:38:2253 virtual void concat(const SkMatrix& matrix) = 0;
54 virtual void setMatrix(const SkMatrix& matrix) = 0;
enne98c9f8052017-03-15 19:38:2255
56 virtual void clipRect(const SkRect& rect,
57 SkClipOp op,
58 bool do_anti_alias) = 0;
59 void clipRect(const SkRect& rect, SkClipOp op) { clipRect(rect, op, false); }
60 void clipRect(const SkRect& rect, bool do_anti_alias) {
61 clipRect(rect, SkClipOp::kIntersect, do_anti_alias);
enned2501572017-03-09 19:59:1762 }
enne98c9f8052017-03-15 19:38:2263 void clipRect(const SkRect& rect) {
64 clipRect(rect, SkClipOp::kIntersect, false);
enned2501572017-03-09 19:59:1765 }
66
enne98c9f8052017-03-15 19:38:2267 virtual void clipRRect(const SkRRect& rrect,
68 SkClipOp op,
69 bool do_anti_alias) = 0;
70 void clipRRect(const SkRRect& rrect, bool do_anti_alias) {
71 clipRRect(rrect, SkClipOp::kIntersect, do_anti_alias);
enned2501572017-03-09 19:59:1772 }
enne98c9f8052017-03-15 19:38:2273 void clipRRect(const SkRRect& rrect, SkClipOp op) {
74 clipRRect(rrect, op, false);
enned2501572017-03-09 19:59:1775 }
enne98c9f8052017-03-15 19:38:2276 void clipRRect(const SkRRect& rrect) {
77 clipRRect(rrect, SkClipOp::kIntersect, false);
enned2501572017-03-09 19:59:1778 }
enned2501572017-03-09 19:59:1779
enne98c9f8052017-03-15 19:38:2280 virtual void clipPath(const SkPath& path,
81 SkClipOp op,
82 bool do_anti_alias) = 0;
83 void clipPath(const SkPath& path, SkClipOp op) { clipPath(path, op, false); }
84 void clipPath(const SkPath& path, bool do_anti_alias) {
85 clipPath(path, SkClipOp::kIntersect, do_anti_alias);
enned2501572017-03-09 19:59:1786 }
enne98c9f8052017-03-15 19:38:2287
88 virtual bool quickReject(const SkRect& rect) const = 0;
89 virtual bool quickReject(const SkPath& path) const = 0;
90 virtual SkRect getLocalClipBounds() const = 0;
91 virtual bool getLocalClipBounds(SkRect* bounds) const = 0;
92 virtual SkIRect getDeviceClipBounds() const = 0;
93 virtual bool getDeviceClipBounds(SkIRect* bounds) const = 0;
94 virtual void drawColor(SkColor color, SkBlendMode mode) = 0;
95 void drawColor(SkColor color) { drawColor(color, SkBlendMode::kSrcOver); }
enne59df29de2017-05-02 03:23:3796
97 // TODO(enne): This is a synonym for drawColor with kSrc. Remove it.
enne98c9f8052017-03-15 19:38:2298 virtual void clear(SkColor color) = 0;
99
100 virtual void drawLine(SkScalar x0,
101 SkScalar y0,
102 SkScalar x1,
103 SkScalar y1,
104 const PaintFlags& flags) = 0;
105 virtual void drawRect(const SkRect& rect, const PaintFlags& flags) = 0;
106 virtual void drawIRect(const SkIRect& rect, const PaintFlags& flags) = 0;
107 virtual void drawOval(const SkRect& oval, const PaintFlags& flags) = 0;
108 virtual void drawRRect(const SkRRect& rrect, const PaintFlags& flags) = 0;
109 virtual void drawDRRect(const SkRRect& outer,
110 const SkRRect& inner,
111 const PaintFlags& flags) = 0;
112 virtual void drawCircle(SkScalar cx,
113 SkScalar cy,
114 SkScalar radius,
115 const PaintFlags& flags) = 0;
116 virtual void drawArc(const SkRect& oval,
117 SkScalar start_angle,
118 SkScalar sweep_angle,
119 bool use_center,
120 const PaintFlags& flags) = 0;
121 virtual void drawRoundRect(const SkRect& rect,
122 SkScalar rx,
123 SkScalar ry,
124 const PaintFlags& flags) = 0;
125 virtual void drawPath(const SkPath& path, const PaintFlags& flags) = 0;
vmpstr94cfa882017-04-14 01:19:35126 virtual void drawImage(const PaintImage& image,
enne98c9f8052017-03-15 19:38:22127 SkScalar left,
128 SkScalar top,
129 const PaintFlags* flags) = 0;
vmpstr94cfa882017-04-14 01:19:35130 void drawImage(const PaintImage& image, SkScalar left, SkScalar top) {
enne98c9f8052017-03-15 19:38:22131 drawImage(image, left, top, nullptr);
enned2501572017-03-09 19:59:17132 }
133
134 enum SrcRectConstraint {
135 kStrict_SrcRectConstraint = SkCanvas::kStrict_SrcRectConstraint,
136 kFast_SrcRectConstraint = SkCanvas::kFast_SrcRectConstraint,
137 };
138
vmpstr94cfa882017-04-14 01:19:35139 virtual void drawImageRect(const PaintImage& image,
enne98c9f8052017-03-15 19:38:22140 const SkRect& src,
141 const SkRect& dst,
142 const PaintFlags* flags,
143 SrcRectConstraint constraint) = 0;
144 virtual void drawBitmap(const SkBitmap& bitmap,
145 SkScalar left,
146 SkScalar top,
147 const PaintFlags* flags) = 0;
148 void drawBitmap(const SkBitmap& bitmap, SkScalar left, SkScalar top) {
149 drawBitmap(bitmap, left, top, nullptr);
enned2501572017-03-09 19:59:17150 }
151
enne98c9f8052017-03-15 19:38:22152 virtual void drawText(const void* text,
153 size_t byte_length,
154 SkScalar x,
155 SkScalar y,
156 const PaintFlags& flags) = 0;
157 virtual void drawPosText(const void* text,
158 size_t byte_length,
159 const SkPoint pos[],
160 const PaintFlags& flags) = 0;
enne8c44b142017-03-20 19:34:31161 virtual void drawTextBlob(sk_sp<SkTextBlob> blob,
enne98c9f8052017-03-15 19:38:22162 SkScalar x,
163 SkScalar y,
164 const PaintFlags& flags) = 0;
enned2501572017-03-09 19:59:17165
chrishtr2da019302017-03-20 23:35:03166 virtual void drawDisplayItemList(
enneea15ddb2017-03-22 01:49:08167 scoped_refptr<DisplayItemList> display_item_list) = 0;
chrishtr2da019302017-03-20 23:35:03168
ennedffda502017-03-23 20:46:43169 virtual void drawPicture(sk_sp<const PaintRecord> record) = 0;
enned2501572017-03-09 19:59:17170
enne98c9f8052017-03-15 19:38:22171 virtual bool isClipEmpty() const = 0;
172 virtual bool isClipRect() const = 0;
173 virtual const SkMatrix& getTotalMatrix() const = 0;
enned2501572017-03-09 19:59:17174
enne5fa117a2017-04-04 20:56:58175 enum class AnnotationType {
176 URL,
177 NAMED_DESTINATION,
178 LINK_TO_DESTINATION,
179 };
180 virtual void Annotate(AnnotationType type,
181 const SkRect& rect,
182 sk_sp<SkData> data) = 0;
enned2501572017-03-09 19:59:17183
enne2458d6d32017-03-21 20:13:01184 // TODO(enne): maybe this should live on PaintRecord, but that's not
185 // possible when PaintRecord is a typedef.
186 virtual void PlaybackPaintRecord(sk_sp<const PaintRecord> record) = 0;
187
enned2501572017-03-09 19:59:17188 protected:
189 friend class PaintSurface;
190 friend class PaintRecorder;
enne59df29de2017-05-02 03:23:37191
192 private:
193 DISALLOW_COPY_AND_ASSIGN(PaintCanvas);
enne34f6084c2017-02-02 22:39:08194};
195
enned2501572017-03-09 19:59:17196class CC_PAINT_EXPORT PaintCanvasAutoRestore {
197 public:
enne98c9f8052017-03-15 19:38:22198 PaintCanvasAutoRestore(PaintCanvas* canvas, bool save) : canvas_(canvas) {
enned2501572017-03-09 19:59:17199 if (canvas_) {
200 save_count_ = canvas_->getSaveCount();
201 if (save) {
202 canvas_->save();
203 }
204 }
205 }
206
enne98c9f8052017-03-15 19:38:22207 ~PaintCanvasAutoRestore() {
enned2501572017-03-09 19:59:17208 if (canvas_) {
209 canvas_->restoreToCount(save_count_);
210 }
211 }
212
enne98c9f8052017-03-15 19:38:22213 void restore() {
enned2501572017-03-09 19:59:17214 if (canvas_) {
215 canvas_->restoreToCount(save_count_);
216 canvas_ = nullptr;
217 }
218 }
219
220 private:
221 PaintCanvas* canvas_ = nullptr;
222 int save_count_ = 0;
223};
224
225// TODO(enne): Move all these functions into PaintCanvas. These are only
226// separate now to make the transition to concrete types easier by keeping
227// the base PaintCanvas type equivalent to the SkCanvas interface and
228// all these helper functions potentially operating on both.
enne7b64edf32017-02-16 20:10:02229
enne7b64edf32017-02-16 20:10:02230// Following routines are used in print preview workflow to mark the
231// preview metafile.
232#if defined(OS_MACOSX)
233CC_PAINT_EXPORT void SetIsPreviewMetafile(PaintCanvas* canvas, bool is_preview);
234CC_PAINT_EXPORT bool IsPreviewMetafile(PaintCanvas* canvas);
235#endif
236
enne34f6084c2017-02-02 22:39:08237} // namespace cc
238
239#endif // CC_PAINT_PAINT_CANVAS_H_