blob: b77f906e28861e57f0210e0255f02197edaea80d [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"
Brian Salomon588cec72018-11-14 13:56:37 -050017#include "SkRectPriv.h"
18#include "SkTraceEvent.h"
Brian Salomon89527432016-12-16 09:52:16 -050019#include "ops/GrClearOp.h"
Brian Salomon89527432016-12-16 09:52:16 -050020#include "ops/GrCopySurfaceOp.h"
Robert Phillipsf2361d22016-10-25 14:20:06 -040021
reed@google.comac10a2d2010-12-22 21:39:39 +000022////////////////////////////////////////////////////////////////////////////////
23
Brian Salomon09d994e2016-12-21 11:14:46 -050024// Experimentally we have found that most combining occurs within the first 10 comparisons.
Brian Salomon588cec72018-11-14 13:56:37 -050025static const int kMaxOpMergeDistance = 10;
26static const int kMaxOpChainDistance = 10;
27
28////////////////////////////////////////////////////////////////////////////////
29
30using DstProxy = GrXferProcessor::DstProxy;
31
32////////////////////////////////////////////////////////////////////////////////
33
34static inline bool can_reorder(const SkRect& a, const SkRect& b) { return !GrRectsOverlap(a, b); }
35
36////////////////////////////////////////////////////////////////////////////////
37
38inline GrRenderTargetOpList::OpChain::List::List(std::unique_ptr<GrOp> op)
39 : fHead(std::move(op)), fTail(fHead.get()) {
40 this->validate();
41}
42
43inline GrRenderTargetOpList::OpChain::List::List(List&& that) { *this = std::move(that); }
44
45inline GrRenderTargetOpList::OpChain::List& GrRenderTargetOpList::OpChain::List::operator=(
46 List&& that) {
47 fHead = std::move(that.fHead);
48 fTail = that.fTail;
49 that.fTail = nullptr;
50 this->validate();
51 return *this;
52}
53
54inline std::unique_ptr<GrOp> GrRenderTargetOpList::OpChain::List::popHead() {
55 SkASSERT(fHead);
56 auto temp = fHead->cutChain();
57 std::swap(temp, fHead);
58 if (!fHead) {
59 SkASSERT(fTail == temp.get());
60 fTail = nullptr;
61 }
62 return temp;
63}
64
65inline std::unique_ptr<GrOp> GrRenderTargetOpList::OpChain::List::removeOp(GrOp* op) {
66#ifdef SK_DEBUG
67 auto head = op;
68 while (head->prevInChain()) { head = head->prevInChain(); }
69 SkASSERT(head == fHead.get());
70#endif
71 auto prev = op->prevInChain();
72 if (!prev) {
73 SkASSERT(op == fHead.get());
74 return this->popHead();
75 }
76 auto temp = prev->cutChain();
77 if (auto next = temp->cutChain()) {
78 prev->chainConcat(std::move(next));
79 } else {
80 SkASSERT(fTail == op);
81 fTail = prev;
82 }
83 this->validate();
84 return temp;
85}
86
87inline void GrRenderTargetOpList::OpChain::List::pushHead(std::unique_ptr<GrOp> op) {
88 SkASSERT(op);
89 SkASSERT(op->isChainHead());
90 SkASSERT(op->isChainTail());
91 if (fHead) {
92 op->chainConcat(std::move(fHead));
93 fHead = std::move(op);
94 } else {
95 fHead = std::move(op);
96 fTail = fHead.get();
97 }
98}
99
100inline void GrRenderTargetOpList::OpChain::List::pushTail(std::unique_ptr<GrOp> op) {
101 SkASSERT(op->isChainTail());
102 fTail->chainConcat(std::move(op));
103 fTail = fTail->nextInChain();
104}
105
106inline void GrRenderTargetOpList::OpChain::List::validate() const {
107#ifdef SK_DEBUG
108 if (fHead) {
109 SkASSERT(fTail);
110 fHead->validateChain(fTail);
111 }
112#endif
113}
114
115////////////////////////////////////////////////////////////////////////////////
116
117GrRenderTargetOpList::OpChain::OpChain(std::unique_ptr<GrOp> op, GrAppliedClip* appliedClip,
118 const DstProxy* dstProxy)
119 : fList{std::move(op)}, fAppliedClip(appliedClip) {
120 if (dstProxy) {
121 fDstProxy = *dstProxy;
122 }
123 fBounds = fList.head()->bounds();
124}
125
126void GrRenderTargetOpList::OpChain::visitProxies(const GrOp::VisitProxyFunc& func,
127 GrOp::VisitorType visitor) const {
128 if (fList.empty()) {
129 return;
130 }
131 for (const auto& op : GrOp::ChainRange<>(fList.head())) {
132 op.visitProxies(func, visitor);
133 }
134 if (fDstProxy.proxy()) {
135 func(fDstProxy.proxy());
136 }
137 if (fAppliedClip) {
138 fAppliedClip->visitProxies(func);
139 }
140}
141
142void GrRenderTargetOpList::OpChain::deleteOps(GrOpMemoryPool* pool) {
143 while (!fList.empty()) {
144 pool->release(fList.popHead());
145 }
146}
147
148// Concatenates two op chains and attempts to merge ops across the chains. Assumes that we know that
149// the two chains are chainable. Returns the new chain.
150GrRenderTargetOpList::OpChain::List GrRenderTargetOpList::OpChain::DoConcat(
151 List chainA, List chainB, const GrCaps& caps, GrOpMemoryPool* pool,
152 GrAuditTrail* auditTrail) {
153 // We process ops in chain b from head to tail. We attempt to merge with nodes in a, starting
154 // at chain a's tail and working toward the head. We produce one of the following outcomes:
155 // 1) b's head is merged into an op in a.
156 // 2) An op from chain a is merged into b's head. (In this case b's head gets processed again.)
157 // 3) b's head is popped from chain a and added at the tail of a.
158 // After result 3 we don't want to attempt to merge the next head of b with the new tail of a,
159 // as we assume merges were already attempted when chain b was created. So we keep track of the
160 // original tail of a and start our iteration of a there. We also track the bounds of the nodes
161 // appended to chain a that will be skipped for bounds testing. If the original tail of a is
162 // merged into an op in b (case 2) then we advance the "original tail" towards the head of a.
163 GrOp* origATail = chainA.tail();
164 SkRect skipBounds = SkRectPriv::MakeLargestInverted();
165 do {
166 int numMergeChecks = 0;
167 bool merged = false;
168 bool noSkip = (origATail == chainA.tail());
169 SkASSERT(noSkip == (skipBounds == SkRectPriv::MakeLargestInverted()));
170 bool canBackwardMerge = noSkip || can_reorder(chainB.head()->bounds(), skipBounds);
171 SkRect forwardMergeBounds = skipBounds;
172 GrOp* a = origATail;
173 while (a) {
174 bool canForwardMerge =
175 (a == chainA.tail()) || can_reorder(a->bounds(), forwardMergeBounds);
176 if (canForwardMerge || canBackwardMerge) {
177 auto result = a->combineIfPossible(chainB.head(), caps);
178 SkASSERT(result != GrOp::CombineResult::kCannotCombine);
179 merged = (result == GrOp::CombineResult::kMerged);
180 GrOP_INFO("\t\t%d: (%s opID: %u) -> Combining with (%s, opID: %u)\n", i,
181 chainB.head()->name(), chainB.head()->uniqueID(), a->name(),
182 a->uniqueID());
Brian Salomon588cec72018-11-14 13:56:37 -0500183 }
184 if (merged) {
Brian Salomon52a6ed32018-11-26 10:30:58 -0500185 GR_AUDIT_TRAIL_OPS_RESULT_COMBINED(auditTrail, a, chainB.head());
Brian Salomon588cec72018-11-14 13:56:37 -0500186 if (canBackwardMerge) {
187 pool->release(chainB.popHead());
188 } else {
189 // We merged the contents of b's head into a. We will replace b's head with a in
190 // chain b.
191 SkASSERT(canForwardMerge);
192 if (a == origATail) {
193 origATail = a->prevInChain();
194 }
195 std::unique_ptr<GrOp> detachedA = chainA.removeOp(a);
196 pool->release(chainB.popHead());
197 chainB.pushHead(std::move(detachedA));
198 if (chainA.empty()) {
199 // We merged all the nodes in chain a to chain b.
200 return chainB;
201 }
202 }
203 break;
204 } else {
205 if (++numMergeChecks == kMaxOpMergeDistance) {
206 break;
207 }
208 forwardMergeBounds.joinNonEmptyArg(a->bounds());
209 canBackwardMerge =
210 canBackwardMerge && can_reorder(chainB.head()->bounds(), a->bounds());
211 a = a->prevInChain();
212 }
213 }
214 // If we weren't able to merge b's head then pop b's head from chain b and make it the new
215 // tail of a.
216 if (!merged) {
217 chainA.pushTail(chainB.popHead());
218 skipBounds.joinNonEmptyArg(chainA.tail()->bounds());
219 }
220 } while (!chainB.empty());
221 return chainA;
222}
223
224// Attempts to concatenate two chains and merge ops across the chains. Upon failure the original
225// chain heads and tails are returned. Upon success the new chain's head and tail are returned
226// (and null for the second head/tail).
227std::tuple<GrRenderTargetOpList::OpChain::List, GrRenderTargetOpList::OpChain::List>
228GrRenderTargetOpList::OpChain::TryConcat(List chainA, const DstProxy& dstProxyA,
229 const GrAppliedClip* appliedClipA, List chainB,
230 const DstProxy& dstProxyB,
231 const GrAppliedClip* appliedClipB, const GrCaps& caps,
232 GrOpMemoryPool* pool, GrAuditTrail* auditTrail) {
233 SkASSERT(!chainA.empty());
234 SkASSERT(!chainB.empty());
235 // All returns use explicit tuple constructor rather than {a, b} to work around old GCC bug.
236 if (chainA.head()->classID() != chainB.head()->classID() ||
237 SkToBool(appliedClipA) != SkToBool(appliedClipB) ||
238 (appliedClipA && *appliedClipA != *appliedClipB) ||
239 SkToBool(dstProxyA.proxy()) != SkToBool(dstProxyB.proxy()) ||
240 (dstProxyA.proxy() && dstProxyA != dstProxyB)) {
241 return std::tuple<List, List>(std::move(chainA), std::move(chainB));
242 }
243 SkDEBUGCODE(bool first = true;)
244 do {
245 switch (chainA.tail()->combineIfPossible(chainB.head(), caps)) {
246 case GrOp::CombineResult::kCannotCombine:
247 // If an op supports chaining then it is required that chaining is transitive and
248 // that if any two ops in two different chains can merge then the two chains
249 // may also be chained together. Thus, we should only hit this on the first
250 // iteration.
251 SkASSERT(first);
252 return std::tuple<List, List>(std::move(chainA), std::move(chainB));
253 case GrOp::CombineResult::kMayChain:
254 chainA = DoConcat(std::move(chainA), std::move(chainB), caps, pool, auditTrail);
255 return std::tuple<List, List>(std::move(chainA), List());
256 case GrOp::CombineResult::kMerged: {
257 GrOP_INFO("\t\t%d: (%s opID: %u) -> Combining with (%s, opID: %u)\n", i,
258 chainB.tail()->name(), chainB.tail()->uniqueID(), chainB.head()->name(),
259 chainB.head()->uniqueID());
260 GR_AUDIT_TRAIL_OPS_RESULT_COMBINED(auditTrail, chainA.tail(), chainB.head());
261 pool->release(chainB.popHead());
262 break;
263 }
264 }
265 SkDEBUGCODE(first = false);
266 } while (!chainB.empty());
267 // All the ops from chain b merged.
268 return std::tuple<List, List>(std::move(chainA), List());
269}
270
271bool GrRenderTargetOpList::OpChain::prependChain(OpChain* that, const GrCaps& caps,
272 GrOpMemoryPool* pool, GrAuditTrail* auditTrail) {
273 std::tie(that->fList, fList) = TryConcat(
274 std::move(that->fList), that->dstProxy(), that->appliedClip(), std::move(fList),
275 this->dstProxy(), this->appliedClip(), caps, pool, auditTrail);
276 if (!fList.empty()) {
277 this->validate();
278 // append failed
279 return false;
280 }
281 // 'that' owns the combined chain. Move it into 'this'.
282 fList = std::move(that->fList);
283 fBounds.joinPossiblyEmptyRect(that->fBounds);
284
285 that->fDstProxy.setProxy(nullptr);
286 if (that->fAppliedClip) {
287 for (int i = 0; i < that->fAppliedClip->numClipCoverageFragmentProcessors(); ++i) {
288 that->fAppliedClip->detachClipCoverageFragmentProcessor(i);
289 }
290 }
291 this->validate();
292 return true;
293}
294
295std::unique_ptr<GrOp> GrRenderTargetOpList::OpChain::appendOp(std::unique_ptr<GrOp> op,
296 const DstProxy* dstProxy,
297 const GrAppliedClip* appliedClip,
298 const GrCaps& caps,
299 GrOpMemoryPool* pool,
300 GrAuditTrail* auditTrail) {
301 const GrXferProcessor::DstProxy noDstProxy;
302 if (!dstProxy) {
303 dstProxy = &noDstProxy;
304 }
305 SkASSERT(op->isChainHead() && op->isChainTail());
306 SkRect opBounds = op->bounds();
307 List chain(std::move(op));
308 std::tie(fList, chain) =
309 TryConcat(std::move(fList), this->dstProxy(), fAppliedClip, std::move(chain), *dstProxy,
310 appliedClip, caps, pool, auditTrail);
Mike Klein16885072018-12-11 09:54:31 -0500311 if (!chain.empty()) { // NOLINT(bugprone-use-after-move)
Brian Salomon588cec72018-11-14 13:56:37 -0500312 // append failed, give the op back to the caller.
313 this->validate();
314 return chain.popHead();
315 }
316 fBounds.joinPossiblyEmptyRect(opBounds);
317 this->validate();
318 return nullptr;
319}
320
321inline void GrRenderTargetOpList::OpChain::validate() const {
322#ifdef SK_DEBUG
323 fList.validate();
324 for (const auto& op : GrOp::ChainRange<>(fList.head())) {
325 // Not using SkRect::contains because we allow empty rects.
326 SkASSERT(fBounds.fLeft <= op.bounds().fLeft && fBounds.fTop <= op.bounds().fTop &&
327 fBounds.fRight >= op.bounds().fRight && fBounds.fBottom >= op.bounds().fBottom);
328 }
329#endif
330}
331
332////////////////////////////////////////////////////////////////////////////////
bsalomon489147c2015-12-14 12:13:09 -0800333
Robert Phillips3a9710b2018-03-27 17:51:55 -0400334GrRenderTargetOpList::GrRenderTargetOpList(GrResourceProvider* resourceProvider,
Robert Phillipsc994a932018-06-19 13:09:54 -0400335 sk_sp<GrOpMemoryPool> opMemoryPool,
Robert Phillips3a9710b2018-03-27 17:51:55 -0400336 GrRenderTargetProxy* proxy,
Robert Phillips8185f592017-04-26 08:31:08 -0400337 GrAuditTrail* auditTrail)
Robert Phillipsc994a932018-06-19 13:09:54 -0400338 : INHERITED(resourceProvider, std::move(opMemoryPool), proxy, auditTrail)
Brian Salomonc3833b42018-07-09 18:23:58 +0000339 , fLastClipStackGenID(SK_InvalidUniqueID)
Robert Phillipsb6deea82017-05-11 14:14:30 -0400340 SkDEBUGCODE(, fNumClips(0)) {
bsalomon4061b122015-05-29 10:26:19 -0700341}
342
Robert Phillipsc994a932018-06-19 13:09:54 -0400343void GrRenderTargetOpList::deleteOps() {
Brian Salomon588cec72018-11-14 13:56:37 -0500344 for (auto& chain : fOpChains) {
345 chain.deleteOps(fOpMemoryPool.get());
Robert Phillipsc994a932018-06-19 13:09:54 -0400346 }
Brian Salomon588cec72018-11-14 13:56:37 -0500347 fOpChains.reset();
Robert Phillipsc994a932018-06-19 13:09:54 -0400348}
349
Robert Phillipsf2361d22016-10-25 14:20:06 -0400350GrRenderTargetOpList::~GrRenderTargetOpList() {
Robert Phillipsc994a932018-06-19 13:09:54 -0400351 this->deleteOps();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000352}
353
354////////////////////////////////////////////////////////////////////////////////
355
robertphillips4beb5c12015-10-20 07:50:00 -0700356#ifdef SK_DEBUG
Robert Phillips27483912018-04-20 12:43:18 -0400357void GrRenderTargetOpList::dump(bool printDependencies) const {
358 INHERITED::dump(printDependencies);
Robert Phillipsf2361d22016-10-25 14:20:06 -0400359
Brian Salomon588cec72018-11-14 13:56:37 -0500360 SkDebugf("ops (%d):\n", fOpChains.count());
361 for (int i = 0; i < fOpChains.count(); ++i) {
robertphillips4beb5c12015-10-20 07:50:00 -0700362 SkDebugf("*******************************\n");
Brian Salomon588cec72018-11-14 13:56:37 -0500363 if (!fOpChains[i].head()) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500364 SkDebugf("%d: <combined forward or failed instantiation>\n", i);
bsalomonaecc0182016-03-07 11:50:44 -0800365 } else {
Brian Salomon588cec72018-11-14 13:56:37 -0500366 SkDebugf("%d: %s\n", i, fOpChains[i].head()->name());
367 SkRect bounds = fOpChains[i].bounds();
Brian Salomon9e50f7b2017-03-06 12:02:34 -0500368 SkDebugf("ClippedBounds: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", bounds.fLeft,
369 bounds.fTop, bounds.fRight, bounds.fBottom);
Brian Salomon588cec72018-11-14 13:56:37 -0500370 for (const auto& op : GrOp::ChainRange<>(fOpChains[i].head())) {
371 SkString info = SkTabString(op.dumpInfo(), 1);
372 SkDebugf("%s\n", info.c_str());
373 bounds = op.bounds();
374 SkDebugf("\tClippedBounds: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", bounds.fLeft,
375 bounds.fTop, bounds.fRight, bounds.fBottom);
376 }
bsalomonaecc0182016-03-07 11:50:44 -0800377 }
robertphillips4beb5c12015-10-20 07:50:00 -0700378 }
379}
Chris Dalton706a6ff2017-11-29 22:01:06 -0700380
381void GrRenderTargetOpList::visitProxies_debugOnly(const GrOp::VisitProxyFunc& func) const {
Brian Salomon588cec72018-11-14 13:56:37 -0500382 for (const OpChain& chain : fOpChains) {
383 chain.visitProxies(func, GrOp::VisitorType::kOther);
Chris Dalton706a6ff2017-11-29 22:01:06 -0700384 }
385}
Brian Salomonc525d4f2018-09-17 15:48:20 -0400386
robertphillips4beb5c12015-10-20 07:50:00 -0700387#endif
388
Brian Osman407b3422017-08-22 15:01:32 -0400389void GrRenderTargetOpList::onPrepare(GrOpFlushState* flushState) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400390 SkASSERT(fTarget.get()->peekRenderTarget());
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400391 SkASSERT(this->isClosed());
Stan Iliev2af578d2017-08-16 13:00:28 -0400392#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
393 TRACE_EVENT0("skia", TRACE_FUNC);
394#endif
robertphillipsa106c622015-10-16 09:07:06 -0700395
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500396 // Loop over the ops that haven't yet been prepared.
Brian Salomon588cec72018-11-14 13:56:37 -0500397 for (const auto& chain : fOpChains) {
398 if (chain.head()) {
Stan Iliev2af578d2017-08-16 13:00:28 -0400399#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
Brian Salomon588cec72018-11-14 13:56:37 -0500400 TRACE_EVENT0("skia", chain.head()->name());
Stan Iliev2af578d2017-08-16 13:00:28 -0400401#endif
Brian Salomon29b60c92017-10-31 14:42:10 -0400402 GrOpFlushState::OpArgs opArgs = {
Brian Salomon588cec72018-11-14 13:56:37 -0500403 chain.head(),
Robert Phillips2890fbf2017-07-26 15:48:41 -0400404 fTarget.get()->asRenderTargetProxy(),
Brian Salomon588cec72018-11-14 13:56:37 -0500405 chain.appliedClip(),
406 chain.dstProxy()
Robert Phillips318c4192017-05-17 09:36:38 -0400407 };
Brian Salomon29b60c92017-10-31 14:42:10 -0400408 flushState->setOpArgs(&opArgs);
Brian Salomon588cec72018-11-14 13:56:37 -0500409 chain.head()->prepare(flushState);
Brian Salomon29b60c92017-10-31 14:42:10 -0400410 flushState->setOpArgs(nullptr);
bsalomonaecc0182016-03-07 11:50:44 -0800411 }
bsalomon512be532015-09-10 10:42:55 -0700412 }
robertphillipsa13e2022015-11-11 12:01:09 -0800413}
bsalomon512be532015-09-10 10:42:55 -0700414
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400415static GrGpuRTCommandBuffer* create_command_buffer(GrGpu* gpu,
416 GrRenderTarget* rt,
417 GrSurfaceOrigin origin,
Ethan Nicholas56d19a52018-10-15 11:26:20 -0400418 const SkRect& bounds,
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400419 GrLoadOp colorLoadOp,
Brian Osman9a9baae2018-11-05 15:06:26 -0500420 const SkPMColor4f& loadClearColor,
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400421 GrLoadOp stencilLoadOp) {
Robert Phillipscb2e2352017-08-30 16:44:40 -0400422 const GrGpuRTCommandBuffer::LoadAndStoreInfo kColorLoadStoreInfo {
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400423 colorLoadOp,
424 GrStoreOp::kStore,
425 loadClearColor
Robert Phillips178ce3e2017-04-13 09:15:47 -0400426 };
427
Robert Phillips95214472017-08-08 18:00:03 -0400428 // TODO:
429 // We would like to (at this level) only ever clear & discard. We would need
430 // to stop splitting up higher level opLists for copyOps to achieve that.
431 // Note: we would still need SB loads and stores but they would happen at a
432 // lower level (inside the VK command buffer).
Greg Daniel500d58b2017-08-24 15:59:33 -0400433 const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo stencilLoadAndStoreInfo {
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400434 stencilLoadOp,
435 GrStoreOp::kStore,
Robert Phillips95214472017-08-08 18:00:03 -0400436 };
437
Ethan Nicholas56d19a52018-10-15 11:26:20 -0400438 return gpu->getCommandBuffer(rt, origin, bounds, kColorLoadStoreInfo, stencilLoadAndStoreInfo);
Robert Phillips178ce3e2017-04-13 09:15:47 -0400439}
440
Brian Salomon25a88092016-12-01 09:36:50 -0500441// TODO: this is where GrOp::renderTarget is used (which is fine since it
Robert Phillips294870f2016-11-11 12:38:40 -0500442// is at flush time). However, we need to store the RenderTargetProxy in the
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500443// Ops and instantiate them here.
Brian Osman407b3422017-08-22 15:01:32 -0400444bool GrRenderTargetOpList::onExecute(GrOpFlushState* flushState) {
Greg Danieldbdba602018-04-20 11:52:43 -0400445 // TODO: Forcing the execution of the discard here isn't ideal since it will cause us to do a
446 // discard and then store the data back in memory so that the load op on future draws doesn't
447 // think the memory is unitialized. Ideally we would want a system where we are tracking whether
448 // the proxy itself has valid data or not, and then use that as a signal on whether we should be
449 // loading or discarding. In that world we wouldni;t need to worry about executing oplists with
450 // no ops just to do a discard.
Brian Salomon588cec72018-11-14 13:56:37 -0500451 if (fOpChains.empty() && GrLoadOp::kClear != fColorLoadOp &&
Greg Danieldbdba602018-04-20 11:52:43 -0400452 GrLoadOp::kDiscard != fColorLoadOp) {
bsalomondc438982016-08-31 11:53:49 -0700453 return false;
egdanielb4021cf2016-07-28 08:53:07 -0700454 }
Robert Phillips4a395042017-04-24 16:27:17 +0000455
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400456 SkASSERT(fTarget.get()->peekRenderTarget());
Stan Iliev2af578d2017-08-16 13:00:28 -0400457 TRACE_EVENT0("skia", TRACE_FUNC);
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400458
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400459 // TODO: at the very least, we want the stencil store op to always be discard (at this
460 // level). In Vulkan, sub-command buffers would still need to load & store the stencil buffer.
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400461 GrGpuRTCommandBuffer* commandBuffer = create_command_buffer(
Robert Phillips95214472017-08-08 18:00:03 -0400462 flushState->gpu(),
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400463 fTarget.get()->peekRenderTarget(),
Robert Phillips95214472017-08-08 18:00:03 -0400464 fTarget.get()->origin(),
Ethan Nicholas56d19a52018-10-15 11:26:20 -0400465 fTarget.get()->getBoundsRect(),
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400466 fColorLoadOp,
467 fLoadClearColor,
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400468 fStencilLoadOp);
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400469 flushState->setCommandBuffer(commandBuffer);
Robert Phillips95214472017-08-08 18:00:03 -0400470 commandBuffer->begin();
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400471
472 // Draw all the generated geometry.
Brian Salomon588cec72018-11-14 13:56:37 -0500473 for (const auto& chain : fOpChains) {
474 if (!chain.head()) {
bsalomonaecc0182016-03-07 11:50:44 -0800475 continue;
476 }
Stan Iliev2af578d2017-08-16 13:00:28 -0400477#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
Brian Salomon588cec72018-11-14 13:56:37 -0500478 TRACE_EVENT0("skia", chain.head()->name());
Stan Iliev2af578d2017-08-16 13:00:28 -0400479#endif
Robert Phillips178ce3e2017-04-13 09:15:47 -0400480
Brian Salomon29b60c92017-10-31 14:42:10 -0400481 GrOpFlushState::OpArgs opArgs {
Brian Salomon588cec72018-11-14 13:56:37 -0500482 chain.head(),
Robert Phillips2890fbf2017-07-26 15:48:41 -0400483 fTarget.get()->asRenderTargetProxy(),
Brian Salomon588cec72018-11-14 13:56:37 -0500484 chain.appliedClip(),
485 chain.dstProxy()
Robert Phillips178ce3e2017-04-13 09:15:47 -0400486 };
487
Brian Salomon29b60c92017-10-31 14:42:10 -0400488 flushState->setOpArgs(&opArgs);
Brian Salomon588cec72018-11-14 13:56:37 -0500489 chain.head()->execute(flushState, chain.bounds());
Brian Salomon29b60c92017-10-31 14:42:10 -0400490 flushState->setOpArgs(nullptr);
bsalomon512be532015-09-10 10:42:55 -0700491 }
Robert Phillips178ce3e2017-04-13 09:15:47 -0400492
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400493 commandBuffer->end();
494 flushState->gpu()->submit(commandBuffer);
Robert Phillips178ce3e2017-04-13 09:15:47 -0400495 flushState->setCommandBuffer(nullptr);
ethannicholas22793252016-01-30 09:59:10 -0800496
bsalomondc438982016-08-31 11:53:49 -0700497 return true;
bsalomona73239a2015-04-28 13:35:17 -0700498}
499
Chris Daltona84cacf2017-10-04 10:30:29 -0600500void GrRenderTargetOpList::endFlush() {
Brian Salomonc3833b42018-07-09 18:23:58 +0000501 fLastClipStackGenID = SK_InvalidUniqueID;
Robert Phillipsc994a932018-06-19 13:09:54 -0400502 this->deleteOps();
Chris Daltonc82dd4e2017-11-20 18:20:28 -0700503 fClipAllocator.reset();
Chris Daltona84cacf2017-10-04 10:30:29 -0600504 INHERITED::endFlush();
bsalomon512be532015-09-10 10:42:55 -0700505}
506
Robert Phillips380b90c2017-08-30 07:41:07 -0400507void GrRenderTargetOpList::discard() {
508 // Discard calls to in-progress opLists are ignored. Calls at the start update the
509 // opLists' color & stencil load ops.
510 if (this->isEmpty()) {
511 fColorLoadOp = GrLoadOp::kDiscard;
512 fStencilLoadOp = GrLoadOp::kDiscard;
513 }
514}
515
Brian Osman9a9baae2018-11-05 15:06:26 -0500516void GrRenderTargetOpList::fullClear(GrContext* context, const SkPMColor4f& color) {
Robert Phillips380b90c2017-08-30 07:41:07 -0400517
518 // This is conservative. If the opList is marked as needing a stencil buffer then there
519 // may be a prior op that writes to the stencil buffer. Although the clear will ignore the
520 // stencil buffer, following draw ops may not so we can't get rid of all the preceding ops.
521 // Beware! If we ever add any ops that have a side effect beyond modifying the stencil
522 // buffer we will need a more elaborate tracking system (skbug.com/7002).
523 if (this->isEmpty() || !fTarget.get()->asRenderTargetProxy()->needsStencil()) {
Robert Phillipsc994a932018-06-19 13:09:54 -0400524 this->deleteOps();
Brian Osman099fa0f2017-10-02 16:38:32 -0400525 fDeferredProxies.reset();
Greg Daniel070cbaf2019-01-03 17:35:54 -0500526
527 // If the opList is using a render target which wraps a vulkan command buffer, we can't do a
528 // clear load since we cannot change the render pass that we are using. Thus we fall back to
529 // making a clear op in this case.
530 if (!fTarget.get()->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
531 fColorLoadOp = GrLoadOp::kClear;
532 fLoadClearColor = color;
533 return;
534 }
bsalomonfd8d0132016-08-11 11:25:33 -0700535 }
Robert Phillips380b90c2017-08-30 07:41:07 -0400536
Robert Phillips7c525e62018-06-12 10:11:12 -0400537 std::unique_ptr<GrClearOp> op(GrClearOp::Make(context, GrFixedClip::Disabled(),
538 color, fTarget.get()));
Robert Phillipsf7a72612017-03-31 10:03:45 -0400539 if (!op) {
540 return;
541 }
Robert Phillips5efd5ea2017-05-30 13:47:32 -0400542
Robert Phillips7c525e62018-06-12 10:11:12 -0400543 this->recordOp(std::move(op), *context->contextPriv().caps());
bsalomon9f129de2016-08-10 16:31:05 -0700544}
545
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000546////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000547
Robert Phillips81dd3e02017-06-23 11:59:24 -0400548// This closely parallels GrTextureOpList::copySurface but renderTargetOpLists
549// also store the applied clip and dest proxy with the op
Robert Phillips7c525e62018-06-12 10:11:12 -0400550bool GrRenderTargetOpList::copySurface(GrContext* context,
Robert Phillipsa16f6cb2017-06-01 11:06:13 -0400551 GrSurfaceProxy* dst,
Robert Phillipsbf25d432017-04-07 10:08:53 -0400552 GrSurfaceProxy* src,
Robert Phillipsf2361d22016-10-25 14:20:06 -0400553 const SkIRect& srcRect,
554 const SkIPoint& dstPoint) {
Robert Phillips5efd5ea2017-05-30 13:47:32 -0400555 SkASSERT(dst->asRenderTargetProxy() == fTarget.get());
Robert Phillips7c525e62018-06-12 10:11:12 -0400556 std::unique_ptr<GrOp> op = GrCopySurfaceOp::Make(context, dst, src, srcRect, dstPoint);
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500557 if (!op) {
bsalomonb8fea972016-02-16 07:34:17 -0800558 return false;
559 }
robertphillips498d7ac2015-10-30 10:11:30 -0700560
Robert Phillips7c525e62018-06-12 10:11:12 -0400561 this->addOp(std::move(op), *context->contextPriv().caps());
bsalomonb8fea972016-02-16 07:34:17 -0800562 return true;
bsalomon@google.comeb851172013-04-15 13:51:00 +0000563}
564
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500565void GrRenderTargetOpList::purgeOpsWithUninstantiatedProxies() {
566 bool hasUninstantiatedProxy = false;
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400567 auto checkInstantiation = [&hasUninstantiatedProxy](GrSurfaceProxy* p) {
568 if (!p->isInstantiated()) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500569 hasUninstantiatedProxy = true;
570 }
571 };
Brian Salomon588cec72018-11-14 13:56:37 -0500572 for (OpChain& recordedOp : fOpChains) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500573 hasUninstantiatedProxy = false;
Brian Salomon588cec72018-11-14 13:56:37 -0500574 recordedOp.visitProxies(checkInstantiation, GrOp::VisitorType::kOther);
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500575 if (hasUninstantiatedProxy) {
576 // When instantiation of the proxy fails we drop the Op
Brian Salomon588cec72018-11-14 13:56:37 -0500577 recordedOp.deleteOps(fOpMemoryPool.get());
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500578 }
579 }
580}
581
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400582void GrRenderTargetOpList::gatherProxyIntervals(GrResourceAllocator* alloc) const {
583 unsigned int cur = alloc->numOps();
584
Robert Phillips51b20f22017-12-01 15:32:35 -0500585 for (int i = 0; i < fDeferredProxies.count(); ++i) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400586 SkASSERT(!fDeferredProxies[i]->isInstantiated());
Robert Phillips51b20f22017-12-01 15:32:35 -0500587 // We give all the deferred proxies a write usage at the very start of flushing. This
588 // locks them out of being reused for the entire flush until they are read - and then
589 // they can be recycled. This is a bit unfortunate because a flush can proceed in waves
590 // with sub-flushes. The deferred proxies only need to be pinned from the start of
591 // the sub-flush in which they appear.
592 alloc->addInterval(fDeferredProxies[i], 0, 0);
593 }
594
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400595 // Add the interval for all the writes to this opList's target
Brian Salomon588cec72018-11-14 13:56:37 -0500596 if (fOpChains.count()) {
597 alloc->addInterval(fTarget.get(), cur, cur + fOpChains.count() - 1);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500598 } else {
599 // This can happen if there is a loadOp (e.g., a clear) but no other draws. In this case we
600 // still need to add an interval for the destination so we create a fake op# for
601 // the missing clear op.
602 alloc->addInterval(fTarget.get());
603 alloc->incOps();
604 }
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400605
Chris Dalton8816b932017-11-29 16:48:25 -0700606 auto gather = [ alloc SkDEBUGCODE(, this) ] (GrSurfaceProxy* p) {
607 alloc->addInterval(p SkDEBUGCODE(, fTarget.get() == p));
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400608 };
Brian Salomon588cec72018-11-14 13:56:37 -0500609 for (const OpChain& recordedOp : fOpChains) {
Brian Salomon7d94bb52018-10-12 14:37:19 -0400610 // only diff from the GrTextureOpList version
611 recordedOp.visitProxies(gather, GrOp::VisitorType::kAllocatorGather);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500612
613 // Even though the op may have been moved we still need to increment the op count to
614 // keep all the math consistent.
615 alloc->incOps();
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400616 }
617}
618
Brian Salomon348a0372018-10-31 10:42:18 -0400619void GrRenderTargetOpList::recordOp(std::unique_ptr<GrOp> op,
620 const GrCaps& caps,
621 GrAppliedClip* clip,
622 const DstProxy* dstProxy) {
Ethan Nicholas029b22c2018-10-18 16:49:56 -0400623 SkDEBUGCODE(op->validate();)
Robert Phillips318c4192017-05-17 09:36:38 -0400624 SkASSERT(fTarget.get());
Robert Phillipsee683652017-04-26 11:53:10 -0400625
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500626 // A closed GrOpList should never receive new/more ops
robertphillips6a186652015-10-20 07:37:58 -0700627 SkASSERT(!this->isClosed());
Brian Salomon19ec80f2018-11-16 13:27:30 -0500628 if (!op->bounds().isFinite()) {
629 fOpMemoryPool->release(std::move(op));
630 return;
631 }
robertphillipsa106c622015-10-16 09:07:06 -0700632
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500633 // Check if there is an op we can combine with by linearly searching back until we either
634 // 1) check every op
bsalomon512be532015-09-10 10:42:55 -0700635 // 2) intersect with something
636 // 3) find a 'blocker'
Robert Phillips5efd5ea2017-05-30 13:47:32 -0400637 GR_AUDIT_TRAIL_ADD_OP(fAuditTrail, op.get(), fTarget.get()->uniqueID());
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400638 GrOP_INFO("opList: %d Recording (%s, opID: %u)\n"
639 "\tBounds [L: %.2f, T: %.2f R: %.2f B: %.2f]\n",
640 this->uniqueID(),
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500641 op->name(),
642 op->uniqueID(),
Robert Phillips1119dc32017-04-11 12:54:57 -0400643 op->bounds().fLeft, op->bounds().fTop,
644 op->bounds().fRight, op->bounds().fBottom);
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500645 GrOP_INFO(SkTabString(op->dumpInfo(), 1).c_str());
Brian Salomon25a88092016-12-01 09:36:50 -0500646 GrOP_INFO("\tOutcome:\n");
Brian Salomon588cec72018-11-14 13:56:37 -0500647 int maxCandidates = SkTMin(kMaxOpChainDistance, fOpChains.count());
Robert Phillips318c4192017-05-17 09:36:38 -0400648 if (maxCandidates) {
bsalomon512be532015-09-10 10:42:55 -0700649 int i = 0;
650 while (true) {
Brian Salomon588cec72018-11-14 13:56:37 -0500651 OpChain& candidate = fOpChains.fromBack(i);
652 op = candidate.appendOp(std::move(op), dstProxy, clip, caps, fOpMemoryPool.get(),
653 fAuditTrail);
654 if (!op) {
655 return;
bsalomon512be532015-09-10 10:42:55 -0700656 }
Brian Salomona7682c82018-10-24 10:04:37 -0400657 // Stop going backwards if we would cause a painter's order violation.
Brian Salomon588cec72018-11-14 13:56:37 -0500658 if (!can_reorder(candidate.bounds(), op->bounds())) {
659 GrOP_INFO("\t\tBackward: Intersects with chain (%s, head opID: %u)\n",
660 candidate.head()->name(), candidate.head()->uniqueID());
bsalomon512be532015-09-10 10:42:55 -0700661 break;
662 }
Brian Salomon588cec72018-11-14 13:56:37 -0500663 if (++i == maxCandidates) {
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400664 GrOP_INFO("\t\tBackward: Reached max lookback or beginning of op array %d\n", i);
bsalomon512be532015-09-10 10:42:55 -0700665 break;
666 }
667 }
668 } else {
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400669 GrOP_INFO("\t\tBackward: FirstOp\n");
bsalomon512be532015-09-10 10:42:55 -0700670 }
Brian Salomon54d212e2017-03-21 14:22:38 -0400671 if (clip) {
672 clip = fClipAllocator.make<GrAppliedClip>(std::move(*clip));
Robert Phillipsc84c0302017-05-08 15:35:11 -0400673 SkDEBUGCODE(fNumClips++;)
Brian Salomon54d212e2017-03-21 14:22:38 -0400674 }
Brian Salomon588cec72018-11-14 13:56:37 -0500675 fOpChains.emplace_back(std::move(op), clip, dstProxy);
bsalomon512be532015-09-10 10:42:55 -0700676}
677
Robert Phillipsee683652017-04-26 11:53:10 -0400678void GrRenderTargetOpList::forwardCombine(const GrCaps& caps) {
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400679 SkASSERT(!this->isClosed());
Brian Salomon588cec72018-11-14 13:56:37 -0500680 GrOP_INFO("opList: %d ForwardCombine %d ops:\n", this->uniqueID(), fOpChains.count());
Robert Phillips48567ac2017-06-01 08:46:00 -0400681
Brian Salomon588cec72018-11-14 13:56:37 -0500682 for (int i = 0; i < fOpChains.count() - 1; ++i) {
683 OpChain& chain = fOpChains[i];
684 int maxCandidateIdx = SkTMin(i + kMaxOpChainDistance, fOpChains.count() - 1);
bsalomonaecc0182016-03-07 11:50:44 -0800685 int j = i + 1;
686 while (true) {
Brian Salomon588cec72018-11-14 13:56:37 -0500687 OpChain& candidate = fOpChains[j];
688 if (candidate.prependChain(&chain, caps, fOpMemoryPool.get(), fAuditTrail)) {
bsalomonaecc0182016-03-07 11:50:44 -0800689 break;
690 }
Robert Phillipsc84c0302017-05-08 15:35:11 -0400691 // Stop traversing if we would cause a painter's order violation.
Brian Salomon588cec72018-11-14 13:56:37 -0500692 if (!can_reorder(chain.bounds(), candidate.bounds())) {
693 GrOP_INFO(
694 "\t\t%d: chain (%s head opID: %u) -> "
695 "Intersects with chain (%s, head opID: %u)\n",
696 i, chain.head()->name(), chain.head()->uniqueID(), candidate.head()->name(),
697 candidate.head()->uniqueID());
bsalomonaecc0182016-03-07 11:50:44 -0800698 break;
699 }
Brian Salomona7682c82018-10-24 10:04:37 -0400700 if (++j > maxCandidateIdx) {
Brian Salomon588cec72018-11-14 13:56:37 -0500701 GrOP_INFO("\t\t%d: chain (%s opID: %u) -> Reached max lookahead or end of array\n",
702 i, chain.head()->name(), chain.head()->uniqueID());
bsalomonaecc0182016-03-07 11:50:44 -0800703 break;
704 }
705 }
706 }
707}
708