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 | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 8 | #include "GrProgramElement.h" |
bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame] | 9 | #include "GrGpuResourceRef.h" |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 10 | |
bsalomon | 52e9d63 | 2014-09-05 12:23:12 -0700 | [diff] [blame] | 11 | uint32_t GrProgramElement::CreateUniqueID() { |
| 12 | static int32_t gUniqueID = SK_InvalidUniqueID; |
| 13 | uint32_t id; |
| 14 | do { |
| 15 | id = static_cast<uint32_t>(sk_atomic_inc(&gUniqueID) + 1); |
| 16 | } while (id == SK_InvalidUniqueID); |
| 17 | return id; |
| 18 | } |
| 19 | |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 20 | void GrProgramElement::convertRefToPendingExecution() const { |
bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame] | 21 | // This function makes it so that all the GrGpuResourceRefs own a single ref to their |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 22 | // underlying GrGpuResource if there are any refs to the GrProgramElement and a single |
| 23 | // pending read/write if there are any pending executions of the GrProgramElement. The |
bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame] | 24 | // GrGpuResourceRef will give up its single ref and/or pending read/write in its destructor. |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 25 | SkASSERT(fRefCnt > 0); |
| 26 | if (0 == fPendingExecutions) { |
bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame] | 27 | for (int i = 0; i < fGpuResources.count(); ++i) { |
| 28 | fGpuResources[i]->markPendingIO(); |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 29 | } |
| 30 | } |
| 31 | ++fPendingExecutions; |
| 32 | this->unref(); |
| 33 | if (0 == fRefCnt) { |
bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame] | 34 | for (int i = 0; i < fGpuResources.count(); ++i) { |
| 35 | fGpuResources[i]->removeRef(); |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 36 | } |
| 37 | } |
| 38 | } |
bsalomon | ac8d619 | 2014-09-04 14:13:44 -0700 | [diff] [blame] | 39 | |
| 40 | void GrProgramElement::completedExecution() const { |
| 41 | this->validate(); |
| 42 | --fPendingExecutions; |
| 43 | if (0 == fPendingExecutions) { |
| 44 | if (0 == fRefCnt) { |
| 45 | SkDELETE(this); |
| 46 | } else { |
| 47 | // Now our pending executions have ocurred and we still have refs. Convert |
| 48 | // ownership of our resources back to regular refs. |
bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame] | 49 | for (int i = 0; i < fGpuResources.count(); ++i) { |
| 50 | fGpuResources[i]->pendingIOComplete(); |
bsalomon | ac8d619 | 2014-09-04 14:13:44 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | } |
| 54 | } |
| 55 | } |