blob: ec186a481e7c04976395f612ae1d89c410ed8eda [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
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 Guydda570202010-07-06 11:39:32 -070023#include "LayerCache.h"
Romain Guyfb8b7632010-08-23 21:05:08 -070024#include "Properties.h"
Romain Guydda570202010-07-06 11:39:32 -070025
26namespace android {
27namespace uirenderer {
28
29///////////////////////////////////////////////////////////////////////////////
30// Constructors/destructor
31///////////////////////////////////////////////////////////////////////////////
32
Romain Guy8550c4c2010-10-08 15:49:53 -070033LayerCache::LayerCache(): mSize(0), mMaxSize(MB(DEFAULT_LAYER_CACHE_SIZE)) {
Romain Guyfb8b7632010-08-23 21:05:08 -070034 char property[PROPERTY_VALUE_MAX];
35 if (property_get(PROPERTY_LAYER_CACHE_SIZE, property, NULL) > 0) {
36 LOGD(" Setting layer cache size to %sMB", property);
37 setMaxSize(MB(atof(property)));
38 } else {
39 LOGD(" Using default layer cache size of %.2fMB", DEFAULT_LAYER_CACHE_SIZE);
40 }
41}
42
Romain Guydda570202010-07-06 11:39:32 -070043LayerCache::~LayerCache() {
Romain Guy5f0c6a42010-07-07 13:06:26 -070044 clear();
Romain Guydda570202010-07-06 11:39:32 -070045}
46
47///////////////////////////////////////////////////////////////////////////////
48// Size management
49///////////////////////////////////////////////////////////////////////////////
50
51uint32_t LayerCache::getSize() {
52 return mSize;
53}
54
55uint32_t LayerCache::getMaxSize() {
56 return mMaxSize;
57}
58
59void LayerCache::setMaxSize(uint32_t maxSize) {
Romain Guy8550c4c2010-10-08 15:49:53 -070060 clear();
Romain Guydda570202010-07-06 11:39:32 -070061 mMaxSize = maxSize;
Romain Guydda570202010-07-06 11:39:32 -070062}
63
64///////////////////////////////////////////////////////////////////////////////
65// Caching
66///////////////////////////////////////////////////////////////////////////////
67
68void LayerCache::deleteLayer(Layer* layer) {
69 if (layer) {
Romain Guy8550c4c2010-10-08 15:49:53 -070070 mSize -= layer->width * layer->height * 4;
Romain Guydda570202010-07-06 11:39:32 -070071 glDeleteTextures(1, &layer->texture);
72 delete layer;
73 }
74}
75
76void LayerCache::clear() {
Romain Guy8550c4c2010-10-08 15:49:53 -070077 size_t count = mCache.size();
78 for (size_t i = 0; i < count; i++) {
79 deleteLayer(mCache.itemAt(i).mLayer);
80 }
Romain Guydda570202010-07-06 11:39:32 -070081 mCache.clear();
Romain Guydda570202010-07-06 11:39:32 -070082}
83
Romain Guy8550c4c2010-10-08 15:49:53 -070084Layer* LayerCache::get(const uint32_t width, const uint32_t height) {
85 Layer* layer = NULL;
Romain Guyf18fd992010-07-08 11:45:51 -070086
Romain Guy8550c4c2010-10-08 15:49:53 -070087 LayerEntry entry(width, height);
88 ssize_t index = mCache.indexOf(entry);
89
90 if (index >= 0) {
91 entry = mCache.itemAt(index);
92 mCache.removeAt(index);
93
94 layer = entry.mLayer;
95 mSize -= layer->width * layer->height * 4;
96
97 LAYER_LOGD("Reusing layer %dx%d", layer->width, layer->height);
Romain Guyf18fd992010-07-08 11:45:51 -070098 } else {
Romain Guy8550c4c2010-10-08 15:49:53 -070099 LAYER_LOGD("Creating new layer %dx%d", entry.mWidth, entry.mHeight);
Romain Guyf18fd992010-07-08 11:45:51 -0700100
Romain Guy8550c4c2010-10-08 15:49:53 -0700101 layer = new Layer(entry.mWidth, entry.mHeight);
Romain Guyf18fd992010-07-08 11:45:51 -0700102 layer->blend = true;
Romain Guy38c85b92010-09-22 22:48:20 -0700103 layer->empty = true;
Romain Guyeb993562010-10-05 18:14:38 -0700104 layer->fbo = 0;
Romain Guy171c5922011-01-06 10:04:23 -0800105 layer->colorFilter = NULL;
Romain Guyf18fd992010-07-08 11:45:51 -0700106
Romain Guyf18fd992010-07-08 11:45:51 -0700107 glGenTextures(1, &layer->texture);
108 glBindTexture(GL_TEXTURE_2D, layer->texture);
109
Romain Guy0bb56672010-10-01 00:25:02 -0700110 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
111
Romain Guy38c85b92010-09-22 22:48:20 -0700112 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
113 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Romain Guyf18fd992010-07-08 11:45:51 -0700114
115 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
116 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Romain Guyeb993562010-10-05 18:14:38 -0700117
118#if DEBUG_LAYERS
Romain Guy8550c4c2010-10-08 15:49:53 -0700119 size_t size = mCache.size();
120 for (size_t i = 0; i < size; i++) {
121 const LayerEntry& entry = mCache.itemAt(i);
122 LAYER_LOGD(" Layer size %dx%d", entry.mWidth, entry.mHeight);
Romain Guyeb993562010-10-05 18:14:38 -0700123 }
124#endif
Romain Guydda570202010-07-06 11:39:32 -0700125 }
Romain Guyf18fd992010-07-08 11:45:51 -0700126
Romain Guydda570202010-07-06 11:39:32 -0700127 return layer;
128}
129
Romain Guy8550c4c2010-10-08 15:49:53 -0700130bool LayerCache::put(Layer* layer) {
131 const uint32_t size = layer->width * layer->height * 4;
Romain Guydda570202010-07-06 11:39:32 -0700132 // Don't even try to cache a layer that's bigger than the cache
133 if (size < mMaxSize) {
Romain Guy8550c4c2010-10-08 15:49:53 -0700134 // TODO: Use an LRU
Romain Guydda570202010-07-06 11:39:32 -0700135 while (mSize + size > mMaxSize) {
Romain Guye9108052010-10-12 18:15:42 -0700136 size_t position = 0;
137#if LAYER_REMOVE_BIGGEST
138 position = mCache.size() - 1;
139#endif
140 Layer* victim = mCache.itemAt(position).mLayer;
141 deleteLayer(victim);
142 mCache.removeAt(position);
Romain Guy8550c4c2010-10-08 15:49:53 -0700143
Romain Guye9108052010-10-12 18:15:42 -0700144 LAYER_LOGD(" Deleting layer %.2fx%.2f", victim->layer.getWidth(),
145 victim->layer.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700146 }
147
Romain Guy8550c4c2010-10-08 15:49:53 -0700148 LayerEntry entry(layer);
149
150 mCache.add(entry);
Romain Guydda570202010-07-06 11:39:32 -0700151 mSize += size;
152
153 return true;
154 }
155 return false;
156}
157
158}; // namespace uirenderer
159}; // namespace android