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: |
| 62 | AvailableForScratchUse(bool calledDuringFlush) : fFlushing(calledDuringFlush) { } |
| 63 | |
| 64 | bool operator()(const GrGpuResource* resource) const { |
| 65 | if (fFlushing) { |
| 66 | // If this request is coming during draw buffer flush then no refs are allowed |
| 67 | // either by drawing code or for pending io operations. |
| 68 | // This will be removed when flush no longer creates resources. |
| 69 | return resource->reffedOnlyByCache() && !resource->internalHasPendingIO() && |
| 70 | GrGpuResource::kYes_IsScratch == resource->fIsScratch; |
| 71 | } else { |
| 72 | // Because duties are currently shared between GrResourceCache and GrResourceCache2, the |
| 73 | // current interpretation of this rule is that only GrResourceCache has a ref but that |
| 74 | // it has been marked as a scratch resource. |
| 75 | return resource->reffedOnlyByCache() && |
| 76 | GrGpuResource::kYes_IsScratch == resource->fIsScratch; |
| 77 | } |
| 78 | } |
| 79 | private: |
| 80 | bool fFlushing; |
| 81 | }; |
| 82 | |
| 83 | GrGpuResource* GrResourceCache2::findAndRefScratchResource(const GrResourceKey& scratchKey, |
| 84 | bool calledDuringFlush) { |
| 85 | SkASSERT(scratchKey.isScratch()); |
| 86 | return SkSafeRef(fScratchMap.find(scratchKey, AvailableForScratchUse(calledDuringFlush))); |
| 87 | } |