blob: 1b0fc2cfd1d7ad3528e33c28c333988d0766776a [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 Guy8550c4c2010-10-08 15:49:53 -070022#include "utils/SortedList.h"
Romain Guydda570202010-07-06 11:39:32 -070023
24namespace android {
25namespace uirenderer {
26
John Reck3b202512014-06-23 13:13:08 -070027class RenderState;
28
Romain Guyf18fd992010-07-08 11:45:51 -070029///////////////////////////////////////////////////////////////////////////////
30// Defines
31///////////////////////////////////////////////////////////////////////////////
32
Romain Guyf18fd992010-07-08 11:45:51 -070033// Debug
34#if DEBUG_LAYERS
Steve Block5baa3a62011-12-20 16:23:08 +000035 #define LAYER_LOGD(...) ALOGD(__VA_ARGS__)
Romain Guyf18fd992010-07-08 11:45:51 -070036#else
37 #define LAYER_LOGD(...)
38#endif
39
40///////////////////////////////////////////////////////////////////////////////
41// Cache
42///////////////////////////////////////////////////////////////////////////////
43
Romain Guy8550c4c2010-10-08 15:49:53 -070044class LayerCache {
Romain Guydda570202010-07-06 11:39:32 -070045public:
Romain Guyfb8b7632010-08-23 21:05:08 -070046 LayerCache();
Romain Guydda570202010-07-06 11:39:32 -070047 ~LayerCache();
48
49 /**
Romain Guy8550c4c2010-10-08 15:49:53 -070050 * Returns a layer large enough for the specified dimensions. If no suitable
51 * layer can be found, a new one is created and returned. If creating a new
Romain Guyf18fd992010-07-08 11:45:51 -070052 * layer fails, NULL is returned.
53 *
54 * When a layer is obtained from the cache, it is removed and the total
55 * size of the cache goes down.
56 *
Romain Guy8550c4c2010-10-08 15:49:53 -070057 * @param width The desired width of the layer
Romain Guy8d4aeb72013-02-12 16:08:55 -080058 * @param height The desired height of the layer
Romain Guydda570202010-07-06 11:39:32 -070059 */
John Reck3b202512014-06-23 13:13:08 -070060 Layer* get(RenderState& renderState, const uint32_t width, const uint32_t height);
Romain Guyeb993562010-10-05 18:14:38 -070061
Romain Guydda570202010-07-06 11:39:32 -070062 /**
63 * Adds the layer to the cache. The layer will not be added if there is
Romain Guy8550c4c2010-10-08 15:49:53 -070064 * not enough space available. Adding a layer can cause other layers to
65 * be removed from the cache.
Romain Guydda570202010-07-06 11:39:32 -070066 *
Romain Guyf18fd992010-07-08 11:45:51 -070067 * @param layer The layer to add to the cache
68 *
Romain Guydda570202010-07-06 11:39:32 -070069 * @return True if the layer was added, false otherwise.
70 */
Romain Guy8550c4c2010-10-08 15:49:53 -070071 bool put(Layer* layer);
Romain Guydda570202010-07-06 11:39:32 -070072 /**
73 * Clears the cache. This causes all layers to be deleted.
74 */
75 void clear();
76
77 /**
78 * Sets the maximum size of the cache in bytes.
79 */
80 void setMaxSize(uint32_t maxSize);
81 /**
82 * Returns the maximum size of the cache in bytes.
83 */
84 uint32_t getMaxSize();
85 /**
86 * Returns the current size of the cache in bytes.
87 */
88 uint32_t getSize();
89
Romain Guyeea60692011-07-26 20:35:55 -070090 /**
91 * Prints out the content of the cache.
92 */
93 void dump();
94
Romain Guy8d4aeb72013-02-12 16:08:55 -080095private:
Romain Guy8550c4c2010-10-08 15:49:53 -070096 struct LayerEntry {
97 LayerEntry():
98 mLayer(NULL), mWidth(0), mHeight(0) {
99 }
100
101 LayerEntry(const uint32_t layerWidth, const uint32_t layerHeight): mLayer(NULL) {
Romain Guy2055aba2013-01-18 16:42:51 -0800102 mWidth = Layer::computeIdealWidth(layerWidth);
103 mHeight = Layer::computeIdealHeight(layerHeight);
Romain Guy8550c4c2010-10-08 15:49:53 -0700104 }
105
Romain Guy8550c4c2010-10-08 15:49:53 -0700106 LayerEntry(Layer* layer):
Romain Guy9ace8f52011-07-07 20:50:11 -0700107 mLayer(layer), mWidth(layer->getWidth()), mHeight(layer->getHeight()) {
Romain Guy8550c4c2010-10-08 15:49:53 -0700108 }
109
Romain Guye3a9b242013-01-08 11:15:30 -0800110 static int compare(const LayerEntry& lhs, const LayerEntry& rhs);
111
112 bool operator==(const LayerEntry& other) const {
113 return compare(*this, other) == 0;
Romain Guy8550c4c2010-10-08 15:49:53 -0700114 }
115
Romain Guye3a9b242013-01-08 11:15:30 -0800116 bool operator!=(const LayerEntry& other) const {
117 return compare(*this, other) != 0;
Romain Guy8550c4c2010-10-08 15:49:53 -0700118 }
119
Romain Guy8d4aeb72013-02-12 16:08:55 -0800120 friend inline int strictly_order_type(const LayerEntry& lhs, const LayerEntry& rhs) {
121 return LayerEntry::compare(lhs, rhs) < 0;
122 }
123
124 friend inline int compare_type(const LayerEntry& lhs, const LayerEntry& rhs) {
125 return LayerEntry::compare(lhs, rhs);
126 }
127
Romain Guy8550c4c2010-10-08 15:49:53 -0700128 Layer* mLayer;
129 uint32_t mWidth;
130 uint32_t mHeight;
131 }; // struct LayerEntry
132
Romain Guye3a9b242013-01-08 11:15:30 -0800133 void deleteLayer(Layer* layer);
134
Romain Guy8550c4c2010-10-08 15:49:53 -0700135 SortedList<LayerEntry> mCache;
Romain Guydda570202010-07-06 11:39:32 -0700136
137 uint32_t mSize;
138 uint32_t mMaxSize;
139}; // class LayerCache
140
141}; // namespace uirenderer
142}; // namespace android
143
Romain Guy5b3b3522010-10-27 18:57:51 -0700144#endif // ANDROID_HWUI_LAYER_CACHE_H