blob: 5961715b7a34f11da0301b37e871d5aa745af801 [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/core/SkTTopoSort.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"
28#include "src/gpu/GrRenderTargetContext.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040029#include "src/gpu/GrRenderTargetProxy.h"
Chris Dalton6b498102019-08-01 14:14:52 -060030#include "src/gpu/GrRenderTask.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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050035#include "src/gpu/GrSurfaceProxyPriv.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000036#include "src/gpu/GrTexture.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040037#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050038#include "src/gpu/GrTextureProxyPriv.h"
Chris Dalton3d770272019-08-14 09:24:37 -060039#include "src/gpu/GrTextureResolveRenderTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050040#include "src/gpu/GrTracing.h"
Greg Danielbbfec9d2019-08-20 10:56:51 -040041#include "src/gpu/GrTransferFromRenderTask.h"
Greg Danielc30f1a92019-09-06 15:28:58 -040042#include "src/gpu/GrWaitRenderTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050043#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
Herb Derbya08bde62020-06-12 15:46:06 -040044#include "src/gpu/text/GrSDFTOptions.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050045#include "src/image/SkSurface_Gpu.h"
robertphillips498d7ac2015-10-30 10:11:30 -070046
Robert Phillips22310d62018-09-05 11:07:21 -040047///////////////////////////////////////////////////////////////////////////////////////////////////
Robert Phillips69893702019-02-22 11:16:30 -050048GrDrawingManager::GrDrawingManager(GrRecordingContext* context,
Robert Phillips22310d62018-09-05 11:07:21 -040049 const GrPathRendererChain::Options& optionsForPathRendererChain,
Greg Danielf41b2bd2019-08-22 16:19:24 -040050 bool reduceOpsTaskSplitting)
Robert Phillips22310d62018-09-05 11:07:21 -040051 : fContext(context)
52 , fOptionsForPathRendererChain(optionsForPathRendererChain)
Robert Phillips22310d62018-09-05 11:07:21 -040053 , fPathRendererChain(nullptr)
54 , fSoftwarePathRenderer(nullptr)
Robert Phillips6db27c22019-05-01 10:43:56 -040055 , fFlushing(false)
Herb Derby082232b2020-06-10 15:08:18 -040056 , fReduceOpsTaskSplitting(reduceOpsTaskSplitting) { }
Robert Phillips22310d62018-09-05 11:07:21 -040057
robertphillips3dc6ae52015-10-20 09:54:32 -070058GrDrawingManager::~GrDrawingManager() {
Adlai Holler33432272020-11-11 13:54:37 -050059 this->closeAllTasks();
60 this->removeRenderTasks(0, fDAG.count());
robertphillips3dc6ae52015-10-20 09:54:32 -070061}
62
Robert Phillipsa9162df2019-02-11 14:12:03 -050063bool GrDrawingManager::wasAbandoned() const {
Robert Phillips9eb00022020-06-30 15:30:12 -040064 return fContext->abandoned();
robertphillips3dc6ae52015-10-20 09:54:32 -070065}
66
robertphillips68737822015-10-29 12:12:21 -070067void GrDrawingManager::freeGpuResources() {
Jim Van Verth106b5c42017-09-26 12:45:29 -040068 for (int i = fOnFlushCBObjects.count() - 1; i >= 0; --i) {
69 if (!fOnFlushCBObjects[i]->retainOnFreeGpuResources()) {
70 // it's safe to just do this because we're iterating in reverse
71 fOnFlushCBObjects.removeShuffle(i);
72 }
73 }
74
robertphillips68737822015-10-29 12:12:21 -070075 // a path renderer may be holding onto resources
robertphillips13391dd2015-10-30 05:15:11 -070076 fPathRendererChain = nullptr;
Ben Wagner9ec70c62018-07-12 13:30:47 -040077 fSoftwarePathRenderer = nullptr;
Robert Phillipse3302df2017-04-24 07:31:02 -040078}
79
Adlai Hollerc2bfcff2020-11-06 15:39:36 -050080// MDB TODO: make use of the 'proxies' parameter.
Greg Daniel9efe3862020-06-11 11:51:06 -040081bool GrDrawingManager::flush(
Adlai Hollerc2bfcff2020-11-06 15:39:36 -050082 SkSpan<GrSurfaceProxy*> proxies,
Greg Daniel9efe3862020-06-11 11:51:06 -040083 SkSurface::BackendSurfaceAccess access,
84 const GrFlushInfo& info,
85 const GrBackendSurfaceMutableState* newState) {
Brian Salomon57d2beab2018-09-10 09:35:41 -040086 GR_CREATE_TRACE_MARKER_CONTEXT("GrDrawingManager", "flush", fContext);
Brian Salomondcbb9d92017-07-19 10:53:20 -040087
robertphillips7761d612016-05-16 09:14:53 -070088 if (fFlushing || this->wasAbandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -040089 if (info.fSubmittedProc) {
90 info.fSubmittedProc(info.fSubmittedContext, false);
91 }
Greg Daniele6bfb7d2019-04-17 15:26:11 -040092 if (info.fFinishedProc) {
93 info.fFinishedProc(info.fFinishedContext);
Greg Daniela3aa75a2019-04-12 14:24:55 -040094 }
Greg Danielfe159622020-04-10 17:43:51 +000095 return false;
joshualittb8918c42015-12-18 09:59:46 -080096 }
Robert Phillips602df412019-04-08 11:10:39 -040097
Robert Phillips38d64b02018-09-04 13:23:26 -040098 SkDEBUGCODE(this->validate());
99
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500100 // As of now we only short-circuit if we got an explicit list of surfaces to flush.
101 if (!proxies.empty() && !info.fNumSemaphores && !info.fFinishedProc &&
Greg Danielce9f0162020-06-30 13:42:46 -0400102 access == SkSurface::BackendSurfaceAccess::kNoAccess && !newState) {
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500103 bool allUnused = std::all_of(proxies.begin(), proxies.end(), [&](GrSurfaceProxy* proxy) {
Adlai Holler33432272020-11-11 13:54:37 -0500104 bool used = std::any_of(fDAG.begin(), fDAG.end(), [&](auto& task) {
105 return task && task->isUsed(proxy);
106 });
107 return !used && !this->isDDLTarget(proxy);
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500108 });
109 if (allUnused) {
Greg Daniel55822f12020-05-26 11:26:45 -0400110 if (info.fSubmittedProc) {
111 info.fSubmittedProc(info.fSubmittedContext, true);
112 }
Greg Danielfe159622020-04-10 17:43:51 +0000113 return false;
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400114 }
Robert Phillips9313aa72019-04-09 18:41:27 -0400115 }
116
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400117 auto direct = fContext->asDirectContext();
Adlai Holleraee25fd2020-11-06 12:34:56 -0500118 SkASSERT(direct);
Brian Salomon9241a6d2019-10-03 13:26:54 -0400119 direct->priv().clientMappedBufferManager()->process();
Robert Phillips6a6de562019-02-15 15:19:15 -0500120
121 GrGpu* gpu = direct->priv().getGpu();
Greg Daniel55822f12020-05-26 11:26:45 -0400122 // We have a non abandoned and direct GrContext. It must have a GrGpu.
123 SkASSERT(gpu);
Greg Daniela3aa75a2019-04-12 14:24:55 -0400124
joshualittb8918c42015-12-18 09:59:46 -0800125 fFlushing = true;
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400126
Robert Phillips6a6de562019-02-15 15:19:15 -0500127 auto resourceProvider = direct->priv().resourceProvider();
128 auto resourceCache = direct->priv().getResourceCache();
129
Chris Dalton6b498102019-08-01 14:14:52 -0600130 // Semi-usually the GrRenderTasks are already closed at this point, but sometimes Ganesh needs
Greg Danielf41b2bd2019-08-22 16:19:24 -0400131 // to flush mid-draw. In that case, the SkGpuDevice's opsTasks won't be closed but need to be
132 // flushed anyway. Closing such opsTasks here will mean new ones will be created to replace them
Chris Dalton6b498102019-08-01 14:14:52 -0600133 // if the SkGpuDevice(s) write to them again.
Adlai Holler33432272020-11-11 13:54:37 -0500134 this->closeAllTasks();
Greg Danielf41b2bd2019-08-22 16:19:24 -0400135 fActiveOpsTask = nullptr;
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400136
Adlai Holler33432272020-11-11 13:54:37 -0500137 this->sortTasks();
Brian Salomon601ac802019-02-07 13:37:16 -0500138 if (!fCpuBufferCache) {
139 // We cache more buffers when the backend is using client side arrays. Otherwise, we
140 // expect each pool will use a CPU buffer as a staging buffer before uploading to a GPU
141 // buffer object. Each pool only requires one staging buffer at a time.
142 int maxCachedBuffers = fContext->priv().caps()->preferClientSideDynamicBuffers() ? 2 : 6;
143 fCpuBufferCache = GrBufferAllocPool::CpuBufferCache::Make(maxCachedBuffers);
Brian Salomon58f153c2018-10-18 21:51:15 -0400144 }
Robert Phillipsa4c93ac2017-05-18 11:40:04 -0400145
Robert Phillipse5f73282019-06-18 17:15:04 -0400146 GrOpFlushState flushState(gpu, resourceProvider, &fTokenTracker, fCpuBufferCache);
Robert Phillips40a29d72018-01-18 12:59:22 -0500147
Chris Daltonfe199b72017-05-05 11:26:15 -0400148 GrOnFlushResourceProvider onFlushProvider(this);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400149
Chris Dalton12658942017-10-05 19:45:25 -0600150 // Prepare any onFlush op lists (e.g. atlases).
Chris Daltonfe199b72017-05-05 11:26:15 -0400151 if (!fOnFlushCBObjects.empty()) {
Robert Phillips07f675d2020-11-16 13:44:01 -0500152 fFlushingRenderTaskIDs.reserve_back(fDAG.count());
153 for (const auto& task : fDAG) {
154 if (task) {
155 task->gatherIDs(&fFlushingRenderTaskIDs);
Adlai Holler33432272020-11-11 13:54:37 -0500156 }
157 }
Robert Phillips22310d62018-09-05 11:07:21 -0400158
Chris Daltonfe199b72017-05-05 11:26:15 -0400159 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
Adlai Holler9902cff2020-11-11 08:51:25 -0500160 onFlushCBObject->preFlush(&onFlushProvider, fFlushingRenderTaskIDs);
Chris Daltonc4b47352019-08-23 10:10:36 -0600161 }
162 for (const auto& onFlushRenderTask : fOnFlushRenderTasks) {
163 onFlushRenderTask->makeClosed(*fContext->priv().caps());
Chris Dalton706a6ff2017-11-29 22:01:06 -0700164#ifdef SK_DEBUG
Chris Daltonc4b47352019-08-23 10:10:36 -0600165 // OnFlush callbacks are invoked during flush, and are therefore expected to handle
166 // resource allocation & usage on their own. (No deferred or lazy proxies!)
167 onFlushRenderTask->visitTargetAndSrcProxies_debugOnly(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400168 [](GrSurfaceProxy* p, GrMipmapped mipMapped) {
Chris Daltonc4b47352019-08-23 10:10:36 -0600169 SkASSERT(!p->asTextureProxy() || !p->asTextureProxy()->texPriv().isDeferred());
Brian Salomonbeb7f522019-08-30 16:19:42 -0400170 SkASSERT(!p->isLazy());
Chris Dalton4ece96d2019-08-30 11:26:39 -0600171 if (p->requiresManualMSAAResolve()) {
172 // The onFlush callback is responsible for ensuring MSAA gets resolved.
173 SkASSERT(p->asRenderTargetProxy() && !p->asRenderTargetProxy()->isMSAADirty());
174 }
Brian Salomon7e67dca2020-07-21 09:27:25 -0400175 if (GrMipmapped::kYes == mipMapped) {
Chris Daltonc4b47352019-08-23 10:10:36 -0600176 // The onFlush callback is responsible for regenerating mips if needed.
Brian Salomon8c82a872020-07-21 12:09:58 -0400177 SkASSERT(p->asTextureProxy() && !p->asTextureProxy()->mipmapsAreDirty());
Chris Daltonc4b47352019-08-23 10:10:36 -0600178 }
179 });
Chris Dalton706a6ff2017-11-29 22:01:06 -0700180#endif
Chris Daltonc4b47352019-08-23 10:10:36 -0600181 onFlushRenderTask->prepare(&flushState);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400182 }
183 }
184
robertphillipsa13e2022015-11-11 12:01:09 -0800185#if 0
Brian Salomon09d994e2016-12-21 11:14:46 -0500186 // Enable this to print out verbose GrOp information
Adlai Hollerabdfd392020-10-19 09:00:36 -0400187 SkDEBUGCODE(SkDebugf("onFlush renderTasks (%d):\n", fOnFlushRenderTasks.count()));
Chris Daltonc4b47352019-08-23 10:10:36 -0600188 for (const auto& onFlushRenderTask : fOnFlushRenderTasks) {
Adlai Hollerabdfd392020-10-19 09:00:36 -0400189 SkDEBUGCODE(onFlushRenderTask->dump(/* printDependencies */ true);)
Chris Daltonc4b47352019-08-23 10:10:36 -0600190 }
Adlai Holler33432272020-11-11 13:54:37 -0500191 SkDEBUGCODE(SkDebugf("Normal renderTasks (%d):\n", fDAG.count()));
192 for (const auto& task : fDAG) {
193 SkDEBUGCODE(task->dump(/* printDependencies */ true);)
robertphillips3dc6ae52015-10-20 09:54:32 -0700194 }
robertphillipsa13e2022015-11-11 12:01:09 -0800195#endif
196
Robert Phillipseafd48a2017-11-16 07:52:08 -0500197 int startIndex, stopIndex;
198 bool flushed = false;
199
Robert Phillipsf8e25022017-11-08 15:24:31 -0500200 {
Adlai Holler33432272020-11-11 13:54:37 -0500201 GrResourceAllocator alloc(resourceProvider SkDEBUGCODE(, fDAG.count()));
202 for (int i = 0; i < fDAG.count(); ++i) {
203 if (fDAG[i]) {
204 fDAG[i]->gatherProxyIntervals(&alloc);
Robert Phillips22310d62018-09-05 11:07:21 -0400205 }
Greg Danielf41b2bd2019-08-22 16:19:24 -0400206 alloc.markEndOfOpsTask(i);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500207 }
Robert Phillipsc73666f2019-04-24 08:49:48 -0400208 alloc.determineRecyclability();
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400209
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500210 GrResourceAllocator::AssignError error = GrResourceAllocator::AssignError::kNoError;
Chris Dalton6b498102019-08-01 14:14:52 -0600211 int numRenderTasksExecuted = 0;
Brian Salomon577aa0f2018-11-30 13:32:23 -0500212 while (alloc.assign(&startIndex, &stopIndex, &error)) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500213 if (GrResourceAllocator::AssignError::kFailedProxyInstantiation == error) {
214 for (int i = startIndex; i < stopIndex; ++i) {
Adlai Holler33432272020-11-11 13:54:37 -0500215 GrRenderTask* renderTask = fDAG[i].get();
Chris Dalton6b498102019-08-01 14:14:52 -0600216 if (!renderTask) {
217 continue;
Robert Phillips01a91282018-07-26 08:03:04 -0400218 }
Chris Dalton6b498102019-08-01 14:14:52 -0600219 if (!renderTask->isInstantiated()) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400220 // No need to call the renderTask's handleInternalAllocationFailure
221 // since we will already skip executing the renderTask since it is not
222 // instantiated.
Chris Dalton6b498102019-08-01 14:14:52 -0600223 continue;
Robert Phillipsbbcb7f72018-05-31 11:16:19 -0400224 }
Chris Dalton6b498102019-08-01 14:14:52 -0600225 renderTask->handleInternalAllocationFailure();
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500226 }
Adlai Holler96ead542020-06-26 08:50:14 -0400227 this->removeRenderTasks(startIndex, stopIndex);
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500228 }
Robert Phillips4150eea2018-02-07 17:08:21 -0500229
Chris Dalton6b498102019-08-01 14:14:52 -0600230 if (this->executeRenderTasks(
231 startIndex, stopIndex, &flushState, &numRenderTasksExecuted)) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500232 flushed = true;
233 }
bsalomondc438982016-08-31 11:53:49 -0700234 }
Greg Danielc42b20b2017-10-04 10:34:49 -0400235 }
236
Chris Dalton91ab1552018-04-18 13:24:25 -0600237#ifdef SK_DEBUG
Adlai Holler33432272020-11-11 13:54:37 -0500238 for (const auto& task : fDAG) {
Adlai Holler96ead542020-06-26 08:50:14 -0400239 // All render tasks should have been cleared out by now – we only reset the array below to
240 // reclaim storage.
Adlai Holler33432272020-11-11 13:54:37 -0500241 SkASSERT(!task);
Chris Dalton91ab1552018-04-18 13:24:25 -0600242 }
243#endif
Adlai Holler25df1f72020-06-09 13:08:27 -0400244 fLastRenderTasks.reset();
Robert Phillips22310d62018-09-05 11:07:21 -0400245 fDAG.reset();
Robert Phillips15c91422019-05-07 16:54:48 -0400246 this->clearDDLTargets();
robertphillipsa13e2022015-11-11 12:01:09 -0800247
Robert Phillipsc994a932018-06-19 13:09:54 -0400248#ifdef SK_DEBUG
249 // In non-DDL mode this checks that all the flushed ops have been freed from the memory pool.
250 // When we move to partial flushes this assert will no longer be valid.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400251 // In DDL mode this check is somewhat superfluous since the memory for most of the ops/opsTasks
Robert Phillipsc994a932018-06-19 13:09:54 -0400252 // will be stored in the DDL's GrOpMemoryPools.
Herb Derbye32e1ab2020-10-27 10:29:46 -0400253 GrMemoryPool* opMemoryPool = fContext->priv().opMemoryPool();
Robert Phillipsc994a932018-06-19 13:09:54 -0400254 opMemoryPool->isEmpty();
255#endif
256
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500257 gpu->executeFlushInfo(proxies, access, info, newState);
robertphillipsa13e2022015-11-11 12:01:09 -0800258
Brian Salomon57d2beab2018-09-10 09:35:41 -0400259 // Give the cache a chance to purge resources that become purgeable due to flushing.
260 if (flushed) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500261 resourceCache->purgeAsNeeded();
Brian Salomon876a0172019-03-08 11:12:14 -0500262 flushed = false;
bsalomonb77a9072016-09-07 10:02:04 -0700263 }
Chris Daltonfe199b72017-05-05 11:26:15 -0400264 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
Adlai Holler9902cff2020-11-11 08:51:25 -0500265 onFlushCBObject->postFlush(fTokenTracker.nextTokenToFlush(), fFlushingRenderTaskIDs);
Brian Salomon876a0172019-03-08 11:12:14 -0500266 flushed = true;
267 }
268 if (flushed) {
269 resourceCache->purgeAsNeeded();
Chris Daltonfe199b72017-05-05 11:26:15 -0400270 }
Chris Dalton6b498102019-08-01 14:14:52 -0600271 fFlushingRenderTaskIDs.reset();
joshualittb8918c42015-12-18 09:59:46 -0800272 fFlushing = false;
Greg Daniel51316782017-08-02 15:10:09 +0000273
Greg Danielfe159622020-04-10 17:43:51 +0000274 return true;
275}
276
277bool GrDrawingManager::submitToGpu(bool syncToCpu) {
278 if (fFlushing || this->wasAbandoned()) {
279 return false;
280 }
281
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400282 auto direct = fContext->asDirectContext();
Greg Danielfe159622020-04-10 17:43:51 +0000283 if (!direct) {
284 return false; // Can't submit while DDL recording
285 }
286 GrGpu* gpu = direct->priv().getGpu();
287 return gpu->submitToGpu(syncToCpu);
robertphillips3dc6ae52015-10-20 09:54:32 -0700288}
289
Chris Dalton6b498102019-08-01 14:14:52 -0600290bool GrDrawingManager::executeRenderTasks(int startIndex, int stopIndex, GrOpFlushState* flushState,
291 int* numRenderTasksExecuted) {
Adlai Holler33432272020-11-11 13:54:37 -0500292 SkASSERT(startIndex <= stopIndex && stopIndex <= fDAG.count());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500293
Robert Phillips27483912018-04-20 12:43:18 -0400294#if GR_FLUSH_TIME_OP_SPEW
Greg Danielf41b2bd2019-08-22 16:19:24 -0400295 SkDebugf("Flushing opsTask: %d to %d out of [%d, %d]\n",
Adlai Holler33432272020-11-11 13:54:37 -0500296 startIndex, stopIndex, 0, fDAG.count());
Robert Phillips27483912018-04-20 12:43:18 -0400297 for (int i = startIndex; i < stopIndex; ++i) {
Adlai Holler33432272020-11-11 13:54:37 -0500298 if (fDAG[i]) {
299 fDAG[i]->dump(true);
Robert Phillips1734dd32018-08-21 13:52:09 -0400300 }
Robert Phillips27483912018-04-20 12:43:18 -0400301 }
302#endif
303
Chris Dalton6b498102019-08-01 14:14:52 -0600304 bool anyRenderTasksExecuted = false;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500305
306 for (int i = startIndex; i < stopIndex; ++i) {
Adlai Holler33432272020-11-11 13:54:37 -0500307 GrRenderTask* renderTask = fDAG[i].get();
Greg Daniel15ecdf92019-08-30 15:35:23 -0400308 if (!renderTask || !renderTask->isInstantiated()) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500309 continue;
310 }
311
Chris Dalton6b498102019-08-01 14:14:52 -0600312 SkASSERT(renderTask->deferredProxiesAreInstantiated());
Robert Phillips22310d62018-09-05 11:07:21 -0400313
Chris Dalton6b498102019-08-01 14:14:52 -0600314 renderTask->prepare(flushState);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500315 }
316
317 // Upload all data to the GPU
318 flushState->preExecuteDraws();
319
Greg Danield2073452018-12-07 11:20:33 -0500320 // For Vulkan, if we have too many oplists to be flushed we end up allocating a lot of resources
321 // for each command buffer associated with the oplists. If this gets too large we can cause the
322 // devices to go OOM. In practice we usually only hit this case in our tests, but to be safe we
323 // put a cap on the number of oplists we will execute before flushing to the GPU to relieve some
324 // memory pressure.
Chris Dalton6b498102019-08-01 14:14:52 -0600325 static constexpr int kMaxRenderTasksBeforeFlush = 100;
Greg Danield2073452018-12-07 11:20:33 -0500326
Chris Daltonc4b47352019-08-23 10:10:36 -0600327 // Execute the onFlush renderTasks first, if any.
328 for (sk_sp<GrRenderTask>& onFlushRenderTask : fOnFlushRenderTasks) {
329 if (!onFlushRenderTask->execute(flushState)) {
330 SkDebugf("WARNING: onFlushRenderTask failed to execute.\n");
Robert Phillipseafd48a2017-11-16 07:52:08 -0500331 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600332 SkASSERT(onFlushRenderTask->unique());
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400333 onFlushRenderTask->disown(this);
Chris Daltonc4b47352019-08-23 10:10:36 -0600334 onFlushRenderTask = nullptr;
Chris Dalton6b498102019-08-01 14:14:52 -0600335 (*numRenderTasksExecuted)++;
336 if (*numRenderTasksExecuted >= kMaxRenderTasksBeforeFlush) {
Greg Danielfe159622020-04-10 17:43:51 +0000337 flushState->gpu()->submitToGpu(false);
Chris Dalton6b498102019-08-01 14:14:52 -0600338 *numRenderTasksExecuted = 0;
Greg Danield2073452018-12-07 11:20:33 -0500339 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500340 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600341 fOnFlushRenderTasks.reset();
Robert Phillipseafd48a2017-11-16 07:52:08 -0500342
343 // Execute the normal op lists.
344 for (int i = startIndex; i < stopIndex; ++i) {
Adlai Holler33432272020-11-11 13:54:37 -0500345 GrRenderTask* renderTask = fDAG[i].get();
Greg Daniel15ecdf92019-08-30 15:35:23 -0400346 if (!renderTask || !renderTask->isInstantiated()) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500347 continue;
348 }
349
Greg Daniel15ecdf92019-08-30 15:35:23 -0400350 if (renderTask->execute(flushState)) {
Chris Dalton6b498102019-08-01 14:14:52 -0600351 anyRenderTasksExecuted = true;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500352 }
Chris Dalton6b498102019-08-01 14:14:52 -0600353 (*numRenderTasksExecuted)++;
354 if (*numRenderTasksExecuted >= kMaxRenderTasksBeforeFlush) {
Greg Danielfe159622020-04-10 17:43:51 +0000355 flushState->gpu()->submitToGpu(false);
Chris Dalton6b498102019-08-01 14:14:52 -0600356 *numRenderTasksExecuted = 0;
Greg Danield2073452018-12-07 11:20:33 -0500357 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500358 }
359
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400360 SkASSERT(!flushState->opsRenderPass());
Robert Phillips40a29d72018-01-18 12:59:22 -0500361 SkASSERT(fTokenTracker.nextDrawToken() == fTokenTracker.nextTokenToFlush());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500362
Chris Dalton6b498102019-08-01 14:14:52 -0600363 // We reset the flush state before the RenderTasks so that the last resources to be freed are
364 // those that are written to in the RenderTasks. This helps to make sure the most recently used
365 // resources are the last to be purged by the resource cache.
Robert Phillipseafd48a2017-11-16 07:52:08 -0500366 flushState->reset();
367
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400368 this->removeRenderTasks(startIndex, stopIndex);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500369
Chris Dalton6b498102019-08-01 14:14:52 -0600370 return anyRenderTasksExecuted;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500371}
372
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400373void GrDrawingManager::removeRenderTasks(int startIndex, int stopIndex) {
374 for (int i = startIndex; i < stopIndex; ++i) {
Adlai Holler33432272020-11-11 13:54:37 -0500375 GrRenderTask* task = fDAG[i].get();
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400376 if (!task) {
377 continue;
378 }
Robert Phillips07f675d2020-11-16 13:44:01 -0500379 if (!task->unique() || task->requiresExplicitCleanup()) {
380 // TODO: Eventually uniqueness should be guaranteed: http://skbug.com/7111.
381 // DDLs, however, will always require an explicit notification for when they
382 // can clean up resources.
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400383 task->endFlush(this);
384 }
385 task->disown(this);
Adlai Holler33432272020-11-11 13:54:37 -0500386
387 // This doesn't cleanup any referring pointers (e.g. dependency pointers in the DAG).
388 // It works right now bc this is only called after the topological sort is complete
389 // (so the dangling pointers aren't used).
390 fDAG[i] = nullptr;
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400391 }
Adlai Holler33432272020-11-11 13:54:37 -0500392}
393
394void GrDrawingManager::sortTasks() {
395 if (!SkTTopoSort<GrRenderTask, GrRenderTask::TopoSortTraits>(&fDAG)) {
396 SkDEBUGFAIL("Render task topo sort failed.");
397 return;
398 }
399
400#ifdef SK_DEBUG
401 // This block checks for any unnecessary splits in the opsTasks. If two sequential opsTasks
402 // share the same backing GrSurfaceProxy it means the opsTask was artificially split.
403 if (!fDAG.empty()) {
404 GrOpsTask* prevOpsTask = fDAG[0]->asOpsTask();
405 for (int i = 1; i < fDAG.count(); ++i) {
406 GrOpsTask* curOpsTask = fDAG[i]->asOpsTask();
407
408 if (prevOpsTask && curOpsTask) {
409 SkASSERT(prevOpsTask->target(0).proxy() != curOpsTask->target(0).proxy());
410 }
411
412 prevOpsTask = curOpsTask;
413 }
414 }
415#endif
416}
417
418void GrDrawingManager::closeAllTasks() {
419 const GrCaps& caps = *fContext->priv().caps();
420 for (auto& task : fDAG) {
421 if (task) {
422 task->makeClosed(caps);
423 }
424 }
425}
426
427GrRenderTask* GrDrawingManager::insertTaskBeforeLast(sk_sp<GrRenderTask> task) {
428 SkASSERT(!fDAG.empty());
429 if (!task) {
430 return nullptr;
431 }
432 // Release 'fDAG.back()' and grab the raw pointer, in case the SkTArray grows
433 // and reallocates during emplace_back.
434 // TODO: Either use std::vector that can do this for us, or use SkSTArray to get the
435 // perf win.
436 fDAG.emplace_back(fDAG.back().release());
437 return (fDAG[fDAG.count() - 2] = std::move(task)).get();
438}
439
440GrRenderTask* GrDrawingManager::appendTask(sk_sp<GrRenderTask> task) {
441 if (!task) {
442 return nullptr;
443 }
444 return fDAG.push_back(std::move(task)).get();
445}
446
447void GrDrawingManager::appendTasks(SkSpan<const sk_sp<GrRenderTask>> tasks) {
448#ifdef SK_DEBUG
449 for (const auto& task : tasks) {
450 SkASSERT(task && task->unique());
451 }
452#endif
453 fDAG.push_back_n(tasks.count(), tasks.begin());
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400454}
455
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400456static void resolve_and_mipmap(GrGpu* gpu, GrSurfaceProxy* proxy) {
457 if (!proxy->isInstantiated()) {
458 return;
459 }
460
461 // In the flushSurfaces case, we need to resolve MSAA immediately after flush. This is
462 // because clients expect the flushed surface's backing texture to be fully resolved
463 // upon return.
464 if (proxy->requiresManualMSAAResolve()) {
465 auto* rtProxy = proxy->asRenderTargetProxy();
466 SkASSERT(rtProxy);
467 if (rtProxy->isMSAADirty()) {
468 SkASSERT(rtProxy->peekRenderTarget());
Jim Van Verthbb61fe32020-07-07 16:39:04 -0400469 gpu->resolveRenderTarget(rtProxy->peekRenderTarget(), rtProxy->msaaDirtyRect());
470 gpu->submitToGpu(false);
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400471 rtProxy->markMSAAResolved();
472 }
473 }
474 // If, after a flush, any of the proxies of interest have dirty mipmaps, regenerate them in
475 // case their backend textures are being stolen.
476 // (This special case is exercised by the ReimportImageTextureWithMipLevels test.)
477 // FIXME: It may be more ideal to plumb down a "we're going to steal the backends" flag.
478 if (auto* textureProxy = proxy->asTextureProxy()) {
Brian Salomon8c82a872020-07-21 12:09:58 -0400479 if (textureProxy->mipmapsAreDirty()) {
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400480 SkASSERT(textureProxy->peekTexture());
481 gpu->regenerateMipMapLevels(textureProxy->peekTexture());
Brian Salomon8c82a872020-07-21 12:09:58 -0400482 textureProxy->markMipmapsClean();
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400483 }
484 }
485}
486
Greg Daniel9efe3862020-06-11 11:51:06 -0400487GrSemaphoresSubmitted GrDrawingManager::flushSurfaces(
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500488 SkSpan<GrSurfaceProxy*> proxies,
Greg Daniel9efe3862020-06-11 11:51:06 -0400489 SkSurface::BackendSurfaceAccess access,
490 const GrFlushInfo& info,
491 const GrBackendSurfaceMutableState* newState) {
bsalomon6a2b1942016-09-08 11:28:59 -0700492 if (this->wasAbandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -0400493 if (info.fSubmittedProc) {
494 info.fSubmittedProc(info.fSubmittedContext, false);
495 }
496 if (info.fFinishedProc) {
497 info.fFinishedProc(info.fFinishedContext);
498 }
Greg Daniel51316782017-08-02 15:10:09 +0000499 return GrSemaphoresSubmitted::kNo;
bsalomon6a2b1942016-09-08 11:28:59 -0700500 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400501 SkDEBUGCODE(this->validate());
bsalomon6a2b1942016-09-08 11:28:59 -0700502
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400503 auto direct = fContext->asDirectContext();
Adlai Holleraee25fd2020-11-06 12:34:56 -0500504 SkASSERT(direct);
Robert Phillips6a6de562019-02-15 15:19:15 -0500505 GrGpu* gpu = direct->priv().getGpu();
Greg Daniel55822f12020-05-26 11:26:45 -0400506 // We have a non abandoned and direct GrContext. It must have a GrGpu.
507 SkASSERT(gpu);
Robert Phillips874b5352018-03-16 08:48:24 -0400508
Robert Phillipsacc10fa2019-04-01 09:50:20 -0400509 // TODO: It is important to upgrade the drawingmanager to just flushing the
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400510 // portion of the DAG required by 'proxies' in order to restore some of the
Robert Phillipsacc10fa2019-04-01 09:50:20 -0400511 // semantics of this method.
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500512 bool didFlush = this->flush(proxies, access, info, newState);
513 for (GrSurfaceProxy* proxy : proxies) {
514 resolve_and_mipmap(gpu, proxy);
bsalomon6a2b1942016-09-08 11:28:59 -0700515 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400516
517 SkDEBUGCODE(this->validate());
Greg Danielfe159622020-04-10 17:43:51 +0000518
Greg Daniel04283f32020-05-20 13:16:00 -0400519 if (!didFlush || (!direct->priv().caps()->semaphoreSupport() && info.fNumSemaphores)) {
Greg Danielfe159622020-04-10 17:43:51 +0000520 return GrSemaphoresSubmitted::kNo;
521 }
522 return GrSemaphoresSubmitted::kYes;
bsalomon6a2b1942016-09-08 11:28:59 -0700523}
524
Chris Daltonfe199b72017-05-05 11:26:15 -0400525void GrDrawingManager::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
526 fOnFlushCBObjects.push_back(onFlushCBObject);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400527}
528
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500529#if GR_TEST_UTILS
530void GrDrawingManager::testingOnly_removeOnFlushCallbackObject(GrOnFlushCallbackObject* cb) {
531 int n = std::find(fOnFlushCBObjects.begin(), fOnFlushCBObjects.end(), cb) -
532 fOnFlushCBObjects.begin();
533 SkASSERT(n < fOnFlushCBObjects.count());
534 fOnFlushCBObjects.removeShuffle(n);
535}
536#endif
537
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400538void GrDrawingManager::setLastRenderTask(const GrSurfaceProxy* proxy, GrRenderTask* task) {
539#ifdef SK_DEBUG
Robert Phillips07f675d2020-11-16 13:44:01 -0500540 if (auto prior = this->getLastRenderTask(proxy)) {
541 SkASSERT(prior->isClosed() || prior == task);
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400542 }
543#endif
Adlai Holler25df1f72020-06-09 13:08:27 -0400544 uint32_t key = proxy->uniqueID().asUInt();
545 if (task) {
546 fLastRenderTasks.set(key, task);
547 } else if (fLastRenderTasks.find(key)) {
548 fLastRenderTasks.remove(key);
549 }
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400550}
551
552GrRenderTask* GrDrawingManager::getLastRenderTask(const GrSurfaceProxy* proxy) const {
553 auto entry = fLastRenderTasks.find(proxy->uniqueID().asUInt());
554 return entry ? *entry : nullptr;
555}
556
557GrOpsTask* GrDrawingManager::getLastOpsTask(const GrSurfaceProxy* proxy) const {
558 GrRenderTask* task = this->getLastRenderTask(proxy);
559 return task ? task->asOpsTask() : nullptr;
560}
561
562
Chris Dalton6b498102019-08-01 14:14:52 -0600563void GrDrawingManager::moveRenderTasksToDDL(SkDeferredDisplayList* ddl) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400564 SkDEBUGCODE(this->validate());
565
Chris Dalton6b498102019-08-01 14:14:52 -0600566 // no renderTask should receive a new command after this
Adlai Holler33432272020-11-11 13:54:37 -0500567 this->closeAllTasks();
Greg Danielf41b2bd2019-08-22 16:19:24 -0400568 fActiveOpsTask = nullptr;
Robert Phillips7a137052018-02-01 11:23:12 -0500569
Robert Phillips07f675d2020-11-16 13:44:01 -0500570 this->sortTasks();
571
Adlai Holler33432272020-11-11 13:54:37 -0500572 fDAG.swap(ddl->fRenderTasks);
573 SkASSERT(fDAG.empty());
Robert Phillips867ce8f2018-06-21 10:28:36 -0400574
Robert Phillips19f466d2020-02-26 10:27:07 -0500575 for (auto& renderTask : ddl->fRenderTasks) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400576 renderTask->disown(this);
Robert Phillips29f38542019-10-16 09:20:25 -0400577 renderTask->prePrepare(fContext);
Robert Phillips7327c9d2019-10-08 16:32:56 -0400578 }
579
Michael Ludwig2c316bd2019-12-19 14:50:44 -0500580 ddl->fArenas = std::move(fContext->priv().detachArenas());
Robert Phillips61fc7992019-10-22 11:58:17 -0400581
Robert Phillipsf6a0b452020-02-18 14:26:46 -0500582 fContext->priv().detachProgramData(&ddl->fProgramData);
Robert Phillips576b6a12019-12-06 13:05:49 -0500583
Robert Phillips774168e2018-05-31 12:43:27 -0400584 if (fPathRendererChain) {
585 if (auto ccpr = fPathRendererChain->getCoverageCountingPathRenderer()) {
586 ddl->fPendingPaths = ccpr->detachPendingPaths();
587 }
588 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400589
590 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500591}
592
Robert Phillips07f675d2020-11-16 13:44:01 -0500593void GrDrawingManager::createDDLTask(sk_sp<const SkDeferredDisplayList> ddl,
594 GrRenderTargetProxy* newDest) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400595 SkDEBUGCODE(this->validate());
596
Greg Danielf41b2bd2019-08-22 16:19:24 -0400597 if (fActiveOpsTask) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400598 // This is a temporary fix for the partial-MDB world. In that world we're not
Greg Danielf41b2bd2019-08-22 16:19:24 -0400599 // reordering so ops that (in the single opsTask world) would've just glommed onto the
600 // end of the single opsTask but referred to a far earlier RT need to appear in their
601 // own opsTask.
602 fActiveOpsTask->makeClosed(*fContext->priv().caps());
603 fActiveOpsTask = nullptr;
Robert Phillips38d64b02018-09-04 13:23:26 -0400604 }
605
Robert Phillips07f675d2020-11-16 13:44:01 -0500606 // Propagate the DDL proxy's state information to the replay target.
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400607 if (ddl->priv().targetProxy()->isMSAADirty()) {
608 newDest->markMSAADirty(ddl->priv().targetProxy()->msaaDirtyRect(),
609 ddl->characterization().origin());
610 }
611 GrTextureProxy* newTextureProxy = newDest->asTextureProxy();
Brian Salomon8c82a872020-07-21 12:09:58 -0400612 if (newTextureProxy && GrMipmapped::kYes == newTextureProxy->mipmapped()) {
613 newTextureProxy->markMipmapsDirty();
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400614 }
615
616 this->addDDLTarget(newDest, ddl->priv().targetProxy());
Robert Phillips15c91422019-05-07 16:54:48 -0400617
Robert Phillips62000362018-02-01 09:10:04 -0500618 // Here we jam the proxy that backs the current replay SkSurface into the LazyProxyData.
Robert Phillips07f675d2020-11-16 13:44:01 -0500619 // The lazy proxy that references it (in the DDL opsTasks) will then steal its GrTexture.
Robert Phillips62000362018-02-01 09:10:04 -0500620 ddl->fLazyProxyData->fReplayDest = newDest;
Robert Phillips774168e2018-05-31 12:43:27 -0400621
622 if (ddl->fPendingPaths.size()) {
623 GrCoverageCountingPathRenderer* ccpr = this->getCoverageCountingPathRenderer();
624
625 ccpr->mergePendingPaths(ddl->fPendingPaths);
626 }
Robert Phillips22310d62018-09-05 11:07:21 -0400627
Robert Phillips07f675d2020-11-16 13:44:01 -0500628 // Add a task to handle drawing and lifetime management of the DDL.
629 SkDEBUGCODE(auto ddlTask =) this->appendTask(sk_make_sp<GrDDLTask>(this,
630 sk_ref_sp(newDest),
631 std::move(ddl)));
632 SkASSERT(ddlTask->isClosed());
Adlai Holler7580ad42020-06-24 13:45:25 -0400633
Robert Phillips38d64b02018-09-04 13:23:26 -0400634 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500635}
636
Robert Phillips38d64b02018-09-04 13:23:26 -0400637#ifdef SK_DEBUG
638void GrDrawingManager::validate() const {
Adlai Holler3078f852020-11-05 15:44:50 -0500639 if (fReduceOpsTaskSplitting) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400640 SkASSERT(!fActiveOpsTask);
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400641 } else {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400642 if (fActiveOpsTask) {
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400643 SkASSERT(!fDAG.empty());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400644 SkASSERT(!fActiveOpsTask->isClosed());
Adlai Holler33432272020-11-11 13:54:37 -0500645 SkASSERT(fActiveOpsTask == fDAG.back().get());
Robert Phillips38d64b02018-09-04 13:23:26 -0400646 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400647
Adlai Holler33432272020-11-11 13:54:37 -0500648 for (int i = 0; i < fDAG.count(); ++i) {
649 if (fActiveOpsTask != fDAG[i].get()) {
Chris Daltone2a903e2019-09-18 13:41:50 -0600650 // The resolveTask associated with the activeTask remains open for as long as the
651 // activeTask does.
652 bool isActiveResolveTask =
Adlai Holler33432272020-11-11 13:54:37 -0500653 fActiveOpsTask && fActiveOpsTask->fTextureResolveTask == fDAG[i].get();
654 SkASSERT(isActiveResolveTask || fDAG[i]->isClosed());
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400655 }
656 }
657
658 if (!fDAG.empty() && !fDAG.back()->isClosed()) {
Adlai Holler33432272020-11-11 13:54:37 -0500659 SkASSERT(fActiveOpsTask == fDAG.back().get());
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400660 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400661 }
662}
663#endif
664
Greg Danielbbfec9d2019-08-20 10:56:51 -0400665void GrDrawingManager::closeRenderTasksForNewRenderTask(GrSurfaceProxy* target) {
Adlai Holler3078f852020-11-05 15:44:50 -0500666 if (target && fReduceOpsTaskSplitting) {
Chris Dalton5fe99772019-08-06 11:57:39 -0600667 // In this case we need to close all the renderTasks that rely on the current contents of
668 // 'target'. That is bc we're going to update the content of the proxy so they need to be
669 // split in case they use both the old and new content. (This is a bit of an overkill: they
670 // really only need to be split if they ever reference proxy's contents again but that is
671 // hard to predict/handle).
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400672 if (GrRenderTask* lastRenderTask = this->getLastRenderTask(target)) {
Chris Dalton6b498102019-08-01 14:14:52 -0600673 lastRenderTask->closeThoseWhoDependOnMe(*fContext->priv().caps());
Robert Phillips46acf9d2018-10-09 09:31:40 -0400674 }
Greg Danielf41b2bd2019-08-22 16:19:24 -0400675 } else if (fActiveOpsTask) {
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400676 // This is a temporary fix for the partial-MDB world. In that world we're not
Greg Danielf41b2bd2019-08-22 16:19:24 -0400677 // reordering so ops that (in the single opsTask world) would've just glommed onto the
678 // end of the single opsTask but referred to a far earlier RT need to appear in their
679 // own opsTask.
680 fActiveOpsTask->makeClosed(*fContext->priv().caps());
681 fActiveOpsTask = nullptr;
robertphillips3dc6ae52015-10-20 09:54:32 -0700682 }
Chris Dalton5fe99772019-08-06 11:57:39 -0600683}
684
Greg Daniel16f5c652019-10-29 11:26:01 -0400685sk_sp<GrOpsTask> GrDrawingManager::newOpsTask(GrSurfaceProxyView surfaceView,
686 bool managedOpsTask) {
Chris Dalton5fe99772019-08-06 11:57:39 -0600687 SkDEBUGCODE(this->validate());
688 SkASSERT(fContext);
689
Greg Daniel16f5c652019-10-29 11:26:01 -0400690 GrSurfaceProxy* proxy = surfaceView.proxy();
691 this->closeRenderTasksForNewRenderTask(proxy);
robertphillips3dc6ae52015-10-20 09:54:32 -0700692
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400693 sk_sp<GrOpsTask> opsTask(new GrOpsTask(this, fContext->priv().arenas(),
Michael Ludwigd0840ec2019-12-12 09:48:38 -0500694 std::move(surfaceView),
695 fContext->priv().auditTrail()));
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400696 SkASSERT(this->getLastRenderTask(proxy) == opsTask.get());
robertphillips3dc6ae52015-10-20 09:54:32 -0700697
Greg Danielf41b2bd2019-08-22 16:19:24 -0400698 if (managedOpsTask) {
Adlai Holler33432272020-11-11 13:54:37 -0500699 this->appendTask(opsTask);
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400700
Adlai Holler3078f852020-11-05 15:44:50 -0500701 if (!fReduceOpsTaskSplitting) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400702 fActiveOpsTask = opsTask.get();
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400703 }
Robert Phillips941d1442017-06-14 16:37:02 -0400704 }
robertphillips3dc6ae52015-10-20 09:54:32 -0700705
Robert Phillips38d64b02018-09-04 13:23:26 -0400706 SkDEBUGCODE(this->validate());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400707 return opsTask;
robertphillips3dc6ae52015-10-20 09:54:32 -0700708}
709
Chris Daltone2a903e2019-09-18 13:41:50 -0600710GrTextureResolveRenderTask* GrDrawingManager::newTextureResolveRenderTask(const GrCaps& caps) {
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600711 // Unlike in the "new opsTask" case, we do not want to close the active opsTask, nor (if we are
Chris Daltone2a903e2019-09-18 13:41:50 -0600712 // in sorting and opsTask reduction mode) the render tasks that depend on any proxy's current
Greg Danielf41b2bd2019-08-22 16:19:24 -0400713 // state. This is because those opsTasks can still receive new ops and because if they refer to
Chris Dalton4ece96d2019-08-30 11:26:39 -0600714 // the mipmapped version of 'proxy', they will then come to depend on the render task being
715 // created here.
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600716 //
Greg Danielf41b2bd2019-08-22 16:19:24 -0400717 // Add the new textureResolveTask before the fActiveOpsTask (if not in
718 // sorting/opsTask-splitting-reduction mode) because it will depend upon this resolve task.
Chris Dalton3d770272019-08-14 09:24:37 -0600719 // NOTE: Putting it here will also reduce the amount of work required by the topological sort.
Adlai Holler33432272020-11-11 13:54:37 -0500720 GrRenderTask* task = this->insertTaskBeforeLast(sk_make_sp<GrTextureResolveRenderTask>());
721 return static_cast<GrTextureResolveRenderTask*>(task);
Chris Dalton3d770272019-08-14 09:24:37 -0600722}
723
Greg Danielc30f1a92019-09-06 15:28:58 -0400724void GrDrawingManager::newWaitRenderTask(sk_sp<GrSurfaceProxy> proxy,
Greg Daniel301015c2019-11-18 14:06:46 -0500725 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> semaphores,
Greg Danielc30f1a92019-09-06 15:28:58 -0400726 int numSemaphores) {
727 SkDEBUGCODE(this->validate());
728 SkASSERT(fContext);
729
730 const GrCaps& caps = *fContext->priv().caps();
731
Greg Daniel16f5c652019-10-29 11:26:01 -0400732 sk_sp<GrWaitRenderTask> waitTask = sk_make_sp<GrWaitRenderTask>(GrSurfaceProxyView(proxy),
733 std::move(semaphores),
Greg Danielc30f1a92019-09-06 15:28:58 -0400734 numSemaphores);
735 if (fReduceOpsTaskSplitting) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400736 GrRenderTask* lastTask = this->getLastRenderTask(proxy.get());
Greg Danielc30f1a92019-09-06 15:28:58 -0400737 if (lastTask && !lastTask->isClosed()) {
738 // We directly make the currently open renderTask depend on waitTask instead of using
739 // the proxy version of addDependency. The waitTask will never need to trigger any
740 // resolves or mip map generation which is the main advantage of going through the proxy
741 // version. Additionally we would've had to temporarily set the wait task as the
742 // lastRenderTask on the proxy, add the dependency, and then reset the lastRenderTask to
743 // lastTask. Additionally we add all dependencies of lastTask to waitTask so that the
744 // waitTask doesn't get reordered before them and unnecessarily block those tasks.
745 // Note: Any previous Ops already in lastTask will get blocked by the wait semaphore
746 // even though they don't need to be for correctness.
747
748 // Make sure we add the dependencies of lastTask to waitTask first or else we'll get a
749 // circular self dependency of waitTask on waitTask.
750 waitTask->addDependenciesFromOtherTask(lastTask);
751 lastTask->addDependency(waitTask.get());
752 } else {
753 // If there is a last task we set the waitTask to depend on it so that it doesn't get
754 // reordered in front of the lastTask causing the lastTask to be blocked by the
755 // semaphore. Again we directly just go through adding the dependency to the task and
756 // not the proxy since we don't need to worry about resolving anything.
757 if (lastTask) {
758 waitTask->addDependency(lastTask);
759 }
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400760 this->setLastRenderTask(proxy.get(), waitTask.get());
Greg Danielc30f1a92019-09-06 15:28:58 -0400761 }
Adlai Holler33432272020-11-11 13:54:37 -0500762 this->appendTask(waitTask);
Greg Danielc30f1a92019-09-06 15:28:58 -0400763 } else {
Adlai Holler33d569e2020-06-16 14:30:08 -0400764 if (fActiveOpsTask && (fActiveOpsTask->target(0).proxy() == proxy.get())) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400765 SkASSERT(this->getLastRenderTask(proxy.get()) == fActiveOpsTask);
Adlai Holler33432272020-11-11 13:54:37 -0500766 this->insertTaskBeforeLast(waitTask);
Greg Danielc30f1a92019-09-06 15:28:58 -0400767 // In this case we keep the current renderTask open but just insert the new waitTask
768 // before it in the list. The waitTask will never need to trigger any resolves or mip
769 // map generation which is the main advantage of going through the proxy version.
770 // Additionally we would've had to temporarily set the wait task as the lastRenderTask
771 // on the proxy, add the dependency, and then reset the lastRenderTask to
772 // fActiveOpsTask. Additionally we make the waitTask depend on all of fActiveOpsTask
773 // dependencies so that we don't unnecessarily reorder the waitTask before them.
774 // Note: Any previous Ops already in fActiveOpsTask will get blocked by the wait
775 // semaphore even though they don't need to be for correctness.
776
777 // Make sure we add the dependencies of fActiveOpsTask to waitTask first or else we'll
778 // get a circular self dependency of waitTask on waitTask.
779 waitTask->addDependenciesFromOtherTask(fActiveOpsTask);
780 fActiveOpsTask->addDependency(waitTask.get());
781 } else {
782 // In this case we just close the previous RenderTask and start and append the waitTask
783 // to the DAG. Since it is the last task now we call setLastRenderTask on the proxy. If
784 // there is a lastTask on the proxy we make waitTask depend on that task. This
785 // dependency isn't strictly needed but it does keep the DAG from reordering the
786 // waitTask earlier and blocking more tasks.
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400787 if (GrRenderTask* lastTask = this->getLastRenderTask(proxy.get())) {
Greg Danielc30f1a92019-09-06 15:28:58 -0400788 waitTask->addDependency(lastTask);
789 }
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400790 this->setLastRenderTask(proxy.get(), waitTask.get());
Greg Danielc30f1a92019-09-06 15:28:58 -0400791 this->closeRenderTasksForNewRenderTask(proxy.get());
Adlai Holler33432272020-11-11 13:54:37 -0500792 this->appendTask(waitTask);
Greg Danielc30f1a92019-09-06 15:28:58 -0400793 }
794 }
795 waitTask->makeClosed(caps);
796
797 SkDEBUGCODE(this->validate());
798}
799
Greg Danielbbfec9d2019-08-20 10:56:51 -0400800void GrDrawingManager::newTransferFromRenderTask(sk_sp<GrSurfaceProxy> srcProxy,
801 const SkIRect& srcRect,
802 GrColorType surfaceColorType,
803 GrColorType dstColorType,
804 sk_sp<GrGpuBuffer> dstBuffer,
805 size_t dstOffset) {
806 SkDEBUGCODE(this->validate());
807 SkASSERT(fContext);
808 // This copies from srcProxy to dstBuffer so it doesn't have a real target.
809 this->closeRenderTasksForNewRenderTask(nullptr);
810
Adlai Holler33432272020-11-11 13:54:37 -0500811 GrRenderTask* task = this->appendTask(sk_make_sp<GrTransferFromRenderTask>(
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400812 srcProxy, srcRect, surfaceColorType, dstColorType,
813 std::move(dstBuffer), dstOffset));
Greg Danielbbfec9d2019-08-20 10:56:51 -0400814
815 const GrCaps& caps = *fContext->priv().caps();
816
Brian Salomon7e67dca2020-07-21 09:27:25 -0400817 // We always say GrMipmapped::kNo here since we are always just copying from the base layer. We
Greg Danielbbfec9d2019-08-20 10:56:51 -0400818 // don't need to make sure the whole mip map chain is valid.
Brian Salomon7e67dca2020-07-21 09:27:25 -0400819 task->addDependency(this, srcProxy.get(), GrMipmapped::kNo,
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400820 GrTextureResolveManager(this), caps);
Greg Danielbbfec9d2019-08-20 10:56:51 -0400821 task->makeClosed(caps);
822
Greg Danielbbfec9d2019-08-20 10:56:51 -0400823 // We have closed the previous active oplist but since a new oplist isn't being added there
824 // shouldn't be an active one.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400825 SkASSERT(!fActiveOpsTask);
Greg Danielbbfec9d2019-08-20 10:56:51 -0400826 SkDEBUGCODE(this->validate());
827}
828
Greg Daniel16f5c652019-10-29 11:26:01 -0400829bool GrDrawingManager::newCopyRenderTask(GrSurfaceProxyView srcView,
Greg Daniele227fe42019-08-21 13:52:24 -0400830 const SkIRect& srcRect,
Greg Daniel16f5c652019-10-29 11:26:01 -0400831 GrSurfaceProxyView dstView,
Greg Daniele227fe42019-08-21 13:52:24 -0400832 const SkIPoint& dstPoint) {
833 SkDEBUGCODE(this->validate());
834 SkASSERT(fContext);
Greg Daniele227fe42019-08-21 13:52:24 -0400835
Greg Daniel16f5c652019-10-29 11:26:01 -0400836 this->closeRenderTasksForNewRenderTask(dstView.proxy());
Brian Salomone4bce012019-09-20 15:34:23 -0400837 const GrCaps& caps = *fContext->priv().caps();
838
Greg Daniel16f5c652019-10-29 11:26:01 -0400839 GrSurfaceProxy* srcProxy = srcView.proxy();
840
Brian Salomone4bce012019-09-20 15:34:23 -0400841 GrRenderTask* task =
Adlai Holler33432272020-11-11 13:54:37 -0500842 this->appendTask(GrCopyRenderTask::Make(this, std::move(srcView), srcRect,
843 std::move(dstView), dstPoint, &caps));
Greg Daniele227fe42019-08-21 13:52:24 -0400844 if (!task) {
845 return false;
846 }
847
Brian Salomon7e67dca2020-07-21 09:27:25 -0400848 // We always say GrMipmapped::kNo here since we are always just copying from the base layer to
Greg Daniele227fe42019-08-21 13:52:24 -0400849 // another base layer. We don't need to make sure the whole mip map chain is valid.
Brian Salomon7e67dca2020-07-21 09:27:25 -0400850 task->addDependency(this, srcProxy, GrMipmapped::kNo, GrTextureResolveManager(this), caps);
Greg Daniele227fe42019-08-21 13:52:24 -0400851 task->makeClosed(caps);
852
Greg Daniele227fe42019-08-21 13:52:24 -0400853 // We have closed the previous active oplist but since a new oplist isn't being added there
854 // shouldn't be an active one.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400855 SkASSERT(!fActiveOpsTask);
Greg Daniele227fe42019-08-21 13:52:24 -0400856 SkDEBUGCODE(this->validate());
857 return true;
858}
859
robertphillips68737822015-10-29 12:12:21 -0700860/*
861 * This method finds a path renderer that can draw the specified path on
862 * the provided target.
863 * Due to its expense, the software path renderer has split out so it can
864 * can be individually allowed/disallowed via the "allowSW" boolean.
865 */
866GrPathRenderer* GrDrawingManager::getPathRenderer(const GrPathRenderer::CanDrawPathArgs& args,
867 bool allowSW,
868 GrPathRendererChain::DrawType drawType,
869 GrPathRenderer::StencilSupport* stencilSupport) {
870
871 if (!fPathRendererChain) {
John Stilesfbd050b2020-08-03 13:21:46 -0400872 fPathRendererChain =
873 std::make_unique<GrPathRendererChain>(fContext, fOptionsForPathRendererChain);
robertphillips68737822015-10-29 12:12:21 -0700874 }
875
876 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(args, drawType, stencilSupport);
877 if (!pr && allowSW) {
Brian Salomone7df0bb2018-05-07 14:44:57 -0400878 auto swPR = this->getSoftwarePathRenderer();
879 if (GrPathRenderer::CanDrawPath::kNo != swPR->canDrawPath(args)) {
880 pr = swPR;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500881 }
robertphillips68737822015-10-29 12:12:21 -0700882 }
883
Robert Phillipsd81379d2020-04-21 10:39:02 -0400884#if GR_PATH_RENDERER_SPEW
885 if (pr) {
886 SkDebugf("getPathRenderer: %s\n", pr->name());
887 }
888#endif
889
robertphillips68737822015-10-29 12:12:21 -0700890 return pr;
891}
892
Brian Salomone7df0bb2018-05-07 14:44:57 -0400893GrPathRenderer* GrDrawingManager::getSoftwarePathRenderer() {
894 if (!fSoftwarePathRenderer) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400895 fSoftwarePathRenderer.reset(
Robert Phillips9da87e02019-02-04 13:26:26 -0500896 new GrSoftwarePathRenderer(fContext->priv().proxyProvider(),
Ben Wagner9ec70c62018-07-12 13:30:47 -0400897 fOptionsForPathRendererChain.fAllowPathMaskCaching));
Brian Salomone7df0bb2018-05-07 14:44:57 -0400898 }
Ben Wagner9ec70c62018-07-12 13:30:47 -0400899 return fSoftwarePathRenderer.get();
Brian Salomone7df0bb2018-05-07 14:44:57 -0400900}
901
Chris Daltonfddb6c02017-11-04 15:22:22 -0600902GrCoverageCountingPathRenderer* GrDrawingManager::getCoverageCountingPathRenderer() {
903 if (!fPathRendererChain) {
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500904 fPathRendererChain = std::make_unique<GrPathRendererChain>(fContext,
905 fOptionsForPathRendererChain);
Chris Daltonfddb6c02017-11-04 15:22:22 -0600906 }
907 return fPathRendererChain->getCoverageCountingPathRenderer();
908}
909
Brian Salomon653f42f2018-07-10 10:07:31 -0400910void GrDrawingManager::flushIfNecessary() {
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400911 auto direct = fContext->asDirectContext();
Robert Phillips6a6de562019-02-15 15:19:15 -0500912 if (!direct) {
913 return;
914 }
915
916 auto resourceCache = direct->priv().getResourceCache();
Brian Salomon653f42f2018-07-10 10:07:31 -0400917 if (resourceCache && resourceCache->requestsFlush()) {
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500918 if (this->flush({}, SkSurface::BackendSurfaceAccess::kNoAccess, GrFlushInfo(), nullptr)) {
Greg Danielfe159622020-04-10 17:43:51 +0000919 this->submitToGpu(false);
920 }
Brian Salomon57d2beab2018-09-10 09:35:41 -0400921 resourceCache->purgeAsNeeded();
Brian Salomon653f42f2018-07-10 10:07:31 -0400922 }
923}