blob: 23d90188aceaa440035ed0d2f3ccbbc3d575b309 [file] [log] [blame]
Romain Guyfb8b7632010-08-23 21:05:08 -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 Guy5b3b3522010-10-27 18:57:51 -070017#ifndef ANDROID_HWUI_CACHES_H
18#define ANDROID_HWUI_CACHES_H
Romain Guyfb8b7632010-08-23 21:05:08 -070019
Romain Guya2341a92010-09-08 18:04:33 -070020#ifndef LOG_TAG
21 #define LOG_TAG "OpenGLRenderer"
22#endif
Romain Guyfb8b7632010-08-23 21:05:08 -070023
24#include <utils/Singleton.h>
25
Romain Guy79537452011-10-12 13:48:51 -070026#include <cutils/compiler.h>
27
Romain Guy746b7402010-10-26 16:27:31 -070028#include "Extensions.h"
Romain Guy03750a02010-10-18 14:06:08 -070029#include "FontRenderer.h"
30#include "GammaFontRenderer.h"
Romain Guyfb8b7632010-08-23 21:05:08 -070031#include "TextureCache.h"
32#include "LayerCache.h"
33#include "GradientCache.h"
34#include "PatchCache.h"
Romain Guyfb8b7632010-08-23 21:05:08 -070035#include "ProgramCache.h"
Romain Guy01d58e42011-01-19 21:54:02 -080036#include "ShapeCache.h"
Romain Guyff26a0c2011-01-20 11:35:46 -080037#include "PathCache.h"
Romain Guyfb8b7632010-08-23 21:05:08 -070038#include "TextDropShadowCache.h"
Romain Guye2d345e2010-09-24 18:39:22 -070039#include "FboCache.h"
Chet Haase5c13d892010-10-08 08:37:55 -070040#include "ResourceCache.h"
Romain Guy211efea2012-07-31 21:16:07 -070041#include "Dither.h"
Romain Guyfb8b7632010-08-23 21:05:08 -070042
43namespace android {
44namespace uirenderer {
45
Romain Guy03750a02010-10-18 14:06:08 -070046///////////////////////////////////////////////////////////////////////////////
47// Globals
48///////////////////////////////////////////////////////////////////////////////
49
50#define REQUIRED_TEXTURE_UNITS_COUNT 3
51
Romain Guy5b3b3522010-10-27 18:57:51 -070052#define REGION_MESH_QUAD_COUNT 512
53
Romain Guy03750a02010-10-18 14:06:08 -070054// Generates simple and textured vertices
55#define FV(x, y, u, v) { { x, y }, { u, v } }
56
57// This array is never used directly but used as a memcpy source in the
58// OpenGLRenderer constructor
59static const TextureVertex gMeshVertices[] = {
60 FV(0.0f, 0.0f, 0.0f, 0.0f),
61 FV(1.0f, 0.0f, 1.0f, 0.0f),
62 FV(0.0f, 1.0f, 0.0f, 1.0f),
63 FV(1.0f, 1.0f, 1.0f, 1.0f)
64};
65static const GLsizei gMeshStride = sizeof(TextureVertex);
Chet Haase5b0200b2011-04-13 17:58:08 -070066static const GLsizei gVertexStride = sizeof(Vertex);
67static const GLsizei gAlphaVertexStride = sizeof(AlphaVertex);
Chet Haase99585ad2011-05-02 15:00:16 -070068static const GLsizei gAAVertexStride = sizeof(AAVertex);
Romain Guy03750a02010-10-18 14:06:08 -070069static const GLsizei gMeshTextureOffset = 2 * sizeof(float);
Chris Craik6ebdc112012-08-31 18:24:33 -070070static const GLsizei gVertexAlphaOffset = 2 * sizeof(float);
Chet Haase99585ad2011-05-02 15:00:16 -070071static const GLsizei gVertexAAWidthOffset = 2 * sizeof(float);
72static const GLsizei gVertexAALengthOffset = 3 * sizeof(float);
Romain Guy03750a02010-10-18 14:06:08 -070073static const GLsizei gMeshCount = 4;
74
Romain Guya1d3c912011-12-13 14:55:06 -080075static const GLenum gTextureUnits[] = {
76 GL_TEXTURE0,
77 GL_TEXTURE1,
78 GL_TEXTURE2
79};
80
Romain Guy03750a02010-10-18 14:06:08 -070081///////////////////////////////////////////////////////////////////////////////
82// Debug
83///////////////////////////////////////////////////////////////////////////////
84
Romain Guyfb8b7632010-08-23 21:05:08 -070085struct CacheLogger {
86 CacheLogger() {
Romain Guyd6b2a002011-06-17 17:45:59 -070087 INIT_LOGD("Creating OpenGL renderer caches");
Romain Guyfb8b7632010-08-23 21:05:08 -070088 }
89}; // struct CacheLogger
90
Romain Guy03750a02010-10-18 14:06:08 -070091///////////////////////////////////////////////////////////////////////////////
92// Caches
93///////////////////////////////////////////////////////////////////////////////
94
Romain Guybb0acdf2012-03-05 13:44:35 -080095class DisplayList;
96
Romain Guy79537452011-10-12 13:48:51 -070097class ANDROID_API Caches: public Singleton<Caches> {
Chet Haasedd78cca2010-10-22 18:59:26 -070098 Caches();
Romain Guyfb8b7632010-08-23 21:05:08 -070099
100 friend class Singleton<Caches>;
101
Romain Guye190aa62010-11-10 19:01:29 -0800102 CacheLogger mLogger;
Romain Guy9bca4792010-10-25 18:42:25 -0700103
Romain Guyfb8b7632010-08-23 21:05:08 -0700104public:
Romain Guybdf76092011-07-18 15:00:43 -0700105 enum FlushMode {
Romain Guy6d7475d2011-07-27 16:28:21 -0700106 kFlushMode_Layers = 0,
107 kFlushMode_Moderate,
Romain Guybdf76092011-07-18 15:00:43 -0700108 kFlushMode_Full
109 };
110
111 /**
Romain Guydfa10462012-05-12 16:18:58 -0700112 * Initialize caches.
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800113 */
114 void init();
115
116 /**
Romain Guybdf76092011-07-18 15:00:43 -0700117 * Flush the cache.
118 *
119 * @param mode Indicates how much of the cache should be flushed
120 */
121 void flush(FlushMode mode);
122
Romain Guy5b3b3522010-10-27 18:57:51 -0700123 /**
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800124 * Destroys all resources associated with this cache. This should
125 * be called after a flush(kFlushMode_Full).
126 */
127 void terminate();
128
129 /**
Romain Guye190aa62010-11-10 19:01:29 -0800130 * Indicates whether the renderer is in debug mode.
131 * This debug mode provides limited information to app developers.
132 */
133 DebugLevel getDebugLevel() const {
134 return mDebugLevel;
135 }
136
137 /**
Romain Guyfe48f652010-11-11 15:36:56 -0800138 * Call this on each frame to ensure that garbage is deleted from
139 * GPU memory.
140 */
141 void clearGarbage();
142
143 /**
Romain Guyada830f2011-01-13 12:13:20 -0800144 * Can be used to delete a layer from a non EGL thread.
Romain Guy57066eb2011-01-12 12:53:32 -0800145 */
Romain Guyada830f2011-01-13 12:13:20 -0800146 void deleteLayerDeferred(Layer* layer);
Romain Guy57066eb2011-01-12 12:53:32 -0800147
Romain Guybb0acdf2012-03-05 13:44:35 -0800148 /*
149 * Can be used to delete a display list from a non EGL thread.
150 */
151 void deleteDisplayListDeferred(DisplayList* layer);
152
Romain Guy57066eb2011-01-12 12:53:32 -0800153 /**
Romain Guy5b3b3522010-10-27 18:57:51 -0700154 * Binds the VBO used to render simple textured quads.
155 */
Romain Guyf3a910b42011-12-12 20:35:21 -0800156 bool bindMeshBuffer();
Romain Guy5b3b3522010-10-27 18:57:51 -0700157
158 /**
159 * Binds the specified VBO if needed.
160 */
Romain Guyf3a910b42011-12-12 20:35:21 -0800161 bool bindMeshBuffer(const GLuint buffer);
Romain Guy5b3b3522010-10-27 18:57:51 -0700162
163 /**
164 * Unbinds the VBO used to render simple textured quads.
165 */
Romain Guyf3a910b42011-12-12 20:35:21 -0800166 bool unbindMeshBuffer();
167
Romain Guy15bc6432011-12-13 13:11:32 -0800168 bool bindIndicesBuffer(const GLuint buffer);
169 bool unbindIndicesBuffer();
170
Romain Guyf3a910b42011-12-12 20:35:21 -0800171 /**
172 * Binds an attrib to the specified float vertex pointer.
173 * Assumes a stride of gMeshStride and a size of 2.
174 */
175 void bindPositionVertexPointer(bool force, GLuint slot, GLvoid* vertices,
176 GLsizei stride = gMeshStride);
177
178 /**
179 * Binds an attrib to the specified float vertex pointer.
180 * Assumes a stride of gMeshStride and a size of 2.
181 */
182 void bindTexCoordsVertexPointer(bool force, GLuint slot, GLvoid* vertices);
183
184 /**
185 * Resets the vertex pointers.
186 */
187 void resetVertexPointers();
188 void resetTexCoordsVertexPointer();
Romain Guy03750a02010-10-18 14:06:08 -0700189
Romain Guy15bc6432011-12-13 13:11:32 -0800190 void enableTexCoordsVertexArray();
191 void disbaleTexCoordsVertexArray();
192
Romain Guy5b3b3522010-10-27 18:57:51 -0700193 /**
Romain Guya1d3c912011-12-13 14:55:06 -0800194 * Activate the specified texture unit. The texture unit must
195 * be specified using an integer number (0 for GL_TEXTURE0 etc.)
196 */
197 void activeTexture(GLuint textureUnit);
198
199 /**
Romain Guy8f85e802011-12-14 19:23:32 -0800200 * Sets the scissor for the current surface.
201 */
Romain Guy8a4ac612012-07-17 17:32:48 -0700202 bool setScissor(GLint x, GLint y, GLint width, GLint height);
Romain Guy8f85e802011-12-14 19:23:32 -0800203
204 /**
Romain Guy82bc7a72012-01-03 14:13:39 -0800205 * Resets the scissor state.
206 */
207 void resetScissor();
208
Romain Guy8a4ac612012-07-17 17:32:48 -0700209 bool enableScissor();
210 bool disableScissor();
Romain Guy586cae32012-07-13 15:28:31 -0700211 void setScissorEnabled(bool enabled);
212
Romain Guy82bc7a72012-01-03 14:13:39 -0800213 /**
Romain Guy5b3b3522010-10-27 18:57:51 -0700214 * Returns the mesh used to draw regions. Calling this method will
215 * bind a VBO of type GL_ELEMENT_ARRAY_BUFFER that contains the
216 * indices for the region mesh.
217 */
218 TextureVertex* getRegionMesh();
219
Romain Guyc15008e2010-11-10 11:59:15 -0800220 /**
221 * Displays the memory usage of each cache and the total sum.
222 */
223 void dumpMemoryUsage();
Chet Haase9c1e23b2011-03-24 10:51:31 -0700224 void dumpMemoryUsage(String8& log);
Romain Guyc15008e2010-11-10 11:59:15 -0800225
Romain Guyfb8b7632010-08-23 21:05:08 -0700226 bool blend;
227 GLenum lastSrcMode;
228 GLenum lastDstMode;
229 Program* currentProgram;
Romain Guy586cae32012-07-13 15:28:31 -0700230 bool scissorEnabled;
Romain Guyfb8b7632010-08-23 21:05:08 -0700231
Romain Guy746b7402010-10-26 16:27:31 -0700232 // VBO to draw with
Romain Guy03750a02010-10-18 14:06:08 -0700233 GLuint meshBuffer;
Romain Guy03750a02010-10-18 14:06:08 -0700234
Romain Guy746b7402010-10-26 16:27:31 -0700235 // GL extensions
236 Extensions extensions;
237
238 // Misc
239 GLint maxTextureSize;
Romain Guy4ff0cf42012-08-06 14:51:10 -0700240 bool debugLayersUpdates;
Romain Guy746b7402010-10-26 16:27:31 -0700241
Romain Guyfb8b7632010-08-23 21:05:08 -0700242 TextureCache textureCache;
243 LayerCache layerCache;
244 GradientCache gradientCache;
245 ProgramCache programCache;
246 PathCache pathCache;
Romain Guy01d58e42011-01-19 21:54:02 -0800247 RoundRectShapeCache roundRectShapeCache;
248 CircleShapeCache circleShapeCache;
Romain Guyc1cd9ba2011-01-23 14:18:41 -0800249 OvalShapeCache ovalShapeCache;
250 RectShapeCache rectShapeCache;
Romain Guy8b2f5262011-01-23 16:15:02 -0800251 ArcShapeCache arcShapeCache;
Romain Guyfb8b7632010-08-23 21:05:08 -0700252 PatchCache patchCache;
253 TextDropShadowCache dropShadowCache;
Romain Guye2d345e2010-09-24 18:39:22 -0700254 FboCache fboCache;
Chet Haase5c13d892010-10-08 08:37:55 -0700255 ResourceCache resourceCache;
Romain Guy211efea2012-07-31 21:16:07 -0700256 Dither dither;
Romain Guy29d89972010-09-22 16:10:57 -0700257
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700258 GammaFontRenderer* fontRenderer;
259
Romain Guydfa10462012-05-12 16:18:58 -0700260 // Debug methods
Romain Guy13631f32012-01-30 17:41:55 -0800261 PFNGLINSERTEVENTMARKEREXTPROC eventMark;
262 PFNGLPUSHGROUPMARKEREXTPROC startMark;
263 PFNGLPOPGROUPMARKEREXTPROC endMark;
264
Romain Guydfa10462012-05-12 16:18:58 -0700265 PFNGLLABELOBJECTEXTPROC setLabel;
266 PFNGLGETOBJECTLABELEXTPROC getLabel;
267
Romain Guye190aa62010-11-10 19:01:29 -0800268private:
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700269 void initFont();
Romain Guydfa10462012-05-12 16:18:58 -0700270 void initExtensions();
271 void initConstraints();
Romain Guy4ff0cf42012-08-06 14:51:10 -0700272 void initProperties();
Romain Guydfa10462012-05-12 16:18:58 -0700273
274 static void eventMarkNull(GLsizei length, const GLchar* marker) { }
275 static void startMarkNull(GLsizei length, const GLchar* marker) { }
Romain Guy13631f32012-01-30 17:41:55 -0800276 static void endMarkNull() { }
277
Romain Guydfa10462012-05-12 16:18:58 -0700278 static void setLabelNull(GLenum type, uint object, GLsizei length,
279 const char* label) { }
280 static void getLabelNull(GLenum type, uint object, GLsizei bufferSize,
281 GLsizei* length, char* label) {
282 if (length) *length = 0;
283 if (label) *label = '\0';
284 }
285
Romain Guyf3a910b42011-12-12 20:35:21 -0800286 GLuint mCurrentBuffer;
Romain Guy15bc6432011-12-13 13:11:32 -0800287 GLuint mCurrentIndicesBuffer;
Romain Guyf3a910b42011-12-12 20:35:21 -0800288 void* mCurrentPositionPointer;
289 void* mCurrentTexCoordsPointer;
290
Romain Guy15bc6432011-12-13 13:11:32 -0800291 bool mTexCoordsArrayEnabled;
292
Romain Guya1d3c912011-12-13 14:55:06 -0800293 GLuint mTextureUnit;
294
Romain Guy8f85e802011-12-14 19:23:32 -0800295 GLint mScissorX;
296 GLint mScissorY;
297 GLint mScissorWidth;
298 GLint mScissorHeight;
299
Romain Guyf3a910b42011-12-12 20:35:21 -0800300 // Used to render layers
301 TextureVertex* mRegionMesh;
302 GLuint mRegionMeshIndices;
303
304 mutable Mutex mGarbageLock;
305 Vector<Layer*> mLayerGarbage;
Romain Guybb0acdf2012-03-05 13:44:35 -0800306 Vector<DisplayList*> mDisplayListGarbage;
Romain Guyf3a910b42011-12-12 20:35:21 -0800307
Romain Guye190aa62010-11-10 19:01:29 -0800308 DebugLevel mDebugLevel;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800309 bool mInitialized;
Romain Guyfb8b7632010-08-23 21:05:08 -0700310}; // class Caches
311
312}; // namespace uirenderer
Romain Guyfb8b7632010-08-23 21:05:08 -0700313}; // namespace android
314
Romain Guy5b3b3522010-10-27 18:57:51 -0700315#endif // ANDROID_HWUI_CACHES_H