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