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