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