LayerCache.cpp revision e3a9b24b5e3f9b2058486814a6d27729e51ad466
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 <GLES2/gl2.h> 20 21#include <utils/Log.h> 22 23#include "Caches.h" 24#include "Debug.h" 25#include "LayerCache.h" 26#include "Properties.h" 27 28namespace android { 29namespace uirenderer { 30 31/////////////////////////////////////////////////////////////////////////////// 32// Constructors/destructor 33/////////////////////////////////////////////////////////////////////////////// 34 35LayerCache::LayerCache(): mSize(0), mMaxSize(MB(DEFAULT_LAYER_CACHE_SIZE)) { 36 char property[PROPERTY_VALUE_MAX]; 37 if (property_get(PROPERTY_LAYER_CACHE_SIZE, property, NULL) > 0) { 38 INIT_LOGD(" Setting layer cache size to %sMB", property); 39 setMaxSize(MB(atof(property))); 40 } else { 41 INIT_LOGD(" Using default layer cache size of %.2fMB", DEFAULT_LAYER_CACHE_SIZE); 42 } 43} 44 45LayerCache::~LayerCache() { 46 clear(); 47} 48 49/////////////////////////////////////////////////////////////////////////////// 50// Size management 51/////////////////////////////////////////////////////////////////////////////// 52 53uint32_t LayerCache::getSize() { 54 return mSize; 55} 56 57uint32_t LayerCache::getMaxSize() { 58 return mMaxSize; 59} 60 61void LayerCache::setMaxSize(uint32_t maxSize) { 62 clear(); 63 mMaxSize = maxSize; 64} 65 66/////////////////////////////////////////////////////////////////////////////// 67// Caching 68/////////////////////////////////////////////////////////////////////////////// 69 70int LayerCache::LayerEntry::compare(const LayerCache::LayerEntry& lhs, 71 const LayerCache::LayerEntry& rhs) { 72 int deltaInt = int(lhs.mWidth) - int(rhs.mWidth); 73 if (deltaInt != 0) return deltaInt; 74 75 return int(lhs.mHeight) - int(rhs.mHeight); 76} 77 78void LayerCache::deleteLayer(Layer* layer) { 79 if (layer) { 80 LAYER_LOGD("Destroying layer %dx%d, fbo %d", layer->getWidth(), layer->getHeight(), 81 layer->getFbo()); 82 mSize -= layer->getWidth() * layer->getHeight() * 4; 83 Caches::getInstance().resourceCache.decrementRefcount(layer); 84 } 85} 86 87void LayerCache::clear() { 88 size_t count = mCache.size(); 89 for (size_t i = 0; i < count; i++) { 90 deleteLayer(mCache.itemAt(i).mLayer); 91 } 92 mCache.clear(); 93} 94 95Layer* LayerCache::get(const uint32_t width, const uint32_t height) { 96 Layer* layer = NULL; 97 98 LayerEntry entry(width, height); 99 ssize_t index = mCache.indexOf(entry); 100 101 if (index >= 0) { 102 entry = mCache.itemAt(index); 103 mCache.removeAt(index); 104 105 layer = entry.mLayer; 106 mSize -= layer->getWidth() * layer->getHeight() * 4; 107 108 LAYER_LOGD("Reusing layer %dx%d", layer->getWidth(), layer->getHeight()); 109 } else { 110 LAYER_LOGD("Creating new layer %dx%d", entry.mWidth, entry.mHeight); 111 112 layer = new Layer(entry.mWidth, entry.mHeight); 113 layer->setBlend(true); 114 layer->setEmpty(true); 115 layer->setFbo(0); 116 117 layer->generateTexture(); 118 layer->bindTexture(); 119 layer->setFilter(GL_NEAREST); 120 layer->setWrap(GL_CLAMP_TO_EDGE, false); 121 glPixelStorei(GL_UNPACK_ALIGNMENT, 4); 122 123#if DEBUG_LAYERS 124 dump(); 125#endif 126 } 127 128 return layer; 129} 130 131void LayerCache::dump() { 132 size_t size = mCache.size(); 133 for (size_t i = 0; i < size; i++) { 134 const LayerEntry& entry = mCache.itemAt(i); 135 LAYER_LOGD(" Layer size %dx%d", entry.mWidth, entry.mHeight); 136 } 137} 138 139bool LayerCache::resize(Layer* layer, const uint32_t width, const uint32_t height) { 140 // TODO: We should be smarter and see if we have a texture of the appropriate 141 // size already in the cache, and reuse it instead of creating a new one 142 143 LayerEntry entry(width, height); 144 if (entry.mWidth <= layer->getWidth() && entry.mHeight <= layer->getHeight()) { 145 return true; 146 } 147 148 uint32_t oldWidth = layer->getWidth(); 149 uint32_t oldHeight = layer->getHeight(); 150 151 Caches::getInstance().activeTexture(0); 152 layer->bindTexture(); 153 layer->setSize(entry.mWidth, entry.mHeight); 154 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE); 155 156 if (glGetError() != GL_NO_ERROR) { 157 layer->setSize(oldWidth, oldHeight); 158 return false; 159 } 160 161 return true; 162} 163 164bool LayerCache::put(Layer* layer) { 165 if (!layer->isCacheable()) return false; 166 167 const uint32_t size = layer->getWidth() * layer->getHeight() * 4; 168 // Don't even try to cache a layer that's bigger than the cache 169 if (size < mMaxSize) { 170 // TODO: Use an LRU 171 while (mSize + size > mMaxSize) { 172 size_t position = 0; 173#if LAYER_REMOVE_BIGGEST_FIRST 174 position = mCache.size() - 1; 175#endif 176 Layer* victim = mCache.itemAt(position).mLayer; 177 deleteLayer(victim); 178 mCache.removeAt(position); 179 180 LAYER_LOGD(" Deleting layer %.2fx%.2f", victim->layer.getWidth(), 181 victim->layer.getHeight()); 182 } 183 184 layer->deferredUpdateScheduled = false; 185 layer->renderer = NULL; 186 layer->displayList = NULL; 187 188 LayerEntry entry(layer); 189 190 mCache.add(entry); 191 mSize += size; 192 193 return true; 194 } 195 return false; 196} 197 198}; // namespace uirenderer 199}; // namespace android 200