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 <vector> 25 26 #include <GLES3/gl3.h> 27 28 #include <utils/KeyedVector.h> 29 #include <utils/Singleton.h> 30 #include <utils/Vector.h> 31 32 #include <cutils/compiler.h> 33 34 #include "thread/TaskProcessor.h" 35 #include "thread/TaskManager.h" 36 37 #include "AssetAtlas.h" 38 #include "Extensions.h" 39 #include "FontRenderer.h" 40 #include "GammaFontRenderer.h" 41 #include "TextureCache.h" 42 #include "LayerCache.h" 43 #include "RenderBufferCache.h" 44 #include "GradientCache.h" 45 #include "PatchCache.h" 46 #include "ProgramCache.h" 47 #include "PathCache.h" 48 #include "TessellationCache.h" 49 #include "TextDropShadowCache.h" 50 #include "FboCache.h" 51 #include "ResourceCache.h" 52 #include "Stencil.h" 53 #include "Dither.h" 54 55 namespace android { 56 namespace uirenderer { 57 58 /////////////////////////////////////////////////////////////////////////////// 59 // Globals 60 /////////////////////////////////////////////////////////////////////////////// 61 62 // GL ES 2.0 defines that at least 16 texture units must be supported 63 #define REQUIRED_TEXTURE_UNITS_COUNT 3 64 65 // Maximum number of quads that pre-allocated meshes can draw 66 static const uint32_t gMaxNumberOfQuads = 2048; 67 68 // Generates simple and textured vertices 69 #define FV(x, y, u, v) { x, y, u, v } 70 71 // This array is never used directly but used as a memcpy source in the 72 // OpenGLRenderer constructor 73 static const TextureVertex gMeshVertices[] = { 74 FV(0.0f, 0.0f, 0.0f, 0.0f), 75 FV(1.0f, 0.0f, 1.0f, 0.0f), 76 FV(0.0f, 1.0f, 0.0f, 1.0f), 77 FV(1.0f, 1.0f, 1.0f, 1.0f) 78 }; 79 static const GLsizei gMeshStride = sizeof(TextureVertex); 80 static const GLsizei gVertexStride = sizeof(Vertex); 81 static const GLsizei gAlphaVertexStride = sizeof(AlphaVertex); 82 static const GLsizei gMeshTextureOffset = 2 * sizeof(float); 83 static const GLsizei gVertexAlphaOffset = 2 * sizeof(float); 84 static const GLsizei gVertexAAWidthOffset = 2 * sizeof(float); 85 static const GLsizei gVertexAALengthOffset = 3 * sizeof(float); 86 static const GLsizei gMeshCount = 4; 87 88 // Must define as many texture units as specified by REQUIRED_TEXTURE_UNITS_COUNT 89 static const GLenum gTextureUnits[] = { 90 GL_TEXTURE0, 91 GL_TEXTURE1, 92 GL_TEXTURE2 93 }; 94 95 /////////////////////////////////////////////////////////////////////////////// 96 // Debug 97 /////////////////////////////////////////////////////////////////////////////// 98 99 struct CacheLogger { CacheLoggerCacheLogger100 CacheLogger() { 101 INIT_LOGD("Creating OpenGL renderer caches"); 102 } 103 }; // struct CacheLogger 104 105 /////////////////////////////////////////////////////////////////////////////// 106 // Caches 107 /////////////////////////////////////////////////////////////////////////////// 108 109 class RenderNode; 110 class RenderState; 111 112 class ANDROID_API Caches: public Singleton<Caches> { 113 Caches(); 114 115 friend class Singleton<Caches>; 116 117 CacheLogger mLogger; 118 119 public: 120 enum FlushMode { 121 kFlushMode_Layers = 0, 122 kFlushMode_Moderate, 123 kFlushMode_Full 124 }; 125 126 /** 127 * Initialize caches. 128 */ 129 bool init(); 130 131 /** 132 * Initialize global system properties. 133 */ 134 bool initProperties(); 135 setRenderState(RenderState * renderState)136 void setRenderState(RenderState* renderState) { mRenderState = renderState; } 137 138 /** 139 * Flush the cache. 140 * 141 * @param mode Indicates how much of the cache should be flushed 142 */ 143 void flush(FlushMode mode); 144 145 /** 146 * Destroys all resources associated with this cache. This should 147 * be called after a flush(kFlushMode_Full). 148 */ 149 void terminate(); 150 151 /** 152 * Indicates whether the renderer is in debug mode. 153 * This debug mode provides limited information to app developers. 154 */ getDebugLevel()155 DebugLevel getDebugLevel() const { 156 return mDebugLevel; 157 } 158 159 /** 160 * Returns a non-premultiplied ARGB color for the specified 161 * amount of overdraw (1 for 1x, 2 for 2x, etc.) 162 */ 163 uint32_t getOverdrawColor(uint32_t amount) const; 164 165 /** 166 * Call this on each frame to ensure that garbage is deleted from 167 * GPU memory. 168 */ 169 void clearGarbage(); 170 171 /** 172 * Can be used to delete a layer from a non EGL thread. 173 */ 174 void deleteLayerDeferred(Layer* layer); 175 176 /** 177 * Binds the VBO used to render simple textured quads. 178 */ 179 bool bindMeshBuffer(); 180 181 /** 182 * Binds the specified VBO if needed. 183 */ 184 bool bindMeshBuffer(const GLuint buffer); 185 186 /** 187 * Unbinds the VBO used to render simple textured quads. 188 */ 189 bool unbindMeshBuffer(); 190 191 /** 192 * Binds a global indices buffer that can draw up to 193 * gMaxNumberOfQuads quads. 194 */ 195 bool bindQuadIndicesBuffer(); 196 bool bindShadowIndicesBuffer(); 197 bool unbindIndicesBuffer(); 198 199 /** 200 * Binds the specified buffer as the current GL unpack pixel buffer. 201 */ 202 bool bindPixelBuffer(const GLuint buffer); 203 204 /** 205 * Resets the current unpack pixel buffer to 0 (default value.) 206 */ 207 bool unbindPixelBuffer(); 208 209 /** 210 * Binds an attrib to the specified float vertex pointer. 211 * Assumes a stride of gMeshStride and a size of 2. 212 */ 213 void bindPositionVertexPointer(bool force, const GLvoid* vertices, GLsizei stride = gMeshStride); 214 215 /** 216 * Binds an attrib to the specified float vertex pointer. 217 * Assumes a stride of gMeshStride and a size of 2. 218 */ 219 void bindTexCoordsVertexPointer(bool force, const GLvoid* vertices, GLsizei stride = gMeshStride); 220 221 /** 222 * Resets the vertex pointers. 223 */ 224 void resetVertexPointers(); 225 void resetTexCoordsVertexPointer(); 226 227 void enableTexCoordsVertexArray(); 228 void disableTexCoordsVertexArray(); 229 230 /** 231 * Activate the specified texture unit. The texture unit must 232 * be specified using an integer number (0 for GL_TEXTURE0 etc.) 233 */ 234 void activeTexture(GLuint textureUnit); 235 236 /** 237 * Invalidate the cached value of the active texture unit. 238 */ 239 void resetActiveTexture(); 240 241 /** 242 * Binds the specified texture as a GL_TEXTURE_2D texture. 243 * All texture bindings must be performed with this method or 244 * bindTexture(GLenum, GLuint). 245 */ 246 void bindTexture(GLuint texture); 247 248 /** 249 * Binds the specified texture with the specified render target. 250 * All texture bindings must be performed with this method or 251 * bindTexture(GLuint). 252 */ 253 void bindTexture(GLenum target, GLuint texture); 254 255 /** 256 * Deletes the specified texture and clears it from the cache 257 * of bound textures. 258 * All textures must be deleted using this method. 259 */ 260 void deleteTexture(GLuint texture); 261 262 /** 263 * Signals that the cache of bound textures should be cleared. 264 * Other users of the context may have altered which textures are bound. 265 */ 266 void resetBoundTextures(); 267 268 /** 269 * Clear the cache of bound textures. 270 */ 271 void unbindTexture(GLuint texture); 272 273 /** 274 * Sets the scissor for the current surface. 275 */ 276 bool setScissor(GLint x, GLint y, GLint width, GLint height); 277 278 /** 279 * Resets the scissor state. 280 */ 281 void resetScissor(); 282 283 bool enableScissor(); 284 bool disableScissor(); 285 void setScissorEnabled(bool enabled); 286 287 void startTiling(GLuint x, GLuint y, GLuint width, GLuint height, bool discard); 288 void endTiling(); 289 290 /** 291 * Returns the mesh used to draw regions. Calling this method will 292 * bind a VBO of type GL_ELEMENT_ARRAY_BUFFER that contains the 293 * indices for the region mesh. 294 */ 295 TextureVertex* getRegionMesh(); 296 297 /** 298 * Displays the memory usage of each cache and the total sum. 299 */ 300 void dumpMemoryUsage(); 301 void dumpMemoryUsage(String8& log); 302 303 bool hasRegisteredFunctors(); 304 void registerFunctors(uint32_t functorCount); 305 void unregisterFunctors(uint32_t functorCount); 306 307 bool blend; 308 GLenum lastSrcMode; 309 GLenum lastDstMode; 310 Program* currentProgram; 311 bool scissorEnabled; 312 313 bool drawDeferDisabled; 314 bool drawReorderDisabled; 315 316 // VBO to draw with 317 GLuint meshBuffer; 318 319 // Misc 320 GLint maxTextureSize; 321 322 // Debugging 323 bool debugLayersUpdates; 324 bool debugOverdraw; 325 326 enum StencilClipDebug { 327 kStencilHide, 328 kStencilShowHighlight, 329 kStencilShowRegion 330 }; 331 StencilClipDebug debugStencilClip; 332 333 TextureCache textureCache; 334 LayerCache layerCache; 335 RenderBufferCache renderBufferCache; 336 GradientCache gradientCache; 337 ProgramCache programCache; 338 PathCache pathCache; 339 PatchCache patchCache; 340 TessellationCache tessellationCache; 341 TextDropShadowCache dropShadowCache; 342 FboCache fboCache; 343 344 GammaFontRenderer* fontRenderer; 345 346 TaskManager tasks; 347 348 Dither dither; 349 Stencil stencil; 350 351 bool gpuPixelBuffersEnabled; 352 353 // Debug methods 354 PFNGLINSERTEVENTMARKEREXTPROC eventMark; 355 PFNGLPUSHGROUPMARKEREXTPROC startMark; 356 PFNGLPOPGROUPMARKEREXTPROC endMark; 357 358 PFNGLLABELOBJECTEXTPROC setLabel; 359 PFNGLGETOBJECTLABELEXTPROC getLabel; 360 361 // TEMPORARY properties 362 void initTempProperties(); 363 void setTempProperty(const char* name, const char* value); 364 365 float propertyLightDiameter; 366 float propertyLightPosY; 367 float propertyLightPosZ; 368 float propertyAmbientRatio; 369 int propertyAmbientShadowStrength; 370 int propertySpotShadowStrength; 371 372 private: 373 enum OverdrawColorSet { 374 kColorSet_Default = 0, 375 kColorSet_Deuteranomaly 376 }; 377 378 void initFont(); 379 void initExtensions(); 380 void initConstraints(); 381 void initStaticProperties(); 382 383 bool bindIndicesBufferInternal(const GLuint buffer); 384 eventMarkNull(GLsizei length,const GLchar * marker)385 static void eventMarkNull(GLsizei length, const GLchar* marker) { } startMarkNull(GLsizei length,const GLchar * marker)386 static void startMarkNull(GLsizei length, const GLchar* marker) { } endMarkNull()387 static void endMarkNull() { } 388 setLabelNull(GLenum type,uint object,GLsizei length,const char * label)389 static void setLabelNull(GLenum type, uint object, GLsizei length, 390 const char* label) { } getLabelNull(GLenum type,uint object,GLsizei bufferSize,GLsizei * length,char * label)391 static void getLabelNull(GLenum type, uint object, GLsizei bufferSize, 392 GLsizei* length, char* label) { 393 if (length) *length = 0; 394 if (label) *label = '\0'; 395 } 396 397 GLuint mCurrentBuffer; 398 GLuint mCurrentIndicesBuffer; 399 GLuint mCurrentPixelBuffer; 400 const void* mCurrentPositionPointer; 401 GLsizei mCurrentPositionStride; 402 const void* mCurrentTexCoordsPointer; 403 GLsizei mCurrentTexCoordsStride; 404 405 bool mTexCoordsArrayEnabled; 406 407 GLuint mTextureUnit; 408 409 GLint mScissorX; 410 GLint mScissorY; 411 GLint mScissorWidth; 412 GLint mScissorHeight; 413 414 Extensions& mExtensions; 415 416 // Used to render layers 417 TextureVertex* mRegionMesh; 418 419 // Global index buffer 420 GLuint mMeshIndices; 421 GLuint mShadowStripsIndices; 422 423 mutable Mutex mGarbageLock; 424 Vector<Layer*> mLayerGarbage; 425 426 DebugLevel mDebugLevel; 427 bool mInitialized; 428 429 uint32_t mFunctorsCount; 430 431 // Caches texture bindings for the GL_TEXTURE_2D target 432 GLuint mBoundTextures[REQUIRED_TEXTURE_UNITS_COUNT]; 433 434 OverdrawColorSet mOverdrawDebugColorSet; 435 436 RenderState* mRenderState; 437 }; // class Caches 438 439 }; // namespace uirenderer 440 }; // namespace android 441 442 #endif // ANDROID_HWUI_CACHES_H 443