| 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" |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 GrResourceCache2::~GrResourceCache2() { | 32 GrResourceCache2::~GrResourceCache2() { |
| 33 this->releaseAll(); | 33 this->releaseAll(); |
| 34 } | 34 } |
| 35 | 35 |
| 36 void GrResourceCache2::insertResource(GrGpuResource* resource) { | 36 void GrResourceCache2::insertResource(GrGpuResource* resource) { |
| 37 SkASSERT(resource); | 37 SkASSERT(resource); |
| 38 SkASSERT(!resource->wasDestroyed()); | 38 SkASSERT(!resource->wasDestroyed()); |
| 39 SkASSERT(!this->isInCache(resource)); | 39 SkASSERT(!this->isInCache(resource)); |
| 40 fResources.addToHead(resource); | 40 fResources.addToHead(resource); |
| 41 ++fCount; | 41 ++fCount; |
| 42 if (!resource->getScratchKey().isNullScratch()) { | 42 if (!resource->cacheAccess().getScratchKey().isNullScratch()) { |
| 43 // TODO(bsalomon): Make this assertion possible. | 43 // TODO(bsalomon): Make this assertion possible. |
| 44 // SkASSERT(!resource->isWrapped()); | 44 // SkASSERT(!resource->isWrapped()); |
| 45 fScratchMap.insert(resource->getScratchKey(), resource); | 45 fScratchMap.insert(resource->cacheAccess().getScratchKey(), resource); |
| 46 } | 46 } |
| 47 } | 47 } |
| 48 | 48 |
| 49 void GrResourceCache2::removeResource(GrGpuResource* resource) { | 49 void GrResourceCache2::removeResource(GrGpuResource* resource) { |
| 50 SkASSERT(this->isInCache(resource)); | 50 SkASSERT(this->isInCache(resource)); |
| 51 fResources.remove(resource); | 51 fResources.remove(resource); |
| 52 if (!resource->getScratchKey().isNullScratch()) { | 52 if (!resource->cacheAccess().getScratchKey().isNullScratch()) { |
| 53 fScratchMap.remove(resource->getScratchKey(), resource); | 53 fScratchMap.remove(resource->cacheAccess().getScratchKey(), resource); |
| 54 } | 54 } |
| 55 if (const GrResourceKey* contentKey = resource->getContentKey()) { | 55 if (const GrResourceKey* contentKey = resource->cacheAccess().getContentKey(
)) { |
| 56 fContentHash.remove(*contentKey); | 56 fContentHash.remove(*contentKey); |
| 57 } | 57 } |
| 58 --fCount; | 58 --fCount; |
| 59 } | 59 } |
| 60 | 60 |
| 61 void GrResourceCache2::abandonAll() { | 61 void GrResourceCache2::abandonAll() { |
| 62 while (GrGpuResource* head = fResources.head()) { | 62 while (GrGpuResource* head = fResources.head()) { |
| 63 SkASSERT(!head->wasDestroyed()); | 63 SkASSERT(!head->wasDestroyed()); |
| 64 head->abandon(); | 64 head->abandon(); |
| 65 // abandon should have already removed this from the list. | 65 // abandon should have already removed this from the list. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 79 } | 79 } |
| 80 SkASSERT(!fScratchMap.count()); | 80 SkASSERT(!fScratchMap.count()); |
| 81 SkASSERT(!fCount); | 81 SkASSERT(!fCount); |
| 82 } | 82 } |
| 83 | 83 |
| 84 class GrResourceCache2::AvailableForScratchUse { | 84 class GrResourceCache2::AvailableForScratchUse { |
| 85 public: | 85 public: |
| 86 AvailableForScratchUse(bool rejectPendingIO) : fRejectPendingIO(rejectPendin
gIO) { } | 86 AvailableForScratchUse(bool rejectPendingIO) : fRejectPendingIO(rejectPendin
gIO) { } |
| 87 | 87 |
| 88 bool operator()(const GrGpuResource* resource) const { | 88 bool operator()(const GrGpuResource* resource) const { |
| 89 if (!resource->reffedOnlyByCache() || !resource->isScratch()) { | 89 if (!resource->reffedOnlyByCache() || !resource->cacheAccess().isScratch
()) { |
| 90 return false; | 90 return false; |
| 91 } | 91 } |
| 92 | 92 |
| 93 return !fRejectPendingIO || !resource->internalHasPendingIO(); | 93 return !fRejectPendingIO || !resource->internalHasPendingIO(); |
| 94 } | 94 } |
| 95 | 95 |
| 96 private: | 96 private: |
| 97 bool fRejectPendingIO; | 97 bool fRejectPendingIO; |
| 98 }; | 98 }; |
| 99 | 99 |
| 100 GrGpuResource* GrResourceCache2::findAndRefScratchResource(const GrResourceKey&
scratchKey, | 100 GrGpuResource* GrResourceCache2::findAndRefScratchResource(const GrResourceKey&
scratchKey, |
| 101 uint32_t flags) { | 101 uint32_t flags) { |
| 102 SkASSERT(scratchKey.isScratch()); | 102 SkASSERT(scratchKey.isScratch()); |
| 103 | 103 |
| 104 if (flags & (kPreferNoPendingIO_ScratchFlag | kRequireNoPendingIO_ScratchFla
g)) { | 104 if (flags & (kPreferNoPendingIO_ScratchFlag | kRequireNoPendingIO_ScratchFla
g)) { |
| 105 GrGpuResource* resource = fScratchMap.find(scratchKey, AvailableForScrat
chUse(true)); | 105 GrGpuResource* resource = fScratchMap.find(scratchKey, AvailableForScrat
chUse(true)); |
| 106 if (resource) { | 106 if (resource) { |
| 107 return SkRef(resource); | 107 return SkRef(resource); |
| 108 } else if (flags & kRequireNoPendingIO_ScratchFlag) { | 108 } else if (flags & kRequireNoPendingIO_ScratchFlag) { |
| 109 return NULL; | 109 return NULL; |
| 110 } | 110 } |
| 111 // 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, |
| 112 // but there is still space in our budget for the resource. | 112 // but there is still space in our budget for the resource. |
| 113 } | 113 } |
| 114 return SkSafeRef(fScratchMap.find(scratchKey, AvailableForScratchUse(false))
); | 114 return SkSafeRef(fScratchMap.find(scratchKey, AvailableForScratchUse(false))
); |
| 115 } | 115 } |
| 116 | 116 |
| 117 #if 0 | |
| 118 void GrResourceCache2::willRemoveContentKey(const GrGpuResource* resource) { | |
| 119 SkASSERT(resource); | |
| 120 SkASSERT(resource->getContentKey()); | |
| 121 SkDEBUGCODE(GrGpuResource* res = fContentHash.find(*resource->getContentKey(
))); | |
| 122 SkASSERT(res == resource); | |
| 123 | |
| 124 fContentHash.remove(*resource->getContentKey()); | |
| 125 } | |
| 126 #endif | |
| 127 | |
| 128 bool GrResourceCache2::didSetContentKey(GrGpuResource* resource) { | 117 bool GrResourceCache2::didSetContentKey(GrGpuResource* resource) { |
| 129 SkASSERT(resource); | 118 SkASSERT(resource); |
| 130 SkASSERT(resource->getContentKey()); | 119 SkASSERT(resource->cacheAccess().getContentKey()); |
| 131 SkASSERT(!resource->getContentKey()->isScratch()); | 120 SkASSERT(!resource->cacheAccess().getContentKey()->isScratch()); |
| 132 | 121 |
| 133 GrGpuResource* res = fContentHash.find(*resource->getContentKey()); | 122 GrGpuResource* res = fContentHash.find(*resource->cacheAccess().getContentKe
y()); |
| 134 if (NULL != res) { | 123 if (NULL != res) { |
| 135 return false; | 124 return false; |
| 136 } | 125 } |
| 137 | 126 |
| 138 fContentHash.add(resource); | 127 fContentHash.add(resource); |
| 139 return true; | 128 return true; |
| 140 } | 129 } |
| OLD | NEW |