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 | #ifndef GrProgramElement_DEFINED |
| 9 | #define GrProgramElement_DEFINED |
| 10 | |
| 11 | #include "SkRefCnt.h" |
| 12 | #include "SkTArray.h" |
| 13 | |
bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame] | 14 | class GrGpuResourceRef; |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 15 | |
| 16 | /** |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 17 | * Base class for GrProcessor. GrDrawState uses this to manage |
| 18 | * transitioning a GrProcessor from being owned by a client to being scheduled for execution. It |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 19 | * converts resources owned by the effect from being ref'ed to having pending reads/writes. |
| 20 | * |
| 21 | * All GrGpuResource objects owned by a GrProgramElement or derived classes (either directly or |
bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame] | 22 | * indirectly) must be wrapped in a GrGpuResourceRef and registered with the GrProgramElement using |
| 23 | * addGpuResource(). This allows the regular refs to be converted to pending IO events |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 24 | * when the program element is scheduled for deferred execution. |
| 25 | */ |
| 26 | class GrProgramElement : public SkNoncopyable { |
| 27 | public: |
mtklein | 6f07665 | 2015-01-13 08:22:43 -0800 | [diff] [blame] | 28 | SK_DECLARE_INST_COUNT(GrProgramElement) |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 29 | |
| 30 | virtual ~GrProgramElement() { |
| 31 | // fRefCnt can be one when an effect is created statically using GR_CREATE_STATIC_EFFECT |
| 32 | SkASSERT((0 == fRefCnt || 1 == fRefCnt) && 0 == fPendingExecutions); |
| 33 | // Set to invalid values. |
| 34 | SkDEBUGCODE(fRefCnt = fPendingExecutions = -10;) |
| 35 | } |
| 36 | |
| 37 | void ref() const { |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 38 | this->validate(); |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 39 | // Once the ref cnt reaches zero it should never be ref'ed again. |
| 40 | SkASSERT(fRefCnt > 0); |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 41 | ++fRefCnt; |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 42 | this->validate(); |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | void unref() const { |
| 46 | this->validate(); |
| 47 | --fRefCnt; |
bsalomon | d012877 | 2014-10-03 05:31:41 -0700 | [diff] [blame] | 48 | if (0 == fRefCnt) { |
| 49 | if (0 == fPendingExecutions) { |
| 50 | SkDELETE(this); |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 51 | return; |
bsalomon | d012877 | 2014-10-03 05:31:41 -0700 | [diff] [blame] | 52 | } else { |
| 53 | this->removeRefs(); |
| 54 | } |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 55 | } |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 56 | this->validate(); |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 57 | } |
| 58 | |
bsalomon | 52e9d63 | 2014-09-05 12:23:12 -0700 | [diff] [blame] | 59 | /** |
| 60 | * Gets an id that is unique for this GrProgramElement object. This will never return 0. |
| 61 | */ |
| 62 | uint32_t getUniqueID() const { return fUniqueID; } |
| 63 | |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 64 | void validate() const { |
| 65 | #ifdef SK_DEBUG |
| 66 | SkASSERT(fRefCnt >= 0); |
| 67 | SkASSERT(fPendingExecutions >= 0); |
| 68 | SkASSERT(fRefCnt + fPendingExecutions > 0); |
| 69 | #endif |
| 70 | } |
| 71 | |
| 72 | protected: |
bsalomon | 52e9d63 | 2014-09-05 12:23:12 -0700 | [diff] [blame] | 73 | GrProgramElement() : fRefCnt(1), fPendingExecutions(0), fUniqueID(CreateUniqueID()) {} |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 74 | |
| 75 | /** Subclasses registers their resources using this function. It is assumed the GrProgramResouce |
| 76 | is and will remain owned by the subclass and this function will retain a raw ptr. Once a |
bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame] | 77 | GrGpuResourceRef is registered its setResource must not be called. |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 78 | */ |
bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame] | 79 | void addGpuResource(const GrGpuResourceRef* res) { |
| 80 | fGpuResources.push_back(res); |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | private: |
bsalomon | 52e9d63 | 2014-09-05 12:23:12 -0700 | [diff] [blame] | 84 | static uint32_t CreateUniqueID(); |
| 85 | |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 86 | void addPendingExecution() const { |
| 87 | this->validate(); |
| 88 | SkASSERT(fRefCnt > 0); |
| 89 | if (0 == fPendingExecutions) { |
| 90 | this->addPendingIOs(); |
| 91 | } |
| 92 | ++fPendingExecutions; |
| 93 | this->validate(); |
| 94 | } |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 95 | |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 96 | void completedExecution() const { |
| 97 | this->validate(); |
| 98 | --fPendingExecutions; |
| 99 | if (0 == fPendingExecutions) { |
| 100 | if (0 == fRefCnt) { |
| 101 | SkDELETE(this); |
| 102 | return; |
| 103 | } else { |
| 104 | this->pendingIOComplete(); |
| 105 | } |
| 106 | } |
| 107 | this->validate(); |
| 108 | } |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 109 | |
bsalomon | d012877 | 2014-10-03 05:31:41 -0700 | [diff] [blame] | 110 | void removeRefs() const; |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 111 | void addPendingIOs() const; |
| 112 | void pendingIOComplete() const; |
bsalomon | d012877 | 2014-10-03 05:31:41 -0700 | [diff] [blame] | 113 | |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 114 | mutable int32_t fRefCnt; |
| 115 | // Count of deferred executions not yet issued to the 3D API. |
| 116 | mutable int32_t fPendingExecutions; |
bsalomon | 52e9d63 | 2014-09-05 12:23:12 -0700 | [diff] [blame] | 117 | uint32_t fUniqueID; |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 118 | |
bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame] | 119 | SkSTArray<4, const GrGpuResourceRef*, true> fGpuResources; |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 120 | |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 121 | // Only this class can access addPendingExecution() and completedExecution(). |
| 122 | template <typename T> friend class GrPendingProgramElement; |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 123 | |
| 124 | typedef SkNoncopyable INHERITED; |
| 125 | }; |
| 126 | |
| 127 | #endif |