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 | * |
bsalomon | 4204800 | 2015-08-27 16:43:48 -0700 | [diff] [blame] | 20 | * Base class for GrProcessor. This exists to manage transitioning a GrProcessor from being owned by |
| 21 | * a client to being scheduled for execution. While a GrProcessor is ref'ed by drawing code its |
| 22 | * GrGpu resources must also be ref'ed to prevent incorrectly recycling them through the cache. |
| 23 | * However, once the GrProcessor is baked into a GrPipeline and the drawing code has stopped ref'ing |
| 24 | * it, it's internal resources can be recycled in some cases. |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 25 | * |
bsalomon | 4204800 | 2015-08-27 16:43:48 -0700 | [diff] [blame] | 26 | * We track this using two types of refs on GrProgramElement. A regular ref is owned by any client |
| 27 | * that may continue to issue draws that use the GrProgramElement. The GrPipeline owns "pending |
| 28 | * executions" instead of refs. A pending execution is cleared by ~GrPipeline(). |
| 29 | * |
| 30 | * While a GrProgramElement is ref'ed any resources it owns are also ref'ed. However, once it gets |
| 31 | * into the state where it has pending executions AND no refs then it converts its ownership of |
| 32 | * 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] | 33 | * 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] | 34 | * the resource. |
| 35 | * |
Brian Salomon | e57194f | 2017-01-09 15:30:02 -0500 | [diff] [blame] | 36 | * To make this work the subclass, GrProcessor, implements addPendingIOs and pendingIOComplete. The |
| 37 | * former adds pending reads/writes as appropriate when the processor is recorded in a GrOpList. The |
| 38 | * latter removes them after the op list executes the operation. These calls must propagate to any |
| 39 | * children processors. Similarly, the subclass implements a removeRefs function in order to remove |
| 40 | * refs from resources once the processor is only owned for pending execution. |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 41 | */ |
Brian Salomon | e57194f | 2017-01-09 15:30:02 -0500 | [diff] [blame] | 42 | template<typename DERIVED> 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 | e57194f | 2017-01-09 15:30:02 -0500 | [diff] [blame] | 68 | static_cast<const DERIVED*>(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(); |
| 87 | SkASSERT(fRefCnt > 0); |
| 88 | if (0 == fPendingExecutions) { |
Brian Salomon | e57194f | 2017-01-09 15:30:02 -0500 | [diff] [blame] | 89 | static_cast<const DERIVED*>(this)->addPendingIOs(); |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 90 | } |
| 91 | ++fPendingExecutions; |
| 92 | this->validate(); |
| 93 | } |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 94 | |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 95 | void completedExecution() const { |
| 96 | this->validate(); |
| 97 | --fPendingExecutions; |
| 98 | if (0 == fPendingExecutions) { |
| 99 | if (0 == fRefCnt) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 100 | delete this; |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 101 | return; |
| 102 | } else { |
Brian Salomon | e57194f | 2017-01-09 15:30:02 -0500 | [diff] [blame] | 103 | static_cast<const DERIVED*>(this)->pendingIOComplete(); |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | this->validate(); |
| 107 | } |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 108 | |
bsalomon | 4204800 | 2015-08-27 16:43:48 -0700 | [diff] [blame] | 109 | private: |
| 110 | /** This will be called when the ref cnt is zero. The object may or may not have pending |
| 111 | executions. */ |
| 112 | virtual void notifyRefCntIsZero() const = 0; |
| 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 | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 117 | |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 118 | // Only this class can access addPendingExecution() and completedExecution(). |
| 119 | template <typename T> friend class GrPendingProgramElement; |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 120 | |
| 121 | typedef SkNoncopyable INHERITED; |
| 122 | }; |
| 123 | |
| 124 | #endif |