blob: ba89eca301bcfdd56849deb4fd561c2c6cbd6bf6 [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 Phillips61fc7992019-10-22 11:58:17 -0400584 ddl->fOpPOD = fContext->priv().detachOpPOD();
585
Robert Phillips774168e2018-05-31 12:43:27 -0400586 if (fPathRendererChain) {
587 if (auto ccpr = fPathRendererChain->getCoverageCountingPathRenderer()) {
588 ddl->fPendingPaths = ccpr->detachPendingPaths();
589 }
590 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400591
592 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500593}
594
Chris Dalton6b498102019-08-01 14:14:52 -0600595void GrDrawingManager::copyRenderTasksFromDDL(const SkDeferredDisplayList* ddl,
Robert Phillips62000362018-02-01 09:10:04 -0500596 GrRenderTargetProxy* newDest) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400597 SkDEBUGCODE(this->validate());
598
Greg Danielf41b2bd2019-08-22 16:19:24 -0400599 if (fActiveOpsTask) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400600 // This is a temporary fix for the partial-MDB world. In that world we're not
Greg Danielf41b2bd2019-08-22 16:19:24 -0400601 // reordering so ops that (in the single opsTask world) would've just glommed onto the
602 // end of the single opsTask but referred to a far earlier RT need to appear in their
603 // own opsTask.
604 fActiveOpsTask->makeClosed(*fContext->priv().caps());
605 fActiveOpsTask = nullptr;
Robert Phillips38d64b02018-09-04 13:23:26 -0400606 }
607
Robert Phillips15c91422019-05-07 16:54:48 -0400608 this->addDDLTarget(newDest);
609
Robert Phillips62000362018-02-01 09:10:04 -0500610 // Here we jam the proxy that backs the current replay SkSurface into the LazyProxyData.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400611 // The lazy proxy that references it (in the copied opsTasks) will steal its GrTexture.
Robert Phillips62000362018-02-01 09:10:04 -0500612 ddl->fLazyProxyData->fReplayDest = newDest;
Robert Phillips774168e2018-05-31 12:43:27 -0400613
614 if (ddl->fPendingPaths.size()) {
615 GrCoverageCountingPathRenderer* ccpr = this->getCoverageCountingPathRenderer();
616
617 ccpr->mergePendingPaths(ddl->fPendingPaths);
618 }
Robert Phillips22310d62018-09-05 11:07:21 -0400619
Chris Dalton6b498102019-08-01 14:14:52 -0600620 fDAG.add(ddl->fRenderTasks);
Robert Phillips38d64b02018-09-04 13:23:26 -0400621
622 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500623}
624
Robert Phillips38d64b02018-09-04 13:23:26 -0400625#ifdef SK_DEBUG
626void GrDrawingManager::validate() const {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400627 if (fDAG.sortingRenderTasks() && fReduceOpsTaskSplitting) {
628 SkASSERT(!fActiveOpsTask);
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400629 } else {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400630 if (fActiveOpsTask) {
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400631 SkASSERT(!fDAG.empty());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400632 SkASSERT(!fActiveOpsTask->isClosed());
633 SkASSERT(fActiveOpsTask == fDAG.back());
Robert Phillips38d64b02018-09-04 13:23:26 -0400634 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400635
Chris Dalton6b498102019-08-01 14:14:52 -0600636 for (int i = 0; i < fDAG.numRenderTasks(); ++i) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400637 if (fActiveOpsTask != fDAG.renderTask(i)) {
Chris Daltone2a903e2019-09-18 13:41:50 -0600638 // The resolveTask associated with the activeTask remains open for as long as the
639 // activeTask does.
640 bool isActiveResolveTask =
641 fActiveOpsTask && fActiveOpsTask->fTextureResolveTask == fDAG.renderTask(i);
642 SkASSERT(isActiveResolveTask || fDAG.renderTask(i)->isClosed());
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400643 }
644 }
645
646 if (!fDAG.empty() && !fDAG.back()->isClosed()) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400647 SkASSERT(fActiveOpsTask == fDAG.back());
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400648 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400649 }
650}
651#endif
652
Greg Danielbbfec9d2019-08-20 10:56:51 -0400653void GrDrawingManager::closeRenderTasksForNewRenderTask(GrSurfaceProxy* target) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400654 if (target && fDAG.sortingRenderTasks() && fReduceOpsTaskSplitting) {
Chris Dalton5fe99772019-08-06 11:57:39 -0600655 // In this case we need to close all the renderTasks that rely on the current contents of
656 // 'target'. That is bc we're going to update the content of the proxy so they need to be
657 // split in case they use both the old and new content. (This is a bit of an overkill: they
658 // really only need to be split if they ever reference proxy's contents again but that is
659 // hard to predict/handle).
660 if (GrRenderTask* lastRenderTask = target->getLastRenderTask()) {
Chris Dalton6b498102019-08-01 14:14:52 -0600661 lastRenderTask->closeThoseWhoDependOnMe(*fContext->priv().caps());
Robert Phillips46acf9d2018-10-09 09:31:40 -0400662 }
Greg Danielf41b2bd2019-08-22 16:19:24 -0400663 } else if (fActiveOpsTask) {
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400664 // This is a temporary fix for the partial-MDB world. In that world we're not
Greg Danielf41b2bd2019-08-22 16:19:24 -0400665 // reordering so ops that (in the single opsTask world) would've just glommed onto the
666 // end of the single opsTask but referred to a far earlier RT need to appear in their
667 // own opsTask.
668 fActiveOpsTask->makeClosed(*fContext->priv().caps());
669 fActiveOpsTask = nullptr;
robertphillips3dc6ae52015-10-20 09:54:32 -0700670 }
Chris Dalton5fe99772019-08-06 11:57:39 -0600671}
672
Greg Danielf41b2bd2019-08-22 16:19:24 -0400673sk_sp<GrOpsTask> GrDrawingManager::newOpsTask(sk_sp<GrRenderTargetProxy> rtp, bool managedOpsTask) {
Chris Dalton5fe99772019-08-06 11:57:39 -0600674 SkDEBUGCODE(this->validate());
675 SkASSERT(fContext);
676
Greg Danielbbfec9d2019-08-20 10:56:51 -0400677 this->closeRenderTasksForNewRenderTask(rtp.get());
robertphillips3dc6ae52015-10-20 09:54:32 -0700678
Greg Danielf41b2bd2019-08-22 16:19:24 -0400679 sk_sp<GrOpsTask> opsTask(new GrOpsTask(fContext->priv().refOpMemoryPool(), rtp,
680 fContext->priv().auditTrail()));
681 SkASSERT(rtp->getLastRenderTask() == opsTask.get());
robertphillips3dc6ae52015-10-20 09:54:32 -0700682
Greg Danielf41b2bd2019-08-22 16:19:24 -0400683 if (managedOpsTask) {
684 fDAG.add(opsTask);
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400685
Greg Danielf41b2bd2019-08-22 16:19:24 -0400686 if (!fDAG.sortingRenderTasks() || !fReduceOpsTaskSplitting) {
687 fActiveOpsTask = opsTask.get();
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400688 }
Robert Phillips941d1442017-06-14 16:37:02 -0400689 }
robertphillips3dc6ae52015-10-20 09:54:32 -0700690
Robert Phillips38d64b02018-09-04 13:23:26 -0400691 SkDEBUGCODE(this->validate());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400692 return opsTask;
robertphillips3dc6ae52015-10-20 09:54:32 -0700693}
694
Chris Daltone2a903e2019-09-18 13:41:50 -0600695GrTextureResolveRenderTask* GrDrawingManager::newTextureResolveRenderTask(const GrCaps& caps) {
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600696 // 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 -0600697 // in sorting and opsTask reduction mode) the render tasks that depend on any proxy's current
Greg Danielf41b2bd2019-08-22 16:19:24 -0400698 // state. This is because those opsTasks can still receive new ops and because if they refer to
Chris Dalton4ece96d2019-08-30 11:26:39 -0600699 // the mipmapped version of 'proxy', they will then come to depend on the render task being
700 // created here.
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600701 //
Greg Danielf41b2bd2019-08-22 16:19:24 -0400702 // Add the new textureResolveTask before the fActiveOpsTask (if not in
703 // sorting/opsTask-splitting-reduction mode) because it will depend upon this resolve task.
Chris Dalton3d770272019-08-14 09:24:37 -0600704 // NOTE: Putting it here will also reduce the amount of work required by the topological sort.
Chris Daltone2a903e2019-09-18 13:41:50 -0600705 return static_cast<GrTextureResolveRenderTask*>(fDAG.addBeforeLast(
706 sk_make_sp<GrTextureResolveRenderTask>()));
Chris Dalton3d770272019-08-14 09:24:37 -0600707}
708
Greg Danielc30f1a92019-09-06 15:28:58 -0400709void GrDrawingManager::newWaitRenderTask(sk_sp<GrSurfaceProxy> proxy,
710 std::unique_ptr<sk_sp<GrSemaphore>[]> semaphores,
711 int numSemaphores) {
712 SkDEBUGCODE(this->validate());
713 SkASSERT(fContext);
714
715 const GrCaps& caps = *fContext->priv().caps();
716
717 sk_sp<GrWaitRenderTask> waitTask = sk_make_sp<GrWaitRenderTask>(proxy, std::move(semaphores),
718 numSemaphores);
719 if (fReduceOpsTaskSplitting) {
720 GrRenderTask* lastTask = proxy->getLastRenderTask();
721 if (lastTask && !lastTask->isClosed()) {
722 // We directly make the currently open renderTask depend on waitTask instead of using
723 // the proxy version of addDependency. The waitTask will never need to trigger any
724 // resolves or mip map generation which is the main advantage of going through the proxy
725 // version. Additionally we would've had to temporarily set the wait task as the
726 // lastRenderTask on the proxy, add the dependency, and then reset the lastRenderTask to
727 // lastTask. Additionally we add all dependencies of lastTask to waitTask so that the
728 // waitTask doesn't get reordered before them and unnecessarily block those tasks.
729 // Note: Any previous Ops already in lastTask will get blocked by the wait semaphore
730 // even though they don't need to be for correctness.
731
732 // Make sure we add the dependencies of lastTask to waitTask first or else we'll get a
733 // circular self dependency of waitTask on waitTask.
734 waitTask->addDependenciesFromOtherTask(lastTask);
735 lastTask->addDependency(waitTask.get());
736 } else {
737 // If there is a last task we set the waitTask to depend on it so that it doesn't get
738 // reordered in front of the lastTask causing the lastTask to be blocked by the
739 // semaphore. Again we directly just go through adding the dependency to the task and
740 // not the proxy since we don't need to worry about resolving anything.
741 if (lastTask) {
742 waitTask->addDependency(lastTask);
743 }
744 proxy->setLastRenderTask(waitTask.get());
745 }
746 fDAG.add(waitTask);
747 } else {
748 if (fActiveOpsTask && (fActiveOpsTask->fTarget == proxy)) {
749 SkASSERT(proxy->getLastRenderTask() == fActiveOpsTask);
750 fDAG.addBeforeLast(waitTask);
751 // In this case we keep the current renderTask open but just insert the new waitTask
752 // before it in the list. The waitTask will never need to trigger any resolves or mip
753 // map generation which is the main advantage of going through the proxy version.
754 // Additionally we would've had to temporarily set the wait task as the lastRenderTask
755 // on the proxy, add the dependency, and then reset the lastRenderTask to
756 // fActiveOpsTask. Additionally we make the waitTask depend on all of fActiveOpsTask
757 // dependencies so that we don't unnecessarily reorder the waitTask before them.
758 // Note: Any previous Ops already in fActiveOpsTask will get blocked by the wait
759 // semaphore even though they don't need to be for correctness.
760
761 // Make sure we add the dependencies of fActiveOpsTask to waitTask first or else we'll
762 // get a circular self dependency of waitTask on waitTask.
763 waitTask->addDependenciesFromOtherTask(fActiveOpsTask);
764 fActiveOpsTask->addDependency(waitTask.get());
765 } else {
766 // In this case we just close the previous RenderTask and start and append the waitTask
767 // to the DAG. Since it is the last task now we call setLastRenderTask on the proxy. If
768 // there is a lastTask on the proxy we make waitTask depend on that task. This
769 // dependency isn't strictly needed but it does keep the DAG from reordering the
770 // waitTask earlier and blocking more tasks.
771 if (GrRenderTask* lastTask = proxy->getLastRenderTask()) {
772 waitTask->addDependency(lastTask);
773 }
774 proxy->setLastRenderTask(waitTask.get());
775 this->closeRenderTasksForNewRenderTask(proxy.get());
776 fDAG.add(waitTask);
777 }
778 }
779 waitTask->makeClosed(caps);
780
781 SkDEBUGCODE(this->validate());
782}
783
Greg Danielbbfec9d2019-08-20 10:56:51 -0400784void GrDrawingManager::newTransferFromRenderTask(sk_sp<GrSurfaceProxy> srcProxy,
785 const SkIRect& srcRect,
786 GrColorType surfaceColorType,
787 GrColorType dstColorType,
788 sk_sp<GrGpuBuffer> dstBuffer,
789 size_t dstOffset) {
790 SkDEBUGCODE(this->validate());
791 SkASSERT(fContext);
792 // This copies from srcProxy to dstBuffer so it doesn't have a real target.
793 this->closeRenderTasksForNewRenderTask(nullptr);
794
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600795 GrRenderTask* task = fDAG.add(sk_make_sp<GrTransferFromRenderTask>(
796 srcProxy, srcRect, surfaceColorType, dstColorType, std::move(dstBuffer), dstOffset));
Greg Danielbbfec9d2019-08-20 10:56:51 -0400797
798 const GrCaps& caps = *fContext->priv().caps();
799
800 // We always say GrMipMapped::kNo here since we are always just copying from the base layer. We
801 // don't need to make sure the whole mip map chain is valid.
802 task->addDependency(srcProxy.get(), GrMipMapped::kNo, GrTextureResolveManager(this), caps);
803 task->makeClosed(caps);
804
Greg Danielbbfec9d2019-08-20 10:56:51 -0400805 // We have closed the previous active oplist but since a new oplist isn't being added there
806 // shouldn't be an active one.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400807 SkASSERT(!fActiveOpsTask);
Greg Danielbbfec9d2019-08-20 10:56:51 -0400808 SkDEBUGCODE(this->validate());
809}
810
Greg Daniele227fe42019-08-21 13:52:24 -0400811bool GrDrawingManager::newCopyRenderTask(sk_sp<GrSurfaceProxy> srcProxy,
812 const SkIRect& srcRect,
813 sk_sp<GrSurfaceProxy> dstProxy,
814 const SkIPoint& dstPoint) {
815 SkDEBUGCODE(this->validate());
816 SkASSERT(fContext);
Greg Daniele227fe42019-08-21 13:52:24 -0400817
Brian Salomone4bce012019-09-20 15:34:23 -0400818 this->closeRenderTasksForNewRenderTask(dstProxy.get());
819 const GrCaps& caps = *fContext->priv().caps();
820
821 GrRenderTask* task =
822 fDAG.add(GrCopyRenderTask::Make(srcProxy, srcRect, dstProxy, dstPoint, &caps));
Greg Daniele227fe42019-08-21 13:52:24 -0400823 if (!task) {
824 return false;
825 }
826
Greg Daniele227fe42019-08-21 13:52:24 -0400827
828 // We always say GrMipMapped::kNo here since we are always just copying from the base layer to
829 // another base layer. We don't need to make sure the whole mip map chain is valid.
830 task->addDependency(srcProxy.get(), GrMipMapped::kNo, GrTextureResolveManager(this), caps);
831 task->makeClosed(caps);
832
Greg Daniele227fe42019-08-21 13:52:24 -0400833 // We have closed the previous active oplist but since a new oplist isn't being added there
834 // shouldn't be an active one.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400835 SkASSERT(!fActiveOpsTask);
Greg Daniele227fe42019-08-21 13:52:24 -0400836 SkDEBUGCODE(this->validate());
837 return true;
838}
839
Herb Derby26cbe512018-05-24 14:39:01 -0400840GrTextContext* GrDrawingManager::getTextContext() {
841 if (!fTextContext) {
842 fTextContext = GrTextContext::Make(fOptionsForTextContext);
brianosman86e76262016-08-11 12:17:31 -0700843 }
844
Herb Derby26cbe512018-05-24 14:39:01 -0400845 return fTextContext.get();
brianosman86e76262016-08-11 12:17:31 -0700846}
847
robertphillips68737822015-10-29 12:12:21 -0700848/*
849 * This method finds a path renderer that can draw the specified path on
850 * the provided target.
851 * Due to its expense, the software path renderer has split out so it can
852 * can be individually allowed/disallowed via the "allowSW" boolean.
853 */
854GrPathRenderer* GrDrawingManager::getPathRenderer(const GrPathRenderer::CanDrawPathArgs& args,
855 bool allowSW,
856 GrPathRendererChain::DrawType drawType,
857 GrPathRenderer::StencilSupport* stencilSupport) {
858
859 if (!fPathRendererChain) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400860 fPathRendererChain.reset(new GrPathRendererChain(fContext, fOptionsForPathRendererChain));
robertphillips68737822015-10-29 12:12:21 -0700861 }
862
863 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(args, drawType, stencilSupport);
864 if (!pr && allowSW) {
Brian Salomone7df0bb2018-05-07 14:44:57 -0400865 auto swPR = this->getSoftwarePathRenderer();
866 if (GrPathRenderer::CanDrawPath::kNo != swPR->canDrawPath(args)) {
867 pr = swPR;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500868 }
robertphillips68737822015-10-29 12:12:21 -0700869 }
870
871 return pr;
872}
873
Brian Salomone7df0bb2018-05-07 14:44:57 -0400874GrPathRenderer* GrDrawingManager::getSoftwarePathRenderer() {
875 if (!fSoftwarePathRenderer) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400876 fSoftwarePathRenderer.reset(
Robert Phillips9da87e02019-02-04 13:26:26 -0500877 new GrSoftwarePathRenderer(fContext->priv().proxyProvider(),
Ben Wagner9ec70c62018-07-12 13:30:47 -0400878 fOptionsForPathRendererChain.fAllowPathMaskCaching));
Brian Salomone7df0bb2018-05-07 14:44:57 -0400879 }
Ben Wagner9ec70c62018-07-12 13:30:47 -0400880 return fSoftwarePathRenderer.get();
Brian Salomone7df0bb2018-05-07 14:44:57 -0400881}
882
Chris Daltonfddb6c02017-11-04 15:22:22 -0600883GrCoverageCountingPathRenderer* GrDrawingManager::getCoverageCountingPathRenderer() {
884 if (!fPathRendererChain) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400885 fPathRendererChain.reset(new GrPathRendererChain(fContext, fOptionsForPathRendererChain));
Chris Daltonfddb6c02017-11-04 15:22:22 -0600886 }
887 return fPathRendererChain->getCoverageCountingPathRenderer();
888}
889
Brian Salomon653f42f2018-07-10 10:07:31 -0400890void GrDrawingManager::flushIfNecessary() {
Robert Phillips6a6de562019-02-15 15:19:15 -0500891 auto direct = fContext->priv().asDirectContext();
892 if (!direct) {
893 return;
894 }
895
896 auto resourceCache = direct->priv().getResourceCache();
Brian Salomon653f42f2018-07-10 10:07:31 -0400897 if (resourceCache && resourceCache->requestsFlush()) {
Greg Daniel797efca2019-05-09 14:04:20 -0400898 this->flush(nullptr, 0, SkSurface::BackendSurfaceAccess::kNoAccess, GrFlushInfo(),
899 GrPrepareForExternalIORequests());
Brian Salomon57d2bea2018-09-10 09:35:41 -0400900 resourceCache->purgeAsNeeded();
Brian Salomon653f42f2018-07-10 10:07:31 -0400901 }
902}
903
Brian Salomonbf6b9792019-08-21 09:38:10 -0400904std::unique_ptr<GrRenderTargetContext> GrDrawingManager::makeRenderTargetContext(
Brian Salomond6287472019-06-24 15:50:07 -0400905 sk_sp<GrSurfaceProxy> sProxy,
906 GrColorType colorType,
907 sk_sp<SkColorSpace> colorSpace,
908 const SkSurfaceProps* surfaceProps,
Greg Danielf41b2bd2019-08-22 16:19:24 -0400909 bool managedOpsTask) {
Robert Phillips37430132016-11-09 06:50:43 -0500910 if (this->wasAbandoned() || !sProxy->asRenderTargetProxy()) {
robertphillips3dc6ae52015-10-20 09:54:32 -0700911 return nullptr;
912 }
913
Robert Phillips0d075de2019-03-04 11:08:13 -0500914 sk_sp<GrRenderTargetProxy> renderTargetProxy(sk_ref_sp(sProxy->asRenderTargetProxy()));
Robert Phillips2c862492017-01-18 10:08:39 -0500915
Greg Daniela83de582019-10-22 09:33:25 -0400916 GrSurfaceOrigin origin = renderTargetProxy->origin();
917 GrSwizzle texSwizzle = renderTargetProxy->textureSwizzle();
918 GrSwizzle outSwizzle = renderTargetProxy->outputSwizzle();
919
Brian Salomonbf6b9792019-08-21 09:38:10 -0400920 return std::unique_ptr<GrRenderTargetContext>(
921 new GrRenderTargetContext(fContext,
922 std::move(renderTargetProxy),
923 colorType,
Greg Daniela83de582019-10-22 09:33:25 -0400924 origin,
925 texSwizzle,
926 outSwizzle,
Brian Salomonbf6b9792019-08-21 09:38:10 -0400927 std::move(colorSpace),
928 surfaceProps,
Greg Danielf41b2bd2019-08-22 16:19:24 -0400929 managedOpsTask));
robertphillips3dc6ae52015-10-20 09:54:32 -0700930}
Brian Osman45580d32016-11-23 09:37:01 -0500931
Brian Salomonbf6b9792019-08-21 09:38:10 -0400932std::unique_ptr<GrTextureContext> GrDrawingManager::makeTextureContext(
933 sk_sp<GrSurfaceProxy> sProxy,
934 GrColorType colorType,
935 SkAlphaType alphaType,
936 sk_sp<SkColorSpace> colorSpace) {
Brian Osman45580d32016-11-23 09:37:01 -0500937 if (this->wasAbandoned() || !sProxy->asTextureProxy()) {
938 return nullptr;
939 }
940
Robert Phillips383c4182018-02-07 12:47:44 -0500941 // GrTextureRenderTargets should always be using a GrRenderTargetContext
Brian Osman45580d32016-11-23 09:37:01 -0500942 SkASSERT(!sProxy->asRenderTargetProxy());
943
944 sk_sp<GrTextureProxy> textureProxy(sk_ref_sp(sProxy->asTextureProxy()));
945
Greg Daniel901b98e2019-10-22 09:54:02 -0400946 GrSurfaceOrigin origin = textureProxy->origin();
947 GrSwizzle texSwizzle = textureProxy->textureSwizzle();
948
Brian Salomonbf6b9792019-08-21 09:38:10 -0400949 return std::unique_ptr<GrTextureContext>(new GrTextureContext(
Greg Daniel901b98e2019-10-22 09:54:02 -0400950 fContext, std::move(textureProxy), colorType, alphaType, std::move(colorSpace), origin,
951 texSwizzle));
Brian Osman45580d32016-11-23 09:37:01 -0500952}