blob: c358c8096cfdd7efe570c21788846e9af6271513 [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
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
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);
64 void put(K key, V value);
65 V remove(K key);
66 V removeOldest();
Romain Guyce0537b2010-06-29 21:05:21 -070067
Romain Guy7d139ba2010-07-02 11:20:34 -070068 uint32_t size() const;
Romain Guyce0537b2010-06-29 21:05:21 -070069
Romain Guydda570202010-07-06 11:39:32 -070070 void addToCache(sp<Entry<K, V> > entry, K key, V value);
71 void attachToCache(sp<Entry<K, V> > entry);
72 void detachFromCache(sp<Entry<K, V> > entry);
Romain Guyce0537b2010-06-29 21:05:21 -070073
Romain Guy5f0c6a42010-07-07 13:06:26 -070074 V removeAt(ssize_t index);
75
Romain Guy6c818932010-07-07 15:15:32 -070076 KeyedVector<K, sp<Entry<K, V> > > mCache;
Romain Guy7d139ba2010-07-02 11:20:34 -070077 uint32_t mMaxCapacity;
Romain Guyce0537b2010-06-29 21:05:21 -070078
Romain Guydda570202010-07-06 11:39:32 -070079 OnEntryRemoved<K, V>* mListener;
Romain Guyce0537b2010-06-29 21:05:21 -070080
Romain Guydda570202010-07-06 11:39:32 -070081 sp<Entry<K, V> > mOldest;
Romain Guy5f0c6a42010-07-07 13:06:26 -070082 sp<Entry<K, V> > mYoungest;
Romain Guyce0537b2010-06-29 21:05:21 -070083}; // class GenerationCache
84
85template<typename K, typename V>
Romain Guy5f0c6a42010-07-07 13:06:26 -070086GenerationCache<K, V>::GenerationCache(uint32_t maxCapacity): mMaxCapacity(maxCapacity), mListener(NULL) {
87};
88
89template<typename K, typename V>
90GenerationCache<K, V>::~GenerationCache() {
91 clear();
Romain Guy5f0c6a42010-07-07 13:06:26 -070092};
93
94template<typename K, typename V>
Romain Guy7d139ba2010-07-02 11:20:34 -070095uint32_t GenerationCache<K, V>::size() const {
Romain Guy6c818932010-07-07 15:15:32 -070096 return mCache.size();
Romain Guyce0537b2010-06-29 21:05:21 -070097}
98
99template<typename K, typename V>
Romain Guydda570202010-07-06 11:39:32 -0700100void GenerationCache<K, V>::setOnEntryRemovedListener(OnEntryRemoved<K, V>* listener) {
Romain Guyce0537b2010-06-29 21:05:21 -0700101 mListener = listener;
102}
103
104template<typename K, typename V>
105void GenerationCache<K, V>::clear() {
106 if (mListener) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700107 for (uint32_t i = 0; i < mCache.size(); i++) {
108 sp<Entry<K, V> > entry = mCache.valueAt(i);
109 if (mListener) {
110 (*mListener)(entry->key, entry->value);
111 }
Romain Guyce0537b2010-06-29 21:05:21 -0700112 }
Romain Guyce0537b2010-06-29 21:05:21 -0700113 }
Romain Guyfb8b7632010-08-23 21:05:08 -0700114 mCache.clear();
Romain Guy5f0c6a42010-07-07 13:06:26 -0700115 mYoungest.clear();
Romain Guyce0537b2010-06-29 21:05:21 -0700116 mOldest.clear();
117}
118
119template<typename K, typename V>
Romain Guydda570202010-07-06 11:39:32 -0700120bool GenerationCache<K, V>::contains(K key) const {
Romain Guy6c818932010-07-07 15:15:32 -0700121 return mCache.indexOfKey(key) >= 0;
Romain Guyce0537b2010-06-29 21:05:21 -0700122}
123
124template<typename K, typename V>
Romain Guydda570202010-07-06 11:39:32 -0700125V GenerationCache<K, V>::get(K key) {
Romain Guy6c818932010-07-07 15:15:32 -0700126 ssize_t index = mCache.indexOfKey(key);
Romain Guyce0537b2010-06-29 21:05:21 -0700127 if (index >= 0) {
Romain Guy6c818932010-07-07 15:15:32 -0700128 sp<Entry<K, V> > entry = mCache.valueAt(index);
Romain Guyce0537b2010-06-29 21:05:21 -0700129 if (entry.get()) {
130 detachFromCache(entry);
131 attachToCache(entry);
132 return entry->value;
133 }
134 }
135
136 return NULL;
137}
138
139template<typename K, typename V>
Romain Guydda570202010-07-06 11:39:32 -0700140void GenerationCache<K, V>::put(K key, V value) {
Romain Guy6c818932010-07-07 15:15:32 -0700141 if (mMaxCapacity != kUnlimitedCapacity && mCache.size() >= mMaxCapacity) {
Romain Guyce0537b2010-06-29 21:05:21 -0700142 removeOldest();
143 }
144
Romain Guy6c818932010-07-07 15:15:32 -0700145 ssize_t index = mCache.indexOfKey(key);
Romain Guyce0537b2010-06-29 21:05:21 -0700146 if (index >= 0) {
Romain Guy6c818932010-07-07 15:15:32 -0700147 sp<Entry<K, V> > entry = mCache.valueAt(index);
Romain Guyce0537b2010-06-29 21:05:21 -0700148 detachFromCache(entry);
149 addToCache(entry, key, value);
150 } else {
Romain Guydda570202010-07-06 11:39:32 -0700151 sp<Entry<K, V> > entry = new Entry<K, V>;
Romain Guyce0537b2010-06-29 21:05:21 -0700152 addToCache(entry, key, value);
153 }
154}
155
156template<typename K, typename V>
Romain Guydda570202010-07-06 11:39:32 -0700157void GenerationCache<K, V>::addToCache(sp<Entry<K, V> > entry, K key, V value) {
Romain Guyce0537b2010-06-29 21:05:21 -0700158 entry->key = key;
159 entry->value = value;
Romain Guyae5575b2010-07-29 18:48:04 -0700160 mCache.add(key, entry);
Romain Guyce0537b2010-06-29 21:05:21 -0700161 attachToCache(entry);
162}
163
164template<typename K, typename V>
Romain Guydda570202010-07-06 11:39:32 -0700165V GenerationCache<K, V>::remove(K key) {
Romain Guy6c818932010-07-07 15:15:32 -0700166 ssize_t index = mCache.indexOfKey(key);
Romain Guyce0537b2010-06-29 21:05:21 -0700167 if (index >= 0) {
Romain Guy5f0c6a42010-07-07 13:06:26 -0700168 return removeAt(index);
Romain Guyce0537b2010-06-29 21:05:21 -0700169 }
170
171 return NULL;
172}
173
174template<typename K, typename V>
Romain Guy5f0c6a42010-07-07 13:06:26 -0700175V GenerationCache<K, V>::removeAt(ssize_t index) {
Romain Guy6c818932010-07-07 15:15:32 -0700176 sp<Entry<K, V> > entry = mCache.valueAt(index);
Romain Guy5f0c6a42010-07-07 13:06:26 -0700177 if (mListener) {
178 (*mListener)(entry->key, entry->value);
179 }
Romain Guy6c818932010-07-07 15:15:32 -0700180 mCache.removeItemsAt(index, 1);
Romain Guy5f0c6a42010-07-07 13:06:26 -0700181 detachFromCache(entry);
182
183 return entry->value;
184}
185
186template<typename K, typename V>
Romain Guydda570202010-07-06 11:39:32 -0700187V GenerationCache<K, V>::removeOldest() {
Romain Guyce0537b2010-06-29 21:05:21 -0700188 if (mOldest.get()) {
Romain Guyae5575b2010-07-29 18:48:04 -0700189 ssize_t index = mCache.indexOfKey(mOldest->key);
190 if (index >= 0) {
191 return removeAt(index);
192 }
Romain Guyce0537b2010-06-29 21:05:21 -0700193 }
Romain Guydda570202010-07-06 11:39:32 -0700194
195 return NULL;
Romain Guyce0537b2010-06-29 21:05:21 -0700196}
197
198template<typename K, typename V>
Romain Guydda570202010-07-06 11:39:32 -0700199void GenerationCache<K, V>::attachToCache(sp<Entry<K, V> > entry) {
Romain Guy5f0c6a42010-07-07 13:06:26 -0700200 if (!mYoungest.get()) {
201 mYoungest = mOldest = entry;
Romain Guyce0537b2010-06-29 21:05:21 -0700202 } else {
Romain Guy5f0c6a42010-07-07 13:06:26 -0700203 entry->parent = mYoungest;
204 mYoungest->child = entry;
205 mYoungest = entry;
Romain Guyce0537b2010-06-29 21:05:21 -0700206 }
207}
208
209template<typename K, typename V>
Romain Guydda570202010-07-06 11:39:32 -0700210void GenerationCache<K, V>::detachFromCache(sp<Entry<K, V> > entry) {
Romain Guyce0537b2010-06-29 21:05:21 -0700211 if (entry->parent.get()) {
212 entry->parent->child = entry->child;
213 }
214
215 if (entry->child.get()) {
216 entry->child->parent = entry->parent;
217 }
218
219 if (mOldest == entry) {
220 mOldest = entry->child;
221 }
222
Romain Guy5f0c6a42010-07-07 13:06:26 -0700223 if (mYoungest == entry) {
224 mYoungest = entry->parent;
Romain Guyce0537b2010-06-29 21:05:21 -0700225 }
226
227 entry->parent.clear();
228 entry->child.clear();
229}
230
231}; // namespace uirenderer
232}; // namespace android
233
234#endif // ANDROID_UI_GENERATION_CACHE_H