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" |
Romain Guy | 28d8ff6 | 2011-08-22 14:01:34 -0700 | [diff] [blame] | 22 | #include "Properties.h" |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 23 | #include "utils/SortedList.h" |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 24 | |
| 25 | namespace android { |
| 26 | namespace uirenderer { |
| 27 | |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 28 | /////////////////////////////////////////////////////////////////////////////// |
| 29 | // Defines |
| 30 | /////////////////////////////////////////////////////////////////////////////// |
| 31 | |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 32 | // Debug |
| 33 | #if DEBUG_LAYERS |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 34 | #define LAYER_LOGD(...) ALOGD(__VA_ARGS__) |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 35 | #else |
| 36 | #define LAYER_LOGD(...) |
| 37 | #endif |
| 38 | |
| 39 | /////////////////////////////////////////////////////////////////////////////// |
| 40 | // Cache |
| 41 | /////////////////////////////////////////////////////////////////////////////// |
| 42 | |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 43 | class LayerCache { |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 44 | public: |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 45 | LayerCache(); |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 46 | ~LayerCache(); |
| 47 | |
| 48 | /** |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 49 | * 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 Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 51 | * 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 Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 56 | * @param width The desired width of the layer |
| 57 | * @param width The desired height of the layer |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 58 | */ |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 59 | Layer* get(const uint32_t width, const uint32_t height); |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 60 | |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 61 | /** |
| 62 | * 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] | 63 | * not enough space available. Adding a layer can cause other layers to |
| 64 | * be removed from the cache. |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 65 | * |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 66 | * @param layer The layer to add to the cache |
| 67 | * |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 68 | * @return True if the layer was added, false otherwise. |
| 69 | */ |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 70 | bool put(Layer* layer); |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 71 | /** |
| 72 | * Clears the cache. This causes all layers to be deleted. |
| 73 | */ |
| 74 | void clear(); |
Romain Guy | 09b7c91 | 2011-02-02 20:28:09 -0800 | [diff] [blame] | 75 | /** |
| 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 Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 86 | |
| 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 Guy | eea6069 | 2011-07-26 20:35:55 -0700 | [diff] [blame] | 100 | /** |
| 101 | * Prints out the content of the cache. |
| 102 | */ |
| 103 | void dump(); |
| 104 | |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 105 | struct LayerEntry { |
| 106 | LayerEntry(): |
| 107 | mLayer(NULL), mWidth(0), mHeight(0) { |
| 108 | } |
| 109 | |
| 110 | LayerEntry(const uint32_t layerWidth, const uint32_t layerHeight): mLayer(NULL) { |
| 111 | mWidth = uint32_t(ceilf(layerWidth / float(LAYER_SIZE)) * LAYER_SIZE); |
| 112 | mHeight = uint32_t(ceilf(layerHeight / float(LAYER_SIZE)) * LAYER_SIZE); |
| 113 | } |
| 114 | |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 115 | LayerEntry(Layer* layer): |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 116 | mLayer(layer), mWidth(layer->getWidth()), mHeight(layer->getHeight()) { |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame^] | 119 | static int compare(const LayerEntry& lhs, const LayerEntry& rhs); |
| 120 | |
| 121 | bool operator==(const LayerEntry& other) const { |
| 122 | return compare(*this, other) == 0; |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame^] | 125 | bool operator!=(const LayerEntry& other) const { |
| 126 | return compare(*this, other) != 0; |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | Layer* mLayer; |
| 130 | uint32_t mWidth; |
| 131 | uint32_t mHeight; |
| 132 | }; // struct LayerEntry |
| 133 | |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame^] | 134 | private: |
| 135 | void deleteLayer(Layer* layer); |
| 136 | |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 137 | SortedList<LayerEntry> mCache; |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 138 | |
| 139 | uint32_t mSize; |
| 140 | uint32_t mMaxSize; |
| 141 | }; // class LayerCache |
| 142 | |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame^] | 143 | inline int strictly_order_type(const LayerCache::LayerEntry& lhs, |
| 144 | const LayerCache::LayerEntry& rhs) { |
| 145 | return LayerCache::LayerEntry::compare(lhs, rhs) < 0; |
| 146 | } |
| 147 | |
| 148 | inline int compare_type(const LayerCache::LayerEntry& lhs, const LayerCache::LayerEntry& rhs) { |
| 149 | return LayerCache::LayerEntry::compare(lhs, rhs); |
| 150 | } |
| 151 | |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 152 | }; // namespace uirenderer |
| 153 | }; // namespace android |
| 154 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 155 | #endif // ANDROID_HWUI_LAYER_CACHE_H |