aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/gpu/GrGpuResourceRef.cpp
blob: 74b1ef0eba6f45016a43686e187c8e8a0a63d073 (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
130
131
132
133
/*
 * 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 "GrGpuResourceRef.h"
#include "GrGpuResource.h"

GrGpuResourceRef::GrGpuResourceRef() {
    fResource = NULL;
    fOwnRef = false;
    fPendingIO = false;
    fIOType = kNone_IOType;
}

GrGpuResourceRef::GrGpuResourceRef(GrGpuResource* resource, IOType ioType) {
    fResource = NULL;
    fOwnRef = false;
    fPendingIO = false;
    this->setResource(resource, ioType);
}

GrGpuResourceRef::~GrGpuResourceRef() {
    if (fOwnRef) {
        SkASSERT(fResource);
        fResource->unref();
    }
    if (fPendingIO) {
        switch (fIOType) {
            case kNone_IOType:
                SkFAIL("Shouldn't get here if fIOType is kNone.");
                break;
            case kRead_IOType:
                fResource->completedRead();
                break;
            case kWrite_IOType:
                fResource->completedWrite();
                break;
            case kRW_IOType:
                fResource->completedRead();
                fResource->completedWrite();
                break;
        }
    }
}

void GrGpuResourceRef::reset() {
    SkASSERT(!fPendingIO);
    SkASSERT(SkToBool(fResource) == fOwnRef);
    if (fOwnRef) {
        fResource->unref();
        fOwnRef = false;
        fResource = NULL;
        fIOType = kNone_IOType;
    }
}

void GrGpuResourceRef::setResource(GrGpuResource* resource, IOType ioType) {
    SkASSERT(!fPendingIO);
    SkASSERT(SkToBool(fResource) == fOwnRef);
    SkSafeUnref(fResource);
    if (NULL == resource) {
        fResource = NULL;
        fOwnRef = false;
        fIOType = kNone_IOType;
    } else {
        SkASSERT(kNone_IOType != ioType);
        fResource = resource;
        fOwnRef = true;
        fIOType = ioType;
    }
}

void GrGpuResourceRef::markPendingIO() const {
    // This should only be called when the owning GrProgramElement gets its first
    // pendingExecution ref.
    SkASSERT(!fPendingIO);
    SkASSERT(fResource);
    fPendingIO = true;
    switch (fIOType) {
        case kNone_IOType:
            SkFAIL("GrGpuResourceRef with neither reads nor writes?");
            break;
        case kRead_IOType:
            fResource->addPendingRead();
            break;
        case kWrite_IOType:
            fResource->addPendingWrite();
            break;
        case kRW_IOType:
            fResource->addPendingRead();
            fResource->addPendingWrite();
            break;

    }
}

void GrGpuResourceRef::pendingIOComplete() const {
    // This should only be called when the owner's pending executions have ocurred but it is still
    // reffed.
    SkASSERT(fOwnRef);
    SkASSERT(fPendingIO);
    switch (fIOType) {
        case kNone_IOType:
            SkFAIL("GrGpuResourceRef with neither reads nor writes?");
            break;
        case kRead_IOType:
            fResource->completedRead();
            break;
        case kWrite_IOType:
            fResource->completedWrite();
            break;
        case kRW_IOType:
            fResource->completedRead();
            fResource->completedWrite();
            break;

    }
    fPendingIO = false;
}

void GrGpuResourceRef::removeRef() const {
    // This should only be called once, when the owners last ref goes away and
    // there is a pending execution.
    SkASSERT(fOwnRef);
    SkASSERT(fPendingIO);
    SkASSERT(kNone_IOType != fIOType);
    SkASSERT(fResource);
    fResource->unref();
    fOwnRef = false;
}