blob: b2d795f3516ba12777f2474ae70a6fab216d62d4 [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 Guyc9855a52011-01-21 21:14:15 -080023#include "Debug.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
52uint32_t LayerCache::getSize() {
53 return mSize;
54}
55
56uint32_t LayerCache::getMaxSize() {
57 return mMaxSize;
58}
59
60void LayerCache::setMaxSize(uint32_t maxSize) {
Romain Guy8550c4c2010-10-08 15:49:53 -070061 clear();
Romain Guydda57022010-07-06 11:39:32 -070062 mMaxSize = maxSize;
Romain Guydda57022010-07-06 11:39:32 -070063}
64
65///////////////////////////////////////////////////////////////////////////////
66// Caching
67///////////////////////////////////////////////////////////////////////////////
68
69void LayerCache::deleteLayer(Layer* layer) {
70 if (layer) {
Romain Guy8550c4c2010-10-08 15:49:53 -070071 mSize -= layer->width * layer->height * 4;
Romain Guydda57022010-07-06 11:39:32 -070072 glDeleteTextures(1, &layer->texture);
73 delete layer;
74 }
75}
76
77void LayerCache::clear() {
Romain Guy8550c4c2010-10-08 15:49:53 -070078 size_t count = mCache.size();
79 for (size_t i = 0; i < count; i++) {
80 deleteLayer(mCache.itemAt(i).mLayer);
81 }
Romain Guydda57022010-07-06 11:39:32 -070082 mCache.clear();
Romain Guydda57022010-07-06 11:39:32 -070083}
84
Romain Guy8550c4c2010-10-08 15:49:53 -070085Layer* LayerCache::get(const uint32_t width, const uint32_t height) {
86 Layer* layer = NULL;
Romain Guyf18fd992010-07-08 11:45:51 -070087
Romain Guy8550c4c2010-10-08 15:49:53 -070088 LayerEntry entry(width, height);
89 ssize_t index = mCache.indexOf(entry);
90
91 if (index >= 0) {
92 entry = mCache.itemAt(index);
93 mCache.removeAt(index);
94
95 layer = entry.mLayer;
96 mSize -= layer->width * layer->height * 4;
97
98 LAYER_LOGD("Reusing layer %dx%d", layer->width, layer->height);
Romain Guyf18fd992010-07-08 11:45:51 -070099 } else {
Romain Guy8550c4c2010-10-08 15:49:53 -0700100 LAYER_LOGD("Creating new layer %dx%d", entry.mWidth, entry.mHeight);
Romain Guyf18fd992010-07-08 11:45:51 -0700101
Romain Guy8550c4c2010-10-08 15:49:53 -0700102 layer = new Layer(entry.mWidth, entry.mHeight);
Romain Guyf18fd992010-07-08 11:45:51 -0700103 layer->blend = true;
Romain Guy38c85b92010-09-22 22:48:20 -0700104 layer->empty = true;
Romain Guyeb993562010-10-05 18:14:38 -0700105 layer->fbo = 0;
Romain Guy171c5922011-01-06 10:04:23 -0800106 layer->colorFilter = NULL;
Romain Guyf18fd992010-07-08 11:45:51 -0700107
Romain Guyf18fd992010-07-08 11:45:51 -0700108 glGenTextures(1, &layer->texture);
109 glBindTexture(GL_TEXTURE_2D, layer->texture);
110
Romain Guy0bb56672010-10-01 00:25:02 -0700111 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
112
Romain Guy38c85b92010-09-22 22:48:20 -0700113 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
114 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Romain Guyf18fd992010-07-08 11:45:51 -0700115
116 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
117 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Romain Guyeb993562010-10-05 18:14:38 -0700118
119#if DEBUG_LAYERS
Romain Guy8550c4c2010-10-08 15:49:53 -0700120 size_t size = mCache.size();
121 for (size_t i = 0; i < size; i++) {
122 const LayerEntry& entry = mCache.itemAt(i);
123 LAYER_LOGD(" Layer size %dx%d", entry.mWidth, entry.mHeight);
Romain Guyeb993562010-10-05 18:14:38 -0700124 }
125#endif
Romain Guydda57022010-07-06 11:39:32 -0700126 }
Romain Guyf18fd992010-07-08 11:45:51 -0700127
Romain Guydda57022010-07-06 11:39:32 -0700128 return layer;
129}
130
Romain Guy09b7c912011-02-02 20:28:09 -0800131bool LayerCache::resize(Layer* layer, const uint32_t width, const uint32_t height) {
132 // TODO: We should be smarter and see if we have a texture of the appropriate
133 // size already in the cache, and reuse it instead of creating a new one
134
135 LayerEntry entry(width, height);
136 if (entry.mWidth <= layer->width && entry.mHeight <= layer->height) {
137 return true;
138 }
139
140 glActiveTexture(GL_TEXTURE0);
141 glBindTexture(GL_TEXTURE_2D, layer->texture);
142
143 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, entry.mWidth, entry.mHeight, 0,
144 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
145
146 if (glGetError() != GL_NO_ERROR) {
147 return false;
148 }
149
150 layer->width = entry.mWidth;
151 layer->height = entry.mHeight;
152
153 return true;
154}
155
Romain Guy8550c4c2010-10-08 15:49:53 -0700156bool LayerCache::put(Layer* layer) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700157 if (!layer->isCacheable) return false;
158
Romain Guy8550c4c2010-10-08 15:49:53 -0700159 const uint32_t size = layer->width * layer->height * 4;
Romain Guydda57022010-07-06 11:39:32 -0700160 // Don't even try to cache a layer that's bigger than the cache
161 if (size < mMaxSize) {
Romain Guy8550c4c2010-10-08 15:49:53 -0700162 // TODO: Use an LRU
Romain Guydda57022010-07-06 11:39:32 -0700163 while (mSize + size > mMaxSize) {
Romain Guye9108052010-10-12 18:15:42 -0700164 size_t position = 0;
165#if LAYER_REMOVE_BIGGEST
166 position = mCache.size() - 1;
167#endif
168 Layer* victim = mCache.itemAt(position).mLayer;
169 deleteLayer(victim);
170 mCache.removeAt(position);
Romain Guy8550c4c2010-10-08 15:49:53 -0700171
Romain Guye9108052010-10-12 18:15:42 -0700172 LAYER_LOGD(" Deleting layer %.2fx%.2f", victim->layer.getWidth(),
173 victim->layer.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700174 }
175
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
181 return true;
182 }
183 return false;
184}
185
186}; // namespace uirenderer
187}; // namespace android