blob: 4c83022fca38db695a6e3e9e7b9d1fc151fbb294 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/gpu/GrBackendSemaphore.h"
11#include "include/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/private/GrRecordingContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/private/SkDeferredDisplayList.h"
14#include "src/core/SkTTopoSort.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040015#include "src/gpu/GrAuditTrail.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/GrContextPriv.h"
17#include "src/gpu/GrGpu.h"
18#include "src/gpu/GrMemoryPool.h"
19#include "src/gpu/GrOnFlushResourceProvider.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040020#include "src/gpu/GrOpList.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/GrRecordingContextPriv.h"
22#include "src/gpu/GrRenderTargetContext.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040023#include "src/gpu/GrRenderTargetProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/GrResourceAllocator.h"
25#include "src/gpu/GrResourceProvider.h"
26#include "src/gpu/GrSoftwarePathRenderer.h"
27#include "src/gpu/GrSurfaceProxyPriv.h"
28#include "src/gpu/GrTextureContext.h"
29#include "src/gpu/GrTextureOpList.h"
30#include "src/gpu/GrTexturePriv.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040031#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050032#include "src/gpu/GrTextureProxyPriv.h"
33#include "src/gpu/GrTracing.h"
34#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
35#include "src/gpu/text/GrTextContext.h"
36#include "src/image/SkSurface_Gpu.h"
robertphillips498d7ac2015-10-30 10:11:30 -070037
Robert Phillips12c46292019-04-23 07:36:17 -040038GrDrawingManager::OpListDAG::OpListDAG(bool sortOpLists) : fSortOpLists(sortOpLists) {}
Robert Phillipsa3f70262018-02-08 10:59:38 -050039
Robert Phillips22310d62018-09-05 11:07:21 -040040GrDrawingManager::OpListDAG::~OpListDAG() {}
41
42void GrDrawingManager::OpListDAG::gatherIDs(SkSTArray<8, uint32_t, true>* idArray) const {
43 idArray->reset(fOpLists.count());
Robert Phillipsf2361d22016-10-25 14:20:06 -040044 for (int i = 0; i < fOpLists.count(); ++i) {
Robert Phillips22310d62018-09-05 11:07:21 -040045 if (fOpLists[i]) {
46 (*idArray)[i] = fOpLists[i]->uniqueID();
47 }
48 }
49}
50
51void GrDrawingManager::OpListDAG::reset() {
52 fOpLists.reset();
53}
54
55void GrDrawingManager::OpListDAG::removeOpList(int index) {
56 if (!fOpLists[index]->unique()) {
57 // TODO: Eventually this should be guaranteed unique: http://skbug.com/7111
58 fOpLists[index]->endFlush();
59 }
60
61 fOpLists[index] = nullptr;
62}
63
64void GrDrawingManager::OpListDAG::removeOpLists(int startIndex, int stopIndex) {
65 for (int i = startIndex; i < stopIndex; ++i) {
66 if (!fOpLists[i]) {
67 continue;
68 }
69 this->removeOpList(i);
70 }
71}
72
Robert Phillips9313aa72019-04-09 18:41:27 -040073bool GrDrawingManager::OpListDAG::isUsed(GrSurfaceProxy* proxy) const {
74 for (int i = 0; i < fOpLists.count(); ++i) {
75 if (fOpLists[i] && fOpLists[i]->isUsed(proxy)) {
76 return true;
77 }
78 }
79
80 return false;
81}
82
Robert Phillips22310d62018-09-05 11:07:21 -040083void GrDrawingManager::OpListDAG::add(sk_sp<GrOpList> opList) {
84 fOpLists.emplace_back(std::move(opList));
85}
86
87void GrDrawingManager::OpListDAG::add(const SkTArray<sk_sp<GrOpList>>& opLists) {
88 fOpLists.push_back_n(opLists.count(), opLists.begin());
89}
90
91void GrDrawingManager::OpListDAG::swap(SkTArray<sk_sp<GrOpList>>* opLists) {
92 SkASSERT(opLists->empty());
93 opLists->swap(fOpLists);
94}
95
96void GrDrawingManager::OpListDAG::prepForFlush() {
97 if (fSortOpLists) {
98 SkDEBUGCODE(bool result =) SkTTopoSort<GrOpList, GrOpList::TopoSortTraits>(&fOpLists);
99 SkASSERT(result);
100 }
101
102#ifdef SK_DEBUG
103 // This block checks for any unnecessary splits in the opLists. If two sequential opLists
104 // share the same backing GrSurfaceProxy it means the opList was artificially split.
105 if (fOpLists.count()) {
106 GrRenderTargetOpList* prevOpList = fOpLists[0]->asRenderTargetOpList();
107 for (int i = 1; i < fOpLists.count(); ++i) {
108 GrRenderTargetOpList* curOpList = fOpLists[i]->asRenderTargetOpList();
109
110 if (prevOpList && curOpList) {
111 SkASSERT(prevOpList->fTarget.get() != curOpList->fTarget.get());
112 }
113
114 prevOpList = curOpList;
115 }
116 }
117#endif
118}
119
120void GrDrawingManager::OpListDAG::closeAll(const GrCaps* caps) {
121 for (int i = 0; i < fOpLists.count(); ++i) {
122 if (fOpLists[i]) {
123 fOpLists[i]->makeClosed(*caps);
124 }
125 }
126}
127
128void GrDrawingManager::OpListDAG::cleanup(const GrCaps* caps) {
129 for (int i = 0; i < fOpLists.count(); ++i) {
130 if (!fOpLists[i]) {
131 continue;
132 }
133
Robert Phillipsee683652017-04-26 11:53:10 -0400134 // no opList should receive a new command after this
Robert Phillips22310d62018-09-05 11:07:21 -0400135 fOpLists[i]->makeClosed(*caps);
robertphillips0dfa62c2015-11-16 06:23:31 -0800136
Robert Phillipsf2361d22016-10-25 14:20:06 -0400137 // We shouldn't need to do this, but it turns out some clients still hold onto opLists
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400138 // after a cleanup.
139 // MDB TODO: is this still true?
Chris Daltona84cacf2017-10-04 10:30:29 -0600140 if (!fOpLists[i]->unique()) {
141 // TODO: Eventually this should be guaranteed unique.
142 // https://bugs.chromium.org/p/skia/issues/detail?id=7111
143 fOpLists[i]->endFlush();
144 }
robertphillips3dc6ae52015-10-20 09:54:32 -0700145 }
146
Robert Phillipsf2361d22016-10-25 14:20:06 -0400147 fOpLists.reset();
Robert Phillips22310d62018-09-05 11:07:21 -0400148}
149
150///////////////////////////////////////////////////////////////////////////////////////////////////
Robert Phillips69893702019-02-22 11:16:30 -0500151GrDrawingManager::GrDrawingManager(GrRecordingContext* context,
Robert Phillips22310d62018-09-05 11:07:21 -0400152 const GrPathRendererChain::Options& optionsForPathRendererChain,
153 const GrTextContext::Options& optionsForTextContext,
Robert Phillips56181ba2019-03-08 12:00:45 -0500154 bool sortOpLists,
Robert Phillips6db27c22019-05-01 10:43:56 -0400155 bool reduceOpListSplitting)
Robert Phillips22310d62018-09-05 11:07:21 -0400156 : fContext(context)
157 , fOptionsForPathRendererChain(optionsForPathRendererChain)
158 , fOptionsForTextContext(optionsForTextContext)
Robert Phillips12c46292019-04-23 07:36:17 -0400159 , fDAG(sortOpLists)
Robert Phillips22310d62018-09-05 11:07:21 -0400160 , fTextContext(nullptr)
161 , fPathRendererChain(nullptr)
162 , fSoftwarePathRenderer(nullptr)
Robert Phillips6db27c22019-05-01 10:43:56 -0400163 , fFlushing(false)
164 , fReduceOpListSplitting(reduceOpListSplitting) {
Robert Phillips22310d62018-09-05 11:07:21 -0400165}
166
167void GrDrawingManager::cleanup() {
Robert Phillips9da87e02019-02-04 13:26:26 -0500168 fDAG.cleanup(fContext->priv().caps());
robertphillips3dc6ae52015-10-20 09:54:32 -0700169
robertphillips13391dd2015-10-30 05:15:11 -0700170 fPathRendererChain = nullptr;
Ben Wagner9ec70c62018-07-12 13:30:47 -0400171 fSoftwarePathRenderer = nullptr;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400172
173 fOnFlushCBObjects.reset();
robertphillips3dc6ae52015-10-20 09:54:32 -0700174}
175
176GrDrawingManager::~GrDrawingManager() {
177 this->cleanup();
178}
179
Robert Phillipsa9162df2019-02-11 14:12:03 -0500180bool GrDrawingManager::wasAbandoned() const {
Robert Phillips6a6de562019-02-15 15:19:15 -0500181 return fContext->priv().abandoned();
robertphillips3dc6ae52015-10-20 09:54:32 -0700182}
183
robertphillips68737822015-10-29 12:12:21 -0700184void GrDrawingManager::freeGpuResources() {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400185 for (int i = fOnFlushCBObjects.count() - 1; i >= 0; --i) {
186 if (!fOnFlushCBObjects[i]->retainOnFreeGpuResources()) {
187 // it's safe to just do this because we're iterating in reverse
188 fOnFlushCBObjects.removeShuffle(i);
189 }
190 }
191
robertphillips68737822015-10-29 12:12:21 -0700192 // a path renderer may be holding onto resources
robertphillips13391dd2015-10-30 05:15:11 -0700193 fPathRendererChain = nullptr;
Ben Wagner9ec70c62018-07-12 13:30:47 -0400194 fSoftwarePathRenderer = nullptr;
Robert Phillipse3302df2017-04-24 07:31:02 -0400195}
196
Robert Phillips7ee385e2017-03-30 08:02:11 -0400197// MDB TODO: make use of the 'proxy' parameter.
Greg Daniel797efca2019-05-09 14:04:20 -0400198GrSemaphoresSubmitted GrDrawingManager::flush(GrSurfaceProxy* proxies[], int numProxies,
199 SkSurface::BackendSurfaceAccess access, const GrFlushInfo& info,
200 const GrPrepareForExternalIORequests& externalRequests) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400201 SkASSERT(numProxies >= 0);
202 SkASSERT(!numProxies || proxies);
Brian Salomon57d2beab2018-09-10 09:35:41 -0400203 GR_CREATE_TRACE_MARKER_CONTEXT("GrDrawingManager", "flush", fContext);
Brian Salomondcbb9d92017-07-19 10:53:20 -0400204
robertphillips7761d612016-05-16 09:14:53 -0700205 if (fFlushing || this->wasAbandoned()) {
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400206 if (info.fFinishedProc) {
207 info.fFinishedProc(info.fFinishedContext);
Greg Daniela3aa75a2019-04-12 14:24:55 -0400208 }
Greg Daniel51316782017-08-02 15:10:09 +0000209 return GrSemaphoresSubmitted::kNo;
joshualittb8918c42015-12-18 09:59:46 -0800210 }
Robert Phillips602df412019-04-08 11:10:39 -0400211
Robert Phillips38d64b02018-09-04 13:23:26 -0400212 SkDEBUGCODE(this->validate());
213
Greg Daniel797efca2019-05-09 14:04:20 -0400214 if (kNone_GrFlushFlags == info.fFlags && !info.fNumSemaphores && !info.fFinishedProc &&
215 !externalRequests.hasRequests()) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400216 bool canSkip = numProxies > 0;
217 for (int i = 0; i < numProxies && canSkip; ++i) {
218 canSkip = !fDAG.isUsed(proxies[i]) && !this->isDDLTarget(proxies[i]);
219 }
220 if (canSkip) {
221 return GrSemaphoresSubmitted::kNo;
222 }
Robert Phillips9313aa72019-04-09 18:41:27 -0400223 }
224
Robert Phillips6a6de562019-02-15 15:19:15 -0500225 auto direct = fContext->priv().asDirectContext();
226 if (!direct) {
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400227 if (info.fFinishedProc) {
228 info.fFinishedProc(info.fFinishedContext);
Greg Daniela3aa75a2019-04-12 14:24:55 -0400229 }
Robert Phillips6a6de562019-02-15 15:19:15 -0500230 return GrSemaphoresSubmitted::kNo; // Can't flush while DDL recording
231 }
232
233 GrGpu* gpu = direct->priv().getGpu();
Robert Phillips874b5352018-03-16 08:48:24 -0400234 if (!gpu) {
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400235 if (info.fFinishedProc) {
236 info.fFinishedProc(info.fFinishedContext);
Greg Daniela3aa75a2019-04-12 14:24:55 -0400237 }
Robert Phillips874b5352018-03-16 08:48:24 -0400238 return GrSemaphoresSubmitted::kNo; // Can't flush while DDL recording
239 }
Greg Daniela3aa75a2019-04-12 14:24:55 -0400240
joshualittb8918c42015-12-18 09:59:46 -0800241 fFlushing = true;
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400242
Robert Phillips6a6de562019-02-15 15:19:15 -0500243 auto resourceProvider = direct->priv().resourceProvider();
244 auto resourceCache = direct->priv().getResourceCache();
245
Robert Phillips38d64b02018-09-04 13:23:26 -0400246 // Semi-usually the GrOpLists are already closed at this point, but sometimes Ganesh
247 // needs to flush mid-draw. In that case, the SkGpuDevice's GrOpLists won't be closed
248 // but need to be flushed anyway. Closing such GrOpLists here will mean new
249 // GrOpLists will be created to replace them if the SkGpuDevice(s) write to them again.
Robert Phillips9da87e02019-02-04 13:26:26 -0500250 fDAG.closeAll(fContext->priv().caps());
Robert Phillips38d64b02018-09-04 13:23:26 -0400251 fActiveOpList = nullptr;
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400252
Robert Phillips22310d62018-09-05 11:07:21 -0400253 fDAG.prepForFlush();
Brian Salomon601ac802019-02-07 13:37:16 -0500254 if (!fCpuBufferCache) {
255 // We cache more buffers when the backend is using client side arrays. Otherwise, we
256 // expect each pool will use a CPU buffer as a staging buffer before uploading to a GPU
257 // buffer object. Each pool only requires one staging buffer at a time.
258 int maxCachedBuffers = fContext->priv().caps()->preferClientSideDynamicBuffers() ? 2 : 6;
259 fCpuBufferCache = GrBufferAllocPool::CpuBufferCache::Make(maxCachedBuffers);
Brian Salomon58f153c2018-10-18 21:51:15 -0400260 }
Robert Phillipsa4c93ac2017-05-18 11:40:04 -0400261
Robert Phillipse5f73282019-06-18 17:15:04 -0400262 GrOpFlushState flushState(gpu, resourceProvider, &fTokenTracker, fCpuBufferCache);
Robert Phillips40a29d72018-01-18 12:59:22 -0500263
Chris Daltonfe199b72017-05-05 11:26:15 -0400264 GrOnFlushResourceProvider onFlushProvider(this);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500265 // TODO: AFAICT the only reason fFlushState is on GrDrawingManager rather than on the
266 // stack here is to preserve the flush tokens.
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400267
Chris Dalton12658942017-10-05 19:45:25 -0600268 // Prepare any onFlush op lists (e.g. atlases).
Chris Daltonfe199b72017-05-05 11:26:15 -0400269 if (!fOnFlushCBObjects.empty()) {
Robert Phillips22310d62018-09-05 11:07:21 -0400270 fDAG.gatherIDs(&fFlushingOpListIDs);
271
Chris Dalton12658942017-10-05 19:45:25 -0600272 SkSTArray<4, sk_sp<GrRenderTargetContext>> renderTargetContexts;
Chris Daltonfe199b72017-05-05 11:26:15 -0400273 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
274 onFlushCBObject->preFlush(&onFlushProvider,
Chris Dalton3968ff92017-11-27 12:26:31 -0700275 fFlushingOpListIDs.begin(), fFlushingOpListIDs.count(),
Chris Daltonfe199b72017-05-05 11:26:15 -0400276 &renderTargetContexts);
Chris Dalton12658942017-10-05 19:45:25 -0600277 for (const sk_sp<GrRenderTargetContext>& rtc : renderTargetContexts) {
Chris Dalton706a6ff2017-11-29 22:01:06 -0700278 sk_sp<GrRenderTargetOpList> onFlushOpList = sk_ref_sp(rtc->getRTOpList());
Chris Dalton12658942017-10-05 19:45:25 -0600279 if (!onFlushOpList) {
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400280 continue; // Odd - but not a big deal
281 }
Chris Dalton706a6ff2017-11-29 22:01:06 -0700282#ifdef SK_DEBUG
283 // OnFlush callbacks are already invoked during flush, and are therefore expected to
284 // handle resource allocation & usage on their own. (No deferred or lazy proxies!)
Chris Dalton7eb5c0f2019-05-23 15:15:47 -0600285 onFlushOpList->visitProxies_debugOnly([](GrSurfaceProxy* p, GrMipMapped) {
Chris Dalton706a6ff2017-11-29 22:01:06 -0700286 SkASSERT(!p->asTextureProxy() || !p->asTextureProxy()->texPriv().isDeferred());
Greg Daniel65fa8ca2018-01-10 17:06:31 -0500287 SkASSERT(GrSurfaceProxy::LazyState::kNot == p->lazyInstantiationState());
Chris Dalton706a6ff2017-11-29 22:01:06 -0700288 });
289#endif
Robert Phillips9da87e02019-02-04 13:26:26 -0500290 onFlushOpList->makeClosed(*fContext->priv().caps());
Robert Phillips40a29d72018-01-18 12:59:22 -0500291 onFlushOpList->prepare(&flushState);
Chris Dalton3968ff92017-11-27 12:26:31 -0700292 fOnFlushCBOpLists.push_back(std::move(onFlushOpList));
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400293 }
294 renderTargetContexts.reset();
295 }
296 }
297
robertphillipsa13e2022015-11-11 12:01:09 -0800298#if 0
Brian Salomon09d994e2016-12-21 11:14:46 -0500299 // Enable this to print out verbose GrOp information
Robert Phillipsf2361d22016-10-25 14:20:06 -0400300 for (int i = 0; i < fOpLists.count(); ++i) {
301 SkDEBUGCODE(fOpLists[i]->dump();)
robertphillips3dc6ae52015-10-20 09:54:32 -0700302 }
robertphillipsa13e2022015-11-11 12:01:09 -0800303#endif
304
Robert Phillipseafd48a2017-11-16 07:52:08 -0500305 int startIndex, stopIndex;
306 bool flushed = false;
307
Robert Phillipsf8e25022017-11-08 15:24:31 -0500308 {
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400309 GrResourceAllocator alloc(resourceProvider, flushState.deinstantiateProxyTracker()
310 SkDEBUGCODE(, fDAG.numOpLists()));
Robert Phillips22310d62018-09-05 11:07:21 -0400311 for (int i = 0; i < fDAG.numOpLists(); ++i) {
312 if (fDAG.opList(i)) {
313 fDAG.opList(i)->gatherProxyIntervals(&alloc);
314 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500315 alloc.markEndOfOpList(i);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500316 }
Robert Phillipsc73666f2019-04-24 08:49:48 -0400317 alloc.determineRecyclability();
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400318
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500319 GrResourceAllocator::AssignError error = GrResourceAllocator::AssignError::kNoError;
Greg Danield2073452018-12-07 11:20:33 -0500320 int numOpListsExecuted = 0;
Brian Salomon577aa0f2018-11-30 13:32:23 -0500321 while (alloc.assign(&startIndex, &stopIndex, &error)) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500322 if (GrResourceAllocator::AssignError::kFailedProxyInstantiation == error) {
323 for (int i = startIndex; i < stopIndex; ++i) {
Robert Phillips82774f82019-06-20 14:38:27 -0400324 if (fDAG.opList(i) && !fDAG.opList(i)->isInstantiated()) {
325 // If the backing surface wasn't allocated, drop the entire opList.
Robert Phillips22310d62018-09-05 11:07:21 -0400326 fDAG.removeOpList(i);
Robert Phillips01a91282018-07-26 08:03:04 -0400327 }
Robert Phillips22310d62018-09-05 11:07:21 -0400328 if (fDAG.opList(i)) {
329 fDAG.opList(i)->purgeOpsWithUninstantiatedProxies();
Robert Phillipsbbcb7f72018-05-31 11:16:19 -0400330 }
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500331 }
332 }
Robert Phillips4150eea2018-02-07 17:08:21 -0500333
Greg Danield2073452018-12-07 11:20:33 -0500334 if (this->executeOpLists(startIndex, stopIndex, &flushState, &numOpListsExecuted)) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500335 flushed = true;
336 }
bsalomondc438982016-08-31 11:53:49 -0700337 }
Greg Danielc42b20b2017-10-04 10:34:49 -0400338 }
339
Chris Dalton91ab1552018-04-18 13:24:25 -0600340#ifdef SK_DEBUG
Robert Phillips22310d62018-09-05 11:07:21 -0400341 for (int i = 0; i < fDAG.numOpLists(); ++i) {
Chris Dalton91ab1552018-04-18 13:24:25 -0600342 // If there are any remaining opLists at this point, make sure they will not survive the
343 // flush. Otherwise we need to call endFlush() on them.
344 // http://skbug.com/7111
Robert Phillips22310d62018-09-05 11:07:21 -0400345 SkASSERT(!fDAG.opList(i) || fDAG.opList(i)->unique());
Chris Dalton91ab1552018-04-18 13:24:25 -0600346 }
347#endif
Robert Phillips22310d62018-09-05 11:07:21 -0400348 fDAG.reset();
Robert Phillips15c91422019-05-07 16:54:48 -0400349 this->clearDDLTargets();
robertphillipsa13e2022015-11-11 12:01:09 -0800350
Robert Phillipsc994a932018-06-19 13:09:54 -0400351#ifdef SK_DEBUG
352 // In non-DDL mode this checks that all the flushed ops have been freed from the memory pool.
353 // When we move to partial flushes this assert will no longer be valid.
354 // In DDL mode this check is somewhat superfluous since the memory for most of the ops/opLists
355 // will be stored in the DDL's GrOpMemoryPools.
Robert Phillips9da87e02019-02-04 13:26:26 -0500356 GrOpMemoryPool* opMemoryPool = fContext->priv().opMemoryPool();
Robert Phillipsc994a932018-06-19 13:09:54 -0400357 opMemoryPool->isEmpty();
358#endif
359
Greg Daniel797efca2019-05-09 14:04:20 -0400360 GrSemaphoresSubmitted result = gpu->finishFlush(proxies, numProxies, access, info,
361 externalRequests);
robertphillipsa13e2022015-11-11 12:01:09 -0800362
Brian Salomon876a0172019-03-08 11:12:14 -0500363 flushState.deinstantiateProxyTracker()->deinstantiateAllProxies();
364
Brian Salomon57d2beab2018-09-10 09:35:41 -0400365 // Give the cache a chance to purge resources that become purgeable due to flushing.
366 if (flushed) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500367 resourceCache->purgeAsNeeded();
Brian Salomon876a0172019-03-08 11:12:14 -0500368 flushed = false;
bsalomonb77a9072016-09-07 10:02:04 -0700369 }
Chris Daltonfe199b72017-05-05 11:26:15 -0400370 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
Robert Phillips40a29d72018-01-18 12:59:22 -0500371 onFlushCBObject->postFlush(fTokenTracker.nextTokenToFlush(), fFlushingOpListIDs.begin(),
Chris Dalton3968ff92017-11-27 12:26:31 -0700372 fFlushingOpListIDs.count());
Brian Salomon876a0172019-03-08 11:12:14 -0500373 flushed = true;
374 }
375 if (flushed) {
376 resourceCache->purgeAsNeeded();
Chris Daltonfe199b72017-05-05 11:26:15 -0400377 }
Chris Dalton3968ff92017-11-27 12:26:31 -0700378 fFlushingOpListIDs.reset();
joshualittb8918c42015-12-18 09:59:46 -0800379 fFlushing = false;
Greg Daniel51316782017-08-02 15:10:09 +0000380
381 return result;
robertphillips3dc6ae52015-10-20 09:54:32 -0700382}
383
Greg Danield2073452018-12-07 11:20:33 -0500384bool GrDrawingManager::executeOpLists(int startIndex, int stopIndex, GrOpFlushState* flushState,
385 int* numOpListsExecuted) {
Robert Phillips22310d62018-09-05 11:07:21 -0400386 SkASSERT(startIndex <= stopIndex && stopIndex <= fDAG.numOpLists());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500387
Robert Phillips27483912018-04-20 12:43:18 -0400388#if GR_FLUSH_TIME_OP_SPEW
389 SkDebugf("Flushing opLists: %d to %d out of [%d, %d]\n",
Robert Phillips22310d62018-09-05 11:07:21 -0400390 startIndex, stopIndex, 0, fDAG.numOpLists());
Robert Phillips27483912018-04-20 12:43:18 -0400391 for (int i = startIndex; i < stopIndex; ++i) {
Robert Phillips22310d62018-09-05 11:07:21 -0400392 if (fDAG.opList(i)) {
393 fDAG.opList(i)->dump(true);
Robert Phillips1734dd32018-08-21 13:52:09 -0400394 }
Robert Phillips27483912018-04-20 12:43:18 -0400395 }
396#endif
397
Robert Phillipseafd48a2017-11-16 07:52:08 -0500398 bool anyOpListsExecuted = false;
399
400 for (int i = startIndex; i < stopIndex; ++i) {
Robert Phillips22310d62018-09-05 11:07:21 -0400401 if (!fDAG.opList(i)) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500402 continue;
403 }
404
Robert Phillips22310d62018-09-05 11:07:21 -0400405 GrOpList* opList = fDAG.opList(i);
Robert Phillips82774f82019-06-20 14:38:27 -0400406 SkASSERT(opList->isInstantiated());
407 SkASSERT(opList->deferredProxiesAreInstantiated());
Robert Phillips22310d62018-09-05 11:07:21 -0400408
Robert Phillips22310d62018-09-05 11:07:21 -0400409 opList->prepare(flushState);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500410 }
411
412 // Upload all data to the GPU
413 flushState->preExecuteDraws();
414
Greg Danield2073452018-12-07 11:20:33 -0500415 // For Vulkan, if we have too many oplists to be flushed we end up allocating a lot of resources
416 // for each command buffer associated with the oplists. If this gets too large we can cause the
417 // devices to go OOM. In practice we usually only hit this case in our tests, but to be safe we
418 // put a cap on the number of oplists we will execute before flushing to the GPU to relieve some
419 // memory pressure.
420 static constexpr int kMaxOpListsBeforeFlush = 100;
421
Robert Phillipseafd48a2017-11-16 07:52:08 -0500422 // Execute the onFlush op lists first, if any.
Chris Dalton3968ff92017-11-27 12:26:31 -0700423 for (sk_sp<GrOpList>& onFlushOpList : fOnFlushCBOpLists) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500424 if (!onFlushOpList->execute(flushState)) {
425 SkDebugf("WARNING: onFlushOpList failed to execute.\n");
426 }
427 SkASSERT(onFlushOpList->unique());
428 onFlushOpList = nullptr;
Greg Danield2073452018-12-07 11:20:33 -0500429 (*numOpListsExecuted)++;
430 if (*numOpListsExecuted >= kMaxOpListsBeforeFlush) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400431 flushState->gpu()->finishFlush(nullptr, 0, SkSurface::BackendSurfaceAccess::kNoAccess,
Greg Daniel797efca2019-05-09 14:04:20 -0400432 GrFlushInfo(), GrPrepareForExternalIORequests());
Greg Danield2073452018-12-07 11:20:33 -0500433 *numOpListsExecuted = 0;
434 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500435 }
Chris Dalton3968ff92017-11-27 12:26:31 -0700436 fOnFlushCBOpLists.reset();
Robert Phillipseafd48a2017-11-16 07:52:08 -0500437
438 // Execute the normal op lists.
439 for (int i = startIndex; i < stopIndex; ++i) {
Robert Phillips22310d62018-09-05 11:07:21 -0400440 if (!fDAG.opList(i)) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500441 continue;
442 }
443
Robert Phillips22310d62018-09-05 11:07:21 -0400444 if (fDAG.opList(i)->execute(flushState)) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500445 anyOpListsExecuted = true;
446 }
Greg Danield2073452018-12-07 11:20:33 -0500447 (*numOpListsExecuted)++;
448 if (*numOpListsExecuted >= kMaxOpListsBeforeFlush) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400449 flushState->gpu()->finishFlush(nullptr, 0, SkSurface::BackendSurfaceAccess::kNoAccess,
Greg Daniel797efca2019-05-09 14:04:20 -0400450 GrFlushInfo(), GrPrepareForExternalIORequests());
Greg Danield2073452018-12-07 11:20:33 -0500451 *numOpListsExecuted = 0;
452 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500453 }
454
455 SkASSERT(!flushState->commandBuffer());
Robert Phillips40a29d72018-01-18 12:59:22 -0500456 SkASSERT(fTokenTracker.nextDrawToken() == fTokenTracker.nextTokenToFlush());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500457
458 // We reset the flush state before the OpLists so that the last resources to be freed are those
459 // that are written to in the OpLists. This helps to make sure the most recently used resources
460 // are the last to be purged by the resource cache.
461 flushState->reset();
462
Robert Phillips22310d62018-09-05 11:07:21 -0400463 fDAG.removeOpLists(startIndex, stopIndex);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500464
465 return anyOpListsExecuted;
466}
467
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400468GrSemaphoresSubmitted GrDrawingManager::flushSurfaces(GrSurfaceProxy* proxies[], int numProxies,
469 SkSurface::BackendSurfaceAccess access,
470 const GrFlushInfo& info) {
bsalomon6a2b1942016-09-08 11:28:59 -0700471 if (this->wasAbandoned()) {
Greg Daniel51316782017-08-02 15:10:09 +0000472 return GrSemaphoresSubmitted::kNo;
bsalomon6a2b1942016-09-08 11:28:59 -0700473 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400474 SkDEBUGCODE(this->validate());
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400475 SkASSERT(numProxies >= 0);
476 SkASSERT(!numProxies || proxies);
bsalomon6a2b1942016-09-08 11:28:59 -0700477
Robert Phillips6a6de562019-02-15 15:19:15 -0500478 auto direct = fContext->priv().asDirectContext();
479 if (!direct) {
480 return GrSemaphoresSubmitted::kNo; // Can't flush while DDL recording
481 }
482
483 GrGpu* gpu = direct->priv().getGpu();
Robert Phillips874b5352018-03-16 08:48:24 -0400484 if (!gpu) {
485 return GrSemaphoresSubmitted::kNo; // Can't flush while DDL recording
486 }
487
Robert Phillipsacc10fa2019-04-01 09:50:20 -0400488 // TODO: It is important to upgrade the drawingmanager to just flushing the
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400489 // portion of the DAG required by 'proxies' in order to restore some of the
Robert Phillipsacc10fa2019-04-01 09:50:20 -0400490 // semantics of this method.
Greg Daniel797efca2019-05-09 14:04:20 -0400491 GrSemaphoresSubmitted result = this->flush(proxies, numProxies, access, info,
492 GrPrepareForExternalIORequests());
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400493 for (int i = 0; i < numProxies; ++i) {
494 if (!proxies[i]->isInstantiated()) {
495 return result;
496 }
Robert Phillips7ee385e2017-03-30 08:02:11 -0400497 }
498
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400499 for (int i = 0; i < numProxies; ++i) {
500 GrSurface* surface = proxies[i]->peekSurface();
501 if (auto* rt = surface->asRenderTarget()) {
502 gpu->resolveRenderTarget(rt);
503 }
504 if (auto* tex = surface->asTexture()) {
505 if (tex->texturePriv().mipMapped() == GrMipMapped::kYes &&
506 tex->texturePriv().mipMapsAreDirty()) {
507 gpu->regenerateMipMapLevels(tex);
508 }
Brian Salomon930f9392018-06-20 16:25:26 -0400509 }
bsalomon6a2b1942016-09-08 11:28:59 -0700510 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400511
512 SkDEBUGCODE(this->validate());
Greg Daniel51316782017-08-02 15:10:09 +0000513 return result;
bsalomon6a2b1942016-09-08 11:28:59 -0700514}
515
Chris Daltonfe199b72017-05-05 11:26:15 -0400516void GrDrawingManager::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
517 fOnFlushCBObjects.push_back(onFlushCBObject);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400518}
519
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500520#if GR_TEST_UTILS
521void GrDrawingManager::testingOnly_removeOnFlushCallbackObject(GrOnFlushCallbackObject* cb) {
522 int n = std::find(fOnFlushCBObjects.begin(), fOnFlushCBObjects.end(), cb) -
523 fOnFlushCBObjects.begin();
524 SkASSERT(n < fOnFlushCBObjects.count());
525 fOnFlushCBObjects.removeShuffle(n);
526}
527#endif
528
Robert Phillips62000362018-02-01 09:10:04 -0500529void GrDrawingManager::moveOpListsToDDL(SkDeferredDisplayList* ddl) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400530 SkDEBUGCODE(this->validate());
531
532 // no opList should receive a new command after this
Robert Phillips9da87e02019-02-04 13:26:26 -0500533 fDAG.closeAll(fContext->priv().caps());
Robert Phillips38d64b02018-09-04 13:23:26 -0400534 fActiveOpList = nullptr;
Robert Phillips7a137052018-02-01 11:23:12 -0500535
Robert Phillips22310d62018-09-05 11:07:21 -0400536 fDAG.swap(&ddl->fOpLists);
Robert Phillips867ce8f2018-06-21 10:28:36 -0400537
Robert Phillips774168e2018-05-31 12:43:27 -0400538 if (fPathRendererChain) {
539 if (auto ccpr = fPathRendererChain->getCoverageCountingPathRenderer()) {
540 ddl->fPendingPaths = ccpr->detachPendingPaths();
541 }
542 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400543
544 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500545}
546
547void GrDrawingManager::copyOpListsFromDDL(const SkDeferredDisplayList* ddl,
548 GrRenderTargetProxy* newDest) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400549 SkDEBUGCODE(this->validate());
550
551 if (fActiveOpList) {
552 // This is a temporary fix for the partial-MDB world. In that world we're not
553 // reordering so ops that (in the single opList world) would've just glommed onto the
554 // end of the single opList but referred to a far earlier RT need to appear in their
555 // own opList.
Robert Phillips9da87e02019-02-04 13:26:26 -0500556 fActiveOpList->makeClosed(*fContext->priv().caps());
Robert Phillips38d64b02018-09-04 13:23:26 -0400557 fActiveOpList = nullptr;
558 }
559
Robert Phillips15c91422019-05-07 16:54:48 -0400560 this->addDDLTarget(newDest);
561
Robert Phillips62000362018-02-01 09:10:04 -0500562 // Here we jam the proxy that backs the current replay SkSurface into the LazyProxyData.
563 // The lazy proxy that references it (in the copied opLists) will steal its GrTexture.
564 ddl->fLazyProxyData->fReplayDest = newDest;
Robert Phillips774168e2018-05-31 12:43:27 -0400565
566 if (ddl->fPendingPaths.size()) {
567 GrCoverageCountingPathRenderer* ccpr = this->getCoverageCountingPathRenderer();
568
569 ccpr->mergePendingPaths(ddl->fPendingPaths);
570 }
Robert Phillips22310d62018-09-05 11:07:21 -0400571
572 fDAG.add(ddl->fOpLists);
Robert Phillips38d64b02018-09-04 13:23:26 -0400573
574 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500575}
576
Robert Phillips38d64b02018-09-04 13:23:26 -0400577#ifdef SK_DEBUG
578void GrDrawingManager::validate() const {
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400579 if (fDAG.sortingOpLists() && fReduceOpListSplitting) {
580 SkASSERT(!fActiveOpList);
581 } else {
582 if (fActiveOpList) {
583 SkASSERT(!fDAG.empty());
584 SkASSERT(!fActiveOpList->isClosed());
585 SkASSERT(fActiveOpList == fDAG.back());
Robert Phillips38d64b02018-09-04 13:23:26 -0400586 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400587
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400588 for (int i = 0; i < fDAG.numOpLists(); ++i) {
589 if (fActiveOpList != fDAG.opList(i)) {
590 SkASSERT(fDAG.opList(i)->isClosed());
591 }
592 }
593
594 if (!fDAG.empty() && !fDAG.back()->isClosed()) {
595 SkASSERT(fActiveOpList == fDAG.back());
596 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400597 }
598}
599#endif
600
Robert Phillips831a2932019-04-12 17:18:39 -0400601sk_sp<GrRenderTargetOpList> GrDrawingManager::newRTOpList(sk_sp<GrRenderTargetProxy> rtp,
Robert Phillips941d1442017-06-14 16:37:02 -0400602 bool managedOpList) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400603 SkDEBUGCODE(this->validate());
robertphillips3dc6ae52015-10-20 09:54:32 -0700604 SkASSERT(fContext);
605
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400606 if (fDAG.sortingOpLists() && fReduceOpListSplitting) {
607 // In this case we need to close all the opLists that rely on the current contents of
608 // 'rtp'. That is bc we're going to update the content of the proxy so they need to be
609 // split in case they use both the old and new content. (This is a bit of an overkill:
610 // they really only need to be split if they ever reference proxy's contents again but
611 // that is hard to predict/handle).
612 if (GrOpList* lastOpList = rtp->getLastOpList()) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500613 lastOpList->closeThoseWhoDependOnMe(*fContext->priv().caps());
Robert Phillips46acf9d2018-10-09 09:31:40 -0400614 }
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400615 } else if (fActiveOpList) {
616 // This is a temporary fix for the partial-MDB world. In that world we're not
617 // reordering so ops that (in the single opList world) would've just glommed onto the
618 // end of the single opList but referred to a far earlier RT need to appear in their
619 // own opList.
Robert Phillips9da87e02019-02-04 13:26:26 -0500620 fActiveOpList->makeClosed(*fContext->priv().caps());
Robert Phillips38d64b02018-09-04 13:23:26 -0400621 fActiveOpList = nullptr;
robertphillips3dc6ae52015-10-20 09:54:32 -0700622 }
robertphillips3dc6ae52015-10-20 09:54:32 -0700623
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500624 sk_sp<GrRenderTargetOpList> opList(new GrRenderTargetOpList(
Robert Phillips9da87e02019-02-04 13:26:26 -0500625 fContext->priv().refOpMemoryPool(),
Robert Phillips3a9710b2018-03-27 17:51:55 -0400626 rtp,
Robert Phillipsd6841482019-02-08 10:29:20 -0500627 fContext->priv().auditTrail()));
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400628 SkASSERT(rtp->getLastOpList() == opList.get());
robertphillips3dc6ae52015-10-20 09:54:32 -0700629
Robert Phillips941d1442017-06-14 16:37:02 -0400630 if (managedOpList) {
Robert Phillips22310d62018-09-05 11:07:21 -0400631 fDAG.add(opList);
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400632
633 if (!fDAG.sortingOpLists() || !fReduceOpListSplitting) {
634 fActiveOpList = opList.get();
635 }
Robert Phillips941d1442017-06-14 16:37:02 -0400636 }
robertphillips3dc6ae52015-10-20 09:54:32 -0700637
Robert Phillips38d64b02018-09-04 13:23:26 -0400638 SkDEBUGCODE(this->validate());
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400639 return opList;
robertphillips3dc6ae52015-10-20 09:54:32 -0700640}
641
Robert Phillips831a2932019-04-12 17:18:39 -0400642sk_sp<GrTextureOpList> GrDrawingManager::newTextureOpList(sk_sp<GrTextureProxy> textureProxy) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400643 SkDEBUGCODE(this->validate());
Brian Osman45580d32016-11-23 09:37:01 -0500644 SkASSERT(fContext);
645
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400646 if (fDAG.sortingOpLists() && fReduceOpListSplitting) {
647 // In this case we need to close all the opLists that rely on the current contents of
648 // 'texture'. That is bc we're going to update the content of the proxy so they need to
649 // be split in case they use both the old and new content. (This is a bit of an
650 // overkill: they really only need to be split if they ever reference proxy's contents
651 // again but that is hard to predict/handle).
652 if (GrOpList* lastOpList = textureProxy->getLastOpList()) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500653 lastOpList->closeThoseWhoDependOnMe(*fContext->priv().caps());
Robert Phillips46acf9d2018-10-09 09:31:40 -0400654 }
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400655 } else if (fActiveOpList) {
656 // This is a temporary fix for the partial-MDB world. In that world we're not
657 // reordering so ops that (in the single opList world) would've just glommed onto the
658 // end of the single opList but referred to a far earlier RT need to appear in their
659 // own opList.
Robert Phillips9da87e02019-02-04 13:26:26 -0500660 fActiveOpList->makeClosed(*fContext->priv().caps());
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400661 fActiveOpList = nullptr;
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400662 }
663
Robert Phillips12c46292019-04-23 07:36:17 -0400664 sk_sp<GrTextureOpList> opList(new GrTextureOpList(fContext->priv().refOpMemoryPool(),
Robert Phillips5efd5ea2017-05-30 13:47:32 -0400665 textureProxy,
Robert Phillipsd6841482019-02-08 10:29:20 -0500666 fContext->priv().auditTrail()));
Brian Osman45580d32016-11-23 09:37:01 -0500667
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400668 SkASSERT(textureProxy->getLastOpList() == opList.get());
Robert Phillips4a395042017-04-24 16:27:17 +0000669
Robert Phillips22310d62018-09-05 11:07:21 -0400670 fDAG.add(opList);
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400671 if (!fDAG.sortingOpLists() || !fReduceOpListSplitting) {
672 fActiveOpList = opList.get();
673 }
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400674
Robert Phillips38d64b02018-09-04 13:23:26 -0400675 SkDEBUGCODE(this->validate());
Robert Phillips4a395042017-04-24 16:27:17 +0000676 return opList;
Brian Osman45580d32016-11-23 09:37:01 -0500677}
678
Herb Derby26cbe512018-05-24 14:39:01 -0400679GrTextContext* GrDrawingManager::getTextContext() {
680 if (!fTextContext) {
681 fTextContext = GrTextContext::Make(fOptionsForTextContext);
brianosman86e76262016-08-11 12:17:31 -0700682 }
683
Herb Derby26cbe512018-05-24 14:39:01 -0400684 return fTextContext.get();
brianosman86e76262016-08-11 12:17:31 -0700685}
686
robertphillips68737822015-10-29 12:12:21 -0700687/*
688 * This method finds a path renderer that can draw the specified path on
689 * the provided target.
690 * Due to its expense, the software path renderer has split out so it can
691 * can be individually allowed/disallowed via the "allowSW" boolean.
692 */
693GrPathRenderer* GrDrawingManager::getPathRenderer(const GrPathRenderer::CanDrawPathArgs& args,
694 bool allowSW,
695 GrPathRendererChain::DrawType drawType,
696 GrPathRenderer::StencilSupport* stencilSupport) {
697
698 if (!fPathRendererChain) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400699 fPathRendererChain.reset(new GrPathRendererChain(fContext, fOptionsForPathRendererChain));
robertphillips68737822015-10-29 12:12:21 -0700700 }
701
702 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(args, drawType, stencilSupport);
703 if (!pr && allowSW) {
Brian Salomone7df0bb2018-05-07 14:44:57 -0400704 auto swPR = this->getSoftwarePathRenderer();
705 if (GrPathRenderer::CanDrawPath::kNo != swPR->canDrawPath(args)) {
706 pr = swPR;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500707 }
robertphillips68737822015-10-29 12:12:21 -0700708 }
709
710 return pr;
711}
712
Brian Salomone7df0bb2018-05-07 14:44:57 -0400713GrPathRenderer* GrDrawingManager::getSoftwarePathRenderer() {
714 if (!fSoftwarePathRenderer) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400715 fSoftwarePathRenderer.reset(
Robert Phillips9da87e02019-02-04 13:26:26 -0500716 new GrSoftwarePathRenderer(fContext->priv().proxyProvider(),
Ben Wagner9ec70c62018-07-12 13:30:47 -0400717 fOptionsForPathRendererChain.fAllowPathMaskCaching));
Brian Salomone7df0bb2018-05-07 14:44:57 -0400718 }
Ben Wagner9ec70c62018-07-12 13:30:47 -0400719 return fSoftwarePathRenderer.get();
Brian Salomone7df0bb2018-05-07 14:44:57 -0400720}
721
Chris Daltonfddb6c02017-11-04 15:22:22 -0600722GrCoverageCountingPathRenderer* GrDrawingManager::getCoverageCountingPathRenderer() {
723 if (!fPathRendererChain) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400724 fPathRendererChain.reset(new GrPathRendererChain(fContext, fOptionsForPathRendererChain));
Chris Daltonfddb6c02017-11-04 15:22:22 -0600725 }
726 return fPathRendererChain->getCoverageCountingPathRenderer();
727}
728
Brian Salomon653f42f2018-07-10 10:07:31 -0400729void GrDrawingManager::flushIfNecessary() {
Robert Phillips6a6de562019-02-15 15:19:15 -0500730 auto direct = fContext->priv().asDirectContext();
731 if (!direct) {
732 return;
733 }
734
735 auto resourceCache = direct->priv().getResourceCache();
Brian Salomon653f42f2018-07-10 10:07:31 -0400736 if (resourceCache && resourceCache->requestsFlush()) {
Greg Daniel797efca2019-05-09 14:04:20 -0400737 this->flush(nullptr, 0, SkSurface::BackendSurfaceAccess::kNoAccess, GrFlushInfo(),
738 GrPrepareForExternalIORequests());
Brian Salomon57d2beab2018-09-10 09:35:41 -0400739 resourceCache->purgeAsNeeded();
Brian Salomon653f42f2018-07-10 10:07:31 -0400740 }
741}
742
Brian Osman11052242016-10-27 14:47:55 -0400743sk_sp<GrRenderTargetContext> GrDrawingManager::makeRenderTargetContext(
Brian Salomond6287472019-06-24 15:50:07 -0400744 sk_sp<GrSurfaceProxy> sProxy,
745 GrColorType colorType,
746 sk_sp<SkColorSpace> colorSpace,
747 const SkSurfaceProps* surfaceProps,
748 bool managedOpList) {
Robert Phillips37430132016-11-09 06:50:43 -0500749 if (this->wasAbandoned() || !sProxy->asRenderTargetProxy()) {
robertphillips3dc6ae52015-10-20 09:54:32 -0700750 return nullptr;
751 }
752
brianosman0e22eb82016-08-30 07:07:59 -0700753 // SkSurface catches bad color space usage at creation. This check handles anything that slips
Brian Salomon366093f2018-02-13 09:25:22 -0500754 // by, including internal usage.
Robert Phillipsf209e882019-06-25 15:59:50 -0400755 if (!SkSurface_Gpu::Valid(fContext->priv().caps(), sProxy->backendFormat())) {
brianosmana9c3c6a2016-09-29 10:08:36 -0700756 SkDEBUGFAIL("Invalid config and colorspace combination");
brianosman0e22eb82016-08-30 07:07:59 -0700757 return nullptr;
758 }
joshualitt96880d92016-02-16 10:36:53 -0800759
Robert Phillips0d075de2019-03-04 11:08:13 -0500760 sk_sp<GrRenderTargetProxy> renderTargetProxy(sk_ref_sp(sProxy->asRenderTargetProxy()));
Robert Phillips2c862492017-01-18 10:08:39 -0500761
Robert Phillips0d075de2019-03-04 11:08:13 -0500762 return sk_sp<GrRenderTargetContext>(new GrRenderTargetContext(fContext,
763 std::move(renderTargetProxy),
Brian Salomond6287472019-06-24 15:50:07 -0400764 colorType,
Robert Phillips0d075de2019-03-04 11:08:13 -0500765 std::move(colorSpace),
766 surfaceProps,
767 managedOpList));
robertphillips3dc6ae52015-10-20 09:54:32 -0700768}
Brian Osman45580d32016-11-23 09:37:01 -0500769
Robert Phillips2c862492017-01-18 10:08:39 -0500770sk_sp<GrTextureContext> GrDrawingManager::makeTextureContext(sk_sp<GrSurfaceProxy> sProxy,
Brian Salomond6287472019-06-24 15:50:07 -0400771 GrColorType colorType,
Brian Salomone7499c72019-06-24 12:12:36 -0400772 SkAlphaType alphaType,
Robert Phillips2c862492017-01-18 10:08:39 -0500773 sk_sp<SkColorSpace> colorSpace) {
Brian Osman45580d32016-11-23 09:37:01 -0500774 if (this->wasAbandoned() || !sProxy->asTextureProxy()) {
775 return nullptr;
776 }
777
Robert Phillips2c862492017-01-18 10:08:39 -0500778 // SkSurface catches bad color space usage at creation. This check handles anything that slips
Brian Salomon366093f2018-02-13 09:25:22 -0500779 // by, including internal usage.
Robert Phillipsf209e882019-06-25 15:59:50 -0400780 if (!SkSurface_Gpu::Valid(fContext->priv().caps(), sProxy->backendFormat())) {
Robert Phillips2c862492017-01-18 10:08:39 -0500781 SkDEBUGFAIL("Invalid config and colorspace combination");
782 return nullptr;
783 }
784
Robert Phillips383c4182018-02-07 12:47:44 -0500785 // GrTextureRenderTargets should always be using a GrRenderTargetContext
Brian Osman45580d32016-11-23 09:37:01 -0500786 SkASSERT(!sProxy->asRenderTargetProxy());
787
788 sk_sp<GrTextureProxy> textureProxy(sk_ref_sp(sProxy->asTextureProxy()));
789
Robert Phillips0d075de2019-03-04 11:08:13 -0500790 return sk_sp<GrTextureContext>(new GrTextureContext(fContext,
791 std::move(textureProxy),
Brian Salomond6287472019-06-24 15:50:07 -0400792 colorType,
793 alphaType,
Robert Phillips0d075de2019-03-04 11:08:13 -0500794 std::move(colorSpace)));
Brian Osman45580d32016-11-23 09:37:01 -0500795}