[go: nahoru, domu]

blob: bd32f3d968b2bb0521dea55888364d063522bfdf [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"
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;
enne59df29de2017-05-02 03:23:3733class PaintOpBuffer;
34
Justin Novosad2756a7d52021-06-09 15:50:3835enum class UsePaintCache { kDisabled = 0, kEnabled };
36
enne59df29de2017-05-02 03:23:3737using PaintRecord = PaintOpBuffer;
enne34f6084c2017-02-02 22:39:0838
Adrienne Walker26aac6e52018-10-23 19:23:3739// PaintCanvas is the cc/paint wrapper of SkCanvas. It has a more restricted
40// interface than SkCanvas (trimmed back to only what Chrome uses). Its reason
41// for existence is so that it can do custom serialization logic into a
42// PaintOpBuffer which (unlike SkPicture) is mutable, handles image replacement,
43// and can be serialized in custom ways (such as using the transfer cache).
44//
45// PaintCanvas is usually implemented by either:
46// (1) SkiaPaintCanvas, which is backed by an SkCanvas, usually for rasterizing.
47// (2) RecordPaintCanvas, which records paint commands into a PaintOpBuffer.
48//
49// SkiaPaintCanvas allows callers to go from PaintCanvas to SkCanvas (or
50// PaintRecord to SkPicture), but this is a one way trip. There is no way to go
51// from SkCanvas to PaintCanvas or from SkPicture back into PaintRecord.
enned2501572017-03-09 19:59:1752class CC_PAINT_EXPORT PaintCanvas {
enne34f6084c2017-02-02 22:39:0853 public:
Vladimir Levinf06d1cd72019-03-13 18:24:1054 PaintCanvas() = default;
55 PaintCanvas(const PaintCanvas&) = delete;
56 virtual ~PaintCanvas() = default;
57
58 PaintCanvas& operator=(const PaintCanvas&) = delete;
khushalsagarb74f9122017-03-22 04:37:4759
enne59df29de2017-05-02 03:23:3760 // TODO(enne): this only appears to mostly be used to determine if this is
61 // recording or not, so could be simplified or removed.
enne98c9f8052017-03-15 19:38:2262 virtual SkImageInfo imageInfo() const = 0;
enned2501572017-03-09 19:59:1763
Tom Andersonbec09512019-09-16 20:32:1664 virtual void* accessTopLayerPixels(SkImageInfo* info,
65 size_t* rowBytes,
66 SkIPoint* origin = nullptr) = 0;
67
enned2501572017-03-09 19:59:1768 // TODO(enne): It would be nice to get rid of flush() entirely, as it
69 // doesn't really make sense for recording. However, this gets used by
zhuoyu.qian4689dde22017-10-16 04:11:4870 // PaintCanvasVideoRenderer which takes a PaintCanvas to paint both
enned2501572017-03-09 19:59:1771 // software and hardware video. This is super entangled with ImageBuffer
72 // and canvas/video painting in Blink where the same paths are used for
73 // both recording and gpu work.
enne98c9f8052017-03-15 19:38:2274 virtual void flush() = 0;
enned2501572017-03-09 19:59:1775
enne98c9f8052017-03-15 19:38:2276 virtual int save() = 0;
77 virtual int saveLayer(const SkRect* bounds, const PaintFlags* flags) = 0;
Peter Kastingfe4ba932019-01-07 20:21:0578 virtual int saveLayerAlpha(const SkRect* bounds, uint8_t 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 Krajeski5e542c82020-12-05 01:50:2187 // TODO(aaronhk): crbug.com/1153330 deprecate these in favor of the SkM44
Aaron Krajeski8a128e232020-12-15 21:47:0688 // versions.
enne98c9f8052017-03-15 19:38:2289 virtual void concat(const SkMatrix& matrix) = 0;
90 virtual void setMatrix(const SkMatrix& matrix) = 0;
Aaron Krajeski8a128e232020-12-15 21:47:0691 virtual void concat(const SkM44& matrix) = 0;
Aaron Krajeski5e542c82020-12-05 01:50:2192 virtual void setMatrix(const SkM44& matrix) = 0;
93
enne98c9f8052017-03-15 19:38:2294 virtual void clipRect(const SkRect& rect,
95 SkClipOp op,
96 bool do_anti_alias) = 0;
97 void clipRect(const SkRect& rect, SkClipOp op) { clipRect(rect, op, false); }
98 void clipRect(const SkRect& rect, bool do_anti_alias) {
99 clipRect(rect, SkClipOp::kIntersect, do_anti_alias);
enned2501572017-03-09 19:59:17100 }
enne98c9f8052017-03-15 19:38:22101 void clipRect(const SkRect& rect) {
102 clipRect(rect, SkClipOp::kIntersect, false);
enned2501572017-03-09 19:59:17103 }
104
enne98c9f8052017-03-15 19:38:22105 virtual void clipRRect(const SkRRect& rrect,
106 SkClipOp op,
107 bool do_anti_alias) = 0;
108 void clipRRect(const SkRRect& rrect, bool do_anti_alias) {
109 clipRRect(rrect, SkClipOp::kIntersect, do_anti_alias);
enned2501572017-03-09 19:59:17110 }
enne98c9f8052017-03-15 19:38:22111 void clipRRect(const SkRRect& rrect, SkClipOp op) {
112 clipRRect(rrect, op, false);
enned2501572017-03-09 19:59:17113 }
enne98c9f8052017-03-15 19:38:22114 void clipRRect(const SkRRect& rrect) {
115 clipRRect(rrect, SkClipOp::kIntersect, false);
enned2501572017-03-09 19:59:17116 }
enned2501572017-03-09 19:59:17117
enne98c9f8052017-03-15 19:38:22118 virtual void clipPath(const SkPath& path,
119 SkClipOp op,
Justin Novosad2756a7d52021-06-09 15:50:38120 bool do_anti_alias,
121 UsePaintCache) = 0;
122 void clipPath(const SkPath& path, SkClipOp op, bool do_anti_alias) {
123 clipPath(path, op, do_anti_alias, UsePaintCache::kEnabled);
124 }
125 void clipPath(const SkPath& path, SkClipOp op) {
126 clipPath(path, op, /*do_anti_alias=*/false, UsePaintCache::kEnabled);
127 }
enne98c9f8052017-03-15 19:38:22128 void clipPath(const SkPath& path, bool do_anti_alias) {
Justin Novosad2756a7d52021-06-09 15:50:38129 clipPath(path, SkClipOp::kIntersect, do_anti_alias,
130 UsePaintCache::kEnabled);
enned2501572017-03-09 19:59:17131 }
enne98c9f8052017-03-15 19:38:22132
enne98c9f8052017-03-15 19:38:22133 virtual SkRect getLocalClipBounds() const = 0;
134 virtual bool getLocalClipBounds(SkRect* bounds) const = 0;
135 virtual SkIRect getDeviceClipBounds() const = 0;
136 virtual bool getDeviceClipBounds(SkIRect* bounds) const = 0;
137 virtual void drawColor(SkColor color, SkBlendMode mode) = 0;
138 void drawColor(SkColor color) { drawColor(color, SkBlendMode::kSrcOver); }
enne59df29de2017-05-02 03:23:37139
140 // TODO(enne): This is a synonym for drawColor with kSrc. Remove it.
enne98c9f8052017-03-15 19:38:22141 virtual void clear(SkColor color) = 0;
142
143 virtual void drawLine(SkScalar x0,
144 SkScalar y0,
145 SkScalar x1,
146 SkScalar y1,
147 const PaintFlags& flags) = 0;
148 virtual void drawRect(const SkRect& rect, const PaintFlags& flags) = 0;
149 virtual void drawIRect(const SkIRect& rect, const PaintFlags& flags) = 0;
150 virtual void drawOval(const SkRect& oval, const PaintFlags& flags) = 0;
151 virtual void drawRRect(const SkRRect& rrect, const PaintFlags& flags) = 0;
152 virtual void drawDRRect(const SkRRect& outer,
153 const SkRRect& inner,
154 const PaintFlags& flags) = 0;
enne98c9f8052017-03-15 19:38:22155 virtual void drawRoundRect(const SkRect& rect,
156 SkScalar rx,
157 SkScalar ry,
158 const PaintFlags& flags) = 0;
Justin Novosad2756a7d52021-06-09 15:50:38159 virtual void drawPath(const SkPath& path,
160 const PaintFlags& flags,
161 UsePaintCache) = 0;
162 void drawPath(const SkPath& path, const PaintFlags& flags) {
163 drawPath(path, flags, UsePaintCache::kEnabled);
164 }
vmpstr94cfa882017-04-14 01:19:35165 virtual void drawImage(const PaintImage& image,
enne98c9f8052017-03-15 19:38:22166 SkScalar left,
167 SkScalar top,
Mike Reed859086a2021-01-23 18:19:15168 const SkSamplingOptions&,
enne98c9f8052017-03-15 19:38:22169 const PaintFlags* flags) = 0;
vmpstr94cfa882017-04-14 01:19:35170 void drawImage(const PaintImage& image, SkScalar left, SkScalar top) {
Mike Reed859086a2021-01-23 18:19:15171 drawImage(image, left, top, SkSamplingOptions(), nullptr);
enned2501572017-03-09 19:59:17172 }
173
vmpstr94cfa882017-04-14 01:19:35174 virtual void drawImageRect(const PaintImage& image,
enne98c9f8052017-03-15 19:38:22175 const SkRect& src,
176 const SkRect& dst,
Mike Reed859086a2021-01-23 18:19:15177 const SkSamplingOptions&,
enne98c9f8052017-03-15 19:38:22178 const PaintFlags* flags,
jongdeok.kim50b377072020-05-15 22:16:12179 SkCanvas::SrcRectConstraint constraint) = 0;
Mike Reed859086a2021-01-23 18:19:15180 void drawImageRect(const PaintImage& image,
181 const SkRect& src,
182 const SkRect& dst,
183 SkCanvas::SrcRectConstraint constraint) {
184 drawImageRect(image, src, dst, SkSamplingOptions(), nullptr, constraint);
185 }
enned2501572017-03-09 19:59:17186
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.
ennedffda502017-03-23 20:46:43212 virtual void drawPicture(sk_sp<const PaintRecord> record) = 0;
enned2501572017-03-09 19:59:17213
enne98c9f8052017-03-15 19:38:22214 virtual bool isClipEmpty() const = 0;
Mike Reedf97e2d162020-01-07 15:26:43215 virtual SkMatrix getTotalMatrix() const = 0;
Aaron Krajeskie18de512021-04-15 15:17:03216 virtual SkM44 getLocalToDevice() const = 0;
enned2501572017-03-09 19:59:17217
Justin Novosaddae425072021-11-11 19:50:43218 virtual bool NeedsFlush() const = 0;
219
Florin Malita15935772019-02-06 21:52:24220 // Used for printing
enne5fa117a2017-04-04 20:56:58221 enum class AnnotationType {
222 URL,
223 NAMED_DESTINATION,
224 LINK_TO_DESTINATION,
225 };
226 virtual void Annotate(AnnotationType type,
227 const SkRect& rect,
228 sk_sp<SkData> data) = 0;
Florin Malita15935772019-02-06 21:52:24229 printing::MetafileSkia* GetPrintingMetafile() const { return metafile_; }
230 void SetPrintingMetafile(printing::MetafileSkia* metafile) {
231 metafile_ = metafile;
232 }
ckitagawa53eeaef2019-11-08 15:17:34233 paint_preview::PaintPreviewTracker* GetPaintPreviewTracker() const {
234 return tracker_;
235 }
236 void SetPaintPreviewTracker(paint_preview::PaintPreviewTracker* tracker) {
237 tracker_ = tracker;
238 }
enned2501572017-03-09 19:59:17239
Wei Li2a9bfe42018-01-13 05:42:56240 // Subclasses can override to handle custom data.
241 virtual void recordCustomData(uint32_t id) {}
242
Dominic Mazzonicf5d6b02020-03-06 19:49:12243 // Used for marked content in PDF files.
244 virtual void setNodeId(int) = 0;
245
enne59df29de2017-05-02 03:23:37246 private:
Florin Malita15935772019-02-06 21:52:24247 printing::MetafileSkia* metafile_ = nullptr;
ckitagawa53eeaef2019-11-08 15:17:34248 paint_preview::PaintPreviewTracker* tracker_ = nullptr;
enne34f6084c2017-02-02 22:39:08249};
250
enned2501572017-03-09 19:59:17251class CC_PAINT_EXPORT PaintCanvasAutoRestore {
252 public:
enne98c9f8052017-03-15 19:38:22253 PaintCanvasAutoRestore(PaintCanvas* canvas, bool save) : canvas_(canvas) {
enned2501572017-03-09 19:59:17254 if (canvas_) {
255 save_count_ = canvas_->getSaveCount();
256 if (save) {
257 canvas_->save();
258 }
259 }
260 }
261
enne98c9f8052017-03-15 19:38:22262 ~PaintCanvasAutoRestore() {
enned2501572017-03-09 19:59:17263 if (canvas_) {
264 canvas_->restoreToCount(save_count_);
265 }
266 }
267
enne98c9f8052017-03-15 19:38:22268 void restore() {
enned2501572017-03-09 19:59:17269 if (canvas_) {
270 canvas_->restoreToCount(save_count_);
271 canvas_ = nullptr;
272 }
273 }
274
275 private:
Keishi Hattori0e45c022021-11-27 09:25:52276 raw_ptr<PaintCanvas> canvas_ = nullptr;
enned2501572017-03-09 19:59:17277 int save_count_ = 0;
278};
279
enne34f6084c2017-02-02 22:39:08280} // namespace cc
281
282#endif // CC_PAINT_PAINT_CANVAS_H_