reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2010 Google Inc. |
| 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 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame^] | 18 | #include "GrResourceCache.h" |
| 19 | #include "GrResource.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 20 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame^] | 21 | GrResourceEntry::GrResourceEntry(const GrResourceKey& key, GrResource* resource) |
| 22 | : fKey(key), fResource(resource) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 23 | fLockCount = 0; |
| 24 | fPrev = fNext = NULL; |
| 25 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame^] | 26 | // we assume ownership of the resource, and will unref it when we die |
| 27 | GrAssert(resource); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 28 | } |
| 29 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame^] | 30 | GrResourceEntry::~GrResourceEntry() { |
| 31 | fResource->unref(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | #if GR_DEBUG |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame^] | 35 | void GrResourceEntry::validate() const { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 36 | GrAssert(fLockCount >= 0); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame^] | 37 | GrAssert(fResource); |
| 38 | fResource->validate(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 39 | } |
| 40 | #endif |
| 41 | |
| 42 | /////////////////////////////////////////////////////////////////////////////// |
| 43 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame^] | 44 | GrResourceCache::GrResourceCache(int maxCount, size_t maxBytes) : |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 45 | fMaxCount(maxCount), |
| 46 | fMaxBytes(maxBytes) { |
| 47 | fEntryCount = 0; |
| 48 | fEntryBytes = 0; |
| 49 | fClientDetachedCount = 0; |
| 50 | fClientDetachedBytes = 0; |
| 51 | |
| 52 | fHead = fTail = NULL; |
| 53 | } |
| 54 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame^] | 55 | GrResourceCache::~GrResourceCache() { |
| 56 | GrAutoResourceCacheValidate atcv(this); |
reed@google.com | 01804b4 | 2011-01-18 21:50:41 +0000 | [diff] [blame] | 57 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 58 | this->removeAll(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 59 | } |
| 60 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame^] | 61 | void GrResourceCache::getLimits(int* maxResources, size_t* maxResourceBytes) const{ |
| 62 | if (maxResources) { |
| 63 | *maxResources = fMaxCount; |
reed@google.com | 01804b4 | 2011-01-18 21:50:41 +0000 | [diff] [blame] | 64 | } |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame^] | 65 | if (maxResourceBytes) { |
| 66 | *maxResourceBytes = fMaxBytes; |
reed@google.com | 01804b4 | 2011-01-18 21:50:41 +0000 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame^] | 70 | void GrResourceCache::setLimits(int maxResources, size_t maxResourceBytes) { |
| 71 | bool smaller = (maxResources < fMaxCount) || (maxResourceBytes < fMaxBytes); |
reed@google.com | 01804b4 | 2011-01-18 21:50:41 +0000 | [diff] [blame] | 72 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame^] | 73 | fMaxCount = maxResources; |
| 74 | fMaxBytes = maxResourceBytes; |
reed@google.com | 01804b4 | 2011-01-18 21:50:41 +0000 | [diff] [blame] | 75 | |
| 76 | if (smaller) { |
| 77 | this->purgeAsNeeded(); |
| 78 | } |
| 79 | } |
| 80 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame^] | 81 | void GrResourceCache::internalDetach(GrResourceEntry* entry, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 82 | bool clientDetach) { |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame^] | 83 | GrResourceEntry* prev = entry->fPrev; |
| 84 | GrResourceEntry* next = entry->fNext; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 85 | |
| 86 | if (prev) { |
| 87 | prev->fNext = next; |
| 88 | } else { |
| 89 | fHead = next; |
| 90 | } |
| 91 | if (next) { |
| 92 | next->fPrev = prev; |
| 93 | } else { |
| 94 | fTail = prev; |
| 95 | } |
| 96 | |
| 97 | // update our stats |
| 98 | if (clientDetach) { |
| 99 | fClientDetachedCount += 1; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame^] | 100 | fClientDetachedBytes += entry->resource()->sizeInBytes(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 101 | } else { |
| 102 | fEntryCount -= 1; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame^] | 103 | fEntryBytes -= entry->resource()->sizeInBytes(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame^] | 107 | void GrResourceCache::attachToHead(GrResourceEntry* entry, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 108 | bool clientReattach) { |
| 109 | entry->fPrev = NULL; |
| 110 | entry->fNext = fHead; |
| 111 | if (fHead) { |
| 112 | fHead->fPrev = entry; |
| 113 | } |
| 114 | fHead = entry; |
| 115 | if (NULL == fTail) { |
| 116 | fTail = entry; |
| 117 | } |
| 118 | |
| 119 | // update our stats |
| 120 | if (clientReattach) { |
| 121 | fClientDetachedCount -= 1; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame^] | 122 | fClientDetachedBytes -= entry->resource()->sizeInBytes(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 123 | } else { |
| 124 | fEntryCount += 1; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame^] | 125 | fEntryBytes += entry->resource()->sizeInBytes(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame^] | 129 | class GrResourceCache::Key { |
| 130 | typedef GrResourceEntry T; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 131 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame^] | 132 | const GrResourceKey& fKey; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 133 | public: |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame^] | 134 | Key(const GrResourceKey& key) : fKey(key) {} |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 135 | |
| 136 | uint32_t getHash() const { return fKey.hashIndex(); } |
reed@google.com | 01804b4 | 2011-01-18 21:50:41 +0000 | [diff] [blame] | 137 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 138 | static bool LT(const T& entry, const Key& key) { |
| 139 | return entry.key() < key.fKey; |
| 140 | } |
| 141 | static bool EQ(const T& entry, const Key& key) { |
| 142 | return entry.key() == key.fKey; |
| 143 | } |
| 144 | #if GR_DEBUG |
| 145 | static uint32_t GetHash(const T& entry) { |
| 146 | return entry.key().hashIndex(); |
| 147 | } |
| 148 | static bool LT(const T& a, const T& b) { |
| 149 | return a.key() < b.key(); |
| 150 | } |
| 151 | static bool EQ(const T& a, const T& b) { |
| 152 | return a.key() == b.key(); |
| 153 | } |
| 154 | #endif |
| 155 | }; |
| 156 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame^] | 157 | GrResourceEntry* GrResourceCache::findAndLock(const GrResourceKey& key) { |
| 158 | GrAutoResourceCacheValidate atcv(this); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 159 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame^] | 160 | GrResourceEntry* entry = fCache.find(key); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 161 | if (entry) { |
| 162 | this->internalDetach(entry, false); |
| 163 | this->attachToHead(entry, false); |
| 164 | // mark the entry as "busy" so it doesn't get purged |
| 165 | entry->lock(); |
| 166 | } |
| 167 | return entry; |
| 168 | } |
| 169 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame^] | 170 | GrResourceEntry* GrResourceCache::createAndLock(const GrResourceKey& key, |
| 171 | GrResource* resource) { |
| 172 | GrAutoResourceCacheValidate atcv(this); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 173 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame^] | 174 | GrResourceEntry* entry = new GrResourceEntry(key, resource); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 175 | |
| 176 | this->attachToHead(entry, false); |
| 177 | fCache.insert(key, entry); |
| 178 | |
| 179 | #if GR_DUMP_TEXTURE_UPLOAD |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame^] | 180 | GrPrintf("--- add resource to cache %p, count=%d bytes= %d %d\n", |
| 181 | entry, fEntryCount, resource->sizeInBytes(), fEntryBytes); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 182 | #endif |
| 183 | |
| 184 | // mark the entry as "busy" so it doesn't get purged |
| 185 | entry->lock(); |
| 186 | this->purgeAsNeeded(); |
| 187 | return entry; |
| 188 | } |
| 189 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame^] | 190 | void GrResourceCache::detach(GrResourceEntry* entry) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 191 | internalDetach(entry, true); |
| 192 | fCache.remove(entry->fKey, entry); |
| 193 | } |
| 194 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame^] | 195 | void GrResourceCache::reattachAndUnlock(GrResourceEntry* entry) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 196 | attachToHead(entry, true); |
| 197 | fCache.insert(entry->key(), entry); |
| 198 | unlock(entry); |
| 199 | } |
| 200 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame^] | 201 | void GrResourceCache::unlock(GrResourceEntry* entry) { |
| 202 | GrAutoResourceCacheValidate atcv(this); |
reed@google.com | 01804b4 | 2011-01-18 21:50:41 +0000 | [diff] [blame] | 203 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 204 | GrAssert(entry); |
| 205 | GrAssert(entry->isLocked()); |
| 206 | GrAssert(fCache.find(entry->key())); |
| 207 | |
| 208 | entry->unlock(); |
| 209 | this->purgeAsNeeded(); |
| 210 | } |
| 211 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame^] | 212 | void GrResourceCache::purgeAsNeeded() { |
| 213 | GrAutoResourceCacheValidate atcv(this); |
reed@google.com | 01804b4 | 2011-01-18 21:50:41 +0000 | [diff] [blame] | 214 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame^] | 215 | GrResourceEntry* entry = fTail; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 216 | while (entry) { |
| 217 | if (fEntryCount <= fMaxCount && fEntryBytes <= fMaxBytes) { |
| 218 | break; |
| 219 | } |
| 220 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame^] | 221 | GrResourceEntry* prev = entry->fPrev; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 222 | if (!entry->isLocked()) { |
| 223 | // remove from our cache |
| 224 | fCache.remove(entry->fKey, entry); |
reed@google.com | 01804b4 | 2011-01-18 21:50:41 +0000 | [diff] [blame] | 225 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 226 | // remove from our llist |
| 227 | this->internalDetach(entry, false); |
| 228 | |
| 229 | #if GR_DUMP_TEXTURE_UPLOAD |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame^] | 230 | GrPrintf("--- ~resource from cache %p [%d %d]\n", entry->resource(), |
| 231 | entry->resource()->width(), |
| 232 | entry->resource()->height()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 233 | #endif |
| 234 | delete entry; |
| 235 | } |
| 236 | entry = prev; |
| 237 | } |
| 238 | } |
| 239 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame^] | 240 | void GrResourceCache::removeAll() { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 241 | GrAssert(!fClientDetachedCount); |
| 242 | GrAssert(!fClientDetachedBytes); |
reed@google.com | 01804b4 | 2011-01-18 21:50:41 +0000 | [diff] [blame] | 243 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame^] | 244 | GrResourceEntry* entry = fHead; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 245 | while (entry) { |
| 246 | GrAssert(!entry->isLocked()); |
| 247 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame^] | 248 | GrResourceEntry* next = entry->fNext; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 249 | delete entry; |
| 250 | entry = next; |
| 251 | } |
| 252 | |
| 253 | fCache.removeAll(); |
| 254 | fHead = fTail = NULL; |
| 255 | fEntryCount = 0; |
| 256 | fEntryBytes = 0; |
| 257 | } |
| 258 | |
| 259 | /////////////////////////////////////////////////////////////////////////////// |
| 260 | |
| 261 | #if GR_DEBUG |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame^] | 262 | static int countMatches(const GrResourceEntry* head, const GrResourceEntry* target) { |
| 263 | const GrResourceEntry* entry = head; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 264 | int count = 0; |
| 265 | while (entry) { |
| 266 | if (target == entry) { |
| 267 | count += 1; |
| 268 | } |
| 269 | entry = entry->next(); |
| 270 | } |
| 271 | return count; |
| 272 | } |
| 273 | |
reed@google.com | b89a643 | 2011-02-07 13:20:30 +0000 | [diff] [blame] | 274 | #if GR_DEBUG |
| 275 | static bool both_zero_or_nonzero(int count, size_t bytes) { |
| 276 | return (count == 0 && bytes == 0) || (count > 0 && bytes > 0); |
| 277 | } |
| 278 | #endif |
| 279 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame^] | 280 | void GrResourceCache::validate() const { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 281 | GrAssert(!fHead == !fTail); |
reed@google.com | b89a643 | 2011-02-07 13:20:30 +0000 | [diff] [blame] | 282 | GrAssert(both_zero_or_nonzero(fEntryCount, fEntryBytes)); |
| 283 | GrAssert(both_zero_or_nonzero(fClientDetachedCount, fClientDetachedBytes)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 284 | GrAssert(fClientDetachedBytes <= fEntryBytes); |
| 285 | GrAssert(fClientDetachedCount <= fEntryCount); |
| 286 | GrAssert((fEntryCount - fClientDetachedCount) == fCache.count()); |
reed@google.com | 01804b4 | 2011-01-18 21:50:41 +0000 | [diff] [blame] | 287 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 288 | fCache.validate(); |
| 289 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame^] | 290 | GrResourceEntry* entry = fHead; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 291 | int count = 0; |
| 292 | size_t bytes = 0; |
| 293 | while (entry) { |
| 294 | entry->validate(); |
| 295 | GrAssert(fCache.find(entry->key())); |
| 296 | count += 1; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame^] | 297 | bytes += entry->resource()->sizeInBytes(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 298 | entry = entry->fNext; |
| 299 | } |
| 300 | GrAssert(count == fEntryCount - fClientDetachedCount); |
| 301 | GrAssert(bytes == fEntryBytes - fClientDetachedBytes); |
| 302 | |
| 303 | count = 0; |
| 304 | for (entry = fTail; entry; entry = entry->fPrev) { |
| 305 | count += 1; |
| 306 | } |
| 307 | GrAssert(count == fEntryCount - fClientDetachedCount); |
| 308 | |
| 309 | for (int i = 0; i < count; i++) { |
| 310 | int matches = countMatches(fHead, fCache.getArray()[i]); |
| 311 | GrAssert(1 == matches); |
| 312 | } |
| 313 | } |
| 314 | #endif |
| 315 | |
| 316 | |