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) { |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | GrRenderTask::~GrRenderTask() { |
| 30 | if (fTarget && this == fTarget->getLastRenderTask()) { |
| 31 | // Ensure the target proxy doesn't keep hold of a dangling back pointer. |
| 32 | fTarget->setLastRenderTask(nullptr); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | #ifdef SK_DEBUG |
| 37 | bool GrRenderTask::deferredProxiesAreInstantiated() const { |
| 38 | for (int i = 0; i < fDeferredProxies.count(); ++i) { |
| 39 | if (!fDeferredProxies[i]->isInstantiated()) { |
| 40 | return false; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | return true; |
| 45 | } |
| 46 | #endif |
| 47 | |
Chris Dalton | aa3cbb8 | 2019-08-21 00:01:21 -0600 | [diff] [blame] | 48 | void GrRenderTask::makeClosed(const GrCaps& caps) { |
| 49 | if (this->isClosed()) { |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | if (ExpectedOutcome::kTargetDirty == this->onMakeClosed(caps)) { |
| 54 | GrTextureProxy* textureProxy = fTarget->asTextureProxy(); |
| 55 | if (textureProxy && GrMipMapped::kYes == textureProxy->mipMapped()) { |
| 56 | textureProxy->markMipMapsDirty(); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | this->setFlag(kClosed_Flag); |
| 61 | } |
| 62 | |
| 63 | |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 64 | void GrRenderTask::prepare(GrOpFlushState* flushState) { |
| 65 | for (int i = 0; i < fDeferredProxies.count(); ++i) { |
| 66 | fDeferredProxies[i]->texPriv().scheduleUpload(flushState); |
| 67 | } |
| 68 | |
| 69 | this->onPrepare(flushState); |
| 70 | } |
| 71 | |
| 72 | // Add a GrRenderTask-based dependency |
| 73 | void GrRenderTask::addDependency(GrRenderTask* dependedOn) { |
| 74 | SkASSERT(!dependedOn->dependsOn(this)); // loops are bad |
| 75 | |
| 76 | if (this->dependsOn(dependedOn)) { |
| 77 | return; // don't add duplicate dependencies |
| 78 | } |
| 79 | |
| 80 | fDependencies.push_back(dependedOn); |
| 81 | dependedOn->addDependent(this); |
| 82 | |
| 83 | SkDEBUGCODE(this->validate()); |
| 84 | } |
| 85 | |
| 86 | // Convert from a GrSurface-based dependency to a GrRenderTask one |
Chris Dalton | 3d77027 | 2019-08-14 09:24:37 -0600 | [diff] [blame] | 87 | void GrRenderTask::addDependency(GrSurfaceProxy* dependedOn, GrMipMapped mipMapped, |
| 88 | GrTextureResolveManager textureResolveManager, |
Chris Dalton | 0875512 | 2019-08-05 16:13:47 -0600 | [diff] [blame] | 89 | const GrCaps& caps) { |
Chris Dalton | aa3cbb8 | 2019-08-21 00:01:21 -0600 | [diff] [blame] | 90 | // If it is still receiving dependencies, this GrRenderTask shouldn't be closed |
| 91 | SkASSERT(!this->isClosed()); |
Chris Dalton | 3d77027 | 2019-08-14 09:24:37 -0600 | [diff] [blame] | 92 | |
Chris Dalton | aa3cbb8 | 2019-08-21 00:01:21 -0600 | [diff] [blame] | 93 | GrRenderTask* dependedOnTask = dependedOn->getLastRenderTask(); |
| 94 | |
| 95 | if (dependedOnTask == this) { |
| 96 | // self-read - presumably for dst reads. We can't make it closed in the self-read case. |
| 97 | SkASSERT(GrMipMapped::kNo == mipMapped); |
| 98 | SkASSERT(!dependedOn->asTextureProxy() || |
| 99 | !dependedOn->asTextureProxy()->texPriv().isDeferred()); |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | if (dependedOnTask) { |
| 104 | // We are closing 'dependedOnTask' here bc the current contents of it are what 'this' |
| 105 | // renderTask depends on. We need a break in 'dependedOnTask' so that the usage of |
| 106 | // that state has a chance to execute. |
| 107 | dependedOnTask->makeClosed(caps); |
| 108 | } |
| 109 | |
| 110 | GrTextureProxy* textureProxy = dependedOn->asTextureProxy(); |
Chris Dalton | 3d77027 | 2019-08-14 09:24:37 -0600 | [diff] [blame] | 111 | if (GrMipMapped::kYes == mipMapped) { |
| 112 | SkASSERT(textureProxy); |
| 113 | if (GrMipMapped::kYes != textureProxy->mipMapped()) { |
| 114 | // There are some cases where we might be given a non-mipmapped texture with a mipmap |
| 115 | // filter. See skbug.com/7094. |
| 116 | mipMapped = GrMipMapped::kNo; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | // Does this proxy have mipmaps that need to be regenerated? |
| 121 | if (GrMipMapped::kYes == mipMapped && textureProxy->mipMapsAreDirty()) { |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame^] | 122 | // Create a renderTask that resolves the texture's mipmap data. |
Chris Dalton | 3d77027 | 2019-08-14 09:24:37 -0600 | [diff] [blame] | 123 | GrRenderTask* textureResolveTask = textureResolveManager.newTextureResolveRenderTask( |
| 124 | sk_ref_sp(textureProxy), GrTextureResolveFlags::kMipMaps, caps); |
| 125 | |
| 126 | // The GrTextureResolveRenderTask factory should have called addDependency (in this |
| 127 | // instance, recursively) on the textureProxy. |
| 128 | SkASSERT(!dependedOnTask || textureResolveTask->dependsOn(dependedOnTask)); |
| 129 | SkASSERT(!textureProxy->texPriv().isDeferred() || |
| 130 | textureResolveTask->fDeferredProxies.back() == textureProxy); |
| 131 | |
Chris Dalton | aa3cbb8 | 2019-08-21 00:01:21 -0600 | [diff] [blame] | 132 | // The GrTextureResolveRenderTask factory should have also marked the mipmaps clean, set the |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame^] | 133 | // last renderTask on the textureProxy to textureResolveTask, and closed textureResolveTask. |
Chris Dalton | 3d77027 | 2019-08-14 09:24:37 -0600 | [diff] [blame] | 134 | SkASSERT(!textureProxy->mipMapsAreDirty()); |
| 135 | SkASSERT(textureProxy->getLastRenderTask() == textureResolveTask); |
Chris Dalton | aa3cbb8 | 2019-08-21 00:01:21 -0600 | [diff] [blame] | 136 | SkASSERT(textureResolveTask->isClosed()); |
Chris Dalton | 3d77027 | 2019-08-14 09:24:37 -0600 | [diff] [blame] | 137 | |
| 138 | // Fall through and add textureResolveTask as a dependency of "this". |
| 139 | dependedOnTask = textureResolveTask; |
| 140 | } else if (textureProxy && textureProxy->texPriv().isDeferred()) { |
| 141 | fDeferredProxies.push_back(textureProxy); |
| 142 | } |
| 143 | |
| 144 | if (dependedOnTask) { |
Chris Dalton | aa3cbb8 | 2019-08-21 00:01:21 -0600 | [diff] [blame] | 145 | this->addDependency(dependedOnTask); |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 146 | } |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | bool GrRenderTask::dependsOn(const GrRenderTask* dependedOn) const { |
| 150 | for (int i = 0; i < fDependencies.count(); ++i) { |
| 151 | if (fDependencies[i] == dependedOn) { |
| 152 | return true; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | |
| 160 | void GrRenderTask::addDependent(GrRenderTask* dependent) { |
| 161 | fDependents.push_back(dependent); |
| 162 | } |
| 163 | |
| 164 | #ifdef SK_DEBUG |
| 165 | bool GrRenderTask::isDependedent(const GrRenderTask* dependent) const { |
| 166 | for (int i = 0; i < fDependents.count(); ++i) { |
| 167 | if (fDependents[i] == dependent) { |
| 168 | return true; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | return false; |
| 173 | } |
| 174 | |
| 175 | void GrRenderTask::validate() const { |
| 176 | // TODO: check for loops and duplicates |
| 177 | |
| 178 | for (int i = 0; i < fDependencies.count(); ++i) { |
| 179 | SkASSERT(fDependencies[i]->isDependedent(this)); |
| 180 | } |
| 181 | } |
| 182 | #endif |
| 183 | |
| 184 | void GrRenderTask::closeThoseWhoDependOnMe(const GrCaps& caps) { |
| 185 | for (int i = 0; i < fDependents.count(); ++i) { |
| 186 | if (!fDependents[i]->isClosed()) { |
| 187 | fDependents[i]->makeClosed(caps); |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | bool GrRenderTask::isInstantiated() const { |
Greg Daniel | bbfec9d | 2019-08-20 10:56:51 -0400 | [diff] [blame] | 193 | // Some renderTasks (e.g. GrTransferFromRenderTask) don't have a target. |
| 194 | if (!fTarget) { |
| 195 | return true; |
| 196 | } |
| 197 | |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 198 | if (!fTarget->isInstantiated()) { |
| 199 | return false; |
| 200 | } |
| 201 | |
| 202 | int minStencilSampleCount = (fTarget->asRenderTargetProxy()) |
| 203 | ? fTarget->asRenderTargetProxy()->numStencilSamples() |
| 204 | : 0; |
| 205 | |
| 206 | if (minStencilSampleCount) { |
| 207 | GrRenderTarget* rt = fTarget->peekRenderTarget(); |
| 208 | SkASSERT(rt); |
| 209 | |
| 210 | GrStencilAttachment* stencil = rt->renderTargetPriv().getStencilAttachment(); |
| 211 | if (!stencil) { |
| 212 | return false; |
| 213 | } |
| 214 | SkASSERT(stencil->numSamples() >= minStencilSampleCount); |
| 215 | } |
| 216 | |
| 217 | GrSurface* surface = fTarget->peekSurface(); |
| 218 | if (surface->wasDestroyed()) { |
| 219 | return false; |
| 220 | } |
| 221 | |
| 222 | return true; |
| 223 | } |
| 224 | |
| 225 | #ifdef SK_DEBUG |
| 226 | void GrRenderTask::dump(bool printDependencies) const { |
| 227 | SkDebugf("--------------------------------------------------------------\n"); |
| 228 | SkDebugf("renderTaskID: %d - proxyID: %d - surfaceID: %d\n", fUniqueID, |
| 229 | fTarget ? fTarget->uniqueID().asUInt() : -1, |
| 230 | fTarget && fTarget->peekSurface() |
| 231 | ? fTarget->peekSurface()->uniqueID().asUInt() |
| 232 | : -1); |
| 233 | |
| 234 | if (printDependencies) { |
| 235 | SkDebugf("I rely On (%d): ", fDependencies.count()); |
| 236 | for (int i = 0; i < fDependencies.count(); ++i) { |
| 237 | SkDebugf("%d, ", fDependencies[i]->fUniqueID); |
| 238 | } |
| 239 | SkDebugf("\n"); |
| 240 | |
| 241 | SkDebugf("(%d) Rely On Me: ", fDependents.count()); |
| 242 | for (int i = 0; i < fDependents.count(); ++i) { |
| 243 | SkDebugf("%d, ", fDependents[i]->fUniqueID); |
| 244 | } |
| 245 | SkDebugf("\n"); |
| 246 | } |
| 247 | } |
| 248 | #endif |