blob: ae79907f11339f55bec0ad9d8200507b775e3ef3 [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
20#include "SkBitmap.h"
21#include "SkCanvas.h"
22#include "SkMatrix.h"
23
24namespace android {
25
26class Canvas {
27public:
28 virtual ~Canvas() {};
29
30 static Canvas* create_canvas(SkBitmap* bitmap);
Leon Scroggins III18981292014-12-17 11:30:31 -050031
32 /**
33 * Create a new Canvas object which delegates to an SkCanvas.
34 *
35 * @param skiaCanvas Must not be NULL. All drawing calls will be
36 * delegated to this object. This function will call ref() on the
37 * SkCanvas, and the returned Canvas will unref() it upon
38 * destruction.
39 * @return new Canvas object. Will not return NULL.
40 */
Derek Sollenbergercae05e02014-07-24 15:22:13 -040041 static Canvas* create_canvas(SkCanvas* skiaCanvas);
42
43 // TODO: enable HWUI to either create similar canvas wrapper or subclass
44 // directly from Canvas
45 //static Canvas* create_canvas(uirenderer::Renderer* renderer);
46
47 // TODO: this is a temporary affordance until all necessary logic can be
48 // moved within this interface! Further, the return value should
49 // NOT be unref'd and is valid until this canvas is destroyed or a
50 // new bitmap is set.
51 virtual SkCanvas* getSkCanvas() = 0;
52
53 virtual void setBitmap(SkBitmap* bitmap, bool copyState) = 0;
54
55 virtual bool isOpaque() = 0;
56 virtual int width() = 0;
57 virtual int height() = 0;
58
59// ----------------------------------------------------------------------------
60// Canvas state operations
61// ----------------------------------------------------------------------------
62 // Save (layer)
63 virtual int getSaveCount() const = 0;
64 virtual int save(SkCanvas::SaveFlags flags) = 0;
65 virtual void restore() = 0;
66 virtual void restoreToCount(int saveCount) = 0;
67
68 virtual int saveLayer(float left, float top, float right, float bottom,
69 const SkPaint* paint, SkCanvas::SaveFlags flags) = 0;
70 virtual int saveLayerAlpha(float left, float top, float right, float bottom,
71 int alpha, SkCanvas::SaveFlags flags) = 0;
72
73 // Matrix
74 virtual void getMatrix(SkMatrix* outMatrix) const = 0;
75 virtual void setMatrix(const SkMatrix& matrix) = 0;
76
77 virtual void concat(const SkMatrix& matrix) = 0;
78 virtual void rotate(float degrees) = 0;
79 virtual void scale(float sx, float sy) = 0;
80 virtual void skew(float sx, float sy) = 0;
81 virtual void translate(float dx, float dy) = 0;
82
83 // clip
84 virtual bool getClipBounds(SkRect* outRect) const = 0;
85 virtual bool quickRejectRect(float left, float top, float right, float bottom) const = 0;
86 virtual bool quickRejectPath(const SkPath& path) const = 0;
87
88 virtual bool clipRect(float left, float top, float right, float bottom, SkRegion::Op op) = 0;
89 virtual bool clipPath(const SkPath* path, SkRegion::Op op) = 0;
90 virtual bool clipRegion(const SkRegion* region, SkRegion::Op op) = 0;
91
92 // filters
93 virtual SkDrawFilter* getDrawFilter() = 0;
94 virtual void setDrawFilter(SkDrawFilter* drawFilter) = 0;
95
96// ----------------------------------------------------------------------------
97// Canvas draw operations
98// ----------------------------------------------------------------------------
99 virtual void drawColor(int color, SkXfermode::Mode mode) = 0;
100 virtual void drawPaint(const SkPaint& paint) = 0;
101
102 // Geometry
103 virtual void drawPoint(float x, float y, const SkPaint& paint) = 0;
104 virtual void drawPoints(const float* points, int count, const SkPaint& paint) = 0;
105 virtual void drawLine(float startX, float startY, float stopX, float stopY,
106 const SkPaint& paint) = 0;
107 virtual void drawLines(const float* points, int count, const SkPaint& paint) = 0;
108 virtual void drawRect(float left, float top, float right, float bottom,
109 const SkPaint& paint) = 0;
110 virtual void drawRoundRect(float left, float top, float right, float bottom,
111 float rx, float ry, const SkPaint& paint) = 0;
112 virtual void drawCircle(float x, float y, float radius, const SkPaint& paint) = 0;
113 virtual void drawOval(float left, float top, float right, float bottom,
114 const SkPaint& paint) = 0;
115 virtual void drawArc(float left, float top, float right, float bottom,
116 float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) = 0;
117 virtual void drawPath(const SkPath& path, const SkPaint& paint) = 0;
118 virtual void drawVertices(SkCanvas::VertexMode vertexMode, int vertexCount,
119 const float* verts, const float* tex, const int* colors,
120 const uint16_t* indices, int indexCount, const SkPaint& paint) = 0;
121
122 // Bitmap-based
123 virtual void drawBitmap(const SkBitmap& bitmap, float left, float top,
124 const SkPaint* paint) = 0;
125 virtual void drawBitmap(const SkBitmap& bitmap, const SkMatrix& matrix,
126 const SkPaint* paint) = 0;
127 virtual void drawBitmap(const SkBitmap& bitmap, float srcLeft, float srcTop,
128 float srcRight, float srcBottom, float dstLeft, float dstTop,
129 float dstRight, float dstBottom, const SkPaint* paint) = 0;
130 virtual void drawBitmapMesh(const SkBitmap& bitmap, int meshWidth, int meshHeight,
131 const float* vertices, const int* colors, const SkPaint* paint) = 0;
132
133 // Text
Tom Hudson8dfaa492014-12-09 15:03:44 -0500134 /**
135 * drawText: count is of glyphs
136 * totalAdvance is ignored in software renderering, used by hardware renderer for
137 * text decorations (underlines, strikethroughs).
138 */
139 virtual void drawText(const uint16_t* glyphs, const float* positions, int count,
Derek Sollenbergercae05e02014-07-24 15:22:13 -0400140 const SkPaint& paint, float x, float y,
Tom Hudson8dfaa492014-12-09 15:03:44 -0500141 float boundsLeft, float boundsTop, float boundsRight, float boundsBottom,
142 float totalAdvance) = 0;
143 /** drawPosText: count is of UTF16 characters, posCount is floats (2 * glyphs) */
Derek Sollenbergercae05e02014-07-24 15:22:13 -0400144 virtual void drawPosText(const uint16_t* text, const float* positions, int count,
145 int posCount, const SkPaint& paint) = 0;
Tom Hudson8dfaa492014-12-09 15:03:44 -0500146 /** drawTextOnPath: count is of glyphs */
Derek Sollenbergercae05e02014-07-24 15:22:13 -0400147 virtual void drawTextOnPath(const uint16_t* glyphs, int count, const SkPath& path,
148 float hOffset, float vOffset, const SkPaint& paint) = 0;
149
Tom Hudson8dfaa492014-12-09 15:03:44 -0500150 /**
Derek Sollenbergercae05e02014-07-24 15:22:13 -0400151 * Specifies if the positions passed to ::drawText are absolute or relative
152 * to the (x,y) value provided.
153 *
154 * If true the (x,y) values are ignored. Otherwise, those (x,y) values need
155 * to be added to each glyph's position to get its absolute position.
156 */
157 virtual bool drawTextAbsolutePos() const = 0;
158};
159
160}; // namespace android
161#endif // ANDROID_GRAPHICS_CANVAS_H