• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2010 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 "GrRenderTargetOpList.h"
9 #include "GrAuditTrail.h"
10 #include "GrCaps.h"
11 #include "GrGpu.h"
12 #include "GrGpuCommandBuffer.h"
13 #include "GrMemoryPool.h"
14 #include "GrRecordingContext.h"
15 #include "GrRecordingContextPriv.h"
16 #include "GrRect.h"
17 #include "GrRenderTargetContext.h"
18 #include "GrResourceAllocator.h"
19 #include "SkExchange.h"
20 #include "SkRectPriv.h"
21 #include "SkTraceEvent.h"
22 #include "ops/GrClearOp.h"
23 #include "ops/GrCopySurfaceOp.h"
24 
25 ////////////////////////////////////////////////////////////////////////////////
26 
27 // Experimentally we have found that most combining occurs within the first 10 comparisons.
28 static const int kMaxOpMergeDistance = 10;
29 static const int kMaxOpChainDistance = 10;
30 
31 ////////////////////////////////////////////////////////////////////////////////
32 
33 using DstProxy = GrXferProcessor::DstProxy;
34 
35 ////////////////////////////////////////////////////////////////////////////////
36 
can_reorder(const SkRect & a,const SkRect & b)37 static inline bool can_reorder(const SkRect& a, const SkRect& b) { return !GrRectsOverlap(a, b); }
38 
39 ////////////////////////////////////////////////////////////////////////////////
40 
List(std::unique_ptr<GrOp> op)41 inline GrRenderTargetOpList::OpChain::List::List(std::unique_ptr<GrOp> op)
42         : fHead(std::move(op)), fTail(fHead.get()) {
43     this->validate();
44 }
45 
List(List && that)46 inline GrRenderTargetOpList::OpChain::List::List(List&& that) { *this = std::move(that); }
47 
operator =(List && that)48 inline GrRenderTargetOpList::OpChain::List& GrRenderTargetOpList::OpChain::List::operator=(
49         List&& that) {
50     fHead = std::move(that.fHead);
51     fTail = that.fTail;
52     that.fTail = nullptr;
53     this->validate();
54     return *this;
55 }
56 
popHead()57 inline std::unique_ptr<GrOp> GrRenderTargetOpList::OpChain::List::popHead() {
58     SkASSERT(fHead);
59     auto temp = fHead->cutChain();
60     std::swap(temp, fHead);
61     if (!fHead) {
62         SkASSERT(fTail == temp.get());
63         fTail = nullptr;
64     }
65     return temp;
66 }
67 
removeOp(GrOp * op)68 inline std::unique_ptr<GrOp> GrRenderTargetOpList::OpChain::List::removeOp(GrOp* op) {
69 #ifdef SK_DEBUG
70     auto head = op;
71     while (head->prevInChain()) { head = head->prevInChain(); }
72     SkASSERT(head == fHead.get());
73 #endif
74     auto prev = op->prevInChain();
75     if (!prev) {
76         SkASSERT(op == fHead.get());
77         return this->popHead();
78     }
79     auto temp = prev->cutChain();
80     if (auto next = temp->cutChain()) {
81         prev->chainConcat(std::move(next));
82     } else {
83         SkASSERT(fTail == op);
84         fTail = prev;
85     }
86     this->validate();
87     return temp;
88 }
89 
pushHead(std::unique_ptr<GrOp> op)90 inline void GrRenderTargetOpList::OpChain::List::pushHead(std::unique_ptr<GrOp> op) {
91     SkASSERT(op);
92     SkASSERT(op->isChainHead());
93     SkASSERT(op->isChainTail());
94     if (fHead) {
95         op->chainConcat(std::move(fHead));
96         fHead = std::move(op);
97     } else {
98         fHead = std::move(op);
99         fTail = fHead.get();
100     }
101 }
102 
pushTail(std::unique_ptr<GrOp> op)103 inline void GrRenderTargetOpList::OpChain::List::pushTail(std::unique_ptr<GrOp> op) {
104     SkASSERT(op->isChainTail());
105     fTail->chainConcat(std::move(op));
106     fTail = fTail->nextInChain();
107 }
108 
validate() const109 inline void GrRenderTargetOpList::OpChain::List::validate() const {
110 #ifdef SK_DEBUG
111     if (fHead) {
112         SkASSERT(fTail);
113         fHead->validateChain(fTail);
114     }
115 #endif
116 }
117 
118 ////////////////////////////////////////////////////////////////////////////////
119 
OpChain(std::unique_ptr<GrOp> op,GrProcessorSet::Analysis processorAnalysis,GrAppliedClip * appliedClip,const DstProxy * dstProxy)120 GrRenderTargetOpList::OpChain::OpChain(std::unique_ptr<GrOp> op,
121                                        GrProcessorSet::Analysis processorAnalysis,
122                                        GrAppliedClip* appliedClip, const DstProxy* dstProxy)
123         : fList{std::move(op)}
124         , fProcessorAnalysis(processorAnalysis)
125         , fAppliedClip(appliedClip) {
126     if (fProcessorAnalysis.requiresDstTexture()) {
127         SkASSERT(dstProxy && dstProxy->proxy());
128         fDstProxy = *dstProxy;
129     }
130     fBounds = fList.head()->bounds();
131 }
132 
visitProxies(const GrOp::VisitProxyFunc & func,GrOp::VisitorType visitor) const133 void GrRenderTargetOpList::OpChain::visitProxies(const GrOp::VisitProxyFunc& func,
134                                                  GrOp::VisitorType visitor) const {
135     if (fList.empty()) {
136         return;
137     }
138     for (const auto& op : GrOp::ChainRange<>(fList.head())) {
139         op.visitProxies(func, visitor);
140     }
141     if (fDstProxy.proxy()) {
142         func(fDstProxy.proxy());
143     }
144     if (fAppliedClip) {
145         fAppliedClip->visitProxies(func);
146     }
147 }
148 
deleteOps(GrOpMemoryPool * pool)149 void GrRenderTargetOpList::OpChain::deleteOps(GrOpMemoryPool* pool) {
150     while (!fList.empty()) {
151         pool->release(fList.popHead());
152     }
153 }
154 
155 // Concatenates two op chains and attempts to merge ops across the chains. Assumes that we know that
156 // the two chains are chainable. Returns the new chain.
DoConcat(List chainA,List chainB,const GrCaps & caps,GrOpMemoryPool * pool,GrAuditTrail * auditTrail)157 GrRenderTargetOpList::OpChain::List GrRenderTargetOpList::OpChain::DoConcat(
158         List chainA, List chainB, const GrCaps& caps, GrOpMemoryPool* pool,
159         GrAuditTrail* auditTrail) {
160     // We process ops in chain b from head to tail. We attempt to merge with nodes in a, starting
161     // at chain a's tail and working toward the head. We produce one of the following outcomes:
162     // 1) b's head is merged into an op in a.
163     // 2) An op from chain a is merged into b's head. (In this case b's head gets processed again.)
164     // 3) b's head is popped from chain a and added at the tail of a.
165     // After result 3 we don't want to attempt to merge the next head of b with the new tail of a,
166     // as we assume merges were already attempted when chain b was created. So we keep track of the
167     // original tail of a and start our iteration of a there. We also track the bounds of the nodes
168     // appended to chain a that will be skipped for bounds testing. If the original tail of a is
169     // merged into an op in b (case 2) then we advance the "original tail" towards the head of a.
170     GrOp* origATail = chainA.tail();
171     SkRect skipBounds = SkRectPriv::MakeLargestInverted();
172     do {
173         int numMergeChecks = 0;
174         bool merged = false;
175         bool noSkip = (origATail == chainA.tail());
176         SkASSERT(noSkip == (skipBounds == SkRectPriv::MakeLargestInverted()));
177         bool canBackwardMerge = noSkip || can_reorder(chainB.head()->bounds(), skipBounds);
178         SkRect forwardMergeBounds = skipBounds;
179         GrOp* a = origATail;
180         while (a) {
181             bool canForwardMerge =
182                     (a == chainA.tail()) || can_reorder(a->bounds(), forwardMergeBounds);
183             if (canForwardMerge || canBackwardMerge) {
184                 auto result = a->combineIfPossible(chainB.head(), caps);
185                 SkASSERT(result != GrOp::CombineResult::kCannotCombine);
186                 merged = (result == GrOp::CombineResult::kMerged);
187                 GrOP_INFO("\t\t: (%s opID: %u) -> Combining with (%s, opID: %u)\n",
188                           chainB.head()->name(), chainB.head()->uniqueID(), a->name(),
189                           a->uniqueID());
190             }
191             if (merged) {
192                 GR_AUDIT_TRAIL_OPS_RESULT_COMBINED(auditTrail, a, chainB.head());
193                 if (canBackwardMerge) {
194                     pool->release(chainB.popHead());
195                 } else {
196                     // We merged the contents of b's head into a. We will replace b's head with a in
197                     // chain b.
198                     SkASSERT(canForwardMerge);
199                     if (a == origATail) {
200                         origATail = a->prevInChain();
201                     }
202                     std::unique_ptr<GrOp> detachedA = chainA.removeOp(a);
203                     pool->release(chainB.popHead());
204                     chainB.pushHead(std::move(detachedA));
205                     if (chainA.empty()) {
206                         // We merged all the nodes in chain a to chain b.
207                         return chainB;
208                     }
209                 }
210                 break;
211             } else {
212                 if (++numMergeChecks == kMaxOpMergeDistance) {
213                     break;
214                 }
215                 forwardMergeBounds.joinNonEmptyArg(a->bounds());
216                 canBackwardMerge =
217                         canBackwardMerge && can_reorder(chainB.head()->bounds(), a->bounds());
218                 a = a->prevInChain();
219             }
220         }
221         // If we weren't able to merge b's head then pop b's head from chain b and make it the new
222         // tail of a.
223         if (!merged) {
224             chainA.pushTail(chainB.popHead());
225             skipBounds.joinNonEmptyArg(chainA.tail()->bounds());
226         }
227     } while (!chainB.empty());
228     return chainA;
229 }
230 
231 // Attempts to concatenate the given chain onto our own and merge ops across the chains. Returns
232 // whether the operation succeeded. On success, the provided list will be returned empty.
tryConcat(List * list,GrProcessorSet::Analysis processorAnalysis,const DstProxy & dstProxy,const GrAppliedClip * appliedClip,const SkRect & bounds,const GrCaps & caps,GrOpMemoryPool * pool,GrAuditTrail * auditTrail)233 bool GrRenderTargetOpList::OpChain::tryConcat(
234         List* list, GrProcessorSet::Analysis processorAnalysis, const DstProxy& dstProxy,
235         const GrAppliedClip* appliedClip, const SkRect& bounds, const GrCaps& caps,
236         GrOpMemoryPool* pool, GrAuditTrail* auditTrail) {
237     SkASSERT(!fList.empty());
238     SkASSERT(!list->empty());
239     SkASSERT(fProcessorAnalysis.requiresDstTexture() == SkToBool(fDstProxy.proxy()));
240     SkASSERT(processorAnalysis.requiresDstTexture() == SkToBool(dstProxy.proxy()));
241     // All returns use explicit tuple constructor rather than {a, b} to work around old GCC bug.
242     if (fList.head()->classID() != list->head()->classID() ||
243         SkToBool(fAppliedClip) != SkToBool(appliedClip) ||
244         (fAppliedClip && *fAppliedClip != *appliedClip) ||
245         (fProcessorAnalysis.requiresNonOverlappingDraws() !=
246                 processorAnalysis.requiresNonOverlappingDraws()) ||
247         (fProcessorAnalysis.requiresNonOverlappingDraws() &&
248                 // Non-overlaping draws are only required when Ganesh will either insert a barrier,
249                 // or read back a new dst texture between draws. In either case, we can neither
250                 // chain nor combine overlapping Ops.
251                 GrRectsTouchOrOverlap(fBounds, bounds)) ||
252         (fProcessorAnalysis.requiresDstTexture() != processorAnalysis.requiresDstTexture()) ||
253         (fProcessorAnalysis.requiresDstTexture() && fDstProxy != dstProxy)) {
254         return false;
255     }
256 
257     SkDEBUGCODE(bool first = true;)
258     do {
259         switch (fList.tail()->combineIfPossible(list->head(), caps)) {
260             case GrOp::CombineResult::kCannotCombine:
261                 // If an op supports chaining then it is required that chaining is transitive and
262                 // that if any two ops in two different chains can merge then the two chains
263                 // may also be chained together. Thus, we should only hit this on the first
264                 // iteration.
265                 SkASSERT(first);
266                 return false;
267             case GrOp::CombineResult::kMayChain:
268                 fList = DoConcat(std::move(fList), skstd::exchange(*list, List()), caps, pool,
269                                  auditTrail);
270                 // The above exchange cleared out 'list'. The list needs to be empty now for the
271                 // loop to terminate.
272                 SkASSERT(list->empty());
273                 break;
274             case GrOp::CombineResult::kMerged: {
275                 GrOP_INFO("\t\t: (%s opID: %u) -> Combining with (%s, opID: %u)\n",
276                           list->tail()->name(), list->tail()->uniqueID(), list->head()->name(),
277                           list->head()->uniqueID());
278                 GR_AUDIT_TRAIL_OPS_RESULT_COMBINED(auditTrail, fList.tail(), list->head());
279                 pool->release(list->popHead());
280                 break;
281             }
282         }
283         SkDEBUGCODE(first = false);
284     } while (!list->empty());
285 
286     // The new ops were successfully merged and/or chained onto our own.
287     fBounds.joinPossiblyEmptyRect(bounds);
288     return true;
289 }
290 
prependChain(OpChain * that,const GrCaps & caps,GrOpMemoryPool * pool,GrAuditTrail * auditTrail)291 bool GrRenderTargetOpList::OpChain::prependChain(OpChain* that, const GrCaps& caps,
292                                                  GrOpMemoryPool* pool, GrAuditTrail* auditTrail) {
293     if (!that->tryConcat(
294             &fList, fProcessorAnalysis, fDstProxy, fAppliedClip, fBounds, caps, pool, auditTrail)) {
295         this->validate();
296         // append failed
297         return false;
298     }
299 
300     // 'that' owns the combined chain. Move it into 'this'.
301     SkASSERT(fList.empty());
302     fList = std::move(that->fList);
303     fBounds = that->fBounds;
304 
305     that->fDstProxy.setProxy(nullptr);
306     if (that->fAppliedClip) {
307         for (int i = 0; i < that->fAppliedClip->numClipCoverageFragmentProcessors(); ++i) {
308             that->fAppliedClip->detachClipCoverageFragmentProcessor(i);
309         }
310     }
311     this->validate();
312     return true;
313 }
314 
appendOp(std::unique_ptr<GrOp> op,GrProcessorSet::Analysis processorAnalysis,const DstProxy * dstProxy,const GrAppliedClip * appliedClip,const GrCaps & caps,GrOpMemoryPool * pool,GrAuditTrail * auditTrail)315 std::unique_ptr<GrOp> GrRenderTargetOpList::OpChain::appendOp(
316         std::unique_ptr<GrOp> op, GrProcessorSet::Analysis processorAnalysis,
317         const DstProxy* dstProxy, const GrAppliedClip* appliedClip, const GrCaps& caps,
318         GrOpMemoryPool* pool, GrAuditTrail* auditTrail) {
319     const GrXferProcessor::DstProxy noDstProxy;
320     if (!dstProxy) {
321         dstProxy = &noDstProxy;
322     }
323     SkASSERT(op->isChainHead() && op->isChainTail());
324     SkRect opBounds = op->bounds();
325     List chain(std::move(op));
326     if (!this->tryConcat(
327             &chain, processorAnalysis, *dstProxy, appliedClip, opBounds, caps, pool, auditTrail)) {
328         // append failed, give the op back to the caller.
329         this->validate();
330         return chain.popHead();
331     }
332 
333     SkASSERT(chain.empty());
334     this->validate();
335     return nullptr;
336 }
337 
validate() const338 inline void GrRenderTargetOpList::OpChain::validate() const {
339 #ifdef SK_DEBUG
340     fList.validate();
341     for (const auto& op : GrOp::ChainRange<>(fList.head())) {
342         // Not using SkRect::contains because we allow empty rects.
343         SkASSERT(fBounds.fLeft <= op.bounds().fLeft && fBounds.fTop <= op.bounds().fTop &&
344                  fBounds.fRight >= op.bounds().fRight && fBounds.fBottom >= op.bounds().fBottom);
345     }
346 #endif
347 }
348 
349 ////////////////////////////////////////////////////////////////////////////////
350 
GrRenderTargetOpList(GrResourceProvider * resourceProvider,sk_sp<GrOpMemoryPool> opMemoryPool,GrRenderTargetProxy * proxy,GrAuditTrail * auditTrail)351 GrRenderTargetOpList::GrRenderTargetOpList(GrResourceProvider* resourceProvider,
352                                            sk_sp<GrOpMemoryPool> opMemoryPool,
353                                            GrRenderTargetProxy* proxy,
354                                            GrAuditTrail* auditTrail)
355         : INHERITED(resourceProvider, std::move(opMemoryPool), proxy, auditTrail)
356         , fLastClipStackGenID(SK_InvalidUniqueID)
357         SkDEBUGCODE(, fNumClips(0)) {
358 }
359 
deleteOps()360 void GrRenderTargetOpList::deleteOps() {
361     for (auto& chain : fOpChains) {
362         chain.deleteOps(fOpMemoryPool.get());
363     }
364     fOpChains.reset();
365 }
366 
~GrRenderTargetOpList()367 GrRenderTargetOpList::~GrRenderTargetOpList() {
368     this->deleteOps();
369 }
370 
371 ////////////////////////////////////////////////////////////////////////////////
372 
373 #ifdef SK_DEBUG
dump(bool printDependencies) const374 void GrRenderTargetOpList::dump(bool printDependencies) const {
375     INHERITED::dump(printDependencies);
376 
377     SkDebugf("ops (%d):\n", fOpChains.count());
378     for (int i = 0; i < fOpChains.count(); ++i) {
379         SkDebugf("*******************************\n");
380         if (!fOpChains[i].head()) {
381             SkDebugf("%d: <combined forward or failed instantiation>\n", i);
382         } else {
383             SkDebugf("%d: %s\n", i, fOpChains[i].head()->name());
384             SkRect bounds = fOpChains[i].bounds();
385             SkDebugf("ClippedBounds: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", bounds.fLeft,
386                      bounds.fTop, bounds.fRight, bounds.fBottom);
387             for (const auto& op : GrOp::ChainRange<>(fOpChains[i].head())) {
388                 SkString info = SkTabString(op.dumpInfo(), 1);
389                 SkDebugf("%s\n", info.c_str());
390                 bounds = op.bounds();
391                 SkDebugf("\tClippedBounds: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", bounds.fLeft,
392                          bounds.fTop, bounds.fRight, bounds.fBottom);
393             }
394         }
395     }
396 }
397 
visitProxies_debugOnly(const GrOp::VisitProxyFunc & func) const398 void GrRenderTargetOpList::visitProxies_debugOnly(const GrOp::VisitProxyFunc& func) const {
399     for (const OpChain& chain : fOpChains) {
400         chain.visitProxies(func, GrOp::VisitorType::kOther);
401     }
402 }
403 
404 #endif
405 
onPrepare(GrOpFlushState * flushState)406 void GrRenderTargetOpList::onPrepare(GrOpFlushState* flushState) {
407     SkASSERT(fTarget.get()->peekRenderTarget());
408     SkASSERT(this->isClosed());
409 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
410     TRACE_EVENT0("skia", TRACE_FUNC);
411 #endif
412 
413     // Loop over the ops that haven't yet been prepared.
414     for (const auto& chain : fOpChains) {
415         if (chain.head()) {
416 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
417             TRACE_EVENT0("skia", chain.head()->name());
418 #endif
419             GrOpFlushState::OpArgs opArgs = {
420                 chain.head(),
421                 fTarget.get()->asRenderTargetProxy(),
422                 chain.appliedClip(),
423                 chain.dstProxy()
424             };
425             flushState->setOpArgs(&opArgs);
426             chain.head()->prepare(flushState);
427             flushState->setOpArgs(nullptr);
428         }
429     }
430 }
431 
create_command_buffer(GrGpu * gpu,GrRenderTarget * rt,GrSurfaceOrigin origin,const SkRect & bounds,GrLoadOp colorLoadOp,const SkPMColor4f & loadClearColor,GrLoadOp stencilLoadOp)432 static GrGpuRTCommandBuffer* create_command_buffer(GrGpu* gpu,
433                                                    GrRenderTarget* rt,
434                                                    GrSurfaceOrigin origin,
435                                                    const SkRect& bounds,
436                                                    GrLoadOp colorLoadOp,
437                                                    const SkPMColor4f& loadClearColor,
438                                                    GrLoadOp stencilLoadOp) {
439     const GrGpuRTCommandBuffer::LoadAndStoreInfo kColorLoadStoreInfo {
440         colorLoadOp,
441         GrStoreOp::kStore,
442         loadClearColor
443     };
444 
445     // TODO:
446     // We would like to (at this level) only ever clear & discard. We would need
447     // to stop splitting up higher level opLists for copyOps to achieve that.
448     // Note: we would still need SB loads and stores but they would happen at a
449     // lower level (inside the VK command buffer).
450     const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo stencilLoadAndStoreInfo {
451         stencilLoadOp,
452         GrStoreOp::kStore,
453     };
454 
455     return gpu->getCommandBuffer(rt, origin, bounds, kColorLoadStoreInfo, stencilLoadAndStoreInfo);
456 }
457 
458 // TODO: this is where GrOp::renderTarget is used (which is fine since it
459 // is at flush time). However, we need to store the RenderTargetProxy in the
460 // Ops and instantiate them here.
onExecute(GrOpFlushState * flushState)461 bool GrRenderTargetOpList::onExecute(GrOpFlushState* flushState) {
462     // TODO: Forcing the execution of the discard here isn't ideal since it will cause us to do a
463     // discard and then store the data back in memory so that the load op on future draws doesn't
464     // think the memory is unitialized. Ideally we would want a system where we are tracking whether
465     // the proxy itself has valid data or not, and then use that as a signal on whether we should be
466     // loading or discarding. In that world we wouldni;t need to worry about executing oplists with
467     // no ops just to do a discard.
468     if (fOpChains.empty() && GrLoadOp::kClear != fColorLoadOp &&
469         GrLoadOp::kDiscard != fColorLoadOp) {
470         return false;
471     }
472 
473     SkASSERT(fTarget.get()->peekRenderTarget());
474     TRACE_EVENT0("skia", TRACE_FUNC);
475 
476     // TODO: at the very least, we want the stencil store op to always be discard (at this
477     // level). In Vulkan, sub-command buffers would still need to load & store the stencil buffer.
478 
479     // Make sure load ops are not kClear if the GPU needs to use draws for clears
480     SkASSERT(fColorLoadOp != GrLoadOp::kClear ||
481              !flushState->gpu()->caps()->performColorClearsAsDraws());
482     SkASSERT(fStencilLoadOp != GrLoadOp::kClear ||
483              !flushState->gpu()->caps()->performStencilClearsAsDraws());
484     GrGpuRTCommandBuffer* commandBuffer = create_command_buffer(
485                                                     flushState->gpu(),
486                                                     fTarget.get()->peekRenderTarget(),
487                                                     fTarget.get()->origin(),
488                                                     fTarget.get()->getBoundsRect(),
489                                                     fColorLoadOp,
490                                                     fLoadClearColor,
491                                                     fStencilLoadOp);
492     flushState->setCommandBuffer(commandBuffer);
493     commandBuffer->begin();
494 
495     // Draw all the generated geometry.
496     for (const auto& chain : fOpChains) {
497         if (!chain.head()) {
498             continue;
499         }
500 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
501         TRACE_EVENT0("skia", chain.head()->name());
502 #endif
503 
504         GrOpFlushState::OpArgs opArgs {
505             chain.head(),
506             fTarget.get()->asRenderTargetProxy(),
507             chain.appliedClip(),
508             chain.dstProxy()
509         };
510 
511         flushState->setOpArgs(&opArgs);
512         chain.head()->execute(flushState, chain.bounds());
513         flushState->setOpArgs(nullptr);
514     }
515 
516     commandBuffer->end();
517     flushState->gpu()->submit(commandBuffer);
518     flushState->setCommandBuffer(nullptr);
519 
520     return true;
521 }
522 
endFlush()523 void GrRenderTargetOpList::endFlush() {
524     fLastClipStackGenID = SK_InvalidUniqueID;
525     this->deleteOps();
526     fClipAllocator.reset();
527     INHERITED::endFlush();
528 }
529 
discard()530 void GrRenderTargetOpList::discard() {
531     // Discard calls to in-progress opLists are ignored. Calls at the start update the
532     // opLists' color & stencil load ops.
533     if (this->isEmpty()) {
534         fColorLoadOp = GrLoadOp::kDiscard;
535         fStencilLoadOp = GrLoadOp::kDiscard;
536     }
537 }
538 
setStencilLoadOp(GrLoadOp op)539 void GrRenderTargetOpList::setStencilLoadOp(GrLoadOp op) {
540     fStencilLoadOp = op;
541 }
542 
setColorLoadOp(GrLoadOp op,const SkPMColor4f & color)543 void GrRenderTargetOpList::setColorLoadOp(GrLoadOp op, const SkPMColor4f& color) {
544     fColorLoadOp = op;
545     fLoadClearColor = color;
546 }
547 
resetForFullscreenClear()548 bool GrRenderTargetOpList::resetForFullscreenClear() {
549     // Mark the color load op as discard (this may be followed by a clearColorOnLoad call to make
550     // the load op kClear, or it may be followed by an explicit op). In the event of an absClear()
551     // after a regular clear(), we could end up with a clear load op and a real clear op in the list
552     // if the load op were not reset here.
553     fColorLoadOp = GrLoadOp::kDiscard;
554 
555     // Regardless of how the clear is implemented (native clear or a fullscreen quad), all prior ops
556     // would be overwritten, so discard them entirely. The one exception is if the opList is marked
557     // as needing a stencil buffer then there may be a prior op that writes to the stencil buffer.
558     // Although the clear will ignore the stencil buffer, following draw ops may not so we can't get
559     // rid of all the preceding ops. Beware! If we ever add any ops that have a side effect beyond
560     // modifying the stencil buffer we will need a more elaborate tracking system (skbug.com/7002).
561     // Additionally, if we previously recorded a wait op, we cannot delete the wait op. Until we
562     // track the wait ops separately from normal ops, we have to avoid clearing out any ops.
563     if (this->isEmpty() || (!fTarget.get()->asRenderTargetProxy()->needsStencil() && !fHasWaitOp)) {
564         this->deleteOps();
565         fDeferredProxies.reset();
566 
567         // If the opList is using a render target which wraps a vulkan command buffer, we can't do a
568         // clear load since we cannot change the render pass that we are using. Thus we fall back to
569         // making a clear op in this case.
570         return !fTarget.get()->asRenderTargetProxy()->wrapsVkSecondaryCB();
571     }
572 
573     // Could not empty the list, so an op must be added to handle the clear
574     return false;
575 }
576 
577 ////////////////////////////////////////////////////////////////////////////////
578 
579 // This closely parallels GrTextureOpList::copySurface but renderTargetOpLists
580 // also store the applied clip and dest proxy with the op
copySurface(GrRecordingContext * context,GrSurfaceProxy * dst,GrSurfaceProxy * src,const SkIRect & srcRect,const SkIPoint & dstPoint)581 bool GrRenderTargetOpList::copySurface(GrRecordingContext* context,
582                                        GrSurfaceProxy* dst,
583                                        GrSurfaceProxy* src,
584                                        const SkIRect& srcRect,
585                                        const SkIPoint& dstPoint) {
586     SkASSERT(dst->asRenderTargetProxy() == fTarget.get());
587     std::unique_ptr<GrOp> op = GrCopySurfaceOp::Make(context, dst, src, srcRect, dstPoint);
588     if (!op) {
589         return false;
590     }
591 
592     this->addOp(std::move(op), *context->priv().caps());
593     return true;
594 }
595 
purgeOpsWithUninstantiatedProxies()596 void GrRenderTargetOpList::purgeOpsWithUninstantiatedProxies() {
597     bool hasUninstantiatedProxy = false;
598     auto checkInstantiation = [&hasUninstantiatedProxy](GrSurfaceProxy* p) {
599         if (!p->isInstantiated()) {
600             hasUninstantiatedProxy = true;
601         }
602     };
603     for (OpChain& recordedOp : fOpChains) {
604         hasUninstantiatedProxy = false;
605         recordedOp.visitProxies(checkInstantiation, GrOp::VisitorType::kOther);
606         if (hasUninstantiatedProxy) {
607             // When instantiation of the proxy fails we drop the Op
608             recordedOp.deleteOps(fOpMemoryPool.get());
609         }
610     }
611 }
612 
gatherProxyIntervals(GrResourceAllocator * alloc) const613 void GrRenderTargetOpList::gatherProxyIntervals(GrResourceAllocator* alloc) const {
614     unsigned int cur = alloc->numOps();
615 
616     for (int i = 0; i < fDeferredProxies.count(); ++i) {
617         SkASSERT(!fDeferredProxies[i]->isInstantiated());
618         // We give all the deferred proxies a write usage at the very start of flushing. This
619         // locks them out of being reused for the entire flush until they are read - and then
620         // they can be recycled. This is a bit unfortunate because a flush can proceed in waves
621         // with sub-flushes. The deferred proxies only need to be pinned from the start of
622         // the sub-flush in which they appear.
623         alloc->addInterval(fDeferredProxies[i], 0, 0);
624     }
625 
626     // Add the interval for all the writes to this opList's target
627     if (fOpChains.count()) {
628         alloc->addInterval(fTarget.get(), cur, cur + fOpChains.count() - 1);
629     } else {
630         // This can happen if there is a loadOp (e.g., a clear) but no other draws. In this case we
631         // still need to add an interval for the destination so we create a fake op# for
632         // the missing clear op.
633         alloc->addInterval(fTarget.get());
634         alloc->incOps();
635     }
636 
637     auto gather = [ alloc SkDEBUGCODE(, this) ] (GrSurfaceProxy* p) {
638         alloc->addInterval(p SkDEBUGCODE(, fTarget.get() == p));
639     };
640     for (const OpChain& recordedOp : fOpChains) {
641         // only diff from the GrTextureOpList version
642         recordedOp.visitProxies(gather, GrOp::VisitorType::kAllocatorGather);
643 
644         // Even though the op may have been moved we still need to increment the op count to
645         // keep all the math consistent.
646         alloc->incOps();
647     }
648 }
649 
recordOp(std::unique_ptr<GrOp> op,GrProcessorSet::Analysis processorAnalysis,GrAppliedClip * clip,const DstProxy * dstProxy,const GrCaps & caps)650 void GrRenderTargetOpList::recordOp(
651         std::unique_ptr<GrOp> op, GrProcessorSet::Analysis processorAnalysis, GrAppliedClip* clip,
652         const DstProxy* dstProxy, const GrCaps& caps) {
653     SkDEBUGCODE(op->validate();)
654     SkASSERT(processorAnalysis.requiresDstTexture() == (dstProxy && dstProxy->proxy()));
655     SkASSERT(fTarget.get());
656 
657     // A closed GrOpList should never receive new/more ops
658     SkASSERT(!this->isClosed());
659     if (!op->bounds().isFinite()) {
660         fOpMemoryPool->release(std::move(op));
661         return;
662     }
663 
664     // Check if there is an op we can combine with by linearly searching back until we either
665     // 1) check every op
666     // 2) intersect with something
667     // 3) find a 'blocker'
668     GR_AUDIT_TRAIL_ADD_OP(fAuditTrail, op.get(), fTarget.get()->uniqueID());
669     GrOP_INFO("opList: %d Recording (%s, opID: %u)\n"
670               "\tBounds [L: %.2f, T: %.2f R: %.2f B: %.2f]\n",
671                this->uniqueID(),
672                op->name(),
673                op->uniqueID(),
674                op->bounds().fLeft, op->bounds().fTop,
675                op->bounds().fRight, op->bounds().fBottom);
676     GrOP_INFO(SkTabString(op->dumpInfo(), 1).c_str());
677     GrOP_INFO("\tOutcome:\n");
678     int maxCandidates = SkTMin(kMaxOpChainDistance, fOpChains.count());
679     if (maxCandidates) {
680         int i = 0;
681         while (true) {
682             OpChain& candidate = fOpChains.fromBack(i);
683             op = candidate.appendOp(std::move(op), processorAnalysis, dstProxy, clip, caps,
684                                     fOpMemoryPool.get(), fAuditTrail);
685             if (!op) {
686                 return;
687             }
688             // Stop going backwards if we would cause a painter's order violation.
689             if (!can_reorder(candidate.bounds(), op->bounds())) {
690                 GrOP_INFO("\t\tBackward: Intersects with chain (%s, head opID: %u)\n",
691                           candidate.head()->name(), candidate.head()->uniqueID());
692                 break;
693             }
694             if (++i == maxCandidates) {
695                 GrOP_INFO("\t\tBackward: Reached max lookback or beginning of op array %d\n", i);
696                 break;
697             }
698         }
699     } else {
700         GrOP_INFO("\t\tBackward: FirstOp\n");
701     }
702     if (clip) {
703         clip = fClipAllocator.make<GrAppliedClip>(std::move(*clip));
704         SkDEBUGCODE(fNumClips++;)
705     }
706     fOpChains.emplace_back(std::move(op), processorAnalysis, clip, dstProxy);
707 }
708 
forwardCombine(const GrCaps & caps)709 void GrRenderTargetOpList::forwardCombine(const GrCaps& caps) {
710     SkASSERT(!this->isClosed());
711     GrOP_INFO("opList: %d ForwardCombine %d ops:\n", this->uniqueID(), fOpChains.count());
712 
713     for (int i = 0; i < fOpChains.count() - 1; ++i) {
714         OpChain& chain = fOpChains[i];
715         int maxCandidateIdx = SkTMin(i + kMaxOpChainDistance, fOpChains.count() - 1);
716         int j = i + 1;
717         while (true) {
718             OpChain& candidate = fOpChains[j];
719             if (candidate.prependChain(&chain, caps, fOpMemoryPool.get(), fAuditTrail)) {
720                 break;
721             }
722             // Stop traversing if we would cause a painter's order violation.
723             if (!can_reorder(chain.bounds(), candidate.bounds())) {
724                 GrOP_INFO(
725                         "\t\t%d: chain (%s head opID: %u) -> "
726                         "Intersects with chain (%s, head opID: %u)\n",
727                         i, chain.head()->name(), chain.head()->uniqueID(), candidate.head()->name(),
728                         candidate.head()->uniqueID());
729                 break;
730             }
731             if (++j > maxCandidateIdx) {
732                 GrOP_INFO("\t\t%d: chain (%s opID: %u) -> Reached max lookahead or end of array\n",
733                           i, chain.head()->name(), chain.head()->uniqueID());
734                 break;
735             }
736         }
737     }
738 }
739 
740