blob: e89d3aa3b88629a60afea96380d33c36ce066be1 [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"
12#include "src/core/SkTraceEvent.h"
Greg Danielc0d69152020-10-08 14:59:00 -040013#include "src/gpu/GrAttachment.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040014#include "src/gpu/GrAuditTrail.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/GrCaps.h"
16#include "src/gpu/GrGpu.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/gpu/GrMemoryPool.h"
Greg Daniele227fe42019-08-21 13:52:24 -040018#include "src/gpu/GrOpFlushState.h"
Greg Daniel2d41d0d2019-08-26 11:08:51 -040019#include "src/gpu/GrOpsRenderPass.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/GrRecordingContextPriv.h"
Chris Dalton674f77a2019-09-30 20:49:39 -060021#include "src/gpu/GrRenderTarget.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/gpu/GrResourceAllocator.h"
Brian Salomoneebe7352020-12-09 16:37:04 -050023#include "src/gpu/GrSurfaceDrawContext.h"
Brian Salomon4cfae3b2020-07-23 10:33:24 -040024#include "src/gpu/GrTexture.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040025#include "src/gpu/geometry/GrRect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/gpu/ops/GrClearOp.h"
Robert Phillipsf2361d22016-10-25 14:20:06 -040027
reed@google.comac10a2d2010-12-22 21:39:39 +000028////////////////////////////////////////////////////////////////////////////////
29
Brian Salomon09d994e2016-12-21 11:14:46 -050030// Experimentally we have found that most combining occurs within the first 10 comparisons.
Brian Salomon588cec72018-11-14 13:56:37 -050031static const int kMaxOpMergeDistance = 10;
32static const int kMaxOpChainDistance = 10;
33
34////////////////////////////////////////////////////////////////////////////////
35
Greg Daniel524e28b2019-11-01 11:48:53 -040036using DstProxyView = GrXferProcessor::DstProxyView;
Brian Salomon588cec72018-11-14 13:56:37 -050037
38////////////////////////////////////////////////////////////////////////////////
39
40static inline bool can_reorder(const SkRect& a, const SkRect& b) { return !GrRectsOverlap(a, b); }
41
42////////////////////////////////////////////////////////////////////////////////
43
Herb Derbyc76d4092020-10-07 16:46:15 -040044inline GrOpsTask::OpChain::List::List(GrOp::Owner op)
Brian Salomon588cec72018-11-14 13:56:37 -050045 : fHead(std::move(op)), fTail(fHead.get()) {
46 this->validate();
47}
48
Greg Danielf41b2bd2019-08-22 16:19:24 -040049inline GrOpsTask::OpChain::List::List(List&& that) { *this = std::move(that); }
Brian Salomon588cec72018-11-14 13:56:37 -050050
Greg Danielf41b2bd2019-08-22 16:19:24 -040051inline GrOpsTask::OpChain::List& GrOpsTask::OpChain::List::operator=(List&& that) {
Brian Salomon588cec72018-11-14 13:56:37 -050052 fHead = std::move(that.fHead);
53 fTail = that.fTail;
54 that.fTail = nullptr;
55 this->validate();
56 return *this;
57}
58
Herb Derbyc76d4092020-10-07 16:46:15 -040059inline GrOp::Owner GrOpsTask::OpChain::List::popHead() {
Brian Salomon588cec72018-11-14 13:56:37 -050060 SkASSERT(fHead);
61 auto temp = fHead->cutChain();
62 std::swap(temp, fHead);
63 if (!fHead) {
64 SkASSERT(fTail == temp.get());
65 fTail = nullptr;
66 }
67 return temp;
68}
69
Herb Derbyc76d4092020-10-07 16:46:15 -040070inline GrOp::Owner GrOpsTask::OpChain::List::removeOp(GrOp* op) {
Brian Salomon588cec72018-11-14 13:56:37 -050071#ifdef SK_DEBUG
72 auto head = op;
73 while (head->prevInChain()) { head = head->prevInChain(); }
74 SkASSERT(head == fHead.get());
75#endif
76 auto prev = op->prevInChain();
77 if (!prev) {
78 SkASSERT(op == fHead.get());
79 return this->popHead();
80 }
81 auto temp = prev->cutChain();
82 if (auto next = temp->cutChain()) {
83 prev->chainConcat(std::move(next));
84 } else {
85 SkASSERT(fTail == op);
86 fTail = prev;
87 }
88 this->validate();
89 return temp;
90}
91
Herb Derbyc76d4092020-10-07 16:46:15 -040092inline void GrOpsTask::OpChain::List::pushHead(GrOp::Owner op) {
Brian Salomon588cec72018-11-14 13:56:37 -050093 SkASSERT(op);
94 SkASSERT(op->isChainHead());
95 SkASSERT(op->isChainTail());
96 if (fHead) {
97 op->chainConcat(std::move(fHead));
98 fHead = std::move(op);
99 } else {
100 fHead = std::move(op);
101 fTail = fHead.get();
102 }
103}
104
Herb Derbyc76d4092020-10-07 16:46:15 -0400105inline void GrOpsTask::OpChain::List::pushTail(GrOp::Owner op) {
Brian Salomon588cec72018-11-14 13:56:37 -0500106 SkASSERT(op->isChainTail());
107 fTail->chainConcat(std::move(op));
108 fTail = fTail->nextInChain();
109}
110
Greg Danielf41b2bd2019-08-22 16:19:24 -0400111inline void GrOpsTask::OpChain::List::validate() const {
Brian Salomon588cec72018-11-14 13:56:37 -0500112#ifdef SK_DEBUG
113 if (fHead) {
114 SkASSERT(fTail);
115 fHead->validateChain(fTail);
116 }
117#endif
118}
119
120////////////////////////////////////////////////////////////////////////////////
121
Herb Derbyc76d4092020-10-07 16:46:15 -0400122GrOpsTask::OpChain::OpChain(GrOp::Owner op,
Greg Danielf41b2bd2019-08-22 16:19:24 -0400123 GrProcessorSet::Analysis processorAnalysis,
Greg Daniel524e28b2019-11-01 11:48:53 -0400124 GrAppliedClip* appliedClip, const DstProxyView* dstProxyView)
Chris Dalton945ee652019-01-23 09:10:36 -0700125 : fList{std::move(op)}
126 , fProcessorAnalysis(processorAnalysis)
127 , fAppliedClip(appliedClip) {
128 if (fProcessorAnalysis.requiresDstTexture()) {
Greg Daniel524e28b2019-11-01 11:48:53 -0400129 SkASSERT(dstProxyView && dstProxyView->proxy());
130 fDstProxyView = *dstProxyView;
Brian Salomon588cec72018-11-14 13:56:37 -0500131 }
132 fBounds = fList.head()->bounds();
133}
134
Greg Danielf41b2bd2019-08-22 16:19:24 -0400135void GrOpsTask::OpChain::visitProxies(const GrOp::VisitProxyFunc& func) const {
Brian Salomon588cec72018-11-14 13:56:37 -0500136 if (fList.empty()) {
137 return;
138 }
139 for (const auto& op : GrOp::ChainRange<>(fList.head())) {
Chris Dalton1706cbf2019-05-21 19:35:29 -0600140 op.visitProxies(func);
Brian Salomon588cec72018-11-14 13:56:37 -0500141 }
Greg Daniel524e28b2019-11-01 11:48:53 -0400142 if (fDstProxyView.proxy()) {
Brian Salomon7e67dca2020-07-21 09:27:25 -0400143 func(fDstProxyView.proxy(), GrMipmapped::kNo);
Brian Salomon588cec72018-11-14 13:56:37 -0500144 }
145 if (fAppliedClip) {
146 fAppliedClip->visitProxies(func);
147 }
148}
149
Herb Derbye32e1ab2020-10-27 10:29:46 -0400150void GrOpsTask::OpChain::deleteOps() {
Brian Salomon588cec72018-11-14 13:56:37 -0500151 while (!fList.empty()) {
Herb Derbyc76d4092020-10-07 16:46:15 -0400152 // Since the value goes out of scope immediately, the GrOp::Owner deletes the op.
153 fList.popHead();
Brian Salomon588cec72018-11-14 13:56:37 -0500154 }
155}
156
157// Concatenates two op chains and attempts to merge ops across the chains. Assumes that we know that
158// the two chains are chainable. Returns the new chain.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400159GrOpsTask::OpChain::List GrOpsTask::OpChain::DoConcat(
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500160 List chainA, List chainB, const GrCaps& caps, GrRecordingContext::Arenas* arenas,
161 GrAuditTrail* auditTrail) {
Brian Salomon588cec72018-11-14 13:56:37 -0500162 // We process ops in chain b from head to tail. We attempt to merge with nodes in a, starting
163 // at chain a's tail and working toward the head. We produce one of the following outcomes:
164 // 1) b's head is merged into an op in a.
165 // 2) An op from chain a is merged into b's head. (In this case b's head gets processed again.)
166 // 3) b's head is popped from chain a and added at the tail of a.
167 // After result 3 we don't want to attempt to merge the next head of b with the new tail of a,
168 // as we assume merges were already attempted when chain b was created. So we keep track of the
169 // original tail of a and start our iteration of a there. We also track the bounds of the nodes
170 // appended to chain a that will be skipped for bounds testing. If the original tail of a is
171 // merged into an op in b (case 2) then we advance the "original tail" towards the head of a.
172 GrOp* origATail = chainA.tail();
173 SkRect skipBounds = SkRectPriv::MakeLargestInverted();
174 do {
175 int numMergeChecks = 0;
176 bool merged = false;
177 bool noSkip = (origATail == chainA.tail());
178 SkASSERT(noSkip == (skipBounds == SkRectPriv::MakeLargestInverted()));
179 bool canBackwardMerge = noSkip || can_reorder(chainB.head()->bounds(), skipBounds);
180 SkRect forwardMergeBounds = skipBounds;
181 GrOp* a = origATail;
182 while (a) {
183 bool canForwardMerge =
184 (a == chainA.tail()) || can_reorder(a->bounds(), forwardMergeBounds);
185 if (canForwardMerge || canBackwardMerge) {
Herb Derbye25c3002020-10-27 15:57:27 -0400186 auto result = a->combineIfPossible(
187 chainB.head(), arenas->recordTimeAllocator(), caps);
Brian Salomon588cec72018-11-14 13:56:37 -0500188 SkASSERT(result != GrOp::CombineResult::kCannotCombine);
189 merged = (result == GrOp::CombineResult::kMerged);
Robert Phillips9548c3b422019-01-08 12:35:43 -0500190 GrOP_INFO("\t\t: (%s opID: %u) -> Combining with (%s, opID: %u)\n",
Brian Salomon588cec72018-11-14 13:56:37 -0500191 chainB.head()->name(), chainB.head()->uniqueID(), a->name(),
192 a->uniqueID());
Brian Salomon588cec72018-11-14 13:56:37 -0500193 }
194 if (merged) {
Brian Salomon52a6ed32018-11-26 10:30:58 -0500195 GR_AUDIT_TRAIL_OPS_RESULT_COMBINED(auditTrail, a, chainB.head());
Brian Salomon588cec72018-11-14 13:56:37 -0500196 if (canBackwardMerge) {
Herb Derbyc76d4092020-10-07 16:46:15 -0400197 // The GrOp::Owner releases the op.
198 chainB.popHead();
Brian Salomon588cec72018-11-14 13:56:37 -0500199 } else {
200 // We merged the contents of b's head into a. We will replace b's head with a in
201 // chain b.
202 SkASSERT(canForwardMerge);
203 if (a == origATail) {
204 origATail = a->prevInChain();
205 }
Herb Derbyc76d4092020-10-07 16:46:15 -0400206 GrOp::Owner detachedA = chainA.removeOp(a);
207 // The GrOp::Owner releases the op.
208 chainB.popHead();
Brian Salomon588cec72018-11-14 13:56:37 -0500209 chainB.pushHead(std::move(detachedA));
210 if (chainA.empty()) {
211 // We merged all the nodes in chain a to chain b.
212 return chainB;
213 }
214 }
215 break;
216 } else {
217 if (++numMergeChecks == kMaxOpMergeDistance) {
218 break;
219 }
220 forwardMergeBounds.joinNonEmptyArg(a->bounds());
221 canBackwardMerge =
222 canBackwardMerge && can_reorder(chainB.head()->bounds(), a->bounds());
223 a = a->prevInChain();
224 }
225 }
226 // If we weren't able to merge b's head then pop b's head from chain b and make it the new
227 // tail of a.
228 if (!merged) {
229 chainA.pushTail(chainB.popHead());
230 skipBounds.joinNonEmptyArg(chainA.tail()->bounds());
231 }
232 } while (!chainB.empty());
233 return chainA;
234}
235
Chris Dalton945ee652019-01-23 09:10:36 -0700236// Attempts to concatenate the given chain onto our own and merge ops across the chains. Returns
237// whether the operation succeeded. On success, the provided list will be returned empty.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400238bool GrOpsTask::OpChain::tryConcat(
Greg Daniel524e28b2019-11-01 11:48:53 -0400239 List* list, GrProcessorSet::Analysis processorAnalysis, const DstProxyView& dstProxyView,
Chris Dalton945ee652019-01-23 09:10:36 -0700240 const GrAppliedClip* appliedClip, const SkRect& bounds, const GrCaps& caps,
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500241 GrRecordingContext::Arenas* arenas, GrAuditTrail* auditTrail) {
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700242 SkASSERT(!fList.empty());
243 SkASSERT(!list->empty());
Greg Daniel524e28b2019-11-01 11:48:53 -0400244 SkASSERT(fProcessorAnalysis.requiresDstTexture() == SkToBool(fDstProxyView.proxy()));
245 SkASSERT(processorAnalysis.requiresDstTexture() == SkToBool(dstProxyView.proxy()));
Brian Salomon588cec72018-11-14 13:56:37 -0500246 // All returns use explicit tuple constructor rather than {a, b} to work around old GCC bug.
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700247 if (fList.head()->classID() != list->head()->classID() ||
248 SkToBool(fAppliedClip) != SkToBool(appliedClip) ||
249 (fAppliedClip && *fAppliedClip != *appliedClip) ||
Chris Dalton945ee652019-01-23 09:10:36 -0700250 (fProcessorAnalysis.requiresNonOverlappingDraws() !=
251 processorAnalysis.requiresNonOverlappingDraws()) ||
252 (fProcessorAnalysis.requiresNonOverlappingDraws() &&
253 // Non-overlaping draws are only required when Ganesh will either insert a barrier,
254 // or read back a new dst texture between draws. In either case, we can neither
255 // chain nor combine overlapping Ops.
256 GrRectsTouchOrOverlap(fBounds, bounds)) ||
257 (fProcessorAnalysis.requiresDstTexture() != processorAnalysis.requiresDstTexture()) ||
Greg Daniel524e28b2019-11-01 11:48:53 -0400258 (fProcessorAnalysis.requiresDstTexture() && fDstProxyView != dstProxyView)) {
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700259 return false;
Brian Salomon588cec72018-11-14 13:56:37 -0500260 }
Chris Daltonee21e6b2019-01-22 14:04:43 -0700261
Brian Salomon588cec72018-11-14 13:56:37 -0500262 SkDEBUGCODE(bool first = true;)
263 do {
Herb Derbye25c3002020-10-27 15:57:27 -0400264 switch (fList.tail()->combineIfPossible(list->head(), arenas->recordTimeAllocator(), caps))
265 {
Brian Salomon588cec72018-11-14 13:56:37 -0500266 case GrOp::CombineResult::kCannotCombine:
267 // If an op supports chaining then it is required that chaining is transitive and
268 // that if any two ops in two different chains can merge then the two chains
269 // may also be chained together. Thus, we should only hit this on the first
270 // iteration.
271 SkASSERT(first);
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700272 return false;
Brian Salomon588cec72018-11-14 13:56:37 -0500273 case GrOp::CombineResult::kMayChain:
Adlai Holler5ba50af2020-04-29 21:11:14 -0400274 fList = DoConcat(std::move(fList), std::exchange(*list, List()), caps, arenas,
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500275 auditTrail);
Chris Daltonee21e6b2019-01-22 14:04:43 -0700276 // The above exchange cleared out 'list'. The list needs to be empty now for the
277 // loop to terminate.
278 SkASSERT(list->empty());
279 break;
Brian Salomon588cec72018-11-14 13:56:37 -0500280 case GrOp::CombineResult::kMerged: {
Robert Phillips9548c3b422019-01-08 12:35:43 -0500281 GrOP_INFO("\t\t: (%s opID: %u) -> Combining with (%s, opID: %u)\n",
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700282 list->tail()->name(), list->tail()->uniqueID(), list->head()->name(),
283 list->head()->uniqueID());
284 GR_AUDIT_TRAIL_OPS_RESULT_COMBINED(auditTrail, fList.tail(), list->head());
Herb Derbyc76d4092020-10-07 16:46:15 -0400285 // The GrOp::Owner releases the op.
286 list->popHead();
Brian Salomon588cec72018-11-14 13:56:37 -0500287 break;
288 }
289 }
290 SkDEBUGCODE(first = false);
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700291 } while (!list->empty());
Chris Daltonee21e6b2019-01-22 14:04:43 -0700292
293 // The new ops were successfully merged and/or chained onto our own.
294 fBounds.joinPossiblyEmptyRect(bounds);
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700295 return true;
Brian Salomon588cec72018-11-14 13:56:37 -0500296}
297
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500298bool GrOpsTask::OpChain::prependChain(OpChain* that, const GrCaps& caps,
299 GrRecordingContext::Arenas* arenas,
300 GrAuditTrail* auditTrail) {
Greg Daniel524e28b2019-11-01 11:48:53 -0400301 if (!that->tryConcat(&fList, fProcessorAnalysis, fDstProxyView, fAppliedClip, fBounds, caps,
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500302 arenas, auditTrail)) {
Brian Salomon588cec72018-11-14 13:56:37 -0500303 this->validate();
304 // append failed
305 return false;
306 }
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700307
Brian Salomon588cec72018-11-14 13:56:37 -0500308 // 'that' owns the combined chain. Move it into 'this'.
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700309 SkASSERT(fList.empty());
Brian Salomon588cec72018-11-14 13:56:37 -0500310 fList = std::move(that->fList);
Chris Daltonee21e6b2019-01-22 14:04:43 -0700311 fBounds = that->fBounds;
Brian Salomon588cec72018-11-14 13:56:37 -0500312
Greg Daniel524e28b2019-11-01 11:48:53 -0400313 that->fDstProxyView.setProxyView({});
John Stiles59e18dc2020-07-22 18:18:12 -0400314 if (that->fAppliedClip && that->fAppliedClip->hasCoverageFragmentProcessor()) {
315 // Obliterates the processor.
316 that->fAppliedClip->detachCoverageFragmentProcessor();
Brian Salomon588cec72018-11-14 13:56:37 -0500317 }
318 this->validate();
319 return true;
320}
321
Herb Derbyc76d4092020-10-07 16:46:15 -0400322GrOp::Owner GrOpsTask::OpChain::appendOp(
323 GrOp::Owner op, GrProcessorSet::Analysis processorAnalysis,
Greg Daniel524e28b2019-11-01 11:48:53 -0400324 const DstProxyView* dstProxyView, const GrAppliedClip* appliedClip, const GrCaps& caps,
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500325 GrRecordingContext::Arenas* arenas, GrAuditTrail* auditTrail) {
Greg Daniel524e28b2019-11-01 11:48:53 -0400326 const GrXferProcessor::DstProxyView noDstProxyView;
327 if (!dstProxyView) {
328 dstProxyView = &noDstProxyView;
Brian Salomon588cec72018-11-14 13:56:37 -0500329 }
330 SkASSERT(op->isChainHead() && op->isChainTail());
331 SkRect opBounds = op->bounds();
332 List chain(std::move(op));
Chris Dalton945ee652019-01-23 09:10:36 -0700333 if (!this->tryConcat(
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500334 &chain, processorAnalysis, *dstProxyView, appliedClip, opBounds, caps,
335 arenas, auditTrail)) {
Brian Salomon588cec72018-11-14 13:56:37 -0500336 // append failed, give the op back to the caller.
337 this->validate();
338 return chain.popHead();
339 }
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700340
341 SkASSERT(chain.empty());
Brian Salomon588cec72018-11-14 13:56:37 -0500342 this->validate();
343 return nullptr;
344}
345
Greg Danielf41b2bd2019-08-22 16:19:24 -0400346inline void GrOpsTask::OpChain::validate() const {
Brian Salomon588cec72018-11-14 13:56:37 -0500347#ifdef SK_DEBUG
348 fList.validate();
349 for (const auto& op : GrOp::ChainRange<>(fList.head())) {
350 // Not using SkRect::contains because we allow empty rects.
351 SkASSERT(fBounds.fLeft <= op.bounds().fLeft && fBounds.fTop <= op.bounds().fTop &&
352 fBounds.fRight >= op.bounds().fRight && fBounds.fBottom >= op.bounds().fBottom);
353 }
354#endif
355}
356
357////////////////////////////////////////////////////////////////////////////////
bsalomon489147c2015-12-14 12:13:09 -0800358
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400359GrOpsTask::GrOpsTask(GrDrawingManager* drawingMgr, GrRecordingContext::Arenas arenas,
Greg Daniel16f5c652019-10-29 11:26:01 -0400360 GrSurfaceProxyView view,
Greg Danielf41b2bd2019-08-22 16:19:24 -0400361 GrAuditTrail* auditTrail)
Adlai Holler33d569e2020-06-16 14:30:08 -0400362 : GrRenderTask()
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500363 , fArenas(arenas)
Greg Danielf41b2bd2019-08-22 16:19:24 -0400364 , fAuditTrail(auditTrail)
Robert Phillipsb6deea82017-05-11 14:14:30 -0400365 SkDEBUGCODE(, fNumClips(0)) {
Adlai Holler33d569e2020-06-16 14:30:08 -0400366 this->addTarget(drawingMgr, std::move(view));
bsalomon4061b122015-05-29 10:26:19 -0700367}
368
Greg Danielf41b2bd2019-08-22 16:19:24 -0400369void GrOpsTask::deleteOps() {
Brian Salomon588cec72018-11-14 13:56:37 -0500370 for (auto& chain : fOpChains) {
Herb Derbye32e1ab2020-10-27 10:29:46 -0400371 chain.deleteOps();
Robert Phillipsc994a932018-06-19 13:09:54 -0400372 }
Brian Salomon588cec72018-11-14 13:56:37 -0500373 fOpChains.reset();
Robert Phillipsc994a932018-06-19 13:09:54 -0400374}
375
Greg Danielf41b2bd2019-08-22 16:19:24 -0400376GrOpsTask::~GrOpsTask() {
Robert Phillipsc994a932018-06-19 13:09:54 -0400377 this->deleteOps();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000378}
379
Adlai Hollerabe45182020-11-17 09:22:13 -0500380void GrOpsTask::addOp(GrDrawingManager* drawingMgr, GrOp::Owner op,
381 GrTextureResolveManager textureResolveManager, const GrCaps& caps) {
382 auto addDependency = [&](GrSurfaceProxy* p, GrMipmapped mipmapped) {
383 this->addDependency(drawingMgr, p, mipmapped, textureResolveManager, caps);
384 };
385
386 op->visitProxies(addDependency);
387
388 this->recordOp(std::move(op), GrProcessorSet::EmptySetAnalysis(), nullptr, nullptr, caps);
389}
390
391void GrOpsTask::addDrawOp(GrDrawingManager* drawingMgr, GrOp::Owner op,
392 const GrProcessorSet::Analysis& processorAnalysis,
393 GrAppliedClip&& clip, const DstProxyView& dstProxyView,
394 GrTextureResolveManager textureResolveManager, const GrCaps& caps) {
395 auto addDependency = [&](GrSurfaceProxy* p, GrMipmapped mipmapped) {
396 this->addSampledTexture(p);
397 this->addDependency(drawingMgr, p, mipmapped, textureResolveManager, caps);
398 };
399
400 op->visitProxies(addDependency);
401 clip.visitProxies(addDependency);
402 if (dstProxyView.proxy()) {
403 if (GrDstSampleTypeUsesTexture(dstProxyView.dstSampleType())) {
404 this->addSampledTexture(dstProxyView.proxy());
405 }
406 addDependency(dstProxyView.proxy(), GrMipmapped::kNo);
407 if (this->target(0).proxy() == dstProxyView.proxy()) {
408 // Since we are sampling and drawing to the same surface we will need to use
409 // texture barriers.
410 SkASSERT(GrDstSampleTypeDirectlySamplesDst(dstProxyView.dstSampleType()));
411 fRenderPassXferBarriers |= GrXferBarrierFlags::kTexture;
412 }
413 SkASSERT(dstProxyView.dstSampleType() != GrDstSampleType::kAsInputAttachment ||
414 dstProxyView.offset().isZero());
415 }
416
417 if (processorAnalysis.usesNonCoherentHWBlending()) {
418 fRenderPassXferBarriers |= GrXferBarrierFlags::kBlend;
419 }
420
421 this->recordOp(std::move(op), processorAnalysis, clip.doesClip() ? &clip : nullptr,
422 &dstProxyView, caps);
423}
424
425
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400426void GrOpsTask::endFlush(GrDrawingManager* drawingMgr) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400427 fLastClipStackGenID = SK_InvalidUniqueID;
428 this->deleteOps();
429 fClipAllocator.reset();
Chris Dalton706a6ff2017-11-29 22:01:06 -0700430
Greg Danielf41b2bd2019-08-22 16:19:24 -0400431 fDeferredProxies.reset();
Greg Danielb20d7e52019-09-03 13:54:39 -0400432 fSampledProxies.reset();
Greg Danielf41b2bd2019-08-22 16:19:24 -0400433 fAuditTrail = nullptr;
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400434
435 GrRenderTask::endFlush(drawingMgr);
Greg Danielf21bf9e2019-08-22 20:12:20 +0000436}
437
Robert Phillips29f38542019-10-16 09:20:25 -0400438void GrOpsTask::onPrePrepare(GrRecordingContext* context) {
Robert Phillips7327c9d2019-10-08 16:32:56 -0400439 SkASSERT(this->isClosed());
440#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
441 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
442#endif
443 // TODO: remove the check for discard here once reduced op splitting is turned on. Currently we
444 // can end up with GrOpsTasks that only have a discard load op and no ops. For vulkan validation
445 // we need to keep that discard and not drop it. Once we have reduce op list splitting enabled
446 // we shouldn't end up with GrOpsTasks with only discard.
447 if (this->isNoOp() || (fClippedContentBounds.isEmpty() && fColorLoadOp != GrLoadOp::kDiscard)) {
448 return;
449 }
450
451 for (const auto& chain : fOpChains) {
452 if (chain.shouldExecute()) {
Robert Phillips8053c972019-11-21 10:44:53 -0500453 chain.head()->prePrepare(context,
Adlai Hollere2296f72020-11-19 13:41:26 -0500454 this->target(0),
Robert Phillips8053c972019-11-21 10:44:53 -0500455 chain.appliedClip(),
Greg Danield358cbe2020-09-11 09:33:54 -0400456 chain.dstProxyView(),
Greg Daniel42dbca52020-11-20 10:22:43 -0500457 fRenderPassXferBarriers,
458 fColorLoadOp);
Robert Phillips7327c9d2019-10-08 16:32:56 -0400459 }
460 }
461}
462
Greg Danielf41b2bd2019-08-22 16:19:24 -0400463void GrOpsTask::onPrepare(GrOpFlushState* flushState) {
Adlai Holler33d569e2020-06-16 14:30:08 -0400464 SkASSERT(this->target(0).proxy()->peekRenderTarget());
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400465 SkASSERT(this->isClosed());
Stan Iliev2af578d2017-08-16 13:00:28 -0400466#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
Brian Salomon5f394272019-07-02 14:07:49 -0400467 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Stan Iliev2af578d2017-08-16 13:00:28 -0400468#endif
Greg Daniel94ed83f2019-09-27 13:05:43 -0400469 // TODO: remove the check for discard here once reduced op splitting is turned on. Currently we
470 // can end up with GrOpsTasks that only have a discard load op and no ops. For vulkan validation
471 // we need to keep that discard and not drop it. Once we have reduce op list splitting enabled
472 // we shouldn't end up with GrOpsTasks with only discard.
473 if (this->isNoOp() || (fClippedContentBounds.isEmpty() && fColorLoadOp != GrLoadOp::kDiscard)) {
474 return;
475 }
robertphillipsa106c622015-10-16 09:07:06 -0700476
Greg Danielb20d7e52019-09-03 13:54:39 -0400477 flushState->setSampledProxyArray(&fSampledProxies);
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500478 // Loop over the ops that haven't yet been prepared.
Brian Salomon588cec72018-11-14 13:56:37 -0500479 for (const auto& chain : fOpChains) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400480 if (chain.shouldExecute()) {
Stan Iliev2af578d2017-08-16 13:00:28 -0400481#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
Brian Salomon5f394272019-07-02 14:07:49 -0400482 TRACE_EVENT0("skia.gpu", chain.head()->name());
Stan Iliev2af578d2017-08-16 13:00:28 -0400483#endif
Robert Phillips901aff02019-10-08 12:32:56 -0400484 GrOpFlushState::OpArgs opArgs(chain.head(),
Adlai Hollere2296f72020-11-19 13:41:26 -0500485 this->target(0),
Robert Phillips901aff02019-10-08 12:32:56 -0400486 chain.appliedClip(),
Greg Danield358cbe2020-09-11 09:33:54 -0400487 chain.dstProxyView(),
Greg Daniel42dbca52020-11-20 10:22:43 -0500488 fRenderPassXferBarriers,
489 fColorLoadOp);
Robert Phillips405413f2019-10-04 10:39:28 -0400490
Brian Salomon29b60c92017-10-31 14:42:10 -0400491 flushState->setOpArgs(&opArgs);
Robert Phillipsdf70f152019-11-15 14:57:05 -0500492
493 // Temporary debugging helper: for debugging prePrepare w/o going through DDLs
494 // Delete once most of the GrOps have an onPrePrepare.
Adlai Holler33d569e2020-06-16 14:30:08 -0400495 // chain.head()->prePrepare(flushState->gpu()->getContext(), &this->target(0),
Robert Phillipsdf70f152019-11-15 14:57:05 -0500496 // chain.appliedClip());
497
Robert Phillips7327c9d2019-10-08 16:32:56 -0400498 // GrOp::prePrepare may or may not have been called at this point
Brian Salomon588cec72018-11-14 13:56:37 -0500499 chain.head()->prepare(flushState);
Brian Salomon29b60c92017-10-31 14:42:10 -0400500 flushState->setOpArgs(nullptr);
bsalomonaecc0182016-03-07 11:50:44 -0800501 }
bsalomon512be532015-09-10 10:42:55 -0700502 }
Greg Danielb20d7e52019-09-03 13:54:39 -0400503 flushState->setSampledProxyArray(nullptr);
robertphillipsa13e2022015-11-11 12:01:09 -0800504}
bsalomon512be532015-09-10 10:42:55 -0700505
Greg Danielc0d69152020-10-08 14:59:00 -0400506static GrOpsRenderPass* create_render_pass(GrGpu* gpu,
507 GrRenderTarget* rt,
508 GrAttachment* stencil,
509 GrSurfaceOrigin origin,
510 const SkIRect& bounds,
511 GrLoadOp colorLoadOp,
Brian Salomon07bc9a22020-12-02 13:37:16 -0500512 const std::array<float, 4>& loadClearColor,
Greg Danielc0d69152020-10-08 14:59:00 -0400513 GrLoadOp stencilLoadOp,
514 GrStoreOp stencilStoreOp,
515 const SkTArray<GrSurfaceProxy*, true>& sampledProxies,
516 GrXferBarrierFlags renderPassXferBarriers) {
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400517 const GrOpsRenderPass::LoadAndStoreInfo kColorLoadStoreInfo {
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400518 colorLoadOp,
519 GrStoreOp::kStore,
520 loadClearColor
Robert Phillips178ce3e2017-04-13 09:15:47 -0400521 };
522
Robert Phillips95214472017-08-08 18:00:03 -0400523 // TODO:
524 // We would like to (at this level) only ever clear & discard. We would need
Greg Danielf41b2bd2019-08-22 16:19:24 -0400525 // to stop splitting up higher level OpsTasks for copyOps to achieve that.
Robert Phillips95214472017-08-08 18:00:03 -0400526 // Note: we would still need SB loads and stores but they would happen at a
527 // lower level (inside the VK command buffer).
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400528 const GrOpsRenderPass::StencilLoadAndStoreInfo stencilLoadAndStoreInfo {
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400529 stencilLoadOp,
Chris Dalton674f77a2019-09-30 20:49:39 -0600530 stencilStoreOp,
Robert Phillips95214472017-08-08 18:00:03 -0400531 };
532
Robert Phillips96f22372020-05-20 12:31:18 -0400533 return gpu->getOpsRenderPass(rt, stencil, origin, bounds,
Greg Daniel9a18b082020-08-14 14:03:50 -0400534 kColorLoadStoreInfo, stencilLoadAndStoreInfo, sampledProxies,
Greg Daniel21774362020-09-14 10:36:43 -0400535 renderPassXferBarriers);
Robert Phillips178ce3e2017-04-13 09:15:47 -0400536}
537
Brian Salomon25a88092016-12-01 09:36:50 -0500538// TODO: this is where GrOp::renderTarget is used (which is fine since it
Robert Phillips294870f2016-11-11 12:38:40 -0500539// is at flush time). However, we need to store the RenderTargetProxy in the
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500540// Ops and instantiate them here.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400541bool GrOpsTask::onExecute(GrOpFlushState* flushState) {
Greg Daniel94ed83f2019-09-27 13:05:43 -0400542 // TODO: remove the check for discard here once reduced op splitting is turned on. Currently we
543 // can end up with GrOpsTasks that only have a discard load op and no ops. For vulkan validation
544 // we need to keep that discard and not drop it. Once we have reduce op list splitting enabled
545 // we shouldn't end up with GrOpsTasks with only discard.
546 if (this->isNoOp() || (fClippedContentBounds.isEmpty() && fColorLoadOp != GrLoadOp::kDiscard)) {
bsalomondc438982016-08-31 11:53:49 -0700547 return false;
egdanielb4021cf2016-07-28 08:53:07 -0700548 }
Robert Phillips4a395042017-04-24 16:27:17 +0000549
Adlai Holler33d569e2020-06-16 14:30:08 -0400550 SkASSERT(this->numTargets() == 1);
551 GrRenderTargetProxy* proxy = this->target(0).proxy()->asRenderTargetProxy();
Greg Daniel16f5c652019-10-29 11:26:01 -0400552 SkASSERT(proxy);
Brian Salomon5f394272019-07-02 14:07:49 -0400553 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400554
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500555 // Make sure load ops are not kClear if the GPU needs to use draws for clears
556 SkASSERT(fColorLoadOp != GrLoadOp::kClear ||
557 !flushState->gpu()->caps()->performColorClearsAsDraws());
Chris Dalton674f77a2019-09-30 20:49:39 -0600558
559 const GrCaps& caps = *flushState->gpu()->caps();
Greg Daniel16f5c652019-10-29 11:26:01 -0400560 GrRenderTarget* renderTarget = proxy->peekRenderTarget();
Chris Dalton674f77a2019-09-30 20:49:39 -0600561 SkASSERT(renderTarget);
Chris Dalton0b68dda2019-11-07 21:08:03 -0700562
Greg Danielc0d69152020-10-08 14:59:00 -0400563 GrAttachment* stencil = nullptr;
Chris Dalton0b68dda2019-11-07 21:08:03 -0700564 if (int numStencilSamples = proxy->numStencilSamples()) {
565 if (!flushState->resourceProvider()->attachStencilAttachment(
566 renderTarget, numStencilSamples)) {
567 SkDebugf("WARNING: failed to attach a stencil buffer. Rendering will be skipped.\n");
568 return false;
569 }
Brian Salomonf7f54332020-07-28 09:23:35 -0400570 stencil = renderTarget->getStencilAttachment();
Chris Dalton0b68dda2019-11-07 21:08:03 -0700571 }
572
Chris Daltonf00b95b2019-11-07 21:14:41 -0700573 SkASSERT(!stencil || stencil->numSamples() == proxy->numStencilSamples());
Chris Dalton674f77a2019-09-30 20:49:39 -0600574
575 GrLoadOp stencilLoadOp;
576 switch (fInitialStencilContent) {
577 case StencilContent::kDontCare:
578 stencilLoadOp = GrLoadOp::kDiscard;
579 break;
580 case StencilContent::kUserBitsCleared:
581 SkASSERT(!caps.performStencilClearsAsDraws());
582 SkASSERT(stencil);
583 if (caps.discardStencilValuesAfterRenderPass()) {
584 // Always clear the stencil if it is being discarded after render passes. This is
585 // also an optimization because we are on a tiler and it avoids loading the values
586 // from memory.
587 stencilLoadOp = GrLoadOp::kClear;
588 break;
589 }
590 if (!stencil->hasPerformedInitialClear()) {
591 stencilLoadOp = GrLoadOp::kClear;
592 stencil->markHasPerformedInitialClear();
593 break;
594 }
595 // renderTargetContexts are required to leave the user stencil bits in a cleared state
596 // once finished, meaning the stencil values will always remain cleared after the
597 // initial clear. Just fall through to reloading the existing (cleared) stencil values
598 // from memory.
John Stiles30212b72020-06-11 17:55:07 -0400599 [[fallthrough]];
Chris Dalton674f77a2019-09-30 20:49:39 -0600600 case StencilContent::kPreserved:
601 SkASSERT(stencil);
602 stencilLoadOp = GrLoadOp::kLoad;
603 break;
604 }
605
606 // NOTE: If fMustPreserveStencil is set, then we are executing a renderTargetContext that split
607 // its opsTask.
608 //
609 // FIXME: We don't currently flag render passes that don't use stencil at all. In that case
610 // their store op might be "discard", and we currently make the assumption that a discard will
611 // not invalidate what's already in main memory. This is probably ok for now, but certainly
612 // something we want to address soon.
613 GrStoreOp stencilStoreOp = (caps.discardStencilValuesAfterRenderPass() && !fMustPreserveStencil)
614 ? GrStoreOp::kDiscard
615 : GrStoreOp::kStore;
616
Greg Daniel4a0d36d2019-09-30 12:24:36 -0400617 GrOpsRenderPass* renderPass = create_render_pass(
Adlai Holler33d569e2020-06-16 14:30:08 -0400618 flushState->gpu(), proxy->peekRenderTarget(), stencil, this->target(0).origin(),
Chris Dalton674f77a2019-09-30 20:49:39 -0600619 fClippedContentBounds, fColorLoadOp, fLoadClearColor, stencilLoadOp, stencilStoreOp,
Greg Daniel21774362020-09-14 10:36:43 -0400620 fSampledProxies, fRenderPassXferBarriers);
621
Greg Danielfa3adf72019-11-07 09:53:41 -0500622 if (!renderPass) {
623 return false;
624 }
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400625 flushState->setOpsRenderPass(renderPass);
626 renderPass->begin();
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400627
628 // Draw all the generated geometry.
Brian Salomon588cec72018-11-14 13:56:37 -0500629 for (const auto& chain : fOpChains) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400630 if (!chain.shouldExecute()) {
bsalomonaecc0182016-03-07 11:50:44 -0800631 continue;
632 }
Stan Iliev2af578d2017-08-16 13:00:28 -0400633#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
Brian Salomon5f394272019-07-02 14:07:49 -0400634 TRACE_EVENT0("skia.gpu", chain.head()->name());
Stan Iliev2af578d2017-08-16 13:00:28 -0400635#endif
Robert Phillips178ce3e2017-04-13 09:15:47 -0400636
Robert Phillips405413f2019-10-04 10:39:28 -0400637 GrOpFlushState::OpArgs opArgs(chain.head(),
Adlai Hollere2296f72020-11-19 13:41:26 -0500638 this->target(0),
Robert Phillips405413f2019-10-04 10:39:28 -0400639 chain.appliedClip(),
Greg Danield358cbe2020-09-11 09:33:54 -0400640 chain.dstProxyView(),
Greg Daniel42dbca52020-11-20 10:22:43 -0500641 fRenderPassXferBarriers,
642 fColorLoadOp);
Robert Phillips178ce3e2017-04-13 09:15:47 -0400643
Brian Salomon29b60c92017-10-31 14:42:10 -0400644 flushState->setOpArgs(&opArgs);
Brian Salomon588cec72018-11-14 13:56:37 -0500645 chain.head()->execute(flushState, chain.bounds());
Brian Salomon29b60c92017-10-31 14:42:10 -0400646 flushState->setOpArgs(nullptr);
bsalomon512be532015-09-10 10:42:55 -0700647 }
Robert Phillips178ce3e2017-04-13 09:15:47 -0400648
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400649 renderPass->end();
650 flushState->gpu()->submit(renderPass);
651 flushState->setOpsRenderPass(nullptr);
ethannicholas22793252016-01-30 09:59:10 -0800652
bsalomondc438982016-08-31 11:53:49 -0700653 return true;
bsalomona73239a2015-04-28 13:35:17 -0700654}
655
Brian Salomon07bc9a22020-12-02 13:37:16 -0500656void GrOpsTask::setColorLoadOp(GrLoadOp op, std::array<float, 4> color) {
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500657 fColorLoadOp = op;
658 fLoadClearColor = color;
Chris Dalton16a33c62019-09-24 22:19:17 -0600659 if (GrLoadOp::kClear == fColorLoadOp) {
Adlai Holler33d569e2020-06-16 14:30:08 -0400660 GrSurfaceProxy* proxy = this->target(0).proxy();
Greg Daniel16f5c652019-10-29 11:26:01 -0400661 SkASSERT(proxy);
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400662 fTotalBounds = proxy->backingStoreBoundsRect();
Chris Dalton16a33c62019-09-24 22:19:17 -0600663 }
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500664}
665
Greg Danielf41b2bd2019-08-22 16:19:24 -0400666bool GrOpsTask::resetForFullscreenClear(CanDiscardPreviousOps canDiscardPreviousOps) {
Chris Dalton6b982802019-06-27 13:53:46 -0600667 if (CanDiscardPreviousOps::kYes == canDiscardPreviousOps || this->isEmpty()) {
Robert Phillipsc994a932018-06-19 13:09:54 -0400668 this->deleteOps();
Brian Osman099fa0f2017-10-02 16:38:32 -0400669 fDeferredProxies.reset();
Greg Danielb20d7e52019-09-03 13:54:39 -0400670 fSampledProxies.reset();
Greg Daniel070cbaf2019-01-03 17:35:54 -0500671
Greg Danielf41b2bd2019-08-22 16:19:24 -0400672 // If the opsTask is using a render target which wraps a vulkan command buffer, we can't do
673 // a clear load since we cannot change the render pass that we are using. Thus we fall back
674 // to making a clear op in this case.
Adlai Holler33d569e2020-06-16 14:30:08 -0400675 return !this->target(0).asRenderTargetProxy()->wrapsVkSecondaryCB();
bsalomonfd8d0132016-08-11 11:25:33 -0700676 }
Robert Phillips380b90c2017-08-30 07:41:07 -0400677
Greg Danielf41b2bd2019-08-22 16:19:24 -0400678 // Could not empty the task, so an op must be added to handle the clear
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500679 return false;
bsalomon9f129de2016-08-10 16:31:05 -0700680}
681
Greg Danielf41b2bd2019-08-22 16:19:24 -0400682void GrOpsTask::discard() {
683 // Discard calls to in-progress opsTasks are ignored. Calls at the start update the
684 // opsTasks' color & stencil load ops.
685 if (this->isEmpty()) {
686 fColorLoadOp = GrLoadOp::kDiscard;
Chris Dalton674f77a2019-09-30 20:49:39 -0600687 fInitialStencilContent = StencilContent::kDontCare;
Chris Dalton16a33c62019-09-24 22:19:17 -0600688 fTotalBounds.setEmpty();
Greg Danielf41b2bd2019-08-22 16:19:24 -0400689 }
690}
691
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000692////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000693
John Stiles1e0136e2020-08-12 18:44:00 -0400694#if GR_TEST_UTILS
Greg Danielf41b2bd2019-08-22 16:19:24 -0400695void GrOpsTask::dump(bool printDependencies) const {
696 GrRenderTask::dump(printDependencies);
697
Chris Dalton674f77a2019-09-30 20:49:39 -0600698 SkDebugf("fColorLoadOp: ");
699 switch (fColorLoadOp) {
700 case GrLoadOp::kLoad:
701 SkDebugf("kLoad\n");
702 break;
703 case GrLoadOp::kClear:
Brian Salomon07bc9a22020-12-02 13:37:16 -0500704 SkDebugf("kClear {%g, %g, %g, %g}\n",
705 fLoadClearColor[0],
706 fLoadClearColor[1],
707 fLoadClearColor[2],
708 fLoadClearColor[3]);
Chris Dalton674f77a2019-09-30 20:49:39 -0600709 break;
710 case GrLoadOp::kDiscard:
711 SkDebugf("kDiscard\n");
712 break;
713 }
714
715 SkDebugf("fInitialStencilContent: ");
716 switch (fInitialStencilContent) {
717 case StencilContent::kDontCare:
718 SkDebugf("kDontCare\n");
719 break;
720 case StencilContent::kUserBitsCleared:
721 SkDebugf("kUserBitsCleared\n");
722 break;
723 case StencilContent::kPreserved:
724 SkDebugf("kPreserved\n");
725 break;
726 }
Greg Danielf41b2bd2019-08-22 16:19:24 -0400727
728 SkDebugf("ops (%d):\n", fOpChains.count());
729 for (int i = 0; i < fOpChains.count(); ++i) {
730 SkDebugf("*******************************\n");
731 if (!fOpChains[i].head()) {
732 SkDebugf("%d: <combined forward or failed instantiation>\n", i);
733 } else {
734 SkDebugf("%d: %s\n", i, fOpChains[i].head()->name());
735 SkRect bounds = fOpChains[i].bounds();
736 SkDebugf("ClippedBounds: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", bounds.fLeft,
737 bounds.fTop, bounds.fRight, bounds.fBottom);
738 for (const auto& op : GrOp::ChainRange<>(fOpChains[i].head())) {
739 SkString info = SkTabString(op.dumpInfo(), 1);
740 SkDebugf("%s\n", info.c_str());
741 bounds = op.bounds();
742 SkDebugf("\tClippedBounds: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", bounds.fLeft,
743 bounds.fTop, bounds.fRight, bounds.fBottom);
744 }
745 }
746 }
747}
John Stiles1e0136e2020-08-12 18:44:00 -0400748#endif
Greg Danielf41b2bd2019-08-22 16:19:24 -0400749
John Stiles1e0136e2020-08-12 18:44:00 -0400750#ifdef SK_DEBUG
Michael Ludwigfcdd0612019-11-25 08:34:31 -0500751void GrOpsTask::visitProxies_debugOnly(const GrOp::VisitProxyFunc& func) const {
Brian Salomon7e67dca2020-07-21 09:27:25 -0400752 auto textureFunc = [ func ] (GrSurfaceProxy* tex, GrMipmapped mipmapped) {
Greg Danieldcf9ca12019-08-27 14:30:21 -0400753 func(tex, mipmapped);
754 };
755
Greg Danielf41b2bd2019-08-22 16:19:24 -0400756 for (const OpChain& chain : fOpChains) {
Greg Danieldcf9ca12019-08-27 14:30:21 -0400757 chain.visitProxies(textureFunc);
Greg Danielf41b2bd2019-08-22 16:19:24 -0400758 }
759}
760
761#endif
762
763////////////////////////////////////////////////////////////////////////////////
764
765bool GrOpsTask::onIsUsed(GrSurfaceProxy* proxyToCheck) const {
766 bool used = false;
767
Brian Salomon7e67dca2020-07-21 09:27:25 -0400768 auto visit = [ proxyToCheck, &used ] (GrSurfaceProxy* p, GrMipmapped) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400769 if (p == proxyToCheck) {
770 used = true;
771 }
772 };
773 for (const OpChain& recordedOp : fOpChains) {
774 recordedOp.visitProxies(visit);
775 }
776
777 return used;
778}
779
780void GrOpsTask::handleInternalAllocationFailure() {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500781 bool hasUninstantiatedProxy = false;
Brian Salomon7e67dca2020-07-21 09:27:25 -0400782 auto checkInstantiation = [&hasUninstantiatedProxy](GrSurfaceProxy* p, GrMipmapped) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400783 if (!p->isInstantiated()) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500784 hasUninstantiatedProxy = true;
785 }
786 };
Brian Salomon588cec72018-11-14 13:56:37 -0500787 for (OpChain& recordedOp : fOpChains) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500788 hasUninstantiatedProxy = false;
Chris Dalton1706cbf2019-05-21 19:35:29 -0600789 recordedOp.visitProxies(checkInstantiation);
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500790 if (hasUninstantiatedProxy) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400791 recordedOp.setSkipExecuteFlag();
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500792 }
793 }
794}
795
Greg Danielf41b2bd2019-08-22 16:19:24 -0400796void GrOpsTask::gatherProxyIntervals(GrResourceAllocator* alloc) const {
Robert Phillips51b20f22017-12-01 15:32:35 -0500797 for (int i = 0; i < fDeferredProxies.count(); ++i) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400798 SkASSERT(!fDeferredProxies[i]->isInstantiated());
Robert Phillips51b20f22017-12-01 15:32:35 -0500799 // We give all the deferred proxies a write usage at the very start of flushing. This
800 // locks them out of being reused for the entire flush until they are read - and then
801 // they can be recycled. This is a bit unfortunate because a flush can proceed in waves
802 // with sub-flushes. The deferred proxies only need to be pinned from the start of
803 // the sub-flush in which they appear.
Robert Phillipsc73666f2019-04-24 08:49:48 -0400804 alloc->addInterval(fDeferredProxies[i], 0, 0, GrResourceAllocator::ActualUse::kNo);
Robert Phillips51b20f22017-12-01 15:32:35 -0500805 }
806
Adlai Holler33d569e2020-06-16 14:30:08 -0400807 GrSurfaceProxy* targetProxy = this->target(0).proxy();
Greg Daniel16f5c652019-10-29 11:26:01 -0400808
Greg Danielf41b2bd2019-08-22 16:19:24 -0400809 // Add the interval for all the writes to this GrOpsTasks's target
Brian Salomon588cec72018-11-14 13:56:37 -0500810 if (fOpChains.count()) {
Robert Phillips3bf3d4a2019-03-27 07:09:09 -0400811 unsigned int cur = alloc->curOp();
812
Greg Daniel16f5c652019-10-29 11:26:01 -0400813 alloc->addInterval(targetProxy, cur, cur + fOpChains.count() - 1,
Robert Phillipsc73666f2019-04-24 08:49:48 -0400814 GrResourceAllocator::ActualUse::kYes);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500815 } else {
816 // This can happen if there is a loadOp (e.g., a clear) but no other draws. In this case we
817 // still need to add an interval for the destination so we create a fake op# for
818 // the missing clear op.
Greg Daniel16f5c652019-10-29 11:26:01 -0400819 alloc->addInterval(targetProxy, alloc->curOp(), alloc->curOp(),
Robert Phillipsc73666f2019-04-24 08:49:48 -0400820 GrResourceAllocator::ActualUse::kYes);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500821 alloc->incOps();
822 }
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400823
Brian Salomon7e67dca2020-07-21 09:27:25 -0400824 auto gather = [ alloc SkDEBUGCODE(, this) ] (GrSurfaceProxy* p, GrMipmapped) {
Robert Phillipsc73666f2019-04-24 08:49:48 -0400825 alloc->addInterval(p, alloc->curOp(), alloc->curOp(), GrResourceAllocator::ActualUse::kYes
Adlai Holler33d569e2020-06-16 14:30:08 -0400826 SkDEBUGCODE(, this->target(0).proxy() == p));
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400827 };
Brian Salomon588cec72018-11-14 13:56:37 -0500828 for (const OpChain& recordedOp : fOpChains) {
Chris Dalton1706cbf2019-05-21 19:35:29 -0600829 recordedOp.visitProxies(gather);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500830
Robert Phillips3bf3d4a2019-03-27 07:09:09 -0400831 // 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 -0500832 // keep all the math consistent.
833 alloc->incOps();
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400834 }
835}
836
Greg Danielf41b2bd2019-08-22 16:19:24 -0400837void GrOpsTask::recordOp(
Herb Derbyc76d4092020-10-07 16:46:15 -0400838 GrOp::Owner op, GrProcessorSet::Analysis processorAnalysis, GrAppliedClip* clip,
Greg Daniel524e28b2019-11-01 11:48:53 -0400839 const DstProxyView* dstProxyView, const GrCaps& caps) {
Ethan Nicholas029b22c2018-10-18 16:49:56 -0400840 SkDEBUGCODE(op->validate();)
Greg Daniel524e28b2019-11-01 11:48:53 -0400841 SkASSERT(processorAnalysis.requiresDstTexture() == (dstProxyView && dstProxyView->proxy()));
Adlai Holler33d569e2020-06-16 14:30:08 -0400842 GrSurfaceProxy* proxy = this->target(0).proxy();
Greg Daniel16f5c652019-10-29 11:26:01 -0400843 SkASSERT(proxy);
Robert Phillipsee683652017-04-26 11:53:10 -0400844
Greg Danielf41b2bd2019-08-22 16:19:24 -0400845 // A closed GrOpsTask should never receive new/more ops
robertphillips6a186652015-10-20 07:37:58 -0700846 SkASSERT(!this->isClosed());
Brian Salomon19ec80f2018-11-16 13:27:30 -0500847 if (!op->bounds().isFinite()) {
Brian Salomon19ec80f2018-11-16 13:27:30 -0500848 return;
849 }
robertphillipsa106c622015-10-16 09:07:06 -0700850
Chris Dalton16a33c62019-09-24 22:19:17 -0600851 // Account for this op's bounds before we attempt to combine.
852 // NOTE: The caller should have already called "op->setClippedBounds()" by now, if applicable.
853 fTotalBounds.join(op->bounds());
854
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500855 // Check if there is an op we can combine with by linearly searching back until we either
856 // 1) check every op
bsalomon512be532015-09-10 10:42:55 -0700857 // 2) intersect with something
858 // 3) find a 'blocker'
Greg Daniel16f5c652019-10-29 11:26:01 -0400859 GR_AUDIT_TRAIL_ADD_OP(fAuditTrail, op.get(), proxy->uniqueID());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400860 GrOP_INFO("opsTask: %d Recording (%s, opID: %u)\n"
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400861 "\tBounds [L: %.2f, T: %.2f R: %.2f B: %.2f]\n",
862 this->uniqueID(),
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500863 op->name(),
864 op->uniqueID(),
Robert Phillips1119dc32017-04-11 12:54:57 -0400865 op->bounds().fLeft, op->bounds().fTop,
866 op->bounds().fRight, op->bounds().fBottom);
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500867 GrOP_INFO(SkTabString(op->dumpInfo(), 1).c_str());
Brian Salomon25a88092016-12-01 09:36:50 -0500868 GrOP_INFO("\tOutcome:\n");
Brian Osman788b9162020-02-07 10:36:46 -0500869 int maxCandidates = std::min(kMaxOpChainDistance, fOpChains.count());
Robert Phillips318c4192017-05-17 09:36:38 -0400870 if (maxCandidates) {
bsalomon512be532015-09-10 10:42:55 -0700871 int i = 0;
872 while (true) {
Brian Salomon588cec72018-11-14 13:56:37 -0500873 OpChain& candidate = fOpChains.fromBack(i);
Greg Daniel524e28b2019-11-01 11:48:53 -0400874 op = candidate.appendOp(std::move(op), processorAnalysis, dstProxyView, clip, caps,
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500875 &fArenas, fAuditTrail);
Brian Salomon588cec72018-11-14 13:56:37 -0500876 if (!op) {
877 return;
bsalomon512be532015-09-10 10:42:55 -0700878 }
Brian Salomona7682c82018-10-24 10:04:37 -0400879 // Stop going backwards if we would cause a painter's order violation.
Brian Salomon588cec72018-11-14 13:56:37 -0500880 if (!can_reorder(candidate.bounds(), op->bounds())) {
881 GrOP_INFO("\t\tBackward: Intersects with chain (%s, head opID: %u)\n",
882 candidate.head()->name(), candidate.head()->uniqueID());
bsalomon512be532015-09-10 10:42:55 -0700883 break;
884 }
Brian Salomon588cec72018-11-14 13:56:37 -0500885 if (++i == maxCandidates) {
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400886 GrOP_INFO("\t\tBackward: Reached max lookback or beginning of op array %d\n", i);
bsalomon512be532015-09-10 10:42:55 -0700887 break;
888 }
889 }
890 } else {
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400891 GrOP_INFO("\t\tBackward: FirstOp\n");
bsalomon512be532015-09-10 10:42:55 -0700892 }
Brian Salomon54d212e2017-03-21 14:22:38 -0400893 if (clip) {
894 clip = fClipAllocator.make<GrAppliedClip>(std::move(*clip));
Robert Phillipsc84c0302017-05-08 15:35:11 -0400895 SkDEBUGCODE(fNumClips++;)
Brian Salomon54d212e2017-03-21 14:22:38 -0400896 }
Greg Daniel524e28b2019-11-01 11:48:53 -0400897 fOpChains.emplace_back(std::move(op), processorAnalysis, clip, dstProxyView);
bsalomon512be532015-09-10 10:42:55 -0700898}
899
Greg Danielf41b2bd2019-08-22 16:19:24 -0400900void GrOpsTask::forwardCombine(const GrCaps& caps) {
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400901 SkASSERT(!this->isClosed());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400902 GrOP_INFO("opsTask: %d ForwardCombine %d ops:\n", this->uniqueID(), fOpChains.count());
Robert Phillips48567ac2017-06-01 08:46:00 -0400903
Brian Salomon588cec72018-11-14 13:56:37 -0500904 for (int i = 0; i < fOpChains.count() - 1; ++i) {
905 OpChain& chain = fOpChains[i];
Brian Osman788b9162020-02-07 10:36:46 -0500906 int maxCandidateIdx = std::min(i + kMaxOpChainDistance, fOpChains.count() - 1);
bsalomonaecc0182016-03-07 11:50:44 -0800907 int j = i + 1;
908 while (true) {
Brian Salomon588cec72018-11-14 13:56:37 -0500909 OpChain& candidate = fOpChains[j];
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500910 if (candidate.prependChain(&chain, caps, &fArenas, fAuditTrail)) {
bsalomonaecc0182016-03-07 11:50:44 -0800911 break;
912 }
Robert Phillipsc84c0302017-05-08 15:35:11 -0400913 // Stop traversing if we would cause a painter's order violation.
Brian Salomon588cec72018-11-14 13:56:37 -0500914 if (!can_reorder(chain.bounds(), candidate.bounds())) {
915 GrOP_INFO(
916 "\t\t%d: chain (%s head opID: %u) -> "
917 "Intersects with chain (%s, head opID: %u)\n",
918 i, chain.head()->name(), chain.head()->uniqueID(), candidate.head()->name(),
919 candidate.head()->uniqueID());
bsalomonaecc0182016-03-07 11:50:44 -0800920 break;
921 }
Brian Salomona7682c82018-10-24 10:04:37 -0400922 if (++j > maxCandidateIdx) {
Brian Salomon588cec72018-11-14 13:56:37 -0500923 GrOP_INFO("\t\t%d: chain (%s opID: %u) -> Reached max lookahead or end of array\n",
924 i, chain.head()->name(), chain.head()->uniqueID());
bsalomonaecc0182016-03-07 11:50:44 -0800925 break;
926 }
927 }
928 }
929}
930
Robert Phillips07f675d2020-11-16 13:44:01 -0500931GrRenderTask::ExpectedOutcome GrOpsTask::onMakeClosed(const GrCaps& caps,
932 SkIRect* targetUpdateBounds) {
Chris Dalton16a33c62019-09-24 22:19:17 -0600933 this->forwardCombine(caps);
934 if (!this->isNoOp()) {
Adlai Holler33d569e2020-06-16 14:30:08 -0400935 GrSurfaceProxy* proxy = this->target(0).proxy();
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400936 // Use the entire backing store bounds since the GPU doesn't clip automatically to the
937 // logical dimensions.
938 SkRect clippedContentBounds = proxy->backingStoreBoundsRect();
Adlai Holler33d569e2020-06-16 14:30:08 -0400939 // TODO: If we can fix up GLPrograms test to always intersect the target proxy bounds
Greg Daniel16f5c652019-10-29 11:26:01 -0400940 // then we can simply assert here that the bounds intersect.
Chris Dalton16a33c62019-09-24 22:19:17 -0600941 if (clippedContentBounds.intersect(fTotalBounds)) {
Greg Daniel94ed83f2019-09-27 13:05:43 -0400942 clippedContentBounds.roundOut(&fClippedContentBounds);
943 *targetUpdateBounds = fClippedContentBounds;
Chris Dalton16a33c62019-09-24 22:19:17 -0600944 return ExpectedOutcome::kTargetDirty;
945 }
946 }
947 return ExpectedOutcome::kTargetUnchanged;
948}