1/*
2 * Copyright 2019 Google Inc.
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 GrOpsTask_DEFINED
9#define GrOpsTask_DEFINED
10
11#include "include/core/SkMatrix.h"
12#include "include/core/SkRefCnt.h"
13#include "include/core/SkStrokeRec.h"
14#include "include/core/SkTypes.h"
15#include "include/gpu/GrRecordingContext.h"
16#include "include/private/SkColorData.h"
17#include "include/private/SkTArray.h"
18#include "include/private/SkTDArray.h"
19#include "src/core/SkArenaAlloc.h"
20#include "src/core/SkClipStack.h"
21#include "src/core/SkStringUtils.h"
22#include "src/core/SkTLazy.h"
23#include "src/gpu/GrAppliedClip.h"
24#include "src/gpu/GrPathRendering.h"
25#include "src/gpu/GrPrimitiveProcessor.h"
26#include "src/gpu/GrRenderTask.h"
27#include "src/gpu/ops/GrDrawOp.h"
28#include "src/gpu/ops/GrOp.h"
29
30class GrAuditTrail;
31class GrCaps;
32class GrClearOp;
33class GrGpuBuffer;
34class GrRenderTargetProxy;
35
36/** Observer is notified when a GrOpsTask is closed. */
37class GrOpsTaskClosedObserver {
38public:
39 virtual ~GrOpsTaskClosedObserver() = 0;
40 /**
41 * Called when the GrOpsTask is closed. Must not add/remove observers to 'task'.
42 * The GrOpsTask will remove all its observers after it finishes calling wasClosed().
43 */
44 virtual void wasClosed(const GrOpsTask& task) = 0;
45};
46
47class GrOpsTask : public GrRenderTask {
48private:
49 using DstProxyView = GrXferProcessor::DstProxyView;
50
51public:
52 // The Arenas must outlive the GrOpsTask, either by preserving the context that owns
53 // the pool, or by moving the pool to the DDL that takes over the GrOpsTask.
54 GrOpsTask(GrDrawingManager*, GrRecordingContext::Arenas, GrSurfaceProxyView, GrAuditTrail*);
55 ~GrOpsTask() override;
56
57 GrOpsTask* asOpsTask() override { return this; }
58
59 void addClosedObserver(GrOpsTaskClosedObserver* observer) {
60 SkASSERT(observer);
61 fClosedObservers.push_back(observer);
62 }
63
64 void removeClosedObserver(GrOpsTaskClosedObserver* observer);
65
66 bool isEmpty() const { return fOpChains.empty(); }
67
68 /**
69 * Empties the draw buffer of any queued up draws.
70 */
71 void endFlush(GrDrawingManager*) override;
72
73 void onPrePrepare(GrRecordingContext*) override;
74 /**
75 * Together these two functions flush all queued up draws to GrCommandBuffer. The return value
76 * of executeOps() indicates whether any commands were actually issued to the GPU.
77 */
78 void onPrepare(GrOpFlushState* flushState) override;
79 bool onExecute(GrOpFlushState* flushState) override;
80
81 void addSampledTexture(GrSurfaceProxy* proxy) {
82 // This function takes a GrSurfaceProxy because all subsequent uses of the proxy do not
83 // require the specifics of GrTextureProxy, so this avoids a number of unnecessary virtual
84 // asTextureProxy() calls. However, sampling the proxy implicitly requires that the proxy
85 // be a texture. Eventually, when proxies are a unified type with flags, this can just
86 // assert that capability.
87 SkASSERT(proxy->asTextureProxy());
88 fSampledProxies.push_back(proxy);
89 }
90
91 void addOp(GrDrawingManager* drawingMgr, std::unique_ptr<GrOp> op,
92 GrTextureResolveManager textureResolveManager, const GrCaps& caps) {
93 auto addDependency = [ drawingMgr, textureResolveManager, &caps, this ] (
94 GrSurfaceProxy* p, GrMipmapped mipmapped) {
95 this->addDependency(drawingMgr, p, mipmapped, textureResolveManager, caps);
96 };
97
98 op->visitProxies(addDependency);
99
100 this->recordOp(std::move(op), GrProcessorSet::EmptySetAnalysis(), nullptr, nullptr, caps);
101 }
102
103 void addDrawOp(GrDrawingManager* drawingMgr, std::unique_ptr<GrDrawOp> op,
104 const GrProcessorSet::Analysis& processorAnalysis,
105 GrAppliedClip&& clip, const DstProxyView& dstProxyView,
106 GrTextureResolveManager textureResolveManager, const GrCaps& caps) {
107 auto addDependency = [ drawingMgr, textureResolveManager, &caps, this ] (
108 GrSurfaceProxy* p, GrMipmapped mipmapped) {
109 this->addSampledTexture(p);
110 this->addDependency(drawingMgr, p, mipmapped, textureResolveManager, caps);
111 };
112
113 op->visitProxies(addDependency);
114 clip.visitProxies(addDependency);
115 if (dstProxyView.proxy()) {
116 this->addSampledTexture(dstProxyView.proxy());
117 addDependency(dstProxyView.proxy(), GrMipmapped::kNo);
118 }
119
120 this->recordOp(std::move(op), processorAnalysis, clip.doesClip() ? &clip : nullptr,
121 &dstProxyView, caps);
122 }
123
124 void discard();
125
126#ifdef SK_DEBUG
127 int numClips() const override { return fNumClips; }
128 void visitProxies_debugOnly(const GrOp::VisitProxyFunc&) const override;
129#endif
130
131#if GR_TEST_UTILS
132 void dump(bool printDependencies) const override;
133 const char* name() const final { return "Ops"; }
134 int numOpChains() const { return fOpChains.count(); }
135 const GrOp* getChain(int index) const { return fOpChains[index].head(); }
136#endif
137
138private:
139 bool isNoOp() const {
140 // TODO: GrLoadOp::kDiscard (i.e., storing a discard) should also be grounds for skipping
141 // execution. We currently don't because of Vulkan. See http://skbug.com/9373.
142 //
143 // TODO: We should also consider stencil load/store here. We get away with it for now
144 // because we never discard stencil buffers.
145 return fOpChains.empty() && GrLoadOp::kLoad == fColorLoadOp;
146 }
147
148 void deleteOps();
149
150 enum class StencilContent {
151 kDontCare,
152 kUserBitsCleared, // User bits: cleared
153 // Clip bit: don't care (Ganesh always pre-clears the clip bit.)
154 kPreserved
155 };
156
157 // Lets the caller specify what the content of the stencil buffer should be at the beginning
158 // of the render pass.
159 //
160 // When requesting kClear: Tilers will load the stencil buffer with a "clear" op; non-tilers
161 // will clear the stencil on first load, and then preserve it on subsequent loads. (Preserving
162 // works because renderTargetContexts are required to leave the user bits in a cleared state
163 // once finished.)
164 //
165 // NOTE: initialContent must not be kClear if caps.performStencilClearsAsDraws() is true.
166 void setInitialStencilContent(StencilContent initialContent) {
167 fInitialStencilContent = initialContent;
168 }
169
170 // If a renderTargetContext splits its opsTask, it uses this method to guarantee stencil values
171 // get preserved across its split tasks.
172 void setMustPreserveStencil() { fMustPreserveStencil = true; }
173
174 // Must only be called if native color buffer clearing is enabled.
175 void setColorLoadOp(GrLoadOp op, const SkPMColor4f& color);
176 // Sets the clear color to transparent black
177 void setColorLoadOp(GrLoadOp op) {
178 static const SkPMColor4f kDefaultClearColor = {0.f, 0.f, 0.f, 0.f};
179 this->setColorLoadOp(op, kDefaultClearColor);
180 }
181
182 enum class CanDiscardPreviousOps : bool {
183 kYes = true,
184 kNo = false
185 };
186
187 // Perform book-keeping for a fullscreen clear, regardless of how the clear is implemented later
188 // (i.e. setColorLoadOp(), adding a ClearOp, or adding a GrFillRectOp that covers the device).
189 // Returns true if the clear can be converted into a load op (barring device caps).
190 bool resetForFullscreenClear(CanDiscardPreviousOps);
191
192 class OpChain {
193 public:
194 OpChain(std::unique_ptr<GrOp>, GrProcessorSet::Analysis, GrAppliedClip*,
195 const DstProxyView*);
196 ~OpChain() {
197 // The ops are stored in a GrMemoryPool and must be explicitly deleted via the pool.
198 SkASSERT(fList.empty());
199 }
200
201 OpChain(const OpChain&) = delete;
202 OpChain& operator=(const OpChain&) = delete;
203 OpChain(OpChain&&) = default;
204 OpChain& operator=(OpChain&&) = default;
205
206 void visitProxies(const GrOp::VisitProxyFunc&) const;
207
208 GrOp* head() const { return fList.head(); }
209
210 GrAppliedClip* appliedClip() const { return fAppliedClip; }
211 const DstProxyView& dstProxyView() const { return fDstProxyView; }
212 const SkRect& bounds() const { return fBounds; }
213
214 // Deletes all the ops in the chain via the pool.
215 void deleteOps(GrOpMemoryPool* pool);
216
217 // Attempts to move the ops from the passed chain to this chain at the head. Also attempts
218 // to merge ops between the chains. Upon success the passed chain is empty.
219 // Fails when the chains aren't of the same op type, have different clips or dst proxies.
220 bool prependChain(OpChain*, const GrCaps&, GrRecordingContext::Arenas*, GrAuditTrail*);
221
222 // Attempts to add 'op' to this chain either by merging or adding to the tail. Returns
223 // 'op' to the caller upon failure, otherwise null. Fails when the op and chain aren't of
224 // the same op type, have different clips or dst proxies.
225 std::unique_ptr<GrOp> appendOp(std::unique_ptr<GrOp> op, GrProcessorSet::Analysis,
226 const DstProxyView*, const GrAppliedClip*, const GrCaps&,
227 GrRecordingContext::Arenas*, GrAuditTrail*);
228
229 void setSkipExecuteFlag() { fSkipExecute = true; }
230 bool shouldExecute() const {
231 return SkToBool(this->head()) && !fSkipExecute;
232 }
233
234 private:
235 class List {
236 public:
237 List() = default;
238 List(std::unique_ptr<GrOp>);
239 List(List&&);
240 List& operator=(List&& that);
241
242 bool empty() const { return !SkToBool(fHead); }
243 GrOp* head() const { return fHead.get(); }
244 GrOp* tail() const { return fTail; }
245
246 std::unique_ptr<GrOp> popHead();
247 std::unique_ptr<GrOp> removeOp(GrOp* op);
248 void pushHead(std::unique_ptr<GrOp> op);
249 void pushTail(std::unique_ptr<GrOp>);
250
251 void validate() const;
252
253 private:
254 std::unique_ptr<GrOp> fHead;
255 GrOp* fTail = nullptr;
256 };
257
258 void validate() const;
259
260 bool tryConcat(List*, GrProcessorSet::Analysis, const DstProxyView&, const GrAppliedClip*,
261 const SkRect& bounds, const GrCaps&, GrRecordingContext::Arenas*,
262 GrAuditTrail*);
263 static List DoConcat(List, List, const GrCaps&, GrRecordingContext::Arenas*, GrAuditTrail*);
264
265 List fList;
266 GrProcessorSet::Analysis fProcessorAnalysis;
267 DstProxyView fDstProxyView;
268 GrAppliedClip* fAppliedClip;
269 SkRect fBounds;
270
271 // We set this flag to true if any of the ops' proxies fail to instantiate so that we know
272 // not to try and draw the op.
273 bool fSkipExecute = false;
274 };
275
276
277 bool onIsUsed(GrSurfaceProxy*) const override;
278
279 void handleInternalAllocationFailure() override;
280
281 void gatherProxyIntervals(GrResourceAllocator*) const override;
282
283 void recordOp(std::unique_ptr<GrOp>, GrProcessorSet::Analysis, GrAppliedClip*,
284 const DstProxyView*, const GrCaps& caps);
285
286 void forwardCombine(const GrCaps&);
287
288 ExpectedOutcome onMakeClosed(const GrCaps& caps, SkIRect* targetUpdateBounds) override;
289
290 friend class OpsTaskTestingAccess;
291 friend class GrRenderTargetContextPriv; // for stencil clip state. TODO: this is invasive
292
293 // The RTC and OpsTask have to work together to handle buffer clears. In most cases, buffer
294 // clearing can be done natively, in which case the op list's load ops are sufficient. In other
295 // cases, draw ops must be used, which makes the RTC the best place for those decisions. This,
296 // however, requires that the RTC be able to coordinate with the op list to achieve similar ends
297 friend class GrRenderTargetContext;
298
299 // This is a backpointer to the Arenas that holds the memory for this GrOpsTask's ops. In the
300 // DDL case, the Arenas must have been detached from the original recording context and moved
301 // into the owning DDL.
302 GrRecordingContext::Arenas fArenas;
303 GrAuditTrail* fAuditTrail;
304
305 SkSTArray<2, GrOpsTaskClosedObserver*, true> fClosedObservers;
306
307 GrLoadOp fColorLoadOp = GrLoadOp::kLoad;
308 SkPMColor4f fLoadClearColor = SK_PMColor4fTRANSPARENT;
309 StencilContent fInitialStencilContent = StencilContent::kDontCare;
310 bool fMustPreserveStencil = false;
311
312 uint32_t fLastClipStackGenID = SK_InvalidUniqueID;
313 SkIRect fLastDevClipBounds;
314 int fLastClipNumAnalyticElements;
315
316 // For ops/opsTask we have mean: 5 stdDev: 28
317 SkSTArray<25, OpChain> fOpChains;
318
319 // MDB TODO: 4096 for the first allocation of the clip space will be huge overkill.
320 // Gather statistics to determine the correct size.
321 SkArenaAllocWithReset fClipAllocator{4096};
322 SkDEBUGCODE(int fNumClips;)
323
324 // TODO: We could look into this being a set if we find we're adding a lot of duplicates that is
325 // causing slow downs.
326 SkTArray<GrSurfaceProxy*, true> fSampledProxies;
327
328 SkRect fTotalBounds = SkRect::MakeEmpty();
329 SkIRect fClippedContentBounds = SkIRect::MakeEmpty();
330};
331
332#endif
333