1 /*
2  * Copyright (C) 2014 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 <cutils/compiler.h>
20 #include <utils/Functor.h>
21 
22 #include "GlFunctorLifecycleListener.h"
23 #include "utils/Macros.h"
24 #include <androidfw/ResourceTypes.h>
25 
26 #include <SkBitmap.h>
27 #include <SkCanvas.h>
28 #include <SkMatrix.h>
29 
30 class SkCanvasState;
31 
32 namespace minikin {
33     class Layout;
34 }
35 
36 namespace android {
37 
38 namespace uirenderer {
39     class CanvasPropertyPaint;
40     class CanvasPropertyPrimitive;
41     class DeferredLayerUpdater;
42     class DisplayList;
43     class RenderNode;
44 }
45 
46 namespace SaveFlags {
47 
48 // These must match the corresponding Canvas API constants.
49 enum {
50     Matrix        = 0x01,
51     Clip          = 0x02,
52     HasAlphaLayer = 0x04,
53     ClipToLayer   = 0x10,
54 
55     // Helper constant
56     MatrixClip    = Matrix | Clip,
57 };
58 typedef uint32_t Flags;
59 
60 } // namespace SaveFlags
61 
62 namespace uirenderer {
63 class SkiaCanvasProxy;
64 namespace VectorDrawable {
65 class Tree;
66 };
67 };
68 typedef uirenderer::VectorDrawable::Tree VectorDrawableRoot;
69 
70 class Bitmap;
71 class Paint;
72 struct Typeface;
73 
74 class ANDROID_API Canvas {
75 public:
~Canvas()76     virtual ~Canvas() {};
77 
78     static Canvas* create_canvas(const SkBitmap& bitmap);
79 
80     /**
81      *  Create a new Canvas object that records view system drawing operations for deferred
82      *  rendering. A canvas returned by this call supports calls to the resetRecording(...) and
83      *  finishRecording() calls.  The latter call returns a DisplayList that is specific to the
84      *  RenderPipeline defined by Properties::getRenderPipelineType().
85      *
86      *  @param width of the requested Canvas.
87      *  @param height of the requested Canvas.
88      *  @param renderNode is an optional parameter that specifies the node that will consume the
89      *      DisplayList produced by the returned Canvas.  This enables the reuse of select C++
90      *      objects as a speed optimization.
91      *  @return new non-null Canvas Object.  The type of DisplayList produced by this canvas is
92             determined based on Properties::getRenderPipelineType().
93      *
94      */
95     static WARN_UNUSED_RESULT Canvas* create_recording_canvas(int width, int height,
96             uirenderer::RenderNode* renderNode = nullptr);
97 
98     enum class XformToSRGB {
99         // Transform any Bitmaps to the sRGB color space before drawing.
100         kImmediate,
101 
102         // Draw the Bitmap as is.  This likely means that we are recording and that the
103         // transform can be handled at playback time.
104         kDefer,
105     };
106 
107     /**
108      *  Create a new Canvas object which delegates to an SkCanvas.
109      *
110      *  @param skiaCanvas Must not be NULL. All drawing calls will be
111      *      delegated to this object. This function will call ref() on the
112      *      SkCanvas, and the returned Canvas will unref() it upon
113      *      destruction.
114      *  @param xformToSRGB Indicates if bitmaps should be xformed to the sRGB
115      *      color space before drawing.
116      *  @return new non-null Canvas Object.  The type of DisplayList produced by this canvas is
117      *      determined based on  Properties::getRenderPipelineType().
118      */
119     static Canvas* create_canvas(SkCanvas* skiaCanvas, XformToSRGB xformToSRGB);
120 
121     /**
122      *  Provides a Skia SkCanvas interface that acts as a proxy to this Canvas.
123      *  It is useful for testing and clients (e.g. Picture/Movie) that expect to
124      *  draw their contents into an SkCanvas.
125      *
126      *  The SkCanvas returned is *only* valid until another Canvas call is made
127      *  that would change state (e.g. matrix or clip). Clients of asSkCanvas()
128      *  are responsible for *not* persisting this pointer.
129      *
130      *  Further, the returned SkCanvas should NOT be unref'd and is valid until
131      *  this canvas is destroyed or a new bitmap is set.
132      */
133     virtual SkCanvas* asSkCanvas() = 0;
134 
135 
136     virtual void setBitmap(const SkBitmap& bitmap) = 0;
137 
138     virtual bool isOpaque() = 0;
139     virtual int width() = 0;
140     virtual int height() = 0;
141 
142 // ----------------------------------------------------------------------------
143 // View System operations (not exposed in public Canvas API)
144 // ----------------------------------------------------------------------------
145 
146     virtual void resetRecording(int width, int height,
147             uirenderer::RenderNode* renderNode = nullptr) = 0;
148     virtual uirenderer::DisplayList* finishRecording() = 0;
149     virtual void insertReorderBarrier(bool enableReorder) = 0;
150 
151     virtual void setHighContrastText(bool highContrastText) = 0;
152     virtual bool isHighContrastText() = 0;
153 
154     virtual void drawRoundRect(uirenderer::CanvasPropertyPrimitive* left,
155             uirenderer::CanvasPropertyPrimitive* top, uirenderer::CanvasPropertyPrimitive* right,
156             uirenderer::CanvasPropertyPrimitive* bottom, uirenderer::CanvasPropertyPrimitive* rx,
157             uirenderer::CanvasPropertyPrimitive* ry, uirenderer::CanvasPropertyPaint* paint) = 0;
158     virtual void drawCircle(uirenderer::CanvasPropertyPrimitive* x,
159             uirenderer::CanvasPropertyPrimitive* y, uirenderer::CanvasPropertyPrimitive* radius,
160             uirenderer::CanvasPropertyPaint* paint) = 0;
161 
162     virtual void drawLayer(uirenderer::DeferredLayerUpdater* layerHandle) = 0;
163     virtual void drawRenderNode(uirenderer::RenderNode* renderNode) = 0;
164     virtual void callDrawGLFunction(Functor* functor,
165             uirenderer::GlFunctorLifecycleListener* listener) = 0;
166 
167 // ----------------------------------------------------------------------------
168 // Canvas state operations
169 // ----------------------------------------------------------------------------
170 
171     // Save (layer)
172     virtual int getSaveCount() const = 0;
173     virtual int save(SaveFlags::Flags flags) = 0;
174     virtual void restore() = 0;
175     virtual void restoreToCount(int saveCount) = 0;
176 
177     virtual int saveLayer(float left, float top, float right, float bottom,
178                 const SkPaint* paint, SaveFlags::Flags flags) = 0;
179     virtual int saveLayerAlpha(float left, float top, float right, float bottom,
180             int alpha, SaveFlags::Flags flags) = 0;
181 
182     // Matrix
183     virtual void getMatrix(SkMatrix* outMatrix) const = 0;
184     virtual void setMatrix(const SkMatrix& matrix) = 0;
185 
186     virtual void concat(const SkMatrix& matrix) = 0;
187     virtual void rotate(float degrees) = 0;
188     virtual void scale(float sx, float sy) = 0;
189     virtual void skew(float sx, float sy) = 0;
190     virtual void translate(float dx, float dy) = 0;
191 
192     // clip
193     virtual bool getClipBounds(SkRect* outRect) const = 0;
194     virtual bool quickRejectRect(float left, float top, float right, float bottom) const = 0;
195     virtual bool quickRejectPath(const SkPath& path) const = 0;
196 
197     virtual bool clipRect(float left, float top, float right, float bottom,
198             SkClipOp op) = 0;
199     virtual bool clipPath(const SkPath* path, SkClipOp op) = 0;
200 
201     // filters
202     virtual SkDrawFilter* getDrawFilter() = 0;
203     virtual void setDrawFilter(SkDrawFilter* drawFilter) = 0;
204 
205     // WebView only
captureCanvasState()206     virtual SkCanvasState* captureCanvasState() const { return nullptr; }
207 
208 // ----------------------------------------------------------------------------
209 // Canvas draw operations
210 // ----------------------------------------------------------------------------
211     virtual void drawColor(int color, SkBlendMode mode) = 0;
212     virtual void drawPaint(const SkPaint& paint) = 0;
213 
214     // Geometry
215     virtual void drawPoint(float x, float y, const SkPaint& paint) = 0;
216     virtual void drawPoints(const float* points, int floatCount, const SkPaint& paint) = 0;
217     virtual void drawLine(float startX, float startY, float stopX, float stopY,
218                 const SkPaint& paint) = 0;
219     virtual void drawLines(const float* points, int floatCount, const SkPaint& paint) = 0;
220     virtual void drawRect(float left, float top, float right, float bottom,
221             const SkPaint& paint) = 0;
222     virtual void drawRegion(const SkRegion& region, const SkPaint& paint) = 0;
223     virtual void drawRoundRect(float left, float top, float right, float bottom,
224             float rx, float ry, const SkPaint& paint) = 0;
225     virtual void drawCircle(float x, float y, float radius, const SkPaint& paint) = 0;
226     virtual void drawOval(float left, float top, float right, float bottom,
227             const SkPaint& paint) = 0;
228     virtual void drawArc(float left, float top, float right, float bottom,
229             float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) = 0;
230     virtual void drawPath(const SkPath& path, const SkPaint& paint) = 0;
231     virtual void drawVertices(SkCanvas::VertexMode vertexMode, int vertexCount,
232                               const float* verts, const float* tex, const int* colors,
233                               const uint16_t* indices, int indexCount, const SkPaint& paint) = 0;
234 
235     // Bitmap-based
236     virtual void drawBitmap(Bitmap& bitmap, float left, float top,
237             const SkPaint* paint) = 0;
238     virtual void drawBitmap(Bitmap& bitmap, const SkMatrix& matrix,
239             const SkPaint* paint) = 0;
240     virtual void drawBitmap(Bitmap& bitmap, float srcLeft, float srcTop,
241             float srcRight, float srcBottom, float dstLeft, float dstTop,
242             float dstRight, float dstBottom, const SkPaint* paint) = 0;
243     virtual void drawBitmapMesh(Bitmap& bitmap, int meshWidth, int meshHeight,
244             const float* vertices, const int* colors, const SkPaint* paint) = 0;
245     virtual void drawNinePatch(Bitmap& bitmap, const android::Res_png_9patch& chunk,
246             float dstLeft, float dstTop, float dstRight, float dstBottom,
247             const SkPaint* paint) = 0;
248 
249     /**
250      * Specifies if the positions passed to ::drawText are absolute or relative
251      * to the (x,y) value provided.
252      *
253      * If true the (x,y) values are ignored. Otherwise, those (x,y) values need
254      * to be added to each glyph's position to get its absolute position.
255      */
256     virtual bool drawTextAbsolutePos() const = 0;
257 
258     /**
259      * Draws a VectorDrawable onto the canvas.
260      */
261     virtual void drawVectorDrawable(VectorDrawableRoot* tree) = 0;
262 
263     /**
264      * Converts utf16 text to glyphs, calculating position and boundary,
265      * and delegating the final draw to virtual drawGlyphs method.
266      */
267     void drawText(const uint16_t* text, int start, int count, int contextCount,
268             float x, float y, int bidiFlags, const Paint& origPaint, Typeface* typeface);
269 
270     void drawTextOnPath(const uint16_t* text, int count, int bidiFlags, const SkPath& path,
271             float hOffset, float vOffset, const Paint& paint, Typeface* typeface);
272 
273 protected:
274     void drawTextDecorations(float x, float y, float length, const SkPaint& paint);
275 
276     /**
277      * drawText: count is of glyphs
278      * totalAdvance: used to define width of text decorations (underlines, strikethroughs).
279      */
280     virtual void drawGlyphs(const uint16_t* glyphs, const float* positions, int count,
281             const SkPaint& paint, float x, float y,
282             float boundsLeft, float boundsTop, float boundsRight, float boundsBottom,
283             float totalAdvance) = 0;
284     virtual void drawLayoutOnPath(const minikin::Layout& layout, float hOffset, float vOffset,
285             const SkPaint& paint, const SkPath& path, size_t start, size_t end) = 0;
286     friend class DrawTextFunctor;
287     friend class DrawTextOnPathFunctor;
288     friend class uirenderer::SkiaCanvasProxy;
289 };
290 
291 }; // namespace android
292