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/SkTArray.h" 13 #include "src/core/SkTInternalLList.h" 14 #include "src/gpu/GrSurfaceProxyView.h" 15 #include "src/gpu/GrTextureProxy.h" 16 #include "src/gpu/GrTextureResolveManager.h" 17 #include "src/gpu/ops/GrOp.h" 18 19 class GrMockRenderTask; 20 class GrOpFlushState; 21 class GrOpsTask; 22 class GrResourceAllocator; 23 class GrTextureResolveRenderTask; 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 27 // contents. (e.g., an opsTask that executes a command buffer, a task to regenerate mipmaps, etc.) 28 class GrRenderTask : public SkRefCnt { 29 public: 30 GrRenderTask(); 31 SkDEBUGCODE(~GrRenderTask() override); 32 33 void makeClosed(const GrCaps&); 34 prePrepare(GrRecordingContext * context)35 void prePrepare(GrRecordingContext* context) { this->onPrePrepare(context); } 36 37 // These two methods are only invoked at flush time 38 void prepare(GrOpFlushState* flushState); execute(GrOpFlushState * flushState)39 bool execute(GrOpFlushState* flushState) { return this->onExecute(flushState); } 40 requiresExplicitCleanup()41 virtual bool requiresExplicitCleanup() const { return false; } 42 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 endFlush(GrDrawingManager *)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*); 52 isClosed()53 bool isClosed() const { return this->isSetFlag(kClosed_Flag); } 54 55 /** 56 * Make this task skippable. This must be used purely for optimization purposes 57 * at this point as not all tasks will actually skip their work. It would be better if we could 58 * detect tasks that can be skipped automatically. We'd need to support minimal flushes (i.e., 59 * only flush that which is required for SkSurfaces/SkImages) and the ability to detect 60 * "orphaned tasks" and clean them out from the DAG so they don't indefinitely accumulate. 61 * Finally, we'd probably have to track whether a proxy's backing store was imported or ever 62 * exported to the client in case the client is doing direct reads outside of Skia and thus 63 * may require tasks targeting the proxy to execute even if our DAG contains no reads. 64 */ 65 void makeSkippable(); 66 isSkippable()67 bool isSkippable() const { return this->isSetFlag(kSkippable_Flag); } 68 69 /* 70 * Notify this GrRenderTask that it relies on the contents of 'dependedOn' 71 */ 72 void addDependency(GrDrawingManager*, GrSurfaceProxy* dependedOn, GrMipmapped, 73 GrTextureResolveManager, const GrCaps& caps); 74 75 /* 76 * Notify this GrRenderTask that it relies on the contents of all GrRenderTasks which otherTask 77 * depends on. 78 */ 79 void addDependenciesFromOtherTask(GrRenderTask* otherTask); 80 dependencies()81 SkSpan<GrRenderTask*> dependencies() { return SkMakeSpan(fDependencies); } dependents()82 SkSpan<GrRenderTask*> dependents() { return SkMakeSpan(fDependents); } 83 84 void replaceDependency(const GrRenderTask* toReplace, GrRenderTask* replaceWith); 85 void replaceDependent(const GrRenderTask* toReplace, GrRenderTask* replaceWith); 86 87 88 /* 89 * Does this renderTask depend on 'dependedOn'? 90 */ 91 bool dependsOn(const GrRenderTask* dependedOn) const; 92 gatherIDs(SkSTArray<8,uint32_t,true> * idArray)93 virtual void gatherIDs(SkSTArray<8, uint32_t, true>* idArray) const { 94 idArray->push_back(fUniqueID); 95 } uniqueID()96 uint32_t uniqueID() const { return fUniqueID; } numTargets()97 virtual int numTargets() const { return fTargets.count(); } target(int i)98 GrSurfaceProxy* target(int i) const { return fTargets[i].get(); } 99 100 /* 101 * Safely cast this GrRenderTask to a GrOpsTask (if possible). 102 */ asOpsTask()103 virtual GrOpsTask* asOpsTask() { return nullptr; } 104 105 #if GR_TEST_UTILS 106 /* 107 * Dump out the GrRenderTask dependency DAG 108 */ 109 virtual void dump(const SkString& label, 110 SkString indent, 111 bool printDependencies, 112 bool close) const; 113 virtual const char* name() const = 0; 114 #endif 115 116 #ifdef SK_DEBUG numClips()117 virtual int numClips() const { return 0; } 118 119 virtual void visitProxies_debugOnly(const GrOp::VisitProxyFunc&) const = 0; 120 visitTargetAndSrcProxies_debugOnly(const GrOp::VisitProxyFunc & fn)121 void visitTargetAndSrcProxies_debugOnly(const GrOp::VisitProxyFunc& fn) const { 122 this->visitProxies_debugOnly(fn); 123 for (const sk_sp<GrSurfaceProxy>& target : fTargets) { 124 fn(target.get(), GrMipmapped::kNo); 125 } 126 } 127 #endif 128 isUsed(GrSurfaceProxy * proxy)129 bool isUsed(GrSurfaceProxy* proxy) const { 130 for (const sk_sp<GrSurfaceProxy>& target : fTargets) { 131 if (target.get() == proxy) { 132 return true; 133 } 134 } 135 136 return this->onIsUsed(proxy); 137 } 138 139 // Feed proxy usage intervals to the GrResourceAllocator class 140 virtual void gatherProxyIntervals(GrResourceAllocator*) const = 0; 141 142 // In addition to just the GrSurface being allocated, has the stencil buffer been allocated (if 143 // it is required)? 144 bool isInstantiated() const; 145 146 // Used by GrRenderTaskCluster. 147 SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrRenderTask); 148 149 protected: 150 SkDEBUGCODE(bool deferredProxiesAreInstantiated() const;) 151 152 // Add a target surface proxy to the list of targets for this task. 153 // This also informs the drawing manager to update the lastRenderTask association. 154 void addTarget(GrDrawingManager*, sk_sp<GrSurfaceProxy>); 155 156 // Helper that adds the proxy owned by a view. addTarget(GrDrawingManager * dm,const GrSurfaceProxyView & view)157 void addTarget(GrDrawingManager* dm, const GrSurfaceProxyView& view) { 158 this->addTarget(dm, view.refProxy()); 159 } 160 161 enum class ExpectedOutcome : bool { 162 kTargetUnchanged, 163 kTargetDirty, 164 }; 165 166 // Performs any work to finalize this renderTask prior to execution. If returning 167 // ExpectedOutcome::kTargetDiry, the caller is also responsible to fill out the area it will 168 // modify in targetUpdateBounds. 169 // 170 // targetUpdateBounds must not extend beyond the proxy bounds. 171 virtual ExpectedOutcome onMakeClosed(const GrCaps&, SkIRect* targetUpdateBounds) = 0; 172 173 SkSTArray<1, sk_sp<GrSurfaceProxy>> fTargets; 174 175 // List of texture proxies whose contents are being prepared on a worker thread 176 // TODO: this list exists so we can fire off the proper upload when an renderTask begins 177 // executing. Can this be replaced? 178 SkTArray<GrTextureProxy*, true> fDeferredProxies; 179 180 enum Flags { 181 kClosed_Flag = 0x01, //!< This task can't accept any more dependencies. 182 kDisowned_Flag = 0x02, //!< This task is disowned by its creating GrDrawingManager. 183 kSkippable_Flag = 0x04, //!< This task is skippable. 184 185 kWasOutput_Flag = 0x08, //!< Flag for topological sorting 186 kTempMark_Flag = 0x10, //!< Flag for topological sorting 187 }; 188 setFlag(uint32_t flag)189 void setFlag(uint32_t flag) { 190 fFlags |= flag; 191 } 192 resetFlag(uint32_t flag)193 void resetFlag(uint32_t flag) { 194 fFlags &= ~flag; 195 } 196 isSetFlag(uint32_t flag)197 bool isSetFlag(uint32_t flag) const { 198 return SkToBool(fFlags & flag); 199 } 200 setIndex(uint32_t index)201 void setIndex(uint32_t index) { 202 SkASSERT(!this->isSetFlag(kWasOutput_Flag)); 203 SkASSERT(index < (1 << 27)); 204 fFlags |= index << 5; 205 } 206 getIndex()207 uint32_t getIndex() const { 208 SkASSERT(this->isSetFlag(kWasOutput_Flag)); 209 return fFlags >> 5; 210 } 211 212 private: 213 // for TopoSortTraits, fTextureResolveTask, closeThoseWhoDependOnMe, addDependency 214 friend class GrDrawingManager; 215 friend class GrMockRenderTask; 216 217 // Derived classes can override to indicate usage of proxies _other than target proxies_. 218 // GrRenderTask itself will handle checking the target proxies. 219 virtual bool onIsUsed(GrSurfaceProxy*) const = 0; 220 221 void addDependency(GrRenderTask* dependedOn); 222 void addDependent(GrRenderTask* dependent); 223 SkDEBUGCODE(bool isDependent(const GrRenderTask* dependent) const;) 224 SkDEBUGCODE(void validate() const;) 225 void closeThoseWhoDependOnMe(const GrCaps&); 226 227 static uint32_t CreateUniqueID(); 228 229 struct TopoSortTraits { GetIndexTopoSortTraits230 static uint32_t GetIndex(GrRenderTask* renderTask) { 231 return renderTask->getIndex(); 232 } OutputTopoSortTraits233 static void Output(GrRenderTask* renderTask, uint32_t index) { 234 renderTask->setIndex(index); 235 renderTask->setFlag(kWasOutput_Flag); 236 } WasOutputTopoSortTraits237 static bool WasOutput(const GrRenderTask* renderTask) { 238 return renderTask->isSetFlag(kWasOutput_Flag); 239 } SetTempMarkTopoSortTraits240 static void SetTempMark(GrRenderTask* renderTask) { 241 renderTask->setFlag(kTempMark_Flag); 242 } ResetTempMarkTopoSortTraits243 static void ResetTempMark(GrRenderTask* renderTask) { 244 renderTask->resetFlag(kTempMark_Flag); 245 } IsTempMarkedTopoSortTraits246 static bool IsTempMarked(const GrRenderTask* renderTask) { 247 return renderTask->isSetFlag(kTempMark_Flag); 248 } NumDependenciesTopoSortTraits249 static int NumDependencies(const GrRenderTask* renderTask) { 250 return renderTask->fDependencies.count(); 251 } DependencyTopoSortTraits252 static GrRenderTask* Dependency(GrRenderTask* renderTask, int index) { 253 return renderTask->fDependencies[index]; 254 } 255 }; 256 onMakeSkippable()257 virtual void onMakeSkippable() {} onPrePrepare(GrRecordingContext *)258 virtual void onPrePrepare(GrRecordingContext*) {} // Only GrOpsTask currently overrides this onPrepare(GrOpFlushState *)259 virtual void onPrepare(GrOpFlushState*) {} // GrOpsTask and GrDDLTask override this 260 virtual bool onExecute(GrOpFlushState* flushState) = 0; 261 262 const uint32_t fUniqueID; 263 uint32_t fFlags; 264 265 // 'this' GrRenderTask relies on the output of the GrRenderTasks in 'fDependencies' 266 SkSTArray<1, GrRenderTask*, true> fDependencies; 267 // 'this' GrRenderTask's output is relied on by the GrRenderTasks in 'fDependents' 268 SkSTArray<1, GrRenderTask*, true> fDependents; 269 270 // For performance reasons, we should perform texture resolves back-to-back as much as possible. 271 // (http://skbug.com/9406). To accomplish this, we make and reuse one single resolve task for 272 // each render task, then add it as a dependency during makeClosed(). 273 GrTextureResolveRenderTask* fTextureResolveTask = nullptr; 274 275 SkDEBUGCODE(GrDrawingManager *fDrawingMgr = nullptr;) 276 }; 277 278 #endif 279