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 | d250157 | 2017-03-09 19:59:17 | [diff] [blame] | 9 | #include "base/macros.h" |
enne | ea15ddb | 2017-03-22 01:49:08 | [diff] [blame] | 10 | #include "base/memory/ref_counted.h" |
enne | 7b64edf3 | 2017-02-16 20:10:02 | [diff] [blame] | 11 | #include "build/build_config.h" |
enne | 34f6084c | 2017-02-02 22:39:08 | [diff] [blame] | 12 | #include "cc/paint/paint_export.h" |
vmpstr | 94cfa88 | 2017-04-14 01:19:35 | [diff] [blame] | 13 | #include "cc/paint/paint_image.h" |
enne | 34f6084c | 2017-02-02 22:39:08 | [diff] [blame] | 14 | #include "third_party/skia/include/core/SkCanvas.h" |
Khushal | 464808c9 | 2018-10-19 23:45:59 | [diff] [blame] | 15 | #include "third_party/skia/include/core/SkTextBlob.h" |
enne | 34f6084c | 2017-02-02 22:39:08 | [diff] [blame] | 16 | |
| 17 | namespace cc { |
Malay Keshav | cf4ceef1 | 2018-10-19 17:56:51 | [diff] [blame] | 18 | class SkottieWrapper; |
enne | d250157 | 2017-03-09 19:59:17 | [diff] [blame] | 19 | class PaintFlags; |
enne | 59df29de | 2017-05-02 03:23:37 | [diff] [blame] | 20 | class PaintOpBuffer; |
| 21 | |
| 22 | using PaintRecord = PaintOpBuffer; |
enne | 34f6084c | 2017-02-02 22:39:08 | [diff] [blame] | 23 | |
Adrienne Walker | 26aac6e5 | 2018-10-23 19:23:37 | [diff] [blame^] | 24 | // 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. |
enne | d250157 | 2017-03-09 19:59:17 | [diff] [blame] | 37 | class CC_PAINT_EXPORT PaintCanvas { |
enne | 34f6084c | 2017-02-02 22:39:08 | [diff] [blame] | 38 | public: |
enne | 59df29de | 2017-05-02 03:23:37 | [diff] [blame] | 39 | PaintCanvas() {} |
khushalsagar | b74f912 | 2017-03-22 04:37:47 | [diff] [blame] | 40 | virtual ~PaintCanvas() {} |
| 41 | |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 42 | virtual SkMetaData& getMetaData() = 0; |
enne | 59df29de | 2017-05-02 03:23:37 | [diff] [blame] | 43 | |
| 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. |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 46 | virtual SkImageInfo imageInfo() const = 0; |
enne | d250157 | 2017-03-09 19:59:17 | [diff] [blame] | 47 | |
enne | d250157 | 2017-03-09 19:59:17 | [diff] [blame] | 48 | // 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.qian | 4689dde2 | 2017-10-16 04:11:48 | [diff] [blame] | 50 | // PaintCanvasVideoRenderer which takes a PaintCanvas to paint both |
enne | d250157 | 2017-03-09 19:59:17 | [diff] [blame] | 51 | // 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. |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 54 | virtual void flush() = 0; |
enne | d250157 | 2017-03-09 19:59:17 | [diff] [blame] | 55 | |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 56 | virtual int save() = 0; |
| 57 | virtual int saveLayer(const SkRect* bounds, const PaintFlags* flags) = 0; |
danakj | f4730d5 | 2017-06-06 22:03:02 | [diff] [blame] | 58 | virtual int saveLayerAlpha(const SkRect* bounds, |
| 59 | uint8_t alpha, |
| 60 | bool preserve_lcd_text_requests) = 0; |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 61 | |
| 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; |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 68 | virtual void concat(const SkMatrix& matrix) = 0; |
| 69 | virtual void setMatrix(const SkMatrix& matrix) = 0; |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 70 | |
| 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); |
enne | d250157 | 2017-03-09 19:59:17 | [diff] [blame] | 77 | } |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 78 | void clipRect(const SkRect& rect) { |
| 79 | clipRect(rect, SkClipOp::kIntersect, false); |
enne | d250157 | 2017-03-09 19:59:17 | [diff] [blame] | 80 | } |
| 81 | |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 82 | 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); |
enne | d250157 | 2017-03-09 19:59:17 | [diff] [blame] | 87 | } |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 88 | void clipRRect(const SkRRect& rrect, SkClipOp op) { |
| 89 | clipRRect(rrect, op, false); |
enne | d250157 | 2017-03-09 19:59:17 | [diff] [blame] | 90 | } |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 91 | void clipRRect(const SkRRect& rrect) { |
| 92 | clipRRect(rrect, SkClipOp::kIntersect, false); |
enne | d250157 | 2017-03-09 19:59:17 | [diff] [blame] | 93 | } |
enne | d250157 | 2017-03-09 19:59:17 | [diff] [blame] | 94 | |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 95 | 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); |
enne | d250157 | 2017-03-09 19:59:17 | [diff] [blame] | 101 | } |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 102 | |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 103 | 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); } |
enne | 59df29de | 2017-05-02 03:23:37 | [diff] [blame] | 109 | |
| 110 | // TODO(enne): This is a synonym for drawColor with kSrc. Remove it. |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 111 | 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; |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 125 | 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; |
vmpstr | 94cfa88 | 2017-04-14 01:19:35 | [diff] [blame] | 130 | virtual void drawImage(const PaintImage& image, |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 131 | SkScalar left, |
| 132 | SkScalar top, |
| 133 | const PaintFlags* flags) = 0; |
vmpstr | 94cfa88 | 2017-04-14 01:19:35 | [diff] [blame] | 134 | void drawImage(const PaintImage& image, SkScalar left, SkScalar top) { |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 135 | drawImage(image, left, top, nullptr); |
enne | d250157 | 2017-03-09 19:59:17 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | enum SrcRectConstraint { |
| 139 | kStrict_SrcRectConstraint = SkCanvas::kStrict_SrcRectConstraint, |
| 140 | kFast_SrcRectConstraint = SkCanvas::kFast_SrcRectConstraint, |
| 141 | }; |
| 142 | |
vmpstr | 94cfa88 | 2017-04-14 01:19:35 | [diff] [blame] | 143 | virtual void drawImageRect(const PaintImage& image, |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 144 | const SkRect& src, |
| 145 | const SkRect& dst, |
| 146 | const PaintFlags* flags, |
| 147 | SrcRectConstraint constraint) = 0; |
enne | d250157 | 2017-03-09 19:59:17 | [diff] [blame] | 148 | |
Malay Keshav | cf4ceef1 | 2018-10-19 17:56:51 | [diff] [blame] | 149 | // 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 | |
Khushal | 464808c9 | 2018-10-19 23:45:59 | [diff] [blame] | 156 | virtual void drawTextBlob(sk_sp<SkTextBlob> blob, |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 157 | SkScalar x, |
| 158 | SkScalar y, |
| 159 | const PaintFlags& flags) = 0; |
enne | d250157 | 2017-03-09 19:59:17 | [diff] [blame] | 160 | |
Adrienne Walker | 15ce2350 | 2017-05-08 18:08:15 | [diff] [blame] | 161 | // Unlike SkCanvas::drawPicture, this only plays back the PaintRecord and does |
| 162 | // not add an additional clip. This is closer to SkPicture::playback. |
enne | dffda50 | 2017-03-23 20:46:43 | [diff] [blame] | 163 | virtual void drawPicture(sk_sp<const PaintRecord> record) = 0; |
enne | d250157 | 2017-03-09 19:59:17 | [diff] [blame] | 164 | |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 165 | virtual bool isClipEmpty() const = 0; |
| 166 | virtual bool isClipRect() const = 0; |
| 167 | virtual const SkMatrix& getTotalMatrix() const = 0; |
enne | d250157 | 2017-03-09 19:59:17 | [diff] [blame] | 168 | |
enne | 5fa117a | 2017-04-04 20:56:58 | [diff] [blame] | 169 | 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; |
enne | d250157 | 2017-03-09 19:59:17 | [diff] [blame] | 177 | |
Wei Li | 2a9bfe4 | 2018-01-13 05:42:56 | [diff] [blame] | 178 | // Subclasses can override to handle custom data. |
| 179 | virtual void recordCustomData(uint32_t id) {} |
| 180 | |
enne | 59df29de | 2017-05-02 03:23:37 | [diff] [blame] | 181 | private: |
| 182 | DISALLOW_COPY_AND_ASSIGN(PaintCanvas); |
enne | 34f6084c | 2017-02-02 22:39:08 | [diff] [blame] | 183 | }; |
| 184 | |
enne | d250157 | 2017-03-09 19:59:17 | [diff] [blame] | 185 | class CC_PAINT_EXPORT PaintCanvasAutoRestore { |
| 186 | public: |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 187 | PaintCanvasAutoRestore(PaintCanvas* canvas, bool save) : canvas_(canvas) { |
enne | d250157 | 2017-03-09 19:59:17 | [diff] [blame] | 188 | if (canvas_) { |
| 189 | save_count_ = canvas_->getSaveCount(); |
| 190 | if (save) { |
| 191 | canvas_->save(); |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 196 | ~PaintCanvasAutoRestore() { |
enne | d250157 | 2017-03-09 19:59:17 | [diff] [blame] | 197 | if (canvas_) { |
| 198 | canvas_->restoreToCount(save_count_); |
| 199 | } |
| 200 | } |
| 201 | |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 202 | void restore() { |
enne | d250157 | 2017-03-09 19:59:17 | [diff] [blame] | 203 | 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 | |
enne | 34f6084c | 2017-02-02 22:39:08 | [diff] [blame] | 214 | } // namespace cc |
| 215 | |
| 216 | #endif // CC_PAINT_PAINT_CANVAS_H_ |