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