blob: 58361c9568f12ff74ed08891c1f6798cec7711bd [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 Guyfb8b7632010-08-23 21:05:08 -070041
42namespace android {
43namespace uirenderer {
44
Romain Guy03750a02010-10-18 14:06:08 -070045///////////////////////////////////////////////////////////////////////////////
46// Globals
47///////////////////////////////////////////////////////////////////////////////
48
49#define REQUIRED_TEXTURE_UNITS_COUNT 3
50
Romain Guy5b3b3522010-10-27 18:57:51 -070051#define REGION_MESH_QUAD_COUNT 512
52
Romain Guy03750a02010-10-18 14:06:08 -070053// Generates simple and textured vertices
54#define FV(x, y, u, v) { { x, y }, { u, v } }
55
56// This array is never used directly but used as a memcpy source in the
57// OpenGLRenderer constructor
58static const TextureVertex gMeshVertices[] = {
59 FV(0.0f, 0.0f, 0.0f, 0.0f),
60 FV(1.0f, 0.0f, 1.0f, 0.0f),
61 FV(0.0f, 1.0f, 0.0f, 1.0f),
62 FV(1.0f, 1.0f, 1.0f, 1.0f)
63};
64static const GLsizei gMeshStride = sizeof(TextureVertex);
Chet Haase5b0200b2011-04-13 17:58:08 -070065static const GLsizei gVertexStride = sizeof(Vertex);
66static const GLsizei gAlphaVertexStride = sizeof(AlphaVertex);
Chet Haase99585ad2011-05-02 15:00:16 -070067static const GLsizei gAAVertexStride = sizeof(AAVertex);
Romain Guy03750a02010-10-18 14:06:08 -070068static const GLsizei gMeshTextureOffset = 2 * sizeof(float);
Chet Haase99585ad2011-05-02 15:00:16 -070069static const GLsizei gVertexAAWidthOffset = 2 * sizeof(float);
70static const GLsizei gVertexAALengthOffset = 3 * sizeof(float);
Romain Guy03750a02010-10-18 14:06:08 -070071static const GLsizei gMeshCount = 4;
72
Romain Guya1d3c912011-12-13 14:55:06 -080073static const GLenum gTextureUnits[] = {
74 GL_TEXTURE0,
75 GL_TEXTURE1,
76 GL_TEXTURE2
77};
78
Romain Guy03750a02010-10-18 14:06:08 -070079///////////////////////////////////////////////////////////////////////////////
80// Debug
81///////////////////////////////////////////////////////////////////////////////
82
Romain Guyfb8b7632010-08-23 21:05:08 -070083struct CacheLogger {
84 CacheLogger() {
Romain Guyd6b2a002011-06-17 17:45:59 -070085 INIT_LOGD("Creating OpenGL renderer caches");
Romain Guyfb8b7632010-08-23 21:05:08 -070086 }
87}; // struct CacheLogger
88
Romain Guy03750a02010-10-18 14:06:08 -070089///////////////////////////////////////////////////////////////////////////////
90// Caches
91///////////////////////////////////////////////////////////////////////////////
92
Romain Guybb0acdf2012-03-05 13:44:35 -080093class DisplayList;
94
Romain Guy79537452011-10-12 13:48:51 -070095class ANDROID_API Caches: public Singleton<Caches> {
Chet Haasedd78cca2010-10-22 18:59:26 -070096 Caches();
Romain Guyfb8b7632010-08-23 21:05:08 -070097
98 friend class Singleton<Caches>;
99
Romain Guye190aa62010-11-10 19:01:29 -0800100 CacheLogger mLogger;
Romain Guy9bca4792010-10-25 18:42:25 -0700101
Romain Guyfb8b7632010-08-23 21:05:08 -0700102public:
Romain Guybdf76092011-07-18 15:00:43 -0700103 enum FlushMode {
Romain Guy6d7475d2011-07-27 16:28:21 -0700104 kFlushMode_Layers = 0,
105 kFlushMode_Moderate,
Romain Guybdf76092011-07-18 15:00:43 -0700106 kFlushMode_Full
107 };
108
109 /**
Romain Guydfa10462012-05-12 16:18:58 -0700110 * Initialize caches.
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800111 */
112 void init();
113
114 /**
Romain Guybdf76092011-07-18 15:00:43 -0700115 * Flush the cache.
116 *
117 * @param mode Indicates how much of the cache should be flushed
118 */
119 void flush(FlushMode mode);
120
Romain Guy5b3b3522010-10-27 18:57:51 -0700121 /**
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800122 * Destroys all resources associated with this cache. This should
123 * be called after a flush(kFlushMode_Full).
124 */
125 void terminate();
126
127 /**
Romain Guye190aa62010-11-10 19:01:29 -0800128 * Indicates whether the renderer is in debug mode.
129 * This debug mode provides limited information to app developers.
130 */
131 DebugLevel getDebugLevel() const {
132 return mDebugLevel;
133 }
134
135 /**
Romain Guyfe48f652010-11-11 15:36:56 -0800136 * Call this on each frame to ensure that garbage is deleted from
137 * GPU memory.
138 */
139 void clearGarbage();
140
141 /**
Romain Guyada830f2011-01-13 12:13:20 -0800142 * Can be used to delete a layer from a non EGL thread.
Romain Guy57066eb2011-01-12 12:53:32 -0800143 */
Romain Guyada830f2011-01-13 12:13:20 -0800144 void deleteLayerDeferred(Layer* layer);
Romain Guy57066eb2011-01-12 12:53:32 -0800145
Romain Guybb0acdf2012-03-05 13:44:35 -0800146 /*
147 * Can be used to delete a display list from a non EGL thread.
148 */
149 void deleteDisplayListDeferred(DisplayList* layer);
150
Romain Guy57066eb2011-01-12 12:53:32 -0800151 /**
Romain Guy5b3b3522010-10-27 18:57:51 -0700152 * Binds the VBO used to render simple textured quads.
153 */
Romain Guyf3a910b42011-12-12 20:35:21 -0800154 bool bindMeshBuffer();
Romain Guy5b3b3522010-10-27 18:57:51 -0700155
156 /**
157 * Binds the specified VBO if needed.
158 */
Romain Guyf3a910b42011-12-12 20:35:21 -0800159 bool bindMeshBuffer(const GLuint buffer);
Romain Guy5b3b3522010-10-27 18:57:51 -0700160
161 /**
162 * Unbinds the VBO used to render simple textured quads.
163 */
Romain Guyf3a910b42011-12-12 20:35:21 -0800164 bool unbindMeshBuffer();
165
Romain Guy15bc6432011-12-13 13:11:32 -0800166 bool bindIndicesBuffer(const GLuint buffer);
167 bool unbindIndicesBuffer();
168
Romain Guyf3a910b42011-12-12 20:35:21 -0800169 /**
170 * Binds an attrib to the specified float vertex pointer.
171 * Assumes a stride of gMeshStride and a size of 2.
172 */
173 void bindPositionVertexPointer(bool force, GLuint slot, GLvoid* vertices,
174 GLsizei stride = gMeshStride);
175
176 /**
177 * Binds an attrib to the specified float vertex pointer.
178 * Assumes a stride of gMeshStride and a size of 2.
179 */
180 void bindTexCoordsVertexPointer(bool force, GLuint slot, GLvoid* vertices);
181
182 /**
183 * Resets the vertex pointers.
184 */
185 void resetVertexPointers();
186 void resetTexCoordsVertexPointer();
Romain Guy03750a02010-10-18 14:06:08 -0700187
Romain Guy15bc6432011-12-13 13:11:32 -0800188 void enableTexCoordsVertexArray();
189 void disbaleTexCoordsVertexArray();
190
Romain Guy5b3b3522010-10-27 18:57:51 -0700191 /**
Romain Guya1d3c912011-12-13 14:55:06 -0800192 * Activate the specified texture unit. The texture unit must
193 * be specified using an integer number (0 for GL_TEXTURE0 etc.)
194 */
195 void activeTexture(GLuint textureUnit);
196
197 /**
Romain Guy8f85e802011-12-14 19:23:32 -0800198 * Sets the scissor for the current surface.
199 */
200 void setScissor(GLint x, GLint y, GLint width, GLint height);
201
202 /**
Romain Guy82bc7a72012-01-03 14:13:39 -0800203 * Resets the scissor state.
204 */
205 void resetScissor();
206
207 /**
Romain Guy5b3b3522010-10-27 18:57:51 -0700208 * Returns the mesh used to draw regions. Calling this method will
209 * bind a VBO of type GL_ELEMENT_ARRAY_BUFFER that contains the
210 * indices for the region mesh.
211 */
212 TextureVertex* getRegionMesh();
213
Romain Guyc15008e2010-11-10 11:59:15 -0800214 /**
215 * Displays the memory usage of each cache and the total sum.
216 */
217 void dumpMemoryUsage();
Chet Haase9c1e23b2011-03-24 10:51:31 -0700218 void dumpMemoryUsage(String8& log);
Romain Guyc15008e2010-11-10 11:59:15 -0800219
Romain Guyfb8b7632010-08-23 21:05:08 -0700220 bool blend;
221 GLenum lastSrcMode;
222 GLenum lastDstMode;
223 Program* currentProgram;
224
Romain Guy746b7402010-10-26 16:27:31 -0700225 // VBO to draw with
Romain Guy03750a02010-10-18 14:06:08 -0700226 GLuint meshBuffer;
Romain Guy03750a02010-10-18 14:06:08 -0700227
Romain Guy746b7402010-10-26 16:27:31 -0700228 // GL extensions
229 Extensions extensions;
230
231 // Misc
232 GLint maxTextureSize;
233
Romain Guyfb8b7632010-08-23 21:05:08 -0700234 TextureCache textureCache;
235 LayerCache layerCache;
236 GradientCache gradientCache;
237 ProgramCache programCache;
238 PathCache pathCache;
Romain Guy01d58e42011-01-19 21:54:02 -0800239 RoundRectShapeCache roundRectShapeCache;
240 CircleShapeCache circleShapeCache;
Romain Guyc1cd9ba32011-01-23 14:18:41 -0800241 OvalShapeCache ovalShapeCache;
242 RectShapeCache rectShapeCache;
Romain Guy8b2f5262011-01-23 16:15:02 -0800243 ArcShapeCache arcShapeCache;
Romain Guyfb8b7632010-08-23 21:05:08 -0700244 PatchCache patchCache;
245 TextDropShadowCache dropShadowCache;
Romain Guye2d345e2010-09-24 18:39:22 -0700246 FboCache fboCache;
Romain Guy11fd6542010-10-04 17:10:58 -0700247 GammaFontRenderer fontRenderer;
Chet Haase5c13d892010-10-08 08:37:55 -0700248 ResourceCache resourceCache;
Romain Guy29d89972010-09-22 16:10:57 -0700249
Romain Guydfa10462012-05-12 16:18:58 -0700250 // Debug methods
Romain Guy13631f32012-01-30 17:41:55 -0800251 PFNGLINSERTEVENTMARKEREXTPROC eventMark;
252 PFNGLPUSHGROUPMARKEREXTPROC startMark;
253 PFNGLPOPGROUPMARKEREXTPROC endMark;
254
Romain Guydfa10462012-05-12 16:18:58 -0700255 PFNGLLABELOBJECTEXTPROC setLabel;
256 PFNGLGETOBJECTLABELEXTPROC getLabel;
257
Romain Guye190aa62010-11-10 19:01:29 -0800258private:
Romain Guydfa10462012-05-12 16:18:58 -0700259 void initExtensions();
260 void initConstraints();
261
262 static void eventMarkNull(GLsizei length, const GLchar* marker) { }
263 static void startMarkNull(GLsizei length, const GLchar* marker) { }
Romain Guy13631f32012-01-30 17:41:55 -0800264 static void endMarkNull() { }
265
Romain Guydfa10462012-05-12 16:18:58 -0700266 static void setLabelNull(GLenum type, uint object, GLsizei length,
267 const char* label) { }
268 static void getLabelNull(GLenum type, uint object, GLsizei bufferSize,
269 GLsizei* length, char* label) {
270 if (length) *length = 0;
271 if (label) *label = '\0';
272 }
273
Romain Guyf3a910b42011-12-12 20:35:21 -0800274 GLuint mCurrentBuffer;
Romain Guy15bc6432011-12-13 13:11:32 -0800275 GLuint mCurrentIndicesBuffer;
Romain Guyf3a910b42011-12-12 20:35:21 -0800276 void* mCurrentPositionPointer;
277 void* mCurrentTexCoordsPointer;
278
Romain Guy15bc6432011-12-13 13:11:32 -0800279 bool mTexCoordsArrayEnabled;
280
Romain Guya1d3c912011-12-13 14:55:06 -0800281 GLuint mTextureUnit;
282
Romain Guy8f85e802011-12-14 19:23:32 -0800283 GLint mScissorX;
284 GLint mScissorY;
285 GLint mScissorWidth;
286 GLint mScissorHeight;
287
Romain Guyf3a910b42011-12-12 20:35:21 -0800288 // Used to render layers
289 TextureVertex* mRegionMesh;
290 GLuint mRegionMeshIndices;
291
292 mutable Mutex mGarbageLock;
293 Vector<Layer*> mLayerGarbage;
Romain Guybb0acdf2012-03-05 13:44:35 -0800294 Vector<DisplayList*> mDisplayListGarbage;
Romain Guyf3a910b42011-12-12 20:35:21 -0800295
Romain Guye190aa62010-11-10 19:01:29 -0800296 DebugLevel mDebugLevel;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800297 bool mInitialized;
Romain Guyfb8b7632010-08-23 21:05:08 -0700298}; // class Caches
299
300}; // namespace uirenderer
Romain Guyfb8b7632010-08-23 21:05:08 -0700301}; // namespace android
302
Romain Guy5b3b3522010-10-27 18:57:51 -0700303#endif // ANDROID_HWUI_CACHES_H