Debian Codegraph @

src/gpu/ganesh/GrDrawingManager.cpp

raw ◀ ▶
Source reconstructed at patch state 2 of 4 — not the installed (fully patched) source.
4 Debian patches touch this file
1 /*
2 * Copyright 2015 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 #include "src/gpu/ganesh/GrDrawingManager.h"
8
9 #include "include/core/SkData.h"
10 #include "include/core/SkRect.h"
11 #include "include/core/SkSize.h"
12 #include "include/core/SkSurface.h"
13 #include "include/gpu/GpuTypes.h"
14 #include "include/gpu/ganesh/GrDirectContext.h"
15 #include "include/gpu/ganesh/GrRecordingContext.h"
16 #include "include/gpu/ganesh/GrTypes.h"
17 #include "include/private/base/SkAssert.h"
18 #include "include/private/base/SkTo.h"
19 #include "include/private/chromium/GrDeferredDisplayList.h"
20 #include "include/private/chromium/GrSurfaceCharacterization.h"
21 #include "include/private/gpu/ganesh/GrTypesPriv.h"
22 #include "src/base/SkTInternalLList.h"
23 #include "src/core/SkTraceEvent.h"
24 #include "src/gpu/GpuTypesPriv.h"
25 #include "src/gpu/ganesh/GrAuditTrail.h"
26 #include "src/gpu/ganesh/GrBufferTransferRenderTask.h"
27 #include "src/gpu/ganesh/GrBufferUpdateRenderTask.h"
28 #include "src/gpu/ganesh/GrClientMappedBufferManager.h"
29 #include "src/gpu/ganesh/GrCopyRenderTask.h"
30 #include "src/gpu/ganesh/GrDDLTask.h"
31 #include "src/gpu/ganesh/GrDeferredDisplayListPriv.h"
32 #include "src/gpu/ganesh/GrDirectContextPriv.h"
33 #include "src/gpu/ganesh/GrGpu.h"
34 #include "src/gpu/ganesh/GrGpuBuffer.h"
35 #include "src/gpu/ganesh/GrNativeRect.h"
36 #include "src/gpu/ganesh/GrOnFlushResourceProvider.h"
37 #include "src/gpu/ganesh/GrOpFlushState.h"
38 #include "src/gpu/ganesh/GrRecordingContextPriv.h"
39 #include "src/gpu/ganesh/GrRenderTargetProxy.h"
40 #include "src/gpu/ganesh/GrRenderTask.h"
41 #include "src/gpu/ganesh/GrRenderTaskCluster.h"
42 #include "src/gpu/ganesh/GrResourceAllocator.h"
43 #include "src/gpu/ganesh/GrResourceCache.h"
44 #include "src/gpu/ganesh/GrSurfaceProxyView.h"
45 #include "src/gpu/ganesh/GrTTopoSort.h"
46 #include "src/gpu/ganesh/GrTextureProxy.h"
47 #include "src/gpu/ganesh/GrTextureResolveManager.h"
48 #include "src/gpu/ganesh/GrTextureResolveRenderTask.h"
49 #include "src/gpu/ganesh/GrTracing.h"
50 #include "src/gpu/ganesh/GrTransferFromRenderTask.h"
51 #include "src/gpu/ganesh/GrWaitRenderTask.h"
52 #include "src/gpu/ganesh/GrWritePixelsRenderTask.h"
53 #include "src/gpu/ganesh/ops/GrOp.h"
54 #include "src/gpu/ganesh/ops/OpsTask.h"
55 #include "src/gpu/ganesh/ops/SoftwarePathRenderer.h"
56
57 #include <algorithm>
58 #include <memory>
59 #include <optional>
60 #include <utility>
61
62 using namespace skia_private;
63
64 ///////////////////////////////////////////////////////////////////////////////////////////////////
65 GrDrawingManager::GrDrawingManager(GrRecordingContext* rContext,
66 const PathRendererChain::Options& optionsForPathRendererChain,
67 bool reduceOpsTaskSplitting)
68 : fContext(rContext)
69 , fOptionsForPathRendererChain(optionsForPathRendererChain)
70 , fPathRendererChain(nullptr)
71 , fSoftwarePathRenderer(nullptr)
72 , fReduceOpsTaskSplitting(reduceOpsTaskSplitting) {
73 }
74
75 GrDrawingManager::~GrDrawingManager() {
76 this->closeAllTasks();
77 this->removeRenderTasks();
78 }
79
80 bool GrDrawingManager::wasAbandoned() const {
81 return fContext->abandoned();
82 }
83
84 void GrDrawingManager::freeGpuResources() {
85 for (int i = fOnFlushCBObjects.size() - 1; i >= 0; --i) {
86 if (!fOnFlushCBObjects[i]->retainOnFreeGpuResources()) {
87 // it's safe to just do this because we're iterating in reverse
88 fOnFlushCBObjects.removeShuffle(i);
89 }
90 }
91
92 // a path renderer may be holding onto resources
93 fPathRendererChain = nullptr;
94 fSoftwarePathRenderer = nullptr;
95 }
96
97 // MDB TODO: make use of the 'proxies' parameter.
98 bool GrDrawingManager::flush(SkSpan<GrSurfaceProxy*> proxies,
99 SkSurfaces::BackendSurfaceAccess access,
100 const GrFlushInfo& info,
101 const skgpu::MutableTextureState* newState) {
102 GR_CREATE_TRACE_MARKER_CONTEXT("GrDrawingManager", "flush", fContext);
103
104 if (fFlushing || this->wasAbandoned()) {
105 if (info.fSubmittedProc) {
106 info.fSubmittedProc(info.fSubmittedContext, false);
107 }
108 if (info.fFinishedProc) {
109 info.fFinishedProc(info.fFinishedContext);
110 }
111 return false;
112 }
113
114 SkDEBUGCODE(this->validate());
115
116 // As of now we only short-circuit if we got an explicit list of surfaces to flush.
117 if (!proxies.empty() && !info.fNumSemaphores && !info.fFinishedProc &&
118 access == SkSurfaces::BackendSurfaceAccess::kNoAccess && !newState) {
119 bool allUnused = std::all_of(proxies.begin(), proxies.end(), [&](GrSurfaceProxy* proxy) {
120 bool used = std::any_of(fDAG.begin(), fDAG.end(), [&](auto& task) {
121 return task && task->isUsed(proxy);
122 });
123 return !used;
124 });
125 if (allUnused) {
126 if (info.fSubmittedProc) {
127 info.fSubmittedProc(info.fSubmittedContext, true);
128 }
129 // Nothing to flush is a success (fSubmittedProc is already called with `true`
130 // above).
131 return true;
132 }
133 }
134
135 auto dContext = fContext->asDirectContext();
136 SkASSERT(dContext);
137 dContext->priv().clientMappedBufferManager()->process();
138
139 GrGpu* gpu = dContext->priv().getGpu();
140 // We have a non abandoned and direct GrContext. It must have a GrGpu.
141 SkASSERT(gpu);
142
143 fFlushing = true;
144
145 auto resourceProvider = dContext->priv().resourceProvider();
146 auto resourceCache = dContext->priv().getResourceCache();
147
148 // Semi-usually the GrRenderTasks are already closed at this point, but sometimes Ganesh needs
149 // to flush mid-draw. In that case, the SkGpuDevice's opsTasks won't be closed but need to be
150 // flushed anyway. Closing such opsTasks here will mean new ones will be created to replace them
151 // if the SkGpuDevice(s) write to them again.
152 this->closeAllTasks();
153 fActiveOpsTask = nullptr;
154
155 this->sortTasks();
156
157 if (!fCpuBufferCache) {
158 // We cache more buffers when the backend is using client side arrays. Otherwise, we
159 // expect each pool will use a CPU buffer as a staging buffer before uploading to a GPU
160 // buffer object. Each pool only requires one staging buffer at a time.
161 int maxCachedBuffers = fContext->priv().caps()->preferClientSideDynamicBuffers() ? 2 : 6;
162 fCpuBufferCache = GrBufferAllocPool::CpuBufferCache::Make(maxCachedBuffers);
163 }
164
165 GrOpFlushState flushState(gpu, resourceProvider, &fTokenTracker, fCpuBufferCache);
166
167 std::optional<GrTimerQuery> timerQuery;
168 if (info.fFinishedWithStatsProc && (info.fGpuStatsFlags & skgpu::GpuStatsFlags::kElapsedTime)) {
169 timerQuery = gpu->startTimerQuery();
170 }
171 GrOnFlushResourceProvider onFlushProvider(this);
172
173 // Prepare any onFlush op lists (e.g. atlases).
174 bool preFlushSuccessful = true;
175 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
176 preFlushSuccessful &= onFlushCBObject->preFlush(&onFlushProvider);
177 }
178
179 bool cachePurgeNeeded = false;
180 bool flushSuccessful = false;
181
182 if (preFlushSuccessful) {
183 bool usingReorderedDAG = false;
184 GrResourceAllocator resourceAllocator(dContext);
185 if (fReduceOpsTaskSplitting) {
186 usingReorderedDAG = this->reorderTasks(&resourceAllocator);
187 if (!usingReorderedDAG) {
188 resourceAllocator.reset();
189 }
190 }
191
192 #if 0
193 // Enable this to print out verbose GrOp information
194 SkDEBUGCODE(SkDebugf("RenderTasks (%d):\n", fDAG.count()));
195 for (const auto& task : fDAG) {
196 SkDEBUGCODE(task->dump(/* printDependencies */ true);)
197 }
198 #endif
199
200 if (!resourceAllocator.failedInstantiation()) {
201 if (!usingReorderedDAG) {
202 for (const auto& task : fDAG) {
203 SkASSERT(task);
204 task->gatherProxyIntervals(&resourceAllocator);
205 }
206 resourceAllocator.planAssignment();
207 }
208 resourceAllocator.assign();
209 }
210
211 if (!resourceAllocator.failedInstantiation()) {
212 cachePurgeNeeded = this->executeRenderTasks(&flushState);
213 flushSuccessful = true;
214 }
215 }
216 this->removeRenderTasks();
217
218 gpu->executeFlushInfo(proxies, access, info, std::move(timerQuery), newState);
219
220 // Give the cache a chance to purge resources that become purgeable due to flushing.
221 if (cachePurgeNeeded) {
222 resourceCache->purgeAsNeeded();
223 cachePurgeNeeded = false;
224 }
225 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
226 onFlushCBObject->postFlush(fTokenTracker.nextFlushToken());
227 cachePurgeNeeded = true;
228 }
229 if (cachePurgeNeeded) {
230 resourceCache->purgeAsNeeded();
231 }
232 fFlushing = false;
233
234 return flushSuccessful;
235 }
236
237 bool GrDrawingManager::submitToGpu() {
238 if (fFlushing || this->wasAbandoned()) {
239 return false;
240 }
241
242 auto direct = fContext->asDirectContext();
243 if (!direct) {
244 return false; // Can't submit while DDL recording
245 }
246 GrGpu* gpu = direct->priv().getGpu();
247 return gpu->submitToGpu();
248 }
249
250 bool GrDrawingManager::executeRenderTasks(GrOpFlushState* flushState) {
251 #if GR_FLUSH_TIME_OP_SPEW
252 SkDebugf("Flushing %d opsTasks\n", fDAG.size());
253 for (int i = 0; i < fDAG.size(); ++i) {
254 if (fDAG[i]) {
255 SkString label;
256 label.printf("task %d/%d", i, fDAG.size());
257 fDAG[i]->dump(label, {}, true, true);
258 }
259 }
260 #endif
261
262 bool anyRenderTasksExecuted = false;
263
264 for (const auto& renderTask : fDAG) {
265 if (!renderTask || !renderTask->isInstantiated()) {
266 continue;
267 }
268
269 SkASSERT(renderTask->deferredProxiesAreInstantiated());
270
271 renderTask->prepare(flushState);
272 }
273
274 // Upload all data to the GPU
275 flushState->preExecuteDraws();
276
277 // For Vulkan, if we have too many oplists to be flushed we end up allocating a lot of resources
278 // for each command buffer associated with the oplists. If this gets too large we can cause the
279 // devices to go OOM. In practice we usually only hit this case in our tests, but to be safe we
280 // put a cap on the number of oplists we will execute before flushing to the GPU to relieve some
281 // memory pressure.
282 static constexpr int kMaxRenderTasksBeforeFlush = 100;
283 int numRenderTasksExecuted = 0;
284
285 // Unlike kMaxRenderTasksBeforeFlush, this is a global limit.
286 static constexpr int kMaxRenderPassesBeforeFlush = 100;
287
288 // Execute the normal op lists.
289 for (const auto& renderTask : fDAG) {
290 SkASSERT(renderTask);
291 if (!renderTask->isInstantiated()) {
292 continue;
293 }
294
295 if (renderTask->execute(flushState)) {
296 anyRenderTasksExecuted = true;
297 }
298 if (++numRenderTasksExecuted >= kMaxRenderTasksBeforeFlush ||
299 flushState->gpu()->getCurrentSubmitRenderPassCount() >= kMaxRenderPassesBeforeFlush) {
300 flushState->gpu()->submitToGpu();
301 numRenderTasksExecuted = 0;
302 }
303 }
304
305 SkASSERT(!flushState->opsRenderPass());
306 SkASSERT(fTokenTracker.nextDrawToken() == fTokenTracker.nextFlushToken());
307
308 // We reset the flush state before the RenderTasks so that the last resources to be freed are
309 // those that are written to in the RenderTasks. This helps to make sure the most recently used
310 // resources are the last to be purged by the resource cache.
311 flushState->reset();
312
313 return anyRenderTasksExecuted;
314 }
315
316 void GrDrawingManager::removeRenderTasks() {
317 for (const auto& task : fDAG) {
318 SkASSERT(task);
319 if (!task->unique() || task->requiresExplicitCleanup()) {
320 // TODO: Eventually uniqueness should be guaranteed: skbug.com/40038346.
321 // DDLs, however, will always require an explicit notification for when they
322 // can clean up resources.
323 task->endFlush(this);
324 }
325 task->disown(this);
326 }
327 fDAG.clear();
328 fReorderBlockerTaskIndices.clear();
329 fLastRenderTasks.reset();
330 }
331
332 void GrDrawingManager::sortTasks() {
333 // We separately sort the ranges around non-reorderable tasks.
334 for (size_t i = 0, start = 0, end; start < SkToSizeT(fDAG.size()); ++i, start = end + 1) {
335 end = i == fReorderBlockerTaskIndices.size() ? fDAG.size() : fReorderBlockerTaskIndices[i];
336 SkSpan span(fDAG.begin() + start, end - start);
337
338 SkASSERT(std::none_of(span.begin(), span.end(), [](const auto& t) {
339 return t->blocksReordering();
340 }));
341 SkASSERT(span.data() + span.size() == fDAG.end() || fDAG[end]->blocksReordering());
342
343 #if defined(SK_DEBUG)
344 // In order to partition the dag array like this it must be the case that each partition
345 // only depends on nodes in the partition or earlier partitions.
346 auto check = [&](const GrRenderTask* task, auto&& check) -> void {
347 SkASSERT(GrRenderTask::TopoSortTraits::WasOutput(task) ||
348 std::any_of(span.begin(), span.end(), [task](const auto& n) {
349 return n.get() == task; }));
350 for (int i = 0; i < task->fDependencies.size(); ++i) {
351 check(task->fDependencies[i], check);
352 }
353 };
354 for (const auto& node : span) {
355 check(node.get(), check);
356 }
357 #endif
358
359 bool sorted = GrTTopoSort<GrRenderTask, GrRenderTask::TopoSortTraits>(span, start);
360 if (!sorted) {
361 SkDEBUGFAIL("Render task topo sort failed.");
362 }
363
364 #ifdef SK_DEBUG
365 if (sorted && !span.empty()) {
366 // This block checks for any unnecessary splits in the opsTasks. If two sequential
367 // opsTasks could have merged it means the opsTask was artificially split.
368 auto prevOpsTask = span[0]->asOpsTask();
369 for (size_t j = 1; j < span.size(); ++j) {
370 auto curOpsTask = span[j]->asOpsTask();
371
372 if (prevOpsTask && curOpsTask) {
373 SkASSERT(!prevOpsTask->canMerge(curOpsTask));
374 }
375
376 prevOpsTask = curOpsTask;
377 }
378 }
379 #endif
380 }
381 }
382
383 // Reorder the array to match the llist without reffing & unreffing sk_sp's.
384 // Both args must contain the same objects.
385 // This is basically a shim because clustering uses LList but the rest of drawmgr uses array.
386 template <typename T>
387 static void reorder_array_by_llist(const SkTInternalLList<T>& llist, TArray<sk_sp<T>>* array) {
388 int i = 0;
389 for (T* t : llist) {
390 // Release the pointer that used to live here so it doesn't get unreffed.
391 [[maybe_unused]] T* old = array->at(i).release();
392 array->at(i++).reset(t);
393 }
394 SkASSERT(i == array->size());
395 }
396
397 bool GrDrawingManager::reorderTasks(GrResourceAllocator* resourceAllocator) {
398 SkASSERT(fReduceOpsTaskSplitting);
399 // We separately sort the ranges around non-reorderable tasks.
400 bool clustered = false;
401 SkTInternalLList<GrRenderTask> llist;
402 for (size_t i = 0, start = 0, end; start < SkToSizeT(fDAG.size()); ++i, start = end + 1) {
403 end = i == fReorderBlockerTaskIndices.size() ? fDAG.size() : fReorderBlockerTaskIndices[i];
404 SkSpan span(fDAG.begin() + start, end - start);
405 SkASSERT(std::none_of(span.begin(), span.end(), [](const auto& t) {
406 return t->blocksReordering();
407 }));
408
409 SkTInternalLList<GrRenderTask> subllist;
410 if (GrClusterRenderTasks(span, &subllist)) {
411 clustered = true;
412 }
413
414 if (i < fReorderBlockerTaskIndices.size()) {
415 SkASSERT(fDAG[fReorderBlockerTaskIndices[i]]->blocksReordering());
416 subllist.addToTail(fDAG[fReorderBlockerTaskIndices[i]].get());
417 }
418 llist.concat(std::move(subllist));
419 }
420 if (!clustered) {
421 return false;
422 }
423
424 for (GrRenderTask* task : llist) {
425 task->gatherProxyIntervals(resourceAllocator);
426 }
427 if (!resourceAllocator->planAssignment()) {
428 return false;
429 }
430 if (!resourceAllocator->makeBudgetHeadroom()) {
431 auto dContext = fContext->asDirectContext();
432 SkASSERT(dContext);
433 dContext->priv().getGpu()->stats()->incNumReorderedDAGsOverBudget();
434 return false;
435 }
436 reorder_array_by_llist(llist, &fDAG);
437
438 int newCount = 0;
439 for (int i = 0; i < fDAG.size(); i++) {
440 sk_sp<GrRenderTask>& task = fDAG[i];
441 if (auto opsTask = task->asOpsTask()) {
442 size_t remaining = fDAG.size() - i - 1;
443 SkSpan<sk_sp<GrRenderTask>> nextTasks{fDAG.end() - remaining, remaining};
444 int removeCount = opsTask->mergeFrom(nextTasks);
445 for (const auto& removed : nextTasks.first(removeCount)) {
446 removed->disown(this);
447 }
448 i += removeCount;
449 }
450 fDAG[newCount++] = std::move(task);
451 }
452 fDAG.resize_back(newCount);
453 return true;
454 }
455
456 void GrDrawingManager::closeAllTasks() {
457 for (auto& task : fDAG) {
458 if (task) {
459 task->makeClosed(fContext);
460 }
461 }
462 }
463
464 GrRenderTask* GrDrawingManager::insertTaskBeforeLast(sk_sp<GrRenderTask> task) {
465 if (!task) {
466 return nullptr;
467 }
468 if (fDAG.empty()) {
469 return fDAG.push_back(std::move(task)).get();
470 }
471 if (!fReorderBlockerTaskIndices.empty() && fReorderBlockerTaskIndices.back() == fDAG.size()) {
472 fReorderBlockerTaskIndices.back()++;
473 }
474 fDAG.push_back(std::move(task));
475 auto& penultimate = fDAG.fromBack(1);
476 fDAG.back().swap(penultimate);
477 return penultimate.get();
478 }
479
480 GrRenderTask* GrDrawingManager::appendTask(sk_sp<GrRenderTask> task) {
481 if (!task) {
482 return nullptr;
483 }
484 if (task->blocksReordering()) {
485 fReorderBlockerTaskIndices.push_back(fDAG.size());
486 }
487 return fDAG.push_back(std::move(task)).get();
488 }
489
490 static void resolve_and_mipmap(GrGpu* gpu, GrSurfaceProxy* proxy) {
491 if (!proxy->isInstantiated()) {
492 return;
493 }
494
495 // In the flushSurfaces case, we need to resolve MSAA immediately after flush. This is
496 // because clients expect the flushed surface's backing texture to be fully resolved
497 // upon return.
498 if (proxy->requiresManualMSAAResolve()) {
499 auto* rtProxy = proxy->asRenderTargetProxy();
500 SkASSERT(rtProxy);
501 if (rtProxy->isMSAADirty()) {
502 SkASSERT(rtProxy->peekRenderTarget());
503 gpu->resolveRenderTarget(rtProxy->peekRenderTarget(), rtProxy->msaaDirtyRect());
504 gpu->submitToGpu();
505 rtProxy->markMSAAResolved();
506 }
507 }
508 // If, after a flush, any of the proxies of interest have dirty mipmaps, regenerate them in
509 // case their backend textures are being stolen.
510 // (This special case is exercised by the ReimportImageTextureWithMipLevels test.)
511 // FIXME: It may be more ideal to plumb down a "we're going to steal the backends" flag.
512 if (auto* textureProxy = proxy->asTextureProxy()) {
513 if (textureProxy->mipmapsAreDirty()) {
514 SkASSERT(textureProxy->peekTexture());
515 if (gpu->regenerateMipMapLevels(textureProxy->peekTexture())) {
516 textureProxy->markMipmapsClean();
517 }
518 }
519 }
520 }
521
522 GrSemaphoresSubmitted GrDrawingManager::flushSurfaces(SkSpan<GrSurfaceProxy*> proxies,
523 SkSurfaces::BackendSurfaceAccess access,
524 const GrFlushInfo& info,
525 const skgpu::MutableTextureState* newState) {
526 if (this->wasAbandoned()) {
527 if (info.fSubmittedProc) {
528 info.fSubmittedProc(info.fSubmittedContext, false);
529 }
530 if (info.fFinishedProc) {
531 info.fFinishedProc(info.fFinishedContext);
532 }
533 return GrSemaphoresSubmitted::kNo;
534 }
535 SkDEBUGCODE(this->validate());
536
537 auto direct = fContext->asDirectContext();
538 SkASSERT(direct);
539 GrGpu* gpu = direct->priv().getGpu();
540 // We have a non abandoned and direct GrContext. It must have a GrGpu.
541 SkASSERT(gpu);
542
543 // TODO: It is important to upgrade the drawingmanager to just flushing the
544 // portion of the DAG required by 'proxies' in order to restore some of the
545 // semantics of this method.
546 bool didFlush = this->flush(proxies, access, info, newState);
547 if (didFlush) {
548 // Only resolve/regen mips if the flush actually executed the render tasks.
549 for (GrSurfaceProxy* proxy : proxies) {
550 resolve_and_mipmap(gpu, proxy);
551 }
552 }
553
554 SkDEBUGCODE(this->validate());
555
556 if (!didFlush || (!direct->priv().caps()->backendSemaphoreSupport() && info.fNumSemaphores)) {
557 return GrSemaphoresSubmitted::kNo;
558 }
559 return GrSemaphoresSubmitted::kYes;
560 }
561
562 void GrDrawingManager::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
563 fOnFlushCBObjects.push_back(onFlushCBObject);
564 }
565
566 #if defined(GPU_TEST_UTILS)
567 void GrDrawingManager::testingOnly_removeOnFlushCallbackObject(GrOnFlushCallbackObject* cb) {
568 int n = std::find(fOnFlushCBObjects.begin(), fOnFlushCBObjects.end(), cb) -
569 fOnFlushCBObjects.begin();
570 SkASSERT(n < fOnFlushCBObjects.size());
571 fOnFlushCBObjects.removeShuffle(n);
572 }
573 #endif
574
575 void GrDrawingManager::setLastRenderTask(const GrSurfaceProxy* proxy, GrRenderTask* task) {
576 #ifdef SK_DEBUG
577 if (auto prior = this->getLastRenderTask(proxy)) {
578 SkASSERT(prior->isClosed() || prior == task);
579 }
580 #endif
581 uint32_t key = proxy->uniqueID().asUInt();
582 if (task) {
583 fLastRenderTasks.set(key, task);
584 } else if (fLastRenderTasks.find(key)) {
585 fLastRenderTasks.remove(key);
586 }
587 }
588
589 GrRenderTask* GrDrawingManager::getLastRenderTask(const GrSurfaceProxy* proxy) const {
590 auto entry = fLastRenderTasks.find(proxy->uniqueID().asUInt());
591 return entry ? *entry : nullptr;
592 }
593
594 skgpu::ganesh::OpsTask* GrDrawingManager::getLastOpsTask(const GrSurfaceProxy* proxy) const {
595 GrRenderTask* task = this->getLastRenderTask(proxy);
596 return task ? task->asOpsTask() : nullptr;
597 }
598
599 void GrDrawingManager::moveRenderTasksToDDL(GrDeferredDisplayList* ddl) {
600 SkDEBUGCODE(this->validate());
601
602 // no renderTask should receive a new command after this
603 this->closeAllTasks();
604 fActiveOpsTask = nullptr;
605
606 this->sortTasks();
607
608 fDAG.swap(ddl->fRenderTasks);
609 SkASSERT(fDAG.empty());
610 fReorderBlockerTaskIndices.clear();
611
612 for (auto& renderTask : ddl->fRenderTasks) {
613 renderTask->disown(this);
614 renderTask->prePrepare(fContext);
615 }
616
617 ddl->fArenas = std::move(fContext->priv().detachArenas());
618
619 fContext->priv().detachProgramData(&ddl->fProgramData);
620
621 SkDEBUGCODE(this->validate());
622 }
623
624 void GrDrawingManager::createDDLTask(sk_sp<const GrDeferredDisplayList> ddl,
625 sk_sp<GrRenderTargetProxy> newDest) {
626 SkDEBUGCODE(this->validate());
627
628 if (fActiveOpsTask) {
629 // This is a temporary fix for the partial-MDB world. In that world we're not
630 // reordering so ops that (in the single opsTask world) would've just glommed onto the
631 // end of the single opsTask but referred to a far earlier RT need to appear in their
632 // own opsTask.
633 fActiveOpsTask->makeClosed(fContext);
634 fActiveOpsTask = nullptr;
635 }
636
637 // Propagate the DDL proxy's state information to the replay target.
638 if (ddl->priv().targetProxy()->isMSAADirty()) {
639 auto nativeRect = GrNativeRect::MakeIRectRelativeTo(
640 ddl->characterization().origin(),
641 ddl->priv().targetProxy()->backingStoreDimensions().height(),
642 ddl->priv().targetProxy()->msaaDirtyRect());
643 newDest->markMSAADirty(nativeRect);
644 }
645 GrTextureProxy* newTextureProxy = newDest->asTextureProxy();
646 if (newTextureProxy && skgpu::Mipmapped::kYes == newTextureProxy->mipmapped()) {
647 newTextureProxy->markMipmapsDirty();
648 }
649
650 // Here we jam the proxy that backs the current replay SkSurface into the LazyProxyData.
651 // The lazy proxy that references it (in the DDL opsTasks) will then steal its GrTexture.
652 ddl->fLazyProxyData->fReplayDest = newDest.get();
653
654 // Add a task to handle drawing and lifetime management of the DDL.
655 SkDEBUGCODE(auto ddlTask =) this->appendTask(sk_make_sp<GrDDLTask>(this,
656 std::move(newDest),
657 std::move(ddl)));
658 SkASSERT(ddlTask->isClosed());
659
660 SkDEBUGCODE(this->validate());
661 }
662
663 #ifdef SK_DEBUG
664 void GrDrawingManager::validate() const {
665 if (fActiveOpsTask) {
666 SkASSERT(!fDAG.empty());
667 SkASSERT(!fActiveOpsTask->isClosed());
668 SkASSERT(fActiveOpsTask == fDAG.back().get());
669 }
670
671 for (int i = 0; i < fDAG.size(); ++i) {
672 if (fActiveOpsTask != fDAG[i].get()) {
673 // The resolveTask associated with the activeTask remains open for as long as the
674 // activeTask does.
675 bool isActiveResolveTask =
676 fActiveOpsTask && fActiveOpsTask->fTextureResolveTask == fDAG[i].get();
677 bool isAtlas = fDAG[i]->isSetFlag(GrRenderTask::kAtlas_Flag);
678 SkASSERT(isActiveResolveTask || isAtlas || fDAG[i]->isClosed());
679 }
680 }
681
682 // The active opsTask, if any, should always be at the back of the DAG.
683 if (!fDAG.empty()) {
684 if (fDAG.back()->isSetFlag(GrRenderTask::kAtlas_Flag)) {
685 SkASSERT(fActiveOpsTask == nullptr);
686 SkASSERT(!fDAG.back()->isClosed());
687 } else if (fDAG.back()->isClosed()) {
688 SkASSERT(fActiveOpsTask == nullptr);
689 } else {
690 SkASSERT(fActiveOpsTask == fDAG.back().get());
691 }
692 } else {
693 SkASSERT(fActiveOpsTask == nullptr);
694 }
695 }
696 #endif // SK_DEBUG
697
698 void GrDrawingManager::closeActiveOpsTask() {
699 if (fActiveOpsTask) {
700 // This is a temporary fix for the partial-MDB world. In that world we're not
701 // reordering so ops that (in the single opsTask world) would've just glommed onto the
702 // end of the single opsTask but referred to a far earlier RT need to appear in their
703 // own opsTask.
704 fActiveOpsTask->makeClosed(fContext);
705 fActiveOpsTask = nullptr;
706 }
707 }
708
709 sk_sp<skgpu::ganesh::OpsTask> GrDrawingManager::newOpsTask(GrSurfaceProxyView surfaceView,
710 sk_sp<GrArenas> arenas) {
711 SkDEBUGCODE(this->validate());
712 SkASSERT(fContext);
713
714 this->closeActiveOpsTask();
715
716 sk_sp<skgpu::ganesh::OpsTask> opsTask(new skgpu::ganesh::OpsTask(
717 this, std::move(surfaceView), fContext->priv().auditTrail(), std::move(arenas)));
718
719 SkASSERT(this->getLastRenderTask(opsTask->target(0)) == opsTask.get());
720
721 this->appendTask(opsTask);
722
723 fActiveOpsTask = opsTask.get();
724
725 SkDEBUGCODE(this->validate());
726 return opsTask;
727 }
728
729 void GrDrawingManager::addAtlasTask(sk_sp<GrRenderTask> atlasTask,
730 GrRenderTask* previousAtlasTask) {
731 SkDEBUGCODE(this->validate());
732 SkASSERT(fContext);
733
734 if (previousAtlasTask) {
735 previousAtlasTask->makeClosed(fContext);
736 for (GrRenderTask* previousAtlasUser : previousAtlasTask->dependents()) {
737 // Make the new atlas depend on everybody who used the old atlas, and close their tasks.
738 // This guarantees that the previous atlas is totally out of service before we render
739 // the next one, meaning there is only ever one atlas active at a time and that they can
740 // all share the same texture.
741 atlasTask->addDependency(previousAtlasUser);
742 previousAtlasUser->makeClosed(fContext);
743 if (previousAtlasUser == fActiveOpsTask) {
744 fActiveOpsTask = nullptr;
745 }
746 }
747 }
748
749 atlasTask->setFlag(GrRenderTask::kAtlas_Flag);
750 this->insertTaskBeforeLast(std::move(atlasTask));
751
752 SkDEBUGCODE(this->validate());
753 }
754
755 GrTextureResolveRenderTask* GrDrawingManager::newTextureResolveRenderTaskBefore(
756 const GrCaps& caps) {
757 // Unlike in the "new opsTask" case, we do not want to close the active opsTask, nor (if we are
758 // in sorting and opsTask reduction mode) the render tasks that depend on any proxy's current
759 // state. This is because those opsTasks can still receive new ops and because if they refer to
760 // the mipmapped version of 'proxy', they will then come to depend on the render task being
761 // created here.
762 //
763 // Add the new textureResolveTask before the fActiveOpsTask (if not in
764 // sorting/opsTask-splitting-reduction mode) because it will depend upon this resolve task.
765 // NOTE: Putting it here will also reduce the amount of work required by the topological sort.
766 GrRenderTask* task = this->insertTaskBeforeLast(sk_make_sp<GrTextureResolveRenderTask>());
767 return static_cast<GrTextureResolveRenderTask*>(task);
768 }
769
770 void GrDrawingManager::newTextureResolveRenderTask(sk_sp<GrSurfaceProxy> proxy,
771 GrSurfaceProxy::ResolveFlags flags,
772 const GrCaps& caps) {
773 SkDEBUGCODE(this->validate());
774 SkASSERT(fContext);
775
776 if (!proxy->requiresManualMSAAResolve()) {
777 SkDEBUGCODE(this->validate());
778 return;
779 }
780
781 GrRenderTask* lastTask = this->getLastRenderTask(proxy.get());
782 if (!proxy->asRenderTargetProxy()->isMSAADirty() && (!lastTask || lastTask->isClosed())) {
783 SkDEBUGCODE(this->validate());
784 return;
785 }
786
787 this->closeActiveOpsTask();
788
789 auto resolveTask = sk_make_sp<GrTextureResolveRenderTask>();
790 // Add proxy also adds all the needed dependencies we need
791 resolveTask->addProxy(this, std::move(proxy), flags, caps);
792
793 auto task = this->appendTask(std::move(resolveTask));
794 task->makeClosed(fContext);
795
796 // We have closed the previous active oplist but since a new oplist isn't being added there
797 // shouldn't be an active one.
798 SkASSERT(!fActiveOpsTask);
799 SkDEBUGCODE(this->validate());
800 }
801
802 void GrDrawingManager::newWaitRenderTask(const sk_sp<GrSurfaceProxy>& proxy,
803 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> semaphores,
804 int numSemaphores) {
805 SkDEBUGCODE(this->validate());
806 SkASSERT(fContext);
807
808 sk_sp<GrWaitRenderTask> waitTask = sk_make_sp<GrWaitRenderTask>(GrSurfaceProxyView(proxy),
809 std::move(semaphores),
810 numSemaphores);
811
812 if (fActiveOpsTask && (fActiveOpsTask->target(0) == proxy.get())) {
813 SkASSERT(this->getLastRenderTask(proxy.get()) == fActiveOpsTask);
814 this->insertTaskBeforeLast(waitTask);
815 // In this case we keep the current renderTask open but just insert the new waitTask
816 // before it in the list. The waitTask will never need to trigger any resolves or mip
817 // map generation which is the main advantage of going through the proxy version.
818 // Additionally we would've had to temporarily set the wait task as the lastRenderTask
819 // on the proxy, add the dependency, and then reset the lastRenderTask to
820 // fActiveOpsTask. Additionally we make the waitTask depend on all of fActiveOpsTask
821 // dependencies so that we don't unnecessarily reorder the waitTask before them.
822 // Note: Any previous Ops already in fActiveOpsTask will get blocked by the wait
823 // semaphore even though they don't need to be for correctness.
824
825 // Make sure we add the dependencies of fActiveOpsTask to waitTask first or else we'll
826 // get a circular self dependency of waitTask on waitTask.
827 waitTask->addDependenciesFromOtherTask(fActiveOpsTask);
828 fActiveOpsTask->addDependency(waitTask.get());
829 } else {
830 // In this case we just close the previous RenderTask and start and append the waitTask
831 // to the DAG. Since it is the last task now we call setLastRenderTask on the proxy. If
832 // there is a lastTask on the proxy we make waitTask depend on that task. This
833 // dependency isn't strictly needed but it does keep the DAG from reordering the
834 // waitTask earlier and blocking more tasks.
835 if (GrRenderTask* lastTask = this->getLastRenderTask(proxy.get())) {
836 waitTask->addDependency(lastTask);
837 }
838 this->setLastRenderTask(proxy.get(), waitTask.get());
839 this->closeActiveOpsTask();
840 this->appendTask(waitTask);
841 }
842 waitTask->makeClosed(fContext);
843
844 SkDEBUGCODE(this->validate());
845 }
846
847 void GrDrawingManager::newTransferFromRenderTask(const sk_sp<GrSurfaceProxy>& srcProxy,
848 const SkIRect& srcRect,
849 GrColorType surfaceColorType,
850 GrColorType dstColorType,
851 sk_sp<GrGpuBuffer> dstBuffer,
852 size_t dstOffset) {
853 SkDEBUGCODE(this->validate());
854 SkASSERT(fContext);
855 this->closeActiveOpsTask();
856
857 GrRenderTask* task = this->appendTask(sk_make_sp<GrTransferFromRenderTask>(
858 srcProxy, srcRect, surfaceColorType, dstColorType,
859 std::move(dstBuffer), dstOffset));
860
861 const GrCaps& caps = *fContext->priv().caps();
862
863 // We always say skgpu::Mipmapped::kNo here since we are always just copying from the base
864 // layer. We don't need to make sure the whole mip map chain is valid.
865 task->addDependency(
866 this, srcProxy.get(), skgpu::Mipmapped::kNo, GrTextureResolveManager(this), caps);
867 task->makeClosed(fContext);
868
869 // We have closed the previous active oplist but since a new oplist isn't being added there
870 // shouldn't be an active one.
871 SkASSERT(!fActiveOpsTask);
872 SkDEBUGCODE(this->validate());
873 }
874
875 void GrDrawingManager::newBufferTransferTask(sk_sp<GrGpuBuffer> src,
876 size_t srcOffset,
877 sk_sp<GrGpuBuffer> dst,
878 size_t dstOffset,
879 size_t size) {
880 SkASSERT(src);
881 SkASSERT(dst);
882 SkASSERT(srcOffset + size <= src->size());
883 SkASSERT(dstOffset + size <= dst->size());
884 SkASSERT(src->intendedType() == GrGpuBufferType::kXferCpuToGpu);
885 SkASSERT(dst->intendedType() != GrGpuBufferType::kXferCpuToGpu);
886
887 SkDEBUGCODE(this->validate());
888 SkASSERT(fContext);
889
890 this->closeActiveOpsTask();
891
892 sk_sp<GrRenderTask> task = GrBufferTransferRenderTask::Make(std::move(src),
893 srcOffset,
894 std::move(dst),
895 dstOffset,
896 size);
897 SkASSERT(task);
898
899 this->appendTask(task);
900 task->makeClosed(fContext);
901
902 // We have closed the previous active oplist but since a new oplist isn't being added there
903 // shouldn't be an active one.
904 SkASSERT(!fActiveOpsTask);
905 SkDEBUGCODE(this->validate());
906 }
907
908 void GrDrawingManager::newBufferUpdateTask(sk_sp<SkData> src,
909 sk_sp<GrGpuBuffer> dst,
910 size_t dstOffset) {
911 SkASSERT(src);
912 SkASSERT(dst);
913 SkASSERT(dstOffset + src->size() <= dst->size());
914 SkASSERT(dst->intendedType() != GrGpuBufferType::kXferCpuToGpu);
915 SkASSERT(!dst->isMapped());
916
917 SkDEBUGCODE(this->validate());
918 SkASSERT(fContext);
919
920 this->closeActiveOpsTask();
921
922 sk_sp<GrRenderTask> task = GrBufferUpdateRenderTask::Make(std::move(src),
923 std::move(dst),
924 dstOffset);
925 SkASSERT(task);
926
927 this->appendTask(task);
928 task->makeClosed(fContext);
929
930 // We have closed the previous active oplist but since a new oplist isn't being added there
931 // shouldn't be an active one.
932 SkASSERT(!fActiveOpsTask);
933 SkDEBUGCODE(this->validate());
934 }
935
936 sk_sp<GrRenderTask> GrDrawingManager::newCopyRenderTask(sk_sp<GrSurfaceProxy> dst,
937 SkIRect dstRect,
938 const sk_sp<GrSurfaceProxy>& src,
939 SkIRect srcRect,
940 GrSamplerState::Filter filter,
941 GrSurfaceOrigin origin) {
942 SkDEBUGCODE(this->validate());
943 SkASSERT(fContext);
944
945 // It'd be nicer to check this in GrCopyRenderTask::Make. This gets complicated because of
946 // "active ops task" tracking. dst will be the target of our copy task but it might also be the
947 // target of the active ops task. We currently require the active ops task to be closed before
948 // making a new task that targets the same proxy. However, if we first close the active ops
949 // task, then fail to make a copy task, the next active ops task may target the same proxy. This
950 // will trip an assert related to unnecessary ops task splitting.
951 if (src->framebufferOnly()) {
952 return nullptr;
953 }
954
955 this->closeActiveOpsTask();
956
957 sk_sp<GrRenderTask> task = GrCopyRenderTask::Make(this,
958 std::move(dst),
959 dstRect,
960 src,
961 srcRect,
962 filter,
963 origin);
964 if (!task) {
965 return nullptr;
966 }
967
968 this->appendTask(task);
969
970 const GrCaps& caps = *fContext->priv().caps();
971 // We always say skgpu::Mipmapped::kNo here since we are always just copying from the base layer
972 // to another base layer. We don't need to make sure the whole mip map chain is valid.
973 task->addDependency(
974 this, src.get(), skgpu::Mipmapped::kNo, GrTextureResolveManager(this), caps);
975 task->makeClosed(fContext);
976
977 // We have closed the previous active oplist but since a new oplist isn't being added there
978 // shouldn't be an active one.
979 SkASSERT(!fActiveOpsTask);
980 SkDEBUGCODE(this->validate());
981 return task;
982 }
983
984 bool GrDrawingManager::newWritePixelsTask(sk_sp<GrSurfaceProxy> dst,
985 SkIRect rect,
986 GrColorType srcColorType,
987 GrColorType dstColorType,
988 const GrMipLevel levels[],
989 int levelCount) {
990 SkDEBUGCODE(this->validate());
991 SkASSERT(fContext);
992
993 this->closeActiveOpsTask();
994
995 GrRenderTask* task = this->appendTask(GrWritePixelsTask::Make(this,
996 std::move(dst),
997 rect,
998 srcColorType,
999 dstColorType,
1000 levels,
1001 levelCount));
1002 if (!task) {
1003 return false;
1004 }
1005
1006 task->makeClosed(fContext);
1007
1008 // We have closed the previous active oplist but since a new oplist isn't being added there
1009 // shouldn't be an active one.
1010 SkASSERT(!fActiveOpsTask);
1011 SkDEBUGCODE(this->validate());
1012 return true;
1013 }
1014
1015 /*
1016 * This method finds a path renderer that can draw the specified path on
1017 * the provided target.
1018 * Due to its expense, the software path renderer has split out so it can
1019 * can be individually allowed/disallowed via the "allowSW" boolean.
1020 */
1021 skgpu::ganesh::PathRenderer* GrDrawingManager::getPathRenderer(
1022 const PathRenderer::CanDrawPathArgs& args,
1023 bool allowSW,
1024 PathRendererChain::DrawType drawType,
1025 PathRenderer::StencilSupport* stencilSupport) {
1026 if (!fPathRendererChain) {
1027 fPathRendererChain =
1028 std::make_unique<PathRendererChain>(fContext, fOptionsForPathRendererChain);
1029 }
1030
1031 auto pr = fPathRendererChain->getPathRenderer(args, drawType, stencilSupport);
1032 if (!pr && allowSW) {
1033 auto swPR = this->getSoftwarePathRenderer();
1034 if (PathRenderer::CanDrawPath::kNo != swPR->canDrawPath(args)) {
1035 pr = swPR;
1036 }
1037 }
1038
1039 #if GR_PATH_RENDERER_SPEW
1040 if (pr) {
1041 SkDebugf("getPathRenderer: %s\n", pr->name());
1042 }
1043 #endif
1044
1045 return pr;
1046 }
1047
1048 skgpu::ganesh::PathRenderer* GrDrawingManager::getSoftwarePathRenderer() {
1049 if (!fSoftwarePathRenderer) {
1050 fSoftwarePathRenderer.reset(new skgpu::ganesh::SoftwarePathRenderer(
1051 fContext->priv().proxyProvider(),
1052 fOptionsForPathRendererChain.fAllowPathMaskCaching));
1053 }
1054 return fSoftwarePathRenderer.get();
1055 }
1056
1057 skgpu::ganesh::AtlasPathRenderer* GrDrawingManager::getAtlasPathRenderer() {
1058 if (!fPathRendererChain) {
1059 fPathRendererChain = std::make_unique<PathRendererChain>(fContext,
1060 fOptionsForPathRendererChain);
1061 }
1062 return fPathRendererChain->getAtlasPathRenderer();
1063 }
1064
1065 skgpu::ganesh::PathRenderer* GrDrawingManager::getTessellationPathRenderer() {
1066 if (!fPathRendererChain) {
1067 fPathRendererChain = std::make_unique<PathRendererChain>(fContext,
1068 fOptionsForPathRendererChain);
1069 }
1070 return fPathRendererChain->getTessellationPathRenderer();
1071 }