[go: nahoru, domu]

blob: 2b86174f7fe7282732f1d3576798b87756b14de8 [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"
Florin Malita3efd85352024-01-12 19:25:4015#include "cc/paint/refcounted_buffer.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 Krajeski859caf632022-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
Florin Malita3efd85352024-01-12 19:25:40182 virtual void drawVertices(scoped_refptr<RefCountedBuffer<SkPoint>> vertices,
183 scoped_refptr<RefCountedBuffer<SkPoint>> uvs,
184 scoped_refptr<RefCountedBuffer<uint16_t>> indices,
185 const PaintFlags& flags) = 0;
186
Malay Keshavcf4ceef12018-10-19 17:56:51187 // Draws the frame of the |skottie| animation specified by the normalized time
188 // t [0->first frame..1->last frame] at the destination bounds given by |dst|
Eric Sum4cf51cc2021-11-09 02:21:38189 // onto the canvas. |images| is a map from asset id to the corresponding image
190 // to use when rendering this frame; it may be empty if this animation frame
191 // does not contain any images in it.
Malay Keshavcf4ceef12018-10-19 17:56:51192 virtual void drawSkottie(scoped_refptr<SkottieWrapper> skottie,
193 const SkRect& dst,
Eric Sum4cf51cc2021-11-09 02:21:38194 float t,
Avery Musbachdc01059f2022-01-12 22:10:59195 SkottieFrameDataMap images,
Eric Sumc7435712022-02-22 18:33:44196 const SkottieColorMap& color_map,
197 SkottieTextPropertyValueMap text_map) = 0;
Malay Keshavcf4ceef12018-10-19 17:56:51198
Khushal464808c92018-10-19 23:45:59199 virtual void drawTextBlob(sk_sp<SkTextBlob> blob,
enne98c9f8052017-03-15 19:38:22200 SkScalar x,
201 SkScalar y,
202 const PaintFlags& flags) = 0;
enned2501572017-03-09 19:59:17203
Tao Baiedaf87092019-01-30 23:12:54204 virtual void drawTextBlob(sk_sp<SkTextBlob> blob,
205 SkScalar x,
206 SkScalar y,
Tao Bai1e1deb412019-07-21 16:14:00207 NodeId node_id,
208 const PaintFlags& flags) = 0;
Tao Baiedaf87092019-01-30 23:12:54209
Adrienne Walker15ce23502017-05-08 18:08:15210 // Unlike SkCanvas::drawPicture, this only plays back the PaintRecord and does
211 // not add an additional clip. This is closer to SkPicture::playback.
Xianzhu Wangdee05a02022-12-19 22:06:09212 virtual void drawPicture(PaintRecord record) = 0;
enned2501572017-03-09 19:59:17213
Aaron Krajeskie18de512021-04-15 15:17:03214 virtual SkM44 getLocalToDevice() const = 0;
enned2501572017-03-09 19:59:17215
Justin Novosaddae425072021-11-11 19:50:43216 virtual bool NeedsFlush() const = 0;
217
Florin Malita15935772019-02-06 21:52:24218 // Used for printing
enne5fa117a2017-04-04 20:56:58219 enum class AnnotationType {
Ho Cheung06365192023-09-14 17:09:54220 kUrl,
221 kNameDestination,
222 kLinkToDestination,
enne5fa117a2017-04-04 20:56:58223 };
224 virtual void Annotate(AnnotationType type,
225 const SkRect& rect,
226 sk_sp<SkData> data) = 0;
Florin Malita15935772019-02-06 21:52:24227 printing::MetafileSkia* GetPrintingMetafile() const { return metafile_; }
228 void SetPrintingMetafile(printing::MetafileSkia* metafile) {
229 metafile_ = metafile;
230 }
ckitagawa53eeaef2019-11-08 15:17:34231 paint_preview::PaintPreviewTracker* GetPaintPreviewTracker() const {
232 return tracker_;
233 }
234 void SetPaintPreviewTracker(paint_preview::PaintPreviewTracker* tracker) {
235 tracker_ = tracker;
236 }
enned2501572017-03-09 19:59:17237
Wei Li2a9bfe42018-01-13 05:42:56238 // Subclasses can override to handle custom data.
239 virtual void recordCustomData(uint32_t id) {}
240
Dominic Mazzonicf5d6b02020-03-06 19:49:12241 // Used for marked content in PDF files.
242 virtual void setNodeId(int) = 0;
243
enne59df29de2017-05-02 03:23:37244 private:
Lei Zhang2b17b612023-08-24 16:05:05245 raw_ptr<printing::MetafileSkia> metafile_ = nullptr;
Bartek Nowierskif5eeeba2024-01-25 12:49:39246 raw_ptr<paint_preview::PaintPreviewTracker, DanglingUntriaged> tracker_ =
247 nullptr;
enne34f6084c2017-02-02 22:39:08248};
249
enned2501572017-03-09 19:59:17250class CC_PAINT_EXPORT PaintCanvasAutoRestore {
251 public:
enne98c9f8052017-03-15 19:38:22252 PaintCanvasAutoRestore(PaintCanvas* canvas, bool save) : canvas_(canvas) {
enned2501572017-03-09 19:59:17253 if (canvas_) {
254 save_count_ = canvas_->getSaveCount();
255 if (save) {
256 canvas_->save();
257 }
258 }
259 }
260
enne98c9f8052017-03-15 19:38:22261 ~PaintCanvasAutoRestore() {
enned2501572017-03-09 19:59:17262 if (canvas_) {
263 canvas_->restoreToCount(save_count_);
264 }
265 }
266
enne98c9f8052017-03-15 19:38:22267 void restore() {
enned2501572017-03-09 19:59:17268 if (canvas_) {
269 canvas_->restoreToCount(save_count_);
270 canvas_ = nullptr;
271 }
272 }
273
274 private:
Keishi Hattori0e45c022021-11-27 09:25:52275 raw_ptr<PaintCanvas> canvas_ = nullptr;
enned2501572017-03-09 19:59:17276 int save_count_ = 0;
277};
278
enne34f6084c2017-02-02 22:39:08279} // namespace cc
280
281#endif // CC_PAINT_PAINT_CANVAS_H_