blob: eea707edb457f93859d9ac28d1a33a0f9dd35b1c [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 Guyc9855a52011-01-21 21:14:15 -080024#include "Debug.h"
Romain Guydda57022010-07-06 11:39:32 -070025#include "LayerCache.h"
Romain Guyfb8b7632010-08-23 21:05:08 -070026#include "Properties.h"
Romain Guydda57022010-07-06 11:39:32 -070027
28namespace android {
29namespace uirenderer {
30
31///////////////////////////////////////////////////////////////////////////////
32// Constructors/destructor
33///////////////////////////////////////////////////////////////////////////////
34
Romain Guy8550c4c2010-10-08 15:49:53 -070035LayerCache::LayerCache(): mSize(0), mMaxSize(MB(DEFAULT_LAYER_CACHE_SIZE)) {
Romain Guyfb8b7632010-08-23 21:05:08 -070036 char property[PROPERTY_VALUE_MAX];
37 if (property_get(PROPERTY_LAYER_CACHE_SIZE, property, NULL) > 0) {
Romain Guyc9855a52011-01-21 21:14:15 -080038 INIT_LOGD(" Setting layer cache size to %sMB", property);
Romain Guyfb8b7632010-08-23 21:05:08 -070039 setMaxSize(MB(atof(property)));
40 } else {
Romain Guyc9855a52011-01-21 21:14:15 -080041 INIT_LOGD(" Using default layer cache size of %.2fMB", DEFAULT_LAYER_CACHE_SIZE);
Romain Guyfb8b7632010-08-23 21:05:08 -070042 }
43}
44
Romain Guydda57022010-07-06 11:39:32 -070045LayerCache::~LayerCache() {
Romain Guy5f0c6a42010-07-07 13:06:26 -070046 clear();
Romain Guydda57022010-07-06 11:39:32 -070047}
48
49///////////////////////////////////////////////////////////////////////////////
50// Size management
51///////////////////////////////////////////////////////////////////////////////
52
53uint32_t LayerCache::getSize() {
54 return mSize;
55}
56
57uint32_t LayerCache::getMaxSize() {
58 return mMaxSize;
59}
60
61void LayerCache::setMaxSize(uint32_t maxSize) {
Romain Guy8550c4c2010-10-08 15:49:53 -070062 clear();
Romain Guydda57022010-07-06 11:39:32 -070063 mMaxSize = maxSize;
Romain Guydda57022010-07-06 11:39:32 -070064}
65
66///////////////////////////////////////////////////////////////////////////////
67// Caching
68///////////////////////////////////////////////////////////////////////////////
69
70void LayerCache::deleteLayer(Layer* layer) {
71 if (layer) {
Romain Guy5c88fc72012-04-02 17:43:05 -070072 GLuint fbo = layer->getFbo();
73 LAYER_LOGD("Destroying layer %dx%d, fbo %d", layer->getWidth(), layer->getHeight(), fbo);
74
Romain Guy9ace8f52011-07-07 20:50:11 -070075 mSize -= layer->getWidth() * layer->getHeight() * 4;
Romain Guy5c88fc72012-04-02 17:43:05 -070076
77 if (fbo) Caches::getInstance().fboCache.put(fbo);
Romain Guy9ace8f52011-07-07 20:50:11 -070078 layer->deleteTexture();
Romain Guy5c88fc72012-04-02 17:43:05 -070079
Romain Guydda57022010-07-06 11:39:32 -070080 delete layer;
81 }
82}
83
84void LayerCache::clear() {
Romain Guy8550c4c2010-10-08 15:49:53 -070085 size_t count = mCache.size();
86 for (size_t i = 0; i < count; i++) {
87 deleteLayer(mCache.itemAt(i).mLayer);
88 }
Romain Guydda57022010-07-06 11:39:32 -070089 mCache.clear();
Romain Guydda57022010-07-06 11:39:32 -070090}
91
Romain Guy8550c4c2010-10-08 15:49:53 -070092Layer* LayerCache::get(const uint32_t width, const uint32_t height) {
93 Layer* layer = NULL;
Romain Guyf18fd992010-07-08 11:45:51 -070094
Romain Guy8550c4c2010-10-08 15:49:53 -070095 LayerEntry entry(width, height);
96 ssize_t index = mCache.indexOf(entry);
97
98 if (index >= 0) {
99 entry = mCache.itemAt(index);
100 mCache.removeAt(index);
101
102 layer = entry.mLayer;
Romain Guy9ace8f52011-07-07 20:50:11 -0700103 mSize -= layer->getWidth() * layer->getHeight() * 4;
Romain Guy8550c4c2010-10-08 15:49:53 -0700104
Romain Guy9ace8f52011-07-07 20:50:11 -0700105 LAYER_LOGD("Reusing layer %dx%d", layer->getWidth(), layer->getHeight());
Romain Guyf18fd992010-07-08 11:45:51 -0700106 } else {
Romain Guy8550c4c2010-10-08 15:49:53 -0700107 LAYER_LOGD("Creating new layer %dx%d", entry.mWidth, entry.mHeight);
Romain Guyf18fd992010-07-08 11:45:51 -0700108
Romain Guy8550c4c2010-10-08 15:49:53 -0700109 layer = new Layer(entry.mWidth, entry.mHeight);
Romain Guy9ace8f52011-07-07 20:50:11 -0700110 layer->setBlend(true);
111 layer->setEmpty(true);
112 layer->setFbo(0);
Romain Guyf18fd992010-07-08 11:45:51 -0700113
Romain Guy9ace8f52011-07-07 20:50:11 -0700114 layer->generateTexture();
115 layer->bindTexture();
Romain Guy39d252a2011-12-12 18:14:06 -0800116 layer->setFilter(GL_NEAREST);
117 layer->setWrap(GL_CLAMP_TO_EDGE, false);
Romain Guy0bb56672010-10-01 00:25:02 -0700118 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
119
Romain Guyeb993562010-10-05 18:14:38 -0700120#if DEBUG_LAYERS
Romain Guyeea60692011-07-26 20:35:55 -0700121 dump();
Romain Guyeb993562010-10-05 18:14:38 -0700122#endif
Romain Guydda57022010-07-06 11:39:32 -0700123 }
Romain Guyf18fd992010-07-08 11:45:51 -0700124
Romain Guydda57022010-07-06 11:39:32 -0700125 return layer;
126}
127
Romain Guyeea60692011-07-26 20:35:55 -0700128void LayerCache::dump() {
129 size_t size = mCache.size();
130 for (size_t i = 0; i < size; i++) {
131 const LayerEntry& entry = mCache.itemAt(i);
132 LAYER_LOGD(" Layer size %dx%d", entry.mWidth, entry.mHeight);
133 }
134}
135
Romain Guy09b7c912011-02-02 20:28:09 -0800136bool LayerCache::resize(Layer* layer, const uint32_t width, const uint32_t height) {
137 // TODO: We should be smarter and see if we have a texture of the appropriate
138 // size already in the cache, and reuse it instead of creating a new one
139
140 LayerEntry entry(width, height);
Romain Guy9ace8f52011-07-07 20:50:11 -0700141 if (entry.mWidth <= layer->getWidth() && entry.mHeight <= layer->getHeight()) {
Romain Guy09b7c912011-02-02 20:28:09 -0800142 return true;
143 }
144
Romain Guy9ace8f52011-07-07 20:50:11 -0700145 uint32_t oldWidth = layer->getWidth();
146 uint32_t oldHeight = layer->getHeight();
Romain Guy09b7c912011-02-02 20:28:09 -0800147
Romain Guya1d3c912011-12-13 14:55:06 -0800148 Caches::getInstance().activeTexture(0);
Romain Guy9ace8f52011-07-07 20:50:11 -0700149 layer->bindTexture();
150 layer->setSize(entry.mWidth, entry.mHeight);
151 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
Romain Guy09b7c912011-02-02 20:28:09 -0800152
153 if (glGetError() != GL_NO_ERROR) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700154 layer->setSize(oldWidth, oldHeight);
Romain Guy09b7c912011-02-02 20:28:09 -0800155 return false;
156 }
157
Romain Guy09b7c912011-02-02 20:28:09 -0800158 return true;
159}
160
Romain Guy8550c4c2010-10-08 15:49:53 -0700161bool LayerCache::put(Layer* layer) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700162 if (!layer->isCacheable()) return false;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700163
Romain Guy9ace8f52011-07-07 20:50:11 -0700164 const uint32_t size = layer->getWidth() * layer->getHeight() * 4;
Romain Guydda57022010-07-06 11:39:32 -0700165 // Don't even try to cache a layer that's bigger than the cache
166 if (size < mMaxSize) {
Romain Guy8550c4c2010-10-08 15:49:53 -0700167 // TODO: Use an LRU
Romain Guydda57022010-07-06 11:39:32 -0700168 while (mSize + size > mMaxSize) {
Romain Guye9108052010-10-12 18:15:42 -0700169 size_t position = 0;
Romain Guy28d8ff62011-08-22 14:01:34 -0700170#if LAYER_REMOVE_BIGGEST_FIRST
Romain Guye9108052010-10-12 18:15:42 -0700171 position = mCache.size() - 1;
172#endif
173 Layer* victim = mCache.itemAt(position).mLayer;
174 deleteLayer(victim);
175 mCache.removeAt(position);
Romain Guy8550c4c2010-10-08 15:49:53 -0700176
Romain Guye9108052010-10-12 18:15:42 -0700177 LAYER_LOGD(" Deleting layer %.2fx%.2f", victim->layer.getWidth(),
178 victim->layer.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700179 }
180
Romain Guy5c88fc72012-04-02 17:43:05 -0700181 layer->deferredUpdateScheduled = false;
182 layer->renderer = NULL;
183 layer->displayList = NULL;
184
Romain Guy8550c4c2010-10-08 15:49:53 -0700185 LayerEntry entry(layer);
186
187 mCache.add(entry);
Romain Guydda57022010-07-06 11:39:32 -0700188 mSize += size;
189
190 return true;
191 }
192 return false;
193}
194
195}; // namespace uirenderer
196}; // namespace android