[go: nahoru, domu]

blob: 1b2e0321745a1b3a30f4d2e087e35063b97ae58d [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"
enneea15ddb2017-03-22 01:49:0810#include "base/memory/ref_counted.h"
enne7b64edf32017-02-16 20:10:0211#include "build/build_config.h"
Tao Bai1e1deb412019-07-21 16:14:0012#include "cc/paint/node_id.h"
enne34f6084c2017-02-02 22:39:0813#include "cc/paint/paint_export.h"
vmpstr94cfa882017-04-14 01:19:3514#include "cc/paint/paint_image.h"
Avery Musbachdc01059f2022-01-12 22:10:5915#include "cc/paint/skottie_color_map.h"
Eric Sum4cf51cc2021-11-09 02:21:3816#include "cc/paint/skottie_frame_data.h"
Eric Sumc7435712022-02-22 18:33:4417#include "cc/paint/skottie_text_property_value.h"
enne34f6084c2017-02-02 22:39:0818#include "third_party/skia/include/core/SkCanvas.h"
Peter Kasting73ee7ef2021-07-20 19:47:5619
20class SkTextBlob;
enne34f6084c2017-02-02 22:39:0821
Florin Malita15935772019-02-06 21:52:2422namespace printing {
23class MetafileSkia;
24} // namespace printing
25
ckitagawa53eeaef2019-11-08 15:17:3426namespace paint_preview {
27class PaintPreviewTracker;
28} // namespace paint_preview
29
enne34f6084c2017-02-02 22:39:0830namespace cc {
Malay Keshavcf4ceef12018-10-19 17:56:5131class SkottieWrapper;
enned2501572017-03-09 19:59:1732class PaintFlags;
Xianzhu Wangdee05a02022-12-19 22:06:0933class PaintRecord;
enne59df29de2017-05-02 03:23:3734
Justin Novosad2756a7d52021-06-09 15:50:3835enum class UsePaintCache { kDisabled = 0, kEnabled };
36
Adrienne Walker26aac6e52018-10-23 19:23:3737// PaintCanvas is the cc/paint wrapper of SkCanvas. It has a more restricted
38// interface than SkCanvas (trimmed back to only what Chrome uses). Its reason
39// for existence is so that it can do custom serialization logic into a
40// PaintOpBuffer which (unlike SkPicture) is mutable, handles image replacement,
41// and can be serialized in custom ways (such as using the transfer cache).
42//
43// PaintCanvas is usually implemented by either:
44// (1) SkiaPaintCanvas, which is backed by an SkCanvas, usually for rasterizing.
45// (2) RecordPaintCanvas, which records paint commands into a PaintOpBuffer.
46//
47// SkiaPaintCanvas allows callers to go from PaintCanvas to SkCanvas (or
48// PaintRecord to SkPicture), but this is a one way trip. There is no way to go
49// from SkCanvas to PaintCanvas or from SkPicture back into PaintRecord.
enned2501572017-03-09 19:59:1750class CC_PAINT_EXPORT PaintCanvas {
enne34f6084c2017-02-02 22:39:0851 public:
Vladimir Levinf06d1cd72019-03-13 18:24:1052 PaintCanvas() = default;
53 PaintCanvas(const PaintCanvas&) = delete;
54 virtual ~PaintCanvas() = default;
55
56 PaintCanvas& operator=(const PaintCanvas&) = delete;
khushalsagarb74f9122017-03-22 04:37:4757
enne59df29de2017-05-02 03:23:3758 // TODO(enne): this only appears to mostly be used to determine if this is
59 // recording or not, so could be simplified or removed.
enne98c9f8052017-03-15 19:38:2260 virtual SkImageInfo imageInfo() const = 0;
enned2501572017-03-09 19:59:1761
Tom Andersonbec09512019-09-16 20:32:1662 virtual void* accessTopLayerPixels(SkImageInfo* info,
63 size_t* rowBytes,
64 SkIPoint* origin = nullptr) = 0;
65
enned2501572017-03-09 19:59:1766 // TODO(enne): It would be nice to get rid of flush() entirely, as it
67 // doesn't really make sense for recording. However, this gets used by
zhuoyu.qian4689dde22017-10-16 04:11:4868 // PaintCanvasVideoRenderer which takes a PaintCanvas to paint both
enned2501572017-03-09 19:59:1769 // software and hardware video. This is super entangled with ImageBuffer
70 // and canvas/video painting in Blink where the same paths are used for
71 // both recording and gpu work.
enne98c9f8052017-03-15 19:38:2272 virtual void flush() = 0;
enned2501572017-03-09 19:59:1773
enne98c9f8052017-03-15 19:38:2274 virtual int save() = 0;
Xianzhu Wang33e63232022-12-24 18:30:3775 virtual int saveLayer(const PaintFlags& flags) = 0;
76 virtual int saveLayer(const SkRect& bounds, const PaintFlags& flags) = 0;
Jean-Philippe Gravel3f6f3312023-01-19 21:10:3377 virtual int saveLayerAlphaf(float alpha) = 0;
78 virtual int saveLayerAlphaf(const SkRect& bounds, float alpha) = 0;
enne98c9f8052017-03-15 19:38:2279
80 virtual void restore() = 0;
81 virtual int getSaveCount() const = 0;
82 virtual void restoreToCount(int save_count) = 0;
83 virtual void translate(SkScalar dx, SkScalar dy) = 0;
84 virtual void scale(SkScalar sx, SkScalar sy) = 0;
Vladimir Levin32c16e132021-11-19 20:26:4885 void scale(SkScalar s) { scale(s, s); }
enne98c9f8052017-03-15 19:38:2286 virtual void rotate(SkScalar degrees) = 0;
Aaron Krajeski8a128e232020-12-15 21:47:0687 virtual void concat(const SkM44& matrix) = 0;
Aaron Krajeski5e542c82020-12-05 01:50:2188 virtual void setMatrix(const SkM44& matrix) = 0;
89
enne98c9f8052017-03-15 19:38:2290 virtual void clipRect(const SkRect& rect,
91 SkClipOp op,
92 bool do_anti_alias) = 0;
93 void clipRect(const SkRect& rect, SkClipOp op) { clipRect(rect, op, false); }
94 void clipRect(const SkRect& rect, bool do_anti_alias) {
95 clipRect(rect, SkClipOp::kIntersect, do_anti_alias);
enned2501572017-03-09 19:59:1796 }
enne98c9f8052017-03-15 19:38:2297 void clipRect(const SkRect& rect) {
98 clipRect(rect, SkClipOp::kIntersect, false);
enned2501572017-03-09 19:59:1799 }
100
enne98c9f8052017-03-15 19:38:22101 virtual void clipRRect(const SkRRect& rrect,
102 SkClipOp op,
103 bool do_anti_alias) = 0;
104 void clipRRect(const SkRRect& rrect, bool do_anti_alias) {
105 clipRRect(rrect, SkClipOp::kIntersect, do_anti_alias);
enned2501572017-03-09 19:59:17106 }
enne98c9f8052017-03-15 19:38:22107 void clipRRect(const SkRRect& rrect, SkClipOp op) {
108 clipRRect(rrect, op, false);
enned2501572017-03-09 19:59:17109 }
enne98c9f8052017-03-15 19:38:22110 void clipRRect(const SkRRect& rrect) {
111 clipRRect(rrect, SkClipOp::kIntersect, false);
enned2501572017-03-09 19:59:17112 }
enned2501572017-03-09 19:59:17113
enne98c9f8052017-03-15 19:38:22114 virtual void clipPath(const SkPath& path,
115 SkClipOp op,
Justin Novosad2756a7d52021-06-09 15:50:38116 bool do_anti_alias,
117 UsePaintCache) = 0;
118 void clipPath(const SkPath& path, SkClipOp op, bool do_anti_alias) {
119 clipPath(path, op, do_anti_alias, UsePaintCache::kEnabled);
120 }
121 void clipPath(const SkPath& path, SkClipOp op) {
122 clipPath(path, op, /*do_anti_alias=*/false, UsePaintCache::kEnabled);
123 }
enne98c9f8052017-03-15 19:38:22124 void clipPath(const SkPath& path, bool do_anti_alias) {
Justin Novosad2756a7d52021-06-09 15:50:38125 clipPath(path, SkClipOp::kIntersect, do_anti_alias,
126 UsePaintCache::kEnabled);
enned2501572017-03-09 19:59:17127 }
enne98c9f8052017-03-15 19:38:22128
enne98c9f8052017-03-15 19:38:22129 virtual bool getLocalClipBounds(SkRect* bounds) const = 0;
enne98c9f8052017-03-15 19:38:22130 virtual bool getDeviceClipBounds(SkIRect* bounds) const = 0;
Aaron Krajeski859caf62022-07-13 21:38:14131 virtual void drawColor(SkColor4f color, SkBlendMode mode) = 0;
132 void drawColor(SkColor4f color) { drawColor(color, SkBlendMode::kSrcOver); }
enne59df29de2017-05-02 03:23:37133
134 // TODO(enne): This is a synonym for drawColor with kSrc. Remove it.
Aaron Krajeskif325d982022-07-08 16:12:35135 virtual void clear(SkColor4f color) = 0;
enne98c9f8052017-03-15 19:38:22136
137 virtual void drawLine(SkScalar x0,
138 SkScalar y0,
139 SkScalar x1,
140 SkScalar y1,
141 const PaintFlags& flags) = 0;
142 virtual void drawRect(const SkRect& rect, const PaintFlags& flags) = 0;
143 virtual void drawIRect(const SkIRect& rect, const PaintFlags& flags) = 0;
144 virtual void drawOval(const SkRect& oval, const PaintFlags& flags) = 0;
145 virtual void drawRRect(const SkRRect& rrect, const PaintFlags& flags) = 0;
146 virtual void drawDRRect(const SkRRect& outer,
147 const SkRRect& inner,
148 const PaintFlags& flags) = 0;
enne98c9f8052017-03-15 19:38:22149 virtual void drawRoundRect(const SkRect& rect,
150 SkScalar rx,
151 SkScalar ry,
152 const PaintFlags& flags) = 0;
Justin Novosad2756a7d52021-06-09 15:50:38153 virtual void drawPath(const SkPath& path,
154 const PaintFlags& flags,
155 UsePaintCache) = 0;
156 void drawPath(const SkPath& path, const PaintFlags& flags) {
157 drawPath(path, flags, UsePaintCache::kEnabled);
158 }
vmpstr94cfa882017-04-14 01:19:35159 virtual void drawImage(const PaintImage& image,
enne98c9f8052017-03-15 19:38:22160 SkScalar left,
161 SkScalar top,
Mike Reed859086a2021-01-23 18:19:15162 const SkSamplingOptions&,
enne98c9f8052017-03-15 19:38:22163 const PaintFlags* flags) = 0;
vmpstr94cfa882017-04-14 01:19:35164 void drawImage(const PaintImage& image, SkScalar left, SkScalar top) {
Mike Reed859086a2021-01-23 18:19:15165 drawImage(image, left, top, SkSamplingOptions(), nullptr);
enned2501572017-03-09 19:59:17166 }
167
vmpstr94cfa882017-04-14 01:19:35168 virtual void drawImageRect(const PaintImage& image,
enne98c9f8052017-03-15 19:38:22169 const SkRect& src,
170 const SkRect& dst,
Mike Reed859086a2021-01-23 18:19:15171 const SkSamplingOptions&,
enne98c9f8052017-03-15 19:38:22172 const PaintFlags* flags,
jongdeok.kim50b377072020-05-15 22:16:12173 SkCanvas::SrcRectConstraint constraint) = 0;
Mike Reed859086a2021-01-23 18:19:15174 void drawImageRect(const PaintImage& image,
175 const SkRect& src,
176 const SkRect& dst,
177 SkCanvas::SrcRectConstraint constraint) {
178 drawImageRect(image, src, dst, SkSamplingOptions(), nullptr, constraint);
179 }
enned2501572017-03-09 19:59:17180
Malay Keshavcf4ceef12018-10-19 17:56:51181 // Draws the frame of the |skottie| animation specified by the normalized time
182 // t [0->first frame..1->last frame] at the destination bounds given by |dst|
Eric Sum4cf51cc2021-11-09 02:21:38183 // onto the canvas. |images| is a map from asset id to the corresponding image
184 // to use when rendering this frame; it may be empty if this animation frame
185 // does not contain any images in it.
Malay Keshavcf4ceef12018-10-19 17:56:51186 virtual void drawSkottie(scoped_refptr<SkottieWrapper> skottie,
187 const SkRect& dst,
Eric Sum4cf51cc2021-11-09 02:21:38188 float t,
Avery Musbachdc01059f2022-01-12 22:10:59189 SkottieFrameDataMap images,
Eric Sumc7435712022-02-22 18:33:44190 const SkottieColorMap& color_map,
191 SkottieTextPropertyValueMap text_map) = 0;
Malay Keshavcf4ceef12018-10-19 17:56:51192
Khushal464808c92018-10-19 23:45:59193 virtual void drawTextBlob(sk_sp<SkTextBlob> blob,
enne98c9f8052017-03-15 19:38:22194 SkScalar x,
195 SkScalar y,
196 const PaintFlags& flags) = 0;
enned2501572017-03-09 19:59:17197
Tao Baiedaf87092019-01-30 23:12:54198 virtual void drawTextBlob(sk_sp<SkTextBlob> blob,
199 SkScalar x,
200 SkScalar y,
Tao Bai1e1deb412019-07-21 16:14:00201 NodeId node_id,
202 const PaintFlags& flags) = 0;
Tao Baiedaf87092019-01-30 23:12:54203
Adrienne Walker15ce23502017-05-08 18:08:15204 // Unlike SkCanvas::drawPicture, this only plays back the PaintRecord and does
205 // not add an additional clip. This is closer to SkPicture::playback.
Xianzhu Wangdee05a02022-12-19 22:06:09206 virtual void drawPicture(PaintRecord record) = 0;
enned2501572017-03-09 19:59:17207
Aaron Krajeskie18de512021-04-15 15:17:03208 virtual SkM44 getLocalToDevice() const = 0;
enned2501572017-03-09 19:59:17209
Justin Novosaddae425072021-11-11 19:50:43210 virtual bool NeedsFlush() const = 0;
211
Florin Malita15935772019-02-06 21:52:24212 // Used for printing
enne5fa117a2017-04-04 20:56:58213 enum class AnnotationType {
214 URL,
215 NAMED_DESTINATION,
216 LINK_TO_DESTINATION,
217 };
218 virtual void Annotate(AnnotationType type,
219 const SkRect& rect,
220 sk_sp<SkData> data) = 0;
Florin Malita15935772019-02-06 21:52:24221 printing::MetafileSkia* GetPrintingMetafile() const { return metafile_; }
222 void SetPrintingMetafile(printing::MetafileSkia* metafile) {
223 metafile_ = metafile;
224 }
ckitagawa53eeaef2019-11-08 15:17:34225 paint_preview::PaintPreviewTracker* GetPaintPreviewTracker() const {
226 return tracker_;
227 }
228 void SetPaintPreviewTracker(paint_preview::PaintPreviewTracker* tracker) {
229 tracker_ = tracker;
230 }
enned2501572017-03-09 19:59:17231
Wei Li2a9bfe42018-01-13 05:42:56232 // Subclasses can override to handle custom data.
233 virtual void recordCustomData(uint32_t id) {}
234
Dominic Mazzonicf5d6b02020-03-06 19:49:12235 // Used for marked content in PDF files.
236 virtual void setNodeId(int) = 0;
237
enne59df29de2017-05-02 03:23:37238 private:
Florin Malita15935772019-02-06 21:52:24239 printing::MetafileSkia* metafile_ = nullptr;
ckitagawa53eeaef2019-11-08 15:17:34240 paint_preview::PaintPreviewTracker* tracker_ = nullptr;
enne34f6084c2017-02-02 22:39:08241};
242
enned2501572017-03-09 19:59:17243class CC_PAINT_EXPORT PaintCanvasAutoRestore {
244 public:
enne98c9f8052017-03-15 19:38:22245 PaintCanvasAutoRestore(PaintCanvas* canvas, bool save) : canvas_(canvas) {
enned2501572017-03-09 19:59:17246 if (canvas_) {
247 save_count_ = canvas_->getSaveCount();
248 if (save) {
249 canvas_->save();
250 }
251 }
252 }
253
enne98c9f8052017-03-15 19:38:22254 ~PaintCanvasAutoRestore() {
enned2501572017-03-09 19:59:17255 if (canvas_) {
256 canvas_->restoreToCount(save_count_);
257 }
258 }
259
enne98c9f8052017-03-15 19:38:22260 void restore() {
enned2501572017-03-09 19:59:17261 if (canvas_) {
262 canvas_->restoreToCount(save_count_);
263 canvas_ = nullptr;
264 }
265 }
266
267 private:
Keishi Hattori0e45c022021-11-27 09:25:52268 raw_ptr<PaintCanvas> canvas_ = nullptr;
enned2501572017-03-09 19:59:17269 int save_count_ = 0;
270};
271
enne34f6084c2017-02-02 22:39:08272} // namespace cc
273
274#endif // CC_PAINT_PAINT_CANVAS_H_