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

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

Issue 726913002: Revert of Replace GrResourceCache with GrResourceCache2. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: 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 | « src/gpu/GrResourceCache2.h ('k') | src/gpu/GrStencilBuffer.cpp » ('j') | 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"
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
20 GrResourceKey& GrResourceKey::NullScratchKey() { 13 GrResourceKey& GrResourceKey::NullScratchKey() {
21 static const GrCacheID::Key kBogusKey = { { {0} } }; 14 static const GrCacheID::Key kBogusKey = { { {0} } };
22 static GrCacheID kBogusID(ScratchDomain(), kBogusKey); 15 static GrCacheID kBogusID(ScratchDomain(), kBogusKey);
23 static GrResourceKey kNullScratchKey(kBogusID, NoneResourceType(), 0); 16 static GrResourceKey kNullScratchKey(kBogusID, NoneResourceType(), 0);
24 return kNullScratchKey; 17 return kNullScratchKey;
25 } 18 }
26 19
27 GrResourceKey::ResourceType GrResourceKey::NoneResourceType() { 20 GrResourceKey::ResourceType GrResourceKey::NoneResourceType() {
28 static const ResourceType gNoneResourceType = GenerateResourceType(); 21 static const ResourceType gNoneResourceType = GenerateResourceType();
29 return gNoneResourceType; 22 return gNoneResourceType;
30 } 23 }
31 24
32 GrCacheID::Domain GrResourceKey::ScratchDomain() { 25 GrCacheID::Domain GrResourceKey::ScratchDomain() {
33 static const GrCacheID::Domain gDomain = GrCacheID::GenerateDomain(); 26 static const GrCacheID::Domain gDomain = GrCacheID::GenerateDomain();
34 return gDomain; 27 return gDomain;
35 } 28 }
36 29
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
48 ////////////////////////////////////////////////////////////////////////////// 30 //////////////////////////////////////////////////////////////////////////////
49 31
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
78 GrResourceCache2::~GrResourceCache2() { 32 GrResourceCache2::~GrResourceCache2() {
79 this->releaseAll(); 33 this->releaseAll();
80 } 34 }
81 35
82 void GrResourceCache2::setLimits(int count, size_t bytes) {
83 fMaxCount = count;
84 fMaxBytes = bytes;
85 this->purgeAsNeeded();
86 }
87
88 void GrResourceCache2::insertResource(GrGpuResource* resource) { 36 void GrResourceCache2::insertResource(GrGpuResource* resource) {
89 AutoValidate av(this);
90
91 SkASSERT(resource); 37 SkASSERT(resource);
92 SkASSERT(!resource->wasDestroyed()); 38 SkASSERT(!resource->wasDestroyed());
93 SkASSERT(!this->isInCache(resource)); 39 SkASSERT(!this->isInCache(resource));
94 SkASSERT(!fPurging);
95 fResources.addToHead(resource); 40 fResources.addToHead(resource);
96 resource->ref();
97
98 ++fCount; 41 ++fCount;
99 SkDEBUGCODE(fHighWaterCount = SkTMax(fCount, fHighWaterCount));
100 fBytes += resource->gpuMemorySize();
101 SkDEBUGCODE(fHighWaterBytes = SkTMax(fBytes, fHighWaterBytes));
102 if (!resource->cacheAccess().getScratchKey().isNullScratch()) { 42 if (!resource->cacheAccess().getScratchKey().isNullScratch()) {
103 // TODO(bsalomon): Make this assertion possible. 43 // TODO(bsalomon): Make this assertion possible.
104 // SkASSERT(!resource->isWrapped()); 44 // SkASSERT(!resource->isWrapped());
105 fScratchMap.insert(resource->cacheAccess().getScratchKey(), resource); 45 fScratchMap.insert(resource->cacheAccess().getScratchKey(), resource);
106 } 46 }
107
108 this->purgeAsNeeded();
109 } 47 }
110 48
111 void GrResourceCache2::removeResource(GrGpuResource* resource) { 49 void GrResourceCache2::removeResource(GrGpuResource* resource) {
112 AutoValidate av(this);
113
114 --fCount;
115 fBytes -= resource->gpuMemorySize();
116 SkASSERT(this->isInCache(resource)); 50 SkASSERT(this->isInCache(resource));
117 fResources.remove(resource); 51 fResources.remove(resource);
118 if (!resource->cacheAccess().getScratchKey().isNullScratch()) { 52 if (!resource->cacheAccess().getScratchKey().isNullScratch()) {
119 fScratchMap.remove(resource->cacheAccess().getScratchKey(), resource); 53 fScratchMap.remove(resource->cacheAccess().getScratchKey(), resource);
120 } 54 }
121 if (const GrResourceKey* contentKey = resource->cacheAccess().getContentKey( )) { 55 if (const GrResourceKey* contentKey = resource->cacheAccess().getContentKey( )) {
122 fContentHash.remove(*contentKey); 56 fContentHash.remove(*contentKey);
123 } 57 }
58 --fCount;
124 } 59 }
125 60
126 void GrResourceCache2::abandonAll() { 61 void GrResourceCache2::abandonAll() {
127 AutoValidate av(this);
128
129 SkASSERT(!fPurging);
130 while (GrGpuResource* head = fResources.head()) { 62 while (GrGpuResource* head = fResources.head()) {
131 SkASSERT(!head->wasDestroyed()); 63 SkASSERT(!head->wasDestroyed());
132 head->abandon(); 64 head->abandon();
133 head->unref();
134 // abandon should have already removed this from the list. 65 // abandon should have already removed this from the list.
135 SkASSERT(head != fResources.head()); 66 SkASSERT(head != fResources.head());
136 } 67 }
137 SkASSERT(!fScratchMap.count()); 68 SkASSERT(!fScratchMap.count());
138 SkASSERT(!fContentHash.count()); 69 SkASSERT(!fContentHash.count());
139 SkASSERT(!fCount); 70 SkASSERT(!fCount);
140 } 71 }
141 72
142 void GrResourceCache2::releaseAll() { 73 void GrResourceCache2::releaseAll() {
143 AutoValidate av(this);
144
145 SkASSERT(!fPurging);
146 while (GrGpuResource* head = fResources.head()) { 74 while (GrGpuResource* head = fResources.head()) {
147 SkASSERT(!head->wasDestroyed()); 75 SkASSERT(!head->wasDestroyed());
148 head->release(); 76 head->release();
149 head->unref();
150 // release should have already removed this from the list. 77 // release should have already removed this from the list.
151 SkASSERT(head != fResources.head()); 78 SkASSERT(head != fResources.head());
152 } 79 }
153 SkASSERT(!fScratchMap.count()); 80 SkASSERT(!fScratchMap.count());
154 SkASSERT(!fCount); 81 SkASSERT(!fCount);
155 } 82 }
156 83
157 class GrResourceCache2::AvailableForScratchUse { 84 class GrResourceCache2::AvailableForScratchUse {
158 public: 85 public:
159 AvailableForScratchUse(bool rejectPendingIO) : fRejectPendingIO(rejectPendin gIO) { } 86 AvailableForScratchUse(bool rejectPendingIO) : fRejectPendingIO(rejectPendin gIO) { }
160 87
161 bool operator()(const GrGpuResource* resource) const { 88 bool operator()(const GrGpuResource* resource) const {
162 if (!resource->reffedOnlyByCache() || !resource->cacheAccess().isScratch ()) { 89 if (!resource->reffedOnlyByCache() || !resource->cacheAccess().isScratch ()) {
163 return false; 90 return false;
164 } 91 }
165 92
166 return !fRejectPendingIO || !resource->internalHasPendingIO(); 93 return !fRejectPendingIO || !resource->internalHasPendingIO();
167 } 94 }
168 95
169 private: 96 private:
170 bool fRejectPendingIO; 97 bool fRejectPendingIO;
171 }; 98 };
172 99
173 GrGpuResource* GrResourceCache2::findAndRefScratchResource(const GrResourceKey& scratchKey, 100 GrGpuResource* GrResourceCache2::findAndRefScratchResource(const GrResourceKey& scratchKey,
174 uint32_t flags) { 101 uint32_t flags) {
175 AutoValidate av(this);
176
177 SkASSERT(!fPurging);
178 SkASSERT(scratchKey.isScratch()); 102 SkASSERT(scratchKey.isScratch());
179 103
180 GrGpuResource* resource;
181 if (flags & (kPreferNoPendingIO_ScratchFlag | kRequireNoPendingIO_ScratchFla g)) { 104 if (flags & (kPreferNoPendingIO_ScratchFlag | kRequireNoPendingIO_ScratchFla g)) {
182 resource = fScratchMap.find(scratchKey, AvailableForScratchUse(true)); 105 GrGpuResource* resource = fScratchMap.find(scratchKey, AvailableForScrat chUse(true));
183 if (resource) { 106 if (resource) {
184 this->makeResourceMRU(resource);
185 return SkRef(resource); 107 return SkRef(resource);
186 } else if (flags & kRequireNoPendingIO_ScratchFlag) { 108 } else if (flags & kRequireNoPendingIO_ScratchFlag) {
187 return NULL; 109 return NULL;
188 } 110 }
189 // TODO: fail here when kPrefer is specified, we didn't find a resource without pending io, 111 // TODO: fail here when kPrefer is specified, we didn't find a resource without pending io,
190 // but there is still space in our budget for the resource. 112 // but there is still space in our budget for the resource.
191 } 113 }
192 resource = fScratchMap.find(scratchKey, AvailableForScratchUse(false)); 114 return SkSafeRef(fScratchMap.find(scratchKey, AvailableForScratchUse(false)) );
193 if (resource) {
194 resource->ref();
195 this->makeResourceMRU(resource);
196 }
197 return resource;
198 } 115 }
199 116
200 bool GrResourceCache2::didSetContentKey(GrGpuResource* resource) { 117 bool GrResourceCache2::didSetContentKey(GrGpuResource* resource) {
201 SkASSERT(!fPurging);
202 SkASSERT(resource); 118 SkASSERT(resource);
203 SkASSERT(this->isInCache(resource));
204 SkASSERT(resource->cacheAccess().getContentKey()); 119 SkASSERT(resource->cacheAccess().getContentKey());
205 SkASSERT(!resource->cacheAccess().getContentKey()->isScratch()); 120 SkASSERT(!resource->cacheAccess().getContentKey()->isScratch());
206 121
207 GrGpuResource* res = fContentHash.find(*resource->cacheAccess().getContentKe y()); 122 GrGpuResource* res = fContentHash.find(*resource->cacheAccess().getContentKe y());
208 if (NULL != res) { 123 if (NULL != res) {
209 return false; 124 return false;
210 } 125 }
211 126
212 fContentHash.add(resource); 127 fContentHash.add(resource);
213 this->validate();
214 return true; 128 return true;
215 } 129 }
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) {
295 GrGpuResource* prev = resourceIter.prev();
296 if (resource->isPurgable()) {
297 resource->unref();
298 }
299 resource = prev;
300 if (fCount <= fMaxCount && fBytes <= fMaxBytes) {
301 overBudget = false;
302 resource = NULL;
303 }
304 }
305
306 if (!fNewlyPurgableResourceWhilePurging && overBudget && fOverBudgetCB) {
307 // Despite the purge we're still over budget. Call our over budget c allback.
308 (*fOverBudgetCB)(fOverBudgetData);
309 }
310 } while (overBudget && fNewlyPurgableResourceWhilePurging);
311
312 fNewlyPurgableResourceWhilePurging = false;
313 fPurging = false;
314 }
315
316 void GrResourceCache2::purgeAllUnlocked() {
317 SkASSERT(!fPurging);
318 SkASSERT(!fNewlyPurgableResourceWhilePurging);
319
320 fPurging = true;
321
322 AutoValidate av(this); // Put this after setting fPurging so we're allowed t o be over budget.
323
324 do {
325 fNewlyPurgableResourceWhilePurging = false;
326 ResourceList::Iter resourceIter;
327 GrGpuResource* resource =
328 resourceIter.init(fResources, ResourceList::Iter::kTail_IterStart);
329
330 while (resource) {
331 GrGpuResource* prev = resourceIter.prev();
332 if (resource->isPurgable()) {
333 resource->unref();
334 }
335 resource = prev;
336 }
337
338 if (!fNewlyPurgableResourceWhilePurging && fCount && fOverBudgetCB) {
339 (*fOverBudgetCB)(fOverBudgetData);
340 }
341 } while (fNewlyPurgableResourceWhilePurging);
342 fPurging = false;
343 }
344
345 #ifdef SK_DEBUG
346 void GrResourceCache2::validate() const {
347 size_t bytes = 0;
348 int count = 0;
349 int locked = 0;
350 int scratch = 0;
351 int couldBeScratch = 0;
352 int content = 0;
353
354 ResourceList::Iter iter;
355 GrGpuResource* resource = iter.init(fResources, ResourceList::Iter::kHead_It erStart);
356 for ( ; resource; resource = iter.next()) {
357 bytes += resource->gpuMemorySize();
358 ++count;
359
360 if (!resource->isPurgable()) {
361 ++locked;
362 }
363
364 if (resource->cacheAccess().isScratch()) {
365 SkASSERT(NULL == resource->cacheAccess().getContentKey());
366 ++scratch;
367 SkASSERT(fScratchMap.countForKey(resource->cacheAccess().getScratchK ey()));
368 } else if (!resource->cacheAccess().getScratchKey().isNullScratch()) {
369 SkASSERT(NULL != resource->cacheAccess().getContentKey());
370 ++couldBeScratch;
371 SkASSERT(fScratchMap.countForKey(resource->cacheAccess().getScratchK ey()));
372 }
373
374 if (const GrResourceKey* contentKey = resource->cacheAccess().getContent Key()) {
375 ++content;
376 SkASSERT(fContentHash.find(*contentKey) == resource);
377 }
378 }
379
380 SkASSERT(bytes == fBytes);
381 SkASSERT(count == fCount);
382 #if GR_CACHE_STATS
383 SkASSERT(bytes <= fHighWaterBytes);
384 SkASSERT(count <= fHighWaterCount);
385 #endif
386 SkASSERT(content == fContentHash.count());
387 SkASSERT(scratch + couldBeScratch == fScratchMap.count());
388
389 bool overBudget = bytes > fMaxBytes || count > fMaxCount;
390 SkASSERT(!overBudget || locked == count || fPurging);
391 }
392 #endif
393
394 #if GR_CACHE_STATS
395 void GrResourceCache2::printStats() const {
396 this->validate();
397
398 int locked = 0;
399 int scratch = 0;
400
401 ResourceList::Iter iter;
402 GrGpuResource* resource = iter.init(fResources, ResourceList::Iter::kHead_It erStart);
403
404 for ( ; resource; resource = iter.next()) {
405 if (!resource->isPurgable()) {
406 ++locked;
407 }
408 if (resource->cacheAccess().isScratch()) {
409 ++scratch;
410 }
411 }
412
413 float countUtilization = (100.f * fCount) / fMaxCount;
414 float byteUtilization = (100.f * fBytes) / fMaxBytes;
415
416 SkDebugf("Budget: %d items %d bytes\n", fMaxCount, fMaxBytes);
417 SkDebugf("\t\tEntry Count: current %d (%d locked, %d scratch %.2g%% full), h igh %d\n",
418 fCount, locked, scratch, countUtilization, fHighWaterCount);
419 SkDebugf("\t\tEntry Bytes: current %d (%.2g%% full) high %d\n",
420 fBytes, byteUtilization, fHighWaterBytes);
421 }
422
423 #endif
OLDNEW
« no previous file with comments | « src/gpu/GrResourceCache2.h ('k') | src/gpu/GrStencilBuffer.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698