blob: 1b090e759f66435e780605d4dff0abf2aedf6dbf [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"
Adlai Holler6c9bb622020-06-25 09:21:18 -040041#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 {
48 idArray->reset(fRenderTasks.count());
49 for (int i = 0; i < fRenderTasks.count(); ++i) {
50 if (fRenderTasks[i]) {
51 (*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
Greg Danielf41b2bd2019-08-22 16:19:24 -040094void GrDrawingManager::RenderTaskDAG::add(const SkTArray<sk_sp<GrRenderTask>>& renderTasks) {
Robert Phillips19f466d2020-02-26 10:27:07 -050095#ifdef SK_DEBUG
96 for (auto& renderTask : renderTasks) {
97 SkASSERT(renderTask->unique());
98 }
99#endif
100
Greg Danielf41b2bd2019-08-22 16:19:24 -0400101 fRenderTasks.push_back_n(renderTasks.count(), renderTasks.begin());
Robert Phillips22310d62018-09-05 11:07:21 -0400102}
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) {
Chris Dalton6b498102019-08-01 14:14:52 -0600246 onFlushCBObject->preFlush(&onFlushProvider, fFlushingRenderTaskIDs.begin(),
Chris Daltonc4b47352019-08-23 10:10:36 -0600247 fFlushingRenderTaskIDs.count());
248 }
249 for (const auto& onFlushRenderTask : fOnFlushRenderTasks) {
250 onFlushRenderTask->makeClosed(*fContext->priv().caps());
Chris Dalton706a6ff2017-11-29 22:01:06 -0700251#ifdef SK_DEBUG
Chris Daltonc4b47352019-08-23 10:10:36 -0600252 // OnFlush callbacks are invoked during flush, and are therefore expected to handle
253 // resource allocation & usage on their own. (No deferred or lazy proxies!)
254 onFlushRenderTask->visitTargetAndSrcProxies_debugOnly(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400255 [](GrSurfaceProxy* p, GrMipmapped mipMapped) {
Chris Daltonc4b47352019-08-23 10:10:36 -0600256 SkASSERT(!p->asTextureProxy() || !p->asTextureProxy()->texPriv().isDeferred());
Brian Salomonbeb7f522019-08-30 16:19:42 -0400257 SkASSERT(!p->isLazy());
Chris Dalton4ece96d2019-08-30 11:26:39 -0600258 if (p->requiresManualMSAAResolve()) {
259 // The onFlush callback is responsible for ensuring MSAA gets resolved.
260 SkASSERT(p->asRenderTargetProxy() && !p->asRenderTargetProxy()->isMSAADirty());
261 }
Brian Salomon7e67dca2020-07-21 09:27:25 -0400262 if (GrMipmapped::kYes == mipMapped) {
Chris Daltonc4b47352019-08-23 10:10:36 -0600263 // The onFlush callback is responsible for regenerating mips if needed.
Brian Salomon8c82a872020-07-21 12:09:58 -0400264 SkASSERT(p->asTextureProxy() && !p->asTextureProxy()->mipmapsAreDirty());
Chris Daltonc4b47352019-08-23 10:10:36 -0600265 }
266 });
Chris Dalton706a6ff2017-11-29 22:01:06 -0700267#endif
Chris Daltonc4b47352019-08-23 10:10:36 -0600268 onFlushRenderTask->prepare(&flushState);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400269 }
270 }
271
robertphillipsa13e2022015-11-11 12:01:09 -0800272#if 0
Brian Salomon09d994e2016-12-21 11:14:46 -0500273 // Enable this to print out verbose GrOp information
Adlai Hollerabdfd392020-10-19 09:00:36 -0400274 SkDEBUGCODE(SkDebugf("onFlush renderTasks (%d):\n", fOnFlushRenderTasks.count()));
Chris Daltonc4b47352019-08-23 10:10:36 -0600275 for (const auto& onFlushRenderTask : fOnFlushRenderTasks) {
Adlai Hollerabdfd392020-10-19 09:00:36 -0400276 SkDEBUGCODE(onFlushRenderTask->dump(/* printDependencies */ true);)
Chris Daltonc4b47352019-08-23 10:10:36 -0600277 }
Adlai Hollerabdfd392020-10-19 09:00:36 -0400278 SkDEBUGCODE(SkDebugf("Normal renderTasks (%d):\n", fDAG.numRenderTasks()));
279 for (int i = 0; i < fDAG.numRenderTasks(); ++i) {
280 SkDEBUGCODE(fDAG.renderTask(i)->dump(/* printDependencies */ true);)
robertphillips3dc6ae52015-10-20 09:54:32 -0700281 }
robertphillipsa13e2022015-11-11 12:01:09 -0800282#endif
283
Robert Phillipseafd48a2017-11-16 07:52:08 -0500284 int startIndex, stopIndex;
285 bool flushed = false;
286
Robert Phillipsf8e25022017-11-08 15:24:31 -0500287 {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400288 GrResourceAllocator alloc(resourceProvider SkDEBUGCODE(, fDAG.numRenderTasks()));
Chris Dalton6b498102019-08-01 14:14:52 -0600289 for (int i = 0; i < fDAG.numRenderTasks(); ++i) {
290 if (fDAG.renderTask(i)) {
291 fDAG.renderTask(i)->gatherProxyIntervals(&alloc);
Robert Phillips22310d62018-09-05 11:07:21 -0400292 }
Greg Danielf41b2bd2019-08-22 16:19:24 -0400293 alloc.markEndOfOpsTask(i);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500294 }
Robert Phillipsc73666f2019-04-24 08:49:48 -0400295 alloc.determineRecyclability();
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400296
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500297 GrResourceAllocator::AssignError error = GrResourceAllocator::AssignError::kNoError;
Chris Dalton6b498102019-08-01 14:14:52 -0600298 int numRenderTasksExecuted = 0;
Brian Salomon577aa0f2018-11-30 13:32:23 -0500299 while (alloc.assign(&startIndex, &stopIndex, &error)) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500300 if (GrResourceAllocator::AssignError::kFailedProxyInstantiation == error) {
301 for (int i = startIndex; i < stopIndex; ++i) {
Chris Dalton6b498102019-08-01 14:14:52 -0600302 GrRenderTask* renderTask = fDAG.renderTask(i);
303 if (!renderTask) {
304 continue;
Robert Phillips01a91282018-07-26 08:03:04 -0400305 }
Chris Dalton6b498102019-08-01 14:14:52 -0600306 if (!renderTask->isInstantiated()) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400307 // No need to call the renderTask's handleInternalAllocationFailure
308 // since we will already skip executing the renderTask since it is not
309 // instantiated.
Chris Dalton6b498102019-08-01 14:14:52 -0600310 continue;
Robert Phillipsbbcb7f72018-05-31 11:16:19 -0400311 }
Chris Dalton6b498102019-08-01 14:14:52 -0600312 renderTask->handleInternalAllocationFailure();
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500313 }
Adlai Holler96ead542020-06-26 08:50:14 -0400314 this->removeRenderTasks(startIndex, stopIndex);
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500315 }
Robert Phillips4150eea2018-02-07 17:08:21 -0500316
Chris Dalton6b498102019-08-01 14:14:52 -0600317 if (this->executeRenderTasks(
318 startIndex, stopIndex, &flushState, &numRenderTasksExecuted)) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500319 flushed = true;
320 }
bsalomondc438982016-08-31 11:53:49 -0700321 }
Greg Danielc42b20b2017-10-04 10:34:49 -0400322 }
323
Chris Dalton91ab1552018-04-18 13:24:25 -0600324#ifdef SK_DEBUG
Chris Dalton6b498102019-08-01 14:14:52 -0600325 for (int i = 0; i < fDAG.numRenderTasks(); ++i) {
Adlai Holler96ead542020-06-26 08:50:14 -0400326 // All render tasks should have been cleared out by now – we only reset the array below to
327 // reclaim storage.
328 SkASSERT(!fDAG.renderTask(i));
Chris Dalton91ab1552018-04-18 13:24:25 -0600329 }
330#endif
Adlai Holler25df1f72020-06-09 13:08:27 -0400331 fLastRenderTasks.reset();
Robert Phillips22310d62018-09-05 11:07:21 -0400332 fDAG.reset();
Robert Phillips15c91422019-05-07 16:54:48 -0400333 this->clearDDLTargets();
robertphillipsa13e2022015-11-11 12:01:09 -0800334
Robert Phillipsc994a932018-06-19 13:09:54 -0400335#ifdef SK_DEBUG
336 // In non-DDL mode this checks that all the flushed ops have been freed from the memory pool.
337 // When we move to partial flushes this assert will no longer be valid.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400338 // In DDL mode this check is somewhat superfluous since the memory for most of the ops/opsTasks
Robert Phillipsc994a932018-06-19 13:09:54 -0400339 // will be stored in the DDL's GrOpMemoryPools.
Herb Derbye32e1ab2020-10-27 10:29:46 -0400340 GrMemoryPool* opMemoryPool = fContext->priv().opMemoryPool();
Robert Phillipsc994a932018-06-19 13:09:54 -0400341 opMemoryPool->isEmpty();
342#endif
343
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500344 gpu->executeFlushInfo(proxies, access, info, newState);
robertphillipsa13e2022015-11-11 12:01:09 -0800345
Brian Salomon57d2beab2018-09-10 09:35:41 -0400346 // Give the cache a chance to purge resources that become purgeable due to flushing.
347 if (flushed) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500348 resourceCache->purgeAsNeeded();
Brian Salomon876a0172019-03-08 11:12:14 -0500349 flushed = false;
bsalomonb77a9072016-09-07 10:02:04 -0700350 }
Chris Daltonfe199b72017-05-05 11:26:15 -0400351 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
Chris Dalton6b498102019-08-01 14:14:52 -0600352 onFlushCBObject->postFlush(fTokenTracker.nextTokenToFlush(), fFlushingRenderTaskIDs.begin(),
353 fFlushingRenderTaskIDs.count());
Brian Salomon876a0172019-03-08 11:12:14 -0500354 flushed = true;
355 }
356 if (flushed) {
357 resourceCache->purgeAsNeeded();
Chris Daltonfe199b72017-05-05 11:26:15 -0400358 }
Chris Dalton6b498102019-08-01 14:14:52 -0600359 fFlushingRenderTaskIDs.reset();
joshualittb8918c42015-12-18 09:59:46 -0800360 fFlushing = false;
Greg Daniel51316782017-08-02 15:10:09 +0000361
Greg Danielfe159622020-04-10 17:43:51 +0000362 return true;
363}
364
365bool GrDrawingManager::submitToGpu(bool syncToCpu) {
366 if (fFlushing || this->wasAbandoned()) {
367 return false;
368 }
369
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400370 auto direct = fContext->asDirectContext();
Greg Danielfe159622020-04-10 17:43:51 +0000371 if (!direct) {
372 return false; // Can't submit while DDL recording
373 }
374 GrGpu* gpu = direct->priv().getGpu();
375 return gpu->submitToGpu(syncToCpu);
robertphillips3dc6ae52015-10-20 09:54:32 -0700376}
377
Chris Dalton6b498102019-08-01 14:14:52 -0600378bool GrDrawingManager::executeRenderTasks(int startIndex, int stopIndex, GrOpFlushState* flushState,
379 int* numRenderTasksExecuted) {
380 SkASSERT(startIndex <= stopIndex && stopIndex <= fDAG.numRenderTasks());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500381
Robert Phillips27483912018-04-20 12:43:18 -0400382#if GR_FLUSH_TIME_OP_SPEW
Greg Danielf41b2bd2019-08-22 16:19:24 -0400383 SkDebugf("Flushing opsTask: %d to %d out of [%d, %d]\n",
Chris Dalton6b498102019-08-01 14:14:52 -0600384 startIndex, stopIndex, 0, fDAG.numRenderTasks());
Robert Phillips27483912018-04-20 12:43:18 -0400385 for (int i = startIndex; i < stopIndex; ++i) {
Chris Dalton6b498102019-08-01 14:14:52 -0600386 if (fDAG.renderTask(i)) {
387 fDAG.renderTask(i)->dump(true);
Robert Phillips1734dd32018-08-21 13:52:09 -0400388 }
Robert Phillips27483912018-04-20 12:43:18 -0400389 }
390#endif
391
Chris Dalton6b498102019-08-01 14:14:52 -0600392 bool anyRenderTasksExecuted = false;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500393
394 for (int i = startIndex; i < stopIndex; ++i) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400395 GrRenderTask* renderTask = fDAG.renderTask(i);
396 if (!renderTask || !renderTask->isInstantiated()) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500397 continue;
398 }
399
Chris Dalton6b498102019-08-01 14:14:52 -0600400 SkASSERT(renderTask->deferredProxiesAreInstantiated());
Robert Phillips22310d62018-09-05 11:07:21 -0400401
Chris Dalton6b498102019-08-01 14:14:52 -0600402 renderTask->prepare(flushState);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500403 }
404
405 // Upload all data to the GPU
406 flushState->preExecuteDraws();
407
Greg Danield2073452018-12-07 11:20:33 -0500408 // For Vulkan, if we have too many oplists to be flushed we end up allocating a lot of resources
409 // for each command buffer associated with the oplists. If this gets too large we can cause the
410 // devices to go OOM. In practice we usually only hit this case in our tests, but to be safe we
411 // put a cap on the number of oplists we will execute before flushing to the GPU to relieve some
412 // memory pressure.
Chris Dalton6b498102019-08-01 14:14:52 -0600413 static constexpr int kMaxRenderTasksBeforeFlush = 100;
Greg Danield2073452018-12-07 11:20:33 -0500414
Chris Daltonc4b47352019-08-23 10:10:36 -0600415 // Execute the onFlush renderTasks first, if any.
416 for (sk_sp<GrRenderTask>& onFlushRenderTask : fOnFlushRenderTasks) {
417 if (!onFlushRenderTask->execute(flushState)) {
418 SkDebugf("WARNING: onFlushRenderTask failed to execute.\n");
Robert Phillipseafd48a2017-11-16 07:52:08 -0500419 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600420 SkASSERT(onFlushRenderTask->unique());
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400421 onFlushRenderTask->disown(this);
Chris Daltonc4b47352019-08-23 10:10:36 -0600422 onFlushRenderTask = nullptr;
Chris Dalton6b498102019-08-01 14:14:52 -0600423 (*numRenderTasksExecuted)++;
424 if (*numRenderTasksExecuted >= kMaxRenderTasksBeforeFlush) {
Greg Danielfe159622020-04-10 17:43:51 +0000425 flushState->gpu()->submitToGpu(false);
Chris Dalton6b498102019-08-01 14:14:52 -0600426 *numRenderTasksExecuted = 0;
Greg Danield2073452018-12-07 11:20:33 -0500427 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500428 }
Chris Daltonc4b47352019-08-23 10:10:36 -0600429 fOnFlushRenderTasks.reset();
Robert Phillipseafd48a2017-11-16 07:52:08 -0500430
431 // Execute the normal op lists.
432 for (int i = startIndex; i < stopIndex; ++i) {
Greg Daniel15ecdf92019-08-30 15:35:23 -0400433 GrRenderTask* renderTask = fDAG.renderTask(i);
434 if (!renderTask || !renderTask->isInstantiated()) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500435 continue;
436 }
437
Greg Daniel15ecdf92019-08-30 15:35:23 -0400438 if (renderTask->execute(flushState)) {
Chris Dalton6b498102019-08-01 14:14:52 -0600439 anyRenderTasksExecuted = true;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500440 }
Chris Dalton6b498102019-08-01 14:14:52 -0600441 (*numRenderTasksExecuted)++;
442 if (*numRenderTasksExecuted >= kMaxRenderTasksBeforeFlush) {
Greg Danielfe159622020-04-10 17:43:51 +0000443 flushState->gpu()->submitToGpu(false);
Chris Dalton6b498102019-08-01 14:14:52 -0600444 *numRenderTasksExecuted = 0;
Greg Danield2073452018-12-07 11:20:33 -0500445 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500446 }
447
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400448 SkASSERT(!flushState->opsRenderPass());
Robert Phillips40a29d72018-01-18 12:59:22 -0500449 SkASSERT(fTokenTracker.nextDrawToken() == fTokenTracker.nextTokenToFlush());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500450
Chris Dalton6b498102019-08-01 14:14:52 -0600451 // We reset the flush state before the RenderTasks so that the last resources to be freed are
452 // those that are written to in the RenderTasks. This helps to make sure the most recently used
453 // resources are the last to be purged by the resource cache.
Robert Phillipseafd48a2017-11-16 07:52:08 -0500454 flushState->reset();
455
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400456 this->removeRenderTasks(startIndex, stopIndex);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500457
Chris Dalton6b498102019-08-01 14:14:52 -0600458 return anyRenderTasksExecuted;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500459}
460
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400461void GrDrawingManager::removeRenderTasks(int startIndex, int stopIndex) {
462 for (int i = startIndex; i < stopIndex; ++i) {
463 GrRenderTask* task = fDAG.renderTask(i);
464 if (!task) {
465 continue;
466 }
467 if (!task->unique()) {
468 // TODO: Eventually this should be guaranteed unique: http://skbug.com/7111
469 task->endFlush(this);
470 }
471 task->disown(this);
472 }
Adlai Holler96ead542020-06-26 08:50:14 -0400473 fDAG.rawRemoveRenderTasks(startIndex, stopIndex);
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400474}
475
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400476static void resolve_and_mipmap(GrGpu* gpu, GrSurfaceProxy* proxy) {
477 if (!proxy->isInstantiated()) {
478 return;
479 }
480
481 // In the flushSurfaces case, we need to resolve MSAA immediately after flush. This is
482 // because clients expect the flushed surface's backing texture to be fully resolved
483 // upon return.
484 if (proxy->requiresManualMSAAResolve()) {
485 auto* rtProxy = proxy->asRenderTargetProxy();
486 SkASSERT(rtProxy);
487 if (rtProxy->isMSAADirty()) {
488 SkASSERT(rtProxy->peekRenderTarget());
Jim Van Verthbb61fe32020-07-07 16:39:04 -0400489 gpu->resolveRenderTarget(rtProxy->peekRenderTarget(), rtProxy->msaaDirtyRect());
490 gpu->submitToGpu(false);
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400491 rtProxy->markMSAAResolved();
492 }
493 }
494 // If, after a flush, any of the proxies of interest have dirty mipmaps, regenerate them in
495 // case their backend textures are being stolen.
496 // (This special case is exercised by the ReimportImageTextureWithMipLevels test.)
497 // FIXME: It may be more ideal to plumb down a "we're going to steal the backends" flag.
498 if (auto* textureProxy = proxy->asTextureProxy()) {
Brian Salomon8c82a872020-07-21 12:09:58 -0400499 if (textureProxy->mipmapsAreDirty()) {
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400500 SkASSERT(textureProxy->peekTexture());
501 gpu->regenerateMipMapLevels(textureProxy->peekTexture());
Brian Salomon8c82a872020-07-21 12:09:58 -0400502 textureProxy->markMipmapsClean();
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400503 }
504 }
505}
506
Greg Daniel9efe3862020-06-11 11:51:06 -0400507GrSemaphoresSubmitted GrDrawingManager::flushSurfaces(
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500508 SkSpan<GrSurfaceProxy*> proxies,
Greg Daniel9efe3862020-06-11 11:51:06 -0400509 SkSurface::BackendSurfaceAccess access,
510 const GrFlushInfo& info,
511 const GrBackendSurfaceMutableState* newState) {
bsalomon6a2b1942016-09-08 11:28:59 -0700512 if (this->wasAbandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -0400513 if (info.fSubmittedProc) {
514 info.fSubmittedProc(info.fSubmittedContext, false);
515 }
516 if (info.fFinishedProc) {
517 info.fFinishedProc(info.fFinishedContext);
518 }
Greg Daniel51316782017-08-02 15:10:09 +0000519 return GrSemaphoresSubmitted::kNo;
bsalomon6a2b1942016-09-08 11:28:59 -0700520 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400521 SkDEBUGCODE(this->validate());
bsalomon6a2b1942016-09-08 11:28:59 -0700522
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400523 auto direct = fContext->asDirectContext();
Adlai Holleraee25fd2020-11-06 12:34:56 -0500524 SkASSERT(direct);
Robert Phillips6a6de562019-02-15 15:19:15 -0500525 GrGpu* gpu = direct->priv().getGpu();
Greg Daniel55822f12020-05-26 11:26:45 -0400526 // We have a non abandoned and direct GrContext. It must have a GrGpu.
527 SkASSERT(gpu);
Robert Phillips874b5352018-03-16 08:48:24 -0400528
Robert Phillipsacc10fa2019-04-01 09:50:20 -0400529 // TODO: It is important to upgrade the drawingmanager to just flushing the
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400530 // portion of the DAG required by 'proxies' in order to restore some of the
Robert Phillipsacc10fa2019-04-01 09:50:20 -0400531 // semantics of this method.
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500532 bool didFlush = this->flush(proxies, access, info, newState);
533 for (GrSurfaceProxy* proxy : proxies) {
534 resolve_and_mipmap(gpu, proxy);
bsalomon6a2b1942016-09-08 11:28:59 -0700535 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400536
537 SkDEBUGCODE(this->validate());
Greg Danielfe159622020-04-10 17:43:51 +0000538
Greg Daniel04283f32020-05-20 13:16:00 -0400539 if (!didFlush || (!direct->priv().caps()->semaphoreSupport() && info.fNumSemaphores)) {
Greg Danielfe159622020-04-10 17:43:51 +0000540 return GrSemaphoresSubmitted::kNo;
541 }
542 return GrSemaphoresSubmitted::kYes;
bsalomon6a2b1942016-09-08 11:28:59 -0700543}
544
Chris Daltonfe199b72017-05-05 11:26:15 -0400545void GrDrawingManager::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
546 fOnFlushCBObjects.push_back(onFlushCBObject);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400547}
548
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500549#if GR_TEST_UTILS
550void GrDrawingManager::testingOnly_removeOnFlushCallbackObject(GrOnFlushCallbackObject* cb) {
551 int n = std::find(fOnFlushCBObjects.begin(), fOnFlushCBObjects.end(), cb) -
552 fOnFlushCBObjects.begin();
553 SkASSERT(n < fOnFlushCBObjects.count());
554 fOnFlushCBObjects.removeShuffle(n);
555}
556#endif
557
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400558void GrDrawingManager::setLastRenderTask(const GrSurfaceProxy* proxy, GrRenderTask* task) {
559#ifdef SK_DEBUG
560 if (GrRenderTask* prior = this->getLastRenderTask(proxy)) {
561 SkASSERT(prior->isClosed());
562 }
563#endif
Adlai Holler25df1f72020-06-09 13:08:27 -0400564 uint32_t key = proxy->uniqueID().asUInt();
565 if (task) {
566 fLastRenderTasks.set(key, task);
567 } else if (fLastRenderTasks.find(key)) {
568 fLastRenderTasks.remove(key);
569 }
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400570}
571
572GrRenderTask* GrDrawingManager::getLastRenderTask(const GrSurfaceProxy* proxy) const {
573 auto entry = fLastRenderTasks.find(proxy->uniqueID().asUInt());
574 return entry ? *entry : nullptr;
575}
576
577GrOpsTask* GrDrawingManager::getLastOpsTask(const GrSurfaceProxy* proxy) const {
578 GrRenderTask* task = this->getLastRenderTask(proxy);
579 return task ? task->asOpsTask() : nullptr;
580}
581
582
Chris Dalton6b498102019-08-01 14:14:52 -0600583void GrDrawingManager::moveRenderTasksToDDL(SkDeferredDisplayList* ddl) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400584 SkDEBUGCODE(this->validate());
585
Chris Dalton6b498102019-08-01 14:14:52 -0600586 // no renderTask should receive a new command after this
Robert Phillips9da87e02019-02-04 13:26:26 -0500587 fDAG.closeAll(fContext->priv().caps());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400588 fActiveOpsTask = nullptr;
Robert Phillips7a137052018-02-01 11:23:12 -0500589
Chris Dalton6b498102019-08-01 14:14:52 -0600590 fDAG.swap(&ddl->fRenderTasks);
Robert Phillips19f466d2020-02-26 10:27:07 -0500591 SkASSERT(!fDAG.numRenderTasks());
Robert Phillips867ce8f2018-06-21 10:28:36 -0400592
Robert Phillips19f466d2020-02-26 10:27:07 -0500593 for (auto& renderTask : ddl->fRenderTasks) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400594 renderTask->disown(this);
Robert Phillips29f38542019-10-16 09:20:25 -0400595 renderTask->prePrepare(fContext);
Robert Phillips7327c9d2019-10-08 16:32:56 -0400596 }
597
Michael Ludwig2c316bd2019-12-19 14:50:44 -0500598 ddl->fArenas = std::move(fContext->priv().detachArenas());
Robert Phillips61fc7992019-10-22 11:58:17 -0400599
Robert Phillipsf6a0b452020-02-18 14:26:46 -0500600 fContext->priv().detachProgramData(&ddl->fProgramData);
Robert Phillips576b6a12019-12-06 13:05:49 -0500601
Robert Phillips774168e2018-05-31 12:43:27 -0400602 if (fPathRendererChain) {
603 if (auto ccpr = fPathRendererChain->getCoverageCountingPathRenderer()) {
604 ddl->fPendingPaths = ccpr->detachPendingPaths();
605 }
606 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400607
608 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500609}
610
Adlai Holler7580ad42020-06-24 13:45:25 -0400611void GrDrawingManager::copyRenderTasksFromDDL(sk_sp<const SkDeferredDisplayList> ddl,
612 GrRenderTargetProxy* newDest) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400613 SkDEBUGCODE(this->validate());
614
Greg Danielf41b2bd2019-08-22 16:19:24 -0400615 if (fActiveOpsTask) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400616 // This is a temporary fix for the partial-MDB world. In that world we're not
Greg Danielf41b2bd2019-08-22 16:19:24 -0400617 // reordering so ops that (in the single opsTask world) would've just glommed onto the
618 // end of the single opsTask but referred to a far earlier RT need to appear in their
619 // own opsTask.
620 fActiveOpsTask->makeClosed(*fContext->priv().caps());
621 fActiveOpsTask = nullptr;
Robert Phillips38d64b02018-09-04 13:23:26 -0400622 }
623
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400624 // Propagate the DDL proxy's state information to the replaying DDL.
625 if (ddl->priv().targetProxy()->isMSAADirty()) {
626 newDest->markMSAADirty(ddl->priv().targetProxy()->msaaDirtyRect(),
627 ddl->characterization().origin());
628 }
629 GrTextureProxy* newTextureProxy = newDest->asTextureProxy();
Brian Salomon8c82a872020-07-21 12:09:58 -0400630 if (newTextureProxy && GrMipmapped::kYes == newTextureProxy->mipmapped()) {
631 newTextureProxy->markMipmapsDirty();
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400632 }
633
634 this->addDDLTarget(newDest, ddl->priv().targetProxy());
Robert Phillips15c91422019-05-07 16:54:48 -0400635
Robert Phillips62000362018-02-01 09:10:04 -0500636 // Here we jam the proxy that backs the current replay SkSurface into the LazyProxyData.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400637 // The lazy proxy that references it (in the copied opsTasks) will steal its GrTexture.
Robert Phillips62000362018-02-01 09:10:04 -0500638 ddl->fLazyProxyData->fReplayDest = newDest;
Robert Phillips774168e2018-05-31 12:43:27 -0400639
640 if (ddl->fPendingPaths.size()) {
641 GrCoverageCountingPathRenderer* ccpr = this->getCoverageCountingPathRenderer();
642
643 ccpr->mergePendingPaths(ddl->fPendingPaths);
644 }
Robert Phillips22310d62018-09-05 11:07:21 -0400645
Chris Dalton6b498102019-08-01 14:14:52 -0600646 fDAG.add(ddl->fRenderTasks);
Robert Phillips38d64b02018-09-04 13:23:26 -0400647
Adlai Holler6c9bb622020-06-25 09:21:18 -0400648 // Add a task to unref the DDL after flush.
649 GrRenderTask* unrefTask = fDAG.add(sk_make_sp<GrUnrefDDLTask>(std::move(ddl)));
650 unrefTask->makeClosed(*fContext->priv().caps());
Adlai Holler7580ad42020-06-24 13:45:25 -0400651
Robert Phillips38d64b02018-09-04 13:23:26 -0400652 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500653}
654
Robert Phillips38d64b02018-09-04 13:23:26 -0400655#ifdef SK_DEBUG
656void GrDrawingManager::validate() const {
Adlai Holler3078f852020-11-05 15:44:50 -0500657 if (fReduceOpsTaskSplitting) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400658 SkASSERT(!fActiveOpsTask);
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400659 } else {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400660 if (fActiveOpsTask) {
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400661 SkASSERT(!fDAG.empty());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400662 SkASSERT(!fActiveOpsTask->isClosed());
663 SkASSERT(fActiveOpsTask == fDAG.back());
Robert Phillips38d64b02018-09-04 13:23:26 -0400664 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400665
Chris Dalton6b498102019-08-01 14:14:52 -0600666 for (int i = 0; i < fDAG.numRenderTasks(); ++i) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400667 if (fActiveOpsTask != fDAG.renderTask(i)) {
Chris Daltone2a903e2019-09-18 13:41:50 -0600668 // The resolveTask associated with the activeTask remains open for as long as the
669 // activeTask does.
670 bool isActiveResolveTask =
671 fActiveOpsTask && fActiveOpsTask->fTextureResolveTask == fDAG.renderTask(i);
672 SkASSERT(isActiveResolveTask || fDAG.renderTask(i)->isClosed());
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400673 }
674 }
675
676 if (!fDAG.empty() && !fDAG.back()->isClosed()) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400677 SkASSERT(fActiveOpsTask == fDAG.back());
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400678 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400679 }
680}
681#endif
682
Greg Danielbbfec9d2019-08-20 10:56:51 -0400683void GrDrawingManager::closeRenderTasksForNewRenderTask(GrSurfaceProxy* target) {
Adlai Holler3078f852020-11-05 15:44:50 -0500684 if (target && fReduceOpsTaskSplitting) {
Chris Dalton5fe99772019-08-06 11:57:39 -0600685 // In this case we need to close all the renderTasks that rely on the current contents of
686 // 'target'. That is bc we're going to update the content of the proxy so they need to be
687 // split in case they use both the old and new content. (This is a bit of an overkill: they
688 // really only need to be split if they ever reference proxy's contents again but that is
689 // hard to predict/handle).
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400690 if (GrRenderTask* lastRenderTask = this->getLastRenderTask(target)) {
Chris Dalton6b498102019-08-01 14:14:52 -0600691 lastRenderTask->closeThoseWhoDependOnMe(*fContext->priv().caps());
Robert Phillips46acf9d2018-10-09 09:31:40 -0400692 }
Greg Danielf41b2bd2019-08-22 16:19:24 -0400693 } else if (fActiveOpsTask) {
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400694 // This is a temporary fix for the partial-MDB world. In that world we're not
Greg Danielf41b2bd2019-08-22 16:19:24 -0400695 // reordering so ops that (in the single opsTask world) would've just glommed onto the
696 // end of the single opsTask but referred to a far earlier RT need to appear in their
697 // own opsTask.
698 fActiveOpsTask->makeClosed(*fContext->priv().caps());
699 fActiveOpsTask = nullptr;
robertphillips3dc6ae52015-10-20 09:54:32 -0700700 }
Chris Dalton5fe99772019-08-06 11:57:39 -0600701}
702
Greg Daniel16f5c652019-10-29 11:26:01 -0400703sk_sp<GrOpsTask> GrDrawingManager::newOpsTask(GrSurfaceProxyView surfaceView,
704 bool managedOpsTask) {
Chris Dalton5fe99772019-08-06 11:57:39 -0600705 SkDEBUGCODE(this->validate());
706 SkASSERT(fContext);
707
Greg Daniel16f5c652019-10-29 11:26:01 -0400708 GrSurfaceProxy* proxy = surfaceView.proxy();
709 this->closeRenderTasksForNewRenderTask(proxy);
robertphillips3dc6ae52015-10-20 09:54:32 -0700710
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400711 sk_sp<GrOpsTask> opsTask(new GrOpsTask(this, fContext->priv().arenas(),
Michael Ludwigd0840ec2019-12-12 09:48:38 -0500712 std::move(surfaceView),
713 fContext->priv().auditTrail()));
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400714 SkASSERT(this->getLastRenderTask(proxy) == opsTask.get());
robertphillips3dc6ae52015-10-20 09:54:32 -0700715
Greg Danielf41b2bd2019-08-22 16:19:24 -0400716 if (managedOpsTask) {
717 fDAG.add(opsTask);
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400718
Adlai Holler3078f852020-11-05 15:44:50 -0500719 if (!fReduceOpsTaskSplitting) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400720 fActiveOpsTask = opsTask.get();
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400721 }
Robert Phillips941d1442017-06-14 16:37:02 -0400722 }
robertphillips3dc6ae52015-10-20 09:54:32 -0700723
Robert Phillips38d64b02018-09-04 13:23:26 -0400724 SkDEBUGCODE(this->validate());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400725 return opsTask;
robertphillips3dc6ae52015-10-20 09:54:32 -0700726}
727
Chris Daltone2a903e2019-09-18 13:41:50 -0600728GrTextureResolveRenderTask* GrDrawingManager::newTextureResolveRenderTask(const GrCaps& caps) {
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600729 // 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 -0600730 // in sorting and opsTask reduction mode) the render tasks that depend on any proxy's current
Greg Danielf41b2bd2019-08-22 16:19:24 -0400731 // state. This is because those opsTasks can still receive new ops and because if they refer to
Chris Dalton4ece96d2019-08-30 11:26:39 -0600732 // the mipmapped version of 'proxy', they will then come to depend on the render task being
733 // created here.
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600734 //
Greg Danielf41b2bd2019-08-22 16:19:24 -0400735 // Add the new textureResolveTask before the fActiveOpsTask (if not in
736 // sorting/opsTask-splitting-reduction mode) because it will depend upon this resolve task.
Chris Dalton3d770272019-08-14 09:24:37 -0600737 // NOTE: Putting it here will also reduce the amount of work required by the topological sort.
Chris Daltone2a903e2019-09-18 13:41:50 -0600738 return static_cast<GrTextureResolveRenderTask*>(fDAG.addBeforeLast(
739 sk_make_sp<GrTextureResolveRenderTask>()));
Chris Dalton3d770272019-08-14 09:24:37 -0600740}
741
Greg Danielc30f1a92019-09-06 15:28:58 -0400742void GrDrawingManager::newWaitRenderTask(sk_sp<GrSurfaceProxy> proxy,
Greg Daniel301015c2019-11-18 14:06:46 -0500743 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> semaphores,
Greg Danielc30f1a92019-09-06 15:28:58 -0400744 int numSemaphores) {
745 SkDEBUGCODE(this->validate());
746 SkASSERT(fContext);
747
748 const GrCaps& caps = *fContext->priv().caps();
749
Greg Daniel16f5c652019-10-29 11:26:01 -0400750 sk_sp<GrWaitRenderTask> waitTask = sk_make_sp<GrWaitRenderTask>(GrSurfaceProxyView(proxy),
751 std::move(semaphores),
Greg Danielc30f1a92019-09-06 15:28:58 -0400752 numSemaphores);
753 if (fReduceOpsTaskSplitting) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400754 GrRenderTask* lastTask = this->getLastRenderTask(proxy.get());
Greg Danielc30f1a92019-09-06 15:28:58 -0400755 if (lastTask && !lastTask->isClosed()) {
756 // We directly make the currently open renderTask depend on waitTask instead of using
757 // the proxy version of addDependency. The waitTask will never need to trigger any
758 // resolves or mip map generation which is the main advantage of going through the proxy
759 // version. Additionally we would've had to temporarily set the wait task as the
760 // lastRenderTask on the proxy, add the dependency, and then reset the lastRenderTask to
761 // lastTask. Additionally we add all dependencies of lastTask to waitTask so that the
762 // waitTask doesn't get reordered before them and unnecessarily block those tasks.
763 // Note: Any previous Ops already in lastTask will get blocked by the wait semaphore
764 // even though they don't need to be for correctness.
765
766 // Make sure we add the dependencies of lastTask to waitTask first or else we'll get a
767 // circular self dependency of waitTask on waitTask.
768 waitTask->addDependenciesFromOtherTask(lastTask);
769 lastTask->addDependency(waitTask.get());
770 } else {
771 // If there is a last task we set the waitTask to depend on it so that it doesn't get
772 // reordered in front of the lastTask causing the lastTask to be blocked by the
773 // semaphore. Again we directly just go through adding the dependency to the task and
774 // not the proxy since we don't need to worry about resolving anything.
775 if (lastTask) {
776 waitTask->addDependency(lastTask);
777 }
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400778 this->setLastRenderTask(proxy.get(), waitTask.get());
Greg Danielc30f1a92019-09-06 15:28:58 -0400779 }
780 fDAG.add(waitTask);
781 } else {
Adlai Holler33d569e2020-06-16 14:30:08 -0400782 if (fActiveOpsTask && (fActiveOpsTask->target(0).proxy() == proxy.get())) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400783 SkASSERT(this->getLastRenderTask(proxy.get()) == fActiveOpsTask);
Greg Danielc30f1a92019-09-06 15:28:58 -0400784 fDAG.addBeforeLast(waitTask);
785 // In this case we keep the current renderTask open but just insert the new waitTask
786 // before it in the list. The waitTask will never need to trigger any resolves or mip
787 // map generation which is the main advantage of going through the proxy version.
788 // Additionally we would've had to temporarily set the wait task as the lastRenderTask
789 // on the proxy, add the dependency, and then reset the lastRenderTask to
790 // fActiveOpsTask. Additionally we make the waitTask depend on all of fActiveOpsTask
791 // dependencies so that we don't unnecessarily reorder the waitTask before them.
792 // Note: Any previous Ops already in fActiveOpsTask will get blocked by the wait
793 // semaphore even though they don't need to be for correctness.
794
795 // Make sure we add the dependencies of fActiveOpsTask to waitTask first or else we'll
796 // get a circular self dependency of waitTask on waitTask.
797 waitTask->addDependenciesFromOtherTask(fActiveOpsTask);
798 fActiveOpsTask->addDependency(waitTask.get());
799 } else {
800 // In this case we just close the previous RenderTask and start and append the waitTask
801 // to the DAG. Since it is the last task now we call setLastRenderTask on the proxy. If
802 // there is a lastTask on the proxy we make waitTask depend on that task. This
803 // dependency isn't strictly needed but it does keep the DAG from reordering the
804 // waitTask earlier and blocking more tasks.
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400805 if (GrRenderTask* lastTask = this->getLastRenderTask(proxy.get())) {
Greg Danielc30f1a92019-09-06 15:28:58 -0400806 waitTask->addDependency(lastTask);
807 }
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400808 this->setLastRenderTask(proxy.get(), waitTask.get());
Greg Danielc30f1a92019-09-06 15:28:58 -0400809 this->closeRenderTasksForNewRenderTask(proxy.get());
810 fDAG.add(waitTask);
811 }
812 }
813 waitTask->makeClosed(caps);
814
815 SkDEBUGCODE(this->validate());
816}
817
Greg Danielbbfec9d2019-08-20 10:56:51 -0400818void GrDrawingManager::newTransferFromRenderTask(sk_sp<GrSurfaceProxy> srcProxy,
819 const SkIRect& srcRect,
820 GrColorType surfaceColorType,
821 GrColorType dstColorType,
822 sk_sp<GrGpuBuffer> dstBuffer,
823 size_t dstOffset) {
824 SkDEBUGCODE(this->validate());
825 SkASSERT(fContext);
826 // This copies from srcProxy to dstBuffer so it doesn't have a real target.
827 this->closeRenderTasksForNewRenderTask(nullptr);
828
Chris Dalton6aeb8e82019-08-27 11:52:19 -0600829 GrRenderTask* task = fDAG.add(sk_make_sp<GrTransferFromRenderTask>(
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400830 srcProxy, srcRect, surfaceColorType, dstColorType,
831 std::move(dstBuffer), dstOffset));
Greg Danielbbfec9d2019-08-20 10:56:51 -0400832
833 const GrCaps& caps = *fContext->priv().caps();
834
Brian Salomon7e67dca2020-07-21 09:27:25 -0400835 // We always say GrMipmapped::kNo here since we are always just copying from the base layer. We
Greg Danielbbfec9d2019-08-20 10:56:51 -0400836 // don't need to make sure the whole mip map chain is valid.
Brian Salomon7e67dca2020-07-21 09:27:25 -0400837 task->addDependency(this, srcProxy.get(), GrMipmapped::kNo,
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400838 GrTextureResolveManager(this), caps);
Greg Danielbbfec9d2019-08-20 10:56:51 -0400839 task->makeClosed(caps);
840
Greg Danielbbfec9d2019-08-20 10:56:51 -0400841 // We have closed the previous active oplist but since a new oplist isn't being added there
842 // shouldn't be an active one.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400843 SkASSERT(!fActiveOpsTask);
Greg Danielbbfec9d2019-08-20 10:56:51 -0400844 SkDEBUGCODE(this->validate());
845}
846
Greg Daniel16f5c652019-10-29 11:26:01 -0400847bool GrDrawingManager::newCopyRenderTask(GrSurfaceProxyView srcView,
Greg Daniele227fe42019-08-21 13:52:24 -0400848 const SkIRect& srcRect,
Greg Daniel16f5c652019-10-29 11:26:01 -0400849 GrSurfaceProxyView dstView,
Greg Daniele227fe42019-08-21 13:52:24 -0400850 const SkIPoint& dstPoint) {
851 SkDEBUGCODE(this->validate());
852 SkASSERT(fContext);
Greg Daniele227fe42019-08-21 13:52:24 -0400853
Greg Daniel16f5c652019-10-29 11:26:01 -0400854 this->closeRenderTasksForNewRenderTask(dstView.proxy());
Brian Salomone4bce012019-09-20 15:34:23 -0400855 const GrCaps& caps = *fContext->priv().caps();
856
Greg Daniel16f5c652019-10-29 11:26:01 -0400857 GrSurfaceProxy* srcProxy = srcView.proxy();
858
Brian Salomone4bce012019-09-20 15:34:23 -0400859 GrRenderTask* task =
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400860 fDAG.add(GrCopyRenderTask::Make(this, std::move(srcView), srcRect, std::move(dstView),
Greg Daniel16f5c652019-10-29 11:26:01 -0400861 dstPoint, &caps));
Greg Daniele227fe42019-08-21 13:52:24 -0400862 if (!task) {
863 return false;
864 }
865
Brian Salomon7e67dca2020-07-21 09:27:25 -0400866 // We always say GrMipmapped::kNo here since we are always just copying from the base layer to
Greg Daniele227fe42019-08-21 13:52:24 -0400867 // another base layer. We don't need to make sure the whole mip map chain is valid.
Brian Salomon7e67dca2020-07-21 09:27:25 -0400868 task->addDependency(this, srcProxy, GrMipmapped::kNo, GrTextureResolveManager(this), caps);
Greg Daniele227fe42019-08-21 13:52:24 -0400869 task->makeClosed(caps);
870
Greg Daniele227fe42019-08-21 13:52:24 -0400871 // We have closed the previous active oplist but since a new oplist isn't being added there
872 // shouldn't be an active one.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400873 SkASSERT(!fActiveOpsTask);
Greg Daniele227fe42019-08-21 13:52:24 -0400874 SkDEBUGCODE(this->validate());
875 return true;
876}
877
robertphillips68737822015-10-29 12:12:21 -0700878/*
879 * This method finds a path renderer that can draw the specified path on
880 * the provided target.
881 * Due to its expense, the software path renderer has split out so it can
882 * can be individually allowed/disallowed via the "allowSW" boolean.
883 */
884GrPathRenderer* GrDrawingManager::getPathRenderer(const GrPathRenderer::CanDrawPathArgs& args,
885 bool allowSW,
886 GrPathRendererChain::DrawType drawType,
887 GrPathRenderer::StencilSupport* stencilSupport) {
888
889 if (!fPathRendererChain) {
John Stilesfbd050b2020-08-03 13:21:46 -0400890 fPathRendererChain =
891 std::make_unique<GrPathRendererChain>(fContext, fOptionsForPathRendererChain);
robertphillips68737822015-10-29 12:12:21 -0700892 }
893
894 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(args, drawType, stencilSupport);
895 if (!pr && allowSW) {
Brian Salomone7df0bb2018-05-07 14:44:57 -0400896 auto swPR = this->getSoftwarePathRenderer();
897 if (GrPathRenderer::CanDrawPath::kNo != swPR->canDrawPath(args)) {
898 pr = swPR;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500899 }
robertphillips68737822015-10-29 12:12:21 -0700900 }
901
Robert Phillipsd81379d2020-04-21 10:39:02 -0400902#if GR_PATH_RENDERER_SPEW
903 if (pr) {
904 SkDebugf("getPathRenderer: %s\n", pr->name());
905 }
906#endif
907
robertphillips68737822015-10-29 12:12:21 -0700908 return pr;
909}
910
Brian Salomone7df0bb2018-05-07 14:44:57 -0400911GrPathRenderer* GrDrawingManager::getSoftwarePathRenderer() {
912 if (!fSoftwarePathRenderer) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400913 fSoftwarePathRenderer.reset(
Robert Phillips9da87e02019-02-04 13:26:26 -0500914 new GrSoftwarePathRenderer(fContext->priv().proxyProvider(),
Ben Wagner9ec70c62018-07-12 13:30:47 -0400915 fOptionsForPathRendererChain.fAllowPathMaskCaching));
Brian Salomone7df0bb2018-05-07 14:44:57 -0400916 }
Ben Wagner9ec70c62018-07-12 13:30:47 -0400917 return fSoftwarePathRenderer.get();
Brian Salomone7df0bb2018-05-07 14:44:57 -0400918}
919
Chris Daltonfddb6c02017-11-04 15:22:22 -0600920GrCoverageCountingPathRenderer* GrDrawingManager::getCoverageCountingPathRenderer() {
921 if (!fPathRendererChain) {
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500922 fPathRendererChain = std::make_unique<GrPathRendererChain>(fContext,
923 fOptionsForPathRendererChain);
Chris Daltonfddb6c02017-11-04 15:22:22 -0600924 }
925 return fPathRendererChain->getCoverageCountingPathRenderer();
926}
927
Brian Salomon653f42f2018-07-10 10:07:31 -0400928void GrDrawingManager::flushIfNecessary() {
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400929 auto direct = fContext->asDirectContext();
Robert Phillips6a6de562019-02-15 15:19:15 -0500930 if (!direct) {
931 return;
932 }
933
934 auto resourceCache = direct->priv().getResourceCache();
Brian Salomon653f42f2018-07-10 10:07:31 -0400935 if (resourceCache && resourceCache->requestsFlush()) {
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500936 if (this->flush({}, SkSurface::BackendSurfaceAccess::kNoAccess, GrFlushInfo(), nullptr)) {
Greg Danielfe159622020-04-10 17:43:51 +0000937 this->submitToGpu(false);
938 }
Brian Salomon57d2beab2018-09-10 09:35:41 -0400939 resourceCache->purgeAsNeeded();
Brian Salomon653f42f2018-07-10 10:07:31 -0400940 }
941}
942