blob: 5c9d67f4ba59f2dd36881fded6def156a97c40a4 [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
Adlai Holler08f53112021-01-20 17:44:15 -050019class GrMockRenderTask;
Chris Dalton6b498102019-08-01 14:14:52 -060020class GrOpFlushState;
Greg Danielf41b2bd2019-08-22 16:19:24 -040021class GrOpsTask;
Chris Dalton6b498102019-08-01 14:14:52 -060022class GrResourceAllocator;
Chris Daltone2a903e2019-09-18 13:41:50 -060023class GrTextureResolveRenderTask;
Chris Dalton6b498102019-08-01 14:14:52 -060024
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 Danielf41b2bd2019-08-22 16:19:24 -040027// contents. (e.g., an opsTask that executes a command buffer, a task to regenerate mipmaps, etc.)
Chris Dalton6b498102019-08-01 14:14:52 -060028class GrRenderTask : public SkRefCnt {
29public:
Greg Daniel16f5c652019-10-29 11:26:01 -040030 GrRenderTask();
Adlai Holler33d569e2020-06-16 14:30:08 -040031 SkDEBUGCODE(~GrRenderTask() override);
Chris Dalton6b498102019-08-01 14:14:52 -060032
Chris Daltonaa3cbb82019-08-21 00:01:21 -060033 void makeClosed(const GrCaps&);
34
Robert Phillips29f38542019-10-16 09:20:25 -040035 void prePrepare(GrRecordingContext* context) { this->onPrePrepare(context); }
Robert Phillips7327c9d2019-10-08 16:32:56 -040036
Chris Dalton6b498102019-08-01 14:14:52 -060037 // These two methods are only invoked at flush time
Adlai Holler9e2c50e2021-02-09 14:41:52 -050038 void prepare(GrOpFlushState* flushState);
Chris Dalton6b498102019-08-01 14:14:52 -060039 bool execute(GrOpFlushState* flushState) { return this->onExecute(flushState); }
40
Robert Phillips07f675d2020-11-16 13:44:01 -050041 virtual bool requiresExplicitCleanup() const { return false; }
42
Chris Dalton6b498102019-08-01 14:14:52 -060043 // 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 Hollerd71b7b02020-06-08 15:55:00 -040046 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 Dalton6b498102019-08-01 14:14:52 -060052
53 bool isClosed() const { return this->isSetFlag(kClosed_Flag); }
54
Brian Salomond63638b2021-03-05 14:00:07 -050055 /**
Adlai Hollere9ea4142021-04-27 14:31:56 -040056 * Make this task skippable. This must be used purely for optimization purposes
Brian Salomond63638b2021-03-05 14:00:07 -050057 * 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 */
Adlai Hollere9ea4142021-04-27 14:31:56 -040065 void makeSkippable();
66
67 bool isSkippable() const { return this->isSetFlag(kSkippable_Flag); }
Brian Salomond63638b2021-03-05 14:00:07 -050068
Chris Dalton6b498102019-08-01 14:14:52 -060069 /*
70 * Notify this GrRenderTask that it relies on the contents of 'dependedOn'
71 */
Brian Salomon7e67dca2020-07-21 09:27:25 -040072 void addDependency(GrDrawingManager*, GrSurfaceProxy* dependedOn, GrMipmapped,
Adlai Hollerd71b7b02020-06-08 15:55:00 -040073 GrTextureResolveManager, const GrCaps& caps);
Chris Dalton6b498102019-08-01 14:14:52 -060074
75 /*
Greg Danielc30f1a92019-09-06 15:28:58 -040076 * Notify this GrRenderTask that it relies on the contents of all GrRenderTasks which otherTask
77 * depends on.
78 */
79 void addDependenciesFromOtherTask(GrRenderTask* otherTask);
80
Adlai Holler08f53112021-01-20 17:44:15 -050081 SkSpan<GrRenderTask*> dependencies() { return SkSpan<GrRenderTask*>(fDependencies); }
Herb Derbyf5b03fc2021-04-29 14:01:12 -040082 SkSpan<GrRenderTask*> dependents() { return SkSpan<GrRenderTask*>(fDependents); }
83
84 void replaceDependency(const GrRenderTask* toReplace, GrRenderTask* replaceWith);
85 void replaceDependent(const GrRenderTask* toReplace, GrRenderTask* replaceWith);
86
Adlai Holler08f53112021-01-20 17:44:15 -050087
Greg Danielc30f1a92019-09-06 15:28:58 -040088 /*
Chris Dalton6b498102019-08-01 14:14:52 -060089 * Does this renderTask depend on 'dependedOn'?
90 */
91 bool dependsOn(const GrRenderTask* dependedOn) const;
92
Robert Phillips07f675d2020-11-16 13:44:01 -050093 virtual void gatherIDs(SkSTArray<8, uint32_t, true>* idArray) const {
94 idArray->push_back(fUniqueID);
95 }
Chris Dalton6b498102019-08-01 14:14:52 -060096 uint32_t uniqueID() const { return fUniqueID; }
Robert Phillips07f675d2020-11-16 13:44:01 -050097 virtual int numTargets() const { return fTargets.count(); }
Brian Salomon982127b2021-01-21 10:43:35 -050098 GrSurfaceProxy* target(int i) const { return fTargets[i].get(); }
Chris Dalton6b498102019-08-01 14:14:52 -060099
100 /*
Greg Danielf41b2bd2019-08-22 16:19:24 -0400101 * Safely cast this GrRenderTask to a GrOpsTask (if possible).
Chris Dalton6b498102019-08-01 14:14:52 -0600102 */
Greg Danielf41b2bd2019-08-22 16:19:24 -0400103 virtual GrOpsTask* asOpsTask() { return nullptr; }
Chris Dalton6b498102019-08-01 14:14:52 -0600104
John Stiles1e0136e2020-08-12 18:44:00 -0400105#if GR_TEST_UTILS
Chris Dalton6b498102019-08-01 14:14:52 -0600106 /*
107 * Dump out the GrRenderTask dependency DAG
108 */
Robert Phillips047d5bb2021-01-08 13:39:19 -0500109 virtual void dump(const SkString& label,
110 SkString indent,
111 bool printDependencies,
112 bool close) const;
Robert Phillips44e2c5f2020-04-14 13:00:04 -0400113 virtual const char* name() const = 0;
John Stiles1e0136e2020-08-12 18:44:00 -0400114#endif
Chris Dalton6b498102019-08-01 14:14:52 -0600115
John Stiles1e0136e2020-08-12 18:44:00 -0400116#ifdef SK_DEBUG
Chris Daltonc4b47352019-08-23 10:10:36 -0600117 virtual int numClips() const { return 0; }
118
Michael Ludwigfcdd0612019-11-25 08:34:31 -0500119 virtual void visitProxies_debugOnly(const GrOp::VisitProxyFunc&) const = 0;
Chris Daltonc4b47352019-08-23 10:10:36 -0600120
Michael Ludwigfcdd0612019-11-25 08:34:31 -0500121 void visitTargetAndSrcProxies_debugOnly(const GrOp::VisitProxyFunc& fn) const {
Chris Daltonc4b47352019-08-23 10:10:36 -0600122 this->visitProxies_debugOnly(fn);
Brian Salomon982127b2021-01-21 10:43:35 -0500123 for (const sk_sp<GrSurfaceProxy>& target : fTargets) {
124 fn(target.get(), GrMipmapped::kNo);
Chris Daltone2a903e2019-09-18 13:41:50 -0600125 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600126 }
127#endif
Chris Dalton6b498102019-08-01 14:14:52 -0600128
Robert Phillips07f675d2020-11-16 13:44:01 -0500129 bool isUsed(GrSurfaceProxy* proxy) const {
Brian Salomon982127b2021-01-21 10:43:35 -0500130 for (const sk_sp<GrSurfaceProxy>& target : fTargets) {
131 if (target.get() == proxy) {
Robert Phillips07f675d2020-11-16 13:44:01 -0500132 return true;
133 }
134 }
135
136 return this->onIsUsed(proxy);
137 }
138
Robert Phillips9ef7e202020-11-17 13:11:09 -0500139 // Feed proxy usage intervals to the GrResourceAllocator class
140 virtual void gatherProxyIntervals(GrResourceAllocator*) const = 0;
141
Chris Dalton6b498102019-08-01 14:14:52 -0600142 // In addition to just the GrSurface being allocated, has the stencil buffer been allocated (if
143 // it is required)?
144 bool isInstantiated() const;
145
Adlai Holler08f53112021-01-20 17:44:15 -0500146 // Used by GrRenderTaskCluster.
Adlai Holler78036082021-01-07 11:41:33 -0500147 SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrRenderTask);
148
Robert Phillips9ef7e202020-11-17 13:11:09 -0500149protected:
Adlai Holler9e2c50e2021-02-09 14:41:52 -0500150 SkDEBUGCODE(bool deferredProxiesAreInstantiated() const;)
151
Adlai Holler33d569e2020-06-16 14:30:08 -0400152 // 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.
Brian Salomon982127b2021-01-21 10:43:35 -0500154 void addTarget(GrDrawingManager*, sk_sp<GrSurfaceProxy>);
155
156 // Helper that adds the proxy owned by a view.
157 void addTarget(GrDrawingManager* dm, const GrSurfaceProxyView& view) {
158 this->addTarget(dm, view.refProxy());
159 }
Adlai Holler33d569e2020-06-16 14:30:08 -0400160
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600161 enum class ExpectedOutcome : bool {
162 kTargetUnchanged,
163 kTargetDirty,
164 };
165
Chris Dalton16a33c62019-09-24 22:19:17 -0600166 // 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;
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600172
Brian Salomon982127b2021-01-21 10:43:35 -0500173 SkSTArray<1, sk_sp<GrSurfaceProxy>> fTargets;
Chris Dalton6b498102019-08-01 14:14:52 -0600174
Adlai Holler9e2c50e2021-02-09 14:41:52 -0500175 // 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
Chris Dalton6b498102019-08-01 14:14:52 -0600180 enum Flags {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400181 kClosed_Flag = 0x01, //!< This task can't accept any more dependencies.
182 kDisowned_Flag = 0x02, //!< This task is disowned by its creating GrDrawingManager.
Adlai Hollere9ea4142021-04-27 14:31:56 -0400183 kSkippable_Flag = 0x04, //!< This task is skippable.
Chris Dalton6b498102019-08-01 14:14:52 -0600184
Adlai Hollere9ea4142021-04-27 14:31:56 -0400185 kWasOutput_Flag = 0x08, //!< Flag for topological sorting
186 kTempMark_Flag = 0x10, //!< Flag for topological sorting
Chris Dalton6b498102019-08-01 14:14:52 -0600187 };
188
189 void setFlag(uint32_t flag) {
190 fFlags |= flag;
191 }
192
193 void resetFlag(uint32_t flag) {
194 fFlags &= ~flag;
195 }
196
197 bool isSetFlag(uint32_t flag) const {
198 return SkToBool(fFlags & flag);
199 }
200
Robert Phillips4dbff752020-11-18 12:16:31 -0500201 void setIndex(uint32_t index) {
202 SkASSERT(!this->isSetFlag(kWasOutput_Flag));
Adlai Hollere9ea4142021-04-27 14:31:56 -0400203 SkASSERT(index < (1 << 27));
204 fFlags |= index << 5;
Robert Phillips4dbff752020-11-18 12:16:31 -0500205 }
206
207 uint32_t getIndex() const {
208 SkASSERT(this->isSetFlag(kWasOutput_Flag));
Adlai Hollere9ea4142021-04-27 14:31:56 -0400209 return fFlags >> 5;
Robert Phillips4dbff752020-11-18 12:16:31 -0500210 }
211
Robert Phillips9ef7e202020-11-17 13:11:09 -0500212private:
213 // for TopoSortTraits, fTextureResolveTask, closeThoseWhoDependOnMe, addDependency
214 friend class GrDrawingManager;
Adlai Holler08f53112021-01-20 17:44:15 -0500215 friend class GrMockRenderTask;
Robert Phillips9ef7e202020-11-17 13:11:09 -0500216
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);
Adlai Holler78036082021-01-07 11:41:33 -0500223 SkDEBUGCODE(bool isDependent(const GrRenderTask* dependent) const;)
Robert Phillips9ef7e202020-11-17 13:11:09 -0500224 SkDEBUGCODE(void validate() const;)
225 void closeThoseWhoDependOnMe(const GrCaps&);
226
227 static uint32_t CreateUniqueID();
228
Chris Dalton6b498102019-08-01 14:14:52 -0600229 struct TopoSortTraits {
Robert Phillips4dbff752020-11-18 12:16:31 -0500230 static uint32_t GetIndex(GrRenderTask* renderTask) {
231 return renderTask->getIndex();
232 }
233 static void Output(GrRenderTask* renderTask, uint32_t index) {
234 renderTask->setIndex(index);
Chris Dalton6b498102019-08-01 14:14:52 -0600235 renderTask->setFlag(kWasOutput_Flag);
236 }
237 static bool WasOutput(const GrRenderTask* renderTask) {
238 return renderTask->isSetFlag(kWasOutput_Flag);
239 }
240 static void SetTempMark(GrRenderTask* renderTask) {
241 renderTask->setFlag(kTempMark_Flag);
242 }
243 static void ResetTempMark(GrRenderTask* renderTask) {
244 renderTask->resetFlag(kTempMark_Flag);
245 }
246 static bool IsTempMarked(const GrRenderTask* renderTask) {
247 return renderTask->isSetFlag(kTempMark_Flag);
248 }
249 static int NumDependencies(const GrRenderTask* renderTask) {
250 return renderTask->fDependencies.count();
251 }
252 static GrRenderTask* Dependency(GrRenderTask* renderTask, int index) {
253 return renderTask->fDependencies[index];
254 }
255 };
256
Adlai Hollere9ea4142021-04-27 14:31:56 -0400257 virtual void onMakeSkippable() {}
258 virtual void onPrePrepare(GrRecordingContext*) {} // Only GrOpsTask currently overrides this
259 virtual void onPrepare(GrOpFlushState*) {} // GrOpsTask and GrDDLTask override this
Chris Dalton6b498102019-08-01 14:14:52 -0600260 virtual bool onExecute(GrOpFlushState* flushState) = 0;
261
262 const uint32_t fUniqueID;
263 uint32_t fFlags;
264
Greg Danielf41b2bd2019-08-22 16:19:24 -0400265 // 'this' GrRenderTask relies on the output of the GrRenderTasks in 'fDependencies'
Chris Dalton6b498102019-08-01 14:14:52 -0600266 SkSTArray<1, GrRenderTask*, true> fDependencies;
Greg Danielf41b2bd2019-08-22 16:19:24 -0400267 // 'this' GrRenderTask's output is relied on by the GrRenderTasks in 'fDependents'
Chris Dalton6b498102019-08-01 14:14:52 -0600268 SkSTArray<1, GrRenderTask*, true> fDependents;
Chris Daltone2a903e2019-09-18 13:41:50 -0600269
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;
Adlai Holler96ead542020-06-26 08:50:14 -0400274
275 SkDEBUGCODE(GrDrawingManager *fDrawingMgr = nullptr;)
Chris Dalton6b498102019-08-01 14:14:52 -0600276};
277
278#endif