Renderer.h revision d1ad5e62fda248c6d185cde3cb6d9f01a223066c
1/*
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
20#include <SkRegion.h>
21
22#include <utils/String8.h>
23
24#include "AssetAtlas.h"
25#include "SkPaint.h"
26
27namespace android {
28
29class Functor;
30struct Res_png_9patch;
31
32namespace uirenderer {
33
34class RenderNode;
35class Layer;
36class Matrix4;
37class SkiaColorFilter;
38class 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    /**
59     * Indicates whether this renderer is recording drawing commands for later playback.
60     * If this method returns true, the drawing commands are deferred.
61     */
62    virtual bool isRecording() const {
63        return false;
64    }
65
66    /**
67     * Safely retrieves the mode from the specified xfermode. If the specified
68     * xfermode is null, the mode is assumed to be SkXfermode::kSrcOver_Mode.
69     */
70    static inline SkXfermode::Mode getXfermode(SkXfermode* mode) {
71        SkXfermode::Mode resultMode;
72        if (!SkXfermode::AsMode(mode, &resultMode)) {
73            resultMode = SkXfermode::kSrcOver_Mode;
74        }
75        return resultMode;
76    }
77
78// ----------------------------------------------------------------------------
79// Frame state operations
80// ----------------------------------------------------------------------------
81    /**
82     * Sets the dimension of the underlying drawing surface. This method must
83     * be called at least once every time the drawing surface changes size.
84     *
85     * @param width The width in pixels of the underlysing surface
86     * @param height The height in pixels of the underlysing surface
87     */
88    virtual void setViewport(int width, int height) = 0;
89
90    /**
91     * Prepares the renderer to draw a frame. This method must be invoked
92     * at the beginning of each frame. When this method is invoked, the
93     * entire drawing surface is assumed to be redrawn.
94     *
95     * @param opaque If true, the target surface is considered opaque
96     *               and will not be cleared. If false, the target surface
97     *               will be cleared
98     */
99    virtual status_t prepare(bool opaque) = 0;
100
101    /**
102     * Prepares the renderer to draw a frame. This method must be invoked
103     * at the beginning of each frame. Only the specified rectangle of the
104     * frame is assumed to be dirty. A clip will automatically be set to
105     * the specified rectangle.
106     *
107     * @param left The left coordinate of the dirty rectangle
108     * @param top The top coordinate of the dirty rectangle
109     * @param right The right coordinate of the dirty rectangle
110     * @param bottom The bottom coordinate of the dirty rectangle
111     * @param opaque If true, the target surface is considered opaque
112     *               and will not be cleared. If false, the target surface
113     *               will be cleared in the specified dirty rectangle
114     */
115    virtual status_t prepareDirty(float left, float top, float right, float bottom,
116            bool opaque) = 0;
117
118    /**
119     * Indicates the end of a frame. This method must be invoked whenever
120     * the caller is done rendering a frame.
121     */
122    virtual void finish() = 0;
123
124    /**
125     * This method must be invoked before handing control over to a draw functor.
126     * See callDrawGLFunction() for instance.
127     *
128     * This command must not be recorded inside display lists.
129     */
130    virtual void interrupt() = 0;
131
132    /**
133     * This method must be invoked after getting control back from a draw functor.
134     *
135     * This command must not be recorded inside display lists.
136     */
137    virtual void resume() = 0;
138
139// ----------------------------------------------------------------------------
140// Canvas state operations
141// ----------------------------------------------------------------------------
142    // Save (layer)
143    virtual int getSaveCount() const = 0;
144    virtual int save(int flags) = 0;
145    virtual void restore() = 0;
146    virtual void restoreToCount(int saveCount) = 0;
147
148    virtual int saveLayer(float left, float top, float right, float bottom,
149            const SkPaint* paint, int flags) = 0;
150
151    int saveLayerAlpha(float left, float top, float right, float bottom,
152            int alpha, int flags) {
153        SkPaint paint;
154        paint.setAlpha(alpha);
155        return saveLayer(left, top, right, bottom, &paint, flags);
156    }
157
158    // Matrix
159    virtual void getMatrix(SkMatrix* outMatrix) const = 0;
160    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
165    virtual void setMatrix(const SkMatrix* matrix) = 0;
166    virtual void concatMatrix(const SkMatrix* matrix) = 0;
167
168    // clip
169    virtual const Rect& getLocalClipBounds() const = 0;
170    virtual bool quickRejectConservative(float left, float top,
171            float right, float bottom) const = 0;
172    virtual bool clipRect(float left, float top, float right, float bottom, SkRegion::Op op) = 0;
173    virtual bool clipPath(const SkPath* path, SkRegion::Op op) = 0;
174    virtual bool clipRegion(const SkRegion* region, SkRegion::Op op) = 0;
175
176    // Misc - should be implemented with SkPaint inspection
177    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
186    virtual status_t drawBitmap(const SkBitmap* bitmap, float left, float top,
187            const SkPaint* paint) = 0;
188    virtual status_t drawBitmap(const SkBitmap* bitmap, const SkMatrix* matrix,
189            const SkPaint* paint) = 0;
190    virtual status_t drawBitmap(const SkBitmap* bitmap, float srcLeft, float srcTop,
191            float srcRight, float srcBottom, float dstLeft, float dstTop,
192            float dstRight, float dstBottom, const SkPaint* paint) = 0;
193    virtual status_t drawBitmapData(const SkBitmap* bitmap, float left, float top,
194            const SkPaint* paint) = 0;
195    virtual status_t drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight,
196            const float* vertices, const int* colors, const SkPaint* paint) = 0;
197    virtual status_t drawPatch(const SkBitmap* bitmap, const Res_png_9patch* patch,
198            float left, float top, float right, float bottom, const SkPaint* paint) = 0;
199
200    // Shapes
201    virtual status_t drawRect(float left, float top, float right, float bottom,
202            const SkPaint* paint) = 0;
203    virtual status_t drawRects(const float* rects, int count, const SkPaint* paint) = 0;
204    virtual status_t drawRoundRect(float left, float top, float right, float bottom,
205            float rx, float ry, const SkPaint* paint) = 0;
206    virtual status_t drawCircle(float x, float y, float radius, const SkPaint* paint) = 0;
207    virtual status_t drawOval(float left, float top, float right, float bottom,
208            const SkPaint* paint) = 0;
209    virtual status_t drawArc(float left, float top, float right, float bottom,
210            float startAngle, float sweepAngle, bool useCenter, const SkPaint* paint) = 0;
211    virtual status_t drawPath(const SkPath* path, const SkPaint* paint) = 0;
212    virtual status_t drawLines(const float* points, int count, const SkPaint* paint) = 0;
213    virtual status_t drawPoints(const float* points, int count, const SkPaint* paint) = 0;
214
215    // Text
216    virtual status_t drawText(const char* text, int bytesCount, int count, float x, float y,
217            const float* positions, const SkPaint* paint, float totalAdvance, const Rect& bounds,
218            DrawOpMode drawOpMode = kDrawOpMode_Immediate) = 0;
219    virtual status_t drawTextOnPath(const char* text, int bytesCount, int count, const SkPath* path,
220            float hOffset, float vOffset, const SkPaint* paint) = 0;
221    virtual status_t drawPosText(const char* text, int bytesCount, int count,
222            const float* positions, const SkPaint* paint) = 0;
223
224// ----------------------------------------------------------------------------
225// Canvas draw operations - special
226// ----------------------------------------------------------------------------
227    virtual status_t drawLayer(Layer* layer, float x, float y) = 0;
228    virtual status_t drawDisplayList(RenderNode* displayList, Rect& dirty,
229            int32_t replayFlags) = 0;
230
231    // TODO: rename for consistency
232    virtual status_t callDrawGLFunction(Functor* functor, Rect& dirty) = 0;
233}; // class Renderer
234
235}; // namespace uirenderer
236}; // namespace android
237
238#endif // ANDROID_HWUI_RENDERER_H
239