blob: 9db2864247840600a792b7dcc650e6f4c830b9ab [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)
Robert Phillips22310d62018-09-05 11:07:21 -0400159 , fDAG(explicitlyAllocating, sortOpLists)
160 , fTextContext(nullptr)
161 , fPathRendererChain(nullptr)
162 , fSoftwarePathRenderer(nullptr)
163 , fFlushing(false) {
Robert Phillips46acf9d2018-10-09 09:31:40 -0400164 if (GrContextOptions::Enable::kNo == reduceOpListSplitting) {
165 fReduceOpListSplitting = false;
166 } else if (GrContextOptions::Enable::kYes == reduceOpListSplitting) {
167 fReduceOpListSplitting = true;
168 } else {
169 // For now, this is only turned on when explicitly enabled. Once mini-flushes are
170 // implemented it should be enabled whenever sorting is enabled.
171 fReduceOpListSplitting = false; // sortOpLists
172 }
Robert Phillips22310d62018-09-05 11:07:21 -0400173}
174
175void GrDrawingManager::cleanup() {
Robert Phillips9da87e02019-02-04 13:26:26 -0500176 fDAG.cleanup(fContext->priv().caps());
robertphillips3dc6ae52015-10-20 09:54:32 -0700177
robertphillips13391dd2015-10-30 05:15:11 -0700178 fPathRendererChain = nullptr;
Ben Wagner9ec70c62018-07-12 13:30:47 -0400179 fSoftwarePathRenderer = nullptr;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400180
181 fOnFlushCBObjects.reset();
robertphillips3dc6ae52015-10-20 09:54:32 -0700182}
183
184GrDrawingManager::~GrDrawingManager() {
185 this->cleanup();
186}
187
Robert Phillipsa9162df2019-02-11 14:12:03 -0500188bool GrDrawingManager::wasAbandoned() const {
189 return fContext->abandoned();
robertphillips3dc6ae52015-10-20 09:54:32 -0700190}
191
robertphillips68737822015-10-29 12:12:21 -0700192void GrDrawingManager::freeGpuResources() {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400193 for (int i = fOnFlushCBObjects.count() - 1; i >= 0; --i) {
194 if (!fOnFlushCBObjects[i]->retainOnFreeGpuResources()) {
195 // it's safe to just do this because we're iterating in reverse
196 fOnFlushCBObjects.removeShuffle(i);
197 }
198 }
199
robertphillips68737822015-10-29 12:12:21 -0700200 // a path renderer may be holding onto resources
robertphillips13391dd2015-10-30 05:15:11 -0700201 fPathRendererChain = nullptr;
Ben Wagner9ec70c62018-07-12 13:30:47 -0400202 fSoftwarePathRenderer = nullptr;
Robert Phillipse3302df2017-04-24 07:31:02 -0400203}
204
Robert Phillips7ee385e2017-03-30 08:02:11 -0400205// MDB TODO: make use of the 'proxy' parameter.
Brian Salomon57d2beab2018-09-10 09:35:41 -0400206GrSemaphoresSubmitted GrDrawingManager::flush(GrSurfaceProxy*,
207 int numSemaphores,
208 GrBackendSemaphore backendSemaphores[]) {
209 GR_CREATE_TRACE_MARKER_CONTEXT("GrDrawingManager", "flush", fContext);
Brian Salomondcbb9d92017-07-19 10:53:20 -0400210
robertphillips7761d612016-05-16 09:14:53 -0700211 if (fFlushing || this->wasAbandoned()) {
Greg Daniel51316782017-08-02 15:10:09 +0000212 return GrSemaphoresSubmitted::kNo;
joshualittb8918c42015-12-18 09:59:46 -0800213 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400214 SkDEBUGCODE(this->validate());
215
Robert Phillips9da87e02019-02-04 13:26:26 -0500216 GrGpu* gpu = fContext->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 Phillips38d64b02018-09-04 13:23:26 -0400222 // Semi-usually the GrOpLists are already closed at this point, but sometimes Ganesh
223 // needs to flush mid-draw. In that case, the SkGpuDevice's GrOpLists won't be closed
224 // but need to be flushed anyway. Closing such GrOpLists here will mean new
225 // GrOpLists will be created to replace them if the SkGpuDevice(s) write to them again.
Robert Phillips9da87e02019-02-04 13:26:26 -0500226 fDAG.closeAll(fContext->priv().caps());
Robert Phillips38d64b02018-09-04 13:23:26 -0400227 fActiveOpList = nullptr;
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400228
Robert Phillips22310d62018-09-05 11:07:21 -0400229 fDAG.prepForFlush();
Brian Salomon601ac802019-02-07 13:37:16 -0500230 if (!fCpuBufferCache) {
231 // We cache more buffers when the backend is using client side arrays. Otherwise, we
232 // expect each pool will use a CPU buffer as a staging buffer before uploading to a GPU
233 // buffer object. Each pool only requires one staging buffer at a time.
234 int maxCachedBuffers = fContext->priv().caps()->preferClientSideDynamicBuffers() ? 2 : 6;
235 fCpuBufferCache = GrBufferAllocPool::CpuBufferCache::Make(maxCachedBuffers);
Brian Salomon58f153c2018-10-18 21:51:15 -0400236 }
Robert Phillipsa4c93ac2017-05-18 11:40:04 -0400237
Robert Phillips9da87e02019-02-04 13:26:26 -0500238 GrOpFlushState flushState(gpu, fContext->priv().resourceProvider(), &fTokenTracker,
Brian Salomon601ac802019-02-07 13:37:16 -0500239 fCpuBufferCache);
Robert Phillips40a29d72018-01-18 12:59:22 -0500240
Chris Daltonfe199b72017-05-05 11:26:15 -0400241 GrOnFlushResourceProvider onFlushProvider(this);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500242 // TODO: AFAICT the only reason fFlushState is on GrDrawingManager rather than on the
243 // stack here is to preserve the flush tokens.
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400244
Chris Dalton12658942017-10-05 19:45:25 -0600245 // Prepare any onFlush op lists (e.g. atlases).
Chris Daltonfe199b72017-05-05 11:26:15 -0400246 if (!fOnFlushCBObjects.empty()) {
Robert Phillips22310d62018-09-05 11:07:21 -0400247 fDAG.gatherIDs(&fFlushingOpListIDs);
248
Chris Dalton12658942017-10-05 19:45:25 -0600249 SkSTArray<4, sk_sp<GrRenderTargetContext>> renderTargetContexts;
Chris Daltonfe199b72017-05-05 11:26:15 -0400250 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
251 onFlushCBObject->preFlush(&onFlushProvider,
Chris Dalton3968ff92017-11-27 12:26:31 -0700252 fFlushingOpListIDs.begin(), fFlushingOpListIDs.count(),
Chris Daltonfe199b72017-05-05 11:26:15 -0400253 &renderTargetContexts);
Chris Dalton12658942017-10-05 19:45:25 -0600254 for (const sk_sp<GrRenderTargetContext>& rtc : renderTargetContexts) {
Chris Dalton706a6ff2017-11-29 22:01:06 -0700255 sk_sp<GrRenderTargetOpList> onFlushOpList = sk_ref_sp(rtc->getRTOpList());
Chris Dalton12658942017-10-05 19:45:25 -0600256 if (!onFlushOpList) {
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400257 continue; // Odd - but not a big deal
258 }
Chris Dalton706a6ff2017-11-29 22:01:06 -0700259#ifdef SK_DEBUG
260 // OnFlush callbacks are already invoked during flush, and are therefore expected to
261 // handle resource allocation & usage on their own. (No deferred or lazy proxies!)
262 onFlushOpList->visitProxies_debugOnly([](GrSurfaceProxy* p) {
263 SkASSERT(!p->asTextureProxy() || !p->asTextureProxy()->texPriv().isDeferred());
Greg Daniel65fa8ca2018-01-10 17:06:31 -0500264 SkASSERT(GrSurfaceProxy::LazyState::kNot == p->lazyInstantiationState());
Chris Dalton706a6ff2017-11-29 22:01:06 -0700265 });
266#endif
Robert Phillips9da87e02019-02-04 13:26:26 -0500267 onFlushOpList->makeClosed(*fContext->priv().caps());
Robert Phillips40a29d72018-01-18 12:59:22 -0500268 onFlushOpList->prepare(&flushState);
Chris Dalton3968ff92017-11-27 12:26:31 -0700269 fOnFlushCBOpLists.push_back(std::move(onFlushOpList));
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400270 }
271 renderTargetContexts.reset();
272 }
273 }
274
robertphillipsa13e2022015-11-11 12:01:09 -0800275#if 0
Brian Salomon09d994e2016-12-21 11:14:46 -0500276 // Enable this to print out verbose GrOp information
Robert Phillipsf2361d22016-10-25 14:20:06 -0400277 for (int i = 0; i < fOpLists.count(); ++i) {
278 SkDEBUGCODE(fOpLists[i]->dump();)
robertphillips3dc6ae52015-10-20 09:54:32 -0700279 }
robertphillipsa13e2022015-11-11 12:01:09 -0800280#endif
281
Robert Phillipseafd48a2017-11-16 07:52:08 -0500282 int startIndex, stopIndex;
283 bool flushed = false;
284
Robert Phillipsf8e25022017-11-08 15:24:31 -0500285 {
Robert Phillips9da87e02019-02-04 13:26:26 -0500286 GrResourceAllocator alloc(fContext->priv().resourceProvider(),
Brian Salomon967df202018-12-07 11:15:53 -0500287 flushState.deinstantiateProxyTracker());
Robert Phillips22310d62018-09-05 11:07:21 -0400288 for (int i = 0; i < fDAG.numOpLists(); ++i) {
289 if (fDAG.opList(i)) {
290 fDAG.opList(i)->gatherProxyIntervals(&alloc);
291 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500292 alloc.markEndOfOpList(i);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500293 }
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400294
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500295 GrResourceAllocator::AssignError error = GrResourceAllocator::AssignError::kNoError;
Greg Danield2073452018-12-07 11:20:33 -0500296 int numOpListsExecuted = 0;
Brian Salomon577aa0f2018-11-30 13:32:23 -0500297 while (alloc.assign(&startIndex, &stopIndex, &error)) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500298 if (GrResourceAllocator::AssignError::kFailedProxyInstantiation == error) {
299 for (int i = startIndex; i < stopIndex; ++i) {
Robert Phillips22310d62018-09-05 11:07:21 -0400300 if (fDAG.opList(i) && !fDAG.opList(i)->isFullyInstantiated()) {
Robert Phillips01a91282018-07-26 08:03:04 -0400301 // If the backing surface wasn't allocated drop the entire opList.
Robert Phillips22310d62018-09-05 11:07:21 -0400302 fDAG.removeOpList(i);
Robert Phillips01a91282018-07-26 08:03:04 -0400303 }
Robert Phillips22310d62018-09-05 11:07:21 -0400304 if (fDAG.opList(i)) {
305 fDAG.opList(i)->purgeOpsWithUninstantiatedProxies();
Robert Phillipsbbcb7f72018-05-31 11:16:19 -0400306 }
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500307 }
308 }
Robert Phillips4150eea2018-02-07 17:08:21 -0500309
Greg Danield2073452018-12-07 11:20:33 -0500310 if (this->executeOpLists(startIndex, stopIndex, &flushState, &numOpListsExecuted)) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500311 flushed = true;
312 }
bsalomondc438982016-08-31 11:53:49 -0700313 }
Greg Danielc42b20b2017-10-04 10:34:49 -0400314 }
315
Chris Dalton91ab1552018-04-18 13:24:25 -0600316#ifdef SK_DEBUG
Robert Phillips22310d62018-09-05 11:07:21 -0400317 for (int i = 0; i < fDAG.numOpLists(); ++i) {
Chris Dalton91ab1552018-04-18 13:24:25 -0600318 // If there are any remaining opLists at this point, make sure they will not survive the
319 // flush. Otherwise we need to call endFlush() on them.
320 // http://skbug.com/7111
Robert Phillips22310d62018-09-05 11:07:21 -0400321 SkASSERT(!fDAG.opList(i) || fDAG.opList(i)->unique());
Chris Dalton91ab1552018-04-18 13:24:25 -0600322 }
323#endif
Robert Phillips22310d62018-09-05 11:07:21 -0400324 fDAG.reset();
robertphillipsa13e2022015-11-11 12:01:09 -0800325
Robert Phillipsc994a932018-06-19 13:09:54 -0400326#ifdef SK_DEBUG
327 // In non-DDL mode this checks that all the flushed ops have been freed from the memory pool.
328 // When we move to partial flushes this assert will no longer be valid.
329 // In DDL mode this check is somewhat superfluous since the memory for most of the ops/opLists
330 // will be stored in the DDL's GrOpMemoryPools.
Robert Phillips9da87e02019-02-04 13:26:26 -0500331 GrOpMemoryPool* opMemoryPool = fContext->priv().opMemoryPool();
Robert Phillipsc994a932018-06-19 13:09:54 -0400332 opMemoryPool->isEmpty();
333#endif
334
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500335 GrSemaphoresSubmitted result = gpu->finishFlush(numSemaphores, backendSemaphores);
robertphillipsa13e2022015-11-11 12:01:09 -0800336
Brian Salomon967df202018-12-07 11:15:53 -0500337 flushState.deinstantiateProxyTracker()->deinstantiateAllProxies();
Greg Daniel4684f822018-03-08 15:27:36 -0500338
Brian Salomon57d2beab2018-09-10 09:35:41 -0400339 // Give the cache a chance to purge resources that become purgeable due to flushing.
340 if (flushed) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500341 fContext->priv().getResourceCache()->purgeAsNeeded();
bsalomonb77a9072016-09-07 10:02:04 -0700342 }
Chris Daltonfe199b72017-05-05 11:26:15 -0400343 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
Robert Phillips40a29d72018-01-18 12:59:22 -0500344 onFlushCBObject->postFlush(fTokenTracker.nextTokenToFlush(), fFlushingOpListIDs.begin(),
Chris Dalton3968ff92017-11-27 12:26:31 -0700345 fFlushingOpListIDs.count());
Chris Daltonfe199b72017-05-05 11:26:15 -0400346 }
Chris Dalton3968ff92017-11-27 12:26:31 -0700347 fFlushingOpListIDs.reset();
joshualittb8918c42015-12-18 09:59:46 -0800348 fFlushing = false;
Greg Daniel51316782017-08-02 15:10:09 +0000349
350 return result;
robertphillips3dc6ae52015-10-20 09:54:32 -0700351}
352
Greg Danield2073452018-12-07 11:20:33 -0500353bool GrDrawingManager::executeOpLists(int startIndex, int stopIndex, GrOpFlushState* flushState,
354 int* numOpListsExecuted) {
Robert Phillips22310d62018-09-05 11:07:21 -0400355 SkASSERT(startIndex <= stopIndex && stopIndex <= fDAG.numOpLists());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500356
Robert Phillips27483912018-04-20 12:43:18 -0400357#if GR_FLUSH_TIME_OP_SPEW
358 SkDebugf("Flushing opLists: %d to %d out of [%d, %d]\n",
Robert Phillips22310d62018-09-05 11:07:21 -0400359 startIndex, stopIndex, 0, fDAG.numOpLists());
Robert Phillips27483912018-04-20 12:43:18 -0400360 for (int i = startIndex; i < stopIndex; ++i) {
Robert Phillips22310d62018-09-05 11:07:21 -0400361 if (fDAG.opList(i)) {
362 fDAG.opList(i)->dump(true);
Robert Phillips1734dd32018-08-21 13:52:09 -0400363 }
Robert Phillips27483912018-04-20 12:43:18 -0400364 }
365#endif
366
Robert Phillips9da87e02019-02-04 13:26:26 -0500367 GrResourceProvider* resourceProvider = fContext->priv().resourceProvider();
Robert Phillipseafd48a2017-11-16 07:52:08 -0500368 bool anyOpListsExecuted = false;
369
370 for (int i = startIndex; i < stopIndex; ++i) {
Robert Phillips22310d62018-09-05 11:07:21 -0400371 if (!fDAG.opList(i)) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500372 continue;
373 }
374
Robert Phillips22310d62018-09-05 11:07:21 -0400375 GrOpList* opList = fDAG.opList(i);
376
Robert Phillips4150eea2018-02-07 17:08:21 -0500377 if (resourceProvider->explicitlyAllocateGPUResources()) {
Robert Phillips22310d62018-09-05 11:07:21 -0400378 if (!opList->isFullyInstantiated()) {
Robert Phillips4150eea2018-02-07 17:08:21 -0500379 // If the backing surface wasn't allocated drop the draw of the entire opList.
Robert Phillips22310d62018-09-05 11:07:21 -0400380 fDAG.removeOpList(i);
Robert Phillips4150eea2018-02-07 17:08:21 -0500381 continue;
382 }
383 } else {
Robert Phillips22310d62018-09-05 11:07:21 -0400384 if (!opList->instantiate(resourceProvider)) {
385 fDAG.removeOpList(i);
Robert Phillips4150eea2018-02-07 17:08:21 -0500386 continue;
387 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500388 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500389
390 // TODO: handle this instantiation via lazy surface proxies?
391 // Instantiate all deferred proxies (being built on worker threads) so we can upload them
Robert Phillips9da87e02019-02-04 13:26:26 -0500392 opList->instantiateDeferredProxies(fContext->priv().resourceProvider());
Robert Phillips22310d62018-09-05 11:07:21 -0400393 opList->prepare(flushState);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500394 }
395
396 // Upload all data to the GPU
397 flushState->preExecuteDraws();
398
Greg Danield2073452018-12-07 11:20:33 -0500399 // For Vulkan, if we have too many oplists to be flushed we end up allocating a lot of resources
400 // for each command buffer associated with the oplists. If this gets too large we can cause the
401 // devices to go OOM. In practice we usually only hit this case in our tests, but to be safe we
402 // put a cap on the number of oplists we will execute before flushing to the GPU to relieve some
403 // memory pressure.
404 static constexpr int kMaxOpListsBeforeFlush = 100;
405
Robert Phillipseafd48a2017-11-16 07:52:08 -0500406 // Execute the onFlush op lists first, if any.
Chris Dalton3968ff92017-11-27 12:26:31 -0700407 for (sk_sp<GrOpList>& onFlushOpList : fOnFlushCBOpLists) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500408 if (!onFlushOpList->execute(flushState)) {
409 SkDebugf("WARNING: onFlushOpList failed to execute.\n");
410 }
411 SkASSERT(onFlushOpList->unique());
412 onFlushOpList = nullptr;
Greg Danield2073452018-12-07 11:20:33 -0500413 (*numOpListsExecuted)++;
414 if (*numOpListsExecuted >= kMaxOpListsBeforeFlush) {
415 flushState->gpu()->finishFlush(0, nullptr);
416 *numOpListsExecuted = 0;
417 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500418 }
Chris Dalton3968ff92017-11-27 12:26:31 -0700419 fOnFlushCBOpLists.reset();
Robert Phillipseafd48a2017-11-16 07:52:08 -0500420
421 // Execute the normal op lists.
422 for (int i = startIndex; i < stopIndex; ++i) {
Robert Phillips22310d62018-09-05 11:07:21 -0400423 if (!fDAG.opList(i)) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500424 continue;
425 }
426
Robert Phillips22310d62018-09-05 11:07:21 -0400427 if (fDAG.opList(i)->execute(flushState)) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500428 anyOpListsExecuted = true;
429 }
Greg Danield2073452018-12-07 11:20:33 -0500430 (*numOpListsExecuted)++;
431 if (*numOpListsExecuted >= kMaxOpListsBeforeFlush) {
432 flushState->gpu()->finishFlush(0, nullptr);
433 *numOpListsExecuted = 0;
434 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500435 }
436
437 SkASSERT(!flushState->commandBuffer());
Robert Phillips40a29d72018-01-18 12:59:22 -0500438 SkASSERT(fTokenTracker.nextDrawToken() == fTokenTracker.nextTokenToFlush());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500439
440 // We reset the flush state before the OpLists so that the last resources to be freed are those
441 // that are written to in the OpLists. This helps to make sure the most recently used resources
442 // are the last to be purged by the resource cache.
443 flushState->reset();
444
Robert Phillips22310d62018-09-05 11:07:21 -0400445 fDAG.removeOpLists(startIndex, stopIndex);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500446
447 return anyOpListsExecuted;
448}
449
Greg Daniel51316782017-08-02 15:10:09 +0000450GrSemaphoresSubmitted GrDrawingManager::prepareSurfaceForExternalIO(
451 GrSurfaceProxy* proxy, int numSemaphores, GrBackendSemaphore backendSemaphores[]) {
bsalomon6a2b1942016-09-08 11:28:59 -0700452 if (this->wasAbandoned()) {
Greg Daniel51316782017-08-02 15:10:09 +0000453 return GrSemaphoresSubmitted::kNo;
bsalomon6a2b1942016-09-08 11:28:59 -0700454 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400455 SkDEBUGCODE(this->validate());
Robert Phillips7ee385e2017-03-30 08:02:11 -0400456 SkASSERT(proxy);
bsalomon6a2b1942016-09-08 11:28:59 -0700457
Robert Phillips9da87e02019-02-04 13:26:26 -0500458 GrGpu* gpu = fContext->priv().getGpu();
Robert Phillips874b5352018-03-16 08:48:24 -0400459 if (!gpu) {
460 return GrSemaphoresSubmitted::kNo; // Can't flush while DDL recording
461 }
462
Kevin Lubick42846132018-01-05 10:11:11 -0500463 GrSemaphoresSubmitted result = GrSemaphoresSubmitted::kNo;
Greg Daniel51316782017-08-02 15:10:09 +0000464 if (proxy->priv().hasPendingIO() || numSemaphores) {
465 result = this->flush(proxy, numSemaphores, backendSemaphores);
bsalomon6a2b1942016-09-08 11:28:59 -0700466 }
467
Robert Phillips9da87e02019-02-04 13:26:26 -0500468 if (!proxy->instantiate(fContext->priv().resourceProvider())) {
Greg Daniel51316782017-08-02 15:10:09 +0000469 return result;
Robert Phillips7ee385e2017-03-30 08:02:11 -0400470 }
471
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400472 GrSurface* surface = proxy->peekSurface();
Brian Salomon930f9392018-06-20 16:25:26 -0400473 if (auto* rt = surface->asRenderTarget()) {
474 gpu->resolveRenderTarget(rt);
475 }
476 if (auto* tex = surface->asTexture()) {
477 if (tex->texturePriv().mipMapped() == GrMipMapped::kYes &&
478 tex->texturePriv().mipMapsAreDirty()) {
479 gpu->regenerateMipMapLevels(tex);
480 }
bsalomon6a2b1942016-09-08 11:28:59 -0700481 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400482
483 SkDEBUGCODE(this->validate());
Greg Daniel51316782017-08-02 15:10:09 +0000484 return result;
bsalomon6a2b1942016-09-08 11:28:59 -0700485}
486
Chris Daltonfe199b72017-05-05 11:26:15 -0400487void GrDrawingManager::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
488 fOnFlushCBObjects.push_back(onFlushCBObject);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400489}
490
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500491#if GR_TEST_UTILS
492void GrDrawingManager::testingOnly_removeOnFlushCallbackObject(GrOnFlushCallbackObject* cb) {
493 int n = std::find(fOnFlushCBObjects.begin(), fOnFlushCBObjects.end(), cb) -
494 fOnFlushCBObjects.begin();
495 SkASSERT(n < fOnFlushCBObjects.count());
496 fOnFlushCBObjects.removeShuffle(n);
497}
498#endif
499
Robert Phillips62000362018-02-01 09:10:04 -0500500void GrDrawingManager::moveOpListsToDDL(SkDeferredDisplayList* ddl) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400501 SkDEBUGCODE(this->validate());
502
503 // no opList should receive a new command after this
Robert Phillips9da87e02019-02-04 13:26:26 -0500504 fDAG.closeAll(fContext->priv().caps());
Robert Phillips38d64b02018-09-04 13:23:26 -0400505 fActiveOpList = nullptr;
Robert Phillips7a137052018-02-01 11:23:12 -0500506
Robert Phillips22310d62018-09-05 11:07:21 -0400507 fDAG.swap(&ddl->fOpLists);
Robert Phillips867ce8f2018-06-21 10:28:36 -0400508
Robert Phillips774168e2018-05-31 12:43:27 -0400509 if (fPathRendererChain) {
510 if (auto ccpr = fPathRendererChain->getCoverageCountingPathRenderer()) {
511 ddl->fPendingPaths = ccpr->detachPendingPaths();
512 }
513 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400514
515 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500516}
517
518void GrDrawingManager::copyOpListsFromDDL(const SkDeferredDisplayList* ddl,
519 GrRenderTargetProxy* newDest) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400520 SkDEBUGCODE(this->validate());
521
522 if (fActiveOpList) {
523 // This is a temporary fix for the partial-MDB world. In that world we're not
524 // reordering so ops that (in the single opList world) would've just glommed onto the
525 // end of the single opList but referred to a far earlier RT need to appear in their
526 // own opList.
Robert Phillips9da87e02019-02-04 13:26:26 -0500527 fActiveOpList->makeClosed(*fContext->priv().caps());
Robert Phillips38d64b02018-09-04 13:23:26 -0400528 fActiveOpList = nullptr;
529 }
530
Robert Phillips62000362018-02-01 09:10:04 -0500531 // Here we jam the proxy that backs the current replay SkSurface into the LazyProxyData.
532 // The lazy proxy that references it (in the copied opLists) will steal its GrTexture.
533 ddl->fLazyProxyData->fReplayDest = newDest;
Robert Phillips774168e2018-05-31 12:43:27 -0400534
535 if (ddl->fPendingPaths.size()) {
536 GrCoverageCountingPathRenderer* ccpr = this->getCoverageCountingPathRenderer();
537
538 ccpr->mergePendingPaths(ddl->fPendingPaths);
539 }
Robert Phillips22310d62018-09-05 11:07:21 -0400540
541 fDAG.add(ddl->fOpLists);
Robert Phillips38d64b02018-09-04 13:23:26 -0400542
543 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500544}
545
Robert Phillips38d64b02018-09-04 13:23:26 -0400546#ifdef SK_DEBUG
547void GrDrawingManager::validate() const {
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400548 if (fDAG.sortingOpLists() && fReduceOpListSplitting) {
549 SkASSERT(!fActiveOpList);
550 } else {
551 if (fActiveOpList) {
552 SkASSERT(!fDAG.empty());
553 SkASSERT(!fActiveOpList->isClosed());
554 SkASSERT(fActiveOpList == fDAG.back());
Robert Phillips38d64b02018-09-04 13:23:26 -0400555 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400556
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400557 for (int i = 0; i < fDAG.numOpLists(); ++i) {
558 if (fActiveOpList != fDAG.opList(i)) {
559 SkASSERT(fDAG.opList(i)->isClosed());
560 }
561 }
562
563 if (!fDAG.empty() && !fDAG.back()->isClosed()) {
564 SkASSERT(fActiveOpList == fDAG.back());
565 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400566 }
567}
568#endif
569
Robert Phillips941d1442017-06-14 16:37:02 -0400570sk_sp<GrRenderTargetOpList> GrDrawingManager::newRTOpList(GrRenderTargetProxy* rtp,
571 bool managedOpList) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400572 SkDEBUGCODE(this->validate());
robertphillips3dc6ae52015-10-20 09:54:32 -0700573 SkASSERT(fContext);
574
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400575 if (fDAG.sortingOpLists() && fReduceOpListSplitting) {
576 // In this case we need to close all the opLists that rely on the current contents of
577 // 'rtp'. That is bc we're going to update the content of the proxy so they need to be
578 // split in case they use both the old and new content. (This is a bit of an overkill:
579 // they really only need to be split if they ever reference proxy's contents again but
580 // that is hard to predict/handle).
581 if (GrOpList* lastOpList = rtp->getLastOpList()) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500582 lastOpList->closeThoseWhoDependOnMe(*fContext->priv().caps());
Robert Phillips46acf9d2018-10-09 09:31:40 -0400583 }
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400584 } else if (fActiveOpList) {
585 // This is a temporary fix for the partial-MDB world. In that world we're not
586 // reordering so ops that (in the single opList world) would've just glommed onto the
587 // end of the single opList but referred to a far earlier RT need to appear in their
588 // own opList.
Robert Phillips9da87e02019-02-04 13:26:26 -0500589 fActiveOpList->makeClosed(*fContext->priv().caps());
Robert Phillips38d64b02018-09-04 13:23:26 -0400590 fActiveOpList = nullptr;
robertphillips3dc6ae52015-10-20 09:54:32 -0700591 }
robertphillips3dc6ae52015-10-20 09:54:32 -0700592
Robert Phillips9da87e02019-02-04 13:26:26 -0500593 auto resourceProvider = fContext->priv().resourceProvider();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500594
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500595 sk_sp<GrRenderTargetOpList> opList(new GrRenderTargetOpList(
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500596 resourceProvider,
Robert Phillips9da87e02019-02-04 13:26:26 -0500597 fContext->priv().refOpMemoryPool(),
Robert Phillips3a9710b2018-03-27 17:51:55 -0400598 rtp,
Robert Phillipsd6841482019-02-08 10:29:20 -0500599 fContext->priv().auditTrail()));
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400600 SkASSERT(rtp->getLastOpList() == opList.get());
robertphillips3dc6ae52015-10-20 09:54:32 -0700601
Robert Phillips941d1442017-06-14 16:37:02 -0400602 if (managedOpList) {
Robert Phillips22310d62018-09-05 11:07:21 -0400603 fDAG.add(opList);
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400604
605 if (!fDAG.sortingOpLists() || !fReduceOpListSplitting) {
606 fActiveOpList = opList.get();
607 }
Robert Phillips941d1442017-06-14 16:37:02 -0400608 }
robertphillips3dc6ae52015-10-20 09:54:32 -0700609
Robert Phillips38d64b02018-09-04 13:23:26 -0400610 SkDEBUGCODE(this->validate());
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400611 return opList;
robertphillips3dc6ae52015-10-20 09:54:32 -0700612}
613
Robert Phillipsb6deea82017-05-11 14:14:30 -0400614sk_sp<GrTextureOpList> GrDrawingManager::newTextureOpList(GrTextureProxy* textureProxy) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400615 SkDEBUGCODE(this->validate());
Brian Osman45580d32016-11-23 09:37:01 -0500616 SkASSERT(fContext);
617
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400618 if (fDAG.sortingOpLists() && fReduceOpListSplitting) {
619 // In this case we need to close all the opLists that rely on the current contents of
620 // 'texture'. That is bc we're going to update the content of the proxy so they need to
621 // be split in case they use both the old and new content. (This is a bit of an
622 // overkill: they really only need to be split if they ever reference proxy's contents
623 // again but that is hard to predict/handle).
624 if (GrOpList* lastOpList = textureProxy->getLastOpList()) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500625 lastOpList->closeThoseWhoDependOnMe(*fContext->priv().caps());
Robert Phillips46acf9d2018-10-09 09:31:40 -0400626 }
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400627 } else if (fActiveOpList) {
628 // This is a temporary fix for the partial-MDB world. In that world we're not
629 // reordering so ops that (in the single opList world) would've just glommed onto the
630 // end of the single opList but referred to a far earlier RT need to appear in their
631 // own opList.
Robert Phillips9da87e02019-02-04 13:26:26 -0500632 fActiveOpList->makeClosed(*fContext->priv().caps());
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400633 fActiveOpList = nullptr;
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400634 }
635
Robert Phillips9da87e02019-02-04 13:26:26 -0500636 sk_sp<GrTextureOpList> opList(new GrTextureOpList(fContext->priv().resourceProvider(),
637 fContext->priv().refOpMemoryPool(),
Robert Phillips5efd5ea2017-05-30 13:47:32 -0400638 textureProxy,
Robert Phillipsd6841482019-02-08 10:29:20 -0500639 fContext->priv().auditTrail()));
Brian Osman45580d32016-11-23 09:37:01 -0500640
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400641 SkASSERT(textureProxy->getLastOpList() == opList.get());
Robert Phillips4a395042017-04-24 16:27:17 +0000642
Robert Phillips22310d62018-09-05 11:07:21 -0400643 fDAG.add(opList);
Robert Phillipsb7a98ef2018-10-10 09:26:00 -0400644 if (!fDAG.sortingOpLists() || !fReduceOpListSplitting) {
645 fActiveOpList = opList.get();
646 }
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400647
Robert Phillips38d64b02018-09-04 13:23:26 -0400648 SkDEBUGCODE(this->validate());
Robert Phillips4a395042017-04-24 16:27:17 +0000649 return opList;
Brian Osman45580d32016-11-23 09:37:01 -0500650}
651
Herb Derby26cbe512018-05-24 14:39:01 -0400652GrTextContext* GrDrawingManager::getTextContext() {
653 if (!fTextContext) {
654 fTextContext = GrTextContext::Make(fOptionsForTextContext);
brianosman86e76262016-08-11 12:17:31 -0700655 }
656
Herb Derby26cbe512018-05-24 14:39:01 -0400657 return fTextContext.get();
brianosman86e76262016-08-11 12:17:31 -0700658}
659
robertphillips68737822015-10-29 12:12:21 -0700660/*
661 * This method finds a path renderer that can draw the specified path on
662 * the provided target.
663 * Due to its expense, the software path renderer has split out so it can
664 * can be individually allowed/disallowed via the "allowSW" boolean.
665 */
666GrPathRenderer* GrDrawingManager::getPathRenderer(const GrPathRenderer::CanDrawPathArgs& args,
667 bool allowSW,
668 GrPathRendererChain::DrawType drawType,
669 GrPathRenderer::StencilSupport* stencilSupport) {
670
671 if (!fPathRendererChain) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400672 fPathRendererChain.reset(new GrPathRendererChain(fContext, fOptionsForPathRendererChain));
robertphillips68737822015-10-29 12:12:21 -0700673 }
674
675 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(args, drawType, stencilSupport);
676 if (!pr && allowSW) {
Brian Salomone7df0bb2018-05-07 14:44:57 -0400677 auto swPR = this->getSoftwarePathRenderer();
678 if (GrPathRenderer::CanDrawPath::kNo != swPR->canDrawPath(args)) {
679 pr = swPR;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500680 }
robertphillips68737822015-10-29 12:12:21 -0700681 }
682
683 return pr;
684}
685
Brian Salomone7df0bb2018-05-07 14:44:57 -0400686GrPathRenderer* GrDrawingManager::getSoftwarePathRenderer() {
687 if (!fSoftwarePathRenderer) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400688 fSoftwarePathRenderer.reset(
Robert Phillips9da87e02019-02-04 13:26:26 -0500689 new GrSoftwarePathRenderer(fContext->priv().proxyProvider(),
Ben Wagner9ec70c62018-07-12 13:30:47 -0400690 fOptionsForPathRendererChain.fAllowPathMaskCaching));
Brian Salomone7df0bb2018-05-07 14:44:57 -0400691 }
Ben Wagner9ec70c62018-07-12 13:30:47 -0400692 return fSoftwarePathRenderer.get();
Brian Salomone7df0bb2018-05-07 14:44:57 -0400693}
694
Chris Daltonfddb6c02017-11-04 15:22:22 -0600695GrCoverageCountingPathRenderer* GrDrawingManager::getCoverageCountingPathRenderer() {
696 if (!fPathRendererChain) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400697 fPathRendererChain.reset(new GrPathRendererChain(fContext, fOptionsForPathRendererChain));
Chris Daltonfddb6c02017-11-04 15:22:22 -0600698 }
699 return fPathRendererChain->getCoverageCountingPathRenderer();
700}
701
Brian Salomon653f42f2018-07-10 10:07:31 -0400702void GrDrawingManager::flushIfNecessary() {
Robert Phillips9da87e02019-02-04 13:26:26 -0500703 GrResourceCache* resourceCache = fContext->priv().getResourceCache();
Brian Salomon653f42f2018-07-10 10:07:31 -0400704 if (resourceCache && resourceCache->requestsFlush()) {
Brian Salomon57d2beab2018-09-10 09:35:41 -0400705 this->flush(nullptr, 0, nullptr);
706 resourceCache->purgeAsNeeded();
Brian Salomon653f42f2018-07-10 10:07:31 -0400707 }
708}
709
Brian Osman11052242016-10-27 14:47:55 -0400710sk_sp<GrRenderTargetContext> GrDrawingManager::makeRenderTargetContext(
Robert Phillips37430132016-11-09 06:50:43 -0500711 sk_sp<GrSurfaceProxy> sProxy,
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400712 sk_sp<SkColorSpace> colorSpace,
Robert Phillips941d1442017-06-14 16:37:02 -0400713 const SkSurfaceProps* surfaceProps,
714 bool managedOpList) {
Robert Phillips37430132016-11-09 06:50:43 -0500715 if (this->wasAbandoned() || !sProxy->asRenderTargetProxy()) {
robertphillips3dc6ae52015-10-20 09:54:32 -0700716 return nullptr;
717 }
718
brianosman0e22eb82016-08-30 07:07:59 -0700719 // SkSurface catches bad color space usage at creation. This check handles anything that slips
Brian Salomon366093f2018-02-13 09:25:22 -0500720 // by, including internal usage.
Robert Phillips9da87e02019-02-04 13:26:26 -0500721 if (!SkSurface_Gpu::Valid(fContext->priv().caps(), sProxy->config(), colorSpace.get())) {
brianosmana9c3c6a2016-09-29 10:08:36 -0700722 SkDEBUGFAIL("Invalid config and colorspace combination");
brianosman0e22eb82016-08-30 07:07:59 -0700723 return nullptr;
724 }
joshualitt96880d92016-02-16 10:36:53 -0800725
Robert Phillips2c862492017-01-18 10:08:39 -0500726 sk_sp<GrRenderTargetProxy> rtp(sk_ref_sp(sProxy->asRenderTargetProxy()));
727
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500728 return sk_sp<GrRenderTargetContext>(new GrRenderTargetContext(
729 fContext, this, std::move(rtp),
730 std::move(colorSpace),
731 surfaceProps,
Robert Phillipsd6841482019-02-08 10:29:20 -0500732 fContext->priv().auditTrail(),
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500733 fSingleOwner, managedOpList));
robertphillips3dc6ae52015-10-20 09:54:32 -0700734}
Brian Osman45580d32016-11-23 09:37:01 -0500735
Robert Phillips2c862492017-01-18 10:08:39 -0500736sk_sp<GrTextureContext> GrDrawingManager::makeTextureContext(sk_sp<GrSurfaceProxy> sProxy,
737 sk_sp<SkColorSpace> colorSpace) {
Brian Osman45580d32016-11-23 09:37:01 -0500738 if (this->wasAbandoned() || !sProxy->asTextureProxy()) {
739 return nullptr;
740 }
741
Robert Phillips2c862492017-01-18 10:08:39 -0500742 // SkSurface catches bad color space usage at creation. This check handles anything that slips
Brian Salomon366093f2018-02-13 09:25:22 -0500743 // by, including internal usage.
Robert Phillips9da87e02019-02-04 13:26:26 -0500744 if (!SkSurface_Gpu::Valid(fContext->priv().caps(), sProxy->config(), colorSpace.get())) {
Robert Phillips2c862492017-01-18 10:08:39 -0500745 SkDEBUGFAIL("Invalid config and colorspace combination");
746 return nullptr;
747 }
748
Robert Phillips383c4182018-02-07 12:47:44 -0500749 // GrTextureRenderTargets should always be using a GrRenderTargetContext
Brian Osman45580d32016-11-23 09:37:01 -0500750 SkASSERT(!sProxy->asRenderTargetProxy());
751
752 sk_sp<GrTextureProxy> textureProxy(sk_ref_sp(sProxy->asTextureProxy()));
753
754 return sk_sp<GrTextureContext>(new GrTextureContext(fContext, this, std::move(textureProxy),
Robert Phillips2c862492017-01-18 10:08:39 -0500755 std::move(colorSpace),
Robert Phillipsd6841482019-02-08 10:29:20 -0500756 fContext->priv().auditTrail(),
Robert Phillips2c862492017-01-18 10:08:39 -0500757 fSingleOwner));
Brian Osman45580d32016-11-23 09:37:01 -0500758}