blob: d481632d97f9fe8f6be0ee0f9119207323dd426c [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 Phillipsa3f70262018-02-08 10:59:38 -050035// Turn on/off the sorting of opLists at flush time
36#ifndef SK_DISABLE_RENDER_TARGET_SORTING
37 #define SK_DISABLE_RENDER_TARGET_SORTING
38#endif
39
Robert Phillipsa3f70262018-02-08 10:59:38 -050040GrDrawingManager::GrDrawingManager(GrContext* context,
41 const GrPathRendererChain::Options& optionsForPathRendererChain,
Herb Derby26cbe512018-05-24 14:39:01 -040042 const GrTextContext::Options& optionsForTextContext,
Robert Phillipsa3f70262018-02-08 10:59:38 -050043 GrSingleOwner* singleOwner,
Robert Phillips64ecdce2018-04-02 10:26:39 -040044 bool explicitlyAllocating,
Robert Phillipsa3f70262018-02-08 10:59:38 -050045 GrContextOptions::Enable sortRenderTargets)
46 : fContext(context)
47 , fOptionsForPathRendererChain(optionsForPathRendererChain)
Herb Derby26cbe512018-05-24 14:39:01 -040048 , fOptionsForTextContext(optionsForTextContext)
Robert Phillipsa3f70262018-02-08 10:59:38 -050049 , fSingleOwner(singleOwner)
50 , fAbandoned(false)
Herb Derby26cbe512018-05-24 14:39:01 -040051 , fTextContext(nullptr)
Robert Phillipsa3f70262018-02-08 10:59:38 -050052 , fPathRendererChain(nullptr)
53 , fSoftwarePathRenderer(nullptr)
54 , fFlushing(false) {
55
56 if (GrContextOptions::Enable::kNo == sortRenderTargets) {
57 fSortRenderTargets = false;
58 } else if (GrContextOptions::Enable::kYes == sortRenderTargets) {
59 fSortRenderTargets = true;
60 } else {
Robert Phillips64ecdce2018-04-02 10:26:39 -040061 // By default we always enable sorting when we're explicitly allocating GPU resources
62 fSortRenderTargets = explicitlyAllocating;
Robert Phillipsa3f70262018-02-08 10:59:38 -050063 }
64}
65
robertphillips3dc6ae52015-10-20 09:54:32 -070066void GrDrawingManager::cleanup() {
Robert Phillipsf2361d22016-10-25 14:20:06 -040067 for (int i = 0; i < fOpLists.count(); ++i) {
Robert Phillipsee683652017-04-26 11:53:10 -040068 // no opList should receive a new command after this
Brian Salomonc7fe0f72018-05-11 10:14:21 -040069 fOpLists[i]->makeClosed(*fContext->contextPriv().caps());
robertphillips0dfa62c2015-11-16 06:23:31 -080070
Robert Phillipsf2361d22016-10-25 14:20:06 -040071 // We shouldn't need to do this, but it turns out some clients still hold onto opLists
Robert Phillips6cdc22c2017-05-11 16:29:14 -040072 // after a cleanup.
73 // MDB TODO: is this still true?
Chris Daltona84cacf2017-10-04 10:30:29 -060074 if (!fOpLists[i]->unique()) {
75 // TODO: Eventually this should be guaranteed unique.
76 // https://bugs.chromium.org/p/skia/issues/detail?id=7111
77 fOpLists[i]->endFlush();
78 }
robertphillips3dc6ae52015-10-20 09:54:32 -070079 }
80
Robert Phillipsf2361d22016-10-25 14:20:06 -040081 fOpLists.reset();
robertphillips3dc6ae52015-10-20 09:54:32 -070082
robertphillips13391dd2015-10-30 05:15:11 -070083 delete fPathRendererChain;
84 fPathRendererChain = nullptr;
robertphillips68737822015-10-29 12:12:21 -070085 SkSafeSetNull(fSoftwarePathRenderer);
Jim Van Verth106b5c42017-09-26 12:45:29 -040086
87 fOnFlushCBObjects.reset();
robertphillips3dc6ae52015-10-20 09:54:32 -070088}
89
90GrDrawingManager::~GrDrawingManager() {
91 this->cleanup();
92}
93
94void GrDrawingManager::abandon() {
95 fAbandoned = true;
96 this->cleanup();
97}
98
robertphillips68737822015-10-29 12:12:21 -070099void GrDrawingManager::freeGpuResources() {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400100 for (int i = fOnFlushCBObjects.count() - 1; i >= 0; --i) {
101 if (!fOnFlushCBObjects[i]->retainOnFreeGpuResources()) {
102 // it's safe to just do this because we're iterating in reverse
103 fOnFlushCBObjects.removeShuffle(i);
104 }
105 }
106
robertphillips68737822015-10-29 12:12:21 -0700107 // a path renderer may be holding onto resources
robertphillips13391dd2015-10-30 05:15:11 -0700108 delete fPathRendererChain;
109 fPathRendererChain = nullptr;
robertphillips68737822015-10-29 12:12:21 -0700110 SkSafeSetNull(fSoftwarePathRenderer);
Robert Phillipse3302df2017-04-24 07:31:02 -0400111}
112
Robert Phillips7ee385e2017-03-30 08:02:11 -0400113// MDB TODO: make use of the 'proxy' parameter.
Greg Daniel51316782017-08-02 15:10:09 +0000114GrSemaphoresSubmitted GrDrawingManager::internalFlush(GrSurfaceProxy*,
115 GrResourceCache::FlushType type,
116 int numSemaphores,
117 GrBackendSemaphore backendSemaphores[]) {
Brian Salomondcbb9d92017-07-19 10:53:20 -0400118 GR_CREATE_TRACE_MARKER_CONTEXT("GrDrawingManager", "internalFlush", fContext);
119
robertphillips7761d612016-05-16 09:14:53 -0700120 if (fFlushing || this->wasAbandoned()) {
Greg Daniel51316782017-08-02 15:10:09 +0000121 return GrSemaphoresSubmitted::kNo;
joshualittb8918c42015-12-18 09:59:46 -0800122 }
Robert Phillips874b5352018-03-16 08:48:24 -0400123 GrGpu* gpu = fContext->contextPriv().getGpu();
124 if (!gpu) {
125 return GrSemaphoresSubmitted::kNo; // Can't flush while DDL recording
126 }
joshualittb8918c42015-12-18 09:59:46 -0800127 fFlushing = true;
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400128
129 for (int i = 0; i < fOpLists.count(); ++i) {
130 // Semi-usually the GrOpLists are already closed at this point, but sometimes Ganesh
131 // needs to flush mid-draw. In that case, the SkGpuDevice's GrOpLists won't be closed
132 // but need to be flushed anyway. Closing such GrOpLists here will mean new
133 // GrOpLists will be created to replace them if the SkGpuDevice(s) write to them again.
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400134 fOpLists[i]->makeClosed(*fContext->contextPriv().caps());
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400135 }
136
Robert Phillipsa4c93ac2017-05-18 11:40:04 -0400137#ifdef SK_DEBUG
138 // This block checks for any unnecessary splits in the opLists. If two sequential opLists
139 // share the same backing GrSurfaceProxy it means the opList was artificially split.
140 if (fOpLists.count()) {
141 GrRenderTargetOpList* prevOpList = fOpLists[0]->asRenderTargetOpList();
142 for (int i = 1; i < fOpLists.count(); ++i) {
143 GrRenderTargetOpList* curOpList = fOpLists[i]->asRenderTargetOpList();
144
145 if (prevOpList && curOpList) {
146 SkASSERT(prevOpList->fTarget.get() != curOpList->fTarget.get());
147 }
148
149 prevOpList = curOpList;
150 }
151 }
152#endif
153
Robert Phillips4150eea2018-02-07 17:08:21 -0500154 if (fSortRenderTargets) {
155 SkDEBUGCODE(bool result =) SkTTopoSort<GrOpList, GrOpList::TopoSortTraits>(&fOpLists);
156 SkASSERT(result);
157 }
robertphillips3dc6ae52015-10-20 09:54:32 -0700158
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500159 GrOpFlushState flushState(gpu, fContext->contextPriv().resourceProvider(),
Robert Phillips40a29d72018-01-18 12:59:22 -0500160 &fTokenTracker);
161
Chris Daltonfe199b72017-05-05 11:26:15 -0400162 GrOnFlushResourceProvider onFlushProvider(this);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500163 // TODO: AFAICT the only reason fFlushState is on GrDrawingManager rather than on the
164 // stack here is to preserve the flush tokens.
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400165
Chris Dalton12658942017-10-05 19:45:25 -0600166 // Prepare any onFlush op lists (e.g. atlases).
Chris Daltonfe199b72017-05-05 11:26:15 -0400167 if (!fOnFlushCBObjects.empty()) {
Chris Dalton3968ff92017-11-27 12:26:31 -0700168 fFlushingOpListIDs.reset(fOpLists.count());
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400169 for (int i = 0; i < fOpLists.count(); ++i) {
Chris Dalton3968ff92017-11-27 12:26:31 -0700170 fFlushingOpListIDs[i] = fOpLists[i]->uniqueID();
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400171 }
Chris Dalton12658942017-10-05 19:45:25 -0600172 SkSTArray<4, sk_sp<GrRenderTargetContext>> renderTargetContexts;
Chris Daltonfe199b72017-05-05 11:26:15 -0400173 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
174 onFlushCBObject->preFlush(&onFlushProvider,
Chris Dalton3968ff92017-11-27 12:26:31 -0700175 fFlushingOpListIDs.begin(), fFlushingOpListIDs.count(),
Chris Daltonfe199b72017-05-05 11:26:15 -0400176 &renderTargetContexts);
Chris Dalton12658942017-10-05 19:45:25 -0600177 for (const sk_sp<GrRenderTargetContext>& rtc : renderTargetContexts) {
Chris Dalton706a6ff2017-11-29 22:01:06 -0700178 sk_sp<GrRenderTargetOpList> onFlushOpList = sk_ref_sp(rtc->getRTOpList());
Chris Dalton12658942017-10-05 19:45:25 -0600179 if (!onFlushOpList) {
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400180 continue; // Odd - but not a big deal
181 }
Chris Dalton706a6ff2017-11-29 22:01:06 -0700182#ifdef SK_DEBUG
183 // OnFlush callbacks are already invoked during flush, and are therefore expected to
184 // handle resource allocation & usage on their own. (No deferred or lazy proxies!)
185 onFlushOpList->visitProxies_debugOnly([](GrSurfaceProxy* p) {
186 SkASSERT(!p->asTextureProxy() || !p->asTextureProxy()->texPriv().isDeferred());
Greg Daniel65fa8ca2018-01-10 17:06:31 -0500187 SkASSERT(GrSurfaceProxy::LazyState::kNot == p->lazyInstantiationState());
Chris Dalton706a6ff2017-11-29 22:01:06 -0700188 });
189#endif
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400190 onFlushOpList->makeClosed(*fContext->contextPriv().caps());
Robert Phillips40a29d72018-01-18 12:59:22 -0500191 onFlushOpList->prepare(&flushState);
Chris Dalton3968ff92017-11-27 12:26:31 -0700192 fOnFlushCBOpLists.push_back(std::move(onFlushOpList));
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400193 }
194 renderTargetContexts.reset();
195 }
196 }
197
robertphillipsa13e2022015-11-11 12:01:09 -0800198#if 0
Brian Salomon09d994e2016-12-21 11:14:46 -0500199 // Enable this to print out verbose GrOp information
Robert Phillipsf2361d22016-10-25 14:20:06 -0400200 for (int i = 0; i < fOpLists.count(); ++i) {
201 SkDEBUGCODE(fOpLists[i]->dump();)
robertphillips3dc6ae52015-10-20 09:54:32 -0700202 }
robertphillipsa13e2022015-11-11 12:01:09 -0800203#endif
204
Robert Phillipseafd48a2017-11-16 07:52:08 -0500205 int startIndex, stopIndex;
206 bool flushed = false;
207
Robert Phillipsf8e25022017-11-08 15:24:31 -0500208 {
Robert Phillips6be756b2018-01-16 15:07:54 -0500209 GrResourceAllocator alloc(fContext->contextPriv().resourceProvider());
Robert Phillipsf8e25022017-11-08 15:24:31 -0500210 for (int i = 0; i < fOpLists.count(); ++i) {
211 fOpLists[i]->gatherProxyIntervals(&alloc);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500212 alloc.markEndOfOpList(i);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500213 }
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400214
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500215 GrResourceAllocator::AssignError error = GrResourceAllocator::AssignError::kNoError;
Greg Daniel4684f822018-03-08 15:27:36 -0500216 while (alloc.assign(&startIndex, &stopIndex, flushState.uninstantiateProxyTracker(),
217 &error)) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500218 if (GrResourceAllocator::AssignError::kFailedProxyInstantiation == error) {
219 for (int i = startIndex; i < stopIndex; ++i) {
Robert Phillipsbbcb7f72018-05-31 11:16:19 -0400220 if (fOpLists[i]) {
221 fOpLists[i]->purgeOpsWithUninstantiatedProxies();
222 }
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500223 }
224 }
Robert Phillips4150eea2018-02-07 17:08:21 -0500225
Robert Phillips40a29d72018-01-18 12:59:22 -0500226 if (this->executeOpLists(startIndex, stopIndex, &flushState)) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500227 flushed = true;
228 }
bsalomondc438982016-08-31 11:53:49 -0700229 }
Greg Danielc42b20b2017-10-04 10:34:49 -0400230 }
231
Chris Dalton91ab1552018-04-18 13:24:25 -0600232#ifdef SK_DEBUG
233 for (const auto& opList : fOpLists) {
234 // If there are any remaining opLists at this point, make sure they will not survive the
235 // flush. Otherwise we need to call endFlush() on them.
236 // http://skbug.com/7111
237 SkASSERT(!opList || opList->unique());
238 }
239#endif
Robert Phillips18e94842017-05-15 13:06:44 -0400240 fOpLists.reset();
robertphillipsa13e2022015-11-11 12:01:09 -0800241
Robert Phillipsc994a932018-06-19 13:09:54 -0400242#ifdef SK_DEBUG
243 // In non-DDL mode this checks that all the flushed ops have been freed from the memory pool.
244 // When we move to partial flushes this assert will no longer be valid.
245 // In DDL mode this check is somewhat superfluous since the memory for most of the ops/opLists
246 // will be stored in the DDL's GrOpMemoryPools.
247 GrOpMemoryPool* opMemoryPool = fContext->contextPriv().opMemoryPool();
248 opMemoryPool->isEmpty();
249#endif
250
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500251 GrSemaphoresSubmitted result = gpu->finishFlush(numSemaphores, backendSemaphores);
robertphillipsa13e2022015-11-11 12:01:09 -0800252
Greg Daniel4684f822018-03-08 15:27:36 -0500253 flushState.uninstantiateProxyTracker()->uninstantiateAllProxies();
254
robertphillipsee843b22016-10-04 05:30:20 -0700255 // We always have to notify the cache when it requested a flush so it can reset its state.
256 if (flushed || type == GrResourceCache::FlushType::kCacheRequested) {
Robert Phillips6be756b2018-01-16 15:07:54 -0500257 fContext->contextPriv().getResourceCache()->notifyFlushOccurred(type);
bsalomonb77a9072016-09-07 10:02:04 -0700258 }
Chris Daltonfe199b72017-05-05 11:26:15 -0400259 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
Robert Phillips40a29d72018-01-18 12:59:22 -0500260 onFlushCBObject->postFlush(fTokenTracker.nextTokenToFlush(), fFlushingOpListIDs.begin(),
Chris Dalton3968ff92017-11-27 12:26:31 -0700261 fFlushingOpListIDs.count());
Chris Daltonfe199b72017-05-05 11:26:15 -0400262 }
Chris Dalton3968ff92017-11-27 12:26:31 -0700263 fFlushingOpListIDs.reset();
joshualittb8918c42015-12-18 09:59:46 -0800264 fFlushing = false;
Greg Daniel51316782017-08-02 15:10:09 +0000265
266 return result;
robertphillips3dc6ae52015-10-20 09:54:32 -0700267}
268
Chris Dalton91ab1552018-04-18 13:24:25 -0600269static void end_oplist_flush_if_not_unique(const sk_sp<GrOpList>& opList) {
270 if (!opList->unique()) {
271 // TODO: Eventually this should be guaranteed unique: http://skbug.com/7111
272 opList->endFlush();
273 }
274}
275
Robert Phillipseafd48a2017-11-16 07:52:08 -0500276bool GrDrawingManager::executeOpLists(int startIndex, int stopIndex, GrOpFlushState* flushState) {
277 SkASSERT(startIndex <= stopIndex && stopIndex <= fOpLists.count());
278
Robert Phillips27483912018-04-20 12:43:18 -0400279#if GR_FLUSH_TIME_OP_SPEW
280 SkDebugf("Flushing opLists: %d to %d out of [%d, %d]\n",
281 startIndex, stopIndex, 0, fOpLists.count());
282 for (int i = startIndex; i < stopIndex; ++i) {
283 fOpLists[i]->dump(false);
284 }
285#endif
286
Robert Phillips4150eea2018-02-07 17:08:21 -0500287 GrResourceProvider* resourceProvider = fContext->contextPriv().resourceProvider();
Robert Phillipseafd48a2017-11-16 07:52:08 -0500288 bool anyOpListsExecuted = false;
289
290 for (int i = startIndex; i < stopIndex; ++i) {
291 if (!fOpLists[i]) {
292 continue;
293 }
294
Robert Phillips4150eea2018-02-07 17:08:21 -0500295 if (resourceProvider->explicitlyAllocateGPUResources()) {
296 if (!fOpLists[i]->isInstantiated()) {
297 // If the backing surface wasn't allocated drop the draw of the entire opList.
Chris Dalton91ab1552018-04-18 13:24:25 -0600298 end_oplist_flush_if_not_unique(fOpLists[i]); // http://skbug.com/7111
Robert Phillips4150eea2018-02-07 17:08:21 -0500299 fOpLists[i] = nullptr;
300 continue;
301 }
302 } else {
303 if (!fOpLists[i]->instantiate(resourceProvider)) {
304 SkDebugf("OpList failed to instantiate.\n");
Chris Dalton91ab1552018-04-18 13:24:25 -0600305 end_oplist_flush_if_not_unique(fOpLists[i]); // http://skbug.com/7111
Robert Phillips4150eea2018-02-07 17:08:21 -0500306 fOpLists[i] = nullptr;
307 continue;
308 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500309 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500310
311 // TODO: handle this instantiation via lazy surface proxies?
312 // Instantiate all deferred proxies (being built on worker threads) so we can upload them
Robert Phillips6be756b2018-01-16 15:07:54 -0500313 fOpLists[i]->instantiateDeferredProxies(fContext->contextPriv().resourceProvider());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500314 fOpLists[i]->prepare(flushState);
315 }
316
317 // Upload all data to the GPU
318 flushState->preExecuteDraws();
319
320 // Execute the onFlush op lists first, if any.
Chris Dalton3968ff92017-11-27 12:26:31 -0700321 for (sk_sp<GrOpList>& onFlushOpList : fOnFlushCBOpLists) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500322 if (!onFlushOpList->execute(flushState)) {
323 SkDebugf("WARNING: onFlushOpList failed to execute.\n");
324 }
325 SkASSERT(onFlushOpList->unique());
326 onFlushOpList = nullptr;
327 }
Chris Dalton3968ff92017-11-27 12:26:31 -0700328 fOnFlushCBOpLists.reset();
Robert Phillipseafd48a2017-11-16 07:52:08 -0500329
330 // Execute the normal op lists.
331 for (int i = startIndex; i < stopIndex; ++i) {
332 if (!fOpLists[i]) {
333 continue;
334 }
335
336 if (fOpLists[i]->execute(flushState)) {
337 anyOpListsExecuted = true;
338 }
339 }
340
341 SkASSERT(!flushState->commandBuffer());
Robert Phillips40a29d72018-01-18 12:59:22 -0500342 SkASSERT(fTokenTracker.nextDrawToken() == fTokenTracker.nextTokenToFlush());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500343
344 // We reset the flush state before the OpLists so that the last resources to be freed are those
345 // that are written to in the OpLists. This helps to make sure the most recently used resources
346 // are the last to be purged by the resource cache.
347 flushState->reset();
348
349 for (int i = startIndex; i < stopIndex; ++i) {
350 if (!fOpLists[i]) {
351 continue;
352 }
Chris Dalton91ab1552018-04-18 13:24:25 -0600353 end_oplist_flush_if_not_unique(fOpLists[i]); // http://skbug.com/7111
Robert Phillipseafd48a2017-11-16 07:52:08 -0500354 fOpLists[i] = nullptr;
355 }
356
357 return anyOpListsExecuted;
358}
359
Greg Daniel51316782017-08-02 15:10:09 +0000360GrSemaphoresSubmitted GrDrawingManager::prepareSurfaceForExternalIO(
361 GrSurfaceProxy* proxy, int numSemaphores, GrBackendSemaphore backendSemaphores[]) {
bsalomon6a2b1942016-09-08 11:28:59 -0700362 if (this->wasAbandoned()) {
Greg Daniel51316782017-08-02 15:10:09 +0000363 return GrSemaphoresSubmitted::kNo;
bsalomon6a2b1942016-09-08 11:28:59 -0700364 }
Robert Phillips7ee385e2017-03-30 08:02:11 -0400365 SkASSERT(proxy);
bsalomon6a2b1942016-09-08 11:28:59 -0700366
Robert Phillips874b5352018-03-16 08:48:24 -0400367 GrGpu* gpu = fContext->contextPriv().getGpu();
368 if (!gpu) {
369 return GrSemaphoresSubmitted::kNo; // Can't flush while DDL recording
370 }
371
Kevin Lubick42846132018-01-05 10:11:11 -0500372 GrSemaphoresSubmitted result = GrSemaphoresSubmitted::kNo;
Greg Daniel51316782017-08-02 15:10:09 +0000373 if (proxy->priv().hasPendingIO() || numSemaphores) {
374 result = this->flush(proxy, numSemaphores, backendSemaphores);
bsalomon6a2b1942016-09-08 11:28:59 -0700375 }
376
Robert Phillips6be756b2018-01-16 15:07:54 -0500377 if (!proxy->instantiate(fContext->contextPriv().resourceProvider())) {
Greg Daniel51316782017-08-02 15:10:09 +0000378 return result;
Robert Phillips7ee385e2017-03-30 08:02:11 -0400379 }
380
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400381 GrSurface* surface = proxy->priv().peekSurface();
Brian Salomon930f9392018-06-20 16:25:26 -0400382 if (auto* rt = surface->asRenderTarget()) {
383 gpu->resolveRenderTarget(rt);
384 }
Brian Salomon94575462018-06-21 17:00:26 -0400385#if 0
386 // This is temporarily is disabled. See comment in SkImage_Gpu.cpp,
387 // new_wrapped_texture_common().
Brian Salomon930f9392018-06-20 16:25:26 -0400388 if (auto* tex = surface->asTexture()) {
389 if (tex->texturePriv().mipMapped() == GrMipMapped::kYes &&
390 tex->texturePriv().mipMapsAreDirty()) {
391 gpu->regenerateMipMapLevels(tex);
392 }
bsalomon6a2b1942016-09-08 11:28:59 -0700393 }
Brian Salomon94575462018-06-21 17:00:26 -0400394#endif
Greg Daniel51316782017-08-02 15:10:09 +0000395 return result;
bsalomon6a2b1942016-09-08 11:28:59 -0700396}
397
Chris Daltonfe199b72017-05-05 11:26:15 -0400398void GrDrawingManager::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
399 fOnFlushCBObjects.push_back(onFlushCBObject);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400400}
401
Robert Phillips62000362018-02-01 09:10:04 -0500402void GrDrawingManager::moveOpListsToDDL(SkDeferredDisplayList* ddl) {
Robert Phillips7a137052018-02-01 11:23:12 -0500403 for (int i = 0; i < fOpLists.count(); ++i) {
404 // no opList should receive a new command after this
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400405 fOpLists[i]->makeClosed(*fContext->contextPriv().caps());
Robert Phillips7a137052018-02-01 11:23:12 -0500406 }
407
Robert Phillips867ce8f2018-06-21 10:28:36 -0400408 SkASSERT(ddl->fOpLists.empty());
409 ddl->fOpLists.swap(fOpLists);
410
Robert Phillips774168e2018-05-31 12:43:27 -0400411 if (fPathRendererChain) {
412 if (auto ccpr = fPathRendererChain->getCoverageCountingPathRenderer()) {
413 ddl->fPendingPaths = ccpr->detachPendingPaths();
414 }
415 }
Robert Phillips62000362018-02-01 09:10:04 -0500416}
417
418void GrDrawingManager::copyOpListsFromDDL(const SkDeferredDisplayList* ddl,
419 GrRenderTargetProxy* newDest) {
Robert Phillips62000362018-02-01 09:10:04 -0500420 // Here we jam the proxy that backs the current replay SkSurface into the LazyProxyData.
421 // The lazy proxy that references it (in the copied opLists) will steal its GrTexture.
422 ddl->fLazyProxyData->fReplayDest = newDest;
Robert Phillips774168e2018-05-31 12:43:27 -0400423
424 if (ddl->fPendingPaths.size()) {
425 GrCoverageCountingPathRenderer* ccpr = this->getCoverageCountingPathRenderer();
426
427 ccpr->mergePendingPaths(ddl->fPendingPaths);
428 }
Robert Phillips62000362018-02-01 09:10:04 -0500429 fOpLists.push_back_n(ddl->fOpLists.count(), ddl->fOpLists.begin());
Robert Phillips62000362018-02-01 09:10:04 -0500430}
431
Robert Phillips941d1442017-06-14 16:37:02 -0400432sk_sp<GrRenderTargetOpList> GrDrawingManager::newRTOpList(GrRenderTargetProxy* rtp,
433 bool managedOpList) {
robertphillips3dc6ae52015-10-20 09:54:32 -0700434 SkASSERT(fContext);
435
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400436 // This is a temporary fix for the partial-MDB world. In that world we're not reordering
437 // so ops that (in the single opList world) would've just glommed onto the end of the single
438 // opList but referred to a far earlier RT need to appear in their own opList.
439 if (!fOpLists.empty()) {
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400440 fOpLists.back()->makeClosed(*fContext->contextPriv().caps());
robertphillips3dc6ae52015-10-20 09:54:32 -0700441 }
robertphillips3dc6ae52015-10-20 09:54:32 -0700442
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500443 auto resourceProvider = fContext->contextPriv().resourceProvider();
444
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500445 sk_sp<GrRenderTargetOpList> opList(new GrRenderTargetOpList(
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500446 resourceProvider,
Robert Phillipsc994a932018-06-19 13:09:54 -0400447 fContext->contextPriv().refOpMemoryPool(),
Robert Phillips3a9710b2018-03-27 17:51:55 -0400448 rtp,
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500449 fContext->contextPriv().getAuditTrail()));
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400450 SkASSERT(rtp->getLastOpList() == opList.get());
robertphillips3dc6ae52015-10-20 09:54:32 -0700451
Robert Phillips941d1442017-06-14 16:37:02 -0400452 if (managedOpList) {
453 fOpLists.push_back() = opList;
454 }
robertphillips3dc6ae52015-10-20 09:54:32 -0700455
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400456 return opList;
robertphillips3dc6ae52015-10-20 09:54:32 -0700457}
458
Robert Phillipsb6deea82017-05-11 14:14:30 -0400459sk_sp<GrTextureOpList> GrDrawingManager::newTextureOpList(GrTextureProxy* textureProxy) {
Brian Osman45580d32016-11-23 09:37:01 -0500460 SkASSERT(fContext);
461
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400462 // This is a temporary fix for the partial-MDB world. In that world we're not reordering
463 // so ops that (in the single opList world) would've just glommed onto the end of the single
464 // opList but referred to a far earlier RT need to appear in their own opList.
465 if (!fOpLists.empty()) {
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400466 fOpLists.back()->makeClosed(*fContext->contextPriv().caps());
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400467 }
468
Robert Phillips6be756b2018-01-16 15:07:54 -0500469 sk_sp<GrTextureOpList> opList(new GrTextureOpList(fContext->contextPriv().resourceProvider(),
Robert Phillipsc994a932018-06-19 13:09:54 -0400470 fContext->contextPriv().refOpMemoryPool(),
Robert Phillips5efd5ea2017-05-30 13:47:32 -0400471 textureProxy,
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500472 fContext->contextPriv().getAuditTrail()));
Brian Osman45580d32016-11-23 09:37:01 -0500473
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400474 SkASSERT(textureProxy->getLastOpList() == opList.get());
Robert Phillips4a395042017-04-24 16:27:17 +0000475
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400476 fOpLists.push_back() = opList;
477
Robert Phillips4a395042017-04-24 16:27:17 +0000478 return opList;
Brian Osman45580d32016-11-23 09:37:01 -0500479}
480
Herb Derby26cbe512018-05-24 14:39:01 -0400481GrTextContext* GrDrawingManager::getTextContext() {
482 if (!fTextContext) {
483 fTextContext = GrTextContext::Make(fOptionsForTextContext);
brianosman86e76262016-08-11 12:17:31 -0700484 }
485
Herb Derby26cbe512018-05-24 14:39:01 -0400486 return fTextContext.get();
brianosman86e76262016-08-11 12:17:31 -0700487}
488
robertphillips68737822015-10-29 12:12:21 -0700489/*
490 * This method finds a path renderer that can draw the specified path on
491 * the provided target.
492 * Due to its expense, the software path renderer has split out so it can
493 * can be individually allowed/disallowed via the "allowSW" boolean.
494 */
495GrPathRenderer* GrDrawingManager::getPathRenderer(const GrPathRenderer::CanDrawPathArgs& args,
496 bool allowSW,
497 GrPathRendererChain::DrawType drawType,
498 GrPathRenderer::StencilSupport* stencilSupport) {
499
500 if (!fPathRendererChain) {
bsalomon6b2552f2016-09-15 13:50:26 -0700501 fPathRendererChain = new GrPathRendererChain(fContext, fOptionsForPathRendererChain);
robertphillips68737822015-10-29 12:12:21 -0700502 }
503
504 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(args, drawType, stencilSupport);
505 if (!pr && allowSW) {
Brian Salomone7df0bb2018-05-07 14:44:57 -0400506 auto swPR = this->getSoftwarePathRenderer();
507 if (GrPathRenderer::CanDrawPath::kNo != swPR->canDrawPath(args)) {
508 pr = swPR;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500509 }
robertphillips68737822015-10-29 12:12:21 -0700510 }
511
512 return pr;
513}
514
Brian Salomone7df0bb2018-05-07 14:44:57 -0400515GrPathRenderer* GrDrawingManager::getSoftwarePathRenderer() {
516 if (!fSoftwarePathRenderer) {
517 fSoftwarePathRenderer =
518 new GrSoftwarePathRenderer(fContext->contextPriv().proxyProvider(),
519 fOptionsForPathRendererChain.fAllowPathMaskCaching);
520 }
521 return fSoftwarePathRenderer;
522}
523
Chris Daltonfddb6c02017-11-04 15:22:22 -0600524GrCoverageCountingPathRenderer* GrDrawingManager::getCoverageCountingPathRenderer() {
525 if (!fPathRendererChain) {
526 fPathRendererChain = new GrPathRendererChain(fContext, fOptionsForPathRendererChain);
527 }
528 return fPathRendererChain->getCoverageCountingPathRenderer();
529}
530
Brian Osman11052242016-10-27 14:47:55 -0400531sk_sp<GrRenderTargetContext> GrDrawingManager::makeRenderTargetContext(
Robert Phillips37430132016-11-09 06:50:43 -0500532 sk_sp<GrSurfaceProxy> sProxy,
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400533 sk_sp<SkColorSpace> colorSpace,
Robert Phillips941d1442017-06-14 16:37:02 -0400534 const SkSurfaceProps* surfaceProps,
535 bool managedOpList) {
Robert Phillips37430132016-11-09 06:50:43 -0500536 if (this->wasAbandoned() || !sProxy->asRenderTargetProxy()) {
robertphillips3dc6ae52015-10-20 09:54:32 -0700537 return nullptr;
538 }
539
brianosman0e22eb82016-08-30 07:07:59 -0700540 // SkSurface catches bad color space usage at creation. This check handles anything that slips
Brian Salomon366093f2018-02-13 09:25:22 -0500541 // by, including internal usage.
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400542 if (!SkSurface_Gpu::Valid(fContext->contextPriv().caps(), sProxy->config(), colorSpace.get())) {
brianosmana9c3c6a2016-09-29 10:08:36 -0700543 SkDEBUGFAIL("Invalid config and colorspace combination");
brianosman0e22eb82016-08-30 07:07:59 -0700544 return nullptr;
545 }
joshualitt96880d92016-02-16 10:36:53 -0800546
Robert Phillips2c862492017-01-18 10:08:39 -0500547 sk_sp<GrRenderTargetProxy> rtp(sk_ref_sp(sProxy->asRenderTargetProxy()));
548
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500549 return sk_sp<GrRenderTargetContext>(new GrRenderTargetContext(
550 fContext, this, std::move(rtp),
551 std::move(colorSpace),
552 surfaceProps,
553 fContext->contextPriv().getAuditTrail(),
554 fSingleOwner, managedOpList));
robertphillips3dc6ae52015-10-20 09:54:32 -0700555}
Brian Osman45580d32016-11-23 09:37:01 -0500556
Robert Phillips2c862492017-01-18 10:08:39 -0500557sk_sp<GrTextureContext> GrDrawingManager::makeTextureContext(sk_sp<GrSurfaceProxy> sProxy,
558 sk_sp<SkColorSpace> colorSpace) {
Brian Osman45580d32016-11-23 09:37:01 -0500559 if (this->wasAbandoned() || !sProxy->asTextureProxy()) {
560 return nullptr;
561 }
562
Robert Phillips2c862492017-01-18 10:08:39 -0500563 // SkSurface catches bad color space usage at creation. This check handles anything that slips
Brian Salomon366093f2018-02-13 09:25:22 -0500564 // by, including internal usage.
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400565 if (!SkSurface_Gpu::Valid(fContext->contextPriv().caps(), sProxy->config(), colorSpace.get())) {
Robert Phillips2c862492017-01-18 10:08:39 -0500566 SkDEBUGFAIL("Invalid config and colorspace combination");
567 return nullptr;
568 }
569
Robert Phillips383c4182018-02-07 12:47:44 -0500570 // GrTextureRenderTargets should always be using a GrRenderTargetContext
Brian Osman45580d32016-11-23 09:37:01 -0500571 SkASSERT(!sProxy->asRenderTargetProxy());
572
573 sk_sp<GrTextureProxy> textureProxy(sk_ref_sp(sProxy->asTextureProxy()));
574
575 return sk_sp<GrTextureContext>(new GrTextureContext(fContext, this, std::move(textureProxy),
Robert Phillips2c862492017-01-18 10:08:39 -0500576 std::move(colorSpace),
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500577 fContext->contextPriv().getAuditTrail(),
Robert Phillips2c862492017-01-18 10:08:39 -0500578 fSingleOwner));
Brian Osman45580d32016-11-23 09:37:01 -0500579}