blob: 10da8f947f2684eaaaf7d2d456e8f2a496ec189a [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
bsalomon5eb41fd2016-09-06 13:49:32 -07008#include "GrDrawingManager.h"
Robert Phillips69893702019-02-22 11:16:30 -05009
Greg Daniel51316782017-08-02 15:10:09 +000010#include "GrBackendSemaphore.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050011#include "GrContextPriv.h"
Robert Phillips646e4292017-06-13 12:44:56 -040012#include "GrGpu.h"
Robert Phillipsc994a932018-06-19 13:09:54 -040013#include "GrMemoryPool.h"
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040014#include "GrOnFlushResourceProvider.h"
Robert Phillips9d6c64f2017-09-14 10:56:45 -040015#include "GrOpList.h"
Robert Phillips69893702019-02-22 11:16:30 -050016#include "GrRecordingContext.h"
17#include "GrRecordingContextPriv.h"
Brian Osman11052242016-10-27 14:47:55 -040018#include "GrRenderTargetContext.h"
Robert Phillipsc7635fa2016-10-28 13:25:24 -040019#include "GrRenderTargetProxy.h"
Robert Phillipsd375dbf2017-09-14 12:45:25 -040020#include "GrResourceAllocator.h"
robertphillips3dc6ae52015-10-20 09:54:32 -070021#include "GrResourceProvider.h"
robertphillips68737822015-10-29 12:12:21 -070022#include "GrSoftwarePathRenderer.h"
Robert Phillips7ee385e2017-03-30 08:02:11 -040023#include "GrSurfaceProxyPriv.h"
Brian Salomon930f9392018-06-20 16:25:26 -040024#include "GrTexture.h"
Brian Osman45580d32016-11-23 09:37:01 -050025#include "GrTextureContext.h"
26#include "GrTextureOpList.h"
Brian Salomon930f9392018-06-20 16:25:26 -040027#include "GrTexturePriv.h"
Chris Dalton706a6ff2017-11-29 22:01:06 -070028#include "GrTextureProxy.h"
29#include "GrTextureProxyPriv.h"
Brian Salomon930f9392018-06-20 16:25:26 -040030#include "GrTracing.h"
Robert Phillips62000362018-02-01 09:10:04 -050031#include "SkDeferredDisplayList.h"
brianosman0e22eb82016-08-30 07:07:59 -070032#include "SkSurface_Gpu.h"
robertphillips3dc6ae52015-10-20 09:54:32 -070033#include "SkTTopoSort.h"
Robert Phillips774168e2018-05-31 12:43:27 -040034#include "ccpr/GrCoverageCountingPathRenderer.h"
Herb Derby26cbe512018-05-24 14:39:01 -040035#include "text/GrTextContext.h"
robertphillips498d7ac2015-10-30 10:11:30 -070036
Robert Phillips56181ba2019-03-08 12:00:45 -050037GrDrawingManager::OpListDAG::OpListDAG(bool explicitlyAllocating, bool sortOpLists)
38 : fSortOpLists(sortOpLists) {
39 SkASSERT(!sortOpLists || explicitlyAllocating);
Robert Phillipsa3f70262018-02-08 10:59:38 -050040}
41
Robert Phillips22310d62018-09-05 11:07:21 -040042GrDrawingManager::OpListDAG::~OpListDAG() {}
43
44void GrDrawingManager::OpListDAG::gatherIDs(SkSTArray<8, uint32_t, true>* idArray) const {
45 idArray->reset(fOpLists.count());
Robert Phillipsf2361d22016-10-25 14:20:06 -040046 for (int i = 0; i < fOpLists.count(); ++i) {
Robert Phillips22310d62018-09-05 11:07:21 -040047 if (fOpLists[i]) {
48 (*idArray)[i] = fOpLists[i]->uniqueID();
49 }
50 }
51}
52
53void GrDrawingManager::OpListDAG::reset() {
54 fOpLists.reset();
55}
56
57void GrDrawingManager::OpListDAG::removeOpList(int index) {
58 if (!fOpLists[index]->unique()) {
59 // TODO: Eventually this should be guaranteed unique: http://skbug.com/7111
60 fOpLists[index]->endFlush();
61 }
62
63 fOpLists[index] = nullptr;
64}
65
66void GrDrawingManager::OpListDAG::removeOpLists(int startIndex, int stopIndex) {
67 for (int i = startIndex; i < stopIndex; ++i) {
68 if (!fOpLists[i]) {
69 continue;
70 }
71 this->removeOpList(i);
72 }
73}
74
75void GrDrawingManager::OpListDAG::add(sk_sp<GrOpList> opList) {
76 fOpLists.emplace_back(std::move(opList));
77}
78
79void GrDrawingManager::OpListDAG::add(const SkTArray<sk_sp<GrOpList>>& opLists) {
80 fOpLists.push_back_n(opLists.count(), opLists.begin());
81}
82
83void GrDrawingManager::OpListDAG::swap(SkTArray<sk_sp<GrOpList>>* opLists) {
84 SkASSERT(opLists->empty());
85 opLists->swap(fOpLists);
86}
87
88void GrDrawingManager::OpListDAG::prepForFlush() {
89 if (fSortOpLists) {
90 SkDEBUGCODE(bool result =) SkTTopoSort<GrOpList, GrOpList::TopoSortTraits>(&fOpLists);
91 SkASSERT(result);
92 }
93
94#ifdef SK_DEBUG
95 // This block checks for any unnecessary splits in the opLists. If two sequential opLists
96 // share the same backing GrSurfaceProxy it means the opList was artificially split.
97 if (fOpLists.count()) {
98 GrRenderTargetOpList* prevOpList = fOpLists[0]->asRenderTargetOpList();
99 for (int i = 1; i < fOpLists.count(); ++i) {
100 GrRenderTargetOpList* curOpList = fOpLists[i]->asRenderTargetOpList();
101
102 if (prevOpList && curOpList) {
103 SkASSERT(prevOpList->fTarget.get() != curOpList->fTarget.get());
104 }
105
106 prevOpList = curOpList;
107 }
108 }
109#endif
110}
111
112void GrDrawingManager::OpListDAG::closeAll(const GrCaps* caps) {
113 for (int i = 0; i < fOpLists.count(); ++i) {
114 if (fOpLists[i]) {
115 fOpLists[i]->makeClosed(*caps);
116 }
117 }
118}
119
120void GrDrawingManager::OpListDAG::cleanup(const GrCaps* caps) {
121 for (int i = 0; i < fOpLists.count(); ++i) {
122 if (!fOpLists[i]) {
123 continue;
124 }
125
Robert Phillipsee683652017-04-26 11:53:10 -0400126 // no opList should receive a new command after this
Robert Phillips22310d62018-09-05 11:07:21 -0400127 fOpLists[i]->makeClosed(*caps);
robertphillips0dfa62c2015-11-16 06:23:31 -0800128
Robert Phillipsf2361d22016-10-25 14:20:06 -0400129 // We shouldn't need to do this, but it turns out some clients still hold onto opLists
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400130 // after a cleanup.
131 // MDB TODO: is this still true?
Chris Daltona84cacf2017-10-04 10:30:29 -0600132 if (!fOpLists[i]->unique()) {
133 // TODO: Eventually this should be guaranteed unique.
134 // https://bugs.chromium.org/p/skia/issues/detail?id=7111
135 fOpLists[i]->endFlush();
136 }
robertphillips3dc6ae52015-10-20 09:54:32 -0700137 }
138
Robert Phillipsf2361d22016-10-25 14:20:06 -0400139 fOpLists.reset();
Robert Phillips22310d62018-09-05 11:07:21 -0400140}
141
142///////////////////////////////////////////////////////////////////////////////////////////////////
Robert Phillips69893702019-02-22 11:16:30 -0500143GrDrawingManager::GrDrawingManager(GrRecordingContext* context,
Robert Phillips22310d62018-09-05 11:07:21 -0400144 const GrPathRendererChain::Options& optionsForPathRendererChain,
145 const GrTextContext::Options& optionsForTextContext,
Robert Phillips22310d62018-09-05 11:07:21 -0400146 bool explicitlyAllocating,
Robert Phillips56181ba2019-03-08 12:00:45 -0500147 bool sortOpLists,
Robert Phillips60dd62b2019-03-12 16:28:59 +0000148 GrContextOptions::Enable reduceOpListSplitting)
Robert Phillips22310d62018-09-05 11:07:21 -0400149 : fContext(context)
150 , fOptionsForPathRendererChain(optionsForPathRendererChain)
151 , fOptionsForTextContext(optionsForTextContext)
Robert Phillips22310d62018-09-05 11:07:21 -0400152 , fDAG(explicitlyAllocating, sortOpLists)
153 , fTextContext(nullptr)
154 , fPathRendererChain(nullptr)
155 , fSoftwarePathRenderer(nullptr)
Robert Phillips60dd62b2019-03-12 16:28:59 +0000156 , fFlushing(false) {
157 if (GrContextOptions::Enable::kNo == reduceOpListSplitting) {
158 fReduceOpListSplitting = false;
159 } else if (GrContextOptions::Enable::kYes == reduceOpListSplitting) {
160 fReduceOpListSplitting = true;
161 } else {
162 // For now, this is only turned on when explicitly enabled. Once mini-flushes are
163 // implemented it should be enabled whenever sorting is enabled.
164 fReduceOpListSplitting = false; // sortOpLists
165 }
Robert Phillips22310d62018-09-05 11:07:21 -0400166}
167
168void GrDrawingManager::cleanup() {
Robert Phillips9da87e02019-02-04 13:26:26 -0500169 fDAG.cleanup(fContext->priv().caps());
robertphillips3dc6ae52015-10-20 09:54:32 -0700170
robertphillips13391dd2015-10-30 05:15:11 -0700171 fPathRendererChain = nullptr;
Ben Wagner9ec70c62018-07-12 13:30:47 -0400172 fSoftwarePathRenderer = nullptr;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400173
174 fOnFlushCBObjects.reset();
robertphillips3dc6ae52015-10-20 09:54:32 -0700175}
176
177GrDrawingManager::~GrDrawingManager() {
178 this->cleanup();
179}
180
Robert Phillipsa9162df2019-02-11 14:12:03 -0500181bool GrDrawingManager::wasAbandoned() const {
Robert Phillips6a6de562019-02-15 15:19:15 -0500182 return fContext->priv().abandoned();
robertphillips3dc6ae52015-10-20 09:54:32 -0700183}
184
robertphillips68737822015-10-29 12:12:21 -0700185void GrDrawingManager::freeGpuResources() {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400186 for (int i = fOnFlushCBObjects.count() - 1; i >= 0; --i) {
187 if (!fOnFlushCBObjects[i]->retainOnFreeGpuResources()) {
188 // it's safe to just do this because we're iterating in reverse
189 fOnFlushCBObjects.removeShuffle(i);
190 }
191 }
192
robertphillips68737822015-10-29 12:12:21 -0700193 // a path renderer may be holding onto resources
robertphillips13391dd2015-10-30 05:15:11 -0700194 fPathRendererChain = nullptr;
Ben Wagner9ec70c62018-07-12 13:30:47 -0400195 fSoftwarePathRenderer = nullptr;
Robert Phillipse3302df2017-04-24 07:31:02 -0400196}
197
Robert Phillips7ee385e2017-03-30 08:02:11 -0400198// MDB TODO: make use of the 'proxy' parameter.
Greg Danielbae71212019-03-01 15:24:35 -0500199GrSemaphoresSubmitted GrDrawingManager::flush(GrSurfaceProxy* proxy,
200 SkSurface::BackendSurfaceAccess access,
201 SkSurface::FlushFlags flags,
Brian Salomon57d2bea2018-09-10 09:35:41 -0400202 int numSemaphores,
203 GrBackendSemaphore backendSemaphores[]) {
204 GR_CREATE_TRACE_MARKER_CONTEXT("GrDrawingManager", "flush", fContext);
Brian Salomondcbb9d92017-07-19 10:53:20 -0400205
robertphillips7761d612016-05-16 09:14:53 -0700206 if (fFlushing || this->wasAbandoned()) {
Greg Daniel51316782017-08-02 15:10:09 +0000207 return GrSemaphoresSubmitted::kNo;
joshualittb8918c42015-12-18 09:59:46 -0800208 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400209 SkDEBUGCODE(this->validate());
210
Robert Phillips6a6de562019-02-15 15:19:15 -0500211 auto direct = fContext->priv().asDirectContext();
212 if (!direct) {
213 return GrSemaphoresSubmitted::kNo; // Can't flush while DDL recording
214 }
215
216 GrGpu* gpu = direct->priv().getGpu();
Robert Phillips874b5352018-03-16 08:48:24 -0400217 if (!gpu) {
218 return GrSemaphoresSubmitted::kNo; // Can't flush while DDL recording
219 }
joshualittb8918c42015-12-18 09:59:46 -0800220 fFlushing = true;
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400221
Robert Phillips6a6de562019-02-15 15:19:15 -0500222 auto resourceProvider = direct->priv().resourceProvider();
223 auto resourceCache = direct->priv().getResourceCache();
224
Robert Phillips38d64b02018-09-04 13:23:26 -0400225 // Semi-usually the GrOpLists are already closed at this point, but sometimes Ganesh
226 // needs to flush mid-draw. In that case, the SkGpuDevice's GrOpLists won't be closed
227 // but need to be flushed anyway. Closing such GrOpLists here will mean new
228 // GrOpLists will be created to replace them if the SkGpuDevice(s) write to them again.
Robert Phillips9da87e02019-02-04 13:26:26 -0500229 fDAG.closeAll(fContext->priv().caps());
Robert Phillips38d64b02018-09-04 13:23:26 -0400230 fActiveOpList = nullptr;
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400231
Robert Phillips22310d62018-09-05 11:07:21 -0400232 fDAG.prepForFlush();
Brian Salomon601ac802019-02-07 13:37:16 -0500233 if (!fCpuBufferCache) {
234 // We cache more buffers when the backend is using client side arrays. Otherwise, we
235 // expect each pool will use a CPU buffer as a staging buffer before uploading to a GPU
236 // buffer object. Each pool only requires one staging buffer at a time.
237 int maxCachedBuffers = fContext->priv().caps()->preferClientSideDynamicBuffers() ? 2 : 6;
238 fCpuBufferCache = GrBufferAllocPool::CpuBufferCache::Make(maxCachedBuffers);
Brian Salomon58f153c2018-10-18 21:51:15 -0400239 }
Robert Phillipsa4c93ac2017-05-18 11:40:04 -0400240
Robert Phillips6a6de562019-02-15 15:19:15 -0500241 GrOpFlushState flushState(gpu, resourceProvider, &fTokenTracker, fCpuBufferCache);
Robert Phillips40a29d72018-01-18 12:59:22 -0500242
Chris Daltonfe199b72017-05-05 11:26:15 -0400243 GrOnFlushResourceProvider onFlushProvider(this);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500244 // TODO: AFAICT the only reason fFlushState is on GrDrawingManager rather than on the
245 // stack here is to preserve the flush tokens.
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400246
Chris Dalton12658942017-10-05 19:45:25 -0600247 // Prepare any onFlush op lists (e.g. atlases).
Chris Daltonfe199b72017-05-05 11:26:15 -0400248 if (!fOnFlushCBObjects.empty()) {
Robert Phillips22310d62018-09-05 11:07:21 -0400249 fDAG.gatherIDs(&fFlushingOpListIDs);
250
Chris Dalton12658942017-10-05 19:45:25 -0600251 SkSTArray<4, sk_sp<GrRenderTargetContext>> renderTargetContexts;
Chris Daltonfe199b72017-05-05 11:26:15 -0400252 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
253 onFlushCBObject->preFlush(&onFlushProvider,
Chris Dalton3968ff92017-11-27 12:26:31 -0700254 fFlushingOpListIDs.begin(), fFlushingOpListIDs.count(),
Chris Daltonfe199b72017-05-05 11:26:15 -0400255 &renderTargetContexts);
Chris Dalton12658942017-10-05 19:45:25 -0600256 for (const sk_sp<GrRenderTargetContext>& rtc : renderTargetContexts) {
Chris Dalton706a6ff2017-11-29 22:01:06 -0700257 sk_sp<GrRenderTargetOpList> onFlushOpList = sk_ref_sp(rtc->getRTOpList());
Chris Dalton12658942017-10-05 19:45:25 -0600258 if (!onFlushOpList) {
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400259 continue; // Odd - but not a big deal
260 }
Chris Dalton706a6ff2017-11-29 22:01:06 -0700261#ifdef SK_DEBUG
262 // OnFlush callbacks are already invoked during flush, and are therefore expected to
263 // handle resource allocation & usage on their own. (No deferred or lazy proxies!)
264 onFlushOpList->visitProxies_debugOnly([](GrSurfaceProxy* p) {
265 SkASSERT(!p->asTextureProxy() || !p->asTextureProxy()->texPriv().isDeferred());
Greg Daniel65fa8ca2018-01-10 17:06:31 -0500266 SkASSERT(GrSurfaceProxy::LazyState::kNot == p->lazyInstantiationState());
Chris Dalton706a6ff2017-11-29 22:01:06 -0700267 });
268#endif
Robert Phillips9da87e02019-02-04 13:26:26 -0500269 onFlushOpList->makeClosed(*fContext->priv().caps());
Robert Phillips40a29d72018-01-18 12:59:22 -0500270 onFlushOpList->prepare(&flushState);
Chris Dalton3968ff92017-11-27 12:26:31 -0700271 fOnFlushCBOpLists.push_back(std::move(onFlushOpList));
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400272 }
273 renderTargetContexts.reset();
274 }
275 }
276
robertphillipsa13e2022015-11-11 12:01:09 -0800277#if 0
Brian Salomon09d994e2016-12-21 11:14:46 -0500278 // Enable this to print out verbose GrOp information
Robert Phillipsf2361d22016-10-25 14:20:06 -0400279 for (int i = 0; i < fOpLists.count(); ++i) {
280 SkDEBUGCODE(fOpLists[i]->dump();)
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 Salomon876a0172019-03-08 11:12:14 -0500288 GrResourceAllocator alloc(resourceProvider, flushState.deinstantiateProxyTracker());
Robert Phillips22310d62018-09-05 11:07:21 -0400289 for (int i = 0; i < fDAG.numOpLists(); ++i) {
290 if (fDAG.opList(i)) {
291 fDAG.opList(i)->gatherProxyIntervals(&alloc);
292 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500293 alloc.markEndOfOpList(i);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500294 }
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400295
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500296 GrResourceAllocator::AssignError error = GrResourceAllocator::AssignError::kNoError;
Greg Danield2073452018-12-07 11:20:33 -0500297 int numOpListsExecuted = 0;
Brian Salomon577aa0f2018-11-30 13:32:23 -0500298 while (alloc.assign(&startIndex, &stopIndex, &error)) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500299 if (GrResourceAllocator::AssignError::kFailedProxyInstantiation == error) {
300 for (int i = startIndex; i < stopIndex; ++i) {
Robert Phillips22310d62018-09-05 11:07:21 -0400301 if (fDAG.opList(i) && !fDAG.opList(i)->isFullyInstantiated()) {
Robert Phillips01a91282018-07-26 08:03:04 -0400302 // If the backing surface wasn't allocated drop the entire opList.
Robert Phillips22310d62018-09-05 11:07:21 -0400303 fDAG.removeOpList(i);
Robert Phillips01a91282018-07-26 08:03:04 -0400304 }
Robert Phillips22310d62018-09-05 11:07:21 -0400305 if (fDAG.opList(i)) {
306 fDAG.opList(i)->purgeOpsWithUninstantiatedProxies();
Robert Phillipsbbcb7f72018-05-31 11:16:19 -0400307 }
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500308 }
309 }
Robert Phillips4150eea2018-02-07 17:08:21 -0500310
Greg Danield2073452018-12-07 11:20:33 -0500311 if (this->executeOpLists(startIndex, stopIndex, &flushState, &numOpListsExecuted)) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500312 flushed = true;
313 }
bsalomondc438982016-08-31 11:53:49 -0700314 }
Greg Danielc42b20b2017-10-04 10:34:49 -0400315 }
316
Chris Dalton91ab1552018-04-18 13:24:25 -0600317#ifdef SK_DEBUG
Robert Phillips22310d62018-09-05 11:07:21 -0400318 for (int i = 0; i < fDAG.numOpLists(); ++i) {
Chris Dalton91ab1552018-04-18 13:24:25 -0600319 // If there are any remaining opLists at this point, make sure they will not survive the
320 // flush. Otherwise we need to call endFlush() on them.
321 // http://skbug.com/7111
Robert Phillips22310d62018-09-05 11:07:21 -0400322 SkASSERT(!fDAG.opList(i) || fDAG.opList(i)->unique());
Chris Dalton91ab1552018-04-18 13:24:25 -0600323 }
324#endif
Robert Phillips22310d62018-09-05 11:07:21 -0400325 fDAG.reset();
robertphillipsa13e2022015-11-11 12:01:09 -0800326
Robert Phillipsc994a932018-06-19 13:09:54 -0400327#ifdef SK_DEBUG
328 // In non-DDL mode this checks that all the flushed ops have been freed from the memory pool.
329 // When we move to partial flushes this assert will no longer be valid.
330 // In DDL mode this check is somewhat superfluous since the memory for most of the ops/opLists
331 // will be stored in the DDL's GrOpMemoryPools.
Robert Phillips9da87e02019-02-04 13:26:26 -0500332 GrOpMemoryPool* opMemoryPool = fContext->priv().opMemoryPool();
Robert Phillipsc994a932018-06-19 13:09:54 -0400333 opMemoryPool->isEmpty();
334#endif
335
Greg Danielbae71212019-03-01 15:24:35 -0500336 GrSemaphoresSubmitted result = gpu->finishFlush(proxy, access, flags, numSemaphores,
337 backendSemaphores);
robertphillipsa13e2022015-11-11 12:01:09 -0800338
Brian Salomon876a0172019-03-08 11:12:14 -0500339 flushState.deinstantiateProxyTracker()->deinstantiateAllProxies();
340
Brian Salomon57d2bea2018-09-10 09:35:41 -0400341 // Give the cache a chance to purge resources that become purgeable due to flushing.
342 if (flushed) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500343 resourceCache->purgeAsNeeded();
Brian Salomon876a0172019-03-08 11:12:14 -0500344 flushed = false;
bsalomonb77a9072016-09-07 10:02:04 -0700345 }
Chris Daltonfe199b72017-05-05 11:26:15 -0400346 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
Robert Phillips40a29d72018-01-18 12:59:22 -0500347 onFlushCBObject->postFlush(fTokenTracker.nextTokenToFlush(), fFlushingOpListIDs.begin(),
Chris Dalton3968ff92017-11-27 12:26:31 -0700348 fFlushingOpListIDs.count());
Brian Salomon876a0172019-03-08 11:12:14 -0500349 flushed = true;
350 }
351 if (flushed) {
352 resourceCache->purgeAsNeeded();
Chris Daltonfe199b72017-05-05 11:26:15 -0400353 }
Chris Dalton3968ff92017-11-27 12:26:31 -0700354 fFlushingOpListIDs.reset();
joshualittb8918c42015-12-18 09:59:46 -0800355 fFlushing = false;
Greg Daniel51316782017-08-02 15:10:09 +0000356
357 return result;
robertphillips3dc6ae52015-10-20 09:54:32 -0700358}
359
Greg Danield2073452018-12-07 11:20:33 -0500360bool GrDrawingManager::executeOpLists(int startIndex, int stopIndex, GrOpFlushState* flushState,
361 int* numOpListsExecuted) {
Robert Phillips22310d62018-09-05 11:07:21 -0400362 SkASSERT(startIndex <= stopIndex && stopIndex <= fDAG.numOpLists());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500363
Robert Phillips27483912018-04-20 12:43:18 -0400364#if GR_FLUSH_TIME_OP_SPEW
365 SkDebugf("Flushing opLists: %d to %d out of [%d, %d]\n",
Robert Phillips22310d62018-09-05 11:07:21 -0400366 startIndex, stopIndex, 0, fDAG.numOpLists());
Robert Phillips27483912018-04-20 12:43:18 -0400367 for (int i = startIndex; i < stopIndex; ++i) {
Robert Phillips22310d62018-09-05 11:07:21 -0400368 if (fDAG.opList(i)) {
369 fDAG.opList(i)->dump(true);
Robert Phillips1734dd32018-08-21 13:52:09 -0400370 }
Robert Phillips27483912018-04-20 12:43:18 -0400371 }
372#endif
373
Robert Phillips6a6de562019-02-15 15:19:15 -0500374 auto direct = fContext->priv().asDirectContext();
375 if (!direct) {
376 return false;
377 }
378
Robert Phillips69893702019-02-22 11:16:30 -0500379 auto resourceProvider = direct->priv().resourceProvider();
Robert Phillipseafd48a2017-11-16 07:52:08 -0500380 bool anyOpListsExecuted = false;
381
382 for (int i = startIndex; i < stopIndex; ++i) {
Robert Phillips22310d62018-09-05 11:07:21 -0400383 if (!fDAG.opList(i)) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500384 continue;
385 }
386
Robert Phillips22310d62018-09-05 11:07:21 -0400387 GrOpList* opList = fDAG.opList(i);
388
Robert Phillips4150eea2018-02-07 17:08:21 -0500389 if (resourceProvider->explicitlyAllocateGPUResources()) {
Robert Phillips22310d62018-09-05 11:07:21 -0400390 if (!opList->isFullyInstantiated()) {
Robert Phillips4150eea2018-02-07 17:08:21 -0500391 // If the backing surface wasn't allocated drop the draw of the entire opList.
Robert Phillips22310d62018-09-05 11:07:21 -0400392 fDAG.removeOpList(i);
Robert Phillips4150eea2018-02-07 17:08:21 -0500393 continue;
394 }
395 } else {
Robert Phillips22310d62018-09-05 11:07:21 -0400396 if (!opList->instantiate(resourceProvider)) {
397 fDAG.removeOpList(i);
Robert Phillips4150eea2018-02-07 17:08:21 -0500398 continue;
399 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500400 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500401
402 // TODO: handle this instantiation via lazy surface proxies?
403 // Instantiate all deferred proxies (being built on worker threads) so we can upload them
Robert Phillips6a6de562019-02-15 15:19:15 -0500404 opList->instantiateDeferredProxies(resourceProvider);
Robert Phillips22310d62018-09-05 11:07:21 -0400405 opList->prepare(flushState);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500406 }
407
408 // Upload all data to the GPU
409 flushState->preExecuteDraws();
410
Greg Danield2073452018-12-07 11:20:33 -0500411 // For Vulkan, if we have too many oplists to be flushed we end up allocating a lot of resources
412 // for each command buffer associated with the oplists. If this gets too large we can cause the
413 // devices to go OOM. In practice we usually only hit this case in our tests, but to be safe we
414 // put a cap on the number of oplists we will execute before flushing to the GPU to relieve some
415 // memory pressure.
416 static constexpr int kMaxOpListsBeforeFlush = 100;
417
Robert Phillipseafd48a2017-11-16 07:52:08 -0500418 // Execute the onFlush op lists first, if any.
Chris Dalton3968ff92017-11-27 12:26:31 -0700419 for (sk_sp<GrOpList>& onFlushOpList : fOnFlushCBOpLists) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500420 if (!onFlushOpList->execute(flushState)) {
421 SkDebugf("WARNING: onFlushOpList failed to execute.\n");
422 }
423 SkASSERT(onFlushOpList->unique());
424 onFlushOpList = nullptr;
Greg Danield2073452018-12-07 11:20:33 -0500425 (*numOpListsExecuted)++;
426 if (*numOpListsExecuted >= kMaxOpListsBeforeFlush) {
Greg Danielbae71212019-03-01 15:24:35 -0500427 flushState->gpu()->finishFlush(nullptr, SkSurface::BackendSurfaceAccess::kNoAccess,
428 SkSurface::kNone_FlushFlags, 0, nullptr);
Greg Danield2073452018-12-07 11:20:33 -0500429 *numOpListsExecuted = 0;
430 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500431 }
Chris Dalton3968ff92017-11-27 12:26:31 -0700432 fOnFlushCBOpLists.reset();
Robert Phillipseafd48a2017-11-16 07:52:08 -0500433
434 // Execute the normal op lists.
435 for (int i = startIndex; i < stopIndex; ++i) {
Robert Phillips22310d62018-09-05 11:07:21 -0400436 if (!fDAG.opList(i)) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500437 continue;
438 }
439
Robert Phillips22310d62018-09-05 11:07:21 -0400440 if (fDAG.opList(i)->execute(flushState)) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500441 anyOpListsExecuted = true;
442 }
Greg Danield2073452018-12-07 11:20:33 -0500443 (*numOpListsExecuted)++;
444 if (*numOpListsExecuted >= kMaxOpListsBeforeFlush) {
Greg Danielbae71212019-03-01 15:24:35 -0500445 flushState->gpu()->finishFlush(nullptr, SkSurface::BackendSurfaceAccess::kNoAccess,
446 SkSurface::kNone_FlushFlags, 0, nullptr);
Greg Danield2073452018-12-07 11:20:33 -0500447 *numOpListsExecuted = 0;
448 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500449 }
450
451 SkASSERT(!flushState->commandBuffer());
Robert Phillips40a29d72018-01-18 12:59:22 -0500452 SkASSERT(fTokenTracker.nextDrawToken() == fTokenTracker.nextTokenToFlush());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500453
454 // We reset the flush state before the OpLists so that the last resources to be freed are those
455 // that are written to in the OpLists. This helps to make sure the most recently used resources
456 // are the last to be purged by the resource cache.
457 flushState->reset();
458
Robert Phillips22310d62018-09-05 11:07:21 -0400459 fDAG.removeOpLists(startIndex, stopIndex);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500460
461 return anyOpListsExecuted;
462}
463
Greg Daniel51316782017-08-02 15:10:09 +0000464GrSemaphoresSubmitted GrDrawingManager::prepareSurfaceForExternalIO(
Greg Danielbae71212019-03-01 15:24:35 -0500465 GrSurfaceProxy* proxy, SkSurface::BackendSurfaceAccess access, SkSurface::FlushFlags flags,
466 int numSemaphores, GrBackendSemaphore backendSemaphores[]) {
bsalomon6a2b1942016-09-08 11:28:59 -0700467 if (this->wasAbandoned()) {
Greg Daniel51316782017-08-02 15:10:09 +0000468 return GrSemaphoresSubmitted::kNo;
bsalomon6a2b1942016-09-08 11:28:59 -0700469 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400470 SkDEBUGCODE(this->validate());
Robert Phillips7ee385e2017-03-30 08:02:11 -0400471 SkASSERT(proxy);
bsalomon6a2b1942016-09-08 11:28:59 -0700472
Robert Phillips6a6de562019-02-15 15:19:15 -0500473 auto direct = fContext->priv().asDirectContext();
474 if (!direct) {
475 return GrSemaphoresSubmitted::kNo; // Can't flush while DDL recording
476 }
477
478 GrGpu* gpu = direct->priv().getGpu();
Robert Phillips874b5352018-03-16 08:48:24 -0400479 if (!gpu) {
480 return GrSemaphoresSubmitted::kNo; // Can't flush while DDL recording
481 }
482
Robert Phillips6a6de562019-02-15 15:19:15 -0500483 auto resourceProvider = direct->priv().resourceProvider();
484
Kevin Lubick42846132018-01-05 10:11:11 -0500485 GrSemaphoresSubmitted result = GrSemaphoresSubmitted::kNo;
Greg Danielbae71212019-03-01 15:24:35 -0500486 if (proxy->priv().hasPendingIO() || numSemaphores ||
487 SkToBool(flags & SkSurface::kSyncCpu_FlushFlag)) {
488 result = this->flush(proxy, access, flags, numSemaphores, backendSemaphores);
bsalomon6a2b1942016-09-08 11:28:59 -0700489 }
490
Robert Phillips6a6de562019-02-15 15:19:15 -0500491 if (!proxy->instantiate(resourceProvider)) {
Greg Daniel51316782017-08-02 15:10:09 +0000492 return result;
Robert Phillips7ee385e2017-03-30 08:02:11 -0400493 }
494
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400495 GrSurface* surface = proxy->peekSurface();
Brian Salomon930f9392018-06-20 16:25:26 -0400496 if (auto* rt = surface->asRenderTarget()) {
497 gpu->resolveRenderTarget(rt);
498 }
499 if (auto* tex = surface->asTexture()) {
500 if (tex->texturePriv().mipMapped() == GrMipMapped::kYes &&
501 tex->texturePriv().mipMapsAreDirty()) {
502 gpu->regenerateMipMapLevels(tex);
503 }
bsalomon6a2b1942016-09-08 11:28:59 -0700504 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400505
506 SkDEBUGCODE(this->validate());
Greg Daniel51316782017-08-02 15:10:09 +0000507 return result;
bsalomon6a2b1942016-09-08 11:28:59 -0700508}
509
Chris Daltonfe199b72017-05-05 11:26:15 -0400510void GrDrawingManager::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
511 fOnFlushCBObjects.push_back(onFlushCBObject);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400512}
513
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500514#if GR_TEST_UTILS
515void GrDrawingManager::testingOnly_removeOnFlushCallbackObject(GrOnFlushCallbackObject* cb) {
516 int n = std::find(fOnFlushCBObjects.begin(), fOnFlushCBObjects.end(), cb) -
517 fOnFlushCBObjects.begin();
518 SkASSERT(n < fOnFlushCBObjects.count());
519 fOnFlushCBObjects.removeShuffle(n);
520}
521#endif
522
Robert Phillips62000362018-02-01 09:10:04 -0500523void GrDrawingManager::moveOpListsToDDL(SkDeferredDisplayList* ddl) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400524 SkDEBUGCODE(this->validate());
525
526 // no opList should receive a new command after this
Robert Phillips9da87e02019-02-04 13:26:26 -0500527 fDAG.closeAll(fContext->priv().caps());
Robert Phillips38d64b02018-09-04 13:23:26 -0400528 fActiveOpList = nullptr;
Robert Phillips7a137052018-02-01 11:23:12 -0500529
Robert Phillips22310d62018-09-05 11:07:21 -0400530 fDAG.swap(&ddl->fOpLists);
Robert Phillips867ce8f2018-06-21 10:28:36 -0400531
Robert Phillips774168e2018-05-31 12:43:27 -0400532 if (fPathRendererChain) {
533 if (auto ccpr = fPathRendererChain->getCoverageCountingPathRenderer()) {
534 ddl->fPendingPaths = ccpr->detachPendingPaths();
535 }
536 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400537
538 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500539}
540
541void GrDrawingManager::copyOpListsFromDDL(const SkDeferredDisplayList* ddl,
542 GrRenderTargetProxy* newDest) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400543 SkDEBUGCODE(this->validate());
544
545 if (fActiveOpList) {
546 // This is a temporary fix for the partial-MDB world. In that world we're not
547 // reordering so ops that (in the single opList world) would've just glommed onto the
548 // end of the single opList but referred to a far earlier RT need to appear in their
549 // own opList.
Robert Phillips9da87e02019-02-04 13:26:26 -0500550 fActiveOpList->makeClosed(*fContext->priv().caps());
Robert Phillips38d64b02018-09-04 13:23:26 -0400551 fActiveOpList = nullptr;
552 }
553
Robert Phillips62000362018-02-01 09:10:04 -0500554 // Here we jam the proxy that backs the current replay SkSurface into the LazyProxyData.
555 // The lazy proxy that references it (in the copied opLists) will steal its GrTexture.
556 ddl->fLazyProxyData->fReplayDest = newDest;
Robert Phillips774168e2018-05-31 12:43:27 -0400557
558 if (ddl->fPendingPaths.size()) {
559 GrCoverageCountingPathRenderer* ccpr = this->getCoverageCountingPathRenderer();
560
561 ccpr->mergePendingPaths(ddl->fPendingPaths);
562 }
Robert Phillips22310d62018-09-05 11:07:21 -0400563
564 fDAG.add(ddl->fOpLists);
Robert Phillips38d64b02018-09-04 13:23:26 -0400565
566 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500567}
568
Robert Phillips38d64b02018-09-04 13:23:26 -0400569#ifdef SK_DEBUG
570void GrDrawingManager::validate() const {
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400571 if (fDAG.sortingOpLists() && fReduceOpListSplitting) {
572 SkASSERT(!fActiveOpList);
573 } else {
574 if (fActiveOpList) {
575 SkASSERT(!fDAG.empty());
576 SkASSERT(!fActiveOpList->isClosed());
577 SkASSERT(fActiveOpList == fDAG.back());
Robert Phillips38d64b02018-09-04 13:23:26 -0400578 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400579
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400580 for (int i = 0; i < fDAG.numOpLists(); ++i) {
581 if (fActiveOpList != fDAG.opList(i)) {
582 SkASSERT(fDAG.opList(i)->isClosed());
583 }
584 }
585
586 if (!fDAG.empty() && !fDAG.back()->isClosed()) {
587 SkASSERT(fActiveOpList == fDAG.back());
588 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400589 }
590}
591#endif
592
Robert Phillips941d1442017-06-14 16:37:02 -0400593sk_sp<GrRenderTargetOpList> GrDrawingManager::newRTOpList(GrRenderTargetProxy* rtp,
594 bool managedOpList) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400595 SkDEBUGCODE(this->validate());
robertphillips3dc6ae52015-10-20 09:54:32 -0700596 SkASSERT(fContext);
597
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400598 if (fDAG.sortingOpLists() && fReduceOpListSplitting) {
599 // In this case we need to close all the opLists that rely on the current contents of
600 // 'rtp'. That is bc we're going to update the content of the proxy so they need to be
601 // split in case they use both the old and new content. (This is a bit of an overkill:
602 // they really only need to be split if they ever reference proxy's contents again but
603 // that is hard to predict/handle).
604 if (GrOpList* lastOpList = rtp->getLastOpList()) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500605 lastOpList->closeThoseWhoDependOnMe(*fContext->priv().caps());
Robert Phillips46acf9d2018-10-09 09:31:40 -0400606 }
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400607 } else if (fActiveOpList) {
608 // This is a temporary fix for the partial-MDB world. In that world we're not
609 // reordering so ops that (in the single opList world) would've just glommed onto the
610 // end of the single opList but referred to a far earlier RT need to appear in their
611 // own opList.
Robert Phillips9da87e02019-02-04 13:26:26 -0500612 fActiveOpList->makeClosed(*fContext->priv().caps());
Robert Phillips38d64b02018-09-04 13:23:26 -0400613 fActiveOpList = nullptr;
robertphillips3dc6ae52015-10-20 09:54:32 -0700614 }
robertphillips3dc6ae52015-10-20 09:54:32 -0700615
Robert Phillips6a6de562019-02-15 15:19:15 -0500616 // MDB TODO: this is unfortunate. GrOpList only needs the resourceProvider here so that, when
617 // not explicitly allocating resources, it can immediately instantiate 'rtp' so that the use
618 // order matches the allocation order (see the comment in GrOpList's ctor).
619 GrResourceProvider* resourceProvider = nullptr;
620 if (fContext->priv().asDirectContext()) {
621 resourceProvider = fContext->priv().asDirectContext()->priv().resourceProvider();
622 }
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500623
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500624 sk_sp<GrRenderTargetOpList> opList(new GrRenderTargetOpList(
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500625 resourceProvider,
Robert Phillips9da87e02019-02-04 13:26:26 -0500626 fContext->priv().refOpMemoryPool(),
Robert Phillips3a9710b2018-03-27 17:51:55 -0400627 rtp,
Robert Phillipsd6841482019-02-08 10:29:20 -0500628 fContext->priv().auditTrail()));
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400629 SkASSERT(rtp->getLastOpList() == opList.get());
robertphillips3dc6ae52015-10-20 09:54:32 -0700630
Robert Phillips941d1442017-06-14 16:37:02 -0400631 if (managedOpList) {
Robert Phillips22310d62018-09-05 11:07:21 -0400632 fDAG.add(opList);
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400633
634 if (!fDAG.sortingOpLists() || !fReduceOpListSplitting) {
635 fActiveOpList = opList.get();
636 }
Robert Phillips941d1442017-06-14 16:37:02 -0400637 }
robertphillips3dc6ae52015-10-20 09:54:32 -0700638
Robert Phillips38d64b02018-09-04 13:23:26 -0400639 SkDEBUGCODE(this->validate());
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400640 return opList;
robertphillips3dc6ae52015-10-20 09:54:32 -0700641}
642
Robert Phillipsb6deea82017-05-11 14:14:30 -0400643sk_sp<GrTextureOpList> GrDrawingManager::newTextureOpList(GrTextureProxy* textureProxy) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400644 SkDEBUGCODE(this->validate());
Brian Osman45580d32016-11-23 09:37:01 -0500645 SkASSERT(fContext);
646
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400647 if (fDAG.sortingOpLists() && fReduceOpListSplitting) {
648 // In this case we need to close all the opLists that rely on the current contents of
649 // 'texture'. That is bc we're going to update the content of the proxy so they need to
650 // be split in case they use both the old and new content. (This is a bit of an
651 // overkill: they really only need to be split if they ever reference proxy's contents
652 // again but that is hard to predict/handle).
653 if (GrOpList* lastOpList = textureProxy->getLastOpList()) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500654 lastOpList->closeThoseWhoDependOnMe(*fContext->priv().caps());
Robert Phillips46acf9d2018-10-09 09:31:40 -0400655 }
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400656 } else if (fActiveOpList) {
657 // This is a temporary fix for the partial-MDB world. In that world we're not
658 // reordering so ops that (in the single opList world) would've just glommed onto the
659 // end of the single opList but referred to a far earlier RT need to appear in their
660 // own opList.
Robert Phillips9da87e02019-02-04 13:26:26 -0500661 fActiveOpList->makeClosed(*fContext->priv().caps());
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400662 fActiveOpList = nullptr;
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400663 }
664
Robert Phillips6a6de562019-02-15 15:19:15 -0500665 // MDB TODO: this is unfortunate. GrOpList only needs the resourceProvider here so that, when
666 // not explicitly allocating resources, it can immediately instantiate 'texureProxy' so that
667 // the use order matches the allocation order (see the comment in GrOpList's ctor).
668 GrResourceProvider* resourceProvider = nullptr;
669 if (fContext->priv().asDirectContext()) {
670 resourceProvider = fContext->priv().asDirectContext()->priv().resourceProvider();
671 }
672
673 sk_sp<GrTextureOpList> opList(new GrTextureOpList(resourceProvider,
Robert Phillips9da87e02019-02-04 13:26:26 -0500674 fContext->priv().refOpMemoryPool(),
Robert Phillips5efd5ea2017-05-30 13:47:32 -0400675 textureProxy,
Robert Phillipsd6841482019-02-08 10:29:20 -0500676 fContext->priv().auditTrail()));
Brian Osman45580d32016-11-23 09:37:01 -0500677
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400678 SkASSERT(textureProxy->getLastOpList() == opList.get());
Robert Phillips4a395042017-04-24 16:27:17 +0000679
Robert Phillips22310d62018-09-05 11:07:21 -0400680 fDAG.add(opList);
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400681 if (!fDAG.sortingOpLists() || !fReduceOpListSplitting) {
682 fActiveOpList = opList.get();
683 }
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400684
Robert Phillips38d64b02018-09-04 13:23:26 -0400685 SkDEBUGCODE(this->validate());
Robert Phillips4a395042017-04-24 16:27:17 +0000686 return opList;
Brian Osman45580d32016-11-23 09:37:01 -0500687}
688
Herb Derby26cbe512018-05-24 14:39:01 -0400689GrTextContext* GrDrawingManager::getTextContext() {
690 if (!fTextContext) {
691 fTextContext = GrTextContext::Make(fOptionsForTextContext);
brianosman86e76262016-08-11 12:17:31 -0700692 }
693
Herb Derby26cbe512018-05-24 14:39:01 -0400694 return fTextContext.get();
brianosman86e76262016-08-11 12:17:31 -0700695}
696
robertphillips68737822015-10-29 12:12:21 -0700697/*
698 * This method finds a path renderer that can draw the specified path on
699 * the provided target.
700 * Due to its expense, the software path renderer has split out so it can
701 * can be individually allowed/disallowed via the "allowSW" boolean.
702 */
703GrPathRenderer* GrDrawingManager::getPathRenderer(const GrPathRenderer::CanDrawPathArgs& args,
704 bool allowSW,
705 GrPathRendererChain::DrawType drawType,
706 GrPathRenderer::StencilSupport* stencilSupport) {
707
708 if (!fPathRendererChain) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400709 fPathRendererChain.reset(new GrPathRendererChain(fContext, fOptionsForPathRendererChain));
robertphillips68737822015-10-29 12:12:21 -0700710 }
711
712 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(args, drawType, stencilSupport);
713 if (!pr && allowSW) {
Brian Salomone7df0bb2018-05-07 14:44:57 -0400714 auto swPR = this->getSoftwarePathRenderer();
715 if (GrPathRenderer::CanDrawPath::kNo != swPR->canDrawPath(args)) {
716 pr = swPR;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500717 }
robertphillips68737822015-10-29 12:12:21 -0700718 }
719
720 return pr;
721}
722
Brian Salomone7df0bb2018-05-07 14:44:57 -0400723GrPathRenderer* GrDrawingManager::getSoftwarePathRenderer() {
724 if (!fSoftwarePathRenderer) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400725 fSoftwarePathRenderer.reset(
Robert Phillips9da87e02019-02-04 13:26:26 -0500726 new GrSoftwarePathRenderer(fContext->priv().proxyProvider(),
Ben Wagner9ec70c62018-07-12 13:30:47 -0400727 fOptionsForPathRendererChain.fAllowPathMaskCaching));
Brian Salomone7df0bb2018-05-07 14:44:57 -0400728 }
Ben Wagner9ec70c62018-07-12 13:30:47 -0400729 return fSoftwarePathRenderer.get();
Brian Salomone7df0bb2018-05-07 14:44:57 -0400730}
731
Chris Daltonfddb6c02017-11-04 15:22:22 -0600732GrCoverageCountingPathRenderer* GrDrawingManager::getCoverageCountingPathRenderer() {
733 if (!fPathRendererChain) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400734 fPathRendererChain.reset(new GrPathRendererChain(fContext, fOptionsForPathRendererChain));
Chris Daltonfddb6c02017-11-04 15:22:22 -0600735 }
736 return fPathRendererChain->getCoverageCountingPathRenderer();
737}
738
Brian Salomon653f42f2018-07-10 10:07:31 -0400739void GrDrawingManager::flushIfNecessary() {
Robert Phillips6a6de562019-02-15 15:19:15 -0500740 auto direct = fContext->priv().asDirectContext();
741 if (!direct) {
742 return;
743 }
744
745 auto resourceCache = direct->priv().getResourceCache();
Brian Salomon653f42f2018-07-10 10:07:31 -0400746 if (resourceCache && resourceCache->requestsFlush()) {
Greg Danielbae71212019-03-01 15:24:35 -0500747 this->flush(nullptr, SkSurface::BackendSurfaceAccess::kNoAccess,
748 SkSurface::kNone_FlushFlags, 0, nullptr);
Brian Salomon57d2bea2018-09-10 09:35:41 -0400749 resourceCache->purgeAsNeeded();
Brian Salomon653f42f2018-07-10 10:07:31 -0400750 }
751}
752
Brian Osman11052242016-10-27 14:47:55 -0400753sk_sp<GrRenderTargetContext> GrDrawingManager::makeRenderTargetContext(
Robert Phillips37430132016-11-09 06:50:43 -0500754 sk_sp<GrSurfaceProxy> sProxy,
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400755 sk_sp<SkColorSpace> colorSpace,
Robert Phillips941d1442017-06-14 16:37:02 -0400756 const SkSurfaceProps* surfaceProps,
757 bool managedOpList) {
Robert Phillips37430132016-11-09 06:50:43 -0500758 if (this->wasAbandoned() || !sProxy->asRenderTargetProxy()) {
robertphillips3dc6ae52015-10-20 09:54:32 -0700759 return nullptr;
760 }
761
brianosman0e22eb82016-08-30 07:07:59 -0700762 // SkSurface catches bad color space usage at creation. This check handles anything that slips
Brian Salomon366093f2018-02-13 09:25:22 -0500763 // by, including internal usage.
Robert Phillips9da87e02019-02-04 13:26:26 -0500764 if (!SkSurface_Gpu::Valid(fContext->priv().caps(), sProxy->config(), colorSpace.get())) {
brianosmana9c3c6a2016-09-29 10:08:36 -0700765 SkDEBUGFAIL("Invalid config and colorspace combination");
brianosman0e22eb82016-08-30 07:07:59 -0700766 return nullptr;
767 }
joshualitt96880d92016-02-16 10:36:53 -0800768
Robert Phillips0d075de2019-03-04 11:08:13 -0500769 sk_sp<GrRenderTargetProxy> renderTargetProxy(sk_ref_sp(sProxy->asRenderTargetProxy()));
Robert Phillips2c862492017-01-18 10:08:39 -0500770
Robert Phillips0d075de2019-03-04 11:08:13 -0500771 return sk_sp<GrRenderTargetContext>(new GrRenderTargetContext(fContext,
772 std::move(renderTargetProxy),
773 std::move(colorSpace),
774 surfaceProps,
775 managedOpList));
robertphillips3dc6ae52015-10-20 09:54:32 -0700776}
Brian Osman45580d32016-11-23 09:37:01 -0500777
Robert Phillips2c862492017-01-18 10:08:39 -0500778sk_sp<GrTextureContext> GrDrawingManager::makeTextureContext(sk_sp<GrSurfaceProxy> sProxy,
779 sk_sp<SkColorSpace> colorSpace) {
Brian Osman45580d32016-11-23 09:37:01 -0500780 if (this->wasAbandoned() || !sProxy->asTextureProxy()) {
781 return nullptr;
782 }
783
Robert Phillips2c862492017-01-18 10:08:39 -0500784 // SkSurface catches bad color space usage at creation. This check handles anything that slips
Brian Salomon366093f2018-02-13 09:25:22 -0500785 // by, including internal usage.
Robert Phillips9da87e02019-02-04 13:26:26 -0500786 if (!SkSurface_Gpu::Valid(fContext->priv().caps(), sProxy->config(), colorSpace.get())) {
Robert Phillips2c862492017-01-18 10:08:39 -0500787 SkDEBUGFAIL("Invalid config and colorspace combination");
788 return nullptr;
789 }
790
Robert Phillips383c4182018-02-07 12:47:44 -0500791 // GrTextureRenderTargets should always be using a GrRenderTargetContext
Brian Osman45580d32016-11-23 09:37:01 -0500792 SkASSERT(!sProxy->asRenderTargetProxy());
793
794 sk_sp<GrTextureProxy> textureProxy(sk_ref_sp(sProxy->asTextureProxy()));
795
Robert Phillips0d075de2019-03-04 11:08:13 -0500796 return sk_sp<GrTextureContext>(new GrTextureContext(fContext,
797 std::move(textureProxy),
798 std::move(colorSpace)));
Brian Osman45580d32016-11-23 09:37:01 -0500799}