blob: 9dc2a43ecac7afffa61aa6563e32b57e4d411c3e [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 Guydda57022010-07-06 11:39:32 -070038#include "LayerCache.h"
Romain Guyc0ac1932010-07-19 18:43:02 -070039#include "GradientCache.h"
Romain Guyf7f93552010-07-08 19:17:03 -070040#include "PatchCache.h"
41#include "Vertex.h"
Romain Guybb9524b2010-06-22 18:56:38 -070042
Romain Guye4d01122010-06-16 18:44:05 -070043namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070044namespace uirenderer {
Romain Guye4d01122010-06-16 18:44:05 -070045
Romain Guyf6a11b82010-06-23 17:47:49 -070046///////////////////////////////////////////////////////////////////////////////
47// Support
48///////////////////////////////////////////////////////////////////////////////
49
Romain Guy5cbbce52010-06-27 22:59:20 -070050/**
Romain Guy026c5e162010-06-28 17:12:22 -070051 * Structure mapping Skia xfermodes to OpenGL blending factors.
52 */
53struct Blender {
54 SkXfermode::Mode mode;
55 GLenum src;
56 GLenum dst;
57}; // struct Blender
58
Romain Guyf6a11b82010-06-23 17:47:49 -070059///////////////////////////////////////////////////////////////////////////////
60// Renderer
61///////////////////////////////////////////////////////////////////////////////
62
Romain Guy5cbbce52010-06-27 22:59:20 -070063/**
64 * OpenGL renderer used to draw accelerated 2D graphics. The API is a
65 * simplified version of Skia's Canvas API.
66 */
Romain Guy85bf02f2010-06-22 13:11:24 -070067class OpenGLRenderer {
Romain Guye4d01122010-06-16 18:44:05 -070068public:
Romain Guy85bf02f2010-06-22 13:11:24 -070069 OpenGLRenderer();
70 ~OpenGLRenderer();
Romain Guye4d01122010-06-16 18:44:05 -070071
72 void setViewport(int width, int height);
73 void prepare();
Romain Guy08ae3172010-06-21 19:35:50 -070074
Romain Guybb9524b2010-06-22 18:56:38 -070075 int getSaveCount() const;
76 int save(int flags);
77 void restore();
78 void restoreToCount(int saveCount);
79
Romain Guybd6b79b2010-06-26 00:13:53 -070080 int saveLayer(float left, float top, float right, float bottom, const SkPaint* p, int flags);
81 int saveLayerAlpha(float left, float top, float right, float bottom, int alpha, int flags);
82
Romain Guyf6a11b82010-06-23 17:47:49 -070083 void translate(float dx, float dy);
84 void rotate(float degrees);
85 void scale(float sx, float sy);
86
87 void setMatrix(SkMatrix* matrix);
88 void getMatrix(SkMatrix* matrix);
89 void concatMatrix(SkMatrix* matrix);
90
Romain Guy9d5316e2010-06-24 19:30:36 -070091 const Rect& getClipBounds();
Romain Guyc7d53492010-06-25 13:41:57 -070092 bool quickReject(float left, float top, float right, float bottom);
Romain Guy079ba2c2010-07-16 14:12:24 -070093 bool clipRect(float left, float top, float right, float bottom, SkRegion::Op op);
Romain Guybb9524b2010-06-22 18:56:38 -070094
Romain Guyc1396e92010-06-30 17:56:19 -070095 void drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint);
Romain Guyf86ef572010-07-01 11:05:42 -070096 void drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint);
Romain Guy8ba548f2010-06-30 19:21:21 -070097 void drawBitmap(SkBitmap* bitmap, float srcLeft, float srcTop, float srcRight, float srcBottom,
Romain Guyf86ef572010-07-01 11:05:42 -070098 float dstLeft, float dstTop, float dstRight, float dstBottom, const SkPaint* paint);
Romain Guydeba7852010-07-07 17:54:48 -070099 void drawPatch(SkBitmap* bitmap, Res_png_9patch* patch, float left, float top,
100 float right, float bottom, const SkPaint* paint);
Romain Guy85bf02f2010-06-22 13:11:24 -0700101 void drawColor(int color, SkXfermode::Mode mode);
Romain Guybd6b79b2010-06-26 00:13:53 -0700102 void drawRect(float left, float top, float right, float bottom, const SkPaint* paint);
Romain Guy08ae3172010-06-21 19:35:50 -0700103
Romain Guyd27977d2010-07-14 19:18:51 -0700104 void resetShader();
105 void setupBitmapShader(SkBitmap* bitmap, SkShader::TileMode tileX, SkShader::TileMode tileY,
106 SkMatrix* matrix, bool hasAlpha);
Romain Guyf9764a42010-07-16 23:13:33 -0700107 void setupLinearGradientShader(SkShader* shader, float* bounds, uint32_t* colors,
Romain Guyc0ac1932010-07-19 18:43:02 -0700108 float* positions, int count, SkShader::TileMode tileMode,
109 SkMatrix* matrix, bool hasAlpha);
Romain Guyd27977d2010-07-14 19:18:51 -0700110
Romain Guy85bf02f2010-06-22 13:11:24 -0700111private:
Romain Guy5cbbce52010-06-27 22:59:20 -0700112 /**
Romain Guyd27977d2010-07-14 19:18:51 -0700113 * Type of Skia shader in use.
114 */
115 enum ShaderType {
116 kShaderNone,
117 kShaderBitmap,
118 kShaderLinearGradient,
119 kShaderCircularGradient,
120 kShaderSweepGradient
121 };
122
123 /**
Romain Guy5cbbce52010-06-27 22:59:20 -0700124 * Saves the current state of the renderer as a new snapshot.
125 * The new snapshot is saved in mSnapshot and the previous snapshot
126 * is linked from mSnapshot->previous.
127 *
128 * @return The new save count. This value can be passed to #restoreToCount()
129 */
Romain Guybb9524b2010-06-22 18:56:38 -0700130 int saveSnapshot();
Romain Guy5cbbce52010-06-27 22:59:20 -0700131
132 /**
133 * Restores the current snapshot; mSnapshot becomes mSnapshot->previous.
134 *
135 * @return True if the clip should be also reapplied by calling
136 * #setScissorFromClip().
137 */
Romain Guybb9524b2010-06-22 18:56:38 -0700138 bool restoreSnapshot();
139
Romain Guy5cbbce52010-06-27 22:59:20 -0700140 /**
141 * Sets the clipping rectangle using glScissor. The clip is defined by
142 * the current snapshot's clipRect member.
143 */
Romain Guybb9524b2010-06-22 18:56:38 -0700144 void setScissorFromClip();
145
Romain Guy5cbbce52010-06-27 22:59:20 -0700146 /**
Romain Guyd55a8612010-06-28 17:42:46 -0700147 * Compose the layer defined in the current snapshot with the layer
148 * defined by the previous snapshot.
149 *
150 * The current snapshot *must* be a layer (flag kFlagIsLayer set.)
151 *
152 * @param curent The current snapshot containing the layer to compose
153 * @param previous The previous snapshot to compose the current layer with
154 */
155 void composeLayer(sp<Snapshot> current, sp<Snapshot> previous);
156
157 /**
158 * Creates a new layer stored in the specified snapshot.
159 *
160 * @param snapshot The snapshot associated with the new layer
161 * @param left The left coordinate of the layer
162 * @param top The top coordinate of the layer
163 * @param right The right coordinate of the layer
164 * @param bottom The bottom coordinate of the layer
165 * @param alpha The translucency of the layer
166 * @param mode The blending mode of the layer
167 * @param flags The layer save flags
168 *
169 * @return True if the layer was successfully created, false otherwise
170 */
171 bool createLayer(sp<Snapshot> snapshot, float left, float top, float right, float bottom,
172 int alpha, SkXfermode::Mode mode, int flags);
173
174 /**
Romain Guy5cbbce52010-06-27 22:59:20 -0700175 * Draws a colored rectangle with the specified color. The specified coordinates
176 * are transformed by the current snapshot's transform matrix.
177 *
178 * @param left The left coordinate of the rectangle
179 * @param top The top coordinate of the rectangle
180 * @param right The right coordinate of the rectangle
181 * @param bottom The bottom coordinate of the rectangle
182 * @param color The rectangle's ARGB color, defined as a packed 32 bits word
Romain Guy026c5e162010-06-28 17:12:22 -0700183 * @param mode The Skia xfermode to use
Romain Guy3d58c032010-07-14 16:34:53 -0700184 * @param ignoreTransform True if the current transform should be ignored
Romain Guy5cbbce52010-06-27 22:59:20 -0700185 */
Romain Guy026c5e162010-06-28 17:12:22 -0700186 void drawColorRect(float left, float top, float right, float bottom,
Romain Guy3d58c032010-07-14 16:34:53 -0700187 int color, SkXfermode::Mode mode, bool ignoreTransform = false);
Romain Guy5cbbce52010-06-27 22:59:20 -0700188
189 /**
190 * Draws a textured rectangle with the specified texture. The specified coordinates
191 * are transformed by the current snapshot's transform matrix.
192 *
193 * @param left The left coordinate of the rectangle
194 * @param top The top coordinate of the rectangle
195 * @param right The right coordinate of the rectangle
196 * @param bottom The bottom coordinate of the rectangle
197 * @param texture The texture name to map onto the rectangle
198 * @param alpha An additional translucency parameter, between 0.0f and 1.0f
Romain Guyd55a8612010-06-28 17:42:46 -0700199 * @param mode The blending mode
Romain Guyc1396e92010-06-30 17:56:19 -0700200 * @param blend True if the texture contains an alpha channel
Romain Guy5cbbce52010-06-27 22:59:20 -0700201 */
Romain Guybd6b79b2010-06-26 00:13:53 -0700202 void drawTextureRect(float left, float top, float right, float bottom, GLuint texture,
Romain Guya9794742010-07-13 11:37:54 -0700203 float alpha, SkXfermode::Mode mode, bool blend);
Romain Guyc7d53492010-06-25 13:41:57 -0700204
Romain Guy026c5e162010-06-28 17:12:22 -0700205 /**
Romain Guy82ba8142010-07-09 13:25:56 -0700206 * Draws a textured rectangle with the specified texture. The specified coordinates
207 * are transformed by the current snapshot's transform matrix.
208 *
209 * @param left The left coordinate of the rectangle
210 * @param top The top coordinate of the rectangle
211 * @param right The right coordinate of the rectangle
212 * @param bottom The bottom coordinate of the rectangle
213 * @param texture The texture to use
214 * @param paint The paint containing the alpha, blending mode, etc.
Romain Guy82ba8142010-07-09 13:25:56 -0700215 */
Romain Guya9794742010-07-13 11:37:54 -0700216 void drawTextureRect(float left, float top, float right, float bottom,
217 const Texture* texture, const SkPaint* paint);
Romain Guy82ba8142010-07-09 13:25:56 -0700218
219 /**
220 * Draws a textured mesh with the specified texture. If the indices are omitted, the
221 * mesh is drawn as a simple quad.
222 *
223 * @param left The left coordinate of the rectangle
224 * @param top The top coordinate of the rectangle
225 * @param right The right coordinate of the rectangle
226 * @param bottom The bottom coordinate of the rectangle
227 * @param texture The texture name to map onto the rectangle
228 * @param alpha An additional translucency parameter, between 0.0f and 1.0f
229 * @param mode The blending mode
230 * @param blend True if the texture contains an alpha channel
Romain Guy82ba8142010-07-09 13:25:56 -0700231 * @param vertices The vertices that define the mesh
232 * @param texCoords The texture coordinates of each vertex
233 * @param indices The indices of the vertices, can be NULL
234 * @param elementsCount The number of elements in the mesh, required by indices
Romain Guyf7f93552010-07-08 19:17:03 -0700235 */
236 void drawTextureMesh(float left, float top, float right, float bottom, GLuint texture,
Romain Guya9794742010-07-13 11:37:54 -0700237 float alpha, SkXfermode::Mode mode, bool blend,
Romain Guyf7f93552010-07-08 19:17:03 -0700238 GLvoid* vertices, GLvoid* texCoords, GLvoid* indices, GLsizei elementsCount = 0);
239
240 /**
Romain Guyd27977d2010-07-14 19:18:51 -0700241 * Fills the specified rectangle with the currently set bitmap shader.
242 *
243 * @param left The left coordinate of the rectangle
244 * @param top The top coordinate of the rectangle
245 * @param right The right coordinate of the rectangle
246 * @param bottom The bottom coordinate of the rectangle
247 * @param alpha An additional translucency parameter, between 0.0f and 1.0f
248 * @param mode The blending mode
249 */
250 void drawBitmapShader(float left, float top, float right, float bottom, float alpha,
251 SkXfermode::Mode mode);
252
253 /**
Romain Guyc0ac1932010-07-19 18:43:02 -0700254 * Fills the specified rectangle with the currently set linear gradient shader.
255 *
256 * @param left The left coordinate of the rectangle
257 * @param top The top coordinate of the rectangle
258 * @param right The right coordinate of the rectangle
259 * @param bottom The bottom coordinate of the rectangle
260 * @param alpha An additional translucency parameter, between 0.0f and 1.0f
261 * @param mode The blending mode
262 */
263 void drawLinearGradientShader(float left, float top, float right, float bottom, float alpha,
264 SkXfermode::Mode mode);
265
266 /**
Romain Guy026c5e162010-06-28 17:12:22 -0700267 * Resets the texture coordinates stored in mDrawTextureVertices. Setting the values
268 * back to default is achieved by calling:
269 *
Romain Guy8ba548f2010-06-30 19:21:21 -0700270 * resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guy026c5e162010-06-28 17:12:22 -0700271 *
272 * @param u1 The left coordinate of the texture
273 * @param v1 The bottom coordinate of the texture
274 * @param u2 The right coordinate of the texture
275 * @param v2 The top coordinate of the texture
276 */
277 void resetDrawTextureTexCoords(float u1, float v1, float u2, float v2);
278
Romain Guy8ba548f2010-06-30 19:21:21 -0700279 /**
280 * Gets the alpha and xfermode out of a paint object. If the paint is null
281 * alpha will be 255 and the xfermode will be SRC_OVER.
282 *
283 * @param paint The paint to extract values from
284 * @param alpha Where to store the resulting alpha
285 * @param mode Where to store the resulting xfermode
286 */
287 inline void getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode);
288
Romain Guyf7f93552010-07-08 19:17:03 -0700289 /**
Romain Guya1db5742010-07-20 13:09:13 -0700290 * Binds the specified texture with the specified wrap modes.
291 */
292 inline void bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT);
293
294 /**
Romain Guy82ba8142010-07-09 13:25:56 -0700295 * Enable or disable blending as necessary. This function sets the appropriate
296 * blend function based on the specified xfermode.
297 */
Romain Guya9794742010-07-13 11:37:54 -0700298 inline void chooseBlending(bool blend, SkXfermode::Mode mode, bool isPremultiplied = true);
Romain Guy82ba8142010-07-09 13:25:56 -0700299
Romain Guy260e1022010-07-12 14:41:06 -0700300 /**
Romain Guyd27977d2010-07-14 19:18:51 -0700301 * Use the specified program with the current GL context. If the program is already
302 * in use, it will not be bound again. If it is not in use, the current program is
303 * marked unused and the specified program becomes used and becomes the new
304 * current program.
Romain Guy6926c722010-07-12 20:20:03 -0700305 *
Romain Guyd27977d2010-07-14 19:18:51 -0700306 * @param program The program to use
307 *
308 * @return true If the specified program was already in use, false otherwise.
Romain Guy260e1022010-07-12 14:41:06 -0700309 */
Romain Guyd27977d2010-07-14 19:18:51 -0700310 inline bool useProgram(const sp<Program>& program);
Romain Guy260e1022010-07-12 14:41:06 -0700311
Romain Guybb9524b2010-06-22 18:56:38 -0700312 // Dimensions of the drawing surface
313 int mWidth, mHeight;
314
Romain Guy85bf02f2010-06-22 13:11:24 -0700315 // Matrix used for ortho projection in shaders
Romain Guy260e1022010-07-12 14:41:06 -0700316 mat4 mOrthoMatrix;
Romain Guybb9524b2010-06-22 18:56:38 -0700317
Romain Guyc7d53492010-06-25 13:41:57 -0700318 // Model-view matrix used to position/size objects
319 mat4 mModelView;
320
Romain Guybb9524b2010-06-22 18:56:38 -0700321 // Number of saved states
322 int mSaveCount;
Romain Guyf6a11b82010-06-23 17:47:49 -0700323 // Base state
324 Snapshot mFirstSnapshot;
Romain Guybb9524b2010-06-22 18:56:38 -0700325 // Current state
326 sp<Snapshot> mSnapshot;
Romain Guy9d5316e2010-06-24 19:30:36 -0700327
328 // Shaders
Romain Guyd27977d2010-07-14 19:18:51 -0700329 sp<Program> mCurrentProgram;
330 sp<DrawColorProgram> mDrawColorProgram;
331 sp<DrawTextureProgram> mDrawTextureProgram;
Romain Guyf9764a42010-07-16 23:13:33 -0700332 sp<DrawLinearGradientProgram> mDrawLinearGradientProgram;
Romain Guy026c5e162010-06-28 17:12:22 -0700333
334 // Used to draw textured quads
335 TextureVertex mDrawTextureVertices[4];
Romain Guyce0537b2010-06-29 21:05:21 -0700336
Romain Guy1e793862010-07-16 15:07:42 -0700337 // Current texture state
338 GLuint mLastTexture;
339
Romain Guy82ba8142010-07-09 13:25:56 -0700340 // Last known blend state
341 bool mBlend;
342 GLenum mLastSrcMode;
343 GLenum mLastDstMode;
344
Romain Guy7fac2e12010-07-16 17:10:13 -0700345 // Skia shaders
Romain Guyd27977d2010-07-14 19:18:51 -0700346 ShaderType mShader;
Romain Guyf9764a42010-07-16 23:13:33 -0700347 SkShader* mShaderKey;
Romain Guyd27977d2010-07-14 19:18:51 -0700348 bool mShaderBlend;
Romain Guya1db5742010-07-20 13:09:13 -0700349 GLenum mShaderTileX;
350 GLenum mShaderTileY;
Romain Guyd27977d2010-07-14 19:18:51 -0700351 SkMatrix* mShaderMatrix;
Romain Guy7fac2e12010-07-16 17:10:13 -0700352 // Bitmaps
353 SkBitmap* mShaderBitmap;
354 // Gradients
355 float* mShaderBounds;
356 uint32_t* mShaderColors;
357 float* mShaderPositions;
Romain Guyc0ac1932010-07-19 18:43:02 -0700358 int mShaderCount;
Romain Guyd27977d2010-07-14 19:18:51 -0700359
Romain Guy82ba8142010-07-09 13:25:56 -0700360 // Various caches
Romain Guyce0537b2010-06-29 21:05:21 -0700361 TextureCache mTextureCache;
Romain Guydda57022010-07-06 11:39:32 -0700362 LayerCache mLayerCache;
Romain Guyc0ac1932010-07-19 18:43:02 -0700363 GradientCache mGradientCache;
Romain Guyf7f93552010-07-08 19:17:03 -0700364 PatchCache mPatchCache;
Romain Guybb9524b2010-06-22 18:56:38 -0700365}; // class OpenGLRenderer
Romain Guye4d01122010-06-16 18:44:05 -0700366
Romain Guy9d5316e2010-06-24 19:30:36 -0700367}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -0700368}; // namespace android
369
Romain Guy9d5316e2010-06-24 19:30:36 -0700370#endif // ANDROID_UI_OPENGL_RENDERER_H