blob: 0cb006fe872d9bdd1eb14f9fd2344721f8b83fae [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"
14#include "src/gpu/GrTextureProxy.h"
Chris Dalton08755122019-08-05 16:13:47 -060015#include "src/gpu/GrTextureResolveManager.h"
Chris Dalton6b498102019-08-01 14:14:52 -060016
17class GrOpFlushState;
Greg Danielf41b2bd2019-08-22 16:19:24 -040018class GrOpsTask;
Chris Dalton6b498102019-08-01 14:14:52 -060019class GrResourceAllocator;
Chris Daltone2a903e2019-09-18 13:41:50 -060020class GrTextureResolveRenderTask;
Chris Dalton6b498102019-08-01 14:14:52 -060021
22// This class abstracts a task that targets a single GrSurfaceProxy, participates in the
23// GrDrawingManager's DAG, and implements the onExecute method to modify its target proxy's
Greg Danielf41b2bd2019-08-22 16:19:24 -040024// contents. (e.g., an opsTask that executes a command buffer, a task to regenerate mipmaps, etc.)
Chris Dalton6b498102019-08-01 14:14:52 -060025class GrRenderTask : public SkRefCnt {
26public:
27 GrRenderTask(sk_sp<GrSurfaceProxy> target);
28 ~GrRenderTask() override;
29
Chris Daltonaa3cbb82019-08-21 00:01:21 -060030 void makeClosed(const GrCaps&);
31
Chris Dalton6b498102019-08-01 14:14:52 -060032 // These two methods are only invoked at flush time
33 void prepare(GrOpFlushState* flushState);
34 bool execute(GrOpFlushState* flushState) { return this->onExecute(flushState); }
35
Chris Dalton6b498102019-08-01 14:14:52 -060036 // Called when this class will survive a flush and needs to truncate its ops and start over.
37 // TODO: ultimately it should be invalid for an op list to survive a flush.
38 // https://bugs.chromium.org/p/skia/issues/detail?id=7111
39 virtual void endFlush() {}
40
41 bool isClosed() const { return this->isSetFlag(kClosed_Flag); }
42
43 /*
44 * Notify this GrRenderTask that it relies on the contents of 'dependedOn'
45 */
Chris Dalton08755122019-08-05 16:13:47 -060046 void addDependency(GrSurfaceProxy* dependedOn, GrMipMapped, GrTextureResolveManager,
47 const GrCaps& caps);
Chris Dalton6b498102019-08-01 14:14:52 -060048
49 /*
Greg Danielc30f1a92019-09-06 15:28:58 -040050 * Notify this GrRenderTask that it relies on the contents of all GrRenderTasks which otherTask
51 * depends on.
52 */
53 void addDependenciesFromOtherTask(GrRenderTask* otherTask);
54
55 /*
Chris Dalton6b498102019-08-01 14:14:52 -060056 * Does this renderTask depend on 'dependedOn'?
57 */
58 bool dependsOn(const GrRenderTask* dependedOn) const;
59
60 uint32_t uniqueID() const { return fUniqueID; }
61
62 /*
Greg Danielf41b2bd2019-08-22 16:19:24 -040063 * Safely cast this GrRenderTask to a GrOpsTask (if possible).
Chris Dalton6b498102019-08-01 14:14:52 -060064 */
Greg Danielf41b2bd2019-08-22 16:19:24 -040065 virtual GrOpsTask* asOpsTask() { return nullptr; }
Chris Dalton6b498102019-08-01 14:14:52 -060066
Chris Daltonc4b47352019-08-23 10:10:36 -060067#ifdef SK_DEBUG
Chris Dalton6b498102019-08-01 14:14:52 -060068 /*
69 * Dump out the GrRenderTask dependency DAG
70 */
Chris Daltonc4b47352019-08-23 10:10:36 -060071 virtual void dump(bool printDependencies) const;
Chris Dalton6b498102019-08-01 14:14:52 -060072
Chris Daltonc4b47352019-08-23 10:10:36 -060073 virtual int numClips() const { return 0; }
74
Greg Danieldcf9ca12019-08-27 14:30:21 -040075 using VisitSurfaceProxyFunc = std::function<void(GrSurfaceProxy*, GrMipMapped)>;
Chris Daltonc4b47352019-08-23 10:10:36 -060076
Greg Danieldcf9ca12019-08-27 14:30:21 -040077 virtual void visitProxies_debugOnly(const VisitSurfaceProxyFunc&) const = 0;
78
79 void visitTargetAndSrcProxies_debugOnly(const VisitSurfaceProxyFunc& fn) const {
Chris Daltonc4b47352019-08-23 10:10:36 -060080 this->visitProxies_debugOnly(fn);
Chris Daltone2a903e2019-09-18 13:41:50 -060081 if (fTarget) {
82 fn(fTarget.get(), GrMipMapped::kNo);
83 }
Chris Daltonc4b47352019-08-23 10:10:36 -060084 }
85#endif
Chris Dalton6b498102019-08-01 14:14:52 -060086
87protected:
88 // In addition to just the GrSurface being allocated, has the stencil buffer been allocated (if
89 // it is required)?
90 bool isInstantiated() const;
91
92 SkDEBUGCODE(bool deferredProxiesAreInstantiated() const;)
93
Chris Daltonaa3cbb82019-08-21 00:01:21 -060094 enum class ExpectedOutcome : bool {
95 kTargetUnchanged,
96 kTargetDirty,
97 };
98
99 virtual ExpectedOutcome onMakeClosed(const GrCaps&) = 0;
100
Chris Dalton6b498102019-08-01 14:14:52 -0600101 sk_sp<GrSurfaceProxy> fTarget;
102
103 // List of texture proxies whose contents are being prepared on a worker thread
104 // TODO: this list exists so we can fire off the proper upload when an renderTask begins
105 // executing. Can this be replaced?
106 SkTArray<GrTextureProxy*, true> fDeferredProxies;
107
108private:
109 // for resetFlag, TopoSortTraits, gatherProxyIntervals, handleInternalAllocationFailure
110 friend class GrDrawingManager;
111
112 // Drops any pending operations that reference proxies that are not instantiated.
113 // NOTE: Derived classes don't need to check fTarget. That is handled when the drawingManager
114 // calls isInstantiated.
115 virtual void handleInternalAllocationFailure() = 0;
116
117 virtual bool onIsUsed(GrSurfaceProxy*) const = 0;
118
119 bool isUsed(GrSurfaceProxy* proxy) const {
120 if (proxy == fTarget.get()) {
121 return true;
122 }
123
124 return this->onIsUsed(proxy);
125 }
126
127 void addDependency(GrRenderTask* dependedOn);
128 void addDependent(GrRenderTask* dependent);
129 SkDEBUGCODE(bool isDependedent(const GrRenderTask* dependent) const;)
130 SkDEBUGCODE(void validate() const;)
131 void closeThoseWhoDependOnMe(const GrCaps&);
132
133 // Feed proxy usage intervals to the GrResourceAllocator class
134 virtual void gatherProxyIntervals(GrResourceAllocator*) const = 0;
135
136 static uint32_t CreateUniqueID();
137
138 enum Flags {
139 kClosed_Flag = 0x01, //!< This GrRenderTask can't accept any more dependencies.
140
141 kWasOutput_Flag = 0x02, //!< Flag for topological sorting
142 kTempMark_Flag = 0x04, //!< Flag for topological sorting
143 };
144
145 void setFlag(uint32_t flag) {
146 fFlags |= flag;
147 }
148
149 void resetFlag(uint32_t flag) {
150 fFlags &= ~flag;
151 }
152
153 bool isSetFlag(uint32_t flag) const {
154 return SkToBool(fFlags & flag);
155 }
156
157 struct TopoSortTraits {
158 static void Output(GrRenderTask* renderTask, int /* index */) {
159 renderTask->setFlag(kWasOutput_Flag);
160 }
161 static bool WasOutput(const GrRenderTask* renderTask) {
162 return renderTask->isSetFlag(kWasOutput_Flag);
163 }
164 static void SetTempMark(GrRenderTask* renderTask) {
165 renderTask->setFlag(kTempMark_Flag);
166 }
167 static void ResetTempMark(GrRenderTask* renderTask) {
168 renderTask->resetFlag(kTempMark_Flag);
169 }
170 static bool IsTempMarked(const GrRenderTask* renderTask) {
171 return renderTask->isSetFlag(kTempMark_Flag);
172 }
173 static int NumDependencies(const GrRenderTask* renderTask) {
174 return renderTask->fDependencies.count();
175 }
176 static GrRenderTask* Dependency(GrRenderTask* renderTask, int index) {
177 return renderTask->fDependencies[index];
178 }
179 };
180
181 virtual void onPrepare(GrOpFlushState* flushState) = 0;
182 virtual bool onExecute(GrOpFlushState* flushState) = 0;
183
184 const uint32_t fUniqueID;
185 uint32_t fFlags;
186
Greg Danielf41b2bd2019-08-22 16:19:24 -0400187 // 'this' GrRenderTask relies on the output of the GrRenderTasks in 'fDependencies'
Chris Dalton6b498102019-08-01 14:14:52 -0600188 SkSTArray<1, GrRenderTask*, true> fDependencies;
Greg Danielf41b2bd2019-08-22 16:19:24 -0400189 // 'this' GrRenderTask's output is relied on by the GrRenderTasks in 'fDependents'
Chris Dalton6b498102019-08-01 14:14:52 -0600190 SkSTArray<1, GrRenderTask*, true> fDependents;
Chris Daltone2a903e2019-09-18 13:41:50 -0600191
192 // For performance reasons, we should perform texture resolves back-to-back as much as possible.
193 // (http://skbug.com/9406). To accomplish this, we make and reuse one single resolve task for
194 // each render task, then add it as a dependency during makeClosed().
195 GrTextureResolveRenderTask* fTextureResolveTask = nullptr;
Chris Dalton6b498102019-08-01 14:14:52 -0600196};
197
198#endif