blob: a55ea632870f8409aaa7d5d13792fcfd2e7c6a74 [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
John Stilesfbd050b2020-08-03 13:21:46 -040010#include <memory>
11
Robert Phillips4d5594d2020-02-21 14:24:40 -050012#include "include/core/SkDeferredDisplayList.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/gpu/GrBackendSemaphore.h"
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040014#include "include/gpu/GrDirectContext.h"
15#include "include/gpu/GrRecordingContext.h"
Robert Phillips7eb0a5f2020-06-09 14:15:09 -040016#include "src/core/SkDeferredDisplayListPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/core/SkTTopoSort.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040018#include "src/gpu/GrAuditTrail.h"
Brian Salomon9241a6d2019-10-03 13:26:54 -040019#include "src/gpu/GrClientMappedBufferManager.h"
Greg Daniele227fe42019-08-21 13:52:24 -040020#include "src/gpu/GrCopyRenderTask.h"
Adlai Hollera0693042020-10-14 11:23:11 -040021#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/gpu/GrGpu.h"
23#include "src/gpu/GrMemoryPool.h"
24#include "src/gpu/GrOnFlushResourceProvider.h"
25#include "src/gpu/GrRecordingContextPriv.h"
26#include "src/gpu/GrRenderTargetContext.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040027#include "src/gpu/GrRenderTargetProxy.h"
Chris Dalton6b498102019-08-01 14:14:52 -060028#include "src/gpu/GrRenderTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "src/gpu/GrResourceAllocator.h"
30#include "src/gpu/GrResourceProvider.h"
31#include "src/gpu/GrSoftwarePathRenderer.h"
Greg Daniel46e366a2019-12-16 14:38:36 -050032#include "src/gpu/GrSurfaceContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050033#include "src/gpu/GrSurfaceProxyPriv.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000034#include "src/gpu/GrTexture.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040035#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050036#include "src/gpu/GrTextureProxyPriv.h"
Chris Dalton3d770272019-08-14 09:24:37 -060037#include "src/gpu/GrTextureResolveRenderTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050038#include "src/gpu/GrTracing.h"
Greg Danielbbfec9d2019-08-20 10:56:51 -040039#include "src/gpu/GrTransferFromRenderTask.h"
Adlai Holler6c9bb622020-06-25 09:21:18 -040040#include "src/gpu/GrUnrefDDLTask.h"
Greg Danielc30f1a92019-09-06 15:28:58 -040041#include "src/gpu/GrWaitRenderTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050042#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
Herb Derbya08bde62020-06-12 15:46:06 -040043#include "src/gpu/text/GrSDFTOptions.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050044#include "src/image/SkSurface_Gpu.h"
robertphillips498d7ac2015-10-30 10:11:30 -070045
Chris Dalton6b498102019-08-01 14:14:52 -060046void GrDrawingManager::RenderTaskDAG::gatherIDs(SkSTArray<8, uint32_t, true>* idArray) const {
47 idArray->reset(fRenderTasks.count());
48 for (int i = 0; i < fRenderTasks.count(); ++i) {
49 if (fRenderTasks[i]) {
50 (*idArray)[i] = fRenderTasks[i]->uniqueID();
Robert Phillips22310d62018-09-05 11:07:21 -040051 }
52 }
53}
54
Chris Dalton6b498102019-08-01 14:14:52 -060055void GrDrawingManager::RenderTaskDAG::reset() {
56 fRenderTasks.reset();
Robert Phillips22310d62018-09-05 11:07:21 -040057}
58
Adlai Holler96ead542020-06-26 08:50:14 -040059void GrDrawingManager::RenderTaskDAG::rawRemoveRenderTasks(int startIndex, int stopIndex) {
Robert Phillips22310d62018-09-05 11:07:21 -040060 for (int i = startIndex; i < stopIndex; ++i) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -040061 fRenderTasks[i] = nullptr;
Robert Phillips22310d62018-09-05 11:07:21 -040062 }
63}
64
Chris Dalton6b498102019-08-01 14:14:52 -060065bool GrDrawingManager::RenderTaskDAG::isUsed(GrSurfaceProxy* proxy) const {
Adlai Holler33d569e2020-06-16 14:30:08 -040066 for (const auto& task : fRenderTasks) {
67 if (task && task->isUsed(proxy)) {
Robert Phillips9313aa72019-04-09 18:41:27 -040068 return true;
69 }
70 }
71
72 return false;
73}
74
Chris Dalton3d770272019-08-14 09:24:37 -060075GrRenderTask* GrDrawingManager::RenderTaskDAG::add(sk_sp<GrRenderTask> renderTask) {
Chris Dalton6aeb8e82019-08-27 11:52:19 -060076 if (renderTask) {
77 return fRenderTasks.emplace_back(std::move(renderTask)).get();
78 }
79 return nullptr;
Chris Dalton3d770272019-08-14 09:24:37 -060080}
81
82GrRenderTask* GrDrawingManager::RenderTaskDAG::addBeforeLast(sk_sp<GrRenderTask> renderTask) {
83 SkASSERT(!fRenderTasks.empty());
Chris Dalton6aeb8e82019-08-27 11:52:19 -060084 if (renderTask) {
85 // Release 'fRenderTasks.back()' and grab the raw pointer, in case the SkTArray grows
86 // and reallocates during emplace_back.
87 fRenderTasks.emplace_back(fRenderTasks.back().release());
88 return (fRenderTasks[fRenderTasks.count() - 2] = std::move(renderTask)).get();
89 }
90 return nullptr;
Robert Phillips22310d62018-09-05 11:07:21 -040091}
92
Greg Danielf41b2bd2019-08-22 16:19:24 -040093void GrDrawingManager::RenderTaskDAG::add(const SkTArray<sk_sp<GrRenderTask>>& renderTasks) {
Robert Phillips19f466d2020-02-26 10:27:07 -050094#ifdef SK_DEBUG
95 for (auto& renderTask : renderTasks) {
96 SkASSERT(renderTask->unique());
97 }
98#endif
99
Greg Danielf41b2bd2019-08-22 16:19:24 -0400100 fRenderTasks.push_back_n(renderTasks.count(), renderTasks.begin());
Robert Phillips22310d62018-09-05 11:07:21 -0400101}
102
Greg Danielf41b2bd2019-08-22 16:19:24 -0400103void GrDrawingManager::RenderTaskDAG::swap(SkTArray<sk_sp<GrRenderTask>>* renderTasks) {
104 SkASSERT(renderTasks->empty());
105 renderTasks->swap(fRenderTasks);
Robert Phillips22310d62018-09-05 11:07:21 -0400106}
107
Chris Dalton6b498102019-08-01 14:14:52 -0600108void GrDrawingManager::RenderTaskDAG::prepForFlush() {
Adlai Holler3078f852020-11-05 15:44:50 -0500109 if (!SkTTopoSort<GrRenderTask, GrRenderTask::TopoSortTraits>(&fRenderTasks)) {
110 SkDEBUGFAIL("Render task topo sort failed.");
111 return;
Robert Phillips22310d62018-09-05 11:07:21 -0400112 }
113
114#ifdef SK_DEBUG
Greg Danielf41b2bd2019-08-22 16:19:24 -0400115 // This block checks for any unnecessary splits in the opsTasks. If two sequential opsTasks
116 // share the same backing GrSurfaceProxy it means the opsTask was artificially split.
Chris Dalton6b498102019-08-01 14:14:52 -0600117 if (fRenderTasks.count()) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400118 GrOpsTask* prevOpsTask = fRenderTasks[0]->asOpsTask();
Chris Dalton6b498102019-08-01 14:14:52 -0600119 for (int i = 1; i < fRenderTasks.count(); ++i) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400120 GrOpsTask* curOpsTask = fRenderTasks[i]->asOpsTask();
Robert Phillips22310d62018-09-05 11:07:21 -0400121
Greg Danielf41b2bd2019-08-22 16:19:24 -0400122 if (prevOpsTask && curOpsTask) {
Adlai Holler33d569e2020-06-16 14:30:08 -0400123 SkASSERT(prevOpsTask->target(0).proxy() != curOpsTask->target(0).proxy());
Robert Phillips22310d62018-09-05 11:07:21 -0400124 }
125
Greg Danielf41b2bd2019-08-22 16:19:24 -0400126 prevOpsTask = curOpsTask;
Robert Phillips22310d62018-09-05 11:07:21 -0400127 }
128 }
129#endif
130}
131
Chris Dalton6b498102019-08-01 14:14:52 -0600132void GrDrawingManager::RenderTaskDAG::closeAll(const GrCaps* caps) {
Adlai Holler96ead542020-06-26 08:50:14 -0400133 for (auto& task : fRenderTasks) {
134 if (task) {
135 task->makeClosed(*caps);
Robert Phillips22310d62018-09-05 11:07:21 -0400136 }
137 }
138}
139
Robert Phillips22310d62018-09-05 11:07:21 -0400140///////////////////////////////////////////////////////////////////////////////////////////////////
Robert Phillips69893702019-02-22 11:16:30 -0500141GrDrawingManager::GrDrawingManager(GrRecordingContext* context,
Robert Phillips22310d62018-09-05 11:07:21 -0400142 const GrPathRendererChain::Options& optionsForPathRendererChain,
Greg Danielf41b2bd2019-08-22 16:19:24 -0400143 bool reduceOpsTaskSplitting)
Robert Phillips22310d62018-09-05 11:07:21 -0400144 : fContext(context)
145 , fOptionsForPathRendererChain(optionsForPathRendererChain)
Robert Phillips22310d62018-09-05 11:07:21 -0400146 , fPathRendererChain(nullptr)
147 , fSoftwarePathRenderer(nullptr)
Robert Phillips6db27c22019-05-01 10:43:56 -0400148 , fFlushing(false)
Herb Derby082232b2020-06-10 15:08:18 -0400149 , fReduceOpsTaskSplitting(reduceOpsTaskSplitting) { }
Robert Phillips22310d62018-09-05 11:07:21 -0400150
robertphillips3dc6ae52015-10-20 09:54:32 -0700151GrDrawingManager::~GrDrawingManager() {
Adlai Holler96ead542020-06-26 08:50:14 -0400152 fDAG.closeAll(fContext->priv().caps());
153 this->removeRenderTasks(0, fDAG.numRenderTasks());
robertphillips3dc6ae52015-10-20 09:54:32 -0700154}
155
Robert Phillipsa9162df2019-02-11 14:12:03 -0500156bool GrDrawingManager::wasAbandoned() const {
Robert Phillips9eb00022020-06-30 15:30:12 -0400157 return fContext->abandoned();
robertphillips3dc6ae52015-10-20 09:54:32 -0700158}
159
robertphillips68737822015-10-29 12:12:21 -0700160void GrDrawingManager::freeGpuResources() {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400161 for (int i = fOnFlushCBObjects.count() - 1; i >= 0; --i) {
162 if (!fOnFlushCBObjects[i]->retainOnFreeGpuResources()) {
163 // it's safe to just do this because we're iterating in reverse
164 fOnFlushCBObjects.removeShuffle(i);
165 }
166 }
167
robertphillips68737822015-10-29 12:12:21 -0700168 // a path renderer may be holding onto resources
robertphillips13391dd2015-10-30 05:15:11 -0700169 fPathRendererChain = nullptr;
Ben Wagner9ec70c62018-07-12 13:30:47 -0400170 fSoftwarePathRenderer = nullptr;
Robert Phillipse3302df2017-04-24 07:31:02 -0400171}
172
Robert Phillips7ee385e2017-03-30 08:02:11 -0400173// MDB TODO: make use of the 'proxy' parameter.
Greg Daniel9efe3862020-06-11 11:51:06 -0400174bool GrDrawingManager::flush(
175 GrSurfaceProxy* proxies[],
176 int numProxies,
177 SkSurface::BackendSurfaceAccess access,
178 const GrFlushInfo& info,
179 const GrBackendSurfaceMutableState* newState) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400180 SkASSERT(numProxies >= 0);
181 SkASSERT(!numProxies || proxies);
Brian Salomon57d2beab2018-09-10 09:35:41 -0400182 GR_CREATE_TRACE_MARKER_CONTEXT("GrDrawingManager", "flush", fContext);
Brian Salomondcbb9d92017-07-19 10:53:20 -0400183
robertphillips7761d612016-05-16 09:14:53 -0700184 if (fFlushing || this->wasAbandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -0400185 if (info.fSubmittedProc) {
186 info.fSubmittedProc(info.fSubmittedContext, false);
187 }
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400188 if (info.fFinishedProc) {
189 info.fFinishedProc(info.fFinishedContext);
Greg Daniela3aa75a2019-04-12 14:24:55 -0400190 }
Greg Danielfe159622020-04-10 17:43:51 +0000191 return false;
joshualittb8918c42015-12-18 09:59:46 -0800192 }
Robert Phillips602df412019-04-08 11:10:39 -0400193
Robert Phillips38d64b02018-09-04 13:23:26 -0400194 SkDEBUGCODE(this->validate());
195
Greg Danielce9f0162020-06-30 13:42:46 -0400196 if (!info.fNumSemaphores && !info.fFinishedProc &&
197 access == SkSurface::BackendSurfaceAccess::kNoAccess && !newState) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400198 bool canSkip = numProxies > 0;
199 for (int i = 0; i < numProxies && canSkip; ++i) {
200 canSkip = !fDAG.isUsed(proxies[i]) && !this->isDDLTarget(proxies[i]);
201 }
202 if (canSkip) {
Greg Daniel55822f12020-05-26 11:26:45 -0400203 if (info.fSubmittedProc) {
204 info.fSubmittedProc(info.fSubmittedContext, true);
205 }
Greg Danielfe159622020-04-10 17:43:51 +0000206 return false;
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400207 }
Robert Phillips9313aa72019-04-09 18:41:27 -0400208 }
209
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400210 auto direct = fContext->asDirectContext();
Robert Phillips6a6de562019-02-15 15:19:15 -0500211 if (!direct) {
Greg Daniel55822f12020-05-26 11:26:45 -0400212 if (info.fSubmittedProc) {
213 info.fSubmittedProc(info.fSubmittedContext, false);
214 }
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400215 if (info.fFinishedProc) {
216 info.fFinishedProc(info.fFinishedContext);
Greg Daniela3aa75a2019-04-12 14:24:55 -0400217 }
Greg Danielfe159622020-04-10 17:43:51 +0000218 return false; // Can't flush while DDL recording
Robert Phillips6a6de562019-02-15 15:19:15 -0500219 }
Brian Salomon9241a6d2019-10-03 13:26:54 -0400220 direct->priv().clientMappedBufferManager()->process();
Robert Phillips6a6de562019-02-15 15:19:15 -0500221
222 GrGpu* gpu = direct->priv().getGpu();
Greg Daniel55822f12020-05-26 11:26:45 -0400223 // We have a non abandoned and direct GrContext. It must have a GrGpu.
224 SkASSERT(gpu);
Greg Daniela3aa75a2019-04-12 14:24:55 -0400225
joshualittb8918c42015-12-18 09:59:46 -0800226 fFlushing = true;
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400227
Robert Phillips6a6de562019-02-15 15:19:15 -0500228 auto resourceProvider = direct->priv().resourceProvider();
229 auto resourceCache = direct->priv().getResourceCache();
230
Chris Dalton6b498102019-08-01 14:14:52 -0600231 // Semi-usually the GrRenderTasks are already closed at this point, but sometimes Ganesh needs
Greg Danielf41b2bd2019-08-22 16:19:24 -0400232 // to flush mid-draw. In that case, the SkGpuDevice's opsTasks won't be closed but need to be
233 // flushed anyway. Closing such opsTasks here will mean new ones will be created to replace them
Chris Dalton6b498102019-08-01 14:14:52 -0600234 // if the SkGpuDevice(s) write to them again.
Robert Phillips9da87e02019-02-04 13:26:26 -0500235 fDAG.closeAll(fContext->priv().caps());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400236 fActiveOpsTask = nullptr;
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400237
Robert Phillips22310d62018-09-05 11:07:21 -0400238 fDAG.prepForFlush();
Brian Salomon601ac802019-02-07 13:37:16 -0500239 if (!fCpuBufferCache) {
240 // We cache more buffers when the backend is using client side arrays. Otherwise, we
241 // expect each pool will use a CPU buffer as a staging buffer before uploading to a GPU
242 // buffer object. Each pool only requires one staging buffer at a time.
243 int maxCachedBuffers = fContext->priv().caps()->preferClientSideDynamicBuffers() ? 2 : 6;
244 fCpuBufferCache = GrBufferAllocPool::CpuBufferCache::Make(maxCachedBuffers);
Brian Salomon58f153c2018-10-18 21:51:15 -0400245 }
Robert Phillipsa4c93ac2017-05-18 11:40:04 -0400246
Robert Phillipse5f73282019-06-18 17:15:04 -0400247 GrOpFlushState flushState(gpu, resourceProvider, &fTokenTracker, fCpuBufferCache);
Robert Phillips40a29d72018-01-18 12:59:22 -0500248
Chris Daltonfe199b72017-05-05 11:26:15 -0400249 GrOnFlushResourceProvider onFlushProvider(this);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400250
Chris Dalton12658942017-10-05 19:45:25 -0600251 // Prepare any onFlush op lists (e.g. atlases).
Chris Daltonfe199b72017-05-05 11:26:15 -0400252 if (!fOnFlushCBObjects.empty()) {
Chris Dalton6b498102019-08-01 14:14:52 -0600253 fDAG.gatherIDs(&fFlushingRenderTaskIDs);
Robert Phillips22310d62018-09-05 11:07:21 -0400254
Chris Daltonfe199b72017-05-05 11:26:15 -0400255 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
Chris Dalton6b498102019-08-01 14:14:52 -0600256 onFlushCBObject->preFlush(&onFlushProvider, fFlushingRenderTaskIDs.begin(),
Chris Daltonc4b47352019-08-23 10:10:36 -0600257 fFlushingRenderTaskIDs.count());
258 }
259 for (const auto& onFlushRenderTask : fOnFlushRenderTasks) {
260 onFlushRenderTask->makeClosed(*fContext->priv().caps());
Chris Dalton706a6ff2017-11-29 22:01:06 -0700261#ifdef SK_DEBUG
Chris Daltonc4b47352019-08-23 10:10:36 -0600262 // OnFlush callbacks are invoked during flush, and are therefore expected to handle
263 // resource allocation & usage on their own. (No deferred or lazy proxies!)
264 onFlushRenderTask->visitTargetAndSrcProxies_debugOnly(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400265 [](GrSurfaceProxy* p, GrMipmapped mipMapped) {
Chris Daltonc4b47352019-08-23 10:10:36 -0600266 SkASSERT(!p->asTextureProxy() || !p->asTextureProxy()->texPriv().isDeferred());
Brian Salomonbeb7f522019-08-30 16:19:42 -0400267 SkASSERT(!p->isLazy());
Chris Dalton4ece96d2019-08-30 11:26:39 -0600268 if (p->requiresManualMSAAResolve()) {
269 // The onFlush callback is responsible for ensuring MSAA gets resolved.
270 SkASSERT(p->asRenderTargetProxy() && !p->asRenderTargetProxy()->isMSAADirty());
271 }
Brian Salomon7e67dca2020-07-21 09:27:25 -0400272 if (GrMipmapped::kYes == mipMapped) {
Chris Daltonc4b47352019-08-23 10:10:36 -0600273 // The onFlush callback is responsible for regenerating mips if needed.
Brian Salomon8c82a872020-07-21 12:09:58 -0400274 SkASSERT(p->asTextureProxy() && !p->asTextureProxy()->mipmapsAreDirty());
Chris Daltonc4b47352019-08-23 10:10:36 -0600275 }
276 });
Chris Dalton706a6ff2017-11-29 22:01:06 -0700277#endif
Chris Daltonc4b47352019-08-23 10:10:36 -0600278 onFlushRenderTask->prepare(&flushState);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400279 }
280 }
281
robertphillipsa13e2022015-11-11 12:01:09 -0800282#if 0
Brian Salomon09d994e2016-12-21 11:14:46 -0500283 // Enable this to print out verbose GrOp information
Adlai Hollerabdfd392020-10-19 09:00:36 -0400284 SkDEBUGCODE(SkDebugf("onFlush renderTasks (%d):\n", fOnFlushRenderTasks.count()));
Chris Daltonc4b47352019-08-23 10:10:36 -0600285 for (const auto& onFlushRenderTask : fOnFlushRenderTasks) {
Adlai Hollerabdfd392020-10-19 09:00:36 -0400286 SkDEBUGCODE(onFlushRenderTask->dump(/* printDependencies */ true);)
Chris Daltonc4b47352019-08-23 10:10:36 -0600287 }
Adlai Hollerabdfd392020-10-19 09:00:36 -0400288 SkDEBUGCODE(SkDebugf("Normal renderTasks (%d):\n", fDAG.numRenderTasks()));
289 for (int i = 0; i < fDAG.numRenderTasks(); ++i) {
290 SkDEBUGCODE(fDAG.renderTask(i)->dump(/* printDependencies */ true);)
robertphillips3dc6ae52015-10-20 09:54:32 -0700291 }
robertphillipsa13e2022015-11-11 12:01:09 -0800292#endif
293
Robert Phillipseafd48a2017-11-16 07:52:08 -0500294 int startIndex, stopIndex;
295 bool flushed = false;
296
Robert Phillipsf8e25022017-11-08 15:24:31 -0500297 {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400298 GrResourceAllocator alloc(resourceProvider SkDEBUGCODE(, fDAG.numRenderTasks()));
Chris Dalton6b498102019-08-01 14:14:52 -0600299 for (int i = 0; i < fDAG.numRenderTasks(); ++i) {
300 if (fDAG.renderTask(i)) {
301 fDAG.renderTask(i)->gatherProxyIntervals(&alloc);
Robert Phillips22310d62018-09-05 11:07:21 -0400302 }
Greg Danielf41b2bd2019-08-22 16:19:24 -0400303 alloc.markEndOfOpsTask(i);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500304 }
Robert Phillipsc73666f2019-04-24 08:49:48 -0400305 alloc.determineRecyclability();
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400306
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500307 GrResourceAllocator::AssignError error = GrResourceAllocator::AssignError::kNoError;
Chris Dalton6b498102019-08-01 14:14:52 -0600308 int numRenderTasksExecuted = 0;
Brian Salomon577aa0f2018-11-30 13:32:23 -0500309 while (alloc.assign(&startIndex, &stopIndex, &error)) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500310 if (GrResourceAllocator::AssignError::kFailedProxyInstantiation == error) {
311 for (int i = startIndex; i < stopIndex; ++i) {
Chris Dalton6b498102019-08-01 14:14:52 -0600312 GrRenderTask* renderTask = fDAG.renderTask(i);
313 if (!renderTask) {
314 continue;
Robert Phillips01a91282018-07-26 08:03:04 -0400315 }
Chris Dalton6b498102019-08-01 14:14:52 -0600316 if (!renderTask->isInstantiated()) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400317 // No need to call the renderTask's handleInternalAllocationFailure
318 // since we will already skip executing the renderTask since it is not
319 // instantiated.
Chris Dalton6b498102019-08-01 14:14:52 -0600320 continue;
Robert Phillipsbbcb7f72018-05-31 11:16:19 -0400321 }
Chris Dalton6b498102019-08-01 14:14:52 -0600322 renderTask->handleInternalAllocationFailure();
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500323 }
Adlai Holler96ead542020-06-26 08:50:14 -0400324 this->removeRenderTasks(startIndex, stopIndex);
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500325 }
Robert Phillips4150eea2018-02-07 17:08:21 -0500326
Chris Dalton6b498102019-08-01 14:14:52 -0600327 if (this->executeRenderTasks(
328 startIndex, stopIndex, &flushState, &numRenderTasksExecuted)) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500329 flushed = true;
330 }
bsalomondc438982016-08-31 11:53:49 -0700331 }
Greg Danielc42b20b2017-10-04 10:34:49 -0400332 }
333
Chris Dalton91ab1552018-04-18 13:24:25 -0600334#ifdef SK_DEBUG
Chris Dalton6b498102019-08-01 14:14:52 -0600335 for (int i = 0; i < fDAG.numRenderTasks(); ++i) {
Adlai Holler96ead542020-06-26 08:50:14 -0400336 // All render tasks should have been cleared out by now – we only reset the array below to
337 // reclaim storage.
338 SkASSERT(!fDAG.renderTask(i));
Chris Dalton91ab1552018-04-18 13:24:25 -0600339 }
340#endif
Adlai Holler25df1f72020-06-09 13:08:27 -0400341 fLastRenderTasks.reset();
Robert Phillips22310d62018-09-05 11:07:21 -0400342 fDAG.reset();
Robert Phillips15c91422019-05-07 16:54:48 -0400343 this->clearDDLTargets();
robertphillipsa13e2022015-11-11 12:01:09 -0800344
Robert Phillipsc994a932018-06-19 13:09:54 -0400345#ifdef SK_DEBUG
346 // In non-DDL mode this checks that all the flushed ops have been freed from the memory pool.
347 // When we move to partial flushes this assert will no longer be valid.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400348 // In DDL mode this check is somewhat superfluous since the memory for most of the ops/opsTasks
Robert Phillipsc994a932018-06-19 13:09:54 -0400349 // will be stored in the DDL's GrOpMemoryPools.
Herb Derbye32e1ab2020-10-27 10:29:46 -0400350 GrMemoryPool* opMemoryPool = fContext->priv().opMemoryPool();
Robert Phillipsc994a932018-06-19 13:09:54 -0400351 opMemoryPool->isEmpty();
352#endif
353
Greg Daniel9efe3862020-06-11 11:51:06 -0400354 gpu->executeFlushInfo(proxies, numProxies, access, info, newState);
robertphillipsa13e2022015-11-11 12:01:09 -0800355
Brian Salomon57d2beab2018-09-10 09:35:41 -0400356 // Give the cache a chance to purge resources that become purgeable due to flushing.
357 if (flushed) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500358 resourceCache->purgeAsNeeded();
Brian Salomon876a0172019-03-08 11:12:14 -0500359 flushed = false;
bsalomonb77a9072016-09-07 10:02:04 -0700360 }
Chris Daltonfe199b72017-05-05 11:26:15 -0400361 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
Chris Dalton6b498102019-08-01 14:14:52 -0600362 onFlushCBObject->postFlush(fTokenTracker.nextTokenToFlush(), fFlushingRenderTaskIDs.begin(),
363 fFlushingRenderTaskIDs.count());
Brian Salomon876a0172019-03-08 11:12:14 -0500364 flushed = true;
365 }
366 if (flushed) {
367 resourceCache->purgeAsNeeded();
Chris Daltonfe199b72017-05-05 11:26:15 -0400368 }
Chris Dalton6b498102019-08-01 14:14:52 -0600369 fFlushingRenderTaskIDs.reset();
joshualittb8918c42015-12-18 09:59:46 -0800370 fFlushing = false;
Greg Daniel51316782017-08-02 15:10:09 +0000371
Greg Danielfe159622020-04-10 17:43:51 +0000372 return true;
373}
374
375bool GrDrawingManager::submitToGpu(bool syncToCpu) {
376 if (fFlushing || this->wasAbandoned()) {
377 return false;
378 }
379
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400380 auto direct = fContext->asDirectContext();
Greg Danielfe159622020-04-10 17:43:51 +0000381 if (!direct) {
382 return false; // Can't submit while DDL recording
383 }
384 GrGpu* gpu = direct->priv().getGpu();
385 return gpu->submitToGpu(syncToCpu);
robertphillips3dc6ae52015-10-20 09:54:32 -0700386}
387
Chris Dalton6b498102019-08-01 14:14:52 -0600388bool GrDrawingManager::executeRenderTasks(int startIndex, int stopIndex, GrOpFlushState* flushState,
389 int* numRenderTasksExecuted) {
390 SkASSERT(startIndex <= stopIndex && stopIndex <= fDAG.numRenderTasks());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500391
Robert Phillips27483912018-04-20 12:43:18 -0400392#if GR_FLUSH_TIME_OP_SPEW
Greg Danielf41b2bd2019-08-22 16:19:24 -0400393 SkDebugf("Flushing opsTask: %d to %d out of [%d, %d]\n",
Chris Dalton6b498102019-08-01 14:14:52 -0600394 startIndex, stopIndex, 0, fDAG.numRenderTasks());
Robert Phillips27483912018-04-20 12:43:18 -0400395 for (int i = startIndex; i < stopIndex; ++i) {
Chris Dalton6b498102019-08-01 14:14:52 -0600396 if (fDAG.renderTask(i)) {
397 fDAG.renderTask(i)->dump(true);
Robert Phillips1734dd32018-08-21 13:52:09 -0400398 }
Robert Phillips27483912018-04-20 12:43:18 -0400399 }
400#endif
401
Chris Dalton6b498102019-08-01 14:14:52 -0600402 bool anyRenderTasksExecuted = false;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500403
404 for (int i = startIndex; i < stopIndex; ++i) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400405 GrRenderTask* renderTask = fDAG.renderTask(i);
406 if (!renderTask || !renderTask->isInstantiated()) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500407 continue;
408 }
409
Chris Dalton6b498102019-08-01 14:14:52 -0600410 SkASSERT(renderTask->deferredProxiesAreInstantiated());
Robert Phillips22310d62018-09-05 11:07:21 -0400411
Chris Dalton6b498102019-08-01 14:14:52 -0600412 renderTask->prepare(flushState);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500413 }
414
415 // Upload all data to the GPU
416 flushState->preExecuteDraws();
417
Greg Danield2073452018-12-07 11:20:33 -0500418 // For Vulkan, if we have too many oplists to be flushed we end up allocating a lot of resources
419 // for each command buffer associated with the oplists. If this gets too large we can cause the
420 // devices to go OOM. In practice we usually only hit this case in our tests, but to be safe we
421 // put a cap on the number of oplists we will execute before flushing to the GPU to relieve some
422 // memory pressure.
Chris Dalton6b498102019-08-01 14:14:52 -0600423 static constexpr int kMaxRenderTasksBeforeFlush = 100;
Greg Danield2073452018-12-07 11:20:33 -0500424
Chris Daltonc4b47352019-08-23 10:10:36 -0600425 // Execute the onFlush renderTasks first, if any.
426 for (sk_sp<GrRenderTask>& onFlushRenderTask : fOnFlushRenderTasks) {
427 if (!onFlushRenderTask->execute(flushState)) {
428 SkDebugf("WARNING: onFlushRenderTask failed to execute.\n");
Robert Phillipseafd48a2017-11-16 07:52:08 -0500429 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600430 SkASSERT(onFlushRenderTask->unique());
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400431 onFlushRenderTask->disown(this);
Chris Daltonc4b47352019-08-23 10:10:36 -0600432 onFlushRenderTask = nullptr;
Chris Dalton6b498102019-08-01 14:14:52 -0600433 (*numRenderTasksExecuted)++;
434 if (*numRenderTasksExecuted >= kMaxRenderTasksBeforeFlush) {
Greg Danielfe159622020-04-10 17:43:51 +0000435 flushState->gpu()->submitToGpu(false);
Chris Dalton6b498102019-08-01 14:14:52 -0600436 *numRenderTasksExecuted = 0;
Greg Danield2073452018-12-07 11:20:33 -0500437 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500438 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600439 fOnFlushRenderTasks.reset();
Robert Phillipseafd48a2017-11-16 07:52:08 -0500440
441 // Execute the normal op lists.
442 for (int i = startIndex; i < stopIndex; ++i) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400443 GrRenderTask* renderTask = fDAG.renderTask(i);
444 if (!renderTask || !renderTask->isInstantiated()) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500445 continue;
446 }
447
Greg Daniel15ecdf92019-08-30 15:35:23 -0400448 if (renderTask->execute(flushState)) {
Chris Dalton6b498102019-08-01 14:14:52 -0600449 anyRenderTasksExecuted = true;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500450 }
Chris Dalton6b498102019-08-01 14:14:52 -0600451 (*numRenderTasksExecuted)++;
452 if (*numRenderTasksExecuted >= kMaxRenderTasksBeforeFlush) {
Greg Danielfe159622020-04-10 17:43:51 +0000453 flushState->gpu()->submitToGpu(false);
Chris Dalton6b498102019-08-01 14:14:52 -0600454 *numRenderTasksExecuted = 0;
Greg Danield2073452018-12-07 11:20:33 -0500455 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500456 }
457
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400458 SkASSERT(!flushState->opsRenderPass());
Robert Phillips40a29d72018-01-18 12:59:22 -0500459 SkASSERT(fTokenTracker.nextDrawToken() == fTokenTracker.nextTokenToFlush());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500460
Chris Dalton6b498102019-08-01 14:14:52 -0600461 // We reset the flush state before the RenderTasks so that the last resources to be freed are
462 // those that are written to in the RenderTasks. This helps to make sure the most recently used
463 // resources are the last to be purged by the resource cache.
Robert Phillipseafd48a2017-11-16 07:52:08 -0500464 flushState->reset();
465
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400466 this->removeRenderTasks(startIndex, stopIndex);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500467
Chris Dalton6b498102019-08-01 14:14:52 -0600468 return anyRenderTasksExecuted;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500469}
470
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400471void GrDrawingManager::removeRenderTasks(int startIndex, int stopIndex) {
472 for (int i = startIndex; i < stopIndex; ++i) {
473 GrRenderTask* task = fDAG.renderTask(i);
474 if (!task) {
475 continue;
476 }
477 if (!task->unique()) {
478 // TODO: Eventually this should be guaranteed unique: http://skbug.com/7111
479 task->endFlush(this);
480 }
481 task->disown(this);
482 }
Adlai Holler96ead542020-06-26 08:50:14 -0400483 fDAG.rawRemoveRenderTasks(startIndex, stopIndex);
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400484}
485
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400486static void resolve_and_mipmap(GrGpu* gpu, GrSurfaceProxy* proxy) {
487 if (!proxy->isInstantiated()) {
488 return;
489 }
490
491 // In the flushSurfaces case, we need to resolve MSAA immediately after flush. This is
492 // because clients expect the flushed surface's backing texture to be fully resolved
493 // upon return.
494 if (proxy->requiresManualMSAAResolve()) {
495 auto* rtProxy = proxy->asRenderTargetProxy();
496 SkASSERT(rtProxy);
497 if (rtProxy->isMSAADirty()) {
498 SkASSERT(rtProxy->peekRenderTarget());
Jim Van Verthbb61fe32020-07-07 16:39:04 -0400499 gpu->resolveRenderTarget(rtProxy->peekRenderTarget(), rtProxy->msaaDirtyRect());
500 gpu->submitToGpu(false);
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400501 rtProxy->markMSAAResolved();
502 }
503 }
504 // If, after a flush, any of the proxies of interest have dirty mipmaps, regenerate them in
505 // case their backend textures are being stolen.
506 // (This special case is exercised by the ReimportImageTextureWithMipLevels test.)
507 // FIXME: It may be more ideal to plumb down a "we're going to steal the backends" flag.
508 if (auto* textureProxy = proxy->asTextureProxy()) {
Brian Salomon8c82a872020-07-21 12:09:58 -0400509 if (textureProxy->mipmapsAreDirty()) {
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400510 SkASSERT(textureProxy->peekTexture());
511 gpu->regenerateMipMapLevels(textureProxy->peekTexture());
Brian Salomon8c82a872020-07-21 12:09:58 -0400512 textureProxy->markMipmapsClean();
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400513 }
514 }
515}
516
Greg Daniel9efe3862020-06-11 11:51:06 -0400517GrSemaphoresSubmitted GrDrawingManager::flushSurfaces(
518 GrSurfaceProxy* proxies[],
519 int numProxies,
520 SkSurface::BackendSurfaceAccess access,
521 const GrFlushInfo& info,
522 const GrBackendSurfaceMutableState* newState) {
bsalomon6a2b1942016-09-08 11:28:59 -0700523 if (this->wasAbandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -0400524 if (info.fSubmittedProc) {
525 info.fSubmittedProc(info.fSubmittedContext, false);
526 }
527 if (info.fFinishedProc) {
528 info.fFinishedProc(info.fFinishedContext);
529 }
Greg Daniel51316782017-08-02 15:10:09 +0000530 return GrSemaphoresSubmitted::kNo;
bsalomon6a2b1942016-09-08 11:28:59 -0700531 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400532 SkDEBUGCODE(this->validate());
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400533 SkASSERT(numProxies >= 0);
534 SkASSERT(!numProxies || proxies);
bsalomon6a2b1942016-09-08 11:28:59 -0700535
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400536 auto direct = fContext->asDirectContext();
Robert Phillips6a6de562019-02-15 15:19:15 -0500537 if (!direct) {
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 }
Robert Phillips6a6de562019-02-15 15:19:15 -0500544 return GrSemaphoresSubmitted::kNo; // Can't flush while DDL recording
545 }
546
547 GrGpu* gpu = direct->priv().getGpu();
Greg Daniel55822f12020-05-26 11:26:45 -0400548 // We have a non abandoned and direct GrContext. It must have a GrGpu.
549 SkASSERT(gpu);
Robert Phillips874b5352018-03-16 08:48:24 -0400550
Robert Phillipsacc10fa2019-04-01 09:50:20 -0400551 // TODO: It is important to upgrade the drawingmanager to just flushing the
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400552 // portion of the DAG required by 'proxies' in order to restore some of the
Robert Phillipsacc10fa2019-04-01 09:50:20 -0400553 // semantics of this method.
Greg Daniel9efe3862020-06-11 11:51:06 -0400554 bool didFlush = this->flush(proxies, numProxies, access, info, newState);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400555 for (int i = 0; i < numProxies; ++i) {
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400556 resolve_and_mipmap(gpu, proxies[i]);
bsalomon6a2b1942016-09-08 11:28:59 -0700557 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400558
559 SkDEBUGCODE(this->validate());
Greg Danielfe159622020-04-10 17:43:51 +0000560
Greg Daniel04283f32020-05-20 13:16:00 -0400561 if (!didFlush || (!direct->priv().caps()->semaphoreSupport() && info.fNumSemaphores)) {
Greg Danielfe159622020-04-10 17:43:51 +0000562 return GrSemaphoresSubmitted::kNo;
563 }
564 return GrSemaphoresSubmitted::kYes;
bsalomon6a2b1942016-09-08 11:28:59 -0700565}
566
Chris Daltonfe199b72017-05-05 11:26:15 -0400567void GrDrawingManager::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
568 fOnFlushCBObjects.push_back(onFlushCBObject);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400569}
570
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500571#if GR_TEST_UTILS
572void GrDrawingManager::testingOnly_removeOnFlushCallbackObject(GrOnFlushCallbackObject* cb) {
573 int n = std::find(fOnFlushCBObjects.begin(), fOnFlushCBObjects.end(), cb) -
574 fOnFlushCBObjects.begin();
575 SkASSERT(n < fOnFlushCBObjects.count());
576 fOnFlushCBObjects.removeShuffle(n);
577}
578#endif
579
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400580void GrDrawingManager::setLastRenderTask(const GrSurfaceProxy* proxy, GrRenderTask* task) {
581#ifdef SK_DEBUG
582 if (GrRenderTask* prior = this->getLastRenderTask(proxy)) {
583 SkASSERT(prior->isClosed());
584 }
585#endif
Adlai Holler25df1f72020-06-09 13:08:27 -0400586 uint32_t key = proxy->uniqueID().asUInt();
587 if (task) {
588 fLastRenderTasks.set(key, task);
589 } else if (fLastRenderTasks.find(key)) {
590 fLastRenderTasks.remove(key);
591 }
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400592}
593
594GrRenderTask* GrDrawingManager::getLastRenderTask(const GrSurfaceProxy* proxy) const {
595 auto entry = fLastRenderTasks.find(proxy->uniqueID().asUInt());
596 return entry ? *entry : nullptr;
597}
598
599GrOpsTask* GrDrawingManager::getLastOpsTask(const GrSurfaceProxy* proxy) const {
600 GrRenderTask* task = this->getLastRenderTask(proxy);
601 return task ? task->asOpsTask() : nullptr;
602}
603
604
Chris Dalton6b498102019-08-01 14:14:52 -0600605void GrDrawingManager::moveRenderTasksToDDL(SkDeferredDisplayList* ddl) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400606 SkDEBUGCODE(this->validate());
607
Chris Dalton6b498102019-08-01 14:14:52 -0600608 // no renderTask should receive a new command after this
Robert Phillips9da87e02019-02-04 13:26:26 -0500609 fDAG.closeAll(fContext->priv().caps());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400610 fActiveOpsTask = nullptr;
Robert Phillips7a137052018-02-01 11:23:12 -0500611
Chris Dalton6b498102019-08-01 14:14:52 -0600612 fDAG.swap(&ddl->fRenderTasks);
Robert Phillips19f466d2020-02-26 10:27:07 -0500613 SkASSERT(!fDAG.numRenderTasks());
Robert Phillips867ce8f2018-06-21 10:28:36 -0400614
Robert Phillips19f466d2020-02-26 10:27:07 -0500615 for (auto& renderTask : ddl->fRenderTasks) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400616 renderTask->disown(this);
Robert Phillips29f38542019-10-16 09:20:25 -0400617 renderTask->prePrepare(fContext);
Robert Phillips7327c9d2019-10-08 16:32:56 -0400618 }
619
Michael Ludwig2c316bd2019-12-19 14:50:44 -0500620 ddl->fArenas = std::move(fContext->priv().detachArenas());
Robert Phillips61fc7992019-10-22 11:58:17 -0400621
Robert Phillipsf6a0b452020-02-18 14:26:46 -0500622 fContext->priv().detachProgramData(&ddl->fProgramData);
Robert Phillips576b6a12019-12-06 13:05:49 -0500623
Robert Phillips774168e2018-05-31 12:43:27 -0400624 if (fPathRendererChain) {
625 if (auto ccpr = fPathRendererChain->getCoverageCountingPathRenderer()) {
626 ddl->fPendingPaths = ccpr->detachPendingPaths();
627 }
628 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400629
630 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500631}
632
Adlai Holler7580ad42020-06-24 13:45:25 -0400633void GrDrawingManager::copyRenderTasksFromDDL(sk_sp<const SkDeferredDisplayList> ddl,
634 GrRenderTargetProxy* newDest) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400635 SkDEBUGCODE(this->validate());
636
Greg Danielf41b2bd2019-08-22 16:19:24 -0400637 if (fActiveOpsTask) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400638 // This is a temporary fix for the partial-MDB world. In that world we're not
Greg Danielf41b2bd2019-08-22 16:19:24 -0400639 // reordering so ops that (in the single opsTask world) would've just glommed onto the
640 // end of the single opsTask but referred to a far earlier RT need to appear in their
641 // own opsTask.
642 fActiveOpsTask->makeClosed(*fContext->priv().caps());
643 fActiveOpsTask = nullptr;
Robert Phillips38d64b02018-09-04 13:23:26 -0400644 }
645
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400646 // Propagate the DDL proxy's state information to the replaying DDL.
647 if (ddl->priv().targetProxy()->isMSAADirty()) {
648 newDest->markMSAADirty(ddl->priv().targetProxy()->msaaDirtyRect(),
649 ddl->characterization().origin());
650 }
651 GrTextureProxy* newTextureProxy = newDest->asTextureProxy();
Brian Salomon8c82a872020-07-21 12:09:58 -0400652 if (newTextureProxy && GrMipmapped::kYes == newTextureProxy->mipmapped()) {
653 newTextureProxy->markMipmapsDirty();
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400654 }
655
656 this->addDDLTarget(newDest, ddl->priv().targetProxy());
Robert Phillips15c91422019-05-07 16:54:48 -0400657
Robert Phillips62000362018-02-01 09:10:04 -0500658 // Here we jam the proxy that backs the current replay SkSurface into the LazyProxyData.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400659 // The lazy proxy that references it (in the copied opsTasks) will steal its GrTexture.
Robert Phillips62000362018-02-01 09:10:04 -0500660 ddl->fLazyProxyData->fReplayDest = newDest;
Robert Phillips774168e2018-05-31 12:43:27 -0400661
662 if (ddl->fPendingPaths.size()) {
663 GrCoverageCountingPathRenderer* ccpr = this->getCoverageCountingPathRenderer();
664
665 ccpr->mergePendingPaths(ddl->fPendingPaths);
666 }
Robert Phillips22310d62018-09-05 11:07:21 -0400667
Chris Dalton6b498102019-08-01 14:14:52 -0600668 fDAG.add(ddl->fRenderTasks);
Robert Phillips38d64b02018-09-04 13:23:26 -0400669
Adlai Holler6c9bb622020-06-25 09:21:18 -0400670 // Add a task to unref the DDL after flush.
671 GrRenderTask* unrefTask = fDAG.add(sk_make_sp<GrUnrefDDLTask>(std::move(ddl)));
672 unrefTask->makeClosed(*fContext->priv().caps());
Adlai Holler7580ad42020-06-24 13:45:25 -0400673
Robert Phillips38d64b02018-09-04 13:23:26 -0400674 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500675}
676
Robert Phillips38d64b02018-09-04 13:23:26 -0400677#ifdef SK_DEBUG
678void GrDrawingManager::validate() const {
Adlai Holler3078f852020-11-05 15:44:50 -0500679 if (fReduceOpsTaskSplitting) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400680 SkASSERT(!fActiveOpsTask);
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400681 } else {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400682 if (fActiveOpsTask) {
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400683 SkASSERT(!fDAG.empty());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400684 SkASSERT(!fActiveOpsTask->isClosed());
685 SkASSERT(fActiveOpsTask == fDAG.back());
Robert Phillips38d64b02018-09-04 13:23:26 -0400686 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400687
Chris Dalton6b498102019-08-01 14:14:52 -0600688 for (int i = 0; i < fDAG.numRenderTasks(); ++i) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400689 if (fActiveOpsTask != fDAG.renderTask(i)) {
Chris Daltone2a903e2019-09-18 13:41:50 -0600690 // The resolveTask associated with the activeTask remains open for as long as the
691 // activeTask does.
692 bool isActiveResolveTask =
693 fActiveOpsTask && fActiveOpsTask->fTextureResolveTask == fDAG.renderTask(i);
694 SkASSERT(isActiveResolveTask || fDAG.renderTask(i)->isClosed());
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400695 }
696 }
697
698 if (!fDAG.empty() && !fDAG.back()->isClosed()) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400699 SkASSERT(fActiveOpsTask == fDAG.back());
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400700 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400701 }
702}
703#endif
704
Greg Danielbbfec9d2019-08-20 10:56:51 -0400705void GrDrawingManager::closeRenderTasksForNewRenderTask(GrSurfaceProxy* target) {
Adlai Holler3078f852020-11-05 15:44:50 -0500706 if (target && fReduceOpsTaskSplitting) {
Chris Dalton5fe99772019-08-06 11:57:39 -0600707 // In this case we need to close all the renderTasks that rely on the current contents of
708 // 'target'. That is bc we're going to update the content of the proxy so they need to be
709 // split in case they use both the old and new content. (This is a bit of an overkill: they
710 // really only need to be split if they ever reference proxy's contents again but that is
711 // hard to predict/handle).
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400712 if (GrRenderTask* lastRenderTask = this->getLastRenderTask(target)) {
Chris Dalton6b498102019-08-01 14:14:52 -0600713 lastRenderTask->closeThoseWhoDependOnMe(*fContext->priv().caps());
Robert Phillips46acf9d2018-10-09 09:31:40 -0400714 }
Greg Danielf41b2bd2019-08-22 16:19:24 -0400715 } else if (fActiveOpsTask) {
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400716 // This is a temporary fix for the partial-MDB world. In that world we're not
Greg Danielf41b2bd2019-08-22 16:19:24 -0400717 // reordering so ops that (in the single opsTask world) would've just glommed onto the
718 // end of the single opsTask but referred to a far earlier RT need to appear in their
719 // own opsTask.
720 fActiveOpsTask->makeClosed(*fContext->priv().caps());
721 fActiveOpsTask = nullptr;
robertphillips3dc6ae52015-10-20 09:54:32 -0700722 }
Chris Dalton5fe99772019-08-06 11:57:39 -0600723}
724
Greg Daniel16f5c652019-10-29 11:26:01 -0400725sk_sp<GrOpsTask> GrDrawingManager::newOpsTask(GrSurfaceProxyView surfaceView,
726 bool managedOpsTask) {
Chris Dalton5fe99772019-08-06 11:57:39 -0600727 SkDEBUGCODE(this->validate());
728 SkASSERT(fContext);
729
Greg Daniel16f5c652019-10-29 11:26:01 -0400730 GrSurfaceProxy* proxy = surfaceView.proxy();
731 this->closeRenderTasksForNewRenderTask(proxy);
robertphillips3dc6ae52015-10-20 09:54:32 -0700732
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400733 sk_sp<GrOpsTask> opsTask(new GrOpsTask(this, fContext->priv().arenas(),
Michael Ludwigd0840ec2019-12-12 09:48:38 -0500734 std::move(surfaceView),
735 fContext->priv().auditTrail()));
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400736 SkASSERT(this->getLastRenderTask(proxy) == opsTask.get());
robertphillips3dc6ae52015-10-20 09:54:32 -0700737
Greg Danielf41b2bd2019-08-22 16:19:24 -0400738 if (managedOpsTask) {
739 fDAG.add(opsTask);
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400740
Adlai Holler3078f852020-11-05 15:44:50 -0500741 if (!fReduceOpsTaskSplitting) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400742 fActiveOpsTask = opsTask.get();
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400743 }
Robert Phillips941d1442017-06-14 16:37:02 -0400744 }
robertphillips3dc6ae52015-10-20 09:54:32 -0700745
Robert Phillips38d64b02018-09-04 13:23:26 -0400746 SkDEBUGCODE(this->validate());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400747 return opsTask;
robertphillips3dc6ae52015-10-20 09:54:32 -0700748}
749
Chris Daltone2a903e2019-09-18 13:41:50 -0600750GrTextureResolveRenderTask* GrDrawingManager::newTextureResolveRenderTask(const GrCaps& caps) {
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600751 // 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 -0600752 // in sorting and opsTask reduction mode) the render tasks that depend on any proxy's current
Greg Danielf41b2bd2019-08-22 16:19:24 -0400753 // state. This is because those opsTasks can still receive new ops and because if they refer to
Chris Dalton4ece96d2019-08-30 11:26:39 -0600754 // the mipmapped version of 'proxy', they will then come to depend on the render task being
755 // created here.
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600756 //
Greg Danielf41b2bd2019-08-22 16:19:24 -0400757 // Add the new textureResolveTask before the fActiveOpsTask (if not in
758 // sorting/opsTask-splitting-reduction mode) because it will depend upon this resolve task.
Chris Dalton3d770272019-08-14 09:24:37 -0600759 // NOTE: Putting it here will also reduce the amount of work required by the topological sort.
Chris Daltone2a903e2019-09-18 13:41:50 -0600760 return static_cast<GrTextureResolveRenderTask*>(fDAG.addBeforeLast(
761 sk_make_sp<GrTextureResolveRenderTask>()));
Chris Dalton3d770272019-08-14 09:24:37 -0600762}
763
Greg Danielc30f1a92019-09-06 15:28:58 -0400764void GrDrawingManager::newWaitRenderTask(sk_sp<GrSurfaceProxy> proxy,
Greg Daniel301015c2019-11-18 14:06:46 -0500765 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> semaphores,
Greg Danielc30f1a92019-09-06 15:28:58 -0400766 int numSemaphores) {
767 SkDEBUGCODE(this->validate());
768 SkASSERT(fContext);
769
770 const GrCaps& caps = *fContext->priv().caps();
771
Greg Daniel16f5c652019-10-29 11:26:01 -0400772 sk_sp<GrWaitRenderTask> waitTask = sk_make_sp<GrWaitRenderTask>(GrSurfaceProxyView(proxy),
773 std::move(semaphores),
Greg Danielc30f1a92019-09-06 15:28:58 -0400774 numSemaphores);
775 if (fReduceOpsTaskSplitting) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400776 GrRenderTask* lastTask = this->getLastRenderTask(proxy.get());
Greg Danielc30f1a92019-09-06 15:28:58 -0400777 if (lastTask && !lastTask->isClosed()) {
778 // We directly make the currently open renderTask depend on waitTask instead of using
779 // the proxy version of addDependency. The waitTask will never need to trigger any
780 // resolves or mip map generation which is the main advantage of going through the proxy
781 // version. Additionally we would've had to temporarily set the wait task as the
782 // lastRenderTask on the proxy, add the dependency, and then reset the lastRenderTask to
783 // lastTask. Additionally we add all dependencies of lastTask to waitTask so that the
784 // waitTask doesn't get reordered before them and unnecessarily block those tasks.
785 // Note: Any previous Ops already in lastTask will get blocked by the wait semaphore
786 // even though they don't need to be for correctness.
787
788 // Make sure we add the dependencies of lastTask to waitTask first or else we'll get a
789 // circular self dependency of waitTask on waitTask.
790 waitTask->addDependenciesFromOtherTask(lastTask);
791 lastTask->addDependency(waitTask.get());
792 } else {
793 // If there is a last task we set the waitTask to depend on it so that it doesn't get
794 // reordered in front of the lastTask causing the lastTask to be blocked by the
795 // semaphore. Again we directly just go through adding the dependency to the task and
796 // not the proxy since we don't need to worry about resolving anything.
797 if (lastTask) {
798 waitTask->addDependency(lastTask);
799 }
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400800 this->setLastRenderTask(proxy.get(), waitTask.get());
Greg Danielc30f1a92019-09-06 15:28:58 -0400801 }
802 fDAG.add(waitTask);
803 } else {
Adlai Holler33d569e2020-06-16 14:30:08 -0400804 if (fActiveOpsTask && (fActiveOpsTask->target(0).proxy() == proxy.get())) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400805 SkASSERT(this->getLastRenderTask(proxy.get()) == fActiveOpsTask);
Greg Danielc30f1a92019-09-06 15:28:58 -0400806 fDAG.addBeforeLast(waitTask);
807 // In this case we keep the current renderTask open but just insert the new waitTask
808 // before it in the list. The waitTask will never need to trigger any resolves or mip
809 // map generation which is the main advantage of going through the proxy version.
810 // Additionally we would've had to temporarily set the wait task as the lastRenderTask
811 // on the proxy, add the dependency, and then reset the lastRenderTask to
812 // fActiveOpsTask. Additionally we make the waitTask depend on all of fActiveOpsTask
813 // dependencies so that we don't unnecessarily reorder the waitTask before them.
814 // Note: Any previous Ops already in fActiveOpsTask will get blocked by the wait
815 // semaphore even though they don't need to be for correctness.
816
817 // Make sure we add the dependencies of fActiveOpsTask to waitTask first or else we'll
818 // get a circular self dependency of waitTask on waitTask.
819 waitTask->addDependenciesFromOtherTask(fActiveOpsTask);
820 fActiveOpsTask->addDependency(waitTask.get());
821 } else {
822 // In this case we just close the previous RenderTask and start and append the waitTask
823 // to the DAG. Since it is the last task now we call setLastRenderTask on the proxy. If
824 // there is a lastTask on the proxy we make waitTask depend on that task. This
825 // dependency isn't strictly needed but it does keep the DAG from reordering the
826 // waitTask earlier and blocking more tasks.
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400827 if (GrRenderTask* lastTask = this->getLastRenderTask(proxy.get())) {
Greg Danielc30f1a92019-09-06 15:28:58 -0400828 waitTask->addDependency(lastTask);
829 }
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400830 this->setLastRenderTask(proxy.get(), waitTask.get());
Greg Danielc30f1a92019-09-06 15:28:58 -0400831 this->closeRenderTasksForNewRenderTask(proxy.get());
832 fDAG.add(waitTask);
833 }
834 }
835 waitTask->makeClosed(caps);
836
837 SkDEBUGCODE(this->validate());
838}
839
Greg Danielbbfec9d2019-08-20 10:56:51 -0400840void GrDrawingManager::newTransferFromRenderTask(sk_sp<GrSurfaceProxy> srcProxy,
841 const SkIRect& srcRect,
842 GrColorType surfaceColorType,
843 GrColorType dstColorType,
844 sk_sp<GrGpuBuffer> dstBuffer,
845 size_t dstOffset) {
846 SkDEBUGCODE(this->validate());
847 SkASSERT(fContext);
848 // This copies from srcProxy to dstBuffer so it doesn't have a real target.
849 this->closeRenderTasksForNewRenderTask(nullptr);
850
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600851 GrRenderTask* task = fDAG.add(sk_make_sp<GrTransferFromRenderTask>(
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400852 srcProxy, srcRect, surfaceColorType, dstColorType,
853 std::move(dstBuffer), dstOffset));
Greg Danielbbfec9d2019-08-20 10:56:51 -0400854
855 const GrCaps& caps = *fContext->priv().caps();
856
Brian Salomon7e67dca2020-07-21 09:27:25 -0400857 // We always say GrMipmapped::kNo here since we are always just copying from the base layer. We
Greg Danielbbfec9d2019-08-20 10:56:51 -0400858 // don't need to make sure the whole mip map chain is valid.
Brian Salomon7e67dca2020-07-21 09:27:25 -0400859 task->addDependency(this, srcProxy.get(), GrMipmapped::kNo,
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400860 GrTextureResolveManager(this), caps);
Greg Danielbbfec9d2019-08-20 10:56:51 -0400861 task->makeClosed(caps);
862
Greg Danielbbfec9d2019-08-20 10:56:51 -0400863 // We have closed the previous active oplist but since a new oplist isn't being added there
864 // shouldn't be an active one.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400865 SkASSERT(!fActiveOpsTask);
Greg Danielbbfec9d2019-08-20 10:56:51 -0400866 SkDEBUGCODE(this->validate());
867}
868
Greg Daniel16f5c652019-10-29 11:26:01 -0400869bool GrDrawingManager::newCopyRenderTask(GrSurfaceProxyView srcView,
Greg Daniele227fe42019-08-21 13:52:24 -0400870 const SkIRect& srcRect,
Greg Daniel16f5c652019-10-29 11:26:01 -0400871 GrSurfaceProxyView dstView,
Greg Daniele227fe42019-08-21 13:52:24 -0400872 const SkIPoint& dstPoint) {
873 SkDEBUGCODE(this->validate());
874 SkASSERT(fContext);
Greg Daniele227fe42019-08-21 13:52:24 -0400875
Greg Daniel16f5c652019-10-29 11:26:01 -0400876 this->closeRenderTasksForNewRenderTask(dstView.proxy());
Brian Salomone4bce012019-09-20 15:34:23 -0400877 const GrCaps& caps = *fContext->priv().caps();
878
Greg Daniel16f5c652019-10-29 11:26:01 -0400879 GrSurfaceProxy* srcProxy = srcView.proxy();
880
Brian Salomone4bce012019-09-20 15:34:23 -0400881 GrRenderTask* task =
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400882 fDAG.add(GrCopyRenderTask::Make(this, std::move(srcView), srcRect, std::move(dstView),
Greg Daniel16f5c652019-10-29 11:26:01 -0400883 dstPoint, &caps));
Greg Daniele227fe42019-08-21 13:52:24 -0400884 if (!task) {
885 return false;
886 }
887
Brian Salomon7e67dca2020-07-21 09:27:25 -0400888 // We always say GrMipmapped::kNo here since we are always just copying from the base layer to
Greg Daniele227fe42019-08-21 13:52:24 -0400889 // another base layer. We don't need to make sure the whole mip map chain is valid.
Brian Salomon7e67dca2020-07-21 09:27:25 -0400890 task->addDependency(this, srcProxy, GrMipmapped::kNo, GrTextureResolveManager(this), caps);
Greg Daniele227fe42019-08-21 13:52:24 -0400891 task->makeClosed(caps);
892
Greg Daniele227fe42019-08-21 13:52:24 -0400893 // We have closed the previous active oplist but since a new oplist isn't being added there
894 // shouldn't be an active one.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400895 SkASSERT(!fActiveOpsTask);
Greg Daniele227fe42019-08-21 13:52:24 -0400896 SkDEBUGCODE(this->validate());
897 return true;
898}
899
robertphillips68737822015-10-29 12:12:21 -0700900/*
901 * This method finds a path renderer that can draw the specified path on
902 * the provided target.
903 * Due to its expense, the software path renderer has split out so it can
904 * can be individually allowed/disallowed via the "allowSW" boolean.
905 */
906GrPathRenderer* GrDrawingManager::getPathRenderer(const GrPathRenderer::CanDrawPathArgs& args,
907 bool allowSW,
908 GrPathRendererChain::DrawType drawType,
909 GrPathRenderer::StencilSupport* stencilSupport) {
910
911 if (!fPathRendererChain) {
John Stilesfbd050b2020-08-03 13:21:46 -0400912 fPathRendererChain =
913 std::make_unique<GrPathRendererChain>(fContext, fOptionsForPathRendererChain);
robertphillips68737822015-10-29 12:12:21 -0700914 }
915
916 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(args, drawType, stencilSupport);
917 if (!pr && allowSW) {
Brian Salomone7df0bb2018-05-07 14:44:57 -0400918 auto swPR = this->getSoftwarePathRenderer();
919 if (GrPathRenderer::CanDrawPath::kNo != swPR->canDrawPath(args)) {
920 pr = swPR;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500921 }
robertphillips68737822015-10-29 12:12:21 -0700922 }
923
Robert Phillipsd81379d2020-04-21 10:39:02 -0400924#if GR_PATH_RENDERER_SPEW
925 if (pr) {
926 SkDebugf("getPathRenderer: %s\n", pr->name());
927 }
928#endif
929
robertphillips68737822015-10-29 12:12:21 -0700930 return pr;
931}
932
Brian Salomone7df0bb2018-05-07 14:44:57 -0400933GrPathRenderer* GrDrawingManager::getSoftwarePathRenderer() {
934 if (!fSoftwarePathRenderer) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400935 fSoftwarePathRenderer.reset(
Robert Phillips9da87e02019-02-04 13:26:26 -0500936 new GrSoftwarePathRenderer(fContext->priv().proxyProvider(),
Ben Wagner9ec70c62018-07-12 13:30:47 -0400937 fOptionsForPathRendererChain.fAllowPathMaskCaching));
Brian Salomone7df0bb2018-05-07 14:44:57 -0400938 }
Ben Wagner9ec70c62018-07-12 13:30:47 -0400939 return fSoftwarePathRenderer.get();
Brian Salomone7df0bb2018-05-07 14:44:57 -0400940}
941
Chris Daltonfddb6c02017-11-04 15:22:22 -0600942GrCoverageCountingPathRenderer* GrDrawingManager::getCoverageCountingPathRenderer() {
943 if (!fPathRendererChain) {
John Stilesfbd050b2020-08-03 13:21:46 -0400944 fPathRendererChain = std::make_unique<GrPathRendererChain>(fContext, fOptionsForPathRendererChain);
Chris Daltonfddb6c02017-11-04 15:22:22 -0600945 }
946 return fPathRendererChain->getCoverageCountingPathRenderer();
947}
948
Brian Salomon653f42f2018-07-10 10:07:31 -0400949void GrDrawingManager::flushIfNecessary() {
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400950 auto direct = fContext->asDirectContext();
Robert Phillips6a6de562019-02-15 15:19:15 -0500951 if (!direct) {
952 return;
953 }
954
955 auto resourceCache = direct->priv().getResourceCache();
Brian Salomon653f42f2018-07-10 10:07:31 -0400956 if (resourceCache && resourceCache->requestsFlush()) {
Greg Daniel9efe3862020-06-11 11:51:06 -0400957 if (this->flush(nullptr, 0, SkSurface::BackendSurfaceAccess::kNoAccess, GrFlushInfo(),
958 nullptr)) {
Greg Danielfe159622020-04-10 17:43:51 +0000959 this->submitToGpu(false);
960 }
Brian Salomon57d2beab2018-09-10 09:35:41 -0400961 resourceCache->purgeAsNeeded();
Brian Salomon653f42f2018-07-10 10:07:31 -0400962 }
963}
964