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 #include "src/gpu/GrOpsTask.h"
9
10 #include "include/gpu/GrRecordingContext.h"
11 #include "src/core/SkRectPriv.h"
12 #include "src/core/SkTraceEvent.h"
13 #include "src/gpu/GrAttachment.h"
14 #include "src/gpu/GrAuditTrail.h"
15 #include "src/gpu/GrCaps.h"
16 #include "src/gpu/GrGpu.h"
17 #include "src/gpu/GrMemoryPool.h"
18 #include "src/gpu/GrOpFlushState.h"
19 #include "src/gpu/GrOpsRenderPass.h"
20 #include "src/gpu/GrRecordingContextPriv.h"
21 #include "src/gpu/GrRenderTarget.h"
22 #include "src/gpu/GrRenderTargetContext.h"
23 #include "src/gpu/GrResourceAllocator.h"
24 #include "src/gpu/GrTexture.h"
25 #include "src/gpu/geometry/GrRect.h"
26 #include "src/gpu/ops/GrClearOp.h"
27
28 ////////////////////////////////////////////////////////////////////////////////
29
30 // Experimentally we have found that most combining occurs within the first 10 comparisons.
31 static const int kMaxOpMergeDistance = 10;
32 static const int kMaxOpChainDistance = 10;
33
34 ////////////////////////////////////////////////////////////////////////////////
35
36 using DstProxyView = GrXferProcessor::DstProxyView;
37
38 ////////////////////////////////////////////////////////////////////////////////
39
can_reorder(const SkRect & a,const SkRect & b)40 static inline bool can_reorder(const SkRect& a, const SkRect& b) { return !GrRectsOverlap(a, b); }
41
42 ////////////////////////////////////////////////////////////////////////////////
43
List(GrOp::Owner op)44 inline GrOpsTask::OpChain::List::List(GrOp::Owner op)
45 : fHead(std::move(op)), fTail(fHead.get()) {
46 this->validate();
47 }
48
List(List && that)49 inline GrOpsTask::OpChain::List::List(List&& that) { *this = std::move(that); }
50
operator =(List && that)51 inline GrOpsTask::OpChain::List& GrOpsTask::OpChain::List::operator=(List&& that) {
52 fHead = std::move(that.fHead);
53 fTail = that.fTail;
54 that.fTail = nullptr;
55 this->validate();
56 return *this;
57 }
58
popHead()59 inline GrOp::Owner GrOpsTask::OpChain::List::popHead() {
60 SkASSERT(fHead);
61 auto temp = fHead->cutChain();
62 std::swap(temp, fHead);
63 if (!fHead) {
64 SkASSERT(fTail == temp.get());
65 fTail = nullptr;
66 }
67 return temp;
68 }
69
removeOp(GrOp * op)70 inline GrOp::Owner GrOpsTask::OpChain::List::removeOp(GrOp* op) {
71 #ifdef SK_DEBUG
72 auto head = op;
73 while (head->prevInChain()) { head = head->prevInChain(); }
74 SkASSERT(head == fHead.get());
75 #endif
76 auto prev = op->prevInChain();
77 if (!prev) {
78 SkASSERT(op == fHead.get());
79 return this->popHead();
80 }
81 auto temp = prev->cutChain();
82 if (auto next = temp->cutChain()) {
83 prev->chainConcat(std::move(next));
84 } else {
85 SkASSERT(fTail == op);
86 fTail = prev;
87 }
88 this->validate();
89 return temp;
90 }
91
pushHead(GrOp::Owner op)92 inline void GrOpsTask::OpChain::List::pushHead(GrOp::Owner op) {
93 SkASSERT(op);
94 SkASSERT(op->isChainHead());
95 SkASSERT(op->isChainTail());
96 if (fHead) {
97 op->chainConcat(std::move(fHead));
98 fHead = std::move(op);
99 } else {
100 fHead = std::move(op);
101 fTail = fHead.get();
102 }
103 }
104
pushTail(GrOp::Owner op)105 inline void GrOpsTask::OpChain::List::pushTail(GrOp::Owner op) {
106 SkASSERT(op->isChainTail());
107 fTail->chainConcat(std::move(op));
108 fTail = fTail->nextInChain();
109 }
110
validate() const111 inline void GrOpsTask::OpChain::List::validate() const {
112 #ifdef SK_DEBUG
113 if (fHead) {
114 SkASSERT(fTail);
115 fHead->validateChain(fTail);
116 }
117 #endif
118 }
119
120 ////////////////////////////////////////////////////////////////////////////////
121
OpChain(GrOp::Owner op,GrProcessorSet::Analysis processorAnalysis,GrAppliedClip * appliedClip,const DstProxyView * dstProxyView)122 GrOpsTask::OpChain::OpChain(GrOp::Owner op,
123 GrProcessorSet::Analysis processorAnalysis,
124 GrAppliedClip* appliedClip, const DstProxyView* dstProxyView)
125 : fList{std::move(op)}
126 , fProcessorAnalysis(processorAnalysis)
127 , fAppliedClip(appliedClip) {
128 if (fProcessorAnalysis.requiresDstTexture()) {
129 SkASSERT(dstProxyView && dstProxyView->proxy());
130 fDstProxyView = *dstProxyView;
131 }
132 fBounds = fList.head()->bounds();
133 }
134
visitProxies(const GrOp::VisitProxyFunc & func) const135 void GrOpsTask::OpChain::visitProxies(const GrOp::VisitProxyFunc& func) const {
136 if (fList.empty()) {
137 return;
138 }
139 for (const auto& op : GrOp::ChainRange<>(fList.head())) {
140 op.visitProxies(func);
141 }
142 if (fDstProxyView.proxy()) {
143 func(fDstProxyView.proxy(), GrMipmapped::kNo);
144 }
145 if (fAppliedClip) {
146 fAppliedClip->visitProxies(func);
147 }
148 }
149
deleteOps()150 void GrOpsTask::OpChain::deleteOps() {
151 while (!fList.empty()) {
152 // Since the value goes out of scope immediately, the GrOp::Owner deletes the op.
153 fList.popHead();
154 }
155 }
156
157 // Concatenates two op chains and attempts to merge ops across the chains. Assumes that we know that
158 // the two chains are chainable. Returns the new chain.
DoConcat(List chainA,List chainB,const GrCaps & caps,GrRecordingContext::Arenas * arenas,GrAuditTrail * auditTrail)159 GrOpsTask::OpChain::List GrOpsTask::OpChain::DoConcat(
160 List chainA, List chainB, const GrCaps& caps, GrRecordingContext::Arenas* arenas,
161 GrAuditTrail* auditTrail) {
162 // We process ops in chain b from head to tail. We attempt to merge with nodes in a, starting
163 // at chain a's tail and working toward the head. We produce one of the following outcomes:
164 // 1) b's head is merged into an op in a.
165 // 2) An op from chain a is merged into b's head. (In this case b's head gets processed again.)
166 // 3) b's head is popped from chain a and added at the tail of a.
167 // After result 3 we don't want to attempt to merge the next head of b with the new tail of a,
168 // as we assume merges were already attempted when chain b was created. So we keep track of the
169 // original tail of a and start our iteration of a there. We also track the bounds of the nodes
170 // appended to chain a that will be skipped for bounds testing. If the original tail of a is
171 // merged into an op in b (case 2) then we advance the "original tail" towards the head of a.
172 GrOp* origATail = chainA.tail();
173 SkRect skipBounds = SkRectPriv::MakeLargestInverted();
174 do {
175 int numMergeChecks = 0;
176 bool merged = false;
177 bool noSkip = (origATail == chainA.tail());
178 SkASSERT(noSkip == (skipBounds == SkRectPriv::MakeLargestInverted()));
179 bool canBackwardMerge = noSkip || can_reorder(chainB.head()->bounds(), skipBounds);
180 SkRect forwardMergeBounds = skipBounds;
181 GrOp* a = origATail;
182 while (a) {
183 bool canForwardMerge =
184 (a == chainA.tail()) || can_reorder(a->bounds(), forwardMergeBounds);
185 if (canForwardMerge || canBackwardMerge) {
186 auto result = a->combineIfPossible(
187 chainB.head(), arenas->recordTimeAllocator(), caps);
188 SkASSERT(result != GrOp::CombineResult::kCannotCombine);
189 merged = (result == GrOp::CombineResult::kMerged);
190 GrOP_INFO("\t\t: (%s opID: %u) -> Combining with (%s, opID: %u)\n",
191 chainB.head()->name(), chainB.head()->uniqueID(), a->name(),
192 a->uniqueID());
193 }
194 if (merged) {
195 GR_AUDIT_TRAIL_OPS_RESULT_COMBINED(auditTrail, a, chainB.head());
196 if (canBackwardMerge) {
197 // The GrOp::Owner releases the op.
198 chainB.popHead();
199 } else {
200 // We merged the contents of b's head into a. We will replace b's head with a in
201 // chain b.
202 SkASSERT(canForwardMerge);
203 if (a == origATail) {
204 origATail = a->prevInChain();
205 }
206 GrOp::Owner detachedA = chainA.removeOp(a);
207 // The GrOp::Owner releases the op.
208 chainB.popHead();
209 chainB.pushHead(std::move(detachedA));
210 if (chainA.empty()) {
211 // We merged all the nodes in chain a to chain b.
212 return chainB;
213 }
214 }
215 break;
216 } else {
217 if (++numMergeChecks == kMaxOpMergeDistance) {
218 break;
219 }
220 forwardMergeBounds.joinNonEmptyArg(a->bounds());
221 canBackwardMerge =
222 canBackwardMerge && can_reorder(chainB.head()->bounds(), a->bounds());
223 a = a->prevInChain();
224 }
225 }
226 // If we weren't able to merge b's head then pop b's head from chain b and make it the new
227 // tail of a.
228 if (!merged) {
229 chainA.pushTail(chainB.popHead());
230 skipBounds.joinNonEmptyArg(chainA.tail()->bounds());
231 }
232 } while (!chainB.empty());
233 return chainA;
234 }
235
236 // Attempts to concatenate the given chain onto our own and merge ops across the chains. Returns
237 // whether the operation succeeded. On success, the provided list will be returned empty.
tryConcat(List * list,GrProcessorSet::Analysis processorAnalysis,const DstProxyView & dstProxyView,const GrAppliedClip * appliedClip,const SkRect & bounds,const GrCaps & caps,GrRecordingContext::Arenas * arenas,GrAuditTrail * auditTrail)238 bool GrOpsTask::OpChain::tryConcat(
239 List* list, GrProcessorSet::Analysis processorAnalysis, const DstProxyView& dstProxyView,
240 const GrAppliedClip* appliedClip, const SkRect& bounds, const GrCaps& caps,
241 GrRecordingContext::Arenas* arenas, GrAuditTrail* auditTrail) {
242 SkASSERT(!fList.empty());
243 SkASSERT(!list->empty());
244 SkASSERT(fProcessorAnalysis.requiresDstTexture() == SkToBool(fDstProxyView.proxy()));
245 SkASSERT(processorAnalysis.requiresDstTexture() == SkToBool(dstProxyView.proxy()));
246 // All returns use explicit tuple constructor rather than {a, b} to work around old GCC bug.
247 if (fList.head()->classID() != list->head()->classID() ||
248 SkToBool(fAppliedClip) != SkToBool(appliedClip) ||
249 (fAppliedClip && *fAppliedClip != *appliedClip) ||
250 (fProcessorAnalysis.requiresNonOverlappingDraws() !=
251 processorAnalysis.requiresNonOverlappingDraws()) ||
252 (fProcessorAnalysis.requiresNonOverlappingDraws() &&
253 // Non-overlaping draws are only required when Ganesh will either insert a barrier,
254 // or read back a new dst texture between draws. In either case, we can neither
255 // chain nor combine overlapping Ops.
256 GrRectsTouchOrOverlap(fBounds, bounds)) ||
257 (fProcessorAnalysis.requiresDstTexture() != processorAnalysis.requiresDstTexture()) ||
258 (fProcessorAnalysis.requiresDstTexture() && fDstProxyView != dstProxyView)) {
259 return false;
260 }
261
262 SkDEBUGCODE(bool first = true;)
263 do {
264 switch (fList.tail()->combineIfPossible(list->head(), arenas->recordTimeAllocator(), caps))
265 {
266 case GrOp::CombineResult::kCannotCombine:
267 // If an op supports chaining then it is required that chaining is transitive and
268 // that if any two ops in two different chains can merge then the two chains
269 // may also be chained together. Thus, we should only hit this on the first
270 // iteration.
271 SkASSERT(first);
272 return false;
273 case GrOp::CombineResult::kMayChain:
274 fList = DoConcat(std::move(fList), std::exchange(*list, List()), caps, arenas,
275 auditTrail);
276 // The above exchange cleared out 'list'. The list needs to be empty now for the
277 // loop to terminate.
278 SkASSERT(list->empty());
279 break;
280 case GrOp::CombineResult::kMerged: {
281 GrOP_INFO("\t\t: (%s opID: %u) -> Combining with (%s, opID: %u)\n",
282 list->tail()->name(), list->tail()->uniqueID(), list->head()->name(),
283 list->head()->uniqueID());
284 GR_AUDIT_TRAIL_OPS_RESULT_COMBINED(auditTrail, fList.tail(), list->head());
285 // The GrOp::Owner releases the op.
286 list->popHead();
287 break;
288 }
289 }
290 SkDEBUGCODE(first = false);
291 } while (!list->empty());
292
293 // The new ops were successfully merged and/or chained onto our own.
294 fBounds.joinPossiblyEmptyRect(bounds);
295 return true;
296 }
297
prependChain(OpChain * that,const GrCaps & caps,GrRecordingContext::Arenas * arenas,GrAuditTrail * auditTrail)298 bool GrOpsTask::OpChain::prependChain(OpChain* that, const GrCaps& caps,
299 GrRecordingContext::Arenas* arenas,
300 GrAuditTrail* auditTrail) {
301 if (!that->tryConcat(&fList, fProcessorAnalysis, fDstProxyView, fAppliedClip, fBounds, caps,
302 arenas, auditTrail)) {
303 this->validate();
304 // append failed
305 return false;
306 }
307
308 // 'that' owns the combined chain. Move it into 'this'.
309 SkASSERT(fList.empty());
310 fList = std::move(that->fList);
311 fBounds = that->fBounds;
312
313 that->fDstProxyView.setProxyView({});
314 if (that->fAppliedClip && that->fAppliedClip->hasCoverageFragmentProcessor()) {
315 // Obliterates the processor.
316 that->fAppliedClip->detachCoverageFragmentProcessor();
317 }
318 this->validate();
319 return true;
320 }
321
appendOp(GrOp::Owner op,GrProcessorSet::Analysis processorAnalysis,const DstProxyView * dstProxyView,const GrAppliedClip * appliedClip,const GrCaps & caps,GrRecordingContext::Arenas * arenas,GrAuditTrail * auditTrail)322 GrOp::Owner GrOpsTask::OpChain::appendOp(
323 GrOp::Owner op, GrProcessorSet::Analysis processorAnalysis,
324 const DstProxyView* dstProxyView, const GrAppliedClip* appliedClip, const GrCaps& caps,
325 GrRecordingContext::Arenas* arenas, GrAuditTrail* auditTrail) {
326 const GrXferProcessor::DstProxyView noDstProxyView;
327 if (!dstProxyView) {
328 dstProxyView = &noDstProxyView;
329 }
330 SkASSERT(op->isChainHead() && op->isChainTail());
331 SkRect opBounds = op->bounds();
332 List chain(std::move(op));
333 if (!this->tryConcat(
334 &chain, processorAnalysis, *dstProxyView, appliedClip, opBounds, caps,
335 arenas, auditTrail)) {
336 // append failed, give the op back to the caller.
337 this->validate();
338 return chain.popHead();
339 }
340
341 SkASSERT(chain.empty());
342 this->validate();
343 return nullptr;
344 }
345
validate() const346 inline void GrOpsTask::OpChain::validate() const {
347 #ifdef SK_DEBUG
348 fList.validate();
349 for (const auto& op : GrOp::ChainRange<>(fList.head())) {
350 // Not using SkRect::contains because we allow empty rects.
351 SkASSERT(fBounds.fLeft <= op.bounds().fLeft && fBounds.fTop <= op.bounds().fTop &&
352 fBounds.fRight >= op.bounds().fRight && fBounds.fBottom >= op.bounds().fBottom);
353 }
354 #endif
355 }
356
357 ////////////////////////////////////////////////////////////////////////////////
358
GrOpsTask(GrDrawingManager * drawingMgr,GrRecordingContext::Arenas arenas,GrSurfaceProxyView view,GrAuditTrail * auditTrail)359 GrOpsTask::GrOpsTask(GrDrawingManager* drawingMgr, GrRecordingContext::Arenas arenas,
360 GrSurfaceProxyView view,
361 GrAuditTrail* auditTrail)
362 : GrRenderTask()
363 , fArenas(arenas)
364 , fAuditTrail(auditTrail)
365 SkDEBUGCODE(, fNumClips(0)) {
366 this->addTarget(drawingMgr, std::move(view));
367 }
368
deleteOps()369 void GrOpsTask::deleteOps() {
370 for (auto& chain : fOpChains) {
371 chain.deleteOps();
372 }
373 fOpChains.reset();
374 }
375
~GrOpsTask()376 GrOpsTask::~GrOpsTask() {
377 this->deleteOps();
378 }
379
endFlush(GrDrawingManager * drawingMgr)380 void GrOpsTask::endFlush(GrDrawingManager* drawingMgr) {
381 fLastClipStackGenID = SK_InvalidUniqueID;
382 this->deleteOps();
383 fClipAllocator.reset();
384
385 fDeferredProxies.reset();
386 fSampledProxies.reset();
387 fAuditTrail = nullptr;
388
389 GrRenderTask::endFlush(drawingMgr);
390 }
391
onPrePrepare(GrRecordingContext * context)392 void GrOpsTask::onPrePrepare(GrRecordingContext* context) {
393 SkASSERT(this->isClosed());
394 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
395 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
396 #endif
397 // TODO: remove the check for discard here once reduced op splitting is turned on. Currently we
398 // can end up with GrOpsTasks that only have a discard load op and no ops. For vulkan validation
399 // we need to keep that discard and not drop it. Once we have reduce op list splitting enabled
400 // we shouldn't end up with GrOpsTasks with only discard.
401 if (this->isNoOp() || (fClippedContentBounds.isEmpty() && fColorLoadOp != GrLoadOp::kDiscard)) {
402 return;
403 }
404
405 for (const auto& chain : fOpChains) {
406 if (chain.shouldExecute()) {
407 chain.head()->prePrepare(context,
408 &fTargets[0],
409 chain.appliedClip(),
410 chain.dstProxyView(),
411 fRenderPassXferBarriers);
412 }
413 }
414 }
415
onPrepare(GrOpFlushState * flushState)416 void GrOpsTask::onPrepare(GrOpFlushState* flushState) {
417 SkASSERT(this->target(0).proxy()->peekRenderTarget());
418 SkASSERT(this->isClosed());
419 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
420 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
421 #endif
422 // TODO: remove the check for discard here once reduced op splitting is turned on. Currently we
423 // can end up with GrOpsTasks that only have a discard load op and no ops. For vulkan validation
424 // we need to keep that discard and not drop it. Once we have reduce op list splitting enabled
425 // we shouldn't end up with GrOpsTasks with only discard.
426 if (this->isNoOp() || (fClippedContentBounds.isEmpty() && fColorLoadOp != GrLoadOp::kDiscard)) {
427 return;
428 }
429
430 flushState->setSampledProxyArray(&fSampledProxies);
431 // Loop over the ops that haven't yet been prepared.
432 for (const auto& chain : fOpChains) {
433 if (chain.shouldExecute()) {
434 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
435 TRACE_EVENT0("skia.gpu", chain.head()->name());
436 #endif
437 GrOpFlushState::OpArgs opArgs(chain.head(),
438 &fTargets[0],
439 chain.appliedClip(),
440 chain.dstProxyView(),
441 fRenderPassXferBarriers);
442
443 flushState->setOpArgs(&opArgs);
444
445 // Temporary debugging helper: for debugging prePrepare w/o going through DDLs
446 // Delete once most of the GrOps have an onPrePrepare.
447 // chain.head()->prePrepare(flushState->gpu()->getContext(), &this->target(0),
448 // chain.appliedClip());
449
450 // GrOp::prePrepare may or may not have been called at this point
451 chain.head()->prepare(flushState);
452 flushState->setOpArgs(nullptr);
453 }
454 }
455 flushState->setSampledProxyArray(nullptr);
456 }
457
create_render_pass(GrGpu * gpu,GrRenderTarget * rt,GrAttachment * stencil,GrSurfaceOrigin origin,const SkIRect & bounds,GrLoadOp colorLoadOp,const SkPMColor4f & loadClearColor,GrLoadOp stencilLoadOp,GrStoreOp stencilStoreOp,const SkTArray<GrSurfaceProxy *,true> & sampledProxies,GrXferBarrierFlags renderPassXferBarriers)458 static GrOpsRenderPass* create_render_pass(GrGpu* gpu,
459 GrRenderTarget* rt,
460 GrAttachment* stencil,
461 GrSurfaceOrigin origin,
462 const SkIRect& bounds,
463 GrLoadOp colorLoadOp,
464 const SkPMColor4f& loadClearColor,
465 GrLoadOp stencilLoadOp,
466 GrStoreOp stencilStoreOp,
467 const SkTArray<GrSurfaceProxy*, true>& sampledProxies,
468 GrXferBarrierFlags renderPassXferBarriers) {
469 const GrOpsRenderPass::LoadAndStoreInfo kColorLoadStoreInfo {
470 colorLoadOp,
471 GrStoreOp::kStore,
472 loadClearColor
473 };
474
475 // TODO:
476 // We would like to (at this level) only ever clear & discard. We would need
477 // to stop splitting up higher level OpsTasks for copyOps to achieve that.
478 // Note: we would still need SB loads and stores but they would happen at a
479 // lower level (inside the VK command buffer).
480 const GrOpsRenderPass::StencilLoadAndStoreInfo stencilLoadAndStoreInfo {
481 stencilLoadOp,
482 stencilStoreOp,
483 };
484
485 return gpu->getOpsRenderPass(rt, stencil, origin, bounds,
486 kColorLoadStoreInfo, stencilLoadAndStoreInfo, sampledProxies,
487 renderPassXferBarriers);
488 }
489
490 // TODO: this is where GrOp::renderTarget is used (which is fine since it
491 // is at flush time). However, we need to store the RenderTargetProxy in the
492 // Ops and instantiate them here.
onExecute(GrOpFlushState * flushState)493 bool GrOpsTask::onExecute(GrOpFlushState* flushState) {
494 // TODO: remove the check for discard here once reduced op splitting is turned on. Currently we
495 // can end up with GrOpsTasks that only have a discard load op and no ops. For vulkan validation
496 // we need to keep that discard and not drop it. Once we have reduce op list splitting enabled
497 // we shouldn't end up with GrOpsTasks with only discard.
498 if (this->isNoOp() || (fClippedContentBounds.isEmpty() && fColorLoadOp != GrLoadOp::kDiscard)) {
499 return false;
500 }
501
502 SkASSERT(this->numTargets() == 1);
503 GrRenderTargetProxy* proxy = this->target(0).proxy()->asRenderTargetProxy();
504 SkASSERT(proxy);
505 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
506
507 // Make sure load ops are not kClear if the GPU needs to use draws for clears
508 SkASSERT(fColorLoadOp != GrLoadOp::kClear ||
509 !flushState->gpu()->caps()->performColorClearsAsDraws());
510
511 const GrCaps& caps = *flushState->gpu()->caps();
512 GrRenderTarget* renderTarget = proxy->peekRenderTarget();
513 SkASSERT(renderTarget);
514
515 GrAttachment* stencil = nullptr;
516 if (int numStencilSamples = proxy->numStencilSamples()) {
517 if (!flushState->resourceProvider()->attachStencilAttachment(
518 renderTarget, numStencilSamples)) {
519 SkDebugf("WARNING: failed to attach a stencil buffer. Rendering will be skipped.\n");
520 return false;
521 }
522 stencil = renderTarget->getStencilAttachment();
523 }
524
525 SkASSERT(!stencil || stencil->numSamples() == proxy->numStencilSamples());
526
527 GrLoadOp stencilLoadOp;
528 switch (fInitialStencilContent) {
529 case StencilContent::kDontCare:
530 stencilLoadOp = GrLoadOp::kDiscard;
531 break;
532 case StencilContent::kUserBitsCleared:
533 SkASSERT(!caps.performStencilClearsAsDraws());
534 SkASSERT(stencil);
535 if (caps.discardStencilValuesAfterRenderPass()) {
536 // Always clear the stencil if it is being discarded after render passes. This is
537 // also an optimization because we are on a tiler and it avoids loading the values
538 // from memory.
539 stencilLoadOp = GrLoadOp::kClear;
540 break;
541 }
542 if (!stencil->hasPerformedInitialClear()) {
543 stencilLoadOp = GrLoadOp::kClear;
544 stencil->markHasPerformedInitialClear();
545 break;
546 }
547 // renderTargetContexts are required to leave the user stencil bits in a cleared state
548 // once finished, meaning the stencil values will always remain cleared after the
549 // initial clear. Just fall through to reloading the existing (cleared) stencil values
550 // from memory.
551 [[fallthrough]];
552 case StencilContent::kPreserved:
553 SkASSERT(stencil);
554 stencilLoadOp = GrLoadOp::kLoad;
555 break;
556 }
557
558 // NOTE: If fMustPreserveStencil is set, then we are executing a renderTargetContext that split
559 // its opsTask.
560 //
561 // FIXME: We don't currently flag render passes that don't use stencil at all. In that case
562 // their store op might be "discard", and we currently make the assumption that a discard will
563 // not invalidate what's already in main memory. This is probably ok for now, but certainly
564 // something we want to address soon.
565 GrStoreOp stencilStoreOp = (caps.discardStencilValuesAfterRenderPass() && !fMustPreserveStencil)
566 ? GrStoreOp::kDiscard
567 : GrStoreOp::kStore;
568
569 GrOpsRenderPass* renderPass = create_render_pass(
570 flushState->gpu(), proxy->peekRenderTarget(), stencil, this->target(0).origin(),
571 fClippedContentBounds, fColorLoadOp, fLoadClearColor, stencilLoadOp, stencilStoreOp,
572 fSampledProxies, fRenderPassXferBarriers);
573
574 if (!renderPass) {
575 return false;
576 }
577 flushState->setOpsRenderPass(renderPass);
578 renderPass->begin();
579
580 // Draw all the generated geometry.
581 for (const auto& chain : fOpChains) {
582 if (!chain.shouldExecute()) {
583 continue;
584 }
585 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
586 TRACE_EVENT0("skia.gpu", chain.head()->name());
587 #endif
588
589 GrOpFlushState::OpArgs opArgs(chain.head(),
590 &fTargets[0],
591 chain.appliedClip(),
592 chain.dstProxyView(),
593 fRenderPassXferBarriers);
594
595 flushState->setOpArgs(&opArgs);
596 chain.head()->execute(flushState, chain.bounds());
597 flushState->setOpArgs(nullptr);
598 }
599
600 renderPass->end();
601 flushState->gpu()->submit(renderPass);
602 flushState->setOpsRenderPass(nullptr);
603
604 return true;
605 }
606
setColorLoadOp(GrLoadOp op,const SkPMColor4f & color)607 void GrOpsTask::setColorLoadOp(GrLoadOp op, const SkPMColor4f& color) {
608 fColorLoadOp = op;
609 fLoadClearColor = color;
610 if (GrLoadOp::kClear == fColorLoadOp) {
611 GrSurfaceProxy* proxy = this->target(0).proxy();
612 SkASSERT(proxy);
613 fTotalBounds = proxy->backingStoreBoundsRect();
614 }
615 }
616
resetForFullscreenClear(CanDiscardPreviousOps canDiscardPreviousOps)617 bool GrOpsTask::resetForFullscreenClear(CanDiscardPreviousOps canDiscardPreviousOps) {
618 if (CanDiscardPreviousOps::kYes == canDiscardPreviousOps || this->isEmpty()) {
619 this->deleteOps();
620 fDeferredProxies.reset();
621 fSampledProxies.reset();
622
623 // If the opsTask is using a render target which wraps a vulkan command buffer, we can't do
624 // a clear load since we cannot change the render pass that we are using. Thus we fall back
625 // to making a clear op in this case.
626 return !this->target(0).asRenderTargetProxy()->wrapsVkSecondaryCB();
627 }
628
629 // Could not empty the task, so an op must be added to handle the clear
630 return false;
631 }
632
discard()633 void GrOpsTask::discard() {
634 // Discard calls to in-progress opsTasks are ignored. Calls at the start update the
635 // opsTasks' color & stencil load ops.
636 if (this->isEmpty()) {
637 fColorLoadOp = GrLoadOp::kDiscard;
638 fInitialStencilContent = StencilContent::kDontCare;
639 fTotalBounds.setEmpty();
640 }
641 }
642
643 ////////////////////////////////////////////////////////////////////////////////
644
645 #if GR_TEST_UTILS
dump(bool printDependencies) const646 void GrOpsTask::dump(bool printDependencies) const {
647 GrRenderTask::dump(printDependencies);
648
649 SkDebugf("fColorLoadOp: ");
650 switch (fColorLoadOp) {
651 case GrLoadOp::kLoad:
652 SkDebugf("kLoad\n");
653 break;
654 case GrLoadOp::kClear:
655 SkDebugf("kClear (0x%x)\n", fLoadClearColor.toBytes_RGBA());
656 break;
657 case GrLoadOp::kDiscard:
658 SkDebugf("kDiscard\n");
659 break;
660 }
661
662 SkDebugf("fInitialStencilContent: ");
663 switch (fInitialStencilContent) {
664 case StencilContent::kDontCare:
665 SkDebugf("kDontCare\n");
666 break;
667 case StencilContent::kUserBitsCleared:
668 SkDebugf("kUserBitsCleared\n");
669 break;
670 case StencilContent::kPreserved:
671 SkDebugf("kPreserved\n");
672 break;
673 }
674
675 SkDebugf("ops (%d):\n", fOpChains.count());
676 for (int i = 0; i < fOpChains.count(); ++i) {
677 SkDebugf("*******************************\n");
678 if (!fOpChains[i].head()) {
679 SkDebugf("%d: <combined forward or failed instantiation>\n", i);
680 } else {
681 SkDebugf("%d: %s\n", i, fOpChains[i].head()->name());
682 SkRect bounds = fOpChains[i].bounds();
683 SkDebugf("ClippedBounds: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", bounds.fLeft,
684 bounds.fTop, bounds.fRight, bounds.fBottom);
685 for (const auto& op : GrOp::ChainRange<>(fOpChains[i].head())) {
686 SkString info = SkTabString(op.dumpInfo(), 1);
687 SkDebugf("%s\n", info.c_str());
688 bounds = op.bounds();
689 SkDebugf("\tClippedBounds: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", bounds.fLeft,
690 bounds.fTop, bounds.fRight, bounds.fBottom);
691 }
692 }
693 }
694 }
695 #endif
696
697 #ifdef SK_DEBUG
visitProxies_debugOnly(const GrOp::VisitProxyFunc & func) const698 void GrOpsTask::visitProxies_debugOnly(const GrOp::VisitProxyFunc& func) const {
699 auto textureFunc = [ func ] (GrSurfaceProxy* tex, GrMipmapped mipmapped) {
700 func(tex, mipmapped);
701 };
702
703 for (const OpChain& chain : fOpChains) {
704 chain.visitProxies(textureFunc);
705 }
706 }
707
708 #endif
709
710 ////////////////////////////////////////////////////////////////////////////////
711
onIsUsed(GrSurfaceProxy * proxyToCheck) const712 bool GrOpsTask::onIsUsed(GrSurfaceProxy* proxyToCheck) const {
713 bool used = false;
714
715 auto visit = [ proxyToCheck, &used ] (GrSurfaceProxy* p, GrMipmapped) {
716 if (p == proxyToCheck) {
717 used = true;
718 }
719 };
720 for (const OpChain& recordedOp : fOpChains) {
721 recordedOp.visitProxies(visit);
722 }
723
724 return used;
725 }
726
handleInternalAllocationFailure()727 void GrOpsTask::handleInternalAllocationFailure() {
728 bool hasUninstantiatedProxy = false;
729 auto checkInstantiation = [&hasUninstantiatedProxy](GrSurfaceProxy* p, GrMipmapped) {
730 if (!p->isInstantiated()) {
731 hasUninstantiatedProxy = true;
732 }
733 };
734 for (OpChain& recordedOp : fOpChains) {
735 hasUninstantiatedProxy = false;
736 recordedOp.visitProxies(checkInstantiation);
737 if (hasUninstantiatedProxy) {
738 recordedOp.setSkipExecuteFlag();
739 }
740 }
741 }
742
gatherProxyIntervals(GrResourceAllocator * alloc) const743 void GrOpsTask::gatherProxyIntervals(GrResourceAllocator* alloc) const {
744 for (int i = 0; i < fDeferredProxies.count(); ++i) {
745 SkASSERT(!fDeferredProxies[i]->isInstantiated());
746 // We give all the deferred proxies a write usage at the very start of flushing. This
747 // locks them out of being reused for the entire flush until they are read - and then
748 // they can be recycled. This is a bit unfortunate because a flush can proceed in waves
749 // with sub-flushes. The deferred proxies only need to be pinned from the start of
750 // the sub-flush in which they appear.
751 alloc->addInterval(fDeferredProxies[i], 0, 0, GrResourceAllocator::ActualUse::kNo);
752 }
753
754 GrSurfaceProxy* targetProxy = this->target(0).proxy();
755
756 // Add the interval for all the writes to this GrOpsTasks's target
757 if (fOpChains.count()) {
758 unsigned int cur = alloc->curOp();
759
760 alloc->addInterval(targetProxy, cur, cur + fOpChains.count() - 1,
761 GrResourceAllocator::ActualUse::kYes);
762 } else {
763 // This can happen if there is a loadOp (e.g., a clear) but no other draws. In this case we
764 // still need to add an interval for the destination so we create a fake op# for
765 // the missing clear op.
766 alloc->addInterval(targetProxy, alloc->curOp(), alloc->curOp(),
767 GrResourceAllocator::ActualUse::kYes);
768 alloc->incOps();
769 }
770
771 auto gather = [ alloc SkDEBUGCODE(, this) ] (GrSurfaceProxy* p, GrMipmapped) {
772 alloc->addInterval(p, alloc->curOp(), alloc->curOp(), GrResourceAllocator::ActualUse::kYes
773 SkDEBUGCODE(, this->target(0).proxy() == p));
774 };
775 for (const OpChain& recordedOp : fOpChains) {
776 recordedOp.visitProxies(gather);
777
778 // Even though the op may have been (re)moved we still need to increment the op count to
779 // keep all the math consistent.
780 alloc->incOps();
781 }
782 }
783
recordOp(GrOp::Owner op,GrProcessorSet::Analysis processorAnalysis,GrAppliedClip * clip,const DstProxyView * dstProxyView,const GrCaps & caps)784 void GrOpsTask::recordOp(
785 GrOp::Owner op, GrProcessorSet::Analysis processorAnalysis, GrAppliedClip* clip,
786 const DstProxyView* dstProxyView, const GrCaps& caps) {
787 SkDEBUGCODE(op->validate();)
788 SkASSERT(processorAnalysis.requiresDstTexture() == (dstProxyView && dstProxyView->proxy()));
789 GrSurfaceProxy* proxy = this->target(0).proxy();
790 SkASSERT(proxy);
791
792 // A closed GrOpsTask should never receive new/more ops
793 SkASSERT(!this->isClosed());
794 if (!op->bounds().isFinite()) {
795 return;
796 }
797
798 // Account for this op's bounds before we attempt to combine.
799 // NOTE: The caller should have already called "op->setClippedBounds()" by now, if applicable.
800 fTotalBounds.join(op->bounds());
801
802 // Check if there is an op we can combine with by linearly searching back until we either
803 // 1) check every op
804 // 2) intersect with something
805 // 3) find a 'blocker'
806 GR_AUDIT_TRAIL_ADD_OP(fAuditTrail, op.get(), proxy->uniqueID());
807 GrOP_INFO("opsTask: %d Recording (%s, opID: %u)\n"
808 "\tBounds [L: %.2f, T: %.2f R: %.2f B: %.2f]\n",
809 this->uniqueID(),
810 op->name(),
811 op->uniqueID(),
812 op->bounds().fLeft, op->bounds().fTop,
813 op->bounds().fRight, op->bounds().fBottom);
814 GrOP_INFO(SkTabString(op->dumpInfo(), 1).c_str());
815 GrOP_INFO("\tOutcome:\n");
816 int maxCandidates = std::min(kMaxOpChainDistance, fOpChains.count());
817 if (maxCandidates) {
818 int i = 0;
819 while (true) {
820 OpChain& candidate = fOpChains.fromBack(i);
821 op = candidate.appendOp(std::move(op), processorAnalysis, dstProxyView, clip, caps,
822 &fArenas, fAuditTrail);
823 if (!op) {
824 return;
825 }
826 // Stop going backwards if we would cause a painter's order violation.
827 if (!can_reorder(candidate.bounds(), op->bounds())) {
828 GrOP_INFO("\t\tBackward: Intersects with chain (%s, head opID: %u)\n",
829 candidate.head()->name(), candidate.head()->uniqueID());
830 break;
831 }
832 if (++i == maxCandidates) {
833 GrOP_INFO("\t\tBackward: Reached max lookback or beginning of op array %d\n", i);
834 break;
835 }
836 }
837 } else {
838 GrOP_INFO("\t\tBackward: FirstOp\n");
839 }
840 if (clip) {
841 clip = fClipAllocator.make<GrAppliedClip>(std::move(*clip));
842 SkDEBUGCODE(fNumClips++;)
843 }
844 fOpChains.emplace_back(std::move(op), processorAnalysis, clip, dstProxyView);
845 }
846
forwardCombine(const GrCaps & caps)847 void GrOpsTask::forwardCombine(const GrCaps& caps) {
848 SkASSERT(!this->isClosed());
849 GrOP_INFO("opsTask: %d ForwardCombine %d ops:\n", this->uniqueID(), fOpChains.count());
850
851 for (int i = 0; i < fOpChains.count() - 1; ++i) {
852 OpChain& chain = fOpChains[i];
853 int maxCandidateIdx = std::min(i + kMaxOpChainDistance, fOpChains.count() - 1);
854 int j = i + 1;
855 while (true) {
856 OpChain& candidate = fOpChains[j];
857 if (candidate.prependChain(&chain, caps, &fArenas, fAuditTrail)) {
858 break;
859 }
860 // Stop traversing if we would cause a painter's order violation.
861 if (!can_reorder(chain.bounds(), candidate.bounds())) {
862 GrOP_INFO(
863 "\t\t%d: chain (%s head opID: %u) -> "
864 "Intersects with chain (%s, head opID: %u)\n",
865 i, chain.head()->name(), chain.head()->uniqueID(), candidate.head()->name(),
866 candidate.head()->uniqueID());
867 break;
868 }
869 if (++j > maxCandidateIdx) {
870 GrOP_INFO("\t\t%d: chain (%s opID: %u) -> Reached max lookahead or end of array\n",
871 i, chain.head()->name(), chain.head()->uniqueID());
872 break;
873 }
874 }
875 }
876 }
877
onMakeClosed(const GrCaps & caps,SkIRect * targetUpdateBounds)878 GrRenderTask::ExpectedOutcome GrOpsTask::onMakeClosed(
879 const GrCaps& caps, SkIRect* targetUpdateBounds) {
880 this->forwardCombine(caps);
881 if (!this->isNoOp()) {
882 GrSurfaceProxy* proxy = this->target(0).proxy();
883 // Use the entire backing store bounds since the GPU doesn't clip automatically to the
884 // logical dimensions.
885 SkRect clippedContentBounds = proxy->backingStoreBoundsRect();
886 // TODO: If we can fix up GLPrograms test to always intersect the target proxy bounds
887 // then we can simply assert here that the bounds intersect.
888 if (clippedContentBounds.intersect(fTotalBounds)) {
889 clippedContentBounds.roundOut(&fClippedContentBounds);
890 *targetUpdateBounds = fClippedContentBounds;
891 return ExpectedOutcome::kTargetDirty;
892 }
893 }
894 return ExpectedOutcome::kTargetUnchanged;
895 }
896