bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 1 | /* |
2 | * Copyright 2014 Google Inc. | ||||
3 | * | ||||
4 | * Use of this source code is governed by a BSD-style license that can be | ||||
5 | * found in the LICENSE file. | ||||
6 | */ | ||||
7 | |||||
bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame] | 8 | #include "GrGpuResourceRef.h" |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 9 | |
bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame] | 10 | GrGpuResourceRef::GrGpuResourceRef() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 11 | fResource = nullptr; |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 12 | fOwnRef = false; |
13 | fPendingIO = false; | ||||
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 14 | } |
15 | |||||
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 16 | GrGpuResourceRef::GrGpuResourceRef(GrGpuResource* resource, GrIOType ioType) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 17 | fResource = nullptr; |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 18 | fOwnRef = false; |
19 | fPendingIO = false; | ||||
20 | this->setResource(resource, ioType); | ||||
21 | } | ||||
22 | |||||
bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame] | 23 | GrGpuResourceRef::~GrGpuResourceRef() { |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 24 | if (fOwnRef) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 25 | SkASSERT(fResource); |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 26 | fResource->unref(); |
27 | } | ||||
28 | if (fPendingIO) { | ||||
29 | switch (fIOType) { | ||||
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 30 | case kRead_GrIOType: |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 31 | fResource->completedRead(); |
32 | break; | ||||
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 33 | case kWrite_GrIOType: |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 34 | fResource->completedWrite(); |
35 | break; | ||||
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 36 | case kRW_GrIOType: |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 37 | fResource->completedRead(); |
38 | fResource->completedWrite(); | ||||
39 | break; | ||||
40 | } | ||||
41 | } | ||||
42 | } | ||||
43 | |||||
bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame] | 44 | void GrGpuResourceRef::reset() { |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 45 | SkASSERT(!fPendingIO); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 46 | SkASSERT(SkToBool(fResource) == fOwnRef); |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 47 | if (fOwnRef) { |
48 | fResource->unref(); | ||||
49 | fOwnRef = false; | ||||
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 50 | fResource = nullptr; |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 51 | } |
52 | } | ||||
53 | |||||
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 54 | void GrGpuResourceRef::setResource(GrGpuResource* resource, GrIOType ioType) { |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 55 | SkASSERT(!fPendingIO); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 56 | SkASSERT(SkToBool(fResource) == fOwnRef); |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 57 | SkSafeUnref(fResource); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 58 | if (nullptr == resource) { |
59 | fResource = nullptr; | ||||
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 60 | fOwnRef = false; |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 61 | } else { |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 62 | fResource = resource; |
63 | fOwnRef = true; | ||||
64 | fIOType = ioType; | ||||
65 | } | ||||
66 | } | ||||
67 | |||||
bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame] | 68 | void GrGpuResourceRef::markPendingIO() const { |
bsalomon | ac8d619 | 2014-09-04 14:13:44 -0700 | [diff] [blame] | 69 | // This should only be called when the owning GrProgramElement gets its first |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 70 | // pendingExecution ref. |
71 | SkASSERT(!fPendingIO); | ||||
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 72 | SkASSERT(fResource); |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 73 | fPendingIO = true; |
74 | switch (fIOType) { | ||||
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 75 | case kRead_GrIOType: |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 76 | fResource->addPendingRead(); |
77 | break; | ||||
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 78 | case kWrite_GrIOType: |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 79 | fResource->addPendingWrite(); |
80 | break; | ||||
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 81 | case kRW_GrIOType: |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 82 | fResource->addPendingRead(); |
83 | fResource->addPendingWrite(); | ||||
84 | break; | ||||
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 85 | } |
86 | } | ||||
87 | |||||
bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame] | 88 | void GrGpuResourceRef::pendingIOComplete() const { |
bsalomon | ac8d619 | 2014-09-04 14:13:44 -0700 | [diff] [blame] | 89 | // This should only be called when the owner's pending executions have ocurred but it is still |
90 | // reffed. | ||||
91 | SkASSERT(fOwnRef); | ||||
92 | SkASSERT(fPendingIO); | ||||
93 | switch (fIOType) { | ||||
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 94 | case kRead_GrIOType: |
bsalomon | ac8d619 | 2014-09-04 14:13:44 -0700 | [diff] [blame] | 95 | fResource->completedRead(); |
96 | break; | ||||
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 97 | case kWrite_GrIOType: |
bsalomon | ac8d619 | 2014-09-04 14:13:44 -0700 | [diff] [blame] | 98 | fResource->completedWrite(); |
99 | break; | ||||
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 100 | case kRW_GrIOType: |
bsalomon | ac8d619 | 2014-09-04 14:13:44 -0700 | [diff] [blame] | 101 | fResource->completedRead(); |
102 | fResource->completedWrite(); | ||||
103 | break; | ||||
104 | |||||
105 | } | ||||
106 | fPendingIO = false; | ||||
107 | } | ||||
108 | |||||
bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame] | 109 | void GrGpuResourceRef::removeRef() const { |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 110 | // This should only be called once, when the owners last ref goes away and |
111 | // there is a pending execution. | ||||
112 | SkASSERT(fOwnRef); | ||||
113 | SkASSERT(fPendingIO); | ||||
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 114 | SkASSERT(fResource); |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 115 | fResource->unref(); |
116 | fOwnRef = false; | ||||
117 | } |