• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "src/gpu/ganesh/ops/OpsTask.h"
8 
9 #include "include/core/SkSize.h"
10 #include "include/core/SkString.h"
11 #include "include/gpu/GpuTypes.h"
12 #include "include/gpu/ganesh/GrRecordingContext.h"
13 #include "include/private/base/SkPoint_impl.h"
14 #include "src/base/SkArenaAlloc.h"
15 #include "src/base/SkScopeExit.h"
16 #include "src/core/SkRectPriv.h"
17 #include "src/core/SkStringUtils.h"
18 #include "src/core/SkTraceEvent.h"
19 #include "src/gpu/ganesh/GrAppliedClip.h"
20 #include "src/gpu/ganesh/GrAttachment.h"
21 #include "src/gpu/ganesh/GrAuditTrail.h"
22 #include "src/gpu/ganesh/GrCaps.h"
23 #include "src/gpu/ganesh/GrGpu.h"
24 #include "src/gpu/ganesh/GrNativeRect.h"
25 #include "src/gpu/ganesh/GrOpFlushState.h"
26 #include "src/gpu/ganesh/GrOpsRenderPass.h"
27 #include "src/gpu/ganesh/GrRecordingContextPriv.h"
28 #include "src/gpu/ganesh/GrRenderTarget.h"
29 #include "src/gpu/ganesh/GrRenderTargetProxy.h"
30 #include "src/gpu/ganesh/GrResourceAllocator.h"
31 #include "src/gpu/ganesh/GrResourceProvider.h"
32 #include "src/gpu/ganesh/GrSurfaceProxyView.h"
33 #include "src/gpu/ganesh/GrTextureProxy.h"
34 #include "src/gpu/ganesh/GrTextureResolveManager.h"
35 #include "src/gpu/ganesh/geometry/GrRect.h"
36 
37 #include <algorithm>
38 #include <cstddef>
39 #include <cstdint>
40 #include <functional>
41 #include <memory>
42 #include <utility>
43 
44 class GrDrawingManager;
45 enum GrSurfaceOrigin : int;
46 
47 using namespace skia_private;
48 
49 ////////////////////////////////////////////////////////////////////////////////
50 
51 namespace {
52 
53 // Experimentally we have found that most combining occurs within the first 10 comparisons.
54 static const int kMaxOpMergeDistance = 10;
55 static const int kMaxOpChainDistance = 10;
56 
57 ////////////////////////////////////////////////////////////////////////////////
58 
can_reorder(const SkRect & a,const SkRect & b)59 inline bool can_reorder(const SkRect& a, const SkRect& b) { return !GrRectsOverlap(a, b); }
60 
create_render_pass(GrGpu * gpu,GrRenderTarget * rt,bool useMSAASurface,GrAttachment * stencil,GrSurfaceOrigin origin,const SkIRect & bounds,GrLoadOp colorLoadOp,const std::array<float,4> & loadClearColor,GrLoadOp stencilLoadOp,GrStoreOp stencilStoreOp,const TArray<GrSurfaceProxy *,true> & sampledProxies,GrXferBarrierFlags renderPassXferBarriers)61 GrOpsRenderPass* create_render_pass(GrGpu* gpu,
62                                     GrRenderTarget* rt,
63                                     bool useMSAASurface,
64                                     GrAttachment* stencil,
65                                     GrSurfaceOrigin origin,
66                                     const SkIRect& bounds,
67                                     GrLoadOp colorLoadOp,
68                                     const std::array<float, 4>& loadClearColor,
69                                     GrLoadOp stencilLoadOp,
70                                     GrStoreOp stencilStoreOp,
71                                     const TArray<GrSurfaceProxy*, true>& sampledProxies,
72                                     GrXferBarrierFlags renderPassXferBarriers) {
73     const GrOpsRenderPass::LoadAndStoreInfo kColorLoadStoreInfo {
74         colorLoadOp,
75         GrStoreOp::kStore,
76         loadClearColor
77     };
78 
79     // TODO:
80     // We would like to (at this level) only ever clear & discard. We would need
81     // to stop splitting up higher level OpsTasks for copyOps to achieve that.
82     // Note: we would still need SB loads and stores but they would happen at a
83     // lower level (inside the VK command buffer).
84     const GrOpsRenderPass::StencilLoadAndStoreInfo stencilLoadAndStoreInfo {
85         stencilLoadOp,
86         stencilStoreOp,
87     };
88 
89     return gpu->getOpsRenderPass(rt, useMSAASurface, stencil, origin, bounds, kColorLoadStoreInfo,
90                                  stencilLoadAndStoreInfo, sampledProxies, renderPassXferBarriers);
91 }
92 
93 } // anonymous namespace
94 
95 ////////////////////////////////////////////////////////////////////////////////
96 
97 namespace skgpu::ganesh {
98 
List(GrOp::Owner op)99 inline OpsTask::OpChain::List::List(GrOp::Owner op)
100         : fHead(std::move(op)), fTail(fHead.get()) {
101     this->validate();
102 }
103 
List(List && that)104 inline OpsTask::OpChain::List::List(List&& that) { *this = std::move(that); }
105 
operator =(List && that)106 inline OpsTask::OpChain::List& OpsTask::OpChain::List::operator=(List&& that) {
107     fHead = std::move(that.fHead);
108     fTail = that.fTail;
109     that.fTail = nullptr;
110     this->validate();
111     return *this;
112 }
113 
popHead()114 inline GrOp::Owner OpsTask::OpChain::List::popHead() {
115     SkASSERT(fHead);
116     auto temp = fHead->cutChain();
117     std::swap(temp, fHead);
118     if (!fHead) {
119         SkASSERT(fTail == temp.get());
120         fTail = nullptr;
121     }
122     return temp;
123 }
124 
removeOp(GrOp * op)125 inline GrOp::Owner OpsTask::OpChain::List::removeOp(GrOp* op) {
126 #ifdef SK_DEBUG
127     auto head = op;
128     while (head->prevInChain()) { head = head->prevInChain(); }
129     SkASSERT(head == fHead.get());
130 #endif
131     auto prev = op->prevInChain();
132     if (!prev) {
133         SkASSERT(op == fHead.get());
134         return this->popHead();
135     }
136     auto temp = prev->cutChain();
137     if (auto next = temp->cutChain()) {
138         prev->chainConcat(std::move(next));
139     } else {
140         SkASSERT(fTail == op);
141         fTail = prev;
142     }
143     this->validate();
144     return temp;
145 }
146 
pushHead(GrOp::Owner op)147 inline void OpsTask::OpChain::List::pushHead(GrOp::Owner op) {
148     SkASSERT(op);
149     SkASSERT(op->isChainHead());
150     SkASSERT(op->isChainTail());
151     if (fHead) {
152         op->chainConcat(std::move(fHead));
153         fHead = std::move(op);
154     } else {
155         fHead = std::move(op);
156         fTail = fHead.get();
157     }
158 }
159 
pushTail(GrOp::Owner op)160 inline void OpsTask::OpChain::List::pushTail(GrOp::Owner op) {
161     SkASSERT(op->isChainTail());
162     fTail->chainConcat(std::move(op));
163     fTail = fTail->nextInChain();
164 }
165 
validate() const166 inline void OpsTask::OpChain::List::validate() const {
167 #ifdef SK_DEBUG
168     if (fHead) {
169         SkASSERT(fTail);
170         fHead->validateChain(fTail);
171     }
172 #endif
173 }
174 
175 ////////////////////////////////////////////////////////////////////////////////
176 
OpChain(GrOp::Owner op,GrProcessorSet::Analysis processorAnalysis,GrAppliedClip * appliedClip,const GrDstProxyView * dstProxyView)177 OpsTask::OpChain::OpChain(GrOp::Owner op, GrProcessorSet::Analysis processorAnalysis,
178                           GrAppliedClip* appliedClip, const GrDstProxyView* dstProxyView)
179         : fList{std::move(op)}
180         , fProcessorAnalysis(processorAnalysis)
181         , fAppliedClip(appliedClip) {
182     if (fProcessorAnalysis.requiresDstTexture()) {
183         SkASSERT(dstProxyView && dstProxyView->proxy());
184         fDstProxyView = *dstProxyView;
185     }
186     fBounds = fList.head()->bounds();
187 }
188 
visitProxies(const GrVisitProxyFunc & func) const189 void OpsTask::OpChain::visitProxies(const GrVisitProxyFunc& func) const {
190     if (fList.empty()) {
191         return;
192     }
193     for (const auto& op : GrOp::ChainRange<>(fList.head())) {
194         op.visitProxies(func);
195     }
196     if (fDstProxyView.proxy()) {
197         func(fDstProxyView.proxy(), skgpu::Mipmapped::kNo);
198     }
199     if (fAppliedClip) {
200         fAppliedClip->visitProxies(func);
201     }
202 }
203 
deleteOps()204 void OpsTask::OpChain::deleteOps() {
205     while (!fList.empty()) {
206         // Since the value goes out of scope immediately, the GrOp::Owner deletes the op.
207         fList.popHead();
208     }
209 }
210 
211 // Concatenates two op chains and attempts to merge ops across the chains. Assumes that we know that
212 // the two chains are chainable. Returns the new chain.
DoConcat(List chainA,List chainB,const GrCaps & caps,SkArenaAlloc * opsTaskArena,GrAuditTrail * auditTrail)213 OpsTask::OpChain::List OpsTask::OpChain::DoConcat(List chainA, List chainB, const GrCaps& caps,
214                                                   SkArenaAlloc* opsTaskArena,
215                                                   GrAuditTrail* auditTrail) {
216     // We process ops in chain b from head to tail. We attempt to merge with nodes in a, starting
217     // at chain a's tail and working toward the head. We produce one of the following outcomes:
218     // 1) b's head is merged into an op in a.
219     // 2) An op from chain a is merged into b's head. (In this case b's head gets processed again.)
220     // 3) b's head is popped from chain a and added at the tail of a.
221     // After result 3 we don't want to attempt to merge the next head of b with the new tail of a,
222     // as we assume merges were already attempted when chain b was created. So we keep track of the
223     // original tail of a and start our iteration of a there. We also track the bounds of the nodes
224     // appended to chain a that will be skipped for bounds testing. If the original tail of a is
225     // merged into an op in b (case 2) then we advance the "original tail" towards the head of a.
226     GrOp* origATail = chainA.tail();
227     SkRect skipBounds = SkRectPriv::MakeLargestInverted();
228     do {
229         int numMergeChecks = 0;
230         bool merged = false;
231         bool noSkip = (origATail == chainA.tail());
232         SkASSERT(noSkip == (skipBounds == SkRectPriv::MakeLargestInverted()));
233         bool canBackwardMerge = noSkip || can_reorder(chainB.head()->bounds(), skipBounds);
234         SkRect forwardMergeBounds = skipBounds;
235         GrOp* a = origATail;
236         while (a) {
237             bool canForwardMerge =
238                     (a == chainA.tail()) || can_reorder(a->bounds(), forwardMergeBounds);
239             if (canForwardMerge || canBackwardMerge) {
240                 auto result = a->combineIfPossible(chainB.head(), opsTaskArena, caps);
241                 SkASSERT(result != GrOp::CombineResult::kCannotCombine);
242                 merged = (result == GrOp::CombineResult::kMerged);
243                 GrOP_INFO("\t\t: (%s opID: %u) -> Combining with (%s, opID: %u)\n",
244                           chainB.head()->name(), chainB.head()->uniqueID(), a->name(),
245                           a->uniqueID());
246             }
247             if (merged) {
248                 GR_AUDIT_TRAIL_OPS_RESULT_COMBINED(auditTrail, a, chainB.head());
249                 if (canBackwardMerge) {
250                     // The GrOp::Owner releases the op.
251                     chainB.popHead();
252                 } else {
253                     // We merged the contents of b's head into a. We will replace b's head with a in
254                     // chain b.
255                     SkASSERT(canForwardMerge);
256                     if (a == origATail) {
257                         origATail = a->prevInChain();
258                     }
259                     GrOp::Owner detachedA = chainA.removeOp(a);
260                     // The GrOp::Owner releases the op.
261                     chainB.popHead();
262                     chainB.pushHead(std::move(detachedA));
263                     if (chainA.empty()) {
264                         // We merged all the nodes in chain a to chain b.
265                         return chainB;
266                     }
267                 }
268                 break;
269             } else {
270                 if (++numMergeChecks == kMaxOpMergeDistance) {
271                     break;
272                 }
273                 forwardMergeBounds.joinNonEmptyArg(a->bounds());
274                 canBackwardMerge =
275                         canBackwardMerge && can_reorder(chainB.head()->bounds(), a->bounds());
276                 a = a->prevInChain();
277             }
278         }
279         // If we weren't able to merge b's head then pop b's head from chain b and make it the new
280         // tail of a.
281         if (!merged) {
282             chainA.pushTail(chainB.popHead());
283             skipBounds.joinNonEmptyArg(chainA.tail()->bounds());
284         }
285     } while (!chainB.empty());
286     return chainA;
287 }
288 
289 // Attempts to concatenate the given chain onto our own and merge ops across the chains. Returns
290 // whether the operation succeeded. On success, the provided list will be returned empty.
tryConcat(List * list,GrProcessorSet::Analysis processorAnalysis,const GrDstProxyView & dstProxyView,const GrAppliedClip * appliedClip,const SkRect & bounds,const GrCaps & caps,SkArenaAlloc * opsTaskArena,GrAuditTrail * auditTrail)291 bool OpsTask::OpChain::tryConcat(
292         List* list, GrProcessorSet::Analysis processorAnalysis, const GrDstProxyView& dstProxyView,
293         const GrAppliedClip* appliedClip, const SkRect& bounds, const GrCaps& caps,
294         SkArenaAlloc* opsTaskArena, GrAuditTrail* auditTrail) {
295     SkASSERT(!fList.empty());
296     SkASSERT(!list->empty());
297     SkASSERT(fProcessorAnalysis.requiresDstTexture() == SkToBool(fDstProxyView.proxy()));
298     SkASSERT(processorAnalysis.requiresDstTexture() == SkToBool(dstProxyView.proxy()));
299     // All returns use explicit tuple constructor rather than {a, b} to work around old GCC bug.
300     if (fList.head()->classID() != list->head()->classID() ||
301         SkToBool(fAppliedClip) != SkToBool(appliedClip) ||
302         (fAppliedClip && *fAppliedClip != *appliedClip) ||
303         (fProcessorAnalysis.requiresNonOverlappingDraws() !=
304                 processorAnalysis.requiresNonOverlappingDraws()) ||
305         (fProcessorAnalysis.requiresNonOverlappingDraws() &&
306                 // Non-overlaping draws are only required when Ganesh will either insert a barrier,
307                 // or read back a new dst texture between draws. In either case, we can neither
308                 // chain nor combine overlapping Ops.
309                 GrRectsTouchOrOverlap(fBounds, bounds)) ||
310         (fProcessorAnalysis.requiresDstTexture() != processorAnalysis.requiresDstTexture()) ||
311         (fProcessorAnalysis.requiresDstTexture() && fDstProxyView != dstProxyView)) {
312         return false;
313     }
314 
315     SkDEBUGCODE(bool first = true;)
316     do {
317         switch (fList.tail()->combineIfPossible(list->head(), opsTaskArena, caps))
318         {
319             case GrOp::CombineResult::kCannotCombine:
320                 // If an op supports chaining then it is required that chaining is transitive and
321                 // that if any two ops in two different chains can merge then the two chains
322                 // may also be chained together. Thus, we should only hit this on the first
323                 // iteration.
324                 SkASSERT(first);
325                 return false;
326             case GrOp::CombineResult::kMayChain:
327                 fList = DoConcat(std::move(fList), std::exchange(*list, List()), caps, opsTaskArena,
328                                  auditTrail);
329                 // The above exchange cleared out 'list'. The list needs to be empty now for the
330                 // loop to terminate.
331                 SkASSERT(list->empty());
332                 break;
333             case GrOp::CombineResult::kMerged: {
334                 GrOP_INFO("\t\t: (%s opID: %u) -> Combining with (%s, opID: %u)\n",
335                           list->tail()->name(), list->tail()->uniqueID(), list->head()->name(),
336                           list->head()->uniqueID());
337                 GR_AUDIT_TRAIL_OPS_RESULT_COMBINED(auditTrail, fList.tail(), list->head());
338                 // The GrOp::Owner releases the op.
339                 list->popHead();
340                 break;
341             }
342         }
343         SkDEBUGCODE(first = false);
344     } while (!list->empty());
345 
346     // The new ops were successfully merged and/or chained onto our own.
347     fBounds.joinPossiblyEmptyRect(bounds);
348     return true;
349 }
350 
prependChain(OpChain * that,const GrCaps & caps,SkArenaAlloc * opsTaskArena,GrAuditTrail * auditTrail)351 bool OpsTask::OpChain::prependChain(OpChain* that, const GrCaps& caps, SkArenaAlloc* opsTaskArena,
352                                     GrAuditTrail* auditTrail) {
353     if (!that->tryConcat(&fList, fProcessorAnalysis, fDstProxyView, fAppliedClip, fBounds, caps,
354                          opsTaskArena, auditTrail)) {
355         this->validate();
356         // append failed
357         return false;
358     }
359 
360     // 'that' owns the combined chain. Move it into 'this'.
361     SkASSERT(fList.empty());
362     fList = std::move(that->fList);
363     fBounds = that->fBounds;
364 
365     that->fDstProxyView.setProxyView({});
366     if (that->fAppliedClip && that->fAppliedClip->hasCoverageFragmentProcessor()) {
367         // Obliterates the processor.
368         that->fAppliedClip->detachCoverageFragmentProcessor();
369     }
370     this->validate();
371     return true;
372 }
373 
appendOp(GrOp::Owner op,GrProcessorSet::Analysis processorAnalysis,const GrDstProxyView * dstProxyView,const GrAppliedClip * appliedClip,const GrCaps & caps,SkArenaAlloc * opsTaskArena,GrAuditTrail * auditTrail)374 GrOp::Owner OpsTask::OpChain::appendOp(
375         GrOp::Owner op, GrProcessorSet::Analysis processorAnalysis,
376         const GrDstProxyView* dstProxyView, const GrAppliedClip* appliedClip, const GrCaps& caps,
377         SkArenaAlloc* opsTaskArena, GrAuditTrail* auditTrail) {
378     const GrDstProxyView noDstProxyView;
379     if (!dstProxyView) {
380         dstProxyView = &noDstProxyView;
381     }
382     SkASSERT(op->isChainHead() && op->isChainTail());
383     SkRect opBounds = op->bounds();
384     List chain(std::move(op));
385     if (!this->tryConcat(&chain, processorAnalysis, *dstProxyView, appliedClip, opBounds, caps,
386                          opsTaskArena, auditTrail)) {
387         // append failed, give the op back to the caller.
388         this->validate();
389         return chain.popHead();
390     }
391 
392     SkASSERT(chain.empty());
393     this->validate();
394     return nullptr;
395 }
396 
validate() const397 inline void OpsTask::OpChain::validate() const {
398 #ifdef SK_DEBUG
399     fList.validate();
400     for (const auto& op : GrOp::ChainRange<>(fList.head())) {
401         // Not using SkRect::contains because we allow empty rects.
402         SkASSERT(fBounds.fLeft <= op.bounds().fLeft && fBounds.fTop <= op.bounds().fTop &&
403                  fBounds.fRight >= op.bounds().fRight && fBounds.fBottom >= op.bounds().fBottom);
404     }
405 #endif
406 }
407 
408 ////////////////////////////////////////////////////////////////////////////////
409 
OpsTask(GrDrawingManager * drawingMgr,GrSurfaceProxyView view,GrAuditTrail * auditTrail,sk_sp<GrArenas> arenas)410 OpsTask::OpsTask(GrDrawingManager* drawingMgr,
411                  GrSurfaceProxyView view,
412                  GrAuditTrail* auditTrail,
413                  sk_sp<GrArenas> arenas)
414         : GrRenderTask()
415         , fAuditTrail(auditTrail)
416         , fUsesMSAASurface(view.asRenderTargetProxy()->numSamples() > 1)
417         , fTargetSwizzle(view.swizzle())
418         , fTargetOrigin(view.origin())
419         , fArenas{std::move(arenas)}
420           SkDEBUGCODE(, fNumClips(0)) {
421     this->addTarget(drawingMgr, view.detachProxy());
422 }
423 
deleteOps()424 void OpsTask::deleteOps() {
425     for (auto& chain : fOpChains) {
426         chain.deleteOps();
427     }
428     fOpChains.clear();
429 }
430 
~OpsTask()431 OpsTask::~OpsTask() {
432     this->deleteOps();
433 }
434 
addOp(GrDrawingManager * drawingMgr,GrOp::Owner op,GrTextureResolveManager textureResolveManager,const GrCaps & caps)435 void OpsTask::addOp(GrDrawingManager* drawingMgr, GrOp::Owner op,
436                     GrTextureResolveManager textureResolveManager, const GrCaps& caps) {
437     auto addDependency = [&](GrSurfaceProxy* p, skgpu::Mipmapped mipmapped) {
438         this->addDependency(drawingMgr, p, mipmapped, textureResolveManager, caps);
439     };
440 
441     op->visitProxies(addDependency);
442 
443     this->recordOp(std::move(op), false/*usesMSAA*/, GrProcessorSet::EmptySetAnalysis(), nullptr,
444                    nullptr, caps);
445 }
446 
addDrawOp(GrDrawingManager * drawingMgr,GrOp::Owner op,bool usesMSAA,const GrProcessorSet::Analysis & processorAnalysis,GrAppliedClip && clip,const GrDstProxyView & dstProxyView,GrTextureResolveManager textureResolveManager,const GrCaps & caps)447 void OpsTask::addDrawOp(GrDrawingManager* drawingMgr, GrOp::Owner op, bool usesMSAA,
448                         const GrProcessorSet::Analysis& processorAnalysis, GrAppliedClip&& clip,
449                         const GrDstProxyView& dstProxyView,
450                         GrTextureResolveManager textureResolveManager, const GrCaps& caps) {
451 #ifdef SKIA_OHOS
452     if (UNLIKELY(SkOHOSDebugLevelTraceUtil::getEnableDebugTrace()) && drawingMgr) {
453         drawingMgr->increaseDrawOpNum();
454     }
455 #endif
456     auto addDependency = [&](GrSurfaceProxy* p, skgpu::Mipmapped mipmapped) {
457         this->addSampledTexture(p);
458         this->addDependency(drawingMgr, p, mipmapped, textureResolveManager, caps);
459     };
460 
461     op->visitProxies(addDependency);
462     clip.visitProxies(addDependency);
463     if (dstProxyView.proxy()) {
464         if (!(dstProxyView.dstSampleFlags() & GrDstSampleFlags::kAsInputAttachment)) {
465             this->addSampledTexture(dstProxyView.proxy());
466         }
467         if (dstProxyView.dstSampleFlags() & GrDstSampleFlags::kRequiresTextureBarrier) {
468             fRenderPassXferBarriers |= GrXferBarrierFlags::kTexture;
469         }
470         addDependency(dstProxyView.proxy(), skgpu::Mipmapped::kNo);
471         SkASSERT(!(dstProxyView.dstSampleFlags() & GrDstSampleFlags::kAsInputAttachment) ||
472                  dstProxyView.offset().isZero());
473     }
474 
475     if (processorAnalysis.usesNonCoherentHWBlending()) {
476         fRenderPassXferBarriers |= GrXferBarrierFlags::kBlend;
477     }
478 
479     this->recordOp(std::move(op), usesMSAA, processorAnalysis, clip.doesClip() ? &clip : nullptr,
480                    &dstProxyView, caps);
481 }
482 
endFlush(GrDrawingManager * drawingMgr)483 void OpsTask::endFlush(GrDrawingManager* drawingMgr) {
484     fLastClipStackGenID = SK_InvalidUniqueID;
485     this->deleteOps();
486 
487     fDeferredProxies.clear();
488     fSampledProxies.clear();
489     fAuditTrail = nullptr;
490 #ifdef SKIA_OHOS
491     fNumOpChainsExecuted = 0;
492 #endif
493     GrRenderTask::endFlush(drawingMgr);
494 }
495 
onPrePrepare(GrRecordingContext * context)496 void OpsTask::onPrePrepare(GrRecordingContext* context) {
497     SkASSERT(this->isClosed());
498     // TODO: remove the check for discard here once reduced op splitting is turned on. Currently we
499     // can end up with OpsTasks that only have a discard load op and no ops. For vulkan validation
500     // we need to keep that discard and not drop it. Once we have reduce op list splitting enabled
501     // we shouldn't end up with OpsTasks with only discard.
502     if (this->isColorNoOp() ||
503         (fClippedContentBounds.isEmpty() && fColorLoadOp != GrLoadOp::kDiscard)) {
504         return;
505     }
506     TRACE_EVENT0("skia.gpu", TRACE_FUNC);
507 
508     GrSurfaceProxyView dstView(sk_ref_sp(this->target(0)), fTargetOrigin, fTargetSwizzle);
509     for (const auto& chain : fOpChains) {
510         if (chain.shouldExecute()) {
511             chain.head()->prePrepare(context,
512                                      dstView,
513                                      chain.appliedClip(),
514                                      chain.dstProxyView(),
515                                      fRenderPassXferBarriers,
516                                      fColorLoadOp);
517         }
518     }
519 }
520 
onPrepare(GrOpFlushState * flushState)521 void OpsTask::onPrepare(GrOpFlushState* flushState) {
522     SkASSERT(this->target(0)->peekRenderTarget());
523     SkASSERT(this->isClosed());
524     // TODO: remove the check for discard here once reduced op splitting is turned on. Currently we
525     // can end up with OpsTasks that only have a discard load op and no ops. For vulkan validation
526     // we need to keep that discard and not drop it. Once we have reduce op list splitting enabled
527     // we shouldn't end up with OpsTasks with only discard.
528     if (this->isColorNoOp() ||
529         (fClippedContentBounds.isEmpty() && fColorLoadOp != GrLoadOp::kDiscard)) {
530         return;
531     }
532 #ifdef SKIA_OHOS
533     HITRACE_OHOS_NAME_FMT_LEVEL(DebugTraceLevel::DETAIL, "onPrepare");
534 #else
535     TRACE_EVENT0_ALWAYS("skia.gpu", TRACE_FUNC);
536 #endif
537 
538     flushState->setSampledProxyArray(&fSampledProxies);
539     GrSurfaceProxyView dstView(sk_ref_sp(this->target(0)), fTargetOrigin, fTargetSwizzle);
540     auto grGpu = flushState->gpu();
541     // Loop over the ops that haven't yet been prepared.
542     GrGpuResourceTag tag;
543     for (const auto& chain : fOpChains) {
544         if (chain.shouldExecute()) {
545             tag = chain.head()->getGrOpTag();
546             if (grGpu && tag.isGrTagValid()) {
547                 grGpu->setCurrentGrResourceTag(tag);
548             }
549             GrOpFlushState::OpArgs opArgs(chain.head(),
550                                           dstView,
551                                           fUsesMSAASurface,
552                                           chain.appliedClip(),
553                                           chain.dstProxyView(),
554                                           fRenderPassXferBarriers,
555                                           fColorLoadOp);
556 
557             flushState->setOpArgs(&opArgs);
558 
559             // Temporary debugging helper: for debugging prePrepare w/o going through DDLs
560             // Delete once most of the GrOps have an onPrePrepare.
561             // chain.head()->prePrepare(flushState->gpu()->getContext(), &this->target(0),
562             //                          chain.appliedClip());
563 
564             // GrOp::prePrepare may or may not have been called at this point
565             chain.head()->prepare(flushState);
566             flushState->setOpArgs(nullptr);
567             if (grGpu && tag.isGrTagValid()) {
568                 grGpu->popGrResourceTag();
569             }
570         }
571     }
572     flushState->setSampledProxyArray(nullptr);
573 }
574 
575 // TODO: this is where GrOp::renderTarget is used (which is fine since it
576 // is at flush time). However, we need to store the RenderTargetProxy in the
577 // Ops and instantiate them here.
onExecute(GrOpFlushState * flushState)578 bool OpsTask::onExecute(GrOpFlushState* flushState) {
579     SkASSERT(this->numTargets() == 1);
580     GrRenderTargetProxy* proxy = this->target(0)->asRenderTargetProxy();
581     SkASSERT(proxy);
582     SK_AT_SCOPE_EXIT(proxy->clearArenas());
583 
584     if (this->isColorNoOp() || fClippedContentBounds.isEmpty()) {
585         return false;
586     }
587 #ifdef SKIA_OHOS
588     HITRACE_OHOS_NAME_FMT_LEVEL(DebugTraceLevel::DETAIL, "onExecute");
589 #else
590     TRACE_EVENT0_ALWAYS("skia.gpu", TRACE_FUNC);
591 #endif
592 
593     // Make sure load ops are not kClear if the GPU needs to use draws for clears
594     SkASSERT(fColorLoadOp != GrLoadOp::kClear ||
595              !flushState->gpu()->caps()->performColorClearsAsDraws());
596 
597     const GrCaps& caps = *flushState->gpu()->caps();
598     GrRenderTarget* renderTarget = proxy->peekRenderTarget();
599     SkASSERT(renderTarget);
600 
601     GrAttachment* stencil = nullptr;
602     if (proxy->needsStencil()) {
603         SkASSERT(proxy->canUseStencil(caps));
604         if (!flushState->resourceProvider()->attachStencilAttachment(renderTarget,
605                                                                      fUsesMSAASurface)) {
606             SkDebugf("WARNING: failed to attach a stencil buffer. Rendering will be skipped.\n");
607             return false;
608         }
609         stencil = renderTarget->getStencilAttachment(fUsesMSAASurface);
610     }
611 
612     GrLoadOp stencilLoadOp;
613     switch (fInitialStencilContent) {
614         case StencilContent::kDontCare:
615             stencilLoadOp = GrLoadOp::kDiscard;
616             break;
617         case StencilContent::kUserBitsCleared:
618             SkASSERT(!caps.performStencilClearsAsDraws());
619             SkASSERT(stencil);
620             if (caps.discardStencilValuesAfterRenderPass()) {
621                 // Always clear the stencil if it is being discarded after render passes. This is
622                 // also an optimization because we are on a tiler and it avoids loading the values
623                 // from memory.
624                 stencilLoadOp = GrLoadOp::kClear;
625                 break;
626             }
627             if (!stencil->hasPerformedInitialClear()) {
628                 stencilLoadOp = GrLoadOp::kClear;
629                 stencil->markHasPerformedInitialClear();
630                 break;
631             }
632             // SurfaceDrawContexts are required to leave the user stencil bits in a cleared state
633             // once finished, meaning the stencil values will always remain cleared after the
634             // initial clear. Just fall through to reloading the existing (cleared) stencil values
635             // from memory.
636             [[fallthrough]];
637         case StencilContent::kPreserved:
638             SkASSERT(stencil);
639             stencilLoadOp = GrLoadOp::kLoad;
640             break;
641     }
642 
643     // NOTE: If fMustPreserveStencil is set, then we are executing a surfaceDrawContext that split
644     // its opsTask.
645     //
646     // FIXME: We don't currently flag render passes that don't use stencil at all. In that case
647     // their store op might be "discard", and we currently make the assumption that a discard will
648     // not invalidate what's already in main memory. This is probably ok for now, but certainly
649     // something we want to address soon.
650     GrStoreOp stencilStoreOp = (caps.discardStencilValuesAfterRenderPass() && !fMustPreserveStencil)
651             ? GrStoreOp::kDiscard
652             : GrStoreOp::kStore;
653 
654     GrOpsRenderPass* renderPass = create_render_pass(flushState->gpu(),
655                                                      proxy->peekRenderTarget(),
656                                                      fUsesMSAASurface,
657                                                      stencil,
658                                                      fTargetOrigin,
659                                                      fClippedContentBounds,
660                                                      fColorLoadOp,
661                                                      fLoadClearColor,
662                                                      stencilLoadOp,
663                                                      stencilStoreOp,
664                                                      fSampledProxies,
665                                                      fRenderPassXferBarriers);
666 
667     if (!renderPass) {
668         return false;
669     }
670     flushState->setOpsRenderPass(renderPass);
671     renderPass->begin();
672 
673     GrSurfaceProxyView dstView(sk_ref_sp(this->target(0)), fTargetOrigin, fTargetSwizzle);
674 
675     auto grGpu = flushState->gpu();
676     // Draw all the generated geometry.
677     GrGpuResourceTag tag;
678     for (const auto& chain : fOpChains) {
679         if (!chain.shouldExecute()) {
680             continue;
681         }
682 #ifdef SKIA_OHOS
683         fNumOpChainsExecuted++;
684 #endif
685         tag = chain.head()->getGrOpTag();
686             if (grGpu && tag.isGrTagValid()) {
687                 grGpu->setCurrentGrResourceTag(tag);
688             }
689 
690         GrOpFlushState::OpArgs opArgs(chain.head(),
691                                       dstView,
692                                       fUsesMSAASurface,
693                                       chain.appliedClip(),
694                                       chain.dstProxyView(),
695                                       fRenderPassXferBarriers,
696                                       fColorLoadOp);
697 
698         flushState->setOpArgs(&opArgs);
699         chain.head()->execute(flushState, chain.bounds());
700         flushState->setOpArgs(nullptr);
701         if (grGpu && tag.isGrTagValid()) {
702                 grGpu->popGrResourceTag();
703             }
704     }
705 
706     renderPass->end();
707     flushState->gpu()->submit(renderPass);
708     flushState->setOpsRenderPass(nullptr);
709 
710     return true;
711 }
712 
setColorLoadOp(GrLoadOp op,std::array<float,4> color)713 void OpsTask::setColorLoadOp(GrLoadOp op, std::array<float, 4> color) {
714     fColorLoadOp = op;
715     fLoadClearColor = color;
716     if (GrLoadOp::kClear == fColorLoadOp) {
717         GrSurfaceProxy* proxy = this->target(0);
718         SkASSERT(proxy);
719         fTotalBounds = proxy->backingStoreBoundsRect();
720     }
721 }
722 
reset()723 void OpsTask::reset() {
724     fDeferredProxies.clear();
725     fSampledProxies.clear();
726     fClippedContentBounds = SkIRect::MakeEmpty();
727     fTotalBounds = SkRect::MakeEmpty();
728     this->deleteOps();
729     fRenderPassXferBarriers = GrXferBarrierFlags::kNone;
730 }
731 
canMerge(const OpsTask * opsTask) const732 bool OpsTask::canMerge(const OpsTask* opsTask) const {
733     return this->target(0) == opsTask->target(0) &&
734            fArenas == opsTask->fArenas &&
735            !opsTask->fCannotMergeBackward;
736 }
737 
mergeFrom(SkSpan<const sk_sp<GrRenderTask>> tasks)738 int OpsTask::mergeFrom(SkSpan<const sk_sp<GrRenderTask>> tasks) {
739     int mergedCount = 0;
740     for (const sk_sp<GrRenderTask>& task : tasks) {
741         auto opsTask = task->asOpsTask();
742         if (!opsTask || !this->canMerge(opsTask)) {
743             break;
744         }
745         SkASSERT(fTargetSwizzle == opsTask->fTargetSwizzle);
746         SkASSERT(fTargetOrigin == opsTask->fTargetOrigin);
747         if (GrLoadOp::kClear == opsTask->fColorLoadOp) {
748             // TODO(11903): Go back to actually dropping ops tasks when we are merged with
749             // color clear.
750             return 0;
751         }
752         mergedCount += 1;
753     }
754     if (0 == mergedCount) {
755         return 0;
756     }
757 
758     SkSpan<const sk_sp<OpsTask>> mergingNodes(
759             reinterpret_cast<const sk_sp<OpsTask>*>(tasks.data()), SkToSizeT(mergedCount));
760     int addlDeferredProxyCount = 0;
761     int addlProxyCount = 0;
762     int addlOpChainCount = 0;
763     for (const auto& toMerge : mergingNodes) {
764         addlDeferredProxyCount += toMerge->fDeferredProxies.size();
765         addlProxyCount += toMerge->fSampledProxies.size();
766         addlOpChainCount += toMerge->fOpChains.size();
767         fClippedContentBounds.join(toMerge->fClippedContentBounds);
768         fTotalBounds.join(toMerge->fTotalBounds);
769         fRenderPassXferBarriers |= toMerge->fRenderPassXferBarriers;
770         if (fInitialStencilContent == StencilContent::kDontCare) {
771             // Propogate the first stencil content that isn't kDontCare.
772             //
773             // Once the stencil has any kind of initial content that isn't kDontCare, then the
774             // inital contents of subsequent opsTasks that get merged in don't matter.
775             //
776             // (This works because the opsTask all target the same render target and are in
777             // painter's order. kPreserved obviously happens automatically with a merge, and kClear
778             // is also automatic because the contract is for ops to leave the stencil buffer in a
779             // cleared state when finished.)
780             fInitialStencilContent = toMerge->fInitialStencilContent;
781         }
782         fUsesMSAASurface |= toMerge->fUsesMSAASurface;
783         SkDEBUGCODE(fNumClips += toMerge->fNumClips);
784     }
785 
786     fLastClipStackGenID = SK_InvalidUniqueID;
787     fDeferredProxies.reserve_exact(fDeferredProxies.size() + addlDeferredProxyCount);
788     fSampledProxies.reserve_exact(fSampledProxies.size() + addlProxyCount);
789     fOpChains.reserve_exact(fOpChains.size() + addlOpChainCount);
790     for (const auto& toMerge : mergingNodes) {
791         for (GrRenderTask* renderTask : toMerge->dependents()) {
792             renderTask->replaceDependency(toMerge.get(), this);
793         }
794         for (GrRenderTask* renderTask : toMerge->dependencies()) {
795             renderTask->replaceDependent(toMerge.get(), this);
796         }
797         fDeferredProxies.move_back_n(toMerge->fDeferredProxies.size(),
798                                      toMerge->fDeferredProxies.data());
799         fSampledProxies.move_back_n(toMerge->fSampledProxies.size(),
800                                     toMerge->fSampledProxies.data());
801         fOpChains.move_back_n(toMerge->fOpChains.size(),
802                               toMerge->fOpChains.data());
803         toMerge->fDeferredProxies.clear();
804         toMerge->fSampledProxies.clear();
805         toMerge->fOpChains.clear();
806     }
807     fMustPreserveStencil = mergingNodes.back()->fMustPreserveStencil;
808     return mergedCount;
809 }
810 
resetForFullscreenClear(CanDiscardPreviousOps canDiscardPreviousOps)811 bool OpsTask::resetForFullscreenClear(CanDiscardPreviousOps canDiscardPreviousOps) {
812     if (CanDiscardPreviousOps::kYes == canDiscardPreviousOps || this->isEmpty()) {
813         this->deleteOps();
814         fDeferredProxies.clear();
815         fSampledProxies.clear();
816 
817         // If the opsTask is using a render target which wraps a vulkan command buffer, we can't do
818         // a clear load since we cannot change the render pass that we are using. Thus we fall back
819         // to making a clear op in this case.
820         return !this->target(0)->asRenderTargetProxy()->wrapsVkSecondaryCB();
821     }
822 
823     // Could not empty the task, so an op must be added to handle the clear
824     return false;
825 }
826 
discard()827 void OpsTask::discard() {
828     // Discard calls to in-progress opsTasks are ignored. Calls at the start update the
829     // opsTasks' color & stencil load ops.
830     if (this->isEmpty()) {
831         fColorLoadOp = GrLoadOp::kDiscard;
832         fInitialStencilContent = StencilContent::kDontCare;
833         fTotalBounds.setEmpty();
834     }
835 }
836 
837 ////////////////////////////////////////////////////////////////////////////////
838 
839 #if defined(GPU_TEST_UTILS)
dump(const SkString & label,SkString indent,bool printDependencies,bool close) const840 void OpsTask::dump(const SkString& label,
841                    SkString indent,
842                    bool printDependencies,
843                    bool close) const {
844     GrRenderTask::dump(label, indent, printDependencies, false);
845 
846     SkDebugf("%sfColorLoadOp: ", indent.c_str());
847     switch (fColorLoadOp) {
848         case GrLoadOp::kLoad:
849             SkDebugf("kLoad\n");
850             break;
851         case GrLoadOp::kClear:
852             SkDebugf("kClear {%g, %g, %g, %g}\n",
853                      fLoadClearColor[0],
854                      fLoadClearColor[1],
855                      fLoadClearColor[2],
856                      fLoadClearColor[3]);
857             break;
858         case GrLoadOp::kDiscard:
859             SkDebugf("kDiscard\n");
860             break;
861     }
862 
863     SkDebugf("%sfInitialStencilContent: ", indent.c_str());
864     switch (fInitialStencilContent) {
865         case StencilContent::kDontCare:
866             SkDebugf("kDontCare\n");
867             break;
868         case StencilContent::kUserBitsCleared:
869             SkDebugf("kUserBitsCleared\n");
870             break;
871         case StencilContent::kPreserved:
872             SkDebugf("kPreserved\n");
873             break;
874     }
875 
876     SkDebugf("%s%d ops:\n", indent.c_str(), fOpChains.size());
877     for (int i = 0; i < fOpChains.size(); ++i) {
878         SkDebugf("%s*******************************\n", indent.c_str());
879         if (!fOpChains[i].head()) {
880             SkDebugf("%s%d: <combined forward or failed instantiation>\n", indent.c_str(), i);
881         } else {
882             SkDebugf("%s%d: %s\n", indent.c_str(), i, fOpChains[i].head()->name());
883             SkRect bounds = fOpChains[i].bounds();
884             SkDebugf("%sClippedBounds: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n",
885                      indent.c_str(),
886                      bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom);
887             for (const auto& op : GrOp::ChainRange<>(fOpChains[i].head())) {
888                 SkString info = SkTabString(op.dumpInfo(), 1);
889                 SkDebugf("%s%s\n", indent.c_str(), info.c_str());
890                 bounds = op.bounds();
891                 SkDebugf("%s\tClippedBounds: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n",
892                          indent.c_str(),
893                          bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom);
894             }
895         }
896     }
897 
898     if (close) {
899         SkDebugf("%s--------------------------------------------------------------\n\n",
900                  indent.c_str());
901     }
902 }
903 #endif
904 
905 #ifdef SK_DEBUG
visitProxies_debugOnly(const GrVisitProxyFunc & func) const906 void OpsTask::visitProxies_debugOnly(const GrVisitProxyFunc& func) const {
907     auto textureFunc = [func](GrSurfaceProxy* tex, skgpu::Mipmapped mipmapped) {
908         func(tex, mipmapped);
909     };
910 
911     for (const OpChain& chain : fOpChains) {
912         chain.visitProxies(textureFunc);
913     }
914 }
915 
916 #endif
917 
918 ////////////////////////////////////////////////////////////////////////////////
919 
onMakeSkippable()920 void OpsTask::onMakeSkippable() {
921     this->deleteOps();
922     fDeferredProxies.clear();
923     fColorLoadOp = GrLoadOp::kLoad;
924     SkASSERT(this->isColorNoOp());
925 }
926 
onIsUsed(GrSurfaceProxy * proxyToCheck) const927 bool OpsTask::onIsUsed(GrSurfaceProxy* proxyToCheck) const {
928     bool used = false;
929     for (GrSurfaceProxy* proxy : fSampledProxies) {
930         if (proxy == proxyToCheck) {
931             used = true;
932             break;
933         }
934     }
935 #ifdef SK_DEBUG
936     bool usedSlow = false;
937     auto visit = [proxyToCheck, &usedSlow](GrSurfaceProxy* p, skgpu::Mipmapped) {
938         if (p == proxyToCheck) {
939             usedSlow = true;
940         }
941     };
942     this->visitProxies_debugOnly(visit);
943     SkASSERT(used == usedSlow);
944 #endif
945 
946     return used;
947 }
948 
gatherProxyIntervals(GrResourceAllocator * alloc) const949 void OpsTask::gatherProxyIntervals(GrResourceAllocator* alloc) const {
950     SkASSERT(this->isClosed());
951     if (this->isColorNoOp()) {
952         return;
953     }
954 
955     for (int i = 0; i < fDeferredProxies.size(); ++i) {
956         SkASSERT(!fDeferredProxies[i]->isInstantiated());
957         // We give all the deferred proxies a write usage at the very start of flushing. This
958         // locks them out of being reused for the entire flush until they are read - and then
959         // they can be recycled. This is a bit unfortunate because a flush can proceed in waves
960         // with sub-flushes. The deferred proxies only need to be pinned from the start of
961         // the sub-flush in which they appear.
962         alloc->addInterval(fDeferredProxies[i], 0, 0, GrResourceAllocator::ActualUse::kNo,
963                            GrResourceAllocator::AllowRecycling::kYes);
964     }
965 
966     GrSurfaceProxy* targetSurface = this->target(0);
967     SkASSERT(targetSurface);
968     GrRenderTargetProxy* targetProxy = targetSurface->asRenderTargetProxy();
969 
970     // Add the interval for all the writes to this OpsTasks's target
971     if (!fOpChains.empty()) {
972         unsigned int cur = alloc->curOp();
973 
974         alloc->addInterval(targetProxy, cur, cur + fOpChains.size() - 1,
975                            GrResourceAllocator::ActualUse::kYes,
976                            GrResourceAllocator::AllowRecycling::kYes);
977     } else {
978         // This can happen if there is a loadOp (e.g., a clear) but no other draws. In this case we
979         // still need to add an interval for the destination so we create a fake op# for
980         // the missing clear op.
981         alloc->addInterval(targetProxy, alloc->curOp(), alloc->curOp(),
982                            GrResourceAllocator::ActualUse::kYes,
983                            GrResourceAllocator::AllowRecycling::kYes);
984         alloc->incOps();
985     }
986 
987     GrResourceAllocator::AllowRecycling allowRecycling =
988             targetProxy->wrapsVkSecondaryCB() ? GrResourceAllocator::AllowRecycling::kNo
989                                               : GrResourceAllocator::AllowRecycling::kYes;
990 
991     auto gather = [alloc, allowRecycling SkDEBUGCODE(, this)](GrSurfaceProxy* p, skgpu::Mipmapped) {
992         alloc->addInterval(p,
993                            alloc->curOp(),
994                            alloc->curOp(),
995                            GrResourceAllocator::ActualUse::kYes,
996                            allowRecycling
997                            SkDEBUGCODE(, this->target(0) == p));
998     };
999     // TODO: visitProxies is expensive. Can we do this with fSampledProxies instead?
1000     for (const OpChain& recordedOp : fOpChains) {
1001         recordedOp.visitProxies(gather);
1002 
1003         // Even though the op may have been (re)moved we still need to increment the op count to
1004         // keep all the math consistent.
1005         alloc->incOps();
1006     }
1007 }
1008 
recordOp(GrOp::Owner op,bool usesMSAA,GrProcessorSet::Analysis processorAnalysis,GrAppliedClip * clip,const GrDstProxyView * dstProxyView,const GrCaps & caps)1009 void OpsTask::recordOp(
1010         GrOp::Owner op, bool usesMSAA, GrProcessorSet::Analysis processorAnalysis,
1011         GrAppliedClip* clip, const GrDstProxyView* dstProxyView, const GrCaps& caps) {
1012     GrSurfaceProxy* proxy = this->target(0);
1013 #ifdef SK_DEBUG
1014     op->validate();
1015     SkASSERT(processorAnalysis.requiresDstTexture() == (dstProxyView && dstProxyView->proxy()));
1016     SkASSERT(proxy);
1017     // A closed OpsTask should never receive new/more ops
1018     SkASSERT(!this->isClosed());
1019     // Ensure we can support dynamic msaa if the caller is trying to trigger it.
1020     if (proxy->asRenderTargetProxy()->numSamples() == 1 && usesMSAA) {
1021         SkASSERT(caps.supportsDynamicMSAA(proxy->asRenderTargetProxy()));
1022     }
1023 #endif
1024 
1025     if (!op->bounds().isFinite()) {
1026         return;
1027     }
1028 
1029     fUsesMSAASurface |= usesMSAA;
1030 
1031     // Account for this op's bounds before we attempt to combine.
1032     // NOTE: The caller should have already called "op->setClippedBounds()" by now, if applicable.
1033     fTotalBounds.join(op->bounds());
1034 
1035     // Check if there is an op we can combine with by linearly searching back until we either
1036     // 1) check every op
1037     // 2) intersect with something
1038     // 3) find a 'blocker'
1039     GR_AUDIT_TRAIL_ADD_OP(fAuditTrail, op.get(), proxy->uniqueID());
1040     GrOP_INFO("opsTask: %d Recording (%s, opID: %u)\n"
1041               "\tBounds [L: %.2f, T: %.2f R: %.2f B: %.2f]\n",
1042                this->uniqueID(),
1043                op->name(),
1044                op->uniqueID(),
1045                op->bounds().fLeft, op->bounds().fTop,
1046                op->bounds().fRight, op->bounds().fBottom);
1047     GrOP_INFO(SkTabString(op->dumpInfo(), 1).c_str());
1048     GrOP_INFO("\tOutcome:\n");
1049     int maxCandidates = std::min(kMaxOpChainDistance, fOpChains.size());
1050     if (maxCandidates) {
1051         int i = 0;
1052         while (true) {
1053             OpChain& candidate = fOpChains.fromBack(i);
1054             op = candidate.appendOp(std::move(op), processorAnalysis, dstProxyView, clip, caps,
1055                                     fArenas->arenaAlloc(), fAuditTrail);
1056             if (!op) {
1057                 return;
1058             }
1059             // Stop going backwards if we would cause a painter's order violation.
1060             if (!can_reorder(candidate.bounds(), op->bounds())) {
1061                 GrOP_INFO("\t\tBackward: Intersects with chain (%s, head opID: %u)\n",
1062                           candidate.head()->name(), candidate.head()->uniqueID());
1063                 break;
1064             }
1065             if (++i == maxCandidates) {
1066                 GrOP_INFO("\t\tBackward: Reached max lookback or beginning of op array %d\n", i);
1067                 break;
1068             }
1069         }
1070     } else {
1071         GrOP_INFO("\t\tBackward: FirstOp\n");
1072     }
1073     if (clip) {
1074         clip = fArenas->arenaAlloc()->make<GrAppliedClip>(std::move(*clip));
1075         SkDEBUGCODE(fNumClips++;)
1076     }
1077     fOpChains.emplace_back(std::move(op), processorAnalysis, clip, dstProxyView);
1078 }
1079 
forwardCombine(const GrCaps & caps)1080 void OpsTask::forwardCombine(const GrCaps& caps) {
1081     SkASSERT(!this->isClosed());
1082     GrOP_INFO("opsTask: %d ForwardCombine %d ops:\n", this->uniqueID(), fOpChains.size());
1083 
1084     for (int i = 0; i < fOpChains.size() - 1; ++i) {
1085         OpChain& chain = fOpChains[i];
1086         int maxCandidateIdx = std::min(i + kMaxOpChainDistance, fOpChains.size() - 1);
1087         int j = i + 1;
1088         while (true) {
1089             OpChain& candidate = fOpChains[j];
1090             if (candidate.prependChain(&chain, caps, fArenas->arenaAlloc(), fAuditTrail)) {
1091                 break;
1092             }
1093             // Stop traversing if we would cause a painter's order violation.
1094             if (!can_reorder(chain.bounds(), candidate.bounds())) {
1095                 GrOP_INFO(
1096                         "\t\t%d: chain (%s head opID: %u) -> "
1097                         "Intersects with chain (%s, head opID: %u)\n",
1098                         i, chain.head()->name(), chain.head()->uniqueID(), candidate.head()->name(),
1099                         candidate.head()->uniqueID());
1100                 break;
1101             }
1102             if (++j > maxCandidateIdx) {
1103                 GrOP_INFO("\t\t%d: chain (%s opID: %u) -> Reached max lookahead or end of array\n",
1104                           i, chain.head()->name(), chain.head()->uniqueID());
1105                 break;
1106             }
1107         }
1108     }
1109 }
1110 
onMakeClosed(GrRecordingContext * rContext,SkIRect * targetUpdateBounds)1111 GrRenderTask::ExpectedOutcome OpsTask::onMakeClosed(GrRecordingContext* rContext,
1112                                                     SkIRect* targetUpdateBounds) {
1113     this->forwardCombine(*rContext->priv().caps());
1114     if (!this->isColorNoOp()) {
1115         GrSurfaceProxy* proxy = this->target(0);
1116         // Use the entire backing store bounds since the GPU doesn't clip automatically to the
1117         // logical dimensions.
1118         SkRect clippedContentBounds = proxy->backingStoreBoundsRect();
1119         // TODO: If we can fix up GLPrograms test to always intersect the target proxy bounds
1120         // then we can simply assert here that the bounds intersect.
1121         if (clippedContentBounds.intersect(fTotalBounds)) {
1122             clippedContentBounds.roundOut(&fClippedContentBounds);
1123             *targetUpdateBounds = GrNativeRect::MakeIRectRelativeTo(
1124                     fTargetOrigin,
1125                     this->target(0)->backingStoreDimensions().height(),
1126                     fClippedContentBounds);
1127             return ExpectedOutcome::kTargetDirty;
1128         }
1129     }
1130     return ExpectedOutcome::kTargetUnchanged;
1131 }
1132 
1133 }  // namespace skgpu::ganesh
1134