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