LayerCache.h revision 8550c4c7b5952b7a4e1e0ede95c9492d03099a13
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 "utils/SortedList.h" 22 23namespace android { 24namespace uirenderer { 25 26/////////////////////////////////////////////////////////////////////////////// 27// Defines 28/////////////////////////////////////////////////////////////////////////////// 29 30// Debug 31#define DEBUG_LAYERS 0 32 33// Textures used by layers must have dimensions multiples of this number 34#define LAYER_SIZE 64 35 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 47class LayerCache { 48public: 49 LayerCache(); 50 ~LayerCache(); 51 52 /** 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 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 * 60 * @param width The desired width of the layer 61 * @param width The desired height of the layer 62 */ 63 Layer* get(const uint32_t width, const uint32_t height); 64 65 /** 66 * Adds the layer to the cache. The layer will not be added if there is 67 * not enough space available. Adding a layer can cause other layers to 68 * be removed from the cache. 69 * 70 * @param layer The layer to add to the cache 71 * 72 * @return True if the layer was added, false otherwise. 73 */ 74 bool put(Layer* layer); 75 /** 76 * Clears the cache. This causes all layers to be deleted. 77 */ 78 void clear(); 79 80 /** 81 * Sets the maximum size of the cache in bytes. 82 */ 83 void setMaxSize(uint32_t maxSize); 84 /** 85 * Returns the maximum size of the cache in bytes. 86 */ 87 uint32_t getMaxSize(); 88 /** 89 * Returns the current size of the cache in bytes. 90 */ 91 uint32_t getSize(); 92 93private: 94 void deleteLayer(Layer* layer); 95 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 = uint32_t(ceilf(layerWidth / float(LAYER_SIZE)) * LAYER_SIZE); 103 mHeight = uint32_t(ceilf(layerHeight / float(LAYER_SIZE)) * LAYER_SIZE); 104 } 105 106 LayerEntry(const LayerEntry& entry): 107 mLayer(entry.mLayer), mWidth(entry.mWidth), mHeight(entry.mHeight) { 108 } 109 110 LayerEntry(Layer* layer): 111 mLayer(layer), mWidth(layer->width), mHeight(layer->height) { 112 } 113 114 bool operator<(const LayerEntry& rhs) const { 115 if (mWidth == rhs.mWidth) { 116 return mHeight < rhs.mHeight; 117 } 118 return mWidth < rhs.mWidth; 119 } 120 121 bool operator==(const LayerEntry& rhs) const { 122 return mWidth == rhs.mWidth && mHeight == rhs.mHeight; 123 } 124 125 Layer* mLayer; 126 uint32_t mWidth; 127 uint32_t mHeight; 128 }; // struct LayerEntry 129 130 SortedList<LayerEntry> mCache; 131 132 uint32_t mSize; 133 uint32_t mMaxSize; 134}; // class LayerCache 135 136}; // namespace uirenderer 137}; // namespace android 138 139#endif // ANDROID_UI_LAYER_CACHE_H 140