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 #include "SkRefCnt.h" | |
13 | 12 |
14 GrResourceCache2::~GrResourceCache2() { | 13 GrResourceCache2::~GrResourceCache2() { |
15 this->releaseAll(); | 14 this->releaseAll(); |
16 } | 15 } |
17 | 16 |
18 void GrResourceCache2::insertResource(GrGpuResource* resource) { | 17 void GrResourceCache2::insertResource(GrGpuResource* resource) { |
19 SkASSERT(resource); | 18 SkASSERT(resource); |
20 SkASSERT(!resource->wasDestroyed()); | 19 SkASSERT(!resource->wasDestroyed()); |
21 SkASSERT(!this->isInCache(resource)); | 20 SkASSERT(!this->isInCache(resource)); |
22 fResources.addToHead(resource); | 21 fResources.addToHead(resource); |
23 ++fCount; | 22 ++fCount; |
24 if (!resource->getScratchKey().isNullScratch()) { | 23 if (!resource->getScratchKey().isNullScratch()) { |
| 24 // TODO(bsalomon): Make this assertion possible. |
| 25 // SkASSERT(!resource->isWrapped()); |
25 fScratchMap.insert(resource->getScratchKey(), resource); | 26 fScratchMap.insert(resource->getScratchKey(), resource); |
26 } | 27 } |
27 } | 28 } |
28 | 29 |
29 void GrResourceCache2::removeResource(GrGpuResource* resource) { | 30 void GrResourceCache2::removeResource(GrGpuResource* resource) { |
30 SkASSERT(this->isInCache(resource)); | 31 SkASSERT(this->isInCache(resource)); |
31 fResources.remove(resource); | 32 fResources.remove(resource); |
32 if (!resource->getScratchKey().isNullScratch()) { | 33 if (!resource->getScratchKey().isNullScratch()) { |
33 fScratchMap.remove(resource->getScratchKey(), resource); | 34 fScratchMap.remove(resource->getScratchKey(), resource); |
34 } | 35 } |
| 36 if (const GrResourceKey* contentKey = resource->getContentKey()) { |
| 37 fContentHash.remove(*contentKey); |
| 38 } |
35 --fCount; | 39 --fCount; |
36 } | 40 } |
37 | 41 |
38 void GrResourceCache2::abandonAll() { | 42 void GrResourceCache2::abandonAll() { |
39 while (GrGpuResource* head = fResources.head()) { | 43 while (GrGpuResource* head = fResources.head()) { |
40 SkASSERT(!head->wasDestroyed()); | 44 SkASSERT(!head->wasDestroyed()); |
41 head->abandon(); | 45 head->abandon(); |
42 // abandon should have already removed this from the list. | 46 // abandon should have already removed this from the list. |
43 SkASSERT(head != fResources.head()); | 47 SkASSERT(head != fResources.head()); |
44 } | 48 } |
45 SkASSERT(!fScratchMap.count()); | 49 SkASSERT(!fScratchMap.count()); |
| 50 SkASSERT(!fContentHash.count()); |
46 SkASSERT(!fCount); | 51 SkASSERT(!fCount); |
47 } | 52 } |
48 | 53 |
49 void GrResourceCache2::releaseAll() { | 54 void GrResourceCache2::releaseAll() { |
50 while (GrGpuResource* head = fResources.head()) { | 55 while (GrGpuResource* head = fResources.head()) { |
51 SkASSERT(!head->wasDestroyed()); | 56 SkASSERT(!head->wasDestroyed()); |
52 head->release(); | 57 head->release(); |
53 // release should have already removed this from the list. | 58 // release should have already removed this from the list. |
54 SkASSERT(head != fResources.head()); | 59 SkASSERT(head != fResources.head()); |
55 } | 60 } |
(...skipping 26 matching lines...) Expand all Loading... |
82 if (resource) { | 87 if (resource) { |
83 return SkRef(resource); | 88 return SkRef(resource); |
84 } else if (flags & kRequireNoPendingIO_ScratchFlag) { | 89 } else if (flags & kRequireNoPendingIO_ScratchFlag) { |
85 return NULL; | 90 return NULL; |
86 } | 91 } |
87 // TODO: fail here when kPrefer is specified, we didn't find a resource
without pending io, | 92 // TODO: fail here when kPrefer is specified, we didn't find a resource
without pending io, |
88 // but there is still space in our budget for the resource. | 93 // but there is still space in our budget for the resource. |
89 } | 94 } |
90 return SkSafeRef(fScratchMap.find(scratchKey, AvailableForScratchUse(false))
); | 95 return SkSafeRef(fScratchMap.find(scratchKey, AvailableForScratchUse(false))
); |
91 } | 96 } |
| 97 |
| 98 void GrResourceCache2::willRemoveContentKey(const GrGpuResource* resource) { |
| 99 SkASSERT(resource); |
| 100 SkASSERT(resource->getContentKey()); |
| 101 SkDEBUGCODE(GrGpuResource* res = fContentHash.find(*resource->getContentKey(
))); |
| 102 SkASSERT(res == resource); |
| 103 |
| 104 fContentHash.remove(*resource->getContentKey()); |
| 105 } |
| 106 |
| 107 bool GrResourceCache2::didAddContentKey(GrGpuResource* resource) { |
| 108 SkASSERT(resource); |
| 109 SkASSERT(resource->getContentKey()); |
| 110 |
| 111 GrGpuResource* res = fContentHash.find(*resource->getContentKey()); |
| 112 if (NULL != res) { |
| 113 return false; |
| 114 } |
| 115 |
| 116 fContentHash.add(resource); |
| 117 return true; |
| 118 } |
OLD | NEW |