blob: 42db1eb1f970a8e51259d5372b996452f72e67b0 [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"
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"
28#include "src/gpu/GrSurfaceProxyPriv.h"
29#include "src/gpu/GrTextureContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/gpu/GrTexturePriv.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040031#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050032#include "src/gpu/GrTextureProxyPriv.h"
Chris Dalton3d770272019-08-14 09:24:37 -060033#include "src/gpu/GrTextureResolveRenderTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050034#include "src/gpu/GrTracing.h"
Greg Danielbbfec9d2019-08-20 10:56:51 -040035#include "src/gpu/GrTransferFromRenderTask.h"
Greg Danielc30f1a92019-09-06 15:28:58 -040036#include "src/gpu/GrWaitRenderTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050037#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
38#include "src/gpu/text/GrTextContext.h"
39#include "src/image/SkSurface_Gpu.h"
robertphillips498d7ac2015-10-30 10:11:30 -070040
Chris Dalton6b498102019-08-01 14:14:52 -060041GrDrawingManager::RenderTaskDAG::RenderTaskDAG(bool sortRenderTasks)
42 : fSortRenderTasks(sortRenderTasks) {}
Robert Phillipsa3f70262018-02-08 10:59:38 -050043
Chris Dalton6b498102019-08-01 14:14:52 -060044GrDrawingManager::RenderTaskDAG::~RenderTaskDAG() {}
Robert Phillips22310d62018-09-05 11:07:21 -040045
Chris Dalton6b498102019-08-01 14:14:52 -060046void GrDrawingManager::RenderTaskDAG::gatherIDs(SkSTArray<8, uint32_t, true>* idArray) const {
47 idArray->reset(fRenderTasks.count());
48 for (int i = 0; i < fRenderTasks.count(); ++i) {
49 if (fRenderTasks[i]) {
50 (*idArray)[i] = fRenderTasks[i]->uniqueID();
Robert Phillips22310d62018-09-05 11:07:21 -040051 }
52 }
53}
54
Chris Dalton6b498102019-08-01 14:14:52 -060055void GrDrawingManager::RenderTaskDAG::reset() {
56 fRenderTasks.reset();
Robert Phillips22310d62018-09-05 11:07:21 -040057}
58
Chris Dalton6b498102019-08-01 14:14:52 -060059void GrDrawingManager::RenderTaskDAG::removeRenderTask(int index) {
60 if (!fRenderTasks[index]->unique()) {
Robert Phillips22310d62018-09-05 11:07:21 -040061 // TODO: Eventually this should be guaranteed unique: http://skbug.com/7111
Chris Dalton6b498102019-08-01 14:14:52 -060062 fRenderTasks[index]->endFlush();
Robert Phillips22310d62018-09-05 11:07:21 -040063 }
64
Chris Dalton6b498102019-08-01 14:14:52 -060065 fRenderTasks[index] = nullptr;
Robert Phillips22310d62018-09-05 11:07:21 -040066}
67
Chris Dalton6b498102019-08-01 14:14:52 -060068void GrDrawingManager::RenderTaskDAG::removeRenderTasks(int startIndex, int stopIndex) {
Robert Phillips22310d62018-09-05 11:07:21 -040069 for (int i = startIndex; i < stopIndex; ++i) {
Chris Dalton6b498102019-08-01 14:14:52 -060070 if (!fRenderTasks[i]) {
Robert Phillips22310d62018-09-05 11:07:21 -040071 continue;
72 }
Chris Dalton6b498102019-08-01 14:14:52 -060073 this->removeRenderTask(i);
Robert Phillips22310d62018-09-05 11:07:21 -040074 }
75}
76
Chris Dalton6b498102019-08-01 14:14:52 -060077bool GrDrawingManager::RenderTaskDAG::isUsed(GrSurfaceProxy* proxy) const {
78 for (int i = 0; i < fRenderTasks.count(); ++i) {
79 if (fRenderTasks[i] && fRenderTasks[i]->isUsed(proxy)) {
Robert Phillips9313aa72019-04-09 18:41:27 -040080 return true;
81 }
82 }
83
84 return false;
85}
86
Chris Dalton3d770272019-08-14 09:24:37 -060087GrRenderTask* GrDrawingManager::RenderTaskDAG::add(sk_sp<GrRenderTask> renderTask) {
Chris Dalton6aeb8e82019-08-27 11:52:19 -060088 if (renderTask) {
89 return fRenderTasks.emplace_back(std::move(renderTask)).get();
90 }
91 return nullptr;
Chris Dalton3d770272019-08-14 09:24:37 -060092}
93
94GrRenderTask* GrDrawingManager::RenderTaskDAG::addBeforeLast(sk_sp<GrRenderTask> renderTask) {
95 SkASSERT(!fRenderTasks.empty());
Chris Dalton6aeb8e82019-08-27 11:52:19 -060096 if (renderTask) {
97 // Release 'fRenderTasks.back()' and grab the raw pointer, in case the SkTArray grows
98 // and reallocates during emplace_back.
99 fRenderTasks.emplace_back(fRenderTasks.back().release());
100 return (fRenderTasks[fRenderTasks.count() - 2] = std::move(renderTask)).get();
101 }
102 return nullptr;
Robert Phillips22310d62018-09-05 11:07:21 -0400103}
104
Greg Danielf41b2bd2019-08-22 16:19:24 -0400105void GrDrawingManager::RenderTaskDAG::add(const SkTArray<sk_sp<GrRenderTask>>& renderTasks) {
106 fRenderTasks.push_back_n(renderTasks.count(), renderTasks.begin());
Robert Phillips22310d62018-09-05 11:07:21 -0400107}
108
Greg Danielf41b2bd2019-08-22 16:19:24 -0400109void GrDrawingManager::RenderTaskDAG::swap(SkTArray<sk_sp<GrRenderTask>>* renderTasks) {
110 SkASSERT(renderTasks->empty());
111 renderTasks->swap(fRenderTasks);
Robert Phillips22310d62018-09-05 11:07:21 -0400112}
113
Chris Dalton6b498102019-08-01 14:14:52 -0600114void GrDrawingManager::RenderTaskDAG::prepForFlush() {
115 if (fSortRenderTasks) {
116 SkDEBUGCODE(bool result =) SkTTopoSort<GrRenderTask, GrRenderTask::TopoSortTraits>(
117 &fRenderTasks);
Robert Phillips22310d62018-09-05 11:07:21 -0400118 SkASSERT(result);
119 }
120
121#ifdef SK_DEBUG
Greg Danielf41b2bd2019-08-22 16:19:24 -0400122 // This block checks for any unnecessary splits in the opsTasks. If two sequential opsTasks
123 // share the same backing GrSurfaceProxy it means the opsTask was artificially split.
Chris Dalton6b498102019-08-01 14:14:52 -0600124 if (fRenderTasks.count()) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400125 GrOpsTask* prevOpsTask = fRenderTasks[0]->asOpsTask();
Chris Dalton6b498102019-08-01 14:14:52 -0600126 for (int i = 1; i < fRenderTasks.count(); ++i) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400127 GrOpsTask* curOpsTask = fRenderTasks[i]->asOpsTask();
Robert Phillips22310d62018-09-05 11:07:21 -0400128
Greg Danielf41b2bd2019-08-22 16:19:24 -0400129 if (prevOpsTask && curOpsTask) {
130 SkASSERT(prevOpsTask->fTarget.get() != curOpsTask->fTarget.get());
Robert Phillips22310d62018-09-05 11:07:21 -0400131 }
132
Greg Danielf41b2bd2019-08-22 16:19:24 -0400133 prevOpsTask = curOpsTask;
Robert Phillips22310d62018-09-05 11:07:21 -0400134 }
135 }
136#endif
137}
138
Chris Dalton6b498102019-08-01 14:14:52 -0600139void GrDrawingManager::RenderTaskDAG::closeAll(const GrCaps* caps) {
140 for (int i = 0; i < fRenderTasks.count(); ++i) {
141 if (fRenderTasks[i]) {
142 fRenderTasks[i]->makeClosed(*caps);
Robert Phillips22310d62018-09-05 11:07:21 -0400143 }
144 }
145}
146
Chris Dalton6b498102019-08-01 14:14:52 -0600147void GrDrawingManager::RenderTaskDAG::cleanup(const GrCaps* caps) {
148 for (int i = 0; i < fRenderTasks.count(); ++i) {
149 if (!fRenderTasks[i]) {
Robert Phillips22310d62018-09-05 11:07:21 -0400150 continue;
151 }
152
Chris Dalton6b498102019-08-01 14:14:52 -0600153 // no renderTask should receive a dependency
154 fRenderTasks[i]->makeClosed(*caps);
robertphillips0dfa62c2015-11-16 06:23:31 -0800155
Greg Danielf41b2bd2019-08-22 16:19:24 -0400156 // We shouldn't need to do this, but it turns out some clients still hold onto opsTasks
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400157 // after a cleanup.
158 // MDB TODO: is this still true?
Chris Dalton6b498102019-08-01 14:14:52 -0600159 if (!fRenderTasks[i]->unique()) {
Chris Daltona84cacf2017-10-04 10:30:29 -0600160 // TODO: Eventually this should be guaranteed unique.
161 // https://bugs.chromium.org/p/skia/issues/detail?id=7111
Chris Dalton6b498102019-08-01 14:14:52 -0600162 fRenderTasks[i]->endFlush();
Chris Daltona84cacf2017-10-04 10:30:29 -0600163 }
robertphillips3dc6ae52015-10-20 09:54:32 -0700164 }
165
Chris Dalton6b498102019-08-01 14:14:52 -0600166 fRenderTasks.reset();
Robert Phillips22310d62018-09-05 11:07:21 -0400167}
168
169///////////////////////////////////////////////////////////////////////////////////////////////////
Robert Phillips69893702019-02-22 11:16:30 -0500170GrDrawingManager::GrDrawingManager(GrRecordingContext* context,
Robert Phillips22310d62018-09-05 11:07:21 -0400171 const GrPathRendererChain::Options& optionsForPathRendererChain,
172 const GrTextContext::Options& optionsForTextContext,
Chris Dalton6b498102019-08-01 14:14:52 -0600173 bool sortRenderTasks,
Greg Danielf41b2bd2019-08-22 16:19:24 -0400174 bool reduceOpsTaskSplitting)
Robert Phillips22310d62018-09-05 11:07:21 -0400175 : fContext(context)
176 , fOptionsForPathRendererChain(optionsForPathRendererChain)
177 , fOptionsForTextContext(optionsForTextContext)
Chris Dalton6b498102019-08-01 14:14:52 -0600178 , fDAG(sortRenderTasks)
Robert Phillips22310d62018-09-05 11:07:21 -0400179 , fTextContext(nullptr)
180 , fPathRendererChain(nullptr)
181 , fSoftwarePathRenderer(nullptr)
Robert Phillips6db27c22019-05-01 10:43:56 -0400182 , fFlushing(false)
Greg Danielf41b2bd2019-08-22 16:19:24 -0400183 , fReduceOpsTaskSplitting(reduceOpsTaskSplitting) {
Robert Phillips22310d62018-09-05 11:07:21 -0400184}
185
186void GrDrawingManager::cleanup() {
Robert Phillips9da87e02019-02-04 13:26:26 -0500187 fDAG.cleanup(fContext->priv().caps());
robertphillips3dc6ae52015-10-20 09:54:32 -0700188
robertphillips13391dd2015-10-30 05:15:11 -0700189 fPathRendererChain = nullptr;
Ben Wagner9ec70c62018-07-12 13:30:47 -0400190 fSoftwarePathRenderer = nullptr;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400191
192 fOnFlushCBObjects.reset();
robertphillips3dc6ae52015-10-20 09:54:32 -0700193}
194
195GrDrawingManager::~GrDrawingManager() {
196 this->cleanup();
197}
198
Robert Phillipsa9162df2019-02-11 14:12:03 -0500199bool GrDrawingManager::wasAbandoned() const {
Robert Phillips6a6de562019-02-15 15:19:15 -0500200 return fContext->priv().abandoned();
robertphillips3dc6ae52015-10-20 09:54:32 -0700201}
202
robertphillips68737822015-10-29 12:12:21 -0700203void GrDrawingManager::freeGpuResources() {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400204 for (int i = fOnFlushCBObjects.count() - 1; i >= 0; --i) {
205 if (!fOnFlushCBObjects[i]->retainOnFreeGpuResources()) {
206 // it's safe to just do this because we're iterating in reverse
207 fOnFlushCBObjects.removeShuffle(i);
208 }
209 }
210
robertphillips68737822015-10-29 12:12:21 -0700211 // a path renderer may be holding onto resources
robertphillips13391dd2015-10-30 05:15:11 -0700212 fPathRendererChain = nullptr;
Ben Wagner9ec70c62018-07-12 13:30:47 -0400213 fSoftwarePathRenderer = nullptr;
Robert Phillipse3302df2017-04-24 07:31:02 -0400214}
215
Robert Phillips7ee385e2017-03-30 08:02:11 -0400216// MDB TODO: make use of the 'proxy' parameter.
Greg Daniel797efca2019-05-09 14:04:20 -0400217GrSemaphoresSubmitted GrDrawingManager::flush(GrSurfaceProxy* proxies[], int numProxies,
218 SkSurface::BackendSurfaceAccess access, const GrFlushInfo& info,
219 const GrPrepareForExternalIORequests& externalRequests) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400220 SkASSERT(numProxies >= 0);
221 SkASSERT(!numProxies || proxies);
Brian Salomon57d2beab2018-09-10 09:35:41 -0400222 GR_CREATE_TRACE_MARKER_CONTEXT("GrDrawingManager", "flush", fContext);
Brian Salomondcbb9d92017-07-19 10:53:20 -0400223
robertphillips7761d612016-05-16 09:14:53 -0700224 if (fFlushing || this->wasAbandoned()) {
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400225 if (info.fFinishedProc) {
226 info.fFinishedProc(info.fFinishedContext);
Greg Daniela3aa75a2019-04-12 14:24:55 -0400227 }
Greg Daniel51316782017-08-02 15:10:09 +0000228 return GrSemaphoresSubmitted::kNo;
joshualittb8918c42015-12-18 09:59:46 -0800229 }
Robert Phillips602df412019-04-08 11:10:39 -0400230
Robert Phillips38d64b02018-09-04 13:23:26 -0400231 SkDEBUGCODE(this->validate());
232
Greg Daniel797efca2019-05-09 14:04:20 -0400233 if (kNone_GrFlushFlags == info.fFlags && !info.fNumSemaphores && !info.fFinishedProc &&
234 !externalRequests.hasRequests()) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400235 bool canSkip = numProxies > 0;
236 for (int i = 0; i < numProxies && canSkip; ++i) {
237 canSkip = !fDAG.isUsed(proxies[i]) && !this->isDDLTarget(proxies[i]);
238 }
239 if (canSkip) {
240 return GrSemaphoresSubmitted::kNo;
241 }
Robert Phillips9313aa72019-04-09 18:41:27 -0400242 }
243
Robert Phillips6a6de562019-02-15 15:19:15 -0500244 auto direct = fContext->priv().asDirectContext();
245 if (!direct) {
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400246 if (info.fFinishedProc) {
247 info.fFinishedProc(info.fFinishedContext);
Greg Daniela3aa75a2019-04-12 14:24:55 -0400248 }
Robert Phillips6a6de562019-02-15 15:19:15 -0500249 return GrSemaphoresSubmitted::kNo; // Can't flush while DDL recording
250 }
251
252 GrGpu* gpu = direct->priv().getGpu();
Robert Phillips874b5352018-03-16 08:48:24 -0400253 if (!gpu) {
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400254 if (info.fFinishedProc) {
255 info.fFinishedProc(info.fFinishedContext);
Greg Daniela3aa75a2019-04-12 14:24:55 -0400256 }
Robert Phillips874b5352018-03-16 08:48:24 -0400257 return GrSemaphoresSubmitted::kNo; // Can't flush while DDL recording
258 }
Greg Daniela3aa75a2019-04-12 14:24:55 -0400259
joshualittb8918c42015-12-18 09:59:46 -0800260 fFlushing = true;
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400261
Robert Phillips6a6de562019-02-15 15:19:15 -0500262 auto resourceProvider = direct->priv().resourceProvider();
263 auto resourceCache = direct->priv().getResourceCache();
264
Chris Dalton6b498102019-08-01 14:14:52 -0600265 // Semi-usually the GrRenderTasks are already closed at this point, but sometimes Ganesh needs
Greg Danielf41b2bd2019-08-22 16:19:24 -0400266 // to flush mid-draw. In that case, the SkGpuDevice's opsTasks won't be closed but need to be
267 // flushed anyway. Closing such opsTasks here will mean new ones will be created to replace them
Chris Dalton6b498102019-08-01 14:14:52 -0600268 // if the SkGpuDevice(s) write to them again.
Robert Phillips9da87e02019-02-04 13:26:26 -0500269 fDAG.closeAll(fContext->priv().caps());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400270 fActiveOpsTask = nullptr;
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400271
Robert Phillips22310d62018-09-05 11:07:21 -0400272 fDAG.prepForFlush();
Brian Salomon601ac802019-02-07 13:37:16 -0500273 if (!fCpuBufferCache) {
274 // We cache more buffers when the backend is using client side arrays. Otherwise, we
275 // expect each pool will use a CPU buffer as a staging buffer before uploading to a GPU
276 // buffer object. Each pool only requires one staging buffer at a time.
277 int maxCachedBuffers = fContext->priv().caps()->preferClientSideDynamicBuffers() ? 2 : 6;
278 fCpuBufferCache = GrBufferAllocPool::CpuBufferCache::Make(maxCachedBuffers);
Brian Salomon58f153c2018-10-18 21:51:15 -0400279 }
Robert Phillipsa4c93ac2017-05-18 11:40:04 -0400280
Robert Phillipse5f73282019-06-18 17:15:04 -0400281 GrOpFlushState flushState(gpu, resourceProvider, &fTokenTracker, fCpuBufferCache);
Robert Phillips40a29d72018-01-18 12:59:22 -0500282
Chris Daltonfe199b72017-05-05 11:26:15 -0400283 GrOnFlushResourceProvider onFlushProvider(this);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500284 // TODO: AFAICT the only reason fFlushState is on GrDrawingManager rather than on the
285 // stack here is to preserve the flush tokens.
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400286
Chris Dalton12658942017-10-05 19:45:25 -0600287 // Prepare any onFlush op lists (e.g. atlases).
Chris Daltonfe199b72017-05-05 11:26:15 -0400288 if (!fOnFlushCBObjects.empty()) {
Chris Dalton6b498102019-08-01 14:14:52 -0600289 fDAG.gatherIDs(&fFlushingRenderTaskIDs);
Robert Phillips22310d62018-09-05 11:07:21 -0400290
Chris Daltonfe199b72017-05-05 11:26:15 -0400291 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
Chris Dalton6b498102019-08-01 14:14:52 -0600292 onFlushCBObject->preFlush(&onFlushProvider, fFlushingRenderTaskIDs.begin(),
Chris Daltonc4b47352019-08-23 10:10:36 -0600293 fFlushingRenderTaskIDs.count());
294 }
295 for (const auto& onFlushRenderTask : fOnFlushRenderTasks) {
296 onFlushRenderTask->makeClosed(*fContext->priv().caps());
Chris Dalton706a6ff2017-11-29 22:01:06 -0700297#ifdef SK_DEBUG
Chris Daltonc4b47352019-08-23 10:10:36 -0600298 // OnFlush callbacks are invoked during flush, and are therefore expected to handle
299 // resource allocation & usage on their own. (No deferred or lazy proxies!)
300 onFlushRenderTask->visitTargetAndSrcProxies_debugOnly(
301 [](GrSurfaceProxy* p, GrMipMapped mipMapped) {
302 SkASSERT(!p->asTextureProxy() || !p->asTextureProxy()->texPriv().isDeferred());
Brian Salomonbeb7f522019-08-30 16:19:42 -0400303 SkASSERT(!p->isLazy());
Chris Dalton4ece96d2019-08-30 11:26:39 -0600304 if (p->requiresManualMSAAResolve()) {
305 // The onFlush callback is responsible for ensuring MSAA gets resolved.
306 SkASSERT(p->asRenderTargetProxy() && !p->asRenderTargetProxy()->isMSAADirty());
307 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600308 if (GrMipMapped::kYes == mipMapped) {
309 // The onFlush callback is responsible for regenerating mips if needed.
310 SkASSERT(p->asTextureProxy() && !p->asTextureProxy()->mipMapsAreDirty());
311 }
312 });
Chris Dalton706a6ff2017-11-29 22:01:06 -0700313#endif
Chris Daltonc4b47352019-08-23 10:10:36 -0600314 onFlushRenderTask->prepare(&flushState);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400315 }
316 }
317
robertphillipsa13e2022015-11-11 12:01:09 -0800318#if 0
Brian Salomon09d994e2016-12-21 11:14:46 -0500319 // Enable this to print out verbose GrOp information
Chris Daltonc4b47352019-08-23 10:10:36 -0600320 SkDEBUGCODE(SkDebugf("onFlush renderTasks:"));
321 for (const auto& onFlushRenderTask : fOnFlushRenderTasks) {
322 SkDEBUGCODE(onFlushRenderTask->dump();)
323 }
324 SkDEBUGCODE(SkDebugf("Normal renderTasks:"));
Chris Dalton6b498102019-08-01 14:14:52 -0600325 for (int i = 0; i < fRenderTasks.count(); ++i) {
326 SkDEBUGCODE(fRenderTasks[i]->dump();)
robertphillips3dc6ae52015-10-20 09:54:32 -0700327 }
robertphillipsa13e2022015-11-11 12:01:09 -0800328#endif
329
Robert Phillipseafd48a2017-11-16 07:52:08 -0500330 int startIndex, stopIndex;
331 bool flushed = false;
332
Robert Phillipsf8e25022017-11-08 15:24:31 -0500333 {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400334 GrResourceAllocator alloc(resourceProvider SkDEBUGCODE(, fDAG.numRenderTasks()));
Chris Dalton6b498102019-08-01 14:14:52 -0600335 for (int i = 0; i < fDAG.numRenderTasks(); ++i) {
336 if (fDAG.renderTask(i)) {
337 fDAG.renderTask(i)->gatherProxyIntervals(&alloc);
Robert Phillips22310d62018-09-05 11:07:21 -0400338 }
Greg Danielf41b2bd2019-08-22 16:19:24 -0400339 alloc.markEndOfOpsTask(i);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500340 }
Robert Phillipsc73666f2019-04-24 08:49:48 -0400341 alloc.determineRecyclability();
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400342
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500343 GrResourceAllocator::AssignError error = GrResourceAllocator::AssignError::kNoError;
Chris Dalton6b498102019-08-01 14:14:52 -0600344 int numRenderTasksExecuted = 0;
Brian Salomon577aa0f2018-11-30 13:32:23 -0500345 while (alloc.assign(&startIndex, &stopIndex, &error)) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500346 if (GrResourceAllocator::AssignError::kFailedProxyInstantiation == error) {
347 for (int i = startIndex; i < stopIndex; ++i) {
Chris Dalton6b498102019-08-01 14:14:52 -0600348 GrRenderTask* renderTask = fDAG.renderTask(i);
349 if (!renderTask) {
350 continue;
Robert Phillips01a91282018-07-26 08:03:04 -0400351 }
Chris Dalton6b498102019-08-01 14:14:52 -0600352 if (!renderTask->isInstantiated()) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400353 // No need to call the renderTask's handleInternalAllocationFailure
354 // since we will already skip executing the renderTask since it is not
355 // instantiated.
Chris Dalton6b498102019-08-01 14:14:52 -0600356 continue;
Robert Phillipsbbcb7f72018-05-31 11:16:19 -0400357 }
Chris Dalton6b498102019-08-01 14:14:52 -0600358 renderTask->handleInternalAllocationFailure();
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500359 }
360 }
Robert Phillips4150eea2018-02-07 17:08:21 -0500361
Chris Dalton6b498102019-08-01 14:14:52 -0600362 if (this->executeRenderTasks(
363 startIndex, stopIndex, &flushState, &numRenderTasksExecuted)) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500364 flushed = true;
365 }
bsalomondc438982016-08-31 11:53:49 -0700366 }
Greg Danielc42b20b2017-10-04 10:34:49 -0400367 }
368
Chris Dalton91ab1552018-04-18 13:24:25 -0600369#ifdef SK_DEBUG
Chris Dalton6b498102019-08-01 14:14:52 -0600370 for (int i = 0; i < fDAG.numRenderTasks(); ++i) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400371 // If there are any remaining opsTaskss at this point, make sure they will not survive the
Chris Dalton91ab1552018-04-18 13:24:25 -0600372 // flush. Otherwise we need to call endFlush() on them.
373 // http://skbug.com/7111
Chris Dalton6b498102019-08-01 14:14:52 -0600374 SkASSERT(!fDAG.renderTask(i) || fDAG.renderTask(i)->unique());
Chris Dalton91ab1552018-04-18 13:24:25 -0600375 }
376#endif
Robert Phillips22310d62018-09-05 11:07:21 -0400377 fDAG.reset();
Robert Phillips15c91422019-05-07 16:54:48 -0400378 this->clearDDLTargets();
robertphillipsa13e2022015-11-11 12:01:09 -0800379
Robert Phillipsc994a932018-06-19 13:09:54 -0400380#ifdef SK_DEBUG
381 // In non-DDL mode this checks that all the flushed ops have been freed from the memory pool.
382 // When we move to partial flushes this assert will no longer be valid.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400383 // In DDL mode this check is somewhat superfluous since the memory for most of the ops/opsTasks
Robert Phillipsc994a932018-06-19 13:09:54 -0400384 // will be stored in the DDL's GrOpMemoryPools.
Robert Phillips9da87e02019-02-04 13:26:26 -0500385 GrOpMemoryPool* opMemoryPool = fContext->priv().opMemoryPool();
Robert Phillipsc994a932018-06-19 13:09:54 -0400386 opMemoryPool->isEmpty();
387#endif
388
Greg Daniel797efca2019-05-09 14:04:20 -0400389 GrSemaphoresSubmitted result = gpu->finishFlush(proxies, numProxies, access, info,
390 externalRequests);
robertphillipsa13e2022015-11-11 12:01:09 -0800391
Brian Salomon57d2beab2018-09-10 09:35:41 -0400392 // Give the cache a chance to purge resources that become purgeable due to flushing.
393 if (flushed) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500394 resourceCache->purgeAsNeeded();
Brian Salomon876a0172019-03-08 11:12:14 -0500395 flushed = false;
bsalomonb77a9072016-09-07 10:02:04 -0700396 }
Chris Daltonfe199b72017-05-05 11:26:15 -0400397 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
Chris Dalton6b498102019-08-01 14:14:52 -0600398 onFlushCBObject->postFlush(fTokenTracker.nextTokenToFlush(), fFlushingRenderTaskIDs.begin(),
399 fFlushingRenderTaskIDs.count());
Brian Salomon876a0172019-03-08 11:12:14 -0500400 flushed = true;
401 }
402 if (flushed) {
403 resourceCache->purgeAsNeeded();
Chris Daltonfe199b72017-05-05 11:26:15 -0400404 }
Chris Dalton6b498102019-08-01 14:14:52 -0600405 fFlushingRenderTaskIDs.reset();
joshualittb8918c42015-12-18 09:59:46 -0800406 fFlushing = false;
Greg Daniel51316782017-08-02 15:10:09 +0000407
408 return result;
robertphillips3dc6ae52015-10-20 09:54:32 -0700409}
410
Chris Dalton6b498102019-08-01 14:14:52 -0600411bool GrDrawingManager::executeRenderTasks(int startIndex, int stopIndex, GrOpFlushState* flushState,
412 int* numRenderTasksExecuted) {
413 SkASSERT(startIndex <= stopIndex && stopIndex <= fDAG.numRenderTasks());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500414
Robert Phillips27483912018-04-20 12:43:18 -0400415#if GR_FLUSH_TIME_OP_SPEW
Greg Danielf41b2bd2019-08-22 16:19:24 -0400416 SkDebugf("Flushing opsTask: %d to %d out of [%d, %d]\n",
Chris Dalton6b498102019-08-01 14:14:52 -0600417 startIndex, stopIndex, 0, fDAG.numRenderTasks());
Robert Phillips27483912018-04-20 12:43:18 -0400418 for (int i = startIndex; i < stopIndex; ++i) {
Chris Dalton6b498102019-08-01 14:14:52 -0600419 if (fDAG.renderTask(i)) {
420 fDAG.renderTask(i)->dump(true);
Robert Phillips1734dd32018-08-21 13:52:09 -0400421 }
Robert Phillips27483912018-04-20 12:43:18 -0400422 }
423#endif
424
Chris Dalton6b498102019-08-01 14:14:52 -0600425 bool anyRenderTasksExecuted = false;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500426
427 for (int i = startIndex; i < stopIndex; ++i) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400428 GrRenderTask* renderTask = fDAG.renderTask(i);
429 if (!renderTask || !renderTask->isInstantiated()) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500430 continue;
431 }
432
Chris Dalton6b498102019-08-01 14:14:52 -0600433 SkASSERT(renderTask->deferredProxiesAreInstantiated());
Robert Phillips22310d62018-09-05 11:07:21 -0400434
Chris Dalton6b498102019-08-01 14:14:52 -0600435 renderTask->prepare(flushState);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500436 }
437
438 // Upload all data to the GPU
439 flushState->preExecuteDraws();
440
Greg Danield2073452018-12-07 11:20:33 -0500441 // For Vulkan, if we have too many oplists to be flushed we end up allocating a lot of resources
442 // for each command buffer associated with the oplists. If this gets too large we can cause the
443 // devices to go OOM. In practice we usually only hit this case in our tests, but to be safe we
444 // put a cap on the number of oplists we will execute before flushing to the GPU to relieve some
445 // memory pressure.
Chris Dalton6b498102019-08-01 14:14:52 -0600446 static constexpr int kMaxRenderTasksBeforeFlush = 100;
Greg Danield2073452018-12-07 11:20:33 -0500447
Chris Daltonc4b47352019-08-23 10:10:36 -0600448 // Execute the onFlush renderTasks first, if any.
449 for (sk_sp<GrRenderTask>& onFlushRenderTask : fOnFlushRenderTasks) {
450 if (!onFlushRenderTask->execute(flushState)) {
451 SkDebugf("WARNING: onFlushRenderTask failed to execute.\n");
Robert Phillipseafd48a2017-11-16 07:52:08 -0500452 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600453 SkASSERT(onFlushRenderTask->unique());
454 onFlushRenderTask = nullptr;
Chris Dalton6b498102019-08-01 14:14:52 -0600455 (*numRenderTasksExecuted)++;
456 if (*numRenderTasksExecuted >= kMaxRenderTasksBeforeFlush) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400457 flushState->gpu()->finishFlush(nullptr, 0, SkSurface::BackendSurfaceAccess::kNoAccess,
Greg Daniel797efca2019-05-09 14:04:20 -0400458 GrFlushInfo(), GrPrepareForExternalIORequests());
Chris Dalton6b498102019-08-01 14:14:52 -0600459 *numRenderTasksExecuted = 0;
Greg Danield2073452018-12-07 11:20:33 -0500460 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500461 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600462 fOnFlushRenderTasks.reset();
Robert Phillipseafd48a2017-11-16 07:52:08 -0500463
464 // Execute the normal op lists.
465 for (int i = startIndex; i < stopIndex; ++i) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400466 GrRenderTask* renderTask = fDAG.renderTask(i);
467 if (!renderTask || !renderTask->isInstantiated()) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500468 continue;
469 }
470
Greg Daniel15ecdf92019-08-30 15:35:23 -0400471 if (renderTask->execute(flushState)) {
Chris Dalton6b498102019-08-01 14:14:52 -0600472 anyRenderTasksExecuted = true;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500473 }
Chris Dalton6b498102019-08-01 14:14:52 -0600474 (*numRenderTasksExecuted)++;
475 if (*numRenderTasksExecuted >= kMaxRenderTasksBeforeFlush) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400476 flushState->gpu()->finishFlush(nullptr, 0, SkSurface::BackendSurfaceAccess::kNoAccess,
Greg Daniel797efca2019-05-09 14:04:20 -0400477 GrFlushInfo(), GrPrepareForExternalIORequests());
Chris Dalton6b498102019-08-01 14:14:52 -0600478 *numRenderTasksExecuted = 0;
Greg Danield2073452018-12-07 11:20:33 -0500479 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500480 }
481
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400482 SkASSERT(!flushState->opsRenderPass());
Robert Phillips40a29d72018-01-18 12:59:22 -0500483 SkASSERT(fTokenTracker.nextDrawToken() == fTokenTracker.nextTokenToFlush());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500484
Chris Dalton6b498102019-08-01 14:14:52 -0600485 // We reset the flush state before the RenderTasks so that the last resources to be freed are
486 // those that are written to in the RenderTasks. This helps to make sure the most recently used
487 // resources are the last to be purged by the resource cache.
Robert Phillipseafd48a2017-11-16 07:52:08 -0500488 flushState->reset();
489
Chris Dalton6b498102019-08-01 14:14:52 -0600490 fDAG.removeRenderTasks(startIndex, stopIndex);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500491
Chris Dalton6b498102019-08-01 14:14:52 -0600492 return anyRenderTasksExecuted;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500493}
494
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400495GrSemaphoresSubmitted GrDrawingManager::flushSurfaces(GrSurfaceProxy* proxies[], int numProxies,
496 SkSurface::BackendSurfaceAccess access,
497 const GrFlushInfo& info) {
bsalomon6a2b1942016-09-08 11:28:59 -0700498 if (this->wasAbandoned()) {
Greg Daniel51316782017-08-02 15:10:09 +0000499 return GrSemaphoresSubmitted::kNo;
bsalomon6a2b1942016-09-08 11:28:59 -0700500 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400501 SkDEBUGCODE(this->validate());
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400502 SkASSERT(numProxies >= 0);
503 SkASSERT(!numProxies || proxies);
bsalomon6a2b1942016-09-08 11:28:59 -0700504
Robert Phillips6a6de562019-02-15 15:19:15 -0500505 auto direct = fContext->priv().asDirectContext();
506 if (!direct) {
507 return GrSemaphoresSubmitted::kNo; // Can't flush while DDL recording
508 }
509
510 GrGpu* gpu = direct->priv().getGpu();
Robert Phillips874b5352018-03-16 08:48:24 -0400511 if (!gpu) {
512 return GrSemaphoresSubmitted::kNo; // Can't flush while DDL recording
513 }
514
Robert Phillipsacc10fa2019-04-01 09:50:20 -0400515 // TODO: It is important to upgrade the drawingmanager to just flushing the
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400516 // portion of the DAG required by 'proxies' in order to restore some of the
Robert Phillipsacc10fa2019-04-01 09:50:20 -0400517 // semantics of this method.
Greg Daniel797efca2019-05-09 14:04:20 -0400518 GrSemaphoresSubmitted result = this->flush(proxies, numProxies, access, info,
519 GrPrepareForExternalIORequests());
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400520 for (int i = 0; i < numProxies; ++i) {
Chris Dalton4ece96d2019-08-30 11:26:39 -0600521 GrSurfaceProxy* proxy = proxies[i];
522 if (!proxy->isInstantiated()) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400523 return result;
524 }
Chris Dalton4ece96d2019-08-30 11:26:39 -0600525 // In the flushSurfaces case, we need to resolve MSAA immediately after flush. This is
526 // because the client will call through to this method when drawing into a target created by
527 // wrapBackendTextureAsRenderTarget, and will expect the original texture to be fully
528 // resolved upon return.
529 if (proxy->requiresManualMSAAResolve()) {
530 auto* rtProxy = proxy->asRenderTargetProxy();
531 SkASSERT(rtProxy);
532 if (rtProxy->isMSAADirty()) {
533 SkASSERT(rtProxy->peekRenderTarget());
534 gpu->resolveRenderTarget(rtProxy->peekRenderTarget());
535 rtProxy->markMSAAResolved();
536 }
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400537 }
Chris Dalton3d770272019-08-14 09:24:37 -0600538 // If, after a flush, any of the proxies of interest have dirty mipmaps, regenerate them in
539 // case their backend textures are being stolen.
540 // (This special case is exercised by the ReimportImageTextureWithMipLevels test.)
541 // 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 -0600542 if (auto* textureProxy = proxy->asTextureProxy()) {
Chris Dalton3d770272019-08-14 09:24:37 -0600543 if (textureProxy->mipMapsAreDirty()) {
Chris Dalton4ece96d2019-08-30 11:26:39 -0600544 SkASSERT(textureProxy->peekTexture());
Chris Dalton3d770272019-08-14 09:24:37 -0600545 gpu->regenerateMipMapLevels(textureProxy->peekTexture());
546 textureProxy->markMipMapsClean();
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400547 }
Brian Salomon930f9392018-06-20 16:25:26 -0400548 }
bsalomon6a2b1942016-09-08 11:28:59 -0700549 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400550
551 SkDEBUGCODE(this->validate());
Greg Daniel51316782017-08-02 15:10:09 +0000552 return result;
bsalomon6a2b1942016-09-08 11:28:59 -0700553}
554
Chris Daltonfe199b72017-05-05 11:26:15 -0400555void GrDrawingManager::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
556 fOnFlushCBObjects.push_back(onFlushCBObject);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400557}
558
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500559#if GR_TEST_UTILS
560void GrDrawingManager::testingOnly_removeOnFlushCallbackObject(GrOnFlushCallbackObject* cb) {
561 int n = std::find(fOnFlushCBObjects.begin(), fOnFlushCBObjects.end(), cb) -
562 fOnFlushCBObjects.begin();
563 SkASSERT(n < fOnFlushCBObjects.count());
564 fOnFlushCBObjects.removeShuffle(n);
565}
566#endif
567
Chris Dalton6b498102019-08-01 14:14:52 -0600568void GrDrawingManager::moveRenderTasksToDDL(SkDeferredDisplayList* ddl) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400569 SkDEBUGCODE(this->validate());
570
Chris Dalton6b498102019-08-01 14:14:52 -0600571 // no renderTask should receive a new command after this
Robert Phillips9da87e02019-02-04 13:26:26 -0500572 fDAG.closeAll(fContext->priv().caps());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400573 fActiveOpsTask = nullptr;
Robert Phillips7a137052018-02-01 11:23:12 -0500574
Chris Dalton6b498102019-08-01 14:14:52 -0600575 fDAG.swap(&ddl->fRenderTasks);
Robert Phillips867ce8f2018-06-21 10:28:36 -0400576
Robert Phillips774168e2018-05-31 12:43:27 -0400577 if (fPathRendererChain) {
578 if (auto ccpr = fPathRendererChain->getCoverageCountingPathRenderer()) {
579 ddl->fPendingPaths = ccpr->detachPendingPaths();
580 }
581 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400582
583 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500584}
585
Chris Dalton6b498102019-08-01 14:14:52 -0600586void GrDrawingManager::copyRenderTasksFromDDL(const SkDeferredDisplayList* ddl,
Robert Phillips62000362018-02-01 09:10:04 -0500587 GrRenderTargetProxy* newDest) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400588 SkDEBUGCODE(this->validate());
589
Greg Danielf41b2bd2019-08-22 16:19:24 -0400590 if (fActiveOpsTask) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400591 // This is a temporary fix for the partial-MDB world. In that world we're not
Greg Danielf41b2bd2019-08-22 16:19:24 -0400592 // reordering so ops that (in the single opsTask world) would've just glommed onto the
593 // end of the single opsTask but referred to a far earlier RT need to appear in their
594 // own opsTask.
595 fActiveOpsTask->makeClosed(*fContext->priv().caps());
596 fActiveOpsTask = nullptr;
Robert Phillips38d64b02018-09-04 13:23:26 -0400597 }
598
Robert Phillips15c91422019-05-07 16:54:48 -0400599 this->addDDLTarget(newDest);
600
Robert Phillips62000362018-02-01 09:10:04 -0500601 // Here we jam the proxy that backs the current replay SkSurface into the LazyProxyData.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400602 // The lazy proxy that references it (in the copied opsTasks) will steal its GrTexture.
Robert Phillips62000362018-02-01 09:10:04 -0500603 ddl->fLazyProxyData->fReplayDest = newDest;
Robert Phillips774168e2018-05-31 12:43:27 -0400604
605 if (ddl->fPendingPaths.size()) {
606 GrCoverageCountingPathRenderer* ccpr = this->getCoverageCountingPathRenderer();
607
608 ccpr->mergePendingPaths(ddl->fPendingPaths);
609 }
Robert Phillips22310d62018-09-05 11:07:21 -0400610
Chris Dalton6b498102019-08-01 14:14:52 -0600611 fDAG.add(ddl->fRenderTasks);
Robert Phillips38d64b02018-09-04 13:23:26 -0400612
613 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500614}
615
Robert Phillips38d64b02018-09-04 13:23:26 -0400616#ifdef SK_DEBUG
617void GrDrawingManager::validate() const {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400618 if (fDAG.sortingRenderTasks() && fReduceOpsTaskSplitting) {
619 SkASSERT(!fActiveOpsTask);
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400620 } else {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400621 if (fActiveOpsTask) {
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400622 SkASSERT(!fDAG.empty());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400623 SkASSERT(!fActiveOpsTask->isClosed());
624 SkASSERT(fActiveOpsTask == fDAG.back());
Robert Phillips38d64b02018-09-04 13:23:26 -0400625 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400626
Chris Dalton6b498102019-08-01 14:14:52 -0600627 for (int i = 0; i < fDAG.numRenderTasks(); ++i) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400628 if (fActiveOpsTask != fDAG.renderTask(i)) {
Chris Daltone2a903e2019-09-18 13:41:50 -0600629 // The resolveTask associated with the activeTask remains open for as long as the
630 // activeTask does.
631 bool isActiveResolveTask =
632 fActiveOpsTask && fActiveOpsTask->fTextureResolveTask == fDAG.renderTask(i);
633 SkASSERT(isActiveResolveTask || fDAG.renderTask(i)->isClosed());
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400634 }
635 }
636
637 if (!fDAG.empty() && !fDAG.back()->isClosed()) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400638 SkASSERT(fActiveOpsTask == fDAG.back());
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400639 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400640 }
641}
642#endif
643
Greg Danielbbfec9d2019-08-20 10:56:51 -0400644void GrDrawingManager::closeRenderTasksForNewRenderTask(GrSurfaceProxy* target) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400645 if (target && fDAG.sortingRenderTasks() && fReduceOpsTaskSplitting) {
Chris Dalton5fe99772019-08-06 11:57:39 -0600646 // In this case we need to close all the renderTasks that rely on the current contents of
647 // 'target'. That is bc we're going to update the content of the proxy so they need to be
648 // split in case they use both the old and new content. (This is a bit of an overkill: they
649 // really only need to be split if they ever reference proxy's contents again but that is
650 // hard to predict/handle).
651 if (GrRenderTask* lastRenderTask = target->getLastRenderTask()) {
Chris Dalton6b498102019-08-01 14:14:52 -0600652 lastRenderTask->closeThoseWhoDependOnMe(*fContext->priv().caps());
Robert Phillips46acf9d2018-10-09 09:31:40 -0400653 }
Greg Danielf41b2bd2019-08-22 16:19:24 -0400654 } else if (fActiveOpsTask) {
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400655 // This is a temporary fix for the partial-MDB world. In that world we're not
Greg Danielf41b2bd2019-08-22 16:19:24 -0400656 // reordering so ops that (in the single opsTask world) would've just glommed onto the
657 // end of the single opsTask but referred to a far earlier RT need to appear in their
658 // own opsTask.
659 fActiveOpsTask->makeClosed(*fContext->priv().caps());
660 fActiveOpsTask = nullptr;
robertphillips3dc6ae52015-10-20 09:54:32 -0700661 }
Chris Dalton5fe99772019-08-06 11:57:39 -0600662}
663
Greg Danielf41b2bd2019-08-22 16:19:24 -0400664sk_sp<GrOpsTask> GrDrawingManager::newOpsTask(sk_sp<GrRenderTargetProxy> rtp, bool managedOpsTask) {
Chris Dalton5fe99772019-08-06 11:57:39 -0600665 SkDEBUGCODE(this->validate());
666 SkASSERT(fContext);
667
Greg Danielbbfec9d2019-08-20 10:56:51 -0400668 this->closeRenderTasksForNewRenderTask(rtp.get());
robertphillips3dc6ae52015-10-20 09:54:32 -0700669
Greg Danielf41b2bd2019-08-22 16:19:24 -0400670 sk_sp<GrOpsTask> opsTask(new GrOpsTask(fContext->priv().refOpMemoryPool(), rtp,
671 fContext->priv().auditTrail()));
672 SkASSERT(rtp->getLastRenderTask() == opsTask.get());
robertphillips3dc6ae52015-10-20 09:54:32 -0700673
Greg Danielf41b2bd2019-08-22 16:19:24 -0400674 if (managedOpsTask) {
675 fDAG.add(opsTask);
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400676
Greg Danielf41b2bd2019-08-22 16:19:24 -0400677 if (!fDAG.sortingRenderTasks() || !fReduceOpsTaskSplitting) {
678 fActiveOpsTask = opsTask.get();
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400679 }
Robert Phillips941d1442017-06-14 16:37:02 -0400680 }
robertphillips3dc6ae52015-10-20 09:54:32 -0700681
Robert Phillips38d64b02018-09-04 13:23:26 -0400682 SkDEBUGCODE(this->validate());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400683 return opsTask;
robertphillips3dc6ae52015-10-20 09:54:32 -0700684}
685
Chris Daltone2a903e2019-09-18 13:41:50 -0600686GrTextureResolveRenderTask* GrDrawingManager::newTextureResolveRenderTask(const GrCaps& caps) {
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600687 // 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 -0600688 // in sorting and opsTask reduction mode) the render tasks that depend on any proxy's current
Greg Danielf41b2bd2019-08-22 16:19:24 -0400689 // state. This is because those opsTasks can still receive new ops and because if they refer to
Chris Dalton4ece96d2019-08-30 11:26:39 -0600690 // the mipmapped version of 'proxy', they will then come to depend on the render task being
691 // created here.
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600692 //
Greg Danielf41b2bd2019-08-22 16:19:24 -0400693 // Add the new textureResolveTask before the fActiveOpsTask (if not in
694 // sorting/opsTask-splitting-reduction mode) because it will depend upon this resolve task.
Chris Dalton3d770272019-08-14 09:24:37 -0600695 // NOTE: Putting it here will also reduce the amount of work required by the topological sort.
Chris Daltone2a903e2019-09-18 13:41:50 -0600696 return static_cast<GrTextureResolveRenderTask*>(fDAG.addBeforeLast(
697 sk_make_sp<GrTextureResolveRenderTask>()));
Chris Dalton3d770272019-08-14 09:24:37 -0600698}
699
Greg Danielc30f1a92019-09-06 15:28:58 -0400700void GrDrawingManager::newWaitRenderTask(sk_sp<GrSurfaceProxy> proxy,
701 std::unique_ptr<sk_sp<GrSemaphore>[]> semaphores,
702 int numSemaphores) {
703 SkDEBUGCODE(this->validate());
704 SkASSERT(fContext);
705
706 const GrCaps& caps = *fContext->priv().caps();
707
708 sk_sp<GrWaitRenderTask> waitTask = sk_make_sp<GrWaitRenderTask>(proxy, std::move(semaphores),
709 numSemaphores);
710 if (fReduceOpsTaskSplitting) {
711 GrRenderTask* lastTask = proxy->getLastRenderTask();
712 if (lastTask && !lastTask->isClosed()) {
713 // We directly make the currently open renderTask depend on waitTask instead of using
714 // the proxy version of addDependency. The waitTask will never need to trigger any
715 // resolves or mip map generation which is the main advantage of going through the proxy
716 // version. Additionally we would've had to temporarily set the wait task as the
717 // lastRenderTask on the proxy, add the dependency, and then reset the lastRenderTask to
718 // lastTask. Additionally we add all dependencies of lastTask to waitTask so that the
719 // waitTask doesn't get reordered before them and unnecessarily block those tasks.
720 // Note: Any previous Ops already in lastTask will get blocked by the wait semaphore
721 // even though they don't need to be for correctness.
722
723 // Make sure we add the dependencies of lastTask to waitTask first or else we'll get a
724 // circular self dependency of waitTask on waitTask.
725 waitTask->addDependenciesFromOtherTask(lastTask);
726 lastTask->addDependency(waitTask.get());
727 } else {
728 // If there is a last task we set the waitTask to depend on it so that it doesn't get
729 // reordered in front of the lastTask causing the lastTask to be blocked by the
730 // semaphore. Again we directly just go through adding the dependency to the task and
731 // not the proxy since we don't need to worry about resolving anything.
732 if (lastTask) {
733 waitTask->addDependency(lastTask);
734 }
735 proxy->setLastRenderTask(waitTask.get());
736 }
737 fDAG.add(waitTask);
738 } else {
739 if (fActiveOpsTask && (fActiveOpsTask->fTarget == proxy)) {
740 SkASSERT(proxy->getLastRenderTask() == fActiveOpsTask);
741 fDAG.addBeforeLast(waitTask);
742 // In this case we keep the current renderTask open but just insert the new waitTask
743 // before it in the list. The waitTask will never need to trigger any resolves or mip
744 // map generation which is the main advantage of going through the proxy version.
745 // Additionally we would've had to temporarily set the wait task as the lastRenderTask
746 // on the proxy, add the dependency, and then reset the lastRenderTask to
747 // fActiveOpsTask. Additionally we make the waitTask depend on all of fActiveOpsTask
748 // dependencies so that we don't unnecessarily reorder the waitTask before them.
749 // Note: Any previous Ops already in fActiveOpsTask will get blocked by the wait
750 // semaphore even though they don't need to be for correctness.
751
752 // Make sure we add the dependencies of fActiveOpsTask to waitTask first or else we'll
753 // get a circular self dependency of waitTask on waitTask.
754 waitTask->addDependenciesFromOtherTask(fActiveOpsTask);
755 fActiveOpsTask->addDependency(waitTask.get());
756 } else {
757 // In this case we just close the previous RenderTask and start and append the waitTask
758 // to the DAG. Since it is the last task now we call setLastRenderTask on the proxy. If
759 // there is a lastTask on the proxy we make waitTask depend on that task. This
760 // dependency isn't strictly needed but it does keep the DAG from reordering the
761 // waitTask earlier and blocking more tasks.
762 if (GrRenderTask* lastTask = proxy->getLastRenderTask()) {
763 waitTask->addDependency(lastTask);
764 }
765 proxy->setLastRenderTask(waitTask.get());
766 this->closeRenderTasksForNewRenderTask(proxy.get());
767 fDAG.add(waitTask);
768 }
769 }
770 waitTask->makeClosed(caps);
771
772 SkDEBUGCODE(this->validate());
773}
774
Greg Danielbbfec9d2019-08-20 10:56:51 -0400775void GrDrawingManager::newTransferFromRenderTask(sk_sp<GrSurfaceProxy> srcProxy,
776 const SkIRect& srcRect,
777 GrColorType surfaceColorType,
778 GrColorType dstColorType,
779 sk_sp<GrGpuBuffer> dstBuffer,
780 size_t dstOffset) {
781 SkDEBUGCODE(this->validate());
782 SkASSERT(fContext);
783 // This copies from srcProxy to dstBuffer so it doesn't have a real target.
784 this->closeRenderTasksForNewRenderTask(nullptr);
785
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600786 GrRenderTask* task = fDAG.add(sk_make_sp<GrTransferFromRenderTask>(
787 srcProxy, srcRect, surfaceColorType, dstColorType, std::move(dstBuffer), dstOffset));
Greg Danielbbfec9d2019-08-20 10:56:51 -0400788
789 const GrCaps& caps = *fContext->priv().caps();
790
791 // We always say GrMipMapped::kNo here since we are always just copying from the base layer. We
792 // don't need to make sure the whole mip map chain is valid.
793 task->addDependency(srcProxy.get(), GrMipMapped::kNo, GrTextureResolveManager(this), caps);
794 task->makeClosed(caps);
795
Greg Danielbbfec9d2019-08-20 10:56:51 -0400796 // We have closed the previous active oplist but since a new oplist isn't being added there
797 // shouldn't be an active one.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400798 SkASSERT(!fActiveOpsTask);
Greg Danielbbfec9d2019-08-20 10:56:51 -0400799 SkDEBUGCODE(this->validate());
800}
801
Greg Daniele227fe42019-08-21 13:52:24 -0400802bool GrDrawingManager::newCopyRenderTask(sk_sp<GrSurfaceProxy> srcProxy,
803 const SkIRect& srcRect,
804 sk_sp<GrSurfaceProxy> dstProxy,
805 const SkIPoint& dstPoint) {
806 SkDEBUGCODE(this->validate());
807 SkASSERT(fContext);
808 this->closeRenderTasksForNewRenderTask(dstProxy.get());
809
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600810 GrRenderTask* task = fDAG.add(GrCopyRenderTask::Make(srcProxy, srcRect, dstProxy, dstPoint));
Greg Daniele227fe42019-08-21 13:52:24 -0400811 if (!task) {
812 return false;
813 }
814
815 const GrCaps& caps = *fContext->priv().caps();
816
817 // We always say GrMipMapped::kNo here since we are always just copying from the base layer to
818 // another base layer. We don't need to make sure the whole mip map chain is valid.
819 task->addDependency(srcProxy.get(), GrMipMapped::kNo, GrTextureResolveManager(this), caps);
820 task->makeClosed(caps);
821
Greg Daniele227fe42019-08-21 13:52:24 -0400822 // We have closed the previous active oplist but since a new oplist isn't being added there
823 // shouldn't be an active one.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400824 SkASSERT(!fActiveOpsTask);
Greg Daniele227fe42019-08-21 13:52:24 -0400825 SkDEBUGCODE(this->validate());
826 return true;
827}
828
Herb Derby26cbe512018-05-24 14:39:01 -0400829GrTextContext* GrDrawingManager::getTextContext() {
830 if (!fTextContext) {
831 fTextContext = GrTextContext::Make(fOptionsForTextContext);
brianosman86e76262016-08-11 12:17:31 -0700832 }
833
Herb Derby26cbe512018-05-24 14:39:01 -0400834 return fTextContext.get();
brianosman86e76262016-08-11 12:17:31 -0700835}
836
robertphillips68737822015-10-29 12:12:21 -0700837/*
838 * This method finds a path renderer that can draw the specified path on
839 * the provided target.
840 * Due to its expense, the software path renderer has split out so it can
841 * can be individually allowed/disallowed via the "allowSW" boolean.
842 */
843GrPathRenderer* GrDrawingManager::getPathRenderer(const GrPathRenderer::CanDrawPathArgs& args,
844 bool allowSW,
845 GrPathRendererChain::DrawType drawType,
846 GrPathRenderer::StencilSupport* stencilSupport) {
847
848 if (!fPathRendererChain) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400849 fPathRendererChain.reset(new GrPathRendererChain(fContext, fOptionsForPathRendererChain));
robertphillips68737822015-10-29 12:12:21 -0700850 }
851
852 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(args, drawType, stencilSupport);
853 if (!pr && allowSW) {
Brian Salomone7df0bb2018-05-07 14:44:57 -0400854 auto swPR = this->getSoftwarePathRenderer();
855 if (GrPathRenderer::CanDrawPath::kNo != swPR->canDrawPath(args)) {
856 pr = swPR;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500857 }
robertphillips68737822015-10-29 12:12:21 -0700858 }
859
860 return pr;
861}
862
Brian Salomone7df0bb2018-05-07 14:44:57 -0400863GrPathRenderer* GrDrawingManager::getSoftwarePathRenderer() {
864 if (!fSoftwarePathRenderer) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400865 fSoftwarePathRenderer.reset(
Robert Phillips9da87e02019-02-04 13:26:26 -0500866 new GrSoftwarePathRenderer(fContext->priv().proxyProvider(),
Ben Wagner9ec70c62018-07-12 13:30:47 -0400867 fOptionsForPathRendererChain.fAllowPathMaskCaching));
Brian Salomone7df0bb2018-05-07 14:44:57 -0400868 }
Ben Wagner9ec70c62018-07-12 13:30:47 -0400869 return fSoftwarePathRenderer.get();
Brian Salomone7df0bb2018-05-07 14:44:57 -0400870}
871
Chris Daltonfddb6c02017-11-04 15:22:22 -0600872GrCoverageCountingPathRenderer* GrDrawingManager::getCoverageCountingPathRenderer() {
873 if (!fPathRendererChain) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400874 fPathRendererChain.reset(new GrPathRendererChain(fContext, fOptionsForPathRendererChain));
Chris Daltonfddb6c02017-11-04 15:22:22 -0600875 }
876 return fPathRendererChain->getCoverageCountingPathRenderer();
877}
878
Brian Salomon653f42f2018-07-10 10:07:31 -0400879void GrDrawingManager::flushIfNecessary() {
Robert Phillips6a6de562019-02-15 15:19:15 -0500880 auto direct = fContext->priv().asDirectContext();
881 if (!direct) {
882 return;
883 }
884
885 auto resourceCache = direct->priv().getResourceCache();
Brian Salomon653f42f2018-07-10 10:07:31 -0400886 if (resourceCache && resourceCache->requestsFlush()) {
Greg Daniel797efca2019-05-09 14:04:20 -0400887 this->flush(nullptr, 0, SkSurface::BackendSurfaceAccess::kNoAccess, GrFlushInfo(),
888 GrPrepareForExternalIORequests());
Brian Salomon57d2beab2018-09-10 09:35:41 -0400889 resourceCache->purgeAsNeeded();
Brian Salomon653f42f2018-07-10 10:07:31 -0400890 }
891}
892
Brian Salomonbf6b9792019-08-21 09:38:10 -0400893std::unique_ptr<GrRenderTargetContext> GrDrawingManager::makeRenderTargetContext(
Brian Salomond6287472019-06-24 15:50:07 -0400894 sk_sp<GrSurfaceProxy> sProxy,
895 GrColorType colorType,
896 sk_sp<SkColorSpace> colorSpace,
897 const SkSurfaceProps* surfaceProps,
Greg Danielf41b2bd2019-08-22 16:19:24 -0400898 bool managedOpsTask) {
Robert Phillips37430132016-11-09 06:50:43 -0500899 if (this->wasAbandoned() || !sProxy->asRenderTargetProxy()) {
robertphillips3dc6ae52015-10-20 09:54:32 -0700900 return nullptr;
901 }
902
brianosman0e22eb82016-08-30 07:07:59 -0700903 // SkSurface catches bad color space usage at creation. This check handles anything that slips
Brian Salomon366093f2018-02-13 09:25:22 -0500904 // by, including internal usage.
Robert Phillipsf209e882019-06-25 15:59:50 -0400905 if (!SkSurface_Gpu::Valid(fContext->priv().caps(), sProxy->backendFormat())) {
brianosmana9c3c6a2016-09-29 10:08:36 -0700906 SkDEBUGFAIL("Invalid config and colorspace combination");
brianosman0e22eb82016-08-30 07:07:59 -0700907 return nullptr;
908 }
joshualitt96880d92016-02-16 10:36:53 -0800909
Robert Phillips0d075de2019-03-04 11:08:13 -0500910 sk_sp<GrRenderTargetProxy> renderTargetProxy(sk_ref_sp(sProxy->asRenderTargetProxy()));
Robert Phillips2c862492017-01-18 10:08:39 -0500911
Brian Salomonbf6b9792019-08-21 09:38:10 -0400912 return std::unique_ptr<GrRenderTargetContext>(
913 new GrRenderTargetContext(fContext,
914 std::move(renderTargetProxy),
915 colorType,
916 std::move(colorSpace),
917 surfaceProps,
Greg Danielf41b2bd2019-08-22 16:19:24 -0400918 managedOpsTask));
robertphillips3dc6ae52015-10-20 09:54:32 -0700919}
Brian Osman45580d32016-11-23 09:37:01 -0500920
Brian Salomonbf6b9792019-08-21 09:38:10 -0400921std::unique_ptr<GrTextureContext> GrDrawingManager::makeTextureContext(
922 sk_sp<GrSurfaceProxy> sProxy,
923 GrColorType colorType,
924 SkAlphaType alphaType,
925 sk_sp<SkColorSpace> colorSpace) {
Brian Osman45580d32016-11-23 09:37:01 -0500926 if (this->wasAbandoned() || !sProxy->asTextureProxy()) {
927 return nullptr;
928 }
929
Robert Phillips2c862492017-01-18 10:08:39 -0500930 // SkSurface catches bad color space usage at creation. This check handles anything that slips
Brian Salomon366093f2018-02-13 09:25:22 -0500931 // by, including internal usage.
Robert Phillipsf209e882019-06-25 15:59:50 -0400932 if (!SkSurface_Gpu::Valid(fContext->priv().caps(), sProxy->backendFormat())) {
Robert Phillips2c862492017-01-18 10:08:39 -0500933 SkDEBUGFAIL("Invalid config and colorspace combination");
934 return nullptr;
935 }
936
Robert Phillips383c4182018-02-07 12:47:44 -0500937 // GrTextureRenderTargets should always be using a GrRenderTargetContext
Brian Osman45580d32016-11-23 09:37:01 -0500938 SkASSERT(!sProxy->asRenderTargetProxy());
939
940 sk_sp<GrTextureProxy> textureProxy(sk_ref_sp(sProxy->asTextureProxy()));
941
Brian Salomonbf6b9792019-08-21 09:38:10 -0400942 return std::unique_ptr<GrTextureContext>(new GrTextureContext(
943 fContext, std::move(textureProxy), colorType, alphaType, std::move(colorSpace)));
Brian Osman45580d32016-11-23 09:37:01 -0500944}