blob: df9bf309a40dffb03c5a1dea07d97f067dcc9d2e [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"
Robert Phillips7eb0a5f2020-06-09 14:15:09 -040013#include "src/core/SkDeferredDisplayListPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/core/SkTTopoSort.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040015#include "src/gpu/GrAuditTrail.h"
Brian Salomon9241a6d2019-10-03 13:26:54 -040016#include "src/gpu/GrClientMappedBufferManager.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/gpu/GrContextPriv.h"
Greg Daniele227fe42019-08-21 13:52:24 -040018#include "src/gpu/GrCopyRenderTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/gpu/GrGpu.h"
20#include "src/gpu/GrMemoryPool.h"
21#include "src/gpu/GrOnFlushResourceProvider.h"
22#include "src/gpu/GrRecordingContextPriv.h"
23#include "src/gpu/GrRenderTargetContext.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040024#include "src/gpu/GrRenderTargetProxy.h"
Chris Dalton6b498102019-08-01 14:14:52 -060025#include "src/gpu/GrRenderTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/gpu/GrResourceAllocator.h"
27#include "src/gpu/GrResourceProvider.h"
28#include "src/gpu/GrSoftwarePathRenderer.h"
Greg Daniel46e366a2019-12-16 14:38:36 -050029#include "src/gpu/GrSurfaceContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/gpu/GrSurfaceProxyPriv.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000031#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050032#include "src/gpu/GrTexturePriv.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040033#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050034#include "src/gpu/GrTextureProxyPriv.h"
Chris Dalton3d770272019-08-14 09:24:37 -060035#include "src/gpu/GrTextureResolveRenderTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050036#include "src/gpu/GrTracing.h"
Greg Danielbbfec9d2019-08-20 10:56:51 -040037#include "src/gpu/GrTransferFromRenderTask.h"
Greg Danielc30f1a92019-09-06 15:28:58 -040038#include "src/gpu/GrWaitRenderTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050039#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
Herb Derbya08bde62020-06-12 15:46:06 -040040#include "src/gpu/text/GrSDFTOptions.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050041#include "src/image/SkSurface_Gpu.h"
robertphillips498d7ac2015-10-30 10:11:30 -070042
Chris Dalton6b498102019-08-01 14:14:52 -060043GrDrawingManager::RenderTaskDAG::RenderTaskDAG(bool sortRenderTasks)
44 : fSortRenderTasks(sortRenderTasks) {}
Robert Phillipsa3f70262018-02-08 10:59:38 -050045
Chris Dalton6b498102019-08-01 14:14:52 -060046GrDrawingManager::RenderTaskDAG::~RenderTaskDAG() {}
Robert Phillips22310d62018-09-05 11:07:21 -040047
Chris Dalton6b498102019-08-01 14:14:52 -060048void GrDrawingManager::RenderTaskDAG::gatherIDs(SkSTArray<8, uint32_t, true>* idArray) const {
49 idArray->reset(fRenderTasks.count());
50 for (int i = 0; i < fRenderTasks.count(); ++i) {
51 if (fRenderTasks[i]) {
52 (*idArray)[i] = fRenderTasks[i]->uniqueID();
Robert Phillips22310d62018-09-05 11:07:21 -040053 }
54 }
55}
56
Chris Dalton6b498102019-08-01 14:14:52 -060057void GrDrawingManager::RenderTaskDAG::reset() {
58 fRenderTasks.reset();
Robert Phillips22310d62018-09-05 11:07:21 -040059}
60
Adlai Holler6f1487f2020-06-17 18:33:08 -040061void GrDrawingManager::RenderTaskDAG::rawRemoveRenderTasks(int startIndex, int stopIndex) {
Robert Phillips22310d62018-09-05 11:07:21 -040062 for (int i = startIndex; i < stopIndex; ++i) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -040063 fRenderTasks[i] = nullptr;
Robert Phillips22310d62018-09-05 11:07:21 -040064 }
65}
66
Chris Dalton6b498102019-08-01 14:14:52 -060067bool GrDrawingManager::RenderTaskDAG::isUsed(GrSurfaceProxy* proxy) const {
Adlai Holler33d569e2020-06-16 14:30:08 -040068 for (const auto& task : fRenderTasks) {
69 if (task && task->isUsed(proxy)) {
Robert Phillips9313aa72019-04-09 18:41:27 -040070 return true;
71 }
72 }
73
74 return false;
75}
76
Chris Dalton3d770272019-08-14 09:24:37 -060077GrRenderTask* GrDrawingManager::RenderTaskDAG::add(sk_sp<GrRenderTask> renderTask) {
Chris Dalton6aeb8e82019-08-27 11:52:19 -060078 if (renderTask) {
79 return fRenderTasks.emplace_back(std::move(renderTask)).get();
80 }
81 return nullptr;
Chris Dalton3d770272019-08-14 09:24:37 -060082}
83
84GrRenderTask* GrDrawingManager::RenderTaskDAG::addBeforeLast(sk_sp<GrRenderTask> renderTask) {
85 SkASSERT(!fRenderTasks.empty());
Chris Dalton6aeb8e82019-08-27 11:52:19 -060086 if (renderTask) {
87 // Release 'fRenderTasks.back()' and grab the raw pointer, in case the SkTArray grows
88 // and reallocates during emplace_back.
89 fRenderTasks.emplace_back(fRenderTasks.back().release());
90 return (fRenderTasks[fRenderTasks.count() - 2] = std::move(renderTask)).get();
91 }
92 return nullptr;
Robert Phillips22310d62018-09-05 11:07:21 -040093}
94
Greg Danielf41b2bd2019-08-22 16:19:24 -040095void GrDrawingManager::RenderTaskDAG::add(const SkTArray<sk_sp<GrRenderTask>>& renderTasks) {
Robert Phillips19f466d2020-02-26 10:27:07 -050096#ifdef SK_DEBUG
97 for (auto& renderTask : renderTasks) {
98 SkASSERT(renderTask->unique());
99 }
100#endif
101
Greg Danielf41b2bd2019-08-22 16:19:24 -0400102 fRenderTasks.push_back_n(renderTasks.count(), renderTasks.begin());
Robert Phillips22310d62018-09-05 11:07:21 -0400103}
104
Greg Danielf41b2bd2019-08-22 16:19:24 -0400105void GrDrawingManager::RenderTaskDAG::swap(SkTArray<sk_sp<GrRenderTask>>* renderTasks) {
106 SkASSERT(renderTasks->empty());
107 renderTasks->swap(fRenderTasks);
Robert Phillips22310d62018-09-05 11:07:21 -0400108}
109
Chris Dalton6b498102019-08-01 14:14:52 -0600110void GrDrawingManager::RenderTaskDAG::prepForFlush() {
111 if (fSortRenderTasks) {
112 SkDEBUGCODE(bool result =) SkTTopoSort<GrRenderTask, GrRenderTask::TopoSortTraits>(
113 &fRenderTasks);
Robert Phillips22310d62018-09-05 11:07:21 -0400114 SkASSERT(result);
115 }
116
117#ifdef SK_DEBUG
Greg Danielf41b2bd2019-08-22 16:19:24 -0400118 // This block checks for any unnecessary splits in the opsTasks. If two sequential opsTasks
119 // share the same backing GrSurfaceProxy it means the opsTask was artificially split.
Chris Dalton6b498102019-08-01 14:14:52 -0600120 if (fRenderTasks.count()) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400121 GrOpsTask* prevOpsTask = fRenderTasks[0]->asOpsTask();
Chris Dalton6b498102019-08-01 14:14:52 -0600122 for (int i = 1; i < fRenderTasks.count(); ++i) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400123 GrOpsTask* curOpsTask = fRenderTasks[i]->asOpsTask();
Robert Phillips22310d62018-09-05 11:07:21 -0400124
Greg Danielf41b2bd2019-08-22 16:19:24 -0400125 if (prevOpsTask && curOpsTask) {
Adlai Holler33d569e2020-06-16 14:30:08 -0400126 SkASSERT(prevOpsTask->target(0).proxy() != curOpsTask->target(0).proxy());
Robert Phillips22310d62018-09-05 11:07:21 -0400127 }
128
Greg Danielf41b2bd2019-08-22 16:19:24 -0400129 prevOpsTask = curOpsTask;
Robert Phillips22310d62018-09-05 11:07:21 -0400130 }
131 }
132#endif
133}
134
Chris Dalton6b498102019-08-01 14:14:52 -0600135void GrDrawingManager::RenderTaskDAG::closeAll(const GrCaps* caps) {
Adlai Holler6f1487f2020-06-17 18:33:08 -0400136 for (auto& task : fRenderTasks) {
137 if (task) {
138 task->makeClosed(*caps);
Robert Phillips22310d62018-09-05 11:07:21 -0400139 }
140 }
141}
142
Robert Phillips22310d62018-09-05 11:07:21 -0400143///////////////////////////////////////////////////////////////////////////////////////////////////
Robert Phillips69893702019-02-22 11:16:30 -0500144GrDrawingManager::GrDrawingManager(GrRecordingContext* context,
Robert Phillips22310d62018-09-05 11:07:21 -0400145 const GrPathRendererChain::Options& optionsForPathRendererChain,
Chris Dalton6b498102019-08-01 14:14:52 -0600146 bool sortRenderTasks,
Greg Danielf41b2bd2019-08-22 16:19:24 -0400147 bool reduceOpsTaskSplitting)
Robert Phillips22310d62018-09-05 11:07:21 -0400148 : fContext(context)
149 , fOptionsForPathRendererChain(optionsForPathRendererChain)
Chris Dalton6b498102019-08-01 14:14:52 -0600150 , fDAG(sortRenderTasks)
Robert Phillips22310d62018-09-05 11:07:21 -0400151 , fPathRendererChain(nullptr)
152 , fSoftwarePathRenderer(nullptr)
Robert Phillips6db27c22019-05-01 10:43:56 -0400153 , fFlushing(false)
Herb Derby082232b2020-06-10 15:08:18 -0400154 , fReduceOpsTaskSplitting(reduceOpsTaskSplitting) { }
Robert Phillips22310d62018-09-05 11:07:21 -0400155
robertphillips3dc6ae52015-10-20 09:54:32 -0700156GrDrawingManager::~GrDrawingManager() {
Adlai Holler6f1487f2020-06-17 18:33:08 -0400157 fDAG.closeAll(fContext->priv().caps());
158 this->removeRenderTasks(0, fDAG.numRenderTasks());
robertphillips3dc6ae52015-10-20 09:54:32 -0700159}
160
Robert Phillipsa9162df2019-02-11 14:12:03 -0500161bool GrDrawingManager::wasAbandoned() const {
Robert Phillips6a6de562019-02-15 15:19:15 -0500162 return fContext->priv().abandoned();
robertphillips3dc6ae52015-10-20 09:54:32 -0700163}
164
robertphillips68737822015-10-29 12:12:21 -0700165void GrDrawingManager::freeGpuResources() {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400166 for (int i = fOnFlushCBObjects.count() - 1; i >= 0; --i) {
167 if (!fOnFlushCBObjects[i]->retainOnFreeGpuResources()) {
168 // it's safe to just do this because we're iterating in reverse
169 fOnFlushCBObjects.removeShuffle(i);
170 }
171 }
172
robertphillips68737822015-10-29 12:12:21 -0700173 // a path renderer may be holding onto resources
robertphillips13391dd2015-10-30 05:15:11 -0700174 fPathRendererChain = nullptr;
Ben Wagner9ec70c62018-07-12 13:30:47 -0400175 fSoftwarePathRenderer = nullptr;
Robert Phillipse3302df2017-04-24 07:31:02 -0400176}
177
Robert Phillips7ee385e2017-03-30 08:02:11 -0400178// MDB TODO: make use of the 'proxy' parameter.
Greg Daniel9efe3862020-06-11 11:51:06 -0400179bool GrDrawingManager::flush(
180 GrSurfaceProxy* proxies[],
181 int numProxies,
182 SkSurface::BackendSurfaceAccess access,
183 const GrFlushInfo& info,
184 const GrBackendSurfaceMutableState* newState) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400185 SkASSERT(numProxies >= 0);
186 SkASSERT(!numProxies || proxies);
Brian Salomon57d2beab2018-09-10 09:35:41 -0400187 GR_CREATE_TRACE_MARKER_CONTEXT("GrDrawingManager", "flush", fContext);
Brian Salomondcbb9d92017-07-19 10:53:20 -0400188
robertphillips7761d612016-05-16 09:14:53 -0700189 if (fFlushing || this->wasAbandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -0400190 if (info.fSubmittedProc) {
191 info.fSubmittedProc(info.fSubmittedContext, false);
192 }
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400193 if (info.fFinishedProc) {
194 info.fFinishedProc(info.fFinishedContext);
Greg Daniela3aa75a2019-04-12 14:24:55 -0400195 }
Greg Danielfe159622020-04-10 17:43:51 +0000196 return false;
joshualittb8918c42015-12-18 09:59:46 -0800197 }
Robert Phillips602df412019-04-08 11:10:39 -0400198
Robert Phillips38d64b02018-09-04 13:23:26 -0400199 SkDEBUGCODE(this->validate());
200
Greg Daniel797efca2019-05-09 14:04:20 -0400201 if (kNone_GrFlushFlags == info.fFlags && !info.fNumSemaphores && !info.fFinishedProc &&
Greg Daniel9efe3862020-06-11 11:51:06 -0400202 access == SkSurface::BackendSurfaceAccess::kNoAccess && !newState) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400203 bool canSkip = numProxies > 0;
204 for (int i = 0; i < numProxies && canSkip; ++i) {
205 canSkip = !fDAG.isUsed(proxies[i]) && !this->isDDLTarget(proxies[i]);
206 }
207 if (canSkip) {
Greg Daniel55822f12020-05-26 11:26:45 -0400208 if (info.fSubmittedProc) {
209 info.fSubmittedProc(info.fSubmittedContext, true);
210 }
Greg Danielfe159622020-04-10 17:43:51 +0000211 return false;
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400212 }
Robert Phillips9313aa72019-04-09 18:41:27 -0400213 }
214
Robert Phillips6a6de562019-02-15 15:19:15 -0500215 auto direct = fContext->priv().asDirectContext();
216 if (!direct) {
Greg Daniel55822f12020-05-26 11:26:45 -0400217 if (info.fSubmittedProc) {
218 info.fSubmittedProc(info.fSubmittedContext, false);
219 }
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400220 if (info.fFinishedProc) {
221 info.fFinishedProc(info.fFinishedContext);
Greg Daniela3aa75a2019-04-12 14:24:55 -0400222 }
Greg Danielfe159622020-04-10 17:43:51 +0000223 return false; // Can't flush while DDL recording
Robert Phillips6a6de562019-02-15 15:19:15 -0500224 }
Brian Salomon9241a6d2019-10-03 13:26:54 -0400225 direct->priv().clientMappedBufferManager()->process();
Robert Phillips6a6de562019-02-15 15:19:15 -0500226
227 GrGpu* gpu = direct->priv().getGpu();
Greg Daniel55822f12020-05-26 11:26:45 -0400228 // We have a non abandoned and direct GrContext. It must have a GrGpu.
229 SkASSERT(gpu);
Greg Daniela3aa75a2019-04-12 14:24:55 -0400230
joshualittb8918c42015-12-18 09:59:46 -0800231 fFlushing = true;
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400232
Robert Phillips6a6de562019-02-15 15:19:15 -0500233 auto resourceProvider = direct->priv().resourceProvider();
234 auto resourceCache = direct->priv().getResourceCache();
235
Chris Dalton6b498102019-08-01 14:14:52 -0600236 // Semi-usually the GrRenderTasks are already closed at this point, but sometimes Ganesh needs
Greg Danielf41b2bd2019-08-22 16:19:24 -0400237 // to flush mid-draw. In that case, the SkGpuDevice's opsTasks won't be closed but need to be
238 // flushed anyway. Closing such opsTasks here will mean new ones will be created to replace them
Chris Dalton6b498102019-08-01 14:14:52 -0600239 // if the SkGpuDevice(s) write to them again.
Robert Phillips9da87e02019-02-04 13:26:26 -0500240 fDAG.closeAll(fContext->priv().caps());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400241 fActiveOpsTask = nullptr;
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400242
Robert Phillips22310d62018-09-05 11:07:21 -0400243 fDAG.prepForFlush();
Brian Salomon601ac802019-02-07 13:37:16 -0500244 if (!fCpuBufferCache) {
245 // We cache more buffers when the backend is using client side arrays. Otherwise, we
246 // expect each pool will use a CPU buffer as a staging buffer before uploading to a GPU
247 // buffer object. Each pool only requires one staging buffer at a time.
248 int maxCachedBuffers = fContext->priv().caps()->preferClientSideDynamicBuffers() ? 2 : 6;
249 fCpuBufferCache = GrBufferAllocPool::CpuBufferCache::Make(maxCachedBuffers);
Brian Salomon58f153c2018-10-18 21:51:15 -0400250 }
Robert Phillipsa4c93ac2017-05-18 11:40:04 -0400251
Robert Phillipse5f73282019-06-18 17:15:04 -0400252 GrOpFlushState flushState(gpu, resourceProvider, &fTokenTracker, fCpuBufferCache);
Robert Phillips40a29d72018-01-18 12:59:22 -0500253
Chris Daltonfe199b72017-05-05 11:26:15 -0400254 GrOnFlushResourceProvider onFlushProvider(this);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500255 // TODO: AFAICT the only reason fFlushState is on GrDrawingManager rather than on the
256 // stack here is to preserve the flush tokens.
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400257
Chris Dalton12658942017-10-05 19:45:25 -0600258 // Prepare any onFlush op lists (e.g. atlases).
Chris Daltonfe199b72017-05-05 11:26:15 -0400259 if (!fOnFlushCBObjects.empty()) {
Chris Dalton6b498102019-08-01 14:14:52 -0600260 fDAG.gatherIDs(&fFlushingRenderTaskIDs);
Robert Phillips22310d62018-09-05 11:07:21 -0400261
Chris Daltonfe199b72017-05-05 11:26:15 -0400262 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
Chris Dalton6b498102019-08-01 14:14:52 -0600263 onFlushCBObject->preFlush(&onFlushProvider, fFlushingRenderTaskIDs.begin(),
Chris Daltonc4b47352019-08-23 10:10:36 -0600264 fFlushingRenderTaskIDs.count());
265 }
266 for (const auto& onFlushRenderTask : fOnFlushRenderTasks) {
267 onFlushRenderTask->makeClosed(*fContext->priv().caps());
Chris Dalton706a6ff2017-11-29 22:01:06 -0700268#ifdef SK_DEBUG
Chris Daltonc4b47352019-08-23 10:10:36 -0600269 // OnFlush callbacks are invoked during flush, and are therefore expected to handle
270 // resource allocation & usage on their own. (No deferred or lazy proxies!)
271 onFlushRenderTask->visitTargetAndSrcProxies_debugOnly(
272 [](GrSurfaceProxy* p, GrMipMapped mipMapped) {
273 SkASSERT(!p->asTextureProxy() || !p->asTextureProxy()->texPriv().isDeferred());
Brian Salomonbeb7f522019-08-30 16:19:42 -0400274 SkASSERT(!p->isLazy());
Chris Dalton4ece96d2019-08-30 11:26:39 -0600275 if (p->requiresManualMSAAResolve()) {
276 // The onFlush callback is responsible for ensuring MSAA gets resolved.
277 SkASSERT(p->asRenderTargetProxy() && !p->asRenderTargetProxy()->isMSAADirty());
278 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600279 if (GrMipMapped::kYes == mipMapped) {
280 // The onFlush callback is responsible for regenerating mips if needed.
281 SkASSERT(p->asTextureProxy() && !p->asTextureProxy()->mipMapsAreDirty());
282 }
283 });
Chris Dalton706a6ff2017-11-29 22:01:06 -0700284#endif
Chris Daltonc4b47352019-08-23 10:10:36 -0600285 onFlushRenderTask->prepare(&flushState);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400286 }
287 }
288
robertphillipsa13e2022015-11-11 12:01:09 -0800289#if 0
Brian Salomon09d994e2016-12-21 11:14:46 -0500290 // Enable this to print out verbose GrOp information
Chris Daltonc4b47352019-08-23 10:10:36 -0600291 SkDEBUGCODE(SkDebugf("onFlush renderTasks:"));
292 for (const auto& onFlushRenderTask : fOnFlushRenderTasks) {
293 SkDEBUGCODE(onFlushRenderTask->dump();)
294 }
295 SkDEBUGCODE(SkDebugf("Normal renderTasks:"));
Chris Dalton6b498102019-08-01 14:14:52 -0600296 for (int i = 0; i < fRenderTasks.count(); ++i) {
297 SkDEBUGCODE(fRenderTasks[i]->dump();)
robertphillips3dc6ae52015-10-20 09:54:32 -0700298 }
robertphillipsa13e2022015-11-11 12:01:09 -0800299#endif
300
Robert Phillipseafd48a2017-11-16 07:52:08 -0500301 int startIndex, stopIndex;
302 bool flushed = false;
303
Robert Phillipsf8e25022017-11-08 15:24:31 -0500304 {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400305 GrResourceAllocator alloc(resourceProvider SkDEBUGCODE(, fDAG.numRenderTasks()));
Chris Dalton6b498102019-08-01 14:14:52 -0600306 for (int i = 0; i < fDAG.numRenderTasks(); ++i) {
307 if (fDAG.renderTask(i)) {
308 fDAG.renderTask(i)->gatherProxyIntervals(&alloc);
Robert Phillips22310d62018-09-05 11:07:21 -0400309 }
Greg Danielf41b2bd2019-08-22 16:19:24 -0400310 alloc.markEndOfOpsTask(i);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500311 }
Robert Phillipsc73666f2019-04-24 08:49:48 -0400312 alloc.determineRecyclability();
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400313
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500314 GrResourceAllocator::AssignError error = GrResourceAllocator::AssignError::kNoError;
Chris Dalton6b498102019-08-01 14:14:52 -0600315 int numRenderTasksExecuted = 0;
Brian Salomon577aa0f2018-11-30 13:32:23 -0500316 while (alloc.assign(&startIndex, &stopIndex, &error)) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500317 if (GrResourceAllocator::AssignError::kFailedProxyInstantiation == error) {
318 for (int i = startIndex; i < stopIndex; ++i) {
Chris Dalton6b498102019-08-01 14:14:52 -0600319 GrRenderTask* renderTask = fDAG.renderTask(i);
320 if (!renderTask) {
321 continue;
Robert Phillips01a91282018-07-26 08:03:04 -0400322 }
Chris Dalton6b498102019-08-01 14:14:52 -0600323 if (!renderTask->isInstantiated()) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400324 // No need to call the renderTask's handleInternalAllocationFailure
325 // since we will already skip executing the renderTask since it is not
326 // instantiated.
Chris Dalton6b498102019-08-01 14:14:52 -0600327 continue;
Robert Phillipsbbcb7f72018-05-31 11:16:19 -0400328 }
Chris Dalton6b498102019-08-01 14:14:52 -0600329 renderTask->handleInternalAllocationFailure();
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500330 }
Adlai Holler6f1487f2020-06-17 18:33:08 -0400331 this->removeRenderTasks(startIndex, stopIndex);
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500332 }
Robert Phillips4150eea2018-02-07 17:08:21 -0500333
Chris Dalton6b498102019-08-01 14:14:52 -0600334 if (this->executeRenderTasks(
335 startIndex, stopIndex, &flushState, &numRenderTasksExecuted)) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500336 flushed = true;
337 }
bsalomondc438982016-08-31 11:53:49 -0700338 }
Greg Danielc42b20b2017-10-04 10:34:49 -0400339 }
340
Chris Dalton91ab1552018-04-18 13:24:25 -0600341#ifdef SK_DEBUG
Chris Dalton6b498102019-08-01 14:14:52 -0600342 for (int i = 0; i < fDAG.numRenderTasks(); ++i) {
Adlai Holler6f1487f2020-06-17 18:33:08 -0400343 // All render tasks should have been cleared out by now – we only reset the array below to
344 // reclaim storage.
345 SkASSERT(!fDAG.renderTask(i));
Chris Dalton91ab1552018-04-18 13:24:25 -0600346 }
347#endif
Adlai Holler25df1f72020-06-09 13:08:27 -0400348 fLastRenderTasks.reset();
Robert Phillips22310d62018-09-05 11:07:21 -0400349 fDAG.reset();
Robert Phillips15c91422019-05-07 16:54:48 -0400350 this->clearDDLTargets();
robertphillipsa13e2022015-11-11 12:01:09 -0800351
Robert Phillipsc994a932018-06-19 13:09:54 -0400352#ifdef SK_DEBUG
353 // In non-DDL mode this checks that all the flushed ops have been freed from the memory pool.
354 // When we move to partial flushes this assert will no longer be valid.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400355 // In DDL mode this check is somewhat superfluous since the memory for most of the ops/opsTasks
Robert Phillipsc994a932018-06-19 13:09:54 -0400356 // will be stored in the DDL's GrOpMemoryPools.
Robert Phillips9da87e02019-02-04 13:26:26 -0500357 GrOpMemoryPool* opMemoryPool = fContext->priv().opMemoryPool();
Robert Phillipsc994a932018-06-19 13:09:54 -0400358 opMemoryPool->isEmpty();
359#endif
360
Greg Daniel9efe3862020-06-11 11:51:06 -0400361 gpu->executeFlushInfo(proxies, numProxies, access, info, newState);
robertphillipsa13e2022015-11-11 12:01:09 -0800362
Brian Salomon57d2beab2018-09-10 09:35:41 -0400363 // Give the cache a chance to purge resources that become purgeable due to flushing.
364 if (flushed) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500365 resourceCache->purgeAsNeeded();
Brian Salomon876a0172019-03-08 11:12:14 -0500366 flushed = false;
bsalomonb77a9072016-09-07 10:02:04 -0700367 }
Chris Daltonfe199b72017-05-05 11:26:15 -0400368 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
Chris Dalton6b498102019-08-01 14:14:52 -0600369 onFlushCBObject->postFlush(fTokenTracker.nextTokenToFlush(), fFlushingRenderTaskIDs.begin(),
370 fFlushingRenderTaskIDs.count());
Brian Salomon876a0172019-03-08 11:12:14 -0500371 flushed = true;
372 }
373 if (flushed) {
374 resourceCache->purgeAsNeeded();
Chris Daltonfe199b72017-05-05 11:26:15 -0400375 }
Chris Dalton6b498102019-08-01 14:14:52 -0600376 fFlushingRenderTaskIDs.reset();
joshualittb8918c42015-12-18 09:59:46 -0800377 fFlushing = false;
Greg Daniel51316782017-08-02 15:10:09 +0000378
Greg Danielfe159622020-04-10 17:43:51 +0000379 return true;
380}
381
382bool GrDrawingManager::submitToGpu(bool syncToCpu) {
383 if (fFlushing || this->wasAbandoned()) {
384 return false;
385 }
386
387 auto direct = fContext->priv().asDirectContext();
388 if (!direct) {
389 return false; // Can't submit while DDL recording
390 }
391 GrGpu* gpu = direct->priv().getGpu();
392 return gpu->submitToGpu(syncToCpu);
robertphillips3dc6ae52015-10-20 09:54:32 -0700393}
394
Chris Dalton6b498102019-08-01 14:14:52 -0600395bool GrDrawingManager::executeRenderTasks(int startIndex, int stopIndex, GrOpFlushState* flushState,
396 int* numRenderTasksExecuted) {
397 SkASSERT(startIndex <= stopIndex && stopIndex <= fDAG.numRenderTasks());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500398
Robert Phillips27483912018-04-20 12:43:18 -0400399#if GR_FLUSH_TIME_OP_SPEW
Greg Danielf41b2bd2019-08-22 16:19:24 -0400400 SkDebugf("Flushing opsTask: %d to %d out of [%d, %d]\n",
Chris Dalton6b498102019-08-01 14:14:52 -0600401 startIndex, stopIndex, 0, fDAG.numRenderTasks());
Robert Phillips27483912018-04-20 12:43:18 -0400402 for (int i = startIndex; i < stopIndex; ++i) {
Chris Dalton6b498102019-08-01 14:14:52 -0600403 if (fDAG.renderTask(i)) {
404 fDAG.renderTask(i)->dump(true);
Robert Phillips1734dd32018-08-21 13:52:09 -0400405 }
Robert Phillips27483912018-04-20 12:43:18 -0400406 }
407#endif
408
Chris Dalton6b498102019-08-01 14:14:52 -0600409 bool anyRenderTasksExecuted = false;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500410
411 for (int i = startIndex; i < stopIndex; ++i) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400412 GrRenderTask* renderTask = fDAG.renderTask(i);
413 if (!renderTask || !renderTask->isInstantiated()) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500414 continue;
415 }
416
Chris Dalton6b498102019-08-01 14:14:52 -0600417 SkASSERT(renderTask->deferredProxiesAreInstantiated());
Robert Phillips22310d62018-09-05 11:07:21 -0400418
Chris Dalton6b498102019-08-01 14:14:52 -0600419 renderTask->prepare(flushState);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500420 }
421
422 // Upload all data to the GPU
423 flushState->preExecuteDraws();
424
Greg Danield2073452018-12-07 11:20:33 -0500425 // For Vulkan, if we have too many oplists to be flushed we end up allocating a lot of resources
426 // for each command buffer associated with the oplists. If this gets too large we can cause the
427 // devices to go OOM. In practice we usually only hit this case in our tests, but to be safe we
428 // put a cap on the number of oplists we will execute before flushing to the GPU to relieve some
429 // memory pressure.
Chris Dalton6b498102019-08-01 14:14:52 -0600430 static constexpr int kMaxRenderTasksBeforeFlush = 100;
Greg Danield2073452018-12-07 11:20:33 -0500431
Chris Daltonc4b47352019-08-23 10:10:36 -0600432 // Execute the onFlush renderTasks first, if any.
433 for (sk_sp<GrRenderTask>& onFlushRenderTask : fOnFlushRenderTasks) {
434 if (!onFlushRenderTask->execute(flushState)) {
435 SkDebugf("WARNING: onFlushRenderTask failed to execute.\n");
Robert Phillipseafd48a2017-11-16 07:52:08 -0500436 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600437 SkASSERT(onFlushRenderTask->unique());
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400438 onFlushRenderTask->disown(this);
Chris Daltonc4b47352019-08-23 10:10:36 -0600439 onFlushRenderTask = nullptr;
Chris Dalton6b498102019-08-01 14:14:52 -0600440 (*numRenderTasksExecuted)++;
441 if (*numRenderTasksExecuted >= kMaxRenderTasksBeforeFlush) {
Greg Danielfe159622020-04-10 17:43:51 +0000442 flushState->gpu()->submitToGpu(false);
Chris Dalton6b498102019-08-01 14:14:52 -0600443 *numRenderTasksExecuted = 0;
Greg Danield2073452018-12-07 11:20:33 -0500444 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500445 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600446 fOnFlushRenderTasks.reset();
Robert Phillipseafd48a2017-11-16 07:52:08 -0500447
448 // Execute the normal op lists.
449 for (int i = startIndex; i < stopIndex; ++i) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400450 GrRenderTask* renderTask = fDAG.renderTask(i);
451 if (!renderTask || !renderTask->isInstantiated()) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500452 continue;
453 }
454
Greg Daniel15ecdf92019-08-30 15:35:23 -0400455 if (renderTask->execute(flushState)) {
Chris Dalton6b498102019-08-01 14:14:52 -0600456 anyRenderTasksExecuted = true;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500457 }
Chris Dalton6b498102019-08-01 14:14:52 -0600458 (*numRenderTasksExecuted)++;
459 if (*numRenderTasksExecuted >= kMaxRenderTasksBeforeFlush) {
Greg Danielfe159622020-04-10 17:43:51 +0000460 flushState->gpu()->submitToGpu(false);
Chris Dalton6b498102019-08-01 14:14:52 -0600461 *numRenderTasksExecuted = 0;
Greg Danield2073452018-12-07 11:20:33 -0500462 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500463 }
464
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400465 SkASSERT(!flushState->opsRenderPass());
Robert Phillips40a29d72018-01-18 12:59:22 -0500466 SkASSERT(fTokenTracker.nextDrawToken() == fTokenTracker.nextTokenToFlush());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500467
Chris Dalton6b498102019-08-01 14:14:52 -0600468 // We reset the flush state before the RenderTasks so that the last resources to be freed are
469 // those that are written to in the RenderTasks. This helps to make sure the most recently used
470 // resources are the last to be purged by the resource cache.
Robert Phillipseafd48a2017-11-16 07:52:08 -0500471 flushState->reset();
472
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400473 this->removeRenderTasks(startIndex, stopIndex);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500474
Chris Dalton6b498102019-08-01 14:14:52 -0600475 return anyRenderTasksExecuted;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500476}
477
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400478void GrDrawingManager::removeRenderTasks(int startIndex, int stopIndex) {
479 for (int i = startIndex; i < stopIndex; ++i) {
480 GrRenderTask* task = fDAG.renderTask(i);
481 if (!task) {
482 continue;
483 }
484 if (!task->unique()) {
485 // TODO: Eventually this should be guaranteed unique: http://skbug.com/7111
486 task->endFlush(this);
487 }
488 task->disown(this);
489 }
Adlai Holler6f1487f2020-06-17 18:33:08 -0400490 fDAG.rawRemoveRenderTasks(startIndex, stopIndex);
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400491}
492
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400493static void resolve_and_mipmap(GrGpu* gpu, GrSurfaceProxy* proxy) {
494 if (!proxy->isInstantiated()) {
495 return;
496 }
497
498 // In the flushSurfaces case, we need to resolve MSAA immediately after flush. This is
499 // because clients expect the flushed surface's backing texture to be fully resolved
500 // upon return.
501 if (proxy->requiresManualMSAAResolve()) {
502 auto* rtProxy = proxy->asRenderTargetProxy();
503 SkASSERT(rtProxy);
504 if (rtProxy->isMSAADirty()) {
505 SkASSERT(rtProxy->peekRenderTarget());
506 gpu->resolveRenderTarget(rtProxy->peekRenderTarget(), rtProxy->msaaDirtyRect(),
507 GrGpu::ForExternalIO::kYes);
508 rtProxy->markMSAAResolved();
509 }
510 }
511 // If, after a flush, any of the proxies of interest have dirty mipmaps, regenerate them in
512 // case their backend textures are being stolen.
513 // (This special case is exercised by the ReimportImageTextureWithMipLevels test.)
514 // FIXME: It may be more ideal to plumb down a "we're going to steal the backends" flag.
515 if (auto* textureProxy = proxy->asTextureProxy()) {
516 if (textureProxy->mipMapsAreDirty()) {
517 SkASSERT(textureProxy->peekTexture());
518 gpu->regenerateMipMapLevels(textureProxy->peekTexture());
519 textureProxy->markMipMapsClean();
520 }
521 }
522}
523
Greg Daniel9efe3862020-06-11 11:51:06 -0400524GrSemaphoresSubmitted GrDrawingManager::flushSurfaces(
525 GrSurfaceProxy* proxies[],
526 int numProxies,
527 SkSurface::BackendSurfaceAccess access,
528 const GrFlushInfo& info,
529 const GrBackendSurfaceMutableState* newState) {
bsalomon6a2b1942016-09-08 11:28:59 -0700530 if (this->wasAbandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -0400531 if (info.fSubmittedProc) {
532 info.fSubmittedProc(info.fSubmittedContext, false);
533 }
534 if (info.fFinishedProc) {
535 info.fFinishedProc(info.fFinishedContext);
536 }
Greg Daniel51316782017-08-02 15:10:09 +0000537 return GrSemaphoresSubmitted::kNo;
bsalomon6a2b1942016-09-08 11:28:59 -0700538 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400539 SkDEBUGCODE(this->validate());
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400540 SkASSERT(numProxies >= 0);
541 SkASSERT(!numProxies || proxies);
bsalomon6a2b1942016-09-08 11:28:59 -0700542
Robert Phillips6a6de562019-02-15 15:19:15 -0500543 auto direct = fContext->priv().asDirectContext();
544 if (!direct) {
Greg Daniel55822f12020-05-26 11:26:45 -0400545 if (info.fSubmittedProc) {
546 info.fSubmittedProc(info.fSubmittedContext, false);
547 }
548 if (info.fFinishedProc) {
549 info.fFinishedProc(info.fFinishedContext);
550 }
Robert Phillips6a6de562019-02-15 15:19:15 -0500551 return GrSemaphoresSubmitted::kNo; // Can't flush while DDL recording
552 }
553
554 GrGpu* gpu = direct->priv().getGpu();
Greg Daniel55822f12020-05-26 11:26:45 -0400555 // We have a non abandoned and direct GrContext. It must have a GrGpu.
556 SkASSERT(gpu);
Robert Phillips874b5352018-03-16 08:48:24 -0400557
Robert Phillipsacc10fa2019-04-01 09:50:20 -0400558 // TODO: It is important to upgrade the drawingmanager to just flushing the
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400559 // portion of the DAG required by 'proxies' in order to restore some of the
Robert Phillipsacc10fa2019-04-01 09:50:20 -0400560 // semantics of this method.
Greg Daniel9efe3862020-06-11 11:51:06 -0400561 bool didFlush = this->flush(proxies, numProxies, access, info, newState);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400562 for (int i = 0; i < numProxies; ++i) {
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400563 resolve_and_mipmap(gpu, proxies[i]);
bsalomon6a2b1942016-09-08 11:28:59 -0700564 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400565
566 SkDEBUGCODE(this->validate());
Greg Danielfe159622020-04-10 17:43:51 +0000567
Greg Daniel04283f32020-05-20 13:16:00 -0400568 if (!didFlush || (!direct->priv().caps()->semaphoreSupport() && info.fNumSemaphores)) {
Greg Danielfe159622020-04-10 17:43:51 +0000569 return GrSemaphoresSubmitted::kNo;
570 }
571 return GrSemaphoresSubmitted::kYes;
bsalomon6a2b1942016-09-08 11:28:59 -0700572}
573
Chris Daltonfe199b72017-05-05 11:26:15 -0400574void GrDrawingManager::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
575 fOnFlushCBObjects.push_back(onFlushCBObject);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400576}
577
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500578#if GR_TEST_UTILS
579void GrDrawingManager::testingOnly_removeOnFlushCallbackObject(GrOnFlushCallbackObject* cb) {
580 int n = std::find(fOnFlushCBObjects.begin(), fOnFlushCBObjects.end(), cb) -
581 fOnFlushCBObjects.begin();
582 SkASSERT(n < fOnFlushCBObjects.count());
583 fOnFlushCBObjects.removeShuffle(n);
584}
585#endif
586
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400587void GrDrawingManager::setLastRenderTask(const GrSurfaceProxy* proxy, GrRenderTask* task) {
588#ifdef SK_DEBUG
589 if (GrRenderTask* prior = this->getLastRenderTask(proxy)) {
590 SkASSERT(prior->isClosed());
591 }
592#endif
Adlai Holler25df1f72020-06-09 13:08:27 -0400593 uint32_t key = proxy->uniqueID().asUInt();
594 if (task) {
595 fLastRenderTasks.set(key, task);
596 } else if (fLastRenderTasks.find(key)) {
597 fLastRenderTasks.remove(key);
598 }
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400599}
600
601GrRenderTask* GrDrawingManager::getLastRenderTask(const GrSurfaceProxy* proxy) const {
602 auto entry = fLastRenderTasks.find(proxy->uniqueID().asUInt());
603 return entry ? *entry : nullptr;
604}
605
606GrOpsTask* GrDrawingManager::getLastOpsTask(const GrSurfaceProxy* proxy) const {
607 GrRenderTask* task = this->getLastRenderTask(proxy);
608 return task ? task->asOpsTask() : nullptr;
609}
610
611
Chris Dalton6b498102019-08-01 14:14:52 -0600612void GrDrawingManager::moveRenderTasksToDDL(SkDeferredDisplayList* ddl) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400613 SkDEBUGCODE(this->validate());
614
Chris Dalton6b498102019-08-01 14:14:52 -0600615 // no renderTask should receive a new command after this
Robert Phillips9da87e02019-02-04 13:26:26 -0500616 fDAG.closeAll(fContext->priv().caps());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400617 fActiveOpsTask = nullptr;
Robert Phillips7a137052018-02-01 11:23:12 -0500618
Chris Dalton6b498102019-08-01 14:14:52 -0600619 fDAG.swap(&ddl->fRenderTasks);
Robert Phillips19f466d2020-02-26 10:27:07 -0500620 SkASSERT(!fDAG.numRenderTasks());
Robert Phillips867ce8f2018-06-21 10:28:36 -0400621
Robert Phillips19f466d2020-02-26 10:27:07 -0500622 for (auto& renderTask : ddl->fRenderTasks) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400623 renderTask->disown(this);
Robert Phillips29f38542019-10-16 09:20:25 -0400624 renderTask->prePrepare(fContext);
Robert Phillips7327c9d2019-10-08 16:32:56 -0400625 }
626
Michael Ludwig2c316bd2019-12-19 14:50:44 -0500627 ddl->fArenas = std::move(fContext->priv().detachArenas());
Robert Phillips61fc7992019-10-22 11:58:17 -0400628
Robert Phillipsf6a0b452020-02-18 14:26:46 -0500629 fContext->priv().detachProgramData(&ddl->fProgramData);
Robert Phillips576b6a12019-12-06 13:05:49 -0500630
Robert Phillips774168e2018-05-31 12:43:27 -0400631 if (fPathRendererChain) {
632 if (auto ccpr = fPathRendererChain->getCoverageCountingPathRenderer()) {
633 ddl->fPendingPaths = ccpr->detachPendingPaths();
634 }
635 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400636
637 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500638}
639
Chris Dalton6b498102019-08-01 14:14:52 -0600640void GrDrawingManager::copyRenderTasksFromDDL(const SkDeferredDisplayList* ddl,
Robert Phillips933484f2019-11-26 09:38:55 -0500641 GrRenderTargetProxy* newDest) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400642 SkDEBUGCODE(this->validate());
643
Greg Danielf41b2bd2019-08-22 16:19:24 -0400644 if (fActiveOpsTask) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400645 // This is a temporary fix for the partial-MDB world. In that world we're not
Greg Danielf41b2bd2019-08-22 16:19:24 -0400646 // reordering so ops that (in the single opsTask world) would've just glommed onto the
647 // end of the single opsTask but referred to a far earlier RT need to appear in their
648 // own opsTask.
649 fActiveOpsTask->makeClosed(*fContext->priv().caps());
650 fActiveOpsTask = nullptr;
Robert Phillips38d64b02018-09-04 13:23:26 -0400651 }
652
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400653 // Propagate the DDL proxy's state information to the replaying DDL.
654 if (ddl->priv().targetProxy()->isMSAADirty()) {
655 newDest->markMSAADirty(ddl->priv().targetProxy()->msaaDirtyRect(),
656 ddl->characterization().origin());
657 }
658 GrTextureProxy* newTextureProxy = newDest->asTextureProxy();
659 if (newTextureProxy && GrMipMapped::kYes == newTextureProxy->mipMapped()) {
660 newTextureProxy->markMipMapsDirty();
661 }
662
663 this->addDDLTarget(newDest, ddl->priv().targetProxy());
Robert Phillips15c91422019-05-07 16:54:48 -0400664
Robert Phillips62000362018-02-01 09:10:04 -0500665 // Here we jam the proxy that backs the current replay SkSurface into the LazyProxyData.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400666 // The lazy proxy that references it (in the copied opsTasks) will steal its GrTexture.
Robert Phillips62000362018-02-01 09:10:04 -0500667 ddl->fLazyProxyData->fReplayDest = newDest;
Robert Phillips774168e2018-05-31 12:43:27 -0400668
669 if (ddl->fPendingPaths.size()) {
670 GrCoverageCountingPathRenderer* ccpr = this->getCoverageCountingPathRenderer();
671
672 ccpr->mergePendingPaths(ddl->fPendingPaths);
673 }
Robert Phillips22310d62018-09-05 11:07:21 -0400674
Chris Dalton6b498102019-08-01 14:14:52 -0600675 fDAG.add(ddl->fRenderTasks);
Robert Phillips38d64b02018-09-04 13:23:26 -0400676
677 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500678}
679
Robert Phillips38d64b02018-09-04 13:23:26 -0400680#ifdef SK_DEBUG
681void GrDrawingManager::validate() const {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400682 if (fDAG.sortingRenderTasks() && fReduceOpsTaskSplitting) {
683 SkASSERT(!fActiveOpsTask);
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400684 } else {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400685 if (fActiveOpsTask) {
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400686 SkASSERT(!fDAG.empty());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400687 SkASSERT(!fActiveOpsTask->isClosed());
688 SkASSERT(fActiveOpsTask == fDAG.back());
Robert Phillips38d64b02018-09-04 13:23:26 -0400689 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400690
Chris Dalton6b498102019-08-01 14:14:52 -0600691 for (int i = 0; i < fDAG.numRenderTasks(); ++i) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400692 if (fActiveOpsTask != fDAG.renderTask(i)) {
Chris Daltone2a903e2019-09-18 13:41:50 -0600693 // The resolveTask associated with the activeTask remains open for as long as the
694 // activeTask does.
695 bool isActiveResolveTask =
696 fActiveOpsTask && fActiveOpsTask->fTextureResolveTask == fDAG.renderTask(i);
697 SkASSERT(isActiveResolveTask || fDAG.renderTask(i)->isClosed());
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400698 }
699 }
700
701 if (!fDAG.empty() && !fDAG.back()->isClosed()) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400702 SkASSERT(fActiveOpsTask == fDAG.back());
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400703 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400704 }
705}
706#endif
707
Greg Danielbbfec9d2019-08-20 10:56:51 -0400708void GrDrawingManager::closeRenderTasksForNewRenderTask(GrSurfaceProxy* target) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400709 if (target && fDAG.sortingRenderTasks() && fReduceOpsTaskSplitting) {
Chris Dalton5fe99772019-08-06 11:57:39 -0600710 // In this case we need to close all the renderTasks that rely on the current contents of
711 // 'target'. That is bc we're going to update the content of the proxy so they need to be
712 // split in case they use both the old and new content. (This is a bit of an overkill: they
713 // really only need to be split if they ever reference proxy's contents again but that is
714 // hard to predict/handle).
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400715 if (GrRenderTask* lastRenderTask = this->getLastRenderTask(target)) {
Chris Dalton6b498102019-08-01 14:14:52 -0600716 lastRenderTask->closeThoseWhoDependOnMe(*fContext->priv().caps());
Robert Phillips46acf9d2018-10-09 09:31:40 -0400717 }
Greg Danielf41b2bd2019-08-22 16:19:24 -0400718 } else if (fActiveOpsTask) {
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400719 // This is a temporary fix for the partial-MDB world. In that world we're not
Greg Danielf41b2bd2019-08-22 16:19:24 -0400720 // reordering so ops that (in the single opsTask world) would've just glommed onto the
721 // end of the single opsTask but referred to a far earlier RT need to appear in their
722 // own opsTask.
723 fActiveOpsTask->makeClosed(*fContext->priv().caps());
724 fActiveOpsTask = nullptr;
robertphillips3dc6ae52015-10-20 09:54:32 -0700725 }
Chris Dalton5fe99772019-08-06 11:57:39 -0600726}
727
Greg Daniel16f5c652019-10-29 11:26:01 -0400728sk_sp<GrOpsTask> GrDrawingManager::newOpsTask(GrSurfaceProxyView surfaceView,
729 bool managedOpsTask) {
Chris Dalton5fe99772019-08-06 11:57:39 -0600730 SkDEBUGCODE(this->validate());
731 SkASSERT(fContext);
732
Greg Daniel16f5c652019-10-29 11:26:01 -0400733 GrSurfaceProxy* proxy = surfaceView.proxy();
734 this->closeRenderTasksForNewRenderTask(proxy);
robertphillips3dc6ae52015-10-20 09:54:32 -0700735
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400736 sk_sp<GrOpsTask> opsTask(new GrOpsTask(this, fContext->priv().arenas(),
Michael Ludwigd0840ec2019-12-12 09:48:38 -0500737 std::move(surfaceView),
738 fContext->priv().auditTrail()));
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400739 SkASSERT(this->getLastRenderTask(proxy) == opsTask.get());
robertphillips3dc6ae52015-10-20 09:54:32 -0700740
Greg Danielf41b2bd2019-08-22 16:19:24 -0400741 if (managedOpsTask) {
742 fDAG.add(opsTask);
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400743
Greg Danielf41b2bd2019-08-22 16:19:24 -0400744 if (!fDAG.sortingRenderTasks() || !fReduceOpsTaskSplitting) {
745 fActiveOpsTask = opsTask.get();
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400746 }
Robert Phillips941d1442017-06-14 16:37:02 -0400747 }
robertphillips3dc6ae52015-10-20 09:54:32 -0700748
Robert Phillips38d64b02018-09-04 13:23:26 -0400749 SkDEBUGCODE(this->validate());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400750 return opsTask;
robertphillips3dc6ae52015-10-20 09:54:32 -0700751}
752
Chris Daltone2a903e2019-09-18 13:41:50 -0600753GrTextureResolveRenderTask* GrDrawingManager::newTextureResolveRenderTask(const GrCaps& caps) {
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600754 // 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 -0600755 // in sorting and opsTask reduction mode) the render tasks that depend on any proxy's current
Greg Danielf41b2bd2019-08-22 16:19:24 -0400756 // state. This is because those opsTasks can still receive new ops and because if they refer to
Chris Dalton4ece96d2019-08-30 11:26:39 -0600757 // the mipmapped version of 'proxy', they will then come to depend on the render task being
758 // created here.
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600759 //
Greg Danielf41b2bd2019-08-22 16:19:24 -0400760 // Add the new textureResolveTask before the fActiveOpsTask (if not in
761 // sorting/opsTask-splitting-reduction mode) because it will depend upon this resolve task.
Chris Dalton3d770272019-08-14 09:24:37 -0600762 // NOTE: Putting it here will also reduce the amount of work required by the topological sort.
Chris Daltone2a903e2019-09-18 13:41:50 -0600763 return static_cast<GrTextureResolveRenderTask*>(fDAG.addBeforeLast(
764 sk_make_sp<GrTextureResolveRenderTask>()));
Chris Dalton3d770272019-08-14 09:24:37 -0600765}
766
Greg Danielc30f1a92019-09-06 15:28:58 -0400767void GrDrawingManager::newWaitRenderTask(sk_sp<GrSurfaceProxy> proxy,
Greg Daniel301015c2019-11-18 14:06:46 -0500768 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> semaphores,
Greg Danielc30f1a92019-09-06 15:28:58 -0400769 int numSemaphores) {
770 SkDEBUGCODE(this->validate());
771 SkASSERT(fContext);
772
773 const GrCaps& caps = *fContext->priv().caps();
774
Greg Daniel16f5c652019-10-29 11:26:01 -0400775 sk_sp<GrWaitRenderTask> waitTask = sk_make_sp<GrWaitRenderTask>(GrSurfaceProxyView(proxy),
776 std::move(semaphores),
Greg Danielc30f1a92019-09-06 15:28:58 -0400777 numSemaphores);
778 if (fReduceOpsTaskSplitting) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400779 GrRenderTask* lastTask = this->getLastRenderTask(proxy.get());
Greg Danielc30f1a92019-09-06 15:28:58 -0400780 if (lastTask && !lastTask->isClosed()) {
781 // We directly make the currently open renderTask depend on waitTask instead of using
782 // the proxy version of addDependency. The waitTask will never need to trigger any
783 // resolves or mip map generation which is the main advantage of going through the proxy
784 // version. Additionally we would've had to temporarily set the wait task as the
785 // lastRenderTask on the proxy, add the dependency, and then reset the lastRenderTask to
786 // lastTask. Additionally we add all dependencies of lastTask to waitTask so that the
787 // waitTask doesn't get reordered before them and unnecessarily block those tasks.
788 // Note: Any previous Ops already in lastTask will get blocked by the wait semaphore
789 // even though they don't need to be for correctness.
790
791 // Make sure we add the dependencies of lastTask to waitTask first or else we'll get a
792 // circular self dependency of waitTask on waitTask.
793 waitTask->addDependenciesFromOtherTask(lastTask);
794 lastTask->addDependency(waitTask.get());
795 } else {
796 // If there is a last task we set the waitTask to depend on it so that it doesn't get
797 // reordered in front of the lastTask causing the lastTask to be blocked by the
798 // semaphore. Again we directly just go through adding the dependency to the task and
799 // not the proxy since we don't need to worry about resolving anything.
800 if (lastTask) {
801 waitTask->addDependency(lastTask);
802 }
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400803 this->setLastRenderTask(proxy.get(), waitTask.get());
Greg Danielc30f1a92019-09-06 15:28:58 -0400804 }
805 fDAG.add(waitTask);
806 } else {
Adlai Holler33d569e2020-06-16 14:30:08 -0400807 if (fActiveOpsTask && (fActiveOpsTask->target(0).proxy() == proxy.get())) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400808 SkASSERT(this->getLastRenderTask(proxy.get()) == fActiveOpsTask);
Greg Danielc30f1a92019-09-06 15:28:58 -0400809 fDAG.addBeforeLast(waitTask);
810 // In this case we keep the current renderTask open but just insert the new waitTask
811 // before it in the list. The waitTask will never need to trigger any resolves or mip
812 // map generation which is the main advantage of going through the proxy version.
813 // Additionally we would've had to temporarily set the wait task as the lastRenderTask
814 // on the proxy, add the dependency, and then reset the lastRenderTask to
815 // fActiveOpsTask. Additionally we make the waitTask depend on all of fActiveOpsTask
816 // dependencies so that we don't unnecessarily reorder the waitTask before them.
817 // Note: Any previous Ops already in fActiveOpsTask will get blocked by the wait
818 // semaphore even though they don't need to be for correctness.
819
820 // Make sure we add the dependencies of fActiveOpsTask to waitTask first or else we'll
821 // get a circular self dependency of waitTask on waitTask.
822 waitTask->addDependenciesFromOtherTask(fActiveOpsTask);
823 fActiveOpsTask->addDependency(waitTask.get());
824 } else {
825 // In this case we just close the previous RenderTask and start and append the waitTask
826 // to the DAG. Since it is the last task now we call setLastRenderTask on the proxy. If
827 // there is a lastTask on the proxy we make waitTask depend on that task. This
828 // dependency isn't strictly needed but it does keep the DAG from reordering the
829 // waitTask earlier and blocking more tasks.
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400830 if (GrRenderTask* lastTask = this->getLastRenderTask(proxy.get())) {
Greg Danielc30f1a92019-09-06 15:28:58 -0400831 waitTask->addDependency(lastTask);
832 }
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400833 this->setLastRenderTask(proxy.get(), waitTask.get());
Greg Danielc30f1a92019-09-06 15:28:58 -0400834 this->closeRenderTasksForNewRenderTask(proxy.get());
835 fDAG.add(waitTask);
836 }
837 }
838 waitTask->makeClosed(caps);
839
840 SkDEBUGCODE(this->validate());
841}
842
Greg Danielbbfec9d2019-08-20 10:56:51 -0400843void GrDrawingManager::newTransferFromRenderTask(sk_sp<GrSurfaceProxy> srcProxy,
844 const SkIRect& srcRect,
845 GrColorType surfaceColorType,
846 GrColorType dstColorType,
847 sk_sp<GrGpuBuffer> dstBuffer,
848 size_t dstOffset) {
849 SkDEBUGCODE(this->validate());
850 SkASSERT(fContext);
851 // This copies from srcProxy to dstBuffer so it doesn't have a real target.
852 this->closeRenderTasksForNewRenderTask(nullptr);
853
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600854 GrRenderTask* task = fDAG.add(sk_make_sp<GrTransferFromRenderTask>(
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400855 srcProxy, srcRect, surfaceColorType, dstColorType,
856 std::move(dstBuffer), dstOffset));
Greg Danielbbfec9d2019-08-20 10:56:51 -0400857
858 const GrCaps& caps = *fContext->priv().caps();
859
860 // We always say GrMipMapped::kNo here since we are always just copying from the base layer. We
861 // don't need to make sure the whole mip map chain is valid.
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400862 task->addDependency(this, srcProxy.get(), GrMipMapped::kNo,
863 GrTextureResolveManager(this), caps);
Greg Danielbbfec9d2019-08-20 10:56:51 -0400864 task->makeClosed(caps);
865
Greg Danielbbfec9d2019-08-20 10:56:51 -0400866 // We have closed the previous active oplist but since a new oplist isn't being added there
867 // shouldn't be an active one.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400868 SkASSERT(!fActiveOpsTask);
Greg Danielbbfec9d2019-08-20 10:56:51 -0400869 SkDEBUGCODE(this->validate());
870}
871
Greg Daniel16f5c652019-10-29 11:26:01 -0400872bool GrDrawingManager::newCopyRenderTask(GrSurfaceProxyView srcView,
Greg Daniele227fe42019-08-21 13:52:24 -0400873 const SkIRect& srcRect,
Greg Daniel16f5c652019-10-29 11:26:01 -0400874 GrSurfaceProxyView dstView,
Greg Daniele227fe42019-08-21 13:52:24 -0400875 const SkIPoint& dstPoint) {
876 SkDEBUGCODE(this->validate());
877 SkASSERT(fContext);
Greg Daniele227fe42019-08-21 13:52:24 -0400878
Greg Daniel16f5c652019-10-29 11:26:01 -0400879 this->closeRenderTasksForNewRenderTask(dstView.proxy());
Brian Salomone4bce012019-09-20 15:34:23 -0400880 const GrCaps& caps = *fContext->priv().caps();
881
Greg Daniel16f5c652019-10-29 11:26:01 -0400882 GrSurfaceProxy* srcProxy = srcView.proxy();
883
Brian Salomone4bce012019-09-20 15:34:23 -0400884 GrRenderTask* task =
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400885 fDAG.add(GrCopyRenderTask::Make(this, std::move(srcView), srcRect, std::move(dstView),
Greg Daniel16f5c652019-10-29 11:26:01 -0400886 dstPoint, &caps));
Greg Daniele227fe42019-08-21 13:52:24 -0400887 if (!task) {
888 return false;
889 }
890
Greg Daniele227fe42019-08-21 13:52:24 -0400891 // We always say GrMipMapped::kNo here since we are always just copying from the base layer to
892 // another base layer. We don't need to make sure the whole mip map chain is valid.
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400893 task->addDependency(this, srcProxy, GrMipMapped::kNo, GrTextureResolveManager(this), caps);
Greg Daniele227fe42019-08-21 13:52:24 -0400894 task->makeClosed(caps);
895
Greg Daniele227fe42019-08-21 13:52:24 -0400896 // We have closed the previous active oplist but since a new oplist isn't being added there
897 // shouldn't be an active one.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400898 SkASSERT(!fActiveOpsTask);
Greg Daniele227fe42019-08-21 13:52:24 -0400899 SkDEBUGCODE(this->validate());
900 return true;
901}
902
robertphillips68737822015-10-29 12:12:21 -0700903/*
904 * This method finds a path renderer that can draw the specified path on
905 * the provided target.
906 * Due to its expense, the software path renderer has split out so it can
907 * can be individually allowed/disallowed via the "allowSW" boolean.
908 */
909GrPathRenderer* GrDrawingManager::getPathRenderer(const GrPathRenderer::CanDrawPathArgs& args,
910 bool allowSW,
911 GrPathRendererChain::DrawType drawType,
912 GrPathRenderer::StencilSupport* stencilSupport) {
913
914 if (!fPathRendererChain) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400915 fPathRendererChain.reset(new GrPathRendererChain(fContext, fOptionsForPathRendererChain));
robertphillips68737822015-10-29 12:12:21 -0700916 }
917
918 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(args, drawType, stencilSupport);
919 if (!pr && allowSW) {
Brian Salomone7df0bb2018-05-07 14:44:57 -0400920 auto swPR = this->getSoftwarePathRenderer();
921 if (GrPathRenderer::CanDrawPath::kNo != swPR->canDrawPath(args)) {
922 pr = swPR;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500923 }
robertphillips68737822015-10-29 12:12:21 -0700924 }
925
Robert Phillipsd81379d2020-04-21 10:39:02 -0400926#if GR_PATH_RENDERER_SPEW
927 if (pr) {
928 SkDebugf("getPathRenderer: %s\n", pr->name());
929 }
930#endif
931
robertphillips68737822015-10-29 12:12:21 -0700932 return pr;
933}
934
Brian Salomone7df0bb2018-05-07 14:44:57 -0400935GrPathRenderer* GrDrawingManager::getSoftwarePathRenderer() {
936 if (!fSoftwarePathRenderer) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400937 fSoftwarePathRenderer.reset(
Robert Phillips9da87e02019-02-04 13:26:26 -0500938 new GrSoftwarePathRenderer(fContext->priv().proxyProvider(),
Ben Wagner9ec70c62018-07-12 13:30:47 -0400939 fOptionsForPathRendererChain.fAllowPathMaskCaching));
Brian Salomone7df0bb2018-05-07 14:44:57 -0400940 }
Ben Wagner9ec70c62018-07-12 13:30:47 -0400941 return fSoftwarePathRenderer.get();
Brian Salomone7df0bb2018-05-07 14:44:57 -0400942}
943
Chris Daltonfddb6c02017-11-04 15:22:22 -0600944GrCoverageCountingPathRenderer* GrDrawingManager::getCoverageCountingPathRenderer() {
945 if (!fPathRendererChain) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400946 fPathRendererChain.reset(new GrPathRendererChain(fContext, fOptionsForPathRendererChain));
Chris Daltonfddb6c02017-11-04 15:22:22 -0600947 }
948 return fPathRendererChain->getCoverageCountingPathRenderer();
949}
950
Brian Salomon653f42f2018-07-10 10:07:31 -0400951void GrDrawingManager::flushIfNecessary() {
Robert Phillips6a6de562019-02-15 15:19:15 -0500952 auto direct = fContext->priv().asDirectContext();
953 if (!direct) {
954 return;
955 }
956
957 auto resourceCache = direct->priv().getResourceCache();
Brian Salomon653f42f2018-07-10 10:07:31 -0400958 if (resourceCache && resourceCache->requestsFlush()) {
Greg Daniel9efe3862020-06-11 11:51:06 -0400959 if (this->flush(nullptr, 0, SkSurface::BackendSurfaceAccess::kNoAccess, GrFlushInfo(),
960 nullptr)) {
Greg Danielfe159622020-04-10 17:43:51 +0000961 this->submitToGpu(false);
962 }
Brian Salomon57d2beab2018-09-10 09:35:41 -0400963 resourceCache->purgeAsNeeded();
Brian Salomon653f42f2018-07-10 10:07:31 -0400964 }
965}
966