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 "LayerCache.h" 25 #include "Properties.h" 26 27 namespace android { 28 namespace uirenderer { 29 30 /////////////////////////////////////////////////////////////////////////////// 31 // Constructors/destructor 32 /////////////////////////////////////////////////////////////////////////////// 33 34 LayerCache::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 44 LayerCache::~LayerCache() { 45 clear(); 46 } 47 48 /////////////////////////////////////////////////////////////////////////////// 49 // Size management 50 /////////////////////////////////////////////////////////////////////////////// 51 52 size_t LayerCache::getCount() { 53 return mCache.size(); 54 } 55 56 uint32_t LayerCache::getSize() { 57 return mSize; 58 } 59 60 uint32_t LayerCache::getMaxSize() { 61 return mMaxSize; 62 } 63 64 void LayerCache::setMaxSize(uint32_t maxSize) { 65 clear(); 66 mMaxSize = maxSize; 67 } 68 69 /////////////////////////////////////////////////////////////////////////////// 70 // Caching 71 /////////////////////////////////////////////////////////////////////////////// 72 73 int LayerCache::LayerEntry::compare(const LayerCache::LayerEntry& lhs, 74 const LayerCache::LayerEntry& rhs) { 75 int deltaInt = int(lhs.mWidth) - int(rhs.mWidth); 76 if (deltaInt != 0) return deltaInt; 77 78 return int(lhs.mHeight) - int(rhs.mHeight); 79 } 80 81 void LayerCache::deleteLayer(Layer* layer) { 82 if (layer) { 83 LAYER_LOGD("Destroying layer %dx%d, fbo %d", layer->getWidth(), layer->getHeight(), 84 layer->getFbo()); 85 mSize -= layer->getWidth() * layer->getHeight() * 4; 86 layer->state = Layer::kState_DeletedFromCache; 87 layer->decStrong(0); 88 } 89 } 90 91 void LayerCache::clear() { 92 size_t count = mCache.size(); 93 for (size_t i = 0; i < count; i++) { 94 deleteLayer(mCache.itemAt(i).mLayer); 95 } 96 mCache.clear(); 97 } 98 99 Layer* LayerCache::get(RenderState& renderState, const uint32_t width, const uint32_t height) { 100 Layer* layer = NULL; 101 102 LayerEntry entry(width, height); 103 ssize_t index = mCache.indexOf(entry); 104 105 if (index >= 0) { 106 entry = mCache.itemAt(index); 107 mCache.removeAt(index); 108 109 layer = entry.mLayer; 110 layer->state = Layer::kState_RemovedFromCache; 111 mSize -= layer->getWidth() * layer->getHeight() * 4; 112 113 LAYER_LOGD("Reusing layer %dx%d", layer->getWidth(), layer->getHeight()); 114 } else { 115 LAYER_LOGD("Creating new layer %dx%d", entry.mWidth, entry.mHeight); 116 117 layer = new Layer(Layer::kType_DisplayList, renderState, entry.mWidth, entry.mHeight); 118 layer->setBlend(true); 119 layer->setEmpty(true); 120 layer->setFbo(0); 121 122 layer->generateTexture(); 123 layer->bindTexture(); 124 layer->setFilter(GL_NEAREST); 125 layer->setWrap(GL_CLAMP_TO_EDGE, false); 126 glPixelStorei(GL_UNPACK_ALIGNMENT, 4); 127 128 #if DEBUG_LAYERS 129 dump(); 130 #endif 131 } 132 133 return layer; 134 } 135 136 void LayerCache::dump() { 137 size_t size = mCache.size(); 138 for (size_t i = 0; i < size; i++) { 139 const LayerEntry& entry = mCache.itemAt(i); 140 LAYER_LOGD(" Layer size %dx%d", entry.mWidth, entry.mHeight); 141 } 142 } 143 144 bool LayerCache::put(Layer* layer) { 145 if (!layer->isCacheable()) return false; 146 147 const uint32_t size = layer->getWidth() * layer->getHeight() * 4; 148 // Don't even try to cache a layer that's bigger than the cache 149 if (size < mMaxSize) { 150 // TODO: Use an LRU 151 while (mSize + size > mMaxSize) { 152 size_t position = 0; 153 #if LAYER_REMOVE_BIGGEST_FIRST 154 position = mCache.size() - 1; 155 #endif 156 Layer* victim = mCache.itemAt(position).mLayer; 157 deleteLayer(victim); 158 mCache.removeAt(position); 159 160 LAYER_LOGD(" Deleting layer %.2fx%.2f", victim->layer.getWidth(), 161 victim->layer.getHeight()); 162 } 163 164 layer->cancelDefer(); 165 166 LayerEntry entry(layer); 167 168 mCache.add(entry); 169 mSize += size; 170 171 layer->state = Layer::kState_InCache; 172 return true; 173 } 174 175 layer->state = Layer::kState_FailedToCache; 176 return false; 177 } 178 179 }; // namespace uirenderer 180 }; // namespace android 181