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 | #include "GrGpuResource.h" |
| 10 | |
bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame^] | 11 | GrGpuResourceRef::GrGpuResourceRef() { |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 12 | fResource = NULL; |
| 13 | fOwnRef = false; |
| 14 | fPendingIO = false; |
| 15 | fIOType = kNone_IOType; |
| 16 | } |
| 17 | |
bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame^] | 18 | GrGpuResourceRef::GrGpuResourceRef(GrGpuResource* resource, IOType ioType) { |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 19 | fResource = NULL; |
| 20 | fOwnRef = false; |
| 21 | fPendingIO = false; |
| 22 | this->setResource(resource, ioType); |
| 23 | } |
| 24 | |
bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame^] | 25 | GrGpuResourceRef::~GrGpuResourceRef() { |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 26 | if (fOwnRef) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 27 | SkASSERT(fResource); |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 28 | fResource->unref(); |
| 29 | } |
| 30 | if (fPendingIO) { |
| 31 | switch (fIOType) { |
| 32 | case kNone_IOType: |
| 33 | SkFAIL("Shouldn't get here if fIOType is kNone."); |
| 34 | break; |
| 35 | case kRead_IOType: |
| 36 | fResource->completedRead(); |
| 37 | break; |
| 38 | case kWrite_IOType: |
| 39 | fResource->completedWrite(); |
| 40 | break; |
| 41 | case kRW_IOType: |
| 42 | fResource->completedRead(); |
| 43 | fResource->completedWrite(); |
| 44 | break; |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame^] | 49 | void GrGpuResourceRef::reset() { |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 50 | SkASSERT(!fPendingIO); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 51 | SkASSERT(SkToBool(fResource) == fOwnRef); |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 52 | if (fOwnRef) { |
| 53 | fResource->unref(); |
| 54 | fOwnRef = false; |
| 55 | fResource = NULL; |
| 56 | fIOType = kNone_IOType; |
| 57 | } |
| 58 | } |
| 59 | |
bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame^] | 60 | void GrGpuResourceRef::setResource(GrGpuResource* resource, IOType ioType) { |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 61 | SkASSERT(!fPendingIO); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 62 | SkASSERT(SkToBool(fResource) == fOwnRef); |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 63 | SkSafeUnref(fResource); |
| 64 | if (NULL == resource) { |
| 65 | fResource = NULL; |
| 66 | fOwnRef = false; |
| 67 | fIOType = kNone_IOType; |
| 68 | } else { |
| 69 | SkASSERT(kNone_IOType != ioType); |
| 70 | fResource = resource; |
| 71 | fOwnRef = true; |
| 72 | fIOType = ioType; |
| 73 | } |
| 74 | } |
| 75 | |
bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame^] | 76 | void GrGpuResourceRef::markPendingIO() const { |
bsalomon | ac8d619 | 2014-09-04 14:13:44 -0700 | [diff] [blame] | 77 | // This should only be called when the owning GrProgramElement gets its first |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 78 | // pendingExecution ref. |
| 79 | SkASSERT(!fPendingIO); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 80 | SkASSERT(fResource); |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 81 | fPendingIO = true; |
| 82 | switch (fIOType) { |
| 83 | case kNone_IOType: |
bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame^] | 84 | SkFAIL("GrGpuResourceRef with neither reads nor writes?"); |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 85 | break; |
| 86 | case kRead_IOType: |
| 87 | fResource->addPendingRead(); |
| 88 | break; |
| 89 | case kWrite_IOType: |
| 90 | fResource->addPendingWrite(); |
| 91 | break; |
| 92 | case kRW_IOType: |
| 93 | fResource->addPendingRead(); |
| 94 | fResource->addPendingWrite(); |
| 95 | break; |
| 96 | |
| 97 | } |
| 98 | } |
| 99 | |
bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame^] | 100 | void GrGpuResourceRef::pendingIOComplete() const { |
bsalomon | ac8d619 | 2014-09-04 14:13:44 -0700 | [diff] [blame] | 101 | // This should only be called when the owner's pending executions have ocurred but it is still |
| 102 | // reffed. |
| 103 | SkASSERT(fOwnRef); |
| 104 | SkASSERT(fPendingIO); |
| 105 | switch (fIOType) { |
| 106 | case kNone_IOType: |
bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame^] | 107 | SkFAIL("GrGpuResourceRef with neither reads nor writes?"); |
bsalomon | ac8d619 | 2014-09-04 14:13:44 -0700 | [diff] [blame] | 108 | break; |
| 109 | case kRead_IOType: |
| 110 | fResource->completedRead(); |
| 111 | break; |
| 112 | case kWrite_IOType: |
| 113 | fResource->completedWrite(); |
| 114 | break; |
| 115 | case kRW_IOType: |
| 116 | fResource->completedRead(); |
| 117 | fResource->completedWrite(); |
| 118 | break; |
| 119 | |
| 120 | } |
| 121 | fPendingIO = false; |
| 122 | } |
| 123 | |
bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame^] | 124 | void GrGpuResourceRef::removeRef() const { |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 125 | // This should only be called once, when the owners last ref goes away and |
| 126 | // there is a pending execution. |
| 127 | SkASSERT(fOwnRef); |
| 128 | SkASSERT(fPendingIO); |
| 129 | SkASSERT(kNone_IOType != fIOType); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 130 | SkASSERT(fResource); |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 131 | fResource->unref(); |
| 132 | fOwnRef = false; |
| 133 | } |