blob: bc980e70d19cf8a30b05d87225cb8fb391f7e290 [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
Adlai Hollerc2bfcff2020-11-06 15:39:36 -050010#include <algorithm>
John Stilesfbd050b2020-08-03 13:21:46 -040011#include <memory>
12
Robert Phillips4d5594d2020-02-21 14:24:40 -050013#include "include/core/SkDeferredDisplayList.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/gpu/GrBackendSemaphore.h"
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040015#include "include/gpu/GrDirectContext.h"
16#include "include/gpu/GrRecordingContext.h"
Robert Phillips7eb0a5f2020-06-09 14:15:09 -040017#include "src/core/SkDeferredDisplayListPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/core/SkTTopoSort.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040019#include "src/gpu/GrAuditTrail.h"
Brian Salomon9241a6d2019-10-03 13:26:54 -040020#include "src/gpu/GrClientMappedBufferManager.h"
Greg Daniele227fe42019-08-21 13:52:24 -040021#include "src/gpu/GrCopyRenderTask.h"
Robert Phillips02dd0ed2020-11-09 17:03:34 -050022#include "src/gpu/GrDDLTask.h"
Adlai Hollera0693042020-10-14 11:23:11 -040023#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/GrGpu.h"
25#include "src/gpu/GrMemoryPool.h"
26#include "src/gpu/GrOnFlushResourceProvider.h"
27#include "src/gpu/GrRecordingContextPriv.h"
28#include "src/gpu/GrRenderTargetContext.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040029#include "src/gpu/GrRenderTargetProxy.h"
Chris Dalton6b498102019-08-01 14:14:52 -060030#include "src/gpu/GrRenderTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "src/gpu/GrResourceAllocator.h"
32#include "src/gpu/GrResourceProvider.h"
33#include "src/gpu/GrSoftwarePathRenderer.h"
Greg Daniel46e366a2019-12-16 14:38:36 -050034#include "src/gpu/GrSurfaceContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050035#include "src/gpu/GrSurfaceProxyPriv.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000036#include "src/gpu/GrTexture.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040037#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050038#include "src/gpu/GrTextureProxyPriv.h"
Chris Dalton3d770272019-08-14 09:24:37 -060039#include "src/gpu/GrTextureResolveRenderTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050040#include "src/gpu/GrTracing.h"
Greg Danielbbfec9d2019-08-20 10:56:51 -040041#include "src/gpu/GrTransferFromRenderTask.h"
Greg Danielc30f1a92019-09-06 15:28:58 -040042#include "src/gpu/GrWaitRenderTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050043#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
Herb Derbya08bde62020-06-12 15:46:06 -040044#include "src/gpu/text/GrSDFTOptions.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050045#include "src/image/SkSurface_Gpu.h"
robertphillips498d7ac2015-10-30 10:11:30 -070046
Chris Dalton6b498102019-08-01 14:14:52 -060047void GrDrawingManager::RenderTaskDAG::gatherIDs(SkSTArray<8, uint32_t, true>* idArray) const {
Robert Phillips88e8bb22020-11-10 12:01:52 -050048 idArray->reserve_back(fRenderTasks.count());
Chris Dalton6b498102019-08-01 14:14:52 -060049 for (int i = 0; i < fRenderTasks.count(); ++i) {
50 if (fRenderTasks[i]) {
Robert Phillips02dd0ed2020-11-09 17:03:34 -050051 fRenderTasks[i]->gatherIDs(idArray);
Robert Phillips22310d62018-09-05 11:07:21 -040052 }
53 }
54}
55
Chris Dalton6b498102019-08-01 14:14:52 -060056void GrDrawingManager::RenderTaskDAG::reset() {
57 fRenderTasks.reset();
Robert Phillips22310d62018-09-05 11:07:21 -040058}
59
Adlai Holler96ead542020-06-26 08:50:14 -040060void GrDrawingManager::RenderTaskDAG::rawRemoveRenderTasks(int startIndex, int stopIndex) {
Robert Phillips22310d62018-09-05 11:07:21 -040061 for (int i = startIndex; i < stopIndex; ++i) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -040062 fRenderTasks[i] = nullptr;
Robert Phillips22310d62018-09-05 11:07:21 -040063 }
64}
65
Chris Dalton6b498102019-08-01 14:14:52 -060066bool GrDrawingManager::RenderTaskDAG::isUsed(GrSurfaceProxy* proxy) const {
Adlai Holler33d569e2020-06-16 14:30:08 -040067 for (const auto& task : fRenderTasks) {
68 if (task && task->isUsed(proxy)) {
Robert Phillips9313aa72019-04-09 18:41:27 -040069 return true;
70 }
71 }
72
73 return false;
74}
75
Chris Dalton3d770272019-08-14 09:24:37 -060076GrRenderTask* GrDrawingManager::RenderTaskDAG::add(sk_sp<GrRenderTask> renderTask) {
Chris Dalton6aeb8e82019-08-27 11:52:19 -060077 if (renderTask) {
78 return fRenderTasks.emplace_back(std::move(renderTask)).get();
79 }
80 return nullptr;
Chris Dalton3d770272019-08-14 09:24:37 -060081}
82
83GrRenderTask* GrDrawingManager::RenderTaskDAG::addBeforeLast(sk_sp<GrRenderTask> renderTask) {
84 SkASSERT(!fRenderTasks.empty());
Chris Dalton6aeb8e82019-08-27 11:52:19 -060085 if (renderTask) {
86 // Release 'fRenderTasks.back()' and grab the raw pointer, in case the SkTArray grows
87 // and reallocates during emplace_back.
88 fRenderTasks.emplace_back(fRenderTasks.back().release());
89 return (fRenderTasks[fRenderTasks.count() - 2] = std::move(renderTask)).get();
90 }
91 return nullptr;
Robert Phillips22310d62018-09-05 11:07:21 -040092}
93
Greg Danielf41b2bd2019-08-22 16:19:24 -040094void GrDrawingManager::RenderTaskDAG::swap(SkTArray<sk_sp<GrRenderTask>>* renderTasks) {
95 SkASSERT(renderTasks->empty());
96 renderTasks->swap(fRenderTasks);
Robert Phillips22310d62018-09-05 11:07:21 -040097}
98
Chris Dalton6b498102019-08-01 14:14:52 -060099void GrDrawingManager::RenderTaskDAG::prepForFlush() {
Adlai Holler3078f852020-11-05 15:44:50 -0500100 if (!SkTTopoSort<GrRenderTask, GrRenderTask::TopoSortTraits>(&fRenderTasks)) {
101 SkDEBUGFAIL("Render task topo sort failed.");
102 return;
Robert Phillips22310d62018-09-05 11:07:21 -0400103 }
104
105#ifdef SK_DEBUG
Greg Danielf41b2bd2019-08-22 16:19:24 -0400106 // This block checks for any unnecessary splits in the opsTasks. If two sequential opsTasks
107 // share the same backing GrSurfaceProxy it means the opsTask was artificially split.
Chris Dalton6b498102019-08-01 14:14:52 -0600108 if (fRenderTasks.count()) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400109 GrOpsTask* prevOpsTask = fRenderTasks[0]->asOpsTask();
Chris Dalton6b498102019-08-01 14:14:52 -0600110 for (int i = 1; i < fRenderTasks.count(); ++i) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400111 GrOpsTask* curOpsTask = fRenderTasks[i]->asOpsTask();
Robert Phillips22310d62018-09-05 11:07:21 -0400112
Greg Danielf41b2bd2019-08-22 16:19:24 -0400113 if (prevOpsTask && curOpsTask) {
Adlai Holler33d569e2020-06-16 14:30:08 -0400114 SkASSERT(prevOpsTask->target(0).proxy() != curOpsTask->target(0).proxy());
Robert Phillips22310d62018-09-05 11:07:21 -0400115 }
116
Greg Danielf41b2bd2019-08-22 16:19:24 -0400117 prevOpsTask = curOpsTask;
Robert Phillips22310d62018-09-05 11:07:21 -0400118 }
119 }
120#endif
121}
122
Chris Dalton6b498102019-08-01 14:14:52 -0600123void GrDrawingManager::RenderTaskDAG::closeAll(const GrCaps* caps) {
Adlai Holler96ead542020-06-26 08:50:14 -0400124 for (auto& task : fRenderTasks) {
125 if (task) {
126 task->makeClosed(*caps);
Robert Phillips22310d62018-09-05 11:07:21 -0400127 }
128 }
129}
130
Robert Phillips22310d62018-09-05 11:07:21 -0400131///////////////////////////////////////////////////////////////////////////////////////////////////
Robert Phillips69893702019-02-22 11:16:30 -0500132GrDrawingManager::GrDrawingManager(GrRecordingContext* context,
Robert Phillips22310d62018-09-05 11:07:21 -0400133 const GrPathRendererChain::Options& optionsForPathRendererChain,
Greg Danielf41b2bd2019-08-22 16:19:24 -0400134 bool reduceOpsTaskSplitting)
Robert Phillips22310d62018-09-05 11:07:21 -0400135 : fContext(context)
136 , fOptionsForPathRendererChain(optionsForPathRendererChain)
Robert Phillips22310d62018-09-05 11:07:21 -0400137 , fPathRendererChain(nullptr)
138 , fSoftwarePathRenderer(nullptr)
Robert Phillips6db27c22019-05-01 10:43:56 -0400139 , fFlushing(false)
Herb Derby082232b2020-06-10 15:08:18 -0400140 , fReduceOpsTaskSplitting(reduceOpsTaskSplitting) { }
Robert Phillips22310d62018-09-05 11:07:21 -0400141
robertphillips3dc6ae52015-10-20 09:54:32 -0700142GrDrawingManager::~GrDrawingManager() {
Adlai Holler96ead542020-06-26 08:50:14 -0400143 fDAG.closeAll(fContext->priv().caps());
144 this->removeRenderTasks(0, fDAG.numRenderTasks());
robertphillips3dc6ae52015-10-20 09:54:32 -0700145}
146
Robert Phillipsa9162df2019-02-11 14:12:03 -0500147bool GrDrawingManager::wasAbandoned() const {
Robert Phillips9eb00022020-06-30 15:30:12 -0400148 return fContext->abandoned();
robertphillips3dc6ae52015-10-20 09:54:32 -0700149}
150
robertphillips68737822015-10-29 12:12:21 -0700151void GrDrawingManager::freeGpuResources() {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400152 for (int i = fOnFlushCBObjects.count() - 1; i >= 0; --i) {
153 if (!fOnFlushCBObjects[i]->retainOnFreeGpuResources()) {
154 // it's safe to just do this because we're iterating in reverse
155 fOnFlushCBObjects.removeShuffle(i);
156 }
157 }
158
robertphillips68737822015-10-29 12:12:21 -0700159 // a path renderer may be holding onto resources
robertphillips13391dd2015-10-30 05:15:11 -0700160 fPathRendererChain = nullptr;
Ben Wagner9ec70c62018-07-12 13:30:47 -0400161 fSoftwarePathRenderer = nullptr;
Robert Phillipse3302df2017-04-24 07:31:02 -0400162}
163
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500164// MDB TODO: make use of the 'proxies' parameter.
Greg Daniel9efe3862020-06-11 11:51:06 -0400165bool GrDrawingManager::flush(
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500166 SkSpan<GrSurfaceProxy*> proxies,
Greg Daniel9efe3862020-06-11 11:51:06 -0400167 SkSurface::BackendSurfaceAccess access,
168 const GrFlushInfo& info,
169 const GrBackendSurfaceMutableState* newState) {
Brian Salomon57d2beab2018-09-10 09:35:41 -0400170 GR_CREATE_TRACE_MARKER_CONTEXT("GrDrawingManager", "flush", fContext);
Brian Salomondcbb9d92017-07-19 10:53:20 -0400171
robertphillips7761d612016-05-16 09:14:53 -0700172 if (fFlushing || this->wasAbandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -0400173 if (info.fSubmittedProc) {
174 info.fSubmittedProc(info.fSubmittedContext, false);
175 }
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400176 if (info.fFinishedProc) {
177 info.fFinishedProc(info.fFinishedContext);
Greg Daniela3aa75a2019-04-12 14:24:55 -0400178 }
Greg Danielfe159622020-04-10 17:43:51 +0000179 return false;
joshualittb8918c42015-12-18 09:59:46 -0800180 }
Robert Phillips602df412019-04-08 11:10:39 -0400181
Robert Phillips38d64b02018-09-04 13:23:26 -0400182 SkDEBUGCODE(this->validate());
183
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500184 // As of now we only short-circuit if we got an explicit list of surfaces to flush.
185 if (!proxies.empty() && !info.fNumSemaphores && !info.fFinishedProc &&
Greg Danielce9f0162020-06-30 13:42:46 -0400186 access == SkSurface::BackendSurfaceAccess::kNoAccess && !newState) {
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500187 bool allUnused = std::all_of(proxies.begin(), proxies.end(), [&](GrSurfaceProxy* proxy) {
188 return !fDAG.isUsed(proxy) && !this->isDDLTarget(proxy);
189 });
190 if (allUnused) {
Greg Daniel55822f12020-05-26 11:26:45 -0400191 if (info.fSubmittedProc) {
192 info.fSubmittedProc(info.fSubmittedContext, true);
193 }
Greg Danielfe159622020-04-10 17:43:51 +0000194 return false;
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400195 }
Robert Phillips9313aa72019-04-09 18:41:27 -0400196 }
197
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400198 auto direct = fContext->asDirectContext();
Adlai Holleraee25fd2020-11-06 12:34:56 -0500199 SkASSERT(direct);
Brian Salomon9241a6d2019-10-03 13:26:54 -0400200 direct->priv().clientMappedBufferManager()->process();
Robert Phillips6a6de562019-02-15 15:19:15 -0500201
202 GrGpu* gpu = direct->priv().getGpu();
Greg Daniel55822f12020-05-26 11:26:45 -0400203 // We have a non abandoned and direct GrContext. It must have a GrGpu.
204 SkASSERT(gpu);
Greg Daniela3aa75a2019-04-12 14:24:55 -0400205
joshualittb8918c42015-12-18 09:59:46 -0800206 fFlushing = true;
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400207
Robert Phillips6a6de562019-02-15 15:19:15 -0500208 auto resourceProvider = direct->priv().resourceProvider();
209 auto resourceCache = direct->priv().getResourceCache();
210
Chris Dalton6b498102019-08-01 14:14:52 -0600211 // Semi-usually the GrRenderTasks are already closed at this point, but sometimes Ganesh needs
Greg Danielf41b2bd2019-08-22 16:19:24 -0400212 // to flush mid-draw. In that case, the SkGpuDevice's opsTasks won't be closed but need to be
213 // flushed anyway. Closing such opsTasks here will mean new ones will be created to replace them
Chris Dalton6b498102019-08-01 14:14:52 -0600214 // if the SkGpuDevice(s) write to them again.
Robert Phillips9da87e02019-02-04 13:26:26 -0500215 fDAG.closeAll(fContext->priv().caps());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400216 fActiveOpsTask = nullptr;
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400217
Robert Phillips22310d62018-09-05 11:07:21 -0400218 fDAG.prepForFlush();
Brian Salomon601ac802019-02-07 13:37:16 -0500219 if (!fCpuBufferCache) {
220 // We cache more buffers when the backend is using client side arrays. Otherwise, we
221 // expect each pool will use a CPU buffer as a staging buffer before uploading to a GPU
222 // buffer object. Each pool only requires one staging buffer at a time.
223 int maxCachedBuffers = fContext->priv().caps()->preferClientSideDynamicBuffers() ? 2 : 6;
224 fCpuBufferCache = GrBufferAllocPool::CpuBufferCache::Make(maxCachedBuffers);
Brian Salomon58f153c2018-10-18 21:51:15 -0400225 }
Robert Phillipsa4c93ac2017-05-18 11:40:04 -0400226
Robert Phillipse5f73282019-06-18 17:15:04 -0400227 GrOpFlushState flushState(gpu, resourceProvider, &fTokenTracker, fCpuBufferCache);
Robert Phillips40a29d72018-01-18 12:59:22 -0500228
Chris Daltonfe199b72017-05-05 11:26:15 -0400229 GrOnFlushResourceProvider onFlushProvider(this);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400230
Chris Dalton12658942017-10-05 19:45:25 -0600231 // Prepare any onFlush op lists (e.g. atlases).
Chris Daltonfe199b72017-05-05 11:26:15 -0400232 if (!fOnFlushCBObjects.empty()) {
Chris Dalton6b498102019-08-01 14:14:52 -0600233 fDAG.gatherIDs(&fFlushingRenderTaskIDs);
Robert Phillips22310d62018-09-05 11:07:21 -0400234
Chris Daltonfe199b72017-05-05 11:26:15 -0400235 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
Chris Dalton6b498102019-08-01 14:14:52 -0600236 onFlushCBObject->preFlush(&onFlushProvider, fFlushingRenderTaskIDs.begin(),
Chris Daltonc4b47352019-08-23 10:10:36 -0600237 fFlushingRenderTaskIDs.count());
238 }
239 for (const auto& onFlushRenderTask : fOnFlushRenderTasks) {
240 onFlushRenderTask->makeClosed(*fContext->priv().caps());
Chris Dalton706a6ff2017-11-29 22:01:06 -0700241#ifdef SK_DEBUG
Chris Daltonc4b47352019-08-23 10:10:36 -0600242 // OnFlush callbacks are invoked during flush, and are therefore expected to handle
243 // resource allocation & usage on their own. (No deferred or lazy proxies!)
244 onFlushRenderTask->visitTargetAndSrcProxies_debugOnly(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400245 [](GrSurfaceProxy* p, GrMipmapped mipMapped) {
Chris Daltonc4b47352019-08-23 10:10:36 -0600246 SkASSERT(!p->asTextureProxy() || !p->asTextureProxy()->texPriv().isDeferred());
Brian Salomonbeb7f522019-08-30 16:19:42 -0400247 SkASSERT(!p->isLazy());
Chris Dalton4ece96d2019-08-30 11:26:39 -0600248 if (p->requiresManualMSAAResolve()) {
249 // The onFlush callback is responsible for ensuring MSAA gets resolved.
250 SkASSERT(p->asRenderTargetProxy() && !p->asRenderTargetProxy()->isMSAADirty());
251 }
Brian Salomon7e67dca2020-07-21 09:27:25 -0400252 if (GrMipmapped::kYes == mipMapped) {
Chris Daltonc4b47352019-08-23 10:10:36 -0600253 // The onFlush callback is responsible for regenerating mips if needed.
Brian Salomon8c82a872020-07-21 12:09:58 -0400254 SkASSERT(p->asTextureProxy() && !p->asTextureProxy()->mipmapsAreDirty());
Chris Daltonc4b47352019-08-23 10:10:36 -0600255 }
256 });
Chris Dalton706a6ff2017-11-29 22:01:06 -0700257#endif
Chris Daltonc4b47352019-08-23 10:10:36 -0600258 onFlushRenderTask->prepare(&flushState);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400259 }
260 }
261
robertphillipsa13e2022015-11-11 12:01:09 -0800262#if 0
Brian Salomon09d994e2016-12-21 11:14:46 -0500263 // Enable this to print out verbose GrOp information
Adlai Hollerabdfd392020-10-19 09:00:36 -0400264 SkDEBUGCODE(SkDebugf("onFlush renderTasks (%d):\n", fOnFlushRenderTasks.count()));
Chris Daltonc4b47352019-08-23 10:10:36 -0600265 for (const auto& onFlushRenderTask : fOnFlushRenderTasks) {
Adlai Hollerabdfd392020-10-19 09:00:36 -0400266 SkDEBUGCODE(onFlushRenderTask->dump(/* printDependencies */ true);)
Chris Daltonc4b47352019-08-23 10:10:36 -0600267 }
Adlai Hollerabdfd392020-10-19 09:00:36 -0400268 SkDEBUGCODE(SkDebugf("Normal renderTasks (%d):\n", fDAG.numRenderTasks()));
269 for (int i = 0; i < fDAG.numRenderTasks(); ++i) {
270 SkDEBUGCODE(fDAG.renderTask(i)->dump(/* printDependencies */ true);)
robertphillips3dc6ae52015-10-20 09:54:32 -0700271 }
robertphillipsa13e2022015-11-11 12:01:09 -0800272#endif
273
Robert Phillipseafd48a2017-11-16 07:52:08 -0500274 int startIndex, stopIndex;
275 bool flushed = false;
276
Robert Phillipsf8e25022017-11-08 15:24:31 -0500277 {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400278 GrResourceAllocator alloc(resourceProvider SkDEBUGCODE(, fDAG.numRenderTasks()));
Chris Dalton6b498102019-08-01 14:14:52 -0600279 for (int i = 0; i < fDAG.numRenderTasks(); ++i) {
280 if (fDAG.renderTask(i)) {
281 fDAG.renderTask(i)->gatherProxyIntervals(&alloc);
Robert Phillips22310d62018-09-05 11:07:21 -0400282 }
Greg Danielf41b2bd2019-08-22 16:19:24 -0400283 alloc.markEndOfOpsTask(i);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500284 }
Robert Phillipsc73666f2019-04-24 08:49:48 -0400285 alloc.determineRecyclability();
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400286
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500287 GrResourceAllocator::AssignError error = GrResourceAllocator::AssignError::kNoError;
Chris Dalton6b498102019-08-01 14:14:52 -0600288 int numRenderTasksExecuted = 0;
Brian Salomon577aa0f2018-11-30 13:32:23 -0500289 while (alloc.assign(&startIndex, &stopIndex, &error)) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500290 if (GrResourceAllocator::AssignError::kFailedProxyInstantiation == error) {
291 for (int i = startIndex; i < stopIndex; ++i) {
Chris Dalton6b498102019-08-01 14:14:52 -0600292 GrRenderTask* renderTask = fDAG.renderTask(i);
293 if (!renderTask) {
294 continue;
Robert Phillips01a91282018-07-26 08:03:04 -0400295 }
Chris Dalton6b498102019-08-01 14:14:52 -0600296 if (!renderTask->isInstantiated()) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400297 // No need to call the renderTask's handleInternalAllocationFailure
298 // since we will already skip executing the renderTask since it is not
299 // instantiated.
Chris Dalton6b498102019-08-01 14:14:52 -0600300 continue;
Robert Phillipsbbcb7f72018-05-31 11:16:19 -0400301 }
Chris Dalton6b498102019-08-01 14:14:52 -0600302 renderTask->handleInternalAllocationFailure();
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500303 }
Adlai Holler96ead542020-06-26 08:50:14 -0400304 this->removeRenderTasks(startIndex, stopIndex);
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500305 }
Robert Phillips4150eea2018-02-07 17:08:21 -0500306
Chris Dalton6b498102019-08-01 14:14:52 -0600307 if (this->executeRenderTasks(
308 startIndex, stopIndex, &flushState, &numRenderTasksExecuted)) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500309 flushed = true;
310 }
bsalomondc438982016-08-31 11:53:49 -0700311 }
Greg Danielc42b20b2017-10-04 10:34:49 -0400312 }
313
Chris Dalton91ab1552018-04-18 13:24:25 -0600314#ifdef SK_DEBUG
Chris Dalton6b498102019-08-01 14:14:52 -0600315 for (int i = 0; i < fDAG.numRenderTasks(); ++i) {
Adlai Holler96ead542020-06-26 08:50:14 -0400316 // All render tasks should have been cleared out by now – we only reset the array below to
317 // reclaim storage.
318 SkASSERT(!fDAG.renderTask(i));
Chris Dalton91ab1552018-04-18 13:24:25 -0600319 }
320#endif
Adlai Holler25df1f72020-06-09 13:08:27 -0400321 fLastRenderTasks.reset();
Robert Phillips22310d62018-09-05 11:07:21 -0400322 fDAG.reset();
Robert Phillips15c91422019-05-07 16:54:48 -0400323 this->clearDDLTargets();
robertphillipsa13e2022015-11-11 12:01:09 -0800324
Robert Phillipsc994a932018-06-19 13:09:54 -0400325#ifdef SK_DEBUG
326 // In non-DDL mode this checks that all the flushed ops have been freed from the memory pool.
327 // When we move to partial flushes this assert will no longer be valid.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400328 // In DDL mode this check is somewhat superfluous since the memory for most of the ops/opsTasks
Robert Phillipsc994a932018-06-19 13:09:54 -0400329 // will be stored in the DDL's GrOpMemoryPools.
Herb Derbye32e1ab2020-10-27 10:29:46 -0400330 GrMemoryPool* opMemoryPool = fContext->priv().opMemoryPool();
Robert Phillipsc994a932018-06-19 13:09:54 -0400331 opMemoryPool->isEmpty();
332#endif
333
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500334 gpu->executeFlushInfo(proxies, access, info, newState);
robertphillipsa13e2022015-11-11 12:01:09 -0800335
Brian Salomon57d2beab2018-09-10 09:35:41 -0400336 // Give the cache a chance to purge resources that become purgeable due to flushing.
337 if (flushed) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500338 resourceCache->purgeAsNeeded();
Brian Salomon876a0172019-03-08 11:12:14 -0500339 flushed = false;
bsalomonb77a9072016-09-07 10:02:04 -0700340 }
Chris Daltonfe199b72017-05-05 11:26:15 -0400341 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
Chris Dalton6b498102019-08-01 14:14:52 -0600342 onFlushCBObject->postFlush(fTokenTracker.nextTokenToFlush(), fFlushingRenderTaskIDs.begin(),
343 fFlushingRenderTaskIDs.count());
Brian Salomon876a0172019-03-08 11:12:14 -0500344 flushed = true;
345 }
346 if (flushed) {
347 resourceCache->purgeAsNeeded();
Chris Daltonfe199b72017-05-05 11:26:15 -0400348 }
Chris Dalton6b498102019-08-01 14:14:52 -0600349 fFlushingRenderTaskIDs.reset();
joshualittb8918c42015-12-18 09:59:46 -0800350 fFlushing = false;
Greg Daniel51316782017-08-02 15:10:09 +0000351
Greg Danielfe159622020-04-10 17:43:51 +0000352 return true;
353}
354
355bool GrDrawingManager::submitToGpu(bool syncToCpu) {
356 if (fFlushing || this->wasAbandoned()) {
357 return false;
358 }
359
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400360 auto direct = fContext->asDirectContext();
Greg Danielfe159622020-04-10 17:43:51 +0000361 if (!direct) {
362 return false; // Can't submit while DDL recording
363 }
364 GrGpu* gpu = direct->priv().getGpu();
365 return gpu->submitToGpu(syncToCpu);
robertphillips3dc6ae52015-10-20 09:54:32 -0700366}
367
Chris Dalton6b498102019-08-01 14:14:52 -0600368bool GrDrawingManager::executeRenderTasks(int startIndex, int stopIndex, GrOpFlushState* flushState,
369 int* numRenderTasksExecuted) {
370 SkASSERT(startIndex <= stopIndex && stopIndex <= fDAG.numRenderTasks());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500371
Robert Phillips27483912018-04-20 12:43:18 -0400372#if GR_FLUSH_TIME_OP_SPEW
Greg Danielf41b2bd2019-08-22 16:19:24 -0400373 SkDebugf("Flushing opsTask: %d to %d out of [%d, %d]\n",
Chris Dalton6b498102019-08-01 14:14:52 -0600374 startIndex, stopIndex, 0, fDAG.numRenderTasks());
Robert Phillips27483912018-04-20 12:43:18 -0400375 for (int i = startIndex; i < stopIndex; ++i) {
Chris Dalton6b498102019-08-01 14:14:52 -0600376 if (fDAG.renderTask(i)) {
377 fDAG.renderTask(i)->dump(true);
Robert Phillips1734dd32018-08-21 13:52:09 -0400378 }
Robert Phillips27483912018-04-20 12:43:18 -0400379 }
380#endif
381
Chris Dalton6b498102019-08-01 14:14:52 -0600382 bool anyRenderTasksExecuted = false;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500383
384 for (int i = startIndex; i < stopIndex; ++i) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400385 GrRenderTask* renderTask = fDAG.renderTask(i);
386 if (!renderTask || !renderTask->isInstantiated()) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500387 continue;
388 }
389
Chris Dalton6b498102019-08-01 14:14:52 -0600390 SkASSERT(renderTask->deferredProxiesAreInstantiated());
Robert Phillips22310d62018-09-05 11:07:21 -0400391
Chris Dalton6b498102019-08-01 14:14:52 -0600392 renderTask->prepare(flushState);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500393 }
394
395 // Upload all data to the GPU
396 flushState->preExecuteDraws();
397
Greg Danield2073452018-12-07 11:20:33 -0500398 // For Vulkan, if we have too many oplists to be flushed we end up allocating a lot of resources
399 // for each command buffer associated with the oplists. If this gets too large we can cause the
400 // devices to go OOM. In practice we usually only hit this case in our tests, but to be safe we
401 // put a cap on the number of oplists we will execute before flushing to the GPU to relieve some
402 // memory pressure.
Chris Dalton6b498102019-08-01 14:14:52 -0600403 static constexpr int kMaxRenderTasksBeforeFlush = 100;
Greg Danield2073452018-12-07 11:20:33 -0500404
Chris Daltonc4b47352019-08-23 10:10:36 -0600405 // Execute the onFlush renderTasks first, if any.
406 for (sk_sp<GrRenderTask>& onFlushRenderTask : fOnFlushRenderTasks) {
407 if (!onFlushRenderTask->execute(flushState)) {
408 SkDebugf("WARNING: onFlushRenderTask failed to execute.\n");
Robert Phillipseafd48a2017-11-16 07:52:08 -0500409 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600410 SkASSERT(onFlushRenderTask->unique());
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400411 onFlushRenderTask->disown(this);
Chris Daltonc4b47352019-08-23 10:10:36 -0600412 onFlushRenderTask = nullptr;
Chris Dalton6b498102019-08-01 14:14:52 -0600413 (*numRenderTasksExecuted)++;
414 if (*numRenderTasksExecuted >= kMaxRenderTasksBeforeFlush) {
Greg Danielfe159622020-04-10 17:43:51 +0000415 flushState->gpu()->submitToGpu(false);
Chris Dalton6b498102019-08-01 14:14:52 -0600416 *numRenderTasksExecuted = 0;
Greg Danield2073452018-12-07 11:20:33 -0500417 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500418 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600419 fOnFlushRenderTasks.reset();
Robert Phillipseafd48a2017-11-16 07:52:08 -0500420
421 // Execute the normal op lists.
422 for (int i = startIndex; i < stopIndex; ++i) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400423 GrRenderTask* renderTask = fDAG.renderTask(i);
424 if (!renderTask || !renderTask->isInstantiated()) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500425 continue;
426 }
427
Greg Daniel15ecdf92019-08-30 15:35:23 -0400428 if (renderTask->execute(flushState)) {
Chris Dalton6b498102019-08-01 14:14:52 -0600429 anyRenderTasksExecuted = true;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500430 }
Chris Dalton6b498102019-08-01 14:14:52 -0600431 (*numRenderTasksExecuted)++;
432 if (*numRenderTasksExecuted >= kMaxRenderTasksBeforeFlush) {
Greg Danielfe159622020-04-10 17:43:51 +0000433 flushState->gpu()->submitToGpu(false);
Chris Dalton6b498102019-08-01 14:14:52 -0600434 *numRenderTasksExecuted = 0;
Greg Danield2073452018-12-07 11:20:33 -0500435 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500436 }
437
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400438 SkASSERT(!flushState->opsRenderPass());
Robert Phillips40a29d72018-01-18 12:59:22 -0500439 SkASSERT(fTokenTracker.nextDrawToken() == fTokenTracker.nextTokenToFlush());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500440
Chris Dalton6b498102019-08-01 14:14:52 -0600441 // We reset the flush state before the RenderTasks so that the last resources to be freed are
442 // those that are written to in the RenderTasks. This helps to make sure the most recently used
443 // resources are the last to be purged by the resource cache.
Robert Phillipseafd48a2017-11-16 07:52:08 -0500444 flushState->reset();
445
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400446 this->removeRenderTasks(startIndex, stopIndex);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500447
Chris Dalton6b498102019-08-01 14:14:52 -0600448 return anyRenderTasksExecuted;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500449}
450
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400451void GrDrawingManager::removeRenderTasks(int startIndex, int stopIndex) {
452 for (int i = startIndex; i < stopIndex; ++i) {
453 GrRenderTask* task = fDAG.renderTask(i);
454 if (!task) {
455 continue;
456 }
Robert Phillips02dd0ed2020-11-09 17:03:34 -0500457 if (!task->unique() || task->requiresExplicitCleanup()) {
458 // TODO: Eventually uniqueness should be guaranteed: http://skbug.com/7111.
459 // DDLs, however, will always require an explicit notification for when they
460 // can clean up resources.
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400461 task->endFlush(this);
462 }
463 task->disown(this);
464 }
Adlai Holler96ead542020-06-26 08:50:14 -0400465 fDAG.rawRemoveRenderTasks(startIndex, stopIndex);
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400466}
467
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400468static void resolve_and_mipmap(GrGpu* gpu, GrSurfaceProxy* proxy) {
469 if (!proxy->isInstantiated()) {
470 return;
471 }
472
473 // In the flushSurfaces case, we need to resolve MSAA immediately after flush. This is
474 // because clients expect the flushed surface's backing texture to be fully resolved
475 // upon return.
476 if (proxy->requiresManualMSAAResolve()) {
477 auto* rtProxy = proxy->asRenderTargetProxy();
478 SkASSERT(rtProxy);
479 if (rtProxy->isMSAADirty()) {
480 SkASSERT(rtProxy->peekRenderTarget());
Jim Van Verthbb61fe32020-07-07 16:39:04 -0400481 gpu->resolveRenderTarget(rtProxy->peekRenderTarget(), rtProxy->msaaDirtyRect());
482 gpu->submitToGpu(false);
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400483 rtProxy->markMSAAResolved();
484 }
485 }
486 // If, after a flush, any of the proxies of interest have dirty mipmaps, regenerate them in
487 // case their backend textures are being stolen.
488 // (This special case is exercised by the ReimportImageTextureWithMipLevels test.)
489 // FIXME: It may be more ideal to plumb down a "we're going to steal the backends" flag.
490 if (auto* textureProxy = proxy->asTextureProxy()) {
Brian Salomon8c82a872020-07-21 12:09:58 -0400491 if (textureProxy->mipmapsAreDirty()) {
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400492 SkASSERT(textureProxy->peekTexture());
493 gpu->regenerateMipMapLevels(textureProxy->peekTexture());
Brian Salomon8c82a872020-07-21 12:09:58 -0400494 textureProxy->markMipmapsClean();
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400495 }
496 }
497}
498
Greg Daniel9efe3862020-06-11 11:51:06 -0400499GrSemaphoresSubmitted GrDrawingManager::flushSurfaces(
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500500 SkSpan<GrSurfaceProxy*> proxies,
Greg Daniel9efe3862020-06-11 11:51:06 -0400501 SkSurface::BackendSurfaceAccess access,
502 const GrFlushInfo& info,
503 const GrBackendSurfaceMutableState* newState) {
bsalomon6a2b1942016-09-08 11:28:59 -0700504 if (this->wasAbandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -0400505 if (info.fSubmittedProc) {
506 info.fSubmittedProc(info.fSubmittedContext, false);
507 }
508 if (info.fFinishedProc) {
509 info.fFinishedProc(info.fFinishedContext);
510 }
Greg Daniel51316782017-08-02 15:10:09 +0000511 return GrSemaphoresSubmitted::kNo;
bsalomon6a2b1942016-09-08 11:28:59 -0700512 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400513 SkDEBUGCODE(this->validate());
bsalomon6a2b1942016-09-08 11:28:59 -0700514
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400515 auto direct = fContext->asDirectContext();
Adlai Holleraee25fd2020-11-06 12:34:56 -0500516 SkASSERT(direct);
Robert Phillips6a6de562019-02-15 15:19:15 -0500517 GrGpu* gpu = direct->priv().getGpu();
Greg Daniel55822f12020-05-26 11:26:45 -0400518 // We have a non abandoned and direct GrContext. It must have a GrGpu.
519 SkASSERT(gpu);
Robert Phillips874b5352018-03-16 08:48:24 -0400520
Robert Phillipsacc10fa2019-04-01 09:50:20 -0400521 // TODO: It is important to upgrade the drawingmanager to just flushing the
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400522 // portion of the DAG required by 'proxies' in order to restore some of the
Robert Phillipsacc10fa2019-04-01 09:50:20 -0400523 // semantics of this method.
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500524 bool didFlush = this->flush(proxies, access, info, newState);
525 for (GrSurfaceProxy* proxy : proxies) {
526 resolve_and_mipmap(gpu, proxy);
bsalomon6a2b1942016-09-08 11:28:59 -0700527 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400528
529 SkDEBUGCODE(this->validate());
Greg Danielfe159622020-04-10 17:43:51 +0000530
Greg Daniel04283f32020-05-20 13:16:00 -0400531 if (!didFlush || (!direct->priv().caps()->semaphoreSupport() && info.fNumSemaphores)) {
Greg Danielfe159622020-04-10 17:43:51 +0000532 return GrSemaphoresSubmitted::kNo;
533 }
534 return GrSemaphoresSubmitted::kYes;
bsalomon6a2b1942016-09-08 11:28:59 -0700535}
536
Chris Daltonfe199b72017-05-05 11:26:15 -0400537void GrDrawingManager::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
538 fOnFlushCBObjects.push_back(onFlushCBObject);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400539}
540
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500541#if GR_TEST_UTILS
542void GrDrawingManager::testingOnly_removeOnFlushCallbackObject(GrOnFlushCallbackObject* cb) {
543 int n = std::find(fOnFlushCBObjects.begin(), fOnFlushCBObjects.end(), cb) -
544 fOnFlushCBObjects.begin();
545 SkASSERT(n < fOnFlushCBObjects.count());
546 fOnFlushCBObjects.removeShuffle(n);
547}
548#endif
549
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400550void GrDrawingManager::setLastRenderTask(const GrSurfaceProxy* proxy, GrRenderTask* task) {
551#ifdef SK_DEBUG
Robert Phillips02dd0ed2020-11-09 17:03:34 -0500552 if (auto prior = this->getLastRenderTask(proxy)) {
553 SkASSERT(prior->isClosed() || prior == task);
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400554 }
555#endif
Adlai Holler25df1f72020-06-09 13:08:27 -0400556 uint32_t key = proxy->uniqueID().asUInt();
557 if (task) {
558 fLastRenderTasks.set(key, task);
559 } else if (fLastRenderTasks.find(key)) {
560 fLastRenderTasks.remove(key);
561 }
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400562}
563
564GrRenderTask* GrDrawingManager::getLastRenderTask(const GrSurfaceProxy* proxy) const {
565 auto entry = fLastRenderTasks.find(proxy->uniqueID().asUInt());
566 return entry ? *entry : nullptr;
567}
568
569GrOpsTask* GrDrawingManager::getLastOpsTask(const GrSurfaceProxy* proxy) const {
570 GrRenderTask* task = this->getLastRenderTask(proxy);
571 return task ? task->asOpsTask() : nullptr;
572}
573
574
Chris Dalton6b498102019-08-01 14:14:52 -0600575void GrDrawingManager::moveRenderTasksToDDL(SkDeferredDisplayList* ddl) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400576 SkDEBUGCODE(this->validate());
577
Chris Dalton6b498102019-08-01 14:14:52 -0600578 // no renderTask should receive a new command after this
Robert Phillips9da87e02019-02-04 13:26:26 -0500579 fDAG.closeAll(fContext->priv().caps());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400580 fActiveOpsTask = nullptr;
Robert Phillips7a137052018-02-01 11:23:12 -0500581
Robert Phillips02dd0ed2020-11-09 17:03:34 -0500582 fDAG.prepForFlush();
583
Chris Dalton6b498102019-08-01 14:14:52 -0600584 fDAG.swap(&ddl->fRenderTasks);
Robert Phillips19f466d2020-02-26 10:27:07 -0500585 SkASSERT(!fDAG.numRenderTasks());
Robert Phillips867ce8f2018-06-21 10:28:36 -0400586
Robert Phillips19f466d2020-02-26 10:27:07 -0500587 for (auto& renderTask : ddl->fRenderTasks) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400588 renderTask->disown(this);
Robert Phillips29f38542019-10-16 09:20:25 -0400589 renderTask->prePrepare(fContext);
Robert Phillips7327c9d2019-10-08 16:32:56 -0400590 }
591
Michael Ludwig2c316bd2019-12-19 14:50:44 -0500592 ddl->fArenas = std::move(fContext->priv().detachArenas());
Robert Phillips61fc7992019-10-22 11:58:17 -0400593
Robert Phillipsf6a0b452020-02-18 14:26:46 -0500594 fContext->priv().detachProgramData(&ddl->fProgramData);
Robert Phillips576b6a12019-12-06 13:05:49 -0500595
Robert Phillips774168e2018-05-31 12:43:27 -0400596 if (fPathRendererChain) {
597 if (auto ccpr = fPathRendererChain->getCoverageCountingPathRenderer()) {
598 ddl->fPendingPaths = ccpr->detachPendingPaths();
599 }
600 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400601
602 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500603}
604
Robert Phillips02dd0ed2020-11-09 17:03:34 -0500605void GrDrawingManager::createDDLTask(sk_sp<const SkDeferredDisplayList> ddl,
606 GrRenderTargetProxy* newDest) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400607 SkDEBUGCODE(this->validate());
608
Greg Danielf41b2bd2019-08-22 16:19:24 -0400609 if (fActiveOpsTask) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400610 // This is a temporary fix for the partial-MDB world. In that world we're not
Greg Danielf41b2bd2019-08-22 16:19:24 -0400611 // reordering so ops that (in the single opsTask world) would've just glommed onto the
612 // end of the single opsTask but referred to a far earlier RT need to appear in their
613 // own opsTask.
614 fActiveOpsTask->makeClosed(*fContext->priv().caps());
615 fActiveOpsTask = nullptr;
Robert Phillips38d64b02018-09-04 13:23:26 -0400616 }
617
Robert Phillips02dd0ed2020-11-09 17:03:34 -0500618 // Propagate the DDL proxy's state information to the replay target.
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400619 if (ddl->priv().targetProxy()->isMSAADirty()) {
620 newDest->markMSAADirty(ddl->priv().targetProxy()->msaaDirtyRect(),
621 ddl->characterization().origin());
622 }
623 GrTextureProxy* newTextureProxy = newDest->asTextureProxy();
Brian Salomon8c82a872020-07-21 12:09:58 -0400624 if (newTextureProxy && GrMipmapped::kYes == newTextureProxy->mipmapped()) {
625 newTextureProxy->markMipmapsDirty();
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400626 }
627
628 this->addDDLTarget(newDest, ddl->priv().targetProxy());
Robert Phillips15c91422019-05-07 16:54:48 -0400629
Robert Phillips62000362018-02-01 09:10:04 -0500630 // Here we jam the proxy that backs the current replay SkSurface into the LazyProxyData.
Robert Phillips02dd0ed2020-11-09 17:03:34 -0500631 // The lazy proxy that references it (in the DDL opsTasks) will then steal its GrTexture.
Robert Phillips62000362018-02-01 09:10:04 -0500632 ddl->fLazyProxyData->fReplayDest = newDest;
Robert Phillips774168e2018-05-31 12:43:27 -0400633
634 if (ddl->fPendingPaths.size()) {
635 GrCoverageCountingPathRenderer* ccpr = this->getCoverageCountingPathRenderer();
636
637 ccpr->mergePendingPaths(ddl->fPendingPaths);
638 }
Robert Phillips22310d62018-09-05 11:07:21 -0400639
Robert Phillips02dd0ed2020-11-09 17:03:34 -0500640 // Add a task to handle drawing and lifetime management of the DDL.
641 SkDEBUGCODE(auto ddlTask =) fDAG.add(sk_make_sp<GrDDLTask>(this,
642 sk_ref_sp(newDest),
643 std::move(ddl)));
644 SkASSERT(ddlTask->isClosed());
Adlai Holler7580ad42020-06-24 13:45:25 -0400645
Robert Phillips38d64b02018-09-04 13:23:26 -0400646 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500647}
648
Robert Phillips38d64b02018-09-04 13:23:26 -0400649#ifdef SK_DEBUG
650void GrDrawingManager::validate() const {
Adlai Holler3078f852020-11-05 15:44:50 -0500651 if (fReduceOpsTaskSplitting) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400652 SkASSERT(!fActiveOpsTask);
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400653 } else {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400654 if (fActiveOpsTask) {
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400655 SkASSERT(!fDAG.empty());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400656 SkASSERT(!fActiveOpsTask->isClosed());
657 SkASSERT(fActiveOpsTask == fDAG.back());
Robert Phillips38d64b02018-09-04 13:23:26 -0400658 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400659
Chris Dalton6b498102019-08-01 14:14:52 -0600660 for (int i = 0; i < fDAG.numRenderTasks(); ++i) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400661 if (fActiveOpsTask != fDAG.renderTask(i)) {
Chris Daltone2a903e2019-09-18 13:41:50 -0600662 // The resolveTask associated with the activeTask remains open for as long as the
663 // activeTask does.
664 bool isActiveResolveTask =
665 fActiveOpsTask && fActiveOpsTask->fTextureResolveTask == fDAG.renderTask(i);
666 SkASSERT(isActiveResolveTask || fDAG.renderTask(i)->isClosed());
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400667 }
668 }
669
670 if (!fDAG.empty() && !fDAG.back()->isClosed()) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400671 SkASSERT(fActiveOpsTask == fDAG.back());
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400672 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400673 }
674}
675#endif
676
Greg Danielbbfec9d2019-08-20 10:56:51 -0400677void GrDrawingManager::closeRenderTasksForNewRenderTask(GrSurfaceProxy* target) {
Adlai Holler3078f852020-11-05 15:44:50 -0500678 if (target && fReduceOpsTaskSplitting) {
Chris Dalton5fe99772019-08-06 11:57:39 -0600679 // In this case we need to close all the renderTasks that rely on the current contents of
680 // 'target'. That is bc we're going to update the content of the proxy so they need to be
681 // split in case they use both the old and new content. (This is a bit of an overkill: they
682 // really only need to be split if they ever reference proxy's contents again but that is
683 // hard to predict/handle).
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400684 if (GrRenderTask* lastRenderTask = this->getLastRenderTask(target)) {
Chris Dalton6b498102019-08-01 14:14:52 -0600685 lastRenderTask->closeThoseWhoDependOnMe(*fContext->priv().caps());
Robert Phillips46acf9d2018-10-09 09:31:40 -0400686 }
Greg Danielf41b2bd2019-08-22 16:19:24 -0400687 } else if (fActiveOpsTask) {
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400688 // This is a temporary fix for the partial-MDB world. In that world we're not
Greg Danielf41b2bd2019-08-22 16:19:24 -0400689 // reordering so ops that (in the single opsTask world) would've just glommed onto the
690 // end of the single opsTask but referred to a far earlier RT need to appear in their
691 // own opsTask.
692 fActiveOpsTask->makeClosed(*fContext->priv().caps());
693 fActiveOpsTask = nullptr;
robertphillips3dc6ae52015-10-20 09:54:32 -0700694 }
Chris Dalton5fe99772019-08-06 11:57:39 -0600695}
696
Greg Daniel16f5c652019-10-29 11:26:01 -0400697sk_sp<GrOpsTask> GrDrawingManager::newOpsTask(GrSurfaceProxyView surfaceView,
698 bool managedOpsTask) {
Chris Dalton5fe99772019-08-06 11:57:39 -0600699 SkDEBUGCODE(this->validate());
700 SkASSERT(fContext);
701
Greg Daniel16f5c652019-10-29 11:26:01 -0400702 GrSurfaceProxy* proxy = surfaceView.proxy();
703 this->closeRenderTasksForNewRenderTask(proxy);
robertphillips3dc6ae52015-10-20 09:54:32 -0700704
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400705 sk_sp<GrOpsTask> opsTask(new GrOpsTask(this, fContext->priv().arenas(),
Michael Ludwigd0840ec2019-12-12 09:48:38 -0500706 std::move(surfaceView),
707 fContext->priv().auditTrail()));
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400708 SkASSERT(this->getLastRenderTask(proxy) == opsTask.get());
robertphillips3dc6ae52015-10-20 09:54:32 -0700709
Greg Danielf41b2bd2019-08-22 16:19:24 -0400710 if (managedOpsTask) {
711 fDAG.add(opsTask);
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400712
Adlai Holler3078f852020-11-05 15:44:50 -0500713 if (!fReduceOpsTaskSplitting) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400714 fActiveOpsTask = opsTask.get();
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400715 }
Robert Phillips941d1442017-06-14 16:37:02 -0400716 }
robertphillips3dc6ae52015-10-20 09:54:32 -0700717
Robert Phillips38d64b02018-09-04 13:23:26 -0400718 SkDEBUGCODE(this->validate());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400719 return opsTask;
robertphillips3dc6ae52015-10-20 09:54:32 -0700720}
721
Chris Daltone2a903e2019-09-18 13:41:50 -0600722GrTextureResolveRenderTask* GrDrawingManager::newTextureResolveRenderTask(const GrCaps& caps) {
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600723 // 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 -0600724 // in sorting and opsTask reduction mode) the render tasks that depend on any proxy's current
Greg Danielf41b2bd2019-08-22 16:19:24 -0400725 // state. This is because those opsTasks can still receive new ops and because if they refer to
Chris Dalton4ece96d2019-08-30 11:26:39 -0600726 // the mipmapped version of 'proxy', they will then come to depend on the render task being
727 // created here.
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600728 //
Greg Danielf41b2bd2019-08-22 16:19:24 -0400729 // Add the new textureResolveTask before the fActiveOpsTask (if not in
730 // sorting/opsTask-splitting-reduction mode) because it will depend upon this resolve task.
Chris Dalton3d770272019-08-14 09:24:37 -0600731 // NOTE: Putting it here will also reduce the amount of work required by the topological sort.
Chris Daltone2a903e2019-09-18 13:41:50 -0600732 return static_cast<GrTextureResolveRenderTask*>(fDAG.addBeforeLast(
733 sk_make_sp<GrTextureResolveRenderTask>()));
Chris Dalton3d770272019-08-14 09:24:37 -0600734}
735
Greg Danielc30f1a92019-09-06 15:28:58 -0400736void GrDrawingManager::newWaitRenderTask(sk_sp<GrSurfaceProxy> proxy,
Greg Daniel301015c2019-11-18 14:06:46 -0500737 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> semaphores,
Greg Danielc30f1a92019-09-06 15:28:58 -0400738 int numSemaphores) {
739 SkDEBUGCODE(this->validate());
740 SkASSERT(fContext);
741
742 const GrCaps& caps = *fContext->priv().caps();
743
Greg Daniel16f5c652019-10-29 11:26:01 -0400744 sk_sp<GrWaitRenderTask> waitTask = sk_make_sp<GrWaitRenderTask>(GrSurfaceProxyView(proxy),
745 std::move(semaphores),
Greg Danielc30f1a92019-09-06 15:28:58 -0400746 numSemaphores);
747 if (fReduceOpsTaskSplitting) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400748 GrRenderTask* lastTask = this->getLastRenderTask(proxy.get());
Greg Danielc30f1a92019-09-06 15:28:58 -0400749 if (lastTask && !lastTask->isClosed()) {
750 // We directly make the currently open renderTask depend on waitTask instead of using
751 // the proxy version of addDependency. The waitTask will never need to trigger any
752 // resolves or mip map generation which is the main advantage of going through the proxy
753 // version. Additionally we would've had to temporarily set the wait task as the
754 // lastRenderTask on the proxy, add the dependency, and then reset the lastRenderTask to
755 // lastTask. Additionally we add all dependencies of lastTask to waitTask so that the
756 // waitTask doesn't get reordered before them and unnecessarily block those tasks.
757 // Note: Any previous Ops already in lastTask will get blocked by the wait semaphore
758 // even though they don't need to be for correctness.
759
760 // Make sure we add the dependencies of lastTask to waitTask first or else we'll get a
761 // circular self dependency of waitTask on waitTask.
762 waitTask->addDependenciesFromOtherTask(lastTask);
763 lastTask->addDependency(waitTask.get());
764 } else {
765 // If there is a last task we set the waitTask to depend on it so that it doesn't get
766 // reordered in front of the lastTask causing the lastTask to be blocked by the
767 // semaphore. Again we directly just go through adding the dependency to the task and
768 // not the proxy since we don't need to worry about resolving anything.
769 if (lastTask) {
770 waitTask->addDependency(lastTask);
771 }
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400772 this->setLastRenderTask(proxy.get(), waitTask.get());
Greg Danielc30f1a92019-09-06 15:28:58 -0400773 }
774 fDAG.add(waitTask);
775 } else {
Adlai Holler33d569e2020-06-16 14:30:08 -0400776 if (fActiveOpsTask && (fActiveOpsTask->target(0).proxy() == proxy.get())) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400777 SkASSERT(this->getLastRenderTask(proxy.get()) == fActiveOpsTask);
Greg Danielc30f1a92019-09-06 15:28:58 -0400778 fDAG.addBeforeLast(waitTask);
779 // In this case we keep the current renderTask open but just insert the new waitTask
780 // before it in the list. The waitTask will never need to trigger any resolves or mip
781 // map generation which is the main advantage of going through the proxy version.
782 // Additionally we would've had to temporarily set the wait task as the lastRenderTask
783 // on the proxy, add the dependency, and then reset the lastRenderTask to
784 // fActiveOpsTask. Additionally we make the waitTask depend on all of fActiveOpsTask
785 // dependencies so that we don't unnecessarily reorder the waitTask before them.
786 // Note: Any previous Ops already in fActiveOpsTask will get blocked by the wait
787 // semaphore even though they don't need to be for correctness.
788
789 // Make sure we add the dependencies of fActiveOpsTask to waitTask first or else we'll
790 // get a circular self dependency of waitTask on waitTask.
791 waitTask->addDependenciesFromOtherTask(fActiveOpsTask);
792 fActiveOpsTask->addDependency(waitTask.get());
793 } else {
794 // In this case we just close the previous RenderTask and start and append the waitTask
795 // to the DAG. Since it is the last task now we call setLastRenderTask on the proxy. If
796 // there is a lastTask on the proxy we make waitTask depend on that task. This
797 // dependency isn't strictly needed but it does keep the DAG from reordering the
798 // waitTask earlier and blocking more tasks.
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400799 if (GrRenderTask* lastTask = this->getLastRenderTask(proxy.get())) {
Greg Danielc30f1a92019-09-06 15:28:58 -0400800 waitTask->addDependency(lastTask);
801 }
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400802 this->setLastRenderTask(proxy.get(), waitTask.get());
Greg Danielc30f1a92019-09-06 15:28:58 -0400803 this->closeRenderTasksForNewRenderTask(proxy.get());
804 fDAG.add(waitTask);
805 }
806 }
807 waitTask->makeClosed(caps);
808
809 SkDEBUGCODE(this->validate());
810}
811
Greg Danielbbfec9d2019-08-20 10:56:51 -0400812void GrDrawingManager::newTransferFromRenderTask(sk_sp<GrSurfaceProxy> srcProxy,
813 const SkIRect& srcRect,
814 GrColorType surfaceColorType,
815 GrColorType dstColorType,
816 sk_sp<GrGpuBuffer> dstBuffer,
817 size_t dstOffset) {
818 SkDEBUGCODE(this->validate());
819 SkASSERT(fContext);
820 // This copies from srcProxy to dstBuffer so it doesn't have a real target.
821 this->closeRenderTasksForNewRenderTask(nullptr);
822
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600823 GrRenderTask* task = fDAG.add(sk_make_sp<GrTransferFromRenderTask>(
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400824 srcProxy, srcRect, surfaceColorType, dstColorType,
825 std::move(dstBuffer), dstOffset));
Greg Danielbbfec9d2019-08-20 10:56:51 -0400826
827 const GrCaps& caps = *fContext->priv().caps();
828
Brian Salomon7e67dca2020-07-21 09:27:25 -0400829 // We always say GrMipmapped::kNo here since we are always just copying from the base layer. We
Greg Danielbbfec9d2019-08-20 10:56:51 -0400830 // don't need to make sure the whole mip map chain is valid.
Brian Salomon7e67dca2020-07-21 09:27:25 -0400831 task->addDependency(this, srcProxy.get(), GrMipmapped::kNo,
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400832 GrTextureResolveManager(this), caps);
Greg Danielbbfec9d2019-08-20 10:56:51 -0400833 task->makeClosed(caps);
834
Greg Danielbbfec9d2019-08-20 10:56:51 -0400835 // We have closed the previous active oplist but since a new oplist isn't being added there
836 // shouldn't be an active one.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400837 SkASSERT(!fActiveOpsTask);
Greg Danielbbfec9d2019-08-20 10:56:51 -0400838 SkDEBUGCODE(this->validate());
839}
840
Greg Daniel16f5c652019-10-29 11:26:01 -0400841bool GrDrawingManager::newCopyRenderTask(GrSurfaceProxyView srcView,
Greg Daniele227fe42019-08-21 13:52:24 -0400842 const SkIRect& srcRect,
Greg Daniel16f5c652019-10-29 11:26:01 -0400843 GrSurfaceProxyView dstView,
Greg Daniele227fe42019-08-21 13:52:24 -0400844 const SkIPoint& dstPoint) {
845 SkDEBUGCODE(this->validate());
846 SkASSERT(fContext);
Greg Daniele227fe42019-08-21 13:52:24 -0400847
Greg Daniel16f5c652019-10-29 11:26:01 -0400848 this->closeRenderTasksForNewRenderTask(dstView.proxy());
Brian Salomone4bce012019-09-20 15:34:23 -0400849 const GrCaps& caps = *fContext->priv().caps();
850
Greg Daniel16f5c652019-10-29 11:26:01 -0400851 GrSurfaceProxy* srcProxy = srcView.proxy();
852
Brian Salomone4bce012019-09-20 15:34:23 -0400853 GrRenderTask* task =
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400854 fDAG.add(GrCopyRenderTask::Make(this, std::move(srcView), srcRect, std::move(dstView),
Greg Daniel16f5c652019-10-29 11:26:01 -0400855 dstPoint, &caps));
Greg Daniele227fe42019-08-21 13:52:24 -0400856 if (!task) {
857 return false;
858 }
859
Brian Salomon7e67dca2020-07-21 09:27:25 -0400860 // We always say GrMipmapped::kNo here since we are always just copying from the base layer to
Greg Daniele227fe42019-08-21 13:52:24 -0400861 // another base layer. We don't need to make sure the whole mip map chain is valid.
Brian Salomon7e67dca2020-07-21 09:27:25 -0400862 task->addDependency(this, srcProxy, GrMipmapped::kNo, GrTextureResolveManager(this), caps);
Greg Daniele227fe42019-08-21 13:52:24 -0400863 task->makeClosed(caps);
864
Greg Daniele227fe42019-08-21 13:52:24 -0400865 // We have closed the previous active oplist but since a new oplist isn't being added there
866 // shouldn't be an active one.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400867 SkASSERT(!fActiveOpsTask);
Greg Daniele227fe42019-08-21 13:52:24 -0400868 SkDEBUGCODE(this->validate());
869 return true;
870}
871
robertphillips68737822015-10-29 12:12:21 -0700872/*
873 * This method finds a path renderer that can draw the specified path on
874 * the provided target.
875 * Due to its expense, the software path renderer has split out so it can
876 * can be individually allowed/disallowed via the "allowSW" boolean.
877 */
878GrPathRenderer* GrDrawingManager::getPathRenderer(const GrPathRenderer::CanDrawPathArgs& args,
879 bool allowSW,
880 GrPathRendererChain::DrawType drawType,
881 GrPathRenderer::StencilSupport* stencilSupport) {
882
883 if (!fPathRendererChain) {
John Stilesfbd050b2020-08-03 13:21:46 -0400884 fPathRendererChain =
885 std::make_unique<GrPathRendererChain>(fContext, fOptionsForPathRendererChain);
robertphillips68737822015-10-29 12:12:21 -0700886 }
887
888 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(args, drawType, stencilSupport);
889 if (!pr && allowSW) {
Brian Salomone7df0bb2018-05-07 14:44:57 -0400890 auto swPR = this->getSoftwarePathRenderer();
891 if (GrPathRenderer::CanDrawPath::kNo != swPR->canDrawPath(args)) {
892 pr = swPR;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500893 }
robertphillips68737822015-10-29 12:12:21 -0700894 }
895
Robert Phillipsd81379d2020-04-21 10:39:02 -0400896#if GR_PATH_RENDERER_SPEW
897 if (pr) {
898 SkDebugf("getPathRenderer: %s\n", pr->name());
899 }
900#endif
901
robertphillips68737822015-10-29 12:12:21 -0700902 return pr;
903}
904
Brian Salomone7df0bb2018-05-07 14:44:57 -0400905GrPathRenderer* GrDrawingManager::getSoftwarePathRenderer() {
906 if (!fSoftwarePathRenderer) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400907 fSoftwarePathRenderer.reset(
Robert Phillips9da87e02019-02-04 13:26:26 -0500908 new GrSoftwarePathRenderer(fContext->priv().proxyProvider(),
Ben Wagner9ec70c62018-07-12 13:30:47 -0400909 fOptionsForPathRendererChain.fAllowPathMaskCaching));
Brian Salomone7df0bb2018-05-07 14:44:57 -0400910 }
Ben Wagner9ec70c62018-07-12 13:30:47 -0400911 return fSoftwarePathRenderer.get();
Brian Salomone7df0bb2018-05-07 14:44:57 -0400912}
913
Chris Daltonfddb6c02017-11-04 15:22:22 -0600914GrCoverageCountingPathRenderer* GrDrawingManager::getCoverageCountingPathRenderer() {
915 if (!fPathRendererChain) {
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500916 fPathRendererChain = std::make_unique<GrPathRendererChain>(fContext,
917 fOptionsForPathRendererChain);
Chris Daltonfddb6c02017-11-04 15:22:22 -0600918 }
919 return fPathRendererChain->getCoverageCountingPathRenderer();
920}
921
Brian Salomon653f42f2018-07-10 10:07:31 -0400922void GrDrawingManager::flushIfNecessary() {
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400923 auto direct = fContext->asDirectContext();
Robert Phillips6a6de562019-02-15 15:19:15 -0500924 if (!direct) {
925 return;
926 }
927
928 auto resourceCache = direct->priv().getResourceCache();
Brian Salomon653f42f2018-07-10 10:07:31 -0400929 if (resourceCache && resourceCache->requestsFlush()) {
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500930 if (this->flush({}, SkSurface::BackendSurfaceAccess::kNoAccess, GrFlushInfo(), nullptr)) {
Greg Danielfe159622020-04-10 17:43:51 +0000931 this->submitToGpu(false);
932 }
Brian Salomon57d2beab2018-09-10 09:35:41 -0400933 resourceCache->purgeAsNeeded();
Brian Salomon653f42f2018-07-10 10:07:31 -0400934 }
935}