blob: 3216cd2d91d41026d273062de8d9ac5e6e964bbe [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
17#define LOG_TAG "OpenGLRenderer"
18
19#include <GLES2/gl2.h>
20
Romain Guyf18fd992010-07-08 11:45:51 -070021#include <utils/Log.h>
22
Romain Guya1d3c912011-12-13 14:55:06 -080023#include "Caches.h"
Romain Guydda57022010-07-06 11:39:32 -070024#include "LayerCache.h"
Romain Guyfb8b7632010-08-23 21:05:08 -070025#include "Properties.h"
Romain Guydda57022010-07-06 11:39:32 -070026
27namespace android {
28namespace uirenderer {
29
30///////////////////////////////////////////////////////////////////////////////
31// Constructors/destructor
32///////////////////////////////////////////////////////////////////////////////
33
Romain Guy8550c4c2010-10-08 15:49:53 -070034LayerCache::LayerCache(): mSize(0), mMaxSize(MB(DEFAULT_LAYER_CACHE_SIZE)) {
Romain Guyfb8b7632010-08-23 21:05:08 -070035 char property[PROPERTY_VALUE_MAX];
36 if (property_get(PROPERTY_LAYER_CACHE_SIZE, property, NULL) > 0) {
Romain Guyc9855a52011-01-21 21:14:15 -080037 INIT_LOGD(" Setting layer cache size to %sMB", property);
Romain Guyfb8b7632010-08-23 21:05:08 -070038 setMaxSize(MB(atof(property)));
39 } else {
Romain Guyc9855a52011-01-21 21:14:15 -080040 INIT_LOGD(" Using default layer cache size of %.2fMB", DEFAULT_LAYER_CACHE_SIZE);
Romain Guyfb8b7632010-08-23 21:05:08 -070041 }
42}
43
Romain Guydda57022010-07-06 11:39:32 -070044LayerCache::~LayerCache() {
Romain Guy5f0c6a42010-07-07 13:06:26 -070045 clear();
Romain Guydda57022010-07-06 11:39:32 -070046}
47
48///////////////////////////////////////////////////////////////////////////////
49// Size management
50///////////////////////////////////////////////////////////////////////////////
51
John Reck17035b02014-09-03 07:39:53 -070052size_t LayerCache::getCount() {
53 return mCache.size();
54}
55
Romain Guydda57022010-07-06 11:39:32 -070056uint32_t LayerCache::getSize() {
57 return mSize;
58}
59
60uint32_t LayerCache::getMaxSize() {
61 return mMaxSize;
62}
63
64void LayerCache::setMaxSize(uint32_t maxSize) {
Romain Guy8550c4c2010-10-08 15:49:53 -070065 clear();
Romain Guydda57022010-07-06 11:39:32 -070066 mMaxSize = maxSize;
Romain Guydda57022010-07-06 11:39:32 -070067}
68
69///////////////////////////////////////////////////////////////////////////////
70// Caching
71///////////////////////////////////////////////////////////////////////////////
72
Romain Guye3a9b242013-01-08 11:15:30 -080073int 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
Romain Guydda57022010-07-06 11:39:32 -070081void LayerCache::deleteLayer(Layer* layer) {
82 if (layer) {
Andreas Gampe1e196742014-11-10 15:23:43 -080083 if (kDebugLayers) {
84 ALOGD("Destroying layer %dx%d, fbo %d", layer->getWidth(), layer->getHeight(),
85 layer->getFbo());
86 }
Romain Guy9ace8f52011-07-07 20:50:11 -070087 mSize -= layer->getWidth() * layer->getHeight() * 4;
Chris Craikbfd1cd62014-09-10 13:04:31 -070088 layer->state = Layer::kState_DeletedFromCache;
John Reck0e89e2b2014-10-31 14:49:06 -070089 layer->decStrong(0);
Romain Guydda57022010-07-06 11:39:32 -070090 }
91}
92
93void LayerCache::clear() {
Romain Guy8550c4c2010-10-08 15:49:53 -070094 size_t count = mCache.size();
95 for (size_t i = 0; i < count; i++) {
96 deleteLayer(mCache.itemAt(i).mLayer);
97 }
Romain Guydda57022010-07-06 11:39:32 -070098 mCache.clear();
Romain Guydda57022010-07-06 11:39:32 -070099}
100
John Reck3b202512014-06-23 13:13:08 -0700101Layer* LayerCache::get(RenderState& renderState, const uint32_t width, const uint32_t height) {
Romain Guy8550c4c2010-10-08 15:49:53 -0700102 Layer* layer = NULL;
Romain Guyf18fd992010-07-08 11:45:51 -0700103
Romain Guy8550c4c2010-10-08 15:49:53 -0700104 LayerEntry entry(width, height);
105 ssize_t index = mCache.indexOf(entry);
106
107 if (index >= 0) {
108 entry = mCache.itemAt(index);
109 mCache.removeAt(index);
110
111 layer = entry.mLayer;
Chris Craikbfd1cd62014-09-10 13:04:31 -0700112 layer->state = Layer::kState_RemovedFromCache;
Romain Guy9ace8f52011-07-07 20:50:11 -0700113 mSize -= layer->getWidth() * layer->getHeight() * 4;
Romain Guy8550c4c2010-10-08 15:49:53 -0700114
Andreas Gampe1e196742014-11-10 15:23:43 -0800115 if (kDebugLayers) {
116 ALOGD("Reusing layer %dx%d", layer->getWidth(), layer->getHeight());
117 }
Romain Guyf18fd992010-07-08 11:45:51 -0700118 } else {
Andreas Gampe1e196742014-11-10 15:23:43 -0800119 if (kDebugLayers) {
120 ALOGD("Creating new layer %dx%d", entry.mWidth, entry.mHeight);
121 }
Romain Guyf18fd992010-07-08 11:45:51 -0700122
Chris Craik8a226d22014-09-08 16:40:21 -0700123 layer = new Layer(Layer::kType_DisplayList, renderState, entry.mWidth, entry.mHeight);
Romain Guy9ace8f52011-07-07 20:50:11 -0700124 layer->setBlend(true);
125 layer->setEmpty(true);
126 layer->setFbo(0);
Romain Guyf18fd992010-07-08 11:45:51 -0700127
Romain Guy9ace8f52011-07-07 20:50:11 -0700128 layer->generateTexture();
129 layer->bindTexture();
Romain Guy39d252a2011-12-12 18:14:06 -0800130 layer->setFilter(GL_NEAREST);
131 layer->setWrap(GL_CLAMP_TO_EDGE, false);
Romain Guy0bb56672010-10-01 00:25:02 -0700132 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
133
Romain Guyeb993562010-10-05 18:14:38 -0700134#if DEBUG_LAYERS
Romain Guyeea60692011-07-26 20:35:55 -0700135 dump();
Romain Guyeb993562010-10-05 18:14:38 -0700136#endif
Romain Guydda57022010-07-06 11:39:32 -0700137 }
Romain Guyf18fd992010-07-08 11:45:51 -0700138
Romain Guydda57022010-07-06 11:39:32 -0700139 return layer;
140}
141
Romain Guyeea60692011-07-26 20:35:55 -0700142void LayerCache::dump() {
143 size_t size = mCache.size();
144 for (size_t i = 0; i < size; i++) {
145 const LayerEntry& entry = mCache.itemAt(i);
Andreas Gampe1e196742014-11-10 15:23:43 -0800146 if (kDebugLayers) {
147 ALOGD(" Layer size %dx%d", entry.mWidth, entry.mHeight);
148 }
Romain Guyeea60692011-07-26 20:35:55 -0700149 }
150}
151
Romain Guy8550c4c2010-10-08 15:49:53 -0700152bool LayerCache::put(Layer* layer) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700153 if (!layer->isCacheable()) return false;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700154
Romain Guy9ace8f52011-07-07 20:50:11 -0700155 const uint32_t size = layer->getWidth() * layer->getHeight() * 4;
Romain Guydda57022010-07-06 11:39:32 -0700156 // Don't even try to cache a layer that's bigger than the cache
157 if (size < mMaxSize) {
Romain Guy8550c4c2010-10-08 15:49:53 -0700158 // TODO: Use an LRU
Romain Guydda57022010-07-06 11:39:32 -0700159 while (mSize + size > mMaxSize) {
Romain Guye9108052010-10-12 18:15:42 -0700160 size_t position = 0;
Romain Guy28d8ff62011-08-22 14:01:34 -0700161#if LAYER_REMOVE_BIGGEST_FIRST
Romain Guye9108052010-10-12 18:15:42 -0700162 position = mCache.size() - 1;
163#endif
164 Layer* victim = mCache.itemAt(position).mLayer;
165 deleteLayer(victim);
166 mCache.removeAt(position);
Romain Guy8550c4c2010-10-08 15:49:53 -0700167
Andreas Gampe1e196742014-11-10 15:23:43 -0800168 if (kDebugLayers) {
169 ALOGD(" Deleting layer %.2fx%.2f", victim->layer.getWidth(),
170 victim->layer.getHeight());
171 }
Romain Guydda57022010-07-06 11:39:32 -0700172 }
173
Romain Guye93482f2013-06-17 13:14:51 -0700174 layer->cancelDefer();
Romain Guy5c88fc72012-04-02 17:43:05 -0700175
Romain Guy8550c4c2010-10-08 15:49:53 -0700176 LayerEntry entry(layer);
177
178 mCache.add(entry);
Romain Guydda57022010-07-06 11:39:32 -0700179 mSize += size;
180
Chris Craikbfd1cd62014-09-10 13:04:31 -0700181 layer->state = Layer::kState_InCache;
Romain Guydda57022010-07-06 11:39:32 -0700182 return true;
183 }
Chris Craikbfd1cd62014-09-10 13:04:31 -0700184
185 layer->state = Layer::kState_FailedToCache;
Romain Guydda57022010-07-06 11:39:32 -0700186 return false;
187}
188
189}; // namespace uirenderer
190}; // namespace android