blob: 405679d4f17d6c8140b05748f0c0811a59e153a1 [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 {
bsalomonac8d6192014-09-04 14:13:44 -070069 // This should only be called when the owning GrProgramElement gets its first
bsalomon95740982014-09-04 13:12:37 -070070 // pendingExecution ref.
71 SkASSERT(!fPendingIO);
bsalomon49f085d2014-09-05 13:34:00 -070072 SkASSERT(fResource);
bsalomon95740982014-09-04 13:12:37 -070073 fPendingIO = true;
74 switch (fIOType) {
bsalomonbcf0a522014-10-08 08:40:09 -070075 case kRead_GrIOType:
bsalomon95740982014-09-04 13:12:37 -070076 fResource->addPendingRead();
77 break;
bsalomonbcf0a522014-10-08 08:40:09 -070078 case kWrite_GrIOType:
bsalomon95740982014-09-04 13:12:37 -070079 fResource->addPendingWrite();
80 break;
bsalomonbcf0a522014-10-08 08:40:09 -070081 case kRW_GrIOType:
bsalomon95740982014-09-04 13:12:37 -070082 fResource->addPendingRead();
83 fResource->addPendingWrite();
84 break;
bsalomon95740982014-09-04 13:12:37 -070085 }
86}
87
bsalomonf96ba022014-09-17 08:05:40 -070088void GrGpuResourceRef::pendingIOComplete() const {
bsalomonac8d6192014-09-04 14:13:44 -070089 // 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) {
bsalomonbcf0a522014-10-08 08:40:09 -070094 case kRead_GrIOType:
bsalomonac8d6192014-09-04 14:13:44 -070095 fResource->completedRead();
96 break;
bsalomonbcf0a522014-10-08 08:40:09 -070097 case kWrite_GrIOType:
bsalomonac8d6192014-09-04 14:13:44 -070098 fResource->completedWrite();
99 break;
bsalomonbcf0a522014-10-08 08:40:09 -0700100 case kRW_GrIOType:
bsalomonac8d6192014-09-04 14:13:44 -0700101 fResource->completedRead();
102 fResource->completedWrite();
103 break;
104
105 }
106 fPendingIO = false;
107}
108
bsalomonf96ba022014-09-17 08:05:40 -0700109void GrGpuResourceRef::removeRef() const {
bsalomon95740982014-09-04 13:12:37 -0700110 // 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);
bsalomon49f085d2014-09-05 13:34:00 -0700114 SkASSERT(fResource);
bsalomon95740982014-09-04 13:12:37 -0700115 fResource->unref();
116 fOwnRef = false;
117}