blob: 975be056b15f07b55147ade55e5ad916c8645480 [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 Guy51769a62010-07-23 00:28:00 -070033#include "Extensions.h"
Romain Guyf6a11b82010-06-23 17:47:49 -070034#include "Matrix.h"
Romain Guy5cbbce52010-06-27 22:59:20 -070035#include "Program.h"
Romain Guybb9524b2010-06-22 18:56:38 -070036#include "Rect.h"
Romain Guy5cbbce52010-06-27 22:59:20 -070037#include "Snapshot.h"
Romain Guyce0537b2010-06-29 21:05:21 -070038#include "TextureCache.h"
Romain Guydda57022010-07-06 11:39:32 -070039#include "LayerCache.h"
Romain Guyc0ac1932010-07-19 18:43:02 -070040#include "GradientCache.h"
Romain Guyf7f93552010-07-08 19:17:03 -070041#include "PatchCache.h"
42#include "Vertex.h"
Romain Guy694b5192010-07-21 21:33:20 -070043#include "FontRenderer.h"
Romain Guyac670c02010-07-27 17:39:27 -070044#include "ProgramCache.h"
Romain Guybb9524b2010-06-22 18:56:38 -070045
Romain Guye4d01122010-06-16 18:44:05 -070046namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070047namespace uirenderer {
Romain Guye4d01122010-06-16 18:44:05 -070048
Romain Guyf6a11b82010-06-23 17:47:49 -070049///////////////////////////////////////////////////////////////////////////////
50// Support
51///////////////////////////////////////////////////////////////////////////////
52
Romain Guy5cbbce52010-06-27 22:59:20 -070053/**
Romain Guy026c5e162010-06-28 17:12:22 -070054 * 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
Romain Guyf6a11b82010-06-23 17:47:49 -070062///////////////////////////////////////////////////////////////////////////////
63// Renderer
64///////////////////////////////////////////////////////////////////////////////
65
Romain Guy5cbbce52010-06-27 22:59:20 -070066/**
67 * OpenGL renderer used to draw accelerated 2D graphics. The API is a
68 * simplified version of Skia's Canvas API.
69 */
Romain Guy85bf02f2010-06-22 13:11:24 -070070class OpenGLRenderer {
Romain Guye4d01122010-06-16 18:44:05 -070071public:
Romain Guy85bf02f2010-06-22 13:11:24 -070072 OpenGLRenderer();
73 ~OpenGLRenderer();
Romain Guye4d01122010-06-16 18:44:05 -070074
75 void setViewport(int width, int height);
76 void prepare();
Romain Guy08ae3172010-06-21 19:35:50 -070077
Romain Guybb9524b2010-06-22 18:56:38 -070078 int getSaveCount() const;
79 int save(int flags);
80 void restore();
81 void restoreToCount(int saveCount);
82
Romain Guybd6b79b2010-06-26 00:13:53 -070083 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
Romain Guyf6a11b82010-06-23 17:47:49 -070086 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
Romain Guy9d5316e2010-06-24 19:30:36 -070094 const Rect& getClipBounds();
Romain Guyc7d53492010-06-25 13:41:57 -070095 bool quickReject(float left, float top, float right, float bottom);
Romain Guy079ba2c2010-07-16 14:12:24 -070096 bool clipRect(float left, float top, float right, float bottom, SkRegion::Op op);
Romain Guybb9524b2010-06-22 18:56:38 -070097
Romain Guyc1396e92010-06-30 17:56:19 -070098 void drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint);
Romain Guyf86ef572010-07-01 11:05:42 -070099 void drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint);
Romain Guy8ba548f2010-06-30 19:21:21 -0700100 void drawBitmap(SkBitmap* bitmap, float srcLeft, float srcTop, float srcRight, float srcBottom,
Romain Guyf86ef572010-07-01 11:05:42 -0700101 float dstLeft, float dstTop, float dstRight, float dstBottom, const SkPaint* paint);
Romain Guydeba7852010-07-07 17:54:48 -0700102 void drawPatch(SkBitmap* bitmap, Res_png_9patch* patch, float left, float top,
103 float right, float bottom, const SkPaint* paint);
Romain Guy85bf02f2010-06-22 13:11:24 -0700104 void drawColor(int color, SkXfermode::Mode mode);
Romain Guybd6b79b2010-06-26 00:13:53 -0700105 void drawRect(float left, float top, float right, float bottom, const SkPaint* paint);
Romain Guy08ae3172010-06-21 19:35:50 -0700106
Romain Guyd27977d2010-07-14 19:18:51 -0700107 void resetShader();
108 void setupBitmapShader(SkBitmap* bitmap, SkShader::TileMode tileX, SkShader::TileMode tileY,
109 SkMatrix* matrix, bool hasAlpha);
Romain Guyf9764a42010-07-16 23:13:33 -0700110 void setupLinearGradientShader(SkShader* shader, float* bounds, uint32_t* colors,
Romain Guyc0ac1932010-07-19 18:43:02 -0700111 float* positions, int count, SkShader::TileMode tileMode,
112 SkMatrix* matrix, bool hasAlpha);
Romain Guyd27977d2010-07-14 19:18:51 -0700113
Romain Guye8e62a42010-07-23 18:55:21 -0700114 void drawText(const char* text, int bytesCount, int count, float x, float y, SkPaint* paint);
Romain Guy694b5192010-07-21 21:33:20 -0700115
Romain Guy85bf02f2010-06-22 13:11:24 -0700116private:
Romain Guy5cbbce52010-06-27 22:59:20 -0700117 /**
Romain Guyd27977d2010-07-14 19:18:51 -0700118 * Type of Skia shader in use.
119 */
120 enum ShaderType {
121 kShaderNone,
122 kShaderBitmap,
123 kShaderLinearGradient,
124 kShaderCircularGradient,
125 kShaderSweepGradient
126 };
127
128 /**
Romain Guy5cbbce52010-06-27 22:59:20 -0700129 * 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 */
Romain Guybb9524b2010-06-22 18:56:38 -0700135 int saveSnapshot();
Romain Guy5cbbce52010-06-27 22:59:20 -0700136
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 */
Romain Guybb9524b2010-06-22 18:56:38 -0700143 bool restoreSnapshot();
144
Romain Guy5cbbce52010-06-27 22:59:20 -0700145 /**
146 * Sets the clipping rectangle using glScissor. The clip is defined by
147 * the current snapshot's clipRect member.
148 */
Romain Guybb9524b2010-06-22 18:56:38 -0700149 void setScissorFromClip();
150
Romain Guy5cbbce52010-06-27 22:59:20 -0700151 /**
Romain Guyd55a8612010-06-28 17:42:46 -0700152 * 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 /**
Romain Guy5cbbce52010-06-27 22:59:20 -0700180 * 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
Romain Guy026c5e162010-06-28 17:12:22 -0700188 * @param mode The Skia xfermode to use
Romain Guy3d58c032010-07-14 16:34:53 -0700189 * @param ignoreTransform True if the current transform should be ignored
Romain Guy5cbbce52010-06-27 22:59:20 -0700190 */
Romain Guy026c5e162010-06-28 17:12:22 -0700191 void drawColorRect(float left, float top, float right, float bottom,
Romain Guy3d58c032010-07-14 16:34:53 -0700192 int color, SkXfermode::Mode mode, bool ignoreTransform = false);
Romain Guy5cbbce52010-06-27 22:59:20 -0700193
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
Romain Guyd55a8612010-06-28 17:42:46 -0700204 * @param mode The blending mode
Romain Guyc1396e92010-06-30 17:56:19 -0700205 * @param blend True if the texture contains an alpha channel
Romain Guy5cbbce52010-06-27 22:59:20 -0700206 */
Romain Guybd6b79b2010-06-26 00:13:53 -0700207 void drawTextureRect(float left, float top, float right, float bottom, GLuint texture,
Romain Guya9794742010-07-13 11:37:54 -0700208 float alpha, SkXfermode::Mode mode, bool blend);
Romain Guyc7d53492010-06-25 13:41:57 -0700209
Romain Guy026c5e162010-06-28 17:12:22 -0700210 /**
Romain Guy82ba8142010-07-09 13:25:56 -0700211 * 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.
Romain Guy82ba8142010-07-09 13:25:56 -0700220 */
Romain Guya9794742010-07-13 11:37:54 -0700221 void drawTextureRect(float left, float top, float right, float bottom,
222 const Texture* texture, const SkPaint* paint);
Romain Guy82ba8142010-07-09 13:25:56 -0700223
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
Romain Guy82ba8142010-07-09 13:25:56 -0700236 * @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
Romain Guyf7f93552010-07-08 19:17:03 -0700240 */
241 void drawTextureMesh(float left, float top, float right, float bottom, GLuint texture,
Romain Guya9794742010-07-13 11:37:54 -0700242 float alpha, SkXfermode::Mode mode, bool blend,
Romain Guyf7f93552010-07-08 19:17:03 -0700243 GLvoid* vertices, GLvoid* texCoords, GLvoid* indices, GLsizei elementsCount = 0);
244
245 /**
Romain Guyd27977d2010-07-14 19:18:51 -0700246 * 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 /**
Romain Guyc0ac1932010-07-19 18:43:02 -0700259 * 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 /**
Romain Guyac670c02010-07-27 17:39:27 -0700272 * Resets the texture coordinates stored in mMeshVertices. Setting the values
Romain Guy026c5e162010-06-28 17:12:22 -0700273 * back to default is achieved by calling:
274 *
Romain Guy8ba548f2010-06-30 19:21:21 -0700275 * resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guy026c5e162010-06-28 17:12:22 -0700276 *
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
Romain Guy8ba548f2010-06-30 19:21:21 -0700284 /**
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
Romain Guyf7f93552010-07-08 19:17:03 -0700294 /**
Romain Guya1db5742010-07-20 13:09:13 -0700295 * Binds the specified texture with the specified wrap modes.
296 */
297 inline void bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT);
298
299 /**
Romain Guy82ba8142010-07-09 13:25:56 -0700300 * Enable or disable blending as necessary. This function sets the appropriate
301 * blend function based on the specified xfermode.
302 */
Romain Guya9794742010-07-13 11:37:54 -0700303 inline void chooseBlending(bool blend, SkXfermode::Mode mode, bool isPremultiplied = true);
Romain Guy82ba8142010-07-09 13:25:56 -0700304
Romain Guy260e1022010-07-12 14:41:06 -0700305 /**
Romain Guyd27977d2010-07-14 19:18:51 -0700306 * 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.
Romain Guy6926c722010-07-12 20:20:03 -0700310 *
Romain Guyd27977d2010-07-14 19:18:51 -0700311 * @param program The program to use
312 *
313 * @return true If the specified program was already in use, false otherwise.
Romain Guy260e1022010-07-12 14:41:06 -0700314 */
Romain Guyd27977d2010-07-14 19:18:51 -0700315 inline bool useProgram(const sp<Program>& program);
Romain Guy260e1022010-07-12 14:41:06 -0700316
Romain Guybb9524b2010-06-22 18:56:38 -0700317 // Dimensions of the drawing surface
318 int mWidth, mHeight;
319
Romain Guy85bf02f2010-06-22 13:11:24 -0700320 // Matrix used for ortho projection in shaders
Romain Guy260e1022010-07-12 14:41:06 -0700321 mat4 mOrthoMatrix;
Romain Guybb9524b2010-06-22 18:56:38 -0700322
Romain Guyc7d53492010-06-25 13:41:57 -0700323 // Model-view matrix used to position/size objects
324 mat4 mModelView;
325
Romain Guybb9524b2010-06-22 18:56:38 -0700326 // Number of saved states
327 int mSaveCount;
Romain Guyf6a11b82010-06-23 17:47:49 -0700328 // Base state
329 Snapshot mFirstSnapshot;
Romain Guybb9524b2010-06-22 18:56:38 -0700330 // Current state
331 sp<Snapshot> mSnapshot;
Romain Guy9d5316e2010-06-24 19:30:36 -0700332
333 // Shaders
Romain Guyd27977d2010-07-14 19:18:51 -0700334 sp<Program> mCurrentProgram;
335 sp<DrawColorProgram> mDrawColorProgram;
336 sp<DrawTextureProgram> mDrawTextureProgram;
Romain Guy694b5192010-07-21 21:33:20 -0700337 sp<DrawTextProgram> mDrawTextProgram;
Romain Guyf9764a42010-07-16 23:13:33 -0700338 sp<DrawLinearGradientProgram> mDrawLinearGradientProgram;
Romain Guy026c5e162010-06-28 17:12:22 -0700339
340 // Used to draw textured quads
Romain Guyac670c02010-07-27 17:39:27 -0700341 TextureVertex mMeshVertices[4];
Romain Guyce0537b2010-06-29 21:05:21 -0700342
Romain Guy1e793862010-07-16 15:07:42 -0700343 // Current texture state
344 GLuint mLastTexture;
345
Romain Guy82ba8142010-07-09 13:25:56 -0700346 // Last known blend state
347 bool mBlend;
348 GLenum mLastSrcMode;
349 GLenum mLastDstMode;
350
Romain Guy7fac2e12010-07-16 17:10:13 -0700351 // Skia shaders
Romain Guyd27977d2010-07-14 19:18:51 -0700352 ShaderType mShader;
Romain Guyf9764a42010-07-16 23:13:33 -0700353 SkShader* mShaderKey;
Romain Guyd27977d2010-07-14 19:18:51 -0700354 bool mShaderBlend;
Romain Guya1db5742010-07-20 13:09:13 -0700355 GLenum mShaderTileX;
356 GLenum mShaderTileY;
Romain Guyd27977d2010-07-14 19:18:51 -0700357 SkMatrix* mShaderMatrix;
Romain Guy7fac2e12010-07-16 17:10:13 -0700358 // Bitmaps
359 SkBitmap* mShaderBitmap;
360 // Gradients
361 float* mShaderBounds;
362 uint32_t* mShaderColors;
363 float* mShaderPositions;
Romain Guyc0ac1932010-07-19 18:43:02 -0700364 int mShaderCount;
Romain Guyd27977d2010-07-14 19:18:51 -0700365
Romain Guy51769a62010-07-23 00:28:00 -0700366 // GL extensions
367 Extensions mExtensions;
368
Romain Guy694b5192010-07-21 21:33:20 -0700369 // Font renderer
370 FontRenderer mFontRenderer;
371
Romain Guy82ba8142010-07-09 13:25:56 -0700372 // Various caches
Romain Guyce0537b2010-06-29 21:05:21 -0700373 TextureCache mTextureCache;
Romain Guydda57022010-07-06 11:39:32 -0700374 LayerCache mLayerCache;
Romain Guyc0ac1932010-07-19 18:43:02 -0700375 GradientCache mGradientCache;
Romain Guyac670c02010-07-27 17:39:27 -0700376 ProgramCache mProgramCache;
Romain Guyf7f93552010-07-08 19:17:03 -0700377 PatchCache mPatchCache;
Romain Guybb9524b2010-06-22 18:56:38 -0700378}; // class OpenGLRenderer
Romain Guye4d01122010-06-16 18:44:05 -0700379
Romain Guy9d5316e2010-06-24 19:30:36 -0700380}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -0700381}; // namespace android
382
Romain Guy9d5316e2010-06-24 19:30:36 -0700383#endif // ANDROID_UI_OPENGL_RENDERER_H