LayerCache.h revision e5df231434357424cea8d2b8d0cdf31253a98110
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#ifndef ANDROID_HWUI_LAYER_CACHE_H
18#define ANDROID_HWUI_LAYER_CACHE_H
19
20#include "Debug.h"
21#include "Layer.h"
22#include "utils/SortedList.h"
23
24namespace android {
25namespace uirenderer {
26
27///////////////////////////////////////////////////////////////////////////////
28// Defines
29///////////////////////////////////////////////////////////////////////////////
30
31// Indicates whether to remove the biggest layers first, or the smaller ones
32#define LAYER_REMOVE_BIGGEST 0
33// Textures used by layers must have dimensions multiples of this number
34#define LAYER_SIZE 64
35
36// Debug
37#if DEBUG_LAYERS
38    #define LAYER_LOGD(...) LOGD(__VA_ARGS__)
39#else
40    #define LAYER_LOGD(...)
41#endif
42
43///////////////////////////////////////////////////////////////////////////////
44// Cache
45///////////////////////////////////////////////////////////////////////////////
46
47class LayerCache {
48public:
49    LayerCache();
50    ~LayerCache();
51
52    /**
53     * Returns a layer large enough for the specified dimensions. If no suitable
54     * layer can be found, a new one is created and returned. If creating a new
55     * layer fails, NULL is returned.
56     *
57     * When a layer is obtained from the cache, it is removed and the total
58     * size of the cache goes down.
59     *
60     * @param width The desired width of the layer
61     * @param width The desired height of the layer
62     */
63    Layer* get(const uint32_t width, const uint32_t height);
64
65    /**
66     * Adds the layer to the cache. The layer will not be added if there is
67     * not enough space available. Adding a layer can cause other layers to
68     * be removed from the cache.
69     *
70     * @param layer The layer to add to the cache
71     *
72     * @return True if the layer was added, false otherwise.
73     */
74    bool put(Layer* layer);
75    /**
76     * Clears the cache. This causes all layers to be deleted.
77     */
78    void clear();
79    /**
80     * Resize the specified layer if needed.
81     *
82     * @param layer The layer to resize
83     * @param width The new width of the layer
84     * @param height The new height of the layer
85     *
86     * @return True if the layer was resized or nothing happened, false if
87     *         a failure occurred during the resizing operation
88     */
89    bool resize(Layer* layer, const uint32_t width, const uint32_t height);
90
91    /**
92     * Sets the maximum size of the cache in bytes.
93     */
94    void setMaxSize(uint32_t maxSize);
95    /**
96     * Returns the maximum size of the cache in bytes.
97     */
98    uint32_t getMaxSize();
99    /**
100     * Returns the current size of the cache in bytes.
101     */
102    uint32_t getSize();
103
104    /**
105     * Prints out the content of the cache.
106     */
107    void dump();
108
109private:
110    void deleteLayer(Layer* layer);
111
112    struct LayerEntry {
113        LayerEntry():
114            mLayer(NULL), mWidth(0), mHeight(0) {
115        }
116
117        LayerEntry(const uint32_t layerWidth, const uint32_t layerHeight): mLayer(NULL) {
118            mWidth = uint32_t(ceilf(layerWidth / float(LAYER_SIZE)) * LAYER_SIZE);
119            mHeight = uint32_t(ceilf(layerHeight / float(LAYER_SIZE)) * LAYER_SIZE);
120        }
121
122        LayerEntry(Layer* layer):
123            mLayer(layer), mWidth(layer->getWidth()), mHeight(layer->getHeight()) {
124        }
125
126        bool operator<(const LayerEntry& rhs) const {
127            if (mWidth == rhs.mWidth) {
128                return mHeight < rhs.mHeight;
129            }
130            return mWidth < rhs.mWidth;
131        }
132
133        bool operator==(const LayerEntry& rhs) const {
134            return mWidth == rhs.mWidth && mHeight == rhs.mHeight;
135        }
136
137        Layer* mLayer;
138        uint32_t mWidth;
139        uint32_t mHeight;
140    }; // struct LayerEntry
141
142    SortedList<LayerEntry> mCache;
143
144    uint32_t mSize;
145    uint32_t mMaxSize;
146}; // class LayerCache
147
148}; // namespace uirenderer
149}; // namespace android
150
151#endif // ANDROID_HWUI_LAYER_CACHE_H
152