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