Caches.cpp revision 211efea7376371ee755edd2ad03e83ef6eea464e
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 56 mDebugLevel = readDebugLevel(); 57 ALOGD("Enabling debug mode %d", mDebugLevel); 58 59#if RENDER_LAYERS_AS_REGIONS 60 INIT_LOGD("Layers will be composited as regions"); 61#endif 62} 63 64void Caches::init() { 65 if (mInitialized) return; 66 67 glGenBuffers(1, &meshBuffer); 68 glBindBuffer(GL_ARRAY_BUFFER, meshBuffer); 69 glBufferData(GL_ARRAY_BUFFER, sizeof(gMeshVertices), gMeshVertices, GL_STATIC_DRAW); 70 71 mCurrentBuffer = meshBuffer; 72 mCurrentIndicesBuffer = 0; 73 mCurrentPositionPointer = this; 74 mCurrentTexCoordsPointer = this; 75 76 mTexCoordsArrayEnabled = false; 77 78 glDisable(GL_SCISSOR_TEST); 79 scissorEnabled = false; 80 mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0; 81 82 glActiveTexture(gTextureUnits[0]); 83 mTextureUnit = 0; 84 85 mRegionMesh = NULL; 86 87 blend = false; 88 lastSrcMode = GL_ZERO; 89 lastDstMode = GL_ZERO; 90 currentProgram = NULL; 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 129void Caches::terminate() { 130 if (!mInitialized) return; 131 132 glDeleteBuffers(1, &meshBuffer); 133 mCurrentBuffer = 0; 134 135 glDeleteBuffers(1, &mRegionMeshIndices); 136 delete[] mRegionMesh; 137 mRegionMesh = NULL; 138 139 fboCache.clear(); 140 141 programCache.clear(); 142 currentProgram = NULL; 143 144 mInitialized = false; 145} 146 147/////////////////////////////////////////////////////////////////////////////// 148// Debug 149/////////////////////////////////////////////////////////////////////////////// 150 151void Caches::dumpMemoryUsage() { 152 String8 stringLog; 153 dumpMemoryUsage(stringLog); 154 ALOGD("%s", stringLog.string()); 155} 156 157void Caches::dumpMemoryUsage(String8 &log) { 158 log.appendFormat("Current memory usage / total memory usage (bytes):\n"); 159 log.appendFormat(" TextureCache %8d / %8d\n", 160 textureCache.getSize(), textureCache.getMaxSize()); 161 log.appendFormat(" LayerCache %8d / %8d\n", 162 layerCache.getSize(), layerCache.getMaxSize()); 163 log.appendFormat(" GradientCache %8d / %8d\n", 164 gradientCache.getSize(), gradientCache.getMaxSize()); 165 log.appendFormat(" PathCache %8d / %8d\n", 166 pathCache.getSize(), pathCache.getMaxSize()); 167 log.appendFormat(" CircleShapeCache %8d / %8d\n", 168 circleShapeCache.getSize(), circleShapeCache.getMaxSize()); 169 log.appendFormat(" OvalShapeCache %8d / %8d\n", 170 ovalShapeCache.getSize(), ovalShapeCache.getMaxSize()); 171 log.appendFormat(" RoundRectShapeCache %8d / %8d\n", 172 roundRectShapeCache.getSize(), roundRectShapeCache.getMaxSize()); 173 log.appendFormat(" RectShapeCache %8d / %8d\n", 174 rectShapeCache.getSize(), rectShapeCache.getMaxSize()); 175 log.appendFormat(" ArcShapeCache %8d / %8d\n", 176 arcShapeCache.getSize(), arcShapeCache.getMaxSize()); 177 log.appendFormat(" TextDropShadowCache %8d / %8d\n", dropShadowCache.getSize(), 178 dropShadowCache.getMaxSize()); 179 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) { 180 const uint32_t size = fontRenderer->getFontRendererSize(i); 181 log.appendFormat(" FontRenderer %d %8d / %8d\n", i, size, size); 182 } 183 log.appendFormat("Other:\n"); 184 log.appendFormat(" FboCache %8d / %8d\n", 185 fboCache.getSize(), fboCache.getMaxSize()); 186 log.appendFormat(" PatchCache %8d / %8d\n", 187 patchCache.getSize(), patchCache.getMaxSize()); 188 189 uint32_t total = 0; 190 total += textureCache.getSize(); 191 total += layerCache.getSize(); 192 total += gradientCache.getSize(); 193 total += pathCache.getSize(); 194 total += dropShadowCache.getSize(); 195 total += roundRectShapeCache.getSize(); 196 total += circleShapeCache.getSize(); 197 total += ovalShapeCache.getSize(); 198 total += rectShapeCache.getSize(); 199 total += arcShapeCache.getSize(); 200 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) { 201 total += fontRenderer->getFontRendererSize(i); 202 } 203 204 log.appendFormat("Total memory usage:\n"); 205 log.appendFormat(" %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f); 206} 207 208/////////////////////////////////////////////////////////////////////////////// 209// Memory management 210/////////////////////////////////////////////////////////////////////////////// 211 212void Caches::clearGarbage() { 213 textureCache.clearGarbage(); 214 pathCache.clearGarbage(); 215 216 Mutex::Autolock _l(mGarbageLock); 217 218 size_t count = mLayerGarbage.size(); 219 for (size_t i = 0; i < count; i++) { 220 Layer* layer = mLayerGarbage.itemAt(i); 221 LayerRenderer::destroyLayer(layer); 222 } 223 mLayerGarbage.clear(); 224 225 count = mDisplayListGarbage.size(); 226 for (size_t i = 0; i < count; i++) { 227 DisplayList* displayList = mDisplayListGarbage.itemAt(i); 228 delete displayList; 229 } 230 mDisplayListGarbage.clear(); 231} 232 233void Caches::deleteLayerDeferred(Layer* layer) { 234 Mutex::Autolock _l(mGarbageLock); 235 mLayerGarbage.push(layer); 236} 237 238void Caches::deleteDisplayListDeferred(DisplayList* displayList) { 239 Mutex::Autolock _l(mGarbageLock); 240 mDisplayListGarbage.push(displayList); 241} 242 243void Caches::flush(FlushMode mode) { 244 FLUSH_LOGD("Flushing caches (mode %d)", mode); 245 246 clearGarbage(); 247 248 switch (mode) { 249 case kFlushMode_Full: 250 textureCache.clear(); 251 patchCache.clear(); 252 dropShadowCache.clear(); 253 gradientCache.clear(); 254 fontRenderer->clear(); 255 dither.clear(); 256 // fall through 257 case kFlushMode_Moderate: 258 fontRenderer->flush(); 259 textureCache.flush(); 260 pathCache.clear(); 261 roundRectShapeCache.clear(); 262 circleShapeCache.clear(); 263 ovalShapeCache.clear(); 264 rectShapeCache.clear(); 265 arcShapeCache.clear(); 266 // fall through 267 case kFlushMode_Layers: 268 layerCache.clear(); 269 break; 270 } 271} 272 273/////////////////////////////////////////////////////////////////////////////// 274// VBO 275/////////////////////////////////////////////////////////////////////////////// 276 277bool Caches::bindMeshBuffer() { 278 return bindMeshBuffer(meshBuffer); 279} 280 281bool Caches::bindMeshBuffer(const GLuint buffer) { 282 if (mCurrentBuffer != buffer) { 283 glBindBuffer(GL_ARRAY_BUFFER, buffer); 284 mCurrentBuffer = buffer; 285 return true; 286 } 287 return false; 288} 289 290bool Caches::unbindMeshBuffer() { 291 if (mCurrentBuffer) { 292 glBindBuffer(GL_ARRAY_BUFFER, 0); 293 mCurrentBuffer = 0; 294 return true; 295 } 296 return false; 297} 298 299bool Caches::bindIndicesBuffer(const GLuint buffer) { 300 if (mCurrentIndicesBuffer != buffer) { 301 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer); 302 mCurrentIndicesBuffer = buffer; 303 return true; 304 } 305 return false; 306} 307 308bool Caches::unbindIndicesBuffer() { 309 if (mCurrentIndicesBuffer) { 310 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); 311 mCurrentIndicesBuffer = 0; 312 return true; 313 } 314 return false; 315} 316 317void Caches::bindPositionVertexPointer(bool force, GLuint slot, GLvoid* vertices, GLsizei stride) { 318 if (force || vertices != mCurrentPositionPointer) { 319 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices); 320 mCurrentPositionPointer = vertices; 321 } 322} 323 324void Caches::bindTexCoordsVertexPointer(bool force, GLuint slot, GLvoid* vertices) { 325 if (force || vertices != mCurrentTexCoordsPointer) { 326 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, gMeshStride, vertices); 327 mCurrentTexCoordsPointer = vertices; 328 } 329} 330 331void Caches::resetVertexPointers() { 332 mCurrentPositionPointer = this; 333 mCurrentTexCoordsPointer = this; 334} 335 336void Caches::resetTexCoordsVertexPointer() { 337 mCurrentTexCoordsPointer = this; 338} 339 340void Caches::enableTexCoordsVertexArray() { 341 if (!mTexCoordsArrayEnabled) { 342 glEnableVertexAttribArray(Program::kBindingTexCoords); 343 mCurrentTexCoordsPointer = this; 344 mTexCoordsArrayEnabled = true; 345 } 346} 347 348void Caches::disbaleTexCoordsVertexArray() { 349 if (mTexCoordsArrayEnabled) { 350 glDisableVertexAttribArray(Program::kBindingTexCoords); 351 mTexCoordsArrayEnabled = false; 352 } 353} 354 355void Caches::activeTexture(GLuint textureUnit) { 356 if (mTextureUnit != textureUnit) { 357 glActiveTexture(gTextureUnits[textureUnit]); 358 mTextureUnit = textureUnit; 359 } 360} 361 362bool Caches::setScissor(GLint x, GLint y, GLint width, GLint height) { 363 if (scissorEnabled && (x != mScissorX || y != mScissorY || 364 width != mScissorWidth || height != mScissorHeight)) { 365 366 glScissor(x, y, width, height); 367 368 mScissorX = x; 369 mScissorY = y; 370 mScissorWidth = width; 371 mScissorHeight = height; 372 373 return true; 374 } 375 return false; 376} 377 378bool Caches::enableScissor() { 379 if (!scissorEnabled) { 380 glEnable(GL_SCISSOR_TEST); 381 scissorEnabled = true; 382 return true; 383 } 384 return false; 385} 386 387bool Caches::disableScissor() { 388 if (scissorEnabled) { 389 glDisable(GL_SCISSOR_TEST); 390 scissorEnabled = false; 391 return true; 392 } 393 return false; 394} 395 396void Caches::setScissorEnabled(bool enabled) { 397 if (scissorEnabled != enabled) { 398 if (enabled) glEnable(GL_SCISSOR_TEST); 399 else glDisable(GL_SCISSOR_TEST); 400 scissorEnabled = enabled; 401 } 402} 403 404void Caches::resetScissor() { 405 mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0; 406} 407 408TextureVertex* Caches::getRegionMesh() { 409 // Create the mesh, 2 triangles and 4 vertices per rectangle in the region 410 if (!mRegionMesh) { 411 mRegionMesh = new TextureVertex[REGION_MESH_QUAD_COUNT * 4]; 412 413 uint16_t* regionIndices = new uint16_t[REGION_MESH_QUAD_COUNT * 6]; 414 for (int i = 0; i < REGION_MESH_QUAD_COUNT; i++) { 415 uint16_t quad = i * 4; 416 int index = i * 6; 417 regionIndices[index ] = quad; // top-left 418 regionIndices[index + 1] = quad + 1; // top-right 419 regionIndices[index + 2] = quad + 2; // bottom-left 420 regionIndices[index + 3] = quad + 2; // bottom-left 421 regionIndices[index + 4] = quad + 1; // top-right 422 regionIndices[index + 5] = quad + 3; // bottom-right 423 } 424 425 glGenBuffers(1, &mRegionMeshIndices); 426 bindIndicesBuffer(mRegionMeshIndices); 427 glBufferData(GL_ELEMENT_ARRAY_BUFFER, REGION_MESH_QUAD_COUNT * 6 * sizeof(uint16_t), 428 regionIndices, GL_STATIC_DRAW); 429 430 delete[] regionIndices; 431 } else { 432 bindIndicesBuffer(mRegionMeshIndices); 433 } 434 435 return mRegionMesh; 436} 437 438}; // namespace uirenderer 439}; // namespace android 440