blob: 71125b1f13e3893fd213b8ef7e9119d9e3bec769 [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"
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 Derbya08bde62020-06-12 15:46:06 -040046#include "src/gpu/text/GrSDFTOptions.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050047#include "src/image/SkSurface_Gpu.h"
robertphillips498d7ac2015-10-30 10:11:30 -070048
Robert Phillips22310d62018-09-05 11:07:21 -040049///////////////////////////////////////////////////////////////////////////////////////////////////
Robert Phillips69893702019-02-22 11:16:30 -050050GrDrawingManager::GrDrawingManager(GrRecordingContext* context,
Robert Phillips22310d62018-09-05 11:07:21 -040051 const GrPathRendererChain::Options& optionsForPathRendererChain,
Greg Danielf41b2bd2019-08-22 16:19:24 -040052 bool reduceOpsTaskSplitting)
Robert Phillips22310d62018-09-05 11:07:21 -040053 : fContext(context)
54 , fOptionsForPathRendererChain(optionsForPathRendererChain)
Robert Phillips22310d62018-09-05 11:07:21 -040055 , fPathRendererChain(nullptr)
56 , fSoftwarePathRenderer(nullptr)
Robert Phillips6db27c22019-05-01 10:43:56 -040057 , fFlushing(false)
Herb Derby082232b2020-06-10 15:08:18 -040058 , fReduceOpsTaskSplitting(reduceOpsTaskSplitting) { }
Robert Phillips22310d62018-09-05 11:07:21 -040059
robertphillips3dc6ae52015-10-20 09:54:32 -070060GrDrawingManager::~GrDrawingManager() {
Adlai Holler33432272020-11-11 13:54:37 -050061 this->closeAllTasks();
62 this->removeRenderTasks(0, fDAG.count());
robertphillips3dc6ae52015-10-20 09:54:32 -070063}
64
Robert Phillipsa9162df2019-02-11 14:12:03 -050065bool GrDrawingManager::wasAbandoned() const {
Robert Phillips9eb00022020-06-30 15:30:12 -040066 return fContext->abandoned();
robertphillips3dc6ae52015-10-20 09:54:32 -070067}
68
robertphillips68737822015-10-29 12:12:21 -070069void GrDrawingManager::freeGpuResources() {
Jim Van Verth106b5c42017-09-26 12:45:29 -040070 for (int i = fOnFlushCBObjects.count() - 1; i >= 0; --i) {
71 if (!fOnFlushCBObjects[i]->retainOnFreeGpuResources()) {
72 // it's safe to just do this because we're iterating in reverse
73 fOnFlushCBObjects.removeShuffle(i);
74 }
75 }
76
robertphillips68737822015-10-29 12:12:21 -070077 // a path renderer may be holding onto resources
robertphillips13391dd2015-10-30 05:15:11 -070078 fPathRendererChain = nullptr;
Ben Wagner9ec70c62018-07-12 13:30:47 -040079 fSoftwarePathRenderer = nullptr;
Robert Phillipse3302df2017-04-24 07:31:02 -040080}
81
Adlai Hollerc2bfcff2020-11-06 15:39:36 -050082// MDB TODO: make use of the 'proxies' parameter.
Greg Daniel9efe3862020-06-11 11:51:06 -040083bool GrDrawingManager::flush(
Adlai Hollerc2bfcff2020-11-06 15:39:36 -050084 SkSpan<GrSurfaceProxy*> proxies,
Greg Daniel9efe3862020-06-11 11:51:06 -040085 SkSurface::BackendSurfaceAccess access,
86 const GrFlushInfo& info,
87 const GrBackendSurfaceMutableState* newState) {
Brian Salomon57d2beab2018-09-10 09:35:41 -040088 GR_CREATE_TRACE_MARKER_CONTEXT("GrDrawingManager", "flush", fContext);
Brian Salomondcbb9d92017-07-19 10:53:20 -040089
robertphillips7761d612016-05-16 09:14:53 -070090 if (fFlushing || this->wasAbandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -040091 if (info.fSubmittedProc) {
92 info.fSubmittedProc(info.fSubmittedContext, false);
93 }
Greg Daniele6bfb7d2019-04-17 15:26:11 -040094 if (info.fFinishedProc) {
95 info.fFinishedProc(info.fFinishedContext);
Greg Daniela3aa75a2019-04-12 14:24:55 -040096 }
Greg Danielfe159622020-04-10 17:43:51 +000097 return false;
joshualittb8918c42015-12-18 09:59:46 -080098 }
Robert Phillips602df412019-04-08 11:10:39 -040099
Robert Phillips38d64b02018-09-04 13:23:26 -0400100 SkDEBUGCODE(this->validate());
101
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500102 // As of now we only short-circuit if we got an explicit list of surfaces to flush.
103 if (!proxies.empty() && !info.fNumSemaphores && !info.fFinishedProc &&
Greg Danielce9f0162020-06-30 13:42:46 -0400104 access == SkSurface::BackendSurfaceAccess::kNoAccess && !newState) {
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500105 bool allUnused = std::all_of(proxies.begin(), proxies.end(), [&](GrSurfaceProxy* proxy) {
Adlai Holler33432272020-11-11 13:54:37 -0500106 bool used = std::any_of(fDAG.begin(), fDAG.end(), [&](auto& task) {
107 return task && task->isUsed(proxy);
108 });
Robert Phillips58a8ccc2021-01-11 09:07:55 -0500109 return !used;
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500110 });
111 if (allUnused) {
Greg Daniel55822f12020-05-26 11:26:45 -0400112 if (info.fSubmittedProc) {
113 info.fSubmittedProc(info.fSubmittedContext, true);
114 }
Greg Danielfe159622020-04-10 17:43:51 +0000115 return false;
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400116 }
Robert Phillips9313aa72019-04-09 18:41:27 -0400117 }
118
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400119 auto direct = fContext->asDirectContext();
Adlai Holleraee25fd2020-11-06 12:34:56 -0500120 SkASSERT(direct);
Brian Salomon9241a6d2019-10-03 13:26:54 -0400121 direct->priv().clientMappedBufferManager()->process();
Robert Phillips6a6de562019-02-15 15:19:15 -0500122
123 GrGpu* gpu = direct->priv().getGpu();
Greg Daniel55822f12020-05-26 11:26:45 -0400124 // We have a non abandoned and direct GrContext. It must have a GrGpu.
125 SkASSERT(gpu);
Greg Daniela3aa75a2019-04-12 14:24:55 -0400126
joshualittb8918c42015-12-18 09:59:46 -0800127 fFlushing = true;
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400128
Robert Phillips6a6de562019-02-15 15:19:15 -0500129 auto resourceProvider = direct->priv().resourceProvider();
130 auto resourceCache = direct->priv().getResourceCache();
131
Chris Dalton6b498102019-08-01 14:14:52 -0600132 // Semi-usually the GrRenderTasks are already closed at this point, but sometimes Ganesh needs
Greg Danielf41b2bd2019-08-22 16:19:24 -0400133 // to flush mid-draw. In that case, the SkGpuDevice's opsTasks won't be closed but need to be
134 // flushed anyway. Closing such opsTasks here will mean new ones will be created to replace them
Chris Dalton6b498102019-08-01 14:14:52 -0600135 // if the SkGpuDevice(s) write to them again.
Adlai Holler33432272020-11-11 13:54:37 -0500136 this->closeAllTasks();
Adlai Holler039f90c2020-11-19 15:20:31 +0000137 fActiveOpsTask = nullptr;
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400138
Adlai Holler33432272020-11-11 13:54:37 -0500139 this->sortTasks();
Adlai Holler78036082021-01-07 11:41:33 -0500140
141 if (fReduceOpsTaskSplitting) {
142 this->reorderTasks();
143 }
144
Brian Salomon601ac802019-02-07 13:37:16 -0500145 if (!fCpuBufferCache) {
146 // We cache more buffers when the backend is using client side arrays. Otherwise, we
147 // expect each pool will use a CPU buffer as a staging buffer before uploading to a GPU
148 // buffer object. Each pool only requires one staging buffer at a time.
149 int maxCachedBuffers = fContext->priv().caps()->preferClientSideDynamicBuffers() ? 2 : 6;
150 fCpuBufferCache = GrBufferAllocPool::CpuBufferCache::Make(maxCachedBuffers);
Brian Salomon58f153c2018-10-18 21:51:15 -0400151 }
Robert Phillipsa4c93ac2017-05-18 11:40:04 -0400152
Robert Phillipse5f73282019-06-18 17:15:04 -0400153 GrOpFlushState flushState(gpu, resourceProvider, &fTokenTracker, fCpuBufferCache);
Robert Phillips40a29d72018-01-18 12:59:22 -0500154
Chris Daltonfe199b72017-05-05 11:26:15 -0400155 GrOnFlushResourceProvider onFlushProvider(this);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400156
Chris Dalton12658942017-10-05 19:45:25 -0600157 // Prepare any onFlush op lists (e.g. atlases).
Chris Daltonfe199b72017-05-05 11:26:15 -0400158 if (!fOnFlushCBObjects.empty()) {
Robert Phillips07f675d2020-11-16 13:44:01 -0500159 fFlushingRenderTaskIDs.reserve_back(fDAG.count());
160 for (const auto& task : fDAG) {
161 if (task) {
162 task->gatherIDs(&fFlushingRenderTaskIDs);
Adlai Holler33432272020-11-11 13:54:37 -0500163 }
164 }
Robert Phillips22310d62018-09-05 11:07:21 -0400165
Chris Daltonfe199b72017-05-05 11:26:15 -0400166 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
Adlai Holler9902cff2020-11-11 08:51:25 -0500167 onFlushCBObject->preFlush(&onFlushProvider, fFlushingRenderTaskIDs);
Chris Daltonc4b47352019-08-23 10:10:36 -0600168 }
169 for (const auto& onFlushRenderTask : fOnFlushRenderTasks) {
170 onFlushRenderTask->makeClosed(*fContext->priv().caps());
Chris Dalton706a6ff2017-11-29 22:01:06 -0700171#ifdef SK_DEBUG
Chris Daltonc4b47352019-08-23 10:10:36 -0600172 // OnFlush callbacks are invoked during flush, and are therefore expected to handle
Adlai Hollera3987cc2021-02-05 15:52:28 -0500173 // resource allocation & usage on their own. (No lazy proxies!)
Chris Daltonc4b47352019-08-23 10:10:36 -0600174 onFlushRenderTask->visitTargetAndSrcProxies_debugOnly(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400175 [](GrSurfaceProxy* p, GrMipmapped mipMapped) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400176 SkASSERT(!p->isLazy());
Chris Dalton4ece96d2019-08-30 11:26:39 -0600177 if (p->requiresManualMSAAResolve()) {
178 // The onFlush callback is responsible for ensuring MSAA gets resolved.
179 SkASSERT(p->asRenderTargetProxy() && !p->asRenderTargetProxy()->isMSAADirty());
180 }
Brian Salomon7e67dca2020-07-21 09:27:25 -0400181 if (GrMipmapped::kYes == mipMapped) {
Chris Daltonc4b47352019-08-23 10:10:36 -0600182 // The onFlush callback is responsible for regenerating mips if needed.
Brian Salomon8c82a872020-07-21 12:09:58 -0400183 SkASSERT(p->asTextureProxy() && !p->asTextureProxy()->mipmapsAreDirty());
Chris Daltonc4b47352019-08-23 10:10:36 -0600184 }
185 });
Chris Dalton706a6ff2017-11-29 22:01:06 -0700186#endif
Chris Daltonc4b47352019-08-23 10:10:36 -0600187 onFlushRenderTask->prepare(&flushState);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400188 }
189 }
190
robertphillipsa13e2022015-11-11 12:01:09 -0800191#if 0
Brian Salomon09d994e2016-12-21 11:14:46 -0500192 // Enable this to print out verbose GrOp information
Adlai Hollerabdfd392020-10-19 09:00:36 -0400193 SkDEBUGCODE(SkDebugf("onFlush renderTasks (%d):\n", fOnFlushRenderTasks.count()));
Chris Daltonc4b47352019-08-23 10:10:36 -0600194 for (const auto& onFlushRenderTask : fOnFlushRenderTasks) {
Adlai Hollerabdfd392020-10-19 09:00:36 -0400195 SkDEBUGCODE(onFlushRenderTask->dump(/* printDependencies */ true);)
Chris Daltonc4b47352019-08-23 10:10:36 -0600196 }
Adlai Holler33432272020-11-11 13:54:37 -0500197 SkDEBUGCODE(SkDebugf("Normal renderTasks (%d):\n", fDAG.count()));
198 for (const auto& task : fDAG) {
199 SkDEBUGCODE(task->dump(/* printDependencies */ true);)
robertphillips3dc6ae52015-10-20 09:54:32 -0700200 }
robertphillipsa13e2022015-11-11 12:01:09 -0800201#endif
202
Robert Phillipseafd48a2017-11-16 07:52:08 -0500203 int startIndex, stopIndex;
204 bool flushed = false;
205
Robert Phillipsf8e25022017-11-08 15:24:31 -0500206 {
Adlai Holler33432272020-11-11 13:54:37 -0500207 GrResourceAllocator alloc(resourceProvider SkDEBUGCODE(, fDAG.count()));
208 for (int i = 0; i < fDAG.count(); ++i) {
209 if (fDAG[i]) {
210 fDAG[i]->gatherProxyIntervals(&alloc);
Robert Phillips22310d62018-09-05 11:07:21 -0400211 }
Greg Danielf41b2bd2019-08-22 16:19:24 -0400212 alloc.markEndOfOpsTask(i);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500213 }
Robert Phillipsc73666f2019-04-24 08:49:48 -0400214 alloc.determineRecyclability();
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400215
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500216 GrResourceAllocator::AssignError error = GrResourceAllocator::AssignError::kNoError;
Chris Dalton6b498102019-08-01 14:14:52 -0600217 int numRenderTasksExecuted = 0;
Brian Salomon577aa0f2018-11-30 13:32:23 -0500218 while (alloc.assign(&startIndex, &stopIndex, &error)) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500219 if (GrResourceAllocator::AssignError::kFailedProxyInstantiation == error) {
220 for (int i = startIndex; i < stopIndex; ++i) {
Adlai Holler33432272020-11-11 13:54:37 -0500221 GrRenderTask* renderTask = fDAG[i].get();
Chris Dalton6b498102019-08-01 14:14:52 -0600222 if (!renderTask) {
223 continue;
Robert Phillips01a91282018-07-26 08:03:04 -0400224 }
Chris Dalton6b498102019-08-01 14:14:52 -0600225 if (!renderTask->isInstantiated()) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400226 // No need to call the renderTask's handleInternalAllocationFailure
227 // since we will already skip executing the renderTask since it is not
228 // instantiated.
Chris Dalton6b498102019-08-01 14:14:52 -0600229 continue;
Robert Phillipsbbcb7f72018-05-31 11:16:19 -0400230 }
Chris Dalton6b498102019-08-01 14:14:52 -0600231 renderTask->handleInternalAllocationFailure();
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500232 }
Adlai Holler96ead542020-06-26 08:50:14 -0400233 this->removeRenderTasks(startIndex, stopIndex);
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500234 }
Robert Phillips4150eea2018-02-07 17:08:21 -0500235
Chris Dalton6b498102019-08-01 14:14:52 -0600236 if (this->executeRenderTasks(
237 startIndex, stopIndex, &flushState, &numRenderTasksExecuted)) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500238 flushed = true;
239 }
bsalomondc438982016-08-31 11:53:49 -0700240 }
Greg Danielc42b20b2017-10-04 10:34:49 -0400241 }
242
Chris Dalton91ab1552018-04-18 13:24:25 -0600243#ifdef SK_DEBUG
Adlai Holler33432272020-11-11 13:54:37 -0500244 for (const auto& task : fDAG) {
Adlai Holler96ead542020-06-26 08:50:14 -0400245 // All render tasks should have been cleared out by now – we only reset the array below to
246 // reclaim storage.
Adlai Holler33432272020-11-11 13:54:37 -0500247 SkASSERT(!task);
Chris Dalton91ab1552018-04-18 13:24:25 -0600248 }
249#endif
Adlai Holler25df1f72020-06-09 13:08:27 -0400250 fLastRenderTasks.reset();
Robert Phillips22310d62018-09-05 11:07:21 -0400251 fDAG.reset();
robertphillipsa13e2022015-11-11 12:01:09 -0800252
Robert Phillipsc994a932018-06-19 13:09:54 -0400253#ifdef SK_DEBUG
254 // In non-DDL mode this checks that all the flushed ops have been freed from the memory pool.
255 // When we move to partial flushes this assert will no longer be valid.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400256 // In DDL mode this check is somewhat superfluous since the memory for most of the ops/opsTasks
Robert Phillipsc994a932018-06-19 13:09:54 -0400257 // will be stored in the DDL's GrOpMemoryPools.
Herb Derbye32e1ab2020-10-27 10:29:46 -0400258 GrMemoryPool* opMemoryPool = fContext->priv().opMemoryPool();
Robert Phillipsc994a932018-06-19 13:09:54 -0400259 opMemoryPool->isEmpty();
260#endif
261
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500262 gpu->executeFlushInfo(proxies, access, info, newState);
robertphillipsa13e2022015-11-11 12:01:09 -0800263
Brian Salomon57d2beab2018-09-10 09:35:41 -0400264 // Give the cache a chance to purge resources that become purgeable due to flushing.
265 if (flushed) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500266 resourceCache->purgeAsNeeded();
Brian Salomon876a0172019-03-08 11:12:14 -0500267 flushed = false;
bsalomonb77a9072016-09-07 10:02:04 -0700268 }
Chris Daltonfe199b72017-05-05 11:26:15 -0400269 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
Adlai Holler9902cff2020-11-11 08:51:25 -0500270 onFlushCBObject->postFlush(fTokenTracker.nextTokenToFlush(), fFlushingRenderTaskIDs);
Brian Salomon876a0172019-03-08 11:12:14 -0500271 flushed = true;
272 }
273 if (flushed) {
274 resourceCache->purgeAsNeeded();
Chris Daltonfe199b72017-05-05 11:26:15 -0400275 }
Chris Dalton6b498102019-08-01 14:14:52 -0600276 fFlushingRenderTaskIDs.reset();
joshualittb8918c42015-12-18 09:59:46 -0800277 fFlushing = false;
Greg Daniel51316782017-08-02 15:10:09 +0000278
Greg Danielfe159622020-04-10 17:43:51 +0000279 return true;
280}
281
282bool GrDrawingManager::submitToGpu(bool syncToCpu) {
283 if (fFlushing || this->wasAbandoned()) {
284 return false;
285 }
286
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400287 auto direct = fContext->asDirectContext();
Greg Danielfe159622020-04-10 17:43:51 +0000288 if (!direct) {
289 return false; // Can't submit while DDL recording
290 }
291 GrGpu* gpu = direct->priv().getGpu();
292 return gpu->submitToGpu(syncToCpu);
robertphillips3dc6ae52015-10-20 09:54:32 -0700293}
294
Chris Dalton6b498102019-08-01 14:14:52 -0600295bool GrDrawingManager::executeRenderTasks(int startIndex, int stopIndex, GrOpFlushState* flushState,
296 int* numRenderTasksExecuted) {
Adlai Holler33432272020-11-11 13:54:37 -0500297 SkASSERT(startIndex <= stopIndex && stopIndex <= fDAG.count());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500298
Robert Phillips27483912018-04-20 12:43:18 -0400299#if GR_FLUSH_TIME_OP_SPEW
Greg Danielf41b2bd2019-08-22 16:19:24 -0400300 SkDebugf("Flushing opsTask: %d to %d out of [%d, %d]\n",
Adlai Holler33432272020-11-11 13:54:37 -0500301 startIndex, stopIndex, 0, fDAG.count());
Robert Phillips27483912018-04-20 12:43:18 -0400302 for (int i = startIndex; i < stopIndex; ++i) {
Adlai Holler33432272020-11-11 13:54:37 -0500303 if (fDAG[i]) {
Robert Phillips047d5bb2021-01-08 13:39:19 -0500304 SkString label;
305 label.printf("task %d/%d", i, fDAG.count());
306 fDAG[i]->dump(label, {}, true, true);
Robert Phillips1734dd32018-08-21 13:52:09 -0400307 }
Robert Phillips27483912018-04-20 12:43:18 -0400308 }
309#endif
310
Chris Dalton6b498102019-08-01 14:14:52 -0600311 bool anyRenderTasksExecuted = false;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500312
313 for (int i = startIndex; i < stopIndex; ++i) {
Adlai Holler33432272020-11-11 13:54:37 -0500314 GrRenderTask* renderTask = fDAG[i].get();
Greg Daniel15ecdf92019-08-30 15:35:23 -0400315 if (!renderTask || !renderTask->isInstantiated()) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500316 continue;
317 }
318
Chris Dalton6b498102019-08-01 14:14:52 -0600319 renderTask->prepare(flushState);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500320 }
321
322 // Upload all data to the GPU
323 flushState->preExecuteDraws();
324
Greg Danield2073452018-12-07 11:20:33 -0500325 // For Vulkan, if we have too many oplists to be flushed we end up allocating a lot of resources
326 // for each command buffer associated with the oplists. If this gets too large we can cause the
327 // devices to go OOM. In practice we usually only hit this case in our tests, but to be safe we
328 // put a cap on the number of oplists we will execute before flushing to the GPU to relieve some
329 // memory pressure.
Chris Dalton6b498102019-08-01 14:14:52 -0600330 static constexpr int kMaxRenderTasksBeforeFlush = 100;
Greg Danield2073452018-12-07 11:20:33 -0500331
Chris Daltonc4b47352019-08-23 10:10:36 -0600332 // Execute the onFlush renderTasks first, if any.
333 for (sk_sp<GrRenderTask>& onFlushRenderTask : fOnFlushRenderTasks) {
334 if (!onFlushRenderTask->execute(flushState)) {
335 SkDebugf("WARNING: onFlushRenderTask failed to execute.\n");
Robert Phillipseafd48a2017-11-16 07:52:08 -0500336 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600337 SkASSERT(onFlushRenderTask->unique());
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400338 onFlushRenderTask->disown(this);
Chris Daltonc4b47352019-08-23 10:10:36 -0600339 onFlushRenderTask = nullptr;
Chris Dalton6b498102019-08-01 14:14:52 -0600340 (*numRenderTasksExecuted)++;
341 if (*numRenderTasksExecuted >= kMaxRenderTasksBeforeFlush) {
Greg Danielfe159622020-04-10 17:43:51 +0000342 flushState->gpu()->submitToGpu(false);
Chris Dalton6b498102019-08-01 14:14:52 -0600343 *numRenderTasksExecuted = 0;
Greg Danield2073452018-12-07 11:20:33 -0500344 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500345 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600346 fOnFlushRenderTasks.reset();
Robert Phillipseafd48a2017-11-16 07:52:08 -0500347
348 // Execute the normal op lists.
349 for (int i = startIndex; i < stopIndex; ++i) {
Adlai Holler33432272020-11-11 13:54:37 -0500350 GrRenderTask* renderTask = fDAG[i].get();
Greg Daniel15ecdf92019-08-30 15:35:23 -0400351 if (!renderTask || !renderTask->isInstantiated()) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500352 continue;
353 }
354
Greg Daniel15ecdf92019-08-30 15:35:23 -0400355 if (renderTask->execute(flushState)) {
Chris Dalton6b498102019-08-01 14:14:52 -0600356 anyRenderTasksExecuted = true;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500357 }
Chris Dalton6b498102019-08-01 14:14:52 -0600358 (*numRenderTasksExecuted)++;
359 if (*numRenderTasksExecuted >= kMaxRenderTasksBeforeFlush) {
Greg Danielfe159622020-04-10 17:43:51 +0000360 flushState->gpu()->submitToGpu(false);
Chris Dalton6b498102019-08-01 14:14:52 -0600361 *numRenderTasksExecuted = 0;
Greg Danield2073452018-12-07 11:20:33 -0500362 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500363 }
364
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400365 SkASSERT(!flushState->opsRenderPass());
Robert Phillips40a29d72018-01-18 12:59:22 -0500366 SkASSERT(fTokenTracker.nextDrawToken() == fTokenTracker.nextTokenToFlush());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500367
Chris Dalton6b498102019-08-01 14:14:52 -0600368 // We reset the flush state before the RenderTasks so that the last resources to be freed are
369 // those that are written to in the RenderTasks. This helps to make sure the most recently used
370 // resources are the last to be purged by the resource cache.
Robert Phillipseafd48a2017-11-16 07:52:08 -0500371 flushState->reset();
372
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400373 this->removeRenderTasks(startIndex, stopIndex);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500374
Chris Dalton6b498102019-08-01 14:14:52 -0600375 return anyRenderTasksExecuted;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500376}
377
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400378void GrDrawingManager::removeRenderTasks(int startIndex, int stopIndex) {
379 for (int i = startIndex; i < stopIndex; ++i) {
Adlai Holler33432272020-11-11 13:54:37 -0500380 GrRenderTask* task = fDAG[i].get();
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400381 if (!task) {
382 continue;
383 }
Robert Phillips07f675d2020-11-16 13:44:01 -0500384 if (!task->unique() || task->requiresExplicitCleanup()) {
385 // TODO: Eventually uniqueness should be guaranteed: http://skbug.com/7111.
386 // DDLs, however, will always require an explicit notification for when they
387 // can clean up resources.
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400388 task->endFlush(this);
389 }
390 task->disown(this);
Adlai Holler33432272020-11-11 13:54:37 -0500391
392 // This doesn't cleanup any referring pointers (e.g. dependency pointers in the DAG).
393 // It works right now bc this is only called after the topological sort is complete
394 // (so the dangling pointers aren't used).
395 fDAG[i] = nullptr;
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400396 }
Adlai Holler33432272020-11-11 13:54:37 -0500397}
398
399void GrDrawingManager::sortTasks() {
Robert Phillipsfbbc3bb2020-11-16 14:58:56 -0500400 if (!GrTTopoSort<GrRenderTask, GrRenderTask::TopoSortTraits>(&fDAG)) {
Adlai Holler33432272020-11-11 13:54:37 -0500401 SkDEBUGFAIL("Render task topo sort failed.");
402 return;
403 }
404
405#ifdef SK_DEBUG
406 // This block checks for any unnecessary splits in the opsTasks. If two sequential opsTasks
407 // share the same backing GrSurfaceProxy it means the opsTask was artificially split.
408 if (!fDAG.empty()) {
409 GrOpsTask* prevOpsTask = fDAG[0]->asOpsTask();
410 for (int i = 1; i < fDAG.count(); ++i) {
411 GrOpsTask* curOpsTask = fDAG[i]->asOpsTask();
412
413 if (prevOpsTask && curOpsTask) {
Brian Salomon982127b2021-01-21 10:43:35 -0500414 SkASSERT(prevOpsTask->target(0) != curOpsTask->target(0));
Adlai Holler33432272020-11-11 13:54:37 -0500415 }
416
417 prevOpsTask = curOpsTask;
418 }
419 }
420#endif
421}
422
Adlai Holler78036082021-01-07 11:41:33 -0500423// Reorder the array to match the llist without reffing & unreffing sk_sp's.
Adlai Holler69c57e42021-01-25 15:44:26 +0000424// Both args must contain the same objects.
Adlai Holler78036082021-01-07 11:41:33 -0500425// This is basically a shim because clustering uses LList but the rest of drawmgr uses array.
426template <typename T>
427static void reorder_array_by_llist(const SkTInternalLList<T>& llist, SkTArray<sk_sp<T>>* array) {
428 int i = 0;
429 for (T* t : llist) {
Adlai Holler69c57e42021-01-25 15:44:26 +0000430 // Release the pointer that used to live here so it doesn't get unreffed.
431 [[maybe_unused]] T* old = array->at(i).release();
Adlai Holler78036082021-01-07 11:41:33 -0500432 array->at(i++).reset(t);
433 }
Adlai Holler69c57e42021-01-25 15:44:26 +0000434 SkASSERT(i == array->count());
Adlai Holler78036082021-01-07 11:41:33 -0500435}
436
437void GrDrawingManager::reorderTasks() {
438 SkASSERT(fReduceOpsTaskSplitting);
439 SkTInternalLList<GrRenderTask> llist;
Adlai Holler08f53112021-01-20 17:44:15 -0500440 bool clustered = GrClusterRenderTasks(fDAG, &llist);
Adlai Holler78036082021-01-07 11:41:33 -0500441 if (!clustered) {
442 return;
443 }
444 // TODO: Handle case where proposed order would blow our memory budget.
445 // Such cases are currently pathological, so we could just return here and keep current order.
446 reorder_array_by_llist(llist, &fDAG);
Adlai Holler93439d92021-01-26 09:20:39 -0500447
448 int newCount = 0;
449 for (int i = 0; i < fDAG.count(); i++) {
450 sk_sp<GrRenderTask>& task = fDAG[i];
451 if (auto opsTask = task->asOpsTask()) {
452 size_t remaining = fDAG.size() - i - 1;
453 SkSpan<sk_sp<GrRenderTask>> nextTasks{fDAG.end() - remaining, remaining};
454 int removeCount = opsTask->mergeFrom(nextTasks);
455 for (const auto& removed : nextTasks.first(removeCount)) {
456 removed->disown(this);
457 }
458 i += removeCount;
459 }
460 fDAG[newCount++] = std::move(task);
461 }
462 fDAG.resize_back(newCount);
Adlai Holler78036082021-01-07 11:41:33 -0500463}
464
Adlai Holler33432272020-11-11 13:54:37 -0500465void GrDrawingManager::closeAllTasks() {
466 const GrCaps& caps = *fContext->priv().caps();
467 for (auto& task : fDAG) {
468 if (task) {
469 task->makeClosed(caps);
470 }
471 }
472}
473
474GrRenderTask* GrDrawingManager::insertTaskBeforeLast(sk_sp<GrRenderTask> task) {
475 SkASSERT(!fDAG.empty());
476 if (!task) {
477 return nullptr;
478 }
479 // Release 'fDAG.back()' and grab the raw pointer, in case the SkTArray grows
480 // and reallocates during emplace_back.
481 // TODO: Either use std::vector that can do this for us, or use SkSTArray to get the
482 // perf win.
483 fDAG.emplace_back(fDAG.back().release());
484 return (fDAG[fDAG.count() - 2] = std::move(task)).get();
485}
486
487GrRenderTask* GrDrawingManager::appendTask(sk_sp<GrRenderTask> task) {
488 if (!task) {
489 return nullptr;
490 }
491 return fDAG.push_back(std::move(task)).get();
492}
493
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400494static void resolve_and_mipmap(GrGpu* gpu, GrSurfaceProxy* proxy) {
495 if (!proxy->isInstantiated()) {
496 return;
497 }
498
499 // In the flushSurfaces case, we need to resolve MSAA immediately after flush. This is
500 // because clients expect the flushed surface's backing texture to be fully resolved
501 // upon return.
502 if (proxy->requiresManualMSAAResolve()) {
503 auto* rtProxy = proxy->asRenderTargetProxy();
504 SkASSERT(rtProxy);
505 if (rtProxy->isMSAADirty()) {
506 SkASSERT(rtProxy->peekRenderTarget());
Jim Van Verthbb61fe32020-07-07 16:39:04 -0400507 gpu->resolveRenderTarget(rtProxy->peekRenderTarget(), rtProxy->msaaDirtyRect());
508 gpu->submitToGpu(false);
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400509 rtProxy->markMSAAResolved();
510 }
511 }
512 // If, after a flush, any of the proxies of interest have dirty mipmaps, regenerate them in
513 // case their backend textures are being stolen.
514 // (This special case is exercised by the ReimportImageTextureWithMipLevels test.)
515 // FIXME: It may be more ideal to plumb down a "we're going to steal the backends" flag.
516 if (auto* textureProxy = proxy->asTextureProxy()) {
Brian Salomon8c82a872020-07-21 12:09:58 -0400517 if (textureProxy->mipmapsAreDirty()) {
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400518 SkASSERT(textureProxy->peekTexture());
519 gpu->regenerateMipMapLevels(textureProxy->peekTexture());
Brian Salomon8c82a872020-07-21 12:09:58 -0400520 textureProxy->markMipmapsClean();
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400521 }
522 }
523}
524
Greg Daniel9efe3862020-06-11 11:51:06 -0400525GrSemaphoresSubmitted GrDrawingManager::flushSurfaces(
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500526 SkSpan<GrSurfaceProxy*> proxies,
Greg Daniel9efe3862020-06-11 11:51:06 -0400527 SkSurface::BackendSurfaceAccess access,
528 const GrFlushInfo& info,
529 const GrBackendSurfaceMutableState* newState) {
bsalomon6a2b1942016-09-08 11:28:59 -0700530 if (this->wasAbandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -0400531 if (info.fSubmittedProc) {
532 info.fSubmittedProc(info.fSubmittedContext, false);
533 }
534 if (info.fFinishedProc) {
535 info.fFinishedProc(info.fFinishedContext);
536 }
Greg Daniel51316782017-08-02 15:10:09 +0000537 return GrSemaphoresSubmitted::kNo;
bsalomon6a2b1942016-09-08 11:28:59 -0700538 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400539 SkDEBUGCODE(this->validate());
bsalomon6a2b1942016-09-08 11:28:59 -0700540
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400541 auto direct = fContext->asDirectContext();
Adlai Holleraee25fd2020-11-06 12:34:56 -0500542 SkASSERT(direct);
Robert Phillips6a6de562019-02-15 15:19:15 -0500543 GrGpu* gpu = direct->priv().getGpu();
Greg Daniel55822f12020-05-26 11:26:45 -0400544 // We have a non abandoned and direct GrContext. It must have a GrGpu.
545 SkASSERT(gpu);
Robert Phillips874b5352018-03-16 08:48:24 -0400546
Robert Phillipsacc10fa2019-04-01 09:50:20 -0400547 // TODO: It is important to upgrade the drawingmanager to just flushing the
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400548 // portion of the DAG required by 'proxies' in order to restore some of the
Robert Phillipsacc10fa2019-04-01 09:50:20 -0400549 // semantics of this method.
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500550 bool didFlush = this->flush(proxies, access, info, newState);
551 for (GrSurfaceProxy* proxy : proxies) {
552 resolve_and_mipmap(gpu, proxy);
bsalomon6a2b1942016-09-08 11:28:59 -0700553 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400554
555 SkDEBUGCODE(this->validate());
Greg Danielfe159622020-04-10 17:43:51 +0000556
Greg Daniel04283f32020-05-20 13:16:00 -0400557 if (!didFlush || (!direct->priv().caps()->semaphoreSupport() && info.fNumSemaphores)) {
Greg Danielfe159622020-04-10 17:43:51 +0000558 return GrSemaphoresSubmitted::kNo;
559 }
560 return GrSemaphoresSubmitted::kYes;
bsalomon6a2b1942016-09-08 11:28:59 -0700561}
562
Chris Daltonfe199b72017-05-05 11:26:15 -0400563void GrDrawingManager::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
564 fOnFlushCBObjects.push_back(onFlushCBObject);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400565}
566
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500567#if GR_TEST_UTILS
568void GrDrawingManager::testingOnly_removeOnFlushCallbackObject(GrOnFlushCallbackObject* cb) {
569 int n = std::find(fOnFlushCBObjects.begin(), fOnFlushCBObjects.end(), cb) -
570 fOnFlushCBObjects.begin();
571 SkASSERT(n < fOnFlushCBObjects.count());
572 fOnFlushCBObjects.removeShuffle(n);
573}
574#endif
575
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400576void GrDrawingManager::setLastRenderTask(const GrSurfaceProxy* proxy, GrRenderTask* task) {
577#ifdef SK_DEBUG
Robert Phillips07f675d2020-11-16 13:44:01 -0500578 if (auto prior = this->getLastRenderTask(proxy)) {
579 SkASSERT(prior->isClosed() || prior == task);
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400580 }
581#endif
Adlai Holler25df1f72020-06-09 13:08:27 -0400582 uint32_t key = proxy->uniqueID().asUInt();
583 if (task) {
584 fLastRenderTasks.set(key, task);
585 } else if (fLastRenderTasks.find(key)) {
586 fLastRenderTasks.remove(key);
587 }
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400588}
589
590GrRenderTask* GrDrawingManager::getLastRenderTask(const GrSurfaceProxy* proxy) const {
591 auto entry = fLastRenderTasks.find(proxy->uniqueID().asUInt());
592 return entry ? *entry : nullptr;
593}
594
595GrOpsTask* GrDrawingManager::getLastOpsTask(const GrSurfaceProxy* proxy) const {
596 GrRenderTask* task = this->getLastRenderTask(proxy);
597 return task ? task->asOpsTask() : nullptr;
598}
599
600
Chris Dalton6b498102019-08-01 14:14:52 -0600601void GrDrawingManager::moveRenderTasksToDDL(SkDeferredDisplayList* ddl) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400602 SkDEBUGCODE(this->validate());
603
Chris Dalton6b498102019-08-01 14:14:52 -0600604 // no renderTask should receive a new command after this
Adlai Holler33432272020-11-11 13:54:37 -0500605 this->closeAllTasks();
Adlai Holler039f90c2020-11-19 15:20:31 +0000606 fActiveOpsTask = nullptr;
Robert Phillips7a137052018-02-01 11:23:12 -0500607
Robert Phillips07f675d2020-11-16 13:44:01 -0500608 this->sortTasks();
609
Adlai Holler33432272020-11-11 13:54:37 -0500610 fDAG.swap(ddl->fRenderTasks);
611 SkASSERT(fDAG.empty());
Robert Phillips867ce8f2018-06-21 10:28:36 -0400612
Robert Phillips19f466d2020-02-26 10:27:07 -0500613 for (auto& renderTask : ddl->fRenderTasks) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400614 renderTask->disown(this);
Robert Phillips29f38542019-10-16 09:20:25 -0400615 renderTask->prePrepare(fContext);
Robert Phillips7327c9d2019-10-08 16:32:56 -0400616 }
617
Michael Ludwig2c316bd2019-12-19 14:50:44 -0500618 ddl->fArenas = std::move(fContext->priv().detachArenas());
Robert Phillips61fc7992019-10-22 11:58:17 -0400619
Robert Phillipsf6a0b452020-02-18 14:26:46 -0500620 fContext->priv().detachProgramData(&ddl->fProgramData);
Robert Phillips576b6a12019-12-06 13:05:49 -0500621
Robert Phillips774168e2018-05-31 12:43:27 -0400622 if (fPathRendererChain) {
623 if (auto ccpr = fPathRendererChain->getCoverageCountingPathRenderer()) {
624 ddl->fPendingPaths = ccpr->detachPendingPaths();
625 }
626 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400627
628 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500629}
630
Robert Phillips07f675d2020-11-16 13:44:01 -0500631void GrDrawingManager::createDDLTask(sk_sp<const SkDeferredDisplayList> ddl,
Robert Phillipseb54bb52021-01-08 17:20:18 -0500632 sk_sp<GrRenderTargetProxy> newDest,
Robert Phillips88b29612020-11-16 15:15:08 -0500633 SkIPoint offset) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400634 SkDEBUGCODE(this->validate());
635
Adlai Holler039f90c2020-11-19 15:20:31 +0000636 if (fActiveOpsTask) {
637 // This is a temporary fix for the partial-MDB world. In that world we're not
638 // reordering so ops that (in the single opsTask world) would've just glommed onto the
639 // end of the single opsTask but referred to a far earlier RT need to appear in their
640 // own opsTask.
641 fActiveOpsTask->makeClosed(*fContext->priv().caps());
642 fActiveOpsTask = nullptr;
Robert Phillips38d64b02018-09-04 13:23:26 -0400643 }
644
Robert Phillips07f675d2020-11-16 13:44:01 -0500645 // Propagate the DDL proxy's state information to the replay target.
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400646 if (ddl->priv().targetProxy()->isMSAADirty()) {
Brian Salomon982127b2021-01-21 10:43:35 -0500647 auto nativeRect = GrNativeRect::MakeIRectRelativeTo(
648 ddl->characterization().origin(),
649 ddl->priv().targetProxy()->backingStoreDimensions().height(),
650 ddl->priv().targetProxy()->msaaDirtyRect());
651 newDest->markMSAADirty(nativeRect);
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400652 }
653 GrTextureProxy* newTextureProxy = newDest->asTextureProxy();
Brian Salomon8c82a872020-07-21 12:09:58 -0400654 if (newTextureProxy && GrMipmapped::kYes == newTextureProxy->mipmapped()) {
655 newTextureProxy->markMipmapsDirty();
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400656 }
657
Robert Phillips62000362018-02-01 09:10:04 -0500658 // Here we jam the proxy that backs the current replay SkSurface into the LazyProxyData.
Robert Phillips07f675d2020-11-16 13:44:01 -0500659 // The lazy proxy that references it (in the DDL opsTasks) will then steal its GrTexture.
Robert Phillipseb54bb52021-01-08 17:20:18 -0500660 ddl->fLazyProxyData->fReplayDest = newDest.get();
Robert Phillips774168e2018-05-31 12:43:27 -0400661
662 if (ddl->fPendingPaths.size()) {
663 GrCoverageCountingPathRenderer* ccpr = this->getCoverageCountingPathRenderer();
664
665 ccpr->mergePendingPaths(ddl->fPendingPaths);
666 }
Robert Phillips22310d62018-09-05 11:07:21 -0400667
Robert Phillips07f675d2020-11-16 13:44:01 -0500668 // Add a task to handle drawing and lifetime management of the DDL.
669 SkDEBUGCODE(auto ddlTask =) this->appendTask(sk_make_sp<GrDDLTask>(this,
Robert Phillipseb54bb52021-01-08 17:20:18 -0500670 std::move(newDest),
Robert Phillips88b29612020-11-16 15:15:08 -0500671 std::move(ddl),
672 offset));
Robert Phillips07f675d2020-11-16 13:44:01 -0500673 SkASSERT(ddlTask->isClosed());
Adlai Holler7580ad42020-06-24 13:45:25 -0400674
Robert Phillips38d64b02018-09-04 13:23:26 -0400675 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500676}
677
Robert Phillips38d64b02018-09-04 13:23:26 -0400678#ifdef SK_DEBUG
679void GrDrawingManager::validate() const {
Adlai Holler4ef108e2020-12-30 16:21:13 +0000680 if (fActiveOpsTask) {
681 SkASSERT(!fDAG.empty());
682 SkASSERT(!fActiveOpsTask->isClosed());
683 SkASSERT(fActiveOpsTask == fDAG.back().get());
684 }
Adlai Holler039f90c2020-11-19 15:20:31 +0000685
Adlai Holler4ef108e2020-12-30 16:21:13 +0000686 for (int i = 0; i < fDAG.count(); ++i) {
687 if (fActiveOpsTask != fDAG[i].get()) {
688 // The resolveTask associated with the activeTask remains open for as long as the
689 // activeTask does.
690 bool isActiveResolveTask =
691 fActiveOpsTask && fActiveOpsTask->fTextureResolveTask == fDAG[i].get();
692 SkASSERT(isActiveResolveTask || fDAG[i]->isClosed());
Adlai Holler76893192020-12-22 19:41:19 +0000693 }
Adlai Holler4ef108e2020-12-30 16:21:13 +0000694 }
Adlai Holler76893192020-12-22 19:41:19 +0000695
Adlai Holler4ef108e2020-12-30 16:21:13 +0000696 if (!fDAG.empty() && !fDAG.back()->isClosed()) {
697 SkASSERT(fActiveOpsTask == fDAG.back().get());
Robert Phillips38d64b02018-09-04 13:23:26 -0400698 }
699}
700#endif
701
Adlai Holler4ef108e2020-12-30 16:21:13 +0000702void GrDrawingManager::closeActiveOpsTask() {
703 if (fActiveOpsTask) {
704 // This is a temporary fix for the partial-MDB world. In that world we're not
Adlai Holler039f90c2020-11-19 15:20:31 +0000705 // reordering so ops that (in the single opsTask world) would've just glommed onto the
Greg Danielf41b2bd2019-08-22 16:19:24 -0400706 // end of the single opsTask but referred to a far earlier RT need to appear in their
707 // own opsTask.
Adlai Holler039f90c2020-11-19 15:20:31 +0000708 fActiveOpsTask->makeClosed(*fContext->priv().caps());
709 fActiveOpsTask = nullptr;
robertphillips3dc6ae52015-10-20 09:54:32 -0700710 }
Chris Dalton5fe99772019-08-06 11:57:39 -0600711}
712
Greg Daniel16f5c652019-10-29 11:26:01 -0400713sk_sp<GrOpsTask> GrDrawingManager::newOpsTask(GrSurfaceProxyView surfaceView,
Robert Phillips19006652020-11-19 14:20:57 -0500714 bool flushTimeOpsTask) {
Chris Dalton5fe99772019-08-06 11:57:39 -0600715 SkDEBUGCODE(this->validate());
716 SkASSERT(fContext);
717
Adlai Holler4ef108e2020-12-30 16:21:13 +0000718 this->closeActiveOpsTask();
robertphillips3dc6ae52015-10-20 09:54:32 -0700719
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400720 sk_sp<GrOpsTask> opsTask(new GrOpsTask(this, fContext->priv().arenas(),
Michael Ludwigd0840ec2019-12-12 09:48:38 -0500721 std::move(surfaceView),
722 fContext->priv().auditTrail()));
Brian Salomon982127b2021-01-21 10:43:35 -0500723 SkASSERT(this->getLastRenderTask(opsTask->target(0)) == opsTask.get());
robertphillips3dc6ae52015-10-20 09:54:32 -0700724
Robert Phillips19006652020-11-19 14:20:57 -0500725 if (flushTimeOpsTask) {
726 fOnFlushRenderTasks.push_back(opsTask);
727 } else {
Adlai Holler33432272020-11-11 13:54:37 -0500728 this->appendTask(opsTask);
Adlai Holler039f90c2020-11-19 15:20:31 +0000729
Adlai Holler4ef108e2020-12-30 16:21:13 +0000730 fActiveOpsTask = opsTask.get();
Robert Phillips941d1442017-06-14 16:37:02 -0400731 }
robertphillips3dc6ae52015-10-20 09:54:32 -0700732
Robert Phillips38d64b02018-09-04 13:23:26 -0400733 SkDEBUGCODE(this->validate());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400734 return opsTask;
robertphillips3dc6ae52015-10-20 09:54:32 -0700735}
736
Adlai Holler039f90c2020-11-19 15:20:31 +0000737GrTextureResolveRenderTask* GrDrawingManager::newTextureResolveRenderTask(const GrCaps& caps) {
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600738 // 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 +0000739 // in sorting and opsTask reduction mode) the render tasks that depend on any proxy's current
Greg Danielf41b2bd2019-08-22 16:19:24 -0400740 // state. This is because those opsTasks can still receive new ops and because if they refer to
Adlai Holler039f90c2020-11-19 15:20:31 +0000741 // the mipmapped version of 'proxy', they will then come to depend on the render task being
Chris Dalton4ece96d2019-08-30 11:26:39 -0600742 // created here.
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600743 //
Adlai Holler039f90c2020-11-19 15:20:31 +0000744 // Add the new textureResolveTask before the fActiveOpsTask (if not in
745 // sorting/opsTask-splitting-reduction mode) because it will depend upon this resolve task.
Chris Dalton3d770272019-08-14 09:24:37 -0600746 // NOTE: Putting it here will also reduce the amount of work required by the topological sort.
Adlai Holler33432272020-11-11 13:54:37 -0500747 GrRenderTask* task = this->insertTaskBeforeLast(sk_make_sp<GrTextureResolveRenderTask>());
748 return static_cast<GrTextureResolveRenderTask*>(task);
Chris Dalton3d770272019-08-14 09:24:37 -0600749}
750
Greg Danielc30f1a92019-09-06 15:28:58 -0400751void GrDrawingManager::newWaitRenderTask(sk_sp<GrSurfaceProxy> proxy,
Greg Daniel301015c2019-11-18 14:06:46 -0500752 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> semaphores,
Greg Danielc30f1a92019-09-06 15:28:58 -0400753 int numSemaphores) {
754 SkDEBUGCODE(this->validate());
755 SkASSERT(fContext);
756
757 const GrCaps& caps = *fContext->priv().caps();
758
Greg Daniel16f5c652019-10-29 11:26:01 -0400759 sk_sp<GrWaitRenderTask> waitTask = sk_make_sp<GrWaitRenderTask>(GrSurfaceProxyView(proxy),
760 std::move(semaphores),
Greg Danielc30f1a92019-09-06 15:28:58 -0400761 numSemaphores);
Greg Danielc30f1a92019-09-06 15:28:58 -0400762
Brian Salomon982127b2021-01-21 10:43:35 -0500763 if (fActiveOpsTask && (fActiveOpsTask->target(0) == proxy.get())) {
Adlai Holler4ef108e2020-12-30 16:21:13 +0000764 SkASSERT(this->getLastRenderTask(proxy.get()) == fActiveOpsTask);
765 this->insertTaskBeforeLast(waitTask);
766 // In this case we keep the current renderTask open but just insert the new waitTask
767 // before it in the list. The waitTask will never need to trigger any resolves or mip
768 // map generation which is the main advantage of going through the proxy version.
769 // Additionally we would've had to temporarily set the wait task as the lastRenderTask
770 // on the proxy, add the dependency, and then reset the lastRenderTask to
771 // fActiveOpsTask. Additionally we make the waitTask depend on all of fActiveOpsTask
772 // dependencies so that we don't unnecessarily reorder the waitTask before them.
773 // Note: Any previous Ops already in fActiveOpsTask will get blocked by the wait
774 // semaphore even though they don't need to be for correctness.
775
776 // Make sure we add the dependencies of fActiveOpsTask to waitTask first or else we'll
777 // get a circular self dependency of waitTask on waitTask.
778 waitTask->addDependenciesFromOtherTask(fActiveOpsTask);
779 fActiveOpsTask->addDependency(waitTask.get());
Adlai Holler76893192020-12-22 19:41:19 +0000780 } else {
Adlai Holler4ef108e2020-12-30 16:21:13 +0000781 // In this case we just close the previous RenderTask and start and append the waitTask
782 // to the DAG. Since it is the last task now we call setLastRenderTask on the proxy. If
783 // there is a lastTask on the proxy we make waitTask depend on that task. This
784 // dependency isn't strictly needed but it does keep the DAG from reordering the
785 // waitTask earlier and blocking more tasks.
786 if (GrRenderTask* lastTask = this->getLastRenderTask(proxy.get())) {
787 waitTask->addDependency(lastTask);
Adlai Holler76893192020-12-22 19:41:19 +0000788 }
Adlai Holler4ef108e2020-12-30 16:21:13 +0000789 this->setLastRenderTask(proxy.get(), waitTask.get());
790 this->closeActiveOpsTask();
791 this->appendTask(waitTask);
Greg Danielc30f1a92019-09-06 15:28:58 -0400792 }
793 waitTask->makeClosed(caps);
794
795 SkDEBUGCODE(this->validate());
796}
797
Greg Danielbbfec9d2019-08-20 10:56:51 -0400798void GrDrawingManager::newTransferFromRenderTask(sk_sp<GrSurfaceProxy> srcProxy,
799 const SkIRect& srcRect,
800 GrColorType surfaceColorType,
801 GrColorType dstColorType,
802 sk_sp<GrGpuBuffer> dstBuffer,
803 size_t dstOffset) {
804 SkDEBUGCODE(this->validate());
805 SkASSERT(fContext);
Adlai Holler4ef108e2020-12-30 16:21:13 +0000806 this->closeActiveOpsTask();
Greg Danielbbfec9d2019-08-20 10:56:51 -0400807
Adlai Holler33432272020-11-11 13:54:37 -0500808 GrRenderTask* task = this->appendTask(sk_make_sp<GrTransferFromRenderTask>(
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400809 srcProxy, srcRect, surfaceColorType, dstColorType,
810 std::move(dstBuffer), dstOffset));
Greg Danielbbfec9d2019-08-20 10:56:51 -0400811
812 const GrCaps& caps = *fContext->priv().caps();
813
Brian Salomon7e67dca2020-07-21 09:27:25 -0400814 // We always say GrMipmapped::kNo here since we are always just copying from the base layer. We
Greg Danielbbfec9d2019-08-20 10:56:51 -0400815 // don't need to make sure the whole mip map chain is valid.
Brian Salomon7e67dca2020-07-21 09:27:25 -0400816 task->addDependency(this, srcProxy.get(), GrMipmapped::kNo,
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400817 GrTextureResolveManager(this), caps);
Greg Danielbbfec9d2019-08-20 10:56:51 -0400818 task->makeClosed(caps);
819
Greg Danielbbfec9d2019-08-20 10:56:51 -0400820 // We have closed the previous active oplist but since a new oplist isn't being added there
821 // shouldn't be an active one.
Adlai Holler039f90c2020-11-19 15:20:31 +0000822 SkASSERT(!fActiveOpsTask);
Greg Danielbbfec9d2019-08-20 10:56:51 -0400823 SkDEBUGCODE(this->validate());
824}
825
Brian Salomon982127b2021-01-21 10:43:35 -0500826bool GrDrawingManager::newCopyRenderTask(sk_sp<GrSurfaceProxy> src,
827 SkIRect srcRect,
828 sk_sp<GrSurfaceProxy> dst,
Brian Salomon0f9f8002021-01-22 16:30:50 -0500829 SkIPoint dstPoint,
830 GrSurfaceOrigin origin) {
Greg Daniele227fe42019-08-21 13:52:24 -0400831 SkDEBUGCODE(this->validate());
832 SkASSERT(fContext);
Greg Daniele227fe42019-08-21 13:52:24 -0400833
Brian Salomon56c78f42021-02-03 16:56:55 -0500834 // It'd be nicer to check this in GrCopyRenderTask::Make. This gets complicated because of
835 // "active ops task" tracking. dst will be the target of our copy task but it might also be the
836 // target of the active ops task. We currently require the active ops task to be closed before
837 // making a new task that targets the same proxy. However, if we first close the active ops
838 // task, then fail to make a copy task, the next active ops task may target the same proxy. This
839 // will trip an assert related to unnecessary ops task splitting.
840 if (src->framebufferOnly()) {
841 return false;
842 }
843
Brian Salomon507c5652021-02-03 20:32:06 +0000844 this->closeActiveOpsTask();
845
846 GrRenderTask* task = this->appendTask(GrCopyRenderTask::Make(this,
847 src,
848 srcRect,
849 std::move(dst),
850 dstPoint,
851 origin));
Greg Daniele227fe42019-08-21 13:52:24 -0400852 if (!task) {
853 return false;
854 }
855
Brian Salomon0f9f8002021-01-22 16:30:50 -0500856 const GrCaps& caps = *fContext->priv().caps();
Brian Salomon7e67dca2020-07-21 09:27:25 -0400857 // We always say GrMipmapped::kNo here since we are always just copying from the base layer to
Greg Daniele227fe42019-08-21 13:52:24 -0400858 // another base layer. We don't need to make sure the whole mip map chain is valid.
Brian Salomon507c5652021-02-03 20:32:06 +0000859 task->addDependency(this, src.get(), GrMipmapped::kNo, GrTextureResolveManager(this), caps);
860 task->makeClosed(caps);
Greg Daniele227fe42019-08-21 13:52:24 -0400861
Greg Daniele227fe42019-08-21 13:52:24 -0400862 // We have closed the previous active oplist but since a new oplist isn't being added there
863 // shouldn't be an active one.
Adlai Holler039f90c2020-11-19 15:20:31 +0000864 SkASSERT(!fActiveOpsTask);
Greg Daniele227fe42019-08-21 13:52:24 -0400865 SkDEBUGCODE(this->validate());
866 return true;
867}
868
Brian Salomonbe1084b2021-01-26 13:29:30 -0500869bool GrDrawingManager::newWritePixelsTask(sk_sp<GrSurfaceProxy> dst,
870 SkIRect rect,
871 GrColorType srcColorType,
872 GrColorType dstColorType,
873 const GrMipLevel levels[],
874 int levelCount,
875 sk_sp<SkData> owner) {
876 SkDEBUGCODE(this->validate());
877 SkASSERT(fContext);
878
879 this->closeActiveOpsTask();
880 const GrCaps& caps = *fContext->priv().caps();
881
882 // On platforms that prefer flushes over VRAM use (i.e., ANGLE) we're better off forcing a
883 // complete flush here.
884 if (!caps.preferVRAMUseOverFlushes()) {
885 this->flushSurfaces(SkSpan<GrSurfaceProxy*>{},
886 SkSurface::BackendSurfaceAccess::kNoAccess,
887 GrFlushInfo{},
888 nullptr);
889 }
890
891 GrRenderTask* task = this->appendTask(GrWritePixelsTask::Make(this,
892 std::move(dst),
893 rect,
894 srcColorType,
895 dstColorType,
896 levels,
897 levelCount,
898 std::move(owner)));
899 if (!task) {
900 return false;
901 }
902
903 task->makeClosed(caps);
904
905 // We have closed the previous active oplist but since a new oplist isn't being added there
906 // shouldn't be an active one.
907 SkASSERT(!fActiveOpsTask);
908 SkDEBUGCODE(this->validate());
909 return true;
910}
911
robertphillips68737822015-10-29 12:12:21 -0700912/*
913 * This method finds a path renderer that can draw the specified path on
914 * the provided target.
915 * Due to its expense, the software path renderer has split out so it can
916 * can be individually allowed/disallowed via the "allowSW" boolean.
917 */
918GrPathRenderer* GrDrawingManager::getPathRenderer(const GrPathRenderer::CanDrawPathArgs& args,
919 bool allowSW,
920 GrPathRendererChain::DrawType drawType,
921 GrPathRenderer::StencilSupport* stencilSupport) {
922
923 if (!fPathRendererChain) {
John Stilesfbd050b2020-08-03 13:21:46 -0400924 fPathRendererChain =
925 std::make_unique<GrPathRendererChain>(fContext, fOptionsForPathRendererChain);
robertphillips68737822015-10-29 12:12:21 -0700926 }
927
928 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(args, drawType, stencilSupport);
929 if (!pr && allowSW) {
Brian Salomone7df0bb2018-05-07 14:44:57 -0400930 auto swPR = this->getSoftwarePathRenderer();
931 if (GrPathRenderer::CanDrawPath::kNo != swPR->canDrawPath(args)) {
932 pr = swPR;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500933 }
robertphillips68737822015-10-29 12:12:21 -0700934 }
935
Robert Phillipsd81379d2020-04-21 10:39:02 -0400936#if GR_PATH_RENDERER_SPEW
937 if (pr) {
938 SkDebugf("getPathRenderer: %s\n", pr->name());
939 }
940#endif
941
robertphillips68737822015-10-29 12:12:21 -0700942 return pr;
943}
944
Brian Salomone7df0bb2018-05-07 14:44:57 -0400945GrPathRenderer* GrDrawingManager::getSoftwarePathRenderer() {
946 if (!fSoftwarePathRenderer) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400947 fSoftwarePathRenderer.reset(
Robert Phillips9da87e02019-02-04 13:26:26 -0500948 new GrSoftwarePathRenderer(fContext->priv().proxyProvider(),
Ben Wagner9ec70c62018-07-12 13:30:47 -0400949 fOptionsForPathRendererChain.fAllowPathMaskCaching));
Brian Salomone7df0bb2018-05-07 14:44:57 -0400950 }
Ben Wagner9ec70c62018-07-12 13:30:47 -0400951 return fSoftwarePathRenderer.get();
Brian Salomone7df0bb2018-05-07 14:44:57 -0400952}
953
Chris Daltonfddb6c02017-11-04 15:22:22 -0600954GrCoverageCountingPathRenderer* GrDrawingManager::getCoverageCountingPathRenderer() {
955 if (!fPathRendererChain) {
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500956 fPathRendererChain = std::make_unique<GrPathRendererChain>(fContext,
957 fOptionsForPathRendererChain);
Chris Daltonfddb6c02017-11-04 15:22:22 -0600958 }
959 return fPathRendererChain->getCoverageCountingPathRenderer();
960}
961
Chris Daltone6ae4762021-02-05 14:56:21 -0700962GrPathRenderer* GrDrawingManager::getTessellationPathRenderer() {
963 if (!fPathRendererChain) {
964 fPathRendererChain = std::make_unique<GrPathRendererChain>(fContext,
965 fOptionsForPathRendererChain);
966 }
967 return fPathRendererChain->getTessellationPathRenderer();
968}
969
Brian Salomon653f42f2018-07-10 10:07:31 -0400970void GrDrawingManager::flushIfNecessary() {
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400971 auto direct = fContext->asDirectContext();
Robert Phillips6a6de562019-02-15 15:19:15 -0500972 if (!direct) {
973 return;
974 }
975
976 auto resourceCache = direct->priv().getResourceCache();
Brian Salomon653f42f2018-07-10 10:07:31 -0400977 if (resourceCache && resourceCache->requestsFlush()) {
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500978 if (this->flush({}, SkSurface::BackendSurfaceAccess::kNoAccess, GrFlushInfo(), nullptr)) {
Greg Danielfe159622020-04-10 17:43:51 +0000979 this->submitToGpu(false);
980 }
Brian Salomon57d2beab2018-09-10 09:35:41 -0400981 resourceCache->purgeAsNeeded();
Brian Salomon653f42f2018-07-10 10:07:31 -0400982 }
983}