blob: f2d7bda36499373ba2a7ec0163245bf7fcfe77e1 [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"
Mike Reeda6cb58a2021-07-10 13:31:34 -040026#include "hwui/BlurDrawLooper.h"
Derek Sollenbergerc1908132016-07-15 10:28:16 -040027
28#include <SkCanvas.h>
Brian Osmana9ded202021-07-28 15:17:56 +000029#include <SkDeque.h>
Lucas Dupin00af5272021-04-29 20:30:01 -070030#include "pipeline/skia/AnimatedDrawables.h"
Mike Reedc2dbc032019-07-25 12:28:29 -040031#include "src/core/SkArenaAlloc.h"
Ben Wagner0ed10be2018-06-28 17:08:16 -040032
33#include <cassert>
34#include <optional>
Derek Sollenbergerc1908132016-07-15 10:28:16 -040035
36namespace android {
37
38// Holds an SkCanvas reference plus additional native data.
39class SkiaCanvas : public Canvas {
40public:
41 explicit SkiaCanvas(const SkBitmap& bitmap);
42
43 /**
44 * Create a new SkiaCanvas.
45 *
46 * @param canvas SkCanvas to handle calls made to this SkiaCanvas. Must
Mike Reed6acfe162016-11-18 17:21:09 -050047 * not be NULL. This constructor does not take ownership, so the caller
48 * must guarantee that it remains valid while the SkiaCanvas is valid.
Derek Sollenbergerc1908132016-07-15 10:28:16 -040049 */
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -040050 explicit SkiaCanvas(SkCanvas* canvas);
Derek Sollenbergerc1908132016-07-15 10:28:16 -040051
Stan Iliev021693b2016-10-17 16:26:15 -040052 virtual ~SkiaCanvas();
53
Stan Ilievc0e7a902016-10-13 17:07:09 -040054 virtual void resetRecording(int width, int height,
John Reck1bcacfd2017-11-03 10:12:19 -070055 uirenderer::RenderNode* renderNode) override {
Derek Sollenbergerc1908132016-07-15 10:28:16 -040056 LOG_ALWAYS_FATAL("SkiaCanvas cannot be reset as a recording canvas");
57 }
58
John Reck2e48df32021-01-19 18:06:05 -050059 virtual void finishRecording(uirenderer::RenderNode*) override {
Derek Sollenbergerc1908132016-07-15 10:28:16 -040060 LOG_ALWAYS_FATAL("SkiaCanvas does not produce a DisplayList");
Derek Sollenbergerc1908132016-07-15 10:28:16 -040061 }
Leon Scroggins III0f53e102020-05-05 15:53:29 -040062 virtual void enableZ(bool enableZ) override {
63 LOG_ALWAYS_FATAL("SkiaCanvas does not support enableZ");
Derek Sollenbergerc1908132016-07-15 10:28:16 -040064 }
65
Nader Jawad2dc632a2021-03-29 18:51:29 -070066 virtual void punchHole(const SkRRect& rect) override;
67
Derek Sollenbergerc1908132016-07-15 10:28:16 -040068 virtual void setBitmap(const SkBitmap& bitmap) override;
69
70 virtual bool isOpaque() override;
71 virtual int width() override;
72 virtual int height() override;
73
Derek Sollenbergerc1908132016-07-15 10:28:16 -040074 virtual int getSaveCount() const override;
75 virtual int save(SaveFlags::Flags flags) override;
76 virtual void restore() override;
77 virtual void restoreToCount(int saveCount) override;
Mike Reeda6cb58a2021-07-10 13:31:34 -040078 virtual void restoreUnclippedLayer(int saveCount, const Paint& paint) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -040079
John Recka00eef212020-11-16 12:45:55 -050080 virtual int saveLayer(float left, float top, float right, float bottom, const SkPaint* paint) override;
81 virtual int saveLayerAlpha(float left, float top, float right, float bottom, int alpha) override;
Derek Sollenberger24fc9012018-12-07 14:12:12 -050082 virtual int saveUnclippedLayer(int left, int top, int right, int bottom) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -040083
84 virtual void getMatrix(SkMatrix* outMatrix) const override;
85 virtual void setMatrix(const SkMatrix& matrix) override;
86 virtual void concat(const SkMatrix& matrix) override;
87 virtual void rotate(float degrees) override;
88 virtual void scale(float sx, float sy) override;
89 virtual void skew(float sx, float sy) override;
90 virtual void translate(float dx, float dy) override;
91
92 virtual bool getClipBounds(SkRect* outRect) const override;
93 virtual bool quickRejectRect(float left, float top, float right, float bottom) const override;
94 virtual bool quickRejectPath(const SkPath& path) const override;
John Reck1bcacfd2017-11-03 10:12:19 -070095 virtual bool clipRect(float left, float top, float right, float bottom, SkClipOp op) override;
Mike Reed6e49c9f2016-12-02 15:36:59 -050096 virtual bool clipPath(const SkPath* path, SkClipOp op) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -040097
Ben Wagner0ed10be2018-06-28 17:08:16 -040098 virtual PaintFilter* getPaintFilter() override;
99 virtual void setPaintFilter(sk_sp<PaintFilter> paintFilter) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400100
Matt Sarettd0814db2017-04-13 09:33:18 -0400101 virtual SkCanvasState* captureCanvasState() const override;
102
Mike Reed260ab722016-10-07 15:59:20 -0400103 virtual void drawColor(int color, SkBlendMode mode) override;
Mike Reeda6cb58a2021-07-10 13:31:34 -0400104 virtual void drawPaint(const Paint& paint) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400105
Mike Reedc2dbc032019-07-25 12:28:29 -0400106 virtual void drawPoint(float x, float y, const Paint& paint) override;
107 virtual void drawPoints(const float* points, int count, const Paint& paint) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400108 virtual void drawLine(float startX, float startY, float stopX, float stopY,
Mike Reedc2dbc032019-07-25 12:28:29 -0400109 const Paint& paint) override;
110 virtual void drawLines(const float* points, int count, const Paint& paint) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400111 virtual void drawRect(float left, float top, float right, float bottom,
Mike Reedc2dbc032019-07-25 12:28:29 -0400112 const Paint& paint) override;
113 virtual void drawRegion(const SkRegion& region, const Paint& paint) override;
John Reck1bcacfd2017-11-03 10:12:19 -0700114 virtual void drawRoundRect(float left, float top, float right, float bottom, float rx, float ry,
Mike Reedc2dbc032019-07-25 12:28:29 -0400115 const Paint& paint) override;
Nader Jawadadfe1d92018-09-27 12:27:36 -0700116
117 virtual void drawDoubleRoundRect(const SkRRect& outer, const SkRRect& inner,
Mike Reedc2dbc032019-07-25 12:28:29 -0400118 const Paint& paint) override;
Nader Jawadadfe1d92018-09-27 12:27:36 -0700119
Mike Reedc2dbc032019-07-25 12:28:29 -0400120 virtual void drawCircle(float x, float y, float radius, const Paint& paint) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400121 virtual void drawOval(float left, float top, float right, float bottom,
Mike Reedc2dbc032019-07-25 12:28:29 -0400122 const Paint& paint) override;
John Reck1bcacfd2017-11-03 10:12:19 -0700123 virtual void drawArc(float left, float top, float right, float bottom, float startAngle,
Mike Reedc2dbc032019-07-25 12:28:29 -0400124 float sweepAngle, bool useCenter, const Paint& paint) override;
125 virtual void drawPath(const SkPath& path, const Paint& paint) override;
126 virtual void drawVertices(const SkVertices*, SkBlendMode, const Paint& paint) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400127
Mike Reedc2dbc032019-07-25 12:28:29 -0400128 virtual void drawBitmap(Bitmap& bitmap, float left, float top, const Paint* paint) override;
129 virtual void drawBitmap(Bitmap& bitmap, const SkMatrix& matrix, const Paint* paint) override;
John Reck1bcacfd2017-11-03 10:12:19 -0700130 virtual void drawBitmap(Bitmap& bitmap, float srcLeft, float srcTop, float srcRight,
131 float srcBottom, float dstLeft, float dstTop, float dstRight,
Mike Reedc2dbc032019-07-25 12:28:29 -0400132 float dstBottom, const Paint* paint) override;
sergeyv5fd2a1c2016-10-20 15:04:28 -0700133 virtual void drawBitmapMesh(Bitmap& bitmap, int meshWidth, int meshHeight,
John Reck1bcacfd2017-11-03 10:12:19 -0700134 const float* vertices, const int* colors,
Mike Reedc2dbc032019-07-25 12:28:29 -0400135 const Paint* paint) override;
John Reck1bcacfd2017-11-03 10:12:19 -0700136 virtual void drawNinePatch(Bitmap& bitmap, const android::Res_png_9patch& chunk, float dstLeft,
137 float dstTop, float dstRight, float dstBottom,
Mike Reedc2dbc032019-07-25 12:28:29 -0400138 const Paint* paint) override;
Derek Sollenberger2d142132018-01-22 10:25:26 -0500139 virtual double drawAnimatedImage(AnimatedImageDrawable* imgDrawable) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400140
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400141 virtual void drawVectorDrawable(VectorDrawableRoot* vectorDrawable) override;
142
143 virtual void drawRoundRect(uirenderer::CanvasPropertyPrimitive* left,
John Reck1bcacfd2017-11-03 10:12:19 -0700144 uirenderer::CanvasPropertyPrimitive* top,
145 uirenderer::CanvasPropertyPrimitive* right,
146 uirenderer::CanvasPropertyPrimitive* bottom,
147 uirenderer::CanvasPropertyPrimitive* rx,
148 uirenderer::CanvasPropertyPrimitive* ry,
149 uirenderer::CanvasPropertyPaint* paint) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400150 virtual void drawCircle(uirenderer::CanvasPropertyPrimitive* x,
John Reck1bcacfd2017-11-03 10:12:19 -0700151 uirenderer::CanvasPropertyPrimitive* y,
152 uirenderer::CanvasPropertyPrimitive* radius,
153 uirenderer::CanvasPropertyPaint* paint) override;
Lucas Dupin00af5272021-04-29 20:30:01 -0700154 virtual void drawRipple(const uirenderer::skiapipeline::RippleDrawableParams& params) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400155
156 virtual void drawLayer(uirenderer::DeferredLayerUpdater* layerHandle) override;
157 virtual void drawRenderNode(uirenderer::RenderNode* renderNode) override;
John Reck894e85a2019-07-15 16:16:44 -0700158 virtual void drawPicture(const SkPicture& picture) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400159
160protected:
Stan Ilievf50806a2016-10-24 10:40:39 -0400161 SkiaCanvas();
John Reck894e85a2019-07-15 16:16:44 -0700162 SkCanvas* asSkCanvas() { return mCanvas; }
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400163 void reset(SkCanvas* skiaCanvas);
164 void drawDrawable(SkDrawable* drawable) { mCanvas->drawDrawable(drawable); }
165
Mike Reed2dfd55d2019-01-08 16:19:03 -0500166 virtual void drawGlyphs(ReadGlyphFunc glyphFunc, int count, const Paint& paint, float x,
Seigo Nonaka63af7ba2020-09-21 23:07:43 -0700167 float y, float totalAdvance) override;
Yuqian Liafc221492016-07-18 13:07:42 -0400168 virtual void drawLayoutOnPath(const minikin::Layout& layout, float hOffset, float vOffset,
Mike Reed2dfd55d2019-01-08 16:19:03 -0500169 const Paint& paint, const SkPath& path, size_t start,
John Reck1bcacfd2017-11-03 10:12:19 -0700170 size_t end) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400171
Mike Reeda6cb58a2021-07-10 13:31:34 -0400172 void onFilterPaint(Paint& paint);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400173
Mike Reeda6cb58a2021-07-10 13:31:34 -0400174 Paint filterPaint(const Paint& src) {
175 Paint dst(src);
Mike Reedbb4cb482021-02-22 14:21:20 -0500176 this->onFilterPaint(dst);
177 return dst;
178 }
Ben Wagner0ed10be2018-06-28 17:08:16 -0400179
Mike Reed0f9dce72021-02-12 21:20:33 -0500180 // proc(const SkPaint& modifiedPaint)
Mike Reedbb4cb482021-02-22 14:21:20 -0500181 template <typename Proc>
182 void applyLooper(const Paint* paint, Proc proc, void (*preFilter)(SkPaint&) = nullptr) {
183 BlurDrawLooper* looper = paint ? paint->getLooper() : nullptr;
Mike Reeda6cb58a2021-07-10 13:31:34 -0400184 Paint pnt = paint ? *paint : Paint();
Mike Reedbb4cb482021-02-22 14:21:20 -0500185 if (preFilter) {
Mike Reeda6cb58a2021-07-10 13:31:34 -0400186 preFilter(pnt);
Mike Reedc2dbc032019-07-25 12:28:29 -0400187 }
Mike Reeda6cb58a2021-07-10 13:31:34 -0400188 this->onFilterPaint(pnt);
Mike Reedc2dbc032019-07-25 12:28:29 -0400189 if (looper) {
Mike Reeda6cb58a2021-07-10 13:31:34 -0400190 looper->apply(pnt, [&](SkPoint offset, const Paint& modifiedPaint) {
Mike Reed0f9dce72021-02-12 21:20:33 -0500191 mCanvas->save();
192 mCanvas->translate(offset.fX, offset.fY);
193 proc(modifiedPaint);
194 mCanvas->restore();
195 });
Mike Reedc2dbc032019-07-25 12:28:29 -0400196 } else {
Mike Reeda6cb58a2021-07-10 13:31:34 -0400197 proc(pnt);
Mike Reedc2dbc032019-07-25 12:28:29 -0400198 }
199 }
200
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400201private:
202 struct SaveRec {
John Reck1bcacfd2017-11-03 10:12:19 -0700203 int saveCount;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400204 SaveFlags::Flags saveFlags;
John Reck1bcacfd2017-11-03 10:12:19 -0700205 size_t clipIndex;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400206 };
207
Stan Ilievf50806a2016-10-24 10:40:39 -0400208 const SaveRec* currentSaveRec() const;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400209 void recordPartialSave(SaveFlags::Flags flags);
Stan Ilievf50806a2016-10-24 10:40:39 -0400210
John Reck1bcacfd2017-11-03 10:12:19 -0700211 template <typename T>
Mike Reed6e49c9f2016-12-02 15:36:59 -0500212 void recordClip(const T&, SkClipOp);
Stan Ilievf50806a2016-10-24 10:40:39 -0400213 void applyPersistentClips(size_t clipStartIndex);
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400214
Mike Reedc2dbc032019-07-25 12:28:29 -0400215 void drawPoints(const float* points, int count, const Paint& paint, SkCanvas::PointMode mode);
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400216
Stan Ilievf50806a2016-10-24 10:40:39 -0400217 class Clip;
218
John Reck1bcacfd2017-11-03 10:12:19 -0700219 std::unique_ptr<SkCanvas> mCanvasOwned; // might own a canvas we allocated
220 SkCanvas* mCanvas; // we do NOT own this canvas, it must survive us
221 // unless it is the same as mCanvasOwned.get()
222 std::unique_ptr<SkDeque> mSaveStack; // lazily allocated, tracks partial saves.
223 std::vector<Clip> mClipStack; // tracks persistent clips.
Ben Wagner0ed10be2018-06-28 17:08:16 -0400224 sk_sp<PaintFilter> mPaintFilter;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400225};
226
John Reck1bcacfd2017-11-03 10:12:19 -0700227} // namespace android