Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 "GrOpList.h" |
Robert Phillips | 5efd5ea | 2017-05-30 13:47:32 -0400 | [diff] [blame] | 9 | |
| 10 | #include "GrContext.h" |
Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 11 | #include "GrDeferredProxyUploader.h" |
Robert Phillips | c994a93 | 2018-06-19 13:09:54 -0400 | [diff] [blame] | 12 | #include "GrMemoryPool.h" |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 13 | #include "GrSurfaceProxy.h" |
Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 14 | #include "GrTextureProxyPriv.h" |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 15 | |
Robert Phillips | c589b0b | 2017-04-17 07:53:07 -0400 | [diff] [blame] | 16 | #include "SkAtomics.h" |
| 17 | |
Robert Phillips | c013892 | 2017-03-08 11:50:55 -0500 | [diff] [blame] | 18 | uint32_t GrOpList::CreateUniqueID() { |
| 19 | static int32_t gUniqueID = SK_InvalidUniqueID; |
| 20 | uint32_t id; |
| 21 | // Loop in case our global wraps around, as we never want to return a 0. |
| 22 | do { |
| 23 | id = static_cast<uint32_t>(sk_atomic_inc(&gUniqueID) + 1); |
| 24 | } while (id == SK_InvalidUniqueID); |
| 25 | return id; |
| 26 | } |
| 27 | |
Robert Phillips | c994a93 | 2018-06-19 13:09:54 -0400 | [diff] [blame] | 28 | GrOpList::GrOpList(GrResourceProvider* resourceProvider, sk_sp<GrOpMemoryPool> opMemoryPool, |
Robert Phillips | 5efd5ea | 2017-05-30 13:47:32 -0400 | [diff] [blame] | 29 | GrSurfaceProxy* surfaceProxy, GrAuditTrail* auditTrail) |
Robert Phillips | c994a93 | 2018-06-19 13:09:54 -0400 | [diff] [blame] | 30 | : fOpMemoryPool(std::move(opMemoryPool)) |
| 31 | , fAuditTrail(auditTrail) |
| 32 | , fUniqueID(CreateUniqueID()) |
| 33 | , fFlags(0) { |
| 34 | SkASSERT(fOpMemoryPool); |
Robert Phillips | 5efd5ea | 2017-05-30 13:47:32 -0400 | [diff] [blame] | 35 | fTarget.setProxy(sk_ref_sp(surfaceProxy), kWrite_GrIOType); |
Robert Phillips | 6cdc22c | 2017-05-11 16:29:14 -0400 | [diff] [blame] | 36 | fTarget.get()->setLastOpList(this); |
Robert Phillips | 5efd5ea | 2017-05-30 13:47:32 -0400 | [diff] [blame] | 37 | |
Robert Phillips | 4150eea | 2018-02-07 17:08:21 -0500 | [diff] [blame] | 38 | if (resourceProvider && !resourceProvider->explicitlyAllocateGPUResources()) { |
| 39 | // MDB TODO: remove this! We are currently moving to having all the ops that target |
| 40 | // the RT as a dest (e.g., clear, etc.) rely on the opList's 'fTarget' pointer |
| 41 | // for the IO Ref. This works well but until they are all swapped over (and none |
| 42 | // are pre-emptively instantiating proxies themselves) we need to instantiate |
| 43 | // here so that the GrSurfaces are created in an order that preserves the GrSurface |
| 44 | // re-use assumptions. |
| 45 | fTarget.get()->instantiate(resourceProvider); |
| 46 | } |
| 47 | |
Robert Phillips | 5f567c7 | 2017-09-14 08:27:37 -0400 | [diff] [blame] | 48 | fTarget.markPendingIO(); |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | GrOpList::~GrOpList() { |
Chris Dalton | a84cacf | 2017-10-04 10:30:29 -0600 | [diff] [blame] | 52 | if (fTarget.get() && this == fTarget.get()->getLastOpList()) { |
| 53 | // Ensure the target proxy doesn't keep hold of a dangling back pointer. |
| 54 | fTarget.get()->setLastOpList(nullptr); |
| 55 | } |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 56 | } |
| 57 | |
Robert Phillips | 318c419 | 2017-05-17 09:36:38 -0400 | [diff] [blame] | 58 | bool GrOpList::instantiate(GrResourceProvider* resourceProvider) { |
| 59 | return SkToBool(fTarget.get()->instantiate(resourceProvider)); |
| 60 | } |
| 61 | |
Chris Dalton | a84cacf | 2017-10-04 10:30:29 -0600 | [diff] [blame] | 62 | void GrOpList::endFlush() { |
Robert Phillips | 6cdc22c | 2017-05-11 16:29:14 -0400 | [diff] [blame] | 63 | if (fTarget.get() && this == fTarget.get()->getLastOpList()) { |
| 64 | fTarget.get()->setLastOpList(nullptr); |
| 65 | } |
| 66 | |
Robert Phillips | 5efd5ea | 2017-05-30 13:47:32 -0400 | [diff] [blame] | 67 | fTarget.reset(); |
Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 68 | fDeferredProxies.reset(); |
Robert Phillips | 6cdc22c | 2017-05-11 16:29:14 -0400 | [diff] [blame] | 69 | fAuditTrail = nullptr; |
| 70 | } |
| 71 | |
Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 72 | void GrOpList::instantiateDeferredProxies(GrResourceProvider* resourceProvider) { |
| 73 | for (int i = 0; i < fDeferredProxies.count(); ++i) { |
Robert Phillips | 4150eea | 2018-02-07 17:08:21 -0500 | [diff] [blame] | 74 | if (resourceProvider->explicitlyAllocateGPUResources()) { |
| 75 | SkASSERT(fDeferredProxies[i]->priv().isInstantiated()); |
| 76 | } else { |
| 77 | fDeferredProxies[i]->instantiate(resourceProvider); |
| 78 | } |
Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 79 | } |
Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 80 | } |
| 81 | |
Brian Osman | 407b342 | 2017-08-22 15:01:32 -0400 | [diff] [blame] | 82 | void GrOpList::prepare(GrOpFlushState* flushState) { |
Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 83 | for (int i = 0; i < fDeferredProxies.count(); ++i) { |
| 84 | fDeferredProxies[i]->texPriv().scheduleUpload(flushState); |
Brian Osman | 407b342 | 2017-08-22 15:01:32 -0400 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | this->onPrepare(flushState); |
| 88 | } |
| 89 | |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 90 | // Add a GrOpList-based dependency |
| 91 | void GrOpList::addDependency(GrOpList* dependedOn) { |
| 92 | SkASSERT(!dependedOn->dependsOn(this)); // loops are bad |
| 93 | |
| 94 | if (this->dependsOn(dependedOn)) { |
| 95 | return; // don't add duplicate dependencies |
| 96 | } |
| 97 | |
Robert Phillips | 73fd0d6 | 2017-09-15 11:48:08 +0000 | [diff] [blame] | 98 | fDependencies.push_back(dependedOn); |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | // Convert from a GrSurface-based dependency to a GrOpList one |
Robert Phillips | ee68365 | 2017-04-26 11:53:10 -0400 | [diff] [blame] | 102 | void GrOpList::addDependency(GrSurfaceProxy* dependedOn, const GrCaps& caps) { |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 103 | if (dependedOn->getLastOpList()) { |
| 104 | // If it is still receiving dependencies, this GrOpList shouldn't be closed |
| 105 | SkASSERT(!this->isClosed()); |
| 106 | |
| 107 | GrOpList* opList = dependedOn->getLastOpList(); |
| 108 | if (opList == this) { |
| 109 | // self-read - presumably for dst reads |
| 110 | } else { |
| 111 | this->addDependency(opList); |
| 112 | |
| 113 | // Can't make it closed in the self-read case |
Robert Phillips | ee68365 | 2017-04-26 11:53:10 -0400 | [diff] [blame] | 114 | opList->makeClosed(caps); |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 115 | } |
| 116 | } |
Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 117 | |
| 118 | if (GrTextureProxy* textureProxy = dependedOn->asTextureProxy()) { |
| 119 | if (textureProxy->texPriv().isDeferred()) { |
| 120 | fDeferredProxies.push_back(textureProxy); |
| 121 | } |
| 122 | } |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 123 | } |
| 124 | |
Robert Phillips | 09dfc47 | 2017-09-13 15:25:47 -0400 | [diff] [blame] | 125 | bool GrOpList::isInstantiated() const { |
| 126 | return fTarget.get()->priv().isInstantiated(); |
| 127 | } |
| 128 | |
Robert Phillips | 4150eea | 2018-02-07 17:08:21 -0500 | [diff] [blame] | 129 | #ifdef SK_DEBUG |
Robert Phillips | 2748391 | 2018-04-20 12:43:18 -0400 | [diff] [blame] | 130 | void GrOpList::dump(bool printDependencies) const { |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 131 | SkDebugf("--------------------------------------------------------------\n"); |
Robert Phillips | 2748391 | 2018-04-20 12:43:18 -0400 | [diff] [blame] | 132 | SkDebugf("opList: %d -> RT: %d\n", fUniqueID, fTarget.get() ? fTarget.get()->uniqueID().asUInt() |
| 133 | : -1); |
| 134 | |
| 135 | if (printDependencies) { |
| 136 | SkDebugf("relies On (%d): ", fDependencies.count()); |
| 137 | for (int i = 0; i < fDependencies.count(); ++i) { |
| 138 | SkDebugf("%d, ", fDependencies[i]->fUniqueID); |
| 139 | } |
| 140 | SkDebugf("\n"); |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 141 | } |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 142 | } |
| 143 | #endif |