blob: b585a2799cadb071e4157e99e94c6805f9ea5087 [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
John Reck849911a2015-01-20 07:51:14 -080030class ANDROID_API Canvas {
Derek Sollenbergercae05e02014-07-24 15:22:13 -040031public:
32 virtual ~Canvas() {};
33
John Reckc1b33d62015-04-22 09:04:45 -070034 static Canvas* create_canvas(const SkBitmap& bitmap);
Leon Scroggins III18981292014-12-17 11:30:31 -050035
36 /**
37 * Create a new Canvas object which delegates to an SkCanvas.
38 *
39 * @param skiaCanvas Must not be NULL. All drawing calls will be
40 * delegated to this object. This function will call ref() on the
41 * SkCanvas, and the returned Canvas will unref() it upon
42 * destruction.
43 * @return new Canvas object. Will not return NULL.
44 */
Derek Sollenbergercae05e02014-07-24 15:22:13 -040045 static Canvas* create_canvas(SkCanvas* skiaCanvas);
46
Derek Sollenberger1db141f2014-12-16 08:37:20 -050047 /**
48 * Provides a Skia SkCanvas interface that acts as a proxy to this Canvas.
49 * It is useful for testing and clients (e.g. Picture/Movie) that expect to
50 * draw their contents into an SkCanvas.
51 *
Tom Hudson90fb1f62015-06-24 09:13:50 -040052 * The SkCanvas returned is *only* valid until another Canvas call is made
53 * that would change state (e.g. matrix or clip). Clients of asSkCanvas()
54 * are responsible for *not* persisting this pointer.
55 *
Derek Sollenberger1db141f2014-12-16 08:37:20 -050056 * Further, the returned SkCanvas should NOT be unref'd and is valid until
57 * this canvas is destroyed or a new bitmap is set.
58 */
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050059 virtual SkCanvas* asSkCanvas() = 0;
Derek Sollenbergercae05e02014-07-24 15:22:13 -040060
John Reckc1b33d62015-04-22 09:04:45 -070061 virtual void setBitmap(const SkBitmap& bitmap) = 0;
Derek Sollenbergercae05e02014-07-24 15:22:13 -040062
63 virtual bool isOpaque() = 0;
64 virtual int width() = 0;
65 virtual int height() = 0;
66
Derek Sollenberger6578a982015-07-13 13:24:29 -040067 virtual void setHighContrastText(bool highContrastText) = 0;
68 virtual bool isHighContrastText() = 0;
69
Derek Sollenbergercae05e02014-07-24 15:22:13 -040070// ----------------------------------------------------------------------------
71// Canvas state operations
72// ----------------------------------------------------------------------------
73 // Save (layer)
74 virtual int getSaveCount() const = 0;
75 virtual int save(SkCanvas::SaveFlags flags) = 0;
76 virtual void restore() = 0;
77 virtual void restoreToCount(int saveCount) = 0;
78
79 virtual int saveLayer(float left, float top, float right, float bottom,
80 const SkPaint* paint, SkCanvas::SaveFlags flags) = 0;
81 virtual int saveLayerAlpha(float left, float top, float right, float bottom,
82 int alpha, SkCanvas::SaveFlags flags) = 0;
83
84 // Matrix
85 virtual void getMatrix(SkMatrix* outMatrix) const = 0;
86 virtual void setMatrix(const SkMatrix& matrix) = 0;
87
88 virtual void concat(const SkMatrix& matrix) = 0;
89 virtual void rotate(float degrees) = 0;
90 virtual void scale(float sx, float sy) = 0;
91 virtual void skew(float sx, float sy) = 0;
92 virtual void translate(float dx, float dy) = 0;
93
94 // clip
95 virtual bool getClipBounds(SkRect* outRect) const = 0;
96 virtual bool quickRejectRect(float left, float top, float right, float bottom) const = 0;
97 virtual bool quickRejectPath(const SkPath& path) const = 0;
98
John Reckc1b33d62015-04-22 09:04:45 -070099 virtual bool clipRect(float left, float top, float right, float bottom,
100 SkRegion::Op op = SkRegion::kIntersect_Op) = 0;
Derek Sollenbergercae05e02014-07-24 15:22:13 -0400101 virtual bool clipPath(const SkPath* path, SkRegion::Op op) = 0;
102 virtual bool clipRegion(const SkRegion* region, SkRegion::Op op) = 0;
103
104 // filters
105 virtual SkDrawFilter* getDrawFilter() = 0;
106 virtual void setDrawFilter(SkDrawFilter* drawFilter) = 0;
107
108// ----------------------------------------------------------------------------
109// Canvas draw operations
110// ----------------------------------------------------------------------------
111 virtual void drawColor(int color, SkXfermode::Mode mode) = 0;
112 virtual void drawPaint(const SkPaint& paint) = 0;
113
114 // Geometry
115 virtual void drawPoint(float x, float y, const SkPaint& paint) = 0;
116 virtual void drawPoints(const float* points, int count, const SkPaint& paint) = 0;
117 virtual void drawLine(float startX, float startY, float stopX, float stopY,
118 const SkPaint& paint) = 0;
119 virtual void drawLines(const float* points, int count, const SkPaint& paint) = 0;
120 virtual void drawRect(float left, float top, float right, float bottom,
121 const SkPaint& paint) = 0;
Derek Sollenberger94394b32015-07-10 09:58:41 -0400122 virtual void drawRegion(const SkRegion& region, const SkPaint& paint) = 0;
Derek Sollenbergercae05e02014-07-24 15:22:13 -0400123 virtual void drawRoundRect(float left, float top, float right, float bottom,
124 float rx, float ry, const SkPaint& paint) = 0;
125 virtual void drawCircle(float x, float y, float radius, const SkPaint& paint) = 0;
126 virtual void drawOval(float left, float top, float right, float bottom,
127 const SkPaint& paint) = 0;
128 virtual void drawArc(float left, float top, float right, float bottom,
129 float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) = 0;
130 virtual void drawPath(const SkPath& path, const SkPaint& paint) = 0;
131 virtual void drawVertices(SkCanvas::VertexMode vertexMode, int vertexCount,
132 const float* verts, const float* tex, const int* colors,
133 const uint16_t* indices, int indexCount, const SkPaint& paint) = 0;
134
135 // Bitmap-based
136 virtual void drawBitmap(const SkBitmap& bitmap, float left, float top,
137 const SkPaint* paint) = 0;
138 virtual void drawBitmap(const SkBitmap& bitmap, const SkMatrix& matrix,
139 const SkPaint* paint) = 0;
140 virtual void drawBitmap(const SkBitmap& bitmap, float srcLeft, float srcTop,
141 float srcRight, float srcBottom, float dstLeft, float dstTop,
142 float dstRight, float dstBottom, const SkPaint* paint) = 0;
143 virtual void drawBitmapMesh(const SkBitmap& bitmap, int meshWidth, int meshHeight,
144 const float* vertices, const int* colors, const SkPaint* paint) = 0;
Derek Sollenberger4c5efe92015-07-10 13:56:39 -0400145 virtual void drawNinePatch(const SkBitmap& bitmap, const android::Res_png_9patch& chunk,
146 float dstLeft, float dstTop, float dstRight, float dstBottom,
147 const SkPaint* paint) = 0;
Derek Sollenbergercae05e02014-07-24 15:22:13 -0400148
149 // Text
Tom Hudson8dfaa492014-12-09 15:03:44 -0500150 /**
151 * drawText: count is of glyphs
Chris Craika1717272015-11-19 13:02:43 -0800152 * totalAdvance: used to define width of text decorations (underlines, strikethroughs).
Tom Hudson8dfaa492014-12-09 15:03:44 -0500153 */
154 virtual void drawText(const uint16_t* glyphs, const float* positions, int count,
Derek Sollenbergercae05e02014-07-24 15:22:13 -0400155 const SkPaint& paint, float x, float y,
Tom Hudson8dfaa492014-12-09 15:03:44 -0500156 float boundsLeft, float boundsTop, float boundsRight, float boundsBottom,
157 float totalAdvance) = 0;
Tom Hudson8dfaa492014-12-09 15:03:44 -0500158 /** drawTextOnPath: count is of glyphs */
Derek Sollenbergercae05e02014-07-24 15:22:13 -0400159 virtual void drawTextOnPath(const uint16_t* glyphs, int count, const SkPath& path,
160 float hOffset, float vOffset, const SkPaint& paint) = 0;
161
Tom Hudson8dfaa492014-12-09 15:03:44 -0500162 /**
Derek Sollenbergercae05e02014-07-24 15:22:13 -0400163 * Specifies if the positions passed to ::drawText are absolute or relative
164 * to the (x,y) value provided.
165 *
166 * If true the (x,y) values are ignored. Otherwise, those (x,y) values need
167 * to be added to each glyph's position to get its absolute position.
168 */
169 virtual bool drawTextAbsolutePos() const = 0;
Chris Craika1717272015-11-19 13:02:43 -0800170
171protected:
172 void drawTextDecorations(float x, float y, float length, const SkPaint& paint);
Derek Sollenbergercae05e02014-07-24 15:22:13 -0400173};
174
175}; // namespace android
176#endif // ANDROID_GRAPHICS_CANVAS_H