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 { |
Robert Phillips | a91e0b7 | 2017-05-01 13:12:20 -0400 | [diff] [blame] | 69 | if (!fResource) { |
| 70 | return; |
| 71 | } |
| 72 | |
bsalomon | ac8d619 | 2014-09-04 14:13:44 -0700 | [diff] [blame] | 73 | // This should only be called when the owning GrProgramElement gets its first |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 74 | // pendingExecution ref. |
| 75 | SkASSERT(!fPendingIO); |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 76 | fPendingIO = true; |
| 77 | switch (fIOType) { |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 78 | case kRead_GrIOType: |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 79 | fResource->addPendingRead(); |
| 80 | break; |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 81 | case kWrite_GrIOType: |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 82 | fResource->addPendingWrite(); |
| 83 | break; |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 84 | case kRW_GrIOType: |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 85 | fResource->addPendingRead(); |
| 86 | fResource->addPendingWrite(); |
| 87 | break; |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame] | 91 | void GrGpuResourceRef::pendingIOComplete() const { |
Robert Phillips | a91e0b7 | 2017-05-01 13:12:20 -0400 | [diff] [blame] | 92 | if (!fResource) { |
| 93 | return; |
| 94 | } |
| 95 | |
bsalomon | ac8d619 | 2014-09-04 14:13:44 -0700 | [diff] [blame] | 96 | // This should only be called when the owner's pending executions have ocurred but it is still |
| 97 | // reffed. |
| 98 | SkASSERT(fOwnRef); |
| 99 | SkASSERT(fPendingIO); |
| 100 | switch (fIOType) { |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 101 | case kRead_GrIOType: |
bsalomon | ac8d619 | 2014-09-04 14:13:44 -0700 | [diff] [blame] | 102 | fResource->completedRead(); |
| 103 | break; |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 104 | case kWrite_GrIOType: |
bsalomon | ac8d619 | 2014-09-04 14:13:44 -0700 | [diff] [blame] | 105 | fResource->completedWrite(); |
| 106 | break; |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 107 | case kRW_GrIOType: |
bsalomon | ac8d619 | 2014-09-04 14:13:44 -0700 | [diff] [blame] | 108 | fResource->completedRead(); |
| 109 | fResource->completedWrite(); |
| 110 | break; |
| 111 | |
| 112 | } |
| 113 | fPendingIO = false; |
| 114 | } |
| 115 | |
bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame] | 116 | void GrGpuResourceRef::removeRef() const { |
Robert Phillips | a91e0b7 | 2017-05-01 13:12:20 -0400 | [diff] [blame] | 117 | if (!fResource) { |
| 118 | return; |
| 119 | } |
| 120 | |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 121 | // This should only be called once, when the owners last ref goes away and |
| 122 | // there is a pending execution. |
| 123 | SkASSERT(fOwnRef); |
| 124 | SkASSERT(fPendingIO); |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 125 | fResource->unref(); |
| 126 | fOwnRef = false; |
| 127 | } |
Robert Phillips | 952a243 | 2017-05-04 08:52:22 -0400 | [diff] [blame] | 128 | |
| 129 | /////////////////////////////////////////////////////////////////////////////// |
| 130 | #include "GrTextureProxy.h" |
| 131 | |
Robert Phillips | b6deea8 | 2017-05-11 14:14:30 -0400 | [diff] [blame] | 132 | GrSurfaceProxyRef::GrSurfaceProxyRef() { |
Robert Phillips | 952a243 | 2017-05-04 08:52:22 -0400 | [diff] [blame] | 133 | fProxy = nullptr; |
| 134 | fOwnRef = false; |
| 135 | fPendingIO = false; |
| 136 | } |
| 137 | |
Robert Phillips | b6deea8 | 2017-05-11 14:14:30 -0400 | [diff] [blame] | 138 | GrSurfaceProxyRef::GrSurfaceProxyRef(sk_sp<GrSurfaceProxy> proxy, GrIOType ioType) { |
Robert Phillips | 952a243 | 2017-05-04 08:52:22 -0400 | [diff] [blame] | 139 | fProxy = nullptr; |
| 140 | fOwnRef = false; |
| 141 | fPendingIO = false; |
Robert Phillips | b6deea8 | 2017-05-11 14:14:30 -0400 | [diff] [blame] | 142 | this->setProxy(std::move(proxy), ioType); |
Robert Phillips | 952a243 | 2017-05-04 08:52:22 -0400 | [diff] [blame] | 143 | } |
| 144 | |
Robert Phillips | b6deea8 | 2017-05-11 14:14:30 -0400 | [diff] [blame] | 145 | GrSurfaceProxyRef::~GrSurfaceProxyRef() { |
Robert Phillips | d991486 | 2017-06-14 15:16:59 -0400 | [diff] [blame] | 146 | this->reset(); |
| 147 | } |
| 148 | |
| 149 | void GrSurfaceProxyRef::reset() { |
Robert Phillips | c8ecb03 | 2017-06-14 17:55:12 +0000 | [diff] [blame] | 150 | if (fPendingIO) { |
Robert Phillips | d991486 | 2017-06-14 15:16:59 -0400 | [diff] [blame] | 151 | SkASSERT(fProxy); |
Robert Phillips | 952a243 | 2017-05-04 08:52:22 -0400 | [diff] [blame] | 152 | switch (fIOType) { |
| 153 | case kRead_GrIOType: |
| 154 | fProxy->completedRead(); |
| 155 | break; |
| 156 | case kWrite_GrIOType: |
| 157 | fProxy->completedWrite(); |
| 158 | break; |
| 159 | case kRW_GrIOType: |
| 160 | fProxy->completedRead(); |
| 161 | fProxy->completedWrite(); |
| 162 | break; |
| 163 | } |
Robert Phillips | d991486 | 2017-06-14 15:16:59 -0400 | [diff] [blame] | 164 | fPendingIO = false; |
Robert Phillips | 952a243 | 2017-05-04 08:52:22 -0400 | [diff] [blame] | 165 | } |
Robert Phillips | 952a243 | 2017-05-04 08:52:22 -0400 | [diff] [blame] | 166 | if (fOwnRef) { |
Robert Phillips | d991486 | 2017-06-14 15:16:59 -0400 | [diff] [blame] | 167 | SkASSERT(fProxy); |
Robert Phillips | 952a243 | 2017-05-04 08:52:22 -0400 | [diff] [blame] | 168 | fProxy->unref(); |
| 169 | fOwnRef = false; |
Robert Phillips | 952a243 | 2017-05-04 08:52:22 -0400 | [diff] [blame] | 170 | } |
Robert Phillips | d991486 | 2017-06-14 15:16:59 -0400 | [diff] [blame] | 171 | |
| 172 | fProxy = nullptr; |
Robert Phillips | 952a243 | 2017-05-04 08:52:22 -0400 | [diff] [blame] | 173 | } |
| 174 | |
Robert Phillips | b6deea8 | 2017-05-11 14:14:30 -0400 | [diff] [blame] | 175 | void GrSurfaceProxyRef::setProxy(sk_sp<GrSurfaceProxy> proxy, GrIOType ioType) { |
Robert Phillips | 952a243 | 2017-05-04 08:52:22 -0400 | [diff] [blame] | 176 | SkASSERT(!fPendingIO); |
| 177 | SkASSERT(SkToBool(fProxy) == fOwnRef); |
| 178 | SkSafeUnref(fProxy); |
| 179 | if (!proxy) { |
| 180 | fProxy = nullptr; |
| 181 | fOwnRef = false; |
| 182 | } else { |
| 183 | fProxy = proxy.release(); // due to the semantics of this class we unpack from sk_sp |
| 184 | fOwnRef = true; |
| 185 | fIOType = ioType; |
| 186 | } |
| 187 | } |
| 188 | |
Robert Phillips | b6deea8 | 2017-05-11 14:14:30 -0400 | [diff] [blame] | 189 | void GrSurfaceProxyRef::markPendingIO() const { |
Robert Phillips | 952a243 | 2017-05-04 08:52:22 -0400 | [diff] [blame] | 190 | // This should only be called when the owning GrProgramElement gets its first |
| 191 | // pendingExecution ref. |
| 192 | SkASSERT(!fPendingIO); |
| 193 | SkASSERT(fProxy); |
| 194 | fPendingIO = true; |
| 195 | switch (fIOType) { |
| 196 | case kRead_GrIOType: |
| 197 | fProxy->addPendingRead(); |
| 198 | break; |
| 199 | case kWrite_GrIOType: |
| 200 | fProxy->addPendingWrite(); |
| 201 | break; |
| 202 | case kRW_GrIOType: |
| 203 | fProxy->addPendingRead(); |
| 204 | fProxy->addPendingWrite(); |
| 205 | break; |
| 206 | } |
| 207 | } |
| 208 | |
Robert Phillips | b6deea8 | 2017-05-11 14:14:30 -0400 | [diff] [blame] | 209 | void GrSurfaceProxyRef::pendingIOComplete() const { |
Robert Phillips | 952a243 | 2017-05-04 08:52:22 -0400 | [diff] [blame] | 210 | // This should only be called when the owner's pending executions have ocurred but it is still |
| 211 | // reffed. |
| 212 | SkASSERT(fOwnRef); |
| 213 | SkASSERT(fPendingIO); |
| 214 | switch (fIOType) { |
| 215 | case kRead_GrIOType: |
| 216 | fProxy->completedRead(); |
| 217 | break; |
| 218 | case kWrite_GrIOType: |
| 219 | fProxy->completedWrite(); |
| 220 | break; |
| 221 | case kRW_GrIOType: |
| 222 | fProxy->completedRead(); |
| 223 | fProxy->completedWrite(); |
| 224 | break; |
| 225 | |
| 226 | } |
| 227 | fPendingIO = false; |
| 228 | } |
| 229 | |
Robert Phillips | b6deea8 | 2017-05-11 14:14:30 -0400 | [diff] [blame] | 230 | void GrSurfaceProxyRef::removeRef() const { |
Robert Phillips | 952a243 | 2017-05-04 08:52:22 -0400 | [diff] [blame] | 231 | // This should only be called once, when the owners last ref goes away and |
| 232 | // there is a pending execution. |
| 233 | SkASSERT(fOwnRef); |
| 234 | SkASSERT(fPendingIO); |
| 235 | SkASSERT(fProxy); |
| 236 | fProxy->unref(); |
| 237 | fOwnRef = false; |
| 238 | } |
| 239 | |