blob: ade2ac7a83642c6033409ec20e3e1473dffe7e4b [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
Romain Guydda570202010-07-06 11:39:32 -070017#include <GLES2/gl2.h>
18
Romain Guyf18fd992010-07-08 11:45:51 -070019#include <utils/Log.h>
20
Romain Guya1d3c912011-12-13 14:55:06 -080021#include "Caches.h"
Romain Guydda570202010-07-06 11:39:32 -070022#include "LayerCache.h"
Romain Guyfb8b7632010-08-23 21:05:08 -070023#include "Properties.h"
Romain Guydda570202010-07-06 11:39:32 -070024
25namespace android {
26namespace uirenderer {
27
28///////////////////////////////////////////////////////////////////////////////
29// Constructors/destructor
30///////////////////////////////////////////////////////////////////////////////
31
Romain Guy8550c4c2010-10-08 15:49:53 -070032LayerCache::LayerCache(): mSize(0), mMaxSize(MB(DEFAULT_LAYER_CACHE_SIZE)) {
Romain Guyfb8b7632010-08-23 21:05:08 -070033 char property[PROPERTY_VALUE_MAX];
Chris Craikd41c4d82015-01-05 15:51:13 -080034 if (property_get(PROPERTY_LAYER_CACHE_SIZE, property, nullptr) > 0) {
Romain Guyc9855a52011-01-21 21:14:15 -080035 INIT_LOGD(" Setting layer cache size to %sMB", property);
Romain Guyfb8b7632010-08-23 21:05:08 -070036 setMaxSize(MB(atof(property)));
37 } else {
Romain Guyc9855a52011-01-21 21:14:15 -080038 INIT_LOGD(" Using default layer cache size of %.2fMB", DEFAULT_LAYER_CACHE_SIZE);
Romain Guyfb8b7632010-08-23 21:05:08 -070039 }
40}
41
Romain Guydda570202010-07-06 11:39:32 -070042LayerCache::~LayerCache() {
Romain Guy5f0c6a42010-07-07 13:06:26 -070043 clear();
Romain Guydda570202010-07-06 11:39:32 -070044}
45
46///////////////////////////////////////////////////////////////////////////////
47// Size management
48///////////////////////////////////////////////////////////////////////////////
49
John Reck17035b02014-09-03 07:39:53 -070050size_t LayerCache::getCount() {
51 return mCache.size();
52}
53
Romain Guydda570202010-07-06 11:39:32 -070054uint32_t LayerCache::getSize() {
55 return mSize;
56}
57
58uint32_t LayerCache::getMaxSize() {
59 return mMaxSize;
60}
61
62void LayerCache::setMaxSize(uint32_t maxSize) {
Romain Guy8550c4c2010-10-08 15:49:53 -070063 clear();
Romain Guydda570202010-07-06 11:39:32 -070064 mMaxSize = maxSize;
Romain Guydda570202010-07-06 11:39:32 -070065}
66
67///////////////////////////////////////////////////////////////////////////////
68// Caching
69///////////////////////////////////////////////////////////////////////////////
70
Romain Guye3a9b242013-01-08 11:15:30 -080071int LayerCache::LayerEntry::compare(const LayerCache::LayerEntry& lhs,
72 const LayerCache::LayerEntry& rhs) {
73 int deltaInt = int(lhs.mWidth) - int(rhs.mWidth);
74 if (deltaInt != 0) return deltaInt;
75
76 return int(lhs.mHeight) - int(rhs.mHeight);
77}
78
Romain Guydda570202010-07-06 11:39:32 -070079void LayerCache::deleteLayer(Layer* layer) {
80 if (layer) {
Chris Craik07adacf2014-12-19 10:08:40 -080081 LAYER_LOGD("Destroying layer %dx%d, fbo %d", layer->getWidth(), layer->getHeight(),
82 layer->getFbo());
Romain Guy9ace8f52011-07-07 20:50:11 -070083 mSize -= layer->getWidth() * layer->getHeight() * 4;
Chris Craikbfd1cd62014-09-10 13:04:31 -070084 layer->state = Layer::kState_DeletedFromCache;
Chris Craikd41c4d82015-01-05 15:51:13 -080085 layer->decStrong(nullptr);
Romain Guydda570202010-07-06 11:39:32 -070086 }
87}
88
89void LayerCache::clear() {
Romain Guy8550c4c2010-10-08 15:49:53 -070090 size_t count = mCache.size();
91 for (size_t i = 0; i < count; i++) {
92 deleteLayer(mCache.itemAt(i).mLayer);
93 }
Romain Guydda570202010-07-06 11:39:32 -070094 mCache.clear();
Romain Guydda570202010-07-06 11:39:32 -070095}
96
John Reck3b202512014-06-23 13:13:08 -070097Layer* LayerCache::get(RenderState& renderState, const uint32_t width, const uint32_t height) {
Chris Craikd41c4d82015-01-05 15:51:13 -080098 Layer* layer = nullptr;
Romain Guyf18fd992010-07-08 11:45:51 -070099
Romain Guy8550c4c2010-10-08 15:49:53 -0700100 LayerEntry entry(width, height);
101 ssize_t index = mCache.indexOf(entry);
102
103 if (index >= 0) {
104 entry = mCache.itemAt(index);
105 mCache.removeAt(index);
106
107 layer = entry.mLayer;
Chris Craikbfd1cd62014-09-10 13:04:31 -0700108 layer->state = Layer::kState_RemovedFromCache;
Romain Guy9ace8f52011-07-07 20:50:11 -0700109 mSize -= layer->getWidth() * layer->getHeight() * 4;
Romain Guy8550c4c2010-10-08 15:49:53 -0700110
Chris Craik07adacf2014-12-19 10:08:40 -0800111 LAYER_LOGD("Reusing layer %dx%d", layer->getWidth(), layer->getHeight());
Romain Guyf18fd992010-07-08 11:45:51 -0700112 } else {
Chris Craik07adacf2014-12-19 10:08:40 -0800113 LAYER_LOGD("Creating new layer %dx%d", entry.mWidth, entry.mHeight);
Romain Guyf18fd992010-07-08 11:45:51 -0700114
Chris Craik8a226d22014-09-08 16:40:21 -0700115 layer = new Layer(Layer::kType_DisplayList, renderState, entry.mWidth, entry.mHeight);
Romain Guy9ace8f52011-07-07 20:50:11 -0700116 layer->setBlend(true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700117 layer->generateTexture();
118 layer->bindTexture();
Romain Guy39d252a2011-12-12 18:14:06 -0800119 layer->setFilter(GL_NEAREST);
120 layer->setWrap(GL_CLAMP_TO_EDGE, false);
Romain Guy0bb56672010-10-01 00:25:02 -0700121 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
122
Romain Guyeb993562010-10-05 18:14:38 -0700123#if DEBUG_LAYERS
Romain Guyeea60692011-07-26 20:35:55 -0700124 dump();
Romain Guyeb993562010-10-05 18:14:38 -0700125#endif
Romain Guydda570202010-07-06 11:39:32 -0700126 }
Romain Guyf18fd992010-07-08 11:45:51 -0700127
Romain Guydda570202010-07-06 11:39:32 -0700128 return layer;
129}
130
Romain Guyeea60692011-07-26 20:35:55 -0700131void LayerCache::dump() {
132 size_t size = mCache.size();
133 for (size_t i = 0; i < size; i++) {
134 const LayerEntry& entry = mCache.itemAt(i);
Chris Craik07adacf2014-12-19 10:08:40 -0800135 ALOGD(" Layer size %dx%d", entry.mWidth, entry.mHeight);
Romain Guyeea60692011-07-26 20:35:55 -0700136 }
137}
138
Romain Guy8550c4c2010-10-08 15:49:53 -0700139bool LayerCache::put(Layer* layer) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700140 if (!layer->isCacheable()) return false;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700141
Romain Guy9ace8f52011-07-07 20:50:11 -0700142 const uint32_t size = layer->getWidth() * layer->getHeight() * 4;
Romain Guydda570202010-07-06 11:39:32 -0700143 // Don't even try to cache a layer that's bigger than the cache
144 if (size < mMaxSize) {
Romain Guy8550c4c2010-10-08 15:49:53 -0700145 // TODO: Use an LRU
Romain Guydda570202010-07-06 11:39:32 -0700146 while (mSize + size > mMaxSize) {
Romain Guye9108052010-10-12 18:15:42 -0700147 size_t position = 0;
Romain Guy28d8ff62011-08-22 14:01:34 -0700148#if LAYER_REMOVE_BIGGEST_FIRST
Romain Guye9108052010-10-12 18:15:42 -0700149 position = mCache.size() - 1;
150#endif
151 Layer* victim = mCache.itemAt(position).mLayer;
152 deleteLayer(victim);
153 mCache.removeAt(position);
Romain Guy8550c4c2010-10-08 15:49:53 -0700154
Chris Craik07adacf2014-12-19 10:08:40 -0800155 LAYER_LOGD(" Deleting layer %.2fx%.2f", victim->layer.getWidth(),
156 victim->layer.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700157 }
158
Romain Guye93482f2013-06-17 13:14:51 -0700159 layer->cancelDefer();
Romain Guy5c88fc72012-04-02 17:43:05 -0700160
Romain Guy8550c4c2010-10-08 15:49:53 -0700161 LayerEntry entry(layer);
162
163 mCache.add(entry);
Romain Guydda570202010-07-06 11:39:32 -0700164 mSize += size;
165
Chris Craikbfd1cd62014-09-10 13:04:31 -0700166 layer->state = Layer::kState_InCache;
Romain Guydda570202010-07-06 11:39:32 -0700167 return true;
168 }
Chris Craikbfd1cd62014-09-10 13:04:31 -0700169
170 layer->state = Layer::kState_FailedToCache;
Romain Guydda570202010-07-06 11:39:32 -0700171 return false;
172}
173
174}; // namespace uirenderer
175}; // namespace android