Canvas.h revision fc9999505a36c66892d7ccce85187936105f4f36
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 "utils/NinePatch.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    /**
97     *  Create a new Canvas object which delegates to an SkCanvas.
98     *
99     *  @param skiaCanvas Must not be NULL. All drawing calls will be
100     *      delegated to this object. This function will call ref() on the
101     *      SkCanvas, and the returned Canvas will unref() it upon
102     *      destruction.
103     *  @return new non-null Canvas Object.  The type of DisplayList produced by this canvas is
104     *      determined based on  Properties::getRenderPipelineType().
105     */
106    static Canvas* create_canvas(SkCanvas* skiaCanvas);
107
108    /**
109     *  Provides a Skia SkCanvas interface that acts as a proxy to this Canvas.
110     *  It is useful for testing and clients (e.g. Picture/Movie) that expect to
111     *  draw their contents into an SkCanvas.
112     *
113     *  The SkCanvas returned is *only* valid until another Canvas call is made
114     *  that would change state (e.g. matrix or clip). Clients of asSkCanvas()
115     *  are responsible for *not* persisting this pointer.
116     *
117     *  Further, the returned SkCanvas should NOT be unref'd and is valid until
118     *  this canvas is destroyed or a new bitmap is set.
119     */
120    virtual SkCanvas* asSkCanvas() = 0;
121
122
123    virtual void setBitmap(const SkBitmap& bitmap) = 0;
124
125    virtual bool isOpaque() = 0;
126    virtual int width() = 0;
127    virtual int height() = 0;
128
129// ----------------------------------------------------------------------------
130// View System operations (not exposed in public Canvas API)
131// ----------------------------------------------------------------------------
132
133    virtual void resetRecording(int width, int height,
134            uirenderer::RenderNode* renderNode = nullptr) = 0;
135    virtual uirenderer::DisplayList* finishRecording() = 0;
136    virtual void insertReorderBarrier(bool enableReorder) = 0;
137
138    virtual void setHighContrastText(bool highContrastText) = 0;
139    virtual bool isHighContrastText() = 0;
140
141    virtual void drawRoundRect(uirenderer::CanvasPropertyPrimitive* left,
142            uirenderer::CanvasPropertyPrimitive* top, uirenderer::CanvasPropertyPrimitive* right,
143            uirenderer::CanvasPropertyPrimitive* bottom, uirenderer::CanvasPropertyPrimitive* rx,
144            uirenderer::CanvasPropertyPrimitive* ry, uirenderer::CanvasPropertyPaint* paint) = 0;
145    virtual void drawCircle(uirenderer::CanvasPropertyPrimitive* x,
146            uirenderer::CanvasPropertyPrimitive* y, uirenderer::CanvasPropertyPrimitive* radius,
147            uirenderer::CanvasPropertyPaint* paint) = 0;
148
149    virtual void drawLayer(uirenderer::DeferredLayerUpdater* layerHandle) = 0;
150    virtual void drawRenderNode(uirenderer::RenderNode* renderNode) = 0;
151    virtual void callDrawGLFunction(Functor* functor,
152            uirenderer::GlFunctorLifecycleListener* listener) = 0;
153
154// ----------------------------------------------------------------------------
155// Canvas state operations
156// ----------------------------------------------------------------------------
157
158    // Save (layer)
159    virtual int getSaveCount() const = 0;
160    virtual int save(SaveFlags::Flags flags) = 0;
161    virtual void restore() = 0;
162    virtual void restoreToCount(int saveCount) = 0;
163
164    virtual int saveLayer(float left, float top, float right, float bottom,
165                const SkPaint* paint, SaveFlags::Flags flags) = 0;
166    virtual int saveLayerAlpha(float left, float top, float right, float bottom,
167            int alpha, SaveFlags::Flags flags) = 0;
168
169    // Matrix
170    virtual void getMatrix(SkMatrix* outMatrix) const = 0;
171    virtual void setMatrix(const SkMatrix& matrix) = 0;
172
173    virtual void concat(const SkMatrix& matrix) = 0;
174    virtual void rotate(float degrees) = 0;
175    virtual void scale(float sx, float sy) = 0;
176    virtual void skew(float sx, float sy) = 0;
177    virtual void translate(float dx, float dy) = 0;
178
179    // clip
180    virtual bool getClipBounds(SkRect* outRect) const = 0;
181    virtual bool quickRejectRect(float left, float top, float right, float bottom) const = 0;
182    virtual bool quickRejectPath(const SkPath& path) const = 0;
183
184    virtual bool clipRect(float left, float top, float right, float bottom,
185            SkRegion::Op op = SkRegion::kIntersect_Op) = 0;
186    virtual bool clipPath(const SkPath* path, SkRegion::Op op) = 0;
187    virtual bool clipRegion(const SkRegion* region, SkRegion::Op op) = 0;
188
189    // filters
190    virtual SkDrawFilter* getDrawFilter() = 0;
191    virtual void setDrawFilter(SkDrawFilter* drawFilter) = 0;
192
193// ----------------------------------------------------------------------------
194// Canvas draw operations
195// ----------------------------------------------------------------------------
196    virtual void drawColor(int color, SkBlendMode mode) = 0;
197    virtual void drawPaint(const SkPaint& paint) = 0;
198
199    // Geometry
200    virtual void drawPoint(float x, float y, const SkPaint& paint) = 0;
201    virtual void drawPoints(const float* points, int floatCount, const SkPaint& paint) = 0;
202    virtual void drawLine(float startX, float startY, float stopX, float stopY,
203                const SkPaint& paint) = 0;
204    virtual void drawLines(const float* points, int floatCount, const SkPaint& paint) = 0;
205    virtual void drawRect(float left, float top, float right, float bottom,
206            const SkPaint& paint) = 0;
207    virtual void drawRegion(const SkRegion& region, const SkPaint& paint) = 0;
208    virtual void drawRoundRect(float left, float top, float right, float bottom,
209            float rx, float ry, const SkPaint& paint) = 0;
210    virtual void drawCircle(float x, float y, float radius, const SkPaint& paint) = 0;
211    virtual void drawOval(float left, float top, float right, float bottom,
212            const SkPaint& paint) = 0;
213    virtual void drawArc(float left, float top, float right, float bottom,
214            float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) = 0;
215    virtual void drawPath(const SkPath& path, const SkPaint& paint) = 0;
216    virtual void drawVertices(SkCanvas::VertexMode vertexMode, int vertexCount,
217                              const float* verts, const float* tex, const int* colors,
218                              const uint16_t* indices, int indexCount, const SkPaint& paint) = 0;
219
220    // Bitmap-based
221    virtual void drawBitmap(Bitmap& bitmap, float left, float top,
222            const SkPaint* paint) = 0;
223    virtual void drawBitmap(Bitmap& bitmap, const SkMatrix& matrix,
224            const SkPaint* paint) = 0;
225    virtual void drawBitmap(Bitmap& bitmap, float srcLeft, float srcTop,
226            float srcRight, float srcBottom, float dstLeft, float dstTop,
227            float dstRight, float dstBottom, const SkPaint* paint) = 0;
228    virtual void drawBitmapMesh(const SkBitmap& bitmap, int meshWidth, int meshHeight,
229            const float* vertices, const int* colors, const SkPaint* paint) = 0;
230    virtual void drawNinePatch(const SkBitmap& bitmap, const android::Res_png_9patch& chunk,
231            float dstLeft, float dstTop, float dstRight, float dstBottom,
232            const SkPaint* paint) = 0;
233
234    /**
235     * Specifies if the positions passed to ::drawText are absolute or relative
236     * to the (x,y) value provided.
237     *
238     * If true the (x,y) values are ignored. Otherwise, those (x,y) values need
239     * to be added to each glyph's position to get its absolute position.
240     */
241    virtual bool drawTextAbsolutePos() const = 0;
242
243    /**
244     * Draws a VectorDrawable onto the canvas.
245     */
246    virtual void drawVectorDrawable(VectorDrawableRoot* tree) = 0;
247
248    /**
249     * Converts utf16 text to glyphs, calculating position and boundary,
250     * and delegating the final draw to virtual drawGlyphs method.
251     */
252    void drawText(const uint16_t* text, int start, int count, int contextCount,
253            float x, float y, int bidiFlags, const Paint& origPaint, Typeface* typeface);
254
255    void drawTextOnPath(const uint16_t* text, int count, int bidiFlags, const SkPath& path,
256            float hOffset, float vOffset, const Paint& paint, Typeface* typeface);
257
258protected:
259    void drawTextDecorations(float x, float y, float length, const SkPaint& paint);
260
261    /**
262     * drawText: count is of glyphs
263     * totalAdvance: used to define width of text decorations (underlines, strikethroughs).
264     */
265    virtual void drawGlyphs(const uint16_t* glyphs, const float* positions, int count,
266            const SkPaint& paint, float x, float y,
267            float boundsLeft, float boundsTop, float boundsRight, float boundsBottom,
268            float totalAdvance) = 0;
269    virtual void drawLayoutOnPath(const minikin::Layout& layout, float hOffset, float vOffset,
270            const SkPaint& paint, const SkPath& path, size_t start, size_t end) = 0;
271    friend class DrawTextFunctor;
272    friend class DrawTextOnPathFunctor;
273    friend class uirenderer::SkiaCanvasProxy;
274};
275
276}; // namespace android
277