Caches.cpp revision baf29e7cf433624687c9d6b3bac180d33add8e0f
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 "LayerRenderer.h" 21#include "Properties.h" 22#include "renderstate/RenderState.h" 23#include "ShadowTessellator.h" 24#include "utils/GLUtils.h" 25 26#include <cutils/properties.h> 27#include <utils/Log.h> 28#include <utils/String8.h> 29 30namespace android { 31namespace uirenderer { 32 33Caches* Caches::sInstance = nullptr; 34 35/////////////////////////////////////////////////////////////////////////////// 36// Macros 37/////////////////////////////////////////////////////////////////////////////// 38 39#if DEBUG_CACHE_FLUSH 40 #define FLUSH_LOGD(...) ALOGD(__VA_ARGS__) 41#else 42 #define FLUSH_LOGD(...) 43#endif 44 45/////////////////////////////////////////////////////////////////////////////// 46// Constructors/destructor 47/////////////////////////////////////////////////////////////////////////////// 48 49Caches::Caches(RenderState& renderState) 50 : gradientCache(mExtensions) 51 , patchCache(renderState) 52 , programCache(mExtensions) 53 , dither(*this) 54 , mRenderState(&renderState) 55 , mInitialized(false) { 56 INIT_LOGD("Creating OpenGL renderer caches"); 57 init(); 58 initConstraints(); 59 initStaticProperties(); 60 initExtensions(); 61} 62 63bool Caches::init() { 64 if (mInitialized) return false; 65 66 ATRACE_NAME("Caches::init"); 67 68 mRegionMesh = nullptr; 69 mProgram = nullptr; 70 71 patchCache.init(); 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 log.appendFormat(" LayerCache %8d / %8d (numLayers = %zu)\n", 169 layerCache.getSize(), layerCache.getMaxSize(), layerCache.getCount()); 170 if (mRenderState) { 171 int memused = 0; 172 for (std::set<Layer*>::iterator it = mRenderState->mActiveLayers.begin(); 173 it != mRenderState->mActiveLayers.end(); it++) { 174 const Layer* layer = *it; 175 log.appendFormat(" Layer size %dx%d; isTextureLayer()=%d; texid=%u fbo=%u; refs=%d\n", 176 layer->getWidth(), layer->getHeight(), 177 layer->isTextureLayer(), layer->getTextureId(), 178 layer->getFbo(), layer->getStrongCount()); 179 memused += layer->getWidth() * layer->getHeight() * 4; 180 } 181 log.appendFormat(" Layers total %8d (numLayers = %zu)\n", 182 memused, mRenderState->mActiveLayers.size()); 183 total += memused; 184 } 185 log.appendFormat(" RenderBufferCache %8d / %8d\n", 186 renderBufferCache.getSize(), renderBufferCache.getMaxSize()); 187 log.appendFormat(" GradientCache %8d / %8d\n", 188 gradientCache.getSize(), gradientCache.getMaxSize()); 189 log.appendFormat(" PathCache %8d / %8d\n", 190 pathCache.getSize(), pathCache.getMaxSize()); 191 log.appendFormat(" TessellationCache %8d / %8d\n", 192 tessellationCache.getSize(), tessellationCache.getMaxSize()); 193 log.appendFormat(" TextDropShadowCache %8d / %8d\n", dropShadowCache.getSize(), 194 dropShadowCache.getMaxSize()); 195 log.appendFormat(" PatchCache %8d / %8d\n", 196 patchCache.getSize(), patchCache.getMaxSize()); 197 198 fontRenderer.dumpMemoryUsage(log); 199 200 log.appendFormat("Other:\n"); 201 log.appendFormat(" FboCache %8d / %8d\n", 202 fboCache.getSize(), fboCache.getMaxSize()); 203 204 total += textureCache.getSize(); 205 total += renderBufferCache.getSize(); 206 total += gradientCache.getSize(); 207 total += pathCache.getSize(); 208 total += tessellationCache.getSize(); 209 total += dropShadowCache.getSize(); 210 total += patchCache.getSize(); 211 total += fontRenderer.getSize(); 212 213 log.appendFormat("Total memory usage:\n"); 214 log.appendFormat(" %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f); 215} 216 217/////////////////////////////////////////////////////////////////////////////// 218// Memory management 219/////////////////////////////////////////////////////////////////////////////// 220 221void Caches::clearGarbage() { 222 textureCache.clearGarbage(); 223 pathCache.clearGarbage(); 224 patchCache.clearGarbage(); 225} 226 227void Caches::flush(FlushMode mode) { 228 FLUSH_LOGD("Flushing caches (mode %d)", mode); 229 230 switch (mode) { 231 case FlushMode::Full: 232 textureCache.clear(); 233 patchCache.clear(); 234 dropShadowCache.clear(); 235 gradientCache.clear(); 236 fontRenderer.clear(); 237 fboCache.clear(); 238 dither.clear(); 239 // fall through 240 case FlushMode::Moderate: 241 fontRenderer.flush(); 242 textureCache.flush(); 243 pathCache.clear(); 244 tessellationCache.clear(); 245 // fall through 246 case FlushMode::Layers: 247 layerCache.clear(); 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