blob: 4ce8c218b30e244ef65b84f7291a8056c2865f9d [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"
33#include "src/gpu/GrSoftwarePathRenderer.h"
Greg Daniel46e366a2019-12-16 14:38:36 -050034#include "src/gpu/GrSurfaceContext.h"
Brian Salomoneebe7352020-12-09 16:37:04 -050035#include "src/gpu/GrSurfaceDrawContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050036#include "src/gpu/GrSurfaceProxyPriv.h"
Robert Phillipsfbbc3bb2020-11-16 14:58:56 -050037#include "src/gpu/GrTTopoSort.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000038#include "src/gpu/GrTexture.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040039#include "src/gpu/GrTextureProxy.h"
Adlai Holler9e2c50e2021-02-09 14:41:52 -050040#include "src/gpu/GrTextureProxyPriv.h"
Chris Dalton3d770272019-08-14 09:24:37 -060041#include "src/gpu/GrTextureResolveRenderTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050042#include "src/gpu/GrTracing.h"
Greg Danielbbfec9d2019-08-20 10:56:51 -040043#include "src/gpu/GrTransferFromRenderTask.h"
Greg Danielc30f1a92019-09-06 15:28:58 -040044#include "src/gpu/GrWaitRenderTask.h"
Brian Salomonbe1084b2021-01-26 13:29:30 -050045#include "src/gpu/GrWritePixelsRenderTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050046#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
Herb Derbybf2dd2a2021-03-04 10:13:22 -050047#include "src/gpu/text/GrSDFTControl.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050048#include "src/image/SkSurface_Gpu.h"
robertphillips498d7ac2015-10-30 10:11:30 -070049
Robert Phillips22310d62018-09-05 11:07:21 -040050///////////////////////////////////////////////////////////////////////////////////////////////////
Robert Phillips69893702019-02-22 11:16:30 -050051GrDrawingManager::GrDrawingManager(GrRecordingContext* context,
Robert Phillips22310d62018-09-05 11:07:21 -040052 const GrPathRendererChain::Options& optionsForPathRendererChain,
Greg Danielf41b2bd2019-08-22 16:19:24 -040053 bool reduceOpsTaskSplitting)
Robert Phillips22310d62018-09-05 11:07:21 -040054 : fContext(context)
55 , fOptionsForPathRendererChain(optionsForPathRendererChain)
Robert Phillips22310d62018-09-05 11:07:21 -040056 , fPathRendererChain(nullptr)
57 , fSoftwarePathRenderer(nullptr)
Robert Phillips6db27c22019-05-01 10:43:56 -040058 , fFlushing(false)
Herb Derby082232b2020-06-10 15:08:18 -040059 , fReduceOpsTaskSplitting(reduceOpsTaskSplitting) { }
Robert Phillips22310d62018-09-05 11:07:21 -040060
robertphillips3dc6ae52015-10-20 09:54:32 -070061GrDrawingManager::~GrDrawingManager() {
Adlai Holler33432272020-11-11 13:54:37 -050062 this->closeAllTasks();
Adlai Hollerc616e1c2021-02-11 15:18:17 -050063 this->removeRenderTasks();
robertphillips3dc6ae52015-10-20 09:54:32 -070064}
65
Robert Phillipsa9162df2019-02-11 14:12:03 -050066bool GrDrawingManager::wasAbandoned() const {
Robert Phillips9eb00022020-06-30 15:30:12 -040067 return fContext->abandoned();
robertphillips3dc6ae52015-10-20 09:54:32 -070068}
69
robertphillips68737822015-10-29 12:12:21 -070070void GrDrawingManager::freeGpuResources() {
Jim Van Verth106b5c42017-09-26 12:45:29 -040071 for (int i = fOnFlushCBObjects.count() - 1; i >= 0; --i) {
72 if (!fOnFlushCBObjects[i]->retainOnFreeGpuResources()) {
73 // it's safe to just do this because we're iterating in reverse
74 fOnFlushCBObjects.removeShuffle(i);
75 }
76 }
77
robertphillips68737822015-10-29 12:12:21 -070078 // a path renderer may be holding onto resources
robertphillips13391dd2015-10-30 05:15:11 -070079 fPathRendererChain = nullptr;
Ben Wagner9ec70c62018-07-12 13:30:47 -040080 fSoftwarePathRenderer = nullptr;
Robert Phillipse3302df2017-04-24 07:31:02 -040081}
82
Adlai Hollerc2bfcff2020-11-06 15:39:36 -050083// MDB TODO: make use of the 'proxies' parameter.
Greg Daniel9efe3862020-06-11 11:51:06 -040084bool GrDrawingManager::flush(
Adlai Hollerc2bfcff2020-11-06 15:39:36 -050085 SkSpan<GrSurfaceProxy*> proxies,
Greg Daniel9efe3862020-06-11 11:51:06 -040086 SkSurface::BackendSurfaceAccess access,
87 const GrFlushInfo& info,
88 const GrBackendSurfaceMutableState* newState) {
Brian Salomon57d2beab2018-09-10 09:35:41 -040089 GR_CREATE_TRACE_MARKER_CONTEXT("GrDrawingManager", "flush", fContext);
Brian Salomondcbb9d92017-07-19 10:53:20 -040090
robertphillips7761d612016-05-16 09:14:53 -070091 if (fFlushing || this->wasAbandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -040092 if (info.fSubmittedProc) {
93 info.fSubmittedProc(info.fSubmittedContext, false);
94 }
Greg Daniele6bfb7d2019-04-17 15:26:11 -040095 if (info.fFinishedProc) {
96 info.fFinishedProc(info.fFinishedContext);
Greg Daniela3aa75a2019-04-12 14:24:55 -040097 }
Greg Danielfe159622020-04-10 17:43:51 +000098 return false;
joshualittb8918c42015-12-18 09:59:46 -080099 }
Robert Phillips602df412019-04-08 11:10:39 -0400100
Robert Phillips38d64b02018-09-04 13:23:26 -0400101 SkDEBUGCODE(this->validate());
102
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500103 // As of now we only short-circuit if we got an explicit list of surfaces to flush.
104 if (!proxies.empty() && !info.fNumSemaphores && !info.fFinishedProc &&
Greg Danielce9f0162020-06-30 13:42:46 -0400105 access == SkSurface::BackendSurfaceAccess::kNoAccess && !newState) {
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500106 bool allUnused = std::all_of(proxies.begin(), proxies.end(), [&](GrSurfaceProxy* proxy) {
Adlai Holler33432272020-11-11 13:54:37 -0500107 bool used = std::any_of(fDAG.begin(), fDAG.end(), [&](auto& task) {
108 return task && task->isUsed(proxy);
109 });
Robert Phillips58a8ccc2021-01-11 09:07:55 -0500110 return !used;
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500111 });
112 if (allUnused) {
Greg Daniel55822f12020-05-26 11:26:45 -0400113 if (info.fSubmittedProc) {
114 info.fSubmittedProc(info.fSubmittedContext, true);
115 }
Greg Danielfe159622020-04-10 17:43:51 +0000116 return false;
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400117 }
Robert Phillips9313aa72019-04-09 18:41:27 -0400118 }
119
Adlai Hollerca1137b2021-04-08 11:39:55 -0400120 auto dContext = fContext->asDirectContext();
121 SkASSERT(dContext);
122 dContext->priv().clientMappedBufferManager()->process();
Robert Phillips6a6de562019-02-15 15:19:15 -0500123
Adlai Hollerca1137b2021-04-08 11:39:55 -0400124 GrGpu* gpu = dContext->priv().getGpu();
Greg Daniel55822f12020-05-26 11:26:45 -0400125 // We have a non abandoned and direct GrContext. It must have a GrGpu.
126 SkASSERT(gpu);
Greg Daniela3aa75a2019-04-12 14:24:55 -0400127
joshualittb8918c42015-12-18 09:59:46 -0800128 fFlushing = true;
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400129
Adlai Hollerca1137b2021-04-08 11:39:55 -0400130 auto resourceProvider = dContext->priv().resourceProvider();
131 auto resourceCache = dContext->priv().getResourceCache();
Robert Phillips6a6de562019-02-15 15:19:15 -0500132
Chris Dalton6b498102019-08-01 14:14:52 -0600133 // Semi-usually the GrRenderTasks are already closed at this point, but sometimes Ganesh needs
Greg Danielf41b2bd2019-08-22 16:19:24 -0400134 // to flush mid-draw. In that case, the SkGpuDevice's opsTasks won't be closed but need to be
135 // flushed anyway. Closing such opsTasks here will mean new ones will be created to replace them
Chris Dalton6b498102019-08-01 14:14:52 -0600136 // if the SkGpuDevice(s) write to them again.
Adlai Holler33432272020-11-11 13:54:37 -0500137 this->closeAllTasks();
Adlai Holler039f90c2020-11-19 15:20:31 +0000138 fActiveOpsTask = nullptr;
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400139
Adlai Holler33432272020-11-11 13:54:37 -0500140 this->sortTasks();
Adlai Holler78036082021-01-07 11:41:33 -0500141
Brian Salomon601ac802019-02-07 13:37:16 -0500142 if (!fCpuBufferCache) {
143 // We cache more buffers when the backend is using client side arrays. Otherwise, we
144 // expect each pool will use a CPU buffer as a staging buffer before uploading to a GPU
145 // buffer object. Each pool only requires one staging buffer at a time.
146 int maxCachedBuffers = fContext->priv().caps()->preferClientSideDynamicBuffers() ? 2 : 6;
147 fCpuBufferCache = GrBufferAllocPool::CpuBufferCache::Make(maxCachedBuffers);
Brian Salomon58f153c2018-10-18 21:51:15 -0400148 }
Robert Phillipsa4c93ac2017-05-18 11:40:04 -0400149
Robert Phillipse5f73282019-06-18 17:15:04 -0400150 GrOpFlushState flushState(gpu, resourceProvider, &fTokenTracker, fCpuBufferCache);
Robert Phillips40a29d72018-01-18 12:59:22 -0500151
Chris Daltonfe199b72017-05-05 11:26:15 -0400152 GrOnFlushResourceProvider onFlushProvider(this);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400153
Chris Dalton12658942017-10-05 19:45:25 -0600154 // Prepare any onFlush op lists (e.g. atlases).
Chris Daltonfe199b72017-05-05 11:26:15 -0400155 if (!fOnFlushCBObjects.empty()) {
Robert Phillips07f675d2020-11-16 13:44:01 -0500156 fFlushingRenderTaskIDs.reserve_back(fDAG.count());
157 for (const auto& task : fDAG) {
158 if (task) {
159 task->gatherIDs(&fFlushingRenderTaskIDs);
Adlai Holler33432272020-11-11 13:54:37 -0500160 }
161 }
Robert Phillips22310d62018-09-05 11:07:21 -0400162
Chris Daltonfe199b72017-05-05 11:26:15 -0400163 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
Brian Osmanae87bf12021-05-11 13:36:10 -0400164 onFlushCBObject->preFlush(&onFlushProvider, SkMakeSpan(fFlushingRenderTaskIDs));
Chris Daltonc4b47352019-08-23 10:10:36 -0600165 }
166 for (const auto& onFlushRenderTask : fOnFlushRenderTasks) {
167 onFlushRenderTask->makeClosed(*fContext->priv().caps());
Chris Dalton706a6ff2017-11-29 22:01:06 -0700168#ifdef SK_DEBUG
Chris Daltonc4b47352019-08-23 10:10:36 -0600169 // OnFlush callbacks are invoked during flush, and are therefore expected to handle
Adlai Holler9e2c50e2021-02-09 14:41:52 -0500170 // resource allocation & usage on their own. (No deferred or lazy proxies!)
Chris Daltonc4b47352019-08-23 10:10:36 -0600171 onFlushRenderTask->visitTargetAndSrcProxies_debugOnly(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400172 [](GrSurfaceProxy* p, GrMipmapped mipMapped) {
Adlai Holler9e2c50e2021-02-09 14:41:52 -0500173 SkASSERT(!p->asTextureProxy() || !p->asTextureProxy()->texPriv().isDeferred());
Brian Salomonbeb7f522019-08-30 16:19:42 -0400174 SkASSERT(!p->isLazy());
Chris Dalton4ece96d2019-08-30 11:26:39 -0600175 if (p->requiresManualMSAAResolve()) {
176 // The onFlush callback is responsible for ensuring MSAA gets resolved.
177 SkASSERT(p->asRenderTargetProxy() && !p->asRenderTargetProxy()->isMSAADirty());
178 }
Brian Salomon7e67dca2020-07-21 09:27:25 -0400179 if (GrMipmapped::kYes == mipMapped) {
Chris Daltonc4b47352019-08-23 10:10:36 -0600180 // The onFlush callback is responsible for regenerating mips if needed.
Brian Salomon8c82a872020-07-21 12:09:58 -0400181 SkASSERT(p->asTextureProxy() && !p->asTextureProxy()->mipmapsAreDirty());
Chris Daltonc4b47352019-08-23 10:10:36 -0600182 }
183 });
Chris Dalton706a6ff2017-11-29 22:01:06 -0700184#endif
Chris Daltonc4b47352019-08-23 10:10:36 -0600185 onFlushRenderTask->prepare(&flushState);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400186 }
187 }
188
Adlai Holler141b38b2021-05-05 15:16:40 +0000189 bool usingReorderedDAG = false;
190 GrResourceAllocator resourceAllocator(dContext);
191 if (fReduceOpsTaskSplitting) {
192 usingReorderedDAG = this->reorderTasks(&resourceAllocator);
193 if (!usingReorderedDAG) {
194 resourceAllocator.reset();
195 }
196 }
197
robertphillipsa13e2022015-11-11 12:01:09 -0800198#if 0
Brian Salomon09d994e2016-12-21 11:14:46 -0500199 // Enable this to print out verbose GrOp information
Adlai Hollerabdfd392020-10-19 09:00:36 -0400200 SkDEBUGCODE(SkDebugf("onFlush renderTasks (%d):\n", fOnFlushRenderTasks.count()));
Chris Daltonc4b47352019-08-23 10:10:36 -0600201 for (const auto& onFlushRenderTask : fOnFlushRenderTasks) {
Adlai Hollerabdfd392020-10-19 09:00:36 -0400202 SkDEBUGCODE(onFlushRenderTask->dump(/* printDependencies */ true);)
Chris Daltonc4b47352019-08-23 10:10:36 -0600203 }
Adlai Holler33432272020-11-11 13:54:37 -0500204 SkDEBUGCODE(SkDebugf("Normal renderTasks (%d):\n", fDAG.count()));
205 for (const auto& task : fDAG) {
206 SkDEBUGCODE(task->dump(/* printDependencies */ true);)
robertphillips3dc6ae52015-10-20 09:54:32 -0700207 }
robertphillipsa13e2022015-11-11 12:01:09 -0800208#endif
209
Adlai Holler387309c2021-04-15 15:03:27 -0400210 if (!resourceAllocator.failedInstantiation()) {
211 if (!usingReorderedDAG) {
212 for (const auto& task : fDAG) {
213 SkASSERT(task);
214 task->gatherProxyIntervals(&resourceAllocator);
Adlai Hollerfe919742021-04-10 07:42:15 -0400215 }
Adlai Holler387309c2021-04-15 15:03:27 -0400216 resourceAllocator.planAssignment();
Adlai Hollerfe919742021-04-10 07:42:15 -0400217 }
Adlai Holler387309c2021-04-15 15:03:27 -0400218 resourceAllocator.assign();
Greg Danielc42b20b2017-10-04 10:34:49 -0400219 }
Adlai Holler387309c2021-04-15 15:03:27 -0400220 bool flushed = !resourceAllocator.failedInstantiation() &&
221 this->executeRenderTasks(&flushState);
Adlai Holler19fd5142021-03-08 10:19:30 -0700222 this->removeRenderTasks();
robertphillipsa13e2022015-11-11 12:01:09 -0800223
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500224 gpu->executeFlushInfo(proxies, access, info, newState);
robertphillipsa13e2022015-11-11 12:01:09 -0800225
Brian Salomon57d2beab2018-09-10 09:35:41 -0400226 // Give the cache a chance to purge resources that become purgeable due to flushing.
227 if (flushed) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500228 resourceCache->purgeAsNeeded();
Brian Salomon876a0172019-03-08 11:12:14 -0500229 flushed = false;
bsalomonb77a9072016-09-07 10:02:04 -0700230 }
Chris Daltonfe199b72017-05-05 11:26:15 -0400231 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
Brian Osmanae87bf12021-05-11 13:36:10 -0400232 onFlushCBObject->postFlush(fTokenTracker.nextTokenToFlush(),
233 SkMakeSpan(fFlushingRenderTaskIDs));
Brian Salomon876a0172019-03-08 11:12:14 -0500234 flushed = true;
235 }
236 if (flushed) {
237 resourceCache->purgeAsNeeded();
Chris Daltonfe199b72017-05-05 11:26:15 -0400238 }
Chris Dalton6b498102019-08-01 14:14:52 -0600239 fFlushingRenderTaskIDs.reset();
joshualittb8918c42015-12-18 09:59:46 -0800240 fFlushing = false;
Greg Daniel51316782017-08-02 15:10:09 +0000241
Greg Danielfe159622020-04-10 17:43:51 +0000242 return true;
243}
244
245bool GrDrawingManager::submitToGpu(bool syncToCpu) {
246 if (fFlushing || this->wasAbandoned()) {
247 return false;
248 }
249
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400250 auto direct = fContext->asDirectContext();
Greg Danielfe159622020-04-10 17:43:51 +0000251 if (!direct) {
252 return false; // Can't submit while DDL recording
253 }
254 GrGpu* gpu = direct->priv().getGpu();
255 return gpu->submitToGpu(syncToCpu);
robertphillips3dc6ae52015-10-20 09:54:32 -0700256}
257
Adlai Hollerc616e1c2021-02-11 15:18:17 -0500258bool GrDrawingManager::executeRenderTasks(GrOpFlushState* flushState) {
Robert Phillips27483912018-04-20 12:43:18 -0400259#if GR_FLUSH_TIME_OP_SPEW
Adlai Hollerc616e1c2021-02-11 15:18:17 -0500260 SkDebugf("Flushing %d opsTasks\n", fDAG.count());
261 for (int i = 0; i < fDAG.count(); ++i) {
Adlai Holler33432272020-11-11 13:54:37 -0500262 if (fDAG[i]) {
Robert Phillips047d5bb2021-01-08 13:39:19 -0500263 SkString label;
264 label.printf("task %d/%d", i, fDAG.count());
265 fDAG[i]->dump(label, {}, true, true);
Robert Phillips1734dd32018-08-21 13:52:09 -0400266 }
Robert Phillips27483912018-04-20 12:43:18 -0400267 }
268#endif
269
Chris Dalton6b498102019-08-01 14:14:52 -0600270 bool anyRenderTasksExecuted = false;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500271
Adlai Hollerc616e1c2021-02-11 15:18:17 -0500272 for (const auto& renderTask : fDAG) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400273 if (!renderTask || !renderTask->isInstantiated()) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500274 continue;
275 }
276
Adlai Holler9e2c50e2021-02-09 14:41:52 -0500277 SkASSERT(renderTask->deferredProxiesAreInstantiated());
278
Chris Dalton6b498102019-08-01 14:14:52 -0600279 renderTask->prepare(flushState);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500280 }
281
282 // Upload all data to the GPU
283 flushState->preExecuteDraws();
284
Greg Danield2073452018-12-07 11:20:33 -0500285 // For Vulkan, if we have too many oplists to be flushed we end up allocating a lot of resources
286 // for each command buffer associated with the oplists. If this gets too large we can cause the
287 // devices to go OOM. In practice we usually only hit this case in our tests, but to be safe we
288 // put a cap on the number of oplists we will execute before flushing to the GPU to relieve some
289 // memory pressure.
Chris Dalton6b498102019-08-01 14:14:52 -0600290 static constexpr int kMaxRenderTasksBeforeFlush = 100;
Adlai Hollerc616e1c2021-02-11 15:18:17 -0500291 int numRenderTasksExecuted = 0;
Greg Danield2073452018-12-07 11:20:33 -0500292
Chris Daltonc4b47352019-08-23 10:10:36 -0600293 // Execute the onFlush renderTasks first, if any.
294 for (sk_sp<GrRenderTask>& onFlushRenderTask : fOnFlushRenderTasks) {
295 if (!onFlushRenderTask->execute(flushState)) {
296 SkDebugf("WARNING: onFlushRenderTask failed to execute.\n");
Robert Phillipseafd48a2017-11-16 07:52:08 -0500297 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600298 SkASSERT(onFlushRenderTask->unique());
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400299 onFlushRenderTask->disown(this);
Chris Daltonc4b47352019-08-23 10:10:36 -0600300 onFlushRenderTask = nullptr;
Adlai Hollerc616e1c2021-02-11 15:18:17 -0500301 if (++numRenderTasksExecuted >= kMaxRenderTasksBeforeFlush) {
Greg Danielfe159622020-04-10 17:43:51 +0000302 flushState->gpu()->submitToGpu(false);
Adlai Hollerc616e1c2021-02-11 15:18:17 -0500303 numRenderTasksExecuted = 0;
Greg Danield2073452018-12-07 11:20:33 -0500304 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500305 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600306 fOnFlushRenderTasks.reset();
Robert Phillipseafd48a2017-11-16 07:52:08 -0500307
308 // Execute the normal op lists.
Adlai Hollerc616e1c2021-02-11 15:18:17 -0500309 for (const auto& renderTask : fDAG) {
310 SkASSERT(renderTask);
311 if (!renderTask->isInstantiated()) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500312 continue;
313 }
314
Greg Daniel15ecdf92019-08-30 15:35:23 -0400315 if (renderTask->execute(flushState)) {
Chris Dalton6b498102019-08-01 14:14:52 -0600316 anyRenderTasksExecuted = true;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500317 }
Adlai Hollerc616e1c2021-02-11 15:18:17 -0500318 if (++numRenderTasksExecuted >= kMaxRenderTasksBeforeFlush) {
Greg Danielfe159622020-04-10 17:43:51 +0000319 flushState->gpu()->submitToGpu(false);
Adlai Hollerc616e1c2021-02-11 15:18:17 -0500320 numRenderTasksExecuted = 0;
Greg Danield2073452018-12-07 11:20:33 -0500321 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500322 }
323
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400324 SkASSERT(!flushState->opsRenderPass());
Robert Phillips40a29d72018-01-18 12:59:22 -0500325 SkASSERT(fTokenTracker.nextDrawToken() == fTokenTracker.nextTokenToFlush());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500326
Chris Dalton6b498102019-08-01 14:14:52 -0600327 // We reset the flush state before the RenderTasks so that the last resources to be freed are
328 // those that are written to in the RenderTasks. This helps to make sure the most recently used
329 // resources are the last to be purged by the resource cache.
Robert Phillipseafd48a2017-11-16 07:52:08 -0500330 flushState->reset();
331
Chris Dalton6b498102019-08-01 14:14:52 -0600332 return anyRenderTasksExecuted;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500333}
334
Adlai Hollerc616e1c2021-02-11 15:18:17 -0500335void GrDrawingManager::removeRenderTasks() {
336 for (const auto& task : fDAG) {
337 SkASSERT(task);
Robert Phillips07f675d2020-11-16 13:44:01 -0500338 if (!task->unique() || task->requiresExplicitCleanup()) {
339 // TODO: Eventually uniqueness should be guaranteed: http://skbug.com/7111.
340 // DDLs, however, will always require an explicit notification for when they
341 // can clean up resources.
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400342 task->endFlush(this);
343 }
344 task->disown(this);
345 }
Adlai Hollerc616e1c2021-02-11 15:18:17 -0500346 fDAG.reset();
347 fLastRenderTasks.reset();
Chris Dalton39811682021-03-11 22:45:48 -0700348 for (const sk_sp<GrRenderTask>& onFlushRenderTask : fOnFlushRenderTasks) {
349 onFlushRenderTask->disown(this);
350 }
351 fOnFlushRenderTasks.reset();
Adlai Holler33432272020-11-11 13:54:37 -0500352}
353
354void GrDrawingManager::sortTasks() {
Robert Phillipsfbbc3bb2020-11-16 14:58:56 -0500355 if (!GrTTopoSort<GrRenderTask, GrRenderTask::TopoSortTraits>(&fDAG)) {
Adlai Holler33432272020-11-11 13:54:37 -0500356 SkDEBUGFAIL("Render task topo sort failed.");
357 return;
358 }
359
360#ifdef SK_DEBUG
361 // This block checks for any unnecessary splits in the opsTasks. If two sequential opsTasks
362 // share the same backing GrSurfaceProxy it means the opsTask was artificially split.
363 if (!fDAG.empty()) {
364 GrOpsTask* prevOpsTask = fDAG[0]->asOpsTask();
365 for (int i = 1; i < fDAG.count(); ++i) {
366 GrOpsTask* curOpsTask = fDAG[i]->asOpsTask();
367
368 if (prevOpsTask && curOpsTask) {
Brian Salomon982127b2021-01-21 10:43:35 -0500369 SkASSERT(prevOpsTask->target(0) != curOpsTask->target(0));
Adlai Holler33432272020-11-11 13:54:37 -0500370 }
371
372 prevOpsTask = curOpsTask;
373 }
374 }
375#endif
376}
377
Adlai Holler78036082021-01-07 11:41:33 -0500378// Reorder the array to match the llist without reffing & unreffing sk_sp's.
Adlai Holler69c57e42021-01-25 15:44:26 +0000379// Both args must contain the same objects.
Adlai Holler78036082021-01-07 11:41:33 -0500380// This is basically a shim because clustering uses LList but the rest of drawmgr uses array.
381template <typename T>
382static void reorder_array_by_llist(const SkTInternalLList<T>& llist, SkTArray<sk_sp<T>>* array) {
383 int i = 0;
384 for (T* t : llist) {
Adlai Holler69c57e42021-01-25 15:44:26 +0000385 // Release the pointer that used to live here so it doesn't get unreffed.
386 [[maybe_unused]] T* old = array->at(i).release();
Adlai Holler78036082021-01-07 11:41:33 -0500387 array->at(i++).reset(t);
388 }
Adlai Holler69c57e42021-01-25 15:44:26 +0000389 SkASSERT(i == array->count());
Adlai Holler78036082021-01-07 11:41:33 -0500390}
391
Adlai Holler387309c2021-04-15 15:03:27 -0400392bool GrDrawingManager::reorderTasks(GrResourceAllocator* resourceAllocator) {
Adlai Holler78036082021-01-07 11:41:33 -0500393 SkASSERT(fReduceOpsTaskSplitting);
394 SkTInternalLList<GrRenderTask> llist;
Brian Osmanae87bf12021-05-11 13:36:10 -0400395 bool clustered = GrClusterRenderTasks(SkMakeSpan(fDAG), &llist);
Adlai Holler78036082021-01-07 11:41:33 -0500396 if (!clustered) {
Adlai Holler387309c2021-04-15 15:03:27 -0400397 return false;
398 }
399
400 for (GrRenderTask* task : llist) {
401 task->gatherProxyIntervals(resourceAllocator);
402 }
403 if (!resourceAllocator->planAssignment()) {
404 return false;
405 }
406 if (!resourceAllocator->makeBudgetHeadroom()) {
407 auto dContext = fContext->asDirectContext();
408 SkASSERT(dContext);
409 dContext->priv().getGpu()->stats()->incNumReorderedDAGsOverBudget();
410 return false;
Adlai Holler78036082021-01-07 11:41:33 -0500411 }
Adlai Holler78036082021-01-07 11:41:33 -0500412 reorder_array_by_llist(llist, &fDAG);
Adlai Holler93439d92021-01-26 09:20:39 -0500413
414 int newCount = 0;
415 for (int i = 0; i < fDAG.count(); i++) {
416 sk_sp<GrRenderTask>& task = fDAG[i];
417 if (auto opsTask = task->asOpsTask()) {
418 size_t remaining = fDAG.size() - i - 1;
419 SkSpan<sk_sp<GrRenderTask>> nextTasks{fDAG.end() - remaining, remaining};
420 int removeCount = opsTask->mergeFrom(nextTasks);
421 for (const auto& removed : nextTasks.first(removeCount)) {
422 removed->disown(this);
423 }
424 i += removeCount;
425 }
426 fDAG[newCount++] = std::move(task);
427 }
428 fDAG.resize_back(newCount);
Adlai Holler387309c2021-04-15 15:03:27 -0400429 return true;
Adlai Holler78036082021-01-07 11:41:33 -0500430}
431
Adlai Holler33432272020-11-11 13:54:37 -0500432void GrDrawingManager::closeAllTasks() {
433 const GrCaps& caps = *fContext->priv().caps();
434 for (auto& task : fDAG) {
435 if (task) {
436 task->makeClosed(caps);
437 }
438 }
439}
440
441GrRenderTask* GrDrawingManager::insertTaskBeforeLast(sk_sp<GrRenderTask> task) {
442 SkASSERT(!fDAG.empty());
443 if (!task) {
444 return nullptr;
445 }
446 // Release 'fDAG.back()' and grab the raw pointer, in case the SkTArray grows
447 // and reallocates during emplace_back.
448 // TODO: Either use std::vector that can do this for us, or use SkSTArray to get the
449 // perf win.
450 fDAG.emplace_back(fDAG.back().release());
451 return (fDAG[fDAG.count() - 2] = std::move(task)).get();
452}
453
454GrRenderTask* GrDrawingManager::appendTask(sk_sp<GrRenderTask> task) {
455 if (!task) {
456 return nullptr;
457 }
458 return fDAG.push_back(std::move(task)).get();
459}
460
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400461static void resolve_and_mipmap(GrGpu* gpu, GrSurfaceProxy* proxy) {
462 if (!proxy->isInstantiated()) {
463 return;
464 }
465
466 // In the flushSurfaces case, we need to resolve MSAA immediately after flush. This is
467 // because clients expect the flushed surface's backing texture to be fully resolved
468 // upon return.
469 if (proxy->requiresManualMSAAResolve()) {
470 auto* rtProxy = proxy->asRenderTargetProxy();
471 SkASSERT(rtProxy);
472 if (rtProxy->isMSAADirty()) {
473 SkASSERT(rtProxy->peekRenderTarget());
Jim Van Verthbb61fe32020-07-07 16:39:04 -0400474 gpu->resolveRenderTarget(rtProxy->peekRenderTarget(), rtProxy->msaaDirtyRect());
475 gpu->submitToGpu(false);
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400476 rtProxy->markMSAAResolved();
477 }
478 }
479 // If, after a flush, any of the proxies of interest have dirty mipmaps, regenerate them in
480 // case their backend textures are being stolen.
481 // (This special case is exercised by the ReimportImageTextureWithMipLevels test.)
482 // FIXME: It may be more ideal to plumb down a "we're going to steal the backends" flag.
483 if (auto* textureProxy = proxy->asTextureProxy()) {
Brian Salomon8c82a872020-07-21 12:09:58 -0400484 if (textureProxy->mipmapsAreDirty()) {
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400485 SkASSERT(textureProxy->peekTexture());
486 gpu->regenerateMipMapLevels(textureProxy->peekTexture());
Brian Salomon8c82a872020-07-21 12:09:58 -0400487 textureProxy->markMipmapsClean();
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400488 }
489 }
490}
491
Greg Daniel9efe3862020-06-11 11:51:06 -0400492GrSemaphoresSubmitted GrDrawingManager::flushSurfaces(
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500493 SkSpan<GrSurfaceProxy*> proxies,
Greg Daniel9efe3862020-06-11 11:51:06 -0400494 SkSurface::BackendSurfaceAccess access,
495 const GrFlushInfo& info,
496 const GrBackendSurfaceMutableState* newState) {
bsalomon6a2b1942016-09-08 11:28:59 -0700497 if (this->wasAbandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -0400498 if (info.fSubmittedProc) {
499 info.fSubmittedProc(info.fSubmittedContext, false);
500 }
501 if (info.fFinishedProc) {
502 info.fFinishedProc(info.fFinishedContext);
503 }
Greg Daniel51316782017-08-02 15:10:09 +0000504 return GrSemaphoresSubmitted::kNo;
bsalomon6a2b1942016-09-08 11:28:59 -0700505 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400506 SkDEBUGCODE(this->validate());
bsalomon6a2b1942016-09-08 11:28:59 -0700507
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400508 auto direct = fContext->asDirectContext();
Adlai Holleraee25fd2020-11-06 12:34:56 -0500509 SkASSERT(direct);
Robert Phillips6a6de562019-02-15 15:19:15 -0500510 GrGpu* gpu = direct->priv().getGpu();
Greg Daniel55822f12020-05-26 11:26:45 -0400511 // We have a non abandoned and direct GrContext. It must have a GrGpu.
512 SkASSERT(gpu);
Robert Phillips874b5352018-03-16 08:48:24 -0400513
Robert Phillipsacc10fa2019-04-01 09:50:20 -0400514 // TODO: It is important to upgrade the drawingmanager to just flushing the
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400515 // portion of the DAG required by 'proxies' in order to restore some of the
Robert Phillipsacc10fa2019-04-01 09:50:20 -0400516 // semantics of this method.
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500517 bool didFlush = this->flush(proxies, access, info, newState);
518 for (GrSurfaceProxy* proxy : proxies) {
519 resolve_and_mipmap(gpu, proxy);
bsalomon6a2b1942016-09-08 11:28:59 -0700520 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400521
522 SkDEBUGCODE(this->validate());
Greg Danielfe159622020-04-10 17:43:51 +0000523
Greg Daniel04283f32020-05-20 13:16:00 -0400524 if (!didFlush || (!direct->priv().caps()->semaphoreSupport() && info.fNumSemaphores)) {
Greg Danielfe159622020-04-10 17:43:51 +0000525 return GrSemaphoresSubmitted::kNo;
526 }
527 return GrSemaphoresSubmitted::kYes;
bsalomon6a2b1942016-09-08 11:28:59 -0700528}
529
Chris Daltonfe199b72017-05-05 11:26:15 -0400530void GrDrawingManager::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
531 fOnFlushCBObjects.push_back(onFlushCBObject);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400532}
533
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500534#if GR_TEST_UTILS
535void GrDrawingManager::testingOnly_removeOnFlushCallbackObject(GrOnFlushCallbackObject* cb) {
536 int n = std::find(fOnFlushCBObjects.begin(), fOnFlushCBObjects.end(), cb) -
537 fOnFlushCBObjects.begin();
538 SkASSERT(n < fOnFlushCBObjects.count());
539 fOnFlushCBObjects.removeShuffle(n);
540}
541#endif
542
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400543void GrDrawingManager::setLastRenderTask(const GrSurfaceProxy* proxy, GrRenderTask* task) {
544#ifdef SK_DEBUG
Robert Phillips07f675d2020-11-16 13:44:01 -0500545 if (auto prior = this->getLastRenderTask(proxy)) {
546 SkASSERT(prior->isClosed() || prior == task);
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400547 }
548#endif
Adlai Holler25df1f72020-06-09 13:08:27 -0400549 uint32_t key = proxy->uniqueID().asUInt();
550 if (task) {
551 fLastRenderTasks.set(key, task);
552 } else if (fLastRenderTasks.find(key)) {
553 fLastRenderTasks.remove(key);
554 }
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400555}
556
557GrRenderTask* GrDrawingManager::getLastRenderTask(const GrSurfaceProxy* proxy) const {
558 auto entry = fLastRenderTasks.find(proxy->uniqueID().asUInt());
559 return entry ? *entry : nullptr;
560}
561
562GrOpsTask* GrDrawingManager::getLastOpsTask(const GrSurfaceProxy* proxy) const {
563 GrRenderTask* task = this->getLastRenderTask(proxy);
564 return task ? task->asOpsTask() : nullptr;
565}
566
567
Chris Dalton6b498102019-08-01 14:14:52 -0600568void GrDrawingManager::moveRenderTasksToDDL(SkDeferredDisplayList* ddl) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400569 SkDEBUGCODE(this->validate());
570
Chris Dalton6b498102019-08-01 14:14:52 -0600571 // no renderTask should receive a new command after this
Adlai Holler33432272020-11-11 13:54:37 -0500572 this->closeAllTasks();
Adlai Holler039f90c2020-11-19 15:20:31 +0000573 fActiveOpsTask = nullptr;
Robert Phillips7a137052018-02-01 11:23:12 -0500574
Robert Phillips07f675d2020-11-16 13:44:01 -0500575 this->sortTasks();
576
Adlai Holler33432272020-11-11 13:54:37 -0500577 fDAG.swap(ddl->fRenderTasks);
578 SkASSERT(fDAG.empty());
Robert Phillips867ce8f2018-06-21 10:28:36 -0400579
Robert Phillips19f466d2020-02-26 10:27:07 -0500580 for (auto& renderTask : ddl->fRenderTasks) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400581 renderTask->disown(this);
Robert Phillips29f38542019-10-16 09:20:25 -0400582 renderTask->prePrepare(fContext);
Robert Phillips7327c9d2019-10-08 16:32:56 -0400583 }
584
Michael Ludwig2c316bd2019-12-19 14:50:44 -0500585 ddl->fArenas = std::move(fContext->priv().detachArenas());
Robert Phillips61fc7992019-10-22 11:58:17 -0400586
Robert Phillipsf6a0b452020-02-18 14:26:46 -0500587 fContext->priv().detachProgramData(&ddl->fProgramData);
Robert Phillips576b6a12019-12-06 13:05:49 -0500588
Robert Phillips774168e2018-05-31 12:43:27 -0400589 if (fPathRendererChain) {
590 if (auto ccpr = fPathRendererChain->getCoverageCountingPathRenderer()) {
591 ddl->fPendingPaths = ccpr->detachPendingPaths();
592 }
593 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400594
595 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500596}
597
Robert Phillips07f675d2020-11-16 13:44:01 -0500598void GrDrawingManager::createDDLTask(sk_sp<const SkDeferredDisplayList> ddl,
Robert Phillipseb54bb52021-01-08 17:20:18 -0500599 sk_sp<GrRenderTargetProxy> newDest,
Robert Phillips88b29612020-11-16 15:15:08 -0500600 SkIPoint offset) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400601 SkDEBUGCODE(this->validate());
602
Adlai Holler039f90c2020-11-19 15:20:31 +0000603 if (fActiveOpsTask) {
Adlai Hollerc44e21f2021-02-22 14:08:07 -0500604 // This is a temporary fix for the partial-MDB world. In that world we're not
Adlai Holler039f90c2020-11-19 15:20:31 +0000605 // reordering so ops that (in the single opsTask world) would've just glommed onto the
606 // end of the single opsTask but referred to a far earlier RT need to appear in their
607 // own opsTask.
608 fActiveOpsTask->makeClosed(*fContext->priv().caps());
609 fActiveOpsTask = nullptr;
Robert Phillips38d64b02018-09-04 13:23:26 -0400610 }
611
Robert Phillips07f675d2020-11-16 13:44:01 -0500612 // Propagate the DDL proxy's state information to the replay target.
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400613 if (ddl->priv().targetProxy()->isMSAADirty()) {
Brian Salomon982127b2021-01-21 10:43:35 -0500614 auto nativeRect = GrNativeRect::MakeIRectRelativeTo(
615 ddl->characterization().origin(),
616 ddl->priv().targetProxy()->backingStoreDimensions().height(),
617 ddl->priv().targetProxy()->msaaDirtyRect());
618 newDest->markMSAADirty(nativeRect);
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400619 }
620 GrTextureProxy* newTextureProxy = newDest->asTextureProxy();
Brian Salomon8c82a872020-07-21 12:09:58 -0400621 if (newTextureProxy && GrMipmapped::kYes == newTextureProxy->mipmapped()) {
622 newTextureProxy->markMipmapsDirty();
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400623 }
624
Robert Phillips62000362018-02-01 09:10:04 -0500625 // Here we jam the proxy that backs the current replay SkSurface into the LazyProxyData.
Robert Phillips07f675d2020-11-16 13:44:01 -0500626 // The lazy proxy that references it (in the DDL opsTasks) will then steal its GrTexture.
Robert Phillipseb54bb52021-01-08 17:20:18 -0500627 ddl->fLazyProxyData->fReplayDest = newDest.get();
Robert Phillips774168e2018-05-31 12:43:27 -0400628
629 if (ddl->fPendingPaths.size()) {
630 GrCoverageCountingPathRenderer* ccpr = this->getCoverageCountingPathRenderer();
631
632 ccpr->mergePendingPaths(ddl->fPendingPaths);
633 }
Robert Phillips22310d62018-09-05 11:07:21 -0400634
Robert Phillips07f675d2020-11-16 13:44:01 -0500635 // Add a task to handle drawing and lifetime management of the DDL.
636 SkDEBUGCODE(auto ddlTask =) this->appendTask(sk_make_sp<GrDDLTask>(this,
Robert Phillipseb54bb52021-01-08 17:20:18 -0500637 std::move(newDest),
Robert Phillips88b29612020-11-16 15:15:08 -0500638 std::move(ddl),
639 offset));
Robert Phillips07f675d2020-11-16 13:44:01 -0500640 SkASSERT(ddlTask->isClosed());
Adlai Holler7580ad42020-06-24 13:45:25 -0400641
Robert Phillips38d64b02018-09-04 13:23:26 -0400642 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500643}
644
Robert Phillips38d64b02018-09-04 13:23:26 -0400645#ifdef SK_DEBUG
646void GrDrawingManager::validate() const {
Adlai Holler4ef108e2020-12-30 16:21:13 +0000647 if (fActiveOpsTask) {
648 SkASSERT(!fDAG.empty());
649 SkASSERT(!fActiveOpsTask->isClosed());
650 SkASSERT(fActiveOpsTask == fDAG.back().get());
651 }
Adlai Holler039f90c2020-11-19 15:20:31 +0000652
Adlai Holler4ef108e2020-12-30 16:21:13 +0000653 for (int i = 0; i < fDAG.count(); ++i) {
654 if (fActiveOpsTask != fDAG[i].get()) {
655 // The resolveTask associated with the activeTask remains open for as long as the
656 // activeTask does.
657 bool isActiveResolveTask =
658 fActiveOpsTask && fActiveOpsTask->fTextureResolveTask == fDAG[i].get();
659 SkASSERT(isActiveResolveTask || fDAG[i]->isClosed());
Adlai Holler76893192020-12-22 19:41:19 +0000660 }
Adlai Holler4ef108e2020-12-30 16:21:13 +0000661 }
Adlai Holler76893192020-12-22 19:41:19 +0000662
Adlai Holler4ef108e2020-12-30 16:21:13 +0000663 if (!fDAG.empty() && !fDAG.back()->isClosed()) {
664 SkASSERT(fActiveOpsTask == fDAG.back().get());
Robert Phillips38d64b02018-09-04 13:23:26 -0400665 }
666}
667#endif
668
Adlai Holler4ef108e2020-12-30 16:21:13 +0000669void GrDrawingManager::closeActiveOpsTask() {
670 if (fActiveOpsTask) {
671 // This is a temporary fix for the partial-MDB world. In that world we're not
Adlai Holler039f90c2020-11-19 15:20:31 +0000672 // reordering so ops that (in the single opsTask world) would've just glommed onto the
Greg Danielf41b2bd2019-08-22 16:19:24 -0400673 // end of the single opsTask but referred to a far earlier RT need to appear in their
674 // own opsTask.
Adlai Holler039f90c2020-11-19 15:20:31 +0000675 fActiveOpsTask->makeClosed(*fContext->priv().caps());
676 fActiveOpsTask = nullptr;
robertphillips3dc6ae52015-10-20 09:54:32 -0700677 }
Chris Dalton5fe99772019-08-06 11:57:39 -0600678}
679
Greg Daniel16f5c652019-10-29 11:26:01 -0400680sk_sp<GrOpsTask> GrDrawingManager::newOpsTask(GrSurfaceProxyView surfaceView,
Herb Derby0b1228d2021-04-05 18:38:35 -0400681 sk_sp<GrArenas> arenas,
Robert Phillips19006652020-11-19 14:20:57 -0500682 bool flushTimeOpsTask) {
Chris Dalton5fe99772019-08-06 11:57:39 -0600683 SkDEBUGCODE(this->validate());
684 SkASSERT(fContext);
685
Adlai Holler4ef108e2020-12-30 16:21:13 +0000686 this->closeActiveOpsTask();
robertphillips3dc6ae52015-10-20 09:54:32 -0700687
Herb Derby0b1228d2021-04-05 18:38:35 -0400688 sk_sp<GrOpsTask> opsTask(new GrOpsTask(this,
689 std::move(surfaceView),
690 fContext->priv().auditTrail(),
691 std::move(arenas)));
Brian Salomon982127b2021-01-21 10:43:35 -0500692 SkASSERT(this->getLastRenderTask(opsTask->target(0)) == opsTask.get());
robertphillips3dc6ae52015-10-20 09:54:32 -0700693
Robert Phillips19006652020-11-19 14:20:57 -0500694 if (flushTimeOpsTask) {
695 fOnFlushRenderTasks.push_back(opsTask);
696 } else {
Adlai Holler33432272020-11-11 13:54:37 -0500697 this->appendTask(opsTask);
Adlai Holler039f90c2020-11-19 15:20:31 +0000698
Adlai Holler4ef108e2020-12-30 16:21:13 +0000699 fActiveOpsTask = opsTask.get();
Robert Phillips941d1442017-06-14 16:37:02 -0400700 }
robertphillips3dc6ae52015-10-20 09:54:32 -0700701
Robert Phillips38d64b02018-09-04 13:23:26 -0400702 SkDEBUGCODE(this->validate());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400703 return opsTask;
robertphillips3dc6ae52015-10-20 09:54:32 -0700704}
705
Adlai Holler039f90c2020-11-19 15:20:31 +0000706GrTextureResolveRenderTask* GrDrawingManager::newTextureResolveRenderTask(const GrCaps& caps) {
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600707 // 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 +0000708 // in sorting and opsTask reduction mode) the render tasks that depend on any proxy's current
Greg Danielf41b2bd2019-08-22 16:19:24 -0400709 // state. This is because those opsTasks can still receive new ops and because if they refer to
Adlai Holler039f90c2020-11-19 15:20:31 +0000710 // the mipmapped version of 'proxy', they will then come to depend on the render task being
Chris Dalton4ece96d2019-08-30 11:26:39 -0600711 // created here.
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600712 //
Adlai Holler039f90c2020-11-19 15:20:31 +0000713 // Add the new textureResolveTask before the fActiveOpsTask (if not in
714 // sorting/opsTask-splitting-reduction mode) because it will depend upon this resolve task.
Chris Dalton3d770272019-08-14 09:24:37 -0600715 // NOTE: Putting it here will also reduce the amount of work required by the topological sort.
Adlai Holler33432272020-11-11 13:54:37 -0500716 GrRenderTask* task = this->insertTaskBeforeLast(sk_make_sp<GrTextureResolveRenderTask>());
717 return static_cast<GrTextureResolveRenderTask*>(task);
Chris Dalton3d770272019-08-14 09:24:37 -0600718}
719
Greg Danielc30f1a92019-09-06 15:28:58 -0400720void GrDrawingManager::newWaitRenderTask(sk_sp<GrSurfaceProxy> proxy,
Greg Daniel301015c2019-11-18 14:06:46 -0500721 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> semaphores,
Greg Danielc30f1a92019-09-06 15:28:58 -0400722 int numSemaphores) {
723 SkDEBUGCODE(this->validate());
724 SkASSERT(fContext);
725
726 const GrCaps& caps = *fContext->priv().caps();
727
Greg Daniel16f5c652019-10-29 11:26:01 -0400728 sk_sp<GrWaitRenderTask> waitTask = sk_make_sp<GrWaitRenderTask>(GrSurfaceProxyView(proxy),
729 std::move(semaphores),
Greg Danielc30f1a92019-09-06 15:28:58 -0400730 numSemaphores);
Greg Danielc30f1a92019-09-06 15:28:58 -0400731
Brian Salomon982127b2021-01-21 10:43:35 -0500732 if (fActiveOpsTask && (fActiveOpsTask->target(0) == proxy.get())) {
Adlai Holler4ef108e2020-12-30 16:21:13 +0000733 SkASSERT(this->getLastRenderTask(proxy.get()) == fActiveOpsTask);
734 this->insertTaskBeforeLast(waitTask);
735 // In this case we keep the current renderTask open but just insert the new waitTask
736 // before it in the list. The waitTask will never need to trigger any resolves or mip
737 // map generation which is the main advantage of going through the proxy version.
738 // Additionally we would've had to temporarily set the wait task as the lastRenderTask
739 // on the proxy, add the dependency, and then reset the lastRenderTask to
740 // fActiveOpsTask. Additionally we make the waitTask depend on all of fActiveOpsTask
741 // dependencies so that we don't unnecessarily reorder the waitTask before them.
742 // Note: Any previous Ops already in fActiveOpsTask will get blocked by the wait
743 // semaphore even though they don't need to be for correctness.
744
745 // Make sure we add the dependencies of fActiveOpsTask to waitTask first or else we'll
746 // get a circular self dependency of waitTask on waitTask.
747 waitTask->addDependenciesFromOtherTask(fActiveOpsTask);
748 fActiveOpsTask->addDependency(waitTask.get());
Adlai Holler76893192020-12-22 19:41:19 +0000749 } else {
Adlai Holler4ef108e2020-12-30 16:21:13 +0000750 // In this case we just close the previous RenderTask and start and append the waitTask
751 // to the DAG. Since it is the last task now we call setLastRenderTask on the proxy. If
752 // there is a lastTask on the proxy we make waitTask depend on that task. This
753 // dependency isn't strictly needed but it does keep the DAG from reordering the
754 // waitTask earlier and blocking more tasks.
755 if (GrRenderTask* lastTask = this->getLastRenderTask(proxy.get())) {
756 waitTask->addDependency(lastTask);
Adlai Holler76893192020-12-22 19:41:19 +0000757 }
Adlai Holler4ef108e2020-12-30 16:21:13 +0000758 this->setLastRenderTask(proxy.get(), waitTask.get());
759 this->closeActiveOpsTask();
760 this->appendTask(waitTask);
Greg Danielc30f1a92019-09-06 15:28:58 -0400761 }
762 waitTask->makeClosed(caps);
763
764 SkDEBUGCODE(this->validate());
765}
766
Greg Danielbbfec9d2019-08-20 10:56:51 -0400767void GrDrawingManager::newTransferFromRenderTask(sk_sp<GrSurfaceProxy> srcProxy,
768 const SkIRect& srcRect,
769 GrColorType surfaceColorType,
770 GrColorType dstColorType,
771 sk_sp<GrGpuBuffer> dstBuffer,
772 size_t dstOffset) {
773 SkDEBUGCODE(this->validate());
774 SkASSERT(fContext);
Adlai Holler4ef108e2020-12-30 16:21:13 +0000775 this->closeActiveOpsTask();
Greg Danielbbfec9d2019-08-20 10:56:51 -0400776
Adlai Holler33432272020-11-11 13:54:37 -0500777 GrRenderTask* task = this->appendTask(sk_make_sp<GrTransferFromRenderTask>(
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400778 srcProxy, srcRect, surfaceColorType, dstColorType,
779 std::move(dstBuffer), dstOffset));
Greg Danielbbfec9d2019-08-20 10:56:51 -0400780
781 const GrCaps& caps = *fContext->priv().caps();
782
Brian Salomon7e67dca2020-07-21 09:27:25 -0400783 // We always say GrMipmapped::kNo here since we are always just copying from the base layer. We
Greg Danielbbfec9d2019-08-20 10:56:51 -0400784 // don't need to make sure the whole mip map chain is valid.
Brian Salomon7e67dca2020-07-21 09:27:25 -0400785 task->addDependency(this, srcProxy.get(), GrMipmapped::kNo,
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400786 GrTextureResolveManager(this), caps);
Greg Danielbbfec9d2019-08-20 10:56:51 -0400787 task->makeClosed(caps);
788
Greg Danielbbfec9d2019-08-20 10:56:51 -0400789 // We have closed the previous active oplist but since a new oplist isn't being added there
790 // shouldn't be an active one.
Adlai Holler039f90c2020-11-19 15:20:31 +0000791 SkASSERT(!fActiveOpsTask);
Greg Danielbbfec9d2019-08-20 10:56:51 -0400792 SkDEBUGCODE(this->validate());
793}
794
Brian Salomond63638b2021-03-05 14:00:07 -0500795sk_sp<GrRenderTask> GrDrawingManager::newCopyRenderTask(sk_sp<GrSurfaceProxy> src,
796 SkIRect srcRect,
797 sk_sp<GrSurfaceProxy> dst,
798 SkIPoint dstPoint,
799 GrSurfaceOrigin origin) {
Greg Daniele227fe42019-08-21 13:52:24 -0400800 SkDEBUGCODE(this->validate());
801 SkASSERT(fContext);
Greg Daniele227fe42019-08-21 13:52:24 -0400802
Brian Salomon56c78f42021-02-03 16:56:55 -0500803 // It'd be nicer to check this in GrCopyRenderTask::Make. This gets complicated because of
804 // "active ops task" tracking. dst will be the target of our copy task but it might also be the
805 // target of the active ops task. We currently require the active ops task to be closed before
806 // making a new task that targets the same proxy. However, if we first close the active ops
807 // task, then fail to make a copy task, the next active ops task may target the same proxy. This
808 // will trip an assert related to unnecessary ops task splitting.
809 if (src->framebufferOnly()) {
Brian Salomond63638b2021-03-05 14:00:07 -0500810 return nullptr;
Brian Salomon56c78f42021-02-03 16:56:55 -0500811 }
812
Brian Salomon507c5652021-02-03 20:32:06 +0000813 this->closeActiveOpsTask();
814
Brian Salomond63638b2021-03-05 14:00:07 -0500815 sk_sp<GrRenderTask> task = GrCopyRenderTask::Make(this,
816 src,
817 srcRect,
818 std::move(dst),
819 dstPoint,
820 origin);
Greg Daniele227fe42019-08-21 13:52:24 -0400821 if (!task) {
Brian Salomond63638b2021-03-05 14:00:07 -0500822 return nullptr;
Greg Daniele227fe42019-08-21 13:52:24 -0400823 }
824
Brian Salomond63638b2021-03-05 14:00:07 -0500825 this->appendTask(task);
826
Brian Salomon0f9f8002021-01-22 16:30:50 -0500827 const GrCaps& caps = *fContext->priv().caps();
Brian Salomon7e67dca2020-07-21 09:27:25 -0400828 // We always say GrMipmapped::kNo here since we are always just copying from the base layer to
Greg Daniele227fe42019-08-21 13:52:24 -0400829 // another base layer. We don't need to make sure the whole mip map chain is valid.
Brian Salomon507c5652021-02-03 20:32:06 +0000830 task->addDependency(this, src.get(), GrMipmapped::kNo, GrTextureResolveManager(this), caps);
831 task->makeClosed(caps);
Greg Daniele227fe42019-08-21 13:52:24 -0400832
Greg Daniele227fe42019-08-21 13:52:24 -0400833 // We have closed the previous active oplist but since a new oplist isn't being added there
834 // shouldn't be an active one.
Adlai Holler039f90c2020-11-19 15:20:31 +0000835 SkASSERT(!fActiveOpsTask);
Greg Daniele227fe42019-08-21 13:52:24 -0400836 SkDEBUGCODE(this->validate());
Brian Salomond63638b2021-03-05 14:00:07 -0500837 return task;
Greg Daniele227fe42019-08-21 13:52:24 -0400838}
839
Brian Salomonbe1084b2021-01-26 13:29:30 -0500840bool GrDrawingManager::newWritePixelsTask(sk_sp<GrSurfaceProxy> dst,
841 SkIRect rect,
842 GrColorType srcColorType,
843 GrColorType dstColorType,
844 const GrMipLevel levels[],
Brian Salomon75ee7372021-04-06 15:04:35 -0400845 int levelCount) {
Brian Salomonbe1084b2021-01-26 13:29:30 -0500846 SkDEBUGCODE(this->validate());
847 SkASSERT(fContext);
848
849 this->closeActiveOpsTask();
850 const GrCaps& caps = *fContext->priv().caps();
851
852 // On platforms that prefer flushes over VRAM use (i.e., ANGLE) we're better off forcing a
853 // complete flush here.
854 if (!caps.preferVRAMUseOverFlushes()) {
855 this->flushSurfaces(SkSpan<GrSurfaceProxy*>{},
856 SkSurface::BackendSurfaceAccess::kNoAccess,
857 GrFlushInfo{},
858 nullptr);
859 }
860
861 GrRenderTask* task = this->appendTask(GrWritePixelsTask::Make(this,
862 std::move(dst),
863 rect,
864 srcColorType,
865 dstColorType,
866 levels,
Brian Salomon75ee7372021-04-06 15:04:35 -0400867 levelCount));
Brian Salomonbe1084b2021-01-26 13:29:30 -0500868 if (!task) {
869 return false;
870 }
871
872 task->makeClosed(caps);
873
874 // We have closed the previous active oplist but since a new oplist isn't being added there
875 // shouldn't be an active one.
876 SkASSERT(!fActiveOpsTask);
877 SkDEBUGCODE(this->validate());
878 return true;
879}
880
robertphillips68737822015-10-29 12:12:21 -0700881/*
882 * This method finds a path renderer that can draw the specified path on
883 * the provided target.
884 * Due to its expense, the software path renderer has split out so it can
885 * can be individually allowed/disallowed via the "allowSW" boolean.
886 */
887GrPathRenderer* GrDrawingManager::getPathRenderer(const GrPathRenderer::CanDrawPathArgs& args,
888 bool allowSW,
889 GrPathRendererChain::DrawType drawType,
890 GrPathRenderer::StencilSupport* stencilSupport) {
891
892 if (!fPathRendererChain) {
John Stilesfbd050b2020-08-03 13:21:46 -0400893 fPathRendererChain =
894 std::make_unique<GrPathRendererChain>(fContext, fOptionsForPathRendererChain);
robertphillips68737822015-10-29 12:12:21 -0700895 }
896
897 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(args, drawType, stencilSupport);
898 if (!pr && allowSW) {
Brian Salomone7df0bb2018-05-07 14:44:57 -0400899 auto swPR = this->getSoftwarePathRenderer();
900 if (GrPathRenderer::CanDrawPath::kNo != swPR->canDrawPath(args)) {
901 pr = swPR;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500902 }
robertphillips68737822015-10-29 12:12:21 -0700903 }
904
Robert Phillipsd81379d2020-04-21 10:39:02 -0400905#if GR_PATH_RENDERER_SPEW
906 if (pr) {
907 SkDebugf("getPathRenderer: %s\n", pr->name());
908 }
909#endif
910
robertphillips68737822015-10-29 12:12:21 -0700911 return pr;
912}
913
Brian Salomone7df0bb2018-05-07 14:44:57 -0400914GrPathRenderer* GrDrawingManager::getSoftwarePathRenderer() {
915 if (!fSoftwarePathRenderer) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400916 fSoftwarePathRenderer.reset(
Robert Phillips9da87e02019-02-04 13:26:26 -0500917 new GrSoftwarePathRenderer(fContext->priv().proxyProvider(),
Ben Wagner9ec70c62018-07-12 13:30:47 -0400918 fOptionsForPathRendererChain.fAllowPathMaskCaching));
Brian Salomone7df0bb2018-05-07 14:44:57 -0400919 }
Ben Wagner9ec70c62018-07-12 13:30:47 -0400920 return fSoftwarePathRenderer.get();
Brian Salomone7df0bb2018-05-07 14:44:57 -0400921}
922
Chris Daltonfddb6c02017-11-04 15:22:22 -0600923GrCoverageCountingPathRenderer* GrDrawingManager::getCoverageCountingPathRenderer() {
924 if (!fPathRendererChain) {
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500925 fPathRendererChain = std::make_unique<GrPathRendererChain>(fContext,
926 fOptionsForPathRendererChain);
Chris Daltonfddb6c02017-11-04 15:22:22 -0600927 }
928 return fPathRendererChain->getCoverageCountingPathRenderer();
929}
930
Chris Daltone6ae4762021-02-05 14:56:21 -0700931GrPathRenderer* GrDrawingManager::getTessellationPathRenderer() {
932 if (!fPathRendererChain) {
933 fPathRendererChain = std::make_unique<GrPathRendererChain>(fContext,
934 fOptionsForPathRendererChain);
935 }
936 return fPathRendererChain->getTessellationPathRenderer();
937}
938
Brian Salomon653f42f2018-07-10 10:07:31 -0400939void GrDrawingManager::flushIfNecessary() {
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400940 auto direct = fContext->asDirectContext();
Robert Phillips6a6de562019-02-15 15:19:15 -0500941 if (!direct) {
942 return;
943 }
944
945 auto resourceCache = direct->priv().getResourceCache();
Brian Salomon653f42f2018-07-10 10:07:31 -0400946 if (resourceCache && resourceCache->requestsFlush()) {
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500947 if (this->flush({}, SkSurface::BackendSurfaceAccess::kNoAccess, GrFlushInfo(), nullptr)) {
Greg Danielfe159622020-04-10 17:43:51 +0000948 this->submitToGpu(false);
949 }
Brian Salomon57d2beab2018-09-10 09:35:41 -0400950 resourceCache->purgeAsNeeded();
Brian Salomon653f42f2018-07-10 10:07:31 -0400951 }
952}