Caches.cpp revision 253f2c213f6ecda63b6872aee77bd30d5ec07c82
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#include "Caches.h" 18 19#include "GammaFontRenderer.h" 20#include "Layer.h" 21#include "Properties.h" 22#include "renderstate/RenderState.h" 23#include "ShadowTessellator.h" 24#ifdef BUGREPORT_FONT_CACHE_USAGE 25#include "font/FontCacheHistoryTracker.h" 26#endif 27#include "utils/GLUtils.h" 28 29#include <cutils/properties.h> 30#include <utils/Log.h> 31#include <utils/String8.h> 32 33namespace android { 34namespace uirenderer { 35 36Caches* Caches::sInstance = nullptr; 37 38/////////////////////////////////////////////////////////////////////////////// 39// Macros 40/////////////////////////////////////////////////////////////////////////////// 41 42#if DEBUG_CACHE_FLUSH 43 #define FLUSH_LOGD(...) ALOGD(__VA_ARGS__) 44#else 45 #define FLUSH_LOGD(...) 46#endif 47 48/////////////////////////////////////////////////////////////////////////////// 49// Constructors/destructor 50/////////////////////////////////////////////////////////////////////////////// 51 52Caches::Caches(RenderState& renderState) 53 : gradientCache(mExtensions) 54 , patchCache(renderState) 55 , programCache(mExtensions) 56 , mRenderState(&renderState) 57 , mInitialized(false) { 58 INIT_LOGD("Creating OpenGL renderer caches"); 59 init(); 60 initConstraints(); 61 initStaticProperties(); 62 initExtensions(); 63} 64 65bool Caches::init() { 66 if (mInitialized) return false; 67 68 ATRACE_NAME("Caches::init"); 69 70 mRegionMesh = nullptr; 71 mProgram = nullptr; 72 73 mInitialized = true; 74 75 mPixelBufferState = new PixelBufferState(); 76 mTextureState = new TextureState(); 77 mTextureState->constructTexture(*this); 78 79 return true; 80} 81 82void Caches::initExtensions() { 83 if (mExtensions.hasDebugMarker()) { 84 eventMark = glInsertEventMarkerEXT; 85 86 startMark = glPushGroupMarkerEXT; 87 endMark = glPopGroupMarkerEXT; 88 } else { 89 eventMark = eventMarkNull; 90 startMark = startMarkNull; 91 endMark = endMarkNull; 92 } 93} 94 95void Caches::initConstraints() { 96 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize); 97} 98 99void Caches::initStaticProperties() { 100 // OpenGL ES 3.0+ specific features 101 gpuPixelBuffersEnabled = mExtensions.hasPixelBufferObjects() 102 && property_get_bool(PROPERTY_ENABLE_GPU_PIXEL_BUFFERS, true); 103} 104 105void Caches::terminate() { 106 if (!mInitialized) return; 107 mRegionMesh.reset(nullptr); 108 109 fboCache.clear(); 110 111 programCache.clear(); 112 mProgram = nullptr; 113 114 patchCache.clear(); 115 116 clearGarbage(); 117 118 delete mPixelBufferState; 119 mPixelBufferState = nullptr; 120 delete mTextureState; 121 mTextureState = nullptr; 122 mInitialized = false; 123} 124 125void Caches::setProgram(const ProgramDescription& description) { 126 setProgram(programCache.get(description)); 127} 128 129void Caches::setProgram(Program* program) { 130 if (!program || !program->isInUse()) { 131 if (mProgram) { 132 mProgram->remove(); 133 } 134 if (program) { 135 program->use(); 136 } 137 mProgram = program; 138 } 139} 140 141/////////////////////////////////////////////////////////////////////////////// 142// Debug 143/////////////////////////////////////////////////////////////////////////////// 144 145uint32_t Caches::getOverdrawColor(uint32_t amount) const { 146 static uint32_t sOverdrawColors[2][4] = { 147 { 0x2f0000ff, 0x2f00ff00, 0x3fff0000, 0x7fff0000 }, 148 { 0x2f0000ff, 0x4fffff00, 0x5fff8ad8, 0x7fff0000 } 149 }; 150 if (amount < 1) amount = 1; 151 if (amount > 4) amount = 4; 152 153 int overdrawColorIndex = static_cast<int>(Properties::overdrawColorSet); 154 return sOverdrawColors[overdrawColorIndex][amount - 1]; 155} 156 157void Caches::dumpMemoryUsage() { 158 String8 stringLog; 159 dumpMemoryUsage(stringLog); 160 ALOGD("%s", stringLog.string()); 161} 162 163void Caches::dumpMemoryUsage(String8 &log) { 164 uint32_t total = 0; 165 log.appendFormat("Current memory usage / total memory usage (bytes):\n"); 166 log.appendFormat(" TextureCache %8d / %8d\n", 167 textureCache.getSize(), textureCache.getMaxSize()); 168 if (mRenderState) { 169 int memused = 0; 170 for (std::set<Layer*>::iterator it = mRenderState->mActiveLayers.begin(); 171 it != mRenderState->mActiveLayers.end(); it++) { 172 const Layer* layer = *it; 173 log.appendFormat(" Layer size %dx%d; texid=%u refs=%d\n", 174 layer->getWidth(), layer->getHeight(), 175 layer->getTextureId(), 176 layer->getStrongCount()); 177 memused += layer->getWidth() * layer->getHeight() * 4; 178 } 179 log.appendFormat(" Layers total %8d (numLayers = %zu)\n", 180 memused, mRenderState->mActiveLayers.size()); 181 total += memused; 182 } 183 log.appendFormat(" RenderBufferCache %8d / %8d\n", 184 renderBufferCache.getSize(), renderBufferCache.getMaxSize()); 185 log.appendFormat(" GradientCache %8d / %8d\n", 186 gradientCache.getSize(), gradientCache.getMaxSize()); 187 log.appendFormat(" PathCache %8d / %8d\n", 188 pathCache.getSize(), pathCache.getMaxSize()); 189 log.appendFormat(" TessellationCache %8d / %8d\n", 190 tessellationCache.getSize(), tessellationCache.getMaxSize()); 191 log.appendFormat(" TextDropShadowCache %8d / %8d\n", dropShadowCache.getSize(), 192 dropShadowCache.getMaxSize()); 193 log.appendFormat(" PatchCache %8d / %8d\n", 194 patchCache.getSize(), patchCache.getMaxSize()); 195 196 fontRenderer.dumpMemoryUsage(log); 197 198 log.appendFormat("Other:\n"); 199 log.appendFormat(" FboCache %8d / %8d\n", 200 fboCache.getSize(), fboCache.getMaxSize()); 201 202 total += textureCache.getSize(); 203 total += renderBufferCache.getSize(); 204 total += gradientCache.getSize(); 205 total += pathCache.getSize(); 206 total += tessellationCache.getSize(); 207 total += dropShadowCache.getSize(); 208 total += patchCache.getSize(); 209 total += fontRenderer.getSize(); 210 211 log.appendFormat("Total memory usage:\n"); 212 log.appendFormat(" %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f); 213 214#ifdef BUGREPORT_FONT_CACHE_USAGE 215 fontRenderer.getFontRenderer().historyTracker().dump(log); 216#endif 217} 218 219/////////////////////////////////////////////////////////////////////////////// 220// Memory management 221/////////////////////////////////////////////////////////////////////////////// 222 223void Caches::clearGarbage() { 224 textureCache.clearGarbage(); 225 pathCache.clearGarbage(); 226 patchCache.clearGarbage(); 227} 228 229void Caches::flush(FlushMode mode) { 230 FLUSH_LOGD("Flushing caches (mode %d)", mode); 231 232 switch (mode) { 233 case FlushMode::Full: 234 textureCache.clear(); 235 patchCache.clear(); 236 dropShadowCache.clear(); 237 gradientCache.clear(); 238 fontRenderer.clear(); 239 fboCache.clear(); 240 // fall through 241 case FlushMode::Moderate: 242 fontRenderer.flush(); 243 textureCache.flush(); 244 pathCache.clear(); 245 tessellationCache.clear(); 246 // fall through 247 case FlushMode::Layers: 248 renderBufferCache.clear(); 249 break; 250 } 251 252 clearGarbage(); 253 glFinish(); 254 // Errors during cleanup should be considered non-fatal, dump them and 255 // and move on. TODO: All errors or just errors like bad surface? 256 GLUtils::dumpGLErrors(); 257} 258 259/////////////////////////////////////////////////////////////////////////////// 260// Regions 261/////////////////////////////////////////////////////////////////////////////// 262 263TextureVertex* Caches::getRegionMesh() { 264 // Create the mesh, 2 triangles and 4 vertices per rectangle in the region 265 if (!mRegionMesh) { 266 mRegionMesh.reset(new TextureVertex[kMaxNumberOfQuads * 4]); 267 } 268 269 return mRegionMesh.get(); 270} 271 272/////////////////////////////////////////////////////////////////////////////// 273// Temporary Properties 274/////////////////////////////////////////////////////////////////////////////// 275 276}; // namespace uirenderer 277}; // namespace android 278