LayerCache.h revision 9ace8f5e79e76893fe4ca9e4d10f6c4056330485
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
104private:
105    void deleteLayer(Layer* layer);
106
107    struct LayerEntry {
108        LayerEntry():
109            mLayer(NULL), mWidth(0), mHeight(0) {
110        }
111
112        LayerEntry(const uint32_t layerWidth, const uint32_t layerHeight): mLayer(NULL) {
113            mWidth = uint32_t(ceilf(layerWidth / float(LAYER_SIZE)) * LAYER_SIZE);
114            mHeight = uint32_t(ceilf(layerHeight / float(LAYER_SIZE)) * LAYER_SIZE);
115        }
116
117        LayerEntry(const LayerEntry& entry):
118            mLayer(entry.mLayer), mWidth(entry.mWidth), mHeight(entry.mHeight) {
119        }
120
121        LayerEntry(Layer* layer):
122            mLayer(layer), mWidth(layer->getWidth()), mHeight(layer->getHeight()) {
123        }
124
125        bool operator<(const LayerEntry& rhs) const {
126            if (mWidth == rhs.mWidth) {
127                return mHeight < rhs.mHeight;
128            }
129            return mWidth < rhs.mWidth;
130        }
131
132        bool operator==(const LayerEntry& rhs) const {
133            return mWidth == rhs.mWidth && mHeight == rhs.mHeight;
134        }
135
136        Layer* mLayer;
137        uint32_t mWidth;
138        uint32_t mHeight;
139    }; // struct LayerEntry
140
141    SortedList<LayerEntry> mCache;
142
143    uint32_t mSize;
144    uint32_t mMaxSize;
145}; // class LayerCache
146
147}; // namespace uirenderer
148}; // namespace android
149
150#endif // ANDROID_HWUI_LAYER_CACHE_H
151