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 "LayerCache.h"
25 #include "Properties.h"
26 
27 namespace android {
28 namespace uirenderer {
29 
30 ///////////////////////////////////////////////////////////////////////////////
31 // Constructors/destructor
32 ///////////////////////////////////////////////////////////////////////////////
33 
LayerCache()34 LayerCache::LayerCache(): mSize(0), mMaxSize(MB(DEFAULT_LAYER_CACHE_SIZE)) {
35     char property[PROPERTY_VALUE_MAX];
36     if (property_get(PROPERTY_LAYER_CACHE_SIZE, property, nullptr) > 0) {
37         INIT_LOGD("  Setting layer cache size to %sMB", property);
38         setMaxSize(MB(atof(property)));
39     } else {
40         INIT_LOGD("  Using default layer cache size of %.2fMB", DEFAULT_LAYER_CACHE_SIZE);
41     }
42 }
43 
~LayerCache()44 LayerCache::~LayerCache() {
45     clear();
46 }
47 
48 ///////////////////////////////////////////////////////////////////////////////
49 // Size management
50 ///////////////////////////////////////////////////////////////////////////////
51 
getCount()52 size_t LayerCache::getCount() {
53     return mCache.size();
54 }
55 
getSize()56 uint32_t LayerCache::getSize() {
57     return mSize;
58 }
59 
getMaxSize()60 uint32_t LayerCache::getMaxSize() {
61     return mMaxSize;
62 }
63 
setMaxSize(uint32_t maxSize)64 void LayerCache::setMaxSize(uint32_t maxSize) {
65     clear();
66     mMaxSize = maxSize;
67 }
68 
69 ///////////////////////////////////////////////////////////////////////////////
70 // Caching
71 ///////////////////////////////////////////////////////////////////////////////
72 
compare(const LayerCache::LayerEntry & lhs,const LayerCache::LayerEntry & rhs)73 int LayerCache::LayerEntry::compare(const LayerCache::LayerEntry& lhs,
74         const LayerCache::LayerEntry& rhs) {
75     int deltaInt = int(lhs.mWidth) - int(rhs.mWidth);
76     if (deltaInt != 0) return deltaInt;
77 
78     return int(lhs.mHeight) - int(rhs.mHeight);
79 }
80 
deleteLayer(Layer * layer)81 void LayerCache::deleteLayer(Layer* layer) {
82     if (layer) {
83         LAYER_LOGD("Destroying layer %dx%d, fbo %d", layer->getWidth(), layer->getHeight(),
84                 layer->getFbo());
85         mSize -= layer->getWidth() * layer->getHeight() * 4;
86         layer->state = Layer::kState_DeletedFromCache;
87         layer->decStrong(nullptr);
88     }
89 }
90 
clear()91 void LayerCache::clear() {
92     size_t count = mCache.size();
93     for (size_t i = 0; i < count; i++) {
94         deleteLayer(mCache.itemAt(i).mLayer);
95     }
96     mCache.clear();
97 }
98 
get(RenderState & renderState,const uint32_t width,const uint32_t height)99 Layer* LayerCache::get(RenderState& renderState, const uint32_t width, const uint32_t height) {
100     Layer* layer = nullptr;
101 
102     LayerEntry entry(width, height);
103     ssize_t index = mCache.indexOf(entry);
104 
105     if (index >= 0) {
106         entry = mCache.itemAt(index);
107         mCache.removeAt(index);
108 
109         layer = entry.mLayer;
110         layer->state = Layer::kState_RemovedFromCache;
111         mSize -= layer->getWidth() * layer->getHeight() * 4;
112 
113         LAYER_LOGD("Reusing layer %dx%d", layer->getWidth(), layer->getHeight());
114     } else {
115         LAYER_LOGD("Creating new layer %dx%d", entry.mWidth, entry.mHeight);
116 
117         layer = new Layer(Layer::kType_DisplayList, renderState, entry.mWidth, entry.mHeight);
118         layer->setBlend(true);
119         layer->generateTexture();
120         layer->bindTexture();
121         layer->setFilter(GL_NEAREST);
122         layer->setWrap(GL_CLAMP_TO_EDGE, false);
123         glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
124 
125 #if DEBUG_LAYERS
126         dump();
127 #endif
128     }
129 
130     return layer;
131 }
132 
dump()133 void LayerCache::dump() {
134     size_t size = mCache.size();
135     for (size_t i = 0; i < size; i++) {
136         const LayerEntry& entry = mCache.itemAt(i);
137         ALOGD("  Layer size %dx%d", entry.mWidth, entry.mHeight);
138     }
139 }
140 
put(Layer * layer)141 bool LayerCache::put(Layer* layer) {
142     if (!layer->isCacheable()) return false;
143 
144     const uint32_t size = layer->getWidth() * layer->getHeight() * 4;
145     // Don't even try to cache a layer that's bigger than the cache
146     if (size < mMaxSize) {
147         // TODO: Use an LRU
148         while (mSize + size > mMaxSize) {
149             size_t position = 0;
150 #if LAYER_REMOVE_BIGGEST_FIRST
151             position = mCache.size() - 1;
152 #endif
153             Layer* victim = mCache.itemAt(position).mLayer;
154             deleteLayer(victim);
155             mCache.removeAt(position);
156 
157             LAYER_LOGD("  Deleting layer %.2fx%.2f", victim->layer.getWidth(),
158                     victim->layer.getHeight());
159         }
160 
161         layer->cancelDefer();
162 
163         LayerEntry entry(layer);
164 
165         mCache.add(entry);
166         mSize += size;
167 
168         layer->state = Layer::kState_InCache;
169         return true;
170     }
171 
172     layer->state = Layer::kState_FailedToCache;
173     return false;
174 }
175 
176 }; // namespace uirenderer
177 }; // namespace android
178