blob: 5144c59eef44f38ca5da81bb90caba0d7817caa1 [file] [log] [blame]
bsalomonc8dc1f72014-08-21 13:02:13 -07001
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"
bsalomonbcf0a522014-10-08 08:40:09 -070011#include "GrGpuResource.h"
12#include "SkRefCnt.h"
bsalomonc8dc1f72014-08-21 13:02:13 -070013
14GrResourceCache2::~GrResourceCache2() {
15 this->releaseAll();
16}
17
18void GrResourceCache2::insertResource(GrGpuResource* resource) {
bsalomon49f085d2014-09-05 13:34:00 -070019 SkASSERT(resource);
bsalomonc8dc1f72014-08-21 13:02:13 -070020 SkASSERT(!resource->wasDestroyed());
bsalomon16961262014-08-26 14:01:07 -070021 SkASSERT(!this->isInCache(resource));
bsalomonc8dc1f72014-08-21 13:02:13 -070022 fResources.addToHead(resource);
23 ++fCount;
bsalomon744998e2014-08-28 09:54:34 -070024 if (!resource->getScratchKey().isNullScratch()) {
25 fScratchMap.insert(resource->getScratchKey(), resource);
26 }
bsalomonc8dc1f72014-08-21 13:02:13 -070027}
28
29void GrResourceCache2::removeResource(GrGpuResource* resource) {
bsalomon16961262014-08-26 14:01:07 -070030 SkASSERT(this->isInCache(resource));
bsalomon744998e2014-08-28 09:54:34 -070031 fResources.remove(resource);
32 if (!resource->getScratchKey().isNullScratch()) {
33 fScratchMap.remove(resource->getScratchKey(), resource);
34 }
bsalomonc8dc1f72014-08-21 13:02:13 -070035 --fCount;
36}
37
38void 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 }
bsalomon744998e2014-08-28 09:54:34 -070045 SkASSERT(!fScratchMap.count());
bsalomonc8dc1f72014-08-21 13:02:13 -070046 SkASSERT(!fCount);
47}
48
49void 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 }
bsalomon744998e2014-08-28 09:54:34 -070056 SkASSERT(!fScratchMap.count());
bsalomonc8dc1f72014-08-21 13:02:13 -070057 SkASSERT(!fCount);
58}
bsalomonbcf0a522014-10-08 08:40:09 -070059
60class GrResourceCache2::AvailableForScratchUse {
61public:
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() &&
bsalomon1e2530b2014-10-09 09:57:18 -070070 resource->isScratch();
bsalomonbcf0a522014-10-08 08:40:09 -070071 } 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.
bsalomon1e2530b2014-10-09 09:57:18 -070075 return resource->reffedOnlyByCache() && resource->isScratch();
bsalomonbcf0a522014-10-08 08:40:09 -070076 }
77 }
bsalomon1e2530b2014-10-09 09:57:18 -070078
bsalomonbcf0a522014-10-08 08:40:09 -070079private:
80 bool fFlushing;
81};
82
83GrGpuResource* GrResourceCache2::findAndRefScratchResource(const GrResourceKey& scratchKey,
84 bool calledDuringFlush) {
85 SkASSERT(scratchKey.isScratch());
86 return SkSafeRef(fScratchMap.find(scratchKey, AvailableForScratchUse(calledDuringFlush)));
87}