3 * Copyright 2014 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
10 #include "GrResourceCache2.h"
11 #include "GrGpuResource.h"
14 GrResourceCache2::~GrResourceCache2() {
18 void GrResourceCache2::insertResource(GrGpuResource* resource) {
20 SkASSERT(!resource->wasDestroyed());
21 SkASSERT(!this->isInCache(resource));
22 fResources.addToHead(resource);
24 if (!resource->getScratchKey().isNullScratch()) {
25 fScratchMap.insert(resource->getScratchKey(), resource);
29 void GrResourceCache2::removeResource(GrGpuResource* resource) {
30 SkASSERT(this->isInCache(resource));
31 fResources.remove(resource);
32 if (!resource->getScratchKey().isNullScratch()) {
33 fScratchMap.remove(resource->getScratchKey(), resource);
38 void GrResourceCache2::abandonAll() {
39 while (GrGpuResource* head = fResources.head()) {
40 SkASSERT(!head->wasDestroyed());
42 // abandon should have already removed this from the list.
43 SkASSERT(head != fResources.head());
45 SkASSERT(!fScratchMap.count());
49 void GrResourceCache2::releaseAll() {
50 while (GrGpuResource* head = fResources.head()) {
51 SkASSERT(!head->wasDestroyed());
53 // release should have already removed this from the list.
54 SkASSERT(head != fResources.head());
56 SkASSERT(!fScratchMap.count());
60 class GrResourceCache2::AvailableForScratchUse {
62 AvailableForScratchUse(bool rejectPendingIO) : fRejectPendingIO(rejectPendingIO) { }
64 bool operator()(const GrGpuResource* resource) const {
65 if (!resource->reffedOnlyByCache() || !resource->isScratch()) {
69 return !fRejectPendingIO || !resource->internalHasPendingIO();
73 bool fRejectPendingIO;
76 GrGpuResource* GrResourceCache2::findAndRefScratchResource(const GrResourceKey& scratchKey,
78 SkASSERT(scratchKey.isScratch());
80 if (flags & (kPreferNoPendingIO_ScratchFlag | kRequireNoPendingIO_ScratchFlag)) {
81 GrGpuResource* resource = fScratchMap.find(scratchKey, AvailableForScratchUse(true));
83 return SkRef(resource);
84 } else if (flags & kRequireNoPendingIO_ScratchFlag) {
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.
90 return SkSafeRef(fScratchMap.find(scratchKey, AvailableForScratchUse(false)));