aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/gpu/GrResourceCache2.cpp
blob: 76673cf9f95453cbed8a6fd3f81093d556ca2bff (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

/*
 * 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"  
#include "SkRefCnt.h"

GrResourceCache2::~GrResourceCache2() {
    this->releaseAll();
}

void GrResourceCache2::insertResource(GrGpuResource* resource) {
    SkASSERT(resource);
    SkASSERT(!resource->wasDestroyed());
    SkASSERT(!this->isInCache(resource));
    fResources.addToHead(resource);
    ++fCount;
    if (!resource->getScratchKey().isNullScratch()) {
        fScratchMap.insert(resource->getScratchKey(), resource);
    }
}

void GrResourceCache2::removeResource(GrGpuResource* resource) {
    SkASSERT(this->isInCache(resource));
    fResources.remove(resource);    
    if (!resource->getScratchKey().isNullScratch()) {
        fScratchMap.remove(resource->getScratchKey(), resource);
    }
    --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(!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 calledDuringFlush) : fFlushing(calledDuringFlush) { }

    bool operator()(const GrGpuResource* resource) const {
        if (fFlushing) {
            // If this request is coming during draw buffer flush then no refs are allowed
            // either by drawing code or for pending io operations.
            // This will be removed when flush no longer creates resources.
            return resource->reffedOnlyByCache() && !resource->internalHasPendingIO() &&
                   (NULL == resource->getContentKey());
        } else {
            // Because duties are currently shared between GrResourceCache and GrResourceCache2, the
            // current interpretation of this rule is that only GrResourceCache has a ref but that
            // it has been marked as a scratch resource.
            return resource->reffedOnlyByCache() && (NULL == resource->getContentKey());
        }
    }

private:
    bool fFlushing;
};

GrGpuResource* GrResourceCache2::findAndRefScratchResource(const GrResourceKey& scratchKey,
                                                           bool calledDuringFlush) {
    SkASSERT(scratchKey.isScratch());
    return SkSafeRef(fScratchMap.find(scratchKey, AvailableForScratchUse(calledDuringFlush)));
}