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 | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 22 | #include "utils/SortedList.h" |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 23 | |
| 24 | namespace android { |
| 25 | namespace uirenderer { |
| 26 | |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 27 | /////////////////////////////////////////////////////////////////////////////// |
| 28 | // Defines |
| 29 | /////////////////////////////////////////////////////////////////////////////// |
| 30 | |
Romain Guy | e910805 | 2010-10-12 18:15:42 -0700 | [diff] [blame] | 31 | // Indicates whether to remove the biggest layers first, or the smaller ones |
| 32 | #define LAYER_REMOVE_BIGGEST 0 |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 33 | // Textures used by layers must have dimensions multiples of this number |
| 34 | #define LAYER_SIZE 64 |
| 35 | |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 36 | // Debug |
| 37 | #if DEBUG_LAYERS |
| 38 | #define LAYER_LOGD(...) LOGD(__VA_ARGS__) |
| 39 | #else |
| 40 | #define LAYER_LOGD(...) |
| 41 | #endif |
| 42 | |
| 43 | /////////////////////////////////////////////////////////////////////////////// |
| 44 | // Cache |
| 45 | /////////////////////////////////////////////////////////////////////////////// |
| 46 | |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 47 | class LayerCache { |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 48 | public: |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 49 | LayerCache(); |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 50 | ~LayerCache(); |
| 51 | |
| 52 | /** |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 53 | * Returns a layer large enough for the specified dimensions. If no suitable |
| 54 | * 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] | 55 | * layer fails, NULL is returned. |
| 56 | * |
| 57 | * When a layer is obtained from the cache, it is removed and the total |
| 58 | * size of the cache goes down. |
| 59 | * |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 60 | * @param width The desired width of the layer |
| 61 | * @param width The desired height of the layer |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 62 | */ |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 63 | Layer* get(const uint32_t width, const uint32_t height); |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 64 | |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 65 | /** |
| 66 | * 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] | 67 | * not enough space available. Adding a layer can cause other layers to |
| 68 | * be removed from the cache. |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 69 | * |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 70 | * @param layer The layer to add to the cache |
| 71 | * |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 72 | * @return True if the layer was added, false otherwise. |
| 73 | */ |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 74 | bool put(Layer* layer); |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 75 | /** |
| 76 | * Clears the cache. This causes all layers to be deleted. |
| 77 | */ |
| 78 | void clear(); |
Romain Guy | 09b7c91 | 2011-02-02 20:28:09 -0800 | [diff] [blame] | 79 | /** |
| 80 | * Resize the specified layer if needed. |
| 81 | * |
| 82 | * @param layer The layer to resize |
| 83 | * @param width The new width of the layer |
| 84 | * @param height The new height of the layer |
| 85 | * |
| 86 | * @return True if the layer was resized or nothing happened, false if |
| 87 | * a failure occurred during the resizing operation |
| 88 | */ |
| 89 | bool resize(Layer* layer, const uint32_t width, const uint32_t height); |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 90 | |
| 91 | /** |
| 92 | * Sets the maximum size of the cache in bytes. |
| 93 | */ |
| 94 | void setMaxSize(uint32_t maxSize); |
| 95 | /** |
| 96 | * Returns the maximum size of the cache in bytes. |
| 97 | */ |
| 98 | uint32_t getMaxSize(); |
| 99 | /** |
| 100 | * Returns the current size of the cache in bytes. |
| 101 | */ |
| 102 | uint32_t getSize(); |
| 103 | |
Romain Guy | eea6069 | 2011-07-26 20:35:55 -0700 | [diff] [blame] | 104 | /** |
| 105 | * Prints out the content of the cache. |
| 106 | */ |
| 107 | void dump(); |
| 108 | |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 109 | private: |
| 110 | void deleteLayer(Layer* layer); |
| 111 | |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 112 | struct LayerEntry { |
| 113 | LayerEntry(): |
| 114 | mLayer(NULL), mWidth(0), mHeight(0) { |
| 115 | } |
| 116 | |
| 117 | LayerEntry(const uint32_t layerWidth, const uint32_t layerHeight): mLayer(NULL) { |
| 118 | mWidth = uint32_t(ceilf(layerWidth / float(LAYER_SIZE)) * LAYER_SIZE); |
| 119 | mHeight = uint32_t(ceilf(layerHeight / float(LAYER_SIZE)) * LAYER_SIZE); |
| 120 | } |
| 121 | |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 122 | LayerEntry(Layer* layer): |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 123 | mLayer(layer), mWidth(layer->getWidth()), mHeight(layer->getHeight()) { |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | bool operator<(const LayerEntry& rhs) const { |
| 127 | if (mWidth == rhs.mWidth) { |
| 128 | return mHeight < rhs.mHeight; |
| 129 | } |
| 130 | return mWidth < rhs.mWidth; |
| 131 | } |
| 132 | |
| 133 | bool operator==(const LayerEntry& rhs) const { |
| 134 | return mWidth == rhs.mWidth && mHeight == rhs.mHeight; |
| 135 | } |
| 136 | |
| 137 | Layer* mLayer; |
| 138 | uint32_t mWidth; |
| 139 | uint32_t mHeight; |
| 140 | }; // struct LayerEntry |
| 141 | |
| 142 | SortedList<LayerEntry> mCache; |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 143 | |
| 144 | uint32_t mSize; |
| 145 | uint32_t mMaxSize; |
| 146 | }; // class LayerCache |
| 147 | |
| 148 | }; // namespace uirenderer |
| 149 | }; // namespace android |
| 150 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 151 | #endif // ANDROID_HWUI_LAYER_CACHE_H |