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