OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2014 Google Inc. | 3 * Copyright 2014 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 | 9 |
10 #include "GrResourceCache2.h" | 10 #include "GrResourceCache2.h" |
11 #include "GrGpuResource.h" | 11 #include "GrGpuResource.h" |
12 | 12 |
| 13 #include "SkGr.h" |
| 14 #include "SkMessageBus.h" |
| 15 |
| 16 DECLARE_SKMESSAGEBUS_MESSAGE(GrResourceInvalidatedMessage); |
| 17 |
| 18 ////////////////////////////////////////////////////////////////////////////// |
| 19 |
13 GrResourceKey& GrResourceKey::NullScratchKey() { | 20 GrResourceKey& GrResourceKey::NullScratchKey() { |
14 static const GrCacheID::Key kBogusKey = { { {0} } }; | 21 static const GrCacheID::Key kBogusKey = { { {0} } }; |
15 static GrCacheID kBogusID(ScratchDomain(), kBogusKey); | 22 static GrCacheID kBogusID(ScratchDomain(), kBogusKey); |
16 static GrResourceKey kNullScratchKey(kBogusID, NoneResourceType(), 0); | 23 static GrResourceKey kNullScratchKey(kBogusID, NoneResourceType(), 0); |
17 return kNullScratchKey; | 24 return kNullScratchKey; |
18 } | 25 } |
19 | 26 |
20 GrResourceKey::ResourceType GrResourceKey::NoneResourceType() { | 27 GrResourceKey::ResourceType GrResourceKey::NoneResourceType() { |
21 static const ResourceType gNoneResourceType = GenerateResourceType(); | 28 static const ResourceType gNoneResourceType = GenerateResourceType(); |
22 return gNoneResourceType; | 29 return gNoneResourceType; |
23 } | 30 } |
24 | 31 |
25 GrCacheID::Domain GrResourceKey::ScratchDomain() { | 32 GrCacheID::Domain GrResourceKey::ScratchDomain() { |
26 static const GrCacheID::Domain gDomain = GrCacheID::GenerateDomain(); | 33 static const GrCacheID::Domain gDomain = GrCacheID::GenerateDomain(); |
27 return gDomain; | 34 return gDomain; |
28 } | 35 } |
29 | 36 |
| 37 GrResourceKey::ResourceType GrResourceKey::GenerateResourceType() { |
| 38 static int32_t gNextType = 0; |
| 39 |
| 40 int32_t type = sk_atomic_inc(&gNextType); |
| 41 if (type >= (1 << 8 * sizeof(ResourceType))) { |
| 42 SkFAIL("Too many Resource Types"); |
| 43 } |
| 44 |
| 45 return static_cast<ResourceType>(type); |
| 46 } |
| 47 |
30 ////////////////////////////////////////////////////////////////////////////// | 48 ////////////////////////////////////////////////////////////////////////////// |
31 | 49 |
| 50 class GrResourceCache2::AutoValidate : ::SkNoncopyable { |
| 51 public: |
| 52 AutoValidate(GrResourceCache2* cache) : fCache(cache) { cache->validate(); } |
| 53 ~AutoValidate() { fCache->validate(); } |
| 54 private: |
| 55 GrResourceCache2* fCache; |
| 56 }; |
| 57 |
| 58 ////////////////////////////////////////////////////////////////////////////// |
| 59 |
| 60 static const int kDefaultMaxCount = 2 * (1 << 10); |
| 61 static const size_t kDefaultMaxSize = 96 * (1 << 20); |
| 62 |
| 63 GrResourceCache2::GrResourceCache2() |
| 64 : fMaxCount(kDefaultMaxCount) |
| 65 , fMaxBytes(kDefaultMaxSize) |
| 66 #if GR_CACHE_STATS |
| 67 , fHighWaterCount(0) |
| 68 , fHighWaterBytes(0) |
| 69 #endif |
| 70 , fCount(0) |
| 71 , fBytes(0) |
| 72 , fPurging(false) |
| 73 , fNewlyPurgableResourceWhilePurging(false) |
| 74 , fOverBudgetCB(NULL) |
| 75 , fOverBudgetData(NULL) { |
| 76 } |
| 77 |
32 GrResourceCache2::~GrResourceCache2() { | 78 GrResourceCache2::~GrResourceCache2() { |
33 this->releaseAll(); | 79 this->releaseAll(); |
34 } | 80 } |
35 | 81 |
| 82 void GrResourceCache2::setLimits(int count, size_t bytes) { |
| 83 fMaxCount = count; |
| 84 fMaxBytes = bytes; |
| 85 this->purgeAsNeeded(); |
| 86 } |
| 87 |
36 void GrResourceCache2::insertResource(GrGpuResource* resource) { | 88 void GrResourceCache2::insertResource(GrGpuResource* resource) { |
| 89 AutoValidate av(this); |
| 90 |
37 SkASSERT(resource); | 91 SkASSERT(resource); |
38 SkASSERT(!resource->wasDestroyed()); | 92 SkASSERT(!resource->wasDestroyed()); |
39 SkASSERT(!this->isInCache(resource)); | 93 SkASSERT(!this->isInCache(resource)); |
| 94 SkASSERT(!fPurging); |
40 fResources.addToHead(resource); | 95 fResources.addToHead(resource); |
| 96 resource->ref(); |
| 97 |
41 ++fCount; | 98 ++fCount; |
| 99 SkDEBUGCODE(fHighWaterCount = SkTMax(fCount, fHighWaterCount)); |
| 100 fBytes += resource->gpuMemorySize(); |
| 101 SkDEBUGCODE(fHighWaterBytes = SkTMax(fBytes, fHighWaterBytes)); |
42 if (!resource->cacheAccess().getScratchKey().isNullScratch()) { | 102 if (!resource->cacheAccess().getScratchKey().isNullScratch()) { |
43 // TODO(bsalomon): Make this assertion possible. | 103 // TODO(bsalomon): Make this assertion possible. |
44 // SkASSERT(!resource->isWrapped()); | 104 // SkASSERT(!resource->isWrapped()); |
45 fScratchMap.insert(resource->cacheAccess().getScratchKey(), resource); | 105 fScratchMap.insert(resource->cacheAccess().getScratchKey(), resource); |
46 } | 106 } |
| 107 |
| 108 this->purgeAsNeeded(); |
47 } | 109 } |
48 | 110 |
49 void GrResourceCache2::removeResource(GrGpuResource* resource) { | 111 void GrResourceCache2::removeResource(GrGpuResource* resource) { |
| 112 AutoValidate av(this); |
| 113 |
| 114 --fCount; |
| 115 fBytes -= resource->gpuMemorySize(); |
50 SkASSERT(this->isInCache(resource)); | 116 SkASSERT(this->isInCache(resource)); |
51 fResources.remove(resource); | 117 fResources.remove(resource); |
52 if (!resource->cacheAccess().getScratchKey().isNullScratch()) { | 118 if (!resource->cacheAccess().getScratchKey().isNullScratch()) { |
53 fScratchMap.remove(resource->cacheAccess().getScratchKey(), resource); | 119 fScratchMap.remove(resource->cacheAccess().getScratchKey(), resource); |
54 } | 120 } |
55 if (const GrResourceKey* contentKey = resource->cacheAccess().getContentKey(
)) { | 121 if (const GrResourceKey* contentKey = resource->cacheAccess().getContentKey(
)) { |
56 fContentHash.remove(*contentKey); | 122 fContentHash.remove(*contentKey); |
57 } | 123 } |
58 --fCount; | |
59 } | 124 } |
60 | 125 |
61 void GrResourceCache2::abandonAll() { | 126 void GrResourceCache2::abandonAll() { |
| 127 AutoValidate av(this); |
| 128 |
| 129 SkASSERT(!fPurging); |
62 while (GrGpuResource* head = fResources.head()) { | 130 while (GrGpuResource* head = fResources.head()) { |
63 SkASSERT(!head->wasDestroyed()); | 131 SkASSERT(!head->wasDestroyed()); |
64 head->abandon(); | 132 head->abandon(); |
| 133 head->unref(); |
65 // abandon should have already removed this from the list. | 134 // abandon should have already removed this from the list. |
66 SkASSERT(head != fResources.head()); | 135 SkASSERT(head != fResources.head()); |
67 } | 136 } |
68 SkASSERT(!fScratchMap.count()); | 137 SkASSERT(!fScratchMap.count()); |
69 SkASSERT(!fContentHash.count()); | 138 SkASSERT(!fContentHash.count()); |
70 SkASSERT(!fCount); | 139 SkASSERT(!fCount); |
71 } | 140 } |
72 | 141 |
73 void GrResourceCache2::releaseAll() { | 142 void GrResourceCache2::releaseAll() { |
| 143 AutoValidate av(this); |
| 144 |
| 145 SkASSERT(!fPurging); |
74 while (GrGpuResource* head = fResources.head()) { | 146 while (GrGpuResource* head = fResources.head()) { |
75 SkASSERT(!head->wasDestroyed()); | 147 SkASSERT(!head->wasDestroyed()); |
76 head->release(); | 148 head->release(); |
| 149 head->unref(); |
77 // release should have already removed this from the list. | 150 // release should have already removed this from the list. |
78 SkASSERT(head != fResources.head()); | 151 SkASSERT(head != fResources.head()); |
79 } | 152 } |
80 SkASSERT(!fScratchMap.count()); | 153 SkASSERT(!fScratchMap.count()); |
81 SkASSERT(!fCount); | 154 SkASSERT(!fCount); |
82 } | 155 } |
83 | 156 |
84 class GrResourceCache2::AvailableForScratchUse { | 157 class GrResourceCache2::AvailableForScratchUse { |
85 public: | 158 public: |
86 AvailableForScratchUse(bool rejectPendingIO) : fRejectPendingIO(rejectPendin
gIO) { } | 159 AvailableForScratchUse(bool rejectPendingIO) : fRejectPendingIO(rejectPendin
gIO) { } |
87 | 160 |
88 bool operator()(const GrGpuResource* resource) const { | 161 bool operator()(const GrGpuResource* resource) const { |
89 if (!resource->reffedOnlyByCache() || !resource->cacheAccess().isScratch
()) { | 162 if (!resource->reffedOnlyByCache() || !resource->cacheAccess().isScratch
()) { |
90 return false; | 163 return false; |
91 } | 164 } |
92 | 165 |
93 return !fRejectPendingIO || !resource->internalHasPendingIO(); | 166 return !fRejectPendingIO || !resource->internalHasPendingIO(); |
94 } | 167 } |
95 | 168 |
96 private: | 169 private: |
97 bool fRejectPendingIO; | 170 bool fRejectPendingIO; |
98 }; | 171 }; |
99 | 172 |
100 GrGpuResource* GrResourceCache2::findAndRefScratchResource(const GrResourceKey&
scratchKey, | 173 GrGpuResource* GrResourceCache2::findAndRefScratchResource(const GrResourceKey&
scratchKey, |
101 uint32_t flags) { | 174 uint32_t flags) { |
| 175 AutoValidate av(this); |
| 176 |
| 177 SkASSERT(!fPurging); |
102 SkASSERT(scratchKey.isScratch()); | 178 SkASSERT(scratchKey.isScratch()); |
103 | 179 |
| 180 GrGpuResource* resource; |
104 if (flags & (kPreferNoPendingIO_ScratchFlag | kRequireNoPendingIO_ScratchFla
g)) { | 181 if (flags & (kPreferNoPendingIO_ScratchFlag | kRequireNoPendingIO_ScratchFla
g)) { |
105 GrGpuResource* resource = fScratchMap.find(scratchKey, AvailableForScrat
chUse(true)); | 182 resource = fScratchMap.find(scratchKey, AvailableForScratchUse(true)); |
106 if (resource) { | 183 if (resource) { |
| 184 this->makeResourceMRU(resource); |
107 return SkRef(resource); | 185 return SkRef(resource); |
108 } else if (flags & kRequireNoPendingIO_ScratchFlag) { | 186 } else if (flags & kRequireNoPendingIO_ScratchFlag) { |
109 return NULL; | 187 return NULL; |
110 } | 188 } |
111 // TODO: fail here when kPrefer is specified, we didn't find a resource
without pending io, | 189 // TODO: fail here when kPrefer is specified, we didn't find a resource
without pending io, |
112 // but there is still space in our budget for the resource. | 190 // but there is still space in our budget for the resource. |
113 } | 191 } |
114 return SkSafeRef(fScratchMap.find(scratchKey, AvailableForScratchUse(false))
); | 192 resource = fScratchMap.find(scratchKey, AvailableForScratchUse(false)); |
| 193 if (resource) { |
| 194 resource->ref(); |
| 195 this->makeResourceMRU(resource); |
| 196 } |
| 197 return resource; |
115 } | 198 } |
116 | 199 |
117 bool GrResourceCache2::didSetContentKey(GrGpuResource* resource) { | 200 bool GrResourceCache2::didSetContentKey(GrGpuResource* resource) { |
| 201 SkASSERT(!fPurging); |
118 SkASSERT(resource); | 202 SkASSERT(resource); |
| 203 SkASSERT(this->isInCache(resource)); |
119 SkASSERT(resource->cacheAccess().getContentKey()); | 204 SkASSERT(resource->cacheAccess().getContentKey()); |
120 SkASSERT(!resource->cacheAccess().getContentKey()->isScratch()); | 205 SkASSERT(!resource->cacheAccess().getContentKey()->isScratch()); |
121 | 206 |
122 GrGpuResource* res = fContentHash.find(*resource->cacheAccess().getContentKe
y()); | 207 GrGpuResource* res = fContentHash.find(*resource->cacheAccess().getContentKe
y()); |
123 if (NULL != res) { | 208 if (NULL != res) { |
124 return false; | 209 return false; |
125 } | 210 } |
126 | 211 |
127 fContentHash.add(resource); | 212 fContentHash.add(resource); |
| 213 this->validate(); |
128 return true; | 214 return true; |
129 } | 215 } |
| 216 |
| 217 void GrResourceCache2::makeResourceMRU(GrGpuResource* resource) { |
| 218 AutoValidate av(this); |
| 219 |
| 220 SkASSERT(!fPurging); |
| 221 SkASSERT(resource); |
| 222 SkASSERT(this->isInCache(resource)); |
| 223 fResources.remove(resource); |
| 224 fResources.addToHead(resource); |
| 225 } |
| 226 |
| 227 void GrResourceCache2::notifyPurgable(const GrGpuResource* resource) { |
| 228 SkASSERT(resource); |
| 229 SkASSERT(this->isInCache(resource)); |
| 230 SkASSERT(resource->isPurgable()); |
| 231 |
| 232 // We can't purge if in the middle of purging because purge is iterating. In
stead record |
| 233 // that additional resources became purgable. |
| 234 if (fPurging) { |
| 235 fNewlyPurgableResourceWhilePurging = true; |
| 236 return; |
| 237 } |
| 238 |
| 239 // Purge the resource if we're over budget |
| 240 bool overBudget = fCount > fMaxCount || fBytes > fMaxBytes; |
| 241 |
| 242 // We should not be over budget here unless all resources are unpuragble. |
| 243 #ifdef SK_DEBUG |
| 244 if (overBudget) { |
| 245 ResourceList::Iter iter; |
| 246 GrGpuResource* r = iter.init(fResources, ResourceList::Iter::kHead_IterS
tart); |
| 247 for ( ; r; r = iter.next()) { |
| 248 SkASSERT(r == resource || !r->isPurgable()); |
| 249 } |
| 250 } |
| 251 #endif |
| 252 |
| 253 // Also purge if the resource has neither a valid scratch key nor a content
key. |
| 254 bool noKey = !resource->cacheAccess().isScratch() && |
| 255 (NULL == resource->cacheAccess().getContentKey()); |
| 256 |
| 257 if (overBudget || noKey) { |
| 258 SkDEBUGCODE(int beforeCount = fCount;) |
| 259 resource->unref(); |
| 260 // We should at least have freed resource. It may have in turn freed oth
er resources. |
| 261 SkASSERT(fCount < beforeCount); |
| 262 } |
| 263 |
| 264 this->validate(); |
| 265 } |
| 266 |
| 267 void GrResourceCache2::didChangeGpuMemorySize(const GrGpuResource* resource, siz
e_t oldSize) { |
| 268 // SkASSERT(!fPurging); GrPathRange increases size during flush. :( |
| 269 SkASSERT(resource); |
| 270 SkASSERT(this->isInCache(resource)); |
| 271 |
| 272 fBytes += resource->gpuMemorySize() - oldSize; |
| 273 SkDEBUGCODE(fHighWaterBytes = SkTMax(fBytes, fHighWaterBytes)); |
| 274 |
| 275 this->purgeAsNeeded(); |
| 276 this->validate(); |
| 277 } |
| 278 |
| 279 void GrResourceCache2::internalPurgeAsNeeded() { |
| 280 SkASSERT(!fPurging); |
| 281 SkASSERT(!fNewlyPurgableResourceWhilePurging); |
| 282 SkASSERT(fCount > fMaxCount || fBytes > fMaxBytes); |
| 283 |
| 284 fPurging = true; |
| 285 |
| 286 AutoValidate av(this); // Put this after setting fPurging so we're allowed t
o be over budget. |
| 287 |
| 288 bool overBudget = true; |
| 289 do { |
| 290 fNewlyPurgableResourceWhilePurging = false; |
| 291 ResourceList::Iter resourceIter; |
| 292 GrGpuResource* resource = resourceIter.init(fResources, |
| 293 ResourceList::Iter::kTail_It
erStart); |
| 294 |
| 295 while (resource) { |
| 296 GrGpuResource* prev = resourceIter.prev(); |
| 297 if (resource->isPurgable()) { |
| 298 resource->unref(); |
| 299 } |
| 300 resource = prev; |
| 301 if (fCount <= fMaxCount && fBytes <= fMaxBytes) { |
| 302 overBudget = false; |
| 303 resource = NULL; |
| 304 } |
| 305 } |
| 306 |
| 307 if (!fNewlyPurgableResourceWhilePurging && overBudget && fOverBudgetCB)
{ |
| 308 // Despite the purge we're still over budget. Call our over budget c
allback. |
| 309 (*fOverBudgetCB)(fOverBudgetData); |
| 310 } |
| 311 } while (overBudget && fNewlyPurgableResourceWhilePurging); |
| 312 |
| 313 fNewlyPurgableResourceWhilePurging = false; |
| 314 fPurging = false; |
| 315 } |
| 316 |
| 317 void GrResourceCache2::purgeAllUnlocked() { |
| 318 SkASSERT(!fPurging); |
| 319 SkASSERT(!fNewlyPurgableResourceWhilePurging); |
| 320 |
| 321 fPurging = true; |
| 322 |
| 323 AutoValidate av(this); // Put this after setting fPurging so we're allowed t
o be over budget. |
| 324 |
| 325 do { |
| 326 fNewlyPurgableResourceWhilePurging = false; |
| 327 ResourceList::Iter resourceIter; |
| 328 GrGpuResource* resource = |
| 329 resourceIter.init(fResources, ResourceList::Iter::kTail_IterStart); |
| 330 |
| 331 while (resource) { |
| 332 GrGpuResource* prev = resourceIter.prev(); |
| 333 if (resource->isPurgable()) { |
| 334 resource->unref(); |
| 335 } |
| 336 resource = prev; |
| 337 } |
| 338 |
| 339 if (!fNewlyPurgableResourceWhilePurging && fCount && fOverBudgetCB) { |
| 340 (*fOverBudgetCB)(fOverBudgetData); |
| 341 } |
| 342 } while (fNewlyPurgableResourceWhilePurging); |
| 343 fPurging = false; |
| 344 } |
| 345 |
| 346 #ifdef SK_DEBUG |
| 347 void GrResourceCache2::validate() const { |
| 348 size_t bytes = 0; |
| 349 int count = 0; |
| 350 int locked = 0; |
| 351 int scratch = 0; |
| 352 int couldBeScratch = 0; |
| 353 int content = 0; |
| 354 |
| 355 ResourceList::Iter iter; |
| 356 GrGpuResource* resource = iter.init(fResources, ResourceList::Iter::kHead_It
erStart); |
| 357 for ( ; resource; resource = iter.next()) { |
| 358 bytes += resource->gpuMemorySize(); |
| 359 ++count; |
| 360 |
| 361 if (!resource->isPurgable()) { |
| 362 ++locked; |
| 363 } |
| 364 |
| 365 if (resource->cacheAccess().isScratch()) { |
| 366 SkASSERT(NULL == resource->cacheAccess().getContentKey()); |
| 367 ++scratch; |
| 368 SkASSERT(fScratchMap.countForKey(resource->cacheAccess().getScratchK
ey())); |
| 369 } else if (!resource->cacheAccess().getScratchKey().isNullScratch()) { |
| 370 SkASSERT(NULL != resource->cacheAccess().getContentKey()); |
| 371 ++couldBeScratch; |
| 372 SkASSERT(fScratchMap.countForKey(resource->cacheAccess().getScratchK
ey())); |
| 373 } |
| 374 |
| 375 if (const GrResourceKey* contentKey = resource->cacheAccess().getContent
Key()) { |
| 376 ++content; |
| 377 SkASSERT(fContentHash.find(*contentKey) == resource); |
| 378 } |
| 379 } |
| 380 |
| 381 SkASSERT(bytes == fBytes); |
| 382 SkASSERT(count == fCount); |
| 383 #if GR_CACHE_STATS |
| 384 SkASSERT(bytes <= fHighWaterBytes); |
| 385 SkASSERT(count <= fHighWaterCount); |
| 386 #endif |
| 387 SkASSERT(content == fContentHash.count()); |
| 388 SkASSERT(scratch + couldBeScratch == fScratchMap.count()); |
| 389 |
| 390 bool overBudget = bytes > fMaxBytes || count > fMaxCount; |
| 391 SkASSERT(!overBudget || locked == count || fPurging); |
| 392 } |
| 393 #endif |
| 394 |
| 395 #if GR_CACHE_STATS |
| 396 void GrResourceCache2::printStats() const { |
| 397 this->validate(); |
| 398 |
| 399 int locked = 0; |
| 400 int scratch = 0; |
| 401 |
| 402 ResourceList::Iter iter; |
| 403 GrGpuResource* resource = iter.init(fResources, ResourceList::Iter::kHead_It
erStart); |
| 404 |
| 405 for ( ; resource; resource = iter.next()) { |
| 406 if (!resource->isPurgable()) { |
| 407 ++locked; |
| 408 } |
| 409 if (resource->cacheAccess().isScratch()) { |
| 410 ++scratch; |
| 411 } |
| 412 } |
| 413 |
| 414 float countUtilization = (100.f * fCount) / fMaxCount; |
| 415 float byteUtilization = (100.f * fBytes) / fMaxBytes; |
| 416 |
| 417 SkDebugf("Budget: %d items %d bytes\n", fMaxCount, fMaxBytes); |
| 418 SkDebugf("\t\tEntry Count: current %d (%d locked, %d scratch %.2g%% full), h
igh %d\n", |
| 419 fCount, locked, scratch, countUtilization, fHighWaterCount); |
| 420 SkDebugf("\t\tEntry Bytes: current %d (%.2g%% full) high %d\n", |
| 421 fBytes, byteUtilization, fHighWaterBytes); |
| 422 } |
| 423 |
| 424 #endif |
OLD | NEW |