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