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 | |
| 23 | namespace android { |
| 24 | namespace uirenderer { |
| 25 | |
| 26 | template<typename EntryKey, typename EntryValue> |
| 27 | class OnEntryRemoved { |
| 28 | public: |
| 29 | virtual ~OnEntryRemoved() { }; |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 30 | virtual void operator()(EntryKey& key, EntryValue& value) = 0; |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 31 | }; // class OnEntryRemoved |
| 32 | |
Romain Guy | 5f0c6a4 | 2010-07-07 13:06:26 -0700 | [diff] [blame] | 33 | template<typename EntryKey, typename EntryValue> |
| 34 | struct Entry: public LightRefBase<Entry<EntryKey, EntryValue> > { |
| 35 | Entry() { } |
| 36 | Entry(const Entry<EntryKey, EntryValue>& e): |
Romain Guy | ae5575b | 2010-07-29 18:48:04 -0700 | [diff] [blame] | 37 | key(e.key), value(e.value), parent(e.parent), child(e.child) { } |
Romain Guy | 5f0c6a4 | 2010-07-07 13:06:26 -0700 | [diff] [blame] | 38 | Entry(sp<Entry<EntryKey, EntryValue> > e): |
Romain Guy | ae5575b | 2010-07-29 18:48:04 -0700 | [diff] [blame] | 39 | key(e->key), value(e->value), parent(e->parent), child(e->child) { } |
Romain Guy | 5f0c6a4 | 2010-07-07 13:06:26 -0700 | [diff] [blame] | 40 | |
| 41 | EntryKey key; |
| 42 | EntryValue value; |
Romain Guy | 5f0c6a4 | 2010-07-07 13:06:26 -0700 | [diff] [blame] | 43 | |
| 44 | sp<Entry<EntryKey, EntryValue> > parent; |
| 45 | sp<Entry<EntryKey, EntryValue> > child; |
| 46 | }; // struct Entry |
| 47 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 48 | template<typename K, typename V> |
| 49 | class GenerationCache { |
| 50 | public: |
Romain Guy | 5f0c6a4 | 2010-07-07 13:06:26 -0700 | [diff] [blame] | 51 | GenerationCache(uint32_t maxCapacity); |
| 52 | virtual ~GenerationCache(); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 53 | |
Romain Guy | 121e224 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 54 | enum Capacity { |
| 55 | kUnlimitedCapacity, |
| 56 | }; |
| 57 | |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 58 | void setOnEntryRemovedListener(OnEntryRemoved<K, V>* listener); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 59 | |
| 60 | void clear(); |
| 61 | |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 62 | bool contains(K key) const; |
| 63 | V get(K key); |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 64 | K getKeyAt(uint32_t index) const; |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 65 | void put(K key, V value); |
| 66 | V remove(K key); |
| 67 | V removeOldest(); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 68 | |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 69 | uint32_t size() const; |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 70 | |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 71 | void addToCache(sp<Entry<K, V> > entry, K key, V value); |
| 72 | void attachToCache(sp<Entry<K, V> > entry); |
| 73 | void detachFromCache(sp<Entry<K, V> > entry); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 74 | |
Romain Guy | 5f0c6a4 | 2010-07-07 13:06:26 -0700 | [diff] [blame] | 75 | V removeAt(ssize_t index); |
| 76 | |
Romain Guy | 6c81893 | 2010-07-07 15:15:32 -0700 | [diff] [blame] | 77 | KeyedVector<K, sp<Entry<K, V> > > mCache; |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 78 | uint32_t mMaxCapacity; |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 79 | |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 80 | OnEntryRemoved<K, V>* mListener; |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 81 | |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 82 | sp<Entry<K, V> > mOldest; |
Romain Guy | 5f0c6a4 | 2010-07-07 13:06:26 -0700 | [diff] [blame] | 83 | sp<Entry<K, V> > mYoungest; |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 84 | }; // class GenerationCache |
| 85 | |
| 86 | template<typename K, typename V> |
Romain Guy | 5f0c6a4 | 2010-07-07 13:06:26 -0700 | [diff] [blame] | 87 | GenerationCache<K, V>::GenerationCache(uint32_t maxCapacity): mMaxCapacity(maxCapacity), mListener(NULL) { |
| 88 | }; |
| 89 | |
| 90 | template<typename K, typename V> |
| 91 | GenerationCache<K, V>::~GenerationCache() { |
| 92 | clear(); |
Romain Guy | 5f0c6a4 | 2010-07-07 13:06:26 -0700 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | template<typename K, typename V> |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 96 | uint32_t GenerationCache<K, V>::size() const { |
Romain Guy | 6c81893 | 2010-07-07 15:15:32 -0700 | [diff] [blame] | 97 | return mCache.size(); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | template<typename K, typename V> |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 101 | void GenerationCache<K, V>::setOnEntryRemovedListener(OnEntryRemoved<K, V>* listener) { |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 102 | mListener = listener; |
| 103 | } |
| 104 | |
| 105 | template<typename K, typename V> |
| 106 | void GenerationCache<K, V>::clear() { |
| 107 | if (mListener) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 108 | for (uint32_t i = 0; i < mCache.size(); i++) { |
| 109 | sp<Entry<K, V> > entry = mCache.valueAt(i); |
| 110 | if (mListener) { |
| 111 | (*mListener)(entry->key, entry->value); |
| 112 | } |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 113 | } |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 114 | } |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 115 | mCache.clear(); |
Romain Guy | 5f0c6a4 | 2010-07-07 13:06:26 -0700 | [diff] [blame] | 116 | mYoungest.clear(); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 117 | mOldest.clear(); |
| 118 | } |
| 119 | |
| 120 | template<typename K, typename V> |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 121 | bool GenerationCache<K, V>::contains(K key) const { |
Romain Guy | 6c81893 | 2010-07-07 15:15:32 -0700 | [diff] [blame] | 122 | return mCache.indexOfKey(key) >= 0; |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | template<typename K, typename V> |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 126 | K GenerationCache<K, V>::getKeyAt(uint32_t index) const { |
| 127 | return mCache.keyAt(index); |
| 128 | } |
| 129 | |
| 130 | template<typename K, typename V> |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 131 | V GenerationCache<K, V>::get(K key) { |
Romain Guy | 6c81893 | 2010-07-07 15:15:32 -0700 | [diff] [blame] | 132 | ssize_t index = mCache.indexOfKey(key); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 133 | if (index >= 0) { |
Romain Guy | 6c81893 | 2010-07-07 15:15:32 -0700 | [diff] [blame] | 134 | sp<Entry<K, V> > entry = mCache.valueAt(index); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 135 | if (entry.get()) { |
| 136 | detachFromCache(entry); |
| 137 | attachToCache(entry); |
| 138 | return entry->value; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | return NULL; |
| 143 | } |
| 144 | |
| 145 | template<typename K, typename V> |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 146 | void GenerationCache<K, V>::put(K key, V value) { |
Romain Guy | 6c81893 | 2010-07-07 15:15:32 -0700 | [diff] [blame] | 147 | if (mMaxCapacity != kUnlimitedCapacity && mCache.size() >= mMaxCapacity) { |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 148 | removeOldest(); |
| 149 | } |
| 150 | |
Romain Guy | 6c81893 | 2010-07-07 15:15:32 -0700 | [diff] [blame] | 151 | ssize_t index = mCache.indexOfKey(key); |
Romain Guy | 9aaa826 | 2010-09-08 15:15:43 -0700 | [diff] [blame] | 152 | if (index < 0) { |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 153 | sp<Entry<K, V> > entry = new Entry<K, V>; |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 154 | addToCache(entry, key, value); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | template<typename K, typename V> |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 159 | 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] | 160 | entry->key = key; |
| 161 | entry->value = value; |
Romain Guy | ae5575b | 2010-07-29 18:48:04 -0700 | [diff] [blame] | 162 | mCache.add(key, entry); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 163 | attachToCache(entry); |
| 164 | } |
| 165 | |
| 166 | template<typename K, typename V> |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 167 | V GenerationCache<K, V>::remove(K key) { |
Romain Guy | 6c81893 | 2010-07-07 15:15:32 -0700 | [diff] [blame] | 168 | ssize_t index = mCache.indexOfKey(key); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 169 | if (index >= 0) { |
Romain Guy | 5f0c6a4 | 2010-07-07 13:06:26 -0700 | [diff] [blame] | 170 | return removeAt(index); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | return NULL; |
| 174 | } |
| 175 | |
| 176 | template<typename K, typename V> |
Romain Guy | 5f0c6a4 | 2010-07-07 13:06:26 -0700 | [diff] [blame] | 177 | V GenerationCache<K, V>::removeAt(ssize_t index) { |
Romain Guy | 6c81893 | 2010-07-07 15:15:32 -0700 | [diff] [blame] | 178 | sp<Entry<K, V> > entry = mCache.valueAt(index); |
Romain Guy | 5f0c6a4 | 2010-07-07 13:06:26 -0700 | [diff] [blame] | 179 | if (mListener) { |
| 180 | (*mListener)(entry->key, entry->value); |
| 181 | } |
Romain Guy | 6c81893 | 2010-07-07 15:15:32 -0700 | [diff] [blame] | 182 | mCache.removeItemsAt(index, 1); |
Romain Guy | 5f0c6a4 | 2010-07-07 13:06:26 -0700 | [diff] [blame] | 183 | detachFromCache(entry); |
| 184 | |
| 185 | return entry->value; |
| 186 | } |
| 187 | |
| 188 | template<typename K, typename V> |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 189 | V GenerationCache<K, V>::removeOldest() { |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 190 | if (mOldest.get()) { |
Romain Guy | ae5575b | 2010-07-29 18:48:04 -0700 | [diff] [blame] | 191 | ssize_t index = mCache.indexOfKey(mOldest->key); |
| 192 | if (index >= 0) { |
| 193 | return removeAt(index); |
| 194 | } |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 195 | } |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 196 | |
| 197 | return NULL; |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | template<typename K, typename V> |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 201 | void GenerationCache<K, V>::attachToCache(sp<Entry<K, V> > entry) { |
Romain Guy | 5f0c6a4 | 2010-07-07 13:06:26 -0700 | [diff] [blame] | 202 | if (!mYoungest.get()) { |
| 203 | mYoungest = mOldest = entry; |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 204 | } else { |
Romain Guy | 5f0c6a4 | 2010-07-07 13:06:26 -0700 | [diff] [blame] | 205 | entry->parent = mYoungest; |
| 206 | mYoungest->child = entry; |
| 207 | mYoungest = entry; |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | |
| 211 | template<typename K, typename V> |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 212 | void GenerationCache<K, V>::detachFromCache(sp<Entry<K, V> > entry) { |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 213 | if (entry->parent.get()) { |
| 214 | entry->parent->child = entry->child; |
| 215 | } |
| 216 | |
| 217 | if (entry->child.get()) { |
| 218 | entry->child->parent = entry->parent; |
| 219 | } |
| 220 | |
| 221 | if (mOldest == entry) { |
| 222 | mOldest = entry->child; |
| 223 | } |
| 224 | |
Romain Guy | 5f0c6a4 | 2010-07-07 13:06:26 -0700 | [diff] [blame] | 225 | if (mYoungest == entry) { |
| 226 | mYoungest = entry->parent; |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | entry->parent.clear(); |
| 230 | entry->child.clear(); |
| 231 | } |
| 232 | |
| 233 | }; // namespace uirenderer |
| 234 | }; // namespace android |
| 235 | |
| 236 | #endif // ANDROID_UI_GENERATION_CACHE_H |