1 /*
2  * Copyright (C) 2018 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 
17 #pragma once
18 
19 #include <SkCanvas.h>
20 #include <SkCanvasVirtualEnforcer.h>
21 #include <SkDrawable.h>
22 #include <SkGainmapInfo.h>
23 #include <SkNoDrawCanvas.h>
24 #include <SkPaint.h>
25 #include <SkPath.h>
26 #include <SkRect.h>
27 #include <SkRuntimeEffect.h>
28 #include <log/log.h>
29 
30 #include <cstdlib>
31 #include <utility>
32 #include <vector>
33 
34 #include "CanvasTransform.h"
35 #include "Gainmap.h"
36 #include "hwui/Bitmap.h"
37 #include "pipeline/skia/AnimatedDrawables.h"
38 #include "utils/AutoMalloc.h"
39 #include "utils/Macros.h"
40 #include "utils/TypeLogic.h"
41 
42 enum class SkBlendMode;
43 class SkRRect;
44 class Mesh;
45 
46 namespace android {
47 namespace uirenderer {
48 
49 namespace skiapipeline {
50 class FunctorDrawable;
51 }
52 
53 namespace VectorDrawable {
54 class Tree;
55 }
56 typedef uirenderer::VectorDrawable::Tree VectorDrawableRoot;
57 
58 enum class DisplayListOpType : uint8_t {
59 #define X(T) T,
60 #include "DisplayListOps.in"
61 #undef X
62 };
63 
64 struct DisplayListOp {
65     const uint8_t type : 8;
66     const uint32_t skip : 24;
67 };
68 
69 static_assert(sizeof(DisplayListOp) == 4);
70 
71 class DrawMeshPayload {
72 public:
DrawMeshPayload(const SkMesh * mesh)73     explicit DrawMeshPayload(const SkMesh* mesh) : mesh(mesh) {}
DrawMeshPayload(const Mesh * meshWrapper)74     explicit DrawMeshPayload(const Mesh* meshWrapper) : meshWrapper(meshWrapper) {}
75 
76     [[nodiscard]] const SkMesh& getSkMesh() const;
77 
78 private:
79     const SkMesh* mesh = nullptr;
80     const Mesh* meshWrapper = nullptr;
81 };
82 
83 struct DrawImagePayload {
DrawImagePayloadDrawImagePayload84     explicit DrawImagePayload(Bitmap& bitmap)
85             : image(bitmap.makeImage()), palette(bitmap.palette()) {
86         if (bitmap.hasGainmap()) {
87             auto gainmap = bitmap.gainmap();
88             gainmapInfo = gainmap->info;
89             gainmapImage = gainmap->bitmap->makeImage();
90         }
91     }
92 
DrawImagePayloadDrawImagePayload93     explicit DrawImagePayload(const SkImage* image)
94             : image(sk_ref_sp(image)), palette(BitmapPalette::Unknown) {}
95 
96     DrawImagePayload(const DrawImagePayload&) = default;
97     DrawImagePayload(DrawImagePayload&&) = default;
98     DrawImagePayload& operator=(const DrawImagePayload&) = default;
99     DrawImagePayload& operator=(DrawImagePayload&&) = default;
100     ~DrawImagePayload() = default;
101 
102     sk_sp<SkImage> image;
103     BitmapPalette palette;
104 
105     sk_sp<SkImage> gainmapImage;
106     SkGainmapInfo gainmapInfo;
107 };
108 
109 class RecordingCanvas;
110 
111 class DisplayListData final {
112 public:
DisplayListData()113     DisplayListData() : mHasText(false) {}
114     ~DisplayListData();
115 
116     void draw(SkCanvas* canvas) const;
117 
118     void reset();
empty()119     bool empty() const { return fUsed == 0; }
120 
121     void applyColorTransform(ColorTransform transform);
122 
hasText()123     bool hasText() const { return mHasText; }
usedSize()124     size_t usedSize() const { return fUsed; }
allocatedSize()125     size_t allocatedSize() const { return fReserved; }
126 
127 private:
128     friend class RecordingCanvas;
129 
130     void flush();
131 
132     void save();
133     void saveLayer(const SkRect*, const SkPaint*, const SkImageFilter*, SkCanvas::SaveLayerFlags);
134     void saveBehind(const SkRect*);
135     void restore();
136 
137     void concat(const SkM44&);
138     void setMatrix(const SkM44&);
139     void scale(SkScalar, SkScalar);
140     void translate(SkScalar, SkScalar);
141     void translateZ(SkScalar);
142 
143     void clipPath(const SkPath&, SkClipOp, bool aa);
144     void clipRect(const SkRect&, SkClipOp, bool aa);
145     void clipRRect(const SkRRect&, SkClipOp, bool aa);
146     void clipRegion(const SkRegion&, SkClipOp);
147     void resetClip();
148 
149     void drawPaint(const SkPaint&);
150     void drawBehind(const SkPaint&);
151     void drawPath(const SkPath&, const SkPaint&);
152     void drawRect(const SkRect&, const SkPaint&);
153     void drawRegion(const SkRegion&, const SkPaint&);
154     void drawOval(const SkRect&, const SkPaint&);
155     void drawArc(const SkRect&, SkScalar, SkScalar, bool, const SkPaint&);
156     void drawRRect(const SkRRect&, const SkPaint&);
157     void drawDRRect(const SkRRect&, const SkRRect&, const SkPaint&);
158 
159     void drawMesh(const SkMesh&, const sk_sp<SkBlender>&, const SkPaint&);
160     void drawMesh(const Mesh&, const sk_sp<SkBlender>&, const SkPaint&);
161 
162     void drawAnnotation(const SkRect&, const char*, SkData*);
163     void drawDrawable(SkDrawable*, const SkMatrix*);
164     void drawPicture(const SkPicture*, const SkMatrix*, const SkPaint*);
165 
166     void drawTextBlob(const SkTextBlob*, SkScalar, SkScalar, const SkPaint&);
167 
168     void drawImage(DrawImagePayload&&, SkScalar, SkScalar, const SkSamplingOptions&,
169                    const SkPaint*);
170     void drawImageRect(DrawImagePayload&&, const SkRect*, const SkRect&, const SkSamplingOptions&,
171                        const SkPaint*, SkCanvas::SrcRectConstraint);
172     void drawImageLattice(DrawImagePayload&&, const SkCanvas::Lattice&, const SkRect&, SkFilterMode,
173                           const SkPaint*);
174 
175     void drawPatch(const SkPoint[12], const SkColor[4], const SkPoint[4], SkBlendMode,
176                    const SkPaint&);
177     void drawPoints(SkCanvas::PointMode, size_t, const SkPoint[], const SkPaint&);
178     void drawVertices(const SkVertices*, SkBlendMode, const SkPaint&);
179     void drawAtlas(const SkImage*, const SkRSXform[], const SkRect[], const SkColor[], int,
180                    SkBlendMode, const SkSamplingOptions&, const SkRect*, const SkPaint*);
181     void drawRippleDrawable(const skiapipeline::RippleDrawableParams& params);
182     void drawShadowRec(const SkPath&, const SkDrawShadowRec&);
183     void drawVectorDrawable(VectorDrawableRoot* tree);
184     void drawWebView(skiapipeline::FunctorDrawable*);
185 
186     template <typename T, typename... Args>
187     void* push(size_t, Args&&...);
188 
189     template <typename Fn, typename... Args>
190     void map(const Fn[], Args...) const;
191 
192     AutoTMalloc<uint8_t> fBytes;
193     size_t fUsed = 0;
194     size_t fReserved = 0;
195 
196     bool mHasText : 1;
197 };
198 
199 class RecordingCanvas final : public SkCanvasVirtualEnforcer<SkNoDrawCanvas> {
200 public:
201     RecordingCanvas();
202     void reset(DisplayListData*, const SkIRect& bounds);
203 
204     sk_sp<SkSurface> onNewSurface(const SkImageInfo&, const SkSurfaceProps&) override;
205 
206     void willSave() override;
207     SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec&) override;
208     void willRestore() override;
209     bool onDoSaveBehind(const SkRect*) override;
210 
211     void onFlush() override;
212 
213     void didConcat44(const SkM44&) override;
214     void didSetM44(const SkM44&) override;
215     void didScale(SkScalar, SkScalar) override;
216     void didTranslate(SkScalar, SkScalar) override;
217 
218     void onClipRect(const SkRect&, SkClipOp, ClipEdgeStyle) override;
219     void onClipRRect(const SkRRect&, SkClipOp, ClipEdgeStyle) override;
220     void onClipPath(const SkPath&, SkClipOp, ClipEdgeStyle) override;
221     void onClipRegion(const SkRegion&, SkClipOp) override;
222     void onResetClip() override;
223 
224     void onDrawPaint(const SkPaint&) override;
225     void onDrawBehind(const SkPaint&) override;
226     void onDrawPath(const SkPath&, const SkPaint&) override;
227     void onDrawRect(const SkRect&, const SkPaint&) override;
228     void onDrawRegion(const SkRegion&, const SkPaint&) override;
229     void onDrawOval(const SkRect&, const SkPaint&) override;
230     void onDrawArc(const SkRect&, SkScalar, SkScalar, bool, const SkPaint&) override;
231     void onDrawRRect(const SkRRect&, const SkPaint&) override;
232     void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override;
233 
234     void onDrawDrawable(SkDrawable*, const SkMatrix*) override;
235     void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override;
236     void onDrawAnnotation(const SkRect&, const char[], SkData*) override;
237 
238     void onDrawTextBlob(const SkTextBlob*, SkScalar, SkScalar, const SkPaint&) override;
239 
240     void drawRippleDrawable(const skiapipeline::RippleDrawableParams& params);
241 
242     void drawImage(DrawImagePayload&&, SkScalar, SkScalar, const SkSamplingOptions&,
243                    const SkPaint*);
244     void drawImageRect(DrawImagePayload&&, const SkRect&, const SkRect&, const SkSamplingOptions&,
245                        const SkPaint*, SrcRectConstraint);
246     void drawImageLattice(DrawImagePayload&&, const Lattice& lattice, const SkRect&, SkFilterMode,
247                           const SkPaint*);
248 
249     void onDrawImage2(const SkImage*, SkScalar, SkScalar, const SkSamplingOptions&,
250                       const SkPaint*) override;
251     void onDrawImageLattice2(const SkImage*, const Lattice&, const SkRect&, SkFilterMode,
252                              const SkPaint*) override;
253     void onDrawImageRect2(const SkImage*, const SkRect&, const SkRect&, const SkSamplingOptions&,
254                           const SkPaint*, SrcRectConstraint) override;
255 
256     void onDrawPatch(const SkPoint[12], const SkColor[4], const SkPoint[4], SkBlendMode,
257                      const SkPaint&) override;
258     void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override;
259     void onDrawVerticesObject(const SkVertices*, SkBlendMode, const SkPaint&) override;
260     void onDrawMesh(const SkMesh&, sk_sp<SkBlender>, const SkPaint&) override;
261     void onDrawAtlas2(const SkImage*, const SkRSXform[], const SkRect[], const SkColor[], int,
262                      SkBlendMode, const SkSamplingOptions&, const SkRect*, const SkPaint*) override;
263     void onDrawShadowRec(const SkPath&, const SkDrawShadowRec&) override;
264 
265     void drawMesh(const Mesh& mesh, sk_sp<SkBlender> blender, const SkPaint& paint);
266     void drawVectorDrawable(VectorDrawableRoot* tree);
267     void drawWebView(skiapipeline::FunctorDrawable*);
268 
269     /**
270      * If "isClipMayBeComplex" returns false, it is guaranteed the current clip is a rectangle.
271      * If the return value is true, then clip may or may not be complex (there is no guarantee).
272      */
isClipMayBeComplex()273     inline bool isClipMayBeComplex() { return mClipMayBeComplex; }
274 
275 private:
276     typedef SkCanvasVirtualEnforcer<SkNoDrawCanvas> INHERITED;
277 
setClipMayBeComplex()278     inline void setClipMayBeComplex() {
279         if (!mClipMayBeComplex) {
280             mComplexSaveCount = mSaveCount;
281             mClipMayBeComplex = true;
282         }
283     }
284 
285     DisplayListData* fDL;
286 
287     /**
288      * mClipMayBeComplex tracks if the current clip is a rectangle. This flag is used to promote
289      * FunctorDrawable to a layer, if it is clipped by a non-rect.
290      */
291     bool mClipMayBeComplex = false;
292 
293     /**
294      * mSaveCount is the current level of our save tree.
295      */
296     int mSaveCount = 0;
297 
298     /**
299      * mComplexSaveCount is the first save level, which has a complex clip. Every level below
300      * mComplexSaveCount is assumed to have a complex clip and every level above mComplexSaveCount
301      * is guaranteed to not be complex.
302      */
303     int mComplexSaveCount = 0;
304 };
305 
306 }  // namespace uirenderer
307 }  // namespace android
308