[go: nahoru, domu]

blob: 04e6bf713b7031b4e4c330929528fac64ce7e59d [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;
Aaron Krajeskif325d982022-07-08 16:12:35142 virtual void clear(SkColor4f color) = 0;
enne98c9f8052017-03-15 19:38:22143
144 virtual void drawLine(SkScalar x0,
145 SkScalar y0,
146 SkScalar x1,
147 SkScalar y1,
148 const PaintFlags& flags) = 0;
149 virtual void drawRect(const SkRect& rect, const PaintFlags& flags) = 0;
150 virtual void drawIRect(const SkIRect& rect, const PaintFlags& flags) = 0;
151 virtual void drawOval(const SkRect& oval, const PaintFlags& flags) = 0;
152 virtual void drawRRect(const SkRRect& rrect, const PaintFlags& flags) = 0;
153 virtual void drawDRRect(const SkRRect& outer,
154 const SkRRect& inner,
155 const PaintFlags& flags) = 0;
enne98c9f8052017-03-15 19:38:22156 virtual void drawRoundRect(const SkRect& rect,
157 SkScalar rx,
158 SkScalar ry,
159 const PaintFlags& flags) = 0;
Justin Novosad2756a7d52021-06-09 15:50:38160 virtual void drawPath(const SkPath& path,
161 const PaintFlags& flags,
162 UsePaintCache) = 0;
163 void drawPath(const SkPath& path, const PaintFlags& flags) {
164 drawPath(path, flags, UsePaintCache::kEnabled);
165 }
vmpstr94cfa882017-04-14 01:19:35166 virtual void drawImage(const PaintImage& image,
enne98c9f8052017-03-15 19:38:22167 SkScalar left,
168 SkScalar top,
Mike Reed859086a2021-01-23 18:19:15169 const SkSamplingOptions&,
enne98c9f8052017-03-15 19:38:22170 const PaintFlags* flags) = 0;
vmpstr94cfa882017-04-14 01:19:35171 void drawImage(const PaintImage& image, SkScalar left, SkScalar top) {
Mike Reed859086a2021-01-23 18:19:15172 drawImage(image, left, top, SkSamplingOptions(), nullptr);
enned2501572017-03-09 19:59:17173 }
174
vmpstr94cfa882017-04-14 01:19:35175 virtual void drawImageRect(const PaintImage& image,
enne98c9f8052017-03-15 19:38:22176 const SkRect& src,
177 const SkRect& dst,
Mike Reed859086a2021-01-23 18:19:15178 const SkSamplingOptions&,
enne98c9f8052017-03-15 19:38:22179 const PaintFlags* flags,
jongdeok.kim50b377072020-05-15 22:16:12180 SkCanvas::SrcRectConstraint constraint) = 0;
Mike Reed859086a2021-01-23 18:19:15181 void drawImageRect(const PaintImage& image,
182 const SkRect& src,
183 const SkRect& dst,
184 SkCanvas::SrcRectConstraint constraint) {
185 drawImageRect(image, src, dst, SkSamplingOptions(), nullptr, constraint);
186 }
enned2501572017-03-09 19:59:17187
Malay Keshavcf4ceef12018-10-19 17:56:51188 // Draws the frame of the |skottie| animation specified by the normalized time
189 // t [0->first frame..1->last frame] at the destination bounds given by |dst|
Eric Sum4cf51cc2021-11-09 02:21:38190 // onto the canvas. |images| is a map from asset id to the corresponding image
191 // to use when rendering this frame; it may be empty if this animation frame
192 // does not contain any images in it.
Malay Keshavcf4ceef12018-10-19 17:56:51193 virtual void drawSkottie(scoped_refptr<SkottieWrapper> skottie,
194 const SkRect& dst,
Eric Sum4cf51cc2021-11-09 02:21:38195 float t,
Avery Musbachdc01059f2022-01-12 22:10:59196 SkottieFrameDataMap images,
Eric Sumc7435712022-02-22 18:33:44197 const SkottieColorMap& color_map,
198 SkottieTextPropertyValueMap text_map) = 0;
Malay Keshavcf4ceef12018-10-19 17:56:51199
Khushal464808c92018-10-19 23:45:59200 virtual void drawTextBlob(sk_sp<SkTextBlob> blob,
enne98c9f8052017-03-15 19:38:22201 SkScalar x,
202 SkScalar y,
203 const PaintFlags& flags) = 0;
enned2501572017-03-09 19:59:17204
Tao Baiedaf87092019-01-30 23:12:54205 virtual void drawTextBlob(sk_sp<SkTextBlob> blob,
206 SkScalar x,
207 SkScalar y,
Tao Bai1e1deb412019-07-21 16:14:00208 NodeId node_id,
209 const PaintFlags& flags) = 0;
Tao Baiedaf87092019-01-30 23:12:54210
Adrienne Walker15ce23502017-05-08 18:08:15211 // Unlike SkCanvas::drawPicture, this only plays back the PaintRecord and does
212 // not add an additional clip. This is closer to SkPicture::playback.
ennedffda502017-03-23 20:46:43213 virtual void drawPicture(sk_sp<const PaintRecord> record) = 0;
enned2501572017-03-09 19:59:17214
enne98c9f8052017-03-15 19:38:22215 virtual bool isClipEmpty() const = 0;
Mike Reedf97e2d162020-01-07 15:26:43216 virtual SkMatrix getTotalMatrix() const = 0;
Aaron Krajeskie18de512021-04-15 15:17:03217 virtual SkM44 getLocalToDevice() const = 0;
enned2501572017-03-09 19:59:17218
Justin Novosaddae425072021-11-11 19:50:43219 virtual bool NeedsFlush() const = 0;
220
Florin Malita15935772019-02-06 21:52:24221 // Used for printing
enne5fa117a2017-04-04 20:56:58222 enum class AnnotationType {
223 URL,
224 NAMED_DESTINATION,
225 LINK_TO_DESTINATION,
226 };
227 virtual void Annotate(AnnotationType type,
228 const SkRect& rect,
229 sk_sp<SkData> data) = 0;
Florin Malita15935772019-02-06 21:52:24230 printing::MetafileSkia* GetPrintingMetafile() const { return metafile_; }
231 void SetPrintingMetafile(printing::MetafileSkia* metafile) {
232 metafile_ = metafile;
233 }
ckitagawa53eeaef2019-11-08 15:17:34234 paint_preview::PaintPreviewTracker* GetPaintPreviewTracker() const {
235 return tracker_;
236 }
237 void SetPaintPreviewTracker(paint_preview::PaintPreviewTracker* tracker) {
238 tracker_ = tracker;
239 }
enned2501572017-03-09 19:59:17240
Wei Li2a9bfe42018-01-13 05:42:56241 // Subclasses can override to handle custom data.
242 virtual void recordCustomData(uint32_t id) {}
243
Dominic Mazzonicf5d6b02020-03-06 19:49:12244 // Used for marked content in PDF files.
245 virtual void setNodeId(int) = 0;
246
enne59df29de2017-05-02 03:23:37247 private:
Florin Malita15935772019-02-06 21:52:24248 printing::MetafileSkia* metafile_ = nullptr;
ckitagawa53eeaef2019-11-08 15:17:34249 paint_preview::PaintPreviewTracker* tracker_ = nullptr;
enne34f6084c2017-02-02 22:39:08250};
251
enned2501572017-03-09 19:59:17252class CC_PAINT_EXPORT PaintCanvasAutoRestore {
253 public:
enne98c9f8052017-03-15 19:38:22254 PaintCanvasAutoRestore(PaintCanvas* canvas, bool save) : canvas_(canvas) {
enned2501572017-03-09 19:59:17255 if (canvas_) {
256 save_count_ = canvas_->getSaveCount();
257 if (save) {
258 canvas_->save();
259 }
260 }
261 }
262
enne98c9f8052017-03-15 19:38:22263 ~PaintCanvasAutoRestore() {
enned2501572017-03-09 19:59:17264 if (canvas_) {
265 canvas_->restoreToCount(save_count_);
266 }
267 }
268
enne98c9f8052017-03-15 19:38:22269 void restore() {
enned2501572017-03-09 19:59:17270 if (canvas_) {
271 canvas_->restoreToCount(save_count_);
272 canvas_ = nullptr;
273 }
274 }
275
276 private:
Keishi Hattori0e45c022021-11-27 09:25:52277 raw_ptr<PaintCanvas> canvas_ = nullptr;
enned2501572017-03-09 19:59:17278 int save_count_ = 0;
279};
280
enne34f6084c2017-02-02 22:39:08281} // namespace cc
282
283#endif // CC_PAINT_PAINT_CANVAS_H_