Caches.cpp revision 82bc7a772747fcf8a6fe7097f70bf2981429ffe9
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 "Properties.h" 24#include "LayerRenderer.h" 25 26namespace android { 27 28#ifdef USE_OPENGL_RENDERER 29using namespace uirenderer; 30ANDROID_SINGLETON_STATIC_INSTANCE(Caches); 31#endif 32 33namespace uirenderer { 34 35/////////////////////////////////////////////////////////////////////////////// 36// Macros 37/////////////////////////////////////////////////////////////////////////////// 38 39#if DEBUG_CACHE_FLUSH 40 #define FLUSH_LOGD(...) LOGD(__VA_ARGS__) 41#else 42 #define FLUSH_LOGD(...) 43#endif 44 45/////////////////////////////////////////////////////////////////////////////// 46// Constructors/destructor 47/////////////////////////////////////////////////////////////////////////////// 48 49Caches::Caches(): Singleton<Caches>(), mInitialized(false) { 50 GLint maxTextureUnits; 51 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits); 52 if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) { 53 LOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT); 54 } 55 56 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize); 57 58 init(); 59 60 mDebugLevel = readDebugLevel(); 61 LOGD("Enabling debug mode %d", mDebugLevel); 62 63#if RENDER_LAYERS_AS_REGIONS 64 INIT_LOGD("Layers will be composited as regions"); 65#endif 66} 67 68void Caches::init() { 69 if (mInitialized) return; 70 71 glGenBuffers(1, &meshBuffer); 72 glBindBuffer(GL_ARRAY_BUFFER, meshBuffer); 73 glBufferData(GL_ARRAY_BUFFER, sizeof(gMeshVertices), gMeshVertices, GL_STATIC_DRAW); 74 75 mCurrentBuffer = meshBuffer; 76 mCurrentIndicesBuffer = 0; 77 mCurrentPositionPointer = this; 78 mCurrentTexCoordsPointer = this; 79 80 mTexCoordsArrayEnabled = false; 81 82 mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0; 83 84 glActiveTexture(gTextureUnits[0]); 85 mTextureUnit = 0; 86 87 mRegionMesh = NULL; 88 89 blend = false; 90 lastSrcMode = GL_ZERO; 91 lastDstMode = GL_ZERO; 92 currentProgram = NULL; 93 94 mInitialized = true; 95} 96 97void Caches::terminate() { 98 if (!mInitialized) return; 99 100 glDeleteBuffers(1, &meshBuffer); 101 mCurrentBuffer = 0; 102 103 glDeleteBuffers(1, &mRegionMeshIndices); 104 delete[] mRegionMesh; 105 mRegionMesh = NULL; 106 107 fboCache.clear(); 108 109 programCache.clear(); 110 currentProgram = NULL; 111 112 mInitialized = false; 113} 114 115/////////////////////////////////////////////////////////////////////////////// 116// Debug 117/////////////////////////////////////////////////////////////////////////////// 118 119void Caches::dumpMemoryUsage() { 120 String8 stringLog; 121 dumpMemoryUsage(stringLog); 122 LOGD("%s", stringLog.string()); 123} 124 125void Caches::dumpMemoryUsage(String8 &log) { 126 log.appendFormat("Current memory usage / total memory usage (bytes):\n"); 127 log.appendFormat(" TextureCache %8d / %8d\n", 128 textureCache.getSize(), textureCache.getMaxSize()); 129 log.appendFormat(" LayerCache %8d / %8d\n", 130 layerCache.getSize(), layerCache.getMaxSize()); 131 log.appendFormat(" GradientCache %8d / %8d\n", 132 gradientCache.getSize(), gradientCache.getMaxSize()); 133 log.appendFormat(" PathCache %8d / %8d\n", 134 pathCache.getSize(), pathCache.getMaxSize()); 135 log.appendFormat(" CircleShapeCache %8d / %8d\n", 136 circleShapeCache.getSize(), circleShapeCache.getMaxSize()); 137 log.appendFormat(" OvalShapeCache %8d / %8d\n", 138 ovalShapeCache.getSize(), ovalShapeCache.getMaxSize()); 139 log.appendFormat(" RoundRectShapeCache %8d / %8d\n", 140 roundRectShapeCache.getSize(), roundRectShapeCache.getMaxSize()); 141 log.appendFormat(" RectShapeCache %8d / %8d\n", 142 rectShapeCache.getSize(), rectShapeCache.getMaxSize()); 143 log.appendFormat(" ArcShapeCache %8d / %8d\n", 144 arcShapeCache.getSize(), arcShapeCache.getMaxSize()); 145 log.appendFormat(" TextDropShadowCache %8d / %8d\n", dropShadowCache.getSize(), 146 dropShadowCache.getMaxSize()); 147 for (uint32_t i = 0; i < fontRenderer.getFontRendererCount(); i++) { 148 const uint32_t size = fontRenderer.getFontRendererSize(i); 149 log.appendFormat(" FontRenderer %d %8d / %8d\n", i, size, size); 150 } 151 log.appendFormat("Other:\n"); 152 log.appendFormat(" FboCache %8d / %8d\n", 153 fboCache.getSize(), fboCache.getMaxSize()); 154 log.appendFormat(" PatchCache %8d / %8d\n", 155 patchCache.getSize(), patchCache.getMaxSize()); 156 157 uint32_t total = 0; 158 total += textureCache.getSize(); 159 total += layerCache.getSize(); 160 total += gradientCache.getSize(); 161 total += pathCache.getSize(); 162 total += dropShadowCache.getSize(); 163 total += roundRectShapeCache.getSize(); 164 total += circleShapeCache.getSize(); 165 total += ovalShapeCache.getSize(); 166 total += rectShapeCache.getSize(); 167 total += arcShapeCache.getSize(); 168 for (uint32_t i = 0; i < fontRenderer.getFontRendererCount(); i++) { 169 total += fontRenderer.getFontRendererSize(i); 170 } 171 172 log.appendFormat("Total memory usage:\n"); 173 log.appendFormat(" %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f); 174} 175 176/////////////////////////////////////////////////////////////////////////////// 177// Memory management 178/////////////////////////////////////////////////////////////////////////////// 179 180void Caches::clearGarbage() { 181 textureCache.clearGarbage(); 182 pathCache.clearGarbage(); 183 184 Mutex::Autolock _l(mGarbageLock); 185 186 size_t count = mLayerGarbage.size(); 187 for (size_t i = 0; i < count; i++) { 188 Layer* layer = mLayerGarbage.itemAt(i); 189 LayerRenderer::destroyLayer(layer); 190 } 191 mLayerGarbage.clear(); 192} 193 194void Caches::deleteLayerDeferred(Layer* layer) { 195 Mutex::Autolock _l(mGarbageLock); 196 mLayerGarbage.push(layer); 197} 198 199void Caches::flush(FlushMode mode) { 200 FLUSH_LOGD("Flushing caches (mode %d)", mode); 201 202 clearGarbage(); 203 204 switch (mode) { 205 case kFlushMode_Full: 206 textureCache.clear(); 207 patchCache.clear(); 208 dropShadowCache.clear(); 209 gradientCache.clear(); 210 fontRenderer.clear(); 211 // fall through 212 case kFlushMode_Moderate: 213 fontRenderer.flush(); 214 textureCache.flush(); 215 pathCache.clear(); 216 roundRectShapeCache.clear(); 217 circleShapeCache.clear(); 218 ovalShapeCache.clear(); 219 rectShapeCache.clear(); 220 arcShapeCache.clear(); 221 // fall through 222 case kFlushMode_Layers: 223 layerCache.clear(); 224 break; 225 } 226} 227 228/////////////////////////////////////////////////////////////////////////////// 229// VBO 230/////////////////////////////////////////////////////////////////////////////// 231 232bool Caches::bindMeshBuffer() { 233 return bindMeshBuffer(meshBuffer); 234} 235 236bool Caches::bindMeshBuffer(const GLuint buffer) { 237 if (mCurrentBuffer != buffer) { 238 glBindBuffer(GL_ARRAY_BUFFER, buffer); 239 mCurrentBuffer = buffer; 240 return true; 241 } 242 return false; 243} 244 245bool Caches::unbindMeshBuffer() { 246 if (mCurrentBuffer) { 247 glBindBuffer(GL_ARRAY_BUFFER, 0); 248 mCurrentBuffer = 0; 249 return true; 250 } 251 return false; 252} 253 254bool Caches::bindIndicesBuffer(const GLuint buffer) { 255 if (mCurrentIndicesBuffer != buffer) { 256 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer); 257 mCurrentIndicesBuffer = buffer; 258 return true; 259 } 260 return false; 261} 262 263bool Caches::unbindIndicesBuffer() { 264 if (mCurrentIndicesBuffer) { 265 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); 266 mCurrentIndicesBuffer = 0; 267 return true; 268 } 269 return false; 270} 271 272void Caches::bindPositionVertexPointer(bool force, GLuint slot, GLvoid* vertices, GLsizei stride) { 273 if (force || vertices != mCurrentPositionPointer) { 274 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices); 275 mCurrentPositionPointer = vertices; 276 } 277} 278 279void Caches::bindTexCoordsVertexPointer(bool force, GLuint slot, GLvoid* vertices) { 280 if (force || vertices != mCurrentTexCoordsPointer) { 281 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, gMeshStride, vertices); 282 mCurrentTexCoordsPointer = vertices; 283 } 284} 285 286void Caches::resetVertexPointers() { 287 mCurrentPositionPointer = this; 288 mCurrentTexCoordsPointer = this; 289} 290 291void Caches::resetTexCoordsVertexPointer() { 292 mCurrentTexCoordsPointer = this; 293} 294 295void Caches::enableTexCoordsVertexArray() { 296 if (!mTexCoordsArrayEnabled) { 297 glEnableVertexAttribArray(Program::kBindingTexCoords); 298 mCurrentTexCoordsPointer = this; 299 mTexCoordsArrayEnabled = true; 300 } 301} 302 303void Caches::disbaleTexCoordsVertexArray() { 304 if (mTexCoordsArrayEnabled) { 305 glDisableVertexAttribArray(Program::kBindingTexCoords); 306 mTexCoordsArrayEnabled = false; 307 } 308} 309 310void Caches::activeTexture(GLuint textureUnit) { 311 if (mTextureUnit != textureUnit) { 312 glActiveTexture(gTextureUnits[textureUnit]); 313 mTextureUnit = textureUnit; 314 } 315} 316 317void Caches::setScissor(GLint x, GLint y, GLint width, GLint height) { 318 if (x != mScissorX || y != mScissorY || width != mScissorWidth || height != mScissorHeight) { 319 glScissor(x, y, width, height); 320 321 mScissorX = x; 322 mScissorY = y; 323 mScissorWidth = width; 324 mScissorHeight = height; 325 } 326} 327 328void Caches::resetScissor() { 329 mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0; 330} 331 332TextureVertex* Caches::getRegionMesh() { 333 // Create the mesh, 2 triangles and 4 vertices per rectangle in the region 334 if (!mRegionMesh) { 335 mRegionMesh = new TextureVertex[REGION_MESH_QUAD_COUNT * 4]; 336 337 uint16_t* regionIndices = new uint16_t[REGION_MESH_QUAD_COUNT * 6]; 338 for (int i = 0; i < REGION_MESH_QUAD_COUNT; i++) { 339 uint16_t quad = i * 4; 340 int index = i * 6; 341 regionIndices[index ] = quad; // top-left 342 regionIndices[index + 1] = quad + 1; // top-right 343 regionIndices[index + 2] = quad + 2; // bottom-left 344 regionIndices[index + 3] = quad + 2; // bottom-left 345 regionIndices[index + 4] = quad + 1; // top-right 346 regionIndices[index + 5] = quad + 3; // bottom-right 347 } 348 349 glGenBuffers(1, &mRegionMeshIndices); 350 bindIndicesBuffer(mRegionMeshIndices); 351 glBufferData(GL_ELEMENT_ARRAY_BUFFER, REGION_MESH_QUAD_COUNT * 6 * sizeof(uint16_t), 352 regionIndices, GL_STATIC_DRAW); 353 354 delete[] regionIndices; 355 } else { 356 bindIndicesBuffer(mRegionMeshIndices); 357 } 358 359 return mRegionMesh; 360} 361 362}; // namespace uirenderer 363}; // namespace android 364