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