[go: nahoru, domu]

blob: c8f959d5067238e670abf0423c240c949e470e6f [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"
Vladimir Levin06970f12017-11-09 02:05:2814#include "cc/paint/paint_text_blob.h"
enne34f6084c2017-02-02 22:39:0815#include "third_party/skia/include/core/SkCanvas.h"
enne34f6084c2017-02-02 22:39:0816
17namespace cc {
18
enned2501572017-03-09 19:59:1719class PaintFlags;
enne59df29de2017-05-02 03:23:3720class PaintOpBuffer;
21
22using PaintRecord = PaintOpBuffer;
enne34f6084c2017-02-02 22:39:0823
enned2501572017-03-09 19:59:1724class CC_PAINT_EXPORT PaintCanvas {
enne34f6084c2017-02-02 22:39:0825 public:
enne59df29de2017-05-02 03:23:3726 PaintCanvas() {}
khushalsagarb74f9122017-03-22 04:37:4727 virtual ~PaintCanvas() {}
28
enne98c9f8052017-03-15 19:38:2229 virtual SkMetaData& getMetaData() = 0;
enne59df29de2017-05-02 03:23:3730
31 // TODO(enne): this only appears to mostly be used to determine if this is
32 // recording or not, so could be simplified or removed.
enne98c9f8052017-03-15 19:38:2233 virtual SkImageInfo imageInfo() const = 0;
enned2501572017-03-09 19:59:1734
enned2501572017-03-09 19:59:1735 // TODO(enne): It would be nice to get rid of flush() entirely, as it
36 // doesn't really make sense for recording. However, this gets used by
zhuoyu.qian4689dde22017-10-16 04:11:4837 // PaintCanvasVideoRenderer which takes a PaintCanvas to paint both
enned2501572017-03-09 19:59:1738 // software and hardware video. This is super entangled with ImageBuffer
39 // and canvas/video painting in Blink where the same paths are used for
40 // both recording and gpu work.
enne98c9f8052017-03-15 19:38:2241 virtual void flush() = 0;
enned2501572017-03-09 19:59:1742
enne98c9f8052017-03-15 19:38:2243 virtual int save() = 0;
44 virtual int saveLayer(const SkRect* bounds, const PaintFlags* flags) = 0;
danakjf4730d52017-06-06 22:03:0245 virtual int saveLayerAlpha(const SkRect* bounds,
46 uint8_t alpha,
47 bool preserve_lcd_text_requests) = 0;
enne98c9f8052017-03-15 19:38:2248
49 virtual void restore() = 0;
50 virtual int getSaveCount() const = 0;
51 virtual void restoreToCount(int save_count) = 0;
52 virtual void translate(SkScalar dx, SkScalar dy) = 0;
53 virtual void scale(SkScalar sx, SkScalar sy) = 0;
54 virtual void rotate(SkScalar degrees) = 0;
enne98c9f8052017-03-15 19:38:2255 virtual void concat(const SkMatrix& matrix) = 0;
56 virtual void setMatrix(const SkMatrix& matrix) = 0;
enne98c9f8052017-03-15 19:38:2257
58 virtual void clipRect(const SkRect& rect,
59 SkClipOp op,
60 bool do_anti_alias) = 0;
61 void clipRect(const SkRect& rect, SkClipOp op) { clipRect(rect, op, false); }
62 void clipRect(const SkRect& rect, bool do_anti_alias) {
63 clipRect(rect, SkClipOp::kIntersect, do_anti_alias);
enned2501572017-03-09 19:59:1764 }
enne98c9f8052017-03-15 19:38:2265 void clipRect(const SkRect& rect) {
66 clipRect(rect, SkClipOp::kIntersect, false);
enned2501572017-03-09 19:59:1767 }
68
enne98c9f8052017-03-15 19:38:2269 virtual void clipRRect(const SkRRect& rrect,
70 SkClipOp op,
71 bool do_anti_alias) = 0;
72 void clipRRect(const SkRRect& rrect, bool do_anti_alias) {
73 clipRRect(rrect, SkClipOp::kIntersect, do_anti_alias);
enned2501572017-03-09 19:59:1774 }
enne98c9f8052017-03-15 19:38:2275 void clipRRect(const SkRRect& rrect, SkClipOp op) {
76 clipRRect(rrect, op, false);
enned2501572017-03-09 19:59:1777 }
enne98c9f8052017-03-15 19:38:2278 void clipRRect(const SkRRect& rrect) {
79 clipRRect(rrect, SkClipOp::kIntersect, false);
enned2501572017-03-09 19:59:1780 }
enned2501572017-03-09 19:59:1781
enne98c9f8052017-03-15 19:38:2282 virtual void clipPath(const SkPath& path,
83 SkClipOp op,
84 bool do_anti_alias) = 0;
85 void clipPath(const SkPath& path, SkClipOp op) { clipPath(path, op, false); }
86 void clipPath(const SkPath& path, bool do_anti_alias) {
87 clipPath(path, SkClipOp::kIntersect, do_anti_alias);
enned2501572017-03-09 19:59:1788 }
enne98c9f8052017-03-15 19:38:2289
enne98c9f8052017-03-15 19:38:2290 virtual SkRect getLocalClipBounds() const = 0;
91 virtual bool getLocalClipBounds(SkRect* bounds) const = 0;
92 virtual SkIRect getDeviceClipBounds() const = 0;
93 virtual bool getDeviceClipBounds(SkIRect* bounds) const = 0;
94 virtual void drawColor(SkColor color, SkBlendMode mode) = 0;
95 void drawColor(SkColor color) { drawColor(color, SkBlendMode::kSrcOver); }
enne59df29de2017-05-02 03:23:3796
97 // TODO(enne): This is a synonym for drawColor with kSrc. Remove it.
enne98c9f8052017-03-15 19:38:2298 virtual void clear(SkColor color) = 0;
99
100 virtual void drawLine(SkScalar x0,
101 SkScalar y0,
102 SkScalar x1,
103 SkScalar y1,
104 const PaintFlags& flags) = 0;
105 virtual void drawRect(const SkRect& rect, const PaintFlags& flags) = 0;
106 virtual void drawIRect(const SkIRect& rect, const PaintFlags& flags) = 0;
107 virtual void drawOval(const SkRect& oval, const PaintFlags& flags) = 0;
108 virtual void drawRRect(const SkRRect& rrect, const PaintFlags& flags) = 0;
109 virtual void drawDRRect(const SkRRect& outer,
110 const SkRRect& inner,
111 const PaintFlags& flags) = 0;
enne98c9f8052017-03-15 19:38:22112 virtual void drawRoundRect(const SkRect& rect,
113 SkScalar rx,
114 SkScalar ry,
115 const PaintFlags& flags) = 0;
116 virtual void drawPath(const SkPath& path, const PaintFlags& flags) = 0;
vmpstr94cfa882017-04-14 01:19:35117 virtual void drawImage(const PaintImage& image,
enne98c9f8052017-03-15 19:38:22118 SkScalar left,
119 SkScalar top,
120 const PaintFlags* flags) = 0;
vmpstr94cfa882017-04-14 01:19:35121 void drawImage(const PaintImage& image, SkScalar left, SkScalar top) {
enne98c9f8052017-03-15 19:38:22122 drawImage(image, left, top, nullptr);
enned2501572017-03-09 19:59:17123 }
124
125 enum SrcRectConstraint {
126 kStrict_SrcRectConstraint = SkCanvas::kStrict_SrcRectConstraint,
127 kFast_SrcRectConstraint = SkCanvas::kFast_SrcRectConstraint,
128 };
129
vmpstr94cfa882017-04-14 01:19:35130 virtual void drawImageRect(const PaintImage& image,
enne98c9f8052017-03-15 19:38:22131 const SkRect& src,
132 const SkRect& dst,
133 const PaintFlags* flags,
134 SrcRectConstraint constraint) = 0;
135 virtual void drawBitmap(const SkBitmap& bitmap,
136 SkScalar left,
137 SkScalar top,
138 const PaintFlags* flags) = 0;
139 void drawBitmap(const SkBitmap& bitmap, SkScalar left, SkScalar top) {
140 drawBitmap(bitmap, left, top, nullptr);
enned2501572017-03-09 19:59:17141 }
142
Vladimir Levin06970f12017-11-09 02:05:28143 virtual void drawTextBlob(scoped_refptr<PaintTextBlob> blob,
enne98c9f8052017-03-15 19:38:22144 SkScalar x,
145 SkScalar y,
146 const PaintFlags& flags) = 0;
enned2501572017-03-09 19:59:17147
Adrienne Walker15ce23502017-05-08 18:08:15148 // Unlike SkCanvas::drawPicture, this only plays back the PaintRecord and does
149 // not add an additional clip. This is closer to SkPicture::playback.
ennedffda502017-03-23 20:46:43150 virtual void drawPicture(sk_sp<const PaintRecord> record) = 0;
enned2501572017-03-09 19:59:17151
enne98c9f8052017-03-15 19:38:22152 virtual bool isClipEmpty() const = 0;
153 virtual bool isClipRect() const = 0;
154 virtual const SkMatrix& getTotalMatrix() const = 0;
enned2501572017-03-09 19:59:17155
enne5fa117a2017-04-04 20:56:58156 enum class AnnotationType {
157 URL,
158 NAMED_DESTINATION,
159 LINK_TO_DESTINATION,
160 };
161 virtual void Annotate(AnnotationType type,
162 const SkRect& rect,
163 sk_sp<SkData> data) = 0;
enned2501572017-03-09 19:59:17164
Wei Li2a9bfe42018-01-13 05:42:56165 // Subclasses can override to handle custom data.
166 virtual void recordCustomData(uint32_t id) {}
167
enne59df29de2017-05-02 03:23:37168 private:
169 DISALLOW_COPY_AND_ASSIGN(PaintCanvas);
enne34f6084c2017-02-02 22:39:08170};
171
enned2501572017-03-09 19:59:17172class CC_PAINT_EXPORT PaintCanvasAutoRestore {
173 public:
enne98c9f8052017-03-15 19:38:22174 PaintCanvasAutoRestore(PaintCanvas* canvas, bool save) : canvas_(canvas) {
enned2501572017-03-09 19:59:17175 if (canvas_) {
176 save_count_ = canvas_->getSaveCount();
177 if (save) {
178 canvas_->save();
179 }
180 }
181 }
182
enne98c9f8052017-03-15 19:38:22183 ~PaintCanvasAutoRestore() {
enned2501572017-03-09 19:59:17184 if (canvas_) {
185 canvas_->restoreToCount(save_count_);
186 }
187 }
188
enne98c9f8052017-03-15 19:38:22189 void restore() {
enned2501572017-03-09 19:59:17190 if (canvas_) {
191 canvas_->restoreToCount(save_count_);
192 canvas_ = nullptr;
193 }
194 }
195
196 private:
197 PaintCanvas* canvas_ = nullptr;
198 int save_count_ = 0;
199};
200
enne34f6084c2017-02-02 22:39:08201} // namespace cc
202
203#endif // CC_PAINT_PAINT_CANVAS_H_