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