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