blob: 48d83b9f91967ace93995d8d597463b5933d9dff [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>
Chris Craik14e51302013-12-30 15:32:54 -080023#include <utils/String8.h>
24
Chris Craikb4589422013-12-26 15:13:13 -080025#include "AssetAtlas.h"
Chris Craikb4589422013-12-26 15:13:13 -080026
Derek Sollenberger09c2d4f2014-10-15 09:21:10 -040027class SkDrawFilter;
28
Chris Craikb4589422013-12-26 15:13:13 -080029namespace android {
Chris Craik14e51302013-12-30 15:32:54 -080030
31class Functor;
Chris Craik564acf72014-01-02 16:46:18 -080032struct Res_png_9patch;
Chris Craik14e51302013-12-30 15:32:54 -080033
Chris Craikb4589422013-12-26 15:13:13 -080034namespace uirenderer {
35
John Recke18264b2014-03-12 13:56:30 -070036class RenderNode;
Chris Craikb4589422013-12-26 15:13:13 -080037class Layer;
38class Matrix4;
39class SkiaColorFilter;
Chris Craikb4589422013-12-26 15:13:13 -080040class Patch;
41
42enum DrawOpMode {
43 kDrawOpMode_Immediate,
44 kDrawOpMode_Defer,
45 kDrawOpMode_Flush
46};
47
48/**
49 * Hwui's abstract version of Canvas.
50 *
51 * Provides methods for frame state operations, as well as the SkCanvas style transform/clip state,
52 * and varied drawing operations.
53 *
54 * Should at some point interact with native SkCanvas.
55 */
56class ANDROID_API Renderer {
57public:
58 virtual ~Renderer() {}
59
Chris Craikb4589422013-12-26 15:13:13 -080060// ----------------------------------------------------------------------------
61// Frame state operations
62// ----------------------------------------------------------------------------
63 /**
64 * Sets the dimension of the underlying drawing surface. This method must
65 * be called at least once every time the drawing surface changes size.
66 *
67 * @param width The width in pixels of the underlysing surface
68 * @param height The height in pixels of the underlysing surface
69 */
70 virtual void setViewport(int width, int height) = 0;
71
72 /**
73 * Prepares the renderer to draw a frame. This method must be invoked
74 * at the beginning of each frame. When this method is invoked, the
75 * entire drawing surface is assumed to be redrawn.
76 *
77 * @param opaque If true, the target surface is considered opaque
78 * and will not be cleared. If false, the target surface
79 * will be cleared
80 */
Tom Hudson107843d2014-09-08 11:26:26 -040081 virtual void prepare(bool opaque) = 0;
Chris Craikb4589422013-12-26 15:13:13 -080082
83 /**
84 * Prepares the renderer to draw a frame. This method must be invoked
85 * at the beginning of each frame. Only the specified rectangle of the
86 * frame is assumed to be dirty. A clip will automatically be set to
87 * the specified rectangle.
88 *
89 * @param left The left coordinate of the dirty rectangle
90 * @param top The top coordinate of the dirty rectangle
91 * @param right The right coordinate of the dirty rectangle
92 * @param bottom The bottom coordinate of the dirty rectangle
93 * @param opaque If true, the target surface is considered opaque
94 * and will not be cleared. If false, the target surface
95 * will be cleared in the specified dirty rectangle
96 */
Tom Hudson107843d2014-09-08 11:26:26 -040097 virtual void prepareDirty(float left, float top, float right, float bottom,
Chris Craikb4589422013-12-26 15:13:13 -080098 bool opaque) = 0;
99
100 /**
101 * Indicates the end of a frame. This method must be invoked whenever
102 * the caller is done rendering a frame.
Tom Hudson107843d2014-09-08 11:26:26 -0400103 * Returns true if any drawing was done during the frame (the output
104 * has changed / is "dirty" and should be displayed to the user).
Chris Craikb4589422013-12-26 15:13:13 -0800105 */
Tom Hudson107843d2014-09-08 11:26:26 -0400106 virtual bool finish() = 0;
Chris Craikb4589422013-12-26 15:13:13 -0800107
Chris Craikb4589422013-12-26 15:13:13 -0800108// ----------------------------------------------------------------------------
109// Canvas state operations
110// ----------------------------------------------------------------------------
Chris Craik14e51302013-12-30 15:32:54 -0800111 // Save (layer)
Chris Craikb4589422013-12-26 15:13:13 -0800112 virtual int getSaveCount() const = 0;
Chris Craikb4589422013-12-26 15:13:13 -0800113 virtual int save(int flags) = 0;
114 virtual void restore() = 0;
115 virtual void restoreToCount(int saveCount) = 0;
116
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500117 virtual int saveLayer(float left, float top, float right, float bottom,
118 const SkPaint* paint, int flags) = 0;
119
Chris Craikb4589422013-12-26 15:13:13 -0800120 int saveLayerAlpha(float left, float top, float right, float bottom,
121 int alpha, int flags) {
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500122 SkPaint paint;
123 paint.setAlpha(alpha);
124 return saveLayer(left, top, right, bottom, &paint, flags);
Chris Craikb4589422013-12-26 15:13:13 -0800125 }
Chris Craikb4589422013-12-26 15:13:13 -0800126
127 // Matrix
Chris Craik14e51302013-12-30 15:32:54 -0800128 virtual void getMatrix(SkMatrix* outMatrix) const = 0;
Chris Craikb4589422013-12-26 15:13:13 -0800129 virtual void translate(float dx, float dy, float dz = 0.0f) = 0;
130 virtual void rotate(float degrees) = 0;
131 virtual void scale(float sx, float sy) = 0;
132 virtual void skew(float sx, float sy) = 0;
133
Derek Sollenberger13908822013-12-10 12:28:58 -0500134 virtual void setMatrix(const SkMatrix& matrix) = 0;
135 virtual void concatMatrix(const SkMatrix& matrix) = 0;
Chris Craikb4589422013-12-26 15:13:13 -0800136
Chris Craik14e51302013-12-30 15:32:54 -0800137 // clip
Chris Craik3f0854292014-04-15 16:18:08 -0700138 virtual const Rect& getLocalClipBounds() const = 0;
Chris Craik14e51302013-12-30 15:32:54 -0800139 virtual bool quickRejectConservative(float left, float top,
140 float right, float bottom) const = 0;
Chris Craikb4589422013-12-26 15:13:13 -0800141 virtual bool clipRect(float left, float top, float right, float bottom, SkRegion::Op op) = 0;
Chris Craikd218a922014-01-02 17:13:34 -0800142 virtual bool clipPath(const SkPath* path, SkRegion::Op op) = 0;
143 virtual bool clipRegion(const SkRegion* region, SkRegion::Op op) = 0;
Chris Craikb4589422013-12-26 15:13:13 -0800144
Derek Sollenberger09c2d4f2014-10-15 09:21:10 -0400145 // Misc
146 virtual void setDrawFilter(SkDrawFilter* filter) = 0;
Chris Craikb4589422013-12-26 15:13:13 -0800147
148// ----------------------------------------------------------------------------
149// Canvas draw operations
150// ----------------------------------------------------------------------------
Tom Hudson107843d2014-09-08 11:26:26 -0400151 virtual void drawColor(int color, SkXfermode::Mode mode) = 0;
Chris Craikb4589422013-12-26 15:13:13 -0800152
153 // Bitmap-based
Tom Hudson107843d2014-09-08 11:26:26 -0400154 virtual void drawBitmap(const SkBitmap* bitmap, const SkPaint* paint) = 0;
155 virtual void drawBitmap(const SkBitmap* bitmap, float srcLeft, float srcTop,
Chris Craikb4589422013-12-26 15:13:13 -0800156 float srcRight, float srcBottom, float dstLeft, float dstTop,
Chris Craikd218a922014-01-02 17:13:34 -0800157 float dstRight, float dstBottom, const SkPaint* paint) = 0;
Tom Hudson107843d2014-09-08 11:26:26 -0400158 virtual void drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight,
Chris Craikd218a922014-01-02 17:13:34 -0800159 const float* vertices, const int* colors, const SkPaint* paint) = 0;
Tom Hudson107843d2014-09-08 11:26:26 -0400160 virtual void drawPatch(const SkBitmap* bitmap, const Res_png_9patch* patch,
Chris Craikd218a922014-01-02 17:13:34 -0800161 float left, float top, float right, float bottom, const SkPaint* paint) = 0;
Chris Craikb4589422013-12-26 15:13:13 -0800162
163 // Shapes
Tom Hudson107843d2014-09-08 11:26:26 -0400164 virtual void drawRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -0800165 const SkPaint* paint) = 0;
Tom Hudson107843d2014-09-08 11:26:26 -0400166 virtual void drawRects(const float* rects, int count, const SkPaint* paint) = 0;
167 virtual void drawRoundRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -0800168 float rx, float ry, const SkPaint* paint) = 0;
Tom Hudson107843d2014-09-08 11:26:26 -0400169 virtual void drawCircle(float x, float y, float radius, const SkPaint* paint) = 0;
170 virtual void drawOval(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -0800171 const SkPaint* paint) = 0;
Tom Hudson107843d2014-09-08 11:26:26 -0400172 virtual void drawArc(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -0800173 float startAngle, float sweepAngle, bool useCenter, const SkPaint* paint) = 0;
Tom Hudson107843d2014-09-08 11:26:26 -0400174 virtual void drawPath(const SkPath* path, const SkPaint* paint) = 0;
175 virtual void drawLines(const float* points, int count, const SkPaint* paint) = 0;
176 virtual void drawPoints(const float* points, int count, const SkPaint* paint) = 0;
Chris Craikb4589422013-12-26 15:13:13 -0800177
178 // Text
Tom Hudson107843d2014-09-08 11:26:26 -0400179 virtual void drawText(const char* text, int bytesCount, int count, float x, float y,
Chris Craikd218a922014-01-02 17:13:34 -0800180 const float* positions, const SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craikb4589422013-12-26 15:13:13 -0800181 DrawOpMode drawOpMode = kDrawOpMode_Immediate) = 0;
Tom Hudson107843d2014-09-08 11:26:26 -0400182 virtual void drawTextOnPath(const char* text, int bytesCount, int count, const SkPath* path,
Chris Craikd218a922014-01-02 17:13:34 -0800183 float hOffset, float vOffset, const SkPaint* paint) = 0;
Tom Hudson107843d2014-09-08 11:26:26 -0400184 virtual void drawPosText(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -0800185 const float* positions, const SkPaint* paint) = 0;
Chris Craikb4589422013-12-26 15:13:13 -0800186
187// ----------------------------------------------------------------------------
188// Canvas draw operations - special
189// ----------------------------------------------------------------------------
Tom Hudson107843d2014-09-08 11:26:26 -0400190 virtual void drawRenderNode(RenderNode* renderNode, Rect& dirty,
Chris Craikb4589422013-12-26 15:13:13 -0800191 int32_t replayFlags) = 0;
192
193 // TODO: rename for consistency
Tom Hudson107843d2014-09-08 11:26:26 -0400194 virtual void callDrawGLFunction(Functor* functor, Rect& dirty) = 0;
Chris Craikb4589422013-12-26 15:13:13 -0800195}; // class Renderer
196
197}; // namespace uirenderer
198}; // namespace android
199
200#endif // ANDROID_HWUI_RENDERER_H