Romain Guy | ce0537b | 2010-06-29 21:05:21 -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_GENERATION_CACHE_H |
| 18 | #define ANDROID_UI_GENERATION_CACHE_H |
| 19 | |
| 20 | #include <utils/KeyedVector.h> |
| 21 | #include <utils/RefBase.h> |
| 22 | |
Romain Guy | 5f0c6a4 | 2010-07-07 13:06:26 -0700 | [diff] [blame] | 23 | #include "SortedList.h" |
| 24 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 25 | namespace android { |
| 26 | namespace uirenderer { |
| 27 | |
Romain Guy | 5f0c6a4 | 2010-07-07 13:06:26 -0700 | [diff] [blame] | 28 | template<typename K, typename V> |
| 29 | class GenerationCacheStorage { |
| 30 | public: |
| 31 | virtual ~GenerationCacheStorage(); |
| 32 | |
| 33 | virtual size_t size() const = 0; |
| 34 | virtual void clear() = 0; |
| 35 | virtual ssize_t add(const K& key, const V& item) = 0; |
| 36 | virtual ssize_t indexOfKey(const K& key) const = 0; |
| 37 | virtual const V& valueAt(size_t index) const = 0; |
| 38 | virtual ssize_t removeItemsAt(size_t index, size_t count) = 0; |
| 39 | }; // GenerationCacheStorage |
| 40 | |
| 41 | template<typename K, typename V> |
| 42 | GenerationCacheStorage<K, V>::~GenerationCacheStorage() { |
| 43 | } |
| 44 | |
| 45 | template<typename K, typename V> |
| 46 | class KeyedVectorStorage: public GenerationCacheStorage<K, V> { |
| 47 | public: |
| 48 | KeyedVectorStorage() { } |
| 49 | ~KeyedVectorStorage() { } |
| 50 | |
| 51 | inline size_t size() const { return mStorage.size(); } |
| 52 | inline void clear() { mStorage.clear(); } |
| 53 | inline ssize_t add(const K& key, const V& value) { return mStorage.add(key, value); } |
| 54 | inline ssize_t indexOfKey(const K& key) const { return mStorage.indexOfKey(key); } |
| 55 | inline const V& valueAt(size_t index) const { return mStorage.valueAt(index); } |
| 56 | inline ssize_t removeItemsAt(size_t index, size_t count) { |
| 57 | return mStorage.removeItemsAt(index, count); |
| 58 | } |
| 59 | private: |
| 60 | KeyedVector<K, V> mStorage; |
| 61 | }; // class KeyedVectorStorage |
| 62 | |
| 63 | template<typename K, typename V> |
| 64 | class SortedListStorage: public GenerationCacheStorage<K, V> { |
| 65 | public: |
| 66 | SortedListStorage() { } |
| 67 | ~SortedListStorage() { } |
| 68 | |
| 69 | inline size_t size() const { return mStorage.size(); } |
| 70 | inline void clear() { mStorage.clear(); } |
| 71 | inline ssize_t add(const K& key, const V& value) { |
| 72 | return mStorage.add(key_value_pair_t<K, V>(key, value)); |
| 73 | } |
| 74 | inline ssize_t indexOfKey(const K& key) const { |
| 75 | return mStorage.indexOf(key_value_pair_t<K, V>(key)); |
| 76 | } |
| 77 | inline const V& valueAt(size_t index) const { return mStorage.itemAt(index).value; } |
| 78 | inline ssize_t removeItemsAt(size_t index, size_t count) { |
| 79 | return mStorage.removeItemsAt(index, count); |
| 80 | } |
| 81 | private: |
| 82 | SortedList<key_value_pair_t<K, V> > mStorage; |
| 83 | }; // class SortedListStorage |
| 84 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 85 | template<typename EntryKey, typename EntryValue> |
| 86 | class OnEntryRemoved { |
| 87 | public: |
| 88 | virtual ~OnEntryRemoved() { }; |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 89 | virtual void operator()(EntryKey& key, EntryValue& value) = 0; |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 90 | }; // class OnEntryRemoved |
| 91 | |
Romain Guy | 5f0c6a4 | 2010-07-07 13:06:26 -0700 | [diff] [blame] | 92 | template<typename EntryKey, typename EntryValue> |
| 93 | struct Entry: public LightRefBase<Entry<EntryKey, EntryValue> > { |
| 94 | Entry() { } |
| 95 | Entry(const Entry<EntryKey, EntryValue>& e): |
| 96 | key(e.key), value(e.value), index(e.index), parent(e.parent), child(e.child) { } |
| 97 | Entry(sp<Entry<EntryKey, EntryValue> > e): |
| 98 | key(e->key), value(e->value), index(e->index), parent(e->parent), child(e->child) { } |
| 99 | |
| 100 | EntryKey key; |
| 101 | EntryValue value; |
| 102 | ssize_t index; |
| 103 | |
| 104 | sp<Entry<EntryKey, EntryValue> > parent; |
| 105 | sp<Entry<EntryKey, EntryValue> > child; |
| 106 | }; // struct Entry |
| 107 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 108 | template<typename K, typename V> |
| 109 | class GenerationCache { |
| 110 | public: |
Romain Guy | 5f0c6a4 | 2010-07-07 13:06:26 -0700 | [diff] [blame] | 111 | GenerationCache(uint32_t maxCapacity); |
| 112 | virtual ~GenerationCache(); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 113 | |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 114 | enum Capacity { |
| 115 | kUnlimitedCapacity, |
| 116 | }; |
| 117 | |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 118 | void setOnEntryRemovedListener(OnEntryRemoved<K, V>* listener); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 119 | |
| 120 | void clear(); |
| 121 | |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 122 | bool contains(K key) const; |
| 123 | V get(K key); |
| 124 | void put(K key, V value); |
| 125 | V remove(K key); |
| 126 | V removeOldest(); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 127 | |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 128 | uint32_t size() const; |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 129 | |
Romain Guy | 5f0c6a4 | 2010-07-07 13:06:26 -0700 | [diff] [blame] | 130 | protected: |
| 131 | virtual GenerationCacheStorage<K, sp<Entry<K, V> > >* createStorage() = 0; |
| 132 | GenerationCacheStorage<K, sp<Entry<K, V> > >* mCache; |
| 133 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 134 | private: |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 135 | void addToCache(sp<Entry<K, V> > entry, K key, V value); |
| 136 | void attachToCache(sp<Entry<K, V> > entry); |
| 137 | void detachFromCache(sp<Entry<K, V> > entry); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 138 | |
Romain Guy | 5f0c6a4 | 2010-07-07 13:06:26 -0700 | [diff] [blame] | 139 | V removeAt(ssize_t index); |
| 140 | |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 141 | uint32_t mMaxCapacity; |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 142 | |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 143 | OnEntryRemoved<K, V>* mListener; |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 144 | |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 145 | sp<Entry<K, V> > mOldest; |
Romain Guy | 5f0c6a4 | 2010-07-07 13:06:26 -0700 | [diff] [blame] | 146 | sp<Entry<K, V> > mYoungest; |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 147 | }; // class GenerationCache |
| 148 | |
| 149 | template<typename K, typename V> |
Romain Guy | 5f0c6a4 | 2010-07-07 13:06:26 -0700 | [diff] [blame] | 150 | class GenerationSingleCache: public GenerationCache<K, V> { |
| 151 | public: |
| 152 | GenerationSingleCache(uint32_t maxCapacity): GenerationCache<K, V>(maxCapacity) { |
| 153 | GenerationCache<K, V>::mCache = createStorage(); |
| 154 | }; |
| 155 | ~GenerationSingleCache() { } |
| 156 | protected: |
| 157 | GenerationCacheStorage<K, sp<Entry<K, V> > >* createStorage() { |
| 158 | return new KeyedVectorStorage<K, sp<Entry<K, V> > >; |
| 159 | } |
| 160 | }; // GenerationSingleCache |
| 161 | |
| 162 | template<typename K, typename V> |
| 163 | class GenerationMultiCache: public GenerationCache<K, V> { |
| 164 | public: |
| 165 | GenerationMultiCache(uint32_t maxCapacity): GenerationCache<K, V>(maxCapacity) { |
| 166 | GenerationCache<K, V>::mCache = createStorage(); |
| 167 | }; |
| 168 | ~GenerationMultiCache() { } |
| 169 | protected: |
| 170 | GenerationCacheStorage<K, sp<Entry<K, V> > >* createStorage() { |
| 171 | return new SortedListStorage<K, sp<Entry<K, V> > >; |
| 172 | } |
| 173 | }; // GenerationMultiCache |
| 174 | |
| 175 | template<typename K, typename V> |
| 176 | GenerationCache<K, V>::GenerationCache(uint32_t maxCapacity): mMaxCapacity(maxCapacity), mListener(NULL) { |
| 177 | }; |
| 178 | |
| 179 | template<typename K, typename V> |
| 180 | GenerationCache<K, V>::~GenerationCache() { |
| 181 | clear(); |
| 182 | delete mCache; |
| 183 | }; |
| 184 | |
| 185 | template<typename K, typename V> |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 186 | uint32_t GenerationCache<K, V>::size() const { |
Romain Guy | 5f0c6a4 | 2010-07-07 13:06:26 -0700 | [diff] [blame] | 187 | return mCache->size(); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | template<typename K, typename V> |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 191 | void GenerationCache<K, V>::setOnEntryRemovedListener(OnEntryRemoved<K, V>* listener) { |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 192 | mListener = listener; |
| 193 | } |
| 194 | |
| 195 | template<typename K, typename V> |
| 196 | void GenerationCache<K, V>::clear() { |
| 197 | if (mListener) { |
Romain Guy | 5f0c6a4 | 2010-07-07 13:06:26 -0700 | [diff] [blame] | 198 | while (mCache->size() > 0) { |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 199 | removeOldest(); |
| 200 | } |
| 201 | } else { |
Romain Guy | 5f0c6a4 | 2010-07-07 13:06:26 -0700 | [diff] [blame] | 202 | mCache->clear(); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 203 | } |
Romain Guy | 5f0c6a4 | 2010-07-07 13:06:26 -0700 | [diff] [blame] | 204 | mYoungest.clear(); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 205 | mOldest.clear(); |
| 206 | } |
| 207 | |
| 208 | template<typename K, typename V> |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 209 | bool GenerationCache<K, V>::contains(K key) const { |
Romain Guy | 5f0c6a4 | 2010-07-07 13:06:26 -0700 | [diff] [blame] | 210 | return mCache->indexOfKey(key) >= 0; |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | template<typename K, typename V> |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 214 | V GenerationCache<K, V>::get(K key) { |
Romain Guy | 5f0c6a4 | 2010-07-07 13:06:26 -0700 | [diff] [blame] | 215 | ssize_t index = mCache->indexOfKey(key); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 216 | if (index >= 0) { |
Romain Guy | 5f0c6a4 | 2010-07-07 13:06:26 -0700 | [diff] [blame] | 217 | sp<Entry<K, V> > entry = mCache->valueAt(index); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 218 | if (entry.get()) { |
| 219 | detachFromCache(entry); |
| 220 | attachToCache(entry); |
| 221 | return entry->value; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | return NULL; |
| 226 | } |
| 227 | |
| 228 | template<typename K, typename V> |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 229 | void GenerationCache<K, V>::put(K key, V value) { |
Romain Guy | 5f0c6a4 | 2010-07-07 13:06:26 -0700 | [diff] [blame] | 230 | if (mMaxCapacity != kUnlimitedCapacity && mCache->size() >= mMaxCapacity) { |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 231 | removeOldest(); |
| 232 | } |
| 233 | |
Romain Guy | 5f0c6a4 | 2010-07-07 13:06:26 -0700 | [diff] [blame] | 234 | ssize_t index = mCache->indexOfKey(key); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 235 | if (index >= 0) { |
Romain Guy | 5f0c6a4 | 2010-07-07 13:06:26 -0700 | [diff] [blame] | 236 | sp<Entry<K, V> > entry = mCache->valueAt(index); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 237 | detachFromCache(entry); |
| 238 | addToCache(entry, key, value); |
| 239 | } else { |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 240 | sp<Entry<K, V> > entry = new Entry<K, V>; |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 241 | addToCache(entry, key, value); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | template<typename K, typename V> |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 246 | void GenerationCache<K, V>::addToCache(sp<Entry<K, V> > entry, K key, V value) { |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 247 | entry->key = key; |
| 248 | entry->value = value; |
Romain Guy | 5f0c6a4 | 2010-07-07 13:06:26 -0700 | [diff] [blame] | 249 | entry->index = mCache->add(key, entry); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 250 | attachToCache(entry); |
| 251 | } |
| 252 | |
| 253 | template<typename K, typename V> |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 254 | V GenerationCache<K, V>::remove(K key) { |
Romain Guy | 5f0c6a4 | 2010-07-07 13:06:26 -0700 | [diff] [blame] | 255 | ssize_t index = mCache->indexOfKey(key); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 256 | if (index >= 0) { |
Romain Guy | 5f0c6a4 | 2010-07-07 13:06:26 -0700 | [diff] [blame] | 257 | return removeAt(index); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | return NULL; |
| 261 | } |
| 262 | |
| 263 | template<typename K, typename V> |
Romain Guy | 5f0c6a4 | 2010-07-07 13:06:26 -0700 | [diff] [blame] | 264 | V GenerationCache<K, V>::removeAt(ssize_t index) { |
| 265 | sp<Entry<K, V> > entry = mCache->valueAt(index); |
| 266 | if (mListener) { |
| 267 | (*mListener)(entry->key, entry->value); |
| 268 | } |
| 269 | mCache->removeItemsAt(index, 1); |
| 270 | detachFromCache(entry); |
| 271 | |
| 272 | return entry->value; |
| 273 | } |
| 274 | |
| 275 | template<typename K, typename V> |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 276 | V GenerationCache<K, V>::removeOldest() { |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 277 | if (mOldest.get()) { |
Romain Guy | 5f0c6a4 | 2010-07-07 13:06:26 -0700 | [diff] [blame] | 278 | return removeAt(mOldest->index); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 279 | } |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 280 | |
| 281 | return NULL; |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | template<typename K, typename V> |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 285 | void GenerationCache<K, V>::attachToCache(sp<Entry<K, V> > entry) { |
Romain Guy | 5f0c6a4 | 2010-07-07 13:06:26 -0700 | [diff] [blame] | 286 | if (!mYoungest.get()) { |
| 287 | mYoungest = mOldest = entry; |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 288 | } else { |
Romain Guy | 5f0c6a4 | 2010-07-07 13:06:26 -0700 | [diff] [blame] | 289 | entry->parent = mYoungest; |
| 290 | mYoungest->child = entry; |
| 291 | mYoungest = entry; |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | |
| 295 | template<typename K, typename V> |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 296 | void GenerationCache<K, V>::detachFromCache(sp<Entry<K, V> > entry) { |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 297 | if (entry->parent.get()) { |
| 298 | entry->parent->child = entry->child; |
| 299 | } |
| 300 | |
| 301 | if (entry->child.get()) { |
| 302 | entry->child->parent = entry->parent; |
| 303 | } |
| 304 | |
| 305 | if (mOldest == entry) { |
| 306 | mOldest = entry->child; |
| 307 | } |
| 308 | |
Romain Guy | 5f0c6a4 | 2010-07-07 13:06:26 -0700 | [diff] [blame] | 309 | if (mYoungest == entry) { |
| 310 | mYoungest = entry->parent; |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | entry->parent.clear(); |
| 314 | entry->child.clear(); |
| 315 | } |
| 316 | |
| 317 | }; // namespace uirenderer |
| 318 | }; // namespace android |
| 319 | |
| 320 | #endif // ANDROID_UI_GENERATION_CACHE_H |