blob: b117754347edee1b5fb1c4a26f51338da09921be [file] [log] [blame]
Romain Guydda57022010-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 Guydda57022010-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 Guydda57022010-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 Guydda57022010-07-06 11:39:32 -070037LayerCache::~LayerCache() {
Romain Guy5f0c6a42010-07-07 13:06:26 -070038 clear();
Romain Guydda57022010-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 Guydda57022010-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 Guydda57022010-07-06 11:39:32 -070059 mMaxSize = maxSize;
Romain Guydda57022010-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 Guydda57022010-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 Guydda57022010-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 Guydda57022010-07-06 11:39:32 -070088 mCache.clear();
Romain Guydda57022010-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 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
116
Romain Guyeb993562010-10-05 18:14:38 -0700117#if DEBUG_LAYERS
Romain Guyeea60692011-07-26 20:35:55 -0700118 dump();
Romain Guyeb993562010-10-05 18:14:38 -0700119#endif
Romain Guydda57022010-07-06 11:39:32 -0700120 }
Romain Guyf18fd992010-07-08 11:45:51 -0700121
Romain Guydda57022010-07-06 11:39:32 -0700122 return layer;
123}
124
Romain Guyeea60692011-07-26 20:35:55 -0700125void LayerCache::dump() {
John Reckbef837d2015-07-29 16:51:05 -0700126 for (auto entry : mCache) {
Chris Craik07adacf2014-12-19 10:08:40 -0800127 ALOGD(" Layer size %dx%d", entry.mWidth, entry.mHeight);
Romain Guyeea60692011-07-26 20:35:55 -0700128 }
129}
130
Romain Guy8550c4c2010-10-08 15:49:53 -0700131bool LayerCache::put(Layer* layer) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700132 if (!layer->isCacheable()) return false;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700133
Romain Guy9ace8f52011-07-07 20:50:11 -0700134 const uint32_t size = layer->getWidth() * layer->getHeight() * 4;
Romain Guydda57022010-07-06 11:39:32 -0700135 // Don't even try to cache a layer that's bigger than the cache
136 if (size < mMaxSize) {
Romain Guy8550c4c2010-10-08 15:49:53 -0700137 // TODO: Use an LRU
Romain Guydda57022010-07-06 11:39:32 -0700138 while (mSize + size > mMaxSize) {
John Reckbef837d2015-07-29 16:51:05 -0700139 Layer* victim = mCache.begin()->mLayer;
Romain Guye9108052010-10-12 18:15:42 -0700140 deleteLayer(victim);
John Reckbef837d2015-07-29 16:51:05 -0700141 mCache.erase(mCache.begin());
Romain Guy8550c4c2010-10-08 15:49:53 -0700142
Chris Craik07adacf2014-12-19 10:08:40 -0800143 LAYER_LOGD(" Deleting layer %.2fx%.2f", victim->layer.getWidth(),
144 victim->layer.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700145 }
146
Romain Guye93482f2013-06-17 13:14:51 -0700147 layer->cancelDefer();
Romain Guy5c88fc72012-04-02 17:43:05 -0700148
Romain Guy8550c4c2010-10-08 15:49:53 -0700149 LayerEntry entry(layer);
150
John Reckbef837d2015-07-29 16:51:05 -0700151 mCache.insert(entry);
Romain Guydda57022010-07-06 11:39:32 -0700152 mSize += size;
153
Chris Craikb9ce116d2015-08-20 15:14:06 -0700154 layer->state = Layer::State::InCache;
Romain Guydda57022010-07-06 11:39:32 -0700155 return true;
156 }
Chris Craikbfd1cd62014-09-10 13:04:31 -0700157
Chris Craikb9ce116d2015-08-20 15:14:06 -0700158 layer->state = Layer::State::FailedToCache;
Romain Guydda57022010-07-06 11:39:32 -0700159 return false;
160}
161
162}; // namespace uirenderer
163}; // namespace android