blob: 044817aea83b90bbad9433175413604966857cad [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;
18class GrOpList;
19class GrRenderTargetOpList;
20class GrResourceAllocator;
21class GrTextureOpList;
22
23// This class abstracts a task that targets a single GrSurfaceProxy, participates in the
24// GrDrawingManager's DAG, and implements the onExecute method to modify its target proxy's
25// contents. (e.g., an opList that executes a command buffer, a task to regenerate mipmaps, etc.)
26class GrRenderTask : public SkRefCnt {
27public:
28 GrRenderTask(sk_sp<GrSurfaceProxy> target);
29 ~GrRenderTask() override;
30
Chris Daltonaa3cbb82019-08-21 00:01:21 -060031 void makeClosed(const GrCaps&);
32
Chris Dalton6b498102019-08-01 14:14:52 -060033 // These two methods are only invoked at flush time
34 void prepare(GrOpFlushState* flushState);
35 bool execute(GrOpFlushState* flushState) { return this->onExecute(flushState); }
36
Chris Dalton6b498102019-08-01 14:14:52 -060037 // Called when this class will survive a flush and needs to truncate its ops and start over.
38 // TODO: ultimately it should be invalid for an op list to survive a flush.
39 // https://bugs.chromium.org/p/skia/issues/detail?id=7111
40 virtual void endFlush() {}
41
42 bool isClosed() const { return this->isSetFlag(kClosed_Flag); }
43
44 /*
45 * Notify this GrRenderTask that it relies on the contents of 'dependedOn'
46 */
Chris Dalton08755122019-08-05 16:13:47 -060047 void addDependency(GrSurfaceProxy* dependedOn, GrMipMapped, GrTextureResolveManager,
48 const GrCaps& caps);
Chris Dalton6b498102019-08-01 14:14:52 -060049
50 /*
51 * Does this renderTask depend on 'dependedOn'?
52 */
53 bool dependsOn(const GrRenderTask* dependedOn) const;
54
55 uint32_t uniqueID() const { return fUniqueID; }
56
57 /*
58 * Safely cast this GrRenderTask to a GrTextureOpList (if possible).
59 */
60 virtual GrTextureOpList* asTextureOpList() { return nullptr; }
61
62 /*
63 * Safely cast this GrRenderTask to a GrRenderTargetOpList (if possible).
64 */
65 virtual GrRenderTargetOpList* asRenderTargetOpList() { return nullptr; }
66
67 /*
68 * Dump out the GrRenderTask dependency DAG
69 */
70 SkDEBUGCODE(virtual void dump(bool printDependencies) const;)
71
72 SkDEBUGCODE(virtual int numClips() const { return 0; })
73
74protected:
75 // In addition to just the GrSurface being allocated, has the stencil buffer been allocated (if
76 // it is required)?
77 bool isInstantiated() const;
78
79 SkDEBUGCODE(bool deferredProxiesAreInstantiated() const;)
80
Chris Daltonaa3cbb82019-08-21 00:01:21 -060081 enum class ExpectedOutcome : bool {
82 kTargetUnchanged,
83 kTargetDirty,
84 };
85
86 virtual ExpectedOutcome onMakeClosed(const GrCaps&) = 0;
87
Chris Dalton6b498102019-08-01 14:14:52 -060088 sk_sp<GrSurfaceProxy> fTarget;
89
90 // List of texture proxies whose contents are being prepared on a worker thread
91 // TODO: this list exists so we can fire off the proper upload when an renderTask begins
92 // executing. Can this be replaced?
93 SkTArray<GrTextureProxy*, true> fDeferredProxies;
94
95private:
96 // for resetFlag, TopoSortTraits, gatherProxyIntervals, handleInternalAllocationFailure
97 friend class GrDrawingManager;
98
99 // Drops any pending operations that reference proxies that are not instantiated.
100 // NOTE: Derived classes don't need to check fTarget. That is handled when the drawingManager
101 // calls isInstantiated.
102 virtual void handleInternalAllocationFailure() = 0;
103
104 virtual bool onIsUsed(GrSurfaceProxy*) const = 0;
105
106 bool isUsed(GrSurfaceProxy* proxy) const {
107 if (proxy == fTarget.get()) {
108 return true;
109 }
110
111 return this->onIsUsed(proxy);
112 }
113
114 void addDependency(GrRenderTask* dependedOn);
115 void addDependent(GrRenderTask* dependent);
116 SkDEBUGCODE(bool isDependedent(const GrRenderTask* dependent) const;)
117 SkDEBUGCODE(void validate() const;)
118 void closeThoseWhoDependOnMe(const GrCaps&);
119
120 // Feed proxy usage intervals to the GrResourceAllocator class
121 virtual void gatherProxyIntervals(GrResourceAllocator*) const = 0;
122
123 static uint32_t CreateUniqueID();
124
125 enum Flags {
126 kClosed_Flag = 0x01, //!< This GrRenderTask can't accept any more dependencies.
127
128 kWasOutput_Flag = 0x02, //!< Flag for topological sorting
129 kTempMark_Flag = 0x04, //!< Flag for topological sorting
130 };
131
132 void setFlag(uint32_t flag) {
133 fFlags |= flag;
134 }
135
136 void resetFlag(uint32_t flag) {
137 fFlags &= ~flag;
138 }
139
140 bool isSetFlag(uint32_t flag) const {
141 return SkToBool(fFlags & flag);
142 }
143
144 struct TopoSortTraits {
145 static void Output(GrRenderTask* renderTask, int /* index */) {
146 renderTask->setFlag(kWasOutput_Flag);
147 }
148 static bool WasOutput(const GrRenderTask* renderTask) {
149 return renderTask->isSetFlag(kWasOutput_Flag);
150 }
151 static void SetTempMark(GrRenderTask* renderTask) {
152 renderTask->setFlag(kTempMark_Flag);
153 }
154 static void ResetTempMark(GrRenderTask* renderTask) {
155 renderTask->resetFlag(kTempMark_Flag);
156 }
157 static bool IsTempMarked(const GrRenderTask* renderTask) {
158 return renderTask->isSetFlag(kTempMark_Flag);
159 }
160 static int NumDependencies(const GrRenderTask* renderTask) {
161 return renderTask->fDependencies.count();
162 }
163 static GrRenderTask* Dependency(GrRenderTask* renderTask, int index) {
164 return renderTask->fDependencies[index];
165 }
166 };
167
168 virtual void onPrepare(GrOpFlushState* flushState) = 0;
169 virtual bool onExecute(GrOpFlushState* flushState) = 0;
170
171 const uint32_t fUniqueID;
172 uint32_t fFlags;
173
174 // 'this' GrOpList relies on the output of the GrOpLists in 'fDependencies'
175 SkSTArray<1, GrRenderTask*, true> fDependencies;
176 // 'this' GrOpList's output is relied on by the GrOpLists in 'fDependents'
177 SkSTArray<1, GrRenderTask*, true> fDependents;
178};
179
180#endif