blob: f5cd2668e19eaf26c8aac6807109c82c930ab2d2 [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
John Recke18264b2014-03-12 13:56:30 -070034class RenderNode;
Chris Craikb4589422013-12-26 15:13:13 -080035class Layer;
36class Matrix4;
37class SkiaColorFilter;
Chris Craikb4589422013-12-26 15:13:13 -080038class Patch;
39
40enum DrawOpMode {
41 kDrawOpMode_Immediate,
42 kDrawOpMode_Defer,
43 kDrawOpMode_Flush
44};
45
46/**
47 * Hwui's abstract version of Canvas.
48 *
49 * Provides methods for frame state operations, as well as the SkCanvas style transform/clip state,
50 * and varied drawing operations.
51 *
52 * Should at some point interact with native SkCanvas.
53 */
54class ANDROID_API Renderer {
55public:
56 virtual ~Renderer() {}
57
58 /**
Chris Craikb4589422013-12-26 15:13:13 -080059 * Safely retrieves the mode from the specified xfermode. If the specified
60 * xfermode is null, the mode is assumed to be SkXfermode::kSrcOver_Mode.
61 */
62 static inline SkXfermode::Mode getXfermode(SkXfermode* mode) {
63 SkXfermode::Mode resultMode;
64 if (!SkXfermode::AsMode(mode, &resultMode)) {
65 resultMode = SkXfermode::kSrcOver_Mode;
66 }
67 return resultMode;
68 }
69
70// ----------------------------------------------------------------------------
71// Frame state operations
72// ----------------------------------------------------------------------------
73 /**
74 * Sets the dimension of the underlying drawing surface. This method must
75 * be called at least once every time the drawing surface changes size.
76 *
77 * @param width The width in pixels of the underlysing surface
78 * @param height The height in pixels of the underlysing surface
79 */
80 virtual void setViewport(int width, int height) = 0;
81
82 /**
83 * Prepares the renderer to draw a frame. This method must be invoked
84 * at the beginning of each frame. When this method is invoked, the
85 * entire drawing surface is assumed to be redrawn.
86 *
87 * @param opaque If true, the target surface is considered opaque
88 * and will not be cleared. If false, the target surface
89 * will be cleared
90 */
91 virtual status_t prepare(bool opaque) = 0;
92
93 /**
94 * Prepares the renderer to draw a frame. This method must be invoked
95 * at the beginning of each frame. Only the specified rectangle of the
96 * frame is assumed to be dirty. A clip will automatically be set to
97 * the specified rectangle.
98 *
99 * @param left The left coordinate of the dirty rectangle
100 * @param top The top coordinate of the dirty rectangle
101 * @param right The right coordinate of the dirty rectangle
102 * @param bottom The bottom coordinate of the dirty rectangle
103 * @param opaque If true, the target surface is considered opaque
104 * and will not be cleared. If false, the target surface
105 * will be cleared in the specified dirty rectangle
106 */
107 virtual status_t prepareDirty(float left, float top, float right, float bottom,
108 bool opaque) = 0;
109
110 /**
111 * Indicates the end of a frame. This method must be invoked whenever
112 * the caller is done rendering a frame.
113 */
114 virtual void finish() = 0;
115
Chris Craikb4589422013-12-26 15:13:13 -0800116// ----------------------------------------------------------------------------
117// Canvas state operations
118// ----------------------------------------------------------------------------
Chris Craik14e51302013-12-30 15:32:54 -0800119 // Save (layer)
Chris Craikb4589422013-12-26 15:13:13 -0800120 virtual int getSaveCount() const = 0;
Chris Craikb4589422013-12-26 15:13:13 -0800121 virtual int save(int flags) = 0;
122 virtual void restore() = 0;
123 virtual void restoreToCount(int saveCount) = 0;
124
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500125 virtual int saveLayer(float left, float top, float right, float bottom,
126 const SkPaint* paint, int flags) = 0;
127
Chris Craikb4589422013-12-26 15:13:13 -0800128 int saveLayerAlpha(float left, float top, float right, float bottom,
129 int alpha, int flags) {
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500130 SkPaint paint;
131 paint.setAlpha(alpha);
132 return saveLayer(left, top, right, bottom, &paint, flags);
Chris Craikb4589422013-12-26 15:13:13 -0800133 }
Chris Craikb4589422013-12-26 15:13:13 -0800134
135 // Matrix
Chris Craik14e51302013-12-30 15:32:54 -0800136 virtual void getMatrix(SkMatrix* outMatrix) const = 0;
Chris Craikb4589422013-12-26 15:13:13 -0800137 virtual void translate(float dx, float dy, float dz = 0.0f) = 0;
138 virtual void rotate(float degrees) = 0;
139 virtual void scale(float sx, float sy) = 0;
140 virtual void skew(float sx, float sy) = 0;
141
Derek Sollenberger13908822013-12-10 12:28:58 -0500142 virtual void setMatrix(const SkMatrix& matrix) = 0;
143 virtual void concatMatrix(const SkMatrix& matrix) = 0;
Chris Craikb4589422013-12-26 15:13:13 -0800144
Chris Craik14e51302013-12-30 15:32:54 -0800145 // clip
Chris Craik3f0854292014-04-15 16:18:08 -0700146 virtual const Rect& getLocalClipBounds() const = 0;
Chris Craik14e51302013-12-30 15:32:54 -0800147 virtual bool quickRejectConservative(float left, float top,
148 float right, float bottom) const = 0;
Chris Craikb4589422013-12-26 15:13:13 -0800149 virtual bool clipRect(float left, float top, float right, float bottom, SkRegion::Op op) = 0;
Chris Craikd218a922014-01-02 17:13:34 -0800150 virtual bool clipPath(const SkPath* path, SkRegion::Op op) = 0;
151 virtual bool clipRegion(const SkRegion* region, SkRegion::Op op) = 0;
Chris Craikb4589422013-12-26 15:13:13 -0800152
153 // Misc - should be implemented with SkPaint inspection
Chris Craikb4589422013-12-26 15:13:13 -0800154 virtual void resetPaintFilter() = 0;
155 virtual void setupPaintFilter(int clearBits, int setBits) = 0;
156
157// ----------------------------------------------------------------------------
158// Canvas draw operations
159// ----------------------------------------------------------------------------
160 virtual status_t drawColor(int color, SkXfermode::Mode mode) = 0;
161
162 // Bitmap-based
Chris Craik79647502014-08-06 13:42:24 -0700163 virtual status_t drawBitmap(const SkBitmap* bitmap, const SkPaint* paint) = 0;
Chris Craikd218a922014-01-02 17:13:34 -0800164 virtual status_t drawBitmap(const SkBitmap* bitmap, float srcLeft, float srcTop,
Chris Craikb4589422013-12-26 15:13:13 -0800165 float srcRight, float srcBottom, float dstLeft, float dstTop,
Chris Craikd218a922014-01-02 17:13:34 -0800166 float dstRight, float dstBottom, const SkPaint* paint) = 0;
Chris Craik79647502014-08-06 13:42:24 -0700167 virtual status_t drawBitmapData(const SkBitmap* bitmap, const SkPaint* paint) = 0;
Chris Craikd218a922014-01-02 17:13:34 -0800168 virtual status_t drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight,
169 const float* vertices, const int* colors, const SkPaint* paint) = 0;
170 virtual status_t drawPatch(const SkBitmap* bitmap, const Res_png_9patch* patch,
171 float left, float top, float right, float bottom, const SkPaint* paint) = 0;
Chris Craikb4589422013-12-26 15:13:13 -0800172
173 // Shapes
Chris Craikd218a922014-01-02 17:13:34 -0800174 virtual status_t drawRect(float left, float top, float right, float bottom,
175 const SkPaint* paint) = 0;
176 virtual status_t drawRects(const float* rects, int count, const SkPaint* paint) = 0;
Chris Craikb4589422013-12-26 15:13:13 -0800177 virtual status_t drawRoundRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -0800178 float rx, float ry, const SkPaint* paint) = 0;
179 virtual status_t drawCircle(float x, float y, float radius, const SkPaint* paint) = 0;
180 virtual status_t drawOval(float left, float top, float right, float bottom,
181 const SkPaint* paint) = 0;
Chris Craikb4589422013-12-26 15:13:13 -0800182 virtual status_t drawArc(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -0800183 float startAngle, float sweepAngle, bool useCenter, const SkPaint* paint) = 0;
184 virtual status_t drawPath(const SkPath* path, const SkPaint* paint) = 0;
185 virtual status_t drawLines(const float* points, int count, const SkPaint* paint) = 0;
186 virtual status_t drawPoints(const float* points, int count, const SkPaint* paint) = 0;
Chris Craikb4589422013-12-26 15:13:13 -0800187
188 // Text
189 virtual status_t drawText(const char* text, int bytesCount, int count, float x, float y,
Chris Craikd218a922014-01-02 17:13:34 -0800190 const float* positions, const SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craikb4589422013-12-26 15:13:13 -0800191 DrawOpMode drawOpMode = kDrawOpMode_Immediate) = 0;
Chris Craikd218a922014-01-02 17:13:34 -0800192 virtual status_t drawTextOnPath(const char* text, int bytesCount, int count, const SkPath* path,
193 float hOffset, float vOffset, const SkPaint* paint) = 0;
Chris Craikb4589422013-12-26 15:13:13 -0800194 virtual status_t drawPosText(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -0800195 const float* positions, const SkPaint* paint) = 0;
Chris Craikb4589422013-12-26 15:13:13 -0800196
197// ----------------------------------------------------------------------------
198// Canvas draw operations - special
199// ----------------------------------------------------------------------------
200 virtual status_t drawLayer(Layer* layer, float x, float y) = 0;
Chris Craika7090e02014-06-20 16:01:00 -0700201 virtual status_t drawRenderNode(RenderNode* renderNode, Rect& dirty,
Chris Craikb4589422013-12-26 15:13:13 -0800202 int32_t replayFlags) = 0;
203
204 // TODO: rename for consistency
205 virtual status_t callDrawGLFunction(Functor* functor, Rect& dirty) = 0;
Chris Craikb4589422013-12-26 15:13:13 -0800206}; // class Renderer
207
208}; // namespace uirenderer
209}; // namespace android
210
211#endif // ANDROID_HWUI_RENDERER_H