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