blob: b3cef88ba8ea83df973b7d4ae1da2e8f9c7f813a [file] [log] [blame]
Romain Guye4d01122010-06-16 18:44:05 -07001/*
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
Romain Guy9d5316e2010-06-24 19:30:36 -070017#ifndef ANDROID_UI_OPENGL_RENDERER_H
18#define ANDROID_UI_OPENGL_RENDERER_H
19
20#include <GLES2/gl2.h>
21#include <GLES2/gl2ext.h>
Romain Guy85bf02f2010-06-22 13:11:24 -070022
Romain Guyce0537b2010-06-29 21:05:21 -070023#include <SkBitmap.h>
Romain Guyf6a11b82010-06-23 17:47:49 -070024#include <SkMatrix.h>
Romain Guyce0537b2010-06-29 21:05:21 -070025#include <SkPaint.h>
Romain Guy079ba2c2010-07-16 14:12:24 -070026#include <SkRegion.h>
Romain Guyd27977d2010-07-14 19:18:51 -070027#include <SkShader.h>
Romain Guy85bf02f2010-06-22 13:11:24 -070028#include <SkXfermode.h>
Romain Guye4d01122010-06-16 18:44:05 -070029
Romain Guybb9524b2010-06-22 18:56:38 -070030#include <utils/RefBase.h>
Romain Guydeba7852010-07-07 17:54:48 -070031#include <utils/ResourceTypes.h>
Romain Guybb9524b2010-06-22 18:56:38 -070032
Romain Guyf6a11b82010-06-23 17:47:49 -070033#include "Matrix.h"
Romain Guy5cbbce52010-06-27 22:59:20 -070034#include "Program.h"
Romain Guybb9524b2010-06-22 18:56:38 -070035#include "Rect.h"
Romain Guy5cbbce52010-06-27 22:59:20 -070036#include "Snapshot.h"
Romain Guyce0537b2010-06-29 21:05:21 -070037#include "TextureCache.h"
Romain Guydda570202010-07-06 11:39:32 -070038#include "LayerCache.h"
Romain Guyf7f93552010-07-08 19:17:03 -070039#include "PatchCache.h"
40#include "Vertex.h"
Romain Guybb9524b2010-06-22 18:56:38 -070041
Romain Guye4d01122010-06-16 18:44:05 -070042namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070043namespace uirenderer {
Romain Guye4d01122010-06-16 18:44:05 -070044
Romain Guyf6a11b82010-06-23 17:47:49 -070045///////////////////////////////////////////////////////////////////////////////
46// Support
47///////////////////////////////////////////////////////////////////////////////
48
Romain Guy5cbbce52010-06-27 22:59:20 -070049/**
Romain Guy026c5e162010-06-28 17:12:22 -070050 * Structure mapping Skia xfermodes to OpenGL blending factors.
51 */
52struct Blender {
53 SkXfermode::Mode mode;
54 GLenum src;
55 GLenum dst;
56}; // struct Blender
57
Romain Guyf6a11b82010-06-23 17:47:49 -070058///////////////////////////////////////////////////////////////////////////////
59// Renderer
60///////////////////////////////////////////////////////////////////////////////
61
Romain Guy5cbbce52010-06-27 22:59:20 -070062/**
63 * OpenGL renderer used to draw accelerated 2D graphics. The API is a
64 * simplified version of Skia's Canvas API.
65 */
Romain Guy85bf02f2010-06-22 13:11:24 -070066class OpenGLRenderer {
Romain Guye4d01122010-06-16 18:44:05 -070067public:
Romain Guy85bf02f2010-06-22 13:11:24 -070068 OpenGLRenderer();
69 ~OpenGLRenderer();
Romain Guye4d01122010-06-16 18:44:05 -070070
71 void setViewport(int width, int height);
72 void prepare();
Romain Guy08ae3172010-06-21 19:35:50 -070073
Romain Guybb9524b2010-06-22 18:56:38 -070074 int getSaveCount() const;
75 int save(int flags);
76 void restore();
77 void restoreToCount(int saveCount);
78
Romain Guybd6b79b2010-06-26 00:13:53 -070079 int saveLayer(float left, float top, float right, float bottom, const SkPaint* p, int flags);
80 int saveLayerAlpha(float left, float top, float right, float bottom, int alpha, int flags);
81
Romain Guyf6a11b82010-06-23 17:47:49 -070082 void translate(float dx, float dy);
83 void rotate(float degrees);
84 void scale(float sx, float sy);
85
86 void setMatrix(SkMatrix* matrix);
87 void getMatrix(SkMatrix* matrix);
88 void concatMatrix(SkMatrix* matrix);
89
Romain Guy9d5316e2010-06-24 19:30:36 -070090 const Rect& getClipBounds();
Romain Guyc7d53492010-06-25 13:41:57 -070091 bool quickReject(float left, float top, float right, float bottom);
Romain Guy079ba2c2010-07-16 14:12:24 -070092 bool clipRect(float left, float top, float right, float bottom, SkRegion::Op op);
Romain Guybb9524b2010-06-22 18:56:38 -070093
Romain Guyc1396e92010-06-30 17:56:19 -070094 void drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint);
Romain Guyf86ef572010-07-01 11:05:42 -070095 void drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint);
Romain Guy8ba548f2010-06-30 19:21:21 -070096 void drawBitmap(SkBitmap* bitmap, float srcLeft, float srcTop, float srcRight, float srcBottom,
Romain Guyf86ef572010-07-01 11:05:42 -070097 float dstLeft, float dstTop, float dstRight, float dstBottom, const SkPaint* paint);
Romain Guydeba7852010-07-07 17:54:48 -070098 void drawPatch(SkBitmap* bitmap, Res_png_9patch* patch, float left, float top,
99 float right, float bottom, const SkPaint* paint);
Romain Guy85bf02f2010-06-22 13:11:24 -0700100 void drawColor(int color, SkXfermode::Mode mode);
Romain Guybd6b79b2010-06-26 00:13:53 -0700101 void drawRect(float left, float top, float right, float bottom, const SkPaint* paint);
Romain Guy08ae3172010-06-21 19:35:50 -0700102
Romain Guyd27977d2010-07-14 19:18:51 -0700103 void resetShader();
104 void setupBitmapShader(SkBitmap* bitmap, SkShader::TileMode tileX, SkShader::TileMode tileY,
105 SkMatrix* matrix, bool hasAlpha);
Romain Guy7fac2e12010-07-16 17:10:13 -0700106 void setupLinearGradientShader(float* bounds, uint32_t* colors, float* positions,
107 SkShader::TileMode tileMode, SkMatrix* matrix, bool hasAlpha);
Romain Guyd27977d2010-07-14 19:18:51 -0700108
Romain Guy85bf02f2010-06-22 13:11:24 -0700109private:
Romain Guy5cbbce52010-06-27 22:59:20 -0700110 /**
Romain Guyd27977d2010-07-14 19:18:51 -0700111 * Type of Skia shader in use.
112 */
113 enum ShaderType {
114 kShaderNone,
115 kShaderBitmap,
116 kShaderLinearGradient,
117 kShaderCircularGradient,
118 kShaderSweepGradient
119 };
120
121 /**
Romain Guy5cbbce52010-06-27 22:59:20 -0700122 * Saves the current state of the renderer as a new snapshot.
123 * The new snapshot is saved in mSnapshot and the previous snapshot
124 * is linked from mSnapshot->previous.
125 *
126 * @return The new save count. This value can be passed to #restoreToCount()
127 */
Romain Guybb9524b2010-06-22 18:56:38 -0700128 int saveSnapshot();
Romain Guy5cbbce52010-06-27 22:59:20 -0700129
130 /**
131 * Restores the current snapshot; mSnapshot becomes mSnapshot->previous.
132 *
133 * @return True if the clip should be also reapplied by calling
134 * #setScissorFromClip().
135 */
Romain Guybb9524b2010-06-22 18:56:38 -0700136 bool restoreSnapshot();
137
Romain Guy5cbbce52010-06-27 22:59:20 -0700138 /**
139 * Sets the clipping rectangle using glScissor. The clip is defined by
140 * the current snapshot's clipRect member.
141 */
Romain Guybb9524b2010-06-22 18:56:38 -0700142 void setScissorFromClip();
143
Romain Guy5cbbce52010-06-27 22:59:20 -0700144 /**
Romain Guyd55a8612010-06-28 17:42:46 -0700145 * Compose the layer defined in the current snapshot with the layer
146 * defined by the previous snapshot.
147 *
148 * The current snapshot *must* be a layer (flag kFlagIsLayer set.)
149 *
150 * @param curent The current snapshot containing the layer to compose
151 * @param previous The previous snapshot to compose the current layer with
152 */
153 void composeLayer(sp<Snapshot> current, sp<Snapshot> previous);
154
155 /**
156 * Creates a new layer stored in the specified snapshot.
157 *
158 * @param snapshot The snapshot associated with the new layer
159 * @param left The left coordinate of the layer
160 * @param top The top coordinate of the layer
161 * @param right The right coordinate of the layer
162 * @param bottom The bottom coordinate of the layer
163 * @param alpha The translucency of the layer
164 * @param mode The blending mode of the layer
165 * @param flags The layer save flags
166 *
167 * @return True if the layer was successfully created, false otherwise
168 */
169 bool createLayer(sp<Snapshot> snapshot, float left, float top, float right, float bottom,
170 int alpha, SkXfermode::Mode mode, int flags);
171
172 /**
Romain Guy5cbbce52010-06-27 22:59:20 -0700173 * Draws a colored rectangle with the specified color. The specified coordinates
174 * are transformed by the current snapshot's transform matrix.
175 *
176 * @param left The left coordinate of the rectangle
177 * @param top The top coordinate of the rectangle
178 * @param right The right coordinate of the rectangle
179 * @param bottom The bottom coordinate of the rectangle
180 * @param color The rectangle's ARGB color, defined as a packed 32 bits word
Romain Guy026c5e162010-06-28 17:12:22 -0700181 * @param mode The Skia xfermode to use
Romain Guy3d58c032010-07-14 16:34:53 -0700182 * @param ignoreTransform True if the current transform should be ignored
Romain Guy5cbbce52010-06-27 22:59:20 -0700183 */
Romain Guy026c5e162010-06-28 17:12:22 -0700184 void drawColorRect(float left, float top, float right, float bottom,
Romain Guy3d58c032010-07-14 16:34:53 -0700185 int color, SkXfermode::Mode mode, bool ignoreTransform = false);
Romain Guy5cbbce52010-06-27 22:59:20 -0700186
187 /**
188 * Draws a textured rectangle with the specified texture. The specified coordinates
189 * are transformed by the current snapshot's transform matrix.
190 *
191 * @param left The left coordinate of the rectangle
192 * @param top The top coordinate of the rectangle
193 * @param right The right coordinate of the rectangle
194 * @param bottom The bottom coordinate of the rectangle
195 * @param texture The texture name to map onto the rectangle
196 * @param alpha An additional translucency parameter, between 0.0f and 1.0f
Romain Guyd55a8612010-06-28 17:42:46 -0700197 * @param mode The blending mode
Romain Guyc1396e92010-06-30 17:56:19 -0700198 * @param blend True if the texture contains an alpha channel
Romain Guy5cbbce52010-06-27 22:59:20 -0700199 */
Romain Guybd6b79b2010-06-26 00:13:53 -0700200 void drawTextureRect(float left, float top, float right, float bottom, GLuint texture,
Romain Guya9794742010-07-13 11:37:54 -0700201 float alpha, SkXfermode::Mode mode, bool blend);
Romain Guyc7d53492010-06-25 13:41:57 -0700202
Romain Guy026c5e162010-06-28 17:12:22 -0700203 /**
Romain Guy82ba8142010-07-09 13:25:56 -0700204 * Draws a textured rectangle with the specified texture. The specified coordinates
205 * are transformed by the current snapshot's transform matrix.
206 *
207 * @param left The left coordinate of the rectangle
208 * @param top The top coordinate of the rectangle
209 * @param right The right coordinate of the rectangle
210 * @param bottom The bottom coordinate of the rectangle
211 * @param texture The texture to use
212 * @param paint The paint containing the alpha, blending mode, etc.
Romain Guy82ba8142010-07-09 13:25:56 -0700213 */
Romain Guya9794742010-07-13 11:37:54 -0700214 void drawTextureRect(float left, float top, float right, float bottom,
215 const Texture* texture, const SkPaint* paint);
Romain Guy82ba8142010-07-09 13:25:56 -0700216
217 /**
218 * Draws a textured mesh with the specified texture. If the indices are omitted, the
219 * mesh is drawn as a simple quad.
220 *
221 * @param left The left coordinate of the rectangle
222 * @param top The top coordinate of the rectangle
223 * @param right The right coordinate of the rectangle
224 * @param bottom The bottom coordinate of the rectangle
225 * @param texture The texture name to map onto the rectangle
226 * @param alpha An additional translucency parameter, between 0.0f and 1.0f
227 * @param mode The blending mode
228 * @param blend True if the texture contains an alpha channel
Romain Guy82ba8142010-07-09 13:25:56 -0700229 * @param vertices The vertices that define the mesh
230 * @param texCoords The texture coordinates of each vertex
231 * @param indices The indices of the vertices, can be NULL
232 * @param elementsCount The number of elements in the mesh, required by indices
Romain Guyf7f93552010-07-08 19:17:03 -0700233 */
234 void drawTextureMesh(float left, float top, float right, float bottom, GLuint texture,
Romain Guya9794742010-07-13 11:37:54 -0700235 float alpha, SkXfermode::Mode mode, bool blend,
Romain Guyf7f93552010-07-08 19:17:03 -0700236 GLvoid* vertices, GLvoid* texCoords, GLvoid* indices, GLsizei elementsCount = 0);
237
238 /**
Romain Guyd27977d2010-07-14 19:18:51 -0700239 * Fills the specified rectangle with the currently set bitmap shader.
240 *
241 * @param left The left coordinate of the rectangle
242 * @param top The top coordinate of the rectangle
243 * @param right The right coordinate of the rectangle
244 * @param bottom The bottom coordinate of the rectangle
245 * @param alpha An additional translucency parameter, between 0.0f and 1.0f
246 * @param mode The blending mode
247 */
248 void drawBitmapShader(float left, float top, float right, float bottom, float alpha,
249 SkXfermode::Mode mode);
250
251 /**
Romain Guy026c5e162010-06-28 17:12:22 -0700252 * Resets the texture coordinates stored in mDrawTextureVertices. Setting the values
253 * back to default is achieved by calling:
254 *
Romain Guy8ba548f2010-06-30 19:21:21 -0700255 * resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guy026c5e162010-06-28 17:12:22 -0700256 *
257 * @param u1 The left coordinate of the texture
258 * @param v1 The bottom coordinate of the texture
259 * @param u2 The right coordinate of the texture
260 * @param v2 The top coordinate of the texture
261 */
262 void resetDrawTextureTexCoords(float u1, float v1, float u2, float v2);
263
Romain Guy8ba548f2010-06-30 19:21:21 -0700264 /**
265 * Gets the alpha and xfermode out of a paint object. If the paint is null
266 * alpha will be 255 and the xfermode will be SRC_OVER.
267 *
268 * @param paint The paint to extract values from
269 * @param alpha Where to store the resulting alpha
270 * @param mode Where to store the resulting xfermode
271 */
272 inline void getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode);
273
Romain Guyf7f93552010-07-08 19:17:03 -0700274 /**
Romain Guy82ba8142010-07-09 13:25:56 -0700275 * Enable or disable blending as necessary. This function sets the appropriate
276 * blend function based on the specified xfermode.
277 */
Romain Guya9794742010-07-13 11:37:54 -0700278 inline void chooseBlending(bool blend, SkXfermode::Mode mode, bool isPremultiplied = true);
Romain Guy82ba8142010-07-09 13:25:56 -0700279
Romain Guy260e1022010-07-12 14:41:06 -0700280 /**
Romain Guyd27977d2010-07-14 19:18:51 -0700281 * Use the specified program with the current GL context. If the program is already
282 * in use, it will not be bound again. If it is not in use, the current program is
283 * marked unused and the specified program becomes used and becomes the new
284 * current program.
Romain Guy6926c722010-07-12 20:20:03 -0700285 *
Romain Guyd27977d2010-07-14 19:18:51 -0700286 * @param program The program to use
287 *
288 * @return true If the specified program was already in use, false otherwise.
Romain Guy260e1022010-07-12 14:41:06 -0700289 */
Romain Guyd27977d2010-07-14 19:18:51 -0700290 inline bool useProgram(const sp<Program>& program);
Romain Guy260e1022010-07-12 14:41:06 -0700291
Romain Guybb9524b2010-06-22 18:56:38 -0700292 // Dimensions of the drawing surface
293 int mWidth, mHeight;
294
Romain Guy85bf02f2010-06-22 13:11:24 -0700295 // Matrix used for ortho projection in shaders
Romain Guy260e1022010-07-12 14:41:06 -0700296 mat4 mOrthoMatrix;
Romain Guybb9524b2010-06-22 18:56:38 -0700297
Romain Guyc7d53492010-06-25 13:41:57 -0700298 // Model-view matrix used to position/size objects
299 mat4 mModelView;
300
Romain Guybb9524b2010-06-22 18:56:38 -0700301 // Number of saved states
302 int mSaveCount;
Romain Guyf6a11b82010-06-23 17:47:49 -0700303 // Base state
304 Snapshot mFirstSnapshot;
Romain Guybb9524b2010-06-22 18:56:38 -0700305 // Current state
306 sp<Snapshot> mSnapshot;
Romain Guy9d5316e2010-06-24 19:30:36 -0700307
308 // Shaders
Romain Guyd27977d2010-07-14 19:18:51 -0700309 sp<Program> mCurrentProgram;
310 sp<DrawColorProgram> mDrawColorProgram;
311 sp<DrawTextureProgram> mDrawTextureProgram;
Romain Guy026c5e162010-06-28 17:12:22 -0700312
313 // Used to draw textured quads
314 TextureVertex mDrawTextureVertices[4];
Romain Guyce0537b2010-06-29 21:05:21 -0700315
Romain Guy1e793862010-07-16 15:07:42 -0700316 // Current texture state
317 GLuint mLastTexture;
318
Romain Guy82ba8142010-07-09 13:25:56 -0700319 // Last known blend state
320 bool mBlend;
321 GLenum mLastSrcMode;
322 GLenum mLastDstMode;
323
Romain Guy7fac2e12010-07-16 17:10:13 -0700324 // Skia shaders
Romain Guyd27977d2010-07-14 19:18:51 -0700325 ShaderType mShader;
326 bool mShaderBlend;
Romain Guyd27977d2010-07-14 19:18:51 -0700327 SkShader::TileMode mShaderTileX;
328 SkShader::TileMode mShaderTileY;
329 SkMatrix* mShaderMatrix;
Romain Guy7fac2e12010-07-16 17:10:13 -0700330 // Bitmaps
331 SkBitmap* mShaderBitmap;
332 // Gradients
333 float* mShaderBounds;
334 uint32_t* mShaderColors;
335 float* mShaderPositions;
Romain Guyd27977d2010-07-14 19:18:51 -0700336
Romain Guy82ba8142010-07-09 13:25:56 -0700337 // Various caches
Romain Guyce0537b2010-06-29 21:05:21 -0700338 TextureCache mTextureCache;
Romain Guydda570202010-07-06 11:39:32 -0700339 LayerCache mLayerCache;
Romain Guyf7f93552010-07-08 19:17:03 -0700340 PatchCache mPatchCache;
Romain Guybb9524b2010-06-22 18:56:38 -0700341}; // class OpenGLRenderer
Romain Guye4d01122010-06-16 18:44:05 -0700342
Romain Guy9d5316e2010-06-24 19:30:36 -0700343}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -0700344}; // namespace android
345
Romain Guy9d5316e2010-06-24 19:30:36 -0700346#endif // ANDROID_UI_OPENGL_RENDERER_H