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