blob: 3f57873b59f9af4cafcf9e4d7eda24dfb2815a7d [file] [log] [blame]
Chris Craikb4589422013-12-26 15:13:13 -08001/*
2 * Copyright (C) 2013 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_HWUI_RENDERER_H
18#define ANDROID_HWUI_RENDERER_H
19
Chris Craik14e51302013-12-30 15:32:54 -080020#include <SkRegion.h>
21
22#include <utils/String8.h>
23
Chris Craikb4589422013-12-26 15:13:13 -080024#include "AssetAtlas.h"
25#include "SkPaint.h"
26
27namespace android {
Chris Craik14e51302013-12-30 15:32:54 -080028
29class Functor;
Chris Craik564acf72014-01-02 16:46:18 -080030struct Res_png_9patch;
Chris Craik14e51302013-12-30 15:32:54 -080031
Chris Craikb4589422013-12-26 15:13:13 -080032namespace uirenderer {
33
34class DisplayList;
35class Layer;
36class Matrix4;
37class SkiaColorFilter;
38class SkiaShader;
39class Patch;
40
41enum DrawOpMode {
42 kDrawOpMode_Immediate,
43 kDrawOpMode_Defer,
44 kDrawOpMode_Flush
45};
46
47/**
48 * Hwui's abstract version of Canvas.
49 *
50 * Provides methods for frame state operations, as well as the SkCanvas style transform/clip state,
51 * and varied drawing operations.
52 *
53 * Should at some point interact with native SkCanvas.
54 */
55class ANDROID_API Renderer {
56public:
57 virtual ~Renderer() {}
58
59 /**
60 * Sets the name of this renderer. The name is optional and empty by default, for debugging
61 * purposes only. If the pointer is null the name is set to the empty string.
62 */
63 void setName(const char * name) {
64 if (name) {
65 mName.setTo(name);
66 } else {
67 mName.clear();
68 }
69 }
70
71 /**
72 * Returns the name of this renderer as UTF8 string.
73 * The returned pointer is never null.
74 */
75 const char* getName() const {
76 return mName.string();
77 }
78
79 /**
80 * Indicates whether this renderer is recording drawing commands for later playback.
81 * If this method returns true, the drawing commands are deferred.
82 */
83 virtual bool isRecording() const {
84 return false;
85 }
86
87 /**
88 * Safely retrieves the mode from the specified xfermode. If the specified
89 * xfermode is null, the mode is assumed to be SkXfermode::kSrcOver_Mode.
90 */
91 static inline SkXfermode::Mode getXfermode(SkXfermode* mode) {
92 SkXfermode::Mode resultMode;
93 if (!SkXfermode::AsMode(mode, &resultMode)) {
94 resultMode = SkXfermode::kSrcOver_Mode;
95 }
96 return resultMode;
97 }
98
99// ----------------------------------------------------------------------------
100// Frame state operations
101// ----------------------------------------------------------------------------
102 /**
103 * Sets the dimension of the underlying drawing surface. This method must
104 * be called at least once every time the drawing surface changes size.
105 *
106 * @param width The width in pixels of the underlysing surface
107 * @param height The height in pixels of the underlysing surface
108 */
109 virtual void setViewport(int width, int height) = 0;
110
111 /**
112 * Prepares the renderer to draw a frame. This method must be invoked
113 * at the beginning of each frame. When this method is invoked, the
114 * entire drawing surface is assumed to be redrawn.
115 *
116 * @param opaque If true, the target surface is considered opaque
117 * and will not be cleared. If false, the target surface
118 * will be cleared
119 */
120 virtual status_t prepare(bool opaque) = 0;
121
122 /**
123 * Prepares the renderer to draw a frame. This method must be invoked
124 * at the beginning of each frame. Only the specified rectangle of the
125 * frame is assumed to be dirty. A clip will automatically be set to
126 * the specified rectangle.
127 *
128 * @param left The left coordinate of the dirty rectangle
129 * @param top The top coordinate of the dirty rectangle
130 * @param right The right coordinate of the dirty rectangle
131 * @param bottom The bottom coordinate of the dirty rectangle
132 * @param opaque If true, the target surface is considered opaque
133 * and will not be cleared. If false, the target surface
134 * will be cleared in the specified dirty rectangle
135 */
136 virtual status_t prepareDirty(float left, float top, float right, float bottom,
137 bool opaque) = 0;
138
139 /**
140 * Indicates the end of a frame. This method must be invoked whenever
141 * the caller is done rendering a frame.
142 */
143 virtual void finish() = 0;
144
145 /**
146 * This method must be invoked before handing control over to a draw functor.
147 * See callDrawGLFunction() for instance.
148 *
149 * This command must not be recorded inside display lists.
150 */
151 virtual void interrupt() = 0;
152
153 /**
154 * This method must be invoked after getting control back from a draw functor.
155 *
156 * This command must not be recorded inside display lists.
157 */
158 virtual void resume() = 0;
159
160// ----------------------------------------------------------------------------
161// Canvas state operations
162// ----------------------------------------------------------------------------
Chris Craik14e51302013-12-30 15:32:54 -0800163 // Save (layer)
Chris Craikb4589422013-12-26 15:13:13 -0800164 virtual int getSaveCount() const = 0;
Chris Craikb4589422013-12-26 15:13:13 -0800165 virtual int save(int flags) = 0;
166 virtual void restore() = 0;
167 virtual void restoreToCount(int saveCount) = 0;
168
169 int saveLayer(float left, float top, float right, float bottom,
170 const SkPaint* paint, int flags) {
171 SkXfermode::Mode mode = SkXfermode::kSrcOver_Mode;
172 int alpha = 255;
173 if (paint) {
174 mode = getXfermode(paint->getXfermode());
175 alpha = paint->getAlpha();
176 }
177 return saveLayer(left, top, right, bottom, alpha, mode, flags);
178 }
179 int saveLayerAlpha(float left, float top, float right, float bottom,
180 int alpha, int flags) {
181 return saveLayer(left, top, right, bottom, alpha, SkXfermode::kSrcOver_Mode, flags);
182 }
183 virtual int saveLayer(float left, float top, float right, float bottom,
184 int alpha, SkXfermode::Mode mode, int flags) = 0;
185
186 // Matrix
Chris Craik14e51302013-12-30 15:32:54 -0800187 virtual void getMatrix(SkMatrix* outMatrix) const = 0;
Chris Craikb4589422013-12-26 15:13:13 -0800188 virtual void translate(float dx, float dy, float dz = 0.0f) = 0;
189 virtual void rotate(float degrees) = 0;
190 virtual void scale(float sx, float sy) = 0;
191 virtual void skew(float sx, float sy) = 0;
192
193 virtual void setMatrix(SkMatrix* matrix) = 0;
194 virtual void concatMatrix(SkMatrix* matrix) = 0;
Chris Craikb4589422013-12-26 15:13:13 -0800195
Chris Craik14e51302013-12-30 15:32:54 -0800196 // clip
197 virtual const Rect& getClipBounds() const = 0;
198 virtual bool quickRejectConservative(float left, float top,
199 float right, float bottom) const = 0;
Chris Craikb4589422013-12-26 15:13:13 -0800200 virtual bool clipRect(float left, float top, float right, float bottom, SkRegion::Op op) = 0;
201 virtual bool clipPath(SkPath* path, SkRegion::Op op) = 0;
202 virtual bool clipRegion(SkRegion* region, SkRegion::Op op) = 0;
203
204 // Misc - should be implemented with SkPaint inspection
205 virtual void resetShader() = 0;
206 virtual void setupShader(SkiaShader* shader) = 0;
207
208 virtual void resetColorFilter() = 0;
209 virtual void setupColorFilter(SkiaColorFilter* filter) = 0;
210
211 virtual void resetShadow() = 0;
212 virtual void setupShadow(float radius, float dx, float dy, int color) = 0;
213
214 virtual void resetPaintFilter() = 0;
215 virtual void setupPaintFilter(int clearBits, int setBits) = 0;
216
217// ----------------------------------------------------------------------------
218// Canvas draw operations
219// ----------------------------------------------------------------------------
220 virtual status_t drawColor(int color, SkXfermode::Mode mode) = 0;
221
222 // Bitmap-based
223 virtual status_t drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) = 0;
224 virtual status_t drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) = 0;
225 virtual status_t drawBitmap(SkBitmap* bitmap, float srcLeft, float srcTop,
226 float srcRight, float srcBottom, float dstLeft, float dstTop,
227 float dstRight, float dstBottom, SkPaint* paint) = 0;
228 virtual status_t drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) = 0;
229 virtual status_t drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
230 float* vertices, int* colors, SkPaint* paint) = 0;
231 virtual status_t drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
232 float left, float top, float right, float bottom, SkPaint* paint) = 0;
233
234 // Shapes
235 virtual status_t drawRect(float left, float top, float right, float bottom, SkPaint* paint) = 0;
236 virtual status_t drawRects(const float* rects, int count, SkPaint* paint) = 0;
237 virtual status_t drawRoundRect(float left, float top, float right, float bottom,
238 float rx, float ry, SkPaint* paint) = 0;
239 virtual status_t drawCircle(float x, float y, float radius, SkPaint* paint) = 0;
240 virtual status_t drawOval(float left, float top, float right, float bottom, SkPaint* paint) = 0;
241 virtual status_t drawArc(float left, float top, float right, float bottom,
242 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) = 0;
243 virtual status_t drawPath(SkPath* path, SkPaint* paint) = 0;
244 virtual status_t drawLines(float* points, int count, SkPaint* paint) = 0;
245 virtual status_t drawPoints(float* points, int count, SkPaint* paint) = 0;
246
247 // Text
248 virtual status_t drawText(const char* text, int bytesCount, int count, float x, float y,
249 const float* positions, SkPaint* paint, float totalAdvance, const Rect& bounds,
250 DrawOpMode drawOpMode = kDrawOpMode_Immediate) = 0;
251 virtual status_t drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
252 float hOffset, float vOffset, SkPaint* paint) = 0;
253 virtual status_t drawPosText(const char* text, int bytesCount, int count,
254 const float* positions, SkPaint* paint) = 0;
255
256// ----------------------------------------------------------------------------
257// Canvas draw operations - special
258// ----------------------------------------------------------------------------
259 virtual status_t drawLayer(Layer* layer, float x, float y) = 0;
260 virtual status_t drawDisplayList(DisplayList* displayList, Rect& dirty,
261 int32_t replayFlags) = 0;
262
263 // TODO: rename for consistency
264 virtual status_t callDrawGLFunction(Functor* functor, Rect& dirty) = 0;
265
266private:
267 // Optional name of the renderer
268 String8 mName;
269}; // class Renderer
270
271}; // namespace uirenderer
272}; // namespace android
273
274#endif // ANDROID_HWUI_RENDERER_H