Chris Dalton | 3d77027 | 2019-08-14 09:24:37 -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/GrTextureResolveRenderTask.h" |
| 9 | |
| 10 | #include "src/gpu/GrGpu.h" |
| 11 | #include "src/gpu/GrMemoryPool.h" |
| 12 | #include "src/gpu/GrOpFlushState.h" |
Chris Dalton | 4ece96d | 2019-08-30 11:26:39 -0600 | [diff] [blame] | 13 | #include "src/gpu/GrRenderTarget.h" |
Chris Dalton | 3d77027 | 2019-08-14 09:24:37 -0600 | [diff] [blame] | 14 | #include "src/gpu/GrResourceAllocator.h" |
| 15 | #include "src/gpu/GrTexturePriv.h" |
| 16 | |
Chris Dalton | e2a903e | 2019-09-18 13:41:50 -0600 | [diff] [blame] | 17 | GrTextureResolveRenderTask::~GrTextureResolveRenderTask() { |
| 18 | for (const auto& resolve : fResolves) { |
| 19 | // Ensure the proxy doesn't keep hold of a dangling back pointer. |
| 20 | resolve.fProxy->setLastRenderTask(nullptr); |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | void GrTextureResolveRenderTask::addProxy( |
| 25 | sk_sp<GrSurfaceProxy> proxy, GrSurfaceProxy::ResolveFlags flags, const GrCaps& caps) { |
| 26 | // Ensure the last render task that operated on the proxy is closed. That's where msaa and |
| 27 | // mipmaps should have been marked dirty. |
| 28 | SkASSERT(!proxy->getLastRenderTask() || proxy->getLastRenderTask()->isClosed()); |
| 29 | SkASSERT(GrSurfaceProxy::ResolveFlags::kNone != flags); |
| 30 | |
| 31 | if (GrSurfaceProxy::ResolveFlags::kMSAA & flags) { |
| 32 | GrRenderTargetProxy* renderTargetProxy = proxy->asRenderTargetProxy(); |
Chris Dalton | 4ece96d | 2019-08-30 11:26:39 -0600 | [diff] [blame] | 33 | SkASSERT(renderTargetProxy); |
| 34 | SkASSERT(renderTargetProxy->isMSAADirty()); |
| 35 | renderTargetProxy->markMSAAResolved(); |
| 36 | } |
| 37 | |
Chris Dalton | e2a903e | 2019-09-18 13:41:50 -0600 | [diff] [blame] | 38 | if (GrSurfaceProxy::ResolveFlags::kMipMaps & flags) { |
| 39 | GrTextureProxy* textureProxy = proxy->asTextureProxy(); |
Chris Dalton | 4ece96d | 2019-08-30 11:26:39 -0600 | [diff] [blame] | 40 | SkASSERT(GrMipMapped::kYes == textureProxy->mipMapped()); |
| 41 | SkASSERT(textureProxy->mipMapsAreDirty()); |
| 42 | textureProxy->markMipMapsClean(); |
| 43 | } |
| 44 | |
Chris Dalton | e2a903e | 2019-09-18 13:41:50 -0600 | [diff] [blame] | 45 | // Add the proxy as a dependency: We will read the existing contents of this texture while |
Chris Dalton | 3d77027 | 2019-08-14 09:24:37 -0600 | [diff] [blame] | 46 | // generating mipmap levels and/or resolving MSAA. |
Chris Dalton | e2a903e | 2019-09-18 13:41:50 -0600 | [diff] [blame] | 47 | this->addDependency(proxy.get(), GrMipMapped::kNo, GrTextureResolveManager(nullptr), caps); |
| 48 | proxy->setLastRenderTask(this); |
Chris Dalton | 3d77027 | 2019-08-14 09:24:37 -0600 | [diff] [blame] | 49 | |
Chris Dalton | e2a903e | 2019-09-18 13:41:50 -0600 | [diff] [blame] | 50 | fResolves.emplace_back(std::move(proxy), flags); |
Chris Dalton | 3d77027 | 2019-08-14 09:24:37 -0600 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | void GrTextureResolveRenderTask::gatherProxyIntervals(GrResourceAllocator* alloc) const { |
Chris Dalton | e2a903e | 2019-09-18 13:41:50 -0600 | [diff] [blame] | 54 | // This renderTask doesn't have "normal" ops, however we still need to add intervals so |
| 55 | // fEndOfOpsTaskOpIndices will remain in sync. We create fake op#'s to capture the fact that we |
| 56 | // manipulate the resolve proxies. |
| 57 | auto fakeOp = alloc->curOp(); |
| 58 | for (const auto& resolve : fResolves) { |
| 59 | alloc->addInterval(resolve.fProxy.get(), fakeOp, fakeOp, |
| 60 | GrResourceAllocator::ActualUse::kYes); |
| 61 | } |
Chris Dalton | 3d77027 | 2019-08-14 09:24:37 -0600 | [diff] [blame] | 62 | alloc->incOps(); |
| 63 | } |
| 64 | |
| 65 | bool GrTextureResolveRenderTask::onExecute(GrOpFlushState* flushState) { |
Chris Dalton | e2a903e | 2019-09-18 13:41:50 -0600 | [diff] [blame] | 66 | // Resolve all msaa back-to-back, before regenerating mipmaps. |
| 67 | for (const auto& resolve : fResolves) { |
| 68 | if (GrSurfaceProxy::ResolveFlags::kMSAA & resolve.fFlags) { |
| 69 | // peekRenderTarget might be null if there was an instantiation error. |
| 70 | GrRenderTarget* renderTarget = resolve.fProxy->peekRenderTarget(); |
| 71 | if (renderTarget && renderTarget->needsResolve()) { |
| 72 | flushState->gpu()->resolveRenderTarget(renderTarget); |
| 73 | } |
Chris Dalton | 4ece96d | 2019-08-30 11:26:39 -0600 | [diff] [blame] | 74 | } |
| 75 | } |
Chris Dalton | e2a903e | 2019-09-18 13:41:50 -0600 | [diff] [blame] | 76 | // Regenerate all mipmaps back-to-back. |
| 77 | for (const auto& resolve : fResolves) { |
| 78 | if (GrSurfaceProxy::ResolveFlags::kMipMaps & resolve.fFlags) { |
| 79 | // peekTexture might be null if there was an instantiation error. |
| 80 | GrTexture* texture = resolve.fProxy->peekTexture(); |
| 81 | if (texture && texture->texturePriv().mipMapsAreDirty()) { |
| 82 | flushState->gpu()->regenerateMipMapLevels(texture); |
| 83 | } |
Chris Dalton | 4ece96d | 2019-08-30 11:26:39 -0600 | [diff] [blame] | 84 | } |
Chris Dalton | 3d77027 | 2019-08-14 09:24:37 -0600 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | return true; |
| 88 | } |
Chris Dalton | e2a903e | 2019-09-18 13:41:50 -0600 | [diff] [blame] | 89 | |
| 90 | #ifdef SK_DEBUG |
| 91 | void GrTextureResolveRenderTask::visitProxies_debugOnly(const VisitSurfaceProxyFunc& fn) const { |
| 92 | for (const auto& resolve : fResolves) { |
| 93 | fn(resolve.fProxy.get(), GrMipMapped::kNo); |
| 94 | } |
| 95 | } |
| 96 | #endif |