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#include "LayerCache.h" 18 19#include "Caches.h" 20#include "Properties.h" 21 22#include <utils/Log.h> 23 24#include <GLES2/gl2.h> 25 26namespace android { 27namespace uirenderer { 28 29/////////////////////////////////////////////////////////////////////////////// 30// Constructors/destructor 31/////////////////////////////////////////////////////////////////////////////// 32 33LayerCache::LayerCache() 34 : mSize(0) 35 , mMaxSize(Properties::layerPoolSize) {} 36 37LayerCache::~LayerCache() { 38 clear(); 39} 40 41/////////////////////////////////////////////////////////////////////////////// 42// Size management 43/////////////////////////////////////////////////////////////////////////////// 44 45size_t LayerCache::getCount() { 46 return mCache.size(); 47} 48 49uint32_t LayerCache::getSize() { 50 return mSize; 51} 52 53uint32_t LayerCache::getMaxSize() { 54 return mMaxSize; 55} 56 57void LayerCache::setMaxSize(uint32_t maxSize) { 58 clear(); 59 mMaxSize = maxSize; 60} 61 62/////////////////////////////////////////////////////////////////////////////// 63// Caching 64/////////////////////////////////////////////////////////////////////////////// 65 66int LayerCache::LayerEntry::compare(const LayerCache::LayerEntry& lhs, 67 const LayerCache::LayerEntry& rhs) { 68 int deltaInt = int(lhs.mWidth) - int(rhs.mWidth); 69 if (deltaInt != 0) return deltaInt; 70 71 return int(lhs.mHeight) - int(rhs.mHeight); 72} 73 74void LayerCache::deleteLayer(Layer* layer) { 75 if (layer) { 76 LAYER_LOGD("Destroying layer %dx%d, fbo %d", layer->getWidth(), layer->getHeight(), 77 layer->getFbo()); 78 mSize -= layer->getWidth() * layer->getHeight() * 4; 79 layer->state = Layer::State::DeletedFromCache; 80 layer->decStrong(nullptr); 81 } 82} 83 84void LayerCache::clear() { 85 for (auto entry : mCache) { 86 deleteLayer(entry.mLayer); 87 } 88 mCache.clear(); 89} 90 91Layer* LayerCache::get(RenderState& renderState, const uint32_t width, const uint32_t height) { 92 Layer* layer = nullptr; 93 94 LayerEntry entry(width, height); 95 auto iter = mCache.find(entry); 96 97 if (iter != mCache.end()) { 98 entry = *iter; 99 mCache.erase(iter); 100 101 layer = entry.mLayer; 102 layer->state = Layer::State::RemovedFromCache; 103 mSize -= layer->getWidth() * layer->getHeight() * 4; 104 105 LAYER_LOGD("Reusing layer %dx%d", layer->getWidth(), layer->getHeight()); 106 } else { 107 LAYER_LOGD("Creating new layer %dx%d", entry.mWidth, entry.mHeight); 108 109 layer = new Layer(Layer::Type::DisplayList, renderState, entry.mWidth, entry.mHeight); 110 layer->setBlend(true); 111 layer->generateTexture(); 112 layer->bindTexture(); 113 layer->setFilter(GL_NEAREST); 114 layer->setWrap(GL_CLAMP_TO_EDGE, false); 115 116#if DEBUG_LAYERS 117 dump(); 118#endif 119 } 120 121 return layer; 122} 123 124void LayerCache::dump() { 125 for (auto entry : mCache) { 126 ALOGD(" Layer size %dx%d", entry.mWidth, entry.mHeight); 127 } 128} 129 130bool LayerCache::put(Layer* layer) { 131 if (!layer->isCacheable()) return false; 132 133 const uint32_t size = layer->getWidth() * layer->getHeight() * 4; 134 // Don't even try to cache a layer that's bigger than the cache 135 if (size < mMaxSize) { 136 // TODO: Use an LRU 137 while (mSize + size > mMaxSize) { 138 Layer* victim = mCache.begin()->mLayer; 139 deleteLayer(victim); 140 mCache.erase(mCache.begin()); 141 142 LAYER_LOGD(" Deleting layer %.2fx%.2f", victim->layer.getWidth(), 143 victim->layer.getHeight()); 144 } 145 146 layer->cancelDefer(); 147 148 LayerEntry entry(layer); 149 150 mCache.insert(entry); 151 mSize += size; 152 153 layer->state = Layer::State::InCache; 154 return true; 155 } 156 157 layer->state = Layer::State::FailedToCache; 158 return false; 159} 160 161}; // namespace uirenderer 162}; // namespace android 163