blob: d5b73c9274261431fae233966ecdb84ad44b4c4b [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050045#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
Herb Derbybf2dd2a2021-03-04 10:13:22 -050046#include "src/gpu/text/GrSDFTControl.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050047#include "src/image/SkSurface_Gpu.h"
robertphillips498d7ac2015-10-30 10:11:30 -070048
Robert Phillips3674f582021-06-16 12:05:54 -040049#if GR_OGA
50#include "src/gpu/GrSoftwarePathRenderer.h"
51#endif
52
Robert Phillips22310d62018-09-05 11:07:21 -040053///////////////////////////////////////////////////////////////////////////////////////////////////
Robert Phillips3674f582021-06-16 12:05:54 -040054#if GR_OGA
55GrDrawingManager::GrDrawingManager(GrRecordingContext* rContext,
Robert Phillips22310d62018-09-05 11:07:21 -040056 const GrPathRendererChain::Options& optionsForPathRendererChain,
Greg Danielf41b2bd2019-08-22 16:19:24 -040057 bool reduceOpsTaskSplitting)
Robert Phillips3674f582021-06-16 12:05:54 -040058 : fContext(rContext)
Robert Phillips22310d62018-09-05 11:07:21 -040059 , fOptionsForPathRendererChain(optionsForPathRendererChain)
Robert Phillips22310d62018-09-05 11:07:21 -040060 , fPathRendererChain(nullptr)
61 , fSoftwarePathRenderer(nullptr)
Robert Phillips3674f582021-06-16 12:05:54 -040062 , fReduceOpsTaskSplitting(reduceOpsTaskSplitting) {
63}
64
65#else
66
67GrDrawingManager::GrDrawingManager(GrRecordingContext* rContext, bool reduceOpsTaskSplitting)
68 : fContext(rContext)
69 , fReduceOpsTaskSplitting(reduceOpsTaskSplitting) {
70}
71
72#endif
Robert Phillips22310d62018-09-05 11:07:21 -040073
robertphillips3dc6ae52015-10-20 09:54:32 -070074GrDrawingManager::~GrDrawingManager() {
Adlai Holler33432272020-11-11 13:54:37 -050075 this->closeAllTasks();
Adlai Hollerc616e1c2021-02-11 15:18:17 -050076 this->removeRenderTasks();
robertphillips3dc6ae52015-10-20 09:54:32 -070077}
78
Robert Phillipsa9162df2019-02-11 14:12:03 -050079bool GrDrawingManager::wasAbandoned() const {
Robert Phillips9eb00022020-06-30 15:30:12 -040080 return fContext->abandoned();
robertphillips3dc6ae52015-10-20 09:54:32 -070081}
82
robertphillips68737822015-10-29 12:12:21 -070083void GrDrawingManager::freeGpuResources() {
Jim Van Verth106b5c42017-09-26 12:45:29 -040084 for (int i = fOnFlushCBObjects.count() - 1; i >= 0; --i) {
85 if (!fOnFlushCBObjects[i]->retainOnFreeGpuResources()) {
86 // it's safe to just do this because we're iterating in reverse
87 fOnFlushCBObjects.removeShuffle(i);
88 }
89 }
90
Robert Phillips3674f582021-06-16 12:05:54 -040091#if GR_OGA
robertphillips68737822015-10-29 12:12:21 -070092 // a path renderer may be holding onto resources
robertphillips13391dd2015-10-30 05:15:11 -070093 fPathRendererChain = nullptr;
Ben Wagner9ec70c62018-07-12 13:30:47 -040094 fSoftwarePathRenderer = nullptr;
Robert Phillips3674f582021-06-16 12:05:54 -040095#endif
Robert Phillipse3302df2017-04-24 07:31:02 -040096}
97
Adlai Hollerc2bfcff2020-11-06 15:39:36 -050098// MDB TODO: make use of the 'proxies' parameter.
Greg Daniel9efe3862020-06-11 11:51:06 -040099bool GrDrawingManager::flush(
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500100 SkSpan<GrSurfaceProxy*> proxies,
Greg Daniel9efe3862020-06-11 11:51:06 -0400101 SkSurface::BackendSurfaceAccess access,
102 const GrFlushInfo& info,
103 const GrBackendSurfaceMutableState* newState) {
Brian Salomon57d2beab2018-09-10 09:35:41 -0400104 GR_CREATE_TRACE_MARKER_CONTEXT("GrDrawingManager", "flush", fContext);
Brian Salomondcbb9d92017-07-19 10:53:20 -0400105
robertphillips7761d612016-05-16 09:14:53 -0700106 if (fFlushing || this->wasAbandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -0400107 if (info.fSubmittedProc) {
108 info.fSubmittedProc(info.fSubmittedContext, false);
109 }
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400110 if (info.fFinishedProc) {
111 info.fFinishedProc(info.fFinishedContext);
Greg Daniela3aa75a2019-04-12 14:24:55 -0400112 }
Greg Danielfe159622020-04-10 17:43:51 +0000113 return false;
joshualittb8918c42015-12-18 09:59:46 -0800114 }
Robert Phillips602df412019-04-08 11:10:39 -0400115
Robert Phillips38d64b02018-09-04 13:23:26 -0400116 SkDEBUGCODE(this->validate());
117
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500118 // As of now we only short-circuit if we got an explicit list of surfaces to flush.
119 if (!proxies.empty() && !info.fNumSemaphores && !info.fFinishedProc &&
Greg Danielce9f0162020-06-30 13:42:46 -0400120 access == SkSurface::BackendSurfaceAccess::kNoAccess && !newState) {
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500121 bool allUnused = std::all_of(proxies.begin(), proxies.end(), [&](GrSurfaceProxy* proxy) {
Adlai Holler33432272020-11-11 13:54:37 -0500122 bool used = std::any_of(fDAG.begin(), fDAG.end(), [&](auto& task) {
123 return task && task->isUsed(proxy);
124 });
Robert Phillips58a8ccc2021-01-11 09:07:55 -0500125 return !used;
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500126 });
127 if (allUnused) {
Greg Daniel55822f12020-05-26 11:26:45 -0400128 if (info.fSubmittedProc) {
129 info.fSubmittedProc(info.fSubmittedContext, true);
130 }
Greg Danielfe159622020-04-10 17:43:51 +0000131 return false;
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400132 }
Robert Phillips9313aa72019-04-09 18:41:27 -0400133 }
134
Adlai Hollerca1137b2021-04-08 11:39:55 -0400135 auto dContext = fContext->asDirectContext();
136 SkASSERT(dContext);
137 dContext->priv().clientMappedBufferManager()->process();
Robert Phillips6a6de562019-02-15 15:19:15 -0500138
Adlai Hollerca1137b2021-04-08 11:39:55 -0400139 GrGpu* gpu = dContext->priv().getGpu();
Greg Daniel55822f12020-05-26 11:26:45 -0400140 // We have a non abandoned and direct GrContext. It must have a GrGpu.
141 SkASSERT(gpu);
Greg Daniela3aa75a2019-04-12 14:24:55 -0400142
joshualittb8918c42015-12-18 09:59:46 -0800143 fFlushing = true;
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400144
Adlai Hollerca1137b2021-04-08 11:39:55 -0400145 auto resourceProvider = dContext->priv().resourceProvider();
146 auto resourceCache = dContext->priv().getResourceCache();
Robert Phillips6a6de562019-02-15 15:19:15 -0500147
Chris Dalton6b498102019-08-01 14:14:52 -0600148 // Semi-usually the GrRenderTasks are already closed at this point, but sometimes Ganesh needs
Greg Danielf41b2bd2019-08-22 16:19:24 -0400149 // to flush mid-draw. In that case, the SkGpuDevice's opsTasks won't be closed but need to be
150 // flushed anyway. Closing such opsTasks here will mean new ones will be created to replace them
Chris Dalton6b498102019-08-01 14:14:52 -0600151 // if the SkGpuDevice(s) write to them again.
Adlai Holler33432272020-11-11 13:54:37 -0500152 this->closeAllTasks();
Adlai Holler039f90c2020-11-19 15:20:31 +0000153 fActiveOpsTask = nullptr;
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400154
Adlai Holler33432272020-11-11 13:54:37 -0500155 this->sortTasks();
Adlai Holler78036082021-01-07 11:41:33 -0500156
Brian Salomon601ac802019-02-07 13:37:16 -0500157 if (!fCpuBufferCache) {
158 // We cache more buffers when the backend is using client side arrays. Otherwise, we
159 // expect each pool will use a CPU buffer as a staging buffer before uploading to a GPU
160 // buffer object. Each pool only requires one staging buffer at a time.
161 int maxCachedBuffers = fContext->priv().caps()->preferClientSideDynamicBuffers() ? 2 : 6;
162 fCpuBufferCache = GrBufferAllocPool::CpuBufferCache::Make(maxCachedBuffers);
Brian Salomon58f153c2018-10-18 21:51:15 -0400163 }
Robert Phillipsa4c93ac2017-05-18 11:40:04 -0400164
Robert Phillipse5f73282019-06-18 17:15:04 -0400165 GrOpFlushState flushState(gpu, resourceProvider, &fTokenTracker, fCpuBufferCache);
Robert Phillips40a29d72018-01-18 12:59:22 -0500166
Chris Daltonfe199b72017-05-05 11:26:15 -0400167 GrOnFlushResourceProvider onFlushProvider(this);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400168
Chris Dalton12658942017-10-05 19:45:25 -0600169 // Prepare any onFlush op lists (e.g. atlases).
Chris Daltonfe199b72017-05-05 11:26:15 -0400170 if (!fOnFlushCBObjects.empty()) {
Robert Phillips07f675d2020-11-16 13:44:01 -0500171 fFlushingRenderTaskIDs.reserve_back(fDAG.count());
172 for (const auto& task : fDAG) {
173 if (task) {
174 task->gatherIDs(&fFlushingRenderTaskIDs);
Adlai Holler33432272020-11-11 13:54:37 -0500175 }
176 }
Robert Phillips22310d62018-09-05 11:07:21 -0400177
Chris Daltonfe199b72017-05-05 11:26:15 -0400178 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
Brian Osmanae87bf12021-05-11 13:36:10 -0400179 onFlushCBObject->preFlush(&onFlushProvider, SkMakeSpan(fFlushingRenderTaskIDs));
Chris Daltonc4b47352019-08-23 10:10:36 -0600180 }
181 for (const auto& onFlushRenderTask : fOnFlushRenderTasks) {
182 onFlushRenderTask->makeClosed(*fContext->priv().caps());
Chris Dalton706a6ff2017-11-29 22:01:06 -0700183#ifdef SK_DEBUG
Chris Daltonc4b47352019-08-23 10:10:36 -0600184 // OnFlush callbacks are invoked during flush, and are therefore expected to handle
Adlai Holler9e2c50e2021-02-09 14:41:52 -0500185 // resource allocation & usage on their own. (No deferred or lazy proxies!)
Chris Daltonc4b47352019-08-23 10:10:36 -0600186 onFlushRenderTask->visitTargetAndSrcProxies_debugOnly(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400187 [](GrSurfaceProxy* p, GrMipmapped mipMapped) {
Adlai Holler9e2c50e2021-02-09 14:41:52 -0500188 SkASSERT(!p->asTextureProxy() || !p->asTextureProxy()->texPriv().isDeferred());
Brian Salomonbeb7f522019-08-30 16:19:42 -0400189 SkASSERT(!p->isLazy());
Chris Dalton4ece96d2019-08-30 11:26:39 -0600190 if (p->requiresManualMSAAResolve()) {
191 // The onFlush callback is responsible for ensuring MSAA gets resolved.
192 SkASSERT(p->asRenderTargetProxy() && !p->asRenderTargetProxy()->isMSAADirty());
193 }
Brian Salomon7e67dca2020-07-21 09:27:25 -0400194 if (GrMipmapped::kYes == mipMapped) {
Chris Daltonc4b47352019-08-23 10:10:36 -0600195 // The onFlush callback is responsible for regenerating mips if needed.
Brian Salomon8c82a872020-07-21 12:09:58 -0400196 SkASSERT(p->asTextureProxy() && !p->asTextureProxy()->mipmapsAreDirty());
Chris Daltonc4b47352019-08-23 10:10:36 -0600197 }
198 });
Chris Dalton706a6ff2017-11-29 22:01:06 -0700199#endif
Chris Daltonc4b47352019-08-23 10:10:36 -0600200 onFlushRenderTask->prepare(&flushState);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400201 }
202 }
203
Adlai Holler141b38b2021-05-05 15:16:40 +0000204 bool usingReorderedDAG = false;
205 GrResourceAllocator resourceAllocator(dContext);
206 if (fReduceOpsTaskSplitting) {
207 usingReorderedDAG = this->reorderTasks(&resourceAllocator);
208 if (!usingReorderedDAG) {
209 resourceAllocator.reset();
210 }
211 }
212
robertphillipsa13e2022015-11-11 12:01:09 -0800213#if 0
Brian Salomon09d994e2016-12-21 11:14:46 -0500214 // Enable this to print out verbose GrOp information
Adlai Hollerabdfd392020-10-19 09:00:36 -0400215 SkDEBUGCODE(SkDebugf("onFlush renderTasks (%d):\n", fOnFlushRenderTasks.count()));
Chris Daltonc4b47352019-08-23 10:10:36 -0600216 for (const auto& onFlushRenderTask : fOnFlushRenderTasks) {
Adlai Hollerabdfd392020-10-19 09:00:36 -0400217 SkDEBUGCODE(onFlushRenderTask->dump(/* printDependencies */ true);)
Chris Daltonc4b47352019-08-23 10:10:36 -0600218 }
Adlai Holler33432272020-11-11 13:54:37 -0500219 SkDEBUGCODE(SkDebugf("Normal renderTasks (%d):\n", fDAG.count()));
220 for (const auto& task : fDAG) {
221 SkDEBUGCODE(task->dump(/* printDependencies */ true);)
robertphillips3dc6ae52015-10-20 09:54:32 -0700222 }
robertphillipsa13e2022015-11-11 12:01:09 -0800223#endif
224
Adlai Holler387309c2021-04-15 15:03:27 -0400225 if (!resourceAllocator.failedInstantiation()) {
226 if (!usingReorderedDAG) {
227 for (const auto& task : fDAG) {
228 SkASSERT(task);
229 task->gatherProxyIntervals(&resourceAllocator);
Adlai Hollerfe919742021-04-10 07:42:15 -0400230 }
Adlai Holler387309c2021-04-15 15:03:27 -0400231 resourceAllocator.planAssignment();
Adlai Hollerfe919742021-04-10 07:42:15 -0400232 }
Adlai Holler387309c2021-04-15 15:03:27 -0400233 resourceAllocator.assign();
Greg Danielc42b20b2017-10-04 10:34:49 -0400234 }
Adlai Holler387309c2021-04-15 15:03:27 -0400235 bool flushed = !resourceAllocator.failedInstantiation() &&
236 this->executeRenderTasks(&flushState);
Adlai Holler19fd5142021-03-08 10:19:30 -0700237 this->removeRenderTasks();
robertphillipsa13e2022015-11-11 12:01:09 -0800238
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500239 gpu->executeFlushInfo(proxies, access, info, newState);
robertphillipsa13e2022015-11-11 12:01:09 -0800240
Brian Salomon57d2beab2018-09-10 09:35:41 -0400241 // Give the cache a chance to purge resources that become purgeable due to flushing.
242 if (flushed) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500243 resourceCache->purgeAsNeeded();
Brian Salomon876a0172019-03-08 11:12:14 -0500244 flushed = false;
bsalomonb77a9072016-09-07 10:02:04 -0700245 }
Chris Daltonfe199b72017-05-05 11:26:15 -0400246 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
Brian Osmanae87bf12021-05-11 13:36:10 -0400247 onFlushCBObject->postFlush(fTokenTracker.nextTokenToFlush(),
248 SkMakeSpan(fFlushingRenderTaskIDs));
Brian Salomon876a0172019-03-08 11:12:14 -0500249 flushed = true;
250 }
251 if (flushed) {
252 resourceCache->purgeAsNeeded();
Chris Daltonfe199b72017-05-05 11:26:15 -0400253 }
Chris Dalton6b498102019-08-01 14:14:52 -0600254 fFlushingRenderTaskIDs.reset();
joshualittb8918c42015-12-18 09:59:46 -0800255 fFlushing = false;
Greg Daniel51316782017-08-02 15:10:09 +0000256
Greg Danielfe159622020-04-10 17:43:51 +0000257 return true;
258}
259
260bool GrDrawingManager::submitToGpu(bool syncToCpu) {
261 if (fFlushing || this->wasAbandoned()) {
262 return false;
263 }
264
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400265 auto direct = fContext->asDirectContext();
Greg Danielfe159622020-04-10 17:43:51 +0000266 if (!direct) {
267 return false; // Can't submit while DDL recording
268 }
269 GrGpu* gpu = direct->priv().getGpu();
270 return gpu->submitToGpu(syncToCpu);
robertphillips3dc6ae52015-10-20 09:54:32 -0700271}
272
Adlai Hollerc616e1c2021-02-11 15:18:17 -0500273bool GrDrawingManager::executeRenderTasks(GrOpFlushState* flushState) {
Robert Phillips27483912018-04-20 12:43:18 -0400274#if GR_FLUSH_TIME_OP_SPEW
Adlai Hollerc616e1c2021-02-11 15:18:17 -0500275 SkDebugf("Flushing %d opsTasks\n", fDAG.count());
276 for (int i = 0; i < fDAG.count(); ++i) {
Adlai Holler33432272020-11-11 13:54:37 -0500277 if (fDAG[i]) {
Robert Phillips047d5bb2021-01-08 13:39:19 -0500278 SkString label;
279 label.printf("task %d/%d", i, fDAG.count());
280 fDAG[i]->dump(label, {}, true, true);
Robert Phillips1734dd32018-08-21 13:52:09 -0400281 }
Robert Phillips27483912018-04-20 12:43:18 -0400282 }
283#endif
284
Chris Dalton6b498102019-08-01 14:14:52 -0600285 bool anyRenderTasksExecuted = false;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500286
Adlai Hollerc616e1c2021-02-11 15:18:17 -0500287 for (const auto& renderTask : fDAG) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400288 if (!renderTask || !renderTask->isInstantiated()) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500289 continue;
290 }
291
Adlai Holler9e2c50e2021-02-09 14:41:52 -0500292 SkASSERT(renderTask->deferredProxiesAreInstantiated());
293
Chris Dalton6b498102019-08-01 14:14:52 -0600294 renderTask->prepare(flushState);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500295 }
296
297 // Upload all data to the GPU
298 flushState->preExecuteDraws();
299
Greg Danield2073452018-12-07 11:20:33 -0500300 // For Vulkan, if we have too many oplists to be flushed we end up allocating a lot of resources
301 // for each command buffer associated with the oplists. If this gets too large we can cause the
302 // devices to go OOM. In practice we usually only hit this case in our tests, but to be safe we
303 // put a cap on the number of oplists we will execute before flushing to the GPU to relieve some
304 // memory pressure.
Chris Dalton6b498102019-08-01 14:14:52 -0600305 static constexpr int kMaxRenderTasksBeforeFlush = 100;
Adlai Hollerc616e1c2021-02-11 15:18:17 -0500306 int numRenderTasksExecuted = 0;
Greg Danield2073452018-12-07 11:20:33 -0500307
Chris Daltonc4b47352019-08-23 10:10:36 -0600308 // Execute the onFlush renderTasks first, if any.
309 for (sk_sp<GrRenderTask>& onFlushRenderTask : fOnFlushRenderTasks) {
310 if (!onFlushRenderTask->execute(flushState)) {
311 SkDebugf("WARNING: onFlushRenderTask failed to execute.\n");
Robert Phillipseafd48a2017-11-16 07:52:08 -0500312 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600313 SkASSERT(onFlushRenderTask->unique());
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400314 onFlushRenderTask->disown(this);
Chris Daltonc4b47352019-08-23 10:10:36 -0600315 onFlushRenderTask = nullptr;
Adlai Hollerc616e1c2021-02-11 15:18:17 -0500316 if (++numRenderTasksExecuted >= kMaxRenderTasksBeforeFlush) {
Greg Danielfe159622020-04-10 17:43:51 +0000317 flushState->gpu()->submitToGpu(false);
Adlai Hollerc616e1c2021-02-11 15:18:17 -0500318 numRenderTasksExecuted = 0;
Greg Danield2073452018-12-07 11:20:33 -0500319 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500320 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600321 fOnFlushRenderTasks.reset();
Robert Phillipseafd48a2017-11-16 07:52:08 -0500322
323 // Execute the normal op lists.
Adlai Hollerc616e1c2021-02-11 15:18:17 -0500324 for (const auto& renderTask : fDAG) {
325 SkASSERT(renderTask);
326 if (!renderTask->isInstantiated()) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500327 continue;
328 }
329
Greg Daniel15ecdf92019-08-30 15:35:23 -0400330 if (renderTask->execute(flushState)) {
Chris Dalton6b498102019-08-01 14:14:52 -0600331 anyRenderTasksExecuted = true;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500332 }
Adlai Hollerc616e1c2021-02-11 15:18:17 -0500333 if (++numRenderTasksExecuted >= kMaxRenderTasksBeforeFlush) {
Greg Danielfe159622020-04-10 17:43:51 +0000334 flushState->gpu()->submitToGpu(false);
Adlai Hollerc616e1c2021-02-11 15:18:17 -0500335 numRenderTasksExecuted = 0;
Greg Danield2073452018-12-07 11:20:33 -0500336 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500337 }
338
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400339 SkASSERT(!flushState->opsRenderPass());
Robert Phillips40a29d72018-01-18 12:59:22 -0500340 SkASSERT(fTokenTracker.nextDrawToken() == fTokenTracker.nextTokenToFlush());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500341
Chris Dalton6b498102019-08-01 14:14:52 -0600342 // We reset the flush state before the RenderTasks so that the last resources to be freed are
343 // those that are written to in the RenderTasks. This helps to make sure the most recently used
344 // resources are the last to be purged by the resource cache.
Robert Phillipseafd48a2017-11-16 07:52:08 -0500345 flushState->reset();
346
Chris Dalton6b498102019-08-01 14:14:52 -0600347 return anyRenderTasksExecuted;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500348}
349
Adlai Hollerc616e1c2021-02-11 15:18:17 -0500350void GrDrawingManager::removeRenderTasks() {
351 for (const auto& task : fDAG) {
352 SkASSERT(task);
Robert Phillips07f675d2020-11-16 13:44:01 -0500353 if (!task->unique() || task->requiresExplicitCleanup()) {
354 // TODO: Eventually uniqueness should be guaranteed: http://skbug.com/7111.
355 // DDLs, however, will always require an explicit notification for when they
356 // can clean up resources.
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400357 task->endFlush(this);
358 }
359 task->disown(this);
360 }
Adlai Hollerc616e1c2021-02-11 15:18:17 -0500361 fDAG.reset();
362 fLastRenderTasks.reset();
Chris Dalton39811682021-03-11 22:45:48 -0700363 for (const sk_sp<GrRenderTask>& onFlushRenderTask : fOnFlushRenderTasks) {
364 onFlushRenderTask->disown(this);
365 }
366 fOnFlushRenderTasks.reset();
Adlai Holler33432272020-11-11 13:54:37 -0500367}
368
369void GrDrawingManager::sortTasks() {
Robert Phillipsfbbc3bb2020-11-16 14:58:56 -0500370 if (!GrTTopoSort<GrRenderTask, GrRenderTask::TopoSortTraits>(&fDAG)) {
Adlai Holler33432272020-11-11 13:54:37 -0500371 SkDEBUGFAIL("Render task topo sort failed.");
372 return;
373 }
374
375#ifdef SK_DEBUG
376 // This block checks for any unnecessary splits in the opsTasks. If two sequential opsTasks
377 // share the same backing GrSurfaceProxy it means the opsTask was artificially split.
378 if (!fDAG.empty()) {
379 GrOpsTask* prevOpsTask = fDAG[0]->asOpsTask();
380 for (int i = 1; i < fDAG.count(); ++i) {
381 GrOpsTask* curOpsTask = fDAG[i]->asOpsTask();
382
383 if (prevOpsTask && curOpsTask) {
Brian Salomon982127b2021-01-21 10:43:35 -0500384 SkASSERT(prevOpsTask->target(0) != curOpsTask->target(0));
Adlai Holler33432272020-11-11 13:54:37 -0500385 }
386
387 prevOpsTask = curOpsTask;
388 }
389 }
390#endif
391}
392
Adlai Holler78036082021-01-07 11:41:33 -0500393// Reorder the array to match the llist without reffing & unreffing sk_sp's.
Adlai Holler69c57e42021-01-25 15:44:26 +0000394// Both args must contain the same objects.
Adlai Holler78036082021-01-07 11:41:33 -0500395// This is basically a shim because clustering uses LList but the rest of drawmgr uses array.
396template <typename T>
397static void reorder_array_by_llist(const SkTInternalLList<T>& llist, SkTArray<sk_sp<T>>* array) {
398 int i = 0;
399 for (T* t : llist) {
Adlai Holler69c57e42021-01-25 15:44:26 +0000400 // Release the pointer that used to live here so it doesn't get unreffed.
401 [[maybe_unused]] T* old = array->at(i).release();
Adlai Holler78036082021-01-07 11:41:33 -0500402 array->at(i++).reset(t);
403 }
Adlai Holler69c57e42021-01-25 15:44:26 +0000404 SkASSERT(i == array->count());
Adlai Holler78036082021-01-07 11:41:33 -0500405}
406
Adlai Holler387309c2021-04-15 15:03:27 -0400407bool GrDrawingManager::reorderTasks(GrResourceAllocator* resourceAllocator) {
Adlai Holler78036082021-01-07 11:41:33 -0500408 SkASSERT(fReduceOpsTaskSplitting);
409 SkTInternalLList<GrRenderTask> llist;
Brian Osmanae87bf12021-05-11 13:36:10 -0400410 bool clustered = GrClusterRenderTasks(SkMakeSpan(fDAG), &llist);
Adlai Holler78036082021-01-07 11:41:33 -0500411 if (!clustered) {
Adlai Holler387309c2021-04-15 15:03:27 -0400412 return false;
413 }
414
415 for (GrRenderTask* task : llist) {
416 task->gatherProxyIntervals(resourceAllocator);
417 }
418 if (!resourceAllocator->planAssignment()) {
419 return false;
420 }
421 if (!resourceAllocator->makeBudgetHeadroom()) {
422 auto dContext = fContext->asDirectContext();
423 SkASSERT(dContext);
424 dContext->priv().getGpu()->stats()->incNumReorderedDAGsOverBudget();
425 return false;
Adlai Holler78036082021-01-07 11:41:33 -0500426 }
Adlai Holler78036082021-01-07 11:41:33 -0500427 reorder_array_by_llist(llist, &fDAG);
Adlai Holler93439d92021-01-26 09:20:39 -0500428
429 int newCount = 0;
430 for (int i = 0; i < fDAG.count(); i++) {
431 sk_sp<GrRenderTask>& task = fDAG[i];
432 if (auto opsTask = task->asOpsTask()) {
433 size_t remaining = fDAG.size() - i - 1;
434 SkSpan<sk_sp<GrRenderTask>> nextTasks{fDAG.end() - remaining, remaining};
435 int removeCount = opsTask->mergeFrom(nextTasks);
436 for (const auto& removed : nextTasks.first(removeCount)) {
437 removed->disown(this);
438 }
439 i += removeCount;
440 }
441 fDAG[newCount++] = std::move(task);
442 }
443 fDAG.resize_back(newCount);
Adlai Holler387309c2021-04-15 15:03:27 -0400444 return true;
Adlai Holler78036082021-01-07 11:41:33 -0500445}
446
Adlai Holler33432272020-11-11 13:54:37 -0500447void GrDrawingManager::closeAllTasks() {
448 const GrCaps& caps = *fContext->priv().caps();
449 for (auto& task : fDAG) {
450 if (task) {
451 task->makeClosed(caps);
452 }
453 }
454}
455
456GrRenderTask* GrDrawingManager::insertTaskBeforeLast(sk_sp<GrRenderTask> task) {
457 SkASSERT(!fDAG.empty());
458 if (!task) {
459 return nullptr;
460 }
461 // Release 'fDAG.back()' and grab the raw pointer, in case the SkTArray grows
462 // and reallocates during emplace_back.
463 // TODO: Either use std::vector that can do this for us, or use SkSTArray to get the
464 // perf win.
465 fDAG.emplace_back(fDAG.back().release());
466 return (fDAG[fDAG.count() - 2] = std::move(task)).get();
467}
468
469GrRenderTask* GrDrawingManager::appendTask(sk_sp<GrRenderTask> task) {
470 if (!task) {
471 return nullptr;
472 }
473 return fDAG.push_back(std::move(task)).get();
474}
475
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400476static void resolve_and_mipmap(GrGpu* gpu, GrSurfaceProxy* proxy) {
477 if (!proxy->isInstantiated()) {
478 return;
479 }
480
481 // In the flushSurfaces case, we need to resolve MSAA immediately after flush. This is
482 // because clients expect the flushed surface's backing texture to be fully resolved
483 // upon return.
484 if (proxy->requiresManualMSAAResolve()) {
485 auto* rtProxy = proxy->asRenderTargetProxy();
486 SkASSERT(rtProxy);
487 if (rtProxy->isMSAADirty()) {
488 SkASSERT(rtProxy->peekRenderTarget());
Jim Van Verthbb61fe32020-07-07 16:39:04 -0400489 gpu->resolveRenderTarget(rtProxy->peekRenderTarget(), rtProxy->msaaDirtyRect());
490 gpu->submitToGpu(false);
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400491 rtProxy->markMSAAResolved();
492 }
493 }
494 // If, after a flush, any of the proxies of interest have dirty mipmaps, regenerate them in
495 // case their backend textures are being stolen.
496 // (This special case is exercised by the ReimportImageTextureWithMipLevels test.)
497 // FIXME: It may be more ideal to plumb down a "we're going to steal the backends" flag.
498 if (auto* textureProxy = proxy->asTextureProxy()) {
Brian Salomon8c82a872020-07-21 12:09:58 -0400499 if (textureProxy->mipmapsAreDirty()) {
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400500 SkASSERT(textureProxy->peekTexture());
501 gpu->regenerateMipMapLevels(textureProxy->peekTexture());
Brian Salomon8c82a872020-07-21 12:09:58 -0400502 textureProxy->markMipmapsClean();
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400503 }
504 }
505}
506
Greg Daniel9efe3862020-06-11 11:51:06 -0400507GrSemaphoresSubmitted GrDrawingManager::flushSurfaces(
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500508 SkSpan<GrSurfaceProxy*> proxies,
Greg Daniel9efe3862020-06-11 11:51:06 -0400509 SkSurface::BackendSurfaceAccess access,
510 const GrFlushInfo& info,
511 const GrBackendSurfaceMutableState* newState) {
bsalomon6a2b1942016-09-08 11:28:59 -0700512 if (this->wasAbandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -0400513 if (info.fSubmittedProc) {
514 info.fSubmittedProc(info.fSubmittedContext, false);
515 }
516 if (info.fFinishedProc) {
517 info.fFinishedProc(info.fFinishedContext);
518 }
Greg Daniel51316782017-08-02 15:10:09 +0000519 return GrSemaphoresSubmitted::kNo;
bsalomon6a2b1942016-09-08 11:28:59 -0700520 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400521 SkDEBUGCODE(this->validate());
bsalomon6a2b1942016-09-08 11:28:59 -0700522
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400523 auto direct = fContext->asDirectContext();
Adlai Holleraee25fd2020-11-06 12:34:56 -0500524 SkASSERT(direct);
Robert Phillips6a6de562019-02-15 15:19:15 -0500525 GrGpu* gpu = direct->priv().getGpu();
Greg Daniel55822f12020-05-26 11:26:45 -0400526 // We have a non abandoned and direct GrContext. It must have a GrGpu.
527 SkASSERT(gpu);
Robert Phillips874b5352018-03-16 08:48:24 -0400528
Robert Phillipsacc10fa2019-04-01 09:50:20 -0400529 // TODO: It is important to upgrade the drawingmanager to just flushing the
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400530 // portion of the DAG required by 'proxies' in order to restore some of the
Robert Phillipsacc10fa2019-04-01 09:50:20 -0400531 // semantics of this method.
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500532 bool didFlush = this->flush(proxies, access, info, newState);
533 for (GrSurfaceProxy* proxy : proxies) {
534 resolve_and_mipmap(gpu, proxy);
bsalomon6a2b1942016-09-08 11:28:59 -0700535 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400536
537 SkDEBUGCODE(this->validate());
Greg Danielfe159622020-04-10 17:43:51 +0000538
Greg Daniel04283f32020-05-20 13:16:00 -0400539 if (!didFlush || (!direct->priv().caps()->semaphoreSupport() && info.fNumSemaphores)) {
Greg Danielfe159622020-04-10 17:43:51 +0000540 return GrSemaphoresSubmitted::kNo;
541 }
542 return GrSemaphoresSubmitted::kYes;
bsalomon6a2b1942016-09-08 11:28:59 -0700543}
544
Chris Daltonfe199b72017-05-05 11:26:15 -0400545void GrDrawingManager::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
546 fOnFlushCBObjects.push_back(onFlushCBObject);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400547}
548
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500549#if GR_TEST_UTILS
550void GrDrawingManager::testingOnly_removeOnFlushCallbackObject(GrOnFlushCallbackObject* cb) {
551 int n = std::find(fOnFlushCBObjects.begin(), fOnFlushCBObjects.end(), cb) -
552 fOnFlushCBObjects.begin();
553 SkASSERT(n < fOnFlushCBObjects.count());
554 fOnFlushCBObjects.removeShuffle(n);
555}
556#endif
557
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400558void GrDrawingManager::setLastRenderTask(const GrSurfaceProxy* proxy, GrRenderTask* task) {
559#ifdef SK_DEBUG
Robert Phillips07f675d2020-11-16 13:44:01 -0500560 if (auto prior = this->getLastRenderTask(proxy)) {
561 SkASSERT(prior->isClosed() || prior == task);
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400562 }
563#endif
Adlai Holler25df1f72020-06-09 13:08:27 -0400564 uint32_t key = proxy->uniqueID().asUInt();
565 if (task) {
566 fLastRenderTasks.set(key, task);
567 } else if (fLastRenderTasks.find(key)) {
568 fLastRenderTasks.remove(key);
569 }
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400570}
571
572GrRenderTask* GrDrawingManager::getLastRenderTask(const GrSurfaceProxy* proxy) const {
573 auto entry = fLastRenderTasks.find(proxy->uniqueID().asUInt());
574 return entry ? *entry : nullptr;
575}
576
577GrOpsTask* GrDrawingManager::getLastOpsTask(const GrSurfaceProxy* proxy) const {
578 GrRenderTask* task = this->getLastRenderTask(proxy);
579 return task ? task->asOpsTask() : nullptr;
580}
581
582
Chris Dalton6b498102019-08-01 14:14:52 -0600583void GrDrawingManager::moveRenderTasksToDDL(SkDeferredDisplayList* ddl) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400584 SkDEBUGCODE(this->validate());
585
Chris Dalton6b498102019-08-01 14:14:52 -0600586 // no renderTask should receive a new command after this
Adlai Holler33432272020-11-11 13:54:37 -0500587 this->closeAllTasks();
Adlai Holler039f90c2020-11-19 15:20:31 +0000588 fActiveOpsTask = nullptr;
Robert Phillips7a137052018-02-01 11:23:12 -0500589
Robert Phillips07f675d2020-11-16 13:44:01 -0500590 this->sortTasks();
591
Adlai Holler33432272020-11-11 13:54:37 -0500592 fDAG.swap(ddl->fRenderTasks);
593 SkASSERT(fDAG.empty());
Robert Phillips867ce8f2018-06-21 10:28:36 -0400594
Robert Phillips19f466d2020-02-26 10:27:07 -0500595 for (auto& renderTask : ddl->fRenderTasks) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400596 renderTask->disown(this);
Robert Phillips29f38542019-10-16 09:20:25 -0400597 renderTask->prePrepare(fContext);
Robert Phillips7327c9d2019-10-08 16:32:56 -0400598 }
599
Michael Ludwig2c316bd2019-12-19 14:50:44 -0500600 ddl->fArenas = std::move(fContext->priv().detachArenas());
Robert Phillips61fc7992019-10-22 11:58:17 -0400601
Robert Phillipsf6a0b452020-02-18 14:26:46 -0500602 fContext->priv().detachProgramData(&ddl->fProgramData);
Robert Phillips576b6a12019-12-06 13:05:49 -0500603
Robert Phillips3674f582021-06-16 12:05:54 -0400604#if GR_OGA
Robert Phillips774168e2018-05-31 12:43:27 -0400605 if (fPathRendererChain) {
606 if (auto ccpr = fPathRendererChain->getCoverageCountingPathRenderer()) {
607 ddl->fPendingPaths = ccpr->detachPendingPaths();
608 }
609 }
Robert Phillips3674f582021-06-16 12:05:54 -0400610#endif
Robert Phillips38d64b02018-09-04 13:23:26 -0400611
612 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500613}
614
Robert Phillips07f675d2020-11-16 13:44:01 -0500615void GrDrawingManager::createDDLTask(sk_sp<const SkDeferredDisplayList> ddl,
Robert Phillipseb54bb52021-01-08 17:20:18 -0500616 sk_sp<GrRenderTargetProxy> newDest,
Robert Phillips88b29612020-11-16 15:15:08 -0500617 SkIPoint offset) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400618 SkDEBUGCODE(this->validate());
619
Adlai Holler039f90c2020-11-19 15:20:31 +0000620 if (fActiveOpsTask) {
Adlai Hollerc44e21f2021-02-22 14:08:07 -0500621 // This is a temporary fix for the partial-MDB world. In that world we're not
Adlai Holler039f90c2020-11-19 15:20:31 +0000622 // reordering so ops that (in the single opsTask world) would've just glommed onto the
623 // end of the single opsTask but referred to a far earlier RT need to appear in their
624 // own opsTask.
625 fActiveOpsTask->makeClosed(*fContext->priv().caps());
626 fActiveOpsTask = nullptr;
Robert Phillips38d64b02018-09-04 13:23:26 -0400627 }
628
Robert Phillips07f675d2020-11-16 13:44:01 -0500629 // Propagate the DDL proxy's state information to the replay target.
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400630 if (ddl->priv().targetProxy()->isMSAADirty()) {
Brian Salomon982127b2021-01-21 10:43:35 -0500631 auto nativeRect = GrNativeRect::MakeIRectRelativeTo(
632 ddl->characterization().origin(),
633 ddl->priv().targetProxy()->backingStoreDimensions().height(),
634 ddl->priv().targetProxy()->msaaDirtyRect());
635 newDest->markMSAADirty(nativeRect);
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400636 }
637 GrTextureProxy* newTextureProxy = newDest->asTextureProxy();
Brian Salomon8c82a872020-07-21 12:09:58 -0400638 if (newTextureProxy && GrMipmapped::kYes == newTextureProxy->mipmapped()) {
639 newTextureProxy->markMipmapsDirty();
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400640 }
641
Robert Phillips62000362018-02-01 09:10:04 -0500642 // Here we jam the proxy that backs the current replay SkSurface into the LazyProxyData.
Robert Phillips07f675d2020-11-16 13:44:01 -0500643 // The lazy proxy that references it (in the DDL opsTasks) will then steal its GrTexture.
Robert Phillipseb54bb52021-01-08 17:20:18 -0500644 ddl->fLazyProxyData->fReplayDest = newDest.get();
Robert Phillips774168e2018-05-31 12:43:27 -0400645
Robert Phillips3674f582021-06-16 12:05:54 -0400646#if GR_OGA
Robert Phillips774168e2018-05-31 12:43:27 -0400647 if (ddl->fPendingPaths.size()) {
648 GrCoverageCountingPathRenderer* ccpr = this->getCoverageCountingPathRenderer();
649
650 ccpr->mergePendingPaths(ddl->fPendingPaths);
651 }
Robert Phillips3674f582021-06-16 12:05:54 -0400652#endif
Robert Phillips22310d62018-09-05 11:07:21 -0400653
Robert Phillips07f675d2020-11-16 13:44:01 -0500654 // Add a task to handle drawing and lifetime management of the DDL.
655 SkDEBUGCODE(auto ddlTask =) this->appendTask(sk_make_sp<GrDDLTask>(this,
Robert Phillipseb54bb52021-01-08 17:20:18 -0500656 std::move(newDest),
Robert Phillips88b29612020-11-16 15:15:08 -0500657 std::move(ddl),
658 offset));
Robert Phillips07f675d2020-11-16 13:44:01 -0500659 SkASSERT(ddlTask->isClosed());
Adlai Holler7580ad42020-06-24 13:45:25 -0400660
Robert Phillips38d64b02018-09-04 13:23:26 -0400661 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500662}
663
Robert Phillips38d64b02018-09-04 13:23:26 -0400664#ifdef SK_DEBUG
665void GrDrawingManager::validate() const {
Adlai Holler4ef108e2020-12-30 16:21:13 +0000666 if (fActiveOpsTask) {
667 SkASSERT(!fDAG.empty());
668 SkASSERT(!fActiveOpsTask->isClosed());
669 SkASSERT(fActiveOpsTask == fDAG.back().get());
670 }
Adlai Holler039f90c2020-11-19 15:20:31 +0000671
Adlai Holler4ef108e2020-12-30 16:21:13 +0000672 for (int i = 0; i < fDAG.count(); ++i) {
673 if (fActiveOpsTask != fDAG[i].get()) {
674 // The resolveTask associated with the activeTask remains open for as long as the
675 // activeTask does.
676 bool isActiveResolveTask =
677 fActiveOpsTask && fActiveOpsTask->fTextureResolveTask == fDAG[i].get();
678 SkASSERT(isActiveResolveTask || fDAG[i]->isClosed());
Adlai Holler76893192020-12-22 19:41:19 +0000679 }
Adlai Holler4ef108e2020-12-30 16:21:13 +0000680 }
Adlai Holler76893192020-12-22 19:41:19 +0000681
Adlai Holler4ef108e2020-12-30 16:21:13 +0000682 if (!fDAG.empty() && !fDAG.back()->isClosed()) {
683 SkASSERT(fActiveOpsTask == fDAG.back().get());
Robert Phillips38d64b02018-09-04 13:23:26 -0400684 }
685}
686#endif
687
Adlai Holler4ef108e2020-12-30 16:21:13 +0000688void GrDrawingManager::closeActiveOpsTask() {
689 if (fActiveOpsTask) {
690 // This is a temporary fix for the partial-MDB world. In that world we're not
Adlai Holler039f90c2020-11-19 15:20:31 +0000691 // reordering so ops that (in the single opsTask world) would've just glommed onto the
Greg Danielf41b2bd2019-08-22 16:19:24 -0400692 // end of the single opsTask but referred to a far earlier RT need to appear in their
693 // own opsTask.
Adlai Holler039f90c2020-11-19 15:20:31 +0000694 fActiveOpsTask->makeClosed(*fContext->priv().caps());
695 fActiveOpsTask = nullptr;
robertphillips3dc6ae52015-10-20 09:54:32 -0700696 }
Chris Dalton5fe99772019-08-06 11:57:39 -0600697}
698
Greg Daniel16f5c652019-10-29 11:26:01 -0400699sk_sp<GrOpsTask> GrDrawingManager::newOpsTask(GrSurfaceProxyView surfaceView,
Herb Derby0b1228d2021-04-05 18:38:35 -0400700 sk_sp<GrArenas> arenas,
Robert Phillips19006652020-11-19 14:20:57 -0500701 bool flushTimeOpsTask) {
Chris Dalton5fe99772019-08-06 11:57:39 -0600702 SkDEBUGCODE(this->validate());
703 SkASSERT(fContext);
704
Adlai Holler4ef108e2020-12-30 16:21:13 +0000705 this->closeActiveOpsTask();
robertphillips3dc6ae52015-10-20 09:54:32 -0700706
Herb Derby0b1228d2021-04-05 18:38:35 -0400707 sk_sp<GrOpsTask> opsTask(new GrOpsTask(this,
708 std::move(surfaceView),
709 fContext->priv().auditTrail(),
710 std::move(arenas)));
Brian Salomon982127b2021-01-21 10:43:35 -0500711 SkASSERT(this->getLastRenderTask(opsTask->target(0)) == opsTask.get());
robertphillips3dc6ae52015-10-20 09:54:32 -0700712
Robert Phillips19006652020-11-19 14:20:57 -0500713 if (flushTimeOpsTask) {
714 fOnFlushRenderTasks.push_back(opsTask);
715 } else {
Adlai Holler33432272020-11-11 13:54:37 -0500716 this->appendTask(opsTask);
Adlai Holler039f90c2020-11-19 15:20:31 +0000717
Adlai Holler4ef108e2020-12-30 16:21:13 +0000718 fActiveOpsTask = opsTask.get();
Robert Phillips941d1442017-06-14 16:37:02 -0400719 }
robertphillips3dc6ae52015-10-20 09:54:32 -0700720
Robert Phillips38d64b02018-09-04 13:23:26 -0400721 SkDEBUGCODE(this->validate());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400722 return opsTask;
robertphillips3dc6ae52015-10-20 09:54:32 -0700723}
724
Adlai Holler039f90c2020-11-19 15:20:31 +0000725GrTextureResolveRenderTask* GrDrawingManager::newTextureResolveRenderTask(const GrCaps& caps) {
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600726 // 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 +0000727 // in sorting and opsTask reduction mode) the render tasks that depend on any proxy's current
Greg Danielf41b2bd2019-08-22 16:19:24 -0400728 // state. This is because those opsTasks can still receive new ops and because if they refer to
Adlai Holler039f90c2020-11-19 15:20:31 +0000729 // the mipmapped version of 'proxy', they will then come to depend on the render task being
Chris Dalton4ece96d2019-08-30 11:26:39 -0600730 // created here.
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600731 //
Adlai Holler039f90c2020-11-19 15:20:31 +0000732 // Add the new textureResolveTask before the fActiveOpsTask (if not in
733 // sorting/opsTask-splitting-reduction mode) because it will depend upon this resolve task.
Chris Dalton3d770272019-08-14 09:24:37 -0600734 // NOTE: Putting it here will also reduce the amount of work required by the topological sort.
Adlai Holler33432272020-11-11 13:54:37 -0500735 GrRenderTask* task = this->insertTaskBeforeLast(sk_make_sp<GrTextureResolveRenderTask>());
736 return static_cast<GrTextureResolveRenderTask*>(task);
Chris Dalton3d770272019-08-14 09:24:37 -0600737}
738
Greg Danielc30f1a92019-09-06 15:28:58 -0400739void GrDrawingManager::newWaitRenderTask(sk_sp<GrSurfaceProxy> proxy,
Greg Daniel301015c2019-11-18 14:06:46 -0500740 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> semaphores,
Greg Danielc30f1a92019-09-06 15:28:58 -0400741 int numSemaphores) {
742 SkDEBUGCODE(this->validate());
743 SkASSERT(fContext);
744
745 const GrCaps& caps = *fContext->priv().caps();
746
Greg Daniel16f5c652019-10-29 11:26:01 -0400747 sk_sp<GrWaitRenderTask> waitTask = sk_make_sp<GrWaitRenderTask>(GrSurfaceProxyView(proxy),
748 std::move(semaphores),
Greg Danielc30f1a92019-09-06 15:28:58 -0400749 numSemaphores);
Greg Danielc30f1a92019-09-06 15:28:58 -0400750
Brian Salomon982127b2021-01-21 10:43:35 -0500751 if (fActiveOpsTask && (fActiveOpsTask->target(0) == proxy.get())) {
Adlai Holler4ef108e2020-12-30 16:21:13 +0000752 SkASSERT(this->getLastRenderTask(proxy.get()) == fActiveOpsTask);
753 this->insertTaskBeforeLast(waitTask);
754 // In this case we keep the current renderTask open but just insert the new waitTask
755 // before it in the list. The waitTask will never need to trigger any resolves or mip
756 // map generation which is the main advantage of going through the proxy version.
757 // Additionally we would've had to temporarily set the wait task as the lastRenderTask
758 // on the proxy, add the dependency, and then reset the lastRenderTask to
759 // fActiveOpsTask. Additionally we make the waitTask depend on all of fActiveOpsTask
760 // dependencies so that we don't unnecessarily reorder the waitTask before them.
761 // Note: Any previous Ops already in fActiveOpsTask will get blocked by the wait
762 // semaphore even though they don't need to be for correctness.
763
764 // Make sure we add the dependencies of fActiveOpsTask to waitTask first or else we'll
765 // get a circular self dependency of waitTask on waitTask.
766 waitTask->addDependenciesFromOtherTask(fActiveOpsTask);
767 fActiveOpsTask->addDependency(waitTask.get());
Adlai Holler76893192020-12-22 19:41:19 +0000768 } else {
Adlai Holler4ef108e2020-12-30 16:21:13 +0000769 // In this case we just close the previous RenderTask and start and append the waitTask
770 // to the DAG. Since it is the last task now we call setLastRenderTask on the proxy. If
771 // there is a lastTask on the proxy we make waitTask depend on that task. This
772 // dependency isn't strictly needed but it does keep the DAG from reordering the
773 // waitTask earlier and blocking more tasks.
774 if (GrRenderTask* lastTask = this->getLastRenderTask(proxy.get())) {
775 waitTask->addDependency(lastTask);
Adlai Holler76893192020-12-22 19:41:19 +0000776 }
Adlai Holler4ef108e2020-12-30 16:21:13 +0000777 this->setLastRenderTask(proxy.get(), waitTask.get());
778 this->closeActiveOpsTask();
779 this->appendTask(waitTask);
Greg Danielc30f1a92019-09-06 15:28:58 -0400780 }
781 waitTask->makeClosed(caps);
782
783 SkDEBUGCODE(this->validate());
784}
785
Greg Danielbbfec9d2019-08-20 10:56:51 -0400786void GrDrawingManager::newTransferFromRenderTask(sk_sp<GrSurfaceProxy> srcProxy,
787 const SkIRect& srcRect,
788 GrColorType surfaceColorType,
789 GrColorType dstColorType,
790 sk_sp<GrGpuBuffer> dstBuffer,
791 size_t dstOffset) {
792 SkDEBUGCODE(this->validate());
793 SkASSERT(fContext);
Adlai Holler4ef108e2020-12-30 16:21:13 +0000794 this->closeActiveOpsTask();
Greg Danielbbfec9d2019-08-20 10:56:51 -0400795
Adlai Holler33432272020-11-11 13:54:37 -0500796 GrRenderTask* task = this->appendTask(sk_make_sp<GrTransferFromRenderTask>(
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400797 srcProxy, srcRect, surfaceColorType, dstColorType,
798 std::move(dstBuffer), dstOffset));
Greg Danielbbfec9d2019-08-20 10:56:51 -0400799
800 const GrCaps& caps = *fContext->priv().caps();
801
Brian Salomon7e67dca2020-07-21 09:27:25 -0400802 // We always say GrMipmapped::kNo here since we are always just copying from the base layer. We
Greg Danielbbfec9d2019-08-20 10:56:51 -0400803 // don't need to make sure the whole mip map chain is valid.
Brian Salomon7e67dca2020-07-21 09:27:25 -0400804 task->addDependency(this, srcProxy.get(), GrMipmapped::kNo,
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400805 GrTextureResolveManager(this), caps);
Greg Danielbbfec9d2019-08-20 10:56:51 -0400806 task->makeClosed(caps);
807
Greg Danielbbfec9d2019-08-20 10:56:51 -0400808 // We have closed the previous active oplist but since a new oplist isn't being added there
809 // shouldn't be an active one.
Adlai Holler039f90c2020-11-19 15:20:31 +0000810 SkASSERT(!fActiveOpsTask);
Greg Danielbbfec9d2019-08-20 10:56:51 -0400811 SkDEBUGCODE(this->validate());
812}
813
Brian Salomond63638b2021-03-05 14:00:07 -0500814sk_sp<GrRenderTask> GrDrawingManager::newCopyRenderTask(sk_sp<GrSurfaceProxy> src,
815 SkIRect srcRect,
816 sk_sp<GrSurfaceProxy> dst,
817 SkIPoint dstPoint,
818 GrSurfaceOrigin origin) {
Greg Daniele227fe42019-08-21 13:52:24 -0400819 SkDEBUGCODE(this->validate());
820 SkASSERT(fContext);
Greg Daniele227fe42019-08-21 13:52:24 -0400821
Brian Salomon56c78f42021-02-03 16:56:55 -0500822 // It'd be nicer to check this in GrCopyRenderTask::Make. This gets complicated because of
823 // "active ops task" tracking. dst will be the target of our copy task but it might also be the
824 // target of the active ops task. We currently require the active ops task to be closed before
825 // making a new task that targets the same proxy. However, if we first close the active ops
826 // task, then fail to make a copy task, the next active ops task may target the same proxy. This
827 // will trip an assert related to unnecessary ops task splitting.
828 if (src->framebufferOnly()) {
Brian Salomond63638b2021-03-05 14:00:07 -0500829 return nullptr;
Brian Salomon56c78f42021-02-03 16:56:55 -0500830 }
831
Brian Salomon507c5652021-02-03 20:32:06 +0000832 this->closeActiveOpsTask();
833
Brian Salomond63638b2021-03-05 14:00:07 -0500834 sk_sp<GrRenderTask> task = GrCopyRenderTask::Make(this,
835 src,
836 srcRect,
837 std::move(dst),
838 dstPoint,
839 origin);
Greg Daniele227fe42019-08-21 13:52:24 -0400840 if (!task) {
Brian Salomond63638b2021-03-05 14:00:07 -0500841 return nullptr;
Greg Daniele227fe42019-08-21 13:52:24 -0400842 }
843
Brian Salomond63638b2021-03-05 14:00:07 -0500844 this->appendTask(task);
845
Brian Salomon0f9f8002021-01-22 16:30:50 -0500846 const GrCaps& caps = *fContext->priv().caps();
Brian Salomon7e67dca2020-07-21 09:27:25 -0400847 // We always say GrMipmapped::kNo here since we are always just copying from the base layer to
Greg Daniele227fe42019-08-21 13:52:24 -0400848 // another base layer. We don't need to make sure the whole mip map chain is valid.
Brian Salomon507c5652021-02-03 20:32:06 +0000849 task->addDependency(this, src.get(), GrMipmapped::kNo, GrTextureResolveManager(this), caps);
850 task->makeClosed(caps);
Greg Daniele227fe42019-08-21 13:52:24 -0400851
Greg Daniele227fe42019-08-21 13:52:24 -0400852 // We have closed the previous active oplist but since a new oplist isn't being added there
853 // shouldn't be an active one.
Adlai Holler039f90c2020-11-19 15:20:31 +0000854 SkASSERT(!fActiveOpsTask);
Greg Daniele227fe42019-08-21 13:52:24 -0400855 SkDEBUGCODE(this->validate());
Brian Salomond63638b2021-03-05 14:00:07 -0500856 return task;
Greg Daniele227fe42019-08-21 13:52:24 -0400857}
858
Brian Salomonbe1084b2021-01-26 13:29:30 -0500859bool GrDrawingManager::newWritePixelsTask(sk_sp<GrSurfaceProxy> dst,
860 SkIRect rect,
861 GrColorType srcColorType,
862 GrColorType dstColorType,
863 const GrMipLevel levels[],
Brian Salomon75ee7372021-04-06 15:04:35 -0400864 int levelCount) {
Brian Salomonbe1084b2021-01-26 13:29:30 -0500865 SkDEBUGCODE(this->validate());
866 SkASSERT(fContext);
867
868 this->closeActiveOpsTask();
869 const GrCaps& caps = *fContext->priv().caps();
870
871 // On platforms that prefer flushes over VRAM use (i.e., ANGLE) we're better off forcing a
872 // complete flush here.
873 if (!caps.preferVRAMUseOverFlushes()) {
874 this->flushSurfaces(SkSpan<GrSurfaceProxy*>{},
875 SkSurface::BackendSurfaceAccess::kNoAccess,
876 GrFlushInfo{},
877 nullptr);
878 }
879
880 GrRenderTask* task = this->appendTask(GrWritePixelsTask::Make(this,
881 std::move(dst),
882 rect,
883 srcColorType,
884 dstColorType,
885 levels,
Brian Salomon75ee7372021-04-06 15:04:35 -0400886 levelCount));
Brian Salomonbe1084b2021-01-26 13:29:30 -0500887 if (!task) {
888 return false;
889 }
890
891 task->makeClosed(caps);
892
893 // We have closed the previous active oplist but since a new oplist isn't being added there
894 // shouldn't be an active one.
895 SkASSERT(!fActiveOpsTask);
896 SkDEBUGCODE(this->validate());
897 return true;
898}
899
Robert Phillips3674f582021-06-16 12:05:54 -0400900#if GR_OGA
robertphillips68737822015-10-29 12:12:21 -0700901/*
902 * This method finds a path renderer that can draw the specified path on
903 * the provided target.
904 * Due to its expense, the software path renderer has split out so it can
905 * can be individually allowed/disallowed via the "allowSW" boolean.
906 */
907GrPathRenderer* GrDrawingManager::getPathRenderer(const GrPathRenderer::CanDrawPathArgs& args,
908 bool allowSW,
909 GrPathRendererChain::DrawType drawType,
910 GrPathRenderer::StencilSupport* stencilSupport) {
911
912 if (!fPathRendererChain) {
John Stilesfbd050b2020-08-03 13:21:46 -0400913 fPathRendererChain =
914 std::make_unique<GrPathRendererChain>(fContext, fOptionsForPathRendererChain);
robertphillips68737822015-10-29 12:12:21 -0700915 }
916
917 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(args, drawType, stencilSupport);
918 if (!pr && allowSW) {
Brian Salomone7df0bb2018-05-07 14:44:57 -0400919 auto swPR = this->getSoftwarePathRenderer();
920 if (GrPathRenderer::CanDrawPath::kNo != swPR->canDrawPath(args)) {
921 pr = swPR;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500922 }
robertphillips68737822015-10-29 12:12:21 -0700923 }
924
Robert Phillipsd81379d2020-04-21 10:39:02 -0400925#if GR_PATH_RENDERER_SPEW
926 if (pr) {
927 SkDebugf("getPathRenderer: %s\n", pr->name());
928 }
929#endif
930
robertphillips68737822015-10-29 12:12:21 -0700931 return pr;
932}
933
Brian Salomone7df0bb2018-05-07 14:44:57 -0400934GrPathRenderer* GrDrawingManager::getSoftwarePathRenderer() {
935 if (!fSoftwarePathRenderer) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400936 fSoftwarePathRenderer.reset(
Robert Phillips9da87e02019-02-04 13:26:26 -0500937 new GrSoftwarePathRenderer(fContext->priv().proxyProvider(),
Ben Wagner9ec70c62018-07-12 13:30:47 -0400938 fOptionsForPathRendererChain.fAllowPathMaskCaching));
Brian Salomone7df0bb2018-05-07 14:44:57 -0400939 }
Ben Wagner9ec70c62018-07-12 13:30:47 -0400940 return fSoftwarePathRenderer.get();
Brian Salomone7df0bb2018-05-07 14:44:57 -0400941}
942
Chris Daltonfddb6c02017-11-04 15:22:22 -0600943GrCoverageCountingPathRenderer* GrDrawingManager::getCoverageCountingPathRenderer() {
944 if (!fPathRendererChain) {
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500945 fPathRendererChain = std::make_unique<GrPathRendererChain>(fContext,
946 fOptionsForPathRendererChain);
Chris Daltonfddb6c02017-11-04 15:22:22 -0600947 }
948 return fPathRendererChain->getCoverageCountingPathRenderer();
949}
950
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600951GrTessellationPathRenderer* GrDrawingManager::getTessellationPathRenderer() {
Chris Daltone6ae4762021-02-05 14:56:21 -0700952 if (!fPathRendererChain) {
953 fPathRendererChain = std::make_unique<GrPathRendererChain>(fContext,
954 fOptionsForPathRendererChain);
955 }
956 return fPathRendererChain->getTessellationPathRenderer();
957}
958
Robert Phillips3674f582021-06-16 12:05:54 -0400959#endif // GR_OGA
960
Brian Salomon653f42f2018-07-10 10:07:31 -0400961void GrDrawingManager::flushIfNecessary() {
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400962 auto direct = fContext->asDirectContext();
Robert Phillips6a6de562019-02-15 15:19:15 -0500963 if (!direct) {
964 return;
965 }
966
967 auto resourceCache = direct->priv().getResourceCache();
Brian Salomon653f42f2018-07-10 10:07:31 -0400968 if (resourceCache && resourceCache->requestsFlush()) {
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500969 if (this->flush({}, SkSurface::BackendSurfaceAccess::kNoAccess, GrFlushInfo(), nullptr)) {
Greg Danielfe159622020-04-10 17:43:51 +0000970 this->submitToGpu(false);
971 }
Brian Salomon57d2beab2018-09-10 09:35:41 -0400972 resourceCache->purgeAsNeeded();
Brian Salomon653f42f2018-07-10 10:07:31 -0400973 }
974}