| 1 |
/* |
| 2 |
* Copyright 2011 Google Inc. |
| 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/ganesh/gl/GrGLRenderTarget.h" |
| 9 |
|
| 10 |
#include "include/core/SkString.h" |
| 11 |
#include "include/core/SkTraceMemoryDump.h" |
| 12 |
#include "include/gpu/GpuTypes.h" |
| 13 |
#include "include/gpu/ganesh/GrDirectContext.h" |
| 14 |
#include "include/gpu/ganesh/GrTypes.h" |
| 15 |
#include "include/gpu/ganesh/gl/GrGLBackendSurface.h" |
| 16 |
#include "include/gpu/ganesh/gl/GrGLFunctions.h" |
| 17 |
#include "include/gpu/ganesh/gl/GrGLInterface.h" |
| 18 |
#include "include/private/base/SkAssert.h" |
| 19 |
#include "include/private/base/SkDebug.h" |
| 20 |
#include "include/private/gpu/ganesh/GrTypesPriv.h" |
| 21 |
#include "src/gpu/ganesh/GrAttachment.h" |
| 22 |
#include "src/gpu/ganesh/GrBackendUtils.h" |
| 23 |
#include "src/gpu/ganesh/GrCaps.h" |
| 24 |
#include "src/gpu/ganesh/GrDirectContextPriv.h" |
| 25 |
#include "src/gpu/ganesh/GrGpu.h" |
| 26 |
#include "src/gpu/ganesh/GrResourceProvider.h" |
| 27 |
#include "src/gpu/ganesh/gl/GrGLCaps.h" |
| 28 |
#include "src/gpu/ganesh/gl/GrGLGpu.h" |
| 29 |
#include "src/gpu/ganesh/gl/GrGLTexture.h" |
| 30 |
#include "src/gpu/ganesh/gl/GrGLUtil.h" |
| 31 |
|
| 32 |
#include <utility> |
| 33 |
|
| 34 |
#define GPUGL static_cast<GrGLGpu*>(this->getGpu()) |
| 35 |
#define GL_CALL(X) GR_GL_CALL(GPUGL->glInterface(), X) |
| 36 |
#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(GPUGL->glInterface(), RET, X) |
| 37 |
|
| 38 |
// Because this class is virtually derived from GrSurface we must explicitly call its constructor. |
| 39 |
// Constructor for wrapped render targets. |
| 40 |
GrGLRenderTarget::GrGLRenderTarget(GrGLGpu* gpu, |
| 41 |
const SkISize& dimensions, |
| 42 |
GrGLFormat format, |
| 43 |
int sampleCount, |
| 44 |
const IDs& ids, |
| 45 |
sk_sp<GrGLAttachment> stencil, |
| 46 |
skgpu::Protected isProtected, |
| 47 |
std::string_view label) |
| 48 |
: GrSurface(gpu, dimensions, isProtected, label) |
| 49 |
, GrRenderTarget(gpu, dimensions, sampleCount, isProtected, label, std::move(stencil)) { |
| 50 |
this->init(format, ids); |
| 51 |
this->setFlags(gpu->glCaps(), ids); |
| 52 |
this->registerWithCacheWrapped(GrWrapCacheable::kNo); |
| 53 |
} |
| 54 |
|
| 55 |
GrGLRenderTarget::GrGLRenderTarget(GrGLGpu* gpu, |
| 56 |
const SkISize& dimensions, |
| 57 |
GrGLFormat format, |
| 58 |
int sampleCount, |
| 59 |
const IDs& ids, |
| 60 |
skgpu::Protected isProtected, |
| 61 |
std::string_view label) |
| 62 |
: GrSurface(gpu, dimensions, isProtected, label) |
| 63 |
, GrRenderTarget(gpu, dimensions, sampleCount, isProtected, label) { |
| 64 |
this->init(format, ids); |
| 65 |
this->setFlags(gpu->glCaps(), ids); |
| 66 |
} |
| 67 |
|
| 68 |
inline void GrGLRenderTarget::setFlags(const GrGLCaps& glCaps, const IDs& idDesc) { |
| 69 |
if ((fMultisampleFBOID | fSingleSampleFBOID) == 0) { |
| 70 |
this->setGLRTFBOIDIs0(); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
void GrGLRenderTarget::init(GrGLFormat format, const IDs& idDesc) { |
| 75 |
fMultisampleFBOID = idDesc.fMultisampleFBOID; |
| 76 |
fSingleSampleFBOID = idDesc.fSingleSampleFBOID; |
| 77 |
fMSColorRenderbufferID = idDesc.fMSColorRenderbufferID; |
| 78 |
fRTFBOOwnership = idDesc.fRTFBOOwnership; |
| 79 |
fRTFormat = format; |
| 80 |
fTotalMemorySamplesPerPixel = idDesc.fTotalMemorySamplesPerPixel; |
| 81 |
} |
| 82 |
|
| 83 |
GrGLFormat stencil_bits_to_format(int stencilBits) { |
| 84 |
SkASSERT(stencilBits); |
| 85 |
switch (stencilBits) { |
| 86 |
case 8: |
| 87 |
// We pick the packed format here so when we query total size we are at least not |
| 88 |
// underestimating the total size of the stencil buffer. However, in reality this |
| 89 |
// rarely matters since we usually don't care about the size of wrapped objects. |
| 90 |
return GrGLFormat::kDEPTH24_STENCIL8; |
| 91 |
case 16: |
| 92 |
return GrGLFormat::kSTENCIL_INDEX16; |
| 93 |
default: |
| 94 |
SkASSERT(false); |
| 95 |
return GrGLFormat::kUnknown; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
sk_sp<GrGLRenderTarget> GrGLRenderTarget::MakeWrapped(GrGLGpu* gpu, |
| 100 |
const SkISize& dimensions, |
| 101 |
GrGLFormat format, |
| 102 |
int sampleCount, |
| 103 |
const IDs& idDesc, |
| 104 |
int stencilBits, |
| 105 |
skgpu::Protected isProtected, |
| 106 |
std::string_view label) { |
| 107 |
sk_sp<GrGLAttachment> sb; |
| 108 |
if (stencilBits) { |
| 109 |
// We pick a "fake" actual format that matches the number of stencil bits. When wrapping |
| 110 |
// an FBO with some number of stencil bits all we care about in the future is that we have |
| 111 |
// a format with the same number of stencil bits. We don't even directly use the format or |
| 112 |
// any other properties. Thus it is fine for us to just assign an arbitrary format that |
| 113 |
// matches the stencil bit count. |
| 114 |
GrGLFormat sFmt = stencil_bits_to_format(stencilBits); |
| 115 |
|
| 116 |
// We don't have the actual renderbufferID but we need to make an attachment for the stencil |
| 117 |
// so we just set it to an invalid value of 0 to make sure we don't explicitly use it or try |
| 118 |
// and delete it. |
| 119 |
sb = GrGLAttachment::MakeWrappedRenderBuffer(gpu, |
| 120 |
/*renderbufferID=*/0, |
| 121 |
dimensions, |
| 122 |
GrAttachment::UsageFlags::kStencilAttachment, |
| 123 |
sampleCount, |
| 124 |
sFmt); |
| 125 |
} |
| 126 |
return sk_sp<GrGLRenderTarget>(new GrGLRenderTarget( |
| 127 |
gpu, dimensions, format, sampleCount, idDesc, std::move(sb), isProtected, label)); |
| 128 |
} |
| 129 |
|
| 130 |
GrBackendRenderTarget GrGLRenderTarget::getBackendRenderTarget() const { |
| 131 |
bool useMultisampleFBO = (this->numSamples() > 1); |
| 132 |
GrGLFramebufferInfo fbi; |
| 133 |
fbi.fFBOID = (useMultisampleFBO) ? fMultisampleFBOID : fSingleSampleFBOID; |
| 134 |
fbi.fFormat = GrGLFormatToEnum(this->format()); |
| 135 |
fbi.fProtected = skgpu::Protected(this->isProtected()); |
| 136 |
int numStencilBits = 0; |
| 137 |
if (GrAttachment* stencil = this->getStencilAttachment(useMultisampleFBO)) { |
| 138 |
numStencilBits = GrBackendFormatStencilBits(stencil->backendFormat()); |
| 139 |
} |
| 140 |
|
| 141 |
return GrBackendRenderTargets::MakeGL( |
| 142 |
this->width(), this->height(), this->numSamples(), numStencilBits, fbi); |
| 143 |
} |
| 144 |
|
| 145 |
GrBackendFormat GrGLRenderTarget::backendFormat() const { |
| 146 |
// We should never have a GrGLRenderTarget (even a textureable one with a target that is not |
| 147 |
// texture 2D. |
| 148 |
return GrBackendFormats::MakeGL(GrGLFormatToEnum(fRTFormat), GR_GL_TEXTURE_2D); |
| 149 |
} |
| 150 |
|
| 151 |
size_t GrGLRenderTarget::onGpuMemorySize() const { |
| 152 |
return GrSurface::ComputeSize(this->backendFormat(), |
| 153 |
this->dimensions(), |
| 154 |
fTotalMemorySamplesPerPixel, |
| 155 |
skgpu::Mipmapped::kNo); |
| 156 |
} |
| 157 |
|
| 158 |
void GrGLRenderTarget::onSetLabel() { |
| 159 |
SkASSERT(fMSColorRenderbufferID); |
| 160 |
SkASSERT(fRTFBOOwnership == GrBackendObjectOwnership::kOwned); |
| 161 |
} |
| 162 |
|
| 163 |
bool GrGLRenderTarget::completeStencilAttachment(GrAttachment* stencil, bool useMultisampleFBO) { |
| 164 |
// We defer attaching the new stencil buffer until the next time our framebuffer is bound. |
| 165 |
if (this->getStencilAttachment(useMultisampleFBO) != stencil) { |
| 166 |
fNeedsStencilAttachmentBind[useMultisampleFBO] = true; |
| 167 |
} |
| 168 |
return true; |
| 169 |
} |
| 170 |
|
| 171 |
bool GrGLRenderTarget::ensureDynamicMSAAAttachment() { |
| 172 |
SkASSERT(this->numSamples() == 1); |
| 173 |
if (fMultisampleFBOID) { |
| 174 |
return true; |
| 175 |
} |
| 176 |
SkASSERT(!fDynamicMSAAAttachment); |
| 177 |
|
| 178 |
GrResourceProvider* resourceProvider = this->getContext()->priv().resourceProvider(); |
| 179 |
const GrCaps& caps = *this->getGpu()->caps(); |
| 180 |
|
| 181 |
int internalSampleCount = caps.internalMultisampleCount(this->backendFormat()); |
| 182 |
if (internalSampleCount <= 1) { |
| 183 |
return false; |
| 184 |
} |
| 185 |
|
| 186 |
if (resourceProvider->caps()->msaaResolvesAutomatically() && this->asTexture()) { |
| 187 |
// We can use EXT_multisampled_render_to_texture for MSAA. We will configure the FBO as MSAA |
| 188 |
// or not during bindFBO(). |
| 189 |
fMultisampleFBOID = fSingleSampleFBOID; |
| 190 |
return true; |
| 191 |
} |
| 192 |
|
| 193 |
GL_CALL(GenFramebuffers(1, &fMultisampleFBOID)); |
| 194 |
if (!fMultisampleFBOID) { |
| 195 |
return false; |
| 196 |
} |
| 197 |
|
| 198 |
this->getGLGpu()->bindFramebuffer(GR_GL_FRAMEBUFFER, fMultisampleFBOID); |
| 199 |
|
| 200 |
fDynamicMSAAAttachment.reset( |
| 201 |
static_cast<GrGLAttachment*>(resourceProvider->getDiscardableMSAAAttachment( |
| 202 |
this->dimensions(), this->backendFormat(), internalSampleCount, |
| 203 |
GrProtected(this->isProtected()), GrMemoryless::kNo).release())); |
| 204 |
if (!fDynamicMSAAAttachment) { |
| 205 |
return false; |
| 206 |
} |
| 207 |
|
| 208 |
GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_RENDERBUFFER, |
| 209 |
fDynamicMSAAAttachment->renderbufferID())); |
| 210 |
return true; |
| 211 |
} |
| 212 |
|
| 213 |
void GrGLRenderTarget::bindInternal(GrGLenum fboTarget, bool useMultisampleFBO) { |
| 214 |
GrGLuint fboId = useMultisampleFBO ? fMultisampleFBOID : fSingleSampleFBOID; |
| 215 |
this->getGLGpu()->bindFramebuffer(fboTarget, fboId); |
| 216 |
|
| 217 |
if (fSingleSampleFBOID != 0 && |
| 218 |
fSingleSampleFBOID == fMultisampleFBOID && |
| 219 |
useMultisampleFBO != fDMSAARenderToTextureFBOIsMultisample) { |
| 220 |
auto* glTex = static_cast<GrGLTexture*>(this->asTexture()); |
| 221 |
if (this->getGLGpu()->glCaps().bindTexture0WhenChangingTextureFBOMultisampleCount()) { |
| 222 |
GL_CALL(FramebufferTexture2D(fboTarget, |
| 223 |
GR_GL_COLOR_ATTACHMENT0, |
| 224 |
GR_GL_TEXTURE_2D, |
| 225 |
0 /*texture*/, |
| 226 |
0 /*mipMapLevel*/)); |
| 227 |
} |
| 228 |
if (useMultisampleFBO) { |
| 229 |
int sampleCount = this->numSamples() > 1 ? |
| 230 |
this->numSamples() : |
| 231 |
this->getGpu()->caps()->internalMultisampleCount(this->backendFormat()); |
| 232 |
GL_CALL(FramebufferTexture2DMultisample(fboTarget, |
| 233 |
GR_GL_COLOR_ATTACHMENT0, |
| 234 |
glTex->target(), |
| 235 |
glTex->textureID(), |
| 236 |
0 /*mipMapLevel*/, |
| 237 |
sampleCount)); |
| 238 |
} else { |
| 239 |
GL_CALL(FramebufferTexture2D(fboTarget, |
| 240 |
GR_GL_COLOR_ATTACHMENT0, |
| 241 |
glTex->target(), |
| 242 |
glTex->textureID(), |
| 243 |
0 /*mipMapLevel*/)); |
| 244 |
} |
| 245 |
fDMSAARenderToTextureFBOIsMultisample = useMultisampleFBO; |
| 246 |
fNeedsStencilAttachmentBind[useMultisampleFBO] = true; |
| 247 |
} |
| 248 |
|
| 249 |
// Make sure the stencil attachment is valid. Even though a color buffer op doesn't use stencil, |
| 250 |
// our FBO still needs to be "framebuffer complete". |
| 251 |
if (fNeedsStencilAttachmentBind[useMultisampleFBO]) { |
| 252 |
if (auto stencil = this->getStencilAttachment(useMultisampleFBO)) { |
| 253 |
const GrGLAttachment* glStencil = static_cast<const GrGLAttachment*>(stencil); |
| 254 |
GL_CALL(FramebufferRenderbuffer(fboTarget, |
| 255 |
GR_GL_STENCIL_ATTACHMENT, |
| 256 |
GR_GL_RENDERBUFFER, |
| 257 |
glStencil->renderbufferID())); |
| 258 |
if (GrGLFormatIsPackedDepthStencil(glStencil->format())) { |
| 259 |
GL_CALL(FramebufferRenderbuffer(fboTarget, |
| 260 |
GR_GL_DEPTH_ATTACHMENT, |
| 261 |
GR_GL_RENDERBUFFER, |
| 262 |
glStencil->renderbufferID())); |
| 263 |
} else { |
| 264 |
GL_CALL(FramebufferRenderbuffer(fboTarget, |
| 265 |
GR_GL_DEPTH_ATTACHMENT, |
| 266 |
GR_GL_RENDERBUFFER, |
| 267 |
0)); |
| 268 |
} |
| 269 |
} else { |
| 270 |
GL_CALL(FramebufferRenderbuffer(fboTarget, |
| 271 |
GR_GL_STENCIL_ATTACHMENT, |
| 272 |
GR_GL_RENDERBUFFER, |
| 273 |
0)); |
| 274 |
GL_CALL(FramebufferRenderbuffer(fboTarget, |
| 275 |
GR_GL_DEPTH_ATTACHMENT, |
| 276 |
GR_GL_RENDERBUFFER, |
| 277 |
0)); |
| 278 |
} |
| 279 |
#ifdef SK_DEBUG |
| 280 |
if (!this->getGLGpu()->glCaps().skipErrorChecks() && |
| 281 |
!this->getGLGpu()->glCaps().rebindColorAttachmentAfterCheckFramebufferStatus()) { |
| 282 |
GrGLenum status; |
| 283 |
GL_CALL_RET(status, CheckFramebufferStatus(fboTarget)); |
| 284 |
if (status != GR_GL_FRAMEBUFFER_COMPLETE) { |
| 285 |
// This can fail if the context has been asynchronously abandoned (see |
| 286 |
// skbug.com/40036375). |
| 287 |
SkDebugf("WARNING: failed to attach stencil.\n"); |
| 288 |
} |
| 289 |
} |
| 290 |
#endif |
| 291 |
fNeedsStencilAttachmentBind[useMultisampleFBO] = false; |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
void GrGLRenderTarget::bindForResolve(GrGLGpu::ResolveDirection resolveDirection) { |
| 296 |
// If the multisample FBO is nonzero, it means we always have something to resolve (even if the |
| 297 |
// single sample buffer is FBO 0). If it's zero, then there's nothing to resolve. |
| 298 |
SkASSERT(fMultisampleFBOID != 0); |
| 299 |
|
| 300 |
// In the EXT_multisampled_render_to_texture case, we shouldn't be resolving anything. |
| 301 |
SkASSERT(!this->isMultisampledRenderToTexture()); |
| 302 |
|
| 303 |
if (resolveDirection == GrGLGpu::ResolveDirection::kMSAAToSingle) { |
| 304 |
this->bindInternal(GR_GL_READ_FRAMEBUFFER, true); |
| 305 |
this->bindInternal(GR_GL_DRAW_FRAMEBUFFER, false); |
| 306 |
} else { |
| 307 |
SkASSERT(resolveDirection == GrGLGpu::ResolveDirection::kSingleToMSAA); |
| 308 |
SkASSERT(this->getGLGpu()->glCaps().canResolveSingleToMSAA()); |
| 309 |
this->bindInternal(GR_GL_READ_FRAMEBUFFER, false); |
| 310 |
this->bindInternal(GR_GL_DRAW_FRAMEBUFFER, true); |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
void GrGLRenderTarget::onRelease() { |
| 315 |
if (GrBackendObjectOwnership::kBorrowed != fRTFBOOwnership) { |
| 316 |
GrGLGpu* gpu = this->getGLGpu(); |
| 317 |
if (fSingleSampleFBOID) { |
| 318 |
gpu->deleteFramebuffer(fSingleSampleFBOID); |
| 319 |
} |
| 320 |
if (fMultisampleFBOID && fMultisampleFBOID != fSingleSampleFBOID) { |
| 321 |
gpu->deleteFramebuffer(fMultisampleFBOID); |
| 322 |
} |
| 323 |
if (fMSColorRenderbufferID) { |
| 324 |
GL_CALL(DeleteRenderbuffers(1, &fMSColorRenderbufferID)); |
| 325 |
} |
| 326 |
} |
| 327 |
fMultisampleFBOID = kUnresolvableFBOID; |
| 328 |
fSingleSampleFBOID = kUnresolvableFBOID; |
| 329 |
fMSColorRenderbufferID = 0; |
| 330 |
INHERITED::onRelease(); |
| 331 |
} |
| 332 |
|
| 333 |
void GrGLRenderTarget::onAbandon() { |
| 334 |
fMultisampleFBOID = kUnresolvableFBOID; |
| 335 |
fSingleSampleFBOID = kUnresolvableFBOID; |
| 336 |
fMSColorRenderbufferID = 0; |
| 337 |
INHERITED::onAbandon(); |
| 338 |
} |
| 339 |
|
| 340 |
GrGLGpu* GrGLRenderTarget::getGLGpu() const { |
| 341 |
SkASSERT(!this->wasDestroyed()); |
| 342 |
return static_cast<GrGLGpu*>(this->getGpu()); |
| 343 |
} |
| 344 |
|
| 345 |
bool GrGLRenderTarget::canAttemptStencilAttachment(bool useMultisampleFBO) const { |
| 346 |
// This cap should have been handled at a higher level. |
| 347 |
SkASSERT(!this->getGpu()->getContext()->priv().caps()->avoidStencilBuffers()); |
| 348 |
// Only modify the FBO's attachments if we have created the FBO. Public APIs do not currently |
| 349 |
// allow for borrowed FBO ownership, so we can safely assume that if an object is owned, |
| 350 |
// Skia created it. |
| 351 |
return this->fRTFBOOwnership == GrBackendObjectOwnership::kOwned || |
| 352 |
// The dmsaa attachment is always owned and always supports adding stencil. |
| 353 |
(this->numSamples() == 1 && useMultisampleFBO); |
| 354 |
} |
| 355 |
|
| 356 |
void GrGLRenderTarget::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const { |
| 357 |
// Don't check this->fRefsWrappedObjects, as we might be the base of a GrGLTextureRenderTarget |
| 358 |
// which is multiply inherited from both ourselves and a texture. In these cases, one part |
| 359 |
// (texture, rt) may be wrapped, while the other is owned by Skia. |
| 360 |
bool refsWrappedRenderTargetObjects = |
| 361 |
this->fRTFBOOwnership == GrBackendObjectOwnership::kBorrowed; |
| 362 |
if (refsWrappedRenderTargetObjects && !traceMemoryDump->shouldDumpWrappedObjects()) { |
| 363 |
return; |
| 364 |
} |
| 365 |
|
| 366 |
int numSamplesNotInTexture = fTotalMemorySamplesPerPixel; |
| 367 |
if (this->asTexture()) { |
| 368 |
--numSamplesNotInTexture; // GrGLTexture::dumpMemoryStatistics accounts for 1 sample. |
| 369 |
} |
| 370 |
if (numSamplesNotInTexture >= 1) { |
| 371 |
size_t size = GrSurface::ComputeSize(this->backendFormat(), |
| 372 |
this->dimensions(), |
| 373 |
numSamplesNotInTexture, |
| 374 |
skgpu::Mipmapped::kNo); |
| 375 |
|
| 376 |
// Due to this resource having both a texture and a renderbuffer component, dump as |
| 377 |
// skia/gpu_resources/resource_#/renderbuffer |
| 378 |
SkString resourceName = this->getResourceName(); |
| 379 |
resourceName.append("/renderbuffer"); |
| 380 |
|
| 381 |
this->dumpMemoryStatisticsPriv(traceMemoryDump, resourceName, "RenderTarget", size); |
| 382 |
|
| 383 |
SkString renderbuffer_id; |
| 384 |
renderbuffer_id.appendU32(fMSColorRenderbufferID); |
| 385 |
traceMemoryDump->setMemoryBacking(resourceName.c_str(), "gl_renderbuffer", |
| 386 |
renderbuffer_id.c_str()); |
| 387 |
} |
| 388 |
} |