blob: 49c63e4c23cf5e00ebe626f3d477ea751e165127 [file] [log] [blame]
robertphillips3dc6ae52015-10-20 09:54:32 -07001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/GrDrawingManager.h"
Robert Phillips69893702019-02-22 11:16:30 -05009
Adlai Hollerc2bfcff2020-11-06 15:39:36 -050010#include <algorithm>
John Stilesfbd050b2020-08-03 13:21:46 -040011#include <memory>
12
Robert Phillips4d5594d2020-02-21 14:24:40 -050013#include "include/core/SkDeferredDisplayList.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/gpu/GrBackendSemaphore.h"
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040015#include "include/gpu/GrDirectContext.h"
16#include "include/gpu/GrRecordingContext.h"
Robert Phillips7eb0a5f2020-06-09 14:15:09 -040017#include "src/core/SkDeferredDisplayListPriv.h"
Adlai Holler78036082021-01-07 11:41:33 -050018#include "src/core/SkTInternalLList.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040019#include "src/gpu/GrAuditTrail.h"
Brian Salomon9241a6d2019-10-03 13:26:54 -040020#include "src/gpu/GrClientMappedBufferManager.h"
Greg Daniele227fe42019-08-21 13:52:24 -040021#include "src/gpu/GrCopyRenderTask.h"
Robert Phillips07f675d2020-11-16 13:44:01 -050022#include "src/gpu/GrDDLTask.h"
Adlai Hollera0693042020-10-14 11:23:11 -040023#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/GrGpu.h"
25#include "src/gpu/GrMemoryPool.h"
26#include "src/gpu/GrOnFlushResourceProvider.h"
27#include "src/gpu/GrRecordingContextPriv.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040028#include "src/gpu/GrRenderTargetProxy.h"
Chris Dalton6b498102019-08-01 14:14:52 -060029#include "src/gpu/GrRenderTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/gpu/GrResourceAllocator.h"
31#include "src/gpu/GrResourceProvider.h"
32#include "src/gpu/GrSoftwarePathRenderer.h"
Greg Daniel46e366a2019-12-16 14:38:36 -050033#include "src/gpu/GrSurfaceContext.h"
Brian Salomoneebe7352020-12-09 16:37:04 -050034#include "src/gpu/GrSurfaceDrawContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050035#include "src/gpu/GrSurfaceProxyPriv.h"
Adlai Holler78036082021-01-07 11:41:33 -050036#include "src/gpu/GrTCluster.h"
Robert Phillipsfbbc3bb2020-11-16 14:58:56 -050037#include "src/gpu/GrTTopoSort.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000038#include "src/gpu/GrTexture.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040039#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050040#include "src/gpu/GrTextureProxyPriv.h"
Chris Dalton3d770272019-08-14 09:24:37 -060041#include "src/gpu/GrTextureResolveRenderTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050042#include "src/gpu/GrTracing.h"
Greg Danielbbfec9d2019-08-20 10:56:51 -040043#include "src/gpu/GrTransferFromRenderTask.h"
Greg Danielc30f1a92019-09-06 15:28:58 -040044#include "src/gpu/GrWaitRenderTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050045#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
Herb Derbya08bde62020-06-12 15:46:06 -040046#include "src/gpu/text/GrSDFTOptions.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050047#include "src/image/SkSurface_Gpu.h"
robertphillips498d7ac2015-10-30 10:11:30 -070048
Robert Phillips22310d62018-09-05 11:07:21 -040049///////////////////////////////////////////////////////////////////////////////////////////////////
Robert Phillips69893702019-02-22 11:16:30 -050050GrDrawingManager::GrDrawingManager(GrRecordingContext* context,
Robert Phillips22310d62018-09-05 11:07:21 -040051 const GrPathRendererChain::Options& optionsForPathRendererChain,
Greg Danielf41b2bd2019-08-22 16:19:24 -040052 bool reduceOpsTaskSplitting)
Robert Phillips22310d62018-09-05 11:07:21 -040053 : fContext(context)
54 , fOptionsForPathRendererChain(optionsForPathRendererChain)
Robert Phillips22310d62018-09-05 11:07:21 -040055 , fPathRendererChain(nullptr)
56 , fSoftwarePathRenderer(nullptr)
Robert Phillips6db27c22019-05-01 10:43:56 -040057 , fFlushing(false)
Herb Derby082232b2020-06-10 15:08:18 -040058 , fReduceOpsTaskSplitting(reduceOpsTaskSplitting) { }
Robert Phillips22310d62018-09-05 11:07:21 -040059
robertphillips3dc6ae52015-10-20 09:54:32 -070060GrDrawingManager::~GrDrawingManager() {
Adlai Holler33432272020-11-11 13:54:37 -050061 this->closeAllTasks();
62 this->removeRenderTasks(0, fDAG.count());
robertphillips3dc6ae52015-10-20 09:54:32 -070063}
64
Robert Phillipsa9162df2019-02-11 14:12:03 -050065bool GrDrawingManager::wasAbandoned() const {
Robert Phillips9eb00022020-06-30 15:30:12 -040066 return fContext->abandoned();
robertphillips3dc6ae52015-10-20 09:54:32 -070067}
68
robertphillips68737822015-10-29 12:12:21 -070069void GrDrawingManager::freeGpuResources() {
Jim Van Verth106b5c42017-09-26 12:45:29 -040070 for (int i = fOnFlushCBObjects.count() - 1; i >= 0; --i) {
71 if (!fOnFlushCBObjects[i]->retainOnFreeGpuResources()) {
72 // it's safe to just do this because we're iterating in reverse
73 fOnFlushCBObjects.removeShuffle(i);
74 }
75 }
76
robertphillips68737822015-10-29 12:12:21 -070077 // a path renderer may be holding onto resources
robertphillips13391dd2015-10-30 05:15:11 -070078 fPathRendererChain = nullptr;
Ben Wagner9ec70c62018-07-12 13:30:47 -040079 fSoftwarePathRenderer = nullptr;
Robert Phillipse3302df2017-04-24 07:31:02 -040080}
81
Adlai Hollerc2bfcff2020-11-06 15:39:36 -050082// MDB TODO: make use of the 'proxies' parameter.
Greg Daniel9efe3862020-06-11 11:51:06 -040083bool GrDrawingManager::flush(
Adlai Hollerc2bfcff2020-11-06 15:39:36 -050084 SkSpan<GrSurfaceProxy*> proxies,
Greg Daniel9efe3862020-06-11 11:51:06 -040085 SkSurface::BackendSurfaceAccess access,
86 const GrFlushInfo& info,
87 const GrBackendSurfaceMutableState* newState) {
Brian Salomon57d2beab2018-09-10 09:35:41 -040088 GR_CREATE_TRACE_MARKER_CONTEXT("GrDrawingManager", "flush", fContext);
Brian Salomondcbb9d92017-07-19 10:53:20 -040089
robertphillips7761d612016-05-16 09:14:53 -070090 if (fFlushing || this->wasAbandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -040091 if (info.fSubmittedProc) {
92 info.fSubmittedProc(info.fSubmittedContext, false);
93 }
Greg Daniele6bfb7d2019-04-17 15:26:11 -040094 if (info.fFinishedProc) {
95 info.fFinishedProc(info.fFinishedContext);
Greg Daniela3aa75a2019-04-12 14:24:55 -040096 }
Greg Danielfe159622020-04-10 17:43:51 +000097 return false;
joshualittb8918c42015-12-18 09:59:46 -080098 }
Robert Phillips602df412019-04-08 11:10:39 -040099
Robert Phillips38d64b02018-09-04 13:23:26 -0400100 SkDEBUGCODE(this->validate());
101
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500102 // As of now we only short-circuit if we got an explicit list of surfaces to flush.
103 if (!proxies.empty() && !info.fNumSemaphores && !info.fFinishedProc &&
Greg Danielce9f0162020-06-30 13:42:46 -0400104 access == SkSurface::BackendSurfaceAccess::kNoAccess && !newState) {
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500105 bool allUnused = std::all_of(proxies.begin(), proxies.end(), [&](GrSurfaceProxy* proxy) {
Adlai Holler33432272020-11-11 13:54:37 -0500106 bool used = std::any_of(fDAG.begin(), fDAG.end(), [&](auto& task) {
Robert Phillips183461f2021-01-08 14:44:51 -0500107 // TODO: the DDLTask should now just return true here instead of
108 // needing GrDrawingManager::isDDLTarget.
Adlai Holler33432272020-11-11 13:54:37 -0500109 return task && task->isUsed(proxy);
110 });
111 return !used && !this->isDDLTarget(proxy);
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500112 });
113 if (allUnused) {
Greg Daniel55822f12020-05-26 11:26:45 -0400114 if (info.fSubmittedProc) {
115 info.fSubmittedProc(info.fSubmittedContext, true);
116 }
Greg Danielfe159622020-04-10 17:43:51 +0000117 return false;
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400118 }
Robert Phillips9313aa72019-04-09 18:41:27 -0400119 }
120
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400121 auto direct = fContext->asDirectContext();
Adlai Holleraee25fd2020-11-06 12:34:56 -0500122 SkASSERT(direct);
Brian Salomon9241a6d2019-10-03 13:26:54 -0400123 direct->priv().clientMappedBufferManager()->process();
Robert Phillips6a6de562019-02-15 15:19:15 -0500124
125 GrGpu* gpu = direct->priv().getGpu();
Greg Daniel55822f12020-05-26 11:26:45 -0400126 // We have a non abandoned and direct GrContext. It must have a GrGpu.
127 SkASSERT(gpu);
Greg Daniela3aa75a2019-04-12 14:24:55 -0400128
joshualittb8918c42015-12-18 09:59:46 -0800129 fFlushing = true;
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400130
Robert Phillips6a6de562019-02-15 15:19:15 -0500131 auto resourceProvider = direct->priv().resourceProvider();
132 auto resourceCache = direct->priv().getResourceCache();
133
Chris Dalton6b498102019-08-01 14:14:52 -0600134 // Semi-usually the GrRenderTasks are already closed at this point, but sometimes Ganesh needs
Greg Danielf41b2bd2019-08-22 16:19:24 -0400135 // to flush mid-draw. In that case, the SkGpuDevice's opsTasks won't be closed but need to be
136 // flushed anyway. Closing such opsTasks here will mean new ones will be created to replace them
Chris Dalton6b498102019-08-01 14:14:52 -0600137 // if the SkGpuDevice(s) write to them again.
Adlai Holler33432272020-11-11 13:54:37 -0500138 this->closeAllTasks();
Adlai Holler039f90c2020-11-19 15:20:31 +0000139 fActiveOpsTask = nullptr;
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400140
Adlai Holler33432272020-11-11 13:54:37 -0500141 this->sortTasks();
Adlai Holler78036082021-01-07 11:41:33 -0500142
143 if (fReduceOpsTaskSplitting) {
144 this->reorderTasks();
145 }
146
Brian Salomon601ac802019-02-07 13:37:16 -0500147 if (!fCpuBufferCache) {
148 // We cache more buffers when the backend is using client side arrays. Otherwise, we
149 // expect each pool will use a CPU buffer as a staging buffer before uploading to a GPU
150 // buffer object. Each pool only requires one staging buffer at a time.
151 int maxCachedBuffers = fContext->priv().caps()->preferClientSideDynamicBuffers() ? 2 : 6;
152 fCpuBufferCache = GrBufferAllocPool::CpuBufferCache::Make(maxCachedBuffers);
Brian Salomon58f153c2018-10-18 21:51:15 -0400153 }
Robert Phillipsa4c93ac2017-05-18 11:40:04 -0400154
Robert Phillipse5f73282019-06-18 17:15:04 -0400155 GrOpFlushState flushState(gpu, resourceProvider, &fTokenTracker, fCpuBufferCache);
Robert Phillips40a29d72018-01-18 12:59:22 -0500156
Chris Daltonfe199b72017-05-05 11:26:15 -0400157 GrOnFlushResourceProvider onFlushProvider(this);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400158
Chris Dalton12658942017-10-05 19:45:25 -0600159 // Prepare any onFlush op lists (e.g. atlases).
Chris Daltonfe199b72017-05-05 11:26:15 -0400160 if (!fOnFlushCBObjects.empty()) {
Robert Phillips07f675d2020-11-16 13:44:01 -0500161 fFlushingRenderTaskIDs.reserve_back(fDAG.count());
162 for (const auto& task : fDAG) {
163 if (task) {
164 task->gatherIDs(&fFlushingRenderTaskIDs);
Adlai Holler33432272020-11-11 13:54:37 -0500165 }
166 }
Robert Phillips22310d62018-09-05 11:07:21 -0400167
Chris Daltonfe199b72017-05-05 11:26:15 -0400168 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
Adlai Holler9902cff2020-11-11 08:51:25 -0500169 onFlushCBObject->preFlush(&onFlushProvider, fFlushingRenderTaskIDs);
Chris Daltonc4b47352019-08-23 10:10:36 -0600170 }
171 for (const auto& onFlushRenderTask : fOnFlushRenderTasks) {
172 onFlushRenderTask->makeClosed(*fContext->priv().caps());
Chris Dalton706a6ff2017-11-29 22:01:06 -0700173#ifdef SK_DEBUG
Chris Daltonc4b47352019-08-23 10:10:36 -0600174 // OnFlush callbacks are invoked during flush, and are therefore expected to handle
175 // resource allocation & usage on their own. (No deferred or lazy proxies!)
176 onFlushRenderTask->visitTargetAndSrcProxies_debugOnly(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400177 [](GrSurfaceProxy* p, GrMipmapped mipMapped) {
Chris Daltonc4b47352019-08-23 10:10:36 -0600178 SkASSERT(!p->asTextureProxy() || !p->asTextureProxy()->texPriv().isDeferred());
Brian Salomonbeb7f522019-08-30 16:19:42 -0400179 SkASSERT(!p->isLazy());
Chris Dalton4ece96d2019-08-30 11:26:39 -0600180 if (p->requiresManualMSAAResolve()) {
181 // The onFlush callback is responsible for ensuring MSAA gets resolved.
182 SkASSERT(p->asRenderTargetProxy() && !p->asRenderTargetProxy()->isMSAADirty());
183 }
Brian Salomon7e67dca2020-07-21 09:27:25 -0400184 if (GrMipmapped::kYes == mipMapped) {
Chris Daltonc4b47352019-08-23 10:10:36 -0600185 // The onFlush callback is responsible for regenerating mips if needed.
Brian Salomon8c82a872020-07-21 12:09:58 -0400186 SkASSERT(p->asTextureProxy() && !p->asTextureProxy()->mipmapsAreDirty());
Chris Daltonc4b47352019-08-23 10:10:36 -0600187 }
188 });
Chris Dalton706a6ff2017-11-29 22:01:06 -0700189#endif
Chris Daltonc4b47352019-08-23 10:10:36 -0600190 onFlushRenderTask->prepare(&flushState);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400191 }
192 }
193
robertphillipsa13e2022015-11-11 12:01:09 -0800194#if 0
Brian Salomon09d994e2016-12-21 11:14:46 -0500195 // Enable this to print out verbose GrOp information
Adlai Hollerabdfd392020-10-19 09:00:36 -0400196 SkDEBUGCODE(SkDebugf("onFlush renderTasks (%d):\n", fOnFlushRenderTasks.count()));
Chris Daltonc4b47352019-08-23 10:10:36 -0600197 for (const auto& onFlushRenderTask : fOnFlushRenderTasks) {
Adlai Hollerabdfd392020-10-19 09:00:36 -0400198 SkDEBUGCODE(onFlushRenderTask->dump(/* printDependencies */ true);)
Chris Daltonc4b47352019-08-23 10:10:36 -0600199 }
Adlai Holler33432272020-11-11 13:54:37 -0500200 SkDEBUGCODE(SkDebugf("Normal renderTasks (%d):\n", fDAG.count()));
201 for (const auto& task : fDAG) {
202 SkDEBUGCODE(task->dump(/* printDependencies */ true);)
robertphillips3dc6ae52015-10-20 09:54:32 -0700203 }
robertphillipsa13e2022015-11-11 12:01:09 -0800204#endif
205
Robert Phillipseafd48a2017-11-16 07:52:08 -0500206 int startIndex, stopIndex;
207 bool flushed = false;
208
Robert Phillipsf8e25022017-11-08 15:24:31 -0500209 {
Adlai Holler33432272020-11-11 13:54:37 -0500210 GrResourceAllocator alloc(resourceProvider SkDEBUGCODE(, fDAG.count()));
211 for (int i = 0; i < fDAG.count(); ++i) {
212 if (fDAG[i]) {
213 fDAG[i]->gatherProxyIntervals(&alloc);
Robert Phillips22310d62018-09-05 11:07:21 -0400214 }
Greg Danielf41b2bd2019-08-22 16:19:24 -0400215 alloc.markEndOfOpsTask(i);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500216 }
Robert Phillipsc73666f2019-04-24 08:49:48 -0400217 alloc.determineRecyclability();
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400218
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500219 GrResourceAllocator::AssignError error = GrResourceAllocator::AssignError::kNoError;
Chris Dalton6b498102019-08-01 14:14:52 -0600220 int numRenderTasksExecuted = 0;
Brian Salomon577aa0f2018-11-30 13:32:23 -0500221 while (alloc.assign(&startIndex, &stopIndex, &error)) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500222 if (GrResourceAllocator::AssignError::kFailedProxyInstantiation == error) {
223 for (int i = startIndex; i < stopIndex; ++i) {
Adlai Holler33432272020-11-11 13:54:37 -0500224 GrRenderTask* renderTask = fDAG[i].get();
Chris Dalton6b498102019-08-01 14:14:52 -0600225 if (!renderTask) {
226 continue;
Robert Phillips01a91282018-07-26 08:03:04 -0400227 }
Chris Dalton6b498102019-08-01 14:14:52 -0600228 if (!renderTask->isInstantiated()) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400229 // No need to call the renderTask's handleInternalAllocationFailure
230 // since we will already skip executing the renderTask since it is not
231 // instantiated.
Chris Dalton6b498102019-08-01 14:14:52 -0600232 continue;
Robert Phillipsbbcb7f72018-05-31 11:16:19 -0400233 }
Chris Dalton6b498102019-08-01 14:14:52 -0600234 renderTask->handleInternalAllocationFailure();
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500235 }
Adlai Holler96ead542020-06-26 08:50:14 -0400236 this->removeRenderTasks(startIndex, stopIndex);
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500237 }
Robert Phillips4150eea2018-02-07 17:08:21 -0500238
Chris Dalton6b498102019-08-01 14:14:52 -0600239 if (this->executeRenderTasks(
240 startIndex, stopIndex, &flushState, &numRenderTasksExecuted)) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500241 flushed = true;
242 }
bsalomondc438982016-08-31 11:53:49 -0700243 }
Greg Danielc42b20b2017-10-04 10:34:49 -0400244 }
245
Chris Dalton91ab1552018-04-18 13:24:25 -0600246#ifdef SK_DEBUG
Adlai Holler33432272020-11-11 13:54:37 -0500247 for (const auto& task : fDAG) {
Adlai Holler96ead542020-06-26 08:50:14 -0400248 // All render tasks should have been cleared out by now – we only reset the array below to
249 // reclaim storage.
Adlai Holler33432272020-11-11 13:54:37 -0500250 SkASSERT(!task);
Chris Dalton91ab1552018-04-18 13:24:25 -0600251 }
252#endif
Adlai Holler25df1f72020-06-09 13:08:27 -0400253 fLastRenderTasks.reset();
Robert Phillips22310d62018-09-05 11:07:21 -0400254 fDAG.reset();
Robert Phillips15c91422019-05-07 16:54:48 -0400255 this->clearDDLTargets();
robertphillipsa13e2022015-11-11 12:01:09 -0800256
Robert Phillipsc994a932018-06-19 13:09:54 -0400257#ifdef SK_DEBUG
258 // In non-DDL mode this checks that all the flushed ops have been freed from the memory pool.
259 // When we move to partial flushes this assert will no longer be valid.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400260 // In DDL mode this check is somewhat superfluous since the memory for most of the ops/opsTasks
Robert Phillipsc994a932018-06-19 13:09:54 -0400261 // will be stored in the DDL's GrOpMemoryPools.
Herb Derbye32e1ab2020-10-27 10:29:46 -0400262 GrMemoryPool* opMemoryPool = fContext->priv().opMemoryPool();
Robert Phillipsc994a932018-06-19 13:09:54 -0400263 opMemoryPool->isEmpty();
264#endif
265
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500266 gpu->executeFlushInfo(proxies, access, info, newState);
robertphillipsa13e2022015-11-11 12:01:09 -0800267
Brian Salomon57d2beab2018-09-10 09:35:41 -0400268 // Give the cache a chance to purge resources that become purgeable due to flushing.
269 if (flushed) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500270 resourceCache->purgeAsNeeded();
Brian Salomon876a0172019-03-08 11:12:14 -0500271 flushed = false;
bsalomonb77a9072016-09-07 10:02:04 -0700272 }
Chris Daltonfe199b72017-05-05 11:26:15 -0400273 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
Adlai Holler9902cff2020-11-11 08:51:25 -0500274 onFlushCBObject->postFlush(fTokenTracker.nextTokenToFlush(), fFlushingRenderTaskIDs);
Brian Salomon876a0172019-03-08 11:12:14 -0500275 flushed = true;
276 }
277 if (flushed) {
278 resourceCache->purgeAsNeeded();
Chris Daltonfe199b72017-05-05 11:26:15 -0400279 }
Chris Dalton6b498102019-08-01 14:14:52 -0600280 fFlushingRenderTaskIDs.reset();
joshualittb8918c42015-12-18 09:59:46 -0800281 fFlushing = false;
Greg Daniel51316782017-08-02 15:10:09 +0000282
Greg Danielfe159622020-04-10 17:43:51 +0000283 return true;
284}
285
286bool GrDrawingManager::submitToGpu(bool syncToCpu) {
287 if (fFlushing || this->wasAbandoned()) {
288 return false;
289 }
290
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400291 auto direct = fContext->asDirectContext();
Greg Danielfe159622020-04-10 17:43:51 +0000292 if (!direct) {
293 return false; // Can't submit while DDL recording
294 }
295 GrGpu* gpu = direct->priv().getGpu();
296 return gpu->submitToGpu(syncToCpu);
robertphillips3dc6ae52015-10-20 09:54:32 -0700297}
298
Chris Dalton6b498102019-08-01 14:14:52 -0600299bool GrDrawingManager::executeRenderTasks(int startIndex, int stopIndex, GrOpFlushState* flushState,
300 int* numRenderTasksExecuted) {
Adlai Holler33432272020-11-11 13:54:37 -0500301 SkASSERT(startIndex <= stopIndex && stopIndex <= fDAG.count());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500302
Robert Phillips27483912018-04-20 12:43:18 -0400303#if GR_FLUSH_TIME_OP_SPEW
Greg Danielf41b2bd2019-08-22 16:19:24 -0400304 SkDebugf("Flushing opsTask: %d to %d out of [%d, %d]\n",
Adlai Holler33432272020-11-11 13:54:37 -0500305 startIndex, stopIndex, 0, fDAG.count());
Robert Phillips27483912018-04-20 12:43:18 -0400306 for (int i = startIndex; i < stopIndex; ++i) {
Adlai Holler33432272020-11-11 13:54:37 -0500307 if (fDAG[i]) {
Robert Phillips047d5bb2021-01-08 13:39:19 -0500308 SkString label;
309 label.printf("task %d/%d", i, fDAG.count());
310 fDAG[i]->dump(label, {}, true, true);
Robert Phillips1734dd32018-08-21 13:52:09 -0400311 }
Robert Phillips27483912018-04-20 12:43:18 -0400312 }
313#endif
314
Chris Dalton6b498102019-08-01 14:14:52 -0600315 bool anyRenderTasksExecuted = false;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500316
317 for (int i = startIndex; i < stopIndex; ++i) {
Adlai Holler33432272020-11-11 13:54:37 -0500318 GrRenderTask* renderTask = fDAG[i].get();
Greg Daniel15ecdf92019-08-30 15:35:23 -0400319 if (!renderTask || !renderTask->isInstantiated()) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500320 continue;
321 }
322
Chris Dalton6b498102019-08-01 14:14:52 -0600323 SkASSERT(renderTask->deferredProxiesAreInstantiated());
Robert Phillips22310d62018-09-05 11:07:21 -0400324
Chris Dalton6b498102019-08-01 14:14:52 -0600325 renderTask->prepare(flushState);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500326 }
327
328 // Upload all data to the GPU
329 flushState->preExecuteDraws();
330
Greg Danield2073452018-12-07 11:20:33 -0500331 // For Vulkan, if we have too many oplists to be flushed we end up allocating a lot of resources
332 // for each command buffer associated with the oplists. If this gets too large we can cause the
333 // devices to go OOM. In practice we usually only hit this case in our tests, but to be safe we
334 // put a cap on the number of oplists we will execute before flushing to the GPU to relieve some
335 // memory pressure.
Chris Dalton6b498102019-08-01 14:14:52 -0600336 static constexpr int kMaxRenderTasksBeforeFlush = 100;
Greg Danield2073452018-12-07 11:20:33 -0500337
Chris Daltonc4b47352019-08-23 10:10:36 -0600338 // Execute the onFlush renderTasks first, if any.
339 for (sk_sp<GrRenderTask>& onFlushRenderTask : fOnFlushRenderTasks) {
340 if (!onFlushRenderTask->execute(flushState)) {
341 SkDebugf("WARNING: onFlushRenderTask failed to execute.\n");
Robert Phillipseafd48a2017-11-16 07:52:08 -0500342 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600343 SkASSERT(onFlushRenderTask->unique());
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400344 onFlushRenderTask->disown(this);
Chris Daltonc4b47352019-08-23 10:10:36 -0600345 onFlushRenderTask = nullptr;
Chris Dalton6b498102019-08-01 14:14:52 -0600346 (*numRenderTasksExecuted)++;
347 if (*numRenderTasksExecuted >= kMaxRenderTasksBeforeFlush) {
Greg Danielfe159622020-04-10 17:43:51 +0000348 flushState->gpu()->submitToGpu(false);
Chris Dalton6b498102019-08-01 14:14:52 -0600349 *numRenderTasksExecuted = 0;
Greg Danield2073452018-12-07 11:20:33 -0500350 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500351 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600352 fOnFlushRenderTasks.reset();
Robert Phillipseafd48a2017-11-16 07:52:08 -0500353
354 // Execute the normal op lists.
355 for (int i = startIndex; i < stopIndex; ++i) {
Adlai Holler33432272020-11-11 13:54:37 -0500356 GrRenderTask* renderTask = fDAG[i].get();
Greg Daniel15ecdf92019-08-30 15:35:23 -0400357 if (!renderTask || !renderTask->isInstantiated()) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500358 continue;
359 }
360
Greg Daniel15ecdf92019-08-30 15:35:23 -0400361 if (renderTask->execute(flushState)) {
Chris Dalton6b498102019-08-01 14:14:52 -0600362 anyRenderTasksExecuted = true;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500363 }
Chris Dalton6b498102019-08-01 14:14:52 -0600364 (*numRenderTasksExecuted)++;
365 if (*numRenderTasksExecuted >= kMaxRenderTasksBeforeFlush) {
Greg Danielfe159622020-04-10 17:43:51 +0000366 flushState->gpu()->submitToGpu(false);
Chris Dalton6b498102019-08-01 14:14:52 -0600367 *numRenderTasksExecuted = 0;
Greg Danield2073452018-12-07 11:20:33 -0500368 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500369 }
370
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400371 SkASSERT(!flushState->opsRenderPass());
Robert Phillips40a29d72018-01-18 12:59:22 -0500372 SkASSERT(fTokenTracker.nextDrawToken() == fTokenTracker.nextTokenToFlush());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500373
Chris Dalton6b498102019-08-01 14:14:52 -0600374 // We reset the flush state before the RenderTasks so that the last resources to be freed are
375 // those that are written to in the RenderTasks. This helps to make sure the most recently used
376 // resources are the last to be purged by the resource cache.
Robert Phillipseafd48a2017-11-16 07:52:08 -0500377 flushState->reset();
378
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400379 this->removeRenderTasks(startIndex, stopIndex);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500380
Chris Dalton6b498102019-08-01 14:14:52 -0600381 return anyRenderTasksExecuted;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500382}
383
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400384void GrDrawingManager::removeRenderTasks(int startIndex, int stopIndex) {
385 for (int i = startIndex; i < stopIndex; ++i) {
Adlai Holler33432272020-11-11 13:54:37 -0500386 GrRenderTask* task = fDAG[i].get();
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400387 if (!task) {
388 continue;
389 }
Robert Phillips07f675d2020-11-16 13:44:01 -0500390 if (!task->unique() || task->requiresExplicitCleanup()) {
391 // TODO: Eventually uniqueness should be guaranteed: http://skbug.com/7111.
392 // DDLs, however, will always require an explicit notification for when they
393 // can clean up resources.
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400394 task->endFlush(this);
395 }
396 task->disown(this);
Adlai Holler33432272020-11-11 13:54:37 -0500397
398 // This doesn't cleanup any referring pointers (e.g. dependency pointers in the DAG).
399 // It works right now bc this is only called after the topological sort is complete
400 // (so the dangling pointers aren't used).
401 fDAG[i] = nullptr;
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400402 }
Adlai Holler33432272020-11-11 13:54:37 -0500403}
404
405void GrDrawingManager::sortTasks() {
Robert Phillipsfbbc3bb2020-11-16 14:58:56 -0500406 if (!GrTTopoSort<GrRenderTask, GrRenderTask::TopoSortTraits>(&fDAG)) {
Adlai Holler33432272020-11-11 13:54:37 -0500407 SkDEBUGFAIL("Render task topo sort failed.");
408 return;
409 }
410
411#ifdef SK_DEBUG
412 // This block checks for any unnecessary splits in the opsTasks. If two sequential opsTasks
413 // share the same backing GrSurfaceProxy it means the opsTask was artificially split.
414 if (!fDAG.empty()) {
415 GrOpsTask* prevOpsTask = fDAG[0]->asOpsTask();
416 for (int i = 1; i < fDAG.count(); ++i) {
417 GrOpsTask* curOpsTask = fDAG[i]->asOpsTask();
418
419 if (prevOpsTask && curOpsTask) {
420 SkASSERT(prevOpsTask->target(0).proxy() != curOpsTask->target(0).proxy());
421 }
422
423 prevOpsTask = curOpsTask;
424 }
425 }
426#endif
427}
428
Adlai Holler78036082021-01-07 11:41:33 -0500429// Reorder the array to match the llist without reffing & unreffing sk_sp's.
430// Both args must contain the same objects.
431// This is basically a shim because clustering uses LList but the rest of drawmgr uses array.
432template <typename T>
433static void reorder_array_by_llist(const SkTInternalLList<T>& llist, SkTArray<sk_sp<T>>* array) {
434 int i = 0;
435 for (T* t : llist) {
436 // Release the pointer that used to live here so it doesn't get unreffed.
437 [[maybe_unused]] T* old = array->at(i).release();
438 array->at(i++).reset(t);
439 }
440 SkASSERT(i == array->count());
441}
442
443void GrDrawingManager::reorderTasks() {
444 SkASSERT(fReduceOpsTaskSplitting);
445 SkTInternalLList<GrRenderTask> llist;
446 bool clustered = GrTCluster<GrRenderTask, GrRenderTask::ClusterTraits>(fDAG, &llist);
447 if (!clustered) {
448 return;
449 }
450 // TODO: Handle case where proposed order would blow our memory budget.
451 // Such cases are currently pathological, so we could just return here and keep current order.
452 reorder_array_by_llist(llist, &fDAG);
453}
454
Adlai Holler33432272020-11-11 13:54:37 -0500455void GrDrawingManager::closeAllTasks() {
456 const GrCaps& caps = *fContext->priv().caps();
457 for (auto& task : fDAG) {
458 if (task) {
459 task->makeClosed(caps);
460 }
461 }
462}
463
464GrRenderTask* GrDrawingManager::insertTaskBeforeLast(sk_sp<GrRenderTask> task) {
465 SkASSERT(!fDAG.empty());
466 if (!task) {
467 return nullptr;
468 }
469 // Release 'fDAG.back()' and grab the raw pointer, in case the SkTArray grows
470 // and reallocates during emplace_back.
471 // TODO: Either use std::vector that can do this for us, or use SkSTArray to get the
472 // perf win.
473 fDAG.emplace_back(fDAG.back().release());
474 return (fDAG[fDAG.count() - 2] = std::move(task)).get();
475}
476
477GrRenderTask* GrDrawingManager::appendTask(sk_sp<GrRenderTask> task) {
478 if (!task) {
479 return nullptr;
480 }
481 return fDAG.push_back(std::move(task)).get();
482}
483
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400484static void resolve_and_mipmap(GrGpu* gpu, GrSurfaceProxy* proxy) {
485 if (!proxy->isInstantiated()) {
486 return;
487 }
488
489 // In the flushSurfaces case, we need to resolve MSAA immediately after flush. This is
490 // because clients expect the flushed surface's backing texture to be fully resolved
491 // upon return.
492 if (proxy->requiresManualMSAAResolve()) {
493 auto* rtProxy = proxy->asRenderTargetProxy();
494 SkASSERT(rtProxy);
495 if (rtProxy->isMSAADirty()) {
496 SkASSERT(rtProxy->peekRenderTarget());
Jim Van Verthbb61fe32020-07-07 16:39:04 -0400497 gpu->resolveRenderTarget(rtProxy->peekRenderTarget(), rtProxy->msaaDirtyRect());
498 gpu->submitToGpu(false);
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400499 rtProxy->markMSAAResolved();
500 }
501 }
502 // If, after a flush, any of the proxies of interest have dirty mipmaps, regenerate them in
503 // case their backend textures are being stolen.
504 // (This special case is exercised by the ReimportImageTextureWithMipLevels test.)
505 // FIXME: It may be more ideal to plumb down a "we're going to steal the backends" flag.
506 if (auto* textureProxy = proxy->asTextureProxy()) {
Brian Salomon8c82a872020-07-21 12:09:58 -0400507 if (textureProxy->mipmapsAreDirty()) {
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400508 SkASSERT(textureProxy->peekTexture());
509 gpu->regenerateMipMapLevels(textureProxy->peekTexture());
Brian Salomon8c82a872020-07-21 12:09:58 -0400510 textureProxy->markMipmapsClean();
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400511 }
512 }
513}
514
Greg Daniel9efe3862020-06-11 11:51:06 -0400515GrSemaphoresSubmitted GrDrawingManager::flushSurfaces(
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500516 SkSpan<GrSurfaceProxy*> proxies,
Greg Daniel9efe3862020-06-11 11:51:06 -0400517 SkSurface::BackendSurfaceAccess access,
518 const GrFlushInfo& info,
519 const GrBackendSurfaceMutableState* newState) {
bsalomon6a2b1942016-09-08 11:28:59 -0700520 if (this->wasAbandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -0400521 if (info.fSubmittedProc) {
522 info.fSubmittedProc(info.fSubmittedContext, false);
523 }
524 if (info.fFinishedProc) {
525 info.fFinishedProc(info.fFinishedContext);
526 }
Greg Daniel51316782017-08-02 15:10:09 +0000527 return GrSemaphoresSubmitted::kNo;
bsalomon6a2b1942016-09-08 11:28:59 -0700528 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400529 SkDEBUGCODE(this->validate());
bsalomon6a2b1942016-09-08 11:28:59 -0700530
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400531 auto direct = fContext->asDirectContext();
Adlai Holleraee25fd2020-11-06 12:34:56 -0500532 SkASSERT(direct);
Robert Phillips6a6de562019-02-15 15:19:15 -0500533 GrGpu* gpu = direct->priv().getGpu();
Greg Daniel55822f12020-05-26 11:26:45 -0400534 // We have a non abandoned and direct GrContext. It must have a GrGpu.
535 SkASSERT(gpu);
Robert Phillips874b5352018-03-16 08:48:24 -0400536
Robert Phillipsacc10fa2019-04-01 09:50:20 -0400537 // TODO: It is important to upgrade the drawingmanager to just flushing the
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400538 // portion of the DAG required by 'proxies' in order to restore some of the
Robert Phillipsacc10fa2019-04-01 09:50:20 -0400539 // semantics of this method.
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500540 bool didFlush = this->flush(proxies, access, info, newState);
541 for (GrSurfaceProxy* proxy : proxies) {
542 resolve_and_mipmap(gpu, proxy);
bsalomon6a2b1942016-09-08 11:28:59 -0700543 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400544
545 SkDEBUGCODE(this->validate());
Greg Danielfe159622020-04-10 17:43:51 +0000546
Greg Daniel04283f32020-05-20 13:16:00 -0400547 if (!didFlush || (!direct->priv().caps()->semaphoreSupport() && info.fNumSemaphores)) {
Greg Danielfe159622020-04-10 17:43:51 +0000548 return GrSemaphoresSubmitted::kNo;
549 }
550 return GrSemaphoresSubmitted::kYes;
bsalomon6a2b1942016-09-08 11:28:59 -0700551}
552
Chris Daltonfe199b72017-05-05 11:26:15 -0400553void GrDrawingManager::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
554 fOnFlushCBObjects.push_back(onFlushCBObject);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400555}
556
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500557#if GR_TEST_UTILS
558void GrDrawingManager::testingOnly_removeOnFlushCallbackObject(GrOnFlushCallbackObject* cb) {
559 int n = std::find(fOnFlushCBObjects.begin(), fOnFlushCBObjects.end(), cb) -
560 fOnFlushCBObjects.begin();
561 SkASSERT(n < fOnFlushCBObjects.count());
562 fOnFlushCBObjects.removeShuffle(n);
563}
564#endif
565
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400566void GrDrawingManager::setLastRenderTask(const GrSurfaceProxy* proxy, GrRenderTask* task) {
567#ifdef SK_DEBUG
Robert Phillips07f675d2020-11-16 13:44:01 -0500568 if (auto prior = this->getLastRenderTask(proxy)) {
569 SkASSERT(prior->isClosed() || prior == task);
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400570 }
571#endif
Adlai Holler25df1f72020-06-09 13:08:27 -0400572 uint32_t key = proxy->uniqueID().asUInt();
573 if (task) {
574 fLastRenderTasks.set(key, task);
575 } else if (fLastRenderTasks.find(key)) {
576 fLastRenderTasks.remove(key);
577 }
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400578}
579
580GrRenderTask* GrDrawingManager::getLastRenderTask(const GrSurfaceProxy* proxy) const {
581 auto entry = fLastRenderTasks.find(proxy->uniqueID().asUInt());
582 return entry ? *entry : nullptr;
583}
584
585GrOpsTask* GrDrawingManager::getLastOpsTask(const GrSurfaceProxy* proxy) const {
586 GrRenderTask* task = this->getLastRenderTask(proxy);
587 return task ? task->asOpsTask() : nullptr;
588}
589
590
Chris Dalton6b498102019-08-01 14:14:52 -0600591void GrDrawingManager::moveRenderTasksToDDL(SkDeferredDisplayList* ddl) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400592 SkDEBUGCODE(this->validate());
593
Chris Dalton6b498102019-08-01 14:14:52 -0600594 // no renderTask should receive a new command after this
Adlai Holler33432272020-11-11 13:54:37 -0500595 this->closeAllTasks();
Adlai Holler039f90c2020-11-19 15:20:31 +0000596 fActiveOpsTask = nullptr;
Robert Phillips7a137052018-02-01 11:23:12 -0500597
Robert Phillips07f675d2020-11-16 13:44:01 -0500598 this->sortTasks();
599
Adlai Holler33432272020-11-11 13:54:37 -0500600 fDAG.swap(ddl->fRenderTasks);
601 SkASSERT(fDAG.empty());
Robert Phillips867ce8f2018-06-21 10:28:36 -0400602
Robert Phillips19f466d2020-02-26 10:27:07 -0500603 for (auto& renderTask : ddl->fRenderTasks) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400604 renderTask->disown(this);
Robert Phillips29f38542019-10-16 09:20:25 -0400605 renderTask->prePrepare(fContext);
Robert Phillips7327c9d2019-10-08 16:32:56 -0400606 }
607
Michael Ludwig2c316bd2019-12-19 14:50:44 -0500608 ddl->fArenas = std::move(fContext->priv().detachArenas());
Robert Phillips61fc7992019-10-22 11:58:17 -0400609
Robert Phillipsf6a0b452020-02-18 14:26:46 -0500610 fContext->priv().detachProgramData(&ddl->fProgramData);
Robert Phillips576b6a12019-12-06 13:05:49 -0500611
Robert Phillips774168e2018-05-31 12:43:27 -0400612 if (fPathRendererChain) {
613 if (auto ccpr = fPathRendererChain->getCoverageCountingPathRenderer()) {
614 ddl->fPendingPaths = ccpr->detachPendingPaths();
615 }
616 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400617
618 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500619}
620
Robert Phillips07f675d2020-11-16 13:44:01 -0500621void GrDrawingManager::createDDLTask(sk_sp<const SkDeferredDisplayList> ddl,
Robert Phillips88b29612020-11-16 15:15:08 -0500622 GrRenderTargetProxy* newDest,
623 SkIPoint offset) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400624 SkDEBUGCODE(this->validate());
625
Adlai Holler039f90c2020-11-19 15:20:31 +0000626 if (fActiveOpsTask) {
627 // This is a temporary fix for the partial-MDB world. In that world we're not
628 // reordering so ops that (in the single opsTask world) would've just glommed onto the
629 // end of the single opsTask but referred to a far earlier RT need to appear in their
630 // own opsTask.
631 fActiveOpsTask->makeClosed(*fContext->priv().caps());
632 fActiveOpsTask = nullptr;
Robert Phillips38d64b02018-09-04 13:23:26 -0400633 }
634
Robert Phillips07f675d2020-11-16 13:44:01 -0500635 // Propagate the DDL proxy's state information to the replay target.
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400636 if (ddl->priv().targetProxy()->isMSAADirty()) {
637 newDest->markMSAADirty(ddl->priv().targetProxy()->msaaDirtyRect(),
638 ddl->characterization().origin());
639 }
640 GrTextureProxy* newTextureProxy = newDest->asTextureProxy();
Brian Salomon8c82a872020-07-21 12:09:58 -0400641 if (newTextureProxy && GrMipmapped::kYes == newTextureProxy->mipmapped()) {
642 newTextureProxy->markMipmapsDirty();
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400643 }
644
645 this->addDDLTarget(newDest, ddl->priv().targetProxy());
Robert Phillips15c91422019-05-07 16:54:48 -0400646
Robert Phillips62000362018-02-01 09:10:04 -0500647 // Here we jam the proxy that backs the current replay SkSurface into the LazyProxyData.
Robert Phillips07f675d2020-11-16 13:44:01 -0500648 // The lazy proxy that references it (in the DDL opsTasks) will then steal its GrTexture.
Robert Phillips62000362018-02-01 09:10:04 -0500649 ddl->fLazyProxyData->fReplayDest = newDest;
Robert Phillips774168e2018-05-31 12:43:27 -0400650
651 if (ddl->fPendingPaths.size()) {
652 GrCoverageCountingPathRenderer* ccpr = this->getCoverageCountingPathRenderer();
653
654 ccpr->mergePendingPaths(ddl->fPendingPaths);
655 }
Robert Phillips22310d62018-09-05 11:07:21 -0400656
Robert Phillips07f675d2020-11-16 13:44:01 -0500657 // Add a task to handle drawing and lifetime management of the DDL.
658 SkDEBUGCODE(auto ddlTask =) this->appendTask(sk_make_sp<GrDDLTask>(this,
659 sk_ref_sp(newDest),
Robert Phillips88b29612020-11-16 15:15:08 -0500660 std::move(ddl),
661 offset));
Robert Phillips07f675d2020-11-16 13:44:01 -0500662 SkASSERT(ddlTask->isClosed());
Adlai Holler7580ad42020-06-24 13:45:25 -0400663
Robert Phillips38d64b02018-09-04 13:23:26 -0400664 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500665}
666
Robert Phillips38d64b02018-09-04 13:23:26 -0400667#ifdef SK_DEBUG
668void GrDrawingManager::validate() const {
Adlai Holler4ef108e2020-12-30 16:21:13 +0000669 if (fActiveOpsTask) {
670 SkASSERT(!fDAG.empty());
671 SkASSERT(!fActiveOpsTask->isClosed());
672 SkASSERT(fActiveOpsTask == fDAG.back().get());
673 }
Adlai Holler039f90c2020-11-19 15:20:31 +0000674
Adlai Holler4ef108e2020-12-30 16:21:13 +0000675 for (int i = 0; i < fDAG.count(); ++i) {
676 if (fActiveOpsTask != fDAG[i].get()) {
677 // The resolveTask associated with the activeTask remains open for as long as the
678 // activeTask does.
679 bool isActiveResolveTask =
680 fActiveOpsTask && fActiveOpsTask->fTextureResolveTask == fDAG[i].get();
681 SkASSERT(isActiveResolveTask || fDAG[i]->isClosed());
Adlai Holler76893192020-12-22 19:41:19 +0000682 }
Adlai Holler4ef108e2020-12-30 16:21:13 +0000683 }
Adlai Holler76893192020-12-22 19:41:19 +0000684
Adlai Holler4ef108e2020-12-30 16:21:13 +0000685 if (!fDAG.empty() && !fDAG.back()->isClosed()) {
686 SkASSERT(fActiveOpsTask == fDAG.back().get());
Robert Phillips38d64b02018-09-04 13:23:26 -0400687 }
688}
689#endif
690
Robert Phillips183461f2021-01-08 14:44:51 -0500691void GrDrawingManager::addDDLTarget(GrSurfaceProxy* newTarget, GrRenderTargetProxy* ddlTarget) {
692 SkASSERT(ddlTarget->isDDLTarget());
693 fDDLTargets.set(newTarget->uniqueID().asUInt(), ddlTarget);
694}
695
Adlai Holler4ef108e2020-12-30 16:21:13 +0000696void GrDrawingManager::closeActiveOpsTask() {
697 if (fActiveOpsTask) {
698 // This is a temporary fix for the partial-MDB world. In that world we're not
Adlai Holler039f90c2020-11-19 15:20:31 +0000699 // reordering so ops that (in the single opsTask world) would've just glommed onto the
Greg Danielf41b2bd2019-08-22 16:19:24 -0400700 // end of the single opsTask but referred to a far earlier RT need to appear in their
701 // own opsTask.
Adlai Holler039f90c2020-11-19 15:20:31 +0000702 fActiveOpsTask->makeClosed(*fContext->priv().caps());
703 fActiveOpsTask = nullptr;
robertphillips3dc6ae52015-10-20 09:54:32 -0700704 }
Chris Dalton5fe99772019-08-06 11:57:39 -0600705}
706
Greg Daniel16f5c652019-10-29 11:26:01 -0400707sk_sp<GrOpsTask> GrDrawingManager::newOpsTask(GrSurfaceProxyView surfaceView,
Robert Phillips19006652020-11-19 14:20:57 -0500708 bool flushTimeOpsTask) {
Chris Dalton5fe99772019-08-06 11:57:39 -0600709 SkDEBUGCODE(this->validate());
710 SkASSERT(fContext);
711
Adlai Holler4ef108e2020-12-30 16:21:13 +0000712 this->closeActiveOpsTask();
robertphillips3dc6ae52015-10-20 09:54:32 -0700713
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400714 sk_sp<GrOpsTask> opsTask(new GrOpsTask(this, fContext->priv().arenas(),
Michael Ludwigd0840ec2019-12-12 09:48:38 -0500715 std::move(surfaceView),
716 fContext->priv().auditTrail()));
Adlai Holler4ef108e2020-12-30 16:21:13 +0000717 SkASSERT(this->getLastRenderTask(opsTask->target(0).proxy()) == opsTask.get());
robertphillips3dc6ae52015-10-20 09:54:32 -0700718
Robert Phillips19006652020-11-19 14:20:57 -0500719 if (flushTimeOpsTask) {
720 fOnFlushRenderTasks.push_back(opsTask);
721 } else {
Adlai Holler33432272020-11-11 13:54:37 -0500722 this->appendTask(opsTask);
Adlai Holler039f90c2020-11-19 15:20:31 +0000723
Adlai Holler4ef108e2020-12-30 16:21:13 +0000724 fActiveOpsTask = opsTask.get();
Robert Phillips941d1442017-06-14 16:37:02 -0400725 }
robertphillips3dc6ae52015-10-20 09:54:32 -0700726
Robert Phillips38d64b02018-09-04 13:23:26 -0400727 SkDEBUGCODE(this->validate());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400728 return opsTask;
robertphillips3dc6ae52015-10-20 09:54:32 -0700729}
730
Adlai Holler039f90c2020-11-19 15:20:31 +0000731GrTextureResolveRenderTask* GrDrawingManager::newTextureResolveRenderTask(const GrCaps& caps) {
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600732 // Unlike in the "new opsTask" case, we do not want to close the active opsTask, nor (if we are
Adlai Holler039f90c2020-11-19 15:20:31 +0000733 // in sorting and opsTask reduction mode) the render tasks that depend on any proxy's current
Greg Danielf41b2bd2019-08-22 16:19:24 -0400734 // state. This is because those opsTasks can still receive new ops and because if they refer to
Adlai Holler039f90c2020-11-19 15:20:31 +0000735 // the mipmapped version of 'proxy', they will then come to depend on the render task being
Chris Dalton4ece96d2019-08-30 11:26:39 -0600736 // created here.
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600737 //
Adlai Holler039f90c2020-11-19 15:20:31 +0000738 // Add the new textureResolveTask before the fActiveOpsTask (if not in
739 // sorting/opsTask-splitting-reduction mode) because it will depend upon this resolve task.
Chris Dalton3d770272019-08-14 09:24:37 -0600740 // NOTE: Putting it here will also reduce the amount of work required by the topological sort.
Adlai Holler33432272020-11-11 13:54:37 -0500741 GrRenderTask* task = this->insertTaskBeforeLast(sk_make_sp<GrTextureResolveRenderTask>());
742 return static_cast<GrTextureResolveRenderTask*>(task);
Chris Dalton3d770272019-08-14 09:24:37 -0600743}
744
Greg Danielc30f1a92019-09-06 15:28:58 -0400745void GrDrawingManager::newWaitRenderTask(sk_sp<GrSurfaceProxy> proxy,
Greg Daniel301015c2019-11-18 14:06:46 -0500746 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> semaphores,
Greg Danielc30f1a92019-09-06 15:28:58 -0400747 int numSemaphores) {
748 SkDEBUGCODE(this->validate());
749 SkASSERT(fContext);
750
751 const GrCaps& caps = *fContext->priv().caps();
752
Greg Daniel16f5c652019-10-29 11:26:01 -0400753 sk_sp<GrWaitRenderTask> waitTask = sk_make_sp<GrWaitRenderTask>(GrSurfaceProxyView(proxy),
754 std::move(semaphores),
Greg Danielc30f1a92019-09-06 15:28:58 -0400755 numSemaphores);
Greg Danielc30f1a92019-09-06 15:28:58 -0400756
Adlai Holler4ef108e2020-12-30 16:21:13 +0000757 if (fActiveOpsTask && (fActiveOpsTask->target(0).proxy() == proxy.get())) {
758 SkASSERT(this->getLastRenderTask(proxy.get()) == fActiveOpsTask);
759 this->insertTaskBeforeLast(waitTask);
760 // In this case we keep the current renderTask open but just insert the new waitTask
761 // before it in the list. The waitTask will never need to trigger any resolves or mip
762 // map generation which is the main advantage of going through the proxy version.
763 // Additionally we would've had to temporarily set the wait task as the lastRenderTask
764 // on the proxy, add the dependency, and then reset the lastRenderTask to
765 // fActiveOpsTask. Additionally we make the waitTask depend on all of fActiveOpsTask
766 // dependencies so that we don't unnecessarily reorder the waitTask before them.
767 // Note: Any previous Ops already in fActiveOpsTask will get blocked by the wait
768 // semaphore even though they don't need to be for correctness.
769
770 // Make sure we add the dependencies of fActiveOpsTask to waitTask first or else we'll
771 // get a circular self dependency of waitTask on waitTask.
772 waitTask->addDependenciesFromOtherTask(fActiveOpsTask);
773 fActiveOpsTask->addDependency(waitTask.get());
Adlai Holler76893192020-12-22 19:41:19 +0000774 } else {
Adlai Holler4ef108e2020-12-30 16:21:13 +0000775 // In this case we just close the previous RenderTask and start and append the waitTask
776 // to the DAG. Since it is the last task now we call setLastRenderTask on the proxy. If
777 // there is a lastTask on the proxy we make waitTask depend on that task. This
778 // dependency isn't strictly needed but it does keep the DAG from reordering the
779 // waitTask earlier and blocking more tasks.
780 if (GrRenderTask* lastTask = this->getLastRenderTask(proxy.get())) {
781 waitTask->addDependency(lastTask);
Adlai Holler76893192020-12-22 19:41:19 +0000782 }
Adlai Holler4ef108e2020-12-30 16:21:13 +0000783 this->setLastRenderTask(proxy.get(), waitTask.get());
784 this->closeActiveOpsTask();
785 this->appendTask(waitTask);
Greg Danielc30f1a92019-09-06 15:28:58 -0400786 }
787 waitTask->makeClosed(caps);
788
789 SkDEBUGCODE(this->validate());
790}
791
Greg Danielbbfec9d2019-08-20 10:56:51 -0400792void GrDrawingManager::newTransferFromRenderTask(sk_sp<GrSurfaceProxy> srcProxy,
793 const SkIRect& srcRect,
794 GrColorType surfaceColorType,
795 GrColorType dstColorType,
796 sk_sp<GrGpuBuffer> dstBuffer,
797 size_t dstOffset) {
798 SkDEBUGCODE(this->validate());
799 SkASSERT(fContext);
Adlai Holler4ef108e2020-12-30 16:21:13 +0000800 this->closeActiveOpsTask();
Greg Danielbbfec9d2019-08-20 10:56:51 -0400801
Adlai Holler33432272020-11-11 13:54:37 -0500802 GrRenderTask* task = this->appendTask(sk_make_sp<GrTransferFromRenderTask>(
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400803 srcProxy, srcRect, surfaceColorType, dstColorType,
804 std::move(dstBuffer), dstOffset));
Greg Danielbbfec9d2019-08-20 10:56:51 -0400805
806 const GrCaps& caps = *fContext->priv().caps();
807
Brian Salomon7e67dca2020-07-21 09:27:25 -0400808 // We always say GrMipmapped::kNo here since we are always just copying from the base layer. We
Greg Danielbbfec9d2019-08-20 10:56:51 -0400809 // don't need to make sure the whole mip map chain is valid.
Brian Salomon7e67dca2020-07-21 09:27:25 -0400810 task->addDependency(this, srcProxy.get(), GrMipmapped::kNo,
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400811 GrTextureResolveManager(this), caps);
Greg Danielbbfec9d2019-08-20 10:56:51 -0400812 task->makeClosed(caps);
813
Greg Danielbbfec9d2019-08-20 10:56:51 -0400814 // We have closed the previous active oplist but since a new oplist isn't being added there
815 // shouldn't be an active one.
Adlai Holler039f90c2020-11-19 15:20:31 +0000816 SkASSERT(!fActiveOpsTask);
Greg Danielbbfec9d2019-08-20 10:56:51 -0400817 SkDEBUGCODE(this->validate());
818}
819
Greg Daniel16f5c652019-10-29 11:26:01 -0400820bool GrDrawingManager::newCopyRenderTask(GrSurfaceProxyView srcView,
Greg Daniele227fe42019-08-21 13:52:24 -0400821 const SkIRect& srcRect,
Greg Daniel16f5c652019-10-29 11:26:01 -0400822 GrSurfaceProxyView dstView,
Greg Daniele227fe42019-08-21 13:52:24 -0400823 const SkIPoint& dstPoint) {
824 SkDEBUGCODE(this->validate());
825 SkASSERT(fContext);
Greg Daniele227fe42019-08-21 13:52:24 -0400826
Adlai Holler4ef108e2020-12-30 16:21:13 +0000827 this->closeActiveOpsTask();
Brian Salomone4bce012019-09-20 15:34:23 -0400828 const GrCaps& caps = *fContext->priv().caps();
829
Greg Daniel16f5c652019-10-29 11:26:01 -0400830 GrSurfaceProxy* srcProxy = srcView.proxy();
831
Brian Salomone4bce012019-09-20 15:34:23 -0400832 GrRenderTask* task =
Adlai Holler33432272020-11-11 13:54:37 -0500833 this->appendTask(GrCopyRenderTask::Make(this, std::move(srcView), srcRect,
834 std::move(dstView), dstPoint, &caps));
Greg Daniele227fe42019-08-21 13:52:24 -0400835 if (!task) {
836 return false;
837 }
838
Brian Salomon7e67dca2020-07-21 09:27:25 -0400839 // We always say GrMipmapped::kNo here since we are always just copying from the base layer to
Greg Daniele227fe42019-08-21 13:52:24 -0400840 // another base layer. We don't need to make sure the whole mip map chain is valid.
Brian Salomon7e67dca2020-07-21 09:27:25 -0400841 task->addDependency(this, srcProxy, GrMipmapped::kNo, GrTextureResolveManager(this), caps);
Greg Daniele227fe42019-08-21 13:52:24 -0400842 task->makeClosed(caps);
843
Greg Daniele227fe42019-08-21 13:52:24 -0400844 // We have closed the previous active oplist but since a new oplist isn't being added there
845 // shouldn't be an active one.
Adlai Holler039f90c2020-11-19 15:20:31 +0000846 SkASSERT(!fActiveOpsTask);
Greg Daniele227fe42019-08-21 13:52:24 -0400847 SkDEBUGCODE(this->validate());
848 return true;
849}
850
robertphillips68737822015-10-29 12:12:21 -0700851/*
852 * This method finds a path renderer that can draw the specified path on
853 * the provided target.
854 * Due to its expense, the software path renderer has split out so it can
855 * can be individually allowed/disallowed via the "allowSW" boolean.
856 */
857GrPathRenderer* GrDrawingManager::getPathRenderer(const GrPathRenderer::CanDrawPathArgs& args,
858 bool allowSW,
859 GrPathRendererChain::DrawType drawType,
860 GrPathRenderer::StencilSupport* stencilSupport) {
861
862 if (!fPathRendererChain) {
John Stilesfbd050b2020-08-03 13:21:46 -0400863 fPathRendererChain =
864 std::make_unique<GrPathRendererChain>(fContext, fOptionsForPathRendererChain);
robertphillips68737822015-10-29 12:12:21 -0700865 }
866
867 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(args, drawType, stencilSupport);
868 if (!pr && allowSW) {
Brian Salomone7df0bb2018-05-07 14:44:57 -0400869 auto swPR = this->getSoftwarePathRenderer();
870 if (GrPathRenderer::CanDrawPath::kNo != swPR->canDrawPath(args)) {
871 pr = swPR;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500872 }
robertphillips68737822015-10-29 12:12:21 -0700873 }
874
Robert Phillipsd81379d2020-04-21 10:39:02 -0400875#if GR_PATH_RENDERER_SPEW
876 if (pr) {
877 SkDebugf("getPathRenderer: %s\n", pr->name());
878 }
879#endif
880
robertphillips68737822015-10-29 12:12:21 -0700881 return pr;
882}
883
Brian Salomone7df0bb2018-05-07 14:44:57 -0400884GrPathRenderer* GrDrawingManager::getSoftwarePathRenderer() {
885 if (!fSoftwarePathRenderer) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400886 fSoftwarePathRenderer.reset(
Robert Phillips9da87e02019-02-04 13:26:26 -0500887 new GrSoftwarePathRenderer(fContext->priv().proxyProvider(),
Ben Wagner9ec70c62018-07-12 13:30:47 -0400888 fOptionsForPathRendererChain.fAllowPathMaskCaching));
Brian Salomone7df0bb2018-05-07 14:44:57 -0400889 }
Ben Wagner9ec70c62018-07-12 13:30:47 -0400890 return fSoftwarePathRenderer.get();
Brian Salomone7df0bb2018-05-07 14:44:57 -0400891}
892
Chris Daltonfddb6c02017-11-04 15:22:22 -0600893GrCoverageCountingPathRenderer* GrDrawingManager::getCoverageCountingPathRenderer() {
894 if (!fPathRendererChain) {
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500895 fPathRendererChain = std::make_unique<GrPathRendererChain>(fContext,
896 fOptionsForPathRendererChain);
Chris Daltonfddb6c02017-11-04 15:22:22 -0600897 }
898 return fPathRendererChain->getCoverageCountingPathRenderer();
899}
900
Brian Salomon653f42f2018-07-10 10:07:31 -0400901void GrDrawingManager::flushIfNecessary() {
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400902 auto direct = fContext->asDirectContext();
Robert Phillips6a6de562019-02-15 15:19:15 -0500903 if (!direct) {
904 return;
905 }
906
907 auto resourceCache = direct->priv().getResourceCache();
Brian Salomon653f42f2018-07-10 10:07:31 -0400908 if (resourceCache && resourceCache->requestsFlush()) {
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500909 if (this->flush({}, SkSurface::BackendSurfaceAccess::kNoAccess, GrFlushInfo(), nullptr)) {
Greg Danielfe159622020-04-10 17:43:51 +0000910 this->submitToGpu(false);
911 }
Brian Salomon57d2beab2018-09-10 09:35:41 -0400912 resourceCache->purgeAsNeeded();
Brian Salomon653f42f2018-07-10 10:07:31 -0400913 }
914}