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