Debian Codegraph @

libs/hwui/SkiaCanvas.h

raw symbols SCIP
1 /*
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
18 #include "CanvasProperty.h"
19 #ifdef __ANDROID__ // Layoutlib does not support hardware acceleration
20 #include "DeferredLayerUpdater.h"
21 #endif
22 #include "RenderNode.h"
23 #include "VectorDrawable.h"
24 #include "hwui/Canvas.h"
25 #include "hwui/Paint.h"
26 #include "hwui/BlurDrawLooper.h"
27
28 #include <SkCanvas.h>
29 #include <SkDeque.h>
30 #include "pipeline/skia/AnimatedDrawables.h"
31 #include "src/core/SkArenaAlloc.h"
32
33 #include <cassert>
34 #include <optional>
35
36 namespace android {
37
38 // Holds an SkCanvas reference plus additional native data.
39 class SkiaCanvas : public Canvas {
40 public:
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
47 * not be NULL. This constructor does not take ownership, so the caller
48 * must guarantee that it remains valid while the SkiaCanvas is valid.
49 */
50 explicit SkiaCanvas(SkCanvas* canvas);
51
52 virtual ~SkiaCanvas();
53
54 virtual void resetRecording(int width, int height,
55 uirenderer::RenderNode* renderNode) override {
56 LOG_ALWAYS_FATAL("SkiaCanvas cannot be reset as a recording canvas");
57 }
58
59 virtual void finishRecording(uirenderer::RenderNode*) override {
60 LOG_ALWAYS_FATAL("SkiaCanvas does not produce a DisplayList");
61 }
62 virtual void enableZ(bool enableZ) override {
63 LOG_ALWAYS_FATAL("SkiaCanvas does not support enableZ");
64 }
65
66 virtual void punchHole(const SkRRect& rect) override;
67
68 virtual void setBitmap(const SkBitmap& bitmap) override;
69
70 virtual bool isOpaque() override;
71 virtual int width() override;
72 virtual int height() override;
73
74 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;
78 virtual void restoreUnclippedLayer(int saveCount, const Paint& paint) override;
79
80 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;
82 virtual int saveUnclippedLayer(int left, int top, int right, int bottom) override;
83
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;
95 virtual bool clipRect(float left, float top, float right, float bottom, SkClipOp op) override;
96 virtual bool clipPath(const SkPath* path, SkClipOp op) override;
97 virtual bool replaceClipRect_deprecated(float left, float top, float right,
98 float bottom) override;
99 virtual bool replaceClipPath_deprecated(const SkPath* path) override;
100
101 virtual PaintFilter* getPaintFilter() override;
102 virtual void setPaintFilter(sk_sp<PaintFilter> paintFilter) override;
103
104 virtual SkCanvasState* captureCanvasState() const override;
105
106 virtual void drawColor(int color, SkBlendMode mode) override;
107 virtual void drawPaint(const Paint& paint) override;
108
109 virtual void drawPoint(float x, float y, const Paint& paint) override;
110 virtual void drawPoints(const float* points, int count, const Paint& paint) override;
111 virtual void drawLine(float startX, float startY, float stopX, float stopY,
112 const Paint& paint) override;
113 virtual void drawLines(const float* points, int count, const Paint& paint) override;
114 virtual void drawRect(float left, float top, float right, float bottom,
115 const Paint& paint) override;
116 virtual void drawRegion(const SkRegion& region, const Paint& paint) override;
117 virtual void drawRoundRect(float left, float top, float right, float bottom, float rx, float ry,
118 const Paint& paint) override;
119
120 virtual void drawDoubleRoundRect(const SkRRect& outer, const SkRRect& inner,
121 const Paint& paint) override;
122
123 virtual void drawCircle(float x, float y, float radius, const Paint& paint) override;
124 virtual void drawOval(float left, float top, float right, float bottom,
125 const Paint& paint) override;
126 virtual void drawArc(float left, float top, float right, float bottom, float startAngle,
127 float sweepAngle, bool useCenter, const Paint& paint) override;
128 virtual void drawPath(const SkPath& path, const Paint& paint) override;
129 virtual void drawVertices(const SkVertices*, SkBlendMode, const Paint& paint) override;
130
131 virtual void drawBitmap(Bitmap& bitmap, float left, float top, const Paint* paint) override;
132 virtual void drawBitmap(Bitmap& bitmap, const SkMatrix& matrix, const Paint* paint) override;
133 virtual void drawBitmap(Bitmap& bitmap, float srcLeft, float srcTop, float srcRight,
134 float srcBottom, float dstLeft, float dstTop, float dstRight,
135 float dstBottom, const Paint* paint) override;
136 virtual void drawBitmapMesh(Bitmap& bitmap, int meshWidth, int meshHeight,
137 const float* vertices, const int* colors,
138 const Paint* paint) override;
139 virtual void drawNinePatch(Bitmap& bitmap, const android::Res_png_9patch& chunk, float dstLeft,
140 float dstTop, float dstRight, float dstBottom,
141 const Paint* paint) override;
142 virtual double drawAnimatedImage(AnimatedImageDrawable* imgDrawable) override;
143
144 virtual void drawVectorDrawable(VectorDrawableRoot* vectorDrawable) override;
145
146 virtual void drawRoundRect(uirenderer::CanvasPropertyPrimitive* left,
147 uirenderer::CanvasPropertyPrimitive* top,
148 uirenderer::CanvasPropertyPrimitive* right,
149 uirenderer::CanvasPropertyPrimitive* bottom,
150 uirenderer::CanvasPropertyPrimitive* rx,
151 uirenderer::CanvasPropertyPrimitive* ry,
152 uirenderer::CanvasPropertyPaint* paint) override;
153 virtual void drawCircle(uirenderer::CanvasPropertyPrimitive* x,
154 uirenderer::CanvasPropertyPrimitive* y,
155 uirenderer::CanvasPropertyPrimitive* radius,
156 uirenderer::CanvasPropertyPaint* paint) override;
157 virtual void drawRipple(const uirenderer::skiapipeline::RippleDrawableParams& params) override;
158
159 virtual void drawLayer(uirenderer::DeferredLayerUpdater* layerHandle) override;
160 virtual void drawRenderNode(uirenderer::RenderNode* renderNode) override;
161 virtual void drawPicture(const SkPicture& picture) override;
162
163 protected:
164 SkiaCanvas();
165 SkCanvas* asSkCanvas() { return mCanvas; }
166 void reset(SkCanvas* skiaCanvas);
167 void drawDrawable(SkDrawable* drawable) { mCanvas->drawDrawable(drawable); }
168
169 virtual void drawGlyphs(ReadGlyphFunc glyphFunc, int count, const Paint& paint, float x,
170 float y, float totalAdvance) override;
171 virtual void drawLayoutOnPath(const minikin::Layout& layout, float hOffset, float vOffset,
172 const Paint& paint, const SkPath& path, size_t start,
173 size_t end) override;
174
175 void onFilterPaint(Paint& paint);
176
177 Paint filterPaint(const Paint& src) {
178 Paint dst(src);
179 this->onFilterPaint(dst);
180 return dst;
181 }
182
183 // proc(const SkPaint& modifiedPaint)
184 template <typename Proc>
185 void applyLooper(const Paint* paint, Proc proc, void (*preFilter)(SkPaint&) = nullptr) {
186 BlurDrawLooper* looper = paint ? paint->getLooper() : nullptr;
187 Paint pnt = paint ? *paint : Paint();
188 if (preFilter) {
189 preFilter(pnt);
190 }
191 this->onFilterPaint(pnt);
192 if (looper) {
193 looper->apply(pnt, [&](SkPoint offset, const Paint& modifiedPaint) {
194 mCanvas->save();
195 mCanvas->translate(offset.fX, offset.fY);
196 proc(modifiedPaint);
197 mCanvas->restore();
198 });
199 } else {
200 proc(pnt);
201 }
202 }
203
204 private:
205 struct SaveRec {
206 int saveCount;
207 SaveFlags::Flags saveFlags;
208 size_t clipIndex;
209 };
210
211 const SaveRec* currentSaveRec() const;
212 void recordPartialSave(SaveFlags::Flags flags);
213
214 template <typename T>
215 void recordClip(const T&, SkClipOp);
216 void applyPersistentClips(size_t clipStartIndex);
217
218 void drawPoints(const float* points, int count, const Paint& paint, SkCanvas::PointMode mode);
219
220 class Clip;
221
222 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.
227 sk_sp<PaintFilter> mPaintFilter;
228 };
229
230 } // namespace android