blob: 0b8f3abc4d9b0587d27daa4f5a19a5612889e341 [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"
Adlai Hollera0693042020-10-14 11:23:11 -040022#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/GrGpu.h"
24#include "src/gpu/GrMemoryPool.h"
25#include "src/gpu/GrOnFlushResourceProvider.h"
26#include "src/gpu/GrRecordingContextPriv.h"
27#include "src/gpu/GrRenderTargetContext.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040028#include "src/gpu/GrRenderTargetProxy.h"
Chris Dalton6b498102019-08-01 14:14:52 -060029#include "src/gpu/GrRenderTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/gpu/GrResourceAllocator.h"
31#include "src/gpu/GrResourceProvider.h"
32#include "src/gpu/GrSoftwarePathRenderer.h"
Greg Daniel46e366a2019-12-16 14:38:36 -050033#include "src/gpu/GrSurfaceContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050034#include "src/gpu/GrSurfaceProxyPriv.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000035#include "src/gpu/GrTexture.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040036#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050037#include "src/gpu/GrTextureProxyPriv.h"
Chris Dalton3d770272019-08-14 09:24:37 -060038#include "src/gpu/GrTextureResolveRenderTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050039#include "src/gpu/GrTracing.h"
Greg Danielbbfec9d2019-08-20 10:56:51 -040040#include "src/gpu/GrTransferFromRenderTask.h"
Robert Phillipsa0e248d2020-11-11 09:39:27 -050041#include "src/gpu/GrUnrefDDLTask.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 Phillipsa0e248d2020-11-11 09:39:27 -050048 idArray->reset(fRenderTasks.count());
Chris Dalton6b498102019-08-01 14:14:52 -060049 for (int i = 0; i < fRenderTasks.count(); ++i) {
50 if (fRenderTasks[i]) {
Robert Phillipsa0e248d2020-11-11 09:39:27 -050051 (*idArray)[i] = fRenderTasks[i]->uniqueID();
Robert Phillips22310d62018-09-05 11:07:21 -040052 }
53 }
54}
55
Chris Dalton6b498102019-08-01 14:14:52 -060056void GrDrawingManager::RenderTaskDAG::reset() {
57 fRenderTasks.reset();
Robert Phillips22310d62018-09-05 11:07:21 -040058}
59
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
Robert Phillipsa0e248d2020-11-11 09:39:27 -050094void GrDrawingManager::RenderTaskDAG::add(const SkTArray<sk_sp<GrRenderTask>>& renderTasks) {
95#ifdef SK_DEBUG
96 for (auto& renderTask : renderTasks) {
97 SkASSERT(renderTask->unique());
98 }
99#endif
100
101 fRenderTasks.push_back_n(renderTasks.count(), renderTasks.begin());
102}
103
Greg Danielf41b2bd2019-08-22 16:19:24 -0400104void GrDrawingManager::RenderTaskDAG::swap(SkTArray<sk_sp<GrRenderTask>>* renderTasks) {
105 SkASSERT(renderTasks->empty());
106 renderTasks->swap(fRenderTasks);
Robert Phillips22310d62018-09-05 11:07:21 -0400107}
108
Chris Dalton6b498102019-08-01 14:14:52 -0600109void GrDrawingManager::RenderTaskDAG::prepForFlush() {
Adlai Holler3078f852020-11-05 15:44:50 -0500110 if (!SkTTopoSort<GrRenderTask, GrRenderTask::TopoSortTraits>(&fRenderTasks)) {
111 SkDEBUGFAIL("Render task topo sort failed.");
112 return;
Robert Phillips22310d62018-09-05 11:07:21 -0400113 }
114
115#ifdef SK_DEBUG
Greg Danielf41b2bd2019-08-22 16:19:24 -0400116 // This block checks for any unnecessary splits in the opsTasks. If two sequential opsTasks
117 // share the same backing GrSurfaceProxy it means the opsTask was artificially split.
Chris Dalton6b498102019-08-01 14:14:52 -0600118 if (fRenderTasks.count()) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400119 GrOpsTask* prevOpsTask = fRenderTasks[0]->asOpsTask();
Chris Dalton6b498102019-08-01 14:14:52 -0600120 for (int i = 1; i < fRenderTasks.count(); ++i) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400121 GrOpsTask* curOpsTask = fRenderTasks[i]->asOpsTask();
Robert Phillips22310d62018-09-05 11:07:21 -0400122
Greg Danielf41b2bd2019-08-22 16:19:24 -0400123 if (prevOpsTask && curOpsTask) {
Adlai Holler33d569e2020-06-16 14:30:08 -0400124 SkASSERT(prevOpsTask->target(0).proxy() != curOpsTask->target(0).proxy());
Robert Phillips22310d62018-09-05 11:07:21 -0400125 }
126
Greg Danielf41b2bd2019-08-22 16:19:24 -0400127 prevOpsTask = curOpsTask;
Robert Phillips22310d62018-09-05 11:07:21 -0400128 }
129 }
130#endif
131}
132
Chris Dalton6b498102019-08-01 14:14:52 -0600133void GrDrawingManager::RenderTaskDAG::closeAll(const GrCaps* caps) {
Adlai Holler96ead542020-06-26 08:50:14 -0400134 for (auto& task : fRenderTasks) {
135 if (task) {
136 task->makeClosed(*caps);
Robert Phillips22310d62018-09-05 11:07:21 -0400137 }
138 }
139}
140
Robert Phillips22310d62018-09-05 11:07:21 -0400141///////////////////////////////////////////////////////////////////////////////////////////////////
Robert Phillips69893702019-02-22 11:16:30 -0500142GrDrawingManager::GrDrawingManager(GrRecordingContext* context,
Robert Phillips22310d62018-09-05 11:07:21 -0400143 const GrPathRendererChain::Options& optionsForPathRendererChain,
Greg Danielf41b2bd2019-08-22 16:19:24 -0400144 bool reduceOpsTaskSplitting)
Robert Phillips22310d62018-09-05 11:07:21 -0400145 : fContext(context)
146 , fOptionsForPathRendererChain(optionsForPathRendererChain)
Robert Phillips22310d62018-09-05 11:07:21 -0400147 , fPathRendererChain(nullptr)
148 , fSoftwarePathRenderer(nullptr)
Robert Phillips6db27c22019-05-01 10:43:56 -0400149 , fFlushing(false)
Herb Derby082232b2020-06-10 15:08:18 -0400150 , fReduceOpsTaskSplitting(reduceOpsTaskSplitting) { }
Robert Phillips22310d62018-09-05 11:07:21 -0400151
robertphillips3dc6ae52015-10-20 09:54:32 -0700152GrDrawingManager::~GrDrawingManager() {
Adlai Holler96ead542020-06-26 08:50:14 -0400153 fDAG.closeAll(fContext->priv().caps());
154 this->removeRenderTasks(0, fDAG.numRenderTasks());
robertphillips3dc6ae52015-10-20 09:54:32 -0700155}
156
Robert Phillipsa9162df2019-02-11 14:12:03 -0500157bool GrDrawingManager::wasAbandoned() const {
Robert Phillips9eb00022020-06-30 15:30:12 -0400158 return fContext->abandoned();
robertphillips3dc6ae52015-10-20 09:54:32 -0700159}
160
robertphillips68737822015-10-29 12:12:21 -0700161void GrDrawingManager::freeGpuResources() {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400162 for (int i = fOnFlushCBObjects.count() - 1; i >= 0; --i) {
163 if (!fOnFlushCBObjects[i]->retainOnFreeGpuResources()) {
164 // it's safe to just do this because we're iterating in reverse
165 fOnFlushCBObjects.removeShuffle(i);
166 }
167 }
168
robertphillips68737822015-10-29 12:12:21 -0700169 // a path renderer may be holding onto resources
robertphillips13391dd2015-10-30 05:15:11 -0700170 fPathRendererChain = nullptr;
Ben Wagner9ec70c62018-07-12 13:30:47 -0400171 fSoftwarePathRenderer = nullptr;
Robert Phillipse3302df2017-04-24 07:31:02 -0400172}
173
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500174// MDB TODO: make use of the 'proxies' parameter.
Greg Daniel9efe3862020-06-11 11:51:06 -0400175bool GrDrawingManager::flush(
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500176 SkSpan<GrSurfaceProxy*> proxies,
Greg Daniel9efe3862020-06-11 11:51:06 -0400177 SkSurface::BackendSurfaceAccess access,
178 const GrFlushInfo& info,
179 const GrBackendSurfaceMutableState* newState) {
Brian Salomon57d2beab2018-09-10 09:35:41 -0400180 GR_CREATE_TRACE_MARKER_CONTEXT("GrDrawingManager", "flush", fContext);
Brian Salomondcbb9d92017-07-19 10:53:20 -0400181
robertphillips7761d612016-05-16 09:14:53 -0700182 if (fFlushing || this->wasAbandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -0400183 if (info.fSubmittedProc) {
184 info.fSubmittedProc(info.fSubmittedContext, false);
185 }
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400186 if (info.fFinishedProc) {
187 info.fFinishedProc(info.fFinishedContext);
Greg Daniela3aa75a2019-04-12 14:24:55 -0400188 }
Greg Danielfe159622020-04-10 17:43:51 +0000189 return false;
joshualittb8918c42015-12-18 09:59:46 -0800190 }
Robert Phillips602df412019-04-08 11:10:39 -0400191
Robert Phillips38d64b02018-09-04 13:23:26 -0400192 SkDEBUGCODE(this->validate());
193
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500194 // As of now we only short-circuit if we got an explicit list of surfaces to flush.
195 if (!proxies.empty() && !info.fNumSemaphores && !info.fFinishedProc &&
Greg Danielce9f0162020-06-30 13:42:46 -0400196 access == SkSurface::BackendSurfaceAccess::kNoAccess && !newState) {
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500197 bool allUnused = std::all_of(proxies.begin(), proxies.end(), [&](GrSurfaceProxy* proxy) {
198 return !fDAG.isUsed(proxy) && !this->isDDLTarget(proxy);
199 });
200 if (allUnused) {
Greg Daniel55822f12020-05-26 11:26:45 -0400201 if (info.fSubmittedProc) {
202 info.fSubmittedProc(info.fSubmittedContext, true);
203 }
Greg Danielfe159622020-04-10 17:43:51 +0000204 return false;
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400205 }
Robert Phillips9313aa72019-04-09 18:41:27 -0400206 }
207
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400208 auto direct = fContext->asDirectContext();
Adlai Holleraee25fd2020-11-06 12:34:56 -0500209 SkASSERT(direct);
Brian Salomon9241a6d2019-10-03 13:26:54 -0400210 direct->priv().clientMappedBufferManager()->process();
Robert Phillips6a6de562019-02-15 15:19:15 -0500211
212 GrGpu* gpu = direct->priv().getGpu();
Greg Daniel55822f12020-05-26 11:26:45 -0400213 // We have a non abandoned and direct GrContext. It must have a GrGpu.
214 SkASSERT(gpu);
Greg Daniela3aa75a2019-04-12 14:24:55 -0400215
joshualittb8918c42015-12-18 09:59:46 -0800216 fFlushing = true;
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400217
Robert Phillips6a6de562019-02-15 15:19:15 -0500218 auto resourceProvider = direct->priv().resourceProvider();
219 auto resourceCache = direct->priv().getResourceCache();
220
Chris Dalton6b498102019-08-01 14:14:52 -0600221 // Semi-usually the GrRenderTasks are already closed at this point, but sometimes Ganesh needs
Greg Danielf41b2bd2019-08-22 16:19:24 -0400222 // to flush mid-draw. In that case, the SkGpuDevice's opsTasks won't be closed but need to be
223 // flushed anyway. Closing such opsTasks here will mean new ones will be created to replace them
Chris Dalton6b498102019-08-01 14:14:52 -0600224 // if the SkGpuDevice(s) write to them again.
Robert Phillips9da87e02019-02-04 13:26:26 -0500225 fDAG.closeAll(fContext->priv().caps());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400226 fActiveOpsTask = nullptr;
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400227
Robert Phillips22310d62018-09-05 11:07:21 -0400228 fDAG.prepForFlush();
Brian Salomon601ac802019-02-07 13:37:16 -0500229 if (!fCpuBufferCache) {
230 // We cache more buffers when the backend is using client side arrays. Otherwise, we
231 // expect each pool will use a CPU buffer as a staging buffer before uploading to a GPU
232 // buffer object. Each pool only requires one staging buffer at a time.
233 int maxCachedBuffers = fContext->priv().caps()->preferClientSideDynamicBuffers() ? 2 : 6;
234 fCpuBufferCache = GrBufferAllocPool::CpuBufferCache::Make(maxCachedBuffers);
Brian Salomon58f153c2018-10-18 21:51:15 -0400235 }
Robert Phillipsa4c93ac2017-05-18 11:40:04 -0400236
Robert Phillipse5f73282019-06-18 17:15:04 -0400237 GrOpFlushState flushState(gpu, resourceProvider, &fTokenTracker, fCpuBufferCache);
Robert Phillips40a29d72018-01-18 12:59:22 -0500238
Chris Daltonfe199b72017-05-05 11:26:15 -0400239 GrOnFlushResourceProvider onFlushProvider(this);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400240
Chris Dalton12658942017-10-05 19:45:25 -0600241 // Prepare any onFlush op lists (e.g. atlases).
Chris Daltonfe199b72017-05-05 11:26:15 -0400242 if (!fOnFlushCBObjects.empty()) {
Chris Dalton6b498102019-08-01 14:14:52 -0600243 fDAG.gatherIDs(&fFlushingRenderTaskIDs);
Robert Phillips22310d62018-09-05 11:07:21 -0400244
Chris Daltonfe199b72017-05-05 11:26:15 -0400245 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
Adlai Holler9902cff2020-11-11 08:51:25 -0500246 onFlushCBObject->preFlush(&onFlushProvider, fFlushingRenderTaskIDs);
Chris Daltonc4b47352019-08-23 10:10:36 -0600247 }
248 for (const auto& onFlushRenderTask : fOnFlushRenderTasks) {
249 onFlushRenderTask->makeClosed(*fContext->priv().caps());
Chris Dalton706a6ff2017-11-29 22:01:06 -0700250#ifdef SK_DEBUG
Chris Daltonc4b47352019-08-23 10:10:36 -0600251 // OnFlush callbacks are invoked during flush, and are therefore expected to handle
252 // resource allocation & usage on their own. (No deferred or lazy proxies!)
253 onFlushRenderTask->visitTargetAndSrcProxies_debugOnly(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400254 [](GrSurfaceProxy* p, GrMipmapped mipMapped) {
Chris Daltonc4b47352019-08-23 10:10:36 -0600255 SkASSERT(!p->asTextureProxy() || !p->asTextureProxy()->texPriv().isDeferred());
Brian Salomonbeb7f522019-08-30 16:19:42 -0400256 SkASSERT(!p->isLazy());
Chris Dalton4ece96d2019-08-30 11:26:39 -0600257 if (p->requiresManualMSAAResolve()) {
258 // The onFlush callback is responsible for ensuring MSAA gets resolved.
259 SkASSERT(p->asRenderTargetProxy() && !p->asRenderTargetProxy()->isMSAADirty());
260 }
Brian Salomon7e67dca2020-07-21 09:27:25 -0400261 if (GrMipmapped::kYes == mipMapped) {
Chris Daltonc4b47352019-08-23 10:10:36 -0600262 // The onFlush callback is responsible for regenerating mips if needed.
Brian Salomon8c82a872020-07-21 12:09:58 -0400263 SkASSERT(p->asTextureProxy() && !p->asTextureProxy()->mipmapsAreDirty());
Chris Daltonc4b47352019-08-23 10:10:36 -0600264 }
265 });
Chris Dalton706a6ff2017-11-29 22:01:06 -0700266#endif
Chris Daltonc4b47352019-08-23 10:10:36 -0600267 onFlushRenderTask->prepare(&flushState);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400268 }
269 }
270
robertphillipsa13e2022015-11-11 12:01:09 -0800271#if 0
Brian Salomon09d994e2016-12-21 11:14:46 -0500272 // Enable this to print out verbose GrOp information
Adlai Hollerabdfd392020-10-19 09:00:36 -0400273 SkDEBUGCODE(SkDebugf("onFlush renderTasks (%d):\n", fOnFlushRenderTasks.count()));
Chris Daltonc4b47352019-08-23 10:10:36 -0600274 for (const auto& onFlushRenderTask : fOnFlushRenderTasks) {
Adlai Hollerabdfd392020-10-19 09:00:36 -0400275 SkDEBUGCODE(onFlushRenderTask->dump(/* printDependencies */ true);)
Chris Daltonc4b47352019-08-23 10:10:36 -0600276 }
Adlai Hollerabdfd392020-10-19 09:00:36 -0400277 SkDEBUGCODE(SkDebugf("Normal renderTasks (%d):\n", fDAG.numRenderTasks()));
278 for (int i = 0; i < fDAG.numRenderTasks(); ++i) {
279 SkDEBUGCODE(fDAG.renderTask(i)->dump(/* printDependencies */ true);)
robertphillips3dc6ae52015-10-20 09:54:32 -0700280 }
robertphillipsa13e2022015-11-11 12:01:09 -0800281#endif
282
Robert Phillipseafd48a2017-11-16 07:52:08 -0500283 int startIndex, stopIndex;
284 bool flushed = false;
285
Robert Phillipsf8e25022017-11-08 15:24:31 -0500286 {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400287 GrResourceAllocator alloc(resourceProvider SkDEBUGCODE(, fDAG.numRenderTasks()));
Chris Dalton6b498102019-08-01 14:14:52 -0600288 for (int i = 0; i < fDAG.numRenderTasks(); ++i) {
289 if (fDAG.renderTask(i)) {
290 fDAG.renderTask(i)->gatherProxyIntervals(&alloc);
Robert Phillips22310d62018-09-05 11:07:21 -0400291 }
Greg Danielf41b2bd2019-08-22 16:19:24 -0400292 alloc.markEndOfOpsTask(i);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500293 }
Robert Phillipsc73666f2019-04-24 08:49:48 -0400294 alloc.determineRecyclability();
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400295
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500296 GrResourceAllocator::AssignError error = GrResourceAllocator::AssignError::kNoError;
Chris Dalton6b498102019-08-01 14:14:52 -0600297 int numRenderTasksExecuted = 0;
Brian Salomon577aa0f2018-11-30 13:32:23 -0500298 while (alloc.assign(&startIndex, &stopIndex, &error)) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500299 if (GrResourceAllocator::AssignError::kFailedProxyInstantiation == error) {
300 for (int i = startIndex; i < stopIndex; ++i) {
Chris Dalton6b498102019-08-01 14:14:52 -0600301 GrRenderTask* renderTask = fDAG.renderTask(i);
302 if (!renderTask) {
303 continue;
Robert Phillips01a91282018-07-26 08:03:04 -0400304 }
Chris Dalton6b498102019-08-01 14:14:52 -0600305 if (!renderTask->isInstantiated()) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400306 // No need to call the renderTask's handleInternalAllocationFailure
307 // since we will already skip executing the renderTask since it is not
308 // instantiated.
Chris Dalton6b498102019-08-01 14:14:52 -0600309 continue;
Robert Phillipsbbcb7f72018-05-31 11:16:19 -0400310 }
Chris Dalton6b498102019-08-01 14:14:52 -0600311 renderTask->handleInternalAllocationFailure();
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500312 }
Adlai Holler96ead542020-06-26 08:50:14 -0400313 this->removeRenderTasks(startIndex, stopIndex);
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500314 }
Robert Phillips4150eea2018-02-07 17:08:21 -0500315
Chris Dalton6b498102019-08-01 14:14:52 -0600316 if (this->executeRenderTasks(
317 startIndex, stopIndex, &flushState, &numRenderTasksExecuted)) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500318 flushed = true;
319 }
bsalomondc438982016-08-31 11:53:49 -0700320 }
Greg Danielc42b20b2017-10-04 10:34:49 -0400321 }
322
Chris Dalton91ab1552018-04-18 13:24:25 -0600323#ifdef SK_DEBUG
Chris Dalton6b498102019-08-01 14:14:52 -0600324 for (int i = 0; i < fDAG.numRenderTasks(); ++i) {
Adlai Holler96ead542020-06-26 08:50:14 -0400325 // All render tasks should have been cleared out by now – we only reset the array below to
326 // reclaim storage.
327 SkASSERT(!fDAG.renderTask(i));
Chris Dalton91ab1552018-04-18 13:24:25 -0600328 }
329#endif
Adlai Holler25df1f72020-06-09 13:08:27 -0400330 fLastRenderTasks.reset();
Robert Phillips22310d62018-09-05 11:07:21 -0400331 fDAG.reset();
Robert Phillips15c91422019-05-07 16:54:48 -0400332 this->clearDDLTargets();
robertphillipsa13e2022015-11-11 12:01:09 -0800333
Robert Phillipsc994a932018-06-19 13:09:54 -0400334#ifdef SK_DEBUG
335 // In non-DDL mode this checks that all the flushed ops have been freed from the memory pool.
336 // When we move to partial flushes this assert will no longer be valid.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400337 // In DDL mode this check is somewhat superfluous since the memory for most of the ops/opsTasks
Robert Phillipsc994a932018-06-19 13:09:54 -0400338 // will be stored in the DDL's GrOpMemoryPools.
Herb Derbye32e1ab2020-10-27 10:29:46 -0400339 GrMemoryPool* opMemoryPool = fContext->priv().opMemoryPool();
Robert Phillipsc994a932018-06-19 13:09:54 -0400340 opMemoryPool->isEmpty();
341#endif
342
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500343 gpu->executeFlushInfo(proxies, access, info, newState);
robertphillipsa13e2022015-11-11 12:01:09 -0800344
Brian Salomon57d2beab2018-09-10 09:35:41 -0400345 // Give the cache a chance to purge resources that become purgeable due to flushing.
346 if (flushed) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500347 resourceCache->purgeAsNeeded();
Brian Salomon876a0172019-03-08 11:12:14 -0500348 flushed = false;
bsalomonb77a9072016-09-07 10:02:04 -0700349 }
Chris Daltonfe199b72017-05-05 11:26:15 -0400350 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
Adlai Holler9902cff2020-11-11 08:51:25 -0500351 onFlushCBObject->postFlush(fTokenTracker.nextTokenToFlush(), fFlushingRenderTaskIDs);
Brian Salomon876a0172019-03-08 11:12:14 -0500352 flushed = true;
353 }
354 if (flushed) {
355 resourceCache->purgeAsNeeded();
Chris Daltonfe199b72017-05-05 11:26:15 -0400356 }
Chris Dalton6b498102019-08-01 14:14:52 -0600357 fFlushingRenderTaskIDs.reset();
joshualittb8918c42015-12-18 09:59:46 -0800358 fFlushing = false;
Greg Daniel51316782017-08-02 15:10:09 +0000359
Greg Danielfe159622020-04-10 17:43:51 +0000360 return true;
361}
362
363bool GrDrawingManager::submitToGpu(bool syncToCpu) {
364 if (fFlushing || this->wasAbandoned()) {
365 return false;
366 }
367
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400368 auto direct = fContext->asDirectContext();
Greg Danielfe159622020-04-10 17:43:51 +0000369 if (!direct) {
370 return false; // Can't submit while DDL recording
371 }
372 GrGpu* gpu = direct->priv().getGpu();
373 return gpu->submitToGpu(syncToCpu);
robertphillips3dc6ae52015-10-20 09:54:32 -0700374}
375
Chris Dalton6b498102019-08-01 14:14:52 -0600376bool GrDrawingManager::executeRenderTasks(int startIndex, int stopIndex, GrOpFlushState* flushState,
377 int* numRenderTasksExecuted) {
378 SkASSERT(startIndex <= stopIndex && stopIndex <= fDAG.numRenderTasks());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500379
Robert Phillips27483912018-04-20 12:43:18 -0400380#if GR_FLUSH_TIME_OP_SPEW
Greg Danielf41b2bd2019-08-22 16:19:24 -0400381 SkDebugf("Flushing opsTask: %d to %d out of [%d, %d]\n",
Chris Dalton6b498102019-08-01 14:14:52 -0600382 startIndex, stopIndex, 0, fDAG.numRenderTasks());
Robert Phillips27483912018-04-20 12:43:18 -0400383 for (int i = startIndex; i < stopIndex; ++i) {
Chris Dalton6b498102019-08-01 14:14:52 -0600384 if (fDAG.renderTask(i)) {
385 fDAG.renderTask(i)->dump(true);
Robert Phillips1734dd32018-08-21 13:52:09 -0400386 }
Robert Phillips27483912018-04-20 12:43:18 -0400387 }
388#endif
389
Chris Dalton6b498102019-08-01 14:14:52 -0600390 bool anyRenderTasksExecuted = false;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500391
392 for (int i = startIndex; i < stopIndex; ++i) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400393 GrRenderTask* renderTask = fDAG.renderTask(i);
394 if (!renderTask || !renderTask->isInstantiated()) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500395 continue;
396 }
397
Chris Dalton6b498102019-08-01 14:14:52 -0600398 SkASSERT(renderTask->deferredProxiesAreInstantiated());
Robert Phillips22310d62018-09-05 11:07:21 -0400399
Chris Dalton6b498102019-08-01 14:14:52 -0600400 renderTask->prepare(flushState);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500401 }
402
403 // Upload all data to the GPU
404 flushState->preExecuteDraws();
405
Greg Danield2073452018-12-07 11:20:33 -0500406 // For Vulkan, if we have too many oplists to be flushed we end up allocating a lot of resources
407 // for each command buffer associated with the oplists. If this gets too large we can cause the
408 // devices to go OOM. In practice we usually only hit this case in our tests, but to be safe we
409 // put a cap on the number of oplists we will execute before flushing to the GPU to relieve some
410 // memory pressure.
Chris Dalton6b498102019-08-01 14:14:52 -0600411 static constexpr int kMaxRenderTasksBeforeFlush = 100;
Greg Danield2073452018-12-07 11:20:33 -0500412
Chris Daltonc4b47352019-08-23 10:10:36 -0600413 // Execute the onFlush renderTasks first, if any.
414 for (sk_sp<GrRenderTask>& onFlushRenderTask : fOnFlushRenderTasks) {
415 if (!onFlushRenderTask->execute(flushState)) {
416 SkDebugf("WARNING: onFlushRenderTask failed to execute.\n");
Robert Phillipseafd48a2017-11-16 07:52:08 -0500417 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600418 SkASSERT(onFlushRenderTask->unique());
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400419 onFlushRenderTask->disown(this);
Chris Daltonc4b47352019-08-23 10:10:36 -0600420 onFlushRenderTask = nullptr;
Chris Dalton6b498102019-08-01 14:14:52 -0600421 (*numRenderTasksExecuted)++;
422 if (*numRenderTasksExecuted >= kMaxRenderTasksBeforeFlush) {
Greg Danielfe159622020-04-10 17:43:51 +0000423 flushState->gpu()->submitToGpu(false);
Chris Dalton6b498102019-08-01 14:14:52 -0600424 *numRenderTasksExecuted = 0;
Greg Danield2073452018-12-07 11:20:33 -0500425 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500426 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600427 fOnFlushRenderTasks.reset();
Robert Phillipseafd48a2017-11-16 07:52:08 -0500428
429 // Execute the normal op lists.
430 for (int i = startIndex; i < stopIndex; ++i) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400431 GrRenderTask* renderTask = fDAG.renderTask(i);
432 if (!renderTask || !renderTask->isInstantiated()) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500433 continue;
434 }
435
Greg Daniel15ecdf92019-08-30 15:35:23 -0400436 if (renderTask->execute(flushState)) {
Chris Dalton6b498102019-08-01 14:14:52 -0600437 anyRenderTasksExecuted = true;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500438 }
Chris Dalton6b498102019-08-01 14:14:52 -0600439 (*numRenderTasksExecuted)++;
440 if (*numRenderTasksExecuted >= kMaxRenderTasksBeforeFlush) {
Greg Danielfe159622020-04-10 17:43:51 +0000441 flushState->gpu()->submitToGpu(false);
Chris Dalton6b498102019-08-01 14:14:52 -0600442 *numRenderTasksExecuted = 0;
Greg Danield2073452018-12-07 11:20:33 -0500443 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500444 }
445
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400446 SkASSERT(!flushState->opsRenderPass());
Robert Phillips40a29d72018-01-18 12:59:22 -0500447 SkASSERT(fTokenTracker.nextDrawToken() == fTokenTracker.nextTokenToFlush());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500448
Chris Dalton6b498102019-08-01 14:14:52 -0600449 // We reset the flush state before the RenderTasks so that the last resources to be freed are
450 // those that are written to in the RenderTasks. This helps to make sure the most recently used
451 // resources are the last to be purged by the resource cache.
Robert Phillipseafd48a2017-11-16 07:52:08 -0500452 flushState->reset();
453
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400454 this->removeRenderTasks(startIndex, stopIndex);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500455
Chris Dalton6b498102019-08-01 14:14:52 -0600456 return anyRenderTasksExecuted;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500457}
458
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400459void GrDrawingManager::removeRenderTasks(int startIndex, int stopIndex) {
460 for (int i = startIndex; i < stopIndex; ++i) {
461 GrRenderTask* task = fDAG.renderTask(i);
462 if (!task) {
463 continue;
464 }
Robert Phillipsa0e248d2020-11-11 09:39:27 -0500465 if (!task->unique()) {
466 // TODO: Eventually this should be guaranteed unique: http://skbug.com/7111
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400467 task->endFlush(this);
468 }
469 task->disown(this);
470 }
Adlai Holler96ead542020-06-26 08:50:14 -0400471 fDAG.rawRemoveRenderTasks(startIndex, stopIndex);
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400472}
473
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400474static void resolve_and_mipmap(GrGpu* gpu, GrSurfaceProxy* proxy) {
475 if (!proxy->isInstantiated()) {
476 return;
477 }
478
479 // In the flushSurfaces case, we need to resolve MSAA immediately after flush. This is
480 // because clients expect the flushed surface's backing texture to be fully resolved
481 // upon return.
482 if (proxy->requiresManualMSAAResolve()) {
483 auto* rtProxy = proxy->asRenderTargetProxy();
484 SkASSERT(rtProxy);
485 if (rtProxy->isMSAADirty()) {
486 SkASSERT(rtProxy->peekRenderTarget());
Jim Van Verthbb61fe32020-07-07 16:39:04 -0400487 gpu->resolveRenderTarget(rtProxy->peekRenderTarget(), rtProxy->msaaDirtyRect());
488 gpu->submitToGpu(false);
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400489 rtProxy->markMSAAResolved();
490 }
491 }
492 // If, after a flush, any of the proxies of interest have dirty mipmaps, regenerate them in
493 // case their backend textures are being stolen.
494 // (This special case is exercised by the ReimportImageTextureWithMipLevels test.)
495 // FIXME: It may be more ideal to plumb down a "we're going to steal the backends" flag.
496 if (auto* textureProxy = proxy->asTextureProxy()) {
Brian Salomon8c82a872020-07-21 12:09:58 -0400497 if (textureProxy->mipmapsAreDirty()) {
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400498 SkASSERT(textureProxy->peekTexture());
499 gpu->regenerateMipMapLevels(textureProxy->peekTexture());
Brian Salomon8c82a872020-07-21 12:09:58 -0400500 textureProxy->markMipmapsClean();
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400501 }
502 }
503}
504
Greg Daniel9efe3862020-06-11 11:51:06 -0400505GrSemaphoresSubmitted GrDrawingManager::flushSurfaces(
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500506 SkSpan<GrSurfaceProxy*> proxies,
Greg Daniel9efe3862020-06-11 11:51:06 -0400507 SkSurface::BackendSurfaceAccess access,
508 const GrFlushInfo& info,
509 const GrBackendSurfaceMutableState* newState) {
bsalomon6a2b1942016-09-08 11:28:59 -0700510 if (this->wasAbandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -0400511 if (info.fSubmittedProc) {
512 info.fSubmittedProc(info.fSubmittedContext, false);
513 }
514 if (info.fFinishedProc) {
515 info.fFinishedProc(info.fFinishedContext);
516 }
Greg Daniel51316782017-08-02 15:10:09 +0000517 return GrSemaphoresSubmitted::kNo;
bsalomon6a2b1942016-09-08 11:28:59 -0700518 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400519 SkDEBUGCODE(this->validate());
bsalomon6a2b1942016-09-08 11:28:59 -0700520
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400521 auto direct = fContext->asDirectContext();
Adlai Holleraee25fd2020-11-06 12:34:56 -0500522 SkASSERT(direct);
Robert Phillips6a6de562019-02-15 15:19:15 -0500523 GrGpu* gpu = direct->priv().getGpu();
Greg Daniel55822f12020-05-26 11:26:45 -0400524 // We have a non abandoned and direct GrContext. It must have a GrGpu.
525 SkASSERT(gpu);
Robert Phillips874b5352018-03-16 08:48:24 -0400526
Robert Phillipsacc10fa2019-04-01 09:50:20 -0400527 // TODO: It is important to upgrade the drawingmanager to just flushing the
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400528 // portion of the DAG required by 'proxies' in order to restore some of the
Robert Phillipsacc10fa2019-04-01 09:50:20 -0400529 // semantics of this method.
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500530 bool didFlush = this->flush(proxies, access, info, newState);
531 for (GrSurfaceProxy* proxy : proxies) {
532 resolve_and_mipmap(gpu, proxy);
bsalomon6a2b1942016-09-08 11:28:59 -0700533 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400534
535 SkDEBUGCODE(this->validate());
Greg Danielfe159622020-04-10 17:43:51 +0000536
Greg Daniel04283f32020-05-20 13:16:00 -0400537 if (!didFlush || (!direct->priv().caps()->semaphoreSupport() && info.fNumSemaphores)) {
Greg Danielfe159622020-04-10 17:43:51 +0000538 return GrSemaphoresSubmitted::kNo;
539 }
540 return GrSemaphoresSubmitted::kYes;
bsalomon6a2b1942016-09-08 11:28:59 -0700541}
542
Chris Daltonfe199b72017-05-05 11:26:15 -0400543void GrDrawingManager::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
544 fOnFlushCBObjects.push_back(onFlushCBObject);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400545}
546
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500547#if GR_TEST_UTILS
548void GrDrawingManager::testingOnly_removeOnFlushCallbackObject(GrOnFlushCallbackObject* cb) {
549 int n = std::find(fOnFlushCBObjects.begin(), fOnFlushCBObjects.end(), cb) -
550 fOnFlushCBObjects.begin();
551 SkASSERT(n < fOnFlushCBObjects.count());
552 fOnFlushCBObjects.removeShuffle(n);
553}
554#endif
555
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400556void GrDrawingManager::setLastRenderTask(const GrSurfaceProxy* proxy, GrRenderTask* task) {
557#ifdef SK_DEBUG
Robert Phillipsa0e248d2020-11-11 09:39:27 -0500558 if (GrRenderTask* prior = this->getLastRenderTask(proxy)) {
559 SkASSERT(prior->isClosed());
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400560 }
561#endif
Adlai Holler25df1f72020-06-09 13:08:27 -0400562 uint32_t key = proxy->uniqueID().asUInt();
563 if (task) {
564 fLastRenderTasks.set(key, task);
565 } else if (fLastRenderTasks.find(key)) {
566 fLastRenderTasks.remove(key);
567 }
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400568}
569
570GrRenderTask* GrDrawingManager::getLastRenderTask(const GrSurfaceProxy* proxy) const {
571 auto entry = fLastRenderTasks.find(proxy->uniqueID().asUInt());
572 return entry ? *entry : nullptr;
573}
574
575GrOpsTask* GrDrawingManager::getLastOpsTask(const GrSurfaceProxy* proxy) const {
576 GrRenderTask* task = this->getLastRenderTask(proxy);
577 return task ? task->asOpsTask() : nullptr;
578}
579
580
Chris Dalton6b498102019-08-01 14:14:52 -0600581void GrDrawingManager::moveRenderTasksToDDL(SkDeferredDisplayList* ddl) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400582 SkDEBUGCODE(this->validate());
583
Chris Dalton6b498102019-08-01 14:14:52 -0600584 // no renderTask should receive a new command after this
Robert Phillips9da87e02019-02-04 13:26:26 -0500585 fDAG.closeAll(fContext->priv().caps());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400586 fActiveOpsTask = nullptr;
Robert Phillips7a137052018-02-01 11:23:12 -0500587
Chris Dalton6b498102019-08-01 14:14:52 -0600588 fDAG.swap(&ddl->fRenderTasks);
Robert Phillips19f466d2020-02-26 10:27:07 -0500589 SkASSERT(!fDAG.numRenderTasks());
Robert Phillips867ce8f2018-06-21 10:28:36 -0400590
Robert Phillips19f466d2020-02-26 10:27:07 -0500591 for (auto& renderTask : ddl->fRenderTasks) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400592 renderTask->disown(this);
Robert Phillips29f38542019-10-16 09:20:25 -0400593 renderTask->prePrepare(fContext);
Robert Phillips7327c9d2019-10-08 16:32:56 -0400594 }
595
Michael Ludwig2c316bd2019-12-19 14:50:44 -0500596 ddl->fArenas = std::move(fContext->priv().detachArenas());
Robert Phillips61fc7992019-10-22 11:58:17 -0400597
Robert Phillipsf6a0b452020-02-18 14:26:46 -0500598 fContext->priv().detachProgramData(&ddl->fProgramData);
Robert Phillips576b6a12019-12-06 13:05:49 -0500599
Robert Phillips774168e2018-05-31 12:43:27 -0400600 if (fPathRendererChain) {
601 if (auto ccpr = fPathRendererChain->getCoverageCountingPathRenderer()) {
602 ddl->fPendingPaths = ccpr->detachPendingPaths();
603 }
604 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400605
606 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500607}
608
Robert Phillipsa0e248d2020-11-11 09:39:27 -0500609void GrDrawingManager::copyRenderTasksFromDDL(sk_sp<const SkDeferredDisplayList> ddl,
610 GrRenderTargetProxy* newDest) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400611 SkDEBUGCODE(this->validate());
612
Greg Danielf41b2bd2019-08-22 16:19:24 -0400613 if (fActiveOpsTask) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400614 // This is a temporary fix for the partial-MDB world. In that world we're not
Greg Danielf41b2bd2019-08-22 16:19:24 -0400615 // reordering so ops that (in the single opsTask world) would've just glommed onto the
616 // end of the single opsTask but referred to a far earlier RT need to appear in their
617 // own opsTask.
618 fActiveOpsTask->makeClosed(*fContext->priv().caps());
619 fActiveOpsTask = nullptr;
Robert Phillips38d64b02018-09-04 13:23:26 -0400620 }
621
Robert Phillipsa0e248d2020-11-11 09:39:27 -0500622 // Propagate the DDL proxy's state information to the replaying DDL.
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400623 if (ddl->priv().targetProxy()->isMSAADirty()) {
624 newDest->markMSAADirty(ddl->priv().targetProxy()->msaaDirtyRect(),
625 ddl->characterization().origin());
626 }
627 GrTextureProxy* newTextureProxy = newDest->asTextureProxy();
Brian Salomon8c82a872020-07-21 12:09:58 -0400628 if (newTextureProxy && GrMipmapped::kYes == newTextureProxy->mipmapped()) {
629 newTextureProxy->markMipmapsDirty();
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400630 }
631
632 this->addDDLTarget(newDest, ddl->priv().targetProxy());
Robert Phillips15c91422019-05-07 16:54:48 -0400633
Robert Phillips62000362018-02-01 09:10:04 -0500634 // Here we jam the proxy that backs the current replay SkSurface into the LazyProxyData.
Robert Phillipsa0e248d2020-11-11 09:39:27 -0500635 // The lazy proxy that references it (in the copied opsTasks) will steal its GrTexture.
Robert Phillips62000362018-02-01 09:10:04 -0500636 ddl->fLazyProxyData->fReplayDest = newDest;
Robert Phillips774168e2018-05-31 12:43:27 -0400637
638 if (ddl->fPendingPaths.size()) {
639 GrCoverageCountingPathRenderer* ccpr = this->getCoverageCountingPathRenderer();
640
641 ccpr->mergePendingPaths(ddl->fPendingPaths);
642 }
Robert Phillips22310d62018-09-05 11:07:21 -0400643
Robert Phillipsa0e248d2020-11-11 09:39:27 -0500644 fDAG.add(ddl->fRenderTasks);
645
646 // Add a task to unref the DDL after flush.
647 GrRenderTask* unrefTask = fDAG.add(sk_make_sp<GrUnrefDDLTask>(std::move(ddl)));
648 unrefTask->makeClosed(*fContext->priv().caps());
Adlai Holler7580ad42020-06-24 13:45:25 -0400649
Robert Phillips38d64b02018-09-04 13:23:26 -0400650 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500651}
652
Robert Phillips38d64b02018-09-04 13:23:26 -0400653#ifdef SK_DEBUG
654void GrDrawingManager::validate() const {
Adlai Holler3078f852020-11-05 15:44:50 -0500655 if (fReduceOpsTaskSplitting) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400656 SkASSERT(!fActiveOpsTask);
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400657 } else {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400658 if (fActiveOpsTask) {
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400659 SkASSERT(!fDAG.empty());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400660 SkASSERT(!fActiveOpsTask->isClosed());
661 SkASSERT(fActiveOpsTask == fDAG.back());
Robert Phillips38d64b02018-09-04 13:23:26 -0400662 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400663
Chris Dalton6b498102019-08-01 14:14:52 -0600664 for (int i = 0; i < fDAG.numRenderTasks(); ++i) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400665 if (fActiveOpsTask != fDAG.renderTask(i)) {
Chris Daltone2a903e2019-09-18 13:41:50 -0600666 // The resolveTask associated with the activeTask remains open for as long as the
667 // activeTask does.
668 bool isActiveResolveTask =
669 fActiveOpsTask && fActiveOpsTask->fTextureResolveTask == fDAG.renderTask(i);
670 SkASSERT(isActiveResolveTask || fDAG.renderTask(i)->isClosed());
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400671 }
672 }
673
674 if (!fDAG.empty() && !fDAG.back()->isClosed()) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400675 SkASSERT(fActiveOpsTask == fDAG.back());
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400676 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400677 }
678}
679#endif
680
Greg Danielbbfec9d2019-08-20 10:56:51 -0400681void GrDrawingManager::closeRenderTasksForNewRenderTask(GrSurfaceProxy* target) {
Adlai Holler3078f852020-11-05 15:44:50 -0500682 if (target && fReduceOpsTaskSplitting) {
Chris Dalton5fe99772019-08-06 11:57:39 -0600683 // In this case we need to close all the renderTasks that rely on the current contents of
684 // 'target'. That is bc we're going to update the content of the proxy so they need to be
685 // split in case they use both the old and new content. (This is a bit of an overkill: they
686 // really only need to be split if they ever reference proxy's contents again but that is
687 // hard to predict/handle).
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400688 if (GrRenderTask* lastRenderTask = this->getLastRenderTask(target)) {
Chris Dalton6b498102019-08-01 14:14:52 -0600689 lastRenderTask->closeThoseWhoDependOnMe(*fContext->priv().caps());
Robert Phillips46acf9d2018-10-09 09:31:40 -0400690 }
Greg Danielf41b2bd2019-08-22 16:19:24 -0400691 } else if (fActiveOpsTask) {
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400692 // This is a temporary fix for the partial-MDB world. In that world we're not
Greg Danielf41b2bd2019-08-22 16:19:24 -0400693 // reordering so ops that (in the single opsTask world) would've just glommed onto the
694 // end of the single opsTask but referred to a far earlier RT need to appear in their
695 // own opsTask.
696 fActiveOpsTask->makeClosed(*fContext->priv().caps());
697 fActiveOpsTask = nullptr;
robertphillips3dc6ae52015-10-20 09:54:32 -0700698 }
Chris Dalton5fe99772019-08-06 11:57:39 -0600699}
700
Greg Daniel16f5c652019-10-29 11:26:01 -0400701sk_sp<GrOpsTask> GrDrawingManager::newOpsTask(GrSurfaceProxyView surfaceView,
702 bool managedOpsTask) {
Chris Dalton5fe99772019-08-06 11:57:39 -0600703 SkDEBUGCODE(this->validate());
704 SkASSERT(fContext);
705
Greg Daniel16f5c652019-10-29 11:26:01 -0400706 GrSurfaceProxy* proxy = surfaceView.proxy();
707 this->closeRenderTasksForNewRenderTask(proxy);
robertphillips3dc6ae52015-10-20 09:54:32 -0700708
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400709 sk_sp<GrOpsTask> opsTask(new GrOpsTask(this, fContext->priv().arenas(),
Michael Ludwigd0840ec2019-12-12 09:48:38 -0500710 std::move(surfaceView),
711 fContext->priv().auditTrail()));
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400712 SkASSERT(this->getLastRenderTask(proxy) == opsTask.get());
robertphillips3dc6ae52015-10-20 09:54:32 -0700713
Greg Danielf41b2bd2019-08-22 16:19:24 -0400714 if (managedOpsTask) {
715 fDAG.add(opsTask);
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400716
Adlai Holler3078f852020-11-05 15:44:50 -0500717 if (!fReduceOpsTaskSplitting) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400718 fActiveOpsTask = opsTask.get();
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400719 }
Robert Phillips941d1442017-06-14 16:37:02 -0400720 }
robertphillips3dc6ae52015-10-20 09:54:32 -0700721
Robert Phillips38d64b02018-09-04 13:23:26 -0400722 SkDEBUGCODE(this->validate());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400723 return opsTask;
robertphillips3dc6ae52015-10-20 09:54:32 -0700724}
725
Chris Daltone2a903e2019-09-18 13:41:50 -0600726GrTextureResolveRenderTask* GrDrawingManager::newTextureResolveRenderTask(const GrCaps& caps) {
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600727 // 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 -0600728 // in sorting and opsTask reduction mode) the render tasks that depend on any proxy's current
Greg Danielf41b2bd2019-08-22 16:19:24 -0400729 // state. This is because those opsTasks can still receive new ops and because if they refer to
Chris Dalton4ece96d2019-08-30 11:26:39 -0600730 // the mipmapped version of 'proxy', they will then come to depend on the render task being
731 // created here.
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600732 //
Greg Danielf41b2bd2019-08-22 16:19:24 -0400733 // Add the new textureResolveTask before the fActiveOpsTask (if not in
734 // sorting/opsTask-splitting-reduction mode) because it will depend upon this resolve task.
Chris Dalton3d770272019-08-14 09:24:37 -0600735 // NOTE: Putting it here will also reduce the amount of work required by the topological sort.
Chris Daltone2a903e2019-09-18 13:41:50 -0600736 return static_cast<GrTextureResolveRenderTask*>(fDAG.addBeforeLast(
737 sk_make_sp<GrTextureResolveRenderTask>()));
Chris Dalton3d770272019-08-14 09:24:37 -0600738}
739
Greg Danielc30f1a92019-09-06 15:28:58 -0400740void GrDrawingManager::newWaitRenderTask(sk_sp<GrSurfaceProxy> proxy,
Greg Daniel301015c2019-11-18 14:06:46 -0500741 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> semaphores,
Greg Danielc30f1a92019-09-06 15:28:58 -0400742 int numSemaphores) {
743 SkDEBUGCODE(this->validate());
744 SkASSERT(fContext);
745
746 const GrCaps& caps = *fContext->priv().caps();
747
Greg Daniel16f5c652019-10-29 11:26:01 -0400748 sk_sp<GrWaitRenderTask> waitTask = sk_make_sp<GrWaitRenderTask>(GrSurfaceProxyView(proxy),
749 std::move(semaphores),
Greg Danielc30f1a92019-09-06 15:28:58 -0400750 numSemaphores);
751 if (fReduceOpsTaskSplitting) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400752 GrRenderTask* lastTask = this->getLastRenderTask(proxy.get());
Greg Danielc30f1a92019-09-06 15:28:58 -0400753 if (lastTask && !lastTask->isClosed()) {
754 // We directly make the currently open renderTask depend on waitTask instead of using
755 // the proxy version of addDependency. The waitTask will never need to trigger any
756 // resolves or mip map generation which is the main advantage of going through the proxy
757 // version. Additionally we would've had to temporarily set the wait task as the
758 // lastRenderTask on the proxy, add the dependency, and then reset the lastRenderTask to
759 // lastTask. Additionally we add all dependencies of lastTask to waitTask so that the
760 // waitTask doesn't get reordered before them and unnecessarily block those tasks.
761 // Note: Any previous Ops already in lastTask will get blocked by the wait semaphore
762 // even though they don't need to be for correctness.
763
764 // Make sure we add the dependencies of lastTask to waitTask first or else we'll get a
765 // circular self dependency of waitTask on waitTask.
766 waitTask->addDependenciesFromOtherTask(lastTask);
767 lastTask->addDependency(waitTask.get());
768 } else {
769 // If there is a last task we set the waitTask to depend on it so that it doesn't get
770 // reordered in front of the lastTask causing the lastTask to be blocked by the
771 // semaphore. Again we directly just go through adding the dependency to the task and
772 // not the proxy since we don't need to worry about resolving anything.
773 if (lastTask) {
774 waitTask->addDependency(lastTask);
775 }
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400776 this->setLastRenderTask(proxy.get(), waitTask.get());
Greg Danielc30f1a92019-09-06 15:28:58 -0400777 }
778 fDAG.add(waitTask);
779 } else {
Adlai Holler33d569e2020-06-16 14:30:08 -0400780 if (fActiveOpsTask && (fActiveOpsTask->target(0).proxy() == proxy.get())) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400781 SkASSERT(this->getLastRenderTask(proxy.get()) == fActiveOpsTask);
Greg Danielc30f1a92019-09-06 15:28:58 -0400782 fDAG.addBeforeLast(waitTask);
783 // In this case we keep the current renderTask open but just insert the new waitTask
784 // before it in the list. The waitTask will never need to trigger any resolves or mip
785 // map generation which is the main advantage of going through the proxy version.
786 // Additionally we would've had to temporarily set the wait task as the lastRenderTask
787 // on the proxy, add the dependency, and then reset the lastRenderTask to
788 // fActiveOpsTask. Additionally we make the waitTask depend on all of fActiveOpsTask
789 // dependencies so that we don't unnecessarily reorder the waitTask before them.
790 // Note: Any previous Ops already in fActiveOpsTask will get blocked by the wait
791 // semaphore even though they don't need to be for correctness.
792
793 // Make sure we add the dependencies of fActiveOpsTask to waitTask first or else we'll
794 // get a circular self dependency of waitTask on waitTask.
795 waitTask->addDependenciesFromOtherTask(fActiveOpsTask);
796 fActiveOpsTask->addDependency(waitTask.get());
797 } else {
798 // In this case we just close the previous RenderTask and start and append the waitTask
799 // to the DAG. Since it is the last task now we call setLastRenderTask on the proxy. If
800 // there is a lastTask on the proxy we make waitTask depend on that task. This
801 // dependency isn't strictly needed but it does keep the DAG from reordering the
802 // waitTask earlier and blocking more tasks.
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400803 if (GrRenderTask* lastTask = this->getLastRenderTask(proxy.get())) {
Greg Danielc30f1a92019-09-06 15:28:58 -0400804 waitTask->addDependency(lastTask);
805 }
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400806 this->setLastRenderTask(proxy.get(), waitTask.get());
Greg Danielc30f1a92019-09-06 15:28:58 -0400807 this->closeRenderTasksForNewRenderTask(proxy.get());
808 fDAG.add(waitTask);
809 }
810 }
811 waitTask->makeClosed(caps);
812
813 SkDEBUGCODE(this->validate());
814}
815
Greg Danielbbfec9d2019-08-20 10:56:51 -0400816void GrDrawingManager::newTransferFromRenderTask(sk_sp<GrSurfaceProxy> srcProxy,
817 const SkIRect& srcRect,
818 GrColorType surfaceColorType,
819 GrColorType dstColorType,
820 sk_sp<GrGpuBuffer> dstBuffer,
821 size_t dstOffset) {
822 SkDEBUGCODE(this->validate());
823 SkASSERT(fContext);
824 // This copies from srcProxy to dstBuffer so it doesn't have a real target.
825 this->closeRenderTasksForNewRenderTask(nullptr);
826
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600827 GrRenderTask* task = fDAG.add(sk_make_sp<GrTransferFromRenderTask>(
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400828 srcProxy, srcRect, surfaceColorType, dstColorType,
829 std::move(dstBuffer), dstOffset));
Greg Danielbbfec9d2019-08-20 10:56:51 -0400830
831 const GrCaps& caps = *fContext->priv().caps();
832
Brian Salomon7e67dca2020-07-21 09:27:25 -0400833 // We always say GrMipmapped::kNo here since we are always just copying from the base layer. We
Greg Danielbbfec9d2019-08-20 10:56:51 -0400834 // don't need to make sure the whole mip map chain is valid.
Brian Salomon7e67dca2020-07-21 09:27:25 -0400835 task->addDependency(this, srcProxy.get(), GrMipmapped::kNo,
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400836 GrTextureResolveManager(this), caps);
Greg Danielbbfec9d2019-08-20 10:56:51 -0400837 task->makeClosed(caps);
838
Greg Danielbbfec9d2019-08-20 10:56:51 -0400839 // We have closed the previous active oplist but since a new oplist isn't being added there
840 // shouldn't be an active one.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400841 SkASSERT(!fActiveOpsTask);
Greg Danielbbfec9d2019-08-20 10:56:51 -0400842 SkDEBUGCODE(this->validate());
843}
844
Greg Daniel16f5c652019-10-29 11:26:01 -0400845bool GrDrawingManager::newCopyRenderTask(GrSurfaceProxyView srcView,
Greg Daniele227fe42019-08-21 13:52:24 -0400846 const SkIRect& srcRect,
Greg Daniel16f5c652019-10-29 11:26:01 -0400847 GrSurfaceProxyView dstView,
Greg Daniele227fe42019-08-21 13:52:24 -0400848 const SkIPoint& dstPoint) {
849 SkDEBUGCODE(this->validate());
850 SkASSERT(fContext);
Greg Daniele227fe42019-08-21 13:52:24 -0400851
Greg Daniel16f5c652019-10-29 11:26:01 -0400852 this->closeRenderTasksForNewRenderTask(dstView.proxy());
Brian Salomone4bce012019-09-20 15:34:23 -0400853 const GrCaps& caps = *fContext->priv().caps();
854
Greg Daniel16f5c652019-10-29 11:26:01 -0400855 GrSurfaceProxy* srcProxy = srcView.proxy();
856
Brian Salomone4bce012019-09-20 15:34:23 -0400857 GrRenderTask* task =
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400858 fDAG.add(GrCopyRenderTask::Make(this, std::move(srcView), srcRect, std::move(dstView),
Greg Daniel16f5c652019-10-29 11:26:01 -0400859 dstPoint, &caps));
Greg Daniele227fe42019-08-21 13:52:24 -0400860 if (!task) {
861 return false;
862 }
863
Brian Salomon7e67dca2020-07-21 09:27:25 -0400864 // We always say GrMipmapped::kNo here since we are always just copying from the base layer to
Greg Daniele227fe42019-08-21 13:52:24 -0400865 // another base layer. We don't need to make sure the whole mip map chain is valid.
Brian Salomon7e67dca2020-07-21 09:27:25 -0400866 task->addDependency(this, srcProxy, GrMipmapped::kNo, GrTextureResolveManager(this), caps);
Greg Daniele227fe42019-08-21 13:52:24 -0400867 task->makeClosed(caps);
868
Greg Daniele227fe42019-08-21 13:52:24 -0400869 // We have closed the previous active oplist but since a new oplist isn't being added there
870 // shouldn't be an active one.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400871 SkASSERT(!fActiveOpsTask);
Greg Daniele227fe42019-08-21 13:52:24 -0400872 SkDEBUGCODE(this->validate());
873 return true;
874}
875
robertphillips68737822015-10-29 12:12:21 -0700876/*
877 * This method finds a path renderer that can draw the specified path on
878 * the provided target.
879 * Due to its expense, the software path renderer has split out so it can
880 * can be individually allowed/disallowed via the "allowSW" boolean.
881 */
882GrPathRenderer* GrDrawingManager::getPathRenderer(const GrPathRenderer::CanDrawPathArgs& args,
883 bool allowSW,
884 GrPathRendererChain::DrawType drawType,
885 GrPathRenderer::StencilSupport* stencilSupport) {
886
887 if (!fPathRendererChain) {
John Stilesfbd050b2020-08-03 13:21:46 -0400888 fPathRendererChain =
889 std::make_unique<GrPathRendererChain>(fContext, fOptionsForPathRendererChain);
robertphillips68737822015-10-29 12:12:21 -0700890 }
891
892 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(args, drawType, stencilSupport);
893 if (!pr && allowSW) {
Brian Salomone7df0bb2018-05-07 14:44:57 -0400894 auto swPR = this->getSoftwarePathRenderer();
895 if (GrPathRenderer::CanDrawPath::kNo != swPR->canDrawPath(args)) {
896 pr = swPR;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500897 }
robertphillips68737822015-10-29 12:12:21 -0700898 }
899
Robert Phillipsd81379d2020-04-21 10:39:02 -0400900#if GR_PATH_RENDERER_SPEW
901 if (pr) {
902 SkDebugf("getPathRenderer: %s\n", pr->name());
903 }
904#endif
905
robertphillips68737822015-10-29 12:12:21 -0700906 return pr;
907}
908
Brian Salomone7df0bb2018-05-07 14:44:57 -0400909GrPathRenderer* GrDrawingManager::getSoftwarePathRenderer() {
910 if (!fSoftwarePathRenderer) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400911 fSoftwarePathRenderer.reset(
Robert Phillips9da87e02019-02-04 13:26:26 -0500912 new GrSoftwarePathRenderer(fContext->priv().proxyProvider(),
Ben Wagner9ec70c62018-07-12 13:30:47 -0400913 fOptionsForPathRendererChain.fAllowPathMaskCaching));
Brian Salomone7df0bb2018-05-07 14:44:57 -0400914 }
Ben Wagner9ec70c62018-07-12 13:30:47 -0400915 return fSoftwarePathRenderer.get();
Brian Salomone7df0bb2018-05-07 14:44:57 -0400916}
917
Chris Daltonfddb6c02017-11-04 15:22:22 -0600918GrCoverageCountingPathRenderer* GrDrawingManager::getCoverageCountingPathRenderer() {
919 if (!fPathRendererChain) {
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500920 fPathRendererChain = std::make_unique<GrPathRendererChain>(fContext,
921 fOptionsForPathRendererChain);
Chris Daltonfddb6c02017-11-04 15:22:22 -0600922 }
923 return fPathRendererChain->getCoverageCountingPathRenderer();
924}
925
Brian Salomon653f42f2018-07-10 10:07:31 -0400926void GrDrawingManager::flushIfNecessary() {
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400927 auto direct = fContext->asDirectContext();
Robert Phillips6a6de562019-02-15 15:19:15 -0500928 if (!direct) {
929 return;
930 }
931
932 auto resourceCache = direct->priv().getResourceCache();
Brian Salomon653f42f2018-07-10 10:07:31 -0400933 if (resourceCache && resourceCache->requestsFlush()) {
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500934 if (this->flush({}, SkSurface::BackendSurfaceAccess::kNoAccess, GrFlushInfo(), nullptr)) {
Greg Danielfe159622020-04-10 17:43:51 +0000935 this->submitToGpu(false);
936 }
Brian Salomon57d2beab2018-09-10 09:35:41 -0400937 resourceCache->purgeAsNeeded();
Brian Salomon653f42f2018-07-10 10:07:31 -0400938 }
939}
Robert Phillipsa0e248d2020-11-11 09:39:27 -0500940