[go: nahoru, domu]

blob: e22af8127f6ee4a6bb768b60d97735b461328c75 [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"
Tao Bai1e1deb412019-07-21 16:14:0011#include "cc/paint/node_id.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"
Khushal464808c92018-10-19 23:45:5915#include "third_party/skia/include/core/SkTextBlob.h"
enne34f6084c2017-02-02 22:39:0816
Florin Malita15935772019-02-06 21:52:2417namespace printing {
18class MetafileSkia;
19} // namespace printing
20
ckitagawa53eeaef2019-11-08 15:17:3421namespace paint_preview {
22class PaintPreviewTracker;
23} // namespace paint_preview
24
enne34f6084c2017-02-02 22:39:0825namespace cc {
Malay Keshavcf4ceef12018-10-19 17:56:5126class SkottieWrapper;
enned2501572017-03-09 19:59:1727class PaintFlags;
enne59df29de2017-05-02 03:23:3728class PaintOpBuffer;
29
30using PaintRecord = PaintOpBuffer;
enne34f6084c2017-02-02 22:39:0831
Adrienne Walker26aac6e52018-10-23 19:23:3732// PaintCanvas is the cc/paint wrapper of SkCanvas. It has a more restricted
33// interface than SkCanvas (trimmed back to only what Chrome uses). Its reason
34// for existence is so that it can do custom serialization logic into a
35// PaintOpBuffer which (unlike SkPicture) is mutable, handles image replacement,
36// and can be serialized in custom ways (such as using the transfer cache).
37//
38// PaintCanvas is usually implemented by either:
39// (1) SkiaPaintCanvas, which is backed by an SkCanvas, usually for rasterizing.
40// (2) RecordPaintCanvas, which records paint commands into a PaintOpBuffer.
41//
42// SkiaPaintCanvas allows callers to go from PaintCanvas to SkCanvas (or
43// PaintRecord to SkPicture), but this is a one way trip. There is no way to go
44// from SkCanvas to PaintCanvas or from SkPicture back into PaintRecord.
enned2501572017-03-09 19:59:1745class CC_PAINT_EXPORT PaintCanvas {
enne34f6084c2017-02-02 22:39:0846 public:
Vladimir Levinf06d1cd72019-03-13 18:24:1047 PaintCanvas() = default;
48 PaintCanvas(const PaintCanvas&) = delete;
49 virtual ~PaintCanvas() = default;
50
51 PaintCanvas& operator=(const PaintCanvas&) = delete;
khushalsagarb74f9122017-03-22 04:37:4752
enne59df29de2017-05-02 03:23:3753 // TODO(enne): this only appears to mostly be used to determine if this is
54 // recording or not, so could be simplified or removed.
enne98c9f8052017-03-15 19:38:2255 virtual SkImageInfo imageInfo() const = 0;
enned2501572017-03-09 19:59:1756
Tom Andersonbec09512019-09-16 20:32:1657 virtual void* accessTopLayerPixels(SkImageInfo* info,
58 size_t* rowBytes,
59 SkIPoint* origin = nullptr) = 0;
60
enned2501572017-03-09 19:59:1761 // TODO(enne): It would be nice to get rid of flush() entirely, as it
62 // doesn't really make sense for recording. However, this gets used by
zhuoyu.qian4689dde22017-10-16 04:11:4863 // PaintCanvasVideoRenderer which takes a PaintCanvas to paint both
enned2501572017-03-09 19:59:1764 // software and hardware video. This is super entangled with ImageBuffer
65 // and canvas/video painting in Blink where the same paths are used for
66 // both recording and gpu work.
enne98c9f8052017-03-15 19:38:2267 virtual void flush() = 0;
enned2501572017-03-09 19:59:1768
enne98c9f8052017-03-15 19:38:2269 virtual int save() = 0;
70 virtual int saveLayer(const SkRect* bounds, const PaintFlags* flags) = 0;
Peter Kastingfe4ba932019-01-07 20:21:0571 virtual int saveLayerAlpha(const SkRect* bounds, uint8_t alpha) = 0;
enne98c9f8052017-03-15 19:38:2272
73 virtual void restore() = 0;
74 virtual int getSaveCount() const = 0;
75 virtual void restoreToCount(int save_count) = 0;
76 virtual void translate(SkScalar dx, SkScalar dy) = 0;
77 virtual void scale(SkScalar sx, SkScalar sy) = 0;
78 virtual void rotate(SkScalar degrees) = 0;
Aaron Krajeski5e542c82020-12-05 01:50:2179 // TODO(aaronhk): crbug.com/1153330 deprecate these in favor of the SkM44
Aaron Krajeski8a128e232020-12-15 21:47:0680 // versions.
enne98c9f8052017-03-15 19:38:2281 virtual void concat(const SkMatrix& matrix) = 0;
82 virtual void setMatrix(const SkMatrix& matrix) = 0;
Aaron Krajeski8a128e232020-12-15 21:47:0683 virtual void concat(const SkM44& matrix) = 0;
Aaron Krajeski5e542c82020-12-05 01:50:2184 virtual void setMatrix(const SkM44& matrix) = 0;
85
enne98c9f8052017-03-15 19:38:2286 virtual void clipRect(const SkRect& rect,
87 SkClipOp op,
88 bool do_anti_alias) = 0;
89 void clipRect(const SkRect& rect, SkClipOp op) { clipRect(rect, op, false); }
90 void clipRect(const SkRect& rect, bool do_anti_alias) {
91 clipRect(rect, SkClipOp::kIntersect, do_anti_alias);
enned2501572017-03-09 19:59:1792 }
enne98c9f8052017-03-15 19:38:2293 void clipRect(const SkRect& rect) {
94 clipRect(rect, SkClipOp::kIntersect, false);
enned2501572017-03-09 19:59:1795 }
96
enne98c9f8052017-03-15 19:38:2297 virtual void clipRRect(const SkRRect& rrect,
98 SkClipOp op,
99 bool do_anti_alias) = 0;
100 void clipRRect(const SkRRect& rrect, bool do_anti_alias) {
101 clipRRect(rrect, SkClipOp::kIntersect, do_anti_alias);
enned2501572017-03-09 19:59:17102 }
enne98c9f8052017-03-15 19:38:22103 void clipRRect(const SkRRect& rrect, SkClipOp op) {
104 clipRRect(rrect, op, false);
enned2501572017-03-09 19:59:17105 }
enne98c9f8052017-03-15 19:38:22106 void clipRRect(const SkRRect& rrect) {
107 clipRRect(rrect, SkClipOp::kIntersect, false);
enned2501572017-03-09 19:59:17108 }
enned2501572017-03-09 19:59:17109
enne98c9f8052017-03-15 19:38:22110 virtual void clipPath(const SkPath& path,
111 SkClipOp op,
112 bool do_anti_alias) = 0;
113 void clipPath(const SkPath& path, SkClipOp op) { clipPath(path, op, false); }
114 void clipPath(const SkPath& path, bool do_anti_alias) {
115 clipPath(path, SkClipOp::kIntersect, do_anti_alias);
enned2501572017-03-09 19:59:17116 }
enne98c9f8052017-03-15 19:38:22117
enne98c9f8052017-03-15 19:38:22118 virtual SkRect getLocalClipBounds() const = 0;
119 virtual bool getLocalClipBounds(SkRect* bounds) const = 0;
120 virtual SkIRect getDeviceClipBounds() const = 0;
121 virtual bool getDeviceClipBounds(SkIRect* bounds) const = 0;
122 virtual void drawColor(SkColor color, SkBlendMode mode) = 0;
123 void drawColor(SkColor color) { drawColor(color, SkBlendMode::kSrcOver); }
enne59df29de2017-05-02 03:23:37124
125 // TODO(enne): This is a synonym for drawColor with kSrc. Remove it.
enne98c9f8052017-03-15 19:38:22126 virtual void clear(SkColor color) = 0;
127
128 virtual void drawLine(SkScalar x0,
129 SkScalar y0,
130 SkScalar x1,
131 SkScalar y1,
132 const PaintFlags& flags) = 0;
133 virtual void drawRect(const SkRect& rect, const PaintFlags& flags) = 0;
134 virtual void drawIRect(const SkIRect& rect, const PaintFlags& flags) = 0;
135 virtual void drawOval(const SkRect& oval, const PaintFlags& flags) = 0;
136 virtual void drawRRect(const SkRRect& rrect, const PaintFlags& flags) = 0;
137 virtual void drawDRRect(const SkRRect& outer,
138 const SkRRect& inner,
139 const PaintFlags& flags) = 0;
enne98c9f8052017-03-15 19:38:22140 virtual void drawRoundRect(const SkRect& rect,
141 SkScalar rx,
142 SkScalar ry,
143 const PaintFlags& flags) = 0;
144 virtual void drawPath(const SkPath& path, const PaintFlags& flags) = 0;
vmpstr94cfa882017-04-14 01:19:35145 virtual void drawImage(const PaintImage& image,
enne98c9f8052017-03-15 19:38:22146 SkScalar left,
147 SkScalar top,
Mike Reed859086a2021-01-23 18:19:15148 const SkSamplingOptions&,
enne98c9f8052017-03-15 19:38:22149 const PaintFlags* flags) = 0;
vmpstr94cfa882017-04-14 01:19:35150 void drawImage(const PaintImage& image, SkScalar left, SkScalar top) {
Mike Reed859086a2021-01-23 18:19:15151 drawImage(image, left, top, SkSamplingOptions(), nullptr);
enned2501572017-03-09 19:59:17152 }
153
vmpstr94cfa882017-04-14 01:19:35154 virtual void drawImageRect(const PaintImage& image,
enne98c9f8052017-03-15 19:38:22155 const SkRect& src,
156 const SkRect& dst,
Mike Reed859086a2021-01-23 18:19:15157 const SkSamplingOptions&,
enne98c9f8052017-03-15 19:38:22158 const PaintFlags* flags,
jongdeok.kim50b377072020-05-15 22:16:12159 SkCanvas::SrcRectConstraint constraint) = 0;
Mike Reed859086a2021-01-23 18:19:15160 void drawImageRect(const PaintImage& image,
161 const SkRect& src,
162 const SkRect& dst,
163 SkCanvas::SrcRectConstraint constraint) {
164 drawImageRect(image, src, dst, SkSamplingOptions(), nullptr, constraint);
165 }
enned2501572017-03-09 19:59:17166
Malay Keshavcf4ceef12018-10-19 17:56:51167 // Draws the frame of the |skottie| animation specified by the normalized time
168 // t [0->first frame..1->last frame] at the destination bounds given by |dst|
169 // onto the canvas.
170 virtual void drawSkottie(scoped_refptr<SkottieWrapper> skottie,
171 const SkRect& dst,
172 float t) = 0;
173
Khushal464808c92018-10-19 23:45:59174 virtual void drawTextBlob(sk_sp<SkTextBlob> blob,
enne98c9f8052017-03-15 19:38:22175 SkScalar x,
176 SkScalar y,
177 const PaintFlags& flags) = 0;
enned2501572017-03-09 19:59:17178
Tao Baiedaf87092019-01-30 23:12:54179 virtual void drawTextBlob(sk_sp<SkTextBlob> blob,
180 SkScalar x,
181 SkScalar y,
Tao Bai1e1deb412019-07-21 16:14:00182 NodeId node_id,
183 const PaintFlags& flags) = 0;
Tao Baiedaf87092019-01-30 23:12:54184
Adrienne Walker15ce23502017-05-08 18:08:15185 // Unlike SkCanvas::drawPicture, this only plays back the PaintRecord and does
186 // not add an additional clip. This is closer to SkPicture::playback.
ennedffda502017-03-23 20:46:43187 virtual void drawPicture(sk_sp<const PaintRecord> record) = 0;
enned2501572017-03-09 19:59:17188
enne98c9f8052017-03-15 19:38:22189 virtual bool isClipEmpty() const = 0;
Mike Reedf97e2d162020-01-07 15:26:43190 virtual SkMatrix getTotalMatrix() const = 0;
enned2501572017-03-09 19:59:17191
Florin Malita15935772019-02-06 21:52:24192 // Used for printing
enne5fa117a2017-04-04 20:56:58193 enum class AnnotationType {
194 URL,
195 NAMED_DESTINATION,
196 LINK_TO_DESTINATION,
197 };
198 virtual void Annotate(AnnotationType type,
199 const SkRect& rect,
200 sk_sp<SkData> data) = 0;
Florin Malita15935772019-02-06 21:52:24201 printing::MetafileSkia* GetPrintingMetafile() const { return metafile_; }
202 void SetPrintingMetafile(printing::MetafileSkia* metafile) {
203 metafile_ = metafile;
204 }
ckitagawa53eeaef2019-11-08 15:17:34205 paint_preview::PaintPreviewTracker* GetPaintPreviewTracker() const {
206 return tracker_;
207 }
208 void SetPaintPreviewTracker(paint_preview::PaintPreviewTracker* tracker) {
209 tracker_ = tracker;
210 }
enned2501572017-03-09 19:59:17211
Wei Li2a9bfe42018-01-13 05:42:56212 // Subclasses can override to handle custom data.
213 virtual void recordCustomData(uint32_t id) {}
214
Dominic Mazzonicf5d6b02020-03-06 19:49:12215 // Used for marked content in PDF files.
216 virtual void setNodeId(int) = 0;
217
enne59df29de2017-05-02 03:23:37218 private:
Florin Malita15935772019-02-06 21:52:24219 printing::MetafileSkia* metafile_ = nullptr;
ckitagawa53eeaef2019-11-08 15:17:34220 paint_preview::PaintPreviewTracker* tracker_ = nullptr;
enne34f6084c2017-02-02 22:39:08221};
222
enned2501572017-03-09 19:59:17223class CC_PAINT_EXPORT PaintCanvasAutoRestore {
224 public:
enne98c9f8052017-03-15 19:38:22225 PaintCanvasAutoRestore(PaintCanvas* canvas, bool save) : canvas_(canvas) {
enned2501572017-03-09 19:59:17226 if (canvas_) {
227 save_count_ = canvas_->getSaveCount();
228 if (save) {
229 canvas_->save();
230 }
231 }
232 }
233
enne98c9f8052017-03-15 19:38:22234 ~PaintCanvasAutoRestore() {
enned2501572017-03-09 19:59:17235 if (canvas_) {
236 canvas_->restoreToCount(save_count_);
237 }
238 }
239
enne98c9f8052017-03-15 19:38:22240 void restore() {
enned2501572017-03-09 19:59:17241 if (canvas_) {
242 canvas_->restoreToCount(save_count_);
243 canvas_ = nullptr;
244 }
245 }
246
247 private:
248 PaintCanvas* canvas_ = nullptr;
249 int save_count_ = 0;
250};
251
enne34f6084c2017-02-02 22:39:08252} // namespace cc
253
254#endif // CC_PAINT_PAINT_CANVAS_H_