blob: e0a0be54fb39bdadd1d8faefeecadbb01f941d0f [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
John Reck2e48df32021-01-19 18:06:05 -050056 virtual void finishRecording(uirenderer::RenderNode*) override {
Derek Sollenbergerc1908132016-07-15 10:28:16 -040057 LOG_ALWAYS_FATAL("SkiaCanvas does not produce a DisplayList");
Derek Sollenbergerc1908132016-07-15 10:28:16 -040058 }
Leon Scroggins III0f53e102020-05-05 15:53:29 -040059 virtual void enableZ(bool enableZ) override {
60 LOG_ALWAYS_FATAL("SkiaCanvas does not support enableZ");
Derek Sollenbergerc1908132016-07-15 10:28:16 -040061 }
62
Nader Jawad2dc632a2021-03-29 18:51:29 -070063 virtual void punchHole(const SkRRect& rect) override;
64
Derek Sollenbergerc1908132016-07-15 10:28:16 -040065 virtual void setBitmap(const SkBitmap& bitmap) override;
66
67 virtual bool isOpaque() override;
68 virtual int width() override;
69 virtual int height() override;
70
Derek Sollenbergerc1908132016-07-15 10:28:16 -040071 virtual int getSaveCount() const override;
72 virtual int save(SaveFlags::Flags flags) override;
73 virtual void restore() override;
74 virtual void restoreToCount(int saveCount) override;
Derek Sollenbergerac33a482019-04-22 16:28:09 -040075 virtual void restoreUnclippedLayer(int saveCount, const SkPaint& paint) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -040076
John Recka00eef212020-11-16 12:45:55 -050077 virtual int saveLayer(float left, float top, float right, float bottom, const SkPaint* paint) override;
78 virtual int saveLayerAlpha(float left, float top, float right, float bottom, int alpha) override;
Derek Sollenberger24fc9012018-12-07 14:12:12 -050079 virtual int saveUnclippedLayer(int left, int top, int right, int bottom) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -040080
81 virtual void getMatrix(SkMatrix* outMatrix) const override;
82 virtual void setMatrix(const SkMatrix& matrix) override;
83 virtual void concat(const SkMatrix& matrix) override;
84 virtual void rotate(float degrees) override;
85 virtual void scale(float sx, float sy) override;
86 virtual void skew(float sx, float sy) override;
87 virtual void translate(float dx, float dy) override;
88
89 virtual bool getClipBounds(SkRect* outRect) const override;
90 virtual bool quickRejectRect(float left, float top, float right, float bottom) const override;
91 virtual bool quickRejectPath(const SkPath& path) const override;
John Reck1bcacfd2017-11-03 10:12:19 -070092 virtual bool clipRect(float left, float top, float right, float bottom, SkClipOp op) override;
Mike Reed6e49c9f2016-12-02 15:36:59 -050093 virtual bool clipPath(const SkPath* path, SkClipOp op) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -040094
Ben Wagner0ed10be2018-06-28 17:08:16 -040095 virtual PaintFilter* getPaintFilter() override;
96 virtual void setPaintFilter(sk_sp<PaintFilter> paintFilter) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -040097
Matt Sarettd0814db2017-04-13 09:33:18 -040098 virtual SkCanvasState* captureCanvasState() const override;
99
Mike Reed260ab722016-10-07 15:59:20 -0400100 virtual void drawColor(int color, SkBlendMode mode) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400101 virtual void drawPaint(const SkPaint& paint) override;
102
Mike Reedc2dbc032019-07-25 12:28:29 -0400103 virtual void drawPoint(float x, float y, const Paint& paint) override;
104 virtual void drawPoints(const float* points, int count, const Paint& paint) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400105 virtual void drawLine(float startX, float startY, float stopX, float stopY,
Mike Reedc2dbc032019-07-25 12:28:29 -0400106 const Paint& paint) override;
107 virtual void drawLines(const float* points, int count, const Paint& paint) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400108 virtual void drawRect(float left, float top, float right, float bottom,
Mike Reedc2dbc032019-07-25 12:28:29 -0400109 const Paint& paint) override;
110 virtual void drawRegion(const SkRegion& region, const Paint& paint) override;
John Reck1bcacfd2017-11-03 10:12:19 -0700111 virtual void drawRoundRect(float left, float top, float right, float bottom, float rx, float ry,
Mike Reedc2dbc032019-07-25 12:28:29 -0400112 const Paint& paint) override;
Nader Jawadadfe1d92018-09-27 12:27:36 -0700113
114 virtual void drawDoubleRoundRect(const SkRRect& outer, const SkRRect& inner,
Mike Reedc2dbc032019-07-25 12:28:29 -0400115 const Paint& paint) override;
Nader Jawadadfe1d92018-09-27 12:27:36 -0700116
Mike Reedc2dbc032019-07-25 12:28:29 -0400117 virtual void drawCircle(float x, float y, float radius, const Paint& paint) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400118 virtual void drawOval(float left, float top, float right, float bottom,
Mike Reedc2dbc032019-07-25 12:28:29 -0400119 const Paint& paint) override;
John Reck1bcacfd2017-11-03 10:12:19 -0700120 virtual void drawArc(float left, float top, float right, float bottom, float startAngle,
Mike Reedc2dbc032019-07-25 12:28:29 -0400121 float sweepAngle, bool useCenter, const Paint& paint) override;
122 virtual void drawPath(const SkPath& path, const Paint& paint) override;
123 virtual void drawVertices(const SkVertices*, SkBlendMode, const Paint& paint) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400124
Mike Reedc2dbc032019-07-25 12:28:29 -0400125 virtual void drawBitmap(Bitmap& bitmap, float left, float top, const Paint* paint) override;
126 virtual void drawBitmap(Bitmap& bitmap, const SkMatrix& matrix, const Paint* paint) override;
John Reck1bcacfd2017-11-03 10:12:19 -0700127 virtual void drawBitmap(Bitmap& bitmap, float srcLeft, float srcTop, float srcRight,
128 float srcBottom, float dstLeft, float dstTop, float dstRight,
Mike Reedc2dbc032019-07-25 12:28:29 -0400129 float dstBottom, const Paint* paint) override;
sergeyv5fd2a1c2016-10-20 15:04:28 -0700130 virtual void drawBitmapMesh(Bitmap& bitmap, int meshWidth, int meshHeight,
John Reck1bcacfd2017-11-03 10:12:19 -0700131 const float* vertices, const int* colors,
Mike Reedc2dbc032019-07-25 12:28:29 -0400132 const Paint* paint) override;
John Reck1bcacfd2017-11-03 10:12:19 -0700133 virtual void drawNinePatch(Bitmap& bitmap, const android::Res_png_9patch& chunk, float dstLeft,
134 float dstTop, float dstRight, float dstBottom,
Mike Reedc2dbc032019-07-25 12:28:29 -0400135 const Paint* paint) override;
Derek Sollenberger2d142132018-01-22 10:25:26 -0500136 virtual double drawAnimatedImage(AnimatedImageDrawable* imgDrawable) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400137
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400138 virtual void drawVectorDrawable(VectorDrawableRoot* vectorDrawable) override;
139
140 virtual void drawRoundRect(uirenderer::CanvasPropertyPrimitive* left,
John Reck1bcacfd2017-11-03 10:12:19 -0700141 uirenderer::CanvasPropertyPrimitive* top,
142 uirenderer::CanvasPropertyPrimitive* right,
143 uirenderer::CanvasPropertyPrimitive* bottom,
144 uirenderer::CanvasPropertyPrimitive* rx,
145 uirenderer::CanvasPropertyPrimitive* ry,
146 uirenderer::CanvasPropertyPaint* paint) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400147 virtual void drawCircle(uirenderer::CanvasPropertyPrimitive* x,
John Reck1bcacfd2017-11-03 10:12:19 -0700148 uirenderer::CanvasPropertyPrimitive* y,
149 uirenderer::CanvasPropertyPrimitive* radius,
150 uirenderer::CanvasPropertyPaint* paint) override;
Derek Sollenbergerdf301aa2020-12-17 11:06:03 -0500151 virtual void drawRipple(uirenderer::CanvasPropertyPrimitive* x,
152 uirenderer::CanvasPropertyPrimitive* y,
153 uirenderer::CanvasPropertyPrimitive* radius,
154 uirenderer::CanvasPropertyPaint* paint,
155 uirenderer::CanvasPropertyPrimitive* progress,
Derek Sollenberger783e5ae2021-01-11 15:44:46 -0500156 const SkRuntimeShaderBuilder& effectBuilder) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400157
158 virtual void drawLayer(uirenderer::DeferredLayerUpdater* layerHandle) override;
159 virtual void drawRenderNode(uirenderer::RenderNode* renderNode) override;
John Reck894e85a2019-07-15 16:16:44 -0700160 virtual void drawPicture(const SkPicture& picture) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400161
162protected:
Stan Ilievf50806a2016-10-24 10:40:39 -0400163 SkiaCanvas();
John Reck894e85a2019-07-15 16:16:44 -0700164 SkCanvas* asSkCanvas() { return mCanvas; }
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400165 void reset(SkCanvas* skiaCanvas);
166 void drawDrawable(SkDrawable* drawable) { mCanvas->drawDrawable(drawable); }
167
Mike Reed2dfd55d2019-01-08 16:19:03 -0500168 virtual void drawGlyphs(ReadGlyphFunc glyphFunc, int count, const Paint& paint, float x,
Seigo Nonaka63af7ba2020-09-21 23:07:43 -0700169 float y, float totalAdvance) override;
Yuqian Liafc221492016-07-18 13:07:42 -0400170 virtual void drawLayoutOnPath(const minikin::Layout& layout, float hOffset, float vOffset,
Mike Reed2dfd55d2019-01-08 16:19:03 -0500171 const Paint& paint, const SkPath& path, size_t start,
John Reck1bcacfd2017-11-03 10:12:19 -0700172 size_t end) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400173
Mike Reedbb4cb482021-02-22 14:21:20 -0500174 void onFilterPaint(SkPaint& paint);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400175
Mike Reedbb4cb482021-02-22 14:21:20 -0500176 SkPaint filterPaint(const SkPaint& src) {
177 SkPaint dst(src);
178 this->onFilterPaint(dst);
179 return dst;
180 }
Ben Wagner0ed10be2018-06-28 17:08:16 -0400181
Mike Reed0f9dce72021-02-12 21:20:33 -0500182 // proc(const SkPaint& modifiedPaint)
Mike Reedbb4cb482021-02-22 14:21:20 -0500183 template <typename Proc>
184 void applyLooper(const Paint* paint, Proc proc, void (*preFilter)(SkPaint&) = nullptr) {
185 BlurDrawLooper* looper = paint ? paint->getLooper() : nullptr;
186 const SkPaint* skpPtr = paint;
187 SkPaint skp = skpPtr ? *skpPtr : SkPaint();
188 if (preFilter) {
189 preFilter(skp);
Mike Reedc2dbc032019-07-25 12:28:29 -0400190 }
Mike Reedbb4cb482021-02-22 14:21:20 -0500191 this->onFilterPaint(skp);
Mike Reedc2dbc032019-07-25 12:28:29 -0400192 if (looper) {
Mike Reed0f9dce72021-02-12 21:20:33 -0500193 looper->apply(skp, [&](SkPoint offset, const SkPaint& modifiedPaint) {
194 mCanvas->save();
195 mCanvas->translate(offset.fX, offset.fY);
196 proc(modifiedPaint);
197 mCanvas->restore();
198 });
Mike Reedc2dbc032019-07-25 12:28:29 -0400199 } else {
200 proc(skp);
201 }
202 }
203
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400204private:
205 struct SaveRec {
John Reck1bcacfd2017-11-03 10:12:19 -0700206 int saveCount;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400207 SaveFlags::Flags saveFlags;
John Reck1bcacfd2017-11-03 10:12:19 -0700208 size_t clipIndex;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400209 };
210
Stan Ilievf50806a2016-10-24 10:40:39 -0400211 const SaveRec* currentSaveRec() const;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400212 void recordPartialSave(SaveFlags::Flags flags);
Stan Ilievf50806a2016-10-24 10:40:39 -0400213
John Reck1bcacfd2017-11-03 10:12:19 -0700214 template <typename T>
Mike Reed6e49c9f2016-12-02 15:36:59 -0500215 void recordClip(const T&, SkClipOp);
Stan Ilievf50806a2016-10-24 10:40:39 -0400216 void applyPersistentClips(size_t clipStartIndex);
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400217
Mike Reedc2dbc032019-07-25 12:28:29 -0400218 void drawPoints(const float* points, int count, const Paint& paint, SkCanvas::PointMode mode);
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400219
Stan Ilievf50806a2016-10-24 10:40:39 -0400220 class Clip;
221
John Reck1bcacfd2017-11-03 10:12:19 -0700222 std::unique_ptr<SkCanvas> mCanvasOwned; // might own a canvas we allocated
223 SkCanvas* mCanvas; // we do NOT own this canvas, it must survive us
224 // unless it is the same as mCanvasOwned.get()
225 std::unique_ptr<SkDeque> mSaveStack; // lazily allocated, tracks partial saves.
226 std::vector<Clip> mClipStack; // tracks persistent clips.
Ben Wagner0ed10be2018-06-28 17:08:16 -0400227 sk_sp<PaintFilter> mPaintFilter;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400228};
229
John Reck1bcacfd2017-11-03 10:12:19 -0700230} // namespace android