bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2014 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
| 9 | |
| 10 | #include "GrResourceCache2.h" |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 11 | #include "GrGpuResource.h" |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 12 | |
| 13 | GrResourceCache2::~GrResourceCache2() { |
| 14 | this->releaseAll(); |
| 15 | } |
| 16 | |
| 17 | void GrResourceCache2::insertResource(GrGpuResource* resource) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 18 | SkASSERT(resource); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 19 | SkASSERT(!resource->wasDestroyed()); |
bsalomon | 1696126 | 2014-08-26 14:01:07 -0700 | [diff] [blame] | 20 | SkASSERT(!this->isInCache(resource)); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 21 | fResources.addToHead(resource); |
| 22 | ++fCount; |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 23 | if (!resource->getScratchKey().isNullScratch()) { |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame^] | 24 | // TODO(bsalomon): Make this assertion possible. |
| 25 | // SkASSERT(!resource->isWrapped()); |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 26 | fScratchMap.insert(resource->getScratchKey(), resource); |
| 27 | } |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | void GrResourceCache2::removeResource(GrGpuResource* resource) { |
bsalomon | 1696126 | 2014-08-26 14:01:07 -0700 | [diff] [blame] | 31 | SkASSERT(this->isInCache(resource)); |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 32 | fResources.remove(resource); |
| 33 | if (!resource->getScratchKey().isNullScratch()) { |
| 34 | fScratchMap.remove(resource->getScratchKey(), resource); |
| 35 | } |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame^] | 36 | if (const GrResourceKey* contentKey = resource->getContentKey()) { |
| 37 | fContentHash.remove(*contentKey); |
| 38 | } |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 39 | --fCount; |
| 40 | } |
| 41 | |
| 42 | void GrResourceCache2::abandonAll() { |
| 43 | while (GrGpuResource* head = fResources.head()) { |
| 44 | SkASSERT(!head->wasDestroyed()); |
| 45 | head->abandon(); |
| 46 | // abandon should have already removed this from the list. |
| 47 | SkASSERT(head != fResources.head()); |
| 48 | } |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 49 | SkASSERT(!fScratchMap.count()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame^] | 50 | SkASSERT(!fContentHash.count()); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 51 | SkASSERT(!fCount); |
| 52 | } |
| 53 | |
| 54 | void GrResourceCache2::releaseAll() { |
| 55 | while (GrGpuResource* head = fResources.head()) { |
| 56 | SkASSERT(!head->wasDestroyed()); |
| 57 | head->release(); |
| 58 | // release should have already removed this from the list. |
| 59 | SkASSERT(head != fResources.head()); |
| 60 | } |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 61 | SkASSERT(!fScratchMap.count()); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 62 | SkASSERT(!fCount); |
| 63 | } |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 64 | |
| 65 | class GrResourceCache2::AvailableForScratchUse { |
| 66 | public: |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 67 | AvailableForScratchUse(bool rejectPendingIO) : fRejectPendingIO(rejectPendingIO) { } |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 68 | |
| 69 | bool operator()(const GrGpuResource* resource) const { |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 70 | if (!resource->reffedOnlyByCache() || !resource->isScratch()) { |
| 71 | return false; |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 72 | } |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 73 | |
| 74 | return !fRejectPendingIO || !resource->internalHasPendingIO(); |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 75 | } |
bsalomon | 1e2530b | 2014-10-09 09:57:18 -0700 | [diff] [blame] | 76 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 77 | private: |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 78 | bool fRejectPendingIO; |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | GrGpuResource* GrResourceCache2::findAndRefScratchResource(const GrResourceKey& scratchKey, |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 82 | uint32_t flags) { |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 83 | SkASSERT(scratchKey.isScratch()); |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 84 | |
| 85 | if (flags & (kPreferNoPendingIO_ScratchFlag | kRequireNoPendingIO_ScratchFlag)) { |
| 86 | GrGpuResource* resource = fScratchMap.find(scratchKey, AvailableForScratchUse(true)); |
| 87 | if (resource) { |
| 88 | return SkRef(resource); |
| 89 | } else if (flags & kRequireNoPendingIO_ScratchFlag) { |
| 90 | return NULL; |
| 91 | } |
| 92 | // TODO: fail here when kPrefer is specified, we didn't find a resource without pending io, |
| 93 | // but there is still space in our budget for the resource. |
| 94 | } |
| 95 | return SkSafeRef(fScratchMap.find(scratchKey, AvailableForScratchUse(false))); |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 96 | } |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame^] | 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 | } |