blob: 3f66d901cde9300dbab2a9abf659c5587e046964 [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
68 virtual void visitProxies_debugOnly(const GrOp::VisitProxyFunc&) const = 0;
69
70 void visitTargetAndSrcProxies_debugOnly(const GrOp::VisitProxyFunc& fn) const {
71 this->visitProxies_debugOnly(fn);
72 fn(fTarget.get(), GrMipMapped::kNo);
73 }
74#endif
Chris Dalton6b498102019-08-01 14:14:52 -060075
76protected:
77 // In addition to just the GrSurface being allocated, has the stencil buffer been allocated (if
78 // it is required)?
79 bool isInstantiated() const;
80
81 SkDEBUGCODE(bool deferredProxiesAreInstantiated() const;)
82
Chris Daltonaa3cbb82019-08-21 00:01:21 -060083 enum class ExpectedOutcome : bool {
84 kTargetUnchanged,
85 kTargetDirty,
86 };
87
88 virtual ExpectedOutcome onMakeClosed(const GrCaps&) = 0;
89
Chris Dalton6b498102019-08-01 14:14:52 -060090 sk_sp<GrSurfaceProxy> fTarget;
91
92 // List of texture proxies whose contents are being prepared on a worker thread
93 // TODO: this list exists so we can fire off the proper upload when an renderTask begins
94 // executing. Can this be replaced?
95 SkTArray<GrTextureProxy*, true> fDeferredProxies;
96
97private:
98 // for resetFlag, TopoSortTraits, gatherProxyIntervals, handleInternalAllocationFailure
99 friend class GrDrawingManager;
100
101 // Drops any pending operations that reference proxies that are not instantiated.
102 // NOTE: Derived classes don't need to check fTarget. That is handled when the drawingManager
103 // calls isInstantiated.
104 virtual void handleInternalAllocationFailure() = 0;
105
106 virtual bool onIsUsed(GrSurfaceProxy*) const = 0;
107
108 bool isUsed(GrSurfaceProxy* proxy) const {
109 if (proxy == fTarget.get()) {
110 return true;
111 }
112
113 return this->onIsUsed(proxy);
114 }
115
116 void addDependency(GrRenderTask* dependedOn);
117 void addDependent(GrRenderTask* dependent);
118 SkDEBUGCODE(bool isDependedent(const GrRenderTask* dependent) const;)
119 SkDEBUGCODE(void validate() const;)
120 void closeThoseWhoDependOnMe(const GrCaps&);
121
122 // Feed proxy usage intervals to the GrResourceAllocator class
123 virtual void gatherProxyIntervals(GrResourceAllocator*) const = 0;
124
125 static uint32_t CreateUniqueID();
126
127 enum Flags {
128 kClosed_Flag = 0x01, //!< This GrRenderTask can't accept any more dependencies.
129
130 kWasOutput_Flag = 0x02, //!< Flag for topological sorting
131 kTempMark_Flag = 0x04, //!< Flag for topological sorting
132 };
133
134 void setFlag(uint32_t flag) {
135 fFlags |= flag;
136 }
137
138 void resetFlag(uint32_t flag) {
139 fFlags &= ~flag;
140 }
141
142 bool isSetFlag(uint32_t flag) const {
143 return SkToBool(fFlags & flag);
144 }
145
146 struct TopoSortTraits {
147 static void Output(GrRenderTask* renderTask, int /* index */) {
148 renderTask->setFlag(kWasOutput_Flag);
149 }
150 static bool WasOutput(const GrRenderTask* renderTask) {
151 return renderTask->isSetFlag(kWasOutput_Flag);
152 }
153 static void SetTempMark(GrRenderTask* renderTask) {
154 renderTask->setFlag(kTempMark_Flag);
155 }
156 static void ResetTempMark(GrRenderTask* renderTask) {
157 renderTask->resetFlag(kTempMark_Flag);
158 }
159 static bool IsTempMarked(const GrRenderTask* renderTask) {
160 return renderTask->isSetFlag(kTempMark_Flag);
161 }
162 static int NumDependencies(const GrRenderTask* renderTask) {
163 return renderTask->fDependencies.count();
164 }
165 static GrRenderTask* Dependency(GrRenderTask* renderTask, int index) {
166 return renderTask->fDependencies[index];
167 }
168 };
169
170 virtual void onPrepare(GrOpFlushState* flushState) = 0;
171 virtual bool onExecute(GrOpFlushState* flushState) = 0;
172
173 const uint32_t fUniqueID;
174 uint32_t fFlags;
175
Greg Danielf41b2bd2019-08-22 16:19:24 -0400176 // 'this' GrRenderTask relies on the output of the GrRenderTasks in 'fDependencies'
Chris Dalton6b498102019-08-01 14:14:52 -0600177 SkSTArray<1, GrRenderTask*, true> fDependencies;
Greg Danielf41b2bd2019-08-22 16:19:24 -0400178 // 'this' GrRenderTask's output is relied on by the GrRenderTasks in 'fDependents'
Chris Dalton6b498102019-08-01 14:14:52 -0600179 SkSTArray<1, GrRenderTask*, true> fDependents;
180};
181
182#endif