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