blob: 1c4d05f2b51dbddbd4f81e76ef541889a889b0f3 [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 Guy0baaac52012-08-31 20:31:01 -070041#include "Stencil.h"
Romain Guy211efea2012-07-31 21:16:07 -070042#include "Dither.h"
Romain Guyfb8b7632010-08-23 21:05:08 -070043
44namespace android {
45namespace uirenderer {
46
Romain Guy03750a02010-10-18 14:06:08 -070047///////////////////////////////////////////////////////////////////////////////
48// Globals
49///////////////////////////////////////////////////////////////////////////////
50
51#define REQUIRED_TEXTURE_UNITS_COUNT 3
52
Romain Guy5b3b3522010-10-27 18:57:51 -070053#define REGION_MESH_QUAD_COUNT 512
54
Romain Guy03750a02010-10-18 14:06:08 -070055// Generates simple and textured vertices
56#define FV(x, y, u, v) { { x, y }, { u, v } }
57
58// This array is never used directly but used as a memcpy source in the
59// OpenGLRenderer constructor
60static const TextureVertex gMeshVertices[] = {
61 FV(0.0f, 0.0f, 0.0f, 0.0f),
62 FV(1.0f, 0.0f, 1.0f, 0.0f),
63 FV(0.0f, 1.0f, 0.0f, 1.0f),
64 FV(1.0f, 1.0f, 1.0f, 1.0f)
65};
66static const GLsizei gMeshStride = sizeof(TextureVertex);
Chet Haase5b0200b2011-04-13 17:58:08 -070067static const GLsizei gVertexStride = sizeof(Vertex);
68static const GLsizei gAlphaVertexStride = sizeof(AlphaVertex);
Chet Haase99585ad2011-05-02 15:00:16 -070069static const GLsizei gAAVertexStride = sizeof(AAVertex);
Romain Guy03750a02010-10-18 14:06:08 -070070static const GLsizei gMeshTextureOffset = 2 * sizeof(float);
Chris Craik6ebdc112012-08-31 18:24:33 -070071static const GLsizei gVertexAlphaOffset = 2 * sizeof(float);
Chet Haase99585ad2011-05-02 15:00:16 -070072static const GLsizei gVertexAAWidthOffset = 2 * sizeof(float);
73static const GLsizei gVertexAALengthOffset = 3 * sizeof(float);
Romain Guy03750a02010-10-18 14:06:08 -070074static const GLsizei gMeshCount = 4;
75
Romain Guya1d3c912011-12-13 14:55:06 -080076static const GLenum gTextureUnits[] = {
77 GL_TEXTURE0,
78 GL_TEXTURE1,
79 GL_TEXTURE2
80};
81
Romain Guy03750a02010-10-18 14:06:08 -070082///////////////////////////////////////////////////////////////////////////////
83// Debug
84///////////////////////////////////////////////////////////////////////////////
85
Romain Guyfb8b7632010-08-23 21:05:08 -070086struct CacheLogger {
87 CacheLogger() {
Romain Guyd6b2a002011-06-17 17:45:59 -070088 INIT_LOGD("Creating OpenGL renderer caches");
Romain Guyfb8b7632010-08-23 21:05:08 -070089 }
90}; // struct CacheLogger
91
Romain Guy03750a02010-10-18 14:06:08 -070092///////////////////////////////////////////////////////////////////////////////
93// Caches
94///////////////////////////////////////////////////////////////////////////////
95
Romain Guybb0acdf2012-03-05 13:44:35 -080096class DisplayList;
97
Romain Guy79537452011-10-12 13:48:51 -070098class ANDROID_API Caches: public Singleton<Caches> {
Chet Haasedd78cca2010-10-22 18:59:26 -070099 Caches();
Romain Guyfb8b7632010-08-23 21:05:08 -0700100
101 friend class Singleton<Caches>;
102
Romain Guye190aa62010-11-10 19:01:29 -0800103 CacheLogger mLogger;
Romain Guy9bca4792010-10-25 18:42:25 -0700104
Romain Guyfb8b7632010-08-23 21:05:08 -0700105public:
Romain Guybdf76092011-07-18 15:00:43 -0700106 enum FlushMode {
Romain Guy6d7475d2011-07-27 16:28:21 -0700107 kFlushMode_Layers = 0,
108 kFlushMode_Moderate,
Romain Guybdf76092011-07-18 15:00:43 -0700109 kFlushMode_Full
110 };
111
112 /**
Romain Guydfa10462012-05-12 16:18:58 -0700113 * Initialize caches.
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800114 */
115 void init();
116
117 /**
Romain Guy5bb3c732012-11-29 17:52:58 -0800118 * Initialize global system properties.
119 */
120 bool initProperties();
121
122 /**
Romain Guybdf76092011-07-18 15:00:43 -0700123 * Flush the cache.
124 *
125 * @param mode Indicates how much of the cache should be flushed
126 */
127 void flush(FlushMode mode);
128
Romain Guy5b3b3522010-10-27 18:57:51 -0700129 /**
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800130 * Destroys all resources associated with this cache. This should
131 * be called after a flush(kFlushMode_Full).
132 */
133 void terminate();
134
135 /**
Romain Guye190aa62010-11-10 19:01:29 -0800136 * Indicates whether the renderer is in debug mode.
137 * This debug mode provides limited information to app developers.
138 */
139 DebugLevel getDebugLevel() const {
140 return mDebugLevel;
141 }
142
143 /**
Romain Guyfe48f652010-11-11 15:36:56 -0800144 * Call this on each frame to ensure that garbage is deleted from
145 * GPU memory.
146 */
147 void clearGarbage();
148
149 /**
Romain Guyada830f2011-01-13 12:13:20 -0800150 * Can be used to delete a layer from a non EGL thread.
Romain Guy57066eb2011-01-12 12:53:32 -0800151 */
Romain Guyada830f2011-01-13 12:13:20 -0800152 void deleteLayerDeferred(Layer* layer);
Romain Guy57066eb2011-01-12 12:53:32 -0800153
Romain Guybb0acdf2012-03-05 13:44:35 -0800154 /*
155 * Can be used to delete a display list from a non EGL thread.
156 */
157 void deleteDisplayListDeferred(DisplayList* layer);
158
Romain Guy57066eb2011-01-12 12:53:32 -0800159 /**
Romain Guy5b3b3522010-10-27 18:57:51 -0700160 * Binds the VBO used to render simple textured quads.
161 */
Romain Guyf3a910b42011-12-12 20:35:21 -0800162 bool bindMeshBuffer();
Romain Guy5b3b3522010-10-27 18:57:51 -0700163
164 /**
165 * Binds the specified VBO if needed.
166 */
Romain Guyf3a910b42011-12-12 20:35:21 -0800167 bool bindMeshBuffer(const GLuint buffer);
Romain Guy5b3b3522010-10-27 18:57:51 -0700168
169 /**
170 * Unbinds the VBO used to render simple textured quads.
171 */
Romain Guyf3a910b42011-12-12 20:35:21 -0800172 bool unbindMeshBuffer();
173
Romain Guy15bc6432011-12-13 13:11:32 -0800174 bool bindIndicesBuffer(const GLuint buffer);
175 bool unbindIndicesBuffer();
176
Romain Guyf3a910b42011-12-12 20:35:21 -0800177 /**
178 * Binds an attrib to the specified float vertex pointer.
179 * Assumes a stride of gMeshStride and a size of 2.
180 */
Chris Craikcb4d6002012-09-25 12:00:29 -0700181 void bindPositionVertexPointer(bool force, GLvoid* vertices, GLsizei stride = gMeshStride);
Romain Guyf3a910b42011-12-12 20:35:21 -0800182
183 /**
184 * Binds an attrib to the specified float vertex pointer.
185 * Assumes a stride of gMeshStride and a size of 2.
186 */
Chris Craikcb4d6002012-09-25 12:00:29 -0700187 void bindTexCoordsVertexPointer(bool force, GLvoid* vertices);
Romain Guyf3a910b42011-12-12 20:35:21 -0800188
189 /**
190 * Resets the vertex pointers.
191 */
192 void resetVertexPointers();
193 void resetTexCoordsVertexPointer();
Romain Guy03750a02010-10-18 14:06:08 -0700194
Romain Guy15bc6432011-12-13 13:11:32 -0800195 void enableTexCoordsVertexArray();
196 void disbaleTexCoordsVertexArray();
197
Romain Guy5b3b3522010-10-27 18:57:51 -0700198 /**
Romain Guya1d3c912011-12-13 14:55:06 -0800199 * Activate the specified texture unit. The texture unit must
200 * be specified using an integer number (0 for GL_TEXTURE0 etc.)
201 */
202 void activeTexture(GLuint textureUnit);
203
204 /**
Romain Guy8f85e802011-12-14 19:23:32 -0800205 * Sets the scissor for the current surface.
206 */
Romain Guy8a4ac612012-07-17 17:32:48 -0700207 bool setScissor(GLint x, GLint y, GLint width, GLint height);
Romain Guy8f85e802011-12-14 19:23:32 -0800208
209 /**
Romain Guy82bc7a72012-01-03 14:13:39 -0800210 * Resets the scissor state.
211 */
212 void resetScissor();
213
Romain Guy8a4ac612012-07-17 17:32:48 -0700214 bool enableScissor();
215 bool disableScissor();
Romain Guy586cae32012-07-13 15:28:31 -0700216 void setScissorEnabled(bool enabled);
217
Romain Guyef359272013-01-31 19:07:29 -0800218 void startTiling(GLuint x, GLuint y, GLuint width, GLuint height, bool discard);
Romain Guy85ef80d2012-09-13 20:26:50 -0700219 void endTiling();
220
Romain Guy82bc7a72012-01-03 14:13:39 -0800221 /**
Romain Guy5b3b3522010-10-27 18:57:51 -0700222 * Returns the mesh used to draw regions. Calling this method will
223 * bind a VBO of type GL_ELEMENT_ARRAY_BUFFER that contains the
224 * indices for the region mesh.
225 */
226 TextureVertex* getRegionMesh();
227
Romain Guyc15008e2010-11-10 11:59:15 -0800228 /**
229 * Displays the memory usage of each cache and the total sum.
230 */
231 void dumpMemoryUsage();
Chet Haase9c1e23b2011-03-24 10:51:31 -0700232 void dumpMemoryUsage(String8& log);
Romain Guyc15008e2010-11-10 11:59:15 -0800233
Romain Guy54c1a642012-09-27 17:55:46 -0700234 bool hasRegisteredFunctors();
235 void registerFunctors(uint32_t functorCount);
236 void unregisterFunctors(uint32_t functorCount);
237
Romain Guyfb8b7632010-08-23 21:05:08 -0700238 bool blend;
239 GLenum lastSrcMode;
240 GLenum lastDstMode;
241 Program* currentProgram;
Romain Guy586cae32012-07-13 15:28:31 -0700242 bool scissorEnabled;
Romain Guyfb8b7632010-08-23 21:05:08 -0700243
Romain Guy746b7402010-10-26 16:27:31 -0700244 // VBO to draw with
Romain Guy03750a02010-10-18 14:06:08 -0700245 GLuint meshBuffer;
Romain Guy03750a02010-10-18 14:06:08 -0700246
Romain Guy746b7402010-10-26 16:27:31 -0700247 // GL extensions
248 Extensions extensions;
249
250 // Misc
251 GLint maxTextureSize;
Romain Guy4ff0cf42012-08-06 14:51:10 -0700252 bool debugLayersUpdates;
Romain Guy7c450aa2012-09-21 19:15:00 -0700253 bool debugOverdraw;
Romain Guy746b7402010-10-26 16:27:31 -0700254
Romain Guyfb8b7632010-08-23 21:05:08 -0700255 TextureCache textureCache;
256 LayerCache layerCache;
257 GradientCache gradientCache;
258 ProgramCache programCache;
259 PathCache pathCache;
Romain Guy01d58e42011-01-19 21:54:02 -0800260 RoundRectShapeCache roundRectShapeCache;
261 CircleShapeCache circleShapeCache;
Romain Guyc1cd9ba32011-01-23 14:18:41 -0800262 OvalShapeCache ovalShapeCache;
263 RectShapeCache rectShapeCache;
Romain Guy8b2f5262011-01-23 16:15:02 -0800264 ArcShapeCache arcShapeCache;
Romain Guyfb8b7632010-08-23 21:05:08 -0700265 PatchCache patchCache;
266 TextDropShadowCache dropShadowCache;
Romain Guye2d345e2010-09-24 18:39:22 -0700267 FboCache fboCache;
Chet Haase5c13d892010-10-08 08:37:55 -0700268 ResourceCache resourceCache;
Romain Guy29d89972010-09-22 16:10:57 -0700269
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700270 GammaFontRenderer* fontRenderer;
271
Romain Guy0baaac52012-08-31 20:31:01 -0700272 Dither dither;
Romain Guy0baaac52012-08-31 20:31:01 -0700273 Stencil stencil;
Romain Guy0baaac52012-08-31 20:31:01 -0700274
Romain Guydfa10462012-05-12 16:18:58 -0700275 // Debug methods
Romain Guy13631f32012-01-30 17:41:55 -0800276 PFNGLINSERTEVENTMARKEREXTPROC eventMark;
277 PFNGLPUSHGROUPMARKEREXTPROC startMark;
278 PFNGLPOPGROUPMARKEREXTPROC endMark;
279
Romain Guydfa10462012-05-12 16:18:58 -0700280 PFNGLLABELOBJECTEXTPROC setLabel;
281 PFNGLGETOBJECTLABELEXTPROC getLabel;
282
Romain Guye190aa62010-11-10 19:01:29 -0800283private:
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700284 void initFont();
Romain Guydfa10462012-05-12 16:18:58 -0700285 void initExtensions();
286 void initConstraints();
287
288 static void eventMarkNull(GLsizei length, const GLchar* marker) { }
289 static void startMarkNull(GLsizei length, const GLchar* marker) { }
Romain Guy13631f32012-01-30 17:41:55 -0800290 static void endMarkNull() { }
291
Romain Guydfa10462012-05-12 16:18:58 -0700292 static void setLabelNull(GLenum type, uint object, GLsizei length,
293 const char* label) { }
294 static void getLabelNull(GLenum type, uint object, GLsizei bufferSize,
295 GLsizei* length, char* label) {
296 if (length) *length = 0;
297 if (label) *label = '\0';
298 }
299
Romain Guyf3a910b42011-12-12 20:35:21 -0800300 GLuint mCurrentBuffer;
Romain Guy15bc6432011-12-13 13:11:32 -0800301 GLuint mCurrentIndicesBuffer;
Romain Guyf3a910b42011-12-12 20:35:21 -0800302 void* mCurrentPositionPointer;
Chris Craik16b897c2012-09-27 13:10:56 -0700303 GLsizei mCurrentPositionStride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800304 void* mCurrentTexCoordsPointer;
305
Romain Guy15bc6432011-12-13 13:11:32 -0800306 bool mTexCoordsArrayEnabled;
307
Romain Guya1d3c912011-12-13 14:55:06 -0800308 GLuint mTextureUnit;
309
Romain Guy8f85e802011-12-14 19:23:32 -0800310 GLint mScissorX;
311 GLint mScissorY;
312 GLint mScissorWidth;
313 GLint mScissorHeight;
314
Romain Guyf3a910b42011-12-12 20:35:21 -0800315 // Used to render layers
316 TextureVertex* mRegionMesh;
317 GLuint mRegionMeshIndices;
318
319 mutable Mutex mGarbageLock;
320 Vector<Layer*> mLayerGarbage;
Romain Guybb0acdf2012-03-05 13:44:35 -0800321 Vector<DisplayList*> mDisplayListGarbage;
Romain Guyf3a910b42011-12-12 20:35:21 -0800322
Romain Guye190aa62010-11-10 19:01:29 -0800323 DebugLevel mDebugLevel;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800324 bool mInitialized;
Romain Guy54c1a642012-09-27 17:55:46 -0700325
326 uint32_t mFunctorsCount;
Romain Guyfb8b7632010-08-23 21:05:08 -0700327}; // class Caches
328
329}; // namespace uirenderer
Romain Guyfb8b7632010-08-23 21:05:08 -0700330}; // namespace android
331
Romain Guy5b3b3522010-10-27 18:57:51 -0700332#endif // ANDROID_HWUI_CACHES_H