blob: 918055f38ff6aeff87729efd9ac747859b503c82 [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 /*
49 * Does this renderTask depend on 'dependedOn'?
50 */
51 bool dependsOn(const GrRenderTask* dependedOn) const;
52
53 uint32_t uniqueID() const { return fUniqueID; }
54
55 /*
Greg Danielf41b2bd2019-08-22 16:19:24 -040056 * Safely cast this GrRenderTask to a GrOpsTask (if possible).
Chris Dalton6b498102019-08-01 14:14:52 -060057 */
Greg Danielf41b2bd2019-08-22 16:19:24 -040058 virtual GrOpsTask* asOpsTask() { return nullptr; }
Chris Dalton6b498102019-08-01 14:14:52 -060059
Chris Daltonc4b47352019-08-23 10:10:36 -060060#ifdef SK_DEBUG
Chris Dalton6b498102019-08-01 14:14:52 -060061 /*
62 * Dump out the GrRenderTask dependency DAG
63 */
Chris Daltonc4b47352019-08-23 10:10:36 -060064 virtual void dump(bool printDependencies) const;
Chris Dalton6b498102019-08-01 14:14:52 -060065
Chris Daltonc4b47352019-08-23 10:10:36 -060066 virtual int numClips() const { return 0; }
67
Greg Danieldcf9ca12019-08-27 14:30:21 -040068 using VisitSurfaceProxyFunc = std::function<void(GrSurfaceProxy*, GrMipMapped)>;
Chris Daltonc4b47352019-08-23 10:10:36 -060069
Greg Danieldcf9ca12019-08-27 14:30:21 -040070 virtual void visitProxies_debugOnly(const VisitSurfaceProxyFunc&) const = 0;
71
72 void visitTargetAndSrcProxies_debugOnly(const VisitSurfaceProxyFunc& fn) const {
Chris Daltonc4b47352019-08-23 10:10:36 -060073 this->visitProxies_debugOnly(fn);
74 fn(fTarget.get(), GrMipMapped::kNo);
75 }
76#endif
Chris Dalton6b498102019-08-01 14:14:52 -060077
78protected:
79 // In addition to just the GrSurface being allocated, has the stencil buffer been allocated (if
80 // it is required)?
81 bool isInstantiated() const;
82
83 SkDEBUGCODE(bool deferredProxiesAreInstantiated() const;)
84
Chris Daltonaa3cbb82019-08-21 00:01:21 -060085 enum class ExpectedOutcome : bool {
86 kTargetUnchanged,
87 kTargetDirty,
88 };
89
90 virtual ExpectedOutcome onMakeClosed(const GrCaps&) = 0;
91
Chris Dalton6b498102019-08-01 14:14:52 -060092 sk_sp<GrSurfaceProxy> fTarget;
93
94 // List of texture proxies whose contents are being prepared on a worker thread
95 // TODO: this list exists so we can fire off the proper upload when an renderTask begins
96 // executing. Can this be replaced?
97 SkTArray<GrTextureProxy*, true> fDeferredProxies;
98
99private:
100 // for resetFlag, TopoSortTraits, gatherProxyIntervals, handleInternalAllocationFailure
101 friend class GrDrawingManager;
102
103 // Drops any pending operations that reference proxies that are not instantiated.
104 // NOTE: Derived classes don't need to check fTarget. That is handled when the drawingManager
105 // calls isInstantiated.
106 virtual void handleInternalAllocationFailure() = 0;
107
108 virtual bool onIsUsed(GrSurfaceProxy*) const = 0;
109
110 bool isUsed(GrSurfaceProxy* proxy) const {
111 if (proxy == fTarget.get()) {
112 return true;
113 }
114
115 return this->onIsUsed(proxy);
116 }
117
118 void addDependency(GrRenderTask* dependedOn);
119 void addDependent(GrRenderTask* dependent);
120 SkDEBUGCODE(bool isDependedent(const GrRenderTask* dependent) const;)
121 SkDEBUGCODE(void validate() const;)
122 void closeThoseWhoDependOnMe(const GrCaps&);
123
124 // Feed proxy usage intervals to the GrResourceAllocator class
125 virtual void gatherProxyIntervals(GrResourceAllocator*) const = 0;
126
127 static uint32_t CreateUniqueID();
128
129 enum Flags {
130 kClosed_Flag = 0x01, //!< This GrRenderTask can't accept any more dependencies.
131
132 kWasOutput_Flag = 0x02, //!< Flag for topological sorting
133 kTempMark_Flag = 0x04, //!< Flag for topological sorting
134 };
135
136 void setFlag(uint32_t flag) {
137 fFlags |= flag;
138 }
139
140 void resetFlag(uint32_t flag) {
141 fFlags &= ~flag;
142 }
143
144 bool isSetFlag(uint32_t flag) const {
145 return SkToBool(fFlags & flag);
146 }
147
148 struct TopoSortTraits {
149 static void Output(GrRenderTask* renderTask, int /* index */) {
150 renderTask->setFlag(kWasOutput_Flag);
151 }
152 static bool WasOutput(const GrRenderTask* renderTask) {
153 return renderTask->isSetFlag(kWasOutput_Flag);
154 }
155 static void SetTempMark(GrRenderTask* renderTask) {
156 renderTask->setFlag(kTempMark_Flag);
157 }
158 static void ResetTempMark(GrRenderTask* renderTask) {
159 renderTask->resetFlag(kTempMark_Flag);
160 }
161 static bool IsTempMarked(const GrRenderTask* renderTask) {
162 return renderTask->isSetFlag(kTempMark_Flag);
163 }
164 static int NumDependencies(const GrRenderTask* renderTask) {
165 return renderTask->fDependencies.count();
166 }
167 static GrRenderTask* Dependency(GrRenderTask* renderTask, int index) {
168 return renderTask->fDependencies[index];
169 }
170 };
171
172 virtual void onPrepare(GrOpFlushState* flushState) = 0;
173 virtual bool onExecute(GrOpFlushState* flushState) = 0;
174
175 const uint32_t fUniqueID;
176 uint32_t fFlags;
177
Greg Danielf41b2bd2019-08-22 16:19:24 -0400178 // 'this' GrRenderTask relies on the output of the GrRenderTasks in 'fDependencies'
Chris Dalton6b498102019-08-01 14:14:52 -0600179 SkSTArray<1, GrRenderTask*, true> fDependencies;
Greg Danielf41b2bd2019-08-22 16:19:24 -0400180 // 'this' GrRenderTask's output is relied on by the GrRenderTasks in 'fDependents'
Chris Dalton6b498102019-08-01 14:14:52 -0600181 SkSTArray<1, GrRenderTask*, true> fDependents;
182};
183
184#endif