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