[go: nahoru, domu]

blob: 06cd77e466c0720b4428c192f4b4395836c97ed0 [file] [log] [blame]
Avi Drissman3f7a9d82022-09-08 20:55:421// Copyright 2017 The Chromium Authors
enne34f6084c2017-02-02 22:39:082// 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"
Keishi Hattori0e45c022021-11-27 09:25:529#include "base/memory/raw_ptr.h"
Łukasz Anforowicz7447c672023-05-25 16:34:5710#include "base/memory/raw_ptr_exclusion.h"
enneea15ddb2017-03-22 01:49:0811#include "base/memory/ref_counted.h"
enne7b64edf32017-02-16 20:10:0212#include "build/build_config.h"
Tao Bai1e1deb412019-07-21 16:14:0013#include "cc/paint/node_id.h"
enne34f6084c2017-02-02 22:39:0814#include "cc/paint/paint_export.h"
vmpstr94cfa882017-04-14 01:19:3515#include "cc/paint/paint_image.h"
Avery Musbachdc01059f2022-01-12 22:10:5916#include "cc/paint/skottie_color_map.h"
Eric Sum4cf51cc2021-11-09 02:21:3817#include "cc/paint/skottie_frame_data.h"
Eric Sumc7435712022-02-22 18:33:4418#include "cc/paint/skottie_text_property_value.h"
enne34f6084c2017-02-02 22:39:0819#include "third_party/skia/include/core/SkCanvas.h"
Peter Kasting73ee7ef2021-07-20 19:47:5620
21class SkTextBlob;
enne34f6084c2017-02-02 22:39:0822
Florin Malita15935772019-02-06 21:52:2423namespace printing {
24class MetafileSkia;
25} // namespace printing
26
ckitagawa53eeaef2019-11-08 15:17:3427namespace paint_preview {
28class PaintPreviewTracker;
29} // namespace paint_preview
30
enne34f6084c2017-02-02 22:39:0831namespace cc {
Malay Keshavcf4ceef12018-10-19 17:56:5132class SkottieWrapper;
enned2501572017-03-09 19:59:1733class PaintFlags;
Xianzhu Wangdee05a02022-12-19 22:06:0934class PaintRecord;
enne59df29de2017-05-02 03:23:3735
Justin Novosad2756a7d52021-06-09 15:50:3836enum class UsePaintCache { kDisabled = 0, kEnabled };
37
Adrienne Walker26aac6e52018-10-23 19:23:3738// PaintCanvas is the cc/paint wrapper of SkCanvas. It has a more restricted
39// interface than SkCanvas (trimmed back to only what Chrome uses). Its reason
40// for existence is so that it can do custom serialization logic into a
41// PaintOpBuffer which (unlike SkPicture) is mutable, handles image replacement,
42// and can be serialized in custom ways (such as using the transfer cache).
43//
44// PaintCanvas is usually implemented by either:
45// (1) SkiaPaintCanvas, which is backed by an SkCanvas, usually for rasterizing.
46// (2) RecordPaintCanvas, which records paint commands into a PaintOpBuffer.
47//
48// SkiaPaintCanvas allows callers to go from PaintCanvas to SkCanvas (or
49// PaintRecord to SkPicture), but this is a one way trip. There is no way to go
50// from SkCanvas to PaintCanvas or from SkPicture back into PaintRecord.
enned2501572017-03-09 19:59:1751class CC_PAINT_EXPORT PaintCanvas {
enne34f6084c2017-02-02 22:39:0852 public:
Vladimir Levinf06d1cd72019-03-13 18:24:1053 PaintCanvas() = default;
54 PaintCanvas(const PaintCanvas&) = delete;
55 virtual ~PaintCanvas() = default;
56
57 PaintCanvas& operator=(const PaintCanvas&) = delete;
khushalsagarb74f9122017-03-22 04:37:4758
enne59df29de2017-05-02 03:23:3759 // TODO(enne): this only appears to mostly be used to determine if this is
60 // recording or not, so could be simplified or removed.
enne98c9f8052017-03-15 19:38:2261 virtual SkImageInfo imageInfo() const = 0;
enned2501572017-03-09 19:59:1762
Tom Andersonbec09512019-09-16 20:32:1663 virtual void* accessTopLayerPixels(SkImageInfo* info,
64 size_t* rowBytes,
65 SkIPoint* origin = nullptr) = 0;
66
enned2501572017-03-09 19:59:1767 // TODO(enne): It would be nice to get rid of flush() entirely, as it
68 // doesn't really make sense for recording. However, this gets used by
zhuoyu.qian4689dde22017-10-16 04:11:4869 // PaintCanvasVideoRenderer which takes a PaintCanvas to paint both
enned2501572017-03-09 19:59:1770 // software and hardware video. This is super entangled with ImageBuffer
71 // and canvas/video painting in Blink where the same paths are used for
72 // both recording and gpu work.
enne98c9f8052017-03-15 19:38:2273 virtual void flush() = 0;
enned2501572017-03-09 19:59:1774
enne98c9f8052017-03-15 19:38:2275 virtual int save() = 0;
Xianzhu Wang33e63232022-12-24 18:30:3776 virtual int saveLayer(const PaintFlags& flags) = 0;
77 virtual int saveLayer(const SkRect& bounds, const PaintFlags& flags) = 0;
Jean-Philippe Gravel3f6f3312023-01-19 21:10:3378 virtual int saveLayerAlphaf(float alpha) = 0;
79 virtual int saveLayerAlphaf(const SkRect& bounds, float alpha) = 0;
enne98c9f8052017-03-15 19:38:2280
81 virtual void restore() = 0;
82 virtual int getSaveCount() const = 0;
83 virtual void restoreToCount(int save_count) = 0;
84 virtual void translate(SkScalar dx, SkScalar dy) = 0;
85 virtual void scale(SkScalar sx, SkScalar sy) = 0;
Vladimir Levin32c16e132021-11-19 20:26:4886 void scale(SkScalar s) { scale(s, s); }
enne98c9f8052017-03-15 19:38:2287 virtual void rotate(SkScalar degrees) = 0;
Aaron Krajeski8a128e232020-12-15 21:47:0688 virtual void concat(const SkM44& matrix) = 0;
Aaron Krajeski5e542c82020-12-05 01:50:2189 virtual void setMatrix(const SkM44& matrix) = 0;
90
enne98c9f8052017-03-15 19:38:2291 virtual void clipRect(const SkRect& rect,
92 SkClipOp op,
93 bool do_anti_alias) = 0;
94 void clipRect(const SkRect& rect, SkClipOp op) { clipRect(rect, op, false); }
95 void clipRect(const SkRect& rect, bool do_anti_alias) {
96 clipRect(rect, SkClipOp::kIntersect, do_anti_alias);
enned2501572017-03-09 19:59:1797 }
enne98c9f8052017-03-15 19:38:2298 void clipRect(const SkRect& rect) {
99 clipRect(rect, SkClipOp::kIntersect, false);
enned2501572017-03-09 19:59:17100 }
101
enne98c9f8052017-03-15 19:38:22102 virtual void clipRRect(const SkRRect& rrect,
103 SkClipOp op,
104 bool do_anti_alias) = 0;
105 void clipRRect(const SkRRect& rrect, bool do_anti_alias) {
106 clipRRect(rrect, SkClipOp::kIntersect, do_anti_alias);
enned2501572017-03-09 19:59:17107 }
enne98c9f8052017-03-15 19:38:22108 void clipRRect(const SkRRect& rrect, SkClipOp op) {
109 clipRRect(rrect, op, false);
enned2501572017-03-09 19:59:17110 }
enne98c9f8052017-03-15 19:38:22111 void clipRRect(const SkRRect& rrect) {
112 clipRRect(rrect, SkClipOp::kIntersect, false);
enned2501572017-03-09 19:59:17113 }
enned2501572017-03-09 19:59:17114
enne98c9f8052017-03-15 19:38:22115 virtual void clipPath(const SkPath& path,
116 SkClipOp op,
Justin Novosad2756a7d52021-06-09 15:50:38117 bool do_anti_alias,
118 UsePaintCache) = 0;
119 void clipPath(const SkPath& path, SkClipOp op, bool do_anti_alias) {
120 clipPath(path, op, do_anti_alias, UsePaintCache::kEnabled);
121 }
122 void clipPath(const SkPath& path, SkClipOp op) {
123 clipPath(path, op, /*do_anti_alias=*/false, UsePaintCache::kEnabled);
124 }
enne98c9f8052017-03-15 19:38:22125 void clipPath(const SkPath& path, bool do_anti_alias) {
Justin Novosad2756a7d52021-06-09 15:50:38126 clipPath(path, SkClipOp::kIntersect, do_anti_alias,
127 UsePaintCache::kEnabled);
enned2501572017-03-09 19:59:17128 }
enne98c9f8052017-03-15 19:38:22129
enne98c9f8052017-03-15 19:38:22130 virtual bool getLocalClipBounds(SkRect* bounds) const = 0;
enne98c9f8052017-03-15 19:38:22131 virtual bool getDeviceClipBounds(SkIRect* bounds) const = 0;
Aaron Krajeski859caf62022-07-13 21:38:14132 virtual void drawColor(SkColor4f color, SkBlendMode mode) = 0;
133 void drawColor(SkColor4f color) { drawColor(color, SkBlendMode::kSrcOver); }
enne59df29de2017-05-02 03:23:37134
135 // TODO(enne): This is a synonym for drawColor with kSrc. Remove it.
Aaron Krajeskif325d982022-07-08 16:12:35136 virtual void clear(SkColor4f color) = 0;
enne98c9f8052017-03-15 19:38:22137
138 virtual void drawLine(SkScalar x0,
139 SkScalar y0,
140 SkScalar x1,
141 SkScalar y1,
142 const PaintFlags& flags) = 0;
143 virtual void drawRect(const SkRect& rect, const PaintFlags& flags) = 0;
144 virtual void drawIRect(const SkIRect& rect, const PaintFlags& flags) = 0;
145 virtual void drawOval(const SkRect& oval, const PaintFlags& flags) = 0;
146 virtual void drawRRect(const SkRRect& rrect, const PaintFlags& flags) = 0;
147 virtual void drawDRRect(const SkRRect& outer,
148 const SkRRect& inner,
149 const PaintFlags& flags) = 0;
enne98c9f8052017-03-15 19:38:22150 virtual void drawRoundRect(const SkRect& rect,
151 SkScalar rx,
152 SkScalar ry,
153 const PaintFlags& flags) = 0;
Justin Novosad2756a7d52021-06-09 15:50:38154 virtual void drawPath(const SkPath& path,
155 const PaintFlags& flags,
156 UsePaintCache) = 0;
157 void drawPath(const SkPath& path, const PaintFlags& flags) {
158 drawPath(path, flags, UsePaintCache::kEnabled);
159 }
vmpstr94cfa882017-04-14 01:19:35160 virtual void drawImage(const PaintImage& image,
enne98c9f8052017-03-15 19:38:22161 SkScalar left,
162 SkScalar top,
Mike Reed859086a2021-01-23 18:19:15163 const SkSamplingOptions&,
enne98c9f8052017-03-15 19:38:22164 const PaintFlags* flags) = 0;
vmpstr94cfa882017-04-14 01:19:35165 void drawImage(const PaintImage& image, SkScalar left, SkScalar top) {
Mike Reed859086a2021-01-23 18:19:15166 drawImage(image, left, top, SkSamplingOptions(), nullptr);
enned2501572017-03-09 19:59:17167 }
168
vmpstr94cfa882017-04-14 01:19:35169 virtual void drawImageRect(const PaintImage& image,
enne98c9f8052017-03-15 19:38:22170 const SkRect& src,
171 const SkRect& dst,
Mike Reed859086a2021-01-23 18:19:15172 const SkSamplingOptions&,
enne98c9f8052017-03-15 19:38:22173 const PaintFlags* flags,
jongdeok.kim50b377072020-05-15 22:16:12174 SkCanvas::SrcRectConstraint constraint) = 0;
Mike Reed859086a2021-01-23 18:19:15175 void drawImageRect(const PaintImage& image,
176 const SkRect& src,
177 const SkRect& dst,
178 SkCanvas::SrcRectConstraint constraint) {
179 drawImageRect(image, src, dst, SkSamplingOptions(), nullptr, constraint);
180 }
enned2501572017-03-09 19:59:17181
Malay Keshavcf4ceef12018-10-19 17:56:51182 // Draws the frame of the |skottie| animation specified by the normalized time
183 // t [0->first frame..1->last frame] at the destination bounds given by |dst|
Eric Sum4cf51cc2021-11-09 02:21:38184 // onto the canvas. |images| is a map from asset id to the corresponding image
185 // to use when rendering this frame; it may be empty if this animation frame
186 // does not contain any images in it.
Malay Keshavcf4ceef12018-10-19 17:56:51187 virtual void drawSkottie(scoped_refptr<SkottieWrapper> skottie,
188 const SkRect& dst,
Eric Sum4cf51cc2021-11-09 02:21:38189 float t,
Avery Musbachdc01059f2022-01-12 22:10:59190 SkottieFrameDataMap images,
Eric Sumc7435712022-02-22 18:33:44191 const SkottieColorMap& color_map,
192 SkottieTextPropertyValueMap text_map) = 0;
Malay Keshavcf4ceef12018-10-19 17:56:51193
Khushal464808c92018-10-19 23:45:59194 virtual void drawTextBlob(sk_sp<SkTextBlob> blob,
enne98c9f8052017-03-15 19:38:22195 SkScalar x,
196 SkScalar y,
197 const PaintFlags& flags) = 0;
enned2501572017-03-09 19:59:17198
Tao Baiedaf87092019-01-30 23:12:54199 virtual void drawTextBlob(sk_sp<SkTextBlob> blob,
200 SkScalar x,
201 SkScalar y,
Tao Bai1e1deb412019-07-21 16:14:00202 NodeId node_id,
203 const PaintFlags& flags) = 0;
Tao Baiedaf87092019-01-30 23:12:54204
Adrienne Walker15ce23502017-05-08 18:08:15205 // Unlike SkCanvas::drawPicture, this only plays back the PaintRecord and does
206 // not add an additional clip. This is closer to SkPicture::playback.
Xianzhu Wangdee05a02022-12-19 22:06:09207 virtual void drawPicture(PaintRecord record) = 0;
enned2501572017-03-09 19:59:17208
Aaron Krajeskie18de512021-04-15 15:17:03209 virtual SkM44 getLocalToDevice() const = 0;
enned2501572017-03-09 19:59:17210
Justin Novosaddae425072021-11-11 19:50:43211 virtual bool NeedsFlush() const = 0;
212
Florin Malita15935772019-02-06 21:52:24213 // Used for printing
enne5fa117a2017-04-04 20:56:58214 enum class AnnotationType {
Ho Cheung06365192023-09-14 17:09:54215 kUrl,
216 kNameDestination,
217 kLinkToDestination,
enne5fa117a2017-04-04 20:56:58218 };
219 virtual void Annotate(AnnotationType type,
220 const SkRect& rect,
221 sk_sp<SkData> data) = 0;
Florin Malita15935772019-02-06 21:52:24222 printing::MetafileSkia* GetPrintingMetafile() const { return metafile_; }
223 void SetPrintingMetafile(printing::MetafileSkia* metafile) {
224 metafile_ = metafile;
225 }
ckitagawa53eeaef2019-11-08 15:17:34226 paint_preview::PaintPreviewTracker* GetPaintPreviewTracker() const {
227 return tracker_;
228 }
229 void SetPaintPreviewTracker(paint_preview::PaintPreviewTracker* tracker) {
230 tracker_ = tracker;
231 }
enned2501572017-03-09 19:59:17232
Wei Li2a9bfe42018-01-13 05:42:56233 // Subclasses can override to handle custom data.
234 virtual void recordCustomData(uint32_t id) {}
235
Dominic Mazzonicf5d6b02020-03-06 19:49:12236 // Used for marked content in PDF files.
237 virtual void setNodeId(int) = 0;
238
enne59df29de2017-05-02 03:23:37239 private:
Lei Zhang2b17b612023-08-24 16:05:05240 raw_ptr<printing::MetafileSkia> metafile_ = nullptr;
Łukasz Anforowicz7447c672023-05-25 16:34:57241 // This field is not a raw_ptr<> because it was filtered by the rewriter for:
242 // #constexpr-ctor-field-initializer
243 RAW_PTR_EXCLUSION paint_preview::PaintPreviewTracker* tracker_ = nullptr;
enne34f6084c2017-02-02 22:39:08244};
245
enned2501572017-03-09 19:59:17246class CC_PAINT_EXPORT PaintCanvasAutoRestore {
247 public:
enne98c9f8052017-03-15 19:38:22248 PaintCanvasAutoRestore(PaintCanvas* canvas, bool save) : canvas_(canvas) {
enned2501572017-03-09 19:59:17249 if (canvas_) {
250 save_count_ = canvas_->getSaveCount();
251 if (save) {
252 canvas_->save();
253 }
254 }
255 }
256
enne98c9f8052017-03-15 19:38:22257 ~PaintCanvasAutoRestore() {
enned2501572017-03-09 19:59:17258 if (canvas_) {
259 canvas_->restoreToCount(save_count_);
260 }
261 }
262
enne98c9f8052017-03-15 19:38:22263 void restore() {
enned2501572017-03-09 19:59:17264 if (canvas_) {
265 canvas_->restoreToCount(save_count_);
266 canvas_ = nullptr;
267 }
268 }
269
270 private:
Keishi Hattori0e45c022021-11-27 09:25:52271 raw_ptr<PaintCanvas> canvas_ = nullptr;
enned2501572017-03-09 19:59:17272 int save_count_ = 0;
273};
274
enne34f6084c2017-02-02 22:39:08275} // namespace cc
276
277#endif // CC_PAINT_PAINT_CANVAS_H_