OpenGLRenderer.h revision d55a86120dd1e8ebcc6906c9ffd463f7460348da
1/*
2 * Copyright (C) 2010 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_UI_OPENGL_RENDERER_H
18#define ANDROID_UI_OPENGL_RENDERER_H
19
20#include <GLES2/gl2.h>
21#include <GLES2/gl2ext.h>
22
23#include <SkMatrix.h>
24#include <SkXfermode.h>
25
26#include <utils/RefBase.h>
27
28#include "Matrix.h"
29#include "Program.h"
30#include "Rect.h"
31#include "Snapshot.h"
32
33namespace android {
34namespace uirenderer {
35
36///////////////////////////////////////////////////////////////////////////////
37// Support
38///////////////////////////////////////////////////////////////////////////////
39
40/**
41 * Simple structure to describe a vertex with a position.
42 * This is used to draw filled rectangles without a texture.
43 */
44struct SimpleVertex {
45    float position[2];
46}; // struct SimpleVertex
47
48/**
49 * Simple structure to describe a vertex with a position and a texture.
50 */
51struct TextureVertex {
52    float position[2];
53    float texture[2];
54}; // struct TextureVertex
55
56/**
57 * Structure mapping Skia xfermodes to OpenGL blending factors.
58 */
59struct Blender {
60    SkXfermode::Mode mode;
61    GLenum src;
62    GLenum dst;
63}; // struct Blender
64
65///////////////////////////////////////////////////////////////////////////////
66// Renderer
67///////////////////////////////////////////////////////////////////////////////
68
69/**
70 * OpenGL renderer used to draw accelerated 2D graphics. The API is a
71 * simplified version of Skia's Canvas API.
72 */
73class OpenGLRenderer {
74public:
75    OpenGLRenderer();
76    ~OpenGLRenderer();
77
78    void setViewport(int width, int height);
79    void prepare();
80
81    int getSaveCount() const;
82    int save(int flags);
83    void restore();
84    void restoreToCount(int saveCount);
85
86    int saveLayer(float left, float top, float right, float bottom, const SkPaint* p, int flags);
87    int saveLayerAlpha(float left, float top, float right, float bottom, int alpha, int flags);
88
89    void translate(float dx, float dy);
90    void rotate(float degrees);
91    void scale(float sx, float sy);
92
93    void setMatrix(SkMatrix* matrix);
94    void getMatrix(SkMatrix* matrix);
95    void concatMatrix(SkMatrix* matrix);
96
97    const Rect& getClipBounds();
98    bool quickReject(float left, float top, float right, float bottom);
99    bool clipRect(float left, float top, float right, float bottom);
100
101    void drawColor(int color, SkXfermode::Mode mode);
102    void drawRect(float left, float top, float right, float bottom, const SkPaint* paint);
103
104private:
105    /**
106     * Saves the current state of the renderer as a new snapshot.
107     * The new snapshot is saved in mSnapshot and the previous snapshot
108     * is linked from mSnapshot->previous.
109     *
110     * @return The new save count. This value can be passed to #restoreToCount()
111     */
112    int saveSnapshot();
113
114    /**
115     * Restores the current snapshot; mSnapshot becomes mSnapshot->previous.
116     *
117     * @return True if the clip should be also reapplied by calling
118     *         #setScissorFromClip().
119     */
120    bool restoreSnapshot();
121
122    /**
123     * Sets the clipping rectangle using glScissor. The clip is defined by
124     * the current snapshot's clipRect member.
125     */
126    void setScissorFromClip();
127
128    /**
129     * Compose the layer defined in the current snapshot with the layer
130     * defined by the previous snapshot.
131     *
132     * The current snapshot *must* be a layer (flag kFlagIsLayer set.)
133     *
134     * @param curent The current snapshot containing the layer to compose
135     * @param previous The previous snapshot to compose the current layer with
136     */
137    void composeLayer(sp<Snapshot> current, sp<Snapshot> previous);
138
139    /**
140     * Creates a new layer stored in the specified snapshot.
141     *
142     * @param snapshot The snapshot associated with the new layer
143     * @param left The left coordinate of the layer
144     * @param top The top coordinate of the layer
145     * @param right The right coordinate of the layer
146     * @param bottom The bottom coordinate of the layer
147     * @param alpha The translucency of the layer
148     * @param mode The blending mode of the layer
149     * @param flags The layer save flags
150     *
151     * @return True if the layer was successfully created, false otherwise
152     */
153    bool createLayer(sp<Snapshot> snapshot, float left, float top, float right, float bottom,
154            int alpha, SkXfermode::Mode mode, int flags);
155
156    /**
157     * Draws a colored rectangle with the specified color. The specified coordinates
158     * are transformed by the current snapshot's transform matrix.
159     *
160     * @param left The left coordinate of the rectangle
161     * @param top The top coordinate of the rectangle
162     * @param right The right coordinate of the rectangle
163     * @param bottom The bottom coordinate of the rectangle
164     * @param color The rectangle's ARGB color, defined as a packed 32 bits word
165     * @param mode The Skia xfermode to use
166     */
167    void drawColorRect(float left, float top, float right, float bottom,
168    		int color, SkXfermode::Mode mode);
169
170    /**
171     * Draws a textured rectangle with the specified texture. The specified coordinates
172     * are transformed by the current snapshot's transform matrix.
173     *
174     * @param left The left coordinate of the rectangle
175     * @param top The top coordinate of the rectangle
176     * @param right The right coordinate of the rectangle
177     * @param bottom The bottom coordinate of the rectangle
178     * @param texture The texture name to map onto the rectangle
179     * @param alpha An additional translucency parameter, between 0.0f and 1.0f
180     * @param mode The blending mode
181     * @param isPremultiplied Indicates whether the texture has premultiplied alpha
182     */
183    void drawTextureRect(float left, float top, float right, float bottom, GLuint texture,
184            float alpha, SkXfermode::Mode mode, bool isPremultiplied = false);
185
186    /**
187     * Resets the texture coordinates stored in mDrawTextureVertices. Setting the values
188     * back to default is achieved by calling:
189     *
190     * resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
191     *
192     * @param u1 The left coordinate of the texture
193     * @param v1 The bottom coordinate of the texture
194     * @param u2 The right coordinate of the texture
195     * @param v2 The top coordinate of the texture
196     */
197    void resetDrawTextureTexCoords(float u1, float v1, float u2, float v2);
198
199    // Dimensions of the drawing surface
200    int mWidth, mHeight;
201
202    // Matrix used for ortho projection in shaders
203    float mOrthoMatrix[16];
204
205    // Model-view matrix used to position/size objects
206    mat4 mModelView;
207
208    // Number of saved states
209    int mSaveCount;
210    // Base state
211    Snapshot mFirstSnapshot;
212    // Current state
213    sp<Snapshot> mSnapshot;
214
215    // Shaders
216    sp<DrawColorProgram> mDrawColorShader;
217    sp<DrawTextureProgram> mDrawTextureShader;
218
219    // Used to draw textured quads
220    TextureVertex mDrawTextureVertices[4];
221}; // class OpenGLRenderer
222
223}; // namespace uirenderer
224}; // namespace android
225
226#endif // ANDROID_UI_OPENGL_RENDERER_H
227