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" |
| 11 | #include "GrGpuResource.h" |
| 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()) { |
| 24 | fScratchMap.insert(resource->getScratchKey(), resource); |
| 25 | } |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | void GrResourceCache2::removeResource(GrGpuResource* resource) { |
bsalomon | 1696126 | 2014-08-26 14:01:07 -0700 | [diff] [blame] | 29 | SkASSERT(this->isInCache(resource)); |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 30 | fResources.remove(resource); |
| 31 | if (!resource->getScratchKey().isNullScratch()) { |
| 32 | fScratchMap.remove(resource->getScratchKey(), resource); |
| 33 | } |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 34 | --fCount; |
| 35 | } |
| 36 | |
| 37 | void GrResourceCache2::abandonAll() { |
| 38 | while (GrGpuResource* head = fResources.head()) { |
| 39 | SkASSERT(!head->wasDestroyed()); |
| 40 | head->abandon(); |
| 41 | // abandon should have already removed this from the list. |
| 42 | SkASSERT(head != fResources.head()); |
| 43 | } |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 44 | SkASSERT(!fScratchMap.count()); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 45 | SkASSERT(!fCount); |
| 46 | } |
| 47 | |
| 48 | void GrResourceCache2::releaseAll() { |
| 49 | while (GrGpuResource* head = fResources.head()) { |
| 50 | SkASSERT(!head->wasDestroyed()); |
| 51 | head->release(); |
| 52 | // release should have already removed this from the list. |
| 53 | SkASSERT(head != fResources.head()); |
| 54 | } |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 55 | SkASSERT(!fScratchMap.count()); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 56 | SkASSERT(!fCount); |
| 57 | } |