Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(892)

Side by Side Diff: src/gpu/GrResourceCache2.cpp

Issue 737533002: Reduce the amount of validation in GrResourceCache2 (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: update Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
83 this->releaseAll(); 83 this->releaseAll();
84 } 84 }
85 85
86 void GrResourceCache2::setLimits(int count, size_t bytes) { 86 void GrResourceCache2::setLimits(int count, size_t bytes) {
87 fMaxCount = count; 87 fMaxCount = count;
88 fMaxBytes = bytes; 88 fMaxBytes = bytes;
89 this->purgeAsNeeded(); 89 this->purgeAsNeeded();
90 } 90 }
91 91
92 void GrResourceCache2::insertResource(GrGpuResource* resource) { 92 void GrResourceCache2::insertResource(GrGpuResource* resource) {
93 AutoValidate av(this);
94
95 SkASSERT(resource); 93 SkASSERT(resource);
96 SkASSERT(!resource->wasDestroyed()); 94 SkASSERT(!resource->wasDestroyed());
97 SkASSERT(!this->isInCache(resource)); 95 SkASSERT(!this->isInCache(resource));
98 SkASSERT(!fPurging); 96 SkASSERT(!fPurging);
99 fResources.addToHead(resource); 97 fResources.addToHead(resource);
100 98
101 size_t size = resource->gpuMemorySize(); 99 size_t size = resource->gpuMemorySize();
102 ++fCount; 100 ++fCount;
103 fBytes += size; 101 fBytes += size;
104 #if GR_CACHE_STATS 102 #if GR_CACHE_STATS
(...skipping 10 matching lines...) Expand all
115 } 113 }
116 if (!resource->cacheAccess().getScratchKey().isNullScratch()) { 114 if (!resource->cacheAccess().getScratchKey().isNullScratch()) {
117 SkASSERT(!resource->cacheAccess().isWrapped()); 115 SkASSERT(!resource->cacheAccess().isWrapped());
118 fScratchMap.insert(resource->cacheAccess().getScratchKey(), resource); 116 fScratchMap.insert(resource->cacheAccess().getScratchKey(), resource);
119 } 117 }
120 118
121 this->purgeAsNeeded(); 119 this->purgeAsNeeded();
122 } 120 }
123 121
124 void GrResourceCache2::removeResource(GrGpuResource* resource) { 122 void GrResourceCache2::removeResource(GrGpuResource* resource) {
125 AutoValidate av(this);
126
127 SkASSERT(this->isInCache(resource)); 123 SkASSERT(this->isInCache(resource));
128 124
129 size_t size = resource->gpuMemorySize(); 125 size_t size = resource->gpuMemorySize();
130 --fCount; 126 --fCount;
131 fBytes -= size; 127 fBytes -= size;
132 if (resource->cacheAccess().isBudgeted()) { 128 if (resource->cacheAccess().isBudgeted()) {
133 --fBudgetedCount; 129 --fBudgetedCount;
134 fBudgetedBytes -= size; 130 fBudgetedBytes -= size;
135 } 131 }
136 132
137 fResources.remove(resource); 133 fResources.remove(resource);
138 if (!resource->cacheAccess().getScratchKey().isNullScratch()) { 134 if (!resource->cacheAccess().getScratchKey().isNullScratch()) {
139 fScratchMap.remove(resource->cacheAccess().getScratchKey(), resource); 135 fScratchMap.remove(resource->cacheAccess().getScratchKey(), resource);
140 } 136 }
141 if (const GrResourceKey* contentKey = resource->cacheAccess().getContentKey( )) { 137 if (const GrResourceKey* contentKey = resource->cacheAccess().getContentKey( )) {
142 fContentHash.remove(*contentKey); 138 fContentHash.remove(*contentKey);
143 } 139 }
140 this->validate();
144 } 141 }
145 142
146 void GrResourceCache2::abandonAll() { 143 void GrResourceCache2::abandonAll() {
147 AutoValidate av(this); 144 AutoValidate av(this);
148 145
149 SkASSERT(!fPurging); 146 SkASSERT(!fPurging);
150 while (GrGpuResource* head = fResources.head()) { 147 while (GrGpuResource* head = fResources.head()) {
151 SkASSERT(!head->wasDestroyed()); 148 SkASSERT(!head->wasDestroyed());
152 head->cacheAccess().abandon(); 149 head->cacheAccess().abandon();
153 // abandon should have already removed this from the list. 150 // abandon should have already removed this from the list.
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 } 185 }
189 return !fRejectPendingIO || !resource->internalHasPendingIO(); 186 return !fRejectPendingIO || !resource->internalHasPendingIO();
190 } 187 }
191 188
192 private: 189 private:
193 bool fRejectPendingIO; 190 bool fRejectPendingIO;
194 }; 191 };
195 192
196 GrGpuResource* GrResourceCache2::findAndRefScratchResource(const GrResourceKey& scratchKey, 193 GrGpuResource* GrResourceCache2::findAndRefScratchResource(const GrResourceKey& scratchKey,
197 uint32_t flags) { 194 uint32_t flags) {
198 AutoValidate av(this);
199
200 SkASSERT(!fPurging); 195 SkASSERT(!fPurging);
201 SkASSERT(scratchKey.isScratch()); 196 SkASSERT(scratchKey.isScratch());
202 197
203 GrGpuResource* resource; 198 GrGpuResource* resource;
204 if (flags & (kPreferNoPendingIO_ScratchFlag | kRequireNoPendingIO_ScratchFla g)) { 199 if (flags & (kPreferNoPendingIO_ScratchFlag | kRequireNoPendingIO_ScratchFla g)) {
205 resource = fScratchMap.find(scratchKey, AvailableForScratchUse(true)); 200 resource = fScratchMap.find(scratchKey, AvailableForScratchUse(true));
206 if (resource) { 201 if (resource) {
202 resource->ref();
207 this->makeResourceMRU(resource); 203 this->makeResourceMRU(resource);
208 return SkRef(resource); 204 this->validate();
205 return resource;
209 } else if (flags & kRequireNoPendingIO_ScratchFlag) { 206 } else if (flags & kRequireNoPendingIO_ScratchFlag) {
210 return NULL; 207 return NULL;
211 } 208 }
212 // TODO: fail here when kPrefer is specified, we didn't find a resource without pending io, 209 // TODO: fail here when kPrefer is specified, we didn't find a resource without pending io,
213 // but there is still space in our budget for the resource. 210 // but there is still space in our budget for the resource.
214 } 211 }
215 resource = fScratchMap.find(scratchKey, AvailableForScratchUse(false)); 212 resource = fScratchMap.find(scratchKey, AvailableForScratchUse(false));
216 if (resource) { 213 if (resource) {
217 resource->ref(); 214 resource->ref();
218 this->makeResourceMRU(resource); 215 this->makeResourceMRU(resource);
216 this->validate();
219 } 217 }
220 return resource; 218 return resource;
221 } 219 }
222 220
223 bool GrResourceCache2::didSetContentKey(GrGpuResource* resource) { 221 bool GrResourceCache2::didSetContentKey(GrGpuResource* resource) {
224 SkASSERT(!fPurging); 222 SkASSERT(!fPurging);
225 SkASSERT(resource); 223 SkASSERT(resource);
226 SkASSERT(this->isInCache(resource)); 224 SkASSERT(this->isInCache(resource));
227 SkASSERT(resource->cacheAccess().getContentKey()); 225 SkASSERT(resource->cacheAccess().getContentKey());
228 SkASSERT(!resource->cacheAccess().getContentKey()->isScratch()); 226 SkASSERT(!resource->cacheAccess().getContentKey()->isScratch());
229 227
230 GrGpuResource* res = fContentHash.find(*resource->cacheAccess().getContentKe y()); 228 GrGpuResource* res = fContentHash.find(*resource->cacheAccess().getContentKe y());
231 if (NULL != res) { 229 if (NULL != res) {
232 return false; 230 return false;
233 } 231 }
234 232
235 fContentHash.add(resource); 233 fContentHash.add(resource);
236 this->validate(); 234 this->validate();
237 return true; 235 return true;
238 } 236 }
239 237
240 void GrResourceCache2::makeResourceMRU(GrGpuResource* resource) { 238 void GrResourceCache2::makeResourceMRU(GrGpuResource* resource) {
241 AutoValidate av(this);
242
243 SkASSERT(!fPurging); 239 SkASSERT(!fPurging);
244 SkASSERT(resource); 240 SkASSERT(resource);
245 SkASSERT(this->isInCache(resource)); 241 SkASSERT(this->isInCache(resource));
246 fResources.remove(resource); 242 fResources.remove(resource);
247 fResources.addToHead(resource); 243 fResources.addToHead(resource);
248 } 244 }
249 245
250 void GrResourceCache2::notifyPurgable(GrGpuResource* resource) { 246 void GrResourceCache2::notifyPurgable(GrGpuResource* resource) {
251 SkASSERT(resource); 247 SkASSERT(resource);
252 SkASSERT(this->isInCache(resource)); 248 SkASSERT(this->isInCache(resource));
253 SkASSERT(resource->isPurgable()); 249 SkASSERT(resource->isPurgable());
254 250
255 // We can't purge if in the middle of purging because purge is iterating. In stead record 251 // We can't purge if in the middle of purging because purge is iterating. In stead record
256 // that additional resources became purgable. 252 // that additional resources became purgable.
257 if (fPurging) { 253 if (fPurging) {
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
322 } 318 }
323 319
324 320
325 void GrResourceCache2::internalPurgeAsNeeded() { 321 void GrResourceCache2::internalPurgeAsNeeded() {
326 SkASSERT(!fPurging); 322 SkASSERT(!fPurging);
327 SkASSERT(!fNewlyPurgableResourceWhilePurging); 323 SkASSERT(!fNewlyPurgableResourceWhilePurging);
328 SkASSERT(fBudgetedCount > fMaxCount || fBudgetedBytes > fMaxBytes); 324 SkASSERT(fBudgetedCount > fMaxCount || fBudgetedBytes > fMaxBytes);
329 325
330 fPurging = true; 326 fPurging = true;
331 327
332 AutoValidate av(this); // Put this after setting fPurging so we're allowed t o be over budget.
333
334 bool overBudget = true; 328 bool overBudget = true;
335 do { 329 do {
336 fNewlyPurgableResourceWhilePurging = false; 330 fNewlyPurgableResourceWhilePurging = false;
337 ResourceList::Iter resourceIter; 331 ResourceList::Iter resourceIter;
338 GrGpuResource* resource = resourceIter.init(fResources, 332 GrGpuResource* resource = resourceIter.init(fResources,
339 ResourceList::Iter::kTail_It erStart); 333 ResourceList::Iter::kTail_It erStart);
340 334
341 while (resource) { 335 while (resource) {
342 GrGpuResource* prev = resourceIter.prev(); 336 GrGpuResource* prev = resourceIter.prev();
343 if (resource->isPurgable()) { 337 if (resource->isPurgable()) {
344 resource->cacheAccess().release(); 338 resource->cacheAccess().release();
345 } 339 }
346 resource = prev; 340 resource = prev;
347 if (fBudgetedCount <= fMaxCount && fBudgetedBytes <= fMaxBytes) { 341 if (fBudgetedCount <= fMaxCount && fBudgetedBytes <= fMaxBytes) {
348 overBudget = false; 342 overBudget = false;
349 resource = NULL; 343 resource = NULL;
350 } 344 }
351 } 345 }
352 346
353 if (!fNewlyPurgableResourceWhilePurging && overBudget && fOverBudgetCB) { 347 if (!fNewlyPurgableResourceWhilePurging && overBudget && fOverBudgetCB) {
354 // Despite the purge we're still over budget. Call our over budget c allback. 348 // Despite the purge we're still over budget. Call our over budget c allback.
355 (*fOverBudgetCB)(fOverBudgetData); 349 (*fOverBudgetCB)(fOverBudgetData);
356 } 350 }
357 } while (overBudget && fNewlyPurgableResourceWhilePurging); 351 } while (overBudget && fNewlyPurgableResourceWhilePurging);
358 352
359 fNewlyPurgableResourceWhilePurging = false; 353 fNewlyPurgableResourceWhilePurging = false;
360 fPurging = false; 354 fPurging = false;
355 this->validate();
361 } 356 }
362 357
363 void GrResourceCache2::purgeAllUnlocked() { 358 void GrResourceCache2::purgeAllUnlocked() {
364 SkASSERT(!fPurging); 359 SkASSERT(!fPurging);
365 SkASSERT(!fNewlyPurgableResourceWhilePurging); 360 SkASSERT(!fNewlyPurgableResourceWhilePurging);
366 361
367 fPurging = true; 362 fPurging = true;
368 363
369 AutoValidate av(this); // Put this after setting fPurging so we're allowed t o be over budget.
370
371 do { 364 do {
372 fNewlyPurgableResourceWhilePurging = false; 365 fNewlyPurgableResourceWhilePurging = false;
373 ResourceList::Iter resourceIter; 366 ResourceList::Iter resourceIter;
374 GrGpuResource* resource = 367 GrGpuResource* resource =
375 resourceIter.init(fResources, ResourceList::Iter::kTail_IterStart); 368 resourceIter.init(fResources, ResourceList::Iter::kTail_IterStart);
376 369
377 while (resource) { 370 while (resource) {
378 GrGpuResource* prev = resourceIter.prev(); 371 GrGpuResource* prev = resourceIter.prev();
379 if (resource->isPurgable()) { 372 if (resource->isPurgable()) {
380 resource->cacheAccess().release(); 373 resource->cacheAccess().release();
381 } 374 }
382 resource = prev; 375 resource = prev;
383 } 376 }
384 377
385 if (!fNewlyPurgableResourceWhilePurging && fCount && fOverBudgetCB) { 378 if (!fNewlyPurgableResourceWhilePurging && fCount && fOverBudgetCB) {
386 (*fOverBudgetCB)(fOverBudgetData); 379 (*fOverBudgetCB)(fOverBudgetData);
387 } 380 }
388 } while (fNewlyPurgableResourceWhilePurging); 381 } while (fNewlyPurgableResourceWhilePurging);
389 fPurging = false; 382 fPurging = false;
383 this->validate();
390 } 384 }
391 385
392 #ifdef SK_DEBUG 386 #ifdef SK_DEBUG
393 void GrResourceCache2::validate() const { 387 void GrResourceCache2::validate() const {
394 size_t bytes = 0; 388 size_t bytes = 0;
395 int count = 0; 389 int count = 0;
396 int budgetedCount = 0; 390 int budgetedCount = 0;
397 size_t budgetedBytes = 0; 391 size_t budgetedBytes = 0;
398 int locked = 0; 392 int locked = 0;
399 int scratch = 0; 393 int scratch = 0;
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
490 484
491 SkDebugf("Budget: %d items %d bytes\n", fMaxCount, fMaxBytes); 485 SkDebugf("Budget: %d items %d bytes\n", fMaxCount, fMaxBytes);
492 SkDebugf("\t\tEntry Count: current %d" 486 SkDebugf("\t\tEntry Count: current %d"
493 " (%d budgeted, %d wrapped, %d locked, %d scratch %.2g%% full), hig h %d\n", 487 " (%d budgeted, %d wrapped, %d locked, %d scratch %.2g%% full), hig h %d\n",
494 fCount, fBudgetedCount, wrapped, locked, scratch, countUtilization, fHig hWaterCount); 488 fCount, fBudgetedCount, wrapped, locked, scratch, countUtilization, fHig hWaterCount);
495 SkDebugf("\t\tEntry Bytes: current %d (budgeted %d, %.2g%% full, %d unbudget ed) high %d\n", 489 SkDebugf("\t\tEntry Bytes: current %d (budgeted %d, %.2g%% full, %d unbudget ed) high %d\n",
496 fBytes, fBudgetedBytes, byteUtilization, unbudgetedSize, fHighWa terBytes); 490 fBytes, fBudgetedBytes, byteUtilization, unbudgetedSize, fHighWa terBytes);
497 } 491 }
498 492
499 #endif 493 #endif
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698