blob: 1a18766e1d2e2f79f3a393ae8c1633148fdb547e [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 Guyfb8b7632010-08-23 21:05:08 -070033LayerCache::LayerCache():
34 mCache(GenerationCache<LayerSize, Layer*>::kUnlimitedCapacity),
35 mIdGenerator(1), mSize(0), mMaxSize(MB(DEFAULT_LAYER_CACHE_SIZE)) {
36 char property[PROPERTY_VALUE_MAX];
37 if (property_get(PROPERTY_LAYER_CACHE_SIZE, property, NULL) > 0) {
38 LOGD(" Setting layer cache size to %sMB", property);
39 setMaxSize(MB(atof(property)));
40 } else {
41 LOGD(" Using default layer cache size of %.2fMB", DEFAULT_LAYER_CACHE_SIZE);
42 }
43}
44
Romain Guydda570202010-07-06 11:39:32 -070045LayerCache::LayerCache(uint32_t maxByteSize):
Romain Guy6c818932010-07-07 15:15:32 -070046 mCache(GenerationCache<LayerSize, Layer*>::kUnlimitedCapacity),
47 mIdGenerator(1), mSize(0), mMaxSize(maxByteSize) {
Romain Guydda570202010-07-06 11:39:32 -070048}
49
50LayerCache::~LayerCache() {
Romain Guy5f0c6a42010-07-07 13:06:26 -070051 clear();
Romain Guydda570202010-07-06 11:39:32 -070052}
53
54///////////////////////////////////////////////////////////////////////////////
55// Size management
56///////////////////////////////////////////////////////////////////////////////
57
58uint32_t LayerCache::getSize() {
59 return mSize;
60}
61
62uint32_t LayerCache::getMaxSize() {
63 return mMaxSize;
64}
65
66void LayerCache::setMaxSize(uint32_t maxSize) {
67 mMaxSize = maxSize;
68 while (mSize > mMaxSize) {
69 Layer* oldest = mCache.removeOldest();
70 deleteLayer(oldest);
71 }
72}
73
74///////////////////////////////////////////////////////////////////////////////
75// Callbacks
76///////////////////////////////////////////////////////////////////////////////
77
78void LayerCache::operator()(LayerSize& size, Layer*& layer) {
79 deleteLayer(layer);
80}
81
82///////////////////////////////////////////////////////////////////////////////
83// Caching
84///////////////////////////////////////////////////////////////////////////////
85
86void LayerCache::deleteLayer(Layer* layer) {
87 if (layer) {
88 mSize -= layer->layer.getWidth() * layer->layer.getHeight() * 4;
89
Romain Guydda570202010-07-06 11:39:32 -070090 glDeleteTextures(1, &layer->texture);
91 delete layer;
92 }
93}
94
95void LayerCache::clear() {
96 mCache.setOnEntryRemovedListener(this);
97 mCache.clear();
98 mCache.setOnEntryRemovedListener(NULL);
99}
100
Romain Guyf607bdc2010-09-10 19:20:06 -0700101Layer* LayerCache::get(LayerSize& size) {
Romain Guydda570202010-07-06 11:39:32 -0700102 Layer* layer = mCache.remove(size);
103 if (layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700104 LAYER_LOGD("Reusing layer");
105
Romain Guydda570202010-07-06 11:39:32 -0700106 mSize -= layer->layer.getWidth() * layer->layer.getHeight() * 4;
Romain Guyf18fd992010-07-08 11:45:51 -0700107 } else {
108 LAYER_LOGD("Creating new layer");
109
110 layer = new Layer;
111 layer->blend = true;
112
Romain Guyf18fd992010-07-08 11:45:51 -0700113 // Generate the texture in which the FBO will draw
114 glGenTextures(1, &layer->texture);
115 glBindTexture(GL_TEXTURE_2D, layer->texture);
116
117 // The FBO will not be scaled, so we can use lower quality filtering
118 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
119 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
120
121 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
122 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
123
124 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size.width, size.height, 0,
125 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
Romain Guydda570202010-07-06 11:39:32 -0700126 }
Romain Guyf18fd992010-07-08 11:45:51 -0700127
Romain Guydda570202010-07-06 11:39:32 -0700128 return layer;
129}
130
131bool LayerCache::put(LayerSize& layerSize, Layer* layer) {
132 const uint32_t size = layerSize.width * layerSize.height * 4;
133 // Don't even try to cache a layer that's bigger than the cache
134 if (size < mMaxSize) {
135 while (mSize + size > mMaxSize) {
136 Layer* oldest = mCache.removeOldest();
137 deleteLayer(oldest);
138 }
139
Romain Guy6c818932010-07-07 15:15:32 -0700140 layerSize.id = mIdGenerator++;
Romain Guydda570202010-07-06 11:39:32 -0700141 mCache.put(layerSize, layer);
142 mSize += size;
143
144 return true;
145 }
146 return false;
147}
148
149}; // namespace uirenderer
150}; // namespace android