blob: 79b6e456eea7d248f52fa7cf5b588e11efd05fa3 [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) {
Adlai Holler9902cff2020-11-11 08:51:25 -0500236 onFlushCBObject->preFlush(&onFlushProvider, fFlushingRenderTaskIDs);
Chris Daltonc4b47352019-08-23 10:10:36 -0600237 }
238 for (const auto& onFlushRenderTask : fOnFlushRenderTasks) {
239 onFlushRenderTask->makeClosed(*fContext->priv().caps());
Chris Dalton706a6ff2017-11-29 22:01:06 -0700240#ifdef SK_DEBUG
Chris Daltonc4b47352019-08-23 10:10:36 -0600241 // OnFlush callbacks are invoked during flush, and are therefore expected to handle
242 // resource allocation & usage on their own. (No deferred or lazy proxies!)
243 onFlushRenderTask->visitTargetAndSrcProxies_debugOnly(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400244 [](GrSurfaceProxy* p, GrMipmapped mipMapped) {
Chris Daltonc4b47352019-08-23 10:10:36 -0600245 SkASSERT(!p->asTextureProxy() || !p->asTextureProxy()->texPriv().isDeferred());
Brian Salomonbeb7f522019-08-30 16:19:42 -0400246 SkASSERT(!p->isLazy());
Chris Dalton4ece96d2019-08-30 11:26:39 -0600247 if (p->requiresManualMSAAResolve()) {
248 // The onFlush callback is responsible for ensuring MSAA gets resolved.
249 SkASSERT(p->asRenderTargetProxy() && !p->asRenderTargetProxy()->isMSAADirty());
250 }
Brian Salomon7e67dca2020-07-21 09:27:25 -0400251 if (GrMipmapped::kYes == mipMapped) {
Chris Daltonc4b47352019-08-23 10:10:36 -0600252 // The onFlush callback is responsible for regenerating mips if needed.
Brian Salomon8c82a872020-07-21 12:09:58 -0400253 SkASSERT(p->asTextureProxy() && !p->asTextureProxy()->mipmapsAreDirty());
Chris Daltonc4b47352019-08-23 10:10:36 -0600254 }
255 });
Chris Dalton706a6ff2017-11-29 22:01:06 -0700256#endif
Chris Daltonc4b47352019-08-23 10:10:36 -0600257 onFlushRenderTask->prepare(&flushState);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400258 }
259 }
260
robertphillipsa13e2022015-11-11 12:01:09 -0800261#if 0
Brian Salomon09d994e2016-12-21 11:14:46 -0500262 // Enable this to print out verbose GrOp information
Adlai Hollerabdfd392020-10-19 09:00:36 -0400263 SkDEBUGCODE(SkDebugf("onFlush renderTasks (%d):\n", fOnFlushRenderTasks.count()));
Chris Daltonc4b47352019-08-23 10:10:36 -0600264 for (const auto& onFlushRenderTask : fOnFlushRenderTasks) {
Adlai Hollerabdfd392020-10-19 09:00:36 -0400265 SkDEBUGCODE(onFlushRenderTask->dump(/* printDependencies */ true);)
Chris Daltonc4b47352019-08-23 10:10:36 -0600266 }
Adlai Hollerabdfd392020-10-19 09:00:36 -0400267 SkDEBUGCODE(SkDebugf("Normal renderTasks (%d):\n", fDAG.numRenderTasks()));
268 for (int i = 0; i < fDAG.numRenderTasks(); ++i) {
269 SkDEBUGCODE(fDAG.renderTask(i)->dump(/* printDependencies */ true);)
robertphillips3dc6ae52015-10-20 09:54:32 -0700270 }
robertphillipsa13e2022015-11-11 12:01:09 -0800271#endif
272
Robert Phillipseafd48a2017-11-16 07:52:08 -0500273 int startIndex, stopIndex;
274 bool flushed = false;
275
Robert Phillipsf8e25022017-11-08 15:24:31 -0500276 {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400277 GrResourceAllocator alloc(resourceProvider SkDEBUGCODE(, fDAG.numRenderTasks()));
Chris Dalton6b498102019-08-01 14:14:52 -0600278 for (int i = 0; i < fDAG.numRenderTasks(); ++i) {
279 if (fDAG.renderTask(i)) {
280 fDAG.renderTask(i)->gatherProxyIntervals(&alloc);
Robert Phillips22310d62018-09-05 11:07:21 -0400281 }
Greg Danielf41b2bd2019-08-22 16:19:24 -0400282 alloc.markEndOfOpsTask(i);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500283 }
Robert Phillipsc73666f2019-04-24 08:49:48 -0400284 alloc.determineRecyclability();
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400285
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500286 GrResourceAllocator::AssignError error = GrResourceAllocator::AssignError::kNoError;
Chris Dalton6b498102019-08-01 14:14:52 -0600287 int numRenderTasksExecuted = 0;
Brian Salomon577aa0f2018-11-30 13:32:23 -0500288 while (alloc.assign(&startIndex, &stopIndex, &error)) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500289 if (GrResourceAllocator::AssignError::kFailedProxyInstantiation == error) {
290 for (int i = startIndex; i < stopIndex; ++i) {
Chris Dalton6b498102019-08-01 14:14:52 -0600291 GrRenderTask* renderTask = fDAG.renderTask(i);
292 if (!renderTask) {
293 continue;
Robert Phillips01a91282018-07-26 08:03:04 -0400294 }
Chris Dalton6b498102019-08-01 14:14:52 -0600295 if (!renderTask->isInstantiated()) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400296 // No need to call the renderTask's handleInternalAllocationFailure
297 // since we will already skip executing the renderTask since it is not
298 // instantiated.
Chris Dalton6b498102019-08-01 14:14:52 -0600299 continue;
Robert Phillipsbbcb7f72018-05-31 11:16:19 -0400300 }
Chris Dalton6b498102019-08-01 14:14:52 -0600301 renderTask->handleInternalAllocationFailure();
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500302 }
Adlai Holler96ead542020-06-26 08:50:14 -0400303 this->removeRenderTasks(startIndex, stopIndex);
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500304 }
Robert Phillips4150eea2018-02-07 17:08:21 -0500305
Chris Dalton6b498102019-08-01 14:14:52 -0600306 if (this->executeRenderTasks(
307 startIndex, stopIndex, &flushState, &numRenderTasksExecuted)) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500308 flushed = true;
309 }
bsalomondc438982016-08-31 11:53:49 -0700310 }
Greg Danielc42b20b2017-10-04 10:34:49 -0400311 }
312
Chris Dalton91ab1552018-04-18 13:24:25 -0600313#ifdef SK_DEBUG
Chris Dalton6b498102019-08-01 14:14:52 -0600314 for (int i = 0; i < fDAG.numRenderTasks(); ++i) {
Adlai Holler96ead542020-06-26 08:50:14 -0400315 // All render tasks should have been cleared out by now – we only reset the array below to
316 // reclaim storage.
317 SkASSERT(!fDAG.renderTask(i));
Chris Dalton91ab1552018-04-18 13:24:25 -0600318 }
319#endif
Adlai Holler25df1f72020-06-09 13:08:27 -0400320 fLastRenderTasks.reset();
Robert Phillips22310d62018-09-05 11:07:21 -0400321 fDAG.reset();
Robert Phillips15c91422019-05-07 16:54:48 -0400322 this->clearDDLTargets();
robertphillipsa13e2022015-11-11 12:01:09 -0800323
Robert Phillipsc994a932018-06-19 13:09:54 -0400324#ifdef SK_DEBUG
325 // In non-DDL mode this checks that all the flushed ops have been freed from the memory pool.
326 // When we move to partial flushes this assert will no longer be valid.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400327 // In DDL mode this check is somewhat superfluous since the memory for most of the ops/opsTasks
Robert Phillipsc994a932018-06-19 13:09:54 -0400328 // will be stored in the DDL's GrOpMemoryPools.
Herb Derbye32e1ab2020-10-27 10:29:46 -0400329 GrMemoryPool* opMemoryPool = fContext->priv().opMemoryPool();
Robert Phillipsc994a932018-06-19 13:09:54 -0400330 opMemoryPool->isEmpty();
331#endif
332
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500333 gpu->executeFlushInfo(proxies, access, info, newState);
robertphillipsa13e2022015-11-11 12:01:09 -0800334
Brian Salomon57d2beab2018-09-10 09:35:41 -0400335 // Give the cache a chance to purge resources that become purgeable due to flushing.
336 if (flushed) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500337 resourceCache->purgeAsNeeded();
Brian Salomon876a0172019-03-08 11:12:14 -0500338 flushed = false;
bsalomonb77a9072016-09-07 10:02:04 -0700339 }
Chris Daltonfe199b72017-05-05 11:26:15 -0400340 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
Adlai Holler9902cff2020-11-11 08:51:25 -0500341 onFlushCBObject->postFlush(fTokenTracker.nextTokenToFlush(), fFlushingRenderTaskIDs);
Brian Salomon876a0172019-03-08 11:12:14 -0500342 flushed = true;
343 }
344 if (flushed) {
345 resourceCache->purgeAsNeeded();
Chris Daltonfe199b72017-05-05 11:26:15 -0400346 }
Chris Dalton6b498102019-08-01 14:14:52 -0600347 fFlushingRenderTaskIDs.reset();
joshualittb8918c42015-12-18 09:59:46 -0800348 fFlushing = false;
Greg Daniel51316782017-08-02 15:10:09 +0000349
Greg Danielfe159622020-04-10 17:43:51 +0000350 return true;
351}
352
353bool GrDrawingManager::submitToGpu(bool syncToCpu) {
354 if (fFlushing || this->wasAbandoned()) {
355 return false;
356 }
357
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400358 auto direct = fContext->asDirectContext();
Greg Danielfe159622020-04-10 17:43:51 +0000359 if (!direct) {
360 return false; // Can't submit while DDL recording
361 }
362 GrGpu* gpu = direct->priv().getGpu();
363 return gpu->submitToGpu(syncToCpu);
robertphillips3dc6ae52015-10-20 09:54:32 -0700364}
365
Chris Dalton6b498102019-08-01 14:14:52 -0600366bool GrDrawingManager::executeRenderTasks(int startIndex, int stopIndex, GrOpFlushState* flushState,
367 int* numRenderTasksExecuted) {
368 SkASSERT(startIndex <= stopIndex && stopIndex <= fDAG.numRenderTasks());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500369
Robert Phillips27483912018-04-20 12:43:18 -0400370#if GR_FLUSH_TIME_OP_SPEW
Greg Danielf41b2bd2019-08-22 16:19:24 -0400371 SkDebugf("Flushing opsTask: %d to %d out of [%d, %d]\n",
Chris Dalton6b498102019-08-01 14:14:52 -0600372 startIndex, stopIndex, 0, fDAG.numRenderTasks());
Robert Phillips27483912018-04-20 12:43:18 -0400373 for (int i = startIndex; i < stopIndex; ++i) {
Chris Dalton6b498102019-08-01 14:14:52 -0600374 if (fDAG.renderTask(i)) {
375 fDAG.renderTask(i)->dump(true);
Robert Phillips1734dd32018-08-21 13:52:09 -0400376 }
Robert Phillips27483912018-04-20 12:43:18 -0400377 }
378#endif
379
Chris Dalton6b498102019-08-01 14:14:52 -0600380 bool anyRenderTasksExecuted = false;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500381
382 for (int i = startIndex; i < stopIndex; ++i) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400383 GrRenderTask* renderTask = fDAG.renderTask(i);
384 if (!renderTask || !renderTask->isInstantiated()) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500385 continue;
386 }
387
Chris Dalton6b498102019-08-01 14:14:52 -0600388 SkASSERT(renderTask->deferredProxiesAreInstantiated());
Robert Phillips22310d62018-09-05 11:07:21 -0400389
Chris Dalton6b498102019-08-01 14:14:52 -0600390 renderTask->prepare(flushState);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500391 }
392
393 // Upload all data to the GPU
394 flushState->preExecuteDraws();
395
Greg Danield2073452018-12-07 11:20:33 -0500396 // For Vulkan, if we have too many oplists to be flushed we end up allocating a lot of resources
397 // for each command buffer associated with the oplists. If this gets too large we can cause the
398 // devices to go OOM. In practice we usually only hit this case in our tests, but to be safe we
399 // put a cap on the number of oplists we will execute before flushing to the GPU to relieve some
400 // memory pressure.
Chris Dalton6b498102019-08-01 14:14:52 -0600401 static constexpr int kMaxRenderTasksBeforeFlush = 100;
Greg Danield2073452018-12-07 11:20:33 -0500402
Chris Daltonc4b47352019-08-23 10:10:36 -0600403 // Execute the onFlush renderTasks first, if any.
404 for (sk_sp<GrRenderTask>& onFlushRenderTask : fOnFlushRenderTasks) {
405 if (!onFlushRenderTask->execute(flushState)) {
406 SkDebugf("WARNING: onFlushRenderTask failed to execute.\n");
Robert Phillipseafd48a2017-11-16 07:52:08 -0500407 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600408 SkASSERT(onFlushRenderTask->unique());
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400409 onFlushRenderTask->disown(this);
Chris Daltonc4b47352019-08-23 10:10:36 -0600410 onFlushRenderTask = nullptr;
Chris Dalton6b498102019-08-01 14:14:52 -0600411 (*numRenderTasksExecuted)++;
412 if (*numRenderTasksExecuted >= kMaxRenderTasksBeforeFlush) {
Greg Danielfe159622020-04-10 17:43:51 +0000413 flushState->gpu()->submitToGpu(false);
Chris Dalton6b498102019-08-01 14:14:52 -0600414 *numRenderTasksExecuted = 0;
Greg Danield2073452018-12-07 11:20:33 -0500415 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500416 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600417 fOnFlushRenderTasks.reset();
Robert Phillipseafd48a2017-11-16 07:52:08 -0500418
419 // Execute the normal op lists.
420 for (int i = startIndex; i < stopIndex; ++i) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400421 GrRenderTask* renderTask = fDAG.renderTask(i);
422 if (!renderTask || !renderTask->isInstantiated()) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500423 continue;
424 }
425
Greg Daniel15ecdf92019-08-30 15:35:23 -0400426 if (renderTask->execute(flushState)) {
Chris Dalton6b498102019-08-01 14:14:52 -0600427 anyRenderTasksExecuted = true;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500428 }
Chris Dalton6b498102019-08-01 14:14:52 -0600429 (*numRenderTasksExecuted)++;
430 if (*numRenderTasksExecuted >= kMaxRenderTasksBeforeFlush) {
Greg Danielfe159622020-04-10 17:43:51 +0000431 flushState->gpu()->submitToGpu(false);
Chris Dalton6b498102019-08-01 14:14:52 -0600432 *numRenderTasksExecuted = 0;
Greg Danield2073452018-12-07 11:20:33 -0500433 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500434 }
435
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400436 SkASSERT(!flushState->opsRenderPass());
Robert Phillips40a29d72018-01-18 12:59:22 -0500437 SkASSERT(fTokenTracker.nextDrawToken() == fTokenTracker.nextTokenToFlush());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500438
Chris Dalton6b498102019-08-01 14:14:52 -0600439 // We reset the flush state before the RenderTasks so that the last resources to be freed are
440 // those that are written to in the RenderTasks. This helps to make sure the most recently used
441 // resources are the last to be purged by the resource cache.
Robert Phillipseafd48a2017-11-16 07:52:08 -0500442 flushState->reset();
443
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400444 this->removeRenderTasks(startIndex, stopIndex);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500445
Chris Dalton6b498102019-08-01 14:14:52 -0600446 return anyRenderTasksExecuted;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500447}
448
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400449void GrDrawingManager::removeRenderTasks(int startIndex, int stopIndex) {
450 for (int i = startIndex; i < stopIndex; ++i) {
451 GrRenderTask* task = fDAG.renderTask(i);
452 if (!task) {
453 continue;
454 }
Robert Phillips02dd0ed2020-11-09 17:03:34 -0500455 if (!task->unique() || task->requiresExplicitCleanup()) {
456 // TODO: Eventually uniqueness should be guaranteed: http://skbug.com/7111.
457 // DDLs, however, will always require an explicit notification for when they
458 // can clean up resources.
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400459 task->endFlush(this);
460 }
461 task->disown(this);
462 }
Adlai Holler96ead542020-06-26 08:50:14 -0400463 fDAG.rawRemoveRenderTasks(startIndex, stopIndex);
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400464}
465
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400466static void resolve_and_mipmap(GrGpu* gpu, GrSurfaceProxy* proxy) {
467 if (!proxy->isInstantiated()) {
468 return;
469 }
470
471 // In the flushSurfaces case, we need to resolve MSAA immediately after flush. This is
472 // because clients expect the flushed surface's backing texture to be fully resolved
473 // upon return.
474 if (proxy->requiresManualMSAAResolve()) {
475 auto* rtProxy = proxy->asRenderTargetProxy();
476 SkASSERT(rtProxy);
477 if (rtProxy->isMSAADirty()) {
478 SkASSERT(rtProxy->peekRenderTarget());
Jim Van Verthbb61fe32020-07-07 16:39:04 -0400479 gpu->resolveRenderTarget(rtProxy->peekRenderTarget(), rtProxy->msaaDirtyRect());
480 gpu->submitToGpu(false);
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400481 rtProxy->markMSAAResolved();
482 }
483 }
484 // If, after a flush, any of the proxies of interest have dirty mipmaps, regenerate them in
485 // case their backend textures are being stolen.
486 // (This special case is exercised by the ReimportImageTextureWithMipLevels test.)
487 // FIXME: It may be more ideal to plumb down a "we're going to steal the backends" flag.
488 if (auto* textureProxy = proxy->asTextureProxy()) {
Brian Salomon8c82a872020-07-21 12:09:58 -0400489 if (textureProxy->mipmapsAreDirty()) {
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400490 SkASSERT(textureProxy->peekTexture());
491 gpu->regenerateMipMapLevels(textureProxy->peekTexture());
Brian Salomon8c82a872020-07-21 12:09:58 -0400492 textureProxy->markMipmapsClean();
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400493 }
494 }
495}
496
Greg Daniel9efe3862020-06-11 11:51:06 -0400497GrSemaphoresSubmitted GrDrawingManager::flushSurfaces(
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500498 SkSpan<GrSurfaceProxy*> proxies,
Greg Daniel9efe3862020-06-11 11:51:06 -0400499 SkSurface::BackendSurfaceAccess access,
500 const GrFlushInfo& info,
501 const GrBackendSurfaceMutableState* newState) {
bsalomon6a2b1942016-09-08 11:28:59 -0700502 if (this->wasAbandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -0400503 if (info.fSubmittedProc) {
504 info.fSubmittedProc(info.fSubmittedContext, false);
505 }
506 if (info.fFinishedProc) {
507 info.fFinishedProc(info.fFinishedContext);
508 }
Greg Daniel51316782017-08-02 15:10:09 +0000509 return GrSemaphoresSubmitted::kNo;
bsalomon6a2b1942016-09-08 11:28:59 -0700510 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400511 SkDEBUGCODE(this->validate());
bsalomon6a2b1942016-09-08 11:28:59 -0700512
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400513 auto direct = fContext->asDirectContext();
Adlai Holleraee25fd2020-11-06 12:34:56 -0500514 SkASSERT(direct);
Robert Phillips6a6de562019-02-15 15:19:15 -0500515 GrGpu* gpu = direct->priv().getGpu();
Greg Daniel55822f12020-05-26 11:26:45 -0400516 // We have a non abandoned and direct GrContext. It must have a GrGpu.
517 SkASSERT(gpu);
Robert Phillips874b5352018-03-16 08:48:24 -0400518
Robert Phillipsacc10fa2019-04-01 09:50:20 -0400519 // TODO: It is important to upgrade the drawingmanager to just flushing the
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400520 // portion of the DAG required by 'proxies' in order to restore some of the
Robert Phillipsacc10fa2019-04-01 09:50:20 -0400521 // semantics of this method.
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500522 bool didFlush = this->flush(proxies, access, info, newState);
523 for (GrSurfaceProxy* proxy : proxies) {
524 resolve_and_mipmap(gpu, proxy);
bsalomon6a2b1942016-09-08 11:28:59 -0700525 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400526
527 SkDEBUGCODE(this->validate());
Greg Danielfe159622020-04-10 17:43:51 +0000528
Greg Daniel04283f32020-05-20 13:16:00 -0400529 if (!didFlush || (!direct->priv().caps()->semaphoreSupport() && info.fNumSemaphores)) {
Greg Danielfe159622020-04-10 17:43:51 +0000530 return GrSemaphoresSubmitted::kNo;
531 }
532 return GrSemaphoresSubmitted::kYes;
bsalomon6a2b1942016-09-08 11:28:59 -0700533}
534
Chris Daltonfe199b72017-05-05 11:26:15 -0400535void GrDrawingManager::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
536 fOnFlushCBObjects.push_back(onFlushCBObject);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400537}
538
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500539#if GR_TEST_UTILS
540void GrDrawingManager::testingOnly_removeOnFlushCallbackObject(GrOnFlushCallbackObject* cb) {
541 int n = std::find(fOnFlushCBObjects.begin(), fOnFlushCBObjects.end(), cb) -
542 fOnFlushCBObjects.begin();
543 SkASSERT(n < fOnFlushCBObjects.count());
544 fOnFlushCBObjects.removeShuffle(n);
545}
546#endif
547
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400548void GrDrawingManager::setLastRenderTask(const GrSurfaceProxy* proxy, GrRenderTask* task) {
549#ifdef SK_DEBUG
Robert Phillips02dd0ed2020-11-09 17:03:34 -0500550 if (auto prior = this->getLastRenderTask(proxy)) {
551 SkASSERT(prior->isClosed() || prior == task);
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400552 }
553#endif
Adlai Holler25df1f72020-06-09 13:08:27 -0400554 uint32_t key = proxy->uniqueID().asUInt();
555 if (task) {
556 fLastRenderTasks.set(key, task);
557 } else if (fLastRenderTasks.find(key)) {
558 fLastRenderTasks.remove(key);
559 }
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400560}
561
562GrRenderTask* GrDrawingManager::getLastRenderTask(const GrSurfaceProxy* proxy) const {
563 auto entry = fLastRenderTasks.find(proxy->uniqueID().asUInt());
564 return entry ? *entry : nullptr;
565}
566
567GrOpsTask* GrDrawingManager::getLastOpsTask(const GrSurfaceProxy* proxy) const {
568 GrRenderTask* task = this->getLastRenderTask(proxy);
569 return task ? task->asOpsTask() : nullptr;
570}
571
572
Chris Dalton6b498102019-08-01 14:14:52 -0600573void GrDrawingManager::moveRenderTasksToDDL(SkDeferredDisplayList* ddl) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400574 SkDEBUGCODE(this->validate());
575
Chris Dalton6b498102019-08-01 14:14:52 -0600576 // no renderTask should receive a new command after this
Robert Phillips9da87e02019-02-04 13:26:26 -0500577 fDAG.closeAll(fContext->priv().caps());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400578 fActiveOpsTask = nullptr;
Robert Phillips7a137052018-02-01 11:23:12 -0500579
Robert Phillips02dd0ed2020-11-09 17:03:34 -0500580 fDAG.prepForFlush();
581
Chris Dalton6b498102019-08-01 14:14:52 -0600582 fDAG.swap(&ddl->fRenderTasks);
Robert Phillips19f466d2020-02-26 10:27:07 -0500583 SkASSERT(!fDAG.numRenderTasks());
Robert Phillips867ce8f2018-06-21 10:28:36 -0400584
Robert Phillips19f466d2020-02-26 10:27:07 -0500585 for (auto& renderTask : ddl->fRenderTasks) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400586 renderTask->disown(this);
Robert Phillips29f38542019-10-16 09:20:25 -0400587 renderTask->prePrepare(fContext);
Robert Phillips7327c9d2019-10-08 16:32:56 -0400588 }
589
Michael Ludwig2c316bd2019-12-19 14:50:44 -0500590 ddl->fArenas = std::move(fContext->priv().detachArenas());
Robert Phillips61fc7992019-10-22 11:58:17 -0400591
Robert Phillipsf6a0b452020-02-18 14:26:46 -0500592 fContext->priv().detachProgramData(&ddl->fProgramData);
Robert Phillips576b6a12019-12-06 13:05:49 -0500593
Robert Phillips774168e2018-05-31 12:43:27 -0400594 if (fPathRendererChain) {
595 if (auto ccpr = fPathRendererChain->getCoverageCountingPathRenderer()) {
596 ddl->fPendingPaths = ccpr->detachPendingPaths();
597 }
598 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400599
600 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500601}
602
Robert Phillips02dd0ed2020-11-09 17:03:34 -0500603void GrDrawingManager::createDDLTask(sk_sp<const SkDeferredDisplayList> ddl,
604 GrRenderTargetProxy* newDest) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400605 SkDEBUGCODE(this->validate());
606
Greg Danielf41b2bd2019-08-22 16:19:24 -0400607 if (fActiveOpsTask) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400608 // This is a temporary fix for the partial-MDB world. In that world we're not
Greg Danielf41b2bd2019-08-22 16:19:24 -0400609 // reordering so ops that (in the single opsTask world) would've just glommed onto the
610 // end of the single opsTask but referred to a far earlier RT need to appear in their
611 // own opsTask.
612 fActiveOpsTask->makeClosed(*fContext->priv().caps());
613 fActiveOpsTask = nullptr;
Robert Phillips38d64b02018-09-04 13:23:26 -0400614 }
615
Robert Phillips02dd0ed2020-11-09 17:03:34 -0500616 // Propagate the DDL proxy's state information to the replay target.
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400617 if (ddl->priv().targetProxy()->isMSAADirty()) {
618 newDest->markMSAADirty(ddl->priv().targetProxy()->msaaDirtyRect(),
619 ddl->characterization().origin());
620 }
621 GrTextureProxy* newTextureProxy = newDest->asTextureProxy();
Brian Salomon8c82a872020-07-21 12:09:58 -0400622 if (newTextureProxy && GrMipmapped::kYes == newTextureProxy->mipmapped()) {
623 newTextureProxy->markMipmapsDirty();
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400624 }
625
626 this->addDDLTarget(newDest, ddl->priv().targetProxy());
Robert Phillips15c91422019-05-07 16:54:48 -0400627
Robert Phillips62000362018-02-01 09:10:04 -0500628 // Here we jam the proxy that backs the current replay SkSurface into the LazyProxyData.
Robert Phillips02dd0ed2020-11-09 17:03:34 -0500629 // The lazy proxy that references it (in the DDL opsTasks) will then steal its GrTexture.
Robert Phillips62000362018-02-01 09:10:04 -0500630 ddl->fLazyProxyData->fReplayDest = newDest;
Robert Phillips774168e2018-05-31 12:43:27 -0400631
632 if (ddl->fPendingPaths.size()) {
633 GrCoverageCountingPathRenderer* ccpr = this->getCoverageCountingPathRenderer();
634
635 ccpr->mergePendingPaths(ddl->fPendingPaths);
636 }
Robert Phillips22310d62018-09-05 11:07:21 -0400637
Robert Phillips02dd0ed2020-11-09 17:03:34 -0500638 // Add a task to handle drawing and lifetime management of the DDL.
639 SkDEBUGCODE(auto ddlTask =) fDAG.add(sk_make_sp<GrDDLTask>(this,
640 sk_ref_sp(newDest),
641 std::move(ddl)));
642 SkASSERT(ddlTask->isClosed());
Adlai Holler7580ad42020-06-24 13:45:25 -0400643
Robert Phillips38d64b02018-09-04 13:23:26 -0400644 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500645}
646
Robert Phillips38d64b02018-09-04 13:23:26 -0400647#ifdef SK_DEBUG
648void GrDrawingManager::validate() const {
Adlai Holler3078f852020-11-05 15:44:50 -0500649 if (fReduceOpsTaskSplitting) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400650 SkASSERT(!fActiveOpsTask);
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400651 } else {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400652 if (fActiveOpsTask) {
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400653 SkASSERT(!fDAG.empty());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400654 SkASSERT(!fActiveOpsTask->isClosed());
655 SkASSERT(fActiveOpsTask == fDAG.back());
Robert Phillips38d64b02018-09-04 13:23:26 -0400656 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400657
Chris Dalton6b498102019-08-01 14:14:52 -0600658 for (int i = 0; i < fDAG.numRenderTasks(); ++i) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400659 if (fActiveOpsTask != fDAG.renderTask(i)) {
Chris Daltone2a903e2019-09-18 13:41:50 -0600660 // The resolveTask associated with the activeTask remains open for as long as the
661 // activeTask does.
662 bool isActiveResolveTask =
663 fActiveOpsTask && fActiveOpsTask->fTextureResolveTask == fDAG.renderTask(i);
664 SkASSERT(isActiveResolveTask || fDAG.renderTask(i)->isClosed());
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400665 }
666 }
667
668 if (!fDAG.empty() && !fDAG.back()->isClosed()) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400669 SkASSERT(fActiveOpsTask == fDAG.back());
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400670 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400671 }
672}
673#endif
674
Greg Danielbbfec9d2019-08-20 10:56:51 -0400675void GrDrawingManager::closeRenderTasksForNewRenderTask(GrSurfaceProxy* target) {
Adlai Holler3078f852020-11-05 15:44:50 -0500676 if (target && fReduceOpsTaskSplitting) {
Chris Dalton5fe99772019-08-06 11:57:39 -0600677 // In this case we need to close all the renderTasks that rely on the current contents of
678 // 'target'. That is bc we're going to update the content of the proxy so they need to be
679 // split in case they use both the old and new content. (This is a bit of an overkill: they
680 // really only need to be split if they ever reference proxy's contents again but that is
681 // hard to predict/handle).
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400682 if (GrRenderTask* lastRenderTask = this->getLastRenderTask(target)) {
Chris Dalton6b498102019-08-01 14:14:52 -0600683 lastRenderTask->closeThoseWhoDependOnMe(*fContext->priv().caps());
Robert Phillips46acf9d2018-10-09 09:31:40 -0400684 }
Greg Danielf41b2bd2019-08-22 16:19:24 -0400685 } else if (fActiveOpsTask) {
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400686 // This is a temporary fix for the partial-MDB world. In that world we're not
Greg Danielf41b2bd2019-08-22 16:19:24 -0400687 // reordering so ops that (in the single opsTask world) would've just glommed onto the
688 // end of the single opsTask but referred to a far earlier RT need to appear in their
689 // own opsTask.
690 fActiveOpsTask->makeClosed(*fContext->priv().caps());
691 fActiveOpsTask = nullptr;
robertphillips3dc6ae52015-10-20 09:54:32 -0700692 }
Chris Dalton5fe99772019-08-06 11:57:39 -0600693}
694
Greg Daniel16f5c652019-10-29 11:26:01 -0400695sk_sp<GrOpsTask> GrDrawingManager::newOpsTask(GrSurfaceProxyView surfaceView,
696 bool managedOpsTask) {
Chris Dalton5fe99772019-08-06 11:57:39 -0600697 SkDEBUGCODE(this->validate());
698 SkASSERT(fContext);
699
Greg Daniel16f5c652019-10-29 11:26:01 -0400700 GrSurfaceProxy* proxy = surfaceView.proxy();
701 this->closeRenderTasksForNewRenderTask(proxy);
robertphillips3dc6ae52015-10-20 09:54:32 -0700702
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400703 sk_sp<GrOpsTask> opsTask(new GrOpsTask(this, fContext->priv().arenas(),
Michael Ludwigd0840ec2019-12-12 09:48:38 -0500704 std::move(surfaceView),
705 fContext->priv().auditTrail()));
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400706 SkASSERT(this->getLastRenderTask(proxy) == opsTask.get());
robertphillips3dc6ae52015-10-20 09:54:32 -0700707
Greg Danielf41b2bd2019-08-22 16:19:24 -0400708 if (managedOpsTask) {
709 fDAG.add(opsTask);
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400710
Adlai Holler3078f852020-11-05 15:44:50 -0500711 if (!fReduceOpsTaskSplitting) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400712 fActiveOpsTask = opsTask.get();
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400713 }
Robert Phillips941d1442017-06-14 16:37:02 -0400714 }
robertphillips3dc6ae52015-10-20 09:54:32 -0700715
Robert Phillips38d64b02018-09-04 13:23:26 -0400716 SkDEBUGCODE(this->validate());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400717 return opsTask;
robertphillips3dc6ae52015-10-20 09:54:32 -0700718}
719
Chris Daltone2a903e2019-09-18 13:41:50 -0600720GrTextureResolveRenderTask* GrDrawingManager::newTextureResolveRenderTask(const GrCaps& caps) {
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600721 // 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 -0600722 // in sorting and opsTask reduction mode) the render tasks that depend on any proxy's current
Greg Danielf41b2bd2019-08-22 16:19:24 -0400723 // state. This is because those opsTasks can still receive new ops and because if they refer to
Chris Dalton4ece96d2019-08-30 11:26:39 -0600724 // the mipmapped version of 'proxy', they will then come to depend on the render task being
725 // created here.
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600726 //
Greg Danielf41b2bd2019-08-22 16:19:24 -0400727 // Add the new textureResolveTask before the fActiveOpsTask (if not in
728 // sorting/opsTask-splitting-reduction mode) because it will depend upon this resolve task.
Chris Dalton3d770272019-08-14 09:24:37 -0600729 // NOTE: Putting it here will also reduce the amount of work required by the topological sort.
Chris Daltone2a903e2019-09-18 13:41:50 -0600730 return static_cast<GrTextureResolveRenderTask*>(fDAG.addBeforeLast(
731 sk_make_sp<GrTextureResolveRenderTask>()));
Chris Dalton3d770272019-08-14 09:24:37 -0600732}
733
Greg Danielc30f1a92019-09-06 15:28:58 -0400734void GrDrawingManager::newWaitRenderTask(sk_sp<GrSurfaceProxy> proxy,
Greg Daniel301015c2019-11-18 14:06:46 -0500735 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> semaphores,
Greg Danielc30f1a92019-09-06 15:28:58 -0400736 int numSemaphores) {
737 SkDEBUGCODE(this->validate());
738 SkASSERT(fContext);
739
740 const GrCaps& caps = *fContext->priv().caps();
741
Greg Daniel16f5c652019-10-29 11:26:01 -0400742 sk_sp<GrWaitRenderTask> waitTask = sk_make_sp<GrWaitRenderTask>(GrSurfaceProxyView(proxy),
743 std::move(semaphores),
Greg Danielc30f1a92019-09-06 15:28:58 -0400744 numSemaphores);
745 if (fReduceOpsTaskSplitting) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400746 GrRenderTask* lastTask = this->getLastRenderTask(proxy.get());
Greg Danielc30f1a92019-09-06 15:28:58 -0400747 if (lastTask && !lastTask->isClosed()) {
748 // We directly make the currently open renderTask depend on waitTask instead of using
749 // the proxy version of addDependency. The waitTask will never need to trigger any
750 // resolves or mip map generation which is the main advantage of going through the proxy
751 // version. Additionally we would've had to temporarily set the wait task as the
752 // lastRenderTask on the proxy, add the dependency, and then reset the lastRenderTask to
753 // lastTask. Additionally we add all dependencies of lastTask to waitTask so that the
754 // waitTask doesn't get reordered before them and unnecessarily block those tasks.
755 // Note: Any previous Ops already in lastTask will get blocked by the wait semaphore
756 // even though they don't need to be for correctness.
757
758 // Make sure we add the dependencies of lastTask to waitTask first or else we'll get a
759 // circular self dependency of waitTask on waitTask.
760 waitTask->addDependenciesFromOtherTask(lastTask);
761 lastTask->addDependency(waitTask.get());
762 } else {
763 // If there is a last task we set the waitTask to depend on it so that it doesn't get
764 // reordered in front of the lastTask causing the lastTask to be blocked by the
765 // semaphore. Again we directly just go through adding the dependency to the task and
766 // not the proxy since we don't need to worry about resolving anything.
767 if (lastTask) {
768 waitTask->addDependency(lastTask);
769 }
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400770 this->setLastRenderTask(proxy.get(), waitTask.get());
Greg Danielc30f1a92019-09-06 15:28:58 -0400771 }
772 fDAG.add(waitTask);
773 } else {
Adlai Holler33d569e2020-06-16 14:30:08 -0400774 if (fActiveOpsTask && (fActiveOpsTask->target(0).proxy() == proxy.get())) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400775 SkASSERT(this->getLastRenderTask(proxy.get()) == fActiveOpsTask);
Greg Danielc30f1a92019-09-06 15:28:58 -0400776 fDAG.addBeforeLast(waitTask);
777 // In this case we keep the current renderTask open but just insert the new waitTask
778 // before it in the list. The waitTask will never need to trigger any resolves or mip
779 // map generation which is the main advantage of going through the proxy version.
780 // Additionally we would've had to temporarily set the wait task as the lastRenderTask
781 // on the proxy, add the dependency, and then reset the lastRenderTask to
782 // fActiveOpsTask. Additionally we make the waitTask depend on all of fActiveOpsTask
783 // dependencies so that we don't unnecessarily reorder the waitTask before them.
784 // Note: Any previous Ops already in fActiveOpsTask will get blocked by the wait
785 // semaphore even though they don't need to be for correctness.
786
787 // Make sure we add the dependencies of fActiveOpsTask to waitTask first or else we'll
788 // get a circular self dependency of waitTask on waitTask.
789 waitTask->addDependenciesFromOtherTask(fActiveOpsTask);
790 fActiveOpsTask->addDependency(waitTask.get());
791 } else {
792 // In this case we just close the previous RenderTask and start and append the waitTask
793 // to the DAG. Since it is the last task now we call setLastRenderTask on the proxy. If
794 // there is a lastTask on the proxy we make waitTask depend on that task. This
795 // dependency isn't strictly needed but it does keep the DAG from reordering the
796 // waitTask earlier and blocking more tasks.
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400797 if (GrRenderTask* lastTask = this->getLastRenderTask(proxy.get())) {
Greg Danielc30f1a92019-09-06 15:28:58 -0400798 waitTask->addDependency(lastTask);
799 }
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400800 this->setLastRenderTask(proxy.get(), waitTask.get());
Greg Danielc30f1a92019-09-06 15:28:58 -0400801 this->closeRenderTasksForNewRenderTask(proxy.get());
802 fDAG.add(waitTask);
803 }
804 }
805 waitTask->makeClosed(caps);
806
807 SkDEBUGCODE(this->validate());
808}
809
Greg Danielbbfec9d2019-08-20 10:56:51 -0400810void GrDrawingManager::newTransferFromRenderTask(sk_sp<GrSurfaceProxy> srcProxy,
811 const SkIRect& srcRect,
812 GrColorType surfaceColorType,
813 GrColorType dstColorType,
814 sk_sp<GrGpuBuffer> dstBuffer,
815 size_t dstOffset) {
816 SkDEBUGCODE(this->validate());
817 SkASSERT(fContext);
818 // This copies from srcProxy to dstBuffer so it doesn't have a real target.
819 this->closeRenderTasksForNewRenderTask(nullptr);
820
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600821 GrRenderTask* task = fDAG.add(sk_make_sp<GrTransferFromRenderTask>(
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400822 srcProxy, srcRect, surfaceColorType, dstColorType,
823 std::move(dstBuffer), dstOffset));
Greg Danielbbfec9d2019-08-20 10:56:51 -0400824
825 const GrCaps& caps = *fContext->priv().caps();
826
Brian Salomon7e67dca2020-07-21 09:27:25 -0400827 // We always say GrMipmapped::kNo here since we are always just copying from the base layer. We
Greg Danielbbfec9d2019-08-20 10:56:51 -0400828 // don't need to make sure the whole mip map chain is valid.
Brian Salomon7e67dca2020-07-21 09:27:25 -0400829 task->addDependency(this, srcProxy.get(), GrMipmapped::kNo,
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400830 GrTextureResolveManager(this), caps);
Greg Danielbbfec9d2019-08-20 10:56:51 -0400831 task->makeClosed(caps);
832
Greg Danielbbfec9d2019-08-20 10:56:51 -0400833 // We have closed the previous active oplist but since a new oplist isn't being added there
834 // shouldn't be an active one.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400835 SkASSERT(!fActiveOpsTask);
Greg Danielbbfec9d2019-08-20 10:56:51 -0400836 SkDEBUGCODE(this->validate());
837}
838
Greg Daniel16f5c652019-10-29 11:26:01 -0400839bool GrDrawingManager::newCopyRenderTask(GrSurfaceProxyView srcView,
Greg Daniele227fe42019-08-21 13:52:24 -0400840 const SkIRect& srcRect,
Greg Daniel16f5c652019-10-29 11:26:01 -0400841 GrSurfaceProxyView dstView,
Greg Daniele227fe42019-08-21 13:52:24 -0400842 const SkIPoint& dstPoint) {
843 SkDEBUGCODE(this->validate());
844 SkASSERT(fContext);
Greg Daniele227fe42019-08-21 13:52:24 -0400845
Greg Daniel16f5c652019-10-29 11:26:01 -0400846 this->closeRenderTasksForNewRenderTask(dstView.proxy());
Brian Salomone4bce012019-09-20 15:34:23 -0400847 const GrCaps& caps = *fContext->priv().caps();
848
Greg Daniel16f5c652019-10-29 11:26:01 -0400849 GrSurfaceProxy* srcProxy = srcView.proxy();
850
Brian Salomone4bce012019-09-20 15:34:23 -0400851 GrRenderTask* task =
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400852 fDAG.add(GrCopyRenderTask::Make(this, std::move(srcView), srcRect, std::move(dstView),
Greg Daniel16f5c652019-10-29 11:26:01 -0400853 dstPoint, &caps));
Greg Daniele227fe42019-08-21 13:52:24 -0400854 if (!task) {
855 return false;
856 }
857
Brian Salomon7e67dca2020-07-21 09:27:25 -0400858 // We always say GrMipmapped::kNo here since we are always just copying from the base layer to
Greg Daniele227fe42019-08-21 13:52:24 -0400859 // another base layer. We don't need to make sure the whole mip map chain is valid.
Brian Salomon7e67dca2020-07-21 09:27:25 -0400860 task->addDependency(this, srcProxy, GrMipmapped::kNo, GrTextureResolveManager(this), caps);
Greg Daniele227fe42019-08-21 13:52:24 -0400861 task->makeClosed(caps);
862
Greg Daniele227fe42019-08-21 13:52:24 -0400863 // We have closed the previous active oplist but since a new oplist isn't being added there
864 // shouldn't be an active one.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400865 SkASSERT(!fActiveOpsTask);
Greg Daniele227fe42019-08-21 13:52:24 -0400866 SkDEBUGCODE(this->validate());
867 return true;
868}
869
robertphillips68737822015-10-29 12:12:21 -0700870/*
871 * This method finds a path renderer that can draw the specified path on
872 * the provided target.
873 * Due to its expense, the software path renderer has split out so it can
874 * can be individually allowed/disallowed via the "allowSW" boolean.
875 */
876GrPathRenderer* GrDrawingManager::getPathRenderer(const GrPathRenderer::CanDrawPathArgs& args,
877 bool allowSW,
878 GrPathRendererChain::DrawType drawType,
879 GrPathRenderer::StencilSupport* stencilSupport) {
880
881 if (!fPathRendererChain) {
John Stilesfbd050b2020-08-03 13:21:46 -0400882 fPathRendererChain =
883 std::make_unique<GrPathRendererChain>(fContext, fOptionsForPathRendererChain);
robertphillips68737822015-10-29 12:12:21 -0700884 }
885
886 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(args, drawType, stencilSupport);
887 if (!pr && allowSW) {
Brian Salomone7df0bb2018-05-07 14:44:57 -0400888 auto swPR = this->getSoftwarePathRenderer();
889 if (GrPathRenderer::CanDrawPath::kNo != swPR->canDrawPath(args)) {
890 pr = swPR;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500891 }
robertphillips68737822015-10-29 12:12:21 -0700892 }
893
Robert Phillipsd81379d2020-04-21 10:39:02 -0400894#if GR_PATH_RENDERER_SPEW
895 if (pr) {
896 SkDebugf("getPathRenderer: %s\n", pr->name());
897 }
898#endif
899
robertphillips68737822015-10-29 12:12:21 -0700900 return pr;
901}
902
Brian Salomone7df0bb2018-05-07 14:44:57 -0400903GrPathRenderer* GrDrawingManager::getSoftwarePathRenderer() {
904 if (!fSoftwarePathRenderer) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400905 fSoftwarePathRenderer.reset(
Robert Phillips9da87e02019-02-04 13:26:26 -0500906 new GrSoftwarePathRenderer(fContext->priv().proxyProvider(),
Ben Wagner9ec70c62018-07-12 13:30:47 -0400907 fOptionsForPathRendererChain.fAllowPathMaskCaching));
Brian Salomone7df0bb2018-05-07 14:44:57 -0400908 }
Ben Wagner9ec70c62018-07-12 13:30:47 -0400909 return fSoftwarePathRenderer.get();
Brian Salomone7df0bb2018-05-07 14:44:57 -0400910}
911
Chris Daltonfddb6c02017-11-04 15:22:22 -0600912GrCoverageCountingPathRenderer* GrDrawingManager::getCoverageCountingPathRenderer() {
913 if (!fPathRendererChain) {
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500914 fPathRendererChain = std::make_unique<GrPathRendererChain>(fContext,
915 fOptionsForPathRendererChain);
Chris Daltonfddb6c02017-11-04 15:22:22 -0600916 }
917 return fPathRendererChain->getCoverageCountingPathRenderer();
918}
919
Brian Salomon653f42f2018-07-10 10:07:31 -0400920void GrDrawingManager::flushIfNecessary() {
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400921 auto direct = fContext->asDirectContext();
Robert Phillips6a6de562019-02-15 15:19:15 -0500922 if (!direct) {
923 return;
924 }
925
926 auto resourceCache = direct->priv().getResourceCache();
Brian Salomon653f42f2018-07-10 10:07:31 -0400927 if (resourceCache && resourceCache->requestsFlush()) {
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500928 if (this->flush({}, SkSurface::BackendSurfaceAccess::kNoAccess, GrFlushInfo(), nullptr)) {
Greg Danielfe159622020-04-10 17:43:51 +0000929 this->submitToGpu(false);
930 }
Brian Salomon57d2beab2018-09-10 09:35:41 -0400931 resourceCache->purgeAsNeeded();
Brian Salomon653f42f2018-07-10 10:07:31 -0400932 }
933}