blob: b4703830ec020fa3180df7e3b3fb99175f83dbe2 [file] [log] [blame]
bsalomon95740982014-09-04 13:12:37 -07001/*
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
bsalomonf96ba022014-09-17 08:05:40 -07008#include "GrGpuResourceRef.h"
bsalomon95740982014-09-04 13:12:37 -07009
bsalomonf96ba022014-09-17 08:05:40 -070010GrGpuResourceRef::GrGpuResourceRef() {
halcanary96fcdcc2015-08-27 07:41:13 -070011 fResource = nullptr;
bsalomon95740982014-09-04 13:12:37 -070012 fOwnRef = false;
13 fPendingIO = false;
bsalomon95740982014-09-04 13:12:37 -070014}
15
bsalomonbcf0a522014-10-08 08:40:09 -070016GrGpuResourceRef::GrGpuResourceRef(GrGpuResource* resource, GrIOType ioType) {
halcanary96fcdcc2015-08-27 07:41:13 -070017 fResource = nullptr;
bsalomon95740982014-09-04 13:12:37 -070018 fOwnRef = false;
19 fPendingIO = false;
20 this->setResource(resource, ioType);
21}
22
bsalomonf96ba022014-09-17 08:05:40 -070023GrGpuResourceRef::~GrGpuResourceRef() {
bsalomon95740982014-09-04 13:12:37 -070024 if (fOwnRef) {
bsalomon49f085d2014-09-05 13:34:00 -070025 SkASSERT(fResource);
bsalomon95740982014-09-04 13:12:37 -070026 fResource->unref();
27 }
28 if (fPendingIO) {
29 switch (fIOType) {
bsalomonbcf0a522014-10-08 08:40:09 -070030 case kRead_GrIOType:
bsalomon95740982014-09-04 13:12:37 -070031 fResource->completedRead();
32 break;
bsalomonbcf0a522014-10-08 08:40:09 -070033 case kWrite_GrIOType:
bsalomon95740982014-09-04 13:12:37 -070034 fResource->completedWrite();
35 break;
bsalomonbcf0a522014-10-08 08:40:09 -070036 case kRW_GrIOType:
bsalomon95740982014-09-04 13:12:37 -070037 fResource->completedRead();
38 fResource->completedWrite();
39 break;
40 }
41 }
42}
43
bsalomonf96ba022014-09-17 08:05:40 -070044void GrGpuResourceRef::reset() {
bsalomon95740982014-09-04 13:12:37 -070045 SkASSERT(!fPendingIO);
bsalomon49f085d2014-09-05 13:34:00 -070046 SkASSERT(SkToBool(fResource) == fOwnRef);
bsalomon95740982014-09-04 13:12:37 -070047 if (fOwnRef) {
48 fResource->unref();
49 fOwnRef = false;
halcanary96fcdcc2015-08-27 07:41:13 -070050 fResource = nullptr;
bsalomon95740982014-09-04 13:12:37 -070051 }
52}
53
bsalomonbcf0a522014-10-08 08:40:09 -070054void GrGpuResourceRef::setResource(GrGpuResource* resource, GrIOType ioType) {
bsalomon95740982014-09-04 13:12:37 -070055 SkASSERT(!fPendingIO);
bsalomon49f085d2014-09-05 13:34:00 -070056 SkASSERT(SkToBool(fResource) == fOwnRef);
bsalomon95740982014-09-04 13:12:37 -070057 SkSafeUnref(fResource);
halcanary96fcdcc2015-08-27 07:41:13 -070058 if (nullptr == resource) {
59 fResource = nullptr;
bsalomon95740982014-09-04 13:12:37 -070060 fOwnRef = false;
bsalomon95740982014-09-04 13:12:37 -070061 } else {
bsalomon95740982014-09-04 13:12:37 -070062 fResource = resource;
63 fOwnRef = true;
64 fIOType = ioType;
65 }
66}
67
bsalomonf96ba022014-09-17 08:05:40 -070068void GrGpuResourceRef::markPendingIO() const {
Robert Phillipsa91e0b72017-05-01 13:12:20 -040069 if (!fResource) {
70 return;
71 }
72
bsalomonac8d6192014-09-04 14:13:44 -070073 // This should only be called when the owning GrProgramElement gets its first
bsalomon95740982014-09-04 13:12:37 -070074 // pendingExecution ref.
75 SkASSERT(!fPendingIO);
bsalomon95740982014-09-04 13:12:37 -070076 fPendingIO = true;
77 switch (fIOType) {
bsalomonbcf0a522014-10-08 08:40:09 -070078 case kRead_GrIOType:
bsalomon95740982014-09-04 13:12:37 -070079 fResource->addPendingRead();
80 break;
bsalomonbcf0a522014-10-08 08:40:09 -070081 case kWrite_GrIOType:
bsalomon95740982014-09-04 13:12:37 -070082 fResource->addPendingWrite();
83 break;
bsalomonbcf0a522014-10-08 08:40:09 -070084 case kRW_GrIOType:
bsalomon95740982014-09-04 13:12:37 -070085 fResource->addPendingRead();
86 fResource->addPendingWrite();
87 break;
bsalomon95740982014-09-04 13:12:37 -070088 }
89}
90
bsalomonf96ba022014-09-17 08:05:40 -070091void GrGpuResourceRef::pendingIOComplete() const {
Robert Phillipsa91e0b72017-05-01 13:12:20 -040092 if (!fResource) {
93 return;
94 }
95
bsalomonac8d6192014-09-04 14:13:44 -070096 // 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) {
bsalomonbcf0a522014-10-08 08:40:09 -0700101 case kRead_GrIOType:
bsalomonac8d6192014-09-04 14:13:44 -0700102 fResource->completedRead();
103 break;
bsalomonbcf0a522014-10-08 08:40:09 -0700104 case kWrite_GrIOType:
bsalomonac8d6192014-09-04 14:13:44 -0700105 fResource->completedWrite();
106 break;
bsalomonbcf0a522014-10-08 08:40:09 -0700107 case kRW_GrIOType:
bsalomonac8d6192014-09-04 14:13:44 -0700108 fResource->completedRead();
109 fResource->completedWrite();
110 break;
111
112 }
113 fPendingIO = false;
114}
115
bsalomonf96ba022014-09-17 08:05:40 -0700116void GrGpuResourceRef::removeRef() const {
Robert Phillipsa91e0b72017-05-01 13:12:20 -0400117 if (!fResource) {
118 return;
119 }
120
bsalomon95740982014-09-04 13:12:37 -0700121 // 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);
bsalomon95740982014-09-04 13:12:37 -0700125 fResource->unref();
126 fOwnRef = false;
127}
Robert Phillips952a2432017-05-04 08:52:22 -0400128
129///////////////////////////////////////////////////////////////////////////////
130#include "GrTextureProxy.h"
131
Robert Phillipsb6deea82017-05-11 14:14:30 -0400132GrSurfaceProxyRef::GrSurfaceProxyRef() {
Robert Phillips952a2432017-05-04 08:52:22 -0400133 fProxy = nullptr;
134 fOwnRef = false;
135 fPendingIO = false;
136}
137
Robert Phillipsb6deea82017-05-11 14:14:30 -0400138GrSurfaceProxyRef::GrSurfaceProxyRef(sk_sp<GrSurfaceProxy> proxy, GrIOType ioType) {
Robert Phillips952a2432017-05-04 08:52:22 -0400139 fProxy = nullptr;
140 fOwnRef = false;
141 fPendingIO = false;
Robert Phillipsb6deea82017-05-11 14:14:30 -0400142 this->setProxy(std::move(proxy), ioType);
Robert Phillips952a2432017-05-04 08:52:22 -0400143}
144
Robert Phillipsb6deea82017-05-11 14:14:30 -0400145GrSurfaceProxyRef::~GrSurfaceProxyRef() {
Robert Phillipsd9914862017-06-14 15:16:59 -0400146 this->reset();
147}
148
149void GrSurfaceProxyRef::reset() {
Robert Phillipsc8ecb032017-06-14 17:55:12 +0000150 if (fPendingIO) {
Robert Phillipsd9914862017-06-14 15:16:59 -0400151 SkASSERT(fProxy);
Robert Phillips952a2432017-05-04 08:52:22 -0400152 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 Phillipsd9914862017-06-14 15:16:59 -0400164 fPendingIO = false;
Robert Phillips952a2432017-05-04 08:52:22 -0400165 }
Robert Phillips952a2432017-05-04 08:52:22 -0400166 if (fOwnRef) {
Robert Phillipsd9914862017-06-14 15:16:59 -0400167 SkASSERT(fProxy);
Robert Phillips952a2432017-05-04 08:52:22 -0400168 fProxy->unref();
169 fOwnRef = false;
Robert Phillips952a2432017-05-04 08:52:22 -0400170 }
Robert Phillipsd9914862017-06-14 15:16:59 -0400171
172 fProxy = nullptr;
Robert Phillips952a2432017-05-04 08:52:22 -0400173}
174
Robert Phillipsb6deea82017-05-11 14:14:30 -0400175void GrSurfaceProxyRef::setProxy(sk_sp<GrSurfaceProxy> proxy, GrIOType ioType) {
Robert Phillips952a2432017-05-04 08:52:22 -0400176 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 Phillipsb6deea82017-05-11 14:14:30 -0400189void GrSurfaceProxyRef::markPendingIO() const {
Robert Phillips952a2432017-05-04 08:52:22 -0400190 // 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 Phillipsb6deea82017-05-11 14:14:30 -0400209void GrSurfaceProxyRef::pendingIOComplete() const {
Robert Phillips952a2432017-05-04 08:52:22 -0400210 // 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 Phillipsb6deea82017-05-11 14:14:30 -0400230void GrSurfaceProxyRef::removeRef() const {
Robert Phillips952a2432017-05-04 08:52:22 -0400231 // 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