blob: d76143bf999c31a2af419e51d5950f3d49f7567b [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
Chris Craik5e00c7c2016-07-06 16:10:09 -070017#pragma once
Derek Sollenbergercae05e02014-07-24 15:22:13 -040018
John Reck849911a2015-01-20 07:51:14 -080019#include <cutils/compiler.h>
Derek Sollenberger6f485562015-07-30 10:00:39 -040020#include <utils/Functor.h>
John Reck849911a2015-01-20 07:51:14 -080021
John Reckcd1c3eb2016-04-14 10:38:54 -070022#include "GlFunctorLifecycleListener.h"
Stan Iliev06152cd2016-07-27 17:55:43 -040023#include "utils/Macros.h"
Derek Sollenberger4c5efe92015-07-10 13:56:39 -040024#include "utils/NinePatch.h"
25
John Reck849911a2015-01-20 07:51:14 -080026#include <SkBitmap.h>
27#include <SkCanvas.h>
28#include <SkMatrix.h>
Derek Sollenbergercae05e02014-07-24 15:22:13 -040029
Yuqian Liafc221492016-07-18 13:07:42 -040030namespace minikin {
31 class Layout;
32}
33
Derek Sollenbergercae05e02014-07-24 15:22:13 -040034namespace android {
35
Derek Sollenberger6f485562015-07-30 10:00:39 -040036namespace uirenderer {
37 class CanvasPropertyPaint;
38 class CanvasPropertyPrimitive;
39 class DeferredLayerUpdater;
40 class DisplayList;
41 class RenderNode;
42}
43
Florin Malitaeecff562015-12-21 10:43:01 -050044namespace SaveFlags {
45
46// These must match the corresponding Canvas API constants.
47enum {
48 Matrix = 0x01,
49 Clip = 0x02,
50 HasAlphaLayer = 0x04,
51 ClipToLayer = 0x10,
52
53 // Helper constant
54 MatrixClip = Matrix | Clip,
55};
56typedef uint32_t Flags;
57
58} // namespace SaveFlags
59
Doris Liu766431a2016-02-04 22:17:11 +000060namespace uirenderer {
Derek Sollenberger79abbf22016-03-24 11:07:19 -040061class SkiaCanvasProxy;
Doris Liu766431a2016-02-04 22:17:11 +000062namespace VectorDrawable {
63class Tree;
64};
65};
66typedef uirenderer::VectorDrawable::Tree VectorDrawableRoot;
67
sergeyvdccca442016-03-21 15:38:21 -070068class Paint;
sergeyvbad99182016-03-17 11:24:22 -070069struct Typeface;
sergeyvdccca442016-03-21 15:38:21 -070070
John Reck849911a2015-01-20 07:51:14 -080071class ANDROID_API Canvas {
Derek Sollenbergercae05e02014-07-24 15:22:13 -040072public:
73 virtual ~Canvas() {};
74
John Reckc1b33d62015-04-22 09:04:45 -070075 static Canvas* create_canvas(const SkBitmap& bitmap);
Leon Scroggins III18981292014-12-17 11:30:31 -050076
Stan Iliev06152cd2016-07-27 17:55:43 -040077 static WARN_UNUSED_RESULT Canvas* create_recording_canvas(int width, int height);
Derek Sollenberger6f485562015-07-30 10:00:39 -040078
Leon Scroggins III18981292014-12-17 11:30:31 -050079 /**
80 * Create a new Canvas object which delegates to an SkCanvas.
81 *
82 * @param skiaCanvas Must not be NULL. All drawing calls will be
83 * delegated to this object. This function will call ref() on the
84 * SkCanvas, and the returned Canvas will unref() it upon
85 * destruction.
86 * @return new Canvas object. Will not return NULL.
87 */
Derek Sollenbergercae05e02014-07-24 15:22:13 -040088 static Canvas* create_canvas(SkCanvas* skiaCanvas);
89
Derek Sollenberger1db141f2014-12-16 08:37:20 -050090 /**
91 * Provides a Skia SkCanvas interface that acts as a proxy to this Canvas.
92 * It is useful for testing and clients (e.g. Picture/Movie) that expect to
93 * draw their contents into an SkCanvas.
94 *
Tom Hudson90fb1f62015-06-24 09:13:50 -040095 * The SkCanvas returned is *only* valid until another Canvas call is made
96 * that would change state (e.g. matrix or clip). Clients of asSkCanvas()
97 * are responsible for *not* persisting this pointer.
98 *
Derek Sollenberger1db141f2014-12-16 08:37:20 -050099 * Further, the returned SkCanvas should NOT be unref'd and is valid until
100 * this canvas is destroyed or a new bitmap is set.
101 */
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -0500102 virtual SkCanvas* asSkCanvas() = 0;
Derek Sollenbergercae05e02014-07-24 15:22:13 -0400103
Derek Sollenberger6f485562015-07-30 10:00:39 -0400104
John Reckc1b33d62015-04-22 09:04:45 -0700105 virtual void setBitmap(const SkBitmap& bitmap) = 0;
Derek Sollenbergercae05e02014-07-24 15:22:13 -0400106
107 virtual bool isOpaque() = 0;
108 virtual int width() = 0;
109 virtual int height() = 0;
110
Derek Sollenberger6f485562015-07-30 10:00:39 -0400111// ----------------------------------------------------------------------------
112// View System operations (not exposed in public Canvas API)
113// ----------------------------------------------------------------------------
114
115 virtual void resetRecording(int width, int height) = 0;
116 virtual uirenderer::DisplayList* finishRecording() = 0;
117 virtual void insertReorderBarrier(bool enableReorder) = 0;
118
Derek Sollenberger6578a982015-07-13 13:24:29 -0400119 virtual void setHighContrastText(bool highContrastText) = 0;
120 virtual bool isHighContrastText() = 0;
121
Derek Sollenberger6f485562015-07-30 10:00:39 -0400122 virtual void drawRoundRect(uirenderer::CanvasPropertyPrimitive* left,
123 uirenderer::CanvasPropertyPrimitive* top, uirenderer::CanvasPropertyPrimitive* right,
124 uirenderer::CanvasPropertyPrimitive* bottom, uirenderer::CanvasPropertyPrimitive* rx,
125 uirenderer::CanvasPropertyPrimitive* ry, uirenderer::CanvasPropertyPaint* paint) = 0;
126 virtual void drawCircle(uirenderer::CanvasPropertyPrimitive* x,
127 uirenderer::CanvasPropertyPrimitive* y, uirenderer::CanvasPropertyPrimitive* radius,
128 uirenderer::CanvasPropertyPaint* paint) = 0;
129
130 virtual void drawLayer(uirenderer::DeferredLayerUpdater* layerHandle) = 0;
131 virtual void drawRenderNode(uirenderer::RenderNode* renderNode) = 0;
John Reckcd1c3eb2016-04-14 10:38:54 -0700132 virtual void callDrawGLFunction(Functor* functor,
133 uirenderer::GlFunctorLifecycleListener* listener) = 0;
Derek Sollenberger6f485562015-07-30 10:00:39 -0400134
Derek Sollenbergercae05e02014-07-24 15:22:13 -0400135// ----------------------------------------------------------------------------
136// Canvas state operations
137// ----------------------------------------------------------------------------
Florin Malitaeecff562015-12-21 10:43:01 -0500138
Derek Sollenbergercae05e02014-07-24 15:22:13 -0400139 // Save (layer)
140 virtual int getSaveCount() const = 0;
Florin Malitaeecff562015-12-21 10:43:01 -0500141 virtual int save(SaveFlags::Flags flags) = 0;
Derek Sollenbergercae05e02014-07-24 15:22:13 -0400142 virtual void restore() = 0;
143 virtual void restoreToCount(int saveCount) = 0;
144
145 virtual int saveLayer(float left, float top, float right, float bottom,
Florin Malitaeecff562015-12-21 10:43:01 -0500146 const SkPaint* paint, SaveFlags::Flags flags) = 0;
Derek Sollenbergercae05e02014-07-24 15:22:13 -0400147 virtual int saveLayerAlpha(float left, float top, float right, float bottom,
Florin Malitaeecff562015-12-21 10:43:01 -0500148 int alpha, SaveFlags::Flags flags) = 0;
Derek Sollenbergercae05e02014-07-24 15:22:13 -0400149
150 // Matrix
151 virtual void getMatrix(SkMatrix* outMatrix) const = 0;
152 virtual void setMatrix(const SkMatrix& matrix) = 0;
153
154 virtual void concat(const SkMatrix& matrix) = 0;
155 virtual void rotate(float degrees) = 0;
156 virtual void scale(float sx, float sy) = 0;
157 virtual void skew(float sx, float sy) = 0;
158 virtual void translate(float dx, float dy) = 0;
159
160 // clip
161 virtual bool getClipBounds(SkRect* outRect) const = 0;
162 virtual bool quickRejectRect(float left, float top, float right, float bottom) const = 0;
163 virtual bool quickRejectPath(const SkPath& path) const = 0;
164
John Reckc1b33d62015-04-22 09:04:45 -0700165 virtual bool clipRect(float left, float top, float right, float bottom,
166 SkRegion::Op op = SkRegion::kIntersect_Op) = 0;
Derek Sollenbergercae05e02014-07-24 15:22:13 -0400167 virtual bool clipPath(const SkPath* path, SkRegion::Op op) = 0;
168 virtual bool clipRegion(const SkRegion* region, SkRegion::Op op) = 0;
169
170 // filters
171 virtual SkDrawFilter* getDrawFilter() = 0;
172 virtual void setDrawFilter(SkDrawFilter* drawFilter) = 0;
173
174// ----------------------------------------------------------------------------
175// Canvas draw operations
176// ----------------------------------------------------------------------------
177 virtual void drawColor(int color, SkXfermode::Mode mode) = 0;
178 virtual void drawPaint(const SkPaint& paint) = 0;
179
180 // Geometry
181 virtual void drawPoint(float x, float y, const SkPaint& paint) = 0;
Chris Craik386aa032015-12-07 17:08:25 -0800182 virtual void drawPoints(const float* points, int floatCount, const SkPaint& paint) = 0;
Derek Sollenbergercae05e02014-07-24 15:22:13 -0400183 virtual void drawLine(float startX, float startY, float stopX, float stopY,
184 const SkPaint& paint) = 0;
Chris Craik386aa032015-12-07 17:08:25 -0800185 virtual void drawLines(const float* points, int floatCount, const SkPaint& paint) = 0;
Derek Sollenbergercae05e02014-07-24 15:22:13 -0400186 virtual void drawRect(float left, float top, float right, float bottom,
187 const SkPaint& paint) = 0;
Derek Sollenberger94394b32015-07-10 09:58:41 -0400188 virtual void drawRegion(const SkRegion& region, const SkPaint& paint) = 0;
Derek Sollenbergercae05e02014-07-24 15:22:13 -0400189 virtual void drawRoundRect(float left, float top, float right, float bottom,
190 float rx, float ry, const SkPaint& paint) = 0;
191 virtual void drawCircle(float x, float y, float radius, const SkPaint& paint) = 0;
192 virtual void drawOval(float left, float top, float right, float bottom,
193 const SkPaint& paint) = 0;
194 virtual void drawArc(float left, float top, float right, float bottom,
195 float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) = 0;
196 virtual void drawPath(const SkPath& path, const SkPaint& paint) = 0;
197 virtual void drawVertices(SkCanvas::VertexMode vertexMode, int vertexCount,
198 const float* verts, const float* tex, const int* colors,
199 const uint16_t* indices, int indexCount, const SkPaint& paint) = 0;
200
201 // Bitmap-based
202 virtual void drawBitmap(const SkBitmap& bitmap, float left, float top,
203 const SkPaint* paint) = 0;
204 virtual void drawBitmap(const SkBitmap& bitmap, const SkMatrix& matrix,
205 const SkPaint* paint) = 0;
206 virtual void drawBitmap(const SkBitmap& bitmap, float srcLeft, float srcTop,
207 float srcRight, float srcBottom, float dstLeft, float dstTop,
208 float dstRight, float dstBottom, const SkPaint* paint) = 0;
209 virtual void drawBitmapMesh(const SkBitmap& bitmap, int meshWidth, int meshHeight,
210 const float* vertices, const int* colors, const SkPaint* paint) = 0;
Derek Sollenberger4c5efe92015-07-10 13:56:39 -0400211 virtual void drawNinePatch(const SkBitmap& bitmap, const android::Res_png_9patch& chunk,
212 float dstLeft, float dstTop, float dstRight, float dstBottom,
213 const SkPaint* paint) = 0;
Derek Sollenbergercae05e02014-07-24 15:22:13 -0400214
Tom Hudson8dfaa492014-12-09 15:03:44 -0500215 /**
Derek Sollenbergercae05e02014-07-24 15:22:13 -0400216 * Specifies if the positions passed to ::drawText are absolute or relative
217 * to the (x,y) value provided.
218 *
219 * If true the (x,y) values are ignored. Otherwise, those (x,y) values need
220 * to be added to each glyph's position to get its absolute position.
221 */
222 virtual bool drawTextAbsolutePos() const = 0;
Chris Craika1717272015-11-19 13:02:43 -0800223
Doris Liu766431a2016-02-04 22:17:11 +0000224 /**
225 * Draws a VectorDrawable onto the canvas.
226 */
John Reck9688a3d2016-06-29 15:11:23 -0700227 virtual void drawVectorDrawable(VectorDrawableRoot* tree) = 0;
Doris Liu766431a2016-02-04 22:17:11 +0000228
sergeyvdccca442016-03-21 15:38:21 -0700229 /**
230 * Converts utf16 text to glyphs, calculating position and boundary,
231 * and delegating the final draw to virtual drawGlyphs method.
232 */
233 void drawText(const uint16_t* text, int start, int count, int contextCount,
sergeyvbad99182016-03-17 11:24:22 -0700234 float x, float y, int bidiFlags, const Paint& origPaint, Typeface* typeface);
sergeyvdccca442016-03-21 15:38:21 -0700235
236 void drawTextOnPath(const uint16_t* text, int count, int bidiFlags, const SkPath& path,
sergeyvbad99182016-03-17 11:24:22 -0700237 float hOffset, float vOffset, const Paint& paint, Typeface* typeface);
sergeyvdccca442016-03-21 15:38:21 -0700238
Chris Craika1717272015-11-19 13:02:43 -0800239protected:
240 void drawTextDecorations(float x, float y, float length, const SkPaint& paint);
Derek Sollenberger79abbf22016-03-24 11:07:19 -0400241
242 /**
243 * drawText: count is of glyphs
244 * totalAdvance: used to define width of text decorations (underlines, strikethroughs).
245 */
246 virtual void drawGlyphs(const uint16_t* glyphs, const float* positions, int count,
247 const SkPaint& paint, float x, float y,
248 float boundsLeft, float boundsTop, float boundsRight, float boundsBottom,
249 float totalAdvance) = 0;
Yuqian Liafc221492016-07-18 13:07:42 -0400250 virtual void drawLayoutOnPath(const minikin::Layout& layout, float hOffset, float vOffset,
251 const SkPaint& paint, const SkPath& path, size_t start, size_t end) = 0;
Derek Sollenberger79abbf22016-03-24 11:07:19 -0400252 friend class DrawTextFunctor;
253 friend class DrawTextOnPathFunctor;
254 friend class uirenderer::SkiaCanvasProxy;
Derek Sollenbergercae05e02014-07-24 15:22:13 -0400255};
256
257}; // namespace android