blob: f5681ce712d5803e8b33852afd2fe6e8a441eacb [file] [log] [blame]
Romain Guydda570202010-07-06 11:39:32 -07001/*
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
Chris Craik9fded232015-11-11 16:42:34 -080017#include "LayerCache.h"
18
19#include "Caches.h"
20#include "Properties.h"
Romain Guydda570202010-07-06 11:39:32 -070021
Romain Guyf18fd992010-07-08 11:45:51 -070022#include <utils/Log.h>
23
Chris Craik9fded232015-11-11 16:42:34 -080024#include <GLES2/gl2.h>
Romain Guydda570202010-07-06 11:39:32 -070025
26namespace android {
27namespace uirenderer {
28
29///////////////////////////////////////////////////////////////////////////////
30// Constructors/destructor
31///////////////////////////////////////////////////////////////////////////////
32
Chris Craik9fded232015-11-11 16:42:34 -080033LayerCache::LayerCache()
34 : mSize(0)
35 , mMaxSize(Properties::layerPoolSize) {}
Romain Guyfb8b7632010-08-23 21:05:08 -070036
Romain Guydda570202010-07-06 11:39:32 -070037LayerCache::~LayerCache() {
Romain Guy5f0c6a42010-07-07 13:06:26 -070038 clear();
Romain Guydda570202010-07-06 11:39:32 -070039}
40
41///////////////////////////////////////////////////////////////////////////////
42// Size management
43///////////////////////////////////////////////////////////////////////////////
44
John Reck17035b02014-09-03 07:39:53 -070045size_t LayerCache::getCount() {
46 return mCache.size();
47}
48
Romain Guydda570202010-07-06 11:39:32 -070049uint32_t LayerCache::getSize() {
50 return mSize;
51}
52
53uint32_t LayerCache::getMaxSize() {
54 return mMaxSize;
55}
56
57void LayerCache::setMaxSize(uint32_t maxSize) {
Romain Guy8550c4c2010-10-08 15:49:53 -070058 clear();
Romain Guydda570202010-07-06 11:39:32 -070059 mMaxSize = maxSize;
Romain Guydda570202010-07-06 11:39:32 -070060}
61
62///////////////////////////////////////////////////////////////////////////////
63// Caching
64///////////////////////////////////////////////////////////////////////////////
65
Romain Guye3a9b242013-01-08 11:15:30 -080066int 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
Romain Guydda570202010-07-06 11:39:32 -070074void LayerCache::deleteLayer(Layer* layer) {
75 if (layer) {
Chris Craik07adacf2014-12-19 10:08:40 -080076 LAYER_LOGD("Destroying layer %dx%d, fbo %d", layer->getWidth(), layer->getHeight(),
77 layer->getFbo());
Romain Guy9ace8f52011-07-07 20:50:11 -070078 mSize -= layer->getWidth() * layer->getHeight() * 4;
Chris Craikb9ce116d2015-08-20 15:14:06 -070079 layer->state = Layer::State::DeletedFromCache;
Chris Craikd41c4d82015-01-05 15:51:13 -080080 layer->decStrong(nullptr);
Romain Guydda570202010-07-06 11:39:32 -070081 }
82}
83
84void LayerCache::clear() {
John Reckbef837d2015-07-29 16:51:05 -070085 for (auto entry : mCache) {
86 deleteLayer(entry.mLayer);
Romain Guy8550c4c2010-10-08 15:49:53 -070087 }
Romain Guydda570202010-07-06 11:39:32 -070088 mCache.clear();
Romain Guydda570202010-07-06 11:39:32 -070089}
90
John Reck3b202512014-06-23 13:13:08 -070091Layer* LayerCache::get(RenderState& renderState, const uint32_t width, const uint32_t height) {
Chris Craikd41c4d82015-01-05 15:51:13 -080092 Layer* layer = nullptr;
Romain Guyf18fd992010-07-08 11:45:51 -070093
Romain Guy8550c4c2010-10-08 15:49:53 -070094 LayerEntry entry(width, height);
John Reckbef837d2015-07-29 16:51:05 -070095 auto iter = mCache.find(entry);
Romain Guy8550c4c2010-10-08 15:49:53 -070096
John Reckbef837d2015-07-29 16:51:05 -070097 if (iter != mCache.end()) {
98 entry = *iter;
99 mCache.erase(iter);
Romain Guy8550c4c2010-10-08 15:49:53 -0700100
101 layer = entry.mLayer;
Chris Craikb9ce116d2015-08-20 15:14:06 -0700102 layer->state = Layer::State::RemovedFromCache;
Romain Guy9ace8f52011-07-07 20:50:11 -0700103 mSize -= layer->getWidth() * layer->getHeight() * 4;
Romain Guy8550c4c2010-10-08 15:49:53 -0700104
Chris Craik07adacf2014-12-19 10:08:40 -0800105 LAYER_LOGD("Reusing layer %dx%d", layer->getWidth(), layer->getHeight());
Romain Guyf18fd992010-07-08 11:45:51 -0700106 } else {
Chris Craik07adacf2014-12-19 10:08:40 -0800107 LAYER_LOGD("Creating new layer %dx%d", entry.mWidth, entry.mHeight);
Romain Guyf18fd992010-07-08 11:45:51 -0700108
Chris Craikb9ce116d2015-08-20 15:14:06 -0700109 layer = new Layer(Layer::Type::DisplayList, renderState, entry.mWidth, entry.mHeight);
Romain Guy9ace8f52011-07-07 20:50:11 -0700110 layer->setBlend(true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700111 layer->generateTexture();
112 layer->bindTexture();
Romain Guy39d252a2011-12-12 18:14:06 -0800113 layer->setFilter(GL_NEAREST);
114 layer->setWrap(GL_CLAMP_TO_EDGE, false);
Romain Guy0bb56672010-10-01 00:25:02 -0700115
Romain Guyeb993562010-10-05 18:14:38 -0700116#if DEBUG_LAYERS
Romain Guyeea60692011-07-26 20:35:55 -0700117 dump();
Romain Guyeb993562010-10-05 18:14:38 -0700118#endif
Romain Guydda570202010-07-06 11:39:32 -0700119 }
Romain Guyf18fd992010-07-08 11:45:51 -0700120
Romain Guydda570202010-07-06 11:39:32 -0700121 return layer;
122}
123
Romain Guyeea60692011-07-26 20:35:55 -0700124void LayerCache::dump() {
John Reckbef837d2015-07-29 16:51:05 -0700125 for (auto entry : mCache) {
Chris Craik07adacf2014-12-19 10:08:40 -0800126 ALOGD(" Layer size %dx%d", entry.mWidth, entry.mHeight);
Romain Guyeea60692011-07-26 20:35:55 -0700127 }
128}
129
Romain Guy8550c4c2010-10-08 15:49:53 -0700130bool LayerCache::put(Layer* layer) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700131 if (!layer->isCacheable()) return false;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700132
Romain Guy9ace8f52011-07-07 20:50:11 -0700133 const uint32_t size = layer->getWidth() * layer->getHeight() * 4;
Romain Guydda570202010-07-06 11:39:32 -0700134 // Don't even try to cache a layer that's bigger than the cache
135 if (size < mMaxSize) {
Romain Guy8550c4c2010-10-08 15:49:53 -0700136 // TODO: Use an LRU
Romain Guydda570202010-07-06 11:39:32 -0700137 while (mSize + size > mMaxSize) {
John Reckbef837d2015-07-29 16:51:05 -0700138 Layer* victim = mCache.begin()->mLayer;
Romain Guye9108052010-10-12 18:15:42 -0700139 deleteLayer(victim);
John Reckbef837d2015-07-29 16:51:05 -0700140 mCache.erase(mCache.begin());
Romain Guy8550c4c2010-10-08 15:49:53 -0700141
Chris Craik07adacf2014-12-19 10:08:40 -0800142 LAYER_LOGD(" Deleting layer %.2fx%.2f", victim->layer.getWidth(),
143 victim->layer.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700144 }
145
Romain Guye93482f2013-06-17 13:14:51 -0700146 layer->cancelDefer();
Romain Guy5c88fc72012-04-02 17:43:05 -0700147
Romain Guy8550c4c2010-10-08 15:49:53 -0700148 LayerEntry entry(layer);
149
John Reckbef837d2015-07-29 16:51:05 -0700150 mCache.insert(entry);
Romain Guydda570202010-07-06 11:39:32 -0700151 mSize += size;
152
Chris Craikb9ce116d2015-08-20 15:14:06 -0700153 layer->state = Layer::State::InCache;
Romain Guydda570202010-07-06 11:39:32 -0700154 return true;
155 }
Chris Craikbfd1cd62014-09-10 13:04:31 -0700156
Chris Craikb9ce116d2015-08-20 15:14:06 -0700157 layer->state = Layer::State::FailedToCache;
Romain Guydda570202010-07-06 11:39:32 -0700158 return false;
159}
160
161}; // namespace uirenderer
162}; // namespace android