blob: 6bc23a3096a5101027f4dfa2aae1c1a4ea6c8a1c [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:
bsalomon000f8292014-10-15 19:04:14 -070062 AvailableForScratchUse(bool rejectPendingIO) : fRejectPendingIO(rejectPendingIO) { }
bsalomonbcf0a522014-10-08 08:40:09 -070063
64 bool operator()(const GrGpuResource* resource) const {
bsalomon000f8292014-10-15 19:04:14 -070065 if (!resource->reffedOnlyByCache() || !resource->isScratch()) {
66 return false;
bsalomonbcf0a522014-10-08 08:40:09 -070067 }
bsalomon000f8292014-10-15 19:04:14 -070068
69 return !fRejectPendingIO || !resource->internalHasPendingIO();
bsalomonbcf0a522014-10-08 08:40:09 -070070 }
bsalomon1e2530b2014-10-09 09:57:18 -070071
bsalomonbcf0a522014-10-08 08:40:09 -070072private:
bsalomon000f8292014-10-15 19:04:14 -070073 bool fRejectPendingIO;
bsalomonbcf0a522014-10-08 08:40:09 -070074};
75
76GrGpuResource* GrResourceCache2::findAndRefScratchResource(const GrResourceKey& scratchKey,
bsalomon000f8292014-10-15 19:04:14 -070077 uint32_t flags) {
bsalomonbcf0a522014-10-08 08:40:09 -070078 SkASSERT(scratchKey.isScratch());
bsalomon000f8292014-10-15 19:04:14 -070079
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)));
bsalomonbcf0a522014-10-08 08:40:09 -070091}