blob: 1df2b2671659b067f86a6409ac6857c56fcd9d2a [file] [log] [blame]
Derek Sollenbergerc1908132016-07-15 10:28:16 -04001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#pragma once
17
Derek Sollenbergerc1908132016-07-15 10:28:16 -040018#include "CanvasProperty.h"
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +010019#ifdef __ANDROID__ // Layoutlib does not support hardware acceleration
Derek Sollenbergerc1908132016-07-15 10:28:16 -040020#include "DeferredLayerUpdater.h"
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +010021#endif
Derek Sollenbergerc1908132016-07-15 10:28:16 -040022#include "RenderNode.h"
23#include "VectorDrawable.h"
John Reck1bcacfd2017-11-03 10:12:19 -070024#include "hwui/Canvas.h"
Mike Reedc2dbc032019-07-25 12:28:29 -040025#include "hwui/Paint.h"
Derek Sollenbergerc1908132016-07-15 10:28:16 -040026
27#include <SkCanvas.h>
Mike Reedc2dbc032019-07-25 12:28:29 -040028#include "src/core/SkArenaAlloc.h"
Ben Wagner0ed10be2018-06-28 17:08:16 -040029
30#include <cassert>
31#include <optional>
Derek Sollenbergerc1908132016-07-15 10:28:16 -040032
33namespace android {
34
35// Holds an SkCanvas reference plus additional native data.
36class SkiaCanvas : public Canvas {
37public:
38 explicit SkiaCanvas(const SkBitmap& bitmap);
39
40 /**
41 * Create a new SkiaCanvas.
42 *
43 * @param canvas SkCanvas to handle calls made to this SkiaCanvas. Must
Mike Reed6acfe162016-11-18 17:21:09 -050044 * not be NULL. This constructor does not take ownership, so the caller
45 * must guarantee that it remains valid while the SkiaCanvas is valid.
Derek Sollenbergerc1908132016-07-15 10:28:16 -040046 */
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -040047 explicit SkiaCanvas(SkCanvas* canvas);
Derek Sollenbergerc1908132016-07-15 10:28:16 -040048
Stan Iliev021693b2016-10-17 16:26:15 -040049 virtual ~SkiaCanvas();
50
Stan Ilievc0e7a902016-10-13 17:07:09 -040051 virtual void resetRecording(int width, int height,
John Reck1bcacfd2017-11-03 10:12:19 -070052 uirenderer::RenderNode* renderNode) override {
Derek Sollenbergerc1908132016-07-15 10:28:16 -040053 LOG_ALWAYS_FATAL("SkiaCanvas cannot be reset as a recording canvas");
54 }
55
56 virtual uirenderer::DisplayList* finishRecording() override {
57 LOG_ALWAYS_FATAL("SkiaCanvas does not produce a DisplayList");
58 return nullptr;
59 }
Leon Scroggins III0f53e102020-05-05 15:53:29 -040060 virtual void enableZ(bool enableZ) override {
61 LOG_ALWAYS_FATAL("SkiaCanvas does not support enableZ");
Derek Sollenbergerc1908132016-07-15 10:28:16 -040062 }
63
64 virtual void setBitmap(const SkBitmap& bitmap) override;
65
66 virtual bool isOpaque() override;
67 virtual int width() override;
68 virtual int height() override;
69
Derek Sollenbergerc1908132016-07-15 10:28:16 -040070 virtual int getSaveCount() const override;
71 virtual int save(SaveFlags::Flags flags) override;
72 virtual void restore() override;
73 virtual void restoreToCount(int saveCount) override;
Derek Sollenbergerac33a482019-04-22 16:28:09 -040074 virtual void restoreUnclippedLayer(int saveCount, const SkPaint& paint) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -040075
John Reck1bcacfd2017-11-03 10:12:19 -070076 virtual int saveLayer(float left, float top, float right, float bottom, const SkPaint* paint,
77 SaveFlags::Flags flags) override;
78 virtual int saveLayerAlpha(float left, float top, float right, float bottom, int alpha,
79 SaveFlags::Flags flags) override;
Derek Sollenberger24fc9012018-12-07 14:12:12 -050080 virtual int saveUnclippedLayer(int left, int top, int right, int bottom) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -040081
82 virtual void getMatrix(SkMatrix* outMatrix) const override;
83 virtual void setMatrix(const SkMatrix& matrix) override;
84 virtual void concat(const SkMatrix& matrix) override;
85 virtual void rotate(float degrees) override;
86 virtual void scale(float sx, float sy) override;
87 virtual void skew(float sx, float sy) override;
88 virtual void translate(float dx, float dy) override;
89
90 virtual bool getClipBounds(SkRect* outRect) const override;
91 virtual bool quickRejectRect(float left, float top, float right, float bottom) const override;
92 virtual bool quickRejectPath(const SkPath& path) const override;
John Reck1bcacfd2017-11-03 10:12:19 -070093 virtual bool clipRect(float left, float top, float right, float bottom, SkClipOp op) override;
Mike Reed6e49c9f2016-12-02 15:36:59 -050094 virtual bool clipPath(const SkPath* path, SkClipOp op) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -040095
Ben Wagner0ed10be2018-06-28 17:08:16 -040096 virtual PaintFilter* getPaintFilter() override;
97 virtual void setPaintFilter(sk_sp<PaintFilter> paintFilter) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -040098
Matt Sarettd0814db2017-04-13 09:33:18 -040099 virtual SkCanvasState* captureCanvasState() const override;
100
Mike Reed260ab722016-10-07 15:59:20 -0400101 virtual void drawColor(int color, SkBlendMode mode) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400102 virtual void drawPaint(const SkPaint& paint) override;
103
Mike Reedc2dbc032019-07-25 12:28:29 -0400104 virtual void drawPoint(float x, float y, const Paint& paint) override;
105 virtual void drawPoints(const float* points, int count, const Paint& paint) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400106 virtual void drawLine(float startX, float startY, float stopX, float stopY,
Mike Reedc2dbc032019-07-25 12:28:29 -0400107 const Paint& paint) override;
108 virtual void drawLines(const float* points, int count, const Paint& paint) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400109 virtual void drawRect(float left, float top, float right, float bottom,
Mike Reedc2dbc032019-07-25 12:28:29 -0400110 const Paint& paint) override;
111 virtual void drawRegion(const SkRegion& region, const Paint& paint) override;
John Reck1bcacfd2017-11-03 10:12:19 -0700112 virtual void drawRoundRect(float left, float top, float right, float bottom, float rx, float ry,
Mike Reedc2dbc032019-07-25 12:28:29 -0400113 const Paint& paint) override;
Nader Jawadadfe1d92018-09-27 12:27:36 -0700114
115 virtual void drawDoubleRoundRect(const SkRRect& outer, const SkRRect& inner,
Mike Reedc2dbc032019-07-25 12:28:29 -0400116 const Paint& paint) override;
Nader Jawadadfe1d92018-09-27 12:27:36 -0700117
Mike Reedc2dbc032019-07-25 12:28:29 -0400118 virtual void drawCircle(float x, float y, float radius, const Paint& paint) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400119 virtual void drawOval(float left, float top, float right, float bottom,
Mike Reedc2dbc032019-07-25 12:28:29 -0400120 const Paint& paint) override;
John Reck1bcacfd2017-11-03 10:12:19 -0700121 virtual void drawArc(float left, float top, float right, float bottom, float startAngle,
Mike Reedc2dbc032019-07-25 12:28:29 -0400122 float sweepAngle, bool useCenter, const Paint& paint) override;
123 virtual void drawPath(const SkPath& path, const Paint& paint) override;
124 virtual void drawVertices(const SkVertices*, SkBlendMode, const Paint& paint) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400125
Mike Reedc2dbc032019-07-25 12:28:29 -0400126 virtual void drawBitmap(Bitmap& bitmap, float left, float top, const Paint* paint) override;
127 virtual void drawBitmap(Bitmap& bitmap, const SkMatrix& matrix, const Paint* paint) override;
John Reck1bcacfd2017-11-03 10:12:19 -0700128 virtual void drawBitmap(Bitmap& bitmap, float srcLeft, float srcTop, float srcRight,
129 float srcBottom, float dstLeft, float dstTop, float dstRight,
Mike Reedc2dbc032019-07-25 12:28:29 -0400130 float dstBottom, const Paint* paint) override;
sergeyv5fd2a1c2016-10-20 15:04:28 -0700131 virtual void drawBitmapMesh(Bitmap& bitmap, int meshWidth, int meshHeight,
John Reck1bcacfd2017-11-03 10:12:19 -0700132 const float* vertices, const int* colors,
Mike Reedc2dbc032019-07-25 12:28:29 -0400133 const Paint* paint) override;
John Reck1bcacfd2017-11-03 10:12:19 -0700134 virtual void drawNinePatch(Bitmap& bitmap, const android::Res_png_9patch& chunk, float dstLeft,
135 float dstTop, float dstRight, float dstBottom,
Mike Reedc2dbc032019-07-25 12:28:29 -0400136 const Paint* paint) override;
Derek Sollenberger2d142132018-01-22 10:25:26 -0500137 virtual double drawAnimatedImage(AnimatedImageDrawable* imgDrawable) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400138
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400139 virtual void drawVectorDrawable(VectorDrawableRoot* vectorDrawable) override;
140
141 virtual void drawRoundRect(uirenderer::CanvasPropertyPrimitive* left,
John Reck1bcacfd2017-11-03 10:12:19 -0700142 uirenderer::CanvasPropertyPrimitive* top,
143 uirenderer::CanvasPropertyPrimitive* right,
144 uirenderer::CanvasPropertyPrimitive* bottom,
145 uirenderer::CanvasPropertyPrimitive* rx,
146 uirenderer::CanvasPropertyPrimitive* ry,
147 uirenderer::CanvasPropertyPaint* paint) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400148 virtual void drawCircle(uirenderer::CanvasPropertyPrimitive* x,
John Reck1bcacfd2017-11-03 10:12:19 -0700149 uirenderer::CanvasPropertyPrimitive* y,
150 uirenderer::CanvasPropertyPrimitive* radius,
151 uirenderer::CanvasPropertyPaint* paint) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400152
153 virtual void drawLayer(uirenderer::DeferredLayerUpdater* layerHandle) override;
154 virtual void drawRenderNode(uirenderer::RenderNode* renderNode) override;
John Reck894e85a2019-07-15 16:16:44 -0700155 virtual void drawPicture(const SkPicture& picture) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400156
157protected:
Stan Ilievf50806a2016-10-24 10:40:39 -0400158 SkiaCanvas();
John Reck894e85a2019-07-15 16:16:44 -0700159 SkCanvas* asSkCanvas() { return mCanvas; }
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400160 void reset(SkCanvas* skiaCanvas);
161 void drawDrawable(SkDrawable* drawable) { mCanvas->drawDrawable(drawable); }
162
Mike Reed2dfd55d2019-01-08 16:19:03 -0500163 virtual void drawGlyphs(ReadGlyphFunc glyphFunc, int count, const Paint& paint, float x,
John Reck1bcacfd2017-11-03 10:12:19 -0700164 float y, float boundsLeft, float boundsTop, float boundsRight,
165 float boundsBottom, float totalAdvance) override;
Yuqian Liafc221492016-07-18 13:07:42 -0400166 virtual void drawLayoutOnPath(const minikin::Layout& layout, float hOffset, float vOffset,
Mike Reed2dfd55d2019-01-08 16:19:03 -0500167 const Paint& paint, const SkPath& path, size_t start,
John Reck1bcacfd2017-11-03 10:12:19 -0700168 size_t end) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400169
Ben Wagner0ed10be2018-06-28 17:08:16 -0400170 /** This class acts as a copy on write SkPaint.
171 *
172 * Initially this will be the SkPaint passed to the contructor.
173 * The first time writable() is called this will become a copy of the
174 * initial SkPaint (or a default SkPaint if nullptr).
175 */
176 struct PaintCoW {
177 PaintCoW(const SkPaint& that) : mPtr(&that) {}
178 PaintCoW(const SkPaint* ptr) : mPtr(ptr) {}
179 PaintCoW(const PaintCoW&) = delete;
180 PaintCoW(PaintCoW&&) = delete;
181 PaintCoW& operator=(const PaintCoW&) = delete;
182 PaintCoW& operator=(PaintCoW&&) = delete;
183 SkPaint& writeable() {
184 if (!mStorage) {
185 if (!mPtr) {
186 mStorage.emplace();
187 } else {
188 mStorage.emplace(*mPtr);
189 }
190 mPtr = &*mStorage;
191 }
192 return *mStorage;
193 }
194 operator const SkPaint*() const { return mPtr; }
195 const SkPaint* operator->() const { assert(mPtr); return mPtr; }
196 const SkPaint& operator*() const { assert(mPtr); return *mPtr; }
197 explicit operator bool() { return mPtr != nullptr; }
198 private:
199 const SkPaint* mPtr;
200 std::optional<SkPaint> mStorage;
201 };
202
203 /** Filters the paint using the current paint filter.
204 *
205 * @param paint the paint to filter. Will be initialized with the default
206 * SkPaint before filtering if filtering is required.
207 */
208 PaintCoW&& filterPaint(PaintCoW&& paint) const;
209
Mike Reedc2dbc032019-07-25 12:28:29 -0400210 template <typename Proc> void apply_looper(const Paint* paint, Proc proc) {
211 SkPaint skp;
212 SkDrawLooper* looper = nullptr;
213 if (paint) {
214 skp = *filterPaint(paint);
215 looper = paint->getLooper();
216 }
217 if (looper) {
218 SkSTArenaAlloc<256> alloc;
219 SkDrawLooper::Context* ctx = looper->makeContext(&alloc);
220 if (ctx) {
221 SkDrawLooper::Context::Info info;
222 for (;;) {
223 SkPaint p = skp;
224 if (!ctx->next(&info, &p)) {
225 break;
226 }
227 mCanvas->save();
228 mCanvas->translate(info.fTranslate.fX, info.fTranslate.fY);
229 proc(p);
230 mCanvas->restore();
231 }
232 }
233 } else {
234 proc(skp);
235 }
236 }
237
238
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400239private:
240 struct SaveRec {
John Reck1bcacfd2017-11-03 10:12:19 -0700241 int saveCount;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400242 SaveFlags::Flags saveFlags;
John Reck1bcacfd2017-11-03 10:12:19 -0700243 size_t clipIndex;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400244 };
245
Stan Ilievf50806a2016-10-24 10:40:39 -0400246 const SaveRec* currentSaveRec() const;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400247 void recordPartialSave(SaveFlags::Flags flags);
Stan Ilievf50806a2016-10-24 10:40:39 -0400248
John Reck1bcacfd2017-11-03 10:12:19 -0700249 template <typename T>
Mike Reed6e49c9f2016-12-02 15:36:59 -0500250 void recordClip(const T&, SkClipOp);
Stan Ilievf50806a2016-10-24 10:40:39 -0400251 void applyPersistentClips(size_t clipStartIndex);
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400252
Mike Reedc2dbc032019-07-25 12:28:29 -0400253 void drawPoints(const float* points, int count, const Paint& paint, SkCanvas::PointMode mode);
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400254
Stan Ilievf50806a2016-10-24 10:40:39 -0400255 class Clip;
256
John Reck1bcacfd2017-11-03 10:12:19 -0700257 std::unique_ptr<SkCanvas> mCanvasOwned; // might own a canvas we allocated
258 SkCanvas* mCanvas; // we do NOT own this canvas, it must survive us
259 // unless it is the same as mCanvasOwned.get()
260 std::unique_ptr<SkDeque> mSaveStack; // lazily allocated, tracks partial saves.
261 std::vector<Clip> mClipStack; // tracks persistent clips.
Ben Wagner0ed10be2018-06-28 17:08:16 -0400262 sk_sp<PaintFilter> mPaintFilter;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400263};
264
John Reck1bcacfd2017-11-03 10:12:19 -0700265} // namespace android