blob: 5e3553bbbbb4301c9c2868249c4e1650033d045f [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 <SkCanvas.h>
Ben Wagner0ed10be2018-06-28 17:08:16 -040023
24#include <cassert>
Kevin Lubickb3731212023-01-05 19:52:09 +000025#include <deque>
Ben Wagner0ed10be2018-06-28 17:08:16 -040026#include <optional>
Derek Sollenbergerc1908132016-07-15 10:28:16 -040027
Kevin Lubickb3731212023-01-05 19:52:09 +000028#include "RenderNode.h"
29#include "VectorDrawable.h"
30#include "hwui/BlurDrawLooper.h"
31#include "hwui/Canvas.h"
32#include "hwui/Paint.h"
33#include "pipeline/skia/AnimatedDrawables.h"
Kevin Lubickb3731212023-01-05 19:52:09 +000034
Kevin Lubick4e8ce462022-12-01 20:29:16 +000035enum class SkBlendMode;
Kevin Lubick856848e2022-02-24 16:24:09 +000036class SkRRect;
37
Derek Sollenbergerc1908132016-07-15 10:28:16 -040038namespace android {
39
40// Holds an SkCanvas reference plus additional native data.
41class SkiaCanvas : public Canvas {
42public:
43 explicit SkiaCanvas(const SkBitmap& bitmap);
44
45 /**
46 * Create a new SkiaCanvas.
47 *
48 * @param canvas SkCanvas to handle calls made to this SkiaCanvas. Must
Mike Reed6acfe162016-11-18 17:21:09 -050049 * not be NULL. This constructor does not take ownership, so the caller
50 * must guarantee that it remains valid while the SkiaCanvas is valid.
Derek Sollenbergerc1908132016-07-15 10:28:16 -040051 */
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -040052 explicit SkiaCanvas(SkCanvas* canvas);
Derek Sollenbergerc1908132016-07-15 10:28:16 -040053
Stan Iliev021693b2016-10-17 16:26:15 -040054 virtual ~SkiaCanvas();
55
Stan Ilievc0e7a902016-10-13 17:07:09 -040056 virtual void resetRecording(int width, int height,
John Reck1bcacfd2017-11-03 10:12:19 -070057 uirenderer::RenderNode* renderNode) override {
Derek Sollenbergerc1908132016-07-15 10:28:16 -040058 LOG_ALWAYS_FATAL("SkiaCanvas cannot be reset as a recording canvas");
59 }
60
John Reck2e48df32021-01-19 18:06:05 -050061 virtual void finishRecording(uirenderer::RenderNode*) override {
Derek Sollenbergerc1908132016-07-15 10:28:16 -040062 LOG_ALWAYS_FATAL("SkiaCanvas does not produce a DisplayList");
Derek Sollenbergerc1908132016-07-15 10:28:16 -040063 }
Leon Scroggins III0f53e102020-05-05 15:53:29 -040064 virtual void enableZ(bool enableZ) override {
65 LOG_ALWAYS_FATAL("SkiaCanvas does not support enableZ");
Derek Sollenbergerc1908132016-07-15 10:28:16 -040066 }
67
Alec Mouri655a5e42022-09-12 17:49:17 +000068 virtual void punchHole(const SkRRect& rect, float alpha) override;
Nader Jawad2dc632a2021-03-29 18:51:29 -070069
Derek Sollenbergerc1908132016-07-15 10:28:16 -040070 virtual void setBitmap(const SkBitmap& bitmap) override;
71
72 virtual bool isOpaque() override;
73 virtual int width() override;
74 virtual int height() override;
75
Derek Sollenbergerc1908132016-07-15 10:28:16 -040076 virtual int getSaveCount() const override;
77 virtual int save(SaveFlags::Flags flags) override;
78 virtual void restore() override;
79 virtual void restoreToCount(int saveCount) override;
Mike Reeda6cb58a2021-07-10 13:31:34 -040080 virtual void restoreUnclippedLayer(int saveCount, const Paint& paint) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -040081
John Recka00eef212020-11-16 12:45:55 -050082 virtual int saveLayer(float left, float top, float right, float bottom, const SkPaint* paint) override;
83 virtual int saveLayerAlpha(float left, float top, float right, float bottom, int alpha) override;
Derek Sollenberger24fc9012018-12-07 14:12:12 -050084 virtual int saveUnclippedLayer(int left, int top, int right, int bottom) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -040085
86 virtual void getMatrix(SkMatrix* outMatrix) const override;
87 virtual void setMatrix(const SkMatrix& matrix) override;
88 virtual void concat(const SkMatrix& matrix) override;
Jorge Betancourtc9806fadcc2023-12-04 15:30:18 -050089 virtual void concat(const SkM44& matrix) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -040090 virtual void rotate(float degrees) override;
91 virtual void scale(float sx, float sy) override;
92 virtual void skew(float sx, float sy) override;
93 virtual void translate(float dx, float dy) override;
94
95 virtual bool getClipBounds(SkRect* outRect) const override;
96 virtual bool quickRejectRect(float left, float top, float right, float bottom) const override;
97 virtual bool quickRejectPath(const SkPath& path) const override;
John Reck1bcacfd2017-11-03 10:12:19 -070098 virtual bool clipRect(float left, float top, float right, float bottom, SkClipOp op) override;
Mike Reed6e49c9f2016-12-02 15:36:59 -050099 virtual bool clipPath(const SkPath* path, SkClipOp op) override;
Michael Ludwig70cf50c22021-07-21 17:02:39 +0000100 virtual bool replaceClipRect_deprecated(float left, float top, float right,
101 float bottom) override;
102 virtual bool replaceClipPath_deprecated(const SkPath* path) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400103
Ben Wagner0ed10be2018-06-28 17:08:16 -0400104 virtual PaintFilter* getPaintFilter() override;
105 virtual void setPaintFilter(sk_sp<PaintFilter> paintFilter) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400106
Matt Sarettd0814db2017-04-13 09:33:18 -0400107 virtual SkCanvasState* captureCanvasState() const override;
108
Mike Reed260ab722016-10-07 15:59:20 -0400109 virtual void drawColor(int color, SkBlendMode mode) override;
Mike Reeda6cb58a2021-07-10 13:31:34 -0400110 virtual void drawPaint(const Paint& paint) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400111
Mike Reedc2dbc032019-07-25 12:28:29 -0400112 virtual void drawPoint(float x, float y, const Paint& paint) override;
113 virtual void drawPoints(const float* points, int count, const Paint& paint) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400114 virtual void drawLine(float startX, float startY, float stopX, float stopY,
Mike Reedc2dbc032019-07-25 12:28:29 -0400115 const Paint& paint) override;
116 virtual void drawLines(const float* points, int count, const Paint& paint) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400117 virtual void drawRect(float left, float top, float right, float bottom,
Mike Reedc2dbc032019-07-25 12:28:29 -0400118 const Paint& paint) override;
119 virtual void drawRegion(const SkRegion& region, const Paint& paint) override;
John Reck1bcacfd2017-11-03 10:12:19 -0700120 virtual void drawRoundRect(float left, float top, float right, float bottom, float rx, float ry,
Mike Reedc2dbc032019-07-25 12:28:29 -0400121 const Paint& paint) override;
Nader Jawadadfe1d92018-09-27 12:27:36 -0700122
Angel Aguayo90c46ee2022-11-08 23:42:09 +0000123 virtual void drawDoubleRoundRect(const SkRRect& outer, const SkRRect& inner,
124 const Paint& paint) override;
Nader Jawadadfe1d92018-09-27 12:27:36 -0700125
Mike Reedc2dbc032019-07-25 12:28:29 -0400126 virtual void drawCircle(float x, float y, float radius, const Paint& paint) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400127 virtual void drawOval(float left, float top, float right, float bottom,
Mike Reedc2dbc032019-07-25 12:28:29 -0400128 const Paint& paint) override;
John Reck1bcacfd2017-11-03 10:12:19 -0700129 virtual void drawArc(float left, float top, float right, float bottom, float startAngle,
Mike Reedc2dbc032019-07-25 12:28:29 -0400130 float sweepAngle, bool useCenter, const Paint& paint) override;
131 virtual void drawPath(const SkPath& path, const Paint& paint) override;
132 virtual void drawVertices(const SkVertices*, SkBlendMode, const Paint& paint) override;
Nader Jawad5f0a8002023-02-21 17:00:51 -0800133 virtual void drawMesh(const Mesh& mesh, sk_sp<SkBlender> blender, const Paint& paint) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400134
Mike Reedc2dbc032019-07-25 12:28:29 -0400135 virtual void drawBitmap(Bitmap& bitmap, float left, float top, const Paint* paint) override;
136 virtual void drawBitmap(Bitmap& bitmap, const SkMatrix& matrix, const Paint* paint) override;
John Reck1bcacfd2017-11-03 10:12:19 -0700137 virtual void drawBitmap(Bitmap& bitmap, float srcLeft, float srcTop, float srcRight,
138 float srcBottom, float dstLeft, float dstTop, float dstRight,
Mike Reedc2dbc032019-07-25 12:28:29 -0400139 float dstBottom, const Paint* paint) override;
sergeyv5fd2a1c2016-10-20 15:04:28 -0700140 virtual void drawBitmapMesh(Bitmap& bitmap, int meshWidth, int meshHeight,
John Reck1bcacfd2017-11-03 10:12:19 -0700141 const float* vertices, const int* colors,
Mike Reedc2dbc032019-07-25 12:28:29 -0400142 const Paint* paint) override;
John Reck1bcacfd2017-11-03 10:12:19 -0700143 virtual void drawNinePatch(Bitmap& bitmap, const android::Res_png_9patch& chunk, float dstLeft,
144 float dstTop, float dstRight, float dstBottom,
Mike Reedc2dbc032019-07-25 12:28:29 -0400145 const Paint* paint) override;
Derek Sollenberger2d142132018-01-22 10:25:26 -0500146 virtual double drawAnimatedImage(AnimatedImageDrawable* imgDrawable) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400147
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400148 virtual void drawVectorDrawable(VectorDrawableRoot* vectorDrawable) override;
149
150 virtual void drawRoundRect(uirenderer::CanvasPropertyPrimitive* left,
John Reck1bcacfd2017-11-03 10:12:19 -0700151 uirenderer::CanvasPropertyPrimitive* top,
152 uirenderer::CanvasPropertyPrimitive* right,
153 uirenderer::CanvasPropertyPrimitive* bottom,
154 uirenderer::CanvasPropertyPrimitive* rx,
155 uirenderer::CanvasPropertyPrimitive* ry,
156 uirenderer::CanvasPropertyPaint* paint) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400157 virtual void drawCircle(uirenderer::CanvasPropertyPrimitive* x,
John Reck1bcacfd2017-11-03 10:12:19 -0700158 uirenderer::CanvasPropertyPrimitive* y,
159 uirenderer::CanvasPropertyPrimitive* radius,
160 uirenderer::CanvasPropertyPaint* paint) override;
Lucas Dupin00af5272021-04-29 20:30:01 -0700161 virtual void drawRipple(const uirenderer::skiapipeline::RippleDrawableParams& params) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400162
163 virtual void drawLayer(uirenderer::DeferredLayerUpdater* layerHandle) override;
164 virtual void drawRenderNode(uirenderer::RenderNode* renderNode) override;
John Reck894e85a2019-07-15 16:16:44 -0700165 virtual void drawPicture(const SkPicture& picture) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400166
167protected:
Stan Ilievf50806a2016-10-24 10:40:39 -0400168 SkiaCanvas();
John Reck894e85a2019-07-15 16:16:44 -0700169 SkCanvas* asSkCanvas() { return mCanvas; }
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400170 void reset(SkCanvas* skiaCanvas);
171 void drawDrawable(SkDrawable* drawable) { mCanvas->drawDrawable(drawable); }
172
Mike Reed2dfd55d2019-01-08 16:19:03 -0500173 virtual void drawGlyphs(ReadGlyphFunc glyphFunc, int count, const Paint& paint, float x,
Seigo Nonaka63af7ba2020-09-21 23:07:43 -0700174 float y, float totalAdvance) override;
Yuqian Liafc221492016-07-18 13:07:42 -0400175 virtual void drawLayoutOnPath(const minikin::Layout& layout, float hOffset, float vOffset,
Mike Reed2dfd55d2019-01-08 16:19:03 -0500176 const Paint& paint, const SkPath& path, size_t start,
John Reck1bcacfd2017-11-03 10:12:19 -0700177 size_t end) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400178
John Reck11fa2062023-07-21 17:10:51 -0400179 virtual void onFilterPaint(Paint& paint);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400180
Mike Reeda6cb58a2021-07-10 13:31:34 -0400181 Paint filterPaint(const Paint& src) {
182 Paint dst(src);
Mike Reedbb4cb482021-02-22 14:21:20 -0500183 this->onFilterPaint(dst);
184 return dst;
185 }
Ben Wagner0ed10be2018-06-28 17:08:16 -0400186
Mike Reed0f9dce72021-02-12 21:20:33 -0500187 // proc(const SkPaint& modifiedPaint)
Mike Reedbb4cb482021-02-22 14:21:20 -0500188 template <typename Proc>
189 void applyLooper(const Paint* paint, Proc proc, void (*preFilter)(SkPaint&) = nullptr) {
190 BlurDrawLooper* looper = paint ? paint->getLooper() : nullptr;
Mike Reeda6cb58a2021-07-10 13:31:34 -0400191 Paint pnt = paint ? *paint : Paint();
Mike Reedbb4cb482021-02-22 14:21:20 -0500192 if (preFilter) {
Mike Reeda6cb58a2021-07-10 13:31:34 -0400193 preFilter(pnt);
Mike Reedc2dbc032019-07-25 12:28:29 -0400194 }
Mike Reeda6cb58a2021-07-10 13:31:34 -0400195 this->onFilterPaint(pnt);
Mike Reedc2dbc032019-07-25 12:28:29 -0400196 if (looper) {
Mike Reeda6cb58a2021-07-10 13:31:34 -0400197 looper->apply(pnt, [&](SkPoint offset, const Paint& modifiedPaint) {
Mike Reed0f9dce72021-02-12 21:20:33 -0500198 mCanvas->save();
199 mCanvas->translate(offset.fX, offset.fY);
200 proc(modifiedPaint);
201 mCanvas->restore();
202 });
Mike Reedc2dbc032019-07-25 12:28:29 -0400203 } else {
Mike Reeda6cb58a2021-07-10 13:31:34 -0400204 proc(pnt);
Mike Reedc2dbc032019-07-25 12:28:29 -0400205 }
206 }
207
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400208private:
209 struct SaveRec {
John Reck1bcacfd2017-11-03 10:12:19 -0700210 int saveCount;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400211 SaveFlags::Flags saveFlags;
John Reck1bcacfd2017-11-03 10:12:19 -0700212 size_t clipIndex;
Kevin Lubickb3731212023-01-05 19:52:09 +0000213
214 SaveRec(int saveCount, SaveFlags::Flags saveFlags, size_t clipIndex)
215 : saveCount(saveCount), saveFlags(saveFlags), clipIndex(clipIndex) {}
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400216 };
217
Stan Ilievf50806a2016-10-24 10:40:39 -0400218 const SaveRec* currentSaveRec() const;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400219 void recordPartialSave(SaveFlags::Flags flags);
Stan Ilievf50806a2016-10-24 10:40:39 -0400220
John Reck1bcacfd2017-11-03 10:12:19 -0700221 template <typename T>
Mike Reed6e49c9f2016-12-02 15:36:59 -0500222 void recordClip(const T&, SkClipOp);
Stan Ilievf50806a2016-10-24 10:40:39 -0400223 void applyPersistentClips(size_t clipStartIndex);
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400224
Mike Reedc2dbc032019-07-25 12:28:29 -0400225 void drawPoints(const float* points, int count, const Paint& paint, SkCanvas::PointMode mode);
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400226
John Reck859af0d2023-11-15 12:40:39 -0500227 bool useGainmapShader(Bitmap& bitmap);
228
Stan Ilievf50806a2016-10-24 10:40:39 -0400229 class Clip;
230
Kevin Lubickb3731212023-01-05 19:52:09 +0000231 std::unique_ptr<SkCanvas> mCanvasOwned; // Might own a canvas we allocated.
232 SkCanvas* mCanvas; // We do NOT own this canvas, it must survive us
233 // unless it is the same as mCanvasOwned.get().
234 std::unique_ptr<std::deque<SaveRec>> mSaveStack; // Lazily allocated, tracks partial saves.
235 std::vector<Clip> mClipStack; // Tracks persistent clips.
Ben Wagner0ed10be2018-06-28 17:08:16 -0400236 sk_sp<PaintFilter> mPaintFilter;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400237};
238
John Reck1bcacfd2017-11-03 10:12:19 -0700239} // namespace android