blob: 9dfe454c7bc1ba1e85a3288339d0e50257f5c137 [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>
21
Derek Sollenberger4c5efe92015-07-10 13:56:39 -040022#include "utils/NinePatch.h"
23
John Reck849911a2015-01-20 07:51:14 -080024#include <SkBitmap.h>
25#include <SkCanvas.h>
26#include <SkMatrix.h>
Derek Sollenbergercae05e02014-07-24 15:22:13 -040027
28namespace android {
29
Florin Malitaeecff562015-12-21 10:43:01 -050030namespace SaveFlags {
31
32// These must match the corresponding Canvas API constants.
33enum {
34 Matrix = 0x01,
35 Clip = 0x02,
36 HasAlphaLayer = 0x04,
37 ClipToLayer = 0x10,
38
39 // Helper constant
40 MatrixClip = Matrix | Clip,
41};
42typedef uint32_t Flags;
43
44} // namespace SaveFlags
45
John Reck849911a2015-01-20 07:51:14 -080046class ANDROID_API Canvas {
Derek Sollenbergercae05e02014-07-24 15:22:13 -040047public:
48 virtual ~Canvas() {};
49
John Reckc1b33d62015-04-22 09:04:45 -070050 static Canvas* create_canvas(const SkBitmap& bitmap);
Leon Scroggins III18981292014-12-17 11:30:31 -050051
52 /**
53 * Create a new Canvas object which delegates to an SkCanvas.
54 *
55 * @param skiaCanvas Must not be NULL. All drawing calls will be
56 * delegated to this object. This function will call ref() on the
57 * SkCanvas, and the returned Canvas will unref() it upon
58 * destruction.
59 * @return new Canvas object. Will not return NULL.
60 */
Derek Sollenbergercae05e02014-07-24 15:22:13 -040061 static Canvas* create_canvas(SkCanvas* skiaCanvas);
62
Derek Sollenberger1db141f2014-12-16 08:37:20 -050063 /**
64 * Provides a Skia SkCanvas interface that acts as a proxy to this Canvas.
65 * It is useful for testing and clients (e.g. Picture/Movie) that expect to
66 * draw their contents into an SkCanvas.
67 *
Tom Hudson90fb1f62015-06-24 09:13:50 -040068 * The SkCanvas returned is *only* valid until another Canvas call is made
69 * that would change state (e.g. matrix or clip). Clients of asSkCanvas()
70 * are responsible for *not* persisting this pointer.
71 *
Derek Sollenberger1db141f2014-12-16 08:37:20 -050072 * Further, the returned SkCanvas should NOT be unref'd and is valid until
73 * this canvas is destroyed or a new bitmap is set.
74 */
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050075 virtual SkCanvas* asSkCanvas() = 0;
Derek Sollenbergercae05e02014-07-24 15:22:13 -040076
John Reckc1b33d62015-04-22 09:04:45 -070077 virtual void setBitmap(const SkBitmap& bitmap) = 0;
Derek Sollenbergercae05e02014-07-24 15:22:13 -040078
79 virtual bool isOpaque() = 0;
80 virtual int width() = 0;
81 virtual int height() = 0;
82
Derek Sollenberger6578a982015-07-13 13:24:29 -040083 virtual void setHighContrastText(bool highContrastText) = 0;
84 virtual bool isHighContrastText() = 0;
85
Derek Sollenbergercae05e02014-07-24 15:22:13 -040086// ----------------------------------------------------------------------------
87// Canvas state operations
88// ----------------------------------------------------------------------------
Florin Malitaeecff562015-12-21 10:43:01 -050089
Derek Sollenbergercae05e02014-07-24 15:22:13 -040090 // Save (layer)
91 virtual int getSaveCount() const = 0;
Florin Malitaeecff562015-12-21 10:43:01 -050092 virtual int save(SaveFlags::Flags flags) = 0;
Derek Sollenbergercae05e02014-07-24 15:22:13 -040093 virtual void restore() = 0;
94 virtual void restoreToCount(int saveCount) = 0;
95
96 virtual int saveLayer(float left, float top, float right, float bottom,
Florin Malitaeecff562015-12-21 10:43:01 -050097 const SkPaint* paint, SaveFlags::Flags flags) = 0;
Derek Sollenbergercae05e02014-07-24 15:22:13 -040098 virtual int saveLayerAlpha(float left, float top, float right, float bottom,
Florin Malitaeecff562015-12-21 10:43:01 -050099 int alpha, SaveFlags::Flags flags) = 0;
Derek Sollenbergercae05e02014-07-24 15:22:13 -0400100
101 // Matrix
102 virtual void getMatrix(SkMatrix* outMatrix) const = 0;
103 virtual void setMatrix(const SkMatrix& matrix) = 0;
104
105 virtual void concat(const SkMatrix& matrix) = 0;
106 virtual void rotate(float degrees) = 0;
107 virtual void scale(float sx, float sy) = 0;
108 virtual void skew(float sx, float sy) = 0;
109 virtual void translate(float dx, float dy) = 0;
110
111 // clip
112 virtual bool getClipBounds(SkRect* outRect) const = 0;
113 virtual bool quickRejectRect(float left, float top, float right, float bottom) const = 0;
114 virtual bool quickRejectPath(const SkPath& path) const = 0;
115
John Reckc1b33d62015-04-22 09:04:45 -0700116 virtual bool clipRect(float left, float top, float right, float bottom,
117 SkRegion::Op op = SkRegion::kIntersect_Op) = 0;
Derek Sollenbergercae05e02014-07-24 15:22:13 -0400118 virtual bool clipPath(const SkPath* path, SkRegion::Op op) = 0;
119 virtual bool clipRegion(const SkRegion* region, SkRegion::Op op) = 0;
120
121 // filters
122 virtual SkDrawFilter* getDrawFilter() = 0;
123 virtual void setDrawFilter(SkDrawFilter* drawFilter) = 0;
124
125// ----------------------------------------------------------------------------
126// Canvas draw operations
127// ----------------------------------------------------------------------------
128 virtual void drawColor(int color, SkXfermode::Mode mode) = 0;
129 virtual void drawPaint(const SkPaint& paint) = 0;
130
131 // Geometry
132 virtual void drawPoint(float x, float y, const SkPaint& paint) = 0;
Chris Craik386aa032015-12-07 17:08:25 -0800133 virtual void drawPoints(const float* points, int floatCount, const SkPaint& paint) = 0;
Derek Sollenbergercae05e02014-07-24 15:22:13 -0400134 virtual void drawLine(float startX, float startY, float stopX, float stopY,
135 const SkPaint& paint) = 0;
Chris Craik386aa032015-12-07 17:08:25 -0800136 virtual void drawLines(const float* points, int floatCount, const SkPaint& paint) = 0;
Derek Sollenbergercae05e02014-07-24 15:22:13 -0400137 virtual void drawRect(float left, float top, float right, float bottom,
138 const SkPaint& paint) = 0;
Derek Sollenberger94394b32015-07-10 09:58:41 -0400139 virtual void drawRegion(const SkRegion& region, const SkPaint& paint) = 0;
Derek Sollenbergercae05e02014-07-24 15:22:13 -0400140 virtual void drawRoundRect(float left, float top, float right, float bottom,
141 float rx, float ry, const SkPaint& paint) = 0;
142 virtual void drawCircle(float x, float y, float radius, const SkPaint& paint) = 0;
143 virtual void drawOval(float left, float top, float right, float bottom,
144 const SkPaint& paint) = 0;
145 virtual void drawArc(float left, float top, float right, float bottom,
146 float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) = 0;
147 virtual void drawPath(const SkPath& path, const SkPaint& paint) = 0;
148 virtual void drawVertices(SkCanvas::VertexMode vertexMode, int vertexCount,
149 const float* verts, const float* tex, const int* colors,
150 const uint16_t* indices, int indexCount, const SkPaint& paint) = 0;
151
152 // Bitmap-based
153 virtual void drawBitmap(const SkBitmap& bitmap, float left, float top,
154 const SkPaint* paint) = 0;
155 virtual void drawBitmap(const SkBitmap& bitmap, const SkMatrix& matrix,
156 const SkPaint* paint) = 0;
157 virtual void drawBitmap(const SkBitmap& bitmap, float srcLeft, float srcTop,
158 float srcRight, float srcBottom, float dstLeft, float dstTop,
159 float dstRight, float dstBottom, const SkPaint* paint) = 0;
160 virtual void drawBitmapMesh(const SkBitmap& bitmap, int meshWidth, int meshHeight,
161 const float* vertices, const int* colors, const SkPaint* paint) = 0;
Derek Sollenberger4c5efe92015-07-10 13:56:39 -0400162 virtual void drawNinePatch(const SkBitmap& bitmap, const android::Res_png_9patch& chunk,
163 float dstLeft, float dstTop, float dstRight, float dstBottom,
164 const SkPaint* paint) = 0;
Derek Sollenbergercae05e02014-07-24 15:22:13 -0400165
166 // Text
Tom Hudson8dfaa492014-12-09 15:03:44 -0500167 /**
168 * drawText: count is of glyphs
Chris Craika1717272015-11-19 13:02:43 -0800169 * totalAdvance: used to define width of text decorations (underlines, strikethroughs).
Tom Hudson8dfaa492014-12-09 15:03:44 -0500170 */
171 virtual void drawText(const uint16_t* glyphs, const float* positions, int count,
Derek Sollenbergercae05e02014-07-24 15:22:13 -0400172 const SkPaint& paint, float x, float y,
Tom Hudson8dfaa492014-12-09 15:03:44 -0500173 float boundsLeft, float boundsTop, float boundsRight, float boundsBottom,
174 float totalAdvance) = 0;
Tom Hudson8dfaa492014-12-09 15:03:44 -0500175 /** drawTextOnPath: count is of glyphs */
Derek Sollenbergercae05e02014-07-24 15:22:13 -0400176 virtual void drawTextOnPath(const uint16_t* glyphs, int count, const SkPath& path,
177 float hOffset, float vOffset, const SkPaint& paint) = 0;
178
Tom Hudson8dfaa492014-12-09 15:03:44 -0500179 /**
Derek Sollenbergercae05e02014-07-24 15:22:13 -0400180 * Specifies if the positions passed to ::drawText are absolute or relative
181 * to the (x,y) value provided.
182 *
183 * If true the (x,y) values are ignored. Otherwise, those (x,y) values need
184 * to be added to each glyph's position to get its absolute position.
185 */
186 virtual bool drawTextAbsolutePos() const = 0;
Chris Craika1717272015-11-19 13:02:43 -0800187
188protected:
189 void drawTextDecorations(float x, float y, float length, const SkPaint& paint);
Derek Sollenbergercae05e02014-07-24 15:22:13 -0400190};
191
192}; // namespace android
193#endif // ANDROID_GRAPHICS_CANVAS_H