blob: 81b8bf36577a12deb8aca4905e64981c7eaddce3 [file] [log] [blame]
Romain Guydda57022010-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 Guydda57022010-07-06 11:39:32 -070019
Romain Guyc15008e2010-11-10 11:59:15 -080020#include "Debug.h"
Romain Guydda57022010-07-06 11:39:32 -070021#include "Layer.h"
Romain Guy8550c4c2010-10-08 15:49:53 -070022#include "utils/SortedList.h"
Romain Guydda57022010-07-06 11:39:32 -070023
24namespace android {
25namespace uirenderer {
26
Romain Guyf18fd992010-07-08 11:45:51 -070027///////////////////////////////////////////////////////////////////////////////
28// Defines
29///////////////////////////////////////////////////////////////////////////////
30
Romain Guye9108052010-10-12 18:15:42 -070031// Indicates whether to remove the biggest layers first, or the smaller ones
32#define LAYER_REMOVE_BIGGEST 0
Romain Guy8550c4c2010-10-08 15:49:53 -070033// Textures used by layers must have dimensions multiples of this number
34#define LAYER_SIZE 64
35
Romain Guyf18fd992010-07-08 11:45:51 -070036// 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
Romain Guy8550c4c2010-10-08 15:49:53 -070047class LayerCache {
Romain Guydda57022010-07-06 11:39:32 -070048public:
Romain Guyfb8b7632010-08-23 21:05:08 -070049 LayerCache();
Romain Guydda57022010-07-06 11:39:32 -070050 ~LayerCache();
51
52 /**
Romain Guy8550c4c2010-10-08 15:49:53 -070053 * 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
Romain Guyf18fd992010-07-08 11:45:51 -070055 * 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 *
Romain Guy8550c4c2010-10-08 15:49:53 -070060 * @param width The desired width of the layer
61 * @param width The desired height of the layer
Romain Guydda57022010-07-06 11:39:32 -070062 */
Romain Guy8550c4c2010-10-08 15:49:53 -070063 Layer* get(const uint32_t width, const uint32_t height);
Romain Guyeb993562010-10-05 18:14:38 -070064
Romain Guydda57022010-07-06 11:39:32 -070065 /**
66 * Adds the layer to the cache. The layer will not be added if there is
Romain Guy8550c4c2010-10-08 15:49:53 -070067 * not enough space available. Adding a layer can cause other layers to
68 * be removed from the cache.
Romain Guydda57022010-07-06 11:39:32 -070069 *
Romain Guyf18fd992010-07-08 11:45:51 -070070 * @param layer The layer to add to the cache
71 *
Romain Guydda57022010-07-06 11:39:32 -070072 * @return True if the layer was added, false otherwise.
73 */
Romain Guy8550c4c2010-10-08 15:49:53 -070074 bool put(Layer* layer);
Romain Guydda57022010-07-06 11:39:32 -070075 /**
76 * Clears the cache. This causes all layers to be deleted.
77 */
78 void clear();
Romain Guy09b7c912011-02-02 20:28:09 -080079 /**
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);
Romain Guydda57022010-07-06 11:39:32 -070090
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
Romain Guy8550c4c2010-10-08 15:49:53 -0700107 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):
Romain Guy9ace8f52011-07-07 20:50:11 -0700122 mLayer(layer), mWidth(layer->getWidth()), mHeight(layer->getHeight()) {
Romain Guy8550c4c2010-10-08 15:49:53 -0700123 }
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;
Romain Guydda57022010-07-06 11:39:32 -0700142
143 uint32_t mSize;
144 uint32_t mMaxSize;
145}; // class LayerCache
146
147}; // namespace uirenderer
148}; // namespace android
149
Romain Guy5b3b3522010-10-27 18:57:51 -0700150#endif // ANDROID_HWUI_LAYER_CACHE_H