blob: c35ad8838f2b9a49e1bd85afd8169f260cd6d56d [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 Guy03750a02010-10-18 14:06:08 -070028#include "FontRenderer.h"
29#include "GammaFontRenderer.h"
Romain Guyfb8b7632010-08-23 21:05:08 -070030#include "TextureCache.h"
31#include "LayerCache.h"
Romain Guy8d4aeb72013-02-12 16:08:55 -080032#include "RenderBufferCache.h"
Romain Guyfb8b7632010-08-23 21:05:08 -070033#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);
Romain Guy03750a02010-10-18 14:06:08 -070069static const GLsizei gMeshTextureOffset = 2 * sizeof(float);
Chris Craik6ebdc112012-08-31 18:24:33 -070070static const GLsizei gVertexAlphaOffset = 2 * sizeof(float);
Chet Haase99585ad2011-05-02 15:00:16 -070071static const GLsizei gVertexAAWidthOffset = 2 * sizeof(float);
72static const GLsizei gVertexAALengthOffset = 3 * sizeof(float);
Romain Guy03750a02010-10-18 14:06:08 -070073static const GLsizei gMeshCount = 4;
74
Romain Guya1d3c912011-12-13 14:55:06 -080075static const GLenum gTextureUnits[] = {
76 GL_TEXTURE0,
77 GL_TEXTURE1,
78 GL_TEXTURE2
79};
80
Romain Guy03750a02010-10-18 14:06:08 -070081///////////////////////////////////////////////////////////////////////////////
82// Debug
83///////////////////////////////////////////////////////////////////////////////
84
Romain Guyfb8b7632010-08-23 21:05:08 -070085struct CacheLogger {
86 CacheLogger() {
Romain Guyd6b2a002011-06-17 17:45:59 -070087 INIT_LOGD("Creating OpenGL renderer caches");
Romain Guyfb8b7632010-08-23 21:05:08 -070088 }
89}; // struct CacheLogger
90
Romain Guy03750a02010-10-18 14:06:08 -070091///////////////////////////////////////////////////////////////////////////////
92// Caches
93///////////////////////////////////////////////////////////////////////////////
94
Romain Guybb0acdf2012-03-05 13:44:35 -080095class DisplayList;
96
Romain Guy79537452011-10-12 13:48:51 -070097class ANDROID_API Caches: public Singleton<Caches> {
Chet Haasedd78cca2010-10-22 18:59:26 -070098 Caches();
Romain Guyfb8b7632010-08-23 21:05:08 -070099
100 friend class Singleton<Caches>;
101
Romain Guye190aa62010-11-10 19:01:29 -0800102 CacheLogger mLogger;
Romain Guy9bca4792010-10-25 18:42:25 -0700103
Romain Guyfb8b7632010-08-23 21:05:08 -0700104public:
Romain Guybdf76092011-07-18 15:00:43 -0700105 enum FlushMode {
Romain Guy6d7475d2011-07-27 16:28:21 -0700106 kFlushMode_Layers = 0,
107 kFlushMode_Moderate,
Romain Guybdf76092011-07-18 15:00:43 -0700108 kFlushMode_Full
109 };
110
111 /**
Romain Guydfa10462012-05-12 16:18:58 -0700112 * Initialize caches.
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800113 */
114 void init();
115
116 /**
Romain Guy5bb3c732012-11-29 17:52:58 -0800117 * Initialize global system properties.
118 */
119 bool initProperties();
120
121 /**
Romain Guybdf76092011-07-18 15:00:43 -0700122 * Flush the cache.
123 *
124 * @param mode Indicates how much of the cache should be flushed
125 */
126 void flush(FlushMode mode);
127
Romain Guy5b3b3522010-10-27 18:57:51 -0700128 /**
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800129 * Destroys all resources associated with this cache. This should
130 * be called after a flush(kFlushMode_Full).
131 */
132 void terminate();
133
134 /**
Romain Guye190aa62010-11-10 19:01:29 -0800135 * Indicates whether the renderer is in debug mode.
136 * This debug mode provides limited information to app developers.
137 */
138 DebugLevel getDebugLevel() const {
139 return mDebugLevel;
140 }
141
142 /**
Romain Guyfe48f652010-11-11 15:36:56 -0800143 * Call this on each frame to ensure that garbage is deleted from
144 * GPU memory.
145 */
146 void clearGarbage();
147
148 /**
Romain Guyada830f2011-01-13 12:13:20 -0800149 * Can be used to delete a layer from a non EGL thread.
Romain Guy57066eb2011-01-12 12:53:32 -0800150 */
Romain Guyada830f2011-01-13 12:13:20 -0800151 void deleteLayerDeferred(Layer* layer);
Romain Guy57066eb2011-01-12 12:53:32 -0800152
Romain Guybb0acdf2012-03-05 13:44:35 -0800153 /*
154 * Can be used to delete a display list from a non EGL thread.
155 */
156 void deleteDisplayListDeferred(DisplayList* layer);
157
Romain Guy57066eb2011-01-12 12:53:32 -0800158 /**
Romain Guy5b3b3522010-10-27 18:57:51 -0700159 * Binds the VBO used to render simple textured quads.
160 */
Romain Guyf3a910b42011-12-12 20:35:21 -0800161 bool bindMeshBuffer();
Romain Guy5b3b3522010-10-27 18:57:51 -0700162
163 /**
164 * Binds the specified VBO if needed.
165 */
Romain Guyf3a910b42011-12-12 20:35:21 -0800166 bool bindMeshBuffer(const GLuint buffer);
Romain Guy5b3b3522010-10-27 18:57:51 -0700167
168 /**
169 * Unbinds the VBO used to render simple textured quads.
170 */
Romain Guyf3a910b42011-12-12 20:35:21 -0800171 bool unbindMeshBuffer();
172
Romain Guy15bc6432011-12-13 13:11:32 -0800173 bool bindIndicesBuffer(const GLuint buffer);
174 bool unbindIndicesBuffer();
175
Romain Guyf3a910b42011-12-12 20:35:21 -0800176 /**
177 * Binds an attrib to the specified float vertex pointer.
178 * Assumes a stride of gMeshStride and a size of 2.
179 */
Chris Craikcb4d6002012-09-25 12:00:29 -0700180 void bindPositionVertexPointer(bool force, GLvoid* vertices, GLsizei stride = gMeshStride);
Romain Guyf3a910b42011-12-12 20:35:21 -0800181
182 /**
183 * Binds an attrib to the specified float vertex pointer.
184 * Assumes a stride of gMeshStride and a size of 2.
185 */
Romain Guyff316ec2013-02-13 18:39:43 -0800186 void bindTexCoordsVertexPointer(bool force, GLvoid* vertices, GLsizei stride = gMeshStride);
Romain Guyf3a910b42011-12-12 20:35:21 -0800187
188 /**
189 * Resets the vertex pointers.
190 */
191 void resetVertexPointers();
192 void resetTexCoordsVertexPointer();
Romain Guy03750a02010-10-18 14:06:08 -0700193
Romain Guy15bc6432011-12-13 13:11:32 -0800194 void enableTexCoordsVertexArray();
Romain Guyff316ec2013-02-13 18:39:43 -0800195 void disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -0800196
Romain Guy5b3b3522010-10-27 18:57:51 -0700197 /**
Romain Guya1d3c912011-12-13 14:55:06 -0800198 * Activate the specified texture unit. The texture unit must
199 * be specified using an integer number (0 for GL_TEXTURE0 etc.)
200 */
201 void activeTexture(GLuint textureUnit);
202
203 /**
Romain Guy8f85e802011-12-14 19:23:32 -0800204 * Sets the scissor for the current surface.
205 */
Romain Guy8a4ac612012-07-17 17:32:48 -0700206 bool setScissor(GLint x, GLint y, GLint width, GLint height);
Romain Guy8f85e802011-12-14 19:23:32 -0800207
208 /**
Romain Guy82bc7a72012-01-03 14:13:39 -0800209 * Resets the scissor state.
210 */
211 void resetScissor();
212
Romain Guy8a4ac612012-07-17 17:32:48 -0700213 bool enableScissor();
214 bool disableScissor();
Romain Guy586cae32012-07-13 15:28:31 -0700215 void setScissorEnabled(bool enabled);
216
Romain Guyef359272013-01-31 19:07:29 -0800217 void startTiling(GLuint x, GLuint y, GLuint width, GLuint height, bool discard);
Romain Guy85ef80d2012-09-13 20:26:50 -0700218 void endTiling();
219
Romain Guy82bc7a72012-01-03 14:13:39 -0800220 /**
Romain Guy5b3b3522010-10-27 18:57:51 -0700221 * Returns the mesh used to draw regions. Calling this method will
222 * bind a VBO of type GL_ELEMENT_ARRAY_BUFFER that contains the
223 * indices for the region mesh.
224 */
225 TextureVertex* getRegionMesh();
226
Romain Guyc15008e2010-11-10 11:59:15 -0800227 /**
228 * Displays the memory usage of each cache and the total sum.
229 */
230 void dumpMemoryUsage();
Chet Haase9c1e23b2011-03-24 10:51:31 -0700231 void dumpMemoryUsage(String8& log);
Romain Guyc15008e2010-11-10 11:59:15 -0800232
Romain Guy54c1a642012-09-27 17:55:46 -0700233 bool hasRegisteredFunctors();
234 void registerFunctors(uint32_t functorCount);
235 void unregisterFunctors(uint32_t functorCount);
236
Romain Guyfb8b7632010-08-23 21:05:08 -0700237 bool blend;
238 GLenum lastSrcMode;
239 GLenum lastDstMode;
240 Program* currentProgram;
Romain Guy586cae32012-07-13 15:28:31 -0700241 bool scissorEnabled;
Romain Guyfb8b7632010-08-23 21:05:08 -0700242
Romain Guy746b7402010-10-26 16:27:31 -0700243 // VBO to draw with
Romain Guy03750a02010-10-18 14:06:08 -0700244 GLuint meshBuffer;
Romain Guy03750a02010-10-18 14:06:08 -0700245
Romain Guy746b7402010-10-26 16:27:31 -0700246 // Misc
247 GLint maxTextureSize;
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800248
249 // Debugging
Romain Guy4ff0cf42012-08-06 14:51:10 -0700250 bool debugLayersUpdates;
Romain Guy7c450aa2012-09-21 19:15:00 -0700251 bool debugOverdraw;
Romain Guy746b7402010-10-26 16:27:31 -0700252
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800253 enum StencilClipDebug {
254 kStencilHide,
255 kStencilShowHighlight,
256 kStencilShowRegion
257 };
258 StencilClipDebug debugStencilClip;
259
Romain Guyfb8b7632010-08-23 21:05:08 -0700260 TextureCache textureCache;
261 LayerCache layerCache;
Romain Guy8d4aeb72013-02-12 16:08:55 -0800262 RenderBufferCache renderBufferCache;
Romain Guyfb8b7632010-08-23 21:05:08 -0700263 GradientCache gradientCache;
264 ProgramCache programCache;
265 PathCache pathCache;
Romain Guy01d58e42011-01-19 21:54:02 -0800266 RoundRectShapeCache roundRectShapeCache;
267 CircleShapeCache circleShapeCache;
Romain Guyc1cd9ba32011-01-23 14:18:41 -0800268 OvalShapeCache ovalShapeCache;
269 RectShapeCache rectShapeCache;
Romain Guy8b2f5262011-01-23 16:15:02 -0800270 ArcShapeCache arcShapeCache;
Romain Guyfb8b7632010-08-23 21:05:08 -0700271 PatchCache patchCache;
272 TextDropShadowCache dropShadowCache;
Romain Guye2d345e2010-09-24 18:39:22 -0700273 FboCache fboCache;
Chet Haase5c13d892010-10-08 08:37:55 -0700274 ResourceCache resourceCache;
Romain Guy29d89972010-09-22 16:10:57 -0700275
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700276 GammaFontRenderer* fontRenderer;
277
Romain Guy0baaac52012-08-31 20:31:01 -0700278 Dither dither;
Romain Guy0baaac52012-08-31 20:31:01 -0700279 Stencil stencil;
Romain Guy0baaac52012-08-31 20:31:01 -0700280
Romain Guydfa10462012-05-12 16:18:58 -0700281 // Debug methods
Romain Guy13631f32012-01-30 17:41:55 -0800282 PFNGLINSERTEVENTMARKEREXTPROC eventMark;
283 PFNGLPUSHGROUPMARKEREXTPROC startMark;
284 PFNGLPOPGROUPMARKEREXTPROC endMark;
285
Romain Guydfa10462012-05-12 16:18:58 -0700286 PFNGLLABELOBJECTEXTPROC setLabel;
287 PFNGLGETOBJECTLABELEXTPROC getLabel;
288
Romain Guye190aa62010-11-10 19:01:29 -0800289private:
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700290 void initFont();
Romain Guydfa10462012-05-12 16:18:58 -0700291 void initExtensions();
292 void initConstraints();
293
294 static void eventMarkNull(GLsizei length, const GLchar* marker) { }
295 static void startMarkNull(GLsizei length, const GLchar* marker) { }
Romain Guy13631f32012-01-30 17:41:55 -0800296 static void endMarkNull() { }
297
Romain Guydfa10462012-05-12 16:18:58 -0700298 static void setLabelNull(GLenum type, uint object, GLsizei length,
299 const char* label) { }
300 static void getLabelNull(GLenum type, uint object, GLsizei bufferSize,
301 GLsizei* length, char* label) {
302 if (length) *length = 0;
303 if (label) *label = '\0';
304 }
305
Romain Guyf3a910b42011-12-12 20:35:21 -0800306 GLuint mCurrentBuffer;
Romain Guy15bc6432011-12-13 13:11:32 -0800307 GLuint mCurrentIndicesBuffer;
Romain Guyf3a910b42011-12-12 20:35:21 -0800308 void* mCurrentPositionPointer;
Chris Craik16b897c2012-09-27 13:10:56 -0700309 GLsizei mCurrentPositionStride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800310 void* mCurrentTexCoordsPointer;
Romain Guyff316ec2013-02-13 18:39:43 -0800311 GLsizei mCurrentTexCoordsStride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800312
Romain Guy15bc6432011-12-13 13:11:32 -0800313 bool mTexCoordsArrayEnabled;
314
Romain Guya1d3c912011-12-13 14:55:06 -0800315 GLuint mTextureUnit;
316
Romain Guy8f85e802011-12-14 19:23:32 -0800317 GLint mScissorX;
318 GLint mScissorY;
319 GLint mScissorWidth;
320 GLint mScissorHeight;
321
Romain Guy3bbacf22013-02-06 16:51:04 -0800322 Extensions& mExtensions;
323
Romain Guyf3a910b42011-12-12 20:35:21 -0800324 // Used to render layers
325 TextureVertex* mRegionMesh;
326 GLuint mRegionMeshIndices;
327
328 mutable Mutex mGarbageLock;
329 Vector<Layer*> mLayerGarbage;
Romain Guybb0acdf2012-03-05 13:44:35 -0800330 Vector<DisplayList*> mDisplayListGarbage;
Romain Guyf3a910b42011-12-12 20:35:21 -0800331
Romain Guye190aa62010-11-10 19:01:29 -0800332 DebugLevel mDebugLevel;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800333 bool mInitialized;
Romain Guy54c1a642012-09-27 17:55:46 -0700334
335 uint32_t mFunctorsCount;
Romain Guyfb8b7632010-08-23 21:05:08 -0700336}; // class Caches
337
338}; // namespace uirenderer
Romain Guyfb8b7632010-08-23 21:05:08 -0700339}; // namespace android
340
Romain Guy5b3b3522010-10-27 18:57:51 -0700341#endif // ANDROID_HWUI_CACHES_H