• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
21 #include <utils/Log.h>
22 
23 #include "Caches.h"
24 #include "Debug.h"
25 #include "LayerCache.h"
26 #include "Properties.h"
27 
28 namespace android {
29 namespace uirenderer {
30 
31 ///////////////////////////////////////////////////////////////////////////////
32 // Constructors/destructor
33 ///////////////////////////////////////////////////////////////////////////////
34 
LayerCache()35 LayerCache::LayerCache(): 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         INIT_LOGD("  Setting layer cache size to %sMB", property);
39         setMaxSize(MB(atof(property)));
40     } else {
41         INIT_LOGD("  Using default layer cache size of %.2fMB", DEFAULT_LAYER_CACHE_SIZE);
42     }
43 }
44 
~LayerCache()45 LayerCache::~LayerCache() {
46     clear();
47 }
48 
49 ///////////////////////////////////////////////////////////////////////////////
50 // Size management
51 ///////////////////////////////////////////////////////////////////////////////
52 
getSize()53 uint32_t LayerCache::getSize() {
54     return mSize;
55 }
56 
getMaxSize()57 uint32_t LayerCache::getMaxSize() {
58     return mMaxSize;
59 }
60 
setMaxSize(uint32_t maxSize)61 void LayerCache::setMaxSize(uint32_t maxSize) {
62     clear();
63     mMaxSize = maxSize;
64 }
65 
66 ///////////////////////////////////////////////////////////////////////////////
67 // Caching
68 ///////////////////////////////////////////////////////////////////////////////
69 
deleteLayer(Layer * layer)70 void LayerCache::deleteLayer(Layer* layer) {
71     if (layer) {
72         LAYER_LOGD("Destroying layer %dx%d, fbo %d", layer->getWidth(), layer->getHeight(),
73                 layer->getFbo());
74         mSize -= layer->getWidth() * layer->getHeight() * 4;
75         Caches::getInstance().resourceCache.decrementRefcount(layer);
76     }
77 }
78 
clear()79 void LayerCache::clear() {
80     size_t count = mCache.size();
81     for (size_t i = 0; i < count; i++) {
82         deleteLayer(mCache.itemAt(i).mLayer);
83     }
84     mCache.clear();
85 }
86 
get(const uint32_t width,const uint32_t height)87 Layer* LayerCache::get(const uint32_t width, const uint32_t height) {
88     Layer* layer = NULL;
89 
90     LayerEntry entry(width, height);
91     ssize_t index = mCache.indexOf(entry);
92 
93     if (index >= 0) {
94         entry = mCache.itemAt(index);
95         mCache.removeAt(index);
96 
97         layer = entry.mLayer;
98         mSize -= layer->getWidth() * layer->getHeight() * 4;
99 
100         LAYER_LOGD("Reusing layer %dx%d", layer->getWidth(), layer->getHeight());
101     } else {
102         LAYER_LOGD("Creating new layer %dx%d", entry.mWidth, entry.mHeight);
103 
104         layer = new Layer(entry.mWidth, entry.mHeight);
105         layer->setBlend(true);
106         layer->setEmpty(true);
107         layer->setFbo(0);
108 
109         layer->generateTexture();
110         layer->bindTexture();
111         layer->setFilter(GL_NEAREST);
112         layer->setWrap(GL_CLAMP_TO_EDGE, false);
113         glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
114 
115 #if DEBUG_LAYERS
116         dump();
117 #endif
118     }
119 
120     return layer;
121 }
122 
dump()123 void LayerCache::dump() {
124     size_t size = mCache.size();
125     for (size_t i = 0; i < size; i++) {
126         const LayerEntry& entry = mCache.itemAt(i);
127         LAYER_LOGD("  Layer size %dx%d", entry.mWidth, entry.mHeight);
128     }
129 }
130 
resize(Layer * layer,const uint32_t width,const uint32_t height)131 bool 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->getWidth() && entry.mHeight <= layer->getHeight()) {
137         return true;
138     }
139 
140     uint32_t oldWidth = layer->getWidth();
141     uint32_t oldHeight = layer->getHeight();
142 
143     Caches::getInstance().activeTexture(0);
144     layer->bindTexture();
145     layer->setSize(entry.mWidth, entry.mHeight);
146     layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
147 
148     if (glGetError() != GL_NO_ERROR) {
149         layer->setSize(oldWidth, oldHeight);
150         return false;
151     }
152 
153     return true;
154 }
155 
put(Layer * layer)156 bool LayerCache::put(Layer* layer) {
157     if (!layer->isCacheable()) return false;
158 
159     const uint32_t size = layer->getWidth() * layer->getHeight() * 4;
160     // Don't even try to cache a layer that's bigger than the cache
161     if (size < mMaxSize) {
162         // TODO: Use an LRU
163         while (mSize + size > mMaxSize) {
164             size_t position = 0;
165 #if LAYER_REMOVE_BIGGEST_FIRST
166             position = mCache.size() - 1;
167 #endif
168             Layer* victim = mCache.itemAt(position).mLayer;
169             deleteLayer(victim);
170             mCache.removeAt(position);
171 
172             LAYER_LOGD("  Deleting layer %.2fx%.2f", victim->layer.getWidth(),
173                     victim->layer.getHeight());
174         }
175 
176         layer->deferredUpdateScheduled = false;
177         layer->renderer = NULL;
178         layer->displayList = NULL;
179 
180         LayerEntry entry(layer);
181 
182         mCache.add(entry);
183         mSize += size;
184 
185         return true;
186     }
187     return false;
188 }
189 
190 }; // namespace uirenderer
191 }; // namespace android
192