blob: 823f941f4ead04092a1689d804e409c4d34407cd [file] [log] [blame]
robertphillips3dc6ae52015-10-20 09:54:32 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/GrDrawingManager.h"
Robert Phillips69893702019-02-22 11:16:30 -05009
John Stilesfbd050b2020-08-03 13:21:46 -040010#include <memory>
11
Robert Phillips4d5594d2020-02-21 14:24:40 -050012#include "include/core/SkDeferredDisplayList.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/gpu/GrBackendSemaphore.h"
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040014#include "include/gpu/GrDirectContext.h"
15#include "include/gpu/GrRecordingContext.h"
Robert Phillips7eb0a5f2020-06-09 14:15:09 -040016#include "src/core/SkDeferredDisplayListPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/core/SkTTopoSort.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040018#include "src/gpu/GrAuditTrail.h"
Brian Salomon9241a6d2019-10-03 13:26:54 -040019#include "src/gpu/GrClientMappedBufferManager.h"
Greg Daniele227fe42019-08-21 13:52:24 -040020#include "src/gpu/GrCopyRenderTask.h"
Adlai Hollera0693042020-10-14 11:23:11 -040021#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/gpu/GrGpu.h"
23#include "src/gpu/GrMemoryPool.h"
24#include "src/gpu/GrOnFlushResourceProvider.h"
25#include "src/gpu/GrRecordingContextPriv.h"
26#include "src/gpu/GrRenderTargetContext.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040027#include "src/gpu/GrRenderTargetProxy.h"
Chris Dalton6b498102019-08-01 14:14:52 -060028#include "src/gpu/GrRenderTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "src/gpu/GrResourceAllocator.h"
30#include "src/gpu/GrResourceProvider.h"
31#include "src/gpu/GrSoftwarePathRenderer.h"
Greg Daniel46e366a2019-12-16 14:38:36 -050032#include "src/gpu/GrSurfaceContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050033#include "src/gpu/GrSurfaceProxyPriv.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000034#include "src/gpu/GrTexture.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040035#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050036#include "src/gpu/GrTextureProxyPriv.h"
Chris Dalton3d770272019-08-14 09:24:37 -060037#include "src/gpu/GrTextureResolveRenderTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050038#include "src/gpu/GrTracing.h"
Greg Danielbbfec9d2019-08-20 10:56:51 -040039#include "src/gpu/GrTransferFromRenderTask.h"
Adlai Holler6c9bb622020-06-25 09:21:18 -040040#include "src/gpu/GrUnrefDDLTask.h"
Greg Danielc30f1a92019-09-06 15:28:58 -040041#include "src/gpu/GrWaitRenderTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050042#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
Herb Derbya08bde62020-06-12 15:46:06 -040043#include "src/gpu/text/GrSDFTOptions.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050044#include "src/image/SkSurface_Gpu.h"
robertphillips498d7ac2015-10-30 10:11:30 -070045
Chris Dalton6b498102019-08-01 14:14:52 -060046GrDrawingManager::RenderTaskDAG::RenderTaskDAG(bool sortRenderTasks)
47 : fSortRenderTasks(sortRenderTasks) {}
Robert Phillipsa3f70262018-02-08 10:59:38 -050048
Chris Dalton6b498102019-08-01 14:14:52 -060049GrDrawingManager::RenderTaskDAG::~RenderTaskDAG() {}
Robert Phillips22310d62018-09-05 11:07:21 -040050
Chris Dalton6b498102019-08-01 14:14:52 -060051void GrDrawingManager::RenderTaskDAG::gatherIDs(SkSTArray<8, uint32_t, true>* idArray) const {
52 idArray->reset(fRenderTasks.count());
53 for (int i = 0; i < fRenderTasks.count(); ++i) {
54 if (fRenderTasks[i]) {
55 (*idArray)[i] = fRenderTasks[i]->uniqueID();
Robert Phillips22310d62018-09-05 11:07:21 -040056 }
57 }
58}
59
Chris Dalton6b498102019-08-01 14:14:52 -060060void GrDrawingManager::RenderTaskDAG::reset() {
61 fRenderTasks.reset();
Robert Phillips22310d62018-09-05 11:07:21 -040062}
63
Adlai Holler96ead542020-06-26 08:50:14 -040064void GrDrawingManager::RenderTaskDAG::rawRemoveRenderTasks(int startIndex, int stopIndex) {
Robert Phillips22310d62018-09-05 11:07:21 -040065 for (int i = startIndex; i < stopIndex; ++i) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -040066 fRenderTasks[i] = nullptr;
Robert Phillips22310d62018-09-05 11:07:21 -040067 }
68}
69
Chris Dalton6b498102019-08-01 14:14:52 -060070bool GrDrawingManager::RenderTaskDAG::isUsed(GrSurfaceProxy* proxy) const {
Adlai Holler33d569e2020-06-16 14:30:08 -040071 for (const auto& task : fRenderTasks) {
72 if (task && task->isUsed(proxy)) {
Robert Phillips9313aa72019-04-09 18:41:27 -040073 return true;
74 }
75 }
76
77 return false;
78}
79
Chris Dalton3d770272019-08-14 09:24:37 -060080GrRenderTask* GrDrawingManager::RenderTaskDAG::add(sk_sp<GrRenderTask> renderTask) {
Chris Dalton6aeb8e82019-08-27 11:52:19 -060081 if (renderTask) {
82 return fRenderTasks.emplace_back(std::move(renderTask)).get();
83 }
84 return nullptr;
Chris Dalton3d770272019-08-14 09:24:37 -060085}
86
87GrRenderTask* GrDrawingManager::RenderTaskDAG::addBeforeLast(sk_sp<GrRenderTask> renderTask) {
88 SkASSERT(!fRenderTasks.empty());
Chris Dalton6aeb8e82019-08-27 11:52:19 -060089 if (renderTask) {
90 // Release 'fRenderTasks.back()' and grab the raw pointer, in case the SkTArray grows
91 // and reallocates during emplace_back.
92 fRenderTasks.emplace_back(fRenderTasks.back().release());
93 return (fRenderTasks[fRenderTasks.count() - 2] = std::move(renderTask)).get();
94 }
95 return nullptr;
Robert Phillips22310d62018-09-05 11:07:21 -040096}
97
Greg Danielf41b2bd2019-08-22 16:19:24 -040098void GrDrawingManager::RenderTaskDAG::add(const SkTArray<sk_sp<GrRenderTask>>& renderTasks) {
Robert Phillips19f466d2020-02-26 10:27:07 -050099#ifdef SK_DEBUG
100 for (auto& renderTask : renderTasks) {
101 SkASSERT(renderTask->unique());
102 }
103#endif
104
Greg Danielf41b2bd2019-08-22 16:19:24 -0400105 fRenderTasks.push_back_n(renderTasks.count(), renderTasks.begin());
Robert Phillips22310d62018-09-05 11:07:21 -0400106}
107
Greg Danielf41b2bd2019-08-22 16:19:24 -0400108void GrDrawingManager::RenderTaskDAG::swap(SkTArray<sk_sp<GrRenderTask>>* renderTasks) {
109 SkASSERT(renderTasks->empty());
110 renderTasks->swap(fRenderTasks);
Robert Phillips22310d62018-09-05 11:07:21 -0400111}
112
Chris Dalton6b498102019-08-01 14:14:52 -0600113void GrDrawingManager::RenderTaskDAG::prepForFlush() {
114 if (fSortRenderTasks) {
115 SkDEBUGCODE(bool result =) SkTTopoSort<GrRenderTask, GrRenderTask::TopoSortTraits>(
116 &fRenderTasks);
Robert Phillips22310d62018-09-05 11:07:21 -0400117 SkASSERT(result);
118 }
119
120#ifdef SK_DEBUG
Greg Danielf41b2bd2019-08-22 16:19:24 -0400121 // This block checks for any unnecessary splits in the opsTasks. If two sequential opsTasks
122 // share the same backing GrSurfaceProxy it means the opsTask was artificially split.
Chris Dalton6b498102019-08-01 14:14:52 -0600123 if (fRenderTasks.count()) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400124 GrOpsTask* prevOpsTask = fRenderTasks[0]->asOpsTask();
Chris Dalton6b498102019-08-01 14:14:52 -0600125 for (int i = 1; i < fRenderTasks.count(); ++i) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400126 GrOpsTask* curOpsTask = fRenderTasks[i]->asOpsTask();
Robert Phillips22310d62018-09-05 11:07:21 -0400127
Greg Danielf41b2bd2019-08-22 16:19:24 -0400128 if (prevOpsTask && curOpsTask) {
Adlai Holler33d569e2020-06-16 14:30:08 -0400129 SkASSERT(prevOpsTask->target(0).proxy() != curOpsTask->target(0).proxy());
Robert Phillips22310d62018-09-05 11:07:21 -0400130 }
131
Greg Danielf41b2bd2019-08-22 16:19:24 -0400132 prevOpsTask = curOpsTask;
Robert Phillips22310d62018-09-05 11:07:21 -0400133 }
134 }
135#endif
136}
137
Chris Dalton6b498102019-08-01 14:14:52 -0600138void GrDrawingManager::RenderTaskDAG::closeAll(const GrCaps* caps) {
Adlai Holler96ead542020-06-26 08:50:14 -0400139 for (auto& task : fRenderTasks) {
140 if (task) {
141 task->makeClosed(*caps);
Robert Phillips22310d62018-09-05 11:07:21 -0400142 }
143 }
144}
145
Robert Phillips22310d62018-09-05 11:07:21 -0400146///////////////////////////////////////////////////////////////////////////////////////////////////
Robert Phillips69893702019-02-22 11:16:30 -0500147GrDrawingManager::GrDrawingManager(GrRecordingContext* context,
Robert Phillips22310d62018-09-05 11:07:21 -0400148 const GrPathRendererChain::Options& optionsForPathRendererChain,
Chris Dalton6b498102019-08-01 14:14:52 -0600149 bool sortRenderTasks,
Greg Danielf41b2bd2019-08-22 16:19:24 -0400150 bool reduceOpsTaskSplitting)
Robert Phillips22310d62018-09-05 11:07:21 -0400151 : fContext(context)
152 , fOptionsForPathRendererChain(optionsForPathRendererChain)
Chris Dalton6b498102019-08-01 14:14:52 -0600153 , fDAG(sortRenderTasks)
Robert Phillips22310d62018-09-05 11:07:21 -0400154 , fPathRendererChain(nullptr)
155 , fSoftwarePathRenderer(nullptr)
Robert Phillips6db27c22019-05-01 10:43:56 -0400156 , fFlushing(false)
Herb Derby082232b2020-06-10 15:08:18 -0400157 , fReduceOpsTaskSplitting(reduceOpsTaskSplitting) { }
Robert Phillips22310d62018-09-05 11:07:21 -0400158
robertphillips3dc6ae52015-10-20 09:54:32 -0700159GrDrawingManager::~GrDrawingManager() {
Adlai Holler96ead542020-06-26 08:50:14 -0400160 fDAG.closeAll(fContext->priv().caps());
161 this->removeRenderTasks(0, fDAG.numRenderTasks());
robertphillips3dc6ae52015-10-20 09:54:32 -0700162}
163
Robert Phillipsa9162df2019-02-11 14:12:03 -0500164bool GrDrawingManager::wasAbandoned() const {
Robert Phillips9eb00022020-06-30 15:30:12 -0400165 return fContext->abandoned();
robertphillips3dc6ae52015-10-20 09:54:32 -0700166}
167
robertphillips68737822015-10-29 12:12:21 -0700168void GrDrawingManager::freeGpuResources() {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400169 for (int i = fOnFlushCBObjects.count() - 1; i >= 0; --i) {
170 if (!fOnFlushCBObjects[i]->retainOnFreeGpuResources()) {
171 // it's safe to just do this because we're iterating in reverse
172 fOnFlushCBObjects.removeShuffle(i);
173 }
174 }
175
robertphillips68737822015-10-29 12:12:21 -0700176 // a path renderer may be holding onto resources
robertphillips13391dd2015-10-30 05:15:11 -0700177 fPathRendererChain = nullptr;
Ben Wagner9ec70c62018-07-12 13:30:47 -0400178 fSoftwarePathRenderer = nullptr;
Robert Phillipse3302df2017-04-24 07:31:02 -0400179}
180
Robert Phillips7ee385e2017-03-30 08:02:11 -0400181// MDB TODO: make use of the 'proxy' parameter.
Greg Daniel9efe3862020-06-11 11:51:06 -0400182bool GrDrawingManager::flush(
183 GrSurfaceProxy* proxies[],
184 int numProxies,
185 SkSurface::BackendSurfaceAccess access,
186 const GrFlushInfo& info,
187 const GrBackendSurfaceMutableState* newState) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400188 SkASSERT(numProxies >= 0);
189 SkASSERT(!numProxies || proxies);
Brian Salomon57d2beab2018-09-10 09:35:41 -0400190 GR_CREATE_TRACE_MARKER_CONTEXT("GrDrawingManager", "flush", fContext);
Brian Salomondcbb9d92017-07-19 10:53:20 -0400191
robertphillips7761d612016-05-16 09:14:53 -0700192 if (fFlushing || this->wasAbandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -0400193 if (info.fSubmittedProc) {
194 info.fSubmittedProc(info.fSubmittedContext, false);
195 }
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400196 if (info.fFinishedProc) {
197 info.fFinishedProc(info.fFinishedContext);
Greg Daniela3aa75a2019-04-12 14:24:55 -0400198 }
Greg Danielfe159622020-04-10 17:43:51 +0000199 return false;
joshualittb8918c42015-12-18 09:59:46 -0800200 }
Robert Phillips602df412019-04-08 11:10:39 -0400201
Robert Phillips38d64b02018-09-04 13:23:26 -0400202 SkDEBUGCODE(this->validate());
203
Greg Danielce9f0162020-06-30 13:42:46 -0400204 if (!info.fNumSemaphores && !info.fFinishedProc &&
205 access == SkSurface::BackendSurfaceAccess::kNoAccess && !newState) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400206 bool canSkip = numProxies > 0;
207 for (int i = 0; i < numProxies && canSkip; ++i) {
208 canSkip = !fDAG.isUsed(proxies[i]) && !this->isDDLTarget(proxies[i]);
209 }
210 if (canSkip) {
Greg Daniel55822f12020-05-26 11:26:45 -0400211 if (info.fSubmittedProc) {
212 info.fSubmittedProc(info.fSubmittedContext, true);
213 }
Greg Danielfe159622020-04-10 17:43:51 +0000214 return false;
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400215 }
Robert Phillips9313aa72019-04-09 18:41:27 -0400216 }
217
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400218 auto direct = fContext->asDirectContext();
Robert Phillips6a6de562019-02-15 15:19:15 -0500219 if (!direct) {
Greg Daniel55822f12020-05-26 11:26:45 -0400220 if (info.fSubmittedProc) {
221 info.fSubmittedProc(info.fSubmittedContext, false);
222 }
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400223 if (info.fFinishedProc) {
224 info.fFinishedProc(info.fFinishedContext);
Greg Daniela3aa75a2019-04-12 14:24:55 -0400225 }
Greg Danielfe159622020-04-10 17:43:51 +0000226 return false; // Can't flush while DDL recording
Robert Phillips6a6de562019-02-15 15:19:15 -0500227 }
Brian Salomon9241a6d2019-10-03 13:26:54 -0400228 direct->priv().clientMappedBufferManager()->process();
Robert Phillips6a6de562019-02-15 15:19:15 -0500229
230 GrGpu* gpu = direct->priv().getGpu();
Greg Daniel55822f12020-05-26 11:26:45 -0400231 // We have a non abandoned and direct GrContext. It must have a GrGpu.
232 SkASSERT(gpu);
Greg Daniela3aa75a2019-04-12 14:24:55 -0400233
joshualittb8918c42015-12-18 09:59:46 -0800234 fFlushing = true;
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400235
Robert Phillips6a6de562019-02-15 15:19:15 -0500236 auto resourceProvider = direct->priv().resourceProvider();
237 auto resourceCache = direct->priv().getResourceCache();
238
Chris Dalton6b498102019-08-01 14:14:52 -0600239 // Semi-usually the GrRenderTasks are already closed at this point, but sometimes Ganesh needs
Greg Danielf41b2bd2019-08-22 16:19:24 -0400240 // to flush mid-draw. In that case, the SkGpuDevice's opsTasks won't be closed but need to be
241 // flushed anyway. Closing such opsTasks here will mean new ones will be created to replace them
Chris Dalton6b498102019-08-01 14:14:52 -0600242 // if the SkGpuDevice(s) write to them again.
Robert Phillips9da87e02019-02-04 13:26:26 -0500243 fDAG.closeAll(fContext->priv().caps());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400244 fActiveOpsTask = nullptr;
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400245
Robert Phillips22310d62018-09-05 11:07:21 -0400246 fDAG.prepForFlush();
Brian Salomon601ac802019-02-07 13:37:16 -0500247 if (!fCpuBufferCache) {
248 // We cache more buffers when the backend is using client side arrays. Otherwise, we
249 // expect each pool will use a CPU buffer as a staging buffer before uploading to a GPU
250 // buffer object. Each pool only requires one staging buffer at a time.
251 int maxCachedBuffers = fContext->priv().caps()->preferClientSideDynamicBuffers() ? 2 : 6;
252 fCpuBufferCache = GrBufferAllocPool::CpuBufferCache::Make(maxCachedBuffers);
Brian Salomon58f153c2018-10-18 21:51:15 -0400253 }
Robert Phillipsa4c93ac2017-05-18 11:40:04 -0400254
Robert Phillipse5f73282019-06-18 17:15:04 -0400255 GrOpFlushState flushState(gpu, resourceProvider, &fTokenTracker, fCpuBufferCache);
Robert Phillips40a29d72018-01-18 12:59:22 -0500256
Chris Daltonfe199b72017-05-05 11:26:15 -0400257 GrOnFlushResourceProvider onFlushProvider(this);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400258
Chris Dalton12658942017-10-05 19:45:25 -0600259 // Prepare any onFlush op lists (e.g. atlases).
Chris Daltonfe199b72017-05-05 11:26:15 -0400260 if (!fOnFlushCBObjects.empty()) {
Chris Dalton6b498102019-08-01 14:14:52 -0600261 fDAG.gatherIDs(&fFlushingRenderTaskIDs);
Robert Phillips22310d62018-09-05 11:07:21 -0400262
Chris Daltonfe199b72017-05-05 11:26:15 -0400263 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
Chris Dalton6b498102019-08-01 14:14:52 -0600264 onFlushCBObject->preFlush(&onFlushProvider, fFlushingRenderTaskIDs.begin(),
Chris Daltonc4b47352019-08-23 10:10:36 -0600265 fFlushingRenderTaskIDs.count());
266 }
267 for (const auto& onFlushRenderTask : fOnFlushRenderTasks) {
268 onFlushRenderTask->makeClosed(*fContext->priv().caps());
Chris Dalton706a6ff2017-11-29 22:01:06 -0700269#ifdef SK_DEBUG
Chris Daltonc4b47352019-08-23 10:10:36 -0600270 // OnFlush callbacks are invoked during flush, and are therefore expected to handle
271 // resource allocation & usage on their own. (No deferred or lazy proxies!)
272 onFlushRenderTask->visitTargetAndSrcProxies_debugOnly(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400273 [](GrSurfaceProxy* p, GrMipmapped mipMapped) {
Chris Daltonc4b47352019-08-23 10:10:36 -0600274 SkASSERT(!p->asTextureProxy() || !p->asTextureProxy()->texPriv().isDeferred());
Brian Salomonbeb7f522019-08-30 16:19:42 -0400275 SkASSERT(!p->isLazy());
Chris Dalton4ece96d2019-08-30 11:26:39 -0600276 if (p->requiresManualMSAAResolve()) {
277 // The onFlush callback is responsible for ensuring MSAA gets resolved.
278 SkASSERT(p->asRenderTargetProxy() && !p->asRenderTargetProxy()->isMSAADirty());
279 }
Brian Salomon7e67dca2020-07-21 09:27:25 -0400280 if (GrMipmapped::kYes == mipMapped) {
Chris Daltonc4b47352019-08-23 10:10:36 -0600281 // The onFlush callback is responsible for regenerating mips if needed.
Brian Salomon8c82a872020-07-21 12:09:58 -0400282 SkASSERT(p->asTextureProxy() && !p->asTextureProxy()->mipmapsAreDirty());
Chris Daltonc4b47352019-08-23 10:10:36 -0600283 }
284 });
Chris Dalton706a6ff2017-11-29 22:01:06 -0700285#endif
Chris Daltonc4b47352019-08-23 10:10:36 -0600286 onFlushRenderTask->prepare(&flushState);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400287 }
288 }
289
robertphillipsa13e2022015-11-11 12:01:09 -0800290#if 0
Brian Salomon09d994e2016-12-21 11:14:46 -0500291 // Enable this to print out verbose GrOp information
Chris Daltonc4b47352019-08-23 10:10:36 -0600292 SkDEBUGCODE(SkDebugf("onFlush renderTasks:"));
293 for (const auto& onFlushRenderTask : fOnFlushRenderTasks) {
294 SkDEBUGCODE(onFlushRenderTask->dump();)
295 }
296 SkDEBUGCODE(SkDebugf("Normal renderTasks:"));
Chris Dalton6b498102019-08-01 14:14:52 -0600297 for (int i = 0; i < fRenderTasks.count(); ++i) {
298 SkDEBUGCODE(fRenderTasks[i]->dump();)
robertphillips3dc6ae52015-10-20 09:54:32 -0700299 }
robertphillipsa13e2022015-11-11 12:01:09 -0800300#endif
301
Robert Phillipseafd48a2017-11-16 07:52:08 -0500302 int startIndex, stopIndex;
303 bool flushed = false;
304
Robert Phillipsf8e25022017-11-08 15:24:31 -0500305 {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400306 GrResourceAllocator alloc(resourceProvider SkDEBUGCODE(, fDAG.numRenderTasks()));
Chris Dalton6b498102019-08-01 14:14:52 -0600307 for (int i = 0; i < fDAG.numRenderTasks(); ++i) {
308 if (fDAG.renderTask(i)) {
309 fDAG.renderTask(i)->gatherProxyIntervals(&alloc);
Robert Phillips22310d62018-09-05 11:07:21 -0400310 }
Greg Danielf41b2bd2019-08-22 16:19:24 -0400311 alloc.markEndOfOpsTask(i);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500312 }
Robert Phillipsc73666f2019-04-24 08:49:48 -0400313 alloc.determineRecyclability();
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400314
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500315 GrResourceAllocator::AssignError error = GrResourceAllocator::AssignError::kNoError;
Chris Dalton6b498102019-08-01 14:14:52 -0600316 int numRenderTasksExecuted = 0;
Brian Salomon577aa0f2018-11-30 13:32:23 -0500317 while (alloc.assign(&startIndex, &stopIndex, &error)) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500318 if (GrResourceAllocator::AssignError::kFailedProxyInstantiation == error) {
319 for (int i = startIndex; i < stopIndex; ++i) {
Chris Dalton6b498102019-08-01 14:14:52 -0600320 GrRenderTask* renderTask = fDAG.renderTask(i);
321 if (!renderTask) {
322 continue;
Robert Phillips01a91282018-07-26 08:03:04 -0400323 }
Chris Dalton6b498102019-08-01 14:14:52 -0600324 if (!renderTask->isInstantiated()) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400325 // No need to call the renderTask's handleInternalAllocationFailure
326 // since we will already skip executing the renderTask since it is not
327 // instantiated.
Chris Dalton6b498102019-08-01 14:14:52 -0600328 continue;
Robert Phillipsbbcb7f72018-05-31 11:16:19 -0400329 }
Chris Dalton6b498102019-08-01 14:14:52 -0600330 renderTask->handleInternalAllocationFailure();
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500331 }
Adlai Holler96ead542020-06-26 08:50:14 -0400332 this->removeRenderTasks(startIndex, stopIndex);
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500333 }
Robert Phillips4150eea2018-02-07 17:08:21 -0500334
Chris Dalton6b498102019-08-01 14:14:52 -0600335 if (this->executeRenderTasks(
336 startIndex, stopIndex, &flushState, &numRenderTasksExecuted)) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500337 flushed = true;
338 }
bsalomondc438982016-08-31 11:53:49 -0700339 }
Greg Danielc42b20b2017-10-04 10:34:49 -0400340 }
341
Chris Dalton91ab1552018-04-18 13:24:25 -0600342#ifdef SK_DEBUG
Chris Dalton6b498102019-08-01 14:14:52 -0600343 for (int i = 0; i < fDAG.numRenderTasks(); ++i) {
Adlai Holler96ead542020-06-26 08:50:14 -0400344 // All render tasks should have been cleared out by now – we only reset the array below to
345 // reclaim storage.
346 SkASSERT(!fDAG.renderTask(i));
Chris Dalton91ab1552018-04-18 13:24:25 -0600347 }
348#endif
Adlai Holler25df1f72020-06-09 13:08:27 -0400349 fLastRenderTasks.reset();
Robert Phillips22310d62018-09-05 11:07:21 -0400350 fDAG.reset();
Robert Phillips15c91422019-05-07 16:54:48 -0400351 this->clearDDLTargets();
robertphillipsa13e2022015-11-11 12:01:09 -0800352
Robert Phillipsc994a932018-06-19 13:09:54 -0400353#ifdef SK_DEBUG
354 // In non-DDL mode this checks that all the flushed ops have been freed from the memory pool.
355 // When we move to partial flushes this assert will no longer be valid.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400356 // In DDL mode this check is somewhat superfluous since the memory for most of the ops/opsTasks
Robert Phillipsc994a932018-06-19 13:09:54 -0400357 // will be stored in the DDL's GrOpMemoryPools.
Robert Phillips9da87e02019-02-04 13:26:26 -0500358 GrOpMemoryPool* opMemoryPool = fContext->priv().opMemoryPool();
Robert Phillipsc994a932018-06-19 13:09:54 -0400359 opMemoryPool->isEmpty();
360#endif
361
Greg Daniel9efe3862020-06-11 11:51:06 -0400362 gpu->executeFlushInfo(proxies, numProxies, access, info, newState);
robertphillipsa13e2022015-11-11 12:01:09 -0800363
Brian Salomon57d2beab2018-09-10 09:35:41 -0400364 // Give the cache a chance to purge resources that become purgeable due to flushing.
365 if (flushed) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500366 resourceCache->purgeAsNeeded();
Brian Salomon876a0172019-03-08 11:12:14 -0500367 flushed = false;
bsalomonb77a9072016-09-07 10:02:04 -0700368 }
Chris Daltonfe199b72017-05-05 11:26:15 -0400369 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
Chris Dalton6b498102019-08-01 14:14:52 -0600370 onFlushCBObject->postFlush(fTokenTracker.nextTokenToFlush(), fFlushingRenderTaskIDs.begin(),
371 fFlushingRenderTaskIDs.count());
Brian Salomon876a0172019-03-08 11:12:14 -0500372 flushed = true;
373 }
374 if (flushed) {
375 resourceCache->purgeAsNeeded();
Chris Daltonfe199b72017-05-05 11:26:15 -0400376 }
Chris Dalton6b498102019-08-01 14:14:52 -0600377 fFlushingRenderTaskIDs.reset();
joshualittb8918c42015-12-18 09:59:46 -0800378 fFlushing = false;
Greg Daniel51316782017-08-02 15:10:09 +0000379
Greg Danielfe159622020-04-10 17:43:51 +0000380 return true;
381}
382
383bool GrDrawingManager::submitToGpu(bool syncToCpu) {
384 if (fFlushing || this->wasAbandoned()) {
385 return false;
386 }
387
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400388 auto direct = fContext->asDirectContext();
Greg Danielfe159622020-04-10 17:43:51 +0000389 if (!direct) {
390 return false; // Can't submit while DDL recording
391 }
392 GrGpu* gpu = direct->priv().getGpu();
393 return gpu->submitToGpu(syncToCpu);
robertphillips3dc6ae52015-10-20 09:54:32 -0700394}
395
Chris Dalton6b498102019-08-01 14:14:52 -0600396bool GrDrawingManager::executeRenderTasks(int startIndex, int stopIndex, GrOpFlushState* flushState,
397 int* numRenderTasksExecuted) {
398 SkASSERT(startIndex <= stopIndex && stopIndex <= fDAG.numRenderTasks());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500399
Robert Phillips27483912018-04-20 12:43:18 -0400400#if GR_FLUSH_TIME_OP_SPEW
Greg Danielf41b2bd2019-08-22 16:19:24 -0400401 SkDebugf("Flushing opsTask: %d to %d out of [%d, %d]\n",
Chris Dalton6b498102019-08-01 14:14:52 -0600402 startIndex, stopIndex, 0, fDAG.numRenderTasks());
Robert Phillips27483912018-04-20 12:43:18 -0400403 for (int i = startIndex; i < stopIndex; ++i) {
Chris Dalton6b498102019-08-01 14:14:52 -0600404 if (fDAG.renderTask(i)) {
405 fDAG.renderTask(i)->dump(true);
Robert Phillips1734dd32018-08-21 13:52:09 -0400406 }
Robert Phillips27483912018-04-20 12:43:18 -0400407 }
408#endif
409
Chris Dalton6b498102019-08-01 14:14:52 -0600410 bool anyRenderTasksExecuted = false;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500411
412 for (int i = startIndex; i < stopIndex; ++i) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400413 GrRenderTask* renderTask = fDAG.renderTask(i);
414 if (!renderTask || !renderTask->isInstantiated()) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500415 continue;
416 }
417
Chris Dalton6b498102019-08-01 14:14:52 -0600418 SkASSERT(renderTask->deferredProxiesAreInstantiated());
Robert Phillips22310d62018-09-05 11:07:21 -0400419
Chris Dalton6b498102019-08-01 14:14:52 -0600420 renderTask->prepare(flushState);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500421 }
422
423 // Upload all data to the GPU
424 flushState->preExecuteDraws();
425
Greg Danield2073452018-12-07 11:20:33 -0500426 // For Vulkan, if we have too many oplists to be flushed we end up allocating a lot of resources
427 // for each command buffer associated with the oplists. If this gets too large we can cause the
428 // devices to go OOM. In practice we usually only hit this case in our tests, but to be safe we
429 // put a cap on the number of oplists we will execute before flushing to the GPU to relieve some
430 // memory pressure.
Chris Dalton6b498102019-08-01 14:14:52 -0600431 static constexpr int kMaxRenderTasksBeforeFlush = 100;
Greg Danield2073452018-12-07 11:20:33 -0500432
Chris Daltonc4b47352019-08-23 10:10:36 -0600433 // Execute the onFlush renderTasks first, if any.
434 for (sk_sp<GrRenderTask>& onFlushRenderTask : fOnFlushRenderTasks) {
435 if (!onFlushRenderTask->execute(flushState)) {
436 SkDebugf("WARNING: onFlushRenderTask failed to execute.\n");
Robert Phillipseafd48a2017-11-16 07:52:08 -0500437 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600438 SkASSERT(onFlushRenderTask->unique());
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400439 onFlushRenderTask->disown(this);
Chris Daltonc4b47352019-08-23 10:10:36 -0600440 onFlushRenderTask = nullptr;
Chris Dalton6b498102019-08-01 14:14:52 -0600441 (*numRenderTasksExecuted)++;
442 if (*numRenderTasksExecuted >= kMaxRenderTasksBeforeFlush) {
Greg Danielfe159622020-04-10 17:43:51 +0000443 flushState->gpu()->submitToGpu(false);
Chris Dalton6b498102019-08-01 14:14:52 -0600444 *numRenderTasksExecuted = 0;
Greg Danield2073452018-12-07 11:20:33 -0500445 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500446 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600447 fOnFlushRenderTasks.reset();
Robert Phillipseafd48a2017-11-16 07:52:08 -0500448
449 // Execute the normal op lists.
450 for (int i = startIndex; i < stopIndex; ++i) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400451 GrRenderTask* renderTask = fDAG.renderTask(i);
452 if (!renderTask || !renderTask->isInstantiated()) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500453 continue;
454 }
455
Greg Daniel15ecdf92019-08-30 15:35:23 -0400456 if (renderTask->execute(flushState)) {
Chris Dalton6b498102019-08-01 14:14:52 -0600457 anyRenderTasksExecuted = true;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500458 }
Chris Dalton6b498102019-08-01 14:14:52 -0600459 (*numRenderTasksExecuted)++;
460 if (*numRenderTasksExecuted >= kMaxRenderTasksBeforeFlush) {
Greg Danielfe159622020-04-10 17:43:51 +0000461 flushState->gpu()->submitToGpu(false);
Chris Dalton6b498102019-08-01 14:14:52 -0600462 *numRenderTasksExecuted = 0;
Greg Danield2073452018-12-07 11:20:33 -0500463 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500464 }
465
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400466 SkASSERT(!flushState->opsRenderPass());
Robert Phillips40a29d72018-01-18 12:59:22 -0500467 SkASSERT(fTokenTracker.nextDrawToken() == fTokenTracker.nextTokenToFlush());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500468
Chris Dalton6b498102019-08-01 14:14:52 -0600469 // We reset the flush state before the RenderTasks so that the last resources to be freed are
470 // those that are written to in the RenderTasks. This helps to make sure the most recently used
471 // resources are the last to be purged by the resource cache.
Robert Phillipseafd48a2017-11-16 07:52:08 -0500472 flushState->reset();
473
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400474 this->removeRenderTasks(startIndex, stopIndex);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500475
Chris Dalton6b498102019-08-01 14:14:52 -0600476 return anyRenderTasksExecuted;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500477}
478
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400479void GrDrawingManager::removeRenderTasks(int startIndex, int stopIndex) {
480 for (int i = startIndex; i < stopIndex; ++i) {
481 GrRenderTask* task = fDAG.renderTask(i);
482 if (!task) {
483 continue;
484 }
485 if (!task->unique()) {
486 // TODO: Eventually this should be guaranteed unique: http://skbug.com/7111
487 task->endFlush(this);
488 }
489 task->disown(this);
490 }
Adlai Holler96ead542020-06-26 08:50:14 -0400491 fDAG.rawRemoveRenderTasks(startIndex, stopIndex);
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400492}
493
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400494static void resolve_and_mipmap(GrGpu* gpu, GrSurfaceProxy* proxy) {
495 if (!proxy->isInstantiated()) {
496 return;
497 }
498
499 // In the flushSurfaces case, we need to resolve MSAA immediately after flush. This is
500 // because clients expect the flushed surface's backing texture to be fully resolved
501 // upon return.
502 if (proxy->requiresManualMSAAResolve()) {
503 auto* rtProxy = proxy->asRenderTargetProxy();
504 SkASSERT(rtProxy);
505 if (rtProxy->isMSAADirty()) {
506 SkASSERT(rtProxy->peekRenderTarget());
Jim Van Verthbb61fe32020-07-07 16:39:04 -0400507 gpu->resolveRenderTarget(rtProxy->peekRenderTarget(), rtProxy->msaaDirtyRect());
508 gpu->submitToGpu(false);
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400509 rtProxy->markMSAAResolved();
510 }
511 }
512 // If, after a flush, any of the proxies of interest have dirty mipmaps, regenerate them in
513 // case their backend textures are being stolen.
514 // (This special case is exercised by the ReimportImageTextureWithMipLevels test.)
515 // FIXME: It may be more ideal to plumb down a "we're going to steal the backends" flag.
516 if (auto* textureProxy = proxy->asTextureProxy()) {
Brian Salomon8c82a872020-07-21 12:09:58 -0400517 if (textureProxy->mipmapsAreDirty()) {
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400518 SkASSERT(textureProxy->peekTexture());
519 gpu->regenerateMipMapLevels(textureProxy->peekTexture());
Brian Salomon8c82a872020-07-21 12:09:58 -0400520 textureProxy->markMipmapsClean();
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400521 }
522 }
523}
524
Greg Daniel9efe3862020-06-11 11:51:06 -0400525GrSemaphoresSubmitted GrDrawingManager::flushSurfaces(
526 GrSurfaceProxy* proxies[],
527 int numProxies,
528 SkSurface::BackendSurfaceAccess access,
529 const GrFlushInfo& info,
530 const GrBackendSurfaceMutableState* newState) {
bsalomon6a2b1942016-09-08 11:28:59 -0700531 if (this->wasAbandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -0400532 if (info.fSubmittedProc) {
533 info.fSubmittedProc(info.fSubmittedContext, false);
534 }
535 if (info.fFinishedProc) {
536 info.fFinishedProc(info.fFinishedContext);
537 }
Greg Daniel51316782017-08-02 15:10:09 +0000538 return GrSemaphoresSubmitted::kNo;
bsalomon6a2b1942016-09-08 11:28:59 -0700539 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400540 SkDEBUGCODE(this->validate());
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400541 SkASSERT(numProxies >= 0);
542 SkASSERT(!numProxies || proxies);
bsalomon6a2b1942016-09-08 11:28:59 -0700543
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400544 auto direct = fContext->asDirectContext();
Robert Phillips6a6de562019-02-15 15:19:15 -0500545 if (!direct) {
Greg Daniel55822f12020-05-26 11:26:45 -0400546 if (info.fSubmittedProc) {
547 info.fSubmittedProc(info.fSubmittedContext, false);
548 }
549 if (info.fFinishedProc) {
550 info.fFinishedProc(info.fFinishedContext);
551 }
Robert Phillips6a6de562019-02-15 15:19:15 -0500552 return GrSemaphoresSubmitted::kNo; // Can't flush while DDL recording
553 }
554
555 GrGpu* gpu = direct->priv().getGpu();
Greg Daniel55822f12020-05-26 11:26:45 -0400556 // We have a non abandoned and direct GrContext. It must have a GrGpu.
557 SkASSERT(gpu);
Robert Phillips874b5352018-03-16 08:48:24 -0400558
Robert Phillipsacc10fa2019-04-01 09:50:20 -0400559 // TODO: It is important to upgrade the drawingmanager to just flushing the
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400560 // portion of the DAG required by 'proxies' in order to restore some of the
Robert Phillipsacc10fa2019-04-01 09:50:20 -0400561 // semantics of this method.
Greg Daniel9efe3862020-06-11 11:51:06 -0400562 bool didFlush = this->flush(proxies, numProxies, access, info, newState);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400563 for (int i = 0; i < numProxies; ++i) {
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400564 resolve_and_mipmap(gpu, proxies[i]);
bsalomon6a2b1942016-09-08 11:28:59 -0700565 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400566
567 SkDEBUGCODE(this->validate());
Greg Danielfe159622020-04-10 17:43:51 +0000568
Greg Daniel04283f32020-05-20 13:16:00 -0400569 if (!didFlush || (!direct->priv().caps()->semaphoreSupport() && info.fNumSemaphores)) {
Greg Danielfe159622020-04-10 17:43:51 +0000570 return GrSemaphoresSubmitted::kNo;
571 }
572 return GrSemaphoresSubmitted::kYes;
bsalomon6a2b1942016-09-08 11:28:59 -0700573}
574
Chris Daltonfe199b72017-05-05 11:26:15 -0400575void GrDrawingManager::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
576 fOnFlushCBObjects.push_back(onFlushCBObject);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400577}
578
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500579#if GR_TEST_UTILS
580void GrDrawingManager::testingOnly_removeOnFlushCallbackObject(GrOnFlushCallbackObject* cb) {
581 int n = std::find(fOnFlushCBObjects.begin(), fOnFlushCBObjects.end(), cb) -
582 fOnFlushCBObjects.begin();
583 SkASSERT(n < fOnFlushCBObjects.count());
584 fOnFlushCBObjects.removeShuffle(n);
585}
586#endif
587
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400588void GrDrawingManager::setLastRenderTask(const GrSurfaceProxy* proxy, GrRenderTask* task) {
589#ifdef SK_DEBUG
590 if (GrRenderTask* prior = this->getLastRenderTask(proxy)) {
591 SkASSERT(prior->isClosed());
592 }
593#endif
Adlai Holler25df1f72020-06-09 13:08:27 -0400594 uint32_t key = proxy->uniqueID().asUInt();
595 if (task) {
596 fLastRenderTasks.set(key, task);
597 } else if (fLastRenderTasks.find(key)) {
598 fLastRenderTasks.remove(key);
599 }
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400600}
601
602GrRenderTask* GrDrawingManager::getLastRenderTask(const GrSurfaceProxy* proxy) const {
603 auto entry = fLastRenderTasks.find(proxy->uniqueID().asUInt());
604 return entry ? *entry : nullptr;
605}
606
607GrOpsTask* GrDrawingManager::getLastOpsTask(const GrSurfaceProxy* proxy) const {
608 GrRenderTask* task = this->getLastRenderTask(proxy);
609 return task ? task->asOpsTask() : nullptr;
610}
611
612
Chris Dalton6b498102019-08-01 14:14:52 -0600613void GrDrawingManager::moveRenderTasksToDDL(SkDeferredDisplayList* ddl) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400614 SkDEBUGCODE(this->validate());
615
Chris Dalton6b498102019-08-01 14:14:52 -0600616 // no renderTask should receive a new command after this
Robert Phillips9da87e02019-02-04 13:26:26 -0500617 fDAG.closeAll(fContext->priv().caps());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400618 fActiveOpsTask = nullptr;
Robert Phillips7a137052018-02-01 11:23:12 -0500619
Chris Dalton6b498102019-08-01 14:14:52 -0600620 fDAG.swap(&ddl->fRenderTasks);
Robert Phillips19f466d2020-02-26 10:27:07 -0500621 SkASSERT(!fDAG.numRenderTasks());
Robert Phillips867ce8f2018-06-21 10:28:36 -0400622
Robert Phillips19f466d2020-02-26 10:27:07 -0500623 for (auto& renderTask : ddl->fRenderTasks) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400624 renderTask->disown(this);
Robert Phillips29f38542019-10-16 09:20:25 -0400625 renderTask->prePrepare(fContext);
Robert Phillips7327c9d2019-10-08 16:32:56 -0400626 }
627
Michael Ludwig2c316bd2019-12-19 14:50:44 -0500628 ddl->fArenas = std::move(fContext->priv().detachArenas());
Robert Phillips61fc7992019-10-22 11:58:17 -0400629
Robert Phillipsf6a0b452020-02-18 14:26:46 -0500630 fContext->priv().detachProgramData(&ddl->fProgramData);
Robert Phillips576b6a12019-12-06 13:05:49 -0500631
Robert Phillips774168e2018-05-31 12:43:27 -0400632 if (fPathRendererChain) {
633 if (auto ccpr = fPathRendererChain->getCoverageCountingPathRenderer()) {
634 ddl->fPendingPaths = ccpr->detachPendingPaths();
635 }
636 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400637
638 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500639}
640
Adlai Holler7580ad42020-06-24 13:45:25 -0400641void GrDrawingManager::copyRenderTasksFromDDL(sk_sp<const SkDeferredDisplayList> ddl,
642 GrRenderTargetProxy* newDest) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400643 SkDEBUGCODE(this->validate());
644
Greg Danielf41b2bd2019-08-22 16:19:24 -0400645 if (fActiveOpsTask) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400646 // This is a temporary fix for the partial-MDB world. In that world we're not
Greg Danielf41b2bd2019-08-22 16:19:24 -0400647 // reordering so ops that (in the single opsTask world) would've just glommed onto the
648 // end of the single opsTask but referred to a far earlier RT need to appear in their
649 // own opsTask.
650 fActiveOpsTask->makeClosed(*fContext->priv().caps());
651 fActiveOpsTask = nullptr;
Robert Phillips38d64b02018-09-04 13:23:26 -0400652 }
653
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400654 // Propagate the DDL proxy's state information to the replaying DDL.
655 if (ddl->priv().targetProxy()->isMSAADirty()) {
656 newDest->markMSAADirty(ddl->priv().targetProxy()->msaaDirtyRect(),
657 ddl->characterization().origin());
658 }
659 GrTextureProxy* newTextureProxy = newDest->asTextureProxy();
Brian Salomon8c82a872020-07-21 12:09:58 -0400660 if (newTextureProxy && GrMipmapped::kYes == newTextureProxy->mipmapped()) {
661 newTextureProxy->markMipmapsDirty();
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400662 }
663
664 this->addDDLTarget(newDest, ddl->priv().targetProxy());
Robert Phillips15c91422019-05-07 16:54:48 -0400665
Robert Phillips62000362018-02-01 09:10:04 -0500666 // Here we jam the proxy that backs the current replay SkSurface into the LazyProxyData.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400667 // The lazy proxy that references it (in the copied opsTasks) will steal its GrTexture.
Robert Phillips62000362018-02-01 09:10:04 -0500668 ddl->fLazyProxyData->fReplayDest = newDest;
Robert Phillips774168e2018-05-31 12:43:27 -0400669
670 if (ddl->fPendingPaths.size()) {
671 GrCoverageCountingPathRenderer* ccpr = this->getCoverageCountingPathRenderer();
672
673 ccpr->mergePendingPaths(ddl->fPendingPaths);
674 }
Robert Phillips22310d62018-09-05 11:07:21 -0400675
Chris Dalton6b498102019-08-01 14:14:52 -0600676 fDAG.add(ddl->fRenderTasks);
Robert Phillips38d64b02018-09-04 13:23:26 -0400677
Adlai Holler6c9bb622020-06-25 09:21:18 -0400678 // Add a task to unref the DDL after flush.
679 GrRenderTask* unrefTask = fDAG.add(sk_make_sp<GrUnrefDDLTask>(std::move(ddl)));
680 unrefTask->makeClosed(*fContext->priv().caps());
Adlai Holler7580ad42020-06-24 13:45:25 -0400681
Robert Phillips38d64b02018-09-04 13:23:26 -0400682 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500683}
684
Robert Phillips38d64b02018-09-04 13:23:26 -0400685#ifdef SK_DEBUG
686void GrDrawingManager::validate() const {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400687 if (fDAG.sortingRenderTasks() && fReduceOpsTaskSplitting) {
688 SkASSERT(!fActiveOpsTask);
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400689 } else {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400690 if (fActiveOpsTask) {
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400691 SkASSERT(!fDAG.empty());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400692 SkASSERT(!fActiveOpsTask->isClosed());
693 SkASSERT(fActiveOpsTask == fDAG.back());
Robert Phillips38d64b02018-09-04 13:23:26 -0400694 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400695
Chris Dalton6b498102019-08-01 14:14:52 -0600696 for (int i = 0; i < fDAG.numRenderTasks(); ++i) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400697 if (fActiveOpsTask != fDAG.renderTask(i)) {
Chris Daltone2a903e2019-09-18 13:41:50 -0600698 // The resolveTask associated with the activeTask remains open for as long as the
699 // activeTask does.
700 bool isActiveResolveTask =
701 fActiveOpsTask && fActiveOpsTask->fTextureResolveTask == fDAG.renderTask(i);
702 SkASSERT(isActiveResolveTask || fDAG.renderTask(i)->isClosed());
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400703 }
704 }
705
706 if (!fDAG.empty() && !fDAG.back()->isClosed()) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400707 SkASSERT(fActiveOpsTask == fDAG.back());
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400708 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400709 }
710}
711#endif
712
Greg Danielbbfec9d2019-08-20 10:56:51 -0400713void GrDrawingManager::closeRenderTasksForNewRenderTask(GrSurfaceProxy* target) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400714 if (target && fDAG.sortingRenderTasks() && fReduceOpsTaskSplitting) {
Chris Dalton5fe99772019-08-06 11:57:39 -0600715 // In this case we need to close all the renderTasks that rely on the current contents of
716 // 'target'. That is bc we're going to update the content of the proxy so they need to be
717 // split in case they use both the old and new content. (This is a bit of an overkill: they
718 // really only need to be split if they ever reference proxy's contents again but that is
719 // hard to predict/handle).
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400720 if (GrRenderTask* lastRenderTask = this->getLastRenderTask(target)) {
Chris Dalton6b498102019-08-01 14:14:52 -0600721 lastRenderTask->closeThoseWhoDependOnMe(*fContext->priv().caps());
Robert Phillips46acf9d2018-10-09 09:31:40 -0400722 }
Greg Danielf41b2bd2019-08-22 16:19:24 -0400723 } else if (fActiveOpsTask) {
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400724 // This is a temporary fix for the partial-MDB world. In that world we're not
Greg Danielf41b2bd2019-08-22 16:19:24 -0400725 // reordering so ops that (in the single opsTask world) would've just glommed onto the
726 // end of the single opsTask but referred to a far earlier RT need to appear in their
727 // own opsTask.
728 fActiveOpsTask->makeClosed(*fContext->priv().caps());
729 fActiveOpsTask = nullptr;
robertphillips3dc6ae52015-10-20 09:54:32 -0700730 }
Chris Dalton5fe99772019-08-06 11:57:39 -0600731}
732
Greg Daniel16f5c652019-10-29 11:26:01 -0400733sk_sp<GrOpsTask> GrDrawingManager::newOpsTask(GrSurfaceProxyView surfaceView,
734 bool managedOpsTask) {
Chris Dalton5fe99772019-08-06 11:57:39 -0600735 SkDEBUGCODE(this->validate());
736 SkASSERT(fContext);
737
Greg Daniel16f5c652019-10-29 11:26:01 -0400738 GrSurfaceProxy* proxy = surfaceView.proxy();
739 this->closeRenderTasksForNewRenderTask(proxy);
robertphillips3dc6ae52015-10-20 09:54:32 -0700740
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400741 sk_sp<GrOpsTask> opsTask(new GrOpsTask(this, fContext->priv().arenas(),
Michael Ludwigd0840ec2019-12-12 09:48:38 -0500742 std::move(surfaceView),
743 fContext->priv().auditTrail()));
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400744 SkASSERT(this->getLastRenderTask(proxy) == opsTask.get());
robertphillips3dc6ae52015-10-20 09:54:32 -0700745
Greg Danielf41b2bd2019-08-22 16:19:24 -0400746 if (managedOpsTask) {
747 fDAG.add(opsTask);
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400748
Greg Danielf41b2bd2019-08-22 16:19:24 -0400749 if (!fDAG.sortingRenderTasks() || !fReduceOpsTaskSplitting) {
750 fActiveOpsTask = opsTask.get();
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400751 }
Robert Phillips941d1442017-06-14 16:37:02 -0400752 }
robertphillips3dc6ae52015-10-20 09:54:32 -0700753
Robert Phillips38d64b02018-09-04 13:23:26 -0400754 SkDEBUGCODE(this->validate());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400755 return opsTask;
robertphillips3dc6ae52015-10-20 09:54:32 -0700756}
757
Chris Daltone2a903e2019-09-18 13:41:50 -0600758GrTextureResolveRenderTask* GrDrawingManager::newTextureResolveRenderTask(const GrCaps& caps) {
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600759 // 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 -0600760 // in sorting and opsTask reduction mode) the render tasks that depend on any proxy's current
Greg Danielf41b2bd2019-08-22 16:19:24 -0400761 // state. This is because those opsTasks can still receive new ops and because if they refer to
Chris Dalton4ece96d2019-08-30 11:26:39 -0600762 // the mipmapped version of 'proxy', they will then come to depend on the render task being
763 // created here.
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600764 //
Greg Danielf41b2bd2019-08-22 16:19:24 -0400765 // Add the new textureResolveTask before the fActiveOpsTask (if not in
766 // sorting/opsTask-splitting-reduction mode) because it will depend upon this resolve task.
Chris Dalton3d770272019-08-14 09:24:37 -0600767 // NOTE: Putting it here will also reduce the amount of work required by the topological sort.
Chris Daltone2a903e2019-09-18 13:41:50 -0600768 return static_cast<GrTextureResolveRenderTask*>(fDAG.addBeforeLast(
769 sk_make_sp<GrTextureResolveRenderTask>()));
Chris Dalton3d770272019-08-14 09:24:37 -0600770}
771
Greg Danielc30f1a92019-09-06 15:28:58 -0400772void GrDrawingManager::newWaitRenderTask(sk_sp<GrSurfaceProxy> proxy,
Greg Daniel301015c2019-11-18 14:06:46 -0500773 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> semaphores,
Greg Danielc30f1a92019-09-06 15:28:58 -0400774 int numSemaphores) {
775 SkDEBUGCODE(this->validate());
776 SkASSERT(fContext);
777
778 const GrCaps& caps = *fContext->priv().caps();
779
Greg Daniel16f5c652019-10-29 11:26:01 -0400780 sk_sp<GrWaitRenderTask> waitTask = sk_make_sp<GrWaitRenderTask>(GrSurfaceProxyView(proxy),
781 std::move(semaphores),
Greg Danielc30f1a92019-09-06 15:28:58 -0400782 numSemaphores);
783 if (fReduceOpsTaskSplitting) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400784 GrRenderTask* lastTask = this->getLastRenderTask(proxy.get());
Greg Danielc30f1a92019-09-06 15:28:58 -0400785 if (lastTask && !lastTask->isClosed()) {
786 // We directly make the currently open renderTask depend on waitTask instead of using
787 // the proxy version of addDependency. The waitTask will never need to trigger any
788 // resolves or mip map generation which is the main advantage of going through the proxy
789 // version. Additionally we would've had to temporarily set the wait task as the
790 // lastRenderTask on the proxy, add the dependency, and then reset the lastRenderTask to
791 // lastTask. Additionally we add all dependencies of lastTask to waitTask so that the
792 // waitTask doesn't get reordered before them and unnecessarily block those tasks.
793 // Note: Any previous Ops already in lastTask will get blocked by the wait semaphore
794 // even though they don't need to be for correctness.
795
796 // Make sure we add the dependencies of lastTask to waitTask first or else we'll get a
797 // circular self dependency of waitTask on waitTask.
798 waitTask->addDependenciesFromOtherTask(lastTask);
799 lastTask->addDependency(waitTask.get());
800 } else {
801 // If there is a last task we set the waitTask to depend on it so that it doesn't get
802 // reordered in front of the lastTask causing the lastTask to be blocked by the
803 // semaphore. Again we directly just go through adding the dependency to the task and
804 // not the proxy since we don't need to worry about resolving anything.
805 if (lastTask) {
806 waitTask->addDependency(lastTask);
807 }
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400808 this->setLastRenderTask(proxy.get(), waitTask.get());
Greg Danielc30f1a92019-09-06 15:28:58 -0400809 }
810 fDAG.add(waitTask);
811 } else {
Adlai Holler33d569e2020-06-16 14:30:08 -0400812 if (fActiveOpsTask && (fActiveOpsTask->target(0).proxy() == proxy.get())) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400813 SkASSERT(this->getLastRenderTask(proxy.get()) == fActiveOpsTask);
Greg Danielc30f1a92019-09-06 15:28:58 -0400814 fDAG.addBeforeLast(waitTask);
815 // In this case we keep the current renderTask open but just insert the new waitTask
816 // before it in the list. The waitTask will never need to trigger any resolves or mip
817 // map generation which is the main advantage of going through the proxy version.
818 // Additionally we would've had to temporarily set the wait task as the lastRenderTask
819 // on the proxy, add the dependency, and then reset the lastRenderTask to
820 // fActiveOpsTask. Additionally we make the waitTask depend on all of fActiveOpsTask
821 // dependencies so that we don't unnecessarily reorder the waitTask before them.
822 // Note: Any previous Ops already in fActiveOpsTask will get blocked by the wait
823 // semaphore even though they don't need to be for correctness.
824
825 // Make sure we add the dependencies of fActiveOpsTask to waitTask first or else we'll
826 // get a circular self dependency of waitTask on waitTask.
827 waitTask->addDependenciesFromOtherTask(fActiveOpsTask);
828 fActiveOpsTask->addDependency(waitTask.get());
829 } else {
830 // In this case we just close the previous RenderTask and start and append the waitTask
831 // to the DAG. Since it is the last task now we call setLastRenderTask on the proxy. If
832 // there is a lastTask on the proxy we make waitTask depend on that task. This
833 // dependency isn't strictly needed but it does keep the DAG from reordering the
834 // waitTask earlier and blocking more tasks.
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400835 if (GrRenderTask* lastTask = this->getLastRenderTask(proxy.get())) {
Greg Danielc30f1a92019-09-06 15:28:58 -0400836 waitTask->addDependency(lastTask);
837 }
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400838 this->setLastRenderTask(proxy.get(), waitTask.get());
Greg Danielc30f1a92019-09-06 15:28:58 -0400839 this->closeRenderTasksForNewRenderTask(proxy.get());
840 fDAG.add(waitTask);
841 }
842 }
843 waitTask->makeClosed(caps);
844
845 SkDEBUGCODE(this->validate());
846}
847
Greg Danielbbfec9d2019-08-20 10:56:51 -0400848void GrDrawingManager::newTransferFromRenderTask(sk_sp<GrSurfaceProxy> srcProxy,
849 const SkIRect& srcRect,
850 GrColorType surfaceColorType,
851 GrColorType dstColorType,
852 sk_sp<GrGpuBuffer> dstBuffer,
853 size_t dstOffset) {
854 SkDEBUGCODE(this->validate());
855 SkASSERT(fContext);
856 // This copies from srcProxy to dstBuffer so it doesn't have a real target.
857 this->closeRenderTasksForNewRenderTask(nullptr);
858
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600859 GrRenderTask* task = fDAG.add(sk_make_sp<GrTransferFromRenderTask>(
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400860 srcProxy, srcRect, surfaceColorType, dstColorType,
861 std::move(dstBuffer), dstOffset));
Greg Danielbbfec9d2019-08-20 10:56:51 -0400862
863 const GrCaps& caps = *fContext->priv().caps();
864
Brian Salomon7e67dca2020-07-21 09:27:25 -0400865 // We always say GrMipmapped::kNo here since we are always just copying from the base layer. We
Greg Danielbbfec9d2019-08-20 10:56:51 -0400866 // don't need to make sure the whole mip map chain is valid.
Brian Salomon7e67dca2020-07-21 09:27:25 -0400867 task->addDependency(this, srcProxy.get(), GrMipmapped::kNo,
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400868 GrTextureResolveManager(this), caps);
Greg Danielbbfec9d2019-08-20 10:56:51 -0400869 task->makeClosed(caps);
870
Greg Danielbbfec9d2019-08-20 10:56:51 -0400871 // We have closed the previous active oplist but since a new oplist isn't being added there
872 // shouldn't be an active one.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400873 SkASSERT(!fActiveOpsTask);
Greg Danielbbfec9d2019-08-20 10:56:51 -0400874 SkDEBUGCODE(this->validate());
875}
876
Greg Daniel16f5c652019-10-29 11:26:01 -0400877bool GrDrawingManager::newCopyRenderTask(GrSurfaceProxyView srcView,
Greg Daniele227fe42019-08-21 13:52:24 -0400878 const SkIRect& srcRect,
Greg Daniel16f5c652019-10-29 11:26:01 -0400879 GrSurfaceProxyView dstView,
Greg Daniele227fe42019-08-21 13:52:24 -0400880 const SkIPoint& dstPoint) {
881 SkDEBUGCODE(this->validate());
882 SkASSERT(fContext);
Greg Daniele227fe42019-08-21 13:52:24 -0400883
Greg Daniel16f5c652019-10-29 11:26:01 -0400884 this->closeRenderTasksForNewRenderTask(dstView.proxy());
Brian Salomone4bce012019-09-20 15:34:23 -0400885 const GrCaps& caps = *fContext->priv().caps();
886
Greg Daniel16f5c652019-10-29 11:26:01 -0400887 GrSurfaceProxy* srcProxy = srcView.proxy();
888
Brian Salomone4bce012019-09-20 15:34:23 -0400889 GrRenderTask* task =
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400890 fDAG.add(GrCopyRenderTask::Make(this, std::move(srcView), srcRect, std::move(dstView),
Greg Daniel16f5c652019-10-29 11:26:01 -0400891 dstPoint, &caps));
Greg Daniele227fe42019-08-21 13:52:24 -0400892 if (!task) {
893 return false;
894 }
895
Brian Salomon7e67dca2020-07-21 09:27:25 -0400896 // We always say GrMipmapped::kNo here since we are always just copying from the base layer to
Greg Daniele227fe42019-08-21 13:52:24 -0400897 // another base layer. We don't need to make sure the whole mip map chain is valid.
Brian Salomon7e67dca2020-07-21 09:27:25 -0400898 task->addDependency(this, srcProxy, GrMipmapped::kNo, GrTextureResolveManager(this), caps);
Greg Daniele227fe42019-08-21 13:52:24 -0400899 task->makeClosed(caps);
900
Greg Daniele227fe42019-08-21 13:52:24 -0400901 // We have closed the previous active oplist but since a new oplist isn't being added there
902 // shouldn't be an active one.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400903 SkASSERT(!fActiveOpsTask);
Greg Daniele227fe42019-08-21 13:52:24 -0400904 SkDEBUGCODE(this->validate());
905 return true;
906}
907
robertphillips68737822015-10-29 12:12:21 -0700908/*
909 * This method finds a path renderer that can draw the specified path on
910 * the provided target.
911 * Due to its expense, the software path renderer has split out so it can
912 * can be individually allowed/disallowed via the "allowSW" boolean.
913 */
914GrPathRenderer* GrDrawingManager::getPathRenderer(const GrPathRenderer::CanDrawPathArgs& args,
915 bool allowSW,
916 GrPathRendererChain::DrawType drawType,
917 GrPathRenderer::StencilSupport* stencilSupport) {
918
919 if (!fPathRendererChain) {
John Stilesfbd050b2020-08-03 13:21:46 -0400920 fPathRendererChain =
921 std::make_unique<GrPathRendererChain>(fContext, fOptionsForPathRendererChain);
robertphillips68737822015-10-29 12:12:21 -0700922 }
923
924 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(args, drawType, stencilSupport);
925 if (!pr && allowSW) {
Brian Salomone7df0bb2018-05-07 14:44:57 -0400926 auto swPR = this->getSoftwarePathRenderer();
927 if (GrPathRenderer::CanDrawPath::kNo != swPR->canDrawPath(args)) {
928 pr = swPR;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500929 }
robertphillips68737822015-10-29 12:12:21 -0700930 }
931
Robert Phillipsd81379d2020-04-21 10:39:02 -0400932#if GR_PATH_RENDERER_SPEW
933 if (pr) {
934 SkDebugf("getPathRenderer: %s\n", pr->name());
935 }
936#endif
937
robertphillips68737822015-10-29 12:12:21 -0700938 return pr;
939}
940
Brian Salomone7df0bb2018-05-07 14:44:57 -0400941GrPathRenderer* GrDrawingManager::getSoftwarePathRenderer() {
942 if (!fSoftwarePathRenderer) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400943 fSoftwarePathRenderer.reset(
Robert Phillips9da87e02019-02-04 13:26:26 -0500944 new GrSoftwarePathRenderer(fContext->priv().proxyProvider(),
Ben Wagner9ec70c62018-07-12 13:30:47 -0400945 fOptionsForPathRendererChain.fAllowPathMaskCaching));
Brian Salomone7df0bb2018-05-07 14:44:57 -0400946 }
Ben Wagner9ec70c62018-07-12 13:30:47 -0400947 return fSoftwarePathRenderer.get();
Brian Salomone7df0bb2018-05-07 14:44:57 -0400948}
949
Chris Daltonfddb6c02017-11-04 15:22:22 -0600950GrCoverageCountingPathRenderer* GrDrawingManager::getCoverageCountingPathRenderer() {
951 if (!fPathRendererChain) {
John Stilesfbd050b2020-08-03 13:21:46 -0400952 fPathRendererChain = std::make_unique<GrPathRendererChain>(fContext, fOptionsForPathRendererChain);
Chris Daltonfddb6c02017-11-04 15:22:22 -0600953 }
954 return fPathRendererChain->getCoverageCountingPathRenderer();
955}
956
Brian Salomon653f42f2018-07-10 10:07:31 -0400957void GrDrawingManager::flushIfNecessary() {
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400958 auto direct = fContext->asDirectContext();
Robert Phillips6a6de562019-02-15 15:19:15 -0500959 if (!direct) {
960 return;
961 }
962
963 auto resourceCache = direct->priv().getResourceCache();
Brian Salomon653f42f2018-07-10 10:07:31 -0400964 if (resourceCache && resourceCache->requestsFlush()) {
Greg Daniel9efe3862020-06-11 11:51:06 -0400965 if (this->flush(nullptr, 0, SkSurface::BackendSurfaceAccess::kNoAccess, GrFlushInfo(),
966 nullptr)) {
Greg Danielfe159622020-04-10 17:43:51 +0000967 this->submitToGpu(false);
968 }
Brian Salomon57d2beab2018-09-10 09:35:41 -0400969 resourceCache->purgeAsNeeded();
Brian Salomon653f42f2018-07-10 10:07:31 -0400970 }
971}
972