blob: 7823cdb12eb3423a772207b34809d154d7110362 [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
Robert Phillipsf2361d22016-10-25 14:20:06 -04008#include "GrRenderTargetOpList.h"
joshualitt086cee12016-01-12 06:45:24 -08009#include "GrAuditTrail.h"
bsalomoneb1cb5c2015-05-22 08:01:09 -070010#include "GrCaps.h"
bsalomon4061b122015-05-29 10:26:19 -070011#include "GrGpu.h"
egdaniel9cb63402016-06-23 08:37:05 -070012#include "GrGpuCommandBuffer.h"
Robert Phillips7c525e62018-06-12 10:11:12 -040013#include "GrMemoryPool.h"
Brian Salomona4677b52017-05-04 12:39:56 -040014#include "GrRect.h"
Brian Salomon467921e2017-03-06 16:17:12 -050015#include "GrRenderTargetContext.h"
Robert Phillipsd375dbf2017-09-14 12:45:25 -040016#include "GrResourceAllocator.h"
Chris Daltonee21e6b2019-01-22 14:04:43 -070017#include "SkExchange.h"
Brian Salomon588cec72018-11-14 13:56:37 -050018#include "SkRectPriv.h"
19#include "SkTraceEvent.h"
Brian Salomon89527432016-12-16 09:52:16 -050020#include "ops/GrClearOp.h"
Brian Salomon89527432016-12-16 09:52:16 -050021#include "ops/GrCopySurfaceOp.h"
Robert Phillipsf2361d22016-10-25 14:20:06 -040022
reed@google.comac10a2d2010-12-22 21:39:39 +000023////////////////////////////////////////////////////////////////////////////////
24
Brian Salomon09d994e2016-12-21 11:14:46 -050025// Experimentally we have found that most combining occurs within the first 10 comparisons.
Brian Salomon588cec72018-11-14 13:56:37 -050026static const int kMaxOpMergeDistance = 10;
27static const int kMaxOpChainDistance = 10;
28
29////////////////////////////////////////////////////////////////////////////////
30
31using DstProxy = GrXferProcessor::DstProxy;
32
33////////////////////////////////////////////////////////////////////////////////
34
35static inline bool can_reorder(const SkRect& a, const SkRect& b) { return !GrRectsOverlap(a, b); }
36
37////////////////////////////////////////////////////////////////////////////////
38
39inline GrRenderTargetOpList::OpChain::List::List(std::unique_ptr<GrOp> op)
40 : fHead(std::move(op)), fTail(fHead.get()) {
41 this->validate();
42}
43
44inline GrRenderTargetOpList::OpChain::List::List(List&& that) { *this = std::move(that); }
45
46inline GrRenderTargetOpList::OpChain::List& GrRenderTargetOpList::OpChain::List::operator=(
47 List&& that) {
48 fHead = std::move(that.fHead);
49 fTail = that.fTail;
50 that.fTail = nullptr;
51 this->validate();
52 return *this;
53}
54
55inline std::unique_ptr<GrOp> GrRenderTargetOpList::OpChain::List::popHead() {
56 SkASSERT(fHead);
57 auto temp = fHead->cutChain();
58 std::swap(temp, fHead);
59 if (!fHead) {
60 SkASSERT(fTail == temp.get());
61 fTail = nullptr;
62 }
63 return temp;
64}
65
66inline std::unique_ptr<GrOp> GrRenderTargetOpList::OpChain::List::removeOp(GrOp* op) {
67#ifdef SK_DEBUG
68 auto head = op;
69 while (head->prevInChain()) { head = head->prevInChain(); }
70 SkASSERT(head == fHead.get());
71#endif
72 auto prev = op->prevInChain();
73 if (!prev) {
74 SkASSERT(op == fHead.get());
75 return this->popHead();
76 }
77 auto temp = prev->cutChain();
78 if (auto next = temp->cutChain()) {
79 prev->chainConcat(std::move(next));
80 } else {
81 SkASSERT(fTail == op);
82 fTail = prev;
83 }
84 this->validate();
85 return temp;
86}
87
88inline void GrRenderTargetOpList::OpChain::List::pushHead(std::unique_ptr<GrOp> op) {
89 SkASSERT(op);
90 SkASSERT(op->isChainHead());
91 SkASSERT(op->isChainTail());
92 if (fHead) {
93 op->chainConcat(std::move(fHead));
94 fHead = std::move(op);
95 } else {
96 fHead = std::move(op);
97 fTail = fHead.get();
98 }
99}
100
101inline void GrRenderTargetOpList::OpChain::List::pushTail(std::unique_ptr<GrOp> op) {
102 SkASSERT(op->isChainTail());
103 fTail->chainConcat(std::move(op));
104 fTail = fTail->nextInChain();
105}
106
107inline void GrRenderTargetOpList::OpChain::List::validate() const {
108#ifdef SK_DEBUG
109 if (fHead) {
110 SkASSERT(fTail);
111 fHead->validateChain(fTail);
112 }
113#endif
114}
115
116////////////////////////////////////////////////////////////////////////////////
117
118GrRenderTargetOpList::OpChain::OpChain(std::unique_ptr<GrOp> op, GrAppliedClip* appliedClip,
119 const DstProxy* dstProxy)
120 : fList{std::move(op)}, fAppliedClip(appliedClip) {
121 if (dstProxy) {
122 fDstProxy = *dstProxy;
123 }
124 fBounds = fList.head()->bounds();
125}
126
127void GrRenderTargetOpList::OpChain::visitProxies(const GrOp::VisitProxyFunc& func,
128 GrOp::VisitorType visitor) const {
129 if (fList.empty()) {
130 return;
131 }
132 for (const auto& op : GrOp::ChainRange<>(fList.head())) {
133 op.visitProxies(func, visitor);
134 }
135 if (fDstProxy.proxy()) {
136 func(fDstProxy.proxy());
137 }
138 if (fAppliedClip) {
139 fAppliedClip->visitProxies(func);
140 }
141}
142
143void GrRenderTargetOpList::OpChain::deleteOps(GrOpMemoryPool* pool) {
144 while (!fList.empty()) {
145 pool->release(fList.popHead());
146 }
147}
148
149// Concatenates two op chains and attempts to merge ops across the chains. Assumes that we know that
150// the two chains are chainable. Returns the new chain.
151GrRenderTargetOpList::OpChain::List GrRenderTargetOpList::OpChain::DoConcat(
152 List chainA, List chainB, const GrCaps& caps, GrOpMemoryPool* pool,
153 GrAuditTrail* auditTrail) {
154 // We process ops in chain b from head to tail. We attempt to merge with nodes in a, starting
155 // at chain a's tail and working toward the head. We produce one of the following outcomes:
156 // 1) b's head is merged into an op in a.
157 // 2) An op from chain a is merged into b's head. (In this case b's head gets processed again.)
158 // 3) b's head is popped from chain a and added at the tail of a.
159 // After result 3 we don't want to attempt to merge the next head of b with the new tail of a,
160 // as we assume merges were already attempted when chain b was created. So we keep track of the
161 // original tail of a and start our iteration of a there. We also track the bounds of the nodes
162 // appended to chain a that will be skipped for bounds testing. If the original tail of a is
163 // merged into an op in b (case 2) then we advance the "original tail" towards the head of a.
164 GrOp* origATail = chainA.tail();
165 SkRect skipBounds = SkRectPriv::MakeLargestInverted();
166 do {
167 int numMergeChecks = 0;
168 bool merged = false;
169 bool noSkip = (origATail == chainA.tail());
170 SkASSERT(noSkip == (skipBounds == SkRectPriv::MakeLargestInverted()));
171 bool canBackwardMerge = noSkip || can_reorder(chainB.head()->bounds(), skipBounds);
172 SkRect forwardMergeBounds = skipBounds;
173 GrOp* a = origATail;
174 while (a) {
175 bool canForwardMerge =
176 (a == chainA.tail()) || can_reorder(a->bounds(), forwardMergeBounds);
177 if (canForwardMerge || canBackwardMerge) {
178 auto result = a->combineIfPossible(chainB.head(), caps);
179 SkASSERT(result != GrOp::CombineResult::kCannotCombine);
180 merged = (result == GrOp::CombineResult::kMerged);
Robert Phillips9548c3b422019-01-08 12:35:43 -0500181 GrOP_INFO("\t\t: (%s opID: %u) -> Combining with (%s, opID: %u)\n",
Brian Salomon588cec72018-11-14 13:56:37 -0500182 chainB.head()->name(), chainB.head()->uniqueID(), a->name(),
183 a->uniqueID());
Brian Salomon588cec72018-11-14 13:56:37 -0500184 }
185 if (merged) {
Brian Salomon52a6ed32018-11-26 10:30:58 -0500186 GR_AUDIT_TRAIL_OPS_RESULT_COMBINED(auditTrail, a, chainB.head());
Brian Salomon588cec72018-11-14 13:56:37 -0500187 if (canBackwardMerge) {
188 pool->release(chainB.popHead());
189 } else {
190 // We merged the contents of b's head into a. We will replace b's head with a in
191 // chain b.
192 SkASSERT(canForwardMerge);
193 if (a == origATail) {
194 origATail = a->prevInChain();
195 }
196 std::unique_ptr<GrOp> detachedA = chainA.removeOp(a);
197 pool->release(chainB.popHead());
198 chainB.pushHead(std::move(detachedA));
199 if (chainA.empty()) {
200 // We merged all the nodes in chain a to chain b.
201 return chainB;
202 }
203 }
204 break;
205 } else {
206 if (++numMergeChecks == kMaxOpMergeDistance) {
207 break;
208 }
209 forwardMergeBounds.joinNonEmptyArg(a->bounds());
210 canBackwardMerge =
211 canBackwardMerge && can_reorder(chainB.head()->bounds(), a->bounds());
212 a = a->prevInChain();
213 }
214 }
215 // If we weren't able to merge b's head then pop b's head from chain b and make it the new
216 // tail of a.
217 if (!merged) {
218 chainA.pushTail(chainB.popHead());
219 skipBounds.joinNonEmptyArg(chainA.tail()->bounds());
220 }
221 } while (!chainB.empty());
222 return chainA;
223}
224
225// Attempts to concatenate two chains and merge ops across the chains. Upon failure the original
226// chain heads and tails are returned. Upon success the new chain's head and tail are returned
227// (and null for the second head/tail).
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700228bool GrRenderTargetOpList::OpChain::tryConcat(
229 List* list, const DstProxy& dstProxy, const GrAppliedClip* appliedClip,
Chris Daltonee21e6b2019-01-22 14:04:43 -0700230 const SkRect& bounds, const GrCaps& caps, GrOpMemoryPool* pool, GrAuditTrail* auditTrail) {
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700231 SkASSERT(!fList.empty());
232 SkASSERT(!list->empty());
Brian Salomon588cec72018-11-14 13:56:37 -0500233 // All returns use explicit tuple constructor rather than {a, b} to work around old GCC bug.
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700234 if (fList.head()->classID() != list->head()->classID() ||
235 SkToBool(fAppliedClip) != SkToBool(appliedClip) ||
236 (fAppliedClip && *fAppliedClip != *appliedClip) ||
237 SkToBool(fDstProxy.proxy()) != SkToBool(dstProxy.proxy()) ||
238 (fDstProxy.proxy() && fDstProxy != dstProxy)) {
239 return false;
Brian Salomon588cec72018-11-14 13:56:37 -0500240 }
Chris Daltonee21e6b2019-01-22 14:04:43 -0700241
Brian Salomon588cec72018-11-14 13:56:37 -0500242 SkDEBUGCODE(bool first = true;)
243 do {
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700244 switch (fList.tail()->combineIfPossible(list->head(), caps)) {
Brian Salomon588cec72018-11-14 13:56:37 -0500245 case GrOp::CombineResult::kCannotCombine:
246 // If an op supports chaining then it is required that chaining is transitive and
247 // that if any two ops in two different chains can merge then the two chains
248 // may also be chained together. Thus, we should only hit this on the first
249 // iteration.
250 SkASSERT(first);
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700251 return false;
Brian Salomon588cec72018-11-14 13:56:37 -0500252 case GrOp::CombineResult::kMayChain:
Chris Daltonee21e6b2019-01-22 14:04:43 -0700253 fList = DoConcat(std::move(fList), skstd::exchange(*list, List()), caps, pool,
254 auditTrail);
255 // The above exchange cleared out 'list'. The list needs to be empty now for the
256 // loop to terminate.
257 SkASSERT(list->empty());
258 break;
Brian Salomon588cec72018-11-14 13:56:37 -0500259 case GrOp::CombineResult::kMerged: {
Robert Phillips9548c3b422019-01-08 12:35:43 -0500260 GrOP_INFO("\t\t: (%s opID: %u) -> Combining with (%s, opID: %u)\n",
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700261 list->tail()->name(), list->tail()->uniqueID(), list->head()->name(),
262 list->head()->uniqueID());
263 GR_AUDIT_TRAIL_OPS_RESULT_COMBINED(auditTrail, fList.tail(), list->head());
264 pool->release(list->popHead());
Brian Salomon588cec72018-11-14 13:56:37 -0500265 break;
266 }
267 }
268 SkDEBUGCODE(first = false);
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700269 } while (!list->empty());
Chris Daltonee21e6b2019-01-22 14:04:43 -0700270
271 // The new ops were successfully merged and/or chained onto our own.
272 fBounds.joinPossiblyEmptyRect(bounds);
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700273 return true;
Brian Salomon588cec72018-11-14 13:56:37 -0500274}
275
276bool GrRenderTargetOpList::OpChain::prependChain(OpChain* that, const GrCaps& caps,
277 GrOpMemoryPool* pool, GrAuditTrail* auditTrail) {
Chris Daltonee21e6b2019-01-22 14:04:43 -0700278 if (!that->tryConcat(&fList, fDstProxy, fAppliedClip, fBounds, caps, pool, auditTrail)) {
Brian Salomon588cec72018-11-14 13:56:37 -0500279 this->validate();
280 // append failed
281 return false;
282 }
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700283
Brian Salomon588cec72018-11-14 13:56:37 -0500284 // 'that' owns the combined chain. Move it into 'this'.
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700285 SkASSERT(fList.empty());
Brian Salomon588cec72018-11-14 13:56:37 -0500286 fList = std::move(that->fList);
Chris Daltonee21e6b2019-01-22 14:04:43 -0700287 fBounds = that->fBounds;
Brian Salomon588cec72018-11-14 13:56:37 -0500288
289 that->fDstProxy.setProxy(nullptr);
290 if (that->fAppliedClip) {
291 for (int i = 0; i < that->fAppliedClip->numClipCoverageFragmentProcessors(); ++i) {
292 that->fAppliedClip->detachClipCoverageFragmentProcessor(i);
293 }
294 }
295 this->validate();
296 return true;
297}
298
299std::unique_ptr<GrOp> GrRenderTargetOpList::OpChain::appendOp(std::unique_ptr<GrOp> op,
300 const DstProxy* dstProxy,
301 const GrAppliedClip* appliedClip,
302 const GrCaps& caps,
303 GrOpMemoryPool* pool,
304 GrAuditTrail* auditTrail) {
305 const GrXferProcessor::DstProxy noDstProxy;
306 if (!dstProxy) {
307 dstProxy = &noDstProxy;
308 }
309 SkASSERT(op->isChainHead() && op->isChainTail());
310 SkRect opBounds = op->bounds();
311 List chain(std::move(op));
Chris Daltonee21e6b2019-01-22 14:04:43 -0700312 if (!this->tryConcat(&chain, *dstProxy, appliedClip, opBounds, caps, pool, auditTrail)) {
Brian Salomon588cec72018-11-14 13:56:37 -0500313 // append failed, give the op back to the caller.
314 this->validate();
315 return chain.popHead();
316 }
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700317
318 SkASSERT(chain.empty());
Brian Salomon588cec72018-11-14 13:56:37 -0500319 this->validate();
320 return nullptr;
321}
322
323inline void GrRenderTargetOpList::OpChain::validate() const {
324#ifdef SK_DEBUG
325 fList.validate();
326 for (const auto& op : GrOp::ChainRange<>(fList.head())) {
327 // Not using SkRect::contains because we allow empty rects.
328 SkASSERT(fBounds.fLeft <= op.bounds().fLeft && fBounds.fTop <= op.bounds().fTop &&
329 fBounds.fRight >= op.bounds().fRight && fBounds.fBottom >= op.bounds().fBottom);
330 }
331#endif
332}
333
334////////////////////////////////////////////////////////////////////////////////
bsalomon489147c2015-12-14 12:13:09 -0800335
Robert Phillips3a9710b2018-03-27 17:51:55 -0400336GrRenderTargetOpList::GrRenderTargetOpList(GrResourceProvider* resourceProvider,
Robert Phillipsc994a932018-06-19 13:09:54 -0400337 sk_sp<GrOpMemoryPool> opMemoryPool,
Robert Phillips3a9710b2018-03-27 17:51:55 -0400338 GrRenderTargetProxy* proxy,
Robert Phillips8185f592017-04-26 08:31:08 -0400339 GrAuditTrail* auditTrail)
Robert Phillipsc994a932018-06-19 13:09:54 -0400340 : INHERITED(resourceProvider, std::move(opMemoryPool), proxy, auditTrail)
Brian Salomonc3833b42018-07-09 18:23:58 +0000341 , fLastClipStackGenID(SK_InvalidUniqueID)
Robert Phillipsb6deea82017-05-11 14:14:30 -0400342 SkDEBUGCODE(, fNumClips(0)) {
bsalomon4061b122015-05-29 10:26:19 -0700343}
344
Robert Phillipsc994a932018-06-19 13:09:54 -0400345void GrRenderTargetOpList::deleteOps() {
Brian Salomon588cec72018-11-14 13:56:37 -0500346 for (auto& chain : fOpChains) {
347 chain.deleteOps(fOpMemoryPool.get());
Robert Phillipsc994a932018-06-19 13:09:54 -0400348 }
Brian Salomon588cec72018-11-14 13:56:37 -0500349 fOpChains.reset();
Robert Phillipsc994a932018-06-19 13:09:54 -0400350}
351
Robert Phillipsf2361d22016-10-25 14:20:06 -0400352GrRenderTargetOpList::~GrRenderTargetOpList() {
Robert Phillipsc994a932018-06-19 13:09:54 -0400353 this->deleteOps();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000354}
355
356////////////////////////////////////////////////////////////////////////////////
357
robertphillips4beb5c12015-10-20 07:50:00 -0700358#ifdef SK_DEBUG
Robert Phillips27483912018-04-20 12:43:18 -0400359void GrRenderTargetOpList::dump(bool printDependencies) const {
360 INHERITED::dump(printDependencies);
Robert Phillipsf2361d22016-10-25 14:20:06 -0400361
Brian Salomon588cec72018-11-14 13:56:37 -0500362 SkDebugf("ops (%d):\n", fOpChains.count());
363 for (int i = 0; i < fOpChains.count(); ++i) {
robertphillips4beb5c12015-10-20 07:50:00 -0700364 SkDebugf("*******************************\n");
Brian Salomon588cec72018-11-14 13:56:37 -0500365 if (!fOpChains[i].head()) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500366 SkDebugf("%d: <combined forward or failed instantiation>\n", i);
bsalomonaecc0182016-03-07 11:50:44 -0800367 } else {
Brian Salomon588cec72018-11-14 13:56:37 -0500368 SkDebugf("%d: %s\n", i, fOpChains[i].head()->name());
369 SkRect bounds = fOpChains[i].bounds();
Brian Salomon9e50f7b2017-03-06 12:02:34 -0500370 SkDebugf("ClippedBounds: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", bounds.fLeft,
371 bounds.fTop, bounds.fRight, bounds.fBottom);
Brian Salomon588cec72018-11-14 13:56:37 -0500372 for (const auto& op : GrOp::ChainRange<>(fOpChains[i].head())) {
373 SkString info = SkTabString(op.dumpInfo(), 1);
374 SkDebugf("%s\n", info.c_str());
375 bounds = op.bounds();
376 SkDebugf("\tClippedBounds: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", bounds.fLeft,
377 bounds.fTop, bounds.fRight, bounds.fBottom);
378 }
bsalomonaecc0182016-03-07 11:50:44 -0800379 }
robertphillips4beb5c12015-10-20 07:50:00 -0700380 }
381}
Chris Dalton706a6ff2017-11-29 22:01:06 -0700382
383void GrRenderTargetOpList::visitProxies_debugOnly(const GrOp::VisitProxyFunc& func) const {
Brian Salomon588cec72018-11-14 13:56:37 -0500384 for (const OpChain& chain : fOpChains) {
385 chain.visitProxies(func, GrOp::VisitorType::kOther);
Chris Dalton706a6ff2017-11-29 22:01:06 -0700386 }
387}
Brian Salomonc525d4f2018-09-17 15:48:20 -0400388
robertphillips4beb5c12015-10-20 07:50:00 -0700389#endif
390
Brian Osman407b3422017-08-22 15:01:32 -0400391void GrRenderTargetOpList::onPrepare(GrOpFlushState* flushState) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400392 SkASSERT(fTarget.get()->peekRenderTarget());
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400393 SkASSERT(this->isClosed());
Stan Iliev2af578d2017-08-16 13:00:28 -0400394#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
395 TRACE_EVENT0("skia", TRACE_FUNC);
396#endif
robertphillipsa106c622015-10-16 09:07:06 -0700397
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500398 // Loop over the ops that haven't yet been prepared.
Brian Salomon588cec72018-11-14 13:56:37 -0500399 for (const auto& chain : fOpChains) {
400 if (chain.head()) {
Stan Iliev2af578d2017-08-16 13:00:28 -0400401#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
Brian Salomon588cec72018-11-14 13:56:37 -0500402 TRACE_EVENT0("skia", chain.head()->name());
Stan Iliev2af578d2017-08-16 13:00:28 -0400403#endif
Brian Salomon29b60c92017-10-31 14:42:10 -0400404 GrOpFlushState::OpArgs opArgs = {
Brian Salomon588cec72018-11-14 13:56:37 -0500405 chain.head(),
Robert Phillips2890fbf2017-07-26 15:48:41 -0400406 fTarget.get()->asRenderTargetProxy(),
Brian Salomon588cec72018-11-14 13:56:37 -0500407 chain.appliedClip(),
408 chain.dstProxy()
Robert Phillips318c4192017-05-17 09:36:38 -0400409 };
Brian Salomon29b60c92017-10-31 14:42:10 -0400410 flushState->setOpArgs(&opArgs);
Brian Salomon588cec72018-11-14 13:56:37 -0500411 chain.head()->prepare(flushState);
Brian Salomon29b60c92017-10-31 14:42:10 -0400412 flushState->setOpArgs(nullptr);
bsalomonaecc0182016-03-07 11:50:44 -0800413 }
bsalomon512be532015-09-10 10:42:55 -0700414 }
robertphillipsa13e2022015-11-11 12:01:09 -0800415}
bsalomon512be532015-09-10 10:42:55 -0700416
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400417static GrGpuRTCommandBuffer* create_command_buffer(GrGpu* gpu,
418 GrRenderTarget* rt,
419 GrSurfaceOrigin origin,
Ethan Nicholas56d19a52018-10-15 11:26:20 -0400420 const SkRect& bounds,
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400421 GrLoadOp colorLoadOp,
Brian Osman9a9baae2018-11-05 15:06:26 -0500422 const SkPMColor4f& loadClearColor,
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400423 GrLoadOp stencilLoadOp) {
Robert Phillipscb2e2352017-08-30 16:44:40 -0400424 const GrGpuRTCommandBuffer::LoadAndStoreInfo kColorLoadStoreInfo {
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400425 colorLoadOp,
426 GrStoreOp::kStore,
427 loadClearColor
Robert Phillips178ce3e2017-04-13 09:15:47 -0400428 };
429
Robert Phillips95214472017-08-08 18:00:03 -0400430 // TODO:
431 // We would like to (at this level) only ever clear & discard. We would need
432 // to stop splitting up higher level opLists for copyOps to achieve that.
433 // Note: we would still need SB loads and stores but they would happen at a
434 // lower level (inside the VK command buffer).
Greg Daniel500d58b2017-08-24 15:59:33 -0400435 const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo stencilLoadAndStoreInfo {
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400436 stencilLoadOp,
437 GrStoreOp::kStore,
Robert Phillips95214472017-08-08 18:00:03 -0400438 };
439
Ethan Nicholas56d19a52018-10-15 11:26:20 -0400440 return gpu->getCommandBuffer(rt, origin, bounds, kColorLoadStoreInfo, stencilLoadAndStoreInfo);
Robert Phillips178ce3e2017-04-13 09:15:47 -0400441}
442
Brian Salomon25a88092016-12-01 09:36:50 -0500443// TODO: this is where GrOp::renderTarget is used (which is fine since it
Robert Phillips294870f2016-11-11 12:38:40 -0500444// is at flush time). However, we need to store the RenderTargetProxy in the
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500445// Ops and instantiate them here.
Brian Osman407b3422017-08-22 15:01:32 -0400446bool GrRenderTargetOpList::onExecute(GrOpFlushState* flushState) {
Greg Danieldbdba602018-04-20 11:52:43 -0400447 // TODO: Forcing the execution of the discard here isn't ideal since it will cause us to do a
448 // discard and then store the data back in memory so that the load op on future draws doesn't
449 // think the memory is unitialized. Ideally we would want a system where we are tracking whether
450 // the proxy itself has valid data or not, and then use that as a signal on whether we should be
451 // loading or discarding. In that world we wouldni;t need to worry about executing oplists with
452 // no ops just to do a discard.
Brian Salomon588cec72018-11-14 13:56:37 -0500453 if (fOpChains.empty() && GrLoadOp::kClear != fColorLoadOp &&
Greg Danieldbdba602018-04-20 11:52:43 -0400454 GrLoadOp::kDiscard != fColorLoadOp) {
bsalomondc438982016-08-31 11:53:49 -0700455 return false;
egdanielb4021cf2016-07-28 08:53:07 -0700456 }
Robert Phillips4a395042017-04-24 16:27:17 +0000457
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400458 SkASSERT(fTarget.get()->peekRenderTarget());
Stan Iliev2af578d2017-08-16 13:00:28 -0400459 TRACE_EVENT0("skia", TRACE_FUNC);
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400460
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400461 // TODO: at the very least, we want the stencil store op to always be discard (at this
462 // level). In Vulkan, sub-command buffers would still need to load & store the stencil buffer.
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500463
464 // Make sure load ops are not kClear if the GPU needs to use draws for clears
465 SkASSERT(fColorLoadOp != GrLoadOp::kClear ||
466 !flushState->gpu()->caps()->performColorClearsAsDraws());
467 SkASSERT(fStencilLoadOp != GrLoadOp::kClear ||
468 !flushState->gpu()->caps()->performStencilClearsAsDraws());
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400469 GrGpuRTCommandBuffer* commandBuffer = create_command_buffer(
Robert Phillips95214472017-08-08 18:00:03 -0400470 flushState->gpu(),
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400471 fTarget.get()->peekRenderTarget(),
Robert Phillips95214472017-08-08 18:00:03 -0400472 fTarget.get()->origin(),
Ethan Nicholas56d19a52018-10-15 11:26:20 -0400473 fTarget.get()->getBoundsRect(),
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400474 fColorLoadOp,
475 fLoadClearColor,
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400476 fStencilLoadOp);
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400477 flushState->setCommandBuffer(commandBuffer);
Robert Phillips95214472017-08-08 18:00:03 -0400478 commandBuffer->begin();
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400479
480 // Draw all the generated geometry.
Brian Salomon588cec72018-11-14 13:56:37 -0500481 for (const auto& chain : fOpChains) {
482 if (!chain.head()) {
bsalomonaecc0182016-03-07 11:50:44 -0800483 continue;
484 }
Stan Iliev2af578d2017-08-16 13:00:28 -0400485#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
Brian Salomon588cec72018-11-14 13:56:37 -0500486 TRACE_EVENT0("skia", chain.head()->name());
Stan Iliev2af578d2017-08-16 13:00:28 -0400487#endif
Robert Phillips178ce3e2017-04-13 09:15:47 -0400488
Brian Salomon29b60c92017-10-31 14:42:10 -0400489 GrOpFlushState::OpArgs opArgs {
Brian Salomon588cec72018-11-14 13:56:37 -0500490 chain.head(),
Robert Phillips2890fbf2017-07-26 15:48:41 -0400491 fTarget.get()->asRenderTargetProxy(),
Brian Salomon588cec72018-11-14 13:56:37 -0500492 chain.appliedClip(),
493 chain.dstProxy()
Robert Phillips178ce3e2017-04-13 09:15:47 -0400494 };
495
Brian Salomon29b60c92017-10-31 14:42:10 -0400496 flushState->setOpArgs(&opArgs);
Brian Salomon588cec72018-11-14 13:56:37 -0500497 chain.head()->execute(flushState, chain.bounds());
Brian Salomon29b60c92017-10-31 14:42:10 -0400498 flushState->setOpArgs(nullptr);
bsalomon512be532015-09-10 10:42:55 -0700499 }
Robert Phillips178ce3e2017-04-13 09:15:47 -0400500
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400501 commandBuffer->end();
502 flushState->gpu()->submit(commandBuffer);
Robert Phillips178ce3e2017-04-13 09:15:47 -0400503 flushState->setCommandBuffer(nullptr);
ethannicholas22793252016-01-30 09:59:10 -0800504
bsalomondc438982016-08-31 11:53:49 -0700505 return true;
bsalomona73239a2015-04-28 13:35:17 -0700506}
507
Chris Daltona84cacf2017-10-04 10:30:29 -0600508void GrRenderTargetOpList::endFlush() {
Brian Salomonc3833b42018-07-09 18:23:58 +0000509 fLastClipStackGenID = SK_InvalidUniqueID;
Robert Phillipsc994a932018-06-19 13:09:54 -0400510 this->deleteOps();
Chris Daltonc82dd4e2017-11-20 18:20:28 -0700511 fClipAllocator.reset();
Chris Daltona84cacf2017-10-04 10:30:29 -0600512 INHERITED::endFlush();
bsalomon512be532015-09-10 10:42:55 -0700513}
514
Robert Phillips380b90c2017-08-30 07:41:07 -0400515void GrRenderTargetOpList::discard() {
516 // Discard calls to in-progress opLists are ignored. Calls at the start update the
517 // opLists' color & stencil load ops.
518 if (this->isEmpty()) {
519 fColorLoadOp = GrLoadOp::kDiscard;
520 fStencilLoadOp = GrLoadOp::kDiscard;
521 }
522}
523
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500524void GrRenderTargetOpList::setStencilLoadOp(GrLoadOp op) {
525 fStencilLoadOp = op;
526}
Robert Phillips380b90c2017-08-30 07:41:07 -0400527
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500528void GrRenderTargetOpList::setColorLoadOp(GrLoadOp op, const SkPMColor4f& color) {
529 fColorLoadOp = op;
530 fLoadClearColor = color;
531}
532
533bool GrRenderTargetOpList::resetForFullscreenClear() {
534 // Mark the color load op as discard (this may be followed by a clearColorOnLoad call to make
535 // the load op kClear, or it may be followed by an explicit op). In the event of an absClear()
536 // after a regular clear(), we could end up with a clear load op and a real clear op in the list
537 // if the load op were not reset here.
538 fColorLoadOp = GrLoadOp::kDiscard;
539
540 // Regardless of how the clear is implemented (native clear or a fullscreen quad), all prior ops
541 // would be overwritten, so discard them entirely. The one exception is if the opList is marked
542 // as needing a stencil buffer then there may be a prior op that writes to the stencil buffer.
543 // Although the clear will ignore the stencil buffer, following draw ops may not so we can't get
544 // rid of all the preceding ops. Beware! If we ever add any ops that have a side effect beyond
545 // modifying the stencil buffer we will need a more elaborate tracking system (skbug.com/7002).
Robert Phillips380b90c2017-08-30 07:41:07 -0400546 if (this->isEmpty() || !fTarget.get()->asRenderTargetProxy()->needsStencil()) {
Robert Phillipsc994a932018-06-19 13:09:54 -0400547 this->deleteOps();
Brian Osman099fa0f2017-10-02 16:38:32 -0400548 fDeferredProxies.reset();
Greg Daniel070cbaf2019-01-03 17:35:54 -0500549
550 // If the opList is using a render target which wraps a vulkan command buffer, we can't do a
551 // clear load since we cannot change the render pass that we are using. Thus we fall back to
552 // making a clear op in this case.
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500553 return !fTarget.get()->asRenderTargetProxy()->wrapsVkSecondaryCB();
bsalomonfd8d0132016-08-11 11:25:33 -0700554 }
Robert Phillips380b90c2017-08-30 07:41:07 -0400555
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500556 // Could not empty the list, so an op must be added to handle the clear
557 return false;
bsalomon9f129de2016-08-10 16:31:05 -0700558}
559
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000560////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000561
Robert Phillips81dd3e02017-06-23 11:59:24 -0400562// This closely parallels GrTextureOpList::copySurface but renderTargetOpLists
563// also store the applied clip and dest proxy with the op
Robert Phillips7c525e62018-06-12 10:11:12 -0400564bool GrRenderTargetOpList::copySurface(GrContext* context,
Robert Phillipsa16f6cb2017-06-01 11:06:13 -0400565 GrSurfaceProxy* dst,
Robert Phillipsbf25d432017-04-07 10:08:53 -0400566 GrSurfaceProxy* src,
Robert Phillipsf2361d22016-10-25 14:20:06 -0400567 const SkIRect& srcRect,
568 const SkIPoint& dstPoint) {
Robert Phillips5efd5ea2017-05-30 13:47:32 -0400569 SkASSERT(dst->asRenderTargetProxy() == fTarget.get());
Robert Phillips7c525e62018-06-12 10:11:12 -0400570 std::unique_ptr<GrOp> op = GrCopySurfaceOp::Make(context, dst, src, srcRect, dstPoint);
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500571 if (!op) {
bsalomonb8fea972016-02-16 07:34:17 -0800572 return false;
573 }
robertphillips498d7ac2015-10-30 10:11:30 -0700574
Robert Phillips7c525e62018-06-12 10:11:12 -0400575 this->addOp(std::move(op), *context->contextPriv().caps());
bsalomonb8fea972016-02-16 07:34:17 -0800576 return true;
bsalomon@google.comeb851172013-04-15 13:51:00 +0000577}
578
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500579void GrRenderTargetOpList::purgeOpsWithUninstantiatedProxies() {
580 bool hasUninstantiatedProxy = false;
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400581 auto checkInstantiation = [&hasUninstantiatedProxy](GrSurfaceProxy* p) {
582 if (!p->isInstantiated()) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500583 hasUninstantiatedProxy = true;
584 }
585 };
Brian Salomon588cec72018-11-14 13:56:37 -0500586 for (OpChain& recordedOp : fOpChains) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500587 hasUninstantiatedProxy = false;
Brian Salomon588cec72018-11-14 13:56:37 -0500588 recordedOp.visitProxies(checkInstantiation, GrOp::VisitorType::kOther);
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500589 if (hasUninstantiatedProxy) {
590 // When instantiation of the proxy fails we drop the Op
Brian Salomon588cec72018-11-14 13:56:37 -0500591 recordedOp.deleteOps(fOpMemoryPool.get());
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500592 }
593 }
594}
595
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400596void GrRenderTargetOpList::gatherProxyIntervals(GrResourceAllocator* alloc) const {
597 unsigned int cur = alloc->numOps();
598
Robert Phillips51b20f22017-12-01 15:32:35 -0500599 for (int i = 0; i < fDeferredProxies.count(); ++i) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400600 SkASSERT(!fDeferredProxies[i]->isInstantiated());
Robert Phillips51b20f22017-12-01 15:32:35 -0500601 // We give all the deferred proxies a write usage at the very start of flushing. This
602 // locks them out of being reused for the entire flush until they are read - and then
603 // they can be recycled. This is a bit unfortunate because a flush can proceed in waves
604 // with sub-flushes. The deferred proxies only need to be pinned from the start of
605 // the sub-flush in which they appear.
606 alloc->addInterval(fDeferredProxies[i], 0, 0);
607 }
608
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400609 // Add the interval for all the writes to this opList's target
Brian Salomon588cec72018-11-14 13:56:37 -0500610 if (fOpChains.count()) {
611 alloc->addInterval(fTarget.get(), cur, cur + fOpChains.count() - 1);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500612 } else {
613 // This can happen if there is a loadOp (e.g., a clear) but no other draws. In this case we
614 // still need to add an interval for the destination so we create a fake op# for
615 // the missing clear op.
616 alloc->addInterval(fTarget.get());
617 alloc->incOps();
618 }
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400619
Chris Dalton8816b932017-11-29 16:48:25 -0700620 auto gather = [ alloc SkDEBUGCODE(, this) ] (GrSurfaceProxy* p) {
621 alloc->addInterval(p SkDEBUGCODE(, fTarget.get() == p));
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400622 };
Brian Salomon588cec72018-11-14 13:56:37 -0500623 for (const OpChain& recordedOp : fOpChains) {
Brian Salomon7d94bb52018-10-12 14:37:19 -0400624 // only diff from the GrTextureOpList version
625 recordedOp.visitProxies(gather, GrOp::VisitorType::kAllocatorGather);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500626
627 // Even though the op may have been moved we still need to increment the op count to
628 // keep all the math consistent.
629 alloc->incOps();
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400630 }
631}
632
Brian Salomon348a0372018-10-31 10:42:18 -0400633void GrRenderTargetOpList::recordOp(std::unique_ptr<GrOp> op,
634 const GrCaps& caps,
635 GrAppliedClip* clip,
636 const DstProxy* dstProxy) {
Ethan Nicholas029b22c2018-10-18 16:49:56 -0400637 SkDEBUGCODE(op->validate();)
Robert Phillips318c4192017-05-17 09:36:38 -0400638 SkASSERT(fTarget.get());
Robert Phillipsee683652017-04-26 11:53:10 -0400639
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500640 // A closed GrOpList should never receive new/more ops
robertphillips6a186652015-10-20 07:37:58 -0700641 SkASSERT(!this->isClosed());
Brian Salomon19ec80f2018-11-16 13:27:30 -0500642 if (!op->bounds().isFinite()) {
643 fOpMemoryPool->release(std::move(op));
644 return;
645 }
robertphillipsa106c622015-10-16 09:07:06 -0700646
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500647 // Check if there is an op we can combine with by linearly searching back until we either
648 // 1) check every op
bsalomon512be532015-09-10 10:42:55 -0700649 // 2) intersect with something
650 // 3) find a 'blocker'
Robert Phillips5efd5ea2017-05-30 13:47:32 -0400651 GR_AUDIT_TRAIL_ADD_OP(fAuditTrail, op.get(), fTarget.get()->uniqueID());
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400652 GrOP_INFO("opList: %d Recording (%s, opID: %u)\n"
653 "\tBounds [L: %.2f, T: %.2f R: %.2f B: %.2f]\n",
654 this->uniqueID(),
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500655 op->name(),
656 op->uniqueID(),
Robert Phillips1119dc32017-04-11 12:54:57 -0400657 op->bounds().fLeft, op->bounds().fTop,
658 op->bounds().fRight, op->bounds().fBottom);
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500659 GrOP_INFO(SkTabString(op->dumpInfo(), 1).c_str());
Brian Salomon25a88092016-12-01 09:36:50 -0500660 GrOP_INFO("\tOutcome:\n");
Brian Salomon588cec72018-11-14 13:56:37 -0500661 int maxCandidates = SkTMin(kMaxOpChainDistance, fOpChains.count());
Robert Phillips318c4192017-05-17 09:36:38 -0400662 if (maxCandidates) {
bsalomon512be532015-09-10 10:42:55 -0700663 int i = 0;
664 while (true) {
Brian Salomon588cec72018-11-14 13:56:37 -0500665 OpChain& candidate = fOpChains.fromBack(i);
666 op = candidate.appendOp(std::move(op), dstProxy, clip, caps, fOpMemoryPool.get(),
667 fAuditTrail);
668 if (!op) {
669 return;
bsalomon512be532015-09-10 10:42:55 -0700670 }
Brian Salomona7682c82018-10-24 10:04:37 -0400671 // Stop going backwards if we would cause a painter's order violation.
Brian Salomon588cec72018-11-14 13:56:37 -0500672 if (!can_reorder(candidate.bounds(), op->bounds())) {
673 GrOP_INFO("\t\tBackward: Intersects with chain (%s, head opID: %u)\n",
674 candidate.head()->name(), candidate.head()->uniqueID());
bsalomon512be532015-09-10 10:42:55 -0700675 break;
676 }
Brian Salomon588cec72018-11-14 13:56:37 -0500677 if (++i == maxCandidates) {
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400678 GrOP_INFO("\t\tBackward: Reached max lookback or beginning of op array %d\n", i);
bsalomon512be532015-09-10 10:42:55 -0700679 break;
680 }
681 }
682 } else {
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400683 GrOP_INFO("\t\tBackward: FirstOp\n");
bsalomon512be532015-09-10 10:42:55 -0700684 }
Brian Salomon54d212e2017-03-21 14:22:38 -0400685 if (clip) {
686 clip = fClipAllocator.make<GrAppliedClip>(std::move(*clip));
Robert Phillipsc84c0302017-05-08 15:35:11 -0400687 SkDEBUGCODE(fNumClips++;)
Brian Salomon54d212e2017-03-21 14:22:38 -0400688 }
Brian Salomon588cec72018-11-14 13:56:37 -0500689 fOpChains.emplace_back(std::move(op), clip, dstProxy);
bsalomon512be532015-09-10 10:42:55 -0700690}
691
Robert Phillipsee683652017-04-26 11:53:10 -0400692void GrRenderTargetOpList::forwardCombine(const GrCaps& caps) {
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400693 SkASSERT(!this->isClosed());
Brian Salomon588cec72018-11-14 13:56:37 -0500694 GrOP_INFO("opList: %d ForwardCombine %d ops:\n", this->uniqueID(), fOpChains.count());
Robert Phillips48567ac2017-06-01 08:46:00 -0400695
Brian Salomon588cec72018-11-14 13:56:37 -0500696 for (int i = 0; i < fOpChains.count() - 1; ++i) {
697 OpChain& chain = fOpChains[i];
698 int maxCandidateIdx = SkTMin(i + kMaxOpChainDistance, fOpChains.count() - 1);
bsalomonaecc0182016-03-07 11:50:44 -0800699 int j = i + 1;
700 while (true) {
Brian Salomon588cec72018-11-14 13:56:37 -0500701 OpChain& candidate = fOpChains[j];
702 if (candidate.prependChain(&chain, caps, fOpMemoryPool.get(), fAuditTrail)) {
bsalomonaecc0182016-03-07 11:50:44 -0800703 break;
704 }
Robert Phillipsc84c0302017-05-08 15:35:11 -0400705 // Stop traversing if we would cause a painter's order violation.
Brian Salomon588cec72018-11-14 13:56:37 -0500706 if (!can_reorder(chain.bounds(), candidate.bounds())) {
707 GrOP_INFO(
708 "\t\t%d: chain (%s head opID: %u) -> "
709 "Intersects with chain (%s, head opID: %u)\n",
710 i, chain.head()->name(), chain.head()->uniqueID(), candidate.head()->name(),
711 candidate.head()->uniqueID());
bsalomonaecc0182016-03-07 11:50:44 -0800712 break;
713 }
Brian Salomona7682c82018-10-24 10:04:37 -0400714 if (++j > maxCandidateIdx) {
Brian Salomon588cec72018-11-14 13:56:37 -0500715 GrOP_INFO("\t\t%d: chain (%s opID: %u) -> Reached max lookahead or end of array\n",
716 i, chain.head()->name(), chain.head()->uniqueID());
bsalomonaecc0182016-03-07 11:50:44 -0800717 break;
718 }
719 }
720 }
721}
722