blob: ff5deb25adbe383f5383e6647d7e9adf1ce36c46 [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 Daniel797efca2019-05-09 14:04:20 -0400224GrSemaphoresSubmitted GrDrawingManager::flush(GrSurfaceProxy* proxies[], int numProxies,
225 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 Daniel51316782017-08-02 15:10:09 +0000235 return GrSemaphoresSubmitted::kNo;
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) {
247 return GrSemaphoresSubmitted::kNo;
248 }
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 }
Robert Phillips6a6de562019-02-15 15:19:15 -0500256 return GrSemaphoresSubmitted::kNo; // Can't flush while DDL recording
257 }
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 }
Robert Phillips874b5352018-03-16 08:48:24 -0400265 return GrSemaphoresSubmitted::kNo; // Can't flush while DDL recording
266 }
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 Daniel797efca2019-05-09 14:04:20 -0400397 GrSemaphoresSubmitted result = gpu->finishFlush(proxies, numProxies, access, info,
398 externalRequests);
robertphillipsa13e2022015-11-11 12:01:09 -0800399
Brian Salomon57d2beab2018-09-10 09:35:41 -0400400 // Give the cache a chance to purge resources that become purgeable due to flushing.
401 if (flushed) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500402 resourceCache->purgeAsNeeded();
Brian Salomon876a0172019-03-08 11:12:14 -0500403 flushed = false;
bsalomonb77a9072016-09-07 10:02:04 -0700404 }
Chris Daltonfe199b72017-05-05 11:26:15 -0400405 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
Chris Dalton6b498102019-08-01 14:14:52 -0600406 onFlushCBObject->postFlush(fTokenTracker.nextTokenToFlush(), fFlushingRenderTaskIDs.begin(),
407 fFlushingRenderTaskIDs.count());
Brian Salomon876a0172019-03-08 11:12:14 -0500408 flushed = true;
409 }
410 if (flushed) {
411 resourceCache->purgeAsNeeded();
Chris Daltonfe199b72017-05-05 11:26:15 -0400412 }
Chris Dalton6b498102019-08-01 14:14:52 -0600413 fFlushingRenderTaskIDs.reset();
joshualittb8918c42015-12-18 09:59:46 -0800414 fFlushing = false;
Greg Daniel51316782017-08-02 15:10:09 +0000415
416 return result;
robertphillips3dc6ae52015-10-20 09:54:32 -0700417}
418
Chris Dalton6b498102019-08-01 14:14:52 -0600419bool GrDrawingManager::executeRenderTasks(int startIndex, int stopIndex, GrOpFlushState* flushState,
420 int* numRenderTasksExecuted) {
421 SkASSERT(startIndex <= stopIndex && stopIndex <= fDAG.numRenderTasks());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500422
Robert Phillips27483912018-04-20 12:43:18 -0400423#if GR_FLUSH_TIME_OP_SPEW
Greg Danielf41b2bd2019-08-22 16:19:24 -0400424 SkDebugf("Flushing opsTask: %d to %d out of [%d, %d]\n",
Chris Dalton6b498102019-08-01 14:14:52 -0600425 startIndex, stopIndex, 0, fDAG.numRenderTasks());
Robert Phillips27483912018-04-20 12:43:18 -0400426 for (int i = startIndex; i < stopIndex; ++i) {
Chris Dalton6b498102019-08-01 14:14:52 -0600427 if (fDAG.renderTask(i)) {
428 fDAG.renderTask(i)->dump(true);
Robert Phillips1734dd32018-08-21 13:52:09 -0400429 }
Robert Phillips27483912018-04-20 12:43:18 -0400430 }
431#endif
432
Chris Dalton6b498102019-08-01 14:14:52 -0600433 bool anyRenderTasksExecuted = false;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500434
435 for (int i = startIndex; i < stopIndex; ++i) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400436 GrRenderTask* renderTask = fDAG.renderTask(i);
437 if (!renderTask || !renderTask->isInstantiated()) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500438 continue;
439 }
440
Chris Dalton6b498102019-08-01 14:14:52 -0600441 SkASSERT(renderTask->deferredProxiesAreInstantiated());
Robert Phillips22310d62018-09-05 11:07:21 -0400442
Chris Dalton6b498102019-08-01 14:14:52 -0600443 renderTask->prepare(flushState);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500444 }
445
446 // Upload all data to the GPU
447 flushState->preExecuteDraws();
448
Greg Danield2073452018-12-07 11:20:33 -0500449 // For Vulkan, if we have too many oplists to be flushed we end up allocating a lot of resources
450 // for each command buffer associated with the oplists. If this gets too large we can cause the
451 // devices to go OOM. In practice we usually only hit this case in our tests, but to be safe we
452 // put a cap on the number of oplists we will execute before flushing to the GPU to relieve some
453 // memory pressure.
Chris Dalton6b498102019-08-01 14:14:52 -0600454 static constexpr int kMaxRenderTasksBeforeFlush = 100;
Greg Danield2073452018-12-07 11:20:33 -0500455
Chris Daltonc4b47352019-08-23 10:10:36 -0600456 // Execute the onFlush renderTasks first, if any.
457 for (sk_sp<GrRenderTask>& onFlushRenderTask : fOnFlushRenderTasks) {
458 if (!onFlushRenderTask->execute(flushState)) {
459 SkDebugf("WARNING: onFlushRenderTask failed to execute.\n");
Robert Phillipseafd48a2017-11-16 07:52:08 -0500460 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600461 SkASSERT(onFlushRenderTask->unique());
462 onFlushRenderTask = nullptr;
Chris Dalton6b498102019-08-01 14:14:52 -0600463 (*numRenderTasksExecuted)++;
464 if (*numRenderTasksExecuted >= kMaxRenderTasksBeforeFlush) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400465 flushState->gpu()->finishFlush(nullptr, 0, SkSurface::BackendSurfaceAccess::kNoAccess,
Greg Daniel797efca2019-05-09 14:04:20 -0400466 GrFlushInfo(), GrPrepareForExternalIORequests());
Chris Dalton6b498102019-08-01 14:14:52 -0600467 *numRenderTasksExecuted = 0;
Greg Danield2073452018-12-07 11:20:33 -0500468 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500469 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600470 fOnFlushRenderTasks.reset();
Robert Phillipseafd48a2017-11-16 07:52:08 -0500471
472 // Execute the normal op lists.
473 for (int i = startIndex; i < stopIndex; ++i) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400474 GrRenderTask* renderTask = fDAG.renderTask(i);
475 if (!renderTask || !renderTask->isInstantiated()) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500476 continue;
477 }
478
Greg Daniel15ecdf92019-08-30 15:35:23 -0400479 if (renderTask->execute(flushState)) {
Chris Dalton6b498102019-08-01 14:14:52 -0600480 anyRenderTasksExecuted = true;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500481 }
Chris Dalton6b498102019-08-01 14:14:52 -0600482 (*numRenderTasksExecuted)++;
483 if (*numRenderTasksExecuted >= kMaxRenderTasksBeforeFlush) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400484 flushState->gpu()->finishFlush(nullptr, 0, SkSurface::BackendSurfaceAccess::kNoAccess,
Greg Daniel797efca2019-05-09 14:04:20 -0400485 GrFlushInfo(), GrPrepareForExternalIORequests());
Chris Dalton6b498102019-08-01 14:14:52 -0600486 *numRenderTasksExecuted = 0;
Greg Danield2073452018-12-07 11:20:33 -0500487 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500488 }
489
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400490 SkASSERT(!flushState->opsRenderPass());
Robert Phillips40a29d72018-01-18 12:59:22 -0500491 SkASSERT(fTokenTracker.nextDrawToken() == fTokenTracker.nextTokenToFlush());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500492
Chris Dalton6b498102019-08-01 14:14:52 -0600493 // We reset the flush state before the RenderTasks so that the last resources to be freed are
494 // those that are written to in the RenderTasks. This helps to make sure the most recently used
495 // resources are the last to be purged by the resource cache.
Robert Phillipseafd48a2017-11-16 07:52:08 -0500496 flushState->reset();
497
Chris Dalton6b498102019-08-01 14:14:52 -0600498 fDAG.removeRenderTasks(startIndex, stopIndex);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500499
Chris Dalton6b498102019-08-01 14:14:52 -0600500 return anyRenderTasksExecuted;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500501}
502
Greg Daniel55f040b2020-02-13 15:38:32 +0000503GrSemaphoresSubmitted GrDrawingManager::flushSurfaces(GrSurfaceProxy* proxies[], int numProxies,
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400504 SkSurface::BackendSurfaceAccess access,
505 const GrFlushInfo& info) {
bsalomon6a2b1942016-09-08 11:28:59 -0700506 if (this->wasAbandoned()) {
Greg Daniel51316782017-08-02 15:10:09 +0000507 return GrSemaphoresSubmitted::kNo;
bsalomon6a2b1942016-09-08 11:28:59 -0700508 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400509 SkDEBUGCODE(this->validate());
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400510 SkASSERT(numProxies >= 0);
511 SkASSERT(!numProxies || proxies);
bsalomon6a2b1942016-09-08 11:28:59 -0700512
Robert Phillips6a6de562019-02-15 15:19:15 -0500513 auto direct = fContext->priv().asDirectContext();
514 if (!direct) {
515 return GrSemaphoresSubmitted::kNo; // Can't flush while DDL recording
516 }
517
518 GrGpu* gpu = direct->priv().getGpu();
Robert Phillips874b5352018-03-16 08:48:24 -0400519 if (!gpu) {
520 return GrSemaphoresSubmitted::kNo; // Can't flush while DDL recording
521 }
522
Robert Phillipsacc10fa2019-04-01 09:50:20 -0400523 // TODO: It is important to upgrade the drawingmanager to just flushing the
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400524 // portion of the DAG required by 'proxies' in order to restore some of the
Robert Phillipsacc10fa2019-04-01 09:50:20 -0400525 // semantics of this method.
Greg Daniel797efca2019-05-09 14:04:20 -0400526 GrSemaphoresSubmitted result = this->flush(proxies, numProxies, access, info,
527 GrPrepareForExternalIORequests());
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400528 for (int i = 0; i < numProxies; ++i) {
Chris Dalton4ece96d2019-08-30 11:26:39 -0600529 GrSurfaceProxy* proxy = proxies[i];
530 if (!proxy->isInstantiated()) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400531 return result;
532 }
Chris Dalton4ece96d2019-08-30 11:26:39 -0600533 // In the flushSurfaces case, we need to resolve MSAA immediately after flush. This is
534 // because the client will call through to this method when drawing into a target created by
535 // wrapBackendTextureAsRenderTarget, and will expect the original texture to be fully
536 // resolved upon return.
537 if (proxy->requiresManualMSAAResolve()) {
538 auto* rtProxy = proxy->asRenderTargetProxy();
539 SkASSERT(rtProxy);
540 if (rtProxy->isMSAADirty()) {
541 SkASSERT(rtProxy->peekRenderTarget());
Chris Dalton16a33c62019-09-24 22:19:17 -0600542 gpu->resolveRenderTarget(rtProxy->peekRenderTarget(), rtProxy->msaaDirtyRect(),
Greg Daniel242536f2020-02-13 14:12:46 -0500543 GrGpu::ForExternalIO::kYes);
Chris Dalton4ece96d2019-08-30 11:26:39 -0600544 rtProxy->markMSAAResolved();
545 }
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400546 }
Chris Dalton3d770272019-08-14 09:24:37 -0600547 // If, after a flush, any of the proxies of interest have dirty mipmaps, regenerate them in
548 // case their backend textures are being stolen.
549 // (This special case is exercised by the ReimportImageTextureWithMipLevels test.)
550 // 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 -0600551 if (auto* textureProxy = proxy->asTextureProxy()) {
Chris Dalton3d770272019-08-14 09:24:37 -0600552 if (textureProxy->mipMapsAreDirty()) {
Chris Dalton4ece96d2019-08-30 11:26:39 -0600553 SkASSERT(textureProxy->peekTexture());
Chris Dalton3d770272019-08-14 09:24:37 -0600554 gpu->regenerateMipMapLevels(textureProxy->peekTexture());
555 textureProxy->markMipMapsClean();
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400556 }
Brian Salomon930f9392018-06-20 16:25:26 -0400557 }
bsalomon6a2b1942016-09-08 11:28:59 -0700558 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400559
560 SkDEBUGCODE(this->validate());
Greg Daniel51316782017-08-02 15:10:09 +0000561 return result;
bsalomon6a2b1942016-09-08 11:28:59 -0700562}
563
Chris Daltonfe199b72017-05-05 11:26:15 -0400564void GrDrawingManager::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
565 fOnFlushCBObjects.push_back(onFlushCBObject);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400566}
567
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500568#if GR_TEST_UTILS
569void GrDrawingManager::testingOnly_removeOnFlushCallbackObject(GrOnFlushCallbackObject* cb) {
570 int n = std::find(fOnFlushCBObjects.begin(), fOnFlushCBObjects.end(), cb) -
571 fOnFlushCBObjects.begin();
572 SkASSERT(n < fOnFlushCBObjects.count());
573 fOnFlushCBObjects.removeShuffle(n);
574}
575#endif
576
Chris Dalton6b498102019-08-01 14:14:52 -0600577void GrDrawingManager::moveRenderTasksToDDL(SkDeferredDisplayList* ddl) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400578 SkDEBUGCODE(this->validate());
579
Chris Dalton6b498102019-08-01 14:14:52 -0600580 // no renderTask should receive a new command after this
Robert Phillips9da87e02019-02-04 13:26:26 -0500581 fDAG.closeAll(fContext->priv().caps());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400582 fActiveOpsTask = nullptr;
Robert Phillips7a137052018-02-01 11:23:12 -0500583
Chris Dalton6b498102019-08-01 14:14:52 -0600584 fDAG.swap(&ddl->fRenderTasks);
Robert Phillips19f466d2020-02-26 10:27:07 -0500585 SkASSERT(!fDAG.numRenderTasks());
Robert Phillips867ce8f2018-06-21 10:28:36 -0400586
Robert Phillips19f466d2020-02-26 10:27:07 -0500587 for (auto& renderTask : ddl->fRenderTasks) {
Robert Phillips29f38542019-10-16 09:20:25 -0400588 renderTask->prePrepare(fContext);
Robert Phillips7327c9d2019-10-08 16:32:56 -0400589 }
590
Michael Ludwig2c316bd2019-12-19 14:50:44 -0500591 ddl->fArenas = std::move(fContext->priv().detachArenas());
Robert Phillips61fc7992019-10-22 11:58:17 -0400592
Robert Phillipsf6a0b452020-02-18 14:26:46 -0500593 fContext->priv().detachProgramData(&ddl->fProgramData);
Robert Phillips576b6a12019-12-06 13:05:49 -0500594
Robert Phillips774168e2018-05-31 12:43:27 -0400595 if (fPathRendererChain) {
596 if (auto ccpr = fPathRendererChain->getCoverageCountingPathRenderer()) {
597 ddl->fPendingPaths = ccpr->detachPendingPaths();
598 }
599 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400600
601 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500602}
603
Chris Dalton6b498102019-08-01 14:14:52 -0600604void GrDrawingManager::copyRenderTasksFromDDL(const SkDeferredDisplayList* ddl,
Robert Phillips933484f2019-11-26 09:38:55 -0500605 GrRenderTargetProxy* newDest) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400606 SkDEBUGCODE(this->validate());
607
Greg Danielf41b2bd2019-08-22 16:19:24 -0400608 if (fActiveOpsTask) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400609 // This is a temporary fix for the partial-MDB world. In that world we're not
Greg Danielf41b2bd2019-08-22 16:19:24 -0400610 // reordering so ops that (in the single opsTask world) would've just glommed onto the
611 // end of the single opsTask but referred to a far earlier RT need to appear in their
612 // own opsTask.
613 fActiveOpsTask->makeClosed(*fContext->priv().caps());
614 fActiveOpsTask = nullptr;
Robert Phillips38d64b02018-09-04 13:23:26 -0400615 }
616
Robert Phillips15c91422019-05-07 16:54:48 -0400617 this->addDDLTarget(newDest);
618
Robert Phillips62000362018-02-01 09:10:04 -0500619 // Here we jam the proxy that backs the current replay SkSurface into the LazyProxyData.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400620 // The lazy proxy that references it (in the copied opsTasks) will steal its GrTexture.
Robert Phillips62000362018-02-01 09:10:04 -0500621 ddl->fLazyProxyData->fReplayDest = newDest;
Robert Phillips774168e2018-05-31 12:43:27 -0400622
623 if (ddl->fPendingPaths.size()) {
624 GrCoverageCountingPathRenderer* ccpr = this->getCoverageCountingPathRenderer();
625
626 ccpr->mergePendingPaths(ddl->fPendingPaths);
627 }
Robert Phillips22310d62018-09-05 11:07:21 -0400628
Chris Dalton6b498102019-08-01 14:14:52 -0600629 fDAG.add(ddl->fRenderTasks);
Robert Phillips38d64b02018-09-04 13:23:26 -0400630
631 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500632}
633
Robert Phillips38d64b02018-09-04 13:23:26 -0400634#ifdef SK_DEBUG
635void GrDrawingManager::validate() const {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400636 if (fDAG.sortingRenderTasks() && fReduceOpsTaskSplitting) {
637 SkASSERT(!fActiveOpsTask);
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400638 } else {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400639 if (fActiveOpsTask) {
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400640 SkASSERT(!fDAG.empty());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400641 SkASSERT(!fActiveOpsTask->isClosed());
642 SkASSERT(fActiveOpsTask == fDAG.back());
Robert Phillips38d64b02018-09-04 13:23:26 -0400643 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400644
Chris Dalton6b498102019-08-01 14:14:52 -0600645 for (int i = 0; i < fDAG.numRenderTasks(); ++i) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400646 if (fActiveOpsTask != fDAG.renderTask(i)) {
Chris Daltone2a903e2019-09-18 13:41:50 -0600647 // The resolveTask associated with the activeTask remains open for as long as the
648 // activeTask does.
649 bool isActiveResolveTask =
650 fActiveOpsTask && fActiveOpsTask->fTextureResolveTask == fDAG.renderTask(i);
651 SkASSERT(isActiveResolveTask || fDAG.renderTask(i)->isClosed());
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400652 }
653 }
654
655 if (!fDAG.empty() && !fDAG.back()->isClosed()) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400656 SkASSERT(fActiveOpsTask == fDAG.back());
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400657 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400658 }
659}
660#endif
661
Greg Danielbbfec9d2019-08-20 10:56:51 -0400662void GrDrawingManager::closeRenderTasksForNewRenderTask(GrSurfaceProxy* target) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400663 if (target && fDAG.sortingRenderTasks() && fReduceOpsTaskSplitting) {
Chris Dalton5fe99772019-08-06 11:57:39 -0600664 // In this case we need to close all the renderTasks that rely on the current contents of
665 // 'target'. That is bc we're going to update the content of the proxy so they need to be
666 // split in case they use both the old and new content. (This is a bit of an overkill: they
667 // really only need to be split if they ever reference proxy's contents again but that is
668 // hard to predict/handle).
669 if (GrRenderTask* lastRenderTask = target->getLastRenderTask()) {
Chris Dalton6b498102019-08-01 14:14:52 -0600670 lastRenderTask->closeThoseWhoDependOnMe(*fContext->priv().caps());
Robert Phillips46acf9d2018-10-09 09:31:40 -0400671 }
Greg Danielf41b2bd2019-08-22 16:19:24 -0400672 } else if (fActiveOpsTask) {
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400673 // This is a temporary fix for the partial-MDB world. In that world we're not
Greg Danielf41b2bd2019-08-22 16:19:24 -0400674 // reordering so ops that (in the single opsTask world) would've just glommed onto the
675 // end of the single opsTask but referred to a far earlier RT need to appear in their
676 // own opsTask.
677 fActiveOpsTask->makeClosed(*fContext->priv().caps());
678 fActiveOpsTask = nullptr;
robertphillips3dc6ae52015-10-20 09:54:32 -0700679 }
Chris Dalton5fe99772019-08-06 11:57:39 -0600680}
681
Greg Daniel16f5c652019-10-29 11:26:01 -0400682sk_sp<GrOpsTask> GrDrawingManager::newOpsTask(GrSurfaceProxyView surfaceView,
683 bool managedOpsTask) {
Chris Dalton5fe99772019-08-06 11:57:39 -0600684 SkDEBUGCODE(this->validate());
685 SkASSERT(fContext);
686
Greg Daniel16f5c652019-10-29 11:26:01 -0400687 GrSurfaceProxy* proxy = surfaceView.proxy();
688 this->closeRenderTasksForNewRenderTask(proxy);
robertphillips3dc6ae52015-10-20 09:54:32 -0700689
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500690 sk_sp<GrOpsTask> opsTask(new GrOpsTask(fContext->priv().arenas(),
Michael Ludwigd0840ec2019-12-12 09:48:38 -0500691 std::move(surfaceView),
692 fContext->priv().auditTrail()));
Greg Daniel16f5c652019-10-29 11:26:01 -0400693 SkASSERT(proxy->getLastRenderTask() == opsTask.get());
robertphillips3dc6ae52015-10-20 09:54:32 -0700694
Greg Danielf41b2bd2019-08-22 16:19:24 -0400695 if (managedOpsTask) {
696 fDAG.add(opsTask);
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400697
Greg Danielf41b2bd2019-08-22 16:19:24 -0400698 if (!fDAG.sortingRenderTasks() || !fReduceOpsTaskSplitting) {
699 fActiveOpsTask = opsTask.get();
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400700 }
Robert Phillips941d1442017-06-14 16:37:02 -0400701 }
robertphillips3dc6ae52015-10-20 09:54:32 -0700702
Robert Phillips38d64b02018-09-04 13:23:26 -0400703 SkDEBUGCODE(this->validate());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400704 return opsTask;
robertphillips3dc6ae52015-10-20 09:54:32 -0700705}
706
Chris Daltone2a903e2019-09-18 13:41:50 -0600707GrTextureResolveRenderTask* GrDrawingManager::newTextureResolveRenderTask(const GrCaps& caps) {
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600708 // 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 -0600709 // in sorting and opsTask reduction mode) the render tasks that depend on any proxy's current
Greg Danielf41b2bd2019-08-22 16:19:24 -0400710 // state. This is because those opsTasks can still receive new ops and because if they refer to
Chris Dalton4ece96d2019-08-30 11:26:39 -0600711 // the mipmapped version of 'proxy', they will then come to depend on the render task being
712 // created here.
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600713 //
Greg Danielf41b2bd2019-08-22 16:19:24 -0400714 // Add the new textureResolveTask before the fActiveOpsTask (if not in
715 // sorting/opsTask-splitting-reduction mode) because it will depend upon this resolve task.
Chris Dalton3d770272019-08-14 09:24:37 -0600716 // NOTE: Putting it here will also reduce the amount of work required by the topological sort.
Chris Daltone2a903e2019-09-18 13:41:50 -0600717 return static_cast<GrTextureResolveRenderTask*>(fDAG.addBeforeLast(
718 sk_make_sp<GrTextureResolveRenderTask>()));
Chris Dalton3d770272019-08-14 09:24:37 -0600719}
720
Greg Danielc30f1a92019-09-06 15:28:58 -0400721void GrDrawingManager::newWaitRenderTask(sk_sp<GrSurfaceProxy> proxy,
Greg Daniel301015c2019-11-18 14:06:46 -0500722 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> semaphores,
Greg Danielc30f1a92019-09-06 15:28:58 -0400723 int numSemaphores) {
724 SkDEBUGCODE(this->validate());
725 SkASSERT(fContext);
726
727 const GrCaps& caps = *fContext->priv().caps();
728
Greg Daniel16f5c652019-10-29 11:26:01 -0400729 sk_sp<GrWaitRenderTask> waitTask = sk_make_sp<GrWaitRenderTask>(GrSurfaceProxyView(proxy),
730 std::move(semaphores),
Greg Danielc30f1a92019-09-06 15:28:58 -0400731 numSemaphores);
732 if (fReduceOpsTaskSplitting) {
733 GrRenderTask* lastTask = proxy->getLastRenderTask();
734 if (lastTask && !lastTask->isClosed()) {
735 // We directly make the currently open renderTask depend on waitTask instead of using
736 // the proxy version of addDependency. The waitTask will never need to trigger any
737 // resolves or mip map generation which is the main advantage of going through the proxy
738 // version. Additionally we would've had to temporarily set the wait task as the
739 // lastRenderTask on the proxy, add the dependency, and then reset the lastRenderTask to
740 // lastTask. Additionally we add all dependencies of lastTask to waitTask so that the
741 // waitTask doesn't get reordered before them and unnecessarily block those tasks.
742 // Note: Any previous Ops already in lastTask will get blocked by the wait semaphore
743 // even though they don't need to be for correctness.
744
745 // Make sure we add the dependencies of lastTask to waitTask first or else we'll get a
746 // circular self dependency of waitTask on waitTask.
747 waitTask->addDependenciesFromOtherTask(lastTask);
748 lastTask->addDependency(waitTask.get());
749 } else {
750 // If there is a last task we set the waitTask to depend on it so that it doesn't get
751 // reordered in front of the lastTask causing the lastTask to be blocked by the
752 // semaphore. Again we directly just go through adding the dependency to the task and
753 // not the proxy since we don't need to worry about resolving anything.
754 if (lastTask) {
755 waitTask->addDependency(lastTask);
756 }
757 proxy->setLastRenderTask(waitTask.get());
758 }
759 fDAG.add(waitTask);
760 } else {
Greg Daniel16f5c652019-10-29 11:26:01 -0400761 if (fActiveOpsTask && (fActiveOpsTask->fTargetView.proxy() == proxy.get())) {
Greg Danielc30f1a92019-09-06 15:28:58 -0400762 SkASSERT(proxy->getLastRenderTask() == fActiveOpsTask);
763 fDAG.addBeforeLast(waitTask);
764 // In this case we keep the current renderTask open but just insert the new waitTask
765 // before it in the list. The waitTask will never need to trigger any resolves or mip
766 // map generation which is the main advantage of going through the proxy version.
767 // Additionally we would've had to temporarily set the wait task as the lastRenderTask
768 // on the proxy, add the dependency, and then reset the lastRenderTask to
769 // fActiveOpsTask. Additionally we make the waitTask depend on all of fActiveOpsTask
770 // dependencies so that we don't unnecessarily reorder the waitTask before them.
771 // Note: Any previous Ops already in fActiveOpsTask will get blocked by the wait
772 // semaphore even though they don't need to be for correctness.
773
774 // Make sure we add the dependencies of fActiveOpsTask to waitTask first or else we'll
775 // get a circular self dependency of waitTask on waitTask.
776 waitTask->addDependenciesFromOtherTask(fActiveOpsTask);
777 fActiveOpsTask->addDependency(waitTask.get());
778 } else {
779 // In this case we just close the previous RenderTask and start and append the waitTask
780 // to the DAG. Since it is the last task now we call setLastRenderTask on the proxy. If
781 // there is a lastTask on the proxy we make waitTask depend on that task. This
782 // dependency isn't strictly needed but it does keep the DAG from reordering the
783 // waitTask earlier and blocking more tasks.
784 if (GrRenderTask* lastTask = proxy->getLastRenderTask()) {
785 waitTask->addDependency(lastTask);
786 }
787 proxy->setLastRenderTask(waitTask.get());
788 this->closeRenderTasksForNewRenderTask(proxy.get());
789 fDAG.add(waitTask);
790 }
791 }
792 waitTask->makeClosed(caps);
793
794 SkDEBUGCODE(this->validate());
795}
796
Greg Danielbbfec9d2019-08-20 10:56:51 -0400797void GrDrawingManager::newTransferFromRenderTask(sk_sp<GrSurfaceProxy> srcProxy,
798 const SkIRect& srcRect,
799 GrColorType surfaceColorType,
800 GrColorType dstColorType,
801 sk_sp<GrGpuBuffer> dstBuffer,
802 size_t dstOffset) {
803 SkDEBUGCODE(this->validate());
804 SkASSERT(fContext);
805 // This copies from srcProxy to dstBuffer so it doesn't have a real target.
806 this->closeRenderTasksForNewRenderTask(nullptr);
807
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600808 GrRenderTask* task = fDAG.add(sk_make_sp<GrTransferFromRenderTask>(
809 srcProxy, srcRect, surfaceColorType, dstColorType, std::move(dstBuffer), dstOffset));
Greg Danielbbfec9d2019-08-20 10:56:51 -0400810
811 const GrCaps& caps = *fContext->priv().caps();
812
813 // We always say GrMipMapped::kNo here since we are always just copying from the base layer. We
814 // don't need to make sure the whole mip map chain is valid.
815 task->addDependency(srcProxy.get(), GrMipMapped::kNo, GrTextureResolveManager(this), caps);
816 task->makeClosed(caps);
817
Greg Danielbbfec9d2019-08-20 10:56:51 -0400818 // We have closed the previous active oplist but since a new oplist isn't being added there
819 // shouldn't be an active one.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400820 SkASSERT(!fActiveOpsTask);
Greg Danielbbfec9d2019-08-20 10:56:51 -0400821 SkDEBUGCODE(this->validate());
822}
823
Greg Daniel16f5c652019-10-29 11:26:01 -0400824bool GrDrawingManager::newCopyRenderTask(GrSurfaceProxyView srcView,
Greg Daniele227fe42019-08-21 13:52:24 -0400825 const SkIRect& srcRect,
Greg Daniel16f5c652019-10-29 11:26:01 -0400826 GrSurfaceProxyView dstView,
Greg Daniele227fe42019-08-21 13:52:24 -0400827 const SkIPoint& dstPoint) {
828 SkDEBUGCODE(this->validate());
829 SkASSERT(fContext);
Greg Daniele227fe42019-08-21 13:52:24 -0400830
Greg Daniel16f5c652019-10-29 11:26:01 -0400831 this->closeRenderTasksForNewRenderTask(dstView.proxy());
Brian Salomone4bce012019-09-20 15:34:23 -0400832 const GrCaps& caps = *fContext->priv().caps();
833
Greg Daniel16f5c652019-10-29 11:26:01 -0400834 GrSurfaceProxy* srcProxy = srcView.proxy();
835
Brian Salomone4bce012019-09-20 15:34:23 -0400836 GrRenderTask* task =
Greg Daniel16f5c652019-10-29 11:26:01 -0400837 fDAG.add(GrCopyRenderTask::Make(std::move(srcView), srcRect, std::move(dstView),
838 dstPoint, &caps));
Greg Daniele227fe42019-08-21 13:52:24 -0400839 if (!task) {
840 return false;
841 }
842
Greg Daniele227fe42019-08-21 13:52:24 -0400843 // We always say GrMipMapped::kNo here since we are always just copying from the base layer to
844 // another base layer. We don't need to make sure the whole mip map chain is valid.
Greg Daniel16f5c652019-10-29 11:26:01 -0400845 task->addDependency(srcProxy, GrMipMapped::kNo, GrTextureResolveManager(this), caps);
Greg Daniele227fe42019-08-21 13:52:24 -0400846 task->makeClosed(caps);
847
Greg Daniele227fe42019-08-21 13:52:24 -0400848 // We have closed the previous active oplist but since a new oplist isn't being added there
849 // shouldn't be an active one.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400850 SkASSERT(!fActiveOpsTask);
Greg Daniele227fe42019-08-21 13:52:24 -0400851 SkDEBUGCODE(this->validate());
852 return true;
853}
854
Herb Derby26cbe512018-05-24 14:39:01 -0400855GrTextContext* GrDrawingManager::getTextContext() {
856 if (!fTextContext) {
857 fTextContext = GrTextContext::Make(fOptionsForTextContext);
brianosman86e76262016-08-11 12:17:31 -0700858 }
859
Herb Derby26cbe512018-05-24 14:39:01 -0400860 return fTextContext.get();
brianosman86e76262016-08-11 12:17:31 -0700861}
862
robertphillips68737822015-10-29 12:12:21 -0700863/*
864 * This method finds a path renderer that can draw the specified path on
865 * the provided target.
866 * Due to its expense, the software path renderer has split out so it can
867 * can be individually allowed/disallowed via the "allowSW" boolean.
868 */
869GrPathRenderer* GrDrawingManager::getPathRenderer(const GrPathRenderer::CanDrawPathArgs& args,
870 bool allowSW,
871 GrPathRendererChain::DrawType drawType,
872 GrPathRenderer::StencilSupport* stencilSupport) {
873
874 if (!fPathRendererChain) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400875 fPathRendererChain.reset(new GrPathRendererChain(fContext, fOptionsForPathRendererChain));
robertphillips68737822015-10-29 12:12:21 -0700876 }
877
878 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(args, drawType, stencilSupport);
879 if (!pr && allowSW) {
Brian Salomone7df0bb2018-05-07 14:44:57 -0400880 auto swPR = this->getSoftwarePathRenderer();
881 if (GrPathRenderer::CanDrawPath::kNo != swPR->canDrawPath(args)) {
882 pr = swPR;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500883 }
robertphillips68737822015-10-29 12:12:21 -0700884 }
885
886 return pr;
887}
888
Brian Salomone7df0bb2018-05-07 14:44:57 -0400889GrPathRenderer* GrDrawingManager::getSoftwarePathRenderer() {
890 if (!fSoftwarePathRenderer) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400891 fSoftwarePathRenderer.reset(
Robert Phillips9da87e02019-02-04 13:26:26 -0500892 new GrSoftwarePathRenderer(fContext->priv().proxyProvider(),
Ben Wagner9ec70c62018-07-12 13:30:47 -0400893 fOptionsForPathRendererChain.fAllowPathMaskCaching));
Brian Salomone7df0bb2018-05-07 14:44:57 -0400894 }
Ben Wagner9ec70c62018-07-12 13:30:47 -0400895 return fSoftwarePathRenderer.get();
Brian Salomone7df0bb2018-05-07 14:44:57 -0400896}
897
Chris Daltonfddb6c02017-11-04 15:22:22 -0600898GrCoverageCountingPathRenderer* GrDrawingManager::getCoverageCountingPathRenderer() {
899 if (!fPathRendererChain) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400900 fPathRendererChain.reset(new GrPathRendererChain(fContext, fOptionsForPathRendererChain));
Chris Daltonfddb6c02017-11-04 15:22:22 -0600901 }
902 return fPathRendererChain->getCoverageCountingPathRenderer();
903}
904
Brian Salomon653f42f2018-07-10 10:07:31 -0400905void GrDrawingManager::flushIfNecessary() {
Robert Phillips6a6de562019-02-15 15:19:15 -0500906 auto direct = fContext->priv().asDirectContext();
907 if (!direct) {
908 return;
909 }
910
911 auto resourceCache = direct->priv().getResourceCache();
Brian Salomon653f42f2018-07-10 10:07:31 -0400912 if (resourceCache && resourceCache->requestsFlush()) {
Greg Daniel797efca2019-05-09 14:04:20 -0400913 this->flush(nullptr, 0, SkSurface::BackendSurfaceAccess::kNoAccess, GrFlushInfo(),
914 GrPrepareForExternalIORequests());
Brian Salomon57d2beab2018-09-10 09:35:41 -0400915 resourceCache->purgeAsNeeded();
Brian Salomon653f42f2018-07-10 10:07:31 -0400916 }
917}
918