blob: a2f8c0585144f44852ca15a34e399602bbe09c40 [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 Craikc5b5f052014-10-01 16:40:16 -070020#include <SkColorFilter.h>
21#include <SkPaint.h>
Chris Craik14e51302013-12-30 15:32:54 -080022#include <SkRegion.h>
23
24#include <utils/String8.h>
25
Chris Craikb4589422013-12-26 15:13:13 -080026#include "AssetAtlas.h"
Chris Craikb4589422013-12-26 15:13:13 -080027
28namespace android {
Chris Craik14e51302013-12-30 15:32:54 -080029
30class Functor;
Chris Craik564acf72014-01-02 16:46:18 -080031struct Res_png_9patch;
Chris Craik14e51302013-12-30 15:32:54 -080032
Chris Craikb4589422013-12-26 15:13:13 -080033namespace uirenderer {
34
John Recke18264b2014-03-12 13:56:30 -070035class RenderNode;
Chris Craikb4589422013-12-26 15:13:13 -080036class Layer;
37class Matrix4;
38class SkiaColorFilter;
Chris Craikb4589422013-12-26 15:13:13 -080039class 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 /**
Chris Craikb4589422013-12-26 15:13:13 -080060 * Safely retrieves the mode from the specified xfermode. If the specified
61 * xfermode is null, the mode is assumed to be SkXfermode::kSrcOver_Mode.
62 */
63 static inline SkXfermode::Mode getXfermode(SkXfermode* mode) {
64 SkXfermode::Mode resultMode;
65 if (!SkXfermode::AsMode(mode, &resultMode)) {
66 resultMode = SkXfermode::kSrcOver_Mode;
67 }
68 return resultMode;
69 }
70
Chris Craik947eabf2014-08-19 10:21:12 -070071 // TODO: move to a method on android:Paint
72 static inline bool paintWillNotDraw(const SkPaint& paint) {
73 return paint.getAlpha() == 0
Chris Craik73821c82014-09-16 17:32:13 -070074 && !paint.getColorFilter()
Chris Craikbc341912014-09-22 18:17:39 -070075 && getXfermode(paint.getXfermode()) == SkXfermode::kSrcOver_Mode;
Chris Craik947eabf2014-08-19 10:21:12 -070076 }
77
78 // TODO: move to a method on android:Paint
79 static inline bool paintWillNotDrawText(const SkPaint& paint) {
80 return paint.getAlpha() == 0
81 && paint.getLooper() == NULL
Chris Craik73821c82014-09-16 17:32:13 -070082 && !paint.getColorFilter()
Chris Craik947eabf2014-08-19 10:21:12 -070083 && getXfermode(paint.getXfermode()) == SkXfermode::kSrcOver_Mode;
84 }
Chris Craikc5b5f052014-10-01 16:40:16 -070085
86 static bool isBlendedColorFilter(const SkColorFilter* filter) {
87 if (filter == NULL) {
88 return false;
89 }
90 return (filter->getFlags() & SkColorFilter::kAlphaUnchanged_Flag) == 0;
91 }
92
Chris Craikb4589422013-12-26 15:13:13 -080093// ----------------------------------------------------------------------------
94// Frame state operations
95// ----------------------------------------------------------------------------
96 /**
97 * Sets the dimension of the underlying drawing surface. This method must
98 * be called at least once every time the drawing surface changes size.
99 *
100 * @param width The width in pixels of the underlysing surface
101 * @param height The height in pixels of the underlysing surface
102 */
103 virtual void setViewport(int width, int height) = 0;
104
105 /**
106 * Prepares the renderer to draw a frame. This method must be invoked
107 * at the beginning of each frame. When this method is invoked, the
108 * entire drawing surface is assumed to be redrawn.
109 *
110 * @param opaque If true, the target surface is considered opaque
111 * and will not be cleared. If false, the target surface
112 * will be cleared
113 */
114 virtual status_t prepare(bool opaque) = 0;
115
116 /**
117 * Prepares the renderer to draw a frame. This method must be invoked
118 * at the beginning of each frame. Only the specified rectangle of the
119 * frame is assumed to be dirty. A clip will automatically be set to
120 * the specified rectangle.
121 *
122 * @param left The left coordinate of the dirty rectangle
123 * @param top The top coordinate of the dirty rectangle
124 * @param right The right coordinate of the dirty rectangle
125 * @param bottom The bottom coordinate of the dirty rectangle
126 * @param opaque If true, the target surface is considered opaque
127 * and will not be cleared. If false, the target surface
128 * will be cleared in the specified dirty rectangle
129 */
130 virtual status_t prepareDirty(float left, float top, float right, float bottom,
131 bool opaque) = 0;
132
133 /**
134 * Indicates the end of a frame. This method must be invoked whenever
135 * the caller is done rendering a frame.
136 */
137 virtual void finish() = 0;
138
Chris Craikb4589422013-12-26 15:13:13 -0800139// ----------------------------------------------------------------------------
140// Canvas state operations
141// ----------------------------------------------------------------------------
Chris Craik14e51302013-12-30 15:32:54 -0800142 // Save (layer)
Chris Craikb4589422013-12-26 15:13:13 -0800143 virtual int getSaveCount() const = 0;
Chris Craikb4589422013-12-26 15:13:13 -0800144 virtual int save(int flags) = 0;
145 virtual void restore() = 0;
146 virtual void restoreToCount(int saveCount) = 0;
147
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500148 virtual int saveLayer(float left, float top, float right, float bottom,
149 const SkPaint* paint, int flags) = 0;
150
Chris Craikb4589422013-12-26 15:13:13 -0800151 int saveLayerAlpha(float left, float top, float right, float bottom,
152 int alpha, int flags) {
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500153 SkPaint paint;
154 paint.setAlpha(alpha);
155 return saveLayer(left, top, right, bottom, &paint, flags);
Chris Craikb4589422013-12-26 15:13:13 -0800156 }
Chris Craikb4589422013-12-26 15:13:13 -0800157
158 // Matrix
Chris Craik14e51302013-12-30 15:32:54 -0800159 virtual void getMatrix(SkMatrix* outMatrix) const = 0;
Chris Craikb4589422013-12-26 15:13:13 -0800160 virtual void translate(float dx, float dy, float dz = 0.0f) = 0;
161 virtual void rotate(float degrees) = 0;
162 virtual void scale(float sx, float sy) = 0;
163 virtual void skew(float sx, float sy) = 0;
164
Derek Sollenberger13908822013-12-10 12:28:58 -0500165 virtual void setMatrix(const SkMatrix& matrix) = 0;
166 virtual void concatMatrix(const SkMatrix& matrix) = 0;
Chris Craikb4589422013-12-26 15:13:13 -0800167
Chris Craik14e51302013-12-30 15:32:54 -0800168 // clip
Chris Craik3f085422014-04-15 16:18:08 -0700169 virtual const Rect& getLocalClipBounds() const = 0;
Chris Craik14e51302013-12-30 15:32:54 -0800170 virtual bool quickRejectConservative(float left, float top,
171 float right, float bottom) const = 0;
Chris Craikb4589422013-12-26 15:13:13 -0800172 virtual bool clipRect(float left, float top, float right, float bottom, SkRegion::Op op) = 0;
Chris Craikd218a922014-01-02 17:13:34 -0800173 virtual bool clipPath(const SkPath* path, SkRegion::Op op) = 0;
174 virtual bool clipRegion(const SkRegion* region, SkRegion::Op op) = 0;
Chris Craikb4589422013-12-26 15:13:13 -0800175
176 // Misc - should be implemented with SkPaint inspection
Chris Craikb4589422013-12-26 15:13:13 -0800177 virtual void resetPaintFilter() = 0;
178 virtual void setupPaintFilter(int clearBits, int setBits) = 0;
179
180// ----------------------------------------------------------------------------
181// Canvas draw operations
182// ----------------------------------------------------------------------------
183 virtual status_t drawColor(int color, SkXfermode::Mode mode) = 0;
184
185 // Bitmap-based
Chris Craik79647502014-08-06 13:42:24 -0700186 virtual status_t drawBitmap(const SkBitmap* bitmap, const SkPaint* paint) = 0;
Chris Craikd218a922014-01-02 17:13:34 -0800187 virtual status_t drawBitmap(const SkBitmap* bitmap, float srcLeft, float srcTop,
Chris Craikb4589422013-12-26 15:13:13 -0800188 float srcRight, float srcBottom, float dstLeft, float dstTop,
Chris Craikd218a922014-01-02 17:13:34 -0800189 float dstRight, float dstBottom, const SkPaint* paint) = 0;
Chris Craik79647502014-08-06 13:42:24 -0700190 virtual status_t drawBitmapData(const SkBitmap* bitmap, const SkPaint* paint) = 0;
Chris Craikd218a922014-01-02 17:13:34 -0800191 virtual status_t drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight,
192 const float* vertices, const int* colors, const SkPaint* paint) = 0;
193 virtual status_t drawPatch(const SkBitmap* bitmap, const Res_png_9patch* patch,
194 float left, float top, float right, float bottom, const SkPaint* paint) = 0;
Chris Craikb4589422013-12-26 15:13:13 -0800195
196 // Shapes
Chris Craikd218a922014-01-02 17:13:34 -0800197 virtual status_t drawRect(float left, float top, float right, float bottom,
198 const SkPaint* paint) = 0;
199 virtual status_t drawRects(const float* rects, int count, const SkPaint* paint) = 0;
Chris Craikb4589422013-12-26 15:13:13 -0800200 virtual status_t drawRoundRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -0800201 float rx, float ry, const SkPaint* paint) = 0;
202 virtual status_t drawCircle(float x, float y, float radius, const SkPaint* paint) = 0;
203 virtual status_t drawOval(float left, float top, float right, float bottom,
204 const SkPaint* paint) = 0;
Chris Craikb4589422013-12-26 15:13:13 -0800205 virtual status_t drawArc(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -0800206 float startAngle, float sweepAngle, bool useCenter, const SkPaint* paint) = 0;
207 virtual status_t drawPath(const SkPath* path, const SkPaint* paint) = 0;
208 virtual status_t drawLines(const float* points, int count, const SkPaint* paint) = 0;
209 virtual status_t drawPoints(const float* points, int count, const SkPaint* paint) = 0;
Chris Craikb4589422013-12-26 15:13:13 -0800210
211 // Text
212 virtual status_t drawText(const char* text, int bytesCount, int count, float x, float y,
Chris Craikd218a922014-01-02 17:13:34 -0800213 const float* positions, const SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craikb4589422013-12-26 15:13:13 -0800214 DrawOpMode drawOpMode = kDrawOpMode_Immediate) = 0;
Chris Craikd218a922014-01-02 17:13:34 -0800215 virtual status_t drawTextOnPath(const char* text, int bytesCount, int count, const SkPath* path,
216 float hOffset, float vOffset, const SkPaint* paint) = 0;
Chris Craikb4589422013-12-26 15:13:13 -0800217 virtual status_t drawPosText(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -0800218 const float* positions, const SkPaint* paint) = 0;
Chris Craikb4589422013-12-26 15:13:13 -0800219
220// ----------------------------------------------------------------------------
221// Canvas draw operations - special
222// ----------------------------------------------------------------------------
223 virtual status_t drawLayer(Layer* layer, float x, float y) = 0;
Chris Craika7090e02014-06-20 16:01:00 -0700224 virtual status_t drawRenderNode(RenderNode* renderNode, Rect& dirty,
Chris Craikb4589422013-12-26 15:13:13 -0800225 int32_t replayFlags) = 0;
226
227 // TODO: rename for consistency
228 virtual status_t callDrawGLFunction(Functor* functor, Rect& dirty) = 0;
Chris Craikb4589422013-12-26 15:13:13 -0800229}; // class Renderer
230
231}; // namespace uirenderer
232}; // namespace android
233
234#endif // ANDROID_HWUI_RENDERER_H