blob: 42e6d9bb8cda35c3afd10564cd24fc0a6293207d [file] [log] [blame]
Romain Guyce0537b2010-06-29 21:05:21 -07001/*
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 Guy5b3b3522010-10-27 18:57:51 -070017#ifndef ANDROID_HWUI_GENERATION_CACHE_H
18#define ANDROID_HWUI_GENERATION_CACHE_H
Romain Guyce0537b2010-06-29 21:05:21 -070019
20#include <utils/KeyedVector.h>
21#include <utils/RefBase.h>
22
23namespace android {
24namespace uirenderer {
25
26template<typename EntryKey, typename EntryValue>
27class OnEntryRemoved {
28public:
29 virtual ~OnEntryRemoved() { };
Romain Guydda570202010-07-06 11:39:32 -070030 virtual void operator()(EntryKey& key, EntryValue& value) = 0;
Romain Guyce0537b2010-06-29 21:05:21 -070031}; // class OnEntryRemoved
32
Romain Guy5f0c6a42010-07-07 13:06:26 -070033template<typename EntryKey, typename EntryValue>
34struct Entry: public LightRefBase<Entry<EntryKey, EntryValue> > {
35 Entry() { }
36 Entry(const Entry<EntryKey, EntryValue>& e):
Romain Guyae5575b2010-07-29 18:48:04 -070037 key(e.key), value(e.value), parent(e.parent), child(e.child) { }
Romain Guy5f0c6a42010-07-07 13:06:26 -070038 Entry(sp<Entry<EntryKey, EntryValue> > e):
Romain Guyae5575b2010-07-29 18:48:04 -070039 key(e->key), value(e->value), parent(e->parent), child(e->child) { }
Romain Guy5f0c6a42010-07-07 13:06:26 -070040
41 EntryKey key;
42 EntryValue value;
Romain Guy5f0c6a42010-07-07 13:06:26 -070043
44 sp<Entry<EntryKey, EntryValue> > parent;
45 sp<Entry<EntryKey, EntryValue> > child;
46}; // struct Entry
47
Romain Guyce0537b2010-06-29 21:05:21 -070048template<typename K, typename V>
49class GenerationCache {
50public:
Romain Guy5f0c6a42010-07-07 13:06:26 -070051 GenerationCache(uint32_t maxCapacity);
52 virtual ~GenerationCache();
Romain Guyce0537b2010-06-29 21:05:21 -070053
Romain Guy121e22422010-07-01 18:26:52 -070054 enum Capacity {
55 kUnlimitedCapacity,
56 };
57
Romain Guydda570202010-07-06 11:39:32 -070058 void setOnEntryRemovedListener(OnEntryRemoved<K, V>* listener);
Romain Guyce0537b2010-06-29 21:05:21 -070059
60 void clear();
61
Romain Guydda570202010-07-06 11:39:32 -070062 bool contains(K key) const;
63 V get(K key);
Romain Guya2341a92010-09-08 18:04:33 -070064 K getKeyAt(uint32_t index) const;
Romain Guy8550c4c2010-10-08 15:49:53 -070065 bool put(K key, V value);
Romain Guydda570202010-07-06 11:39:32 -070066 V remove(K key);
67 V removeOldest();
Romain Guyeb993562010-10-05 18:14:38 -070068 V getValueAt(uint32_t index) const;
Romain Guyce0537b2010-06-29 21:05:21 -070069
Romain Guy7d139ba2010-07-02 11:20:34 -070070 uint32_t size() const;
Romain Guyce0537b2010-06-29 21:05:21 -070071
Romain Guydda570202010-07-06 11:39:32 -070072 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 Guyce0537b2010-06-29 21:05:21 -070075
Romain Guy5f0c6a42010-07-07 13:06:26 -070076 V removeAt(ssize_t index);
77
Romain Guy6c818932010-07-07 15:15:32 -070078 KeyedVector<K, sp<Entry<K, V> > > mCache;
Romain Guy7d139ba2010-07-02 11:20:34 -070079 uint32_t mMaxCapacity;
Romain Guyce0537b2010-06-29 21:05:21 -070080
Romain Guydda570202010-07-06 11:39:32 -070081 OnEntryRemoved<K, V>* mListener;
Romain Guyce0537b2010-06-29 21:05:21 -070082
Romain Guydda570202010-07-06 11:39:32 -070083 sp<Entry<K, V> > mOldest;
Romain Guy5f0c6a42010-07-07 13:06:26 -070084 sp<Entry<K, V> > mYoungest;
Romain Guyce0537b2010-06-29 21:05:21 -070085}; // class GenerationCache
86
87template<typename K, typename V>
Romain Guy5f0c6a42010-07-07 13:06:26 -070088GenerationCache<K, V>::GenerationCache(uint32_t maxCapacity): mMaxCapacity(maxCapacity), mListener(NULL) {
89};
90
91template<typename K, typename V>
92GenerationCache<K, V>::~GenerationCache() {
93 clear();
Romain Guy5f0c6a42010-07-07 13:06:26 -070094};
95
96template<typename K, typename V>
Romain Guy7d139ba2010-07-02 11:20:34 -070097uint32_t GenerationCache<K, V>::size() const {
Romain Guy6c818932010-07-07 15:15:32 -070098 return mCache.size();
Romain Guyce0537b2010-06-29 21:05:21 -070099}
100
101template<typename K, typename V>
Romain Guydda570202010-07-06 11:39:32 -0700102void GenerationCache<K, V>::setOnEntryRemovedListener(OnEntryRemoved<K, V>* listener) {
Romain Guyce0537b2010-06-29 21:05:21 -0700103 mListener = listener;
104}
105
106template<typename K, typename V>
107void GenerationCache<K, V>::clear() {
108 if (mListener) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700109 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 Guyce0537b2010-06-29 21:05:21 -0700114 }
Romain Guyce0537b2010-06-29 21:05:21 -0700115 }
Romain Guyfb8b7632010-08-23 21:05:08 -0700116 mCache.clear();
Romain Guy5f0c6a42010-07-07 13:06:26 -0700117 mYoungest.clear();
Romain Guyce0537b2010-06-29 21:05:21 -0700118 mOldest.clear();
119}
120
121template<typename K, typename V>
Romain Guydda570202010-07-06 11:39:32 -0700122bool GenerationCache<K, V>::contains(K key) const {
Romain Guy6c818932010-07-07 15:15:32 -0700123 return mCache.indexOfKey(key) >= 0;
Romain Guyce0537b2010-06-29 21:05:21 -0700124}
125
126template<typename K, typename V>
Romain Guya2341a92010-09-08 18:04:33 -0700127K GenerationCache<K, V>::getKeyAt(uint32_t index) const {
128 return mCache.keyAt(index);
129}
130
131template<typename K, typename V>
Romain Guyeb993562010-10-05 18:14:38 -0700132V GenerationCache<K, V>::getValueAt(uint32_t index) const {
Romain Guyfe48f652010-11-11 15:36:56 -0800133 return mCache.valueAt(index)->value;
Romain Guyeb993562010-10-05 18:14:38 -0700134}
135
136template<typename K, typename V>
Romain Guydda570202010-07-06 11:39:32 -0700137V GenerationCache<K, V>::get(K key) {
Romain Guy6c818932010-07-07 15:15:32 -0700138 ssize_t index = mCache.indexOfKey(key);
Romain Guyce0537b2010-06-29 21:05:21 -0700139 if (index >= 0) {
Romain Guy6c818932010-07-07 15:15:32 -0700140 sp<Entry<K, V> > entry = mCache.valueAt(index);
Romain Guyce0537b2010-06-29 21:05:21 -0700141 if (entry.get()) {
142 detachFromCache(entry);
143 attachToCache(entry);
144 return entry->value;
145 }
146 }
147
148 return NULL;
149}
150
151template<typename K, typename V>
Romain Guy8550c4c2010-10-08 15:49:53 -0700152bool GenerationCache<K, V>::put(K key, V value) {
Romain Guy6c818932010-07-07 15:15:32 -0700153 if (mMaxCapacity != kUnlimitedCapacity && mCache.size() >= mMaxCapacity) {
Romain Guyce0537b2010-06-29 21:05:21 -0700154 removeOldest();
155 }
156
Romain Guy6c818932010-07-07 15:15:32 -0700157 ssize_t index = mCache.indexOfKey(key);
Romain Guy9aaa8262010-09-08 15:15:43 -0700158 if (index < 0) {
Romain Guydda570202010-07-06 11:39:32 -0700159 sp<Entry<K, V> > entry = new Entry<K, V>;
Romain Guyce0537b2010-06-29 21:05:21 -0700160 addToCache(entry, key, value);
Romain Guy8550c4c2010-10-08 15:49:53 -0700161 return true;
Romain Guyce0537b2010-06-29 21:05:21 -0700162 }
Romain Guy8550c4c2010-10-08 15:49:53 -0700163
164 return false;
Romain Guyce0537b2010-06-29 21:05:21 -0700165}
166
167template<typename K, typename V>
Romain Guydda570202010-07-06 11:39:32 -0700168void GenerationCache<K, V>::addToCache(sp<Entry<K, V> > entry, K key, V value) {
Romain Guyce0537b2010-06-29 21:05:21 -0700169 entry->key = key;
170 entry->value = value;
Romain Guyae5575b2010-07-29 18:48:04 -0700171 mCache.add(key, entry);
Romain Guyce0537b2010-06-29 21:05:21 -0700172 attachToCache(entry);
173}
174
175template<typename K, typename V>
Romain Guydda570202010-07-06 11:39:32 -0700176V GenerationCache<K, V>::remove(K key) {
Romain Guy6c818932010-07-07 15:15:32 -0700177 ssize_t index = mCache.indexOfKey(key);
Romain Guyce0537b2010-06-29 21:05:21 -0700178 if (index >= 0) {
Romain Guy5f0c6a42010-07-07 13:06:26 -0700179 return removeAt(index);
Romain Guyce0537b2010-06-29 21:05:21 -0700180 }
181
182 return NULL;
183}
184
185template<typename K, typename V>
Romain Guy5f0c6a42010-07-07 13:06:26 -0700186V GenerationCache<K, V>::removeAt(ssize_t index) {
Romain Guy6c818932010-07-07 15:15:32 -0700187 sp<Entry<K, V> > entry = mCache.valueAt(index);
Romain Guy5f0c6a42010-07-07 13:06:26 -0700188 if (mListener) {
189 (*mListener)(entry->key, entry->value);
190 }
Romain Guy6c818932010-07-07 15:15:32 -0700191 mCache.removeItemsAt(index, 1);
Romain Guy5f0c6a42010-07-07 13:06:26 -0700192 detachFromCache(entry);
193
194 return entry->value;
195}
196
197template<typename K, typename V>
Romain Guydda570202010-07-06 11:39:32 -0700198V GenerationCache<K, V>::removeOldest() {
Romain Guyce0537b2010-06-29 21:05:21 -0700199 if (mOldest.get()) {
Romain Guyae5575b2010-07-29 18:48:04 -0700200 ssize_t index = mCache.indexOfKey(mOldest->key);
201 if (index >= 0) {
202 return removeAt(index);
203 }
Romain Guyce0537b2010-06-29 21:05:21 -0700204 }
Romain Guydda570202010-07-06 11:39:32 -0700205
206 return NULL;
Romain Guyce0537b2010-06-29 21:05:21 -0700207}
208
209template<typename K, typename V>
Romain Guydda570202010-07-06 11:39:32 -0700210void GenerationCache<K, V>::attachToCache(sp<Entry<K, V> > entry) {
Romain Guy5f0c6a42010-07-07 13:06:26 -0700211 if (!mYoungest.get()) {
212 mYoungest = mOldest = entry;
Romain Guyce0537b2010-06-29 21:05:21 -0700213 } else {
Romain Guy5f0c6a42010-07-07 13:06:26 -0700214 entry->parent = mYoungest;
215 mYoungest->child = entry;
216 mYoungest = entry;
Romain Guyce0537b2010-06-29 21:05:21 -0700217 }
218}
219
220template<typename K, typename V>
Romain Guydda570202010-07-06 11:39:32 -0700221void GenerationCache<K, V>::detachFromCache(sp<Entry<K, V> > entry) {
Romain Guyce0537b2010-06-29 21:05:21 -0700222 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 Guy5f0c6a42010-07-07 13:06:26 -0700234 if (mYoungest == entry) {
235 mYoungest = entry->parent;
Romain Guyce0537b2010-06-29 21:05:21 -0700236 }
237
238 entry->parent.clear();
239 entry->child.clear();
240}
241
242}; // namespace uirenderer
243}; // namespace android
244
Romain Guy5b3b3522010-10-27 18:57:51 -0700245#endif // ANDROID_HWUI_GENERATION_CACHE_H