blob: 54e41380f620c8e902723000e4e635c2c517b577 [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
Romain Guy3b748a42013-04-17 18:54:38 -070024#include <GLES3/gl3.h>
25
26#include <utils/KeyedVector.h>
Romain Guyfb8b7632010-08-23 21:05:08 -070027#include <utils/Singleton.h>
Romain Guy3b748a42013-04-17 18:54:38 -070028#include <utils/Vector.h>
Romain Guyfb8b7632010-08-23 21:05:08 -070029
Romain Guy79537452011-10-12 13:48:51 -070030#include <cutils/compiler.h>
31
Romain Guy5dc7fa72013-03-11 20:48:31 -070032#include "thread/TaskProcessor.h"
33#include "thread/TaskManager.h"
34
Romain Guy3b748a42013-04-17 18:54:38 -070035#include "AssetAtlas.h"
Romain Guy03750a02010-10-18 14:06:08 -070036#include "FontRenderer.h"
37#include "GammaFontRenderer.h"
Romain Guyfb8b7632010-08-23 21:05:08 -070038#include "TextureCache.h"
39#include "LayerCache.h"
Romain Guy8d4aeb72013-02-12 16:08:55 -080040#include "RenderBufferCache.h"
Romain Guyfb8b7632010-08-23 21:05:08 -070041#include "GradientCache.h"
42#include "PatchCache.h"
Romain Guyfb8b7632010-08-23 21:05:08 -070043#include "ProgramCache.h"
Romain Guyff26a0c2011-01-20 11:35:46 -080044#include "PathCache.h"
Romain Guyfb8b7632010-08-23 21:05:08 -070045#include "TextDropShadowCache.h"
Romain Guye2d345e2010-09-24 18:39:22 -070046#include "FboCache.h"
Chet Haase5c13d892010-10-08 08:37:55 -070047#include "ResourceCache.h"
Romain Guy0baaac52012-08-31 20:31:01 -070048#include "Stencil.h"
Romain Guy211efea2012-07-31 21:16:07 -070049#include "Dither.h"
Romain Guyfb8b7632010-08-23 21:05:08 -070050
51namespace android {
52namespace uirenderer {
53
Romain Guy03750a02010-10-18 14:06:08 -070054///////////////////////////////////////////////////////////////////////////////
55// Globals
56///////////////////////////////////////////////////////////////////////////////
57
58#define REQUIRED_TEXTURE_UNITS_COUNT 3
59
Romain Guy5b3b3522010-10-27 18:57:51 -070060#define REGION_MESH_QUAD_COUNT 512
61
Romain Guy03750a02010-10-18 14:06:08 -070062// Generates simple and textured vertices
63#define FV(x, y, u, v) { { x, y }, { u, v } }
64
65// This array is never used directly but used as a memcpy source in the
66// OpenGLRenderer constructor
67static const TextureVertex gMeshVertices[] = {
68 FV(0.0f, 0.0f, 0.0f, 0.0f),
69 FV(1.0f, 0.0f, 1.0f, 0.0f),
70 FV(0.0f, 1.0f, 0.0f, 1.0f),
71 FV(1.0f, 1.0f, 1.0f, 1.0f)
72};
73static const GLsizei gMeshStride = sizeof(TextureVertex);
Chet Haase5b0200b2011-04-13 17:58:08 -070074static const GLsizei gVertexStride = sizeof(Vertex);
75static const GLsizei gAlphaVertexStride = sizeof(AlphaVertex);
Romain Guy03750a02010-10-18 14:06:08 -070076static const GLsizei gMeshTextureOffset = 2 * sizeof(float);
Chris Craik6ebdc112012-08-31 18:24:33 -070077static const GLsizei gVertexAlphaOffset = 2 * sizeof(float);
Chet Haase99585ad2011-05-02 15:00:16 -070078static const GLsizei gVertexAAWidthOffset = 2 * sizeof(float);
79static const GLsizei gVertexAALengthOffset = 3 * sizeof(float);
Romain Guy03750a02010-10-18 14:06:08 -070080static const GLsizei gMeshCount = 4;
81
Romain Guya1d3c912011-12-13 14:55:06 -080082static const GLenum gTextureUnits[] = {
83 GL_TEXTURE0,
84 GL_TEXTURE1,
85 GL_TEXTURE2
86};
87
Romain Guy03750a02010-10-18 14:06:08 -070088///////////////////////////////////////////////////////////////////////////////
89// Debug
90///////////////////////////////////////////////////////////////////////////////
91
Romain Guyfb8b7632010-08-23 21:05:08 -070092struct CacheLogger {
93 CacheLogger() {
Romain Guyd6b2a002011-06-17 17:45:59 -070094 INIT_LOGD("Creating OpenGL renderer caches");
Romain Guyfb8b7632010-08-23 21:05:08 -070095 }
96}; // struct CacheLogger
97
Romain Guy03750a02010-10-18 14:06:08 -070098///////////////////////////////////////////////////////////////////////////////
99// Caches
100///////////////////////////////////////////////////////////////////////////////
101
Romain Guybb0acdf2012-03-05 13:44:35 -0800102class DisplayList;
103
Romain Guy79537452011-10-12 13:48:51 -0700104class ANDROID_API Caches: public Singleton<Caches> {
Chet Haasedd78cca2010-10-22 18:59:26 -0700105 Caches();
Romain Guyfb8b7632010-08-23 21:05:08 -0700106
107 friend class Singleton<Caches>;
108
Romain Guye190aa62010-11-10 19:01:29 -0800109 CacheLogger mLogger;
Romain Guy9bca4792010-10-25 18:42:25 -0700110
Romain Guyfb8b7632010-08-23 21:05:08 -0700111public:
Romain Guybdf76092011-07-18 15:00:43 -0700112 enum FlushMode {
Romain Guy6d7475d2011-07-27 16:28:21 -0700113 kFlushMode_Layers = 0,
114 kFlushMode_Moderate,
Romain Guybdf76092011-07-18 15:00:43 -0700115 kFlushMode_Full
116 };
117
118 /**
Romain Guydfa10462012-05-12 16:18:58 -0700119 * Initialize caches.
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800120 */
Romain Guy3b748a42013-04-17 18:54:38 -0700121 bool init();
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800122
123 /**
Romain Guy5bb3c732012-11-29 17:52:58 -0800124 * Initialize global system properties.
125 */
126 bool initProperties();
127
128 /**
Romain Guybdf76092011-07-18 15:00:43 -0700129 * Flush the cache.
130 *
131 * @param mode Indicates how much of the cache should be flushed
132 */
133 void flush(FlushMode mode);
134
Romain Guy5b3b3522010-10-27 18:57:51 -0700135 /**
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800136 * Destroys all resources associated with this cache. This should
137 * be called after a flush(kFlushMode_Full).
138 */
139 void terminate();
140
141 /**
Romain Guye190aa62010-11-10 19:01:29 -0800142 * Indicates whether the renderer is in debug mode.
143 * This debug mode provides limited information to app developers.
144 */
145 DebugLevel getDebugLevel() const {
146 return mDebugLevel;
147 }
148
149 /**
Romain Guyfe48f652010-11-11 15:36:56 -0800150 * Call this on each frame to ensure that garbage is deleted from
151 * GPU memory.
152 */
153 void clearGarbage();
154
155 /**
Romain Guyada830f2011-01-13 12:13:20 -0800156 * Can be used to delete a layer from a non EGL thread.
Romain Guy57066eb2011-01-12 12:53:32 -0800157 */
Romain Guyada830f2011-01-13 12:13:20 -0800158 void deleteLayerDeferred(Layer* layer);
Romain Guy57066eb2011-01-12 12:53:32 -0800159
Romain Guybb0acdf2012-03-05 13:44:35 -0800160 /*
161 * Can be used to delete a display list from a non EGL thread.
162 */
163 void deleteDisplayListDeferred(DisplayList* layer);
164
Romain Guy57066eb2011-01-12 12:53:32 -0800165 /**
Romain Guy5b3b3522010-10-27 18:57:51 -0700166 * Binds the VBO used to render simple textured quads.
167 */
Romain Guyf3a910b42011-12-12 20:35:21 -0800168 bool bindMeshBuffer();
Romain Guy5b3b3522010-10-27 18:57:51 -0700169
170 /**
171 * Binds the specified VBO if needed.
172 */
Romain Guyf3a910b42011-12-12 20:35:21 -0800173 bool bindMeshBuffer(const GLuint buffer);
Romain Guy5b3b3522010-10-27 18:57:51 -0700174
175 /**
176 * Unbinds the VBO used to render simple textured quads.
177 */
Romain Guyf3a910b42011-12-12 20:35:21 -0800178 bool unbindMeshBuffer();
179
Romain Guy3b748a42013-04-17 18:54:38 -0700180 /**
181 * Binds a global indices buffer that can draw up to
182 * REGION_MESH_QUAD_COUNT quads.
183 */
184 bool bindIndicesBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800185 bool bindIndicesBuffer(const GLuint buffer);
186 bool unbindIndicesBuffer();
187
Romain Guyf3a910b42011-12-12 20:35:21 -0800188 /**
Romain Guycf51a412013-04-08 19:40:31 -0700189 * Binds the specified buffer as the current GL unpack pixel buffer.
190 */
191 bool bindPixelBuffer(const GLuint buffer);
192
193 /**
194 * Resets the current unpack pixel buffer to 0 (default value.)
195 */
196 bool unbindPixelBuffer();
197
198 /**
Romain Guyf3a910b42011-12-12 20:35:21 -0800199 * Binds an attrib to the specified float vertex pointer.
200 * Assumes a stride of gMeshStride and a size of 2.
201 */
Chris Craikcb4d6002012-09-25 12:00:29 -0700202 void bindPositionVertexPointer(bool force, GLvoid* vertices, GLsizei stride = gMeshStride);
Romain Guyf3a910b42011-12-12 20:35:21 -0800203
204 /**
205 * Binds an attrib to the specified float vertex pointer.
206 * Assumes a stride of gMeshStride and a size of 2.
207 */
Romain Guyff316ec2013-02-13 18:39:43 -0800208 void bindTexCoordsVertexPointer(bool force, GLvoid* vertices, GLsizei stride = gMeshStride);
Romain Guyf3a910b42011-12-12 20:35:21 -0800209
210 /**
211 * Resets the vertex pointers.
212 */
213 void resetVertexPointers();
214 void resetTexCoordsVertexPointer();
Romain Guy03750a02010-10-18 14:06:08 -0700215
Romain Guy15bc6432011-12-13 13:11:32 -0800216 void enableTexCoordsVertexArray();
Romain Guyff316ec2013-02-13 18:39:43 -0800217 void disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -0800218
Romain Guy5b3b3522010-10-27 18:57:51 -0700219 /**
Romain Guya1d3c912011-12-13 14:55:06 -0800220 * Activate the specified texture unit. The texture unit must
221 * be specified using an integer number (0 for GL_TEXTURE0 etc.)
222 */
223 void activeTexture(GLuint textureUnit);
224
225 /**
Romain Guy8f85e802011-12-14 19:23:32 -0800226 * Sets the scissor for the current surface.
227 */
Romain Guy8a4ac612012-07-17 17:32:48 -0700228 bool setScissor(GLint x, GLint y, GLint width, GLint height);
Romain Guy8f85e802011-12-14 19:23:32 -0800229
230 /**
Romain Guy82bc7a72012-01-03 14:13:39 -0800231 * Resets the scissor state.
232 */
233 void resetScissor();
234
Romain Guy8a4ac612012-07-17 17:32:48 -0700235 bool enableScissor();
236 bool disableScissor();
Romain Guy586cae32012-07-13 15:28:31 -0700237 void setScissorEnabled(bool enabled);
238
Romain Guyef359272013-01-31 19:07:29 -0800239 void startTiling(GLuint x, GLuint y, GLuint width, GLuint height, bool discard);
Romain Guy85ef80d2012-09-13 20:26:50 -0700240 void endTiling();
241
Romain Guy82bc7a72012-01-03 14:13:39 -0800242 /**
Romain Guy5b3b3522010-10-27 18:57:51 -0700243 * Returns the mesh used to draw regions. Calling this method will
244 * bind a VBO of type GL_ELEMENT_ARRAY_BUFFER that contains the
245 * indices for the region mesh.
246 */
247 TextureVertex* getRegionMesh();
248
Romain Guyc15008e2010-11-10 11:59:15 -0800249 /**
250 * Displays the memory usage of each cache and the total sum.
251 */
252 void dumpMemoryUsage();
Chet Haase9c1e23b2011-03-24 10:51:31 -0700253 void dumpMemoryUsage(String8& log);
Romain Guyc15008e2010-11-10 11:59:15 -0800254
Romain Guy54c1a642012-09-27 17:55:46 -0700255 bool hasRegisteredFunctors();
256 void registerFunctors(uint32_t functorCount);
257 void unregisterFunctors(uint32_t functorCount);
258
Romain Guyfb8b7632010-08-23 21:05:08 -0700259 bool blend;
260 GLenum lastSrcMode;
261 GLenum lastDstMode;
262 Program* currentProgram;
Romain Guy586cae32012-07-13 15:28:31 -0700263 bool scissorEnabled;
Romain Guyfb8b7632010-08-23 21:05:08 -0700264
Romain Guy0f6675332013-03-01 14:31:04 -0800265 bool drawDeferDisabled;
266 bool drawReorderDisabled;
267
Romain Guy746b7402010-10-26 16:27:31 -0700268 // VBO to draw with
Romain Guy03750a02010-10-18 14:06:08 -0700269 GLuint meshBuffer;
Romain Guy03750a02010-10-18 14:06:08 -0700270
Romain Guy746b7402010-10-26 16:27:31 -0700271 // Misc
272 GLint maxTextureSize;
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800273
274 // Debugging
Romain Guy4ff0cf42012-08-06 14:51:10 -0700275 bool debugLayersUpdates;
Romain Guy7c450aa2012-09-21 19:15:00 -0700276 bool debugOverdraw;
Romain Guy746b7402010-10-26 16:27:31 -0700277
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800278 enum StencilClipDebug {
279 kStencilHide,
280 kStencilShowHighlight,
281 kStencilShowRegion
282 };
283 StencilClipDebug debugStencilClip;
284
Romain Guyfb8b7632010-08-23 21:05:08 -0700285 TextureCache textureCache;
286 LayerCache layerCache;
Romain Guy8d4aeb72013-02-12 16:08:55 -0800287 RenderBufferCache renderBufferCache;
Romain Guyfb8b7632010-08-23 21:05:08 -0700288 GradientCache gradientCache;
289 ProgramCache programCache;
290 PathCache pathCache;
291 PatchCache patchCache;
292 TextDropShadowCache dropShadowCache;
Romain Guye2d345e2010-09-24 18:39:22 -0700293 FboCache fboCache;
Chet Haase5c13d892010-10-08 08:37:55 -0700294 ResourceCache resourceCache;
Romain Guy29d89972010-09-22 16:10:57 -0700295
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700296 GammaFontRenderer* fontRenderer;
297
Romain Guy5dc7fa72013-03-11 20:48:31 -0700298 TaskManager tasks;
299
Romain Guy0baaac52012-08-31 20:31:01 -0700300 Dither dither;
Romain Guy0baaac52012-08-31 20:31:01 -0700301 Stencil stencil;
Romain Guy0baaac52012-08-31 20:31:01 -0700302
Romain Guy3b748a42013-04-17 18:54:38 -0700303 AssetAtlas assetAtlas;
304
Romain Guyf9f00162013-05-09 11:50:12 -0700305 bool gpuPixelBuffersEnabled;
306
Romain Guydfa10462012-05-12 16:18:58 -0700307 // Debug methods
Romain Guy13631f32012-01-30 17:41:55 -0800308 PFNGLINSERTEVENTMARKEREXTPROC eventMark;
309 PFNGLPUSHGROUPMARKEREXTPROC startMark;
310 PFNGLPOPGROUPMARKEREXTPROC endMark;
311
Romain Guydfa10462012-05-12 16:18:58 -0700312 PFNGLLABELOBJECTEXTPROC setLabel;
313 PFNGLGETOBJECTLABELEXTPROC getLabel;
314
Romain Guye190aa62010-11-10 19:01:29 -0800315private:
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700316 void initFont();
Romain Guydfa10462012-05-12 16:18:58 -0700317 void initExtensions();
318 void initConstraints();
Romain Guyf9f00162013-05-09 11:50:12 -0700319 void initStaticProperties();
Romain Guydfa10462012-05-12 16:18:58 -0700320
321 static void eventMarkNull(GLsizei length, const GLchar* marker) { }
322 static void startMarkNull(GLsizei length, const GLchar* marker) { }
Romain Guy13631f32012-01-30 17:41:55 -0800323 static void endMarkNull() { }
324
Romain Guydfa10462012-05-12 16:18:58 -0700325 static void setLabelNull(GLenum type, uint object, GLsizei length,
326 const char* label) { }
327 static void getLabelNull(GLenum type, uint object, GLsizei bufferSize,
328 GLsizei* length, char* label) {
329 if (length) *length = 0;
330 if (label) *label = '\0';
331 }
332
Romain Guyf3a910b42011-12-12 20:35:21 -0800333 GLuint mCurrentBuffer;
Romain Guy15bc6432011-12-13 13:11:32 -0800334 GLuint mCurrentIndicesBuffer;
Romain Guycf51a412013-04-08 19:40:31 -0700335 GLuint mCurrentPixelBuffer;
Romain Guyf3a910b42011-12-12 20:35:21 -0800336 void* mCurrentPositionPointer;
Chris Craik16b897c2012-09-27 13:10:56 -0700337 GLsizei mCurrentPositionStride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800338 void* mCurrentTexCoordsPointer;
Romain Guyff316ec2013-02-13 18:39:43 -0800339 GLsizei mCurrentTexCoordsStride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800340
Romain Guy15bc6432011-12-13 13:11:32 -0800341 bool mTexCoordsArrayEnabled;
342
Romain Guya1d3c912011-12-13 14:55:06 -0800343 GLuint mTextureUnit;
344
Romain Guy8f85e802011-12-14 19:23:32 -0800345 GLint mScissorX;
346 GLint mScissorY;
347 GLint mScissorWidth;
348 GLint mScissorHeight;
349
Romain Guy3bbacf22013-02-06 16:51:04 -0800350 Extensions& mExtensions;
351
Romain Guyf3a910b42011-12-12 20:35:21 -0800352 // Used to render layers
353 TextureVertex* mRegionMesh;
Romain Guy3b748a42013-04-17 18:54:38 -0700354
355 // Global index buffer
356 GLuint mMeshIndices;
Romain Guyf3a910b42011-12-12 20:35:21 -0800357
358 mutable Mutex mGarbageLock;
359 Vector<Layer*> mLayerGarbage;
Romain Guybb0acdf2012-03-05 13:44:35 -0800360 Vector<DisplayList*> mDisplayListGarbage;
Romain Guyf3a910b42011-12-12 20:35:21 -0800361
Romain Guye190aa62010-11-10 19:01:29 -0800362 DebugLevel mDebugLevel;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800363 bool mInitialized;
Romain Guy54c1a642012-09-27 17:55:46 -0700364
365 uint32_t mFunctorsCount;
Romain Guyfb8b7632010-08-23 21:05:08 -0700366}; // class Caches
367
368}; // namespace uirenderer
Romain Guyfb8b7632010-08-23 21:05:08 -0700369}; // namespace android
370
Romain Guy5b3b3522010-10-27 18:57:51 -0700371#endif // ANDROID_HWUI_CACHES_H