blob: 2f543c4fecc189a799e0769b11c7dcb3a648c054 [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
Chris Dalton6b498102019-08-01 14:14:52 -060040 // Called when this class will survive a flush and needs to truncate its ops and start over.
41 // TODO: ultimately it should be invalid for an op list to survive a flush.
42 // https://bugs.chromium.org/p/skia/issues/detail?id=7111
Adlai Hollerd71b7b02020-06-08 15:55:00 -040043 virtual void endFlush(GrDrawingManager*) {}
44
45 // This method "disowns" all the GrSurfaceProxies this RenderTask modifies. In
46 // practice this just means telling the drawingManager to forget the relevant
47 // mappings from surface proxy to last modifying rendertask.
48 virtual void disown(GrDrawingManager*);
Chris Dalton6b498102019-08-01 14:14:52 -060049
50 bool isClosed() const { return this->isSetFlag(kClosed_Flag); }
51
52 /*
53 * Notify this GrRenderTask that it relies on the contents of 'dependedOn'
54 */
Brian Salomon7e67dca2020-07-21 09:27:25 -040055 void addDependency(GrDrawingManager*, GrSurfaceProxy* dependedOn, GrMipmapped,
Adlai Hollerd71b7b02020-06-08 15:55:00 -040056 GrTextureResolveManager, const GrCaps& caps);
Chris Dalton6b498102019-08-01 14:14:52 -060057
58 /*
Greg Danielc30f1a92019-09-06 15:28:58 -040059 * Notify this GrRenderTask that it relies on the contents of all GrRenderTasks which otherTask
60 * depends on.
61 */
62 void addDependenciesFromOtherTask(GrRenderTask* otherTask);
63
64 /*
Chris Dalton6b498102019-08-01 14:14:52 -060065 * Does this renderTask depend on 'dependedOn'?
66 */
67 bool dependsOn(const GrRenderTask* dependedOn) const;
68
69 uint32_t uniqueID() const { return fUniqueID; }
Adlai Holler33d569e2020-06-16 14:30:08 -040070 int numTargets() const { return fTargets.count(); }
71 const GrSurfaceProxyView& target(int i) const { return fTargets[i]; }
Chris Dalton6b498102019-08-01 14:14:52 -060072
73 /*
Greg Danielf41b2bd2019-08-22 16:19:24 -040074 * Safely cast this GrRenderTask to a GrOpsTask (if possible).
Chris Dalton6b498102019-08-01 14:14:52 -060075 */
Greg Danielf41b2bd2019-08-22 16:19:24 -040076 virtual GrOpsTask* asOpsTask() { return nullptr; }
Chris Dalton6b498102019-08-01 14:14:52 -060077
Chris Daltonc4b47352019-08-23 10:10:36 -060078#ifdef SK_DEBUG
Chris Dalton6b498102019-08-01 14:14:52 -060079 /*
80 * Dump out the GrRenderTask dependency DAG
81 */
Chris Daltonc4b47352019-08-23 10:10:36 -060082 virtual void dump(bool printDependencies) const;
Robert Phillips44e2c5f2020-04-14 13:00:04 -040083 virtual const char* name() const = 0;
Chris Dalton6b498102019-08-01 14:14:52 -060084
Chris Daltonc4b47352019-08-23 10:10:36 -060085 virtual int numClips() const { return 0; }
86
Michael Ludwigfcdd0612019-11-25 08:34:31 -050087 virtual void visitProxies_debugOnly(const GrOp::VisitProxyFunc&) const = 0;
Chris Daltonc4b47352019-08-23 10:10:36 -060088
Michael Ludwigfcdd0612019-11-25 08:34:31 -050089 void visitTargetAndSrcProxies_debugOnly(const GrOp::VisitProxyFunc& fn) const {
Chris Daltonc4b47352019-08-23 10:10:36 -060090 this->visitProxies_debugOnly(fn);
Adlai Holler33d569e2020-06-16 14:30:08 -040091 for (int i = 0; i < this->numTargets(); ++i) {
Brian Salomon7e67dca2020-07-21 09:27:25 -040092 fn(this->target(i).proxy(), GrMipmapped::kNo);
Chris Daltone2a903e2019-09-18 13:41:50 -060093 }
Chris Daltonc4b47352019-08-23 10:10:36 -060094 }
95#endif
Chris Dalton6b498102019-08-01 14:14:52 -060096
97protected:
98 // In addition to just the GrSurface being allocated, has the stencil buffer been allocated (if
99 // it is required)?
100 bool isInstantiated() const;
101
102 SkDEBUGCODE(bool deferredProxiesAreInstantiated() const;)
103
Adlai Holler33d569e2020-06-16 14:30:08 -0400104 // Add a target surface proxy to the list of targets for this task.
105 // This also informs the drawing manager to update the lastRenderTask association.
106 void addTarget(GrDrawingManager*, GrSurfaceProxyView);
107
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600108 enum class ExpectedOutcome : bool {
109 kTargetUnchanged,
110 kTargetDirty,
111 };
112
Chris Dalton16a33c62019-09-24 22:19:17 -0600113 // Performs any work to finalize this renderTask prior to execution. If returning
114 // ExpectedOutcome::kTargetDiry, the caller is also responsible to fill out the area it will
115 // modify in targetUpdateBounds.
116 //
117 // targetUpdateBounds must not extend beyond the proxy bounds.
118 virtual ExpectedOutcome onMakeClosed(const GrCaps&, SkIRect* targetUpdateBounds) = 0;
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600119
Adlai Holler33d569e2020-06-16 14:30:08 -0400120 SkSTArray<1, GrSurfaceProxyView> fTargets;
Chris Dalton6b498102019-08-01 14:14:52 -0600121
122 // List of texture proxies whose contents are being prepared on a worker thread
123 // TODO: this list exists so we can fire off the proper upload when an renderTask begins
124 // executing. Can this be replaced?
125 SkTArray<GrTextureProxy*, true> fDeferredProxies;
126
127private:
128 // for resetFlag, TopoSortTraits, gatherProxyIntervals, handleInternalAllocationFailure
129 friend class GrDrawingManager;
130
131 // Drops any pending operations that reference proxies that are not instantiated.
Adlai Holler33d569e2020-06-16 14:30:08 -0400132 // NOTE: Derived classes don't need to check targets. That is handled when the
Greg Daniel16f5c652019-10-29 11:26:01 -0400133 // drawingManager calls isInstantiated.
Chris Dalton6b498102019-08-01 14:14:52 -0600134 virtual void handleInternalAllocationFailure() = 0;
135
Adlai Holler33d569e2020-06-16 14:30:08 -0400136 // Derived classes can override to indicate usage of proxies _other than target proxies_.
137 // GrRenderTask itself will handle checking the target proxies.
Chris Dalton6b498102019-08-01 14:14:52 -0600138 virtual bool onIsUsed(GrSurfaceProxy*) const = 0;
139
140 bool isUsed(GrSurfaceProxy* proxy) const {
Adlai Holler33d569e2020-06-16 14:30:08 -0400141 for (const GrSurfaceProxyView& target : fTargets) {
142 if (target.proxy() == proxy) {
143 return true;
144 }
Chris Dalton6b498102019-08-01 14:14:52 -0600145 }
146
147 return this->onIsUsed(proxy);
148 }
149
150 void addDependency(GrRenderTask* dependedOn);
151 void addDependent(GrRenderTask* dependent);
152 SkDEBUGCODE(bool isDependedent(const GrRenderTask* dependent) const;)
153 SkDEBUGCODE(void validate() const;)
154 void closeThoseWhoDependOnMe(const GrCaps&);
155
156 // Feed proxy usage intervals to the GrResourceAllocator class
157 virtual void gatherProxyIntervals(GrResourceAllocator*) const = 0;
158
159 static uint32_t CreateUniqueID();
160
161 enum Flags {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400162 kClosed_Flag = 0x01, //!< This task can't accept any more dependencies.
163 kDisowned_Flag = 0x02, //!< This task is disowned by its creating GrDrawingManager.
Chris Dalton6b498102019-08-01 14:14:52 -0600164
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400165 kWasOutput_Flag = 0x04, //!< Flag for topological sorting
166 kTempMark_Flag = 0x08, //!< Flag for topological sorting
Chris Dalton6b498102019-08-01 14:14:52 -0600167 };
168
169 void setFlag(uint32_t flag) {
170 fFlags |= flag;
171 }
172
173 void resetFlag(uint32_t flag) {
174 fFlags &= ~flag;
175 }
176
177 bool isSetFlag(uint32_t flag) const {
178 return SkToBool(fFlags & flag);
179 }
180
181 struct TopoSortTraits {
182 static void Output(GrRenderTask* renderTask, int /* index */) {
183 renderTask->setFlag(kWasOutput_Flag);
184 }
185 static bool WasOutput(const GrRenderTask* renderTask) {
186 return renderTask->isSetFlag(kWasOutput_Flag);
187 }
188 static void SetTempMark(GrRenderTask* renderTask) {
189 renderTask->setFlag(kTempMark_Flag);
190 }
191 static void ResetTempMark(GrRenderTask* renderTask) {
192 renderTask->resetFlag(kTempMark_Flag);
193 }
194 static bool IsTempMarked(const GrRenderTask* renderTask) {
195 return renderTask->isSetFlag(kTempMark_Flag);
196 }
197 static int NumDependencies(const GrRenderTask* renderTask) {
198 return renderTask->fDependencies.count();
199 }
200 static GrRenderTask* Dependency(GrRenderTask* renderTask, int index) {
201 return renderTask->fDependencies[index];
202 }
203 };
204
Robert Phillips29f38542019-10-16 09:20:25 -0400205 // Only the GrOpsTask currently overrides this virtual
206 virtual void onPrePrepare(GrRecordingContext*) {}
Robert Phillips7327c9d2019-10-08 16:32:56 -0400207 virtual void onPrepare(GrOpFlushState*) {} // Only the GrOpsTask overrides this virtual
Chris Dalton6b498102019-08-01 14:14:52 -0600208 virtual bool onExecute(GrOpFlushState* flushState) = 0;
209
210 const uint32_t fUniqueID;
211 uint32_t fFlags;
212
Greg Danielf41b2bd2019-08-22 16:19:24 -0400213 // 'this' GrRenderTask relies on the output of the GrRenderTasks in 'fDependencies'
Chris Dalton6b498102019-08-01 14:14:52 -0600214 SkSTArray<1, GrRenderTask*, true> fDependencies;
Greg Danielf41b2bd2019-08-22 16:19:24 -0400215 // 'this' GrRenderTask's output is relied on by the GrRenderTasks in 'fDependents'
Chris Dalton6b498102019-08-01 14:14:52 -0600216 SkSTArray<1, GrRenderTask*, true> fDependents;
Chris Daltone2a903e2019-09-18 13:41:50 -0600217
218 // For performance reasons, we should perform texture resolves back-to-back as much as possible.
219 // (http://skbug.com/9406). To accomplish this, we make and reuse one single resolve task for
220 // each render task, then add it as a dependency during makeClosed().
221 GrTextureResolveRenderTask* fTextureResolveTask = nullptr;
Adlai Holler96ead542020-06-26 08:50:14 -0400222
223 SkDEBUGCODE(GrDrawingManager *fDrawingMgr = nullptr;)
Chris Dalton6b498102019-08-01 14:14:52 -0600224};
225
226#endif