[go: nahoru, domu]

blob: 2b9c34a23365618a519c1a2906fd894c33b6ba34 [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"
enneea15ddb2017-03-22 01:49:089#include "base/memory/ref_counted.h"
enne7b64edf32017-02-16 20:10:0210#include "build/build_config.h"
Tao Bai1e1deb412019-07-21 16:14:0011#include "cc/paint/node_id.h"
enne34f6084c2017-02-02 22:39:0812#include "cc/paint/paint_export.h"
vmpstr94cfa882017-04-14 01:19:3513#include "cc/paint/paint_image.h"
enne34f6084c2017-02-02 22:39:0814#include "third_party/skia/include/core/SkCanvas.h"
Khushal464808c92018-10-19 23:45:5915#include "third_party/skia/include/core/SkTextBlob.h"
enne34f6084c2017-02-02 22:39:0816
Florin Malita15935772019-02-06 21:52:2417namespace printing {
18class MetafileSkia;
19} // namespace printing
20
ckitagawa53eeaef2019-11-08 15:17:3421namespace paint_preview {
22class PaintPreviewTracker;
23} // namespace paint_preview
24
enne34f6084c2017-02-02 22:39:0825namespace cc {
Malay Keshavcf4ceef12018-10-19 17:56:5126class SkottieWrapper;
enned2501572017-03-09 19:59:1727class PaintFlags;
enne59df29de2017-05-02 03:23:3728class PaintOpBuffer;
29
30using PaintRecord = PaintOpBuffer;
enne34f6084c2017-02-02 22:39:0831
Adrienne Walker26aac6e52018-10-23 19:23:3732// PaintCanvas is the cc/paint wrapper of SkCanvas. It has a more restricted
33// interface than SkCanvas (trimmed back to only what Chrome uses). Its reason
34// for existence is so that it can do custom serialization logic into a
35// PaintOpBuffer which (unlike SkPicture) is mutable, handles image replacement,
36// and can be serialized in custom ways (such as using the transfer cache).
37//
38// PaintCanvas is usually implemented by either:
39// (1) SkiaPaintCanvas, which is backed by an SkCanvas, usually for rasterizing.
40// (2) RecordPaintCanvas, which records paint commands into a PaintOpBuffer.
41//
42// SkiaPaintCanvas allows callers to go from PaintCanvas to SkCanvas (or
43// PaintRecord to SkPicture), but this is a one way trip. There is no way to go
44// from SkCanvas to PaintCanvas or from SkPicture back into PaintRecord.
enned2501572017-03-09 19:59:1745class CC_PAINT_EXPORT PaintCanvas {
enne34f6084c2017-02-02 22:39:0846 public:
Vladimir Levinf06d1cd72019-03-13 18:24:1047 PaintCanvas() = default;
48 PaintCanvas(const PaintCanvas&) = delete;
49 virtual ~PaintCanvas() = default;
50
51 PaintCanvas& operator=(const PaintCanvas&) = delete;
khushalsagarb74f9122017-03-22 04:37:4752
enne59df29de2017-05-02 03:23:3753 // TODO(enne): this only appears to mostly be used to determine if this is
54 // recording or not, so could be simplified or removed.
enne98c9f8052017-03-15 19:38:2255 virtual SkImageInfo imageInfo() const = 0;
enned2501572017-03-09 19:59:1756
Tom Andersonbec09512019-09-16 20:32:1657 virtual void* accessTopLayerPixels(SkImageInfo* info,
58 size_t* rowBytes,
59 SkIPoint* origin = nullptr) = 0;
60
enned2501572017-03-09 19:59:1761 // TODO(enne): It would be nice to get rid of flush() entirely, as it
62 // doesn't really make sense for recording. However, this gets used by
zhuoyu.qian4689dde22017-10-16 04:11:4863 // PaintCanvasVideoRenderer which takes a PaintCanvas to paint both
enned2501572017-03-09 19:59:1764 // software and hardware video. This is super entangled with ImageBuffer
65 // and canvas/video painting in Blink where the same paths are used for
66 // both recording and gpu work.
enne98c9f8052017-03-15 19:38:2267 virtual void flush() = 0;
enned2501572017-03-09 19:59:1768
enne98c9f8052017-03-15 19:38:2269 virtual int save() = 0;
70 virtual int saveLayer(const SkRect* bounds, const PaintFlags* flags) = 0;
Peter Kastingfe4ba932019-01-07 20:21:0571 virtual int saveLayerAlpha(const SkRect* bounds, uint8_t alpha) = 0;
enne98c9f8052017-03-15 19:38:2272
73 virtual void restore() = 0;
74 virtual int getSaveCount() const = 0;
75 virtual void restoreToCount(int save_count) = 0;
76 virtual void translate(SkScalar dx, SkScalar dy) = 0;
77 virtual void scale(SkScalar sx, SkScalar sy) = 0;
78 virtual void rotate(SkScalar degrees) = 0;
enne98c9f8052017-03-15 19:38:2279 virtual void concat(const SkMatrix& matrix) = 0;
80 virtual void setMatrix(const SkMatrix& matrix) = 0;
enne98c9f8052017-03-15 19:38:2281
82 virtual void clipRect(const SkRect& rect,
83 SkClipOp op,
84 bool do_anti_alias) = 0;
85 void clipRect(const SkRect& rect, SkClipOp op) { clipRect(rect, op, false); }
86 void clipRect(const SkRect& rect, bool do_anti_alias) {
87 clipRect(rect, SkClipOp::kIntersect, do_anti_alias);
enned2501572017-03-09 19:59:1788 }
enne98c9f8052017-03-15 19:38:2289 void clipRect(const SkRect& rect) {
90 clipRect(rect, SkClipOp::kIntersect, false);
enned2501572017-03-09 19:59:1791 }
92
enne98c9f8052017-03-15 19:38:2293 virtual void clipRRect(const SkRRect& rrect,
94 SkClipOp op,
95 bool do_anti_alias) = 0;
96 void clipRRect(const SkRRect& rrect, bool do_anti_alias) {
97 clipRRect(rrect, SkClipOp::kIntersect, do_anti_alias);
enned2501572017-03-09 19:59:1798 }
enne98c9f8052017-03-15 19:38:2299 void clipRRect(const SkRRect& rrect, SkClipOp op) {
100 clipRRect(rrect, op, false);
enned2501572017-03-09 19:59:17101 }
enne98c9f8052017-03-15 19:38:22102 void clipRRect(const SkRRect& rrect) {
103 clipRRect(rrect, SkClipOp::kIntersect, false);
enned2501572017-03-09 19:59:17104 }
enned2501572017-03-09 19:59:17105
enne98c9f8052017-03-15 19:38:22106 virtual void clipPath(const SkPath& path,
107 SkClipOp op,
108 bool do_anti_alias) = 0;
109 void clipPath(const SkPath& path, SkClipOp op) { clipPath(path, op, false); }
110 void clipPath(const SkPath& path, bool do_anti_alias) {
111 clipPath(path, SkClipOp::kIntersect, do_anti_alias);
enned2501572017-03-09 19:59:17112 }
enne98c9f8052017-03-15 19:38:22113
enne98c9f8052017-03-15 19:38:22114 virtual SkRect getLocalClipBounds() const = 0;
115 virtual bool getLocalClipBounds(SkRect* bounds) const = 0;
116 virtual SkIRect getDeviceClipBounds() const = 0;
117 virtual bool getDeviceClipBounds(SkIRect* bounds) const = 0;
118 virtual void drawColor(SkColor color, SkBlendMode mode) = 0;
119 void drawColor(SkColor color) { drawColor(color, SkBlendMode::kSrcOver); }
enne59df29de2017-05-02 03:23:37120
121 // TODO(enne): This is a synonym for drawColor with kSrc. Remove it.
enne98c9f8052017-03-15 19:38:22122 virtual void clear(SkColor color) = 0;
123
124 virtual void drawLine(SkScalar x0,
125 SkScalar y0,
126 SkScalar x1,
127 SkScalar y1,
128 const PaintFlags& flags) = 0;
129 virtual void drawRect(const SkRect& rect, const PaintFlags& flags) = 0;
130 virtual void drawIRect(const SkIRect& rect, const PaintFlags& flags) = 0;
131 virtual void drawOval(const SkRect& oval, const PaintFlags& flags) = 0;
132 virtual void drawRRect(const SkRRect& rrect, const PaintFlags& flags) = 0;
133 virtual void drawDRRect(const SkRRect& outer,
134 const SkRRect& inner,
135 const PaintFlags& flags) = 0;
enne98c9f8052017-03-15 19:38:22136 virtual void drawRoundRect(const SkRect& rect,
137 SkScalar rx,
138 SkScalar ry,
139 const PaintFlags& flags) = 0;
140 virtual void drawPath(const SkPath& path, const PaintFlags& flags) = 0;
vmpstr94cfa882017-04-14 01:19:35141 virtual void drawImage(const PaintImage& image,
enne98c9f8052017-03-15 19:38:22142 SkScalar left,
143 SkScalar top,
144 const PaintFlags* flags) = 0;
vmpstr94cfa882017-04-14 01:19:35145 void drawImage(const PaintImage& image, SkScalar left, SkScalar top) {
enne98c9f8052017-03-15 19:38:22146 drawImage(image, left, top, nullptr);
enned2501572017-03-09 19:59:17147 }
148
vmpstr94cfa882017-04-14 01:19:35149 virtual void drawImageRect(const PaintImage& image,
enne98c9f8052017-03-15 19:38:22150 const SkRect& src,
151 const SkRect& dst,
152 const PaintFlags* flags,
jongdeok.kim50b377072020-05-15 22:16:12153 SkCanvas::SrcRectConstraint constraint) = 0;
enned2501572017-03-09 19:59:17154
Malay Keshavcf4ceef12018-10-19 17:56:51155 // Draws the frame of the |skottie| animation specified by the normalized time
156 // t [0->first frame..1->last frame] at the destination bounds given by |dst|
157 // onto the canvas.
158 virtual void drawSkottie(scoped_refptr<SkottieWrapper> skottie,
159 const SkRect& dst,
160 float t) = 0;
161
Khushal464808c92018-10-19 23:45:59162 virtual void drawTextBlob(sk_sp<SkTextBlob> blob,
enne98c9f8052017-03-15 19:38:22163 SkScalar x,
164 SkScalar y,
165 const PaintFlags& flags) = 0;
enned2501572017-03-09 19:59:17166
Tao Baiedaf87092019-01-30 23:12:54167 virtual void drawTextBlob(sk_sp<SkTextBlob> blob,
168 SkScalar x,
169 SkScalar y,
Tao Bai1e1deb412019-07-21 16:14:00170 NodeId node_id,
171 const PaintFlags& flags) = 0;
Tao Baiedaf87092019-01-30 23:12:54172
Adrienne Walker15ce23502017-05-08 18:08:15173 // Unlike SkCanvas::drawPicture, this only plays back the PaintRecord and does
174 // not add an additional clip. This is closer to SkPicture::playback.
ennedffda502017-03-23 20:46:43175 virtual void drawPicture(sk_sp<const PaintRecord> record) = 0;
enned2501572017-03-09 19:59:17176
enne98c9f8052017-03-15 19:38:22177 virtual bool isClipEmpty() const = 0;
Mike Reedf97e2d162020-01-07 15:26:43178 virtual SkMatrix getTotalMatrix() const = 0;
enned2501572017-03-09 19:59:17179
Florin Malita15935772019-02-06 21:52:24180 // Used for printing
enne5fa117a2017-04-04 20:56:58181 enum class AnnotationType {
182 URL,
183 NAMED_DESTINATION,
184 LINK_TO_DESTINATION,
185 };
186 virtual void Annotate(AnnotationType type,
187 const SkRect& rect,
188 sk_sp<SkData> data) = 0;
Florin Malita15935772019-02-06 21:52:24189 printing::MetafileSkia* GetPrintingMetafile() const { return metafile_; }
190 void SetPrintingMetafile(printing::MetafileSkia* metafile) {
191 metafile_ = metafile;
192 }
ckitagawa53eeaef2019-11-08 15:17:34193 paint_preview::PaintPreviewTracker* GetPaintPreviewTracker() const {
194 return tracker_;
195 }
196 void SetPaintPreviewTracker(paint_preview::PaintPreviewTracker* tracker) {
197 tracker_ = tracker;
198 }
enned2501572017-03-09 19:59:17199
Wei Li2a9bfe42018-01-13 05:42:56200 // Subclasses can override to handle custom data.
201 virtual void recordCustomData(uint32_t id) {}
202
Dominic Mazzonicf5d6b02020-03-06 19:49:12203 // Used for marked content in PDF files.
204 virtual void setNodeId(int) = 0;
205
enne59df29de2017-05-02 03:23:37206 private:
Florin Malita15935772019-02-06 21:52:24207 printing::MetafileSkia* metafile_ = nullptr;
ckitagawa53eeaef2019-11-08 15:17:34208 paint_preview::PaintPreviewTracker* tracker_ = nullptr;
enne34f6084c2017-02-02 22:39:08209};
210
enned2501572017-03-09 19:59:17211class CC_PAINT_EXPORT PaintCanvasAutoRestore {
212 public:
enne98c9f8052017-03-15 19:38:22213 PaintCanvasAutoRestore(PaintCanvas* canvas, bool save) : canvas_(canvas) {
enned2501572017-03-09 19:59:17214 if (canvas_) {
215 save_count_ = canvas_->getSaveCount();
216 if (save) {
217 canvas_->save();
218 }
219 }
220 }
221
enne98c9f8052017-03-15 19:38:22222 ~PaintCanvasAutoRestore() {
enned2501572017-03-09 19:59:17223 if (canvas_) {
224 canvas_->restoreToCount(save_count_);
225 }
226 }
227
enne98c9f8052017-03-15 19:38:22228 void restore() {
enned2501572017-03-09 19:59:17229 if (canvas_) {
230 canvas_->restoreToCount(save_count_);
231 canvas_ = nullptr;
232 }
233 }
234
235 private:
236 PaintCanvas* canvas_ = nullptr;
237 int save_count_ = 0;
238};
239
enne34f6084c2017-02-02 22:39:08240} // namespace cc
241
242#endif // CC_PAINT_PAINT_CANVAS_H_