enne | 34f6084c | 2017-02-02 22:39:08 | [diff] [blame] | 1 | // 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 | |
enne | d250157 | 2017-03-09 19:59:17 | [diff] [blame] | 8 | #include "base/compiler_specific.h" |
enne | ea15ddb | 2017-03-22 01:49:08 | [diff] [blame] | 9 | #include "base/memory/ref_counted.h" |
enne | 7b64edf3 | 2017-02-16 20:10:02 | [diff] [blame] | 10 | #include "build/build_config.h" |
enne | 34f6084c | 2017-02-02 22:39:08 | [diff] [blame] | 11 | #include "cc/paint/paint_export.h" |
vmpstr | 94cfa88 | 2017-04-14 01:19:35 | [diff] [blame] | 12 | #include "cc/paint/paint_image.h" |
enne | 34f6084c | 2017-02-02 22:39:08 | [diff] [blame] | 13 | #include "third_party/skia/include/core/SkCanvas.h" |
Khushal | 464808c9 | 2018-10-19 23:45:59 | [diff] [blame] | 14 | #include "third_party/skia/include/core/SkTextBlob.h" |
enne | 34f6084c | 2017-02-02 22:39:08 | [diff] [blame] | 15 | |
Florin Malita | 1593577 | 2019-02-06 21:52:24 | [diff] [blame] | 16 | namespace printing { |
| 17 | class MetafileSkia; |
| 18 | } // namespace printing |
| 19 | |
enne | 34f6084c | 2017-02-02 22:39:08 | [diff] [blame] | 20 | namespace cc { |
Malay Keshav | cf4ceef1 | 2018-10-19 17:56:51 | [diff] [blame] | 21 | class SkottieWrapper; |
enne | d250157 | 2017-03-09 19:59:17 | [diff] [blame] | 22 | class PaintFlags; |
enne | 59df29de | 2017-05-02 03:23:37 | [diff] [blame] | 23 | class PaintOpBuffer; |
| 24 | |
Tao Bai | edaf8709 | 2019-01-30 23:12:54 | [diff] [blame] | 25 | struct NodeHolder; |
| 26 | |
enne | 59df29de | 2017-05-02 03:23:37 | [diff] [blame] | 27 | using PaintRecord = PaintOpBuffer; |
enne | 34f6084c | 2017-02-02 22:39:08 | [diff] [blame] | 28 | |
Adrienne Walker | 26aac6e5 | 2018-10-23 19:23:37 | [diff] [blame] | 29 | // PaintCanvas is the cc/paint wrapper of SkCanvas. It has a more restricted |
| 30 | // interface than SkCanvas (trimmed back to only what Chrome uses). Its reason |
| 31 | // for existence is so that it can do custom serialization logic into a |
| 32 | // PaintOpBuffer which (unlike SkPicture) is mutable, handles image replacement, |
| 33 | // and can be serialized in custom ways (such as using the transfer cache). |
| 34 | // |
| 35 | // PaintCanvas is usually implemented by either: |
| 36 | // (1) SkiaPaintCanvas, which is backed by an SkCanvas, usually for rasterizing. |
| 37 | // (2) RecordPaintCanvas, which records paint commands into a PaintOpBuffer. |
| 38 | // |
| 39 | // SkiaPaintCanvas allows callers to go from PaintCanvas to SkCanvas (or |
| 40 | // PaintRecord to SkPicture), but this is a one way trip. There is no way to go |
| 41 | // from SkCanvas to PaintCanvas or from SkPicture back into PaintRecord. |
enne | d250157 | 2017-03-09 19:59:17 | [diff] [blame] | 42 | class CC_PAINT_EXPORT PaintCanvas { |
enne | 34f6084c | 2017-02-02 22:39:08 | [diff] [blame] | 43 | public: |
Vladimir Levin | f06d1cd7 | 2019-03-13 18:24:10 | [diff] [blame^] | 44 | PaintCanvas() = default; |
| 45 | PaintCanvas(const PaintCanvas&) = delete; |
| 46 | virtual ~PaintCanvas() = default; |
| 47 | |
| 48 | PaintCanvas& operator=(const PaintCanvas&) = delete; |
khushalsagar | b74f912 | 2017-03-22 04:37:47 | [diff] [blame] | 49 | |
enne | 59df29de | 2017-05-02 03:23:37 | [diff] [blame] | 50 | // TODO(enne): this only appears to mostly be used to determine if this is |
| 51 | // recording or not, so could be simplified or removed. |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 52 | virtual SkImageInfo imageInfo() const = 0; |
enne | d250157 | 2017-03-09 19:59:17 | [diff] [blame] | 53 | |
enne | d250157 | 2017-03-09 19:59:17 | [diff] [blame] | 54 | // TODO(enne): It would be nice to get rid of flush() entirely, as it |
| 55 | // doesn't really make sense for recording. However, this gets used by |
zhuoyu.qian | 4689dde2 | 2017-10-16 04:11:48 | [diff] [blame] | 56 | // PaintCanvasVideoRenderer which takes a PaintCanvas to paint both |
enne | d250157 | 2017-03-09 19:59:17 | [diff] [blame] | 57 | // software and hardware video. This is super entangled with ImageBuffer |
| 58 | // and canvas/video painting in Blink where the same paths are used for |
| 59 | // both recording and gpu work. |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 60 | virtual void flush() = 0; |
enne | d250157 | 2017-03-09 19:59:17 | [diff] [blame] | 61 | |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 62 | virtual int save() = 0; |
| 63 | virtual int saveLayer(const SkRect* bounds, const PaintFlags* flags) = 0; |
Peter Kasting | fe4ba93 | 2019-01-07 20:21:05 | [diff] [blame] | 64 | virtual int saveLayerAlpha(const SkRect* bounds, uint8_t alpha) = 0; |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 65 | |
| 66 | virtual void restore() = 0; |
| 67 | virtual int getSaveCount() const = 0; |
| 68 | virtual void restoreToCount(int save_count) = 0; |
| 69 | virtual void translate(SkScalar dx, SkScalar dy) = 0; |
| 70 | virtual void scale(SkScalar sx, SkScalar sy) = 0; |
| 71 | virtual void rotate(SkScalar degrees) = 0; |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 72 | virtual void concat(const SkMatrix& matrix) = 0; |
| 73 | virtual void setMatrix(const SkMatrix& matrix) = 0; |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 74 | |
| 75 | virtual void clipRect(const SkRect& rect, |
| 76 | SkClipOp op, |
| 77 | bool do_anti_alias) = 0; |
| 78 | void clipRect(const SkRect& rect, SkClipOp op) { clipRect(rect, op, false); } |
| 79 | void clipRect(const SkRect& rect, bool do_anti_alias) { |
| 80 | clipRect(rect, SkClipOp::kIntersect, do_anti_alias); |
enne | d250157 | 2017-03-09 19:59:17 | [diff] [blame] | 81 | } |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 82 | void clipRect(const SkRect& rect) { |
| 83 | clipRect(rect, SkClipOp::kIntersect, false); |
enne | d250157 | 2017-03-09 19:59:17 | [diff] [blame] | 84 | } |
| 85 | |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 86 | virtual void clipRRect(const SkRRect& rrect, |
| 87 | SkClipOp op, |
| 88 | bool do_anti_alias) = 0; |
| 89 | void clipRRect(const SkRRect& rrect, bool do_anti_alias) { |
| 90 | clipRRect(rrect, SkClipOp::kIntersect, do_anti_alias); |
enne | d250157 | 2017-03-09 19:59:17 | [diff] [blame] | 91 | } |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 92 | void clipRRect(const SkRRect& rrect, SkClipOp op) { |
| 93 | clipRRect(rrect, op, false); |
enne | d250157 | 2017-03-09 19:59:17 | [diff] [blame] | 94 | } |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 95 | void clipRRect(const SkRRect& rrect) { |
| 96 | clipRRect(rrect, SkClipOp::kIntersect, false); |
enne | d250157 | 2017-03-09 19:59:17 | [diff] [blame] | 97 | } |
enne | d250157 | 2017-03-09 19:59:17 | [diff] [blame] | 98 | |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 99 | virtual void clipPath(const SkPath& path, |
| 100 | SkClipOp op, |
| 101 | bool do_anti_alias) = 0; |
| 102 | void clipPath(const SkPath& path, SkClipOp op) { clipPath(path, op, false); } |
| 103 | void clipPath(const SkPath& path, bool do_anti_alias) { |
| 104 | clipPath(path, SkClipOp::kIntersect, do_anti_alias); |
enne | d250157 | 2017-03-09 19:59:17 | [diff] [blame] | 105 | } |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 106 | |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 107 | virtual SkRect getLocalClipBounds() const = 0; |
| 108 | virtual bool getLocalClipBounds(SkRect* bounds) const = 0; |
| 109 | virtual SkIRect getDeviceClipBounds() const = 0; |
| 110 | virtual bool getDeviceClipBounds(SkIRect* bounds) const = 0; |
| 111 | virtual void drawColor(SkColor color, SkBlendMode mode) = 0; |
| 112 | void drawColor(SkColor color) { drawColor(color, SkBlendMode::kSrcOver); } |
enne | 59df29de | 2017-05-02 03:23:37 | [diff] [blame] | 113 | |
| 114 | // TODO(enne): This is a synonym for drawColor with kSrc. Remove it. |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 115 | virtual void clear(SkColor color) = 0; |
| 116 | |
| 117 | virtual void drawLine(SkScalar x0, |
| 118 | SkScalar y0, |
| 119 | SkScalar x1, |
| 120 | SkScalar y1, |
| 121 | const PaintFlags& flags) = 0; |
| 122 | virtual void drawRect(const SkRect& rect, const PaintFlags& flags) = 0; |
| 123 | virtual void drawIRect(const SkIRect& rect, const PaintFlags& flags) = 0; |
| 124 | virtual void drawOval(const SkRect& oval, const PaintFlags& flags) = 0; |
| 125 | virtual void drawRRect(const SkRRect& rrect, const PaintFlags& flags) = 0; |
| 126 | virtual void drawDRRect(const SkRRect& outer, |
| 127 | const SkRRect& inner, |
| 128 | const PaintFlags& flags) = 0; |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 129 | virtual void drawRoundRect(const SkRect& rect, |
| 130 | SkScalar rx, |
| 131 | SkScalar ry, |
| 132 | const PaintFlags& flags) = 0; |
| 133 | virtual void drawPath(const SkPath& path, const PaintFlags& flags) = 0; |
vmpstr | 94cfa88 | 2017-04-14 01:19:35 | [diff] [blame] | 134 | virtual void drawImage(const PaintImage& image, |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 135 | SkScalar left, |
| 136 | SkScalar top, |
| 137 | const PaintFlags* flags) = 0; |
vmpstr | 94cfa88 | 2017-04-14 01:19:35 | [diff] [blame] | 138 | void drawImage(const PaintImage& image, SkScalar left, SkScalar top) { |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 139 | drawImage(image, left, top, nullptr); |
enne | d250157 | 2017-03-09 19:59:17 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | enum SrcRectConstraint { |
| 143 | kStrict_SrcRectConstraint = SkCanvas::kStrict_SrcRectConstraint, |
| 144 | kFast_SrcRectConstraint = SkCanvas::kFast_SrcRectConstraint, |
| 145 | }; |
| 146 | |
vmpstr | 94cfa88 | 2017-04-14 01:19:35 | [diff] [blame] | 147 | virtual void drawImageRect(const PaintImage& image, |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 148 | const SkRect& src, |
| 149 | const SkRect& dst, |
| 150 | const PaintFlags* flags, |
| 151 | SrcRectConstraint constraint) = 0; |
enne | d250157 | 2017-03-09 19:59:17 | [diff] [blame] | 152 | |
Malay Keshav | cf4ceef1 | 2018-10-19 17:56:51 | [diff] [blame] | 153 | // Draws the frame of the |skottie| animation specified by the normalized time |
| 154 | // t [0->first frame..1->last frame] at the destination bounds given by |dst| |
| 155 | // onto the canvas. |
| 156 | virtual void drawSkottie(scoped_refptr<SkottieWrapper> skottie, |
| 157 | const SkRect& dst, |
| 158 | float t) = 0; |
| 159 | |
Khushal | 464808c9 | 2018-10-19 23:45:59 | [diff] [blame] | 160 | virtual void drawTextBlob(sk_sp<SkTextBlob> blob, |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 161 | SkScalar x, |
| 162 | SkScalar y, |
| 163 | const PaintFlags& flags) = 0; |
enne | d250157 | 2017-03-09 19:59:17 | [diff] [blame] | 164 | |
Tao Bai | edaf8709 | 2019-01-30 23:12:54 | [diff] [blame] | 165 | virtual void drawTextBlob(sk_sp<SkTextBlob> blob, |
| 166 | SkScalar x, |
| 167 | SkScalar y, |
| 168 | const PaintFlags& flags, |
| 169 | const NodeHolder& holder) = 0; |
| 170 | |
Adrienne Walker | 15ce2350 | 2017-05-08 18:08:15 | [diff] [blame] | 171 | // Unlike SkCanvas::drawPicture, this only plays back the PaintRecord and does |
| 172 | // not add an additional clip. This is closer to SkPicture::playback. |
enne | dffda50 | 2017-03-23 20:46:43 | [diff] [blame] | 173 | virtual void drawPicture(sk_sp<const PaintRecord> record) = 0; |
enne | d250157 | 2017-03-09 19:59:17 | [diff] [blame] | 174 | |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 175 | virtual bool isClipEmpty() const = 0; |
| 176 | virtual bool isClipRect() const = 0; |
| 177 | virtual const SkMatrix& getTotalMatrix() const = 0; |
enne | d250157 | 2017-03-09 19:59:17 | [diff] [blame] | 178 | |
Florin Malita | 1593577 | 2019-02-06 21:52:24 | [diff] [blame] | 179 | // Used for printing |
enne | 5fa117a | 2017-04-04 20:56:58 | [diff] [blame] | 180 | enum class AnnotationType { |
| 181 | URL, |
| 182 | NAMED_DESTINATION, |
| 183 | LINK_TO_DESTINATION, |
| 184 | }; |
| 185 | virtual void Annotate(AnnotationType type, |
| 186 | const SkRect& rect, |
| 187 | sk_sp<SkData> data) = 0; |
Florin Malita | 1593577 | 2019-02-06 21:52:24 | [diff] [blame] | 188 | printing::MetafileSkia* GetPrintingMetafile() const { return metafile_; } |
| 189 | void SetPrintingMetafile(printing::MetafileSkia* metafile) { |
| 190 | metafile_ = metafile; |
| 191 | } |
enne | d250157 | 2017-03-09 19:59:17 | [diff] [blame] | 192 | |
Wei Li | 2a9bfe4 | 2018-01-13 05:42:56 | [diff] [blame] | 193 | // Subclasses can override to handle custom data. |
| 194 | virtual void recordCustomData(uint32_t id) {} |
| 195 | |
enne | 59df29de | 2017-05-02 03:23:37 | [diff] [blame] | 196 | private: |
Florin Malita | 1593577 | 2019-02-06 21:52:24 | [diff] [blame] | 197 | printing::MetafileSkia* metafile_ = nullptr; |
enne | 34f6084c | 2017-02-02 22:39:08 | [diff] [blame] | 198 | }; |
| 199 | |
enne | d250157 | 2017-03-09 19:59:17 | [diff] [blame] | 200 | class CC_PAINT_EXPORT PaintCanvasAutoRestore { |
| 201 | public: |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 202 | PaintCanvasAutoRestore(PaintCanvas* canvas, bool save) : canvas_(canvas) { |
enne | d250157 | 2017-03-09 19:59:17 | [diff] [blame] | 203 | if (canvas_) { |
| 204 | save_count_ = canvas_->getSaveCount(); |
| 205 | if (save) { |
| 206 | canvas_->save(); |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 211 | ~PaintCanvasAutoRestore() { |
enne | d250157 | 2017-03-09 19:59:17 | [diff] [blame] | 212 | if (canvas_) { |
| 213 | canvas_->restoreToCount(save_count_); |
| 214 | } |
| 215 | } |
| 216 | |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 217 | void restore() { |
enne | d250157 | 2017-03-09 19:59:17 | [diff] [blame] | 218 | if (canvas_) { |
| 219 | canvas_->restoreToCount(save_count_); |
| 220 | canvas_ = nullptr; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | private: |
| 225 | PaintCanvas* canvas_ = nullptr; |
| 226 | int save_count_ = 0; |
| 227 | }; |
| 228 | |
enne | 34f6084c | 2017-02-02 22:39:08 | [diff] [blame] | 229 | } // namespace cc |
| 230 | |
| 231 | #endif // CC_PAINT_PAINT_CANVAS_H_ |