Caches.cpp revision f3a910b423db7ad79cf61518bdd9278c048ad0d8
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 mCurrentPositionPointer = this; 77 mCurrentTexCoordsPointer = this; 78 79 mRegionMesh = NULL; 80 81 blend = false; 82 lastSrcMode = GL_ZERO; 83 lastDstMode = GL_ZERO; 84 currentProgram = NULL; 85 86 mInitialized = true; 87} 88 89void Caches::terminate() { 90 if (!mInitialized) return; 91 92 glDeleteBuffers(1, &meshBuffer); 93 mCurrentBuffer = 0; 94 95 glDeleteBuffers(1, &mRegionMeshIndices); 96 delete[] mRegionMesh; 97 mRegionMesh = NULL; 98 99 fboCache.clear(); 100 101 programCache.clear(); 102 currentProgram = NULL; 103 104 mInitialized = false; 105} 106 107/////////////////////////////////////////////////////////////////////////////// 108// Debug 109/////////////////////////////////////////////////////////////////////////////// 110 111void Caches::dumpMemoryUsage() { 112 String8 stringLog; 113 dumpMemoryUsage(stringLog); 114 LOGD("%s", stringLog.string()); 115} 116 117void Caches::dumpMemoryUsage(String8 &log) { 118 log.appendFormat("Current memory usage / total memory usage (bytes):\n"); 119 log.appendFormat(" TextureCache %8d / %8d\n", 120 textureCache.getSize(), textureCache.getMaxSize()); 121 log.appendFormat(" LayerCache %8d / %8d\n", 122 layerCache.getSize(), layerCache.getMaxSize()); 123 log.appendFormat(" GradientCache %8d / %8d\n", 124 gradientCache.getSize(), gradientCache.getMaxSize()); 125 log.appendFormat(" PathCache %8d / %8d\n", 126 pathCache.getSize(), pathCache.getMaxSize()); 127 log.appendFormat(" CircleShapeCache %8d / %8d\n", 128 circleShapeCache.getSize(), circleShapeCache.getMaxSize()); 129 log.appendFormat(" OvalShapeCache %8d / %8d\n", 130 ovalShapeCache.getSize(), ovalShapeCache.getMaxSize()); 131 log.appendFormat(" RoundRectShapeCache %8d / %8d\n", 132 roundRectShapeCache.getSize(), roundRectShapeCache.getMaxSize()); 133 log.appendFormat(" RectShapeCache %8d / %8d\n", 134 rectShapeCache.getSize(), rectShapeCache.getMaxSize()); 135 log.appendFormat(" ArcShapeCache %8d / %8d\n", 136 arcShapeCache.getSize(), arcShapeCache.getMaxSize()); 137 log.appendFormat(" TextDropShadowCache %8d / %8d\n", dropShadowCache.getSize(), 138 dropShadowCache.getMaxSize()); 139 for (uint32_t i = 0; i < fontRenderer.getFontRendererCount(); i++) { 140 const uint32_t size = fontRenderer.getFontRendererSize(i); 141 log.appendFormat(" FontRenderer %d %8d / %8d\n", i, size, size); 142 } 143 log.appendFormat("Other:\n"); 144 log.appendFormat(" FboCache %8d / %8d\n", 145 fboCache.getSize(), fboCache.getMaxSize()); 146 log.appendFormat(" PatchCache %8d / %8d\n", 147 patchCache.getSize(), patchCache.getMaxSize()); 148 149 uint32_t total = 0; 150 total += textureCache.getSize(); 151 total += layerCache.getSize(); 152 total += gradientCache.getSize(); 153 total += pathCache.getSize(); 154 total += dropShadowCache.getSize(); 155 total += roundRectShapeCache.getSize(); 156 total += circleShapeCache.getSize(); 157 total += ovalShapeCache.getSize(); 158 total += rectShapeCache.getSize(); 159 total += arcShapeCache.getSize(); 160 for (uint32_t i = 0; i < fontRenderer.getFontRendererCount(); i++) { 161 total += fontRenderer.getFontRendererSize(i); 162 } 163 164 log.appendFormat("Total memory usage:\n"); 165 log.appendFormat(" %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f); 166} 167 168/////////////////////////////////////////////////////////////////////////////// 169// Memory management 170/////////////////////////////////////////////////////////////////////////////// 171 172void Caches::clearGarbage() { 173 textureCache.clearGarbage(); 174 pathCache.clearGarbage(); 175 176 Mutex::Autolock _l(mGarbageLock); 177 178 size_t count = mLayerGarbage.size(); 179 for (size_t i = 0; i < count; i++) { 180 Layer* layer = mLayerGarbage.itemAt(i); 181 LayerRenderer::destroyLayer(layer); 182 } 183 mLayerGarbage.clear(); 184} 185 186void Caches::deleteLayerDeferred(Layer* layer) { 187 Mutex::Autolock _l(mGarbageLock); 188 mLayerGarbage.push(layer); 189} 190 191void Caches::flush(FlushMode mode) { 192 FLUSH_LOGD("Flushing caches (mode %d)", mode); 193 194 clearGarbage(); 195 196 switch (mode) { 197 case kFlushMode_Full: 198 textureCache.clear(); 199 patchCache.clear(); 200 dropShadowCache.clear(); 201 gradientCache.clear(); 202 fontRenderer.clear(); 203 // fall through 204 case kFlushMode_Moderate: 205 fontRenderer.flush(); 206 textureCache.flush(); 207 pathCache.clear(); 208 roundRectShapeCache.clear(); 209 circleShapeCache.clear(); 210 ovalShapeCache.clear(); 211 rectShapeCache.clear(); 212 arcShapeCache.clear(); 213 // fall through 214 case kFlushMode_Layers: 215 layerCache.clear(); 216 break; 217 } 218} 219 220/////////////////////////////////////////////////////////////////////////////// 221// VBO 222/////////////////////////////////////////////////////////////////////////////// 223 224bool Caches::bindMeshBuffer() { 225 return bindMeshBuffer(meshBuffer); 226} 227 228bool Caches::bindMeshBuffer(const GLuint buffer) { 229 if (mCurrentBuffer != buffer) { 230 glBindBuffer(GL_ARRAY_BUFFER, buffer); 231 mCurrentBuffer = buffer; 232 return true; 233 } 234 return false; 235} 236 237bool Caches::unbindMeshBuffer() { 238 if (mCurrentBuffer) { 239 glBindBuffer(GL_ARRAY_BUFFER, 0); 240 mCurrentBuffer = 0; 241 return true; 242 } 243 return false; 244} 245 246void Caches::bindPositionVertexPointer(bool force, GLuint slot, GLvoid* vertices, GLsizei stride) { 247 if (force || vertices != mCurrentPositionPointer) { 248 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices); 249 mCurrentPositionPointer = vertices; 250 } 251} 252 253void Caches::bindTexCoordsVertexPointer(bool force, GLuint slot, GLvoid* vertices) { 254 if (force || vertices != mCurrentTexCoordsPointer) { 255 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, gMeshStride, vertices); 256 mCurrentTexCoordsPointer = vertices; 257 } 258} 259 260void Caches::resetVertexPointers() { 261 mCurrentPositionPointer = this; 262 mCurrentTexCoordsPointer = this; 263} 264 265void Caches::resetTexCoordsVertexPointer() { 266 mCurrentTexCoordsPointer = this; 267} 268 269TextureVertex* Caches::getRegionMesh() { 270 // Create the mesh, 2 triangles and 4 vertices per rectangle in the region 271 if (!mRegionMesh) { 272 mRegionMesh = new TextureVertex[REGION_MESH_QUAD_COUNT * 4]; 273 274 uint16_t* regionIndices = new uint16_t[REGION_MESH_QUAD_COUNT * 6]; 275 for (int i = 0; i < REGION_MESH_QUAD_COUNT; i++) { 276 uint16_t quad = i * 4; 277 int index = i * 6; 278 regionIndices[index ] = quad; // top-left 279 regionIndices[index + 1] = quad + 1; // top-right 280 regionIndices[index + 2] = quad + 2; // bottom-left 281 regionIndices[index + 3] = quad + 2; // bottom-left 282 regionIndices[index + 4] = quad + 1; // top-right 283 regionIndices[index + 5] = quad + 3; // bottom-right 284 } 285 286 glGenBuffers(1, &mRegionMeshIndices); 287 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mRegionMeshIndices); 288 glBufferData(GL_ELEMENT_ARRAY_BUFFER, REGION_MESH_QUAD_COUNT * 6 * sizeof(uint16_t), 289 regionIndices, GL_STATIC_DRAW); 290 291 delete[] regionIndices; 292 } else { 293 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mRegionMeshIndices); 294 } 295 296 return mRegionMesh; 297} 298 299}; // namespace uirenderer 300}; // namespace android 301