* Copyright 2016 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "src/gpu/ganesh/GrRenderTargetProxy.h"
#include "include/core/SkSize.h"
#include "include/gpu/GpuTypes.h"
#include "include/gpu/ganesh/GrBackendSurface.h"
#include "include/gpu/ganesh/GrTypes.h"
#include "include/private/base/SkTo.h"
#include "src/gpu/SkBackingFit.h"
#include "src/gpu/ganesh/GrCaps.h"
#include "src/gpu/ganesh/GrGpuResourcePriv.h"
#include "src/gpu/ganesh/GrRenderTarget.h"
#include "src/gpu/ganesh/GrSurface.h"
#include "src/gpu/ganesh/GrSurfaceProxyPriv.h"
#include <utility>
#ifdef SK_DEBUG
#include "include/gpu/ganesh/GrDirectContext.h"
#include "src/gpu/ganesh/GrDirectContextPriv.h"
#endif
GrRenderTargetProxy::GrRenderTargetProxy(const GrCaps& caps,
const GrBackendFormat& format,
SkISize dimensions,
int sampleCount,
SkBackingFit fit,
skgpu::Budgeted budgeted,
GrProtected isProtected,
GrInternalSurfaceFlags surfaceFlags,
UseAllocator useAllocator,
std::string_view label)
: INHERITED(
format, dimensions, fit, budgeted, isProtected, surfaceFlags, useAllocator, label)
, fSampleCnt(sampleCount)
, fWrapsVkSecondaryCB(WrapsVkSecondaryCB::kNo) {}
GrRenderTargetProxy::GrRenderTargetProxy(LazyInstantiateCallback&& callback,
const GrBackendFormat& format,
SkISize dimensions,
int sampleCount,
SkBackingFit fit,
skgpu::Budgeted budgeted,
GrProtected isProtected,
GrInternalSurfaceFlags surfaceFlags,
UseAllocator useAllocator,
WrapsVkSecondaryCB wrapsVkSecondaryCB,
std::string_view label)
: INHERITED(std::move(callback),
format,
dimensions,
fit,
budgeted,
isProtected,
surfaceFlags,
useAllocator,
label)
, fSampleCnt(sampleCount)
, fWrapsVkSecondaryCB(wrapsVkSecondaryCB) {}
GrRenderTargetProxy::GrRenderTargetProxy(sk_sp<GrSurface> surf,
UseAllocator useAllocator,
WrapsVkSecondaryCB wrapsVkSecondaryCB)
: INHERITED(std::move(surf), SkBackingFit::kExact, useAllocator)
, fSampleCnt(fTarget->asRenderTarget()->numSamples())
, fWrapsVkSecondaryCB(wrapsVkSecondaryCB) {
SkASSERT(!(this->numSamples() <= 1 ||
fTarget->getContext()->priv().caps()->msaaResolvesAutomatically()) ||
!this->requiresManualMSAAResolve());
}
int GrRenderTargetProxy::maxWindowRectangles(const GrCaps& caps) const {
return this->glRTFBOIDIs0() ? 0 : caps.maxWindowRectangles();
}
bool GrRenderTargetProxy::instantiate(GrResourceProvider* resourceProvider) {
if (this->isLazy()) {
return false;
}
if (!this->instantiateImpl(resourceProvider, fSampleCnt, GrRenderable::kYes,
skgpu::Mipmapped::kNo, nullptr)) {
return false;
}
SkASSERT(this->peekRenderTarget());
SkASSERT(!this->peekTexture());
return true;
}
bool GrRenderTargetProxy::canUseStencil(const GrCaps& caps) const {
if (caps.avoidStencilBuffers() || this->wrapsVkSecondaryCB()) {
return false;
}
if (!this->isInstantiated()) {
if (this->isLazy() && this->backendFormat().backend() == GrBackendApi::kOpenGL) {
return SkToBool(this->asTextureProxy());
} else {
return true;
}
}
GrRenderTarget* rt = this->peekRenderTarget();
bool useMSAASurface = rt->numSamples() > 1;
return rt->getStencilAttachment(useMSAASurface) ||
rt->canAttemptStencilAttachment(useMSAASurface);
}
sk_sp<GrSurface> GrRenderTargetProxy::createSurface(GrResourceProvider* resourceProvider) const {
sk_sp<GrSurface> surface = this->createSurfaceImpl(resourceProvider, fSampleCnt,
GrRenderable::kYes, skgpu::Mipmapped::kNo);
if (!surface) {
return nullptr;
}
SkASSERT(surface->asRenderTarget());
SkASSERT(!surface->asTexture());
return surface;
}
size_t GrRenderTargetProxy::onUninstantiatedGpuMemorySize() const {
int colorSamplesPerPixel = this->numSamples();
if (colorSamplesPerPixel > 1) {
++colorSamplesPerPixel;
}
return GrSurface::ComputeSize(this->backendFormat(), this->dimensions(),
colorSamplesPerPixel, skgpu::Mipmapped::kNo,
!this->priv().isExact());
}
bool GrRenderTargetProxy::refsWrappedObjects() const {
if (!this->isInstantiated()) {
return false;
}
GrSurface* surface = this->peekSurface();
return surface->resourcePriv().refsWrappedObjects();
}
GrSurfaceProxy::LazySurfaceDesc GrRenderTargetProxy::callbackDesc() const {
SkASSERT(!this->isFullyLazy());
SkASSERT(this->isFunctionallyExact());
return {
this->dimensions(),
SkBackingFit::kExact,
GrRenderable::kYes,
skgpu::Mipmapped::kNo,
this->numSamples(),
this->backendFormat(),
GrTextureType::kNone,
this->isProtected(),
this->isBudgeted(),
this->getLabel(),
};
}
#ifdef SK_DEBUG
void GrRenderTargetProxy::onValidateSurface(const GrSurface* surface) {
SkASSERT(surface->asRenderTarget());
SkASSERT(surface->asRenderTarget()->numSamples() == this->numSamples());
GrInternalSurfaceFlags proxyFlags = fSurfaceFlags;
GrInternalSurfaceFlags surfaceFlags = surface->flags();
if (proxyFlags & GrInternalSurfaceFlags::kGLRTFBOIDIs0 && this->numSamples() == 1) {
surfaceFlags |= GrInternalSurfaceFlags::kGLRTFBOIDIs0;
}
SkASSERT(((int)proxyFlags & kGrInternalRenderTargetFlagsMask) ==
((int)surfaceFlags & kGrInternalRenderTargetFlagsMask));
if (proxyFlags & GrInternalSurfaceFlags::kVkRTSupportsInputAttachment) {
SkASSERT(surfaceFlags & GrInternalSurfaceFlags::kVkRTSupportsInputAttachment);
}
}
#endif