blob: b75dd677986d6300b778ecc07e08c770b7fa4fdc [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);
Robert Phillips9548c3b422019-01-08 12:35:43 -0500180 GrOP_INFO("\t\t: (%s opID: %u) -> Combining with (%s, opID: %u)\n",
Brian Salomon588cec72018-11-14 13:56:37 -0500181 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).
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700227bool GrRenderTargetOpList::OpChain::tryConcat(
228 List* list, const DstProxy& dstProxy, const GrAppliedClip* appliedClip,
229 const GrCaps& caps, GrOpMemoryPool* pool, GrAuditTrail* auditTrail) {
230 SkASSERT(!fList.empty());
231 SkASSERT(!list->empty());
Brian Salomon588cec72018-11-14 13:56:37 -0500232 // All returns use explicit tuple constructor rather than {a, b} to work around old GCC bug.
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700233 if (fList.head()->classID() != list->head()->classID() ||
234 SkToBool(fAppliedClip) != SkToBool(appliedClip) ||
235 (fAppliedClip && *fAppliedClip != *appliedClip) ||
236 SkToBool(fDstProxy.proxy()) != SkToBool(dstProxy.proxy()) ||
237 (fDstProxy.proxy() && fDstProxy != dstProxy)) {
238 return false;
Brian Salomon588cec72018-11-14 13:56:37 -0500239 }
240 SkDEBUGCODE(bool first = true;)
241 do {
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700242 switch (fList.tail()->combineIfPossible(list->head(), caps)) {
Brian Salomon588cec72018-11-14 13:56:37 -0500243 case GrOp::CombineResult::kCannotCombine:
244 // If an op supports chaining then it is required that chaining is transitive and
245 // that if any two ops in two different chains can merge then the two chains
246 // may also be chained together. Thus, we should only hit this on the first
247 // iteration.
248 SkASSERT(first);
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700249 return false;
Brian Salomon588cec72018-11-14 13:56:37 -0500250 case GrOp::CombineResult::kMayChain:
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700251 fList = DoConcat(std::move(fList), std::move(*list), caps, pool, auditTrail);
252 return true;
Brian Salomon588cec72018-11-14 13:56:37 -0500253 case GrOp::CombineResult::kMerged: {
Robert Phillips9548c3b422019-01-08 12:35:43 -0500254 GrOP_INFO("\t\t: (%s opID: %u) -> Combining with (%s, opID: %u)\n",
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700255 list->tail()->name(), list->tail()->uniqueID(), list->head()->name(),
256 list->head()->uniqueID());
257 GR_AUDIT_TRAIL_OPS_RESULT_COMBINED(auditTrail, fList.tail(), list->head());
258 pool->release(list->popHead());
Brian Salomon588cec72018-11-14 13:56:37 -0500259 break;
260 }
261 }
262 SkDEBUGCODE(first = false);
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700263 } while (!list->empty());
Brian Salomon588cec72018-11-14 13:56:37 -0500264 // All the ops from chain b merged.
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700265 return true;
Brian Salomon588cec72018-11-14 13:56:37 -0500266}
267
268bool GrRenderTargetOpList::OpChain::prependChain(OpChain* that, const GrCaps& caps,
269 GrOpMemoryPool* pool, GrAuditTrail* auditTrail) {
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700270 if (!that->tryConcat(
271 &fList, this->dstProxy(), this->appliedClip(), caps, pool, auditTrail)) {
Brian Salomon588cec72018-11-14 13:56:37 -0500272 this->validate();
273 // append failed
274 return false;
275 }
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700276
Brian Salomon588cec72018-11-14 13:56:37 -0500277 // 'that' owns the combined chain. Move it into 'this'.
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700278 SkASSERT(fList.empty());
Brian Salomon588cec72018-11-14 13:56:37 -0500279 fList = std::move(that->fList);
280 fBounds.joinPossiblyEmptyRect(that->fBounds);
281
282 that->fDstProxy.setProxy(nullptr);
283 if (that->fAppliedClip) {
284 for (int i = 0; i < that->fAppliedClip->numClipCoverageFragmentProcessors(); ++i) {
285 that->fAppliedClip->detachClipCoverageFragmentProcessor(i);
286 }
287 }
288 this->validate();
289 return true;
290}
291
292std::unique_ptr<GrOp> GrRenderTargetOpList::OpChain::appendOp(std::unique_ptr<GrOp> op,
293 const DstProxy* dstProxy,
294 const GrAppliedClip* appliedClip,
295 const GrCaps& caps,
296 GrOpMemoryPool* pool,
297 GrAuditTrail* auditTrail) {
298 const GrXferProcessor::DstProxy noDstProxy;
299 if (!dstProxy) {
300 dstProxy = &noDstProxy;
301 }
302 SkASSERT(op->isChainHead() && op->isChainTail());
303 SkRect opBounds = op->bounds();
304 List chain(std::move(op));
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700305 if (!this->tryConcat(&chain, *dstProxy, appliedClip, caps, pool, auditTrail)) {
Brian Salomon588cec72018-11-14 13:56:37 -0500306 // append failed, give the op back to the caller.
307 this->validate();
308 return chain.popHead();
309 }
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700310
311 SkASSERT(chain.empty());
Brian Salomon588cec72018-11-14 13:56:37 -0500312 fBounds.joinPossiblyEmptyRect(opBounds);
313 this->validate();
314 return nullptr;
315}
316
317inline void GrRenderTargetOpList::OpChain::validate() const {
318#ifdef SK_DEBUG
319 fList.validate();
320 for (const auto& op : GrOp::ChainRange<>(fList.head())) {
321 // Not using SkRect::contains because we allow empty rects.
322 SkASSERT(fBounds.fLeft <= op.bounds().fLeft && fBounds.fTop <= op.bounds().fTop &&
323 fBounds.fRight >= op.bounds().fRight && fBounds.fBottom >= op.bounds().fBottom);
324 }
325#endif
326}
327
328////////////////////////////////////////////////////////////////////////////////
bsalomon489147c2015-12-14 12:13:09 -0800329
Robert Phillips3a9710b2018-03-27 17:51:55 -0400330GrRenderTargetOpList::GrRenderTargetOpList(GrResourceProvider* resourceProvider,
Robert Phillipsc994a932018-06-19 13:09:54 -0400331 sk_sp<GrOpMemoryPool> opMemoryPool,
Robert Phillips3a9710b2018-03-27 17:51:55 -0400332 GrRenderTargetProxy* proxy,
Robert Phillips8185f592017-04-26 08:31:08 -0400333 GrAuditTrail* auditTrail)
Robert Phillipsc994a932018-06-19 13:09:54 -0400334 : INHERITED(resourceProvider, std::move(opMemoryPool), proxy, auditTrail)
Brian Salomonc3833b42018-07-09 18:23:58 +0000335 , fLastClipStackGenID(SK_InvalidUniqueID)
Robert Phillipsb6deea82017-05-11 14:14:30 -0400336 SkDEBUGCODE(, fNumClips(0)) {
bsalomon4061b122015-05-29 10:26:19 -0700337}
338
Robert Phillipsc994a932018-06-19 13:09:54 -0400339void GrRenderTargetOpList::deleteOps() {
Brian Salomon588cec72018-11-14 13:56:37 -0500340 for (auto& chain : fOpChains) {
341 chain.deleteOps(fOpMemoryPool.get());
Robert Phillipsc994a932018-06-19 13:09:54 -0400342 }
Brian Salomon588cec72018-11-14 13:56:37 -0500343 fOpChains.reset();
Robert Phillipsc994a932018-06-19 13:09:54 -0400344}
345
Robert Phillipsf2361d22016-10-25 14:20:06 -0400346GrRenderTargetOpList::~GrRenderTargetOpList() {
Robert Phillipsc994a932018-06-19 13:09:54 -0400347 this->deleteOps();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000348}
349
350////////////////////////////////////////////////////////////////////////////////
351
robertphillips4beb5c12015-10-20 07:50:00 -0700352#ifdef SK_DEBUG
Robert Phillips27483912018-04-20 12:43:18 -0400353void GrRenderTargetOpList::dump(bool printDependencies) const {
354 INHERITED::dump(printDependencies);
Robert Phillipsf2361d22016-10-25 14:20:06 -0400355
Brian Salomon588cec72018-11-14 13:56:37 -0500356 SkDebugf("ops (%d):\n", fOpChains.count());
357 for (int i = 0; i < fOpChains.count(); ++i) {
robertphillips4beb5c12015-10-20 07:50:00 -0700358 SkDebugf("*******************************\n");
Brian Salomon588cec72018-11-14 13:56:37 -0500359 if (!fOpChains[i].head()) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500360 SkDebugf("%d: <combined forward or failed instantiation>\n", i);
bsalomonaecc0182016-03-07 11:50:44 -0800361 } else {
Brian Salomon588cec72018-11-14 13:56:37 -0500362 SkDebugf("%d: %s\n", i, fOpChains[i].head()->name());
363 SkRect bounds = fOpChains[i].bounds();
Brian Salomon9e50f7b2017-03-06 12:02:34 -0500364 SkDebugf("ClippedBounds: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", bounds.fLeft,
365 bounds.fTop, bounds.fRight, bounds.fBottom);
Brian Salomon588cec72018-11-14 13:56:37 -0500366 for (const auto& op : GrOp::ChainRange<>(fOpChains[i].head())) {
367 SkString info = SkTabString(op.dumpInfo(), 1);
368 SkDebugf("%s\n", info.c_str());
369 bounds = op.bounds();
370 SkDebugf("\tClippedBounds: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", bounds.fLeft,
371 bounds.fTop, bounds.fRight, bounds.fBottom);
372 }
bsalomonaecc0182016-03-07 11:50:44 -0800373 }
robertphillips4beb5c12015-10-20 07:50:00 -0700374 }
375}
Chris Dalton706a6ff2017-11-29 22:01:06 -0700376
377void GrRenderTargetOpList::visitProxies_debugOnly(const GrOp::VisitProxyFunc& func) const {
Brian Salomon588cec72018-11-14 13:56:37 -0500378 for (const OpChain& chain : fOpChains) {
379 chain.visitProxies(func, GrOp::VisitorType::kOther);
Chris Dalton706a6ff2017-11-29 22:01:06 -0700380 }
381}
Brian Salomonc525d4f2018-09-17 15:48:20 -0400382
robertphillips4beb5c12015-10-20 07:50:00 -0700383#endif
384
Brian Osman407b3422017-08-22 15:01:32 -0400385void GrRenderTargetOpList::onPrepare(GrOpFlushState* flushState) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400386 SkASSERT(fTarget.get()->peekRenderTarget());
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400387 SkASSERT(this->isClosed());
Stan Iliev2af578d2017-08-16 13:00:28 -0400388#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
389 TRACE_EVENT0("skia", TRACE_FUNC);
390#endif
robertphillipsa106c622015-10-16 09:07:06 -0700391
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500392 // Loop over the ops that haven't yet been prepared.
Brian Salomon588cec72018-11-14 13:56:37 -0500393 for (const auto& chain : fOpChains) {
394 if (chain.head()) {
Stan Iliev2af578d2017-08-16 13:00:28 -0400395#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
Brian Salomon588cec72018-11-14 13:56:37 -0500396 TRACE_EVENT0("skia", chain.head()->name());
Stan Iliev2af578d2017-08-16 13:00:28 -0400397#endif
Brian Salomon29b60c92017-10-31 14:42:10 -0400398 GrOpFlushState::OpArgs opArgs = {
Brian Salomon588cec72018-11-14 13:56:37 -0500399 chain.head(),
Robert Phillips2890fbf2017-07-26 15:48:41 -0400400 fTarget.get()->asRenderTargetProxy(),
Brian Salomon588cec72018-11-14 13:56:37 -0500401 chain.appliedClip(),
402 chain.dstProxy()
Robert Phillips318c4192017-05-17 09:36:38 -0400403 };
Brian Salomon29b60c92017-10-31 14:42:10 -0400404 flushState->setOpArgs(&opArgs);
Brian Salomon588cec72018-11-14 13:56:37 -0500405 chain.head()->prepare(flushState);
Brian Salomon29b60c92017-10-31 14:42:10 -0400406 flushState->setOpArgs(nullptr);
bsalomonaecc0182016-03-07 11:50:44 -0800407 }
bsalomon512be532015-09-10 10:42:55 -0700408 }
robertphillipsa13e2022015-11-11 12:01:09 -0800409}
bsalomon512be532015-09-10 10:42:55 -0700410
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400411static GrGpuRTCommandBuffer* create_command_buffer(GrGpu* gpu,
412 GrRenderTarget* rt,
413 GrSurfaceOrigin origin,
Ethan Nicholas56d19a52018-10-15 11:26:20 -0400414 const SkRect& bounds,
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400415 GrLoadOp colorLoadOp,
Brian Osman9a9baae2018-11-05 15:06:26 -0500416 const SkPMColor4f& loadClearColor,
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400417 GrLoadOp stencilLoadOp) {
Robert Phillipscb2e2352017-08-30 16:44:40 -0400418 const GrGpuRTCommandBuffer::LoadAndStoreInfo kColorLoadStoreInfo {
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400419 colorLoadOp,
420 GrStoreOp::kStore,
421 loadClearColor
Robert Phillips178ce3e2017-04-13 09:15:47 -0400422 };
423
Robert Phillips95214472017-08-08 18:00:03 -0400424 // TODO:
425 // We would like to (at this level) only ever clear & discard. We would need
426 // to stop splitting up higher level opLists for copyOps to achieve that.
427 // Note: we would still need SB loads and stores but they would happen at a
428 // lower level (inside the VK command buffer).
Greg Daniel500d58b2017-08-24 15:59:33 -0400429 const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo stencilLoadAndStoreInfo {
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400430 stencilLoadOp,
431 GrStoreOp::kStore,
Robert Phillips95214472017-08-08 18:00:03 -0400432 };
433
Ethan Nicholas56d19a52018-10-15 11:26:20 -0400434 return gpu->getCommandBuffer(rt, origin, bounds, kColorLoadStoreInfo, stencilLoadAndStoreInfo);
Robert Phillips178ce3e2017-04-13 09:15:47 -0400435}
436
Brian Salomon25a88092016-12-01 09:36:50 -0500437// TODO: this is where GrOp::renderTarget is used (which is fine since it
Robert Phillips294870f2016-11-11 12:38:40 -0500438// is at flush time). However, we need to store the RenderTargetProxy in the
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500439// Ops and instantiate them here.
Brian Osman407b3422017-08-22 15:01:32 -0400440bool GrRenderTargetOpList::onExecute(GrOpFlushState* flushState) {
Greg Danieldbdba602018-04-20 11:52:43 -0400441 // TODO: Forcing the execution of the discard here isn't ideal since it will cause us to do a
442 // discard and then store the data back in memory so that the load op on future draws doesn't
443 // think the memory is unitialized. Ideally we would want a system where we are tracking whether
444 // the proxy itself has valid data or not, and then use that as a signal on whether we should be
445 // loading or discarding. In that world we wouldni;t need to worry about executing oplists with
446 // no ops just to do a discard.
Brian Salomon588cec72018-11-14 13:56:37 -0500447 if (fOpChains.empty() && GrLoadOp::kClear != fColorLoadOp &&
Greg Danieldbdba602018-04-20 11:52:43 -0400448 GrLoadOp::kDiscard != fColorLoadOp) {
bsalomondc438982016-08-31 11:53:49 -0700449 return false;
egdanielb4021cf2016-07-28 08:53:07 -0700450 }
Robert Phillips4a395042017-04-24 16:27:17 +0000451
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400452 SkASSERT(fTarget.get()->peekRenderTarget());
Stan Iliev2af578d2017-08-16 13:00:28 -0400453 TRACE_EVENT0("skia", TRACE_FUNC);
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400454
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400455 // TODO: at the very least, we want the stencil store op to always be discard (at this
456 // level). In Vulkan, sub-command buffers would still need to load & store the stencil buffer.
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500457
458 // Make sure load ops are not kClear if the GPU needs to use draws for clears
459 SkASSERT(fColorLoadOp != GrLoadOp::kClear ||
460 !flushState->gpu()->caps()->performColorClearsAsDraws());
461 SkASSERT(fStencilLoadOp != GrLoadOp::kClear ||
462 !flushState->gpu()->caps()->performStencilClearsAsDraws());
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400463 GrGpuRTCommandBuffer* commandBuffer = create_command_buffer(
Robert Phillips95214472017-08-08 18:00:03 -0400464 flushState->gpu(),
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400465 fTarget.get()->peekRenderTarget(),
Robert Phillips95214472017-08-08 18:00:03 -0400466 fTarget.get()->origin(),
Ethan Nicholas56d19a52018-10-15 11:26:20 -0400467 fTarget.get()->getBoundsRect(),
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400468 fColorLoadOp,
469 fLoadClearColor,
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400470 fStencilLoadOp);
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400471 flushState->setCommandBuffer(commandBuffer);
Robert Phillips95214472017-08-08 18:00:03 -0400472 commandBuffer->begin();
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400473
474 // Draw all the generated geometry.
Brian Salomon588cec72018-11-14 13:56:37 -0500475 for (const auto& chain : fOpChains) {
476 if (!chain.head()) {
bsalomonaecc0182016-03-07 11:50:44 -0800477 continue;
478 }
Stan Iliev2af578d2017-08-16 13:00:28 -0400479#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
Brian Salomon588cec72018-11-14 13:56:37 -0500480 TRACE_EVENT0("skia", chain.head()->name());
Stan Iliev2af578d2017-08-16 13:00:28 -0400481#endif
Robert Phillips178ce3e2017-04-13 09:15:47 -0400482
Brian Salomon29b60c92017-10-31 14:42:10 -0400483 GrOpFlushState::OpArgs opArgs {
Brian Salomon588cec72018-11-14 13:56:37 -0500484 chain.head(),
Robert Phillips2890fbf2017-07-26 15:48:41 -0400485 fTarget.get()->asRenderTargetProxy(),
Brian Salomon588cec72018-11-14 13:56:37 -0500486 chain.appliedClip(),
487 chain.dstProxy()
Robert Phillips178ce3e2017-04-13 09:15:47 -0400488 };
489
Brian Salomon29b60c92017-10-31 14:42:10 -0400490 flushState->setOpArgs(&opArgs);
Brian Salomon588cec72018-11-14 13:56:37 -0500491 chain.head()->execute(flushState, chain.bounds());
Brian Salomon29b60c92017-10-31 14:42:10 -0400492 flushState->setOpArgs(nullptr);
bsalomon512be532015-09-10 10:42:55 -0700493 }
Robert Phillips178ce3e2017-04-13 09:15:47 -0400494
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400495 commandBuffer->end();
496 flushState->gpu()->submit(commandBuffer);
Robert Phillips178ce3e2017-04-13 09:15:47 -0400497 flushState->setCommandBuffer(nullptr);
ethannicholas22793252016-01-30 09:59:10 -0800498
bsalomondc438982016-08-31 11:53:49 -0700499 return true;
bsalomona73239a2015-04-28 13:35:17 -0700500}
501
Chris Daltona84cacf2017-10-04 10:30:29 -0600502void GrRenderTargetOpList::endFlush() {
Brian Salomonc3833b42018-07-09 18:23:58 +0000503 fLastClipStackGenID = SK_InvalidUniqueID;
Robert Phillipsc994a932018-06-19 13:09:54 -0400504 this->deleteOps();
Chris Daltonc82dd4e2017-11-20 18:20:28 -0700505 fClipAllocator.reset();
Chris Daltona84cacf2017-10-04 10:30:29 -0600506 INHERITED::endFlush();
bsalomon512be532015-09-10 10:42:55 -0700507}
508
Robert Phillips380b90c2017-08-30 07:41:07 -0400509void GrRenderTargetOpList::discard() {
510 // Discard calls to in-progress opLists are ignored. Calls at the start update the
511 // opLists' color & stencil load ops.
512 if (this->isEmpty()) {
513 fColorLoadOp = GrLoadOp::kDiscard;
514 fStencilLoadOp = GrLoadOp::kDiscard;
515 }
516}
517
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500518void GrRenderTargetOpList::setStencilLoadOp(GrLoadOp op) {
519 fStencilLoadOp = op;
520}
Robert Phillips380b90c2017-08-30 07:41:07 -0400521
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500522void GrRenderTargetOpList::setColorLoadOp(GrLoadOp op, const SkPMColor4f& color) {
523 fColorLoadOp = op;
524 fLoadClearColor = color;
525}
526
527bool GrRenderTargetOpList::resetForFullscreenClear() {
528 // Mark the color load op as discard (this may be followed by a clearColorOnLoad call to make
529 // the load op kClear, or it may be followed by an explicit op). In the event of an absClear()
530 // after a regular clear(), we could end up with a clear load op and a real clear op in the list
531 // if the load op were not reset here.
532 fColorLoadOp = GrLoadOp::kDiscard;
533
534 // Regardless of how the clear is implemented (native clear or a fullscreen quad), all prior ops
535 // would be overwritten, so discard them entirely. The one exception is if the opList is marked
536 // as needing a stencil buffer then there may be a prior op that writes to the stencil buffer.
537 // Although the clear will ignore the stencil buffer, following draw ops may not so we can't get
538 // rid of all the preceding ops. Beware! If we ever add any ops that have a side effect beyond
539 // modifying the stencil buffer we will need a more elaborate tracking system (skbug.com/7002).
Robert Phillips380b90c2017-08-30 07:41:07 -0400540 if (this->isEmpty() || !fTarget.get()->asRenderTargetProxy()->needsStencil()) {
Robert Phillipsc994a932018-06-19 13:09:54 -0400541 this->deleteOps();
Brian Osman099fa0f2017-10-02 16:38:32 -0400542 fDeferredProxies.reset();
Greg Daniel070cbaf2019-01-03 17:35:54 -0500543
544 // If the opList is using a render target which wraps a vulkan command buffer, we can't do a
545 // clear load since we cannot change the render pass that we are using. Thus we fall back to
546 // making a clear op in this case.
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500547 return !fTarget.get()->asRenderTargetProxy()->wrapsVkSecondaryCB();
bsalomonfd8d0132016-08-11 11:25:33 -0700548 }
Robert Phillips380b90c2017-08-30 07:41:07 -0400549
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500550 // Could not empty the list, so an op must be added to handle the clear
551 return false;
bsalomon9f129de2016-08-10 16:31:05 -0700552}
553
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000554////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000555
Robert Phillips81dd3e02017-06-23 11:59:24 -0400556// This closely parallels GrTextureOpList::copySurface but renderTargetOpLists
557// also store the applied clip and dest proxy with the op
Robert Phillips7c525e62018-06-12 10:11:12 -0400558bool GrRenderTargetOpList::copySurface(GrContext* context,
Robert Phillipsa16f6cb2017-06-01 11:06:13 -0400559 GrSurfaceProxy* dst,
Robert Phillipsbf25d432017-04-07 10:08:53 -0400560 GrSurfaceProxy* src,
Robert Phillipsf2361d22016-10-25 14:20:06 -0400561 const SkIRect& srcRect,
562 const SkIPoint& dstPoint) {
Robert Phillips5efd5ea2017-05-30 13:47:32 -0400563 SkASSERT(dst->asRenderTargetProxy() == fTarget.get());
Robert Phillips7c525e62018-06-12 10:11:12 -0400564 std::unique_ptr<GrOp> op = GrCopySurfaceOp::Make(context, dst, src, srcRect, dstPoint);
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500565 if (!op) {
bsalomonb8fea972016-02-16 07:34:17 -0800566 return false;
567 }
robertphillips498d7ac2015-10-30 10:11:30 -0700568
Robert Phillips7c525e62018-06-12 10:11:12 -0400569 this->addOp(std::move(op), *context->contextPriv().caps());
bsalomonb8fea972016-02-16 07:34:17 -0800570 return true;
bsalomon@google.comeb851172013-04-15 13:51:00 +0000571}
572
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500573void GrRenderTargetOpList::purgeOpsWithUninstantiatedProxies() {
574 bool hasUninstantiatedProxy = false;
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400575 auto checkInstantiation = [&hasUninstantiatedProxy](GrSurfaceProxy* p) {
576 if (!p->isInstantiated()) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500577 hasUninstantiatedProxy = true;
578 }
579 };
Brian Salomon588cec72018-11-14 13:56:37 -0500580 for (OpChain& recordedOp : fOpChains) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500581 hasUninstantiatedProxy = false;
Brian Salomon588cec72018-11-14 13:56:37 -0500582 recordedOp.visitProxies(checkInstantiation, GrOp::VisitorType::kOther);
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500583 if (hasUninstantiatedProxy) {
584 // When instantiation of the proxy fails we drop the Op
Brian Salomon588cec72018-11-14 13:56:37 -0500585 recordedOp.deleteOps(fOpMemoryPool.get());
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500586 }
587 }
588}
589
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400590void GrRenderTargetOpList::gatherProxyIntervals(GrResourceAllocator* alloc) const {
591 unsigned int cur = alloc->numOps();
592
Robert Phillips51b20f22017-12-01 15:32:35 -0500593 for (int i = 0; i < fDeferredProxies.count(); ++i) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400594 SkASSERT(!fDeferredProxies[i]->isInstantiated());
Robert Phillips51b20f22017-12-01 15:32:35 -0500595 // We give all the deferred proxies a write usage at the very start of flushing. This
596 // locks them out of being reused for the entire flush until they are read - and then
597 // they can be recycled. This is a bit unfortunate because a flush can proceed in waves
598 // with sub-flushes. The deferred proxies only need to be pinned from the start of
599 // the sub-flush in which they appear.
600 alloc->addInterval(fDeferredProxies[i], 0, 0);
601 }
602
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400603 // Add the interval for all the writes to this opList's target
Brian Salomon588cec72018-11-14 13:56:37 -0500604 if (fOpChains.count()) {
605 alloc->addInterval(fTarget.get(), cur, cur + fOpChains.count() - 1);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500606 } else {
607 // This can happen if there is a loadOp (e.g., a clear) but no other draws. In this case we
608 // still need to add an interval for the destination so we create a fake op# for
609 // the missing clear op.
610 alloc->addInterval(fTarget.get());
611 alloc->incOps();
612 }
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400613
Chris Dalton8816b932017-11-29 16:48:25 -0700614 auto gather = [ alloc SkDEBUGCODE(, this) ] (GrSurfaceProxy* p) {
615 alloc->addInterval(p SkDEBUGCODE(, fTarget.get() == p));
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400616 };
Brian Salomon588cec72018-11-14 13:56:37 -0500617 for (const OpChain& recordedOp : fOpChains) {
Brian Salomon7d94bb52018-10-12 14:37:19 -0400618 // only diff from the GrTextureOpList version
619 recordedOp.visitProxies(gather, GrOp::VisitorType::kAllocatorGather);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500620
621 // Even though the op may have been moved we still need to increment the op count to
622 // keep all the math consistent.
623 alloc->incOps();
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400624 }
625}
626
Brian Salomon348a0372018-10-31 10:42:18 -0400627void GrRenderTargetOpList::recordOp(std::unique_ptr<GrOp> op,
628 const GrCaps& caps,
629 GrAppliedClip* clip,
630 const DstProxy* dstProxy) {
Ethan Nicholas029b22c2018-10-18 16:49:56 -0400631 SkDEBUGCODE(op->validate();)
Robert Phillips318c4192017-05-17 09:36:38 -0400632 SkASSERT(fTarget.get());
Robert Phillipsee683652017-04-26 11:53:10 -0400633
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500634 // A closed GrOpList should never receive new/more ops
robertphillips6a186652015-10-20 07:37:58 -0700635 SkASSERT(!this->isClosed());
Brian Salomon19ec80f2018-11-16 13:27:30 -0500636 if (!op->bounds().isFinite()) {
637 fOpMemoryPool->release(std::move(op));
638 return;
639 }
robertphillipsa106c622015-10-16 09:07:06 -0700640
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500641 // Check if there is an op we can combine with by linearly searching back until we either
642 // 1) check every op
bsalomon512be532015-09-10 10:42:55 -0700643 // 2) intersect with something
644 // 3) find a 'blocker'
Robert Phillips5efd5ea2017-05-30 13:47:32 -0400645 GR_AUDIT_TRAIL_ADD_OP(fAuditTrail, op.get(), fTarget.get()->uniqueID());
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400646 GrOP_INFO("opList: %d Recording (%s, opID: %u)\n"
647 "\tBounds [L: %.2f, T: %.2f R: %.2f B: %.2f]\n",
648 this->uniqueID(),
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500649 op->name(),
650 op->uniqueID(),
Robert Phillips1119dc32017-04-11 12:54:57 -0400651 op->bounds().fLeft, op->bounds().fTop,
652 op->bounds().fRight, op->bounds().fBottom);
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500653 GrOP_INFO(SkTabString(op->dumpInfo(), 1).c_str());
Brian Salomon25a88092016-12-01 09:36:50 -0500654 GrOP_INFO("\tOutcome:\n");
Brian Salomon588cec72018-11-14 13:56:37 -0500655 int maxCandidates = SkTMin(kMaxOpChainDistance, fOpChains.count());
Robert Phillips318c4192017-05-17 09:36:38 -0400656 if (maxCandidates) {
bsalomon512be532015-09-10 10:42:55 -0700657 int i = 0;
658 while (true) {
Brian Salomon588cec72018-11-14 13:56:37 -0500659 OpChain& candidate = fOpChains.fromBack(i);
660 op = candidate.appendOp(std::move(op), dstProxy, clip, caps, fOpMemoryPool.get(),
661 fAuditTrail);
662 if (!op) {
663 return;
bsalomon512be532015-09-10 10:42:55 -0700664 }
Brian Salomona7682c82018-10-24 10:04:37 -0400665 // Stop going backwards if we would cause a painter's order violation.
Brian Salomon588cec72018-11-14 13:56:37 -0500666 if (!can_reorder(candidate.bounds(), op->bounds())) {
667 GrOP_INFO("\t\tBackward: Intersects with chain (%s, head opID: %u)\n",
668 candidate.head()->name(), candidate.head()->uniqueID());
bsalomon512be532015-09-10 10:42:55 -0700669 break;
670 }
Brian Salomon588cec72018-11-14 13:56:37 -0500671 if (++i == maxCandidates) {
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400672 GrOP_INFO("\t\tBackward: Reached max lookback or beginning of op array %d\n", i);
bsalomon512be532015-09-10 10:42:55 -0700673 break;
674 }
675 }
676 } else {
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400677 GrOP_INFO("\t\tBackward: FirstOp\n");
bsalomon512be532015-09-10 10:42:55 -0700678 }
Brian Salomon54d212e2017-03-21 14:22:38 -0400679 if (clip) {
680 clip = fClipAllocator.make<GrAppliedClip>(std::move(*clip));
Robert Phillipsc84c0302017-05-08 15:35:11 -0400681 SkDEBUGCODE(fNumClips++;)
Brian Salomon54d212e2017-03-21 14:22:38 -0400682 }
Brian Salomon588cec72018-11-14 13:56:37 -0500683 fOpChains.emplace_back(std::move(op), clip, dstProxy);
bsalomon512be532015-09-10 10:42:55 -0700684}
685
Robert Phillipsee683652017-04-26 11:53:10 -0400686void GrRenderTargetOpList::forwardCombine(const GrCaps& caps) {
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400687 SkASSERT(!this->isClosed());
Brian Salomon588cec72018-11-14 13:56:37 -0500688 GrOP_INFO("opList: %d ForwardCombine %d ops:\n", this->uniqueID(), fOpChains.count());
Robert Phillips48567ac2017-06-01 08:46:00 -0400689
Brian Salomon588cec72018-11-14 13:56:37 -0500690 for (int i = 0; i < fOpChains.count() - 1; ++i) {
691 OpChain& chain = fOpChains[i];
692 int maxCandidateIdx = SkTMin(i + kMaxOpChainDistance, fOpChains.count() - 1);
bsalomonaecc0182016-03-07 11:50:44 -0800693 int j = i + 1;
694 while (true) {
Brian Salomon588cec72018-11-14 13:56:37 -0500695 OpChain& candidate = fOpChains[j];
696 if (candidate.prependChain(&chain, caps, fOpMemoryPool.get(), fAuditTrail)) {
bsalomonaecc0182016-03-07 11:50:44 -0800697 break;
698 }
Robert Phillipsc84c0302017-05-08 15:35:11 -0400699 // Stop traversing if we would cause a painter's order violation.
Brian Salomon588cec72018-11-14 13:56:37 -0500700 if (!can_reorder(chain.bounds(), candidate.bounds())) {
701 GrOP_INFO(
702 "\t\t%d: chain (%s head opID: %u) -> "
703 "Intersects with chain (%s, head opID: %u)\n",
704 i, chain.head()->name(), chain.head()->uniqueID(), candidate.head()->name(),
705 candidate.head()->uniqueID());
bsalomonaecc0182016-03-07 11:50:44 -0800706 break;
707 }
Brian Salomona7682c82018-10-24 10:04:37 -0400708 if (++j > maxCandidateIdx) {
Brian Salomon588cec72018-11-14 13:56:37 -0500709 GrOP_INFO("\t\t%d: chain (%s opID: %u) -> Reached max lookahead or end of array\n",
710 i, chain.head()->name(), chain.head()->uniqueID());
bsalomonaecc0182016-03-07 11:50:44 -0800711 break;
712 }
713 }
714 }
715}
716