blob: 109aac034d72308b9d2483474a42142364a131a6 [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) {
Chris Daltonaa938ce2021-06-23 18:13:59 -0600181 onFlushRenderTask->makeClosed(fContext);
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 Daniel0b04b6b2021-06-24 19:19:00 -0400376 // could have merged 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 Daniel0b04b6b2021-06-24 19:19:00 -0400383 SkASSERT(!prevOpsTask->canMerge(curOpsTask));
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() {
Adlai Holler33432272020-11-11 13:54:37 -0500447 for (auto& task : fDAG) {
448 if (task) {
Chris Daltonaa938ce2021-06-23 18:13:59 -0600449 task->makeClosed(fContext);
Adlai Holler33432272020-11-11 13:54:37 -0500450 }
451 }
452}
453
454GrRenderTask* GrDrawingManager::insertTaskBeforeLast(sk_sp<GrRenderTask> task) {
455 SkASSERT(!fDAG.empty());
456 if (!task) {
457 return nullptr;
458 }
459 // Release 'fDAG.back()' and grab the raw pointer, in case the SkTArray grows
460 // and reallocates during emplace_back.
461 // TODO: Either use std::vector that can do this for us, or use SkSTArray to get the
462 // perf win.
463 fDAG.emplace_back(fDAG.back().release());
464 return (fDAG[fDAG.count() - 2] = std::move(task)).get();
465}
466
467GrRenderTask* GrDrawingManager::appendTask(sk_sp<GrRenderTask> task) {
468 if (!task) {
469 return nullptr;
470 }
471 return fDAG.push_back(std::move(task)).get();
472}
473
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400474static void resolve_and_mipmap(GrGpu* gpu, GrSurfaceProxy* proxy) {
475 if (!proxy->isInstantiated()) {
476 return;
477 }
478
479 // In the flushSurfaces case, we need to resolve MSAA immediately after flush. This is
480 // because clients expect the flushed surface's backing texture to be fully resolved
481 // upon return.
482 if (proxy->requiresManualMSAAResolve()) {
483 auto* rtProxy = proxy->asRenderTargetProxy();
484 SkASSERT(rtProxy);
485 if (rtProxy->isMSAADirty()) {
486 SkASSERT(rtProxy->peekRenderTarget());
Jim Van Verthbb61fe32020-07-07 16:39:04 -0400487 gpu->resolveRenderTarget(rtProxy->peekRenderTarget(), rtProxy->msaaDirtyRect());
488 gpu->submitToGpu(false);
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400489 rtProxy->markMSAAResolved();
490 }
491 }
492 // If, after a flush, any of the proxies of interest have dirty mipmaps, regenerate them in
493 // case their backend textures are being stolen.
494 // (This special case is exercised by the ReimportImageTextureWithMipLevels test.)
495 // FIXME: It may be more ideal to plumb down a "we're going to steal the backends" flag.
496 if (auto* textureProxy = proxy->asTextureProxy()) {
Brian Salomon8c82a872020-07-21 12:09:58 -0400497 if (textureProxy->mipmapsAreDirty()) {
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400498 SkASSERT(textureProxy->peekTexture());
499 gpu->regenerateMipMapLevels(textureProxy->peekTexture());
Brian Salomon8c82a872020-07-21 12:09:58 -0400500 textureProxy->markMipmapsClean();
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400501 }
502 }
503}
504
Greg Daniel9efe3862020-06-11 11:51:06 -0400505GrSemaphoresSubmitted GrDrawingManager::flushSurfaces(
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500506 SkSpan<GrSurfaceProxy*> proxies,
Greg Daniel9efe3862020-06-11 11:51:06 -0400507 SkSurface::BackendSurfaceAccess access,
508 const GrFlushInfo& info,
509 const GrBackendSurfaceMutableState* newState) {
bsalomon6a2b1942016-09-08 11:28:59 -0700510 if (this->wasAbandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -0400511 if (info.fSubmittedProc) {
512 info.fSubmittedProc(info.fSubmittedContext, false);
513 }
514 if (info.fFinishedProc) {
515 info.fFinishedProc(info.fFinishedContext);
516 }
Greg Daniel51316782017-08-02 15:10:09 +0000517 return GrSemaphoresSubmitted::kNo;
bsalomon6a2b1942016-09-08 11:28:59 -0700518 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400519 SkDEBUGCODE(this->validate());
bsalomon6a2b1942016-09-08 11:28:59 -0700520
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400521 auto direct = fContext->asDirectContext();
Adlai Holleraee25fd2020-11-06 12:34:56 -0500522 SkASSERT(direct);
Robert Phillips6a6de562019-02-15 15:19:15 -0500523 GrGpu* gpu = direct->priv().getGpu();
Greg Daniel55822f12020-05-26 11:26:45 -0400524 // We have a non abandoned and direct GrContext. It must have a GrGpu.
525 SkASSERT(gpu);
Robert Phillips874b5352018-03-16 08:48:24 -0400526
Robert Phillipsacc10fa2019-04-01 09:50:20 -0400527 // TODO: It is important to upgrade the drawingmanager to just flushing the
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400528 // portion of the DAG required by 'proxies' in order to restore some of the
Robert Phillipsacc10fa2019-04-01 09:50:20 -0400529 // semantics of this method.
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500530 bool didFlush = this->flush(proxies, access, info, newState);
531 for (GrSurfaceProxy* proxy : proxies) {
532 resolve_and_mipmap(gpu, proxy);
bsalomon6a2b1942016-09-08 11:28:59 -0700533 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400534
535 SkDEBUGCODE(this->validate());
Greg Danielfe159622020-04-10 17:43:51 +0000536
Greg Daniel04283f32020-05-20 13:16:00 -0400537 if (!didFlush || (!direct->priv().caps()->semaphoreSupport() && info.fNumSemaphores)) {
Greg Danielfe159622020-04-10 17:43:51 +0000538 return GrSemaphoresSubmitted::kNo;
539 }
540 return GrSemaphoresSubmitted::kYes;
bsalomon6a2b1942016-09-08 11:28:59 -0700541}
542
Chris Daltonfe199b72017-05-05 11:26:15 -0400543void GrDrawingManager::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
544 fOnFlushCBObjects.push_back(onFlushCBObject);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400545}
546
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500547#if GR_TEST_UTILS
548void GrDrawingManager::testingOnly_removeOnFlushCallbackObject(GrOnFlushCallbackObject* cb) {
549 int n = std::find(fOnFlushCBObjects.begin(), fOnFlushCBObjects.end(), cb) -
550 fOnFlushCBObjects.begin();
551 SkASSERT(n < fOnFlushCBObjects.count());
552 fOnFlushCBObjects.removeShuffle(n);
553}
554#endif
555
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400556void GrDrawingManager::setLastRenderTask(const GrSurfaceProxy* proxy, GrRenderTask* task) {
557#ifdef SK_DEBUG
Robert Phillips07f675d2020-11-16 13:44:01 -0500558 if (auto prior = this->getLastRenderTask(proxy)) {
559 SkASSERT(prior->isClosed() || prior == task);
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400560 }
561#endif
Adlai Holler25df1f72020-06-09 13:08:27 -0400562 uint32_t key = proxy->uniqueID().asUInt();
563 if (task) {
564 fLastRenderTasks.set(key, task);
565 } else if (fLastRenderTasks.find(key)) {
566 fLastRenderTasks.remove(key);
567 }
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400568}
569
570GrRenderTask* GrDrawingManager::getLastRenderTask(const GrSurfaceProxy* proxy) const {
571 auto entry = fLastRenderTasks.find(proxy->uniqueID().asUInt());
572 return entry ? *entry : nullptr;
573}
574
575GrOpsTask* GrDrawingManager::getLastOpsTask(const GrSurfaceProxy* proxy) const {
576 GrRenderTask* task = this->getLastRenderTask(proxy);
577 return task ? task->asOpsTask() : nullptr;
578}
579
580
Chris Dalton6b498102019-08-01 14:14:52 -0600581void GrDrawingManager::moveRenderTasksToDDL(SkDeferredDisplayList* ddl) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400582 SkDEBUGCODE(this->validate());
583
Chris Dalton6b498102019-08-01 14:14:52 -0600584 // no renderTask should receive a new command after this
Adlai Holler33432272020-11-11 13:54:37 -0500585 this->closeAllTasks();
Adlai Holler039f90c2020-11-19 15:20:31 +0000586 fActiveOpsTask = nullptr;
Robert Phillips7a137052018-02-01 11:23:12 -0500587
Robert Phillips07f675d2020-11-16 13:44:01 -0500588 this->sortTasks();
589
Adlai Holler33432272020-11-11 13:54:37 -0500590 fDAG.swap(ddl->fRenderTasks);
591 SkASSERT(fDAG.empty());
Robert Phillips867ce8f2018-06-21 10:28:36 -0400592
Robert Phillips19f466d2020-02-26 10:27:07 -0500593 for (auto& renderTask : ddl->fRenderTasks) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400594 renderTask->disown(this);
Robert Phillips29f38542019-10-16 09:20:25 -0400595 renderTask->prePrepare(fContext);
Robert Phillips7327c9d2019-10-08 16:32:56 -0400596 }
597
Michael Ludwig2c316bd2019-12-19 14:50:44 -0500598 ddl->fArenas = std::move(fContext->priv().detachArenas());
Robert Phillips61fc7992019-10-22 11:58:17 -0400599
Robert Phillipsf6a0b452020-02-18 14:26:46 -0500600 fContext->priv().detachProgramData(&ddl->fProgramData);
Robert Phillips576b6a12019-12-06 13:05:49 -0500601
Robert Phillips38d64b02018-09-04 13:23:26 -0400602 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500603}
604
Robert Phillips07f675d2020-11-16 13:44:01 -0500605void GrDrawingManager::createDDLTask(sk_sp<const SkDeferredDisplayList> ddl,
Robert Phillipseb54bb52021-01-08 17:20:18 -0500606 sk_sp<GrRenderTargetProxy> newDest,
Robert Phillips88b29612020-11-16 15:15:08 -0500607 SkIPoint offset) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400608 SkDEBUGCODE(this->validate());
609
Adlai Holler039f90c2020-11-19 15:20:31 +0000610 if (fActiveOpsTask) {
Adlai Hollerc44e21f2021-02-22 14:08:07 -0500611 // This is a temporary fix for the partial-MDB world. In that world we're not
Adlai Holler039f90c2020-11-19 15:20:31 +0000612 // reordering so ops that (in the single opsTask world) would've just glommed onto the
613 // end of the single opsTask but referred to a far earlier RT need to appear in their
614 // own opsTask.
Chris Daltonaa938ce2021-06-23 18:13:59 -0600615 fActiveOpsTask->makeClosed(fContext);
Adlai Holler039f90c2020-11-19 15:20:31 +0000616 fActiveOpsTask = nullptr;
Robert Phillips38d64b02018-09-04 13:23:26 -0400617 }
618
Robert Phillips07f675d2020-11-16 13:44:01 -0500619 // Propagate the DDL proxy's state information to the replay target.
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400620 if (ddl->priv().targetProxy()->isMSAADirty()) {
Brian Salomon982127b2021-01-21 10:43:35 -0500621 auto nativeRect = GrNativeRect::MakeIRectRelativeTo(
622 ddl->characterization().origin(),
623 ddl->priv().targetProxy()->backingStoreDimensions().height(),
624 ddl->priv().targetProxy()->msaaDirtyRect());
625 newDest->markMSAADirty(nativeRect);
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400626 }
627 GrTextureProxy* newTextureProxy = newDest->asTextureProxy();
Brian Salomon8c82a872020-07-21 12:09:58 -0400628 if (newTextureProxy && GrMipmapped::kYes == newTextureProxy->mipmapped()) {
629 newTextureProxy->markMipmapsDirty();
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400630 }
631
Robert Phillips62000362018-02-01 09:10:04 -0500632 // Here we jam the proxy that backs the current replay SkSurface into the LazyProxyData.
Robert Phillips07f675d2020-11-16 13:44:01 -0500633 // The lazy proxy that references it (in the DDL opsTasks) will then steal its GrTexture.
Robert Phillipseb54bb52021-01-08 17:20:18 -0500634 ddl->fLazyProxyData->fReplayDest = newDest.get();
Robert Phillips774168e2018-05-31 12:43:27 -0400635
Robert Phillips07f675d2020-11-16 13:44:01 -0500636 // Add a task to handle drawing and lifetime management of the DDL.
637 SkDEBUGCODE(auto ddlTask =) this->appendTask(sk_make_sp<GrDDLTask>(this,
Robert Phillipseb54bb52021-01-08 17:20:18 -0500638 std::move(newDest),
Robert Phillips88b29612020-11-16 15:15:08 -0500639 std::move(ddl),
640 offset));
Robert Phillips07f675d2020-11-16 13:44:01 -0500641 SkASSERT(ddlTask->isClosed());
Adlai Holler7580ad42020-06-24 13:45:25 -0400642
Robert Phillips38d64b02018-09-04 13:23:26 -0400643 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500644}
645
Robert Phillips38d64b02018-09-04 13:23:26 -0400646#ifdef SK_DEBUG
647void GrDrawingManager::validate() const {
Adlai Holler4ef108e2020-12-30 16:21:13 +0000648 if (fActiveOpsTask) {
649 SkASSERT(!fDAG.empty());
650 SkASSERT(!fActiveOpsTask->isClosed());
651 SkASSERT(fActiveOpsTask == fDAG.back().get());
652 }
Adlai Holler039f90c2020-11-19 15:20:31 +0000653
Adlai Holler4ef108e2020-12-30 16:21:13 +0000654 for (int i = 0; i < fDAG.count(); ++i) {
655 if (fActiveOpsTask != fDAG[i].get()) {
656 // The resolveTask associated with the activeTask remains open for as long as the
657 // activeTask does.
658 bool isActiveResolveTask =
659 fActiveOpsTask && fActiveOpsTask->fTextureResolveTask == fDAG[i].get();
Chris Dalton83420eb2021-06-23 18:47:09 -0600660 bool isAtlas = fDAG[i]->isSetFlag(GrRenderTask::kAtlas_Flag);
661 SkASSERT(isActiveResolveTask || isAtlas || 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.
Chris Daltonaa938ce2021-06-23 18:13:59 -0600677 fActiveOpsTask->makeClosed(fContext);
Adlai Holler039f90c2020-11-19 15:20:31 +0000678 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
Chris Dalton83420eb2021-06-23 18:47:09 -0600708void GrDrawingManager::addAtlasTask(sk_sp<GrRenderTask> atlasTask,
709 GrRenderTask* previousAtlasTask) {
710 SkDEBUGCODE(this->validate());
711 SkASSERT(fContext);
712
713 if (previousAtlasTask) {
714 previousAtlasTask->makeClosed(fContext);
715 for (GrRenderTask* previousAtlasUser : previousAtlasTask->dependents()) {
716 // Make the new atlas depend on everybody who used the old atlas, and close their tasks.
717 // This guarantees that the previous atlas is totally out of service before we render
718 // the next one, meaning there is only ever one atlas active at a time and that they can
719 // all share the same texture.
720 atlasTask->addDependency(previousAtlasUser);
721 previousAtlasUser->makeClosed(fContext);
722 if (previousAtlasUser == fActiveOpsTask) {
723 fActiveOpsTask = nullptr;
724 }
725 }
726 }
727
728 atlasTask->setFlag(GrRenderTask::kAtlas_Flag);
729 this->insertTaskBeforeLast(std::move(atlasTask));
730
731 SkDEBUGCODE(this->validate());
732}
733
Adlai Holler039f90c2020-11-19 15:20:31 +0000734GrTextureResolveRenderTask* GrDrawingManager::newTextureResolveRenderTask(const GrCaps& caps) {
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600735 // 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 +0000736 // in sorting and opsTask reduction mode) the render tasks that depend on any proxy's current
Greg Danielf41b2bd2019-08-22 16:19:24 -0400737 // state. This is because those opsTasks can still receive new ops and because if they refer to
Adlai Holler039f90c2020-11-19 15:20:31 +0000738 // the mipmapped version of 'proxy', they will then come to depend on the render task being
Chris Dalton4ece96d2019-08-30 11:26:39 -0600739 // created here.
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600740 //
Adlai Holler039f90c2020-11-19 15:20:31 +0000741 // Add the new textureResolveTask before the fActiveOpsTask (if not in
742 // sorting/opsTask-splitting-reduction mode) because it will depend upon this resolve task.
Chris Dalton3d770272019-08-14 09:24:37 -0600743 // NOTE: Putting it here will also reduce the amount of work required by the topological sort.
Adlai Holler33432272020-11-11 13:54:37 -0500744 GrRenderTask* task = this->insertTaskBeforeLast(sk_make_sp<GrTextureResolveRenderTask>());
745 return static_cast<GrTextureResolveRenderTask*>(task);
Chris Dalton3d770272019-08-14 09:24:37 -0600746}
747
Greg Danielc30f1a92019-09-06 15:28:58 -0400748void GrDrawingManager::newWaitRenderTask(sk_sp<GrSurfaceProxy> proxy,
Greg Daniel301015c2019-11-18 14:06:46 -0500749 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> semaphores,
Greg Danielc30f1a92019-09-06 15:28:58 -0400750 int numSemaphores) {
751 SkDEBUGCODE(this->validate());
752 SkASSERT(fContext);
753
Greg Daniel16f5c652019-10-29 11:26:01 -0400754 sk_sp<GrWaitRenderTask> waitTask = sk_make_sp<GrWaitRenderTask>(GrSurfaceProxyView(proxy),
755 std::move(semaphores),
Greg Danielc30f1a92019-09-06 15:28:58 -0400756 numSemaphores);
Greg Danielc30f1a92019-09-06 15:28:58 -0400757
Brian Salomon982127b2021-01-21 10:43:35 -0500758 if (fActiveOpsTask && (fActiveOpsTask->target(0) == proxy.get())) {
Adlai Holler4ef108e2020-12-30 16:21:13 +0000759 SkASSERT(this->getLastRenderTask(proxy.get()) == fActiveOpsTask);
760 this->insertTaskBeforeLast(waitTask);
761 // In this case we keep the current renderTask open but just insert the new waitTask
762 // before it in the list. The waitTask will never need to trigger any resolves or mip
763 // map generation which is the main advantage of going through the proxy version.
764 // Additionally we would've had to temporarily set the wait task as the lastRenderTask
765 // on the proxy, add the dependency, and then reset the lastRenderTask to
766 // fActiveOpsTask. Additionally we make the waitTask depend on all of fActiveOpsTask
767 // dependencies so that we don't unnecessarily reorder the waitTask before them.
768 // Note: Any previous Ops already in fActiveOpsTask will get blocked by the wait
769 // semaphore even though they don't need to be for correctness.
770
771 // Make sure we add the dependencies of fActiveOpsTask to waitTask first or else we'll
772 // get a circular self dependency of waitTask on waitTask.
773 waitTask->addDependenciesFromOtherTask(fActiveOpsTask);
774 fActiveOpsTask->addDependency(waitTask.get());
Adlai Holler76893192020-12-22 19:41:19 +0000775 } else {
Adlai Holler4ef108e2020-12-30 16:21:13 +0000776 // In this case we just close the previous RenderTask and start and append the waitTask
777 // to the DAG. Since it is the last task now we call setLastRenderTask on the proxy. If
778 // there is a lastTask on the proxy we make waitTask depend on that task. This
779 // dependency isn't strictly needed but it does keep the DAG from reordering the
780 // waitTask earlier and blocking more tasks.
781 if (GrRenderTask* lastTask = this->getLastRenderTask(proxy.get())) {
782 waitTask->addDependency(lastTask);
Adlai Holler76893192020-12-22 19:41:19 +0000783 }
Adlai Holler4ef108e2020-12-30 16:21:13 +0000784 this->setLastRenderTask(proxy.get(), waitTask.get());
785 this->closeActiveOpsTask();
786 this->appendTask(waitTask);
Greg Danielc30f1a92019-09-06 15:28:58 -0400787 }
Chris Daltonaa938ce2021-06-23 18:13:59 -0600788 waitTask->makeClosed(fContext);
Greg Danielc30f1a92019-09-06 15:28:58 -0400789
790 SkDEBUGCODE(this->validate());
791}
792
Greg Danielbbfec9d2019-08-20 10:56:51 -0400793void GrDrawingManager::newTransferFromRenderTask(sk_sp<GrSurfaceProxy> srcProxy,
794 const SkIRect& srcRect,
795 GrColorType surfaceColorType,
796 GrColorType dstColorType,
797 sk_sp<GrGpuBuffer> dstBuffer,
798 size_t dstOffset) {
799 SkDEBUGCODE(this->validate());
800 SkASSERT(fContext);
Adlai Holler4ef108e2020-12-30 16:21:13 +0000801 this->closeActiveOpsTask();
Greg Danielbbfec9d2019-08-20 10:56:51 -0400802
Adlai Holler33432272020-11-11 13:54:37 -0500803 GrRenderTask* task = this->appendTask(sk_make_sp<GrTransferFromRenderTask>(
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400804 srcProxy, srcRect, surfaceColorType, dstColorType,
805 std::move(dstBuffer), dstOffset));
Greg Danielbbfec9d2019-08-20 10:56:51 -0400806
807 const GrCaps& caps = *fContext->priv().caps();
808
Brian Salomon7e67dca2020-07-21 09:27:25 -0400809 // We always say GrMipmapped::kNo here since we are always just copying from the base layer. We
Greg Danielbbfec9d2019-08-20 10:56:51 -0400810 // don't need to make sure the whole mip map chain is valid.
Brian Salomon7e67dca2020-07-21 09:27:25 -0400811 task->addDependency(this, srcProxy.get(), GrMipmapped::kNo,
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400812 GrTextureResolveManager(this), caps);
Chris Daltonaa938ce2021-06-23 18:13:59 -0600813 task->makeClosed(fContext);
Greg Danielbbfec9d2019-08-20 10:56:51 -0400814
Greg Danielbbfec9d2019-08-20 10:56:51 -0400815 // We have closed the previous active oplist but since a new oplist isn't being added there
816 // shouldn't be an active one.
Adlai Holler039f90c2020-11-19 15:20:31 +0000817 SkASSERT(!fActiveOpsTask);
Greg Danielbbfec9d2019-08-20 10:56:51 -0400818 SkDEBUGCODE(this->validate());
819}
820
Brian Salomond63638b2021-03-05 14:00:07 -0500821sk_sp<GrRenderTask> GrDrawingManager::newCopyRenderTask(sk_sp<GrSurfaceProxy> src,
822 SkIRect srcRect,
823 sk_sp<GrSurfaceProxy> dst,
824 SkIPoint dstPoint,
825 GrSurfaceOrigin origin) {
Greg Daniele227fe42019-08-21 13:52:24 -0400826 SkDEBUGCODE(this->validate());
827 SkASSERT(fContext);
Greg Daniele227fe42019-08-21 13:52:24 -0400828
Brian Salomon56c78f42021-02-03 16:56:55 -0500829 // It'd be nicer to check this in GrCopyRenderTask::Make. This gets complicated because of
830 // "active ops task" tracking. dst will be the target of our copy task but it might also be the
831 // target of the active ops task. We currently require the active ops task to be closed before
832 // making a new task that targets the same proxy. However, if we first close the active ops
833 // task, then fail to make a copy task, the next active ops task may target the same proxy. This
834 // will trip an assert related to unnecessary ops task splitting.
835 if (src->framebufferOnly()) {
Brian Salomond63638b2021-03-05 14:00:07 -0500836 return nullptr;
Brian Salomon56c78f42021-02-03 16:56:55 -0500837 }
838
Brian Salomon507c5652021-02-03 20:32:06 +0000839 this->closeActiveOpsTask();
840
Brian Salomond63638b2021-03-05 14:00:07 -0500841 sk_sp<GrRenderTask> task = GrCopyRenderTask::Make(this,
842 src,
843 srcRect,
844 std::move(dst),
845 dstPoint,
846 origin);
Greg Daniele227fe42019-08-21 13:52:24 -0400847 if (!task) {
Brian Salomond63638b2021-03-05 14:00:07 -0500848 return nullptr;
Greg Daniele227fe42019-08-21 13:52:24 -0400849 }
850
Brian Salomond63638b2021-03-05 14:00:07 -0500851 this->appendTask(task);
852
Brian Salomon0f9f8002021-01-22 16:30:50 -0500853 const GrCaps& caps = *fContext->priv().caps();
Brian Salomon7e67dca2020-07-21 09:27:25 -0400854 // We always say GrMipmapped::kNo here since we are always just copying from the base layer to
Greg Daniele227fe42019-08-21 13:52:24 -0400855 // another base layer. We don't need to make sure the whole mip map chain is valid.
Brian Salomon507c5652021-02-03 20:32:06 +0000856 task->addDependency(this, src.get(), GrMipmapped::kNo, GrTextureResolveManager(this), caps);
Chris Daltonaa938ce2021-06-23 18:13:59 -0600857 task->makeClosed(fContext);
Greg Daniele227fe42019-08-21 13:52:24 -0400858
Greg Daniele227fe42019-08-21 13:52:24 -0400859 // We have closed the previous active oplist but since a new oplist isn't being added there
860 // shouldn't be an active one.
Adlai Holler039f90c2020-11-19 15:20:31 +0000861 SkASSERT(!fActiveOpsTask);
Greg Daniele227fe42019-08-21 13:52:24 -0400862 SkDEBUGCODE(this->validate());
Brian Salomond63638b2021-03-05 14:00:07 -0500863 return task;
Greg Daniele227fe42019-08-21 13:52:24 -0400864}
865
Brian Salomonbe1084b2021-01-26 13:29:30 -0500866bool GrDrawingManager::newWritePixelsTask(sk_sp<GrSurfaceProxy> dst,
867 SkIRect rect,
868 GrColorType srcColorType,
869 GrColorType dstColorType,
870 const GrMipLevel levels[],
Brian Salomon75ee7372021-04-06 15:04:35 -0400871 int levelCount) {
Brian Salomonbe1084b2021-01-26 13:29:30 -0500872 SkDEBUGCODE(this->validate());
873 SkASSERT(fContext);
874
875 this->closeActiveOpsTask();
876 const GrCaps& caps = *fContext->priv().caps();
877
878 // On platforms that prefer flushes over VRAM use (i.e., ANGLE) we're better off forcing a
879 // complete flush here.
880 if (!caps.preferVRAMUseOverFlushes()) {
881 this->flushSurfaces(SkSpan<GrSurfaceProxy*>{},
882 SkSurface::BackendSurfaceAccess::kNoAccess,
883 GrFlushInfo{},
884 nullptr);
885 }
886
887 GrRenderTask* task = this->appendTask(GrWritePixelsTask::Make(this,
888 std::move(dst),
889 rect,
890 srcColorType,
891 dstColorType,
892 levels,
Brian Salomon75ee7372021-04-06 15:04:35 -0400893 levelCount));
Brian Salomonbe1084b2021-01-26 13:29:30 -0500894 if (!task) {
895 return false;
896 }
897
Chris Daltonaa938ce2021-06-23 18:13:59 -0600898 task->makeClosed(fContext);
Brian Salomonbe1084b2021-01-26 13:29:30 -0500899
900 // We have closed the previous active oplist but since a new oplist isn't being added there
901 // shouldn't be an active one.
902 SkASSERT(!fActiveOpsTask);
903 SkDEBUGCODE(this->validate());
904 return true;
905}
906
Robert Phillips3674f582021-06-16 12:05:54 -0400907#if GR_OGA
robertphillips68737822015-10-29 12:12:21 -0700908/*
909 * This method finds a path renderer that can draw the specified path on
910 * the provided target.
911 * Due to its expense, the software path renderer has split out so it can
912 * can be individually allowed/disallowed via the "allowSW" boolean.
913 */
914GrPathRenderer* GrDrawingManager::getPathRenderer(const GrPathRenderer::CanDrawPathArgs& args,
915 bool allowSW,
916 GrPathRendererChain::DrawType drawType,
917 GrPathRenderer::StencilSupport* stencilSupport) {
918
919 if (!fPathRendererChain) {
John Stilesfbd050b2020-08-03 13:21:46 -0400920 fPathRendererChain =
921 std::make_unique<GrPathRendererChain>(fContext, fOptionsForPathRendererChain);
robertphillips68737822015-10-29 12:12:21 -0700922 }
923
924 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(args, drawType, stencilSupport);
925 if (!pr && allowSW) {
Brian Salomone7df0bb2018-05-07 14:44:57 -0400926 auto swPR = this->getSoftwarePathRenderer();
927 if (GrPathRenderer::CanDrawPath::kNo != swPR->canDrawPath(args)) {
928 pr = swPR;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500929 }
robertphillips68737822015-10-29 12:12:21 -0700930 }
931
Robert Phillipsd81379d2020-04-21 10:39:02 -0400932#if GR_PATH_RENDERER_SPEW
933 if (pr) {
934 SkDebugf("getPathRenderer: %s\n", pr->name());
935 }
936#endif
937
robertphillips68737822015-10-29 12:12:21 -0700938 return pr;
939}
940
Brian Salomone7df0bb2018-05-07 14:44:57 -0400941GrPathRenderer* GrDrawingManager::getSoftwarePathRenderer() {
942 if (!fSoftwarePathRenderer) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400943 fSoftwarePathRenderer.reset(
Robert Phillips9da87e02019-02-04 13:26:26 -0500944 new GrSoftwarePathRenderer(fContext->priv().proxyProvider(),
Ben Wagner9ec70c62018-07-12 13:30:47 -0400945 fOptionsForPathRendererChain.fAllowPathMaskCaching));
Brian Salomone7df0bb2018-05-07 14:44:57 -0400946 }
Ben Wagner9ec70c62018-07-12 13:30:47 -0400947 return fSoftwarePathRenderer.get();
Brian Salomone7df0bb2018-05-07 14:44:57 -0400948}
949
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600950GrTessellationPathRenderer* GrDrawingManager::getTessellationPathRenderer() {
Chris Daltone6ae4762021-02-05 14:56:21 -0700951 if (!fPathRendererChain) {
952 fPathRendererChain = std::make_unique<GrPathRendererChain>(fContext,
953 fOptionsForPathRendererChain);
954 }
955 return fPathRendererChain->getTessellationPathRenderer();
956}
957
Robert Phillips3674f582021-06-16 12:05:54 -0400958#endif // GR_OGA
959
Brian Salomon653f42f2018-07-10 10:07:31 -0400960void GrDrawingManager::flushIfNecessary() {
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400961 auto direct = fContext->asDirectContext();
Robert Phillips6a6de562019-02-15 15:19:15 -0500962 if (!direct) {
963 return;
964 }
965
966 auto resourceCache = direct->priv().getResourceCache();
Brian Salomon653f42f2018-07-10 10:07:31 -0400967 if (resourceCache && resourceCache->requestsFlush()) {
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500968 if (this->flush({}, SkSurface::BackendSurfaceAccess::kNoAccess, GrFlushInfo(), nullptr)) {
Greg Danielfe159622020-04-10 17:43:51 +0000969 this->submitToGpu(false);
970 }
Brian Salomon57d2beab2018-09-10 09:35:41 -0400971 resourceCache->purgeAsNeeded();
Brian Salomon653f42f2018-07-10 10:07:31 -0400972 }
973}