Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2019 Google LLC |
| 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 "src/gpu/GrRenderTask.h" |
| 9 | |
| 10 | #include "src/gpu/GrRenderTargetPriv.h" |
| 11 | #include "src/gpu/GrStencilAttachment.h" |
| 12 | #include "src/gpu/GrTextureProxyPriv.h" |
| 13 | |
| 14 | uint32_t GrRenderTask::CreateUniqueID() { |
| 15 | static std::atomic<uint32_t> nextID{1}; |
| 16 | uint32_t id; |
| 17 | do { |
| 18 | id = nextID++; |
| 19 | } while (id == SK_InvalidUniqueID); |
| 20 | return id; |
| 21 | } |
| 22 | |
| 23 | GrRenderTask::GrRenderTask(sk_sp<GrSurfaceProxy> target) |
| 24 | : fTarget(std::move(target)) |
| 25 | , fUniqueID(CreateUniqueID()) |
| 26 | , fFlags(0) { |
| 27 | fTarget->setLastRenderTask(this); |
| 28 | } |
| 29 | |
| 30 | GrRenderTask::~GrRenderTask() { |
| 31 | if (fTarget && this == fTarget->getLastRenderTask()) { |
| 32 | // Ensure the target proxy doesn't keep hold of a dangling back pointer. |
| 33 | fTarget->setLastRenderTask(nullptr); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | #ifdef SK_DEBUG |
| 38 | bool GrRenderTask::deferredProxiesAreInstantiated() const { |
| 39 | for (int i = 0; i < fDeferredProxies.count(); ++i) { |
| 40 | if (!fDeferredProxies[i]->isInstantiated()) { |
| 41 | return false; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | return true; |
| 46 | } |
| 47 | #endif |
| 48 | |
| 49 | void GrRenderTask::prepare(GrOpFlushState* flushState) { |
| 50 | for (int i = 0; i < fDeferredProxies.count(); ++i) { |
| 51 | fDeferredProxies[i]->texPriv().scheduleUpload(flushState); |
| 52 | } |
| 53 | |
| 54 | this->onPrepare(flushState); |
| 55 | } |
| 56 | |
| 57 | // Add a GrRenderTask-based dependency |
| 58 | void GrRenderTask::addDependency(GrRenderTask* dependedOn) { |
| 59 | SkASSERT(!dependedOn->dependsOn(this)); // loops are bad |
| 60 | |
| 61 | if (this->dependsOn(dependedOn)) { |
| 62 | return; // don't add duplicate dependencies |
| 63 | } |
| 64 | |
| 65 | fDependencies.push_back(dependedOn); |
| 66 | dependedOn->addDependent(this); |
| 67 | |
| 68 | SkDEBUGCODE(this->validate()); |
| 69 | } |
| 70 | |
| 71 | // Convert from a GrSurface-based dependency to a GrRenderTask one |
| 72 | void GrRenderTask::addDependency(GrSurfaceProxy* dependedOn, const GrCaps& caps) { |
| 73 | if (dependedOn->getLastRenderTask()) { |
| 74 | // If it is still receiving dependencies, this GrRenderTask shouldn't be closed |
| 75 | SkASSERT(!this->isClosed()); |
| 76 | |
| 77 | GrRenderTask* dependedOnTask = dependedOn->getLastRenderTask(); |
| 78 | if (dependedOnTask == this) { |
| 79 | // self-read - presumably for dst reads. We can't make it closed in the self-read case. |
| 80 | } else { |
| 81 | this->addDependency(dependedOnTask); |
| 82 | |
| 83 | // We are closing 'dependedOnTask' here bc the current contents of it are what 'this' |
| 84 | // dependedOnTask depends on. We need a break in 'dependedOnTask' so that the usage of |
| 85 | // that state has a chance to execute. |
| 86 | dependedOnTask->makeClosed(caps); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | if (GrTextureProxy* textureProxy = dependedOn->asTextureProxy()) { |
| 91 | if (textureProxy->texPriv().isDeferred()) { |
| 92 | fDeferredProxies.push_back(textureProxy); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | bool GrRenderTask::dependsOn(const GrRenderTask* dependedOn) const { |
| 98 | for (int i = 0; i < fDependencies.count(); ++i) { |
| 99 | if (fDependencies[i] == dependedOn) { |
| 100 | return true; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | |
| 108 | void GrRenderTask::addDependent(GrRenderTask* dependent) { |
| 109 | fDependents.push_back(dependent); |
| 110 | } |
| 111 | |
| 112 | #ifdef SK_DEBUG |
| 113 | bool GrRenderTask::isDependedent(const GrRenderTask* dependent) const { |
| 114 | for (int i = 0; i < fDependents.count(); ++i) { |
| 115 | if (fDependents[i] == dependent) { |
| 116 | return true; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | void GrRenderTask::validate() const { |
| 124 | // TODO: check for loops and duplicates |
| 125 | |
| 126 | for (int i = 0; i < fDependencies.count(); ++i) { |
| 127 | SkASSERT(fDependencies[i]->isDependedent(this)); |
| 128 | } |
| 129 | } |
| 130 | #endif |
| 131 | |
| 132 | void GrRenderTask::closeThoseWhoDependOnMe(const GrCaps& caps) { |
| 133 | for (int i = 0; i < fDependents.count(); ++i) { |
| 134 | if (!fDependents[i]->isClosed()) { |
| 135 | fDependents[i]->makeClosed(caps); |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | bool GrRenderTask::isInstantiated() const { |
| 141 | if (!fTarget->isInstantiated()) { |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | int minStencilSampleCount = (fTarget->asRenderTargetProxy()) |
| 146 | ? fTarget->asRenderTargetProxy()->numStencilSamples() |
| 147 | : 0; |
| 148 | |
| 149 | if (minStencilSampleCount) { |
| 150 | GrRenderTarget* rt = fTarget->peekRenderTarget(); |
| 151 | SkASSERT(rt); |
| 152 | |
| 153 | GrStencilAttachment* stencil = rt->renderTargetPriv().getStencilAttachment(); |
| 154 | if (!stencil) { |
| 155 | return false; |
| 156 | } |
| 157 | SkASSERT(stencil->numSamples() >= minStencilSampleCount); |
| 158 | } |
| 159 | |
| 160 | GrSurface* surface = fTarget->peekSurface(); |
| 161 | if (surface->wasDestroyed()) { |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | return true; |
| 166 | } |
| 167 | |
| 168 | #ifdef SK_DEBUG |
| 169 | void GrRenderTask::dump(bool printDependencies) const { |
| 170 | SkDebugf("--------------------------------------------------------------\n"); |
| 171 | SkDebugf("renderTaskID: %d - proxyID: %d - surfaceID: %d\n", fUniqueID, |
| 172 | fTarget ? fTarget->uniqueID().asUInt() : -1, |
| 173 | fTarget && fTarget->peekSurface() |
| 174 | ? fTarget->peekSurface()->uniqueID().asUInt() |
| 175 | : -1); |
| 176 | |
| 177 | if (printDependencies) { |
| 178 | SkDebugf("I rely On (%d): ", fDependencies.count()); |
| 179 | for (int i = 0; i < fDependencies.count(); ++i) { |
| 180 | SkDebugf("%d, ", fDependencies[i]->fUniqueID); |
| 181 | } |
| 182 | SkDebugf("\n"); |
| 183 | |
| 184 | SkDebugf("(%d) Rely On Me: ", fDependents.count()); |
| 185 | for (int i = 0; i < fDependents.count(); ++i) { |
| 186 | SkDebugf("%d, ", fDependents[i]->fUniqueID); |
| 187 | } |
| 188 | SkDebugf("\n"); |
| 189 | } |
| 190 | } |
| 191 | #endif |