[go: nahoru, domu]

blob: e76805b0b4987255c47694e0bc546d4c84c62d4b [file] [log] [blame]
enne98c9f8052017-03-15 19:38:221// 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"
enne98c9f8052017-03-15 19:38:2212#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"
Khushal515436e22019-04-05 05:03:5117#include "third_party/skia/include/gpu/GrContext.h"
enne98c9f8052017-03-15 19:38:2218
19namespace cc {
Khushal60916ba2018-01-19 19:47:4720class ImageProvider;
enne98c9f8052017-03-15 19:38:2221class 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.
26class CC_PAINT_EXPORT SkiaPaintCanvas final : public PaintCanvas {
27 public:
Khushal058b1a92018-02-14 21:25:5028 struct CC_PAINT_EXPORT ContextFlushes {
29 ContextFlushes();
30
31 bool enable;
32 int max_draws_before_flush;
33 };
34
Khushalaa36bff2018-02-02 01:21:3435 explicit SkiaPaintCanvas(SkCanvas* canvas,
Khushal058b1a92018-02-14 21:25:5036 ImageProvider* image_provider = nullptr,
37 ContextFlushes context_flushes = ContextFlushes());
Khushale320440a2018-08-08 17:32:2838 explicit SkiaPaintCanvas(const SkBitmap& bitmap,
39 ImageProvider* image_provider = nullptr);
enne98c9f8052017-03-15 19:38:2240 explicit SkiaPaintCanvas(const SkBitmap& bitmap, const SkSurfaceProps& props);
ccameron30a7f832017-05-30 03:10:1541 // If |target_color_space| is non-nullptr, then this will wrap |canvas| in a
42 // SkColorSpaceXformCanvas.
Khushal60916ba2018-01-19 19:47:4743 SkiaPaintCanvas(SkCanvas* canvas,
44 sk_sp<SkColorSpace> target_color_space,
Khushal058b1a92018-02-14 21:25:5045 ImageProvider* image_provider = nullptr,
46 ContextFlushes context_flushes = ContextFlushes());
Vladimir Levinf06d1cd72019-03-13 18:24:1047 SkiaPaintCanvas(const SkiaPaintCanvas&) = delete;
khushalsagarb74f9122017-03-22 04:37:4748 ~SkiaPaintCanvas() override;
enne98c9f8052017-03-15 19:38:2249
Vladimir Levinf06d1cd72019-03-13 18:24:1050 SkiaPaintCanvas& operator=(const SkiaPaintCanvas&) = delete;
51
Khushaldc0c2db2018-02-02 18:30:2352 void reset_image_provider() { image_provider_ = nullptr; }
53
enne98c9f8052017-03-15 19:38:2254 SkImageInfo imageInfo() const override;
55
Tom Andersonbec09512019-09-16 20:32:1656 void* accessTopLayerPixels(SkImageInfo* info,
57 size_t* rowBytes,
58 SkIPoint* origin = nullptr) override;
59
enne98c9f8052017-03-15 19:38:2260 void flush() override;
61
enne98c9f8052017-03-15 19:38:2262 int save() override;
63 int saveLayer(const SkRect* bounds, const PaintFlags* flags) override;
Peter Kastingfe4ba932019-01-07 20:21:0564 int saveLayerAlpha(const SkRect* bounds, uint8_t alpha) override;
enne98c9f8052017-03-15 19:38:2265
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;
enne98c9f8052017-03-15 19:38:2272 void concat(const SkMatrix& matrix) override;
73 void setMatrix(const SkMatrix& matrix) override;
enne98c9f8052017-03-15 19:38:2274
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;
enne98c9f8052017-03-15 19:38:2280 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;
enne98c9f8052017-03-15 19:38:2299 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;
vmpstr94cfa882017-04-14 01:19:35104 void drawImage(const PaintImage& image,
enne98c9f8052017-03-15 19:38:22105 SkScalar left,
106 SkScalar top,
107 const PaintFlags* flags) override;
vmpstr94cfa882017-04-14 01:19:35108 void drawImageRect(const PaintImage& image,
enne98c9f8052017-03-15 19:38:22109 const SkRect& src,
110 const SkRect& dst,
111 const PaintFlags* flags,
112 SrcRectConstraint constraint) override;
Malay Keshavcf4ceef12018-10-19 17:56:51113 void drawSkottie(scoped_refptr<SkottieWrapper> skottie,
114 const SkRect& dst,
115 float t) override;
Khushal464808c92018-10-19 23:45:59116 void drawTextBlob(sk_sp<SkTextBlob> blob,
enne98c9f8052017-03-15 19:38:22117 SkScalar x,
118 SkScalar y,
119 const PaintFlags& flags) override;
Tao Baiedaf87092019-01-30 23:12:54120 void drawTextBlob(sk_sp<SkTextBlob> blob,
121 SkScalar x,
122 SkScalar y,
Tao Bai1e1deb412019-07-21 16:14:00123 NodeId node_id,
124 const PaintFlags& flags) override;
enne98c9f8052017-03-15 19:38:22125
ennedffda502017-03-23 20:46:43126 void drawPicture(sk_sp<const PaintRecord> record) override;
enne98c9f8052017-03-15 19:38:22127
128 bool isClipEmpty() const override;
Mike Reedf97e2d162020-01-07 15:26:43129 SkMatrix getTotalMatrix() const override;
enne98c9f8052017-03-15 19:38:22130
enne5fa117a2017-04-04 20:56:58131 void Annotate(AnnotationType type,
132 const SkRect& rect,
133 sk_sp<SkData> data) override;
enne98c9f8052017-03-15 19:38:22134
135 // Don't shadow non-virtual helper functions.
Tom Andersonbec09512019-09-16 20:32:16136 using PaintCanvas::clipPath;
enne98c9f8052017-03-15 19:38:22137 using PaintCanvas::clipRect;
138 using PaintCanvas::clipRRect;
enne98c9f8052017-03-15 19:38:22139 using PaintCanvas::drawColor;
140 using PaintCanvas::drawImage;
enne8c44b142017-03-20 19:34:31141 using PaintCanvas::drawPicture;
enne98c9f8052017-03-15 19:38:22142
Wei Lie13ea642018-02-17 07:55:19143 // 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
enne98c9f8052017-03-15 19:38:22149 private:
Khushal058b1a92018-02-14 21:25:50150 void FlushAfterDrawIfNeeded();
Khushal9fd411f2018-01-20 02:09:51151
Khushal515436e22019-04-05 05:03:51152 int max_texture_size() const {
153 auto* context = canvas_->getGrContext();
154 return context ? context->maxTextureSize() : 0;
155 }
156
enne98c9f8052017-03-15 19:38:22157 SkCanvas* canvas_;
Tom Andersonbec09512019-09-16 20:32:16158 SkBitmap bitmap_;
enne98c9f8052017-03-15 19:38:22159 std::unique_ptr<SkCanvas> owned_;
Khushalaa36bff2018-02-02 01:21:34160 ImageProvider* image_provider_ = nullptr;
vmpstr4f767492017-03-22 21:24:08161
Khushal058b1a92018-02-14 21:25:50162 const ContextFlushes context_flushes_;
163 int num_of_ops_ = 0;
enne98c9f8052017-03-15 19:38:22164};
165
166} // namespace cc
167
168#endif // CC_PAINT_SKIA_PAINT_CANVAS_H_