• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
27 #include <SkCanvas.h>
28 #include "pipeline/skia/AnimatedDrawables.h"
29 #include "src/core/SkArenaAlloc.h"
30 
31 #include <cassert>
32 #include <optional>
33 
34 namespace android {
35 
36 // Holds an SkCanvas reference plus additional native data.
37 class SkiaCanvas : public Canvas {
38 public:
39     explicit SkiaCanvas(const SkBitmap& bitmap);
40 
41     /**
42      *  Create a new SkiaCanvas.
43      *
44      *  @param canvas SkCanvas to handle calls made to this SkiaCanvas. Must
45      *      not be NULL. This constructor does not take ownership, so the caller
46      *      must guarantee that it remains valid while the SkiaCanvas is valid.
47      */
48     explicit SkiaCanvas(SkCanvas* canvas);
49 
50     virtual ~SkiaCanvas();
51 
resetRecording(int width,int height,uirenderer::RenderNode * renderNode)52     virtual void resetRecording(int width, int height,
53                                 uirenderer::RenderNode* renderNode) override {
54         LOG_ALWAYS_FATAL("SkiaCanvas cannot be reset as a recording canvas");
55     }
56 
finishRecording(uirenderer::RenderNode *)57     virtual void finishRecording(uirenderer::RenderNode*) override {
58         LOG_ALWAYS_FATAL("SkiaCanvas does not produce a DisplayList");
59     }
enableZ(bool enableZ)60     virtual void enableZ(bool enableZ) override {
61         LOG_ALWAYS_FATAL("SkiaCanvas does not support enableZ");
62     }
63 
64     virtual void punchHole(const SkRRect& rect) override;
65 
66     virtual void setBitmap(const SkBitmap& bitmap) override;
67 
68     virtual bool isOpaque() override;
69     virtual int width() override;
70     virtual int height() override;
71 
72     virtual int getSaveCount() const override;
73     virtual int save(SaveFlags::Flags flags) override;
74     virtual void restore() override;
75     virtual void restoreToCount(int saveCount) override;
76     virtual void restoreUnclippedLayer(int saveCount, const SkPaint& paint) override;
77 
78     virtual int saveLayer(float left, float top, float right, float bottom, const SkPaint* paint) override;
79     virtual int saveLayerAlpha(float left, float top, float right, float bottom, int alpha) override;
80     virtual int saveUnclippedLayer(int left, int top, int right, int bottom) override;
81 
82     virtual void getMatrix(SkMatrix* outMatrix) const override;
83     virtual void setMatrix(const SkMatrix& matrix) override;
84     virtual void concat(const SkMatrix& matrix) override;
85     virtual void rotate(float degrees) override;
86     virtual void scale(float sx, float sy) override;
87     virtual void skew(float sx, float sy) override;
88     virtual void translate(float dx, float dy) override;
89 
90     virtual bool getClipBounds(SkRect* outRect) const override;
91     virtual bool quickRejectRect(float left, float top, float right, float bottom) const override;
92     virtual bool quickRejectPath(const SkPath& path) const override;
93     virtual bool clipRect(float left, float top, float right, float bottom, SkClipOp op) override;
94     virtual bool clipPath(const SkPath* path, SkClipOp op) override;
95 
96     virtual PaintFilter* getPaintFilter() override;
97     virtual void setPaintFilter(sk_sp<PaintFilter> paintFilter) override;
98 
99     virtual SkCanvasState* captureCanvasState() const override;
100 
101     virtual void drawColor(int color, SkBlendMode mode) override;
102     virtual void drawPaint(const SkPaint& paint) override;
103 
104     virtual void drawPoint(float x, float y, const Paint& paint) override;
105     virtual void drawPoints(const float* points, int count, const Paint& paint) override;
106     virtual void drawLine(float startX, float startY, float stopX, float stopY,
107                           const Paint& paint) override;
108     virtual void drawLines(const float* points, int count, const Paint& paint) override;
109     virtual void drawRect(float left, float top, float right, float bottom,
110                           const Paint& paint) override;
111     virtual void drawRegion(const SkRegion& region, const Paint& paint) override;
112     virtual void drawRoundRect(float left, float top, float right, float bottom, float rx, float ry,
113                                const Paint& paint) override;
114 
115    virtual void drawDoubleRoundRect(const SkRRect& outer, const SkRRect& inner,
116                                const Paint& paint) override;
117 
118     virtual void drawCircle(float x, float y, float radius, const Paint& paint) override;
119     virtual void drawOval(float left, float top, float right, float bottom,
120                           const Paint& paint) override;
121     virtual void drawArc(float left, float top, float right, float bottom, float startAngle,
122                          float sweepAngle, bool useCenter, const Paint& paint) override;
123     virtual void drawPath(const SkPath& path, const Paint& paint) override;
124     virtual void drawVertices(const SkVertices*, SkBlendMode, const Paint& paint) override;
125 
126     virtual void drawBitmap(Bitmap& bitmap, float left, float top, const Paint* paint) override;
127     virtual void drawBitmap(Bitmap& bitmap, const SkMatrix& matrix, const Paint* paint) override;
128     virtual void drawBitmap(Bitmap& bitmap, float srcLeft, float srcTop, float srcRight,
129                             float srcBottom, float dstLeft, float dstTop, float dstRight,
130                             float dstBottom, const Paint* paint) override;
131     virtual void drawBitmapMesh(Bitmap& bitmap, int meshWidth, int meshHeight,
132                                 const float* vertices, const int* colors,
133                                 const Paint* paint) override;
134     virtual void drawNinePatch(Bitmap& bitmap, const android::Res_png_9patch& chunk, float dstLeft,
135                                float dstTop, float dstRight, float dstBottom,
136                                const Paint* paint) override;
137     virtual double drawAnimatedImage(AnimatedImageDrawable* imgDrawable) override;
138 
139     virtual void drawVectorDrawable(VectorDrawableRoot* vectorDrawable) override;
140 
141     virtual void drawRoundRect(uirenderer::CanvasPropertyPrimitive* left,
142                                uirenderer::CanvasPropertyPrimitive* top,
143                                uirenderer::CanvasPropertyPrimitive* right,
144                                uirenderer::CanvasPropertyPrimitive* bottom,
145                                uirenderer::CanvasPropertyPrimitive* rx,
146                                uirenderer::CanvasPropertyPrimitive* ry,
147                                uirenderer::CanvasPropertyPaint* paint) override;
148     virtual void drawCircle(uirenderer::CanvasPropertyPrimitive* x,
149                             uirenderer::CanvasPropertyPrimitive* y,
150                             uirenderer::CanvasPropertyPrimitive* radius,
151                             uirenderer::CanvasPropertyPaint* paint) override;
152     virtual void drawRipple(const uirenderer::skiapipeline::RippleDrawableParams& params) override;
153 
154     virtual void drawLayer(uirenderer::DeferredLayerUpdater* layerHandle) override;
155     virtual void drawRenderNode(uirenderer::RenderNode* renderNode) override;
156     virtual void drawPicture(const SkPicture& picture) override;
157 
158 protected:
159     SkiaCanvas();
asSkCanvas()160     SkCanvas* asSkCanvas() { return mCanvas; }
161     void reset(SkCanvas* skiaCanvas);
drawDrawable(SkDrawable * drawable)162     void drawDrawable(SkDrawable* drawable) { mCanvas->drawDrawable(drawable); }
163 
164     virtual void drawGlyphs(ReadGlyphFunc glyphFunc, int count, const Paint& paint, float x,
165                             float y, float totalAdvance) override;
166     virtual void drawLayoutOnPath(const minikin::Layout& layout, float hOffset, float vOffset,
167                                   const Paint& paint, const SkPath& path, size_t start,
168                                   size_t end) override;
169 
170     void onFilterPaint(SkPaint& paint);
171 
filterPaint(const SkPaint & src)172     SkPaint filterPaint(const SkPaint& src) {
173         SkPaint dst(src);
174         this->onFilterPaint(dst);
175         return dst;
176     }
177 
178     // proc(const SkPaint& modifiedPaint)
179     template <typename Proc>
180     void applyLooper(const Paint* paint, Proc proc, void (*preFilter)(SkPaint&) = nullptr) {
181         BlurDrawLooper* looper = paint ? paint->getLooper() : nullptr;
182         const SkPaint* skpPtr = paint;
183         SkPaint skp = skpPtr ? *skpPtr : SkPaint();
184         if (preFilter) {
185             preFilter(skp);
186         }
187         this->onFilterPaint(skp);
188         if (looper) {
189             looper->apply(skp, [&](SkPoint offset, const SkPaint& modifiedPaint) {
190                 mCanvas->save();
191                 mCanvas->translate(offset.fX, offset.fY);
192                 proc(modifiedPaint);
193                 mCanvas->restore();
194             });
195         } else {
196             proc(skp);
197         }
198     }
199 
200 private:
201     struct SaveRec {
202         int saveCount;
203         SaveFlags::Flags saveFlags;
204         size_t clipIndex;
205     };
206 
207     const SaveRec* currentSaveRec() const;
208     void recordPartialSave(SaveFlags::Flags flags);
209 
210     template <typename T>
211     void recordClip(const T&, SkClipOp);
212     void applyPersistentClips(size_t clipStartIndex);
213 
214     void drawPoints(const float* points, int count, const Paint& paint, SkCanvas::PointMode mode);
215 
216     class Clip;
217 
218     std::unique_ptr<SkCanvas> mCanvasOwned;    // might own a canvas we allocated
219     SkCanvas* mCanvas;                         // we do NOT own this canvas, it must survive us
220                                                // unless it is the same as mCanvasOwned.get()
221     std::unique_ptr<SkDeque> mSaveStack;       // lazily allocated, tracks partial saves.
222     std::vector<Clip> mClipStack;              // tracks persistent clips.
223     sk_sp<PaintFilter> mPaintFilter;
224 };
225 
226 }  // namespace android
227