blob: a5426bc692b6419b1af715d338807abbc50a94f0 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
Greg Danielf41b2bd2019-08-22 16:19:24 -04002 * Copyright 2019 Google Inc.
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
Greg Danielf41b2bd2019-08-22 16:19:24 -04008#include "src/gpu/GrOpsTask.h"
Brian Salomon4d2d6f42019-07-26 14:15:11 -04009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/private/GrRecordingContext.h"
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 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/GrRenderTargetContext.h"
Chris Dalton674f77a2019-09-30 20:49:39 -060023#include "src/gpu/GrRenderTargetPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/GrResourceAllocator.h"
Chris Dalton674f77a2019-09-30 20:49:39 -060025#include "src/gpu/GrStencilAttachment.h"
Chris Dalton95d8ceb2019-07-30 11:17:59 -060026#include "src/gpu/GrTexturePriv.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040027#include "src/gpu/geometry/GrRect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/gpu/ops/GrClearOp.h"
Robert Phillipsf2361d22016-10-25 14:20:06 -040029
reed@google.comac10a2d2010-12-22 21:39:39 +000030////////////////////////////////////////////////////////////////////////////////
31
Brian Salomon09d994e2016-12-21 11:14:46 -050032// Experimentally we have found that most combining occurs within the first 10 comparisons.
Brian Salomon588cec72018-11-14 13:56:37 -050033static const int kMaxOpMergeDistance = 10;
34static const int kMaxOpChainDistance = 10;
35
36////////////////////////////////////////////////////////////////////////////////
37
Greg Daniel524e28b2019-11-01 11:48:53 -040038using DstProxyView = GrXferProcessor::DstProxyView;
Brian Salomon588cec72018-11-14 13:56:37 -050039
40////////////////////////////////////////////////////////////////////////////////
41
Brian Salomon3b8486a2020-04-21 12:43:26 -040042GrOpsTaskClosedObserver::~GrOpsTaskClosedObserver() = default;
43
44////////////////////////////////////////////////////////////////////////////////
45
Brian Salomon588cec72018-11-14 13:56:37 -050046static inline bool can_reorder(const SkRect& a, const SkRect& b) { return !GrRectsOverlap(a, b); }
47
48////////////////////////////////////////////////////////////////////////////////
49
Greg Danielf41b2bd2019-08-22 16:19:24 -040050inline GrOpsTask::OpChain::List::List(std::unique_ptr<GrOp> op)
Brian Salomon588cec72018-11-14 13:56:37 -050051 : fHead(std::move(op)), fTail(fHead.get()) {
52 this->validate();
53}
54
Greg Danielf41b2bd2019-08-22 16:19:24 -040055inline GrOpsTask::OpChain::List::List(List&& that) { *this = std::move(that); }
Brian Salomon588cec72018-11-14 13:56:37 -050056
Greg Danielf41b2bd2019-08-22 16:19:24 -040057inline GrOpsTask::OpChain::List& GrOpsTask::OpChain::List::operator=(List&& that) {
Brian Salomon588cec72018-11-14 13:56:37 -050058 fHead = std::move(that.fHead);
59 fTail = that.fTail;
60 that.fTail = nullptr;
61 this->validate();
62 return *this;
63}
64
Greg Danielf41b2bd2019-08-22 16:19:24 -040065inline std::unique_ptr<GrOp> GrOpsTask::OpChain::List::popHead() {
Brian Salomon588cec72018-11-14 13:56:37 -050066 SkASSERT(fHead);
67 auto temp = fHead->cutChain();
68 std::swap(temp, fHead);
69 if (!fHead) {
70 SkASSERT(fTail == temp.get());
71 fTail = nullptr;
72 }
73 return temp;
74}
75
Greg Danielf41b2bd2019-08-22 16:19:24 -040076inline std::unique_ptr<GrOp> GrOpsTask::OpChain::List::removeOp(GrOp* op) {
Brian Salomon588cec72018-11-14 13:56:37 -050077#ifdef SK_DEBUG
78 auto head = op;
79 while (head->prevInChain()) { head = head->prevInChain(); }
80 SkASSERT(head == fHead.get());
81#endif
82 auto prev = op->prevInChain();
83 if (!prev) {
84 SkASSERT(op == fHead.get());
85 return this->popHead();
86 }
87 auto temp = prev->cutChain();
88 if (auto next = temp->cutChain()) {
89 prev->chainConcat(std::move(next));
90 } else {
91 SkASSERT(fTail == op);
92 fTail = prev;
93 }
94 this->validate();
95 return temp;
96}
97
Greg Danielf41b2bd2019-08-22 16:19:24 -040098inline void GrOpsTask::OpChain::List::pushHead(std::unique_ptr<GrOp> op) {
Brian Salomon588cec72018-11-14 13:56:37 -050099 SkASSERT(op);
100 SkASSERT(op->isChainHead());
101 SkASSERT(op->isChainTail());
102 if (fHead) {
103 op->chainConcat(std::move(fHead));
104 fHead = std::move(op);
105 } else {
106 fHead = std::move(op);
107 fTail = fHead.get();
108 }
109}
110
Greg Danielf41b2bd2019-08-22 16:19:24 -0400111inline void GrOpsTask::OpChain::List::pushTail(std::unique_ptr<GrOp> op) {
Brian Salomon588cec72018-11-14 13:56:37 -0500112 SkASSERT(op->isChainTail());
113 fTail->chainConcat(std::move(op));
114 fTail = fTail->nextInChain();
115}
116
Greg Danielf41b2bd2019-08-22 16:19:24 -0400117inline void GrOpsTask::OpChain::List::validate() const {
Brian Salomon588cec72018-11-14 13:56:37 -0500118#ifdef SK_DEBUG
119 if (fHead) {
120 SkASSERT(fTail);
121 fHead->validateChain(fTail);
122 }
123#endif
124}
125
126////////////////////////////////////////////////////////////////////////////////
127
Greg Danielf41b2bd2019-08-22 16:19:24 -0400128GrOpsTask::OpChain::OpChain(std::unique_ptr<GrOp> op,
129 GrProcessorSet::Analysis processorAnalysis,
Greg Daniel524e28b2019-11-01 11:48:53 -0400130 GrAppliedClip* appliedClip, const DstProxyView* dstProxyView)
Chris Dalton945ee652019-01-23 09:10:36 -0700131 : fList{std::move(op)}
132 , fProcessorAnalysis(processorAnalysis)
133 , fAppliedClip(appliedClip) {
134 if (fProcessorAnalysis.requiresDstTexture()) {
Greg Daniel524e28b2019-11-01 11:48:53 -0400135 SkASSERT(dstProxyView && dstProxyView->proxy());
136 fDstProxyView = *dstProxyView;
Brian Salomon588cec72018-11-14 13:56:37 -0500137 }
138 fBounds = fList.head()->bounds();
139}
140
Greg Danielf41b2bd2019-08-22 16:19:24 -0400141void GrOpsTask::OpChain::visitProxies(const GrOp::VisitProxyFunc& func) const {
Brian Salomon588cec72018-11-14 13:56:37 -0500142 if (fList.empty()) {
143 return;
144 }
145 for (const auto& op : GrOp::ChainRange<>(fList.head())) {
Chris Dalton1706cbf2019-05-21 19:35:29 -0600146 op.visitProxies(func);
Brian Salomon588cec72018-11-14 13:56:37 -0500147 }
Greg Daniel524e28b2019-11-01 11:48:53 -0400148 if (fDstProxyView.proxy()) {
149 func(fDstProxyView.proxy(), GrMipMapped::kNo);
Brian Salomon588cec72018-11-14 13:56:37 -0500150 }
151 if (fAppliedClip) {
152 fAppliedClip->visitProxies(func);
153 }
154}
155
Greg Danielf41b2bd2019-08-22 16:19:24 -0400156void GrOpsTask::OpChain::deleteOps(GrOpMemoryPool* pool) {
Brian Salomon588cec72018-11-14 13:56:37 -0500157 while (!fList.empty()) {
158 pool->release(fList.popHead());
159 }
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) {
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500201 arenas->opMemoryPool()->release(chainB.popHead());
Brian Salomon588cec72018-11-14 13:56:37 -0500202 } else {
203 // We merged the contents of b's head into a. We will replace b's head with a in
204 // chain b.
205 SkASSERT(canForwardMerge);
206 if (a == origATail) {
207 origATail = a->prevInChain();
208 }
209 std::unique_ptr<GrOp> detachedA = chainA.removeOp(a);
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500210 arenas->opMemoryPool()->release(chainB.popHead());
Brian Salomon588cec72018-11-14 13:56:37 -0500211 chainB.pushHead(std::move(detachedA));
212 if (chainA.empty()) {
213 // We merged all the nodes in chain a to chain b.
214 return chainB;
215 }
216 }
217 break;
218 } else {
219 if (++numMergeChecks == kMaxOpMergeDistance) {
220 break;
221 }
222 forwardMergeBounds.joinNonEmptyArg(a->bounds());
223 canBackwardMerge =
224 canBackwardMerge && can_reorder(chainB.head()->bounds(), a->bounds());
225 a = a->prevInChain();
226 }
227 }
228 // If we weren't able to merge b's head then pop b's head from chain b and make it the new
229 // tail of a.
230 if (!merged) {
231 chainA.pushTail(chainB.popHead());
232 skipBounds.joinNonEmptyArg(chainA.tail()->bounds());
233 }
234 } while (!chainB.empty());
235 return chainA;
236}
237
Chris Dalton945ee652019-01-23 09:10:36 -0700238// Attempts to concatenate the given chain onto our own and merge ops across the chains. Returns
239// whether the operation succeeded. On success, the provided list will be returned empty.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400240bool GrOpsTask::OpChain::tryConcat(
Greg Daniel524e28b2019-11-01 11:48:53 -0400241 List* list, GrProcessorSet::Analysis processorAnalysis, const DstProxyView& dstProxyView,
Chris Dalton945ee652019-01-23 09:10:36 -0700242 const GrAppliedClip* appliedClip, const SkRect& bounds, const GrCaps& caps,
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500243 GrRecordingContext::Arenas* arenas, GrAuditTrail* auditTrail) {
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700244 SkASSERT(!fList.empty());
245 SkASSERT(!list->empty());
Greg Daniel524e28b2019-11-01 11:48:53 -0400246 SkASSERT(fProcessorAnalysis.requiresDstTexture() == SkToBool(fDstProxyView.proxy()));
247 SkASSERT(processorAnalysis.requiresDstTexture() == SkToBool(dstProxyView.proxy()));
Brian Salomon588cec72018-11-14 13:56:37 -0500248 // All returns use explicit tuple constructor rather than {a, b} to work around old GCC bug.
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700249 if (fList.head()->classID() != list->head()->classID() ||
250 SkToBool(fAppliedClip) != SkToBool(appliedClip) ||
251 (fAppliedClip && *fAppliedClip != *appliedClip) ||
Chris Dalton945ee652019-01-23 09:10:36 -0700252 (fProcessorAnalysis.requiresNonOverlappingDraws() !=
253 processorAnalysis.requiresNonOverlappingDraws()) ||
254 (fProcessorAnalysis.requiresNonOverlappingDraws() &&
255 // Non-overlaping draws are only required when Ganesh will either insert a barrier,
256 // or read back a new dst texture between draws. In either case, we can neither
257 // chain nor combine overlapping Ops.
258 GrRectsTouchOrOverlap(fBounds, bounds)) ||
259 (fProcessorAnalysis.requiresDstTexture() != processorAnalysis.requiresDstTexture()) ||
Greg Daniel524e28b2019-11-01 11:48:53 -0400260 (fProcessorAnalysis.requiresDstTexture() && fDstProxyView != dstProxyView)) {
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700261 return false;
Brian Salomon588cec72018-11-14 13:56:37 -0500262 }
Chris Daltonee21e6b2019-01-22 14:04:43 -0700263
Brian Salomon588cec72018-11-14 13:56:37 -0500264 SkDEBUGCODE(bool first = true;)
265 do {
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500266 switch (fList.tail()->combineIfPossible(list->head(), arenas, caps)) {
Brian Salomon588cec72018-11-14 13:56:37 -0500267 case GrOp::CombineResult::kCannotCombine:
268 // If an op supports chaining then it is required that chaining is transitive and
269 // that if any two ops in two different chains can merge then the two chains
270 // may also be chained together. Thus, we should only hit this on the first
271 // iteration.
272 SkASSERT(first);
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700273 return false;
Brian Salomon588cec72018-11-14 13:56:37 -0500274 case GrOp::CombineResult::kMayChain:
Adlai Holler5ba50af2020-04-29 21:11:14 -0400275 fList = DoConcat(std::move(fList), std::exchange(*list, List()), caps, arenas,
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500276 auditTrail);
Chris Daltonee21e6b2019-01-22 14:04:43 -0700277 // The above exchange cleared out 'list'. The list needs to be empty now for the
278 // loop to terminate.
279 SkASSERT(list->empty());
280 break;
Brian Salomon588cec72018-11-14 13:56:37 -0500281 case GrOp::CombineResult::kMerged: {
Robert Phillips9548c3b422019-01-08 12:35:43 -0500282 GrOP_INFO("\t\t: (%s opID: %u) -> Combining with (%s, opID: %u)\n",
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700283 list->tail()->name(), list->tail()->uniqueID(), list->head()->name(),
284 list->head()->uniqueID());
285 GR_AUDIT_TRAIL_OPS_RESULT_COMBINED(auditTrail, fList.tail(), list->head());
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500286 arenas->opMemoryPool()->release(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({});
Brian Salomon588cec72018-11-14 13:56:37 -0500314 if (that->fAppliedClip) {
315 for (int i = 0; i < that->fAppliedClip->numClipCoverageFragmentProcessors(); ++i) {
316 that->fAppliedClip->detachClipCoverageFragmentProcessor(i);
317 }
318 }
319 this->validate();
320 return true;
321}
322
Greg Danielf41b2bd2019-08-22 16:19:24 -0400323std::unique_ptr<GrOp> GrOpsTask::OpChain::appendOp(
Chris Dalton945ee652019-01-23 09:10:36 -0700324 std::unique_ptr<GrOp> op, GrProcessorSet::Analysis processorAnalysis,
Greg Daniel524e28b2019-11-01 11:48:53 -0400325 const DstProxyView* dstProxyView, const GrAppliedClip* appliedClip, const GrCaps& caps,
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500326 GrRecordingContext::Arenas* arenas, GrAuditTrail* auditTrail) {
Greg Daniel524e28b2019-11-01 11:48:53 -0400327 const GrXferProcessor::DstProxyView noDstProxyView;
328 if (!dstProxyView) {
329 dstProxyView = &noDstProxyView;
Brian Salomon588cec72018-11-14 13:56:37 -0500330 }
331 SkASSERT(op->isChainHead() && op->isChainTail());
332 SkRect opBounds = op->bounds();
333 List chain(std::move(op));
Chris Dalton945ee652019-01-23 09:10:36 -0700334 if (!this->tryConcat(
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500335 &chain, processorAnalysis, *dstProxyView, appliedClip, opBounds, caps,
336 arenas, auditTrail)) {
Brian Salomon588cec72018-11-14 13:56:37 -0500337 // append failed, give the op back to the caller.
338 this->validate();
339 return chain.popHead();
340 }
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700341
342 SkASSERT(chain.empty());
Brian Salomon588cec72018-11-14 13:56:37 -0500343 this->validate();
344 return nullptr;
345}
346
Greg Danielf41b2bd2019-08-22 16:19:24 -0400347inline void GrOpsTask::OpChain::validate() const {
Brian Salomon588cec72018-11-14 13:56:37 -0500348#ifdef SK_DEBUG
349 fList.validate();
350 for (const auto& op : GrOp::ChainRange<>(fList.head())) {
351 // Not using SkRect::contains because we allow empty rects.
352 SkASSERT(fBounds.fLeft <= op.bounds().fLeft && fBounds.fTop <= op.bounds().fTop &&
353 fBounds.fRight >= op.bounds().fRight && fBounds.fBottom >= op.bounds().fBottom);
354 }
355#endif
356}
357
358////////////////////////////////////////////////////////////////////////////////
bsalomon489147c2015-12-14 12:13:09 -0800359
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500360GrOpsTask::GrOpsTask(GrRecordingContext::Arenas arenas,
Greg Daniel16f5c652019-10-29 11:26:01 -0400361 GrSurfaceProxyView view,
Greg Danielf41b2bd2019-08-22 16:19:24 -0400362 GrAuditTrail* auditTrail)
Greg Daniel16f5c652019-10-29 11:26:01 -0400363 : GrRenderTask(std::move(view))
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500364 , fArenas(arenas)
Greg Danielf41b2bd2019-08-22 16:19:24 -0400365 , fAuditTrail(auditTrail)
Robert Phillipsb6deea82017-05-11 14:14:30 -0400366 SkDEBUGCODE(, fNumClips(0)) {
Greg Daniel16f5c652019-10-29 11:26:01 -0400367 fTargetView.proxy()->setLastRenderTask(this);
bsalomon4061b122015-05-29 10:26:19 -0700368}
369
Greg Danielf41b2bd2019-08-22 16:19:24 -0400370void GrOpsTask::deleteOps() {
Brian Salomon588cec72018-11-14 13:56:37 -0500371 for (auto& chain : fOpChains) {
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500372 chain.deleteOps(fArenas.opMemoryPool());
Robert Phillipsc994a932018-06-19 13:09:54 -0400373 }
Brian Salomon588cec72018-11-14 13:56:37 -0500374 fOpChains.reset();
Robert Phillipsc994a932018-06-19 13:09:54 -0400375}
376
Greg Danielf41b2bd2019-08-22 16:19:24 -0400377GrOpsTask::~GrOpsTask() {
Robert Phillipsc994a932018-06-19 13:09:54 -0400378 this->deleteOps();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000379}
380
Brian Salomonf1c9eae2020-05-01 15:00:34 -0400381void GrOpsTask::removeClosedObserver(GrOpsTaskClosedObserver* observer) {
382 SkASSERT(observer);
383 for (int i = 0; i < fClosedObservers.count(); ++i) {
384 if (fClosedObservers[i] == observer) {
385 fClosedObservers.removeShuffle(i);
386 --i;
387 }
388 }
389}
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000390
Greg Danielf41b2bd2019-08-22 16:19:24 -0400391void GrOpsTask::endFlush() {
392 fLastClipStackGenID = SK_InvalidUniqueID;
393 this->deleteOps();
394 fClipAllocator.reset();
Chris Dalton706a6ff2017-11-29 22:01:06 -0700395
Greg Daniel16f5c652019-10-29 11:26:01 -0400396 GrSurfaceProxy* proxy = fTargetView.proxy();
397 if (proxy && this == proxy->getLastRenderTask()) {
398 proxy->setLastRenderTask(nullptr);
Greg Danielf21bf9e2019-08-22 20:12:20 +0000399 }
Greg Danielf41b2bd2019-08-22 16:19:24 -0400400
Greg Daniel16f5c652019-10-29 11:26:01 -0400401 fTargetView.reset();
Greg Danielf41b2bd2019-08-22 16:19:24 -0400402 fDeferredProxies.reset();
Greg Danielb20d7e52019-09-03 13:54:39 -0400403 fSampledProxies.reset();
Greg Danielf41b2bd2019-08-22 16:19:24 -0400404 fAuditTrail = nullptr;
Greg Danielf21bf9e2019-08-22 20:12:20 +0000405}
406
Robert Phillips29f38542019-10-16 09:20:25 -0400407void GrOpsTask::onPrePrepare(GrRecordingContext* context) {
Robert Phillips7327c9d2019-10-08 16:32:56 -0400408 SkASSERT(this->isClosed());
409#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
410 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
411#endif
412 // TODO: remove the check for discard here once reduced op splitting is turned on. Currently we
413 // can end up with GrOpsTasks that only have a discard load op and no ops. For vulkan validation
414 // we need to keep that discard and not drop it. Once we have reduce op list splitting enabled
415 // we shouldn't end up with GrOpsTasks with only discard.
416 if (this->isNoOp() || (fClippedContentBounds.isEmpty() && fColorLoadOp != GrLoadOp::kDiscard)) {
417 return;
418 }
419
420 for (const auto& chain : fOpChains) {
421 if (chain.shouldExecute()) {
Robert Phillips8053c972019-11-21 10:44:53 -0500422 chain.head()->prePrepare(context,
423 &fTargetView,
424 chain.appliedClip(),
425 chain.dstProxyView());
Robert Phillips7327c9d2019-10-08 16:32:56 -0400426 }
427 }
428}
429
Greg Danielf41b2bd2019-08-22 16:19:24 -0400430void GrOpsTask::onPrepare(GrOpFlushState* flushState) {
Greg Daniel16f5c652019-10-29 11:26:01 -0400431 SkASSERT(fTargetView.proxy()->peekRenderTarget());
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400432 SkASSERT(this->isClosed());
Stan Iliev2af578d2017-08-16 13:00:28 -0400433#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
Brian Salomon5f394272019-07-02 14:07:49 -0400434 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Stan Iliev2af578d2017-08-16 13:00:28 -0400435#endif
Greg Daniel94ed83f2019-09-27 13:05:43 -0400436 // TODO: remove the check for discard here once reduced op splitting is turned on. Currently we
437 // can end up with GrOpsTasks that only have a discard load op and no ops. For vulkan validation
438 // we need to keep that discard and not drop it. Once we have reduce op list splitting enabled
439 // we shouldn't end up with GrOpsTasks with only discard.
440 if (this->isNoOp() || (fClippedContentBounds.isEmpty() && fColorLoadOp != GrLoadOp::kDiscard)) {
441 return;
442 }
robertphillipsa106c622015-10-16 09:07:06 -0700443
Greg Danielb20d7e52019-09-03 13:54:39 -0400444 flushState->setSampledProxyArray(&fSampledProxies);
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500445 // Loop over the ops that haven't yet been prepared.
Brian Salomon588cec72018-11-14 13:56:37 -0500446 for (const auto& chain : fOpChains) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400447 if (chain.shouldExecute()) {
Stan Iliev2af578d2017-08-16 13:00:28 -0400448#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
Brian Salomon5f394272019-07-02 14:07:49 -0400449 TRACE_EVENT0("skia.gpu", chain.head()->name());
Stan Iliev2af578d2017-08-16 13:00:28 -0400450#endif
Robert Phillips901aff02019-10-08 12:32:56 -0400451 GrOpFlushState::OpArgs opArgs(chain.head(),
Greg Daniel16f5c652019-10-29 11:26:01 -0400452 &fTargetView,
Robert Phillips901aff02019-10-08 12:32:56 -0400453 chain.appliedClip(),
Greg Daniel524e28b2019-11-01 11:48:53 -0400454 chain.dstProxyView());
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.
460 // chain.head()->prePrepare(flushState->gpu()->getContext(), &fTargetView,
461 // 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 Daniel4a0d36d2019-09-30 12:24:36 -0400471static GrOpsRenderPass* create_render_pass(
Robert Phillips96f22372020-05-20 12:31:18 -0400472 GrGpu* gpu, GrRenderTarget* rt, GrStencilAttachment* stencil, GrSurfaceOrigin origin,
473 const SkIRect& bounds, GrLoadOp colorLoadOp, const SkPMColor4f& loadClearColor,
474 GrLoadOp stencilLoadOp, GrStoreOp stencilStoreOp,
475 const SkTArray<GrSurfaceProxy*, true>& sampledProxies) {
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400476 const GrOpsRenderPass::LoadAndStoreInfo kColorLoadStoreInfo {
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400477 colorLoadOp,
478 GrStoreOp::kStore,
479 loadClearColor
Robert Phillips178ce3e2017-04-13 09:15:47 -0400480 };
481
Robert Phillips95214472017-08-08 18:00:03 -0400482 // TODO:
483 // We would like to (at this level) only ever clear & discard. We would need
Greg Danielf41b2bd2019-08-22 16:19:24 -0400484 // to stop splitting up higher level OpsTasks for copyOps to achieve that.
Robert Phillips95214472017-08-08 18:00:03 -0400485 // Note: we would still need SB loads and stores but they would happen at a
486 // lower level (inside the VK command buffer).
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400487 const GrOpsRenderPass::StencilLoadAndStoreInfo stencilLoadAndStoreInfo {
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400488 stencilLoadOp,
Chris Dalton674f77a2019-09-30 20:49:39 -0600489 stencilStoreOp,
Robert Phillips95214472017-08-08 18:00:03 -0400490 };
491
Robert Phillips96f22372020-05-20 12:31:18 -0400492 return gpu->getOpsRenderPass(rt, stencil, origin, bounds,
493 kColorLoadStoreInfo, stencilLoadAndStoreInfo, sampledProxies);
Robert Phillips178ce3e2017-04-13 09:15:47 -0400494}
495
Brian Salomon25a88092016-12-01 09:36:50 -0500496// TODO: this is where GrOp::renderTarget is used (which is fine since it
Robert Phillips294870f2016-11-11 12:38:40 -0500497// is at flush time). However, we need to store the RenderTargetProxy in the
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500498// Ops and instantiate them here.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400499bool GrOpsTask::onExecute(GrOpFlushState* flushState) {
Greg Daniel94ed83f2019-09-27 13:05:43 -0400500 // TODO: remove the check for discard here once reduced op splitting is turned on. Currently we
501 // can end up with GrOpsTasks that only have a discard load op and no ops. For vulkan validation
502 // we need to keep that discard and not drop it. Once we have reduce op list splitting enabled
503 // we shouldn't end up with GrOpsTasks with only discard.
504 if (this->isNoOp() || (fClippedContentBounds.isEmpty() && fColorLoadOp != GrLoadOp::kDiscard)) {
bsalomondc438982016-08-31 11:53:49 -0700505 return false;
egdanielb4021cf2016-07-28 08:53:07 -0700506 }
Robert Phillips4a395042017-04-24 16:27:17 +0000507
Chris Dalton0b68dda2019-11-07 21:08:03 -0700508 SkASSERT(fTargetView.proxy());
509 GrRenderTargetProxy* proxy = fTargetView.proxy()->asRenderTargetProxy();
Greg Daniel16f5c652019-10-29 11:26:01 -0400510 SkASSERT(proxy);
Brian Salomon5f394272019-07-02 14:07:49 -0400511 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400512
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500513 // Make sure load ops are not kClear if the GPU needs to use draws for clears
514 SkASSERT(fColorLoadOp != GrLoadOp::kClear ||
515 !flushState->gpu()->caps()->performColorClearsAsDraws());
Chris Dalton674f77a2019-09-30 20:49:39 -0600516
517 const GrCaps& caps = *flushState->gpu()->caps();
Greg Daniel16f5c652019-10-29 11:26:01 -0400518 GrRenderTarget* renderTarget = proxy->peekRenderTarget();
Chris Dalton674f77a2019-09-30 20:49:39 -0600519 SkASSERT(renderTarget);
Chris Dalton0b68dda2019-11-07 21:08:03 -0700520
521 GrStencilAttachment* stencil = nullptr;
522 if (int numStencilSamples = proxy->numStencilSamples()) {
523 if (!flushState->resourceProvider()->attachStencilAttachment(
524 renderTarget, numStencilSamples)) {
525 SkDebugf("WARNING: failed to attach a stencil buffer. Rendering will be skipped.\n");
526 return false;
527 }
528 stencil = renderTarget->renderTargetPriv().getStencilAttachment();
529 }
530
Chris Daltonf00b95b2019-11-07 21:14:41 -0700531 SkASSERT(!stencil || stencil->numSamples() == proxy->numStencilSamples());
Chris Dalton674f77a2019-09-30 20:49:39 -0600532
533 GrLoadOp stencilLoadOp;
534 switch (fInitialStencilContent) {
535 case StencilContent::kDontCare:
536 stencilLoadOp = GrLoadOp::kDiscard;
537 break;
538 case StencilContent::kUserBitsCleared:
539 SkASSERT(!caps.performStencilClearsAsDraws());
540 SkASSERT(stencil);
541 if (caps.discardStencilValuesAfterRenderPass()) {
542 // Always clear the stencil if it is being discarded after render passes. This is
543 // also an optimization because we are on a tiler and it avoids loading the values
544 // from memory.
545 stencilLoadOp = GrLoadOp::kClear;
546 break;
547 }
548 if (!stencil->hasPerformedInitialClear()) {
549 stencilLoadOp = GrLoadOp::kClear;
550 stencil->markHasPerformedInitialClear();
551 break;
552 }
553 // renderTargetContexts are required to leave the user stencil bits in a cleared state
554 // once finished, meaning the stencil values will always remain cleared after the
555 // initial clear. Just fall through to reloading the existing (cleared) stencil values
556 // from memory.
557 case StencilContent::kPreserved:
558 SkASSERT(stencil);
559 stencilLoadOp = GrLoadOp::kLoad;
560 break;
561 }
562
563 // NOTE: If fMustPreserveStencil is set, then we are executing a renderTargetContext that split
564 // its opsTask.
565 //
566 // FIXME: We don't currently flag render passes that don't use stencil at all. In that case
567 // their store op might be "discard", and we currently make the assumption that a discard will
568 // not invalidate what's already in main memory. This is probably ok for now, but certainly
569 // something we want to address soon.
570 GrStoreOp stencilStoreOp = (caps.discardStencilValuesAfterRenderPass() && !fMustPreserveStencil)
571 ? GrStoreOp::kDiscard
572 : GrStoreOp::kStore;
573
Greg Daniel4a0d36d2019-09-30 12:24:36 -0400574 GrOpsRenderPass* renderPass = create_render_pass(
Robert Phillips96f22372020-05-20 12:31:18 -0400575 flushState->gpu(), proxy->peekRenderTarget(), stencil, fTargetView.origin(),
Chris Dalton674f77a2019-09-30 20:49:39 -0600576 fClippedContentBounds, fColorLoadOp, fLoadClearColor, stencilLoadOp, stencilStoreOp,
577 fSampledProxies);
Greg Danielfa3adf72019-11-07 09:53:41 -0500578 if (!renderPass) {
579 return false;
580 }
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400581 flushState->setOpsRenderPass(renderPass);
582 renderPass->begin();
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400583
584 // Draw all the generated geometry.
Brian Salomon588cec72018-11-14 13:56:37 -0500585 for (const auto& chain : fOpChains) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400586 if (!chain.shouldExecute()) {
bsalomonaecc0182016-03-07 11:50:44 -0800587 continue;
588 }
Stan Iliev2af578d2017-08-16 13:00:28 -0400589#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
Brian Salomon5f394272019-07-02 14:07:49 -0400590 TRACE_EVENT0("skia.gpu", chain.head()->name());
Stan Iliev2af578d2017-08-16 13:00:28 -0400591#endif
Robert Phillips178ce3e2017-04-13 09:15:47 -0400592
Robert Phillips405413f2019-10-04 10:39:28 -0400593 GrOpFlushState::OpArgs opArgs(chain.head(),
Greg Daniel16f5c652019-10-29 11:26:01 -0400594 &fTargetView,
Robert Phillips405413f2019-10-04 10:39:28 -0400595 chain.appliedClip(),
Greg Daniel524e28b2019-11-01 11:48:53 -0400596 chain.dstProxyView());
Robert Phillips178ce3e2017-04-13 09:15:47 -0400597
Brian Salomon29b60c92017-10-31 14:42:10 -0400598 flushState->setOpArgs(&opArgs);
Brian Salomon588cec72018-11-14 13:56:37 -0500599 chain.head()->execute(flushState, chain.bounds());
Brian Salomon29b60c92017-10-31 14:42:10 -0400600 flushState->setOpArgs(nullptr);
bsalomon512be532015-09-10 10:42:55 -0700601 }
Robert Phillips178ce3e2017-04-13 09:15:47 -0400602
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400603 renderPass->end();
604 flushState->gpu()->submit(renderPass);
605 flushState->setOpsRenderPass(nullptr);
ethannicholas22793252016-01-30 09:59:10 -0800606
bsalomondc438982016-08-31 11:53:49 -0700607 return true;
bsalomona73239a2015-04-28 13:35:17 -0700608}
609
Greg Danielf41b2bd2019-08-22 16:19:24 -0400610void GrOpsTask::setColorLoadOp(GrLoadOp op, const SkPMColor4f& color) {
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500611 fColorLoadOp = op;
612 fLoadClearColor = color;
Chris Dalton16a33c62019-09-24 22:19:17 -0600613 if (GrLoadOp::kClear == fColorLoadOp) {
Greg Daniel16f5c652019-10-29 11:26:01 -0400614 GrSurfaceProxy* proxy = fTargetView.proxy();
615 SkASSERT(proxy);
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400616 fTotalBounds = proxy->backingStoreBoundsRect();
Chris Dalton16a33c62019-09-24 22:19:17 -0600617 }
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500618}
619
Greg Danielf41b2bd2019-08-22 16:19:24 -0400620bool GrOpsTask::resetForFullscreenClear(CanDiscardPreviousOps canDiscardPreviousOps) {
Chris Dalton6b982802019-06-27 13:53:46 -0600621 // If we previously recorded a wait op, we cannot delete the wait op. Until we track the wait
622 // ops separately from normal ops, we have to avoid clearing out any ops in this case as well.
623 if (fHasWaitOp) {
624 canDiscardPreviousOps = CanDiscardPreviousOps::kNo;
625 }
626
627 if (CanDiscardPreviousOps::kYes == canDiscardPreviousOps || this->isEmpty()) {
Robert Phillipsc994a932018-06-19 13:09:54 -0400628 this->deleteOps();
Brian Osman099fa0f2017-10-02 16:38:32 -0400629 fDeferredProxies.reset();
Greg Danielb20d7e52019-09-03 13:54:39 -0400630 fSampledProxies.reset();
Greg Daniel070cbaf2019-01-03 17:35:54 -0500631
Greg Danielf41b2bd2019-08-22 16:19:24 -0400632 // If the opsTask is using a render target which wraps a vulkan command buffer, we can't do
633 // a clear load since we cannot change the render pass that we are using. Thus we fall back
634 // to making a clear op in this case.
Greg Daniel16f5c652019-10-29 11:26:01 -0400635 return !fTargetView.asRenderTargetProxy()->wrapsVkSecondaryCB();
bsalomonfd8d0132016-08-11 11:25:33 -0700636 }
Robert Phillips380b90c2017-08-30 07:41:07 -0400637
Greg Danielf41b2bd2019-08-22 16:19:24 -0400638 // Could not empty the task, so an op must be added to handle the clear
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500639 return false;
bsalomon9f129de2016-08-10 16:31:05 -0700640}
641
Greg Danielf41b2bd2019-08-22 16:19:24 -0400642void GrOpsTask::discard() {
643 // Discard calls to in-progress opsTasks are ignored. Calls at the start update the
644 // opsTasks' color & stencil load ops.
645 if (this->isEmpty()) {
646 fColorLoadOp = GrLoadOp::kDiscard;
Chris Dalton674f77a2019-09-30 20:49:39 -0600647 fInitialStencilContent = StencilContent::kDontCare;
Chris Dalton16a33c62019-09-24 22:19:17 -0600648 fTotalBounds.setEmpty();
Greg Danielf41b2bd2019-08-22 16:19:24 -0400649 }
650}
651
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000652////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000653
Greg Danielf41b2bd2019-08-22 16:19:24 -0400654#ifdef SK_DEBUG
Greg Danielf41b2bd2019-08-22 16:19:24 -0400655void GrOpsTask::dump(bool printDependencies) const {
656 GrRenderTask::dump(printDependencies);
657
Chris Dalton674f77a2019-09-30 20:49:39 -0600658 SkDebugf("fColorLoadOp: ");
659 switch (fColorLoadOp) {
660 case GrLoadOp::kLoad:
661 SkDebugf("kLoad\n");
662 break;
663 case GrLoadOp::kClear:
664 SkDebugf("kClear (0x%x)\n", fLoadClearColor.toBytes_RGBA());
665 break;
666 case GrLoadOp::kDiscard:
667 SkDebugf("kDiscard\n");
668 break;
669 }
670
671 SkDebugf("fInitialStencilContent: ");
672 switch (fInitialStencilContent) {
673 case StencilContent::kDontCare:
674 SkDebugf("kDontCare\n");
675 break;
676 case StencilContent::kUserBitsCleared:
677 SkDebugf("kUserBitsCleared\n");
678 break;
679 case StencilContent::kPreserved:
680 SkDebugf("kPreserved\n");
681 break;
682 }
Greg Danielf41b2bd2019-08-22 16:19:24 -0400683
684 SkDebugf("ops (%d):\n", fOpChains.count());
685 for (int i = 0; i < fOpChains.count(); ++i) {
686 SkDebugf("*******************************\n");
687 if (!fOpChains[i].head()) {
688 SkDebugf("%d: <combined forward or failed instantiation>\n", i);
689 } else {
690 SkDebugf("%d: %s\n", i, fOpChains[i].head()->name());
691 SkRect bounds = fOpChains[i].bounds();
692 SkDebugf("ClippedBounds: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", bounds.fLeft,
693 bounds.fTop, bounds.fRight, bounds.fBottom);
694 for (const auto& op : GrOp::ChainRange<>(fOpChains[i].head())) {
695 SkString info = SkTabString(op.dumpInfo(), 1);
696 SkDebugf("%s\n", info.c_str());
697 bounds = op.bounds();
698 SkDebugf("\tClippedBounds: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", bounds.fLeft,
699 bounds.fTop, bounds.fRight, bounds.fBottom);
700 }
701 }
702 }
703}
704
Michael Ludwigfcdd0612019-11-25 08:34:31 -0500705void GrOpsTask::visitProxies_debugOnly(const GrOp::VisitProxyFunc& func) const {
706 auto textureFunc = [ func ] (GrSurfaceProxy* tex, GrMipMapped mipmapped) {
Greg Danieldcf9ca12019-08-27 14:30:21 -0400707 func(tex, mipmapped);
708 };
709
Greg Danielf41b2bd2019-08-22 16:19:24 -0400710 for (const OpChain& chain : fOpChains) {
Greg Danieldcf9ca12019-08-27 14:30:21 -0400711 chain.visitProxies(textureFunc);
Greg Danielf41b2bd2019-08-22 16:19:24 -0400712 }
713}
714
715#endif
716
717////////////////////////////////////////////////////////////////////////////////
718
719bool GrOpsTask::onIsUsed(GrSurfaceProxy* proxyToCheck) const {
720 bool used = false;
721
722 auto visit = [ proxyToCheck, &used ] (GrSurfaceProxy* p, GrMipMapped) {
723 if (p == proxyToCheck) {
724 used = true;
725 }
726 };
727 for (const OpChain& recordedOp : fOpChains) {
728 recordedOp.visitProxies(visit);
729 }
730
731 return used;
732}
733
734void GrOpsTask::handleInternalAllocationFailure() {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500735 bool hasUninstantiatedProxy = false;
Chris Dalton7eb5c0f2019-05-23 15:15:47 -0600736 auto checkInstantiation = [&hasUninstantiatedProxy](GrSurfaceProxy* p, GrMipMapped) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400737 if (!p->isInstantiated()) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500738 hasUninstantiatedProxy = true;
739 }
740 };
Brian Salomon588cec72018-11-14 13:56:37 -0500741 for (OpChain& recordedOp : fOpChains) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500742 hasUninstantiatedProxy = false;
Chris Dalton1706cbf2019-05-21 19:35:29 -0600743 recordedOp.visitProxies(checkInstantiation);
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500744 if (hasUninstantiatedProxy) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400745 recordedOp.setSkipExecuteFlag();
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500746 }
747 }
748}
749
Greg Danielf41b2bd2019-08-22 16:19:24 -0400750void GrOpsTask::gatherProxyIntervals(GrResourceAllocator* alloc) const {
Robert Phillips51b20f22017-12-01 15:32:35 -0500751 for (int i = 0; i < fDeferredProxies.count(); ++i) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400752 SkASSERT(!fDeferredProxies[i]->isInstantiated());
Robert Phillips51b20f22017-12-01 15:32:35 -0500753 // We give all the deferred proxies a write usage at the very start of flushing. This
754 // locks them out of being reused for the entire flush until they are read - and then
755 // they can be recycled. This is a bit unfortunate because a flush can proceed in waves
756 // with sub-flushes. The deferred proxies only need to be pinned from the start of
757 // the sub-flush in which they appear.
Robert Phillipsc73666f2019-04-24 08:49:48 -0400758 alloc->addInterval(fDeferredProxies[i], 0, 0, GrResourceAllocator::ActualUse::kNo);
Robert Phillips51b20f22017-12-01 15:32:35 -0500759 }
760
Greg Daniel16f5c652019-10-29 11:26:01 -0400761 GrSurfaceProxy* targetProxy = fTargetView.proxy();
762
Greg Danielf41b2bd2019-08-22 16:19:24 -0400763 // Add the interval for all the writes to this GrOpsTasks's target
Brian Salomon588cec72018-11-14 13:56:37 -0500764 if (fOpChains.count()) {
Robert Phillips3bf3d4a2019-03-27 07:09:09 -0400765 unsigned int cur = alloc->curOp();
766
Greg Daniel16f5c652019-10-29 11:26:01 -0400767 alloc->addInterval(targetProxy, cur, cur + fOpChains.count() - 1,
Robert Phillipsc73666f2019-04-24 08:49:48 -0400768 GrResourceAllocator::ActualUse::kYes);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500769 } else {
770 // This can happen if there is a loadOp (e.g., a clear) but no other draws. In this case we
771 // still need to add an interval for the destination so we create a fake op# for
772 // the missing clear op.
Greg Daniel16f5c652019-10-29 11:26:01 -0400773 alloc->addInterval(targetProxy, alloc->curOp(), alloc->curOp(),
Robert Phillipsc73666f2019-04-24 08:49:48 -0400774 GrResourceAllocator::ActualUse::kYes);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500775 alloc->incOps();
776 }
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400777
Chris Dalton7eb5c0f2019-05-23 15:15:47 -0600778 auto gather = [ alloc SkDEBUGCODE(, this) ] (GrSurfaceProxy* p, GrMipMapped) {
Robert Phillipsc73666f2019-04-24 08:49:48 -0400779 alloc->addInterval(p, alloc->curOp(), alloc->curOp(), GrResourceAllocator::ActualUse::kYes
Greg Daniel16f5c652019-10-29 11:26:01 -0400780 SkDEBUGCODE(, fTargetView.proxy() == p));
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400781 };
Brian Salomon588cec72018-11-14 13:56:37 -0500782 for (const OpChain& recordedOp : fOpChains) {
Chris Dalton1706cbf2019-05-21 19:35:29 -0600783 recordedOp.visitProxies(gather);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500784
Robert Phillips3bf3d4a2019-03-27 07:09:09 -0400785 // 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 -0500786 // keep all the math consistent.
787 alloc->incOps();
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400788 }
789}
790
Greg Danielf41b2bd2019-08-22 16:19:24 -0400791void GrOpsTask::recordOp(
Chris Dalton945ee652019-01-23 09:10:36 -0700792 std::unique_ptr<GrOp> op, GrProcessorSet::Analysis processorAnalysis, GrAppliedClip* clip,
Greg Daniel524e28b2019-11-01 11:48:53 -0400793 const DstProxyView* dstProxyView, const GrCaps& caps) {
Ethan Nicholas029b22c2018-10-18 16:49:56 -0400794 SkDEBUGCODE(op->validate();)
Greg Daniel524e28b2019-11-01 11:48:53 -0400795 SkASSERT(processorAnalysis.requiresDstTexture() == (dstProxyView && dstProxyView->proxy()));
Greg Daniel16f5c652019-10-29 11:26:01 -0400796 GrSurfaceProxy* proxy = fTargetView.proxy();
797 SkASSERT(proxy);
Robert Phillipsee683652017-04-26 11:53:10 -0400798
Greg Danielf41b2bd2019-08-22 16:19:24 -0400799 // A closed GrOpsTask should never receive new/more ops
robertphillips6a186652015-10-20 07:37:58 -0700800 SkASSERT(!this->isClosed());
Brian Salomon19ec80f2018-11-16 13:27:30 -0500801 if (!op->bounds().isFinite()) {
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500802 fArenas.opMemoryPool()->release(std::move(op));
Brian Salomon19ec80f2018-11-16 13:27:30 -0500803 return;
804 }
robertphillipsa106c622015-10-16 09:07:06 -0700805
Chris Dalton16a33c62019-09-24 22:19:17 -0600806 // Account for this op's bounds before we attempt to combine.
807 // NOTE: The caller should have already called "op->setClippedBounds()" by now, if applicable.
808 fTotalBounds.join(op->bounds());
809
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500810 // Check if there is an op we can combine with by linearly searching back until we either
811 // 1) check every op
bsalomon512be532015-09-10 10:42:55 -0700812 // 2) intersect with something
813 // 3) find a 'blocker'
Greg Daniel16f5c652019-10-29 11:26:01 -0400814 GR_AUDIT_TRAIL_ADD_OP(fAuditTrail, op.get(), proxy->uniqueID());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400815 GrOP_INFO("opsTask: %d Recording (%s, opID: %u)\n"
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400816 "\tBounds [L: %.2f, T: %.2f R: %.2f B: %.2f]\n",
817 this->uniqueID(),
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500818 op->name(),
819 op->uniqueID(),
Robert Phillips1119dc32017-04-11 12:54:57 -0400820 op->bounds().fLeft, op->bounds().fTop,
821 op->bounds().fRight, op->bounds().fBottom);
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500822 GrOP_INFO(SkTabString(op->dumpInfo(), 1).c_str());
Brian Salomon25a88092016-12-01 09:36:50 -0500823 GrOP_INFO("\tOutcome:\n");
Brian Osman788b9162020-02-07 10:36:46 -0500824 int maxCandidates = std::min(kMaxOpChainDistance, fOpChains.count());
Robert Phillips318c4192017-05-17 09:36:38 -0400825 if (maxCandidates) {
bsalomon512be532015-09-10 10:42:55 -0700826 int i = 0;
827 while (true) {
Brian Salomon588cec72018-11-14 13:56:37 -0500828 OpChain& candidate = fOpChains.fromBack(i);
Greg Daniel524e28b2019-11-01 11:48:53 -0400829 op = candidate.appendOp(std::move(op), processorAnalysis, dstProxyView, clip, caps,
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500830 &fArenas, fAuditTrail);
Brian Salomon588cec72018-11-14 13:56:37 -0500831 if (!op) {
832 return;
bsalomon512be532015-09-10 10:42:55 -0700833 }
Brian Salomona7682c82018-10-24 10:04:37 -0400834 // Stop going backwards if we would cause a painter's order violation.
Brian Salomon588cec72018-11-14 13:56:37 -0500835 if (!can_reorder(candidate.bounds(), op->bounds())) {
836 GrOP_INFO("\t\tBackward: Intersects with chain (%s, head opID: %u)\n",
837 candidate.head()->name(), candidate.head()->uniqueID());
bsalomon512be532015-09-10 10:42:55 -0700838 break;
839 }
Brian Salomon588cec72018-11-14 13:56:37 -0500840 if (++i == maxCandidates) {
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400841 GrOP_INFO("\t\tBackward: Reached max lookback or beginning of op array %d\n", i);
bsalomon512be532015-09-10 10:42:55 -0700842 break;
843 }
844 }
845 } else {
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400846 GrOP_INFO("\t\tBackward: FirstOp\n");
bsalomon512be532015-09-10 10:42:55 -0700847 }
Brian Salomon54d212e2017-03-21 14:22:38 -0400848 if (clip) {
849 clip = fClipAllocator.make<GrAppliedClip>(std::move(*clip));
Robert Phillipsc84c0302017-05-08 15:35:11 -0400850 SkDEBUGCODE(fNumClips++;)
Brian Salomon54d212e2017-03-21 14:22:38 -0400851 }
Greg Daniel524e28b2019-11-01 11:48:53 -0400852 fOpChains.emplace_back(std::move(op), processorAnalysis, clip, dstProxyView);
bsalomon512be532015-09-10 10:42:55 -0700853}
854
Greg Danielf41b2bd2019-08-22 16:19:24 -0400855void GrOpsTask::forwardCombine(const GrCaps& caps) {
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400856 SkASSERT(!this->isClosed());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400857 GrOP_INFO("opsTask: %d ForwardCombine %d ops:\n", this->uniqueID(), fOpChains.count());
Robert Phillips48567ac2017-06-01 08:46:00 -0400858
Brian Salomon588cec72018-11-14 13:56:37 -0500859 for (int i = 0; i < fOpChains.count() - 1; ++i) {
860 OpChain& chain = fOpChains[i];
Brian Osman788b9162020-02-07 10:36:46 -0500861 int maxCandidateIdx = std::min(i + kMaxOpChainDistance, fOpChains.count() - 1);
bsalomonaecc0182016-03-07 11:50:44 -0800862 int j = i + 1;
863 while (true) {
Brian Salomon588cec72018-11-14 13:56:37 -0500864 OpChain& candidate = fOpChains[j];
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500865 if (candidate.prependChain(&chain, caps, &fArenas, fAuditTrail)) {
bsalomonaecc0182016-03-07 11:50:44 -0800866 break;
867 }
Robert Phillipsc84c0302017-05-08 15:35:11 -0400868 // Stop traversing if we would cause a painter's order violation.
Brian Salomon588cec72018-11-14 13:56:37 -0500869 if (!can_reorder(chain.bounds(), candidate.bounds())) {
870 GrOP_INFO(
871 "\t\t%d: chain (%s head opID: %u) -> "
872 "Intersects with chain (%s, head opID: %u)\n",
873 i, chain.head()->name(), chain.head()->uniqueID(), candidate.head()->name(),
874 candidate.head()->uniqueID());
bsalomonaecc0182016-03-07 11:50:44 -0800875 break;
876 }
Brian Salomona7682c82018-10-24 10:04:37 -0400877 if (++j > maxCandidateIdx) {
Brian Salomon588cec72018-11-14 13:56:37 -0500878 GrOP_INFO("\t\t%d: chain (%s opID: %u) -> Reached max lookahead or end of array\n",
879 i, chain.head()->name(), chain.head()->uniqueID());
bsalomonaecc0182016-03-07 11:50:44 -0800880 break;
881 }
882 }
883 }
884}
885
Chris Dalton16a33c62019-09-24 22:19:17 -0600886GrRenderTask::ExpectedOutcome GrOpsTask::onMakeClosed(
887 const GrCaps& caps, SkIRect* targetUpdateBounds) {
888 this->forwardCombine(caps);
Brian Salomonf1c9eae2020-05-01 15:00:34 -0400889 SkScopeExit triggerObservers([&] {
890 for (const auto& o : fClosedObservers) {
891 o->wasClosed(*this);
Brian Salomon3b8486a2020-04-21 12:43:26 -0400892 }
Brian Salomonf1c9eae2020-05-01 15:00:34 -0400893 fClosedObservers.reset();
Brian Salomon3b8486a2020-04-21 12:43:26 -0400894 });
Chris Dalton16a33c62019-09-24 22:19:17 -0600895 if (!this->isNoOp()) {
Greg Daniel16f5c652019-10-29 11:26:01 -0400896 GrSurfaceProxy* proxy = fTargetView.proxy();
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400897 // Use the entire backing store bounds since the GPU doesn't clip automatically to the
898 // logical dimensions.
899 SkRect clippedContentBounds = proxy->backingStoreBoundsRect();
Greg Daniel16f5c652019-10-29 11:26:01 -0400900 // TODO: If we can fix up GLPrograms test to always intersect the fTargetView proxy bounds
901 // then we can simply assert here that the bounds intersect.
Chris Dalton16a33c62019-09-24 22:19:17 -0600902 if (clippedContentBounds.intersect(fTotalBounds)) {
Greg Daniel94ed83f2019-09-27 13:05:43 -0400903 clippedContentBounds.roundOut(&fClippedContentBounds);
904 *targetUpdateBounds = fClippedContentBounds;
Chris Dalton16a33c62019-09-24 22:19:17 -0600905 return ExpectedOutcome::kTargetDirty;
906 }
907 }
908 return ExpectedOutcome::kTargetUnchanged;
909}