blob: 7d17b9ba41aab122deebb66b94ee6413972f5581 [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
Chris Craik07adacf2014-12-19 10:08:40 -080029///////////////////////////////////////////////////////////////////////////////
30// Defines
31///////////////////////////////////////////////////////////////////////////////
32
Romain Guyf18fd992010-07-08 11:45:51 -070033#if DEBUG_LAYERS
Chris Craik07adacf2014-12-19 10:08:40 -080034 #define LAYER_LOGD(...) ALOGD(__VA_ARGS__)
Romain Guyf18fd992010-07-08 11:45:51 -070035#else
Chris Craik07adacf2014-12-19 10:08:40 -080036 #define LAYER_LOGD(...)
Romain Guyf18fd992010-07-08 11:45:51 -070037#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
Romain Guy8d4aeb72013-02-12 16:08:55 -080057 * @param height The desired height of the layer
Romain Guydda570202010-07-06 11:39:32 -070058 */
John Reck3b202512014-06-23 13:13:08 -070059 Layer* get(RenderState& renderState, 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();
75
76 /**
77 * Sets the maximum size of the cache in bytes.
78 */
79 void setMaxSize(uint32_t maxSize);
80 /**
81 * Returns the maximum size of the cache in bytes.
82 */
83 uint32_t getMaxSize();
84 /**
85 * Returns the current size of the cache in bytes.
86 */
87 uint32_t getSize();
88
John Reck17035b02014-09-03 07:39:53 -070089 size_t getCount();
90
Romain Guyeea60692011-07-26 20:35:55 -070091 /**
92 * Prints out the content of the cache.
93 */
94 void dump();
95
Romain Guy8d4aeb72013-02-12 16:08:55 -080096private:
Romain Guy8550c4c2010-10-08 15:49:53 -070097 struct LayerEntry {
98 LayerEntry():
Chris Craike84a2082014-12-22 14:28:49 -080099 mLayer(nullptr), mWidth(0), mHeight(0) {
Romain Guy8550c4c2010-10-08 15:49:53 -0700100 }
101
Chris Craike84a2082014-12-22 14:28:49 -0800102 LayerEntry(const uint32_t layerWidth, const uint32_t layerHeight): mLayer(nullptr) {
Romain Guy2055aba2013-01-18 16:42:51 -0800103 mWidth = Layer::computeIdealWidth(layerWidth);
104 mHeight = Layer::computeIdealHeight(layerHeight);
Romain Guy8550c4c2010-10-08 15:49:53 -0700105 }
106
Romain Guy8550c4c2010-10-08 15:49:53 -0700107 LayerEntry(Layer* layer):
Romain Guy9ace8f52011-07-07 20:50:11 -0700108 mLayer(layer), mWidth(layer->getWidth()), mHeight(layer->getHeight()) {
Romain Guy8550c4c2010-10-08 15:49:53 -0700109 }
110
Romain Guye3a9b242013-01-08 11:15:30 -0800111 static int compare(const LayerEntry& lhs, const LayerEntry& rhs);
112
113 bool operator==(const LayerEntry& other) const {
114 return compare(*this, other) == 0;
Romain Guy8550c4c2010-10-08 15:49:53 -0700115 }
116
Romain Guye3a9b242013-01-08 11:15:30 -0800117 bool operator!=(const LayerEntry& other) const {
118 return compare(*this, other) != 0;
Romain Guy8550c4c2010-10-08 15:49:53 -0700119 }
120
Romain Guy8d4aeb72013-02-12 16:08:55 -0800121 friend inline int strictly_order_type(const LayerEntry& lhs, const LayerEntry& rhs) {
122 return LayerEntry::compare(lhs, rhs) < 0;
123 }
124
125 friend inline int compare_type(const LayerEntry& lhs, const LayerEntry& rhs) {
126 return LayerEntry::compare(lhs, rhs);
127 }
128
Romain Guy8550c4c2010-10-08 15:49:53 -0700129 Layer* mLayer;
130 uint32_t mWidth;
131 uint32_t mHeight;
132 }; // struct LayerEntry
133
Romain Guye3a9b242013-01-08 11:15:30 -0800134 void deleteLayer(Layer* layer);
135
Romain Guy8550c4c2010-10-08 15:49:53 -0700136 SortedList<LayerEntry> mCache;
Romain Guydda570202010-07-06 11:39:32 -0700137
138 uint32_t mSize;
139 uint32_t mMaxSize;
140}; // class LayerCache
141
142}; // namespace uirenderer
143}; // namespace android
144
Romain Guy5b3b3522010-10-27 18:57:51 -0700145#endif // ANDROID_HWUI_LAYER_CACHE_H