blob: 97651d21d7f0f88e2e36c56360eee4390041d756 [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"
13#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
42static inline bool can_reorder(const SkRect& a, const SkRect& b) { return !GrRectsOverlap(a, b); }
43
44////////////////////////////////////////////////////////////////////////////////
45
Greg Danielf41b2bd2019-08-22 16:19:24 -040046inline GrOpsTask::OpChain::List::List(std::unique_ptr<GrOp> op)
Brian Salomon588cec72018-11-14 13:56:37 -050047 : fHead(std::move(op)), fTail(fHead.get()) {
48 this->validate();
49}
50
Greg Danielf41b2bd2019-08-22 16:19:24 -040051inline GrOpsTask::OpChain::List::List(List&& that) { *this = std::move(that); }
Brian Salomon588cec72018-11-14 13:56:37 -050052
Greg Danielf41b2bd2019-08-22 16:19:24 -040053inline GrOpsTask::OpChain::List& GrOpsTask::OpChain::List::operator=(List&& that) {
Brian Salomon588cec72018-11-14 13:56:37 -050054 fHead = std::move(that.fHead);
55 fTail = that.fTail;
56 that.fTail = nullptr;
57 this->validate();
58 return *this;
59}
60
Greg Danielf41b2bd2019-08-22 16:19:24 -040061inline std::unique_ptr<GrOp> GrOpsTask::OpChain::List::popHead() {
Brian Salomon588cec72018-11-14 13:56:37 -050062 SkASSERT(fHead);
63 auto temp = fHead->cutChain();
64 std::swap(temp, fHead);
65 if (!fHead) {
66 SkASSERT(fTail == temp.get());
67 fTail = nullptr;
68 }
69 return temp;
70}
71
Greg Danielf41b2bd2019-08-22 16:19:24 -040072inline std::unique_ptr<GrOp> GrOpsTask::OpChain::List::removeOp(GrOp* op) {
Brian Salomon588cec72018-11-14 13:56:37 -050073#ifdef SK_DEBUG
74 auto head = op;
75 while (head->prevInChain()) { head = head->prevInChain(); }
76 SkASSERT(head == fHead.get());
77#endif
78 auto prev = op->prevInChain();
79 if (!prev) {
80 SkASSERT(op == fHead.get());
81 return this->popHead();
82 }
83 auto temp = prev->cutChain();
84 if (auto next = temp->cutChain()) {
85 prev->chainConcat(std::move(next));
86 } else {
87 SkASSERT(fTail == op);
88 fTail = prev;
89 }
90 this->validate();
91 return temp;
92}
93
Greg Danielf41b2bd2019-08-22 16:19:24 -040094inline void GrOpsTask::OpChain::List::pushHead(std::unique_ptr<GrOp> op) {
Brian Salomon588cec72018-11-14 13:56:37 -050095 SkASSERT(op);
96 SkASSERT(op->isChainHead());
97 SkASSERT(op->isChainTail());
98 if (fHead) {
99 op->chainConcat(std::move(fHead));
100 fHead = std::move(op);
101 } else {
102 fHead = std::move(op);
103 fTail = fHead.get();
104 }
105}
106
Greg Danielf41b2bd2019-08-22 16:19:24 -0400107inline void GrOpsTask::OpChain::List::pushTail(std::unique_ptr<GrOp> op) {
Brian Salomon588cec72018-11-14 13:56:37 -0500108 SkASSERT(op->isChainTail());
109 fTail->chainConcat(std::move(op));
110 fTail = fTail->nextInChain();
111}
112
Greg Danielf41b2bd2019-08-22 16:19:24 -0400113inline void GrOpsTask::OpChain::List::validate() const {
Brian Salomon588cec72018-11-14 13:56:37 -0500114#ifdef SK_DEBUG
115 if (fHead) {
116 SkASSERT(fTail);
117 fHead->validateChain(fTail);
118 }
119#endif
120}
121
122////////////////////////////////////////////////////////////////////////////////
123
Greg Danielf41b2bd2019-08-22 16:19:24 -0400124GrOpsTask::OpChain::OpChain(std::unique_ptr<GrOp> op,
125 GrProcessorSet::Analysis processorAnalysis,
Greg Daniel524e28b2019-11-01 11:48:53 -0400126 GrAppliedClip* appliedClip, const DstProxyView* dstProxyView)
Chris Dalton945ee652019-01-23 09:10:36 -0700127 : fList{std::move(op)}
128 , fProcessorAnalysis(processorAnalysis)
129 , fAppliedClip(appliedClip) {
130 if (fProcessorAnalysis.requiresDstTexture()) {
Greg Daniel524e28b2019-11-01 11:48:53 -0400131 SkASSERT(dstProxyView && dstProxyView->proxy());
132 fDstProxyView = *dstProxyView;
Brian Salomon588cec72018-11-14 13:56:37 -0500133 }
134 fBounds = fList.head()->bounds();
135}
136
Greg Danielf41b2bd2019-08-22 16:19:24 -0400137void GrOpsTask::OpChain::visitProxies(const GrOp::VisitProxyFunc& func) const {
Brian Salomon588cec72018-11-14 13:56:37 -0500138 if (fList.empty()) {
139 return;
140 }
141 for (const auto& op : GrOp::ChainRange<>(fList.head())) {
Chris Dalton1706cbf2019-05-21 19:35:29 -0600142 op.visitProxies(func);
Brian Salomon588cec72018-11-14 13:56:37 -0500143 }
Greg Daniel524e28b2019-11-01 11:48:53 -0400144 if (fDstProxyView.proxy()) {
145 func(fDstProxyView.proxy(), GrMipMapped::kNo);
Brian Salomon588cec72018-11-14 13:56:37 -0500146 }
147 if (fAppliedClip) {
148 fAppliedClip->visitProxies(func);
149 }
150}
151
Greg Danielf41b2bd2019-08-22 16:19:24 -0400152void GrOpsTask::OpChain::deleteOps(GrOpMemoryPool* pool) {
Brian Salomon588cec72018-11-14 13:56:37 -0500153 while (!fList.empty()) {
154 pool->release(fList.popHead());
155 }
156}
157
158// Concatenates two op chains and attempts to merge ops across the chains. Assumes that we know that
159// the two chains are chainable. Returns the new chain.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400160GrOpsTask::OpChain::List GrOpsTask::OpChain::DoConcat(
Brian Salomon588cec72018-11-14 13:56:37 -0500161 List chainA, List chainB, const GrCaps& caps, GrOpMemoryPool* pool,
162 GrAuditTrail* auditTrail) {
163 // We process ops in chain b from head to tail. We attempt to merge with nodes in a, starting
164 // at chain a's tail and working toward the head. We produce one of the following outcomes:
165 // 1) b's head is merged into an op in a.
166 // 2) An op from chain a is merged into b's head. (In this case b's head gets processed again.)
167 // 3) b's head is popped from chain a and added at the tail of a.
168 // After result 3 we don't want to attempt to merge the next head of b with the new tail of a,
169 // as we assume merges were already attempted when chain b was created. So we keep track of the
170 // original tail of a and start our iteration of a there. We also track the bounds of the nodes
171 // appended to chain a that will be skipped for bounds testing. If the original tail of a is
172 // merged into an op in b (case 2) then we advance the "original tail" towards the head of a.
173 GrOp* origATail = chainA.tail();
174 SkRect skipBounds = SkRectPriv::MakeLargestInverted();
175 do {
176 int numMergeChecks = 0;
177 bool merged = false;
178 bool noSkip = (origATail == chainA.tail());
179 SkASSERT(noSkip == (skipBounds == SkRectPriv::MakeLargestInverted()));
180 bool canBackwardMerge = noSkip || can_reorder(chainB.head()->bounds(), skipBounds);
181 SkRect forwardMergeBounds = skipBounds;
182 GrOp* a = origATail;
183 while (a) {
184 bool canForwardMerge =
185 (a == chainA.tail()) || can_reorder(a->bounds(), forwardMergeBounds);
186 if (canForwardMerge || canBackwardMerge) {
187 auto result = a->combineIfPossible(chainB.head(), caps);
188 SkASSERT(result != GrOp::CombineResult::kCannotCombine);
189 merged = (result == GrOp::CombineResult::kMerged);
Robert Phillips9548c3b422019-01-08 12:35:43 -0500190 GrOP_INFO("\t\t: (%s opID: %u) -> Combining with (%s, opID: %u)\n",
Brian Salomon588cec72018-11-14 13:56:37 -0500191 chainB.head()->name(), chainB.head()->uniqueID(), a->name(),
192 a->uniqueID());
Brian Salomon588cec72018-11-14 13:56:37 -0500193 }
194 if (merged) {
Brian Salomon52a6ed32018-11-26 10:30:58 -0500195 GR_AUDIT_TRAIL_OPS_RESULT_COMBINED(auditTrail, a, chainB.head());
Brian Salomon588cec72018-11-14 13:56:37 -0500196 if (canBackwardMerge) {
197 pool->release(chainB.popHead());
198 } else {
199 // We merged the contents of b's head into a. We will replace b's head with a in
200 // chain b.
201 SkASSERT(canForwardMerge);
202 if (a == origATail) {
203 origATail = a->prevInChain();
204 }
205 std::unique_ptr<GrOp> detachedA = chainA.removeOp(a);
206 pool->release(chainB.popHead());
207 chainB.pushHead(std::move(detachedA));
208 if (chainA.empty()) {
209 // We merged all the nodes in chain a to chain b.
210 return chainB;
211 }
212 }
213 break;
214 } else {
215 if (++numMergeChecks == kMaxOpMergeDistance) {
216 break;
217 }
218 forwardMergeBounds.joinNonEmptyArg(a->bounds());
219 canBackwardMerge =
220 canBackwardMerge && can_reorder(chainB.head()->bounds(), a->bounds());
221 a = a->prevInChain();
222 }
223 }
224 // If we weren't able to merge b's head then pop b's head from chain b and make it the new
225 // tail of a.
226 if (!merged) {
227 chainA.pushTail(chainB.popHead());
228 skipBounds.joinNonEmptyArg(chainA.tail()->bounds());
229 }
230 } while (!chainB.empty());
231 return chainA;
232}
233
Chris Dalton945ee652019-01-23 09:10:36 -0700234// Attempts to concatenate the given chain onto our own and merge ops across the chains. Returns
235// whether the operation succeeded. On success, the provided list will be returned empty.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400236bool GrOpsTask::OpChain::tryConcat(
Greg Daniel524e28b2019-11-01 11:48:53 -0400237 List* list, GrProcessorSet::Analysis processorAnalysis, const DstProxyView& dstProxyView,
Chris Dalton945ee652019-01-23 09:10:36 -0700238 const GrAppliedClip* appliedClip, const SkRect& bounds, const GrCaps& caps,
239 GrOpMemoryPool* pool, GrAuditTrail* auditTrail) {
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700240 SkASSERT(!fList.empty());
241 SkASSERT(!list->empty());
Greg Daniel524e28b2019-11-01 11:48:53 -0400242 SkASSERT(fProcessorAnalysis.requiresDstTexture() == SkToBool(fDstProxyView.proxy()));
243 SkASSERT(processorAnalysis.requiresDstTexture() == SkToBool(dstProxyView.proxy()));
Brian Salomon588cec72018-11-14 13:56:37 -0500244 // All returns use explicit tuple constructor rather than {a, b} to work around old GCC bug.
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700245 if (fList.head()->classID() != list->head()->classID() ||
246 SkToBool(fAppliedClip) != SkToBool(appliedClip) ||
247 (fAppliedClip && *fAppliedClip != *appliedClip) ||
Chris Dalton945ee652019-01-23 09:10:36 -0700248 (fProcessorAnalysis.requiresNonOverlappingDraws() !=
249 processorAnalysis.requiresNonOverlappingDraws()) ||
250 (fProcessorAnalysis.requiresNonOverlappingDraws() &&
251 // Non-overlaping draws are only required when Ganesh will either insert a barrier,
252 // or read back a new dst texture between draws. In either case, we can neither
253 // chain nor combine overlapping Ops.
254 GrRectsTouchOrOverlap(fBounds, bounds)) ||
255 (fProcessorAnalysis.requiresDstTexture() != processorAnalysis.requiresDstTexture()) ||
Greg Daniel524e28b2019-11-01 11:48:53 -0400256 (fProcessorAnalysis.requiresDstTexture() && fDstProxyView != dstProxyView)) {
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700257 return false;
Brian Salomon588cec72018-11-14 13:56:37 -0500258 }
Chris Daltonee21e6b2019-01-22 14:04:43 -0700259
Brian Salomon588cec72018-11-14 13:56:37 -0500260 SkDEBUGCODE(bool first = true;)
261 do {
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700262 switch (fList.tail()->combineIfPossible(list->head(), caps)) {
Brian Salomon588cec72018-11-14 13:56:37 -0500263 case GrOp::CombineResult::kCannotCombine:
264 // If an op supports chaining then it is required that chaining is transitive and
265 // that if any two ops in two different chains can merge then the two chains
266 // may also be chained together. Thus, we should only hit this on the first
267 // iteration.
268 SkASSERT(first);
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700269 return false;
Brian Salomon588cec72018-11-14 13:56:37 -0500270 case GrOp::CombineResult::kMayChain:
Chris Daltonee21e6b2019-01-22 14:04:43 -0700271 fList = DoConcat(std::move(fList), skstd::exchange(*list, List()), caps, pool,
272 auditTrail);
273 // The above exchange cleared out 'list'. The list needs to be empty now for the
274 // loop to terminate.
275 SkASSERT(list->empty());
276 break;
Brian Salomon588cec72018-11-14 13:56:37 -0500277 case GrOp::CombineResult::kMerged: {
Robert Phillips9548c3b422019-01-08 12:35:43 -0500278 GrOP_INFO("\t\t: (%s opID: %u) -> Combining with (%s, opID: %u)\n",
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700279 list->tail()->name(), list->tail()->uniqueID(), list->head()->name(),
280 list->head()->uniqueID());
281 GR_AUDIT_TRAIL_OPS_RESULT_COMBINED(auditTrail, fList.tail(), list->head());
282 pool->release(list->popHead());
Brian Salomon588cec72018-11-14 13:56:37 -0500283 break;
284 }
285 }
286 SkDEBUGCODE(first = false);
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700287 } while (!list->empty());
Chris Daltonee21e6b2019-01-22 14:04:43 -0700288
289 // The new ops were successfully merged and/or chained onto our own.
290 fBounds.joinPossiblyEmptyRect(bounds);
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700291 return true;
Brian Salomon588cec72018-11-14 13:56:37 -0500292}
293
Greg Danielf41b2bd2019-08-22 16:19:24 -0400294bool GrOpsTask::OpChain::prependChain(OpChain* that, const GrCaps& caps, GrOpMemoryPool* pool,
295 GrAuditTrail* auditTrail) {
Greg Daniel524e28b2019-11-01 11:48:53 -0400296 if (!that->tryConcat(&fList, fProcessorAnalysis, fDstProxyView, fAppliedClip, fBounds, caps,
297 pool, auditTrail)) {
Brian Salomon588cec72018-11-14 13:56:37 -0500298 this->validate();
299 // append failed
300 return false;
301 }
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700302
Brian Salomon588cec72018-11-14 13:56:37 -0500303 // 'that' owns the combined chain. Move it into 'this'.
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700304 SkASSERT(fList.empty());
Brian Salomon588cec72018-11-14 13:56:37 -0500305 fList = std::move(that->fList);
Chris Daltonee21e6b2019-01-22 14:04:43 -0700306 fBounds = that->fBounds;
Brian Salomon588cec72018-11-14 13:56:37 -0500307
Greg Daniel524e28b2019-11-01 11:48:53 -0400308 that->fDstProxyView.setProxyView({});
Brian Salomon588cec72018-11-14 13:56:37 -0500309 if (that->fAppliedClip) {
310 for (int i = 0; i < that->fAppliedClip->numClipCoverageFragmentProcessors(); ++i) {
311 that->fAppliedClip->detachClipCoverageFragmentProcessor(i);
312 }
313 }
314 this->validate();
315 return true;
316}
317
Greg Danielf41b2bd2019-08-22 16:19:24 -0400318std::unique_ptr<GrOp> GrOpsTask::OpChain::appendOp(
Chris Dalton945ee652019-01-23 09:10:36 -0700319 std::unique_ptr<GrOp> op, GrProcessorSet::Analysis processorAnalysis,
Greg Daniel524e28b2019-11-01 11:48:53 -0400320 const DstProxyView* dstProxyView, const GrAppliedClip* appliedClip, const GrCaps& caps,
Chris Dalton945ee652019-01-23 09:10:36 -0700321 GrOpMemoryPool* pool, GrAuditTrail* auditTrail) {
Greg Daniel524e28b2019-11-01 11:48:53 -0400322 const GrXferProcessor::DstProxyView noDstProxyView;
323 if (!dstProxyView) {
324 dstProxyView = &noDstProxyView;
Brian Salomon588cec72018-11-14 13:56:37 -0500325 }
326 SkASSERT(op->isChainHead() && op->isChainTail());
327 SkRect opBounds = op->bounds();
328 List chain(std::move(op));
Chris Dalton945ee652019-01-23 09:10:36 -0700329 if (!this->tryConcat(
Greg Daniel524e28b2019-11-01 11:48:53 -0400330 &chain, processorAnalysis, *dstProxyView, appliedClip, opBounds, caps, pool,
331 auditTrail)) {
Brian Salomon588cec72018-11-14 13:56:37 -0500332 // append failed, give the op back to the caller.
333 this->validate();
334 return chain.popHead();
335 }
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700336
337 SkASSERT(chain.empty());
Brian Salomon588cec72018-11-14 13:56:37 -0500338 this->validate();
339 return nullptr;
340}
341
Greg Danielf41b2bd2019-08-22 16:19:24 -0400342inline void GrOpsTask::OpChain::validate() const {
Brian Salomon588cec72018-11-14 13:56:37 -0500343#ifdef SK_DEBUG
344 fList.validate();
345 for (const auto& op : GrOp::ChainRange<>(fList.head())) {
346 // Not using SkRect::contains because we allow empty rects.
347 SkASSERT(fBounds.fLeft <= op.bounds().fLeft && fBounds.fTop <= op.bounds().fTop &&
348 fBounds.fRight >= op.bounds().fRight && fBounds.fBottom >= op.bounds().fBottom);
349 }
350#endif
351}
352
353////////////////////////////////////////////////////////////////////////////////
bsalomon489147c2015-12-14 12:13:09 -0800354
Greg Danielf41b2bd2019-08-22 16:19:24 -0400355GrOpsTask::GrOpsTask(sk_sp<GrOpMemoryPool> opMemoryPool,
Greg Daniel16f5c652019-10-29 11:26:01 -0400356 GrSurfaceProxyView view,
Greg Danielf41b2bd2019-08-22 16:19:24 -0400357 GrAuditTrail* auditTrail)
Greg Daniel16f5c652019-10-29 11:26:01 -0400358 : GrRenderTask(std::move(view))
Greg Danielf41b2bd2019-08-22 16:19:24 -0400359 , fOpMemoryPool(std::move(opMemoryPool))
360 , fAuditTrail(auditTrail)
Brian Salomonc3833b42018-07-09 18:23:58 +0000361 , fLastClipStackGenID(SK_InvalidUniqueID)
Robert Phillipsb6deea82017-05-11 14:14:30 -0400362 SkDEBUGCODE(, fNumClips(0)) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400363 SkASSERT(fOpMemoryPool);
Greg Daniel16f5c652019-10-29 11:26:01 -0400364 fTargetView.proxy()->setLastRenderTask(this);
bsalomon4061b122015-05-29 10:26:19 -0700365}
366
Greg Danielf41b2bd2019-08-22 16:19:24 -0400367void GrOpsTask::deleteOps() {
Brian Salomon588cec72018-11-14 13:56:37 -0500368 for (auto& chain : fOpChains) {
369 chain.deleteOps(fOpMemoryPool.get());
Robert Phillipsc994a932018-06-19 13:09:54 -0400370 }
Brian Salomon588cec72018-11-14 13:56:37 -0500371 fOpChains.reset();
Robert Phillipsc994a932018-06-19 13:09:54 -0400372}
373
Greg Danielf41b2bd2019-08-22 16:19:24 -0400374GrOpsTask::~GrOpsTask() {
Robert Phillipsc994a932018-06-19 13:09:54 -0400375 this->deleteOps();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000376}
377
378////////////////////////////////////////////////////////////////////////////////
379
Greg Danielf41b2bd2019-08-22 16:19:24 -0400380void GrOpsTask::endFlush() {
381 fLastClipStackGenID = SK_InvalidUniqueID;
382 this->deleteOps();
383 fClipAllocator.reset();
Chris Dalton706a6ff2017-11-29 22:01:06 -0700384
Greg Daniel16f5c652019-10-29 11:26:01 -0400385 GrSurfaceProxy* proxy = fTargetView.proxy();
386 if (proxy && this == proxy->getLastRenderTask()) {
387 proxy->setLastRenderTask(nullptr);
Greg Danielf21bf9e2019-08-22 20:12:20 +0000388 }
Greg Danielf41b2bd2019-08-22 16:19:24 -0400389
Greg Daniel16f5c652019-10-29 11:26:01 -0400390 fTargetView.reset();
Greg Danielf41b2bd2019-08-22 16:19:24 -0400391 fDeferredProxies.reset();
Greg Danielb20d7e52019-09-03 13:54:39 -0400392 fSampledProxies.reset();
Greg Danielf41b2bd2019-08-22 16:19:24 -0400393 fAuditTrail = nullptr;
Greg Danielf21bf9e2019-08-22 20:12:20 +0000394}
395
Robert Phillips29f38542019-10-16 09:20:25 -0400396void GrOpsTask::onPrePrepare(GrRecordingContext* context) {
Robert Phillips7327c9d2019-10-08 16:32:56 -0400397 SkASSERT(this->isClosed());
398#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
399 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
400#endif
401 // TODO: remove the check for discard here once reduced op splitting is turned on. Currently we
402 // can end up with GrOpsTasks that only have a discard load op and no ops. For vulkan validation
403 // we need to keep that discard and not drop it. Once we have reduce op list splitting enabled
404 // we shouldn't end up with GrOpsTasks with only discard.
405 if (this->isNoOp() || (fClippedContentBounds.isEmpty() && fColorLoadOp != GrLoadOp::kDiscard)) {
406 return;
407 }
408
409 for (const auto& chain : fOpChains) {
410 if (chain.shouldExecute()) {
Robert Phillips8053c972019-11-21 10:44:53 -0500411 chain.head()->prePrepare(context,
412 &fTargetView,
413 chain.appliedClip(),
414 chain.dstProxyView());
Robert Phillips7327c9d2019-10-08 16:32:56 -0400415 }
416 }
417}
418
Greg Danielf41b2bd2019-08-22 16:19:24 -0400419void GrOpsTask::onPrepare(GrOpFlushState* flushState) {
Greg Daniel16f5c652019-10-29 11:26:01 -0400420 SkASSERT(fTargetView.proxy()->peekRenderTarget());
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400421 SkASSERT(this->isClosed());
Stan Iliev2af578d2017-08-16 13:00:28 -0400422#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
Brian Salomon5f394272019-07-02 14:07:49 -0400423 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Stan Iliev2af578d2017-08-16 13:00:28 -0400424#endif
Greg Daniel94ed83f2019-09-27 13:05:43 -0400425 // TODO: remove the check for discard here once reduced op splitting is turned on. Currently we
426 // can end up with GrOpsTasks that only have a discard load op and no ops. For vulkan validation
427 // we need to keep that discard and not drop it. Once we have reduce op list splitting enabled
428 // we shouldn't end up with GrOpsTasks with only discard.
429 if (this->isNoOp() || (fClippedContentBounds.isEmpty() && fColorLoadOp != GrLoadOp::kDiscard)) {
430 return;
431 }
robertphillipsa106c622015-10-16 09:07:06 -0700432
Greg Danielb20d7e52019-09-03 13:54:39 -0400433 flushState->setSampledProxyArray(&fSampledProxies);
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500434 // Loop over the ops that haven't yet been prepared.
Brian Salomon588cec72018-11-14 13:56:37 -0500435 for (const auto& chain : fOpChains) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400436 if (chain.shouldExecute()) {
Stan Iliev2af578d2017-08-16 13:00:28 -0400437#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
Brian Salomon5f394272019-07-02 14:07:49 -0400438 TRACE_EVENT0("skia.gpu", chain.head()->name());
Stan Iliev2af578d2017-08-16 13:00:28 -0400439#endif
Robert Phillips901aff02019-10-08 12:32:56 -0400440 GrOpFlushState::OpArgs opArgs(chain.head(),
Greg Daniel16f5c652019-10-29 11:26:01 -0400441 &fTargetView,
Robert Phillips901aff02019-10-08 12:32:56 -0400442 chain.appliedClip(),
Greg Daniel524e28b2019-11-01 11:48:53 -0400443 chain.dstProxyView());
Robert Phillips405413f2019-10-04 10:39:28 -0400444
Brian Salomon29b60c92017-10-31 14:42:10 -0400445 flushState->setOpArgs(&opArgs);
Robert Phillipsdf70f152019-11-15 14:57:05 -0500446
447 // Temporary debugging helper: for debugging prePrepare w/o going through DDLs
448 // Delete once most of the GrOps have an onPrePrepare.
449 // chain.head()->prePrepare(flushState->gpu()->getContext(), &fTargetView,
450 // chain.appliedClip());
451
Robert Phillips7327c9d2019-10-08 16:32:56 -0400452 // GrOp::prePrepare may or may not have been called at this point
Brian Salomon588cec72018-11-14 13:56:37 -0500453 chain.head()->prepare(flushState);
Brian Salomon29b60c92017-10-31 14:42:10 -0400454 flushState->setOpArgs(nullptr);
bsalomonaecc0182016-03-07 11:50:44 -0800455 }
bsalomon512be532015-09-10 10:42:55 -0700456 }
Greg Danielb20d7e52019-09-03 13:54:39 -0400457 flushState->setSampledProxyArray(nullptr);
robertphillipsa13e2022015-11-11 12:01:09 -0800458}
bsalomon512be532015-09-10 10:42:55 -0700459
Greg Daniel4a0d36d2019-09-30 12:24:36 -0400460static GrOpsRenderPass* create_render_pass(
461 GrGpu* gpu, GrRenderTarget* rt, GrSurfaceOrigin origin, const SkIRect& bounds,
Greg Danielb20d7e52019-09-03 13:54:39 -0400462 GrLoadOp colorLoadOp, const SkPMColor4f& loadClearColor, GrLoadOp stencilLoadOp,
Chris Dalton674f77a2019-09-30 20:49:39 -0600463 GrStoreOp stencilStoreOp, const SkTArray<GrTextureProxy*, true>& sampledProxies) {
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400464 const GrOpsRenderPass::LoadAndStoreInfo kColorLoadStoreInfo {
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400465 colorLoadOp,
466 GrStoreOp::kStore,
467 loadClearColor
Robert Phillips178ce3e2017-04-13 09:15:47 -0400468 };
469
Robert Phillips95214472017-08-08 18:00:03 -0400470 // TODO:
471 // We would like to (at this level) only ever clear & discard. We would need
Greg Danielf41b2bd2019-08-22 16:19:24 -0400472 // to stop splitting up higher level OpsTasks for copyOps to achieve that.
Robert Phillips95214472017-08-08 18:00:03 -0400473 // Note: we would still need SB loads and stores but they would happen at a
474 // lower level (inside the VK command buffer).
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400475 const GrOpsRenderPass::StencilLoadAndStoreInfo stencilLoadAndStoreInfo {
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400476 stencilLoadOp,
Chris Dalton674f77a2019-09-30 20:49:39 -0600477 stencilStoreOp,
Robert Phillips95214472017-08-08 18:00:03 -0400478 };
479
Greg Danielb20d7e52019-09-03 13:54:39 -0400480 return gpu->getOpsRenderPass(rt, origin, bounds, kColorLoadStoreInfo, stencilLoadAndStoreInfo,
481 sampledProxies);
Robert Phillips178ce3e2017-04-13 09:15:47 -0400482}
483
Brian Salomon25a88092016-12-01 09:36:50 -0500484// TODO: this is where GrOp::renderTarget is used (which is fine since it
Robert Phillips294870f2016-11-11 12:38:40 -0500485// is at flush time). However, we need to store the RenderTargetProxy in the
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500486// Ops and instantiate them here.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400487bool GrOpsTask::onExecute(GrOpFlushState* flushState) {
Greg Daniel94ed83f2019-09-27 13:05:43 -0400488 // TODO: remove the check for discard here once reduced op splitting is turned on. Currently we
489 // can end up with GrOpsTasks that only have a discard load op and no ops. For vulkan validation
490 // we need to keep that discard and not drop it. Once we have reduce op list splitting enabled
491 // we shouldn't end up with GrOpsTasks with only discard.
492 if (this->isNoOp() || (fClippedContentBounds.isEmpty() && fColorLoadOp != GrLoadOp::kDiscard)) {
bsalomondc438982016-08-31 11:53:49 -0700493 return false;
egdanielb4021cf2016-07-28 08:53:07 -0700494 }
Robert Phillips4a395042017-04-24 16:27:17 +0000495
Chris Dalton0b68dda2019-11-07 21:08:03 -0700496 SkASSERT(fTargetView.proxy());
497 GrRenderTargetProxy* proxy = fTargetView.proxy()->asRenderTargetProxy();
Greg Daniel16f5c652019-10-29 11:26:01 -0400498 SkASSERT(proxy);
Brian Salomon5f394272019-07-02 14:07:49 -0400499 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400500
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500501 // Make sure load ops are not kClear if the GPU needs to use draws for clears
502 SkASSERT(fColorLoadOp != GrLoadOp::kClear ||
503 !flushState->gpu()->caps()->performColorClearsAsDraws());
Chris Dalton674f77a2019-09-30 20:49:39 -0600504
505 const GrCaps& caps = *flushState->gpu()->caps();
Greg Daniel16f5c652019-10-29 11:26:01 -0400506 GrRenderTarget* renderTarget = proxy->peekRenderTarget();
Chris Dalton674f77a2019-09-30 20:49:39 -0600507 SkASSERT(renderTarget);
Chris Dalton0b68dda2019-11-07 21:08:03 -0700508
509 GrStencilAttachment* stencil = nullptr;
510 if (int numStencilSamples = proxy->numStencilSamples()) {
511 if (!flushState->resourceProvider()->attachStencilAttachment(
512 renderTarget, numStencilSamples)) {
513 SkDebugf("WARNING: failed to attach a stencil buffer. Rendering will be skipped.\n");
514 return false;
515 }
516 stencil = renderTarget->renderTargetPriv().getStencilAttachment();
517 }
518
Chris Daltonf00b95b2019-11-07 21:14:41 -0700519 SkASSERT(!stencil || stencil->numSamples() == proxy->numStencilSamples());
Chris Dalton674f77a2019-09-30 20:49:39 -0600520
521 GrLoadOp stencilLoadOp;
522 switch (fInitialStencilContent) {
523 case StencilContent::kDontCare:
524 stencilLoadOp = GrLoadOp::kDiscard;
525 break;
526 case StencilContent::kUserBitsCleared:
527 SkASSERT(!caps.performStencilClearsAsDraws());
528 SkASSERT(stencil);
529 if (caps.discardStencilValuesAfterRenderPass()) {
530 // Always clear the stencil if it is being discarded after render passes. This is
531 // also an optimization because we are on a tiler and it avoids loading the values
532 // from memory.
533 stencilLoadOp = GrLoadOp::kClear;
534 break;
535 }
536 if (!stencil->hasPerformedInitialClear()) {
537 stencilLoadOp = GrLoadOp::kClear;
538 stencil->markHasPerformedInitialClear();
539 break;
540 }
541 // renderTargetContexts are required to leave the user stencil bits in a cleared state
542 // once finished, meaning the stencil values will always remain cleared after the
543 // initial clear. Just fall through to reloading the existing (cleared) stencil values
544 // from memory.
545 case StencilContent::kPreserved:
546 SkASSERT(stencil);
547 stencilLoadOp = GrLoadOp::kLoad;
548 break;
549 }
550
551 // NOTE: If fMustPreserveStencil is set, then we are executing a renderTargetContext that split
552 // its opsTask.
553 //
554 // FIXME: We don't currently flag render passes that don't use stencil at all. In that case
555 // their store op might be "discard", and we currently make the assumption that a discard will
556 // not invalidate what's already in main memory. This is probably ok for now, but certainly
557 // something we want to address soon.
558 GrStoreOp stencilStoreOp = (caps.discardStencilValuesAfterRenderPass() && !fMustPreserveStencil)
559 ? GrStoreOp::kDiscard
560 : GrStoreOp::kStore;
561
Greg Daniel4a0d36d2019-09-30 12:24:36 -0400562 GrOpsRenderPass* renderPass = create_render_pass(
Greg Daniel16f5c652019-10-29 11:26:01 -0400563 flushState->gpu(), proxy->peekRenderTarget(), fTargetView.origin(),
Chris Dalton674f77a2019-09-30 20:49:39 -0600564 fClippedContentBounds, fColorLoadOp, fLoadClearColor, stencilLoadOp, stencilStoreOp,
565 fSampledProxies);
Greg Danielfa3adf72019-11-07 09:53:41 -0500566 if (!renderPass) {
567 return false;
568 }
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400569 flushState->setOpsRenderPass(renderPass);
570 renderPass->begin();
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400571
572 // Draw all the generated geometry.
Brian Salomon588cec72018-11-14 13:56:37 -0500573 for (const auto& chain : fOpChains) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400574 if (!chain.shouldExecute()) {
bsalomonaecc0182016-03-07 11:50:44 -0800575 continue;
576 }
Stan Iliev2af578d2017-08-16 13:00:28 -0400577#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
Brian Salomon5f394272019-07-02 14:07:49 -0400578 TRACE_EVENT0("skia.gpu", chain.head()->name());
Stan Iliev2af578d2017-08-16 13:00:28 -0400579#endif
Robert Phillips178ce3e2017-04-13 09:15:47 -0400580
Robert Phillips405413f2019-10-04 10:39:28 -0400581 GrOpFlushState::OpArgs opArgs(chain.head(),
Greg Daniel16f5c652019-10-29 11:26:01 -0400582 &fTargetView,
Robert Phillips405413f2019-10-04 10:39:28 -0400583 chain.appliedClip(),
Greg Daniel524e28b2019-11-01 11:48:53 -0400584 chain.dstProxyView());
Robert Phillips178ce3e2017-04-13 09:15:47 -0400585
Brian Salomon29b60c92017-10-31 14:42:10 -0400586 flushState->setOpArgs(&opArgs);
Brian Salomon588cec72018-11-14 13:56:37 -0500587 chain.head()->execute(flushState, chain.bounds());
Brian Salomon29b60c92017-10-31 14:42:10 -0400588 flushState->setOpArgs(nullptr);
bsalomon512be532015-09-10 10:42:55 -0700589 }
Robert Phillips178ce3e2017-04-13 09:15:47 -0400590
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400591 renderPass->end();
592 flushState->gpu()->submit(renderPass);
593 flushState->setOpsRenderPass(nullptr);
ethannicholas22793252016-01-30 09:59:10 -0800594
bsalomondc438982016-08-31 11:53:49 -0700595 return true;
bsalomona73239a2015-04-28 13:35:17 -0700596}
597
Greg Danielf41b2bd2019-08-22 16:19:24 -0400598void GrOpsTask::setColorLoadOp(GrLoadOp op, const SkPMColor4f& color) {
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500599 fColorLoadOp = op;
600 fLoadClearColor = color;
Chris Dalton16a33c62019-09-24 22:19:17 -0600601 if (GrLoadOp::kClear == fColorLoadOp) {
Greg Daniel16f5c652019-10-29 11:26:01 -0400602 GrSurfaceProxy* proxy = fTargetView.proxy();
603 SkASSERT(proxy);
604 fTotalBounds = proxy->getBoundsRect();
Chris Dalton16a33c62019-09-24 22:19:17 -0600605 }
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500606}
607
Greg Danielf41b2bd2019-08-22 16:19:24 -0400608bool GrOpsTask::resetForFullscreenClear(CanDiscardPreviousOps canDiscardPreviousOps) {
Chris Dalton6b982802019-06-27 13:53:46 -0600609 // If we previously recorded a wait op, we cannot delete the wait op. Until we track the wait
610 // ops separately from normal ops, we have to avoid clearing out any ops in this case as well.
611 if (fHasWaitOp) {
612 canDiscardPreviousOps = CanDiscardPreviousOps::kNo;
613 }
614
615 if (CanDiscardPreviousOps::kYes == canDiscardPreviousOps || this->isEmpty()) {
Robert Phillipsc994a932018-06-19 13:09:54 -0400616 this->deleteOps();
Brian Osman099fa0f2017-10-02 16:38:32 -0400617 fDeferredProxies.reset();
Greg Danielb20d7e52019-09-03 13:54:39 -0400618 fSampledProxies.reset();
Greg Daniel070cbaf2019-01-03 17:35:54 -0500619
Greg Danielf41b2bd2019-08-22 16:19:24 -0400620 // If the opsTask is using a render target which wraps a vulkan command buffer, we can't do
621 // a clear load since we cannot change the render pass that we are using. Thus we fall back
622 // to making a clear op in this case.
Greg Daniel16f5c652019-10-29 11:26:01 -0400623 return !fTargetView.asRenderTargetProxy()->wrapsVkSecondaryCB();
bsalomonfd8d0132016-08-11 11:25:33 -0700624 }
Robert Phillips380b90c2017-08-30 07:41:07 -0400625
Greg Danielf41b2bd2019-08-22 16:19:24 -0400626 // Could not empty the task, so an op must be added to handle the clear
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500627 return false;
bsalomon9f129de2016-08-10 16:31:05 -0700628}
629
Greg Danielf41b2bd2019-08-22 16:19:24 -0400630void GrOpsTask::discard() {
631 // Discard calls to in-progress opsTasks are ignored. Calls at the start update the
632 // opsTasks' color & stencil load ops.
633 if (this->isEmpty()) {
634 fColorLoadOp = GrLoadOp::kDiscard;
Chris Dalton674f77a2019-09-30 20:49:39 -0600635 fInitialStencilContent = StencilContent::kDontCare;
Chris Dalton16a33c62019-09-24 22:19:17 -0600636 fTotalBounds.setEmpty();
Greg Danielf41b2bd2019-08-22 16:19:24 -0400637 }
638}
639
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000640////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000641
Greg Danielf41b2bd2019-08-22 16:19:24 -0400642#ifdef SK_DEBUG
Greg Danielf41b2bd2019-08-22 16:19:24 -0400643void GrOpsTask::dump(bool printDependencies) const {
644 GrRenderTask::dump(printDependencies);
645
Chris Dalton674f77a2019-09-30 20:49:39 -0600646 SkDebugf("fColorLoadOp: ");
647 switch (fColorLoadOp) {
648 case GrLoadOp::kLoad:
649 SkDebugf("kLoad\n");
650 break;
651 case GrLoadOp::kClear:
652 SkDebugf("kClear (0x%x)\n", fLoadClearColor.toBytes_RGBA());
653 break;
654 case GrLoadOp::kDiscard:
655 SkDebugf("kDiscard\n");
656 break;
657 }
658
659 SkDebugf("fInitialStencilContent: ");
660 switch (fInitialStencilContent) {
661 case StencilContent::kDontCare:
662 SkDebugf("kDontCare\n");
663 break;
664 case StencilContent::kUserBitsCleared:
665 SkDebugf("kUserBitsCleared\n");
666 break;
667 case StencilContent::kPreserved:
668 SkDebugf("kPreserved\n");
669 break;
670 }
Greg Danielf41b2bd2019-08-22 16:19:24 -0400671
672 SkDebugf("ops (%d):\n", fOpChains.count());
673 for (int i = 0; i < fOpChains.count(); ++i) {
674 SkDebugf("*******************************\n");
675 if (!fOpChains[i].head()) {
676 SkDebugf("%d: <combined forward or failed instantiation>\n", i);
677 } else {
678 SkDebugf("%d: %s\n", i, fOpChains[i].head()->name());
679 SkRect bounds = fOpChains[i].bounds();
680 SkDebugf("ClippedBounds: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", bounds.fLeft,
681 bounds.fTop, bounds.fRight, bounds.fBottom);
682 for (const auto& op : GrOp::ChainRange<>(fOpChains[i].head())) {
683 SkString info = SkTabString(op.dumpInfo(), 1);
684 SkDebugf("%s\n", info.c_str());
685 bounds = op.bounds();
686 SkDebugf("\tClippedBounds: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", bounds.fLeft,
687 bounds.fTop, bounds.fRight, bounds.fBottom);
688 }
689 }
690 }
691}
692
Greg Danieldcf9ca12019-08-27 14:30:21 -0400693void GrOpsTask::visitProxies_debugOnly(const VisitSurfaceProxyFunc& func) const {
694 auto textureFunc = [ func ] (GrTextureProxy* tex, GrMipMapped mipmapped) {
695 func(tex, mipmapped);
696 };
697
Greg Danielf41b2bd2019-08-22 16:19:24 -0400698 for (const OpChain& chain : fOpChains) {
Greg Danieldcf9ca12019-08-27 14:30:21 -0400699 chain.visitProxies(textureFunc);
Greg Danielf41b2bd2019-08-22 16:19:24 -0400700 }
701}
702
703#endif
704
705////////////////////////////////////////////////////////////////////////////////
706
707bool GrOpsTask::onIsUsed(GrSurfaceProxy* proxyToCheck) const {
708 bool used = false;
709
710 auto visit = [ proxyToCheck, &used ] (GrSurfaceProxy* p, GrMipMapped) {
711 if (p == proxyToCheck) {
712 used = true;
713 }
714 };
715 for (const OpChain& recordedOp : fOpChains) {
716 recordedOp.visitProxies(visit);
717 }
718
719 return used;
720}
721
722void GrOpsTask::handleInternalAllocationFailure() {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500723 bool hasUninstantiatedProxy = false;
Chris Dalton7eb5c0f2019-05-23 15:15:47 -0600724 auto checkInstantiation = [&hasUninstantiatedProxy](GrSurfaceProxy* p, GrMipMapped) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400725 if (!p->isInstantiated()) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500726 hasUninstantiatedProxy = true;
727 }
728 };
Brian Salomon588cec72018-11-14 13:56:37 -0500729 for (OpChain& recordedOp : fOpChains) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500730 hasUninstantiatedProxy = false;
Chris Dalton1706cbf2019-05-21 19:35:29 -0600731 recordedOp.visitProxies(checkInstantiation);
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500732 if (hasUninstantiatedProxy) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400733 recordedOp.setSkipExecuteFlag();
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500734 }
735 }
736}
737
Greg Danielf41b2bd2019-08-22 16:19:24 -0400738void GrOpsTask::gatherProxyIntervals(GrResourceAllocator* alloc) const {
Robert Phillips51b20f22017-12-01 15:32:35 -0500739 for (int i = 0; i < fDeferredProxies.count(); ++i) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400740 SkASSERT(!fDeferredProxies[i]->isInstantiated());
Robert Phillips51b20f22017-12-01 15:32:35 -0500741 // We give all the deferred proxies a write usage at the very start of flushing. This
742 // locks them out of being reused for the entire flush until they are read - and then
743 // they can be recycled. This is a bit unfortunate because a flush can proceed in waves
744 // with sub-flushes. The deferred proxies only need to be pinned from the start of
745 // the sub-flush in which they appear.
Robert Phillipsc73666f2019-04-24 08:49:48 -0400746 alloc->addInterval(fDeferredProxies[i], 0, 0, GrResourceAllocator::ActualUse::kNo);
Robert Phillips51b20f22017-12-01 15:32:35 -0500747 }
748
Greg Daniel16f5c652019-10-29 11:26:01 -0400749 GrSurfaceProxy* targetProxy = fTargetView.proxy();
750
Greg Danielf41b2bd2019-08-22 16:19:24 -0400751 // Add the interval for all the writes to this GrOpsTasks's target
Brian Salomon588cec72018-11-14 13:56:37 -0500752 if (fOpChains.count()) {
Robert Phillips3bf3d4a2019-03-27 07:09:09 -0400753 unsigned int cur = alloc->curOp();
754
Greg Daniel16f5c652019-10-29 11:26:01 -0400755 alloc->addInterval(targetProxy, cur, cur + fOpChains.count() - 1,
Robert Phillipsc73666f2019-04-24 08:49:48 -0400756 GrResourceAllocator::ActualUse::kYes);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500757 } else {
758 // This can happen if there is a loadOp (e.g., a clear) but no other draws. In this case we
759 // still need to add an interval for the destination so we create a fake op# for
760 // the missing clear op.
Greg Daniel16f5c652019-10-29 11:26:01 -0400761 alloc->addInterval(targetProxy, alloc->curOp(), alloc->curOp(),
Robert Phillipsc73666f2019-04-24 08:49:48 -0400762 GrResourceAllocator::ActualUse::kYes);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500763 alloc->incOps();
764 }
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400765
Chris Dalton7eb5c0f2019-05-23 15:15:47 -0600766 auto gather = [ alloc SkDEBUGCODE(, this) ] (GrSurfaceProxy* p, GrMipMapped) {
Robert Phillipsc73666f2019-04-24 08:49:48 -0400767 alloc->addInterval(p, alloc->curOp(), alloc->curOp(), GrResourceAllocator::ActualUse::kYes
Greg Daniel16f5c652019-10-29 11:26:01 -0400768 SkDEBUGCODE(, fTargetView.proxy() == p));
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400769 };
Brian Salomon588cec72018-11-14 13:56:37 -0500770 for (const OpChain& recordedOp : fOpChains) {
Chris Dalton1706cbf2019-05-21 19:35:29 -0600771 recordedOp.visitProxies(gather);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500772
Robert Phillips3bf3d4a2019-03-27 07:09:09 -0400773 // 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 -0500774 // keep all the math consistent.
775 alloc->incOps();
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400776 }
777}
778
Greg Danielf41b2bd2019-08-22 16:19:24 -0400779void GrOpsTask::recordOp(
Chris Dalton945ee652019-01-23 09:10:36 -0700780 std::unique_ptr<GrOp> op, GrProcessorSet::Analysis processorAnalysis, GrAppliedClip* clip,
Greg Daniel524e28b2019-11-01 11:48:53 -0400781 const DstProxyView* dstProxyView, const GrCaps& caps) {
Ethan Nicholas029b22c2018-10-18 16:49:56 -0400782 SkDEBUGCODE(op->validate();)
Greg Daniel524e28b2019-11-01 11:48:53 -0400783 SkASSERT(processorAnalysis.requiresDstTexture() == (dstProxyView && dstProxyView->proxy()));
Greg Daniel16f5c652019-10-29 11:26:01 -0400784 GrSurfaceProxy* proxy = fTargetView.proxy();
785 SkASSERT(proxy);
Robert Phillipsee683652017-04-26 11:53:10 -0400786
Greg Danielf41b2bd2019-08-22 16:19:24 -0400787 // A closed GrOpsTask should never receive new/more ops
robertphillips6a186652015-10-20 07:37:58 -0700788 SkASSERT(!this->isClosed());
Brian Salomon19ec80f2018-11-16 13:27:30 -0500789 if (!op->bounds().isFinite()) {
790 fOpMemoryPool->release(std::move(op));
791 return;
792 }
robertphillipsa106c622015-10-16 09:07:06 -0700793
Chris Dalton16a33c62019-09-24 22:19:17 -0600794 // Account for this op's bounds before we attempt to combine.
795 // NOTE: The caller should have already called "op->setClippedBounds()" by now, if applicable.
796 fTotalBounds.join(op->bounds());
797
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500798 // Check if there is an op we can combine with by linearly searching back until we either
799 // 1) check every op
bsalomon512be532015-09-10 10:42:55 -0700800 // 2) intersect with something
801 // 3) find a 'blocker'
Greg Daniel16f5c652019-10-29 11:26:01 -0400802 GR_AUDIT_TRAIL_ADD_OP(fAuditTrail, op.get(), proxy->uniqueID());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400803 GrOP_INFO("opsTask: %d Recording (%s, opID: %u)\n"
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400804 "\tBounds [L: %.2f, T: %.2f R: %.2f B: %.2f]\n",
805 this->uniqueID(),
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500806 op->name(),
807 op->uniqueID(),
Robert Phillips1119dc32017-04-11 12:54:57 -0400808 op->bounds().fLeft, op->bounds().fTop,
809 op->bounds().fRight, op->bounds().fBottom);
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500810 GrOP_INFO(SkTabString(op->dumpInfo(), 1).c_str());
Brian Salomon25a88092016-12-01 09:36:50 -0500811 GrOP_INFO("\tOutcome:\n");
Brian Salomon588cec72018-11-14 13:56:37 -0500812 int maxCandidates = SkTMin(kMaxOpChainDistance, fOpChains.count());
Robert Phillips318c4192017-05-17 09:36:38 -0400813 if (maxCandidates) {
bsalomon512be532015-09-10 10:42:55 -0700814 int i = 0;
815 while (true) {
Brian Salomon588cec72018-11-14 13:56:37 -0500816 OpChain& candidate = fOpChains.fromBack(i);
Greg Daniel524e28b2019-11-01 11:48:53 -0400817 op = candidate.appendOp(std::move(op), processorAnalysis, dstProxyView, clip, caps,
Chris Dalton945ee652019-01-23 09:10:36 -0700818 fOpMemoryPool.get(), fAuditTrail);
Brian Salomon588cec72018-11-14 13:56:37 -0500819 if (!op) {
820 return;
bsalomon512be532015-09-10 10:42:55 -0700821 }
Brian Salomona7682c82018-10-24 10:04:37 -0400822 // Stop going backwards if we would cause a painter's order violation.
Brian Salomon588cec72018-11-14 13:56:37 -0500823 if (!can_reorder(candidate.bounds(), op->bounds())) {
824 GrOP_INFO("\t\tBackward: Intersects with chain (%s, head opID: %u)\n",
825 candidate.head()->name(), candidate.head()->uniqueID());
bsalomon512be532015-09-10 10:42:55 -0700826 break;
827 }
Brian Salomon588cec72018-11-14 13:56:37 -0500828 if (++i == maxCandidates) {
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400829 GrOP_INFO("\t\tBackward: Reached max lookback or beginning of op array %d\n", i);
bsalomon512be532015-09-10 10:42:55 -0700830 break;
831 }
832 }
833 } else {
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400834 GrOP_INFO("\t\tBackward: FirstOp\n");
bsalomon512be532015-09-10 10:42:55 -0700835 }
Brian Salomon54d212e2017-03-21 14:22:38 -0400836 if (clip) {
837 clip = fClipAllocator.make<GrAppliedClip>(std::move(*clip));
Robert Phillipsc84c0302017-05-08 15:35:11 -0400838 SkDEBUGCODE(fNumClips++;)
Brian Salomon54d212e2017-03-21 14:22:38 -0400839 }
Greg Daniel524e28b2019-11-01 11:48:53 -0400840 fOpChains.emplace_back(std::move(op), processorAnalysis, clip, dstProxyView);
bsalomon512be532015-09-10 10:42:55 -0700841}
842
Greg Danielf41b2bd2019-08-22 16:19:24 -0400843void GrOpsTask::forwardCombine(const GrCaps& caps) {
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400844 SkASSERT(!this->isClosed());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400845 GrOP_INFO("opsTask: %d ForwardCombine %d ops:\n", this->uniqueID(), fOpChains.count());
Robert Phillips48567ac2017-06-01 08:46:00 -0400846
Brian Salomon588cec72018-11-14 13:56:37 -0500847 for (int i = 0; i < fOpChains.count() - 1; ++i) {
848 OpChain& chain = fOpChains[i];
849 int maxCandidateIdx = SkTMin(i + kMaxOpChainDistance, fOpChains.count() - 1);
bsalomonaecc0182016-03-07 11:50:44 -0800850 int j = i + 1;
851 while (true) {
Brian Salomon588cec72018-11-14 13:56:37 -0500852 OpChain& candidate = fOpChains[j];
853 if (candidate.prependChain(&chain, caps, fOpMemoryPool.get(), fAuditTrail)) {
bsalomonaecc0182016-03-07 11:50:44 -0800854 break;
855 }
Robert Phillipsc84c0302017-05-08 15:35:11 -0400856 // Stop traversing if we would cause a painter's order violation.
Brian Salomon588cec72018-11-14 13:56:37 -0500857 if (!can_reorder(chain.bounds(), candidate.bounds())) {
858 GrOP_INFO(
859 "\t\t%d: chain (%s head opID: %u) -> "
860 "Intersects with chain (%s, head opID: %u)\n",
861 i, chain.head()->name(), chain.head()->uniqueID(), candidate.head()->name(),
862 candidate.head()->uniqueID());
bsalomonaecc0182016-03-07 11:50:44 -0800863 break;
864 }
Brian Salomona7682c82018-10-24 10:04:37 -0400865 if (++j > maxCandidateIdx) {
Brian Salomon588cec72018-11-14 13:56:37 -0500866 GrOP_INFO("\t\t%d: chain (%s opID: %u) -> Reached max lookahead or end of array\n",
867 i, chain.head()->name(), chain.head()->uniqueID());
bsalomonaecc0182016-03-07 11:50:44 -0800868 break;
869 }
870 }
871 }
872}
873
Chris Dalton16a33c62019-09-24 22:19:17 -0600874GrRenderTask::ExpectedOutcome GrOpsTask::onMakeClosed(
875 const GrCaps& caps, SkIRect* targetUpdateBounds) {
876 this->forwardCombine(caps);
877 if (!this->isNoOp()) {
Greg Daniel16f5c652019-10-29 11:26:01 -0400878 GrSurfaceProxy* proxy = fTargetView.proxy();
879 SkRect clippedContentBounds = proxy->getBoundsRect();
880 // TODO: If we can fix up GLPrograms test to always intersect the fTargetView proxy bounds
881 // then we can simply assert here that the bounds intersect.
Chris Dalton16a33c62019-09-24 22:19:17 -0600882 if (clippedContentBounds.intersect(fTotalBounds)) {
Greg Daniel94ed83f2019-09-27 13:05:43 -0400883 clippedContentBounds.roundOut(&fClippedContentBounds);
884 *targetUpdateBounds = fClippedContentBounds;
Chris Dalton16a33c62019-09-24 22:19:17 -0600885 return ExpectedOutcome::kTargetDirty;
886 }
887 }
888 return ExpectedOutcome::kTargetUnchanged;
889}