blob: 282aee901006ecce91c432c36ff8fa0cad22df7f [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
Romain Guy8aa195d2013-06-04 18:00:09 -070058// GL ES 2.0 defines that at least 16 texture units must be supported
Romain Guy03750a02010-10-18 14:06:08 -070059#define REQUIRED_TEXTURE_UNITS_COUNT 3
60
Romain Guy31e08e92013-06-18 15:53:53 -070061// Maximum number of quads that pre-allocated meshes can draw
62static const uint32_t gMaxNumberOfQuads = 2048;
Romain Guy5b3b3522010-10-27 18:57:51 -070063
Romain Guy03750a02010-10-18 14:06:08 -070064// Generates simple and textured vertices
65#define FV(x, y, u, v) { { x, y }, { u, v } }
66
67// This array is never used directly but used as a memcpy source in the
68// OpenGLRenderer constructor
69static const TextureVertex gMeshVertices[] = {
70 FV(0.0f, 0.0f, 0.0f, 0.0f),
71 FV(1.0f, 0.0f, 1.0f, 0.0f),
72 FV(0.0f, 1.0f, 0.0f, 1.0f),
73 FV(1.0f, 1.0f, 1.0f, 1.0f)
74};
75static const GLsizei gMeshStride = sizeof(TextureVertex);
Chet Haase5b0200b2011-04-13 17:58:08 -070076static const GLsizei gVertexStride = sizeof(Vertex);
77static const GLsizei gAlphaVertexStride = sizeof(AlphaVertex);
Romain Guy03750a02010-10-18 14:06:08 -070078static const GLsizei gMeshTextureOffset = 2 * sizeof(float);
Chris Craik6ebdc112012-08-31 18:24:33 -070079static const GLsizei gVertexAlphaOffset = 2 * sizeof(float);
Chet Haase99585ad2011-05-02 15:00:16 -070080static const GLsizei gVertexAAWidthOffset = 2 * sizeof(float);
81static const GLsizei gVertexAALengthOffset = 3 * sizeof(float);
Romain Guy03750a02010-10-18 14:06:08 -070082static const GLsizei gMeshCount = 4;
83
Romain Guy8aa195d2013-06-04 18:00:09 -070084// Must define as many texture units as specified by REQUIRED_TEXTURE_UNITS_COUNT
Romain Guya1d3c912011-12-13 14:55:06 -080085static const GLenum gTextureUnits[] = {
86 GL_TEXTURE0,
87 GL_TEXTURE1,
88 GL_TEXTURE2
89};
90
Romain Guy03750a02010-10-18 14:06:08 -070091///////////////////////////////////////////////////////////////////////////////
92// Debug
93///////////////////////////////////////////////////////////////////////////////
94
Romain Guyfb8b7632010-08-23 21:05:08 -070095struct CacheLogger {
96 CacheLogger() {
Romain Guyd6b2a002011-06-17 17:45:59 -070097 INIT_LOGD("Creating OpenGL renderer caches");
Romain Guyfb8b7632010-08-23 21:05:08 -070098 }
99}; // struct CacheLogger
100
Romain Guy03750a02010-10-18 14:06:08 -0700101///////////////////////////////////////////////////////////////////////////////
102// Caches
103///////////////////////////////////////////////////////////////////////////////
104
Romain Guybb0acdf2012-03-05 13:44:35 -0800105class DisplayList;
106
Romain Guy79537452011-10-12 13:48:51 -0700107class ANDROID_API Caches: public Singleton<Caches> {
Chet Haasedd78cca2010-10-22 18:59:26 -0700108 Caches();
Romain Guyfb8b7632010-08-23 21:05:08 -0700109
110 friend class Singleton<Caches>;
111
Romain Guye190aa62010-11-10 19:01:29 -0800112 CacheLogger mLogger;
Romain Guy9bca4792010-10-25 18:42:25 -0700113
Romain Guyfb8b7632010-08-23 21:05:08 -0700114public:
Romain Guybdf76092011-07-18 15:00:43 -0700115 enum FlushMode {
Romain Guy6d7475d2011-07-27 16:28:21 -0700116 kFlushMode_Layers = 0,
117 kFlushMode_Moderate,
Romain Guybdf76092011-07-18 15:00:43 -0700118 kFlushMode_Full
119 };
120
121 /**
Romain Guydfa10462012-05-12 16:18:58 -0700122 * Initialize caches.
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800123 */
Romain Guy3b748a42013-04-17 18:54:38 -0700124 bool init();
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800125
126 /**
Romain Guy5bb3c732012-11-29 17:52:58 -0800127 * Initialize global system properties.
128 */
129 bool initProperties();
130
131 /**
Romain Guybdf76092011-07-18 15:00:43 -0700132 * Flush the cache.
133 *
134 * @param mode Indicates how much of the cache should be flushed
135 */
136 void flush(FlushMode mode);
137
Romain Guy5b3b3522010-10-27 18:57:51 -0700138 /**
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800139 * Destroys all resources associated with this cache. This should
140 * be called after a flush(kFlushMode_Full).
141 */
142 void terminate();
143
144 /**
Romain Guye190aa62010-11-10 19:01:29 -0800145 * Indicates whether the renderer is in debug mode.
146 * This debug mode provides limited information to app developers.
147 */
148 DebugLevel getDebugLevel() const {
149 return mDebugLevel;
150 }
151
152 /**
Romain Guy627c6fd2013-08-21 11:53:18 -0700153 * Returns a non-premultiplied ARGB color for the specified
154 * amount of overdraw (1 for 1x, 2 for 2x, etc.)
155 */
156 uint32_t getOverdrawColor(uint32_t amount) const;
157
158 /**
Romain Guyfe48f652010-11-11 15:36:56 -0800159 * Call this on each frame to ensure that garbage is deleted from
160 * GPU memory.
161 */
162 void clearGarbage();
163
164 /**
Romain Guyada830f2011-01-13 12:13:20 -0800165 * Can be used to delete a layer from a non EGL thread.
Romain Guy57066eb2011-01-12 12:53:32 -0800166 */
Romain Guyada830f2011-01-13 12:13:20 -0800167 void deleteLayerDeferred(Layer* layer);
Romain Guy57066eb2011-01-12 12:53:32 -0800168
Romain Guybb0acdf2012-03-05 13:44:35 -0800169 /*
170 * Can be used to delete a display list from a non EGL thread.
171 */
172 void deleteDisplayListDeferred(DisplayList* layer);
173
Romain Guy57066eb2011-01-12 12:53:32 -0800174 /**
Romain Guy5b3b3522010-10-27 18:57:51 -0700175 * Binds the VBO used to render simple textured quads.
176 */
Romain Guyf3a910b42011-12-12 20:35:21 -0800177 bool bindMeshBuffer();
Romain Guy5b3b3522010-10-27 18:57:51 -0700178
179 /**
180 * Binds the specified VBO if needed.
181 */
Romain Guyf3a910b42011-12-12 20:35:21 -0800182 bool bindMeshBuffer(const GLuint buffer);
Romain Guy5b3b3522010-10-27 18:57:51 -0700183
184 /**
185 * Unbinds the VBO used to render simple textured quads.
186 */
Romain Guyf3a910b42011-12-12 20:35:21 -0800187 bool unbindMeshBuffer();
188
Romain Guy3b748a42013-04-17 18:54:38 -0700189 /**
190 * Binds a global indices buffer that can draw up to
Romain Guy31e08e92013-06-18 15:53:53 -0700191 * gMaxNumberOfQuads quads.
Romain Guy3b748a42013-04-17 18:54:38 -0700192 */
193 bool bindIndicesBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800194 bool bindIndicesBuffer(const GLuint buffer);
195 bool unbindIndicesBuffer();
196
Romain Guyf3a910b42011-12-12 20:35:21 -0800197 /**
Romain Guycf51a412013-04-08 19:40:31 -0700198 * Binds the specified buffer as the current GL unpack pixel buffer.
199 */
200 bool bindPixelBuffer(const GLuint buffer);
201
202 /**
203 * Resets the current unpack pixel buffer to 0 (default value.)
204 */
205 bool unbindPixelBuffer();
206
207 /**
Romain Guyf3a910b42011-12-12 20:35:21 -0800208 * Binds an attrib to the specified float vertex pointer.
209 * Assumes a stride of gMeshStride and a size of 2.
210 */
Chris Craikcb4d6002012-09-25 12:00:29 -0700211 void bindPositionVertexPointer(bool force, GLvoid* vertices, GLsizei stride = gMeshStride);
Romain Guyf3a910b42011-12-12 20:35:21 -0800212
213 /**
214 * Binds an attrib to the specified float vertex pointer.
215 * Assumes a stride of gMeshStride and a size of 2.
216 */
Romain Guyff316ec2013-02-13 18:39:43 -0800217 void bindTexCoordsVertexPointer(bool force, GLvoid* vertices, GLsizei stride = gMeshStride);
Romain Guyf3a910b42011-12-12 20:35:21 -0800218
219 /**
220 * Resets the vertex pointers.
221 */
222 void resetVertexPointers();
223 void resetTexCoordsVertexPointer();
Romain Guy03750a02010-10-18 14:06:08 -0700224
Romain Guy15bc6432011-12-13 13:11:32 -0800225 void enableTexCoordsVertexArray();
Romain Guyff316ec2013-02-13 18:39:43 -0800226 void disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -0800227
Romain Guy5b3b3522010-10-27 18:57:51 -0700228 /**
Romain Guya1d3c912011-12-13 14:55:06 -0800229 * Activate the specified texture unit. The texture unit must
230 * be specified using an integer number (0 for GL_TEXTURE0 etc.)
231 */
232 void activeTexture(GLuint textureUnit);
233
234 /**
Chris Craik9ab2d182013-07-22 16:16:06 -0700235 * Invalidate the cached value of the active texture unit.
236 */
237 void resetActiveTexture();
238
239 /**
Romain Guy8aa195d2013-06-04 18:00:09 -0700240 * Binds the specified texture as a GL_TEXTURE_2D texture.
Romain Guybe1b1272013-06-06 14:02:54 -0700241 * All texture bindings must be performed with this method or
242 * bindTexture(GLenum, GLuint).
Romain Guy8aa195d2013-06-04 18:00:09 -0700243 */
244 void bindTexture(GLuint texture);
245
246 /**
Romain Guybe1b1272013-06-06 14:02:54 -0700247 * Binds the specified texture with the specified render target.
248 * All texture bindings must be performed with this method or
249 * bindTexture(GLuint).
Romain Guy8aa195d2013-06-04 18:00:09 -0700250 */
251 void bindTexture(GLenum target, GLuint texture);
252
253 /**
Romain Guybe1b1272013-06-06 14:02:54 -0700254 * Deletes the specified texture and clears it from the cache
255 * of bound textures.
256 * All textures must be deleted using this method.
257 */
258 void deleteTexture(GLuint texture);
259
260 /**
Romain Guy8aa195d2013-06-04 18:00:09 -0700261 * Signals that the cache of bound textures should be cleared.
262 * Other users of the context may have altered which textures are bound.
263 */
264 void resetBoundTextures();
265
266 /**
Romain Guy8f85e802011-12-14 19:23:32 -0800267 * Sets the scissor for the current surface.
268 */
Romain Guy8a4ac612012-07-17 17:32:48 -0700269 bool setScissor(GLint x, GLint y, GLint width, GLint height);
Romain Guy8f85e802011-12-14 19:23:32 -0800270
271 /**
Romain Guy82bc7a72012-01-03 14:13:39 -0800272 * Resets the scissor state.
273 */
274 void resetScissor();
275
Romain Guy8a4ac612012-07-17 17:32:48 -0700276 bool enableScissor();
277 bool disableScissor();
Romain Guy586cae32012-07-13 15:28:31 -0700278 void setScissorEnabled(bool enabled);
279
Romain Guyef359272013-01-31 19:07:29 -0800280 void startTiling(GLuint x, GLuint y, GLuint width, GLuint height, bool discard);
Romain Guy85ef80d2012-09-13 20:26:50 -0700281 void endTiling();
282
Romain Guy82bc7a72012-01-03 14:13:39 -0800283 /**
Romain Guy5b3b3522010-10-27 18:57:51 -0700284 * Returns the mesh used to draw regions. Calling this method will
285 * bind a VBO of type GL_ELEMENT_ARRAY_BUFFER that contains the
286 * indices for the region mesh.
287 */
288 TextureVertex* getRegionMesh();
289
Romain Guyc15008e2010-11-10 11:59:15 -0800290 /**
291 * Displays the memory usage of each cache and the total sum.
292 */
293 void dumpMemoryUsage();
Chet Haase9c1e23b2011-03-24 10:51:31 -0700294 void dumpMemoryUsage(String8& log);
Romain Guyc15008e2010-11-10 11:59:15 -0800295
Romain Guy54c1a642012-09-27 17:55:46 -0700296 bool hasRegisteredFunctors();
297 void registerFunctors(uint32_t functorCount);
298 void unregisterFunctors(uint32_t functorCount);
299
Romain Guyfb8b7632010-08-23 21:05:08 -0700300 bool blend;
301 GLenum lastSrcMode;
302 GLenum lastDstMode;
303 Program* currentProgram;
Romain Guy586cae32012-07-13 15:28:31 -0700304 bool scissorEnabled;
Romain Guyfb8b7632010-08-23 21:05:08 -0700305
Romain Guy0f6675332013-03-01 14:31:04 -0800306 bool drawDeferDisabled;
307 bool drawReorderDisabled;
308
Romain Guy746b7402010-10-26 16:27:31 -0700309 // VBO to draw with
Romain Guy03750a02010-10-18 14:06:08 -0700310 GLuint meshBuffer;
Romain Guy03750a02010-10-18 14:06:08 -0700311
Romain Guy746b7402010-10-26 16:27:31 -0700312 // Misc
313 GLint maxTextureSize;
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800314
315 // Debugging
Romain Guy4ff0cf42012-08-06 14:51:10 -0700316 bool debugLayersUpdates;
Romain Guy7c450aa2012-09-21 19:15:00 -0700317 bool debugOverdraw;
Romain Guy746b7402010-10-26 16:27:31 -0700318
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800319 enum StencilClipDebug {
320 kStencilHide,
321 kStencilShowHighlight,
322 kStencilShowRegion
323 };
324 StencilClipDebug debugStencilClip;
325
Romain Guyfb8b7632010-08-23 21:05:08 -0700326 TextureCache textureCache;
327 LayerCache layerCache;
Romain Guy8d4aeb72013-02-12 16:08:55 -0800328 RenderBufferCache renderBufferCache;
Romain Guyfb8b7632010-08-23 21:05:08 -0700329 GradientCache gradientCache;
330 ProgramCache programCache;
331 PathCache pathCache;
332 PatchCache patchCache;
333 TextDropShadowCache dropShadowCache;
Romain Guye2d345e2010-09-24 18:39:22 -0700334 FboCache fboCache;
Chet Haase5c13d892010-10-08 08:37:55 -0700335 ResourceCache resourceCache;
Romain Guy29d89972010-09-22 16:10:57 -0700336
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700337 GammaFontRenderer* fontRenderer;
338
Romain Guy5dc7fa72013-03-11 20:48:31 -0700339 TaskManager tasks;
340
Romain Guy0baaac52012-08-31 20:31:01 -0700341 Dither dither;
Romain Guy0baaac52012-08-31 20:31:01 -0700342 Stencil stencil;
Romain Guy0baaac52012-08-31 20:31:01 -0700343
Romain Guy3b748a42013-04-17 18:54:38 -0700344 AssetAtlas assetAtlas;
345
Romain Guyf9f00162013-05-09 11:50:12 -0700346 bool gpuPixelBuffersEnabled;
347
Romain Guydfa10462012-05-12 16:18:58 -0700348 // Debug methods
Romain Guy13631f32012-01-30 17:41:55 -0800349 PFNGLINSERTEVENTMARKEREXTPROC eventMark;
350 PFNGLPUSHGROUPMARKEREXTPROC startMark;
351 PFNGLPOPGROUPMARKEREXTPROC endMark;
352
Romain Guydfa10462012-05-12 16:18:58 -0700353 PFNGLLABELOBJECTEXTPROC setLabel;
354 PFNGLGETOBJECTLABELEXTPROC getLabel;
355
Romain Guye190aa62010-11-10 19:01:29 -0800356private:
Romain Guy627c6fd2013-08-21 11:53:18 -0700357 enum OverdrawColorSet {
358 kColorSet_Default = 0,
359 kColorSet_Deuteranomaly
360 };
361
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700362 void initFont();
Romain Guydfa10462012-05-12 16:18:58 -0700363 void initExtensions();
364 void initConstraints();
Romain Guyf9f00162013-05-09 11:50:12 -0700365 void initStaticProperties();
Romain Guydfa10462012-05-12 16:18:58 -0700366
367 static void eventMarkNull(GLsizei length, const GLchar* marker) { }
368 static void startMarkNull(GLsizei length, const GLchar* marker) { }
Romain Guy13631f32012-01-30 17:41:55 -0800369 static void endMarkNull() { }
370
Romain Guydfa10462012-05-12 16:18:58 -0700371 static void setLabelNull(GLenum type, uint object, GLsizei length,
372 const char* label) { }
373 static void getLabelNull(GLenum type, uint object, GLsizei bufferSize,
374 GLsizei* length, char* label) {
375 if (length) *length = 0;
376 if (label) *label = '\0';
377 }
378
Romain Guyf3a910b42011-12-12 20:35:21 -0800379 GLuint mCurrentBuffer;
Romain Guy15bc6432011-12-13 13:11:32 -0800380 GLuint mCurrentIndicesBuffer;
Romain Guycf51a412013-04-08 19:40:31 -0700381 GLuint mCurrentPixelBuffer;
Romain Guyf3a910b42011-12-12 20:35:21 -0800382 void* mCurrentPositionPointer;
Chris Craik16b897c2012-09-27 13:10:56 -0700383 GLsizei mCurrentPositionStride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800384 void* mCurrentTexCoordsPointer;
Romain Guyff316ec2013-02-13 18:39:43 -0800385 GLsizei mCurrentTexCoordsStride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800386
Romain Guy15bc6432011-12-13 13:11:32 -0800387 bool mTexCoordsArrayEnabled;
388
Romain Guya1d3c912011-12-13 14:55:06 -0800389 GLuint mTextureUnit;
390
Romain Guy8f85e802011-12-14 19:23:32 -0800391 GLint mScissorX;
392 GLint mScissorY;
393 GLint mScissorWidth;
394 GLint mScissorHeight;
395
Romain Guy3bbacf22013-02-06 16:51:04 -0800396 Extensions& mExtensions;
397
Romain Guyf3a910b42011-12-12 20:35:21 -0800398 // Used to render layers
399 TextureVertex* mRegionMesh;
Romain Guy3b748a42013-04-17 18:54:38 -0700400
401 // Global index buffer
402 GLuint mMeshIndices;
Romain Guyf3a910b42011-12-12 20:35:21 -0800403
404 mutable Mutex mGarbageLock;
405 Vector<Layer*> mLayerGarbage;
Romain Guybb0acdf2012-03-05 13:44:35 -0800406 Vector<DisplayList*> mDisplayListGarbage;
Romain Guyf3a910b42011-12-12 20:35:21 -0800407
Romain Guye190aa62010-11-10 19:01:29 -0800408 DebugLevel mDebugLevel;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800409 bool mInitialized;
Romain Guy54c1a642012-09-27 17:55:46 -0700410
411 uint32_t mFunctorsCount;
Romain Guy8aa195d2013-06-04 18:00:09 -0700412
413 GLuint mBoundTextures[REQUIRED_TEXTURE_UNITS_COUNT];
Romain Guy627c6fd2013-08-21 11:53:18 -0700414
415 OverdrawColorSet mOverdrawDebugColorSet;
Romain Guyfb8b7632010-08-23 21:05:08 -0700416}; // class Caches
417
418}; // namespace uirenderer
Romain Guyfb8b7632010-08-23 21:05:08 -0700419}; // namespace android
420
Romain Guy5b3b3522010-10-27 18:57:51 -0700421#endif // ANDROID_HWUI_CACHES_H