Caches.h revision 2ab8298dc37851aab4623ba3f98d71055d653a73
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 55namespace android { 56namespace 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 66static 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 73static 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}; 79static const GLsizei gMeshStride = sizeof(TextureVertex); 80static const GLsizei gVertexStride = sizeof(Vertex); 81static const GLsizei gAlphaVertexStride = sizeof(AlphaVertex); 82static const GLsizei gMeshTextureOffset = 2 * sizeof(float); 83static const GLsizei gVertexAlphaOffset = 2 * sizeof(float); 84static const GLsizei gVertexAAWidthOffset = 2 * sizeof(float); 85static const GLsizei gVertexAALengthOffset = 3 * sizeof(float); 86static const GLsizei gMeshCount = 4; 87 88// Must define as many texture units as specified by REQUIRED_TEXTURE_UNITS_COUNT 89static const GLenum gTextureUnits[] = { 90 GL_TEXTURE0, 91 GL_TEXTURE1, 92 GL_TEXTURE2 93}; 94 95/////////////////////////////////////////////////////////////////////////////// 96// Debug 97/////////////////////////////////////////////////////////////////////////////// 98 99struct CacheLogger { 100 CacheLogger() { 101 INIT_LOGD("Creating OpenGL renderer caches"); 102 } 103}; // struct CacheLogger 104 105/////////////////////////////////////////////////////////////////////////////// 106// Caches 107/////////////////////////////////////////////////////////////////////////////// 108 109class RenderNode; 110class RenderState; 111 112class ANDROID_API Caches: public Singleton<Caches> { 113 Caches(); 114 115 friend class Singleton<Caches>; 116 117 CacheLogger mLogger; 118 119public: 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 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 */ 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 AssetAtlas assetAtlas; 352 353 bool gpuPixelBuffersEnabled; 354 355 // Debug methods 356 PFNGLINSERTEVENTMARKEREXTPROC eventMark; 357 PFNGLPUSHGROUPMARKEREXTPROC startMark; 358 PFNGLPOPGROUPMARKEREXTPROC endMark; 359 360 PFNGLLABELOBJECTEXTPROC setLabel; 361 PFNGLGETOBJECTLABELEXTPROC getLabel; 362 363 // TEMPORARY properties 364 void initTempProperties(); 365 void setTempProperty(const char* name, const char* value); 366 367 float propertyLightDiameter; 368 float propertyLightPosY; 369 float propertyLightPosZ; 370 float propertyAmbientRatio; 371 int propertyAmbientShadowStrength; 372 int propertySpotShadowStrength; 373 374private: 375 enum OverdrawColorSet { 376 kColorSet_Default = 0, 377 kColorSet_Deuteranomaly 378 }; 379 380 void initFont(); 381 void initExtensions(); 382 void initConstraints(); 383 void initStaticProperties(); 384 385 bool bindIndicesBufferInternal(const GLuint buffer); 386 387 static void eventMarkNull(GLsizei /* length */, const GLchar* /* marker */) { } 388 static void startMarkNull(GLsizei /* length */, const GLchar* /* marker */) { } 389 static void endMarkNull() { } 390 391 static void setLabelNull(GLenum /* type */, uint /* object */, GLsizei /* length */, 392 const char* /* label */) { } 393 static void getLabelNull(GLenum /* type */, uint /* object */, GLsizei /* bufferSize */, 394 GLsizei* length, char* label) { 395 if (length) *length = 0; 396 if (label) *label = '\0'; 397 } 398 399 GLuint mCurrentBuffer; 400 GLuint mCurrentIndicesBuffer; 401 GLuint mCurrentPixelBuffer; 402 const void* mCurrentPositionPointer; 403 GLsizei mCurrentPositionStride; 404 const void* mCurrentTexCoordsPointer; 405 GLsizei mCurrentTexCoordsStride; 406 407 bool mTexCoordsArrayEnabled; 408 409 GLuint mTextureUnit; 410 411 GLint mScissorX; 412 GLint mScissorY; 413 GLint mScissorWidth; 414 GLint mScissorHeight; 415 416 Extensions& mExtensions; 417 418 // Used to render layers 419 TextureVertex* mRegionMesh; 420 421 // Global index buffer 422 GLuint mMeshIndices; 423 GLuint mShadowStripsIndices; 424 425 mutable Mutex mGarbageLock; 426 Vector<Layer*> mLayerGarbage; 427 428 DebugLevel mDebugLevel; 429 bool mInitialized; 430 431 uint32_t mFunctorsCount; 432 433 // Caches texture bindings for the GL_TEXTURE_2D target 434 GLuint mBoundTextures[REQUIRED_TEXTURE_UNITS_COUNT]; 435 436 OverdrawColorSet mOverdrawDebugColorSet; 437 438 RenderState* mRenderState; 439}; // class Caches 440 441}; // namespace uirenderer 442}; // namespace android 443 444#endif // ANDROID_HWUI_CACHES_H 445