blob: 83af6b41c7d283c827f2570db8d50062886ad0eb [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/core/SkTTopoSort.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040014#include "src/gpu/GrAuditTrail.h"
Brian Salomon9241a6d2019-10-03 13:26:54 -040015#include "src/gpu/GrClientMappedBufferManager.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/GrContextPriv.h"
Greg Daniele227fe42019-08-21 13:52:24 -040017#include "src/gpu/GrCopyRenderTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrGpu.h"
19#include "src/gpu/GrMemoryPool.h"
20#include "src/gpu/GrOnFlushResourceProvider.h"
21#include "src/gpu/GrRecordingContextPriv.h"
22#include "src/gpu/GrRenderTargetContext.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040023#include "src/gpu/GrRenderTargetProxy.h"
Chris Dalton6b498102019-08-01 14:14:52 -060024#include "src/gpu/GrRenderTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/GrResourceAllocator.h"
26#include "src/gpu/GrResourceProvider.h"
27#include "src/gpu/GrSoftwarePathRenderer.h"
Greg Daniel46e366a2019-12-16 14:38:36 -050028#include "src/gpu/GrSurfaceContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "src/gpu/GrSurfaceProxyPriv.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000030#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "src/gpu/GrTexturePriv.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040032#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050033#include "src/gpu/GrTextureProxyPriv.h"
Chris Dalton3d770272019-08-14 09:24:37 -060034#include "src/gpu/GrTextureResolveRenderTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050035#include "src/gpu/GrTracing.h"
Greg Danielbbfec9d2019-08-20 10:56:51 -040036#include "src/gpu/GrTransferFromRenderTask.h"
Greg Danielc30f1a92019-09-06 15:28:58 -040037#include "src/gpu/GrWaitRenderTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050038#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
39#include "src/gpu/text/GrTextContext.h"
40#include "src/image/SkSurface_Gpu.h"
robertphillips498d7ac2015-10-30 10:11:30 -070041
Chris Dalton6b498102019-08-01 14:14:52 -060042GrDrawingManager::RenderTaskDAG::RenderTaskDAG(bool sortRenderTasks)
43 : fSortRenderTasks(sortRenderTasks) {}
Robert Phillipsa3f70262018-02-08 10:59:38 -050044
Chris Dalton6b498102019-08-01 14:14:52 -060045GrDrawingManager::RenderTaskDAG::~RenderTaskDAG() {}
Robert Phillips22310d62018-09-05 11:07:21 -040046
Chris Dalton6b498102019-08-01 14:14:52 -060047void GrDrawingManager::RenderTaskDAG::gatherIDs(SkSTArray<8, uint32_t, true>* idArray) const {
48 idArray->reset(fRenderTasks.count());
49 for (int i = 0; i < fRenderTasks.count(); ++i) {
50 if (fRenderTasks[i]) {
51 (*idArray)[i] = fRenderTasks[i]->uniqueID();
Robert Phillips22310d62018-09-05 11:07:21 -040052 }
53 }
54}
55
Chris Dalton6b498102019-08-01 14:14:52 -060056void GrDrawingManager::RenderTaskDAG::reset() {
57 fRenderTasks.reset();
Robert Phillips22310d62018-09-05 11:07:21 -040058}
59
Chris Dalton6b498102019-08-01 14:14:52 -060060void GrDrawingManager::RenderTaskDAG::removeRenderTask(int index) {
61 if (!fRenderTasks[index]->unique()) {
Robert Phillips22310d62018-09-05 11:07:21 -040062 // TODO: Eventually this should be guaranteed unique: http://skbug.com/7111
Chris Dalton6b498102019-08-01 14:14:52 -060063 fRenderTasks[index]->endFlush();
Robert Phillips22310d62018-09-05 11:07:21 -040064 }
65
Chris Dalton6b498102019-08-01 14:14:52 -060066 fRenderTasks[index] = nullptr;
Robert Phillips22310d62018-09-05 11:07:21 -040067}
68
Chris Dalton6b498102019-08-01 14:14:52 -060069void GrDrawingManager::RenderTaskDAG::removeRenderTasks(int startIndex, int stopIndex) {
Robert Phillips22310d62018-09-05 11:07:21 -040070 for (int i = startIndex; i < stopIndex; ++i) {
Chris Dalton6b498102019-08-01 14:14:52 -060071 if (!fRenderTasks[i]) {
Robert Phillips22310d62018-09-05 11:07:21 -040072 continue;
73 }
Chris Dalton6b498102019-08-01 14:14:52 -060074 this->removeRenderTask(i);
Robert Phillips22310d62018-09-05 11:07:21 -040075 }
76}
77
Chris Dalton6b498102019-08-01 14:14:52 -060078bool GrDrawingManager::RenderTaskDAG::isUsed(GrSurfaceProxy* proxy) const {
79 for (int i = 0; i < fRenderTasks.count(); ++i) {
80 if (fRenderTasks[i] && fRenderTasks[i]->isUsed(proxy)) {
Robert Phillips9313aa72019-04-09 18:41:27 -040081 return true;
82 }
83 }
84
85 return false;
86}
87
Chris Dalton3d770272019-08-14 09:24:37 -060088GrRenderTask* GrDrawingManager::RenderTaskDAG::add(sk_sp<GrRenderTask> renderTask) {
Chris Dalton6aeb8e82019-08-27 11:52:19 -060089 if (renderTask) {
90 return fRenderTasks.emplace_back(std::move(renderTask)).get();
91 }
92 return nullptr;
Chris Dalton3d770272019-08-14 09:24:37 -060093}
94
95GrRenderTask* GrDrawingManager::RenderTaskDAG::addBeforeLast(sk_sp<GrRenderTask> renderTask) {
96 SkASSERT(!fRenderTasks.empty());
Chris Dalton6aeb8e82019-08-27 11:52:19 -060097 if (renderTask) {
98 // Release 'fRenderTasks.back()' and grab the raw pointer, in case the SkTArray grows
99 // and reallocates during emplace_back.
100 fRenderTasks.emplace_back(fRenderTasks.back().release());
101 return (fRenderTasks[fRenderTasks.count() - 2] = std::move(renderTask)).get();
102 }
103 return nullptr;
Robert Phillips22310d62018-09-05 11:07:21 -0400104}
105
Greg Danielf41b2bd2019-08-22 16:19:24 -0400106void GrDrawingManager::RenderTaskDAG::add(const SkTArray<sk_sp<GrRenderTask>>& renderTasks) {
Robert Phillips19f466d2020-02-26 10:27:07 -0500107#ifdef SK_DEBUG
108 for (auto& renderTask : renderTasks) {
109 SkASSERT(renderTask->unique());
110 }
111#endif
112
Greg Danielf41b2bd2019-08-22 16:19:24 -0400113 fRenderTasks.push_back_n(renderTasks.count(), renderTasks.begin());
Robert Phillips22310d62018-09-05 11:07:21 -0400114}
115
Greg Danielf41b2bd2019-08-22 16:19:24 -0400116void GrDrawingManager::RenderTaskDAG::swap(SkTArray<sk_sp<GrRenderTask>>* renderTasks) {
117 SkASSERT(renderTasks->empty());
118 renderTasks->swap(fRenderTasks);
Robert Phillips22310d62018-09-05 11:07:21 -0400119}
120
Chris Dalton6b498102019-08-01 14:14:52 -0600121void GrDrawingManager::RenderTaskDAG::prepForFlush() {
122 if (fSortRenderTasks) {
123 SkDEBUGCODE(bool result =) SkTTopoSort<GrRenderTask, GrRenderTask::TopoSortTraits>(
124 &fRenderTasks);
Robert Phillips22310d62018-09-05 11:07:21 -0400125 SkASSERT(result);
126 }
127
128#ifdef SK_DEBUG
Greg Danielf41b2bd2019-08-22 16:19:24 -0400129 // This block checks for any unnecessary splits in the opsTasks. If two sequential opsTasks
130 // share the same backing GrSurfaceProxy it means the opsTask was artificially split.
Chris Dalton6b498102019-08-01 14:14:52 -0600131 if (fRenderTasks.count()) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400132 GrOpsTask* prevOpsTask = fRenderTasks[0]->asOpsTask();
Chris Dalton6b498102019-08-01 14:14:52 -0600133 for (int i = 1; i < fRenderTasks.count(); ++i) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400134 GrOpsTask* curOpsTask = fRenderTasks[i]->asOpsTask();
Robert Phillips22310d62018-09-05 11:07:21 -0400135
Greg Danielf41b2bd2019-08-22 16:19:24 -0400136 if (prevOpsTask && curOpsTask) {
Greg Daniel16f5c652019-10-29 11:26:01 -0400137 SkASSERT(prevOpsTask->fTargetView != curOpsTask->fTargetView);
Robert Phillips22310d62018-09-05 11:07:21 -0400138 }
139
Greg Danielf41b2bd2019-08-22 16:19:24 -0400140 prevOpsTask = curOpsTask;
Robert Phillips22310d62018-09-05 11:07:21 -0400141 }
142 }
143#endif
144}
145
Chris Dalton6b498102019-08-01 14:14:52 -0600146void GrDrawingManager::RenderTaskDAG::closeAll(const GrCaps* caps) {
147 for (int i = 0; i < fRenderTasks.count(); ++i) {
148 if (fRenderTasks[i]) {
149 fRenderTasks[i]->makeClosed(*caps);
Robert Phillips22310d62018-09-05 11:07:21 -0400150 }
151 }
152}
153
Chris Dalton6b498102019-08-01 14:14:52 -0600154void GrDrawingManager::RenderTaskDAG::cleanup(const GrCaps* caps) {
155 for (int i = 0; i < fRenderTasks.count(); ++i) {
156 if (!fRenderTasks[i]) {
Robert Phillips22310d62018-09-05 11:07:21 -0400157 continue;
158 }
159
Chris Dalton6b498102019-08-01 14:14:52 -0600160 // no renderTask should receive a dependency
161 fRenderTasks[i]->makeClosed(*caps);
robertphillips0dfa62c2015-11-16 06:23:31 -0800162
Greg Danielf41b2bd2019-08-22 16:19:24 -0400163 // We shouldn't need to do this, but it turns out some clients still hold onto opsTasks
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400164 // after a cleanup.
165 // MDB TODO: is this still true?
Chris Dalton6b498102019-08-01 14:14:52 -0600166 if (!fRenderTasks[i]->unique()) {
Chris Daltona84cacf2017-10-04 10:30:29 -0600167 // TODO: Eventually this should be guaranteed unique.
168 // https://bugs.chromium.org/p/skia/issues/detail?id=7111
Chris Dalton6b498102019-08-01 14:14:52 -0600169 fRenderTasks[i]->endFlush();
Chris Daltona84cacf2017-10-04 10:30:29 -0600170 }
robertphillips3dc6ae52015-10-20 09:54:32 -0700171 }
172
Chris Dalton6b498102019-08-01 14:14:52 -0600173 fRenderTasks.reset();
Robert Phillips22310d62018-09-05 11:07:21 -0400174}
175
176///////////////////////////////////////////////////////////////////////////////////////////////////
Robert Phillips69893702019-02-22 11:16:30 -0500177GrDrawingManager::GrDrawingManager(GrRecordingContext* context,
Robert Phillips22310d62018-09-05 11:07:21 -0400178 const GrPathRendererChain::Options& optionsForPathRendererChain,
179 const GrTextContext::Options& optionsForTextContext,
Chris Dalton6b498102019-08-01 14:14:52 -0600180 bool sortRenderTasks,
Greg Danielf41b2bd2019-08-22 16:19:24 -0400181 bool reduceOpsTaskSplitting)
Robert Phillips22310d62018-09-05 11:07:21 -0400182 : fContext(context)
183 , fOptionsForPathRendererChain(optionsForPathRendererChain)
184 , fOptionsForTextContext(optionsForTextContext)
Chris Dalton6b498102019-08-01 14:14:52 -0600185 , fDAG(sortRenderTasks)
Robert Phillips22310d62018-09-05 11:07:21 -0400186 , fTextContext(nullptr)
187 , fPathRendererChain(nullptr)
188 , fSoftwarePathRenderer(nullptr)
Robert Phillips6db27c22019-05-01 10:43:56 -0400189 , fFlushing(false)
Greg Danielf41b2bd2019-08-22 16:19:24 -0400190 , fReduceOpsTaskSplitting(reduceOpsTaskSplitting) {
Robert Phillips22310d62018-09-05 11:07:21 -0400191}
192
193void GrDrawingManager::cleanup() {
Robert Phillips9da87e02019-02-04 13:26:26 -0500194 fDAG.cleanup(fContext->priv().caps());
robertphillips3dc6ae52015-10-20 09:54:32 -0700195
robertphillips13391dd2015-10-30 05:15:11 -0700196 fPathRendererChain = nullptr;
Ben Wagner9ec70c62018-07-12 13:30:47 -0400197 fSoftwarePathRenderer = nullptr;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400198
199 fOnFlushCBObjects.reset();
robertphillips3dc6ae52015-10-20 09:54:32 -0700200}
201
202GrDrawingManager::~GrDrawingManager() {
203 this->cleanup();
204}
205
Robert Phillipsa9162df2019-02-11 14:12:03 -0500206bool GrDrawingManager::wasAbandoned() const {
Robert Phillips6a6de562019-02-15 15:19:15 -0500207 return fContext->priv().abandoned();
robertphillips3dc6ae52015-10-20 09:54:32 -0700208}
209
robertphillips68737822015-10-29 12:12:21 -0700210void GrDrawingManager::freeGpuResources() {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400211 for (int i = fOnFlushCBObjects.count() - 1; i >= 0; --i) {
212 if (!fOnFlushCBObjects[i]->retainOnFreeGpuResources()) {
213 // it's safe to just do this because we're iterating in reverse
214 fOnFlushCBObjects.removeShuffle(i);
215 }
216 }
217
robertphillips68737822015-10-29 12:12:21 -0700218 // a path renderer may be holding onto resources
robertphillips13391dd2015-10-30 05:15:11 -0700219 fPathRendererChain = nullptr;
Ben Wagner9ec70c62018-07-12 13:30:47 -0400220 fSoftwarePathRenderer = nullptr;
Robert Phillipse3302df2017-04-24 07:31:02 -0400221}
222
Robert Phillips7ee385e2017-03-30 08:02:11 -0400223// MDB TODO: make use of the 'proxy' parameter.
Greg Danielfe159622020-04-10 17:43:51 +0000224bool GrDrawingManager::flush(GrSurfaceProxy* proxies[], int numProxies,
Greg Daniel797efca2019-05-09 14:04:20 -0400225 SkSurface::BackendSurfaceAccess access, const GrFlushInfo& info,
226 const GrPrepareForExternalIORequests& externalRequests) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400227 SkASSERT(numProxies >= 0);
228 SkASSERT(!numProxies || proxies);
Brian Salomon57d2beab2018-09-10 09:35:41 -0400229 GR_CREATE_TRACE_MARKER_CONTEXT("GrDrawingManager", "flush", fContext);
Brian Salomondcbb9d92017-07-19 10:53:20 -0400230
robertphillips7761d612016-05-16 09:14:53 -0700231 if (fFlushing || this->wasAbandoned()) {
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400232 if (info.fFinishedProc) {
233 info.fFinishedProc(info.fFinishedContext);
Greg Daniela3aa75a2019-04-12 14:24:55 -0400234 }
Greg Danielfe159622020-04-10 17:43:51 +0000235 return false;
joshualittb8918c42015-12-18 09:59:46 -0800236 }
Robert Phillips602df412019-04-08 11:10:39 -0400237
Robert Phillips38d64b02018-09-04 13:23:26 -0400238 SkDEBUGCODE(this->validate());
239
Greg Daniel797efca2019-05-09 14:04:20 -0400240 if (kNone_GrFlushFlags == info.fFlags && !info.fNumSemaphores && !info.fFinishedProc &&
241 !externalRequests.hasRequests()) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400242 bool canSkip = numProxies > 0;
243 for (int i = 0; i < numProxies && canSkip; ++i) {
244 canSkip = !fDAG.isUsed(proxies[i]) && !this->isDDLTarget(proxies[i]);
245 }
246 if (canSkip) {
Greg Danielfe159622020-04-10 17:43:51 +0000247 return false;
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400248 }
Robert Phillips9313aa72019-04-09 18:41:27 -0400249 }
250
Robert Phillips6a6de562019-02-15 15:19:15 -0500251 auto direct = fContext->priv().asDirectContext();
252 if (!direct) {
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400253 if (info.fFinishedProc) {
254 info.fFinishedProc(info.fFinishedContext);
Greg Daniela3aa75a2019-04-12 14:24:55 -0400255 }
Greg Danielfe159622020-04-10 17:43:51 +0000256 return false; // Can't flush while DDL recording
Robert Phillips6a6de562019-02-15 15:19:15 -0500257 }
Brian Salomon9241a6d2019-10-03 13:26:54 -0400258 direct->priv().clientMappedBufferManager()->process();
Robert Phillips6a6de562019-02-15 15:19:15 -0500259
260 GrGpu* gpu = direct->priv().getGpu();
Robert Phillips874b5352018-03-16 08:48:24 -0400261 if (!gpu) {
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400262 if (info.fFinishedProc) {
263 info.fFinishedProc(info.fFinishedContext);
Greg Daniela3aa75a2019-04-12 14:24:55 -0400264 }
Greg Danielfe159622020-04-10 17:43:51 +0000265 return false; // Can't flush while DDL recording
Robert Phillips874b5352018-03-16 08:48:24 -0400266 }
Greg Daniela3aa75a2019-04-12 14:24:55 -0400267
joshualittb8918c42015-12-18 09:59:46 -0800268 fFlushing = true;
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400269
Robert Phillips6a6de562019-02-15 15:19:15 -0500270 auto resourceProvider = direct->priv().resourceProvider();
271 auto resourceCache = direct->priv().getResourceCache();
272
Chris Dalton6b498102019-08-01 14:14:52 -0600273 // Semi-usually the GrRenderTasks are already closed at this point, but sometimes Ganesh needs
Greg Danielf41b2bd2019-08-22 16:19:24 -0400274 // to flush mid-draw. In that case, the SkGpuDevice's opsTasks won't be closed but need to be
275 // flushed anyway. Closing such opsTasks here will mean new ones will be created to replace them
Chris Dalton6b498102019-08-01 14:14:52 -0600276 // if the SkGpuDevice(s) write to them again.
Robert Phillips9da87e02019-02-04 13:26:26 -0500277 fDAG.closeAll(fContext->priv().caps());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400278 fActiveOpsTask = nullptr;
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400279
Robert Phillips22310d62018-09-05 11:07:21 -0400280 fDAG.prepForFlush();
Brian Salomon601ac802019-02-07 13:37:16 -0500281 if (!fCpuBufferCache) {
282 // We cache more buffers when the backend is using client side arrays. Otherwise, we
283 // expect each pool will use a CPU buffer as a staging buffer before uploading to a GPU
284 // buffer object. Each pool only requires one staging buffer at a time.
285 int maxCachedBuffers = fContext->priv().caps()->preferClientSideDynamicBuffers() ? 2 : 6;
286 fCpuBufferCache = GrBufferAllocPool::CpuBufferCache::Make(maxCachedBuffers);
Brian Salomon58f153c2018-10-18 21:51:15 -0400287 }
Robert Phillipsa4c93ac2017-05-18 11:40:04 -0400288
Robert Phillipse5f73282019-06-18 17:15:04 -0400289 GrOpFlushState flushState(gpu, resourceProvider, &fTokenTracker, fCpuBufferCache);
Robert Phillips40a29d72018-01-18 12:59:22 -0500290
Chris Daltonfe199b72017-05-05 11:26:15 -0400291 GrOnFlushResourceProvider onFlushProvider(this);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500292 // TODO: AFAICT the only reason fFlushState is on GrDrawingManager rather than on the
293 // stack here is to preserve the flush tokens.
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400294
Chris Dalton12658942017-10-05 19:45:25 -0600295 // Prepare any onFlush op lists (e.g. atlases).
Chris Daltonfe199b72017-05-05 11:26:15 -0400296 if (!fOnFlushCBObjects.empty()) {
Chris Dalton6b498102019-08-01 14:14:52 -0600297 fDAG.gatherIDs(&fFlushingRenderTaskIDs);
Robert Phillips22310d62018-09-05 11:07:21 -0400298
Chris Daltonfe199b72017-05-05 11:26:15 -0400299 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
Chris Dalton6b498102019-08-01 14:14:52 -0600300 onFlushCBObject->preFlush(&onFlushProvider, fFlushingRenderTaskIDs.begin(),
Chris Daltonc4b47352019-08-23 10:10:36 -0600301 fFlushingRenderTaskIDs.count());
302 }
303 for (const auto& onFlushRenderTask : fOnFlushRenderTasks) {
304 onFlushRenderTask->makeClosed(*fContext->priv().caps());
Chris Dalton706a6ff2017-11-29 22:01:06 -0700305#ifdef SK_DEBUG
Chris Daltonc4b47352019-08-23 10:10:36 -0600306 // OnFlush callbacks are invoked during flush, and are therefore expected to handle
307 // resource allocation & usage on their own. (No deferred or lazy proxies!)
308 onFlushRenderTask->visitTargetAndSrcProxies_debugOnly(
309 [](GrSurfaceProxy* p, GrMipMapped mipMapped) {
310 SkASSERT(!p->asTextureProxy() || !p->asTextureProxy()->texPriv().isDeferred());
Brian Salomonbeb7f522019-08-30 16:19:42 -0400311 SkASSERT(!p->isLazy());
Chris Dalton4ece96d2019-08-30 11:26:39 -0600312 if (p->requiresManualMSAAResolve()) {
313 // The onFlush callback is responsible for ensuring MSAA gets resolved.
314 SkASSERT(p->asRenderTargetProxy() && !p->asRenderTargetProxy()->isMSAADirty());
315 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600316 if (GrMipMapped::kYes == mipMapped) {
317 // The onFlush callback is responsible for regenerating mips if needed.
318 SkASSERT(p->asTextureProxy() && !p->asTextureProxy()->mipMapsAreDirty());
319 }
320 });
Chris Dalton706a6ff2017-11-29 22:01:06 -0700321#endif
Chris Daltonc4b47352019-08-23 10:10:36 -0600322 onFlushRenderTask->prepare(&flushState);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400323 }
324 }
325
robertphillipsa13e2022015-11-11 12:01:09 -0800326#if 0
Brian Salomon09d994e2016-12-21 11:14:46 -0500327 // Enable this to print out verbose GrOp information
Chris Daltonc4b47352019-08-23 10:10:36 -0600328 SkDEBUGCODE(SkDebugf("onFlush renderTasks:"));
329 for (const auto& onFlushRenderTask : fOnFlushRenderTasks) {
330 SkDEBUGCODE(onFlushRenderTask->dump();)
331 }
332 SkDEBUGCODE(SkDebugf("Normal renderTasks:"));
Chris Dalton6b498102019-08-01 14:14:52 -0600333 for (int i = 0; i < fRenderTasks.count(); ++i) {
334 SkDEBUGCODE(fRenderTasks[i]->dump();)
robertphillips3dc6ae52015-10-20 09:54:32 -0700335 }
robertphillipsa13e2022015-11-11 12:01:09 -0800336#endif
337
Robert Phillipseafd48a2017-11-16 07:52:08 -0500338 int startIndex, stopIndex;
339 bool flushed = false;
340
Robert Phillipsf8e25022017-11-08 15:24:31 -0500341 {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400342 GrResourceAllocator alloc(resourceProvider SkDEBUGCODE(, fDAG.numRenderTasks()));
Chris Dalton6b498102019-08-01 14:14:52 -0600343 for (int i = 0; i < fDAG.numRenderTasks(); ++i) {
344 if (fDAG.renderTask(i)) {
345 fDAG.renderTask(i)->gatherProxyIntervals(&alloc);
Robert Phillips22310d62018-09-05 11:07:21 -0400346 }
Greg Danielf41b2bd2019-08-22 16:19:24 -0400347 alloc.markEndOfOpsTask(i);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500348 }
Robert Phillipsc73666f2019-04-24 08:49:48 -0400349 alloc.determineRecyclability();
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400350
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500351 GrResourceAllocator::AssignError error = GrResourceAllocator::AssignError::kNoError;
Chris Dalton6b498102019-08-01 14:14:52 -0600352 int numRenderTasksExecuted = 0;
Brian Salomon577aa0f2018-11-30 13:32:23 -0500353 while (alloc.assign(&startIndex, &stopIndex, &error)) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500354 if (GrResourceAllocator::AssignError::kFailedProxyInstantiation == error) {
355 for (int i = startIndex; i < stopIndex; ++i) {
Chris Dalton6b498102019-08-01 14:14:52 -0600356 GrRenderTask* renderTask = fDAG.renderTask(i);
357 if (!renderTask) {
358 continue;
Robert Phillips01a91282018-07-26 08:03:04 -0400359 }
Chris Dalton6b498102019-08-01 14:14:52 -0600360 if (!renderTask->isInstantiated()) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400361 // No need to call the renderTask's handleInternalAllocationFailure
362 // since we will already skip executing the renderTask since it is not
363 // instantiated.
Chris Dalton6b498102019-08-01 14:14:52 -0600364 continue;
Robert Phillipsbbcb7f72018-05-31 11:16:19 -0400365 }
Chris Dalton6b498102019-08-01 14:14:52 -0600366 renderTask->handleInternalAllocationFailure();
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500367 }
368 }
Robert Phillips4150eea2018-02-07 17:08:21 -0500369
Chris Dalton6b498102019-08-01 14:14:52 -0600370 if (this->executeRenderTasks(
371 startIndex, stopIndex, &flushState, &numRenderTasksExecuted)) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500372 flushed = true;
373 }
bsalomondc438982016-08-31 11:53:49 -0700374 }
Greg Danielc42b20b2017-10-04 10:34:49 -0400375 }
376
Chris Dalton91ab1552018-04-18 13:24:25 -0600377#ifdef SK_DEBUG
Chris Dalton6b498102019-08-01 14:14:52 -0600378 for (int i = 0; i < fDAG.numRenderTasks(); ++i) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400379 // If there are any remaining opsTaskss at this point, make sure they will not survive the
Chris Dalton91ab1552018-04-18 13:24:25 -0600380 // flush. Otherwise we need to call endFlush() on them.
381 // http://skbug.com/7111
Chris Dalton6b498102019-08-01 14:14:52 -0600382 SkASSERT(!fDAG.renderTask(i) || fDAG.renderTask(i)->unique());
Chris Dalton91ab1552018-04-18 13:24:25 -0600383 }
384#endif
Robert Phillips22310d62018-09-05 11:07:21 -0400385 fDAG.reset();
Robert Phillips15c91422019-05-07 16:54:48 -0400386 this->clearDDLTargets();
robertphillipsa13e2022015-11-11 12:01:09 -0800387
Robert Phillipsc994a932018-06-19 13:09:54 -0400388#ifdef SK_DEBUG
389 // In non-DDL mode this checks that all the flushed ops have been freed from the memory pool.
390 // When we move to partial flushes this assert will no longer be valid.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400391 // In DDL mode this check is somewhat superfluous since the memory for most of the ops/opsTasks
Robert Phillipsc994a932018-06-19 13:09:54 -0400392 // will be stored in the DDL's GrOpMemoryPools.
Robert Phillips9da87e02019-02-04 13:26:26 -0500393 GrOpMemoryPool* opMemoryPool = fContext->priv().opMemoryPool();
Robert Phillipsc994a932018-06-19 13:09:54 -0400394 opMemoryPool->isEmpty();
395#endif
396
Greg Danielfe159622020-04-10 17:43:51 +0000397 gpu->executeFlushInfo(proxies, numProxies, access, info, externalRequests);
robertphillipsa13e2022015-11-11 12:01:09 -0800398
Brian Salomon57d2beab2018-09-10 09:35:41 -0400399 // Give the cache a chance to purge resources that become purgeable due to flushing.
400 if (flushed) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500401 resourceCache->purgeAsNeeded();
Brian Salomon876a0172019-03-08 11:12:14 -0500402 flushed = false;
bsalomonb77a9072016-09-07 10:02:04 -0700403 }
Chris Daltonfe199b72017-05-05 11:26:15 -0400404 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
Chris Dalton6b498102019-08-01 14:14:52 -0600405 onFlushCBObject->postFlush(fTokenTracker.nextTokenToFlush(), fFlushingRenderTaskIDs.begin(),
406 fFlushingRenderTaskIDs.count());
Brian Salomon876a0172019-03-08 11:12:14 -0500407 flushed = true;
408 }
409 if (flushed) {
410 resourceCache->purgeAsNeeded();
Chris Daltonfe199b72017-05-05 11:26:15 -0400411 }
Chris Dalton6b498102019-08-01 14:14:52 -0600412 fFlushingRenderTaskIDs.reset();
joshualittb8918c42015-12-18 09:59:46 -0800413 fFlushing = false;
Greg Daniel51316782017-08-02 15:10:09 +0000414
Greg Danielfe159622020-04-10 17:43:51 +0000415 return true;
416}
417
418bool GrDrawingManager::submitToGpu(bool syncToCpu) {
419 if (fFlushing || this->wasAbandoned()) {
420 return false;
421 }
422
423 auto direct = fContext->priv().asDirectContext();
424 if (!direct) {
425 return false; // Can't submit while DDL recording
426 }
427 GrGpu* gpu = direct->priv().getGpu();
428 return gpu->submitToGpu(syncToCpu);
robertphillips3dc6ae52015-10-20 09:54:32 -0700429}
430
Chris Dalton6b498102019-08-01 14:14:52 -0600431bool GrDrawingManager::executeRenderTasks(int startIndex, int stopIndex, GrOpFlushState* flushState,
432 int* numRenderTasksExecuted) {
433 SkASSERT(startIndex <= stopIndex && stopIndex <= fDAG.numRenderTasks());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500434
Robert Phillips27483912018-04-20 12:43:18 -0400435#if GR_FLUSH_TIME_OP_SPEW
Greg Danielf41b2bd2019-08-22 16:19:24 -0400436 SkDebugf("Flushing opsTask: %d to %d out of [%d, %d]\n",
Chris Dalton6b498102019-08-01 14:14:52 -0600437 startIndex, stopIndex, 0, fDAG.numRenderTasks());
Robert Phillips27483912018-04-20 12:43:18 -0400438 for (int i = startIndex; i < stopIndex; ++i) {
Chris Dalton6b498102019-08-01 14:14:52 -0600439 if (fDAG.renderTask(i)) {
440 fDAG.renderTask(i)->dump(true);
Robert Phillips1734dd32018-08-21 13:52:09 -0400441 }
Robert Phillips27483912018-04-20 12:43:18 -0400442 }
443#endif
444
Chris Dalton6b498102019-08-01 14:14:52 -0600445 bool anyRenderTasksExecuted = false;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500446
447 for (int i = startIndex; i < stopIndex; ++i) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400448 GrRenderTask* renderTask = fDAG.renderTask(i);
449 if (!renderTask || !renderTask->isInstantiated()) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500450 continue;
451 }
452
Chris Dalton6b498102019-08-01 14:14:52 -0600453 SkASSERT(renderTask->deferredProxiesAreInstantiated());
Robert Phillips22310d62018-09-05 11:07:21 -0400454
Chris Dalton6b498102019-08-01 14:14:52 -0600455 renderTask->prepare(flushState);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500456 }
457
458 // Upload all data to the GPU
459 flushState->preExecuteDraws();
460
Greg Danield2073452018-12-07 11:20:33 -0500461 // For Vulkan, if we have too many oplists to be flushed we end up allocating a lot of resources
462 // for each command buffer associated with the oplists. If this gets too large we can cause the
463 // devices to go OOM. In practice we usually only hit this case in our tests, but to be safe we
464 // put a cap on the number of oplists we will execute before flushing to the GPU to relieve some
465 // memory pressure.
Chris Dalton6b498102019-08-01 14:14:52 -0600466 static constexpr int kMaxRenderTasksBeforeFlush = 100;
Greg Danield2073452018-12-07 11:20:33 -0500467
Chris Daltonc4b47352019-08-23 10:10:36 -0600468 // Execute the onFlush renderTasks first, if any.
469 for (sk_sp<GrRenderTask>& onFlushRenderTask : fOnFlushRenderTasks) {
470 if (!onFlushRenderTask->execute(flushState)) {
471 SkDebugf("WARNING: onFlushRenderTask failed to execute.\n");
Robert Phillipseafd48a2017-11-16 07:52:08 -0500472 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600473 SkASSERT(onFlushRenderTask->unique());
474 onFlushRenderTask = nullptr;
Chris Dalton6b498102019-08-01 14:14:52 -0600475 (*numRenderTasksExecuted)++;
476 if (*numRenderTasksExecuted >= kMaxRenderTasksBeforeFlush) {
Greg Danielfe159622020-04-10 17:43:51 +0000477 flushState->gpu()->submitToGpu(false);
Chris Dalton6b498102019-08-01 14:14:52 -0600478 *numRenderTasksExecuted = 0;
Greg Danield2073452018-12-07 11:20:33 -0500479 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500480 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600481 fOnFlushRenderTasks.reset();
Robert Phillipseafd48a2017-11-16 07:52:08 -0500482
483 // Execute the normal op lists.
484 for (int i = startIndex; i < stopIndex; ++i) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400485 GrRenderTask* renderTask = fDAG.renderTask(i);
486 if (!renderTask || !renderTask->isInstantiated()) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500487 continue;
488 }
489
Greg Daniel15ecdf92019-08-30 15:35:23 -0400490 if (renderTask->execute(flushState)) {
Chris Dalton6b498102019-08-01 14:14:52 -0600491 anyRenderTasksExecuted = true;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500492 }
Chris Dalton6b498102019-08-01 14:14:52 -0600493 (*numRenderTasksExecuted)++;
494 if (*numRenderTasksExecuted >= kMaxRenderTasksBeforeFlush) {
Greg Danielfe159622020-04-10 17:43:51 +0000495 flushState->gpu()->submitToGpu(false);
Chris Dalton6b498102019-08-01 14:14:52 -0600496 *numRenderTasksExecuted = 0;
Greg Danield2073452018-12-07 11:20:33 -0500497 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500498 }
499
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400500 SkASSERT(!flushState->opsRenderPass());
Robert Phillips40a29d72018-01-18 12:59:22 -0500501 SkASSERT(fTokenTracker.nextDrawToken() == fTokenTracker.nextTokenToFlush());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500502
Chris Dalton6b498102019-08-01 14:14:52 -0600503 // We reset the flush state before the RenderTasks so that the last resources to be freed are
504 // those that are written to in the RenderTasks. This helps to make sure the most recently used
505 // resources are the last to be purged by the resource cache.
Robert Phillipseafd48a2017-11-16 07:52:08 -0500506 flushState->reset();
507
Chris Dalton6b498102019-08-01 14:14:52 -0600508 fDAG.removeRenderTasks(startIndex, stopIndex);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500509
Chris Dalton6b498102019-08-01 14:14:52 -0600510 return anyRenderTasksExecuted;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500511}
512
Greg Daniel55f040b2020-02-13 15:38:32 +0000513GrSemaphoresSubmitted GrDrawingManager::flushSurfaces(GrSurfaceProxy* proxies[], int numProxies,
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400514 SkSurface::BackendSurfaceAccess access,
515 const GrFlushInfo& info) {
bsalomon6a2b1942016-09-08 11:28:59 -0700516 if (this->wasAbandoned()) {
Greg Daniel51316782017-08-02 15:10:09 +0000517 return GrSemaphoresSubmitted::kNo;
bsalomon6a2b1942016-09-08 11:28:59 -0700518 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400519 SkDEBUGCODE(this->validate());
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400520 SkASSERT(numProxies >= 0);
521 SkASSERT(!numProxies || proxies);
bsalomon6a2b1942016-09-08 11:28:59 -0700522
Robert Phillips6a6de562019-02-15 15:19:15 -0500523 auto direct = fContext->priv().asDirectContext();
524 if (!direct) {
525 return GrSemaphoresSubmitted::kNo; // Can't flush while DDL recording
526 }
527
528 GrGpu* gpu = direct->priv().getGpu();
Robert Phillips874b5352018-03-16 08:48:24 -0400529 if (!gpu) {
530 return GrSemaphoresSubmitted::kNo; // Can't flush while DDL recording
531 }
532
Robert Phillipsacc10fa2019-04-01 09:50:20 -0400533 // TODO: It is important to upgrade the drawingmanager to just flushing the
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400534 // portion of the DAG required by 'proxies' in order to restore some of the
Robert Phillipsacc10fa2019-04-01 09:50:20 -0400535 // semantics of this method.
Greg Danielfe159622020-04-10 17:43:51 +0000536 bool didFlush = this->flush(proxies, numProxies, access, info,
537 GrPrepareForExternalIORequests());
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400538 for (int i = 0; i < numProxies; ++i) {
Chris Dalton4ece96d2019-08-30 11:26:39 -0600539 GrSurfaceProxy* proxy = proxies[i];
540 if (!proxy->isInstantiated()) {
Greg Danielfe159622020-04-10 17:43:51 +0000541 continue;
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400542 }
Chris Dalton4ece96d2019-08-30 11:26:39 -0600543 // In the flushSurfaces case, we need to resolve MSAA immediately after flush. This is
544 // because the client will call through to this method when drawing into a target created by
545 // wrapBackendTextureAsRenderTarget, and will expect the original texture to be fully
546 // resolved upon return.
547 if (proxy->requiresManualMSAAResolve()) {
548 auto* rtProxy = proxy->asRenderTargetProxy();
549 SkASSERT(rtProxy);
550 if (rtProxy->isMSAADirty()) {
551 SkASSERT(rtProxy->peekRenderTarget());
Chris Dalton16a33c62019-09-24 22:19:17 -0600552 gpu->resolveRenderTarget(rtProxy->peekRenderTarget(), rtProxy->msaaDirtyRect(),
Greg Daniel242536f2020-02-13 14:12:46 -0500553 GrGpu::ForExternalIO::kYes);
Chris Dalton4ece96d2019-08-30 11:26:39 -0600554 rtProxy->markMSAAResolved();
555 }
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400556 }
Chris Dalton3d770272019-08-14 09:24:37 -0600557 // If, after a flush, any of the proxies of interest have dirty mipmaps, regenerate them in
558 // case their backend textures are being stolen.
559 // (This special case is exercised by the ReimportImageTextureWithMipLevels test.)
560 // FIXME: It may be more ideal to plumb down a "we're going to steal the backends" flag.
Chris Dalton4ece96d2019-08-30 11:26:39 -0600561 if (auto* textureProxy = proxy->asTextureProxy()) {
Chris Dalton3d770272019-08-14 09:24:37 -0600562 if (textureProxy->mipMapsAreDirty()) {
Chris Dalton4ece96d2019-08-30 11:26:39 -0600563 SkASSERT(textureProxy->peekTexture());
Chris Dalton3d770272019-08-14 09:24:37 -0600564 gpu->regenerateMipMapLevels(textureProxy->peekTexture());
565 textureProxy->markMipMapsClean();
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400566 }
Brian Salomon930f9392018-06-20 16:25:26 -0400567 }
bsalomon6a2b1942016-09-08 11:28:59 -0700568 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400569
570 SkDEBUGCODE(this->validate());
Greg Danielfe159622020-04-10 17:43:51 +0000571
572 bool submitted = false;
573 if (didFlush) {
574 submitted = this->submitToGpu(SkToBool(info.fFlags & kSyncCpu_GrFlushFlag));
575 }
576
577 if (!submitted || (!direct->priv().caps()->semaphoreSupport() && info.fNumSemaphores)) {
578 return GrSemaphoresSubmitted::kNo;
579 }
580 return GrSemaphoresSubmitted::kYes;
bsalomon6a2b1942016-09-08 11:28:59 -0700581}
582
Chris Daltonfe199b72017-05-05 11:26:15 -0400583void GrDrawingManager::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
584 fOnFlushCBObjects.push_back(onFlushCBObject);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400585}
586
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500587#if GR_TEST_UTILS
588void GrDrawingManager::testingOnly_removeOnFlushCallbackObject(GrOnFlushCallbackObject* cb) {
589 int n = std::find(fOnFlushCBObjects.begin(), fOnFlushCBObjects.end(), cb) -
590 fOnFlushCBObjects.begin();
591 SkASSERT(n < fOnFlushCBObjects.count());
592 fOnFlushCBObjects.removeShuffle(n);
593}
594#endif
595
Chris Dalton6b498102019-08-01 14:14:52 -0600596void GrDrawingManager::moveRenderTasksToDDL(SkDeferredDisplayList* ddl) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400597 SkDEBUGCODE(this->validate());
598
Chris Dalton6b498102019-08-01 14:14:52 -0600599 // no renderTask should receive a new command after this
Robert Phillips9da87e02019-02-04 13:26:26 -0500600 fDAG.closeAll(fContext->priv().caps());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400601 fActiveOpsTask = nullptr;
Robert Phillips7a137052018-02-01 11:23:12 -0500602
Chris Dalton6b498102019-08-01 14:14:52 -0600603 fDAG.swap(&ddl->fRenderTasks);
Robert Phillips19f466d2020-02-26 10:27:07 -0500604 SkASSERT(!fDAG.numRenderTasks());
Robert Phillips867ce8f2018-06-21 10:28:36 -0400605
Robert Phillips19f466d2020-02-26 10:27:07 -0500606 for (auto& renderTask : ddl->fRenderTasks) {
Robert Phillips29f38542019-10-16 09:20:25 -0400607 renderTask->prePrepare(fContext);
Robert Phillips7327c9d2019-10-08 16:32:56 -0400608 }
609
Michael Ludwig2c316bd2019-12-19 14:50:44 -0500610 ddl->fArenas = std::move(fContext->priv().detachArenas());
Robert Phillips61fc7992019-10-22 11:58:17 -0400611
Robert Phillipsf6a0b452020-02-18 14:26:46 -0500612 fContext->priv().detachProgramData(&ddl->fProgramData);
Robert Phillips576b6a12019-12-06 13:05:49 -0500613
Robert Phillips774168e2018-05-31 12:43:27 -0400614 if (fPathRendererChain) {
615 if (auto ccpr = fPathRendererChain->getCoverageCountingPathRenderer()) {
616 ddl->fPendingPaths = ccpr->detachPendingPaths();
617 }
618 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400619
620 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500621}
622
Chris Dalton6b498102019-08-01 14:14:52 -0600623void GrDrawingManager::copyRenderTasksFromDDL(const SkDeferredDisplayList* ddl,
Robert Phillips933484f2019-11-26 09:38:55 -0500624 GrRenderTargetProxy* newDest) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400625 SkDEBUGCODE(this->validate());
626
Greg Danielf41b2bd2019-08-22 16:19:24 -0400627 if (fActiveOpsTask) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400628 // This is a temporary fix for the partial-MDB world. In that world we're not
Greg Danielf41b2bd2019-08-22 16:19:24 -0400629 // reordering so ops that (in the single opsTask world) would've just glommed onto the
630 // end of the single opsTask but referred to a far earlier RT need to appear in their
631 // own opsTask.
632 fActiveOpsTask->makeClosed(*fContext->priv().caps());
633 fActiveOpsTask = nullptr;
Robert Phillips38d64b02018-09-04 13:23:26 -0400634 }
635
Robert Phillips15c91422019-05-07 16:54:48 -0400636 this->addDDLTarget(newDest);
637
Robert Phillips62000362018-02-01 09:10:04 -0500638 // Here we jam the proxy that backs the current replay SkSurface into the LazyProxyData.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400639 // The lazy proxy that references it (in the copied opsTasks) will steal its GrTexture.
Robert Phillips62000362018-02-01 09:10:04 -0500640 ddl->fLazyProxyData->fReplayDest = newDest;
Robert Phillips774168e2018-05-31 12:43:27 -0400641
642 if (ddl->fPendingPaths.size()) {
643 GrCoverageCountingPathRenderer* ccpr = this->getCoverageCountingPathRenderer();
644
645 ccpr->mergePendingPaths(ddl->fPendingPaths);
646 }
Robert Phillips22310d62018-09-05 11:07:21 -0400647
Chris Dalton6b498102019-08-01 14:14:52 -0600648 fDAG.add(ddl->fRenderTasks);
Robert Phillips38d64b02018-09-04 13:23:26 -0400649
650 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500651}
652
Robert Phillips38d64b02018-09-04 13:23:26 -0400653#ifdef SK_DEBUG
654void GrDrawingManager::validate() const {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400655 if (fDAG.sortingRenderTasks() && fReduceOpsTaskSplitting) {
656 SkASSERT(!fActiveOpsTask);
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400657 } else {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400658 if (fActiveOpsTask) {
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400659 SkASSERT(!fDAG.empty());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400660 SkASSERT(!fActiveOpsTask->isClosed());
661 SkASSERT(fActiveOpsTask == fDAG.back());
Robert Phillips38d64b02018-09-04 13:23:26 -0400662 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400663
Chris Dalton6b498102019-08-01 14:14:52 -0600664 for (int i = 0; i < fDAG.numRenderTasks(); ++i) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400665 if (fActiveOpsTask != fDAG.renderTask(i)) {
Chris Daltone2a903e2019-09-18 13:41:50 -0600666 // The resolveTask associated with the activeTask remains open for as long as the
667 // activeTask does.
668 bool isActiveResolveTask =
669 fActiveOpsTask && fActiveOpsTask->fTextureResolveTask == fDAG.renderTask(i);
670 SkASSERT(isActiveResolveTask || fDAG.renderTask(i)->isClosed());
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400671 }
672 }
673
674 if (!fDAG.empty() && !fDAG.back()->isClosed()) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400675 SkASSERT(fActiveOpsTask == fDAG.back());
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400676 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400677 }
678}
679#endif
680
Greg Danielbbfec9d2019-08-20 10:56:51 -0400681void GrDrawingManager::closeRenderTasksForNewRenderTask(GrSurfaceProxy* target) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400682 if (target && fDAG.sortingRenderTasks() && fReduceOpsTaskSplitting) {
Chris Dalton5fe99772019-08-06 11:57:39 -0600683 // In this case we need to close all the renderTasks that rely on the current contents of
684 // 'target'. That is bc we're going to update the content of the proxy so they need to be
685 // split in case they use both the old and new content. (This is a bit of an overkill: they
686 // really only need to be split if they ever reference proxy's contents again but that is
687 // hard to predict/handle).
688 if (GrRenderTask* lastRenderTask = target->getLastRenderTask()) {
Chris Dalton6b498102019-08-01 14:14:52 -0600689 lastRenderTask->closeThoseWhoDependOnMe(*fContext->priv().caps());
Robert Phillips46acf9d2018-10-09 09:31:40 -0400690 }
Greg Danielf41b2bd2019-08-22 16:19:24 -0400691 } else if (fActiveOpsTask) {
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400692 // This is a temporary fix for the partial-MDB world. In that world we're not
Greg Danielf41b2bd2019-08-22 16:19:24 -0400693 // reordering so ops that (in the single opsTask world) would've just glommed onto the
694 // end of the single opsTask but referred to a far earlier RT need to appear in their
695 // own opsTask.
696 fActiveOpsTask->makeClosed(*fContext->priv().caps());
697 fActiveOpsTask = nullptr;
robertphillips3dc6ae52015-10-20 09:54:32 -0700698 }
Chris Dalton5fe99772019-08-06 11:57:39 -0600699}
700
Greg Daniel16f5c652019-10-29 11:26:01 -0400701sk_sp<GrOpsTask> GrDrawingManager::newOpsTask(GrSurfaceProxyView surfaceView,
702 bool managedOpsTask) {
Chris Dalton5fe99772019-08-06 11:57:39 -0600703 SkDEBUGCODE(this->validate());
704 SkASSERT(fContext);
705
Greg Daniel16f5c652019-10-29 11:26:01 -0400706 GrSurfaceProxy* proxy = surfaceView.proxy();
707 this->closeRenderTasksForNewRenderTask(proxy);
robertphillips3dc6ae52015-10-20 09:54:32 -0700708
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500709 sk_sp<GrOpsTask> opsTask(new GrOpsTask(fContext->priv().arenas(),
Michael Ludwigd0840ec2019-12-12 09:48:38 -0500710 std::move(surfaceView),
711 fContext->priv().auditTrail()));
Greg Daniel16f5c652019-10-29 11:26:01 -0400712 SkASSERT(proxy->getLastRenderTask() == opsTask.get());
robertphillips3dc6ae52015-10-20 09:54:32 -0700713
Greg Danielf41b2bd2019-08-22 16:19:24 -0400714 if (managedOpsTask) {
715 fDAG.add(opsTask);
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400716
Greg Danielf41b2bd2019-08-22 16:19:24 -0400717 if (!fDAG.sortingRenderTasks() || !fReduceOpsTaskSplitting) {
718 fActiveOpsTask = opsTask.get();
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400719 }
Robert Phillips941d1442017-06-14 16:37:02 -0400720 }
robertphillips3dc6ae52015-10-20 09:54:32 -0700721
Robert Phillips38d64b02018-09-04 13:23:26 -0400722 SkDEBUGCODE(this->validate());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400723 return opsTask;
robertphillips3dc6ae52015-10-20 09:54:32 -0700724}
725
Chris Daltone2a903e2019-09-18 13:41:50 -0600726GrTextureResolveRenderTask* GrDrawingManager::newTextureResolveRenderTask(const GrCaps& caps) {
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600727 // 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 -0600728 // in sorting and opsTask reduction mode) the render tasks that depend on any proxy's current
Greg Danielf41b2bd2019-08-22 16:19:24 -0400729 // state. This is because those opsTasks can still receive new ops and because if they refer to
Chris Dalton4ece96d2019-08-30 11:26:39 -0600730 // the mipmapped version of 'proxy', they will then come to depend on the render task being
731 // created here.
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600732 //
Greg Danielf41b2bd2019-08-22 16:19:24 -0400733 // Add the new textureResolveTask before the fActiveOpsTask (if not in
734 // sorting/opsTask-splitting-reduction mode) because it will depend upon this resolve task.
Chris Dalton3d770272019-08-14 09:24:37 -0600735 // NOTE: Putting it here will also reduce the amount of work required by the topological sort.
Chris Daltone2a903e2019-09-18 13:41:50 -0600736 return static_cast<GrTextureResolveRenderTask*>(fDAG.addBeforeLast(
737 sk_make_sp<GrTextureResolveRenderTask>()));
Chris Dalton3d770272019-08-14 09:24:37 -0600738}
739
Greg Danielc30f1a92019-09-06 15:28:58 -0400740void GrDrawingManager::newWaitRenderTask(sk_sp<GrSurfaceProxy> proxy,
Greg Daniel301015c2019-11-18 14:06:46 -0500741 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> semaphores,
Greg Danielc30f1a92019-09-06 15:28:58 -0400742 int numSemaphores) {
743 SkDEBUGCODE(this->validate());
744 SkASSERT(fContext);
745
746 const GrCaps& caps = *fContext->priv().caps();
747
Greg Daniel16f5c652019-10-29 11:26:01 -0400748 sk_sp<GrWaitRenderTask> waitTask = sk_make_sp<GrWaitRenderTask>(GrSurfaceProxyView(proxy),
749 std::move(semaphores),
Greg Danielc30f1a92019-09-06 15:28:58 -0400750 numSemaphores);
751 if (fReduceOpsTaskSplitting) {
752 GrRenderTask* lastTask = proxy->getLastRenderTask();
753 if (lastTask && !lastTask->isClosed()) {
754 // We directly make the currently open renderTask depend on waitTask instead of using
755 // the proxy version of addDependency. The waitTask will never need to trigger any
756 // resolves or mip map generation which is the main advantage of going through the proxy
757 // version. Additionally we would've had to temporarily set the wait task as the
758 // lastRenderTask on the proxy, add the dependency, and then reset the lastRenderTask to
759 // lastTask. Additionally we add all dependencies of lastTask to waitTask so that the
760 // waitTask doesn't get reordered before them and unnecessarily block those tasks.
761 // Note: Any previous Ops already in lastTask will get blocked by the wait semaphore
762 // even though they don't need to be for correctness.
763
764 // Make sure we add the dependencies of lastTask to waitTask first or else we'll get a
765 // circular self dependency of waitTask on waitTask.
766 waitTask->addDependenciesFromOtherTask(lastTask);
767 lastTask->addDependency(waitTask.get());
768 } else {
769 // If there is a last task we set the waitTask to depend on it so that it doesn't get
770 // reordered in front of the lastTask causing the lastTask to be blocked by the
771 // semaphore. Again we directly just go through adding the dependency to the task and
772 // not the proxy since we don't need to worry about resolving anything.
773 if (lastTask) {
774 waitTask->addDependency(lastTask);
775 }
776 proxy->setLastRenderTask(waitTask.get());
777 }
778 fDAG.add(waitTask);
779 } else {
Greg Daniel16f5c652019-10-29 11:26:01 -0400780 if (fActiveOpsTask && (fActiveOpsTask->fTargetView.proxy() == proxy.get())) {
Greg Danielc30f1a92019-09-06 15:28:58 -0400781 SkASSERT(proxy->getLastRenderTask() == fActiveOpsTask);
782 fDAG.addBeforeLast(waitTask);
783 // In this case we keep the current renderTask open but just insert the new waitTask
784 // before it in the list. The waitTask will never need to trigger any resolves or mip
785 // map generation which is the main advantage of going through the proxy version.
786 // Additionally we would've had to temporarily set the wait task as the lastRenderTask
787 // on the proxy, add the dependency, and then reset the lastRenderTask to
788 // fActiveOpsTask. Additionally we make the waitTask depend on all of fActiveOpsTask
789 // dependencies so that we don't unnecessarily reorder the waitTask before them.
790 // Note: Any previous Ops already in fActiveOpsTask will get blocked by the wait
791 // semaphore even though they don't need to be for correctness.
792
793 // Make sure we add the dependencies of fActiveOpsTask to waitTask first or else we'll
794 // get a circular self dependency of waitTask on waitTask.
795 waitTask->addDependenciesFromOtherTask(fActiveOpsTask);
796 fActiveOpsTask->addDependency(waitTask.get());
797 } else {
798 // In this case we just close the previous RenderTask and start and append the waitTask
799 // to the DAG. Since it is the last task now we call setLastRenderTask on the proxy. If
800 // there is a lastTask on the proxy we make waitTask depend on that task. This
801 // dependency isn't strictly needed but it does keep the DAG from reordering the
802 // waitTask earlier and blocking more tasks.
803 if (GrRenderTask* lastTask = proxy->getLastRenderTask()) {
804 waitTask->addDependency(lastTask);
805 }
806 proxy->setLastRenderTask(waitTask.get());
807 this->closeRenderTasksForNewRenderTask(proxy.get());
808 fDAG.add(waitTask);
809 }
810 }
811 waitTask->makeClosed(caps);
812
813 SkDEBUGCODE(this->validate());
814}
815
Greg Danielbbfec9d2019-08-20 10:56:51 -0400816void GrDrawingManager::newTransferFromRenderTask(sk_sp<GrSurfaceProxy> srcProxy,
817 const SkIRect& srcRect,
818 GrColorType surfaceColorType,
819 GrColorType dstColorType,
820 sk_sp<GrGpuBuffer> dstBuffer,
821 size_t dstOffset) {
822 SkDEBUGCODE(this->validate());
823 SkASSERT(fContext);
824 // This copies from srcProxy to dstBuffer so it doesn't have a real target.
825 this->closeRenderTasksForNewRenderTask(nullptr);
826
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600827 GrRenderTask* task = fDAG.add(sk_make_sp<GrTransferFromRenderTask>(
828 srcProxy, srcRect, surfaceColorType, dstColorType, std::move(dstBuffer), dstOffset));
Greg Danielbbfec9d2019-08-20 10:56:51 -0400829
830 const GrCaps& caps = *fContext->priv().caps();
831
832 // We always say GrMipMapped::kNo here since we are always just copying from the base layer. We
833 // don't need to make sure the whole mip map chain is valid.
834 task->addDependency(srcProxy.get(), GrMipMapped::kNo, GrTextureResolveManager(this), caps);
835 task->makeClosed(caps);
836
Greg Danielbbfec9d2019-08-20 10:56:51 -0400837 // We have closed the previous active oplist but since a new oplist isn't being added there
838 // shouldn't be an active one.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400839 SkASSERT(!fActiveOpsTask);
Greg Danielbbfec9d2019-08-20 10:56:51 -0400840 SkDEBUGCODE(this->validate());
841}
842
Greg Daniel16f5c652019-10-29 11:26:01 -0400843bool GrDrawingManager::newCopyRenderTask(GrSurfaceProxyView srcView,
Greg Daniele227fe42019-08-21 13:52:24 -0400844 const SkIRect& srcRect,
Greg Daniel16f5c652019-10-29 11:26:01 -0400845 GrSurfaceProxyView dstView,
Greg Daniele227fe42019-08-21 13:52:24 -0400846 const SkIPoint& dstPoint) {
847 SkDEBUGCODE(this->validate());
848 SkASSERT(fContext);
Greg Daniele227fe42019-08-21 13:52:24 -0400849
Greg Daniel16f5c652019-10-29 11:26:01 -0400850 this->closeRenderTasksForNewRenderTask(dstView.proxy());
Brian Salomone4bce012019-09-20 15:34:23 -0400851 const GrCaps& caps = *fContext->priv().caps();
852
Greg Daniel16f5c652019-10-29 11:26:01 -0400853 GrSurfaceProxy* srcProxy = srcView.proxy();
854
Brian Salomone4bce012019-09-20 15:34:23 -0400855 GrRenderTask* task =
Greg Daniel16f5c652019-10-29 11:26:01 -0400856 fDAG.add(GrCopyRenderTask::Make(std::move(srcView), srcRect, std::move(dstView),
857 dstPoint, &caps));
Greg Daniele227fe42019-08-21 13:52:24 -0400858 if (!task) {
859 return false;
860 }
861
Greg Daniele227fe42019-08-21 13:52:24 -0400862 // We always say GrMipMapped::kNo here since we are always just copying from the base layer to
863 // another base layer. We don't need to make sure the whole mip map chain is valid.
Greg Daniel16f5c652019-10-29 11:26:01 -0400864 task->addDependency(srcProxy, GrMipMapped::kNo, GrTextureResolveManager(this), caps);
Greg Daniele227fe42019-08-21 13:52:24 -0400865 task->makeClosed(caps);
866
Greg Daniele227fe42019-08-21 13:52:24 -0400867 // We have closed the previous active oplist but since a new oplist isn't being added there
868 // shouldn't be an active one.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400869 SkASSERT(!fActiveOpsTask);
Greg Daniele227fe42019-08-21 13:52:24 -0400870 SkDEBUGCODE(this->validate());
871 return true;
872}
873
Herb Derby26cbe512018-05-24 14:39:01 -0400874GrTextContext* GrDrawingManager::getTextContext() {
875 if (!fTextContext) {
876 fTextContext = GrTextContext::Make(fOptionsForTextContext);
brianosman86e76262016-08-11 12:17:31 -0700877 }
878
Herb Derby26cbe512018-05-24 14:39:01 -0400879 return fTextContext.get();
brianosman86e76262016-08-11 12:17:31 -0700880}
881
robertphillips68737822015-10-29 12:12:21 -0700882/*
883 * This method finds a path renderer that can draw the specified path on
884 * the provided target.
885 * Due to its expense, the software path renderer has split out so it can
886 * can be individually allowed/disallowed via the "allowSW" boolean.
887 */
888GrPathRenderer* GrDrawingManager::getPathRenderer(const GrPathRenderer::CanDrawPathArgs& args,
889 bool allowSW,
890 GrPathRendererChain::DrawType drawType,
891 GrPathRenderer::StencilSupport* stencilSupport) {
892
893 if (!fPathRendererChain) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400894 fPathRendererChain.reset(new GrPathRendererChain(fContext, fOptionsForPathRendererChain));
robertphillips68737822015-10-29 12:12:21 -0700895 }
896
897 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(args, drawType, stencilSupport);
898 if (!pr && allowSW) {
Brian Salomone7df0bb2018-05-07 14:44:57 -0400899 auto swPR = this->getSoftwarePathRenderer();
900 if (GrPathRenderer::CanDrawPath::kNo != swPR->canDrawPath(args)) {
901 pr = swPR;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500902 }
robertphillips68737822015-10-29 12:12:21 -0700903 }
904
Robert Phillipsd81379d2020-04-21 10:39:02 -0400905#if GR_PATH_RENDERER_SPEW
906 if (pr) {
907 SkDebugf("getPathRenderer: %s\n", pr->name());
908 }
909#endif
910
robertphillips68737822015-10-29 12:12:21 -0700911 return pr;
912}
913
Brian Salomone7df0bb2018-05-07 14:44:57 -0400914GrPathRenderer* GrDrawingManager::getSoftwarePathRenderer() {
915 if (!fSoftwarePathRenderer) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400916 fSoftwarePathRenderer.reset(
Robert Phillips9da87e02019-02-04 13:26:26 -0500917 new GrSoftwarePathRenderer(fContext->priv().proxyProvider(),
Ben Wagner9ec70c62018-07-12 13:30:47 -0400918 fOptionsForPathRendererChain.fAllowPathMaskCaching));
Brian Salomone7df0bb2018-05-07 14:44:57 -0400919 }
Ben Wagner9ec70c62018-07-12 13:30:47 -0400920 return fSoftwarePathRenderer.get();
Brian Salomone7df0bb2018-05-07 14:44:57 -0400921}
922
Chris Daltonfddb6c02017-11-04 15:22:22 -0600923GrCoverageCountingPathRenderer* GrDrawingManager::getCoverageCountingPathRenderer() {
924 if (!fPathRendererChain) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400925 fPathRendererChain.reset(new GrPathRendererChain(fContext, fOptionsForPathRendererChain));
Chris Daltonfddb6c02017-11-04 15:22:22 -0600926 }
927 return fPathRendererChain->getCoverageCountingPathRenderer();
928}
929
Brian Salomon653f42f2018-07-10 10:07:31 -0400930void GrDrawingManager::flushIfNecessary() {
Robert Phillips6a6de562019-02-15 15:19:15 -0500931 auto direct = fContext->priv().asDirectContext();
932 if (!direct) {
933 return;
934 }
935
936 auto resourceCache = direct->priv().getResourceCache();
Brian Salomon653f42f2018-07-10 10:07:31 -0400937 if (resourceCache && resourceCache->requestsFlush()) {
Greg Danielfe159622020-04-10 17:43:51 +0000938 if (this->flush(nullptr, 0, SkSurface::BackendSurfaceAccess::kNoAccess, GrFlushInfo(),
939 GrPrepareForExternalIORequests())) {
940 this->submitToGpu(false);
941 }
Brian Salomon57d2beab2018-09-10 09:35:41 -0400942 resourceCache->purgeAsNeeded();
Brian Salomon653f42f2018-07-10 10:07:31 -0400943 }
944}
945