blob: aa24673c4e30183d9b17e8a64f054c509da3dc4a [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
22#include <SkBitmap.h>
23#include <SkCanvas.h>
24#include <SkMatrix.h>
Derek Sollenbergercae05e02014-07-24 15:22:13 -040025
26namespace android {
27
John Reck849911a2015-01-20 07:51:14 -080028class ANDROID_API Canvas {
Derek Sollenbergercae05e02014-07-24 15:22:13 -040029public:
30 virtual ~Canvas() {};
31
John Reckc294d122015-04-13 15:20:29 -070032 static Canvas* create_canvas(const SkBitmap& bitmap);
Leon Scroggins III18981292014-12-17 11:30:31 -050033
34 /**
35 * Create a new Canvas object which delegates to an SkCanvas.
36 *
37 * @param skiaCanvas Must not be NULL. All drawing calls will be
38 * delegated to this object. This function will call ref() on the
39 * SkCanvas, and the returned Canvas will unref() it upon
40 * destruction.
41 * @return new Canvas object. Will not return NULL.
42 */
Derek Sollenbergercae05e02014-07-24 15:22:13 -040043 static Canvas* create_canvas(SkCanvas* skiaCanvas);
44
Derek Sollenberger1db141f2014-12-16 08:37:20 -050045 /**
46 * Provides a Skia SkCanvas interface that acts as a proxy to this Canvas.
47 * It is useful for testing and clients (e.g. Picture/Movie) that expect to
48 * draw their contents into an SkCanvas.
49 *
50 * Further, the returned SkCanvas should NOT be unref'd and is valid until
51 * this canvas is destroyed or a new bitmap is set.
52 */
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050053 virtual SkCanvas* asSkCanvas() = 0;
Derek Sollenbergercae05e02014-07-24 15:22:13 -040054
John Reckc294d122015-04-13 15:20:29 -070055 virtual void setBitmap(const SkBitmap& bitmap) = 0;
Derek Sollenbergercae05e02014-07-24 15:22:13 -040056
57 virtual bool isOpaque() = 0;
58 virtual int width() = 0;
59 virtual int height() = 0;
60
61// ----------------------------------------------------------------------------
62// Canvas state operations
63// ----------------------------------------------------------------------------
64 // Save (layer)
65 virtual int getSaveCount() const = 0;
66 virtual int save(SkCanvas::SaveFlags flags) = 0;
67 virtual void restore() = 0;
68 virtual void restoreToCount(int saveCount) = 0;
69
70 virtual int saveLayer(float left, float top, float right, float bottom,
71 const SkPaint* paint, SkCanvas::SaveFlags flags) = 0;
72 virtual int saveLayerAlpha(float left, float top, float right, float bottom,
73 int alpha, SkCanvas::SaveFlags flags) = 0;
74
75 // Matrix
76 virtual void getMatrix(SkMatrix* outMatrix) const = 0;
77 virtual void setMatrix(const SkMatrix& matrix) = 0;
78
79 virtual void concat(const SkMatrix& matrix) = 0;
80 virtual void rotate(float degrees) = 0;
81 virtual void scale(float sx, float sy) = 0;
82 virtual void skew(float sx, float sy) = 0;
83 virtual void translate(float dx, float dy) = 0;
84
85 // clip
86 virtual bool getClipBounds(SkRect* outRect) const = 0;
87 virtual bool quickRejectRect(float left, float top, float right, float bottom) const = 0;
88 virtual bool quickRejectPath(const SkPath& path) const = 0;
89
John Reckc294d122015-04-13 15:20:29 -070090 virtual bool clipRect(float left, float top, float right, float bottom,
91 SkRegion::Op op = SkRegion::kIntersect_Op) = 0;
Derek Sollenbergercae05e02014-07-24 15:22:13 -040092 virtual bool clipPath(const SkPath* path, SkRegion::Op op) = 0;
93 virtual bool clipRegion(const SkRegion* region, SkRegion::Op op) = 0;
94
95 // filters
96 virtual SkDrawFilter* getDrawFilter() = 0;
97 virtual void setDrawFilter(SkDrawFilter* drawFilter) = 0;
98
99// ----------------------------------------------------------------------------
100// Canvas draw operations
101// ----------------------------------------------------------------------------
102 virtual void drawColor(int color, SkXfermode::Mode mode) = 0;
103 virtual void drawPaint(const SkPaint& paint) = 0;
104
105 // Geometry
106 virtual void drawPoint(float x, float y, const SkPaint& paint) = 0;
107 virtual void drawPoints(const float* points, int count, const SkPaint& paint) = 0;
108 virtual void drawLine(float startX, float startY, float stopX, float stopY,
109 const SkPaint& paint) = 0;
110 virtual void drawLines(const float* points, int count, const SkPaint& paint) = 0;
111 virtual void drawRect(float left, float top, float right, float bottom,
112 const SkPaint& paint) = 0;
113 virtual void drawRoundRect(float left, float top, float right, float bottom,
114 float rx, float ry, const SkPaint& paint) = 0;
115 virtual void drawCircle(float x, float y, float radius, const SkPaint& paint) = 0;
116 virtual void drawOval(float left, float top, float right, float bottom,
117 const SkPaint& paint) = 0;
118 virtual void drawArc(float left, float top, float right, float bottom,
119 float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) = 0;
120 virtual void drawPath(const SkPath& path, const SkPaint& paint) = 0;
121 virtual void drawVertices(SkCanvas::VertexMode vertexMode, int vertexCount,
122 const float* verts, const float* tex, const int* colors,
123 const uint16_t* indices, int indexCount, const SkPaint& paint) = 0;
124
125 // Bitmap-based
126 virtual void drawBitmap(const SkBitmap& bitmap, float left, float top,
127 const SkPaint* paint) = 0;
128 virtual void drawBitmap(const SkBitmap& bitmap, const SkMatrix& matrix,
129 const SkPaint* paint) = 0;
130 virtual void drawBitmap(const SkBitmap& bitmap, float srcLeft, float srcTop,
131 float srcRight, float srcBottom, float dstLeft, float dstTop,
132 float dstRight, float dstBottom, const SkPaint* paint) = 0;
133 virtual void drawBitmapMesh(const SkBitmap& bitmap, int meshWidth, int meshHeight,
134 const float* vertices, const int* colors, const SkPaint* paint) = 0;
135
136 // Text
Tom Hudson8dfaa492014-12-09 15:03:44 -0500137 /**
138 * drawText: count is of glyphs
139 * totalAdvance is ignored in software renderering, used by hardware renderer for
140 * text decorations (underlines, strikethroughs).
141 */
142 virtual void drawText(const uint16_t* glyphs, const float* positions, int count,
Derek Sollenbergercae05e02014-07-24 15:22:13 -0400143 const SkPaint& paint, float x, float y,
Tom Hudson8dfaa492014-12-09 15:03:44 -0500144 float boundsLeft, float boundsTop, float boundsRight, float boundsBottom,
145 float totalAdvance) = 0;
146 /** drawPosText: count is of UTF16 characters, posCount is floats (2 * glyphs) */
Derek Sollenbergercae05e02014-07-24 15:22:13 -0400147 virtual void drawPosText(const uint16_t* text, const float* positions, int count,
148 int posCount, const SkPaint& paint) = 0;
Tom Hudson8dfaa492014-12-09 15:03:44 -0500149 /** drawTextOnPath: count is of glyphs */
Derek Sollenbergercae05e02014-07-24 15:22:13 -0400150 virtual void drawTextOnPath(const uint16_t* glyphs, int count, const SkPath& path,
151 float hOffset, float vOffset, const SkPaint& paint) = 0;
152
Tom Hudson8dfaa492014-12-09 15:03:44 -0500153 /**
Derek Sollenbergercae05e02014-07-24 15:22:13 -0400154 * Specifies if the positions passed to ::drawText are absolute or relative
155 * to the (x,y) value provided.
156 *
157 * If true the (x,y) values are ignored. Otherwise, those (x,y) values need
158 * to be added to each glyph's position to get its absolute position.
159 */
160 virtual bool drawTextAbsolutePos() const = 0;
161};
162
163}; // namespace android
164#endif // ANDROID_GRAPHICS_CANVAS_H