blob: f4f56d6e594bfb8dabeefd9586c555978c3a272b [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);
Chet Haase99585ad2011-05-02 15:00:16 -070070static const GLsizei gVertexAAWidthOffset = 2 * sizeof(float);
71static const GLsizei gVertexAALengthOffset = 3 * sizeof(float);
Romain Guy03750a02010-10-18 14:06:08 -070072static const GLsizei gMeshCount = 4;
73
Romain Guya1d3c912011-12-13 14:55:06 -080074static const GLenum gTextureUnits[] = {
75 GL_TEXTURE0,
76 GL_TEXTURE1,
77 GL_TEXTURE2
78};
79
Romain Guy03750a02010-10-18 14:06:08 -070080///////////////////////////////////////////////////////////////////////////////
81// Debug
82///////////////////////////////////////////////////////////////////////////////
83
Romain Guyfb8b7632010-08-23 21:05:08 -070084struct CacheLogger {
85 CacheLogger() {
Romain Guyd6b2a002011-06-17 17:45:59 -070086 INIT_LOGD("Creating OpenGL renderer caches");
Romain Guyfb8b7632010-08-23 21:05:08 -070087 }
88}; // struct CacheLogger
89
Romain Guy03750a02010-10-18 14:06:08 -070090///////////////////////////////////////////////////////////////////////////////
91// Caches
92///////////////////////////////////////////////////////////////////////////////
93
Romain Guybb0acdf2012-03-05 13:44:35 -080094class DisplayList;
95
Romain Guy79537452011-10-12 13:48:51 -070096class ANDROID_API Caches: public Singleton<Caches> {
Chet Haasedd78cca2010-10-22 18:59:26 -070097 Caches();
Romain Guyfb8b7632010-08-23 21:05:08 -070098
99 friend class Singleton<Caches>;
100
Romain Guye190aa62010-11-10 19:01:29 -0800101 CacheLogger mLogger;
Romain Guy9bca4792010-10-25 18:42:25 -0700102
Romain Guyfb8b7632010-08-23 21:05:08 -0700103public:
Romain Guybdf76092011-07-18 15:00:43 -0700104 enum FlushMode {
Romain Guy6d7475d2011-07-27 16:28:21 -0700105 kFlushMode_Layers = 0,
106 kFlushMode_Moderate,
Romain Guybdf76092011-07-18 15:00:43 -0700107 kFlushMode_Full
108 };
109
110 /**
Romain Guydfa10462012-05-12 16:18:58 -0700111 * Initialize caches.
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800112 */
113 void init();
114
115 /**
Romain Guybdf76092011-07-18 15:00:43 -0700116 * Flush the cache.
117 *
118 * @param mode Indicates how much of the cache should be flushed
119 */
120 void flush(FlushMode mode);
121
Romain Guy5b3b3522010-10-27 18:57:51 -0700122 /**
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800123 * Destroys all resources associated with this cache. This should
124 * be called after a flush(kFlushMode_Full).
125 */
126 void terminate();
127
128 /**
Romain Guye190aa62010-11-10 19:01:29 -0800129 * Indicates whether the renderer is in debug mode.
130 * This debug mode provides limited information to app developers.
131 */
132 DebugLevel getDebugLevel() const {
133 return mDebugLevel;
134 }
135
136 /**
Romain Guyfe48f652010-11-11 15:36:56 -0800137 * Call this on each frame to ensure that garbage is deleted from
138 * GPU memory.
139 */
140 void clearGarbage();
141
142 /**
Romain Guyada830f2011-01-13 12:13:20 -0800143 * Can be used to delete a layer from a non EGL thread.
Romain Guy57066eb2011-01-12 12:53:32 -0800144 */
Romain Guyada830f2011-01-13 12:13:20 -0800145 void deleteLayerDeferred(Layer* layer);
Romain Guy57066eb2011-01-12 12:53:32 -0800146
Romain Guybb0acdf2012-03-05 13:44:35 -0800147 /*
148 * Can be used to delete a display list from a non EGL thread.
149 */
150 void deleteDisplayListDeferred(DisplayList* layer);
151
Romain Guy57066eb2011-01-12 12:53:32 -0800152 /**
Romain Guy5b3b3522010-10-27 18:57:51 -0700153 * Binds the VBO used to render simple textured quads.
154 */
Romain Guyf3a910b42011-12-12 20:35:21 -0800155 bool bindMeshBuffer();
Romain Guy5b3b3522010-10-27 18:57:51 -0700156
157 /**
158 * Binds the specified VBO if needed.
159 */
Romain Guyf3a910b42011-12-12 20:35:21 -0800160 bool bindMeshBuffer(const GLuint buffer);
Romain Guy5b3b3522010-10-27 18:57:51 -0700161
162 /**
163 * Unbinds the VBO used to render simple textured quads.
164 */
Romain Guyf3a910b42011-12-12 20:35:21 -0800165 bool unbindMeshBuffer();
166
Romain Guy15bc6432011-12-13 13:11:32 -0800167 bool bindIndicesBuffer(const GLuint buffer);
168 bool unbindIndicesBuffer();
169
Romain Guyf3a910b42011-12-12 20:35:21 -0800170 /**
171 * Binds an attrib to the specified float vertex pointer.
172 * Assumes a stride of gMeshStride and a size of 2.
173 */
174 void bindPositionVertexPointer(bool force, GLuint slot, GLvoid* vertices,
175 GLsizei stride = gMeshStride);
176
177 /**
178 * Binds an attrib to the specified float vertex pointer.
179 * Assumes a stride of gMeshStride and a size of 2.
180 */
181 void bindTexCoordsVertexPointer(bool force, GLuint slot, GLvoid* vertices);
182
183 /**
184 * Resets the vertex pointers.
185 */
186 void resetVertexPointers();
187 void resetTexCoordsVertexPointer();
Romain Guy03750a02010-10-18 14:06:08 -0700188
Romain Guy15bc6432011-12-13 13:11:32 -0800189 void enableTexCoordsVertexArray();
190 void disbaleTexCoordsVertexArray();
191
Romain Guy5b3b3522010-10-27 18:57:51 -0700192 /**
Romain Guya1d3c912011-12-13 14:55:06 -0800193 * Activate the specified texture unit. The texture unit must
194 * be specified using an integer number (0 for GL_TEXTURE0 etc.)
195 */
196 void activeTexture(GLuint textureUnit);
197
198 /**
Romain Guy8f85e802011-12-14 19:23:32 -0800199 * Sets the scissor for the current surface.
200 */
Romain Guy8a4ac612012-07-17 17:32:48 -0700201 bool setScissor(GLint x, GLint y, GLint width, GLint height);
Romain Guy8f85e802011-12-14 19:23:32 -0800202
203 /**
Romain Guy82bc7a72012-01-03 14:13:39 -0800204 * Resets the scissor state.
205 */
206 void resetScissor();
207
Romain Guy8a4ac612012-07-17 17:32:48 -0700208 bool enableScissor();
209 bool disableScissor();
Romain Guy586cae32012-07-13 15:28:31 -0700210 void setScissorEnabled(bool enabled);
211
Romain Guy82bc7a72012-01-03 14:13:39 -0800212 /**
Romain Guy5b3b3522010-10-27 18:57:51 -0700213 * Returns the mesh used to draw regions. Calling this method will
214 * bind a VBO of type GL_ELEMENT_ARRAY_BUFFER that contains the
215 * indices for the region mesh.
216 */
217 TextureVertex* getRegionMesh();
218
Romain Guyc15008e2010-11-10 11:59:15 -0800219 /**
220 * Displays the memory usage of each cache and the total sum.
221 */
222 void dumpMemoryUsage();
Chet Haase9c1e23b2011-03-24 10:51:31 -0700223 void dumpMemoryUsage(String8& log);
Romain Guyc15008e2010-11-10 11:59:15 -0800224
Romain Guyfb8b7632010-08-23 21:05:08 -0700225 bool blend;
226 GLenum lastSrcMode;
227 GLenum lastDstMode;
228 Program* currentProgram;
Romain Guy586cae32012-07-13 15:28:31 -0700229 bool scissorEnabled;
Romain Guyfb8b7632010-08-23 21:05:08 -0700230
Romain Guy746b7402010-10-26 16:27:31 -0700231 // VBO to draw with
Romain Guy03750a02010-10-18 14:06:08 -0700232 GLuint meshBuffer;
Romain Guy03750a02010-10-18 14:06:08 -0700233
Romain Guy746b7402010-10-26 16:27:31 -0700234 // GL extensions
235 Extensions extensions;
236
237 // Misc
238 GLint maxTextureSize;
Romain Guy4ff0cf42012-08-06 14:51:10 -0700239 bool debugLayersUpdates;
Romain Guy746b7402010-10-26 16:27:31 -0700240
Romain Guyfb8b7632010-08-23 21:05:08 -0700241 TextureCache textureCache;
242 LayerCache layerCache;
243 GradientCache gradientCache;
244 ProgramCache programCache;
245 PathCache pathCache;
Romain Guy01d58e42011-01-19 21:54:02 -0800246 RoundRectShapeCache roundRectShapeCache;
247 CircleShapeCache circleShapeCache;
Romain Guyc1cd9ba32011-01-23 14:18:41 -0800248 OvalShapeCache ovalShapeCache;
249 RectShapeCache rectShapeCache;
Romain Guy8b2f5262011-01-23 16:15:02 -0800250 ArcShapeCache arcShapeCache;
Romain Guyfb8b7632010-08-23 21:05:08 -0700251 PatchCache patchCache;
252 TextDropShadowCache dropShadowCache;
Romain Guye2d345e2010-09-24 18:39:22 -0700253 FboCache fboCache;
Chet Haase5c13d892010-10-08 08:37:55 -0700254 ResourceCache resourceCache;
Romain Guy211efea2012-07-31 21:16:07 -0700255 Dither dither;
Romain Guy29d89972010-09-22 16:10:57 -0700256
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700257 GammaFontRenderer* fontRenderer;
258
Romain Guydfa10462012-05-12 16:18:58 -0700259 // Debug methods
Romain Guy13631f32012-01-30 17:41:55 -0800260 PFNGLINSERTEVENTMARKEREXTPROC eventMark;
261 PFNGLPUSHGROUPMARKEREXTPROC startMark;
262 PFNGLPOPGROUPMARKEREXTPROC endMark;
263
Romain Guydfa10462012-05-12 16:18:58 -0700264 PFNGLLABELOBJECTEXTPROC setLabel;
265 PFNGLGETOBJECTLABELEXTPROC getLabel;
266
Romain Guye190aa62010-11-10 19:01:29 -0800267private:
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700268 void initFont();
Romain Guydfa10462012-05-12 16:18:58 -0700269 void initExtensions();
270 void initConstraints();
Romain Guy4ff0cf42012-08-06 14:51:10 -0700271 void initProperties();
Romain Guydfa10462012-05-12 16:18:58 -0700272
273 static void eventMarkNull(GLsizei length, const GLchar* marker) { }
274 static void startMarkNull(GLsizei length, const GLchar* marker) { }
Romain Guy13631f32012-01-30 17:41:55 -0800275 static void endMarkNull() { }
276
Romain Guydfa10462012-05-12 16:18:58 -0700277 static void setLabelNull(GLenum type, uint object, GLsizei length,
278 const char* label) { }
279 static void getLabelNull(GLenum type, uint object, GLsizei bufferSize,
280 GLsizei* length, char* label) {
281 if (length) *length = 0;
282 if (label) *label = '\0';
283 }
284
Romain Guyf3a910b42011-12-12 20:35:21 -0800285 GLuint mCurrentBuffer;
Romain Guy15bc6432011-12-13 13:11:32 -0800286 GLuint mCurrentIndicesBuffer;
Romain Guyf3a910b42011-12-12 20:35:21 -0800287 void* mCurrentPositionPointer;
288 void* mCurrentTexCoordsPointer;
289
Romain Guy15bc6432011-12-13 13:11:32 -0800290 bool mTexCoordsArrayEnabled;
291
Romain Guya1d3c912011-12-13 14:55:06 -0800292 GLuint mTextureUnit;
293
Romain Guy8f85e802011-12-14 19:23:32 -0800294 GLint mScissorX;
295 GLint mScissorY;
296 GLint mScissorWidth;
297 GLint mScissorHeight;
298
Romain Guyf3a910b42011-12-12 20:35:21 -0800299 // Used to render layers
300 TextureVertex* mRegionMesh;
301 GLuint mRegionMeshIndices;
302
303 mutable Mutex mGarbageLock;
304 Vector<Layer*> mLayerGarbage;
Romain Guybb0acdf2012-03-05 13:44:35 -0800305 Vector<DisplayList*> mDisplayListGarbage;
Romain Guyf3a910b42011-12-12 20:35:21 -0800306
Romain Guye190aa62010-11-10 19:01:29 -0800307 DebugLevel mDebugLevel;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800308 bool mInitialized;
Romain Guyfb8b7632010-08-23 21:05:08 -0700309}; // class Caches
310
311}; // namespace uirenderer
Romain Guyfb8b7632010-08-23 21:05:08 -0700312}; // namespace android
313
Romain Guy5b3b3522010-10-27 18:57:51 -0700314#endif // ANDROID_HWUI_CACHES_H