blob: 3fb3a18bab0806ddf633d519e4f71f6848abf6d4 [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
Robert Phillips29f38542019-10-16 09:20:25 -040032 void prePrepare(GrRecordingContext* context) { this->onPrePrepare(context); }
Robert Phillips7327c9d2019-10-08 16:32:56 -040033
Chris Dalton6b498102019-08-01 14:14:52 -060034 // These two methods are only invoked at flush time
35 void prepare(GrOpFlushState* flushState);
36 bool execute(GrOpFlushState* flushState) { return this->onExecute(flushState); }
37
Chris Dalton6b498102019-08-01 14:14:52 -060038 // Called when this class will survive a flush and needs to truncate its ops and start over.
39 // TODO: ultimately it should be invalid for an op list to survive a flush.
40 // https://bugs.chromium.org/p/skia/issues/detail?id=7111
41 virtual void endFlush() {}
42
43 bool isClosed() const { return this->isSetFlag(kClosed_Flag); }
44
45 /*
46 * Notify this GrRenderTask that it relies on the contents of 'dependedOn'
47 */
Chris Dalton08755122019-08-05 16:13:47 -060048 void addDependency(GrSurfaceProxy* dependedOn, GrMipMapped, GrTextureResolveManager,
49 const GrCaps& caps);
Chris Dalton6b498102019-08-01 14:14:52 -060050
51 /*
Greg Danielc30f1a92019-09-06 15:28:58 -040052 * Notify this GrRenderTask that it relies on the contents of all GrRenderTasks which otherTask
53 * depends on.
54 */
55 void addDependenciesFromOtherTask(GrRenderTask* otherTask);
56
57 /*
Chris Dalton6b498102019-08-01 14:14:52 -060058 * Does this renderTask depend on 'dependedOn'?
59 */
60 bool dependsOn(const GrRenderTask* dependedOn) const;
61
62 uint32_t uniqueID() const { return fUniqueID; }
63
64 /*
Greg Danielf41b2bd2019-08-22 16:19:24 -040065 * Safely cast this GrRenderTask to a GrOpsTask (if possible).
Chris Dalton6b498102019-08-01 14:14:52 -060066 */
Greg Danielf41b2bd2019-08-22 16:19:24 -040067 virtual GrOpsTask* asOpsTask() { return nullptr; }
Chris Dalton6b498102019-08-01 14:14:52 -060068
Chris Daltonc4b47352019-08-23 10:10:36 -060069#ifdef SK_DEBUG
Chris Dalton6b498102019-08-01 14:14:52 -060070 /*
71 * Dump out the GrRenderTask dependency DAG
72 */
Chris Daltonc4b47352019-08-23 10:10:36 -060073 virtual void dump(bool printDependencies) const;
Chris Dalton6b498102019-08-01 14:14:52 -060074
Chris Daltonc4b47352019-08-23 10:10:36 -060075 virtual int numClips() const { return 0; }
76
Greg Danieldcf9ca12019-08-27 14:30:21 -040077 using VisitSurfaceProxyFunc = std::function<void(GrSurfaceProxy*, GrMipMapped)>;
Chris Daltonc4b47352019-08-23 10:10:36 -060078
Greg Danieldcf9ca12019-08-27 14:30:21 -040079 virtual void visitProxies_debugOnly(const VisitSurfaceProxyFunc&) const = 0;
80
81 void visitTargetAndSrcProxies_debugOnly(const VisitSurfaceProxyFunc& fn) const {
Chris Daltonc4b47352019-08-23 10:10:36 -060082 this->visitProxies_debugOnly(fn);
Chris Daltone2a903e2019-09-18 13:41:50 -060083 if (fTarget) {
84 fn(fTarget.get(), GrMipMapped::kNo);
85 }
Chris Daltonc4b47352019-08-23 10:10:36 -060086 }
87#endif
Chris Dalton6b498102019-08-01 14:14:52 -060088
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
Chris Daltonaa3cbb82019-08-21 00:01:21 -060096 enum class ExpectedOutcome : bool {
97 kTargetUnchanged,
98 kTargetDirty,
99 };
100
Chris Dalton16a33c62019-09-24 22:19:17 -0600101 // 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;
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600107
Chris Dalton6b498102019-08-01 14:14:52 -0600108 sk_sp<GrSurfaceProxy> fTarget;
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 fTarget. That is handled when the drawingManager
121 // 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 == fTarget.get()) {
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
Robert Phillips29f38542019-10-16 09:20:25 -0400188 // Only the GrOpsTask currently overrides this virtual
189 virtual void onPrePrepare(GrRecordingContext*) {}
Robert Phillips7327c9d2019-10-08 16:32:56 -0400190 virtual void onPrepare(GrOpFlushState*) {} // Only the GrOpsTask overrides this virtual
Chris Dalton6b498102019-08-01 14:14:52 -0600191 virtual bool onExecute(GrOpFlushState* flushState) = 0;
192
193 const uint32_t fUniqueID;
194 uint32_t fFlags;
195
Greg Danielf41b2bd2019-08-22 16:19:24 -0400196 // 'this' GrRenderTask relies on the output of the GrRenderTasks in 'fDependencies'
Chris Dalton6b498102019-08-01 14:14:52 -0600197 SkSTArray<1, GrRenderTask*, true> fDependencies;
Greg Danielf41b2bd2019-08-22 16:19:24 -0400198 // 'this' GrRenderTask's output is relied on by the GrRenderTasks in 'fDependents'
Chris Dalton6b498102019-08-01 14:14:52 -0600199 SkSTArray<1, GrRenderTask*, true> fDependents;
Chris Daltone2a903e2019-09-18 13:41:50 -0600200
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;
Chris Dalton6b498102019-08-01 14:14:52 -0600205};
206
207#endif