[go: nahoru, domu]

blob: 0f0c694eff1dd9f3bf9fed253a28be5b9ee37b76 [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"
enneea15ddb2017-03-22 01:49:089#include "base/memory/ref_counted.h"
enne7b64edf32017-02-16 20:10:0210#include "build/build_config.h"
enne34f6084c2017-02-02 22:39:0811#include "cc/paint/paint_export.h"
vmpstr94cfa882017-04-14 01:19:3512#include "cc/paint/paint_image.h"
enne34f6084c2017-02-02 22:39:0813#include "third_party/skia/include/core/SkCanvas.h"
Khushal464808c92018-10-19 23:45:5914#include "third_party/skia/include/core/SkTextBlob.h"
enne34f6084c2017-02-02 22:39:0815
Florin Malita15935772019-02-06 21:52:2416namespace printing {
17class MetafileSkia;
18} // namespace printing
19
enne34f6084c2017-02-02 22:39:0820namespace cc {
Malay Keshavcf4ceef12018-10-19 17:56:5121class SkottieWrapper;
enned2501572017-03-09 19:59:1722class PaintFlags;
enne59df29de2017-05-02 03:23:3723class PaintOpBuffer;
24
Tao Baiedaf87092019-01-30 23:12:5425struct NodeHolder;
26
enne59df29de2017-05-02 03:23:3727using PaintRecord = PaintOpBuffer;
enne34f6084c2017-02-02 22:39:0828
Adrienne Walker26aac6e52018-10-23 19:23:3729// PaintCanvas is the cc/paint wrapper of SkCanvas. It has a more restricted
30// interface than SkCanvas (trimmed back to only what Chrome uses). Its reason
31// for existence is so that it can do custom serialization logic into a
32// PaintOpBuffer which (unlike SkPicture) is mutable, handles image replacement,
33// and can be serialized in custom ways (such as using the transfer cache).
34//
35// PaintCanvas is usually implemented by either:
36// (1) SkiaPaintCanvas, which is backed by an SkCanvas, usually for rasterizing.
37// (2) RecordPaintCanvas, which records paint commands into a PaintOpBuffer.
38//
39// SkiaPaintCanvas allows callers to go from PaintCanvas to SkCanvas (or
40// PaintRecord to SkPicture), but this is a one way trip. There is no way to go
41// from SkCanvas to PaintCanvas or from SkPicture back into PaintRecord.
enned2501572017-03-09 19:59:1742class CC_PAINT_EXPORT PaintCanvas {
enne34f6084c2017-02-02 22:39:0843 public:
Vladimir Levinf06d1cd72019-03-13 18:24:1044 PaintCanvas() = default;
45 PaintCanvas(const PaintCanvas&) = delete;
46 virtual ~PaintCanvas() = default;
47
48 PaintCanvas& operator=(const PaintCanvas&) = delete;
khushalsagarb74f9122017-03-22 04:37:4749
enne59df29de2017-05-02 03:23:3750 // TODO(enne): this only appears to mostly be used to determine if this is
51 // recording or not, so could be simplified or removed.
enne98c9f8052017-03-15 19:38:2252 virtual SkImageInfo imageInfo() const = 0;
enned2501572017-03-09 19:59:1753
enned2501572017-03-09 19:59:1754 // TODO(enne): It would be nice to get rid of flush() entirely, as it
55 // doesn't really make sense for recording. However, this gets used by
zhuoyu.qian4689dde22017-10-16 04:11:4856 // PaintCanvasVideoRenderer which takes a PaintCanvas to paint both
enned2501572017-03-09 19:59:1757 // software and hardware video. This is super entangled with ImageBuffer
58 // and canvas/video painting in Blink where the same paths are used for
59 // both recording and gpu work.
enne98c9f8052017-03-15 19:38:2260 virtual void flush() = 0;
enned2501572017-03-09 19:59:1761
enne98c9f8052017-03-15 19:38:2262 virtual int save() = 0;
63 virtual int saveLayer(const SkRect* bounds, const PaintFlags* flags) = 0;
Peter Kastingfe4ba932019-01-07 20:21:0564 virtual int saveLayerAlpha(const SkRect* bounds, uint8_t alpha) = 0;
enne98c9f8052017-03-15 19:38:2265
66 virtual void restore() = 0;
67 virtual int getSaveCount() const = 0;
68 virtual void restoreToCount(int save_count) = 0;
69 virtual void translate(SkScalar dx, SkScalar dy) = 0;
70 virtual void scale(SkScalar sx, SkScalar sy) = 0;
71 virtual void rotate(SkScalar degrees) = 0;
enne98c9f8052017-03-15 19:38:2272 virtual void concat(const SkMatrix& matrix) = 0;
73 virtual void setMatrix(const SkMatrix& matrix) = 0;
enne98c9f8052017-03-15 19:38:2274
75 virtual void clipRect(const SkRect& rect,
76 SkClipOp op,
77 bool do_anti_alias) = 0;
78 void clipRect(const SkRect& rect, SkClipOp op) { clipRect(rect, op, false); }
79 void clipRect(const SkRect& rect, bool do_anti_alias) {
80 clipRect(rect, SkClipOp::kIntersect, do_anti_alias);
enned2501572017-03-09 19:59:1781 }
enne98c9f8052017-03-15 19:38:2282 void clipRect(const SkRect& rect) {
83 clipRect(rect, SkClipOp::kIntersect, false);
enned2501572017-03-09 19:59:1784 }
85
enne98c9f8052017-03-15 19:38:2286 virtual void clipRRect(const SkRRect& rrect,
87 SkClipOp op,
88 bool do_anti_alias) = 0;
89 void clipRRect(const SkRRect& rrect, bool do_anti_alias) {
90 clipRRect(rrect, SkClipOp::kIntersect, do_anti_alias);
enned2501572017-03-09 19:59:1791 }
enne98c9f8052017-03-15 19:38:2292 void clipRRect(const SkRRect& rrect, SkClipOp op) {
93 clipRRect(rrect, op, false);
enned2501572017-03-09 19:59:1794 }
enne98c9f8052017-03-15 19:38:2295 void clipRRect(const SkRRect& rrect) {
96 clipRRect(rrect, SkClipOp::kIntersect, false);
enned2501572017-03-09 19:59:1797 }
enned2501572017-03-09 19:59:1798
enne98c9f8052017-03-15 19:38:2299 virtual void clipPath(const SkPath& path,
100 SkClipOp op,
101 bool do_anti_alias) = 0;
102 void clipPath(const SkPath& path, SkClipOp op) { clipPath(path, op, false); }
103 void clipPath(const SkPath& path, bool do_anti_alias) {
104 clipPath(path, SkClipOp::kIntersect, do_anti_alias);
enned2501572017-03-09 19:59:17105 }
enne98c9f8052017-03-15 19:38:22106
enne98c9f8052017-03-15 19:38:22107 virtual SkRect getLocalClipBounds() const = 0;
108 virtual bool getLocalClipBounds(SkRect* bounds) const = 0;
109 virtual SkIRect getDeviceClipBounds() const = 0;
110 virtual bool getDeviceClipBounds(SkIRect* bounds) const = 0;
111 virtual void drawColor(SkColor color, SkBlendMode mode) = 0;
112 void drawColor(SkColor color) { drawColor(color, SkBlendMode::kSrcOver); }
enne59df29de2017-05-02 03:23:37113
114 // TODO(enne): This is a synonym for drawColor with kSrc. Remove it.
enne98c9f8052017-03-15 19:38:22115 virtual void clear(SkColor color) = 0;
116
117 virtual void drawLine(SkScalar x0,
118 SkScalar y0,
119 SkScalar x1,
120 SkScalar y1,
121 const PaintFlags& flags) = 0;
122 virtual void drawRect(const SkRect& rect, const PaintFlags& flags) = 0;
123 virtual void drawIRect(const SkIRect& rect, const PaintFlags& flags) = 0;
124 virtual void drawOval(const SkRect& oval, const PaintFlags& flags) = 0;
125 virtual void drawRRect(const SkRRect& rrect, const PaintFlags& flags) = 0;
126 virtual void drawDRRect(const SkRRect& outer,
127 const SkRRect& inner,
128 const PaintFlags& flags) = 0;
enne98c9f8052017-03-15 19:38:22129 virtual void drawRoundRect(const SkRect& rect,
130 SkScalar rx,
131 SkScalar ry,
132 const PaintFlags& flags) = 0;
133 virtual void drawPath(const SkPath& path, const PaintFlags& flags) = 0;
vmpstr94cfa882017-04-14 01:19:35134 virtual void drawImage(const PaintImage& image,
enne98c9f8052017-03-15 19:38:22135 SkScalar left,
136 SkScalar top,
137 const PaintFlags* flags) = 0;
vmpstr94cfa882017-04-14 01:19:35138 void drawImage(const PaintImage& image, SkScalar left, SkScalar top) {
enne98c9f8052017-03-15 19:38:22139 drawImage(image, left, top, nullptr);
enned2501572017-03-09 19:59:17140 }
141
142 enum SrcRectConstraint {
143 kStrict_SrcRectConstraint = SkCanvas::kStrict_SrcRectConstraint,
144 kFast_SrcRectConstraint = SkCanvas::kFast_SrcRectConstraint,
145 };
146
vmpstr94cfa882017-04-14 01:19:35147 virtual void drawImageRect(const PaintImage& image,
enne98c9f8052017-03-15 19:38:22148 const SkRect& src,
149 const SkRect& dst,
150 const PaintFlags* flags,
151 SrcRectConstraint constraint) = 0;
enned2501572017-03-09 19:59:17152
Malay Keshavcf4ceef12018-10-19 17:56:51153 // Draws the frame of the |skottie| animation specified by the normalized time
154 // t [0->first frame..1->last frame] at the destination bounds given by |dst|
155 // onto the canvas.
156 virtual void drawSkottie(scoped_refptr<SkottieWrapper> skottie,
157 const SkRect& dst,
158 float t) = 0;
159
Khushal464808c92018-10-19 23:45:59160 virtual void drawTextBlob(sk_sp<SkTextBlob> blob,
enne98c9f8052017-03-15 19:38:22161 SkScalar x,
162 SkScalar y,
163 const PaintFlags& flags) = 0;
enned2501572017-03-09 19:59:17164
Tao Baiedaf87092019-01-30 23:12:54165 virtual void drawTextBlob(sk_sp<SkTextBlob> blob,
166 SkScalar x,
167 SkScalar y,
168 const PaintFlags& flags,
169 const NodeHolder& holder) = 0;
170
Adrienne Walker15ce23502017-05-08 18:08:15171 // Unlike SkCanvas::drawPicture, this only plays back the PaintRecord and does
172 // not add an additional clip. This is closer to SkPicture::playback.
ennedffda502017-03-23 20:46:43173 virtual void drawPicture(sk_sp<const PaintRecord> record) = 0;
enned2501572017-03-09 19:59:17174
enne98c9f8052017-03-15 19:38:22175 virtual bool isClipEmpty() const = 0;
176 virtual bool isClipRect() const = 0;
177 virtual const SkMatrix& getTotalMatrix() const = 0;
enned2501572017-03-09 19:59:17178
Florin Malita15935772019-02-06 21:52:24179 // Used for printing
enne5fa117a2017-04-04 20:56:58180 enum class AnnotationType {
181 URL,
182 NAMED_DESTINATION,
183 LINK_TO_DESTINATION,
184 };
185 virtual void Annotate(AnnotationType type,
186 const SkRect& rect,
187 sk_sp<SkData> data) = 0;
Florin Malita15935772019-02-06 21:52:24188 printing::MetafileSkia* GetPrintingMetafile() const { return metafile_; }
189 void SetPrintingMetafile(printing::MetafileSkia* metafile) {
190 metafile_ = metafile;
191 }
enned2501572017-03-09 19:59:17192
Wei Li2a9bfe42018-01-13 05:42:56193 // Subclasses can override to handle custom data.
194 virtual void recordCustomData(uint32_t id) {}
195
enne59df29de2017-05-02 03:23:37196 private:
Florin Malita15935772019-02-06 21:52:24197 printing::MetafileSkia* metafile_ = nullptr;
enne34f6084c2017-02-02 22:39:08198};
199
enned2501572017-03-09 19:59:17200class CC_PAINT_EXPORT PaintCanvasAutoRestore {
201 public:
enne98c9f8052017-03-15 19:38:22202 PaintCanvasAutoRestore(PaintCanvas* canvas, bool save) : canvas_(canvas) {
enned2501572017-03-09 19:59:17203 if (canvas_) {
204 save_count_ = canvas_->getSaveCount();
205 if (save) {
206 canvas_->save();
207 }
208 }
209 }
210
enne98c9f8052017-03-15 19:38:22211 ~PaintCanvasAutoRestore() {
enned2501572017-03-09 19:59:17212 if (canvas_) {
213 canvas_->restoreToCount(save_count_);
214 }
215 }
216
enne98c9f8052017-03-15 19:38:22217 void restore() {
enned2501572017-03-09 19:59:17218 if (canvas_) {
219 canvas_->restoreToCount(save_count_);
220 canvas_ = nullptr;
221 }
222 }
223
224 private:
225 PaintCanvas* canvas_ = nullptr;
226 int save_count_ = 0;
227};
228
enne34f6084c2017-02-02 22:39:08229} // namespace cc
230
231#endif // CC_PAINT_PAINT_CANVAS_H_