blob: b6517008863de4fc88dc6b167777342b7cd89621 [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"
29#include "src/gpu/GrSurfaceProxyPriv.h"
30#include "src/gpu/GrTextureContext.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) {
131 SkASSERT(prevOpsTask->fTarget.get() != curOpsTask->fTarget.get());
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 Salomon57d2bea2018-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 Salomon57d2bea2018-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
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400497GrSemaphoresSubmitted GrDrawingManager::flushSurfaces(GrSurfaceProxy* proxies[], int numProxies,
498 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(),
537 rtProxy->origin(), 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
Robert Phillips774168e2018-05-31 12:43:27 -0400584 if (fPathRendererChain) {
585 if (auto ccpr = fPathRendererChain->getCoverageCountingPathRenderer()) {
586 ddl->fPendingPaths = ccpr->detachPendingPaths();
587 }
588 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400589
590 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500591}
592
Chris Dalton6b498102019-08-01 14:14:52 -0600593void GrDrawingManager::copyRenderTasksFromDDL(const SkDeferredDisplayList* ddl,
Robert Phillips62000362018-02-01 09:10:04 -0500594 GrRenderTargetProxy* newDest) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400595 SkDEBUGCODE(this->validate());
596
Greg Danielf41b2bd2019-08-22 16:19:24 -0400597 if (fActiveOpsTask) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400598 // This is a temporary fix for the partial-MDB world. In that world we're not
Greg Danielf41b2bd2019-08-22 16:19:24 -0400599 // reordering so ops that (in the single opsTask world) would've just glommed onto the
600 // end of the single opsTask but referred to a far earlier RT need to appear in their
601 // own opsTask.
602 fActiveOpsTask->makeClosed(*fContext->priv().caps());
603 fActiveOpsTask = nullptr;
Robert Phillips38d64b02018-09-04 13:23:26 -0400604 }
605
Robert Phillips15c91422019-05-07 16:54:48 -0400606 this->addDDLTarget(newDest);
607
Robert Phillips62000362018-02-01 09:10:04 -0500608 // Here we jam the proxy that backs the current replay SkSurface into the LazyProxyData.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400609 // The lazy proxy that references it (in the copied opsTasks) will steal its GrTexture.
Robert Phillips62000362018-02-01 09:10:04 -0500610 ddl->fLazyProxyData->fReplayDest = newDest;
Robert Phillips774168e2018-05-31 12:43:27 -0400611
612 if (ddl->fPendingPaths.size()) {
613 GrCoverageCountingPathRenderer* ccpr = this->getCoverageCountingPathRenderer();
614
615 ccpr->mergePendingPaths(ddl->fPendingPaths);
616 }
Robert Phillips22310d62018-09-05 11:07:21 -0400617
Chris Dalton6b498102019-08-01 14:14:52 -0600618 fDAG.add(ddl->fRenderTasks);
Robert Phillips38d64b02018-09-04 13:23:26 -0400619
620 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500621}
622
Robert Phillips38d64b02018-09-04 13:23:26 -0400623#ifdef SK_DEBUG
624void GrDrawingManager::validate() const {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400625 if (fDAG.sortingRenderTasks() && fReduceOpsTaskSplitting) {
626 SkASSERT(!fActiveOpsTask);
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400627 } else {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400628 if (fActiveOpsTask) {
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400629 SkASSERT(!fDAG.empty());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400630 SkASSERT(!fActiveOpsTask->isClosed());
631 SkASSERT(fActiveOpsTask == fDAG.back());
Robert Phillips38d64b02018-09-04 13:23:26 -0400632 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400633
Chris Dalton6b498102019-08-01 14:14:52 -0600634 for (int i = 0; i < fDAG.numRenderTasks(); ++i) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400635 if (fActiveOpsTask != fDAG.renderTask(i)) {
Chris Daltone2a903e2019-09-18 13:41:50 -0600636 // The resolveTask associated with the activeTask remains open for as long as the
637 // activeTask does.
638 bool isActiveResolveTask =
639 fActiveOpsTask && fActiveOpsTask->fTextureResolveTask == fDAG.renderTask(i);
640 SkASSERT(isActiveResolveTask || fDAG.renderTask(i)->isClosed());
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400641 }
642 }
643
644 if (!fDAG.empty() && !fDAG.back()->isClosed()) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400645 SkASSERT(fActiveOpsTask == fDAG.back());
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400646 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400647 }
648}
649#endif
650
Greg Danielbbfec9d2019-08-20 10:56:51 -0400651void GrDrawingManager::closeRenderTasksForNewRenderTask(GrSurfaceProxy* target) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400652 if (target && fDAG.sortingRenderTasks() && fReduceOpsTaskSplitting) {
Chris Dalton5fe99772019-08-06 11:57:39 -0600653 // In this case we need to close all the renderTasks that rely on the current contents of
654 // 'target'. That is bc we're going to update the content of the proxy so they need to be
655 // split in case they use both the old and new content. (This is a bit of an overkill: they
656 // really only need to be split if they ever reference proxy's contents again but that is
657 // hard to predict/handle).
658 if (GrRenderTask* lastRenderTask = target->getLastRenderTask()) {
Chris Dalton6b498102019-08-01 14:14:52 -0600659 lastRenderTask->closeThoseWhoDependOnMe(*fContext->priv().caps());
Robert Phillips46acf9d2018-10-09 09:31:40 -0400660 }
Greg Danielf41b2bd2019-08-22 16:19:24 -0400661 } else if (fActiveOpsTask) {
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400662 // This is a temporary fix for the partial-MDB world. In that world we're not
Greg Danielf41b2bd2019-08-22 16:19:24 -0400663 // reordering so ops that (in the single opsTask world) would've just glommed onto the
664 // end of the single opsTask but referred to a far earlier RT need to appear in their
665 // own opsTask.
666 fActiveOpsTask->makeClosed(*fContext->priv().caps());
667 fActiveOpsTask = nullptr;
robertphillips3dc6ae52015-10-20 09:54:32 -0700668 }
Chris Dalton5fe99772019-08-06 11:57:39 -0600669}
670
Greg Danielf41b2bd2019-08-22 16:19:24 -0400671sk_sp<GrOpsTask> GrDrawingManager::newOpsTask(sk_sp<GrRenderTargetProxy> rtp, bool managedOpsTask) {
Chris Dalton5fe99772019-08-06 11:57:39 -0600672 SkDEBUGCODE(this->validate());
673 SkASSERT(fContext);
674
Greg Danielbbfec9d2019-08-20 10:56:51 -0400675 this->closeRenderTasksForNewRenderTask(rtp.get());
robertphillips3dc6ae52015-10-20 09:54:32 -0700676
Greg Danielf41b2bd2019-08-22 16:19:24 -0400677 sk_sp<GrOpsTask> opsTask(new GrOpsTask(fContext->priv().refOpMemoryPool(), rtp,
678 fContext->priv().auditTrail()));
679 SkASSERT(rtp->getLastRenderTask() == opsTask.get());
robertphillips3dc6ae52015-10-20 09:54:32 -0700680
Greg Danielf41b2bd2019-08-22 16:19:24 -0400681 if (managedOpsTask) {
682 fDAG.add(opsTask);
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400683
Greg Danielf41b2bd2019-08-22 16:19:24 -0400684 if (!fDAG.sortingRenderTasks() || !fReduceOpsTaskSplitting) {
685 fActiveOpsTask = opsTask.get();
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400686 }
Robert Phillips941d1442017-06-14 16:37:02 -0400687 }
robertphillips3dc6ae52015-10-20 09:54:32 -0700688
Robert Phillips38d64b02018-09-04 13:23:26 -0400689 SkDEBUGCODE(this->validate());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400690 return opsTask;
robertphillips3dc6ae52015-10-20 09:54:32 -0700691}
692
Chris Daltone2a903e2019-09-18 13:41:50 -0600693GrTextureResolveRenderTask* GrDrawingManager::newTextureResolveRenderTask(const GrCaps& caps) {
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600694 // 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 -0600695 // in sorting and opsTask reduction mode) the render tasks that depend on any proxy's current
Greg Danielf41b2bd2019-08-22 16:19:24 -0400696 // state. This is because those opsTasks can still receive new ops and because if they refer to
Chris Dalton4ece96d2019-08-30 11:26:39 -0600697 // the mipmapped version of 'proxy', they will then come to depend on the render task being
698 // created here.
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600699 //
Greg Danielf41b2bd2019-08-22 16:19:24 -0400700 // Add the new textureResolveTask before the fActiveOpsTask (if not in
701 // sorting/opsTask-splitting-reduction mode) because it will depend upon this resolve task.
Chris Dalton3d770272019-08-14 09:24:37 -0600702 // NOTE: Putting it here will also reduce the amount of work required by the topological sort.
Chris Daltone2a903e2019-09-18 13:41:50 -0600703 return static_cast<GrTextureResolveRenderTask*>(fDAG.addBeforeLast(
704 sk_make_sp<GrTextureResolveRenderTask>()));
Chris Dalton3d770272019-08-14 09:24:37 -0600705}
706
Greg Danielc30f1a92019-09-06 15:28:58 -0400707void GrDrawingManager::newWaitRenderTask(sk_sp<GrSurfaceProxy> proxy,
708 std::unique_ptr<sk_sp<GrSemaphore>[]> semaphores,
709 int numSemaphores) {
710 SkDEBUGCODE(this->validate());
711 SkASSERT(fContext);
712
713 const GrCaps& caps = *fContext->priv().caps();
714
715 sk_sp<GrWaitRenderTask> waitTask = sk_make_sp<GrWaitRenderTask>(proxy, std::move(semaphores),
716 numSemaphores);
717 if (fReduceOpsTaskSplitting) {
718 GrRenderTask* lastTask = proxy->getLastRenderTask();
719 if (lastTask && !lastTask->isClosed()) {
720 // We directly make the currently open renderTask depend on waitTask instead of using
721 // the proxy version of addDependency. The waitTask will never need to trigger any
722 // resolves or mip map generation which is the main advantage of going through the proxy
723 // version. Additionally we would've had to temporarily set the wait task as the
724 // lastRenderTask on the proxy, add the dependency, and then reset the lastRenderTask to
725 // lastTask. Additionally we add all dependencies of lastTask to waitTask so that the
726 // waitTask doesn't get reordered before them and unnecessarily block those tasks.
727 // Note: Any previous Ops already in lastTask will get blocked by the wait semaphore
728 // even though they don't need to be for correctness.
729
730 // Make sure we add the dependencies of lastTask to waitTask first or else we'll get a
731 // circular self dependency of waitTask on waitTask.
732 waitTask->addDependenciesFromOtherTask(lastTask);
733 lastTask->addDependency(waitTask.get());
734 } else {
735 // If there is a last task we set the waitTask to depend on it so that it doesn't get
736 // reordered in front of the lastTask causing the lastTask to be blocked by the
737 // semaphore. Again we directly just go through adding the dependency to the task and
738 // not the proxy since we don't need to worry about resolving anything.
739 if (lastTask) {
740 waitTask->addDependency(lastTask);
741 }
742 proxy->setLastRenderTask(waitTask.get());
743 }
744 fDAG.add(waitTask);
745 } else {
746 if (fActiveOpsTask && (fActiveOpsTask->fTarget == proxy)) {
747 SkASSERT(proxy->getLastRenderTask() == fActiveOpsTask);
748 fDAG.addBeforeLast(waitTask);
749 // In this case we keep the current renderTask open but just insert the new waitTask
750 // before it in the list. The waitTask will never need to trigger any resolves or mip
751 // map generation which is the main advantage of going through the proxy version.
752 // Additionally we would've had to temporarily set the wait task as the lastRenderTask
753 // on the proxy, add the dependency, and then reset the lastRenderTask to
754 // fActiveOpsTask. Additionally we make the waitTask depend on all of fActiveOpsTask
755 // dependencies so that we don't unnecessarily reorder the waitTask before them.
756 // Note: Any previous Ops already in fActiveOpsTask will get blocked by the wait
757 // semaphore even though they don't need to be for correctness.
758
759 // Make sure we add the dependencies of fActiveOpsTask to waitTask first or else we'll
760 // get a circular self dependency of waitTask on waitTask.
761 waitTask->addDependenciesFromOtherTask(fActiveOpsTask);
762 fActiveOpsTask->addDependency(waitTask.get());
763 } else {
764 // In this case we just close the previous RenderTask and start and append the waitTask
765 // to the DAG. Since it is the last task now we call setLastRenderTask on the proxy. If
766 // there is a lastTask on the proxy we make waitTask depend on that task. This
767 // dependency isn't strictly needed but it does keep the DAG from reordering the
768 // waitTask earlier and blocking more tasks.
769 if (GrRenderTask* lastTask = proxy->getLastRenderTask()) {
770 waitTask->addDependency(lastTask);
771 }
772 proxy->setLastRenderTask(waitTask.get());
773 this->closeRenderTasksForNewRenderTask(proxy.get());
774 fDAG.add(waitTask);
775 }
776 }
777 waitTask->makeClosed(caps);
778
779 SkDEBUGCODE(this->validate());
780}
781
Greg Danielbbfec9d2019-08-20 10:56:51 -0400782void GrDrawingManager::newTransferFromRenderTask(sk_sp<GrSurfaceProxy> srcProxy,
783 const SkIRect& srcRect,
784 GrColorType surfaceColorType,
785 GrColorType dstColorType,
786 sk_sp<GrGpuBuffer> dstBuffer,
787 size_t dstOffset) {
788 SkDEBUGCODE(this->validate());
789 SkASSERT(fContext);
790 // This copies from srcProxy to dstBuffer so it doesn't have a real target.
791 this->closeRenderTasksForNewRenderTask(nullptr);
792
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600793 GrRenderTask* task = fDAG.add(sk_make_sp<GrTransferFromRenderTask>(
794 srcProxy, srcRect, surfaceColorType, dstColorType, std::move(dstBuffer), dstOffset));
Greg Danielbbfec9d2019-08-20 10:56:51 -0400795
796 const GrCaps& caps = *fContext->priv().caps();
797
798 // We always say GrMipMapped::kNo here since we are always just copying from the base layer. We
799 // don't need to make sure the whole mip map chain is valid.
800 task->addDependency(srcProxy.get(), GrMipMapped::kNo, GrTextureResolveManager(this), caps);
801 task->makeClosed(caps);
802
Greg Danielbbfec9d2019-08-20 10:56:51 -0400803 // We have closed the previous active oplist but since a new oplist isn't being added there
804 // shouldn't be an active one.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400805 SkASSERT(!fActiveOpsTask);
Greg Danielbbfec9d2019-08-20 10:56:51 -0400806 SkDEBUGCODE(this->validate());
807}
808
Greg Daniele227fe42019-08-21 13:52:24 -0400809bool GrDrawingManager::newCopyRenderTask(sk_sp<GrSurfaceProxy> srcProxy,
810 const SkIRect& srcRect,
811 sk_sp<GrSurfaceProxy> dstProxy,
812 const SkIPoint& dstPoint) {
813 SkDEBUGCODE(this->validate());
814 SkASSERT(fContext);
Greg Daniele227fe42019-08-21 13:52:24 -0400815
Brian Salomone4bce012019-09-20 15:34:23 -0400816 this->closeRenderTasksForNewRenderTask(dstProxy.get());
817 const GrCaps& caps = *fContext->priv().caps();
818
819 GrRenderTask* task =
820 fDAG.add(GrCopyRenderTask::Make(srcProxy, srcRect, dstProxy, dstPoint, &caps));
Greg Daniele227fe42019-08-21 13:52:24 -0400821 if (!task) {
822 return false;
823 }
824
Greg Daniele227fe42019-08-21 13:52:24 -0400825
826 // We always say GrMipMapped::kNo here since we are always just copying from the base layer to
827 // another base layer. We don't need to make sure the whole mip map chain is valid.
828 task->addDependency(srcProxy.get(), GrMipMapped::kNo, GrTextureResolveManager(this), caps);
829 task->makeClosed(caps);
830
Greg Daniele227fe42019-08-21 13:52:24 -0400831 // We have closed the previous active oplist but since a new oplist isn't being added there
832 // shouldn't be an active one.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400833 SkASSERT(!fActiveOpsTask);
Greg Daniele227fe42019-08-21 13:52:24 -0400834 SkDEBUGCODE(this->validate());
835 return true;
836}
837
Herb Derby26cbe512018-05-24 14:39:01 -0400838GrTextContext* GrDrawingManager::getTextContext() {
839 if (!fTextContext) {
840 fTextContext = GrTextContext::Make(fOptionsForTextContext);
brianosman86e76262016-08-11 12:17:31 -0700841 }
842
Herb Derby26cbe512018-05-24 14:39:01 -0400843 return fTextContext.get();
brianosman86e76262016-08-11 12:17:31 -0700844}
845
robertphillips68737822015-10-29 12:12:21 -0700846/*
847 * This method finds a path renderer that can draw the specified path on
848 * the provided target.
849 * Due to its expense, the software path renderer has split out so it can
850 * can be individually allowed/disallowed via the "allowSW" boolean.
851 */
852GrPathRenderer* GrDrawingManager::getPathRenderer(const GrPathRenderer::CanDrawPathArgs& args,
853 bool allowSW,
854 GrPathRendererChain::DrawType drawType,
855 GrPathRenderer::StencilSupport* stencilSupport) {
856
857 if (!fPathRendererChain) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400858 fPathRendererChain.reset(new GrPathRendererChain(fContext, fOptionsForPathRendererChain));
robertphillips68737822015-10-29 12:12:21 -0700859 }
860
861 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(args, drawType, stencilSupport);
862 if (!pr && allowSW) {
Brian Salomone7df0bb2018-05-07 14:44:57 -0400863 auto swPR = this->getSoftwarePathRenderer();
864 if (GrPathRenderer::CanDrawPath::kNo != swPR->canDrawPath(args)) {
865 pr = swPR;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500866 }
robertphillips68737822015-10-29 12:12:21 -0700867 }
868
869 return pr;
870}
871
Brian Salomone7df0bb2018-05-07 14:44:57 -0400872GrPathRenderer* GrDrawingManager::getSoftwarePathRenderer() {
873 if (!fSoftwarePathRenderer) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400874 fSoftwarePathRenderer.reset(
Robert Phillips9da87e02019-02-04 13:26:26 -0500875 new GrSoftwarePathRenderer(fContext->priv().proxyProvider(),
Ben Wagner9ec70c62018-07-12 13:30:47 -0400876 fOptionsForPathRendererChain.fAllowPathMaskCaching));
Brian Salomone7df0bb2018-05-07 14:44:57 -0400877 }
Ben Wagner9ec70c62018-07-12 13:30:47 -0400878 return fSoftwarePathRenderer.get();
Brian Salomone7df0bb2018-05-07 14:44:57 -0400879}
880
Chris Daltonfddb6c02017-11-04 15:22:22 -0600881GrCoverageCountingPathRenderer* GrDrawingManager::getCoverageCountingPathRenderer() {
882 if (!fPathRendererChain) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400883 fPathRendererChain.reset(new GrPathRendererChain(fContext, fOptionsForPathRendererChain));
Chris Daltonfddb6c02017-11-04 15:22:22 -0600884 }
885 return fPathRendererChain->getCoverageCountingPathRenderer();
886}
887
Brian Salomon653f42f2018-07-10 10:07:31 -0400888void GrDrawingManager::flushIfNecessary() {
Robert Phillips6a6de562019-02-15 15:19:15 -0500889 auto direct = fContext->priv().asDirectContext();
890 if (!direct) {
891 return;
892 }
893
894 auto resourceCache = direct->priv().getResourceCache();
Brian Salomon653f42f2018-07-10 10:07:31 -0400895 if (resourceCache && resourceCache->requestsFlush()) {
Greg Daniel797efca2019-05-09 14:04:20 -0400896 this->flush(nullptr, 0, SkSurface::BackendSurfaceAccess::kNoAccess, GrFlushInfo(),
897 GrPrepareForExternalIORequests());
Brian Salomon57d2bea2018-09-10 09:35:41 -0400898 resourceCache->purgeAsNeeded();
Brian Salomon653f42f2018-07-10 10:07:31 -0400899 }
900}
901
Brian Salomonbf6b9792019-08-21 09:38:10 -0400902std::unique_ptr<GrRenderTargetContext> GrDrawingManager::makeRenderTargetContext(
Brian Salomond6287472019-06-24 15:50:07 -0400903 sk_sp<GrSurfaceProxy> sProxy,
904 GrColorType colorType,
905 sk_sp<SkColorSpace> colorSpace,
906 const SkSurfaceProps* surfaceProps,
Greg Danielf41b2bd2019-08-22 16:19:24 -0400907 bool managedOpsTask) {
Robert Phillips37430132016-11-09 06:50:43 -0500908 if (this->wasAbandoned() || !sProxy->asRenderTargetProxy()) {
robertphillips3dc6ae52015-10-20 09:54:32 -0700909 return nullptr;
910 }
911
Robert Phillips0d075de2019-03-04 11:08:13 -0500912 sk_sp<GrRenderTargetProxy> renderTargetProxy(sk_ref_sp(sProxy->asRenderTargetProxy()));
Robert Phillips2c862492017-01-18 10:08:39 -0500913
Brian Salomonbf6b9792019-08-21 09:38:10 -0400914 return std::unique_ptr<GrRenderTargetContext>(
915 new GrRenderTargetContext(fContext,
916 std::move(renderTargetProxy),
917 colorType,
918 std::move(colorSpace),
919 surfaceProps,
Greg Danielf41b2bd2019-08-22 16:19:24 -0400920 managedOpsTask));
robertphillips3dc6ae52015-10-20 09:54:32 -0700921}
Brian Osman45580d32016-11-23 09:37:01 -0500922
Brian Salomonbf6b9792019-08-21 09:38:10 -0400923std::unique_ptr<GrTextureContext> GrDrawingManager::makeTextureContext(
924 sk_sp<GrSurfaceProxy> sProxy,
925 GrColorType colorType,
926 SkAlphaType alphaType,
927 sk_sp<SkColorSpace> colorSpace) {
Brian Osman45580d32016-11-23 09:37:01 -0500928 if (this->wasAbandoned() || !sProxy->asTextureProxy()) {
929 return nullptr;
930 }
931
Robert Phillips383c4182018-02-07 12:47:44 -0500932 // GrTextureRenderTargets should always be using a GrRenderTargetContext
Brian Osman45580d32016-11-23 09:37:01 -0500933 SkASSERT(!sProxy->asRenderTargetProxy());
934
935 sk_sp<GrTextureProxy> textureProxy(sk_ref_sp(sProxy->asTextureProxy()));
936
Brian Salomonbf6b9792019-08-21 09:38:10 -0400937 return std::unique_ptr<GrTextureContext>(new GrTextureContext(
938 fContext, std::move(textureProxy), colorType, alphaType, std::move(colorSpace)));
Brian Osman45580d32016-11-23 09:37:01 -0500939}