Caches.cpp revision 3ff0bfdd144bba3b023eda8c49b25fb0d0de8653
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 initExtensions(); 54 initConstraints(); 55 initProperties(); 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 startMark = glPushGroupMarkerEXT; 107 endMark = glPopGroupMarkerEXT; 108 } else { 109 eventMark = eventMarkNull; 110 startMark = startMarkNull; 111 endMark = endMarkNull; 112 } 113 114 if (mExtensions.hasDebugLabel()) { 115 setLabel = glLabelObjectEXT; 116 getLabel = glGetObjectLabelEXT; 117 } else { 118 setLabel = setLabelNull; 119 getLabel = getLabelNull; 120 } 121} 122 123void Caches::initConstraints() { 124 GLint maxTextureUnits; 125 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits); 126 if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) { 127 ALOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT); 128 } 129 130 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize); 131} 132 133bool Caches::initProperties() { 134 bool prevDebugLayersUpdates = debugLayersUpdates; 135 bool prevDebugOverdraw = debugOverdraw; 136 StencilClipDebug prevDebugStencilClip = debugStencilClip; 137 138 char property[PROPERTY_VALUE_MAX]; 139 if (property_get(PROPERTY_DEBUG_LAYERS_UPDATES, property, NULL) > 0) { 140 INIT_LOGD(" Layers updates debug enabled: %s", property); 141 debugLayersUpdates = !strcmp(property, "true"); 142 } else { 143 debugLayersUpdates = false; 144 } 145 146 if (property_get(PROPERTY_DEBUG_OVERDRAW, property, NULL) > 0) { 147 INIT_LOGD(" Overdraw debug enabled: %s", property); 148 debugOverdraw = !strcmp(property, "true"); 149 } else { 150 debugOverdraw = false; 151 } 152 153 // See Properties.h for valid values 154 if (property_get(PROPERTY_DEBUG_STENCIL_CLIP, property, NULL) > 0) { 155 INIT_LOGD(" Stencil clip debug enabled: %s", property); 156 if (!strcmp(property, "hide")) { 157 debugStencilClip = kStencilHide; 158 } else if (!strcmp(property, "highlight")) { 159 debugStencilClip = kStencilShowHighlight; 160 } else if (!strcmp(property, "region")) { 161 debugStencilClip = kStencilShowRegion; 162 } 163 } else { 164 debugStencilClip = kStencilHide; 165 } 166 167 return (prevDebugLayersUpdates != debugLayersUpdates) || 168 (prevDebugOverdraw != debugOverdraw) || 169 (prevDebugStencilClip != debugStencilClip); 170} 171 172void Caches::terminate() { 173 if (!mInitialized) return; 174 175 glDeleteBuffers(1, &meshBuffer); 176 mCurrentBuffer = 0; 177 178 glDeleteBuffers(1, &mRegionMeshIndices); 179 delete[] mRegionMesh; 180 mRegionMesh = NULL; 181 182 fboCache.clear(); 183 184 programCache.clear(); 185 currentProgram = NULL; 186 187 mInitialized = false; 188} 189 190/////////////////////////////////////////////////////////////////////////////// 191// Debug 192/////////////////////////////////////////////////////////////////////////////// 193 194void Caches::dumpMemoryUsage() { 195 String8 stringLog; 196 dumpMemoryUsage(stringLog); 197 ALOGD("%s", stringLog.string()); 198} 199 200void Caches::dumpMemoryUsage(String8 &log) { 201 log.appendFormat("Current memory usage / total memory usage (bytes):\n"); 202 log.appendFormat(" TextureCache %8d / %8d\n", 203 textureCache.getSize(), textureCache.getMaxSize()); 204 log.appendFormat(" LayerCache %8d / %8d\n", 205 layerCache.getSize(), layerCache.getMaxSize()); 206 log.appendFormat(" RenderBufferCache %8d / %8d\n", 207 renderBufferCache.getSize(), renderBufferCache.getMaxSize()); 208 log.appendFormat(" GradientCache %8d / %8d\n", 209 gradientCache.getSize(), gradientCache.getMaxSize()); 210 log.appendFormat(" PathCache %8d / %8d\n", 211 pathCache.getSize(), pathCache.getMaxSize()); 212 log.appendFormat(" CircleShapeCache %8d / %8d\n", 213 circleShapeCache.getSize(), circleShapeCache.getMaxSize()); 214 log.appendFormat(" OvalShapeCache %8d / %8d\n", 215 ovalShapeCache.getSize(), ovalShapeCache.getMaxSize()); 216 log.appendFormat(" RoundRectShapeCache %8d / %8d\n", 217 roundRectShapeCache.getSize(), roundRectShapeCache.getMaxSize()); 218 log.appendFormat(" RectShapeCache %8d / %8d\n", 219 rectShapeCache.getSize(), rectShapeCache.getMaxSize()); 220 log.appendFormat(" ArcShapeCache %8d / %8d\n", 221 arcShapeCache.getSize(), arcShapeCache.getMaxSize()); 222 log.appendFormat(" TextDropShadowCache %8d / %8d\n", dropShadowCache.getSize(), 223 dropShadowCache.getMaxSize()); 224 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) { 225 const uint32_t size = fontRenderer->getFontRendererSize(i); 226 log.appendFormat(" FontRenderer %d %8d / %8d\n", i, size, size); 227 } 228 log.appendFormat("Other:\n"); 229 log.appendFormat(" FboCache %8d / %8d\n", 230 fboCache.getSize(), fboCache.getMaxSize()); 231 log.appendFormat(" PatchCache %8d / %8d\n", 232 patchCache.getSize(), patchCache.getMaxSize()); 233 234 uint32_t total = 0; 235 total += textureCache.getSize(); 236 total += layerCache.getSize(); 237 total += renderBufferCache.getSize(); 238 total += gradientCache.getSize(); 239 total += pathCache.getSize(); 240 total += dropShadowCache.getSize(); 241 total += roundRectShapeCache.getSize(); 242 total += circleShapeCache.getSize(); 243 total += ovalShapeCache.getSize(); 244 total += rectShapeCache.getSize(); 245 total += arcShapeCache.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 roundRectShapeCache.clear(); 314 circleShapeCache.clear(); 315 ovalShapeCache.clear(); 316 rectShapeCache.clear(); 317 arcShapeCache.clear(); 318 // fall through 319 case kFlushMode_Layers: 320 layerCache.clear(); 321 renderBufferCache.clear(); 322 break; 323 } 324 325 clearGarbage(); 326} 327 328/////////////////////////////////////////////////////////////////////////////// 329// VBO 330/////////////////////////////////////////////////////////////////////////////// 331 332bool Caches::bindMeshBuffer() { 333 return bindMeshBuffer(meshBuffer); 334} 335 336bool Caches::bindMeshBuffer(const GLuint buffer) { 337 if (mCurrentBuffer != buffer) { 338 glBindBuffer(GL_ARRAY_BUFFER, buffer); 339 mCurrentBuffer = buffer; 340 return true; 341 } 342 return false; 343} 344 345bool Caches::unbindMeshBuffer() { 346 if (mCurrentBuffer) { 347 glBindBuffer(GL_ARRAY_BUFFER, 0); 348 mCurrentBuffer = 0; 349 return true; 350 } 351 return false; 352} 353 354bool Caches::bindIndicesBuffer(const GLuint buffer) { 355 if (mCurrentIndicesBuffer != buffer) { 356 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer); 357 mCurrentIndicesBuffer = buffer; 358 return true; 359 } 360 return false; 361} 362 363bool Caches::unbindIndicesBuffer() { 364 if (mCurrentIndicesBuffer) { 365 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); 366 mCurrentIndicesBuffer = 0; 367 return true; 368 } 369 return false; 370} 371 372/////////////////////////////////////////////////////////////////////////////// 373// Meshes and textures 374/////////////////////////////////////////////////////////////////////////////// 375 376void Caches::bindPositionVertexPointer(bool force, GLvoid* vertices, GLsizei stride) { 377 if (force || vertices != mCurrentPositionPointer || stride != mCurrentPositionStride) { 378 GLuint slot = currentProgram->position; 379 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices); 380 mCurrentPositionPointer = vertices; 381 mCurrentPositionStride = stride; 382 } 383} 384 385void Caches::bindTexCoordsVertexPointer(bool force, GLvoid* vertices, GLsizei stride) { 386 if (force || vertices != mCurrentTexCoordsPointer || stride != mCurrentTexCoordsStride) { 387 GLuint slot = currentProgram->texCoords; 388 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices); 389 mCurrentTexCoordsPointer = vertices; 390 mCurrentTexCoordsStride = stride; 391 } 392} 393 394void Caches::resetVertexPointers() { 395 mCurrentPositionPointer = this; 396 mCurrentTexCoordsPointer = this; 397} 398 399void Caches::resetTexCoordsVertexPointer() { 400 mCurrentTexCoordsPointer = this; 401} 402 403void Caches::enableTexCoordsVertexArray() { 404 if (!mTexCoordsArrayEnabled) { 405 glEnableVertexAttribArray(Program::kBindingTexCoords); 406 mCurrentTexCoordsPointer = this; 407 mTexCoordsArrayEnabled = true; 408 } 409} 410 411void Caches::disableTexCoordsVertexArray() { 412 if (mTexCoordsArrayEnabled) { 413 glDisableVertexAttribArray(Program::kBindingTexCoords); 414 mTexCoordsArrayEnabled = false; 415 } 416} 417 418void Caches::activeTexture(GLuint textureUnit) { 419 if (mTextureUnit != textureUnit) { 420 glActiveTexture(gTextureUnits[textureUnit]); 421 mTextureUnit = textureUnit; 422 } 423} 424 425/////////////////////////////////////////////////////////////////////////////// 426// Scissor 427/////////////////////////////////////////////////////////////////////////////// 428 429bool Caches::setScissor(GLint x, GLint y, GLint width, GLint height) { 430 if (scissorEnabled && (x != mScissorX || y != mScissorY || 431 width != mScissorWidth || height != mScissorHeight)) { 432 433 if (x < 0) { 434 width += x; 435 x = 0; 436 } 437 if (y < 0) { 438 height += y; 439 y = 0; 440 } 441 if (width < 0) { 442 width = 0; 443 } 444 if (height < 0) { 445 height = 0; 446 } 447 glScissor(x, y, width, height); 448 449 mScissorX = x; 450 mScissorY = y; 451 mScissorWidth = width; 452 mScissorHeight = height; 453 454 return true; 455 } 456 return false; 457} 458 459bool Caches::enableScissor() { 460 if (!scissorEnabled) { 461 glEnable(GL_SCISSOR_TEST); 462 scissorEnabled = true; 463 resetScissor(); 464 return true; 465 } 466 return false; 467} 468 469bool Caches::disableScissor() { 470 if (scissorEnabled) { 471 glDisable(GL_SCISSOR_TEST); 472 scissorEnabled = false; 473 return true; 474 } 475 return false; 476} 477 478void Caches::setScissorEnabled(bool enabled) { 479 if (scissorEnabled != enabled) { 480 if (enabled) glEnable(GL_SCISSOR_TEST); 481 else glDisable(GL_SCISSOR_TEST); 482 scissorEnabled = enabled; 483 } 484} 485 486void Caches::resetScissor() { 487 mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0; 488} 489 490/////////////////////////////////////////////////////////////////////////////// 491// Tiling 492/////////////////////////////////////////////////////////////////////////////// 493 494void Caches::startTiling(GLuint x, GLuint y, GLuint width, GLuint height, bool discard) { 495 if (mExtensions.hasTiledRendering() && !debugOverdraw) { 496 glStartTilingQCOM(x, y, width, height, (discard ? GL_NONE : GL_COLOR_BUFFER_BIT0_QCOM)); 497 } 498} 499 500void Caches::endTiling() { 501 if (mExtensions.hasTiledRendering() && !debugOverdraw) { 502 glEndTilingQCOM(GL_COLOR_BUFFER_BIT0_QCOM); 503 } 504} 505 506bool Caches::hasRegisteredFunctors() { 507 return mFunctorsCount > 0; 508} 509 510void Caches::registerFunctors(uint32_t functorCount) { 511 mFunctorsCount += functorCount; 512} 513 514void Caches::unregisterFunctors(uint32_t functorCount) { 515 if (functorCount > mFunctorsCount) { 516 mFunctorsCount = 0; 517 } else { 518 mFunctorsCount -= functorCount; 519 } 520} 521 522/////////////////////////////////////////////////////////////////////////////// 523// Regions 524/////////////////////////////////////////////////////////////////////////////// 525 526TextureVertex* Caches::getRegionMesh() { 527 // Create the mesh, 2 triangles and 4 vertices per rectangle in the region 528 if (!mRegionMesh) { 529 mRegionMesh = new TextureVertex[REGION_MESH_QUAD_COUNT * 4]; 530 531 uint16_t* regionIndices = new uint16_t[REGION_MESH_QUAD_COUNT * 6]; 532 for (int i = 0; i < REGION_MESH_QUAD_COUNT; i++) { 533 uint16_t quad = i * 4; 534 int index = i * 6; 535 regionIndices[index ] = quad; // top-left 536 regionIndices[index + 1] = quad + 1; // top-right 537 regionIndices[index + 2] = quad + 2; // bottom-left 538 regionIndices[index + 3] = quad + 2; // bottom-left 539 regionIndices[index + 4] = quad + 1; // top-right 540 regionIndices[index + 5] = quad + 3; // bottom-right 541 } 542 543 glGenBuffers(1, &mRegionMeshIndices); 544 bindIndicesBuffer(mRegionMeshIndices); 545 glBufferData(GL_ELEMENT_ARRAY_BUFFER, REGION_MESH_QUAD_COUNT * 6 * sizeof(uint16_t), 546 regionIndices, GL_STATIC_DRAW); 547 548 delete[] regionIndices; 549 } else { 550 bindIndicesBuffer(mRegionMeshIndices); 551 } 552 553 return mRegionMesh; 554} 555 556}; // namespace uirenderer 557}; // namespace android 558