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 | 01a9128 | 2018-07-26 08:03:04 -0400 | [diff] [blame] | 13 | #include "GrRenderTargetPriv.h" |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 14 | #include "GrSurfaceProxy.h" |
Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 15 | #include "GrTextureProxyPriv.h" |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 16 | |
Robert Phillips | c589b0b | 2017-04-17 07:53:07 -0400 | [diff] [blame] | 17 | #include "SkAtomics.h" |
| 18 | |
Robert Phillips | c013892 | 2017-03-08 11:50:55 -0500 | [diff] [blame] | 19 | uint32_t GrOpList::CreateUniqueID() { |
| 20 | static int32_t gUniqueID = SK_InvalidUniqueID; |
| 21 | uint32_t id; |
| 22 | // Loop in case our global wraps around, as we never want to return a 0. |
| 23 | do { |
| 24 | id = static_cast<uint32_t>(sk_atomic_inc(&gUniqueID) + 1); |
| 25 | } while (id == SK_InvalidUniqueID); |
| 26 | return id; |
| 27 | } |
| 28 | |
Robert Phillips | c994a93 | 2018-06-19 13:09:54 -0400 | [diff] [blame] | 29 | GrOpList::GrOpList(GrResourceProvider* resourceProvider, sk_sp<GrOpMemoryPool> opMemoryPool, |
Robert Phillips | 5efd5ea | 2017-05-30 13:47:32 -0400 | [diff] [blame] | 30 | GrSurfaceProxy* surfaceProxy, GrAuditTrail* auditTrail) |
Robert Phillips | c994a93 | 2018-06-19 13:09:54 -0400 | [diff] [blame] | 31 | : fOpMemoryPool(std::move(opMemoryPool)) |
| 32 | , fAuditTrail(auditTrail) |
| 33 | , fUniqueID(CreateUniqueID()) |
| 34 | , fFlags(0) { |
| 35 | SkASSERT(fOpMemoryPool); |
Robert Phillips | 5efd5ea | 2017-05-30 13:47:32 -0400 | [diff] [blame] | 36 | fTarget.setProxy(sk_ref_sp(surfaceProxy), kWrite_GrIOType); |
Robert Phillips | 6cdc22c | 2017-05-11 16:29:14 -0400 | [diff] [blame] | 37 | fTarget.get()->setLastOpList(this); |
Robert Phillips | 5efd5ea | 2017-05-30 13:47:32 -0400 | [diff] [blame] | 38 | |
Robert Phillips | 4150eea | 2018-02-07 17:08:21 -0500 | [diff] [blame] | 39 | if (resourceProvider && !resourceProvider->explicitlyAllocateGPUResources()) { |
| 40 | // MDB TODO: remove this! We are currently moving to having all the ops that target |
| 41 | // the RT as a dest (e.g., clear, etc.) rely on the opList's 'fTarget' pointer |
| 42 | // for the IO Ref. This works well but until they are all swapped over (and none |
| 43 | // are pre-emptively instantiating proxies themselves) we need to instantiate |
| 44 | // here so that the GrSurfaces are created in an order that preserves the GrSurface |
| 45 | // re-use assumptions. |
| 46 | fTarget.get()->instantiate(resourceProvider); |
| 47 | } |
| 48 | |
Robert Phillips | 5f567c7 | 2017-09-14 08:27:37 -0400 | [diff] [blame] | 49 | fTarget.markPendingIO(); |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | GrOpList::~GrOpList() { |
Chris Dalton | a84cacf | 2017-10-04 10:30:29 -0600 | [diff] [blame] | 53 | if (fTarget.get() && this == fTarget.get()->getLastOpList()) { |
| 54 | // Ensure the target proxy doesn't keep hold of a dangling back pointer. |
| 55 | fTarget.get()->setLastOpList(nullptr); |
| 56 | } |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 57 | } |
| 58 | |
Robert Phillips | 318c419 | 2017-05-17 09:36:38 -0400 | [diff] [blame] | 59 | bool GrOpList::instantiate(GrResourceProvider* resourceProvider) { |
| 60 | return SkToBool(fTarget.get()->instantiate(resourceProvider)); |
| 61 | } |
| 62 | |
Chris Dalton | a84cacf | 2017-10-04 10:30:29 -0600 | [diff] [blame] | 63 | void GrOpList::endFlush() { |
Robert Phillips | 6cdc22c | 2017-05-11 16:29:14 -0400 | [diff] [blame] | 64 | if (fTarget.get() && this == fTarget.get()->getLastOpList()) { |
| 65 | fTarget.get()->setLastOpList(nullptr); |
| 66 | } |
| 67 | |
Robert Phillips | 5efd5ea | 2017-05-30 13:47:32 -0400 | [diff] [blame] | 68 | fTarget.reset(); |
Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 69 | fDeferredProxies.reset(); |
Robert Phillips | 6cdc22c | 2017-05-11 16:29:14 -0400 | [diff] [blame] | 70 | fAuditTrail = nullptr; |
| 71 | } |
| 72 | |
Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 73 | void GrOpList::instantiateDeferredProxies(GrResourceProvider* resourceProvider) { |
| 74 | for (int i = 0; i < fDeferredProxies.count(); ++i) { |
Robert Phillips | 4150eea | 2018-02-07 17:08:21 -0500 | [diff] [blame] | 75 | if (resourceProvider->explicitlyAllocateGPUResources()) { |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 76 | SkASSERT(fDeferredProxies[i]->isInstantiated()); |
Robert Phillips | 4150eea | 2018-02-07 17:08:21 -0500 | [diff] [blame] | 77 | } else { |
| 78 | fDeferredProxies[i]->instantiate(resourceProvider); |
| 79 | } |
Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 80 | } |
Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 81 | } |
| 82 | |
Brian Osman | 407b342 | 2017-08-22 15:01:32 -0400 | [diff] [blame] | 83 | void GrOpList::prepare(GrOpFlushState* flushState) { |
Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 84 | for (int i = 0; i < fDeferredProxies.count(); ++i) { |
| 85 | fDeferredProxies[i]->texPriv().scheduleUpload(flushState); |
Brian Osman | 407b342 | 2017-08-22 15:01:32 -0400 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | this->onPrepare(flushState); |
| 89 | } |
| 90 | |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 91 | // Add a GrOpList-based dependency |
| 92 | void GrOpList::addDependency(GrOpList* dependedOn) { |
| 93 | SkASSERT(!dependedOn->dependsOn(this)); // loops are bad |
| 94 | |
| 95 | if (this->dependsOn(dependedOn)) { |
| 96 | return; // don't add duplicate dependencies |
| 97 | } |
| 98 | |
Robert Phillips | 73fd0d6 | 2017-09-15 11:48:08 +0000 | [diff] [blame] | 99 | fDependencies.push_back(dependedOn); |
Robert Phillips | e6d0618 | 2018-08-06 16:56:26 -0400 | [diff] [blame] | 100 | dependedOn->addDependent(this); |
| 101 | |
| 102 | SkDEBUGCODE(this->validate()); |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | // Convert from a GrSurface-based dependency to a GrOpList one |
Robert Phillips | ee68365 | 2017-04-26 11:53:10 -0400 | [diff] [blame] | 106 | void GrOpList::addDependency(GrSurfaceProxy* dependedOn, const GrCaps& caps) { |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 107 | if (dependedOn->getLastOpList()) { |
| 108 | // If it is still receiving dependencies, this GrOpList shouldn't be closed |
| 109 | SkASSERT(!this->isClosed()); |
| 110 | |
| 111 | GrOpList* opList = dependedOn->getLastOpList(); |
| 112 | if (opList == this) { |
Robert Phillips | ce3c28f | 2018-07-18 13:52:40 -0400 | [diff] [blame] | 113 | // self-read - presumably for dst reads. We can't make it closed in the self-read case. |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 114 | } else { |
| 115 | this->addDependency(opList); |
| 116 | |
Robert Phillips | ce3c28f | 2018-07-18 13:52:40 -0400 | [diff] [blame] | 117 | // We are closing 'opList' here bc the current contents of it are what 'this' opList |
| 118 | // depends on. We need a break in 'opList' so that the usage of that state has a |
| 119 | // chance to execute. |
Robert Phillips | ee68365 | 2017-04-26 11:53:10 -0400 | [diff] [blame] | 120 | opList->makeClosed(caps); |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 121 | } |
| 122 | } |
Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 123 | |
| 124 | if (GrTextureProxy* textureProxy = dependedOn->asTextureProxy()) { |
| 125 | if (textureProxy->texPriv().isDeferred()) { |
| 126 | fDeferredProxies.push_back(textureProxy); |
| 127 | } |
| 128 | } |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 129 | } |
| 130 | |
Robert Phillips | ce3c28f | 2018-07-18 13:52:40 -0400 | [diff] [blame] | 131 | bool GrOpList::dependsOn(const GrOpList* dependedOn) const { |
| 132 | for (int i = 0; i < fDependencies.count(); ++i) { |
| 133 | if (fDependencies[i] == dependedOn) { |
| 134 | return true; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | return false; |
| 139 | } |
| 140 | |
Robert Phillips | e6d0618 | 2018-08-06 16:56:26 -0400 | [diff] [blame] | 141 | |
| 142 | void GrOpList::addDependent(GrOpList* dependent) { |
| 143 | fDependents.push_back(dependent); |
| 144 | } |
| 145 | |
| 146 | #ifdef SK_DEBUG |
| 147 | bool GrOpList::isDependedent(const GrOpList* dependent) const { |
| 148 | for (int i = 0; i < fDependents.count(); ++i) { |
| 149 | if (fDependents[i] == dependent) { |
| 150 | return true; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | return false; |
| 155 | } |
| 156 | |
| 157 | void GrOpList::validate() const { |
| 158 | // TODO: check for loops and duplicates |
| 159 | |
| 160 | for (int i = 0; i < fDependencies.count(); ++i) { |
| 161 | SkASSERT(fDependencies[i]->isDependedent(this)); |
| 162 | } |
| 163 | } |
| 164 | #endif |
| 165 | |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 166 | bool GrOpList::isInstantiated() const { return fTarget.get()->isInstantiated(); } |
Robert Phillips | 09dfc47 | 2017-09-13 15:25:47 -0400 | [diff] [blame] | 167 | |
Robert Phillips | 01a9128 | 2018-07-26 08:03:04 -0400 | [diff] [blame] | 168 | bool GrOpList::isFullyInstantiated() const { |
| 169 | if (!this->isInstantiated()) { |
| 170 | return false; |
| 171 | } |
| 172 | |
| 173 | GrSurfaceProxy* proxy = fTarget.get(); |
| 174 | bool needsStencil = proxy->asRenderTargetProxy() |
| 175 | ? proxy->asRenderTargetProxy()->needsStencil() |
| 176 | : false; |
| 177 | |
| 178 | if (needsStencil) { |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 179 | GrRenderTarget* rt = proxy->peekRenderTarget(); |
Robert Phillips | 01a9128 | 2018-07-26 08:03:04 -0400 | [diff] [blame] | 180 | |
| 181 | if (!rt->renderTargetPriv().getStencilAttachment()) { |
| 182 | return false; |
| 183 | } |
| 184 | } |
| 185 | |
Robert Phillips | 5b5d84c | 2018-08-09 15:12:18 -0400 | [diff] [blame] | 186 | GrSurface* surface = proxy->peekSurface(); |
| 187 | if (surface->wasDestroyed()) { |
| 188 | return false; |
| 189 | } |
| 190 | |
Robert Phillips | 01a9128 | 2018-07-26 08:03:04 -0400 | [diff] [blame] | 191 | return true; |
| 192 | } |
| 193 | |
Robert Phillips | 4150eea | 2018-02-07 17:08:21 -0500 | [diff] [blame] | 194 | #ifdef SK_DEBUG |
Robert Phillips | ce3c28f | 2018-07-18 13:52:40 -0400 | [diff] [blame] | 195 | static const char* op_to_name(GrLoadOp op) { |
| 196 | return GrLoadOp::kLoad == op ? "load" : GrLoadOp::kClear == op ? "clear" : "discard"; |
| 197 | } |
| 198 | |
Robert Phillips | 2748391 | 2018-04-20 12:43:18 -0400 | [diff] [blame] | 199 | void GrOpList::dump(bool printDependencies) const { |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 200 | SkDebugf("--------------------------------------------------------------\n"); |
Robert Phillips | ba5c439 | 2018-07-25 12:37:14 -0400 | [diff] [blame] | 201 | SkDebugf("opListID: %d - proxyID: %d - surfaceID: %d\n", fUniqueID, |
| 202 | fTarget.get() ? fTarget.get()->uniqueID().asUInt() : -1, |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 203 | fTarget.get() && fTarget.get()->peekSurface() |
| 204 | ? fTarget.get()->peekSurface()->uniqueID().asUInt() |
| 205 | : -1); |
Robert Phillips | ce3c28f | 2018-07-18 13:52:40 -0400 | [diff] [blame] | 206 | SkDebugf("ColorLoadOp: %s %x StencilLoadOp: %s\n", |
| 207 | op_to_name(fColorLoadOp), |
| 208 | GrLoadOp::kClear == fColorLoadOp ? fLoadClearColor : 0x0, |
| 209 | op_to_name(fStencilLoadOp)); |
Robert Phillips | 2748391 | 2018-04-20 12:43:18 -0400 | [diff] [blame] | 210 | |
| 211 | if (printDependencies) { |
Robert Phillips | ce3c28f | 2018-07-18 13:52:40 -0400 | [diff] [blame] | 212 | SkDebugf("I rely On (%d): ", fDependencies.count()); |
Robert Phillips | 2748391 | 2018-04-20 12:43:18 -0400 | [diff] [blame] | 213 | for (int i = 0; i < fDependencies.count(); ++i) { |
| 214 | SkDebugf("%d, ", fDependencies[i]->fUniqueID); |
| 215 | } |
| 216 | SkDebugf("\n"); |
Robert Phillips | e6d0618 | 2018-08-06 16:56:26 -0400 | [diff] [blame] | 217 | |
| 218 | SkDebugf("(%d) Rely On Me: ", fDependents.count()); |
| 219 | for (int i = 0; i < fDependents.count(); ++i) { |
| 220 | SkDebugf("%d, ", fDependents[i]->fUniqueID); |
| 221 | } |
| 222 | SkDebugf("\n"); |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 223 | } |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 224 | } |
| 225 | #endif |