blob: 83f17d902436d2f795e025cfcf215db8c8dd1039 [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"
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();
29 GrRenderTask(GrSurfaceProxyView);
Chris Dalton6b498102019-08-01 14:14:52 -060030 ~GrRenderTask() override;
31
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
43 virtual void endFlush() {}
44
45 bool isClosed() const { return this->isSetFlag(kClosed_Flag); }
46
47 /*
48 * Notify this GrRenderTask that it relies on the contents of 'dependedOn'
49 */
Chris Dalton08755122019-08-05 16:13:47 -060050 void addDependency(GrSurfaceProxy* dependedOn, GrMipMapped, GrTextureResolveManager,
51 const GrCaps& caps);
Chris Dalton6b498102019-08-01 14:14:52 -060052
53 /*
Greg Danielc30f1a92019-09-06 15:28:58 -040054 * Notify this GrRenderTask that it relies on the contents of all GrRenderTasks which otherTask
55 * depends on.
56 */
57 void addDependenciesFromOtherTask(GrRenderTask* otherTask);
58
59 /*
Chris Dalton6b498102019-08-01 14:14:52 -060060 * Does this renderTask depend on 'dependedOn'?
61 */
62 bool dependsOn(const GrRenderTask* dependedOn) const;
63
64 uint32_t uniqueID() const { return fUniqueID; }
65
66 /*
Greg Danielf41b2bd2019-08-22 16:19:24 -040067 * Safely cast this GrRenderTask to a GrOpsTask (if possible).
Chris Dalton6b498102019-08-01 14:14:52 -060068 */
Greg Danielf41b2bd2019-08-22 16:19:24 -040069 virtual GrOpsTask* asOpsTask() { return nullptr; }
Chris Dalton6b498102019-08-01 14:14:52 -060070
Chris Daltonc4b47352019-08-23 10:10:36 -060071#ifdef SK_DEBUG
Chris Dalton6b498102019-08-01 14:14:52 -060072 /*
73 * Dump out the GrRenderTask dependency DAG
74 */
Chris Daltonc4b47352019-08-23 10:10:36 -060075 virtual void dump(bool printDependencies) const;
Robert Phillips44e2c5f2020-04-14 13:00:04 -040076 virtual const char* name() const = 0;
Chris Dalton6b498102019-08-01 14:14:52 -060077
Chris Daltonc4b47352019-08-23 10:10:36 -060078 virtual int numClips() const { return 0; }
79
Michael Ludwigfcdd0612019-11-25 08:34:31 -050080 virtual void visitProxies_debugOnly(const GrOp::VisitProxyFunc&) const = 0;
Chris Daltonc4b47352019-08-23 10:10:36 -060081
Michael Ludwigfcdd0612019-11-25 08:34:31 -050082 void visitTargetAndSrcProxies_debugOnly(const GrOp::VisitProxyFunc& fn) const {
Chris Daltonc4b47352019-08-23 10:10:36 -060083 this->visitProxies_debugOnly(fn);
Greg Daniel16f5c652019-10-29 11:26:01 -040084 if (fTargetView.proxy()) {
85 fn(fTargetView.proxy(), GrMipMapped::kNo);
Chris Daltone2a903e2019-09-18 13:41:50 -060086 }
Chris Daltonc4b47352019-08-23 10:10:36 -060087 }
88#endif
Chris Dalton6b498102019-08-01 14:14:52 -060089
90protected:
91 // In addition to just the GrSurface being allocated, has the stencil buffer been allocated (if
92 // it is required)?
93 bool isInstantiated() const;
94
95 SkDEBUGCODE(bool deferredProxiesAreInstantiated() const;)
96
Chris Daltonaa3cbb82019-08-21 00:01:21 -060097 enum class ExpectedOutcome : bool {
98 kTargetUnchanged,
99 kTargetDirty,
100 };
101
Chris Dalton16a33c62019-09-24 22:19:17 -0600102 // Performs any work to finalize this renderTask prior to execution. If returning
103 // ExpectedOutcome::kTargetDiry, the caller is also responsible to fill out the area it will
104 // modify in targetUpdateBounds.
105 //
106 // targetUpdateBounds must not extend beyond the proxy bounds.
107 virtual ExpectedOutcome onMakeClosed(const GrCaps&, SkIRect* targetUpdateBounds) = 0;
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600108
Greg Daniel16f5c652019-10-29 11:26:01 -0400109 GrSurfaceProxyView fTargetView;
Chris Dalton6b498102019-08-01 14:14:52 -0600110
111 // List of texture proxies whose contents are being prepared on a worker thread
112 // TODO: this list exists so we can fire off the proper upload when an renderTask begins
113 // executing. Can this be replaced?
114 SkTArray<GrTextureProxy*, true> fDeferredProxies;
115
116private:
117 // for resetFlag, TopoSortTraits, gatherProxyIntervals, handleInternalAllocationFailure
118 friend class GrDrawingManager;
119
120 // Drops any pending operations that reference proxies that are not instantiated.
Greg Daniel16f5c652019-10-29 11:26:01 -0400121 // NOTE: Derived classes don't need to check fTargetView. That is handled when the
122 // drawingManager calls isInstantiated.
Chris Dalton6b498102019-08-01 14:14:52 -0600123 virtual void handleInternalAllocationFailure() = 0;
124
125 virtual bool onIsUsed(GrSurfaceProxy*) const = 0;
126
127 bool isUsed(GrSurfaceProxy* proxy) const {
Greg Daniel16f5c652019-10-29 11:26:01 -0400128 if (proxy == fTargetView.proxy()) {
Chris Dalton6b498102019-08-01 14:14:52 -0600129 return true;
130 }
131
132 return this->onIsUsed(proxy);
133 }
134
135 void addDependency(GrRenderTask* dependedOn);
136 void addDependent(GrRenderTask* dependent);
137 SkDEBUGCODE(bool isDependedent(const GrRenderTask* dependent) const;)
138 SkDEBUGCODE(void validate() const;)
139 void closeThoseWhoDependOnMe(const GrCaps&);
140
141 // Feed proxy usage intervals to the GrResourceAllocator class
142 virtual void gatherProxyIntervals(GrResourceAllocator*) const = 0;
143
144 static uint32_t CreateUniqueID();
145
146 enum Flags {
147 kClosed_Flag = 0x01, //!< This GrRenderTask can't accept any more dependencies.
148
149 kWasOutput_Flag = 0x02, //!< Flag for topological sorting
150 kTempMark_Flag = 0x04, //!< Flag for topological sorting
151 };
152
153 void setFlag(uint32_t flag) {
154 fFlags |= flag;
155 }
156
157 void resetFlag(uint32_t flag) {
158 fFlags &= ~flag;
159 }
160
161 bool isSetFlag(uint32_t flag) const {
162 return SkToBool(fFlags & flag);
163 }
164
165 struct TopoSortTraits {
166 static void Output(GrRenderTask* renderTask, int /* index */) {
167 renderTask->setFlag(kWasOutput_Flag);
168 }
169 static bool WasOutput(const GrRenderTask* renderTask) {
170 return renderTask->isSetFlag(kWasOutput_Flag);
171 }
172 static void SetTempMark(GrRenderTask* renderTask) {
173 renderTask->setFlag(kTempMark_Flag);
174 }
175 static void ResetTempMark(GrRenderTask* renderTask) {
176 renderTask->resetFlag(kTempMark_Flag);
177 }
178 static bool IsTempMarked(const GrRenderTask* renderTask) {
179 return renderTask->isSetFlag(kTempMark_Flag);
180 }
181 static int NumDependencies(const GrRenderTask* renderTask) {
182 return renderTask->fDependencies.count();
183 }
184 static GrRenderTask* Dependency(GrRenderTask* renderTask, int index) {
185 return renderTask->fDependencies[index];
186 }
187 };
188
Robert Phillips29f38542019-10-16 09:20:25 -0400189 // Only the GrOpsTask currently overrides this virtual
190 virtual void onPrePrepare(GrRecordingContext*) {}
Robert Phillips7327c9d2019-10-08 16:32:56 -0400191 virtual void onPrepare(GrOpFlushState*) {} // Only the GrOpsTask overrides this virtual
Chris Dalton6b498102019-08-01 14:14:52 -0600192 virtual bool onExecute(GrOpFlushState* flushState) = 0;
193
194 const uint32_t fUniqueID;
195 uint32_t fFlags;
196
Greg Danielf41b2bd2019-08-22 16:19:24 -0400197 // 'this' GrRenderTask relies on the output of the GrRenderTasks in 'fDependencies'
Chris Dalton6b498102019-08-01 14:14:52 -0600198 SkSTArray<1, GrRenderTask*, true> fDependencies;
Greg Danielf41b2bd2019-08-22 16:19:24 -0400199 // 'this' GrRenderTask's output is relied on by the GrRenderTasks in 'fDependents'
Chris Dalton6b498102019-08-01 14:14:52 -0600200 SkSTArray<1, GrRenderTask*, true> fDependents;
Chris Daltone2a903e2019-09-18 13:41:50 -0600201
202 // For performance reasons, we should perform texture resolves back-to-back as much as possible.
203 // (http://skbug.com/9406). To accomplish this, we make and reuse one single resolve task for
204 // each render task, then add it as a dependency during makeClosed().
205 GrTextureResolveRenderTask* fTextureResolveTask = nullptr;
Chris Dalton6b498102019-08-01 14:14:52 -0600206};
207
208#endif