OpenGLRenderer.h revision ac670c0433d19397d4e36ced2110475b6f54fe26
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 <SkRegion.h>
27#include <SkShader.h>
28#include <SkXfermode.h>
29
30#include <utils/RefBase.h>
31#include <utils/ResourceTypes.h>
32
33#include "Extensions.h"
34#include "Matrix.h"
35#include "Program.h"
36#include "Rect.h"
37#include "Snapshot.h"
38#include "TextureCache.h"
39#include "LayerCache.h"
40#include "GradientCache.h"
41#include "PatchCache.h"
42#include "Vertex.h"
43#include "FontRenderer.h"
44#include "ProgramCache.h"
45
46namespace android {
47namespace uirenderer {
48
49///////////////////////////////////////////////////////////////////////////////
50// Support
51///////////////////////////////////////////////////////////////////////////////
52
53/**
54 * Structure mapping Skia xfermodes to OpenGL blending factors.
55 */
56struct Blender {
57    SkXfermode::Mode mode;
58    GLenum src;
59    GLenum dst;
60}; // struct Blender
61
62///////////////////////////////////////////////////////////////////////////////
63// Renderer
64///////////////////////////////////////////////////////////////////////////////
65
66/**
67 * OpenGL renderer used to draw accelerated 2D graphics. The API is a
68 * simplified version of Skia's Canvas API.
69 */
70class OpenGLRenderer {
71public:
72    OpenGLRenderer();
73    ~OpenGLRenderer();
74
75    void setViewport(int width, int height);
76    void prepare();
77
78    int getSaveCount() const;
79    int save(int flags);
80    void restore();
81    void restoreToCount(int saveCount);
82
83    int saveLayer(float left, float top, float right, float bottom, const SkPaint* p, int flags);
84    int saveLayerAlpha(float left, float top, float right, float bottom, int alpha, int flags);
85
86    void translate(float dx, float dy);
87    void rotate(float degrees);
88    void scale(float sx, float sy);
89
90    void setMatrix(SkMatrix* matrix);
91    void getMatrix(SkMatrix* matrix);
92    void concatMatrix(SkMatrix* matrix);
93
94    const Rect& getClipBounds();
95    bool quickReject(float left, float top, float right, float bottom);
96    bool clipRect(float left, float top, float right, float bottom, SkRegion::Op op);
97
98    void drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint);
99    void drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint);
100    void drawBitmap(SkBitmap* bitmap, float srcLeft, float srcTop, float srcRight, float srcBottom,
101            float dstLeft, float dstTop, float dstRight, float dstBottom, const SkPaint* paint);
102    void drawPatch(SkBitmap* bitmap, Res_png_9patch* patch, float left, float top,
103            float right, float bottom, const SkPaint* paint);
104    void drawColor(int color, SkXfermode::Mode mode);
105    void drawRect(float left, float top, float right, float bottom, const SkPaint* paint);
106
107    void resetShader();
108    void setupBitmapShader(SkBitmap* bitmap, SkShader::TileMode tileX, SkShader::TileMode tileY,
109            SkMatrix* matrix, bool hasAlpha);
110    void setupLinearGradientShader(SkShader* shader, float* bounds, uint32_t* colors,
111            float* positions, int count, SkShader::TileMode tileMode,
112            SkMatrix* matrix, bool hasAlpha);
113
114    void drawText(const char* text, int bytesCount, int count, float x, float y, SkPaint* paint);
115
116private:
117    /**
118     * Type of Skia shader in use.
119     */
120    enum ShaderType {
121        kShaderNone,
122        kShaderBitmap,
123        kShaderLinearGradient,
124        kShaderCircularGradient,
125        kShaderSweepGradient
126    };
127
128    /**
129     * Saves the current state of the renderer as a new snapshot.
130     * The new snapshot is saved in mSnapshot and the previous snapshot
131     * is linked from mSnapshot->previous.
132     *
133     * @return The new save count. This value can be passed to #restoreToCount()
134     */
135    int saveSnapshot();
136
137    /**
138     * Restores the current snapshot; mSnapshot becomes mSnapshot->previous.
139     *
140     * @return True if the clip should be also reapplied by calling
141     *         #setScissorFromClip().
142     */
143    bool restoreSnapshot();
144
145    /**
146     * Sets the clipping rectangle using glScissor. The clip is defined by
147     * the current snapshot's clipRect member.
148     */
149    void setScissorFromClip();
150
151    /**
152     * Compose the layer defined in the current snapshot with the layer
153     * defined by the previous snapshot.
154     *
155     * The current snapshot *must* be a layer (flag kFlagIsLayer set.)
156     *
157     * @param curent The current snapshot containing the layer to compose
158     * @param previous The previous snapshot to compose the current layer with
159     */
160    void composeLayer(sp<Snapshot> current, sp<Snapshot> previous);
161
162    /**
163     * Creates a new layer stored in the specified snapshot.
164     *
165     * @param snapshot The snapshot associated with the new layer
166     * @param left The left coordinate of the layer
167     * @param top The top coordinate of the layer
168     * @param right The right coordinate of the layer
169     * @param bottom The bottom coordinate of the layer
170     * @param alpha The translucency of the layer
171     * @param mode The blending mode of the layer
172     * @param flags The layer save flags
173     *
174     * @return True if the layer was successfully created, false otherwise
175     */
176    bool createLayer(sp<Snapshot> snapshot, float left, float top, float right, float bottom,
177            int alpha, SkXfermode::Mode mode, int flags);
178
179    /**
180     * Draws a colored rectangle with the specified color. The specified coordinates
181     * are transformed by the current snapshot's transform matrix.
182     *
183     * @param left The left coordinate of the rectangle
184     * @param top The top coordinate of the rectangle
185     * @param right The right coordinate of the rectangle
186     * @param bottom The bottom coordinate of the rectangle
187     * @param color The rectangle's ARGB color, defined as a packed 32 bits word
188     * @param mode The Skia xfermode to use
189     * @param ignoreTransform True if the current transform should be ignored
190     */
191    void drawColorRect(float left, float top, float right, float bottom,
192    		int color, SkXfermode::Mode mode, bool ignoreTransform = false);
193
194    /**
195     * Draws a textured rectangle with the specified texture. The specified coordinates
196     * are transformed by the current snapshot's transform matrix.
197     *
198     * @param left The left coordinate of the rectangle
199     * @param top The top coordinate of the rectangle
200     * @param right The right coordinate of the rectangle
201     * @param bottom The bottom coordinate of the rectangle
202     * @param texture The texture name to map onto the rectangle
203     * @param alpha An additional translucency parameter, between 0.0f and 1.0f
204     * @param mode The blending mode
205     * @param blend True if the texture contains an alpha channel
206     */
207    void drawTextureRect(float left, float top, float right, float bottom, GLuint texture,
208            float alpha, SkXfermode::Mode mode, bool blend);
209
210    /**
211     * Draws a textured rectangle with the specified texture. The specified coordinates
212     * are transformed by the current snapshot's transform matrix.
213     *
214     * @param left The left coordinate of the rectangle
215     * @param top The top coordinate of the rectangle
216     * @param right The right coordinate of the rectangle
217     * @param bottom The bottom coordinate of the rectangle
218     * @param texture The texture to use
219     * @param paint The paint containing the alpha, blending mode, etc.
220     */
221    void drawTextureRect(float left, float top, float right, float bottom,
222            const Texture* texture, const SkPaint* paint);
223
224    /**
225     * Draws a textured mesh with the specified texture. If the indices are omitted, the
226     * mesh is drawn as a simple quad.
227     *
228     * @param left The left coordinate of the rectangle
229     * @param top The top coordinate of the rectangle
230     * @param right The right coordinate of the rectangle
231     * @param bottom The bottom coordinate of the rectangle
232     * @param texture The texture name to map onto the rectangle
233     * @param alpha An additional translucency parameter, between 0.0f and 1.0f
234     * @param mode The blending mode
235     * @param blend True if the texture contains an alpha channel
236     * @param vertices The vertices that define the mesh
237     * @param texCoords The texture coordinates of each vertex
238     * @param indices The indices of the vertices, can be NULL
239     * @param elementsCount The number of elements in the mesh, required by indices
240     */
241    void drawTextureMesh(float left, float top, float right, float bottom, GLuint texture,
242            float alpha, SkXfermode::Mode mode, bool blend,
243            GLvoid* vertices, GLvoid* texCoords, GLvoid* indices, GLsizei elementsCount = 0);
244
245    /**
246     * Fills the specified rectangle with the currently set bitmap shader.
247     *
248     * @param left The left coordinate of the rectangle
249     * @param top The top coordinate of the rectangle
250     * @param right The right coordinate of the rectangle
251     * @param bottom The bottom coordinate of the rectangle
252     * @param alpha An additional translucency parameter, between 0.0f and 1.0f
253     * @param mode The blending mode
254     */
255    void drawBitmapShader(float left, float top, float right, float bottom, float alpha,
256            SkXfermode::Mode mode);
257
258    /**
259     * Fills the specified rectangle with the currently set linear gradient shader.
260     *
261     * @param left The left coordinate of the rectangle
262     * @param top The top coordinate of the rectangle
263     * @param right The right coordinate of the rectangle
264     * @param bottom The bottom coordinate of the rectangle
265     * @param alpha An additional translucency parameter, between 0.0f and 1.0f
266     * @param mode The blending mode
267     */
268    void drawLinearGradientShader(float left, float top, float right, float bottom, float alpha,
269            SkXfermode::Mode mode);
270
271    /**
272     * Resets the texture coordinates stored in mMeshVertices. Setting the values
273     * back to default is achieved by calling:
274     *
275     * resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
276     *
277     * @param u1 The left coordinate of the texture
278     * @param v1 The bottom coordinate of the texture
279     * @param u2 The right coordinate of the texture
280     * @param v2 The top coordinate of the texture
281     */
282    void resetDrawTextureTexCoords(float u1, float v1, float u2, float v2);
283
284    /**
285     * Gets the alpha and xfermode out of a paint object. If the paint is null
286     * alpha will be 255 and the xfermode will be SRC_OVER.
287     *
288     * @param paint The paint to extract values from
289     * @param alpha Where to store the resulting alpha
290     * @param mode Where to store the resulting xfermode
291     */
292    inline void getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode);
293
294    /**
295     * Binds the specified texture with the specified wrap modes.
296     */
297    inline void bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT);
298
299    /**
300     * Enable or disable blending as necessary. This function sets the appropriate
301     * blend function based on the specified xfermode.
302     */
303    inline void chooseBlending(bool blend, SkXfermode::Mode mode, bool isPremultiplied = true);
304
305    /**
306     * Use the specified program with the current GL context. If the program is already
307     * in use, it will not be bound again. If it is not in use, the current program is
308     * marked unused and the specified program becomes used and becomes the new
309     * current program.
310     *
311     * @param program The program to use
312     *
313     * @return true If the specified program was already in use, false otherwise.
314     */
315    inline bool useProgram(const sp<Program>& program);
316
317    // Dimensions of the drawing surface
318    int mWidth, mHeight;
319
320    // Matrix used for ortho projection in shaders
321    mat4 mOrthoMatrix;
322
323    // Model-view matrix used to position/size objects
324    mat4 mModelView;
325
326    // Number of saved states
327    int mSaveCount;
328    // Base state
329    Snapshot mFirstSnapshot;
330    // Current state
331    sp<Snapshot> mSnapshot;
332
333    // Shaders
334    sp<Program> mCurrentProgram;
335    sp<DrawColorProgram> mDrawColorProgram;
336    sp<DrawTextureProgram> mDrawTextureProgram;
337    sp<DrawTextProgram> mDrawTextProgram;
338    sp<DrawLinearGradientProgram> mDrawLinearGradientProgram;
339
340    // Used to draw textured quads
341    TextureVertex mMeshVertices[4];
342
343    // Current texture state
344    GLuint mLastTexture;
345
346    // Last known blend state
347    bool mBlend;
348    GLenum mLastSrcMode;
349    GLenum mLastDstMode;
350
351    // Skia shaders
352    ShaderType mShader;
353    SkShader* mShaderKey;
354    bool mShaderBlend;
355    GLenum mShaderTileX;
356    GLenum mShaderTileY;
357    SkMatrix* mShaderMatrix;
358    // Bitmaps
359    SkBitmap* mShaderBitmap;
360    // Gradients
361    float* mShaderBounds;
362    uint32_t* mShaderColors;
363    float* mShaderPositions;
364    int mShaderCount;
365
366    // GL extensions
367    Extensions mExtensions;
368
369    // Font renderer
370    FontRenderer mFontRenderer;
371
372    // Various caches
373    TextureCache mTextureCache;
374    LayerCache mLayerCache;
375    GradientCache mGradientCache;
376    ProgramCache mProgramCache;
377    PatchCache mPatchCache;
378}; // class OpenGLRenderer
379
380}; // namespace uirenderer
381}; // namespace android
382
383#endif // ANDROID_UI_OPENGL_RENDERER_H
384