Debian Codegraph @

src/gpu/ganesh/GrDrawingManager.cpp

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