Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 1 | /* |
| 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 Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 17 | #ifndef ANDROID_HWUI_LAYER_CACHE_H |
| 18 | #define ANDROID_HWUI_LAYER_CACHE_H |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 19 | |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 20 | #include "Debug.h" |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 21 | #include "Layer.h" |
John Reck | bef837d | 2015-07-29 16:51:05 -0700 | [diff] [blame] | 22 | |
| 23 | #include <set> |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 24 | |
| 25 | namespace android { |
| 26 | namespace uirenderer { |
| 27 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 28 | class RenderState; |
| 29 | |
Chris Craik | 07adacf | 2014-12-19 10:08:40 -0800 | [diff] [blame] | 30 | /////////////////////////////////////////////////////////////////////////////// |
| 31 | // Defines |
| 32 | /////////////////////////////////////////////////////////////////////////////// |
| 33 | |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 34 | #if DEBUG_LAYERS |
Chris Craik | 07adacf | 2014-12-19 10:08:40 -0800 | [diff] [blame] | 35 | #define LAYER_LOGD(...) ALOGD(__VA_ARGS__) |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 36 | #else |
Chris Craik | 07adacf | 2014-12-19 10:08:40 -0800 | [diff] [blame] | 37 | #define LAYER_LOGD(...) |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 38 | #endif |
| 39 | |
| 40 | /////////////////////////////////////////////////////////////////////////////// |
| 41 | // Cache |
| 42 | /////////////////////////////////////////////////////////////////////////////// |
| 43 | |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 44 | class LayerCache { |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 45 | public: |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 46 | LayerCache(); |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 47 | ~LayerCache(); |
| 48 | |
| 49 | /** |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 50 | * 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 Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 52 | * 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 Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 57 | * @param width The desired width of the layer |
Romain Guy | 8d4aeb7 | 2013-02-12 16:08:55 -0800 | [diff] [blame] | 58 | * @param height The desired height of the layer |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 59 | */ |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 60 | Layer* get(RenderState& renderState, const uint32_t width, const uint32_t height); |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 61 | |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 62 | /** |
| 63 | * Adds the layer to the cache. The layer will not be added if there is |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 64 | * not enough space available. Adding a layer can cause other layers to |
| 65 | * be removed from the cache. |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 66 | * |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 67 | * @param layer The layer to add to the cache |
| 68 | * |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 69 | * @return True if the layer was added, false otherwise. |
| 70 | */ |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 71 | bool put(Layer* layer); |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 72 | /** |
| 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 | |
John Reck | 17035b0 | 2014-09-03 07:39:53 -0700 | [diff] [blame] | 90 | size_t getCount(); |
| 91 | |
Romain Guy | eea6069 | 2011-07-26 20:35:55 -0700 | [diff] [blame] | 92 | /** |
| 93 | * Prints out the content of the cache. |
| 94 | */ |
| 95 | void dump(); |
| 96 | |
Romain Guy | 8d4aeb7 | 2013-02-12 16:08:55 -0800 | [diff] [blame] | 97 | private: |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 98 | struct LayerEntry { |
| 99 | LayerEntry(): |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame] | 100 | mLayer(nullptr), mWidth(0), mHeight(0) { |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame] | 103 | LayerEntry(const uint32_t layerWidth, const uint32_t layerHeight): mLayer(nullptr) { |
Romain Guy | 2055aba | 2013-01-18 16:42:51 -0800 | [diff] [blame] | 104 | mWidth = Layer::computeIdealWidth(layerWidth); |
| 105 | mHeight = Layer::computeIdealHeight(layerHeight); |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 108 | LayerEntry(Layer* layer): |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 109 | mLayer(layer), mWidth(layer->getWidth()), mHeight(layer->getHeight()) { |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 112 | static int compare(const LayerEntry& lhs, const LayerEntry& rhs); |
| 113 | |
| 114 | bool operator==(const LayerEntry& other) const { |
| 115 | return compare(*this, other) == 0; |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 118 | bool operator!=(const LayerEntry& other) const { |
| 119 | return compare(*this, other) != 0; |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 120 | } |
| 121 | |
John Reck | bef837d | 2015-07-29 16:51:05 -0700 | [diff] [blame] | 122 | bool operator<(const LayerEntry& other) const { |
| 123 | return LayerEntry::compare(*this, other) < 0; |
Romain Guy | 8d4aeb7 | 2013-02-12 16:08:55 -0800 | [diff] [blame] | 124 | } |
| 125 | |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 126 | Layer* mLayer; |
| 127 | uint32_t mWidth; |
| 128 | uint32_t mHeight; |
| 129 | }; // struct LayerEntry |
| 130 | |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 131 | void deleteLayer(Layer* layer); |
| 132 | |
John Reck | bef837d | 2015-07-29 16:51:05 -0700 | [diff] [blame] | 133 | std::multiset<LayerEntry> mCache; |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 134 | |
| 135 | uint32_t mSize; |
| 136 | uint32_t mMaxSize; |
| 137 | }; // class LayerCache |
| 138 | |
| 139 | }; // namespace uirenderer |
| 140 | }; // namespace android |
| 141 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 142 | #endif // ANDROID_HWUI_LAYER_CACHE_H |