enne | 98c9f805 | 2017-03-15 19:38:22 | [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_SKIA_PAINT_CANVAS_H_ |
| 6 | #define CC_PAINT_SKIA_PAINT_CANVAS_H_ |
| 7 | |
| 8 | #include <memory> |
| 9 | |
| 10 | #include "base/compiler_specific.h" |
| 11 | #include "base/logging.h" |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 12 | #include "build/build_config.h" |
| 13 | #include "cc/paint/paint_canvas.h" |
| 14 | #include "cc/paint/paint_flags.h" |
| 15 | #include "cc/paint/paint_record.h" |
| 16 | #include "third_party/skia/include/core/SkCanvas.h" |
Khushal | 515436e2 | 2019-04-05 05:03:51 | [diff] [blame] | 17 | #include "third_party/skia/include/gpu/GrContext.h" |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 18 | |
| 19 | namespace cc { |
Khushal | 60916ba | 2018-01-19 19:47:47 | [diff] [blame] | 20 | class ImageProvider; |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 21 | class PaintFlags; |
| 22 | |
| 23 | // A PaintCanvas derived class that passes PaintCanvas APIs through to |
| 24 | // an SkCanvas. This is more efficient than recording to a PaintRecord |
| 25 | // and then playing back to an SkCanvas. |
| 26 | class CC_PAINT_EXPORT SkiaPaintCanvas final : public PaintCanvas { |
| 27 | public: |
Khushal | 058b1a9 | 2018-02-14 21:25:50 | [diff] [blame] | 28 | struct CC_PAINT_EXPORT ContextFlushes { |
| 29 | ContextFlushes(); |
| 30 | |
| 31 | bool enable; |
| 32 | int max_draws_before_flush; |
| 33 | }; |
| 34 | |
Khushal | aa36bff | 2018-02-02 01:21:34 | [diff] [blame] | 35 | explicit SkiaPaintCanvas(SkCanvas* canvas, |
Khushal | 058b1a9 | 2018-02-14 21:25:50 | [diff] [blame] | 36 | ImageProvider* image_provider = nullptr, |
| 37 | ContextFlushes context_flushes = ContextFlushes()); |
Khushal | e320440a | 2018-08-08 17:32:28 | [diff] [blame] | 38 | explicit SkiaPaintCanvas(const SkBitmap& bitmap, |
| 39 | ImageProvider* image_provider = nullptr); |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 40 | explicit SkiaPaintCanvas(const SkBitmap& bitmap, const SkSurfaceProps& props); |
ccameron | 30a7f83 | 2017-05-30 03:10:15 | [diff] [blame] | 41 | // If |target_color_space| is non-nullptr, then this will wrap |canvas| in a |
| 42 | // SkColorSpaceXformCanvas. |
Khushal | 60916ba | 2018-01-19 19:47:47 | [diff] [blame] | 43 | SkiaPaintCanvas(SkCanvas* canvas, |
| 44 | sk_sp<SkColorSpace> target_color_space, |
Khushal | 058b1a9 | 2018-02-14 21:25:50 | [diff] [blame] | 45 | ImageProvider* image_provider = nullptr, |
| 46 | ContextFlushes context_flushes = ContextFlushes()); |
Vladimir Levin | f06d1cd7 | 2019-03-13 18:24:10 | [diff] [blame] | 47 | SkiaPaintCanvas(const SkiaPaintCanvas&) = delete; |
khushalsagar | b74f912 | 2017-03-22 04:37:47 | [diff] [blame] | 48 | ~SkiaPaintCanvas() override; |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 49 | |
Vladimir Levin | f06d1cd7 | 2019-03-13 18:24:10 | [diff] [blame] | 50 | SkiaPaintCanvas& operator=(const SkiaPaintCanvas&) = delete; |
| 51 | |
Khushal | dc0c2db | 2018-02-02 18:30:23 | [diff] [blame] | 52 | void reset_image_provider() { image_provider_ = nullptr; } |
| 53 | |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 54 | SkImageInfo imageInfo() const override; |
| 55 | |
Tom Anderson | bec0951 | 2019-09-16 20:32:16 | [diff] [blame] | 56 | void* accessTopLayerPixels(SkImageInfo* info, |
| 57 | size_t* rowBytes, |
| 58 | SkIPoint* origin = nullptr) override; |
| 59 | |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 60 | void flush() override; |
| 61 | |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 62 | int save() override; |
| 63 | int saveLayer(const SkRect* bounds, const PaintFlags* flags) override; |
Peter Kasting | fe4ba93 | 2019-01-07 20:21:05 | [diff] [blame] | 64 | int saveLayerAlpha(const SkRect* bounds, uint8_t alpha) override; |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 65 | |
| 66 | void restore() override; |
| 67 | int getSaveCount() const override; |
| 68 | void restoreToCount(int save_count) override; |
| 69 | void translate(SkScalar dx, SkScalar dy) override; |
| 70 | void scale(SkScalar sx, SkScalar sy) override; |
| 71 | void rotate(SkScalar degrees) override; |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 72 | void concat(const SkMatrix& matrix) override; |
| 73 | void setMatrix(const SkMatrix& matrix) override; |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 74 | |
| 75 | void clipRect(const SkRect& rect, SkClipOp op, bool do_anti_alias) override; |
| 76 | void clipRRect(const SkRRect& rrect, |
| 77 | SkClipOp op, |
| 78 | bool do_anti_alias) override; |
| 79 | void clipPath(const SkPath& path, SkClipOp op, bool do_anti_alias) override; |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 80 | SkRect getLocalClipBounds() const override; |
| 81 | bool getLocalClipBounds(SkRect* bounds) const override; |
| 82 | SkIRect getDeviceClipBounds() const override; |
| 83 | bool getDeviceClipBounds(SkIRect* bounds) const override; |
| 84 | void drawColor(SkColor color, SkBlendMode mode) override; |
| 85 | void clear(SkColor color) override; |
| 86 | |
| 87 | void drawLine(SkScalar x0, |
| 88 | SkScalar y0, |
| 89 | SkScalar x1, |
| 90 | SkScalar y1, |
| 91 | const PaintFlags& flags) override; |
| 92 | void drawRect(const SkRect& rect, const PaintFlags& flags) override; |
| 93 | void drawIRect(const SkIRect& rect, const PaintFlags& flags) override; |
| 94 | void drawOval(const SkRect& oval, const PaintFlags& flags) override; |
| 95 | void drawRRect(const SkRRect& rrect, const PaintFlags& flags) override; |
| 96 | void drawDRRect(const SkRRect& outer, |
| 97 | const SkRRect& inner, |
| 98 | const PaintFlags& flags) override; |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 99 | void drawRoundRect(const SkRect& rect, |
| 100 | SkScalar rx, |
| 101 | SkScalar ry, |
| 102 | const PaintFlags& flags) override; |
| 103 | void drawPath(const SkPath& path, const PaintFlags& flags) override; |
vmpstr | 94cfa88 | 2017-04-14 01:19:35 | [diff] [blame] | 104 | void drawImage(const PaintImage& image, |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 105 | SkScalar left, |
| 106 | SkScalar top, |
| 107 | const PaintFlags* flags) override; |
vmpstr | 94cfa88 | 2017-04-14 01:19:35 | [diff] [blame] | 108 | void drawImageRect(const PaintImage& image, |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 109 | const SkRect& src, |
| 110 | const SkRect& dst, |
| 111 | const PaintFlags* flags, |
| 112 | SrcRectConstraint constraint) override; |
Malay Keshav | cf4ceef1 | 2018-10-19 17:56:51 | [diff] [blame] | 113 | void drawSkottie(scoped_refptr<SkottieWrapper> skottie, |
| 114 | const SkRect& dst, |
| 115 | float t) override; |
Khushal | 464808c9 | 2018-10-19 23:45:59 | [diff] [blame] | 116 | void drawTextBlob(sk_sp<SkTextBlob> blob, |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 117 | SkScalar x, |
| 118 | SkScalar y, |
| 119 | const PaintFlags& flags) override; |
Tao Bai | edaf8709 | 2019-01-30 23:12:54 | [diff] [blame] | 120 | void drawTextBlob(sk_sp<SkTextBlob> blob, |
| 121 | SkScalar x, |
| 122 | SkScalar y, |
Tao Bai | 1e1deb41 | 2019-07-21 16:14:00 | [diff] [blame] | 123 | NodeId node_id, |
| 124 | const PaintFlags& flags) override; |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 125 | |
enne | dffda50 | 2017-03-23 20:46:43 | [diff] [blame] | 126 | void drawPicture(sk_sp<const PaintRecord> record) override; |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 127 | |
| 128 | bool isClipEmpty() const override; |
Mike Reed | f97e2d16 | 2020-01-07 15:26:43 | [diff] [blame] | 129 | SkMatrix getTotalMatrix() const override; |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 130 | |
enne | 5fa117a | 2017-04-04 20:56:58 | [diff] [blame] | 131 | void Annotate(AnnotationType type, |
| 132 | const SkRect& rect, |
| 133 | sk_sp<SkData> data) override; |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 134 | |
| 135 | // Don't shadow non-virtual helper functions. |
Tom Anderson | bec0951 | 2019-09-16 20:32:16 | [diff] [blame] | 136 | using PaintCanvas::clipPath; |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 137 | using PaintCanvas::clipRect; |
| 138 | using PaintCanvas::clipRRect; |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 139 | using PaintCanvas::drawColor; |
| 140 | using PaintCanvas::drawImage; |
enne | 8c44b14 | 2017-03-20 19:34:31 | [diff] [blame] | 141 | using PaintCanvas::drawPicture; |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 142 | |
Wei Li | e13ea64 | 2018-02-17 07:55:19 | [diff] [blame] | 143 | // Same as the above drawPicture() except using the given custom data |
| 144 | // raster callback. |
| 145 | void drawPicture( |
| 146 | sk_sp<const PaintRecord> record, |
| 147 | PlaybackParams::CustomDataRasterCallback custom_raster_callback); |
| 148 | |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 149 | private: |
Khushal | 058b1a9 | 2018-02-14 21:25:50 | [diff] [blame] | 150 | void FlushAfterDrawIfNeeded(); |
Khushal | 9fd411f | 2018-01-20 02:09:51 | [diff] [blame] | 151 | |
Khushal | 515436e2 | 2019-04-05 05:03:51 | [diff] [blame] | 152 | int max_texture_size() const { |
| 153 | auto* context = canvas_->getGrContext(); |
| 154 | return context ? context->maxTextureSize() : 0; |
| 155 | } |
| 156 | |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 157 | SkCanvas* canvas_; |
Tom Anderson | bec0951 | 2019-09-16 20:32:16 | [diff] [blame] | 158 | SkBitmap bitmap_; |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 159 | std::unique_ptr<SkCanvas> owned_; |
Khushal | aa36bff | 2018-02-02 01:21:34 | [diff] [blame] | 160 | ImageProvider* image_provider_ = nullptr; |
vmpstr | 4f76749 | 2017-03-22 21:24:08 | [diff] [blame] | 161 | |
Khushal | 058b1a9 | 2018-02-14 21:25:50 | [diff] [blame] | 162 | const ContextFlushes context_flushes_; |
| 163 | int num_of_ops_ = 0; |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 164 | }; |
| 165 | |
| 166 | } // namespace cc |
| 167 | |
| 168 | #endif // CC_PAINT_SKIA_PAINT_CANVAS_H_ |