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