blob: 5c1f946f39ae09affd159c893c316f29c587d7d7 [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
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040010#include "include/gpu/GrRecordingContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/core/SkRectPriv.h"
Brian Salomon3b8486a2020-04-21 12:43:26 -040012#include "src/core/SkScopeExit.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/core/SkTraceEvent.h"
Greg Danielc0d69152020-10-08 14:59:00 -040014#include "src/gpu/GrAttachment.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040015#include "src/gpu/GrAuditTrail.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/GrCaps.h"
17#include "src/gpu/GrGpu.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrMemoryPool.h"
Greg Daniele227fe42019-08-21 13:52:24 -040019#include "src/gpu/GrOpFlushState.h"
Greg Daniel2d41d0d2019-08-26 11:08:51 -040020#include "src/gpu/GrOpsRenderPass.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/GrRecordingContextPriv.h"
Chris Dalton674f77a2019-09-30 20:49:39 -060022#include "src/gpu/GrRenderTarget.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/GrRenderTargetContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/GrResourceAllocator.h"
Brian Salomon4cfae3b2020-07-23 10:33:24 -040025#include "src/gpu/GrTexture.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040026#include "src/gpu/geometry/GrRect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/ops/GrClearOp.h"
Robert Phillipsf2361d22016-10-25 14:20:06 -040028
reed@google.comac10a2d2010-12-22 21:39:39 +000029////////////////////////////////////////////////////////////////////////////////
30
Brian Salomon09d994e2016-12-21 11:14:46 -050031// Experimentally we have found that most combining occurs within the first 10 comparisons.
Brian Salomon588cec72018-11-14 13:56:37 -050032static const int kMaxOpMergeDistance = 10;
33static const int kMaxOpChainDistance = 10;
34
35////////////////////////////////////////////////////////////////////////////////
36
Greg Daniel524e28b2019-11-01 11:48:53 -040037using DstProxyView = GrXferProcessor::DstProxyView;
Brian Salomon588cec72018-11-14 13:56:37 -050038
39////////////////////////////////////////////////////////////////////////////////
40
Brian Salomon3b8486a2020-04-21 12:43:26 -040041GrOpsTaskClosedObserver::~GrOpsTaskClosedObserver() = default;
42
43////////////////////////////////////////////////////////////////////////////////
44
Brian Salomon588cec72018-11-14 13:56:37 -050045static inline bool can_reorder(const SkRect& a, const SkRect& b) { return !GrRectsOverlap(a, b); }
46
47////////////////////////////////////////////////////////////////////////////////
48
Greg Danielf41b2bd2019-08-22 16:19:24 -040049inline GrOpsTask::OpChain::List::List(std::unique_ptr<GrOp> op)
Brian Salomon588cec72018-11-14 13:56:37 -050050 : fHead(std::move(op)), fTail(fHead.get()) {
51 this->validate();
52}
53
Greg Danielf41b2bd2019-08-22 16:19:24 -040054inline GrOpsTask::OpChain::List::List(List&& that) { *this = std::move(that); }
Brian Salomon588cec72018-11-14 13:56:37 -050055
Greg Danielf41b2bd2019-08-22 16:19:24 -040056inline GrOpsTask::OpChain::List& GrOpsTask::OpChain::List::operator=(List&& that) {
Brian Salomon588cec72018-11-14 13:56:37 -050057 fHead = std::move(that.fHead);
58 fTail = that.fTail;
59 that.fTail = nullptr;
60 this->validate();
61 return *this;
62}
63
Greg Danielf41b2bd2019-08-22 16:19:24 -040064inline std::unique_ptr<GrOp> GrOpsTask::OpChain::List::popHead() {
Brian Salomon588cec72018-11-14 13:56:37 -050065 SkASSERT(fHead);
66 auto temp = fHead->cutChain();
67 std::swap(temp, fHead);
68 if (!fHead) {
69 SkASSERT(fTail == temp.get());
70 fTail = nullptr;
71 }
72 return temp;
73}
74
Greg Danielf41b2bd2019-08-22 16:19:24 -040075inline std::unique_ptr<GrOp> GrOpsTask::OpChain::List::removeOp(GrOp* op) {
Brian Salomon588cec72018-11-14 13:56:37 -050076#ifdef SK_DEBUG
77 auto head = op;
78 while (head->prevInChain()) { head = head->prevInChain(); }
79 SkASSERT(head == fHead.get());
80#endif
81 auto prev = op->prevInChain();
82 if (!prev) {
83 SkASSERT(op == fHead.get());
84 return this->popHead();
85 }
86 auto temp = prev->cutChain();
87 if (auto next = temp->cutChain()) {
88 prev->chainConcat(std::move(next));
89 } else {
90 SkASSERT(fTail == op);
91 fTail = prev;
92 }
93 this->validate();
94 return temp;
95}
96
Greg Danielf41b2bd2019-08-22 16:19:24 -040097inline void GrOpsTask::OpChain::List::pushHead(std::unique_ptr<GrOp> op) {
Brian Salomon588cec72018-11-14 13:56:37 -050098 SkASSERT(op);
99 SkASSERT(op->isChainHead());
100 SkASSERT(op->isChainTail());
101 if (fHead) {
102 op->chainConcat(std::move(fHead));
103 fHead = std::move(op);
104 } else {
105 fHead = std::move(op);
106 fTail = fHead.get();
107 }
108}
109
Greg Danielf41b2bd2019-08-22 16:19:24 -0400110inline void GrOpsTask::OpChain::List::pushTail(std::unique_ptr<GrOp> op) {
Brian Salomon588cec72018-11-14 13:56:37 -0500111 SkASSERT(op->isChainTail());
112 fTail->chainConcat(std::move(op));
113 fTail = fTail->nextInChain();
114}
115
Greg Danielf41b2bd2019-08-22 16:19:24 -0400116inline void GrOpsTask::OpChain::List::validate() const {
Brian Salomon588cec72018-11-14 13:56:37 -0500117#ifdef SK_DEBUG
118 if (fHead) {
119 SkASSERT(fTail);
120 fHead->validateChain(fTail);
121 }
122#endif
123}
124
125////////////////////////////////////////////////////////////////////////////////
126
Greg Danielf41b2bd2019-08-22 16:19:24 -0400127GrOpsTask::OpChain::OpChain(std::unique_ptr<GrOp> op,
128 GrProcessorSet::Analysis processorAnalysis,
Greg Daniel524e28b2019-11-01 11:48:53 -0400129 GrAppliedClip* appliedClip, const DstProxyView* dstProxyView)
Chris Dalton945ee652019-01-23 09:10:36 -0700130 : fList{std::move(op)}
131 , fProcessorAnalysis(processorAnalysis)
132 , fAppliedClip(appliedClip) {
133 if (fProcessorAnalysis.requiresDstTexture()) {
Greg Daniel524e28b2019-11-01 11:48:53 -0400134 SkASSERT(dstProxyView && dstProxyView->proxy());
135 fDstProxyView = *dstProxyView;
Brian Salomon588cec72018-11-14 13:56:37 -0500136 }
137 fBounds = fList.head()->bounds();
138}
139
Greg Danielf41b2bd2019-08-22 16:19:24 -0400140void GrOpsTask::OpChain::visitProxies(const GrOp::VisitProxyFunc& func) const {
Brian Salomon588cec72018-11-14 13:56:37 -0500141 if (fList.empty()) {
142 return;
143 }
144 for (const auto& op : GrOp::ChainRange<>(fList.head())) {
Chris Dalton1706cbf2019-05-21 19:35:29 -0600145 op.visitProxies(func);
Brian Salomon588cec72018-11-14 13:56:37 -0500146 }
Greg Daniel524e28b2019-11-01 11:48:53 -0400147 if (fDstProxyView.proxy()) {
Brian Salomon7e67dca2020-07-21 09:27:25 -0400148 func(fDstProxyView.proxy(), GrMipmapped::kNo);
Brian Salomon588cec72018-11-14 13:56:37 -0500149 }
150 if (fAppliedClip) {
151 fAppliedClip->visitProxies(func);
152 }
153}
154
Greg Danielf41b2bd2019-08-22 16:19:24 -0400155void GrOpsTask::OpChain::deleteOps(GrOpMemoryPool* pool) {
Brian Salomon588cec72018-11-14 13:56:37 -0500156 while (!fList.empty()) {
Herb Derbyd6cfe722020-10-05 15:50:47 -0400157 #if defined(GR_OP_ALLOCATE_USE_NEW)
158 // Since the value goes out of scope immediately, the unique_ptr deletes the op.
159 fList.popHead();
160 #else
161 pool->release(fList.popHead());
162 #endif
Brian Salomon588cec72018-11-14 13:56:37 -0500163 }
164}
165
166// Concatenates two op chains and attempts to merge ops across the chains. Assumes that we know that
167// the two chains are chainable. Returns the new chain.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400168GrOpsTask::OpChain::List GrOpsTask::OpChain::DoConcat(
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500169 List chainA, List chainB, const GrCaps& caps, GrRecordingContext::Arenas* arenas,
170 GrAuditTrail* auditTrail) {
Brian Salomon588cec72018-11-14 13:56:37 -0500171 // We process ops in chain b from head to tail. We attempt to merge with nodes in a, starting
172 // at chain a's tail and working toward the head. We produce one of the following outcomes:
173 // 1) b's head is merged into an op in a.
174 // 2) An op from chain a is merged into b's head. (In this case b's head gets processed again.)
175 // 3) b's head is popped from chain a and added at the tail of a.
176 // After result 3 we don't want to attempt to merge the next head of b with the new tail of a,
177 // as we assume merges were already attempted when chain b was created. So we keep track of the
178 // original tail of a and start our iteration of a there. We also track the bounds of the nodes
179 // appended to chain a that will be skipped for bounds testing. If the original tail of a is
180 // merged into an op in b (case 2) then we advance the "original tail" towards the head of a.
181 GrOp* origATail = chainA.tail();
182 SkRect skipBounds = SkRectPriv::MakeLargestInverted();
183 do {
184 int numMergeChecks = 0;
185 bool merged = false;
186 bool noSkip = (origATail == chainA.tail());
187 SkASSERT(noSkip == (skipBounds == SkRectPriv::MakeLargestInverted()));
188 bool canBackwardMerge = noSkip || can_reorder(chainB.head()->bounds(), skipBounds);
189 SkRect forwardMergeBounds = skipBounds;
190 GrOp* a = origATail;
191 while (a) {
192 bool canForwardMerge =
193 (a == chainA.tail()) || can_reorder(a->bounds(), forwardMergeBounds);
194 if (canForwardMerge || canBackwardMerge) {
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500195 auto result = a->combineIfPossible(chainB.head(), arenas, caps);
Brian Salomon588cec72018-11-14 13:56:37 -0500196 SkASSERT(result != GrOp::CombineResult::kCannotCombine);
197 merged = (result == GrOp::CombineResult::kMerged);
Robert Phillips9548c3b422019-01-08 12:35:43 -0500198 GrOP_INFO("\t\t: (%s opID: %u) -> Combining with (%s, opID: %u)\n",
Brian Salomon588cec72018-11-14 13:56:37 -0500199 chainB.head()->name(), chainB.head()->uniqueID(), a->name(),
200 a->uniqueID());
Brian Salomon588cec72018-11-14 13:56:37 -0500201 }
202 if (merged) {
Brian Salomon52a6ed32018-11-26 10:30:58 -0500203 GR_AUDIT_TRAIL_OPS_RESULT_COMBINED(auditTrail, a, chainB.head());
Brian Salomon588cec72018-11-14 13:56:37 -0500204 if (canBackwardMerge) {
Herb Derbyd6cfe722020-10-05 15:50:47 -0400205 #if defined(GR_OP_ALLOCATE_USE_NEW)
206 // The unique_ptr releases the op.
207 chainB.popHead();
208 #else
209 arenas->opMemoryPool()->release(chainB.popHead());
210 #endif
Brian Salomon588cec72018-11-14 13:56:37 -0500211 } else {
212 // We merged the contents of b's head into a. We will replace b's head with a in
213 // chain b.
214 SkASSERT(canForwardMerge);
215 if (a == origATail) {
216 origATail = a->prevInChain();
217 }
218 std::unique_ptr<GrOp> detachedA = chainA.removeOp(a);
Herb Derbyd6cfe722020-10-05 15:50:47 -0400219 #if defined(GR_OP_ALLOCATE_USE_NEW)
220 // The unique_ptr releases the op.
221 chainB.popHead();
222 #else
223 arenas->opMemoryPool()->release(chainB.popHead());
224 #endif
Brian Salomon588cec72018-11-14 13:56:37 -0500225 chainB.pushHead(std::move(detachedA));
226 if (chainA.empty()) {
227 // We merged all the nodes in chain a to chain b.
228 return chainB;
229 }
230 }
231 break;
232 } else {
233 if (++numMergeChecks == kMaxOpMergeDistance) {
234 break;
235 }
236 forwardMergeBounds.joinNonEmptyArg(a->bounds());
237 canBackwardMerge =
238 canBackwardMerge && can_reorder(chainB.head()->bounds(), a->bounds());
239 a = a->prevInChain();
240 }
241 }
242 // If we weren't able to merge b's head then pop b's head from chain b and make it the new
243 // tail of a.
244 if (!merged) {
245 chainA.pushTail(chainB.popHead());
246 skipBounds.joinNonEmptyArg(chainA.tail()->bounds());
247 }
248 } while (!chainB.empty());
249 return chainA;
250}
251
Chris Dalton945ee652019-01-23 09:10:36 -0700252// Attempts to concatenate the given chain onto our own and merge ops across the chains. Returns
253// whether the operation succeeded. On success, the provided list will be returned empty.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400254bool GrOpsTask::OpChain::tryConcat(
Greg Daniel524e28b2019-11-01 11:48:53 -0400255 List* list, GrProcessorSet::Analysis processorAnalysis, const DstProxyView& dstProxyView,
Chris Dalton945ee652019-01-23 09:10:36 -0700256 const GrAppliedClip* appliedClip, const SkRect& bounds, const GrCaps& caps,
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500257 GrRecordingContext::Arenas* arenas, GrAuditTrail* auditTrail) {
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700258 SkASSERT(!fList.empty());
259 SkASSERT(!list->empty());
Greg Daniel524e28b2019-11-01 11:48:53 -0400260 SkASSERT(fProcessorAnalysis.requiresDstTexture() == SkToBool(fDstProxyView.proxy()));
261 SkASSERT(processorAnalysis.requiresDstTexture() == SkToBool(dstProxyView.proxy()));
Brian Salomon588cec72018-11-14 13:56:37 -0500262 // All returns use explicit tuple constructor rather than {a, b} to work around old GCC bug.
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700263 if (fList.head()->classID() != list->head()->classID() ||
264 SkToBool(fAppliedClip) != SkToBool(appliedClip) ||
265 (fAppliedClip && *fAppliedClip != *appliedClip) ||
Chris Dalton945ee652019-01-23 09:10:36 -0700266 (fProcessorAnalysis.requiresNonOverlappingDraws() !=
267 processorAnalysis.requiresNonOverlappingDraws()) ||
268 (fProcessorAnalysis.requiresNonOverlappingDraws() &&
269 // Non-overlaping draws are only required when Ganesh will either insert a barrier,
270 // or read back a new dst texture between draws. In either case, we can neither
271 // chain nor combine overlapping Ops.
272 GrRectsTouchOrOverlap(fBounds, bounds)) ||
273 (fProcessorAnalysis.requiresDstTexture() != processorAnalysis.requiresDstTexture()) ||
Greg Daniel524e28b2019-11-01 11:48:53 -0400274 (fProcessorAnalysis.requiresDstTexture() && fDstProxyView != dstProxyView)) {
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700275 return false;
Brian Salomon588cec72018-11-14 13:56:37 -0500276 }
Chris Daltonee21e6b2019-01-22 14:04:43 -0700277
Brian Salomon588cec72018-11-14 13:56:37 -0500278 SkDEBUGCODE(bool first = true;)
279 do {
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500280 switch (fList.tail()->combineIfPossible(list->head(), arenas, caps)) {
Brian Salomon588cec72018-11-14 13:56:37 -0500281 case GrOp::CombineResult::kCannotCombine:
282 // If an op supports chaining then it is required that chaining is transitive and
283 // that if any two ops in two different chains can merge then the two chains
284 // may also be chained together. Thus, we should only hit this on the first
285 // iteration.
286 SkASSERT(first);
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700287 return false;
Brian Salomon588cec72018-11-14 13:56:37 -0500288 case GrOp::CombineResult::kMayChain:
Adlai Holler5ba50af2020-04-29 21:11:14 -0400289 fList = DoConcat(std::move(fList), std::exchange(*list, List()), caps, arenas,
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500290 auditTrail);
Chris Daltonee21e6b2019-01-22 14:04:43 -0700291 // The above exchange cleared out 'list'. The list needs to be empty now for the
292 // loop to terminate.
293 SkASSERT(list->empty());
294 break;
Brian Salomon588cec72018-11-14 13:56:37 -0500295 case GrOp::CombineResult::kMerged: {
Robert Phillips9548c3b422019-01-08 12:35:43 -0500296 GrOP_INFO("\t\t: (%s opID: %u) -> Combining with (%s, opID: %u)\n",
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700297 list->tail()->name(), list->tail()->uniqueID(), list->head()->name(),
298 list->head()->uniqueID());
299 GR_AUDIT_TRAIL_OPS_RESULT_COMBINED(auditTrail, fList.tail(), list->head());
Herb Derbyd6cfe722020-10-05 15:50:47 -0400300 #if defined(GR_OP_ALLOCATE_USE_NEW)
301 // The unique_ptr releases the op.
302 list->popHead();
303 #else
304 arenas->opMemoryPool()->release(list->popHead());
305 #endif
Brian Salomon588cec72018-11-14 13:56:37 -0500306 break;
307 }
308 }
309 SkDEBUGCODE(first = false);
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700310 } while (!list->empty());
Chris Daltonee21e6b2019-01-22 14:04:43 -0700311
312 // The new ops were successfully merged and/or chained onto our own.
313 fBounds.joinPossiblyEmptyRect(bounds);
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700314 return true;
Brian Salomon588cec72018-11-14 13:56:37 -0500315}
316
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500317bool GrOpsTask::OpChain::prependChain(OpChain* that, const GrCaps& caps,
318 GrRecordingContext::Arenas* arenas,
319 GrAuditTrail* auditTrail) {
Greg Daniel524e28b2019-11-01 11:48:53 -0400320 if (!that->tryConcat(&fList, fProcessorAnalysis, fDstProxyView, fAppliedClip, fBounds, caps,
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500321 arenas, auditTrail)) {
Brian Salomon588cec72018-11-14 13:56:37 -0500322 this->validate();
323 // append failed
324 return false;
325 }
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700326
Brian Salomon588cec72018-11-14 13:56:37 -0500327 // 'that' owns the combined chain. Move it into 'this'.
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700328 SkASSERT(fList.empty());
Brian Salomon588cec72018-11-14 13:56:37 -0500329 fList = std::move(that->fList);
Chris Daltonee21e6b2019-01-22 14:04:43 -0700330 fBounds = that->fBounds;
Brian Salomon588cec72018-11-14 13:56:37 -0500331
Greg Daniel524e28b2019-11-01 11:48:53 -0400332 that->fDstProxyView.setProxyView({});
John Stiles59e18dc2020-07-22 18:18:12 -0400333 if (that->fAppliedClip && that->fAppliedClip->hasCoverageFragmentProcessor()) {
334 // Obliterates the processor.
335 that->fAppliedClip->detachCoverageFragmentProcessor();
Brian Salomon588cec72018-11-14 13:56:37 -0500336 }
337 this->validate();
338 return true;
339}
340
Greg Danielf41b2bd2019-08-22 16:19:24 -0400341std::unique_ptr<GrOp> GrOpsTask::OpChain::appendOp(
Chris Dalton945ee652019-01-23 09:10:36 -0700342 std::unique_ptr<GrOp> op, GrProcessorSet::Analysis processorAnalysis,
Greg Daniel524e28b2019-11-01 11:48:53 -0400343 const DstProxyView* dstProxyView, const GrAppliedClip* appliedClip, const GrCaps& caps,
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500344 GrRecordingContext::Arenas* arenas, GrAuditTrail* auditTrail) {
Greg Daniel524e28b2019-11-01 11:48:53 -0400345 const GrXferProcessor::DstProxyView noDstProxyView;
346 if (!dstProxyView) {
347 dstProxyView = &noDstProxyView;
Brian Salomon588cec72018-11-14 13:56:37 -0500348 }
349 SkASSERT(op->isChainHead() && op->isChainTail());
350 SkRect opBounds = op->bounds();
351 List chain(std::move(op));
Chris Dalton945ee652019-01-23 09:10:36 -0700352 if (!this->tryConcat(
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500353 &chain, processorAnalysis, *dstProxyView, appliedClip, opBounds, caps,
354 arenas, auditTrail)) {
Brian Salomon588cec72018-11-14 13:56:37 -0500355 // append failed, give the op back to the caller.
356 this->validate();
357 return chain.popHead();
358 }
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700359
360 SkASSERT(chain.empty());
Brian Salomon588cec72018-11-14 13:56:37 -0500361 this->validate();
362 return nullptr;
363}
364
Greg Danielf41b2bd2019-08-22 16:19:24 -0400365inline void GrOpsTask::OpChain::validate() const {
Brian Salomon588cec72018-11-14 13:56:37 -0500366#ifdef SK_DEBUG
367 fList.validate();
368 for (const auto& op : GrOp::ChainRange<>(fList.head())) {
369 // Not using SkRect::contains because we allow empty rects.
370 SkASSERT(fBounds.fLeft <= op.bounds().fLeft && fBounds.fTop <= op.bounds().fTop &&
371 fBounds.fRight >= op.bounds().fRight && fBounds.fBottom >= op.bounds().fBottom);
372 }
373#endif
374}
375
376////////////////////////////////////////////////////////////////////////////////
bsalomon489147c2015-12-14 12:13:09 -0800377
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400378GrOpsTask::GrOpsTask(GrDrawingManager* drawingMgr, GrRecordingContext::Arenas arenas,
Greg Daniel16f5c652019-10-29 11:26:01 -0400379 GrSurfaceProxyView view,
Greg Danielf41b2bd2019-08-22 16:19:24 -0400380 GrAuditTrail* auditTrail)
Adlai Holler33d569e2020-06-16 14:30:08 -0400381 : GrRenderTask()
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500382 , fArenas(arenas)
Greg Danielf41b2bd2019-08-22 16:19:24 -0400383 , fAuditTrail(auditTrail)
Robert Phillipsb6deea82017-05-11 14:14:30 -0400384 SkDEBUGCODE(, fNumClips(0)) {
Adlai Holler33d569e2020-06-16 14:30:08 -0400385 this->addTarget(drawingMgr, std::move(view));
bsalomon4061b122015-05-29 10:26:19 -0700386}
387
Greg Danielf41b2bd2019-08-22 16:19:24 -0400388void GrOpsTask::deleteOps() {
Brian Salomon588cec72018-11-14 13:56:37 -0500389 for (auto& chain : fOpChains) {
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500390 chain.deleteOps(fArenas.opMemoryPool());
Robert Phillipsc994a932018-06-19 13:09:54 -0400391 }
Brian Salomon588cec72018-11-14 13:56:37 -0500392 fOpChains.reset();
Robert Phillipsc994a932018-06-19 13:09:54 -0400393}
394
Greg Danielf41b2bd2019-08-22 16:19:24 -0400395GrOpsTask::~GrOpsTask() {
Robert Phillipsc994a932018-06-19 13:09:54 -0400396 this->deleteOps();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000397}
398
Brian Salomonf1c9eae2020-05-01 15:00:34 -0400399void GrOpsTask::removeClosedObserver(GrOpsTaskClosedObserver* observer) {
400 SkASSERT(observer);
401 for (int i = 0; i < fClosedObservers.count(); ++i) {
402 if (fClosedObservers[i] == observer) {
403 fClosedObservers.removeShuffle(i);
404 --i;
405 }
406 }
407}
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000408
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400409void GrOpsTask::endFlush(GrDrawingManager* drawingMgr) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400410 fLastClipStackGenID = SK_InvalidUniqueID;
411 this->deleteOps();
412 fClipAllocator.reset();
Chris Dalton706a6ff2017-11-29 22:01:06 -0700413
Greg Danielf41b2bd2019-08-22 16:19:24 -0400414 fDeferredProxies.reset();
Greg Danielb20d7e52019-09-03 13:54:39 -0400415 fSampledProxies.reset();
Greg Danielf41b2bd2019-08-22 16:19:24 -0400416 fAuditTrail = nullptr;
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400417
418 GrRenderTask::endFlush(drawingMgr);
Greg Danielf21bf9e2019-08-22 20:12:20 +0000419}
420
Robert Phillips29f38542019-10-16 09:20:25 -0400421void GrOpsTask::onPrePrepare(GrRecordingContext* context) {
Robert Phillips7327c9d2019-10-08 16:32:56 -0400422 SkASSERT(this->isClosed());
423#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
424 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
425#endif
426 // TODO: remove the check for discard here once reduced op splitting is turned on. Currently we
427 // can end up with GrOpsTasks that only have a discard load op and no ops. For vulkan validation
428 // we need to keep that discard and not drop it. Once we have reduce op list splitting enabled
429 // we shouldn't end up with GrOpsTasks with only discard.
430 if (this->isNoOp() || (fClippedContentBounds.isEmpty() && fColorLoadOp != GrLoadOp::kDiscard)) {
431 return;
432 }
433
434 for (const auto& chain : fOpChains) {
435 if (chain.shouldExecute()) {
Robert Phillips8053c972019-11-21 10:44:53 -0500436 chain.head()->prePrepare(context,
Adlai Holler33d569e2020-06-16 14:30:08 -0400437 &fTargets[0],
Robert Phillips8053c972019-11-21 10:44:53 -0500438 chain.appliedClip(),
Greg Danield358cbe2020-09-11 09:33:54 -0400439 chain.dstProxyView(),
440 fRenderPassXferBarriers);
Robert Phillips7327c9d2019-10-08 16:32:56 -0400441 }
442 }
443}
444
Greg Danielf41b2bd2019-08-22 16:19:24 -0400445void GrOpsTask::onPrepare(GrOpFlushState* flushState) {
Adlai Holler33d569e2020-06-16 14:30:08 -0400446 SkASSERT(this->target(0).proxy()->peekRenderTarget());
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400447 SkASSERT(this->isClosed());
Stan Iliev2af578d2017-08-16 13:00:28 -0400448#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
Brian Salomon5f394272019-07-02 14:07:49 -0400449 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Stan Iliev2af578d2017-08-16 13:00:28 -0400450#endif
Greg Daniel94ed83f2019-09-27 13:05:43 -0400451 // TODO: remove the check for discard here once reduced op splitting is turned on. Currently we
452 // can end up with GrOpsTasks that only have a discard load op and no ops. For vulkan validation
453 // we need to keep that discard and not drop it. Once we have reduce op list splitting enabled
454 // we shouldn't end up with GrOpsTasks with only discard.
455 if (this->isNoOp() || (fClippedContentBounds.isEmpty() && fColorLoadOp != GrLoadOp::kDiscard)) {
456 return;
457 }
robertphillipsa106c622015-10-16 09:07:06 -0700458
Greg Danielb20d7e52019-09-03 13:54:39 -0400459 flushState->setSampledProxyArray(&fSampledProxies);
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500460 // Loop over the ops that haven't yet been prepared.
Brian Salomon588cec72018-11-14 13:56:37 -0500461 for (const auto& chain : fOpChains) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400462 if (chain.shouldExecute()) {
Stan Iliev2af578d2017-08-16 13:00:28 -0400463#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
Brian Salomon5f394272019-07-02 14:07:49 -0400464 TRACE_EVENT0("skia.gpu", chain.head()->name());
Stan Iliev2af578d2017-08-16 13:00:28 -0400465#endif
Robert Phillips901aff02019-10-08 12:32:56 -0400466 GrOpFlushState::OpArgs opArgs(chain.head(),
Adlai Holler33d569e2020-06-16 14:30:08 -0400467 &fTargets[0],
Robert Phillips901aff02019-10-08 12:32:56 -0400468 chain.appliedClip(),
Greg Danield358cbe2020-09-11 09:33:54 -0400469 chain.dstProxyView(),
470 fRenderPassXferBarriers);
Robert Phillips405413f2019-10-04 10:39:28 -0400471
Brian Salomon29b60c92017-10-31 14:42:10 -0400472 flushState->setOpArgs(&opArgs);
Robert Phillipsdf70f152019-11-15 14:57:05 -0500473
474 // Temporary debugging helper: for debugging prePrepare w/o going through DDLs
475 // Delete once most of the GrOps have an onPrePrepare.
Adlai Holler33d569e2020-06-16 14:30:08 -0400476 // chain.head()->prePrepare(flushState->gpu()->getContext(), &this->target(0),
Robert Phillipsdf70f152019-11-15 14:57:05 -0500477 // chain.appliedClip());
478
Robert Phillips7327c9d2019-10-08 16:32:56 -0400479 // GrOp::prePrepare may or may not have been called at this point
Brian Salomon588cec72018-11-14 13:56:37 -0500480 chain.head()->prepare(flushState);
Brian Salomon29b60c92017-10-31 14:42:10 -0400481 flushState->setOpArgs(nullptr);
bsalomonaecc0182016-03-07 11:50:44 -0800482 }
bsalomon512be532015-09-10 10:42:55 -0700483 }
Greg Danielb20d7e52019-09-03 13:54:39 -0400484 flushState->setSampledProxyArray(nullptr);
robertphillipsa13e2022015-11-11 12:01:09 -0800485}
bsalomon512be532015-09-10 10:42:55 -0700486
Greg Danielc0d69152020-10-08 14:59:00 -0400487static GrOpsRenderPass* create_render_pass(GrGpu* gpu,
488 GrRenderTarget* rt,
489 GrAttachment* stencil,
490 GrSurfaceOrigin origin,
491 const SkIRect& bounds,
492 GrLoadOp colorLoadOp,
493 const SkPMColor4f& loadClearColor,
494 GrLoadOp stencilLoadOp,
495 GrStoreOp stencilStoreOp,
496 const SkTArray<GrSurfaceProxy*, true>& sampledProxies,
497 GrXferBarrierFlags renderPassXferBarriers) {
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400498 const GrOpsRenderPass::LoadAndStoreInfo kColorLoadStoreInfo {
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400499 colorLoadOp,
500 GrStoreOp::kStore,
501 loadClearColor
Robert Phillips178ce3e2017-04-13 09:15:47 -0400502 };
503
Robert Phillips95214472017-08-08 18:00:03 -0400504 // TODO:
505 // We would like to (at this level) only ever clear & discard. We would need
Greg Danielf41b2bd2019-08-22 16:19:24 -0400506 // to stop splitting up higher level OpsTasks for copyOps to achieve that.
Robert Phillips95214472017-08-08 18:00:03 -0400507 // Note: we would still need SB loads and stores but they would happen at a
508 // lower level (inside the VK command buffer).
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400509 const GrOpsRenderPass::StencilLoadAndStoreInfo stencilLoadAndStoreInfo {
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400510 stencilLoadOp,
Chris Dalton674f77a2019-09-30 20:49:39 -0600511 stencilStoreOp,
Robert Phillips95214472017-08-08 18:00:03 -0400512 };
513
Robert Phillips96f22372020-05-20 12:31:18 -0400514 return gpu->getOpsRenderPass(rt, stencil, origin, bounds,
Greg Daniel9a18b082020-08-14 14:03:50 -0400515 kColorLoadStoreInfo, stencilLoadAndStoreInfo, sampledProxies,
Greg Daniel21774362020-09-14 10:36:43 -0400516 renderPassXferBarriers);
Robert Phillips178ce3e2017-04-13 09:15:47 -0400517}
518
Brian Salomon25a88092016-12-01 09:36:50 -0500519// TODO: this is where GrOp::renderTarget is used (which is fine since it
Robert Phillips294870f2016-11-11 12:38:40 -0500520// is at flush time). However, we need to store the RenderTargetProxy in the
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500521// Ops and instantiate them here.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400522bool GrOpsTask::onExecute(GrOpFlushState* flushState) {
Greg Daniel94ed83f2019-09-27 13:05:43 -0400523 // TODO: remove the check for discard here once reduced op splitting is turned on. Currently we
524 // can end up with GrOpsTasks that only have a discard load op and no ops. For vulkan validation
525 // we need to keep that discard and not drop it. Once we have reduce op list splitting enabled
526 // we shouldn't end up with GrOpsTasks with only discard.
527 if (this->isNoOp() || (fClippedContentBounds.isEmpty() && fColorLoadOp != GrLoadOp::kDiscard)) {
bsalomondc438982016-08-31 11:53:49 -0700528 return false;
egdanielb4021cf2016-07-28 08:53:07 -0700529 }
Robert Phillips4a395042017-04-24 16:27:17 +0000530
Adlai Holler33d569e2020-06-16 14:30:08 -0400531 SkASSERT(this->numTargets() == 1);
532 GrRenderTargetProxy* proxy = this->target(0).proxy()->asRenderTargetProxy();
Greg Daniel16f5c652019-10-29 11:26:01 -0400533 SkASSERT(proxy);
Brian Salomon5f394272019-07-02 14:07:49 -0400534 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400535
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500536 // Make sure load ops are not kClear if the GPU needs to use draws for clears
537 SkASSERT(fColorLoadOp != GrLoadOp::kClear ||
538 !flushState->gpu()->caps()->performColorClearsAsDraws());
Chris Dalton674f77a2019-09-30 20:49:39 -0600539
540 const GrCaps& caps = *flushState->gpu()->caps();
Greg Daniel16f5c652019-10-29 11:26:01 -0400541 GrRenderTarget* renderTarget = proxy->peekRenderTarget();
Chris Dalton674f77a2019-09-30 20:49:39 -0600542 SkASSERT(renderTarget);
Chris Dalton0b68dda2019-11-07 21:08:03 -0700543
Greg Danielc0d69152020-10-08 14:59:00 -0400544 GrAttachment* stencil = nullptr;
Chris Dalton0b68dda2019-11-07 21:08:03 -0700545 if (int numStencilSamples = proxy->numStencilSamples()) {
546 if (!flushState->resourceProvider()->attachStencilAttachment(
547 renderTarget, numStencilSamples)) {
548 SkDebugf("WARNING: failed to attach a stencil buffer. Rendering will be skipped.\n");
549 return false;
550 }
Brian Salomonf7f54332020-07-28 09:23:35 -0400551 stencil = renderTarget->getStencilAttachment();
Chris Dalton0b68dda2019-11-07 21:08:03 -0700552 }
553
Chris Daltonf00b95b2019-11-07 21:14:41 -0700554 SkASSERT(!stencil || stencil->numSamples() == proxy->numStencilSamples());
Chris Dalton674f77a2019-09-30 20:49:39 -0600555
556 GrLoadOp stencilLoadOp;
557 switch (fInitialStencilContent) {
558 case StencilContent::kDontCare:
559 stencilLoadOp = GrLoadOp::kDiscard;
560 break;
561 case StencilContent::kUserBitsCleared:
562 SkASSERT(!caps.performStencilClearsAsDraws());
563 SkASSERT(stencil);
564 if (caps.discardStencilValuesAfterRenderPass()) {
565 // Always clear the stencil if it is being discarded after render passes. This is
566 // also an optimization because we are on a tiler and it avoids loading the values
567 // from memory.
568 stencilLoadOp = GrLoadOp::kClear;
569 break;
570 }
571 if (!stencil->hasPerformedInitialClear()) {
572 stencilLoadOp = GrLoadOp::kClear;
573 stencil->markHasPerformedInitialClear();
574 break;
575 }
576 // renderTargetContexts are required to leave the user stencil bits in a cleared state
577 // once finished, meaning the stencil values will always remain cleared after the
578 // initial clear. Just fall through to reloading the existing (cleared) stencil values
579 // from memory.
John Stiles30212b72020-06-11 17:55:07 -0400580 [[fallthrough]];
Chris Dalton674f77a2019-09-30 20:49:39 -0600581 case StencilContent::kPreserved:
582 SkASSERT(stencil);
583 stencilLoadOp = GrLoadOp::kLoad;
584 break;
585 }
586
587 // NOTE: If fMustPreserveStencil is set, then we are executing a renderTargetContext that split
588 // its opsTask.
589 //
590 // FIXME: We don't currently flag render passes that don't use stencil at all. In that case
591 // their store op might be "discard", and we currently make the assumption that a discard will
592 // not invalidate what's already in main memory. This is probably ok for now, but certainly
593 // something we want to address soon.
594 GrStoreOp stencilStoreOp = (caps.discardStencilValuesAfterRenderPass() && !fMustPreserveStencil)
595 ? GrStoreOp::kDiscard
596 : GrStoreOp::kStore;
597
Greg Daniel4a0d36d2019-09-30 12:24:36 -0400598 GrOpsRenderPass* renderPass = create_render_pass(
Adlai Holler33d569e2020-06-16 14:30:08 -0400599 flushState->gpu(), proxy->peekRenderTarget(), stencil, this->target(0).origin(),
Chris Dalton674f77a2019-09-30 20:49:39 -0600600 fClippedContentBounds, fColorLoadOp, fLoadClearColor, stencilLoadOp, stencilStoreOp,
Greg Daniel21774362020-09-14 10:36:43 -0400601 fSampledProxies, fRenderPassXferBarriers);
602
Greg Danielfa3adf72019-11-07 09:53:41 -0500603 if (!renderPass) {
604 return false;
605 }
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400606 flushState->setOpsRenderPass(renderPass);
607 renderPass->begin();
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400608
609 // Draw all the generated geometry.
Brian Salomon588cec72018-11-14 13:56:37 -0500610 for (const auto& chain : fOpChains) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400611 if (!chain.shouldExecute()) {
bsalomonaecc0182016-03-07 11:50:44 -0800612 continue;
613 }
Stan Iliev2af578d2017-08-16 13:00:28 -0400614#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
Brian Salomon5f394272019-07-02 14:07:49 -0400615 TRACE_EVENT0("skia.gpu", chain.head()->name());
Stan Iliev2af578d2017-08-16 13:00:28 -0400616#endif
Robert Phillips178ce3e2017-04-13 09:15:47 -0400617
Robert Phillips405413f2019-10-04 10:39:28 -0400618 GrOpFlushState::OpArgs opArgs(chain.head(),
Adlai Holler33d569e2020-06-16 14:30:08 -0400619 &fTargets[0],
Robert Phillips405413f2019-10-04 10:39:28 -0400620 chain.appliedClip(),
Greg Danield358cbe2020-09-11 09:33:54 -0400621 chain.dstProxyView(),
622 fRenderPassXferBarriers);
Robert Phillips178ce3e2017-04-13 09:15:47 -0400623
Brian Salomon29b60c92017-10-31 14:42:10 -0400624 flushState->setOpArgs(&opArgs);
Brian Salomon588cec72018-11-14 13:56:37 -0500625 chain.head()->execute(flushState, chain.bounds());
Brian Salomon29b60c92017-10-31 14:42:10 -0400626 flushState->setOpArgs(nullptr);
bsalomon512be532015-09-10 10:42:55 -0700627 }
Robert Phillips178ce3e2017-04-13 09:15:47 -0400628
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400629 renderPass->end();
630 flushState->gpu()->submit(renderPass);
631 flushState->setOpsRenderPass(nullptr);
ethannicholas22793252016-01-30 09:59:10 -0800632
bsalomondc438982016-08-31 11:53:49 -0700633 return true;
bsalomona73239a2015-04-28 13:35:17 -0700634}
635
Greg Danielf41b2bd2019-08-22 16:19:24 -0400636void GrOpsTask::setColorLoadOp(GrLoadOp op, const SkPMColor4f& color) {
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500637 fColorLoadOp = op;
638 fLoadClearColor = color;
Chris Dalton16a33c62019-09-24 22:19:17 -0600639 if (GrLoadOp::kClear == fColorLoadOp) {
Adlai Holler33d569e2020-06-16 14:30:08 -0400640 GrSurfaceProxy* proxy = this->target(0).proxy();
Greg Daniel16f5c652019-10-29 11:26:01 -0400641 SkASSERT(proxy);
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400642 fTotalBounds = proxy->backingStoreBoundsRect();
Chris Dalton16a33c62019-09-24 22:19:17 -0600643 }
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500644}
645
Greg Danielf41b2bd2019-08-22 16:19:24 -0400646bool GrOpsTask::resetForFullscreenClear(CanDiscardPreviousOps canDiscardPreviousOps) {
Chris Dalton6b982802019-06-27 13:53:46 -0600647 if (CanDiscardPreviousOps::kYes == canDiscardPreviousOps || this->isEmpty()) {
Robert Phillipsc994a932018-06-19 13:09:54 -0400648 this->deleteOps();
Brian Osman099fa0f2017-10-02 16:38:32 -0400649 fDeferredProxies.reset();
Greg Danielb20d7e52019-09-03 13:54:39 -0400650 fSampledProxies.reset();
Greg Daniel070cbaf2019-01-03 17:35:54 -0500651
Greg Danielf41b2bd2019-08-22 16:19:24 -0400652 // If the opsTask is using a render target which wraps a vulkan command buffer, we can't do
653 // a clear load since we cannot change the render pass that we are using. Thus we fall back
654 // to making a clear op in this case.
Adlai Holler33d569e2020-06-16 14:30:08 -0400655 return !this->target(0).asRenderTargetProxy()->wrapsVkSecondaryCB();
bsalomonfd8d0132016-08-11 11:25:33 -0700656 }
Robert Phillips380b90c2017-08-30 07:41:07 -0400657
Greg Danielf41b2bd2019-08-22 16:19:24 -0400658 // Could not empty the task, so an op must be added to handle the clear
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500659 return false;
bsalomon9f129de2016-08-10 16:31:05 -0700660}
661
Greg Danielf41b2bd2019-08-22 16:19:24 -0400662void GrOpsTask::discard() {
663 // Discard calls to in-progress opsTasks are ignored. Calls at the start update the
664 // opsTasks' color & stencil load ops.
665 if (this->isEmpty()) {
666 fColorLoadOp = GrLoadOp::kDiscard;
Chris Dalton674f77a2019-09-30 20:49:39 -0600667 fInitialStencilContent = StencilContent::kDontCare;
Chris Dalton16a33c62019-09-24 22:19:17 -0600668 fTotalBounds.setEmpty();
Greg Danielf41b2bd2019-08-22 16:19:24 -0400669 }
670}
671
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000672////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000673
John Stiles1e0136e2020-08-12 18:44:00 -0400674#if GR_TEST_UTILS
Greg Danielf41b2bd2019-08-22 16:19:24 -0400675void GrOpsTask::dump(bool printDependencies) const {
676 GrRenderTask::dump(printDependencies);
677
Chris Dalton674f77a2019-09-30 20:49:39 -0600678 SkDebugf("fColorLoadOp: ");
679 switch (fColorLoadOp) {
680 case GrLoadOp::kLoad:
681 SkDebugf("kLoad\n");
682 break;
683 case GrLoadOp::kClear:
684 SkDebugf("kClear (0x%x)\n", fLoadClearColor.toBytes_RGBA());
685 break;
686 case GrLoadOp::kDiscard:
687 SkDebugf("kDiscard\n");
688 break;
689 }
690
691 SkDebugf("fInitialStencilContent: ");
692 switch (fInitialStencilContent) {
693 case StencilContent::kDontCare:
694 SkDebugf("kDontCare\n");
695 break;
696 case StencilContent::kUserBitsCleared:
697 SkDebugf("kUserBitsCleared\n");
698 break;
699 case StencilContent::kPreserved:
700 SkDebugf("kPreserved\n");
701 break;
702 }
Greg Danielf41b2bd2019-08-22 16:19:24 -0400703
704 SkDebugf("ops (%d):\n", fOpChains.count());
705 for (int i = 0; i < fOpChains.count(); ++i) {
706 SkDebugf("*******************************\n");
707 if (!fOpChains[i].head()) {
708 SkDebugf("%d: <combined forward or failed instantiation>\n", i);
709 } else {
710 SkDebugf("%d: %s\n", i, fOpChains[i].head()->name());
711 SkRect bounds = fOpChains[i].bounds();
712 SkDebugf("ClippedBounds: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", bounds.fLeft,
713 bounds.fTop, bounds.fRight, bounds.fBottom);
714 for (const auto& op : GrOp::ChainRange<>(fOpChains[i].head())) {
715 SkString info = SkTabString(op.dumpInfo(), 1);
716 SkDebugf("%s\n", info.c_str());
717 bounds = op.bounds();
718 SkDebugf("\tClippedBounds: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", bounds.fLeft,
719 bounds.fTop, bounds.fRight, bounds.fBottom);
720 }
721 }
722 }
723}
John Stiles1e0136e2020-08-12 18:44:00 -0400724#endif
Greg Danielf41b2bd2019-08-22 16:19:24 -0400725
John Stiles1e0136e2020-08-12 18:44:00 -0400726#ifdef SK_DEBUG
Michael Ludwigfcdd0612019-11-25 08:34:31 -0500727void GrOpsTask::visitProxies_debugOnly(const GrOp::VisitProxyFunc& func) const {
Brian Salomon7e67dca2020-07-21 09:27:25 -0400728 auto textureFunc = [ func ] (GrSurfaceProxy* tex, GrMipmapped mipmapped) {
Greg Danieldcf9ca12019-08-27 14:30:21 -0400729 func(tex, mipmapped);
730 };
731
Greg Danielf41b2bd2019-08-22 16:19:24 -0400732 for (const OpChain& chain : fOpChains) {
Greg Danieldcf9ca12019-08-27 14:30:21 -0400733 chain.visitProxies(textureFunc);
Greg Danielf41b2bd2019-08-22 16:19:24 -0400734 }
735}
736
737#endif
738
739////////////////////////////////////////////////////////////////////////////////
740
741bool GrOpsTask::onIsUsed(GrSurfaceProxy* proxyToCheck) const {
742 bool used = false;
743
Brian Salomon7e67dca2020-07-21 09:27:25 -0400744 auto visit = [ proxyToCheck, &used ] (GrSurfaceProxy* p, GrMipmapped) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400745 if (p == proxyToCheck) {
746 used = true;
747 }
748 };
749 for (const OpChain& recordedOp : fOpChains) {
750 recordedOp.visitProxies(visit);
751 }
752
753 return used;
754}
755
756void GrOpsTask::handleInternalAllocationFailure() {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500757 bool hasUninstantiatedProxy = false;
Brian Salomon7e67dca2020-07-21 09:27:25 -0400758 auto checkInstantiation = [&hasUninstantiatedProxy](GrSurfaceProxy* p, GrMipmapped) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400759 if (!p->isInstantiated()) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500760 hasUninstantiatedProxy = true;
761 }
762 };
Brian Salomon588cec72018-11-14 13:56:37 -0500763 for (OpChain& recordedOp : fOpChains) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500764 hasUninstantiatedProxy = false;
Chris Dalton1706cbf2019-05-21 19:35:29 -0600765 recordedOp.visitProxies(checkInstantiation);
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500766 if (hasUninstantiatedProxy) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400767 recordedOp.setSkipExecuteFlag();
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500768 }
769 }
770}
771
Greg Danielf41b2bd2019-08-22 16:19:24 -0400772void GrOpsTask::gatherProxyIntervals(GrResourceAllocator* alloc) const {
Robert Phillips51b20f22017-12-01 15:32:35 -0500773 for (int i = 0; i < fDeferredProxies.count(); ++i) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400774 SkASSERT(!fDeferredProxies[i]->isInstantiated());
Robert Phillips51b20f22017-12-01 15:32:35 -0500775 // We give all the deferred proxies a write usage at the very start of flushing. This
776 // locks them out of being reused for the entire flush until they are read - and then
777 // they can be recycled. This is a bit unfortunate because a flush can proceed in waves
778 // with sub-flushes. The deferred proxies only need to be pinned from the start of
779 // the sub-flush in which they appear.
Robert Phillipsc73666f2019-04-24 08:49:48 -0400780 alloc->addInterval(fDeferredProxies[i], 0, 0, GrResourceAllocator::ActualUse::kNo);
Robert Phillips51b20f22017-12-01 15:32:35 -0500781 }
782
Adlai Holler33d569e2020-06-16 14:30:08 -0400783 GrSurfaceProxy* targetProxy = this->target(0).proxy();
Greg Daniel16f5c652019-10-29 11:26:01 -0400784
Greg Danielf41b2bd2019-08-22 16:19:24 -0400785 // Add the interval for all the writes to this GrOpsTasks's target
Brian Salomon588cec72018-11-14 13:56:37 -0500786 if (fOpChains.count()) {
Robert Phillips3bf3d4a2019-03-27 07:09:09 -0400787 unsigned int cur = alloc->curOp();
788
Greg Daniel16f5c652019-10-29 11:26:01 -0400789 alloc->addInterval(targetProxy, cur, cur + fOpChains.count() - 1,
Robert Phillipsc73666f2019-04-24 08:49:48 -0400790 GrResourceAllocator::ActualUse::kYes);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500791 } else {
792 // This can happen if there is a loadOp (e.g., a clear) but no other draws. In this case we
793 // still need to add an interval for the destination so we create a fake op# for
794 // the missing clear op.
Greg Daniel16f5c652019-10-29 11:26:01 -0400795 alloc->addInterval(targetProxy, alloc->curOp(), alloc->curOp(),
Robert Phillipsc73666f2019-04-24 08:49:48 -0400796 GrResourceAllocator::ActualUse::kYes);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500797 alloc->incOps();
798 }
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400799
Brian Salomon7e67dca2020-07-21 09:27:25 -0400800 auto gather = [ alloc SkDEBUGCODE(, this) ] (GrSurfaceProxy* p, GrMipmapped) {
Robert Phillipsc73666f2019-04-24 08:49:48 -0400801 alloc->addInterval(p, alloc->curOp(), alloc->curOp(), GrResourceAllocator::ActualUse::kYes
Adlai Holler33d569e2020-06-16 14:30:08 -0400802 SkDEBUGCODE(, this->target(0).proxy() == p));
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400803 };
Brian Salomon588cec72018-11-14 13:56:37 -0500804 for (const OpChain& recordedOp : fOpChains) {
Chris Dalton1706cbf2019-05-21 19:35:29 -0600805 recordedOp.visitProxies(gather);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500806
Robert Phillips3bf3d4a2019-03-27 07:09:09 -0400807 // 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 -0500808 // keep all the math consistent.
809 alloc->incOps();
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400810 }
811}
812
Greg Danielf41b2bd2019-08-22 16:19:24 -0400813void GrOpsTask::recordOp(
Chris Dalton945ee652019-01-23 09:10:36 -0700814 std::unique_ptr<GrOp> op, GrProcessorSet::Analysis processorAnalysis, GrAppliedClip* clip,
Greg Daniel524e28b2019-11-01 11:48:53 -0400815 const DstProxyView* dstProxyView, const GrCaps& caps) {
Ethan Nicholas029b22c2018-10-18 16:49:56 -0400816 SkDEBUGCODE(op->validate();)
Greg Daniel524e28b2019-11-01 11:48:53 -0400817 SkASSERT(processorAnalysis.requiresDstTexture() == (dstProxyView && dstProxyView->proxy()));
Adlai Holler33d569e2020-06-16 14:30:08 -0400818 GrSurfaceProxy* proxy = this->target(0).proxy();
Greg Daniel16f5c652019-10-29 11:26:01 -0400819 SkASSERT(proxy);
Robert Phillipsee683652017-04-26 11:53:10 -0400820
Greg Danielf41b2bd2019-08-22 16:19:24 -0400821 // A closed GrOpsTask should never receive new/more ops
robertphillips6a186652015-10-20 07:37:58 -0700822 SkASSERT(!this->isClosed());
Brian Salomon19ec80f2018-11-16 13:27:30 -0500823 if (!op->bounds().isFinite()) {
Herb Derbyd6cfe722020-10-05 15:50:47 -0400824 #if !defined(GR_OP_ALLOCATE_USE_NEW)
825 fArenas.opMemoryPool()->release(std::move(op));
826 #endif
Brian Salomon19ec80f2018-11-16 13:27:30 -0500827 return;
828 }
robertphillipsa106c622015-10-16 09:07:06 -0700829
Chris Dalton16a33c62019-09-24 22:19:17 -0600830 // Account for this op's bounds before we attempt to combine.
831 // NOTE: The caller should have already called "op->setClippedBounds()" by now, if applicable.
832 fTotalBounds.join(op->bounds());
833
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500834 // Check if there is an op we can combine with by linearly searching back until we either
835 // 1) check every op
bsalomon512be532015-09-10 10:42:55 -0700836 // 2) intersect with something
837 // 3) find a 'blocker'
Greg Daniel16f5c652019-10-29 11:26:01 -0400838 GR_AUDIT_TRAIL_ADD_OP(fAuditTrail, op.get(), proxy->uniqueID());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400839 GrOP_INFO("opsTask: %d Recording (%s, opID: %u)\n"
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400840 "\tBounds [L: %.2f, T: %.2f R: %.2f B: %.2f]\n",
841 this->uniqueID(),
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500842 op->name(),
843 op->uniqueID(),
Robert Phillips1119dc32017-04-11 12:54:57 -0400844 op->bounds().fLeft, op->bounds().fTop,
845 op->bounds().fRight, op->bounds().fBottom);
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500846 GrOP_INFO(SkTabString(op->dumpInfo(), 1).c_str());
Brian Salomon25a88092016-12-01 09:36:50 -0500847 GrOP_INFO("\tOutcome:\n");
Brian Osman788b9162020-02-07 10:36:46 -0500848 int maxCandidates = std::min(kMaxOpChainDistance, fOpChains.count());
Robert Phillips318c4192017-05-17 09:36:38 -0400849 if (maxCandidates) {
bsalomon512be532015-09-10 10:42:55 -0700850 int i = 0;
851 while (true) {
Brian Salomon588cec72018-11-14 13:56:37 -0500852 OpChain& candidate = fOpChains.fromBack(i);
Greg Daniel524e28b2019-11-01 11:48:53 -0400853 op = candidate.appendOp(std::move(op), processorAnalysis, dstProxyView, clip, caps,
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500854 &fArenas, fAuditTrail);
Brian Salomon588cec72018-11-14 13:56:37 -0500855 if (!op) {
856 return;
bsalomon512be532015-09-10 10:42:55 -0700857 }
Brian Salomona7682c82018-10-24 10:04:37 -0400858 // Stop going backwards if we would cause a painter's order violation.
Brian Salomon588cec72018-11-14 13:56:37 -0500859 if (!can_reorder(candidate.bounds(), op->bounds())) {
860 GrOP_INFO("\t\tBackward: Intersects with chain (%s, head opID: %u)\n",
861 candidate.head()->name(), candidate.head()->uniqueID());
bsalomon512be532015-09-10 10:42:55 -0700862 break;
863 }
Brian Salomon588cec72018-11-14 13:56:37 -0500864 if (++i == maxCandidates) {
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400865 GrOP_INFO("\t\tBackward: Reached max lookback or beginning of op array %d\n", i);
bsalomon512be532015-09-10 10:42:55 -0700866 break;
867 }
868 }
869 } else {
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400870 GrOP_INFO("\t\tBackward: FirstOp\n");
bsalomon512be532015-09-10 10:42:55 -0700871 }
Brian Salomon54d212e2017-03-21 14:22:38 -0400872 if (clip) {
873 clip = fClipAllocator.make<GrAppliedClip>(std::move(*clip));
Robert Phillipsc84c0302017-05-08 15:35:11 -0400874 SkDEBUGCODE(fNumClips++;)
Brian Salomon54d212e2017-03-21 14:22:38 -0400875 }
Greg Daniel524e28b2019-11-01 11:48:53 -0400876 fOpChains.emplace_back(std::move(op), processorAnalysis, clip, dstProxyView);
bsalomon512be532015-09-10 10:42:55 -0700877}
878
Greg Danielf41b2bd2019-08-22 16:19:24 -0400879void GrOpsTask::forwardCombine(const GrCaps& caps) {
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400880 SkASSERT(!this->isClosed());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400881 GrOP_INFO("opsTask: %d ForwardCombine %d ops:\n", this->uniqueID(), fOpChains.count());
Robert Phillips48567ac2017-06-01 08:46:00 -0400882
Brian Salomon588cec72018-11-14 13:56:37 -0500883 for (int i = 0; i < fOpChains.count() - 1; ++i) {
884 OpChain& chain = fOpChains[i];
Brian Osman788b9162020-02-07 10:36:46 -0500885 int maxCandidateIdx = std::min(i + kMaxOpChainDistance, fOpChains.count() - 1);
bsalomonaecc0182016-03-07 11:50:44 -0800886 int j = i + 1;
887 while (true) {
Brian Salomon588cec72018-11-14 13:56:37 -0500888 OpChain& candidate = fOpChains[j];
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500889 if (candidate.prependChain(&chain, caps, &fArenas, fAuditTrail)) {
bsalomonaecc0182016-03-07 11:50:44 -0800890 break;
891 }
Robert Phillipsc84c0302017-05-08 15:35:11 -0400892 // Stop traversing if we would cause a painter's order violation.
Brian Salomon588cec72018-11-14 13:56:37 -0500893 if (!can_reorder(chain.bounds(), candidate.bounds())) {
894 GrOP_INFO(
895 "\t\t%d: chain (%s head opID: %u) -> "
896 "Intersects with chain (%s, head opID: %u)\n",
897 i, chain.head()->name(), chain.head()->uniqueID(), candidate.head()->name(),
898 candidate.head()->uniqueID());
bsalomonaecc0182016-03-07 11:50:44 -0800899 break;
900 }
Brian Salomona7682c82018-10-24 10:04:37 -0400901 if (++j > maxCandidateIdx) {
Brian Salomon588cec72018-11-14 13:56:37 -0500902 GrOP_INFO("\t\t%d: chain (%s opID: %u) -> Reached max lookahead or end of array\n",
903 i, chain.head()->name(), chain.head()->uniqueID());
bsalomonaecc0182016-03-07 11:50:44 -0800904 break;
905 }
906 }
907 }
908}
909
Chris Dalton16a33c62019-09-24 22:19:17 -0600910GrRenderTask::ExpectedOutcome GrOpsTask::onMakeClosed(
911 const GrCaps& caps, SkIRect* targetUpdateBounds) {
912 this->forwardCombine(caps);
Brian Salomonf1c9eae2020-05-01 15:00:34 -0400913 SkScopeExit triggerObservers([&] {
914 for (const auto& o : fClosedObservers) {
915 o->wasClosed(*this);
Brian Salomon3b8486a2020-04-21 12:43:26 -0400916 }
Brian Salomonf1c9eae2020-05-01 15:00:34 -0400917 fClosedObservers.reset();
Brian Salomon3b8486a2020-04-21 12:43:26 -0400918 });
Chris Dalton16a33c62019-09-24 22:19:17 -0600919 if (!this->isNoOp()) {
Adlai Holler33d569e2020-06-16 14:30:08 -0400920 GrSurfaceProxy* proxy = this->target(0).proxy();
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400921 // Use the entire backing store bounds since the GPU doesn't clip automatically to the
922 // logical dimensions.
923 SkRect clippedContentBounds = proxy->backingStoreBoundsRect();
Adlai Holler33d569e2020-06-16 14:30:08 -0400924 // TODO: If we can fix up GLPrograms test to always intersect the target proxy bounds
Greg Daniel16f5c652019-10-29 11:26:01 -0400925 // then we can simply assert here that the bounds intersect.
Chris Dalton16a33c62019-09-24 22:19:17 -0600926 if (clippedContentBounds.intersect(fTotalBounds)) {
Greg Daniel94ed83f2019-09-27 13:05:43 -0400927 clippedContentBounds.roundOut(&fClippedContentBounds);
928 *targetUpdateBounds = fClippedContentBounds;
Chris Dalton16a33c62019-09-24 22:19:17 -0600929 return ExpectedOutcome::kTargetDirty;
930 }
931 }
932 return ExpectedOutcome::kTargetUnchanged;
933}