LayerCache.cpp revision a1d3c91afbd52c7e8b01f4a9060c5459f02ae7a5
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 70void LayerCache::deleteLayer(Layer* layer) { 71 if (layer) { 72 LAYER_LOGD("Destroying layer %dx%d", layer->getWidth(), layer->getHeight()); 73 mSize -= layer->getWidth() * layer->getHeight() * 4; 74 layer->deleteFbo(); 75 layer->deleteTexture(); 76 delete layer; 77 } 78} 79 80void LayerCache::clear() { 81 size_t count = mCache.size(); 82 for (size_t i = 0; i < count; i++) { 83 deleteLayer(mCache.itemAt(i).mLayer); 84 } 85 mCache.clear(); 86} 87 88Layer* LayerCache::get(const uint32_t width, const uint32_t height) { 89 Layer* layer = NULL; 90 91 LayerEntry entry(width, height); 92 ssize_t index = mCache.indexOf(entry); 93 94 if (index >= 0) { 95 entry = mCache.itemAt(index); 96 mCache.removeAt(index); 97 98 layer = entry.mLayer; 99 mSize -= layer->getWidth() * layer->getHeight() * 4; 100 101 LAYER_LOGD("Reusing layer %dx%d", layer->getWidth(), layer->getHeight()); 102 } else { 103 LAYER_LOGD("Creating new layer %dx%d", entry.mWidth, entry.mHeight); 104 105 layer = new Layer(entry.mWidth, entry.mHeight); 106 layer->setBlend(true); 107 layer->setEmpty(true); 108 layer->setFbo(0); 109 110 layer->generateTexture(); 111 layer->bindTexture(); 112 layer->setFilter(GL_NEAREST); 113 layer->setWrap(GL_CLAMP_TO_EDGE, false); 114 glPixelStorei(GL_UNPACK_ALIGNMENT, 4); 115 116#if DEBUG_LAYERS 117 dump(); 118#endif 119 } 120 121 return layer; 122} 123 124void LayerCache::dump() { 125 size_t size = mCache.size(); 126 for (size_t i = 0; i < size; i++) { 127 const LayerEntry& entry = mCache.itemAt(i); 128 LAYER_LOGD(" Layer size %dx%d", entry.mWidth, entry.mHeight); 129 } 130} 131 132bool LayerCache::resize(Layer* layer, const uint32_t width, const uint32_t height) { 133 // TODO: We should be smarter and see if we have a texture of the appropriate 134 // size already in the cache, and reuse it instead of creating a new one 135 136 LayerEntry entry(width, height); 137 if (entry.mWidth <= layer->getWidth() && entry.mHeight <= layer->getHeight()) { 138 return true; 139 } 140 141 uint32_t oldWidth = layer->getWidth(); 142 uint32_t oldHeight = layer->getHeight(); 143 144 Caches::getInstance().activeTexture(0); 145 layer->bindTexture(); 146 layer->setSize(entry.mWidth, entry.mHeight); 147 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE); 148 149 if (glGetError() != GL_NO_ERROR) { 150 layer->setSize(oldWidth, oldHeight); 151 return false; 152 } 153 154 return true; 155} 156 157bool LayerCache::put(Layer* layer) { 158 if (!layer->isCacheable()) return false; 159 160 const uint32_t size = layer->getWidth() * layer->getHeight() * 4; 161 // Don't even try to cache a layer that's bigger than the cache 162 if (size < mMaxSize) { 163 // TODO: Use an LRU 164 while (mSize + size > mMaxSize) { 165 size_t position = 0; 166#if LAYER_REMOVE_BIGGEST_FIRST 167 position = mCache.size() - 1; 168#endif 169 Layer* victim = mCache.itemAt(position).mLayer; 170 deleteLayer(victim); 171 mCache.removeAt(position); 172 173 LAYER_LOGD(" Deleting layer %.2fx%.2f", victim->layer.getWidth(), 174 victim->layer.getHeight()); 175 } 176 177 LayerEntry entry(layer); 178 179 mCache.add(entry); 180 mSize += size; 181 182 return true; 183 } 184 return false; 185} 186 187}; // namespace uirenderer 188}; // namespace android 189