Caches.cpp revision c5cbee7d78513527e89450e6369a30a04b2d5e7a
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#define LOG_TAG "OpenGLRenderer" 18 19#include <utils/Log.h> 20#include <utils/String8.h> 21 22#include "Caches.h" 23#include "DisplayListRenderer.h" 24#include "Properties.h" 25#include "LayerRenderer.h" 26 27namespace android { 28 29#ifdef USE_OPENGL_RENDERER 30using namespace uirenderer; 31ANDROID_SINGLETON_STATIC_INSTANCE(Caches); 32#endif 33 34namespace uirenderer { 35 36/////////////////////////////////////////////////////////////////////////////// 37// Macros 38/////////////////////////////////////////////////////////////////////////////// 39 40#if DEBUG_CACHE_FLUSH 41 #define FLUSH_LOGD(...) ALOGD(__VA_ARGS__) 42#else 43 #define FLUSH_LOGD(...) 44#endif 45 46/////////////////////////////////////////////////////////////////////////////// 47// Constructors/destructor 48/////////////////////////////////////////////////////////////////////////////// 49 50Caches::Caches(): Singleton<Caches>(), mExtensions(Extensions::getInstance()), mInitialized(false) { 51 init(); 52 initFont(); 53 initConstraints(); 54 initProperties(); 55 initExtensions(); 56 57 mDebugLevel = readDebugLevel(); 58 ALOGD("Enabling debug mode %d", mDebugLevel); 59} 60 61void Caches::init() { 62 if (mInitialized) return; 63 64 glGenBuffers(1, &meshBuffer); 65 glBindBuffer(GL_ARRAY_BUFFER, meshBuffer); 66 glBufferData(GL_ARRAY_BUFFER, sizeof(gMeshVertices), gMeshVertices, GL_STATIC_DRAW); 67 68 mCurrentBuffer = meshBuffer; 69 mCurrentIndicesBuffer = 0; 70 mCurrentPositionPointer = this; 71 mCurrentPositionStride = 0; 72 mCurrentTexCoordsPointer = this; 73 74 mTexCoordsArrayEnabled = false; 75 76 glDisable(GL_SCISSOR_TEST); 77 scissorEnabled = false; 78 mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0; 79 80 glActiveTexture(gTextureUnits[0]); 81 mTextureUnit = 0; 82 83 mRegionMesh = NULL; 84 85 blend = false; 86 lastSrcMode = GL_ZERO; 87 lastDstMode = GL_ZERO; 88 currentProgram = NULL; 89 90 mFunctorsCount = 0; 91 92 debugLayersUpdates = false; 93 debugOverdraw = false; 94 debugStencilClip = kStencilHide; 95 96 mInitialized = true; 97} 98 99void Caches::initFont() { 100 fontRenderer = GammaFontRenderer::createRenderer(); 101} 102 103void Caches::initExtensions() { 104 if (mExtensions.hasDebugMarker()) { 105 eventMark = glInsertEventMarkerEXT; 106 107 startMark = glPushGroupMarkerEXT; 108 endMark = glPopGroupMarkerEXT; 109 } else { 110 eventMark = eventMarkNull; 111 startMark = startMarkNull; 112 endMark = endMarkNull; 113 } 114 115 if (mExtensions.hasDebugLabel() && (drawDeferDisabled || drawReorderDisabled)) { 116 setLabel = glLabelObjectEXT; 117 getLabel = glGetObjectLabelEXT; 118 } else { 119 setLabel = setLabelNull; 120 getLabel = getLabelNull; 121 } 122} 123 124void Caches::initConstraints() { 125 GLint maxTextureUnits; 126 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits); 127 if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) { 128 ALOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT); 129 } 130 131 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize); 132} 133 134bool Caches::initProperties() { 135 bool prevDebugLayersUpdates = debugLayersUpdates; 136 bool prevDebugOverdraw = debugOverdraw; 137 StencilClipDebug prevDebugStencilClip = debugStencilClip; 138 139 char property[PROPERTY_VALUE_MAX]; 140 if (property_get(PROPERTY_DEBUG_LAYERS_UPDATES, property, NULL) > 0) { 141 INIT_LOGD(" Layers updates debug enabled: %s", property); 142 debugLayersUpdates = !strcmp(property, "true"); 143 } else { 144 debugLayersUpdates = false; 145 } 146 147 if (property_get(PROPERTY_DEBUG_OVERDRAW, property, NULL) > 0) { 148 INIT_LOGD(" Overdraw debug enabled: %s", property); 149 debugOverdraw = !strcmp(property, "true"); 150 } else { 151 debugOverdraw = false; 152 } 153 154 // See Properties.h for valid values 155 if (property_get(PROPERTY_DEBUG_STENCIL_CLIP, property, NULL) > 0) { 156 INIT_LOGD(" Stencil clip debug enabled: %s", property); 157 if (!strcmp(property, "hide")) { 158 debugStencilClip = kStencilHide; 159 } else if (!strcmp(property, "highlight")) { 160 debugStencilClip = kStencilShowHighlight; 161 } else if (!strcmp(property, "region")) { 162 debugStencilClip = kStencilShowRegion; 163 } 164 } else { 165 debugStencilClip = kStencilHide; 166 } 167 168 if (property_get(PROPERTY_DISABLE_DRAW_DEFER, property, "false")) { 169 drawDeferDisabled = !strcasecmp(property, "true"); 170 INIT_LOGD(" Draw defer %s", drawDeferDisabled ? "disabled" : "enabled"); 171 } else { 172 INIT_LOGD(" Draw defer enabled"); 173 } 174 175 if (property_get(PROPERTY_DISABLE_DRAW_REORDER, property, "false")) { 176 drawReorderDisabled = !strcasecmp(property, "true"); 177 INIT_LOGD(" Draw reorder %s", drawReorderDisabled ? "disabled" : "enabled"); 178 } else { 179 INIT_LOGD(" Draw reorder enabled"); 180 } 181 182 return (prevDebugLayersUpdates != debugLayersUpdates) || 183 (prevDebugOverdraw != debugOverdraw) || 184 (prevDebugStencilClip != debugStencilClip); 185} 186 187void Caches::terminate() { 188 if (!mInitialized) return; 189 190 glDeleteBuffers(1, &meshBuffer); 191 mCurrentBuffer = 0; 192 193 glDeleteBuffers(1, &mRegionMeshIndices); 194 delete[] mRegionMesh; 195 mRegionMesh = NULL; 196 197 fboCache.clear(); 198 199 programCache.clear(); 200 currentProgram = NULL; 201 202 mInitialized = false; 203} 204 205/////////////////////////////////////////////////////////////////////////////// 206// Debug 207/////////////////////////////////////////////////////////////////////////////// 208 209void Caches::dumpMemoryUsage() { 210 String8 stringLog; 211 dumpMemoryUsage(stringLog); 212 ALOGD("%s", stringLog.string()); 213} 214 215void Caches::dumpMemoryUsage(String8 &log) { 216 log.appendFormat("Current memory usage / total memory usage (bytes):\n"); 217 log.appendFormat(" TextureCache %8d / %8d\n", 218 textureCache.getSize(), textureCache.getMaxSize()); 219 log.appendFormat(" LayerCache %8d / %8d\n", 220 layerCache.getSize(), layerCache.getMaxSize()); 221 log.appendFormat(" RenderBufferCache %8d / %8d\n", 222 renderBufferCache.getSize(), renderBufferCache.getMaxSize()); 223 log.appendFormat(" GradientCache %8d / %8d\n", 224 gradientCache.getSize(), gradientCache.getMaxSize()); 225 log.appendFormat(" PathCache %8d / %8d\n", 226 pathCache.getSize(), pathCache.getMaxSize()); 227 log.appendFormat(" TextDropShadowCache %8d / %8d\n", dropShadowCache.getSize(), 228 dropShadowCache.getMaxSize()); 229 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) { 230 const uint32_t size = fontRenderer->getFontRendererSize(i); 231 log.appendFormat(" FontRenderer %d %8d / %8d\n", i, size, size); 232 } 233 log.appendFormat("Other:\n"); 234 log.appendFormat(" FboCache %8d / %8d\n", 235 fboCache.getSize(), fboCache.getMaxSize()); 236 log.appendFormat(" PatchCache %8d / %8d\n", 237 patchCache.getSize(), patchCache.getMaxSize()); 238 239 uint32_t total = 0; 240 total += textureCache.getSize(); 241 total += layerCache.getSize(); 242 total += renderBufferCache.getSize(); 243 total += gradientCache.getSize(); 244 total += pathCache.getSize(); 245 total += dropShadowCache.getSize(); 246 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) { 247 total += fontRenderer->getFontRendererSize(i); 248 } 249 250 log.appendFormat("Total memory usage:\n"); 251 log.appendFormat(" %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f); 252} 253 254/////////////////////////////////////////////////////////////////////////////// 255// Memory management 256/////////////////////////////////////////////////////////////////////////////// 257 258void Caches::clearGarbage() { 259 textureCache.clearGarbage(); 260 pathCache.clearGarbage(); 261 262 Vector<DisplayList*> displayLists; 263 Vector<Layer*> layers; 264 265 { // scope for the lock 266 Mutex::Autolock _l(mGarbageLock); 267 displayLists = mDisplayListGarbage; 268 layers = mLayerGarbage; 269 mDisplayListGarbage.clear(); 270 mLayerGarbage.clear(); 271 } 272 273 size_t count = displayLists.size(); 274 for (size_t i = 0; i < count; i++) { 275 DisplayList* displayList = displayLists.itemAt(i); 276 delete displayList; 277 } 278 279 count = layers.size(); 280 for (size_t i = 0; i < count; i++) { 281 Layer* layer = layers.itemAt(i); 282 delete layer; 283 } 284 layers.clear(); 285} 286 287void Caches::deleteLayerDeferred(Layer* layer) { 288 Mutex::Autolock _l(mGarbageLock); 289 mLayerGarbage.push(layer); 290} 291 292void Caches::deleteDisplayListDeferred(DisplayList* displayList) { 293 Mutex::Autolock _l(mGarbageLock); 294 mDisplayListGarbage.push(displayList); 295} 296 297void Caches::flush(FlushMode mode) { 298 FLUSH_LOGD("Flushing caches (mode %d)", mode); 299 300 switch (mode) { 301 case kFlushMode_Full: 302 textureCache.clear(); 303 patchCache.clear(); 304 dropShadowCache.clear(); 305 gradientCache.clear(); 306 fontRenderer->clear(); 307 dither.clear(); 308 // fall through 309 case kFlushMode_Moderate: 310 fontRenderer->flush(); 311 textureCache.flush(); 312 pathCache.clear(); 313 tasks.stop(); 314 // fall through 315 case kFlushMode_Layers: 316 layerCache.clear(); 317 renderBufferCache.clear(); 318 break; 319 } 320 321 clearGarbage(); 322} 323 324/////////////////////////////////////////////////////////////////////////////// 325// VBO 326/////////////////////////////////////////////////////////////////////////////// 327 328bool Caches::bindMeshBuffer() { 329 return bindMeshBuffer(meshBuffer); 330} 331 332bool Caches::bindMeshBuffer(const GLuint buffer) { 333 if (mCurrentBuffer != buffer) { 334 glBindBuffer(GL_ARRAY_BUFFER, buffer); 335 mCurrentBuffer = buffer; 336 return true; 337 } 338 return false; 339} 340 341bool Caches::unbindMeshBuffer() { 342 if (mCurrentBuffer) { 343 glBindBuffer(GL_ARRAY_BUFFER, 0); 344 mCurrentBuffer = 0; 345 return true; 346 } 347 return false; 348} 349 350bool Caches::bindIndicesBuffer(const GLuint buffer) { 351 if (mCurrentIndicesBuffer != buffer) { 352 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer); 353 mCurrentIndicesBuffer = buffer; 354 return true; 355 } 356 return false; 357} 358 359bool Caches::unbindIndicesBuffer() { 360 if (mCurrentIndicesBuffer) { 361 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); 362 mCurrentIndicesBuffer = 0; 363 return true; 364 } 365 return false; 366} 367 368/////////////////////////////////////////////////////////////////////////////// 369// Meshes and textures 370/////////////////////////////////////////////////////////////////////////////// 371 372void Caches::bindPositionVertexPointer(bool force, GLvoid* vertices, GLsizei stride) { 373 if (force || vertices != mCurrentPositionPointer || stride != mCurrentPositionStride) { 374 GLuint slot = currentProgram->position; 375 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices); 376 mCurrentPositionPointer = vertices; 377 mCurrentPositionStride = stride; 378 } 379} 380 381void Caches::bindTexCoordsVertexPointer(bool force, GLvoid* vertices, GLsizei stride) { 382 if (force || vertices != mCurrentTexCoordsPointer || stride != mCurrentTexCoordsStride) { 383 GLuint slot = currentProgram->texCoords; 384 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices); 385 mCurrentTexCoordsPointer = vertices; 386 mCurrentTexCoordsStride = stride; 387 } 388} 389 390void Caches::resetVertexPointers() { 391 mCurrentPositionPointer = this; 392 mCurrentTexCoordsPointer = this; 393} 394 395void Caches::resetTexCoordsVertexPointer() { 396 mCurrentTexCoordsPointer = this; 397} 398 399void Caches::enableTexCoordsVertexArray() { 400 if (!mTexCoordsArrayEnabled) { 401 glEnableVertexAttribArray(Program::kBindingTexCoords); 402 mCurrentTexCoordsPointer = this; 403 mTexCoordsArrayEnabled = true; 404 } 405} 406 407void Caches::disableTexCoordsVertexArray() { 408 if (mTexCoordsArrayEnabled) { 409 glDisableVertexAttribArray(Program::kBindingTexCoords); 410 mTexCoordsArrayEnabled = false; 411 } 412} 413 414void Caches::activeTexture(GLuint textureUnit) { 415 if (mTextureUnit != textureUnit) { 416 glActiveTexture(gTextureUnits[textureUnit]); 417 mTextureUnit = textureUnit; 418 } 419} 420 421/////////////////////////////////////////////////////////////////////////////// 422// Scissor 423/////////////////////////////////////////////////////////////////////////////// 424 425bool Caches::setScissor(GLint x, GLint y, GLint width, GLint height) { 426 if (scissorEnabled && (x != mScissorX || y != mScissorY || 427 width != mScissorWidth || height != mScissorHeight)) { 428 429 if (x < 0) { 430 width += x; 431 x = 0; 432 } 433 if (y < 0) { 434 height += y; 435 y = 0; 436 } 437 if (width < 0) { 438 width = 0; 439 } 440 if (height < 0) { 441 height = 0; 442 } 443 glScissor(x, y, width, height); 444 445 mScissorX = x; 446 mScissorY = y; 447 mScissorWidth = width; 448 mScissorHeight = height; 449 450 return true; 451 } 452 return false; 453} 454 455bool Caches::enableScissor() { 456 if (!scissorEnabled) { 457 glEnable(GL_SCISSOR_TEST); 458 scissorEnabled = true; 459 resetScissor(); 460 return true; 461 } 462 return false; 463} 464 465bool Caches::disableScissor() { 466 if (scissorEnabled) { 467 glDisable(GL_SCISSOR_TEST); 468 scissorEnabled = false; 469 return true; 470 } 471 return false; 472} 473 474void Caches::setScissorEnabled(bool enabled) { 475 if (scissorEnabled != enabled) { 476 if (enabled) glEnable(GL_SCISSOR_TEST); 477 else glDisable(GL_SCISSOR_TEST); 478 scissorEnabled = enabled; 479 } 480} 481 482void Caches::resetScissor() { 483 mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0; 484} 485 486/////////////////////////////////////////////////////////////////////////////// 487// Tiling 488/////////////////////////////////////////////////////////////////////////////// 489 490void Caches::startTiling(GLuint x, GLuint y, GLuint width, GLuint height, bool discard) { 491 if (mExtensions.hasTiledRendering() && !debugOverdraw) { 492 glStartTilingQCOM(x, y, width, height, (discard ? GL_NONE : GL_COLOR_BUFFER_BIT0_QCOM)); 493 } 494} 495 496void Caches::endTiling() { 497 if (mExtensions.hasTiledRendering() && !debugOverdraw) { 498 glEndTilingQCOM(GL_COLOR_BUFFER_BIT0_QCOM); 499 } 500} 501 502bool Caches::hasRegisteredFunctors() { 503 return mFunctorsCount > 0; 504} 505 506void Caches::registerFunctors(uint32_t functorCount) { 507 mFunctorsCount += functorCount; 508} 509 510void Caches::unregisterFunctors(uint32_t functorCount) { 511 if (functorCount > mFunctorsCount) { 512 mFunctorsCount = 0; 513 } else { 514 mFunctorsCount -= functorCount; 515 } 516} 517 518/////////////////////////////////////////////////////////////////////////////// 519// Regions 520/////////////////////////////////////////////////////////////////////////////// 521 522TextureVertex* Caches::getRegionMesh() { 523 // Create the mesh, 2 triangles and 4 vertices per rectangle in the region 524 if (!mRegionMesh) { 525 mRegionMesh = new TextureVertex[REGION_MESH_QUAD_COUNT * 4]; 526 527 uint16_t* regionIndices = new uint16_t[REGION_MESH_QUAD_COUNT * 6]; 528 for (int i = 0; i < REGION_MESH_QUAD_COUNT; i++) { 529 uint16_t quad = i * 4; 530 int index = i * 6; 531 regionIndices[index ] = quad; // top-left 532 regionIndices[index + 1] = quad + 1; // top-right 533 regionIndices[index + 2] = quad + 2; // bottom-left 534 regionIndices[index + 3] = quad + 2; // bottom-left 535 regionIndices[index + 4] = quad + 1; // top-right 536 regionIndices[index + 5] = quad + 3; // bottom-right 537 } 538 539 glGenBuffers(1, &mRegionMeshIndices); 540 bindIndicesBuffer(mRegionMeshIndices); 541 glBufferData(GL_ELEMENT_ARRAY_BUFFER, REGION_MESH_QUAD_COUNT * 6 * sizeof(uint16_t), 542 regionIndices, GL_STATIC_DRAW); 543 544 delete[] regionIndices; 545 } else { 546 bindIndicesBuffer(mRegionMeshIndices); 547 } 548 549 return mRegionMesh; 550} 551 552}; // namespace uirenderer 553}; // namespace android 554