blob: a20477801211f840543e9dedac6614ad601e7ed3 [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 Guydda57022010-07-06 11:39:32 -070023#include "LayerCache.h"
Romain Guyfb8b7632010-08-23 21:05:08 -070024#include "Properties.h"
Romain Guydda57022010-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 Guydda57022010-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 Guydda57022010-07-06 11:39:32 -070048}
49
50LayerCache::~LayerCache() {
Romain Guy5f0c6a42010-07-07 13:06:26 -070051 clear();
Romain Guydda57022010-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
90 glDeleteFramebuffers(1, &layer->fbo);
91 glDeleteTextures(1, &layer->texture);
92 delete layer;
93 }
94}
95
96void LayerCache::clear() {
97 mCache.setOnEntryRemovedListener(this);
98 mCache.clear();
99 mCache.setOnEntryRemovedListener(NULL);
100}
101
Romain Guyf18fd992010-07-08 11:45:51 -0700102Layer* LayerCache::get(LayerSize& size, GLuint previousFbo) {
Romain Guydda57022010-07-06 11:39:32 -0700103 Layer* layer = mCache.remove(size);
104 if (layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700105 LAYER_LOGD("Reusing layer");
106
Romain Guydda57022010-07-06 11:39:32 -0700107 mSize -= layer->layer.getWidth() * layer->layer.getHeight() * 4;
Romain Guyf18fd992010-07-08 11:45:51 -0700108 } else {
109 LAYER_LOGD("Creating new layer");
110
111 layer = new Layer;
112 layer->blend = true;
113
114 // Generate the FBO and attach the texture
115 glGenFramebuffers(1, &layer->fbo);
116 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
117
118 // Generate the texture in which the FBO will draw
119 glGenTextures(1, &layer->texture);
120 glBindTexture(GL_TEXTURE_2D, layer->texture);
121
122 // The FBO will not be scaled, so we can use lower quality filtering
123 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
124 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
125
126 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
127 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
128
129 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size.width, size.height, 0,
130 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
Romain Guyf18fd992010-07-08 11:45:51 -0700131
132 // Bind texture to FBO
133 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
134 layer->texture, 0);
135
136 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
137 if (status != GL_FRAMEBUFFER_COMPLETE) {
138 LOGE("Framebuffer incomplete (GL error code 0x%x)", status);
139
140 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
141
142 glDeleteFramebuffers(1, &layer->fbo);
143 glDeleteTextures(1, &layer->texture);
144 delete layer;
145
146 return NULL;
147 }
Romain Guydda57022010-07-06 11:39:32 -0700148 }
Romain Guyf18fd992010-07-08 11:45:51 -0700149
Romain Guydda57022010-07-06 11:39:32 -0700150 return layer;
151}
152
153bool LayerCache::put(LayerSize& layerSize, Layer* layer) {
154 const uint32_t size = layerSize.width * layerSize.height * 4;
155 // Don't even try to cache a layer that's bigger than the cache
156 if (size < mMaxSize) {
157 while (mSize + size > mMaxSize) {
158 Layer* oldest = mCache.removeOldest();
159 deleteLayer(oldest);
160 }
161
Romain Guy6c818932010-07-07 15:15:32 -0700162 layerSize.id = mIdGenerator++;
Romain Guydda57022010-07-06 11:39:32 -0700163 mCache.put(layerSize, layer);
164 mSize += size;
165
166 return true;
167 }
168 return false;
169}
170
171}; // namespace uirenderer
172}; // namespace android