blob: 2502d08152bb683b1fa0fa8ba3afae22203bab93 [file] [log] [blame]
robertphillips3dc6ae52015-10-20 09:54:32 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/GrDrawingManager.h"
Robert Phillips69893702019-02-22 11:16:30 -05009
Robert Phillips4d5594d2020-02-21 14:24:40 -050010#include "include/core/SkDeferredDisplayList.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/gpu/GrBackendSemaphore.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/private/GrRecordingContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/core/SkTTopoSort.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040014#include "src/gpu/GrAuditTrail.h"
Brian Salomon9241a6d2019-10-03 13:26:54 -040015#include "src/gpu/GrClientMappedBufferManager.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/GrContextPriv.h"
Greg Daniele227fe42019-08-21 13:52:24 -040017#include "src/gpu/GrCopyRenderTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrGpu.h"
19#include "src/gpu/GrMemoryPool.h"
20#include "src/gpu/GrOnFlushResourceProvider.h"
21#include "src/gpu/GrRecordingContextPriv.h"
22#include "src/gpu/GrRenderTargetContext.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040023#include "src/gpu/GrRenderTargetProxy.h"
Chris Dalton6b498102019-08-01 14:14:52 -060024#include "src/gpu/GrRenderTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/GrResourceAllocator.h"
26#include "src/gpu/GrResourceProvider.h"
27#include "src/gpu/GrSoftwarePathRenderer.h"
Greg Daniel46e366a2019-12-16 14:38:36 -050028#include "src/gpu/GrSurfaceContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "src/gpu/GrSurfaceProxyPriv.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000030#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "src/gpu/GrTexturePriv.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040032#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050033#include "src/gpu/GrTextureProxyPriv.h"
Chris Dalton3d770272019-08-14 09:24:37 -060034#include "src/gpu/GrTextureResolveRenderTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050035#include "src/gpu/GrTracing.h"
Greg Danielbbfec9d2019-08-20 10:56:51 -040036#include "src/gpu/GrTransferFromRenderTask.h"
Greg Danielc30f1a92019-09-06 15:28:58 -040037#include "src/gpu/GrWaitRenderTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050038#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
39#include "src/gpu/text/GrTextContext.h"
40#include "src/image/SkSurface_Gpu.h"
robertphillips498d7ac2015-10-30 10:11:30 -070041
Chris Dalton6b498102019-08-01 14:14:52 -060042GrDrawingManager::RenderTaskDAG::RenderTaskDAG(bool sortRenderTasks)
43 : fSortRenderTasks(sortRenderTasks) {}
Robert Phillipsa3f70262018-02-08 10:59:38 -050044
Chris Dalton6b498102019-08-01 14:14:52 -060045GrDrawingManager::RenderTaskDAG::~RenderTaskDAG() {}
Robert Phillips22310d62018-09-05 11:07:21 -040046
Chris Dalton6b498102019-08-01 14:14:52 -060047void GrDrawingManager::RenderTaskDAG::gatherIDs(SkSTArray<8, uint32_t, true>* idArray) const {
48 idArray->reset(fRenderTasks.count());
49 for (int i = 0; i < fRenderTasks.count(); ++i) {
50 if (fRenderTasks[i]) {
51 (*idArray)[i] = fRenderTasks[i]->uniqueID();
Robert Phillips22310d62018-09-05 11:07:21 -040052 }
53 }
54}
55
Chris Dalton6b498102019-08-01 14:14:52 -060056void GrDrawingManager::RenderTaskDAG::reset() {
57 fRenderTasks.reset();
Robert Phillips22310d62018-09-05 11:07:21 -040058}
59
Chris Dalton6b498102019-08-01 14:14:52 -060060void GrDrawingManager::RenderTaskDAG::removeRenderTask(int index) {
61 if (!fRenderTasks[index]->unique()) {
Robert Phillips22310d62018-09-05 11:07:21 -040062 // TODO: Eventually this should be guaranteed unique: http://skbug.com/7111
Chris Dalton6b498102019-08-01 14:14:52 -060063 fRenderTasks[index]->endFlush();
Robert Phillips22310d62018-09-05 11:07:21 -040064 }
65
Chris Dalton6b498102019-08-01 14:14:52 -060066 fRenderTasks[index] = nullptr;
Robert Phillips22310d62018-09-05 11:07:21 -040067}
68
Chris Dalton6b498102019-08-01 14:14:52 -060069void GrDrawingManager::RenderTaskDAG::removeRenderTasks(int startIndex, int stopIndex) {
Robert Phillips22310d62018-09-05 11:07:21 -040070 for (int i = startIndex; i < stopIndex; ++i) {
Chris Dalton6b498102019-08-01 14:14:52 -060071 if (!fRenderTasks[i]) {
Robert Phillips22310d62018-09-05 11:07:21 -040072 continue;
73 }
Chris Dalton6b498102019-08-01 14:14:52 -060074 this->removeRenderTask(i);
Robert Phillips22310d62018-09-05 11:07:21 -040075 }
76}
77
Chris Dalton6b498102019-08-01 14:14:52 -060078bool GrDrawingManager::RenderTaskDAG::isUsed(GrSurfaceProxy* proxy) const {
79 for (int i = 0; i < fRenderTasks.count(); ++i) {
80 if (fRenderTasks[i] && fRenderTasks[i]->isUsed(proxy)) {
Robert Phillips9313aa72019-04-09 18:41:27 -040081 return true;
82 }
83 }
84
85 return false;
86}
87
Chris Dalton3d770272019-08-14 09:24:37 -060088GrRenderTask* GrDrawingManager::RenderTaskDAG::add(sk_sp<GrRenderTask> renderTask) {
Chris Dalton6aeb8e82019-08-27 11:52:19 -060089 if (renderTask) {
90 return fRenderTasks.emplace_back(std::move(renderTask)).get();
91 }
92 return nullptr;
Chris Dalton3d770272019-08-14 09:24:37 -060093}
94
95GrRenderTask* GrDrawingManager::RenderTaskDAG::addBeforeLast(sk_sp<GrRenderTask> renderTask) {
96 SkASSERT(!fRenderTasks.empty());
Chris Dalton6aeb8e82019-08-27 11:52:19 -060097 if (renderTask) {
98 // Release 'fRenderTasks.back()' and grab the raw pointer, in case the SkTArray grows
99 // and reallocates during emplace_back.
100 fRenderTasks.emplace_back(fRenderTasks.back().release());
101 return (fRenderTasks[fRenderTasks.count() - 2] = std::move(renderTask)).get();
102 }
103 return nullptr;
Robert Phillips22310d62018-09-05 11:07:21 -0400104}
105
Greg Danielf41b2bd2019-08-22 16:19:24 -0400106void GrDrawingManager::RenderTaskDAG::add(const SkTArray<sk_sp<GrRenderTask>>& renderTasks) {
Robert Phillips19f466d2020-02-26 10:27:07 -0500107#ifdef SK_DEBUG
108 for (auto& renderTask : renderTasks) {
109 SkASSERT(renderTask->unique());
110 }
111#endif
112
Greg Danielf41b2bd2019-08-22 16:19:24 -0400113 fRenderTasks.push_back_n(renderTasks.count(), renderTasks.begin());
Robert Phillips22310d62018-09-05 11:07:21 -0400114}
115
Greg Danielf41b2bd2019-08-22 16:19:24 -0400116void GrDrawingManager::RenderTaskDAG::swap(SkTArray<sk_sp<GrRenderTask>>* renderTasks) {
117 SkASSERT(renderTasks->empty());
118 renderTasks->swap(fRenderTasks);
Robert Phillips22310d62018-09-05 11:07:21 -0400119}
120
Chris Dalton6b498102019-08-01 14:14:52 -0600121void GrDrawingManager::RenderTaskDAG::prepForFlush() {
122 if (fSortRenderTasks) {
123 SkDEBUGCODE(bool result =) SkTTopoSort<GrRenderTask, GrRenderTask::TopoSortTraits>(
124 &fRenderTasks);
Robert Phillips22310d62018-09-05 11:07:21 -0400125 SkASSERT(result);
126 }
127
128#ifdef SK_DEBUG
Greg Danielf41b2bd2019-08-22 16:19:24 -0400129 // This block checks for any unnecessary splits in the opsTasks. If two sequential opsTasks
130 // share the same backing GrSurfaceProxy it means the opsTask was artificially split.
Chris Dalton6b498102019-08-01 14:14:52 -0600131 if (fRenderTasks.count()) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400132 GrOpsTask* prevOpsTask = fRenderTasks[0]->asOpsTask();
Chris Dalton6b498102019-08-01 14:14:52 -0600133 for (int i = 1; i < fRenderTasks.count(); ++i) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400134 GrOpsTask* curOpsTask = fRenderTasks[i]->asOpsTask();
Robert Phillips22310d62018-09-05 11:07:21 -0400135
Greg Danielf41b2bd2019-08-22 16:19:24 -0400136 if (prevOpsTask && curOpsTask) {
Greg Daniel16f5c652019-10-29 11:26:01 -0400137 SkASSERT(prevOpsTask->fTargetView != curOpsTask->fTargetView);
Robert Phillips22310d62018-09-05 11:07:21 -0400138 }
139
Greg Danielf41b2bd2019-08-22 16:19:24 -0400140 prevOpsTask = curOpsTask;
Robert Phillips22310d62018-09-05 11:07:21 -0400141 }
142 }
143#endif
144}
145
Chris Dalton6b498102019-08-01 14:14:52 -0600146void GrDrawingManager::RenderTaskDAG::closeAll(const GrCaps* caps) {
147 for (int i = 0; i < fRenderTasks.count(); ++i) {
148 if (fRenderTasks[i]) {
149 fRenderTasks[i]->makeClosed(*caps);
Robert Phillips22310d62018-09-05 11:07:21 -0400150 }
151 }
152}
153
Chris Dalton6b498102019-08-01 14:14:52 -0600154void GrDrawingManager::RenderTaskDAG::cleanup(const GrCaps* caps) {
155 for (int i = 0; i < fRenderTasks.count(); ++i) {
156 if (!fRenderTasks[i]) {
Robert Phillips22310d62018-09-05 11:07:21 -0400157 continue;
158 }
159
Chris Dalton6b498102019-08-01 14:14:52 -0600160 // no renderTask should receive a dependency
161 fRenderTasks[i]->makeClosed(*caps);
robertphillips0dfa62c2015-11-16 06:23:31 -0800162
Greg Danielf41b2bd2019-08-22 16:19:24 -0400163 // We shouldn't need to do this, but it turns out some clients still hold onto opsTasks
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400164 // after a cleanup.
165 // MDB TODO: is this still true?
Chris Dalton6b498102019-08-01 14:14:52 -0600166 if (!fRenderTasks[i]->unique()) {
Chris Daltona84cacf2017-10-04 10:30:29 -0600167 // TODO: Eventually this should be guaranteed unique.
168 // https://bugs.chromium.org/p/skia/issues/detail?id=7111
Chris Dalton6b498102019-08-01 14:14:52 -0600169 fRenderTasks[i]->endFlush();
Chris Daltona84cacf2017-10-04 10:30:29 -0600170 }
robertphillips3dc6ae52015-10-20 09:54:32 -0700171 }
172
Chris Dalton6b498102019-08-01 14:14:52 -0600173 fRenderTasks.reset();
Robert Phillips22310d62018-09-05 11:07:21 -0400174}
175
176///////////////////////////////////////////////////////////////////////////////////////////////////
Robert Phillips69893702019-02-22 11:16:30 -0500177GrDrawingManager::GrDrawingManager(GrRecordingContext* context,
Robert Phillips22310d62018-09-05 11:07:21 -0400178 const GrPathRendererChain::Options& optionsForPathRendererChain,
179 const GrTextContext::Options& optionsForTextContext,
Chris Dalton6b498102019-08-01 14:14:52 -0600180 bool sortRenderTasks,
Greg Danielf41b2bd2019-08-22 16:19:24 -0400181 bool reduceOpsTaskSplitting)
Robert Phillips22310d62018-09-05 11:07:21 -0400182 : fContext(context)
183 , fOptionsForPathRendererChain(optionsForPathRendererChain)
184 , fOptionsForTextContext(optionsForTextContext)
Chris Dalton6b498102019-08-01 14:14:52 -0600185 , fDAG(sortRenderTasks)
Robert Phillips22310d62018-09-05 11:07:21 -0400186 , fTextContext(nullptr)
187 , fPathRendererChain(nullptr)
188 , fSoftwarePathRenderer(nullptr)
Robert Phillips6db27c22019-05-01 10:43:56 -0400189 , fFlushing(false)
Greg Danielf41b2bd2019-08-22 16:19:24 -0400190 , fReduceOpsTaskSplitting(reduceOpsTaskSplitting) {
Robert Phillips22310d62018-09-05 11:07:21 -0400191}
192
193void GrDrawingManager::cleanup() {
Robert Phillips9da87e02019-02-04 13:26:26 -0500194 fDAG.cleanup(fContext->priv().caps());
robertphillips3dc6ae52015-10-20 09:54:32 -0700195
robertphillips13391dd2015-10-30 05:15:11 -0700196 fPathRendererChain = nullptr;
Ben Wagner9ec70c62018-07-12 13:30:47 -0400197 fSoftwarePathRenderer = nullptr;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400198
199 fOnFlushCBObjects.reset();
robertphillips3dc6ae52015-10-20 09:54:32 -0700200}
201
202GrDrawingManager::~GrDrawingManager() {
203 this->cleanup();
204}
205
Robert Phillipsa9162df2019-02-11 14:12:03 -0500206bool GrDrawingManager::wasAbandoned() const {
Robert Phillips6a6de562019-02-15 15:19:15 -0500207 return fContext->priv().abandoned();
robertphillips3dc6ae52015-10-20 09:54:32 -0700208}
209
robertphillips68737822015-10-29 12:12:21 -0700210void GrDrawingManager::freeGpuResources() {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400211 for (int i = fOnFlushCBObjects.count() - 1; i >= 0; --i) {
212 if (!fOnFlushCBObjects[i]->retainOnFreeGpuResources()) {
213 // it's safe to just do this because we're iterating in reverse
214 fOnFlushCBObjects.removeShuffle(i);
215 }
216 }
217
robertphillips68737822015-10-29 12:12:21 -0700218 // a path renderer may be holding onto resources
robertphillips13391dd2015-10-30 05:15:11 -0700219 fPathRendererChain = nullptr;
Ben Wagner9ec70c62018-07-12 13:30:47 -0400220 fSoftwarePathRenderer = nullptr;
Robert Phillipse3302df2017-04-24 07:31:02 -0400221}
222
Robert Phillips7ee385e2017-03-30 08:02:11 -0400223// MDB TODO: make use of the 'proxy' parameter.
Greg Danielfe159622020-04-10 17:43:51 +0000224bool GrDrawingManager::flush(GrSurfaceProxy* proxies[], int numProxies,
Greg Daniel797efca2019-05-09 14:04:20 -0400225 SkSurface::BackendSurfaceAccess access, const GrFlushInfo& info,
226 const GrPrepareForExternalIORequests& externalRequests) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400227 SkASSERT(numProxies >= 0);
228 SkASSERT(!numProxies || proxies);
Brian Salomon57d2beab2018-09-10 09:35:41 -0400229 GR_CREATE_TRACE_MARKER_CONTEXT("GrDrawingManager", "flush", fContext);
Brian Salomondcbb9d92017-07-19 10:53:20 -0400230
robertphillips7761d612016-05-16 09:14:53 -0700231 if (fFlushing || this->wasAbandoned()) {
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400232 if (info.fFinishedProc) {
233 info.fFinishedProc(info.fFinishedContext);
Greg Daniela3aa75a2019-04-12 14:24:55 -0400234 }
Greg Danielfe159622020-04-10 17:43:51 +0000235 return false;
joshualittb8918c42015-12-18 09:59:46 -0800236 }
Robert Phillips602df412019-04-08 11:10:39 -0400237
Robert Phillips38d64b02018-09-04 13:23:26 -0400238 SkDEBUGCODE(this->validate());
239
Greg Daniel797efca2019-05-09 14:04:20 -0400240 if (kNone_GrFlushFlags == info.fFlags && !info.fNumSemaphores && !info.fFinishedProc &&
Jim Van Verth682a2f42020-05-13 16:54:09 -0400241 access == SkSurface::BackendSurfaceAccess::kNoAccess &&
Greg Daniel797efca2019-05-09 14:04:20 -0400242 !externalRequests.hasRequests()) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400243 bool canSkip = numProxies > 0;
244 for (int i = 0; i < numProxies && canSkip; ++i) {
245 canSkip = !fDAG.isUsed(proxies[i]) && !this->isDDLTarget(proxies[i]);
246 }
247 if (canSkip) {
Greg Danielfe159622020-04-10 17:43:51 +0000248 return false;
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400249 }
Robert Phillips9313aa72019-04-09 18:41:27 -0400250 }
251
Robert Phillips6a6de562019-02-15 15:19:15 -0500252 auto direct = fContext->priv().asDirectContext();
253 if (!direct) {
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400254 if (info.fFinishedProc) {
255 info.fFinishedProc(info.fFinishedContext);
Greg Daniela3aa75a2019-04-12 14:24:55 -0400256 }
Greg Danielfe159622020-04-10 17:43:51 +0000257 return false; // Can't flush while DDL recording
Robert Phillips6a6de562019-02-15 15:19:15 -0500258 }
Brian Salomon9241a6d2019-10-03 13:26:54 -0400259 direct->priv().clientMappedBufferManager()->process();
Robert Phillips6a6de562019-02-15 15:19:15 -0500260
261 GrGpu* gpu = direct->priv().getGpu();
Robert Phillips874b5352018-03-16 08:48:24 -0400262 if (!gpu) {
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400263 if (info.fFinishedProc) {
264 info.fFinishedProc(info.fFinishedContext);
Greg Daniela3aa75a2019-04-12 14:24:55 -0400265 }
Greg Danielfe159622020-04-10 17:43:51 +0000266 return false; // Can't flush while DDL recording
Robert Phillips874b5352018-03-16 08:48:24 -0400267 }
Greg Daniela3aa75a2019-04-12 14:24:55 -0400268
joshualittb8918c42015-12-18 09:59:46 -0800269 fFlushing = true;
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400270
Robert Phillips6a6de562019-02-15 15:19:15 -0500271 auto resourceProvider = direct->priv().resourceProvider();
272 auto resourceCache = direct->priv().getResourceCache();
273
Chris Dalton6b498102019-08-01 14:14:52 -0600274 // Semi-usually the GrRenderTasks are already closed at this point, but sometimes Ganesh needs
Greg Danielf41b2bd2019-08-22 16:19:24 -0400275 // to flush mid-draw. In that case, the SkGpuDevice's opsTasks won't be closed but need to be
276 // flushed anyway. Closing such opsTasks here will mean new ones will be created to replace them
Chris Dalton6b498102019-08-01 14:14:52 -0600277 // if the SkGpuDevice(s) write to them again.
Robert Phillips9da87e02019-02-04 13:26:26 -0500278 fDAG.closeAll(fContext->priv().caps());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400279 fActiveOpsTask = nullptr;
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400280
Robert Phillips22310d62018-09-05 11:07:21 -0400281 fDAG.prepForFlush();
Brian Salomon601ac802019-02-07 13:37:16 -0500282 if (!fCpuBufferCache) {
283 // We cache more buffers when the backend is using client side arrays. Otherwise, we
284 // expect each pool will use a CPU buffer as a staging buffer before uploading to a GPU
285 // buffer object. Each pool only requires one staging buffer at a time.
286 int maxCachedBuffers = fContext->priv().caps()->preferClientSideDynamicBuffers() ? 2 : 6;
287 fCpuBufferCache = GrBufferAllocPool::CpuBufferCache::Make(maxCachedBuffers);
Brian Salomon58f153c2018-10-18 21:51:15 -0400288 }
Robert Phillipsa4c93ac2017-05-18 11:40:04 -0400289
Robert Phillipse5f73282019-06-18 17:15:04 -0400290 GrOpFlushState flushState(gpu, resourceProvider, &fTokenTracker, fCpuBufferCache);
Robert Phillips40a29d72018-01-18 12:59:22 -0500291
Chris Daltonfe199b72017-05-05 11:26:15 -0400292 GrOnFlushResourceProvider onFlushProvider(this);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500293 // TODO: AFAICT the only reason fFlushState is on GrDrawingManager rather than on the
294 // stack here is to preserve the flush tokens.
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400295
Chris Dalton12658942017-10-05 19:45:25 -0600296 // Prepare any onFlush op lists (e.g. atlases).
Chris Daltonfe199b72017-05-05 11:26:15 -0400297 if (!fOnFlushCBObjects.empty()) {
Chris Dalton6b498102019-08-01 14:14:52 -0600298 fDAG.gatherIDs(&fFlushingRenderTaskIDs);
Robert Phillips22310d62018-09-05 11:07:21 -0400299
Chris Daltonfe199b72017-05-05 11:26:15 -0400300 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
Chris Dalton6b498102019-08-01 14:14:52 -0600301 onFlushCBObject->preFlush(&onFlushProvider, fFlushingRenderTaskIDs.begin(),
Chris Daltonc4b47352019-08-23 10:10:36 -0600302 fFlushingRenderTaskIDs.count());
303 }
304 for (const auto& onFlushRenderTask : fOnFlushRenderTasks) {
305 onFlushRenderTask->makeClosed(*fContext->priv().caps());
Chris Dalton706a6ff2017-11-29 22:01:06 -0700306#ifdef SK_DEBUG
Chris Daltonc4b47352019-08-23 10:10:36 -0600307 // OnFlush callbacks are invoked during flush, and are therefore expected to handle
308 // resource allocation & usage on their own. (No deferred or lazy proxies!)
309 onFlushRenderTask->visitTargetAndSrcProxies_debugOnly(
310 [](GrSurfaceProxy* p, GrMipMapped mipMapped) {
311 SkASSERT(!p->asTextureProxy() || !p->asTextureProxy()->texPriv().isDeferred());
Brian Salomonbeb7f522019-08-30 16:19:42 -0400312 SkASSERT(!p->isLazy());
Chris Dalton4ece96d2019-08-30 11:26:39 -0600313 if (p->requiresManualMSAAResolve()) {
314 // The onFlush callback is responsible for ensuring MSAA gets resolved.
315 SkASSERT(p->asRenderTargetProxy() && !p->asRenderTargetProxy()->isMSAADirty());
316 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600317 if (GrMipMapped::kYes == mipMapped) {
318 // The onFlush callback is responsible for regenerating mips if needed.
319 SkASSERT(p->asTextureProxy() && !p->asTextureProxy()->mipMapsAreDirty());
320 }
321 });
Chris Dalton706a6ff2017-11-29 22:01:06 -0700322#endif
Chris Daltonc4b47352019-08-23 10:10:36 -0600323 onFlushRenderTask->prepare(&flushState);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400324 }
325 }
326
robertphillipsa13e2022015-11-11 12:01:09 -0800327#if 0
Brian Salomon09d994e2016-12-21 11:14:46 -0500328 // Enable this to print out verbose GrOp information
Chris Daltonc4b47352019-08-23 10:10:36 -0600329 SkDEBUGCODE(SkDebugf("onFlush renderTasks:"));
330 for (const auto& onFlushRenderTask : fOnFlushRenderTasks) {
331 SkDEBUGCODE(onFlushRenderTask->dump();)
332 }
333 SkDEBUGCODE(SkDebugf("Normal renderTasks:"));
Chris Dalton6b498102019-08-01 14:14:52 -0600334 for (int i = 0; i < fRenderTasks.count(); ++i) {
335 SkDEBUGCODE(fRenderTasks[i]->dump();)
robertphillips3dc6ae52015-10-20 09:54:32 -0700336 }
robertphillipsa13e2022015-11-11 12:01:09 -0800337#endif
338
Robert Phillipseafd48a2017-11-16 07:52:08 -0500339 int startIndex, stopIndex;
340 bool flushed = false;
341
Robert Phillipsf8e25022017-11-08 15:24:31 -0500342 {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400343 GrResourceAllocator alloc(resourceProvider SkDEBUGCODE(, fDAG.numRenderTasks()));
Chris Dalton6b498102019-08-01 14:14:52 -0600344 for (int i = 0; i < fDAG.numRenderTasks(); ++i) {
345 if (fDAG.renderTask(i)) {
346 fDAG.renderTask(i)->gatherProxyIntervals(&alloc);
Robert Phillips22310d62018-09-05 11:07:21 -0400347 }
Greg Danielf41b2bd2019-08-22 16:19:24 -0400348 alloc.markEndOfOpsTask(i);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500349 }
Robert Phillipsc73666f2019-04-24 08:49:48 -0400350 alloc.determineRecyclability();
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400351
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500352 GrResourceAllocator::AssignError error = GrResourceAllocator::AssignError::kNoError;
Chris Dalton6b498102019-08-01 14:14:52 -0600353 int numRenderTasksExecuted = 0;
Brian Salomon577aa0f2018-11-30 13:32:23 -0500354 while (alloc.assign(&startIndex, &stopIndex, &error)) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500355 if (GrResourceAllocator::AssignError::kFailedProxyInstantiation == error) {
356 for (int i = startIndex; i < stopIndex; ++i) {
Chris Dalton6b498102019-08-01 14:14:52 -0600357 GrRenderTask* renderTask = fDAG.renderTask(i);
358 if (!renderTask) {
359 continue;
Robert Phillips01a91282018-07-26 08:03:04 -0400360 }
Chris Dalton6b498102019-08-01 14:14:52 -0600361 if (!renderTask->isInstantiated()) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400362 // No need to call the renderTask's handleInternalAllocationFailure
363 // since we will already skip executing the renderTask since it is not
364 // instantiated.
Chris Dalton6b498102019-08-01 14:14:52 -0600365 continue;
Robert Phillipsbbcb7f72018-05-31 11:16:19 -0400366 }
Chris Dalton6b498102019-08-01 14:14:52 -0600367 renderTask->handleInternalAllocationFailure();
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500368 }
369 }
Robert Phillips4150eea2018-02-07 17:08:21 -0500370
Chris Dalton6b498102019-08-01 14:14:52 -0600371 if (this->executeRenderTasks(
372 startIndex, stopIndex, &flushState, &numRenderTasksExecuted)) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500373 flushed = true;
374 }
bsalomondc438982016-08-31 11:53:49 -0700375 }
Greg Danielc42b20b2017-10-04 10:34:49 -0400376 }
377
Chris Dalton91ab1552018-04-18 13:24:25 -0600378#ifdef SK_DEBUG
Chris Dalton6b498102019-08-01 14:14:52 -0600379 for (int i = 0; i < fDAG.numRenderTasks(); ++i) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400380 // If there are any remaining opsTaskss at this point, make sure they will not survive the
Chris Dalton91ab1552018-04-18 13:24:25 -0600381 // flush. Otherwise we need to call endFlush() on them.
382 // http://skbug.com/7111
Chris Dalton6b498102019-08-01 14:14:52 -0600383 SkASSERT(!fDAG.renderTask(i) || fDAG.renderTask(i)->unique());
Chris Dalton91ab1552018-04-18 13:24:25 -0600384 }
385#endif
Robert Phillips22310d62018-09-05 11:07:21 -0400386 fDAG.reset();
Robert Phillips15c91422019-05-07 16:54:48 -0400387 this->clearDDLTargets();
robertphillipsa13e2022015-11-11 12:01:09 -0800388
Robert Phillipsc994a932018-06-19 13:09:54 -0400389#ifdef SK_DEBUG
390 // In non-DDL mode this checks that all the flushed ops have been freed from the memory pool.
391 // When we move to partial flushes this assert will no longer be valid.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400392 // In DDL mode this check is somewhat superfluous since the memory for most of the ops/opsTasks
Robert Phillipsc994a932018-06-19 13:09:54 -0400393 // will be stored in the DDL's GrOpMemoryPools.
Robert Phillips9da87e02019-02-04 13:26:26 -0500394 GrOpMemoryPool* opMemoryPool = fContext->priv().opMemoryPool();
Robert Phillipsc994a932018-06-19 13:09:54 -0400395 opMemoryPool->isEmpty();
396#endif
397
Greg Danielfe159622020-04-10 17:43:51 +0000398 gpu->executeFlushInfo(proxies, numProxies, access, info, externalRequests);
robertphillipsa13e2022015-11-11 12:01:09 -0800399
Brian Salomon57d2beab2018-09-10 09:35:41 -0400400 // Give the cache a chance to purge resources that become purgeable due to flushing.
401 if (flushed) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500402 resourceCache->purgeAsNeeded();
Brian Salomon876a0172019-03-08 11:12:14 -0500403 flushed = false;
bsalomonb77a9072016-09-07 10:02:04 -0700404 }
Chris Daltonfe199b72017-05-05 11:26:15 -0400405 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
Chris Dalton6b498102019-08-01 14:14:52 -0600406 onFlushCBObject->postFlush(fTokenTracker.nextTokenToFlush(), fFlushingRenderTaskIDs.begin(),
407 fFlushingRenderTaskIDs.count());
Brian Salomon876a0172019-03-08 11:12:14 -0500408 flushed = true;
409 }
410 if (flushed) {
411 resourceCache->purgeAsNeeded();
Chris Daltonfe199b72017-05-05 11:26:15 -0400412 }
Chris Dalton6b498102019-08-01 14:14:52 -0600413 fFlushingRenderTaskIDs.reset();
joshualittb8918c42015-12-18 09:59:46 -0800414 fFlushing = false;
Greg Daniel51316782017-08-02 15:10:09 +0000415
Greg Danielfe159622020-04-10 17:43:51 +0000416 return true;
417}
418
419bool GrDrawingManager::submitToGpu(bool syncToCpu) {
420 if (fFlushing || this->wasAbandoned()) {
421 return false;
422 }
423
424 auto direct = fContext->priv().asDirectContext();
425 if (!direct) {
426 return false; // Can't submit while DDL recording
427 }
428 GrGpu* gpu = direct->priv().getGpu();
429 return gpu->submitToGpu(syncToCpu);
robertphillips3dc6ae52015-10-20 09:54:32 -0700430}
431
Chris Dalton6b498102019-08-01 14:14:52 -0600432bool GrDrawingManager::executeRenderTasks(int startIndex, int stopIndex, GrOpFlushState* flushState,
433 int* numRenderTasksExecuted) {
434 SkASSERT(startIndex <= stopIndex && stopIndex <= fDAG.numRenderTasks());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500435
Robert Phillips27483912018-04-20 12:43:18 -0400436#if GR_FLUSH_TIME_OP_SPEW
Greg Danielf41b2bd2019-08-22 16:19:24 -0400437 SkDebugf("Flushing opsTask: %d to %d out of [%d, %d]\n",
Chris Dalton6b498102019-08-01 14:14:52 -0600438 startIndex, stopIndex, 0, fDAG.numRenderTasks());
Robert Phillips27483912018-04-20 12:43:18 -0400439 for (int i = startIndex; i < stopIndex; ++i) {
Chris Dalton6b498102019-08-01 14:14:52 -0600440 if (fDAG.renderTask(i)) {
441 fDAG.renderTask(i)->dump(true);
Robert Phillips1734dd32018-08-21 13:52:09 -0400442 }
Robert Phillips27483912018-04-20 12:43:18 -0400443 }
444#endif
445
Chris Dalton6b498102019-08-01 14:14:52 -0600446 bool anyRenderTasksExecuted = false;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500447
448 for (int i = startIndex; i < stopIndex; ++i) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400449 GrRenderTask* renderTask = fDAG.renderTask(i);
450 if (!renderTask || !renderTask->isInstantiated()) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500451 continue;
452 }
453
Chris Dalton6b498102019-08-01 14:14:52 -0600454 SkASSERT(renderTask->deferredProxiesAreInstantiated());
Robert Phillips22310d62018-09-05 11:07:21 -0400455
Chris Dalton6b498102019-08-01 14:14:52 -0600456 renderTask->prepare(flushState);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500457 }
458
459 // Upload all data to the GPU
460 flushState->preExecuteDraws();
461
Greg Danield2073452018-12-07 11:20:33 -0500462 // For Vulkan, if we have too many oplists to be flushed we end up allocating a lot of resources
463 // for each command buffer associated with the oplists. If this gets too large we can cause the
464 // devices to go OOM. In practice we usually only hit this case in our tests, but to be safe we
465 // put a cap on the number of oplists we will execute before flushing to the GPU to relieve some
466 // memory pressure.
Chris Dalton6b498102019-08-01 14:14:52 -0600467 static constexpr int kMaxRenderTasksBeforeFlush = 100;
Greg Danield2073452018-12-07 11:20:33 -0500468
Chris Daltonc4b47352019-08-23 10:10:36 -0600469 // Execute the onFlush renderTasks first, if any.
470 for (sk_sp<GrRenderTask>& onFlushRenderTask : fOnFlushRenderTasks) {
471 if (!onFlushRenderTask->execute(flushState)) {
472 SkDebugf("WARNING: onFlushRenderTask failed to execute.\n");
Robert Phillipseafd48a2017-11-16 07:52:08 -0500473 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600474 SkASSERT(onFlushRenderTask->unique());
475 onFlushRenderTask = nullptr;
Chris Dalton6b498102019-08-01 14:14:52 -0600476 (*numRenderTasksExecuted)++;
477 if (*numRenderTasksExecuted >= kMaxRenderTasksBeforeFlush) {
Greg Danielfe159622020-04-10 17:43:51 +0000478 flushState->gpu()->submitToGpu(false);
Chris Dalton6b498102019-08-01 14:14:52 -0600479 *numRenderTasksExecuted = 0;
Greg Danield2073452018-12-07 11:20:33 -0500480 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500481 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600482 fOnFlushRenderTasks.reset();
Robert Phillipseafd48a2017-11-16 07:52:08 -0500483
484 // Execute the normal op lists.
485 for (int i = startIndex; i < stopIndex; ++i) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400486 GrRenderTask* renderTask = fDAG.renderTask(i);
487 if (!renderTask || !renderTask->isInstantiated()) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500488 continue;
489 }
490
Greg Daniel15ecdf92019-08-30 15:35:23 -0400491 if (renderTask->execute(flushState)) {
Chris Dalton6b498102019-08-01 14:14:52 -0600492 anyRenderTasksExecuted = true;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500493 }
Chris Dalton6b498102019-08-01 14:14:52 -0600494 (*numRenderTasksExecuted)++;
495 if (*numRenderTasksExecuted >= kMaxRenderTasksBeforeFlush) {
Greg Danielfe159622020-04-10 17:43:51 +0000496 flushState->gpu()->submitToGpu(false);
Chris Dalton6b498102019-08-01 14:14:52 -0600497 *numRenderTasksExecuted = 0;
Greg Danield2073452018-12-07 11:20:33 -0500498 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500499 }
500
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400501 SkASSERT(!flushState->opsRenderPass());
Robert Phillips40a29d72018-01-18 12:59:22 -0500502 SkASSERT(fTokenTracker.nextDrawToken() == fTokenTracker.nextTokenToFlush());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500503
Chris Dalton6b498102019-08-01 14:14:52 -0600504 // We reset the flush state before the RenderTasks so that the last resources to be freed are
505 // those that are written to in the RenderTasks. This helps to make sure the most recently used
506 // resources are the last to be purged by the resource cache.
Robert Phillipseafd48a2017-11-16 07:52:08 -0500507 flushState->reset();
508
Chris Dalton6b498102019-08-01 14:14:52 -0600509 fDAG.removeRenderTasks(startIndex, stopIndex);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500510
Chris Dalton6b498102019-08-01 14:14:52 -0600511 return anyRenderTasksExecuted;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500512}
513
Greg Daniel55f040b2020-02-13 15:38:32 +0000514GrSemaphoresSubmitted GrDrawingManager::flushSurfaces(GrSurfaceProxy* proxies[], int numProxies,
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400515 SkSurface::BackendSurfaceAccess access,
516 const GrFlushInfo& info) {
bsalomon6a2b1942016-09-08 11:28:59 -0700517 if (this->wasAbandoned()) {
Greg Daniel51316782017-08-02 15:10:09 +0000518 return GrSemaphoresSubmitted::kNo;
bsalomon6a2b1942016-09-08 11:28:59 -0700519 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400520 SkDEBUGCODE(this->validate());
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400521 SkASSERT(numProxies >= 0);
522 SkASSERT(!numProxies || proxies);
bsalomon6a2b1942016-09-08 11:28:59 -0700523
Robert Phillips6a6de562019-02-15 15:19:15 -0500524 auto direct = fContext->priv().asDirectContext();
525 if (!direct) {
526 return GrSemaphoresSubmitted::kNo; // Can't flush while DDL recording
527 }
528
529 GrGpu* gpu = direct->priv().getGpu();
Robert Phillips874b5352018-03-16 08:48:24 -0400530 if (!gpu) {
531 return GrSemaphoresSubmitted::kNo; // Can't flush while DDL recording
532 }
533
Robert Phillipsacc10fa2019-04-01 09:50:20 -0400534 // TODO: It is important to upgrade the drawingmanager to just flushing the
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400535 // portion of the DAG required by 'proxies' in order to restore some of the
Robert Phillipsacc10fa2019-04-01 09:50:20 -0400536 // semantics of this method.
Greg Danielfe159622020-04-10 17:43:51 +0000537 bool didFlush = this->flush(proxies, numProxies, access, info,
538 GrPrepareForExternalIORequests());
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400539 for (int i = 0; i < numProxies; ++i) {
Chris Dalton4ece96d2019-08-30 11:26:39 -0600540 GrSurfaceProxy* proxy = proxies[i];
541 if (!proxy->isInstantiated()) {
Greg Danielfe159622020-04-10 17:43:51 +0000542 continue;
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400543 }
Chris Dalton4ece96d2019-08-30 11:26:39 -0600544 // In the flushSurfaces case, we need to resolve MSAA immediately after flush. This is
545 // because the client will call through to this method when drawing into a target created by
546 // wrapBackendTextureAsRenderTarget, and will expect the original texture to be fully
547 // resolved upon return.
548 if (proxy->requiresManualMSAAResolve()) {
549 auto* rtProxy = proxy->asRenderTargetProxy();
550 SkASSERT(rtProxy);
551 if (rtProxy->isMSAADirty()) {
552 SkASSERT(rtProxy->peekRenderTarget());
Chris Dalton16a33c62019-09-24 22:19:17 -0600553 gpu->resolveRenderTarget(rtProxy->peekRenderTarget(), rtProxy->msaaDirtyRect(),
Greg Daniel242536f2020-02-13 14:12:46 -0500554 GrGpu::ForExternalIO::kYes);
Chris Dalton4ece96d2019-08-30 11:26:39 -0600555 rtProxy->markMSAAResolved();
556 }
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400557 }
Chris Dalton3d770272019-08-14 09:24:37 -0600558 // If, after a flush, any of the proxies of interest have dirty mipmaps, regenerate them in
559 // case their backend textures are being stolen.
560 // (This special case is exercised by the ReimportImageTextureWithMipLevels test.)
561 // 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 -0600562 if (auto* textureProxy = proxy->asTextureProxy()) {
Chris Dalton3d770272019-08-14 09:24:37 -0600563 if (textureProxy->mipMapsAreDirty()) {
Chris Dalton4ece96d2019-08-30 11:26:39 -0600564 SkASSERT(textureProxy->peekTexture());
Chris Dalton3d770272019-08-14 09:24:37 -0600565 gpu->regenerateMipMapLevels(textureProxy->peekTexture());
566 textureProxy->markMipMapsClean();
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400567 }
Brian Salomon930f9392018-06-20 16:25:26 -0400568 }
bsalomon6a2b1942016-09-08 11:28:59 -0700569 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400570
571 SkDEBUGCODE(this->validate());
Greg Danielfe159622020-04-10 17:43:51 +0000572
573 bool submitted = false;
574 if (didFlush) {
575 submitted = this->submitToGpu(SkToBool(info.fFlags & kSyncCpu_GrFlushFlag));
576 }
577
578 if (!submitted || (!direct->priv().caps()->semaphoreSupport() && info.fNumSemaphores)) {
579 return GrSemaphoresSubmitted::kNo;
580 }
581 return GrSemaphoresSubmitted::kYes;
bsalomon6a2b1942016-09-08 11:28:59 -0700582}
583
Chris Daltonfe199b72017-05-05 11:26:15 -0400584void GrDrawingManager::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
585 fOnFlushCBObjects.push_back(onFlushCBObject);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400586}
587
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500588#if GR_TEST_UTILS
589void GrDrawingManager::testingOnly_removeOnFlushCallbackObject(GrOnFlushCallbackObject* cb) {
590 int n = std::find(fOnFlushCBObjects.begin(), fOnFlushCBObjects.end(), cb) -
591 fOnFlushCBObjects.begin();
592 SkASSERT(n < fOnFlushCBObjects.count());
593 fOnFlushCBObjects.removeShuffle(n);
594}
595#endif
596
Chris Dalton6b498102019-08-01 14:14:52 -0600597void GrDrawingManager::moveRenderTasksToDDL(SkDeferredDisplayList* ddl) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400598 SkDEBUGCODE(this->validate());
599
Chris Dalton6b498102019-08-01 14:14:52 -0600600 // no renderTask should receive a new command after this
Robert Phillips9da87e02019-02-04 13:26:26 -0500601 fDAG.closeAll(fContext->priv().caps());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400602 fActiveOpsTask = nullptr;
Robert Phillips7a137052018-02-01 11:23:12 -0500603
Chris Dalton6b498102019-08-01 14:14:52 -0600604 fDAG.swap(&ddl->fRenderTasks);
Robert Phillips19f466d2020-02-26 10:27:07 -0500605 SkASSERT(!fDAG.numRenderTasks());
Robert Phillips867ce8f2018-06-21 10:28:36 -0400606
Robert Phillips19f466d2020-02-26 10:27:07 -0500607 for (auto& renderTask : ddl->fRenderTasks) {
Robert Phillips29f38542019-10-16 09:20:25 -0400608 renderTask->prePrepare(fContext);
Robert Phillips7327c9d2019-10-08 16:32:56 -0400609 }
610
Michael Ludwig2c316bd2019-12-19 14:50:44 -0500611 ddl->fArenas = std::move(fContext->priv().detachArenas());
Robert Phillips61fc7992019-10-22 11:58:17 -0400612
Robert Phillipsf6a0b452020-02-18 14:26:46 -0500613 fContext->priv().detachProgramData(&ddl->fProgramData);
Robert Phillips576b6a12019-12-06 13:05:49 -0500614
Robert Phillips774168e2018-05-31 12:43:27 -0400615 if (fPathRendererChain) {
616 if (auto ccpr = fPathRendererChain->getCoverageCountingPathRenderer()) {
617 ddl->fPendingPaths = ccpr->detachPendingPaths();
618 }
619 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400620
621 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500622}
623
Chris Dalton6b498102019-08-01 14:14:52 -0600624void GrDrawingManager::copyRenderTasksFromDDL(const SkDeferredDisplayList* ddl,
Robert Phillips933484f2019-11-26 09:38:55 -0500625 GrRenderTargetProxy* newDest) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400626 SkDEBUGCODE(this->validate());
627
Greg Danielf41b2bd2019-08-22 16:19:24 -0400628 if (fActiveOpsTask) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400629 // This is a temporary fix for the partial-MDB world. In that world we're not
Greg Danielf41b2bd2019-08-22 16:19:24 -0400630 // reordering so ops that (in the single opsTask world) would've just glommed onto the
631 // end of the single opsTask but referred to a far earlier RT need to appear in their
632 // own opsTask.
633 fActiveOpsTask->makeClosed(*fContext->priv().caps());
634 fActiveOpsTask = nullptr;
Robert Phillips38d64b02018-09-04 13:23:26 -0400635 }
636
Robert Phillips15c91422019-05-07 16:54:48 -0400637 this->addDDLTarget(newDest);
638
Robert Phillips62000362018-02-01 09:10:04 -0500639 // Here we jam the proxy that backs the current replay SkSurface into the LazyProxyData.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400640 // The lazy proxy that references it (in the copied opsTasks) will steal its GrTexture.
Robert Phillips62000362018-02-01 09:10:04 -0500641 ddl->fLazyProxyData->fReplayDest = newDest;
Robert Phillips774168e2018-05-31 12:43:27 -0400642
643 if (ddl->fPendingPaths.size()) {
644 GrCoverageCountingPathRenderer* ccpr = this->getCoverageCountingPathRenderer();
645
646 ccpr->mergePendingPaths(ddl->fPendingPaths);
647 }
Robert Phillips22310d62018-09-05 11:07:21 -0400648
Chris Dalton6b498102019-08-01 14:14:52 -0600649 fDAG.add(ddl->fRenderTasks);
Robert Phillips38d64b02018-09-04 13:23:26 -0400650
651 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500652}
653
Robert Phillips38d64b02018-09-04 13:23:26 -0400654#ifdef SK_DEBUG
655void GrDrawingManager::validate() const {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400656 if (fDAG.sortingRenderTasks() && fReduceOpsTaskSplitting) {
657 SkASSERT(!fActiveOpsTask);
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400658 } else {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400659 if (fActiveOpsTask) {
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400660 SkASSERT(!fDAG.empty());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400661 SkASSERT(!fActiveOpsTask->isClosed());
662 SkASSERT(fActiveOpsTask == fDAG.back());
Robert Phillips38d64b02018-09-04 13:23:26 -0400663 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400664
Chris Dalton6b498102019-08-01 14:14:52 -0600665 for (int i = 0; i < fDAG.numRenderTasks(); ++i) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400666 if (fActiveOpsTask != fDAG.renderTask(i)) {
Chris Daltone2a903e2019-09-18 13:41:50 -0600667 // The resolveTask associated with the activeTask remains open for as long as the
668 // activeTask does.
669 bool isActiveResolveTask =
670 fActiveOpsTask && fActiveOpsTask->fTextureResolveTask == fDAG.renderTask(i);
671 SkASSERT(isActiveResolveTask || fDAG.renderTask(i)->isClosed());
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400672 }
673 }
674
675 if (!fDAG.empty() && !fDAG.back()->isClosed()) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400676 SkASSERT(fActiveOpsTask == fDAG.back());
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400677 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400678 }
679}
680#endif
681
Greg Danielbbfec9d2019-08-20 10:56:51 -0400682void GrDrawingManager::closeRenderTasksForNewRenderTask(GrSurfaceProxy* target) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400683 if (target && fDAG.sortingRenderTasks() && fReduceOpsTaskSplitting) {
Chris Dalton5fe99772019-08-06 11:57:39 -0600684 // In this case we need to close all the renderTasks that rely on the current contents of
685 // 'target'. That is bc we're going to update the content of the proxy so they need to be
686 // split in case they use both the old and new content. (This is a bit of an overkill: they
687 // really only need to be split if they ever reference proxy's contents again but that is
688 // hard to predict/handle).
689 if (GrRenderTask* lastRenderTask = target->getLastRenderTask()) {
Chris Dalton6b498102019-08-01 14:14:52 -0600690 lastRenderTask->closeThoseWhoDependOnMe(*fContext->priv().caps());
Robert Phillips46acf9d2018-10-09 09:31:40 -0400691 }
Greg Danielf41b2bd2019-08-22 16:19:24 -0400692 } else if (fActiveOpsTask) {
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400693 // This is a temporary fix for the partial-MDB world. In that world we're not
Greg Danielf41b2bd2019-08-22 16:19:24 -0400694 // reordering so ops that (in the single opsTask world) would've just glommed onto the
695 // end of the single opsTask but referred to a far earlier RT need to appear in their
696 // own opsTask.
697 fActiveOpsTask->makeClosed(*fContext->priv().caps());
698 fActiveOpsTask = nullptr;
robertphillips3dc6ae52015-10-20 09:54:32 -0700699 }
Chris Dalton5fe99772019-08-06 11:57:39 -0600700}
701
Greg Daniel16f5c652019-10-29 11:26:01 -0400702sk_sp<GrOpsTask> GrDrawingManager::newOpsTask(GrSurfaceProxyView surfaceView,
703 bool managedOpsTask) {
Chris Dalton5fe99772019-08-06 11:57:39 -0600704 SkDEBUGCODE(this->validate());
705 SkASSERT(fContext);
706
Greg Daniel16f5c652019-10-29 11:26:01 -0400707 GrSurfaceProxy* proxy = surfaceView.proxy();
708 this->closeRenderTasksForNewRenderTask(proxy);
robertphillips3dc6ae52015-10-20 09:54:32 -0700709
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500710 sk_sp<GrOpsTask> opsTask(new GrOpsTask(fContext->priv().arenas(),
Michael Ludwigd0840ec2019-12-12 09:48:38 -0500711 std::move(surfaceView),
712 fContext->priv().auditTrail()));
Greg Daniel16f5c652019-10-29 11:26:01 -0400713 SkASSERT(proxy->getLastRenderTask() == opsTask.get());
robertphillips3dc6ae52015-10-20 09:54:32 -0700714
Greg Danielf41b2bd2019-08-22 16:19:24 -0400715 if (managedOpsTask) {
716 fDAG.add(opsTask);
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400717
Greg Danielf41b2bd2019-08-22 16:19:24 -0400718 if (!fDAG.sortingRenderTasks() || !fReduceOpsTaskSplitting) {
719 fActiveOpsTask = opsTask.get();
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400720 }
Robert Phillips941d1442017-06-14 16:37:02 -0400721 }
robertphillips3dc6ae52015-10-20 09:54:32 -0700722
Robert Phillips38d64b02018-09-04 13:23:26 -0400723 SkDEBUGCODE(this->validate());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400724 return opsTask;
robertphillips3dc6ae52015-10-20 09:54:32 -0700725}
726
Chris Daltone2a903e2019-09-18 13:41:50 -0600727GrTextureResolveRenderTask* GrDrawingManager::newTextureResolveRenderTask(const GrCaps& caps) {
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600728 // 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 -0600729 // in sorting and opsTask reduction mode) the render tasks that depend on any proxy's current
Greg Danielf41b2bd2019-08-22 16:19:24 -0400730 // state. This is because those opsTasks can still receive new ops and because if they refer to
Chris Dalton4ece96d2019-08-30 11:26:39 -0600731 // the mipmapped version of 'proxy', they will then come to depend on the render task being
732 // created here.
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600733 //
Greg Danielf41b2bd2019-08-22 16:19:24 -0400734 // Add the new textureResolveTask before the fActiveOpsTask (if not in
735 // sorting/opsTask-splitting-reduction mode) because it will depend upon this resolve task.
Chris Dalton3d770272019-08-14 09:24:37 -0600736 // NOTE: Putting it here will also reduce the amount of work required by the topological sort.
Chris Daltone2a903e2019-09-18 13:41:50 -0600737 return static_cast<GrTextureResolveRenderTask*>(fDAG.addBeforeLast(
738 sk_make_sp<GrTextureResolveRenderTask>()));
Chris Dalton3d770272019-08-14 09:24:37 -0600739}
740
Greg Danielc30f1a92019-09-06 15:28:58 -0400741void GrDrawingManager::newWaitRenderTask(sk_sp<GrSurfaceProxy> proxy,
Greg Daniel301015c2019-11-18 14:06:46 -0500742 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> semaphores,
Greg Danielc30f1a92019-09-06 15:28:58 -0400743 int numSemaphores) {
744 SkDEBUGCODE(this->validate());
745 SkASSERT(fContext);
746
747 const GrCaps& caps = *fContext->priv().caps();
748
Greg Daniel16f5c652019-10-29 11:26:01 -0400749 sk_sp<GrWaitRenderTask> waitTask = sk_make_sp<GrWaitRenderTask>(GrSurfaceProxyView(proxy),
750 std::move(semaphores),
Greg Danielc30f1a92019-09-06 15:28:58 -0400751 numSemaphores);
752 if (fReduceOpsTaskSplitting) {
753 GrRenderTask* lastTask = proxy->getLastRenderTask();
754 if (lastTask && !lastTask->isClosed()) {
755 // We directly make the currently open renderTask depend on waitTask instead of using
756 // the proxy version of addDependency. The waitTask will never need to trigger any
757 // resolves or mip map generation which is the main advantage of going through the proxy
758 // version. Additionally we would've had to temporarily set the wait task as the
759 // lastRenderTask on the proxy, add the dependency, and then reset the lastRenderTask to
760 // lastTask. Additionally we add all dependencies of lastTask to waitTask so that the
761 // waitTask doesn't get reordered before them and unnecessarily block those tasks.
762 // Note: Any previous Ops already in lastTask will get blocked by the wait semaphore
763 // even though they don't need to be for correctness.
764
765 // Make sure we add the dependencies of lastTask to waitTask first or else we'll get a
766 // circular self dependency of waitTask on waitTask.
767 waitTask->addDependenciesFromOtherTask(lastTask);
768 lastTask->addDependency(waitTask.get());
769 } else {
770 // If there is a last task we set the waitTask to depend on it so that it doesn't get
771 // reordered in front of the lastTask causing the lastTask to be blocked by the
772 // semaphore. Again we directly just go through adding the dependency to the task and
773 // not the proxy since we don't need to worry about resolving anything.
774 if (lastTask) {
775 waitTask->addDependency(lastTask);
776 }
777 proxy->setLastRenderTask(waitTask.get());
778 }
779 fDAG.add(waitTask);
780 } else {
Greg Daniel16f5c652019-10-29 11:26:01 -0400781 if (fActiveOpsTask && (fActiveOpsTask->fTargetView.proxy() == proxy.get())) {
Greg Danielc30f1a92019-09-06 15:28:58 -0400782 SkASSERT(proxy->getLastRenderTask() == fActiveOpsTask);
783 fDAG.addBeforeLast(waitTask);
784 // In this case we keep the current renderTask open but just insert the new waitTask
785 // before it in the list. The waitTask will never need to trigger any resolves or mip
786 // map generation which is the main advantage of going through the proxy version.
787 // Additionally we would've had to temporarily set the wait task as the lastRenderTask
788 // on the proxy, add the dependency, and then reset the lastRenderTask to
789 // fActiveOpsTask. Additionally we make the waitTask depend on all of fActiveOpsTask
790 // dependencies so that we don't unnecessarily reorder the waitTask before them.
791 // Note: Any previous Ops already in fActiveOpsTask will get blocked by the wait
792 // semaphore even though they don't need to be for correctness.
793
794 // Make sure we add the dependencies of fActiveOpsTask to waitTask first or else we'll
795 // get a circular self dependency of waitTask on waitTask.
796 waitTask->addDependenciesFromOtherTask(fActiveOpsTask);
797 fActiveOpsTask->addDependency(waitTask.get());
798 } else {
799 // In this case we just close the previous RenderTask and start and append the waitTask
800 // to the DAG. Since it is the last task now we call setLastRenderTask on the proxy. If
801 // there is a lastTask on the proxy we make waitTask depend on that task. This
802 // dependency isn't strictly needed but it does keep the DAG from reordering the
803 // waitTask earlier and blocking more tasks.
804 if (GrRenderTask* lastTask = proxy->getLastRenderTask()) {
805 waitTask->addDependency(lastTask);
806 }
807 proxy->setLastRenderTask(waitTask.get());
808 this->closeRenderTasksForNewRenderTask(proxy.get());
809 fDAG.add(waitTask);
810 }
811 }
812 waitTask->makeClosed(caps);
813
814 SkDEBUGCODE(this->validate());
815}
816
Greg Danielbbfec9d2019-08-20 10:56:51 -0400817void GrDrawingManager::newTransferFromRenderTask(sk_sp<GrSurfaceProxy> srcProxy,
818 const SkIRect& srcRect,
819 GrColorType surfaceColorType,
820 GrColorType dstColorType,
821 sk_sp<GrGpuBuffer> dstBuffer,
822 size_t dstOffset) {
823 SkDEBUGCODE(this->validate());
824 SkASSERT(fContext);
825 // This copies from srcProxy to dstBuffer so it doesn't have a real target.
826 this->closeRenderTasksForNewRenderTask(nullptr);
827
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600828 GrRenderTask* task = fDAG.add(sk_make_sp<GrTransferFromRenderTask>(
829 srcProxy, srcRect, surfaceColorType, dstColorType, std::move(dstBuffer), dstOffset));
Greg Danielbbfec9d2019-08-20 10:56:51 -0400830
831 const GrCaps& caps = *fContext->priv().caps();
832
833 // We always say GrMipMapped::kNo here since we are always just copying from the base layer. We
834 // don't need to make sure the whole mip map chain is valid.
835 task->addDependency(srcProxy.get(), GrMipMapped::kNo, GrTextureResolveManager(this), caps);
836 task->makeClosed(caps);
837
Greg Danielbbfec9d2019-08-20 10:56:51 -0400838 // We have closed the previous active oplist but since a new oplist isn't being added there
839 // shouldn't be an active one.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400840 SkASSERT(!fActiveOpsTask);
Greg Danielbbfec9d2019-08-20 10:56:51 -0400841 SkDEBUGCODE(this->validate());
842}
843
Greg Daniel16f5c652019-10-29 11:26:01 -0400844bool GrDrawingManager::newCopyRenderTask(GrSurfaceProxyView srcView,
Greg Daniele227fe42019-08-21 13:52:24 -0400845 const SkIRect& srcRect,
Greg Daniel16f5c652019-10-29 11:26:01 -0400846 GrSurfaceProxyView dstView,
Greg Daniele227fe42019-08-21 13:52:24 -0400847 const SkIPoint& dstPoint) {
848 SkDEBUGCODE(this->validate());
849 SkASSERT(fContext);
Greg Daniele227fe42019-08-21 13:52:24 -0400850
Greg Daniel16f5c652019-10-29 11:26:01 -0400851 this->closeRenderTasksForNewRenderTask(dstView.proxy());
Brian Salomone4bce012019-09-20 15:34:23 -0400852 const GrCaps& caps = *fContext->priv().caps();
853
Greg Daniel16f5c652019-10-29 11:26:01 -0400854 GrSurfaceProxy* srcProxy = srcView.proxy();
855
Brian Salomone4bce012019-09-20 15:34:23 -0400856 GrRenderTask* task =
Greg Daniel16f5c652019-10-29 11:26:01 -0400857 fDAG.add(GrCopyRenderTask::Make(std::move(srcView), srcRect, std::move(dstView),
858 dstPoint, &caps));
Greg Daniele227fe42019-08-21 13:52:24 -0400859 if (!task) {
860 return false;
861 }
862
Greg Daniele227fe42019-08-21 13:52:24 -0400863 // We always say GrMipMapped::kNo here since we are always just copying from the base layer to
864 // another base layer. We don't need to make sure the whole mip map chain is valid.
Greg Daniel16f5c652019-10-29 11:26:01 -0400865 task->addDependency(srcProxy, GrMipMapped::kNo, GrTextureResolveManager(this), caps);
Greg Daniele227fe42019-08-21 13:52:24 -0400866 task->makeClosed(caps);
867
Greg Daniele227fe42019-08-21 13:52:24 -0400868 // We have closed the previous active oplist but since a new oplist isn't being added there
869 // shouldn't be an active one.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400870 SkASSERT(!fActiveOpsTask);
Greg Daniele227fe42019-08-21 13:52:24 -0400871 SkDEBUGCODE(this->validate());
872 return true;
873}
874
Herb Derby26cbe512018-05-24 14:39:01 -0400875GrTextContext* GrDrawingManager::getTextContext() {
876 if (!fTextContext) {
877 fTextContext = GrTextContext::Make(fOptionsForTextContext);
brianosman86e76262016-08-11 12:17:31 -0700878 }
879
Herb Derby26cbe512018-05-24 14:39:01 -0400880 return fTextContext.get();
brianosman86e76262016-08-11 12:17:31 -0700881}
882
robertphillips68737822015-10-29 12:12:21 -0700883/*
884 * This method finds a path renderer that can draw the specified path on
885 * the provided target.
886 * Due to its expense, the software path renderer has split out so it can
887 * can be individually allowed/disallowed via the "allowSW" boolean.
888 */
889GrPathRenderer* GrDrawingManager::getPathRenderer(const GrPathRenderer::CanDrawPathArgs& args,
890 bool allowSW,
891 GrPathRendererChain::DrawType drawType,
892 GrPathRenderer::StencilSupport* stencilSupport) {
893
894 if (!fPathRendererChain) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400895 fPathRendererChain.reset(new GrPathRendererChain(fContext, fOptionsForPathRendererChain));
robertphillips68737822015-10-29 12:12:21 -0700896 }
897
898 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(args, drawType, stencilSupport);
899 if (!pr && allowSW) {
Brian Salomone7df0bb2018-05-07 14:44:57 -0400900 auto swPR = this->getSoftwarePathRenderer();
901 if (GrPathRenderer::CanDrawPath::kNo != swPR->canDrawPath(args)) {
902 pr = swPR;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500903 }
robertphillips68737822015-10-29 12:12:21 -0700904 }
905
Robert Phillipsd81379d2020-04-21 10:39:02 -0400906#if GR_PATH_RENDERER_SPEW
907 if (pr) {
908 SkDebugf("getPathRenderer: %s\n", pr->name());
909 }
910#endif
911
robertphillips68737822015-10-29 12:12:21 -0700912 return pr;
913}
914
Brian Salomone7df0bb2018-05-07 14:44:57 -0400915GrPathRenderer* GrDrawingManager::getSoftwarePathRenderer() {
916 if (!fSoftwarePathRenderer) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400917 fSoftwarePathRenderer.reset(
Robert Phillips9da87e02019-02-04 13:26:26 -0500918 new GrSoftwarePathRenderer(fContext->priv().proxyProvider(),
Ben Wagner9ec70c62018-07-12 13:30:47 -0400919 fOptionsForPathRendererChain.fAllowPathMaskCaching));
Brian Salomone7df0bb2018-05-07 14:44:57 -0400920 }
Ben Wagner9ec70c62018-07-12 13:30:47 -0400921 return fSoftwarePathRenderer.get();
Brian Salomone7df0bb2018-05-07 14:44:57 -0400922}
923
Chris Daltonfddb6c02017-11-04 15:22:22 -0600924GrCoverageCountingPathRenderer* GrDrawingManager::getCoverageCountingPathRenderer() {
925 if (!fPathRendererChain) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400926 fPathRendererChain.reset(new GrPathRendererChain(fContext, fOptionsForPathRendererChain));
Chris Daltonfddb6c02017-11-04 15:22:22 -0600927 }
928 return fPathRendererChain->getCoverageCountingPathRenderer();
929}
930
Brian Salomon653f42f2018-07-10 10:07:31 -0400931void GrDrawingManager::flushIfNecessary() {
Robert Phillips6a6de562019-02-15 15:19:15 -0500932 auto direct = fContext->priv().asDirectContext();
933 if (!direct) {
934 return;
935 }
936
937 auto resourceCache = direct->priv().getResourceCache();
Brian Salomon653f42f2018-07-10 10:07:31 -0400938 if (resourceCache && resourceCache->requestsFlush()) {
Greg Danielfe159622020-04-10 17:43:51 +0000939 if (this->flush(nullptr, 0, SkSurface::BackendSurfaceAccess::kNoAccess, GrFlushInfo(),
940 GrPrepareForExternalIORequests())) {
941 this->submitToGpu(false);
942 }
Brian Salomon57d2beab2018-09-10 09:35:41 -0400943 resourceCache->purgeAsNeeded();
Brian Salomon653f42f2018-07-10 10:07:31 -0400944 }
945}
946