blob: 63eb2900751fe3bfc9a2be6862ddc9b0e495ab45 [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
Herb Derbyc76d4092020-10-07 16:46:15 -040049inline GrOpsTask::OpChain::List::List(GrOp::Owner 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
Herb Derbyc76d4092020-10-07 16:46:15 -040064inline GrOp::Owner 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
Herb Derbyc76d4092020-10-07 16:46:15 -040075inline GrOp::Owner 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
Herb Derbyc76d4092020-10-07 16:46:15 -040097inline void GrOpsTask::OpChain::List::pushHead(GrOp::Owner 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
Herb Derbyc76d4092020-10-07 16:46:15 -0400110inline void GrOpsTask::OpChain::List::pushTail(GrOp::Owner 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
Herb Derbyc76d4092020-10-07 16:46:15 -0400127GrOpsTask::OpChain::OpChain(GrOp::Owner op,
Greg Danielf41b2bd2019-08-22 16:19:24 -0400128 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
Herb Derbye32e1ab2020-10-27 10:29:46 -0400155void GrOpsTask::OpChain::deleteOps() {
Brian Salomon588cec72018-11-14 13:56:37 -0500156 while (!fList.empty()) {
Herb Derbyc76d4092020-10-07 16:46:15 -0400157 // Since the value goes out of scope immediately, the GrOp::Owner deletes the op.
158 fList.popHead();
Brian Salomon588cec72018-11-14 13:56:37 -0500159 }
160}
161
162// Concatenates two op chains and attempts to merge ops across the chains. Assumes that we know that
163// the two chains are chainable. Returns the new chain.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400164GrOpsTask::OpChain::List GrOpsTask::OpChain::DoConcat(
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500165 List chainA, List chainB, const GrCaps& caps, GrRecordingContext::Arenas* arenas,
166 GrAuditTrail* auditTrail) {
Brian Salomon588cec72018-11-14 13:56:37 -0500167 // We process ops in chain b from head to tail. We attempt to merge with nodes in a, starting
168 // at chain a's tail and working toward the head. We produce one of the following outcomes:
169 // 1) b's head is merged into an op in a.
170 // 2) An op from chain a is merged into b's head. (In this case b's head gets processed again.)
171 // 3) b's head is popped from chain a and added at the tail of a.
172 // After result 3 we don't want to attempt to merge the next head of b with the new tail of a,
173 // as we assume merges were already attempted when chain b was created. So we keep track of the
174 // original tail of a and start our iteration of a there. We also track the bounds of the nodes
175 // appended to chain a that will be skipped for bounds testing. If the original tail of a is
176 // merged into an op in b (case 2) then we advance the "original tail" towards the head of a.
177 GrOp* origATail = chainA.tail();
178 SkRect skipBounds = SkRectPriv::MakeLargestInverted();
179 do {
180 int numMergeChecks = 0;
181 bool merged = false;
182 bool noSkip = (origATail == chainA.tail());
183 SkASSERT(noSkip == (skipBounds == SkRectPriv::MakeLargestInverted()));
184 bool canBackwardMerge = noSkip || can_reorder(chainB.head()->bounds(), skipBounds);
185 SkRect forwardMergeBounds = skipBounds;
186 GrOp* a = origATail;
187 while (a) {
188 bool canForwardMerge =
189 (a == chainA.tail()) || can_reorder(a->bounds(), forwardMergeBounds);
190 if (canForwardMerge || canBackwardMerge) {
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500191 auto result = a->combineIfPossible(chainB.head(), arenas, caps);
Brian Salomon588cec72018-11-14 13:56:37 -0500192 SkASSERT(result != GrOp::CombineResult::kCannotCombine);
193 merged = (result == GrOp::CombineResult::kMerged);
Robert Phillips9548c3b422019-01-08 12:35:43 -0500194 GrOP_INFO("\t\t: (%s opID: %u) -> Combining with (%s, opID: %u)\n",
Brian Salomon588cec72018-11-14 13:56:37 -0500195 chainB.head()->name(), chainB.head()->uniqueID(), a->name(),
196 a->uniqueID());
Brian Salomon588cec72018-11-14 13:56:37 -0500197 }
198 if (merged) {
Brian Salomon52a6ed32018-11-26 10:30:58 -0500199 GR_AUDIT_TRAIL_OPS_RESULT_COMBINED(auditTrail, a, chainB.head());
Brian Salomon588cec72018-11-14 13:56:37 -0500200 if (canBackwardMerge) {
Herb Derbyc76d4092020-10-07 16:46:15 -0400201 // The GrOp::Owner releases the op.
202 chainB.popHead();
Brian Salomon588cec72018-11-14 13:56:37 -0500203 } else {
204 // We merged the contents of b's head into a. We will replace b's head with a in
205 // chain b.
206 SkASSERT(canForwardMerge);
207 if (a == origATail) {
208 origATail = a->prevInChain();
209 }
Herb Derbyc76d4092020-10-07 16:46:15 -0400210 GrOp::Owner detachedA = chainA.removeOp(a);
211 // The GrOp::Owner releases the op.
212 chainB.popHead();
Brian Salomon588cec72018-11-14 13:56:37 -0500213 chainB.pushHead(std::move(detachedA));
214 if (chainA.empty()) {
215 // We merged all the nodes in chain a to chain b.
216 return chainB;
217 }
218 }
219 break;
220 } else {
221 if (++numMergeChecks == kMaxOpMergeDistance) {
222 break;
223 }
224 forwardMergeBounds.joinNonEmptyArg(a->bounds());
225 canBackwardMerge =
226 canBackwardMerge && can_reorder(chainB.head()->bounds(), a->bounds());
227 a = a->prevInChain();
228 }
229 }
230 // If we weren't able to merge b's head then pop b's head from chain b and make it the new
231 // tail of a.
232 if (!merged) {
233 chainA.pushTail(chainB.popHead());
234 skipBounds.joinNonEmptyArg(chainA.tail()->bounds());
235 }
236 } while (!chainB.empty());
237 return chainA;
238}
239
Chris Dalton945ee652019-01-23 09:10:36 -0700240// Attempts to concatenate the given chain onto our own and merge ops across the chains. Returns
241// whether the operation succeeded. On success, the provided list will be returned empty.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400242bool GrOpsTask::OpChain::tryConcat(
Greg Daniel524e28b2019-11-01 11:48:53 -0400243 List* list, GrProcessorSet::Analysis processorAnalysis, const DstProxyView& dstProxyView,
Chris Dalton945ee652019-01-23 09:10:36 -0700244 const GrAppliedClip* appliedClip, const SkRect& bounds, const GrCaps& caps,
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500245 GrRecordingContext::Arenas* arenas, GrAuditTrail* auditTrail) {
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700246 SkASSERT(!fList.empty());
247 SkASSERT(!list->empty());
Greg Daniel524e28b2019-11-01 11:48:53 -0400248 SkASSERT(fProcessorAnalysis.requiresDstTexture() == SkToBool(fDstProxyView.proxy()));
249 SkASSERT(processorAnalysis.requiresDstTexture() == SkToBool(dstProxyView.proxy()));
Brian Salomon588cec72018-11-14 13:56:37 -0500250 // All returns use explicit tuple constructor rather than {a, b} to work around old GCC bug.
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700251 if (fList.head()->classID() != list->head()->classID() ||
252 SkToBool(fAppliedClip) != SkToBool(appliedClip) ||
253 (fAppliedClip && *fAppliedClip != *appliedClip) ||
Chris Dalton945ee652019-01-23 09:10:36 -0700254 (fProcessorAnalysis.requiresNonOverlappingDraws() !=
255 processorAnalysis.requiresNonOverlappingDraws()) ||
256 (fProcessorAnalysis.requiresNonOverlappingDraws() &&
257 // Non-overlaping draws are only required when Ganesh will either insert a barrier,
258 // or read back a new dst texture between draws. In either case, we can neither
259 // chain nor combine overlapping Ops.
260 GrRectsTouchOrOverlap(fBounds, bounds)) ||
261 (fProcessorAnalysis.requiresDstTexture() != processorAnalysis.requiresDstTexture()) ||
Greg Daniel524e28b2019-11-01 11:48:53 -0400262 (fProcessorAnalysis.requiresDstTexture() && fDstProxyView != dstProxyView)) {
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700263 return false;
Brian Salomon588cec72018-11-14 13:56:37 -0500264 }
Chris Daltonee21e6b2019-01-22 14:04:43 -0700265
Brian Salomon588cec72018-11-14 13:56:37 -0500266 SkDEBUGCODE(bool first = true;)
267 do {
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500268 switch (fList.tail()->combineIfPossible(list->head(), arenas, caps)) {
Brian Salomon588cec72018-11-14 13:56:37 -0500269 case GrOp::CombineResult::kCannotCombine:
270 // If an op supports chaining then it is required that chaining is transitive and
271 // that if any two ops in two different chains can merge then the two chains
272 // may also be chained together. Thus, we should only hit this on the first
273 // iteration.
274 SkASSERT(first);
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700275 return false;
Brian Salomon588cec72018-11-14 13:56:37 -0500276 case GrOp::CombineResult::kMayChain:
Adlai Holler5ba50af2020-04-29 21:11:14 -0400277 fList = DoConcat(std::move(fList), std::exchange(*list, List()), caps, arenas,
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500278 auditTrail);
Chris Daltonee21e6b2019-01-22 14:04:43 -0700279 // The above exchange cleared out 'list'. The list needs to be empty now for the
280 // loop to terminate.
281 SkASSERT(list->empty());
282 break;
Brian Salomon588cec72018-11-14 13:56:37 -0500283 case GrOp::CombineResult::kMerged: {
Robert Phillips9548c3b422019-01-08 12:35:43 -0500284 GrOP_INFO("\t\t: (%s opID: %u) -> Combining with (%s, opID: %u)\n",
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700285 list->tail()->name(), list->tail()->uniqueID(), list->head()->name(),
286 list->head()->uniqueID());
287 GR_AUDIT_TRAIL_OPS_RESULT_COMBINED(auditTrail, fList.tail(), list->head());
Herb Derbyc76d4092020-10-07 16:46:15 -0400288 // The GrOp::Owner releases the op.
289 list->popHead();
Brian Salomon588cec72018-11-14 13:56:37 -0500290 break;
291 }
292 }
293 SkDEBUGCODE(first = false);
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700294 } while (!list->empty());
Chris Daltonee21e6b2019-01-22 14:04:43 -0700295
296 // The new ops were successfully merged and/or chained onto our own.
297 fBounds.joinPossiblyEmptyRect(bounds);
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700298 return true;
Brian Salomon588cec72018-11-14 13:56:37 -0500299}
300
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500301bool GrOpsTask::OpChain::prependChain(OpChain* that, const GrCaps& caps,
302 GrRecordingContext::Arenas* arenas,
303 GrAuditTrail* auditTrail) {
Greg Daniel524e28b2019-11-01 11:48:53 -0400304 if (!that->tryConcat(&fList, fProcessorAnalysis, fDstProxyView, fAppliedClip, fBounds, caps,
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500305 arenas, auditTrail)) {
Brian Salomon588cec72018-11-14 13:56:37 -0500306 this->validate();
307 // append failed
308 return false;
309 }
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700310
Brian Salomon588cec72018-11-14 13:56:37 -0500311 // 'that' owns the combined chain. Move it into 'this'.
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700312 SkASSERT(fList.empty());
Brian Salomon588cec72018-11-14 13:56:37 -0500313 fList = std::move(that->fList);
Chris Daltonee21e6b2019-01-22 14:04:43 -0700314 fBounds = that->fBounds;
Brian Salomon588cec72018-11-14 13:56:37 -0500315
Greg Daniel524e28b2019-11-01 11:48:53 -0400316 that->fDstProxyView.setProxyView({});
John Stiles59e18dc2020-07-22 18:18:12 -0400317 if (that->fAppliedClip && that->fAppliedClip->hasCoverageFragmentProcessor()) {
318 // Obliterates the processor.
319 that->fAppliedClip->detachCoverageFragmentProcessor();
Brian Salomon588cec72018-11-14 13:56:37 -0500320 }
321 this->validate();
322 return true;
323}
324
Herb Derbyc76d4092020-10-07 16:46:15 -0400325GrOp::Owner GrOpsTask::OpChain::appendOp(
326 GrOp::Owner op, GrProcessorSet::Analysis processorAnalysis,
Greg Daniel524e28b2019-11-01 11:48:53 -0400327 const DstProxyView* dstProxyView, const GrAppliedClip* appliedClip, const GrCaps& caps,
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500328 GrRecordingContext::Arenas* arenas, GrAuditTrail* auditTrail) {
Greg Daniel524e28b2019-11-01 11:48:53 -0400329 const GrXferProcessor::DstProxyView noDstProxyView;
330 if (!dstProxyView) {
331 dstProxyView = &noDstProxyView;
Brian Salomon588cec72018-11-14 13:56:37 -0500332 }
333 SkASSERT(op->isChainHead() && op->isChainTail());
334 SkRect opBounds = op->bounds();
335 List chain(std::move(op));
Chris Dalton945ee652019-01-23 09:10:36 -0700336 if (!this->tryConcat(
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500337 &chain, processorAnalysis, *dstProxyView, appliedClip, opBounds, caps,
338 arenas, auditTrail)) {
Brian Salomon588cec72018-11-14 13:56:37 -0500339 // append failed, give the op back to the caller.
340 this->validate();
341 return chain.popHead();
342 }
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700343
344 SkASSERT(chain.empty());
Brian Salomon588cec72018-11-14 13:56:37 -0500345 this->validate();
346 return nullptr;
347}
348
Greg Danielf41b2bd2019-08-22 16:19:24 -0400349inline void GrOpsTask::OpChain::validate() const {
Brian Salomon588cec72018-11-14 13:56:37 -0500350#ifdef SK_DEBUG
351 fList.validate();
352 for (const auto& op : GrOp::ChainRange<>(fList.head())) {
353 // Not using SkRect::contains because we allow empty rects.
354 SkASSERT(fBounds.fLeft <= op.bounds().fLeft && fBounds.fTop <= op.bounds().fTop &&
355 fBounds.fRight >= op.bounds().fRight && fBounds.fBottom >= op.bounds().fBottom);
356 }
357#endif
358}
359
360////////////////////////////////////////////////////////////////////////////////
bsalomon489147c2015-12-14 12:13:09 -0800361
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400362GrOpsTask::GrOpsTask(GrDrawingManager* drawingMgr, GrRecordingContext::Arenas arenas,
Greg Daniel16f5c652019-10-29 11:26:01 -0400363 GrSurfaceProxyView view,
Greg Danielf41b2bd2019-08-22 16:19:24 -0400364 GrAuditTrail* auditTrail)
Adlai Holler33d569e2020-06-16 14:30:08 -0400365 : GrRenderTask()
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500366 , fArenas(arenas)
Greg Danielf41b2bd2019-08-22 16:19:24 -0400367 , fAuditTrail(auditTrail)
Robert Phillipsb6deea82017-05-11 14:14:30 -0400368 SkDEBUGCODE(, fNumClips(0)) {
Adlai Holler33d569e2020-06-16 14:30:08 -0400369 this->addTarget(drawingMgr, std::move(view));
bsalomon4061b122015-05-29 10:26:19 -0700370}
371
Greg Danielf41b2bd2019-08-22 16:19:24 -0400372void GrOpsTask::deleteOps() {
Brian Salomon588cec72018-11-14 13:56:37 -0500373 for (auto& chain : fOpChains) {
Herb Derbye32e1ab2020-10-27 10:29:46 -0400374 chain.deleteOps();
Robert Phillipsc994a932018-06-19 13:09:54 -0400375 }
Brian Salomon588cec72018-11-14 13:56:37 -0500376 fOpChains.reset();
Robert Phillipsc994a932018-06-19 13:09:54 -0400377}
378
Greg Danielf41b2bd2019-08-22 16:19:24 -0400379GrOpsTask::~GrOpsTask() {
Robert Phillipsc994a932018-06-19 13:09:54 -0400380 this->deleteOps();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000381}
382
Brian Salomonf1c9eae2020-05-01 15:00:34 -0400383void GrOpsTask::removeClosedObserver(GrOpsTaskClosedObserver* observer) {
384 SkASSERT(observer);
385 for (int i = 0; i < fClosedObservers.count(); ++i) {
386 if (fClosedObservers[i] == observer) {
387 fClosedObservers.removeShuffle(i);
388 --i;
389 }
390 }
391}
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000392
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400393void GrOpsTask::endFlush(GrDrawingManager* drawingMgr) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400394 fLastClipStackGenID = SK_InvalidUniqueID;
395 this->deleteOps();
396 fClipAllocator.reset();
Chris Dalton706a6ff2017-11-29 22:01:06 -0700397
Greg Danielf41b2bd2019-08-22 16:19:24 -0400398 fDeferredProxies.reset();
Greg Danielb20d7e52019-09-03 13:54:39 -0400399 fSampledProxies.reset();
Greg Danielf41b2bd2019-08-22 16:19:24 -0400400 fAuditTrail = nullptr;
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400401
402 GrRenderTask::endFlush(drawingMgr);
Greg Danielf21bf9e2019-08-22 20:12:20 +0000403}
404
Robert Phillips29f38542019-10-16 09:20:25 -0400405void GrOpsTask::onPrePrepare(GrRecordingContext* context) {
Robert Phillips7327c9d2019-10-08 16:32:56 -0400406 SkASSERT(this->isClosed());
407#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
408 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
409#endif
410 // TODO: remove the check for discard here once reduced op splitting is turned on. Currently we
411 // can end up with GrOpsTasks that only have a discard load op and no ops. For vulkan validation
412 // we need to keep that discard and not drop it. Once we have reduce op list splitting enabled
413 // we shouldn't end up with GrOpsTasks with only discard.
414 if (this->isNoOp() || (fClippedContentBounds.isEmpty() && fColorLoadOp != GrLoadOp::kDiscard)) {
415 return;
416 }
417
418 for (const auto& chain : fOpChains) {
419 if (chain.shouldExecute()) {
Robert Phillips8053c972019-11-21 10:44:53 -0500420 chain.head()->prePrepare(context,
Adlai Holler33d569e2020-06-16 14:30:08 -0400421 &fTargets[0],
Robert Phillips8053c972019-11-21 10:44:53 -0500422 chain.appliedClip(),
Greg Danield358cbe2020-09-11 09:33:54 -0400423 chain.dstProxyView(),
424 fRenderPassXferBarriers);
Robert Phillips7327c9d2019-10-08 16:32:56 -0400425 }
426 }
427}
428
Greg Danielf41b2bd2019-08-22 16:19:24 -0400429void GrOpsTask::onPrepare(GrOpFlushState* flushState) {
Adlai Holler33d569e2020-06-16 14:30:08 -0400430 SkASSERT(this->target(0).proxy()->peekRenderTarget());
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400431 SkASSERT(this->isClosed());
Stan Iliev2af578d2017-08-16 13:00:28 -0400432#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
Brian Salomon5f394272019-07-02 14:07:49 -0400433 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Stan Iliev2af578d2017-08-16 13:00:28 -0400434#endif
Greg Daniel94ed83f2019-09-27 13:05:43 -0400435 // TODO: remove the check for discard here once reduced op splitting is turned on. Currently we
436 // can end up with GrOpsTasks that only have a discard load op and no ops. For vulkan validation
437 // we need to keep that discard and not drop it. Once we have reduce op list splitting enabled
438 // we shouldn't end up with GrOpsTasks with only discard.
439 if (this->isNoOp() || (fClippedContentBounds.isEmpty() && fColorLoadOp != GrLoadOp::kDiscard)) {
440 return;
441 }
robertphillipsa106c622015-10-16 09:07:06 -0700442
Greg Danielb20d7e52019-09-03 13:54:39 -0400443 flushState->setSampledProxyArray(&fSampledProxies);
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500444 // Loop over the ops that haven't yet been prepared.
Brian Salomon588cec72018-11-14 13:56:37 -0500445 for (const auto& chain : fOpChains) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400446 if (chain.shouldExecute()) {
Stan Iliev2af578d2017-08-16 13:00:28 -0400447#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
Brian Salomon5f394272019-07-02 14:07:49 -0400448 TRACE_EVENT0("skia.gpu", chain.head()->name());
Stan Iliev2af578d2017-08-16 13:00:28 -0400449#endif
Robert Phillips901aff02019-10-08 12:32:56 -0400450 GrOpFlushState::OpArgs opArgs(chain.head(),
Adlai Holler33d569e2020-06-16 14:30:08 -0400451 &fTargets[0],
Robert Phillips901aff02019-10-08 12:32:56 -0400452 chain.appliedClip(),
Greg Danield358cbe2020-09-11 09:33:54 -0400453 chain.dstProxyView(),
454 fRenderPassXferBarriers);
Robert Phillips405413f2019-10-04 10:39:28 -0400455
Brian Salomon29b60c92017-10-31 14:42:10 -0400456 flushState->setOpArgs(&opArgs);
Robert Phillipsdf70f152019-11-15 14:57:05 -0500457
458 // Temporary debugging helper: for debugging prePrepare w/o going through DDLs
459 // Delete once most of the GrOps have an onPrePrepare.
Adlai Holler33d569e2020-06-16 14:30:08 -0400460 // chain.head()->prePrepare(flushState->gpu()->getContext(), &this->target(0),
Robert Phillipsdf70f152019-11-15 14:57:05 -0500461 // chain.appliedClip());
462
Robert Phillips7327c9d2019-10-08 16:32:56 -0400463 // GrOp::prePrepare may or may not have been called at this point
Brian Salomon588cec72018-11-14 13:56:37 -0500464 chain.head()->prepare(flushState);
Brian Salomon29b60c92017-10-31 14:42:10 -0400465 flushState->setOpArgs(nullptr);
bsalomonaecc0182016-03-07 11:50:44 -0800466 }
bsalomon512be532015-09-10 10:42:55 -0700467 }
Greg Danielb20d7e52019-09-03 13:54:39 -0400468 flushState->setSampledProxyArray(nullptr);
robertphillipsa13e2022015-11-11 12:01:09 -0800469}
bsalomon512be532015-09-10 10:42:55 -0700470
Greg Danielc0d69152020-10-08 14:59:00 -0400471static GrOpsRenderPass* create_render_pass(GrGpu* gpu,
472 GrRenderTarget* rt,
473 GrAttachment* stencil,
474 GrSurfaceOrigin origin,
475 const SkIRect& bounds,
476 GrLoadOp colorLoadOp,
477 const SkPMColor4f& loadClearColor,
478 GrLoadOp stencilLoadOp,
479 GrStoreOp stencilStoreOp,
480 const SkTArray<GrSurfaceProxy*, true>& sampledProxies,
481 GrXferBarrierFlags renderPassXferBarriers) {
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400482 const GrOpsRenderPass::LoadAndStoreInfo kColorLoadStoreInfo {
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400483 colorLoadOp,
484 GrStoreOp::kStore,
485 loadClearColor
Robert Phillips178ce3e2017-04-13 09:15:47 -0400486 };
487
Robert Phillips95214472017-08-08 18:00:03 -0400488 // TODO:
489 // We would like to (at this level) only ever clear & discard. We would need
Greg Danielf41b2bd2019-08-22 16:19:24 -0400490 // to stop splitting up higher level OpsTasks for copyOps to achieve that.
Robert Phillips95214472017-08-08 18:00:03 -0400491 // Note: we would still need SB loads and stores but they would happen at a
492 // lower level (inside the VK command buffer).
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400493 const GrOpsRenderPass::StencilLoadAndStoreInfo stencilLoadAndStoreInfo {
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400494 stencilLoadOp,
Chris Dalton674f77a2019-09-30 20:49:39 -0600495 stencilStoreOp,
Robert Phillips95214472017-08-08 18:00:03 -0400496 };
497
Robert Phillips96f22372020-05-20 12:31:18 -0400498 return gpu->getOpsRenderPass(rt, stencil, origin, bounds,
Greg Daniel9a18b082020-08-14 14:03:50 -0400499 kColorLoadStoreInfo, stencilLoadAndStoreInfo, sampledProxies,
Greg Daniel21774362020-09-14 10:36:43 -0400500 renderPassXferBarriers);
Robert Phillips178ce3e2017-04-13 09:15:47 -0400501}
502
Brian Salomon25a88092016-12-01 09:36:50 -0500503// TODO: this is where GrOp::renderTarget is used (which is fine since it
Robert Phillips294870f2016-11-11 12:38:40 -0500504// is at flush time). However, we need to store the RenderTargetProxy in the
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500505// Ops and instantiate them here.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400506bool GrOpsTask::onExecute(GrOpFlushState* flushState) {
Greg Daniel94ed83f2019-09-27 13:05:43 -0400507 // TODO: remove the check for discard here once reduced op splitting is turned on. Currently we
508 // can end up with GrOpsTasks that only have a discard load op and no ops. For vulkan validation
509 // we need to keep that discard and not drop it. Once we have reduce op list splitting enabled
510 // we shouldn't end up with GrOpsTasks with only discard.
511 if (this->isNoOp() || (fClippedContentBounds.isEmpty() && fColorLoadOp != GrLoadOp::kDiscard)) {
bsalomondc438982016-08-31 11:53:49 -0700512 return false;
egdanielb4021cf2016-07-28 08:53:07 -0700513 }
Robert Phillips4a395042017-04-24 16:27:17 +0000514
Adlai Holler33d569e2020-06-16 14:30:08 -0400515 SkASSERT(this->numTargets() == 1);
516 GrRenderTargetProxy* proxy = this->target(0).proxy()->asRenderTargetProxy();
Greg Daniel16f5c652019-10-29 11:26:01 -0400517 SkASSERT(proxy);
Brian Salomon5f394272019-07-02 14:07:49 -0400518 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400519
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500520 // Make sure load ops are not kClear if the GPU needs to use draws for clears
521 SkASSERT(fColorLoadOp != GrLoadOp::kClear ||
522 !flushState->gpu()->caps()->performColorClearsAsDraws());
Chris Dalton674f77a2019-09-30 20:49:39 -0600523
524 const GrCaps& caps = *flushState->gpu()->caps();
Greg Daniel16f5c652019-10-29 11:26:01 -0400525 GrRenderTarget* renderTarget = proxy->peekRenderTarget();
Chris Dalton674f77a2019-09-30 20:49:39 -0600526 SkASSERT(renderTarget);
Chris Dalton0b68dda2019-11-07 21:08:03 -0700527
Greg Danielc0d69152020-10-08 14:59:00 -0400528 GrAttachment* stencil = nullptr;
Chris Dalton0b68dda2019-11-07 21:08:03 -0700529 if (int numStencilSamples = proxy->numStencilSamples()) {
530 if (!flushState->resourceProvider()->attachStencilAttachment(
531 renderTarget, numStencilSamples)) {
532 SkDebugf("WARNING: failed to attach a stencil buffer. Rendering will be skipped.\n");
533 return false;
534 }
Brian Salomonf7f54332020-07-28 09:23:35 -0400535 stencil = renderTarget->getStencilAttachment();
Chris Dalton0b68dda2019-11-07 21:08:03 -0700536 }
537
Chris Daltonf00b95b2019-11-07 21:14:41 -0700538 SkASSERT(!stencil || stencil->numSamples() == proxy->numStencilSamples());
Chris Dalton674f77a2019-09-30 20:49:39 -0600539
540 GrLoadOp stencilLoadOp;
541 switch (fInitialStencilContent) {
542 case StencilContent::kDontCare:
543 stencilLoadOp = GrLoadOp::kDiscard;
544 break;
545 case StencilContent::kUserBitsCleared:
546 SkASSERT(!caps.performStencilClearsAsDraws());
547 SkASSERT(stencil);
548 if (caps.discardStencilValuesAfterRenderPass()) {
549 // Always clear the stencil if it is being discarded after render passes. This is
550 // also an optimization because we are on a tiler and it avoids loading the values
551 // from memory.
552 stencilLoadOp = GrLoadOp::kClear;
553 break;
554 }
555 if (!stencil->hasPerformedInitialClear()) {
556 stencilLoadOp = GrLoadOp::kClear;
557 stencil->markHasPerformedInitialClear();
558 break;
559 }
560 // renderTargetContexts are required to leave the user stencil bits in a cleared state
561 // once finished, meaning the stencil values will always remain cleared after the
562 // initial clear. Just fall through to reloading the existing (cleared) stencil values
563 // from memory.
John Stiles30212b72020-06-11 17:55:07 -0400564 [[fallthrough]];
Chris Dalton674f77a2019-09-30 20:49:39 -0600565 case StencilContent::kPreserved:
566 SkASSERT(stencil);
567 stencilLoadOp = GrLoadOp::kLoad;
568 break;
569 }
570
571 // NOTE: If fMustPreserveStencil is set, then we are executing a renderTargetContext that split
572 // its opsTask.
573 //
574 // FIXME: We don't currently flag render passes that don't use stencil at all. In that case
575 // their store op might be "discard", and we currently make the assumption that a discard will
576 // not invalidate what's already in main memory. This is probably ok for now, but certainly
577 // something we want to address soon.
578 GrStoreOp stencilStoreOp = (caps.discardStencilValuesAfterRenderPass() && !fMustPreserveStencil)
579 ? GrStoreOp::kDiscard
580 : GrStoreOp::kStore;
581
Greg Daniel4a0d36d2019-09-30 12:24:36 -0400582 GrOpsRenderPass* renderPass = create_render_pass(
Adlai Holler33d569e2020-06-16 14:30:08 -0400583 flushState->gpu(), proxy->peekRenderTarget(), stencil, this->target(0).origin(),
Chris Dalton674f77a2019-09-30 20:49:39 -0600584 fClippedContentBounds, fColorLoadOp, fLoadClearColor, stencilLoadOp, stencilStoreOp,
Greg Daniel21774362020-09-14 10:36:43 -0400585 fSampledProxies, fRenderPassXferBarriers);
586
Greg Danielfa3adf72019-11-07 09:53:41 -0500587 if (!renderPass) {
588 return false;
589 }
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400590 flushState->setOpsRenderPass(renderPass);
591 renderPass->begin();
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400592
593 // Draw all the generated geometry.
Brian Salomon588cec72018-11-14 13:56:37 -0500594 for (const auto& chain : fOpChains) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400595 if (!chain.shouldExecute()) {
bsalomonaecc0182016-03-07 11:50:44 -0800596 continue;
597 }
Stan Iliev2af578d2017-08-16 13:00:28 -0400598#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
Brian Salomon5f394272019-07-02 14:07:49 -0400599 TRACE_EVENT0("skia.gpu", chain.head()->name());
Stan Iliev2af578d2017-08-16 13:00:28 -0400600#endif
Robert Phillips178ce3e2017-04-13 09:15:47 -0400601
Robert Phillips405413f2019-10-04 10:39:28 -0400602 GrOpFlushState::OpArgs opArgs(chain.head(),
Adlai Holler33d569e2020-06-16 14:30:08 -0400603 &fTargets[0],
Robert Phillips405413f2019-10-04 10:39:28 -0400604 chain.appliedClip(),
Greg Danield358cbe2020-09-11 09:33:54 -0400605 chain.dstProxyView(),
606 fRenderPassXferBarriers);
Robert Phillips178ce3e2017-04-13 09:15:47 -0400607
Brian Salomon29b60c92017-10-31 14:42:10 -0400608 flushState->setOpArgs(&opArgs);
Brian Salomon588cec72018-11-14 13:56:37 -0500609 chain.head()->execute(flushState, chain.bounds());
Brian Salomon29b60c92017-10-31 14:42:10 -0400610 flushState->setOpArgs(nullptr);
bsalomon512be532015-09-10 10:42:55 -0700611 }
Robert Phillips178ce3e2017-04-13 09:15:47 -0400612
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400613 renderPass->end();
614 flushState->gpu()->submit(renderPass);
615 flushState->setOpsRenderPass(nullptr);
ethannicholas22793252016-01-30 09:59:10 -0800616
bsalomondc438982016-08-31 11:53:49 -0700617 return true;
bsalomona73239a2015-04-28 13:35:17 -0700618}
619
Greg Danielf41b2bd2019-08-22 16:19:24 -0400620void GrOpsTask::setColorLoadOp(GrLoadOp op, const SkPMColor4f& color) {
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500621 fColorLoadOp = op;
622 fLoadClearColor = color;
Chris Dalton16a33c62019-09-24 22:19:17 -0600623 if (GrLoadOp::kClear == fColorLoadOp) {
Adlai Holler33d569e2020-06-16 14:30:08 -0400624 GrSurfaceProxy* proxy = this->target(0).proxy();
Greg Daniel16f5c652019-10-29 11:26:01 -0400625 SkASSERT(proxy);
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400626 fTotalBounds = proxy->backingStoreBoundsRect();
Chris Dalton16a33c62019-09-24 22:19:17 -0600627 }
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500628}
629
Greg Danielf41b2bd2019-08-22 16:19:24 -0400630bool GrOpsTask::resetForFullscreenClear(CanDiscardPreviousOps canDiscardPreviousOps) {
Chris Dalton6b982802019-06-27 13:53:46 -0600631 if (CanDiscardPreviousOps::kYes == canDiscardPreviousOps || this->isEmpty()) {
Robert Phillipsc994a932018-06-19 13:09:54 -0400632 this->deleteOps();
Brian Osman099fa0f2017-10-02 16:38:32 -0400633 fDeferredProxies.reset();
Greg Danielb20d7e52019-09-03 13:54:39 -0400634 fSampledProxies.reset();
Greg Daniel070cbaf2019-01-03 17:35:54 -0500635
Greg Danielf41b2bd2019-08-22 16:19:24 -0400636 // If the opsTask is using a render target which wraps a vulkan command buffer, we can't do
637 // a clear load since we cannot change the render pass that we are using. Thus we fall back
638 // to making a clear op in this case.
Adlai Holler33d569e2020-06-16 14:30:08 -0400639 return !this->target(0).asRenderTargetProxy()->wrapsVkSecondaryCB();
bsalomonfd8d0132016-08-11 11:25:33 -0700640 }
Robert Phillips380b90c2017-08-30 07:41:07 -0400641
Greg Danielf41b2bd2019-08-22 16:19:24 -0400642 // Could not empty the task, so an op must be added to handle the clear
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500643 return false;
bsalomon9f129de2016-08-10 16:31:05 -0700644}
645
Greg Danielf41b2bd2019-08-22 16:19:24 -0400646void GrOpsTask::discard() {
647 // Discard calls to in-progress opsTasks are ignored. Calls at the start update the
648 // opsTasks' color & stencil load ops.
649 if (this->isEmpty()) {
650 fColorLoadOp = GrLoadOp::kDiscard;
Chris Dalton674f77a2019-09-30 20:49:39 -0600651 fInitialStencilContent = StencilContent::kDontCare;
Chris Dalton16a33c62019-09-24 22:19:17 -0600652 fTotalBounds.setEmpty();
Greg Danielf41b2bd2019-08-22 16:19:24 -0400653 }
654}
655
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000656////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000657
John Stiles1e0136e2020-08-12 18:44:00 -0400658#if GR_TEST_UTILS
Greg Danielf41b2bd2019-08-22 16:19:24 -0400659void GrOpsTask::dump(bool printDependencies) const {
660 GrRenderTask::dump(printDependencies);
661
Chris Dalton674f77a2019-09-30 20:49:39 -0600662 SkDebugf("fColorLoadOp: ");
663 switch (fColorLoadOp) {
664 case GrLoadOp::kLoad:
665 SkDebugf("kLoad\n");
666 break;
667 case GrLoadOp::kClear:
668 SkDebugf("kClear (0x%x)\n", fLoadClearColor.toBytes_RGBA());
669 break;
670 case GrLoadOp::kDiscard:
671 SkDebugf("kDiscard\n");
672 break;
673 }
674
675 SkDebugf("fInitialStencilContent: ");
676 switch (fInitialStencilContent) {
677 case StencilContent::kDontCare:
678 SkDebugf("kDontCare\n");
679 break;
680 case StencilContent::kUserBitsCleared:
681 SkDebugf("kUserBitsCleared\n");
682 break;
683 case StencilContent::kPreserved:
684 SkDebugf("kPreserved\n");
685 break;
686 }
Greg Danielf41b2bd2019-08-22 16:19:24 -0400687
688 SkDebugf("ops (%d):\n", fOpChains.count());
689 for (int i = 0; i < fOpChains.count(); ++i) {
690 SkDebugf("*******************************\n");
691 if (!fOpChains[i].head()) {
692 SkDebugf("%d: <combined forward or failed instantiation>\n", i);
693 } else {
694 SkDebugf("%d: %s\n", i, fOpChains[i].head()->name());
695 SkRect bounds = fOpChains[i].bounds();
696 SkDebugf("ClippedBounds: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", bounds.fLeft,
697 bounds.fTop, bounds.fRight, bounds.fBottom);
698 for (const auto& op : GrOp::ChainRange<>(fOpChains[i].head())) {
699 SkString info = SkTabString(op.dumpInfo(), 1);
700 SkDebugf("%s\n", info.c_str());
701 bounds = op.bounds();
702 SkDebugf("\tClippedBounds: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", bounds.fLeft,
703 bounds.fTop, bounds.fRight, bounds.fBottom);
704 }
705 }
706 }
707}
John Stiles1e0136e2020-08-12 18:44:00 -0400708#endif
Greg Danielf41b2bd2019-08-22 16:19:24 -0400709
John Stiles1e0136e2020-08-12 18:44:00 -0400710#ifdef SK_DEBUG
Michael Ludwigfcdd0612019-11-25 08:34:31 -0500711void GrOpsTask::visitProxies_debugOnly(const GrOp::VisitProxyFunc& func) const {
Brian Salomon7e67dca2020-07-21 09:27:25 -0400712 auto textureFunc = [ func ] (GrSurfaceProxy* tex, GrMipmapped mipmapped) {
Greg Danieldcf9ca12019-08-27 14:30:21 -0400713 func(tex, mipmapped);
714 };
715
Greg Danielf41b2bd2019-08-22 16:19:24 -0400716 for (const OpChain& chain : fOpChains) {
Greg Danieldcf9ca12019-08-27 14:30:21 -0400717 chain.visitProxies(textureFunc);
Greg Danielf41b2bd2019-08-22 16:19:24 -0400718 }
719}
720
721#endif
722
723////////////////////////////////////////////////////////////////////////////////
724
725bool GrOpsTask::onIsUsed(GrSurfaceProxy* proxyToCheck) const {
726 bool used = false;
727
Brian Salomon7e67dca2020-07-21 09:27:25 -0400728 auto visit = [ proxyToCheck, &used ] (GrSurfaceProxy* p, GrMipmapped) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400729 if (p == proxyToCheck) {
730 used = true;
731 }
732 };
733 for (const OpChain& recordedOp : fOpChains) {
734 recordedOp.visitProxies(visit);
735 }
736
737 return used;
738}
739
740void GrOpsTask::handleInternalAllocationFailure() {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500741 bool hasUninstantiatedProxy = false;
Brian Salomon7e67dca2020-07-21 09:27:25 -0400742 auto checkInstantiation = [&hasUninstantiatedProxy](GrSurfaceProxy* p, GrMipmapped) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400743 if (!p->isInstantiated()) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500744 hasUninstantiatedProxy = true;
745 }
746 };
Brian Salomon588cec72018-11-14 13:56:37 -0500747 for (OpChain& recordedOp : fOpChains) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500748 hasUninstantiatedProxy = false;
Chris Dalton1706cbf2019-05-21 19:35:29 -0600749 recordedOp.visitProxies(checkInstantiation);
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500750 if (hasUninstantiatedProxy) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400751 recordedOp.setSkipExecuteFlag();
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500752 }
753 }
754}
755
Greg Danielf41b2bd2019-08-22 16:19:24 -0400756void GrOpsTask::gatherProxyIntervals(GrResourceAllocator* alloc) const {
Robert Phillips51b20f22017-12-01 15:32:35 -0500757 for (int i = 0; i < fDeferredProxies.count(); ++i) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400758 SkASSERT(!fDeferredProxies[i]->isInstantiated());
Robert Phillips51b20f22017-12-01 15:32:35 -0500759 // We give all the deferred proxies a write usage at the very start of flushing. This
760 // locks them out of being reused for the entire flush until they are read - and then
761 // they can be recycled. This is a bit unfortunate because a flush can proceed in waves
762 // with sub-flushes. The deferred proxies only need to be pinned from the start of
763 // the sub-flush in which they appear.
Robert Phillipsc73666f2019-04-24 08:49:48 -0400764 alloc->addInterval(fDeferredProxies[i], 0, 0, GrResourceAllocator::ActualUse::kNo);
Robert Phillips51b20f22017-12-01 15:32:35 -0500765 }
766
Adlai Holler33d569e2020-06-16 14:30:08 -0400767 GrSurfaceProxy* targetProxy = this->target(0).proxy();
Greg Daniel16f5c652019-10-29 11:26:01 -0400768
Greg Danielf41b2bd2019-08-22 16:19:24 -0400769 // Add the interval for all the writes to this GrOpsTasks's target
Brian Salomon588cec72018-11-14 13:56:37 -0500770 if (fOpChains.count()) {
Robert Phillips3bf3d4a2019-03-27 07:09:09 -0400771 unsigned int cur = alloc->curOp();
772
Greg Daniel16f5c652019-10-29 11:26:01 -0400773 alloc->addInterval(targetProxy, cur, cur + fOpChains.count() - 1,
Robert Phillipsc73666f2019-04-24 08:49:48 -0400774 GrResourceAllocator::ActualUse::kYes);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500775 } else {
776 // This can happen if there is a loadOp (e.g., a clear) but no other draws. In this case we
777 // still need to add an interval for the destination so we create a fake op# for
778 // the missing clear op.
Greg Daniel16f5c652019-10-29 11:26:01 -0400779 alloc->addInterval(targetProxy, alloc->curOp(), alloc->curOp(),
Robert Phillipsc73666f2019-04-24 08:49:48 -0400780 GrResourceAllocator::ActualUse::kYes);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500781 alloc->incOps();
782 }
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400783
Brian Salomon7e67dca2020-07-21 09:27:25 -0400784 auto gather = [ alloc SkDEBUGCODE(, this) ] (GrSurfaceProxy* p, GrMipmapped) {
Robert Phillipsc73666f2019-04-24 08:49:48 -0400785 alloc->addInterval(p, alloc->curOp(), alloc->curOp(), GrResourceAllocator::ActualUse::kYes
Adlai Holler33d569e2020-06-16 14:30:08 -0400786 SkDEBUGCODE(, this->target(0).proxy() == p));
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400787 };
Brian Salomon588cec72018-11-14 13:56:37 -0500788 for (const OpChain& recordedOp : fOpChains) {
Chris Dalton1706cbf2019-05-21 19:35:29 -0600789 recordedOp.visitProxies(gather);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500790
Robert Phillips3bf3d4a2019-03-27 07:09:09 -0400791 // 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 -0500792 // keep all the math consistent.
793 alloc->incOps();
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400794 }
795}
796
Greg Danielf41b2bd2019-08-22 16:19:24 -0400797void GrOpsTask::recordOp(
Herb Derbyc76d4092020-10-07 16:46:15 -0400798 GrOp::Owner op, GrProcessorSet::Analysis processorAnalysis, GrAppliedClip* clip,
Greg Daniel524e28b2019-11-01 11:48:53 -0400799 const DstProxyView* dstProxyView, const GrCaps& caps) {
Ethan Nicholas029b22c2018-10-18 16:49:56 -0400800 SkDEBUGCODE(op->validate();)
Greg Daniel524e28b2019-11-01 11:48:53 -0400801 SkASSERT(processorAnalysis.requiresDstTexture() == (dstProxyView && dstProxyView->proxy()));
Adlai Holler33d569e2020-06-16 14:30:08 -0400802 GrSurfaceProxy* proxy = this->target(0).proxy();
Greg Daniel16f5c652019-10-29 11:26:01 -0400803 SkASSERT(proxy);
Robert Phillipsee683652017-04-26 11:53:10 -0400804
Greg Danielf41b2bd2019-08-22 16:19:24 -0400805 // A closed GrOpsTask should never receive new/more ops
robertphillips6a186652015-10-20 07:37:58 -0700806 SkASSERT(!this->isClosed());
Brian Salomon19ec80f2018-11-16 13:27:30 -0500807 if (!op->bounds().isFinite()) {
Brian Salomon19ec80f2018-11-16 13:27:30 -0500808 return;
809 }
robertphillipsa106c622015-10-16 09:07:06 -0700810
Chris Dalton16a33c62019-09-24 22:19:17 -0600811 // Account for this op's bounds before we attempt to combine.
812 // NOTE: The caller should have already called "op->setClippedBounds()" by now, if applicable.
813 fTotalBounds.join(op->bounds());
814
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500815 // Check if there is an op we can combine with by linearly searching back until we either
816 // 1) check every op
bsalomon512be532015-09-10 10:42:55 -0700817 // 2) intersect with something
818 // 3) find a 'blocker'
Greg Daniel16f5c652019-10-29 11:26:01 -0400819 GR_AUDIT_TRAIL_ADD_OP(fAuditTrail, op.get(), proxy->uniqueID());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400820 GrOP_INFO("opsTask: %d Recording (%s, opID: %u)\n"
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400821 "\tBounds [L: %.2f, T: %.2f R: %.2f B: %.2f]\n",
822 this->uniqueID(),
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500823 op->name(),
824 op->uniqueID(),
Robert Phillips1119dc32017-04-11 12:54:57 -0400825 op->bounds().fLeft, op->bounds().fTop,
826 op->bounds().fRight, op->bounds().fBottom);
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500827 GrOP_INFO(SkTabString(op->dumpInfo(), 1).c_str());
Brian Salomon25a88092016-12-01 09:36:50 -0500828 GrOP_INFO("\tOutcome:\n");
Brian Osman788b9162020-02-07 10:36:46 -0500829 int maxCandidates = std::min(kMaxOpChainDistance, fOpChains.count());
Robert Phillips318c4192017-05-17 09:36:38 -0400830 if (maxCandidates) {
bsalomon512be532015-09-10 10:42:55 -0700831 int i = 0;
832 while (true) {
Brian Salomon588cec72018-11-14 13:56:37 -0500833 OpChain& candidate = fOpChains.fromBack(i);
Greg Daniel524e28b2019-11-01 11:48:53 -0400834 op = candidate.appendOp(std::move(op), processorAnalysis, dstProxyView, clip, caps,
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500835 &fArenas, fAuditTrail);
Brian Salomon588cec72018-11-14 13:56:37 -0500836 if (!op) {
837 return;
bsalomon512be532015-09-10 10:42:55 -0700838 }
Brian Salomona7682c82018-10-24 10:04:37 -0400839 // Stop going backwards if we would cause a painter's order violation.
Brian Salomon588cec72018-11-14 13:56:37 -0500840 if (!can_reorder(candidate.bounds(), op->bounds())) {
841 GrOP_INFO("\t\tBackward: Intersects with chain (%s, head opID: %u)\n",
842 candidate.head()->name(), candidate.head()->uniqueID());
bsalomon512be532015-09-10 10:42:55 -0700843 break;
844 }
Brian Salomon588cec72018-11-14 13:56:37 -0500845 if (++i == maxCandidates) {
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400846 GrOP_INFO("\t\tBackward: Reached max lookback or beginning of op array %d\n", i);
bsalomon512be532015-09-10 10:42:55 -0700847 break;
848 }
849 }
850 } else {
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400851 GrOP_INFO("\t\tBackward: FirstOp\n");
bsalomon512be532015-09-10 10:42:55 -0700852 }
Brian Salomon54d212e2017-03-21 14:22:38 -0400853 if (clip) {
854 clip = fClipAllocator.make<GrAppliedClip>(std::move(*clip));
Robert Phillipsc84c0302017-05-08 15:35:11 -0400855 SkDEBUGCODE(fNumClips++;)
Brian Salomon54d212e2017-03-21 14:22:38 -0400856 }
Greg Daniel524e28b2019-11-01 11:48:53 -0400857 fOpChains.emplace_back(std::move(op), processorAnalysis, clip, dstProxyView);
bsalomon512be532015-09-10 10:42:55 -0700858}
859
Greg Danielf41b2bd2019-08-22 16:19:24 -0400860void GrOpsTask::forwardCombine(const GrCaps& caps) {
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400861 SkASSERT(!this->isClosed());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400862 GrOP_INFO("opsTask: %d ForwardCombine %d ops:\n", this->uniqueID(), fOpChains.count());
Robert Phillips48567ac2017-06-01 08:46:00 -0400863
Brian Salomon588cec72018-11-14 13:56:37 -0500864 for (int i = 0; i < fOpChains.count() - 1; ++i) {
865 OpChain& chain = fOpChains[i];
Brian Osman788b9162020-02-07 10:36:46 -0500866 int maxCandidateIdx = std::min(i + kMaxOpChainDistance, fOpChains.count() - 1);
bsalomonaecc0182016-03-07 11:50:44 -0800867 int j = i + 1;
868 while (true) {
Brian Salomon588cec72018-11-14 13:56:37 -0500869 OpChain& candidate = fOpChains[j];
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500870 if (candidate.prependChain(&chain, caps, &fArenas, fAuditTrail)) {
bsalomonaecc0182016-03-07 11:50:44 -0800871 break;
872 }
Robert Phillipsc84c0302017-05-08 15:35:11 -0400873 // Stop traversing if we would cause a painter's order violation.
Brian Salomon588cec72018-11-14 13:56:37 -0500874 if (!can_reorder(chain.bounds(), candidate.bounds())) {
875 GrOP_INFO(
876 "\t\t%d: chain (%s head opID: %u) -> "
877 "Intersects with chain (%s, head opID: %u)\n",
878 i, chain.head()->name(), chain.head()->uniqueID(), candidate.head()->name(),
879 candidate.head()->uniqueID());
bsalomonaecc0182016-03-07 11:50:44 -0800880 break;
881 }
Brian Salomona7682c82018-10-24 10:04:37 -0400882 if (++j > maxCandidateIdx) {
Brian Salomon588cec72018-11-14 13:56:37 -0500883 GrOP_INFO("\t\t%d: chain (%s opID: %u) -> Reached max lookahead or end of array\n",
884 i, chain.head()->name(), chain.head()->uniqueID());
bsalomonaecc0182016-03-07 11:50:44 -0800885 break;
886 }
887 }
888 }
889}
890
Chris Dalton16a33c62019-09-24 22:19:17 -0600891GrRenderTask::ExpectedOutcome GrOpsTask::onMakeClosed(
892 const GrCaps& caps, SkIRect* targetUpdateBounds) {
893 this->forwardCombine(caps);
Brian Salomonf1c9eae2020-05-01 15:00:34 -0400894 SkScopeExit triggerObservers([&] {
895 for (const auto& o : fClosedObservers) {
896 o->wasClosed(*this);
Brian Salomon3b8486a2020-04-21 12:43:26 -0400897 }
Brian Salomonf1c9eae2020-05-01 15:00:34 -0400898 fClosedObservers.reset();
Brian Salomon3b8486a2020-04-21 12:43:26 -0400899 });
Chris Dalton16a33c62019-09-24 22:19:17 -0600900 if (!this->isNoOp()) {
Adlai Holler33d569e2020-06-16 14:30:08 -0400901 GrSurfaceProxy* proxy = this->target(0).proxy();
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400902 // Use the entire backing store bounds since the GPU doesn't clip automatically to the
903 // logical dimensions.
904 SkRect clippedContentBounds = proxy->backingStoreBoundsRect();
Adlai Holler33d569e2020-06-16 14:30:08 -0400905 // TODO: If we can fix up GLPrograms test to always intersect the target proxy bounds
Greg Daniel16f5c652019-10-29 11:26:01 -0400906 // then we can simply assert here that the bounds intersect.
Chris Dalton16a33c62019-09-24 22:19:17 -0600907 if (clippedContentBounds.intersect(fTotalBounds)) {
Greg Daniel94ed83f2019-09-27 13:05:43 -0400908 clippedContentBounds.roundOut(&fClippedContentBounds);
909 *targetUpdateBounds = fClippedContentBounds;
Chris Dalton16a33c62019-09-24 22:19:17 -0600910 return ExpectedOutcome::kTargetDirty;
911 }
912 }
913 return ExpectedOutcome::kTargetUnchanged;
914}