blob: 83143d78185898c8ed0ed83420228019de1f9d42 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
/*
* Copyright 2014 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "GrResourceCache2.h"
#include "GrGpuResource.h"
GrResourceKey& GrResourceKey::NullScratchKey() {
static const GrCacheID::Key kBogusKey = { { {0} } };
static GrCacheID kBogusID(ScratchDomain(), kBogusKey);
static GrResourceKey kNullScratchKey(kBogusID, NoneResourceType(), 0);
return kNullScratchKey;
}
GrResourceKey::ResourceType GrResourceKey::NoneResourceType() {
static const ResourceType gNoneResourceType = GenerateResourceType();
return gNoneResourceType;
}
GrCacheID::Domain GrResourceKey::ScratchDomain() {
static const GrCacheID::Domain gDomain = GrCacheID::GenerateDomain();
return gDomain;
}
//////////////////////////////////////////////////////////////////////////////
GrResourceCache2::~GrResourceCache2() {
this->releaseAll();
}
void GrResourceCache2::insertResource(GrGpuResource* resource) {
SkASSERT(resource);
SkASSERT(!resource->wasDestroyed());
SkASSERT(!this->isInCache(resource));
fResources.addToHead(resource);
++fCount;
if (!resource->cacheAccess().getScratchKey().isNullScratch()) {
// TODO(bsalomon): Make this assertion possible.
// SkASSERT(!resource->isWrapped());
fScratchMap.insert(resource->cacheAccess().getScratchKey(), resource);
}
}
void GrResourceCache2::removeResource(GrGpuResource* resource) {
SkASSERT(this->isInCache(resource));
fResources.remove(resource);
if (!resource->cacheAccess().getScratchKey().isNullScratch()) {
fScratchMap.remove(resource->cacheAccess().getScratchKey(), resource);
}
if (const GrResourceKey* contentKey = resource->cacheAccess().getContentKey()) {
fContentHash.remove(*contentKey);
}
--fCount;
}
void GrResourceCache2::abandonAll() {
while (GrGpuResource* head = fResources.head()) {
SkASSERT(!head->wasDestroyed());
head->abandon();
// abandon should have already removed this from the list.
SkASSERT(head != fResources.head());
}
SkASSERT(!fScratchMap.count());
SkASSERT(!fContentHash.count());
SkASSERT(!fCount);
}
void GrResourceCache2::releaseAll() {
while (GrGpuResource* head = fResources.head()) {
SkASSERT(!head->wasDestroyed());
head->release();
// release should have already removed this from the list.
SkASSERT(head != fResources.head());
}
SkASSERT(!fScratchMap.count());
SkASSERT(!fCount);
}
class GrResourceCache2::AvailableForScratchUse {
public:
AvailableForScratchUse(bool rejectPendingIO) : fRejectPendingIO(rejectPendingIO) { }
bool operator()(const GrGpuResource* resource) const {
if (!resource->reffedOnlyByCache() || !resource->cacheAccess().isScratch()) {
return false;
}
return !fRejectPendingIO || !resource->internalHasPendingIO();
}
private:
bool fRejectPendingIO;
};
GrGpuResource* GrResourceCache2::findAndRefScratchResource(const GrResourceKey& scratchKey,
uint32_t flags) {
SkASSERT(scratchKey.isScratch());
if (flags & (kPreferNoPendingIO_ScratchFlag | kRequireNoPendingIO_ScratchFlag)) {
GrGpuResource* resource = fScratchMap.find(scratchKey, AvailableForScratchUse(true));
if (resource) {
return SkRef(resource);
} else if (flags & kRequireNoPendingIO_ScratchFlag) {
return NULL;
}
// TODO: fail here when kPrefer is specified, we didn't find a resource without pending io,
// but there is still space in our budget for the resource.
}
return SkSafeRef(fScratchMap.find(scratchKey, AvailableForScratchUse(false)));
}
bool GrResourceCache2::didSetContentKey(GrGpuResource* resource) {
SkASSERT(resource);
SkASSERT(resource->cacheAccess().getContentKey());
SkASSERT(!resource->cacheAccess().getContentKey()->isScratch());
GrGpuResource* res = fContentHash.find(*resource->cacheAccess().getContentKey());
if (NULL != res) {
return false;
}
fContentHash.add(resource);
return true;
}
|