blob: 1e170d99ec356e5c58861a05e2b03d8a4041d584 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2010 Google Inc.
3 *
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
Brian Salomon4d2d6f42019-07-26 14:15:11 -04008#include "src/gpu/GrRenderTargetOpList.h"
9
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"
17#include "src/gpu/GrGpuCommandBuffer.h"
18#include "src/gpu/GrMemoryPool.h"
19#include "src/gpu/GrRecordingContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/GrRenderTargetContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/GrResourceAllocator.h"
Chris Dalton95d8ceb2019-07-30 11:17:59 -060022#include "src/gpu/GrTexturePriv.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040023#include "src/gpu/geometry/GrRect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/ops/GrClearOp.h"
25#include "src/gpu/ops/GrCopySurfaceOp.h"
Brian Salomon4d2d6f42019-07-26 14:15:11 -040026#include "src/gpu/ops/GrTransferFromOp.h"
Robert Phillipsf2361d22016-10-25 14:20:06 -040027
reed@google.comac10a2d2010-12-22 21:39:39 +000028////////////////////////////////////////////////////////////////////////////////
29
Brian Salomon09d994e2016-12-21 11:14:46 -050030// Experimentally we have found that most combining occurs within the first 10 comparisons.
Brian Salomon588cec72018-11-14 13:56:37 -050031static const int kMaxOpMergeDistance = 10;
32static const int kMaxOpChainDistance = 10;
33
34////////////////////////////////////////////////////////////////////////////////
35
36using DstProxy = GrXferProcessor::DstProxy;
37
38////////////////////////////////////////////////////////////////////////////////
39
40static inline bool can_reorder(const SkRect& a, const SkRect& b) { return !GrRectsOverlap(a, b); }
41
42////////////////////////////////////////////////////////////////////////////////
43
44inline GrRenderTargetOpList::OpChain::List::List(std::unique_ptr<GrOp> op)
45 : fHead(std::move(op)), fTail(fHead.get()) {
46 this->validate();
47}
48
49inline GrRenderTargetOpList::OpChain::List::List(List&& that) { *this = std::move(that); }
50
51inline GrRenderTargetOpList::OpChain::List& GrRenderTargetOpList::OpChain::List::operator=(
52 List&& that) {
53 fHead = std::move(that.fHead);
54 fTail = that.fTail;
55 that.fTail = nullptr;
56 this->validate();
57 return *this;
58}
59
60inline std::unique_ptr<GrOp> GrRenderTargetOpList::OpChain::List::popHead() {
61 SkASSERT(fHead);
62 auto temp = fHead->cutChain();
63 std::swap(temp, fHead);
64 if (!fHead) {
65 SkASSERT(fTail == temp.get());
66 fTail = nullptr;
67 }
68 return temp;
69}
70
71inline std::unique_ptr<GrOp> GrRenderTargetOpList::OpChain::List::removeOp(GrOp* op) {
72#ifdef SK_DEBUG
73 auto head = op;
74 while (head->prevInChain()) { head = head->prevInChain(); }
75 SkASSERT(head == fHead.get());
76#endif
77 auto prev = op->prevInChain();
78 if (!prev) {
79 SkASSERT(op == fHead.get());
80 return this->popHead();
81 }
82 auto temp = prev->cutChain();
83 if (auto next = temp->cutChain()) {
84 prev->chainConcat(std::move(next));
85 } else {
86 SkASSERT(fTail == op);
87 fTail = prev;
88 }
89 this->validate();
90 return temp;
91}
92
93inline void GrRenderTargetOpList::OpChain::List::pushHead(std::unique_ptr<GrOp> op) {
94 SkASSERT(op);
95 SkASSERT(op->isChainHead());
96 SkASSERT(op->isChainTail());
97 if (fHead) {
98 op->chainConcat(std::move(fHead));
99 fHead = std::move(op);
100 } else {
101 fHead = std::move(op);
102 fTail = fHead.get();
103 }
104}
105
106inline void GrRenderTargetOpList::OpChain::List::pushTail(std::unique_ptr<GrOp> op) {
107 SkASSERT(op->isChainTail());
108 fTail->chainConcat(std::move(op));
109 fTail = fTail->nextInChain();
110}
111
112inline void GrRenderTargetOpList::OpChain::List::validate() const {
113#ifdef SK_DEBUG
114 if (fHead) {
115 SkASSERT(fTail);
116 fHead->validateChain(fTail);
117 }
118#endif
119}
120
121////////////////////////////////////////////////////////////////////////////////
122
Chris Dalton945ee652019-01-23 09:10:36 -0700123GrRenderTargetOpList::OpChain::OpChain(std::unique_ptr<GrOp> op,
124 GrProcessorSet::Analysis processorAnalysis,
125 GrAppliedClip* appliedClip, const DstProxy* dstProxy)
126 : fList{std::move(op)}
127 , fProcessorAnalysis(processorAnalysis)
128 , fAppliedClip(appliedClip) {
129 if (fProcessorAnalysis.requiresDstTexture()) {
130 SkASSERT(dstProxy && dstProxy->proxy());
Brian Salomon588cec72018-11-14 13:56:37 -0500131 fDstProxy = *dstProxy;
132 }
133 fBounds = fList.head()->bounds();
134}
135
Chris Dalton1706cbf2019-05-21 19:35:29 -0600136void GrRenderTargetOpList::OpChain::visitProxies(const GrOp::VisitProxyFunc& func) const {
Brian Salomon588cec72018-11-14 13:56:37 -0500137 if (fList.empty()) {
138 return;
139 }
140 for (const auto& op : GrOp::ChainRange<>(fList.head())) {
Chris Dalton1706cbf2019-05-21 19:35:29 -0600141 op.visitProxies(func);
Brian Salomon588cec72018-11-14 13:56:37 -0500142 }
143 if (fDstProxy.proxy()) {
Chris Dalton7eb5c0f2019-05-23 15:15:47 -0600144 func(fDstProxy.proxy(), GrMipMapped::kNo);
Brian Salomon588cec72018-11-14 13:56:37 -0500145 }
146 if (fAppliedClip) {
147 fAppliedClip->visitProxies(func);
148 }
149}
150
151void GrRenderTargetOpList::OpChain::deleteOps(GrOpMemoryPool* pool) {
152 while (!fList.empty()) {
153 pool->release(fList.popHead());
154 }
155}
156
157// Concatenates two op chains and attempts to merge ops across the chains. Assumes that we know that
158// the two chains are chainable. Returns the new chain.
159GrRenderTargetOpList::OpChain::List GrRenderTargetOpList::OpChain::DoConcat(
160 List chainA, List chainB, const GrCaps& caps, GrOpMemoryPool* pool,
161 GrAuditTrail* auditTrail) {
162 // We process ops in chain b from head to tail. We attempt to merge with nodes in a, starting
163 // at chain a's tail and working toward the head. We produce one of the following outcomes:
164 // 1) b's head is merged into an op in a.
165 // 2) An op from chain a is merged into b's head. (In this case b's head gets processed again.)
166 // 3) b's head is popped from chain a and added at the tail of a.
167 // After result 3 we don't want to attempt to merge the next head of b with the new tail of a,
168 // as we assume merges were already attempted when chain b was created. So we keep track of the
169 // original tail of a and start our iteration of a there. We also track the bounds of the nodes
170 // appended to chain a that will be skipped for bounds testing. If the original tail of a is
171 // merged into an op in b (case 2) then we advance the "original tail" towards the head of a.
172 GrOp* origATail = chainA.tail();
173 SkRect skipBounds = SkRectPriv::MakeLargestInverted();
174 do {
175 int numMergeChecks = 0;
176 bool merged = false;
177 bool noSkip = (origATail == chainA.tail());
178 SkASSERT(noSkip == (skipBounds == SkRectPriv::MakeLargestInverted()));
179 bool canBackwardMerge = noSkip || can_reorder(chainB.head()->bounds(), skipBounds);
180 SkRect forwardMergeBounds = skipBounds;
181 GrOp* a = origATail;
182 while (a) {
183 bool canForwardMerge =
184 (a == chainA.tail()) || can_reorder(a->bounds(), forwardMergeBounds);
185 if (canForwardMerge || canBackwardMerge) {
186 auto result = a->combineIfPossible(chainB.head(), caps);
187 SkASSERT(result != GrOp::CombineResult::kCannotCombine);
188 merged = (result == GrOp::CombineResult::kMerged);
Robert Phillips9548c3b422019-01-08 12:35:43 -0500189 GrOP_INFO("\t\t: (%s opID: %u) -> Combining with (%s, opID: %u)\n",
Brian Salomon588cec72018-11-14 13:56:37 -0500190 chainB.head()->name(), chainB.head()->uniqueID(), a->name(),
191 a->uniqueID());
Brian Salomon588cec72018-11-14 13:56:37 -0500192 }
193 if (merged) {
Brian Salomon52a6ed32018-11-26 10:30:58 -0500194 GR_AUDIT_TRAIL_OPS_RESULT_COMBINED(auditTrail, a, chainB.head());
Brian Salomon588cec72018-11-14 13:56:37 -0500195 if (canBackwardMerge) {
196 pool->release(chainB.popHead());
197 } else {
198 // We merged the contents of b's head into a. We will replace b's head with a in
199 // chain b.
200 SkASSERT(canForwardMerge);
201 if (a == origATail) {
202 origATail = a->prevInChain();
203 }
204 std::unique_ptr<GrOp> detachedA = chainA.removeOp(a);
205 pool->release(chainB.popHead());
206 chainB.pushHead(std::move(detachedA));
207 if (chainA.empty()) {
208 // We merged all the nodes in chain a to chain b.
209 return chainB;
210 }
211 }
212 break;
213 } else {
214 if (++numMergeChecks == kMaxOpMergeDistance) {
215 break;
216 }
217 forwardMergeBounds.joinNonEmptyArg(a->bounds());
218 canBackwardMerge =
219 canBackwardMerge && can_reorder(chainB.head()->bounds(), a->bounds());
220 a = a->prevInChain();
221 }
222 }
223 // If we weren't able to merge b's head then pop b's head from chain b and make it the new
224 // tail of a.
225 if (!merged) {
226 chainA.pushTail(chainB.popHead());
227 skipBounds.joinNonEmptyArg(chainA.tail()->bounds());
228 }
229 } while (!chainB.empty());
230 return chainA;
231}
232
Chris Dalton945ee652019-01-23 09:10:36 -0700233// Attempts to concatenate the given chain onto our own and merge ops across the chains. Returns
234// whether the operation succeeded. On success, the provided list will be returned empty.
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700235bool GrRenderTargetOpList::OpChain::tryConcat(
Chris Dalton945ee652019-01-23 09:10:36 -0700236 List* list, GrProcessorSet::Analysis processorAnalysis, const DstProxy& dstProxy,
237 const GrAppliedClip* appliedClip, const SkRect& bounds, const GrCaps& caps,
238 GrOpMemoryPool* pool, GrAuditTrail* auditTrail) {
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700239 SkASSERT(!fList.empty());
240 SkASSERT(!list->empty());
Chris Dalton945ee652019-01-23 09:10:36 -0700241 SkASSERT(fProcessorAnalysis.requiresDstTexture() == SkToBool(fDstProxy.proxy()));
242 SkASSERT(processorAnalysis.requiresDstTexture() == SkToBool(dstProxy.proxy()));
Brian Salomon588cec72018-11-14 13:56:37 -0500243 // All returns use explicit tuple constructor rather than {a, b} to work around old GCC bug.
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700244 if (fList.head()->classID() != list->head()->classID() ||
245 SkToBool(fAppliedClip) != SkToBool(appliedClip) ||
246 (fAppliedClip && *fAppliedClip != *appliedClip) ||
Chris Dalton945ee652019-01-23 09:10:36 -0700247 (fProcessorAnalysis.requiresNonOverlappingDraws() !=
248 processorAnalysis.requiresNonOverlappingDraws()) ||
249 (fProcessorAnalysis.requiresNonOverlappingDraws() &&
250 // Non-overlaping draws are only required when Ganesh will either insert a barrier,
251 // or read back a new dst texture between draws. In either case, we can neither
252 // chain nor combine overlapping Ops.
253 GrRectsTouchOrOverlap(fBounds, bounds)) ||
254 (fProcessorAnalysis.requiresDstTexture() != processorAnalysis.requiresDstTexture()) ||
255 (fProcessorAnalysis.requiresDstTexture() && fDstProxy != dstProxy)) {
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700256 return false;
Brian Salomon588cec72018-11-14 13:56:37 -0500257 }
Chris Daltonee21e6b2019-01-22 14:04:43 -0700258
Brian Salomon588cec72018-11-14 13:56:37 -0500259 SkDEBUGCODE(bool first = true;)
260 do {
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700261 switch (fList.tail()->combineIfPossible(list->head(), caps)) {
Brian Salomon588cec72018-11-14 13:56:37 -0500262 case GrOp::CombineResult::kCannotCombine:
263 // If an op supports chaining then it is required that chaining is transitive and
264 // that if any two ops in two different chains can merge then the two chains
265 // may also be chained together. Thus, we should only hit this on the first
266 // iteration.
267 SkASSERT(first);
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700268 return false;
Brian Salomon588cec72018-11-14 13:56:37 -0500269 case GrOp::CombineResult::kMayChain:
Chris Daltonee21e6b2019-01-22 14:04:43 -0700270 fList = DoConcat(std::move(fList), skstd::exchange(*list, List()), caps, pool,
271 auditTrail);
272 // The above exchange cleared out 'list'. The list needs to be empty now for the
273 // loop to terminate.
274 SkASSERT(list->empty());
275 break;
Brian Salomon588cec72018-11-14 13:56:37 -0500276 case GrOp::CombineResult::kMerged: {
Robert Phillips9548c3b422019-01-08 12:35:43 -0500277 GrOP_INFO("\t\t: (%s opID: %u) -> Combining with (%s, opID: %u)\n",
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700278 list->tail()->name(), list->tail()->uniqueID(), list->head()->name(),
279 list->head()->uniqueID());
280 GR_AUDIT_TRAIL_OPS_RESULT_COMBINED(auditTrail, fList.tail(), list->head());
281 pool->release(list->popHead());
Brian Salomon588cec72018-11-14 13:56:37 -0500282 break;
283 }
284 }
285 SkDEBUGCODE(first = false);
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700286 } while (!list->empty());
Chris Daltonee21e6b2019-01-22 14:04:43 -0700287
288 // The new ops were successfully merged and/or chained onto our own.
289 fBounds.joinPossiblyEmptyRect(bounds);
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700290 return true;
Brian Salomon588cec72018-11-14 13:56:37 -0500291}
292
293bool GrRenderTargetOpList::OpChain::prependChain(OpChain* that, const GrCaps& caps,
294 GrOpMemoryPool* pool, GrAuditTrail* auditTrail) {
Chris Dalton945ee652019-01-23 09:10:36 -0700295 if (!that->tryConcat(
296 &fList, fProcessorAnalysis, fDstProxy, fAppliedClip, fBounds, caps, pool, auditTrail)) {
Brian Salomon588cec72018-11-14 13:56:37 -0500297 this->validate();
298 // append failed
299 return false;
300 }
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700301
Brian Salomon588cec72018-11-14 13:56:37 -0500302 // 'that' owns the combined chain. Move it into 'this'.
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700303 SkASSERT(fList.empty());
Brian Salomon588cec72018-11-14 13:56:37 -0500304 fList = std::move(that->fList);
Chris Daltonee21e6b2019-01-22 14:04:43 -0700305 fBounds = that->fBounds;
Brian Salomon588cec72018-11-14 13:56:37 -0500306
307 that->fDstProxy.setProxy(nullptr);
308 if (that->fAppliedClip) {
309 for (int i = 0; i < that->fAppliedClip->numClipCoverageFragmentProcessors(); ++i) {
310 that->fAppliedClip->detachClipCoverageFragmentProcessor(i);
311 }
312 }
313 this->validate();
314 return true;
315}
316
Chris Dalton945ee652019-01-23 09:10:36 -0700317std::unique_ptr<GrOp> GrRenderTargetOpList::OpChain::appendOp(
318 std::unique_ptr<GrOp> op, GrProcessorSet::Analysis processorAnalysis,
319 const DstProxy* dstProxy, const GrAppliedClip* appliedClip, const GrCaps& caps,
320 GrOpMemoryPool* pool, GrAuditTrail* auditTrail) {
Brian Salomon588cec72018-11-14 13:56:37 -0500321 const GrXferProcessor::DstProxy noDstProxy;
322 if (!dstProxy) {
323 dstProxy = &noDstProxy;
324 }
325 SkASSERT(op->isChainHead() && op->isChainTail());
326 SkRect opBounds = op->bounds();
327 List chain(std::move(op));
Chris Dalton945ee652019-01-23 09:10:36 -0700328 if (!this->tryConcat(
329 &chain, processorAnalysis, *dstProxy, appliedClip, opBounds, caps, pool, auditTrail)) {
Brian Salomon588cec72018-11-14 13:56:37 -0500330 // append failed, give the op back to the caller.
331 this->validate();
332 return chain.popHead();
333 }
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700334
335 SkASSERT(chain.empty());
Brian Salomon588cec72018-11-14 13:56:37 -0500336 this->validate();
337 return nullptr;
338}
339
340inline void GrRenderTargetOpList::OpChain::validate() const {
341#ifdef SK_DEBUG
342 fList.validate();
343 for (const auto& op : GrOp::ChainRange<>(fList.head())) {
344 // Not using SkRect::contains because we allow empty rects.
345 SkASSERT(fBounds.fLeft <= op.bounds().fLeft && fBounds.fTop <= op.bounds().fTop &&
346 fBounds.fRight >= op.bounds().fRight && fBounds.fBottom >= op.bounds().fBottom);
347 }
348#endif
349}
350
351////////////////////////////////////////////////////////////////////////////////
bsalomon489147c2015-12-14 12:13:09 -0800352
Robert Phillips12c46292019-04-23 07:36:17 -0400353GrRenderTargetOpList::GrRenderTargetOpList(sk_sp<GrOpMemoryPool> opMemoryPool,
Robert Phillips831a2932019-04-12 17:18:39 -0400354 sk_sp<GrRenderTargetProxy> proxy,
Robert Phillips8185f592017-04-26 08:31:08 -0400355 GrAuditTrail* auditTrail)
Robert Phillips12c46292019-04-23 07:36:17 -0400356 : INHERITED(std::move(opMemoryPool), std::move(proxy), auditTrail)
Brian Salomonc3833b42018-07-09 18:23:58 +0000357 , fLastClipStackGenID(SK_InvalidUniqueID)
Robert Phillipsb6deea82017-05-11 14:14:30 -0400358 SkDEBUGCODE(, fNumClips(0)) {
bsalomon4061b122015-05-29 10:26:19 -0700359}
360
Robert Phillipsc994a932018-06-19 13:09:54 -0400361void GrRenderTargetOpList::deleteOps() {
Brian Salomon588cec72018-11-14 13:56:37 -0500362 for (auto& chain : fOpChains) {
363 chain.deleteOps(fOpMemoryPool.get());
Robert Phillipsc994a932018-06-19 13:09:54 -0400364 }
Brian Salomon588cec72018-11-14 13:56:37 -0500365 fOpChains.reset();
Robert Phillipsc994a932018-06-19 13:09:54 -0400366}
367
Robert Phillipsf2361d22016-10-25 14:20:06 -0400368GrRenderTargetOpList::~GrRenderTargetOpList() {
Robert Phillipsc994a932018-06-19 13:09:54 -0400369 this->deleteOps();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000370}
371
372////////////////////////////////////////////////////////////////////////////////
373
robertphillips4beb5c12015-10-20 07:50:00 -0700374#ifdef SK_DEBUG
Robert Phillips27483912018-04-20 12:43:18 -0400375void GrRenderTargetOpList::dump(bool printDependencies) const {
376 INHERITED::dump(printDependencies);
Robert Phillipsf2361d22016-10-25 14:20:06 -0400377
Brian Salomon588cec72018-11-14 13:56:37 -0500378 SkDebugf("ops (%d):\n", fOpChains.count());
379 for (int i = 0; i < fOpChains.count(); ++i) {
robertphillips4beb5c12015-10-20 07:50:00 -0700380 SkDebugf("*******************************\n");
Brian Salomon588cec72018-11-14 13:56:37 -0500381 if (!fOpChains[i].head()) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500382 SkDebugf("%d: <combined forward or failed instantiation>\n", i);
bsalomonaecc0182016-03-07 11:50:44 -0800383 } else {
Brian Salomon588cec72018-11-14 13:56:37 -0500384 SkDebugf("%d: %s\n", i, fOpChains[i].head()->name());
385 SkRect bounds = fOpChains[i].bounds();
Brian Salomon9e50f7b2017-03-06 12:02:34 -0500386 SkDebugf("ClippedBounds: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", bounds.fLeft,
387 bounds.fTop, bounds.fRight, bounds.fBottom);
Brian Salomon588cec72018-11-14 13:56:37 -0500388 for (const auto& op : GrOp::ChainRange<>(fOpChains[i].head())) {
389 SkString info = SkTabString(op.dumpInfo(), 1);
390 SkDebugf("%s\n", info.c_str());
391 bounds = op.bounds();
392 SkDebugf("\tClippedBounds: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", bounds.fLeft,
393 bounds.fTop, bounds.fRight, bounds.fBottom);
394 }
bsalomonaecc0182016-03-07 11:50:44 -0800395 }
robertphillips4beb5c12015-10-20 07:50:00 -0700396 }
397}
Chris Dalton706a6ff2017-11-29 22:01:06 -0700398
399void GrRenderTargetOpList::visitProxies_debugOnly(const GrOp::VisitProxyFunc& func) const {
Brian Salomon588cec72018-11-14 13:56:37 -0500400 for (const OpChain& chain : fOpChains) {
Chris Dalton1706cbf2019-05-21 19:35:29 -0600401 chain.visitProxies(func);
Chris Dalton706a6ff2017-11-29 22:01:06 -0700402 }
403}
Brian Salomonc525d4f2018-09-17 15:48:20 -0400404
robertphillips4beb5c12015-10-20 07:50:00 -0700405#endif
406
Brian Osman407b3422017-08-22 15:01:32 -0400407void GrRenderTargetOpList::onPrepare(GrOpFlushState* flushState) {
Robert Phillipsb5204762019-06-19 14:12:13 -0400408 SkASSERT(fTarget->peekRenderTarget());
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400409 SkASSERT(this->isClosed());
Stan Iliev2af578d2017-08-16 13:00:28 -0400410#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
Brian Salomon5f394272019-07-02 14:07:49 -0400411 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Stan Iliev2af578d2017-08-16 13:00:28 -0400412#endif
robertphillipsa106c622015-10-16 09:07:06 -0700413
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500414 // Loop over the ops that haven't yet been prepared.
Brian Salomon588cec72018-11-14 13:56:37 -0500415 for (const auto& chain : fOpChains) {
416 if (chain.head()) {
Stan Iliev2af578d2017-08-16 13:00:28 -0400417#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
Brian Salomon5f394272019-07-02 14:07:49 -0400418 TRACE_EVENT0("skia.gpu", chain.head()->name());
Stan Iliev2af578d2017-08-16 13:00:28 -0400419#endif
Brian Salomon29b60c92017-10-31 14:42:10 -0400420 GrOpFlushState::OpArgs opArgs = {
Brian Salomon588cec72018-11-14 13:56:37 -0500421 chain.head(),
Robert Phillipsb5204762019-06-19 14:12:13 -0400422 fTarget->asRenderTargetProxy(),
Brian Salomon588cec72018-11-14 13:56:37 -0500423 chain.appliedClip(),
Greg Daniel2c3398d2019-06-19 11:58:01 -0400424 fTarget.get()->asRenderTargetProxy()->outputSwizzle(),
Brian Salomon588cec72018-11-14 13:56:37 -0500425 chain.dstProxy()
Robert Phillips318c4192017-05-17 09:36:38 -0400426 };
Brian Salomon29b60c92017-10-31 14:42:10 -0400427 flushState->setOpArgs(&opArgs);
Brian Salomon588cec72018-11-14 13:56:37 -0500428 chain.head()->prepare(flushState);
Brian Salomon29b60c92017-10-31 14:42:10 -0400429 flushState->setOpArgs(nullptr);
bsalomonaecc0182016-03-07 11:50:44 -0800430 }
bsalomon512be532015-09-10 10:42:55 -0700431 }
robertphillipsa13e2022015-11-11 12:01:09 -0800432}
bsalomon512be532015-09-10 10:42:55 -0700433
Michael Ludwig6e17f1d2019-05-15 14:00:20 +0000434static GrGpuRTCommandBuffer* create_command_buffer(GrGpu* gpu,
435 GrRenderTarget* rt,
436 GrSurfaceOrigin origin,
437 const SkRect& bounds,
438 GrLoadOp colorLoadOp,
439 const SkPMColor4f& loadClearColor,
440 GrLoadOp stencilLoadOp) {
Robert Phillipscb2e2352017-08-30 16:44:40 -0400441 const GrGpuRTCommandBuffer::LoadAndStoreInfo kColorLoadStoreInfo {
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400442 colorLoadOp,
443 GrStoreOp::kStore,
444 loadClearColor
Robert Phillips178ce3e2017-04-13 09:15:47 -0400445 };
446
Robert Phillips95214472017-08-08 18:00:03 -0400447 // TODO:
448 // We would like to (at this level) only ever clear & discard. We would need
449 // to stop splitting up higher level opLists for copyOps to achieve that.
450 // Note: we would still need SB loads and stores but they would happen at a
451 // lower level (inside the VK command buffer).
Greg Daniel500d58b2017-08-24 15:59:33 -0400452 const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo stencilLoadAndStoreInfo {
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400453 stencilLoadOp,
Michael Ludwig6e17f1d2019-05-15 14:00:20 +0000454 GrStoreOp::kStore,
Robert Phillips95214472017-08-08 18:00:03 -0400455 };
456
Ethan Nicholas56d19a52018-10-15 11:26:20 -0400457 return gpu->getCommandBuffer(rt, origin, bounds, kColorLoadStoreInfo, stencilLoadAndStoreInfo);
Robert Phillips178ce3e2017-04-13 09:15:47 -0400458}
459
Brian Salomon25a88092016-12-01 09:36:50 -0500460// TODO: this is where GrOp::renderTarget is used (which is fine since it
Robert Phillips294870f2016-11-11 12:38:40 -0500461// is at flush time). However, we need to store the RenderTargetProxy in the
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500462// Ops and instantiate them here.
Brian Osman407b3422017-08-22 15:01:32 -0400463bool GrRenderTargetOpList::onExecute(GrOpFlushState* flushState) {
Greg Danieldbdba602018-04-20 11:52:43 -0400464 // TODO: Forcing the execution of the discard here isn't ideal since it will cause us to do a
465 // discard and then store the data back in memory so that the load op on future draws doesn't
466 // think the memory is unitialized. Ideally we would want a system where we are tracking whether
467 // the proxy itself has valid data or not, and then use that as a signal on whether we should be
468 // loading or discarding. In that world we wouldni;t need to worry about executing oplists with
469 // no ops just to do a discard.
Brian Salomon588cec72018-11-14 13:56:37 -0500470 if (fOpChains.empty() && GrLoadOp::kClear != fColorLoadOp &&
Greg Danieldbdba602018-04-20 11:52:43 -0400471 GrLoadOp::kDiscard != fColorLoadOp) {
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600472 // TEMPORARY: We are in the process of moving GrMipMapsStatus from the texture to the proxy.
473 // During this time we want to assert that the proxy resolves mipmaps at the exact same
474 // times the old code would have. A null opList is very exceptional, and the proxy will have
475 // assumed mipmaps are dirty in this scenario. We mark them dirty here on the texture as
476 // well, in order to keep the assert passing.
477 GrTexture* tex = fTarget->peekTexture();
478 if (tex && GrMipMapped::kYes == tex->texturePriv().mipMapped()) {
479 tex->texturePriv().markMipMapsDirty();
480 }
bsalomondc438982016-08-31 11:53:49 -0700481 return false;
egdanielb4021cf2016-07-28 08:53:07 -0700482 }
Robert Phillips4a395042017-04-24 16:27:17 +0000483
Robert Phillipsb5204762019-06-19 14:12:13 -0400484 SkASSERT(fTarget->peekRenderTarget());
Brian Salomon5f394272019-07-02 14:07:49 -0400485 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400486
Michael Ludwig6e17f1d2019-05-15 14:00:20 +0000487 // TODO: at the very least, we want the stencil store op to always be discard (at this
488 // level). In Vulkan, sub-command buffers would still need to load & store the stencil buffer.
489
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500490 // Make sure load ops are not kClear if the GPU needs to use draws for clears
491 SkASSERT(fColorLoadOp != GrLoadOp::kClear ||
492 !flushState->gpu()->caps()->performColorClearsAsDraws());
493 SkASSERT(fStencilLoadOp != GrLoadOp::kClear ||
494 !flushState->gpu()->caps()->performStencilClearsAsDraws());
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400495 GrGpuRTCommandBuffer* commandBuffer = create_command_buffer(
Michael Ludwig6e17f1d2019-05-15 14:00:20 +0000496 flushState->gpu(),
Robert Phillipsb5204762019-06-19 14:12:13 -0400497 fTarget->peekRenderTarget(),
498 fTarget->origin(),
499 fTarget->getBoundsRect(),
Michael Ludwig6e17f1d2019-05-15 14:00:20 +0000500 fColorLoadOp,
501 fLoadClearColor,
502 fStencilLoadOp);
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400503 flushState->setCommandBuffer(commandBuffer);
Robert Phillips95214472017-08-08 18:00:03 -0400504 commandBuffer->begin();
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400505
506 // Draw all the generated geometry.
Brian Salomon588cec72018-11-14 13:56:37 -0500507 for (const auto& chain : fOpChains) {
508 if (!chain.head()) {
bsalomonaecc0182016-03-07 11:50:44 -0800509 continue;
510 }
Stan Iliev2af578d2017-08-16 13:00:28 -0400511#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
Brian Salomon5f394272019-07-02 14:07:49 -0400512 TRACE_EVENT0("skia.gpu", chain.head()->name());
Stan Iliev2af578d2017-08-16 13:00:28 -0400513#endif
Robert Phillips178ce3e2017-04-13 09:15:47 -0400514
Brian Salomon29b60c92017-10-31 14:42:10 -0400515 GrOpFlushState::OpArgs opArgs {
Brian Salomon588cec72018-11-14 13:56:37 -0500516 chain.head(),
Robert Phillipsb5204762019-06-19 14:12:13 -0400517 fTarget->asRenderTargetProxy(),
Brian Salomon588cec72018-11-14 13:56:37 -0500518 chain.appliedClip(),
Greg Daniel2c3398d2019-06-19 11:58:01 -0400519 fTarget.get()->asRenderTargetProxy()->outputSwizzle(),
Brian Salomon588cec72018-11-14 13:56:37 -0500520 chain.dstProxy()
Robert Phillips178ce3e2017-04-13 09:15:47 -0400521 };
522
Brian Salomon29b60c92017-10-31 14:42:10 -0400523 flushState->setOpArgs(&opArgs);
Brian Salomon588cec72018-11-14 13:56:37 -0500524 chain.head()->execute(flushState, chain.bounds());
Brian Salomon29b60c92017-10-31 14:42:10 -0400525 flushState->setOpArgs(nullptr);
bsalomon512be532015-09-10 10:42:55 -0700526 }
Robert Phillips178ce3e2017-04-13 09:15:47 -0400527
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400528 commandBuffer->end();
529 flushState->gpu()->submit(commandBuffer);
Robert Phillips178ce3e2017-04-13 09:15:47 -0400530 flushState->setCommandBuffer(nullptr);
ethannicholas22793252016-01-30 09:59:10 -0800531
bsalomondc438982016-08-31 11:53:49 -0700532 return true;
bsalomona73239a2015-04-28 13:35:17 -0700533}
534
Chris Daltona84cacf2017-10-04 10:30:29 -0600535void GrRenderTargetOpList::endFlush() {
Brian Salomonc3833b42018-07-09 18:23:58 +0000536 fLastClipStackGenID = SK_InvalidUniqueID;
Robert Phillipsc994a932018-06-19 13:09:54 -0400537 this->deleteOps();
Chris Daltonc82dd4e2017-11-20 18:20:28 -0700538 fClipAllocator.reset();
Chris Daltona84cacf2017-10-04 10:30:29 -0600539 INHERITED::endFlush();
bsalomon512be532015-09-10 10:42:55 -0700540}
541
Robert Phillips380b90c2017-08-30 07:41:07 -0400542void GrRenderTargetOpList::discard() {
543 // Discard calls to in-progress opLists are ignored. Calls at the start update the
544 // opLists' color & stencil load ops.
545 if (this->isEmpty()) {
546 fColorLoadOp = GrLoadOp::kDiscard;
547 fStencilLoadOp = GrLoadOp::kDiscard;
548 }
549}
550
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500551void GrRenderTargetOpList::setColorLoadOp(GrLoadOp op, const SkPMColor4f& color) {
552 fColorLoadOp = op;
553 fLoadClearColor = color;
554}
555
Chris Dalton6b982802019-06-27 13:53:46 -0600556bool GrRenderTargetOpList::resetForFullscreenClear(CanDiscardPreviousOps canDiscardPreviousOps) {
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500557 // Mark the color load op as discard (this may be followed by a clearColorOnLoad call to make
558 // the load op kClear, or it may be followed by an explicit op). In the event of an absClear()
559 // after a regular clear(), we could end up with a clear load op and a real clear op in the list
560 // if the load op were not reset here.
561 fColorLoadOp = GrLoadOp::kDiscard;
562
Chris Dalton6b982802019-06-27 13:53:46 -0600563 // If we previously recorded a wait op, we cannot delete the wait op. Until we track the wait
564 // ops separately from normal ops, we have to avoid clearing out any ops in this case as well.
565 if (fHasWaitOp) {
566 canDiscardPreviousOps = CanDiscardPreviousOps::kNo;
567 }
568
569 if (CanDiscardPreviousOps::kYes == canDiscardPreviousOps || this->isEmpty()) {
Robert Phillipsc994a932018-06-19 13:09:54 -0400570 this->deleteOps();
Brian Osman099fa0f2017-10-02 16:38:32 -0400571 fDeferredProxies.reset();
Greg Daniel070cbaf2019-01-03 17:35:54 -0500572
573 // If the opList is using a render target which wraps a vulkan command buffer, we can't do a
574 // clear load since we cannot change the render pass that we are using. Thus we fall back to
575 // making a clear op in this case.
Robert Phillipsb5204762019-06-19 14:12:13 -0400576 return !fTarget->asRenderTargetProxy()->wrapsVkSecondaryCB();
bsalomonfd8d0132016-08-11 11:25:33 -0700577 }
Robert Phillips380b90c2017-08-30 07:41:07 -0400578
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500579 // Could not empty the list, so an op must be added to handle the clear
580 return false;
bsalomon9f129de2016-08-10 16:31:05 -0700581}
582
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000583////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000584
Robert Phillips81dd3e02017-06-23 11:59:24 -0400585// This closely parallels GrTextureOpList::copySurface but renderTargetOpLists
586// also store the applied clip and dest proxy with the op
Robert Phillipsbe9aff22019-02-15 11:33:22 -0500587bool GrRenderTargetOpList::copySurface(GrRecordingContext* context,
Robert Phillipsbf25d432017-04-07 10:08:53 -0400588 GrSurfaceProxy* src,
Robert Phillipsf2361d22016-10-25 14:20:06 -0400589 const SkIRect& srcRect,
590 const SkIPoint& dstPoint) {
Chris Daltonf8e5aad2019-08-02 12:55:23 -0600591 std::unique_ptr<GrOp> op = GrCopySurfaceOp::Make(
592 context, fTarget.get(), src, srcRect, dstPoint);
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500593 if (!op) {
bsalomonb8fea972016-02-16 07:34:17 -0800594 return false;
595 }
robertphillips498d7ac2015-10-30 10:11:30 -0700596
Chris Dalton08755122019-08-05 16:13:47 -0600597 this->addOp(std::move(op), GrTextureResolveManager(context->priv().drawingManager()),
598 *context->priv().caps());
bsalomonb8fea972016-02-16 07:34:17 -0800599 return true;
bsalomon@google.comeb851172013-04-15 13:51:00 +0000600}
601
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400602void GrRenderTargetOpList::transferFrom(GrRecordingContext* context,
603 const SkIRect& srcRect,
Brian Salomonf77c1462019-08-01 15:19:29 -0400604 GrColorType surfaceColorType,
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400605 GrColorType dstColorType,
606 sk_sp<GrGpuBuffer> dst,
607 size_t dstOffset) {
Brian Salomonf77c1462019-08-01 15:19:29 -0400608 auto op = GrTransferFromOp::Make(context, srcRect, surfaceColorType, dstColorType,
609 std::move(dst), dstOffset);
Chris Dalton08755122019-08-05 16:13:47 -0600610 this->addOp(std::move(op), GrTextureResolveManager(context->priv().drawingManager()),
611 *context->priv().caps());
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400612}
613
Chris Dalton6b498102019-08-01 14:14:52 -0600614void GrRenderTargetOpList::handleInternalAllocationFailure() {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500615 bool hasUninstantiatedProxy = false;
Chris Dalton7eb5c0f2019-05-23 15:15:47 -0600616 auto checkInstantiation = [&hasUninstantiatedProxy](GrSurfaceProxy* p, GrMipMapped) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400617 if (!p->isInstantiated()) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500618 hasUninstantiatedProxy = true;
619 }
620 };
Brian Salomon588cec72018-11-14 13:56:37 -0500621 for (OpChain& recordedOp : fOpChains) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500622 hasUninstantiatedProxy = false;
Chris Dalton1706cbf2019-05-21 19:35:29 -0600623 recordedOp.visitProxies(checkInstantiation);
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500624 if (hasUninstantiatedProxy) {
625 // When instantiation of the proxy fails we drop the Op
Brian Salomon588cec72018-11-14 13:56:37 -0500626 recordedOp.deleteOps(fOpMemoryPool.get());
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500627 }
628 }
629}
630
Robert Phillips9313aa72019-04-09 18:41:27 -0400631bool GrRenderTargetOpList::onIsUsed(GrSurfaceProxy* proxyToCheck) const {
632 bool used = false;
633
Chris Dalton7eb5c0f2019-05-23 15:15:47 -0600634 auto visit = [ proxyToCheck, &used ] (GrSurfaceProxy* p, GrMipMapped) {
Robert Phillips9313aa72019-04-09 18:41:27 -0400635 if (p == proxyToCheck) {
636 used = true;
637 }
638 };
639 for (const OpChain& recordedOp : fOpChains) {
Chris Dalton1706cbf2019-05-21 19:35:29 -0600640 recordedOp.visitProxies(visit);
Robert Phillips9313aa72019-04-09 18:41:27 -0400641 }
642
643 return used;
644}
645
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400646void GrRenderTargetOpList::gatherProxyIntervals(GrResourceAllocator* alloc) const {
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400647
Robert Phillips51b20f22017-12-01 15:32:35 -0500648 for (int i = 0; i < fDeferredProxies.count(); ++i) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400649 SkASSERT(!fDeferredProxies[i]->isInstantiated());
Robert Phillips51b20f22017-12-01 15:32:35 -0500650 // We give all the deferred proxies a write usage at the very start of flushing. This
651 // locks them out of being reused for the entire flush until they are read - and then
652 // they can be recycled. This is a bit unfortunate because a flush can proceed in waves
653 // with sub-flushes. The deferred proxies only need to be pinned from the start of
654 // the sub-flush in which they appear.
Robert Phillipsc73666f2019-04-24 08:49:48 -0400655 alloc->addInterval(fDeferredProxies[i], 0, 0, GrResourceAllocator::ActualUse::kNo);
Robert Phillips51b20f22017-12-01 15:32:35 -0500656 }
657
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400658 // Add the interval for all the writes to this opList's target
Brian Salomon588cec72018-11-14 13:56:37 -0500659 if (fOpChains.count()) {
Robert Phillips3bf3d4a2019-03-27 07:09:09 -0400660 unsigned int cur = alloc->curOp();
661
Robert Phillipsc73666f2019-04-24 08:49:48 -0400662 alloc->addInterval(fTarget.get(), cur, cur + fOpChains.count() - 1,
663 GrResourceAllocator::ActualUse::kYes);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500664 } else {
665 // This can happen if there is a loadOp (e.g., a clear) but no other draws. In this case we
666 // still need to add an interval for the destination so we create a fake op# for
667 // the missing clear op.
Robert Phillipsc73666f2019-04-24 08:49:48 -0400668 alloc->addInterval(fTarget.get(), alloc->curOp(), alloc->curOp(),
669 GrResourceAllocator::ActualUse::kYes);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500670 alloc->incOps();
671 }
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400672
Chris Dalton7eb5c0f2019-05-23 15:15:47 -0600673 auto gather = [ alloc SkDEBUGCODE(, this) ] (GrSurfaceProxy* p, GrMipMapped) {
Robert Phillipsc73666f2019-04-24 08:49:48 -0400674 alloc->addInterval(p, alloc->curOp(), alloc->curOp(), GrResourceAllocator::ActualUse::kYes
675 SkDEBUGCODE(, fTarget.get() == p));
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400676 };
Brian Salomon588cec72018-11-14 13:56:37 -0500677 for (const OpChain& recordedOp : fOpChains) {
Brian Salomon7d94bb52018-10-12 14:37:19 -0400678 // only diff from the GrTextureOpList version
Chris Dalton1706cbf2019-05-21 19:35:29 -0600679 recordedOp.visitProxies(gather);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500680
Robert Phillips3bf3d4a2019-03-27 07:09:09 -0400681 // 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 -0500682 // keep all the math consistent.
683 alloc->incOps();
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400684 }
685}
686
Chris Dalton945ee652019-01-23 09:10:36 -0700687void GrRenderTargetOpList::recordOp(
688 std::unique_ptr<GrOp> op, GrProcessorSet::Analysis processorAnalysis, GrAppliedClip* clip,
689 const DstProxy* dstProxy, const GrCaps& caps) {
Ethan Nicholas029b22c2018-10-18 16:49:56 -0400690 SkDEBUGCODE(op->validate();)
Chris Dalton945ee652019-01-23 09:10:36 -0700691 SkASSERT(processorAnalysis.requiresDstTexture() == (dstProxy && dstProxy->proxy()));
Robert Phillipsb5204762019-06-19 14:12:13 -0400692 SkASSERT(fTarget);
Robert Phillipsee683652017-04-26 11:53:10 -0400693
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500694 // A closed GrOpList should never receive new/more ops
robertphillips6a186652015-10-20 07:37:58 -0700695 SkASSERT(!this->isClosed());
Brian Salomon19ec80f2018-11-16 13:27:30 -0500696 if (!op->bounds().isFinite()) {
697 fOpMemoryPool->release(std::move(op));
698 return;
699 }
robertphillipsa106c622015-10-16 09:07:06 -0700700
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500701 // Check if there is an op we can combine with by linearly searching back until we either
702 // 1) check every op
bsalomon512be532015-09-10 10:42:55 -0700703 // 2) intersect with something
704 // 3) find a 'blocker'
Robert Phillipsb5204762019-06-19 14:12:13 -0400705 GR_AUDIT_TRAIL_ADD_OP(fAuditTrail, op.get(), fTarget->uniqueID());
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400706 GrOP_INFO("opList: %d Recording (%s, opID: %u)\n"
707 "\tBounds [L: %.2f, T: %.2f R: %.2f B: %.2f]\n",
708 this->uniqueID(),
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500709 op->name(),
710 op->uniqueID(),
Robert Phillips1119dc32017-04-11 12:54:57 -0400711 op->bounds().fLeft, op->bounds().fTop,
712 op->bounds().fRight, op->bounds().fBottom);
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500713 GrOP_INFO(SkTabString(op->dumpInfo(), 1).c_str());
Brian Salomon25a88092016-12-01 09:36:50 -0500714 GrOP_INFO("\tOutcome:\n");
Brian Salomon588cec72018-11-14 13:56:37 -0500715 int maxCandidates = SkTMin(kMaxOpChainDistance, fOpChains.count());
Robert Phillips318c4192017-05-17 09:36:38 -0400716 if (maxCandidates) {
bsalomon512be532015-09-10 10:42:55 -0700717 int i = 0;
718 while (true) {
Brian Salomon588cec72018-11-14 13:56:37 -0500719 OpChain& candidate = fOpChains.fromBack(i);
Chris Dalton945ee652019-01-23 09:10:36 -0700720 op = candidate.appendOp(std::move(op), processorAnalysis, dstProxy, clip, caps,
721 fOpMemoryPool.get(), fAuditTrail);
Brian Salomon588cec72018-11-14 13:56:37 -0500722 if (!op) {
723 return;
bsalomon512be532015-09-10 10:42:55 -0700724 }
Brian Salomona7682c82018-10-24 10:04:37 -0400725 // Stop going backwards if we would cause a painter's order violation.
Brian Salomon588cec72018-11-14 13:56:37 -0500726 if (!can_reorder(candidate.bounds(), op->bounds())) {
727 GrOP_INFO("\t\tBackward: Intersects with chain (%s, head opID: %u)\n",
728 candidate.head()->name(), candidate.head()->uniqueID());
bsalomon512be532015-09-10 10:42:55 -0700729 break;
730 }
Brian Salomon588cec72018-11-14 13:56:37 -0500731 if (++i == maxCandidates) {
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400732 GrOP_INFO("\t\tBackward: Reached max lookback or beginning of op array %d\n", i);
bsalomon512be532015-09-10 10:42:55 -0700733 break;
734 }
735 }
736 } else {
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400737 GrOP_INFO("\t\tBackward: FirstOp\n");
bsalomon512be532015-09-10 10:42:55 -0700738 }
Brian Salomon54d212e2017-03-21 14:22:38 -0400739 if (clip) {
740 clip = fClipAllocator.make<GrAppliedClip>(std::move(*clip));
Robert Phillipsc84c0302017-05-08 15:35:11 -0400741 SkDEBUGCODE(fNumClips++;)
Brian Salomon54d212e2017-03-21 14:22:38 -0400742 }
Chris Dalton945ee652019-01-23 09:10:36 -0700743 fOpChains.emplace_back(std::move(op), processorAnalysis, clip, dstProxy);
bsalomon512be532015-09-10 10:42:55 -0700744}
745
Robert Phillipsee683652017-04-26 11:53:10 -0400746void GrRenderTargetOpList::forwardCombine(const GrCaps& caps) {
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400747 SkASSERT(!this->isClosed());
Brian Salomon588cec72018-11-14 13:56:37 -0500748 GrOP_INFO("opList: %d ForwardCombine %d ops:\n", this->uniqueID(), fOpChains.count());
Robert Phillips48567ac2017-06-01 08:46:00 -0400749
Brian Salomon588cec72018-11-14 13:56:37 -0500750 for (int i = 0; i < fOpChains.count() - 1; ++i) {
751 OpChain& chain = fOpChains[i];
752 int maxCandidateIdx = SkTMin(i + kMaxOpChainDistance, fOpChains.count() - 1);
bsalomonaecc0182016-03-07 11:50:44 -0800753 int j = i + 1;
754 while (true) {
Brian Salomon588cec72018-11-14 13:56:37 -0500755 OpChain& candidate = fOpChains[j];
756 if (candidate.prependChain(&chain, caps, fOpMemoryPool.get(), fAuditTrail)) {
bsalomonaecc0182016-03-07 11:50:44 -0800757 break;
758 }
Robert Phillipsc84c0302017-05-08 15:35:11 -0400759 // Stop traversing if we would cause a painter's order violation.
Brian Salomon588cec72018-11-14 13:56:37 -0500760 if (!can_reorder(chain.bounds(), candidate.bounds())) {
761 GrOP_INFO(
762 "\t\t%d: chain (%s head opID: %u) -> "
763 "Intersects with chain (%s, head opID: %u)\n",
764 i, chain.head()->name(), chain.head()->uniqueID(), candidate.head()->name(),
765 candidate.head()->uniqueID());
bsalomonaecc0182016-03-07 11:50:44 -0800766 break;
767 }
Brian Salomona7682c82018-10-24 10:04:37 -0400768 if (++j > maxCandidateIdx) {
Brian Salomon588cec72018-11-14 13:56:37 -0500769 GrOP_INFO("\t\t%d: chain (%s opID: %u) -> Reached max lookahead or end of array\n",
770 i, chain.head()->name(), chain.head()->uniqueID());
bsalomonaecc0182016-03-07 11:50:44 -0800771 break;
772 }
773 }
774 }
775}
776