blob: 4df8ab3b4b38fc3a394fcb6d9c15ab2df3dbdc89 [file] [log] [blame]
Romain Guydda570202010-07-06 11:39:32 -07001/*
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
Romain Guy5b3b3522010-10-27 18:57:51 -070017#ifndef ANDROID_HWUI_LAYER_CACHE_H
18#define ANDROID_HWUI_LAYER_CACHE_H
Romain Guydda570202010-07-06 11:39:32 -070019
20#include "Layer.h"
Romain Guy8550c4c2010-10-08 15:49:53 -070021#include "utils/SortedList.h"
Romain Guydda570202010-07-06 11:39:32 -070022
23namespace android {
24namespace uirenderer {
25
Romain Guyf18fd992010-07-08 11:45:51 -070026///////////////////////////////////////////////////////////////////////////////
27// Defines
28///////////////////////////////////////////////////////////////////////////////
29
30// Debug
31#define DEBUG_LAYERS 0
32
Romain Guye9108052010-10-12 18:15:42 -070033// Indicates whether to remove the biggest layers first, or the smaller ones
34#define LAYER_REMOVE_BIGGEST 0
Romain Guy8550c4c2010-10-08 15:49:53 -070035// Textures used by layers must have dimensions multiples of this number
36#define LAYER_SIZE 64
37
Romain Guyf18fd992010-07-08 11:45:51 -070038// 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
Romain Guy8550c4c2010-10-08 15:49:53 -070049class LayerCache {
Romain Guydda570202010-07-06 11:39:32 -070050public:
Romain Guyfb8b7632010-08-23 21:05:08 -070051 LayerCache();
Romain Guydda570202010-07-06 11:39:32 -070052 ~LayerCache();
53
54 /**
Romain Guy8550c4c2010-10-08 15:49:53 -070055 * 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
Romain Guyf18fd992010-07-08 11:45:51 -070057 * 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 *
Romain Guy8550c4c2010-10-08 15:49:53 -070062 * @param width The desired width of the layer
63 * @param width The desired height of the layer
Romain Guydda570202010-07-06 11:39:32 -070064 */
Romain Guy8550c4c2010-10-08 15:49:53 -070065 Layer* get(const uint32_t width, const uint32_t height);
Romain Guyeb993562010-10-05 18:14:38 -070066
Romain Guydda570202010-07-06 11:39:32 -070067 /**
68 * Adds the layer to the cache. The layer will not be added if there is
Romain Guy8550c4c2010-10-08 15:49:53 -070069 * not enough space available. Adding a layer can cause other layers to
70 * be removed from the cache.
Romain Guydda570202010-07-06 11:39:32 -070071 *
Romain Guyf18fd992010-07-08 11:45:51 -070072 * @param layer The layer to add to the cache
73 *
Romain Guydda570202010-07-06 11:39:32 -070074 * @return True if the layer was added, false otherwise.
75 */
Romain Guy8550c4c2010-10-08 15:49:53 -070076 bool put(Layer* layer);
Romain Guydda570202010-07-06 11:39:32 -070077 /**
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
Romain Guy8550c4c2010-10-08 15:49:53 -070098 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;
Romain Guydda570202010-07-06 11:39:32 -0700133
134 uint32_t mSize;
135 uint32_t mMaxSize;
136}; // class LayerCache
137
138}; // namespace uirenderer
139}; // namespace android
140
Romain Guy5b3b3522010-10-27 18:57:51 -0700141#endif // ANDROID_HWUI_LAYER_CACHE_H