blob: 046c1c02c6c7bb267653178d7e423fa35fac0827 [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/private/GrRecordingContext.h"
Robert Phillips7eb0a5f2020-06-09 14:15:09 -040013#include "src/core/SkDeferredDisplayListPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/core/SkTTopoSort.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040015#include "src/gpu/GrAuditTrail.h"
Brian Salomon9241a6d2019-10-03 13:26:54 -040016#include "src/gpu/GrClientMappedBufferManager.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/gpu/GrContextPriv.h"
Greg Daniele227fe42019-08-21 13:52:24 -040018#include "src/gpu/GrCopyRenderTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/gpu/GrGpu.h"
20#include "src/gpu/GrMemoryPool.h"
21#include "src/gpu/GrOnFlushResourceProvider.h"
22#include "src/gpu/GrRecordingContextPriv.h"
23#include "src/gpu/GrRenderTargetContext.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040024#include "src/gpu/GrRenderTargetProxy.h"
Chris Dalton6b498102019-08-01 14:14:52 -060025#include "src/gpu/GrRenderTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/gpu/GrResourceAllocator.h"
27#include "src/gpu/GrResourceProvider.h"
28#include "src/gpu/GrSoftwarePathRenderer.h"
Greg Daniel46e366a2019-12-16 14:38:36 -050029#include "src/gpu/GrSurfaceContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/gpu/GrSurfaceProxyPriv.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000031#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050032#include "src/gpu/GrTexturePriv.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040033#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050034#include "src/gpu/GrTextureProxyPriv.h"
Chris Dalton3d770272019-08-14 09:24:37 -060035#include "src/gpu/GrTextureResolveRenderTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050036#include "src/gpu/GrTracing.h"
Greg Danielbbfec9d2019-08-20 10:56:51 -040037#include "src/gpu/GrTransferFromRenderTask.h"
Greg Danielc30f1a92019-09-06 15:28:58 -040038#include "src/gpu/GrWaitRenderTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050039#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
40#include "src/gpu/text/GrTextContext.h"
41#include "src/image/SkSurface_Gpu.h"
robertphillips498d7ac2015-10-30 10:11:30 -070042
Chris Dalton6b498102019-08-01 14:14:52 -060043GrDrawingManager::RenderTaskDAG::RenderTaskDAG(bool sortRenderTasks)
44 : fSortRenderTasks(sortRenderTasks) {}
Robert Phillipsa3f70262018-02-08 10:59:38 -050045
Chris Dalton6b498102019-08-01 14:14:52 -060046GrDrawingManager::RenderTaskDAG::~RenderTaskDAG() {}
Robert Phillips22310d62018-09-05 11:07:21 -040047
Chris Dalton6b498102019-08-01 14:14:52 -060048void GrDrawingManager::RenderTaskDAG::gatherIDs(SkSTArray<8, uint32_t, true>* idArray) const {
49 idArray->reset(fRenderTasks.count());
50 for (int i = 0; i < fRenderTasks.count(); ++i) {
51 if (fRenderTasks[i]) {
52 (*idArray)[i] = fRenderTasks[i]->uniqueID();
Robert Phillips22310d62018-09-05 11:07:21 -040053 }
54 }
55}
56
Chris Dalton6b498102019-08-01 14:14:52 -060057void GrDrawingManager::RenderTaskDAG::reset() {
58 fRenderTasks.reset();
Robert Phillips22310d62018-09-05 11:07:21 -040059}
60
Chris Dalton6b498102019-08-01 14:14:52 -060061void GrDrawingManager::RenderTaskDAG::removeRenderTasks(int startIndex, int stopIndex) {
Robert Phillips22310d62018-09-05 11:07:21 -040062 for (int i = startIndex; i < stopIndex; ++i) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -040063 fRenderTasks[i] = nullptr;
Robert Phillips22310d62018-09-05 11:07:21 -040064 }
65}
66
Chris Dalton6b498102019-08-01 14:14:52 -060067bool GrDrawingManager::RenderTaskDAG::isUsed(GrSurfaceProxy* proxy) const {
68 for (int i = 0; i < fRenderTasks.count(); ++i) {
69 if (fRenderTasks[i] && fRenderTasks[i]->isUsed(proxy)) {
Robert Phillips9313aa72019-04-09 18:41:27 -040070 return true;
71 }
72 }
73
74 return false;
75}
76
Chris Dalton3d770272019-08-14 09:24:37 -060077GrRenderTask* GrDrawingManager::RenderTaskDAG::add(sk_sp<GrRenderTask> renderTask) {
Chris Dalton6aeb8e82019-08-27 11:52:19 -060078 if (renderTask) {
79 return fRenderTasks.emplace_back(std::move(renderTask)).get();
80 }
81 return nullptr;
Chris Dalton3d770272019-08-14 09:24:37 -060082}
83
84GrRenderTask* GrDrawingManager::RenderTaskDAG::addBeforeLast(sk_sp<GrRenderTask> renderTask) {
85 SkASSERT(!fRenderTasks.empty());
Chris Dalton6aeb8e82019-08-27 11:52:19 -060086 if (renderTask) {
87 // Release 'fRenderTasks.back()' and grab the raw pointer, in case the SkTArray grows
88 // and reallocates during emplace_back.
89 fRenderTasks.emplace_back(fRenderTasks.back().release());
90 return (fRenderTasks[fRenderTasks.count() - 2] = std::move(renderTask)).get();
91 }
92 return nullptr;
Robert Phillips22310d62018-09-05 11:07:21 -040093}
94
Greg Danielf41b2bd2019-08-22 16:19:24 -040095void GrDrawingManager::RenderTaskDAG::add(const SkTArray<sk_sp<GrRenderTask>>& renderTasks) {
Robert Phillips19f466d2020-02-26 10:27:07 -050096#ifdef SK_DEBUG
97 for (auto& renderTask : renderTasks) {
98 SkASSERT(renderTask->unique());
99 }
100#endif
101
Greg Danielf41b2bd2019-08-22 16:19:24 -0400102 fRenderTasks.push_back_n(renderTasks.count(), renderTasks.begin());
Robert Phillips22310d62018-09-05 11:07:21 -0400103}
104
Greg Danielf41b2bd2019-08-22 16:19:24 -0400105void GrDrawingManager::RenderTaskDAG::swap(SkTArray<sk_sp<GrRenderTask>>* renderTasks) {
106 SkASSERT(renderTasks->empty());
107 renderTasks->swap(fRenderTasks);
Robert Phillips22310d62018-09-05 11:07:21 -0400108}
109
Chris Dalton6b498102019-08-01 14:14:52 -0600110void GrDrawingManager::RenderTaskDAG::prepForFlush() {
111 if (fSortRenderTasks) {
112 SkDEBUGCODE(bool result =) SkTTopoSort<GrRenderTask, GrRenderTask::TopoSortTraits>(
113 &fRenderTasks);
Robert Phillips22310d62018-09-05 11:07:21 -0400114 SkASSERT(result);
115 }
116
117#ifdef SK_DEBUG
Greg Danielf41b2bd2019-08-22 16:19:24 -0400118 // This block checks for any unnecessary splits in the opsTasks. If two sequential opsTasks
119 // share the same backing GrSurfaceProxy it means the opsTask was artificially split.
Chris Dalton6b498102019-08-01 14:14:52 -0600120 if (fRenderTasks.count()) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400121 GrOpsTask* prevOpsTask = fRenderTasks[0]->asOpsTask();
Chris Dalton6b498102019-08-01 14:14:52 -0600122 for (int i = 1; i < fRenderTasks.count(); ++i) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400123 GrOpsTask* curOpsTask = fRenderTasks[i]->asOpsTask();
Robert Phillips22310d62018-09-05 11:07:21 -0400124
Greg Danielf41b2bd2019-08-22 16:19:24 -0400125 if (prevOpsTask && curOpsTask) {
Greg Daniel16f5c652019-10-29 11:26:01 -0400126 SkASSERT(prevOpsTask->fTargetView != curOpsTask->fTargetView);
Robert Phillips22310d62018-09-05 11:07:21 -0400127 }
128
Greg Danielf41b2bd2019-08-22 16:19:24 -0400129 prevOpsTask = curOpsTask;
Robert Phillips22310d62018-09-05 11:07:21 -0400130 }
131 }
132#endif
133}
134
Chris Dalton6b498102019-08-01 14:14:52 -0600135void GrDrawingManager::RenderTaskDAG::closeAll(const GrCaps* caps) {
136 for (int i = 0; i < fRenderTasks.count(); ++i) {
137 if (fRenderTasks[i]) {
138 fRenderTasks[i]->makeClosed(*caps);
Robert Phillips22310d62018-09-05 11:07:21 -0400139 }
140 }
141}
142
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400143void GrDrawingManager::RenderTaskDAG::cleanup(GrDrawingManager* drawingMgr, const GrCaps* caps) {
Chris Dalton6b498102019-08-01 14:14:52 -0600144 for (int i = 0; i < fRenderTasks.count(); ++i) {
145 if (!fRenderTasks[i]) {
Robert Phillips22310d62018-09-05 11:07:21 -0400146 continue;
147 }
148
Chris Dalton6b498102019-08-01 14:14:52 -0600149 // no renderTask should receive a dependency
150 fRenderTasks[i]->makeClosed(*caps);
robertphillips0dfa62c2015-11-16 06:23:31 -0800151
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400152 fRenderTasks[i]->disown(drawingMgr);
153
Greg Danielf41b2bd2019-08-22 16:19:24 -0400154 // We shouldn't need to do this, but it turns out some clients still hold onto opsTasks
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400155 // after a cleanup.
156 // MDB TODO: is this still true?
Chris Dalton6b498102019-08-01 14:14:52 -0600157 if (!fRenderTasks[i]->unique()) {
Chris Daltona84cacf2017-10-04 10:30:29 -0600158 // TODO: Eventually this should be guaranteed unique.
159 // https://bugs.chromium.org/p/skia/issues/detail?id=7111
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400160 fRenderTasks[i]->endFlush(drawingMgr);
Chris Daltona84cacf2017-10-04 10:30:29 -0600161 }
robertphillips3dc6ae52015-10-20 09:54:32 -0700162 }
163
Chris Dalton6b498102019-08-01 14:14:52 -0600164 fRenderTasks.reset();
Robert Phillips22310d62018-09-05 11:07:21 -0400165}
166
167///////////////////////////////////////////////////////////////////////////////////////////////////
Robert Phillips69893702019-02-22 11:16:30 -0500168GrDrawingManager::GrDrawingManager(GrRecordingContext* context,
Robert Phillips22310d62018-09-05 11:07:21 -0400169 const GrPathRendererChain::Options& optionsForPathRendererChain,
Chris Dalton6b498102019-08-01 14:14:52 -0600170 bool sortRenderTasks,
Greg Danielf41b2bd2019-08-22 16:19:24 -0400171 bool reduceOpsTaskSplitting)
Robert Phillips22310d62018-09-05 11:07:21 -0400172 : fContext(context)
173 , fOptionsForPathRendererChain(optionsForPathRendererChain)
Chris Dalton6b498102019-08-01 14:14:52 -0600174 , fDAG(sortRenderTasks)
Robert Phillips22310d62018-09-05 11:07:21 -0400175 , fPathRendererChain(nullptr)
176 , fSoftwarePathRenderer(nullptr)
Robert Phillips6db27c22019-05-01 10:43:56 -0400177 , fFlushing(false)
Herb Derby082232b2020-06-10 15:08:18 -0400178 , fReduceOpsTaskSplitting(reduceOpsTaskSplitting) { }
Robert Phillips22310d62018-09-05 11:07:21 -0400179
180void GrDrawingManager::cleanup() {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400181 fDAG.cleanup(this, fContext->priv().caps());
robertphillips3dc6ae52015-10-20 09:54:32 -0700182
robertphillips13391dd2015-10-30 05:15:11 -0700183 fPathRendererChain = nullptr;
Ben Wagner9ec70c62018-07-12 13:30:47 -0400184 fSoftwarePathRenderer = nullptr;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400185
186 fOnFlushCBObjects.reset();
robertphillips3dc6ae52015-10-20 09:54:32 -0700187}
188
189GrDrawingManager::~GrDrawingManager() {
190 this->cleanup();
191}
192
Robert Phillipsa9162df2019-02-11 14:12:03 -0500193bool GrDrawingManager::wasAbandoned() const {
Robert Phillips6a6de562019-02-15 15:19:15 -0500194 return fContext->priv().abandoned();
robertphillips3dc6ae52015-10-20 09:54:32 -0700195}
196
robertphillips68737822015-10-29 12:12:21 -0700197void GrDrawingManager::freeGpuResources() {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400198 for (int i = fOnFlushCBObjects.count() - 1; i >= 0; --i) {
199 if (!fOnFlushCBObjects[i]->retainOnFreeGpuResources()) {
200 // it's safe to just do this because we're iterating in reverse
201 fOnFlushCBObjects.removeShuffle(i);
202 }
203 }
204
robertphillips68737822015-10-29 12:12:21 -0700205 // a path renderer may be holding onto resources
robertphillips13391dd2015-10-30 05:15:11 -0700206 fPathRendererChain = nullptr;
Ben Wagner9ec70c62018-07-12 13:30:47 -0400207 fSoftwarePathRenderer = nullptr;
Robert Phillipse3302df2017-04-24 07:31:02 -0400208}
209
Robert Phillips7ee385e2017-03-30 08:02:11 -0400210// MDB TODO: make use of the 'proxy' parameter.
Greg Danielfe159622020-04-10 17:43:51 +0000211bool GrDrawingManager::flush(GrSurfaceProxy* proxies[], int numProxies,
Greg Daniele8d3cca2020-06-10 10:04:48 -0400212 SkSurface::BackendSurfaceAccess access, const GrFlushInfo& info) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400213 SkASSERT(numProxies >= 0);
214 SkASSERT(!numProxies || proxies);
Brian Salomon57d2beab2018-09-10 09:35:41 -0400215 GR_CREATE_TRACE_MARKER_CONTEXT("GrDrawingManager", "flush", fContext);
Brian Salomondcbb9d92017-07-19 10:53:20 -0400216
robertphillips7761d612016-05-16 09:14:53 -0700217 if (fFlushing || this->wasAbandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -0400218 if (info.fSubmittedProc) {
219 info.fSubmittedProc(info.fSubmittedContext, false);
220 }
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400221 if (info.fFinishedProc) {
222 info.fFinishedProc(info.fFinishedContext);
Greg Daniela3aa75a2019-04-12 14:24:55 -0400223 }
Greg Danielfe159622020-04-10 17:43:51 +0000224 return false;
joshualittb8918c42015-12-18 09:59:46 -0800225 }
Robert Phillips602df412019-04-08 11:10:39 -0400226
Robert Phillips38d64b02018-09-04 13:23:26 -0400227 SkDEBUGCODE(this->validate());
228
Greg Daniel797efca2019-05-09 14:04:20 -0400229 if (kNone_GrFlushFlags == info.fFlags && !info.fNumSemaphores && !info.fFinishedProc &&
Greg Daniele8d3cca2020-06-10 10:04:48 -0400230 access == SkSurface::BackendSurfaceAccess::kNoAccess) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400231 bool canSkip = numProxies > 0;
232 for (int i = 0; i < numProxies && canSkip; ++i) {
233 canSkip = !fDAG.isUsed(proxies[i]) && !this->isDDLTarget(proxies[i]);
234 }
235 if (canSkip) {
Greg Daniel55822f12020-05-26 11:26:45 -0400236 if (info.fSubmittedProc) {
237 info.fSubmittedProc(info.fSubmittedContext, true);
238 }
Greg Danielfe159622020-04-10 17:43:51 +0000239 return false;
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400240 }
Robert Phillips9313aa72019-04-09 18:41:27 -0400241 }
242
Robert Phillips6a6de562019-02-15 15:19:15 -0500243 auto direct = fContext->priv().asDirectContext();
244 if (!direct) {
Greg Daniel55822f12020-05-26 11:26:45 -0400245 if (info.fSubmittedProc) {
246 info.fSubmittedProc(info.fSubmittedContext, false);
247 }
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400248 if (info.fFinishedProc) {
249 info.fFinishedProc(info.fFinishedContext);
Greg Daniela3aa75a2019-04-12 14:24:55 -0400250 }
Greg Danielfe159622020-04-10 17:43:51 +0000251 return false; // Can't flush while DDL recording
Robert Phillips6a6de562019-02-15 15:19:15 -0500252 }
Brian Salomon9241a6d2019-10-03 13:26:54 -0400253 direct->priv().clientMappedBufferManager()->process();
Robert Phillips6a6de562019-02-15 15:19:15 -0500254
255 GrGpu* gpu = direct->priv().getGpu();
Greg Daniel55822f12020-05-26 11:26:45 -0400256 // We have a non abandoned and direct GrContext. It must have a GrGpu.
257 SkASSERT(gpu);
Greg Daniela3aa75a2019-04-12 14:24:55 -0400258
joshualittb8918c42015-12-18 09:59:46 -0800259 fFlushing = true;
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400260
Robert Phillips6a6de562019-02-15 15:19:15 -0500261 auto resourceProvider = direct->priv().resourceProvider();
262 auto resourceCache = direct->priv().getResourceCache();
263
Chris Dalton6b498102019-08-01 14:14:52 -0600264 // Semi-usually the GrRenderTasks are already closed at this point, but sometimes Ganesh needs
Greg Danielf41b2bd2019-08-22 16:19:24 -0400265 // to flush mid-draw. In that case, the SkGpuDevice's opsTasks won't be closed but need to be
266 // flushed anyway. Closing such opsTasks here will mean new ones will be created to replace them
Chris Dalton6b498102019-08-01 14:14:52 -0600267 // if the SkGpuDevice(s) write to them again.
Robert Phillips9da87e02019-02-04 13:26:26 -0500268 fDAG.closeAll(fContext->priv().caps());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400269 fActiveOpsTask = nullptr;
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400270
Robert Phillips22310d62018-09-05 11:07:21 -0400271 fDAG.prepForFlush();
Brian Salomon601ac802019-02-07 13:37:16 -0500272 if (!fCpuBufferCache) {
273 // We cache more buffers when the backend is using client side arrays. Otherwise, we
274 // expect each pool will use a CPU buffer as a staging buffer before uploading to a GPU
275 // buffer object. Each pool only requires one staging buffer at a time.
276 int maxCachedBuffers = fContext->priv().caps()->preferClientSideDynamicBuffers() ? 2 : 6;
277 fCpuBufferCache = GrBufferAllocPool::CpuBufferCache::Make(maxCachedBuffers);
Brian Salomon58f153c2018-10-18 21:51:15 -0400278 }
Robert Phillipsa4c93ac2017-05-18 11:40:04 -0400279
Robert Phillipse5f73282019-06-18 17:15:04 -0400280 GrOpFlushState flushState(gpu, resourceProvider, &fTokenTracker, fCpuBufferCache);
Robert Phillips40a29d72018-01-18 12:59:22 -0500281
Chris Daltonfe199b72017-05-05 11:26:15 -0400282 GrOnFlushResourceProvider onFlushProvider(this);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500283 // TODO: AFAICT the only reason fFlushState is on GrDrawingManager rather than on the
284 // stack here is to preserve the flush tokens.
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400285
Chris Dalton12658942017-10-05 19:45:25 -0600286 // Prepare any onFlush op lists (e.g. atlases).
Chris Daltonfe199b72017-05-05 11:26:15 -0400287 if (!fOnFlushCBObjects.empty()) {
Chris Dalton6b498102019-08-01 14:14:52 -0600288 fDAG.gatherIDs(&fFlushingRenderTaskIDs);
Robert Phillips22310d62018-09-05 11:07:21 -0400289
Chris Daltonfe199b72017-05-05 11:26:15 -0400290 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
Chris Dalton6b498102019-08-01 14:14:52 -0600291 onFlushCBObject->preFlush(&onFlushProvider, fFlushingRenderTaskIDs.begin(),
Chris Daltonc4b47352019-08-23 10:10:36 -0600292 fFlushingRenderTaskIDs.count());
293 }
294 for (const auto& onFlushRenderTask : fOnFlushRenderTasks) {
295 onFlushRenderTask->makeClosed(*fContext->priv().caps());
Chris Dalton706a6ff2017-11-29 22:01:06 -0700296#ifdef SK_DEBUG
Chris Daltonc4b47352019-08-23 10:10:36 -0600297 // OnFlush callbacks are invoked during flush, and are therefore expected to handle
298 // resource allocation & usage on their own. (No deferred or lazy proxies!)
299 onFlushRenderTask->visitTargetAndSrcProxies_debugOnly(
300 [](GrSurfaceProxy* p, GrMipMapped mipMapped) {
301 SkASSERT(!p->asTextureProxy() || !p->asTextureProxy()->texPriv().isDeferred());
Brian Salomonbeb7f522019-08-30 16:19:42 -0400302 SkASSERT(!p->isLazy());
Chris Dalton4ece96d2019-08-30 11:26:39 -0600303 if (p->requiresManualMSAAResolve()) {
304 // The onFlush callback is responsible for ensuring MSAA gets resolved.
305 SkASSERT(p->asRenderTargetProxy() && !p->asRenderTargetProxy()->isMSAADirty());
306 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600307 if (GrMipMapped::kYes == mipMapped) {
308 // The onFlush callback is responsible for regenerating mips if needed.
309 SkASSERT(p->asTextureProxy() && !p->asTextureProxy()->mipMapsAreDirty());
310 }
311 });
Chris Dalton706a6ff2017-11-29 22:01:06 -0700312#endif
Chris Daltonc4b47352019-08-23 10:10:36 -0600313 onFlushRenderTask->prepare(&flushState);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400314 }
315 }
316
robertphillipsa13e2022015-11-11 12:01:09 -0800317#if 0
Brian Salomon09d994e2016-12-21 11:14:46 -0500318 // Enable this to print out verbose GrOp information
Chris Daltonc4b47352019-08-23 10:10:36 -0600319 SkDEBUGCODE(SkDebugf("onFlush renderTasks:"));
320 for (const auto& onFlushRenderTask : fOnFlushRenderTasks) {
321 SkDEBUGCODE(onFlushRenderTask->dump();)
322 }
323 SkDEBUGCODE(SkDebugf("Normal renderTasks:"));
Chris Dalton6b498102019-08-01 14:14:52 -0600324 for (int i = 0; i < fRenderTasks.count(); ++i) {
325 SkDEBUGCODE(fRenderTasks[i]->dump();)
robertphillips3dc6ae52015-10-20 09:54:32 -0700326 }
robertphillipsa13e2022015-11-11 12:01:09 -0800327#endif
328
Robert Phillipseafd48a2017-11-16 07:52:08 -0500329 int startIndex, stopIndex;
330 bool flushed = false;
331
Robert Phillipsf8e25022017-11-08 15:24:31 -0500332 {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400333 GrResourceAllocator alloc(resourceProvider SkDEBUGCODE(, fDAG.numRenderTasks()));
Chris Dalton6b498102019-08-01 14:14:52 -0600334 for (int i = 0; i < fDAG.numRenderTasks(); ++i) {
335 if (fDAG.renderTask(i)) {
336 fDAG.renderTask(i)->gatherProxyIntervals(&alloc);
Robert Phillips22310d62018-09-05 11:07:21 -0400337 }
Greg Danielf41b2bd2019-08-22 16:19:24 -0400338 alloc.markEndOfOpsTask(i);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500339 }
Robert Phillipsc73666f2019-04-24 08:49:48 -0400340 alloc.determineRecyclability();
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400341
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500342 GrResourceAllocator::AssignError error = GrResourceAllocator::AssignError::kNoError;
Chris Dalton6b498102019-08-01 14:14:52 -0600343 int numRenderTasksExecuted = 0;
Brian Salomon577aa0f2018-11-30 13:32:23 -0500344 while (alloc.assign(&startIndex, &stopIndex, &error)) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500345 if (GrResourceAllocator::AssignError::kFailedProxyInstantiation == error) {
346 for (int i = startIndex; i < stopIndex; ++i) {
Chris Dalton6b498102019-08-01 14:14:52 -0600347 GrRenderTask* renderTask = fDAG.renderTask(i);
348 if (!renderTask) {
349 continue;
Robert Phillips01a91282018-07-26 08:03:04 -0400350 }
Chris Dalton6b498102019-08-01 14:14:52 -0600351 if (!renderTask->isInstantiated()) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400352 // No need to call the renderTask's handleInternalAllocationFailure
353 // since we will already skip executing the renderTask since it is not
354 // instantiated.
Chris Dalton6b498102019-08-01 14:14:52 -0600355 continue;
Robert Phillipsbbcb7f72018-05-31 11:16:19 -0400356 }
Chris Dalton6b498102019-08-01 14:14:52 -0600357 renderTask->handleInternalAllocationFailure();
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500358 }
359 }
Robert Phillips4150eea2018-02-07 17:08:21 -0500360
Chris Dalton6b498102019-08-01 14:14:52 -0600361 if (this->executeRenderTasks(
362 startIndex, stopIndex, &flushState, &numRenderTasksExecuted)) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500363 flushed = true;
364 }
bsalomondc438982016-08-31 11:53:49 -0700365 }
Greg Danielc42b20b2017-10-04 10:34:49 -0400366 }
367
Chris Dalton91ab1552018-04-18 13:24:25 -0600368#ifdef SK_DEBUG
Chris Dalton6b498102019-08-01 14:14:52 -0600369 for (int i = 0; i < fDAG.numRenderTasks(); ++i) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400370 // If there are any remaining opsTaskss at this point, make sure they will not survive the
Chris Dalton91ab1552018-04-18 13:24:25 -0600371 // flush. Otherwise we need to call endFlush() on them.
372 // http://skbug.com/7111
Chris Dalton6b498102019-08-01 14:14:52 -0600373 SkASSERT(!fDAG.renderTask(i) || fDAG.renderTask(i)->unique());
Chris Dalton91ab1552018-04-18 13:24:25 -0600374 }
375#endif
Adlai Holler25df1f72020-06-09 13:08:27 -0400376 fLastRenderTasks.reset();
Robert Phillips22310d62018-09-05 11:07:21 -0400377 fDAG.reset();
Robert Phillips15c91422019-05-07 16:54:48 -0400378 this->clearDDLTargets();
robertphillipsa13e2022015-11-11 12:01:09 -0800379
Robert Phillipsc994a932018-06-19 13:09:54 -0400380#ifdef SK_DEBUG
381 // In non-DDL mode this checks that all the flushed ops have been freed from the memory pool.
382 // When we move to partial flushes this assert will no longer be valid.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400383 // In DDL mode this check is somewhat superfluous since the memory for most of the ops/opsTasks
Robert Phillipsc994a932018-06-19 13:09:54 -0400384 // will be stored in the DDL's GrOpMemoryPools.
Robert Phillips9da87e02019-02-04 13:26:26 -0500385 GrOpMemoryPool* opMemoryPool = fContext->priv().opMemoryPool();
Robert Phillipsc994a932018-06-19 13:09:54 -0400386 opMemoryPool->isEmpty();
387#endif
388
Greg Daniele8d3cca2020-06-10 10:04:48 -0400389 gpu->executeFlushInfo(proxies, numProxies, access, info);
robertphillipsa13e2022015-11-11 12:01:09 -0800390
Brian Salomon57d2beab2018-09-10 09:35:41 -0400391 // Give the cache a chance to purge resources that become purgeable due to flushing.
392 if (flushed) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500393 resourceCache->purgeAsNeeded();
Brian Salomon876a0172019-03-08 11:12:14 -0500394 flushed = false;
bsalomonb77a9072016-09-07 10:02:04 -0700395 }
Chris Daltonfe199b72017-05-05 11:26:15 -0400396 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
Chris Dalton6b498102019-08-01 14:14:52 -0600397 onFlushCBObject->postFlush(fTokenTracker.nextTokenToFlush(), fFlushingRenderTaskIDs.begin(),
398 fFlushingRenderTaskIDs.count());
Brian Salomon876a0172019-03-08 11:12:14 -0500399 flushed = true;
400 }
401 if (flushed) {
402 resourceCache->purgeAsNeeded();
Chris Daltonfe199b72017-05-05 11:26:15 -0400403 }
Chris Dalton6b498102019-08-01 14:14:52 -0600404 fFlushingRenderTaskIDs.reset();
joshualittb8918c42015-12-18 09:59:46 -0800405 fFlushing = false;
Greg Daniel51316782017-08-02 15:10:09 +0000406
Greg Danielfe159622020-04-10 17:43:51 +0000407 return true;
408}
409
410bool GrDrawingManager::submitToGpu(bool syncToCpu) {
411 if (fFlushing || this->wasAbandoned()) {
412 return false;
413 }
414
415 auto direct = fContext->priv().asDirectContext();
416 if (!direct) {
417 return false; // Can't submit while DDL recording
418 }
419 GrGpu* gpu = direct->priv().getGpu();
420 return gpu->submitToGpu(syncToCpu);
robertphillips3dc6ae52015-10-20 09:54:32 -0700421}
422
Chris Dalton6b498102019-08-01 14:14:52 -0600423bool GrDrawingManager::executeRenderTasks(int startIndex, int stopIndex, GrOpFlushState* flushState,
424 int* numRenderTasksExecuted) {
425 SkASSERT(startIndex <= stopIndex && stopIndex <= fDAG.numRenderTasks());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500426
Robert Phillips27483912018-04-20 12:43:18 -0400427#if GR_FLUSH_TIME_OP_SPEW
Greg Danielf41b2bd2019-08-22 16:19:24 -0400428 SkDebugf("Flushing opsTask: %d to %d out of [%d, %d]\n",
Chris Dalton6b498102019-08-01 14:14:52 -0600429 startIndex, stopIndex, 0, fDAG.numRenderTasks());
Robert Phillips27483912018-04-20 12:43:18 -0400430 for (int i = startIndex; i < stopIndex; ++i) {
Chris Dalton6b498102019-08-01 14:14:52 -0600431 if (fDAG.renderTask(i)) {
432 fDAG.renderTask(i)->dump(true);
Robert Phillips1734dd32018-08-21 13:52:09 -0400433 }
Robert Phillips27483912018-04-20 12:43:18 -0400434 }
435#endif
436
Chris Dalton6b498102019-08-01 14:14:52 -0600437 bool anyRenderTasksExecuted = false;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500438
439 for (int i = startIndex; i < stopIndex; ++i) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400440 GrRenderTask* renderTask = fDAG.renderTask(i);
441 if (!renderTask || !renderTask->isInstantiated()) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500442 continue;
443 }
444
Chris Dalton6b498102019-08-01 14:14:52 -0600445 SkASSERT(renderTask->deferredProxiesAreInstantiated());
Robert Phillips22310d62018-09-05 11:07:21 -0400446
Chris Dalton6b498102019-08-01 14:14:52 -0600447 renderTask->prepare(flushState);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500448 }
449
450 // Upload all data to the GPU
451 flushState->preExecuteDraws();
452
Greg Danield2073452018-12-07 11:20:33 -0500453 // For Vulkan, if we have too many oplists to be flushed we end up allocating a lot of resources
454 // for each command buffer associated with the oplists. If this gets too large we can cause the
455 // devices to go OOM. In practice we usually only hit this case in our tests, but to be safe we
456 // put a cap on the number of oplists we will execute before flushing to the GPU to relieve some
457 // memory pressure.
Chris Dalton6b498102019-08-01 14:14:52 -0600458 static constexpr int kMaxRenderTasksBeforeFlush = 100;
Greg Danield2073452018-12-07 11:20:33 -0500459
Chris Daltonc4b47352019-08-23 10:10:36 -0600460 // Execute the onFlush renderTasks first, if any.
461 for (sk_sp<GrRenderTask>& onFlushRenderTask : fOnFlushRenderTasks) {
462 if (!onFlushRenderTask->execute(flushState)) {
463 SkDebugf("WARNING: onFlushRenderTask failed to execute.\n");
Robert Phillipseafd48a2017-11-16 07:52:08 -0500464 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600465 SkASSERT(onFlushRenderTask->unique());
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400466 onFlushRenderTask->disown(this);
Chris Daltonc4b47352019-08-23 10:10:36 -0600467 onFlushRenderTask = nullptr;
Chris Dalton6b498102019-08-01 14:14:52 -0600468 (*numRenderTasksExecuted)++;
469 if (*numRenderTasksExecuted >= kMaxRenderTasksBeforeFlush) {
Greg Danielfe159622020-04-10 17:43:51 +0000470 flushState->gpu()->submitToGpu(false);
Chris Dalton6b498102019-08-01 14:14:52 -0600471 *numRenderTasksExecuted = 0;
Greg Danield2073452018-12-07 11:20:33 -0500472 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500473 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600474 fOnFlushRenderTasks.reset();
Robert Phillipseafd48a2017-11-16 07:52:08 -0500475
476 // Execute the normal op lists.
477 for (int i = startIndex; i < stopIndex; ++i) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400478 GrRenderTask* renderTask = fDAG.renderTask(i);
479 if (!renderTask || !renderTask->isInstantiated()) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500480 continue;
481 }
482
Greg Daniel15ecdf92019-08-30 15:35:23 -0400483 if (renderTask->execute(flushState)) {
Chris Dalton6b498102019-08-01 14:14:52 -0600484 anyRenderTasksExecuted = true;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500485 }
Chris Dalton6b498102019-08-01 14:14:52 -0600486 (*numRenderTasksExecuted)++;
487 if (*numRenderTasksExecuted >= kMaxRenderTasksBeforeFlush) {
Greg Danielfe159622020-04-10 17:43:51 +0000488 flushState->gpu()->submitToGpu(false);
Chris Dalton6b498102019-08-01 14:14:52 -0600489 *numRenderTasksExecuted = 0;
Greg Danield2073452018-12-07 11:20:33 -0500490 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500491 }
492
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400493 SkASSERT(!flushState->opsRenderPass());
Robert Phillips40a29d72018-01-18 12:59:22 -0500494 SkASSERT(fTokenTracker.nextDrawToken() == fTokenTracker.nextTokenToFlush());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500495
Chris Dalton6b498102019-08-01 14:14:52 -0600496 // We reset the flush state before the RenderTasks so that the last resources to be freed are
497 // those that are written to in the RenderTasks. This helps to make sure the most recently used
498 // resources are the last to be purged by the resource cache.
Robert Phillipseafd48a2017-11-16 07:52:08 -0500499 flushState->reset();
500
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400501 this->removeRenderTasks(startIndex, stopIndex);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500502
Chris Dalton6b498102019-08-01 14:14:52 -0600503 return anyRenderTasksExecuted;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500504}
505
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400506void GrDrawingManager::removeRenderTasks(int startIndex, int stopIndex) {
507 for (int i = startIndex; i < stopIndex; ++i) {
508 GrRenderTask* task = fDAG.renderTask(i);
509 if (!task) {
510 continue;
511 }
512 if (!task->unique()) {
513 // TODO: Eventually this should be guaranteed unique: http://skbug.com/7111
514 task->endFlush(this);
515 }
516 task->disown(this);
517 }
518 fDAG.removeRenderTasks(startIndex, stopIndex);
519}
520
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400521static void resolve_and_mipmap(GrGpu* gpu, GrSurfaceProxy* proxy) {
522 if (!proxy->isInstantiated()) {
523 return;
524 }
525
526 // In the flushSurfaces case, we need to resolve MSAA immediately after flush. This is
527 // because clients expect the flushed surface's backing texture to be fully resolved
528 // upon return.
529 if (proxy->requiresManualMSAAResolve()) {
530 auto* rtProxy = proxy->asRenderTargetProxy();
531 SkASSERT(rtProxy);
532 if (rtProxy->isMSAADirty()) {
533 SkASSERT(rtProxy->peekRenderTarget());
534 gpu->resolveRenderTarget(rtProxy->peekRenderTarget(), rtProxy->msaaDirtyRect(),
535 GrGpu::ForExternalIO::kYes);
536 rtProxy->markMSAAResolved();
537 }
538 }
539 // If, after a flush, any of the proxies of interest have dirty mipmaps, regenerate them in
540 // case their backend textures are being stolen.
541 // (This special case is exercised by the ReimportImageTextureWithMipLevels test.)
542 // FIXME: It may be more ideal to plumb down a "we're going to steal the backends" flag.
543 if (auto* textureProxy = proxy->asTextureProxy()) {
544 if (textureProxy->mipMapsAreDirty()) {
545 SkASSERT(textureProxy->peekTexture());
546 gpu->regenerateMipMapLevels(textureProxy->peekTexture());
547 textureProxy->markMipMapsClean();
548 }
549 }
550}
551
Greg Daniel55f040b2020-02-13 15:38:32 +0000552GrSemaphoresSubmitted GrDrawingManager::flushSurfaces(GrSurfaceProxy* proxies[], int numProxies,
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400553 SkSurface::BackendSurfaceAccess access,
554 const GrFlushInfo& info) {
bsalomon6a2b1942016-09-08 11:28:59 -0700555 if (this->wasAbandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -0400556 if (info.fSubmittedProc) {
557 info.fSubmittedProc(info.fSubmittedContext, false);
558 }
559 if (info.fFinishedProc) {
560 info.fFinishedProc(info.fFinishedContext);
561 }
Greg Daniel51316782017-08-02 15:10:09 +0000562 return GrSemaphoresSubmitted::kNo;
bsalomon6a2b1942016-09-08 11:28:59 -0700563 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400564 SkDEBUGCODE(this->validate());
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400565 SkASSERT(numProxies >= 0);
566 SkASSERT(!numProxies || proxies);
bsalomon6a2b1942016-09-08 11:28:59 -0700567
Robert Phillips6a6de562019-02-15 15:19:15 -0500568 auto direct = fContext->priv().asDirectContext();
569 if (!direct) {
Greg Daniel55822f12020-05-26 11:26:45 -0400570 if (info.fSubmittedProc) {
571 info.fSubmittedProc(info.fSubmittedContext, false);
572 }
573 if (info.fFinishedProc) {
574 info.fFinishedProc(info.fFinishedContext);
575 }
Robert Phillips6a6de562019-02-15 15:19:15 -0500576 return GrSemaphoresSubmitted::kNo; // Can't flush while DDL recording
577 }
578
579 GrGpu* gpu = direct->priv().getGpu();
Greg Daniel55822f12020-05-26 11:26:45 -0400580 // We have a non abandoned and direct GrContext. It must have a GrGpu.
581 SkASSERT(gpu);
Robert Phillips874b5352018-03-16 08:48:24 -0400582
Robert Phillipsacc10fa2019-04-01 09:50:20 -0400583 // TODO: It is important to upgrade the drawingmanager to just flushing the
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400584 // portion of the DAG required by 'proxies' in order to restore some of the
Robert Phillipsacc10fa2019-04-01 09:50:20 -0400585 // semantics of this method.
Greg Daniele8d3cca2020-06-10 10:04:48 -0400586 bool didFlush = this->flush(proxies, numProxies, access, info);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400587 for (int i = 0; i < numProxies; ++i) {
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400588 resolve_and_mipmap(gpu, proxies[i]);
bsalomon6a2b1942016-09-08 11:28:59 -0700589 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400590
591 SkDEBUGCODE(this->validate());
Greg Danielfe159622020-04-10 17:43:51 +0000592
Greg Daniel04283f32020-05-20 13:16:00 -0400593 if (!didFlush || (!direct->priv().caps()->semaphoreSupport() && info.fNumSemaphores)) {
Greg Danielfe159622020-04-10 17:43:51 +0000594 return GrSemaphoresSubmitted::kNo;
595 }
596 return GrSemaphoresSubmitted::kYes;
bsalomon6a2b1942016-09-08 11:28:59 -0700597}
598
Chris Daltonfe199b72017-05-05 11:26:15 -0400599void GrDrawingManager::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
600 fOnFlushCBObjects.push_back(onFlushCBObject);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400601}
602
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500603#if GR_TEST_UTILS
604void GrDrawingManager::testingOnly_removeOnFlushCallbackObject(GrOnFlushCallbackObject* cb) {
605 int n = std::find(fOnFlushCBObjects.begin(), fOnFlushCBObjects.end(), cb) -
606 fOnFlushCBObjects.begin();
607 SkASSERT(n < fOnFlushCBObjects.count());
608 fOnFlushCBObjects.removeShuffle(n);
609}
610#endif
611
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400612void GrDrawingManager::setLastRenderTask(const GrSurfaceProxy* proxy, GrRenderTask* task) {
613#ifdef SK_DEBUG
614 if (GrRenderTask* prior = this->getLastRenderTask(proxy)) {
615 SkASSERT(prior->isClosed());
616 }
617#endif
Adlai Holler25df1f72020-06-09 13:08:27 -0400618 uint32_t key = proxy->uniqueID().asUInt();
619 if (task) {
620 fLastRenderTasks.set(key, task);
621 } else if (fLastRenderTasks.find(key)) {
622 fLastRenderTasks.remove(key);
623 }
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400624}
625
626GrRenderTask* GrDrawingManager::getLastRenderTask(const GrSurfaceProxy* proxy) const {
627 auto entry = fLastRenderTasks.find(proxy->uniqueID().asUInt());
628 return entry ? *entry : nullptr;
629}
630
631GrOpsTask* GrDrawingManager::getLastOpsTask(const GrSurfaceProxy* proxy) const {
632 GrRenderTask* task = this->getLastRenderTask(proxy);
633 return task ? task->asOpsTask() : nullptr;
634}
635
636
Chris Dalton6b498102019-08-01 14:14:52 -0600637void GrDrawingManager::moveRenderTasksToDDL(SkDeferredDisplayList* ddl) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400638 SkDEBUGCODE(this->validate());
639
Chris Dalton6b498102019-08-01 14:14:52 -0600640 // no renderTask should receive a new command after this
Robert Phillips9da87e02019-02-04 13:26:26 -0500641 fDAG.closeAll(fContext->priv().caps());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400642 fActiveOpsTask = nullptr;
Robert Phillips7a137052018-02-01 11:23:12 -0500643
Chris Dalton6b498102019-08-01 14:14:52 -0600644 fDAG.swap(&ddl->fRenderTasks);
Robert Phillips19f466d2020-02-26 10:27:07 -0500645 SkASSERT(!fDAG.numRenderTasks());
Robert Phillips867ce8f2018-06-21 10:28:36 -0400646
Robert Phillips19f466d2020-02-26 10:27:07 -0500647 for (auto& renderTask : ddl->fRenderTasks) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400648 renderTask->disown(this);
Robert Phillips29f38542019-10-16 09:20:25 -0400649 renderTask->prePrepare(fContext);
Robert Phillips7327c9d2019-10-08 16:32:56 -0400650 }
651
Michael Ludwig2c316bd2019-12-19 14:50:44 -0500652 ddl->fArenas = std::move(fContext->priv().detachArenas());
Robert Phillips61fc7992019-10-22 11:58:17 -0400653
Robert Phillipsf6a0b452020-02-18 14:26:46 -0500654 fContext->priv().detachProgramData(&ddl->fProgramData);
Robert Phillips576b6a12019-12-06 13:05:49 -0500655
Robert Phillips774168e2018-05-31 12:43:27 -0400656 if (fPathRendererChain) {
657 if (auto ccpr = fPathRendererChain->getCoverageCountingPathRenderer()) {
658 ddl->fPendingPaths = ccpr->detachPendingPaths();
659 }
660 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400661
662 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500663}
664
Chris Dalton6b498102019-08-01 14:14:52 -0600665void GrDrawingManager::copyRenderTasksFromDDL(const SkDeferredDisplayList* ddl,
Robert Phillips933484f2019-11-26 09:38:55 -0500666 GrRenderTargetProxy* newDest) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400667 SkDEBUGCODE(this->validate());
668
Greg Danielf41b2bd2019-08-22 16:19:24 -0400669 if (fActiveOpsTask) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400670 // This is a temporary fix for the partial-MDB world. In that world we're not
Greg Danielf41b2bd2019-08-22 16:19:24 -0400671 // reordering so ops that (in the single opsTask world) would've just glommed onto the
672 // end of the single opsTask but referred to a far earlier RT need to appear in their
673 // own opsTask.
674 fActiveOpsTask->makeClosed(*fContext->priv().caps());
675 fActiveOpsTask = nullptr;
Robert Phillips38d64b02018-09-04 13:23:26 -0400676 }
677
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400678 // Propagate the DDL proxy's state information to the replaying DDL.
679 if (ddl->priv().targetProxy()->isMSAADirty()) {
680 newDest->markMSAADirty(ddl->priv().targetProxy()->msaaDirtyRect(),
681 ddl->characterization().origin());
682 }
683 GrTextureProxy* newTextureProxy = newDest->asTextureProxy();
684 if (newTextureProxy && GrMipMapped::kYes == newTextureProxy->mipMapped()) {
685 newTextureProxy->markMipMapsDirty();
686 }
687
688 this->addDDLTarget(newDest, ddl->priv().targetProxy());
Robert Phillips15c91422019-05-07 16:54:48 -0400689
Robert Phillips62000362018-02-01 09:10:04 -0500690 // Here we jam the proxy that backs the current replay SkSurface into the LazyProxyData.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400691 // The lazy proxy that references it (in the copied opsTasks) will steal its GrTexture.
Robert Phillips62000362018-02-01 09:10:04 -0500692 ddl->fLazyProxyData->fReplayDest = newDest;
Robert Phillips774168e2018-05-31 12:43:27 -0400693
694 if (ddl->fPendingPaths.size()) {
695 GrCoverageCountingPathRenderer* ccpr = this->getCoverageCountingPathRenderer();
696
697 ccpr->mergePendingPaths(ddl->fPendingPaths);
698 }
Robert Phillips22310d62018-09-05 11:07:21 -0400699
Chris Dalton6b498102019-08-01 14:14:52 -0600700 fDAG.add(ddl->fRenderTasks);
Robert Phillips38d64b02018-09-04 13:23:26 -0400701
702 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500703}
704
Robert Phillips38d64b02018-09-04 13:23:26 -0400705#ifdef SK_DEBUG
706void GrDrawingManager::validate() const {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400707 if (fDAG.sortingRenderTasks() && fReduceOpsTaskSplitting) {
708 SkASSERT(!fActiveOpsTask);
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400709 } else {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400710 if (fActiveOpsTask) {
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400711 SkASSERT(!fDAG.empty());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400712 SkASSERT(!fActiveOpsTask->isClosed());
713 SkASSERT(fActiveOpsTask == fDAG.back());
Robert Phillips38d64b02018-09-04 13:23:26 -0400714 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400715
Chris Dalton6b498102019-08-01 14:14:52 -0600716 for (int i = 0; i < fDAG.numRenderTasks(); ++i) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400717 if (fActiveOpsTask != fDAG.renderTask(i)) {
Chris Daltone2a903e2019-09-18 13:41:50 -0600718 // The resolveTask associated with the activeTask remains open for as long as the
719 // activeTask does.
720 bool isActiveResolveTask =
721 fActiveOpsTask && fActiveOpsTask->fTextureResolveTask == fDAG.renderTask(i);
722 SkASSERT(isActiveResolveTask || fDAG.renderTask(i)->isClosed());
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400723 }
724 }
725
726 if (!fDAG.empty() && !fDAG.back()->isClosed()) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400727 SkASSERT(fActiveOpsTask == fDAG.back());
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400728 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400729 }
730}
731#endif
732
Greg Danielbbfec9d2019-08-20 10:56:51 -0400733void GrDrawingManager::closeRenderTasksForNewRenderTask(GrSurfaceProxy* target) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400734 if (target && fDAG.sortingRenderTasks() && fReduceOpsTaskSplitting) {
Chris Dalton5fe99772019-08-06 11:57:39 -0600735 // In this case we need to close all the renderTasks that rely on the current contents of
736 // 'target'. That is bc we're going to update the content of the proxy so they need to be
737 // split in case they use both the old and new content. (This is a bit of an overkill: they
738 // really only need to be split if they ever reference proxy's contents again but that is
739 // hard to predict/handle).
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400740 if (GrRenderTask* lastRenderTask = this->getLastRenderTask(target)) {
Chris Dalton6b498102019-08-01 14:14:52 -0600741 lastRenderTask->closeThoseWhoDependOnMe(*fContext->priv().caps());
Robert Phillips46acf9d2018-10-09 09:31:40 -0400742 }
Greg Danielf41b2bd2019-08-22 16:19:24 -0400743 } else if (fActiveOpsTask) {
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400744 // This is a temporary fix for the partial-MDB world. In that world we're not
Greg Danielf41b2bd2019-08-22 16:19:24 -0400745 // reordering so ops that (in the single opsTask world) would've just glommed onto the
746 // end of the single opsTask but referred to a far earlier RT need to appear in their
747 // own opsTask.
748 fActiveOpsTask->makeClosed(*fContext->priv().caps());
749 fActiveOpsTask = nullptr;
robertphillips3dc6ae52015-10-20 09:54:32 -0700750 }
Chris Dalton5fe99772019-08-06 11:57:39 -0600751}
752
Greg Daniel16f5c652019-10-29 11:26:01 -0400753sk_sp<GrOpsTask> GrDrawingManager::newOpsTask(GrSurfaceProxyView surfaceView,
754 bool managedOpsTask) {
Chris Dalton5fe99772019-08-06 11:57:39 -0600755 SkDEBUGCODE(this->validate());
756 SkASSERT(fContext);
757
Greg Daniel16f5c652019-10-29 11:26:01 -0400758 GrSurfaceProxy* proxy = surfaceView.proxy();
759 this->closeRenderTasksForNewRenderTask(proxy);
robertphillips3dc6ae52015-10-20 09:54:32 -0700760
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400761 sk_sp<GrOpsTask> opsTask(new GrOpsTask(this, fContext->priv().arenas(),
Michael Ludwigd0840ec2019-12-12 09:48:38 -0500762 std::move(surfaceView),
763 fContext->priv().auditTrail()));
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400764 SkASSERT(this->getLastRenderTask(proxy) == opsTask.get());
robertphillips3dc6ae52015-10-20 09:54:32 -0700765
Greg Danielf41b2bd2019-08-22 16:19:24 -0400766 if (managedOpsTask) {
767 fDAG.add(opsTask);
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400768
Greg Danielf41b2bd2019-08-22 16:19:24 -0400769 if (!fDAG.sortingRenderTasks() || !fReduceOpsTaskSplitting) {
770 fActiveOpsTask = opsTask.get();
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400771 }
Robert Phillips941d1442017-06-14 16:37:02 -0400772 }
robertphillips3dc6ae52015-10-20 09:54:32 -0700773
Robert Phillips38d64b02018-09-04 13:23:26 -0400774 SkDEBUGCODE(this->validate());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400775 return opsTask;
robertphillips3dc6ae52015-10-20 09:54:32 -0700776}
777
Chris Daltone2a903e2019-09-18 13:41:50 -0600778GrTextureResolveRenderTask* GrDrawingManager::newTextureResolveRenderTask(const GrCaps& caps) {
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600779 // 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 -0600780 // in sorting and opsTask reduction mode) the render tasks that depend on any proxy's current
Greg Danielf41b2bd2019-08-22 16:19:24 -0400781 // state. This is because those opsTasks can still receive new ops and because if they refer to
Chris Dalton4ece96d2019-08-30 11:26:39 -0600782 // the mipmapped version of 'proxy', they will then come to depend on the render task being
783 // created here.
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600784 //
Greg Danielf41b2bd2019-08-22 16:19:24 -0400785 // Add the new textureResolveTask before the fActiveOpsTask (if not in
786 // sorting/opsTask-splitting-reduction mode) because it will depend upon this resolve task.
Chris Dalton3d770272019-08-14 09:24:37 -0600787 // NOTE: Putting it here will also reduce the amount of work required by the topological sort.
Chris Daltone2a903e2019-09-18 13:41:50 -0600788 return static_cast<GrTextureResolveRenderTask*>(fDAG.addBeforeLast(
789 sk_make_sp<GrTextureResolveRenderTask>()));
Chris Dalton3d770272019-08-14 09:24:37 -0600790}
791
Greg Danielc30f1a92019-09-06 15:28:58 -0400792void GrDrawingManager::newWaitRenderTask(sk_sp<GrSurfaceProxy> proxy,
Greg Daniel301015c2019-11-18 14:06:46 -0500793 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> semaphores,
Greg Danielc30f1a92019-09-06 15:28:58 -0400794 int numSemaphores) {
795 SkDEBUGCODE(this->validate());
796 SkASSERT(fContext);
797
798 const GrCaps& caps = *fContext->priv().caps();
799
Greg Daniel16f5c652019-10-29 11:26:01 -0400800 sk_sp<GrWaitRenderTask> waitTask = sk_make_sp<GrWaitRenderTask>(GrSurfaceProxyView(proxy),
801 std::move(semaphores),
Greg Danielc30f1a92019-09-06 15:28:58 -0400802 numSemaphores);
803 if (fReduceOpsTaskSplitting) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400804 GrRenderTask* lastTask = this->getLastRenderTask(proxy.get());
Greg Danielc30f1a92019-09-06 15:28:58 -0400805 if (lastTask && !lastTask->isClosed()) {
806 // We directly make the currently open renderTask depend on waitTask instead of using
807 // the proxy version of addDependency. The waitTask will never need to trigger any
808 // resolves or mip map generation which is the main advantage of going through the proxy
809 // version. Additionally we would've had to temporarily set the wait task as the
810 // lastRenderTask on the proxy, add the dependency, and then reset the lastRenderTask to
811 // lastTask. Additionally we add all dependencies of lastTask to waitTask so that the
812 // waitTask doesn't get reordered before them and unnecessarily block those tasks.
813 // Note: Any previous Ops already in lastTask will get blocked by the wait semaphore
814 // even though they don't need to be for correctness.
815
816 // Make sure we add the dependencies of lastTask to waitTask first or else we'll get a
817 // circular self dependency of waitTask on waitTask.
818 waitTask->addDependenciesFromOtherTask(lastTask);
819 lastTask->addDependency(waitTask.get());
820 } else {
821 // If there is a last task we set the waitTask to depend on it so that it doesn't get
822 // reordered in front of the lastTask causing the lastTask to be blocked by the
823 // semaphore. Again we directly just go through adding the dependency to the task and
824 // not the proxy since we don't need to worry about resolving anything.
825 if (lastTask) {
826 waitTask->addDependency(lastTask);
827 }
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400828 this->setLastRenderTask(proxy.get(), waitTask.get());
Greg Danielc30f1a92019-09-06 15:28:58 -0400829 }
830 fDAG.add(waitTask);
831 } else {
Greg Daniel16f5c652019-10-29 11:26:01 -0400832 if (fActiveOpsTask && (fActiveOpsTask->fTargetView.proxy() == proxy.get())) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400833 SkASSERT(this->getLastRenderTask(proxy.get()) == fActiveOpsTask);
Greg Danielc30f1a92019-09-06 15:28:58 -0400834 fDAG.addBeforeLast(waitTask);
835 // In this case we keep the current renderTask open but just insert the new waitTask
836 // before it in the list. The waitTask will never need to trigger any resolves or mip
837 // map generation which is the main advantage of going through the proxy version.
838 // Additionally we would've had to temporarily set the wait task as the lastRenderTask
839 // on the proxy, add the dependency, and then reset the lastRenderTask to
840 // fActiveOpsTask. Additionally we make the waitTask depend on all of fActiveOpsTask
841 // dependencies so that we don't unnecessarily reorder the waitTask before them.
842 // Note: Any previous Ops already in fActiveOpsTask will get blocked by the wait
843 // semaphore even though they don't need to be for correctness.
844
845 // Make sure we add the dependencies of fActiveOpsTask to waitTask first or else we'll
846 // get a circular self dependency of waitTask on waitTask.
847 waitTask->addDependenciesFromOtherTask(fActiveOpsTask);
848 fActiveOpsTask->addDependency(waitTask.get());
849 } else {
850 // In this case we just close the previous RenderTask and start and append the waitTask
851 // to the DAG. Since it is the last task now we call setLastRenderTask on the proxy. If
852 // there is a lastTask on the proxy we make waitTask depend on that task. This
853 // dependency isn't strictly needed but it does keep the DAG from reordering the
854 // waitTask earlier and blocking more tasks.
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400855 if (GrRenderTask* lastTask = this->getLastRenderTask(proxy.get())) {
Greg Danielc30f1a92019-09-06 15:28:58 -0400856 waitTask->addDependency(lastTask);
857 }
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400858 this->setLastRenderTask(proxy.get(), waitTask.get());
Greg Danielc30f1a92019-09-06 15:28:58 -0400859 this->closeRenderTasksForNewRenderTask(proxy.get());
860 fDAG.add(waitTask);
861 }
862 }
863 waitTask->makeClosed(caps);
864
865 SkDEBUGCODE(this->validate());
866}
867
Greg Danielbbfec9d2019-08-20 10:56:51 -0400868void GrDrawingManager::newTransferFromRenderTask(sk_sp<GrSurfaceProxy> srcProxy,
869 const SkIRect& srcRect,
870 GrColorType surfaceColorType,
871 GrColorType dstColorType,
872 sk_sp<GrGpuBuffer> dstBuffer,
873 size_t dstOffset) {
874 SkDEBUGCODE(this->validate());
875 SkASSERT(fContext);
876 // This copies from srcProxy to dstBuffer so it doesn't have a real target.
877 this->closeRenderTasksForNewRenderTask(nullptr);
878
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600879 GrRenderTask* task = fDAG.add(sk_make_sp<GrTransferFromRenderTask>(
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400880 srcProxy, srcRect, surfaceColorType, dstColorType,
881 std::move(dstBuffer), dstOffset));
Greg Danielbbfec9d2019-08-20 10:56:51 -0400882
883 const GrCaps& caps = *fContext->priv().caps();
884
885 // We always say GrMipMapped::kNo here since we are always just copying from the base layer. We
886 // don't need to make sure the whole mip map chain is valid.
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400887 task->addDependency(this, srcProxy.get(), GrMipMapped::kNo,
888 GrTextureResolveManager(this), caps);
Greg Danielbbfec9d2019-08-20 10:56:51 -0400889 task->makeClosed(caps);
890
Greg Danielbbfec9d2019-08-20 10:56:51 -0400891 // We have closed the previous active oplist but since a new oplist isn't being added there
892 // shouldn't be an active one.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400893 SkASSERT(!fActiveOpsTask);
Greg Danielbbfec9d2019-08-20 10:56:51 -0400894 SkDEBUGCODE(this->validate());
895}
896
Greg Daniel16f5c652019-10-29 11:26:01 -0400897bool GrDrawingManager::newCopyRenderTask(GrSurfaceProxyView srcView,
Greg Daniele227fe42019-08-21 13:52:24 -0400898 const SkIRect& srcRect,
Greg Daniel16f5c652019-10-29 11:26:01 -0400899 GrSurfaceProxyView dstView,
Greg Daniele227fe42019-08-21 13:52:24 -0400900 const SkIPoint& dstPoint) {
901 SkDEBUGCODE(this->validate());
902 SkASSERT(fContext);
Greg Daniele227fe42019-08-21 13:52:24 -0400903
Greg Daniel16f5c652019-10-29 11:26:01 -0400904 this->closeRenderTasksForNewRenderTask(dstView.proxy());
Brian Salomone4bce012019-09-20 15:34:23 -0400905 const GrCaps& caps = *fContext->priv().caps();
906
Greg Daniel16f5c652019-10-29 11:26:01 -0400907 GrSurfaceProxy* srcProxy = srcView.proxy();
908
Brian Salomone4bce012019-09-20 15:34:23 -0400909 GrRenderTask* task =
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400910 fDAG.add(GrCopyRenderTask::Make(this, std::move(srcView), srcRect, std::move(dstView),
Greg Daniel16f5c652019-10-29 11:26:01 -0400911 dstPoint, &caps));
Greg Daniele227fe42019-08-21 13:52:24 -0400912 if (!task) {
913 return false;
914 }
915
Greg Daniele227fe42019-08-21 13:52:24 -0400916 // We always say GrMipMapped::kNo here since we are always just copying from the base layer to
917 // another base layer. We don't need to make sure the whole mip map chain is valid.
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400918 task->addDependency(this, srcProxy, GrMipMapped::kNo, GrTextureResolveManager(this), caps);
Greg Daniele227fe42019-08-21 13:52:24 -0400919 task->makeClosed(caps);
920
Greg Daniele227fe42019-08-21 13:52:24 -0400921 // We have closed the previous active oplist but since a new oplist isn't being added there
922 // shouldn't be an active one.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400923 SkASSERT(!fActiveOpsTask);
Greg Daniele227fe42019-08-21 13:52:24 -0400924 SkDEBUGCODE(this->validate());
925 return true;
926}
927
robertphillips68737822015-10-29 12:12:21 -0700928/*
929 * This method finds a path renderer that can draw the specified path on
930 * the provided target.
931 * Due to its expense, the software path renderer has split out so it can
932 * can be individually allowed/disallowed via the "allowSW" boolean.
933 */
934GrPathRenderer* GrDrawingManager::getPathRenderer(const GrPathRenderer::CanDrawPathArgs& args,
935 bool allowSW,
936 GrPathRendererChain::DrawType drawType,
937 GrPathRenderer::StencilSupport* stencilSupport) {
938
939 if (!fPathRendererChain) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400940 fPathRendererChain.reset(new GrPathRendererChain(fContext, fOptionsForPathRendererChain));
robertphillips68737822015-10-29 12:12:21 -0700941 }
942
943 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(args, drawType, stencilSupport);
944 if (!pr && allowSW) {
Brian Salomone7df0bb2018-05-07 14:44:57 -0400945 auto swPR = this->getSoftwarePathRenderer();
946 if (GrPathRenderer::CanDrawPath::kNo != swPR->canDrawPath(args)) {
947 pr = swPR;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500948 }
robertphillips68737822015-10-29 12:12:21 -0700949 }
950
Robert Phillipsd81379d2020-04-21 10:39:02 -0400951#if GR_PATH_RENDERER_SPEW
952 if (pr) {
953 SkDebugf("getPathRenderer: %s\n", pr->name());
954 }
955#endif
956
robertphillips68737822015-10-29 12:12:21 -0700957 return pr;
958}
959
Brian Salomone7df0bb2018-05-07 14:44:57 -0400960GrPathRenderer* GrDrawingManager::getSoftwarePathRenderer() {
961 if (!fSoftwarePathRenderer) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400962 fSoftwarePathRenderer.reset(
Robert Phillips9da87e02019-02-04 13:26:26 -0500963 new GrSoftwarePathRenderer(fContext->priv().proxyProvider(),
Ben Wagner9ec70c62018-07-12 13:30:47 -0400964 fOptionsForPathRendererChain.fAllowPathMaskCaching));
Brian Salomone7df0bb2018-05-07 14:44:57 -0400965 }
Ben Wagner9ec70c62018-07-12 13:30:47 -0400966 return fSoftwarePathRenderer.get();
Brian Salomone7df0bb2018-05-07 14:44:57 -0400967}
968
Chris Daltonfddb6c02017-11-04 15:22:22 -0600969GrCoverageCountingPathRenderer* GrDrawingManager::getCoverageCountingPathRenderer() {
970 if (!fPathRendererChain) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400971 fPathRendererChain.reset(new GrPathRendererChain(fContext, fOptionsForPathRendererChain));
Chris Daltonfddb6c02017-11-04 15:22:22 -0600972 }
973 return fPathRendererChain->getCoverageCountingPathRenderer();
974}
975
Brian Salomon653f42f2018-07-10 10:07:31 -0400976void GrDrawingManager::flushIfNecessary() {
Robert Phillips6a6de562019-02-15 15:19:15 -0500977 auto direct = fContext->priv().asDirectContext();
978 if (!direct) {
979 return;
980 }
981
982 auto resourceCache = direct->priv().getResourceCache();
Brian Salomon653f42f2018-07-10 10:07:31 -0400983 if (resourceCache && resourceCache->requestsFlush()) {
Greg Daniele8d3cca2020-06-10 10:04:48 -0400984 if (this->flush(nullptr, 0, SkSurface::BackendSurfaceAccess::kNoAccess, GrFlushInfo())) {
Greg Danielfe159622020-04-10 17:43:51 +0000985 this->submitToGpu(false);
986 }
Brian Salomon57d2beab2018-09-10 09:35:41 -0400987 resourceCache->purgeAsNeeded();
Brian Salomon653f42f2018-07-10 10:07:31 -0400988 }
989}
990