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