blob: 2ad46567088bc46a398ecfc0d6a34c060fd1c566 [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 Dalton6b498102019-08-01 14:14:52 -060020
21// This class abstracts a task that targets a single GrSurfaceProxy, participates in the
22// GrDrawingManager's DAG, and implements the onExecute method to modify its target proxy's
Greg Danielf41b2bd2019-08-22 16:19:24 -040023// contents. (e.g., an opsTask that executes a command buffer, a task to regenerate mipmaps, etc.)
Chris Dalton6b498102019-08-01 14:14:52 -060024class GrRenderTask : public SkRefCnt {
25public:
26 GrRenderTask(sk_sp<GrSurfaceProxy> target);
27 ~GrRenderTask() override;
28
Chris Daltonaa3cbb82019-08-21 00:01:21 -060029 void makeClosed(const GrCaps&);
30
Chris Dalton6b498102019-08-01 14:14:52 -060031 // These two methods are only invoked at flush time
32 void prepare(GrOpFlushState* flushState);
33 bool execute(GrOpFlushState* flushState) { return this->onExecute(flushState); }
34
Chris Dalton6b498102019-08-01 14:14:52 -060035 // Called when this class will survive a flush and needs to truncate its ops and start over.
36 // TODO: ultimately it should be invalid for an op list to survive a flush.
37 // https://bugs.chromium.org/p/skia/issues/detail?id=7111
38 virtual void endFlush() {}
39
40 bool isClosed() const { return this->isSetFlag(kClosed_Flag); }
41
42 /*
43 * Notify this GrRenderTask that it relies on the contents of 'dependedOn'
44 */
Chris Dalton08755122019-08-05 16:13:47 -060045 void addDependency(GrSurfaceProxy* dependedOn, GrMipMapped, GrTextureResolveManager,
46 const GrCaps& caps);
Chris Dalton6b498102019-08-01 14:14:52 -060047
48 /*
Greg Danielc30f1a92019-09-06 15:28:58 -040049 * Notify this GrRenderTask that it relies on the contents of all GrRenderTasks which otherTask
50 * depends on.
51 */
52 void addDependenciesFromOtherTask(GrRenderTask* otherTask);
53
54 /*
Chris Dalton6b498102019-08-01 14:14:52 -060055 * Does this renderTask depend on 'dependedOn'?
56 */
57 bool dependsOn(const GrRenderTask* dependedOn) const;
58
59 uint32_t uniqueID() const { return fUniqueID; }
60
61 /*
Greg Danielf41b2bd2019-08-22 16:19:24 -040062 * Safely cast this GrRenderTask to a GrOpsTask (if possible).
Chris Dalton6b498102019-08-01 14:14:52 -060063 */
Greg Danielf41b2bd2019-08-22 16:19:24 -040064 virtual GrOpsTask* asOpsTask() { return nullptr; }
Chris Dalton6b498102019-08-01 14:14:52 -060065
Chris Daltonc4b47352019-08-23 10:10:36 -060066#ifdef SK_DEBUG
Chris Dalton6b498102019-08-01 14:14:52 -060067 /*
68 * Dump out the GrRenderTask dependency DAG
69 */
Chris Daltonc4b47352019-08-23 10:10:36 -060070 virtual void dump(bool printDependencies) const;
Chris Dalton6b498102019-08-01 14:14:52 -060071
Chris Daltonc4b47352019-08-23 10:10:36 -060072 virtual int numClips() const { return 0; }
73
Greg Danieldcf9ca12019-08-27 14:30:21 -040074 using VisitSurfaceProxyFunc = std::function<void(GrSurfaceProxy*, GrMipMapped)>;
Chris Daltonc4b47352019-08-23 10:10:36 -060075
Greg Danieldcf9ca12019-08-27 14:30:21 -040076 virtual void visitProxies_debugOnly(const VisitSurfaceProxyFunc&) const = 0;
77
78 void visitTargetAndSrcProxies_debugOnly(const VisitSurfaceProxyFunc& fn) const {
Chris Daltonc4b47352019-08-23 10:10:36 -060079 this->visitProxies_debugOnly(fn);
80 fn(fTarget.get(), GrMipMapped::kNo);
81 }
82#endif
Chris Dalton6b498102019-08-01 14:14:52 -060083
84protected:
85 // In addition to just the GrSurface being allocated, has the stencil buffer been allocated (if
86 // it is required)?
87 bool isInstantiated() const;
88
89 SkDEBUGCODE(bool deferredProxiesAreInstantiated() const;)
90
Chris Daltonaa3cbb82019-08-21 00:01:21 -060091 enum class ExpectedOutcome : bool {
92 kTargetUnchanged,
93 kTargetDirty,
94 };
95
96 virtual ExpectedOutcome onMakeClosed(const GrCaps&) = 0;
97
Chris Dalton6b498102019-08-01 14:14:52 -060098 sk_sp<GrSurfaceProxy> fTarget;
99
100 // List of texture proxies whose contents are being prepared on a worker thread
101 // TODO: this list exists so we can fire off the proper upload when an renderTask begins
102 // executing. Can this be replaced?
103 SkTArray<GrTextureProxy*, true> fDeferredProxies;
104
105private:
106 // for resetFlag, TopoSortTraits, gatherProxyIntervals, handleInternalAllocationFailure
107 friend class GrDrawingManager;
108
109 // Drops any pending operations that reference proxies that are not instantiated.
110 // NOTE: Derived classes don't need to check fTarget. That is handled when the drawingManager
111 // calls isInstantiated.
112 virtual void handleInternalAllocationFailure() = 0;
113
114 virtual bool onIsUsed(GrSurfaceProxy*) const = 0;
115
116 bool isUsed(GrSurfaceProxy* proxy) const {
117 if (proxy == fTarget.get()) {
118 return true;
119 }
120
121 return this->onIsUsed(proxy);
122 }
123
124 void addDependency(GrRenderTask* dependedOn);
125 void addDependent(GrRenderTask* dependent);
126 SkDEBUGCODE(bool isDependedent(const GrRenderTask* dependent) const;)
127 SkDEBUGCODE(void validate() const;)
128 void closeThoseWhoDependOnMe(const GrCaps&);
129
130 // Feed proxy usage intervals to the GrResourceAllocator class
131 virtual void gatherProxyIntervals(GrResourceAllocator*) const = 0;
132
133 static uint32_t CreateUniqueID();
134
135 enum Flags {
136 kClosed_Flag = 0x01, //!< This GrRenderTask can't accept any more dependencies.
137
138 kWasOutput_Flag = 0x02, //!< Flag for topological sorting
139 kTempMark_Flag = 0x04, //!< Flag for topological sorting
140 };
141
142 void setFlag(uint32_t flag) {
143 fFlags |= flag;
144 }
145
146 void resetFlag(uint32_t flag) {
147 fFlags &= ~flag;
148 }
149
150 bool isSetFlag(uint32_t flag) const {
151 return SkToBool(fFlags & flag);
152 }
153
154 struct TopoSortTraits {
155 static void Output(GrRenderTask* renderTask, int /* index */) {
156 renderTask->setFlag(kWasOutput_Flag);
157 }
158 static bool WasOutput(const GrRenderTask* renderTask) {
159 return renderTask->isSetFlag(kWasOutput_Flag);
160 }
161 static void SetTempMark(GrRenderTask* renderTask) {
162 renderTask->setFlag(kTempMark_Flag);
163 }
164 static void ResetTempMark(GrRenderTask* renderTask) {
165 renderTask->resetFlag(kTempMark_Flag);
166 }
167 static bool IsTempMarked(const GrRenderTask* renderTask) {
168 return renderTask->isSetFlag(kTempMark_Flag);
169 }
170 static int NumDependencies(const GrRenderTask* renderTask) {
171 return renderTask->fDependencies.count();
172 }
173 static GrRenderTask* Dependency(GrRenderTask* renderTask, int index) {
174 return renderTask->fDependencies[index];
175 }
176 };
177
178 virtual void onPrepare(GrOpFlushState* flushState) = 0;
179 virtual bool onExecute(GrOpFlushState* flushState) = 0;
180
181 const uint32_t fUniqueID;
182 uint32_t fFlags;
183
Greg Danielf41b2bd2019-08-22 16:19:24 -0400184 // 'this' GrRenderTask relies on the output of the GrRenderTasks in 'fDependencies'
Chris Dalton6b498102019-08-01 14:14:52 -0600185 SkSTArray<1, GrRenderTask*, true> fDependencies;
Greg Danielf41b2bd2019-08-22 16:19:24 -0400186 // 'this' GrRenderTask's output is relied on by the GrRenderTasks in 'fDependents'
Chris Dalton6b498102019-08-01 14:14:52 -0600187 SkSTArray<1, GrRenderTask*, true> fDependents;
188};
189
190#endif