blob: 504579bbc49026bead0cd851697acfd51b3e9282 [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"
Adlai Holler6c9bb622020-06-25 09:21:18 -040038#include "src/gpu/GrUnrefDDLTask.h"
Greg Danielc30f1a92019-09-06 15:28:58 -040039#include "src/gpu/GrWaitRenderTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050040#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
Herb Derbya08bde62020-06-12 15:46:06 -040041#include "src/gpu/text/GrSDFTOptions.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050042#include "src/image/SkSurface_Gpu.h"
robertphillips498d7ac2015-10-30 10:11:30 -070043
Chris Dalton6b498102019-08-01 14:14:52 -060044GrDrawingManager::RenderTaskDAG::RenderTaskDAG(bool sortRenderTasks)
45 : fSortRenderTasks(sortRenderTasks) {}
Robert Phillipsa3f70262018-02-08 10:59:38 -050046
Chris Dalton6b498102019-08-01 14:14:52 -060047GrDrawingManager::RenderTaskDAG::~RenderTaskDAG() {}
Robert Phillips22310d62018-09-05 11:07:21 -040048
Chris Dalton6b498102019-08-01 14:14:52 -060049void GrDrawingManager::RenderTaskDAG::gatherIDs(SkSTArray<8, uint32_t, true>* idArray) const {
50 idArray->reset(fRenderTasks.count());
51 for (int i = 0; i < fRenderTasks.count(); ++i) {
52 if (fRenderTasks[i]) {
53 (*idArray)[i] = fRenderTasks[i]->uniqueID();
Robert Phillips22310d62018-09-05 11:07:21 -040054 }
55 }
56}
57
Chris Dalton6b498102019-08-01 14:14:52 -060058void GrDrawingManager::RenderTaskDAG::reset() {
59 fRenderTasks.reset();
Robert Phillips22310d62018-09-05 11:07:21 -040060}
61
Adlai Holler96ead542020-06-26 08:50:14 -040062void GrDrawingManager::RenderTaskDAG::rawRemoveRenderTasks(int startIndex, int stopIndex) {
Robert Phillips22310d62018-09-05 11:07:21 -040063 for (int i = startIndex; i < stopIndex; ++i) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -040064 fRenderTasks[i] = nullptr;
Robert Phillips22310d62018-09-05 11:07:21 -040065 }
66}
67
Chris Dalton6b498102019-08-01 14:14:52 -060068bool GrDrawingManager::RenderTaskDAG::isUsed(GrSurfaceProxy* proxy) const {
Adlai Holler33d569e2020-06-16 14:30:08 -040069 for (const auto& task : fRenderTasks) {
70 if (task && task->isUsed(proxy)) {
Robert Phillips9313aa72019-04-09 18:41:27 -040071 return true;
72 }
73 }
74
75 return false;
76}
77
Chris Dalton3d770272019-08-14 09:24:37 -060078GrRenderTask* GrDrawingManager::RenderTaskDAG::add(sk_sp<GrRenderTask> renderTask) {
Chris Dalton6aeb8e82019-08-27 11:52:19 -060079 if (renderTask) {
80 return fRenderTasks.emplace_back(std::move(renderTask)).get();
81 }
82 return nullptr;
Chris Dalton3d770272019-08-14 09:24:37 -060083}
84
85GrRenderTask* GrDrawingManager::RenderTaskDAG::addBeforeLast(sk_sp<GrRenderTask> renderTask) {
86 SkASSERT(!fRenderTasks.empty());
Chris Dalton6aeb8e82019-08-27 11:52:19 -060087 if (renderTask) {
88 // Release 'fRenderTasks.back()' and grab the raw pointer, in case the SkTArray grows
89 // and reallocates during emplace_back.
90 fRenderTasks.emplace_back(fRenderTasks.back().release());
91 return (fRenderTasks[fRenderTasks.count() - 2] = std::move(renderTask)).get();
92 }
93 return nullptr;
Robert Phillips22310d62018-09-05 11:07:21 -040094}
95
Greg Danielf41b2bd2019-08-22 16:19:24 -040096void GrDrawingManager::RenderTaskDAG::add(const SkTArray<sk_sp<GrRenderTask>>& renderTasks) {
Robert Phillips19f466d2020-02-26 10:27:07 -050097#ifdef SK_DEBUG
98 for (auto& renderTask : renderTasks) {
99 SkASSERT(renderTask->unique());
100 }
101#endif
102
Greg Danielf41b2bd2019-08-22 16:19:24 -0400103 fRenderTasks.push_back_n(renderTasks.count(), renderTasks.begin());
Robert Phillips22310d62018-09-05 11:07:21 -0400104}
105
Greg Danielf41b2bd2019-08-22 16:19:24 -0400106void GrDrawingManager::RenderTaskDAG::swap(SkTArray<sk_sp<GrRenderTask>>* renderTasks) {
107 SkASSERT(renderTasks->empty());
108 renderTasks->swap(fRenderTasks);
Robert Phillips22310d62018-09-05 11:07:21 -0400109}
110
Chris Dalton6b498102019-08-01 14:14:52 -0600111void GrDrawingManager::RenderTaskDAG::prepForFlush() {
112 if (fSortRenderTasks) {
113 SkDEBUGCODE(bool result =) SkTTopoSort<GrRenderTask, GrRenderTask::TopoSortTraits>(
114 &fRenderTasks);
Robert Phillips22310d62018-09-05 11:07:21 -0400115 SkASSERT(result);
116 }
117
118#ifdef SK_DEBUG
Greg Danielf41b2bd2019-08-22 16:19:24 -0400119 // This block checks for any unnecessary splits in the opsTasks. If two sequential opsTasks
120 // share the same backing GrSurfaceProxy it means the opsTask was artificially split.
Chris Dalton6b498102019-08-01 14:14:52 -0600121 if (fRenderTasks.count()) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400122 GrOpsTask* prevOpsTask = fRenderTasks[0]->asOpsTask();
Chris Dalton6b498102019-08-01 14:14:52 -0600123 for (int i = 1; i < fRenderTasks.count(); ++i) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400124 GrOpsTask* curOpsTask = fRenderTasks[i]->asOpsTask();
Robert Phillips22310d62018-09-05 11:07:21 -0400125
Greg Danielf41b2bd2019-08-22 16:19:24 -0400126 if (prevOpsTask && curOpsTask) {
Adlai Holler33d569e2020-06-16 14:30:08 -0400127 SkASSERT(prevOpsTask->target(0).proxy() != curOpsTask->target(0).proxy());
Robert Phillips22310d62018-09-05 11:07:21 -0400128 }
129
Greg Danielf41b2bd2019-08-22 16:19:24 -0400130 prevOpsTask = curOpsTask;
Robert Phillips22310d62018-09-05 11:07:21 -0400131 }
132 }
133#endif
134}
135
Chris Dalton6b498102019-08-01 14:14:52 -0600136void GrDrawingManager::RenderTaskDAG::closeAll(const GrCaps* caps) {
Adlai Holler96ead542020-06-26 08:50:14 -0400137 for (auto& task : fRenderTasks) {
138 if (task) {
139 task->makeClosed(*caps);
Robert Phillips22310d62018-09-05 11:07:21 -0400140 }
141 }
142}
143
Robert Phillips22310d62018-09-05 11:07:21 -0400144///////////////////////////////////////////////////////////////////////////////////////////////////
Robert Phillips69893702019-02-22 11:16:30 -0500145GrDrawingManager::GrDrawingManager(GrRecordingContext* context,
Robert Phillips22310d62018-09-05 11:07:21 -0400146 const GrPathRendererChain::Options& optionsForPathRendererChain,
Chris Dalton6b498102019-08-01 14:14:52 -0600147 bool sortRenderTasks,
Greg Danielf41b2bd2019-08-22 16:19:24 -0400148 bool reduceOpsTaskSplitting)
Robert Phillips22310d62018-09-05 11:07:21 -0400149 : fContext(context)
150 , fOptionsForPathRendererChain(optionsForPathRendererChain)
Chris Dalton6b498102019-08-01 14:14:52 -0600151 , fDAG(sortRenderTasks)
Robert Phillips22310d62018-09-05 11:07:21 -0400152 , fPathRendererChain(nullptr)
153 , fSoftwarePathRenderer(nullptr)
Robert Phillips6db27c22019-05-01 10:43:56 -0400154 , fFlushing(false)
Herb Derby082232b2020-06-10 15:08:18 -0400155 , fReduceOpsTaskSplitting(reduceOpsTaskSplitting) { }
Robert Phillips22310d62018-09-05 11:07:21 -0400156
robertphillips3dc6ae52015-10-20 09:54:32 -0700157GrDrawingManager::~GrDrawingManager() {
Adlai Holler96ead542020-06-26 08:50:14 -0400158 fDAG.closeAll(fContext->priv().caps());
159 this->removeRenderTasks(0, fDAG.numRenderTasks());
robertphillips3dc6ae52015-10-20 09:54:32 -0700160}
161
Robert Phillipsa9162df2019-02-11 14:12:03 -0500162bool GrDrawingManager::wasAbandoned() const {
Robert Phillips6a6de562019-02-15 15:19:15 -0500163 return fContext->priv().abandoned();
robertphillips3dc6ae52015-10-20 09:54:32 -0700164}
165
robertphillips68737822015-10-29 12:12:21 -0700166void GrDrawingManager::freeGpuResources() {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400167 for (int i = fOnFlushCBObjects.count() - 1; i >= 0; --i) {
168 if (!fOnFlushCBObjects[i]->retainOnFreeGpuResources()) {
169 // it's safe to just do this because we're iterating in reverse
170 fOnFlushCBObjects.removeShuffle(i);
171 }
172 }
173
robertphillips68737822015-10-29 12:12:21 -0700174 // a path renderer may be holding onto resources
robertphillips13391dd2015-10-30 05:15:11 -0700175 fPathRendererChain = nullptr;
Ben Wagner9ec70c62018-07-12 13:30:47 -0400176 fSoftwarePathRenderer = nullptr;
Robert Phillipse3302df2017-04-24 07:31:02 -0400177}
178
Robert Phillips7ee385e2017-03-30 08:02:11 -0400179// MDB TODO: make use of the 'proxy' parameter.
Greg Daniel9efe3862020-06-11 11:51:06 -0400180bool GrDrawingManager::flush(
181 GrSurfaceProxy* proxies[],
182 int numProxies,
183 SkSurface::BackendSurfaceAccess access,
184 const GrFlushInfo& info,
185 const GrBackendSurfaceMutableState* newState) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400186 SkASSERT(numProxies >= 0);
187 SkASSERT(!numProxies || proxies);
Brian Salomon57d2beab2018-09-10 09:35:41 -0400188 GR_CREATE_TRACE_MARKER_CONTEXT("GrDrawingManager", "flush", fContext);
Brian Salomondcbb9d92017-07-19 10:53:20 -0400189
robertphillips7761d612016-05-16 09:14:53 -0700190 if (fFlushing || this->wasAbandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -0400191 if (info.fSubmittedProc) {
192 info.fSubmittedProc(info.fSubmittedContext, false);
193 }
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400194 if (info.fFinishedProc) {
195 info.fFinishedProc(info.fFinishedContext);
Greg Daniela3aa75a2019-04-12 14:24:55 -0400196 }
Greg Danielfe159622020-04-10 17:43:51 +0000197 return false;
joshualittb8918c42015-12-18 09:59:46 -0800198 }
Robert Phillips602df412019-04-08 11:10:39 -0400199
Robert Phillips38d64b02018-09-04 13:23:26 -0400200 SkDEBUGCODE(this->validate());
201
Greg Daniel797efca2019-05-09 14:04:20 -0400202 if (kNone_GrFlushFlags == info.fFlags && !info.fNumSemaphores && !info.fFinishedProc &&
Greg Daniel9efe3862020-06-11 11:51:06 -0400203 access == SkSurface::BackendSurfaceAccess::kNoAccess && !newState) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400204 bool canSkip = numProxies > 0;
205 for (int i = 0; i < numProxies && canSkip; ++i) {
206 canSkip = !fDAG.isUsed(proxies[i]) && !this->isDDLTarget(proxies[i]);
207 }
208 if (canSkip) {
Greg Daniel55822f12020-05-26 11:26:45 -0400209 if (info.fSubmittedProc) {
210 info.fSubmittedProc(info.fSubmittedContext, true);
211 }
Greg Danielfe159622020-04-10 17:43:51 +0000212 return false;
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400213 }
Robert Phillips9313aa72019-04-09 18:41:27 -0400214 }
215
Robert Phillips6a6de562019-02-15 15:19:15 -0500216 auto direct = fContext->priv().asDirectContext();
217 if (!direct) {
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; // Can't flush while DDL recording
Robert Phillips6a6de562019-02-15 15:19:15 -0500225 }
Brian Salomon9241a6d2019-10-03 13:26:54 -0400226 direct->priv().clientMappedBufferManager()->process();
Robert Phillips6a6de562019-02-15 15:19:15 -0500227
228 GrGpu* gpu = direct->priv().getGpu();
Greg Daniel55822f12020-05-26 11:26:45 -0400229 // We have a non abandoned and direct GrContext. It must have a GrGpu.
230 SkASSERT(gpu);
Greg Daniela3aa75a2019-04-12 14:24:55 -0400231
joshualittb8918c42015-12-18 09:59:46 -0800232 fFlushing = true;
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400233
Robert Phillips6a6de562019-02-15 15:19:15 -0500234 auto resourceProvider = direct->priv().resourceProvider();
235 auto resourceCache = direct->priv().getResourceCache();
236
Chris Dalton6b498102019-08-01 14:14:52 -0600237 // Semi-usually the GrRenderTasks are already closed at this point, but sometimes Ganesh needs
Greg Danielf41b2bd2019-08-22 16:19:24 -0400238 // to flush mid-draw. In that case, the SkGpuDevice's opsTasks won't be closed but need to be
239 // flushed anyway. Closing such opsTasks here will mean new ones will be created to replace them
Chris Dalton6b498102019-08-01 14:14:52 -0600240 // if the SkGpuDevice(s) write to them again.
Robert Phillips9da87e02019-02-04 13:26:26 -0500241 fDAG.closeAll(fContext->priv().caps());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400242 fActiveOpsTask = nullptr;
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400243
Robert Phillips22310d62018-09-05 11:07:21 -0400244 fDAG.prepForFlush();
Brian Salomon601ac802019-02-07 13:37:16 -0500245 if (!fCpuBufferCache) {
246 // We cache more buffers when the backend is using client side arrays. Otherwise, we
247 // expect each pool will use a CPU buffer as a staging buffer before uploading to a GPU
248 // buffer object. Each pool only requires one staging buffer at a time.
249 int maxCachedBuffers = fContext->priv().caps()->preferClientSideDynamicBuffers() ? 2 : 6;
250 fCpuBufferCache = GrBufferAllocPool::CpuBufferCache::Make(maxCachedBuffers);
Brian Salomon58f153c2018-10-18 21:51:15 -0400251 }
Robert Phillipsa4c93ac2017-05-18 11:40:04 -0400252
Robert Phillipse5f73282019-06-18 17:15:04 -0400253 GrOpFlushState flushState(gpu, resourceProvider, &fTokenTracker, fCpuBufferCache);
Robert Phillips40a29d72018-01-18 12:59:22 -0500254
Chris Daltonfe199b72017-05-05 11:26:15 -0400255 GrOnFlushResourceProvider onFlushProvider(this);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400256
Chris Dalton12658942017-10-05 19:45:25 -0600257 // Prepare any onFlush op lists (e.g. atlases).
Chris Daltonfe199b72017-05-05 11:26:15 -0400258 if (!fOnFlushCBObjects.empty()) {
Chris Dalton6b498102019-08-01 14:14:52 -0600259 fDAG.gatherIDs(&fFlushingRenderTaskIDs);
Robert Phillips22310d62018-09-05 11:07:21 -0400260
Chris Daltonfe199b72017-05-05 11:26:15 -0400261 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
Chris Dalton6b498102019-08-01 14:14:52 -0600262 onFlushCBObject->preFlush(&onFlushProvider, fFlushingRenderTaskIDs.begin(),
Chris Daltonc4b47352019-08-23 10:10:36 -0600263 fFlushingRenderTaskIDs.count());
264 }
265 for (const auto& onFlushRenderTask : fOnFlushRenderTasks) {
266 onFlushRenderTask->makeClosed(*fContext->priv().caps());
Chris Dalton706a6ff2017-11-29 22:01:06 -0700267#ifdef SK_DEBUG
Chris Daltonc4b47352019-08-23 10:10:36 -0600268 // OnFlush callbacks are invoked during flush, and are therefore expected to handle
269 // resource allocation & usage on their own. (No deferred or lazy proxies!)
270 onFlushRenderTask->visitTargetAndSrcProxies_debugOnly(
271 [](GrSurfaceProxy* p, GrMipMapped mipMapped) {
272 SkASSERT(!p->asTextureProxy() || !p->asTextureProxy()->texPriv().isDeferred());
Brian Salomonbeb7f522019-08-30 16:19:42 -0400273 SkASSERT(!p->isLazy());
Chris Dalton4ece96d2019-08-30 11:26:39 -0600274 if (p->requiresManualMSAAResolve()) {
275 // The onFlush callback is responsible for ensuring MSAA gets resolved.
276 SkASSERT(p->asRenderTargetProxy() && !p->asRenderTargetProxy()->isMSAADirty());
277 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600278 if (GrMipMapped::kYes == mipMapped) {
279 // The onFlush callback is responsible for regenerating mips if needed.
280 SkASSERT(p->asTextureProxy() && !p->asTextureProxy()->mipMapsAreDirty());
281 }
282 });
Chris Dalton706a6ff2017-11-29 22:01:06 -0700283#endif
Chris Daltonc4b47352019-08-23 10:10:36 -0600284 onFlushRenderTask->prepare(&flushState);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400285 }
286 }
287
robertphillipsa13e2022015-11-11 12:01:09 -0800288#if 0
Brian Salomon09d994e2016-12-21 11:14:46 -0500289 // Enable this to print out verbose GrOp information
Chris Daltonc4b47352019-08-23 10:10:36 -0600290 SkDEBUGCODE(SkDebugf("onFlush renderTasks:"));
291 for (const auto& onFlushRenderTask : fOnFlushRenderTasks) {
292 SkDEBUGCODE(onFlushRenderTask->dump();)
293 }
294 SkDEBUGCODE(SkDebugf("Normal renderTasks:"));
Chris Dalton6b498102019-08-01 14:14:52 -0600295 for (int i = 0; i < fRenderTasks.count(); ++i) {
296 SkDEBUGCODE(fRenderTasks[i]->dump();)
robertphillips3dc6ae52015-10-20 09:54:32 -0700297 }
robertphillipsa13e2022015-11-11 12:01:09 -0800298#endif
299
Robert Phillipseafd48a2017-11-16 07:52:08 -0500300 int startIndex, stopIndex;
301 bool flushed = false;
302
Robert Phillipsf8e25022017-11-08 15:24:31 -0500303 {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400304 GrResourceAllocator alloc(resourceProvider SkDEBUGCODE(, fDAG.numRenderTasks()));
Chris Dalton6b498102019-08-01 14:14:52 -0600305 for (int i = 0; i < fDAG.numRenderTasks(); ++i) {
306 if (fDAG.renderTask(i)) {
307 fDAG.renderTask(i)->gatherProxyIntervals(&alloc);
Robert Phillips22310d62018-09-05 11:07:21 -0400308 }
Greg Danielf41b2bd2019-08-22 16:19:24 -0400309 alloc.markEndOfOpsTask(i);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500310 }
Robert Phillipsc73666f2019-04-24 08:49:48 -0400311 alloc.determineRecyclability();
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400312
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500313 GrResourceAllocator::AssignError error = GrResourceAllocator::AssignError::kNoError;
Chris Dalton6b498102019-08-01 14:14:52 -0600314 int numRenderTasksExecuted = 0;
Brian Salomon577aa0f2018-11-30 13:32:23 -0500315 while (alloc.assign(&startIndex, &stopIndex, &error)) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500316 if (GrResourceAllocator::AssignError::kFailedProxyInstantiation == error) {
317 for (int i = startIndex; i < stopIndex; ++i) {
Chris Dalton6b498102019-08-01 14:14:52 -0600318 GrRenderTask* renderTask = fDAG.renderTask(i);
319 if (!renderTask) {
320 continue;
Robert Phillips01a91282018-07-26 08:03:04 -0400321 }
Chris Dalton6b498102019-08-01 14:14:52 -0600322 if (!renderTask->isInstantiated()) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400323 // No need to call the renderTask's handleInternalAllocationFailure
324 // since we will already skip executing the renderTask since it is not
325 // instantiated.
Chris Dalton6b498102019-08-01 14:14:52 -0600326 continue;
Robert Phillipsbbcb7f72018-05-31 11:16:19 -0400327 }
Chris Dalton6b498102019-08-01 14:14:52 -0600328 renderTask->handleInternalAllocationFailure();
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500329 }
Adlai Holler96ead542020-06-26 08:50:14 -0400330 this->removeRenderTasks(startIndex, stopIndex);
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500331 }
Robert Phillips4150eea2018-02-07 17:08:21 -0500332
Chris Dalton6b498102019-08-01 14:14:52 -0600333 if (this->executeRenderTasks(
334 startIndex, stopIndex, &flushState, &numRenderTasksExecuted)) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500335 flushed = true;
336 }
bsalomondc438982016-08-31 11:53:49 -0700337 }
Greg Danielc42b20b2017-10-04 10:34:49 -0400338 }
339
Chris Dalton91ab1552018-04-18 13:24:25 -0600340#ifdef SK_DEBUG
Chris Dalton6b498102019-08-01 14:14:52 -0600341 for (int i = 0; i < fDAG.numRenderTasks(); ++i) {
Adlai Holler96ead542020-06-26 08:50:14 -0400342 // All render tasks should have been cleared out by now – we only reset the array below to
343 // reclaim storage.
344 SkASSERT(!fDAG.renderTask(i));
Chris Dalton91ab1552018-04-18 13:24:25 -0600345 }
346#endif
Adlai Holler25df1f72020-06-09 13:08:27 -0400347 fLastRenderTasks.reset();
Robert Phillips22310d62018-09-05 11:07:21 -0400348 fDAG.reset();
Robert Phillips15c91422019-05-07 16:54:48 -0400349 this->clearDDLTargets();
robertphillipsa13e2022015-11-11 12:01:09 -0800350
Robert Phillipsc994a932018-06-19 13:09:54 -0400351#ifdef SK_DEBUG
352 // In non-DDL mode this checks that all the flushed ops have been freed from the memory pool.
353 // When we move to partial flushes this assert will no longer be valid.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400354 // In DDL mode this check is somewhat superfluous since the memory for most of the ops/opsTasks
Robert Phillipsc994a932018-06-19 13:09:54 -0400355 // will be stored in the DDL's GrOpMemoryPools.
Robert Phillips9da87e02019-02-04 13:26:26 -0500356 GrOpMemoryPool* opMemoryPool = fContext->priv().opMemoryPool();
Robert Phillipsc994a932018-06-19 13:09:54 -0400357 opMemoryPool->isEmpty();
358#endif
359
Greg Daniel9efe3862020-06-11 11:51:06 -0400360 gpu->executeFlushInfo(proxies, numProxies, access, info, newState);
robertphillipsa13e2022015-11-11 12:01:09 -0800361
Brian Salomon57d2beab2018-09-10 09:35:41 -0400362 // Give the cache a chance to purge resources that become purgeable due to flushing.
363 if (flushed) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500364 resourceCache->purgeAsNeeded();
Brian Salomon876a0172019-03-08 11:12:14 -0500365 flushed = false;
bsalomonb77a9072016-09-07 10:02:04 -0700366 }
Chris Daltonfe199b72017-05-05 11:26:15 -0400367 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
Chris Dalton6b498102019-08-01 14:14:52 -0600368 onFlushCBObject->postFlush(fTokenTracker.nextTokenToFlush(), fFlushingRenderTaskIDs.begin(),
369 fFlushingRenderTaskIDs.count());
Brian Salomon876a0172019-03-08 11:12:14 -0500370 flushed = true;
371 }
372 if (flushed) {
373 resourceCache->purgeAsNeeded();
Chris Daltonfe199b72017-05-05 11:26:15 -0400374 }
Chris Dalton6b498102019-08-01 14:14:52 -0600375 fFlushingRenderTaskIDs.reset();
joshualittb8918c42015-12-18 09:59:46 -0800376 fFlushing = false;
Greg Daniel51316782017-08-02 15:10:09 +0000377
Greg Danielfe159622020-04-10 17:43:51 +0000378 return true;
379}
380
381bool GrDrawingManager::submitToGpu(bool syncToCpu) {
382 if (fFlushing || this->wasAbandoned()) {
383 return false;
384 }
385
386 auto direct = fContext->priv().asDirectContext();
387 if (!direct) {
388 return false; // Can't submit while DDL recording
389 }
390 GrGpu* gpu = direct->priv().getGpu();
391 return gpu->submitToGpu(syncToCpu);
robertphillips3dc6ae52015-10-20 09:54:32 -0700392}
393
Chris Dalton6b498102019-08-01 14:14:52 -0600394bool GrDrawingManager::executeRenderTasks(int startIndex, int stopIndex, GrOpFlushState* flushState,
395 int* numRenderTasksExecuted) {
396 SkASSERT(startIndex <= stopIndex && stopIndex <= fDAG.numRenderTasks());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500397
Robert Phillips27483912018-04-20 12:43:18 -0400398#if GR_FLUSH_TIME_OP_SPEW
Greg Danielf41b2bd2019-08-22 16:19:24 -0400399 SkDebugf("Flushing opsTask: %d to %d out of [%d, %d]\n",
Chris Dalton6b498102019-08-01 14:14:52 -0600400 startIndex, stopIndex, 0, fDAG.numRenderTasks());
Robert Phillips27483912018-04-20 12:43:18 -0400401 for (int i = startIndex; i < stopIndex; ++i) {
Chris Dalton6b498102019-08-01 14:14:52 -0600402 if (fDAG.renderTask(i)) {
403 fDAG.renderTask(i)->dump(true);
Robert Phillips1734dd32018-08-21 13:52:09 -0400404 }
Robert Phillips27483912018-04-20 12:43:18 -0400405 }
406#endif
407
Chris Dalton6b498102019-08-01 14:14:52 -0600408 bool anyRenderTasksExecuted = false;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500409
410 for (int i = startIndex; i < stopIndex; ++i) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400411 GrRenderTask* renderTask = fDAG.renderTask(i);
412 if (!renderTask || !renderTask->isInstantiated()) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500413 continue;
414 }
415
Chris Dalton6b498102019-08-01 14:14:52 -0600416 SkASSERT(renderTask->deferredProxiesAreInstantiated());
Robert Phillips22310d62018-09-05 11:07:21 -0400417
Chris Dalton6b498102019-08-01 14:14:52 -0600418 renderTask->prepare(flushState);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500419 }
420
421 // Upload all data to the GPU
422 flushState->preExecuteDraws();
423
Greg Danield2073452018-12-07 11:20:33 -0500424 // For Vulkan, if we have too many oplists to be flushed we end up allocating a lot of resources
425 // for each command buffer associated with the oplists. If this gets too large we can cause the
426 // devices to go OOM. In practice we usually only hit this case in our tests, but to be safe we
427 // put a cap on the number of oplists we will execute before flushing to the GPU to relieve some
428 // memory pressure.
Chris Dalton6b498102019-08-01 14:14:52 -0600429 static constexpr int kMaxRenderTasksBeforeFlush = 100;
Greg Danield2073452018-12-07 11:20:33 -0500430
Chris Daltonc4b47352019-08-23 10:10:36 -0600431 // Execute the onFlush renderTasks first, if any.
432 for (sk_sp<GrRenderTask>& onFlushRenderTask : fOnFlushRenderTasks) {
433 if (!onFlushRenderTask->execute(flushState)) {
434 SkDebugf("WARNING: onFlushRenderTask failed to execute.\n");
Robert Phillipseafd48a2017-11-16 07:52:08 -0500435 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600436 SkASSERT(onFlushRenderTask->unique());
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400437 onFlushRenderTask->disown(this);
Chris Daltonc4b47352019-08-23 10:10:36 -0600438 onFlushRenderTask = nullptr;
Chris Dalton6b498102019-08-01 14:14:52 -0600439 (*numRenderTasksExecuted)++;
440 if (*numRenderTasksExecuted >= kMaxRenderTasksBeforeFlush) {
Greg Danielfe159622020-04-10 17:43:51 +0000441 flushState->gpu()->submitToGpu(false);
Chris Dalton6b498102019-08-01 14:14:52 -0600442 *numRenderTasksExecuted = 0;
Greg Danield2073452018-12-07 11:20:33 -0500443 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500444 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600445 fOnFlushRenderTasks.reset();
Robert Phillipseafd48a2017-11-16 07:52:08 -0500446
447 // Execute the normal op lists.
448 for (int i = startIndex; i < stopIndex; ++i) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400449 GrRenderTask* renderTask = fDAG.renderTask(i);
450 if (!renderTask || !renderTask->isInstantiated()) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500451 continue;
452 }
453
Greg Daniel15ecdf92019-08-30 15:35:23 -0400454 if (renderTask->execute(flushState)) {
Chris Dalton6b498102019-08-01 14:14:52 -0600455 anyRenderTasksExecuted = true;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500456 }
Chris Dalton6b498102019-08-01 14:14:52 -0600457 (*numRenderTasksExecuted)++;
458 if (*numRenderTasksExecuted >= kMaxRenderTasksBeforeFlush) {
Greg Danielfe159622020-04-10 17:43:51 +0000459 flushState->gpu()->submitToGpu(false);
Chris Dalton6b498102019-08-01 14:14:52 -0600460 *numRenderTasksExecuted = 0;
Greg Danield2073452018-12-07 11:20:33 -0500461 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500462 }
463
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400464 SkASSERT(!flushState->opsRenderPass());
Robert Phillips40a29d72018-01-18 12:59:22 -0500465 SkASSERT(fTokenTracker.nextDrawToken() == fTokenTracker.nextTokenToFlush());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500466
Chris Dalton6b498102019-08-01 14:14:52 -0600467 // We reset the flush state before the RenderTasks so that the last resources to be freed are
468 // those that are written to in the RenderTasks. This helps to make sure the most recently used
469 // resources are the last to be purged by the resource cache.
Robert Phillipseafd48a2017-11-16 07:52:08 -0500470 flushState->reset();
471
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400472 this->removeRenderTasks(startIndex, stopIndex);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500473
Chris Dalton6b498102019-08-01 14:14:52 -0600474 return anyRenderTasksExecuted;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500475}
476
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400477void GrDrawingManager::removeRenderTasks(int startIndex, int stopIndex) {
478 for (int i = startIndex; i < stopIndex; ++i) {
479 GrRenderTask* task = fDAG.renderTask(i);
480 if (!task) {
481 continue;
482 }
483 if (!task->unique()) {
484 // TODO: Eventually this should be guaranteed unique: http://skbug.com/7111
485 task->endFlush(this);
486 }
487 task->disown(this);
488 }
Adlai Holler96ead542020-06-26 08:50:14 -0400489 fDAG.rawRemoveRenderTasks(startIndex, stopIndex);
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400490}
491
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400492static void resolve_and_mipmap(GrGpu* gpu, GrSurfaceProxy* proxy) {
493 if (!proxy->isInstantiated()) {
494 return;
495 }
496
497 // In the flushSurfaces case, we need to resolve MSAA immediately after flush. This is
498 // because clients expect the flushed surface's backing texture to be fully resolved
499 // upon return.
500 if (proxy->requiresManualMSAAResolve()) {
501 auto* rtProxy = proxy->asRenderTargetProxy();
502 SkASSERT(rtProxy);
503 if (rtProxy->isMSAADirty()) {
504 SkASSERT(rtProxy->peekRenderTarget());
505 gpu->resolveRenderTarget(rtProxy->peekRenderTarget(), rtProxy->msaaDirtyRect(),
506 GrGpu::ForExternalIO::kYes);
507 rtProxy->markMSAAResolved();
508 }
509 }
510 // If, after a flush, any of the proxies of interest have dirty mipmaps, regenerate them in
511 // case their backend textures are being stolen.
512 // (This special case is exercised by the ReimportImageTextureWithMipLevels test.)
513 // FIXME: It may be more ideal to plumb down a "we're going to steal the backends" flag.
514 if (auto* textureProxy = proxy->asTextureProxy()) {
515 if (textureProxy->mipMapsAreDirty()) {
516 SkASSERT(textureProxy->peekTexture());
517 gpu->regenerateMipMapLevels(textureProxy->peekTexture());
518 textureProxy->markMipMapsClean();
519 }
520 }
521}
522
Greg Daniel9efe3862020-06-11 11:51:06 -0400523GrSemaphoresSubmitted GrDrawingManager::flushSurfaces(
524 GrSurfaceProxy* proxies[],
525 int numProxies,
526 SkSurface::BackendSurfaceAccess access,
527 const GrFlushInfo& info,
528 const GrBackendSurfaceMutableState* newState) {
bsalomon6a2b1942016-09-08 11:28:59 -0700529 if (this->wasAbandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -0400530 if (info.fSubmittedProc) {
531 info.fSubmittedProc(info.fSubmittedContext, false);
532 }
533 if (info.fFinishedProc) {
534 info.fFinishedProc(info.fFinishedContext);
535 }
Greg Daniel51316782017-08-02 15:10:09 +0000536 return GrSemaphoresSubmitted::kNo;
bsalomon6a2b1942016-09-08 11:28:59 -0700537 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400538 SkDEBUGCODE(this->validate());
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400539 SkASSERT(numProxies >= 0);
540 SkASSERT(!numProxies || proxies);
bsalomon6a2b1942016-09-08 11:28:59 -0700541
Robert Phillips6a6de562019-02-15 15:19:15 -0500542 auto direct = fContext->priv().asDirectContext();
543 if (!direct) {
Greg Daniel55822f12020-05-26 11:26:45 -0400544 if (info.fSubmittedProc) {
545 info.fSubmittedProc(info.fSubmittedContext, false);
546 }
547 if (info.fFinishedProc) {
548 info.fFinishedProc(info.fFinishedContext);
549 }
Robert Phillips6a6de562019-02-15 15:19:15 -0500550 return GrSemaphoresSubmitted::kNo; // Can't flush while DDL recording
551 }
552
553 GrGpu* gpu = direct->priv().getGpu();
Greg Daniel55822f12020-05-26 11:26:45 -0400554 // We have a non abandoned and direct GrContext. It must have a GrGpu.
555 SkASSERT(gpu);
Robert Phillips874b5352018-03-16 08:48:24 -0400556
Robert Phillipsacc10fa2019-04-01 09:50:20 -0400557 // TODO: It is important to upgrade the drawingmanager to just flushing the
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400558 // portion of the DAG required by 'proxies' in order to restore some of the
Robert Phillipsacc10fa2019-04-01 09:50:20 -0400559 // semantics of this method.
Greg Daniel9efe3862020-06-11 11:51:06 -0400560 bool didFlush = this->flush(proxies, numProxies, access, info, newState);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400561 for (int i = 0; i < numProxies; ++i) {
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400562 resolve_and_mipmap(gpu, proxies[i]);
bsalomon6a2b1942016-09-08 11:28:59 -0700563 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400564
565 SkDEBUGCODE(this->validate());
Greg Danielfe159622020-04-10 17:43:51 +0000566
Greg Daniel04283f32020-05-20 13:16:00 -0400567 if (!didFlush || (!direct->priv().caps()->semaphoreSupport() && info.fNumSemaphores)) {
Greg Danielfe159622020-04-10 17:43:51 +0000568 return GrSemaphoresSubmitted::kNo;
569 }
570 return GrSemaphoresSubmitted::kYes;
bsalomon6a2b1942016-09-08 11:28:59 -0700571}
572
Chris Daltonfe199b72017-05-05 11:26:15 -0400573void GrDrawingManager::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
574 fOnFlushCBObjects.push_back(onFlushCBObject);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400575}
576
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500577#if GR_TEST_UTILS
578void GrDrawingManager::testingOnly_removeOnFlushCallbackObject(GrOnFlushCallbackObject* cb) {
579 int n = std::find(fOnFlushCBObjects.begin(), fOnFlushCBObjects.end(), cb) -
580 fOnFlushCBObjects.begin();
581 SkASSERT(n < fOnFlushCBObjects.count());
582 fOnFlushCBObjects.removeShuffle(n);
583}
584#endif
585
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400586void GrDrawingManager::setLastRenderTask(const GrSurfaceProxy* proxy, GrRenderTask* task) {
587#ifdef SK_DEBUG
588 if (GrRenderTask* prior = this->getLastRenderTask(proxy)) {
589 SkASSERT(prior->isClosed());
590 }
591#endif
Adlai Holler25df1f72020-06-09 13:08:27 -0400592 uint32_t key = proxy->uniqueID().asUInt();
593 if (task) {
594 fLastRenderTasks.set(key, task);
595 } else if (fLastRenderTasks.find(key)) {
596 fLastRenderTasks.remove(key);
597 }
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400598}
599
600GrRenderTask* GrDrawingManager::getLastRenderTask(const GrSurfaceProxy* proxy) const {
601 auto entry = fLastRenderTasks.find(proxy->uniqueID().asUInt());
602 return entry ? *entry : nullptr;
603}
604
605GrOpsTask* GrDrawingManager::getLastOpsTask(const GrSurfaceProxy* proxy) const {
606 GrRenderTask* task = this->getLastRenderTask(proxy);
607 return task ? task->asOpsTask() : nullptr;
608}
609
610
Chris Dalton6b498102019-08-01 14:14:52 -0600611void GrDrawingManager::moveRenderTasksToDDL(SkDeferredDisplayList* ddl) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400612 SkDEBUGCODE(this->validate());
613
Chris Dalton6b498102019-08-01 14:14:52 -0600614 // no renderTask should receive a new command after this
Robert Phillips9da87e02019-02-04 13:26:26 -0500615 fDAG.closeAll(fContext->priv().caps());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400616 fActiveOpsTask = nullptr;
Robert Phillips7a137052018-02-01 11:23:12 -0500617
Chris Dalton6b498102019-08-01 14:14:52 -0600618 fDAG.swap(&ddl->fRenderTasks);
Robert Phillips19f466d2020-02-26 10:27:07 -0500619 SkASSERT(!fDAG.numRenderTasks());
Robert Phillips867ce8f2018-06-21 10:28:36 -0400620
Robert Phillips19f466d2020-02-26 10:27:07 -0500621 for (auto& renderTask : ddl->fRenderTasks) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400622 renderTask->disown(this);
Robert Phillips29f38542019-10-16 09:20:25 -0400623 renderTask->prePrepare(fContext);
Robert Phillips7327c9d2019-10-08 16:32:56 -0400624 }
625
Michael Ludwig2c316bd2019-12-19 14:50:44 -0500626 ddl->fArenas = std::move(fContext->priv().detachArenas());
Robert Phillips61fc7992019-10-22 11:58:17 -0400627
Robert Phillipsf6a0b452020-02-18 14:26:46 -0500628 fContext->priv().detachProgramData(&ddl->fProgramData);
Robert Phillips576b6a12019-12-06 13:05:49 -0500629
Robert Phillips774168e2018-05-31 12:43:27 -0400630 if (fPathRendererChain) {
631 if (auto ccpr = fPathRendererChain->getCoverageCountingPathRenderer()) {
632 ddl->fPendingPaths = ccpr->detachPendingPaths();
633 }
634 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400635
636 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500637}
638
Adlai Holler7580ad42020-06-24 13:45:25 -0400639#ifndef SK_DDL_IS_UNIQUE_POINTER
640void GrDrawingManager::copyRenderTasksFromDDL(sk_sp<const SkDeferredDisplayList> ddl,
641 GrRenderTargetProxy* newDest) {
642#else
Chris Dalton6b498102019-08-01 14:14:52 -0600643void GrDrawingManager::copyRenderTasksFromDDL(const SkDeferredDisplayList* ddl,
Robert Phillips933484f2019-11-26 09:38:55 -0500644 GrRenderTargetProxy* newDest) {
Adlai Holler7580ad42020-06-24 13:45:25 -0400645#endif
Robert Phillips38d64b02018-09-04 13:23:26 -0400646 SkDEBUGCODE(this->validate());
647
Greg Danielf41b2bd2019-08-22 16:19:24 -0400648 if (fActiveOpsTask) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400649 // This is a temporary fix for the partial-MDB world. In that world we're not
Greg Danielf41b2bd2019-08-22 16:19:24 -0400650 // reordering so ops that (in the single opsTask world) would've just glommed onto the
651 // end of the single opsTask but referred to a far earlier RT need to appear in their
652 // own opsTask.
653 fActiveOpsTask->makeClosed(*fContext->priv().caps());
654 fActiveOpsTask = nullptr;
Robert Phillips38d64b02018-09-04 13:23:26 -0400655 }
656
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400657 // Propagate the DDL proxy's state information to the replaying DDL.
658 if (ddl->priv().targetProxy()->isMSAADirty()) {
659 newDest->markMSAADirty(ddl->priv().targetProxy()->msaaDirtyRect(),
660 ddl->characterization().origin());
661 }
662 GrTextureProxy* newTextureProxy = newDest->asTextureProxy();
663 if (newTextureProxy && GrMipMapped::kYes == newTextureProxy->mipMapped()) {
664 newTextureProxy->markMipMapsDirty();
665 }
666
667 this->addDDLTarget(newDest, ddl->priv().targetProxy());
Robert Phillips15c91422019-05-07 16:54:48 -0400668
Robert Phillips62000362018-02-01 09:10:04 -0500669 // Here we jam the proxy that backs the current replay SkSurface into the LazyProxyData.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400670 // The lazy proxy that references it (in the copied opsTasks) will steal its GrTexture.
Robert Phillips62000362018-02-01 09:10:04 -0500671 ddl->fLazyProxyData->fReplayDest = newDest;
Robert Phillips774168e2018-05-31 12:43:27 -0400672
673 if (ddl->fPendingPaths.size()) {
674 GrCoverageCountingPathRenderer* ccpr = this->getCoverageCountingPathRenderer();
675
676 ccpr->mergePendingPaths(ddl->fPendingPaths);
677 }
Robert Phillips22310d62018-09-05 11:07:21 -0400678
Chris Dalton6b498102019-08-01 14:14:52 -0600679 fDAG.add(ddl->fRenderTasks);
Robert Phillips38d64b02018-09-04 13:23:26 -0400680
Adlai Holler6c9bb622020-06-25 09:21:18 -0400681#ifndef SK_DDL_IS_UNIQUE_POINTER
682 // Add a task to unref the DDL after flush.
683 GrRenderTask* unrefTask = fDAG.add(sk_make_sp<GrUnrefDDLTask>(std::move(ddl)));
684 unrefTask->makeClosed(*fContext->priv().caps());
685#endif
Adlai Holler7580ad42020-06-24 13:45:25 -0400686
Robert Phillips38d64b02018-09-04 13:23:26 -0400687 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500688}
689
Robert Phillips38d64b02018-09-04 13:23:26 -0400690#ifdef SK_DEBUG
691void GrDrawingManager::validate() const {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400692 if (fDAG.sortingRenderTasks() && fReduceOpsTaskSplitting) {
693 SkASSERT(!fActiveOpsTask);
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400694 } else {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400695 if (fActiveOpsTask) {
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400696 SkASSERT(!fDAG.empty());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400697 SkASSERT(!fActiveOpsTask->isClosed());
698 SkASSERT(fActiveOpsTask == fDAG.back());
Robert Phillips38d64b02018-09-04 13:23:26 -0400699 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400700
Chris Dalton6b498102019-08-01 14:14:52 -0600701 for (int i = 0; i < fDAG.numRenderTasks(); ++i) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400702 if (fActiveOpsTask != fDAG.renderTask(i)) {
Chris Daltone2a903e2019-09-18 13:41:50 -0600703 // The resolveTask associated with the activeTask remains open for as long as the
704 // activeTask does.
705 bool isActiveResolveTask =
706 fActiveOpsTask && fActiveOpsTask->fTextureResolveTask == fDAG.renderTask(i);
707 SkASSERT(isActiveResolveTask || fDAG.renderTask(i)->isClosed());
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400708 }
709 }
710
711 if (!fDAG.empty() && !fDAG.back()->isClosed()) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400712 SkASSERT(fActiveOpsTask == fDAG.back());
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400713 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400714 }
715}
716#endif
717
Greg Danielbbfec9d2019-08-20 10:56:51 -0400718void GrDrawingManager::closeRenderTasksForNewRenderTask(GrSurfaceProxy* target) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400719 if (target && fDAG.sortingRenderTasks() && fReduceOpsTaskSplitting) {
Chris Dalton5fe99772019-08-06 11:57:39 -0600720 // In this case we need to close all the renderTasks that rely on the current contents of
721 // 'target'. That is bc we're going to update the content of the proxy so they need to be
722 // split in case they use both the old and new content. (This is a bit of an overkill: they
723 // really only need to be split if they ever reference proxy's contents again but that is
724 // hard to predict/handle).
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400725 if (GrRenderTask* lastRenderTask = this->getLastRenderTask(target)) {
Chris Dalton6b498102019-08-01 14:14:52 -0600726 lastRenderTask->closeThoseWhoDependOnMe(*fContext->priv().caps());
Robert Phillips46acf9d2018-10-09 09:31:40 -0400727 }
Greg Danielf41b2bd2019-08-22 16:19:24 -0400728 } else if (fActiveOpsTask) {
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400729 // This is a temporary fix for the partial-MDB world. In that world we're not
Greg Danielf41b2bd2019-08-22 16:19:24 -0400730 // reordering so ops that (in the single opsTask world) would've just glommed onto the
731 // end of the single opsTask but referred to a far earlier RT need to appear in their
732 // own opsTask.
733 fActiveOpsTask->makeClosed(*fContext->priv().caps());
734 fActiveOpsTask = nullptr;
robertphillips3dc6ae52015-10-20 09:54:32 -0700735 }
Chris Dalton5fe99772019-08-06 11:57:39 -0600736}
737
Greg Daniel16f5c652019-10-29 11:26:01 -0400738sk_sp<GrOpsTask> GrDrawingManager::newOpsTask(GrSurfaceProxyView surfaceView,
739 bool managedOpsTask) {
Chris Dalton5fe99772019-08-06 11:57:39 -0600740 SkDEBUGCODE(this->validate());
741 SkASSERT(fContext);
742
Greg Daniel16f5c652019-10-29 11:26:01 -0400743 GrSurfaceProxy* proxy = surfaceView.proxy();
744 this->closeRenderTasksForNewRenderTask(proxy);
robertphillips3dc6ae52015-10-20 09:54:32 -0700745
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400746 sk_sp<GrOpsTask> opsTask(new GrOpsTask(this, fContext->priv().arenas(),
Michael Ludwigd0840ec2019-12-12 09:48:38 -0500747 std::move(surfaceView),
748 fContext->priv().auditTrail()));
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400749 SkASSERT(this->getLastRenderTask(proxy) == opsTask.get());
robertphillips3dc6ae52015-10-20 09:54:32 -0700750
Greg Danielf41b2bd2019-08-22 16:19:24 -0400751 if (managedOpsTask) {
752 fDAG.add(opsTask);
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400753
Greg Danielf41b2bd2019-08-22 16:19:24 -0400754 if (!fDAG.sortingRenderTasks() || !fReduceOpsTaskSplitting) {
755 fActiveOpsTask = opsTask.get();
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400756 }
Robert Phillips941d1442017-06-14 16:37:02 -0400757 }
robertphillips3dc6ae52015-10-20 09:54:32 -0700758
Robert Phillips38d64b02018-09-04 13:23:26 -0400759 SkDEBUGCODE(this->validate());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400760 return opsTask;
robertphillips3dc6ae52015-10-20 09:54:32 -0700761}
762
Chris Daltone2a903e2019-09-18 13:41:50 -0600763GrTextureResolveRenderTask* GrDrawingManager::newTextureResolveRenderTask(const GrCaps& caps) {
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600764 // 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 -0600765 // in sorting and opsTask reduction mode) the render tasks that depend on any proxy's current
Greg Danielf41b2bd2019-08-22 16:19:24 -0400766 // state. This is because those opsTasks can still receive new ops and because if they refer to
Chris Dalton4ece96d2019-08-30 11:26:39 -0600767 // the mipmapped version of 'proxy', they will then come to depend on the render task being
768 // created here.
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600769 //
Greg Danielf41b2bd2019-08-22 16:19:24 -0400770 // Add the new textureResolveTask before the fActiveOpsTask (if not in
771 // sorting/opsTask-splitting-reduction mode) because it will depend upon this resolve task.
Chris Dalton3d770272019-08-14 09:24:37 -0600772 // NOTE: Putting it here will also reduce the amount of work required by the topological sort.
Chris Daltone2a903e2019-09-18 13:41:50 -0600773 return static_cast<GrTextureResolveRenderTask*>(fDAG.addBeforeLast(
774 sk_make_sp<GrTextureResolveRenderTask>()));
Chris Dalton3d770272019-08-14 09:24:37 -0600775}
776
Greg Danielc30f1a92019-09-06 15:28:58 -0400777void GrDrawingManager::newWaitRenderTask(sk_sp<GrSurfaceProxy> proxy,
Greg Daniel301015c2019-11-18 14:06:46 -0500778 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> semaphores,
Greg Danielc30f1a92019-09-06 15:28:58 -0400779 int numSemaphores) {
780 SkDEBUGCODE(this->validate());
781 SkASSERT(fContext);
782
783 const GrCaps& caps = *fContext->priv().caps();
784
Greg Daniel16f5c652019-10-29 11:26:01 -0400785 sk_sp<GrWaitRenderTask> waitTask = sk_make_sp<GrWaitRenderTask>(GrSurfaceProxyView(proxy),
786 std::move(semaphores),
Greg Danielc30f1a92019-09-06 15:28:58 -0400787 numSemaphores);
788 if (fReduceOpsTaskSplitting) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400789 GrRenderTask* lastTask = this->getLastRenderTask(proxy.get());
Greg Danielc30f1a92019-09-06 15:28:58 -0400790 if (lastTask && !lastTask->isClosed()) {
791 // We directly make the currently open renderTask depend on waitTask instead of using
792 // the proxy version of addDependency. The waitTask will never need to trigger any
793 // resolves or mip map generation which is the main advantage of going through the proxy
794 // version. Additionally we would've had to temporarily set the wait task as the
795 // lastRenderTask on the proxy, add the dependency, and then reset the lastRenderTask to
796 // lastTask. Additionally we add all dependencies of lastTask to waitTask so that the
797 // waitTask doesn't get reordered before them and unnecessarily block those tasks.
798 // Note: Any previous Ops already in lastTask will get blocked by the wait semaphore
799 // even though they don't need to be for correctness.
800
801 // Make sure we add the dependencies of lastTask to waitTask first or else we'll get a
802 // circular self dependency of waitTask on waitTask.
803 waitTask->addDependenciesFromOtherTask(lastTask);
804 lastTask->addDependency(waitTask.get());
805 } else {
806 // If there is a last task we set the waitTask to depend on it so that it doesn't get
807 // reordered in front of the lastTask causing the lastTask to be blocked by the
808 // semaphore. Again we directly just go through adding the dependency to the task and
809 // not the proxy since we don't need to worry about resolving anything.
810 if (lastTask) {
811 waitTask->addDependency(lastTask);
812 }
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400813 this->setLastRenderTask(proxy.get(), waitTask.get());
Greg Danielc30f1a92019-09-06 15:28:58 -0400814 }
815 fDAG.add(waitTask);
816 } else {
Adlai Holler33d569e2020-06-16 14:30:08 -0400817 if (fActiveOpsTask && (fActiveOpsTask->target(0).proxy() == proxy.get())) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400818 SkASSERT(this->getLastRenderTask(proxy.get()) == fActiveOpsTask);
Greg Danielc30f1a92019-09-06 15:28:58 -0400819 fDAG.addBeforeLast(waitTask);
820 // In this case we keep the current renderTask open but just insert the new waitTask
821 // before it in the list. The waitTask will never need to trigger any resolves or mip
822 // map generation which is the main advantage of going through the proxy version.
823 // Additionally we would've had to temporarily set the wait task as the lastRenderTask
824 // on the proxy, add the dependency, and then reset the lastRenderTask to
825 // fActiveOpsTask. Additionally we make the waitTask depend on all of fActiveOpsTask
826 // dependencies so that we don't unnecessarily reorder the waitTask before them.
827 // Note: Any previous Ops already in fActiveOpsTask will get blocked by the wait
828 // semaphore even though they don't need to be for correctness.
829
830 // Make sure we add the dependencies of fActiveOpsTask to waitTask first or else we'll
831 // get a circular self dependency of waitTask on waitTask.
832 waitTask->addDependenciesFromOtherTask(fActiveOpsTask);
833 fActiveOpsTask->addDependency(waitTask.get());
834 } else {
835 // In this case we just close the previous RenderTask and start and append the waitTask
836 // to the DAG. Since it is the last task now we call setLastRenderTask on the proxy. If
837 // there is a lastTask on the proxy we make waitTask depend on that task. This
838 // dependency isn't strictly needed but it does keep the DAG from reordering the
839 // waitTask earlier and blocking more tasks.
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400840 if (GrRenderTask* lastTask = this->getLastRenderTask(proxy.get())) {
Greg Danielc30f1a92019-09-06 15:28:58 -0400841 waitTask->addDependency(lastTask);
842 }
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400843 this->setLastRenderTask(proxy.get(), waitTask.get());
Greg Danielc30f1a92019-09-06 15:28:58 -0400844 this->closeRenderTasksForNewRenderTask(proxy.get());
845 fDAG.add(waitTask);
846 }
847 }
848 waitTask->makeClosed(caps);
849
850 SkDEBUGCODE(this->validate());
851}
852
Greg Danielbbfec9d2019-08-20 10:56:51 -0400853void GrDrawingManager::newTransferFromRenderTask(sk_sp<GrSurfaceProxy> srcProxy,
854 const SkIRect& srcRect,
855 GrColorType surfaceColorType,
856 GrColorType dstColorType,
857 sk_sp<GrGpuBuffer> dstBuffer,
858 size_t dstOffset) {
859 SkDEBUGCODE(this->validate());
860 SkASSERT(fContext);
861 // This copies from srcProxy to dstBuffer so it doesn't have a real target.
862 this->closeRenderTasksForNewRenderTask(nullptr);
863
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600864 GrRenderTask* task = fDAG.add(sk_make_sp<GrTransferFromRenderTask>(
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400865 srcProxy, srcRect, surfaceColorType, dstColorType,
866 std::move(dstBuffer), dstOffset));
Greg Danielbbfec9d2019-08-20 10:56:51 -0400867
868 const GrCaps& caps = *fContext->priv().caps();
869
870 // We always say GrMipMapped::kNo here since we are always just copying from the base layer. We
871 // don't need to make sure the whole mip map chain is valid.
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400872 task->addDependency(this, srcProxy.get(), GrMipMapped::kNo,
873 GrTextureResolveManager(this), caps);
Greg Danielbbfec9d2019-08-20 10:56:51 -0400874 task->makeClosed(caps);
875
Greg Danielbbfec9d2019-08-20 10:56:51 -0400876 // We have closed the previous active oplist but since a new oplist isn't being added there
877 // shouldn't be an active one.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400878 SkASSERT(!fActiveOpsTask);
Greg Danielbbfec9d2019-08-20 10:56:51 -0400879 SkDEBUGCODE(this->validate());
880}
881
Greg Daniel16f5c652019-10-29 11:26:01 -0400882bool GrDrawingManager::newCopyRenderTask(GrSurfaceProxyView srcView,
Greg Daniele227fe42019-08-21 13:52:24 -0400883 const SkIRect& srcRect,
Greg Daniel16f5c652019-10-29 11:26:01 -0400884 GrSurfaceProxyView dstView,
Greg Daniele227fe42019-08-21 13:52:24 -0400885 const SkIPoint& dstPoint) {
886 SkDEBUGCODE(this->validate());
887 SkASSERT(fContext);
Greg Daniele227fe42019-08-21 13:52:24 -0400888
Greg Daniel16f5c652019-10-29 11:26:01 -0400889 this->closeRenderTasksForNewRenderTask(dstView.proxy());
Brian Salomone4bce012019-09-20 15:34:23 -0400890 const GrCaps& caps = *fContext->priv().caps();
891
Greg Daniel16f5c652019-10-29 11:26:01 -0400892 GrSurfaceProxy* srcProxy = srcView.proxy();
893
Brian Salomone4bce012019-09-20 15:34:23 -0400894 GrRenderTask* task =
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400895 fDAG.add(GrCopyRenderTask::Make(this, std::move(srcView), srcRect, std::move(dstView),
Greg Daniel16f5c652019-10-29 11:26:01 -0400896 dstPoint, &caps));
Greg Daniele227fe42019-08-21 13:52:24 -0400897 if (!task) {
898 return false;
899 }
900
Greg Daniele227fe42019-08-21 13:52:24 -0400901 // We always say GrMipMapped::kNo here since we are always just copying from the base layer to
902 // another base layer. We don't need to make sure the whole mip map chain is valid.
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400903 task->addDependency(this, srcProxy, GrMipMapped::kNo, GrTextureResolveManager(this), caps);
Greg Daniele227fe42019-08-21 13:52:24 -0400904 task->makeClosed(caps);
905
Greg Daniele227fe42019-08-21 13:52:24 -0400906 // We have closed the previous active oplist but since a new oplist isn't being added there
907 // shouldn't be an active one.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400908 SkASSERT(!fActiveOpsTask);
Greg Daniele227fe42019-08-21 13:52:24 -0400909 SkDEBUGCODE(this->validate());
910 return true;
911}
912
robertphillips68737822015-10-29 12:12:21 -0700913/*
914 * This method finds a path renderer that can draw the specified path on
915 * the provided target.
916 * Due to its expense, the software path renderer has split out so it can
917 * can be individually allowed/disallowed via the "allowSW" boolean.
918 */
919GrPathRenderer* GrDrawingManager::getPathRenderer(const GrPathRenderer::CanDrawPathArgs& args,
920 bool allowSW,
921 GrPathRendererChain::DrawType drawType,
922 GrPathRenderer::StencilSupport* stencilSupport) {
923
924 if (!fPathRendererChain) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400925 fPathRendererChain.reset(new GrPathRendererChain(fContext, fOptionsForPathRendererChain));
robertphillips68737822015-10-29 12:12:21 -0700926 }
927
928 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(args, drawType, stencilSupport);
929 if (!pr && allowSW) {
Brian Salomone7df0bb2018-05-07 14:44:57 -0400930 auto swPR = this->getSoftwarePathRenderer();
931 if (GrPathRenderer::CanDrawPath::kNo != swPR->canDrawPath(args)) {
932 pr = swPR;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500933 }
robertphillips68737822015-10-29 12:12:21 -0700934 }
935
Robert Phillipsd81379d2020-04-21 10:39:02 -0400936#if GR_PATH_RENDERER_SPEW
937 if (pr) {
938 SkDebugf("getPathRenderer: %s\n", pr->name());
939 }
940#endif
941
robertphillips68737822015-10-29 12:12:21 -0700942 return pr;
943}
944
Brian Salomone7df0bb2018-05-07 14:44:57 -0400945GrPathRenderer* GrDrawingManager::getSoftwarePathRenderer() {
946 if (!fSoftwarePathRenderer) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400947 fSoftwarePathRenderer.reset(
Robert Phillips9da87e02019-02-04 13:26:26 -0500948 new GrSoftwarePathRenderer(fContext->priv().proxyProvider(),
Ben Wagner9ec70c62018-07-12 13:30:47 -0400949 fOptionsForPathRendererChain.fAllowPathMaskCaching));
Brian Salomone7df0bb2018-05-07 14:44:57 -0400950 }
Ben Wagner9ec70c62018-07-12 13:30:47 -0400951 return fSoftwarePathRenderer.get();
Brian Salomone7df0bb2018-05-07 14:44:57 -0400952}
953
Chris Daltonfddb6c02017-11-04 15:22:22 -0600954GrCoverageCountingPathRenderer* GrDrawingManager::getCoverageCountingPathRenderer() {
955 if (!fPathRendererChain) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400956 fPathRendererChain.reset(new GrPathRendererChain(fContext, fOptionsForPathRendererChain));
Chris Daltonfddb6c02017-11-04 15:22:22 -0600957 }
958 return fPathRendererChain->getCoverageCountingPathRenderer();
959}
960
Brian Salomon653f42f2018-07-10 10:07:31 -0400961void GrDrawingManager::flushIfNecessary() {
Robert Phillips6a6de562019-02-15 15:19:15 -0500962 auto direct = fContext->priv().asDirectContext();
963 if (!direct) {
964 return;
965 }
966
967 auto resourceCache = direct->priv().getResourceCache();
Brian Salomon653f42f2018-07-10 10:07:31 -0400968 if (resourceCache && resourceCache->requestsFlush()) {
Greg Daniel9efe3862020-06-11 11:51:06 -0400969 if (this->flush(nullptr, 0, SkSurface::BackendSurfaceAccess::kNoAccess, GrFlushInfo(),
970 nullptr)) {
Greg Danielfe159622020-04-10 17:43:51 +0000971 this->submitToGpu(false);
972 }
Brian Salomon57d2beab2018-09-10 09:35:41 -0400973 resourceCache->purgeAsNeeded();
Brian Salomon653f42f2018-07-10 10:07:31 -0400974 }
975}
976