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