blob: 701e7b72d1f00f8ee3b481da1b0819fb84e96e4c [file] [log] [blame]
Chris Dalton6b498102019-08-01 14:14:52 -06001/*
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 Salomon07bc9a22020-12-02 13:37:16 -050012#include "include/private/SkTArray.h"
Adlai Holler78036082021-01-07 11:41:33 -050013#include "src/core/SkTInternalLList.h"
Greg Daniel16f5c652019-10-29 11:26:01 -040014#include "src/gpu/GrSurfaceProxyView.h"
Chris Dalton6b498102019-08-01 14:14:52 -060015#include "src/gpu/GrTextureProxy.h"
Chris Dalton08755122019-08-05 16:13:47 -060016#include "src/gpu/GrTextureResolveManager.h"
Herb Derby082232b2020-06-10 15:08:18 -040017#include "src/gpu/ops/GrOp.h"
Chris Dalton6b498102019-08-01 14:14:52 -060018
19class GrOpFlushState;
Greg Danielf41b2bd2019-08-22 16:19:24 -040020class GrOpsTask;
Chris Dalton6b498102019-08-01 14:14:52 -060021class GrResourceAllocator;
Chris Daltone2a903e2019-09-18 13:41:50 -060022class GrTextureResolveRenderTask;
Chris Dalton6b498102019-08-01 14:14:52 -060023
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 Danielf41b2bd2019-08-22 16:19:24 -040026// contents. (e.g., an opsTask that executes a command buffer, a task to regenerate mipmaps, etc.)
Chris Dalton6b498102019-08-01 14:14:52 -060027class GrRenderTask : public SkRefCnt {
28public:
Greg Daniel16f5c652019-10-29 11:26:01 -040029 GrRenderTask();
Adlai Holler33d569e2020-06-16 14:30:08 -040030 SkDEBUGCODE(~GrRenderTask() override);
Chris Dalton6b498102019-08-01 14:14:52 -060031
Chris Daltonaa3cbb82019-08-21 00:01:21 -060032 void makeClosed(const GrCaps&);
33
Robert Phillips29f38542019-10-16 09:20:25 -040034 void prePrepare(GrRecordingContext* context) { this->onPrePrepare(context); }
Robert Phillips7327c9d2019-10-08 16:32:56 -040035
Chris Dalton6b498102019-08-01 14:14:52 -060036 // 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 Phillips07f675d2020-11-16 13:44:01 -050040 virtual bool requiresExplicitCleanup() const { return false; }
41
Chris Dalton6b498102019-08-01 14:14:52 -060042 // 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 Hollerd71b7b02020-06-08 15:55:00 -040045 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 Dalton6b498102019-08-01 14:14:52 -060051
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 Salomon7e67dca2020-07-21 09:27:25 -040057 void addDependency(GrDrawingManager*, GrSurfaceProxy* dependedOn, GrMipmapped,
Adlai Hollerd71b7b02020-06-08 15:55:00 -040058 GrTextureResolveManager, const GrCaps& caps);
Chris Dalton6b498102019-08-01 14:14:52 -060059
60 /*
Greg Danielc30f1a92019-09-06 15:28:58 -040061 * 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 Dalton6b498102019-08-01 14:14:52 -060067 * Does this renderTask depend on 'dependedOn'?
68 */
69 bool dependsOn(const GrRenderTask* dependedOn) const;
70
Robert Phillips07f675d2020-11-16 13:44:01 -050071 virtual void gatherIDs(SkSTArray<8, uint32_t, true>* idArray) const {
72 idArray->push_back(fUniqueID);
73 }
Chris Dalton6b498102019-08-01 14:14:52 -060074 uint32_t uniqueID() const { return fUniqueID; }
Robert Phillips07f675d2020-11-16 13:44:01 -050075 virtual int numTargets() const { return fTargets.count(); }
Adlai Holler33d569e2020-06-16 14:30:08 -040076 const GrSurfaceProxyView& target(int i) const { return fTargets[i]; }
Chris Dalton6b498102019-08-01 14:14:52 -060077
78 /*
Greg Danielf41b2bd2019-08-22 16:19:24 -040079 * Safely cast this GrRenderTask to a GrOpsTask (if possible).
Chris Dalton6b498102019-08-01 14:14:52 -060080 */
Greg Danielf41b2bd2019-08-22 16:19:24 -040081 virtual GrOpsTask* asOpsTask() { return nullptr; }
Chris Dalton6b498102019-08-01 14:14:52 -060082
John Stiles1e0136e2020-08-12 18:44:00 -040083#if GR_TEST_UTILS
Chris Dalton6b498102019-08-01 14:14:52 -060084 /*
85 * Dump out the GrRenderTask dependency DAG
86 */
Robert Phillips047d5bb2021-01-08 13:39:19 -050087 virtual void dump(const SkString& label,
88 SkString indent,
89 bool printDependencies,
90 bool close) const;
Robert Phillips44e2c5f2020-04-14 13:00:04 -040091 virtual const char* name() const = 0;
John Stiles1e0136e2020-08-12 18:44:00 -040092#endif
Chris Dalton6b498102019-08-01 14:14:52 -060093
John Stiles1e0136e2020-08-12 18:44:00 -040094#ifdef SK_DEBUG
Chris Daltonc4b47352019-08-23 10:10:36 -060095 virtual int numClips() const { return 0; }
96
Michael Ludwigfcdd0612019-11-25 08:34:31 -050097 virtual void visitProxies_debugOnly(const GrOp::VisitProxyFunc&) const = 0;
Chris Daltonc4b47352019-08-23 10:10:36 -060098
Michael Ludwigfcdd0612019-11-25 08:34:31 -050099 void visitTargetAndSrcProxies_debugOnly(const GrOp::VisitProxyFunc& fn) const {
Chris Daltonc4b47352019-08-23 10:10:36 -0600100 this->visitProxies_debugOnly(fn);
Robert Phillips07f675d2020-11-16 13:44:01 -0500101 for (const GrSurfaceProxyView& target : fTargets) {
102 fn(target.proxy(), GrMipmapped::kNo);
Chris Daltone2a903e2019-09-18 13:41:50 -0600103 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600104 }
105#endif
Chris Dalton6b498102019-08-01 14:14:52 -0600106
Robert Phillips07f675d2020-11-16 13:44:01 -0500107 bool isUsed(GrSurfaceProxy* proxy) const {
108 for (const GrSurfaceProxyView& target : fTargets) {
109 if (target.proxy() == proxy) {
110 return true;
111 }
112 }
113
114 return this->onIsUsed(proxy);
115 }
116
Robert Phillips9ef7e202020-11-17 13:11:09 -0500117 // Drops any pending operations that reference proxies that are not instantiated.
118 // NOTE: Derived classes don't need to check targets. That is handled when the
119 // drawingManager calls isInstantiated.
120 virtual void handleInternalAllocationFailure() = 0;
121
122 // Feed proxy usage intervals to the GrResourceAllocator class
123 virtual void gatherProxyIntervals(GrResourceAllocator*) const = 0;
124
Chris Dalton6b498102019-08-01 14:14:52 -0600125 // In addition to just the GrSurface being allocated, has the stencil buffer been allocated (if
126 // it is required)?
127 bool isInstantiated() const;
128
Adlai Holler78036082021-01-07 11:41:33 -0500129 // Used by GrTCluster.
130 SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrRenderTask);
131
Robert Phillips9ef7e202020-11-17 13:11:09 -0500132protected:
Chris Dalton6b498102019-08-01 14:14:52 -0600133 SkDEBUGCODE(bool deferredProxiesAreInstantiated() const;)
134
Adlai Holler33d569e2020-06-16 14:30:08 -0400135 // Add a target surface proxy to the list of targets for this task.
136 // This also informs the drawing manager to update the lastRenderTask association.
137 void addTarget(GrDrawingManager*, GrSurfaceProxyView);
138
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600139 enum class ExpectedOutcome : bool {
140 kTargetUnchanged,
141 kTargetDirty,
142 };
143
Chris Dalton16a33c62019-09-24 22:19:17 -0600144 // Performs any work to finalize this renderTask prior to execution. If returning
145 // ExpectedOutcome::kTargetDiry, the caller is also responsible to fill out the area it will
146 // modify in targetUpdateBounds.
147 //
148 // targetUpdateBounds must not extend beyond the proxy bounds.
149 virtual ExpectedOutcome onMakeClosed(const GrCaps&, SkIRect* targetUpdateBounds) = 0;
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600150
Adlai Holler33d569e2020-06-16 14:30:08 -0400151 SkSTArray<1, GrSurfaceProxyView> fTargets;
Chris Dalton6b498102019-08-01 14:14:52 -0600152
153 // List of texture proxies whose contents are being prepared on a worker thread
154 // TODO: this list exists so we can fire off the proper upload when an renderTask begins
155 // executing. Can this be replaced?
156 SkTArray<GrTextureProxy*, true> fDeferredProxies;
157
Chris Dalton6b498102019-08-01 14:14:52 -0600158 enum Flags {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400159 kClosed_Flag = 0x01, //!< This task can't accept any more dependencies.
160 kDisowned_Flag = 0x02, //!< This task is disowned by its creating GrDrawingManager.
Chris Dalton6b498102019-08-01 14:14:52 -0600161
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400162 kWasOutput_Flag = 0x04, //!< Flag for topological sorting
163 kTempMark_Flag = 0x08, //!< Flag for topological sorting
Chris Dalton6b498102019-08-01 14:14:52 -0600164 };
165
166 void setFlag(uint32_t flag) {
167 fFlags |= flag;
168 }
169
170 void resetFlag(uint32_t flag) {
171 fFlags &= ~flag;
172 }
173
174 bool isSetFlag(uint32_t flag) const {
175 return SkToBool(fFlags & flag);
176 }
177
Robert Phillips4dbff752020-11-18 12:16:31 -0500178 void setIndex(uint32_t index) {
179 SkASSERT(!this->isSetFlag(kWasOutput_Flag));
180 SkASSERT(index < (1 << 28));
181 fFlags |= index << 4;
182 }
183
184 uint32_t getIndex() const {
185 SkASSERT(this->isSetFlag(kWasOutput_Flag));
186 return fFlags >> 4;
187 }
188
Robert Phillips9ef7e202020-11-17 13:11:09 -0500189private:
190 // for TopoSortTraits, fTextureResolveTask, closeThoseWhoDependOnMe, addDependency
191 friend class GrDrawingManager;
192
193 // Derived classes can override to indicate usage of proxies _other than target proxies_.
194 // GrRenderTask itself will handle checking the target proxies.
195 virtual bool onIsUsed(GrSurfaceProxy*) const = 0;
196
197 void addDependency(GrRenderTask* dependedOn);
198 void addDependent(GrRenderTask* dependent);
Adlai Holler78036082021-01-07 11:41:33 -0500199 SkDEBUGCODE(bool isDependent(const GrRenderTask* dependent) const;)
Robert Phillips9ef7e202020-11-17 13:11:09 -0500200 SkDEBUGCODE(void validate() const;)
201 void closeThoseWhoDependOnMe(const GrCaps&);
202
203 static uint32_t CreateUniqueID();
204
Chris Dalton6b498102019-08-01 14:14:52 -0600205 struct TopoSortTraits {
Robert Phillips4dbff752020-11-18 12:16:31 -0500206 static uint32_t GetIndex(GrRenderTask* renderTask) {
207 return renderTask->getIndex();
208 }
209 static void Output(GrRenderTask* renderTask, uint32_t index) {
210 renderTask->setIndex(index);
Chris Dalton6b498102019-08-01 14:14:52 -0600211 renderTask->setFlag(kWasOutput_Flag);
212 }
213 static bool WasOutput(const GrRenderTask* renderTask) {
214 return renderTask->isSetFlag(kWasOutput_Flag);
215 }
216 static void SetTempMark(GrRenderTask* renderTask) {
217 renderTask->setFlag(kTempMark_Flag);
218 }
219 static void ResetTempMark(GrRenderTask* renderTask) {
220 renderTask->resetFlag(kTempMark_Flag);
221 }
222 static bool IsTempMarked(const GrRenderTask* renderTask) {
223 return renderTask->isSetFlag(kTempMark_Flag);
224 }
225 static int NumDependencies(const GrRenderTask* renderTask) {
226 return renderTask->fDependencies.count();
227 }
228 static GrRenderTask* Dependency(GrRenderTask* renderTask, int index) {
229 return renderTask->fDependencies[index];
230 }
231 };
232
Adlai Holler78036082021-01-07 11:41:33 -0500233 struct ClusterTraits {
234 static int NumTargets(GrRenderTask* renderTask) {
235 return renderTask->numTargets();
236 }
237 static uint32_t GetTarget(GrRenderTask* renderTask, int index) {
238 return renderTask->target(index).proxy()->uniqueID().asUInt();
239 }
240 static int NumDependencies(const GrRenderTask* renderTask) {
241 return renderTask->fDependencies.count();
242 }
243 static GrRenderTask* Dependency(GrRenderTask* renderTask, int index) {
244 return renderTask->fDependencies[index];
245 }
246 static uint32_t GetID(GrRenderTask* renderTask) {
247 return renderTask->uniqueID();
248 }
249 };
Robert Phillips07f675d2020-11-16 13:44:01 -0500250
251 virtual void onPrePrepare(GrRecordingContext*) {} // Only the GrOpsTask currently overrides this
252 virtual void onPrepare(GrOpFlushState*) {} // Only GrOpsTask and GrDDLTask override this virtual
Chris Dalton6b498102019-08-01 14:14:52 -0600253 virtual bool onExecute(GrOpFlushState* flushState) = 0;
254
255 const uint32_t fUniqueID;
256 uint32_t fFlags;
257
Greg Danielf41b2bd2019-08-22 16:19:24 -0400258 // 'this' GrRenderTask relies on the output of the GrRenderTasks in 'fDependencies'
Chris Dalton6b498102019-08-01 14:14:52 -0600259 SkSTArray<1, GrRenderTask*, true> fDependencies;
Greg Danielf41b2bd2019-08-22 16:19:24 -0400260 // 'this' GrRenderTask's output is relied on by the GrRenderTasks in 'fDependents'
Chris Dalton6b498102019-08-01 14:14:52 -0600261 SkSTArray<1, GrRenderTask*, true> fDependents;
Chris Daltone2a903e2019-09-18 13:41:50 -0600262
263 // For performance reasons, we should perform texture resolves back-to-back as much as possible.
264 // (http://skbug.com/9406). To accomplish this, we make and reuse one single resolve task for
265 // each render task, then add it as a dependency during makeClosed().
266 GrTextureResolveRenderTask* fTextureResolveTask = nullptr;
Adlai Holler96ead542020-06-26 08:50:14 -0400267
268 SkDEBUGCODE(GrDrawingManager *fDrawingMgr = nullptr;)
Chris Dalton6b498102019-08-01 14:14:52 -0600269};
270
271#endif