blob: 208940b35f81e129b7c898b374ebbb0b328721e9 [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"
12#include "include/private/GrOpList.h"
13#include "include/private/GrRecordingContext.h"
14#include "include/private/GrRenderTargetProxy.h"
15#include "include/private/GrTextureProxy.h"
16#include "include/private/SkDeferredDisplayList.h"
17#include "src/core/SkTTopoSort.h"
18#include "src/gpu/GrContextPriv.h"
19#include "src/gpu/GrGpu.h"
20#include "src/gpu/GrMemoryPool.h"
21#include "src/gpu/GrOnFlushResourceProvider.h"
22#include "src/gpu/GrRecordingContextPriv.h"
23#include "src/gpu/GrRenderTargetContext.h"
24#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"
31#include "src/gpu/GrTextureProxyPriv.h"
32#include "src/gpu/GrTracing.h"
33#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
34#include "src/gpu/text/GrTextContext.h"
35#include "src/image/SkSurface_Gpu.h"
robertphillips498d7ac2015-10-30 10:11:30 -070036
Robert Phillips12c46292019-04-23 07:36:17 -040037GrDrawingManager::OpListDAG::OpListDAG(bool sortOpLists) : fSortOpLists(sortOpLists) {}
Robert Phillipsa3f70262018-02-08 10:59:38 -050038
Robert Phillips22310d62018-09-05 11:07:21 -040039GrDrawingManager::OpListDAG::~OpListDAG() {}
40
41void GrDrawingManager::OpListDAG::gatherIDs(SkSTArray<8, uint32_t, true>* idArray) const {
42 idArray->reset(fOpLists.count());
Robert Phillipsf2361d22016-10-25 14:20:06 -040043 for (int i = 0; i < fOpLists.count(); ++i) {
Robert Phillips22310d62018-09-05 11:07:21 -040044 if (fOpLists[i]) {
45 (*idArray)[i] = fOpLists[i]->uniqueID();
46 }
47 }
48}
49
50void GrDrawingManager::OpListDAG::reset() {
51 fOpLists.reset();
52}
53
54void GrDrawingManager::OpListDAG::removeOpList(int index) {
55 if (!fOpLists[index]->unique()) {
56 // TODO: Eventually this should be guaranteed unique: http://skbug.com/7111
57 fOpLists[index]->endFlush();
58 }
59
60 fOpLists[index] = nullptr;
61}
62
63void GrDrawingManager::OpListDAG::removeOpLists(int startIndex, int stopIndex) {
64 for (int i = startIndex; i < stopIndex; ++i) {
65 if (!fOpLists[i]) {
66 continue;
67 }
68 this->removeOpList(i);
69 }
70}
71
Robert Phillips9313aa72019-04-09 18:41:27 -040072bool GrDrawingManager::OpListDAG::isUsed(GrSurfaceProxy* proxy) const {
73 for (int i = 0; i < fOpLists.count(); ++i) {
74 if (fOpLists[i] && fOpLists[i]->isUsed(proxy)) {
75 return true;
76 }
77 }
78
79 return false;
80}
81
Robert Phillips22310d62018-09-05 11:07:21 -040082void GrDrawingManager::OpListDAG::add(sk_sp<GrOpList> opList) {
83 fOpLists.emplace_back(std::move(opList));
84}
85
86void GrDrawingManager::OpListDAG::add(const SkTArray<sk_sp<GrOpList>>& opLists) {
87 fOpLists.push_back_n(opLists.count(), opLists.begin());
88}
89
90void GrDrawingManager::OpListDAG::swap(SkTArray<sk_sp<GrOpList>>* opLists) {
91 SkASSERT(opLists->empty());
92 opLists->swap(fOpLists);
93}
94
95void GrDrawingManager::OpListDAG::prepForFlush() {
96 if (fSortOpLists) {
97 SkDEBUGCODE(bool result =) SkTTopoSort<GrOpList, GrOpList::TopoSortTraits>(&fOpLists);
98 SkASSERT(result);
99 }
100
101#ifdef SK_DEBUG
102 // This block checks for any unnecessary splits in the opLists. If two sequential opLists
103 // share the same backing GrSurfaceProxy it means the opList was artificially split.
104 if (fOpLists.count()) {
105 GrRenderTargetOpList* prevOpList = fOpLists[0]->asRenderTargetOpList();
106 for (int i = 1; i < fOpLists.count(); ++i) {
107 GrRenderTargetOpList* curOpList = fOpLists[i]->asRenderTargetOpList();
108
109 if (prevOpList && curOpList) {
110 SkASSERT(prevOpList->fTarget.get() != curOpList->fTarget.get());
111 }
112
113 prevOpList = curOpList;
114 }
115 }
116#endif
117}
118
119void GrDrawingManager::OpListDAG::closeAll(const GrCaps* caps) {
120 for (int i = 0; i < fOpLists.count(); ++i) {
121 if (fOpLists[i]) {
122 fOpLists[i]->makeClosed(*caps);
123 }
124 }
125}
126
127void GrDrawingManager::OpListDAG::cleanup(const GrCaps* caps) {
128 for (int i = 0; i < fOpLists.count(); ++i) {
129 if (!fOpLists[i]) {
130 continue;
131 }
132
Robert Phillipsee683652017-04-26 11:53:10 -0400133 // no opList should receive a new command after this
Robert Phillips22310d62018-09-05 11:07:21 -0400134 fOpLists[i]->makeClosed(*caps);
robertphillips0dfa62c2015-11-16 06:23:31 -0800135
Robert Phillipsf2361d22016-10-25 14:20:06 -0400136 // We shouldn't need to do this, but it turns out some clients still hold onto opLists
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400137 // after a cleanup.
138 // MDB TODO: is this still true?
Chris Daltona84cacf2017-10-04 10:30:29 -0600139 if (!fOpLists[i]->unique()) {
140 // TODO: Eventually this should be guaranteed unique.
141 // https://bugs.chromium.org/p/skia/issues/detail?id=7111
142 fOpLists[i]->endFlush();
143 }
robertphillips3dc6ae52015-10-20 09:54:32 -0700144 }
145
Robert Phillipsf2361d22016-10-25 14:20:06 -0400146 fOpLists.reset();
Robert Phillips22310d62018-09-05 11:07:21 -0400147}
148
149///////////////////////////////////////////////////////////////////////////////////////////////////
Robert Phillips69893702019-02-22 11:16:30 -0500150GrDrawingManager::GrDrawingManager(GrRecordingContext* context,
Robert Phillips22310d62018-09-05 11:07:21 -0400151 const GrPathRendererChain::Options& optionsForPathRendererChain,
152 const GrTextContext::Options& optionsForTextContext,
Robert Phillips56181ba2019-03-08 12:00:45 -0500153 bool sortOpLists,
Robert Phillips6db27c22019-05-01 10:43:56 -0400154 bool reduceOpListSplitting)
Robert Phillips22310d62018-09-05 11:07:21 -0400155 : fContext(context)
156 , fOptionsForPathRendererChain(optionsForPathRendererChain)
157 , fOptionsForTextContext(optionsForTextContext)
Robert Phillips12c46292019-04-23 07:36:17 -0400158 , fDAG(sortOpLists)
Robert Phillips22310d62018-09-05 11:07:21 -0400159 , fTextContext(nullptr)
160 , fPathRendererChain(nullptr)
161 , fSoftwarePathRenderer(nullptr)
Robert Phillips6db27c22019-05-01 10:43:56 -0400162 , fFlushing(false)
163 , fReduceOpListSplitting(reduceOpListSplitting) {
Robert Phillips22310d62018-09-05 11:07:21 -0400164}
165
166void GrDrawingManager::cleanup() {
Robert Phillips9da87e02019-02-04 13:26:26 -0500167 fDAG.cleanup(fContext->priv().caps());
robertphillips3dc6ae52015-10-20 09:54:32 -0700168
robertphillips13391dd2015-10-30 05:15:11 -0700169 fPathRendererChain = nullptr;
Ben Wagner9ec70c62018-07-12 13:30:47 -0400170 fSoftwarePathRenderer = nullptr;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400171
172 fOnFlushCBObjects.reset();
robertphillips3dc6ae52015-10-20 09:54:32 -0700173}
174
175GrDrawingManager::~GrDrawingManager() {
176 this->cleanup();
177}
178
Robert Phillipsa9162df2019-02-11 14:12:03 -0500179bool GrDrawingManager::wasAbandoned() const {
Robert Phillips6a6de562019-02-15 15:19:15 -0500180 return fContext->priv().abandoned();
robertphillips3dc6ae52015-10-20 09:54:32 -0700181}
182
robertphillips68737822015-10-29 12:12:21 -0700183void GrDrawingManager::freeGpuResources() {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400184 for (int i = fOnFlushCBObjects.count() - 1; i >= 0; --i) {
185 if (!fOnFlushCBObjects[i]->retainOnFreeGpuResources()) {
186 // it's safe to just do this because we're iterating in reverse
187 fOnFlushCBObjects.removeShuffle(i);
188 }
189 }
190
robertphillips68737822015-10-29 12:12:21 -0700191 // a path renderer may be holding onto resources
robertphillips13391dd2015-10-30 05:15:11 -0700192 fPathRendererChain = nullptr;
Ben Wagner9ec70c62018-07-12 13:30:47 -0400193 fSoftwarePathRenderer = nullptr;
Robert Phillipse3302df2017-04-24 07:31:02 -0400194}
195
Robert Phillips7ee385e2017-03-30 08:02:11 -0400196// MDB TODO: make use of the 'proxy' parameter.
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400197GrSemaphoresSubmitted GrDrawingManager::flush(GrSurfaceProxy* proxies[],
198 int numProxies,
Greg Danielbae71212019-03-01 15:24:35 -0500199 SkSurface::BackendSurfaceAccess access,
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400200 const GrFlushInfo& info) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400201 SkASSERT(numProxies >= 0);
202 SkASSERT(!numProxies || proxies);
Brian Salomon57d2bea2018-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
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400214 if (kNone_GrFlushFlags == info.fFlags && !info.fNumSemaphores && !info.fFinishedProc) {
215 bool canSkip = numProxies > 0;
216 for (int i = 0; i < numProxies && canSkip; ++i) {
217 canSkip = !fDAG.isUsed(proxies[i]) && !this->isDDLTarget(proxies[i]);
218 }
219 if (canSkip) {
220 return GrSemaphoresSubmitted::kNo;
221 }
Robert Phillips9313aa72019-04-09 18:41:27 -0400222 }
223
Robert Phillips6a6de562019-02-15 15:19:15 -0500224 auto direct = fContext->priv().asDirectContext();
225 if (!direct) {
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400226 if (info.fFinishedProc) {
227 info.fFinishedProc(info.fFinishedContext);
Greg Daniela3aa75a2019-04-12 14:24:55 -0400228 }
Robert Phillips6a6de562019-02-15 15:19:15 -0500229 return GrSemaphoresSubmitted::kNo; // Can't flush while DDL recording
230 }
231
232 GrGpu* gpu = direct->priv().getGpu();
Robert Phillips874b5352018-03-16 08:48:24 -0400233 if (!gpu) {
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400234 if (info.fFinishedProc) {
235 info.fFinishedProc(info.fFinishedContext);
Greg Daniela3aa75a2019-04-12 14:24:55 -0400236 }
Robert Phillips874b5352018-03-16 08:48:24 -0400237 return GrSemaphoresSubmitted::kNo; // Can't flush while DDL recording
238 }
Greg Daniela3aa75a2019-04-12 14:24:55 -0400239
joshualittb8918c42015-12-18 09:59:46 -0800240 fFlushing = true;
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400241
Robert Phillips6a6de562019-02-15 15:19:15 -0500242 auto resourceProvider = direct->priv().resourceProvider();
243 auto resourceCache = direct->priv().getResourceCache();
244
Robert Phillips38d64b02018-09-04 13:23:26 -0400245 // Semi-usually the GrOpLists are already closed at this point, but sometimes Ganesh
246 // needs to flush mid-draw. In that case, the SkGpuDevice's GrOpLists won't be closed
247 // but need to be flushed anyway. Closing such GrOpLists here will mean new
248 // GrOpLists will be created to replace them if the SkGpuDevice(s) write to them again.
Robert Phillips9da87e02019-02-04 13:26:26 -0500249 fDAG.closeAll(fContext->priv().caps());
Robert Phillips38d64b02018-09-04 13:23:26 -0400250 fActiveOpList = nullptr;
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400251
Robert Phillips22310d62018-09-05 11:07:21 -0400252 fDAG.prepForFlush();
Brian Salomon601ac802019-02-07 13:37:16 -0500253 if (!fCpuBufferCache) {
254 // We cache more buffers when the backend is using client side arrays. Otherwise, we
255 // expect each pool will use a CPU buffer as a staging buffer before uploading to a GPU
256 // buffer object. Each pool only requires one staging buffer at a time.
257 int maxCachedBuffers = fContext->priv().caps()->preferClientSideDynamicBuffers() ? 2 : 6;
258 fCpuBufferCache = GrBufferAllocPool::CpuBufferCache::Make(maxCachedBuffers);
Brian Salomon58f153c2018-10-18 21:51:15 -0400259 }
Robert Phillipsa4c93ac2017-05-18 11:40:04 -0400260
Brian Salomon2c791fc2019-04-02 11:52:03 -0400261 GrOpFlushState flushState(gpu, resourceProvider, resourceCache, &fTokenTracker,
262 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!)
285 onFlushOpList->visitProxies_debugOnly([](GrSurfaceProxy* p) {
286 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 Phillips22310d62018-09-05 11:07:21 -0400324 if (fDAG.opList(i) && !fDAG.opList(i)->isFullyInstantiated()) {
Robert Phillips01a91282018-07-26 08:03:04 -0400325 // 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
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400360 GrSemaphoresSubmitted result = gpu->finishFlush(proxies, numProxies, access, info);
robertphillipsa13e2022015-11-11 12:01:09 -0800361
Brian Salomon876a0172019-03-08 11:12:14 -0500362 flushState.deinstantiateProxyTracker()->deinstantiateAllProxies();
363
Brian Salomon57d2bea2018-09-10 09:35:41 -0400364 // Give the cache a chance to purge resources that become purgeable due to flushing.
365 if (flushed) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500366 resourceCache->purgeAsNeeded();
Brian Salomon876a0172019-03-08 11:12:14 -0500367 flushed = false;
bsalomonb77a9072016-09-07 10:02:04 -0700368 }
Chris Daltonfe199b72017-05-05 11:26:15 -0400369 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
Robert Phillips40a29d72018-01-18 12:59:22 -0500370 onFlushCBObject->postFlush(fTokenTracker.nextTokenToFlush(), fFlushingOpListIDs.begin(),
Chris Dalton3968ff92017-11-27 12:26:31 -0700371 fFlushingOpListIDs.count());
Brian Salomon876a0172019-03-08 11:12:14 -0500372 flushed = true;
373 }
374 if (flushed) {
375 resourceCache->purgeAsNeeded();
Chris Daltonfe199b72017-05-05 11:26:15 -0400376 }
Chris Dalton3968ff92017-11-27 12:26:31 -0700377 fFlushingOpListIDs.reset();
joshualittb8918c42015-12-18 09:59:46 -0800378 fFlushing = false;
Greg Daniel51316782017-08-02 15:10:09 +0000379
380 return result;
robertphillips3dc6ae52015-10-20 09:54:32 -0700381}
382
Greg Danield2073452018-12-07 11:20:33 -0500383bool GrDrawingManager::executeOpLists(int startIndex, int stopIndex, GrOpFlushState* flushState,
384 int* numOpListsExecuted) {
Robert Phillips22310d62018-09-05 11:07:21 -0400385 SkASSERT(startIndex <= stopIndex && stopIndex <= fDAG.numOpLists());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500386
Robert Phillips27483912018-04-20 12:43:18 -0400387#if GR_FLUSH_TIME_OP_SPEW
388 SkDebugf("Flushing opLists: %d to %d out of [%d, %d]\n",
Robert Phillips22310d62018-09-05 11:07:21 -0400389 startIndex, stopIndex, 0, fDAG.numOpLists());
Robert Phillips27483912018-04-20 12:43:18 -0400390 for (int i = startIndex; i < stopIndex; ++i) {
Robert Phillips22310d62018-09-05 11:07:21 -0400391 if (fDAG.opList(i)) {
392 fDAG.opList(i)->dump(true);
Robert Phillips1734dd32018-08-21 13:52:09 -0400393 }
Robert Phillips27483912018-04-20 12:43:18 -0400394 }
395#endif
396
Robert Phillips6a6de562019-02-15 15:19:15 -0500397 auto direct = fContext->priv().asDirectContext();
398 if (!direct) {
399 return false;
400 }
401
Robert Phillips69893702019-02-22 11:16:30 -0500402 auto resourceProvider = direct->priv().resourceProvider();
Robert Phillipseafd48a2017-11-16 07:52:08 -0500403 bool anyOpListsExecuted = false;
404
405 for (int i = startIndex; i < stopIndex; ++i) {
Robert Phillips22310d62018-09-05 11:07:21 -0400406 if (!fDAG.opList(i)) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500407 continue;
408 }
409
Robert Phillips22310d62018-09-05 11:07:21 -0400410 GrOpList* opList = fDAG.opList(i);
411
Robert Phillips12c46292019-04-23 07:36:17 -0400412 if (!opList->isFullyInstantiated()) {
413 // If the backing surface wasn't allocated drop the draw of the entire opList.
414 fDAG.removeOpList(i);
415 continue;
Robert Phillipseafd48a2017-11-16 07:52:08 -0500416 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500417
418 // TODO: handle this instantiation via lazy surface proxies?
419 // Instantiate all deferred proxies (being built on worker threads) so we can upload them
Robert Phillips6a6de562019-02-15 15:19:15 -0500420 opList->instantiateDeferredProxies(resourceProvider);
Robert Phillips22310d62018-09-05 11:07:21 -0400421 opList->prepare(flushState);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500422 }
423
424 // Upload all data to the GPU
425 flushState->preExecuteDraws();
426
Greg Danield2073452018-12-07 11:20:33 -0500427 // For Vulkan, if we have too many oplists to be flushed we end up allocating a lot of resources
428 // for each command buffer associated with the oplists. If this gets too large we can cause the
429 // devices to go OOM. In practice we usually only hit this case in our tests, but to be safe we
430 // put a cap on the number of oplists we will execute before flushing to the GPU to relieve some
431 // memory pressure.
432 static constexpr int kMaxOpListsBeforeFlush = 100;
433
Robert Phillipseafd48a2017-11-16 07:52:08 -0500434 // Execute the onFlush op lists first, if any.
Chris Dalton3968ff92017-11-27 12:26:31 -0700435 for (sk_sp<GrOpList>& onFlushOpList : fOnFlushCBOpLists) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500436 if (!onFlushOpList->execute(flushState)) {
437 SkDebugf("WARNING: onFlushOpList failed to execute.\n");
438 }
439 SkASSERT(onFlushOpList->unique());
440 onFlushOpList = nullptr;
Greg Danield2073452018-12-07 11:20:33 -0500441 (*numOpListsExecuted)++;
442 if (*numOpListsExecuted >= kMaxOpListsBeforeFlush) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400443 flushState->gpu()->finishFlush(nullptr, 0, SkSurface::BackendSurfaceAccess::kNoAccess,
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400444 GrFlushInfo());
Greg Danield2073452018-12-07 11:20:33 -0500445 *numOpListsExecuted = 0;
446 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500447 }
Chris Dalton3968ff92017-11-27 12:26:31 -0700448 fOnFlushCBOpLists.reset();
Robert Phillipseafd48a2017-11-16 07:52:08 -0500449
450 // Execute the normal op lists.
451 for (int i = startIndex; i < stopIndex; ++i) {
Robert Phillips22310d62018-09-05 11:07:21 -0400452 if (!fDAG.opList(i)) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500453 continue;
454 }
455
Robert Phillips22310d62018-09-05 11:07:21 -0400456 if (fDAG.opList(i)->execute(flushState)) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500457 anyOpListsExecuted = true;
458 }
Greg Danield2073452018-12-07 11:20:33 -0500459 (*numOpListsExecuted)++;
460 if (*numOpListsExecuted >= kMaxOpListsBeforeFlush) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400461 flushState->gpu()->finishFlush(nullptr, 0, SkSurface::BackendSurfaceAccess::kNoAccess,
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400462 GrFlushInfo());
Greg Danield2073452018-12-07 11:20:33 -0500463 *numOpListsExecuted = 0;
464 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500465 }
466
467 SkASSERT(!flushState->commandBuffer());
Robert Phillips40a29d72018-01-18 12:59:22 -0500468 SkASSERT(fTokenTracker.nextDrawToken() == fTokenTracker.nextTokenToFlush());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500469
470 // We reset the flush state before the OpLists so that the last resources to be freed are those
471 // that are written to in the OpLists. This helps to make sure the most recently used resources
472 // are the last to be purged by the resource cache.
473 flushState->reset();
474
Robert Phillips22310d62018-09-05 11:07:21 -0400475 fDAG.removeOpLists(startIndex, stopIndex);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500476
477 return anyOpListsExecuted;
478}
479
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400480GrSemaphoresSubmitted GrDrawingManager::flushSurfaces(GrSurfaceProxy* proxies[], int numProxies,
481 SkSurface::BackendSurfaceAccess access,
482 const GrFlushInfo& info) {
bsalomon6a2b1942016-09-08 11:28:59 -0700483 if (this->wasAbandoned()) {
Greg Daniel51316782017-08-02 15:10:09 +0000484 return GrSemaphoresSubmitted::kNo;
bsalomon6a2b1942016-09-08 11:28:59 -0700485 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400486 SkDEBUGCODE(this->validate());
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400487 SkASSERT(numProxies >= 0);
488 SkASSERT(!numProxies || proxies);
bsalomon6a2b1942016-09-08 11:28:59 -0700489
Robert Phillips6a6de562019-02-15 15:19:15 -0500490 auto direct = fContext->priv().asDirectContext();
491 if (!direct) {
492 return GrSemaphoresSubmitted::kNo; // Can't flush while DDL recording
493 }
494
495 GrGpu* gpu = direct->priv().getGpu();
Robert Phillips874b5352018-03-16 08:48:24 -0400496 if (!gpu) {
497 return GrSemaphoresSubmitted::kNo; // Can't flush while DDL recording
498 }
499
Robert Phillipsacc10fa2019-04-01 09:50:20 -0400500 // TODO: It is important to upgrade the drawingmanager to just flushing the
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400501 // portion of the DAG required by 'proxies' in order to restore some of the
Robert Phillipsacc10fa2019-04-01 09:50:20 -0400502 // semantics of this method.
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400503 GrSemaphoresSubmitted result = this->flush(proxies, numProxies, access, info);
504 for (int i = 0; i < numProxies; ++i) {
505 if (!proxies[i]->isInstantiated()) {
506 return result;
507 }
Robert Phillips7ee385e2017-03-30 08:02:11 -0400508 }
509
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400510 for (int i = 0; i < numProxies; ++i) {
511 GrSurface* surface = proxies[i]->peekSurface();
512 if (auto* rt = surface->asRenderTarget()) {
513 gpu->resolveRenderTarget(rt);
514 }
515 if (auto* tex = surface->asTexture()) {
516 if (tex->texturePriv().mipMapped() == GrMipMapped::kYes &&
517 tex->texturePriv().mipMapsAreDirty()) {
518 gpu->regenerateMipMapLevels(tex);
519 }
Brian Salomon930f9392018-06-20 16:25:26 -0400520 }
bsalomon6a2b1942016-09-08 11:28:59 -0700521 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400522
523 SkDEBUGCODE(this->validate());
Greg Daniel51316782017-08-02 15:10:09 +0000524 return result;
bsalomon6a2b1942016-09-08 11:28:59 -0700525}
526
Chris Daltonfe199b72017-05-05 11:26:15 -0400527void GrDrawingManager::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
528 fOnFlushCBObjects.push_back(onFlushCBObject);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400529}
530
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500531#if GR_TEST_UTILS
532void GrDrawingManager::testingOnly_removeOnFlushCallbackObject(GrOnFlushCallbackObject* cb) {
533 int n = std::find(fOnFlushCBObjects.begin(), fOnFlushCBObjects.end(), cb) -
534 fOnFlushCBObjects.begin();
535 SkASSERT(n < fOnFlushCBObjects.count());
536 fOnFlushCBObjects.removeShuffle(n);
537}
538#endif
539
Robert Phillips62000362018-02-01 09:10:04 -0500540void GrDrawingManager::moveOpListsToDDL(SkDeferredDisplayList* ddl) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400541 SkDEBUGCODE(this->validate());
542
543 // no opList should receive a new command after this
Robert Phillips9da87e02019-02-04 13:26:26 -0500544 fDAG.closeAll(fContext->priv().caps());
Robert Phillips38d64b02018-09-04 13:23:26 -0400545 fActiveOpList = nullptr;
Robert Phillips7a137052018-02-01 11:23:12 -0500546
Robert Phillips22310d62018-09-05 11:07:21 -0400547 fDAG.swap(&ddl->fOpLists);
Robert Phillips867ce8f2018-06-21 10:28:36 -0400548
Robert Phillips774168e2018-05-31 12:43:27 -0400549 if (fPathRendererChain) {
550 if (auto ccpr = fPathRendererChain->getCoverageCountingPathRenderer()) {
551 ddl->fPendingPaths = ccpr->detachPendingPaths();
552 }
553 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400554
555 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500556}
557
558void GrDrawingManager::copyOpListsFromDDL(const SkDeferredDisplayList* ddl,
559 GrRenderTargetProxy* newDest) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400560 SkDEBUGCODE(this->validate());
561
562 if (fActiveOpList) {
563 // This is a temporary fix for the partial-MDB world. In that world we're not
564 // reordering so ops that (in the single opList world) would've just glommed onto the
565 // end of the single opList but referred to a far earlier RT need to appear in their
566 // own opList.
Robert Phillips9da87e02019-02-04 13:26:26 -0500567 fActiveOpList->makeClosed(*fContext->priv().caps());
Robert Phillips38d64b02018-09-04 13:23:26 -0400568 fActiveOpList = nullptr;
569 }
570
Robert Phillips15c91422019-05-07 16:54:48 -0400571 this->addDDLTarget(newDest);
572
Robert Phillips62000362018-02-01 09:10:04 -0500573 // Here we jam the proxy that backs the current replay SkSurface into the LazyProxyData.
574 // The lazy proxy that references it (in the copied opLists) will steal its GrTexture.
575 ddl->fLazyProxyData->fReplayDest = newDest;
Robert Phillips774168e2018-05-31 12:43:27 -0400576
577 if (ddl->fPendingPaths.size()) {
578 GrCoverageCountingPathRenderer* ccpr = this->getCoverageCountingPathRenderer();
579
580 ccpr->mergePendingPaths(ddl->fPendingPaths);
581 }
Robert Phillips22310d62018-09-05 11:07:21 -0400582
583 fDAG.add(ddl->fOpLists);
Robert Phillips38d64b02018-09-04 13:23:26 -0400584
585 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500586}
587
Robert Phillips38d64b02018-09-04 13:23:26 -0400588#ifdef SK_DEBUG
589void GrDrawingManager::validate() const {
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400590 if (fDAG.sortingOpLists() && fReduceOpListSplitting) {
591 SkASSERT(!fActiveOpList);
592 } else {
593 if (fActiveOpList) {
594 SkASSERT(!fDAG.empty());
595 SkASSERT(!fActiveOpList->isClosed());
596 SkASSERT(fActiveOpList == fDAG.back());
Robert Phillips38d64b02018-09-04 13:23:26 -0400597 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400598
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400599 for (int i = 0; i < fDAG.numOpLists(); ++i) {
600 if (fActiveOpList != fDAG.opList(i)) {
601 SkASSERT(fDAG.opList(i)->isClosed());
602 }
603 }
604
605 if (!fDAG.empty() && !fDAG.back()->isClosed()) {
606 SkASSERT(fActiveOpList == fDAG.back());
607 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400608 }
609}
610#endif
611
Robert Phillips831a2932019-04-12 17:18:39 -0400612sk_sp<GrRenderTargetOpList> GrDrawingManager::newRTOpList(sk_sp<GrRenderTargetProxy> rtp,
Robert Phillips941d1442017-06-14 16:37:02 -0400613 bool managedOpList) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400614 SkDEBUGCODE(this->validate());
robertphillips3dc6ae52015-10-20 09:54:32 -0700615 SkASSERT(fContext);
616
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400617 if (fDAG.sortingOpLists() && fReduceOpListSplitting) {
618 // In this case we need to close all the opLists that rely on the current contents of
619 // 'rtp'. That is bc we're going to update the content of the proxy so they need to be
620 // split in case they use both the old and new content. (This is a bit of an overkill:
621 // they really only need to be split if they ever reference proxy's contents again but
622 // that is hard to predict/handle).
623 if (GrOpList* lastOpList = rtp->getLastOpList()) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500624 lastOpList->closeThoseWhoDependOnMe(*fContext->priv().caps());
Robert Phillips46acf9d2018-10-09 09:31:40 -0400625 }
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400626 } else if (fActiveOpList) {
627 // This is a temporary fix for the partial-MDB world. In that world we're not
628 // reordering so ops that (in the single opList world) would've just glommed onto the
629 // end of the single opList but referred to a far earlier RT need to appear in their
630 // own opList.
Robert Phillips9da87e02019-02-04 13:26:26 -0500631 fActiveOpList->makeClosed(*fContext->priv().caps());
Robert Phillips38d64b02018-09-04 13:23:26 -0400632 fActiveOpList = nullptr;
robertphillips3dc6ae52015-10-20 09:54:32 -0700633 }
robertphillips3dc6ae52015-10-20 09:54:32 -0700634
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500635 sk_sp<GrRenderTargetOpList> opList(new GrRenderTargetOpList(
Robert Phillips9da87e02019-02-04 13:26:26 -0500636 fContext->priv().refOpMemoryPool(),
Robert Phillips3a9710b2018-03-27 17:51:55 -0400637 rtp,
Robert Phillipsd6841482019-02-08 10:29:20 -0500638 fContext->priv().auditTrail()));
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400639 SkASSERT(rtp->getLastOpList() == opList.get());
robertphillips3dc6ae52015-10-20 09:54:32 -0700640
Robert Phillips941d1442017-06-14 16:37:02 -0400641 if (managedOpList) {
Robert Phillips22310d62018-09-05 11:07:21 -0400642 fDAG.add(opList);
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400643
644 if (!fDAG.sortingOpLists() || !fReduceOpListSplitting) {
645 fActiveOpList = opList.get();
646 }
Robert Phillips941d1442017-06-14 16:37:02 -0400647 }
robertphillips3dc6ae52015-10-20 09:54:32 -0700648
Robert Phillips38d64b02018-09-04 13:23:26 -0400649 SkDEBUGCODE(this->validate());
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400650 return opList;
robertphillips3dc6ae52015-10-20 09:54:32 -0700651}
652
Robert Phillips831a2932019-04-12 17:18:39 -0400653sk_sp<GrTextureOpList> GrDrawingManager::newTextureOpList(sk_sp<GrTextureProxy> textureProxy) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400654 SkDEBUGCODE(this->validate());
Brian Osman45580d32016-11-23 09:37:01 -0500655 SkASSERT(fContext);
656
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400657 if (fDAG.sortingOpLists() && fReduceOpListSplitting) {
658 // In this case we need to close all the opLists that rely on the current contents of
659 // 'texture'. That is bc we're going to update the content of the proxy so they need to
660 // be split in case they use both the old and new content. (This is a bit of an
661 // overkill: they really only need to be split if they ever reference proxy's contents
662 // again but that is hard to predict/handle).
663 if (GrOpList* lastOpList = textureProxy->getLastOpList()) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500664 lastOpList->closeThoseWhoDependOnMe(*fContext->priv().caps());
Robert Phillips46acf9d2018-10-09 09:31:40 -0400665 }
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400666 } else if (fActiveOpList) {
667 // This is a temporary fix for the partial-MDB world. In that world we're not
668 // reordering so ops that (in the single opList world) would've just glommed onto the
669 // end of the single opList but referred to a far earlier RT need to appear in their
670 // own opList.
Robert Phillips9da87e02019-02-04 13:26:26 -0500671 fActiveOpList->makeClosed(*fContext->priv().caps());
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400672 fActiveOpList = nullptr;
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400673 }
674
Robert Phillips12c46292019-04-23 07:36:17 -0400675 sk_sp<GrTextureOpList> opList(new GrTextureOpList(fContext->priv().refOpMemoryPool(),
Robert Phillips5efd5ea2017-05-30 13:47:32 -0400676 textureProxy,
Robert Phillipsd6841482019-02-08 10:29:20 -0500677 fContext->priv().auditTrail()));
Brian Osman45580d32016-11-23 09:37:01 -0500678
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400679 SkASSERT(textureProxy->getLastOpList() == opList.get());
Robert Phillips4a395042017-04-24 16:27:17 +0000680
Robert Phillips22310d62018-09-05 11:07:21 -0400681 fDAG.add(opList);
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400682 if (!fDAG.sortingOpLists() || !fReduceOpListSplitting) {
683 fActiveOpList = opList.get();
684 }
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400685
Robert Phillips38d64b02018-09-04 13:23:26 -0400686 SkDEBUGCODE(this->validate());
Robert Phillips4a395042017-04-24 16:27:17 +0000687 return opList;
Brian Osman45580d32016-11-23 09:37:01 -0500688}
689
Herb Derby26cbe512018-05-24 14:39:01 -0400690GrTextContext* GrDrawingManager::getTextContext() {
691 if (!fTextContext) {
692 fTextContext = GrTextContext::Make(fOptionsForTextContext);
brianosman86e76262016-08-11 12:17:31 -0700693 }
694
Herb Derby26cbe512018-05-24 14:39:01 -0400695 return fTextContext.get();
brianosman86e76262016-08-11 12:17:31 -0700696}
697
robertphillips68737822015-10-29 12:12:21 -0700698/*
699 * This method finds a path renderer that can draw the specified path on
700 * the provided target.
701 * Due to its expense, the software path renderer has split out so it can
702 * can be individually allowed/disallowed via the "allowSW" boolean.
703 */
704GrPathRenderer* GrDrawingManager::getPathRenderer(const GrPathRenderer::CanDrawPathArgs& args,
705 bool allowSW,
706 GrPathRendererChain::DrawType drawType,
707 GrPathRenderer::StencilSupport* stencilSupport) {
708
709 if (!fPathRendererChain) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400710 fPathRendererChain.reset(new GrPathRendererChain(fContext, fOptionsForPathRendererChain));
robertphillips68737822015-10-29 12:12:21 -0700711 }
712
713 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(args, drawType, stencilSupport);
714 if (!pr && allowSW) {
Brian Salomone7df0bb2018-05-07 14:44:57 -0400715 auto swPR = this->getSoftwarePathRenderer();
716 if (GrPathRenderer::CanDrawPath::kNo != swPR->canDrawPath(args)) {
717 pr = swPR;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500718 }
robertphillips68737822015-10-29 12:12:21 -0700719 }
720
721 return pr;
722}
723
Brian Salomone7df0bb2018-05-07 14:44:57 -0400724GrPathRenderer* GrDrawingManager::getSoftwarePathRenderer() {
725 if (!fSoftwarePathRenderer) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400726 fSoftwarePathRenderer.reset(
Robert Phillips9da87e02019-02-04 13:26:26 -0500727 new GrSoftwarePathRenderer(fContext->priv().proxyProvider(),
Ben Wagner9ec70c62018-07-12 13:30:47 -0400728 fOptionsForPathRendererChain.fAllowPathMaskCaching));
Brian Salomone7df0bb2018-05-07 14:44:57 -0400729 }
Ben Wagner9ec70c62018-07-12 13:30:47 -0400730 return fSoftwarePathRenderer.get();
Brian Salomone7df0bb2018-05-07 14:44:57 -0400731}
732
Chris Daltonfddb6c02017-11-04 15:22:22 -0600733GrCoverageCountingPathRenderer* GrDrawingManager::getCoverageCountingPathRenderer() {
734 if (!fPathRendererChain) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400735 fPathRendererChain.reset(new GrPathRendererChain(fContext, fOptionsForPathRendererChain));
Chris Daltonfddb6c02017-11-04 15:22:22 -0600736 }
737 return fPathRendererChain->getCoverageCountingPathRenderer();
738}
739
Brian Salomon653f42f2018-07-10 10:07:31 -0400740void GrDrawingManager::flushIfNecessary() {
Robert Phillips6a6de562019-02-15 15:19:15 -0500741 auto direct = fContext->priv().asDirectContext();
742 if (!direct) {
743 return;
744 }
745
746 auto resourceCache = direct->priv().getResourceCache();
Brian Salomon653f42f2018-07-10 10:07:31 -0400747 if (resourceCache && resourceCache->requestsFlush()) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400748 this->flush(nullptr, 0, SkSurface::BackendSurfaceAccess::kNoAccess, GrFlushInfo());
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}