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