blob: 4b23b71500be13a35661eba07a6fdec28e64f2d8 [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 */
Chris Daltonc4b47352019-08-23 10:10:36 -060087 virtual void dump(bool printDependencies) const;
Robert Phillips44e2c5f2020-04-14 13:00:04 -040088 virtual const char* name() const = 0;
John Stiles1e0136e2020-08-12 18:44:00 -040089#endif
Chris Dalton6b498102019-08-01 14:14:52 -060090
John Stiles1e0136e2020-08-12 18:44:00 -040091#ifdef SK_DEBUG
Chris Daltonc4b47352019-08-23 10:10:36 -060092 virtual int numClips() const { return 0; }
93
Michael Ludwigfcdd0612019-11-25 08:34:31 -050094 virtual void visitProxies_debugOnly(const GrOp::VisitProxyFunc&) const = 0;
Chris Daltonc4b47352019-08-23 10:10:36 -060095
Michael Ludwigfcdd0612019-11-25 08:34:31 -050096 void visitTargetAndSrcProxies_debugOnly(const GrOp::VisitProxyFunc& fn) const {
Chris Daltonc4b47352019-08-23 10:10:36 -060097 this->visitProxies_debugOnly(fn);
Robert Phillips07f675d2020-11-16 13:44:01 -050098 for (const GrSurfaceProxyView& target : fTargets) {
99 fn(target.proxy(), GrMipmapped::kNo);
Chris Daltone2a903e2019-09-18 13:41:50 -0600100 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600101 }
102#endif
Chris Dalton6b498102019-08-01 14:14:52 -0600103
Robert Phillips07f675d2020-11-16 13:44:01 -0500104 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 Phillips9ef7e202020-11-17 13:11:09 -0500114 // 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 Dalton6b498102019-08-01 14:14:52 -0600122 // In addition to just the GrSurface being allocated, has the stencil buffer been allocated (if
123 // it is required)?
124 bool isInstantiated() const;
125
Adlai Holler78036082021-01-07 11:41:33 -0500126 // Used by GrTCluster.
127 SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrRenderTask);
128
Robert Phillips9ef7e202020-11-17 13:11:09 -0500129protected:
Chris Dalton6b498102019-08-01 14:14:52 -0600130 SkDEBUGCODE(bool deferredProxiesAreInstantiated() const;)
131
Adlai Holler33d569e2020-06-16 14:30:08 -0400132 // Add a target surface proxy to the list of targets for this task.
133 // This also informs the drawing manager to update the lastRenderTask association.
134 void addTarget(GrDrawingManager*, GrSurfaceProxyView);
135
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600136 enum class ExpectedOutcome : bool {
137 kTargetUnchanged,
138 kTargetDirty,
139 };
140
Chris Dalton16a33c62019-09-24 22:19:17 -0600141 // Performs any work to finalize this renderTask prior to execution. If returning
142 // ExpectedOutcome::kTargetDiry, the caller is also responsible to fill out the area it will
143 // modify in targetUpdateBounds.
144 //
145 // targetUpdateBounds must not extend beyond the proxy bounds.
146 virtual ExpectedOutcome onMakeClosed(const GrCaps&, SkIRect* targetUpdateBounds) = 0;
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600147
Adlai Holler33d569e2020-06-16 14:30:08 -0400148 SkSTArray<1, GrSurfaceProxyView> fTargets;
Chris Dalton6b498102019-08-01 14:14:52 -0600149
150 // List of texture proxies whose contents are being prepared on a worker thread
151 // TODO: this list exists so we can fire off the proper upload when an renderTask begins
152 // executing. Can this be replaced?
153 SkTArray<GrTextureProxy*, true> fDeferredProxies;
154
Chris Dalton6b498102019-08-01 14:14:52 -0600155 enum Flags {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400156 kClosed_Flag = 0x01, //!< This task can't accept any more dependencies.
157 kDisowned_Flag = 0x02, //!< This task is disowned by its creating GrDrawingManager.
Chris Dalton6b498102019-08-01 14:14:52 -0600158
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400159 kWasOutput_Flag = 0x04, //!< Flag for topological sorting
160 kTempMark_Flag = 0x08, //!< Flag for topological sorting
Chris Dalton6b498102019-08-01 14:14:52 -0600161 };
162
163 void setFlag(uint32_t flag) {
164 fFlags |= flag;
165 }
166
167 void resetFlag(uint32_t flag) {
168 fFlags &= ~flag;
169 }
170
171 bool isSetFlag(uint32_t flag) const {
172 return SkToBool(fFlags & flag);
173 }
174
Robert Phillips4dbff752020-11-18 12:16:31 -0500175 void setIndex(uint32_t index) {
176 SkASSERT(!this->isSetFlag(kWasOutput_Flag));
177 SkASSERT(index < (1 << 28));
178 fFlags |= index << 4;
179 }
180
181 uint32_t getIndex() const {
182 SkASSERT(this->isSetFlag(kWasOutput_Flag));
183 return fFlags >> 4;
184 }
185
Robert Phillips9ef7e202020-11-17 13:11:09 -0500186private:
187 // for TopoSortTraits, fTextureResolveTask, closeThoseWhoDependOnMe, addDependency
188 friend class GrDrawingManager;
189
190 // Derived classes can override to indicate usage of proxies _other than target proxies_.
191 // GrRenderTask itself will handle checking the target proxies.
192 virtual bool onIsUsed(GrSurfaceProxy*) const = 0;
193
194 void addDependency(GrRenderTask* dependedOn);
195 void addDependent(GrRenderTask* dependent);
Adlai Holler78036082021-01-07 11:41:33 -0500196 SkDEBUGCODE(bool isDependent(const GrRenderTask* dependent) const;)
Robert Phillips9ef7e202020-11-17 13:11:09 -0500197 SkDEBUGCODE(void validate() const;)
198 void closeThoseWhoDependOnMe(const GrCaps&);
199
200 static uint32_t CreateUniqueID();
201
Chris Dalton6b498102019-08-01 14:14:52 -0600202 struct TopoSortTraits {
Robert Phillips4dbff752020-11-18 12:16:31 -0500203 static uint32_t GetIndex(GrRenderTask* renderTask) {
204 return renderTask->getIndex();
205 }
206 static void Output(GrRenderTask* renderTask, uint32_t index) {
207 renderTask->setIndex(index);
Chris Dalton6b498102019-08-01 14:14:52 -0600208 renderTask->setFlag(kWasOutput_Flag);
209 }
210 static bool WasOutput(const GrRenderTask* renderTask) {
211 return renderTask->isSetFlag(kWasOutput_Flag);
212 }
213 static void SetTempMark(GrRenderTask* renderTask) {
214 renderTask->setFlag(kTempMark_Flag);
215 }
216 static void ResetTempMark(GrRenderTask* renderTask) {
217 renderTask->resetFlag(kTempMark_Flag);
218 }
219 static bool IsTempMarked(const GrRenderTask* renderTask) {
220 return renderTask->isSetFlag(kTempMark_Flag);
221 }
222 static int NumDependencies(const GrRenderTask* renderTask) {
223 return renderTask->fDependencies.count();
224 }
225 static GrRenderTask* Dependency(GrRenderTask* renderTask, int index) {
226 return renderTask->fDependencies[index];
227 }
228 };
229
Adlai Holler78036082021-01-07 11:41:33 -0500230 struct ClusterTraits {
231 static int NumTargets(GrRenderTask* renderTask) {
232 return renderTask->numTargets();
233 }
234 static uint32_t GetTarget(GrRenderTask* renderTask, int index) {
235 return renderTask->target(index).proxy()->uniqueID().asUInt();
236 }
237 static int NumDependencies(const GrRenderTask* renderTask) {
238 return renderTask->fDependencies.count();
239 }
240 static GrRenderTask* Dependency(GrRenderTask* renderTask, int index) {
241 return renderTask->fDependencies[index];
242 }
243 static uint32_t GetID(GrRenderTask* renderTask) {
244 return renderTask->uniqueID();
245 }
246 };
Robert Phillips07f675d2020-11-16 13:44:01 -0500247
248 virtual void onPrePrepare(GrRecordingContext*) {} // Only the GrOpsTask currently overrides this
249 virtual void onPrepare(GrOpFlushState*) {} // Only GrOpsTask and GrDDLTask override this virtual
Chris Dalton6b498102019-08-01 14:14:52 -0600250 virtual bool onExecute(GrOpFlushState* flushState) = 0;
251
252 const uint32_t fUniqueID;
253 uint32_t fFlags;
254
Greg Danielf41b2bd2019-08-22 16:19:24 -0400255 // 'this' GrRenderTask relies on the output of the GrRenderTasks in 'fDependencies'
Chris Dalton6b498102019-08-01 14:14:52 -0600256 SkSTArray<1, GrRenderTask*, true> fDependencies;
Greg Danielf41b2bd2019-08-22 16:19:24 -0400257 // 'this' GrRenderTask's output is relied on by the GrRenderTasks in 'fDependents'
Chris Dalton6b498102019-08-01 14:14:52 -0600258 SkSTArray<1, GrRenderTask*, true> fDependents;
Chris Daltone2a903e2019-09-18 13:41:50 -0600259
260 // For performance reasons, we should perform texture resolves back-to-back as much as possible.
261 // (http://skbug.com/9406). To accomplish this, we make and reuse one single resolve task for
262 // each render task, then add it as a dependency during makeClosed().
263 GrTextureResolveRenderTask* fTextureResolveTask = nullptr;
Adlai Holler96ead542020-06-26 08:50:14 -0400264
265 SkDEBUGCODE(GrDrawingManager *fDrawingMgr = nullptr;)
Chris Dalton6b498102019-08-01 14:14:52 -0600266};
267
268#endif