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" |
Chris Dalton | e2a903e | 2019-09-18 13:41:50 -0600 | [diff] [blame] | 13 | #include "src/gpu/GrTextureResolveRenderTask.h" |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 14 | |
| 15 | uint32_t GrRenderTask::CreateUniqueID() { |
| 16 | static std::atomic<uint32_t> nextID{1}; |
| 17 | uint32_t id; |
| 18 | do { |
| 19 | id = nextID++; |
| 20 | } while (id == SK_InvalidUniqueID); |
| 21 | return id; |
| 22 | } |
| 23 | |
| 24 | GrRenderTask::GrRenderTask(sk_sp<GrSurfaceProxy> target) |
| 25 | : fTarget(std::move(target)) |
| 26 | , fUniqueID(CreateUniqueID()) |
| 27 | , fFlags(0) { |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 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 | |
Chris Dalton | aa3cbb8 | 2019-08-21 00:01:21 -0600 | [diff] [blame] | 49 | void GrRenderTask::makeClosed(const GrCaps& caps) { |
| 50 | if (this->isClosed()) { |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | if (ExpectedOutcome::kTargetDirty == this->onMakeClosed(caps)) { |
Chris Dalton | 4ece96d | 2019-08-30 11:26:39 -0600 | [diff] [blame] | 55 | if (fTarget->requiresManualMSAAResolve()) { |
| 56 | SkASSERT(fTarget->asRenderTargetProxy()); |
| 57 | fTarget->asRenderTargetProxy()->markMSAADirty(); |
| 58 | } |
Chris Dalton | aa3cbb8 | 2019-08-21 00:01:21 -0600 | [diff] [blame] | 59 | GrTextureProxy* textureProxy = fTarget->asTextureProxy(); |
| 60 | if (textureProxy && GrMipMapped::kYes == textureProxy->mipMapped()) { |
| 61 | textureProxy->markMipMapsDirty(); |
| 62 | } |
| 63 | } |
| 64 | |
Chris Dalton | e2a903e | 2019-09-18 13:41:50 -0600 | [diff] [blame] | 65 | if (fTextureResolveTask) { |
| 66 | this->addDependency(fTextureResolveTask); |
| 67 | fTextureResolveTask->makeClosed(caps); |
| 68 | fTextureResolveTask = nullptr; |
| 69 | } |
| 70 | |
Chris Dalton | aa3cbb8 | 2019-08-21 00:01:21 -0600 | [diff] [blame] | 71 | this->setFlag(kClosed_Flag); |
| 72 | } |
| 73 | |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 74 | void GrRenderTask::prepare(GrOpFlushState* flushState) { |
| 75 | for (int i = 0; i < fDeferredProxies.count(); ++i) { |
| 76 | fDeferredProxies[i]->texPriv().scheduleUpload(flushState); |
| 77 | } |
| 78 | |
| 79 | this->onPrepare(flushState); |
| 80 | } |
| 81 | |
| 82 | // Add a GrRenderTask-based dependency |
| 83 | void GrRenderTask::addDependency(GrRenderTask* dependedOn) { |
| 84 | SkASSERT(!dependedOn->dependsOn(this)); // loops are bad |
Chris Dalton | dc9a74f | 2019-09-18 10:26:16 -0600 | [diff] [blame] | 85 | SkASSERT(!this->dependsOn(dependedOn)); // caller should weed out duplicates |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 86 | |
| 87 | fDependencies.push_back(dependedOn); |
| 88 | dependedOn->addDependent(this); |
| 89 | |
| 90 | SkDEBUGCODE(this->validate()); |
| 91 | } |
| 92 | |
Greg Daniel | c30f1a9 | 2019-09-06 15:28:58 -0400 | [diff] [blame] | 93 | void GrRenderTask::addDependenciesFromOtherTask(GrRenderTask* otherTask) { |
| 94 | SkASSERT(otherTask); |
Chris Dalton | dc9a74f | 2019-09-18 10:26:16 -0600 | [diff] [blame] | 95 | for (GrRenderTask* task : otherTask->fDependencies) { |
Greg Daniel | c30f1a9 | 2019-09-06 15:28:58 -0400 | [diff] [blame] | 96 | // The task should not be adding a dependency to itself. |
Chris Dalton | dc9a74f | 2019-09-18 10:26:16 -0600 | [diff] [blame] | 97 | SkASSERT(task != this); |
| 98 | if (!this->dependsOn(task)) { |
| 99 | this->addDependency(task); |
| 100 | } |
Greg Daniel | c30f1a9 | 2019-09-06 15:28:58 -0400 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 104 | // Convert from a GrSurface-based dependency to a GrRenderTask one |
Chris Dalton | 3d77027 | 2019-08-14 09:24:37 -0600 | [diff] [blame] | 105 | void GrRenderTask::addDependency(GrSurfaceProxy* dependedOn, GrMipMapped mipMapped, |
| 106 | GrTextureResolveManager textureResolveManager, |
Chris Dalton | 0875512 | 2019-08-05 16:13:47 -0600 | [diff] [blame] | 107 | const GrCaps& caps) { |
Chris Dalton | aa3cbb8 | 2019-08-21 00:01:21 -0600 | [diff] [blame] | 108 | // If it is still receiving dependencies, this GrRenderTask shouldn't be closed |
| 109 | SkASSERT(!this->isClosed()); |
Chris Dalton | 3d77027 | 2019-08-14 09:24:37 -0600 | [diff] [blame] | 110 | |
Chris Dalton | aa3cbb8 | 2019-08-21 00:01:21 -0600 | [diff] [blame] | 111 | GrRenderTask* dependedOnTask = dependedOn->getLastRenderTask(); |
| 112 | |
| 113 | if (dependedOnTask == this) { |
Chris Dalton | 4ece96d | 2019-08-30 11:26:39 -0600 | [diff] [blame] | 114 | // self-read - presumably for dst reads. We don't need to do anything in this case. The |
| 115 | // XferProcessor will detect what is happening and insert a texture barrier. |
Chris Dalton | aa3cbb8 | 2019-08-21 00:01:21 -0600 | [diff] [blame] | 116 | SkASSERT(GrMipMapped::kNo == mipMapped); |
Chris Dalton | 4ece96d | 2019-08-30 11:26:39 -0600 | [diff] [blame] | 117 | // We should never attempt a self-read on a surface that has a separate MSAA renderbuffer. |
| 118 | SkASSERT(!dependedOn->requiresManualMSAAResolve()); |
Chris Dalton | aa3cbb8 | 2019-08-21 00:01:21 -0600 | [diff] [blame] | 119 | SkASSERT(!dependedOn->asTextureProxy() || |
| 120 | !dependedOn->asTextureProxy()->texPriv().isDeferred()); |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | if (dependedOnTask) { |
Chris Dalton | e2a903e | 2019-09-18 13:41:50 -0600 | [diff] [blame] | 125 | if (this->dependsOn(dependedOnTask) || fTextureResolveTask == dependedOnTask) { |
| 126 | return; // don't add duplicate dependencies |
| 127 | } |
| 128 | |
Chris Dalton | aa3cbb8 | 2019-08-21 00:01:21 -0600 | [diff] [blame] | 129 | // We are closing 'dependedOnTask' here bc the current contents of it are what 'this' |
| 130 | // renderTask depends on. We need a break in 'dependedOnTask' so that the usage of |
| 131 | // that state has a chance to execute. |
| 132 | dependedOnTask->makeClosed(caps); |
| 133 | } |
| 134 | |
Chris Dalton | 4ece96d | 2019-08-30 11:26:39 -0600 | [diff] [blame] | 135 | auto resolveFlags = GrSurfaceProxy::ResolveFlags::kNone; |
| 136 | |
| 137 | if (dependedOn->requiresManualMSAAResolve()) { |
| 138 | auto* renderTargetProxy = dependedOn->asRenderTargetProxy(); |
| 139 | SkASSERT(renderTargetProxy); |
| 140 | if (renderTargetProxy->isMSAADirty()) { |
| 141 | resolveFlags |= GrSurfaceProxy::ResolveFlags::kMSAA; |
| 142 | } |
| 143 | } |
| 144 | |
Chris Dalton | aa3cbb8 | 2019-08-21 00:01:21 -0600 | [diff] [blame] | 145 | GrTextureProxy* textureProxy = dependedOn->asTextureProxy(); |
Chris Dalton | 3d77027 | 2019-08-14 09:24:37 -0600 | [diff] [blame] | 146 | if (GrMipMapped::kYes == mipMapped) { |
| 147 | SkASSERT(textureProxy); |
| 148 | if (GrMipMapped::kYes != textureProxy->mipMapped()) { |
| 149 | // There are some cases where we might be given a non-mipmapped texture with a mipmap |
| 150 | // filter. See skbug.com/7094. |
| 151 | mipMapped = GrMipMapped::kNo; |
Chris Dalton | 4ece96d | 2019-08-30 11:26:39 -0600 | [diff] [blame] | 152 | } else if (textureProxy->mipMapsAreDirty()) { |
| 153 | resolveFlags |= GrSurfaceProxy::ResolveFlags::kMipMaps; |
Chris Dalton | 3d77027 | 2019-08-14 09:24:37 -0600 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
Chris Dalton | 4ece96d | 2019-08-30 11:26:39 -0600 | [diff] [blame] | 157 | // Does this proxy have msaa to resolve and/or mipmaps to regenerate? |
| 158 | if (GrSurfaceProxy::ResolveFlags::kNone != resolveFlags) { |
Chris Dalton | e2a903e | 2019-09-18 13:41:50 -0600 | [diff] [blame] | 159 | if (!fTextureResolveTask) { |
| 160 | fTextureResolveTask = textureResolveManager.newTextureResolveRenderTask(caps); |
| 161 | } |
| 162 | fTextureResolveTask->addProxy(sk_ref_sp(dependedOn), resolveFlags, caps); |
| 163 | |
| 164 | // addProxy() should have closed the texture proxy's previous task. |
| 165 | SkASSERT(!dependedOnTask || dependedOnTask->isClosed()); |
| 166 | SkASSERT(dependedOn->getLastRenderTask() == fTextureResolveTask); |
Chris Dalton | 3d77027 | 2019-08-14 09:24:37 -0600 | [diff] [blame] | 167 | |
Chris Dalton | 4ece96d | 2019-08-30 11:26:39 -0600 | [diff] [blame] | 168 | #ifdef SK_DEBUG |
Chris Dalton | e2a903e | 2019-09-18 13:41:50 -0600 | [diff] [blame] | 169 | // addProxy() should have called addDependency (in this instance, recursively) on |
| 170 | // fTextureResolveTask. |
Chris Dalton | 4ece96d | 2019-08-30 11:26:39 -0600 | [diff] [blame] | 171 | if (dependedOnTask) { |
Chris Dalton | e2a903e | 2019-09-18 13:41:50 -0600 | [diff] [blame] | 172 | SkASSERT(fTextureResolveTask->dependsOn(dependedOnTask)); |
Chris Dalton | 4ece96d | 2019-08-30 11:26:39 -0600 | [diff] [blame] | 173 | } |
| 174 | if (textureProxy && textureProxy->texPriv().isDeferred()) { |
Chris Dalton | e2a903e | 2019-09-18 13:41:50 -0600 | [diff] [blame] | 175 | SkASSERT(fTextureResolveTask->fDeferredProxies.back() == textureProxy); |
Chris Dalton | 4ece96d | 2019-08-30 11:26:39 -0600 | [diff] [blame] | 176 | } |
Chris Dalton | 3d77027 | 2019-08-14 09:24:37 -0600 | [diff] [blame] | 177 | |
Chris Dalton | 4ece96d | 2019-08-30 11:26:39 -0600 | [diff] [blame] | 178 | // The GrTextureResolveRenderTask factory should have also marked the proxy clean, set the |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 179 | // last renderTask on the textureProxy to textureResolveTask, and closed textureResolveTask. |
Chris Dalton | 4ece96d | 2019-08-30 11:26:39 -0600 | [diff] [blame] | 180 | if (GrRenderTargetProxy* renderTargetProxy = dependedOn->asRenderTargetProxy()) { |
| 181 | SkASSERT(!renderTargetProxy->isMSAADirty()); |
| 182 | } |
| 183 | if (textureProxy) { |
| 184 | SkASSERT(!textureProxy->mipMapsAreDirty()); |
| 185 | } |
Chris Dalton | e2a903e | 2019-09-18 13:41:50 -0600 | [diff] [blame] | 186 | SkASSERT(dependedOn->getLastRenderTask() == fTextureResolveTask); |
Chris Dalton | 4ece96d | 2019-08-30 11:26:39 -0600 | [diff] [blame] | 187 | #endif |
Chris Dalton | e2a903e | 2019-09-18 13:41:50 -0600 | [diff] [blame] | 188 | return; |
| 189 | } |
Chris Dalton | 3d77027 | 2019-08-14 09:24:37 -0600 | [diff] [blame] | 190 | |
Chris Dalton | e2a903e | 2019-09-18 13:41:50 -0600 | [diff] [blame] | 191 | if (textureProxy && textureProxy->texPriv().isDeferred()) { |
Chris Dalton | 3d77027 | 2019-08-14 09:24:37 -0600 | [diff] [blame] | 192 | fDeferredProxies.push_back(textureProxy); |
| 193 | } |
| 194 | |
| 195 | if (dependedOnTask) { |
Chris Dalton | aa3cbb8 | 2019-08-21 00:01:21 -0600 | [diff] [blame] | 196 | this->addDependency(dependedOnTask); |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 197 | } |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | bool GrRenderTask::dependsOn(const GrRenderTask* dependedOn) const { |
| 201 | for (int i = 0; i < fDependencies.count(); ++i) { |
| 202 | if (fDependencies[i] == dependedOn) { |
| 203 | return true; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | return false; |
| 208 | } |
| 209 | |
| 210 | |
| 211 | void GrRenderTask::addDependent(GrRenderTask* dependent) { |
| 212 | fDependents.push_back(dependent); |
| 213 | } |
| 214 | |
| 215 | #ifdef SK_DEBUG |
| 216 | bool GrRenderTask::isDependedent(const GrRenderTask* dependent) const { |
| 217 | for (int i = 0; i < fDependents.count(); ++i) { |
| 218 | if (fDependents[i] == dependent) { |
| 219 | return true; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | return false; |
| 224 | } |
| 225 | |
| 226 | void GrRenderTask::validate() const { |
| 227 | // TODO: check for loops and duplicates |
| 228 | |
| 229 | for (int i = 0; i < fDependencies.count(); ++i) { |
| 230 | SkASSERT(fDependencies[i]->isDependedent(this)); |
| 231 | } |
| 232 | } |
| 233 | #endif |
| 234 | |
| 235 | void GrRenderTask::closeThoseWhoDependOnMe(const GrCaps& caps) { |
| 236 | for (int i = 0; i < fDependents.count(); ++i) { |
| 237 | if (!fDependents[i]->isClosed()) { |
| 238 | fDependents[i]->makeClosed(caps); |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | bool GrRenderTask::isInstantiated() const { |
Greg Daniel | bbfec9d | 2019-08-20 10:56:51 -0400 | [diff] [blame] | 244 | // Some renderTasks (e.g. GrTransferFromRenderTask) don't have a target. |
| 245 | if (!fTarget) { |
| 246 | return true; |
| 247 | } |
| 248 | |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 249 | if (!fTarget->isInstantiated()) { |
| 250 | return false; |
| 251 | } |
| 252 | |
| 253 | int minStencilSampleCount = (fTarget->asRenderTargetProxy()) |
| 254 | ? fTarget->asRenderTargetProxy()->numStencilSamples() |
| 255 | : 0; |
| 256 | |
| 257 | if (minStencilSampleCount) { |
| 258 | GrRenderTarget* rt = fTarget->peekRenderTarget(); |
| 259 | SkASSERT(rt); |
| 260 | |
| 261 | GrStencilAttachment* stencil = rt->renderTargetPriv().getStencilAttachment(); |
| 262 | if (!stencil) { |
| 263 | return false; |
| 264 | } |
| 265 | SkASSERT(stencil->numSamples() >= minStencilSampleCount); |
| 266 | } |
| 267 | |
| 268 | GrSurface* surface = fTarget->peekSurface(); |
| 269 | if (surface->wasDestroyed()) { |
| 270 | return false; |
| 271 | } |
| 272 | |
| 273 | return true; |
| 274 | } |
| 275 | |
| 276 | #ifdef SK_DEBUG |
| 277 | void GrRenderTask::dump(bool printDependencies) const { |
| 278 | SkDebugf("--------------------------------------------------------------\n"); |
| 279 | SkDebugf("renderTaskID: %d - proxyID: %d - surfaceID: %d\n", fUniqueID, |
| 280 | fTarget ? fTarget->uniqueID().asUInt() : -1, |
| 281 | fTarget && fTarget->peekSurface() |
| 282 | ? fTarget->peekSurface()->uniqueID().asUInt() |
| 283 | : -1); |
| 284 | |
| 285 | if (printDependencies) { |
| 286 | SkDebugf("I rely On (%d): ", fDependencies.count()); |
| 287 | for (int i = 0; i < fDependencies.count(); ++i) { |
| 288 | SkDebugf("%d, ", fDependencies[i]->fUniqueID); |
| 289 | } |
| 290 | SkDebugf("\n"); |
| 291 | |
| 292 | SkDebugf("(%d) Rely On Me: ", fDependents.count()); |
| 293 | for (int i = 0; i < fDependents.count(); ++i) { |
| 294 | SkDebugf("%d, ", fDependents[i]->fUniqueID); |
| 295 | } |
| 296 | SkDebugf("\n"); |
| 297 | } |
| 298 | } |
| 299 | #endif |