blob: 3c9b49fd95b67952151c23c0257c48a747b1592b [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"
Adlai Holler08f53112021-01-20 17:44:15 -050030#include "src/gpu/GrRenderTaskCluster.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "src/gpu/GrResourceAllocator.h"
32#include "src/gpu/GrResourceProvider.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"
Robert Phillipsfbbc3bb2020-11-16 14:58:56 -050036#include "src/gpu/GrTTopoSort.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000037#include "src/gpu/GrTexture.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040038#include "src/gpu/GrTextureProxy.h"
Adlai Holler9e2c50e2021-02-09 14:41:52 -050039#include "src/gpu/GrTextureProxyPriv.h"
Chris Dalton3d770272019-08-14 09:24:37 -060040#include "src/gpu/GrTextureResolveRenderTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050041#include "src/gpu/GrTracing.h"
Greg Danielbbfec9d2019-08-20 10:56:51 -040042#include "src/gpu/GrTransferFromRenderTask.h"
Greg Danielc30f1a92019-09-06 15:28:58 -040043#include "src/gpu/GrWaitRenderTask.h"
Brian Salomonbe1084b2021-01-26 13:29:30 -050044#include "src/gpu/GrWritePixelsRenderTask.h"
Herb Derbybf2dd2a2021-03-04 10:13:22 -050045#include "src/gpu/text/GrSDFTControl.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050046#include "src/image/SkSurface_Gpu.h"
robertphillips498d7ac2015-10-30 10:11:30 -070047
Robert Phillips3674f582021-06-16 12:05:54 -040048#if GR_OGA
49#include "src/gpu/GrSoftwarePathRenderer.h"
50#endif
51
Robert Phillips22310d62018-09-05 11:07:21 -040052///////////////////////////////////////////////////////////////////////////////////////////////////
Robert Phillips3674f582021-06-16 12:05:54 -040053#if GR_OGA
54GrDrawingManager::GrDrawingManager(GrRecordingContext* rContext,
Robert Phillips22310d62018-09-05 11:07:21 -040055 const GrPathRendererChain::Options& optionsForPathRendererChain,
Greg Danielf41b2bd2019-08-22 16:19:24 -040056 bool reduceOpsTaskSplitting)
Robert Phillips3674f582021-06-16 12:05:54 -040057 : fContext(rContext)
Robert Phillips22310d62018-09-05 11:07:21 -040058 , fOptionsForPathRendererChain(optionsForPathRendererChain)
Robert Phillips22310d62018-09-05 11:07:21 -040059 , fPathRendererChain(nullptr)
60 , fSoftwarePathRenderer(nullptr)
Robert Phillips3674f582021-06-16 12:05:54 -040061 , fReduceOpsTaskSplitting(reduceOpsTaskSplitting) {
62}
63
64#else
65
66GrDrawingManager::GrDrawingManager(GrRecordingContext* rContext, bool reduceOpsTaskSplitting)
67 : fContext(rContext)
68 , fReduceOpsTaskSplitting(reduceOpsTaskSplitting) {
69}
70
71#endif
Robert Phillips22310d62018-09-05 11:07:21 -040072
robertphillips3dc6ae52015-10-20 09:54:32 -070073GrDrawingManager::~GrDrawingManager() {
Adlai Holler33432272020-11-11 13:54:37 -050074 this->closeAllTasks();
Adlai Hollerc616e1c2021-02-11 15:18:17 -050075 this->removeRenderTasks();
robertphillips3dc6ae52015-10-20 09:54:32 -070076}
77
Robert Phillipsa9162df2019-02-11 14:12:03 -050078bool GrDrawingManager::wasAbandoned() const {
Robert Phillips9eb00022020-06-30 15:30:12 -040079 return fContext->abandoned();
robertphillips3dc6ae52015-10-20 09:54:32 -070080}
81
robertphillips68737822015-10-29 12:12:21 -070082void GrDrawingManager::freeGpuResources() {
Jim Van Verth106b5c42017-09-26 12:45:29 -040083 for (int i = fOnFlushCBObjects.count() - 1; i >= 0; --i) {
84 if (!fOnFlushCBObjects[i]->retainOnFreeGpuResources()) {
85 // it's safe to just do this because we're iterating in reverse
86 fOnFlushCBObjects.removeShuffle(i);
87 }
88 }
89
Robert Phillips3674f582021-06-16 12:05:54 -040090#if GR_OGA
robertphillips68737822015-10-29 12:12:21 -070091 // a path renderer may be holding onto resources
robertphillips13391dd2015-10-30 05:15:11 -070092 fPathRendererChain = nullptr;
Ben Wagner9ec70c62018-07-12 13:30:47 -040093 fSoftwarePathRenderer = nullptr;
Robert Phillips3674f582021-06-16 12:05:54 -040094#endif
Robert Phillipse3302df2017-04-24 07:31:02 -040095}
96
Adlai Hollerc2bfcff2020-11-06 15:39:36 -050097// MDB TODO: make use of the 'proxies' parameter.
Greg Daniel9efe3862020-06-11 11:51:06 -040098bool GrDrawingManager::flush(
Adlai Hollerc2bfcff2020-11-06 15:39:36 -050099 SkSpan<GrSurfaceProxy*> proxies,
Greg Daniel9efe3862020-06-11 11:51:06 -0400100 SkSurface::BackendSurfaceAccess access,
101 const GrFlushInfo& info,
102 const GrBackendSurfaceMutableState* newState) {
Brian Salomon57d2beab2018-09-10 09:35:41 -0400103 GR_CREATE_TRACE_MARKER_CONTEXT("GrDrawingManager", "flush", fContext);
Brian Salomondcbb9d92017-07-19 10:53:20 -0400104
robertphillips7761d612016-05-16 09:14:53 -0700105 if (fFlushing || this->wasAbandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -0400106 if (info.fSubmittedProc) {
107 info.fSubmittedProc(info.fSubmittedContext, false);
108 }
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400109 if (info.fFinishedProc) {
110 info.fFinishedProc(info.fFinishedContext);
Greg Daniela3aa75a2019-04-12 14:24:55 -0400111 }
Greg Danielfe159622020-04-10 17:43:51 +0000112 return false;
joshualittb8918c42015-12-18 09:59:46 -0800113 }
Robert Phillips602df412019-04-08 11:10:39 -0400114
Robert Phillips38d64b02018-09-04 13:23:26 -0400115 SkDEBUGCODE(this->validate());
116
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500117 // As of now we only short-circuit if we got an explicit list of surfaces to flush.
118 if (!proxies.empty() && !info.fNumSemaphores && !info.fFinishedProc &&
Greg Danielce9f0162020-06-30 13:42:46 -0400119 access == SkSurface::BackendSurfaceAccess::kNoAccess && !newState) {
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500120 bool allUnused = std::all_of(proxies.begin(), proxies.end(), [&](GrSurfaceProxy* proxy) {
Adlai Holler33432272020-11-11 13:54:37 -0500121 bool used = std::any_of(fDAG.begin(), fDAG.end(), [&](auto& task) {
122 return task && task->isUsed(proxy);
123 });
Robert Phillips58a8ccc2021-01-11 09:07:55 -0500124 return !used;
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500125 });
126 if (allUnused) {
Greg Daniel55822f12020-05-26 11:26:45 -0400127 if (info.fSubmittedProc) {
128 info.fSubmittedProc(info.fSubmittedContext, true);
129 }
Greg Danielfe159622020-04-10 17:43:51 +0000130 return false;
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400131 }
Robert Phillips9313aa72019-04-09 18:41:27 -0400132 }
133
Adlai Hollerca1137b2021-04-08 11:39:55 -0400134 auto dContext = fContext->asDirectContext();
135 SkASSERT(dContext);
136 dContext->priv().clientMappedBufferManager()->process();
Robert Phillips6a6de562019-02-15 15:19:15 -0500137
Adlai Hollerca1137b2021-04-08 11:39:55 -0400138 GrGpu* gpu = dContext->priv().getGpu();
Greg Daniel55822f12020-05-26 11:26:45 -0400139 // We have a non abandoned and direct GrContext. It must have a GrGpu.
140 SkASSERT(gpu);
Greg Daniela3aa75a2019-04-12 14:24:55 -0400141
joshualittb8918c42015-12-18 09:59:46 -0800142 fFlushing = true;
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400143
Adlai Hollerca1137b2021-04-08 11:39:55 -0400144 auto resourceProvider = dContext->priv().resourceProvider();
145 auto resourceCache = dContext->priv().getResourceCache();
Robert Phillips6a6de562019-02-15 15:19:15 -0500146
Chris Dalton6b498102019-08-01 14:14:52 -0600147 // Semi-usually the GrRenderTasks are already closed at this point, but sometimes Ganesh needs
Greg Danielf41b2bd2019-08-22 16:19:24 -0400148 // to flush mid-draw. In that case, the SkGpuDevice's opsTasks won't be closed but need to be
149 // flushed anyway. Closing such opsTasks here will mean new ones will be created to replace them
Chris Dalton6b498102019-08-01 14:14:52 -0600150 // if the SkGpuDevice(s) write to them again.
Adlai Holler33432272020-11-11 13:54:37 -0500151 this->closeAllTasks();
Adlai Holler039f90c2020-11-19 15:20:31 +0000152 fActiveOpsTask = nullptr;
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400153
Adlai Holler33432272020-11-11 13:54:37 -0500154 this->sortTasks();
Adlai Holler78036082021-01-07 11:41:33 -0500155
Brian Salomon601ac802019-02-07 13:37:16 -0500156 if (!fCpuBufferCache) {
157 // We cache more buffers when the backend is using client side arrays. Otherwise, we
158 // expect each pool will use a CPU buffer as a staging buffer before uploading to a GPU
159 // buffer object. Each pool only requires one staging buffer at a time.
160 int maxCachedBuffers = fContext->priv().caps()->preferClientSideDynamicBuffers() ? 2 : 6;
161 fCpuBufferCache = GrBufferAllocPool::CpuBufferCache::Make(maxCachedBuffers);
Brian Salomon58f153c2018-10-18 21:51:15 -0400162 }
Robert Phillipsa4c93ac2017-05-18 11:40:04 -0400163
Robert Phillipse5f73282019-06-18 17:15:04 -0400164 GrOpFlushState flushState(gpu, resourceProvider, &fTokenTracker, fCpuBufferCache);
Robert Phillips40a29d72018-01-18 12:59:22 -0500165
Chris Daltonfe199b72017-05-05 11:26:15 -0400166 GrOnFlushResourceProvider onFlushProvider(this);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400167
Chris Dalton12658942017-10-05 19:45:25 -0600168 // Prepare any onFlush op lists (e.g. atlases).
Chris Daltonfe199b72017-05-05 11:26:15 -0400169 if (!fOnFlushCBObjects.empty()) {
Robert Phillips07f675d2020-11-16 13:44:01 -0500170 fFlushingRenderTaskIDs.reserve_back(fDAG.count());
171 for (const auto& task : fDAG) {
172 if (task) {
173 task->gatherIDs(&fFlushingRenderTaskIDs);
Adlai Holler33432272020-11-11 13:54:37 -0500174 }
175 }
Robert Phillips22310d62018-09-05 11:07:21 -0400176
Chris Daltonfe199b72017-05-05 11:26:15 -0400177 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
Brian Osmanae87bf12021-05-11 13:36:10 -0400178 onFlushCBObject->preFlush(&onFlushProvider, SkMakeSpan(fFlushingRenderTaskIDs));
Chris Daltonc4b47352019-08-23 10:10:36 -0600179 }
180 for (const auto& onFlushRenderTask : fOnFlushRenderTasks) {
181 onFlushRenderTask->makeClosed(*fContext->priv().caps());
Chris Dalton706a6ff2017-11-29 22:01:06 -0700182#ifdef SK_DEBUG
Chris Daltonc4b47352019-08-23 10:10:36 -0600183 // OnFlush callbacks are invoked during flush, and are therefore expected to handle
Adlai Holler9e2c50e2021-02-09 14:41:52 -0500184 // resource allocation & usage on their own. (No deferred or lazy proxies!)
Chris Daltonc4b47352019-08-23 10:10:36 -0600185 onFlushRenderTask->visitTargetAndSrcProxies_debugOnly(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400186 [](GrSurfaceProxy* p, GrMipmapped mipMapped) {
Adlai Holler9e2c50e2021-02-09 14:41:52 -0500187 SkASSERT(!p->asTextureProxy() || !p->asTextureProxy()->texPriv().isDeferred());
Brian Salomonbeb7f522019-08-30 16:19:42 -0400188 SkASSERT(!p->isLazy());
Chris Dalton4ece96d2019-08-30 11:26:39 -0600189 if (p->requiresManualMSAAResolve()) {
190 // The onFlush callback is responsible for ensuring MSAA gets resolved.
191 SkASSERT(p->asRenderTargetProxy() && !p->asRenderTargetProxy()->isMSAADirty());
192 }
Brian Salomon7e67dca2020-07-21 09:27:25 -0400193 if (GrMipmapped::kYes == mipMapped) {
Chris Daltonc4b47352019-08-23 10:10:36 -0600194 // The onFlush callback is responsible for regenerating mips if needed.
Brian Salomon8c82a872020-07-21 12:09:58 -0400195 SkASSERT(p->asTextureProxy() && !p->asTextureProxy()->mipmapsAreDirty());
Chris Daltonc4b47352019-08-23 10:10:36 -0600196 }
197 });
Chris Dalton706a6ff2017-11-29 22:01:06 -0700198#endif
Chris Daltonc4b47352019-08-23 10:10:36 -0600199 onFlushRenderTask->prepare(&flushState);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400200 }
201 }
202
Adlai Holler141b38b2021-05-05 15:16:40 +0000203 bool usingReorderedDAG = false;
204 GrResourceAllocator resourceAllocator(dContext);
205 if (fReduceOpsTaskSplitting) {
206 usingReorderedDAG = this->reorderTasks(&resourceAllocator);
207 if (!usingReorderedDAG) {
208 resourceAllocator.reset();
209 }
210 }
211
robertphillipsa13e2022015-11-11 12:01:09 -0800212#if 0
Brian Salomon09d994e2016-12-21 11:14:46 -0500213 // Enable this to print out verbose GrOp information
Adlai Hollerabdfd392020-10-19 09:00:36 -0400214 SkDEBUGCODE(SkDebugf("onFlush renderTasks (%d):\n", fOnFlushRenderTasks.count()));
Chris Daltonc4b47352019-08-23 10:10:36 -0600215 for (const auto& onFlushRenderTask : fOnFlushRenderTasks) {
Adlai Hollerabdfd392020-10-19 09:00:36 -0400216 SkDEBUGCODE(onFlushRenderTask->dump(/* printDependencies */ true);)
Chris Daltonc4b47352019-08-23 10:10:36 -0600217 }
Adlai Holler33432272020-11-11 13:54:37 -0500218 SkDEBUGCODE(SkDebugf("Normal renderTasks (%d):\n", fDAG.count()));
219 for (const auto& task : fDAG) {
220 SkDEBUGCODE(task->dump(/* printDependencies */ true);)
robertphillips3dc6ae52015-10-20 09:54:32 -0700221 }
robertphillipsa13e2022015-11-11 12:01:09 -0800222#endif
223
Adlai Holler387309c2021-04-15 15:03:27 -0400224 if (!resourceAllocator.failedInstantiation()) {
225 if (!usingReorderedDAG) {
226 for (const auto& task : fDAG) {
227 SkASSERT(task);
228 task->gatherProxyIntervals(&resourceAllocator);
Adlai Hollerfe919742021-04-10 07:42:15 -0400229 }
Adlai Holler387309c2021-04-15 15:03:27 -0400230 resourceAllocator.planAssignment();
Adlai Hollerfe919742021-04-10 07:42:15 -0400231 }
Adlai Holler387309c2021-04-15 15:03:27 -0400232 resourceAllocator.assign();
Greg Danielc42b20b2017-10-04 10:34:49 -0400233 }
Adlai Holler387309c2021-04-15 15:03:27 -0400234 bool flushed = !resourceAllocator.failedInstantiation() &&
235 this->executeRenderTasks(&flushState);
Adlai Holler19fd5142021-03-08 10:19:30 -0700236 this->removeRenderTasks();
robertphillipsa13e2022015-11-11 12:01:09 -0800237
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500238 gpu->executeFlushInfo(proxies, access, info, newState);
robertphillipsa13e2022015-11-11 12:01:09 -0800239
Brian Salomon57d2beab2018-09-10 09:35:41 -0400240 // Give the cache a chance to purge resources that become purgeable due to flushing.
241 if (flushed) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500242 resourceCache->purgeAsNeeded();
Brian Salomon876a0172019-03-08 11:12:14 -0500243 flushed = false;
bsalomonb77a9072016-09-07 10:02:04 -0700244 }
Chris Daltonfe199b72017-05-05 11:26:15 -0400245 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
Brian Osmanae87bf12021-05-11 13:36:10 -0400246 onFlushCBObject->postFlush(fTokenTracker.nextTokenToFlush(),
247 SkMakeSpan(fFlushingRenderTaskIDs));
Brian Salomon876a0172019-03-08 11:12:14 -0500248 flushed = true;
249 }
250 if (flushed) {
251 resourceCache->purgeAsNeeded();
Chris Daltonfe199b72017-05-05 11:26:15 -0400252 }
Chris Dalton6b498102019-08-01 14:14:52 -0600253 fFlushingRenderTaskIDs.reset();
joshualittb8918c42015-12-18 09:59:46 -0800254 fFlushing = false;
Greg Daniel51316782017-08-02 15:10:09 +0000255
Greg Danielfe159622020-04-10 17:43:51 +0000256 return true;
257}
258
259bool GrDrawingManager::submitToGpu(bool syncToCpu) {
260 if (fFlushing || this->wasAbandoned()) {
261 return false;
262 }
263
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400264 auto direct = fContext->asDirectContext();
Greg Danielfe159622020-04-10 17:43:51 +0000265 if (!direct) {
266 return false; // Can't submit while DDL recording
267 }
268 GrGpu* gpu = direct->priv().getGpu();
269 return gpu->submitToGpu(syncToCpu);
robertphillips3dc6ae52015-10-20 09:54:32 -0700270}
271
Adlai Hollerc616e1c2021-02-11 15:18:17 -0500272bool GrDrawingManager::executeRenderTasks(GrOpFlushState* flushState) {
Robert Phillips27483912018-04-20 12:43:18 -0400273#if GR_FLUSH_TIME_OP_SPEW
Adlai Hollerc616e1c2021-02-11 15:18:17 -0500274 SkDebugf("Flushing %d opsTasks\n", fDAG.count());
275 for (int i = 0; i < fDAG.count(); ++i) {
Adlai Holler33432272020-11-11 13:54:37 -0500276 if (fDAG[i]) {
Robert Phillips047d5bb2021-01-08 13:39:19 -0500277 SkString label;
278 label.printf("task %d/%d", i, fDAG.count());
279 fDAG[i]->dump(label, {}, true, true);
Robert Phillips1734dd32018-08-21 13:52:09 -0400280 }
Robert Phillips27483912018-04-20 12:43:18 -0400281 }
282#endif
283
Chris Dalton6b498102019-08-01 14:14:52 -0600284 bool anyRenderTasksExecuted = false;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500285
Adlai Hollerc616e1c2021-02-11 15:18:17 -0500286 for (const auto& renderTask : fDAG) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400287 if (!renderTask || !renderTask->isInstantiated()) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500288 continue;
289 }
290
Adlai Holler9e2c50e2021-02-09 14:41:52 -0500291 SkASSERT(renderTask->deferredProxiesAreInstantiated());
292
Chris Dalton6b498102019-08-01 14:14:52 -0600293 renderTask->prepare(flushState);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500294 }
295
296 // Upload all data to the GPU
297 flushState->preExecuteDraws();
298
Greg Danield2073452018-12-07 11:20:33 -0500299 // For Vulkan, if we have too many oplists to be flushed we end up allocating a lot of resources
300 // for each command buffer associated with the oplists. If this gets too large we can cause the
301 // devices to go OOM. In practice we usually only hit this case in our tests, but to be safe we
302 // put a cap on the number of oplists we will execute before flushing to the GPU to relieve some
303 // memory pressure.
Chris Dalton6b498102019-08-01 14:14:52 -0600304 static constexpr int kMaxRenderTasksBeforeFlush = 100;
Adlai Hollerc616e1c2021-02-11 15:18:17 -0500305 int numRenderTasksExecuted = 0;
Greg Danield2073452018-12-07 11:20:33 -0500306
Chris Daltonc4b47352019-08-23 10:10:36 -0600307 // Execute the onFlush renderTasks first, if any.
308 for (sk_sp<GrRenderTask>& onFlushRenderTask : fOnFlushRenderTasks) {
309 if (!onFlushRenderTask->execute(flushState)) {
310 SkDebugf("WARNING: onFlushRenderTask failed to execute.\n");
Robert Phillipseafd48a2017-11-16 07:52:08 -0500311 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600312 SkASSERT(onFlushRenderTask->unique());
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400313 onFlushRenderTask->disown(this);
Chris Daltonc4b47352019-08-23 10:10:36 -0600314 onFlushRenderTask = nullptr;
Adlai Hollerc616e1c2021-02-11 15:18:17 -0500315 if (++numRenderTasksExecuted >= kMaxRenderTasksBeforeFlush) {
Greg Danielfe159622020-04-10 17:43:51 +0000316 flushState->gpu()->submitToGpu(false);
Adlai Hollerc616e1c2021-02-11 15:18:17 -0500317 numRenderTasksExecuted = 0;
Greg Danield2073452018-12-07 11:20:33 -0500318 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500319 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600320 fOnFlushRenderTasks.reset();
Robert Phillipseafd48a2017-11-16 07:52:08 -0500321
322 // Execute the normal op lists.
Adlai Hollerc616e1c2021-02-11 15:18:17 -0500323 for (const auto& renderTask : fDAG) {
324 SkASSERT(renderTask);
325 if (!renderTask->isInstantiated()) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500326 continue;
327 }
328
Greg Daniel15ecdf92019-08-30 15:35:23 -0400329 if (renderTask->execute(flushState)) {
Chris Dalton6b498102019-08-01 14:14:52 -0600330 anyRenderTasksExecuted = true;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500331 }
Adlai Hollerc616e1c2021-02-11 15:18:17 -0500332 if (++numRenderTasksExecuted >= kMaxRenderTasksBeforeFlush) {
Greg Danielfe159622020-04-10 17:43:51 +0000333 flushState->gpu()->submitToGpu(false);
Adlai Hollerc616e1c2021-02-11 15:18:17 -0500334 numRenderTasksExecuted = 0;
Greg Danield2073452018-12-07 11:20:33 -0500335 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500336 }
337
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400338 SkASSERT(!flushState->opsRenderPass());
Robert Phillips40a29d72018-01-18 12:59:22 -0500339 SkASSERT(fTokenTracker.nextDrawToken() == fTokenTracker.nextTokenToFlush());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500340
Chris Dalton6b498102019-08-01 14:14:52 -0600341 // We reset the flush state before the RenderTasks so that the last resources to be freed are
342 // those that are written to in the RenderTasks. This helps to make sure the most recently used
343 // resources are the last to be purged by the resource cache.
Robert Phillipseafd48a2017-11-16 07:52:08 -0500344 flushState->reset();
345
Chris Dalton6b498102019-08-01 14:14:52 -0600346 return anyRenderTasksExecuted;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500347}
348
Adlai Hollerc616e1c2021-02-11 15:18:17 -0500349void GrDrawingManager::removeRenderTasks() {
350 for (const auto& task : fDAG) {
351 SkASSERT(task);
Robert Phillips07f675d2020-11-16 13:44:01 -0500352 if (!task->unique() || task->requiresExplicitCleanup()) {
353 // TODO: Eventually uniqueness should be guaranteed: http://skbug.com/7111.
354 // DDLs, however, will always require an explicit notification for when they
355 // can clean up resources.
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400356 task->endFlush(this);
357 }
358 task->disown(this);
359 }
Adlai Hollerc616e1c2021-02-11 15:18:17 -0500360 fDAG.reset();
361 fLastRenderTasks.reset();
Chris Dalton39811682021-03-11 22:45:48 -0700362 for (const sk_sp<GrRenderTask>& onFlushRenderTask : fOnFlushRenderTasks) {
363 onFlushRenderTask->disown(this);
364 }
365 fOnFlushRenderTasks.reset();
Adlai Holler33432272020-11-11 13:54:37 -0500366}
367
368void GrDrawingManager::sortTasks() {
Robert Phillipsfbbc3bb2020-11-16 14:58:56 -0500369 if (!GrTTopoSort<GrRenderTask, GrRenderTask::TopoSortTraits>(&fDAG)) {
Adlai Holler33432272020-11-11 13:54:37 -0500370 SkDEBUGFAIL("Render task topo sort failed.");
371 return;
372 }
373
374#ifdef SK_DEBUG
375 // This block checks for any unnecessary splits in the opsTasks. If two sequential opsTasks
Greg Daniel9a2d3d12021-06-23 22:36:06 +0000376 // share the same backing GrSurfaceProxy it means the opsTask was artificially split.
Adlai Holler33432272020-11-11 13:54:37 -0500377 if (!fDAG.empty()) {
378 GrOpsTask* prevOpsTask = fDAG[0]->asOpsTask();
379 for (int i = 1; i < fDAG.count(); ++i) {
380 GrOpsTask* curOpsTask = fDAG[i]->asOpsTask();
381
382 if (prevOpsTask && curOpsTask) {
Greg Daniel9a2d3d12021-06-23 22:36:06 +0000383 SkASSERT(prevOpsTask->target(0) != curOpsTask->target(0));
Adlai Holler33432272020-11-11 13:54:37 -0500384 }
385
386 prevOpsTask = curOpsTask;
387 }
388 }
389#endif
390}
391
Adlai Holler78036082021-01-07 11:41:33 -0500392// Reorder the array to match the llist without reffing & unreffing sk_sp's.
Adlai Holler69c57e42021-01-25 15:44:26 +0000393// Both args must contain the same objects.
Adlai Holler78036082021-01-07 11:41:33 -0500394// This is basically a shim because clustering uses LList but the rest of drawmgr uses array.
395template <typename T>
396static void reorder_array_by_llist(const SkTInternalLList<T>& llist, SkTArray<sk_sp<T>>* array) {
397 int i = 0;
398 for (T* t : llist) {
Adlai Holler69c57e42021-01-25 15:44:26 +0000399 // Release the pointer that used to live here so it doesn't get unreffed.
400 [[maybe_unused]] T* old = array->at(i).release();
Adlai Holler78036082021-01-07 11:41:33 -0500401 array->at(i++).reset(t);
402 }
Adlai Holler69c57e42021-01-25 15:44:26 +0000403 SkASSERT(i == array->count());
Adlai Holler78036082021-01-07 11:41:33 -0500404}
405
Adlai Holler387309c2021-04-15 15:03:27 -0400406bool GrDrawingManager::reorderTasks(GrResourceAllocator* resourceAllocator) {
Adlai Holler78036082021-01-07 11:41:33 -0500407 SkASSERT(fReduceOpsTaskSplitting);
408 SkTInternalLList<GrRenderTask> llist;
Brian Osmanae87bf12021-05-11 13:36:10 -0400409 bool clustered = GrClusterRenderTasks(SkMakeSpan(fDAG), &llist);
Adlai Holler78036082021-01-07 11:41:33 -0500410 if (!clustered) {
Adlai Holler387309c2021-04-15 15:03:27 -0400411 return false;
412 }
413
414 for (GrRenderTask* task : llist) {
415 task->gatherProxyIntervals(resourceAllocator);
416 }
417 if (!resourceAllocator->planAssignment()) {
418 return false;
419 }
420 if (!resourceAllocator->makeBudgetHeadroom()) {
421 auto dContext = fContext->asDirectContext();
422 SkASSERT(dContext);
423 dContext->priv().getGpu()->stats()->incNumReorderedDAGsOverBudget();
424 return false;
Adlai Holler78036082021-01-07 11:41:33 -0500425 }
Adlai Holler78036082021-01-07 11:41:33 -0500426 reorder_array_by_llist(llist, &fDAG);
Adlai Holler93439d92021-01-26 09:20:39 -0500427
428 int newCount = 0;
429 for (int i = 0; i < fDAG.count(); i++) {
430 sk_sp<GrRenderTask>& task = fDAG[i];
431 if (auto opsTask = task->asOpsTask()) {
432 size_t remaining = fDAG.size() - i - 1;
433 SkSpan<sk_sp<GrRenderTask>> nextTasks{fDAG.end() - remaining, remaining};
434 int removeCount = opsTask->mergeFrom(nextTasks);
435 for (const auto& removed : nextTasks.first(removeCount)) {
436 removed->disown(this);
437 }
438 i += removeCount;
439 }
440 fDAG[newCount++] = std::move(task);
441 }
442 fDAG.resize_back(newCount);
Adlai Holler387309c2021-04-15 15:03:27 -0400443 return true;
Adlai Holler78036082021-01-07 11:41:33 -0500444}
445
Adlai Holler33432272020-11-11 13:54:37 -0500446void GrDrawingManager::closeAllTasks() {
447 const GrCaps& caps = *fContext->priv().caps();
448 for (auto& task : fDAG) {
449 if (task) {
450 task->makeClosed(caps);
451 }
452 }
453}
454
455GrRenderTask* GrDrawingManager::insertTaskBeforeLast(sk_sp<GrRenderTask> task) {
456 SkASSERT(!fDAG.empty());
457 if (!task) {
458 return nullptr;
459 }
460 // Release 'fDAG.back()' and grab the raw pointer, in case the SkTArray grows
461 // and reallocates during emplace_back.
462 // TODO: Either use std::vector that can do this for us, or use SkSTArray to get the
463 // perf win.
464 fDAG.emplace_back(fDAG.back().release());
465 return (fDAG[fDAG.count() - 2] = std::move(task)).get();
466}
467
468GrRenderTask* GrDrawingManager::appendTask(sk_sp<GrRenderTask> task) {
469 if (!task) {
470 return nullptr;
471 }
472 return fDAG.push_back(std::move(task)).get();
473}
474
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400475static void resolve_and_mipmap(GrGpu* gpu, GrSurfaceProxy* proxy) {
476 if (!proxy->isInstantiated()) {
477 return;
478 }
479
480 // In the flushSurfaces case, we need to resolve MSAA immediately after flush. This is
481 // because clients expect the flushed surface's backing texture to be fully resolved
482 // upon return.
483 if (proxy->requiresManualMSAAResolve()) {
484 auto* rtProxy = proxy->asRenderTargetProxy();
485 SkASSERT(rtProxy);
486 if (rtProxy->isMSAADirty()) {
487 SkASSERT(rtProxy->peekRenderTarget());
Jim Van Verthbb61fe32020-07-07 16:39:04 -0400488 gpu->resolveRenderTarget(rtProxy->peekRenderTarget(), rtProxy->msaaDirtyRect());
489 gpu->submitToGpu(false);
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400490 rtProxy->markMSAAResolved();
491 }
492 }
493 // If, after a flush, any of the proxies of interest have dirty mipmaps, regenerate them in
494 // case their backend textures are being stolen.
495 // (This special case is exercised by the ReimportImageTextureWithMipLevels test.)
496 // FIXME: It may be more ideal to plumb down a "we're going to steal the backends" flag.
497 if (auto* textureProxy = proxy->asTextureProxy()) {
Brian Salomon8c82a872020-07-21 12:09:58 -0400498 if (textureProxy->mipmapsAreDirty()) {
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400499 SkASSERT(textureProxy->peekTexture());
500 gpu->regenerateMipMapLevels(textureProxy->peekTexture());
Brian Salomon8c82a872020-07-21 12:09:58 -0400501 textureProxy->markMipmapsClean();
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400502 }
503 }
504}
505
Greg Daniel9efe3862020-06-11 11:51:06 -0400506GrSemaphoresSubmitted GrDrawingManager::flushSurfaces(
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500507 SkSpan<GrSurfaceProxy*> proxies,
Greg Daniel9efe3862020-06-11 11:51:06 -0400508 SkSurface::BackendSurfaceAccess access,
509 const GrFlushInfo& info,
510 const GrBackendSurfaceMutableState* newState) {
bsalomon6a2b1942016-09-08 11:28:59 -0700511 if (this->wasAbandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -0400512 if (info.fSubmittedProc) {
513 info.fSubmittedProc(info.fSubmittedContext, false);
514 }
515 if (info.fFinishedProc) {
516 info.fFinishedProc(info.fFinishedContext);
517 }
Greg Daniel51316782017-08-02 15:10:09 +0000518 return GrSemaphoresSubmitted::kNo;
bsalomon6a2b1942016-09-08 11:28:59 -0700519 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400520 SkDEBUGCODE(this->validate());
bsalomon6a2b1942016-09-08 11:28:59 -0700521
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400522 auto direct = fContext->asDirectContext();
Adlai Holleraee25fd2020-11-06 12:34:56 -0500523 SkASSERT(direct);
Robert Phillips6a6de562019-02-15 15:19:15 -0500524 GrGpu* gpu = direct->priv().getGpu();
Greg Daniel55822f12020-05-26 11:26:45 -0400525 // We have a non abandoned and direct GrContext. It must have a GrGpu.
526 SkASSERT(gpu);
Robert Phillips874b5352018-03-16 08:48:24 -0400527
Robert Phillipsacc10fa2019-04-01 09:50:20 -0400528 // TODO: It is important to upgrade the drawingmanager to just flushing the
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400529 // portion of the DAG required by 'proxies' in order to restore some of the
Robert Phillipsacc10fa2019-04-01 09:50:20 -0400530 // semantics of this method.
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500531 bool didFlush = this->flush(proxies, access, info, newState);
532 for (GrSurfaceProxy* proxy : proxies) {
533 resolve_and_mipmap(gpu, proxy);
bsalomon6a2b1942016-09-08 11:28:59 -0700534 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400535
536 SkDEBUGCODE(this->validate());
Greg Danielfe159622020-04-10 17:43:51 +0000537
Greg Daniel04283f32020-05-20 13:16:00 -0400538 if (!didFlush || (!direct->priv().caps()->semaphoreSupport() && info.fNumSemaphores)) {
Greg Danielfe159622020-04-10 17:43:51 +0000539 return GrSemaphoresSubmitted::kNo;
540 }
541 return GrSemaphoresSubmitted::kYes;
bsalomon6a2b1942016-09-08 11:28:59 -0700542}
543
Chris Daltonfe199b72017-05-05 11:26:15 -0400544void GrDrawingManager::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
545 fOnFlushCBObjects.push_back(onFlushCBObject);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400546}
547
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500548#if GR_TEST_UTILS
549void GrDrawingManager::testingOnly_removeOnFlushCallbackObject(GrOnFlushCallbackObject* cb) {
550 int n = std::find(fOnFlushCBObjects.begin(), fOnFlushCBObjects.end(), cb) -
551 fOnFlushCBObjects.begin();
552 SkASSERT(n < fOnFlushCBObjects.count());
553 fOnFlushCBObjects.removeShuffle(n);
554}
555#endif
556
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400557void GrDrawingManager::setLastRenderTask(const GrSurfaceProxy* proxy, GrRenderTask* task) {
558#ifdef SK_DEBUG
Robert Phillips07f675d2020-11-16 13:44:01 -0500559 if (auto prior = this->getLastRenderTask(proxy)) {
560 SkASSERT(prior->isClosed() || prior == task);
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400561 }
562#endif
Adlai Holler25df1f72020-06-09 13:08:27 -0400563 uint32_t key = proxy->uniqueID().asUInt();
564 if (task) {
565 fLastRenderTasks.set(key, task);
566 } else if (fLastRenderTasks.find(key)) {
567 fLastRenderTasks.remove(key);
568 }
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400569}
570
571GrRenderTask* GrDrawingManager::getLastRenderTask(const GrSurfaceProxy* proxy) const {
572 auto entry = fLastRenderTasks.find(proxy->uniqueID().asUInt());
573 return entry ? *entry : nullptr;
574}
575
576GrOpsTask* GrDrawingManager::getLastOpsTask(const GrSurfaceProxy* proxy) const {
577 GrRenderTask* task = this->getLastRenderTask(proxy);
578 return task ? task->asOpsTask() : nullptr;
579}
580
581
Chris Dalton6b498102019-08-01 14:14:52 -0600582void GrDrawingManager::moveRenderTasksToDDL(SkDeferredDisplayList* ddl) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400583 SkDEBUGCODE(this->validate());
584
Chris Dalton6b498102019-08-01 14:14:52 -0600585 // no renderTask should receive a new command after this
Adlai Holler33432272020-11-11 13:54:37 -0500586 this->closeAllTasks();
Adlai Holler039f90c2020-11-19 15:20:31 +0000587 fActiveOpsTask = nullptr;
Robert Phillips7a137052018-02-01 11:23:12 -0500588
Robert Phillips07f675d2020-11-16 13:44:01 -0500589 this->sortTasks();
590
Adlai Holler33432272020-11-11 13:54:37 -0500591 fDAG.swap(ddl->fRenderTasks);
592 SkASSERT(fDAG.empty());
Robert Phillips867ce8f2018-06-21 10:28:36 -0400593
Robert Phillips19f466d2020-02-26 10:27:07 -0500594 for (auto& renderTask : ddl->fRenderTasks) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400595 renderTask->disown(this);
Robert Phillips29f38542019-10-16 09:20:25 -0400596 renderTask->prePrepare(fContext);
Robert Phillips7327c9d2019-10-08 16:32:56 -0400597 }
598
Michael Ludwig2c316bd2019-12-19 14:50:44 -0500599 ddl->fArenas = std::move(fContext->priv().detachArenas());
Robert Phillips61fc7992019-10-22 11:58:17 -0400600
Robert Phillipsf6a0b452020-02-18 14:26:46 -0500601 fContext->priv().detachProgramData(&ddl->fProgramData);
Robert Phillips576b6a12019-12-06 13:05:49 -0500602
Robert Phillips38d64b02018-09-04 13:23:26 -0400603 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500604}
605
Robert Phillips07f675d2020-11-16 13:44:01 -0500606void GrDrawingManager::createDDLTask(sk_sp<const SkDeferredDisplayList> ddl,
Robert Phillipseb54bb52021-01-08 17:20:18 -0500607 sk_sp<GrRenderTargetProxy> newDest,
Robert Phillips88b29612020-11-16 15:15:08 -0500608 SkIPoint offset) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400609 SkDEBUGCODE(this->validate());
610
Adlai Holler039f90c2020-11-19 15:20:31 +0000611 if (fActiveOpsTask) {
Adlai Hollerc44e21f2021-02-22 14:08:07 -0500612 // This is a temporary fix for the partial-MDB world. In that world we're not
Adlai Holler039f90c2020-11-19 15:20:31 +0000613 // reordering so ops that (in the single opsTask world) would've just glommed onto the
614 // end of the single opsTask but referred to a far earlier RT need to appear in their
615 // own opsTask.
616 fActiveOpsTask->makeClosed(*fContext->priv().caps());
617 fActiveOpsTask = nullptr;
Robert Phillips38d64b02018-09-04 13:23:26 -0400618 }
619
Robert Phillips07f675d2020-11-16 13:44:01 -0500620 // Propagate the DDL proxy's state information to the replay target.
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400621 if (ddl->priv().targetProxy()->isMSAADirty()) {
Brian Salomon982127b2021-01-21 10:43:35 -0500622 auto nativeRect = GrNativeRect::MakeIRectRelativeTo(
623 ddl->characterization().origin(),
624 ddl->priv().targetProxy()->backingStoreDimensions().height(),
625 ddl->priv().targetProxy()->msaaDirtyRect());
626 newDest->markMSAADirty(nativeRect);
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400627 }
628 GrTextureProxy* newTextureProxy = newDest->asTextureProxy();
Brian Salomon8c82a872020-07-21 12:09:58 -0400629 if (newTextureProxy && GrMipmapped::kYes == newTextureProxy->mipmapped()) {
630 newTextureProxy->markMipmapsDirty();
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400631 }
632
Robert Phillips62000362018-02-01 09:10:04 -0500633 // Here we jam the proxy that backs the current replay SkSurface into the LazyProxyData.
Robert Phillips07f675d2020-11-16 13:44:01 -0500634 // The lazy proxy that references it (in the DDL opsTasks) will then steal its GrTexture.
Robert Phillipseb54bb52021-01-08 17:20:18 -0500635 ddl->fLazyProxyData->fReplayDest = newDest.get();
Robert Phillips774168e2018-05-31 12:43:27 -0400636
Robert Phillips07f675d2020-11-16 13:44:01 -0500637 // Add a task to handle drawing and lifetime management of the DDL.
638 SkDEBUGCODE(auto ddlTask =) this->appendTask(sk_make_sp<GrDDLTask>(this,
Robert Phillipseb54bb52021-01-08 17:20:18 -0500639 std::move(newDest),
Robert Phillips88b29612020-11-16 15:15:08 -0500640 std::move(ddl),
641 offset));
Robert Phillips07f675d2020-11-16 13:44:01 -0500642 SkASSERT(ddlTask->isClosed());
Adlai Holler7580ad42020-06-24 13:45:25 -0400643
Robert Phillips38d64b02018-09-04 13:23:26 -0400644 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500645}
646
Robert Phillips38d64b02018-09-04 13:23:26 -0400647#ifdef SK_DEBUG
648void GrDrawingManager::validate() const {
Adlai Holler4ef108e2020-12-30 16:21:13 +0000649 if (fActiveOpsTask) {
650 SkASSERT(!fDAG.empty());
651 SkASSERT(!fActiveOpsTask->isClosed());
652 SkASSERT(fActiveOpsTask == fDAG.back().get());
653 }
Adlai Holler039f90c2020-11-19 15:20:31 +0000654
Adlai Holler4ef108e2020-12-30 16:21:13 +0000655 for (int i = 0; i < fDAG.count(); ++i) {
656 if (fActiveOpsTask != fDAG[i].get()) {
657 // The resolveTask associated with the activeTask remains open for as long as the
658 // activeTask does.
659 bool isActiveResolveTask =
660 fActiveOpsTask && fActiveOpsTask->fTextureResolveTask == fDAG[i].get();
661 SkASSERT(isActiveResolveTask || fDAG[i]->isClosed());
Adlai Holler76893192020-12-22 19:41:19 +0000662 }
Adlai Holler4ef108e2020-12-30 16:21:13 +0000663 }
Adlai Holler76893192020-12-22 19:41:19 +0000664
Adlai Holler4ef108e2020-12-30 16:21:13 +0000665 if (!fDAG.empty() && !fDAG.back()->isClosed()) {
666 SkASSERT(fActiveOpsTask == fDAG.back().get());
Robert Phillips38d64b02018-09-04 13:23:26 -0400667 }
668}
669#endif
670
Adlai Holler4ef108e2020-12-30 16:21:13 +0000671void GrDrawingManager::closeActiveOpsTask() {
672 if (fActiveOpsTask) {
673 // This is a temporary fix for the partial-MDB world. In that world we're not
Adlai Holler039f90c2020-11-19 15:20:31 +0000674 // reordering so ops that (in the single opsTask world) would've just glommed onto the
Greg Danielf41b2bd2019-08-22 16:19:24 -0400675 // end of the single opsTask but referred to a far earlier RT need to appear in their
676 // own opsTask.
Adlai Holler039f90c2020-11-19 15:20:31 +0000677 fActiveOpsTask->makeClosed(*fContext->priv().caps());
678 fActiveOpsTask = nullptr;
robertphillips3dc6ae52015-10-20 09:54:32 -0700679 }
Chris Dalton5fe99772019-08-06 11:57:39 -0600680}
681
Greg Daniel16f5c652019-10-29 11:26:01 -0400682sk_sp<GrOpsTask> GrDrawingManager::newOpsTask(GrSurfaceProxyView surfaceView,
Herb Derby0b1228d2021-04-05 18:38:35 -0400683 sk_sp<GrArenas> arenas,
Robert Phillips19006652020-11-19 14:20:57 -0500684 bool flushTimeOpsTask) {
Chris Dalton5fe99772019-08-06 11:57:39 -0600685 SkDEBUGCODE(this->validate());
686 SkASSERT(fContext);
687
Adlai Holler4ef108e2020-12-30 16:21:13 +0000688 this->closeActiveOpsTask();
robertphillips3dc6ae52015-10-20 09:54:32 -0700689
Herb Derby0b1228d2021-04-05 18:38:35 -0400690 sk_sp<GrOpsTask> opsTask(new GrOpsTask(this,
691 std::move(surfaceView),
692 fContext->priv().auditTrail(),
693 std::move(arenas)));
Brian Salomon982127b2021-01-21 10:43:35 -0500694 SkASSERT(this->getLastRenderTask(opsTask->target(0)) == opsTask.get());
robertphillips3dc6ae52015-10-20 09:54:32 -0700695
Robert Phillips19006652020-11-19 14:20:57 -0500696 if (flushTimeOpsTask) {
697 fOnFlushRenderTasks.push_back(opsTask);
698 } else {
Adlai Holler33432272020-11-11 13:54:37 -0500699 this->appendTask(opsTask);
Adlai Holler039f90c2020-11-19 15:20:31 +0000700
Adlai Holler4ef108e2020-12-30 16:21:13 +0000701 fActiveOpsTask = opsTask.get();
Robert Phillips941d1442017-06-14 16:37:02 -0400702 }
robertphillips3dc6ae52015-10-20 09:54:32 -0700703
Robert Phillips38d64b02018-09-04 13:23:26 -0400704 SkDEBUGCODE(this->validate());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400705 return opsTask;
robertphillips3dc6ae52015-10-20 09:54:32 -0700706}
707
Adlai Holler039f90c2020-11-19 15:20:31 +0000708GrTextureResolveRenderTask* GrDrawingManager::newTextureResolveRenderTask(const GrCaps& caps) {
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600709 // 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 +0000710 // in sorting and opsTask reduction mode) the render tasks that depend on any proxy's current
Greg Danielf41b2bd2019-08-22 16:19:24 -0400711 // state. This is because those opsTasks can still receive new ops and because if they refer to
Adlai Holler039f90c2020-11-19 15:20:31 +0000712 // the mipmapped version of 'proxy', they will then come to depend on the render task being
Chris Dalton4ece96d2019-08-30 11:26:39 -0600713 // created here.
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600714 //
Adlai Holler039f90c2020-11-19 15:20:31 +0000715 // Add the new textureResolveTask before the fActiveOpsTask (if not in
716 // sorting/opsTask-splitting-reduction mode) because it will depend upon this resolve task.
Chris Dalton3d770272019-08-14 09:24:37 -0600717 // NOTE: Putting it here will also reduce the amount of work required by the topological sort.
Adlai Holler33432272020-11-11 13:54:37 -0500718 GrRenderTask* task = this->insertTaskBeforeLast(sk_make_sp<GrTextureResolveRenderTask>());
719 return static_cast<GrTextureResolveRenderTask*>(task);
Chris Dalton3d770272019-08-14 09:24:37 -0600720}
721
Greg Danielc30f1a92019-09-06 15:28:58 -0400722void GrDrawingManager::newWaitRenderTask(sk_sp<GrSurfaceProxy> proxy,
Greg Daniel301015c2019-11-18 14:06:46 -0500723 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> semaphores,
Greg Danielc30f1a92019-09-06 15:28:58 -0400724 int numSemaphores) {
725 SkDEBUGCODE(this->validate());
726 SkASSERT(fContext);
727
728 const GrCaps& caps = *fContext->priv().caps();
729
Greg Daniel16f5c652019-10-29 11:26:01 -0400730 sk_sp<GrWaitRenderTask> waitTask = sk_make_sp<GrWaitRenderTask>(GrSurfaceProxyView(proxy),
731 std::move(semaphores),
Greg Danielc30f1a92019-09-06 15:28:58 -0400732 numSemaphores);
Greg Danielc30f1a92019-09-06 15:28:58 -0400733
Brian Salomon982127b2021-01-21 10:43:35 -0500734 if (fActiveOpsTask && (fActiveOpsTask->target(0) == proxy.get())) {
Adlai Holler4ef108e2020-12-30 16:21:13 +0000735 SkASSERT(this->getLastRenderTask(proxy.get()) == fActiveOpsTask);
736 this->insertTaskBeforeLast(waitTask);
737 // In this case we keep the current renderTask open but just insert the new waitTask
738 // before it in the list. The waitTask will never need to trigger any resolves or mip
739 // map generation which is the main advantage of going through the proxy version.
740 // Additionally we would've had to temporarily set the wait task as the lastRenderTask
741 // on the proxy, add the dependency, and then reset the lastRenderTask to
742 // fActiveOpsTask. Additionally we make the waitTask depend on all of fActiveOpsTask
743 // dependencies so that we don't unnecessarily reorder the waitTask before them.
744 // Note: Any previous Ops already in fActiveOpsTask will get blocked by the wait
745 // semaphore even though they don't need to be for correctness.
746
747 // Make sure we add the dependencies of fActiveOpsTask to waitTask first or else we'll
748 // get a circular self dependency of waitTask on waitTask.
749 waitTask->addDependenciesFromOtherTask(fActiveOpsTask);
750 fActiveOpsTask->addDependency(waitTask.get());
Adlai Holler76893192020-12-22 19:41:19 +0000751 } else {
Adlai Holler4ef108e2020-12-30 16:21:13 +0000752 // In this case we just close the previous RenderTask and start and append the waitTask
753 // to the DAG. Since it is the last task now we call setLastRenderTask on the proxy. If
754 // there is a lastTask on the proxy we make waitTask depend on that task. This
755 // dependency isn't strictly needed but it does keep the DAG from reordering the
756 // waitTask earlier and blocking more tasks.
757 if (GrRenderTask* lastTask = this->getLastRenderTask(proxy.get())) {
758 waitTask->addDependency(lastTask);
Adlai Holler76893192020-12-22 19:41:19 +0000759 }
Adlai Holler4ef108e2020-12-30 16:21:13 +0000760 this->setLastRenderTask(proxy.get(), waitTask.get());
761 this->closeActiveOpsTask();
762 this->appendTask(waitTask);
Greg Danielc30f1a92019-09-06 15:28:58 -0400763 }
764 waitTask->makeClosed(caps);
765
766 SkDEBUGCODE(this->validate());
767}
768
Greg Danielbbfec9d2019-08-20 10:56:51 -0400769void GrDrawingManager::newTransferFromRenderTask(sk_sp<GrSurfaceProxy> srcProxy,
770 const SkIRect& srcRect,
771 GrColorType surfaceColorType,
772 GrColorType dstColorType,
773 sk_sp<GrGpuBuffer> dstBuffer,
774 size_t dstOffset) {
775 SkDEBUGCODE(this->validate());
776 SkASSERT(fContext);
Adlai Holler4ef108e2020-12-30 16:21:13 +0000777 this->closeActiveOpsTask();
Greg Danielbbfec9d2019-08-20 10:56:51 -0400778
Adlai Holler33432272020-11-11 13:54:37 -0500779 GrRenderTask* task = this->appendTask(sk_make_sp<GrTransferFromRenderTask>(
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400780 srcProxy, srcRect, surfaceColorType, dstColorType,
781 std::move(dstBuffer), dstOffset));
Greg Danielbbfec9d2019-08-20 10:56:51 -0400782
783 const GrCaps& caps = *fContext->priv().caps();
784
Brian Salomon7e67dca2020-07-21 09:27:25 -0400785 // We always say GrMipmapped::kNo here since we are always just copying from the base layer. We
Greg Danielbbfec9d2019-08-20 10:56:51 -0400786 // don't need to make sure the whole mip map chain is valid.
Brian Salomon7e67dca2020-07-21 09:27:25 -0400787 task->addDependency(this, srcProxy.get(), GrMipmapped::kNo,
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400788 GrTextureResolveManager(this), caps);
Greg Danielbbfec9d2019-08-20 10:56:51 -0400789 task->makeClosed(caps);
790
Greg Danielbbfec9d2019-08-20 10:56:51 -0400791 // We have closed the previous active oplist but since a new oplist isn't being added there
792 // shouldn't be an active one.
Adlai Holler039f90c2020-11-19 15:20:31 +0000793 SkASSERT(!fActiveOpsTask);
Greg Danielbbfec9d2019-08-20 10:56:51 -0400794 SkDEBUGCODE(this->validate());
795}
796
Brian Salomond63638b2021-03-05 14:00:07 -0500797sk_sp<GrRenderTask> GrDrawingManager::newCopyRenderTask(sk_sp<GrSurfaceProxy> src,
798 SkIRect srcRect,
799 sk_sp<GrSurfaceProxy> dst,
800 SkIPoint dstPoint,
801 GrSurfaceOrigin origin) {
Greg Daniele227fe42019-08-21 13:52:24 -0400802 SkDEBUGCODE(this->validate());
803 SkASSERT(fContext);
Greg Daniele227fe42019-08-21 13:52:24 -0400804
Brian Salomon56c78f42021-02-03 16:56:55 -0500805 // It'd be nicer to check this in GrCopyRenderTask::Make. This gets complicated because of
806 // "active ops task" tracking. dst will be the target of our copy task but it might also be the
807 // target of the active ops task. We currently require the active ops task to be closed before
808 // making a new task that targets the same proxy. However, if we first close the active ops
809 // task, then fail to make a copy task, the next active ops task may target the same proxy. This
810 // will trip an assert related to unnecessary ops task splitting.
811 if (src->framebufferOnly()) {
Brian Salomond63638b2021-03-05 14:00:07 -0500812 return nullptr;
Brian Salomon56c78f42021-02-03 16:56:55 -0500813 }
814
Brian Salomon507c5652021-02-03 20:32:06 +0000815 this->closeActiveOpsTask();
816
Brian Salomond63638b2021-03-05 14:00:07 -0500817 sk_sp<GrRenderTask> task = GrCopyRenderTask::Make(this,
818 src,
819 srcRect,
820 std::move(dst),
821 dstPoint,
822 origin);
Greg Daniele227fe42019-08-21 13:52:24 -0400823 if (!task) {
Brian Salomond63638b2021-03-05 14:00:07 -0500824 return nullptr;
Greg Daniele227fe42019-08-21 13:52:24 -0400825 }
826
Brian Salomond63638b2021-03-05 14:00:07 -0500827 this->appendTask(task);
828
Brian Salomon0f9f8002021-01-22 16:30:50 -0500829 const GrCaps& caps = *fContext->priv().caps();
Brian Salomon7e67dca2020-07-21 09:27:25 -0400830 // We always say GrMipmapped::kNo here since we are always just copying from the base layer to
Greg Daniele227fe42019-08-21 13:52:24 -0400831 // another base layer. We don't need to make sure the whole mip map chain is valid.
Brian Salomon507c5652021-02-03 20:32:06 +0000832 task->addDependency(this, src.get(), GrMipmapped::kNo, GrTextureResolveManager(this), caps);
833 task->makeClosed(caps);
Greg Daniele227fe42019-08-21 13:52:24 -0400834
Greg Daniele227fe42019-08-21 13:52:24 -0400835 // We have closed the previous active oplist but since a new oplist isn't being added there
836 // shouldn't be an active one.
Adlai Holler039f90c2020-11-19 15:20:31 +0000837 SkASSERT(!fActiveOpsTask);
Greg Daniele227fe42019-08-21 13:52:24 -0400838 SkDEBUGCODE(this->validate());
Brian Salomond63638b2021-03-05 14:00:07 -0500839 return task;
Greg Daniele227fe42019-08-21 13:52:24 -0400840}
841
Brian Salomonbe1084b2021-01-26 13:29:30 -0500842bool GrDrawingManager::newWritePixelsTask(sk_sp<GrSurfaceProxy> dst,
843 SkIRect rect,
844 GrColorType srcColorType,
845 GrColorType dstColorType,
846 const GrMipLevel levels[],
Brian Salomon75ee7372021-04-06 15:04:35 -0400847 int levelCount) {
Brian Salomonbe1084b2021-01-26 13:29:30 -0500848 SkDEBUGCODE(this->validate());
849 SkASSERT(fContext);
850
851 this->closeActiveOpsTask();
852 const GrCaps& caps = *fContext->priv().caps();
853
854 // On platforms that prefer flushes over VRAM use (i.e., ANGLE) we're better off forcing a
855 // complete flush here.
856 if (!caps.preferVRAMUseOverFlushes()) {
857 this->flushSurfaces(SkSpan<GrSurfaceProxy*>{},
858 SkSurface::BackendSurfaceAccess::kNoAccess,
859 GrFlushInfo{},
860 nullptr);
861 }
862
863 GrRenderTask* task = this->appendTask(GrWritePixelsTask::Make(this,
864 std::move(dst),
865 rect,
866 srcColorType,
867 dstColorType,
868 levels,
Brian Salomon75ee7372021-04-06 15:04:35 -0400869 levelCount));
Brian Salomonbe1084b2021-01-26 13:29:30 -0500870 if (!task) {
871 return false;
872 }
873
874 task->makeClosed(caps);
875
876 // We have closed the previous active oplist but since a new oplist isn't being added there
877 // shouldn't be an active one.
878 SkASSERT(!fActiveOpsTask);
879 SkDEBUGCODE(this->validate());
880 return true;
881}
882
Robert Phillips3674f582021-06-16 12:05:54 -0400883#if GR_OGA
robertphillips68737822015-10-29 12:12:21 -0700884/*
885 * This method finds a path renderer that can draw the specified path on
886 * the provided target.
887 * Due to its expense, the software path renderer has split out so it can
888 * can be individually allowed/disallowed via the "allowSW" boolean.
889 */
890GrPathRenderer* GrDrawingManager::getPathRenderer(const GrPathRenderer::CanDrawPathArgs& args,
891 bool allowSW,
892 GrPathRendererChain::DrawType drawType,
893 GrPathRenderer::StencilSupport* stencilSupport) {
894
895 if (!fPathRendererChain) {
John Stilesfbd050b2020-08-03 13:21:46 -0400896 fPathRendererChain =
897 std::make_unique<GrPathRendererChain>(fContext, fOptionsForPathRendererChain);
robertphillips68737822015-10-29 12:12:21 -0700898 }
899
900 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(args, drawType, stencilSupport);
901 if (!pr && allowSW) {
Brian Salomone7df0bb2018-05-07 14:44:57 -0400902 auto swPR = this->getSoftwarePathRenderer();
903 if (GrPathRenderer::CanDrawPath::kNo != swPR->canDrawPath(args)) {
904 pr = swPR;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500905 }
robertphillips68737822015-10-29 12:12:21 -0700906 }
907
Robert Phillipsd81379d2020-04-21 10:39:02 -0400908#if GR_PATH_RENDERER_SPEW
909 if (pr) {
910 SkDebugf("getPathRenderer: %s\n", pr->name());
911 }
912#endif
913
robertphillips68737822015-10-29 12:12:21 -0700914 return pr;
915}
916
Brian Salomone7df0bb2018-05-07 14:44:57 -0400917GrPathRenderer* GrDrawingManager::getSoftwarePathRenderer() {
918 if (!fSoftwarePathRenderer) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400919 fSoftwarePathRenderer.reset(
Robert Phillips9da87e02019-02-04 13:26:26 -0500920 new GrSoftwarePathRenderer(fContext->priv().proxyProvider(),
Ben Wagner9ec70c62018-07-12 13:30:47 -0400921 fOptionsForPathRendererChain.fAllowPathMaskCaching));
Brian Salomone7df0bb2018-05-07 14:44:57 -0400922 }
Ben Wagner9ec70c62018-07-12 13:30:47 -0400923 return fSoftwarePathRenderer.get();
Brian Salomone7df0bb2018-05-07 14:44:57 -0400924}
925
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600926GrTessellationPathRenderer* GrDrawingManager::getTessellationPathRenderer() {
Chris Daltone6ae4762021-02-05 14:56:21 -0700927 if (!fPathRendererChain) {
928 fPathRendererChain = std::make_unique<GrPathRendererChain>(fContext,
929 fOptionsForPathRendererChain);
930 }
931 return fPathRendererChain->getTessellationPathRenderer();
932}
933
Robert Phillips3674f582021-06-16 12:05:54 -0400934#endif // GR_OGA
935
Brian Salomon653f42f2018-07-10 10:07:31 -0400936void GrDrawingManager::flushIfNecessary() {
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400937 auto direct = fContext->asDirectContext();
Robert Phillips6a6de562019-02-15 15:19:15 -0500938 if (!direct) {
939 return;
940 }
941
942 auto resourceCache = direct->priv().getResourceCache();
Brian Salomon653f42f2018-07-10 10:07:31 -0400943 if (resourceCache && resourceCache->requestsFlush()) {
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500944 if (this->flush({}, SkSurface::BackendSurfaceAccess::kNoAccess, GrFlushInfo(), nullptr)) {
Greg Danielfe159622020-04-10 17:43:51 +0000945 this->submitToGpu(false);
946 }
Brian Salomon57d2beab2018-09-10 09:35:41 -0400947 resourceCache->purgeAsNeeded();
Brian Salomon653f42f2018-07-10 10:07:31 -0400948 }
949}