blob: 27facdf652cd9b3b31588a566b8152f0582e6b7e [file] [log] [blame]
Derek Sollenbergercae05e02014-07-24 15:22:13 -04001/*
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
John Reck849911a2015-01-20 07:51:14 -080020#include <cutils/compiler.h>
Derek Sollenberger6f485562015-07-30 10:00:39 -040021#include <utils/Functor.h>
John Reck849911a2015-01-20 07:51:14 -080022
Derek Sollenberger4c5efe92015-07-10 13:56:39 -040023#include "utils/NinePatch.h"
24
John Reck849911a2015-01-20 07:51:14 -080025#include <SkBitmap.h>
26#include <SkCanvas.h>
27#include <SkMatrix.h>
Derek Sollenbergercae05e02014-07-24 15:22:13 -040028
29namespace android {
30
Derek Sollenberger6f485562015-07-30 10:00:39 -040031namespace uirenderer {
32 class CanvasPropertyPaint;
33 class CanvasPropertyPrimitive;
34 class DeferredLayerUpdater;
35 class DisplayList;
36 class RenderNode;
37}
38
Florin Malitaeecff562015-12-21 10:43:01 -050039namespace 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
Doris Liu766431a2016-02-04 22:17:11 +000055namespace uirenderer {
56namespace VectorDrawable {
57class Tree;
58};
59};
60typedef uirenderer::VectorDrawable::Tree VectorDrawableRoot;
61
John Reck849911a2015-01-20 07:51:14 -080062class ANDROID_API Canvas {
Derek Sollenbergercae05e02014-07-24 15:22:13 -040063public:
64 virtual ~Canvas() {};
65
John Reckc1b33d62015-04-22 09:04:45 -070066 static Canvas* create_canvas(const SkBitmap& bitmap);
Leon Scroggins III18981292014-12-17 11:30:31 -050067
Derek Sollenberger6f485562015-07-30 10:00:39 -040068 static Canvas* create_recording_canvas(int width, int height);
69
Leon Scroggins III18981292014-12-17 11:30:31 -050070 /**
71 * Create a new Canvas object which delegates to an SkCanvas.
72 *
73 * @param skiaCanvas Must not be NULL. All drawing calls will be
74 * delegated to this object. This function will call ref() on the
75 * SkCanvas, and the returned Canvas will unref() it upon
76 * destruction.
77 * @return new Canvas object. Will not return NULL.
78 */
Derek Sollenbergercae05e02014-07-24 15:22:13 -040079 static Canvas* create_canvas(SkCanvas* skiaCanvas);
80
Derek Sollenberger1db141f2014-12-16 08:37:20 -050081 /**
82 * Provides a Skia SkCanvas interface that acts as a proxy to this Canvas.
83 * It is useful for testing and clients (e.g. Picture/Movie) that expect to
84 * draw their contents into an SkCanvas.
85 *
Tom Hudson90fb1f62015-06-24 09:13:50 -040086 * The SkCanvas returned is *only* valid until another Canvas call is made
87 * that would change state (e.g. matrix or clip). Clients of asSkCanvas()
88 * are responsible for *not* persisting this pointer.
89 *
Derek Sollenberger1db141f2014-12-16 08:37:20 -050090 * Further, the returned SkCanvas should NOT be unref'd and is valid until
91 * this canvas is destroyed or a new bitmap is set.
92 */
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050093 virtual SkCanvas* asSkCanvas() = 0;
Derek Sollenbergercae05e02014-07-24 15:22:13 -040094
Derek Sollenberger6f485562015-07-30 10:00:39 -040095
John Reckc1b33d62015-04-22 09:04:45 -070096 virtual void setBitmap(const SkBitmap& bitmap) = 0;
Derek Sollenbergercae05e02014-07-24 15:22:13 -040097
98 virtual bool isOpaque() = 0;
99 virtual int width() = 0;
100 virtual int height() = 0;
101
Derek Sollenberger6f485562015-07-30 10:00:39 -0400102// ----------------------------------------------------------------------------
103// View System operations (not exposed in public Canvas API)
104// ----------------------------------------------------------------------------
105
106 virtual void resetRecording(int width, int height) = 0;
107 virtual uirenderer::DisplayList* finishRecording() = 0;
108 virtual void insertReorderBarrier(bool enableReorder) = 0;
109
Derek Sollenberger6578a982015-07-13 13:24:29 -0400110 virtual void setHighContrastText(bool highContrastText) = 0;
111 virtual bool isHighContrastText() = 0;
112
Derek Sollenberger6f485562015-07-30 10:00:39 -0400113 virtual void drawRoundRect(uirenderer::CanvasPropertyPrimitive* left,
114 uirenderer::CanvasPropertyPrimitive* top, uirenderer::CanvasPropertyPrimitive* right,
115 uirenderer::CanvasPropertyPrimitive* bottom, uirenderer::CanvasPropertyPrimitive* rx,
116 uirenderer::CanvasPropertyPrimitive* ry, uirenderer::CanvasPropertyPaint* paint) = 0;
117 virtual void drawCircle(uirenderer::CanvasPropertyPrimitive* x,
118 uirenderer::CanvasPropertyPrimitive* y, uirenderer::CanvasPropertyPrimitive* radius,
119 uirenderer::CanvasPropertyPaint* paint) = 0;
120
121 virtual void drawLayer(uirenderer::DeferredLayerUpdater* layerHandle) = 0;
122 virtual void drawRenderNode(uirenderer::RenderNode* renderNode) = 0;
123 virtual void callDrawGLFunction(Functor* functor) = 0;
124
Derek Sollenbergercae05e02014-07-24 15:22:13 -0400125// ----------------------------------------------------------------------------
126// Canvas state operations
127// ----------------------------------------------------------------------------
Florin Malitaeecff562015-12-21 10:43:01 -0500128
Derek Sollenbergercae05e02014-07-24 15:22:13 -0400129 // Save (layer)
130 virtual int getSaveCount() const = 0;
Florin Malitaeecff562015-12-21 10:43:01 -0500131 virtual int save(SaveFlags::Flags flags) = 0;
Derek Sollenbergercae05e02014-07-24 15:22:13 -0400132 virtual void restore() = 0;
133 virtual void restoreToCount(int saveCount) = 0;
134
135 virtual int saveLayer(float left, float top, float right, float bottom,
Florin Malitaeecff562015-12-21 10:43:01 -0500136 const SkPaint* paint, SaveFlags::Flags flags) = 0;
Derek Sollenbergercae05e02014-07-24 15:22:13 -0400137 virtual int saveLayerAlpha(float left, float top, float right, float bottom,
Florin Malitaeecff562015-12-21 10:43:01 -0500138 int alpha, SaveFlags::Flags flags) = 0;
Derek Sollenbergercae05e02014-07-24 15:22:13 -0400139
140 // Matrix
141 virtual void getMatrix(SkMatrix* outMatrix) const = 0;
142 virtual void setMatrix(const SkMatrix& matrix) = 0;
143
144 virtual void concat(const SkMatrix& matrix) = 0;
145 virtual void rotate(float degrees) = 0;
146 virtual void scale(float sx, float sy) = 0;
147 virtual void skew(float sx, float sy) = 0;
148 virtual void translate(float dx, float dy) = 0;
149
150 // clip
151 virtual bool getClipBounds(SkRect* outRect) const = 0;
152 virtual bool quickRejectRect(float left, float top, float right, float bottom) const = 0;
153 virtual bool quickRejectPath(const SkPath& path) const = 0;
154
John Reckc1b33d62015-04-22 09:04:45 -0700155 virtual bool clipRect(float left, float top, float right, float bottom,
156 SkRegion::Op op = SkRegion::kIntersect_Op) = 0;
Derek Sollenbergercae05e02014-07-24 15:22:13 -0400157 virtual bool clipPath(const SkPath* path, SkRegion::Op op) = 0;
158 virtual bool clipRegion(const SkRegion* region, SkRegion::Op op) = 0;
159
160 // filters
161 virtual SkDrawFilter* getDrawFilter() = 0;
162 virtual void setDrawFilter(SkDrawFilter* drawFilter) = 0;
163
164// ----------------------------------------------------------------------------
165// Canvas draw operations
166// ----------------------------------------------------------------------------
167 virtual void drawColor(int color, SkXfermode::Mode mode) = 0;
168 virtual void drawPaint(const SkPaint& paint) = 0;
169
170 // Geometry
171 virtual void drawPoint(float x, float y, const SkPaint& paint) = 0;
Chris Craik386aa032015-12-07 17:08:25 -0800172 virtual void drawPoints(const float* points, int floatCount, const SkPaint& paint) = 0;
Derek Sollenbergercae05e02014-07-24 15:22:13 -0400173 virtual void drawLine(float startX, float startY, float stopX, float stopY,
174 const SkPaint& paint) = 0;
Chris Craik386aa032015-12-07 17:08:25 -0800175 virtual void drawLines(const float* points, int floatCount, const SkPaint& paint) = 0;
Derek Sollenbergercae05e02014-07-24 15:22:13 -0400176 virtual void drawRect(float left, float top, float right, float bottom,
177 const SkPaint& paint) = 0;
Derek Sollenberger94394b32015-07-10 09:58:41 -0400178 virtual void drawRegion(const SkRegion& region, const SkPaint& paint) = 0;
Derek Sollenbergercae05e02014-07-24 15:22:13 -0400179 virtual void drawRoundRect(float left, float top, float right, float bottom,
180 float rx, float ry, const SkPaint& paint) = 0;
181 virtual void drawCircle(float x, float y, float radius, const SkPaint& paint) = 0;
182 virtual void drawOval(float left, float top, float right, float bottom,
183 const SkPaint& paint) = 0;
184 virtual void drawArc(float left, float top, float right, float bottom,
185 float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) = 0;
186 virtual void drawPath(const SkPath& path, const SkPaint& paint) = 0;
187 virtual void drawVertices(SkCanvas::VertexMode vertexMode, int vertexCount,
188 const float* verts, const float* tex, const int* colors,
189 const uint16_t* indices, int indexCount, const SkPaint& paint) = 0;
190
191 // Bitmap-based
192 virtual void drawBitmap(const SkBitmap& bitmap, float left, float top,
193 const SkPaint* paint) = 0;
194 virtual void drawBitmap(const SkBitmap& bitmap, const SkMatrix& matrix,
195 const SkPaint* paint) = 0;
196 virtual void drawBitmap(const SkBitmap& bitmap, float srcLeft, float srcTop,
197 float srcRight, float srcBottom, float dstLeft, float dstTop,
198 float dstRight, float dstBottom, const SkPaint* paint) = 0;
199 virtual void drawBitmapMesh(const SkBitmap& bitmap, int meshWidth, int meshHeight,
200 const float* vertices, const int* colors, const SkPaint* paint) = 0;
Derek Sollenberger4c5efe92015-07-10 13:56:39 -0400201 virtual void drawNinePatch(const SkBitmap& bitmap, const android::Res_png_9patch& chunk,
202 float dstLeft, float dstTop, float dstRight, float dstBottom,
203 const SkPaint* paint) = 0;
Derek Sollenbergercae05e02014-07-24 15:22:13 -0400204
205 // Text
Tom Hudson8dfaa492014-12-09 15:03:44 -0500206 /**
207 * drawText: count is of glyphs
Chris Craika1717272015-11-19 13:02:43 -0800208 * totalAdvance: used to define width of text decorations (underlines, strikethroughs).
Tom Hudson8dfaa492014-12-09 15:03:44 -0500209 */
210 virtual void drawText(const uint16_t* glyphs, const float* positions, int count,
Derek Sollenbergercae05e02014-07-24 15:22:13 -0400211 const SkPaint& paint, float x, float y,
Tom Hudson8dfaa492014-12-09 15:03:44 -0500212 float boundsLeft, float boundsTop, float boundsRight, float boundsBottom,
213 float totalAdvance) = 0;
Tom Hudson8dfaa492014-12-09 15:03:44 -0500214 /** drawTextOnPath: count is of glyphs */
Derek Sollenbergercae05e02014-07-24 15:22:13 -0400215 virtual void drawTextOnPath(const uint16_t* glyphs, int count, const SkPath& path,
216 float hOffset, float vOffset, const SkPaint& paint) = 0;
217
Tom Hudson8dfaa492014-12-09 15:03:44 -0500218 /**
Derek Sollenbergercae05e02014-07-24 15:22:13 -0400219 * Specifies if the positions passed to ::drawText are absolute or relative
220 * to the (x,y) value provided.
221 *
222 * If true the (x,y) values are ignored. Otherwise, those (x,y) values need
223 * to be added to each glyph's position to get its absolute position.
224 */
225 virtual bool drawTextAbsolutePos() const = 0;
Chris Craika1717272015-11-19 13:02:43 -0800226
Doris Liu766431a2016-02-04 22:17:11 +0000227 /**
228 * Draws a VectorDrawable onto the canvas.
229 */
230 virtual void drawVectorDrawable(VectorDrawableRoot* tree);
231
Chris Craika1717272015-11-19 13:02:43 -0800232protected:
233 void drawTextDecorations(float x, float y, float length, const SkPaint& paint);
Derek Sollenbergercae05e02014-07-24 15:22:13 -0400234};
235
236}; // namespace android
237#endif // ANDROID_GRAPHICS_CANVAS_H