blob: ff66b93edfc2788cd9d7687771e3e3791aa08692 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
Greg Danielf41b2bd2019-08-22 16:19:24 -04002 * Copyright 2019 Google Inc.
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
Greg Danielf41b2bd2019-08-22 16:19:24 -04008#include "src/gpu/GrOpsTask.h"
Brian Salomon4d2d6f42019-07-26 14:15:11 -04009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/private/GrRecordingContext.h"
11#include "src/core/SkExchange.h"
12#include "src/core/SkRectPriv.h"
13#include "src/core/SkTraceEvent.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040014#include "src/gpu/GrAuditTrail.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/GrCaps.h"
16#include "src/gpu/GrGpu.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/gpu/GrMemoryPool.h"
Greg Daniele227fe42019-08-21 13:52:24 -040018#include "src/gpu/GrOpFlushState.h"
Greg Daniel2d41d0d2019-08-26 11:08:51 -040019#include "src/gpu/GrOpsRenderPass.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/GrRecordingContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/GrRenderTargetContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/gpu/GrResourceAllocator.h"
Chris Dalton95d8ceb2019-07-30 11:17:59 -060023#include "src/gpu/GrTexturePriv.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040024#include "src/gpu/geometry/GrRect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/ops/GrClearOp.h"
Robert Phillipsf2361d22016-10-25 14:20:06 -040026
reed@google.comac10a2d2010-12-22 21:39:39 +000027////////////////////////////////////////////////////////////////////////////////
28
Brian Salomon09d994e2016-12-21 11:14:46 -050029// Experimentally we have found that most combining occurs within the first 10 comparisons.
Brian Salomon588cec72018-11-14 13:56:37 -050030static const int kMaxOpMergeDistance = 10;
31static const int kMaxOpChainDistance = 10;
32
33////////////////////////////////////////////////////////////////////////////////
34
35using DstProxy = GrXferProcessor::DstProxy;
36
37////////////////////////////////////////////////////////////////////////////////
38
39static inline bool can_reorder(const SkRect& a, const SkRect& b) { return !GrRectsOverlap(a, b); }
40
41////////////////////////////////////////////////////////////////////////////////
42
Greg Danielf41b2bd2019-08-22 16:19:24 -040043inline GrOpsTask::OpChain::List::List(std::unique_ptr<GrOp> op)
Brian Salomon588cec72018-11-14 13:56:37 -050044 : fHead(std::move(op)), fTail(fHead.get()) {
45 this->validate();
46}
47
Greg Danielf41b2bd2019-08-22 16:19:24 -040048inline GrOpsTask::OpChain::List::List(List&& that) { *this = std::move(that); }
Brian Salomon588cec72018-11-14 13:56:37 -050049
Greg Danielf41b2bd2019-08-22 16:19:24 -040050inline GrOpsTask::OpChain::List& GrOpsTask::OpChain::List::operator=(List&& that) {
Brian Salomon588cec72018-11-14 13:56:37 -050051 fHead = std::move(that.fHead);
52 fTail = that.fTail;
53 that.fTail = nullptr;
54 this->validate();
55 return *this;
56}
57
Greg Danielf41b2bd2019-08-22 16:19:24 -040058inline std::unique_ptr<GrOp> GrOpsTask::OpChain::List::popHead() {
Brian Salomon588cec72018-11-14 13:56:37 -050059 SkASSERT(fHead);
60 auto temp = fHead->cutChain();
61 std::swap(temp, fHead);
62 if (!fHead) {
63 SkASSERT(fTail == temp.get());
64 fTail = nullptr;
65 }
66 return temp;
67}
68
Greg Danielf41b2bd2019-08-22 16:19:24 -040069inline std::unique_ptr<GrOp> GrOpsTask::OpChain::List::removeOp(GrOp* op) {
Brian Salomon588cec72018-11-14 13:56:37 -050070#ifdef SK_DEBUG
71 auto head = op;
72 while (head->prevInChain()) { head = head->prevInChain(); }
73 SkASSERT(head == fHead.get());
74#endif
75 auto prev = op->prevInChain();
76 if (!prev) {
77 SkASSERT(op == fHead.get());
78 return this->popHead();
79 }
80 auto temp = prev->cutChain();
81 if (auto next = temp->cutChain()) {
82 prev->chainConcat(std::move(next));
83 } else {
84 SkASSERT(fTail == op);
85 fTail = prev;
86 }
87 this->validate();
88 return temp;
89}
90
Greg Danielf41b2bd2019-08-22 16:19:24 -040091inline void GrOpsTask::OpChain::List::pushHead(std::unique_ptr<GrOp> op) {
Brian Salomon588cec72018-11-14 13:56:37 -050092 SkASSERT(op);
93 SkASSERT(op->isChainHead());
94 SkASSERT(op->isChainTail());
95 if (fHead) {
96 op->chainConcat(std::move(fHead));
97 fHead = std::move(op);
98 } else {
99 fHead = std::move(op);
100 fTail = fHead.get();
101 }
102}
103
Greg Danielf41b2bd2019-08-22 16:19:24 -0400104inline void GrOpsTask::OpChain::List::pushTail(std::unique_ptr<GrOp> op) {
Brian Salomon588cec72018-11-14 13:56:37 -0500105 SkASSERT(op->isChainTail());
106 fTail->chainConcat(std::move(op));
107 fTail = fTail->nextInChain();
108}
109
Greg Danielf41b2bd2019-08-22 16:19:24 -0400110inline void GrOpsTask::OpChain::List::validate() const {
Brian Salomon588cec72018-11-14 13:56:37 -0500111#ifdef SK_DEBUG
112 if (fHead) {
113 SkASSERT(fTail);
114 fHead->validateChain(fTail);
115 }
116#endif
117}
118
119////////////////////////////////////////////////////////////////////////////////
120
Greg Danielf41b2bd2019-08-22 16:19:24 -0400121GrOpsTask::OpChain::OpChain(std::unique_ptr<GrOp> op,
122 GrProcessorSet::Analysis processorAnalysis,
123 GrAppliedClip* appliedClip, const DstProxy* dstProxy)
Chris Dalton945ee652019-01-23 09:10:36 -0700124 : fList{std::move(op)}
125 , fProcessorAnalysis(processorAnalysis)
126 , fAppliedClip(appliedClip) {
127 if (fProcessorAnalysis.requiresDstTexture()) {
128 SkASSERT(dstProxy && dstProxy->proxy());
Brian Salomon588cec72018-11-14 13:56:37 -0500129 fDstProxy = *dstProxy;
130 }
131 fBounds = fList.head()->bounds();
132}
133
Greg Danielf41b2bd2019-08-22 16:19:24 -0400134void GrOpsTask::OpChain::visitProxies(const GrOp::VisitProxyFunc& func) const {
Brian Salomon588cec72018-11-14 13:56:37 -0500135 if (fList.empty()) {
136 return;
137 }
138 for (const auto& op : GrOp::ChainRange<>(fList.head())) {
Chris Dalton1706cbf2019-05-21 19:35:29 -0600139 op.visitProxies(func);
Brian Salomon588cec72018-11-14 13:56:37 -0500140 }
141 if (fDstProxy.proxy()) {
Chris Dalton7eb5c0f2019-05-23 15:15:47 -0600142 func(fDstProxy.proxy(), GrMipMapped::kNo);
Brian Salomon588cec72018-11-14 13:56:37 -0500143 }
144 if (fAppliedClip) {
145 fAppliedClip->visitProxies(func);
146 }
147}
148
Greg Danielf41b2bd2019-08-22 16:19:24 -0400149void GrOpsTask::OpChain::deleteOps(GrOpMemoryPool* pool) {
Brian Salomon588cec72018-11-14 13:56:37 -0500150 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.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400157GrOpsTask::OpChain::List GrOpsTask::OpChain::DoConcat(
Brian Salomon588cec72018-11-14 13:56:37 -0500158 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);
Robert Phillips9548c3b422019-01-08 12:35:43 -0500187 GrOP_INFO("\t\t: (%s opID: %u) -> Combining with (%s, opID: %u)\n",
Brian Salomon588cec72018-11-14 13:56:37 -0500188 chainB.head()->name(), chainB.head()->uniqueID(), a->name(),
189 a->uniqueID());
Brian Salomon588cec72018-11-14 13:56:37 -0500190 }
191 if (merged) {
Brian Salomon52a6ed32018-11-26 10:30:58 -0500192 GR_AUDIT_TRAIL_OPS_RESULT_COMBINED(auditTrail, a, chainB.head());
Brian Salomon588cec72018-11-14 13:56:37 -0500193 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
Chris Dalton945ee652019-01-23 09:10:36 -0700231// 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.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400233bool GrOpsTask::OpChain::tryConcat(
Chris Dalton945ee652019-01-23 09:10:36 -0700234 List* list, GrProcessorSet::Analysis processorAnalysis, const DstProxy& dstProxy,
235 const GrAppliedClip* appliedClip, const SkRect& bounds, const GrCaps& caps,
236 GrOpMemoryPool* pool, GrAuditTrail* auditTrail) {
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700237 SkASSERT(!fList.empty());
238 SkASSERT(!list->empty());
Chris Dalton945ee652019-01-23 09:10:36 -0700239 SkASSERT(fProcessorAnalysis.requiresDstTexture() == SkToBool(fDstProxy.proxy()));
240 SkASSERT(processorAnalysis.requiresDstTexture() == SkToBool(dstProxy.proxy()));
Brian Salomon588cec72018-11-14 13:56:37 -0500241 // All returns use explicit tuple constructor rather than {a, b} to work around old GCC bug.
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700242 if (fList.head()->classID() != list->head()->classID() ||
243 SkToBool(fAppliedClip) != SkToBool(appliedClip) ||
244 (fAppliedClip && *fAppliedClip != *appliedClip) ||
Chris Dalton945ee652019-01-23 09:10:36 -0700245 (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)) {
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700254 return false;
Brian Salomon588cec72018-11-14 13:56:37 -0500255 }
Chris Daltonee21e6b2019-01-22 14:04:43 -0700256
Brian Salomon588cec72018-11-14 13:56:37 -0500257 SkDEBUGCODE(bool first = true;)
258 do {
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700259 switch (fList.tail()->combineIfPossible(list->head(), caps)) {
Brian Salomon588cec72018-11-14 13:56:37 -0500260 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);
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700266 return false;
Brian Salomon588cec72018-11-14 13:56:37 -0500267 case GrOp::CombineResult::kMayChain:
Chris Daltonee21e6b2019-01-22 14:04:43 -0700268 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;
Brian Salomon588cec72018-11-14 13:56:37 -0500274 case GrOp::CombineResult::kMerged: {
Robert Phillips9548c3b422019-01-08 12:35:43 -0500275 GrOP_INFO("\t\t: (%s opID: %u) -> Combining with (%s, opID: %u)\n",
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700276 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());
Brian Salomon588cec72018-11-14 13:56:37 -0500280 break;
281 }
282 }
283 SkDEBUGCODE(first = false);
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700284 } while (!list->empty());
Chris Daltonee21e6b2019-01-22 14:04:43 -0700285
286 // The new ops were successfully merged and/or chained onto our own.
287 fBounds.joinPossiblyEmptyRect(bounds);
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700288 return true;
Brian Salomon588cec72018-11-14 13:56:37 -0500289}
290
Greg Danielf41b2bd2019-08-22 16:19:24 -0400291bool GrOpsTask::OpChain::prependChain(OpChain* that, const GrCaps& caps, GrOpMemoryPool* pool,
292 GrAuditTrail* auditTrail) {
Chris Dalton945ee652019-01-23 09:10:36 -0700293 if (!that->tryConcat(
294 &fList, fProcessorAnalysis, fDstProxy, fAppliedClip, fBounds, caps, pool, auditTrail)) {
Brian Salomon588cec72018-11-14 13:56:37 -0500295 this->validate();
296 // append failed
297 return false;
298 }
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700299
Brian Salomon588cec72018-11-14 13:56:37 -0500300 // 'that' owns the combined chain. Move it into 'this'.
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700301 SkASSERT(fList.empty());
Brian Salomon588cec72018-11-14 13:56:37 -0500302 fList = std::move(that->fList);
Chris Daltonee21e6b2019-01-22 14:04:43 -0700303 fBounds = that->fBounds;
Brian Salomon588cec72018-11-14 13:56:37 -0500304
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
Greg Danielf41b2bd2019-08-22 16:19:24 -0400315std::unique_ptr<GrOp> GrOpsTask::OpChain::appendOp(
Chris Dalton945ee652019-01-23 09:10:36 -0700316 std::unique_ptr<GrOp> op, GrProcessorSet::Analysis processorAnalysis,
317 const DstProxy* dstProxy, const GrAppliedClip* appliedClip, const GrCaps& caps,
318 GrOpMemoryPool* pool, GrAuditTrail* auditTrail) {
Brian Salomon588cec72018-11-14 13:56:37 -0500319 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));
Chris Dalton945ee652019-01-23 09:10:36 -0700326 if (!this->tryConcat(
327 &chain, processorAnalysis, *dstProxy, appliedClip, opBounds, caps, pool, auditTrail)) {
Brian Salomon588cec72018-11-14 13:56:37 -0500328 // append failed, give the op back to the caller.
329 this->validate();
330 return chain.popHead();
331 }
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700332
333 SkASSERT(chain.empty());
Brian Salomon588cec72018-11-14 13:56:37 -0500334 this->validate();
335 return nullptr;
336}
337
Greg Danielf41b2bd2019-08-22 16:19:24 -0400338inline void GrOpsTask::OpChain::validate() const {
Brian Salomon588cec72018-11-14 13:56:37 -0500339#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////////////////////////////////////////////////////////////////////////////////
bsalomon489147c2015-12-14 12:13:09 -0800350
Greg Danielf41b2bd2019-08-22 16:19:24 -0400351GrOpsTask::GrOpsTask(sk_sp<GrOpMemoryPool> opMemoryPool,
352 sk_sp<GrRenderTargetProxy> rtProxy,
353 GrAuditTrail* auditTrail)
354 : GrRenderTask(std::move(rtProxy))
355 , fOpMemoryPool(std::move(opMemoryPool))
356 , fAuditTrail(auditTrail)
Brian Salomonc3833b42018-07-09 18:23:58 +0000357 , fLastClipStackGenID(SK_InvalidUniqueID)
Robert Phillipsb6deea82017-05-11 14:14:30 -0400358 SkDEBUGCODE(, fNumClips(0)) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400359 SkASSERT(fOpMemoryPool);
Chris Dalton3d770272019-08-14 09:24:37 -0600360 fTarget->setLastRenderTask(this);
bsalomon4061b122015-05-29 10:26:19 -0700361}
362
Greg Danielf41b2bd2019-08-22 16:19:24 -0400363void GrOpsTask::deleteOps() {
Brian Salomon588cec72018-11-14 13:56:37 -0500364 for (auto& chain : fOpChains) {
365 chain.deleteOps(fOpMemoryPool.get());
Robert Phillipsc994a932018-06-19 13:09:54 -0400366 }
Brian Salomon588cec72018-11-14 13:56:37 -0500367 fOpChains.reset();
Robert Phillipsc994a932018-06-19 13:09:54 -0400368}
369
Greg Danielf41b2bd2019-08-22 16:19:24 -0400370GrOpsTask::~GrOpsTask() {
Robert Phillipsc994a932018-06-19 13:09:54 -0400371 this->deleteOps();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000372}
373
374////////////////////////////////////////////////////////////////////////////////
375
Greg Danielf41b2bd2019-08-22 16:19:24 -0400376void GrOpsTask::endFlush() {
377 fLastClipStackGenID = SK_InvalidUniqueID;
378 this->deleteOps();
379 fClipAllocator.reset();
Chris Dalton706a6ff2017-11-29 22:01:06 -0700380
Greg Danielf41b2bd2019-08-22 16:19:24 -0400381 if (fTarget && this == fTarget->getLastRenderTask()) {
382 fTarget->setLastRenderTask(nullptr);
Greg Danielf21bf9e2019-08-22 20:12:20 +0000383 }
Greg Danielf41b2bd2019-08-22 16:19:24 -0400384
385 fTarget.reset();
386 fDeferredProxies.reset();
Greg Danielb20d7e52019-09-03 13:54:39 -0400387 fSampledProxies.reset();
Greg Danielf41b2bd2019-08-22 16:19:24 -0400388 fAuditTrail = nullptr;
Greg Danielf21bf9e2019-08-22 20:12:20 +0000389}
390
Greg Danielf41b2bd2019-08-22 16:19:24 -0400391void GrOpsTask::onPrepare(GrOpFlushState* flushState) {
Robert Phillipsb5204762019-06-19 14:12:13 -0400392 SkASSERT(fTarget->peekRenderTarget());
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400393 SkASSERT(this->isClosed());
Stan Iliev2af578d2017-08-16 13:00:28 -0400394#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
Brian Salomon5f394272019-07-02 14:07:49 -0400395 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Stan Iliev2af578d2017-08-16 13:00:28 -0400396#endif
Greg Daniel94ed83f2019-09-27 13:05:43 -0400397 // 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 }
robertphillipsa106c622015-10-16 09:07:06 -0700404
Greg Danielb20d7e52019-09-03 13:54:39 -0400405 flushState->setSampledProxyArray(&fSampledProxies);
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500406 // Loop over the ops that haven't yet been prepared.
Brian Salomon588cec72018-11-14 13:56:37 -0500407 for (const auto& chain : fOpChains) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400408 if (chain.shouldExecute()) {
Stan Iliev2af578d2017-08-16 13:00:28 -0400409#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
Brian Salomon5f394272019-07-02 14:07:49 -0400410 TRACE_EVENT0("skia.gpu", chain.head()->name());
Stan Iliev2af578d2017-08-16 13:00:28 -0400411#endif
Brian Salomon29b60c92017-10-31 14:42:10 -0400412 GrOpFlushState::OpArgs opArgs = {
Brian Salomon588cec72018-11-14 13:56:37 -0500413 chain.head(),
Robert Phillipsb5204762019-06-19 14:12:13 -0400414 fTarget->asRenderTargetProxy(),
Brian Salomon588cec72018-11-14 13:56:37 -0500415 chain.appliedClip(),
Greg Daniel2c3398d2019-06-19 11:58:01 -0400416 fTarget.get()->asRenderTargetProxy()->outputSwizzle(),
Brian Salomon588cec72018-11-14 13:56:37 -0500417 chain.dstProxy()
Robert Phillips318c4192017-05-17 09:36:38 -0400418 };
Brian Salomon29b60c92017-10-31 14:42:10 -0400419 flushState->setOpArgs(&opArgs);
Brian Salomon588cec72018-11-14 13:56:37 -0500420 chain.head()->prepare(flushState);
Brian Salomon29b60c92017-10-31 14:42:10 -0400421 flushState->setOpArgs(nullptr);
bsalomonaecc0182016-03-07 11:50:44 -0800422 }
bsalomon512be532015-09-10 10:42:55 -0700423 }
Greg Danielb20d7e52019-09-03 13:54:39 -0400424 flushState->setSampledProxyArray(nullptr);
robertphillipsa13e2022015-11-11 12:01:09 -0800425}
bsalomon512be532015-09-10 10:42:55 -0700426
Greg Daniel4a0d36d2019-09-30 12:24:36 -0400427static GrOpsRenderPass* create_render_pass(
428 GrGpu* gpu, GrRenderTarget* rt, GrSurfaceOrigin origin, const SkIRect& bounds,
Greg Danielb20d7e52019-09-03 13:54:39 -0400429 GrLoadOp colorLoadOp, const SkPMColor4f& loadClearColor, GrLoadOp stencilLoadOp,
430 const SkTArray<GrTextureProxy*, true>& sampledProxies) {
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400431 const GrOpsRenderPass::LoadAndStoreInfo kColorLoadStoreInfo {
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400432 colorLoadOp,
433 GrStoreOp::kStore,
434 loadClearColor
Robert Phillips178ce3e2017-04-13 09:15:47 -0400435 };
436
Robert Phillips95214472017-08-08 18:00:03 -0400437 // TODO:
438 // We would like to (at this level) only ever clear & discard. We would need
Greg Danielf41b2bd2019-08-22 16:19:24 -0400439 // to stop splitting up higher level OpsTasks for copyOps to achieve that.
Robert Phillips95214472017-08-08 18:00:03 -0400440 // Note: we would still need SB loads and stores but they would happen at a
441 // lower level (inside the VK command buffer).
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400442 const GrOpsRenderPass::StencilLoadAndStoreInfo stencilLoadAndStoreInfo {
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400443 stencilLoadOp,
Michael Ludwig6e17f1d2019-05-15 14:00:20 +0000444 GrStoreOp::kStore,
Robert Phillips95214472017-08-08 18:00:03 -0400445 };
446
Greg Danielb20d7e52019-09-03 13:54:39 -0400447 return gpu->getOpsRenderPass(rt, origin, bounds, kColorLoadStoreInfo, stencilLoadAndStoreInfo,
448 sampledProxies);
Robert Phillips178ce3e2017-04-13 09:15:47 -0400449}
450
Brian Salomon25a88092016-12-01 09:36:50 -0500451// TODO: this is where GrOp::renderTarget is used (which is fine since it
Robert Phillips294870f2016-11-11 12:38:40 -0500452// is at flush time). However, we need to store the RenderTargetProxy in the
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500453// Ops and instantiate them here.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400454bool GrOpsTask::onExecute(GrOpFlushState* flushState) {
Greg Daniel94ed83f2019-09-27 13:05:43 -0400455 // TODO: remove the check for discard here once reduced op splitting is turned on. Currently we
456 // can end up with GrOpsTasks that only have a discard load op and no ops. For vulkan validation
457 // we need to keep that discard and not drop it. Once we have reduce op list splitting enabled
458 // we shouldn't end up with GrOpsTasks with only discard.
459 if (this->isNoOp() || (fClippedContentBounds.isEmpty() && fColorLoadOp != GrLoadOp::kDiscard)) {
bsalomondc438982016-08-31 11:53:49 -0700460 return false;
egdanielb4021cf2016-07-28 08:53:07 -0700461 }
Robert Phillips4a395042017-04-24 16:27:17 +0000462
Robert Phillipsb5204762019-06-19 14:12:13 -0400463 SkASSERT(fTarget->peekRenderTarget());
Brian Salomon5f394272019-07-02 14:07:49 -0400464 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400465
Michael Ludwig6e17f1d2019-05-15 14:00:20 +0000466 // TODO: at the very least, we want the stencil store op to always be discard (at this
467 // level). In Vulkan, sub-command buffers would still need to load & store the stencil buffer.
468
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500469 // Make sure load ops are not kClear if the GPU needs to use draws for clears
470 SkASSERT(fColorLoadOp != GrLoadOp::kClear ||
471 !flushState->gpu()->caps()->performColorClearsAsDraws());
472 SkASSERT(fStencilLoadOp != GrLoadOp::kClear ||
473 !flushState->gpu()->caps()->performStencilClearsAsDraws());
Greg Daniel4a0d36d2019-09-30 12:24:36 -0400474 GrOpsRenderPass* renderPass = create_render_pass(
Michael Ludwig6e17f1d2019-05-15 14:00:20 +0000475 flushState->gpu(),
Robert Phillipsb5204762019-06-19 14:12:13 -0400476 fTarget->peekRenderTarget(),
477 fTarget->origin(),
Greg Daniel4a0d36d2019-09-30 12:24:36 -0400478 fClippedContentBounds,
Michael Ludwig6e17f1d2019-05-15 14:00:20 +0000479 fColorLoadOp,
480 fLoadClearColor,
Greg Danielb20d7e52019-09-03 13:54:39 -0400481 fStencilLoadOp,
482 fSampledProxies);
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400483 flushState->setOpsRenderPass(renderPass);
484 renderPass->begin();
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400485
486 // Draw all the generated geometry.
Brian Salomon588cec72018-11-14 13:56:37 -0500487 for (const auto& chain : fOpChains) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400488 if (!chain.shouldExecute()) {
bsalomonaecc0182016-03-07 11:50:44 -0800489 continue;
490 }
Stan Iliev2af578d2017-08-16 13:00:28 -0400491#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
Brian Salomon5f394272019-07-02 14:07:49 -0400492 TRACE_EVENT0("skia.gpu", chain.head()->name());
Stan Iliev2af578d2017-08-16 13:00:28 -0400493#endif
Robert Phillips178ce3e2017-04-13 09:15:47 -0400494
Brian Salomon29b60c92017-10-31 14:42:10 -0400495 GrOpFlushState::OpArgs opArgs {
Brian Salomon588cec72018-11-14 13:56:37 -0500496 chain.head(),
Robert Phillipsb5204762019-06-19 14:12:13 -0400497 fTarget->asRenderTargetProxy(),
Brian Salomon588cec72018-11-14 13:56:37 -0500498 chain.appliedClip(),
Greg Daniel2c3398d2019-06-19 11:58:01 -0400499 fTarget.get()->asRenderTargetProxy()->outputSwizzle(),
Brian Salomon588cec72018-11-14 13:56:37 -0500500 chain.dstProxy()
Robert Phillips178ce3e2017-04-13 09:15:47 -0400501 };
502
Brian Salomon29b60c92017-10-31 14:42:10 -0400503 flushState->setOpArgs(&opArgs);
Brian Salomon588cec72018-11-14 13:56:37 -0500504 chain.head()->execute(flushState, chain.bounds());
Brian Salomon29b60c92017-10-31 14:42:10 -0400505 flushState->setOpArgs(nullptr);
bsalomon512be532015-09-10 10:42:55 -0700506 }
Robert Phillips178ce3e2017-04-13 09:15:47 -0400507
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400508 renderPass->end();
509 flushState->gpu()->submit(renderPass);
510 flushState->setOpsRenderPass(nullptr);
ethannicholas22793252016-01-30 09:59:10 -0800511
bsalomondc438982016-08-31 11:53:49 -0700512 return true;
bsalomona73239a2015-04-28 13:35:17 -0700513}
514
Greg Danielf41b2bd2019-08-22 16:19:24 -0400515void GrOpsTask::setColorLoadOp(GrLoadOp op, const SkPMColor4f& color) {
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500516 fColorLoadOp = op;
517 fLoadClearColor = color;
Chris Dalton16a33c62019-09-24 22:19:17 -0600518 if (GrLoadOp::kClear == fColorLoadOp) {
519 fTotalBounds.setWH(fTarget->width(), fTarget->height());
520 }
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500521}
522
Greg Danielf41b2bd2019-08-22 16:19:24 -0400523bool GrOpsTask::resetForFullscreenClear(CanDiscardPreviousOps canDiscardPreviousOps) {
Chris Dalton6b982802019-06-27 13:53:46 -0600524 // If we previously recorded a wait op, we cannot delete the wait op. Until we track the wait
525 // ops separately from normal ops, we have to avoid clearing out any ops in this case as well.
526 if (fHasWaitOp) {
527 canDiscardPreviousOps = CanDiscardPreviousOps::kNo;
528 }
529
530 if (CanDiscardPreviousOps::kYes == canDiscardPreviousOps || this->isEmpty()) {
Robert Phillipsc994a932018-06-19 13:09:54 -0400531 this->deleteOps();
Brian Osman099fa0f2017-10-02 16:38:32 -0400532 fDeferredProxies.reset();
Greg Danielb20d7e52019-09-03 13:54:39 -0400533 fSampledProxies.reset();
Greg Daniel070cbaf2019-01-03 17:35:54 -0500534
Greg Danielf41b2bd2019-08-22 16:19:24 -0400535 // If the opsTask is using a render target which wraps a vulkan command buffer, we can't do
536 // a clear load since we cannot change the render pass that we are using. Thus we fall back
537 // to making a clear op in this case.
Robert Phillipsb5204762019-06-19 14:12:13 -0400538 return !fTarget->asRenderTargetProxy()->wrapsVkSecondaryCB();
bsalomonfd8d0132016-08-11 11:25:33 -0700539 }
Robert Phillips380b90c2017-08-30 07:41:07 -0400540
Greg Danielf41b2bd2019-08-22 16:19:24 -0400541 // Could not empty the task, so an op must be added to handle the clear
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500542 return false;
bsalomon9f129de2016-08-10 16:31:05 -0700543}
544
Greg Danielf41b2bd2019-08-22 16:19:24 -0400545void GrOpsTask::discard() {
546 // Discard calls to in-progress opsTasks are ignored. Calls at the start update the
547 // opsTasks' color & stencil load ops.
548 if (this->isEmpty()) {
549 fColorLoadOp = GrLoadOp::kDiscard;
550 fStencilLoadOp = GrLoadOp::kDiscard;
Chris Dalton16a33c62019-09-24 22:19:17 -0600551 fTotalBounds.setEmpty();
Greg Danielf41b2bd2019-08-22 16:19:24 -0400552 }
553}
554
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000555////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000556
Greg Danielf41b2bd2019-08-22 16:19:24 -0400557#ifdef SK_DEBUG
558static const char* load_op_to_name(GrLoadOp op) {
559 return GrLoadOp::kLoad == op ? "load" : GrLoadOp::kClear == op ? "clear" : "discard";
560}
561
562void GrOpsTask::dump(bool printDependencies) const {
563 GrRenderTask::dump(printDependencies);
564
565 SkDebugf("ColorLoadOp: %s %x StencilLoadOp: %s\n",
566 load_op_to_name(fColorLoadOp),
567 GrLoadOp::kClear == fColorLoadOp ? fLoadClearColor.toBytes_RGBA() : 0x0,
568 load_op_to_name(fStencilLoadOp));
569
570 SkDebugf("ops (%d):\n", fOpChains.count());
571 for (int i = 0; i < fOpChains.count(); ++i) {
572 SkDebugf("*******************************\n");
573 if (!fOpChains[i].head()) {
574 SkDebugf("%d: <combined forward or failed instantiation>\n", i);
575 } else {
576 SkDebugf("%d: %s\n", i, fOpChains[i].head()->name());
577 SkRect bounds = fOpChains[i].bounds();
578 SkDebugf("ClippedBounds: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", bounds.fLeft,
579 bounds.fTop, bounds.fRight, bounds.fBottom);
580 for (const auto& op : GrOp::ChainRange<>(fOpChains[i].head())) {
581 SkString info = SkTabString(op.dumpInfo(), 1);
582 SkDebugf("%s\n", info.c_str());
583 bounds = op.bounds();
584 SkDebugf("\tClippedBounds: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", bounds.fLeft,
585 bounds.fTop, bounds.fRight, bounds.fBottom);
586 }
587 }
588 }
589}
590
Greg Danieldcf9ca12019-08-27 14:30:21 -0400591void GrOpsTask::visitProxies_debugOnly(const VisitSurfaceProxyFunc& func) const {
592 auto textureFunc = [ func ] (GrTextureProxy* tex, GrMipMapped mipmapped) {
593 func(tex, mipmapped);
594 };
595
Greg Danielf41b2bd2019-08-22 16:19:24 -0400596 for (const OpChain& chain : fOpChains) {
Greg Danieldcf9ca12019-08-27 14:30:21 -0400597 chain.visitProxies(textureFunc);
Greg Danielf41b2bd2019-08-22 16:19:24 -0400598 }
599}
600
601#endif
602
603////////////////////////////////////////////////////////////////////////////////
604
605bool GrOpsTask::onIsUsed(GrSurfaceProxy* proxyToCheck) const {
606 bool used = false;
607
608 auto visit = [ proxyToCheck, &used ] (GrSurfaceProxy* p, GrMipMapped) {
609 if (p == proxyToCheck) {
610 used = true;
611 }
612 };
613 for (const OpChain& recordedOp : fOpChains) {
614 recordedOp.visitProxies(visit);
615 }
616
617 return used;
618}
619
620void GrOpsTask::handleInternalAllocationFailure() {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500621 bool hasUninstantiatedProxy = false;
Chris Dalton7eb5c0f2019-05-23 15:15:47 -0600622 auto checkInstantiation = [&hasUninstantiatedProxy](GrSurfaceProxy* p, GrMipMapped) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400623 if (!p->isInstantiated()) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500624 hasUninstantiatedProxy = true;
625 }
626 };
Brian Salomon588cec72018-11-14 13:56:37 -0500627 for (OpChain& recordedOp : fOpChains) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500628 hasUninstantiatedProxy = false;
Chris Dalton1706cbf2019-05-21 19:35:29 -0600629 recordedOp.visitProxies(checkInstantiation);
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500630 if (hasUninstantiatedProxy) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400631 recordedOp.setSkipExecuteFlag();
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500632 }
633 }
634}
635
Greg Danielf41b2bd2019-08-22 16:19:24 -0400636void GrOpsTask::gatherProxyIntervals(GrResourceAllocator* alloc) const {
Robert Phillips51b20f22017-12-01 15:32:35 -0500637 for (int i = 0; i < fDeferredProxies.count(); ++i) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400638 SkASSERT(!fDeferredProxies[i]->isInstantiated());
Robert Phillips51b20f22017-12-01 15:32:35 -0500639 // We give all the deferred proxies a write usage at the very start of flushing. This
640 // locks them out of being reused for the entire flush until they are read - and then
641 // they can be recycled. This is a bit unfortunate because a flush can proceed in waves
642 // with sub-flushes. The deferred proxies only need to be pinned from the start of
643 // the sub-flush in which they appear.
Robert Phillipsc73666f2019-04-24 08:49:48 -0400644 alloc->addInterval(fDeferredProxies[i], 0, 0, GrResourceAllocator::ActualUse::kNo);
Robert Phillips51b20f22017-12-01 15:32:35 -0500645 }
646
Greg Danielf41b2bd2019-08-22 16:19:24 -0400647 // Add the interval for all the writes to this GrOpsTasks's target
Brian Salomon588cec72018-11-14 13:56:37 -0500648 if (fOpChains.count()) {
Robert Phillips3bf3d4a2019-03-27 07:09:09 -0400649 unsigned int cur = alloc->curOp();
650
Robert Phillipsc73666f2019-04-24 08:49:48 -0400651 alloc->addInterval(fTarget.get(), cur, cur + fOpChains.count() - 1,
652 GrResourceAllocator::ActualUse::kYes);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500653 } else {
654 // This can happen if there is a loadOp (e.g., a clear) but no other draws. In this case we
655 // still need to add an interval for the destination so we create a fake op# for
656 // the missing clear op.
Robert Phillipsc73666f2019-04-24 08:49:48 -0400657 alloc->addInterval(fTarget.get(), alloc->curOp(), alloc->curOp(),
658 GrResourceAllocator::ActualUse::kYes);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500659 alloc->incOps();
660 }
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400661
Chris Dalton7eb5c0f2019-05-23 15:15:47 -0600662 auto gather = [ alloc SkDEBUGCODE(, this) ] (GrSurfaceProxy* p, GrMipMapped) {
Robert Phillipsc73666f2019-04-24 08:49:48 -0400663 alloc->addInterval(p, alloc->curOp(), alloc->curOp(), GrResourceAllocator::ActualUse::kYes
664 SkDEBUGCODE(, fTarget.get() == p));
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400665 };
Brian Salomon588cec72018-11-14 13:56:37 -0500666 for (const OpChain& recordedOp : fOpChains) {
Chris Dalton1706cbf2019-05-21 19:35:29 -0600667 recordedOp.visitProxies(gather);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500668
Robert Phillips3bf3d4a2019-03-27 07:09:09 -0400669 // Even though the op may have been (re)moved we still need to increment the op count to
Robert Phillipsf8e25022017-11-08 15:24:31 -0500670 // keep all the math consistent.
671 alloc->incOps();
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400672 }
673}
674
Greg Danielf41b2bd2019-08-22 16:19:24 -0400675void GrOpsTask::recordOp(
Chris Dalton945ee652019-01-23 09:10:36 -0700676 std::unique_ptr<GrOp> op, GrProcessorSet::Analysis processorAnalysis, GrAppliedClip* clip,
677 const DstProxy* dstProxy, const GrCaps& caps) {
Ethan Nicholas029b22c2018-10-18 16:49:56 -0400678 SkDEBUGCODE(op->validate();)
Chris Dalton945ee652019-01-23 09:10:36 -0700679 SkASSERT(processorAnalysis.requiresDstTexture() == (dstProxy && dstProxy->proxy()));
Robert Phillipsb5204762019-06-19 14:12:13 -0400680 SkASSERT(fTarget);
Robert Phillipsee683652017-04-26 11:53:10 -0400681
Greg Danielf41b2bd2019-08-22 16:19:24 -0400682 // A closed GrOpsTask should never receive new/more ops
robertphillips6a186652015-10-20 07:37:58 -0700683 SkASSERT(!this->isClosed());
Brian Salomon19ec80f2018-11-16 13:27:30 -0500684 if (!op->bounds().isFinite()) {
685 fOpMemoryPool->release(std::move(op));
686 return;
687 }
robertphillipsa106c622015-10-16 09:07:06 -0700688
Chris Dalton16a33c62019-09-24 22:19:17 -0600689 // Account for this op's bounds before we attempt to combine.
690 // NOTE: The caller should have already called "op->setClippedBounds()" by now, if applicable.
691 fTotalBounds.join(op->bounds());
692
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500693 // Check if there is an op we can combine with by linearly searching back until we either
694 // 1) check every op
bsalomon512be532015-09-10 10:42:55 -0700695 // 2) intersect with something
696 // 3) find a 'blocker'
Robert Phillipsb5204762019-06-19 14:12:13 -0400697 GR_AUDIT_TRAIL_ADD_OP(fAuditTrail, op.get(), fTarget->uniqueID());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400698 GrOP_INFO("opsTask: %d Recording (%s, opID: %u)\n"
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400699 "\tBounds [L: %.2f, T: %.2f R: %.2f B: %.2f]\n",
700 this->uniqueID(),
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500701 op->name(),
702 op->uniqueID(),
Robert Phillips1119dc32017-04-11 12:54:57 -0400703 op->bounds().fLeft, op->bounds().fTop,
704 op->bounds().fRight, op->bounds().fBottom);
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500705 GrOP_INFO(SkTabString(op->dumpInfo(), 1).c_str());
Brian Salomon25a88092016-12-01 09:36:50 -0500706 GrOP_INFO("\tOutcome:\n");
Brian Salomon588cec72018-11-14 13:56:37 -0500707 int maxCandidates = SkTMin(kMaxOpChainDistance, fOpChains.count());
Robert Phillips318c4192017-05-17 09:36:38 -0400708 if (maxCandidates) {
bsalomon512be532015-09-10 10:42:55 -0700709 int i = 0;
710 while (true) {
Brian Salomon588cec72018-11-14 13:56:37 -0500711 OpChain& candidate = fOpChains.fromBack(i);
Chris Dalton945ee652019-01-23 09:10:36 -0700712 op = candidate.appendOp(std::move(op), processorAnalysis, dstProxy, clip, caps,
713 fOpMemoryPool.get(), fAuditTrail);
Brian Salomon588cec72018-11-14 13:56:37 -0500714 if (!op) {
715 return;
bsalomon512be532015-09-10 10:42:55 -0700716 }
Brian Salomona7682c82018-10-24 10:04:37 -0400717 // Stop going backwards if we would cause a painter's order violation.
Brian Salomon588cec72018-11-14 13:56:37 -0500718 if (!can_reorder(candidate.bounds(), op->bounds())) {
719 GrOP_INFO("\t\tBackward: Intersects with chain (%s, head opID: %u)\n",
720 candidate.head()->name(), candidate.head()->uniqueID());
bsalomon512be532015-09-10 10:42:55 -0700721 break;
722 }
Brian Salomon588cec72018-11-14 13:56:37 -0500723 if (++i == maxCandidates) {
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400724 GrOP_INFO("\t\tBackward: Reached max lookback or beginning of op array %d\n", i);
bsalomon512be532015-09-10 10:42:55 -0700725 break;
726 }
727 }
728 } else {
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400729 GrOP_INFO("\t\tBackward: FirstOp\n");
bsalomon512be532015-09-10 10:42:55 -0700730 }
Brian Salomon54d212e2017-03-21 14:22:38 -0400731 if (clip) {
732 clip = fClipAllocator.make<GrAppliedClip>(std::move(*clip));
Robert Phillipsc84c0302017-05-08 15:35:11 -0400733 SkDEBUGCODE(fNumClips++;)
Brian Salomon54d212e2017-03-21 14:22:38 -0400734 }
Chris Dalton945ee652019-01-23 09:10:36 -0700735 fOpChains.emplace_back(std::move(op), processorAnalysis, clip, dstProxy);
bsalomon512be532015-09-10 10:42:55 -0700736}
737
Greg Danielf41b2bd2019-08-22 16:19:24 -0400738void GrOpsTask::forwardCombine(const GrCaps& caps) {
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400739 SkASSERT(!this->isClosed());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400740 GrOP_INFO("opsTask: %d ForwardCombine %d ops:\n", this->uniqueID(), fOpChains.count());
Robert Phillips48567ac2017-06-01 08:46:00 -0400741
Brian Salomon588cec72018-11-14 13:56:37 -0500742 for (int i = 0; i < fOpChains.count() - 1; ++i) {
743 OpChain& chain = fOpChains[i];
744 int maxCandidateIdx = SkTMin(i + kMaxOpChainDistance, fOpChains.count() - 1);
bsalomonaecc0182016-03-07 11:50:44 -0800745 int j = i + 1;
746 while (true) {
Brian Salomon588cec72018-11-14 13:56:37 -0500747 OpChain& candidate = fOpChains[j];
748 if (candidate.prependChain(&chain, caps, fOpMemoryPool.get(), fAuditTrail)) {
bsalomonaecc0182016-03-07 11:50:44 -0800749 break;
750 }
Robert Phillipsc84c0302017-05-08 15:35:11 -0400751 // Stop traversing if we would cause a painter's order violation.
Brian Salomon588cec72018-11-14 13:56:37 -0500752 if (!can_reorder(chain.bounds(), candidate.bounds())) {
753 GrOP_INFO(
754 "\t\t%d: chain (%s head opID: %u) -> "
755 "Intersects with chain (%s, head opID: %u)\n",
756 i, chain.head()->name(), chain.head()->uniqueID(), candidate.head()->name(),
757 candidate.head()->uniqueID());
bsalomonaecc0182016-03-07 11:50:44 -0800758 break;
759 }
Brian Salomona7682c82018-10-24 10:04:37 -0400760 if (++j > maxCandidateIdx) {
Brian Salomon588cec72018-11-14 13:56:37 -0500761 GrOP_INFO("\t\t%d: chain (%s opID: %u) -> Reached max lookahead or end of array\n",
762 i, chain.head()->name(), chain.head()->uniqueID());
bsalomonaecc0182016-03-07 11:50:44 -0800763 break;
764 }
765 }
766 }
767}
768
Chris Dalton16a33c62019-09-24 22:19:17 -0600769GrRenderTask::ExpectedOutcome GrOpsTask::onMakeClosed(
770 const GrCaps& caps, SkIRect* targetUpdateBounds) {
771 this->forwardCombine(caps);
772 if (!this->isNoOp()) {
773 SkRect clippedContentBounds = SkRect::MakeIWH(fTarget->width(), fTarget->height());
Greg Daniel94ed83f2019-09-27 13:05:43 -0400774 // TODO: If we can fix up GLPrograms test to always intersect the fTarget bounds then we can
775 // simply assert here that the bounds intersect.
Chris Dalton16a33c62019-09-24 22:19:17 -0600776 if (clippedContentBounds.intersect(fTotalBounds)) {
Greg Daniel94ed83f2019-09-27 13:05:43 -0400777 clippedContentBounds.roundOut(&fClippedContentBounds);
778 *targetUpdateBounds = fClippedContentBounds;
Chris Dalton16a33c62019-09-24 22:19:17 -0600779 return ExpectedOutcome::kTargetDirty;
780 }
781 }
782 return ExpectedOutcome::kTargetUnchanged;
783}