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 | |
bsalomon | fe369ee | 2014-11-10 11:59:06 -0800 | [diff] [blame^] | 13 | GrResourceKey& GrResourceKey::NullScratchKey() { |
| 14 | static const GrCacheID::Key kBogusKey = { { {0} } }; |
| 15 | static GrCacheID kBogusID(ScratchDomain(), kBogusKey); |
| 16 | static GrResourceKey kNullScratchKey(kBogusID, NoneResourceType(), 0); |
| 17 | return kNullScratchKey; |
| 18 | } |
| 19 | |
| 20 | GrResourceKey::ResourceType GrResourceKey::NoneResourceType() { |
| 21 | static const ResourceType gNoneResourceType = GenerateResourceType(); |
| 22 | return gNoneResourceType; |
| 23 | } |
| 24 | |
| 25 | GrCacheID::Domain GrResourceKey::ScratchDomain() { |
| 26 | static const GrCacheID::Domain gDomain = GrCacheID::GenerateDomain(); |
| 27 | return gDomain; |
| 28 | } |
| 29 | |
| 30 | ////////////////////////////////////////////////////////////////////////////// |
| 31 | |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 32 | GrResourceCache2::~GrResourceCache2() { |
| 33 | this->releaseAll(); |
| 34 | } |
| 35 | |
| 36 | void GrResourceCache2::insertResource(GrGpuResource* resource) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 37 | SkASSERT(resource); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 38 | SkASSERT(!resource->wasDestroyed()); |
bsalomon | 1696126 | 2014-08-26 14:01:07 -0700 | [diff] [blame] | 39 | SkASSERT(!this->isInCache(resource)); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 40 | fResources.addToHead(resource); |
| 41 | ++fCount; |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 42 | if (!resource->getScratchKey().isNullScratch()) { |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 43 | // TODO(bsalomon): Make this assertion possible. |
| 44 | // SkASSERT(!resource->isWrapped()); |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 45 | fScratchMap.insert(resource->getScratchKey(), resource); |
| 46 | } |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | void GrResourceCache2::removeResource(GrGpuResource* resource) { |
bsalomon | 1696126 | 2014-08-26 14:01:07 -0700 | [diff] [blame] | 50 | SkASSERT(this->isInCache(resource)); |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 51 | fResources.remove(resource); |
| 52 | if (!resource->getScratchKey().isNullScratch()) { |
| 53 | fScratchMap.remove(resource->getScratchKey(), resource); |
| 54 | } |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 55 | if (const GrResourceKey* contentKey = resource->getContentKey()) { |
| 56 | fContentHash.remove(*contentKey); |
| 57 | } |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 58 | --fCount; |
| 59 | } |
| 60 | |
| 61 | void GrResourceCache2::abandonAll() { |
| 62 | while (GrGpuResource* head = fResources.head()) { |
| 63 | SkASSERT(!head->wasDestroyed()); |
| 64 | head->abandon(); |
| 65 | // abandon should have already removed this from the list. |
| 66 | SkASSERT(head != fResources.head()); |
| 67 | } |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 68 | SkASSERT(!fScratchMap.count()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 69 | SkASSERT(!fContentHash.count()); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 70 | SkASSERT(!fCount); |
| 71 | } |
| 72 | |
| 73 | void GrResourceCache2::releaseAll() { |
| 74 | while (GrGpuResource* head = fResources.head()) { |
| 75 | SkASSERT(!head->wasDestroyed()); |
| 76 | head->release(); |
| 77 | // release should have already removed this from the list. |
| 78 | SkASSERT(head != fResources.head()); |
| 79 | } |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 80 | SkASSERT(!fScratchMap.count()); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 81 | SkASSERT(!fCount); |
| 82 | } |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 83 | |
| 84 | class GrResourceCache2::AvailableForScratchUse { |
| 85 | public: |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 86 | AvailableForScratchUse(bool rejectPendingIO) : fRejectPendingIO(rejectPendingIO) { } |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 87 | |
| 88 | bool operator()(const GrGpuResource* resource) const { |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 89 | if (!resource->reffedOnlyByCache() || !resource->isScratch()) { |
| 90 | return false; |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 91 | } |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 92 | |
| 93 | return !fRejectPendingIO || !resource->internalHasPendingIO(); |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 94 | } |
bsalomon | 1e2530b | 2014-10-09 09:57:18 -0700 | [diff] [blame] | 95 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 96 | private: |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 97 | bool fRejectPendingIO; |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | GrGpuResource* GrResourceCache2::findAndRefScratchResource(const GrResourceKey& scratchKey, |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 101 | uint32_t flags) { |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 102 | SkASSERT(scratchKey.isScratch()); |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 103 | |
| 104 | if (flags & (kPreferNoPendingIO_ScratchFlag | kRequireNoPendingIO_ScratchFlag)) { |
| 105 | GrGpuResource* resource = fScratchMap.find(scratchKey, AvailableForScratchUse(true)); |
| 106 | if (resource) { |
| 107 | return SkRef(resource); |
| 108 | } else if (flags & kRequireNoPendingIO_ScratchFlag) { |
| 109 | return NULL; |
| 110 | } |
| 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. |
| 113 | } |
| 114 | return SkSafeRef(fScratchMap.find(scratchKey, AvailableForScratchUse(false))); |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 115 | } |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 116 | |
| 117 | void GrResourceCache2::willRemoveContentKey(const GrGpuResource* resource) { |
| 118 | SkASSERT(resource); |
| 119 | SkASSERT(resource->getContentKey()); |
| 120 | SkDEBUGCODE(GrGpuResource* res = fContentHash.find(*resource->getContentKey())); |
| 121 | SkASSERT(res == resource); |
| 122 | |
| 123 | fContentHash.remove(*resource->getContentKey()); |
| 124 | } |
| 125 | |
| 126 | bool GrResourceCache2::didAddContentKey(GrGpuResource* resource) { |
| 127 | SkASSERT(resource); |
| 128 | SkASSERT(resource->getContentKey()); |
| 129 | |
| 130 | GrGpuResource* res = fContentHash.find(*resource->getContentKey()); |
| 131 | if (NULL != res) { |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | fContentHash.add(resource); |
| 136 | return true; |
| 137 | } |