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" |
| 14 | #include "src/gpu/GrTextureProxy.h" |
Chris Dalton | 0875512 | 2019-08-05 16:13:47 -0600 | [diff] [blame] | 15 | #include "src/gpu/GrTextureResolveManager.h" |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 16 | |
| 17 | class GrOpFlushState; |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 18 | class GrOpsTask; |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 19 | class GrResourceAllocator; |
Chris Dalton | e2a903e | 2019-09-18 13:41:50 -0600 | [diff] [blame] | 20 | class GrTextureResolveRenderTask; |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 21 | |
| 22 | // This class abstracts a task that targets a single GrSurfaceProxy, participates in the |
| 23 | // 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] | 24 | // 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] | 25 | class GrRenderTask : public SkRefCnt { |
| 26 | public: |
| 27 | GrRenderTask(sk_sp<GrSurfaceProxy> target); |
| 28 | ~GrRenderTask() override; |
| 29 | |
Chris Dalton | aa3cbb8 | 2019-08-21 00:01:21 -0600 | [diff] [blame] | 30 | void makeClosed(const GrCaps&); |
| 31 | |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 32 | // These two methods are only invoked at flush time |
| 33 | void prepare(GrOpFlushState* flushState); |
| 34 | bool execute(GrOpFlushState* flushState) { return this->onExecute(flushState); } |
| 35 | |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 36 | // Called when this class will survive a flush and needs to truncate its ops and start over. |
| 37 | // TODO: ultimately it should be invalid for an op list to survive a flush. |
| 38 | // https://bugs.chromium.org/p/skia/issues/detail?id=7111 |
| 39 | virtual void endFlush() {} |
| 40 | |
| 41 | bool isClosed() const { return this->isSetFlag(kClosed_Flag); } |
| 42 | |
| 43 | /* |
| 44 | * Notify this GrRenderTask that it relies on the contents of 'dependedOn' |
| 45 | */ |
Chris Dalton | 0875512 | 2019-08-05 16:13:47 -0600 | [diff] [blame] | 46 | void addDependency(GrSurfaceProxy* dependedOn, GrMipMapped, GrTextureResolveManager, |
| 47 | const GrCaps& caps); |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 48 | |
| 49 | /* |
Greg Daniel | c30f1a9 | 2019-09-06 15:28:58 -0400 | [diff] [blame] | 50 | * Notify this GrRenderTask that it relies on the contents of all GrRenderTasks which otherTask |
| 51 | * depends on. |
| 52 | */ |
| 53 | void addDependenciesFromOtherTask(GrRenderTask* otherTask); |
| 54 | |
| 55 | /* |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 56 | * Does this renderTask depend on 'dependedOn'? |
| 57 | */ |
| 58 | bool dependsOn(const GrRenderTask* dependedOn) const; |
| 59 | |
| 60 | uint32_t uniqueID() const { return fUniqueID; } |
| 61 | |
| 62 | /* |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 63 | * Safely cast this GrRenderTask to a GrOpsTask (if possible). |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 64 | */ |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 65 | virtual GrOpsTask* asOpsTask() { return nullptr; } |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 66 | |
Chris Dalton | c4b4735 | 2019-08-23 10:10:36 -0600 | [diff] [blame] | 67 | #ifdef SK_DEBUG |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 68 | /* |
| 69 | * Dump out the GrRenderTask dependency DAG |
| 70 | */ |
Chris Dalton | c4b4735 | 2019-08-23 10:10:36 -0600 | [diff] [blame] | 71 | virtual void dump(bool printDependencies) const; |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 72 | |
Chris Dalton | c4b4735 | 2019-08-23 10:10:36 -0600 | [diff] [blame] | 73 | virtual int numClips() const { return 0; } |
| 74 | |
Greg Daniel | dcf9ca1 | 2019-08-27 14:30:21 -0400 | [diff] [blame] | 75 | using VisitSurfaceProxyFunc = std::function<void(GrSurfaceProxy*, GrMipMapped)>; |
Chris Dalton | c4b4735 | 2019-08-23 10:10:36 -0600 | [diff] [blame] | 76 | |
Greg Daniel | dcf9ca1 | 2019-08-27 14:30:21 -0400 | [diff] [blame] | 77 | virtual void visitProxies_debugOnly(const VisitSurfaceProxyFunc&) const = 0; |
| 78 | |
| 79 | void visitTargetAndSrcProxies_debugOnly(const VisitSurfaceProxyFunc& fn) const { |
Chris Dalton | c4b4735 | 2019-08-23 10:10:36 -0600 | [diff] [blame] | 80 | this->visitProxies_debugOnly(fn); |
Chris Dalton | e2a903e | 2019-09-18 13:41:50 -0600 | [diff] [blame] | 81 | if (fTarget) { |
| 82 | fn(fTarget.get(), GrMipMapped::kNo); |
| 83 | } |
Chris Dalton | c4b4735 | 2019-08-23 10:10:36 -0600 | [diff] [blame] | 84 | } |
| 85 | #endif |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 86 | |
| 87 | protected: |
| 88 | // In addition to just the GrSurface being allocated, has the stencil buffer been allocated (if |
| 89 | // it is required)? |
| 90 | bool isInstantiated() const; |
| 91 | |
| 92 | SkDEBUGCODE(bool deferredProxiesAreInstantiated() const;) |
| 93 | |
Chris Dalton | aa3cbb8 | 2019-08-21 00:01:21 -0600 | [diff] [blame] | 94 | enum class ExpectedOutcome : bool { |
| 95 | kTargetUnchanged, |
| 96 | kTargetDirty, |
| 97 | }; |
| 98 | |
| 99 | virtual ExpectedOutcome onMakeClosed(const GrCaps&) = 0; |
| 100 | |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 101 | sk_sp<GrSurfaceProxy> fTarget; |
| 102 | |
| 103 | // List of texture proxies whose contents are being prepared on a worker thread |
| 104 | // TODO: this list exists so we can fire off the proper upload when an renderTask begins |
| 105 | // executing. Can this be replaced? |
| 106 | SkTArray<GrTextureProxy*, true> fDeferredProxies; |
| 107 | |
| 108 | private: |
| 109 | // for resetFlag, TopoSortTraits, gatherProxyIntervals, handleInternalAllocationFailure |
| 110 | friend class GrDrawingManager; |
| 111 | |
| 112 | // Drops any pending operations that reference proxies that are not instantiated. |
| 113 | // NOTE: Derived classes don't need to check fTarget. That is handled when the drawingManager |
| 114 | // calls isInstantiated. |
| 115 | virtual void handleInternalAllocationFailure() = 0; |
| 116 | |
| 117 | virtual bool onIsUsed(GrSurfaceProxy*) const = 0; |
| 118 | |
| 119 | bool isUsed(GrSurfaceProxy* proxy) const { |
| 120 | if (proxy == fTarget.get()) { |
| 121 | return true; |
| 122 | } |
| 123 | |
| 124 | return this->onIsUsed(proxy); |
| 125 | } |
| 126 | |
| 127 | void addDependency(GrRenderTask* dependedOn); |
| 128 | void addDependent(GrRenderTask* dependent); |
| 129 | SkDEBUGCODE(bool isDependedent(const GrRenderTask* dependent) const;) |
| 130 | SkDEBUGCODE(void validate() const;) |
| 131 | void closeThoseWhoDependOnMe(const GrCaps&); |
| 132 | |
| 133 | // Feed proxy usage intervals to the GrResourceAllocator class |
| 134 | virtual void gatherProxyIntervals(GrResourceAllocator*) const = 0; |
| 135 | |
| 136 | static uint32_t CreateUniqueID(); |
| 137 | |
| 138 | enum Flags { |
| 139 | kClosed_Flag = 0x01, //!< This GrRenderTask can't accept any more dependencies. |
| 140 | |
| 141 | kWasOutput_Flag = 0x02, //!< Flag for topological sorting |
| 142 | kTempMark_Flag = 0x04, //!< Flag for topological sorting |
| 143 | }; |
| 144 | |
| 145 | void setFlag(uint32_t flag) { |
| 146 | fFlags |= flag; |
| 147 | } |
| 148 | |
| 149 | void resetFlag(uint32_t flag) { |
| 150 | fFlags &= ~flag; |
| 151 | } |
| 152 | |
| 153 | bool isSetFlag(uint32_t flag) const { |
| 154 | return SkToBool(fFlags & flag); |
| 155 | } |
| 156 | |
| 157 | struct TopoSortTraits { |
| 158 | static void Output(GrRenderTask* renderTask, int /* index */) { |
| 159 | renderTask->setFlag(kWasOutput_Flag); |
| 160 | } |
| 161 | static bool WasOutput(const GrRenderTask* renderTask) { |
| 162 | return renderTask->isSetFlag(kWasOutput_Flag); |
| 163 | } |
| 164 | static void SetTempMark(GrRenderTask* renderTask) { |
| 165 | renderTask->setFlag(kTempMark_Flag); |
| 166 | } |
| 167 | static void ResetTempMark(GrRenderTask* renderTask) { |
| 168 | renderTask->resetFlag(kTempMark_Flag); |
| 169 | } |
| 170 | static bool IsTempMarked(const GrRenderTask* renderTask) { |
| 171 | return renderTask->isSetFlag(kTempMark_Flag); |
| 172 | } |
| 173 | static int NumDependencies(const GrRenderTask* renderTask) { |
| 174 | return renderTask->fDependencies.count(); |
| 175 | } |
| 176 | static GrRenderTask* Dependency(GrRenderTask* renderTask, int index) { |
| 177 | return renderTask->fDependencies[index]; |
| 178 | } |
| 179 | }; |
| 180 | |
| 181 | virtual void onPrepare(GrOpFlushState* flushState) = 0; |
| 182 | virtual bool onExecute(GrOpFlushState* flushState) = 0; |
| 183 | |
| 184 | const uint32_t fUniqueID; |
| 185 | uint32_t fFlags; |
| 186 | |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 187 | // 'this' GrRenderTask relies on the output of the GrRenderTasks in 'fDependencies' |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 188 | SkSTArray<1, GrRenderTask*, true> fDependencies; |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 189 | // 'this' GrRenderTask's output is relied on by the GrRenderTasks in 'fDependents' |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 190 | SkSTArray<1, GrRenderTask*, true> fDependents; |
Chris Dalton | e2a903e | 2019-09-18 13:41:50 -0600 | [diff] [blame] | 191 | |
| 192 | // For performance reasons, we should perform texture resolves back-to-back as much as possible. |
| 193 | // (http://skbug.com/9406). To accomplish this, we make and reuse one single resolve task for |
| 194 | // each render task, then add it as a dependency during makeClosed(). |
| 195 | GrTextureResolveRenderTask* fTextureResolveTask = nullptr; |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 196 | }; |
| 197 | |
| 198 | #endif |