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