Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 Google LLC |
| 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 GrRenderTask_DEFINED |
| 9 | #define GrRenderTask_DEFINED |
| 10 | |
| 11 | #include "include/core/SkRefCnt.h" |
| 12 | #include "include/private/SkColorData.h" |
| 13 | #include "include/private/SkTDArray.h" |
Greg Daniel | 16f5c65 | 2019-10-29 11:26:01 -0400 | [diff] [blame] | 14 | #include "src/gpu/GrSurfaceProxyView.h" |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 15 | #include "src/gpu/GrTextureProxy.h" |
Chris Dalton | 0875512 | 2019-08-05 16:13:47 -0600 | [diff] [blame] | 16 | #include "src/gpu/GrTextureResolveManager.h" |
Herb Derby | 082232b | 2020-06-10 15:08:18 -0400 | [diff] [blame] | 17 | #include "src/gpu/ops/GrOp.h" |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 18 | |
| 19 | class GrOpFlushState; |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 20 | class GrOpsTask; |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 21 | class GrResourceAllocator; |
Chris Dalton | e2a903e | 2019-09-18 13:41:50 -0600 | [diff] [blame] | 22 | class GrTextureResolveRenderTask; |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 23 | |
| 24 | // This class abstracts a task that targets a single GrSurfaceProxy, participates in the |
| 25 | // GrDrawingManager's DAG, and implements the onExecute method to modify its target proxy's |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 26 | // contents. (e.g., an opsTask that executes a command buffer, a task to regenerate mipmaps, etc.) |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 27 | class GrRenderTask : public SkRefCnt { |
| 28 | public: |
Greg Daniel | 16f5c65 | 2019-10-29 11:26:01 -0400 | [diff] [blame] | 29 | GrRenderTask(); |
Adlai Holler | 33d569e | 2020-06-16 14:30:08 -0400 | [diff] [blame] | 30 | SkDEBUGCODE(~GrRenderTask() override); |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 31 | |
Chris Dalton | aa3cbb8 | 2019-08-21 00:01:21 -0600 | [diff] [blame] | 32 | void makeClosed(const GrCaps&); |
| 33 | |
Robert Phillips | 29f3854 | 2019-10-16 09:20:25 -0400 | [diff] [blame] | 34 | void prePrepare(GrRecordingContext* context) { this->onPrePrepare(context); } |
Robert Phillips | 7327c9d | 2019-10-08 16:32:56 -0400 | [diff] [blame] | 35 | |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 36 | // These two methods are only invoked at flush time |
| 37 | void prepare(GrOpFlushState* flushState); |
| 38 | bool execute(GrOpFlushState* flushState) { return this->onExecute(flushState); } |
| 39 | |
Robert Phillips | 07f675d | 2020-11-16 13:44:01 -0500 | [diff] [blame] | 40 | virtual bool requiresExplicitCleanup() const { return false; } |
| 41 | |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 42 | // Called when this class will survive a flush and needs to truncate its ops and start over. |
| 43 | // TODO: ultimately it should be invalid for an op list to survive a flush. |
| 44 | // https://bugs.chromium.org/p/skia/issues/detail?id=7111 |
Adlai Holler | d71b7b0 | 2020-06-08 15:55:00 -0400 | [diff] [blame] | 45 | virtual void endFlush(GrDrawingManager*) {} |
| 46 | |
| 47 | // This method "disowns" all the GrSurfaceProxies this RenderTask modifies. In |
| 48 | // practice this just means telling the drawingManager to forget the relevant |
| 49 | // mappings from surface proxy to last modifying rendertask. |
| 50 | virtual void disown(GrDrawingManager*); |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 51 | |
| 52 | bool isClosed() const { return this->isSetFlag(kClosed_Flag); } |
| 53 | |
| 54 | /* |
| 55 | * Notify this GrRenderTask that it relies on the contents of 'dependedOn' |
| 56 | */ |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 57 | void addDependency(GrDrawingManager*, GrSurfaceProxy* dependedOn, GrMipmapped, |
Adlai Holler | d71b7b0 | 2020-06-08 15:55:00 -0400 | [diff] [blame] | 58 | GrTextureResolveManager, const GrCaps& caps); |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 59 | |
| 60 | /* |
Greg Daniel | c30f1a9 | 2019-09-06 15:28:58 -0400 | [diff] [blame] | 61 | * Notify this GrRenderTask that it relies on the contents of all GrRenderTasks which otherTask |
| 62 | * depends on. |
| 63 | */ |
| 64 | void addDependenciesFromOtherTask(GrRenderTask* otherTask); |
| 65 | |
| 66 | /* |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 67 | * Does this renderTask depend on 'dependedOn'? |
| 68 | */ |
| 69 | bool dependsOn(const GrRenderTask* dependedOn) const; |
| 70 | |
Robert Phillips | 07f675d | 2020-11-16 13:44:01 -0500 | [diff] [blame] | 71 | virtual void gatherIDs(SkSTArray<8, uint32_t, true>* idArray) const { |
| 72 | idArray->push_back(fUniqueID); |
| 73 | } |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 74 | uint32_t uniqueID() const { return fUniqueID; } |
Robert Phillips | 07f675d | 2020-11-16 13:44:01 -0500 | [diff] [blame] | 75 | virtual int numTargets() const { return fTargets.count(); } |
Adlai Holler | 33d569e | 2020-06-16 14:30:08 -0400 | [diff] [blame] | 76 | const GrSurfaceProxyView& target(int i) const { return fTargets[i]; } |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 77 | |
| 78 | /* |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 79 | * Safely cast this GrRenderTask to a GrOpsTask (if possible). |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 80 | */ |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 81 | virtual GrOpsTask* asOpsTask() { return nullptr; } |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 82 | |
John Stiles | 1e0136e | 2020-08-12 18:44:00 -0400 | [diff] [blame] | 83 | #if GR_TEST_UTILS |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 84 | /* |
| 85 | * Dump out the GrRenderTask dependency DAG |
| 86 | */ |
Chris Dalton | c4b4735 | 2019-08-23 10:10:36 -0600 | [diff] [blame] | 87 | virtual void dump(bool printDependencies) const; |
Robert Phillips | 44e2c5f | 2020-04-14 13:00:04 -0400 | [diff] [blame] | 88 | virtual const char* name() const = 0; |
John Stiles | 1e0136e | 2020-08-12 18:44:00 -0400 | [diff] [blame] | 89 | #endif |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 90 | |
John Stiles | 1e0136e | 2020-08-12 18:44:00 -0400 | [diff] [blame] | 91 | #ifdef SK_DEBUG |
Chris Dalton | c4b4735 | 2019-08-23 10:10:36 -0600 | [diff] [blame] | 92 | virtual int numClips() const { return 0; } |
| 93 | |
Michael Ludwig | fcdd061 | 2019-11-25 08:34:31 -0500 | [diff] [blame] | 94 | virtual void visitProxies_debugOnly(const GrOp::VisitProxyFunc&) const = 0; |
Chris Dalton | c4b4735 | 2019-08-23 10:10:36 -0600 | [diff] [blame] | 95 | |
Michael Ludwig | fcdd061 | 2019-11-25 08:34:31 -0500 | [diff] [blame] | 96 | void visitTargetAndSrcProxies_debugOnly(const GrOp::VisitProxyFunc& fn) const { |
Chris Dalton | c4b4735 | 2019-08-23 10:10:36 -0600 | [diff] [blame] | 97 | this->visitProxies_debugOnly(fn); |
Robert Phillips | 07f675d | 2020-11-16 13:44:01 -0500 | [diff] [blame] | 98 | for (const GrSurfaceProxyView& target : fTargets) { |
| 99 | fn(target.proxy(), GrMipmapped::kNo); |
Chris Dalton | e2a903e | 2019-09-18 13:41:50 -0600 | [diff] [blame] | 100 | } |
Chris Dalton | c4b4735 | 2019-08-23 10:10:36 -0600 | [diff] [blame] | 101 | } |
| 102 | #endif |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 103 | |
Robert Phillips | 07f675d | 2020-11-16 13:44:01 -0500 | [diff] [blame] | 104 | bool isUsed(GrSurfaceProxy* proxy) const { |
| 105 | for (const GrSurfaceProxyView& target : fTargets) { |
| 106 | if (target.proxy() == proxy) { |
| 107 | return true; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | return this->onIsUsed(proxy); |
| 112 | } |
| 113 | |
Robert Phillips | 9ef7e20 | 2020-11-17 13:11:09 -0500 | [diff] [blame^] | 114 | // Drops any pending operations that reference proxies that are not instantiated. |
| 115 | // NOTE: Derived classes don't need to check targets. That is handled when the |
| 116 | // drawingManager calls isInstantiated. |
| 117 | virtual void handleInternalAllocationFailure() = 0; |
| 118 | |
| 119 | // Feed proxy usage intervals to the GrResourceAllocator class |
| 120 | virtual void gatherProxyIntervals(GrResourceAllocator*) const = 0; |
| 121 | |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 122 | // In addition to just the GrSurface being allocated, has the stencil buffer been allocated (if |
| 123 | // it is required)? |
| 124 | bool isInstantiated() const; |
| 125 | |
Robert Phillips | 9ef7e20 | 2020-11-17 13:11:09 -0500 | [diff] [blame^] | 126 | protected: |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 127 | SkDEBUGCODE(bool deferredProxiesAreInstantiated() const;) |
| 128 | |
Adlai Holler | 33d569e | 2020-06-16 14:30:08 -0400 | [diff] [blame] | 129 | // Add a target surface proxy to the list of targets for this task. |
| 130 | // This also informs the drawing manager to update the lastRenderTask association. |
| 131 | void addTarget(GrDrawingManager*, GrSurfaceProxyView); |
| 132 | |
Chris Dalton | aa3cbb8 | 2019-08-21 00:01:21 -0600 | [diff] [blame] | 133 | enum class ExpectedOutcome : bool { |
| 134 | kTargetUnchanged, |
| 135 | kTargetDirty, |
| 136 | }; |
| 137 | |
Chris Dalton | 16a33c6 | 2019-09-24 22:19:17 -0600 | [diff] [blame] | 138 | // Performs any work to finalize this renderTask prior to execution. If returning |
| 139 | // ExpectedOutcome::kTargetDiry, the caller is also responsible to fill out the area it will |
| 140 | // modify in targetUpdateBounds. |
| 141 | // |
| 142 | // targetUpdateBounds must not extend beyond the proxy bounds. |
| 143 | virtual ExpectedOutcome onMakeClosed(const GrCaps&, SkIRect* targetUpdateBounds) = 0; |
Chris Dalton | aa3cbb8 | 2019-08-21 00:01:21 -0600 | [diff] [blame] | 144 | |
Adlai Holler | 33d569e | 2020-06-16 14:30:08 -0400 | [diff] [blame] | 145 | SkSTArray<1, GrSurfaceProxyView> fTargets; |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 146 | |
| 147 | // List of texture proxies whose contents are being prepared on a worker thread |
| 148 | // TODO: this list exists so we can fire off the proper upload when an renderTask begins |
| 149 | // executing. Can this be replaced? |
| 150 | SkTArray<GrTextureProxy*, true> fDeferredProxies; |
| 151 | |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 152 | enum Flags { |
Adlai Holler | d71b7b0 | 2020-06-08 15:55:00 -0400 | [diff] [blame] | 153 | kClosed_Flag = 0x01, //!< This task can't accept any more dependencies. |
| 154 | kDisowned_Flag = 0x02, //!< This task is disowned by its creating GrDrawingManager. |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 155 | |
Adlai Holler | d71b7b0 | 2020-06-08 15:55:00 -0400 | [diff] [blame] | 156 | kWasOutput_Flag = 0x04, //!< Flag for topological sorting |
| 157 | kTempMark_Flag = 0x08, //!< Flag for topological sorting |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 158 | }; |
| 159 | |
| 160 | void setFlag(uint32_t flag) { |
| 161 | fFlags |= flag; |
| 162 | } |
| 163 | |
| 164 | void resetFlag(uint32_t flag) { |
| 165 | fFlags &= ~flag; |
| 166 | } |
| 167 | |
| 168 | bool isSetFlag(uint32_t flag) const { |
| 169 | return SkToBool(fFlags & flag); |
| 170 | } |
| 171 | |
Robert Phillips | 9ef7e20 | 2020-11-17 13:11:09 -0500 | [diff] [blame^] | 172 | private: |
| 173 | // for TopoSortTraits, fTextureResolveTask, closeThoseWhoDependOnMe, addDependency |
| 174 | friend class GrDrawingManager; |
| 175 | |
| 176 | // Derived classes can override to indicate usage of proxies _other than target proxies_. |
| 177 | // GrRenderTask itself will handle checking the target proxies. |
| 178 | virtual bool onIsUsed(GrSurfaceProxy*) const = 0; |
| 179 | |
| 180 | void addDependency(GrRenderTask* dependedOn); |
| 181 | void addDependent(GrRenderTask* dependent); |
| 182 | SkDEBUGCODE(bool isDependedent(const GrRenderTask* dependent) const;) |
| 183 | SkDEBUGCODE(void validate() const;) |
| 184 | void closeThoseWhoDependOnMe(const GrCaps&); |
| 185 | |
| 186 | static uint32_t CreateUniqueID(); |
| 187 | |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 188 | struct TopoSortTraits { |
| 189 | static void Output(GrRenderTask* renderTask, int /* index */) { |
| 190 | renderTask->setFlag(kWasOutput_Flag); |
| 191 | } |
| 192 | static bool WasOutput(const GrRenderTask* renderTask) { |
| 193 | return renderTask->isSetFlag(kWasOutput_Flag); |
| 194 | } |
| 195 | static void SetTempMark(GrRenderTask* renderTask) { |
| 196 | renderTask->setFlag(kTempMark_Flag); |
| 197 | } |
| 198 | static void ResetTempMark(GrRenderTask* renderTask) { |
| 199 | renderTask->resetFlag(kTempMark_Flag); |
| 200 | } |
| 201 | static bool IsTempMarked(const GrRenderTask* renderTask) { |
| 202 | return renderTask->isSetFlag(kTempMark_Flag); |
| 203 | } |
| 204 | static int NumDependencies(const GrRenderTask* renderTask) { |
| 205 | return renderTask->fDependencies.count(); |
| 206 | } |
| 207 | static GrRenderTask* Dependency(GrRenderTask* renderTask, int index) { |
| 208 | return renderTask->fDependencies[index]; |
| 209 | } |
| 210 | }; |
| 211 | |
Robert Phillips | 07f675d | 2020-11-16 13:44:01 -0500 | [diff] [blame] | 212 | |
| 213 | virtual void onPrePrepare(GrRecordingContext*) {} // Only the GrOpsTask currently overrides this |
| 214 | virtual void onPrepare(GrOpFlushState*) {} // Only GrOpsTask and GrDDLTask override this virtual |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 215 | virtual bool onExecute(GrOpFlushState* flushState) = 0; |
| 216 | |
| 217 | const uint32_t fUniqueID; |
| 218 | uint32_t fFlags; |
| 219 | |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 220 | // 'this' GrRenderTask relies on the output of the GrRenderTasks in 'fDependencies' |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 221 | SkSTArray<1, GrRenderTask*, true> fDependencies; |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 222 | // 'this' GrRenderTask's output is relied on by the GrRenderTasks in 'fDependents' |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 223 | SkSTArray<1, GrRenderTask*, true> fDependents; |
Chris Dalton | e2a903e | 2019-09-18 13:41:50 -0600 | [diff] [blame] | 224 | |
| 225 | // For performance reasons, we should perform texture resolves back-to-back as much as possible. |
| 226 | // (http://skbug.com/9406). To accomplish this, we make and reuse one single resolve task for |
| 227 | // each render task, then add it as a dependency during makeClosed(). |
| 228 | GrTextureResolveRenderTask* fTextureResolveTask = nullptr; |
Adlai Holler | 96ead54 | 2020-06-26 08:50:14 -0400 | [diff] [blame] | 229 | |
| 230 | SkDEBUGCODE(GrDrawingManager *fDrawingMgr = nullptr;) |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 231 | }; |
| 232 | |
| 233 | #endif |