LayerCache.cpp revision e91080581f467d55913a8c5ab53dedc2dab2e5b6
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 "LayerCache.h" 24#include "Properties.h" 25 26namespace android { 27namespace uirenderer { 28 29/////////////////////////////////////////////////////////////////////////////// 30// Constructors/destructor 31/////////////////////////////////////////////////////////////////////////////// 32 33LayerCache::LayerCache(): mSize(0), mMaxSize(MB(DEFAULT_LAYER_CACHE_SIZE)) { 34 char property[PROPERTY_VALUE_MAX]; 35 if (property_get(PROPERTY_LAYER_CACHE_SIZE, property, NULL) > 0) { 36 LOGD(" Setting layer cache size to %sMB", property); 37 setMaxSize(MB(atof(property))); 38 } else { 39 LOGD(" Using default layer cache size of %.2fMB", DEFAULT_LAYER_CACHE_SIZE); 40 } 41} 42 43LayerCache::~LayerCache() { 44 clear(); 45} 46 47/////////////////////////////////////////////////////////////////////////////// 48// Size management 49/////////////////////////////////////////////////////////////////////////////// 50 51uint32_t LayerCache::getSize() { 52 return mSize; 53} 54 55uint32_t LayerCache::getMaxSize() { 56 return mMaxSize; 57} 58 59void LayerCache::setMaxSize(uint32_t maxSize) { 60 clear(); 61 mMaxSize = maxSize; 62} 63 64/////////////////////////////////////////////////////////////////////////////// 65// Caching 66/////////////////////////////////////////////////////////////////////////////// 67 68void LayerCache::deleteLayer(Layer* layer) { 69 if (layer) { 70 mSize -= layer->width * layer->height * 4; 71 glDeleteTextures(1, &layer->texture); 72 delete layer; 73 } 74} 75 76void LayerCache::clear() { 77 size_t count = mCache.size(); 78 for (size_t i = 0; i < count; i++) { 79 deleteLayer(mCache.itemAt(i).mLayer); 80 } 81 mCache.clear(); 82} 83 84Layer* LayerCache::get(const uint32_t width, const uint32_t height) { 85 Layer* layer = NULL; 86 87 LayerEntry entry(width, height); 88 ssize_t index = mCache.indexOf(entry); 89 90 if (index >= 0) { 91 entry = mCache.itemAt(index); 92 mCache.removeAt(index); 93 94 layer = entry.mLayer; 95 mSize -= layer->width * layer->height * 4; 96 97 LAYER_LOGD("Reusing layer %dx%d", layer->width, layer->height); 98 } else { 99 LAYER_LOGD("Creating new layer %dx%d", entry.mWidth, entry.mHeight); 100 101 layer = new Layer(entry.mWidth, entry.mHeight); 102 layer->blend = true; 103 layer->empty = true; 104 layer->fbo = 0; 105 106 glGenTextures(1, &layer->texture); 107 glBindTexture(GL_TEXTURE_2D, layer->texture); 108 109 glPixelStorei(GL_UNPACK_ALIGNMENT, 4); 110 111 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 112 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 113 114 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 115 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 116 117#if DEBUG_LAYERS 118 size_t size = mCache.size(); 119 for (size_t i = 0; i < size; i++) { 120 const LayerEntry& entry = mCache.itemAt(i); 121 LAYER_LOGD(" Layer size %dx%d", entry.mWidth, entry.mHeight); 122 } 123#endif 124 } 125 126 return layer; 127} 128 129bool LayerCache::put(Layer* layer) { 130 const uint32_t size = layer->width * layer->height * 4; 131 // Don't even try to cache a layer that's bigger than the cache 132 if (size < mMaxSize) { 133 // TODO: Use an LRU 134 while (mSize + size > mMaxSize) { 135 size_t position = 0; 136#if LAYER_REMOVE_BIGGEST 137 position = mCache.size() - 1; 138#endif 139 Layer* victim = mCache.itemAt(position).mLayer; 140 deleteLayer(victim); 141 mCache.removeAt(position); 142 143 LAYER_LOGD(" Deleting layer %.2fx%.2f", victim->layer.getWidth(), 144 victim->layer.getHeight()); 145 } 146 147 LayerEntry entry(layer); 148 149 mCache.add(entry); 150 mSize += size; 151 152 return true; 153 } 154 return false; 155} 156 157}; // namespace uirenderer 158}; // namespace android 159