| 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 |
#include "src/gpu/ganesh/GrTextureResolveRenderTask.h" |
| 8 |
|
| 9 |
#include "include/gpu/GpuTypes.h" |
| 10 |
#include "include/private/base/SkAssert.h" |
| 11 |
#include "include/private/base/SkMacros.h" |
| 12 |
#include "src/gpu/ganesh/GrDrawingManager.h" |
| 13 |
#include "src/gpu/ganesh/GrGpu.h" |
| 14 |
#include "src/gpu/ganesh/GrOpFlushState.h" |
| 15 |
#include "src/gpu/ganesh/GrRenderTargetProxy.h" |
| 16 |
#include "src/gpu/ganesh/GrResourceAllocator.h" |
| 17 |
#include "src/gpu/ganesh/GrSurfaceProxyView.h" |
| 18 |
#include "src/gpu/ganesh/GrTexture.h" |
| 19 |
#include "src/gpu/ganesh/GrTextureProxy.h" |
| 20 |
#include "src/gpu/ganesh/GrTextureResolveManager.h" |
| 21 |
|
| 22 |
#include <algorithm> |
| 23 |
#include <cstddef> |
| 24 |
#include <utility> |
| 25 |
|
| 26 |
class GrRenderTarget; |
| 27 |
|
| 28 |
void GrTextureResolveRenderTask::addProxy(GrDrawingManager* drawingMgr, |
| 29 |
sk_sp<GrSurfaceProxy> proxyRef, |
| 30 |
GrSurfaceProxy::ResolveFlags flags, |
| 31 |
const GrCaps& caps) { |
| 32 |
GrSurfaceProxy::ResolveFlags newFlags = flags; |
| 33 |
GrSurfaceProxy* proxy = proxyRef.get(); |
| 34 |
Resolve* resolve; |
| 35 |
bool newProxy = false; |
| 36 |
|
| 37 |
// We might just need to update the flags for an existing dependency. |
| 38 |
if (auto found = std::find(fTargets.begin(), fTargets.end(), proxyRef); |
| 39 |
found != fTargets.end()) { |
| 40 |
size_t index = found - fTargets.begin(); |
| 41 |
resolve = &fResolves[index]; |
| 42 |
newFlags = ~resolve->fFlags & flags; |
| 43 |
resolve->fFlags |= flags; |
| 44 |
} else { |
| 45 |
// Ensure the last render task that operated on the proxy is closed. That's where msaa and |
| 46 |
// mipmaps should have been marked dirty. |
| 47 |
SkASSERT(!drawingMgr->getLastRenderTask(proxy) |
| 48 |
|| drawingMgr->getLastRenderTask(proxy)->isClosed()); |
| 49 |
SkASSERT(GrSurfaceProxy::ResolveFlags::kNone != flags); |
| 50 |
resolve = &fResolves.emplace_back(flags); |
| 51 |
newProxy = true; |
| 52 |
} |
| 53 |
|
| 54 |
if (GrSurfaceProxy::ResolveFlags::kMSAA & newFlags) { |
| 55 |
GrRenderTargetProxy* renderTargetProxy = proxy->asRenderTargetProxy(); |
| 56 |
SkASSERT(renderTargetProxy); |
| 57 |
SkASSERT(renderTargetProxy->isMSAADirty()); |
| 58 |
resolve->fMSAAResolveRect = renderTargetProxy->msaaDirtyRect(); |
| 59 |
renderTargetProxy->markMSAAResolved(); |
| 60 |
} |
| 61 |
|
| 62 |
if (GrSurfaceProxy::ResolveFlags::kMipMaps & newFlags) { |
| 63 |
GrTextureProxy* textureProxy = proxy->asTextureProxy(); |
| 64 |
SkASSERT(skgpu::Mipmapped::kYes == textureProxy->mipmapped()); |
| 65 |
SkASSERT(textureProxy->mipmapsAreDirty()); |
| 66 |
textureProxy->markMipmapsClean(); |
| 67 |
} |
| 68 |
|
| 69 |
// We must do this after updating the proxy state because of assertions that the proxy isn't |
| 70 |
// dirty. |
| 71 |
if (newProxy) { |
| 72 |
// Add the proxy as a dependency: We will read the existing contents of this texture while |
| 73 |
// generating mipmap levels and/or resolving MSAA. |
| 74 |
this->addDependency( |
| 75 |
drawingMgr, proxy, skgpu::Mipmapped::kNo, GrTextureResolveManager(nullptr), caps); |
| 76 |
this->addTarget(drawingMgr, GrSurfaceProxyView(std::move(proxyRef))); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
void GrTextureResolveRenderTask::gatherProxyIntervals(GrResourceAllocator* alloc) const { |
| 81 |
// This renderTask doesn't have "normal" ops, however we still need to add intervals so |
| 82 |
// fEndOfOpsTaskOpIndices will remain in sync. We create fake op#'s to capture the fact that we |
| 83 |
// manipulate the resolve proxies. |
| 84 |
auto fakeOp = alloc->curOp(); |
| 85 |
SkASSERT(fResolves.size() == this->numTargets()); |
| 86 |
for (const sk_sp<GrSurfaceProxy>& target : fTargets) { |
| 87 |
alloc->addInterval(target.get(), fakeOp, fakeOp, GrResourceAllocator::ActualUse::kYes, |
| 88 |
GrResourceAllocator::AllowRecycling::kYes); |
| 89 |
} |
| 90 |
alloc->incOps(); |
| 91 |
} |
| 92 |
|
| 93 |
bool GrTextureResolveRenderTask::onExecute(GrOpFlushState* flushState) { |
| 94 |
// Resolve all msaa back-to-back, before regenerating mipmaps. |
| 95 |
SkASSERT(fResolves.size() == this->numTargets()); |
| 96 |
for (int i = 0; i < fResolves.size(); ++i) { |
| •97 |
Resolve& resolve = fResolves[i]; |
| 98 |
if (GrSurfaceProxy::ResolveFlags::kMSAA & resolve.fFlags) { |
| 99 |
GrSurfaceProxy* proxy = this->target(i); |
| 100 |
// peekRenderTarget might be null if there was an instantiation error. |
| 101 |
if (GrRenderTarget* renderTarget = proxy->peekRenderTarget()) { |
| 102 |
flushState->gpu()->resolveRenderTarget(renderTarget, resolve.fMSAAResolveRect); |
| •103 |
resolve.fFlags &= ~GrSurfaceProxy::ResolveFlags::kMSAA; |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
// Regenerate all mipmaps back-to-back. |
| 108 |
for (int i = 0; i < fResolves.size(); ++i) { |
| •109 |
Resolve& resolve = fResolves[i]; |
| 110 |
if (GrSurfaceProxy::ResolveFlags::kMipMaps & resolve.fFlags) { |
| 111 |
// peekTexture might be null if there was an instantiation error. |
| 112 |
GrTexture* texture = this->target(i)->peekTexture(); |
| •113 |
if (texture && (!texture->mipmapsAreDirty() || |
| 114 |
flushState->gpu()->regenerateMipMapLevels(texture))) { |
| 115 |
resolve.fFlags &= ~GrSurfaceProxy::ResolveFlags::kMipMaps; |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
return true; |
| 121 |
} |
| 122 |
|
| •123 |
void GrTextureResolveRenderTask::endFlush(GrDrawingManager* drawingMgr) { |
| 124 |
// Any flags still set here correspond to resolves that were recorded by addProxy() but never |
| 125 |
// executed (the flush was dropped before render-task execution, this task was skipped because |
| 126 |
// its targets failed to instantiate, or a per-target operation failed). Re-mark those proxies |
| 127 |
// dirty so a subsequent flush will re-record the resolve. |
| 128 |
SkASSERT(fResolves.size() == this->numTargets()); |
| 129 |
for (int i = 0; i < fResolves.size(); ++i) { |
| 130 |
const Resolve& resolve = fResolves[i]; |
| 131 |
GrSurfaceProxy* proxy = this->target(i); |
| 132 |
if (GrSurfaceProxy::ResolveFlags::kMSAA & resolve.fFlags) { |
| 133 |
if (GrRenderTargetProxy* rtProxy = proxy->asRenderTargetProxy()) { |
| 134 |
rtProxy->markMSAADirty(resolve.fMSAAResolveRect); |
| 135 |
} |
| 136 |
} |
| 137 |
if (GrSurfaceProxy::ResolveFlags::kMipMaps & resolve.fFlags) { |
| 138 |
if (GrTextureProxy* texProxy = proxy->asTextureProxy()) { |
| 139 |
texProxy->markMipmapsDirty(); |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
this->GrRenderTask::endFlush(drawingMgr); |
| 144 |
} |
| 145 |
|
| 146 |
#ifdef SK_DEBUG |
| 147 |
void GrTextureResolveRenderTask::visitProxies_debugOnly(const GrVisitProxyFunc&) const {} |
| 148 |
#endif |
| 149 |
|
| 150 |
#if defined(GPU_TEST_UTILS) |
| 151 |
GrSurfaceProxy::ResolveFlags |
| 152 |
GrTextureResolveRenderTask::flagsForProxy(sk_sp<GrSurfaceProxy> proxy) const { |
| 153 |
if (auto found = std::find(fTargets.begin(), fTargets.end(), proxy); |
| 154 |
found != fTargets.end()) { |
| 155 |
return fResolves[found - fTargets.begin()].fFlags; |
| 156 |
} |
| 157 |
return GrSurfaceProxy::ResolveFlags::kNone; |
| 158 |
} |
| 159 |
#endif |