LayerCache.h revision 3b20251a355c88193c439f928a84ae69483fb488
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_HWUI_LAYER_CACHE_H 18#define ANDROID_HWUI_LAYER_CACHE_H 19 20#include "Debug.h" 21#include "Layer.h" 22#include "utils/SortedList.h" 23 24namespace android { 25namespace uirenderer { 26 27class RenderState; 28 29/////////////////////////////////////////////////////////////////////////////// 30// Defines 31/////////////////////////////////////////////////////////////////////////////// 32 33// Debug 34#if DEBUG_LAYERS 35 #define LAYER_LOGD(...) ALOGD(__VA_ARGS__) 36#else 37 #define LAYER_LOGD(...) 38#endif 39 40/////////////////////////////////////////////////////////////////////////////// 41// Cache 42/////////////////////////////////////////////////////////////////////////////// 43 44class LayerCache { 45public: 46 LayerCache(); 47 ~LayerCache(); 48 49 /** 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 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 * 57 * @param width The desired width of the layer 58 * @param height The desired height of the layer 59 */ 60 Layer* get(RenderState& renderState, const uint32_t width, const uint32_t height); 61 62 /** 63 * Adds the layer to the cache. The layer will not be added if there is 64 * not enough space available. Adding a layer can cause other layers to 65 * be removed from the cache. 66 * 67 * @param layer The layer to add to the cache 68 * 69 * @return True if the layer was added, false otherwise. 70 */ 71 bool put(Layer* layer); 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 90 /** 91 * Prints out the content of the cache. 92 */ 93 void dump(); 94 95private: 96 struct LayerEntry { 97 LayerEntry(): 98 mLayer(NULL), mWidth(0), mHeight(0) { 99 } 100 101 LayerEntry(const uint32_t layerWidth, const uint32_t layerHeight): mLayer(NULL) { 102 mWidth = Layer::computeIdealWidth(layerWidth); 103 mHeight = Layer::computeIdealHeight(layerHeight); 104 } 105 106 LayerEntry(Layer* layer): 107 mLayer(layer), mWidth(layer->getWidth()), mHeight(layer->getHeight()) { 108 } 109 110 static int compare(const LayerEntry& lhs, const LayerEntry& rhs); 111 112 bool operator==(const LayerEntry& other) const { 113 return compare(*this, other) == 0; 114 } 115 116 bool operator!=(const LayerEntry& other) const { 117 return compare(*this, other) != 0; 118 } 119 120 friend inline int strictly_order_type(const LayerEntry& lhs, const LayerEntry& rhs) { 121 return LayerEntry::compare(lhs, rhs) < 0; 122 } 123 124 friend inline int compare_type(const LayerEntry& lhs, const LayerEntry& rhs) { 125 return LayerEntry::compare(lhs, rhs); 126 } 127 128 Layer* mLayer; 129 uint32_t mWidth; 130 uint32_t mHeight; 131 }; // struct LayerEntry 132 133 void deleteLayer(Layer* layer); 134 135 SortedList<LayerEntry> mCache; 136 137 uint32_t mSize; 138 uint32_t mMaxSize; 139}; // class LayerCache 140 141}; // namespace uirenderer 142}; // namespace android 143 144#endif // ANDROID_HWUI_LAYER_CACHE_H 145