blob: 67f337381af11e2045c5d5cf8195544dccfd9d7a [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
Robert Phillips4d5594d2020-02-21 14:24:40 -050010#include "include/core/SkDeferredDisplayList.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/gpu/GrBackendSemaphore.h"
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040012#include "include/gpu/GrDirectContext.h"
13#include "include/gpu/GrRecordingContext.h"
Robert Phillips7eb0a5f2020-06-09 14:15:09 -040014#include "src/core/SkDeferredDisplayListPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/core/SkTTopoSort.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040016#include "src/gpu/GrAuditTrail.h"
Brian Salomon9241a6d2019-10-03 13:26:54 -040017#include "src/gpu/GrClientMappedBufferManager.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrContextPriv.h"
Greg Daniele227fe42019-08-21 13:52:24 -040019#include "src/gpu/GrCopyRenderTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/GrGpu.h"
21#include "src/gpu/GrMemoryPool.h"
22#include "src/gpu/GrOnFlushResourceProvider.h"
23#include "src/gpu/GrRecordingContextPriv.h"
24#include "src/gpu/GrRenderTargetContext.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040025#include "src/gpu/GrRenderTargetProxy.h"
Chris Dalton6b498102019-08-01 14:14:52 -060026#include "src/gpu/GrRenderTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/GrResourceAllocator.h"
28#include "src/gpu/GrResourceProvider.h"
29#include "src/gpu/GrSoftwarePathRenderer.h"
Greg Daniel46e366a2019-12-16 14:38:36 -050030#include "src/gpu/GrSurfaceContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "src/gpu/GrSurfaceProxyPriv.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000032#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050033#include "src/gpu/GrTexturePriv.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040034#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050035#include "src/gpu/GrTextureProxyPriv.h"
Chris Dalton3d770272019-08-14 09:24:37 -060036#include "src/gpu/GrTextureResolveRenderTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050037#include "src/gpu/GrTracing.h"
Greg Danielbbfec9d2019-08-20 10:56:51 -040038#include "src/gpu/GrTransferFromRenderTask.h"
Adlai Holler6c9bb622020-06-25 09:21:18 -040039#include "src/gpu/GrUnrefDDLTask.h"
Greg Danielc30f1a92019-09-06 15:28:58 -040040#include "src/gpu/GrWaitRenderTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050041#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
Herb Derbya08bde62020-06-12 15:46:06 -040042#include "src/gpu/text/GrSDFTOptions.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050043#include "src/image/SkSurface_Gpu.h"
robertphillips498d7ac2015-10-30 10:11:30 -070044
Chris Dalton6b498102019-08-01 14:14:52 -060045GrDrawingManager::RenderTaskDAG::RenderTaskDAG(bool sortRenderTasks)
46 : fSortRenderTasks(sortRenderTasks) {}
Robert Phillipsa3f70262018-02-08 10:59:38 -050047
Chris Dalton6b498102019-08-01 14:14:52 -060048GrDrawingManager::RenderTaskDAG::~RenderTaskDAG() {}
Robert Phillips22310d62018-09-05 11:07:21 -040049
Chris Dalton6b498102019-08-01 14:14:52 -060050void GrDrawingManager::RenderTaskDAG::gatherIDs(SkSTArray<8, uint32_t, true>* idArray) const {
51 idArray->reset(fRenderTasks.count());
52 for (int i = 0; i < fRenderTasks.count(); ++i) {
53 if (fRenderTasks[i]) {
54 (*idArray)[i] = fRenderTasks[i]->uniqueID();
Robert Phillips22310d62018-09-05 11:07:21 -040055 }
56 }
57}
58
Chris Dalton6b498102019-08-01 14:14:52 -060059void GrDrawingManager::RenderTaskDAG::reset() {
60 fRenderTasks.reset();
Robert Phillips22310d62018-09-05 11:07:21 -040061}
62
Adlai Holler96ead542020-06-26 08:50:14 -040063void GrDrawingManager::RenderTaskDAG::rawRemoveRenderTasks(int startIndex, int stopIndex) {
Robert Phillips22310d62018-09-05 11:07:21 -040064 for (int i = startIndex; i < stopIndex; ++i) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -040065 fRenderTasks[i] = nullptr;
Robert Phillips22310d62018-09-05 11:07:21 -040066 }
67}
68
Chris Dalton6b498102019-08-01 14:14:52 -060069bool GrDrawingManager::RenderTaskDAG::isUsed(GrSurfaceProxy* proxy) const {
Adlai Holler33d569e2020-06-16 14:30:08 -040070 for (const auto& task : fRenderTasks) {
71 if (task && task->isUsed(proxy)) {
Robert Phillips9313aa72019-04-09 18:41:27 -040072 return true;
73 }
74 }
75
76 return false;
77}
78
Chris Dalton3d770272019-08-14 09:24:37 -060079GrRenderTask* GrDrawingManager::RenderTaskDAG::add(sk_sp<GrRenderTask> renderTask) {
Chris Dalton6aeb8e82019-08-27 11:52:19 -060080 if (renderTask) {
81 return fRenderTasks.emplace_back(std::move(renderTask)).get();
82 }
83 return nullptr;
Chris Dalton3d770272019-08-14 09:24:37 -060084}
85
86GrRenderTask* GrDrawingManager::RenderTaskDAG::addBeforeLast(sk_sp<GrRenderTask> renderTask) {
87 SkASSERT(!fRenderTasks.empty());
Chris Dalton6aeb8e82019-08-27 11:52:19 -060088 if (renderTask) {
89 // Release 'fRenderTasks.back()' and grab the raw pointer, in case the SkTArray grows
90 // and reallocates during emplace_back.
91 fRenderTasks.emplace_back(fRenderTasks.back().release());
92 return (fRenderTasks[fRenderTasks.count() - 2] = std::move(renderTask)).get();
93 }
94 return nullptr;
Robert Phillips22310d62018-09-05 11:07:21 -040095}
96
Greg Danielf41b2bd2019-08-22 16:19:24 -040097void GrDrawingManager::RenderTaskDAG::add(const SkTArray<sk_sp<GrRenderTask>>& renderTasks) {
Robert Phillips19f466d2020-02-26 10:27:07 -050098#ifdef SK_DEBUG
99 for (auto& renderTask : renderTasks) {
100 SkASSERT(renderTask->unique());
101 }
102#endif
103
Greg Danielf41b2bd2019-08-22 16:19:24 -0400104 fRenderTasks.push_back_n(renderTasks.count(), renderTasks.begin());
Robert Phillips22310d62018-09-05 11:07:21 -0400105}
106
Greg Danielf41b2bd2019-08-22 16:19:24 -0400107void GrDrawingManager::RenderTaskDAG::swap(SkTArray<sk_sp<GrRenderTask>>* renderTasks) {
108 SkASSERT(renderTasks->empty());
109 renderTasks->swap(fRenderTasks);
Robert Phillips22310d62018-09-05 11:07:21 -0400110}
111
Chris Dalton6b498102019-08-01 14:14:52 -0600112void GrDrawingManager::RenderTaskDAG::prepForFlush() {
113 if (fSortRenderTasks) {
114 SkDEBUGCODE(bool result =) SkTTopoSort<GrRenderTask, GrRenderTask::TopoSortTraits>(
115 &fRenderTasks);
Robert Phillips22310d62018-09-05 11:07:21 -0400116 SkASSERT(result);
117 }
118
119#ifdef SK_DEBUG
Greg Danielf41b2bd2019-08-22 16:19:24 -0400120 // This block checks for any unnecessary splits in the opsTasks. If two sequential opsTasks
121 // share the same backing GrSurfaceProxy it means the opsTask was artificially split.
Chris Dalton6b498102019-08-01 14:14:52 -0600122 if (fRenderTasks.count()) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400123 GrOpsTask* prevOpsTask = fRenderTasks[0]->asOpsTask();
Chris Dalton6b498102019-08-01 14:14:52 -0600124 for (int i = 1; i < fRenderTasks.count(); ++i) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400125 GrOpsTask* curOpsTask = fRenderTasks[i]->asOpsTask();
Robert Phillips22310d62018-09-05 11:07:21 -0400126
Greg Danielf41b2bd2019-08-22 16:19:24 -0400127 if (prevOpsTask && curOpsTask) {
Adlai Holler33d569e2020-06-16 14:30:08 -0400128 SkASSERT(prevOpsTask->target(0).proxy() != curOpsTask->target(0).proxy());
Robert Phillips22310d62018-09-05 11:07:21 -0400129 }
130
Greg Danielf41b2bd2019-08-22 16:19:24 -0400131 prevOpsTask = curOpsTask;
Robert Phillips22310d62018-09-05 11:07:21 -0400132 }
133 }
134#endif
135}
136
Chris Dalton6b498102019-08-01 14:14:52 -0600137void GrDrawingManager::RenderTaskDAG::closeAll(const GrCaps* caps) {
Adlai Holler96ead542020-06-26 08:50:14 -0400138 for (auto& task : fRenderTasks) {
139 if (task) {
140 task->makeClosed(*caps);
Robert Phillips22310d62018-09-05 11:07:21 -0400141 }
142 }
143}
144
Robert Phillips22310d62018-09-05 11:07:21 -0400145///////////////////////////////////////////////////////////////////////////////////////////////////
Robert Phillips69893702019-02-22 11:16:30 -0500146GrDrawingManager::GrDrawingManager(GrRecordingContext* context,
Robert Phillips22310d62018-09-05 11:07:21 -0400147 const GrPathRendererChain::Options& optionsForPathRendererChain,
Chris Dalton6b498102019-08-01 14:14:52 -0600148 bool sortRenderTasks,
Greg Danielf41b2bd2019-08-22 16:19:24 -0400149 bool reduceOpsTaskSplitting)
Robert Phillips22310d62018-09-05 11:07:21 -0400150 : fContext(context)
151 , fOptionsForPathRendererChain(optionsForPathRendererChain)
Chris Dalton6b498102019-08-01 14:14:52 -0600152 , fDAG(sortRenderTasks)
Robert Phillips22310d62018-09-05 11:07:21 -0400153 , fPathRendererChain(nullptr)
154 , fSoftwarePathRenderer(nullptr)
Robert Phillips6db27c22019-05-01 10:43:56 -0400155 , fFlushing(false)
Herb Derby082232b2020-06-10 15:08:18 -0400156 , fReduceOpsTaskSplitting(reduceOpsTaskSplitting) { }
Robert Phillips22310d62018-09-05 11:07:21 -0400157
robertphillips3dc6ae52015-10-20 09:54:32 -0700158GrDrawingManager::~GrDrawingManager() {
Adlai Holler96ead542020-06-26 08:50:14 -0400159 fDAG.closeAll(fContext->priv().caps());
160 this->removeRenderTasks(0, fDAG.numRenderTasks());
robertphillips3dc6ae52015-10-20 09:54:32 -0700161}
162
Robert Phillipsa9162df2019-02-11 14:12:03 -0500163bool GrDrawingManager::wasAbandoned() const {
Robert Phillips9eb00022020-06-30 15:30:12 -0400164 return fContext->abandoned();
robertphillips3dc6ae52015-10-20 09:54:32 -0700165}
166
robertphillips68737822015-10-29 12:12:21 -0700167void GrDrawingManager::freeGpuResources() {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400168 for (int i = fOnFlushCBObjects.count() - 1; i >= 0; --i) {
169 if (!fOnFlushCBObjects[i]->retainOnFreeGpuResources()) {
170 // it's safe to just do this because we're iterating in reverse
171 fOnFlushCBObjects.removeShuffle(i);
172 }
173 }
174
robertphillips68737822015-10-29 12:12:21 -0700175 // a path renderer may be holding onto resources
robertphillips13391dd2015-10-30 05:15:11 -0700176 fPathRendererChain = nullptr;
Ben Wagner9ec70c62018-07-12 13:30:47 -0400177 fSoftwarePathRenderer = nullptr;
Robert Phillipse3302df2017-04-24 07:31:02 -0400178}
179
Robert Phillips7ee385e2017-03-30 08:02:11 -0400180// MDB TODO: make use of the 'proxy' parameter.
Greg Daniel9efe3862020-06-11 11:51:06 -0400181bool GrDrawingManager::flush(
182 GrSurfaceProxy* proxies[],
183 int numProxies,
184 SkSurface::BackendSurfaceAccess access,
185 const GrFlushInfo& info,
186 const GrBackendSurfaceMutableState* newState) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400187 SkASSERT(numProxies >= 0);
188 SkASSERT(!numProxies || proxies);
Brian Salomon57d2beab2018-09-10 09:35:41 -0400189 GR_CREATE_TRACE_MARKER_CONTEXT("GrDrawingManager", "flush", fContext);
Brian Salomondcbb9d92017-07-19 10:53:20 -0400190
robertphillips7761d612016-05-16 09:14:53 -0700191 if (fFlushing || this->wasAbandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -0400192 if (info.fSubmittedProc) {
193 info.fSubmittedProc(info.fSubmittedContext, false);
194 }
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400195 if (info.fFinishedProc) {
196 info.fFinishedProc(info.fFinishedContext);
Greg Daniela3aa75a2019-04-12 14:24:55 -0400197 }
Greg Danielfe159622020-04-10 17:43:51 +0000198 return false;
joshualittb8918c42015-12-18 09:59:46 -0800199 }
Robert Phillips602df412019-04-08 11:10:39 -0400200
Robert Phillips38d64b02018-09-04 13:23:26 -0400201 SkDEBUGCODE(this->validate());
202
Greg Danielce9f0162020-06-30 13:42:46 -0400203 if (!info.fNumSemaphores && !info.fFinishedProc &&
204 access == SkSurface::BackendSurfaceAccess::kNoAccess && !newState) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400205 bool canSkip = numProxies > 0;
206 for (int i = 0; i < numProxies && canSkip; ++i) {
207 canSkip = !fDAG.isUsed(proxies[i]) && !this->isDDLTarget(proxies[i]);
208 }
209 if (canSkip) {
Greg Daniel55822f12020-05-26 11:26:45 -0400210 if (info.fSubmittedProc) {
211 info.fSubmittedProc(info.fSubmittedContext, true);
212 }
Greg Danielfe159622020-04-10 17:43:51 +0000213 return false;
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400214 }
Robert Phillips9313aa72019-04-09 18:41:27 -0400215 }
216
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400217 auto direct = fContext->asDirectContext();
Robert Phillips6a6de562019-02-15 15:19:15 -0500218 if (!direct) {
Greg Daniel55822f12020-05-26 11:26:45 -0400219 if (info.fSubmittedProc) {
220 info.fSubmittedProc(info.fSubmittedContext, false);
221 }
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400222 if (info.fFinishedProc) {
223 info.fFinishedProc(info.fFinishedContext);
Greg Daniela3aa75a2019-04-12 14:24:55 -0400224 }
Greg Danielfe159622020-04-10 17:43:51 +0000225 return false; // Can't flush while DDL recording
Robert Phillips6a6de562019-02-15 15:19:15 -0500226 }
Brian Salomon9241a6d2019-10-03 13:26:54 -0400227 direct->priv().clientMappedBufferManager()->process();
Robert Phillips6a6de562019-02-15 15:19:15 -0500228
229 GrGpu* gpu = direct->priv().getGpu();
Greg Daniel55822f12020-05-26 11:26:45 -0400230 // We have a non abandoned and direct GrContext. It must have a GrGpu.
231 SkASSERT(gpu);
Greg Daniela3aa75a2019-04-12 14:24:55 -0400232
joshualittb8918c42015-12-18 09:59:46 -0800233 fFlushing = true;
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400234
Robert Phillips6a6de562019-02-15 15:19:15 -0500235 auto resourceProvider = direct->priv().resourceProvider();
236 auto resourceCache = direct->priv().getResourceCache();
237
Chris Dalton6b498102019-08-01 14:14:52 -0600238 // Semi-usually the GrRenderTasks are already closed at this point, but sometimes Ganesh needs
Greg Danielf41b2bd2019-08-22 16:19:24 -0400239 // to flush mid-draw. In that case, the SkGpuDevice's opsTasks won't be closed but need to be
240 // flushed anyway. Closing such opsTasks here will mean new ones will be created to replace them
Chris Dalton6b498102019-08-01 14:14:52 -0600241 // if the SkGpuDevice(s) write to them again.
Robert Phillips9da87e02019-02-04 13:26:26 -0500242 fDAG.closeAll(fContext->priv().caps());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400243 fActiveOpsTask = nullptr;
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400244
Robert Phillips22310d62018-09-05 11:07:21 -0400245 fDAG.prepForFlush();
Brian Salomon601ac802019-02-07 13:37:16 -0500246 if (!fCpuBufferCache) {
247 // We cache more buffers when the backend is using client side arrays. Otherwise, we
248 // expect each pool will use a CPU buffer as a staging buffer before uploading to a GPU
249 // buffer object. Each pool only requires one staging buffer at a time.
250 int maxCachedBuffers = fContext->priv().caps()->preferClientSideDynamicBuffers() ? 2 : 6;
251 fCpuBufferCache = GrBufferAllocPool::CpuBufferCache::Make(maxCachedBuffers);
Brian Salomon58f153c2018-10-18 21:51:15 -0400252 }
Robert Phillipsa4c93ac2017-05-18 11:40:04 -0400253
Robert Phillipse5f73282019-06-18 17:15:04 -0400254 GrOpFlushState flushState(gpu, resourceProvider, &fTokenTracker, fCpuBufferCache);
Robert Phillips40a29d72018-01-18 12:59:22 -0500255
Chris Daltonfe199b72017-05-05 11:26:15 -0400256 GrOnFlushResourceProvider onFlushProvider(this);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400257
Chris Dalton12658942017-10-05 19:45:25 -0600258 // Prepare any onFlush op lists (e.g. atlases).
Chris Daltonfe199b72017-05-05 11:26:15 -0400259 if (!fOnFlushCBObjects.empty()) {
Chris Dalton6b498102019-08-01 14:14:52 -0600260 fDAG.gatherIDs(&fFlushingRenderTaskIDs);
Robert Phillips22310d62018-09-05 11:07:21 -0400261
Chris Daltonfe199b72017-05-05 11:26:15 -0400262 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
Chris Dalton6b498102019-08-01 14:14:52 -0600263 onFlushCBObject->preFlush(&onFlushProvider, fFlushingRenderTaskIDs.begin(),
Chris Daltonc4b47352019-08-23 10:10:36 -0600264 fFlushingRenderTaskIDs.count());
265 }
266 for (const auto& onFlushRenderTask : fOnFlushRenderTasks) {
267 onFlushRenderTask->makeClosed(*fContext->priv().caps());
Chris Dalton706a6ff2017-11-29 22:01:06 -0700268#ifdef SK_DEBUG
Chris Daltonc4b47352019-08-23 10:10:36 -0600269 // OnFlush callbacks are invoked during flush, and are therefore expected to handle
270 // resource allocation & usage on their own. (No deferred or lazy proxies!)
271 onFlushRenderTask->visitTargetAndSrcProxies_debugOnly(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400272 [](GrSurfaceProxy* p, GrMipmapped mipMapped) {
Chris Daltonc4b47352019-08-23 10:10:36 -0600273 SkASSERT(!p->asTextureProxy() || !p->asTextureProxy()->texPriv().isDeferred());
Brian Salomonbeb7f522019-08-30 16:19:42 -0400274 SkASSERT(!p->isLazy());
Chris Dalton4ece96d2019-08-30 11:26:39 -0600275 if (p->requiresManualMSAAResolve()) {
276 // The onFlush callback is responsible for ensuring MSAA gets resolved.
277 SkASSERT(p->asRenderTargetProxy() && !p->asRenderTargetProxy()->isMSAADirty());
278 }
Brian Salomon7e67dca2020-07-21 09:27:25 -0400279 if (GrMipmapped::kYes == mipMapped) {
Chris Daltonc4b47352019-08-23 10:10:36 -0600280 // The onFlush callback is responsible for regenerating mips if needed.
Brian Salomon8c82a872020-07-21 12:09:58 -0400281 SkASSERT(p->asTextureProxy() && !p->asTextureProxy()->mipmapsAreDirty());
Chris Daltonc4b47352019-08-23 10:10:36 -0600282 }
283 });
Chris Dalton706a6ff2017-11-29 22:01:06 -0700284#endif
Chris Daltonc4b47352019-08-23 10:10:36 -0600285 onFlushRenderTask->prepare(&flushState);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400286 }
287 }
288
robertphillipsa13e2022015-11-11 12:01:09 -0800289#if 0
Brian Salomon09d994e2016-12-21 11:14:46 -0500290 // Enable this to print out verbose GrOp information
Chris Daltonc4b47352019-08-23 10:10:36 -0600291 SkDEBUGCODE(SkDebugf("onFlush renderTasks:"));
292 for (const auto& onFlushRenderTask : fOnFlushRenderTasks) {
293 SkDEBUGCODE(onFlushRenderTask->dump();)
294 }
295 SkDEBUGCODE(SkDebugf("Normal renderTasks:"));
Chris Dalton6b498102019-08-01 14:14:52 -0600296 for (int i = 0; i < fRenderTasks.count(); ++i) {
297 SkDEBUGCODE(fRenderTasks[i]->dump();)
robertphillips3dc6ae52015-10-20 09:54:32 -0700298 }
robertphillipsa13e2022015-11-11 12:01:09 -0800299#endif
300
Robert Phillipseafd48a2017-11-16 07:52:08 -0500301 int startIndex, stopIndex;
302 bool flushed = false;
303
Robert Phillipsf8e25022017-11-08 15:24:31 -0500304 {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400305 GrResourceAllocator alloc(resourceProvider SkDEBUGCODE(, fDAG.numRenderTasks()));
Chris Dalton6b498102019-08-01 14:14:52 -0600306 for (int i = 0; i < fDAG.numRenderTasks(); ++i) {
307 if (fDAG.renderTask(i)) {
308 fDAG.renderTask(i)->gatherProxyIntervals(&alloc);
Robert Phillips22310d62018-09-05 11:07:21 -0400309 }
Greg Danielf41b2bd2019-08-22 16:19:24 -0400310 alloc.markEndOfOpsTask(i);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500311 }
Robert Phillipsc73666f2019-04-24 08:49:48 -0400312 alloc.determineRecyclability();
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400313
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500314 GrResourceAllocator::AssignError error = GrResourceAllocator::AssignError::kNoError;
Chris Dalton6b498102019-08-01 14:14:52 -0600315 int numRenderTasksExecuted = 0;
Brian Salomon577aa0f2018-11-30 13:32:23 -0500316 while (alloc.assign(&startIndex, &stopIndex, &error)) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500317 if (GrResourceAllocator::AssignError::kFailedProxyInstantiation == error) {
318 for (int i = startIndex; i < stopIndex; ++i) {
Chris Dalton6b498102019-08-01 14:14:52 -0600319 GrRenderTask* renderTask = fDAG.renderTask(i);
320 if (!renderTask) {
321 continue;
Robert Phillips01a91282018-07-26 08:03:04 -0400322 }
Chris Dalton6b498102019-08-01 14:14:52 -0600323 if (!renderTask->isInstantiated()) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400324 // No need to call the renderTask's handleInternalAllocationFailure
325 // since we will already skip executing the renderTask since it is not
326 // instantiated.
Chris Dalton6b498102019-08-01 14:14:52 -0600327 continue;
Robert Phillipsbbcb7f72018-05-31 11:16:19 -0400328 }
Chris Dalton6b498102019-08-01 14:14:52 -0600329 renderTask->handleInternalAllocationFailure();
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500330 }
Adlai Holler96ead542020-06-26 08:50:14 -0400331 this->removeRenderTasks(startIndex, stopIndex);
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500332 }
Robert Phillips4150eea2018-02-07 17:08:21 -0500333
Chris Dalton6b498102019-08-01 14:14:52 -0600334 if (this->executeRenderTasks(
335 startIndex, stopIndex, &flushState, &numRenderTasksExecuted)) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500336 flushed = true;
337 }
bsalomondc438982016-08-31 11:53:49 -0700338 }
Greg Danielc42b20b2017-10-04 10:34:49 -0400339 }
340
Chris Dalton91ab1552018-04-18 13:24:25 -0600341#ifdef SK_DEBUG
Chris Dalton6b498102019-08-01 14:14:52 -0600342 for (int i = 0; i < fDAG.numRenderTasks(); ++i) {
Adlai Holler96ead542020-06-26 08:50:14 -0400343 // All render tasks should have been cleared out by now – we only reset the array below to
344 // reclaim storage.
345 SkASSERT(!fDAG.renderTask(i));
Chris Dalton91ab1552018-04-18 13:24:25 -0600346 }
347#endif
Adlai Holler25df1f72020-06-09 13:08:27 -0400348 fLastRenderTasks.reset();
Robert Phillips22310d62018-09-05 11:07:21 -0400349 fDAG.reset();
Robert Phillips15c91422019-05-07 16:54:48 -0400350 this->clearDDLTargets();
robertphillipsa13e2022015-11-11 12:01:09 -0800351
Robert Phillipsc994a932018-06-19 13:09:54 -0400352#ifdef SK_DEBUG
353 // In non-DDL mode this checks that all the flushed ops have been freed from the memory pool.
354 // When we move to partial flushes this assert will no longer be valid.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400355 // In DDL mode this check is somewhat superfluous since the memory for most of the ops/opsTasks
Robert Phillipsc994a932018-06-19 13:09:54 -0400356 // will be stored in the DDL's GrOpMemoryPools.
Robert Phillips9da87e02019-02-04 13:26:26 -0500357 GrOpMemoryPool* opMemoryPool = fContext->priv().opMemoryPool();
Robert Phillipsc994a932018-06-19 13:09:54 -0400358 opMemoryPool->isEmpty();
359#endif
360
Greg Daniel9efe3862020-06-11 11:51:06 -0400361 gpu->executeFlushInfo(proxies, numProxies, access, info, newState);
robertphillipsa13e2022015-11-11 12:01:09 -0800362
Brian Salomon57d2beab2018-09-10 09:35:41 -0400363 // Give the cache a chance to purge resources that become purgeable due to flushing.
364 if (flushed) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500365 resourceCache->purgeAsNeeded();
Brian Salomon876a0172019-03-08 11:12:14 -0500366 flushed = false;
bsalomonb77a9072016-09-07 10:02:04 -0700367 }
Chris Daltonfe199b72017-05-05 11:26:15 -0400368 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
Chris Dalton6b498102019-08-01 14:14:52 -0600369 onFlushCBObject->postFlush(fTokenTracker.nextTokenToFlush(), fFlushingRenderTaskIDs.begin(),
370 fFlushingRenderTaskIDs.count());
Brian Salomon876a0172019-03-08 11:12:14 -0500371 flushed = true;
372 }
373 if (flushed) {
374 resourceCache->purgeAsNeeded();
Chris Daltonfe199b72017-05-05 11:26:15 -0400375 }
Chris Dalton6b498102019-08-01 14:14:52 -0600376 fFlushingRenderTaskIDs.reset();
joshualittb8918c42015-12-18 09:59:46 -0800377 fFlushing = false;
Greg Daniel51316782017-08-02 15:10:09 +0000378
Greg Danielfe159622020-04-10 17:43:51 +0000379 return true;
380}
381
382bool GrDrawingManager::submitToGpu(bool syncToCpu) {
383 if (fFlushing || this->wasAbandoned()) {
384 return false;
385 }
386
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400387 auto direct = fContext->asDirectContext();
Greg Danielfe159622020-04-10 17:43:51 +0000388 if (!direct) {
389 return false; // Can't submit while DDL recording
390 }
391 GrGpu* gpu = direct->priv().getGpu();
392 return gpu->submitToGpu(syncToCpu);
robertphillips3dc6ae52015-10-20 09:54:32 -0700393}
394
Chris Dalton6b498102019-08-01 14:14:52 -0600395bool GrDrawingManager::executeRenderTasks(int startIndex, int stopIndex, GrOpFlushState* flushState,
396 int* numRenderTasksExecuted) {
397 SkASSERT(startIndex <= stopIndex && stopIndex <= fDAG.numRenderTasks());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500398
Robert Phillips27483912018-04-20 12:43:18 -0400399#if GR_FLUSH_TIME_OP_SPEW
Greg Danielf41b2bd2019-08-22 16:19:24 -0400400 SkDebugf("Flushing opsTask: %d to %d out of [%d, %d]\n",
Chris Dalton6b498102019-08-01 14:14:52 -0600401 startIndex, stopIndex, 0, fDAG.numRenderTasks());
Robert Phillips27483912018-04-20 12:43:18 -0400402 for (int i = startIndex; i < stopIndex; ++i) {
Chris Dalton6b498102019-08-01 14:14:52 -0600403 if (fDAG.renderTask(i)) {
404 fDAG.renderTask(i)->dump(true);
Robert Phillips1734dd32018-08-21 13:52:09 -0400405 }
Robert Phillips27483912018-04-20 12:43:18 -0400406 }
407#endif
408
Chris Dalton6b498102019-08-01 14:14:52 -0600409 bool anyRenderTasksExecuted = false;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500410
411 for (int i = startIndex; i < stopIndex; ++i) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400412 GrRenderTask* renderTask = fDAG.renderTask(i);
413 if (!renderTask || !renderTask->isInstantiated()) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500414 continue;
415 }
416
Chris Dalton6b498102019-08-01 14:14:52 -0600417 SkASSERT(renderTask->deferredProxiesAreInstantiated());
Robert Phillips22310d62018-09-05 11:07:21 -0400418
Chris Dalton6b498102019-08-01 14:14:52 -0600419 renderTask->prepare(flushState);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500420 }
421
422 // Upload all data to the GPU
423 flushState->preExecuteDraws();
424
Greg Danield2073452018-12-07 11:20:33 -0500425 // For Vulkan, if we have too many oplists to be flushed we end up allocating a lot of resources
426 // for each command buffer associated with the oplists. If this gets too large we can cause the
427 // devices to go OOM. In practice we usually only hit this case in our tests, but to be safe we
428 // put a cap on the number of oplists we will execute before flushing to the GPU to relieve some
429 // memory pressure.
Chris Dalton6b498102019-08-01 14:14:52 -0600430 static constexpr int kMaxRenderTasksBeforeFlush = 100;
Greg Danield2073452018-12-07 11:20:33 -0500431
Chris Daltonc4b47352019-08-23 10:10:36 -0600432 // Execute the onFlush renderTasks first, if any.
433 for (sk_sp<GrRenderTask>& onFlushRenderTask : fOnFlushRenderTasks) {
434 if (!onFlushRenderTask->execute(flushState)) {
435 SkDebugf("WARNING: onFlushRenderTask failed to execute.\n");
Robert Phillipseafd48a2017-11-16 07:52:08 -0500436 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600437 SkASSERT(onFlushRenderTask->unique());
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400438 onFlushRenderTask->disown(this);
Chris Daltonc4b47352019-08-23 10:10:36 -0600439 onFlushRenderTask = nullptr;
Chris Dalton6b498102019-08-01 14:14:52 -0600440 (*numRenderTasksExecuted)++;
441 if (*numRenderTasksExecuted >= kMaxRenderTasksBeforeFlush) {
Greg Danielfe159622020-04-10 17:43:51 +0000442 flushState->gpu()->submitToGpu(false);
Chris Dalton6b498102019-08-01 14:14:52 -0600443 *numRenderTasksExecuted = 0;
Greg Danield2073452018-12-07 11:20:33 -0500444 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500445 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600446 fOnFlushRenderTasks.reset();
Robert Phillipseafd48a2017-11-16 07:52:08 -0500447
448 // Execute the normal op lists.
449 for (int i = startIndex; i < stopIndex; ++i) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400450 GrRenderTask* renderTask = fDAG.renderTask(i);
451 if (!renderTask || !renderTask->isInstantiated()) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500452 continue;
453 }
454
Greg Daniel15ecdf92019-08-30 15:35:23 -0400455 if (renderTask->execute(flushState)) {
Chris Dalton6b498102019-08-01 14:14:52 -0600456 anyRenderTasksExecuted = true;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500457 }
Chris Dalton6b498102019-08-01 14:14:52 -0600458 (*numRenderTasksExecuted)++;
459 if (*numRenderTasksExecuted >= kMaxRenderTasksBeforeFlush) {
Greg Danielfe159622020-04-10 17:43:51 +0000460 flushState->gpu()->submitToGpu(false);
Chris Dalton6b498102019-08-01 14:14:52 -0600461 *numRenderTasksExecuted = 0;
Greg Danield2073452018-12-07 11:20:33 -0500462 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500463 }
464
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400465 SkASSERT(!flushState->opsRenderPass());
Robert Phillips40a29d72018-01-18 12:59:22 -0500466 SkASSERT(fTokenTracker.nextDrawToken() == fTokenTracker.nextTokenToFlush());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500467
Chris Dalton6b498102019-08-01 14:14:52 -0600468 // We reset the flush state before the RenderTasks so that the last resources to be freed are
469 // those that are written to in the RenderTasks. This helps to make sure the most recently used
470 // resources are the last to be purged by the resource cache.
Robert Phillipseafd48a2017-11-16 07:52:08 -0500471 flushState->reset();
472
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400473 this->removeRenderTasks(startIndex, stopIndex);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500474
Chris Dalton6b498102019-08-01 14:14:52 -0600475 return anyRenderTasksExecuted;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500476}
477
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400478void GrDrawingManager::removeRenderTasks(int startIndex, int stopIndex) {
479 for (int i = startIndex; i < stopIndex; ++i) {
480 GrRenderTask* task = fDAG.renderTask(i);
481 if (!task) {
482 continue;
483 }
484 if (!task->unique()) {
485 // TODO: Eventually this should be guaranteed unique: http://skbug.com/7111
486 task->endFlush(this);
487 }
488 task->disown(this);
489 }
Adlai Holler96ead542020-06-26 08:50:14 -0400490 fDAG.rawRemoveRenderTasks(startIndex, stopIndex);
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400491}
492
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400493static void resolve_and_mipmap(GrGpu* gpu, GrSurfaceProxy* proxy) {
494 if (!proxy->isInstantiated()) {
495 return;
496 }
497
498 // In the flushSurfaces case, we need to resolve MSAA immediately after flush. This is
499 // because clients expect the flushed surface's backing texture to be fully resolved
500 // upon return.
501 if (proxy->requiresManualMSAAResolve()) {
502 auto* rtProxy = proxy->asRenderTargetProxy();
503 SkASSERT(rtProxy);
504 if (rtProxy->isMSAADirty()) {
505 SkASSERT(rtProxy->peekRenderTarget());
Jim Van Verthbb61fe32020-07-07 16:39:04 -0400506 gpu->resolveRenderTarget(rtProxy->peekRenderTarget(), rtProxy->msaaDirtyRect());
507 gpu->submitToGpu(false);
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400508 rtProxy->markMSAAResolved();
509 }
510 }
511 // If, after a flush, any of the proxies of interest have dirty mipmaps, regenerate them in
512 // case their backend textures are being stolen.
513 // (This special case is exercised by the ReimportImageTextureWithMipLevels test.)
514 // FIXME: It may be more ideal to plumb down a "we're going to steal the backends" flag.
515 if (auto* textureProxy = proxy->asTextureProxy()) {
Brian Salomon8c82a872020-07-21 12:09:58 -0400516 if (textureProxy->mipmapsAreDirty()) {
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400517 SkASSERT(textureProxy->peekTexture());
518 gpu->regenerateMipMapLevels(textureProxy->peekTexture());
Brian Salomon8c82a872020-07-21 12:09:58 -0400519 textureProxy->markMipmapsClean();
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400520 }
521 }
522}
523
Greg Daniel9efe3862020-06-11 11:51:06 -0400524GrSemaphoresSubmitted GrDrawingManager::flushSurfaces(
525 GrSurfaceProxy* proxies[],
526 int numProxies,
527 SkSurface::BackendSurfaceAccess access,
528 const GrFlushInfo& info,
529 const GrBackendSurfaceMutableState* newState) {
bsalomon6a2b1942016-09-08 11:28:59 -0700530 if (this->wasAbandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -0400531 if (info.fSubmittedProc) {
532 info.fSubmittedProc(info.fSubmittedContext, false);
533 }
534 if (info.fFinishedProc) {
535 info.fFinishedProc(info.fFinishedContext);
536 }
Greg Daniel51316782017-08-02 15:10:09 +0000537 return GrSemaphoresSubmitted::kNo;
bsalomon6a2b1942016-09-08 11:28:59 -0700538 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400539 SkDEBUGCODE(this->validate());
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400540 SkASSERT(numProxies >= 0);
541 SkASSERT(!numProxies || proxies);
bsalomon6a2b1942016-09-08 11:28:59 -0700542
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400543 auto direct = fContext->asDirectContext();
Robert Phillips6a6de562019-02-15 15:19:15 -0500544 if (!direct) {
Greg Daniel55822f12020-05-26 11:26:45 -0400545 if (info.fSubmittedProc) {
546 info.fSubmittedProc(info.fSubmittedContext, false);
547 }
548 if (info.fFinishedProc) {
549 info.fFinishedProc(info.fFinishedContext);
550 }
Robert Phillips6a6de562019-02-15 15:19:15 -0500551 return GrSemaphoresSubmitted::kNo; // Can't flush while DDL recording
552 }
553
554 GrGpu* gpu = direct->priv().getGpu();
Greg Daniel55822f12020-05-26 11:26:45 -0400555 // We have a non abandoned and direct GrContext. It must have a GrGpu.
556 SkASSERT(gpu);
Robert Phillips874b5352018-03-16 08:48:24 -0400557
Robert Phillipsacc10fa2019-04-01 09:50:20 -0400558 // TODO: It is important to upgrade the drawingmanager to just flushing the
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400559 // portion of the DAG required by 'proxies' in order to restore some of the
Robert Phillipsacc10fa2019-04-01 09:50:20 -0400560 // semantics of this method.
Greg Daniel9efe3862020-06-11 11:51:06 -0400561 bool didFlush = this->flush(proxies, numProxies, access, info, newState);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400562 for (int i = 0; i < numProxies; ++i) {
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400563 resolve_and_mipmap(gpu, proxies[i]);
bsalomon6a2b1942016-09-08 11:28:59 -0700564 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400565
566 SkDEBUGCODE(this->validate());
Greg Danielfe159622020-04-10 17:43:51 +0000567
Greg Daniel04283f32020-05-20 13:16:00 -0400568 if (!didFlush || (!direct->priv().caps()->semaphoreSupport() && info.fNumSemaphores)) {
Greg Danielfe159622020-04-10 17:43:51 +0000569 return GrSemaphoresSubmitted::kNo;
570 }
571 return GrSemaphoresSubmitted::kYes;
bsalomon6a2b1942016-09-08 11:28:59 -0700572}
573
Chris Daltonfe199b72017-05-05 11:26:15 -0400574void GrDrawingManager::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
575 fOnFlushCBObjects.push_back(onFlushCBObject);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400576}
577
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500578#if GR_TEST_UTILS
579void GrDrawingManager::testingOnly_removeOnFlushCallbackObject(GrOnFlushCallbackObject* cb) {
580 int n = std::find(fOnFlushCBObjects.begin(), fOnFlushCBObjects.end(), cb) -
581 fOnFlushCBObjects.begin();
582 SkASSERT(n < fOnFlushCBObjects.count());
583 fOnFlushCBObjects.removeShuffle(n);
584}
585#endif
586
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400587void GrDrawingManager::setLastRenderTask(const GrSurfaceProxy* proxy, GrRenderTask* task) {
588#ifdef SK_DEBUG
589 if (GrRenderTask* prior = this->getLastRenderTask(proxy)) {
590 SkASSERT(prior->isClosed());
591 }
592#endif
Adlai Holler25df1f72020-06-09 13:08:27 -0400593 uint32_t key = proxy->uniqueID().asUInt();
594 if (task) {
595 fLastRenderTasks.set(key, task);
596 } else if (fLastRenderTasks.find(key)) {
597 fLastRenderTasks.remove(key);
598 }
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400599}
600
601GrRenderTask* GrDrawingManager::getLastRenderTask(const GrSurfaceProxy* proxy) const {
602 auto entry = fLastRenderTasks.find(proxy->uniqueID().asUInt());
603 return entry ? *entry : nullptr;
604}
605
606GrOpsTask* GrDrawingManager::getLastOpsTask(const GrSurfaceProxy* proxy) const {
607 GrRenderTask* task = this->getLastRenderTask(proxy);
608 return task ? task->asOpsTask() : nullptr;
609}
610
611
Chris Dalton6b498102019-08-01 14:14:52 -0600612void GrDrawingManager::moveRenderTasksToDDL(SkDeferredDisplayList* ddl) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400613 SkDEBUGCODE(this->validate());
614
Chris Dalton6b498102019-08-01 14:14:52 -0600615 // no renderTask should receive a new command after this
Robert Phillips9da87e02019-02-04 13:26:26 -0500616 fDAG.closeAll(fContext->priv().caps());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400617 fActiveOpsTask = nullptr;
Robert Phillips7a137052018-02-01 11:23:12 -0500618
Chris Dalton6b498102019-08-01 14:14:52 -0600619 fDAG.swap(&ddl->fRenderTasks);
Robert Phillips19f466d2020-02-26 10:27:07 -0500620 SkASSERT(!fDAG.numRenderTasks());
Robert Phillips867ce8f2018-06-21 10:28:36 -0400621
Robert Phillips19f466d2020-02-26 10:27:07 -0500622 for (auto& renderTask : ddl->fRenderTasks) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400623 renderTask->disown(this);
Robert Phillips29f38542019-10-16 09:20:25 -0400624 renderTask->prePrepare(fContext);
Robert Phillips7327c9d2019-10-08 16:32:56 -0400625 }
626
Michael Ludwig2c316bd2019-12-19 14:50:44 -0500627 ddl->fArenas = std::move(fContext->priv().detachArenas());
Robert Phillips61fc7992019-10-22 11:58:17 -0400628
Robert Phillipsf6a0b452020-02-18 14:26:46 -0500629 fContext->priv().detachProgramData(&ddl->fProgramData);
Robert Phillips576b6a12019-12-06 13:05:49 -0500630
Robert Phillips774168e2018-05-31 12:43:27 -0400631 if (fPathRendererChain) {
632 if (auto ccpr = fPathRendererChain->getCoverageCountingPathRenderer()) {
633 ddl->fPendingPaths = ccpr->detachPendingPaths();
634 }
635 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400636
637 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500638}
639
Adlai Holler7580ad42020-06-24 13:45:25 -0400640void GrDrawingManager::copyRenderTasksFromDDL(sk_sp<const SkDeferredDisplayList> ddl,
641 GrRenderTargetProxy* newDest) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400642 SkDEBUGCODE(this->validate());
643
Greg Danielf41b2bd2019-08-22 16:19:24 -0400644 if (fActiveOpsTask) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400645 // This is a temporary fix for the partial-MDB world. In that world we're not
Greg Danielf41b2bd2019-08-22 16:19:24 -0400646 // reordering so ops that (in the single opsTask world) would've just glommed onto the
647 // end of the single opsTask but referred to a far earlier RT need to appear in their
648 // own opsTask.
649 fActiveOpsTask->makeClosed(*fContext->priv().caps());
650 fActiveOpsTask = nullptr;
Robert Phillips38d64b02018-09-04 13:23:26 -0400651 }
652
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400653 // Propagate the DDL proxy's state information to the replaying DDL.
654 if (ddl->priv().targetProxy()->isMSAADirty()) {
655 newDest->markMSAADirty(ddl->priv().targetProxy()->msaaDirtyRect(),
656 ddl->characterization().origin());
657 }
658 GrTextureProxy* newTextureProxy = newDest->asTextureProxy();
Brian Salomon8c82a872020-07-21 12:09:58 -0400659 if (newTextureProxy && GrMipmapped::kYes == newTextureProxy->mipmapped()) {
660 newTextureProxy->markMipmapsDirty();
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400661 }
662
663 this->addDDLTarget(newDest, ddl->priv().targetProxy());
Robert Phillips15c91422019-05-07 16:54:48 -0400664
Robert Phillips62000362018-02-01 09:10:04 -0500665 // Here we jam the proxy that backs the current replay SkSurface into the LazyProxyData.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400666 // The lazy proxy that references it (in the copied opsTasks) will steal its GrTexture.
Robert Phillips62000362018-02-01 09:10:04 -0500667 ddl->fLazyProxyData->fReplayDest = newDest;
Robert Phillips774168e2018-05-31 12:43:27 -0400668
669 if (ddl->fPendingPaths.size()) {
670 GrCoverageCountingPathRenderer* ccpr = this->getCoverageCountingPathRenderer();
671
672 ccpr->mergePendingPaths(ddl->fPendingPaths);
673 }
Robert Phillips22310d62018-09-05 11:07:21 -0400674
Chris Dalton6b498102019-08-01 14:14:52 -0600675 fDAG.add(ddl->fRenderTasks);
Robert Phillips38d64b02018-09-04 13:23:26 -0400676
Adlai Holler6c9bb622020-06-25 09:21:18 -0400677 // Add a task to unref the DDL after flush.
678 GrRenderTask* unrefTask = fDAG.add(sk_make_sp<GrUnrefDDLTask>(std::move(ddl)));
679 unrefTask->makeClosed(*fContext->priv().caps());
Adlai Holler7580ad42020-06-24 13:45:25 -0400680
Robert Phillips38d64b02018-09-04 13:23:26 -0400681 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500682}
683
Robert Phillips38d64b02018-09-04 13:23:26 -0400684#ifdef SK_DEBUG
685void GrDrawingManager::validate() const {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400686 if (fDAG.sortingRenderTasks() && fReduceOpsTaskSplitting) {
687 SkASSERT(!fActiveOpsTask);
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400688 } else {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400689 if (fActiveOpsTask) {
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400690 SkASSERT(!fDAG.empty());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400691 SkASSERT(!fActiveOpsTask->isClosed());
692 SkASSERT(fActiveOpsTask == fDAG.back());
Robert Phillips38d64b02018-09-04 13:23:26 -0400693 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400694
Chris Dalton6b498102019-08-01 14:14:52 -0600695 for (int i = 0; i < fDAG.numRenderTasks(); ++i) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400696 if (fActiveOpsTask != fDAG.renderTask(i)) {
Chris Daltone2a903e2019-09-18 13:41:50 -0600697 // The resolveTask associated with the activeTask remains open for as long as the
698 // activeTask does.
699 bool isActiveResolveTask =
700 fActiveOpsTask && fActiveOpsTask->fTextureResolveTask == fDAG.renderTask(i);
701 SkASSERT(isActiveResolveTask || fDAG.renderTask(i)->isClosed());
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400702 }
703 }
704
705 if (!fDAG.empty() && !fDAG.back()->isClosed()) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400706 SkASSERT(fActiveOpsTask == fDAG.back());
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400707 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400708 }
709}
710#endif
711
Greg Danielbbfec9d2019-08-20 10:56:51 -0400712void GrDrawingManager::closeRenderTasksForNewRenderTask(GrSurfaceProxy* target) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400713 if (target && fDAG.sortingRenderTasks() && fReduceOpsTaskSplitting) {
Chris Dalton5fe99772019-08-06 11:57:39 -0600714 // In this case we need to close all the renderTasks that rely on the current contents of
715 // 'target'. That is bc we're going to update the content of the proxy so they need to be
716 // split in case they use both the old and new content. (This is a bit of an overkill: they
717 // really only need to be split if they ever reference proxy's contents again but that is
718 // hard to predict/handle).
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400719 if (GrRenderTask* lastRenderTask = this->getLastRenderTask(target)) {
Chris Dalton6b498102019-08-01 14:14:52 -0600720 lastRenderTask->closeThoseWhoDependOnMe(*fContext->priv().caps());
Robert Phillips46acf9d2018-10-09 09:31:40 -0400721 }
Greg Danielf41b2bd2019-08-22 16:19:24 -0400722 } else if (fActiveOpsTask) {
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400723 // This is a temporary fix for the partial-MDB world. In that world we're not
Greg Danielf41b2bd2019-08-22 16:19:24 -0400724 // reordering so ops that (in the single opsTask world) would've just glommed onto the
725 // end of the single opsTask but referred to a far earlier RT need to appear in their
726 // own opsTask.
727 fActiveOpsTask->makeClosed(*fContext->priv().caps());
728 fActiveOpsTask = nullptr;
robertphillips3dc6ae52015-10-20 09:54:32 -0700729 }
Chris Dalton5fe99772019-08-06 11:57:39 -0600730}
731
Greg Daniel16f5c652019-10-29 11:26:01 -0400732sk_sp<GrOpsTask> GrDrawingManager::newOpsTask(GrSurfaceProxyView surfaceView,
733 bool managedOpsTask) {
Chris Dalton5fe99772019-08-06 11:57:39 -0600734 SkDEBUGCODE(this->validate());
735 SkASSERT(fContext);
736
Greg Daniel16f5c652019-10-29 11:26:01 -0400737 GrSurfaceProxy* proxy = surfaceView.proxy();
738 this->closeRenderTasksForNewRenderTask(proxy);
robertphillips3dc6ae52015-10-20 09:54:32 -0700739
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400740 sk_sp<GrOpsTask> opsTask(new GrOpsTask(this, fContext->priv().arenas(),
Michael Ludwigd0840ec2019-12-12 09:48:38 -0500741 std::move(surfaceView),
742 fContext->priv().auditTrail()));
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400743 SkASSERT(this->getLastRenderTask(proxy) == opsTask.get());
robertphillips3dc6ae52015-10-20 09:54:32 -0700744
Greg Danielf41b2bd2019-08-22 16:19:24 -0400745 if (managedOpsTask) {
746 fDAG.add(opsTask);
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400747
Greg Danielf41b2bd2019-08-22 16:19:24 -0400748 if (!fDAG.sortingRenderTasks() || !fReduceOpsTaskSplitting) {
749 fActiveOpsTask = opsTask.get();
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400750 }
Robert Phillips941d1442017-06-14 16:37:02 -0400751 }
robertphillips3dc6ae52015-10-20 09:54:32 -0700752
Robert Phillips38d64b02018-09-04 13:23:26 -0400753 SkDEBUGCODE(this->validate());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400754 return opsTask;
robertphillips3dc6ae52015-10-20 09:54:32 -0700755}
756
Chris Daltone2a903e2019-09-18 13:41:50 -0600757GrTextureResolveRenderTask* GrDrawingManager::newTextureResolveRenderTask(const GrCaps& caps) {
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600758 // 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 -0600759 // in sorting and opsTask reduction mode) the render tasks that depend on any proxy's current
Greg Danielf41b2bd2019-08-22 16:19:24 -0400760 // state. This is because those opsTasks can still receive new ops and because if they refer to
Chris Dalton4ece96d2019-08-30 11:26:39 -0600761 // the mipmapped version of 'proxy', they will then come to depend on the render task being
762 // created here.
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600763 //
Greg Danielf41b2bd2019-08-22 16:19:24 -0400764 // Add the new textureResolveTask before the fActiveOpsTask (if not in
765 // sorting/opsTask-splitting-reduction mode) because it will depend upon this resolve task.
Chris Dalton3d770272019-08-14 09:24:37 -0600766 // NOTE: Putting it here will also reduce the amount of work required by the topological sort.
Chris Daltone2a903e2019-09-18 13:41:50 -0600767 return static_cast<GrTextureResolveRenderTask*>(fDAG.addBeforeLast(
768 sk_make_sp<GrTextureResolveRenderTask>()));
Chris Dalton3d770272019-08-14 09:24:37 -0600769}
770
Greg Danielc30f1a92019-09-06 15:28:58 -0400771void GrDrawingManager::newWaitRenderTask(sk_sp<GrSurfaceProxy> proxy,
Greg Daniel301015c2019-11-18 14:06:46 -0500772 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> semaphores,
Greg Danielc30f1a92019-09-06 15:28:58 -0400773 int numSemaphores) {
774 SkDEBUGCODE(this->validate());
775 SkASSERT(fContext);
776
777 const GrCaps& caps = *fContext->priv().caps();
778
Greg Daniel16f5c652019-10-29 11:26:01 -0400779 sk_sp<GrWaitRenderTask> waitTask = sk_make_sp<GrWaitRenderTask>(GrSurfaceProxyView(proxy),
780 std::move(semaphores),
Greg Danielc30f1a92019-09-06 15:28:58 -0400781 numSemaphores);
782 if (fReduceOpsTaskSplitting) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400783 GrRenderTask* lastTask = this->getLastRenderTask(proxy.get());
Greg Danielc30f1a92019-09-06 15:28:58 -0400784 if (lastTask && !lastTask->isClosed()) {
785 // We directly make the currently open renderTask depend on waitTask instead of using
786 // the proxy version of addDependency. The waitTask will never need to trigger any
787 // resolves or mip map generation which is the main advantage of going through the proxy
788 // version. Additionally we would've had to temporarily set the wait task as the
789 // lastRenderTask on the proxy, add the dependency, and then reset the lastRenderTask to
790 // lastTask. Additionally we add all dependencies of lastTask to waitTask so that the
791 // waitTask doesn't get reordered before them and unnecessarily block those tasks.
792 // Note: Any previous Ops already in lastTask will get blocked by the wait semaphore
793 // even though they don't need to be for correctness.
794
795 // Make sure we add the dependencies of lastTask to waitTask first or else we'll get a
796 // circular self dependency of waitTask on waitTask.
797 waitTask->addDependenciesFromOtherTask(lastTask);
798 lastTask->addDependency(waitTask.get());
799 } else {
800 // If there is a last task we set the waitTask to depend on it so that it doesn't get
801 // reordered in front of the lastTask causing the lastTask to be blocked by the
802 // semaphore. Again we directly just go through adding the dependency to the task and
803 // not the proxy since we don't need to worry about resolving anything.
804 if (lastTask) {
805 waitTask->addDependency(lastTask);
806 }
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400807 this->setLastRenderTask(proxy.get(), waitTask.get());
Greg Danielc30f1a92019-09-06 15:28:58 -0400808 }
809 fDAG.add(waitTask);
810 } else {
Adlai Holler33d569e2020-06-16 14:30:08 -0400811 if (fActiveOpsTask && (fActiveOpsTask->target(0).proxy() == proxy.get())) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400812 SkASSERT(this->getLastRenderTask(proxy.get()) == fActiveOpsTask);
Greg Danielc30f1a92019-09-06 15:28:58 -0400813 fDAG.addBeforeLast(waitTask);
814 // In this case we keep the current renderTask open but just insert the new waitTask
815 // before it in the list. The waitTask will never need to trigger any resolves or mip
816 // map generation which is the main advantage of going through the proxy version.
817 // Additionally we would've had to temporarily set the wait task as the lastRenderTask
818 // on the proxy, add the dependency, and then reset the lastRenderTask to
819 // fActiveOpsTask. Additionally we make the waitTask depend on all of fActiveOpsTask
820 // dependencies so that we don't unnecessarily reorder the waitTask before them.
821 // Note: Any previous Ops already in fActiveOpsTask will get blocked by the wait
822 // semaphore even though they don't need to be for correctness.
823
824 // Make sure we add the dependencies of fActiveOpsTask to waitTask first or else we'll
825 // get a circular self dependency of waitTask on waitTask.
826 waitTask->addDependenciesFromOtherTask(fActiveOpsTask);
827 fActiveOpsTask->addDependency(waitTask.get());
828 } else {
829 // In this case we just close the previous RenderTask and start and append the waitTask
830 // to the DAG. Since it is the last task now we call setLastRenderTask on the proxy. If
831 // there is a lastTask on the proxy we make waitTask depend on that task. This
832 // dependency isn't strictly needed but it does keep the DAG from reordering the
833 // waitTask earlier and blocking more tasks.
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400834 if (GrRenderTask* lastTask = this->getLastRenderTask(proxy.get())) {
Greg Danielc30f1a92019-09-06 15:28:58 -0400835 waitTask->addDependency(lastTask);
836 }
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400837 this->setLastRenderTask(proxy.get(), waitTask.get());
Greg Danielc30f1a92019-09-06 15:28:58 -0400838 this->closeRenderTasksForNewRenderTask(proxy.get());
839 fDAG.add(waitTask);
840 }
841 }
842 waitTask->makeClosed(caps);
843
844 SkDEBUGCODE(this->validate());
845}
846
Greg Danielbbfec9d2019-08-20 10:56:51 -0400847void GrDrawingManager::newTransferFromRenderTask(sk_sp<GrSurfaceProxy> srcProxy,
848 const SkIRect& srcRect,
849 GrColorType surfaceColorType,
850 GrColorType dstColorType,
851 sk_sp<GrGpuBuffer> dstBuffer,
852 size_t dstOffset) {
853 SkDEBUGCODE(this->validate());
854 SkASSERT(fContext);
855 // This copies from srcProxy to dstBuffer so it doesn't have a real target.
856 this->closeRenderTasksForNewRenderTask(nullptr);
857
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600858 GrRenderTask* task = fDAG.add(sk_make_sp<GrTransferFromRenderTask>(
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400859 srcProxy, srcRect, surfaceColorType, dstColorType,
860 std::move(dstBuffer), dstOffset));
Greg Danielbbfec9d2019-08-20 10:56:51 -0400861
862 const GrCaps& caps = *fContext->priv().caps();
863
Brian Salomon7e67dca2020-07-21 09:27:25 -0400864 // We always say GrMipmapped::kNo here since we are always just copying from the base layer. We
Greg Danielbbfec9d2019-08-20 10:56:51 -0400865 // don't need to make sure the whole mip map chain is valid.
Brian Salomon7e67dca2020-07-21 09:27:25 -0400866 task->addDependency(this, srcProxy.get(), GrMipmapped::kNo,
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400867 GrTextureResolveManager(this), caps);
Greg Danielbbfec9d2019-08-20 10:56:51 -0400868 task->makeClosed(caps);
869
Greg Danielbbfec9d2019-08-20 10:56:51 -0400870 // We have closed the previous active oplist but since a new oplist isn't being added there
871 // shouldn't be an active one.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400872 SkASSERT(!fActiveOpsTask);
Greg Danielbbfec9d2019-08-20 10:56:51 -0400873 SkDEBUGCODE(this->validate());
874}
875
Greg Daniel16f5c652019-10-29 11:26:01 -0400876bool GrDrawingManager::newCopyRenderTask(GrSurfaceProxyView srcView,
Greg Daniele227fe42019-08-21 13:52:24 -0400877 const SkIRect& srcRect,
Greg Daniel16f5c652019-10-29 11:26:01 -0400878 GrSurfaceProxyView dstView,
Greg Daniele227fe42019-08-21 13:52:24 -0400879 const SkIPoint& dstPoint) {
880 SkDEBUGCODE(this->validate());
881 SkASSERT(fContext);
Greg Daniele227fe42019-08-21 13:52:24 -0400882
Greg Daniel16f5c652019-10-29 11:26:01 -0400883 this->closeRenderTasksForNewRenderTask(dstView.proxy());
Brian Salomone4bce012019-09-20 15:34:23 -0400884 const GrCaps& caps = *fContext->priv().caps();
885
Greg Daniel16f5c652019-10-29 11:26:01 -0400886 GrSurfaceProxy* srcProxy = srcView.proxy();
887
Brian Salomone4bce012019-09-20 15:34:23 -0400888 GrRenderTask* task =
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400889 fDAG.add(GrCopyRenderTask::Make(this, std::move(srcView), srcRect, std::move(dstView),
Greg Daniel16f5c652019-10-29 11:26:01 -0400890 dstPoint, &caps));
Greg Daniele227fe42019-08-21 13:52:24 -0400891 if (!task) {
892 return false;
893 }
894
Brian Salomon7e67dca2020-07-21 09:27:25 -0400895 // We always say GrMipmapped::kNo here since we are always just copying from the base layer to
Greg Daniele227fe42019-08-21 13:52:24 -0400896 // another base layer. We don't need to make sure the whole mip map chain is valid.
Brian Salomon7e67dca2020-07-21 09:27:25 -0400897 task->addDependency(this, srcProxy, GrMipmapped::kNo, GrTextureResolveManager(this), caps);
Greg Daniele227fe42019-08-21 13:52:24 -0400898 task->makeClosed(caps);
899
Greg Daniele227fe42019-08-21 13:52:24 -0400900 // We have closed the previous active oplist but since a new oplist isn't being added there
901 // shouldn't be an active one.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400902 SkASSERT(!fActiveOpsTask);
Greg Daniele227fe42019-08-21 13:52:24 -0400903 SkDEBUGCODE(this->validate());
904 return true;
905}
906
robertphillips68737822015-10-29 12:12:21 -0700907/*
908 * This method finds a path renderer that can draw the specified path on
909 * the provided target.
910 * Due to its expense, the software path renderer has split out so it can
911 * can be individually allowed/disallowed via the "allowSW" boolean.
912 */
913GrPathRenderer* GrDrawingManager::getPathRenderer(const GrPathRenderer::CanDrawPathArgs& args,
914 bool allowSW,
915 GrPathRendererChain::DrawType drawType,
916 GrPathRenderer::StencilSupport* stencilSupport) {
917
918 if (!fPathRendererChain) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400919 fPathRendererChain.reset(new GrPathRendererChain(fContext, fOptionsForPathRendererChain));
robertphillips68737822015-10-29 12:12:21 -0700920 }
921
922 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(args, drawType, stencilSupport);
923 if (!pr && allowSW) {
Brian Salomone7df0bb2018-05-07 14:44:57 -0400924 auto swPR = this->getSoftwarePathRenderer();
925 if (GrPathRenderer::CanDrawPath::kNo != swPR->canDrawPath(args)) {
926 pr = swPR;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500927 }
robertphillips68737822015-10-29 12:12:21 -0700928 }
929
Robert Phillipsd81379d2020-04-21 10:39:02 -0400930#if GR_PATH_RENDERER_SPEW
931 if (pr) {
932 SkDebugf("getPathRenderer: %s\n", pr->name());
933 }
934#endif
935
robertphillips68737822015-10-29 12:12:21 -0700936 return pr;
937}
938
Brian Salomone7df0bb2018-05-07 14:44:57 -0400939GrPathRenderer* GrDrawingManager::getSoftwarePathRenderer() {
940 if (!fSoftwarePathRenderer) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400941 fSoftwarePathRenderer.reset(
Robert Phillips9da87e02019-02-04 13:26:26 -0500942 new GrSoftwarePathRenderer(fContext->priv().proxyProvider(),
Ben Wagner9ec70c62018-07-12 13:30:47 -0400943 fOptionsForPathRendererChain.fAllowPathMaskCaching));
Brian Salomone7df0bb2018-05-07 14:44:57 -0400944 }
Ben Wagner9ec70c62018-07-12 13:30:47 -0400945 return fSoftwarePathRenderer.get();
Brian Salomone7df0bb2018-05-07 14:44:57 -0400946}
947
Chris Daltonfddb6c02017-11-04 15:22:22 -0600948GrCoverageCountingPathRenderer* GrDrawingManager::getCoverageCountingPathRenderer() {
949 if (!fPathRendererChain) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400950 fPathRendererChain.reset(new GrPathRendererChain(fContext, fOptionsForPathRendererChain));
Chris Daltonfddb6c02017-11-04 15:22:22 -0600951 }
952 return fPathRendererChain->getCoverageCountingPathRenderer();
953}
954
Brian Salomon653f42f2018-07-10 10:07:31 -0400955void GrDrawingManager::flushIfNecessary() {
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400956 auto direct = fContext->asDirectContext();
Robert Phillips6a6de562019-02-15 15:19:15 -0500957 if (!direct) {
958 return;
959 }
960
961 auto resourceCache = direct->priv().getResourceCache();
Brian Salomon653f42f2018-07-10 10:07:31 -0400962 if (resourceCache && resourceCache->requestsFlush()) {
Greg Daniel9efe3862020-06-11 11:51:06 -0400963 if (this->flush(nullptr, 0, SkSurface::BackendSurfaceAccess::kNoAccess, GrFlushInfo(),
964 nullptr)) {
Greg Danielfe159622020-04-10 17:43:51 +0000965 this->submitToGpu(false);
966 }
Brian Salomon57d2beab2018-09-10 09:35:41 -0400967 resourceCache->purgeAsNeeded();
Brian Salomon653f42f2018-07-10 10:07:31 -0400968 }
969}
970