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