blob: fd698e24b7773da0f1392119650083317488555f [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
Romain Guyc15008e2010-11-10 11:59:15 -080020#include "Debug.h"
Romain Guydda570202010-07-06 11:39:32 -070021#include "Layer.h"
Romain Guy28d8ff62011-08-22 14:01:34 -070022#include "Properties.h"
Romain Guy8550c4c2010-10-08 15:49:53 -070023#include "utils/SortedList.h"
Romain Guydda570202010-07-06 11:39:32 -070024
25namespace android {
26namespace uirenderer {
27
Romain Guyf18fd992010-07-08 11:45:51 -070028///////////////////////////////////////////////////////////////////////////////
29// Defines
30///////////////////////////////////////////////////////////////////////////////
31
Romain Guyf18fd992010-07-08 11:45:51 -070032// Debug
33#if DEBUG_LAYERS
Steve Block1afd5ba2011-12-20 16:23:08 +000034 #define LAYER_LOGD(...) ALOGD(__VA_ARGS__)
Romain Guyf18fd992010-07-08 11:45:51 -070035#else
36 #define LAYER_LOGD(...)
37#endif
38
39///////////////////////////////////////////////////////////////////////////////
40// Cache
41///////////////////////////////////////////////////////////////////////////////
42
Romain Guy8550c4c2010-10-08 15:49:53 -070043class LayerCache {
Romain Guydda570202010-07-06 11:39:32 -070044public:
Romain Guyfb8b7632010-08-23 21:05:08 -070045 LayerCache();
Romain Guydda570202010-07-06 11:39:32 -070046 ~LayerCache();
47
48 /**
Romain Guy8550c4c2010-10-08 15:49:53 -070049 * Returns a layer large enough for the specified dimensions. If no suitable
50 * layer can be found, a new one is created and returned. If creating a new
Romain Guyf18fd992010-07-08 11:45:51 -070051 * layer fails, NULL is returned.
52 *
53 * When a layer is obtained from the cache, it is removed and the total
54 * size of the cache goes down.
55 *
Romain Guy8550c4c2010-10-08 15:49:53 -070056 * @param width The desired width of the layer
57 * @param width The desired height of the layer
Romain Guydda570202010-07-06 11:39:32 -070058 */
Romain Guy8550c4c2010-10-08 15:49:53 -070059 Layer* get(const uint32_t width, const uint32_t height);
Romain Guyeb993562010-10-05 18:14:38 -070060
Romain Guydda570202010-07-06 11:39:32 -070061 /**
62 * Adds the layer to the cache. The layer will not be added if there is
Romain Guy8550c4c2010-10-08 15:49:53 -070063 * not enough space available. Adding a layer can cause other layers to
64 * be removed from the cache.
Romain Guydda570202010-07-06 11:39:32 -070065 *
Romain Guyf18fd992010-07-08 11:45:51 -070066 * @param layer The layer to add to the cache
67 *
Romain Guydda570202010-07-06 11:39:32 -070068 * @return True if the layer was added, false otherwise.
69 */
Romain Guy8550c4c2010-10-08 15:49:53 -070070 bool put(Layer* layer);
Romain Guydda570202010-07-06 11:39:32 -070071 /**
72 * Clears the cache. This causes all layers to be deleted.
73 */
74 void clear();
Romain Guy09b7c912011-02-02 20:28:09 -080075 /**
76 * Resize the specified layer if needed.
77 *
78 * @param layer The layer to resize
79 * @param width The new width of the layer
80 * @param height The new height of the layer
81 *
82 * @return True if the layer was resized or nothing happened, false if
83 * a failure occurred during the resizing operation
84 */
85 bool resize(Layer* layer, const uint32_t width, const uint32_t height);
Romain Guydda570202010-07-06 11:39:32 -070086
87 /**
88 * Sets the maximum size of the cache in bytes.
89 */
90 void setMaxSize(uint32_t maxSize);
91 /**
92 * Returns the maximum size of the cache in bytes.
93 */
94 uint32_t getMaxSize();
95 /**
96 * Returns the current size of the cache in bytes.
97 */
98 uint32_t getSize();
99
Romain Guyeea60692011-07-26 20:35:55 -0700100 /**
101 * Prints out the content of the cache.
102 */
103 void dump();
104
Romain Guydda570202010-07-06 11:39:32 -0700105private:
106 void deleteLayer(Layer* layer);
107
Romain Guy8550c4c2010-10-08 15:49:53 -0700108 struct LayerEntry {
109 LayerEntry():
110 mLayer(NULL), mWidth(0), mHeight(0) {
111 }
112
113 LayerEntry(const uint32_t layerWidth, const uint32_t layerHeight): mLayer(NULL) {
114 mWidth = uint32_t(ceilf(layerWidth / float(LAYER_SIZE)) * LAYER_SIZE);
115 mHeight = uint32_t(ceilf(layerHeight / float(LAYER_SIZE)) * LAYER_SIZE);
116 }
117
Romain Guy8550c4c2010-10-08 15:49:53 -0700118 LayerEntry(Layer* layer):
Romain Guy9ace8f52011-07-07 20:50:11 -0700119 mLayer(layer), mWidth(layer->getWidth()), mHeight(layer->getHeight()) {
Romain Guy8550c4c2010-10-08 15:49:53 -0700120 }
121
122 bool operator<(const LayerEntry& rhs) const {
123 if (mWidth == rhs.mWidth) {
124 return mHeight < rhs.mHeight;
125 }
126 return mWidth < rhs.mWidth;
127 }
128
129 bool operator==(const LayerEntry& rhs) const {
130 return mWidth == rhs.mWidth && mHeight == rhs.mHeight;
131 }
132
133 Layer* mLayer;
134 uint32_t mWidth;
135 uint32_t mHeight;
136 }; // struct LayerEntry
137
138 SortedList<LayerEntry> mCache;
Romain Guydda570202010-07-06 11:39:32 -0700139
140 uint32_t mSize;
141 uint32_t mMaxSize;
142}; // class LayerCache
143
144}; // namespace uirenderer
145}; // namespace android
146
Romain Guy5b3b3522010-10-27 18:57:51 -0700147#endif // ANDROID_HWUI_LAYER_CACHE_H