blob: 81810acef163940d52f0986dbf0ed5be6751ffaa [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// Debug
30#if DEBUG_LAYERS
Andreas Gampeedaecc12014-11-10 20:54:07 -080031static const bool kDebugLayers = true;
Romain Guyf18fd992010-07-08 11:45:51 -070032#else
Andreas Gampeedaecc12014-11-10 20:54:07 -080033static const bool kDebugLayers = false;
Romain Guyf18fd992010-07-08 11:45:51 -070034#endif
35
36///////////////////////////////////////////////////////////////////////////////
37// Cache
38///////////////////////////////////////////////////////////////////////////////
39
Romain Guy8550c4c2010-10-08 15:49:53 -070040class LayerCache {
Romain Guydda570202010-07-06 11:39:32 -070041public:
Romain Guyfb8b7632010-08-23 21:05:08 -070042 LayerCache();
Romain Guydda570202010-07-06 11:39:32 -070043 ~LayerCache();
44
45 /**
Romain Guy8550c4c2010-10-08 15:49:53 -070046 * Returns a layer large enough for the specified dimensions. If no suitable
47 * layer can be found, a new one is created and returned. If creating a new
Romain Guyf18fd992010-07-08 11:45:51 -070048 * layer fails, NULL is returned.
49 *
50 * When a layer is obtained from the cache, it is removed and the total
51 * size of the cache goes down.
52 *
Romain Guy8550c4c2010-10-08 15:49:53 -070053 * @param width The desired width of the layer
Romain Guy8d4aeb72013-02-12 16:08:55 -080054 * @param height The desired height of the layer
Romain Guydda570202010-07-06 11:39:32 -070055 */
John Reck3b202512014-06-23 13:13:08 -070056 Layer* get(RenderState& renderState, const uint32_t width, const uint32_t height);
Romain Guyeb993562010-10-05 18:14:38 -070057
Romain Guydda570202010-07-06 11:39:32 -070058 /**
59 * Adds the layer to the cache. The layer will not be added if there is
Romain Guy8550c4c2010-10-08 15:49:53 -070060 * not enough space available. Adding a layer can cause other layers to
61 * be removed from the cache.
Romain Guydda570202010-07-06 11:39:32 -070062 *
Romain Guyf18fd992010-07-08 11:45:51 -070063 * @param layer The layer to add to the cache
64 *
Romain Guydda570202010-07-06 11:39:32 -070065 * @return True if the layer was added, false otherwise.
66 */
Romain Guy8550c4c2010-10-08 15:49:53 -070067 bool put(Layer* layer);
Romain Guydda570202010-07-06 11:39:32 -070068 /**
69 * Clears the cache. This causes all layers to be deleted.
70 */
71 void clear();
72
73 /**
74 * Sets the maximum size of the cache in bytes.
75 */
76 void setMaxSize(uint32_t maxSize);
77 /**
78 * Returns the maximum size of the cache in bytes.
79 */
80 uint32_t getMaxSize();
81 /**
82 * Returns the current size of the cache in bytes.
83 */
84 uint32_t getSize();
85
John Reck17035b02014-09-03 07:39:53 -070086 size_t getCount();
87
Romain Guyeea60692011-07-26 20:35:55 -070088 /**
89 * Prints out the content of the cache.
90 */
91 void dump();
92
Romain Guy8d4aeb72013-02-12 16:08:55 -080093private:
Romain Guy8550c4c2010-10-08 15:49:53 -070094 struct LayerEntry {
95 LayerEntry():
96 mLayer(NULL), mWidth(0), mHeight(0) {
97 }
98
99 LayerEntry(const uint32_t layerWidth, const uint32_t layerHeight): mLayer(NULL) {
Romain Guy2055aba2013-01-18 16:42:51 -0800100 mWidth = Layer::computeIdealWidth(layerWidth);
101 mHeight = Layer::computeIdealHeight(layerHeight);
Romain Guy8550c4c2010-10-08 15:49:53 -0700102 }
103
Romain Guy8550c4c2010-10-08 15:49:53 -0700104 LayerEntry(Layer* layer):
Romain Guy9ace8f52011-07-07 20:50:11 -0700105 mLayer(layer), mWidth(layer->getWidth()), mHeight(layer->getHeight()) {
Romain Guy8550c4c2010-10-08 15:49:53 -0700106 }
107
Romain Guye3a9b242013-01-08 11:15:30 -0800108 static int compare(const LayerEntry& lhs, const LayerEntry& rhs);
109
110 bool operator==(const LayerEntry& other) const {
111 return compare(*this, other) == 0;
Romain Guy8550c4c2010-10-08 15:49:53 -0700112 }
113
Romain Guye3a9b242013-01-08 11:15:30 -0800114 bool operator!=(const LayerEntry& other) const {
115 return compare(*this, other) != 0;
Romain Guy8550c4c2010-10-08 15:49:53 -0700116 }
117
Romain Guy8d4aeb72013-02-12 16:08:55 -0800118 friend inline int strictly_order_type(const LayerEntry& lhs, const LayerEntry& rhs) {
119 return LayerEntry::compare(lhs, rhs) < 0;
120 }
121
122 friend inline int compare_type(const LayerEntry& lhs, const LayerEntry& rhs) {
123 return LayerEntry::compare(lhs, rhs);
124 }
125
Romain Guy8550c4c2010-10-08 15:49:53 -0700126 Layer* mLayer;
127 uint32_t mWidth;
128 uint32_t mHeight;
129 }; // struct LayerEntry
130
Romain Guye3a9b242013-01-08 11:15:30 -0800131 void deleteLayer(Layer* layer);
132
Romain Guy8550c4c2010-10-08 15:49:53 -0700133 SortedList<LayerEntry> mCache;
Romain Guydda570202010-07-06 11:39:32 -0700134
135 uint32_t mSize;
136 uint32_t mMaxSize;
137}; // class LayerCache
138
139}; // namespace uirenderer
140}; // namespace android
141
Romain Guy5b3b3522010-10-27 18:57:51 -0700142#endif // ANDROID_HWUI_LAYER_CACHE_H