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" |
| 12 | #include "SkRefCnt.h" |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 13 | |
| 14 | GrResourceCache2::~GrResourceCache2() { |
| 15 | this->releaseAll(); |
| 16 | } |
| 17 | |
| 18 | void GrResourceCache2::insertResource(GrGpuResource* resource) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 19 | SkASSERT(resource); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 20 | SkASSERT(!resource->wasDestroyed()); |
bsalomon | 1696126 | 2014-08-26 14:01:07 -0700 | [diff] [blame] | 21 | SkASSERT(!this->isInCache(resource)); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 22 | fResources.addToHead(resource); |
| 23 | ++fCount; |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 24 | if (!resource->getScratchKey().isNullScratch()) { |
| 25 | fScratchMap.insert(resource->getScratchKey(), resource); |
| 26 | } |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | void GrResourceCache2::removeResource(GrGpuResource* resource) { |
bsalomon | 1696126 | 2014-08-26 14:01:07 -0700 | [diff] [blame] | 30 | SkASSERT(this->isInCache(resource)); |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 31 | fResources.remove(resource); |
| 32 | if (!resource->getScratchKey().isNullScratch()) { |
| 33 | fScratchMap.remove(resource->getScratchKey(), resource); |
| 34 | } |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 35 | --fCount; |
| 36 | } |
| 37 | |
| 38 | void GrResourceCache2::abandonAll() { |
| 39 | while (GrGpuResource* head = fResources.head()) { |
| 40 | SkASSERT(!head->wasDestroyed()); |
| 41 | head->abandon(); |
| 42 | // abandon should have already removed this from the list. |
| 43 | SkASSERT(head != fResources.head()); |
| 44 | } |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 45 | SkASSERT(!fScratchMap.count()); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 46 | SkASSERT(!fCount); |
| 47 | } |
| 48 | |
| 49 | void GrResourceCache2::releaseAll() { |
| 50 | while (GrGpuResource* head = fResources.head()) { |
| 51 | SkASSERT(!head->wasDestroyed()); |
| 52 | head->release(); |
| 53 | // release should have already removed this from the list. |
| 54 | SkASSERT(head != fResources.head()); |
| 55 | } |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 56 | SkASSERT(!fScratchMap.count()); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 57 | SkASSERT(!fCount); |
| 58 | } |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 59 | |
| 60 | class GrResourceCache2::AvailableForScratchUse { |
| 61 | public: |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame^] | 62 | AvailableForScratchUse(bool rejectPendingIO) : fRejectPendingIO(rejectPendingIO) { } |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 63 | |
| 64 | bool operator()(const GrGpuResource* resource) const { |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame^] | 65 | if (!resource->reffedOnlyByCache() || !resource->isScratch()) { |
| 66 | return false; |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 67 | } |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame^] | 68 | |
| 69 | return !fRejectPendingIO || !resource->internalHasPendingIO(); |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 70 | } |
bsalomon | 1e2530b | 2014-10-09 09:57:18 -0700 | [diff] [blame] | 71 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 72 | private: |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame^] | 73 | bool fRejectPendingIO; |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | GrGpuResource* GrResourceCache2::findAndRefScratchResource(const GrResourceKey& scratchKey, |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame^] | 77 | uint32_t flags) { |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 78 | SkASSERT(scratchKey.isScratch()); |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame^] | 79 | |
| 80 | if (flags & (kPreferNoPendingIO_ScratchFlag | kRequireNoPendingIO_ScratchFlag)) { |
| 81 | GrGpuResource* resource = fScratchMap.find(scratchKey, AvailableForScratchUse(true)); |
| 82 | if (resource) { |
| 83 | return SkRef(resource); |
| 84 | } else if (flags & kRequireNoPendingIO_ScratchFlag) { |
| 85 | return NULL; |
| 86 | } |
| 87 | // 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. |
| 89 | } |
| 90 | return SkSafeRef(fScratchMap.find(scratchKey, AvailableForScratchUse(false))); |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 91 | } |