blob: f63c7e81d7fb90e6d0988316f76f0d9781fbc172 [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"
Greg Daniel51316782017-08-02 15:10:09 +00009#include "GrBackendSemaphore.h"
bsalomonb77a9072016-09-07 10:02:04 -070010#include "GrContext.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"
Brian Osman11052242016-10-27 14:47:55 -040016#include "GrRenderTargetContext.h"
Robert Phillipsc7635fa2016-10-28 13:25:24 -040017#include "GrRenderTargetProxy.h"
Robert Phillipsd375dbf2017-09-14 12:45:25 -040018#include "GrResourceAllocator.h"
robertphillips3dc6ae52015-10-20 09:54:32 -070019#include "GrResourceProvider.h"
robertphillips68737822015-10-29 12:12:21 -070020#include "GrSoftwarePathRenderer.h"
Robert Phillips7ee385e2017-03-30 08:02:11 -040021#include "GrSurfaceProxyPriv.h"
Brian Salomon930f9392018-06-20 16:25:26 -040022#include "GrTexture.h"
Brian Osman45580d32016-11-23 09:37:01 -050023#include "GrTextureContext.h"
24#include "GrTextureOpList.h"
Brian Salomon930f9392018-06-20 16:25:26 -040025#include "GrTexturePriv.h"
Chris Dalton706a6ff2017-11-29 22:01:06 -070026#include "GrTextureProxy.h"
27#include "GrTextureProxyPriv.h"
Brian Salomon930f9392018-06-20 16:25:26 -040028#include "GrTracing.h"
Robert Phillips62000362018-02-01 09:10:04 -050029#include "SkDeferredDisplayList.h"
brianosman0e22eb82016-08-30 07:07:59 -070030#include "SkSurface_Gpu.h"
robertphillips3dc6ae52015-10-20 09:54:32 -070031#include "SkTTopoSort.h"
Robert Phillips774168e2018-05-31 12:43:27 -040032#include "ccpr/GrCoverageCountingPathRenderer.h"
Herb Derby26cbe512018-05-24 14:39:01 -040033#include "text/GrTextContext.h"
robertphillips498d7ac2015-10-30 10:11:30 -070034
Robert Phillips22310d62018-09-05 11:07:21 -040035GrDrawingManager::OpListDAG::OpListDAG(bool explicitlyAllocating,
36 GrContextOptions::Enable sortOpLists) {
37 if (GrContextOptions::Enable::kNo == sortOpLists) {
38 fSortOpLists = false;
39 } else if (GrContextOptions::Enable::kYes == sortOpLists) {
40 fSortOpLists = true;
Robert Phillipsa3f70262018-02-08 10:59:38 -050041 } else {
Robert Phillips64ecdce2018-04-02 10:26:39 -040042 // By default we always enable sorting when we're explicitly allocating GPU resources
Robert Phillips22310d62018-09-05 11:07:21 -040043 fSortOpLists = explicitlyAllocating;
Robert Phillipsa3f70262018-02-08 10:59:38 -050044 }
45}
46
Robert Phillips22310d62018-09-05 11:07:21 -040047GrDrawingManager::OpListDAG::~OpListDAG() {}
48
49void GrDrawingManager::OpListDAG::gatherIDs(SkSTArray<8, uint32_t, true>* idArray) const {
50 idArray->reset(fOpLists.count());
Robert Phillipsf2361d22016-10-25 14:20:06 -040051 for (int i = 0; i < fOpLists.count(); ++i) {
Robert Phillips22310d62018-09-05 11:07:21 -040052 if (fOpLists[i]) {
53 (*idArray)[i] = fOpLists[i]->uniqueID();
54 }
55 }
56}
57
58void GrDrawingManager::OpListDAG::reset() {
59 fOpLists.reset();
60}
61
62void GrDrawingManager::OpListDAG::removeOpList(int index) {
63 if (!fOpLists[index]->unique()) {
64 // TODO: Eventually this should be guaranteed unique: http://skbug.com/7111
65 fOpLists[index]->endFlush();
66 }
67
68 fOpLists[index] = nullptr;
69}
70
71void GrDrawingManager::OpListDAG::removeOpLists(int startIndex, int stopIndex) {
72 for (int i = startIndex; i < stopIndex; ++i) {
73 if (!fOpLists[i]) {
74 continue;
75 }
76 this->removeOpList(i);
77 }
78}
79
80void GrDrawingManager::OpListDAG::add(sk_sp<GrOpList> opList) {
81 fOpLists.emplace_back(std::move(opList));
82}
83
84void GrDrawingManager::OpListDAG::add(const SkTArray<sk_sp<GrOpList>>& opLists) {
85 fOpLists.push_back_n(opLists.count(), opLists.begin());
86}
87
88void GrDrawingManager::OpListDAG::swap(SkTArray<sk_sp<GrOpList>>* opLists) {
89 SkASSERT(opLists->empty());
90 opLists->swap(fOpLists);
91}
92
93void GrDrawingManager::OpListDAG::prepForFlush() {
94 if (fSortOpLists) {
95 SkDEBUGCODE(bool result =) SkTTopoSort<GrOpList, GrOpList::TopoSortTraits>(&fOpLists);
96 SkASSERT(result);
97 }
98
99#ifdef SK_DEBUG
100 // This block checks for any unnecessary splits in the opLists. If two sequential opLists
101 // share the same backing GrSurfaceProxy it means the opList was artificially split.
102 if (fOpLists.count()) {
103 GrRenderTargetOpList* prevOpList = fOpLists[0]->asRenderTargetOpList();
104 for (int i = 1; i < fOpLists.count(); ++i) {
105 GrRenderTargetOpList* curOpList = fOpLists[i]->asRenderTargetOpList();
106
107 if (prevOpList && curOpList) {
108 SkASSERT(prevOpList->fTarget.get() != curOpList->fTarget.get());
109 }
110
111 prevOpList = curOpList;
112 }
113 }
114#endif
115}
116
117void GrDrawingManager::OpListDAG::closeAll(const GrCaps* caps) {
118 for (int i = 0; i < fOpLists.count(); ++i) {
119 if (fOpLists[i]) {
120 fOpLists[i]->makeClosed(*caps);
121 }
122 }
123}
124
125void GrDrawingManager::OpListDAG::cleanup(const GrCaps* caps) {
126 for (int i = 0; i < fOpLists.count(); ++i) {
127 if (!fOpLists[i]) {
128 continue;
129 }
130
Robert Phillipsee683652017-04-26 11:53:10 -0400131 // no opList should receive a new command after this
Robert Phillips22310d62018-09-05 11:07:21 -0400132 fOpLists[i]->makeClosed(*caps);
robertphillips0dfa62c2015-11-16 06:23:31 -0800133
Robert Phillipsf2361d22016-10-25 14:20:06 -0400134 // We shouldn't need to do this, but it turns out some clients still hold onto opLists
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400135 // after a cleanup.
136 // MDB TODO: is this still true?
Chris Daltona84cacf2017-10-04 10:30:29 -0600137 if (!fOpLists[i]->unique()) {
138 // TODO: Eventually this should be guaranteed unique.
139 // https://bugs.chromium.org/p/skia/issues/detail?id=7111
140 fOpLists[i]->endFlush();
141 }
robertphillips3dc6ae52015-10-20 09:54:32 -0700142 }
143
Robert Phillipsf2361d22016-10-25 14:20:06 -0400144 fOpLists.reset();
Robert Phillips22310d62018-09-05 11:07:21 -0400145}
146
147///////////////////////////////////////////////////////////////////////////////////////////////////
148GrDrawingManager::GrDrawingManager(GrContext* context,
149 const GrPathRendererChain::Options& optionsForPathRendererChain,
150 const GrTextContext::Options& optionsForTextContext,
151 GrSingleOwner* singleOwner,
152 bool explicitlyAllocating,
Robert Phillips46acf9d2018-10-09 09:31:40 -0400153 GrContextOptions::Enable sortOpLists,
154 GrContextOptions::Enable reduceOpListSplitting)
Robert Phillips22310d62018-09-05 11:07:21 -0400155 : fContext(context)
156 , fOptionsForPathRendererChain(optionsForPathRendererChain)
157 , fOptionsForTextContext(optionsForTextContext)
158 , fSingleOwner(singleOwner)
159 , fAbandoned(false)
160 , fDAG(explicitlyAllocating, sortOpLists)
161 , fTextContext(nullptr)
162 , fPathRendererChain(nullptr)
163 , fSoftwarePathRenderer(nullptr)
164 , fFlushing(false) {
Robert Phillips46acf9d2018-10-09 09:31:40 -0400165 if (GrContextOptions::Enable::kNo == reduceOpListSplitting) {
166 fReduceOpListSplitting = false;
167 } else if (GrContextOptions::Enable::kYes == reduceOpListSplitting) {
168 fReduceOpListSplitting = true;
169 } else {
170 // For now, this is only turned on when explicitly enabled. Once mini-flushes are
171 // implemented it should be enabled whenever sorting is enabled.
172 fReduceOpListSplitting = false; // sortOpLists
173 }
Robert Phillips22310d62018-09-05 11:07:21 -0400174}
175
176void GrDrawingManager::cleanup() {
Robert Phillips9da87e02019-02-04 13:26:26 -0500177 fDAG.cleanup(fContext->priv().caps());
robertphillips3dc6ae52015-10-20 09:54:32 -0700178
robertphillips13391dd2015-10-30 05:15:11 -0700179 fPathRendererChain = nullptr;
Ben Wagner9ec70c62018-07-12 13:30:47 -0400180 fSoftwarePathRenderer = nullptr;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400181
182 fOnFlushCBObjects.reset();
robertphillips3dc6ae52015-10-20 09:54:32 -0700183}
184
185GrDrawingManager::~GrDrawingManager() {
186 this->cleanup();
187}
188
189void GrDrawingManager::abandon() {
190 fAbandoned = true;
191 this->cleanup();
192}
193
robertphillips68737822015-10-29 12:12:21 -0700194void GrDrawingManager::freeGpuResources() {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400195 for (int i = fOnFlushCBObjects.count() - 1; i >= 0; --i) {
196 if (!fOnFlushCBObjects[i]->retainOnFreeGpuResources()) {
197 // it's safe to just do this because we're iterating in reverse
198 fOnFlushCBObjects.removeShuffle(i);
199 }
200 }
201
robertphillips68737822015-10-29 12:12:21 -0700202 // a path renderer may be holding onto resources
robertphillips13391dd2015-10-30 05:15:11 -0700203 fPathRendererChain = nullptr;
Ben Wagner9ec70c62018-07-12 13:30:47 -0400204 fSoftwarePathRenderer = nullptr;
Robert Phillipse3302df2017-04-24 07:31:02 -0400205}
206
Robert Phillips7ee385e2017-03-30 08:02:11 -0400207// MDB TODO: make use of the 'proxy' parameter.
Brian Salomon57d2bea2018-09-10 09:35:41 -0400208GrSemaphoresSubmitted GrDrawingManager::flush(GrSurfaceProxy*,
209 int numSemaphores,
210 GrBackendSemaphore backendSemaphores[]) {
211 GR_CREATE_TRACE_MARKER_CONTEXT("GrDrawingManager", "flush", fContext);
Brian Salomondcbb9d92017-07-19 10:53:20 -0400212
robertphillips7761d612016-05-16 09:14:53 -0700213 if (fFlushing || this->wasAbandoned()) {
Greg Daniel51316782017-08-02 15:10:09 +0000214 return GrSemaphoresSubmitted::kNo;
joshualittb8918c42015-12-18 09:59:46 -0800215 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400216 SkDEBUGCODE(this->validate());
217
Robert Phillips9da87e02019-02-04 13:26:26 -0500218 GrGpu* gpu = fContext->priv().getGpu();
Robert Phillips874b5352018-03-16 08:48:24 -0400219 if (!gpu) {
220 return GrSemaphoresSubmitted::kNo; // Can't flush while DDL recording
221 }
joshualittb8918c42015-12-18 09:59:46 -0800222 fFlushing = true;
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400223
Robert Phillips38d64b02018-09-04 13:23:26 -0400224 // Semi-usually the GrOpLists are already closed at this point, but sometimes Ganesh
225 // needs to flush mid-draw. In that case, the SkGpuDevice's GrOpLists won't be closed
226 // but need to be flushed anyway. Closing such GrOpLists here will mean new
227 // GrOpLists will be created to replace them if the SkGpuDevice(s) write to them again.
Robert Phillips9da87e02019-02-04 13:26:26 -0500228 fDAG.closeAll(fContext->priv().caps());
Robert Phillips38d64b02018-09-04 13:23:26 -0400229 fActiveOpList = nullptr;
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400230
Robert Phillips22310d62018-09-05 11:07:21 -0400231 fDAG.prepForFlush();
Brian Salomon601ac802019-02-07 13:37:16 -0500232 if (!fCpuBufferCache) {
233 // We cache more buffers when the backend is using client side arrays. Otherwise, we
234 // expect each pool will use a CPU buffer as a staging buffer before uploading to a GPU
235 // buffer object. Each pool only requires one staging buffer at a time.
236 int maxCachedBuffers = fContext->priv().caps()->preferClientSideDynamicBuffers() ? 2 : 6;
237 fCpuBufferCache = GrBufferAllocPool::CpuBufferCache::Make(maxCachedBuffers);
Brian Salomon58f153c2018-10-18 21:51:15 -0400238 }
Robert Phillipsa4c93ac2017-05-18 11:40:04 -0400239
Robert Phillips9da87e02019-02-04 13:26:26 -0500240 GrOpFlushState flushState(gpu, fContext->priv().resourceProvider(), &fTokenTracker,
Brian Salomon601ac802019-02-07 13:37:16 -0500241 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 {
Robert Phillips9da87e02019-02-04 13:26:26 -0500288 GrResourceAllocator alloc(fContext->priv().resourceProvider(),
Brian Salomon967df202018-12-07 11:15:53 -0500289 flushState.deinstantiateProxyTracker());
Robert Phillips22310d62018-09-05 11:07:21 -0400290 for (int i = 0; i < fDAG.numOpLists(); ++i) {
291 if (fDAG.opList(i)) {
292 fDAG.opList(i)->gatherProxyIntervals(&alloc);
293 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500294 alloc.markEndOfOpList(i);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500295 }
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400296
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500297 GrResourceAllocator::AssignError error = GrResourceAllocator::AssignError::kNoError;
Greg Danield2073452018-12-07 11:20:33 -0500298 int numOpListsExecuted = 0;
Brian Salomon577aa0f2018-11-30 13:32:23 -0500299 while (alloc.assign(&startIndex, &stopIndex, &error)) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500300 if (GrResourceAllocator::AssignError::kFailedProxyInstantiation == error) {
301 for (int i = startIndex; i < stopIndex; ++i) {
Robert Phillips22310d62018-09-05 11:07:21 -0400302 if (fDAG.opList(i) && !fDAG.opList(i)->isFullyInstantiated()) {
Robert Phillips01a91282018-07-26 08:03:04 -0400303 // If the backing surface wasn't allocated drop the entire opList.
Robert Phillips22310d62018-09-05 11:07:21 -0400304 fDAG.removeOpList(i);
Robert Phillips01a91282018-07-26 08:03:04 -0400305 }
Robert Phillips22310d62018-09-05 11:07:21 -0400306 if (fDAG.opList(i)) {
307 fDAG.opList(i)->purgeOpsWithUninstantiatedProxies();
Robert Phillipsbbcb7f72018-05-31 11:16:19 -0400308 }
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500309 }
310 }
Robert Phillips4150eea2018-02-07 17:08:21 -0500311
Greg Danield2073452018-12-07 11:20:33 -0500312 if (this->executeOpLists(startIndex, stopIndex, &flushState, &numOpListsExecuted)) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500313 flushed = true;
314 }
bsalomondc438982016-08-31 11:53:49 -0700315 }
Greg Danielc42b20b2017-10-04 10:34:49 -0400316 }
317
Chris Dalton91ab1552018-04-18 13:24:25 -0600318#ifdef SK_DEBUG
Robert Phillips22310d62018-09-05 11:07:21 -0400319 for (int i = 0; i < fDAG.numOpLists(); ++i) {
Chris Dalton91ab1552018-04-18 13:24:25 -0600320 // If there are any remaining opLists at this point, make sure they will not survive the
321 // flush. Otherwise we need to call endFlush() on them.
322 // http://skbug.com/7111
Robert Phillips22310d62018-09-05 11:07:21 -0400323 SkASSERT(!fDAG.opList(i) || fDAG.opList(i)->unique());
Chris Dalton91ab1552018-04-18 13:24:25 -0600324 }
325#endif
Robert Phillips22310d62018-09-05 11:07:21 -0400326 fDAG.reset();
robertphillipsa13e2022015-11-11 12:01:09 -0800327
Robert Phillipsc994a932018-06-19 13:09:54 -0400328#ifdef SK_DEBUG
329 // In non-DDL mode this checks that all the flushed ops have been freed from the memory pool.
330 // When we move to partial flushes this assert will no longer be valid.
331 // In DDL mode this check is somewhat superfluous since the memory for most of the ops/opLists
332 // will be stored in the DDL's GrOpMemoryPools.
Robert Phillips9da87e02019-02-04 13:26:26 -0500333 GrOpMemoryPool* opMemoryPool = fContext->priv().opMemoryPool();
Robert Phillipsc994a932018-06-19 13:09:54 -0400334 opMemoryPool->isEmpty();
335#endif
336
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500337 GrSemaphoresSubmitted result = gpu->finishFlush(numSemaphores, backendSemaphores);
robertphillipsa13e2022015-11-11 12:01:09 -0800338
Brian Salomon967df202018-12-07 11:15:53 -0500339 flushState.deinstantiateProxyTracker()->deinstantiateAllProxies();
Greg Daniel4684f822018-03-08 15:27:36 -0500340
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 Phillips9da87e02019-02-04 13:26:26 -0500343 fContext->priv().getResourceCache()->purgeAsNeeded();
bsalomonb77a9072016-09-07 10:02:04 -0700344 }
Chris Daltonfe199b72017-05-05 11:26:15 -0400345 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
Robert Phillips40a29d72018-01-18 12:59:22 -0500346 onFlushCBObject->postFlush(fTokenTracker.nextTokenToFlush(), fFlushingOpListIDs.begin(),
Chris Dalton3968ff92017-11-27 12:26:31 -0700347 fFlushingOpListIDs.count());
Chris Daltonfe199b72017-05-05 11:26:15 -0400348 }
Chris Dalton3968ff92017-11-27 12:26:31 -0700349 fFlushingOpListIDs.reset();
joshualittb8918c42015-12-18 09:59:46 -0800350 fFlushing = false;
Greg Daniel51316782017-08-02 15:10:09 +0000351
352 return result;
robertphillips3dc6ae52015-10-20 09:54:32 -0700353}
354
Greg Danield2073452018-12-07 11:20:33 -0500355bool GrDrawingManager::executeOpLists(int startIndex, int stopIndex, GrOpFlushState* flushState,
356 int* numOpListsExecuted) {
Robert Phillips22310d62018-09-05 11:07:21 -0400357 SkASSERT(startIndex <= stopIndex && stopIndex <= fDAG.numOpLists());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500358
Robert Phillips27483912018-04-20 12:43:18 -0400359#if GR_FLUSH_TIME_OP_SPEW
360 SkDebugf("Flushing opLists: %d to %d out of [%d, %d]\n",
Robert Phillips22310d62018-09-05 11:07:21 -0400361 startIndex, stopIndex, 0, fDAG.numOpLists());
Robert Phillips27483912018-04-20 12:43:18 -0400362 for (int i = startIndex; i < stopIndex; ++i) {
Robert Phillips22310d62018-09-05 11:07:21 -0400363 if (fDAG.opList(i)) {
364 fDAG.opList(i)->dump(true);
Robert Phillips1734dd32018-08-21 13:52:09 -0400365 }
Robert Phillips27483912018-04-20 12:43:18 -0400366 }
367#endif
368
Robert Phillips9da87e02019-02-04 13:26:26 -0500369 GrResourceProvider* resourceProvider = fContext->priv().resourceProvider();
Robert Phillipseafd48a2017-11-16 07:52:08 -0500370 bool anyOpListsExecuted = false;
371
372 for (int i = startIndex; i < stopIndex; ++i) {
Robert Phillips22310d62018-09-05 11:07:21 -0400373 if (!fDAG.opList(i)) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500374 continue;
375 }
376
Robert Phillips22310d62018-09-05 11:07:21 -0400377 GrOpList* opList = fDAG.opList(i);
378
Robert Phillips4150eea2018-02-07 17:08:21 -0500379 if (resourceProvider->explicitlyAllocateGPUResources()) {
Robert Phillips22310d62018-09-05 11:07:21 -0400380 if (!opList->isFullyInstantiated()) {
Robert Phillips4150eea2018-02-07 17:08:21 -0500381 // If the backing surface wasn't allocated drop the draw of the entire opList.
Robert Phillips22310d62018-09-05 11:07:21 -0400382 fDAG.removeOpList(i);
Robert Phillips4150eea2018-02-07 17:08:21 -0500383 continue;
384 }
385 } else {
Robert Phillips22310d62018-09-05 11:07:21 -0400386 if (!opList->instantiate(resourceProvider)) {
387 fDAG.removeOpList(i);
Robert Phillips4150eea2018-02-07 17:08:21 -0500388 continue;
389 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500390 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500391
392 // TODO: handle this instantiation via lazy surface proxies?
393 // Instantiate all deferred proxies (being built on worker threads) so we can upload them
Robert Phillips9da87e02019-02-04 13:26:26 -0500394 opList->instantiateDeferredProxies(fContext->priv().resourceProvider());
Robert Phillips22310d62018-09-05 11:07:21 -0400395 opList->prepare(flushState);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500396 }
397
398 // Upload all data to the GPU
399 flushState->preExecuteDraws();
400
Greg Danield2073452018-12-07 11:20:33 -0500401 // For Vulkan, if we have too many oplists to be flushed we end up allocating a lot of resources
402 // for each command buffer associated with the oplists. If this gets too large we can cause the
403 // devices to go OOM. In practice we usually only hit this case in our tests, but to be safe we
404 // put a cap on the number of oplists we will execute before flushing to the GPU to relieve some
405 // memory pressure.
406 static constexpr int kMaxOpListsBeforeFlush = 100;
407
Robert Phillipseafd48a2017-11-16 07:52:08 -0500408 // Execute the onFlush op lists first, if any.
Chris Dalton3968ff92017-11-27 12:26:31 -0700409 for (sk_sp<GrOpList>& onFlushOpList : fOnFlushCBOpLists) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500410 if (!onFlushOpList->execute(flushState)) {
411 SkDebugf("WARNING: onFlushOpList failed to execute.\n");
412 }
413 SkASSERT(onFlushOpList->unique());
414 onFlushOpList = nullptr;
Greg Danield2073452018-12-07 11:20:33 -0500415 (*numOpListsExecuted)++;
416 if (*numOpListsExecuted >= kMaxOpListsBeforeFlush) {
417 flushState->gpu()->finishFlush(0, nullptr);
418 *numOpListsExecuted = 0;
419 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500420 }
Chris Dalton3968ff92017-11-27 12:26:31 -0700421 fOnFlushCBOpLists.reset();
Robert Phillipseafd48a2017-11-16 07:52:08 -0500422
423 // Execute the normal op lists.
424 for (int i = startIndex; i < stopIndex; ++i) {
Robert Phillips22310d62018-09-05 11:07:21 -0400425 if (!fDAG.opList(i)) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500426 continue;
427 }
428
Robert Phillips22310d62018-09-05 11:07:21 -0400429 if (fDAG.opList(i)->execute(flushState)) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500430 anyOpListsExecuted = true;
431 }
Greg Danield2073452018-12-07 11:20:33 -0500432 (*numOpListsExecuted)++;
433 if (*numOpListsExecuted >= kMaxOpListsBeforeFlush) {
434 flushState->gpu()->finishFlush(0, nullptr);
435 *numOpListsExecuted = 0;
436 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500437 }
438
439 SkASSERT(!flushState->commandBuffer());
Robert Phillips40a29d72018-01-18 12:59:22 -0500440 SkASSERT(fTokenTracker.nextDrawToken() == fTokenTracker.nextTokenToFlush());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500441
442 // We reset the flush state before the OpLists so that the last resources to be freed are those
443 // that are written to in the OpLists. This helps to make sure the most recently used resources
444 // are the last to be purged by the resource cache.
445 flushState->reset();
446
Robert Phillips22310d62018-09-05 11:07:21 -0400447 fDAG.removeOpLists(startIndex, stopIndex);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500448
449 return anyOpListsExecuted;
450}
451
Greg Daniel51316782017-08-02 15:10:09 +0000452GrSemaphoresSubmitted GrDrawingManager::prepareSurfaceForExternalIO(
453 GrSurfaceProxy* proxy, int numSemaphores, GrBackendSemaphore backendSemaphores[]) {
bsalomon6a2b1942016-09-08 11:28:59 -0700454 if (this->wasAbandoned()) {
Greg Daniel51316782017-08-02 15:10:09 +0000455 return GrSemaphoresSubmitted::kNo;
bsalomon6a2b1942016-09-08 11:28:59 -0700456 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400457 SkDEBUGCODE(this->validate());
Robert Phillips7ee385e2017-03-30 08:02:11 -0400458 SkASSERT(proxy);
bsalomon6a2b1942016-09-08 11:28:59 -0700459
Robert Phillips9da87e02019-02-04 13:26:26 -0500460 GrGpu* gpu = fContext->priv().getGpu();
Robert Phillips874b5352018-03-16 08:48:24 -0400461 if (!gpu) {
462 return GrSemaphoresSubmitted::kNo; // Can't flush while DDL recording
463 }
464
Kevin Lubick42846132018-01-05 10:11:11 -0500465 GrSemaphoresSubmitted result = GrSemaphoresSubmitted::kNo;
Greg Daniel51316782017-08-02 15:10:09 +0000466 if (proxy->priv().hasPendingIO() || numSemaphores) {
467 result = this->flush(proxy, numSemaphores, backendSemaphores);
bsalomon6a2b1942016-09-08 11:28:59 -0700468 }
469
Robert Phillips9da87e02019-02-04 13:26:26 -0500470 if (!proxy->instantiate(fContext->priv().resourceProvider())) {
Greg Daniel51316782017-08-02 15:10:09 +0000471 return result;
Robert Phillips7ee385e2017-03-30 08:02:11 -0400472 }
473
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400474 GrSurface* surface = proxy->peekSurface();
Brian Salomon930f9392018-06-20 16:25:26 -0400475 if (auto* rt = surface->asRenderTarget()) {
476 gpu->resolveRenderTarget(rt);
477 }
478 if (auto* tex = surface->asTexture()) {
479 if (tex->texturePriv().mipMapped() == GrMipMapped::kYes &&
480 tex->texturePriv().mipMapsAreDirty()) {
481 gpu->regenerateMipMapLevels(tex);
482 }
bsalomon6a2b1942016-09-08 11:28:59 -0700483 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400484
485 SkDEBUGCODE(this->validate());
Greg Daniel51316782017-08-02 15:10:09 +0000486 return result;
bsalomon6a2b1942016-09-08 11:28:59 -0700487}
488
Chris Daltonfe199b72017-05-05 11:26:15 -0400489void GrDrawingManager::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
490 fOnFlushCBObjects.push_back(onFlushCBObject);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400491}
492
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500493#if GR_TEST_UTILS
494void GrDrawingManager::testingOnly_removeOnFlushCallbackObject(GrOnFlushCallbackObject* cb) {
495 int n = std::find(fOnFlushCBObjects.begin(), fOnFlushCBObjects.end(), cb) -
496 fOnFlushCBObjects.begin();
497 SkASSERT(n < fOnFlushCBObjects.count());
498 fOnFlushCBObjects.removeShuffle(n);
499}
500#endif
501
Robert Phillips62000362018-02-01 09:10:04 -0500502void GrDrawingManager::moveOpListsToDDL(SkDeferredDisplayList* ddl) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400503 SkDEBUGCODE(this->validate());
504
505 // no opList should receive a new command after this
Robert Phillips9da87e02019-02-04 13:26:26 -0500506 fDAG.closeAll(fContext->priv().caps());
Robert Phillips38d64b02018-09-04 13:23:26 -0400507 fActiveOpList = nullptr;
Robert Phillips7a137052018-02-01 11:23:12 -0500508
Robert Phillips22310d62018-09-05 11:07:21 -0400509 fDAG.swap(&ddl->fOpLists);
Robert Phillips867ce8f2018-06-21 10:28:36 -0400510
Robert Phillips774168e2018-05-31 12:43:27 -0400511 if (fPathRendererChain) {
512 if (auto ccpr = fPathRendererChain->getCoverageCountingPathRenderer()) {
513 ddl->fPendingPaths = ccpr->detachPendingPaths();
514 }
515 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400516
517 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500518}
519
520void GrDrawingManager::copyOpListsFromDDL(const SkDeferredDisplayList* ddl,
521 GrRenderTargetProxy* newDest) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400522 SkDEBUGCODE(this->validate());
523
524 if (fActiveOpList) {
525 // This is a temporary fix for the partial-MDB world. In that world we're not
526 // reordering so ops that (in the single opList world) would've just glommed onto the
527 // end of the single opList but referred to a far earlier RT need to appear in their
528 // own opList.
Robert Phillips9da87e02019-02-04 13:26:26 -0500529 fActiveOpList->makeClosed(*fContext->priv().caps());
Robert Phillips38d64b02018-09-04 13:23:26 -0400530 fActiveOpList = nullptr;
531 }
532
Robert Phillips62000362018-02-01 09:10:04 -0500533 // Here we jam the proxy that backs the current replay SkSurface into the LazyProxyData.
534 // The lazy proxy that references it (in the copied opLists) will steal its GrTexture.
535 ddl->fLazyProxyData->fReplayDest = newDest;
Robert Phillips774168e2018-05-31 12:43:27 -0400536
537 if (ddl->fPendingPaths.size()) {
538 GrCoverageCountingPathRenderer* ccpr = this->getCoverageCountingPathRenderer();
539
540 ccpr->mergePendingPaths(ddl->fPendingPaths);
541 }
Robert Phillips22310d62018-09-05 11:07:21 -0400542
543 fDAG.add(ddl->fOpLists);
Robert Phillips38d64b02018-09-04 13:23:26 -0400544
545 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500546}
547
Robert Phillips38d64b02018-09-04 13:23:26 -0400548#ifdef SK_DEBUG
549void GrDrawingManager::validate() const {
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400550 if (fDAG.sortingOpLists() && fReduceOpListSplitting) {
551 SkASSERT(!fActiveOpList);
552 } else {
553 if (fActiveOpList) {
554 SkASSERT(!fDAG.empty());
555 SkASSERT(!fActiveOpList->isClosed());
556 SkASSERT(fActiveOpList == fDAG.back());
Robert Phillips38d64b02018-09-04 13:23:26 -0400557 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400558
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400559 for (int i = 0; i < fDAG.numOpLists(); ++i) {
560 if (fActiveOpList != fDAG.opList(i)) {
561 SkASSERT(fDAG.opList(i)->isClosed());
562 }
563 }
564
565 if (!fDAG.empty() && !fDAG.back()->isClosed()) {
566 SkASSERT(fActiveOpList == fDAG.back());
567 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400568 }
569}
570#endif
571
Robert Phillips941d1442017-06-14 16:37:02 -0400572sk_sp<GrRenderTargetOpList> GrDrawingManager::newRTOpList(GrRenderTargetProxy* rtp,
573 bool managedOpList) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400574 SkDEBUGCODE(this->validate());
robertphillips3dc6ae52015-10-20 09:54:32 -0700575 SkASSERT(fContext);
576
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400577 if (fDAG.sortingOpLists() && fReduceOpListSplitting) {
578 // In this case we need to close all the opLists that rely on the current contents of
579 // 'rtp'. That is bc we're going to update the content of the proxy so they need to be
580 // split in case they use both the old and new content. (This is a bit of an overkill:
581 // they really only need to be split if they ever reference proxy's contents again but
582 // that is hard to predict/handle).
583 if (GrOpList* lastOpList = rtp->getLastOpList()) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500584 lastOpList->closeThoseWhoDependOnMe(*fContext->priv().caps());
Robert Phillips46acf9d2018-10-09 09:31:40 -0400585 }
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400586 } else if (fActiveOpList) {
587 // This is a temporary fix for the partial-MDB world. In that world we're not
588 // reordering so ops that (in the single opList world) would've just glommed onto the
589 // end of the single opList but referred to a far earlier RT need to appear in their
590 // own opList.
Robert Phillips9da87e02019-02-04 13:26:26 -0500591 fActiveOpList->makeClosed(*fContext->priv().caps());
Robert Phillips38d64b02018-09-04 13:23:26 -0400592 fActiveOpList = nullptr;
robertphillips3dc6ae52015-10-20 09:54:32 -0700593 }
robertphillips3dc6ae52015-10-20 09:54:32 -0700594
Robert Phillips9da87e02019-02-04 13:26:26 -0500595 auto resourceProvider = fContext->priv().resourceProvider();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500596
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500597 sk_sp<GrRenderTargetOpList> opList(new GrRenderTargetOpList(
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500598 resourceProvider,
Robert Phillips9da87e02019-02-04 13:26:26 -0500599 fContext->priv().refOpMemoryPool(),
Robert Phillips3a9710b2018-03-27 17:51:55 -0400600 rtp,
Robert Phillipsd6841482019-02-08 10:29:20 -0500601 fContext->priv().auditTrail()));
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400602 SkASSERT(rtp->getLastOpList() == opList.get());
robertphillips3dc6ae52015-10-20 09:54:32 -0700603
Robert Phillips941d1442017-06-14 16:37:02 -0400604 if (managedOpList) {
Robert Phillips22310d62018-09-05 11:07:21 -0400605 fDAG.add(opList);
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400606
607 if (!fDAG.sortingOpLists() || !fReduceOpListSplitting) {
608 fActiveOpList = opList.get();
609 }
Robert Phillips941d1442017-06-14 16:37:02 -0400610 }
robertphillips3dc6ae52015-10-20 09:54:32 -0700611
Robert Phillips38d64b02018-09-04 13:23:26 -0400612 SkDEBUGCODE(this->validate());
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400613 return opList;
robertphillips3dc6ae52015-10-20 09:54:32 -0700614}
615
Robert Phillipsb6deea82017-05-11 14:14:30 -0400616sk_sp<GrTextureOpList> GrDrawingManager::newTextureOpList(GrTextureProxy* textureProxy) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400617 SkDEBUGCODE(this->validate());
Brian Osman45580d32016-11-23 09:37:01 -0500618 SkASSERT(fContext);
619
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400620 if (fDAG.sortingOpLists() && fReduceOpListSplitting) {
621 // In this case we need to close all the opLists that rely on the current contents of
622 // 'texture'. That is bc we're going to update the content of the proxy so they need to
623 // be split in case they use both the old and new content. (This is a bit of an
624 // overkill: they really only need to be split if they ever reference proxy's contents
625 // again but that is hard to predict/handle).
626 if (GrOpList* lastOpList = textureProxy->getLastOpList()) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500627 lastOpList->closeThoseWhoDependOnMe(*fContext->priv().caps());
Robert Phillips46acf9d2018-10-09 09:31:40 -0400628 }
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400629 } else if (fActiveOpList) {
630 // This is a temporary fix for the partial-MDB world. In that world we're not
631 // reordering so ops that (in the single opList world) would've just glommed onto the
632 // end of the single opList but referred to a far earlier RT need to appear in their
633 // own opList.
Robert Phillips9da87e02019-02-04 13:26:26 -0500634 fActiveOpList->makeClosed(*fContext->priv().caps());
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400635 fActiveOpList = nullptr;
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400636 }
637
Robert Phillips9da87e02019-02-04 13:26:26 -0500638 sk_sp<GrTextureOpList> opList(new GrTextureOpList(fContext->priv().resourceProvider(),
639 fContext->priv().refOpMemoryPool(),
Robert Phillips5efd5ea2017-05-30 13:47:32 -0400640 textureProxy,
Robert Phillipsd6841482019-02-08 10:29:20 -0500641 fContext->priv().auditTrail()));
Brian Osman45580d32016-11-23 09:37:01 -0500642
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400643 SkASSERT(textureProxy->getLastOpList() == opList.get());
Robert Phillips4a395042017-04-24 16:27:17 +0000644
Robert Phillips22310d62018-09-05 11:07:21 -0400645 fDAG.add(opList);
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400646 if (!fDAG.sortingOpLists() || !fReduceOpListSplitting) {
647 fActiveOpList = opList.get();
648 }
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400649
Robert Phillips38d64b02018-09-04 13:23:26 -0400650 SkDEBUGCODE(this->validate());
Robert Phillips4a395042017-04-24 16:27:17 +0000651 return opList;
Brian Osman45580d32016-11-23 09:37:01 -0500652}
653
Herb Derby26cbe512018-05-24 14:39:01 -0400654GrTextContext* GrDrawingManager::getTextContext() {
655 if (!fTextContext) {
656 fTextContext = GrTextContext::Make(fOptionsForTextContext);
brianosman86e76262016-08-11 12:17:31 -0700657 }
658
Herb Derby26cbe512018-05-24 14:39:01 -0400659 return fTextContext.get();
brianosman86e76262016-08-11 12:17:31 -0700660}
661
robertphillips68737822015-10-29 12:12:21 -0700662/*
663 * This method finds a path renderer that can draw the specified path on
664 * the provided target.
665 * Due to its expense, the software path renderer has split out so it can
666 * can be individually allowed/disallowed via the "allowSW" boolean.
667 */
668GrPathRenderer* GrDrawingManager::getPathRenderer(const GrPathRenderer::CanDrawPathArgs& args,
669 bool allowSW,
670 GrPathRendererChain::DrawType drawType,
671 GrPathRenderer::StencilSupport* stencilSupport) {
672
673 if (!fPathRendererChain) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400674 fPathRendererChain.reset(new GrPathRendererChain(fContext, fOptionsForPathRendererChain));
robertphillips68737822015-10-29 12:12:21 -0700675 }
676
677 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(args, drawType, stencilSupport);
678 if (!pr && allowSW) {
Brian Salomone7df0bb2018-05-07 14:44:57 -0400679 auto swPR = this->getSoftwarePathRenderer();
680 if (GrPathRenderer::CanDrawPath::kNo != swPR->canDrawPath(args)) {
681 pr = swPR;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500682 }
robertphillips68737822015-10-29 12:12:21 -0700683 }
684
685 return pr;
686}
687
Brian Salomone7df0bb2018-05-07 14:44:57 -0400688GrPathRenderer* GrDrawingManager::getSoftwarePathRenderer() {
689 if (!fSoftwarePathRenderer) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400690 fSoftwarePathRenderer.reset(
Robert Phillips9da87e02019-02-04 13:26:26 -0500691 new GrSoftwarePathRenderer(fContext->priv().proxyProvider(),
Ben Wagner9ec70c62018-07-12 13:30:47 -0400692 fOptionsForPathRendererChain.fAllowPathMaskCaching));
Brian Salomone7df0bb2018-05-07 14:44:57 -0400693 }
Ben Wagner9ec70c62018-07-12 13:30:47 -0400694 return fSoftwarePathRenderer.get();
Brian Salomone7df0bb2018-05-07 14:44:57 -0400695}
696
Chris Daltonfddb6c02017-11-04 15:22:22 -0600697GrCoverageCountingPathRenderer* GrDrawingManager::getCoverageCountingPathRenderer() {
698 if (!fPathRendererChain) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400699 fPathRendererChain.reset(new GrPathRendererChain(fContext, fOptionsForPathRendererChain));
Chris Daltonfddb6c02017-11-04 15:22:22 -0600700 }
701 return fPathRendererChain->getCoverageCountingPathRenderer();
702}
703
Brian Salomon653f42f2018-07-10 10:07:31 -0400704void GrDrawingManager::flushIfNecessary() {
Robert Phillips9da87e02019-02-04 13:26:26 -0500705 GrResourceCache* resourceCache = fContext->priv().getResourceCache();
Brian Salomon653f42f2018-07-10 10:07:31 -0400706 if (resourceCache && resourceCache->requestsFlush()) {
Brian Salomon57d2bea2018-09-10 09:35:41 -0400707 this->flush(nullptr, 0, nullptr);
708 resourceCache->purgeAsNeeded();
Brian Salomon653f42f2018-07-10 10:07:31 -0400709 }
710}
711
Brian Osman11052242016-10-27 14:47:55 -0400712sk_sp<GrRenderTargetContext> GrDrawingManager::makeRenderTargetContext(
Robert Phillips37430132016-11-09 06:50:43 -0500713 sk_sp<GrSurfaceProxy> sProxy,
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400714 sk_sp<SkColorSpace> colorSpace,
Robert Phillips941d1442017-06-14 16:37:02 -0400715 const SkSurfaceProps* surfaceProps,
716 bool managedOpList) {
Robert Phillips37430132016-11-09 06:50:43 -0500717 if (this->wasAbandoned() || !sProxy->asRenderTargetProxy()) {
robertphillips3dc6ae52015-10-20 09:54:32 -0700718 return nullptr;
719 }
720
brianosman0e22eb82016-08-30 07:07:59 -0700721 // SkSurface catches bad color space usage at creation. This check handles anything that slips
Brian Salomon366093f2018-02-13 09:25:22 -0500722 // by, including internal usage.
Robert Phillips9da87e02019-02-04 13:26:26 -0500723 if (!SkSurface_Gpu::Valid(fContext->priv().caps(), sProxy->config(), colorSpace.get())) {
brianosmana9c3c6a2016-09-29 10:08:36 -0700724 SkDEBUGFAIL("Invalid config and colorspace combination");
brianosman0e22eb82016-08-30 07:07:59 -0700725 return nullptr;
726 }
joshualitt96880d92016-02-16 10:36:53 -0800727
Robert Phillips2c862492017-01-18 10:08:39 -0500728 sk_sp<GrRenderTargetProxy> rtp(sk_ref_sp(sProxy->asRenderTargetProxy()));
729
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500730 return sk_sp<GrRenderTargetContext>(new GrRenderTargetContext(
731 fContext, this, std::move(rtp),
732 std::move(colorSpace),
733 surfaceProps,
Robert Phillipsd6841482019-02-08 10:29:20 -0500734 fContext->priv().auditTrail(),
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500735 fSingleOwner, managedOpList));
robertphillips3dc6ae52015-10-20 09:54:32 -0700736}
Brian Osman45580d32016-11-23 09:37:01 -0500737
Robert Phillips2c862492017-01-18 10:08:39 -0500738sk_sp<GrTextureContext> GrDrawingManager::makeTextureContext(sk_sp<GrSurfaceProxy> sProxy,
739 sk_sp<SkColorSpace> colorSpace) {
Brian Osman45580d32016-11-23 09:37:01 -0500740 if (this->wasAbandoned() || !sProxy->asTextureProxy()) {
741 return nullptr;
742 }
743
Robert Phillips2c862492017-01-18 10:08:39 -0500744 // SkSurface catches bad color space usage at creation. This check handles anything that slips
Brian Salomon366093f2018-02-13 09:25:22 -0500745 // by, including internal usage.
Robert Phillips9da87e02019-02-04 13:26:26 -0500746 if (!SkSurface_Gpu::Valid(fContext->priv().caps(), sProxy->config(), colorSpace.get())) {
Robert Phillips2c862492017-01-18 10:08:39 -0500747 SkDEBUGFAIL("Invalid config and colorspace combination");
748 return nullptr;
749 }
750
Robert Phillips383c4182018-02-07 12:47:44 -0500751 // GrTextureRenderTargets should always be using a GrRenderTargetContext
Brian Osman45580d32016-11-23 09:37:01 -0500752 SkASSERT(!sProxy->asRenderTargetProxy());
753
754 sk_sp<GrTextureProxy> textureProxy(sk_ref_sp(sProxy->asTextureProxy()));
755
756 return sk_sp<GrTextureContext>(new GrTextureContext(fContext, this, std::move(textureProxy),
Robert Phillips2c862492017-01-18 10:08:39 -0500757 std::move(colorSpace),
Robert Phillipsd6841482019-02-08 10:29:20 -0500758 fContext->priv().auditTrail(),
Robert Phillips2c862492017-01-18 10:08:39 -0500759 fSingleOwner));
Brian Osman45580d32016-11-23 09:37:01 -0500760}