blob: e12bbc8e3087ce0b3eb9a9f66194cbb0ff183056 [file] [log] [blame]
Chris Dalton3d770272019-08-14 09:24:37 -06001/*
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 Dalton4ece96d2019-08-30 11:26:39 -060013#include "src/gpu/GrRenderTarget.h"
Chris Dalton3d770272019-08-14 09:24:37 -060014#include "src/gpu/GrResourceAllocator.h"
15#include "src/gpu/GrTexturePriv.h"
16
Chris Daltone2a903e2019-09-18 13:41:50 -060017GrTextureResolveRenderTask::~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
24void GrTextureResolveRenderTask::addProxy(
Chris Dalton16a33c62019-09-24 22:19:17 -060025 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 Daltone2a903e2019-09-18 13:41:50 -060029 // 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 Dalton4ece96d2019-08-30 11:26:39 -060036 SkASSERT(renderTargetProxy);
37 SkASSERT(renderTargetProxy->isMSAADirty());
Chris Dalton16a33c62019-09-24 22:19:17 -060038 fResolves.back().fMSAAResolveRect = renderTargetProxy->msaaDirtyRect();
Chris Dalton4ece96d2019-08-30 11:26:39 -060039 renderTargetProxy->markMSAAResolved();
40 }
41
Chris Daltone2a903e2019-09-18 13:41:50 -060042 if (GrSurfaceProxy::ResolveFlags::kMipMaps & flags) {
43 GrTextureProxy* textureProxy = proxy->asTextureProxy();
Chris Dalton4ece96d2019-08-30 11:26:39 -060044 SkASSERT(GrMipMapped::kYes == textureProxy->mipMapped());
45 SkASSERT(textureProxy->mipMapsAreDirty());
46 textureProxy->markMipMapsClean();
47 }
48
Chris Daltone2a903e2019-09-18 13:41:50 -060049 // Add the proxy as a dependency: We will read the existing contents of this texture while
Chris Dalton3d770272019-08-14 09:24:37 -060050 // generating mipmap levels and/or resolving MSAA.
Chris Dalton16a33c62019-09-24 22:19:17 -060051 this->addDependency(proxy, GrMipMapped::kNo, GrTextureResolveManager(nullptr), caps);
Chris Daltone2a903e2019-09-18 13:41:50 -060052 proxy->setLastRenderTask(this);
Chris Dalton3d770272019-08-14 09:24:37 -060053}
54
55void GrTextureResolveRenderTask::gatherProxyIntervals(GrResourceAllocator* alloc) const {
Chris Daltone2a903e2019-09-18 13:41:50 -060056 // 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 Dalton3d770272019-08-14 09:24:37 -060064 alloc->incOps();
65}
66
67bool GrTextureResolveRenderTask::onExecute(GrOpFlushState* flushState) {
Chris Daltone2a903e2019-09-18 13:41:50 -060068 // 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 Daltonc139d082019-09-26 14:04:24 -060072 if (GrRenderTarget* renderTarget = resolve.fProxy->peekRenderTarget()) {
Chris Dalton798a31d2019-09-26 19:28:32 +000073 flushState->gpu()->resolveRenderTarget(renderTarget, resolve.fMSAAResolveRect,
74 resolve.fProxy->origin(),
75 GrGpu::ForExternalIO::kNo);
Chris Dalton798a31d2019-09-26 19:28:32 +000076 }
Chris Dalton4ece96d2019-08-30 11:26:39 -060077 }
78 }
Chris Daltone2a903e2019-09-18 13:41:50 -060079 // 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 Dalton16a33c62019-09-24 22:19:17 -060086 SkASSERT(!texture->texturePriv().mipMapsAreDirty());
Chris Daltone2a903e2019-09-18 13:41:50 -060087 }
Chris Dalton4ece96d2019-08-30 11:26:39 -060088 }
Chris Dalton3d770272019-08-14 09:24:37 -060089 }
90
91 return true;
92}
Chris Daltone2a903e2019-09-18 13:41:50 -060093
94#ifdef SK_DEBUG
95void GrTextureResolveRenderTask::visitProxies_debugOnly(const VisitSurfaceProxyFunc& fn) const {
96 for (const auto& resolve : fResolves) {
97 fn(resolve.fProxy.get(), GrMipMapped::kNo);
98 }
99}
100#endif