blob: dc32a7ec63fa60854e2afdc83a7ec70c7b7f6d97 [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 Guy01d58e42011-01-19 21:54:02 -080039#include "ShapeCache.h"
Romain Guyff26a0c2011-01-20 11:35:46 -080040#include "PathCache.h"
Romain Guyfb8b7632010-08-23 21:05:08 -070041#include "TextDropShadowCache.h"
Romain Guye2d345e2010-09-24 18:39:22 -070042#include "FboCache.h"
Chet Haase5c13d892010-10-08 08:37:55 -070043#include "ResourceCache.h"
Romain Guy0baaac52012-08-31 20:31:01 -070044#include "Stencil.h"
Romain Guy211efea2012-07-31 21:16:07 -070045#include "Dither.h"
Romain Guyfb8b7632010-08-23 21:05:08 -070046
47namespace android {
48namespace uirenderer {
49
Romain Guy03750a02010-10-18 14:06:08 -070050///////////////////////////////////////////////////////////////////////////////
51// Globals
52///////////////////////////////////////////////////////////////////////////////
53
54#define REQUIRED_TEXTURE_UNITS_COUNT 3
55
Romain Guy5b3b3522010-10-27 18:57:51 -070056#define REGION_MESH_QUAD_COUNT 512
57
Romain Guy03750a02010-10-18 14:06:08 -070058// Generates simple and textured vertices
59#define FV(x, y, u, v) { { x, y }, { u, v } }
60
61// This array is never used directly but used as a memcpy source in the
62// OpenGLRenderer constructor
63static const TextureVertex gMeshVertices[] = {
64 FV(0.0f, 0.0f, 0.0f, 0.0f),
65 FV(1.0f, 0.0f, 1.0f, 0.0f),
66 FV(0.0f, 1.0f, 0.0f, 1.0f),
67 FV(1.0f, 1.0f, 1.0f, 1.0f)
68};
69static const GLsizei gMeshStride = sizeof(TextureVertex);
Chet Haase5b0200b2011-04-13 17:58:08 -070070static const GLsizei gVertexStride = sizeof(Vertex);
71static const GLsizei gAlphaVertexStride = sizeof(AlphaVertex);
Romain Guy03750a02010-10-18 14:06:08 -070072static const GLsizei gMeshTextureOffset = 2 * sizeof(float);
Chris Craik6ebdc112012-08-31 18:24:33 -070073static const GLsizei gVertexAlphaOffset = 2 * sizeof(float);
Chet Haase99585ad2011-05-02 15:00:16 -070074static const GLsizei gVertexAAWidthOffset = 2 * sizeof(float);
75static const GLsizei gVertexAALengthOffset = 3 * sizeof(float);
Romain Guy03750a02010-10-18 14:06:08 -070076static const GLsizei gMeshCount = 4;
77
Romain Guya1d3c912011-12-13 14:55:06 -080078static const GLenum gTextureUnits[] = {
79 GL_TEXTURE0,
80 GL_TEXTURE1,
81 GL_TEXTURE2
82};
83
Romain Guy03750a02010-10-18 14:06:08 -070084///////////////////////////////////////////////////////////////////////////////
85// Debug
86///////////////////////////////////////////////////////////////////////////////
87
Romain Guyfb8b7632010-08-23 21:05:08 -070088struct CacheLogger {
89 CacheLogger() {
Romain Guyd6b2a002011-06-17 17:45:59 -070090 INIT_LOGD("Creating OpenGL renderer caches");
Romain Guyfb8b7632010-08-23 21:05:08 -070091 }
92}; // struct CacheLogger
93
Romain Guy03750a02010-10-18 14:06:08 -070094///////////////////////////////////////////////////////////////////////////////
95// Caches
96///////////////////////////////////////////////////////////////////////////////
97
Romain Guybb0acdf2012-03-05 13:44:35 -080098class DisplayList;
99
Romain Guy79537452011-10-12 13:48:51 -0700100class ANDROID_API Caches: public Singleton<Caches> {
Chet Haasedd78cca2010-10-22 18:59:26 -0700101 Caches();
Romain Guyfb8b7632010-08-23 21:05:08 -0700102
103 friend class Singleton<Caches>;
104
Romain Guye190aa62010-11-10 19:01:29 -0800105 CacheLogger mLogger;
Romain Guy9bca4792010-10-25 18:42:25 -0700106
Romain Guyfb8b7632010-08-23 21:05:08 -0700107public:
Romain Guybdf76092011-07-18 15:00:43 -0700108 enum FlushMode {
Romain Guy6d7475d2011-07-27 16:28:21 -0700109 kFlushMode_Layers = 0,
110 kFlushMode_Moderate,
Romain Guybdf76092011-07-18 15:00:43 -0700111 kFlushMode_Full
112 };
113
114 /**
Romain Guydfa10462012-05-12 16:18:58 -0700115 * Initialize caches.
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800116 */
117 void init();
118
119 /**
Romain Guy5bb3c732012-11-29 17:52:58 -0800120 * Initialize global system properties.
121 */
122 bool initProperties();
123
124 /**
Romain Guybdf76092011-07-18 15:00:43 -0700125 * Flush the cache.
126 *
127 * @param mode Indicates how much of the cache should be flushed
128 */
129 void flush(FlushMode mode);
130
Romain Guy5b3b3522010-10-27 18:57:51 -0700131 /**
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800132 * Destroys all resources associated with this cache. This should
133 * be called after a flush(kFlushMode_Full).
134 */
135 void terminate();
136
137 /**
Romain Guye190aa62010-11-10 19:01:29 -0800138 * Indicates whether the renderer is in debug mode.
139 * This debug mode provides limited information to app developers.
140 */
141 DebugLevel getDebugLevel() const {
142 return mDebugLevel;
143 }
144
145 /**
Romain Guyfe48f652010-11-11 15:36:56 -0800146 * Call this on each frame to ensure that garbage is deleted from
147 * GPU memory.
148 */
149 void clearGarbage();
150
151 /**
Romain Guyada830f2011-01-13 12:13:20 -0800152 * Can be used to delete a layer from a non EGL thread.
Romain Guy57066eb2011-01-12 12:53:32 -0800153 */
Romain Guyada830f2011-01-13 12:13:20 -0800154 void deleteLayerDeferred(Layer* layer);
Romain Guy57066eb2011-01-12 12:53:32 -0800155
Romain Guybb0acdf2012-03-05 13:44:35 -0800156 /*
157 * Can be used to delete a display list from a non EGL thread.
158 */
159 void deleteDisplayListDeferred(DisplayList* layer);
160
Romain Guy57066eb2011-01-12 12:53:32 -0800161 /**
Romain Guy5b3b3522010-10-27 18:57:51 -0700162 * Binds the VBO used to render simple textured quads.
163 */
Romain Guyf3a910b42011-12-12 20:35:21 -0800164 bool bindMeshBuffer();
Romain Guy5b3b3522010-10-27 18:57:51 -0700165
166 /**
167 * Binds the specified VBO if needed.
168 */
Romain Guyf3a910b42011-12-12 20:35:21 -0800169 bool bindMeshBuffer(const GLuint buffer);
Romain Guy5b3b3522010-10-27 18:57:51 -0700170
171 /**
172 * Unbinds the VBO used to render simple textured quads.
173 */
Romain Guyf3a910b42011-12-12 20:35:21 -0800174 bool unbindMeshBuffer();
175
Romain Guy15bc6432011-12-13 13:11:32 -0800176 bool bindIndicesBuffer(const GLuint buffer);
177 bool unbindIndicesBuffer();
178
Romain Guyf3a910b42011-12-12 20:35:21 -0800179 /**
180 * Binds an attrib to the specified float vertex pointer.
181 * Assumes a stride of gMeshStride and a size of 2.
182 */
Chris Craikcb4d6002012-09-25 12:00:29 -0700183 void bindPositionVertexPointer(bool force, GLvoid* vertices, GLsizei stride = gMeshStride);
Romain Guyf3a910b42011-12-12 20:35:21 -0800184
185 /**
186 * Binds an attrib to the specified float vertex pointer.
187 * Assumes a stride of gMeshStride and a size of 2.
188 */
Romain Guyff316ec2013-02-13 18:39:43 -0800189 void bindTexCoordsVertexPointer(bool force, GLvoid* vertices, GLsizei stride = gMeshStride);
Romain Guyf3a910b42011-12-12 20:35:21 -0800190
191 /**
192 * Resets the vertex pointers.
193 */
194 void resetVertexPointers();
195 void resetTexCoordsVertexPointer();
Romain Guy03750a02010-10-18 14:06:08 -0700196
Romain Guy15bc6432011-12-13 13:11:32 -0800197 void enableTexCoordsVertexArray();
Romain Guyff316ec2013-02-13 18:39:43 -0800198 void disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -0800199
Romain Guy5b3b3522010-10-27 18:57:51 -0700200 /**
Romain Guya1d3c912011-12-13 14:55:06 -0800201 * Activate the specified texture unit. The texture unit must
202 * be specified using an integer number (0 for GL_TEXTURE0 etc.)
203 */
204 void activeTexture(GLuint textureUnit);
205
206 /**
Romain Guy8f85e802011-12-14 19:23:32 -0800207 * Sets the scissor for the current surface.
208 */
Romain Guy8a4ac612012-07-17 17:32:48 -0700209 bool setScissor(GLint x, GLint y, GLint width, GLint height);
Romain Guy8f85e802011-12-14 19:23:32 -0800210
211 /**
Romain Guy82bc7a72012-01-03 14:13:39 -0800212 * Resets the scissor state.
213 */
214 void resetScissor();
215
Romain Guy8a4ac612012-07-17 17:32:48 -0700216 bool enableScissor();
217 bool disableScissor();
Romain Guy586cae32012-07-13 15:28:31 -0700218 void setScissorEnabled(bool enabled);
219
Romain Guyef359272013-01-31 19:07:29 -0800220 void startTiling(GLuint x, GLuint y, GLuint width, GLuint height, bool discard);
Romain Guy85ef80d2012-09-13 20:26:50 -0700221 void endTiling();
222
Romain Guy82bc7a72012-01-03 14:13:39 -0800223 /**
Romain Guy5b3b3522010-10-27 18:57:51 -0700224 * Returns the mesh used to draw regions. Calling this method will
225 * bind a VBO of type GL_ELEMENT_ARRAY_BUFFER that contains the
226 * indices for the region mesh.
227 */
228 TextureVertex* getRegionMesh();
229
Romain Guyc15008e2010-11-10 11:59:15 -0800230 /**
231 * Displays the memory usage of each cache and the total sum.
232 */
233 void dumpMemoryUsage();
Chet Haase9c1e23b2011-03-24 10:51:31 -0700234 void dumpMemoryUsage(String8& log);
Romain Guyc15008e2010-11-10 11:59:15 -0800235
Romain Guy54c1a642012-09-27 17:55:46 -0700236 bool hasRegisteredFunctors();
237 void registerFunctors(uint32_t functorCount);
238 void unregisterFunctors(uint32_t functorCount);
239
Romain Guyfb8b7632010-08-23 21:05:08 -0700240 bool blend;
241 GLenum lastSrcMode;
242 GLenum lastDstMode;
243 Program* currentProgram;
Romain Guy586cae32012-07-13 15:28:31 -0700244 bool scissorEnabled;
Romain Guyfb8b7632010-08-23 21:05:08 -0700245
Romain Guy0f667532013-03-01 14:31:04 -0800246 bool drawDeferDisabled;
247 bool drawReorderDisabled;
248
Romain Guy746b7402010-10-26 16:27:31 -0700249 // VBO to draw with
Romain Guy03750a02010-10-18 14:06:08 -0700250 GLuint meshBuffer;
Romain Guy03750a02010-10-18 14:06:08 -0700251
Romain Guy746b7402010-10-26 16:27:31 -0700252 // Misc
253 GLint maxTextureSize;
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800254
255 // Debugging
Romain Guy4ff0cf42012-08-06 14:51:10 -0700256 bool debugLayersUpdates;
Romain Guy7c450aa2012-09-21 19:15:00 -0700257 bool debugOverdraw;
Romain Guy746b7402010-10-26 16:27:31 -0700258
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800259 enum StencilClipDebug {
260 kStencilHide,
261 kStencilShowHighlight,
262 kStencilShowRegion
263 };
264 StencilClipDebug debugStencilClip;
265
Romain Guyfb8b7632010-08-23 21:05:08 -0700266 TextureCache textureCache;
267 LayerCache layerCache;
Romain Guy8d4aeb72013-02-12 16:08:55 -0800268 RenderBufferCache renderBufferCache;
Romain Guyfb8b7632010-08-23 21:05:08 -0700269 GradientCache gradientCache;
270 ProgramCache programCache;
271 PathCache pathCache;
Romain Guy01d58e42011-01-19 21:54:02 -0800272 RoundRectShapeCache roundRectShapeCache;
273 CircleShapeCache circleShapeCache;
Romain Guyc1cd9ba2011-01-23 14:18:41 -0800274 OvalShapeCache ovalShapeCache;
275 RectShapeCache rectShapeCache;
Romain Guy8b2f5262011-01-23 16:15:02 -0800276 ArcShapeCache arcShapeCache;
Romain Guyfb8b7632010-08-23 21:05:08 -0700277 PatchCache patchCache;
278 TextDropShadowCache dropShadowCache;
Romain Guye2d345e2010-09-24 18:39:22 -0700279 FboCache fboCache;
Chet Haase5c13d892010-10-08 08:37:55 -0700280 ResourceCache resourceCache;
Romain Guy29d89972010-09-22 16:10:57 -0700281
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700282 GammaFontRenderer* fontRenderer;
283
Romain Guy5dc7fa72013-03-11 20:48:31 -0700284 TaskManager tasks;
285
Romain Guy0baaac52012-08-31 20:31:01 -0700286 Dither dither;
Romain Guy0baaac52012-08-31 20:31:01 -0700287 Stencil stencil;
Romain Guy0baaac52012-08-31 20:31:01 -0700288
Romain Guydfa10462012-05-12 16:18:58 -0700289 // Debug methods
Romain Guy13631f32012-01-30 17:41:55 -0800290 PFNGLINSERTEVENTMARKEREXTPROC eventMark;
291 PFNGLPUSHGROUPMARKEREXTPROC startMark;
292 PFNGLPOPGROUPMARKEREXTPROC endMark;
293
Romain Guydfa10462012-05-12 16:18:58 -0700294 PFNGLLABELOBJECTEXTPROC setLabel;
295 PFNGLGETOBJECTLABELEXTPROC getLabel;
296
Romain Guye190aa62010-11-10 19:01:29 -0800297private:
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700298 void initFont();
Romain Guydfa10462012-05-12 16:18:58 -0700299 void initExtensions();
300 void initConstraints();
301
302 static void eventMarkNull(GLsizei length, const GLchar* marker) { }
303 static void startMarkNull(GLsizei length, const GLchar* marker) { }
Romain Guy13631f32012-01-30 17:41:55 -0800304 static void endMarkNull() { }
305
Romain Guydfa10462012-05-12 16:18:58 -0700306 static void setLabelNull(GLenum type, uint object, GLsizei length,
307 const char* label) { }
308 static void getLabelNull(GLenum type, uint object, GLsizei bufferSize,
309 GLsizei* length, char* label) {
310 if (length) *length = 0;
311 if (label) *label = '\0';
312 }
313
Romain Guyf3a910b42011-12-12 20:35:21 -0800314 GLuint mCurrentBuffer;
Romain Guy15bc6432011-12-13 13:11:32 -0800315 GLuint mCurrentIndicesBuffer;
Romain Guyf3a910b42011-12-12 20:35:21 -0800316 void* mCurrentPositionPointer;
Chris Craik16b897c2012-09-27 13:10:56 -0700317 GLsizei mCurrentPositionStride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800318 void* mCurrentTexCoordsPointer;
Romain Guyff316ec2013-02-13 18:39:43 -0800319 GLsizei mCurrentTexCoordsStride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800320
Romain Guy15bc6432011-12-13 13:11:32 -0800321 bool mTexCoordsArrayEnabled;
322
Romain Guya1d3c912011-12-13 14:55:06 -0800323 GLuint mTextureUnit;
324
Romain Guy8f85e802011-12-14 19:23:32 -0800325 GLint mScissorX;
326 GLint mScissorY;
327 GLint mScissorWidth;
328 GLint mScissorHeight;
329
Romain Guy3bbacf22013-02-06 16:51:04 -0800330 Extensions& mExtensions;
331
Romain Guyf3a910b42011-12-12 20:35:21 -0800332 // Used to render layers
333 TextureVertex* mRegionMesh;
334 GLuint mRegionMeshIndices;
335
336 mutable Mutex mGarbageLock;
337 Vector<Layer*> mLayerGarbage;
Romain Guybb0acdf2012-03-05 13:44:35 -0800338 Vector<DisplayList*> mDisplayListGarbage;
Romain Guyf3a910b42011-12-12 20:35:21 -0800339
Romain Guye190aa62010-11-10 19:01:29 -0800340 DebugLevel mDebugLevel;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800341 bool mInitialized;
Romain Guy54c1a642012-09-27 17:55:46 -0700342
343 uint32_t mFunctorsCount;
Romain Guyfb8b7632010-08-23 21:05:08 -0700344}; // class Caches
345
346}; // namespace uirenderer
Romain Guyfb8b7632010-08-23 21:05:08 -0700347}; // namespace android
348
Romain Guy5b3b3522010-10-27 18:57:51 -0700349#endif // ANDROID_HWUI_CACHES_H