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