blob: 73495bccc650b041b2c4b0b87369d8a12925337c [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,
153 GrContextOptions::Enable sortOpLists)
154 : fContext(context)
155 , fOptionsForPathRendererChain(optionsForPathRendererChain)
156 , fOptionsForTextContext(optionsForTextContext)
157 , fSingleOwner(singleOwner)
158 , fAbandoned(false)
159 , fDAG(explicitlyAllocating, sortOpLists)
160 , fTextContext(nullptr)
161 , fPathRendererChain(nullptr)
162 , fSoftwarePathRenderer(nullptr)
163 , fFlushing(false) {
164}
165
166void GrDrawingManager::cleanup() {
167 fDAG.cleanup(fContext->contextPriv().caps());
robertphillips3dc6ae52015-10-20 09:54:32 -0700168
robertphillips13391dd2015-10-30 05:15:11 -0700169 fPathRendererChain = nullptr;
Ben Wagner9ec70c62018-07-12 13:30:47 -0400170 fSoftwarePathRenderer = nullptr;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400171
172 fOnFlushCBObjects.reset();
robertphillips3dc6ae52015-10-20 09:54:32 -0700173}
174
175GrDrawingManager::~GrDrawingManager() {
176 this->cleanup();
177}
178
179void GrDrawingManager::abandon() {
180 fAbandoned = true;
181 this->cleanup();
182}
183
robertphillips68737822015-10-29 12:12:21 -0700184void GrDrawingManager::freeGpuResources() {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400185 for (int i = fOnFlushCBObjects.count() - 1; i >= 0; --i) {
186 if (!fOnFlushCBObjects[i]->retainOnFreeGpuResources()) {
187 // it's safe to just do this because we're iterating in reverse
188 fOnFlushCBObjects.removeShuffle(i);
189 }
190 }
191
robertphillips68737822015-10-29 12:12:21 -0700192 // a path renderer may be holding onto resources
robertphillips13391dd2015-10-30 05:15:11 -0700193 fPathRendererChain = nullptr;
Ben Wagner9ec70c62018-07-12 13:30:47 -0400194 fSoftwarePathRenderer = nullptr;
Robert Phillipse3302df2017-04-24 07:31:02 -0400195}
196
Robert Phillips7ee385e2017-03-30 08:02:11 -0400197// MDB TODO: make use of the 'proxy' parameter.
Brian Salomon57d2bea2018-09-10 09:35:41 -0400198GrSemaphoresSubmitted GrDrawingManager::flush(GrSurfaceProxy*,
199 int numSemaphores,
200 GrBackendSemaphore backendSemaphores[]) {
201 GR_CREATE_TRACE_MARKER_CONTEXT("GrDrawingManager", "flush", fContext);
Brian Salomondcbb9d92017-07-19 10:53:20 -0400202
robertphillips7761d612016-05-16 09:14:53 -0700203 if (fFlushing || this->wasAbandoned()) {
Greg Daniel51316782017-08-02 15:10:09 +0000204 return GrSemaphoresSubmitted::kNo;
joshualittb8918c42015-12-18 09:59:46 -0800205 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400206 SkDEBUGCODE(this->validate());
207
Robert Phillips874b5352018-03-16 08:48:24 -0400208 GrGpu* gpu = fContext->contextPriv().getGpu();
209 if (!gpu) {
210 return GrSemaphoresSubmitted::kNo; // Can't flush while DDL recording
211 }
joshualittb8918c42015-12-18 09:59:46 -0800212 fFlushing = true;
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400213
Robert Phillips38d64b02018-09-04 13:23:26 -0400214 // Semi-usually the GrOpLists are already closed at this point, but sometimes Ganesh
215 // needs to flush mid-draw. In that case, the SkGpuDevice's GrOpLists won't be closed
216 // but need to be flushed anyway. Closing such GrOpLists here will mean new
217 // GrOpLists will be created to replace them if the SkGpuDevice(s) write to them again.
Robert Phillips22310d62018-09-05 11:07:21 -0400218 fDAG.closeAll(fContext->contextPriv().caps());
Robert Phillips38d64b02018-09-04 13:23:26 -0400219 fActiveOpList = nullptr;
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400220
Robert Phillips22310d62018-09-05 11:07:21 -0400221 fDAG.prepForFlush();
Robert Phillipsa4c93ac2017-05-18 11:40:04 -0400222
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500223 GrOpFlushState flushState(gpu, fContext->contextPriv().resourceProvider(),
Robert Phillips40a29d72018-01-18 12:59:22 -0500224 &fTokenTracker);
225
Chris Daltonfe199b72017-05-05 11:26:15 -0400226 GrOnFlushResourceProvider onFlushProvider(this);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500227 // TODO: AFAICT the only reason fFlushState is on GrDrawingManager rather than on the
228 // stack here is to preserve the flush tokens.
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400229
Chris Dalton12658942017-10-05 19:45:25 -0600230 // Prepare any onFlush op lists (e.g. atlases).
Chris Daltonfe199b72017-05-05 11:26:15 -0400231 if (!fOnFlushCBObjects.empty()) {
Robert Phillips22310d62018-09-05 11:07:21 -0400232 fDAG.gatherIDs(&fFlushingOpListIDs);
233
Chris Dalton12658942017-10-05 19:45:25 -0600234 SkSTArray<4, sk_sp<GrRenderTargetContext>> renderTargetContexts;
Chris Daltonfe199b72017-05-05 11:26:15 -0400235 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
236 onFlushCBObject->preFlush(&onFlushProvider,
Chris Dalton3968ff92017-11-27 12:26:31 -0700237 fFlushingOpListIDs.begin(), fFlushingOpListIDs.count(),
Chris Daltonfe199b72017-05-05 11:26:15 -0400238 &renderTargetContexts);
Chris Dalton12658942017-10-05 19:45:25 -0600239 for (const sk_sp<GrRenderTargetContext>& rtc : renderTargetContexts) {
Chris Dalton706a6ff2017-11-29 22:01:06 -0700240 sk_sp<GrRenderTargetOpList> onFlushOpList = sk_ref_sp(rtc->getRTOpList());
Chris Dalton12658942017-10-05 19:45:25 -0600241 if (!onFlushOpList) {
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400242 continue; // Odd - but not a big deal
243 }
Chris Dalton706a6ff2017-11-29 22:01:06 -0700244#ifdef SK_DEBUG
245 // OnFlush callbacks are already invoked during flush, and are therefore expected to
246 // handle resource allocation & usage on their own. (No deferred or lazy proxies!)
247 onFlushOpList->visitProxies_debugOnly([](GrSurfaceProxy* p) {
248 SkASSERT(!p->asTextureProxy() || !p->asTextureProxy()->texPriv().isDeferred());
Greg Daniel65fa8ca2018-01-10 17:06:31 -0500249 SkASSERT(GrSurfaceProxy::LazyState::kNot == p->lazyInstantiationState());
Chris Dalton706a6ff2017-11-29 22:01:06 -0700250 });
251#endif
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400252 onFlushOpList->makeClosed(*fContext->contextPriv().caps());
Robert Phillips40a29d72018-01-18 12:59:22 -0500253 onFlushOpList->prepare(&flushState);
Chris Dalton3968ff92017-11-27 12:26:31 -0700254 fOnFlushCBOpLists.push_back(std::move(onFlushOpList));
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400255 }
256 renderTargetContexts.reset();
257 }
258 }
259
robertphillipsa13e2022015-11-11 12:01:09 -0800260#if 0
Brian Salomon09d994e2016-12-21 11:14:46 -0500261 // Enable this to print out verbose GrOp information
Robert Phillipsf2361d22016-10-25 14:20:06 -0400262 for (int i = 0; i < fOpLists.count(); ++i) {
263 SkDEBUGCODE(fOpLists[i]->dump();)
robertphillips3dc6ae52015-10-20 09:54:32 -0700264 }
robertphillipsa13e2022015-11-11 12:01:09 -0800265#endif
266
Robert Phillipseafd48a2017-11-16 07:52:08 -0500267 int startIndex, stopIndex;
268 bool flushed = false;
269
Robert Phillipsf8e25022017-11-08 15:24:31 -0500270 {
Robert Phillips6be756b2018-01-16 15:07:54 -0500271 GrResourceAllocator alloc(fContext->contextPriv().resourceProvider());
Robert Phillips22310d62018-09-05 11:07:21 -0400272 for (int i = 0; i < fDAG.numOpLists(); ++i) {
273 if (fDAG.opList(i)) {
274 fDAG.opList(i)->gatherProxyIntervals(&alloc);
275 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500276 alloc.markEndOfOpList(i);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500277 }
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400278
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500279 GrResourceAllocator::AssignError error = GrResourceAllocator::AssignError::kNoError;
Greg Daniel4684f822018-03-08 15:27:36 -0500280 while (alloc.assign(&startIndex, &stopIndex, flushState.uninstantiateProxyTracker(),
281 &error)) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500282 if (GrResourceAllocator::AssignError::kFailedProxyInstantiation == error) {
283 for (int i = startIndex; i < stopIndex; ++i) {
Robert Phillips22310d62018-09-05 11:07:21 -0400284 if (fDAG.opList(i) && !fDAG.opList(i)->isFullyInstantiated()) {
Robert Phillips01a91282018-07-26 08:03:04 -0400285 // If the backing surface wasn't allocated drop the entire opList.
Robert Phillips22310d62018-09-05 11:07:21 -0400286 fDAG.removeOpList(i);
Robert Phillips01a91282018-07-26 08:03:04 -0400287 }
Robert Phillips22310d62018-09-05 11:07:21 -0400288 if (fDAG.opList(i)) {
289 fDAG.opList(i)->purgeOpsWithUninstantiatedProxies();
Robert Phillipsbbcb7f72018-05-31 11:16:19 -0400290 }
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500291 }
292 }
Robert Phillips4150eea2018-02-07 17:08:21 -0500293
Robert Phillips40a29d72018-01-18 12:59:22 -0500294 if (this->executeOpLists(startIndex, stopIndex, &flushState)) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500295 flushed = true;
296 }
bsalomondc438982016-08-31 11:53:49 -0700297 }
Greg Danielc42b20b2017-10-04 10:34:49 -0400298 }
299
Chris Dalton91ab1552018-04-18 13:24:25 -0600300#ifdef SK_DEBUG
Robert Phillips22310d62018-09-05 11:07:21 -0400301 for (int i = 0; i < fDAG.numOpLists(); ++i) {
Chris Dalton91ab1552018-04-18 13:24:25 -0600302 // If there are any remaining opLists at this point, make sure they will not survive the
303 // flush. Otherwise we need to call endFlush() on them.
304 // http://skbug.com/7111
Robert Phillips22310d62018-09-05 11:07:21 -0400305 SkASSERT(!fDAG.opList(i) || fDAG.opList(i)->unique());
Chris Dalton91ab1552018-04-18 13:24:25 -0600306 }
307#endif
Robert Phillips22310d62018-09-05 11:07:21 -0400308 fDAG.reset();
robertphillipsa13e2022015-11-11 12:01:09 -0800309
Robert Phillipsc994a932018-06-19 13:09:54 -0400310#ifdef SK_DEBUG
311 // In non-DDL mode this checks that all the flushed ops have been freed from the memory pool.
312 // When we move to partial flushes this assert will no longer be valid.
313 // In DDL mode this check is somewhat superfluous since the memory for most of the ops/opLists
314 // will be stored in the DDL's GrOpMemoryPools.
315 GrOpMemoryPool* opMemoryPool = fContext->contextPriv().opMemoryPool();
316 opMemoryPool->isEmpty();
317#endif
318
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500319 GrSemaphoresSubmitted result = gpu->finishFlush(numSemaphores, backendSemaphores);
robertphillipsa13e2022015-11-11 12:01:09 -0800320
Greg Daniel4684f822018-03-08 15:27:36 -0500321 flushState.uninstantiateProxyTracker()->uninstantiateAllProxies();
322
Brian Salomon57d2bea2018-09-10 09:35:41 -0400323 // Give the cache a chance to purge resources that become purgeable due to flushing.
324 if (flushed) {
325 fContext->contextPriv().getResourceCache()->purgeAsNeeded();
bsalomonb77a9072016-09-07 10:02:04 -0700326 }
Chris Daltonfe199b72017-05-05 11:26:15 -0400327 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
Robert Phillips40a29d72018-01-18 12:59:22 -0500328 onFlushCBObject->postFlush(fTokenTracker.nextTokenToFlush(), fFlushingOpListIDs.begin(),
Chris Dalton3968ff92017-11-27 12:26:31 -0700329 fFlushingOpListIDs.count());
Chris Daltonfe199b72017-05-05 11:26:15 -0400330 }
Chris Dalton3968ff92017-11-27 12:26:31 -0700331 fFlushingOpListIDs.reset();
joshualittb8918c42015-12-18 09:59:46 -0800332 fFlushing = false;
Greg Daniel51316782017-08-02 15:10:09 +0000333
334 return result;
robertphillips3dc6ae52015-10-20 09:54:32 -0700335}
336
Robert Phillipseafd48a2017-11-16 07:52:08 -0500337bool GrDrawingManager::executeOpLists(int startIndex, int stopIndex, GrOpFlushState* flushState) {
Robert Phillips22310d62018-09-05 11:07:21 -0400338 SkASSERT(startIndex <= stopIndex && stopIndex <= fDAG.numOpLists());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500339
Robert Phillips27483912018-04-20 12:43:18 -0400340#if GR_FLUSH_TIME_OP_SPEW
341 SkDebugf("Flushing opLists: %d to %d out of [%d, %d]\n",
Robert Phillips22310d62018-09-05 11:07:21 -0400342 startIndex, stopIndex, 0, fDAG.numOpLists());
Robert Phillips27483912018-04-20 12:43:18 -0400343 for (int i = startIndex; i < stopIndex; ++i) {
Robert Phillips22310d62018-09-05 11:07:21 -0400344 if (fDAG.opList(i)) {
345 fDAG.opList(i)->dump(true);
Robert Phillips1734dd32018-08-21 13:52:09 -0400346 }
Robert Phillips27483912018-04-20 12:43:18 -0400347 }
348#endif
349
Robert Phillips4150eea2018-02-07 17:08:21 -0500350 GrResourceProvider* resourceProvider = fContext->contextPriv().resourceProvider();
Robert Phillipseafd48a2017-11-16 07:52:08 -0500351 bool anyOpListsExecuted = false;
352
353 for (int i = startIndex; i < stopIndex; ++i) {
Robert Phillips22310d62018-09-05 11:07:21 -0400354 if (!fDAG.opList(i)) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500355 continue;
356 }
357
Robert Phillips22310d62018-09-05 11:07:21 -0400358 GrOpList* opList = fDAG.opList(i);
359
Robert Phillips4150eea2018-02-07 17:08:21 -0500360 if (resourceProvider->explicitlyAllocateGPUResources()) {
Robert Phillips22310d62018-09-05 11:07:21 -0400361 if (!opList->isFullyInstantiated()) {
Robert Phillips4150eea2018-02-07 17:08:21 -0500362 // If the backing surface wasn't allocated drop the draw of the entire opList.
Robert Phillips22310d62018-09-05 11:07:21 -0400363 fDAG.removeOpList(i);
Robert Phillips4150eea2018-02-07 17:08:21 -0500364 continue;
365 }
366 } else {
Robert Phillips22310d62018-09-05 11:07:21 -0400367 if (!opList->instantiate(resourceProvider)) {
368 fDAG.removeOpList(i);
Robert Phillips4150eea2018-02-07 17:08:21 -0500369 continue;
370 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500371 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500372
373 // TODO: handle this instantiation via lazy surface proxies?
374 // Instantiate all deferred proxies (being built on worker threads) so we can upload them
Robert Phillips22310d62018-09-05 11:07:21 -0400375 opList->instantiateDeferredProxies(fContext->contextPriv().resourceProvider());
376 opList->prepare(flushState);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500377 }
378
379 // Upload all data to the GPU
380 flushState->preExecuteDraws();
381
382 // Execute the onFlush op lists first, if any.
Chris Dalton3968ff92017-11-27 12:26:31 -0700383 for (sk_sp<GrOpList>& onFlushOpList : fOnFlushCBOpLists) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500384 if (!onFlushOpList->execute(flushState)) {
385 SkDebugf("WARNING: onFlushOpList failed to execute.\n");
386 }
387 SkASSERT(onFlushOpList->unique());
388 onFlushOpList = nullptr;
389 }
Chris Dalton3968ff92017-11-27 12:26:31 -0700390 fOnFlushCBOpLists.reset();
Robert Phillipseafd48a2017-11-16 07:52:08 -0500391
392 // Execute the normal op lists.
393 for (int i = startIndex; i < stopIndex; ++i) {
Robert Phillips22310d62018-09-05 11:07:21 -0400394 if (!fDAG.opList(i)) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500395 continue;
396 }
397
Robert Phillips22310d62018-09-05 11:07:21 -0400398 if (fDAG.opList(i)->execute(flushState)) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500399 anyOpListsExecuted = true;
400 }
401 }
402
403 SkASSERT(!flushState->commandBuffer());
Robert Phillips40a29d72018-01-18 12:59:22 -0500404 SkASSERT(fTokenTracker.nextDrawToken() == fTokenTracker.nextTokenToFlush());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500405
406 // We reset the flush state before the OpLists so that the last resources to be freed are those
407 // that are written to in the OpLists. This helps to make sure the most recently used resources
408 // are the last to be purged by the resource cache.
409 flushState->reset();
410
Robert Phillips22310d62018-09-05 11:07:21 -0400411 fDAG.removeOpLists(startIndex, stopIndex);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500412
413 return anyOpListsExecuted;
414}
415
Greg Daniel51316782017-08-02 15:10:09 +0000416GrSemaphoresSubmitted GrDrawingManager::prepareSurfaceForExternalIO(
417 GrSurfaceProxy* proxy, int numSemaphores, GrBackendSemaphore backendSemaphores[]) {
bsalomon6a2b1942016-09-08 11:28:59 -0700418 if (this->wasAbandoned()) {
Greg Daniel51316782017-08-02 15:10:09 +0000419 return GrSemaphoresSubmitted::kNo;
bsalomon6a2b1942016-09-08 11:28:59 -0700420 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400421 SkDEBUGCODE(this->validate());
Robert Phillips7ee385e2017-03-30 08:02:11 -0400422 SkASSERT(proxy);
bsalomon6a2b1942016-09-08 11:28:59 -0700423
Robert Phillips874b5352018-03-16 08:48:24 -0400424 GrGpu* gpu = fContext->contextPriv().getGpu();
425 if (!gpu) {
426 return GrSemaphoresSubmitted::kNo; // Can't flush while DDL recording
427 }
428
Kevin Lubick42846132018-01-05 10:11:11 -0500429 GrSemaphoresSubmitted result = GrSemaphoresSubmitted::kNo;
Greg Daniel51316782017-08-02 15:10:09 +0000430 if (proxy->priv().hasPendingIO() || numSemaphores) {
431 result = this->flush(proxy, numSemaphores, backendSemaphores);
bsalomon6a2b1942016-09-08 11:28:59 -0700432 }
433
Robert Phillips6be756b2018-01-16 15:07:54 -0500434 if (!proxy->instantiate(fContext->contextPriv().resourceProvider())) {
Greg Daniel51316782017-08-02 15:10:09 +0000435 return result;
Robert Phillips7ee385e2017-03-30 08:02:11 -0400436 }
437
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400438 GrSurface* surface = proxy->peekSurface();
Brian Salomon930f9392018-06-20 16:25:26 -0400439 if (auto* rt = surface->asRenderTarget()) {
440 gpu->resolveRenderTarget(rt);
441 }
442 if (auto* tex = surface->asTexture()) {
443 if (tex->texturePriv().mipMapped() == GrMipMapped::kYes &&
444 tex->texturePriv().mipMapsAreDirty()) {
445 gpu->regenerateMipMapLevels(tex);
446 }
bsalomon6a2b1942016-09-08 11:28:59 -0700447 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400448
449 SkDEBUGCODE(this->validate());
Greg Daniel51316782017-08-02 15:10:09 +0000450 return result;
bsalomon6a2b1942016-09-08 11:28:59 -0700451}
452
Chris Daltonfe199b72017-05-05 11:26:15 -0400453void GrDrawingManager::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
454 fOnFlushCBObjects.push_back(onFlushCBObject);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400455}
456
Robert Phillips62000362018-02-01 09:10:04 -0500457void GrDrawingManager::moveOpListsToDDL(SkDeferredDisplayList* ddl) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400458 SkDEBUGCODE(this->validate());
459
460 // no opList should receive a new command after this
Robert Phillips22310d62018-09-05 11:07:21 -0400461 fDAG.closeAll(fContext->contextPriv().caps());
Robert Phillips38d64b02018-09-04 13:23:26 -0400462 fActiveOpList = nullptr;
Robert Phillips7a137052018-02-01 11:23:12 -0500463
Robert Phillips22310d62018-09-05 11:07:21 -0400464 fDAG.swap(&ddl->fOpLists);
Robert Phillips867ce8f2018-06-21 10:28:36 -0400465
Robert Phillips774168e2018-05-31 12:43:27 -0400466 if (fPathRendererChain) {
467 if (auto ccpr = fPathRendererChain->getCoverageCountingPathRenderer()) {
468 ddl->fPendingPaths = ccpr->detachPendingPaths();
469 }
470 }
Robert Phillips38d64b02018-09-04 13:23:26 -0400471
472 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500473}
474
475void GrDrawingManager::copyOpListsFromDDL(const SkDeferredDisplayList* ddl,
476 GrRenderTargetProxy* newDest) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400477 SkDEBUGCODE(this->validate());
478
479 if (fActiveOpList) {
480 // This is a temporary fix for the partial-MDB world. In that world we're not
481 // reordering so ops that (in the single opList world) would've just glommed onto the
482 // end of the single opList but referred to a far earlier RT need to appear in their
483 // own opList.
484 fActiveOpList->makeClosed(*fContext->contextPriv().caps());
485 fActiveOpList = nullptr;
486 }
487
Robert Phillips62000362018-02-01 09:10:04 -0500488 // Here we jam the proxy that backs the current replay SkSurface into the LazyProxyData.
489 // The lazy proxy that references it (in the copied opLists) will steal its GrTexture.
490 ddl->fLazyProxyData->fReplayDest = newDest;
Robert Phillips774168e2018-05-31 12:43:27 -0400491
492 if (ddl->fPendingPaths.size()) {
493 GrCoverageCountingPathRenderer* ccpr = this->getCoverageCountingPathRenderer();
494
495 ccpr->mergePendingPaths(ddl->fPendingPaths);
496 }
Robert Phillips22310d62018-09-05 11:07:21 -0400497
498 fDAG.add(ddl->fOpLists);
Robert Phillips38d64b02018-09-04 13:23:26 -0400499
500 SkDEBUGCODE(this->validate());
Robert Phillips62000362018-02-01 09:10:04 -0500501}
502
Robert Phillips38d64b02018-09-04 13:23:26 -0400503#ifdef SK_DEBUG
504void GrDrawingManager::validate() const {
505 if (fActiveOpList) {
Robert Phillips22310d62018-09-05 11:07:21 -0400506 SkASSERT(!fDAG.empty());
Robert Phillips38d64b02018-09-04 13:23:26 -0400507 SkASSERT(!fActiveOpList->isClosed());
Robert Phillips22310d62018-09-05 11:07:21 -0400508 SkASSERT(fActiveOpList == fDAG.back());
Robert Phillips38d64b02018-09-04 13:23:26 -0400509 }
510
Robert Phillips22310d62018-09-05 11:07:21 -0400511 for (int i = 0; i < fDAG.numOpLists(); ++i) {
512 if (fActiveOpList != fDAG.opList(i)) {
513 SkASSERT(fDAG.opList(i)->isClosed());
Robert Phillips38d64b02018-09-04 13:23:26 -0400514 }
515 }
516
Robert Phillips22310d62018-09-05 11:07:21 -0400517 if (!fDAG.empty() && !fDAG.back()->isClosed()) {
518 SkASSERT(fActiveOpList == fDAG.back());
Robert Phillips38d64b02018-09-04 13:23:26 -0400519 }
520}
521#endif
522
Robert Phillips941d1442017-06-14 16:37:02 -0400523sk_sp<GrRenderTargetOpList> GrDrawingManager::newRTOpList(GrRenderTargetProxy* rtp,
524 bool managedOpList) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400525 SkDEBUGCODE(this->validate());
robertphillips3dc6ae52015-10-20 09:54:32 -0700526 SkASSERT(fContext);
527
Robert Phillips38d64b02018-09-04 13:23:26 -0400528 if (fActiveOpList) {
Robert Phillips6a4e60b2018-07-24 16:09:40 +0000529 // This is a temporary fix for the partial-MDB world. In that world we're not
530 // reordering so ops that (in the single opList world) would've just glommed onto the
531 // end of the single opList but referred to a far earlier RT need to appear in their
532 // own opList.
Robert Phillips38d64b02018-09-04 13:23:26 -0400533 fActiveOpList->makeClosed(*fContext->contextPriv().caps());
534 fActiveOpList = nullptr;
robertphillips3dc6ae52015-10-20 09:54:32 -0700535 }
robertphillips3dc6ae52015-10-20 09:54:32 -0700536
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500537 auto resourceProvider = fContext->contextPriv().resourceProvider();
538
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500539 sk_sp<GrRenderTargetOpList> opList(new GrRenderTargetOpList(
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500540 resourceProvider,
Robert Phillipsc994a932018-06-19 13:09:54 -0400541 fContext->contextPriv().refOpMemoryPool(),
Robert Phillips3a9710b2018-03-27 17:51:55 -0400542 rtp,
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500543 fContext->contextPriv().getAuditTrail()));
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400544 SkASSERT(rtp->getLastOpList() == opList.get());
robertphillips3dc6ae52015-10-20 09:54:32 -0700545
Robert Phillips941d1442017-06-14 16:37:02 -0400546 if (managedOpList) {
Robert Phillips22310d62018-09-05 11:07:21 -0400547 fDAG.add(opList);
Robert Phillips38d64b02018-09-04 13:23:26 -0400548 fActiveOpList = opList.get();
Robert Phillips941d1442017-06-14 16:37:02 -0400549 }
robertphillips3dc6ae52015-10-20 09:54:32 -0700550
Robert Phillips38d64b02018-09-04 13:23:26 -0400551 SkDEBUGCODE(this->validate());
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400552 return opList;
robertphillips3dc6ae52015-10-20 09:54:32 -0700553}
554
Robert Phillipsb6deea82017-05-11 14:14:30 -0400555sk_sp<GrTextureOpList> GrDrawingManager::newTextureOpList(GrTextureProxy* textureProxy) {
Robert Phillips38d64b02018-09-04 13:23:26 -0400556 SkDEBUGCODE(this->validate());
Brian Osman45580d32016-11-23 09:37:01 -0500557 SkASSERT(fContext);
558
Robert Phillips38d64b02018-09-04 13:23:26 -0400559 if (fActiveOpList) {
Robert Phillips6a4e60b2018-07-24 16:09:40 +0000560 // This is a temporary fix for the partial-MDB world. In that world we're not
561 // reordering so ops that (in the single opList world) would've just glommed onto the
562 // end of the single opList but referred to a far earlier RT need to appear in their
563 // own opList.
Robert Phillips38d64b02018-09-04 13:23:26 -0400564 fActiveOpList->makeClosed(*fContext->contextPriv().caps());
565 fActiveOpList = nullptr;
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400566 }
567
Robert Phillips6be756b2018-01-16 15:07:54 -0500568 sk_sp<GrTextureOpList> opList(new GrTextureOpList(fContext->contextPriv().resourceProvider(),
Robert Phillipsc994a932018-06-19 13:09:54 -0400569 fContext->contextPriv().refOpMemoryPool(),
Robert Phillips5efd5ea2017-05-30 13:47:32 -0400570 textureProxy,
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500571 fContext->contextPriv().getAuditTrail()));
Brian Osman45580d32016-11-23 09:37:01 -0500572
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400573 SkASSERT(textureProxy->getLastOpList() == opList.get());
Robert Phillips4a395042017-04-24 16:27:17 +0000574
Robert Phillips22310d62018-09-05 11:07:21 -0400575 fDAG.add(opList);
Robert Phillips38d64b02018-09-04 13:23:26 -0400576 fActiveOpList = opList.get();
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400577
Robert Phillips38d64b02018-09-04 13:23:26 -0400578 SkDEBUGCODE(this->validate());
Robert Phillips4a395042017-04-24 16:27:17 +0000579 return opList;
Brian Osman45580d32016-11-23 09:37:01 -0500580}
581
Herb Derby26cbe512018-05-24 14:39:01 -0400582GrTextContext* GrDrawingManager::getTextContext() {
583 if (!fTextContext) {
584 fTextContext = GrTextContext::Make(fOptionsForTextContext);
brianosman86e76262016-08-11 12:17:31 -0700585 }
586
Herb Derby26cbe512018-05-24 14:39:01 -0400587 return fTextContext.get();
brianosman86e76262016-08-11 12:17:31 -0700588}
589
robertphillips68737822015-10-29 12:12:21 -0700590/*
591 * This method finds a path renderer that can draw the specified path on
592 * the provided target.
593 * Due to its expense, the software path renderer has split out so it can
594 * can be individually allowed/disallowed via the "allowSW" boolean.
595 */
596GrPathRenderer* GrDrawingManager::getPathRenderer(const GrPathRenderer::CanDrawPathArgs& args,
597 bool allowSW,
598 GrPathRendererChain::DrawType drawType,
599 GrPathRenderer::StencilSupport* stencilSupport) {
600
601 if (!fPathRendererChain) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400602 fPathRendererChain.reset(new GrPathRendererChain(fContext, fOptionsForPathRendererChain));
robertphillips68737822015-10-29 12:12:21 -0700603 }
604
605 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(args, drawType, stencilSupport);
606 if (!pr && allowSW) {
Brian Salomone7df0bb2018-05-07 14:44:57 -0400607 auto swPR = this->getSoftwarePathRenderer();
608 if (GrPathRenderer::CanDrawPath::kNo != swPR->canDrawPath(args)) {
609 pr = swPR;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500610 }
robertphillips68737822015-10-29 12:12:21 -0700611 }
612
613 return pr;
614}
615
Brian Salomone7df0bb2018-05-07 14:44:57 -0400616GrPathRenderer* GrDrawingManager::getSoftwarePathRenderer() {
617 if (!fSoftwarePathRenderer) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400618 fSoftwarePathRenderer.reset(
Brian Salomone7df0bb2018-05-07 14:44:57 -0400619 new GrSoftwarePathRenderer(fContext->contextPriv().proxyProvider(),
Ben Wagner9ec70c62018-07-12 13:30:47 -0400620 fOptionsForPathRendererChain.fAllowPathMaskCaching));
Brian Salomone7df0bb2018-05-07 14:44:57 -0400621 }
Ben Wagner9ec70c62018-07-12 13:30:47 -0400622 return fSoftwarePathRenderer.get();
Brian Salomone7df0bb2018-05-07 14:44:57 -0400623}
624
Chris Daltonfddb6c02017-11-04 15:22:22 -0600625GrCoverageCountingPathRenderer* GrDrawingManager::getCoverageCountingPathRenderer() {
626 if (!fPathRendererChain) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400627 fPathRendererChain.reset(new GrPathRendererChain(fContext, fOptionsForPathRendererChain));
Chris Daltonfddb6c02017-11-04 15:22:22 -0600628 }
629 return fPathRendererChain->getCoverageCountingPathRenderer();
630}
631
Brian Salomon653f42f2018-07-10 10:07:31 -0400632void GrDrawingManager::flushIfNecessary() {
633 GrResourceCache* resourceCache = fContext->contextPriv().getResourceCache();
634 if (resourceCache && resourceCache->requestsFlush()) {
Brian Salomon57d2bea2018-09-10 09:35:41 -0400635 this->flush(nullptr, 0, nullptr);
636 resourceCache->purgeAsNeeded();
Brian Salomon653f42f2018-07-10 10:07:31 -0400637 }
638}
639
Brian Osman11052242016-10-27 14:47:55 -0400640sk_sp<GrRenderTargetContext> GrDrawingManager::makeRenderTargetContext(
Robert Phillips37430132016-11-09 06:50:43 -0500641 sk_sp<GrSurfaceProxy> sProxy,
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400642 sk_sp<SkColorSpace> colorSpace,
Robert Phillips941d1442017-06-14 16:37:02 -0400643 const SkSurfaceProps* surfaceProps,
644 bool managedOpList) {
Robert Phillips37430132016-11-09 06:50:43 -0500645 if (this->wasAbandoned() || !sProxy->asRenderTargetProxy()) {
robertphillips3dc6ae52015-10-20 09:54:32 -0700646 return nullptr;
647 }
648
brianosman0e22eb82016-08-30 07:07:59 -0700649 // SkSurface catches bad color space usage at creation. This check handles anything that slips
Brian Salomon366093f2018-02-13 09:25:22 -0500650 // by, including internal usage.
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400651 if (!SkSurface_Gpu::Valid(fContext->contextPriv().caps(), sProxy->config(), colorSpace.get())) {
brianosmana9c3c6a2016-09-29 10:08:36 -0700652 SkDEBUGFAIL("Invalid config and colorspace combination");
brianosman0e22eb82016-08-30 07:07:59 -0700653 return nullptr;
654 }
joshualitt96880d92016-02-16 10:36:53 -0800655
Robert Phillips2c862492017-01-18 10:08:39 -0500656 sk_sp<GrRenderTargetProxy> rtp(sk_ref_sp(sProxy->asRenderTargetProxy()));
657
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500658 return sk_sp<GrRenderTargetContext>(new GrRenderTargetContext(
659 fContext, this, std::move(rtp),
660 std::move(colorSpace),
661 surfaceProps,
662 fContext->contextPriv().getAuditTrail(),
663 fSingleOwner, managedOpList));
robertphillips3dc6ae52015-10-20 09:54:32 -0700664}
Brian Osman45580d32016-11-23 09:37:01 -0500665
Robert Phillips2c862492017-01-18 10:08:39 -0500666sk_sp<GrTextureContext> GrDrawingManager::makeTextureContext(sk_sp<GrSurfaceProxy> sProxy,
667 sk_sp<SkColorSpace> colorSpace) {
Brian Osman45580d32016-11-23 09:37:01 -0500668 if (this->wasAbandoned() || !sProxy->asTextureProxy()) {
669 return nullptr;
670 }
671
Robert Phillips2c862492017-01-18 10:08:39 -0500672 // SkSurface catches bad color space usage at creation. This check handles anything that slips
Brian Salomon366093f2018-02-13 09:25:22 -0500673 // by, including internal usage.
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400674 if (!SkSurface_Gpu::Valid(fContext->contextPriv().caps(), sProxy->config(), colorSpace.get())) {
Robert Phillips2c862492017-01-18 10:08:39 -0500675 SkDEBUGFAIL("Invalid config and colorspace combination");
676 return nullptr;
677 }
678
Robert Phillips383c4182018-02-07 12:47:44 -0500679 // GrTextureRenderTargets should always be using a GrRenderTargetContext
Brian Osman45580d32016-11-23 09:37:01 -0500680 SkASSERT(!sProxy->asRenderTargetProxy());
681
682 sk_sp<GrTextureProxy> textureProxy(sk_ref_sp(sProxy->asTextureProxy()));
683
684 return sk_sp<GrTextureContext>(new GrTextureContext(fContext, this, std::move(textureProxy),
Robert Phillips2c862492017-01-18 10:08:39 -0500685 std::move(colorSpace),
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500686 fContext->contextPriv().getAuditTrail(),
Robert Phillips2c862492017-01-18 10:08:39 -0500687 fSingleOwner));
Brian Osman45580d32016-11-23 09:37:01 -0500688}