[go: nahoru, domu]

blob: 80a6915472d64900441223fb24a644d10a02e28d [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"
enned2501572017-03-09 19:59:179#include "base/macros.h"
enneea15ddb2017-03-22 01:49:0810#include "base/memory/ref_counted.h"
enne7b64edf32017-02-16 20:10:0211#include "build/build_config.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
17namespace cc {
Malay Keshavcf4ceef12018-10-19 17:56:5118class SkottieWrapper;
enned2501572017-03-09 19:59:1719class PaintFlags;
enne59df29de2017-05-02 03:23:3720class PaintOpBuffer;
21
22using PaintRecord = PaintOpBuffer;
enne34f6084c2017-02-02 22:39:0823
Adrienne Walker26aac6e52018-10-23 19:23:3724// PaintCanvas is the cc/paint wrapper of SkCanvas. It has a more restricted
25// interface than SkCanvas (trimmed back to only what Chrome uses). Its reason
26// for existence is so that it can do custom serialization logic into a
27// PaintOpBuffer which (unlike SkPicture) is mutable, handles image replacement,
28// and can be serialized in custom ways (such as using the transfer cache).
29//
30// PaintCanvas is usually implemented by either:
31// (1) SkiaPaintCanvas, which is backed by an SkCanvas, usually for rasterizing.
32// (2) RecordPaintCanvas, which records paint commands into a PaintOpBuffer.
33//
34// SkiaPaintCanvas allows callers to go from PaintCanvas to SkCanvas (or
35// PaintRecord to SkPicture), but this is a one way trip. There is no way to go
36// from SkCanvas to PaintCanvas or from SkPicture back into PaintRecord.
enned2501572017-03-09 19:59:1737class CC_PAINT_EXPORT PaintCanvas {
enne34f6084c2017-02-02 22:39:0838 public:
enne59df29de2017-05-02 03:23:3739 PaintCanvas() {}
khushalsagarb74f9122017-03-22 04:37:4740 virtual ~PaintCanvas() {}
41
enne98c9f8052017-03-15 19:38:2242 virtual SkMetaData& getMetaData() = 0;
enne59df29de2017-05-02 03:23:3743
44 // TODO(enne): this only appears to mostly be used to determine if this is
45 // recording or not, so could be simplified or removed.
enne98c9f8052017-03-15 19:38:2246 virtual SkImageInfo imageInfo() const = 0;
enned2501572017-03-09 19:59:1747
enned2501572017-03-09 19:59:1748 // TODO(enne): It would be nice to get rid of flush() entirely, as it
49 // doesn't really make sense for recording. However, this gets used by
zhuoyu.qian4689dde22017-10-16 04:11:4850 // PaintCanvasVideoRenderer which takes a PaintCanvas to paint both
enned2501572017-03-09 19:59:1751 // software and hardware video. This is super entangled with ImageBuffer
52 // and canvas/video painting in Blink where the same paths are used for
53 // both recording and gpu work.
enne98c9f8052017-03-15 19:38:2254 virtual void flush() = 0;
enned2501572017-03-09 19:59:1755
enne98c9f8052017-03-15 19:38:2256 virtual int save() = 0;
57 virtual int saveLayer(const SkRect* bounds, const PaintFlags* flags) = 0;
danakjf4730d52017-06-06 22:03:0258 virtual int saveLayerAlpha(const SkRect* bounds,
59 uint8_t alpha,
60 bool preserve_lcd_text_requests) = 0;
enne98c9f8052017-03-15 19:38:2261
62 virtual void restore() = 0;
63 virtual int getSaveCount() const = 0;
64 virtual void restoreToCount(int save_count) = 0;
65 virtual void translate(SkScalar dx, SkScalar dy) = 0;
66 virtual void scale(SkScalar sx, SkScalar sy) = 0;
67 virtual void rotate(SkScalar degrees) = 0;
enne98c9f8052017-03-15 19:38:2268 virtual void concat(const SkMatrix& matrix) = 0;
69 virtual void setMatrix(const SkMatrix& matrix) = 0;
enne98c9f8052017-03-15 19:38:2270
71 virtual void clipRect(const SkRect& rect,
72 SkClipOp op,
73 bool do_anti_alias) = 0;
74 void clipRect(const SkRect& rect, SkClipOp op) { clipRect(rect, op, false); }
75 void clipRect(const SkRect& rect, bool do_anti_alias) {
76 clipRect(rect, SkClipOp::kIntersect, do_anti_alias);
enned2501572017-03-09 19:59:1777 }
enne98c9f8052017-03-15 19:38:2278 void clipRect(const SkRect& rect) {
79 clipRect(rect, SkClipOp::kIntersect, false);
enned2501572017-03-09 19:59:1780 }
81
enne98c9f8052017-03-15 19:38:2282 virtual void clipRRect(const SkRRect& rrect,
83 SkClipOp op,
84 bool do_anti_alias) = 0;
85 void clipRRect(const SkRRect& rrect, bool do_anti_alias) {
86 clipRRect(rrect, SkClipOp::kIntersect, do_anti_alias);
enned2501572017-03-09 19:59:1787 }
enne98c9f8052017-03-15 19:38:2288 void clipRRect(const SkRRect& rrect, SkClipOp op) {
89 clipRRect(rrect, op, false);
enned2501572017-03-09 19:59:1790 }
enne98c9f8052017-03-15 19:38:2291 void clipRRect(const SkRRect& rrect) {
92 clipRRect(rrect, SkClipOp::kIntersect, false);
enned2501572017-03-09 19:59:1793 }
enned2501572017-03-09 19:59:1794
enne98c9f8052017-03-15 19:38:2295 virtual void clipPath(const SkPath& path,
96 SkClipOp op,
97 bool do_anti_alias) = 0;
98 void clipPath(const SkPath& path, SkClipOp op) { clipPath(path, op, false); }
99 void clipPath(const SkPath& path, bool do_anti_alias) {
100 clipPath(path, SkClipOp::kIntersect, do_anti_alias);
enned2501572017-03-09 19:59:17101 }
enne98c9f8052017-03-15 19:38:22102
enne98c9f8052017-03-15 19:38:22103 virtual SkRect getLocalClipBounds() const = 0;
104 virtual bool getLocalClipBounds(SkRect* bounds) const = 0;
105 virtual SkIRect getDeviceClipBounds() const = 0;
106 virtual bool getDeviceClipBounds(SkIRect* bounds) const = 0;
107 virtual void drawColor(SkColor color, SkBlendMode mode) = 0;
108 void drawColor(SkColor color) { drawColor(color, SkBlendMode::kSrcOver); }
enne59df29de2017-05-02 03:23:37109
110 // TODO(enne): This is a synonym for drawColor with kSrc. Remove it.
enne98c9f8052017-03-15 19:38:22111 virtual void clear(SkColor color) = 0;
112
113 virtual void drawLine(SkScalar x0,
114 SkScalar y0,
115 SkScalar x1,
116 SkScalar y1,
117 const PaintFlags& flags) = 0;
118 virtual void drawRect(const SkRect& rect, const PaintFlags& flags) = 0;
119 virtual void drawIRect(const SkIRect& rect, const PaintFlags& flags) = 0;
120 virtual void drawOval(const SkRect& oval, const PaintFlags& flags) = 0;
121 virtual void drawRRect(const SkRRect& rrect, const PaintFlags& flags) = 0;
122 virtual void drawDRRect(const SkRRect& outer,
123 const SkRRect& inner,
124 const PaintFlags& flags) = 0;
enne98c9f8052017-03-15 19:38:22125 virtual void drawRoundRect(const SkRect& rect,
126 SkScalar rx,
127 SkScalar ry,
128 const PaintFlags& flags) = 0;
129 virtual void drawPath(const SkPath& path, const PaintFlags& flags) = 0;
vmpstr94cfa882017-04-14 01:19:35130 virtual void drawImage(const PaintImage& image,
enne98c9f8052017-03-15 19:38:22131 SkScalar left,
132 SkScalar top,
133 const PaintFlags* flags) = 0;
vmpstr94cfa882017-04-14 01:19:35134 void drawImage(const PaintImage& image, SkScalar left, SkScalar top) {
enne98c9f8052017-03-15 19:38:22135 drawImage(image, left, top, nullptr);
enned2501572017-03-09 19:59:17136 }
137
138 enum SrcRectConstraint {
139 kStrict_SrcRectConstraint = SkCanvas::kStrict_SrcRectConstraint,
140 kFast_SrcRectConstraint = SkCanvas::kFast_SrcRectConstraint,
141 };
142
vmpstr94cfa882017-04-14 01:19:35143 virtual void drawImageRect(const PaintImage& image,
enne98c9f8052017-03-15 19:38:22144 const SkRect& src,
145 const SkRect& dst,
146 const PaintFlags* flags,
147 SrcRectConstraint constraint) = 0;
enned2501572017-03-09 19:59:17148
Malay Keshavcf4ceef12018-10-19 17:56:51149 // Draws the frame of the |skottie| animation specified by the normalized time
150 // t [0->first frame..1->last frame] at the destination bounds given by |dst|
151 // onto the canvas.
152 virtual void drawSkottie(scoped_refptr<SkottieWrapper> skottie,
153 const SkRect& dst,
154 float t) = 0;
155
Khushal464808c92018-10-19 23:45:59156 virtual void drawTextBlob(sk_sp<SkTextBlob> blob,
enne98c9f8052017-03-15 19:38:22157 SkScalar x,
158 SkScalar y,
159 const PaintFlags& flags) = 0;
enned2501572017-03-09 19:59:17160
Adrienne Walker15ce23502017-05-08 18:08:15161 // Unlike SkCanvas::drawPicture, this only plays back the PaintRecord and does
162 // not add an additional clip. This is closer to SkPicture::playback.
ennedffda502017-03-23 20:46:43163 virtual void drawPicture(sk_sp<const PaintRecord> record) = 0;
enned2501572017-03-09 19:59:17164
enne98c9f8052017-03-15 19:38:22165 virtual bool isClipEmpty() const = 0;
166 virtual bool isClipRect() const = 0;
167 virtual const SkMatrix& getTotalMatrix() const = 0;
enned2501572017-03-09 19:59:17168
enne5fa117a2017-04-04 20:56:58169 enum class AnnotationType {
170 URL,
171 NAMED_DESTINATION,
172 LINK_TO_DESTINATION,
173 };
174 virtual void Annotate(AnnotationType type,
175 const SkRect& rect,
176 sk_sp<SkData> data) = 0;
enned2501572017-03-09 19:59:17177
Wei Li2a9bfe42018-01-13 05:42:56178 // Subclasses can override to handle custom data.
179 virtual void recordCustomData(uint32_t id) {}
180
enne59df29de2017-05-02 03:23:37181 private:
182 DISALLOW_COPY_AND_ASSIGN(PaintCanvas);
enne34f6084c2017-02-02 22:39:08183};
184
enned2501572017-03-09 19:59:17185class CC_PAINT_EXPORT PaintCanvasAutoRestore {
186 public:
enne98c9f8052017-03-15 19:38:22187 PaintCanvasAutoRestore(PaintCanvas* canvas, bool save) : canvas_(canvas) {
enned2501572017-03-09 19:59:17188 if (canvas_) {
189 save_count_ = canvas_->getSaveCount();
190 if (save) {
191 canvas_->save();
192 }
193 }
194 }
195
enne98c9f8052017-03-15 19:38:22196 ~PaintCanvasAutoRestore() {
enned2501572017-03-09 19:59:17197 if (canvas_) {
198 canvas_->restoreToCount(save_count_);
199 }
200 }
201
enne98c9f8052017-03-15 19:38:22202 void restore() {
enned2501572017-03-09 19:59:17203 if (canvas_) {
204 canvas_->restoreToCount(save_count_);
205 canvas_ = nullptr;
206 }
207 }
208
209 private:
210 PaintCanvas* canvas_ = nullptr;
211 int save_count_ = 0;
212};
213
enne34f6084c2017-02-02 22:39:08214} // namespace cc
215
216#endif // CC_PAINT_PAINT_CANVAS_H_