blob: 08c6253473f32077612a5f356684574054425dd7 [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"
Greg Daniel16f5c652019-10-29 11:26:01 -040013#include "src/gpu/GrSurfaceProxyView.h"
Chris Dalton6b498102019-08-01 14:14:52 -060014#include "src/gpu/GrTextureProxy.h"
Chris Dalton08755122019-08-05 16:13:47 -060015#include "src/gpu/GrTextureResolveManager.h"
Herb Derby082232b2020-06-10 15:08:18 -040016#include "src/gpu/ops/GrOp.h"
Chris Dalton6b498102019-08-01 14:14:52 -060017
18class GrOpFlushState;
Greg Danielf41b2bd2019-08-22 16:19:24 -040019class GrOpsTask;
Chris Dalton6b498102019-08-01 14:14:52 -060020class GrResourceAllocator;
Chris Daltone2a903e2019-09-18 13:41:50 -060021class GrTextureResolveRenderTask;
Chris Dalton6b498102019-08-01 14:14:52 -060022
23// This class abstracts a task that targets a single GrSurfaceProxy, participates in the
24// GrDrawingManager's DAG, and implements the onExecute method to modify its target proxy's
Greg Danielf41b2bd2019-08-22 16:19:24 -040025// contents. (e.g., an opsTask that executes a command buffer, a task to regenerate mipmaps, etc.)
Chris Dalton6b498102019-08-01 14:14:52 -060026class GrRenderTask : public SkRefCnt {
27public:
Greg Daniel16f5c652019-10-29 11:26:01 -040028 GrRenderTask();
Adlai Holler33d569e2020-06-16 14:30:08 -040029 SkDEBUGCODE(~GrRenderTask() override);
Chris Dalton6b498102019-08-01 14:14:52 -060030
Chris Daltonaa3cbb82019-08-21 00:01:21 -060031 void makeClosed(const GrCaps&);
32
Robert Phillips29f38542019-10-16 09:20:25 -040033 void prePrepare(GrRecordingContext* context) { this->onPrePrepare(context); }
Robert Phillips7327c9d2019-10-08 16:32:56 -040034
Chris Dalton6b498102019-08-01 14:14:52 -060035 // These two methods are only invoked at flush time
36 void prepare(GrOpFlushState* flushState);
37 bool execute(GrOpFlushState* flushState) { return this->onExecute(flushState); }
38
Robert Phillips07f675d2020-11-16 13:44:01 -050039 virtual bool requiresExplicitCleanup() const { return false; }
40
Chris Dalton6b498102019-08-01 14:14:52 -060041 // Called when this class will survive a flush and needs to truncate its ops and start over.
42 // TODO: ultimately it should be invalid for an op list to survive a flush.
43 // https://bugs.chromium.org/p/skia/issues/detail?id=7111
Adlai Hollerd71b7b02020-06-08 15:55:00 -040044 virtual void endFlush(GrDrawingManager*) {}
45
46 // This method "disowns" all the GrSurfaceProxies this RenderTask modifies. In
47 // practice this just means telling the drawingManager to forget the relevant
48 // mappings from surface proxy to last modifying rendertask.
49 virtual void disown(GrDrawingManager*);
Chris Dalton6b498102019-08-01 14:14:52 -060050
51 bool isClosed() const { return this->isSetFlag(kClosed_Flag); }
52
53 /*
54 * Notify this GrRenderTask that it relies on the contents of 'dependedOn'
55 */
Brian Salomon7e67dca2020-07-21 09:27:25 -040056 void addDependency(GrDrawingManager*, GrSurfaceProxy* dependedOn, GrMipmapped,
Adlai Hollerd71b7b02020-06-08 15:55:00 -040057 GrTextureResolveManager, const GrCaps& caps);
Chris Dalton6b498102019-08-01 14:14:52 -060058
59 /*
Greg Danielc30f1a92019-09-06 15:28:58 -040060 * Notify this GrRenderTask that it relies on the contents of all GrRenderTasks which otherTask
61 * depends on.
62 */
63 void addDependenciesFromOtherTask(GrRenderTask* otherTask);
64
65 /*
Chris Dalton6b498102019-08-01 14:14:52 -060066 * Does this renderTask depend on 'dependedOn'?
67 */
68 bool dependsOn(const GrRenderTask* dependedOn) const;
69
Robert Phillips07f675d2020-11-16 13:44:01 -050070 virtual void gatherIDs(SkSTArray<8, uint32_t, true>* idArray) const {
71 idArray->push_back(fUniqueID);
72 }
Chris Dalton6b498102019-08-01 14:14:52 -060073 uint32_t uniqueID() const { return fUniqueID; }
Robert Phillips07f675d2020-11-16 13:44:01 -050074 virtual int numTargets() const { return fTargets.count(); }
Adlai Holler33d569e2020-06-16 14:30:08 -040075 const GrSurfaceProxyView& target(int i) const { return fTargets[i]; }
Chris Dalton6b498102019-08-01 14:14:52 -060076
77 /*
Greg Danielf41b2bd2019-08-22 16:19:24 -040078 * Safely cast this GrRenderTask to a GrOpsTask (if possible).
Chris Dalton6b498102019-08-01 14:14:52 -060079 */
Greg Danielf41b2bd2019-08-22 16:19:24 -040080 virtual GrOpsTask* asOpsTask() { return nullptr; }
Chris Dalton6b498102019-08-01 14:14:52 -060081
John Stiles1e0136e2020-08-12 18:44:00 -040082#if GR_TEST_UTILS
Chris Dalton6b498102019-08-01 14:14:52 -060083 /*
84 * Dump out the GrRenderTask dependency DAG
85 */
Chris Daltonc4b47352019-08-23 10:10:36 -060086 virtual void dump(bool printDependencies) const;
Robert Phillips44e2c5f2020-04-14 13:00:04 -040087 virtual const char* name() const = 0;
John Stiles1e0136e2020-08-12 18:44:00 -040088#endif
Chris Dalton6b498102019-08-01 14:14:52 -060089
John Stiles1e0136e2020-08-12 18:44:00 -040090#ifdef SK_DEBUG
Chris Daltonc4b47352019-08-23 10:10:36 -060091 virtual int numClips() const { return 0; }
92
Michael Ludwigfcdd0612019-11-25 08:34:31 -050093 virtual void visitProxies_debugOnly(const GrOp::VisitProxyFunc&) const = 0;
Chris Daltonc4b47352019-08-23 10:10:36 -060094
Michael Ludwigfcdd0612019-11-25 08:34:31 -050095 void visitTargetAndSrcProxies_debugOnly(const GrOp::VisitProxyFunc& fn) const {
Chris Daltonc4b47352019-08-23 10:10:36 -060096 this->visitProxies_debugOnly(fn);
Robert Phillips07f675d2020-11-16 13:44:01 -050097 for (const GrSurfaceProxyView& target : fTargets) {
98 fn(target.proxy(), GrMipmapped::kNo);
Chris Daltone2a903e2019-09-18 13:41:50 -060099 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600100 }
101#endif
Chris Dalton6b498102019-08-01 14:14:52 -0600102
Robert Phillips07f675d2020-11-16 13:44:01 -0500103 bool isUsed(GrSurfaceProxy* proxy) const {
104 for (const GrSurfaceProxyView& target : fTargets) {
105 if (target.proxy() == proxy) {
106 return true;
107 }
108 }
109
110 return this->onIsUsed(proxy);
111 }
112
Robert Phillips9ef7e202020-11-17 13:11:09 -0500113 // Drops any pending operations that reference proxies that are not instantiated.
114 // NOTE: Derived classes don't need to check targets. That is handled when the
115 // drawingManager calls isInstantiated.
116 virtual void handleInternalAllocationFailure() = 0;
117
118 // Feed proxy usage intervals to the GrResourceAllocator class
119 virtual void gatherProxyIntervals(GrResourceAllocator*) const = 0;
120
Chris Dalton6b498102019-08-01 14:14:52 -0600121 // In addition to just the GrSurface being allocated, has the stencil buffer been allocated (if
122 // it is required)?
123 bool isInstantiated() const;
124
Robert Phillips9ef7e202020-11-17 13:11:09 -0500125protected:
Chris Dalton6b498102019-08-01 14:14:52 -0600126 SkDEBUGCODE(bool deferredProxiesAreInstantiated() const;)
127
Adlai Holler33d569e2020-06-16 14:30:08 -0400128 // Add a target surface proxy to the list of targets for this task.
129 // This also informs the drawing manager to update the lastRenderTask association.
130 void addTarget(GrDrawingManager*, GrSurfaceProxyView);
131
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600132 enum class ExpectedOutcome : bool {
133 kTargetUnchanged,
134 kTargetDirty,
135 };
136
Chris Dalton16a33c62019-09-24 22:19:17 -0600137 // Performs any work to finalize this renderTask prior to execution. If returning
138 // ExpectedOutcome::kTargetDiry, the caller is also responsible to fill out the area it will
139 // modify in targetUpdateBounds.
140 //
141 // targetUpdateBounds must not extend beyond the proxy bounds.
142 virtual ExpectedOutcome onMakeClosed(const GrCaps&, SkIRect* targetUpdateBounds) = 0;
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600143
Adlai Holler33d569e2020-06-16 14:30:08 -0400144 SkSTArray<1, GrSurfaceProxyView> fTargets;
Chris Dalton6b498102019-08-01 14:14:52 -0600145
146 // List of texture proxies whose contents are being prepared on a worker thread
147 // TODO: this list exists so we can fire off the proper upload when an renderTask begins
148 // executing. Can this be replaced?
149 SkTArray<GrTextureProxy*, true> fDeferredProxies;
150
Chris Dalton6b498102019-08-01 14:14:52 -0600151 enum Flags {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400152 kClosed_Flag = 0x01, //!< This task can't accept any more dependencies.
153 kDisowned_Flag = 0x02, //!< This task is disowned by its creating GrDrawingManager.
Chris Dalton6b498102019-08-01 14:14:52 -0600154
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400155 kWasOutput_Flag = 0x04, //!< Flag for topological sorting
156 kTempMark_Flag = 0x08, //!< Flag for topological sorting
Chris Dalton6b498102019-08-01 14:14:52 -0600157 };
158
159 void setFlag(uint32_t flag) {
160 fFlags |= flag;
161 }
162
163 void resetFlag(uint32_t flag) {
164 fFlags &= ~flag;
165 }
166
167 bool isSetFlag(uint32_t flag) const {
168 return SkToBool(fFlags & flag);
169 }
170
Robert Phillips4dbff752020-11-18 12:16:31 -0500171 void setIndex(uint32_t index) {
172 SkASSERT(!this->isSetFlag(kWasOutput_Flag));
173 SkASSERT(index < (1 << 28));
174 fFlags |= index << 4;
175 }
176
177 uint32_t getIndex() const {
178 SkASSERT(this->isSetFlag(kWasOutput_Flag));
179 return fFlags >> 4;
180 }
181
Robert Phillips9ef7e202020-11-17 13:11:09 -0500182private:
183 // for TopoSortTraits, fTextureResolveTask, closeThoseWhoDependOnMe, addDependency
184 friend class GrDrawingManager;
185
186 // Derived classes can override to indicate usage of proxies _other than target proxies_.
187 // GrRenderTask itself will handle checking the target proxies.
188 virtual bool onIsUsed(GrSurfaceProxy*) const = 0;
189
190 void addDependency(GrRenderTask* dependedOn);
191 void addDependent(GrRenderTask* dependent);
192 SkDEBUGCODE(bool isDependedent(const GrRenderTask* dependent) const;)
193 SkDEBUGCODE(void validate() const;)
194 void closeThoseWhoDependOnMe(const GrCaps&);
195
196 static uint32_t CreateUniqueID();
197
Chris Dalton6b498102019-08-01 14:14:52 -0600198 struct TopoSortTraits {
Robert Phillips4dbff752020-11-18 12:16:31 -0500199 static uint32_t GetIndex(GrRenderTask* renderTask) {
200 return renderTask->getIndex();
201 }
202 static void Output(GrRenderTask* renderTask, uint32_t index) {
203 renderTask->setIndex(index);
Chris Dalton6b498102019-08-01 14:14:52 -0600204 renderTask->setFlag(kWasOutput_Flag);
205 }
206 static bool WasOutput(const GrRenderTask* renderTask) {
207 return renderTask->isSetFlag(kWasOutput_Flag);
208 }
209 static void SetTempMark(GrRenderTask* renderTask) {
210 renderTask->setFlag(kTempMark_Flag);
211 }
212 static void ResetTempMark(GrRenderTask* renderTask) {
213 renderTask->resetFlag(kTempMark_Flag);
214 }
215 static bool IsTempMarked(const GrRenderTask* renderTask) {
216 return renderTask->isSetFlag(kTempMark_Flag);
217 }
218 static int NumDependencies(const GrRenderTask* renderTask) {
219 return renderTask->fDependencies.count();
220 }
221 static GrRenderTask* Dependency(GrRenderTask* renderTask, int index) {
222 return renderTask->fDependencies[index];
223 }
224 };
225
Robert Phillips07f675d2020-11-16 13:44:01 -0500226
227 virtual void onPrePrepare(GrRecordingContext*) {} // Only the GrOpsTask currently overrides this
228 virtual void onPrepare(GrOpFlushState*) {} // Only GrOpsTask and GrDDLTask override this virtual
Chris Dalton6b498102019-08-01 14:14:52 -0600229 virtual bool onExecute(GrOpFlushState* flushState) = 0;
230
231 const uint32_t fUniqueID;
232 uint32_t fFlags;
233
Greg Danielf41b2bd2019-08-22 16:19:24 -0400234 // 'this' GrRenderTask relies on the output of the GrRenderTasks in 'fDependencies'
Chris Dalton6b498102019-08-01 14:14:52 -0600235 SkSTArray<1, GrRenderTask*, true> fDependencies;
Greg Danielf41b2bd2019-08-22 16:19:24 -0400236 // 'this' GrRenderTask's output is relied on by the GrRenderTasks in 'fDependents'
Chris Dalton6b498102019-08-01 14:14:52 -0600237 SkSTArray<1, GrRenderTask*, true> fDependents;
Chris Daltone2a903e2019-09-18 13:41:50 -0600238
239 // For performance reasons, we should perform texture resolves back-to-back as much as possible.
240 // (http://skbug.com/9406). To accomplish this, we make and reuse one single resolve task for
241 // each render task, then add it as a dependency during makeClosed().
242 GrTextureResolveRenderTask* fTextureResolveTask = nullptr;
Adlai Holler96ead542020-06-26 08:50:14 -0400243
244 SkDEBUGCODE(GrDrawingManager *fDrawingMgr = nullptr;)
Chris Dalton6b498102019-08-01 14:14:52 -0600245};
246
247#endif