blob: f9b3825adda12c7c20ee14c27511f79ffc61f40d [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"
Chris Dalton6b498102019-08-01 14:14:52 -060017
Adlai Holler08f53112021-01-20 17:44:15 -050018class GrMockRenderTask;
Chris Dalton6b498102019-08-01 14:14:52 -060019class GrOpFlushState;
Chris Dalton6b498102019-08-01 14:14:52 -060020class GrResourceAllocator;
Chris Daltone2a903e2019-09-18 13:41:50 -060021class GrTextureResolveRenderTask;
Robert Phillips3e87a8e2021-08-25 13:22:24 -040022namespace skgpu { namespace v1 { class OpsTask; }}
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 Daltonaa938ce2021-06-23 18:13:59 -060032 void makeClosed(GrRecordingContext*);
Chris Daltonaa3cbb82019-08-21 00:01:21 -060033
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
Adlai Holler9e2c50e2021-02-09 14:41:52 -050037 void prepare(GrOpFlushState* flushState);
Chris Dalton6b498102019-08-01 14:14:52 -060038 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
Brian Salomond63638b2021-03-05 14:00:07 -050054 /**
Adlai Hollere9ea4142021-04-27 14:31:56 -040055 * Make this task skippable. This must be used purely for optimization purposes
Brian Salomond63638b2021-03-05 14:00:07 -050056 * at this point as not all tasks will actually skip their work. It would be better if we could
57 * detect tasks that can be skipped automatically. We'd need to support minimal flushes (i.e.,
58 * only flush that which is required for SkSurfaces/SkImages) and the ability to detect
59 * "orphaned tasks" and clean them out from the DAG so they don't indefinitely accumulate.
60 * Finally, we'd probably have to track whether a proxy's backing store was imported or ever
61 * exported to the client in case the client is doing direct reads outside of Skia and thus
62 * may require tasks targeting the proxy to execute even if our DAG contains no reads.
63 */
Adlai Hollere9ea4142021-04-27 14:31:56 -040064 void makeSkippable();
65
66 bool isSkippable() const { return this->isSetFlag(kSkippable_Flag); }
Brian Salomond63638b2021-03-05 14:00:07 -050067
Chris Dalton6b498102019-08-01 14:14:52 -060068 /*
69 * Notify this GrRenderTask that it relies on the contents of 'dependedOn'
70 */
Brian Salomon7e67dca2020-07-21 09:27:25 -040071 void addDependency(GrDrawingManager*, GrSurfaceProxy* dependedOn, GrMipmapped,
Adlai Hollerd71b7b02020-06-08 15:55:00 -040072 GrTextureResolveManager, const GrCaps& caps);
Chris Dalton6b498102019-08-01 14:14:52 -060073
74 /*
Greg Danielc30f1a92019-09-06 15:28:58 -040075 * Notify this GrRenderTask that it relies on the contents of all GrRenderTasks which otherTask
76 * depends on.
77 */
78 void addDependenciesFromOtherTask(GrRenderTask* otherTask);
79
Brian Osmanae87bf12021-05-11 13:36:10 -040080 SkSpan<GrRenderTask*> dependencies() { return SkMakeSpan(fDependencies); }
81 SkSpan<GrRenderTask*> dependents() { return SkMakeSpan(fDependents); }
Herb Derbyf5b03fc2021-04-29 14:01:12 -040082
83 void replaceDependency(const GrRenderTask* toReplace, GrRenderTask* replaceWith);
84 void replaceDependent(const GrRenderTask* toReplace, GrRenderTask* replaceWith);
85
Adlai Holler08f53112021-01-20 17:44:15 -050086
Greg Danielc30f1a92019-09-06 15:28:58 -040087 /*
Chris Dalton6b498102019-08-01 14:14:52 -060088 * Does this renderTask depend on 'dependedOn'?
89 */
90 bool dependsOn(const GrRenderTask* dependedOn) const;
91
Robert Phillips07f675d2020-11-16 13:44:01 -050092 virtual void gatherIDs(SkSTArray<8, uint32_t, true>* idArray) const {
93 idArray->push_back(fUniqueID);
94 }
Chris Dalton6b498102019-08-01 14:14:52 -060095 uint32_t uniqueID() const { return fUniqueID; }
Adlai Holler858fc992021-05-17 13:51:46 -040096 int numTargets() const { return fTargets.count(); }
Brian Salomon982127b2021-01-21 10:43:35 -050097 GrSurfaceProxy* target(int i) const { return fTargets[i].get(); }
Chris Dalton6b498102019-08-01 14:14:52 -060098
99 /*
Robert Phillips3e87a8e2021-08-25 13:22:24 -0400100 * Safely cast this GrRenderTask to a OpsTask (if possible).
Chris Dalton6b498102019-08-01 14:14:52 -0600101 */
Robert Phillips3e87a8e2021-08-25 13:22:24 -0400102 virtual skgpu::v1::OpsTask* asOpsTask() { return nullptr; }
Chris Dalton6b498102019-08-01 14:14:52 -0600103
John Stiles1e0136e2020-08-12 18:44:00 -0400104#if GR_TEST_UTILS
Chris Dalton6b498102019-08-01 14:14:52 -0600105 /*
106 * Dump out the GrRenderTask dependency DAG
107 */
Robert Phillips047d5bb2021-01-08 13:39:19 -0500108 virtual void dump(const SkString& label,
109 SkString indent,
110 bool printDependencies,
111 bool close) const;
Robert Phillips44e2c5f2020-04-14 13:00:04 -0400112 virtual const char* name() const = 0;
John Stiles1e0136e2020-08-12 18:44:00 -0400113#endif
Chris Dalton6b498102019-08-01 14:14:52 -0600114
John Stiles1e0136e2020-08-12 18:44:00 -0400115#ifdef SK_DEBUG
Chris Daltonc4b47352019-08-23 10:10:36 -0600116 virtual int numClips() const { return 0; }
117
Robert Phillips294723d2021-06-17 09:23:58 -0400118 virtual void visitProxies_debugOnly(const GrVisitProxyFunc&) const = 0;
Chris Daltonc4b47352019-08-23 10:10:36 -0600119
Robert Phillips294723d2021-06-17 09:23:58 -0400120 void visitTargetAndSrcProxies_debugOnly(const GrVisitProxyFunc& func) const {
121 this->visitProxies_debugOnly(func);
Brian Salomon982127b2021-01-21 10:43:35 -0500122 for (const sk_sp<GrSurfaceProxy>& target : fTargets) {
Robert Phillips294723d2021-06-17 09:23:58 -0400123 func(target.get(), GrMipmapped::kNo);
Chris Daltone2a903e2019-09-18 13:41:50 -0600124 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600125 }
126#endif
Chris Dalton6b498102019-08-01 14:14:52 -0600127
Robert Phillips07f675d2020-11-16 13:44:01 -0500128 bool isUsed(GrSurfaceProxy* proxy) const {
Brian Salomon982127b2021-01-21 10:43:35 -0500129 for (const sk_sp<GrSurfaceProxy>& target : fTargets) {
130 if (target.get() == proxy) {
Robert Phillips07f675d2020-11-16 13:44:01 -0500131 return true;
132 }
133 }
134
135 return this->onIsUsed(proxy);
136 }
137
Robert Phillips9ef7e202020-11-17 13:11:09 -0500138 // Feed proxy usage intervals to the GrResourceAllocator class
139 virtual void gatherProxyIntervals(GrResourceAllocator*) const = 0;
140
Chris Dalton6b498102019-08-01 14:14:52 -0600141 // In addition to just the GrSurface being allocated, has the stencil buffer been allocated (if
142 // it is required)?
143 bool isInstantiated() const;
144
Adlai Holler08f53112021-01-20 17:44:15 -0500145 // Used by GrRenderTaskCluster.
Adlai Holler78036082021-01-07 11:41:33 -0500146 SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrRenderTask);
147
Robert Phillips9ef7e202020-11-17 13:11:09 -0500148protected:
Adlai Holler9e2c50e2021-02-09 14:41:52 -0500149 SkDEBUGCODE(bool deferredProxiesAreInstantiated() const;)
150
Adlai Holler33d569e2020-06-16 14:30:08 -0400151 // Add a target surface proxy to the list of targets for this task.
152 // This also informs the drawing manager to update the lastRenderTask association.
Brian Salomon982127b2021-01-21 10:43:35 -0500153 void addTarget(GrDrawingManager*, sk_sp<GrSurfaceProxy>);
154
155 // Helper that adds the proxy owned by a view.
156 void addTarget(GrDrawingManager* dm, const GrSurfaceProxyView& view) {
157 this->addTarget(dm, view.refProxy());
158 }
Adlai Holler33d569e2020-06-16 14:30:08 -0400159
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600160 enum class ExpectedOutcome : bool {
161 kTargetUnchanged,
162 kTargetDirty,
163 };
164
Chris Dalton16a33c62019-09-24 22:19:17 -0600165 // Performs any work to finalize this renderTask prior to execution. If returning
166 // ExpectedOutcome::kTargetDiry, the caller is also responsible to fill out the area it will
167 // modify in targetUpdateBounds.
168 //
169 // targetUpdateBounds must not extend beyond the proxy bounds.
Chris Daltonaa938ce2021-06-23 18:13:59 -0600170 virtual ExpectedOutcome onMakeClosed(GrRecordingContext*, SkIRect* targetUpdateBounds) = 0;
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600171
Brian Salomon982127b2021-01-21 10:43:35 -0500172 SkSTArray<1, sk_sp<GrSurfaceProxy>> fTargets;
Chris Dalton6b498102019-08-01 14:14:52 -0600173
Adlai Holler9e2c50e2021-02-09 14:41:52 -0500174 // List of texture proxies whose contents are being prepared on a worker thread
175 // TODO: this list exists so we can fire off the proper upload when an renderTask begins
176 // executing. Can this be replaced?
177 SkTArray<GrTextureProxy*, true> fDeferredProxies;
178
Chris Dalton6b498102019-08-01 14:14:52 -0600179 enum Flags {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400180 kClosed_Flag = 0x01, //!< This task can't accept any more dependencies.
181 kDisowned_Flag = 0x02, //!< This task is disowned by its creating GrDrawingManager.
Adlai Hollere9ea4142021-04-27 14:31:56 -0400182 kSkippable_Flag = 0x04, //!< This task is skippable.
Chris Dalton83420eb2021-06-23 18:47:09 -0600183 kAtlas_Flag = 0x08, //!< This task is atlas.
Chris Dalton6b498102019-08-01 14:14:52 -0600184
Chris Dalton83420eb2021-06-23 18:47:09 -0600185 kWasOutput_Flag = 0x10, //!< Flag for topological sorting
186 kTempMark_Flag = 0x20, //!< 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));
Chris Dalton83420eb2021-06-23 18:47:09 -0600203 SkASSERT(index < (1 << 26));
204 fFlags |= index << 6;
Robert Phillips4dbff752020-11-18 12:16:31 -0500205 }
206
207 uint32_t getIndex() const {
208 SkASSERT(this->isSetFlag(kWasOutput_Flag));
Chris Dalton83420eb2021-06-23 18:47:09 -0600209 return fFlags >> 6;
Robert Phillips4dbff752020-11-18 12:16:31 -0500210 }
211
Robert Phillips9ef7e202020-11-17 13:11:09 -0500212private:
Chris Daltonaa938ce2021-06-23 18:13:59 -0600213 // for TopoSortTraits, fTextureResolveTask, addDependency
Robert Phillips9ef7e202020-11-17 13:11:09 -0500214 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;)
Robert Phillips9ef7e202020-11-17 13:11:09 -0500225
226 static uint32_t CreateUniqueID();
227
Chris Dalton6b498102019-08-01 14:14:52 -0600228 struct TopoSortTraits {
Robert Phillips4dbff752020-11-18 12:16:31 -0500229 static uint32_t GetIndex(GrRenderTask* renderTask) {
230 return renderTask->getIndex();
231 }
232 static void Output(GrRenderTask* renderTask, uint32_t index) {
233 renderTask->setIndex(index);
Chris Dalton6b498102019-08-01 14:14:52 -0600234 renderTask->setFlag(kWasOutput_Flag);
235 }
236 static bool WasOutput(const GrRenderTask* renderTask) {
237 return renderTask->isSetFlag(kWasOutput_Flag);
238 }
239 static void SetTempMark(GrRenderTask* renderTask) {
240 renderTask->setFlag(kTempMark_Flag);
241 }
242 static void ResetTempMark(GrRenderTask* renderTask) {
243 renderTask->resetFlag(kTempMark_Flag);
244 }
245 static bool IsTempMarked(const GrRenderTask* renderTask) {
246 return renderTask->isSetFlag(kTempMark_Flag);
247 }
248 static int NumDependencies(const GrRenderTask* renderTask) {
249 return renderTask->fDependencies.count();
250 }
251 static GrRenderTask* Dependency(GrRenderTask* renderTask, int index) {
252 return renderTask->fDependencies[index];
253 }
254 };
255
Adlai Hollere9ea4142021-04-27 14:31:56 -0400256 virtual void onMakeSkippable() {}
Robert Phillips3e87a8e2021-08-25 13:22:24 -0400257 virtual void onPrePrepare(GrRecordingContext*) {} // Only OpsTask currently overrides this
258 virtual void onPrepare(GrOpFlushState*) {} // OpsTask and GrDDLTask override this
Chris Dalton6b498102019-08-01 14:14:52 -0600259 virtual bool onExecute(GrOpFlushState* flushState) = 0;
260
261 const uint32_t fUniqueID;
262 uint32_t fFlags;
263
Greg Danielf41b2bd2019-08-22 16:19:24 -0400264 // 'this' GrRenderTask relies on the output of the GrRenderTasks in 'fDependencies'
Chris Dalton6b498102019-08-01 14:14:52 -0600265 SkSTArray<1, GrRenderTask*, true> fDependencies;
Greg Danielf41b2bd2019-08-22 16:19:24 -0400266 // 'this' GrRenderTask's output is relied on by the GrRenderTasks in 'fDependents'
Chris Dalton6b498102019-08-01 14:14:52 -0600267 SkSTArray<1, GrRenderTask*, true> fDependents;
Chris Daltone2a903e2019-09-18 13:41:50 -0600268
269 // For performance reasons, we should perform texture resolves back-to-back as much as possible.
270 // (http://skbug.com/9406). To accomplish this, we make and reuse one single resolve task for
271 // each render task, then add it as a dependency during makeClosed().
272 GrTextureResolveRenderTask* fTextureResolveTask = nullptr;
Adlai Holler96ead542020-06-26 08:50:14 -0400273
274 SkDEBUGCODE(GrDrawingManager *fDrawingMgr = nullptr;)
Chris Dalton6b498102019-08-01 14:14:52 -0600275};
276
277#endif