blob: 69ea94afde684fa63315bba3fff670f26b96c6da [file] [log] [blame]
robertphillips3dc6ae52015-10-20 09:54:32 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/GrDrawingManager.h"
Robert Phillips69893702019-02-22 11:16:30 -05009
Adlai Hollerc2bfcff2020-11-06 15:39:36 -050010#include <algorithm>
John Stilesfbd050b2020-08-03 13:21:46 -040011#include <memory>
12
Robert Phillips4d5594d2020-02-21 14:24:40 -050013#include "include/core/SkDeferredDisplayList.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/gpu/GrBackendSemaphore.h"
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040015#include "include/gpu/GrDirectContext.h"
16#include "include/gpu/GrRecordingContext.h"
Robert Phillips7eb0a5f2020-06-09 14:15:09 -040017#include "src/core/SkDeferredDisplayListPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/core/SkTTopoSort.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040019#include "src/gpu/GrAuditTrail.h"
Brian Salomon9241a6d2019-10-03 13:26:54 -040020#include "src/gpu/GrClientMappedBufferManager.h"
Greg Daniele227fe42019-08-21 13:52:24 -040021#include "src/gpu/GrCopyRenderTask.h"
Robert Phillips02dd0ed2020-11-09 17:03:34 -050022#include "src/gpu/GrDDLTask.h"
Adlai Hollera0693042020-10-14 11:23:11 -040023#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/GrGpu.h"
25#include "src/gpu/GrMemoryPool.h"
26#include "src/gpu/GrOnFlushResourceProvider.h"
27#include "src/gpu/GrRecordingContextPriv.h"
28#include "src/gpu/GrRenderTargetContext.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040029#include "src/gpu/GrRenderTargetProxy.h"
Chris Dalton6b498102019-08-01 14:14:52 -060030#include "src/gpu/GrRenderTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "src/gpu/GrResourceAllocator.h"
32#include "src/gpu/GrResourceProvider.h"
33#include "src/gpu/GrSoftwarePathRenderer.h"
Greg Daniel46e366a2019-12-16 14:38:36 -050034#include "src/gpu/GrSurfaceContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050035#include "src/gpu/GrSurfaceProxyPriv.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000036#include "src/gpu/GrTexture.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040037#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050038#include "src/gpu/GrTextureProxyPriv.h"
Chris Dalton3d770272019-08-14 09:24:37 -060039#include "src/gpu/GrTextureResolveRenderTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050040#include "src/gpu/GrTracing.h"
Greg Danielbbfec9d2019-08-20 10:56:51 -040041#include "src/gpu/GrTransferFromRenderTask.h"
Greg Danielc30f1a92019-09-06 15:28:58 -040042#include "src/gpu/GrWaitRenderTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050043#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
Herb Derbya08bde62020-06-12 15:46:06 -040044#include "src/gpu/text/GrSDFTOptions.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050045#include "src/image/SkSurface_Gpu.h"
robertphillips498d7ac2015-10-30 10:11:30 -070046
Chris Dalton6b498102019-08-01 14:14:52 -060047void GrDrawingManager::RenderTaskDAG::gatherIDs(SkSTArray<8, uint32_t, true>* idArray) const {
48 idArray->reset(fRenderTasks.count());
49 for (int i = 0; i < fRenderTasks.count(); ++i) {
50 if (fRenderTasks[i]) {
Robert Phillips02dd0ed2020-11-09 17:03:34 -050051 fRenderTasks[i]->gatherIDs(idArray);
Robert Phillips22310d62018-09-05 11:07:21 -040052 }
53 }
54}
55
Chris Dalton6b498102019-08-01 14:14:52 -060056void GrDrawingManager::RenderTaskDAG::reset() {
57 fRenderTasks.reset();
Robert Phillips22310d62018-09-05 11:07:21 -040058}
59
Adlai Holler96ead542020-06-26 08:50:14 -040060void GrDrawingManager::RenderTaskDAG::rawRemoveRenderTasks(int startIndex, int stopIndex) {
Robert Phillips22310d62018-09-05 11:07:21 -040061 for (int i = startIndex; i < stopIndex; ++i) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -040062 fRenderTasks[i] = nullptr;
Robert Phillips22310d62018-09-05 11:07:21 -040063 }
64}
65
Chris Dalton6b498102019-08-01 14:14:52 -060066bool GrDrawingManager::RenderTaskDAG::isUsed(GrSurfaceProxy* proxy) const {
Adlai Holler33d569e2020-06-16 14:30:08 -040067 for (const auto& task : fRenderTasks) {
68 if (task && task->isUsed(proxy)) {
Robert Phillips9313aa72019-04-09 18:41:27 -040069 return true;
70 }
71 }
72
73 return false;
74}
75
Chris Dalton3d770272019-08-14 09:24:37 -060076GrRenderTask* GrDrawingManager::RenderTaskDAG::add(sk_sp<GrRenderTask> renderTask) {
Chris Dalton6aeb8e82019-08-27 11:52:19 -060077 if (renderTask) {
78 return fRenderTasks.emplace_back(std::move(renderTask)).get();
79 }
80 return nullptr;
Chris Dalton3d770272019-08-14 09:24:37 -060081}
82
83GrRenderTask* GrDrawingManager::RenderTaskDAG::addBeforeLast(sk_sp<GrRenderTask> renderTask) {
84 SkASSERT(!fRenderTasks.empty());
Chris Dalton6aeb8e82019-08-27 11:52:19 -060085 if (renderTask) {
86 // Release 'fRenderTasks.back()' and grab the raw pointer, in case the SkTArray grows
87 // and reallocates during emplace_back.
88 fRenderTasks.emplace_back(fRenderTasks.back().release());
89 return (fRenderTasks[fRenderTasks.count() - 2] = std::move(renderTask)).get();
90 }
91 return nullptr;
Robert Phillips22310d62018-09-05 11:07:21 -040092}
93
Greg Danielf41b2bd2019-08-22 16:19:24 -040094void GrDrawingManager::RenderTaskDAG::add(const SkTArray<sk_sp<GrRenderTask>>& renderTasks) {
Robert Phillips19f466d2020-02-26 10:27:07 -050095#ifdef SK_DEBUG
96 for (auto& renderTask : renderTasks) {
97 SkASSERT(renderTask->unique());
98 }
99#endif
100
Greg Danielf41b2bd2019-08-22 16:19:24 -0400101 fRenderTasks.push_back_n(renderTasks.count(), renderTasks.begin());
Robert Phillips22310d62018-09-05 11:07:21 -0400102}
103
Greg Danielf41b2bd2019-08-22 16:19:24 -0400104void GrDrawingManager::RenderTaskDAG::swap(SkTArray<sk_sp<GrRenderTask>>* renderTasks) {
105 SkASSERT(renderTasks->empty());
106 renderTasks->swap(fRenderTasks);
Robert Phillips22310d62018-09-05 11:07:21 -0400107}
108
Chris Dalton6b498102019-08-01 14:14:52 -0600109void GrDrawingManager::RenderTaskDAG::prepForFlush() {
Adlai Holler3078f852020-11-05 15:44:50 -0500110 if (!SkTTopoSort<GrRenderTask, GrRenderTask::TopoSortTraits>(&fRenderTasks)) {
111 SkDEBUGFAIL("Render task topo sort failed.");
112 return;
Robert Phillips22310d62018-09-05 11:07:21 -0400113 }
114
115#ifdef SK_DEBUG
Greg Danielf41b2bd2019-08-22 16:19:24 -0400116 // This block checks for any unnecessary splits in the opsTasks. If two sequential opsTasks
117 // share the same backing GrSurfaceProxy it means the opsTask was artificially split.
Chris Dalton6b498102019-08-01 14:14:52 -0600118 if (fRenderTasks.count()) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400119 GrOpsTask* prevOpsTask = fRenderTasks[0]->asOpsTask();
Chris Dalton6b498102019-08-01 14:14:52 -0600120 for (int i = 1; i < fRenderTasks.count(); ++i) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400121 GrOpsTask* curOpsTask = fRenderTasks[i]->asOpsTask();
Robert Phillips22310d62018-09-05 11:07:21 -0400122
Greg Danielf41b2bd2019-08-22 16:19:24 -0400123 if (prevOpsTask && curOpsTask) {
Adlai Holler33d569e2020-06-16 14:30:08 -0400124 SkASSERT(prevOpsTask->target(0).proxy() != curOpsTask->target(0).proxy());
Robert Phillips22310d62018-09-05 11:07:21 -0400125 }
126
Greg Danielf41b2bd2019-08-22 16:19:24 -0400127 prevOpsTask = curOpsTask;
Robert Phillips22310d62018-09-05 11:07:21 -0400128 }
129 }
130#endif
131}
132
Chris Dalton6b498102019-08-01 14:14:52 -0600133void GrDrawingManager::RenderTaskDAG::closeAll(const GrCaps* caps) {
Adlai Holler96ead542020-06-26 08:50:14 -0400134 for (auto& task : fRenderTasks) {
135 if (task) {
136 task->makeClosed(*caps);
Robert Phillips22310d62018-09-05 11:07:21 -0400137 }
138 }
139}
140
Robert Phillips22310d62018-09-05 11:07:21 -0400141///////////////////////////////////////////////////////////////////////////////////////////////////
Robert Phillips69893702019-02-22 11:16:30 -0500142GrDrawingManager::GrDrawingManager(GrRecordingContext* context,
Robert Phillips22310d62018-09-05 11:07:21 -0400143 const GrPathRendererChain::Options& optionsForPathRendererChain,
Greg Danielf41b2bd2019-08-22 16:19:24 -0400144 bool reduceOpsTaskSplitting)
Robert Phillips22310d62018-09-05 11:07:21 -0400145 : fContext(context)
146 , fOptionsForPathRendererChain(optionsForPathRendererChain)
Robert Phillips22310d62018-09-05 11:07:21 -0400147 , fPathRendererChain(nullptr)
148 , fSoftwarePathRenderer(nullptr)
Robert Phillips6db27c22019-05-01 10:43:56 -0400149 , fFlushing(false)
Herb Derby082232b2020-06-10 15:08:18 -0400150 , fReduceOpsTaskSplitting(reduceOpsTaskSplitting) { }
Robert Phillips22310d62018-09-05 11:07:21 -0400151
robertphillips3dc6ae52015-10-20 09:54:32 -0700152GrDrawingManager::~GrDrawingManager() {
Adlai Holler96ead542020-06-26 08:50:14 -0400153 fDAG.closeAll(fContext->priv().caps());
154 this->removeRenderTasks(0, fDAG.numRenderTasks());
robertphillips3dc6ae52015-10-20 09:54:32 -0700155}
156
Robert Phillipsa9162df2019-02-11 14:12:03 -0500157bool GrDrawingManager::wasAbandoned() const {
Robert Phillips9eb00022020-06-30 15:30:12 -0400158 return fContext->abandoned();
robertphillips3dc6ae52015-10-20 09:54:32 -0700159}
160
robertphillips68737822015-10-29 12:12:21 -0700161void GrDrawingManager::freeGpuResources() {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400162 for (int i = fOnFlushCBObjects.count() - 1; i >= 0; --i) {
163 if (!fOnFlushCBObjects[i]->retainOnFreeGpuResources()) {
164 // it's safe to just do this because we're iterating in reverse
165 fOnFlushCBObjects.removeShuffle(i);
166 }
167 }
168
robertphillips68737822015-10-29 12:12:21 -0700169 // a path renderer may be holding onto resources
robertphillips13391dd2015-10-30 05:15:11 -0700170 fPathRendererChain = nullptr;
Ben Wagner9ec70c62018-07-12 13:30:47 -0400171 fSoftwarePathRenderer = nullptr;
Robert Phillipse3302df2017-04-24 07:31:02 -0400172}
173
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500174// MDB TODO: make use of the 'proxies' parameter.
Greg Daniel9efe3862020-06-11 11:51:06 -0400175bool GrDrawingManager::flush(
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500176 SkSpan<GrSurfaceProxy*> proxies,
Greg Daniel9efe3862020-06-11 11:51:06 -0400177 SkSurface::BackendSurfaceAccess access,
178 const GrFlushInfo& info,
179 const GrBackendSurfaceMutableState* newState) {
Brian Salomon57d2beab2018-09-10 09:35:41 -0400180 GR_CREATE_TRACE_MARKER_CONTEXT("GrDrawingManager", "flush", fContext);
Brian Salomondcbb9d92017-07-19 10:53:20 -0400181
robertphillips7761d612016-05-16 09:14:53 -0700182 if (fFlushing || this->wasAbandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -0400183 if (info.fSubmittedProc) {
184 info.fSubmittedProc(info.fSubmittedContext, false);
185 }
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400186 if (info.fFinishedProc) {
187 info.fFinishedProc(info.fFinishedContext);
Greg Daniela3aa75a2019-04-12 14:24:55 -0400188 }
Greg Danielfe159622020-04-10 17:43:51 +0000189 return false;
joshualittb8918c42015-12-18 09:59:46 -0800190 }
Robert Phillips602df412019-04-08 11:10:39 -0400191
Robert Phillips38d64b02018-09-04 13:23:26 -0400192 SkDEBUGCODE(this->validate());
193
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500194 // As of now we only short-circuit if we got an explicit list of surfaces to flush.
195 if (!proxies.empty() && !info.fNumSemaphores && !info.fFinishedProc &&
Greg Danielce9f0162020-06-30 13:42:46 -0400196 access == SkSurface::BackendSurfaceAccess::kNoAccess && !newState) {
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500197 bool allUnused = std::all_of(proxies.begin(), proxies.end(), [&](GrSurfaceProxy* proxy) {
198 return !fDAG.isUsed(proxy) && !this->isDDLTarget(proxy);
199 });
200 if (allUnused) {
Greg Daniel55822f12020-05-26 11:26:45 -0400201 if (info.fSubmittedProc) {
202 info.fSubmittedProc(info.fSubmittedContext, true);
203 }
Greg Danielfe159622020-04-10 17:43:51 +0000204 return false;
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400205 }
Robert Phillips9313aa72019-04-09 18:41:27 -0400206 }
207
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400208 auto direct = fContext->asDirectContext();
Adlai Holleraee25fd2020-11-06 12:34:56 -0500209 SkASSERT(direct);
Brian Salomon9241a6d2019-10-03 13:26:54 -0400210 direct->priv().clientMappedBufferManager()->process();
Robert Phillips6a6de562019-02-15 15:19:15 -0500211
212 GrGpu* gpu = direct->priv().getGpu();
Greg Daniel55822f12020-05-26 11:26:45 -0400213 // We have a non abandoned and direct GrContext. It must have a GrGpu.
214 SkASSERT(gpu);
Greg Daniela3aa75a2019-04-12 14:24:55 -0400215
joshualittb8918c42015-12-18 09:59:46 -0800216 fFlushing = true;
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400217
Robert Phillips6a6de562019-02-15 15:19:15 -0500218 auto resourceProvider = direct->priv().resourceProvider();
219 auto resourceCache = direct->priv().getResourceCache();
220
Chris Dalton6b498102019-08-01 14:14:52 -0600221 // Semi-usually the GrRenderTasks are already closed at this point, but sometimes Ganesh needs
Greg Danielf41b2bd2019-08-22 16:19:24 -0400222 // to flush mid-draw. In that case, the SkGpuDevice's opsTasks won't be closed but need to be
223 // flushed anyway. Closing such opsTasks here will mean new ones will be created to replace them
Chris Dalton6b498102019-08-01 14:14:52 -0600224 // if the SkGpuDevice(s) write to them again.
Robert Phillips9da87e02019-02-04 13:26:26 -0500225 fDAG.closeAll(fContext->priv().caps());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400226 fActiveOpsTask = nullptr;
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400227
Robert Phillips22310d62018-09-05 11:07:21 -0400228 fDAG.prepForFlush();
Brian Salomon601ac802019-02-07 13:37:16 -0500229 if (!fCpuBufferCache) {
230 // We cache more buffers when the backend is using client side arrays. Otherwise, we
231 // expect each pool will use a CPU buffer as a staging buffer before uploading to a GPU
232 // buffer object. Each pool only requires one staging buffer at a time.
233 int maxCachedBuffers = fContext->priv().caps()->preferClientSideDynamicBuffers() ? 2 : 6;
234 fCpuBufferCache = GrBufferAllocPool::CpuBufferCache::Make(maxCachedBuffers);
Brian Salomon58f153c2018-10-18 21:51:15 -0400235 }
Robert Phillipsa4c93ac2017-05-18 11:40:04 -0400236
Robert Phillipse5f73282019-06-18 17:15:04 -0400237 GrOpFlushState flushState(gpu, resourceProvider, &fTokenTracker, fCpuBufferCache);
Robert Phillips40a29d72018-01-18 12:59:22 -0500238
Chris Daltonfe199b72017-05-05 11:26:15 -0400239 GrOnFlushResourceProvider onFlushProvider(this);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400240
Chris Dalton12658942017-10-05 19:45:25 -0600241 // Prepare any onFlush op lists (e.g. atlases).
Chris Daltonfe199b72017-05-05 11:26:15 -0400242 if (!fOnFlushCBObjects.empty()) {
Chris Dalton6b498102019-08-01 14:14:52 -0600243 fDAG.gatherIDs(&fFlushingRenderTaskIDs);
Robert Phillips22310d62018-09-05 11:07:21 -0400244
Chris Daltonfe199b72017-05-05 11:26:15 -0400245 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
Chris Dalton6b498102019-08-01 14:14:52 -0600246 onFlushCBObject->preFlush(&onFlushProvider, fFlushingRenderTaskIDs.begin(),
Chris Daltonc4b47352019-08-23 10:10:36 -0600247 fFlushingRenderTaskIDs.count());
248 }
249 for (const auto& onFlushRenderTask : fOnFlushRenderTasks) {
250 onFlushRenderTask->makeClosed(*fContext->priv().caps());
Chris Dalton706a6ff2017-11-29 22:01:06 -0700251#ifdef SK_DEBUG
Chris Daltonc4b47352019-08-23 10:10:36 -0600252 // OnFlush callbacks are invoked during flush, and are therefore expected to handle
253 // resource allocation & usage on their own. (No deferred or lazy proxies!)
254 onFlushRenderTask->visitTargetAndSrcProxies_debugOnly(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400255 [](GrSurfaceProxy* p, GrMipmapped mipMapped) {
Chris Daltonc4b47352019-08-23 10:10:36 -0600256 SkASSERT(!p->asTextureProxy() || !p->asTextureProxy()->texPriv().isDeferred());
Brian Salomonbeb7f522019-08-30 16:19:42 -0400257 SkASSERT(!p->isLazy());
Chris Dalton4ece96d2019-08-30 11:26:39 -0600258 if (p->requiresManualMSAAResolve()) {
259 // The onFlush callback is responsible for ensuring MSAA gets resolved.
260 SkASSERT(p->asRenderTargetProxy() && !p->asRenderTargetProxy()->isMSAADirty());
261 }
Brian Salomon7e67dca2020-07-21 09:27:25 -0400262 if (GrMipmapped::kYes == mipMapped) {
Chris Daltonc4b47352019-08-23 10:10:36 -0600263 // The onFlush callback is responsible for regenerating mips if needed.
Brian Salomon8c82a872020-07-21 12:09:58 -0400264 SkASSERT(p->asTextureProxy() && !p->asTextureProxy()->mipmapsAreDirty());
Chris Daltonc4b47352019-08-23 10:10:36 -0600265 }
266 });
Chris Dalton706a6ff2017-11-29 22:01:06 -0700267#endif
Chris Daltonc4b47352019-08-23 10:10:36 -0600268 onFlushRenderTask->prepare(&flushState);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400269 }
270 }
271
robertphillipsa13e2022015-11-11 12:01:09 -0800272#if 0
Brian Salomon09d994e2016-12-21 11:14:46 -0500273 // Enable this to print out verbose GrOp information
Adlai Hollerabdfd392020-10-19 09:00:36 -0400274 SkDEBUGCODE(SkDebugf("onFlush renderTasks (%d):\n", fOnFlushRenderTasks.count()));
Chris Daltonc4b47352019-08-23 10:10:36 -0600275 for (const auto& onFlushRenderTask : fOnFlushRenderTasks) {
Adlai Hollerabdfd392020-10-19 09:00:36 -0400276 SkDEBUGCODE(onFlushRenderTask->dump(/* printDependencies */ true);)
Chris Daltonc4b47352019-08-23 10:10:36 -0600277 }
Adlai Hollerabdfd392020-10-19 09:00:36 -0400278 SkDEBUGCODE(SkDebugf("Normal renderTasks (%d):\n", fDAG.numRenderTasks()));
279 for (int i = 0; i < fDAG.numRenderTasks(); ++i) {
280 SkDEBUGCODE(fDAG.renderTask(i)->dump(/* printDependencies */ true);)
robertphillips3dc6ae52015-10-20 09:54:32 -0700281 }
robertphillipsa13e2022015-11-11 12:01:09 -0800282#endif
283
Robert Phillipseafd48a2017-11-16 07:52:08 -0500284 int startIndex, stopIndex;
285 bool flushed = false;
286
Robert Phillipsf8e25022017-11-08 15:24:31 -0500287 {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400288 GrResourceAllocator alloc(resourceProvider SkDEBUGCODE(, fDAG.numRenderTasks()));
Chris Dalton6b498102019-08-01 14:14:52 -0600289 for (int i = 0; i < fDAG.numRenderTasks(); ++i) {
290 if (fDAG.renderTask(i)) {
291 fDAG.renderTask(i)->gatherProxyIntervals(&alloc);
Robert Phillips22310d62018-09-05 11:07:21 -0400292 }
Greg Danielf41b2bd2019-08-22 16:19:24 -0400293 alloc.markEndOfOpsTask(i);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500294 }
Robert Phillipsc73666f2019-04-24 08:49:48 -0400295 alloc.determineRecyclability();
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400296
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500297 GrResourceAllocator::AssignError error = GrResourceAllocator::AssignError::kNoError;
Chris Dalton6b498102019-08-01 14:14:52 -0600298 int numRenderTasksExecuted = 0;
Brian Salomon577aa0f2018-11-30 13:32:23 -0500299 while (alloc.assign(&startIndex, &stopIndex, &error)) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500300 if (GrResourceAllocator::AssignError::kFailedProxyInstantiation == error) {
301 for (int i = startIndex; i < stopIndex; ++i) {
Chris Dalton6b498102019-08-01 14:14:52 -0600302 GrRenderTask* renderTask = fDAG.renderTask(i);
303 if (!renderTask) {
304 continue;
Robert Phillips01a91282018-07-26 08:03:04 -0400305 }
Chris Dalton6b498102019-08-01 14:14:52 -0600306 if (!renderTask->isInstantiated()) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400307 // No need to call the renderTask's handleInternalAllocationFailure
308 // since we will already skip executing the renderTask since it is not
309 // instantiated.
Chris Dalton6b498102019-08-01 14:14:52 -0600310 continue;
Robert Phillipsbbcb7f72018-05-31 11:16:19 -0400311 }
Chris Dalton6b498102019-08-01 14:14:52 -0600312 renderTask->handleInternalAllocationFailure();
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500313 }
Adlai Holler96ead542020-06-26 08:50:14 -0400314 this->removeRenderTasks(startIndex, stopIndex);
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500315 }
Robert Phillips4150eea2018-02-07 17:08:21 -0500316
Chris Dalton6b498102019-08-01 14:14:52 -0600317 if (this->executeRenderTasks(
318 startIndex, stopIndex, &flushState, &numRenderTasksExecuted)) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500319 flushed = true;
320 }
bsalomondc438982016-08-31 11:53:49 -0700321 }
Greg Danielc42b20b2017-10-04 10:34:49 -0400322 }
323
Chris Dalton91ab1552018-04-18 13:24:25 -0600324#ifdef SK_DEBUG
Chris Dalton6b498102019-08-01 14:14:52 -0600325 for (int i = 0; i < fDAG.numRenderTasks(); ++i) {
Adlai Holler96ead542020-06-26 08:50:14 -0400326 // All render tasks should have been cleared out by now – we only reset the array below to
327 // reclaim storage.
328 SkASSERT(!fDAG.renderTask(i));
Chris Dalton91ab1552018-04-18 13:24:25 -0600329 }
330#endif
Adlai Holler25df1f72020-06-09 13:08:27 -0400331 fLastRenderTasks.reset();
Robert Phillips22310d62018-09-05 11:07:21 -0400332 fDAG.reset();
Robert Phillips15c91422019-05-07 16:54:48 -0400333 this->clearDDLTargets();
robertphillipsa13e2022015-11-11 12:01:09 -0800334
Robert Phillipsc994a932018-06-19 13:09:54 -0400335#ifdef SK_DEBUG
336 // In non-DDL mode this checks that all the flushed ops have been freed from the memory pool.
337 // When we move to partial flushes this assert will no longer be valid.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400338 // In DDL mode this check is somewhat superfluous since the memory for most of the ops/opsTasks
Robert Phillipsc994a932018-06-19 13:09:54 -0400339 // will be stored in the DDL's GrOpMemoryPools.
Herb Derbye32e1ab2020-10-27 10:29:46 -0400340 GrMemoryPool* opMemoryPool = fContext->priv().opMemoryPool();
Robert Phillipsc994a932018-06-19 13:09:54 -0400341 opMemoryPool->isEmpty();
342#endif
343
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500344 gpu->executeFlushInfo(proxies, access, info, newState);
robertphillipsa13e2022015-11-11 12:01:09 -0800345
Brian Salomon57d2beab2018-09-10 09:35:41 -0400346 // Give the cache a chance to purge resources that become purgeable due to flushing.
347 if (flushed) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500348 resourceCache->purgeAsNeeded();
Brian Salomon876a0172019-03-08 11:12:14 -0500349 flushed = false;
bsalomonb77a9072016-09-07 10:02:04 -0700350 }
Chris Daltonfe199b72017-05-05 11:26:15 -0400351 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
Chris Dalton6b498102019-08-01 14:14:52 -0600352 onFlushCBObject->postFlush(fTokenTracker.nextTokenToFlush(), fFlushingRenderTaskIDs.begin(),
353 fFlushingRenderTaskIDs.count());
Brian Salomon876a0172019-03-08 11:12:14 -0500354 flushed = true;
355 }
356 if (flushed) {
357 resourceCache->purgeAsNeeded();
Chris Daltonfe199b72017-05-05 11:26:15 -0400358 }
Chris Dalton6b498102019-08-01 14:14:52 -0600359 fFlushingRenderTaskIDs.reset();
joshualittb8918c42015-12-18 09:59:46 -0800360 fFlushing = false;
Greg Daniel51316782017-08-02 15:10:09 +0000361
Greg Danielfe159622020-04-10 17:43:51 +0000362 return true;
363}
364
365bool GrDrawingManager::submitToGpu(bool syncToCpu) {
366 if (fFlushing || this->wasAbandoned()) {
367 return false;
368 }
369
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400370 auto direct = fContext->asDirectContext();
Greg Danielfe159622020-04-10 17:43:51 +0000371 if (!direct) {
372 return false; // Can't submit while DDL recording
373 }
374 GrGpu* gpu = direct->priv().getGpu();
375 return gpu->submitToGpu(syncToCpu);
robertphillips3dc6ae52015-10-20 09:54:32 -0700376}
377
Chris Dalton6b498102019-08-01 14:14:52 -0600378bool GrDrawingManager::executeRenderTasks(int startIndex, int stopIndex, GrOpFlushState* flushState,
379 int* numRenderTasksExecuted) {
380 SkASSERT(startIndex <= stopIndex && stopIndex <= fDAG.numRenderTasks());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500381
Robert Phillips27483912018-04-20 12:43:18 -0400382#if GR_FLUSH_TIME_OP_SPEW
Greg Danielf41b2bd2019-08-22 16:19:24 -0400383 SkDebugf("Flushing opsTask: %d to %d out of [%d, %d]\n",
Chris Dalton6b498102019-08-01 14:14:52 -0600384 startIndex, stopIndex, 0, fDAG.numRenderTasks());
Robert Phillips27483912018-04-20 12:43:18 -0400385 for (int i = startIndex; i < stopIndex; ++i) {
Chris Dalton6b498102019-08-01 14:14:52 -0600386 if (fDAG.renderTask(i)) {
387 fDAG.renderTask(i)->dump(true);
Robert Phillips1734dd32018-08-21 13:52:09 -0400388 }
Robert Phillips27483912018-04-20 12:43:18 -0400389 }
390#endif
391
Chris Dalton6b498102019-08-01 14:14:52 -0600392 bool anyRenderTasksExecuted = false;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500393
394 for (int i = startIndex; i < stopIndex; ++i) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400395 GrRenderTask* renderTask = fDAG.renderTask(i);
396 if (!renderTask || !renderTask->isInstantiated()) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500397 continue;
398 }
399
Chris Dalton6b498102019-08-01 14:14:52 -0600400 SkASSERT(renderTask->deferredProxiesAreInstantiated());
Robert Phillips22310d62018-09-05 11:07:21 -0400401
Chris Dalton6b498102019-08-01 14:14:52 -0600402 renderTask->prepare(flushState);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500403 }
404
405 // Upload all data to the GPU
406 flushState->preExecuteDraws();
407
Greg Danield2073452018-12-07 11:20:33 -0500408 // For Vulkan, if we have too many oplists to be flushed we end up allocating a lot of resources
409 // for each command buffer associated with the oplists. If this gets too large we can cause the
410 // devices to go OOM. In practice we usually only hit this case in our tests, but to be safe we
411 // put a cap on the number of oplists we will execute before flushing to the GPU to relieve some
412 // memory pressure.
Chris Dalton6b498102019-08-01 14:14:52 -0600413 static constexpr int kMaxRenderTasksBeforeFlush = 100;
Greg Danield2073452018-12-07 11:20:33 -0500414
Chris Daltonc4b47352019-08-23 10:10:36 -0600415 // Execute the onFlush renderTasks first, if any.
416 for (sk_sp<GrRenderTask>& onFlushRenderTask : fOnFlushRenderTasks) {
417 if (!onFlushRenderTask->execute(flushState)) {
418 SkDebugf("WARNING: onFlushRenderTask failed to execute.\n");
Robert Phillipseafd48a2017-11-16 07:52:08 -0500419 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600420 SkASSERT(onFlushRenderTask->unique());
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400421 onFlushRenderTask->disown(this);
Chris Daltonc4b47352019-08-23 10:10:36 -0600422 onFlushRenderTask = nullptr;
Chris Dalton6b498102019-08-01 14:14:52 -0600423 (*numRenderTasksExecuted)++;
424 if (*numRenderTasksExecuted >= kMaxRenderTasksBeforeFlush) {
Greg Danielfe159622020-04-10 17:43:51 +0000425 flushState->gpu()->submitToGpu(false);
Chris Dalton6b498102019-08-01 14:14:52 -0600426 *numRenderTasksExecuted = 0;
Greg Danield2073452018-12-07 11:20:33 -0500427 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500428 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600429 fOnFlushRenderTasks.reset();
Robert Phillipseafd48a2017-11-16 07:52:08 -0500430
431 // Execute the normal op lists.
432 for (int i = startIndex; i < stopIndex; ++i) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400433 GrRenderTask* renderTask = fDAG.renderTask(i);
434 if (!renderTask || !renderTask->isInstantiated()) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500435 continue;
436 }
437
Greg Daniel15ecdf92019-08-30 15:35:23 -0400438 if (renderTask->execute(flushState)) {
Chris Dalton6b498102019-08-01 14:14:52 -0600439 anyRenderTasksExecuted = true;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500440 }
Chris Dalton6b498102019-08-01 14:14:52 -0600441 (*numRenderTasksExecuted)++;
442 if (*numRenderTasksExecuted >= kMaxRenderTasksBeforeFlush) {
Greg Danielfe159622020-04-10 17:43:51 +0000443 flushState->gpu()->submitToGpu(false);
Chris Dalton6b498102019-08-01 14:14:52 -0600444 *numRenderTasksExecuted = 0;
Greg Danield2073452018-12-07 11:20:33 -0500445 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500446 }
447
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400448 SkASSERT(!flushState->opsRenderPass());
Robert Phillips40a29d72018-01-18 12:59:22 -0500449 SkASSERT(fTokenTracker.nextDrawToken() == fTokenTracker.nextTokenToFlush());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500450
Chris Dalton6b498102019-08-01 14:14:52 -0600451 // We reset the flush state before the RenderTasks so that the last resources to be freed are
452 // those that are written to in the RenderTasks. This helps to make sure the most recently used
453 // resources are the last to be purged by the resource cache.
Robert Phillipseafd48a2017-11-16 07:52:08 -0500454 flushState->reset();
455
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400456 this->removeRenderTasks(startIndex, stopIndex);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500457
Chris Dalton6b498102019-08-01 14:14:52 -0600458 return anyRenderTasksExecuted;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500459}
460
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400461void GrDrawingManager::removeRenderTasks(int startIndex, int stopIndex) {
462 for (int i = startIndex; i < stopIndex; ++i) {
463 GrRenderTask* task = fDAG.renderTask(i);
464 if (!task) {
465 continue;
466 }
Robert Phillips02dd0ed2020-11-09 17:03:34 -0500467 if (!task->unique() || task->requiresExplicitCleanup()) {
468 // TODO: Eventually uniqueness should be guaranteed: http://skbug.com/7111.
469 // DDLs, however, will always require an explicit notification for when they
470 // can clean up resources.
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400471 task->endFlush(this);
472 }
473 task->disown(this);
474 }
Adlai Holler96ead542020-06-26 08:50:14 -0400475 fDAG.rawRemoveRenderTasks(startIndex, stopIndex);
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400476}
477
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400478static void resolve_and_mipmap(GrGpu* gpu, GrSurfaceProxy* proxy) {
479 if (!proxy->isInstantiated()) {
480 return;
481 }
482
483 // In the flushSurfaces case, we need to resolve MSAA immediately after flush. This is
484 // because clients expect the flushed surface's backing texture to be fully resolved
485 // upon return.
486 if (proxy->requiresManualMSAAResolve()) {
487 auto* rtProxy = proxy->asRenderTargetProxy();
488 SkASSERT(rtProxy);
489 if (rtProxy->isMSAADirty()) {
490 SkASSERT(rtProxy->peekRenderTarget());
Jim Van Verthbb61fe32020-07-07 16:39:04 -0400491 gpu->resolveRenderTarget(rtProxy->peekRenderTarget(), rtProxy->msaaDirtyRect());
492 gpu->submitToGpu(false);
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400493 rtProxy->markMSAAResolved();
494 }
495 }
496 // If, after a flush, any of the proxies of interest have dirty mipmaps, regenerate them in
497 // case their backend textures are being stolen.
498 // (This special case is exercised by the ReimportImageTextureWithMipLevels test.)
499 // FIXME: It may be more ideal to plumb down a "we're going to steal the backends" flag.
500 if (auto* textureProxy = proxy->asTextureProxy()) {
Brian Salomon8c82a872020-07-21 12:09:58 -0400501 if (textureProxy->mipmapsAreDirty()) {
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400502 SkASSERT(textureProxy->peekTexture());
503 gpu->regenerateMipMapLevels(textureProxy->peekTexture());
Brian Salomon8c82a872020-07-21 12:09:58 -0400504 textureProxy->markMipmapsClean();
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400505 }
506 }
507}
508
Greg Daniel9efe3862020-06-11 11:51:06 -0400509GrSemaphoresSubmitted GrDrawingManager::flushSurfaces(
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500510 SkSpan<GrSurfaceProxy*> proxies,
Greg Daniel9efe3862020-06-11 11:51:06 -0400511 SkSurface::BackendSurfaceAccess access,
512 const GrFlushInfo& info,
513 const GrBackendSurfaceMutableState* newState) {
bsalomon6a2b1942016-09-08 11:28:59 -0700514 if (this->wasAbandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -0400515 if (info.fSubmittedProc) {
516 info.fSubmittedProc(info.fSubmittedContext, false);
517 }
518 if (info.fFinishedProc) {
519 info.fFinishedProc(info.fFinishedContext);
520 }
Greg Daniel51316782017-08-02 15:10:09 +0000521 return GrSemaphoresSubmitted::kNo;
bsalomon6a2b1942016-09-08 11:28:59 -0700522 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400523 SkDEBUGCODE(this->validate());
bsalomon6a2b1942016-09-08 11:28:59 -0700524
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400525 auto direct = fContext->asDirectContext();
Adlai Holleraee25fd2020-11-06 12:34:56 -0500526 SkASSERT(direct);
Robert Phillips6a6de562019-02-15 15:19:15 -0500527 GrGpu* gpu = direct->priv().getGpu();
Greg Daniel55822f12020-05-26 11:26:45 -0400528 // We have a non abandoned and direct GrContext. It must have a GrGpu.
529 SkASSERT(gpu);
Robert Phillips874b5352018-03-16 08:48:24 -0400530
Robert Phillipsacc10fa2019-04-01 09:50:20 -0400531 // TODO: It is important to upgrade the drawingmanager to just flushing the
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400532 // portion of the DAG required by 'proxies' in order to restore some of the
Robert Phillipsacc10fa2019-04-01 09:50:20 -0400533 // semantics of this method.
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500534 bool didFlush = this->flush(proxies, access, info, newState);
535 for (GrSurfaceProxy* proxy : proxies) {
536 resolve_and_mipmap(gpu, proxy);
bsalomon6a2b1942016-09-08 11:28:59 -0700537 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400538
539 SkDEBUGCODE(this->validate());
Greg Danielfe159622020-04-10 17:43:51 +0000540
Greg Daniel04283f32020-05-20 13:16:00 -0400541 if (!didFlush || (!direct->priv().caps()->semaphoreSupport() && info.fNumSemaphores)) {
Greg Danielfe159622020-04-10 17:43:51 +0000542 return GrSemaphoresSubmitted::kNo;
543 }
544 return GrSemaphoresSubmitted::kYes;
bsalomon6a2b1942016-09-08 11:28:59 -0700545}
546
Chris Daltonfe199b72017-05-05 11:26:15 -0400547void GrDrawingManager::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
548 fOnFlushCBObjects.push_back(onFlushCBObject);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400549}
550
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500551#if GR_TEST_UTILS
552void GrDrawingManager::testingOnly_removeOnFlushCallbackObject(GrOnFlushCallbackObject* cb) {
553 int n = std::find(fOnFlushCBObjects.begin(), fOnFlushCBObjects.end(), cb) -
554 fOnFlushCBObjects.begin();
555 SkASSERT(n < fOnFlushCBObjects.count());
556 fOnFlushCBObjects.removeShuffle(n);
557}
558#endif
559
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400560void GrDrawingManager::setLastRenderTask(const GrSurfaceProxy* proxy, GrRenderTask* task) {
561#ifdef SK_DEBUG
Robert Phillips02dd0ed2020-11-09 17:03:34 -0500562 if (auto prior = this->getLastRenderTask(proxy)) {
563 SkASSERT(prior->isClosed() || prior == task);
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400564 }
565#endif
Adlai Holler25df1f72020-06-09 13:08:27 -0400566 uint32_t key = proxy->uniqueID().asUInt();
567 if (task) {
568 fLastRenderTasks.set(key, task);
569 } else if (fLastRenderTasks.find(key)) {
570 fLastRenderTasks.remove(key);
571 }
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400572}
573
574GrRenderTask* GrDrawingManager::getLastRenderTask(const GrSurfaceProxy* proxy) const {
575 auto entry = fLastRenderTasks.find(proxy->uniqueID().asUInt());
576 return entry ? *entry : nullptr;
577}
578
579GrOpsTask* GrDrawingManager::getLastOpsTask(const GrSurfaceProxy* proxy) const {
580 GrRenderTask* task = this->getLastRenderTask(proxy);
581 return task ? task->asOpsTask() : nullptr;
582}
583
584
Chris Dalton6b498102019-08-01 14:14:52 -0600585void GrDrawingManager::moveRenderTasksToDDL(SkDeferredDisplayList* ddl) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400586 SkDEBUGCODE(this->validate());
587
Chris Dalton6b498102019-08-01 14:14:52 -0600588 // no renderTask should receive a new command after this
Robert Phillips9da87e02019-02-04 13:26:26 -0500589 fDAG.closeAll(fContext->priv().caps());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400590 fActiveOpsTask = nullptr;
Robert Phillips7a137052018-02-01 11:23:12 -0500591
Robert Phillips02dd0ed2020-11-09 17:03:34 -0500592 fDAG.prepForFlush();
593
Chris Dalton6b498102019-08-01 14:14:52 -0600594 fDAG.swap(&ddl->fRenderTasks);
Robert Phillips19f466d2020-02-26 10:27:07 -0500595 SkASSERT(!fDAG.numRenderTasks());
Robert Phillips867ce8f2018-06-21 10:28:36 -0400596
Robert Phillips19f466d2020-02-26 10:27:07 -0500597 for (auto& renderTask : ddl->fRenderTasks) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400598 renderTask->disown(this);
Robert Phillips29f38542019-10-16 09:20:25 -0400599 renderTask->prePrepare(fContext);
Robert Phillips7327c9d2019-10-08 16:32:56 -0400600 }
601
Michael Ludwig2c316bd2019-12-19 14:50:44 -0500602 ddl->fArenas = std::move(fContext->priv().detachArenas());
Robert Phillips61fc7992019-10-22 11:58:17 -0400603
Robert Phillipsf6a0b452020-02-18 14:26:46 -0500604 fContext->priv().detachProgramData(&ddl->fProgramData);
Robert Phillips576b6a12019-12-06 13:05:49 -0500605
Robert Phillips774168e2018-05-31 12:43:27 -0400606 if (fPathRendererChain) {
607 if (auto ccpr = fPathRendererChain->getCoverageCountingPathRenderer()) {
608 ddl->fPendingPaths = ccpr->detachPendingPaths();
609 }
610 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400611
612 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500613}
614
Robert Phillips02dd0ed2020-11-09 17:03:34 -0500615void GrDrawingManager::createDDLTask(sk_sp<const SkDeferredDisplayList> ddl,
616 GrRenderTargetProxy* newDest) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400617 SkDEBUGCODE(this->validate());
618
Greg Danielf41b2bd2019-08-22 16:19:24 -0400619 if (fActiveOpsTask) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400620 // This is a temporary fix for the partial-MDB world. In that world we're not
Greg Danielf41b2bd2019-08-22 16:19:24 -0400621 // reordering so ops that (in the single opsTask world) would've just glommed onto the
622 // end of the single opsTask but referred to a far earlier RT need to appear in their
623 // own opsTask.
624 fActiveOpsTask->makeClosed(*fContext->priv().caps());
625 fActiveOpsTask = nullptr;
Robert Phillips38d64b02018-09-04 13:23:26 -0400626 }
627
Robert Phillips02dd0ed2020-11-09 17:03:34 -0500628 // Propagate the DDL proxy's state information to the replay target.
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400629 if (ddl->priv().targetProxy()->isMSAADirty()) {
630 newDest->markMSAADirty(ddl->priv().targetProxy()->msaaDirtyRect(),
631 ddl->characterization().origin());
632 }
633 GrTextureProxy* newTextureProxy = newDest->asTextureProxy();
Brian Salomon8c82a872020-07-21 12:09:58 -0400634 if (newTextureProxy && GrMipmapped::kYes == newTextureProxy->mipmapped()) {
635 newTextureProxy->markMipmapsDirty();
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400636 }
637
638 this->addDDLTarget(newDest, ddl->priv().targetProxy());
Robert Phillips15c91422019-05-07 16:54:48 -0400639
Robert Phillips62000362018-02-01 09:10:04 -0500640 // Here we jam the proxy that backs the current replay SkSurface into the LazyProxyData.
Robert Phillips02dd0ed2020-11-09 17:03:34 -0500641 // The lazy proxy that references it (in the DDL opsTasks) will then steal its GrTexture.
Robert Phillips62000362018-02-01 09:10:04 -0500642 ddl->fLazyProxyData->fReplayDest = newDest;
Robert Phillips774168e2018-05-31 12:43:27 -0400643
644 if (ddl->fPendingPaths.size()) {
645 GrCoverageCountingPathRenderer* ccpr = this->getCoverageCountingPathRenderer();
646
647 ccpr->mergePendingPaths(ddl->fPendingPaths);
648 }
Robert Phillips22310d62018-09-05 11:07:21 -0400649
Robert Phillips02dd0ed2020-11-09 17:03:34 -0500650 // Add a task to handle drawing and lifetime management of the DDL.
651 SkDEBUGCODE(auto ddlTask =) fDAG.add(sk_make_sp<GrDDLTask>(this,
652 sk_ref_sp(newDest),
653 std::move(ddl)));
654 SkASSERT(ddlTask->isClosed());
Adlai Holler7580ad42020-06-24 13:45:25 -0400655
Robert Phillips38d64b02018-09-04 13:23:26 -0400656 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500657}
658
Robert Phillips38d64b02018-09-04 13:23:26 -0400659#ifdef SK_DEBUG
660void GrDrawingManager::validate() const {
Adlai Holler3078f852020-11-05 15:44:50 -0500661 if (fReduceOpsTaskSplitting) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400662 SkASSERT(!fActiveOpsTask);
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400663 } else {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400664 if (fActiveOpsTask) {
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400665 SkASSERT(!fDAG.empty());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400666 SkASSERT(!fActiveOpsTask->isClosed());
667 SkASSERT(fActiveOpsTask == fDAG.back());
Robert Phillips38d64b02018-09-04 13:23:26 -0400668 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400669
Chris Dalton6b498102019-08-01 14:14:52 -0600670 for (int i = 0; i < fDAG.numRenderTasks(); ++i) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400671 if (fActiveOpsTask != fDAG.renderTask(i)) {
Chris Daltone2a903e2019-09-18 13:41:50 -0600672 // The resolveTask associated with the activeTask remains open for as long as the
673 // activeTask does.
674 bool isActiveResolveTask =
675 fActiveOpsTask && fActiveOpsTask->fTextureResolveTask == fDAG.renderTask(i);
676 SkASSERT(isActiveResolveTask || fDAG.renderTask(i)->isClosed());
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400677 }
678 }
679
680 if (!fDAG.empty() && !fDAG.back()->isClosed()) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400681 SkASSERT(fActiveOpsTask == fDAG.back());
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400682 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400683 }
684}
685#endif
686
Greg Danielbbfec9d2019-08-20 10:56:51 -0400687void GrDrawingManager::closeRenderTasksForNewRenderTask(GrSurfaceProxy* target) {
Adlai Holler3078f852020-11-05 15:44:50 -0500688 if (target && fReduceOpsTaskSplitting) {
Chris Dalton5fe99772019-08-06 11:57:39 -0600689 // In this case we need to close all the renderTasks that rely on the current contents of
690 // 'target'. That is bc we're going to update the content of the proxy so they need to be
691 // split in case they use both the old and new content. (This is a bit of an overkill: they
692 // really only need to be split if they ever reference proxy's contents again but that is
693 // hard to predict/handle).
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400694 if (GrRenderTask* lastRenderTask = this->getLastRenderTask(target)) {
Chris Dalton6b498102019-08-01 14:14:52 -0600695 lastRenderTask->closeThoseWhoDependOnMe(*fContext->priv().caps());
Robert Phillips46acf9d2018-10-09 09:31:40 -0400696 }
Greg Danielf41b2bd2019-08-22 16:19:24 -0400697 } else if (fActiveOpsTask) {
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400698 // This is a temporary fix for the partial-MDB world. In that world we're not
Greg Danielf41b2bd2019-08-22 16:19:24 -0400699 // reordering so ops that (in the single opsTask world) would've just glommed onto the
700 // end of the single opsTask but referred to a far earlier RT need to appear in their
701 // own opsTask.
702 fActiveOpsTask->makeClosed(*fContext->priv().caps());
703 fActiveOpsTask = nullptr;
robertphillips3dc6ae52015-10-20 09:54:32 -0700704 }
Chris Dalton5fe99772019-08-06 11:57:39 -0600705}
706
Greg Daniel16f5c652019-10-29 11:26:01 -0400707sk_sp<GrOpsTask> GrDrawingManager::newOpsTask(GrSurfaceProxyView surfaceView,
708 bool managedOpsTask) {
Chris Dalton5fe99772019-08-06 11:57:39 -0600709 SkDEBUGCODE(this->validate());
710 SkASSERT(fContext);
711
Greg Daniel16f5c652019-10-29 11:26:01 -0400712 GrSurfaceProxy* proxy = surfaceView.proxy();
713 this->closeRenderTasksForNewRenderTask(proxy);
robertphillips3dc6ae52015-10-20 09:54:32 -0700714
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400715 sk_sp<GrOpsTask> opsTask(new GrOpsTask(this, fContext->priv().arenas(),
Michael Ludwigd0840ec2019-12-12 09:48:38 -0500716 std::move(surfaceView),
717 fContext->priv().auditTrail()));
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400718 SkASSERT(this->getLastRenderTask(proxy) == opsTask.get());
robertphillips3dc6ae52015-10-20 09:54:32 -0700719
Greg Danielf41b2bd2019-08-22 16:19:24 -0400720 if (managedOpsTask) {
721 fDAG.add(opsTask);
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400722
Adlai Holler3078f852020-11-05 15:44:50 -0500723 if (!fReduceOpsTaskSplitting) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400724 fActiveOpsTask = opsTask.get();
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400725 }
Robert Phillips941d1442017-06-14 16:37:02 -0400726 }
robertphillips3dc6ae52015-10-20 09:54:32 -0700727
Robert Phillips38d64b02018-09-04 13:23:26 -0400728 SkDEBUGCODE(this->validate());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400729 return opsTask;
robertphillips3dc6ae52015-10-20 09:54:32 -0700730}
731
Chris Daltone2a903e2019-09-18 13:41:50 -0600732GrTextureResolveRenderTask* GrDrawingManager::newTextureResolveRenderTask(const GrCaps& caps) {
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600733 // 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 -0600734 // in sorting and opsTask reduction mode) the render tasks that depend on any proxy's current
Greg Danielf41b2bd2019-08-22 16:19:24 -0400735 // state. This is because those opsTasks can still receive new ops and because if they refer to
Chris Dalton4ece96d2019-08-30 11:26:39 -0600736 // the mipmapped version of 'proxy', they will then come to depend on the render task being
737 // created here.
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600738 //
Greg Danielf41b2bd2019-08-22 16:19:24 -0400739 // Add the new textureResolveTask before the fActiveOpsTask (if not in
740 // sorting/opsTask-splitting-reduction mode) because it will depend upon this resolve task.
Chris Dalton3d770272019-08-14 09:24:37 -0600741 // NOTE: Putting it here will also reduce the amount of work required by the topological sort.
Chris Daltone2a903e2019-09-18 13:41:50 -0600742 return static_cast<GrTextureResolveRenderTask*>(fDAG.addBeforeLast(
743 sk_make_sp<GrTextureResolveRenderTask>()));
Chris Dalton3d770272019-08-14 09:24:37 -0600744}
745
Greg Danielc30f1a92019-09-06 15:28:58 -0400746void GrDrawingManager::newWaitRenderTask(sk_sp<GrSurfaceProxy> proxy,
Greg Daniel301015c2019-11-18 14:06:46 -0500747 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> semaphores,
Greg Danielc30f1a92019-09-06 15:28:58 -0400748 int numSemaphores) {
749 SkDEBUGCODE(this->validate());
750 SkASSERT(fContext);
751
752 const GrCaps& caps = *fContext->priv().caps();
753
Greg Daniel16f5c652019-10-29 11:26:01 -0400754 sk_sp<GrWaitRenderTask> waitTask = sk_make_sp<GrWaitRenderTask>(GrSurfaceProxyView(proxy),
755 std::move(semaphores),
Greg Danielc30f1a92019-09-06 15:28:58 -0400756 numSemaphores);
757 if (fReduceOpsTaskSplitting) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400758 GrRenderTask* lastTask = this->getLastRenderTask(proxy.get());
Greg Danielc30f1a92019-09-06 15:28:58 -0400759 if (lastTask && !lastTask->isClosed()) {
760 // We directly make the currently open renderTask depend on waitTask instead of using
761 // the proxy version of addDependency. The waitTask will never need to trigger any
762 // resolves or mip map generation which is the main advantage of going through the proxy
763 // version. Additionally we would've had to temporarily set the wait task as the
764 // lastRenderTask on the proxy, add the dependency, and then reset the lastRenderTask to
765 // lastTask. Additionally we add all dependencies of lastTask to waitTask so that the
766 // waitTask doesn't get reordered before them and unnecessarily block those tasks.
767 // Note: Any previous Ops already in lastTask will get blocked by the wait semaphore
768 // even though they don't need to be for correctness.
769
770 // Make sure we add the dependencies of lastTask to waitTask first or else we'll get a
771 // circular self dependency of waitTask on waitTask.
772 waitTask->addDependenciesFromOtherTask(lastTask);
773 lastTask->addDependency(waitTask.get());
774 } else {
775 // If there is a last task we set the waitTask to depend on it so that it doesn't get
776 // reordered in front of the lastTask causing the lastTask to be blocked by the
777 // semaphore. Again we directly just go through adding the dependency to the task and
778 // not the proxy since we don't need to worry about resolving anything.
779 if (lastTask) {
780 waitTask->addDependency(lastTask);
781 }
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400782 this->setLastRenderTask(proxy.get(), waitTask.get());
Greg Danielc30f1a92019-09-06 15:28:58 -0400783 }
784 fDAG.add(waitTask);
785 } else {
Adlai Holler33d569e2020-06-16 14:30:08 -0400786 if (fActiveOpsTask && (fActiveOpsTask->target(0).proxy() == proxy.get())) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400787 SkASSERT(this->getLastRenderTask(proxy.get()) == fActiveOpsTask);
Greg Danielc30f1a92019-09-06 15:28:58 -0400788 fDAG.addBeforeLast(waitTask);
789 // In this case we keep the current renderTask open but just insert the new waitTask
790 // before it in the list. The waitTask will never need to trigger any resolves or mip
791 // map generation which is the main advantage of going through the proxy version.
792 // Additionally we would've had to temporarily set the wait task as the lastRenderTask
793 // on the proxy, add the dependency, and then reset the lastRenderTask to
794 // fActiveOpsTask. Additionally we make the waitTask depend on all of fActiveOpsTask
795 // dependencies so that we don't unnecessarily reorder the waitTask before them.
796 // Note: Any previous Ops already in fActiveOpsTask will get blocked by the wait
797 // semaphore even though they don't need to be for correctness.
798
799 // Make sure we add the dependencies of fActiveOpsTask to waitTask first or else we'll
800 // get a circular self dependency of waitTask on waitTask.
801 waitTask->addDependenciesFromOtherTask(fActiveOpsTask);
802 fActiveOpsTask->addDependency(waitTask.get());
803 } else {
804 // In this case we just close the previous RenderTask and start and append the waitTask
805 // to the DAG. Since it is the last task now we call setLastRenderTask on the proxy. If
806 // there is a lastTask on the proxy we make waitTask depend on that task. This
807 // dependency isn't strictly needed but it does keep the DAG from reordering the
808 // waitTask earlier and blocking more tasks.
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400809 if (GrRenderTask* lastTask = this->getLastRenderTask(proxy.get())) {
Greg Danielc30f1a92019-09-06 15:28:58 -0400810 waitTask->addDependency(lastTask);
811 }
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400812 this->setLastRenderTask(proxy.get(), waitTask.get());
Greg Danielc30f1a92019-09-06 15:28:58 -0400813 this->closeRenderTasksForNewRenderTask(proxy.get());
814 fDAG.add(waitTask);
815 }
816 }
817 waitTask->makeClosed(caps);
818
819 SkDEBUGCODE(this->validate());
820}
821
Greg Danielbbfec9d2019-08-20 10:56:51 -0400822void GrDrawingManager::newTransferFromRenderTask(sk_sp<GrSurfaceProxy> srcProxy,
823 const SkIRect& srcRect,
824 GrColorType surfaceColorType,
825 GrColorType dstColorType,
826 sk_sp<GrGpuBuffer> dstBuffer,
827 size_t dstOffset) {
828 SkDEBUGCODE(this->validate());
829 SkASSERT(fContext);
830 // This copies from srcProxy to dstBuffer so it doesn't have a real target.
831 this->closeRenderTasksForNewRenderTask(nullptr);
832
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600833 GrRenderTask* task = fDAG.add(sk_make_sp<GrTransferFromRenderTask>(
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400834 srcProxy, srcRect, surfaceColorType, dstColorType,
835 std::move(dstBuffer), dstOffset));
Greg Danielbbfec9d2019-08-20 10:56:51 -0400836
837 const GrCaps& caps = *fContext->priv().caps();
838
Brian Salomon7e67dca2020-07-21 09:27:25 -0400839 // We always say GrMipmapped::kNo here since we are always just copying from the base layer. We
Greg Danielbbfec9d2019-08-20 10:56:51 -0400840 // don't need to make sure the whole mip map chain is valid.
Brian Salomon7e67dca2020-07-21 09:27:25 -0400841 task->addDependency(this, srcProxy.get(), GrMipmapped::kNo,
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400842 GrTextureResolveManager(this), caps);
Greg Danielbbfec9d2019-08-20 10:56:51 -0400843 task->makeClosed(caps);
844
Greg Danielbbfec9d2019-08-20 10:56:51 -0400845 // We have closed the previous active oplist but since a new oplist isn't being added there
846 // shouldn't be an active one.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400847 SkASSERT(!fActiveOpsTask);
Greg Danielbbfec9d2019-08-20 10:56:51 -0400848 SkDEBUGCODE(this->validate());
849}
850
Greg Daniel16f5c652019-10-29 11:26:01 -0400851bool GrDrawingManager::newCopyRenderTask(GrSurfaceProxyView srcView,
Greg Daniele227fe42019-08-21 13:52:24 -0400852 const SkIRect& srcRect,
Greg Daniel16f5c652019-10-29 11:26:01 -0400853 GrSurfaceProxyView dstView,
Greg Daniele227fe42019-08-21 13:52:24 -0400854 const SkIPoint& dstPoint) {
855 SkDEBUGCODE(this->validate());
856 SkASSERT(fContext);
Greg Daniele227fe42019-08-21 13:52:24 -0400857
Greg Daniel16f5c652019-10-29 11:26:01 -0400858 this->closeRenderTasksForNewRenderTask(dstView.proxy());
Brian Salomone4bce012019-09-20 15:34:23 -0400859 const GrCaps& caps = *fContext->priv().caps();
860
Greg Daniel16f5c652019-10-29 11:26:01 -0400861 GrSurfaceProxy* srcProxy = srcView.proxy();
862
Brian Salomone4bce012019-09-20 15:34:23 -0400863 GrRenderTask* task =
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400864 fDAG.add(GrCopyRenderTask::Make(this, std::move(srcView), srcRect, std::move(dstView),
Greg Daniel16f5c652019-10-29 11:26:01 -0400865 dstPoint, &caps));
Greg Daniele227fe42019-08-21 13:52:24 -0400866 if (!task) {
867 return false;
868 }
869
Brian Salomon7e67dca2020-07-21 09:27:25 -0400870 // We always say GrMipmapped::kNo here since we are always just copying from the base layer to
Greg Daniele227fe42019-08-21 13:52:24 -0400871 // another base layer. We don't need to make sure the whole mip map chain is valid.
Brian Salomon7e67dca2020-07-21 09:27:25 -0400872 task->addDependency(this, srcProxy, GrMipmapped::kNo, GrTextureResolveManager(this), caps);
Greg Daniele227fe42019-08-21 13:52:24 -0400873 task->makeClosed(caps);
874
Greg Daniele227fe42019-08-21 13:52:24 -0400875 // We have closed the previous active oplist but since a new oplist isn't being added there
876 // shouldn't be an active one.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400877 SkASSERT(!fActiveOpsTask);
Greg Daniele227fe42019-08-21 13:52:24 -0400878 SkDEBUGCODE(this->validate());
879 return true;
880}
881
robertphillips68737822015-10-29 12:12:21 -0700882/*
883 * This method finds a path renderer that can draw the specified path on
884 * the provided target.
885 * Due to its expense, the software path renderer has split out so it can
886 * can be individually allowed/disallowed via the "allowSW" boolean.
887 */
888GrPathRenderer* GrDrawingManager::getPathRenderer(const GrPathRenderer::CanDrawPathArgs& args,
889 bool allowSW,
890 GrPathRendererChain::DrawType drawType,
891 GrPathRenderer::StencilSupport* stencilSupport) {
892
893 if (!fPathRendererChain) {
John Stilesfbd050b2020-08-03 13:21:46 -0400894 fPathRendererChain =
895 std::make_unique<GrPathRendererChain>(fContext, fOptionsForPathRendererChain);
robertphillips68737822015-10-29 12:12:21 -0700896 }
897
898 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(args, drawType, stencilSupport);
899 if (!pr && allowSW) {
Brian Salomone7df0bb2018-05-07 14:44:57 -0400900 auto swPR = this->getSoftwarePathRenderer();
901 if (GrPathRenderer::CanDrawPath::kNo != swPR->canDrawPath(args)) {
902 pr = swPR;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500903 }
robertphillips68737822015-10-29 12:12:21 -0700904 }
905
Robert Phillipsd81379d2020-04-21 10:39:02 -0400906#if GR_PATH_RENDERER_SPEW
907 if (pr) {
908 SkDebugf("getPathRenderer: %s\n", pr->name());
909 }
910#endif
911
robertphillips68737822015-10-29 12:12:21 -0700912 return pr;
913}
914
Brian Salomone7df0bb2018-05-07 14:44:57 -0400915GrPathRenderer* GrDrawingManager::getSoftwarePathRenderer() {
916 if (!fSoftwarePathRenderer) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400917 fSoftwarePathRenderer.reset(
Robert Phillips9da87e02019-02-04 13:26:26 -0500918 new GrSoftwarePathRenderer(fContext->priv().proxyProvider(),
Ben Wagner9ec70c62018-07-12 13:30:47 -0400919 fOptionsForPathRendererChain.fAllowPathMaskCaching));
Brian Salomone7df0bb2018-05-07 14:44:57 -0400920 }
Ben Wagner9ec70c62018-07-12 13:30:47 -0400921 return fSoftwarePathRenderer.get();
Brian Salomone7df0bb2018-05-07 14:44:57 -0400922}
923
Chris Daltonfddb6c02017-11-04 15:22:22 -0600924GrCoverageCountingPathRenderer* GrDrawingManager::getCoverageCountingPathRenderer() {
925 if (!fPathRendererChain) {
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500926 fPathRendererChain = std::make_unique<GrPathRendererChain>(fContext,
927 fOptionsForPathRendererChain);
Chris Daltonfddb6c02017-11-04 15:22:22 -0600928 }
929 return fPathRendererChain->getCoverageCountingPathRenderer();
930}
931
Brian Salomon653f42f2018-07-10 10:07:31 -0400932void GrDrawingManager::flushIfNecessary() {
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400933 auto direct = fContext->asDirectContext();
Robert Phillips6a6de562019-02-15 15:19:15 -0500934 if (!direct) {
935 return;
936 }
937
938 auto resourceCache = direct->priv().getResourceCache();
Brian Salomon653f42f2018-07-10 10:07:31 -0400939 if (resourceCache && resourceCache->requestsFlush()) {
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500940 if (this->flush({}, SkSurface::BackendSurfaceAccess::kNoAccess, GrFlushInfo(), nullptr)) {
Greg Danielfe159622020-04-10 17:43:51 +0000941 this->submitToGpu(false);
942 }
Brian Salomon57d2beab2018-09-10 09:35:41 -0400943 resourceCache->purgeAsNeeded();
Brian Salomon653f42f2018-07-10 10:07:31 -0400944 }
945}
946