blob: c5f52fb1e6e2e0fc9d78d5ab5aa9039869841683 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
Greg Danielf41b2bd2019-08-22 16:19:24 -04002 * Copyright 2019 Google Inc.
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
Greg Danielf41b2bd2019-08-22 16:19:24 -04008#include "src/gpu/GrOpsTask.h"
Brian Salomon4d2d6f42019-07-26 14:15:11 -04009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/private/GrRecordingContext.h"
11#include "src/core/SkExchange.h"
12#include "src/core/SkRectPriv.h"
Brian Salomon3b8486a2020-04-21 12:43:26 -040013#include "src/core/SkScopeExit.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/core/SkTraceEvent.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040015#include "src/gpu/GrAuditTrail.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/GrCaps.h"
17#include "src/gpu/GrGpu.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrMemoryPool.h"
Greg Daniele227fe42019-08-21 13:52:24 -040019#include "src/gpu/GrOpFlushState.h"
Greg Daniel2d41d0d2019-08-26 11:08:51 -040020#include "src/gpu/GrOpsRenderPass.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/GrRecordingContextPriv.h"
Chris Dalton674f77a2019-09-30 20:49:39 -060022#include "src/gpu/GrRenderTarget.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/GrRenderTargetContext.h"
Chris Dalton674f77a2019-09-30 20:49:39 -060024#include "src/gpu/GrRenderTargetPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/GrResourceAllocator.h"
Chris Dalton674f77a2019-09-30 20:49:39 -060026#include "src/gpu/GrStencilAttachment.h"
Chris Dalton95d8ceb2019-07-30 11:17:59 -060027#include "src/gpu/GrTexturePriv.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040028#include "src/gpu/geometry/GrRect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "src/gpu/ops/GrClearOp.h"
Robert Phillipsf2361d22016-10-25 14:20:06 -040030
reed@google.comac10a2d2010-12-22 21:39:39 +000031////////////////////////////////////////////////////////////////////////////////
32
Brian Salomon09d994e2016-12-21 11:14:46 -050033// Experimentally we have found that most combining occurs within the first 10 comparisons.
Brian Salomon588cec72018-11-14 13:56:37 -050034static const int kMaxOpMergeDistance = 10;
35static const int kMaxOpChainDistance = 10;
36
37////////////////////////////////////////////////////////////////////////////////
38
Greg Daniel524e28b2019-11-01 11:48:53 -040039using DstProxyView = GrXferProcessor::DstProxyView;
Brian Salomon588cec72018-11-14 13:56:37 -050040
41////////////////////////////////////////////////////////////////////////////////
42
Brian Salomon3b8486a2020-04-21 12:43:26 -040043GrOpsTaskClosedObserver::~GrOpsTaskClosedObserver() = default;
44
45////////////////////////////////////////////////////////////////////////////////
46
Brian Salomon588cec72018-11-14 13:56:37 -050047static inline bool can_reorder(const SkRect& a, const SkRect& b) { return !GrRectsOverlap(a, b); }
48
49////////////////////////////////////////////////////////////////////////////////
50
Greg Danielf41b2bd2019-08-22 16:19:24 -040051inline GrOpsTask::OpChain::List::List(std::unique_ptr<GrOp> op)
Brian Salomon588cec72018-11-14 13:56:37 -050052 : fHead(std::move(op)), fTail(fHead.get()) {
53 this->validate();
54}
55
Greg Danielf41b2bd2019-08-22 16:19:24 -040056inline GrOpsTask::OpChain::List::List(List&& that) { *this = std::move(that); }
Brian Salomon588cec72018-11-14 13:56:37 -050057
Greg Danielf41b2bd2019-08-22 16:19:24 -040058inline GrOpsTask::OpChain::List& GrOpsTask::OpChain::List::operator=(List&& that) {
Brian Salomon588cec72018-11-14 13:56:37 -050059 fHead = std::move(that.fHead);
60 fTail = that.fTail;
61 that.fTail = nullptr;
62 this->validate();
63 return *this;
64}
65
Greg Danielf41b2bd2019-08-22 16:19:24 -040066inline std::unique_ptr<GrOp> GrOpsTask::OpChain::List::popHead() {
Brian Salomon588cec72018-11-14 13:56:37 -050067 SkASSERT(fHead);
68 auto temp = fHead->cutChain();
69 std::swap(temp, fHead);
70 if (!fHead) {
71 SkASSERT(fTail == temp.get());
72 fTail = nullptr;
73 }
74 return temp;
75}
76
Greg Danielf41b2bd2019-08-22 16:19:24 -040077inline std::unique_ptr<GrOp> GrOpsTask::OpChain::List::removeOp(GrOp* op) {
Brian Salomon588cec72018-11-14 13:56:37 -050078#ifdef SK_DEBUG
79 auto head = op;
80 while (head->prevInChain()) { head = head->prevInChain(); }
81 SkASSERT(head == fHead.get());
82#endif
83 auto prev = op->prevInChain();
84 if (!prev) {
85 SkASSERT(op == fHead.get());
86 return this->popHead();
87 }
88 auto temp = prev->cutChain();
89 if (auto next = temp->cutChain()) {
90 prev->chainConcat(std::move(next));
91 } else {
92 SkASSERT(fTail == op);
93 fTail = prev;
94 }
95 this->validate();
96 return temp;
97}
98
Greg Danielf41b2bd2019-08-22 16:19:24 -040099inline void GrOpsTask::OpChain::List::pushHead(std::unique_ptr<GrOp> op) {
Brian Salomon588cec72018-11-14 13:56:37 -0500100 SkASSERT(op);
101 SkASSERT(op->isChainHead());
102 SkASSERT(op->isChainTail());
103 if (fHead) {
104 op->chainConcat(std::move(fHead));
105 fHead = std::move(op);
106 } else {
107 fHead = std::move(op);
108 fTail = fHead.get();
109 }
110}
111
Greg Danielf41b2bd2019-08-22 16:19:24 -0400112inline void GrOpsTask::OpChain::List::pushTail(std::unique_ptr<GrOp> op) {
Brian Salomon588cec72018-11-14 13:56:37 -0500113 SkASSERT(op->isChainTail());
114 fTail->chainConcat(std::move(op));
115 fTail = fTail->nextInChain();
116}
117
Greg Danielf41b2bd2019-08-22 16:19:24 -0400118inline void GrOpsTask::OpChain::List::validate() const {
Brian Salomon588cec72018-11-14 13:56:37 -0500119#ifdef SK_DEBUG
120 if (fHead) {
121 SkASSERT(fTail);
122 fHead->validateChain(fTail);
123 }
124#endif
125}
126
127////////////////////////////////////////////////////////////////////////////////
128
Greg Danielf41b2bd2019-08-22 16:19:24 -0400129GrOpsTask::OpChain::OpChain(std::unique_ptr<GrOp> op,
130 GrProcessorSet::Analysis processorAnalysis,
Greg Daniel524e28b2019-11-01 11:48:53 -0400131 GrAppliedClip* appliedClip, const DstProxyView* dstProxyView)
Chris Dalton945ee652019-01-23 09:10:36 -0700132 : fList{std::move(op)}
133 , fProcessorAnalysis(processorAnalysis)
134 , fAppliedClip(appliedClip) {
135 if (fProcessorAnalysis.requiresDstTexture()) {
Greg Daniel524e28b2019-11-01 11:48:53 -0400136 SkASSERT(dstProxyView && dstProxyView->proxy());
137 fDstProxyView = *dstProxyView;
Brian Salomon588cec72018-11-14 13:56:37 -0500138 }
139 fBounds = fList.head()->bounds();
140}
141
Greg Danielf41b2bd2019-08-22 16:19:24 -0400142void GrOpsTask::OpChain::visitProxies(const GrOp::VisitProxyFunc& func) const {
Brian Salomon588cec72018-11-14 13:56:37 -0500143 if (fList.empty()) {
144 return;
145 }
146 for (const auto& op : GrOp::ChainRange<>(fList.head())) {
Chris Dalton1706cbf2019-05-21 19:35:29 -0600147 op.visitProxies(func);
Brian Salomon588cec72018-11-14 13:56:37 -0500148 }
Greg Daniel524e28b2019-11-01 11:48:53 -0400149 if (fDstProxyView.proxy()) {
150 func(fDstProxyView.proxy(), GrMipMapped::kNo);
Brian Salomon588cec72018-11-14 13:56:37 -0500151 }
152 if (fAppliedClip) {
153 fAppliedClip->visitProxies(func);
154 }
155}
156
Greg Danielf41b2bd2019-08-22 16:19:24 -0400157void GrOpsTask::OpChain::deleteOps(GrOpMemoryPool* pool) {
Brian Salomon588cec72018-11-14 13:56:37 -0500158 while (!fList.empty()) {
159 pool->release(fList.popHead());
160 }
161}
162
163// Concatenates two op chains and attempts to merge ops across the chains. Assumes that we know that
164// the two chains are chainable. Returns the new chain.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400165GrOpsTask::OpChain::List GrOpsTask::OpChain::DoConcat(
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500166 List chainA, List chainB, const GrCaps& caps, GrRecordingContext::Arenas* arenas,
167 GrAuditTrail* auditTrail) {
Brian Salomon588cec72018-11-14 13:56:37 -0500168 // We process ops in chain b from head to tail. We attempt to merge with nodes in a, starting
169 // at chain a's tail and working toward the head. We produce one of the following outcomes:
170 // 1) b's head is merged into an op in a.
171 // 2) An op from chain a is merged into b's head. (In this case b's head gets processed again.)
172 // 3) b's head is popped from chain a and added at the tail of a.
173 // After result 3 we don't want to attempt to merge the next head of b with the new tail of a,
174 // as we assume merges were already attempted when chain b was created. So we keep track of the
175 // original tail of a and start our iteration of a there. We also track the bounds of the nodes
176 // appended to chain a that will be skipped for bounds testing. If the original tail of a is
177 // merged into an op in b (case 2) then we advance the "original tail" towards the head of a.
178 GrOp* origATail = chainA.tail();
179 SkRect skipBounds = SkRectPriv::MakeLargestInverted();
180 do {
181 int numMergeChecks = 0;
182 bool merged = false;
183 bool noSkip = (origATail == chainA.tail());
184 SkASSERT(noSkip == (skipBounds == SkRectPriv::MakeLargestInverted()));
185 bool canBackwardMerge = noSkip || can_reorder(chainB.head()->bounds(), skipBounds);
186 SkRect forwardMergeBounds = skipBounds;
187 GrOp* a = origATail;
188 while (a) {
189 bool canForwardMerge =
190 (a == chainA.tail()) || can_reorder(a->bounds(), forwardMergeBounds);
191 if (canForwardMerge || canBackwardMerge) {
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500192 auto result = a->combineIfPossible(chainB.head(), arenas, caps);
Brian Salomon588cec72018-11-14 13:56:37 -0500193 SkASSERT(result != GrOp::CombineResult::kCannotCombine);
194 merged = (result == GrOp::CombineResult::kMerged);
Robert Phillips9548c3b422019-01-08 12:35:43 -0500195 GrOP_INFO("\t\t: (%s opID: %u) -> Combining with (%s, opID: %u)\n",
Brian Salomon588cec72018-11-14 13:56:37 -0500196 chainB.head()->name(), chainB.head()->uniqueID(), a->name(),
197 a->uniqueID());
Brian Salomon588cec72018-11-14 13:56:37 -0500198 }
199 if (merged) {
Brian Salomon52a6ed32018-11-26 10:30:58 -0500200 GR_AUDIT_TRAIL_OPS_RESULT_COMBINED(auditTrail, a, chainB.head());
Brian Salomon588cec72018-11-14 13:56:37 -0500201 if (canBackwardMerge) {
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500202 arenas->opMemoryPool()->release(chainB.popHead());
Brian Salomon588cec72018-11-14 13:56:37 -0500203 } else {
204 // We merged the contents of b's head into a. We will replace b's head with a in
205 // chain b.
206 SkASSERT(canForwardMerge);
207 if (a == origATail) {
208 origATail = a->prevInChain();
209 }
210 std::unique_ptr<GrOp> detachedA = chainA.removeOp(a);
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500211 arenas->opMemoryPool()->release(chainB.popHead());
Brian Salomon588cec72018-11-14 13:56:37 -0500212 chainB.pushHead(std::move(detachedA));
213 if (chainA.empty()) {
214 // We merged all the nodes in chain a to chain b.
215 return chainB;
216 }
217 }
218 break;
219 } else {
220 if (++numMergeChecks == kMaxOpMergeDistance) {
221 break;
222 }
223 forwardMergeBounds.joinNonEmptyArg(a->bounds());
224 canBackwardMerge =
225 canBackwardMerge && can_reorder(chainB.head()->bounds(), a->bounds());
226 a = a->prevInChain();
227 }
228 }
229 // If we weren't able to merge b's head then pop b's head from chain b and make it the new
230 // tail of a.
231 if (!merged) {
232 chainA.pushTail(chainB.popHead());
233 skipBounds.joinNonEmptyArg(chainA.tail()->bounds());
234 }
235 } while (!chainB.empty());
236 return chainA;
237}
238
Chris Dalton945ee652019-01-23 09:10:36 -0700239// Attempts to concatenate the given chain onto our own and merge ops across the chains. Returns
240// whether the operation succeeded. On success, the provided list will be returned empty.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400241bool GrOpsTask::OpChain::tryConcat(
Greg Daniel524e28b2019-11-01 11:48:53 -0400242 List* list, GrProcessorSet::Analysis processorAnalysis, const DstProxyView& dstProxyView,
Chris Dalton945ee652019-01-23 09:10:36 -0700243 const GrAppliedClip* appliedClip, const SkRect& bounds, const GrCaps& caps,
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500244 GrRecordingContext::Arenas* arenas, GrAuditTrail* auditTrail) {
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700245 SkASSERT(!fList.empty());
246 SkASSERT(!list->empty());
Greg Daniel524e28b2019-11-01 11:48:53 -0400247 SkASSERT(fProcessorAnalysis.requiresDstTexture() == SkToBool(fDstProxyView.proxy()));
248 SkASSERT(processorAnalysis.requiresDstTexture() == SkToBool(dstProxyView.proxy()));
Brian Salomon588cec72018-11-14 13:56:37 -0500249 // All returns use explicit tuple constructor rather than {a, b} to work around old GCC bug.
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700250 if (fList.head()->classID() != list->head()->classID() ||
251 SkToBool(fAppliedClip) != SkToBool(appliedClip) ||
252 (fAppliedClip && *fAppliedClip != *appliedClip) ||
Chris Dalton945ee652019-01-23 09:10:36 -0700253 (fProcessorAnalysis.requiresNonOverlappingDraws() !=
254 processorAnalysis.requiresNonOverlappingDraws()) ||
255 (fProcessorAnalysis.requiresNonOverlappingDraws() &&
256 // Non-overlaping draws are only required when Ganesh will either insert a barrier,
257 // or read back a new dst texture between draws. In either case, we can neither
258 // chain nor combine overlapping Ops.
259 GrRectsTouchOrOverlap(fBounds, bounds)) ||
260 (fProcessorAnalysis.requiresDstTexture() != processorAnalysis.requiresDstTexture()) ||
Greg Daniel524e28b2019-11-01 11:48:53 -0400261 (fProcessorAnalysis.requiresDstTexture() && fDstProxyView != dstProxyView)) {
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700262 return false;
Brian Salomon588cec72018-11-14 13:56:37 -0500263 }
Chris Daltonee21e6b2019-01-22 14:04:43 -0700264
Brian Salomon588cec72018-11-14 13:56:37 -0500265 SkDEBUGCODE(bool first = true;)
266 do {
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500267 switch (fList.tail()->combineIfPossible(list->head(), arenas, caps)) {
Brian Salomon588cec72018-11-14 13:56:37 -0500268 case GrOp::CombineResult::kCannotCombine:
269 // If an op supports chaining then it is required that chaining is transitive and
270 // that if any two ops in two different chains can merge then the two chains
271 // may also be chained together. Thus, we should only hit this on the first
272 // iteration.
273 SkASSERT(first);
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700274 return false;
Brian Salomon588cec72018-11-14 13:56:37 -0500275 case GrOp::CombineResult::kMayChain:
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500276 fList = DoConcat(std::move(fList), skstd::exchange(*list, List()), caps, arenas,
277 auditTrail);
Chris Daltonee21e6b2019-01-22 14:04:43 -0700278 // The above exchange cleared out 'list'. The list needs to be empty now for the
279 // loop to terminate.
280 SkASSERT(list->empty());
281 break;
Brian Salomon588cec72018-11-14 13:56:37 -0500282 case GrOp::CombineResult::kMerged: {
Robert Phillips9548c3b422019-01-08 12:35:43 -0500283 GrOP_INFO("\t\t: (%s opID: %u) -> Combining with (%s, opID: %u)\n",
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700284 list->tail()->name(), list->tail()->uniqueID(), list->head()->name(),
285 list->head()->uniqueID());
286 GR_AUDIT_TRAIL_OPS_RESULT_COMBINED(auditTrail, fList.tail(), list->head());
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500287 arenas->opMemoryPool()->release(list->popHead());
Brian Salomon588cec72018-11-14 13:56:37 -0500288 break;
289 }
290 }
291 SkDEBUGCODE(first = false);
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700292 } while (!list->empty());
Chris Daltonee21e6b2019-01-22 14:04:43 -0700293
294 // The new ops were successfully merged and/or chained onto our own.
295 fBounds.joinPossiblyEmptyRect(bounds);
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700296 return true;
Brian Salomon588cec72018-11-14 13:56:37 -0500297}
298
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500299bool GrOpsTask::OpChain::prependChain(OpChain* that, const GrCaps& caps,
300 GrRecordingContext::Arenas* arenas,
301 GrAuditTrail* auditTrail) {
Greg Daniel524e28b2019-11-01 11:48:53 -0400302 if (!that->tryConcat(&fList, fProcessorAnalysis, fDstProxyView, fAppliedClip, fBounds, caps,
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500303 arenas, auditTrail)) {
Brian Salomon588cec72018-11-14 13:56:37 -0500304 this->validate();
305 // append failed
306 return false;
307 }
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700308
Brian Salomon588cec72018-11-14 13:56:37 -0500309 // 'that' owns the combined chain. Move it into 'this'.
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700310 SkASSERT(fList.empty());
Brian Salomon588cec72018-11-14 13:56:37 -0500311 fList = std::move(that->fList);
Chris Daltonee21e6b2019-01-22 14:04:43 -0700312 fBounds = that->fBounds;
Brian Salomon588cec72018-11-14 13:56:37 -0500313
Greg Daniel524e28b2019-11-01 11:48:53 -0400314 that->fDstProxyView.setProxyView({});
Brian Salomon588cec72018-11-14 13:56:37 -0500315 if (that->fAppliedClip) {
316 for (int i = 0; i < that->fAppliedClip->numClipCoverageFragmentProcessors(); ++i) {
317 that->fAppliedClip->detachClipCoverageFragmentProcessor(i);
318 }
319 }
320 this->validate();
321 return true;
322}
323
Greg Danielf41b2bd2019-08-22 16:19:24 -0400324std::unique_ptr<GrOp> GrOpsTask::OpChain::appendOp(
Chris Dalton945ee652019-01-23 09:10:36 -0700325 std::unique_ptr<GrOp> op, GrProcessorSet::Analysis processorAnalysis,
Greg Daniel524e28b2019-11-01 11:48:53 -0400326 const DstProxyView* dstProxyView, const GrAppliedClip* appliedClip, const GrCaps& caps,
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500327 GrRecordingContext::Arenas* arenas, GrAuditTrail* auditTrail) {
Greg Daniel524e28b2019-11-01 11:48:53 -0400328 const GrXferProcessor::DstProxyView noDstProxyView;
329 if (!dstProxyView) {
330 dstProxyView = &noDstProxyView;
Brian Salomon588cec72018-11-14 13:56:37 -0500331 }
332 SkASSERT(op->isChainHead() && op->isChainTail());
333 SkRect opBounds = op->bounds();
334 List chain(std::move(op));
Chris Dalton945ee652019-01-23 09:10:36 -0700335 if (!this->tryConcat(
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500336 &chain, processorAnalysis, *dstProxyView, appliedClip, opBounds, caps,
337 arenas, auditTrail)) {
Brian Salomon588cec72018-11-14 13:56:37 -0500338 // append failed, give the op back to the caller.
339 this->validate();
340 return chain.popHead();
341 }
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700342
343 SkASSERT(chain.empty());
Brian Salomon588cec72018-11-14 13:56:37 -0500344 this->validate();
345 return nullptr;
346}
347
Greg Danielf41b2bd2019-08-22 16:19:24 -0400348inline void GrOpsTask::OpChain::validate() const {
Brian Salomon588cec72018-11-14 13:56:37 -0500349#ifdef SK_DEBUG
350 fList.validate();
351 for (const auto& op : GrOp::ChainRange<>(fList.head())) {
352 // Not using SkRect::contains because we allow empty rects.
353 SkASSERT(fBounds.fLeft <= op.bounds().fLeft && fBounds.fTop <= op.bounds().fTop &&
354 fBounds.fRight >= op.bounds().fRight && fBounds.fBottom >= op.bounds().fBottom);
355 }
356#endif
357}
358
359////////////////////////////////////////////////////////////////////////////////
bsalomon489147c2015-12-14 12:13:09 -0800360
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500361GrOpsTask::GrOpsTask(GrRecordingContext::Arenas arenas,
Greg Daniel16f5c652019-10-29 11:26:01 -0400362 GrSurfaceProxyView view,
Greg Danielf41b2bd2019-08-22 16:19:24 -0400363 GrAuditTrail* auditTrail)
Greg Daniel16f5c652019-10-29 11:26:01 -0400364 : GrRenderTask(std::move(view))
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500365 , fArenas(arenas)
Greg Danielf41b2bd2019-08-22 16:19:24 -0400366 , fAuditTrail(auditTrail)
Robert Phillipsb6deea82017-05-11 14:14:30 -0400367 SkDEBUGCODE(, fNumClips(0)) {
Greg Daniel16f5c652019-10-29 11:26:01 -0400368 fTargetView.proxy()->setLastRenderTask(this);
bsalomon4061b122015-05-29 10:26:19 -0700369}
370
Greg Danielf41b2bd2019-08-22 16:19:24 -0400371void GrOpsTask::deleteOps() {
Brian Salomon588cec72018-11-14 13:56:37 -0500372 for (auto& chain : fOpChains) {
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500373 chain.deleteOps(fArenas.opMemoryPool());
Robert Phillipsc994a932018-06-19 13:09:54 -0400374 }
Brian Salomon588cec72018-11-14 13:56:37 -0500375 fOpChains.reset();
Robert Phillipsc994a932018-06-19 13:09:54 -0400376}
377
Greg Danielf41b2bd2019-08-22 16:19:24 -0400378GrOpsTask::~GrOpsTask() {
Robert Phillipsc994a932018-06-19 13:09:54 -0400379 this->deleteOps();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000380}
381
382////////////////////////////////////////////////////////////////////////////////
383
Greg Danielf41b2bd2019-08-22 16:19:24 -0400384void GrOpsTask::endFlush() {
385 fLastClipStackGenID = SK_InvalidUniqueID;
386 this->deleteOps();
387 fClipAllocator.reset();
Chris Dalton706a6ff2017-11-29 22:01:06 -0700388
Greg Daniel16f5c652019-10-29 11:26:01 -0400389 GrSurfaceProxy* proxy = fTargetView.proxy();
390 if (proxy && this == proxy->getLastRenderTask()) {
391 proxy->setLastRenderTask(nullptr);
Greg Danielf21bf9e2019-08-22 20:12:20 +0000392 }
Greg Danielf41b2bd2019-08-22 16:19:24 -0400393
Greg Daniel16f5c652019-10-29 11:26:01 -0400394 fTargetView.reset();
Greg Danielf41b2bd2019-08-22 16:19:24 -0400395 fDeferredProxies.reset();
Greg Danielb20d7e52019-09-03 13:54:39 -0400396 fSampledProxies.reset();
Greg Danielf41b2bd2019-08-22 16:19:24 -0400397 fAuditTrail = nullptr;
Greg Danielf21bf9e2019-08-22 20:12:20 +0000398}
399
Robert Phillips29f38542019-10-16 09:20:25 -0400400void GrOpsTask::onPrePrepare(GrRecordingContext* context) {
Robert Phillips7327c9d2019-10-08 16:32:56 -0400401 SkASSERT(this->isClosed());
402#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
403 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
404#endif
405 // TODO: remove the check for discard here once reduced op splitting is turned on. Currently we
406 // can end up with GrOpsTasks that only have a discard load op and no ops. For vulkan validation
407 // we need to keep that discard and not drop it. Once we have reduce op list splitting enabled
408 // we shouldn't end up with GrOpsTasks with only discard.
409 if (this->isNoOp() || (fClippedContentBounds.isEmpty() && fColorLoadOp != GrLoadOp::kDiscard)) {
410 return;
411 }
412
413 for (const auto& chain : fOpChains) {
414 if (chain.shouldExecute()) {
Robert Phillips8053c972019-11-21 10:44:53 -0500415 chain.head()->prePrepare(context,
416 &fTargetView,
417 chain.appliedClip(),
418 chain.dstProxyView());
Robert Phillips7327c9d2019-10-08 16:32:56 -0400419 }
420 }
421}
422
Greg Danielf41b2bd2019-08-22 16:19:24 -0400423void GrOpsTask::onPrepare(GrOpFlushState* flushState) {
Greg Daniel16f5c652019-10-29 11:26:01 -0400424 SkASSERT(fTargetView.proxy()->peekRenderTarget());
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400425 SkASSERT(this->isClosed());
Stan Iliev2af578d2017-08-16 13:00:28 -0400426#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
Brian Salomon5f394272019-07-02 14:07:49 -0400427 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Stan Iliev2af578d2017-08-16 13:00:28 -0400428#endif
Greg Daniel94ed83f2019-09-27 13:05:43 -0400429 // TODO: remove the check for discard here once reduced op splitting is turned on. Currently we
430 // can end up with GrOpsTasks that only have a discard load op and no ops. For vulkan validation
431 // we need to keep that discard and not drop it. Once we have reduce op list splitting enabled
432 // we shouldn't end up with GrOpsTasks with only discard.
433 if (this->isNoOp() || (fClippedContentBounds.isEmpty() && fColorLoadOp != GrLoadOp::kDiscard)) {
434 return;
435 }
robertphillipsa106c622015-10-16 09:07:06 -0700436
Greg Danielb20d7e52019-09-03 13:54:39 -0400437 flushState->setSampledProxyArray(&fSampledProxies);
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500438 // Loop over the ops that haven't yet been prepared.
Brian Salomon588cec72018-11-14 13:56:37 -0500439 for (const auto& chain : fOpChains) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400440 if (chain.shouldExecute()) {
Stan Iliev2af578d2017-08-16 13:00:28 -0400441#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
Brian Salomon5f394272019-07-02 14:07:49 -0400442 TRACE_EVENT0("skia.gpu", chain.head()->name());
Stan Iliev2af578d2017-08-16 13:00:28 -0400443#endif
Robert Phillips901aff02019-10-08 12:32:56 -0400444 GrOpFlushState::OpArgs opArgs(chain.head(),
Greg Daniel16f5c652019-10-29 11:26:01 -0400445 &fTargetView,
Robert Phillips901aff02019-10-08 12:32:56 -0400446 chain.appliedClip(),
Greg Daniel524e28b2019-11-01 11:48:53 -0400447 chain.dstProxyView());
Robert Phillips405413f2019-10-04 10:39:28 -0400448
Brian Salomon29b60c92017-10-31 14:42:10 -0400449 flushState->setOpArgs(&opArgs);
Robert Phillipsdf70f152019-11-15 14:57:05 -0500450
451 // Temporary debugging helper: for debugging prePrepare w/o going through DDLs
452 // Delete once most of the GrOps have an onPrePrepare.
453 // chain.head()->prePrepare(flushState->gpu()->getContext(), &fTargetView,
454 // chain.appliedClip());
455
Robert Phillips7327c9d2019-10-08 16:32:56 -0400456 // GrOp::prePrepare may or may not have been called at this point
Brian Salomon588cec72018-11-14 13:56:37 -0500457 chain.head()->prepare(flushState);
Brian Salomon29b60c92017-10-31 14:42:10 -0400458 flushState->setOpArgs(nullptr);
bsalomonaecc0182016-03-07 11:50:44 -0800459 }
bsalomon512be532015-09-10 10:42:55 -0700460 }
Greg Danielb20d7e52019-09-03 13:54:39 -0400461 flushState->setSampledProxyArray(nullptr);
robertphillipsa13e2022015-11-11 12:01:09 -0800462}
bsalomon512be532015-09-10 10:42:55 -0700463
Greg Daniel4a0d36d2019-09-30 12:24:36 -0400464static GrOpsRenderPass* create_render_pass(
465 GrGpu* gpu, GrRenderTarget* rt, GrSurfaceOrigin origin, const SkIRect& bounds,
Greg Danielb20d7e52019-09-03 13:54:39 -0400466 GrLoadOp colorLoadOp, const SkPMColor4f& loadClearColor, GrLoadOp stencilLoadOp,
Michael Ludwigfcdd0612019-11-25 08:34:31 -0500467 GrStoreOp stencilStoreOp, const SkTArray<GrSurfaceProxy*, true>& sampledProxies) {
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400468 const GrOpsRenderPass::LoadAndStoreInfo kColorLoadStoreInfo {
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400469 colorLoadOp,
470 GrStoreOp::kStore,
471 loadClearColor
Robert Phillips178ce3e2017-04-13 09:15:47 -0400472 };
473
Robert Phillips95214472017-08-08 18:00:03 -0400474 // TODO:
475 // We would like to (at this level) only ever clear & discard. We would need
Greg Danielf41b2bd2019-08-22 16:19:24 -0400476 // to stop splitting up higher level OpsTasks for copyOps to achieve that.
Robert Phillips95214472017-08-08 18:00:03 -0400477 // Note: we would still need SB loads and stores but they would happen at a
478 // lower level (inside the VK command buffer).
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400479 const GrOpsRenderPass::StencilLoadAndStoreInfo stencilLoadAndStoreInfo {
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400480 stencilLoadOp,
Chris Dalton674f77a2019-09-30 20:49:39 -0600481 stencilStoreOp,
Robert Phillips95214472017-08-08 18:00:03 -0400482 };
483
Greg Danielb20d7e52019-09-03 13:54:39 -0400484 return gpu->getOpsRenderPass(rt, origin, bounds, kColorLoadStoreInfo, stencilLoadAndStoreInfo,
485 sampledProxies);
Robert Phillips178ce3e2017-04-13 09:15:47 -0400486}
487
Brian Salomon25a88092016-12-01 09:36:50 -0500488// TODO: this is where GrOp::renderTarget is used (which is fine since it
Robert Phillips294870f2016-11-11 12:38:40 -0500489// is at flush time). However, we need to store the RenderTargetProxy in the
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500490// Ops and instantiate them here.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400491bool GrOpsTask::onExecute(GrOpFlushState* flushState) {
Greg Daniel94ed83f2019-09-27 13:05:43 -0400492 // TODO: remove the check for discard here once reduced op splitting is turned on. Currently we
493 // can end up with GrOpsTasks that only have a discard load op and no ops. For vulkan validation
494 // we need to keep that discard and not drop it. Once we have reduce op list splitting enabled
495 // we shouldn't end up with GrOpsTasks with only discard.
496 if (this->isNoOp() || (fClippedContentBounds.isEmpty() && fColorLoadOp != GrLoadOp::kDiscard)) {
bsalomondc438982016-08-31 11:53:49 -0700497 return false;
egdanielb4021cf2016-07-28 08:53:07 -0700498 }
Robert Phillips4a395042017-04-24 16:27:17 +0000499
Chris Dalton0b68dda2019-11-07 21:08:03 -0700500 SkASSERT(fTargetView.proxy());
501 GrRenderTargetProxy* proxy = fTargetView.proxy()->asRenderTargetProxy();
Greg Daniel16f5c652019-10-29 11:26:01 -0400502 SkASSERT(proxy);
Brian Salomon5f394272019-07-02 14:07:49 -0400503 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400504
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500505 // Make sure load ops are not kClear if the GPU needs to use draws for clears
506 SkASSERT(fColorLoadOp != GrLoadOp::kClear ||
507 !flushState->gpu()->caps()->performColorClearsAsDraws());
Chris Dalton674f77a2019-09-30 20:49:39 -0600508
509 const GrCaps& caps = *flushState->gpu()->caps();
Greg Daniel16f5c652019-10-29 11:26:01 -0400510 GrRenderTarget* renderTarget = proxy->peekRenderTarget();
Chris Dalton674f77a2019-09-30 20:49:39 -0600511 SkASSERT(renderTarget);
Chris Dalton0b68dda2019-11-07 21:08:03 -0700512
513 GrStencilAttachment* stencil = nullptr;
514 if (int numStencilSamples = proxy->numStencilSamples()) {
515 if (!flushState->resourceProvider()->attachStencilAttachment(
516 renderTarget, numStencilSamples)) {
517 SkDebugf("WARNING: failed to attach a stencil buffer. Rendering will be skipped.\n");
518 return false;
519 }
520 stencil = renderTarget->renderTargetPriv().getStencilAttachment();
521 }
522
Chris Daltonf00b95b2019-11-07 21:14:41 -0700523 SkASSERT(!stencil || stencil->numSamples() == proxy->numStencilSamples());
Chris Dalton674f77a2019-09-30 20:49:39 -0600524
525 GrLoadOp stencilLoadOp;
526 switch (fInitialStencilContent) {
527 case StencilContent::kDontCare:
528 stencilLoadOp = GrLoadOp::kDiscard;
529 break;
530 case StencilContent::kUserBitsCleared:
531 SkASSERT(!caps.performStencilClearsAsDraws());
532 SkASSERT(stencil);
533 if (caps.discardStencilValuesAfterRenderPass()) {
534 // Always clear the stencil if it is being discarded after render passes. This is
535 // also an optimization because we are on a tiler and it avoids loading the values
536 // from memory.
537 stencilLoadOp = GrLoadOp::kClear;
538 break;
539 }
540 if (!stencil->hasPerformedInitialClear()) {
541 stencilLoadOp = GrLoadOp::kClear;
542 stencil->markHasPerformedInitialClear();
543 break;
544 }
545 // renderTargetContexts are required to leave the user stencil bits in a cleared state
546 // once finished, meaning the stencil values will always remain cleared after the
547 // initial clear. Just fall through to reloading the existing (cleared) stencil values
548 // from memory.
549 case StencilContent::kPreserved:
550 SkASSERT(stencil);
551 stencilLoadOp = GrLoadOp::kLoad;
552 break;
553 }
554
555 // NOTE: If fMustPreserveStencil is set, then we are executing a renderTargetContext that split
556 // its opsTask.
557 //
558 // FIXME: We don't currently flag render passes that don't use stencil at all. In that case
559 // their store op might be "discard", and we currently make the assumption that a discard will
560 // not invalidate what's already in main memory. This is probably ok for now, but certainly
561 // something we want to address soon.
562 GrStoreOp stencilStoreOp = (caps.discardStencilValuesAfterRenderPass() && !fMustPreserveStencil)
563 ? GrStoreOp::kDiscard
564 : GrStoreOp::kStore;
565
Greg Daniel4a0d36d2019-09-30 12:24:36 -0400566 GrOpsRenderPass* renderPass = create_render_pass(
Greg Daniel16f5c652019-10-29 11:26:01 -0400567 flushState->gpu(), proxy->peekRenderTarget(), fTargetView.origin(),
Chris Dalton674f77a2019-09-30 20:49:39 -0600568 fClippedContentBounds, fColorLoadOp, fLoadClearColor, stencilLoadOp, stencilStoreOp,
569 fSampledProxies);
Greg Danielfa3adf72019-11-07 09:53:41 -0500570 if (!renderPass) {
571 return false;
572 }
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400573 flushState->setOpsRenderPass(renderPass);
574 renderPass->begin();
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400575
576 // Draw all the generated geometry.
Brian Salomon588cec72018-11-14 13:56:37 -0500577 for (const auto& chain : fOpChains) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400578 if (!chain.shouldExecute()) {
bsalomonaecc0182016-03-07 11:50:44 -0800579 continue;
580 }
Stan Iliev2af578d2017-08-16 13:00:28 -0400581#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
Brian Salomon5f394272019-07-02 14:07:49 -0400582 TRACE_EVENT0("skia.gpu", chain.head()->name());
Stan Iliev2af578d2017-08-16 13:00:28 -0400583#endif
Robert Phillips178ce3e2017-04-13 09:15:47 -0400584
Robert Phillips405413f2019-10-04 10:39:28 -0400585 GrOpFlushState::OpArgs opArgs(chain.head(),
Greg Daniel16f5c652019-10-29 11:26:01 -0400586 &fTargetView,
Robert Phillips405413f2019-10-04 10:39:28 -0400587 chain.appliedClip(),
Greg Daniel524e28b2019-11-01 11:48:53 -0400588 chain.dstProxyView());
Robert Phillips178ce3e2017-04-13 09:15:47 -0400589
Brian Salomon29b60c92017-10-31 14:42:10 -0400590 flushState->setOpArgs(&opArgs);
Brian Salomon588cec72018-11-14 13:56:37 -0500591 chain.head()->execute(flushState, chain.bounds());
Brian Salomon29b60c92017-10-31 14:42:10 -0400592 flushState->setOpArgs(nullptr);
bsalomon512be532015-09-10 10:42:55 -0700593 }
Robert Phillips178ce3e2017-04-13 09:15:47 -0400594
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400595 renderPass->end();
596 flushState->gpu()->submit(renderPass);
597 flushState->setOpsRenderPass(nullptr);
ethannicholas22793252016-01-30 09:59:10 -0800598
bsalomondc438982016-08-31 11:53:49 -0700599 return true;
bsalomona73239a2015-04-28 13:35:17 -0700600}
601
Greg Danielf41b2bd2019-08-22 16:19:24 -0400602void GrOpsTask::setColorLoadOp(GrLoadOp op, const SkPMColor4f& color) {
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500603 fColorLoadOp = op;
604 fLoadClearColor = color;
Chris Dalton16a33c62019-09-24 22:19:17 -0600605 if (GrLoadOp::kClear == fColorLoadOp) {
Greg Daniel16f5c652019-10-29 11:26:01 -0400606 GrSurfaceProxy* proxy = fTargetView.proxy();
607 SkASSERT(proxy);
608 fTotalBounds = proxy->getBoundsRect();
Chris Dalton16a33c62019-09-24 22:19:17 -0600609 }
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500610}
611
Greg Danielf41b2bd2019-08-22 16:19:24 -0400612bool GrOpsTask::resetForFullscreenClear(CanDiscardPreviousOps canDiscardPreviousOps) {
Chris Dalton6b982802019-06-27 13:53:46 -0600613 // If we previously recorded a wait op, we cannot delete the wait op. Until we track the wait
614 // ops separately from normal ops, we have to avoid clearing out any ops in this case as well.
615 if (fHasWaitOp) {
616 canDiscardPreviousOps = CanDiscardPreviousOps::kNo;
617 }
618
619 if (CanDiscardPreviousOps::kYes == canDiscardPreviousOps || this->isEmpty()) {
Robert Phillipsc994a932018-06-19 13:09:54 -0400620 this->deleteOps();
Brian Osman099fa0f2017-10-02 16:38:32 -0400621 fDeferredProxies.reset();
Greg Danielb20d7e52019-09-03 13:54:39 -0400622 fSampledProxies.reset();
Greg Daniel070cbaf2019-01-03 17:35:54 -0500623
Greg Danielf41b2bd2019-08-22 16:19:24 -0400624 // If the opsTask is using a render target which wraps a vulkan command buffer, we can't do
625 // a clear load since we cannot change the render pass that we are using. Thus we fall back
626 // to making a clear op in this case.
Greg Daniel16f5c652019-10-29 11:26:01 -0400627 return !fTargetView.asRenderTargetProxy()->wrapsVkSecondaryCB();
bsalomonfd8d0132016-08-11 11:25:33 -0700628 }
Robert Phillips380b90c2017-08-30 07:41:07 -0400629
Greg Danielf41b2bd2019-08-22 16:19:24 -0400630 // Could not empty the task, so an op must be added to handle the clear
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500631 return false;
bsalomon9f129de2016-08-10 16:31:05 -0700632}
633
Greg Danielf41b2bd2019-08-22 16:19:24 -0400634void GrOpsTask::discard() {
635 // Discard calls to in-progress opsTasks are ignored. Calls at the start update the
636 // opsTasks' color & stencil load ops.
637 if (this->isEmpty()) {
638 fColorLoadOp = GrLoadOp::kDiscard;
Chris Dalton674f77a2019-09-30 20:49:39 -0600639 fInitialStencilContent = StencilContent::kDontCare;
Chris Dalton16a33c62019-09-24 22:19:17 -0600640 fTotalBounds.setEmpty();
Greg Danielf41b2bd2019-08-22 16:19:24 -0400641 }
642}
643
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000644////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000645
Greg Danielf41b2bd2019-08-22 16:19:24 -0400646#ifdef SK_DEBUG
Greg Danielf41b2bd2019-08-22 16:19:24 -0400647void GrOpsTask::dump(bool printDependencies) const {
648 GrRenderTask::dump(printDependencies);
649
Chris Dalton674f77a2019-09-30 20:49:39 -0600650 SkDebugf("fColorLoadOp: ");
651 switch (fColorLoadOp) {
652 case GrLoadOp::kLoad:
653 SkDebugf("kLoad\n");
654 break;
655 case GrLoadOp::kClear:
656 SkDebugf("kClear (0x%x)\n", fLoadClearColor.toBytes_RGBA());
657 break;
658 case GrLoadOp::kDiscard:
659 SkDebugf("kDiscard\n");
660 break;
661 }
662
663 SkDebugf("fInitialStencilContent: ");
664 switch (fInitialStencilContent) {
665 case StencilContent::kDontCare:
666 SkDebugf("kDontCare\n");
667 break;
668 case StencilContent::kUserBitsCleared:
669 SkDebugf("kUserBitsCleared\n");
670 break;
671 case StencilContent::kPreserved:
672 SkDebugf("kPreserved\n");
673 break;
674 }
Greg Danielf41b2bd2019-08-22 16:19:24 -0400675
676 SkDebugf("ops (%d):\n", fOpChains.count());
677 for (int i = 0; i < fOpChains.count(); ++i) {
678 SkDebugf("*******************************\n");
679 if (!fOpChains[i].head()) {
680 SkDebugf("%d: <combined forward or failed instantiation>\n", i);
681 } else {
682 SkDebugf("%d: %s\n", i, fOpChains[i].head()->name());
683 SkRect bounds = fOpChains[i].bounds();
684 SkDebugf("ClippedBounds: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", bounds.fLeft,
685 bounds.fTop, bounds.fRight, bounds.fBottom);
686 for (const auto& op : GrOp::ChainRange<>(fOpChains[i].head())) {
687 SkString info = SkTabString(op.dumpInfo(), 1);
688 SkDebugf("%s\n", info.c_str());
689 bounds = op.bounds();
690 SkDebugf("\tClippedBounds: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", bounds.fLeft,
691 bounds.fTop, bounds.fRight, bounds.fBottom);
692 }
693 }
694 }
695}
696
Michael Ludwigfcdd0612019-11-25 08:34:31 -0500697void GrOpsTask::visitProxies_debugOnly(const GrOp::VisitProxyFunc& func) const {
698 auto textureFunc = [ func ] (GrSurfaceProxy* tex, GrMipMapped mipmapped) {
Greg Danieldcf9ca12019-08-27 14:30:21 -0400699 func(tex, mipmapped);
700 };
701
Greg Danielf41b2bd2019-08-22 16:19:24 -0400702 for (const OpChain& chain : fOpChains) {
Greg Danieldcf9ca12019-08-27 14:30:21 -0400703 chain.visitProxies(textureFunc);
Greg Danielf41b2bd2019-08-22 16:19:24 -0400704 }
705}
706
707#endif
708
709////////////////////////////////////////////////////////////////////////////////
710
711bool GrOpsTask::onIsUsed(GrSurfaceProxy* proxyToCheck) const {
712 bool used = false;
713
714 auto visit = [ proxyToCheck, &used ] (GrSurfaceProxy* p, GrMipMapped) {
715 if (p == proxyToCheck) {
716 used = true;
717 }
718 };
719 for (const OpChain& recordedOp : fOpChains) {
720 recordedOp.visitProxies(visit);
721 }
722
723 return used;
724}
725
726void GrOpsTask::handleInternalAllocationFailure() {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500727 bool hasUninstantiatedProxy = false;
Chris Dalton7eb5c0f2019-05-23 15:15:47 -0600728 auto checkInstantiation = [&hasUninstantiatedProxy](GrSurfaceProxy* p, GrMipMapped) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400729 if (!p->isInstantiated()) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500730 hasUninstantiatedProxy = true;
731 }
732 };
Brian Salomon588cec72018-11-14 13:56:37 -0500733 for (OpChain& recordedOp : fOpChains) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500734 hasUninstantiatedProxy = false;
Chris Dalton1706cbf2019-05-21 19:35:29 -0600735 recordedOp.visitProxies(checkInstantiation);
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500736 if (hasUninstantiatedProxy) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400737 recordedOp.setSkipExecuteFlag();
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500738 }
739 }
740}
741
Greg Danielf41b2bd2019-08-22 16:19:24 -0400742void GrOpsTask::gatherProxyIntervals(GrResourceAllocator* alloc) const {
Robert Phillips51b20f22017-12-01 15:32:35 -0500743 for (int i = 0; i < fDeferredProxies.count(); ++i) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400744 SkASSERT(!fDeferredProxies[i]->isInstantiated());
Robert Phillips51b20f22017-12-01 15:32:35 -0500745 // We give all the deferred proxies a write usage at the very start of flushing. This
746 // locks them out of being reused for the entire flush until they are read - and then
747 // they can be recycled. This is a bit unfortunate because a flush can proceed in waves
748 // with sub-flushes. The deferred proxies only need to be pinned from the start of
749 // the sub-flush in which they appear.
Robert Phillipsc73666f2019-04-24 08:49:48 -0400750 alloc->addInterval(fDeferredProxies[i], 0, 0, GrResourceAllocator::ActualUse::kNo);
Robert Phillips51b20f22017-12-01 15:32:35 -0500751 }
752
Greg Daniel16f5c652019-10-29 11:26:01 -0400753 GrSurfaceProxy* targetProxy = fTargetView.proxy();
754
Greg Danielf41b2bd2019-08-22 16:19:24 -0400755 // Add the interval for all the writes to this GrOpsTasks's target
Brian Salomon588cec72018-11-14 13:56:37 -0500756 if (fOpChains.count()) {
Robert Phillips3bf3d4a2019-03-27 07:09:09 -0400757 unsigned int cur = alloc->curOp();
758
Greg Daniel16f5c652019-10-29 11:26:01 -0400759 alloc->addInterval(targetProxy, cur, cur + fOpChains.count() - 1,
Robert Phillipsc73666f2019-04-24 08:49:48 -0400760 GrResourceAllocator::ActualUse::kYes);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500761 } else {
762 // This can happen if there is a loadOp (e.g., a clear) but no other draws. In this case we
763 // still need to add an interval for the destination so we create a fake op# for
764 // the missing clear op.
Greg Daniel16f5c652019-10-29 11:26:01 -0400765 alloc->addInterval(targetProxy, alloc->curOp(), alloc->curOp(),
Robert Phillipsc73666f2019-04-24 08:49:48 -0400766 GrResourceAllocator::ActualUse::kYes);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500767 alloc->incOps();
768 }
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400769
Chris Dalton7eb5c0f2019-05-23 15:15:47 -0600770 auto gather = [ alloc SkDEBUGCODE(, this) ] (GrSurfaceProxy* p, GrMipMapped) {
Robert Phillipsc73666f2019-04-24 08:49:48 -0400771 alloc->addInterval(p, alloc->curOp(), alloc->curOp(), GrResourceAllocator::ActualUse::kYes
Greg Daniel16f5c652019-10-29 11:26:01 -0400772 SkDEBUGCODE(, fTargetView.proxy() == p));
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400773 };
Brian Salomon588cec72018-11-14 13:56:37 -0500774 for (const OpChain& recordedOp : fOpChains) {
Chris Dalton1706cbf2019-05-21 19:35:29 -0600775 recordedOp.visitProxies(gather);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500776
Robert Phillips3bf3d4a2019-03-27 07:09:09 -0400777 // 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 -0500778 // keep all the math consistent.
779 alloc->incOps();
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400780 }
781}
782
Greg Danielf41b2bd2019-08-22 16:19:24 -0400783void GrOpsTask::recordOp(
Chris Dalton945ee652019-01-23 09:10:36 -0700784 std::unique_ptr<GrOp> op, GrProcessorSet::Analysis processorAnalysis, GrAppliedClip* clip,
Greg Daniel524e28b2019-11-01 11:48:53 -0400785 const DstProxyView* dstProxyView, const GrCaps& caps) {
Ethan Nicholas029b22c2018-10-18 16:49:56 -0400786 SkDEBUGCODE(op->validate();)
Greg Daniel524e28b2019-11-01 11:48:53 -0400787 SkASSERT(processorAnalysis.requiresDstTexture() == (dstProxyView && dstProxyView->proxy()));
Greg Daniel16f5c652019-10-29 11:26:01 -0400788 GrSurfaceProxy* proxy = fTargetView.proxy();
789 SkASSERT(proxy);
Robert Phillipsee683652017-04-26 11:53:10 -0400790
Greg Danielf41b2bd2019-08-22 16:19:24 -0400791 // A closed GrOpsTask should never receive new/more ops
robertphillips6a186652015-10-20 07:37:58 -0700792 SkASSERT(!this->isClosed());
Brian Salomon19ec80f2018-11-16 13:27:30 -0500793 if (!op->bounds().isFinite()) {
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500794 fArenas.opMemoryPool()->release(std::move(op));
Brian Salomon19ec80f2018-11-16 13:27:30 -0500795 return;
796 }
robertphillipsa106c622015-10-16 09:07:06 -0700797
Chris Dalton16a33c62019-09-24 22:19:17 -0600798 // Account for this op's bounds before we attempt to combine.
799 // NOTE: The caller should have already called "op->setClippedBounds()" by now, if applicable.
800 fTotalBounds.join(op->bounds());
801
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500802 // Check if there is an op we can combine with by linearly searching back until we either
803 // 1) check every op
bsalomon512be532015-09-10 10:42:55 -0700804 // 2) intersect with something
805 // 3) find a 'blocker'
Greg Daniel16f5c652019-10-29 11:26:01 -0400806 GR_AUDIT_TRAIL_ADD_OP(fAuditTrail, op.get(), proxy->uniqueID());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400807 GrOP_INFO("opsTask: %d Recording (%s, opID: %u)\n"
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400808 "\tBounds [L: %.2f, T: %.2f R: %.2f B: %.2f]\n",
809 this->uniqueID(),
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500810 op->name(),
811 op->uniqueID(),
Robert Phillips1119dc32017-04-11 12:54:57 -0400812 op->bounds().fLeft, op->bounds().fTop,
813 op->bounds().fRight, op->bounds().fBottom);
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500814 GrOP_INFO(SkTabString(op->dumpInfo(), 1).c_str());
Brian Salomon25a88092016-12-01 09:36:50 -0500815 GrOP_INFO("\tOutcome:\n");
Brian Osman788b9162020-02-07 10:36:46 -0500816 int maxCandidates = std::min(kMaxOpChainDistance, fOpChains.count());
Robert Phillips318c4192017-05-17 09:36:38 -0400817 if (maxCandidates) {
bsalomon512be532015-09-10 10:42:55 -0700818 int i = 0;
819 while (true) {
Brian Salomon588cec72018-11-14 13:56:37 -0500820 OpChain& candidate = fOpChains.fromBack(i);
Greg Daniel524e28b2019-11-01 11:48:53 -0400821 op = candidate.appendOp(std::move(op), processorAnalysis, dstProxyView, clip, caps,
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500822 &fArenas, fAuditTrail);
Brian Salomon588cec72018-11-14 13:56:37 -0500823 if (!op) {
824 return;
bsalomon512be532015-09-10 10:42:55 -0700825 }
Brian Salomona7682c82018-10-24 10:04:37 -0400826 // Stop going backwards if we would cause a painter's order violation.
Brian Salomon588cec72018-11-14 13:56:37 -0500827 if (!can_reorder(candidate.bounds(), op->bounds())) {
828 GrOP_INFO("\t\tBackward: Intersects with chain (%s, head opID: %u)\n",
829 candidate.head()->name(), candidate.head()->uniqueID());
bsalomon512be532015-09-10 10:42:55 -0700830 break;
831 }
Brian Salomon588cec72018-11-14 13:56:37 -0500832 if (++i == maxCandidates) {
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400833 GrOP_INFO("\t\tBackward: Reached max lookback or beginning of op array %d\n", i);
bsalomon512be532015-09-10 10:42:55 -0700834 break;
835 }
836 }
837 } else {
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400838 GrOP_INFO("\t\tBackward: FirstOp\n");
bsalomon512be532015-09-10 10:42:55 -0700839 }
Brian Salomon54d212e2017-03-21 14:22:38 -0400840 if (clip) {
841 clip = fClipAllocator.make<GrAppliedClip>(std::move(*clip));
Robert Phillipsc84c0302017-05-08 15:35:11 -0400842 SkDEBUGCODE(fNumClips++;)
Brian Salomon54d212e2017-03-21 14:22:38 -0400843 }
Greg Daniel524e28b2019-11-01 11:48:53 -0400844 fOpChains.emplace_back(std::move(op), processorAnalysis, clip, dstProxyView);
bsalomon512be532015-09-10 10:42:55 -0700845}
846
Greg Danielf41b2bd2019-08-22 16:19:24 -0400847void GrOpsTask::forwardCombine(const GrCaps& caps) {
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400848 SkASSERT(!this->isClosed());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400849 GrOP_INFO("opsTask: %d ForwardCombine %d ops:\n", this->uniqueID(), fOpChains.count());
Robert Phillips48567ac2017-06-01 08:46:00 -0400850
Brian Salomon588cec72018-11-14 13:56:37 -0500851 for (int i = 0; i < fOpChains.count() - 1; ++i) {
852 OpChain& chain = fOpChains[i];
Brian Osman788b9162020-02-07 10:36:46 -0500853 int maxCandidateIdx = std::min(i + kMaxOpChainDistance, fOpChains.count() - 1);
bsalomonaecc0182016-03-07 11:50:44 -0800854 int j = i + 1;
855 while (true) {
Brian Salomon588cec72018-11-14 13:56:37 -0500856 OpChain& candidate = fOpChains[j];
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500857 if (candidate.prependChain(&chain, caps, &fArenas, fAuditTrail)) {
bsalomonaecc0182016-03-07 11:50:44 -0800858 break;
859 }
Robert Phillipsc84c0302017-05-08 15:35:11 -0400860 // Stop traversing if we would cause a painter's order violation.
Brian Salomon588cec72018-11-14 13:56:37 -0500861 if (!can_reorder(chain.bounds(), candidate.bounds())) {
862 GrOP_INFO(
863 "\t\t%d: chain (%s head opID: %u) -> "
864 "Intersects with chain (%s, head opID: %u)\n",
865 i, chain.head()->name(), chain.head()->uniqueID(), candidate.head()->name(),
866 candidate.head()->uniqueID());
bsalomonaecc0182016-03-07 11:50:44 -0800867 break;
868 }
Brian Salomona7682c82018-10-24 10:04:37 -0400869 if (++j > maxCandidateIdx) {
Brian Salomon588cec72018-11-14 13:56:37 -0500870 GrOP_INFO("\t\t%d: chain (%s opID: %u) -> Reached max lookahead or end of array\n",
871 i, chain.head()->name(), chain.head()->uniqueID());
bsalomonaecc0182016-03-07 11:50:44 -0800872 break;
873 }
874 }
875 }
876}
877
Chris Dalton16a33c62019-09-24 22:19:17 -0600878GrRenderTask::ExpectedOutcome GrOpsTask::onMakeClosed(
879 const GrCaps& caps, SkIRect* targetUpdateBounds) {
880 this->forwardCombine(caps);
Brian Salomon3b8486a2020-04-21 12:43:26 -0400881 SkScopeExit triggerObserver([&] {
882 if (fClosedObserver) {
883 fClosedObserver->wasClosed(*this);
884 fClosedObserver = nullptr;
885 }
886 });
Chris Dalton16a33c62019-09-24 22:19:17 -0600887 if (!this->isNoOp()) {
Greg Daniel16f5c652019-10-29 11:26:01 -0400888 GrSurfaceProxy* proxy = fTargetView.proxy();
889 SkRect clippedContentBounds = proxy->getBoundsRect();
890 // TODO: If we can fix up GLPrograms test to always intersect the fTargetView proxy bounds
891 // then we can simply assert here that the bounds intersect.
Chris Dalton16a33c62019-09-24 22:19:17 -0600892 if (clippedContentBounds.intersect(fTotalBounds)) {
Greg Daniel94ed83f2019-09-27 13:05:43 -0400893 clippedContentBounds.roundOut(&fClippedContentBounds);
894 *targetUpdateBounds = fClippedContentBounds;
Chris Dalton16a33c62019-09-24 22:19:17 -0600895 return ExpectedOutcome::kTargetDirty;
896 }
897 }
898 return ExpectedOutcome::kTargetUnchanged;
899}