blob: 91b938b5c98946612c5c01b97220402ebf913f89 [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 Guy5dc7fa72013-03-11 20:48:31 -070028#include "thread/TaskProcessor.h"
29#include "thread/TaskManager.h"
30
Romain Guy03750a02010-10-18 14:06:08 -070031#include "FontRenderer.h"
32#include "GammaFontRenderer.h"
Romain Guyfb8b7632010-08-23 21:05:08 -070033#include "TextureCache.h"
34#include "LayerCache.h"
Romain Guy8d4aeb72013-02-12 16:08:55 -080035#include "RenderBufferCache.h"
Romain Guyfb8b7632010-08-23 21:05:08 -070036#include "GradientCache.h"
37#include "PatchCache.h"
Romain Guyfb8b7632010-08-23 21:05:08 -070038#include "ProgramCache.h"
Romain Guyff26a0c2011-01-20 11:35:46 -080039#include "PathCache.h"
Romain Guyfb8b7632010-08-23 21:05:08 -070040#include "TextDropShadowCache.h"
Romain Guye2d345e2010-09-24 18:39:22 -070041#include "FboCache.h"
Chet Haase5c13d892010-10-08 08:37:55 -070042#include "ResourceCache.h"
Romain Guy0baaac52012-08-31 20:31:01 -070043#include "Stencil.h"
Romain Guy211efea2012-07-31 21:16:07 -070044#include "Dither.h"
Romain Guyfb8b7632010-08-23 21:05:08 -070045
46namespace android {
47namespace uirenderer {
48
Romain Guy03750a02010-10-18 14:06:08 -070049///////////////////////////////////////////////////////////////////////////////
50// Globals
51///////////////////////////////////////////////////////////////////////////////
52
53#define REQUIRED_TEXTURE_UNITS_COUNT 3
54
Romain Guy5b3b3522010-10-27 18:57:51 -070055#define REGION_MESH_QUAD_COUNT 512
56
Romain Guy03750a02010-10-18 14:06:08 -070057// Generates simple and textured vertices
58#define FV(x, y, u, v) { { x, y }, { u, v } }
59
60// This array is never used directly but used as a memcpy source in the
61// OpenGLRenderer constructor
62static const TextureVertex gMeshVertices[] = {
63 FV(0.0f, 0.0f, 0.0f, 0.0f),
64 FV(1.0f, 0.0f, 1.0f, 0.0f),
65 FV(0.0f, 1.0f, 0.0f, 1.0f),
66 FV(1.0f, 1.0f, 1.0f, 1.0f)
67};
68static const GLsizei gMeshStride = sizeof(TextureVertex);
Chet Haase5b0200b2011-04-13 17:58:08 -070069static const GLsizei gVertexStride = sizeof(Vertex);
70static const GLsizei gAlphaVertexStride = sizeof(AlphaVertex);
Romain Guy03750a02010-10-18 14:06:08 -070071static const GLsizei gMeshTextureOffset = 2 * sizeof(float);
Chris Craik6ebdc112012-08-31 18:24:33 -070072static const GLsizei gVertexAlphaOffset = 2 * sizeof(float);
Chet Haase99585ad2011-05-02 15:00:16 -070073static const GLsizei gVertexAAWidthOffset = 2 * sizeof(float);
74static const GLsizei gVertexAALengthOffset = 3 * sizeof(float);
Romain Guy03750a02010-10-18 14:06:08 -070075static const GLsizei gMeshCount = 4;
76
Romain Guya1d3c912011-12-13 14:55:06 -080077static const GLenum gTextureUnits[] = {
78 GL_TEXTURE0,
79 GL_TEXTURE1,
80 GL_TEXTURE2
81};
82
Romain Guy03750a02010-10-18 14:06:08 -070083///////////////////////////////////////////////////////////////////////////////
84// Debug
85///////////////////////////////////////////////////////////////////////////////
86
Romain Guyfb8b7632010-08-23 21:05:08 -070087struct CacheLogger {
88 CacheLogger() {
Romain Guyd6b2a002011-06-17 17:45:59 -070089 INIT_LOGD("Creating OpenGL renderer caches");
Romain Guyfb8b7632010-08-23 21:05:08 -070090 }
91}; // struct CacheLogger
92
Romain Guy03750a02010-10-18 14:06:08 -070093///////////////////////////////////////////////////////////////////////////////
94// Caches
95///////////////////////////////////////////////////////////////////////////////
96
Romain Guybb0acdf2012-03-05 13:44:35 -080097class DisplayList;
98
Romain Guy79537452011-10-12 13:48:51 -070099class ANDROID_API Caches: public Singleton<Caches> {
Chet Haasedd78cca2010-10-22 18:59:26 -0700100 Caches();
Romain Guyfb8b7632010-08-23 21:05:08 -0700101
102 friend class Singleton<Caches>;
103
Romain Guye190aa62010-11-10 19:01:29 -0800104 CacheLogger mLogger;
Romain Guy9bca4792010-10-25 18:42:25 -0700105
Romain Guyfb8b7632010-08-23 21:05:08 -0700106public:
Romain Guybdf76092011-07-18 15:00:43 -0700107 enum FlushMode {
Romain Guy6d7475d2011-07-27 16:28:21 -0700108 kFlushMode_Layers = 0,
109 kFlushMode_Moderate,
Romain Guybdf76092011-07-18 15:00:43 -0700110 kFlushMode_Full
111 };
112
113 /**
Romain Guydfa10462012-05-12 16:18:58 -0700114 * Initialize caches.
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800115 */
116 void init();
117
118 /**
Romain Guy5bb3c732012-11-29 17:52:58 -0800119 * Initialize global system properties.
120 */
121 bool initProperties();
122
123 /**
Romain Guybdf76092011-07-18 15:00:43 -0700124 * Flush the cache.
125 *
126 * @param mode Indicates how much of the cache should be flushed
127 */
128 void flush(FlushMode mode);
129
Romain Guy5b3b3522010-10-27 18:57:51 -0700130 /**
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800131 * Destroys all resources associated with this cache. This should
132 * be called after a flush(kFlushMode_Full).
133 */
134 void terminate();
135
136 /**
Romain Guye190aa62010-11-10 19:01:29 -0800137 * Indicates whether the renderer is in debug mode.
138 * This debug mode provides limited information to app developers.
139 */
140 DebugLevel getDebugLevel() const {
141 return mDebugLevel;
142 }
143
144 /**
Romain Guyfe48f652010-11-11 15:36:56 -0800145 * Call this on each frame to ensure that garbage is deleted from
146 * GPU memory.
147 */
148 void clearGarbage();
149
150 /**
Romain Guyada830f2011-01-13 12:13:20 -0800151 * Can be used to delete a layer from a non EGL thread.
Romain Guy57066eb2011-01-12 12:53:32 -0800152 */
Romain Guyada830f2011-01-13 12:13:20 -0800153 void deleteLayerDeferred(Layer* layer);
Romain Guy57066eb2011-01-12 12:53:32 -0800154
Romain Guybb0acdf2012-03-05 13:44:35 -0800155 /*
156 * Can be used to delete a display list from a non EGL thread.
157 */
158 void deleteDisplayListDeferred(DisplayList* layer);
159
Romain Guy57066eb2011-01-12 12:53:32 -0800160 /**
Romain Guy5b3b3522010-10-27 18:57:51 -0700161 * Binds the VBO used to render simple textured quads.
162 */
Romain Guyf3a910b42011-12-12 20:35:21 -0800163 bool bindMeshBuffer();
Romain Guy5b3b3522010-10-27 18:57:51 -0700164
165 /**
166 * Binds the specified VBO if needed.
167 */
Romain Guyf3a910b42011-12-12 20:35:21 -0800168 bool bindMeshBuffer(const GLuint buffer);
Romain Guy5b3b3522010-10-27 18:57:51 -0700169
170 /**
171 * Unbinds the VBO used to render simple textured quads.
172 */
Romain Guyf3a910b42011-12-12 20:35:21 -0800173 bool unbindMeshBuffer();
174
Romain Guy15bc6432011-12-13 13:11:32 -0800175 bool bindIndicesBuffer(const GLuint buffer);
176 bool unbindIndicesBuffer();
177
Romain Guyf3a910b42011-12-12 20:35:21 -0800178 /**
Romain Guycf51a412013-04-08 19:40:31 -0700179 * Binds the specified buffer as the current GL unpack pixel buffer.
180 */
181 bool bindPixelBuffer(const GLuint buffer);
182
183 /**
184 * Resets the current unpack pixel buffer to 0 (default value.)
185 */
186 bool unbindPixelBuffer();
187
188 /**
Romain Guyf3a910b42011-12-12 20:35:21 -0800189 * Binds an attrib to the specified float vertex pointer.
190 * Assumes a stride of gMeshStride and a size of 2.
191 */
Chris Craikcb4d6002012-09-25 12:00:29 -0700192 void bindPositionVertexPointer(bool force, GLvoid* vertices, GLsizei stride = gMeshStride);
Romain Guyf3a910b42011-12-12 20:35:21 -0800193
194 /**
195 * Binds an attrib to the specified float vertex pointer.
196 * Assumes a stride of gMeshStride and a size of 2.
197 */
Romain Guyff316ec2013-02-13 18:39:43 -0800198 void bindTexCoordsVertexPointer(bool force, GLvoid* vertices, GLsizei stride = gMeshStride);
Romain Guyf3a910b42011-12-12 20:35:21 -0800199
200 /**
201 * Resets the vertex pointers.
202 */
203 void resetVertexPointers();
204 void resetTexCoordsVertexPointer();
Romain Guy03750a02010-10-18 14:06:08 -0700205
Romain Guy15bc6432011-12-13 13:11:32 -0800206 void enableTexCoordsVertexArray();
Romain Guyff316ec2013-02-13 18:39:43 -0800207 void disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -0800208
Romain Guy5b3b3522010-10-27 18:57:51 -0700209 /**
Romain Guya1d3c912011-12-13 14:55:06 -0800210 * Activate the specified texture unit. The texture unit must
211 * be specified using an integer number (0 for GL_TEXTURE0 etc.)
212 */
213 void activeTexture(GLuint textureUnit);
214
215 /**
Romain Guy8f85e802011-12-14 19:23:32 -0800216 * Sets the scissor for the current surface.
217 */
Romain Guy8a4ac612012-07-17 17:32:48 -0700218 bool setScissor(GLint x, GLint y, GLint width, GLint height);
Romain Guy8f85e802011-12-14 19:23:32 -0800219
220 /**
Romain Guy82bc7a72012-01-03 14:13:39 -0800221 * Resets the scissor state.
222 */
223 void resetScissor();
224
Romain Guy8a4ac612012-07-17 17:32:48 -0700225 bool enableScissor();
226 bool disableScissor();
Romain Guy586cae32012-07-13 15:28:31 -0700227 void setScissorEnabled(bool enabled);
228
Romain Guyef359272013-01-31 19:07:29 -0800229 void startTiling(GLuint x, GLuint y, GLuint width, GLuint height, bool discard);
Romain Guy85ef80d2012-09-13 20:26:50 -0700230 void endTiling();
231
Romain Guy82bc7a72012-01-03 14:13:39 -0800232 /**
Romain Guy5b3b3522010-10-27 18:57:51 -0700233 * Returns the mesh used to draw regions. Calling this method will
234 * bind a VBO of type GL_ELEMENT_ARRAY_BUFFER that contains the
235 * indices for the region mesh.
236 */
237 TextureVertex* getRegionMesh();
238
Romain Guyc15008e2010-11-10 11:59:15 -0800239 /**
240 * Displays the memory usage of each cache and the total sum.
241 */
242 void dumpMemoryUsage();
Chet Haase9c1e23b2011-03-24 10:51:31 -0700243 void dumpMemoryUsage(String8& log);
Romain Guyc15008e2010-11-10 11:59:15 -0800244
Romain Guy54c1a642012-09-27 17:55:46 -0700245 bool hasRegisteredFunctors();
246 void registerFunctors(uint32_t functorCount);
247 void unregisterFunctors(uint32_t functorCount);
248
Romain Guyfb8b7632010-08-23 21:05:08 -0700249 bool blend;
250 GLenum lastSrcMode;
251 GLenum lastDstMode;
252 Program* currentProgram;
Romain Guy586cae32012-07-13 15:28:31 -0700253 bool scissorEnabled;
Romain Guyfb8b7632010-08-23 21:05:08 -0700254
Romain Guy0f667532013-03-01 14:31:04 -0800255 bool drawDeferDisabled;
256 bool drawReorderDisabled;
257
Romain Guy746b7402010-10-26 16:27:31 -0700258 // VBO to draw with
Romain Guy03750a02010-10-18 14:06:08 -0700259 GLuint meshBuffer;
Romain Guy03750a02010-10-18 14:06:08 -0700260
Romain Guy746b7402010-10-26 16:27:31 -0700261 // Misc
262 GLint maxTextureSize;
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800263
264 // Debugging
Romain Guy4ff0cf42012-08-06 14:51:10 -0700265 bool debugLayersUpdates;
Romain Guy7c450aa2012-09-21 19:15:00 -0700266 bool debugOverdraw;
Romain Guy746b7402010-10-26 16:27:31 -0700267
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800268 enum StencilClipDebug {
269 kStencilHide,
270 kStencilShowHighlight,
271 kStencilShowRegion
272 };
273 StencilClipDebug debugStencilClip;
274
Romain Guyfb8b7632010-08-23 21:05:08 -0700275 TextureCache textureCache;
276 LayerCache layerCache;
Romain Guy8d4aeb72013-02-12 16:08:55 -0800277 RenderBufferCache renderBufferCache;
Romain Guyfb8b7632010-08-23 21:05:08 -0700278 GradientCache gradientCache;
279 ProgramCache programCache;
280 PathCache pathCache;
281 PatchCache patchCache;
282 TextDropShadowCache dropShadowCache;
Romain Guye2d345e2010-09-24 18:39:22 -0700283 FboCache fboCache;
Chet Haase5c13d892010-10-08 08:37:55 -0700284 ResourceCache resourceCache;
Romain Guy29d89972010-09-22 16:10:57 -0700285
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700286 GammaFontRenderer* fontRenderer;
287
Romain Guy5dc7fa72013-03-11 20:48:31 -0700288 TaskManager tasks;
289
Romain Guy0baaac52012-08-31 20:31:01 -0700290 Dither dither;
Romain Guy0baaac52012-08-31 20:31:01 -0700291 Stencil stencil;
Romain Guy0baaac52012-08-31 20:31:01 -0700292
Romain Guydfa10462012-05-12 16:18:58 -0700293 // Debug methods
Romain Guy13631f32012-01-30 17:41:55 -0800294 PFNGLINSERTEVENTMARKEREXTPROC eventMark;
295 PFNGLPUSHGROUPMARKEREXTPROC startMark;
296 PFNGLPOPGROUPMARKEREXTPROC endMark;
297
Romain Guydfa10462012-05-12 16:18:58 -0700298 PFNGLLABELOBJECTEXTPROC setLabel;
299 PFNGLGETOBJECTLABELEXTPROC getLabel;
300
Romain Guye190aa62010-11-10 19:01:29 -0800301private:
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700302 void initFont();
Romain Guydfa10462012-05-12 16:18:58 -0700303 void initExtensions();
304 void initConstraints();
305
306 static void eventMarkNull(GLsizei length, const GLchar* marker) { }
307 static void startMarkNull(GLsizei length, const GLchar* marker) { }
Romain Guy13631f32012-01-30 17:41:55 -0800308 static void endMarkNull() { }
309
Romain Guydfa10462012-05-12 16:18:58 -0700310 static void setLabelNull(GLenum type, uint object, GLsizei length,
311 const char* label) { }
312 static void getLabelNull(GLenum type, uint object, GLsizei bufferSize,
313 GLsizei* length, char* label) {
314 if (length) *length = 0;
315 if (label) *label = '\0';
316 }
317
Romain Guyf3a910b42011-12-12 20:35:21 -0800318 GLuint mCurrentBuffer;
Romain Guy15bc6432011-12-13 13:11:32 -0800319 GLuint mCurrentIndicesBuffer;
Romain Guycf51a412013-04-08 19:40:31 -0700320 GLuint mCurrentPixelBuffer;
Romain Guyf3a910b42011-12-12 20:35:21 -0800321 void* mCurrentPositionPointer;
Chris Craik16b897c2012-09-27 13:10:56 -0700322 GLsizei mCurrentPositionStride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800323 void* mCurrentTexCoordsPointer;
Romain Guyff316ec2013-02-13 18:39:43 -0800324 GLsizei mCurrentTexCoordsStride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800325
Romain Guy15bc6432011-12-13 13:11:32 -0800326 bool mTexCoordsArrayEnabled;
327
Romain Guya1d3c912011-12-13 14:55:06 -0800328 GLuint mTextureUnit;
329
Romain Guy8f85e802011-12-14 19:23:32 -0800330 GLint mScissorX;
331 GLint mScissorY;
332 GLint mScissorWidth;
333 GLint mScissorHeight;
334
Romain Guy3bbacf22013-02-06 16:51:04 -0800335 Extensions& mExtensions;
336
Romain Guyf3a910b42011-12-12 20:35:21 -0800337 // Used to render layers
338 TextureVertex* mRegionMesh;
339 GLuint mRegionMeshIndices;
340
341 mutable Mutex mGarbageLock;
342 Vector<Layer*> mLayerGarbage;
Romain Guybb0acdf2012-03-05 13:44:35 -0800343 Vector<DisplayList*> mDisplayListGarbage;
Romain Guyf3a910b42011-12-12 20:35:21 -0800344
Romain Guye190aa62010-11-10 19:01:29 -0800345 DebugLevel mDebugLevel;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800346 bool mInitialized;
Romain Guy54c1a642012-09-27 17:55:46 -0700347
348 uint32_t mFunctorsCount;
Romain Guyfb8b7632010-08-23 21:05:08 -0700349}; // class Caches
350
351}; // namespace uirenderer
Romain Guyfb8b7632010-08-23 21:05:08 -0700352}; // namespace android
353
Romain Guy5b3b3522010-10-27 18:57:51 -0700354#endif // ANDROID_HWUI_CACHES_H