1/*
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"
14#include "src/gpu/GrSurfaceProxyView.h"
15#include "src/gpu/GrTextureProxy.h"
16#include "src/gpu/GrTextureResolveManager.h"
17
18class GrOpFlushState;
19class GrOpsTask;
20class GrResourceAllocator;
21class GrTextureResolveRenderTask;
22
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
25// contents. (e.g., an opsTask that executes a command buffer, a task to regenerate mipmaps, etc.)
26class GrRenderTask : public SkRefCnt {
27public:
28 GrRenderTask();
29 GrRenderTask(GrSurfaceProxyView);
30 ~GrRenderTask() override;
31
32 void makeClosed(const GrCaps&);
33
34 void prePrepare(GrRecordingContext* context) { this->onPrePrepare(context); }
35
36 // These two methods are only invoked at flush time
37 void prepare(GrOpFlushState* flushState);
38 bool execute(GrOpFlushState* flushState) { return this->onExecute(flushState); }
39
40 // 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 */
50 void addDependency(GrSurfaceProxy* dependedOn, GrMipMapped, GrTextureResolveManager,
51 const GrCaps& caps);
52
53 /*
54 * 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 /*
60 * Does this renderTask depend on 'dependedOn'?
61 */
62 bool dependsOn(const GrRenderTask* dependedOn) const;
63
64 uint32_t uniqueID() const { return fUniqueID; }
65
66 /*
67 * Safely cast this GrRenderTask to a GrOpsTask (if possible).
68 */
69 virtual GrOpsTask* asOpsTask() { return nullptr; }
70
71#ifdef SK_DEBUG
72 /*
73 * Dump out the GrRenderTask dependency DAG
74 */
75 virtual void dump(bool printDependencies) const;
76
77 virtual int numClips() const { return 0; }
78
79 virtual void visitProxies_debugOnly(const GrOp::VisitProxyFunc&) const = 0;
80
81 void visitTargetAndSrcProxies_debugOnly(const GrOp::VisitProxyFunc& fn) const {
82 this->visitProxies_debugOnly(fn);
83 if (fTargetView.proxy()) {
84 fn(fTargetView.proxy(), GrMipMapped::kNo);
85 }
86 }
87#endif
88
89protected:
90 // In addition to just the GrSurface being allocated, has the stencil buffer been allocated (if
91 // it is required)?
92 bool isInstantiated() const;
93
94 SkDEBUGCODE(bool deferredProxiesAreInstantiated() const;)
95
96 enum class ExpectedOutcome : bool {
97 kTargetUnchanged,
98 kTargetDirty,
99 };
100
101 // Performs any work to finalize this renderTask prior to execution. If returning
102 // ExpectedOutcome::kTargetDiry, the caller is also responsible to fill out the area it will
103 // modify in targetUpdateBounds.
104 //
105 // targetUpdateBounds must not extend beyond the proxy bounds.
106 virtual ExpectedOutcome onMakeClosed(const GrCaps&, SkIRect* targetUpdateBounds) = 0;
107
108 GrSurfaceProxyView fTargetView;
109
110 // List of texture proxies whose contents are being prepared on a worker thread
111 // TODO: this list exists so we can fire off the proper upload when an renderTask begins
112 // executing. Can this be replaced?
113 SkTArray<GrTextureProxy*, true> fDeferredProxies;
114
115private:
116 // for resetFlag, TopoSortTraits, gatherProxyIntervals, handleInternalAllocationFailure
117 friend class GrDrawingManager;
118
119 // Drops any pending operations that reference proxies that are not instantiated.
120 // NOTE: Derived classes don't need to check fTargetView. That is handled when the
121 // drawingManager calls isInstantiated.
122 virtual void handleInternalAllocationFailure() = 0;
123
124 virtual bool onIsUsed(GrSurfaceProxy*) const = 0;
125
126 bool isUsed(GrSurfaceProxy* proxy) const {
127 if (proxy == fTargetView.proxy()) {
128 return true;
129 }
130
131 return this->onIsUsed(proxy);
132 }
133
134 void addDependency(GrRenderTask* dependedOn);
135 void addDependent(GrRenderTask* dependent);
136 SkDEBUGCODE(bool isDependedent(const GrRenderTask* dependent) const;)
137 SkDEBUGCODE(void validate() const;)
138 void closeThoseWhoDependOnMe(const GrCaps&);
139
140 // Feed proxy usage intervals to the GrResourceAllocator class
141 virtual void gatherProxyIntervals(GrResourceAllocator*) const = 0;
142
143 static uint32_t CreateUniqueID();
144
145 enum Flags {
146 kClosed_Flag = 0x01, //!< This GrRenderTask can't accept any more dependencies.
147
148 kWasOutput_Flag = 0x02, //!< Flag for topological sorting
149 kTempMark_Flag = 0x04, //!< Flag for topological sorting
150 };
151
152 void setFlag(uint32_t flag) {
153 fFlags |= flag;
154 }
155
156 void resetFlag(uint32_t flag) {
157 fFlags &= ~flag;
158 }
159
160 bool isSetFlag(uint32_t flag) const {
161 return SkToBool(fFlags & flag);
162 }
163
164 struct TopoSortTraits {
165 static void Output(GrRenderTask* renderTask, int /* index */) {
166 renderTask->setFlag(kWasOutput_Flag);
167 }
168 static bool WasOutput(const GrRenderTask* renderTask) {
169 return renderTask->isSetFlag(kWasOutput_Flag);
170 }
171 static void SetTempMark(GrRenderTask* renderTask) {
172 renderTask->setFlag(kTempMark_Flag);
173 }
174 static void ResetTempMark(GrRenderTask* renderTask) {
175 renderTask->resetFlag(kTempMark_Flag);
176 }
177 static bool IsTempMarked(const GrRenderTask* renderTask) {
178 return renderTask->isSetFlag(kTempMark_Flag);
179 }
180 static int NumDependencies(const GrRenderTask* renderTask) {
181 return renderTask->fDependencies.count();
182 }
183 static GrRenderTask* Dependency(GrRenderTask* renderTask, int index) {
184 return renderTask->fDependencies[index];
185 }
186 };
187
188 // Only the GrOpsTask currently overrides this virtual
189 virtual void onPrePrepare(GrRecordingContext*) {}
190 virtual void onPrepare(GrOpFlushState*) {} // Only the GrOpsTask overrides this virtual
191 virtual bool onExecute(GrOpFlushState* flushState) = 0;
192
193 const uint32_t fUniqueID;
194 uint32_t fFlags;
195
196 // 'this' GrRenderTask relies on the output of the GrRenderTasks in 'fDependencies'
197 SkSTArray<1, GrRenderTask*, true> fDependencies;
198 // 'this' GrRenderTask's output is relied on by the GrRenderTasks in 'fDependents'
199 SkSTArray<1, GrRenderTask*, true> fDependents;
200
201 // For performance reasons, we should perform texture resolves back-to-back as much as possible.
202 // (http://skbug.com/9406). To accomplish this, we make and reuse one single resolve task for
203 // each render task, then add it as a dependency during makeClosed().
204 GrTextureResolveRenderTask* fTextureResolveTask = nullptr;
205};
206
207#endif
208