blob: b53f0bc78582934b90d4450919988e34915a6cca [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/gpu/GrBackendSemaphore.h"
11#include "include/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/private/GrRecordingContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/private/SkDeferredDisplayList.h"
14#include "src/core/SkTTopoSort.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040015#include "src/gpu/GrAuditTrail.h"
Brian Salomon9241a6d2019-10-03 13:26:54 -040016#include "src/gpu/GrClientMappedBufferManager.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/gpu/GrContextPriv.h"
Greg Daniele227fe42019-08-21 13:52:24 -040018#include "src/gpu/GrCopyRenderTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/gpu/GrGpu.h"
20#include "src/gpu/GrMemoryPool.h"
21#include "src/gpu/GrOnFlushResourceProvider.h"
22#include "src/gpu/GrRecordingContextPriv.h"
23#include "src/gpu/GrRenderTargetContext.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040024#include "src/gpu/GrRenderTargetProxy.h"
Chris Dalton6b498102019-08-01 14:14:52 -060025#include "src/gpu/GrRenderTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/gpu/GrResourceAllocator.h"
27#include "src/gpu/GrResourceProvider.h"
28#include "src/gpu/GrSoftwarePathRenderer.h"
Greg Daniel46e366a2019-12-16 14:38:36 -050029#include "src/gpu/GrSurfaceContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/gpu/GrSurfaceProxyPriv.h"
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) {
107 fRenderTasks.push_back_n(renderTasks.count(), renderTasks.begin());
Robert Phillips22310d62018-09-05 11:07:21 -0400108}
109
Greg Danielf41b2bd2019-08-22 16:19:24 -0400110void GrDrawingManager::RenderTaskDAG::swap(SkTArray<sk_sp<GrRenderTask>>* renderTasks) {
111 SkASSERT(renderTasks->empty());
112 renderTasks->swap(fRenderTasks);
Robert Phillips22310d62018-09-05 11:07:21 -0400113}
114
Chris Dalton6b498102019-08-01 14:14:52 -0600115void GrDrawingManager::RenderTaskDAG::prepForFlush() {
116 if (fSortRenderTasks) {
117 SkDEBUGCODE(bool result =) SkTTopoSort<GrRenderTask, GrRenderTask::TopoSortTraits>(
118 &fRenderTasks);
Robert Phillips22310d62018-09-05 11:07:21 -0400119 SkASSERT(result);
120 }
121
122#ifdef SK_DEBUG
Greg Danielf41b2bd2019-08-22 16:19:24 -0400123 // This block checks for any unnecessary splits in the opsTasks. If two sequential opsTasks
124 // share the same backing GrSurfaceProxy it means the opsTask was artificially split.
Chris Dalton6b498102019-08-01 14:14:52 -0600125 if (fRenderTasks.count()) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400126 GrOpsTask* prevOpsTask = fRenderTasks[0]->asOpsTask();
Chris Dalton6b498102019-08-01 14:14:52 -0600127 for (int i = 1; i < fRenderTasks.count(); ++i) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400128 GrOpsTask* curOpsTask = fRenderTasks[i]->asOpsTask();
Robert Phillips22310d62018-09-05 11:07:21 -0400129
Greg Danielf41b2bd2019-08-22 16:19:24 -0400130 if (prevOpsTask && curOpsTask) {
Greg Daniel16f5c652019-10-29 11:26:01 -0400131 SkASSERT(prevOpsTask->fTargetView != curOpsTask->fTargetView);
Robert Phillips22310d62018-09-05 11:07:21 -0400132 }
133
Greg Danielf41b2bd2019-08-22 16:19:24 -0400134 prevOpsTask = curOpsTask;
Robert Phillips22310d62018-09-05 11:07:21 -0400135 }
136 }
137#endif
138}
139
Chris Dalton6b498102019-08-01 14:14:52 -0600140void GrDrawingManager::RenderTaskDAG::closeAll(const GrCaps* caps) {
141 for (int i = 0; i < fRenderTasks.count(); ++i) {
142 if (fRenderTasks[i]) {
143 fRenderTasks[i]->makeClosed(*caps);
Robert Phillips22310d62018-09-05 11:07:21 -0400144 }
145 }
146}
147
Chris Dalton6b498102019-08-01 14:14:52 -0600148void GrDrawingManager::RenderTaskDAG::cleanup(const GrCaps* caps) {
149 for (int i = 0; i < fRenderTasks.count(); ++i) {
150 if (!fRenderTasks[i]) {
Robert Phillips22310d62018-09-05 11:07:21 -0400151 continue;
152 }
153
Chris Dalton6b498102019-08-01 14:14:52 -0600154 // no renderTask should receive a dependency
155 fRenderTasks[i]->makeClosed(*caps);
robertphillips0dfa62c2015-11-16 06:23:31 -0800156
Greg Danielf41b2bd2019-08-22 16:19:24 -0400157 // We shouldn't need to do this, but it turns out some clients still hold onto opsTasks
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400158 // after a cleanup.
159 // MDB TODO: is this still true?
Chris Dalton6b498102019-08-01 14:14:52 -0600160 if (!fRenderTasks[i]->unique()) {
Chris Daltona84cacf2017-10-04 10:30:29 -0600161 // TODO: Eventually this should be guaranteed unique.
162 // https://bugs.chromium.org/p/skia/issues/detail?id=7111
Chris Dalton6b498102019-08-01 14:14:52 -0600163 fRenderTasks[i]->endFlush();
Chris Daltona84cacf2017-10-04 10:30:29 -0600164 }
robertphillips3dc6ae52015-10-20 09:54:32 -0700165 }
166
Chris Dalton6b498102019-08-01 14:14:52 -0600167 fRenderTasks.reset();
Robert Phillips22310d62018-09-05 11:07:21 -0400168}
169
170///////////////////////////////////////////////////////////////////////////////////////////////////
Robert Phillips69893702019-02-22 11:16:30 -0500171GrDrawingManager::GrDrawingManager(GrRecordingContext* context,
Robert Phillips22310d62018-09-05 11:07:21 -0400172 const GrPathRendererChain::Options& optionsForPathRendererChain,
173 const GrTextContext::Options& optionsForTextContext,
Chris Dalton6b498102019-08-01 14:14:52 -0600174 bool sortRenderTasks,
Greg Danielf41b2bd2019-08-22 16:19:24 -0400175 bool reduceOpsTaskSplitting)
Robert Phillips22310d62018-09-05 11:07:21 -0400176 : fContext(context)
177 , fOptionsForPathRendererChain(optionsForPathRendererChain)
178 , fOptionsForTextContext(optionsForTextContext)
Chris Dalton6b498102019-08-01 14:14:52 -0600179 , fDAG(sortRenderTasks)
Robert Phillips22310d62018-09-05 11:07:21 -0400180 , fTextContext(nullptr)
181 , fPathRendererChain(nullptr)
182 , fSoftwarePathRenderer(nullptr)
Robert Phillips6db27c22019-05-01 10:43:56 -0400183 , fFlushing(false)
Greg Danielf41b2bd2019-08-22 16:19:24 -0400184 , fReduceOpsTaskSplitting(reduceOpsTaskSplitting) {
Robert Phillips22310d62018-09-05 11:07:21 -0400185}
186
187void GrDrawingManager::cleanup() {
Robert Phillips9da87e02019-02-04 13:26:26 -0500188 fDAG.cleanup(fContext->priv().caps());
robertphillips3dc6ae52015-10-20 09:54:32 -0700189
robertphillips13391dd2015-10-30 05:15:11 -0700190 fPathRendererChain = nullptr;
Ben Wagner9ec70c62018-07-12 13:30:47 -0400191 fSoftwarePathRenderer = nullptr;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400192
193 fOnFlushCBObjects.reset();
robertphillips3dc6ae52015-10-20 09:54:32 -0700194}
195
196GrDrawingManager::~GrDrawingManager() {
197 this->cleanup();
198}
199
Robert Phillipsa9162df2019-02-11 14:12:03 -0500200bool GrDrawingManager::wasAbandoned() const {
Robert Phillips6a6de562019-02-15 15:19:15 -0500201 return fContext->priv().abandoned();
robertphillips3dc6ae52015-10-20 09:54:32 -0700202}
203
robertphillips68737822015-10-29 12:12:21 -0700204void GrDrawingManager::freeGpuResources() {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400205 for (int i = fOnFlushCBObjects.count() - 1; i >= 0; --i) {
206 if (!fOnFlushCBObjects[i]->retainOnFreeGpuResources()) {
207 // it's safe to just do this because we're iterating in reverse
208 fOnFlushCBObjects.removeShuffle(i);
209 }
210 }
211
robertphillips68737822015-10-29 12:12:21 -0700212 // a path renderer may be holding onto resources
robertphillips13391dd2015-10-30 05:15:11 -0700213 fPathRendererChain = nullptr;
Ben Wagner9ec70c62018-07-12 13:30:47 -0400214 fSoftwarePathRenderer = nullptr;
Robert Phillipse3302df2017-04-24 07:31:02 -0400215}
216
Robert Phillips7ee385e2017-03-30 08:02:11 -0400217// MDB TODO: make use of the 'proxy' parameter.
Greg Daniel797efca2019-05-09 14:04:20 -0400218GrSemaphoresSubmitted GrDrawingManager::flush(GrSurfaceProxy* proxies[], int numProxies,
219 SkSurface::BackendSurfaceAccess access, const GrFlushInfo& info,
220 const GrPrepareForExternalIORequests& externalRequests) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400221 SkASSERT(numProxies >= 0);
222 SkASSERT(!numProxies || proxies);
Brian Salomon57d2beab2018-09-10 09:35:41 -0400223 GR_CREATE_TRACE_MARKER_CONTEXT("GrDrawingManager", "flush", fContext);
Brian Salomondcbb9d92017-07-19 10:53:20 -0400224
robertphillips7761d612016-05-16 09:14:53 -0700225 if (fFlushing || this->wasAbandoned()) {
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400226 if (info.fFinishedProc) {
227 info.fFinishedProc(info.fFinishedContext);
Greg Daniela3aa75a2019-04-12 14:24:55 -0400228 }
Greg Daniel51316782017-08-02 15:10:09 +0000229 return GrSemaphoresSubmitted::kNo;
joshualittb8918c42015-12-18 09:59:46 -0800230 }
Robert Phillips602df412019-04-08 11:10:39 -0400231
Robert Phillips38d64b02018-09-04 13:23:26 -0400232 SkDEBUGCODE(this->validate());
233
Greg Daniel797efca2019-05-09 14:04:20 -0400234 if (kNone_GrFlushFlags == info.fFlags && !info.fNumSemaphores && !info.fFinishedProc &&
235 !externalRequests.hasRequests()) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400236 bool canSkip = numProxies > 0;
237 for (int i = 0; i < numProxies && canSkip; ++i) {
238 canSkip = !fDAG.isUsed(proxies[i]) && !this->isDDLTarget(proxies[i]);
239 }
240 if (canSkip) {
241 return GrSemaphoresSubmitted::kNo;
242 }
Robert Phillips9313aa72019-04-09 18:41:27 -0400243 }
244
Robert Phillips6a6de562019-02-15 15:19:15 -0500245 auto direct = fContext->priv().asDirectContext();
246 if (!direct) {
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400247 if (info.fFinishedProc) {
248 info.fFinishedProc(info.fFinishedContext);
Greg Daniela3aa75a2019-04-12 14:24:55 -0400249 }
Robert Phillips6a6de562019-02-15 15:19:15 -0500250 return GrSemaphoresSubmitted::kNo; // Can't flush while DDL recording
251 }
Brian Salomon9241a6d2019-10-03 13:26:54 -0400252 direct->priv().clientMappedBufferManager()->process();
Robert Phillips6a6de562019-02-15 15:19:15 -0500253
254 GrGpu* gpu = direct->priv().getGpu();
Robert Phillips874b5352018-03-16 08:48:24 -0400255 if (!gpu) {
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400256 if (info.fFinishedProc) {
257 info.fFinishedProc(info.fFinishedContext);
Greg Daniela3aa75a2019-04-12 14:24:55 -0400258 }
Robert Phillips874b5352018-03-16 08:48:24 -0400259 return GrSemaphoresSubmitted::kNo; // Can't flush while DDL recording
260 }
Greg Daniela3aa75a2019-04-12 14:24:55 -0400261
joshualittb8918c42015-12-18 09:59:46 -0800262 fFlushing = true;
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400263
Robert Phillips6a6de562019-02-15 15:19:15 -0500264 auto resourceProvider = direct->priv().resourceProvider();
265 auto resourceCache = direct->priv().getResourceCache();
266
Chris Dalton6b498102019-08-01 14:14:52 -0600267 // Semi-usually the GrRenderTasks are already closed at this point, but sometimes Ganesh needs
Greg Danielf41b2bd2019-08-22 16:19:24 -0400268 // to flush mid-draw. In that case, the SkGpuDevice's opsTasks won't be closed but need to be
269 // flushed anyway. Closing such opsTasks here will mean new ones will be created to replace them
Chris Dalton6b498102019-08-01 14:14:52 -0600270 // if the SkGpuDevice(s) write to them again.
Robert Phillips9da87e02019-02-04 13:26:26 -0500271 fDAG.closeAll(fContext->priv().caps());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400272 fActiveOpsTask = nullptr;
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400273
Robert Phillips22310d62018-09-05 11:07:21 -0400274 fDAG.prepForFlush();
Brian Salomon601ac802019-02-07 13:37:16 -0500275 if (!fCpuBufferCache) {
276 // We cache more buffers when the backend is using client side arrays. Otherwise, we
277 // expect each pool will use a CPU buffer as a staging buffer before uploading to a GPU
278 // buffer object. Each pool only requires one staging buffer at a time.
279 int maxCachedBuffers = fContext->priv().caps()->preferClientSideDynamicBuffers() ? 2 : 6;
280 fCpuBufferCache = GrBufferAllocPool::CpuBufferCache::Make(maxCachedBuffers);
Brian Salomon58f153c2018-10-18 21:51:15 -0400281 }
Robert Phillipsa4c93ac2017-05-18 11:40:04 -0400282
Robert Phillipse5f73282019-06-18 17:15:04 -0400283 GrOpFlushState flushState(gpu, resourceProvider, &fTokenTracker, fCpuBufferCache);
Robert Phillips40a29d72018-01-18 12:59:22 -0500284
Chris Daltonfe199b72017-05-05 11:26:15 -0400285 GrOnFlushResourceProvider onFlushProvider(this);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500286 // TODO: AFAICT the only reason fFlushState is on GrDrawingManager rather than on the
287 // stack here is to preserve the flush tokens.
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400288
Chris Dalton12658942017-10-05 19:45:25 -0600289 // Prepare any onFlush op lists (e.g. atlases).
Chris Daltonfe199b72017-05-05 11:26:15 -0400290 if (!fOnFlushCBObjects.empty()) {
Chris Dalton6b498102019-08-01 14:14:52 -0600291 fDAG.gatherIDs(&fFlushingRenderTaskIDs);
Robert Phillips22310d62018-09-05 11:07:21 -0400292
Chris Daltonfe199b72017-05-05 11:26:15 -0400293 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
Chris Dalton6b498102019-08-01 14:14:52 -0600294 onFlushCBObject->preFlush(&onFlushProvider, fFlushingRenderTaskIDs.begin(),
Chris Daltonc4b47352019-08-23 10:10:36 -0600295 fFlushingRenderTaskIDs.count());
296 }
297 for (const auto& onFlushRenderTask : fOnFlushRenderTasks) {
298 onFlushRenderTask->makeClosed(*fContext->priv().caps());
Chris Dalton706a6ff2017-11-29 22:01:06 -0700299#ifdef SK_DEBUG
Chris Daltonc4b47352019-08-23 10:10:36 -0600300 // OnFlush callbacks are invoked during flush, and are therefore expected to handle
301 // resource allocation & usage on their own. (No deferred or lazy proxies!)
302 onFlushRenderTask->visitTargetAndSrcProxies_debugOnly(
303 [](GrSurfaceProxy* p, GrMipMapped mipMapped) {
304 SkASSERT(!p->asTextureProxy() || !p->asTextureProxy()->texPriv().isDeferred());
Brian Salomonbeb7f522019-08-30 16:19:42 -0400305 SkASSERT(!p->isLazy());
Chris Dalton4ece96d2019-08-30 11:26:39 -0600306 if (p->requiresManualMSAAResolve()) {
307 // The onFlush callback is responsible for ensuring MSAA gets resolved.
308 SkASSERT(p->asRenderTargetProxy() && !p->asRenderTargetProxy()->isMSAADirty());
309 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600310 if (GrMipMapped::kYes == mipMapped) {
311 // The onFlush callback is responsible for regenerating mips if needed.
312 SkASSERT(p->asTextureProxy() && !p->asTextureProxy()->mipMapsAreDirty());
313 }
314 });
Chris Dalton706a6ff2017-11-29 22:01:06 -0700315#endif
Chris Daltonc4b47352019-08-23 10:10:36 -0600316 onFlushRenderTask->prepare(&flushState);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400317 }
318 }
319
robertphillipsa13e2022015-11-11 12:01:09 -0800320#if 0
Brian Salomon09d994e2016-12-21 11:14:46 -0500321 // Enable this to print out verbose GrOp information
Chris Daltonc4b47352019-08-23 10:10:36 -0600322 SkDEBUGCODE(SkDebugf("onFlush renderTasks:"));
323 for (const auto& onFlushRenderTask : fOnFlushRenderTasks) {
324 SkDEBUGCODE(onFlushRenderTask->dump();)
325 }
326 SkDEBUGCODE(SkDebugf("Normal renderTasks:"));
Chris Dalton6b498102019-08-01 14:14:52 -0600327 for (int i = 0; i < fRenderTasks.count(); ++i) {
328 SkDEBUGCODE(fRenderTasks[i]->dump();)
robertphillips3dc6ae52015-10-20 09:54:32 -0700329 }
robertphillipsa13e2022015-11-11 12:01:09 -0800330#endif
331
Robert Phillipseafd48a2017-11-16 07:52:08 -0500332 int startIndex, stopIndex;
333 bool flushed = false;
334
Robert Phillipsf8e25022017-11-08 15:24:31 -0500335 {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400336 GrResourceAllocator alloc(resourceProvider SkDEBUGCODE(, fDAG.numRenderTasks()));
Chris Dalton6b498102019-08-01 14:14:52 -0600337 for (int i = 0; i < fDAG.numRenderTasks(); ++i) {
338 if (fDAG.renderTask(i)) {
339 fDAG.renderTask(i)->gatherProxyIntervals(&alloc);
Robert Phillips22310d62018-09-05 11:07:21 -0400340 }
Greg Danielf41b2bd2019-08-22 16:19:24 -0400341 alloc.markEndOfOpsTask(i);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500342 }
Robert Phillipsc73666f2019-04-24 08:49:48 -0400343 alloc.determineRecyclability();
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400344
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500345 GrResourceAllocator::AssignError error = GrResourceAllocator::AssignError::kNoError;
Chris Dalton6b498102019-08-01 14:14:52 -0600346 int numRenderTasksExecuted = 0;
Brian Salomon577aa0f2018-11-30 13:32:23 -0500347 while (alloc.assign(&startIndex, &stopIndex, &error)) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500348 if (GrResourceAllocator::AssignError::kFailedProxyInstantiation == error) {
349 for (int i = startIndex; i < stopIndex; ++i) {
Chris Dalton6b498102019-08-01 14:14:52 -0600350 GrRenderTask* renderTask = fDAG.renderTask(i);
351 if (!renderTask) {
352 continue;
Robert Phillips01a91282018-07-26 08:03:04 -0400353 }
Chris Dalton6b498102019-08-01 14:14:52 -0600354 if (!renderTask->isInstantiated()) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400355 // No need to call the renderTask's handleInternalAllocationFailure
356 // since we will already skip executing the renderTask since it is not
357 // instantiated.
Chris Dalton6b498102019-08-01 14:14:52 -0600358 continue;
Robert Phillipsbbcb7f72018-05-31 11:16:19 -0400359 }
Chris Dalton6b498102019-08-01 14:14:52 -0600360 renderTask->handleInternalAllocationFailure();
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500361 }
362 }
Robert Phillips4150eea2018-02-07 17:08:21 -0500363
Chris Dalton6b498102019-08-01 14:14:52 -0600364 if (this->executeRenderTasks(
365 startIndex, stopIndex, &flushState, &numRenderTasksExecuted)) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500366 flushed = true;
367 }
bsalomondc438982016-08-31 11:53:49 -0700368 }
Greg Danielc42b20b2017-10-04 10:34:49 -0400369 }
370
Chris Dalton91ab1552018-04-18 13:24:25 -0600371#ifdef SK_DEBUG
Chris Dalton6b498102019-08-01 14:14:52 -0600372 for (int i = 0; i < fDAG.numRenderTasks(); ++i) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400373 // If there are any remaining opsTaskss at this point, make sure they will not survive the
Chris Dalton91ab1552018-04-18 13:24:25 -0600374 // flush. Otherwise we need to call endFlush() on them.
375 // http://skbug.com/7111
Chris Dalton6b498102019-08-01 14:14:52 -0600376 SkASSERT(!fDAG.renderTask(i) || fDAG.renderTask(i)->unique());
Chris Dalton91ab1552018-04-18 13:24:25 -0600377 }
378#endif
Robert Phillips22310d62018-09-05 11:07:21 -0400379 fDAG.reset();
Robert Phillips15c91422019-05-07 16:54:48 -0400380 this->clearDDLTargets();
robertphillipsa13e2022015-11-11 12:01:09 -0800381
Robert Phillipsc994a932018-06-19 13:09:54 -0400382#ifdef SK_DEBUG
383 // In non-DDL mode this checks that all the flushed ops have been freed from the memory pool.
384 // When we move to partial flushes this assert will no longer be valid.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400385 // In DDL mode this check is somewhat superfluous since the memory for most of the ops/opsTasks
Robert Phillipsc994a932018-06-19 13:09:54 -0400386 // will be stored in the DDL's GrOpMemoryPools.
Robert Phillips9da87e02019-02-04 13:26:26 -0500387 GrOpMemoryPool* opMemoryPool = fContext->priv().opMemoryPool();
Robert Phillipsc994a932018-06-19 13:09:54 -0400388 opMemoryPool->isEmpty();
389#endif
390
Greg Daniel797efca2019-05-09 14:04:20 -0400391 GrSemaphoresSubmitted result = gpu->finishFlush(proxies, numProxies, access, info,
392 externalRequests);
robertphillipsa13e2022015-11-11 12:01:09 -0800393
Brian Salomon57d2beab2018-09-10 09:35:41 -0400394 // Give the cache a chance to purge resources that become purgeable due to flushing.
395 if (flushed) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500396 resourceCache->purgeAsNeeded();
Brian Salomon876a0172019-03-08 11:12:14 -0500397 flushed = false;
bsalomonb77a9072016-09-07 10:02:04 -0700398 }
Chris Daltonfe199b72017-05-05 11:26:15 -0400399 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
Chris Dalton6b498102019-08-01 14:14:52 -0600400 onFlushCBObject->postFlush(fTokenTracker.nextTokenToFlush(), fFlushingRenderTaskIDs.begin(),
401 fFlushingRenderTaskIDs.count());
Brian Salomon876a0172019-03-08 11:12:14 -0500402 flushed = true;
403 }
404 if (flushed) {
405 resourceCache->purgeAsNeeded();
Chris Daltonfe199b72017-05-05 11:26:15 -0400406 }
Chris Dalton6b498102019-08-01 14:14:52 -0600407 fFlushingRenderTaskIDs.reset();
joshualittb8918c42015-12-18 09:59:46 -0800408 fFlushing = false;
Greg Daniel51316782017-08-02 15:10:09 +0000409
410 return result;
robertphillips3dc6ae52015-10-20 09:54:32 -0700411}
412
Chris Dalton6b498102019-08-01 14:14:52 -0600413bool GrDrawingManager::executeRenderTasks(int startIndex, int stopIndex, GrOpFlushState* flushState,
414 int* numRenderTasksExecuted) {
415 SkASSERT(startIndex <= stopIndex && stopIndex <= fDAG.numRenderTasks());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500416
Robert Phillips27483912018-04-20 12:43:18 -0400417#if GR_FLUSH_TIME_OP_SPEW
Greg Danielf41b2bd2019-08-22 16:19:24 -0400418 SkDebugf("Flushing opsTask: %d to %d out of [%d, %d]\n",
Chris Dalton6b498102019-08-01 14:14:52 -0600419 startIndex, stopIndex, 0, fDAG.numRenderTasks());
Robert Phillips27483912018-04-20 12:43:18 -0400420 for (int i = startIndex; i < stopIndex; ++i) {
Chris Dalton6b498102019-08-01 14:14:52 -0600421 if (fDAG.renderTask(i)) {
422 fDAG.renderTask(i)->dump(true);
Robert Phillips1734dd32018-08-21 13:52:09 -0400423 }
Robert Phillips27483912018-04-20 12:43:18 -0400424 }
425#endif
426
Chris Dalton6b498102019-08-01 14:14:52 -0600427 bool anyRenderTasksExecuted = false;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500428
429 for (int i = startIndex; i < stopIndex; ++i) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400430 GrRenderTask* renderTask = fDAG.renderTask(i);
431 if (!renderTask || !renderTask->isInstantiated()) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500432 continue;
433 }
434
Chris Dalton6b498102019-08-01 14:14:52 -0600435 SkASSERT(renderTask->deferredProxiesAreInstantiated());
Robert Phillips22310d62018-09-05 11:07:21 -0400436
Chris Dalton6b498102019-08-01 14:14:52 -0600437 renderTask->prepare(flushState);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500438 }
439
440 // Upload all data to the GPU
441 flushState->preExecuteDraws();
442
Greg Danield2073452018-12-07 11:20:33 -0500443 // For Vulkan, if we have too many oplists to be flushed we end up allocating a lot of resources
444 // for each command buffer associated with the oplists. If this gets too large we can cause the
445 // devices to go OOM. In practice we usually only hit this case in our tests, but to be safe we
446 // put a cap on the number of oplists we will execute before flushing to the GPU to relieve some
447 // memory pressure.
Chris Dalton6b498102019-08-01 14:14:52 -0600448 static constexpr int kMaxRenderTasksBeforeFlush = 100;
Greg Danield2073452018-12-07 11:20:33 -0500449
Chris Daltonc4b47352019-08-23 10:10:36 -0600450 // Execute the onFlush renderTasks first, if any.
451 for (sk_sp<GrRenderTask>& onFlushRenderTask : fOnFlushRenderTasks) {
452 if (!onFlushRenderTask->execute(flushState)) {
453 SkDebugf("WARNING: onFlushRenderTask failed to execute.\n");
Robert Phillipseafd48a2017-11-16 07:52:08 -0500454 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600455 SkASSERT(onFlushRenderTask->unique());
456 onFlushRenderTask = nullptr;
Chris Dalton6b498102019-08-01 14:14:52 -0600457 (*numRenderTasksExecuted)++;
458 if (*numRenderTasksExecuted >= kMaxRenderTasksBeforeFlush) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400459 flushState->gpu()->finishFlush(nullptr, 0, SkSurface::BackendSurfaceAccess::kNoAccess,
Greg Daniel797efca2019-05-09 14:04:20 -0400460 GrFlushInfo(), GrPrepareForExternalIORequests());
Chris Dalton6b498102019-08-01 14:14:52 -0600461 *numRenderTasksExecuted = 0;
Greg Danield2073452018-12-07 11:20:33 -0500462 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500463 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600464 fOnFlushRenderTasks.reset();
Robert Phillipseafd48a2017-11-16 07:52:08 -0500465
466 // Execute the normal op lists.
467 for (int i = startIndex; i < stopIndex; ++i) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400468 GrRenderTask* renderTask = fDAG.renderTask(i);
469 if (!renderTask || !renderTask->isInstantiated()) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500470 continue;
471 }
472
Greg Daniel15ecdf92019-08-30 15:35:23 -0400473 if (renderTask->execute(flushState)) {
Chris Dalton6b498102019-08-01 14:14:52 -0600474 anyRenderTasksExecuted = true;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500475 }
Chris Dalton6b498102019-08-01 14:14:52 -0600476 (*numRenderTasksExecuted)++;
477 if (*numRenderTasksExecuted >= kMaxRenderTasksBeforeFlush) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400478 flushState->gpu()->finishFlush(nullptr, 0, SkSurface::BackendSurfaceAccess::kNoAccess,
Greg Daniel797efca2019-05-09 14:04:20 -0400479 GrFlushInfo(), GrPrepareForExternalIORequests());
Chris Dalton6b498102019-08-01 14:14:52 -0600480 *numRenderTasksExecuted = 0;
Greg Danield2073452018-12-07 11:20:33 -0500481 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500482 }
483
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400484 SkASSERT(!flushState->opsRenderPass());
Robert Phillips40a29d72018-01-18 12:59:22 -0500485 SkASSERT(fTokenTracker.nextDrawToken() == fTokenTracker.nextTokenToFlush());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500486
Chris Dalton6b498102019-08-01 14:14:52 -0600487 // We reset the flush state before the RenderTasks so that the last resources to be freed are
488 // those that are written to in the RenderTasks. This helps to make sure the most recently used
489 // resources are the last to be purged by the resource cache.
Robert Phillipseafd48a2017-11-16 07:52:08 -0500490 flushState->reset();
491
Chris Dalton6b498102019-08-01 14:14:52 -0600492 fDAG.removeRenderTasks(startIndex, stopIndex);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500493
Chris Dalton6b498102019-08-01 14:14:52 -0600494 return anyRenderTasksExecuted;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500495}
496
Greg Daniel55f040b2020-02-13 15:38:32 +0000497GrSemaphoresSubmitted GrDrawingManager::flushSurfaces(GrSurfaceProxy* proxies[], int numProxies,
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400498 SkSurface::BackendSurfaceAccess access,
499 const GrFlushInfo& info) {
bsalomon6a2b1942016-09-08 11:28:59 -0700500 if (this->wasAbandoned()) {
Greg Daniel51316782017-08-02 15:10:09 +0000501 return GrSemaphoresSubmitted::kNo;
bsalomon6a2b1942016-09-08 11:28:59 -0700502 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400503 SkDEBUGCODE(this->validate());
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400504 SkASSERT(numProxies >= 0);
505 SkASSERT(!numProxies || proxies);
bsalomon6a2b1942016-09-08 11:28:59 -0700506
Robert Phillips6a6de562019-02-15 15:19:15 -0500507 auto direct = fContext->priv().asDirectContext();
508 if (!direct) {
509 return GrSemaphoresSubmitted::kNo; // Can't flush while DDL recording
510 }
511
512 GrGpu* gpu = direct->priv().getGpu();
Robert Phillips874b5352018-03-16 08:48:24 -0400513 if (!gpu) {
514 return GrSemaphoresSubmitted::kNo; // Can't flush while DDL recording
515 }
516
Robert Phillipsacc10fa2019-04-01 09:50:20 -0400517 // TODO: It is important to upgrade the drawingmanager to just flushing the
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400518 // portion of the DAG required by 'proxies' in order to restore some of the
Robert Phillipsacc10fa2019-04-01 09:50:20 -0400519 // semantics of this method.
Greg Daniel797efca2019-05-09 14:04:20 -0400520 GrSemaphoresSubmitted result = this->flush(proxies, numProxies, access, info,
521 GrPrepareForExternalIORequests());
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400522 for (int i = 0; i < numProxies; ++i) {
Chris Dalton4ece96d2019-08-30 11:26:39 -0600523 GrSurfaceProxy* proxy = proxies[i];
524 if (!proxy->isInstantiated()) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400525 return result;
526 }
Chris Dalton4ece96d2019-08-30 11:26:39 -0600527 // In the flushSurfaces case, we need to resolve MSAA immediately after flush. This is
528 // because the client will call through to this method when drawing into a target created by
529 // wrapBackendTextureAsRenderTarget, and will expect the original texture to be fully
530 // resolved upon return.
531 if (proxy->requiresManualMSAAResolve()) {
532 auto* rtProxy = proxy->asRenderTargetProxy();
533 SkASSERT(rtProxy);
534 if (rtProxy->isMSAADirty()) {
535 SkASSERT(rtProxy->peekRenderTarget());
Chris Dalton16a33c62019-09-24 22:19:17 -0600536 gpu->resolveRenderTarget(rtProxy->peekRenderTarget(), rtProxy->msaaDirtyRect(),
Greg Daniel242536f2020-02-13 14:12:46 -0500537 GrGpu::ForExternalIO::kYes);
Chris Dalton4ece96d2019-08-30 11:26:39 -0600538 rtProxy->markMSAAResolved();
539 }
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400540 }
Chris Dalton3d770272019-08-14 09:24:37 -0600541 // If, after a flush, any of the proxies of interest have dirty mipmaps, regenerate them in
542 // case their backend textures are being stolen.
543 // (This special case is exercised by the ReimportImageTextureWithMipLevels test.)
544 // 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 -0600545 if (auto* textureProxy = proxy->asTextureProxy()) {
Chris Dalton3d770272019-08-14 09:24:37 -0600546 if (textureProxy->mipMapsAreDirty()) {
Chris Dalton4ece96d2019-08-30 11:26:39 -0600547 SkASSERT(textureProxy->peekTexture());
Chris Dalton3d770272019-08-14 09:24:37 -0600548 gpu->regenerateMipMapLevels(textureProxy->peekTexture());
549 textureProxy->markMipMapsClean();
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400550 }
Brian Salomon930f9392018-06-20 16:25:26 -0400551 }
bsalomon6a2b1942016-09-08 11:28:59 -0700552 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400553
554 SkDEBUGCODE(this->validate());
Greg Daniel51316782017-08-02 15:10:09 +0000555 return result;
bsalomon6a2b1942016-09-08 11:28:59 -0700556}
557
Chris Daltonfe199b72017-05-05 11:26:15 -0400558void GrDrawingManager::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
559 fOnFlushCBObjects.push_back(onFlushCBObject);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400560}
561
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500562#if GR_TEST_UTILS
563void GrDrawingManager::testingOnly_removeOnFlushCallbackObject(GrOnFlushCallbackObject* cb) {
564 int n = std::find(fOnFlushCBObjects.begin(), fOnFlushCBObjects.end(), cb) -
565 fOnFlushCBObjects.begin();
566 SkASSERT(n < fOnFlushCBObjects.count());
567 fOnFlushCBObjects.removeShuffle(n);
568}
569#endif
570
Chris Dalton6b498102019-08-01 14:14:52 -0600571void GrDrawingManager::moveRenderTasksToDDL(SkDeferredDisplayList* ddl) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400572 SkDEBUGCODE(this->validate());
573
Chris Dalton6b498102019-08-01 14:14:52 -0600574 // no renderTask should receive a new command after this
Robert Phillips9da87e02019-02-04 13:26:26 -0500575 fDAG.closeAll(fContext->priv().caps());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400576 fActiveOpsTask = nullptr;
Robert Phillips7a137052018-02-01 11:23:12 -0500577
Chris Dalton6b498102019-08-01 14:14:52 -0600578 fDAG.swap(&ddl->fRenderTasks);
Robert Phillips867ce8f2018-06-21 10:28:36 -0400579
Robert Phillips7327c9d2019-10-08 16:32:56 -0400580 for (auto renderTask : ddl->fRenderTasks) {
Robert Phillips29f38542019-10-16 09:20:25 -0400581 renderTask->prePrepare(fContext);
Robert Phillips7327c9d2019-10-08 16:32:56 -0400582 }
583
Michael Ludwig2c316bd2019-12-19 14:50:44 -0500584 ddl->fArenas = std::move(fContext->priv().detachArenas());
Robert Phillips61fc7992019-10-22 11:58:17 -0400585
Robert Phillipsf6a0b452020-02-18 14:26:46 -0500586 fContext->priv().detachProgramData(&ddl->fProgramData);
Robert Phillips576b6a12019-12-06 13:05:49 -0500587
Robert Phillips774168e2018-05-31 12:43:27 -0400588 if (fPathRendererChain) {
589 if (auto ccpr = fPathRendererChain->getCoverageCountingPathRenderer()) {
590 ddl->fPendingPaths = ccpr->detachPendingPaths();
591 }
592 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400593
594 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500595}
596
Chris Dalton6b498102019-08-01 14:14:52 -0600597void GrDrawingManager::copyRenderTasksFromDDL(const SkDeferredDisplayList* ddl,
Robert Phillips933484f2019-11-26 09:38:55 -0500598 GrRenderTargetProxy* newDest) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400599 SkDEBUGCODE(this->validate());
600
Greg Danielf41b2bd2019-08-22 16:19:24 -0400601 if (fActiveOpsTask) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400602 // This is a temporary fix for the partial-MDB world. In that world we're not
Greg Danielf41b2bd2019-08-22 16:19:24 -0400603 // reordering so ops that (in the single opsTask world) would've just glommed onto the
604 // end of the single opsTask but referred to a far earlier RT need to appear in their
605 // own opsTask.
606 fActiveOpsTask->makeClosed(*fContext->priv().caps());
607 fActiveOpsTask = nullptr;
Robert Phillips38d64b02018-09-04 13:23:26 -0400608 }
609
Robert Phillips15c91422019-05-07 16:54:48 -0400610 this->addDDLTarget(newDest);
611
Robert Phillips62000362018-02-01 09:10:04 -0500612 // Here we jam the proxy that backs the current replay SkSurface into the LazyProxyData.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400613 // The lazy proxy that references it (in the copied opsTasks) will steal its GrTexture.
Robert Phillips62000362018-02-01 09:10:04 -0500614 ddl->fLazyProxyData->fReplayDest = newDest;
Robert Phillips774168e2018-05-31 12:43:27 -0400615
616 if (ddl->fPendingPaths.size()) {
617 GrCoverageCountingPathRenderer* ccpr = this->getCoverageCountingPathRenderer();
618
619 ccpr->mergePendingPaths(ddl->fPendingPaths);
620 }
Robert Phillips22310d62018-09-05 11:07:21 -0400621
Chris Dalton6b498102019-08-01 14:14:52 -0600622 fDAG.add(ddl->fRenderTasks);
Robert Phillips38d64b02018-09-04 13:23:26 -0400623
624 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500625}
626
Robert Phillips38d64b02018-09-04 13:23:26 -0400627#ifdef SK_DEBUG
628void GrDrawingManager::validate() const {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400629 if (fDAG.sortingRenderTasks() && fReduceOpsTaskSplitting) {
630 SkASSERT(!fActiveOpsTask);
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400631 } else {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400632 if (fActiveOpsTask) {
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400633 SkASSERT(!fDAG.empty());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400634 SkASSERT(!fActiveOpsTask->isClosed());
635 SkASSERT(fActiveOpsTask == fDAG.back());
Robert Phillips38d64b02018-09-04 13:23:26 -0400636 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400637
Chris Dalton6b498102019-08-01 14:14:52 -0600638 for (int i = 0; i < fDAG.numRenderTasks(); ++i) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400639 if (fActiveOpsTask != fDAG.renderTask(i)) {
Chris Daltone2a903e2019-09-18 13:41:50 -0600640 // The resolveTask associated with the activeTask remains open for as long as the
641 // activeTask does.
642 bool isActiveResolveTask =
643 fActiveOpsTask && fActiveOpsTask->fTextureResolveTask == fDAG.renderTask(i);
644 SkASSERT(isActiveResolveTask || fDAG.renderTask(i)->isClosed());
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400645 }
646 }
647
648 if (!fDAG.empty() && !fDAG.back()->isClosed()) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400649 SkASSERT(fActiveOpsTask == fDAG.back());
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400650 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400651 }
652}
653#endif
654
Greg Danielbbfec9d2019-08-20 10:56:51 -0400655void GrDrawingManager::closeRenderTasksForNewRenderTask(GrSurfaceProxy* target) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400656 if (target && fDAG.sortingRenderTasks() && fReduceOpsTaskSplitting) {
Chris Dalton5fe99772019-08-06 11:57:39 -0600657 // In this case we need to close all the renderTasks that rely on the current contents of
658 // 'target'. That is bc we're going to update the content of the proxy so they need to be
659 // split in case they use both the old and new content. (This is a bit of an overkill: they
660 // really only need to be split if they ever reference proxy's contents again but that is
661 // hard to predict/handle).
662 if (GrRenderTask* lastRenderTask = target->getLastRenderTask()) {
Chris Dalton6b498102019-08-01 14:14:52 -0600663 lastRenderTask->closeThoseWhoDependOnMe(*fContext->priv().caps());
Robert Phillips46acf9d2018-10-09 09:31:40 -0400664 }
Greg Danielf41b2bd2019-08-22 16:19:24 -0400665 } else if (fActiveOpsTask) {
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400666 // This is a temporary fix for the partial-MDB world. In that world we're not
Greg Danielf41b2bd2019-08-22 16:19:24 -0400667 // reordering so ops that (in the single opsTask world) would've just glommed onto the
668 // end of the single opsTask but referred to a far earlier RT need to appear in their
669 // own opsTask.
670 fActiveOpsTask->makeClosed(*fContext->priv().caps());
671 fActiveOpsTask = nullptr;
robertphillips3dc6ae52015-10-20 09:54:32 -0700672 }
Chris Dalton5fe99772019-08-06 11:57:39 -0600673}
674
Greg Daniel16f5c652019-10-29 11:26:01 -0400675sk_sp<GrOpsTask> GrDrawingManager::newOpsTask(GrSurfaceProxyView surfaceView,
676 bool managedOpsTask) {
Chris Dalton5fe99772019-08-06 11:57:39 -0600677 SkDEBUGCODE(this->validate());
678 SkASSERT(fContext);
679
Greg Daniel16f5c652019-10-29 11:26:01 -0400680 GrSurfaceProxy* proxy = surfaceView.proxy();
681 this->closeRenderTasksForNewRenderTask(proxy);
robertphillips3dc6ae52015-10-20 09:54:32 -0700682
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500683 sk_sp<GrOpsTask> opsTask(new GrOpsTask(fContext->priv().arenas(),
Michael Ludwigd0840ec2019-12-12 09:48:38 -0500684 std::move(surfaceView),
685 fContext->priv().auditTrail()));
Greg Daniel16f5c652019-10-29 11:26:01 -0400686 SkASSERT(proxy->getLastRenderTask() == opsTask.get());
robertphillips3dc6ae52015-10-20 09:54:32 -0700687
Greg Danielf41b2bd2019-08-22 16:19:24 -0400688 if (managedOpsTask) {
689 fDAG.add(opsTask);
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400690
Greg Danielf41b2bd2019-08-22 16:19:24 -0400691 if (!fDAG.sortingRenderTasks() || !fReduceOpsTaskSplitting) {
692 fActiveOpsTask = opsTask.get();
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400693 }
Robert Phillips941d1442017-06-14 16:37:02 -0400694 }
robertphillips3dc6ae52015-10-20 09:54:32 -0700695
Robert Phillips38d64b02018-09-04 13:23:26 -0400696 SkDEBUGCODE(this->validate());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400697 return opsTask;
robertphillips3dc6ae52015-10-20 09:54:32 -0700698}
699
Chris Daltone2a903e2019-09-18 13:41:50 -0600700GrTextureResolveRenderTask* GrDrawingManager::newTextureResolveRenderTask(const GrCaps& caps) {
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600701 // 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 -0600702 // in sorting and opsTask reduction mode) the render tasks that depend on any proxy's current
Greg Danielf41b2bd2019-08-22 16:19:24 -0400703 // state. This is because those opsTasks can still receive new ops and because if they refer to
Chris Dalton4ece96d2019-08-30 11:26:39 -0600704 // the mipmapped version of 'proxy', they will then come to depend on the render task being
705 // created here.
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600706 //
Greg Danielf41b2bd2019-08-22 16:19:24 -0400707 // Add the new textureResolveTask before the fActiveOpsTask (if not in
708 // sorting/opsTask-splitting-reduction mode) because it will depend upon this resolve task.
Chris Dalton3d770272019-08-14 09:24:37 -0600709 // NOTE: Putting it here will also reduce the amount of work required by the topological sort.
Chris Daltone2a903e2019-09-18 13:41:50 -0600710 return static_cast<GrTextureResolveRenderTask*>(fDAG.addBeforeLast(
711 sk_make_sp<GrTextureResolveRenderTask>()));
Chris Dalton3d770272019-08-14 09:24:37 -0600712}
713
Greg Danielc30f1a92019-09-06 15:28:58 -0400714void GrDrawingManager::newWaitRenderTask(sk_sp<GrSurfaceProxy> proxy,
Greg Daniel301015c2019-11-18 14:06:46 -0500715 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> semaphores,
Greg Danielc30f1a92019-09-06 15:28:58 -0400716 int numSemaphores) {
717 SkDEBUGCODE(this->validate());
718 SkASSERT(fContext);
719
720 const GrCaps& caps = *fContext->priv().caps();
721
Greg Daniel16f5c652019-10-29 11:26:01 -0400722 sk_sp<GrWaitRenderTask> waitTask = sk_make_sp<GrWaitRenderTask>(GrSurfaceProxyView(proxy),
723 std::move(semaphores),
Greg Danielc30f1a92019-09-06 15:28:58 -0400724 numSemaphores);
725 if (fReduceOpsTaskSplitting) {
726 GrRenderTask* lastTask = proxy->getLastRenderTask();
727 if (lastTask && !lastTask->isClosed()) {
728 // We directly make the currently open renderTask depend on waitTask instead of using
729 // the proxy version of addDependency. The waitTask will never need to trigger any
730 // resolves or mip map generation which is the main advantage of going through the proxy
731 // version. Additionally we would've had to temporarily set the wait task as the
732 // lastRenderTask on the proxy, add the dependency, and then reset the lastRenderTask to
733 // lastTask. Additionally we add all dependencies of lastTask to waitTask so that the
734 // waitTask doesn't get reordered before them and unnecessarily block those tasks.
735 // Note: Any previous Ops already in lastTask will get blocked by the wait semaphore
736 // even though they don't need to be for correctness.
737
738 // Make sure we add the dependencies of lastTask to waitTask first or else we'll get a
739 // circular self dependency of waitTask on waitTask.
740 waitTask->addDependenciesFromOtherTask(lastTask);
741 lastTask->addDependency(waitTask.get());
742 } else {
743 // If there is a last task we set the waitTask to depend on it so that it doesn't get
744 // reordered in front of the lastTask causing the lastTask to be blocked by the
745 // semaphore. Again we directly just go through adding the dependency to the task and
746 // not the proxy since we don't need to worry about resolving anything.
747 if (lastTask) {
748 waitTask->addDependency(lastTask);
749 }
750 proxy->setLastRenderTask(waitTask.get());
751 }
752 fDAG.add(waitTask);
753 } else {
Greg Daniel16f5c652019-10-29 11:26:01 -0400754 if (fActiveOpsTask && (fActiveOpsTask->fTargetView.proxy() == proxy.get())) {
Greg Danielc30f1a92019-09-06 15:28:58 -0400755 SkASSERT(proxy->getLastRenderTask() == fActiveOpsTask);
756 fDAG.addBeforeLast(waitTask);
757 // In this case we keep the current renderTask open but just insert the new waitTask
758 // before it in the list. The waitTask will never need to trigger any resolves or mip
759 // map generation which is the main advantage of going through the proxy version.
760 // Additionally we would've had to temporarily set the wait task as the lastRenderTask
761 // on the proxy, add the dependency, and then reset the lastRenderTask to
762 // fActiveOpsTask. Additionally we make the waitTask depend on all of fActiveOpsTask
763 // dependencies so that we don't unnecessarily reorder the waitTask before them.
764 // Note: Any previous Ops already in fActiveOpsTask will get blocked by the wait
765 // semaphore even though they don't need to be for correctness.
766
767 // Make sure we add the dependencies of fActiveOpsTask to waitTask first or else we'll
768 // get a circular self dependency of waitTask on waitTask.
769 waitTask->addDependenciesFromOtherTask(fActiveOpsTask);
770 fActiveOpsTask->addDependency(waitTask.get());
771 } else {
772 // In this case we just close the previous RenderTask and start and append the waitTask
773 // to the DAG. Since it is the last task now we call setLastRenderTask on the proxy. If
774 // there is a lastTask on the proxy we make waitTask depend on that task. This
775 // dependency isn't strictly needed but it does keep the DAG from reordering the
776 // waitTask earlier and blocking more tasks.
777 if (GrRenderTask* lastTask = proxy->getLastRenderTask()) {
778 waitTask->addDependency(lastTask);
779 }
780 proxy->setLastRenderTask(waitTask.get());
781 this->closeRenderTasksForNewRenderTask(proxy.get());
782 fDAG.add(waitTask);
783 }
784 }
785 waitTask->makeClosed(caps);
786
787 SkDEBUGCODE(this->validate());
788}
789
Greg Danielbbfec9d2019-08-20 10:56:51 -0400790void GrDrawingManager::newTransferFromRenderTask(sk_sp<GrSurfaceProxy> srcProxy,
791 const SkIRect& srcRect,
792 GrColorType surfaceColorType,
793 GrColorType dstColorType,
794 sk_sp<GrGpuBuffer> dstBuffer,
795 size_t dstOffset) {
796 SkDEBUGCODE(this->validate());
797 SkASSERT(fContext);
798 // This copies from srcProxy to dstBuffer so it doesn't have a real target.
799 this->closeRenderTasksForNewRenderTask(nullptr);
800
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600801 GrRenderTask* task = fDAG.add(sk_make_sp<GrTransferFromRenderTask>(
802 srcProxy, srcRect, surfaceColorType, dstColorType, std::move(dstBuffer), dstOffset));
Greg Danielbbfec9d2019-08-20 10:56:51 -0400803
804 const GrCaps& caps = *fContext->priv().caps();
805
806 // We always say GrMipMapped::kNo here since we are always just copying from the base layer. We
807 // don't need to make sure the whole mip map chain is valid.
808 task->addDependency(srcProxy.get(), GrMipMapped::kNo, GrTextureResolveManager(this), caps);
809 task->makeClosed(caps);
810
Greg Danielbbfec9d2019-08-20 10:56:51 -0400811 // We have closed the previous active oplist but since a new oplist isn't being added there
812 // shouldn't be an active one.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400813 SkASSERT(!fActiveOpsTask);
Greg Danielbbfec9d2019-08-20 10:56:51 -0400814 SkDEBUGCODE(this->validate());
815}
816
Greg Daniel16f5c652019-10-29 11:26:01 -0400817bool GrDrawingManager::newCopyRenderTask(GrSurfaceProxyView srcView,
Greg Daniele227fe42019-08-21 13:52:24 -0400818 const SkIRect& srcRect,
Greg Daniel16f5c652019-10-29 11:26:01 -0400819 GrSurfaceProxyView dstView,
Greg Daniele227fe42019-08-21 13:52:24 -0400820 const SkIPoint& dstPoint) {
821 SkDEBUGCODE(this->validate());
822 SkASSERT(fContext);
Greg Daniele227fe42019-08-21 13:52:24 -0400823
Greg Daniel16f5c652019-10-29 11:26:01 -0400824 this->closeRenderTasksForNewRenderTask(dstView.proxy());
Brian Salomone4bce012019-09-20 15:34:23 -0400825 const GrCaps& caps = *fContext->priv().caps();
826
Greg Daniel16f5c652019-10-29 11:26:01 -0400827 GrSurfaceProxy* srcProxy = srcView.proxy();
828
Brian Salomone4bce012019-09-20 15:34:23 -0400829 GrRenderTask* task =
Greg Daniel16f5c652019-10-29 11:26:01 -0400830 fDAG.add(GrCopyRenderTask::Make(std::move(srcView), srcRect, std::move(dstView),
831 dstPoint, &caps));
Greg Daniele227fe42019-08-21 13:52:24 -0400832 if (!task) {
833 return false;
834 }
835
Greg Daniele227fe42019-08-21 13:52:24 -0400836 // We always say GrMipMapped::kNo here since we are always just copying from the base layer to
837 // another base layer. We don't need to make sure the whole mip map chain is valid.
Greg Daniel16f5c652019-10-29 11:26:01 -0400838 task->addDependency(srcProxy, GrMipMapped::kNo, GrTextureResolveManager(this), caps);
Greg Daniele227fe42019-08-21 13:52:24 -0400839 task->makeClosed(caps);
840
Greg Daniele227fe42019-08-21 13:52:24 -0400841 // We have closed the previous active oplist but since a new oplist isn't being added there
842 // shouldn't be an active one.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400843 SkASSERT(!fActiveOpsTask);
Greg Daniele227fe42019-08-21 13:52:24 -0400844 SkDEBUGCODE(this->validate());
845 return true;
846}
847
Herb Derby26cbe512018-05-24 14:39:01 -0400848GrTextContext* GrDrawingManager::getTextContext() {
849 if (!fTextContext) {
850 fTextContext = GrTextContext::Make(fOptionsForTextContext);
brianosman86e76262016-08-11 12:17:31 -0700851 }
852
Herb Derby26cbe512018-05-24 14:39:01 -0400853 return fTextContext.get();
brianosman86e76262016-08-11 12:17:31 -0700854}
855
robertphillips68737822015-10-29 12:12:21 -0700856/*
857 * This method finds a path renderer that can draw the specified path on
858 * the provided target.
859 * Due to its expense, the software path renderer has split out so it can
860 * can be individually allowed/disallowed via the "allowSW" boolean.
861 */
862GrPathRenderer* GrDrawingManager::getPathRenderer(const GrPathRenderer::CanDrawPathArgs& args,
863 bool allowSW,
864 GrPathRendererChain::DrawType drawType,
865 GrPathRenderer::StencilSupport* stencilSupport) {
866
867 if (!fPathRendererChain) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400868 fPathRendererChain.reset(new GrPathRendererChain(fContext, fOptionsForPathRendererChain));
robertphillips68737822015-10-29 12:12:21 -0700869 }
870
871 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(args, drawType, stencilSupport);
872 if (!pr && allowSW) {
Brian Salomone7df0bb2018-05-07 14:44:57 -0400873 auto swPR = this->getSoftwarePathRenderer();
874 if (GrPathRenderer::CanDrawPath::kNo != swPR->canDrawPath(args)) {
875 pr = swPR;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500876 }
robertphillips68737822015-10-29 12:12:21 -0700877 }
878
879 return pr;
880}
881
Brian Salomone7df0bb2018-05-07 14:44:57 -0400882GrPathRenderer* GrDrawingManager::getSoftwarePathRenderer() {
883 if (!fSoftwarePathRenderer) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400884 fSoftwarePathRenderer.reset(
Robert Phillips9da87e02019-02-04 13:26:26 -0500885 new GrSoftwarePathRenderer(fContext->priv().proxyProvider(),
Ben Wagner9ec70c62018-07-12 13:30:47 -0400886 fOptionsForPathRendererChain.fAllowPathMaskCaching));
Brian Salomone7df0bb2018-05-07 14:44:57 -0400887 }
Ben Wagner9ec70c62018-07-12 13:30:47 -0400888 return fSoftwarePathRenderer.get();
Brian Salomone7df0bb2018-05-07 14:44:57 -0400889}
890
Chris Daltonfddb6c02017-11-04 15:22:22 -0600891GrCoverageCountingPathRenderer* GrDrawingManager::getCoverageCountingPathRenderer() {
892 if (!fPathRendererChain) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400893 fPathRendererChain.reset(new GrPathRendererChain(fContext, fOptionsForPathRendererChain));
Chris Daltonfddb6c02017-11-04 15:22:22 -0600894 }
895 return fPathRendererChain->getCoverageCountingPathRenderer();
896}
897
Brian Salomon653f42f2018-07-10 10:07:31 -0400898void GrDrawingManager::flushIfNecessary() {
Robert Phillips6a6de562019-02-15 15:19:15 -0500899 auto direct = fContext->priv().asDirectContext();
900 if (!direct) {
901 return;
902 }
903
904 auto resourceCache = direct->priv().getResourceCache();
Brian Salomon653f42f2018-07-10 10:07:31 -0400905 if (resourceCache && resourceCache->requestsFlush()) {
Greg Daniel797efca2019-05-09 14:04:20 -0400906 this->flush(nullptr, 0, SkSurface::BackendSurfaceAccess::kNoAccess, GrFlushInfo(),
907 GrPrepareForExternalIORequests());
Brian Salomon57d2beab2018-09-10 09:35:41 -0400908 resourceCache->purgeAsNeeded();
Brian Salomon653f42f2018-07-10 10:07:31 -0400909 }
910}
911