blob: 89c35da3f0889c7e4fbbb71e5994e0b6354c4970 [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 Guy9ace8f52011-07-07 20:50:11 -070071 mSize -= layer->getWidth() * layer->getHeight() * 4;
Romain Guy912a7b32011-07-26 18:57:28 -070072 layer->deleteFbo();
Romain Guy9ace8f52011-07-07 20:50:11 -070073 layer->deleteTexture();
Romain Guydda57022010-07-06 11:39:32 -070074 delete layer;
75 }
76}
77
78void LayerCache::clear() {
Romain Guy8550c4c2010-10-08 15:49:53 -070079 size_t count = mCache.size();
80 for (size_t i = 0; i < count; i++) {
81 deleteLayer(mCache.itemAt(i).mLayer);
82 }
Romain Guydda57022010-07-06 11:39:32 -070083 mCache.clear();
Romain Guydda57022010-07-06 11:39:32 -070084}
85
Romain Guy8550c4c2010-10-08 15:49:53 -070086Layer* LayerCache::get(const uint32_t width, const uint32_t height) {
87 Layer* layer = NULL;
Romain Guyf18fd992010-07-08 11:45:51 -070088
Romain Guy8550c4c2010-10-08 15:49:53 -070089 LayerEntry entry(width, height);
90 ssize_t index = mCache.indexOf(entry);
91
92 if (index >= 0) {
93 entry = mCache.itemAt(index);
94 mCache.removeAt(index);
95
96 layer = entry.mLayer;
Romain Guy9ace8f52011-07-07 20:50:11 -070097 mSize -= layer->getWidth() * layer->getHeight() * 4;
Romain Guy8550c4c2010-10-08 15:49:53 -070098
Romain Guy9ace8f52011-07-07 20:50:11 -070099 LAYER_LOGD("Reusing layer %dx%d", layer->getWidth(), layer->getHeight());
Romain Guyf18fd992010-07-08 11:45:51 -0700100 } else {
Romain Guy8550c4c2010-10-08 15:49:53 -0700101 LAYER_LOGD("Creating new layer %dx%d", entry.mWidth, entry.mHeight);
Romain Guyf18fd992010-07-08 11:45:51 -0700102
Romain Guy8550c4c2010-10-08 15:49:53 -0700103 layer = new Layer(entry.mWidth, entry.mHeight);
Romain Guy9ace8f52011-07-07 20:50:11 -0700104 layer->setBlend(true);
105 layer->setEmpty(true);
106 layer->setFbo(0);
Romain Guyf18fd992010-07-08 11:45:51 -0700107
Romain Guy9ace8f52011-07-07 20:50:11 -0700108 layer->generateTexture();
109 layer->bindTexture();
110 layer->setFilter(GL_NEAREST, GL_NEAREST);
Romain Guye3c26852011-07-25 16:36:01 -0700111 layer->setWrap(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, false);
Romain Guy0bb56672010-10-01 00:25:02 -0700112 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
113
Romain Guyeb993562010-10-05 18:14:38 -0700114#if DEBUG_LAYERS
Romain Guyeea60692011-07-26 20:35:55 -0700115 dump();
Romain Guyeb993562010-10-05 18:14:38 -0700116#endif
Romain Guydda57022010-07-06 11:39:32 -0700117 }
Romain Guyf18fd992010-07-08 11:45:51 -0700118
Romain Guydda57022010-07-06 11:39:32 -0700119 return layer;
120}
121
Romain Guyeea60692011-07-26 20:35:55 -0700122void LayerCache::dump() {
123 size_t size = mCache.size();
124 for (size_t i = 0; i < size; i++) {
125 const LayerEntry& entry = mCache.itemAt(i);
126 LAYER_LOGD(" Layer size %dx%d", entry.mWidth, entry.mHeight);
127 }
128}
129
Romain Guy09b7c912011-02-02 20:28:09 -0800130bool LayerCache::resize(Layer* layer, const uint32_t width, const uint32_t height) {
131 // TODO: We should be smarter and see if we have a texture of the appropriate
132 // size already in the cache, and reuse it instead of creating a new one
133
134 LayerEntry entry(width, height);
Romain Guy9ace8f52011-07-07 20:50:11 -0700135 if (entry.mWidth <= layer->getWidth() && entry.mHeight <= layer->getHeight()) {
Romain Guy09b7c912011-02-02 20:28:09 -0800136 return true;
137 }
138
Romain Guy9ace8f52011-07-07 20:50:11 -0700139 uint32_t oldWidth = layer->getWidth();
140 uint32_t oldHeight = layer->getHeight();
Romain Guy09b7c912011-02-02 20:28:09 -0800141
Romain Guy9ace8f52011-07-07 20:50:11 -0700142 glActiveTexture(GL_TEXTURE0);
143 layer->bindTexture();
144 layer->setSize(entry.mWidth, entry.mHeight);
145 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
Romain Guy09b7c912011-02-02 20:28:09 -0800146
147 if (glGetError() != GL_NO_ERROR) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700148 layer->setSize(oldWidth, oldHeight);
Romain Guy09b7c912011-02-02 20:28:09 -0800149 return false;
150 }
151
Romain Guy09b7c912011-02-02 20:28:09 -0800152 return true;
153}
154
Romain Guy8550c4c2010-10-08 15:49:53 -0700155bool LayerCache::put(Layer* layer) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700156 if (!layer->isCacheable()) return false;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700157
Romain Guy9ace8f52011-07-07 20:50:11 -0700158 const uint32_t size = layer->getWidth() * layer->getHeight() * 4;
Romain Guydda57022010-07-06 11:39:32 -0700159 // Don't even try to cache a layer that's bigger than the cache
160 if (size < mMaxSize) {
Romain Guy8550c4c2010-10-08 15:49:53 -0700161 // TODO: Use an LRU
Romain Guydda57022010-07-06 11:39:32 -0700162 while (mSize + size > mMaxSize) {
Romain Guye9108052010-10-12 18:15:42 -0700163 size_t position = 0;
164#if LAYER_REMOVE_BIGGEST
165 position = mCache.size() - 1;
166#endif
167 Layer* victim = mCache.itemAt(position).mLayer;
168 deleteLayer(victim);
169 mCache.removeAt(position);
Romain Guy8550c4c2010-10-08 15:49:53 -0700170
Romain Guye9108052010-10-12 18:15:42 -0700171 LAYER_LOGD(" Deleting layer %.2fx%.2f", victim->layer.getWidth(),
172 victim->layer.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700173 }
174
Romain Guy8550c4c2010-10-08 15:49:53 -0700175 LayerEntry entry(layer);
176
177 mCache.add(entry);
Romain Guydda57022010-07-06 11:39:32 -0700178 mSize += size;
179
180 return true;
181 }
182 return false;
183}
184
185}; // namespace uirenderer
186}; // namespace android