1 /*
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
8 #include "GrDrawingManager.h"
9
10 #include "GrBackendSemaphore.h"
11 #include "GrContextPriv.h"
12 #include "GrGpu.h"
13 #include "GrMemoryPool.h"
14 #include "GrOnFlushResourceProvider.h"
15 #include "GrOpList.h"
16 #include "GrRecordingContext.h"
17 #include "GrRecordingContextPriv.h"
18 #include "GrRenderTargetContext.h"
19 #include "GrRenderTargetProxy.h"
20 #include "GrResourceAllocator.h"
21 #include "GrResourceProvider.h"
22 #include "GrSoftwarePathRenderer.h"
23 #include "GrSurfaceProxyPriv.h"
24 #include "GrTexture.h"
25 #include "GrTextureContext.h"
26 #include "GrTextureOpList.h"
27 #include "GrTexturePriv.h"
28 #include "GrTextureProxy.h"
29 #include "GrTextureProxyPriv.h"
30 #include "GrTracing.h"
31 #include "SkDeferredDisplayList.h"
32 #include "SkSurface_Gpu.h"
33 #include "SkTTopoSort.h"
34 #include "ccpr/GrCoverageCountingPathRenderer.h"
35 #include "text/GrTextContext.h"
36
OpListDAG(bool explicitlyAllocating,bool sortOpLists)37 GrDrawingManager::OpListDAG::OpListDAG(bool explicitlyAllocating, bool sortOpLists)
38 : fSortOpLists(sortOpLists) {
39 SkASSERT(!sortOpLists || explicitlyAllocating);
40 }
41
~OpListDAG()42 GrDrawingManager::OpListDAG::~OpListDAG() {}
43
gatherIDs(SkSTArray<8,uint32_t,true> * idArray) const44 void GrDrawingManager::OpListDAG::gatherIDs(SkSTArray<8, uint32_t, true>* idArray) const {
45 idArray->reset(fOpLists.count());
46 for (int i = 0; i < fOpLists.count(); ++i) {
47 if (fOpLists[i]) {
48 (*idArray)[i] = fOpLists[i]->uniqueID();
49 }
50 }
51 }
52
reset()53 void GrDrawingManager::OpListDAG::reset() {
54 fOpLists.reset();
55 }
56
removeOpList(int index)57 void GrDrawingManager::OpListDAG::removeOpList(int index) {
58 if (!fOpLists[index]->unique()) {
59 // TODO: Eventually this should be guaranteed unique: http://skbug.com/7111
60 fOpLists[index]->endFlush();
61 }
62
63 fOpLists[index] = nullptr;
64 }
65
removeOpLists(int startIndex,int stopIndex)66 void GrDrawingManager::OpListDAG::removeOpLists(int startIndex, int stopIndex) {
67 for (int i = startIndex; i < stopIndex; ++i) {
68 if (!fOpLists[i]) {
69 continue;
70 }
71 this->removeOpList(i);
72 }
73 }
74
add(sk_sp<GrOpList> opList)75 void GrDrawingManager::OpListDAG::add(sk_sp<GrOpList> opList) {
76 fOpLists.emplace_back(std::move(opList));
77 }
78
add(const SkTArray<sk_sp<GrOpList>> & opLists)79 void GrDrawingManager::OpListDAG::add(const SkTArray<sk_sp<GrOpList>>& opLists) {
80 fOpLists.push_back_n(opLists.count(), opLists.begin());
81 }
82
swap(SkTArray<sk_sp<GrOpList>> * opLists)83 void GrDrawingManager::OpListDAG::swap(SkTArray<sk_sp<GrOpList>>* opLists) {
84 SkASSERT(opLists->empty());
85 opLists->swap(fOpLists);
86 }
87
prepForFlush()88 void GrDrawingManager::OpListDAG::prepForFlush() {
89 if (fSortOpLists) {
90 SkDEBUGCODE(bool result =) SkTTopoSort<GrOpList, GrOpList::TopoSortTraits>(&fOpLists);
91 SkASSERT(result);
92 }
93
94 #ifdef SK_DEBUG
95 // This block checks for any unnecessary splits in the opLists. If two sequential opLists
96 // share the same backing GrSurfaceProxy it means the opList was artificially split.
97 if (fOpLists.count()) {
98 GrRenderTargetOpList* prevOpList = fOpLists[0]->asRenderTargetOpList();
99 for (int i = 1; i < fOpLists.count(); ++i) {
100 GrRenderTargetOpList* curOpList = fOpLists[i]->asRenderTargetOpList();
101
102 if (prevOpList && curOpList) {
103 SkASSERT(prevOpList->fTarget.get() != curOpList->fTarget.get());
104 }
105
106 prevOpList = curOpList;
107 }
108 }
109 #endif
110 }
111
closeAll(const GrCaps * caps)112 void GrDrawingManager::OpListDAG::closeAll(const GrCaps* caps) {
113 for (int i = 0; i < fOpLists.count(); ++i) {
114 if (fOpLists[i]) {
115 fOpLists[i]->makeClosed(*caps);
116 }
117 }
118 }
119
cleanup(const GrCaps * caps)120 void GrDrawingManager::OpListDAG::cleanup(const GrCaps* caps) {
121 for (int i = 0; i < fOpLists.count(); ++i) {
122 if (!fOpLists[i]) {
123 continue;
124 }
125
126 // no opList should receive a new command after this
127 fOpLists[i]->makeClosed(*caps);
128
129 // We shouldn't need to do this, but it turns out some clients still hold onto opLists
130 // after a cleanup.
131 // MDB TODO: is this still true?
132 if (!fOpLists[i]->unique()) {
133 // TODO: Eventually this should be guaranteed unique.
134 // https://bugs.chromium.org/p/skia/issues/detail?id=7111
135 fOpLists[i]->endFlush();
136 }
137 }
138
139 fOpLists.reset();
140 }
141
142 ///////////////////////////////////////////////////////////////////////////////////////////////////
GrDrawingManager(GrRecordingContext * context,const GrPathRendererChain::Options & optionsForPathRendererChain,const GrTextContext::Options & optionsForTextContext,bool explicitlyAllocating,bool sortOpLists,GrContextOptions::Enable reduceOpListSplitting)143 GrDrawingManager::GrDrawingManager(GrRecordingContext* context,
144 const GrPathRendererChain::Options& optionsForPathRendererChain,
145 const GrTextContext::Options& optionsForTextContext,
146 bool explicitlyAllocating,
147 bool sortOpLists,
148 GrContextOptions::Enable reduceOpListSplitting)
149 : fContext(context)
150 , fOptionsForPathRendererChain(optionsForPathRendererChain)
151 , fOptionsForTextContext(optionsForTextContext)
152 , fDAG(explicitlyAllocating, sortOpLists)
153 , fTextContext(nullptr)
154 , fPathRendererChain(nullptr)
155 , fSoftwarePathRenderer(nullptr)
156 , fFlushing(false) {
157 if (GrContextOptions::Enable::kNo == reduceOpListSplitting) {
158 fReduceOpListSplitting = false;
159 } else if (GrContextOptions::Enable::kYes == reduceOpListSplitting) {
160 fReduceOpListSplitting = true;
161 } else {
162 // For now, this is only turned on when explicitly enabled. Once mini-flushes are
163 // implemented it should be enabled whenever sorting is enabled.
164 fReduceOpListSplitting = false; // sortOpLists
165 }
166 }
167
cleanup()168 void GrDrawingManager::cleanup() {
169 fDAG.cleanup(fContext->priv().caps());
170
171 fPathRendererChain = nullptr;
172 fSoftwarePathRenderer = nullptr;
173
174 fOnFlushCBObjects.reset();
175 }
176
~GrDrawingManager()177 GrDrawingManager::~GrDrawingManager() {
178 this->cleanup();
179 }
180
wasAbandoned() const181 bool GrDrawingManager::wasAbandoned() const {
182 return fContext->priv().abandoned();
183 }
184
freeGpuResources()185 void GrDrawingManager::freeGpuResources() {
186 for (int i = fOnFlushCBObjects.count() - 1; i >= 0; --i) {
187 if (!fOnFlushCBObjects[i]->retainOnFreeGpuResources()) {
188 // it's safe to just do this because we're iterating in reverse
189 fOnFlushCBObjects.removeShuffle(i);
190 }
191 }
192
193 // a path renderer may be holding onto resources
194 fPathRendererChain = nullptr;
195 fSoftwarePathRenderer = nullptr;
196 }
197
198 // MDB TODO: make use of the 'proxy' parameter.
flush(GrSurfaceProxy * proxy,SkSurface::BackendSurfaceAccess access,GrFlushFlags flags,int numSemaphores,GrBackendSemaphore backendSemaphores[],GrGpuFinishedProc finishedProc,GrGpuFinishedContext finishedContext)199 GrSemaphoresSubmitted GrDrawingManager::flush(GrSurfaceProxy* proxy,
200 SkSurface::BackendSurfaceAccess access,
201 GrFlushFlags flags,
202 int numSemaphores,
203 GrBackendSemaphore backendSemaphores[],
204 GrGpuFinishedProc finishedProc,
205 GrGpuFinishedContext finishedContext) {
206 GR_CREATE_TRACE_MARKER_CONTEXT("GrDrawingManager", "flush", fContext);
207
208 if (fFlushing || this->wasAbandoned()) {
209 if (finishedProc) {
210 finishedProc(finishedContext);
211 }
212 return GrSemaphoresSubmitted::kNo;
213 }
214 SkDEBUGCODE(this->validate());
215
216 auto direct = fContext->priv().asDirectContext();
217 if (!direct) {
218 if (finishedProc) {
219 finishedProc(finishedContext);
220 }
221 return GrSemaphoresSubmitted::kNo; // Can't flush while DDL recording
222 }
223
224 GrGpu* gpu = direct->priv().getGpu();
225 if (!gpu) {
226 if (finishedProc) {
227 finishedProc(finishedContext);
228 }
229 return GrSemaphoresSubmitted::kNo; // Can't flush while DDL recording
230 }
231
232 fFlushing = true;
233
234 auto resourceProvider = direct->priv().resourceProvider();
235 auto resourceCache = direct->priv().getResourceCache();
236
237 // Semi-usually the GrOpLists are already closed at this point, but sometimes Ganesh
238 // needs to flush mid-draw. In that case, the SkGpuDevice's GrOpLists won't be closed
239 // but need to be flushed anyway. Closing such GrOpLists here will mean new
240 // GrOpLists will be created to replace them if the SkGpuDevice(s) write to them again.
241 fDAG.closeAll(fContext->priv().caps());
242 fActiveOpList = nullptr;
243
244 fDAG.prepForFlush();
245 if (!fCpuBufferCache) {
246 // We cache more buffers when the backend is using client side arrays. Otherwise, we
247 // expect each pool will use a CPU buffer as a staging buffer before uploading to a GPU
248 // buffer object. Each pool only requires one staging buffer at a time.
249 int maxCachedBuffers = fContext->priv().caps()->preferClientSideDynamicBuffers() ? 2 : 6;
250 fCpuBufferCache = GrBufferAllocPool::CpuBufferCache::Make(maxCachedBuffers);
251 }
252
253 GrOpFlushState flushState(gpu, resourceProvider, &fTokenTracker, fCpuBufferCache);
254
255 GrOnFlushResourceProvider onFlushProvider(this);
256 // TODO: AFAICT the only reason fFlushState is on GrDrawingManager rather than on the
257 // stack here is to preserve the flush tokens.
258
259 // Prepare any onFlush op lists (e.g. atlases).
260 if (!fOnFlushCBObjects.empty()) {
261 fDAG.gatherIDs(&fFlushingOpListIDs);
262
263 SkSTArray<4, sk_sp<GrRenderTargetContext>> renderTargetContexts;
264 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
265 onFlushCBObject->preFlush(&onFlushProvider,
266 fFlushingOpListIDs.begin(), fFlushingOpListIDs.count(),
267 &renderTargetContexts);
268 for (const sk_sp<GrRenderTargetContext>& rtc : renderTargetContexts) {
269 sk_sp<GrRenderTargetOpList> onFlushOpList = sk_ref_sp(rtc->getRTOpList());
270 if (!onFlushOpList) {
271 continue; // Odd - but not a big deal
272 }
273 #ifdef SK_DEBUG
274 // OnFlush callbacks are already invoked during flush, and are therefore expected to
275 // handle resource allocation & usage on their own. (No deferred or lazy proxies!)
276 onFlushOpList->visitProxies_debugOnly([](GrSurfaceProxy* p) {
277 SkASSERT(!p->asTextureProxy() || !p->asTextureProxy()->texPriv().isDeferred());
278 SkASSERT(GrSurfaceProxy::LazyState::kNot == p->lazyInstantiationState());
279 });
280 #endif
281 onFlushOpList->makeClosed(*fContext->priv().caps());
282 onFlushOpList->prepare(&flushState);
283 fOnFlushCBOpLists.push_back(std::move(onFlushOpList));
284 }
285 renderTargetContexts.reset();
286 }
287 }
288
289 #if 0
290 // Enable this to print out verbose GrOp information
291 for (int i = 0; i < fOpLists.count(); ++i) {
292 SkDEBUGCODE(fOpLists[i]->dump();)
293 }
294 #endif
295
296 int startIndex, stopIndex;
297 bool flushed = false;
298
299 {
300 GrResourceAllocator alloc(resourceProvider, flushState.deinstantiateProxyTracker());
301 for (int i = 0; i < fDAG.numOpLists(); ++i) {
302 if (fDAG.opList(i)) {
303 fDAG.opList(i)->gatherProxyIntervals(&alloc);
304 }
305 alloc.markEndOfOpList(i);
306 }
307
308 GrResourceAllocator::AssignError error = GrResourceAllocator::AssignError::kNoError;
309 int numOpListsExecuted = 0;
310 while (alloc.assign(&startIndex, &stopIndex, &error)) {
311 if (GrResourceAllocator::AssignError::kFailedProxyInstantiation == error) {
312 for (int i = startIndex; i < stopIndex; ++i) {
313 if (fDAG.opList(i) && !fDAG.opList(i)->isFullyInstantiated()) {
314 // If the backing surface wasn't allocated drop the entire opList.
315 fDAG.removeOpList(i);
316 }
317 if (fDAG.opList(i)) {
318 fDAG.opList(i)->purgeOpsWithUninstantiatedProxies();
319 }
320 }
321 }
322
323 if (this->executeOpLists(startIndex, stopIndex, &flushState, &numOpListsExecuted)) {
324 flushed = true;
325 }
326 }
327 }
328
329 #ifdef SK_DEBUG
330 for (int i = 0; i < fDAG.numOpLists(); ++i) {
331 // If there are any remaining opLists at this point, make sure they will not survive the
332 // flush. Otherwise we need to call endFlush() on them.
333 // http://skbug.com/7111
334 SkASSERT(!fDAG.opList(i) || fDAG.opList(i)->unique());
335 }
336 #endif
337 fDAG.reset();
338
339 #ifdef SK_DEBUG
340 // In non-DDL mode this checks that all the flushed ops have been freed from the memory pool.
341 // When we move to partial flushes this assert will no longer be valid.
342 // In DDL mode this check is somewhat superfluous since the memory for most of the ops/opLists
343 // will be stored in the DDL's GrOpMemoryPools.
344 GrOpMemoryPool* opMemoryPool = fContext->priv().opMemoryPool();
345 opMemoryPool->isEmpty();
346 #endif
347
348 GrSemaphoresSubmitted result = gpu->finishFlush(proxy, access, flags, numSemaphores,
349 backendSemaphores, finishedProc,
350 finishedContext);
351
352 flushState.deinstantiateProxyTracker()->deinstantiateAllProxies();
353
354 // Give the cache a chance to purge resources that become purgeable due to flushing.
355 if (flushed) {
356 resourceCache->purgeAsNeeded();
357 flushed = false;
358 }
359 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
360 onFlushCBObject->postFlush(fTokenTracker.nextTokenToFlush(), fFlushingOpListIDs.begin(),
361 fFlushingOpListIDs.count());
362 flushed = true;
363 }
364 if (flushed) {
365 resourceCache->purgeAsNeeded();
366 }
367 fFlushingOpListIDs.reset();
368 fFlushing = false;
369
370 return result;
371 }
372
executeOpLists(int startIndex,int stopIndex,GrOpFlushState * flushState,int * numOpListsExecuted)373 bool GrDrawingManager::executeOpLists(int startIndex, int stopIndex, GrOpFlushState* flushState,
374 int* numOpListsExecuted) {
375 SkASSERT(startIndex <= stopIndex && stopIndex <= fDAG.numOpLists());
376
377 #if GR_FLUSH_TIME_OP_SPEW
378 SkDebugf("Flushing opLists: %d to %d out of [%d, %d]\n",
379 startIndex, stopIndex, 0, fDAG.numOpLists());
380 for (int i = startIndex; i < stopIndex; ++i) {
381 if (fDAG.opList(i)) {
382 fDAG.opList(i)->dump(true);
383 }
384 }
385 #endif
386
387 auto direct = fContext->priv().asDirectContext();
388 if (!direct) {
389 return false;
390 }
391
392 auto resourceProvider = direct->priv().resourceProvider();
393 bool anyOpListsExecuted = false;
394
395 for (int i = startIndex; i < stopIndex; ++i) {
396 if (!fDAG.opList(i)) {
397 continue;
398 }
399
400 GrOpList* opList = fDAG.opList(i);
401
402 if (resourceProvider->explicitlyAllocateGPUResources()) {
403 if (!opList->isFullyInstantiated()) {
404 // If the backing surface wasn't allocated drop the draw of the entire opList.
405 fDAG.removeOpList(i);
406 continue;
407 }
408 } else {
409 if (!opList->instantiate(resourceProvider)) {
410 fDAG.removeOpList(i);
411 continue;
412 }
413 }
414
415 // TODO: handle this instantiation via lazy surface proxies?
416 // Instantiate all deferred proxies (being built on worker threads) so we can upload them
417 opList->instantiateDeferredProxies(resourceProvider);
418 opList->prepare(flushState);
419 }
420
421 // Upload all data to the GPU
422 flushState->preExecuteDraws();
423
424 // For Vulkan, if we have too many oplists to be flushed we end up allocating a lot of resources
425 // for each command buffer associated with the oplists. If this gets too large we can cause the
426 // devices to go OOM. In practice we usually only hit this case in our tests, but to be safe we
427 // put a cap on the number of oplists we will execute before flushing to the GPU to relieve some
428 // memory pressure.
429 static constexpr int kMaxOpListsBeforeFlush = 100;
430
431 // Execute the onFlush op lists first, if any.
432 for (sk_sp<GrOpList>& onFlushOpList : fOnFlushCBOpLists) {
433 if (!onFlushOpList->execute(flushState)) {
434 SkDebugf("WARNING: onFlushOpList failed to execute.\n");
435 }
436 SkASSERT(onFlushOpList->unique());
437 onFlushOpList = nullptr;
438 (*numOpListsExecuted)++;
439 if (*numOpListsExecuted >= kMaxOpListsBeforeFlush) {
440 flushState->gpu()->finishFlush(nullptr, SkSurface::BackendSurfaceAccess::kNoAccess,
441 kNone_GrFlushFlags, 0, nullptr, nullptr, nullptr);
442 *numOpListsExecuted = 0;
443 }
444 }
445 fOnFlushCBOpLists.reset();
446
447 // Execute the normal op lists.
448 for (int i = startIndex; i < stopIndex; ++i) {
449 if (!fDAG.opList(i)) {
450 continue;
451 }
452
453 if (fDAG.opList(i)->execute(flushState)) {
454 anyOpListsExecuted = true;
455 }
456 (*numOpListsExecuted)++;
457 if (*numOpListsExecuted >= kMaxOpListsBeforeFlush) {
458 flushState->gpu()->finishFlush(nullptr, SkSurface::BackendSurfaceAccess::kNoAccess,
459 kNone_GrFlushFlags, 0, nullptr, nullptr, nullptr);
460 *numOpListsExecuted = 0;
461 }
462 }
463
464 SkASSERT(!flushState->commandBuffer());
465 SkASSERT(fTokenTracker.nextDrawToken() == fTokenTracker.nextTokenToFlush());
466
467 // We reset the flush state before the OpLists so that the last resources to be freed are those
468 // that are written to in the OpLists. This helps to make sure the most recently used resources
469 // are the last to be purged by the resource cache.
470 flushState->reset();
471
472 fDAG.removeOpLists(startIndex, stopIndex);
473
474 return anyOpListsExecuted;
475 }
476
prepareSurfaceForExternalIO(GrSurfaceProxy * proxy,SkSurface::BackendSurfaceAccess access,GrFlushFlags flags,int numSemaphores,GrBackendSemaphore backendSemaphores[],GrGpuFinishedProc finishedProc,GrGpuFinishedContext finishedContext)477 GrSemaphoresSubmitted GrDrawingManager::prepareSurfaceForExternalIO(
478 GrSurfaceProxy* proxy, SkSurface::BackendSurfaceAccess access, GrFlushFlags flags,
479 int numSemaphores, GrBackendSemaphore backendSemaphores[],
480 GrGpuFinishedProc finishedProc,
481 GrGpuFinishedContext finishedContext) {
482 if (this->wasAbandoned()) {
483 return GrSemaphoresSubmitted::kNo;
484 }
485 SkDEBUGCODE(this->validate());
486 SkASSERT(proxy);
487
488 auto direct = fContext->priv().asDirectContext();
489 if (!direct) {
490 return GrSemaphoresSubmitted::kNo; // Can't flush while DDL recording
491 }
492
493 GrGpu* gpu = direct->priv().getGpu();
494 if (!gpu) {
495 return GrSemaphoresSubmitted::kNo; // Can't flush while DDL recording
496 }
497
498 auto resourceProvider = direct->priv().resourceProvider();
499
500 GrSemaphoresSubmitted result = GrSemaphoresSubmitted::kNo;
501 if (proxy->priv().hasPendingIO() || numSemaphores || finishedProc ||
502 SkToBool(flags & kSyncCpu_GrFlushFlag)) {
503 result = this->flush(proxy, access, flags, numSemaphores, backendSemaphores,
504 finishedProc, finishedContext);
505 }
506
507 if (!proxy->instantiate(resourceProvider)) {
508 return result;
509 }
510
511 GrSurface* surface = proxy->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 }
520 }
521
522 SkDEBUGCODE(this->validate());
523 return result;
524 }
525
addOnFlushCallbackObject(GrOnFlushCallbackObject * onFlushCBObject)526 void GrDrawingManager::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
527 fOnFlushCBObjects.push_back(onFlushCBObject);
528 }
529
530 #if GR_TEST_UTILS
testingOnly_removeOnFlushCallbackObject(GrOnFlushCallbackObject * cb)531 void GrDrawingManager::testingOnly_removeOnFlushCallbackObject(GrOnFlushCallbackObject* cb) {
532 int n = std::find(fOnFlushCBObjects.begin(), fOnFlushCBObjects.end(), cb) -
533 fOnFlushCBObjects.begin();
534 SkASSERT(n < fOnFlushCBObjects.count());
535 fOnFlushCBObjects.removeShuffle(n);
536 }
537 #endif
538
moveOpListsToDDL(SkDeferredDisplayList * ddl)539 void GrDrawingManager::moveOpListsToDDL(SkDeferredDisplayList* ddl) {
540 SkDEBUGCODE(this->validate());
541
542 // no opList should receive a new command after this
543 fDAG.closeAll(fContext->priv().caps());
544 fActiveOpList = nullptr;
545
546 fDAG.swap(&ddl->fOpLists);
547
548 if (fPathRendererChain) {
549 if (auto ccpr = fPathRendererChain->getCoverageCountingPathRenderer()) {
550 ddl->fPendingPaths = ccpr->detachPendingPaths();
551 }
552 }
553
554 SkDEBUGCODE(this->validate());
555 }
556
copyOpListsFromDDL(const SkDeferredDisplayList * ddl,GrRenderTargetProxy * newDest)557 void GrDrawingManager::copyOpListsFromDDL(const SkDeferredDisplayList* ddl,
558 GrRenderTargetProxy* newDest) {
559 SkDEBUGCODE(this->validate());
560
561 if (fActiveOpList) {
562 // This is a temporary fix for the partial-MDB world. In that world we're not
563 // reordering so ops that (in the single opList world) would've just glommed onto the
564 // end of the single opList but referred to a far earlier RT need to appear in their
565 // own opList.
566 fActiveOpList->makeClosed(*fContext->priv().caps());
567 fActiveOpList = nullptr;
568 }
569
570 // Here we jam the proxy that backs the current replay SkSurface into the LazyProxyData.
571 // The lazy proxy that references it (in the copied opLists) will steal its GrTexture.
572 ddl->fLazyProxyData->fReplayDest = newDest;
573
574 if (ddl->fPendingPaths.size()) {
575 GrCoverageCountingPathRenderer* ccpr = this->getCoverageCountingPathRenderer();
576
577 ccpr->mergePendingPaths(ddl->fPendingPaths);
578 }
579
580 fDAG.add(ddl->fOpLists);
581
582 SkDEBUGCODE(this->validate());
583 }
584
585 #ifdef SK_DEBUG
validate() const586 void GrDrawingManager::validate() const {
587 if (fDAG.sortingOpLists() && fReduceOpListSplitting) {
588 SkASSERT(!fActiveOpList);
589 } else {
590 if (fActiveOpList) {
591 SkASSERT(!fDAG.empty());
592 SkASSERT(!fActiveOpList->isClosed());
593 SkASSERT(fActiveOpList == fDAG.back());
594 }
595
596 for (int i = 0; i < fDAG.numOpLists(); ++i) {
597 if (fActiveOpList != fDAG.opList(i)) {
598 SkASSERT(fDAG.opList(i)->isClosed());
599 }
600 }
601
602 if (!fDAG.empty() && !fDAG.back()->isClosed()) {
603 SkASSERT(fActiveOpList == fDAG.back());
604 }
605 }
606 }
607 #endif
608
newRTOpList(GrRenderTargetProxy * rtp,bool managedOpList)609 sk_sp<GrRenderTargetOpList> GrDrawingManager::newRTOpList(GrRenderTargetProxy* rtp,
610 bool managedOpList) {
611 SkDEBUGCODE(this->validate());
612 SkASSERT(fContext);
613
614 if (fDAG.sortingOpLists() && fReduceOpListSplitting) {
615 // In this case we need to close all the opLists that rely on the current contents of
616 // 'rtp'. That is bc we're going to update the content of the proxy so they need to be
617 // split in case they use both the old and new content. (This is a bit of an overkill:
618 // they really only need to be split if they ever reference proxy's contents again but
619 // that is hard to predict/handle).
620 if (GrOpList* lastOpList = rtp->getLastOpList()) {
621 lastOpList->closeThoseWhoDependOnMe(*fContext->priv().caps());
622 }
623 } else if (fActiveOpList) {
624 // This is a temporary fix for the partial-MDB world. In that world we're not
625 // reordering so ops that (in the single opList world) would've just glommed onto the
626 // end of the single opList but referred to a far earlier RT need to appear in their
627 // own opList.
628 fActiveOpList->makeClosed(*fContext->priv().caps());
629 fActiveOpList = nullptr;
630 }
631
632 // MDB TODO: this is unfortunate. GrOpList only needs the resourceProvider here so that, when
633 // not explicitly allocating resources, it can immediately instantiate 'rtp' so that the use
634 // order matches the allocation order (see the comment in GrOpList's ctor).
635 GrResourceProvider* resourceProvider = nullptr;
636 if (fContext->priv().asDirectContext()) {
637 resourceProvider = fContext->priv().asDirectContext()->priv().resourceProvider();
638 }
639
640 sk_sp<GrRenderTargetOpList> opList(new GrRenderTargetOpList(
641 resourceProvider,
642 fContext->priv().refOpMemoryPool(),
643 rtp,
644 fContext->priv().auditTrail()));
645 SkASSERT(rtp->getLastOpList() == opList.get());
646
647 if (managedOpList) {
648 fDAG.add(opList);
649
650 if (!fDAG.sortingOpLists() || !fReduceOpListSplitting) {
651 fActiveOpList = opList.get();
652 }
653 }
654
655 SkDEBUGCODE(this->validate());
656 return opList;
657 }
658
newTextureOpList(GrTextureProxy * textureProxy)659 sk_sp<GrTextureOpList> GrDrawingManager::newTextureOpList(GrTextureProxy* textureProxy) {
660 SkDEBUGCODE(this->validate());
661 SkASSERT(fContext);
662
663 if (fDAG.sortingOpLists() && fReduceOpListSplitting) {
664 // In this case we need to close all the opLists that rely on the current contents of
665 // 'texture'. That is bc we're going to update the content of the proxy so they need to
666 // be split in case they use both the old and new content. (This is a bit of an
667 // overkill: they really only need to be split if they ever reference proxy's contents
668 // again but that is hard to predict/handle).
669 if (GrOpList* lastOpList = textureProxy->getLastOpList()) {
670 lastOpList->closeThoseWhoDependOnMe(*fContext->priv().caps());
671 }
672 } else if (fActiveOpList) {
673 // This is a temporary fix for the partial-MDB world. In that world we're not
674 // reordering so ops that (in the single opList world) would've just glommed onto the
675 // end of the single opList but referred to a far earlier RT need to appear in their
676 // own opList.
677 fActiveOpList->makeClosed(*fContext->priv().caps());
678 fActiveOpList = nullptr;
679 }
680
681 // MDB TODO: this is unfortunate. GrOpList only needs the resourceProvider here so that, when
682 // not explicitly allocating resources, it can immediately instantiate 'texureProxy' so that
683 // the use order matches the allocation order (see the comment in GrOpList's ctor).
684 GrResourceProvider* resourceProvider = nullptr;
685 if (fContext->priv().asDirectContext()) {
686 resourceProvider = fContext->priv().asDirectContext()->priv().resourceProvider();
687 }
688
689 sk_sp<GrTextureOpList> opList(new GrTextureOpList(resourceProvider,
690 fContext->priv().refOpMemoryPool(),
691 textureProxy,
692 fContext->priv().auditTrail()));
693
694 SkASSERT(textureProxy->getLastOpList() == opList.get());
695
696 fDAG.add(opList);
697 if (!fDAG.sortingOpLists() || !fReduceOpListSplitting) {
698 fActiveOpList = opList.get();
699 }
700
701 SkDEBUGCODE(this->validate());
702 return opList;
703 }
704
getTextContext()705 GrTextContext* GrDrawingManager::getTextContext() {
706 if (!fTextContext) {
707 fTextContext = GrTextContext::Make(fOptionsForTextContext);
708 }
709
710 return fTextContext.get();
711 }
712
713 /*
714 * This method finds a path renderer that can draw the specified path on
715 * the provided target.
716 * Due to its expense, the software path renderer has split out so it can
717 * can be individually allowed/disallowed via the "allowSW" boolean.
718 */
getPathRenderer(const GrPathRenderer::CanDrawPathArgs & args,bool allowSW,GrPathRendererChain::DrawType drawType,GrPathRenderer::StencilSupport * stencilSupport)719 GrPathRenderer* GrDrawingManager::getPathRenderer(const GrPathRenderer::CanDrawPathArgs& args,
720 bool allowSW,
721 GrPathRendererChain::DrawType drawType,
722 GrPathRenderer::StencilSupport* stencilSupport) {
723
724 if (!fPathRendererChain) {
725 fPathRendererChain.reset(new GrPathRendererChain(fContext, fOptionsForPathRendererChain));
726 }
727
728 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(args, drawType, stencilSupport);
729 if (!pr && allowSW) {
730 auto swPR = this->getSoftwarePathRenderer();
731 if (GrPathRenderer::CanDrawPath::kNo != swPR->canDrawPath(args)) {
732 pr = swPR;
733 }
734 }
735
736 return pr;
737 }
738
getSoftwarePathRenderer()739 GrPathRenderer* GrDrawingManager::getSoftwarePathRenderer() {
740 if (!fSoftwarePathRenderer) {
741 fSoftwarePathRenderer.reset(
742 new GrSoftwarePathRenderer(fContext->priv().proxyProvider(),
743 fOptionsForPathRendererChain.fAllowPathMaskCaching));
744 }
745 return fSoftwarePathRenderer.get();
746 }
747
getCoverageCountingPathRenderer()748 GrCoverageCountingPathRenderer* GrDrawingManager::getCoverageCountingPathRenderer() {
749 if (!fPathRendererChain) {
750 fPathRendererChain.reset(new GrPathRendererChain(fContext, fOptionsForPathRendererChain));
751 }
752 return fPathRendererChain->getCoverageCountingPathRenderer();
753 }
754
flushIfNecessary()755 void GrDrawingManager::flushIfNecessary() {
756 auto direct = fContext->priv().asDirectContext();
757 if (!direct) {
758 return;
759 }
760
761 auto resourceCache = direct->priv().getResourceCache();
762 if (resourceCache && resourceCache->requestsFlush()) {
763 this->flush(nullptr, SkSurface::BackendSurfaceAccess::kNoAccess,
764 kNone_GrFlushFlags, 0, nullptr, nullptr, nullptr);
765 resourceCache->purgeAsNeeded();
766 }
767 }
768
makeRenderTargetContext(sk_sp<GrSurfaceProxy> sProxy,sk_sp<SkColorSpace> colorSpace,const SkSurfaceProps * surfaceProps,bool managedOpList)769 sk_sp<GrRenderTargetContext> GrDrawingManager::makeRenderTargetContext(
770 sk_sp<GrSurfaceProxy> sProxy,
771 sk_sp<SkColorSpace> colorSpace,
772 const SkSurfaceProps* surfaceProps,
773 bool managedOpList) {
774 if (this->wasAbandoned() || !sProxy->asRenderTargetProxy()) {
775 return nullptr;
776 }
777
778 // SkSurface catches bad color space usage at creation. This check handles anything that slips
779 // by, including internal usage.
780 if (!SkSurface_Gpu::Valid(fContext->priv().caps(), sProxy->config(), colorSpace.get())) {
781 SkDEBUGFAIL("Invalid config and colorspace combination");
782 return nullptr;
783 }
784
785 sk_sp<GrRenderTargetProxy> renderTargetProxy(sk_ref_sp(sProxy->asRenderTargetProxy()));
786
787 return sk_sp<GrRenderTargetContext>(new GrRenderTargetContext(fContext,
788 std::move(renderTargetProxy),
789 std::move(colorSpace),
790 surfaceProps,
791 managedOpList));
792 }
793
makeTextureContext(sk_sp<GrSurfaceProxy> sProxy,sk_sp<SkColorSpace> colorSpace)794 sk_sp<GrTextureContext> GrDrawingManager::makeTextureContext(sk_sp<GrSurfaceProxy> sProxy,
795 sk_sp<SkColorSpace> colorSpace) {
796 if (this->wasAbandoned() || !sProxy->asTextureProxy()) {
797 return nullptr;
798 }
799
800 // SkSurface catches bad color space usage at creation. This check handles anything that slips
801 // by, including internal usage.
802 if (!SkSurface_Gpu::Valid(fContext->priv().caps(), sProxy->config(), colorSpace.get())) {
803 SkDEBUGFAIL("Invalid config and colorspace combination");
804 return nullptr;
805 }
806
807 // GrTextureRenderTargets should always be using a GrRenderTargetContext
808 SkASSERT(!sProxy->asRenderTargetProxy());
809
810 sk_sp<GrTextureProxy> textureProxy(sk_ref_sp(sProxy->asTextureProxy()));
811
812 return sk_sp<GrTextureContext>(new GrTextureContext(fContext,
813 std::move(textureProxy),
814 std::move(colorSpace)));
815 }
816