1 /* 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 17 #ifndef ANDROID_HWUI_CACHES_H 18 #define ANDROID_HWUI_CACHES_H 19 20 #ifndef LOG_TAG 21 #define LOG_TAG "OpenGLRenderer" 22 #endif 23 24 #include <GLES3/gl3.h> 25 26 #include <utils/KeyedVector.h> 27 #include <utils/Singleton.h> 28 #include <utils/Vector.h> 29 30 #include <cutils/compiler.h> 31 32 #include "thread/TaskProcessor.h" 33 #include "thread/TaskManager.h" 34 35 #include "AssetAtlas.h" 36 #include "FontRenderer.h" 37 #include "GammaFontRenderer.h" 38 #include "TextureCache.h" 39 #include "LayerCache.h" 40 #include "RenderBufferCache.h" 41 #include "GradientCache.h" 42 #include "PatchCache.h" 43 #include "ProgramCache.h" 44 #include "PathCache.h" 45 #include "TextDropShadowCache.h" 46 #include "FboCache.h" 47 #include "ResourceCache.h" 48 #include "Stencil.h" 49 #include "Dither.h" 50 51 namespace android { 52 namespace uirenderer { 53 54 /////////////////////////////////////////////////////////////////////////////// 55 // Globals 56 /////////////////////////////////////////////////////////////////////////////// 57 58 // GL ES 2.0 defines that at least 16 texture units must be supported 59 #define REQUIRED_TEXTURE_UNITS_COUNT 3 60 61 // Maximum number of quads that pre-allocated meshes can draw 62 static const uint32_t gMaxNumberOfQuads = 2048; 63 64 // 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 69 static 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 }; 75 static const GLsizei gMeshStride = sizeof(TextureVertex); 76 static const GLsizei gVertexStride = sizeof(Vertex); 77 static const GLsizei gAlphaVertexStride = sizeof(AlphaVertex); 78 static const GLsizei gMeshTextureOffset = 2 * sizeof(float); 79 static const GLsizei gVertexAlphaOffset = 2 * sizeof(float); 80 static const GLsizei gVertexAAWidthOffset = 2 * sizeof(float); 81 static const GLsizei gVertexAALengthOffset = 3 * sizeof(float); 82 static const GLsizei gMeshCount = 4; 83 84 // Must define as many texture units as specified by REQUIRED_TEXTURE_UNITS_COUNT 85 static const GLenum gTextureUnits[] = { 86 GL_TEXTURE0, 87 GL_TEXTURE1, 88 GL_TEXTURE2 89 }; 90 91 /////////////////////////////////////////////////////////////////////////////// 92 // Debug 93 /////////////////////////////////////////////////////////////////////////////// 94 95 struct CacheLogger { 96 CacheLogger() { 97 INIT_LOGD("Creating OpenGL renderer caches"); 98 } 99 }; // struct CacheLogger 100 101 /////////////////////////////////////////////////////////////////////////////// 102 // Caches 103 /////////////////////////////////////////////////////////////////////////////// 104 105 class DisplayList; 106 107 class ANDROID_API Caches: public Singleton<Caches> { 108 Caches(); 109 110 friend class Singleton<Caches>; 111 112 CacheLogger mLogger; 113 114 public: 115 enum FlushMode { 116 kFlushMode_Layers = 0, 117 kFlushMode_Moderate, 118 kFlushMode_Full 119 }; 120 121 /** 122 * Initialize caches. 123 */ 124 bool init(); 125 126 /** 127 * Initialize global system properties. 128 */ 129 bool initProperties(); 130 131 /** 132 * Flush the cache. 133 * 134 * @param mode Indicates how much of the cache should be flushed 135 */ 136 void flush(FlushMode mode); 137 138 /** 139 * Destroys all resources associated with this cache. This should 140 * be called after a flush(kFlushMode_Full). 141 */ 142 void terminate(); 143 144 /** 145 * 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 /** 153 * 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 /** 159 * Call this on each frame to ensure that garbage is deleted from 160 * GPU memory. 161 */ 162 void clearGarbage(); 163 164 /** 165 * Can be used to delete a layer from a non EGL thread. 166 */ 167 void deleteLayerDeferred(Layer* layer); 168 169 /* 170 * Can be used to delete a display list from a non EGL thread. 171 */ 172 void deleteDisplayListDeferred(DisplayList* layer); 173 174 /** 175 * Binds the VBO used to render simple textured quads. 176 */ 177 bool bindMeshBuffer(); 178 179 /** 180 * Binds the specified VBO if needed. 181 */ 182 bool bindMeshBuffer(const GLuint buffer); 183 184 /** 185 * Unbinds the VBO used to render simple textured quads. 186 */ 187 bool unbindMeshBuffer(); 188 189 /** 190 * Binds a global indices buffer that can draw up to 191 * gMaxNumberOfQuads quads. 192 */ 193 bool bindIndicesBuffer(); 194 bool bindIndicesBuffer(const GLuint buffer); 195 bool unbindIndicesBuffer(); 196 197 /** 198 * 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 /** 208 * Binds an attrib to the specified float vertex pointer. 209 * Assumes a stride of gMeshStride and a size of 2. 210 */ 211 void bindPositionVertexPointer(bool force, GLvoid* vertices, GLsizei stride = gMeshStride); 212 213 /** 214 * Binds an attrib to the specified float vertex pointer. 215 * Assumes a stride of gMeshStride and a size of 2. 216 */ 217 void bindTexCoordsVertexPointer(bool force, GLvoid* vertices, GLsizei stride = gMeshStride); 218 219 /** 220 * Resets the vertex pointers. 221 */ 222 void resetVertexPointers(); 223 void resetTexCoordsVertexPointer(); 224 225 void enableTexCoordsVertexArray(); 226 void disableTexCoordsVertexArray(); 227 228 /** 229 * 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 /** 235 * Invalidate the cached value of the active texture unit. 236 */ 237 void resetActiveTexture(); 238 239 /** 240 * Binds the specified texture as a GL_TEXTURE_2D texture. 241 * All texture bindings must be performed with this method or 242 * bindTexture(GLenum, GLuint). 243 */ 244 void bindTexture(GLuint texture); 245 246 /** 247 * Binds the specified texture with the specified render target. 248 * All texture bindings must be performed with this method or 249 * bindTexture(GLuint). 250 */ 251 void bindTexture(GLenum target, GLuint texture); 252 253 /** 254 * 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 /** 261 * 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 /** 267 * Sets the scissor for the current surface. 268 */ 269 bool setScissor(GLint x, GLint y, GLint width, GLint height); 270 271 /** 272 * Resets the scissor state. 273 */ 274 void resetScissor(); 275 276 bool enableScissor(); 277 bool disableScissor(); 278 void setScissorEnabled(bool enabled); 279 280 void startTiling(GLuint x, GLuint y, GLuint width, GLuint height, bool discard); 281 void endTiling(); 282 283 /** 284 * 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 290 /** 291 * Displays the memory usage of each cache and the total sum. 292 */ 293 void dumpMemoryUsage(); 294 void dumpMemoryUsage(String8& log); 295 296 bool hasRegisteredFunctors(); 297 void registerFunctors(uint32_t functorCount); 298 void unregisterFunctors(uint32_t functorCount); 299 300 bool blend; 301 GLenum lastSrcMode; 302 GLenum lastDstMode; 303 Program* currentProgram; 304 bool scissorEnabled; 305 306 bool drawDeferDisabled; 307 bool drawReorderDisabled; 308 309 // VBO to draw with 310 GLuint meshBuffer; 311 312 // Misc 313 GLint maxTextureSize; 314 315 // Debugging 316 bool debugLayersUpdates; 317 bool debugOverdraw; 318 319 enum StencilClipDebug { 320 kStencilHide, 321 kStencilShowHighlight, 322 kStencilShowRegion 323 }; 324 StencilClipDebug debugStencilClip; 325 326 TextureCache textureCache; 327 LayerCache layerCache; 328 RenderBufferCache renderBufferCache; 329 GradientCache gradientCache; 330 ProgramCache programCache; 331 PathCache pathCache; 332 PatchCache patchCache; 333 TextDropShadowCache dropShadowCache; 334 FboCache fboCache; 335 ResourceCache resourceCache; 336 337 GammaFontRenderer* fontRenderer; 338 339 TaskManager tasks; 340 341 Dither dither; 342 Stencil stencil; 343 344 AssetAtlas assetAtlas; 345 346 bool gpuPixelBuffersEnabled; 347 348 // Debug methods 349 PFNGLINSERTEVENTMARKEREXTPROC eventMark; 350 PFNGLPUSHGROUPMARKEREXTPROC startMark; 351 PFNGLPOPGROUPMARKEREXTPROC endMark; 352 353 PFNGLLABELOBJECTEXTPROC setLabel; 354 PFNGLGETOBJECTLABELEXTPROC getLabel; 355 356 private: 357 enum OverdrawColorSet { 358 kColorSet_Default = 0, 359 kColorSet_Deuteranomaly 360 }; 361 362 void initFont(); 363 void initExtensions(); 364 void initConstraints(); 365 void initStaticProperties(); 366 367 static void eventMarkNull(GLsizei length, const GLchar* marker) { } 368 static void startMarkNull(GLsizei length, const GLchar* marker) { } 369 static void endMarkNull() { } 370 371 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 379 GLuint mCurrentBuffer; 380 GLuint mCurrentIndicesBuffer; 381 GLuint mCurrentPixelBuffer; 382 void* mCurrentPositionPointer; 383 GLsizei mCurrentPositionStride; 384 void* mCurrentTexCoordsPointer; 385 GLsizei mCurrentTexCoordsStride; 386 387 bool mTexCoordsArrayEnabled; 388 389 GLuint mTextureUnit; 390 391 GLint mScissorX; 392 GLint mScissorY; 393 GLint mScissorWidth; 394 GLint mScissorHeight; 395 396 Extensions& mExtensions; 397 398 // Used to render layers 399 TextureVertex* mRegionMesh; 400 401 // Global index buffer 402 GLuint mMeshIndices; 403 404 mutable Mutex mGarbageLock; 405 Vector<Layer*> mLayerGarbage; 406 Vector<DisplayList*> mDisplayListGarbage; 407 408 DebugLevel mDebugLevel; 409 bool mInitialized; 410 411 uint32_t mFunctorsCount; 412 413 GLuint mBoundTextures[REQUIRED_TEXTURE_UNITS_COUNT]; 414 415 OverdrawColorSet mOverdrawDebugColorSet; 416 }; // class Caches 417 418 }; // namespace uirenderer 419 }; // namespace android 420 421 #endif // ANDROID_HWUI_CACHES_H 422