blob: 064877124a04c82bba7da1d623c5959420243c96 [file] [log] [blame]
robertphillips3dc6ae52015-10-20 09:54:32 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/GrDrawingManager.h"
Robert Phillips69893702019-02-22 11:16:30 -05009
Adlai Hollerc2bfcff2020-11-06 15:39:36 -050010#include <algorithm>
John Stilesfbd050b2020-08-03 13:21:46 -040011#include <memory>
12
Robert Phillips4d5594d2020-02-21 14:24:40 -050013#include "include/core/SkDeferredDisplayList.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/gpu/GrBackendSemaphore.h"
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040015#include "include/gpu/GrDirectContext.h"
16#include "include/gpu/GrRecordingContext.h"
Robert Phillips7eb0a5f2020-06-09 14:15:09 -040017#include "src/core/SkDeferredDisplayListPriv.h"
Adlai Holler78036082021-01-07 11:41:33 -050018#include "src/core/SkTInternalLList.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040019#include "src/gpu/GrAuditTrail.h"
Brian Salomon9241a6d2019-10-03 13:26:54 -040020#include "src/gpu/GrClientMappedBufferManager.h"
Greg Daniele227fe42019-08-21 13:52:24 -040021#include "src/gpu/GrCopyRenderTask.h"
Robert Phillips07f675d2020-11-16 13:44:01 -050022#include "src/gpu/GrDDLTask.h"
Adlai Hollera0693042020-10-14 11:23:11 -040023#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/GrGpu.h"
25#include "src/gpu/GrMemoryPool.h"
26#include "src/gpu/GrOnFlushResourceProvider.h"
27#include "src/gpu/GrRecordingContextPriv.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040028#include "src/gpu/GrRenderTargetProxy.h"
Chris Dalton6b498102019-08-01 14:14:52 -060029#include "src/gpu/GrRenderTask.h"
Adlai Holler08f53112021-01-20 17:44:15 -050030#include "src/gpu/GrRenderTaskCluster.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "src/gpu/GrResourceAllocator.h"
32#include "src/gpu/GrResourceProvider.h"
33#include "src/gpu/GrSoftwarePathRenderer.h"
Greg Daniel46e366a2019-12-16 14:38:36 -050034#include "src/gpu/GrSurfaceContext.h"
Brian Salomoneebe7352020-12-09 16:37:04 -050035#include "src/gpu/GrSurfaceDrawContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050036#include "src/gpu/GrSurfaceProxyPriv.h"
Robert Phillipsfbbc3bb2020-11-16 14:58:56 -050037#include "src/gpu/GrTTopoSort.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000038#include "src/gpu/GrTexture.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040039#include "src/gpu/GrTextureProxy.h"
Adlai Holler9e2c50e2021-02-09 14:41:52 -050040#include "src/gpu/GrTextureProxyPriv.h"
Chris Dalton3d770272019-08-14 09:24:37 -060041#include "src/gpu/GrTextureResolveRenderTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050042#include "src/gpu/GrTracing.h"
Greg Danielbbfec9d2019-08-20 10:56:51 -040043#include "src/gpu/GrTransferFromRenderTask.h"
Greg Danielc30f1a92019-09-06 15:28:58 -040044#include "src/gpu/GrWaitRenderTask.h"
Brian Salomonbe1084b2021-01-26 13:29:30 -050045#include "src/gpu/GrWritePixelsRenderTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050046#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
Herb Derbya08bde62020-06-12 15:46:06 -040047#include "src/gpu/text/GrSDFTOptions.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050048#include "src/image/SkSurface_Gpu.h"
robertphillips498d7ac2015-10-30 10:11:30 -070049
Robert Phillips22310d62018-09-05 11:07:21 -040050///////////////////////////////////////////////////////////////////////////////////////////////////
Robert Phillips69893702019-02-22 11:16:30 -050051GrDrawingManager::GrDrawingManager(GrRecordingContext* context,
Robert Phillips22310d62018-09-05 11:07:21 -040052 const GrPathRendererChain::Options& optionsForPathRendererChain,
Greg Danielf41b2bd2019-08-22 16:19:24 -040053 bool reduceOpsTaskSplitting)
Robert Phillips22310d62018-09-05 11:07:21 -040054 : fContext(context)
55 , fOptionsForPathRendererChain(optionsForPathRendererChain)
Robert Phillips22310d62018-09-05 11:07:21 -040056 , fPathRendererChain(nullptr)
57 , fSoftwarePathRenderer(nullptr)
Robert Phillips6db27c22019-05-01 10:43:56 -040058 , fFlushing(false)
Herb Derby082232b2020-06-10 15:08:18 -040059 , fReduceOpsTaskSplitting(reduceOpsTaskSplitting) { }
Robert Phillips22310d62018-09-05 11:07:21 -040060
robertphillips3dc6ae52015-10-20 09:54:32 -070061GrDrawingManager::~GrDrawingManager() {
Adlai Holler33432272020-11-11 13:54:37 -050062 this->closeAllTasks();
63 this->removeRenderTasks(0, fDAG.count());
robertphillips3dc6ae52015-10-20 09:54:32 -070064}
65
Robert Phillipsa9162df2019-02-11 14:12:03 -050066bool GrDrawingManager::wasAbandoned() const {
Robert Phillips9eb00022020-06-30 15:30:12 -040067 return fContext->abandoned();
robertphillips3dc6ae52015-10-20 09:54:32 -070068}
69
robertphillips68737822015-10-29 12:12:21 -070070void GrDrawingManager::freeGpuResources() {
Jim Van Verth106b5c42017-09-26 12:45:29 -040071 for (int i = fOnFlushCBObjects.count() - 1; i >= 0; --i) {
72 if (!fOnFlushCBObjects[i]->retainOnFreeGpuResources()) {
73 // it's safe to just do this because we're iterating in reverse
74 fOnFlushCBObjects.removeShuffle(i);
75 }
76 }
77
robertphillips68737822015-10-29 12:12:21 -070078 // a path renderer may be holding onto resources
robertphillips13391dd2015-10-30 05:15:11 -070079 fPathRendererChain = nullptr;
Ben Wagner9ec70c62018-07-12 13:30:47 -040080 fSoftwarePathRenderer = nullptr;
Robert Phillipse3302df2017-04-24 07:31:02 -040081}
82
Adlai Hollerc2bfcff2020-11-06 15:39:36 -050083// MDB TODO: make use of the 'proxies' parameter.
Greg Daniel9efe3862020-06-11 11:51:06 -040084bool GrDrawingManager::flush(
Adlai Hollerc2bfcff2020-11-06 15:39:36 -050085 SkSpan<GrSurfaceProxy*> proxies,
Greg Daniel9efe3862020-06-11 11:51:06 -040086 SkSurface::BackendSurfaceAccess access,
87 const GrFlushInfo& info,
88 const GrBackendSurfaceMutableState* newState) {
Brian Salomon57d2beab2018-09-10 09:35:41 -040089 GR_CREATE_TRACE_MARKER_CONTEXT("GrDrawingManager", "flush", fContext);
Brian Salomondcbb9d92017-07-19 10:53:20 -040090
robertphillips7761d612016-05-16 09:14:53 -070091 if (fFlushing || this->wasAbandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -040092 if (info.fSubmittedProc) {
93 info.fSubmittedProc(info.fSubmittedContext, false);
94 }
Greg Daniele6bfb7d2019-04-17 15:26:11 -040095 if (info.fFinishedProc) {
96 info.fFinishedProc(info.fFinishedContext);
Greg Daniela3aa75a2019-04-12 14:24:55 -040097 }
Greg Danielfe159622020-04-10 17:43:51 +000098 return false;
joshualittb8918c42015-12-18 09:59:46 -080099 }
Robert Phillips602df412019-04-08 11:10:39 -0400100
Robert Phillips38d64b02018-09-04 13:23:26 -0400101 SkDEBUGCODE(this->validate());
102
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500103 // As of now we only short-circuit if we got an explicit list of surfaces to flush.
104 if (!proxies.empty() && !info.fNumSemaphores && !info.fFinishedProc &&
Greg Danielce9f0162020-06-30 13:42:46 -0400105 access == SkSurface::BackendSurfaceAccess::kNoAccess && !newState) {
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500106 bool allUnused = std::all_of(proxies.begin(), proxies.end(), [&](GrSurfaceProxy* proxy) {
Adlai Holler33432272020-11-11 13:54:37 -0500107 bool used = std::any_of(fDAG.begin(), fDAG.end(), [&](auto& task) {
108 return task && task->isUsed(proxy);
109 });
Robert Phillips58a8ccc2021-01-11 09:07:55 -0500110 return !used;
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500111 });
112 if (allUnused) {
Greg Daniel55822f12020-05-26 11:26:45 -0400113 if (info.fSubmittedProc) {
114 info.fSubmittedProc(info.fSubmittedContext, true);
115 }
Greg Danielfe159622020-04-10 17:43:51 +0000116 return false;
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400117 }
Robert Phillips9313aa72019-04-09 18:41:27 -0400118 }
119
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400120 auto direct = fContext->asDirectContext();
Adlai Holleraee25fd2020-11-06 12:34:56 -0500121 SkASSERT(direct);
Brian Salomon9241a6d2019-10-03 13:26:54 -0400122 direct->priv().clientMappedBufferManager()->process();
Robert Phillips6a6de562019-02-15 15:19:15 -0500123
124 GrGpu* gpu = direct->priv().getGpu();
Greg Daniel55822f12020-05-26 11:26:45 -0400125 // We have a non abandoned and direct GrContext. It must have a GrGpu.
126 SkASSERT(gpu);
Greg Daniela3aa75a2019-04-12 14:24:55 -0400127
joshualittb8918c42015-12-18 09:59:46 -0800128 fFlushing = true;
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400129
Robert Phillips6a6de562019-02-15 15:19:15 -0500130 auto resourceProvider = direct->priv().resourceProvider();
131 auto resourceCache = direct->priv().getResourceCache();
132
Chris Dalton6b498102019-08-01 14:14:52 -0600133 // Semi-usually the GrRenderTasks are already closed at this point, but sometimes Ganesh needs
Greg Danielf41b2bd2019-08-22 16:19:24 -0400134 // to flush mid-draw. In that case, the SkGpuDevice's opsTasks won't be closed but need to be
135 // flushed anyway. Closing such opsTasks here will mean new ones will be created to replace them
Chris Dalton6b498102019-08-01 14:14:52 -0600136 // if the SkGpuDevice(s) write to them again.
Adlai Holler33432272020-11-11 13:54:37 -0500137 this->closeAllTasks();
Adlai Holler039f90c2020-11-19 15:20:31 +0000138 fActiveOpsTask = nullptr;
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400139
Adlai Holler33432272020-11-11 13:54:37 -0500140 this->sortTasks();
Adlai Holler78036082021-01-07 11:41:33 -0500141
142 if (fReduceOpsTaskSplitting) {
143 this->reorderTasks();
144 }
145
Brian Salomon601ac802019-02-07 13:37:16 -0500146 if (!fCpuBufferCache) {
147 // We cache more buffers when the backend is using client side arrays. Otherwise, we
148 // expect each pool will use a CPU buffer as a staging buffer before uploading to a GPU
149 // buffer object. Each pool only requires one staging buffer at a time.
150 int maxCachedBuffers = fContext->priv().caps()->preferClientSideDynamicBuffers() ? 2 : 6;
151 fCpuBufferCache = GrBufferAllocPool::CpuBufferCache::Make(maxCachedBuffers);
Brian Salomon58f153c2018-10-18 21:51:15 -0400152 }
Robert Phillipsa4c93ac2017-05-18 11:40:04 -0400153
Robert Phillipse5f73282019-06-18 17:15:04 -0400154 GrOpFlushState flushState(gpu, resourceProvider, &fTokenTracker, fCpuBufferCache);
Robert Phillips40a29d72018-01-18 12:59:22 -0500155
Chris Daltonfe199b72017-05-05 11:26:15 -0400156 GrOnFlushResourceProvider onFlushProvider(this);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400157
Chris Dalton12658942017-10-05 19:45:25 -0600158 // Prepare any onFlush op lists (e.g. atlases).
Chris Daltonfe199b72017-05-05 11:26:15 -0400159 if (!fOnFlushCBObjects.empty()) {
Robert Phillips07f675d2020-11-16 13:44:01 -0500160 fFlushingRenderTaskIDs.reserve_back(fDAG.count());
161 for (const auto& task : fDAG) {
162 if (task) {
163 task->gatherIDs(&fFlushingRenderTaskIDs);
Adlai Holler33432272020-11-11 13:54:37 -0500164 }
165 }
Robert Phillips22310d62018-09-05 11:07:21 -0400166
Chris Daltonfe199b72017-05-05 11:26:15 -0400167 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
Adlai Holler9902cff2020-11-11 08:51:25 -0500168 onFlushCBObject->preFlush(&onFlushProvider, fFlushingRenderTaskIDs);
Chris Daltonc4b47352019-08-23 10:10:36 -0600169 }
170 for (const auto& onFlushRenderTask : fOnFlushRenderTasks) {
171 onFlushRenderTask->makeClosed(*fContext->priv().caps());
Chris Dalton706a6ff2017-11-29 22:01:06 -0700172#ifdef SK_DEBUG
Chris Daltonc4b47352019-08-23 10:10:36 -0600173 // OnFlush callbacks are invoked during flush, and are therefore expected to handle
Adlai Holler9e2c50e2021-02-09 14:41:52 -0500174 // resource allocation & usage on their own. (No deferred or lazy proxies!)
Chris Daltonc4b47352019-08-23 10:10:36 -0600175 onFlushRenderTask->visitTargetAndSrcProxies_debugOnly(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400176 [](GrSurfaceProxy* p, GrMipmapped mipMapped) {
Adlai Holler9e2c50e2021-02-09 14:41:52 -0500177 SkASSERT(!p->asTextureProxy() || !p->asTextureProxy()->texPriv().isDeferred());
Brian Salomonbeb7f522019-08-30 16:19:42 -0400178 SkASSERT(!p->isLazy());
Chris Dalton4ece96d2019-08-30 11:26:39 -0600179 if (p->requiresManualMSAAResolve()) {
180 // The onFlush callback is responsible for ensuring MSAA gets resolved.
181 SkASSERT(p->asRenderTargetProxy() && !p->asRenderTargetProxy()->isMSAADirty());
182 }
Brian Salomon7e67dca2020-07-21 09:27:25 -0400183 if (GrMipmapped::kYes == mipMapped) {
Chris Daltonc4b47352019-08-23 10:10:36 -0600184 // The onFlush callback is responsible for regenerating mips if needed.
Brian Salomon8c82a872020-07-21 12:09:58 -0400185 SkASSERT(p->asTextureProxy() && !p->asTextureProxy()->mipmapsAreDirty());
Chris Daltonc4b47352019-08-23 10:10:36 -0600186 }
187 });
Chris Dalton706a6ff2017-11-29 22:01:06 -0700188#endif
Chris Daltonc4b47352019-08-23 10:10:36 -0600189 onFlushRenderTask->prepare(&flushState);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400190 }
191 }
192
robertphillipsa13e2022015-11-11 12:01:09 -0800193#if 0
Brian Salomon09d994e2016-12-21 11:14:46 -0500194 // Enable this to print out verbose GrOp information
Adlai Hollerabdfd392020-10-19 09:00:36 -0400195 SkDEBUGCODE(SkDebugf("onFlush renderTasks (%d):\n", fOnFlushRenderTasks.count()));
Chris Daltonc4b47352019-08-23 10:10:36 -0600196 for (const auto& onFlushRenderTask : fOnFlushRenderTasks) {
Adlai Hollerabdfd392020-10-19 09:00:36 -0400197 SkDEBUGCODE(onFlushRenderTask->dump(/* printDependencies */ true);)
Chris Daltonc4b47352019-08-23 10:10:36 -0600198 }
Adlai Holler33432272020-11-11 13:54:37 -0500199 SkDEBUGCODE(SkDebugf("Normal renderTasks (%d):\n", fDAG.count()));
200 for (const auto& task : fDAG) {
201 SkDEBUGCODE(task->dump(/* printDependencies */ true);)
robertphillips3dc6ae52015-10-20 09:54:32 -0700202 }
robertphillipsa13e2022015-11-11 12:01:09 -0800203#endif
204
Robert Phillipseafd48a2017-11-16 07:52:08 -0500205 int startIndex, stopIndex;
206 bool flushed = false;
207
Robert Phillipsf8e25022017-11-08 15:24:31 -0500208 {
Adlai Holler33432272020-11-11 13:54:37 -0500209 GrResourceAllocator alloc(resourceProvider SkDEBUGCODE(, fDAG.count()));
210 for (int i = 0; i < fDAG.count(); ++i) {
211 if (fDAG[i]) {
212 fDAG[i]->gatherProxyIntervals(&alloc);
Robert Phillips22310d62018-09-05 11:07:21 -0400213 }
Greg Danielf41b2bd2019-08-22 16:19:24 -0400214 alloc.markEndOfOpsTask(i);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500215 }
Robert Phillipsc73666f2019-04-24 08:49:48 -0400216 alloc.determineRecyclability();
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400217
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500218 GrResourceAllocator::AssignError error = GrResourceAllocator::AssignError::kNoError;
Chris Dalton6b498102019-08-01 14:14:52 -0600219 int numRenderTasksExecuted = 0;
Adlai Holler6f8fbb52021-02-10 10:09:10 -0500220 if (alloc.assign(&startIndex, &stopIndex, &error)) {
221 // TODO: If this always-execute-everything approach works out, remove the startIndex &
222 // stopIndex from the GrResourceProvider API and simplify.
223 SkASSERT(startIndex == 0 && stopIndex == fDAG.count());
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500224 if (GrResourceAllocator::AssignError::kFailedProxyInstantiation == error) {
225 for (int i = startIndex; i < stopIndex; ++i) {
Adlai Holler33432272020-11-11 13:54:37 -0500226 GrRenderTask* renderTask = fDAG[i].get();
Chris Dalton6b498102019-08-01 14:14:52 -0600227 if (!renderTask) {
228 continue;
Robert Phillips01a91282018-07-26 08:03:04 -0400229 }
Chris Dalton6b498102019-08-01 14:14:52 -0600230 if (!renderTask->isInstantiated()) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400231 // No need to call the renderTask's handleInternalAllocationFailure
232 // since we will already skip executing the renderTask since it is not
233 // instantiated.
Chris Dalton6b498102019-08-01 14:14:52 -0600234 continue;
Robert Phillipsbbcb7f72018-05-31 11:16:19 -0400235 }
Chris Dalton6b498102019-08-01 14:14:52 -0600236 renderTask->handleInternalAllocationFailure();
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500237 }
Adlai Holler96ead542020-06-26 08:50:14 -0400238 this->removeRenderTasks(startIndex, stopIndex);
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500239 }
Robert Phillips4150eea2018-02-07 17:08:21 -0500240
Chris Dalton6b498102019-08-01 14:14:52 -0600241 if (this->executeRenderTasks(
242 startIndex, stopIndex, &flushState, &numRenderTasksExecuted)) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500243 flushed = true;
244 }
bsalomondc438982016-08-31 11:53:49 -0700245 }
Greg Danielc42b20b2017-10-04 10:34:49 -0400246 }
247
Chris Dalton91ab1552018-04-18 13:24:25 -0600248#ifdef SK_DEBUG
Adlai Holler33432272020-11-11 13:54:37 -0500249 for (const auto& task : fDAG) {
Adlai Holler96ead542020-06-26 08:50:14 -0400250 // All render tasks should have been cleared out by now – we only reset the array below to
251 // reclaim storage.
Adlai Holler33432272020-11-11 13:54:37 -0500252 SkASSERT(!task);
Chris Dalton91ab1552018-04-18 13:24:25 -0600253 }
254#endif
Adlai Holler25df1f72020-06-09 13:08:27 -0400255 fLastRenderTasks.reset();
Robert Phillips22310d62018-09-05 11:07:21 -0400256 fDAG.reset();
robertphillipsa13e2022015-11-11 12:01:09 -0800257
Robert Phillipsc994a932018-06-19 13:09:54 -0400258#ifdef SK_DEBUG
259 // In non-DDL mode this checks that all the flushed ops have been freed from the memory pool.
260 // When we move to partial flushes this assert will no longer be valid.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400261 // In DDL mode this check is somewhat superfluous since the memory for most of the ops/opsTasks
Robert Phillipsc994a932018-06-19 13:09:54 -0400262 // will be stored in the DDL's GrOpMemoryPools.
Herb Derbye32e1ab2020-10-27 10:29:46 -0400263 GrMemoryPool* opMemoryPool = fContext->priv().opMemoryPool();
Robert Phillipsc994a932018-06-19 13:09:54 -0400264 opMemoryPool->isEmpty();
265#endif
266
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500267 gpu->executeFlushInfo(proxies, access, info, newState);
robertphillipsa13e2022015-11-11 12:01:09 -0800268
Brian Salomon57d2beab2018-09-10 09:35:41 -0400269 // Give the cache a chance to purge resources that become purgeable due to flushing.
270 if (flushed) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500271 resourceCache->purgeAsNeeded();
Brian Salomon876a0172019-03-08 11:12:14 -0500272 flushed = false;
bsalomonb77a9072016-09-07 10:02:04 -0700273 }
Chris Daltonfe199b72017-05-05 11:26:15 -0400274 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
Adlai Holler9902cff2020-11-11 08:51:25 -0500275 onFlushCBObject->postFlush(fTokenTracker.nextTokenToFlush(), fFlushingRenderTaskIDs);
Brian Salomon876a0172019-03-08 11:12:14 -0500276 flushed = true;
277 }
278 if (flushed) {
279 resourceCache->purgeAsNeeded();
Chris Daltonfe199b72017-05-05 11:26:15 -0400280 }
Chris Dalton6b498102019-08-01 14:14:52 -0600281 fFlushingRenderTaskIDs.reset();
joshualittb8918c42015-12-18 09:59:46 -0800282 fFlushing = false;
Greg Daniel51316782017-08-02 15:10:09 +0000283
Greg Danielfe159622020-04-10 17:43:51 +0000284 return true;
285}
286
287bool GrDrawingManager::submitToGpu(bool syncToCpu) {
288 if (fFlushing || this->wasAbandoned()) {
289 return false;
290 }
291
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400292 auto direct = fContext->asDirectContext();
Greg Danielfe159622020-04-10 17:43:51 +0000293 if (!direct) {
294 return false; // Can't submit while DDL recording
295 }
296 GrGpu* gpu = direct->priv().getGpu();
297 return gpu->submitToGpu(syncToCpu);
robertphillips3dc6ae52015-10-20 09:54:32 -0700298}
299
Chris Dalton6b498102019-08-01 14:14:52 -0600300bool GrDrawingManager::executeRenderTasks(int startIndex, int stopIndex, GrOpFlushState* flushState,
301 int* numRenderTasksExecuted) {
Adlai Holler33432272020-11-11 13:54:37 -0500302 SkASSERT(startIndex <= stopIndex && stopIndex <= fDAG.count());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500303
Robert Phillips27483912018-04-20 12:43:18 -0400304#if GR_FLUSH_TIME_OP_SPEW
Greg Danielf41b2bd2019-08-22 16:19:24 -0400305 SkDebugf("Flushing opsTask: %d to %d out of [%d, %d]\n",
Adlai Holler33432272020-11-11 13:54:37 -0500306 startIndex, stopIndex, 0, fDAG.count());
Robert Phillips27483912018-04-20 12:43:18 -0400307 for (int i = startIndex; i < stopIndex; ++i) {
Adlai Holler33432272020-11-11 13:54:37 -0500308 if (fDAG[i]) {
Robert Phillips047d5bb2021-01-08 13:39:19 -0500309 SkString label;
310 label.printf("task %d/%d", i, fDAG.count());
311 fDAG[i]->dump(label, {}, true, true);
Robert Phillips1734dd32018-08-21 13:52:09 -0400312 }
Robert Phillips27483912018-04-20 12:43:18 -0400313 }
314#endif
315
Chris Dalton6b498102019-08-01 14:14:52 -0600316 bool anyRenderTasksExecuted = false;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500317
318 for (int i = startIndex; i < stopIndex; ++i) {
Adlai Holler33432272020-11-11 13:54:37 -0500319 GrRenderTask* renderTask = fDAG[i].get();
Greg Daniel15ecdf92019-08-30 15:35:23 -0400320 if (!renderTask || !renderTask->isInstantiated()) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500321 continue;
322 }
323
Adlai Holler9e2c50e2021-02-09 14:41:52 -0500324 SkASSERT(renderTask->deferredProxiesAreInstantiated());
325
Chris Dalton6b498102019-08-01 14:14:52 -0600326 renderTask->prepare(flushState);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500327 }
328
329 // Upload all data to the GPU
330 flushState->preExecuteDraws();
331
Greg Danield2073452018-12-07 11:20:33 -0500332 // For Vulkan, if we have too many oplists to be flushed we end up allocating a lot of resources
333 // for each command buffer associated with the oplists. If this gets too large we can cause the
334 // devices to go OOM. In practice we usually only hit this case in our tests, but to be safe we
335 // put a cap on the number of oplists we will execute before flushing to the GPU to relieve some
336 // memory pressure.
Chris Dalton6b498102019-08-01 14:14:52 -0600337 static constexpr int kMaxRenderTasksBeforeFlush = 100;
Greg Danield2073452018-12-07 11:20:33 -0500338
Chris Daltonc4b47352019-08-23 10:10:36 -0600339 // Execute the onFlush renderTasks first, if any.
340 for (sk_sp<GrRenderTask>& onFlushRenderTask : fOnFlushRenderTasks) {
341 if (!onFlushRenderTask->execute(flushState)) {
342 SkDebugf("WARNING: onFlushRenderTask failed to execute.\n");
Robert Phillipseafd48a2017-11-16 07:52:08 -0500343 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600344 SkASSERT(onFlushRenderTask->unique());
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400345 onFlushRenderTask->disown(this);
Chris Daltonc4b47352019-08-23 10:10:36 -0600346 onFlushRenderTask = nullptr;
Chris Dalton6b498102019-08-01 14:14:52 -0600347 (*numRenderTasksExecuted)++;
348 if (*numRenderTasksExecuted >= kMaxRenderTasksBeforeFlush) {
Greg Danielfe159622020-04-10 17:43:51 +0000349 flushState->gpu()->submitToGpu(false);
Chris Dalton6b498102019-08-01 14:14:52 -0600350 *numRenderTasksExecuted = 0;
Greg Danield2073452018-12-07 11:20:33 -0500351 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500352 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600353 fOnFlushRenderTasks.reset();
Robert Phillipseafd48a2017-11-16 07:52:08 -0500354
355 // Execute the normal op lists.
356 for (int i = startIndex; i < stopIndex; ++i) {
Adlai Holler33432272020-11-11 13:54:37 -0500357 GrRenderTask* renderTask = fDAG[i].get();
Greg Daniel15ecdf92019-08-30 15:35:23 -0400358 if (!renderTask || !renderTask->isInstantiated()) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500359 continue;
360 }
361
Greg Daniel15ecdf92019-08-30 15:35:23 -0400362 if (renderTask->execute(flushState)) {
Chris Dalton6b498102019-08-01 14:14:52 -0600363 anyRenderTasksExecuted = true;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500364 }
Chris Dalton6b498102019-08-01 14:14:52 -0600365 (*numRenderTasksExecuted)++;
366 if (*numRenderTasksExecuted >= kMaxRenderTasksBeforeFlush) {
Greg Danielfe159622020-04-10 17:43:51 +0000367 flushState->gpu()->submitToGpu(false);
Chris Dalton6b498102019-08-01 14:14:52 -0600368 *numRenderTasksExecuted = 0;
Greg Danield2073452018-12-07 11:20:33 -0500369 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500370 }
371
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400372 SkASSERT(!flushState->opsRenderPass());
Robert Phillips40a29d72018-01-18 12:59:22 -0500373 SkASSERT(fTokenTracker.nextDrawToken() == fTokenTracker.nextTokenToFlush());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500374
Chris Dalton6b498102019-08-01 14:14:52 -0600375 // We reset the flush state before the RenderTasks so that the last resources to be freed are
376 // those that are written to in the RenderTasks. This helps to make sure the most recently used
377 // resources are the last to be purged by the resource cache.
Robert Phillipseafd48a2017-11-16 07:52:08 -0500378 flushState->reset();
379
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400380 this->removeRenderTasks(startIndex, stopIndex);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500381
Chris Dalton6b498102019-08-01 14:14:52 -0600382 return anyRenderTasksExecuted;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500383}
384
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400385void GrDrawingManager::removeRenderTasks(int startIndex, int stopIndex) {
386 for (int i = startIndex; i < stopIndex; ++i) {
Adlai Holler33432272020-11-11 13:54:37 -0500387 GrRenderTask* task = fDAG[i].get();
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400388 if (!task) {
389 continue;
390 }
Robert Phillips07f675d2020-11-16 13:44:01 -0500391 if (!task->unique() || task->requiresExplicitCleanup()) {
392 // TODO: Eventually uniqueness should be guaranteed: http://skbug.com/7111.
393 // DDLs, however, will always require an explicit notification for when they
394 // can clean up resources.
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400395 task->endFlush(this);
396 }
397 task->disown(this);
Adlai Holler33432272020-11-11 13:54:37 -0500398
399 // This doesn't cleanup any referring pointers (e.g. dependency pointers in the DAG).
400 // It works right now bc this is only called after the topological sort is complete
401 // (so the dangling pointers aren't used).
402 fDAG[i] = nullptr;
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400403 }
Adlai Holler33432272020-11-11 13:54:37 -0500404}
405
406void GrDrawingManager::sortTasks() {
Robert Phillipsfbbc3bb2020-11-16 14:58:56 -0500407 if (!GrTTopoSort<GrRenderTask, GrRenderTask::TopoSortTraits>(&fDAG)) {
Adlai Holler33432272020-11-11 13:54:37 -0500408 SkDEBUGFAIL("Render task topo sort failed.");
409 return;
410 }
411
412#ifdef SK_DEBUG
413 // This block checks for any unnecessary splits in the opsTasks. If two sequential opsTasks
414 // share the same backing GrSurfaceProxy it means the opsTask was artificially split.
415 if (!fDAG.empty()) {
416 GrOpsTask* prevOpsTask = fDAG[0]->asOpsTask();
417 for (int i = 1; i < fDAG.count(); ++i) {
418 GrOpsTask* curOpsTask = fDAG[i]->asOpsTask();
419
420 if (prevOpsTask && curOpsTask) {
Brian Salomon982127b2021-01-21 10:43:35 -0500421 SkASSERT(prevOpsTask->target(0) != curOpsTask->target(0));
Adlai Holler33432272020-11-11 13:54:37 -0500422 }
423
424 prevOpsTask = curOpsTask;
425 }
426 }
427#endif
428}
429
Adlai Holler78036082021-01-07 11:41:33 -0500430// Reorder the array to match the llist without reffing & unreffing sk_sp's.
Adlai Holler69c57e42021-01-25 15:44:26 +0000431// Both args must contain the same objects.
Adlai Holler78036082021-01-07 11:41:33 -0500432// This is basically a shim because clustering uses LList but the rest of drawmgr uses array.
433template <typename T>
434static void reorder_array_by_llist(const SkTInternalLList<T>& llist, SkTArray<sk_sp<T>>* array) {
435 int i = 0;
436 for (T* t : llist) {
Adlai Holler69c57e42021-01-25 15:44:26 +0000437 // Release the pointer that used to live here so it doesn't get unreffed.
438 [[maybe_unused]] T* old = array->at(i).release();
Adlai Holler78036082021-01-07 11:41:33 -0500439 array->at(i++).reset(t);
440 }
Adlai Holler69c57e42021-01-25 15:44:26 +0000441 SkASSERT(i == array->count());
Adlai Holler78036082021-01-07 11:41:33 -0500442}
443
444void GrDrawingManager::reorderTasks() {
445 SkASSERT(fReduceOpsTaskSplitting);
446 SkTInternalLList<GrRenderTask> llist;
Adlai Holler08f53112021-01-20 17:44:15 -0500447 bool clustered = GrClusterRenderTasks(fDAG, &llist);
Adlai Holler78036082021-01-07 11:41:33 -0500448 if (!clustered) {
449 return;
450 }
451 // TODO: Handle case where proposed order would blow our memory budget.
452 // Such cases are currently pathological, so we could just return here and keep current order.
453 reorder_array_by_llist(llist, &fDAG);
Adlai Holler93439d92021-01-26 09:20:39 -0500454
455 int newCount = 0;
456 for (int i = 0; i < fDAG.count(); i++) {
457 sk_sp<GrRenderTask>& task = fDAG[i];
458 if (auto opsTask = task->asOpsTask()) {
459 size_t remaining = fDAG.size() - i - 1;
460 SkSpan<sk_sp<GrRenderTask>> nextTasks{fDAG.end() - remaining, remaining};
461 int removeCount = opsTask->mergeFrom(nextTasks);
462 for (const auto& removed : nextTasks.first(removeCount)) {
463 removed->disown(this);
464 }
465 i += removeCount;
466 }
467 fDAG[newCount++] = std::move(task);
468 }
469 fDAG.resize_back(newCount);
Adlai Holler78036082021-01-07 11:41:33 -0500470}
471
Adlai Holler33432272020-11-11 13:54:37 -0500472void GrDrawingManager::closeAllTasks() {
473 const GrCaps& caps = *fContext->priv().caps();
474 for (auto& task : fDAG) {
475 if (task) {
476 task->makeClosed(caps);
477 }
478 }
479}
480
481GrRenderTask* GrDrawingManager::insertTaskBeforeLast(sk_sp<GrRenderTask> task) {
482 SkASSERT(!fDAG.empty());
483 if (!task) {
484 return nullptr;
485 }
486 // Release 'fDAG.back()' and grab the raw pointer, in case the SkTArray grows
487 // and reallocates during emplace_back.
488 // TODO: Either use std::vector that can do this for us, or use SkSTArray to get the
489 // perf win.
490 fDAG.emplace_back(fDAG.back().release());
491 return (fDAG[fDAG.count() - 2] = std::move(task)).get();
492}
493
494GrRenderTask* GrDrawingManager::appendTask(sk_sp<GrRenderTask> task) {
495 if (!task) {
496 return nullptr;
497 }
498 return fDAG.push_back(std::move(task)).get();
499}
500
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400501static void resolve_and_mipmap(GrGpu* gpu, GrSurfaceProxy* proxy) {
502 if (!proxy->isInstantiated()) {
503 return;
504 }
505
506 // In the flushSurfaces case, we need to resolve MSAA immediately after flush. This is
507 // because clients expect the flushed surface's backing texture to be fully resolved
508 // upon return.
509 if (proxy->requiresManualMSAAResolve()) {
510 auto* rtProxy = proxy->asRenderTargetProxy();
511 SkASSERT(rtProxy);
512 if (rtProxy->isMSAADirty()) {
513 SkASSERT(rtProxy->peekRenderTarget());
Jim Van Verthbb61fe32020-07-07 16:39:04 -0400514 gpu->resolveRenderTarget(rtProxy->peekRenderTarget(), rtProxy->msaaDirtyRect());
515 gpu->submitToGpu(false);
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400516 rtProxy->markMSAAResolved();
517 }
518 }
519 // If, after a flush, any of the proxies of interest have dirty mipmaps, regenerate them in
520 // case their backend textures are being stolen.
521 // (This special case is exercised by the ReimportImageTextureWithMipLevels test.)
522 // FIXME: It may be more ideal to plumb down a "we're going to steal the backends" flag.
523 if (auto* textureProxy = proxy->asTextureProxy()) {
Brian Salomon8c82a872020-07-21 12:09:58 -0400524 if (textureProxy->mipmapsAreDirty()) {
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400525 SkASSERT(textureProxy->peekTexture());
526 gpu->regenerateMipMapLevels(textureProxy->peekTexture());
Brian Salomon8c82a872020-07-21 12:09:58 -0400527 textureProxy->markMipmapsClean();
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400528 }
529 }
530}
531
Greg Daniel9efe3862020-06-11 11:51:06 -0400532GrSemaphoresSubmitted GrDrawingManager::flushSurfaces(
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500533 SkSpan<GrSurfaceProxy*> proxies,
Greg Daniel9efe3862020-06-11 11:51:06 -0400534 SkSurface::BackendSurfaceAccess access,
535 const GrFlushInfo& info,
536 const GrBackendSurfaceMutableState* newState) {
bsalomon6a2b1942016-09-08 11:28:59 -0700537 if (this->wasAbandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -0400538 if (info.fSubmittedProc) {
539 info.fSubmittedProc(info.fSubmittedContext, false);
540 }
541 if (info.fFinishedProc) {
542 info.fFinishedProc(info.fFinishedContext);
543 }
Greg Daniel51316782017-08-02 15:10:09 +0000544 return GrSemaphoresSubmitted::kNo;
bsalomon6a2b1942016-09-08 11:28:59 -0700545 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400546 SkDEBUGCODE(this->validate());
bsalomon6a2b1942016-09-08 11:28:59 -0700547
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400548 auto direct = fContext->asDirectContext();
Adlai Holleraee25fd2020-11-06 12:34:56 -0500549 SkASSERT(direct);
Robert Phillips6a6de562019-02-15 15:19:15 -0500550 GrGpu* gpu = direct->priv().getGpu();
Greg Daniel55822f12020-05-26 11:26:45 -0400551 // We have a non abandoned and direct GrContext. It must have a GrGpu.
552 SkASSERT(gpu);
Robert Phillips874b5352018-03-16 08:48:24 -0400553
Robert Phillipsacc10fa2019-04-01 09:50:20 -0400554 // TODO: It is important to upgrade the drawingmanager to just flushing the
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400555 // portion of the DAG required by 'proxies' in order to restore some of the
Robert Phillipsacc10fa2019-04-01 09:50:20 -0400556 // semantics of this method.
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500557 bool didFlush = this->flush(proxies, access, info, newState);
558 for (GrSurfaceProxy* proxy : proxies) {
559 resolve_and_mipmap(gpu, proxy);
bsalomon6a2b1942016-09-08 11:28:59 -0700560 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400561
562 SkDEBUGCODE(this->validate());
Greg Danielfe159622020-04-10 17:43:51 +0000563
Greg Daniel04283f32020-05-20 13:16:00 -0400564 if (!didFlush || (!direct->priv().caps()->semaphoreSupport() && info.fNumSemaphores)) {
Greg Danielfe159622020-04-10 17:43:51 +0000565 return GrSemaphoresSubmitted::kNo;
566 }
567 return GrSemaphoresSubmitted::kYes;
bsalomon6a2b1942016-09-08 11:28:59 -0700568}
569
Chris Daltonfe199b72017-05-05 11:26:15 -0400570void GrDrawingManager::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
571 fOnFlushCBObjects.push_back(onFlushCBObject);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400572}
573
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500574#if GR_TEST_UTILS
575void GrDrawingManager::testingOnly_removeOnFlushCallbackObject(GrOnFlushCallbackObject* cb) {
576 int n = std::find(fOnFlushCBObjects.begin(), fOnFlushCBObjects.end(), cb) -
577 fOnFlushCBObjects.begin();
578 SkASSERT(n < fOnFlushCBObjects.count());
579 fOnFlushCBObjects.removeShuffle(n);
580}
581#endif
582
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400583void GrDrawingManager::setLastRenderTask(const GrSurfaceProxy* proxy, GrRenderTask* task) {
584#ifdef SK_DEBUG
Robert Phillips07f675d2020-11-16 13:44:01 -0500585 if (auto prior = this->getLastRenderTask(proxy)) {
586 SkASSERT(prior->isClosed() || prior == task);
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400587 }
588#endif
Adlai Holler25df1f72020-06-09 13:08:27 -0400589 uint32_t key = proxy->uniqueID().asUInt();
590 if (task) {
591 fLastRenderTasks.set(key, task);
592 } else if (fLastRenderTasks.find(key)) {
593 fLastRenderTasks.remove(key);
594 }
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400595}
596
597GrRenderTask* GrDrawingManager::getLastRenderTask(const GrSurfaceProxy* proxy) const {
598 auto entry = fLastRenderTasks.find(proxy->uniqueID().asUInt());
599 return entry ? *entry : nullptr;
600}
601
602GrOpsTask* GrDrawingManager::getLastOpsTask(const GrSurfaceProxy* proxy) const {
603 GrRenderTask* task = this->getLastRenderTask(proxy);
604 return task ? task->asOpsTask() : nullptr;
605}
606
607
Chris Dalton6b498102019-08-01 14:14:52 -0600608void GrDrawingManager::moveRenderTasksToDDL(SkDeferredDisplayList* ddl) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400609 SkDEBUGCODE(this->validate());
610
Chris Dalton6b498102019-08-01 14:14:52 -0600611 // no renderTask should receive a new command after this
Adlai Holler33432272020-11-11 13:54:37 -0500612 this->closeAllTasks();
Adlai Holler039f90c2020-11-19 15:20:31 +0000613 fActiveOpsTask = nullptr;
Robert Phillips7a137052018-02-01 11:23:12 -0500614
Robert Phillips07f675d2020-11-16 13:44:01 -0500615 this->sortTasks();
616
Adlai Holler33432272020-11-11 13:54:37 -0500617 fDAG.swap(ddl->fRenderTasks);
618 SkASSERT(fDAG.empty());
Robert Phillips867ce8f2018-06-21 10:28:36 -0400619
Robert Phillips19f466d2020-02-26 10:27:07 -0500620 for (auto& renderTask : ddl->fRenderTasks) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400621 renderTask->disown(this);
Robert Phillips29f38542019-10-16 09:20:25 -0400622 renderTask->prePrepare(fContext);
Robert Phillips7327c9d2019-10-08 16:32:56 -0400623 }
624
Michael Ludwig2c316bd2019-12-19 14:50:44 -0500625 ddl->fArenas = std::move(fContext->priv().detachArenas());
Robert Phillips61fc7992019-10-22 11:58:17 -0400626
Robert Phillipsf6a0b452020-02-18 14:26:46 -0500627 fContext->priv().detachProgramData(&ddl->fProgramData);
Robert Phillips576b6a12019-12-06 13:05:49 -0500628
Robert Phillips774168e2018-05-31 12:43:27 -0400629 if (fPathRendererChain) {
630 if (auto ccpr = fPathRendererChain->getCoverageCountingPathRenderer()) {
631 ddl->fPendingPaths = ccpr->detachPendingPaths();
632 }
633 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400634
635 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500636}
637
Robert Phillips07f675d2020-11-16 13:44:01 -0500638void GrDrawingManager::createDDLTask(sk_sp<const SkDeferredDisplayList> ddl,
Robert Phillipseb54bb52021-01-08 17:20:18 -0500639 sk_sp<GrRenderTargetProxy> newDest,
Robert Phillips88b29612020-11-16 15:15:08 -0500640 SkIPoint offset) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400641 SkDEBUGCODE(this->validate());
642
Adlai Holler039f90c2020-11-19 15:20:31 +0000643 if (fActiveOpsTask) {
644 // This is a temporary fix for the partial-MDB world. In that world we're not
645 // reordering so ops that (in the single opsTask world) would've just glommed onto the
646 // end of the single opsTask but referred to a far earlier RT need to appear in their
647 // own opsTask.
648 fActiveOpsTask->makeClosed(*fContext->priv().caps());
649 fActiveOpsTask = nullptr;
Robert Phillips38d64b02018-09-04 13:23:26 -0400650 }
651
Robert Phillips07f675d2020-11-16 13:44:01 -0500652 // Propagate the DDL proxy's state information to the replay target.
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400653 if (ddl->priv().targetProxy()->isMSAADirty()) {
Brian Salomon982127b2021-01-21 10:43:35 -0500654 auto nativeRect = GrNativeRect::MakeIRectRelativeTo(
655 ddl->characterization().origin(),
656 ddl->priv().targetProxy()->backingStoreDimensions().height(),
657 ddl->priv().targetProxy()->msaaDirtyRect());
658 newDest->markMSAADirty(nativeRect);
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400659 }
660 GrTextureProxy* newTextureProxy = newDest->asTextureProxy();
Brian Salomon8c82a872020-07-21 12:09:58 -0400661 if (newTextureProxy && GrMipmapped::kYes == newTextureProxy->mipmapped()) {
662 newTextureProxy->markMipmapsDirty();
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400663 }
664
Robert Phillips62000362018-02-01 09:10:04 -0500665 // Here we jam the proxy that backs the current replay SkSurface into the LazyProxyData.
Robert Phillips07f675d2020-11-16 13:44:01 -0500666 // The lazy proxy that references it (in the DDL opsTasks) will then steal its GrTexture.
Robert Phillipseb54bb52021-01-08 17:20:18 -0500667 ddl->fLazyProxyData->fReplayDest = newDest.get();
Robert Phillips774168e2018-05-31 12:43:27 -0400668
669 if (ddl->fPendingPaths.size()) {
670 GrCoverageCountingPathRenderer* ccpr = this->getCoverageCountingPathRenderer();
671
672 ccpr->mergePendingPaths(ddl->fPendingPaths);
673 }
Robert Phillips22310d62018-09-05 11:07:21 -0400674
Robert Phillips07f675d2020-11-16 13:44:01 -0500675 // Add a task to handle drawing and lifetime management of the DDL.
676 SkDEBUGCODE(auto ddlTask =) this->appendTask(sk_make_sp<GrDDLTask>(this,
Robert Phillipseb54bb52021-01-08 17:20:18 -0500677 std::move(newDest),
Robert Phillips88b29612020-11-16 15:15:08 -0500678 std::move(ddl),
679 offset));
Robert Phillips07f675d2020-11-16 13:44:01 -0500680 SkASSERT(ddlTask->isClosed());
Adlai Holler7580ad42020-06-24 13:45:25 -0400681
Robert Phillips38d64b02018-09-04 13:23:26 -0400682 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500683}
684
Robert Phillips38d64b02018-09-04 13:23:26 -0400685#ifdef SK_DEBUG
686void GrDrawingManager::validate() const {
Adlai Holler4ef108e2020-12-30 16:21:13 +0000687 if (fActiveOpsTask) {
688 SkASSERT(!fDAG.empty());
689 SkASSERT(!fActiveOpsTask->isClosed());
690 SkASSERT(fActiveOpsTask == fDAG.back().get());
691 }
Adlai Holler039f90c2020-11-19 15:20:31 +0000692
Adlai Holler4ef108e2020-12-30 16:21:13 +0000693 for (int i = 0; i < fDAG.count(); ++i) {
694 if (fActiveOpsTask != fDAG[i].get()) {
695 // The resolveTask associated with the activeTask remains open for as long as the
696 // activeTask does.
697 bool isActiveResolveTask =
698 fActiveOpsTask && fActiveOpsTask->fTextureResolveTask == fDAG[i].get();
699 SkASSERT(isActiveResolveTask || fDAG[i]->isClosed());
Adlai Holler76893192020-12-22 19:41:19 +0000700 }
Adlai Holler4ef108e2020-12-30 16:21:13 +0000701 }
Adlai Holler76893192020-12-22 19:41:19 +0000702
Adlai Holler4ef108e2020-12-30 16:21:13 +0000703 if (!fDAG.empty() && !fDAG.back()->isClosed()) {
704 SkASSERT(fActiveOpsTask == fDAG.back().get());
Robert Phillips38d64b02018-09-04 13:23:26 -0400705 }
706}
707#endif
708
Adlai Holler4ef108e2020-12-30 16:21:13 +0000709void GrDrawingManager::closeActiveOpsTask() {
710 if (fActiveOpsTask) {
711 // This is a temporary fix for the partial-MDB world. In that world we're not
Adlai Holler039f90c2020-11-19 15:20:31 +0000712 // reordering so ops that (in the single opsTask world) would've just glommed onto the
Greg Danielf41b2bd2019-08-22 16:19:24 -0400713 // end of the single opsTask but referred to a far earlier RT need to appear in their
714 // own opsTask.
Adlai Holler039f90c2020-11-19 15:20:31 +0000715 fActiveOpsTask->makeClosed(*fContext->priv().caps());
716 fActiveOpsTask = nullptr;
robertphillips3dc6ae52015-10-20 09:54:32 -0700717 }
Chris Dalton5fe99772019-08-06 11:57:39 -0600718}
719
Greg Daniel16f5c652019-10-29 11:26:01 -0400720sk_sp<GrOpsTask> GrDrawingManager::newOpsTask(GrSurfaceProxyView surfaceView,
Robert Phillips19006652020-11-19 14:20:57 -0500721 bool flushTimeOpsTask) {
Chris Dalton5fe99772019-08-06 11:57:39 -0600722 SkDEBUGCODE(this->validate());
723 SkASSERT(fContext);
724
Adlai Holler4ef108e2020-12-30 16:21:13 +0000725 this->closeActiveOpsTask();
robertphillips3dc6ae52015-10-20 09:54:32 -0700726
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400727 sk_sp<GrOpsTask> opsTask(new GrOpsTask(this, fContext->priv().arenas(),
Michael Ludwigd0840ec2019-12-12 09:48:38 -0500728 std::move(surfaceView),
729 fContext->priv().auditTrail()));
Brian Salomon982127b2021-01-21 10:43:35 -0500730 SkASSERT(this->getLastRenderTask(opsTask->target(0)) == opsTask.get());
robertphillips3dc6ae52015-10-20 09:54:32 -0700731
Robert Phillips19006652020-11-19 14:20:57 -0500732 if (flushTimeOpsTask) {
733 fOnFlushRenderTasks.push_back(opsTask);
734 } else {
Adlai Holler33432272020-11-11 13:54:37 -0500735 this->appendTask(opsTask);
Adlai Holler039f90c2020-11-19 15:20:31 +0000736
Adlai Holler4ef108e2020-12-30 16:21:13 +0000737 fActiveOpsTask = opsTask.get();
Robert Phillips941d1442017-06-14 16:37:02 -0400738 }
robertphillips3dc6ae52015-10-20 09:54:32 -0700739
Robert Phillips38d64b02018-09-04 13:23:26 -0400740 SkDEBUGCODE(this->validate());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400741 return opsTask;
robertphillips3dc6ae52015-10-20 09:54:32 -0700742}
743
Adlai Holler039f90c2020-11-19 15:20:31 +0000744GrTextureResolveRenderTask* GrDrawingManager::newTextureResolveRenderTask(const GrCaps& caps) {
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600745 // 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 +0000746 // in sorting and opsTask reduction mode) the render tasks that depend on any proxy's current
Greg Danielf41b2bd2019-08-22 16:19:24 -0400747 // state. This is because those opsTasks can still receive new ops and because if they refer to
Adlai Holler039f90c2020-11-19 15:20:31 +0000748 // the mipmapped version of 'proxy', they will then come to depend on the render task being
Chris Dalton4ece96d2019-08-30 11:26:39 -0600749 // created here.
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600750 //
Adlai Holler039f90c2020-11-19 15:20:31 +0000751 // Add the new textureResolveTask before the fActiveOpsTask (if not in
752 // sorting/opsTask-splitting-reduction mode) because it will depend upon this resolve task.
Chris Dalton3d770272019-08-14 09:24:37 -0600753 // NOTE: Putting it here will also reduce the amount of work required by the topological sort.
Adlai Holler33432272020-11-11 13:54:37 -0500754 GrRenderTask* task = this->insertTaskBeforeLast(sk_make_sp<GrTextureResolveRenderTask>());
755 return static_cast<GrTextureResolveRenderTask*>(task);
Chris Dalton3d770272019-08-14 09:24:37 -0600756}
757
Greg Danielc30f1a92019-09-06 15:28:58 -0400758void GrDrawingManager::newWaitRenderTask(sk_sp<GrSurfaceProxy> proxy,
Greg Daniel301015c2019-11-18 14:06:46 -0500759 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> semaphores,
Greg Danielc30f1a92019-09-06 15:28:58 -0400760 int numSemaphores) {
761 SkDEBUGCODE(this->validate());
762 SkASSERT(fContext);
763
764 const GrCaps& caps = *fContext->priv().caps();
765
Greg Daniel16f5c652019-10-29 11:26:01 -0400766 sk_sp<GrWaitRenderTask> waitTask = sk_make_sp<GrWaitRenderTask>(GrSurfaceProxyView(proxy),
767 std::move(semaphores),
Greg Danielc30f1a92019-09-06 15:28:58 -0400768 numSemaphores);
Greg Danielc30f1a92019-09-06 15:28:58 -0400769
Brian Salomon982127b2021-01-21 10:43:35 -0500770 if (fActiveOpsTask && (fActiveOpsTask->target(0) == proxy.get())) {
Adlai Holler4ef108e2020-12-30 16:21:13 +0000771 SkASSERT(this->getLastRenderTask(proxy.get()) == fActiveOpsTask);
772 this->insertTaskBeforeLast(waitTask);
773 // In this case we keep the current renderTask open but just insert the new waitTask
774 // before it in the list. The waitTask will never need to trigger any resolves or mip
775 // map generation which is the main advantage of going through the proxy version.
776 // Additionally we would've had to temporarily set the wait task as the lastRenderTask
777 // on the proxy, add the dependency, and then reset the lastRenderTask to
778 // fActiveOpsTask. Additionally we make the waitTask depend on all of fActiveOpsTask
779 // dependencies so that we don't unnecessarily reorder the waitTask before them.
780 // Note: Any previous Ops already in fActiveOpsTask will get blocked by the wait
781 // semaphore even though they don't need to be for correctness.
782
783 // Make sure we add the dependencies of fActiveOpsTask to waitTask first or else we'll
784 // get a circular self dependency of waitTask on waitTask.
785 waitTask->addDependenciesFromOtherTask(fActiveOpsTask);
786 fActiveOpsTask->addDependency(waitTask.get());
Adlai Holler76893192020-12-22 19:41:19 +0000787 } else {
Adlai Holler4ef108e2020-12-30 16:21:13 +0000788 // In this case we just close the previous RenderTask and start and append the waitTask
789 // to the DAG. Since it is the last task now we call setLastRenderTask on the proxy. If
790 // there is a lastTask on the proxy we make waitTask depend on that task. This
791 // dependency isn't strictly needed but it does keep the DAG from reordering the
792 // waitTask earlier and blocking more tasks.
793 if (GrRenderTask* lastTask = this->getLastRenderTask(proxy.get())) {
794 waitTask->addDependency(lastTask);
Adlai Holler76893192020-12-22 19:41:19 +0000795 }
Adlai Holler4ef108e2020-12-30 16:21:13 +0000796 this->setLastRenderTask(proxy.get(), waitTask.get());
797 this->closeActiveOpsTask();
798 this->appendTask(waitTask);
Greg Danielc30f1a92019-09-06 15:28:58 -0400799 }
800 waitTask->makeClosed(caps);
801
802 SkDEBUGCODE(this->validate());
803}
804
Greg Danielbbfec9d2019-08-20 10:56:51 -0400805void GrDrawingManager::newTransferFromRenderTask(sk_sp<GrSurfaceProxy> srcProxy,
806 const SkIRect& srcRect,
807 GrColorType surfaceColorType,
808 GrColorType dstColorType,
809 sk_sp<GrGpuBuffer> dstBuffer,
810 size_t dstOffset) {
811 SkDEBUGCODE(this->validate());
812 SkASSERT(fContext);
Adlai Holler4ef108e2020-12-30 16:21:13 +0000813 this->closeActiveOpsTask();
Greg Danielbbfec9d2019-08-20 10:56:51 -0400814
Adlai Holler33432272020-11-11 13:54:37 -0500815 GrRenderTask* task = this->appendTask(sk_make_sp<GrTransferFromRenderTask>(
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400816 srcProxy, srcRect, surfaceColorType, dstColorType,
817 std::move(dstBuffer), dstOffset));
Greg Danielbbfec9d2019-08-20 10:56:51 -0400818
819 const GrCaps& caps = *fContext->priv().caps();
820
Brian Salomon7e67dca2020-07-21 09:27:25 -0400821 // We always say GrMipmapped::kNo here since we are always just copying from the base layer. We
Greg Danielbbfec9d2019-08-20 10:56:51 -0400822 // don't need to make sure the whole mip map chain is valid.
Brian Salomon7e67dca2020-07-21 09:27:25 -0400823 task->addDependency(this, srcProxy.get(), GrMipmapped::kNo,
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400824 GrTextureResolveManager(this), caps);
Greg Danielbbfec9d2019-08-20 10:56:51 -0400825 task->makeClosed(caps);
826
Greg Danielbbfec9d2019-08-20 10:56:51 -0400827 // We have closed the previous active oplist but since a new oplist isn't being added there
828 // shouldn't be an active one.
Adlai Holler039f90c2020-11-19 15:20:31 +0000829 SkASSERT(!fActiveOpsTask);
Greg Danielbbfec9d2019-08-20 10:56:51 -0400830 SkDEBUGCODE(this->validate());
831}
832
Brian Salomon982127b2021-01-21 10:43:35 -0500833bool GrDrawingManager::newCopyRenderTask(sk_sp<GrSurfaceProxy> src,
834 SkIRect srcRect,
835 sk_sp<GrSurfaceProxy> dst,
Brian Salomon0f9f8002021-01-22 16:30:50 -0500836 SkIPoint dstPoint,
837 GrSurfaceOrigin origin) {
Greg Daniele227fe42019-08-21 13:52:24 -0400838 SkDEBUGCODE(this->validate());
839 SkASSERT(fContext);
Greg Daniele227fe42019-08-21 13:52:24 -0400840
Brian Salomon56c78f42021-02-03 16:56:55 -0500841 // It'd be nicer to check this in GrCopyRenderTask::Make. This gets complicated because of
842 // "active ops task" tracking. dst will be the target of our copy task but it might also be the
843 // target of the active ops task. We currently require the active ops task to be closed before
844 // making a new task that targets the same proxy. However, if we first close the active ops
845 // task, then fail to make a copy task, the next active ops task may target the same proxy. This
846 // will trip an assert related to unnecessary ops task splitting.
847 if (src->framebufferOnly()) {
848 return false;
849 }
850
Brian Salomon507c5652021-02-03 20:32:06 +0000851 this->closeActiveOpsTask();
852
853 GrRenderTask* task = this->appendTask(GrCopyRenderTask::Make(this,
854 src,
855 srcRect,
856 std::move(dst),
857 dstPoint,
858 origin));
Greg Daniele227fe42019-08-21 13:52:24 -0400859 if (!task) {
860 return false;
861 }
862
Brian Salomon0f9f8002021-01-22 16:30:50 -0500863 const GrCaps& caps = *fContext->priv().caps();
Brian Salomon7e67dca2020-07-21 09:27:25 -0400864 // We always say GrMipmapped::kNo here since we are always just copying from the base layer to
Greg Daniele227fe42019-08-21 13:52:24 -0400865 // another base layer. We don't need to make sure the whole mip map chain is valid.
Brian Salomon507c5652021-02-03 20:32:06 +0000866 task->addDependency(this, src.get(), GrMipmapped::kNo, GrTextureResolveManager(this), caps);
867 task->makeClosed(caps);
Greg Daniele227fe42019-08-21 13:52:24 -0400868
Greg Daniele227fe42019-08-21 13:52:24 -0400869 // We have closed the previous active oplist but since a new oplist isn't being added there
870 // shouldn't be an active one.
Adlai Holler039f90c2020-11-19 15:20:31 +0000871 SkASSERT(!fActiveOpsTask);
Greg Daniele227fe42019-08-21 13:52:24 -0400872 SkDEBUGCODE(this->validate());
873 return true;
874}
875
Brian Salomonbe1084b2021-01-26 13:29:30 -0500876bool GrDrawingManager::newWritePixelsTask(sk_sp<GrSurfaceProxy> dst,
877 SkIRect rect,
878 GrColorType srcColorType,
879 GrColorType dstColorType,
880 const GrMipLevel levels[],
881 int levelCount,
882 sk_sp<SkData> owner) {
883 SkDEBUGCODE(this->validate());
884 SkASSERT(fContext);
885
886 this->closeActiveOpsTask();
887 const GrCaps& caps = *fContext->priv().caps();
888
889 // On platforms that prefer flushes over VRAM use (i.e., ANGLE) we're better off forcing a
890 // complete flush here.
891 if (!caps.preferVRAMUseOverFlushes()) {
892 this->flushSurfaces(SkSpan<GrSurfaceProxy*>{},
893 SkSurface::BackendSurfaceAccess::kNoAccess,
894 GrFlushInfo{},
895 nullptr);
896 }
897
898 GrRenderTask* task = this->appendTask(GrWritePixelsTask::Make(this,
899 std::move(dst),
900 rect,
901 srcColorType,
902 dstColorType,
903 levels,
904 levelCount,
905 std::move(owner)));
906 if (!task) {
907 return false;
908 }
909
910 task->makeClosed(caps);
911
912 // We have closed the previous active oplist but since a new oplist isn't being added there
913 // shouldn't be an active one.
914 SkASSERT(!fActiveOpsTask);
915 SkDEBUGCODE(this->validate());
916 return true;
917}
918
robertphillips68737822015-10-29 12:12:21 -0700919/*
920 * This method finds a path renderer that can draw the specified path on
921 * the provided target.
922 * Due to its expense, the software path renderer has split out so it can
923 * can be individually allowed/disallowed via the "allowSW" boolean.
924 */
925GrPathRenderer* GrDrawingManager::getPathRenderer(const GrPathRenderer::CanDrawPathArgs& args,
926 bool allowSW,
927 GrPathRendererChain::DrawType drawType,
928 GrPathRenderer::StencilSupport* stencilSupport) {
929
930 if (!fPathRendererChain) {
John Stilesfbd050b2020-08-03 13:21:46 -0400931 fPathRendererChain =
932 std::make_unique<GrPathRendererChain>(fContext, fOptionsForPathRendererChain);
robertphillips68737822015-10-29 12:12:21 -0700933 }
934
935 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(args, drawType, stencilSupport);
936 if (!pr && allowSW) {
Brian Salomone7df0bb2018-05-07 14:44:57 -0400937 auto swPR = this->getSoftwarePathRenderer();
938 if (GrPathRenderer::CanDrawPath::kNo != swPR->canDrawPath(args)) {
939 pr = swPR;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500940 }
robertphillips68737822015-10-29 12:12:21 -0700941 }
942
Robert Phillipsd81379d2020-04-21 10:39:02 -0400943#if GR_PATH_RENDERER_SPEW
944 if (pr) {
945 SkDebugf("getPathRenderer: %s\n", pr->name());
946 }
947#endif
948
robertphillips68737822015-10-29 12:12:21 -0700949 return pr;
950}
951
Brian Salomone7df0bb2018-05-07 14:44:57 -0400952GrPathRenderer* GrDrawingManager::getSoftwarePathRenderer() {
953 if (!fSoftwarePathRenderer) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400954 fSoftwarePathRenderer.reset(
Robert Phillips9da87e02019-02-04 13:26:26 -0500955 new GrSoftwarePathRenderer(fContext->priv().proxyProvider(),
Ben Wagner9ec70c62018-07-12 13:30:47 -0400956 fOptionsForPathRendererChain.fAllowPathMaskCaching));
Brian Salomone7df0bb2018-05-07 14:44:57 -0400957 }
Ben Wagner9ec70c62018-07-12 13:30:47 -0400958 return fSoftwarePathRenderer.get();
Brian Salomone7df0bb2018-05-07 14:44:57 -0400959}
960
Chris Daltonfddb6c02017-11-04 15:22:22 -0600961GrCoverageCountingPathRenderer* GrDrawingManager::getCoverageCountingPathRenderer() {
962 if (!fPathRendererChain) {
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500963 fPathRendererChain = std::make_unique<GrPathRendererChain>(fContext,
964 fOptionsForPathRendererChain);
Chris Daltonfddb6c02017-11-04 15:22:22 -0600965 }
966 return fPathRendererChain->getCoverageCountingPathRenderer();
967}
968
Chris Daltone6ae4762021-02-05 14:56:21 -0700969GrPathRenderer* GrDrawingManager::getTessellationPathRenderer() {
970 if (!fPathRendererChain) {
971 fPathRendererChain = std::make_unique<GrPathRendererChain>(fContext,
972 fOptionsForPathRendererChain);
973 }
974 return fPathRendererChain->getTessellationPathRenderer();
975}
976
Brian Salomon653f42f2018-07-10 10:07:31 -0400977void GrDrawingManager::flushIfNecessary() {
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400978 auto direct = fContext->asDirectContext();
Robert Phillips6a6de562019-02-15 15:19:15 -0500979 if (!direct) {
980 return;
981 }
982
983 auto resourceCache = direct->priv().getResourceCache();
Brian Salomon653f42f2018-07-10 10:07:31 -0400984 if (resourceCache && resourceCache->requestsFlush()) {
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500985 if (this->flush({}, SkSurface::BackendSurfaceAccess::kNoAccess, GrFlushInfo(), nullptr)) {
Greg Danielfe159622020-04-10 17:43:51 +0000986 this->submitToGpu(false);
987 }
Brian Salomon57d2beab2018-09-10 09:35:41 -0400988 resourceCache->purgeAsNeeded();
Brian Salomon653f42f2018-07-10 10:07:31 -0400989 }
990}