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

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

Issue 716143004: Replace GrResourceCache with GrResourceCache2. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: fix uncalled function 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
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"
11 #include "GrGpuResource.h" 11 #include "GrGpuResource.h"
12 12
robertphillips 2014/11/13 14:34:03 out of order ?
bsalomon 2014/11/13 14:51:50 Done.
13 #include "SkMessageBus.h"
14 #include "SkGr.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
robertphillips 2014/11/13 14:34:03 remove this public ?
bsalomon 2014/11/13 14:51:50 Done.
50 class GrResourceCache2::AutoValidate : public ::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 SkASSERT(fCount == beforeCount - 1);
261 }
262
263 this->validate();
264 }
265
266 void GrResourceCache2::didChangeGpuMemorySize(const GrGpuResource* resource, siz e_t oldSize) {
267 // SkASSERT(!fPurging); GrPathRange increases size during flush. :(
268 SkASSERT(resource);
269 SkASSERT(this->isInCache(resource));
270
271 fBytes += resource->gpuMemorySize() - oldSize;
272 SkDEBUGCODE(fHighWaterBytes = SkTMax(fBytes, fHighWaterBytes));
273
274 this->purgeAsNeeded();
275 this->validate();
276 }
277
278 void GrResourceCache2::internalPurgeAsNeeded() {
279 SkASSERT(!fPurging);
280 SkASSERT(!fNewlyPurgableResourceWhilePurging);
281 SkASSERT(fCount > fMaxCount || fBytes > fMaxBytes);
282
283 fPurging = true;
284
285 AutoValidate av(this); // Put this after setting fPurging so we're allowed t o be over budget.
286
287 bool overBudget = true;
288 do {
289 fNewlyPurgableResourceWhilePurging = false;
290 ResourceList::Iter resourceIter;
291 GrGpuResource* resource = resourceIter.init(fResources,
292 ResourceList::Iter::kTail_It erStart);
293
294 while (resource) {
robertphillips 2014/11/13 14:34:03 GrGpuResource* prev = resourceIter.prev(); if (res
bsalomon 2014/11/13 14:51:50 Done
295 if (resource->isPurgable()) {
296 GrGpuResource* prev = resourceIter.prev();
297 resource->unref();
298 resource = prev;
299 } else {
300 resource = resourceIter.prev();
301 }
302 if (fCount <= fMaxCount && fBytes <= fMaxBytes) {
303 overBudget = false;
304 resource = NULL;
305 }
306 }
307
308 if (!fNewlyPurgableResourceWhilePurging && overBudget && fOverBudgetCB) {
309 // Despite the purge we're still over budget. Call our over budget c allback.
310 (*fOverBudgetCB)(fOverBudgetData);
311 }
312 } while (overBudget && fNewlyPurgableResourceWhilePurging);
313
314 fNewlyPurgableResourceWhilePurging = false;
315 fPurging = false;
316 }
317
318 void GrResourceCache2::purgeAllUnlocked() {
319 SkASSERT(!fPurging);
320 SkASSERT(!fNewlyPurgableResourceWhilePurging);
321
322 fPurging = true;
323
324 AutoValidate av(this); // Put this after setting fPurging so we're allowed t o be over budget.
325
326 do {
327 fNewlyPurgableResourceWhilePurging = false;
328 ResourceList::Iter resourceIter;
329 GrGpuResource* resource =
330 resourceIter.init(fResources, ResourceList::Iter::kTail_IterStart);
331
332 while (resource) {
robertphillips 2014/11/13 14:34:03 same here ?
bsalomon 2014/11/13 14:51:50 Done.
333 if (resource->isPurgable()) {
334 GrGpuResource* prev = resourceIter.prev();
335 resource->release();
336 resource->unref();
337 resource = prev;
338 } else {
339 resource = resourceIter.prev();
340 }
341 }
342
robertphillips 2014/11/13 14:34:03 Does this belong here ?
bsalomon 2014/11/13 14:51:50 I think so. If the cache was asked to purge everyt
343 if (!fNewlyPurgableResourceWhilePurging && fCount && fOverBudgetCB) {
344 (*fOverBudgetCB)(fOverBudgetData);
345 }
346 } while (fNewlyPurgableResourceWhilePurging);
347 fPurging = false;
348 }
349
350 #ifdef SK_DEBUG
351 void GrResourceCache2::validate() const {
352 size_t bytes = 0;
353 int count = 0;
354 int locked = 0;
355 int scratch = 0;
356 int couldBeScratch = 0;
357 int content = 0;
358
359 ResourceList::Iter iter;
360 GrGpuResource* resource = iter.init(fResources, ResourceList::Iter::kHead_It erStart);
361 for ( ; resource; resource = iter.next()) {
362 bytes += resource->gpuMemorySize();
363 ++count;
364
365 if (!resource->isPurgable()) {
366 ++locked;
367 }
368
369 if (resource->cacheAccess().isScratch()) {
370 SkASSERT(NULL == resource->cacheAccess().getContentKey());
371 ++scratch;
372 SkASSERT(fScratchMap.countForKey(resource->cacheAccess().getScratchK ey()));
373 } else if (!resource->cacheAccess().getScratchKey().isNullScratch()) {
374 SkASSERT(NULL != resource->cacheAccess().getContentKey());
375 ++couldBeScratch;
376 SkASSERT(fScratchMap.countForKey(resource->cacheAccess().getScratchK ey()));
377 }
378
379 if (const GrResourceKey* contentKey = resource->cacheAccess().getContent Key()) {
380 ++content;
381 SkASSERT(fContentHash.find(*contentKey) == resource);
382 }
383 }
384
385 SkASSERT(bytes == fBytes);
386 SkASSERT(count == fCount);
387 #if GR_CACHE_STATS
388 SkASSERT(bytes <= fHighWaterBytes);
389 SkASSERT(count <= fHighWaterCount);
390 #endif
391 SkASSERT(content == fContentHash.count());
392 SkASSERT(scratch + couldBeScratch == fScratchMap.count());
393
394 bool overBudget = bytes > fMaxBytes || count > fMaxCount;
395 SkASSERT(!overBudget || locked == count || fPurging);
396 }
397 #endif
398
399 #if GR_CACHE_STATS
400 void GrResourceCache2::printStats() const {
401 this->validate();
402
403 int locked = 0;
404 int scratch = 0;
405
406 ResourceList::Iter iter;
407 GrGpuResource* resource = iter.init(fResources, ResourceList::Iter::kHead_It erStart);
408
409 for ( ; resource; resource = iter.next()) {
410 if (!resource->isPurgable()) {
411 ++locked;
412 }
413 if (resource->cacheAccess().isScratch()) {
414 ++scratch;
415 }
416 }
417
418 float countUtilization = (100.f * fCount) / fMaxCount;
419 float byteUtilization = (100.f * fBytes) / fMaxBytes;
420
421 SkDebugf("Budget: %d items %d bytes\n", fMaxCount, fMaxBytes);
422 SkDebugf("\t\tEntry Count: current %d (%d locked, %d scratch %.2g%% full), h igh %d\n",
423 fCount, locked, scratch, countUtilization, fHighWaterCount);
424 SkDebugf("\t\tEntry Bytes: current %d (%.2g%% full) high %d\n",
425 fBytes, byteUtilization, fHighWaterBytes);
426 }
427
428 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698