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