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 | |
| 8 | #include "GrProgramResource.h" |
| 9 | #include "GrGpuResource.h" |
| 10 | |
| 11 | GrProgramResource::GrProgramResource() { |
| 12 | fResource = NULL; |
| 13 | fOwnRef = false; |
| 14 | fPendingIO = false; |
| 15 | fIOType = kNone_IOType; |
| 16 | } |
| 17 | |
| 18 | GrProgramResource::GrProgramResource(GrGpuResource* resource, IOType ioType) { |
| 19 | fResource = NULL; |
| 20 | fOwnRef = false; |
| 21 | fPendingIO = false; |
| 22 | this->setResource(resource, ioType); |
| 23 | } |
| 24 | |
| 25 | GrProgramResource::~GrProgramResource() { |
| 26 | if (fOwnRef) { |
| 27 | SkASSERT(NULL != fResource); |
| 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 | |
| 49 | void GrProgramResource::reset() { |
| 50 | SkASSERT(!fPendingIO); |
| 51 | SkASSERT((NULL != fResource) == fOwnRef); |
| 52 | if (fOwnRef) { |
| 53 | fResource->unref(); |
| 54 | fOwnRef = false; |
| 55 | fResource = NULL; |
| 56 | fIOType = kNone_IOType; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | void GrProgramResource::setResource(GrGpuResource* resource, IOType ioType) { |
| 61 | SkASSERT(!fPendingIO); |
| 62 | SkASSERT((NULL != fResource) == fOwnRef); |
| 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 | |
| 76 | void GrProgramResource::markPendingIO() const { |
| 77 | // This should only be called once, when the owning GrProgramElement gets its first |
| 78 | // pendingExecution ref. |
| 79 | SkASSERT(!fPendingIO); |
| 80 | SkASSERT(NULL != fResource); |
| 81 | fPendingIO = true; |
| 82 | switch (fIOType) { |
| 83 | case kNone_IOType: |
| 84 | SkFAIL("GrProgramResource with neither reads nor writes?"); |
| 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 | |
| 100 | void GrProgramResource::removeRef() const { |
| 101 | // This should only be called once, when the owners last ref goes away and |
| 102 | // there is a pending execution. |
| 103 | SkASSERT(fOwnRef); |
| 104 | SkASSERT(fPendingIO); |
| 105 | SkASSERT(kNone_IOType != fIOType); |
| 106 | SkASSERT(NULL != fResource); |
| 107 | fResource->unref(); |
| 108 | fOwnRef = false; |
| 109 | } |