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 | |
| 17 | #ifndef ANDROID_UI_LAYER_CACHE_H |
| 18 | #define ANDROID_UI_LAYER_CACHE_H |
| 19 | |
| 20 | #include "Layer.h" |
| 21 | #include "GenerationCache.h" |
| 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 | |
| 33 | // Debug |
| 34 | #if DEBUG_LAYERS |
| 35 | #define LAYER_LOGD(...) LOGD(__VA_ARGS__) |
| 36 | #else |
| 37 | #define LAYER_LOGD(...) |
| 38 | #endif |
| 39 | |
| 40 | /////////////////////////////////////////////////////////////////////////////// |
| 41 | // Cache |
| 42 | /////////////////////////////////////////////////////////////////////////////// |
| 43 | |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 44 | class LayerCache: public OnEntryRemoved<LayerSize, Layer*> { |
| 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(uint32_t maxByteSize); |
| 48 | ~LayerCache(); |
| 49 | |
| 50 | /** |
| 51 | * Used as a callback when an entry is removed from the cache. |
| 52 | * Do not invoke directly. |
| 53 | */ |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 54 | void operator()(LayerSize& size, Layer*& layer); |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 55 | |
| 56 | /** |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 57 | * Returns the layer of specified dimensions. If not suitable layer |
| 58 | * can be found, a new one is created and returned. If creating a new |
| 59 | * layer fails, NULL is returned. |
| 60 | * |
| 61 | * When a layer is obtained from the cache, it is removed and the total |
| 62 | * size of the cache goes down. |
| 63 | * |
| 64 | * @param size The dimensions of the desired layer |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 65 | */ |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 66 | Layer* get(LayerSize& size); |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame^] | 67 | |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 68 | /** |
| 69 | * Adds the layer to the cache. The layer will not be added if there is |
| 70 | * not enough space available. |
| 71 | * |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 72 | * @param size The dimensions of the layer |
| 73 | * @param layer The layer to add to the cache |
| 74 | * |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 75 | * @return True if the layer was added, false otherwise. |
| 76 | */ |
| 77 | bool put(LayerSize& size, Layer* layer); |
| 78 | /** |
| 79 | * Clears the cache. This causes all layers to be deleted. |
| 80 | */ |
| 81 | void clear(); |
| 82 | |
| 83 | /** |
| 84 | * Sets the maximum size of the cache in bytes. |
| 85 | */ |
| 86 | void setMaxSize(uint32_t maxSize); |
| 87 | /** |
| 88 | * Returns the maximum size of the cache in bytes. |
| 89 | */ |
| 90 | uint32_t getMaxSize(); |
| 91 | /** |
| 92 | * Returns the current size of the cache in bytes. |
| 93 | */ |
| 94 | uint32_t getSize(); |
| 95 | |
| 96 | private: |
| 97 | void deleteLayer(Layer* layer); |
| 98 | |
Romain Guy | 6c81893 | 2010-07-07 15:15:32 -0700 | [diff] [blame] | 99 | GenerationCache<LayerSize, Layer*> mCache; |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 100 | |
| 101 | uint32_t mSize; |
| 102 | uint32_t mMaxSize; |
| 103 | }; // class LayerCache |
| 104 | |
| 105 | }; // namespace uirenderer |
| 106 | }; // namespace android |
| 107 | |
| 108 | #endif // ANDROID_UI_LAYER_CACHE_H |