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 | |
bungeman | bf521ff | 2016-02-17 13:13:44 -0800 | [diff] [blame] | 11 | #include "../private/SkTArray.h" |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 12 | #include "SkRefCnt.h" |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 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 | /** |
Brian Salomon | e57194f | 2017-01-09 15:30:02 -0500 | [diff] [blame] | 17 | * Note: We are converting GrProcessor from ref counting to a single owner model using move |
| 18 | * semantics. This class will be removed. |
| 19 | * |
Brian Salomon | d61c9d9 | 2017-04-10 10:54:25 -0400 | [diff] [blame] | 20 | * This is used to track "refs" for two separate types GrProcessor ownership. A regular ref is owned |
| 21 | * by any client that may continue to issue draws that use the GrProgramElement. A recorded op or |
| 22 | * GrPipeline uses "pending executions" instead of refs. A pending execution is cleared after the |
| 23 | * draw is executed (or aborted). |
bsalomon | 4204800 | 2015-08-27 16:43:48 -0700 | [diff] [blame] | 24 | * |
| 25 | * While a GrProgramElement is ref'ed any resources it owns are also ref'ed. However, once it gets |
| 26 | * into the state where it has pending executions AND no refs then it converts its ownership of |
| 27 | * its GrGpuResources from refs to pending IOs. The pending IOs allow the cache to track when it is |
Brian Salomon | 09d994e | 2016-12-21 11:14:46 -0500 | [diff] [blame] | 28 | * safe to recycle a resource even though we still have buffered GrOps that read or write to the |
bsalomon | 4204800 | 2015-08-27 16:43:48 -0700 | [diff] [blame] | 29 | * the resource. |
| 30 | * |
Brian Salomon | d61c9d9 | 2017-04-10 10:54:25 -0400 | [diff] [blame] | 31 | * To make this work the subclass GrProcessor implements addPendingIOs, removeRefs, and |
| 32 | * pendingIOComplete. addPendingIOs adds pending reads/writes to GrGpuResources owned by the |
| 33 | * processor as appropriate when the processor is recorded in a GrOpList. removeRefs is called when |
| 34 | * the ref count reaches 0 and the GrProcessor is only owned by "pending executions". |
| 35 | * pendingIOComplete occurs if the resource is still owned by a ref but all recorded draws have been |
| 36 | * completed. Whenever pending executions and refs reach zero the processor is deleted. |
| 37 | * |
| 38 | * The GrProcessor may also implement notifyRefCntIsZero in order to change its ownership of child |
| 39 | * processors from ref to pending execution when the processor is first owned exclusively in pending |
| 40 | * execution mode. |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 41 | */ |
Brian Salomon | d61c9d9 | 2017-04-10 10:54:25 -0400 | [diff] [blame] | 42 | class GrProgramElement : public SkNoncopyable { |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 43 | public: |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 44 | virtual ~GrProgramElement() { |
| 45 | // fRefCnt can be one when an effect is created statically using GR_CREATE_STATIC_EFFECT |
| 46 | SkASSERT((0 == fRefCnt || 1 == fRefCnt) && 0 == fPendingExecutions); |
| 47 | // Set to invalid values. |
| 48 | SkDEBUGCODE(fRefCnt = fPendingExecutions = -10;) |
| 49 | } |
| 50 | |
| 51 | void ref() const { |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 52 | this->validate(); |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 53 | // Once the ref cnt reaches zero it should never be ref'ed again. |
| 54 | SkASSERT(fRefCnt > 0); |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 55 | ++fRefCnt; |
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 | |
| 59 | void unref() const { |
| 60 | this->validate(); |
| 61 | --fRefCnt; |
bsalomon | d012877 | 2014-10-03 05:31:41 -0700 | [diff] [blame] | 62 | if (0 == fRefCnt) { |
bsalomon | 4204800 | 2015-08-27 16:43:48 -0700 | [diff] [blame] | 63 | this->notifyRefCntIsZero(); |
bsalomon | d012877 | 2014-10-03 05:31:41 -0700 | [diff] [blame] | 64 | if (0 == fPendingExecutions) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 65 | delete this; |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 66 | return; |
bsalomon | d012877 | 2014-10-03 05:31:41 -0700 | [diff] [blame] | 67 | } else { |
Brian Salomon | d61c9d9 | 2017-04-10 10:54:25 -0400 | [diff] [blame] | 68 | this->removeRefs(); |
bsalomon | d012877 | 2014-10-03 05:31:41 -0700 | [diff] [blame] | 69 | } |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 70 | } |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 71 | this->validate(); |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | void validate() const { |
| 75 | #ifdef SK_DEBUG |
| 76 | SkASSERT(fRefCnt >= 0); |
| 77 | SkASSERT(fPendingExecutions >= 0); |
| 78 | SkASSERT(fRefCnt + fPendingExecutions > 0); |
| 79 | #endif |
| 80 | } |
| 81 | |
| 82 | protected: |
Brian Salomon | 19ae138 | 2017-01-09 10:14:34 -0500 | [diff] [blame] | 83 | GrProgramElement() : fRefCnt(1), fPendingExecutions(0) {} |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 84 | |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 85 | void addPendingExecution() const { |
| 86 | this->validate(); |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 87 | if (0 == fPendingExecutions) { |
Brian Salomon | d61c9d9 | 2017-04-10 10:54:25 -0400 | [diff] [blame] | 88 | this->addPendingIOs(); |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 89 | } |
| 90 | ++fPendingExecutions; |
| 91 | this->validate(); |
| 92 | } |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 93 | |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 94 | void completedExecution() const { |
| 95 | this->validate(); |
| 96 | --fPendingExecutions; |
| 97 | if (0 == fPendingExecutions) { |
| 98 | if (0 == fRefCnt) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 99 | delete this; |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 100 | return; |
| 101 | } else { |
Brian Salomon | d61c9d9 | 2017-04-10 10:54:25 -0400 | [diff] [blame] | 102 | this->pendingIOComplete(); |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | this->validate(); |
| 106 | } |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 107 | |
bsalomon | 4204800 | 2015-08-27 16:43:48 -0700 | [diff] [blame] | 108 | private: |
Brian Salomon | d61c9d9 | 2017-04-10 10:54:25 -0400 | [diff] [blame] | 109 | virtual void addPendingIOs() const = 0; |
| 110 | virtual void removeRefs() const = 0; |
| 111 | virtual void pendingIOComplete() const = 0; |
| 112 | |
bsalomon | 4204800 | 2015-08-27 16:43:48 -0700 | [diff] [blame] | 113 | /** This will be called when the ref cnt is zero. The object may or may not have pending |
| 114 | executions. */ |
| 115 | virtual void notifyRefCntIsZero() const = 0; |
| 116 | |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 117 | mutable int32_t fRefCnt; |
| 118 | // Count of deferred executions not yet issued to the 3D API. |
| 119 | mutable int32_t fPendingExecutions; |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 120 | |
Brian Salomon | d61c9d9 | 2017-04-10 10:54:25 -0400 | [diff] [blame] | 121 | // Only these classes can access addPendingExecution() and completedExecution(). |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 122 | template <typename T> friend class GrPendingProgramElement; |
Brian Salomon | 92ce594 | 2017-01-18 11:01:10 -0500 | [diff] [blame] | 123 | friend class GrProcessorSet; |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 124 | |
| 125 | typedef SkNoncopyable INHERITED; |
| 126 | }; |
| 127 | |
| 128 | #endif |