1 | /* |
2 | * Copyright 2019 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. |
6 | */ |
7 | |
8 | #include "src/gpu/GrOpsTask.h" |
9 | |
10 | #include "include/gpu/GrRecordingContext.h" |
11 | #include "src/core/SkRectPriv.h" |
12 | #include "src/core/SkScopeExit.h" |
13 | #include "src/core/SkTraceEvent.h" |
14 | #include "src/gpu/GrAuditTrail.h" |
15 | #include "src/gpu/GrCaps.h" |
16 | #include "src/gpu/GrGpu.h" |
17 | #include "src/gpu/GrMemoryPool.h" |
18 | #include "src/gpu/GrOpFlushState.h" |
19 | #include "src/gpu/GrOpsRenderPass.h" |
20 | #include "src/gpu/GrRecordingContextPriv.h" |
21 | #include "src/gpu/GrRenderTarget.h" |
22 | #include "src/gpu/GrRenderTargetContext.h" |
23 | #include "src/gpu/GrResourceAllocator.h" |
24 | #include "src/gpu/GrStencilAttachment.h" |
25 | #include "src/gpu/GrTexture.h" |
26 | #include "src/gpu/geometry/GrRect.h" |
27 | #include "src/gpu/ops/GrClearOp.h" |
28 | |
29 | //////////////////////////////////////////////////////////////////////////////// |
30 | |
31 | // Experimentally we have found that most combining occurs within the first 10 comparisons. |
32 | static const int kMaxOpMergeDistance = 10; |
33 | static const int kMaxOpChainDistance = 10; |
34 | |
35 | //////////////////////////////////////////////////////////////////////////////// |
36 | |
37 | using DstProxyView = GrXferProcessor::DstProxyView; |
38 | |
39 | //////////////////////////////////////////////////////////////////////////////// |
40 | |
41 | GrOpsTaskClosedObserver::~GrOpsTaskClosedObserver() = default; |
42 | |
43 | //////////////////////////////////////////////////////////////////////////////// |
44 | |
45 | static inline bool can_reorder(const SkRect& a, const SkRect& b) { return !GrRectsOverlap(a, b); } |
46 | |
47 | //////////////////////////////////////////////////////////////////////////////// |
48 | |
49 | inline GrOpsTask::OpChain::List::List(std::unique_ptr<GrOp> op) |
50 | : fHead(std::move(op)), fTail(fHead.get()) { |
51 | this->validate(); |
52 | } |
53 | |
54 | inline GrOpsTask::OpChain::List::List(List&& that) { *this = std::move(that); } |
55 | |
56 | inline GrOpsTask::OpChain::List& GrOpsTask::OpChain::List::operator=(List&& that) { |
57 | fHead = std::move(that.fHead); |
58 | fTail = that.fTail; |
59 | that.fTail = nullptr; |
60 | this->validate(); |
61 | return *this; |
62 | } |
63 | |
64 | inline std::unique_ptr<GrOp> GrOpsTask::OpChain::List::popHead() { |
65 | SkASSERT(fHead); |
66 | auto temp = fHead->cutChain(); |
67 | std::swap(temp, fHead); |
68 | if (!fHead) { |
69 | SkASSERT(fTail == temp.get()); |
70 | fTail = nullptr; |
71 | } |
72 | return temp; |
73 | } |
74 | |
75 | inline std::unique_ptr<GrOp> GrOpsTask::OpChain::List::removeOp(GrOp* op) { |
76 | #ifdef SK_DEBUG |
77 | auto head = op; |
78 | while (head->prevInChain()) { head = head->prevInChain(); } |
79 | SkASSERT(head == fHead.get()); |
80 | #endif |
81 | auto prev = op->prevInChain(); |
82 | if (!prev) { |
83 | SkASSERT(op == fHead.get()); |
84 | return this->popHead(); |
85 | } |
86 | auto temp = prev->cutChain(); |
87 | if (auto next = temp->cutChain()) { |
88 | prev->chainConcat(std::move(next)); |
89 | } else { |
90 | SkASSERT(fTail == op); |
91 | fTail = prev; |
92 | } |
93 | this->validate(); |
94 | return temp; |
95 | } |
96 | |
97 | inline void GrOpsTask::OpChain::List::pushHead(std::unique_ptr<GrOp> op) { |
98 | SkASSERT(op); |
99 | SkASSERT(op->isChainHead()); |
100 | SkASSERT(op->isChainTail()); |
101 | if (fHead) { |
102 | op->chainConcat(std::move(fHead)); |
103 | fHead = std::move(op); |
104 | } else { |
105 | fHead = std::move(op); |
106 | fTail = fHead.get(); |
107 | } |
108 | } |
109 | |
110 | inline void GrOpsTask::OpChain::List::pushTail(std::unique_ptr<GrOp> op) { |
111 | SkASSERT(op->isChainTail()); |
112 | fTail->chainConcat(std::move(op)); |
113 | fTail = fTail->nextInChain(); |
114 | } |
115 | |
116 | inline void GrOpsTask::OpChain::List::validate() const { |
117 | #ifdef SK_DEBUG |
118 | if (fHead) { |
119 | SkASSERT(fTail); |
120 | fHead->validateChain(fTail); |
121 | } |
122 | #endif |
123 | } |
124 | |
125 | //////////////////////////////////////////////////////////////////////////////// |
126 | |
127 | GrOpsTask::OpChain::OpChain(std::unique_ptr<GrOp> op, |
128 | GrProcessorSet::Analysis processorAnalysis, |
129 | GrAppliedClip* appliedClip, const DstProxyView* dstProxyView) |
130 | : fList{std::move(op)} |
131 | , fProcessorAnalysis(processorAnalysis) |
132 | , fAppliedClip(appliedClip) { |
133 | if (fProcessorAnalysis.requiresDstTexture()) { |
134 | SkASSERT(dstProxyView && dstProxyView->proxy()); |
135 | fDstProxyView = *dstProxyView; |
136 | } |
137 | fBounds = fList.head()->bounds(); |
138 | } |
139 | |
140 | void GrOpsTask::OpChain::visitProxies(const GrOp::VisitProxyFunc& func) const { |
141 | if (fList.empty()) { |
142 | return; |
143 | } |
144 | for (const auto& op : GrOp::ChainRange<>(fList.head())) { |
145 | op.visitProxies(func); |
146 | } |
147 | if (fDstProxyView.proxy()) { |
148 | func(fDstProxyView.proxy(), GrMipmapped::kNo); |
149 | } |
150 | if (fAppliedClip) { |
151 | fAppliedClip->visitProxies(func); |
152 | } |
153 | } |
154 | |
155 | void GrOpsTask::OpChain::deleteOps(GrOpMemoryPool* pool) { |
156 | while (!fList.empty()) { |
157 | pool->release(fList.popHead()); |
158 | } |
159 | } |
160 | |
161 | // Concatenates two op chains and attempts to merge ops across the chains. Assumes that we know that |
162 | // the two chains are chainable. Returns the new chain. |
163 | GrOpsTask::OpChain::List GrOpsTask::OpChain::DoConcat( |
164 | List chainA, List chainB, const GrCaps& caps, GrRecordingContext::Arenas* arenas, |
165 | GrAuditTrail* auditTrail) { |
166 | // We process ops in chain b from head to tail. We attempt to merge with nodes in a, starting |
167 | // at chain a's tail and working toward the head. We produce one of the following outcomes: |
168 | // 1) b's head is merged into an op in a. |
169 | // 2) An op from chain a is merged into b's head. (In this case b's head gets processed again.) |
170 | // 3) b's head is popped from chain a and added at the tail of a. |
171 | // After result 3 we don't want to attempt to merge the next head of b with the new tail of a, |
172 | // as we assume merges were already attempted when chain b was created. So we keep track of the |
173 | // original tail of a and start our iteration of a there. We also track the bounds of the nodes |
174 | // appended to chain a that will be skipped for bounds testing. If the original tail of a is |
175 | // merged into an op in b (case 2) then we advance the "original tail" towards the head of a. |
176 | GrOp* origATail = chainA.tail(); |
177 | SkRect skipBounds = SkRectPriv::MakeLargestInverted(); |
178 | do { |
179 | int numMergeChecks = 0; |
180 | bool merged = false; |
181 | bool noSkip = (origATail == chainA.tail()); |
182 | SkASSERT(noSkip == (skipBounds == SkRectPriv::MakeLargestInverted())); |
183 | bool canBackwardMerge = noSkip || can_reorder(chainB.head()->bounds(), skipBounds); |
184 | SkRect forwardMergeBounds = skipBounds; |
185 | GrOp* a = origATail; |
186 | while (a) { |
187 | bool canForwardMerge = |
188 | (a == chainA.tail()) || can_reorder(a->bounds(), forwardMergeBounds); |
189 | if (canForwardMerge || canBackwardMerge) { |
190 | auto result = a->combineIfPossible(chainB.head(), arenas, caps); |
191 | SkASSERT(result != GrOp::CombineResult::kCannotCombine); |
192 | merged = (result == GrOp::CombineResult::kMerged); |
193 | GrOP_INFO("\t\t: (%s opID: %u) -> Combining with (%s, opID: %u)\n" , |
194 | chainB.head()->name(), chainB.head()->uniqueID(), a->name(), |
195 | a->uniqueID()); |
196 | } |
197 | if (merged) { |
198 | GR_AUDIT_TRAIL_OPS_RESULT_COMBINED(auditTrail, a, chainB.head()); |
199 | if (canBackwardMerge) { |
200 | arenas->opMemoryPool()->release(chainB.popHead()); |
201 | } else { |
202 | // We merged the contents of b's head into a. We will replace b's head with a in |
203 | // chain b. |
204 | SkASSERT(canForwardMerge); |
205 | if (a == origATail) { |
206 | origATail = a->prevInChain(); |
207 | } |
208 | std::unique_ptr<GrOp> detachedA = chainA.removeOp(a); |
209 | arenas->opMemoryPool()->release(chainB.popHead()); |
210 | chainB.pushHead(std::move(detachedA)); |
211 | if (chainA.empty()) { |
212 | // We merged all the nodes in chain a to chain b. |
213 | return chainB; |
214 | } |
215 | } |
216 | break; |
217 | } else { |
218 | if (++numMergeChecks == kMaxOpMergeDistance) { |
219 | break; |
220 | } |
221 | forwardMergeBounds.joinNonEmptyArg(a->bounds()); |
222 | canBackwardMerge = |
223 | canBackwardMerge && can_reorder(chainB.head()->bounds(), a->bounds()); |
224 | a = a->prevInChain(); |
225 | } |
226 | } |
227 | // If we weren't able to merge b's head then pop b's head from chain b and make it the new |
228 | // tail of a. |
229 | if (!merged) { |
230 | chainA.pushTail(chainB.popHead()); |
231 | skipBounds.joinNonEmptyArg(chainA.tail()->bounds()); |
232 | } |
233 | } while (!chainB.empty()); |
234 | return chainA; |
235 | } |
236 | |
237 | // Attempts to concatenate the given chain onto our own and merge ops across the chains. Returns |
238 | // whether the operation succeeded. On success, the provided list will be returned empty. |
239 | bool GrOpsTask::OpChain::tryConcat( |
240 | List* list, GrProcessorSet::Analysis processorAnalysis, const DstProxyView& dstProxyView, |
241 | const GrAppliedClip* appliedClip, const SkRect& bounds, const GrCaps& caps, |
242 | GrRecordingContext::Arenas* arenas, GrAuditTrail* auditTrail) { |
243 | SkASSERT(!fList.empty()); |
244 | SkASSERT(!list->empty()); |
245 | SkASSERT(fProcessorAnalysis.requiresDstTexture() == SkToBool(fDstProxyView.proxy())); |
246 | SkASSERT(processorAnalysis.requiresDstTexture() == SkToBool(dstProxyView.proxy())); |
247 | // All returns use explicit tuple constructor rather than {a, b} to work around old GCC bug. |
248 | if (fList.head()->classID() != list->head()->classID() || |
249 | SkToBool(fAppliedClip) != SkToBool(appliedClip) || |
250 | (fAppliedClip && *fAppliedClip != *appliedClip) || |
251 | (fProcessorAnalysis.requiresNonOverlappingDraws() != |
252 | processorAnalysis.requiresNonOverlappingDraws()) || |
253 | (fProcessorAnalysis.requiresNonOverlappingDraws() && |
254 | // Non-overlaping draws are only required when Ganesh will either insert a barrier, |
255 | // or read back a new dst texture between draws. In either case, we can neither |
256 | // chain nor combine overlapping Ops. |
257 | GrRectsTouchOrOverlap(fBounds, bounds)) || |
258 | (fProcessorAnalysis.requiresDstTexture() != processorAnalysis.requiresDstTexture()) || |
259 | (fProcessorAnalysis.requiresDstTexture() && fDstProxyView != dstProxyView)) { |
260 | return false; |
261 | } |
262 | |
263 | SkDEBUGCODE(bool first = true;) |
264 | do { |
265 | switch (fList.tail()->combineIfPossible(list->head(), arenas, caps)) { |
266 | case GrOp::CombineResult::kCannotCombine: |
267 | // If an op supports chaining then it is required that chaining is transitive and |
268 | // that if any two ops in two different chains can merge then the two chains |
269 | // may also be chained together. Thus, we should only hit this on the first |
270 | // iteration. |
271 | SkASSERT(first); |
272 | return false; |
273 | case GrOp::CombineResult::kMayChain: |
274 | fList = DoConcat(std::move(fList), std::exchange(*list, List()), caps, arenas, |
275 | auditTrail); |
276 | // The above exchange cleared out 'list'. The list needs to be empty now for the |
277 | // loop to terminate. |
278 | SkASSERT(list->empty()); |
279 | break; |
280 | case GrOp::CombineResult::kMerged: { |
281 | GrOP_INFO("\t\t: (%s opID: %u) -> Combining with (%s, opID: %u)\n" , |
282 | list->tail()->name(), list->tail()->uniqueID(), list->head()->name(), |
283 | list->head()->uniqueID()); |
284 | GR_AUDIT_TRAIL_OPS_RESULT_COMBINED(auditTrail, fList.tail(), list->head()); |
285 | arenas->opMemoryPool()->release(list->popHead()); |
286 | break; |
287 | } |
288 | } |
289 | SkDEBUGCODE(first = false); |
290 | } while (!list->empty()); |
291 | |
292 | // The new ops were successfully merged and/or chained onto our own. |
293 | fBounds.joinPossiblyEmptyRect(bounds); |
294 | return true; |
295 | } |
296 | |
297 | bool GrOpsTask::OpChain::prependChain(OpChain* that, const GrCaps& caps, |
298 | GrRecordingContext::Arenas* arenas, |
299 | GrAuditTrail* auditTrail) { |
300 | if (!that->tryConcat(&fList, fProcessorAnalysis, fDstProxyView, fAppliedClip, fBounds, caps, |
301 | arenas, auditTrail)) { |
302 | this->validate(); |
303 | // append failed |
304 | return false; |
305 | } |
306 | |
307 | // 'that' owns the combined chain. Move it into 'this'. |
308 | SkASSERT(fList.empty()); |
309 | fList = std::move(that->fList); |
310 | fBounds = that->fBounds; |
311 | |
312 | that->fDstProxyView.setProxyView({}); |
313 | if (that->fAppliedClip && that->fAppliedClip->hasCoverageFragmentProcessor()) { |
314 | // Obliterates the processor. |
315 | that->fAppliedClip->detachCoverageFragmentProcessor(); |
316 | } |
317 | this->validate(); |
318 | return true; |
319 | } |
320 | |
321 | std::unique_ptr<GrOp> GrOpsTask::OpChain::appendOp( |
322 | std::unique_ptr<GrOp> op, GrProcessorSet::Analysis processorAnalysis, |
323 | const DstProxyView* dstProxyView, const GrAppliedClip* appliedClip, const GrCaps& caps, |
324 | GrRecordingContext::Arenas* arenas, GrAuditTrail* auditTrail) { |
325 | const GrXferProcessor::DstProxyView noDstProxyView; |
326 | if (!dstProxyView) { |
327 | dstProxyView = &noDstProxyView; |
328 | } |
329 | SkASSERT(op->isChainHead() && op->isChainTail()); |
330 | SkRect opBounds = op->bounds(); |
331 | List chain(std::move(op)); |
332 | if (!this->tryConcat( |
333 | &chain, processorAnalysis, *dstProxyView, appliedClip, opBounds, caps, |
334 | arenas, auditTrail)) { |
335 | // append failed, give the op back to the caller. |
336 | this->validate(); |
337 | return chain.popHead(); |
338 | } |
339 | |
340 | SkASSERT(chain.empty()); |
341 | this->validate(); |
342 | return nullptr; |
343 | } |
344 | |
345 | inline void GrOpsTask::OpChain::validate() const { |
346 | #ifdef SK_DEBUG |
347 | fList.validate(); |
348 | for (const auto& op : GrOp::ChainRange<>(fList.head())) { |
349 | // Not using SkRect::contains because we allow empty rects. |
350 | SkASSERT(fBounds.fLeft <= op.bounds().fLeft && fBounds.fTop <= op.bounds().fTop && |
351 | fBounds.fRight >= op.bounds().fRight && fBounds.fBottom >= op.bounds().fBottom); |
352 | } |
353 | #endif |
354 | } |
355 | |
356 | //////////////////////////////////////////////////////////////////////////////// |
357 | |
358 | GrOpsTask::GrOpsTask(GrDrawingManager* drawingMgr, GrRecordingContext::Arenas arenas, |
359 | GrSurfaceProxyView view, |
360 | GrAuditTrail* auditTrail) |
361 | : GrRenderTask() |
362 | , fArenas(arenas) |
363 | , fAuditTrail(auditTrail) |
364 | SkDEBUGCODE(, fNumClips(0)) { |
365 | this->addTarget(drawingMgr, std::move(view)); |
366 | } |
367 | |
368 | void GrOpsTask::deleteOps() { |
369 | for (auto& chain : fOpChains) { |
370 | chain.deleteOps(fArenas.opMemoryPool()); |
371 | } |
372 | fOpChains.reset(); |
373 | } |
374 | |
375 | GrOpsTask::~GrOpsTask() { |
376 | this->deleteOps(); |
377 | } |
378 | |
379 | void GrOpsTask::removeClosedObserver(GrOpsTaskClosedObserver* observer) { |
380 | SkASSERT(observer); |
381 | for (int i = 0; i < fClosedObservers.count(); ++i) { |
382 | if (fClosedObservers[i] == observer) { |
383 | fClosedObservers.removeShuffle(i); |
384 | --i; |
385 | } |
386 | } |
387 | } |
388 | |
389 | void GrOpsTask::endFlush(GrDrawingManager* drawingMgr) { |
390 | fLastClipStackGenID = SK_InvalidUniqueID; |
391 | this->deleteOps(); |
392 | fClipAllocator.reset(); |
393 | |
394 | fDeferredProxies.reset(); |
395 | fSampledProxies.reset(); |
396 | fAuditTrail = nullptr; |
397 | |
398 | GrRenderTask::endFlush(drawingMgr); |
399 | } |
400 | |
401 | void GrOpsTask::onPrePrepare(GrRecordingContext* context) { |
402 | SkASSERT(this->isClosed()); |
403 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
404 | TRACE_EVENT0("skia.gpu" , TRACE_FUNC); |
405 | #endif |
406 | // TODO: remove the check for discard here once reduced op splitting is turned on. Currently we |
407 | // can end up with GrOpsTasks that only have a discard load op and no ops. For vulkan validation |
408 | // we need to keep that discard and not drop it. Once we have reduce op list splitting enabled |
409 | // we shouldn't end up with GrOpsTasks with only discard. |
410 | if (this->isNoOp() || (fClippedContentBounds.isEmpty() && fColorLoadOp != GrLoadOp::kDiscard)) { |
411 | return; |
412 | } |
413 | |
414 | for (const auto& chain : fOpChains) { |
415 | if (chain.shouldExecute()) { |
416 | chain.head()->prePrepare(context, |
417 | &fTargets[0], |
418 | chain.appliedClip(), |
419 | chain.dstProxyView()); |
420 | } |
421 | } |
422 | } |
423 | |
424 | void GrOpsTask::onPrepare(GrOpFlushState* flushState) { |
425 | SkASSERT(this->target(0).proxy()->peekRenderTarget()); |
426 | SkASSERT(this->isClosed()); |
427 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
428 | TRACE_EVENT0("skia.gpu" , TRACE_FUNC); |
429 | #endif |
430 | // TODO: remove the check for discard here once reduced op splitting is turned on. Currently we |
431 | // can end up with GrOpsTasks that only have a discard load op and no ops. For vulkan validation |
432 | // we need to keep that discard and not drop it. Once we have reduce op list splitting enabled |
433 | // we shouldn't end up with GrOpsTasks with only discard. |
434 | if (this->isNoOp() || (fClippedContentBounds.isEmpty() && fColorLoadOp != GrLoadOp::kDiscard)) { |
435 | return; |
436 | } |
437 | |
438 | flushState->setSampledProxyArray(&fSampledProxies); |
439 | // Loop over the ops that haven't yet been prepared. |
440 | for (const auto& chain : fOpChains) { |
441 | if (chain.shouldExecute()) { |
442 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
443 | TRACE_EVENT0("skia.gpu" , chain.head()->name()); |
444 | #endif |
445 | GrOpFlushState::OpArgs opArgs(chain.head(), |
446 | &fTargets[0], |
447 | chain.appliedClip(), |
448 | chain.dstProxyView()); |
449 | |
450 | flushState->setOpArgs(&opArgs); |
451 | |
452 | // Temporary debugging helper: for debugging prePrepare w/o going through DDLs |
453 | // Delete once most of the GrOps have an onPrePrepare. |
454 | // chain.head()->prePrepare(flushState->gpu()->getContext(), &this->target(0), |
455 | // chain.appliedClip()); |
456 | |
457 | // GrOp::prePrepare may or may not have been called at this point |
458 | chain.head()->prepare(flushState); |
459 | flushState->setOpArgs(nullptr); |
460 | } |
461 | } |
462 | flushState->setSampledProxyArray(nullptr); |
463 | } |
464 | |
465 | static GrOpsRenderPass* create_render_pass( |
466 | GrGpu* gpu, GrRenderTarget* rt, GrStencilAttachment* stencil, GrSurfaceOrigin origin, |
467 | const SkIRect& bounds, GrLoadOp colorLoadOp, const SkPMColor4f& loadClearColor, |
468 | GrLoadOp stencilLoadOp, GrStoreOp stencilStoreOp, |
469 | const SkTArray<GrSurfaceProxy*, true>& sampledProxies) { |
470 | const GrOpsRenderPass::LoadAndStoreInfo kColorLoadStoreInfo { |
471 | colorLoadOp, |
472 | GrStoreOp::kStore, |
473 | loadClearColor |
474 | }; |
475 | |
476 | // TODO: |
477 | // We would like to (at this level) only ever clear & discard. We would need |
478 | // to stop splitting up higher level OpsTasks for copyOps to achieve that. |
479 | // Note: we would still need SB loads and stores but they would happen at a |
480 | // lower level (inside the VK command buffer). |
481 | const GrOpsRenderPass::StencilLoadAndStoreInfo stencilLoadAndStoreInfo { |
482 | stencilLoadOp, |
483 | stencilStoreOp, |
484 | }; |
485 | |
486 | return gpu->getOpsRenderPass(rt, stencil, origin, bounds, |
487 | kColorLoadStoreInfo, stencilLoadAndStoreInfo, sampledProxies); |
488 | } |
489 | |
490 | // TODO: this is where GrOp::renderTarget is used (which is fine since it |
491 | // is at flush time). However, we need to store the RenderTargetProxy in the |
492 | // Ops and instantiate them here. |
493 | bool GrOpsTask::onExecute(GrOpFlushState* flushState) { |
494 | // TODO: remove the check for discard here once reduced op splitting is turned on. Currently we |
495 | // can end up with GrOpsTasks that only have a discard load op and no ops. For vulkan validation |
496 | // we need to keep that discard and not drop it. Once we have reduce op list splitting enabled |
497 | // we shouldn't end up with GrOpsTasks with only discard. |
498 | if (this->isNoOp() || (fClippedContentBounds.isEmpty() && fColorLoadOp != GrLoadOp::kDiscard)) { |
499 | return false; |
500 | } |
501 | |
502 | SkASSERT(this->numTargets() == 1); |
503 | GrRenderTargetProxy* proxy = this->target(0).proxy()->asRenderTargetProxy(); |
504 | SkASSERT(proxy); |
505 | TRACE_EVENT0("skia.gpu" , TRACE_FUNC); |
506 | |
507 | // Make sure load ops are not kClear if the GPU needs to use draws for clears |
508 | SkASSERT(fColorLoadOp != GrLoadOp::kClear || |
509 | !flushState->gpu()->caps()->performColorClearsAsDraws()); |
510 | |
511 | const GrCaps& caps = *flushState->gpu()->caps(); |
512 | GrRenderTarget* renderTarget = proxy->peekRenderTarget(); |
513 | SkASSERT(renderTarget); |
514 | |
515 | GrStencilAttachment* stencil = nullptr; |
516 | if (int numStencilSamples = proxy->numStencilSamples()) { |
517 | if (!flushState->resourceProvider()->attachStencilAttachment( |
518 | renderTarget, numStencilSamples)) { |
519 | SkDebugf("WARNING: failed to attach a stencil buffer. Rendering will be skipped.\n" ); |
520 | return false; |
521 | } |
522 | stencil = renderTarget->getStencilAttachment(); |
523 | } |
524 | |
525 | SkASSERT(!stencil || stencil->numSamples() == proxy->numStencilSamples()); |
526 | |
527 | GrLoadOp stencilLoadOp; |
528 | switch (fInitialStencilContent) { |
529 | case StencilContent::kDontCare: |
530 | stencilLoadOp = GrLoadOp::kDiscard; |
531 | break; |
532 | case StencilContent::kUserBitsCleared: |
533 | SkASSERT(!caps.performStencilClearsAsDraws()); |
534 | SkASSERT(stencil); |
535 | if (caps.discardStencilValuesAfterRenderPass()) { |
536 | // Always clear the stencil if it is being discarded after render passes. This is |
537 | // also an optimization because we are on a tiler and it avoids loading the values |
538 | // from memory. |
539 | stencilLoadOp = GrLoadOp::kClear; |
540 | break; |
541 | } |
542 | if (!stencil->hasPerformedInitialClear()) { |
543 | stencilLoadOp = GrLoadOp::kClear; |
544 | stencil->markHasPerformedInitialClear(); |
545 | break; |
546 | } |
547 | // renderTargetContexts are required to leave the user stencil bits in a cleared state |
548 | // once finished, meaning the stencil values will always remain cleared after the |
549 | // initial clear. Just fall through to reloading the existing (cleared) stencil values |
550 | // from memory. |
551 | [[fallthrough]]; |
552 | case StencilContent::kPreserved: |
553 | SkASSERT(stencil); |
554 | stencilLoadOp = GrLoadOp::kLoad; |
555 | break; |
556 | } |
557 | |
558 | // NOTE: If fMustPreserveStencil is set, then we are executing a renderTargetContext that split |
559 | // its opsTask. |
560 | // |
561 | // FIXME: We don't currently flag render passes that don't use stencil at all. In that case |
562 | // their store op might be "discard", and we currently make the assumption that a discard will |
563 | // not invalidate what's already in main memory. This is probably ok for now, but certainly |
564 | // something we want to address soon. |
565 | GrStoreOp stencilStoreOp = (caps.discardStencilValuesAfterRenderPass() && !fMustPreserveStencil) |
566 | ? GrStoreOp::kDiscard |
567 | : GrStoreOp::kStore; |
568 | |
569 | GrOpsRenderPass* renderPass = create_render_pass( |
570 | flushState->gpu(), proxy->peekRenderTarget(), stencil, this->target(0).origin(), |
571 | fClippedContentBounds, fColorLoadOp, fLoadClearColor, stencilLoadOp, stencilStoreOp, |
572 | fSampledProxies); |
573 | if (!renderPass) { |
574 | return false; |
575 | } |
576 | flushState->setOpsRenderPass(renderPass); |
577 | renderPass->begin(); |
578 | |
579 | // Draw all the generated geometry. |
580 | for (const auto& chain : fOpChains) { |
581 | if (!chain.shouldExecute()) { |
582 | continue; |
583 | } |
584 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
585 | TRACE_EVENT0("skia.gpu" , chain.head()->name()); |
586 | #endif |
587 | |
588 | GrOpFlushState::OpArgs opArgs(chain.head(), |
589 | &fTargets[0], |
590 | chain.appliedClip(), |
591 | chain.dstProxyView()); |
592 | |
593 | flushState->setOpArgs(&opArgs); |
594 | chain.head()->execute(flushState, chain.bounds()); |
595 | flushState->setOpArgs(nullptr); |
596 | } |
597 | |
598 | renderPass->end(); |
599 | flushState->gpu()->submit(renderPass); |
600 | flushState->setOpsRenderPass(nullptr); |
601 | |
602 | return true; |
603 | } |
604 | |
605 | void GrOpsTask::setColorLoadOp(GrLoadOp op, const SkPMColor4f& color) { |
606 | fColorLoadOp = op; |
607 | fLoadClearColor = color; |
608 | if (GrLoadOp::kClear == fColorLoadOp) { |
609 | GrSurfaceProxy* proxy = this->target(0).proxy(); |
610 | SkASSERT(proxy); |
611 | fTotalBounds = proxy->backingStoreBoundsRect(); |
612 | } |
613 | } |
614 | |
615 | bool GrOpsTask::resetForFullscreenClear(CanDiscardPreviousOps canDiscardPreviousOps) { |
616 | if (CanDiscardPreviousOps::kYes == canDiscardPreviousOps || this->isEmpty()) { |
617 | this->deleteOps(); |
618 | fDeferredProxies.reset(); |
619 | fSampledProxies.reset(); |
620 | |
621 | // If the opsTask is using a render target which wraps a vulkan command buffer, we can't do |
622 | // a clear load since we cannot change the render pass that we are using. Thus we fall back |
623 | // to making a clear op in this case. |
624 | return !this->target(0).asRenderTargetProxy()->wrapsVkSecondaryCB(); |
625 | } |
626 | |
627 | // Could not empty the task, so an op must be added to handle the clear |
628 | return false; |
629 | } |
630 | |
631 | void GrOpsTask::discard() { |
632 | // Discard calls to in-progress opsTasks are ignored. Calls at the start update the |
633 | // opsTasks' color & stencil load ops. |
634 | if (this->isEmpty()) { |
635 | fColorLoadOp = GrLoadOp::kDiscard; |
636 | fInitialStencilContent = StencilContent::kDontCare; |
637 | fTotalBounds.setEmpty(); |
638 | } |
639 | } |
640 | |
641 | //////////////////////////////////////////////////////////////////////////////// |
642 | |
643 | #if GR_TEST_UTILS |
644 | void GrOpsTask::dump(bool printDependencies) const { |
645 | GrRenderTask::dump(printDependencies); |
646 | |
647 | SkDebugf("fColorLoadOp: " ); |
648 | switch (fColorLoadOp) { |
649 | case GrLoadOp::kLoad: |
650 | SkDebugf("kLoad\n" ); |
651 | break; |
652 | case GrLoadOp::kClear: |
653 | SkDebugf("kClear (0x%x)\n" , fLoadClearColor.toBytes_RGBA()); |
654 | break; |
655 | case GrLoadOp::kDiscard: |
656 | SkDebugf("kDiscard\n" ); |
657 | break; |
658 | } |
659 | |
660 | SkDebugf("fInitialStencilContent: " ); |
661 | switch (fInitialStencilContent) { |
662 | case StencilContent::kDontCare: |
663 | SkDebugf("kDontCare\n" ); |
664 | break; |
665 | case StencilContent::kUserBitsCleared: |
666 | SkDebugf("kUserBitsCleared\n" ); |
667 | break; |
668 | case StencilContent::kPreserved: |
669 | SkDebugf("kPreserved\n" ); |
670 | break; |
671 | } |
672 | |
673 | SkDebugf("ops (%d):\n" , fOpChains.count()); |
674 | for (int i = 0; i < fOpChains.count(); ++i) { |
675 | SkDebugf("*******************************\n" ); |
676 | if (!fOpChains[i].head()) { |
677 | SkDebugf("%d: <combined forward or failed instantiation>\n" , i); |
678 | } else { |
679 | SkDebugf("%d: %s\n" , i, fOpChains[i].head()->name()); |
680 | SkRect bounds = fOpChains[i].bounds(); |
681 | SkDebugf("ClippedBounds: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n" , bounds.fLeft, |
682 | bounds.fTop, bounds.fRight, bounds.fBottom); |
683 | for (const auto& op : GrOp::ChainRange<>(fOpChains[i].head())) { |
684 | SkString info = SkTabString(op.dumpInfo(), 1); |
685 | SkDebugf("%s\n" , info.c_str()); |
686 | bounds = op.bounds(); |
687 | SkDebugf("\tClippedBounds: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n" , bounds.fLeft, |
688 | bounds.fTop, bounds.fRight, bounds.fBottom); |
689 | } |
690 | } |
691 | } |
692 | } |
693 | #endif |
694 | |
695 | #ifdef SK_DEBUG |
696 | void GrOpsTask::visitProxies_debugOnly(const GrOp::VisitProxyFunc& func) const { |
697 | auto textureFunc = [ func ] (GrSurfaceProxy* tex, GrMipmapped mipmapped) { |
698 | func(tex, mipmapped); |
699 | }; |
700 | |
701 | for (const OpChain& chain : fOpChains) { |
702 | chain.visitProxies(textureFunc); |
703 | } |
704 | } |
705 | |
706 | #endif |
707 | |
708 | //////////////////////////////////////////////////////////////////////////////// |
709 | |
710 | bool GrOpsTask::onIsUsed(GrSurfaceProxy* proxyToCheck) const { |
711 | bool used = false; |
712 | |
713 | auto visit = [ proxyToCheck, &used ] (GrSurfaceProxy* p, GrMipmapped) { |
714 | if (p == proxyToCheck) { |
715 | used = true; |
716 | } |
717 | }; |
718 | for (const OpChain& recordedOp : fOpChains) { |
719 | recordedOp.visitProxies(visit); |
720 | } |
721 | |
722 | return used; |
723 | } |
724 | |
725 | void GrOpsTask::handleInternalAllocationFailure() { |
726 | bool hasUninstantiatedProxy = false; |
727 | auto checkInstantiation = [&hasUninstantiatedProxy](GrSurfaceProxy* p, GrMipmapped) { |
728 | if (!p->isInstantiated()) { |
729 | hasUninstantiatedProxy = true; |
730 | } |
731 | }; |
732 | for (OpChain& recordedOp : fOpChains) { |
733 | hasUninstantiatedProxy = false; |
734 | recordedOp.visitProxies(checkInstantiation); |
735 | if (hasUninstantiatedProxy) { |
736 | recordedOp.setSkipExecuteFlag(); |
737 | } |
738 | } |
739 | } |
740 | |
741 | void GrOpsTask::gatherProxyIntervals(GrResourceAllocator* alloc) const { |
742 | for (int i = 0; i < fDeferredProxies.count(); ++i) { |
743 | SkASSERT(!fDeferredProxies[i]->isInstantiated()); |
744 | // We give all the deferred proxies a write usage at the very start of flushing. This |
745 | // locks them out of being reused for the entire flush until they are read - and then |
746 | // they can be recycled. This is a bit unfortunate because a flush can proceed in waves |
747 | // with sub-flushes. The deferred proxies only need to be pinned from the start of |
748 | // the sub-flush in which they appear. |
749 | alloc->addInterval(fDeferredProxies[i], 0, 0, GrResourceAllocator::ActualUse::kNo); |
750 | } |
751 | |
752 | GrSurfaceProxy* targetProxy = this->target(0).proxy(); |
753 | |
754 | // Add the interval for all the writes to this GrOpsTasks's target |
755 | if (fOpChains.count()) { |
756 | unsigned int cur = alloc->curOp(); |
757 | |
758 | alloc->addInterval(targetProxy, cur, cur + fOpChains.count() - 1, |
759 | GrResourceAllocator::ActualUse::kYes); |
760 | } else { |
761 | // This can happen if there is a loadOp (e.g., a clear) but no other draws. In this case we |
762 | // still need to add an interval for the destination so we create a fake op# for |
763 | // the missing clear op. |
764 | alloc->addInterval(targetProxy, alloc->curOp(), alloc->curOp(), |
765 | GrResourceAllocator::ActualUse::kYes); |
766 | alloc->incOps(); |
767 | } |
768 | |
769 | auto gather = [ alloc SkDEBUGCODE(, this) ] (GrSurfaceProxy* p, GrMipmapped) { |
770 | alloc->addInterval(p, alloc->curOp(), alloc->curOp(), GrResourceAllocator::ActualUse::kYes |
771 | SkDEBUGCODE(, this->target(0).proxy() == p)); |
772 | }; |
773 | for (const OpChain& recordedOp : fOpChains) { |
774 | recordedOp.visitProxies(gather); |
775 | |
776 | // Even though the op may have been (re)moved we still need to increment the op count to |
777 | // keep all the math consistent. |
778 | alloc->incOps(); |
779 | } |
780 | } |
781 | |
782 | void GrOpsTask::recordOp( |
783 | std::unique_ptr<GrOp> op, GrProcessorSet::Analysis processorAnalysis, GrAppliedClip* clip, |
784 | const DstProxyView* dstProxyView, const GrCaps& caps) { |
785 | SkDEBUGCODE(op->validate();) |
786 | SkASSERT(processorAnalysis.requiresDstTexture() == (dstProxyView && dstProxyView->proxy())); |
787 | GrSurfaceProxy* proxy = this->target(0).proxy(); |
788 | SkASSERT(proxy); |
789 | |
790 | // A closed GrOpsTask should never receive new/more ops |
791 | SkASSERT(!this->isClosed()); |
792 | if (!op->bounds().isFinite()) { |
793 | fArenas.opMemoryPool()->release(std::move(op)); |
794 | return; |
795 | } |
796 | |
797 | // Account for this op's bounds before we attempt to combine. |
798 | // NOTE: The caller should have already called "op->setClippedBounds()" by now, if applicable. |
799 | fTotalBounds.join(op->bounds()); |
800 | |
801 | // Check if there is an op we can combine with by linearly searching back until we either |
802 | // 1) check every op |
803 | // 2) intersect with something |
804 | // 3) find a 'blocker' |
805 | GR_AUDIT_TRAIL_ADD_OP(fAuditTrail, op.get(), proxy->uniqueID()); |
806 | GrOP_INFO("opsTask: %d Recording (%s, opID: %u)\n" |
807 | "\tBounds [L: %.2f, T: %.2f R: %.2f B: %.2f]\n" , |
808 | this->uniqueID(), |
809 | op->name(), |
810 | op->uniqueID(), |
811 | op->bounds().fLeft, op->bounds().fTop, |
812 | op->bounds().fRight, op->bounds().fBottom); |
813 | GrOP_INFO(SkTabString(op->dumpInfo(), 1).c_str()); |
814 | GrOP_INFO("\tOutcome:\n" ); |
815 | int maxCandidates = std::min(kMaxOpChainDistance, fOpChains.count()); |
816 | if (maxCandidates) { |
817 | int i = 0; |
818 | while (true) { |
819 | OpChain& candidate = fOpChains.fromBack(i); |
820 | op = candidate.appendOp(std::move(op), processorAnalysis, dstProxyView, clip, caps, |
821 | &fArenas, fAuditTrail); |
822 | if (!op) { |
823 | return; |
824 | } |
825 | // Stop going backwards if we would cause a painter's order violation. |
826 | if (!can_reorder(candidate.bounds(), op->bounds())) { |
827 | GrOP_INFO("\t\tBackward: Intersects with chain (%s, head opID: %u)\n" , |
828 | candidate.head()->name(), candidate.head()->uniqueID()); |
829 | break; |
830 | } |
831 | if (++i == maxCandidates) { |
832 | GrOP_INFO("\t\tBackward: Reached max lookback or beginning of op array %d\n" , i); |
833 | break; |
834 | } |
835 | } |
836 | } else { |
837 | GrOP_INFO("\t\tBackward: FirstOp\n" ); |
838 | } |
839 | if (clip) { |
840 | clip = fClipAllocator.make<GrAppliedClip>(std::move(*clip)); |
841 | SkDEBUGCODE(fNumClips++;) |
842 | } |
843 | fOpChains.emplace_back(std::move(op), processorAnalysis, clip, dstProxyView); |
844 | } |
845 | |
846 | void GrOpsTask::forwardCombine(const GrCaps& caps) { |
847 | SkASSERT(!this->isClosed()); |
848 | GrOP_INFO("opsTask: %d ForwardCombine %d ops:\n" , this->uniqueID(), fOpChains.count()); |
849 | |
850 | for (int i = 0; i < fOpChains.count() - 1; ++i) { |
851 | OpChain& chain = fOpChains[i]; |
852 | int maxCandidateIdx = std::min(i + kMaxOpChainDistance, fOpChains.count() - 1); |
853 | int j = i + 1; |
854 | while (true) { |
855 | OpChain& candidate = fOpChains[j]; |
856 | if (candidate.prependChain(&chain, caps, &fArenas, fAuditTrail)) { |
857 | break; |
858 | } |
859 | // Stop traversing if we would cause a painter's order violation. |
860 | if (!can_reorder(chain.bounds(), candidate.bounds())) { |
861 | GrOP_INFO( |
862 | "\t\t%d: chain (%s head opID: %u) -> " |
863 | "Intersects with chain (%s, head opID: %u)\n" , |
864 | i, chain.head()->name(), chain.head()->uniqueID(), candidate.head()->name(), |
865 | candidate.head()->uniqueID()); |
866 | break; |
867 | } |
868 | if (++j > maxCandidateIdx) { |
869 | GrOP_INFO("\t\t%d: chain (%s opID: %u) -> Reached max lookahead or end of array\n" , |
870 | i, chain.head()->name(), chain.head()->uniqueID()); |
871 | break; |
872 | } |
873 | } |
874 | } |
875 | } |
876 | |
877 | GrRenderTask::ExpectedOutcome GrOpsTask::onMakeClosed( |
878 | const GrCaps& caps, SkIRect* targetUpdateBounds) { |
879 | this->forwardCombine(caps); |
880 | SkScopeExit triggerObservers([&] { |
881 | for (const auto& o : fClosedObservers) { |
882 | o->wasClosed(*this); |
883 | } |
884 | fClosedObservers.reset(); |
885 | }); |
886 | if (!this->isNoOp()) { |
887 | GrSurfaceProxy* proxy = this->target(0).proxy(); |
888 | // Use the entire backing store bounds since the GPU doesn't clip automatically to the |
889 | // logical dimensions. |
890 | SkRect clippedContentBounds = proxy->backingStoreBoundsRect(); |
891 | // TODO: If we can fix up GLPrograms test to always intersect the target proxy bounds |
892 | // then we can simply assert here that the bounds intersect. |
893 | if (clippedContentBounds.intersect(fTotalBounds)) { |
894 | clippedContentBounds.roundOut(&fClippedContentBounds); |
895 | *targetUpdateBounds = fClippedContentBounds; |
896 | return ExpectedOutcome::kTargetDirty; |
897 | } |
898 | } |
899 | return ExpectedOutcome::kTargetUnchanged; |
900 | } |
901 | |