blob: 85ded83eb529d3c23419fd20fa094cc2d23fbb87 [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"
12#include "include/private/SkColorData.h"
13#include "include/private/SkTDArray.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
Chris Dalton6b498102019-08-01 14:14:52 -0600114protected:
115 // In addition to just the GrSurface being allocated, has the stencil buffer been allocated (if
116 // it is required)?
117 bool isInstantiated() const;
118
119 SkDEBUGCODE(bool deferredProxiesAreInstantiated() const;)
120
Adlai Holler33d569e2020-06-16 14:30:08 -0400121 // Add a target surface proxy to the list of targets for this task.
122 // This also informs the drawing manager to update the lastRenderTask association.
123 void addTarget(GrDrawingManager*, GrSurfaceProxyView);
124
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600125 enum class ExpectedOutcome : bool {
126 kTargetUnchanged,
127 kTargetDirty,
128 };
129
Chris Dalton16a33c62019-09-24 22:19:17 -0600130 // Performs any work to finalize this renderTask prior to execution. If returning
131 // ExpectedOutcome::kTargetDiry, the caller is also responsible to fill out the area it will
132 // modify in targetUpdateBounds.
133 //
134 // targetUpdateBounds must not extend beyond the proxy bounds.
135 virtual ExpectedOutcome onMakeClosed(const GrCaps&, SkIRect* targetUpdateBounds) = 0;
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600136
Adlai Holler33d569e2020-06-16 14:30:08 -0400137 SkSTArray<1, GrSurfaceProxyView> fTargets;
Chris Dalton6b498102019-08-01 14:14:52 -0600138
139 // List of texture proxies whose contents are being prepared on a worker thread
140 // TODO: this list exists so we can fire off the proper upload when an renderTask begins
141 // executing. Can this be replaced?
142 SkTArray<GrTextureProxy*, true> fDeferredProxies;
143
144private:
145 // for resetFlag, TopoSortTraits, gatherProxyIntervals, handleInternalAllocationFailure
146 friend class GrDrawingManager;
Robert Phillips07f675d2020-11-16 13:44:01 -0500147 friend class GrDDLTask; // for handleInternalAllocationFailure, gatherProxyIntervals, Flags
148
Chris Dalton6b498102019-08-01 14:14:52 -0600149
150 // Drops any pending operations that reference proxies that are not instantiated.
Adlai Holler33d569e2020-06-16 14:30:08 -0400151 // NOTE: Derived classes don't need to check targets. That is handled when the
Greg Daniel16f5c652019-10-29 11:26:01 -0400152 // drawingManager calls isInstantiated.
Chris Dalton6b498102019-08-01 14:14:52 -0600153 virtual void handleInternalAllocationFailure() = 0;
154
Adlai Holler33d569e2020-06-16 14:30:08 -0400155 // Derived classes can override to indicate usage of proxies _other than target proxies_.
156 // GrRenderTask itself will handle checking the target proxies.
Chris Dalton6b498102019-08-01 14:14:52 -0600157 virtual bool onIsUsed(GrSurfaceProxy*) const = 0;
158
Chris Dalton6b498102019-08-01 14:14:52 -0600159 void addDependency(GrRenderTask* dependedOn);
160 void addDependent(GrRenderTask* dependent);
161 SkDEBUGCODE(bool isDependedent(const GrRenderTask* dependent) const;)
162 SkDEBUGCODE(void validate() const;)
163 void closeThoseWhoDependOnMe(const GrCaps&);
164
165 // Feed proxy usage intervals to the GrResourceAllocator class
166 virtual void gatherProxyIntervals(GrResourceAllocator*) const = 0;
167
168 static uint32_t CreateUniqueID();
169
170 enum Flags {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400171 kClosed_Flag = 0x01, //!< This task can't accept any more dependencies.
172 kDisowned_Flag = 0x02, //!< This task is disowned by its creating GrDrawingManager.
Chris Dalton6b498102019-08-01 14:14:52 -0600173
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400174 kWasOutput_Flag = 0x04, //!< Flag for topological sorting
175 kTempMark_Flag = 0x08, //!< Flag for topological sorting
Chris Dalton6b498102019-08-01 14:14:52 -0600176 };
177
178 void setFlag(uint32_t flag) {
179 fFlags |= flag;
180 }
181
182 void resetFlag(uint32_t flag) {
183 fFlags &= ~flag;
184 }
185
186 bool isSetFlag(uint32_t flag) const {
187 return SkToBool(fFlags & flag);
188 }
189
190 struct TopoSortTraits {
191 static void Output(GrRenderTask* renderTask, int /* index */) {
192 renderTask->setFlag(kWasOutput_Flag);
193 }
194 static bool WasOutput(const GrRenderTask* renderTask) {
195 return renderTask->isSetFlag(kWasOutput_Flag);
196 }
197 static void SetTempMark(GrRenderTask* renderTask) {
198 renderTask->setFlag(kTempMark_Flag);
199 }
200 static void ResetTempMark(GrRenderTask* renderTask) {
201 renderTask->resetFlag(kTempMark_Flag);
202 }
203 static bool IsTempMarked(const GrRenderTask* renderTask) {
204 return renderTask->isSetFlag(kTempMark_Flag);
205 }
206 static int NumDependencies(const GrRenderTask* renderTask) {
207 return renderTask->fDependencies.count();
208 }
209 static GrRenderTask* Dependency(GrRenderTask* renderTask, int index) {
210 return renderTask->fDependencies[index];
211 }
212 };
213
Robert Phillips07f675d2020-11-16 13:44:01 -0500214
215 virtual void onPrePrepare(GrRecordingContext*) {} // Only the GrOpsTask currently overrides this
216 virtual void onPrepare(GrOpFlushState*) {} // Only GrOpsTask and GrDDLTask override this virtual
Chris Dalton6b498102019-08-01 14:14:52 -0600217 virtual bool onExecute(GrOpFlushState* flushState) = 0;
218
219 const uint32_t fUniqueID;
220 uint32_t fFlags;
221
Greg Danielf41b2bd2019-08-22 16:19:24 -0400222 // 'this' GrRenderTask relies on the output of the GrRenderTasks in 'fDependencies'
Chris Dalton6b498102019-08-01 14:14:52 -0600223 SkSTArray<1, GrRenderTask*, true> fDependencies;
Greg Danielf41b2bd2019-08-22 16:19:24 -0400224 // 'this' GrRenderTask's output is relied on by the GrRenderTasks in 'fDependents'
Chris Dalton6b498102019-08-01 14:14:52 -0600225 SkSTArray<1, GrRenderTask*, true> fDependents;
Chris Daltone2a903e2019-09-18 13:41:50 -0600226
227 // For performance reasons, we should perform texture resolves back-to-back as much as possible.
228 // (http://skbug.com/9406). To accomplish this, we make and reuse one single resolve task for
229 // each render task, then add it as a dependency during makeClosed().
230 GrTextureResolveRenderTask* fTextureResolveTask = nullptr;
Adlai Holler96ead542020-06-26 08:50:14 -0400231
232 SkDEBUGCODE(GrDrawingManager *fDrawingMgr = nullptr;)
Chris Dalton6b498102019-08-01 14:14:52 -0600233};
234
235#endif