2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 4; -*- */
|
2013-02-14 03:26:24 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "GLScreenBuffer.h"
|
|
|
|
|
|
|
|
#include <cstring>
|
2014-10-08 08:11:54 +04:00
|
|
|
#include "CompositorTypes.h"
|
2019-05-26 17:30:14 +03:00
|
|
|
#include "mozilla/StaticPrefs.h"
|
2013-02-14 03:26:24 +04:00
|
|
|
#include "GLContext.h"
|
2013-12-06 19:36:13 +04:00
|
|
|
#include "GLBlitHelper.h"
|
2014-01-02 19:17:29 +04:00
|
|
|
#include "GLReadTexImageHelper.h"
|
2015-10-12 06:21:02 +03:00
|
|
|
#include "SharedSurfaceEGL.h"
|
2013-02-14 03:26:24 +04:00
|
|
|
#include "SharedSurfaceGL.h"
|
2015-10-12 06:21:02 +03:00
|
|
|
#include "ScopedGLHelpers.h"
|
|
|
|
#include "gfx2DGlue.h"
|
|
|
|
#include "../layers/ipc/ShadowLayers.h"
|
2016-09-27 06:22:20 +03:00
|
|
|
#include "mozilla/layers/TextureForwarder.h"
|
2015-10-12 06:21:02 +03:00
|
|
|
#include "mozilla/layers/TextureClientSharedSurface.h"
|
|
|
|
|
|
|
|
#ifdef XP_WIN
|
2015-11-06 03:19:24 +03:00
|
|
|
# include "SharedSurfaceANGLE.h" // for SurfaceFactory_ANGLEShareHandle
|
|
|
|
# include "SharedSurfaceD3D11Interop.h" // for SurfaceFactory_D3D11Interop
|
2016-08-24 01:18:55 +03:00
|
|
|
# include "mozilla/gfx/DeviceManagerDx.h"
|
2015-10-12 06:21:02 +03:00
|
|
|
#endif
|
|
|
|
|
2013-07-18 07:24:15 +04:00
|
|
|
#ifdef XP_MACOSX
|
|
|
|
# include "SharedSurfaceIO.h"
|
|
|
|
#endif
|
2015-10-12 06:21:02 +03:00
|
|
|
|
2018-05-24 15:06:31 +03:00
|
|
|
#ifdef MOZ_X11
|
2015-10-12 06:21:02 +03:00
|
|
|
# include "GLXLibrary.h"
|
|
|
|
# include "SharedSurfaceGLX.h"
|
|
|
|
#endif
|
2013-02-14 03:26:24 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace gl {
|
|
|
|
|
2014-07-12 02:10:49 +04:00
|
|
|
using gfx::SurfaceFormat;
|
2014-05-22 14:11:45 +04:00
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
UniquePtr<GLScreenBuffer> GLScreenBuffer::Create(GLContext* gl,
|
2014-07-12 02:10:49 +04:00
|
|
|
const gfx::IntSize& size,
|
|
|
|
const SurfaceCaps& caps) {
|
2014-08-16 04:38:08 +04:00
|
|
|
UniquePtr<GLScreenBuffer> ret;
|
2018-05-30 22:15:35 +03:00
|
|
|
if (caps.antialias && !gl->IsSupported(GLFeature::framebuffer_multisample)) {
|
2018-06-01 18:59:07 +03:00
|
|
|
return ret;
|
2013-02-14 03:26:24 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-06-05 03:15:38 +03:00
|
|
|
layers::TextureFlags flags = layers::TextureFlags::ORIGIN_BOTTOM_LEFT;
|
|
|
|
if (!caps.premultAlpha) {
|
|
|
|
flags |= layers::TextureFlags::NON_PREMULTIPLIED;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2015-06-05 03:15:38 +03:00
|
|
|
UniquePtr<SurfaceFactory> factory =
|
|
|
|
MakeUnique<SurfaceFactory_Basic>(gl, caps, flags);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-05-30 22:15:35 +03:00
|
|
|
ret.reset(new GLScreenBuffer(gl, caps, std::move(factory)));
|
2018-06-01 18:59:07 +03:00
|
|
|
return ret;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2019-02-26 01:07:19 +03:00
|
|
|
/* static */
|
|
|
|
UniquePtr<SurfaceFactory> GLScreenBuffer::CreateFactory(
|
2015-10-12 06:21:02 +03:00
|
|
|
GLContext* gl, const SurfaceCaps& caps,
|
2016-09-27 06:22:20 +03:00
|
|
|
KnowsCompositor* compositorConnection, const layers::TextureFlags& flags) {
|
2017-09-26 07:15:36 +03:00
|
|
|
LayersIPCChannel* ipcChannel = compositorConnection->GetTextureForwarder();
|
|
|
|
const layers::LayersBackend backend =
|
|
|
|
compositorConnection->GetCompositorBackendType();
|
|
|
|
const bool useANGLE = compositorConnection->GetCompositorUseANGLE();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-09-26 07:15:36 +03:00
|
|
|
const bool useGl =
|
2019-05-26 17:30:14 +03:00
|
|
|
!StaticPrefs::WebGLForceLayersReadback() &&
|
2017-09-26 07:15:36 +03:00
|
|
|
(backend == layers::LayersBackend::LAYERS_OPENGL ||
|
|
|
|
(backend == layers::LayersBackend::LAYERS_WR && !useANGLE));
|
|
|
|
const bool useD3D =
|
2019-05-26 17:30:14 +03:00
|
|
|
!StaticPrefs::WebGLForceLayersReadback() &&
|
2017-09-26 07:15:36 +03:00
|
|
|
(backend == layers::LayersBackend::LAYERS_D3D11 ||
|
|
|
|
(backend == layers::LayersBackend::LAYERS_WR && useANGLE));
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-10-12 06:21:02 +03:00
|
|
|
UniquePtr<SurfaceFactory> factory = nullptr;
|
2017-09-26 07:15:36 +03:00
|
|
|
if (useGl) {
|
2015-10-12 06:21:02 +03:00
|
|
|
#if defined(XP_MACOSX)
|
2017-09-26 07:15:36 +03:00
|
|
|
factory = SurfaceFactory_IOSurface::Create(gl, caps, ipcChannel, flags);
|
2018-05-24 15:06:31 +03:00
|
|
|
#elif defined(MOZ_X11)
|
2017-09-26 07:15:36 +03:00
|
|
|
if (sGLXLibrary.UseTextureFromPixmap())
|
|
|
|
factory = SurfaceFactory_GLXDrawable::Create(gl, caps, ipcChannel, flags);
|
2015-09-24 17:11:53 +03:00
|
|
|
#elif defined(MOZ_WIDGET_UIKIT)
|
2017-09-26 07:15:36 +03:00
|
|
|
factory = MakeUnique<SurfaceFactory_GLTexture>(mGLContext, caps, ipcChannel,
|
|
|
|
mFlags);
|
2017-03-04 00:14:27 +03:00
|
|
|
#elif defined(MOZ_WIDGET_ANDROID)
|
2019-05-26 17:29:42 +03:00
|
|
|
if (XRE_IsParentProcess() && !StaticPrefs::WebGLSurfaceTextureEnabled()) {
|
2019-02-22 00:53:22 +03:00
|
|
|
factory = SurfaceFactory_EGLImage::Create(gl, caps, ipcChannel, flags);
|
|
|
|
} else {
|
|
|
|
factory =
|
|
|
|
SurfaceFactory_SurfaceTexture::Create(gl, caps, ipcChannel, flags);
|
|
|
|
}
|
2015-10-12 06:21:02 +03:00
|
|
|
#else
|
2017-09-26 07:15:36 +03:00
|
|
|
if (gl->GetContextType() == GLContextType::EGL) {
|
|
|
|
if (XRE_IsParentProcess()) {
|
|
|
|
factory = SurfaceFactory_EGLImage::Create(gl, caps, ipcChannel, flags);
|
2015-10-12 06:21:02 +03:00
|
|
|
}
|
2017-09-26 07:15:36 +03:00
|
|
|
}
|
2015-10-12 06:21:02 +03:00
|
|
|
#endif
|
2017-09-26 07:15:36 +03:00
|
|
|
} else if (useD3D) {
|
|
|
|
#ifdef XP_WIN
|
2018-12-18 08:54:28 +03:00
|
|
|
// Ensure devices initialization. SharedSurfaceANGLE and
|
|
|
|
// SharedSurfaceD3D11Interop use them. The devices are lazily initialized
|
|
|
|
// with WebRender to reduce memory usage.
|
|
|
|
gfxPlatform::GetPlatform()->EnsureDevicesInitialized();
|
|
|
|
|
2017-09-26 07:15:36 +03:00
|
|
|
// Enable surface sharing only if ANGLE and compositing devices
|
|
|
|
// are both WARP or both not WARP
|
|
|
|
gfx::DeviceManagerDx* dm = gfx::DeviceManagerDx::Get();
|
|
|
|
if (gl->IsANGLE() && (gl->IsWARP() == dm->IsWARP()) &&
|
|
|
|
dm->TextureSharingWorks()) {
|
|
|
|
factory =
|
|
|
|
SurfaceFactory_ANGLEShareHandle::Create(gl, caps, ipcChannel, flags);
|
2015-10-12 06:21:02 +03:00
|
|
|
}
|
|
|
|
|
2019-05-26 17:29:42 +03:00
|
|
|
if (!factory && StaticPrefs::WebGLDXGLEnabled()) {
|
2017-09-26 07:15:36 +03:00
|
|
|
factory =
|
|
|
|
SurfaceFactory_D3D11Interop::Create(gl, caps, ipcChannel, flags);
|
|
|
|
}
|
|
|
|
#endif
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2018-05-24 15:06:31 +03:00
|
|
|
#ifdef MOZ_X11
|
2017-09-26 07:15:36 +03:00
|
|
|
if (!factory && sGLXLibrary.UseTextureFromPixmap()) {
|
|
|
|
factory = SurfaceFactory_GLXDrawable::Create(gl, caps, ipcChannel, flags);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
#endif
|
2017-09-26 07:15:36 +03:00
|
|
|
|
2015-10-12 06:21:02 +03:00
|
|
|
return factory;
|
|
|
|
}
|
|
|
|
|
2015-06-05 03:15:38 +03:00
|
|
|
GLScreenBuffer::GLScreenBuffer(GLContext* gl, const SurfaceCaps& caps,
|
|
|
|
UniquePtr<SurfaceFactory> factory)
|
|
|
|
: mGL(gl),
|
|
|
|
mCaps(caps),
|
2018-05-30 22:15:35 +03:00
|
|
|
mFactory(std::move(factory)),
|
2015-06-05 03:15:38 +03:00
|
|
|
mNeedsBlit(true),
|
|
|
|
mUserReadBufferMode(LOCAL_GL_BACK),
|
2015-07-31 22:56:32 +03:00
|
|
|
mUserDrawBufferMode(LOCAL_GL_BACK),
|
2015-06-05 03:15:38 +03:00
|
|
|
mUserDrawFB(0),
|
|
|
|
mUserReadFB(0),
|
|
|
|
mInternalDrawFB(0),
|
|
|
|
mInternalReadFB(0)
|
|
|
|
#ifdef DEBUG
|
|
|
|
,
|
|
|
|
mInInternalMode_DrawFB(true),
|
|
|
|
mInInternalMode_ReadFB(true)
|
|
|
|
#endif
|
2013-02-14 03:26:24 +04:00
|
|
|
{
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
GLScreenBuffer::~GLScreenBuffer() {
|
2015-06-05 03:15:38 +03:00
|
|
|
mFactory = nullptr;
|
2014-08-16 04:38:07 +04:00
|
|
|
mDraw = nullptr;
|
|
|
|
mRead = nullptr;
|
2016-06-23 19:41:51 +03:00
|
|
|
|
|
|
|
if (!mBack) return;
|
|
|
|
|
|
|
|
// Detach mBack cleanly.
|
|
|
|
mBack->Surf()->ProducerRelease();
|
2013-02-14 03:26:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void GLScreenBuffer::BindAsFramebuffer(GLContext* const gl,
|
|
|
|
GLenum target) const {
|
|
|
|
GLuint drawFB = DrawFB();
|
|
|
|
GLuint readFB = ReadFB();
|
|
|
|
|
2015-09-25 17:29:53 +03:00
|
|
|
if (!gl->IsSupported(GLFeature::split_framebuffer)) {
|
2013-02-14 03:26:24 +04:00
|
|
|
MOZ_ASSERT(drawFB == readFB);
|
|
|
|
gl->raw_fBindFramebuffer(target, readFB);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (target) {
|
|
|
|
case LOCAL_GL_FRAMEBUFFER:
|
|
|
|
gl->raw_fBindFramebuffer(LOCAL_GL_DRAW_FRAMEBUFFER_EXT, drawFB);
|
|
|
|
gl->raw_fBindFramebuffer(LOCAL_GL_READ_FRAMEBUFFER_EXT, readFB);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LOCAL_GL_DRAW_FRAMEBUFFER_EXT:
|
|
|
|
gl->raw_fBindFramebuffer(LOCAL_GL_DRAW_FRAMEBUFFER_EXT, drawFB);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LOCAL_GL_READ_FRAMEBUFFER_EXT:
|
|
|
|
gl->raw_fBindFramebuffer(LOCAL_GL_READ_FRAMEBUFFER_EXT, readFB);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2015-12-10 15:01:00 +03:00
|
|
|
MOZ_CRASH("GFX: Bad `target` for BindFramebuffer.");
|
2013-02-14 03:26:24 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLScreenBuffer::BindFB(GLuint fb) {
|
|
|
|
GLuint drawFB = DrawFB();
|
|
|
|
GLuint readFB = ReadFB();
|
|
|
|
|
|
|
|
mUserDrawFB = fb;
|
|
|
|
mUserReadFB = fb;
|
|
|
|
mInternalDrawFB = (fb == 0) ? drawFB : fb;
|
|
|
|
mInternalReadFB = (fb == 0) ? readFB : fb;
|
|
|
|
|
|
|
|
if (mInternalDrawFB == mInternalReadFB) {
|
|
|
|
mGL->raw_fBindFramebuffer(LOCAL_GL_FRAMEBUFFER, mInternalDrawFB);
|
|
|
|
} else {
|
2015-09-25 17:29:53 +03:00
|
|
|
MOZ_ASSERT(mGL->IsSupported(GLFeature::split_framebuffer));
|
2013-02-14 03:26:24 +04:00
|
|
|
mGL->raw_fBindFramebuffer(LOCAL_GL_DRAW_FRAMEBUFFER_EXT, mInternalDrawFB);
|
|
|
|
mGL->raw_fBindFramebuffer(LOCAL_GL_READ_FRAMEBUFFER_EXT, mInternalReadFB);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
mInInternalMode_DrawFB = false;
|
|
|
|
mInInternalMode_ReadFB = false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLScreenBuffer::BindDrawFB(GLuint fb) {
|
2015-09-25 17:29:53 +03:00
|
|
|
MOZ_ASSERT(mGL->IsSupported(GLFeature::split_framebuffer));
|
2013-02-14 03:26:24 +04:00
|
|
|
|
2014-08-08 06:03:25 +04:00
|
|
|
GLuint drawFB = DrawFB();
|
|
|
|
mUserDrawFB = fb;
|
|
|
|
mInternalDrawFB = (fb == 0) ? drawFB : fb;
|
2013-02-14 03:26:24 +04:00
|
|
|
|
2014-08-08 06:03:25 +04:00
|
|
|
mGL->raw_fBindFramebuffer(LOCAL_GL_DRAW_FRAMEBUFFER_EXT, mInternalDrawFB);
|
2013-02-14 03:26:24 +04:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
mInInternalMode_DrawFB = false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLScreenBuffer::BindReadFB(GLuint fb) {
|
2015-09-25 17:29:53 +03:00
|
|
|
MOZ_ASSERT(mGL->IsSupported(GLFeature::split_framebuffer));
|
2013-02-14 03:26:24 +04:00
|
|
|
|
2014-08-08 06:03:25 +04:00
|
|
|
GLuint readFB = ReadFB();
|
|
|
|
mUserReadFB = fb;
|
|
|
|
mInternalReadFB = (fb == 0) ? readFB : fb;
|
2013-02-14 03:26:24 +04:00
|
|
|
|
2014-08-08 06:03:25 +04:00
|
|
|
mGL->raw_fBindFramebuffer(LOCAL_GL_READ_FRAMEBUFFER_EXT, mInternalReadFB);
|
2013-02-14 03:26:24 +04:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
mInInternalMode_ReadFB = false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-08-08 06:03:25 +04:00
|
|
|
void GLScreenBuffer::BindFB_Internal(GLuint fb) {
|
|
|
|
mInternalDrawFB = mUserDrawFB = fb;
|
|
|
|
mInternalReadFB = mUserReadFB = fb;
|
|
|
|
mGL->raw_fBindFramebuffer(LOCAL_GL_FRAMEBUFFER, mInternalDrawFB);
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
mInInternalMode_DrawFB = true;
|
|
|
|
mInInternalMode_ReadFB = true;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
void GLScreenBuffer::BindDrawFB_Internal(GLuint fb) {
|
2015-09-25 17:29:53 +03:00
|
|
|
MOZ_ASSERT(mGL->IsSupported(GLFeature::split_framebuffer));
|
2014-08-08 06:03:25 +04:00
|
|
|
|
|
|
|
mInternalDrawFB = mUserDrawFB = fb;
|
|
|
|
mGL->raw_fBindFramebuffer(LOCAL_GL_DRAW_FRAMEBUFFER_EXT, mInternalDrawFB);
|
2013-02-14 03:26:24 +04:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
mInInternalMode_DrawFB = true;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLScreenBuffer::BindReadFB_Internal(GLuint fb) {
|
2015-09-25 17:29:53 +03:00
|
|
|
MOZ_ASSERT(mGL->IsSupported(GLFeature::split_framebuffer));
|
2014-08-08 06:03:25 +04:00
|
|
|
|
|
|
|
mInternalReadFB = mUserReadFB = fb;
|
|
|
|
mGL->raw_fBindFramebuffer(LOCAL_GL_READ_FRAMEBUFFER_EXT, mInternalReadFB);
|
2013-02-14 03:26:24 +04:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
mInInternalMode_ReadFB = true;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
GLuint GLScreenBuffer::GetDrawFB() const {
|
|
|
|
#ifdef DEBUG
|
|
|
|
MOZ_ASSERT(!mInInternalMode_DrawFB);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
// Don't need a branch here, because:
|
|
|
|
// LOCAL_GL_DRAW_FRAMEBUFFER_BINDING_EXT == LOCAL_GL_FRAMEBUFFER_BINDING ==
|
|
|
|
// 0x8CA6 We use raw_ here because this is debug code and we need to see what
|
|
|
|
// the driver thinks.
|
|
|
|
GLuint actual = 0;
|
|
|
|
mGL->raw_fGetIntegerv(LOCAL_GL_DRAW_FRAMEBUFFER_BINDING_EXT, (GLint*)&actual);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
GLuint predicted = mInternalDrawFB;
|
2018-11-27 07:23:19 +03:00
|
|
|
if (predicted != actual && !mGL->CheckContextLost()) {
|
2013-02-14 03:26:24 +04:00
|
|
|
printf_stderr("Misprediction: Bound draw FB predicted: %d. Was: %d.\n",
|
|
|
|
predicted, actual);
|
|
|
|
MOZ_ASSERT(false, "Draw FB binding misprediction!");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return mUserDrawFB;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLuint GLScreenBuffer::GetReadFB() const {
|
|
|
|
#ifdef DEBUG
|
|
|
|
MOZ_ASSERT(!mInInternalMode_ReadFB);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
// We use raw_ here because this is debug code and we need to see what
|
|
|
|
// the driver thinks.
|
|
|
|
GLuint actual = 0;
|
2015-09-25 17:29:53 +03:00
|
|
|
if (mGL->IsSupported(GLFeature::split_framebuffer))
|
2013-02-14 03:26:24 +04:00
|
|
|
mGL->raw_fGetIntegerv(LOCAL_GL_READ_FRAMEBUFFER_BINDING_EXT,
|
|
|
|
(GLint*)&actual);
|
|
|
|
else
|
|
|
|
mGL->raw_fGetIntegerv(LOCAL_GL_FRAMEBUFFER_BINDING, (GLint*)&actual);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
GLuint predicted = mInternalReadFB;
|
2018-11-27 07:23:19 +03:00
|
|
|
if (predicted != actual && !mGL->CheckContextLost()) {
|
2013-02-14 03:26:24 +04:00
|
|
|
printf_stderr("Misprediction: Bound read FB predicted: %d. Was: %d.\n",
|
|
|
|
predicted, actual);
|
|
|
|
MOZ_ASSERT(false, "Read FB binding misprediction!");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return mUserReadFB;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLuint GLScreenBuffer::GetFB() const {
|
|
|
|
MOZ_ASSERT(GetDrawFB() == GetReadFB());
|
|
|
|
return GetDrawFB();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLScreenBuffer::DeletingFB(GLuint fb) {
|
|
|
|
if (fb == mInternalDrawFB) {
|
|
|
|
mInternalDrawFB = 0;
|
|
|
|
mUserDrawFB = 0;
|
|
|
|
}
|
|
|
|
if (fb == mInternalReadFB) {
|
|
|
|
mInternalReadFB = 0;
|
|
|
|
mUserReadFB = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLScreenBuffer::AfterDrawCall() {
|
|
|
|
if (mUserDrawFB != 0) return;
|
|
|
|
|
|
|
|
RequireBlit();
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
void GLScreenBuffer::BeforeReadCall() {
|
|
|
|
if (mUserReadFB != 0) return;
|
|
|
|
|
|
|
|
AssureBlitted();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GLScreenBuffer::CopyTexImage2D(GLenum target, GLint level,
|
|
|
|
GLenum internalformat, GLint x, GLint y,
|
2015-03-23 06:55:26 +03:00
|
|
|
GLsizei width, GLsizei height,
|
2013-02-14 03:26:24 +04:00
|
|
|
GLint border) {
|
2015-03-23 06:55:26 +03:00
|
|
|
SharedSurface* surf;
|
2013-02-14 03:26:24 +04:00
|
|
|
if (GetReadFB() == 0) {
|
2015-03-23 06:55:26 +03:00
|
|
|
surf = SharedSurf();
|
2013-02-14 03:26:24 +04:00
|
|
|
} else {
|
|
|
|
surf = mGL->mFBOMapping[GetReadFB()];
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-03-23 06:55:26 +03:00
|
|
|
if (surf) {
|
2013-02-14 03:26:24 +04:00
|
|
|
return surf->CopyTexImage2D(target, level, internalformat, x, y, width,
|
2015-03-23 06:55:26 +03:00
|
|
|
height, border);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2013-02-14 03:26:24 +04:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-03-23 06:55:26 +03:00
|
|
|
bool GLScreenBuffer::ReadPixels(GLint x, GLint y, GLsizei width, GLsizei height,
|
|
|
|
GLenum format, GLenum type, GLvoid* pixels) {
|
2014-07-12 02:10:49 +04:00
|
|
|
// If the currently bound framebuffer is backed by a SharedSurface
|
2013-07-18 07:24:15 +04:00
|
|
|
// then it might want to override how we read pixel data from it.
|
2015-03-23 06:55:26 +03:00
|
|
|
// This is normally only the default framebuffer, but we can also
|
2013-07-18 07:24:15 +04:00
|
|
|
// have SharedSurfaces bound to other framebuffers when doing
|
|
|
|
// readback for BasicLayers.
|
2015-03-23 06:55:26 +03:00
|
|
|
SharedSurface* surf;
|
|
|
|
if (GetReadFB() == 0) {
|
|
|
|
surf = SharedSurf();
|
|
|
|
} else {
|
|
|
|
surf = mGL->mFBOMapping[GetReadFB()];
|
|
|
|
}
|
|
|
|
if (surf) {
|
|
|
|
return surf->ReadPixels(x, y, width, height, format, type, pixels);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-07-12 02:10:49 +04:00
|
|
|
void GLScreenBuffer::RequireBlit() { mNeedsBlit = true; }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-07-12 02:10:49 +04:00
|
|
|
void GLScreenBuffer::AssureBlitted() {
|
2013-02-14 03:26:24 +04:00
|
|
|
if (!mNeedsBlit) return;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
if (mDraw) {
|
2014-07-12 02:10:49 +04:00
|
|
|
GLuint drawFB = DrawFB();
|
|
|
|
GLuint readFB = ReadFB();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
MOZ_ASSERT(drawFB != 0);
|
|
|
|
MOZ_ASSERT(drawFB != readFB);
|
2014-07-12 02:10:49 +04:00
|
|
|
MOZ_ASSERT(mGL->IsSupported(GLFeature::split_framebuffer));
|
|
|
|
MOZ_ASSERT(mDraw->mSize == mRead->Size());
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
ScopedBindFramebuffer boundFB(mGL);
|
2014-07-12 02:10:49 +04:00
|
|
|
ScopedGLState scissor(mGL, LOCAL_GL_SCISSOR_TEST, false);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
BindReadFB_Internal(drawFB);
|
|
|
|
BindDrawFB_Internal(readFB);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-09-25 17:29:53 +03:00
|
|
|
if (mGL->IsSupported(GLFeature::framebuffer_blit)) {
|
2014-07-12 02:10:49 +04:00
|
|
|
const gfx::IntSize& srcSize = mDraw->mSize;
|
|
|
|
const gfx::IntSize& destSize = mRead->Size();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-07-12 02:10:49 +04:00
|
|
|
mGL->raw_fBlitFramebuffer(0, 0, srcSize.width, srcSize.height, 0, 0,
|
|
|
|
destSize.width, destSize.height,
|
|
|
|
LOCAL_GL_COLOR_BUFFER_BIT, LOCAL_GL_NEAREST);
|
|
|
|
} else if (mGL->IsExtensionSupported(
|
|
|
|
GLContext::APPLE_framebuffer_multisample)) {
|
2013-07-18 07:24:15 +04:00
|
|
|
mGL->fResolveMultisampleFramebufferAPPLE();
|
|
|
|
} else {
|
|
|
|
MOZ_CRASH("GFX: No available blit methods.");
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
// Done!
|
|
|
|
}
|
2013-07-18 07:24:15 +04:00
|
|
|
|
|
|
|
mNeedsBlit = false;
|
|
|
|
}
|
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
void GLScreenBuffer::Morph(UniquePtr<SurfaceFactory> newFactory) {
|
|
|
|
MOZ_RELEASE_ASSERT(newFactory, "newFactory must not be null");
|
|
|
|
mFactory = std::move(newFactory);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GLScreenBuffer::Attach(SharedSurface* surf, const gfx::IntSize& size) {
|
2015-09-25 17:29:53 +03:00
|
|
|
ScopedBindFramebuffer autoFB(mGL);
|
2013-02-14 03:26:24 +04:00
|
|
|
|
|
|
|
const bool readNeedsUnlock = (mRead && SharedSurf());
|
2016-06-23 19:42:23 +03:00
|
|
|
if (readNeedsUnlock) {
|
2013-02-14 03:26:24 +04:00
|
|
|
SharedSurf()->UnlockProd();
|
|
|
|
}
|
|
|
|
|
2018-05-30 22:15:35 +03:00
|
|
|
surf->LockProd();
|
2013-02-14 03:26:24 +04:00
|
|
|
|
2014-07-12 02:10:49 +04:00
|
|
|
if (mRead && surf->mAttachType == SharedSurf()->mAttachType &&
|
|
|
|
size == Size()) {
|
2013-02-14 03:26:24 +04:00
|
|
|
// Same size, same type, ready for reuse!
|
|
|
|
mRead->Attach(surf);
|
2018-11-30 13:46:48 +03:00
|
|
|
} else {
|
2013-02-14 03:26:24 +04:00
|
|
|
// Else something changed, so resize:
|
|
|
|
UniquePtr<DrawBuffer> draw;
|
2015-03-16 22:16:50 +03:00
|
|
|
bool drawOk = true;
|
2013-02-14 03:26:24 +04:00
|
|
|
|
2016-06-23 19:42:23 +03:00
|
|
|
/* Don't change out the draw buffer unless we resize. In the
|
2015-03-16 22:16:50 +03:00
|
|
|
* preserveDrawingBuffer:true case, prior contents of the buffer must
|
|
|
|
* be retained. If we're using a draw buffer, it's an MSAA buffer, so
|
2016-06-23 19:42:23 +03:00
|
|
|
* even if we copy the previous frame into the (single-sampled) read
|
|
|
|
* buffer, if we need to re-resolve from draw to read (as triggered by
|
|
|
|
* drawing), we'll need the old MSAA content to still be in the draw
|
2015-03-16 22:16:50 +03:00
|
|
|
* buffer.
|
2018-11-30 13:46:48 +03:00
|
|
|
*/
|
2016-06-23 19:42:23 +03:00
|
|
|
if (!mDraw || size != Size())
|
|
|
|
drawOk = CreateDraw(size, &draw); // Can be null.
|
2013-02-14 03:26:24 +04:00
|
|
|
|
|
|
|
UniquePtr<ReadBuffer> read = CreateRead(surf);
|
2014-04-29 05:11:14 +04:00
|
|
|
bool readOk = !!read;
|
2013-02-14 03:26:24 +04:00
|
|
|
|
|
|
|
if (!drawOk || !readOk) {
|
2014-07-12 02:10:49 +04:00
|
|
|
surf->UnlockProd();
|
2014-04-29 05:11:14 +04:00
|
|
|
if (readNeedsUnlock) {
|
2016-06-23 19:42:23 +03:00
|
|
|
SharedSurf()->LockProd();
|
|
|
|
}
|
2014-04-29 05:11:14 +04:00
|
|
|
return false;
|
2013-02-14 03:26:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (draw) mDraw = std::move(draw);
|
|
|
|
|
2015-02-19 03:57:05 +03:00
|
|
|
mRead = std::move(read);
|
|
|
|
}
|
|
|
|
|
2015-07-31 22:56:32 +03:00
|
|
|
// Check that we're all set up.
|
|
|
|
MOZ_ASSERT(SharedSurf() == surf);
|
|
|
|
|
2015-03-16 22:16:50 +03:00
|
|
|
// Update the ReadBuffer mode.
|
2015-02-19 03:57:05 +03:00
|
|
|
if (mGL->IsSupported(gl::GLFeature::read_buffer)) {
|
|
|
|
BindFB(0);
|
2015-03-16 22:16:50 +03:00
|
|
|
mRead->SetReadBuffer(mUserReadBufferMode);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-03-16 22:16:50 +03:00
|
|
|
|
2015-02-19 03:57:05 +03:00
|
|
|
// Update the DrawBuffer mode.
|
|
|
|
if (mGL->IsSupported(gl::GLFeature::draw_buffers)) {
|
|
|
|
BindFB(0);
|
2015-07-31 22:56:32 +03:00
|
|
|
SetDrawBuffer(mUserDrawBufferMode);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2015-03-16 22:16:50 +03:00
|
|
|
RequireBlit();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-04-29 05:11:14 +04:00
|
|
|
return true;
|
2013-03-22 23:22:20 +04:00
|
|
|
}
|
|
|
|
|
2013-12-10 20:12:18 +04:00
|
|
|
bool GLScreenBuffer::Swap(const gfx::IntSize& size) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<SharedSurfaceTextureClient> newBack = mFactory->NewTexClient(size);
|
2014-10-08 08:01:07 +04:00
|
|
|
if (!newBack) return false;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-02-13 06:31:58 +03:00
|
|
|
// In the case of DXGL interop, the new surface needs to be acquired before
|
2015-11-06 03:19:24 +03:00
|
|
|
// it is attached so that the interop surface is locked, which populates
|
|
|
|
// the GL renderbuffer. This results in the renderbuffer being ready and
|
|
|
|
// attachment to framebuffer succeeds in Attach() call.
|
|
|
|
newBack->Surf()->ProducerAcquire();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-06-23 19:42:23 +03:00
|
|
|
if (!Attach(newBack->Surf(), size)) {
|
|
|
|
newBack->Surf()->ProducerRelease();
|
2014-10-08 08:01:07 +04:00
|
|
|
return false;
|
2016-06-23 19:42:23 +03:00
|
|
|
}
|
2014-10-08 08:01:07 +04:00
|
|
|
// Attach was successful.
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-10-08 08:01:07 +04:00
|
|
|
mFront = mBack;
|
|
|
|
mBack = newBack;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-10-08 08:01:07 +04:00
|
|
|
if (ShouldPreserveBuffer() && mFront && mBack && !mDraw) {
|
|
|
|
auto src = mFront->Surf();
|
|
|
|
auto dest = mBack->Surf();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-01-15 05:04:50 +03:00
|
|
|
// uint32_t srcPixel = ReadPixel(src);
|
|
|
|
// uint32_t destPixel = ReadPixel(dest);
|
|
|
|
// printf_stderr("Before: src: 0x%08x, dest: 0x%08x\n", srcPixel,
|
|
|
|
// destPixel);
|
2015-09-24 22:21:05 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
GLContext::LocalErrorScope errorScope(*mGL);
|
|
|
|
#endif
|
2015-01-15 05:04:50 +03:00
|
|
|
|
2014-10-08 08:01:07 +04:00
|
|
|
SharedSurface::ProdCopy(src, dest, mFactory.get());
|
2015-01-15 05:04:50 +03:00
|
|
|
|
2015-09-24 22:21:05 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
MOZ_ASSERT(!errorScope.GetError());
|
|
|
|
#endif
|
|
|
|
|
2015-01-15 05:04:50 +03:00
|
|
|
// srcPixel = ReadPixel(src);
|
|
|
|
// destPixel = ReadPixel(dest);
|
|
|
|
// printf_stderr("After: src: 0x%08x, dest: 0x%08x\n", srcPixel, destPixel);
|
2013-03-22 23:22:20 +04:00
|
|
|
}
|
|
|
|
|
2015-02-27 22:17:55 +03:00
|
|
|
// XXX: We would prefer to fence earlier on platforms that don't need
|
|
|
|
// the full ProducerAcquire/ProducerRelease semantics, so that the fence
|
|
|
|
// doesn't include the copy operation. Unfortunately, the current API
|
|
|
|
// doesn't expose a good way to do that.
|
|
|
|
if (mFront) {
|
|
|
|
mFront->Surf()->ProducerRelease();
|
|
|
|
}
|
|
|
|
|
2014-10-08 08:01:07 +04:00
|
|
|
return true;
|
2013-02-14 03:26:24 +04:00
|
|
|
}
|
|
|
|
|
2013-12-10 20:12:18 +04:00
|
|
|
bool GLScreenBuffer::PublishFrame(const gfx::IntSize& size) {
|
2013-02-14 03:26:24 +04:00
|
|
|
AssureBlitted();
|
|
|
|
|
|
|
|
bool good = Swap(size);
|
|
|
|
return good;
|
|
|
|
}
|
|
|
|
|
2013-12-10 20:12:18 +04:00
|
|
|
bool GLScreenBuffer::Resize(const gfx::IntSize& size) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<SharedSurfaceTextureClient> newBack = mFactory->NewTexClient(size);
|
2014-10-08 08:01:07 +04:00
|
|
|
if (!newBack) return false;
|
|
|
|
|
|
|
|
if (!Attach(newBack->Surf(), size)) return false;
|
2013-03-22 23:22:20 +04:00
|
|
|
|
2014-10-10 00:33:22 +04:00
|
|
|
if (mBack) mBack->Surf()->ProducerRelease();
|
|
|
|
|
2014-10-08 08:01:07 +04:00
|
|
|
mBack = newBack;
|
2014-10-10 00:33:22 +04:00
|
|
|
|
|
|
|
mBack->Surf()->ProducerAcquire();
|
|
|
|
|
2014-10-08 08:01:07 +04:00
|
|
|
return true;
|
2013-03-22 23:22:20 +04:00
|
|
|
}
|
2013-02-14 03:26:24 +04:00
|
|
|
|
2014-08-16 04:38:07 +04:00
|
|
|
bool GLScreenBuffer::CreateDraw(const gfx::IntSize& size,
|
|
|
|
UniquePtr<DrawBuffer>* out_buffer) {
|
2014-07-12 02:10:49 +04:00
|
|
|
GLContext* gl = mFactory->mGL;
|
|
|
|
const GLFormats& formats = mFactory->mFormats;
|
2013-02-14 03:26:24 +04:00
|
|
|
const SurfaceCaps& caps = mFactory->DrawCaps();
|
|
|
|
|
2014-04-29 05:11:14 +04:00
|
|
|
return DrawBuffer::Create(gl, caps, formats, size, out_buffer);
|
2013-02-14 03:26:24 +04:00
|
|
|
}
|
|
|
|
|
2014-07-12 02:10:49 +04:00
|
|
|
UniquePtr<ReadBuffer> GLScreenBuffer::CreateRead(SharedSurface* surf) {
|
|
|
|
GLContext* gl = mFactory->mGL;
|
|
|
|
const GLFormats& formats = mFactory->mFormats;
|
2013-02-14 03:26:24 +04:00
|
|
|
const SurfaceCaps& caps = mFactory->ReadCaps();
|
|
|
|
|
|
|
|
return ReadBuffer::Create(gl, caps, formats, surf);
|
|
|
|
}
|
|
|
|
|
2015-07-31 22:56:32 +03:00
|
|
|
void GLScreenBuffer::SetDrawBuffer(GLenum mode) {
|
|
|
|
MOZ_ASSERT(mGL->IsSupported(gl::GLFeature::draw_buffers));
|
|
|
|
MOZ_ASSERT(GetDrawFB() == 0);
|
|
|
|
|
|
|
|
if (!mGL->IsSupported(GLFeature::draw_buffers)) return;
|
|
|
|
|
|
|
|
mUserDrawBufferMode = mode;
|
|
|
|
|
|
|
|
GLuint fb = mDraw ? mDraw->mFB : mRead->mFB;
|
|
|
|
GLenum internalMode;
|
|
|
|
|
|
|
|
switch (mode) {
|
|
|
|
case LOCAL_GL_BACK:
|
|
|
|
internalMode = (fb == 0) ? LOCAL_GL_BACK : LOCAL_GL_COLOR_ATTACHMENT0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LOCAL_GL_NONE:
|
|
|
|
internalMode = LOCAL_GL_NONE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2015-12-10 15:01:00 +03:00
|
|
|
MOZ_CRASH("GFX: Bad value.");
|
2015-07-31 22:56:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mGL->MakeCurrent();
|
|
|
|
mGL->fDrawBuffers(1, &internalMode);
|
|
|
|
}
|
|
|
|
|
2015-02-19 03:57:05 +03:00
|
|
|
void GLScreenBuffer::SetReadBuffer(GLenum mode) {
|
|
|
|
MOZ_ASSERT(mGL->IsSupported(gl::GLFeature::read_buffer));
|
|
|
|
MOZ_ASSERT(GetReadFB() == 0);
|
|
|
|
|
|
|
|
mUserReadBufferMode = mode;
|
|
|
|
mRead->SetReadBuffer(mUserReadBufferMode);
|
|
|
|
}
|
|
|
|
|
2014-10-15 01:49:49 +04:00
|
|
|
bool GLScreenBuffer::IsDrawFramebufferDefault() const {
|
|
|
|
if (!mDraw) return IsReadFramebufferDefault();
|
|
|
|
return mDraw->mFB == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GLScreenBuffer::IsReadFramebufferDefault() const {
|
|
|
|
return SharedSurf()->mAttachType == AttachmentType::Screen;
|
|
|
|
}
|
|
|
|
|
2016-04-25 23:17:25 +03:00
|
|
|
uint32_t GLScreenBuffer::DepthBits() const {
|
|
|
|
const GLFormats& formats = mFactory->mFormats;
|
|
|
|
|
|
|
|
if (formats.depth == LOCAL_GL_DEPTH_COMPONENT16) return 16;
|
|
|
|
|
|
|
|
return 24;
|
|
|
|
}
|
|
|
|
|
2014-07-12 02:10:49 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
2015-09-29 06:35:46 +03:00
|
|
|
// Utils
|
|
|
|
|
|
|
|
static void RenderbufferStorageBySamples(GLContext* aGL, GLsizei aSamples,
|
|
|
|
GLenum aInternalFormat,
|
|
|
|
const gfx::IntSize& aSize) {
|
|
|
|
if (aSamples) {
|
|
|
|
aGL->fRenderbufferStorageMultisample(LOCAL_GL_RENDERBUFFER, aSamples,
|
|
|
|
aInternalFormat, aSize.width,
|
|
|
|
aSize.height);
|
|
|
|
} else {
|
|
|
|
aGL->fRenderbufferStorage(LOCAL_GL_RENDERBUFFER, aInternalFormat,
|
|
|
|
aSize.width, aSize.height);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static GLuint CreateRenderbuffer(GLContext* aGL, GLenum aFormat,
|
|
|
|
GLsizei aSamples, const gfx::IntSize& aSize) {
|
|
|
|
GLuint rb = 0;
|
|
|
|
aGL->fGenRenderbuffers(1, &rb);
|
|
|
|
ScopedBindRenderbuffer autoRB(aGL, rb);
|
|
|
|
|
|
|
|
RenderbufferStorageBySamples(aGL, aSamples, aFormat, aSize);
|
|
|
|
|
|
|
|
return rb;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void CreateRenderbuffersForOffscreen(
|
|
|
|
GLContext* aGL, const GLFormats& aFormats, const gfx::IntSize& aSize,
|
|
|
|
bool aMultisample, GLuint* aColorMSRB, GLuint* aDepthRB,
|
|
|
|
GLuint* aStencilRB) {
|
|
|
|
GLsizei samples = aMultisample ? aFormats.samples : 0;
|
|
|
|
if (aColorMSRB) {
|
|
|
|
MOZ_ASSERT(aFormats.samples > 0);
|
|
|
|
MOZ_ASSERT(aFormats.color_rbFormat);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-09-30 02:04:17 +03:00
|
|
|
GLenum colorFormat = aFormats.color_rbFormat;
|
|
|
|
if (aGL->IsANGLE()) {
|
|
|
|
MOZ_ASSERT(colorFormat == LOCAL_GL_RGBA8);
|
|
|
|
colorFormat = LOCAL_GL_BGRA8_EXT;
|
2015-09-29 06:35:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
*aColorMSRB = CreateRenderbuffer(aGL, colorFormat, samples, aSize);
|
|
|
|
}
|
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
if (aDepthRB && aStencilRB && aFormats.depthStencil) {
|
2015-09-29 06:35:46 +03:00
|
|
|
*aDepthRB = CreateRenderbuffer(aGL, aFormats.depthStencil, samples, aSize);
|
|
|
|
*aStencilRB = *aDepthRB;
|
|
|
|
} else {
|
|
|
|
if (aDepthRB) {
|
|
|
|
MOZ_ASSERT(aFormats.depth);
|
|
|
|
|
|
|
|
*aDepthRB = CreateRenderbuffer(aGL, aFormats.depth, samples, aSize);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-09-29 06:35:46 +03:00
|
|
|
|
|
|
|
if (aStencilRB) {
|
|
|
|
MOZ_ASSERT(aFormats.stencil);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-09-29 06:35:46 +03:00
|
|
|
*aStencilRB = CreateRenderbuffer(aGL, aFormats.stencil, samples, aSize);
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-09-29 06:35:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
2014-07-12 02:10:49 +04:00
|
|
|
// DrawBuffer
|
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
bool DrawBuffer::Create(GLContext* const gl, const SurfaceCaps& caps,
|
|
|
|
const GLFormats& formats, const gfx::IntSize& size,
|
2014-08-16 04:38:07 +04:00
|
|
|
UniquePtr<DrawBuffer>* out_buffer) {
|
2014-04-29 05:11:14 +04:00
|
|
|
MOZ_ASSERT(out_buffer);
|
|
|
|
*out_buffer = nullptr;
|
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
if (!caps.color) {
|
|
|
|
MOZ_ASSERT(!caps.alpha && !caps.depth && !caps.stencil);
|
|
|
|
|
|
|
|
// Nothing is needed.
|
2014-04-29 05:11:14 +04:00
|
|
|
return true;
|
2013-02-14 03:26:24 +04:00
|
|
|
}
|
|
|
|
|
2014-10-26 04:32:46 +03:00
|
|
|
if (caps.antialias) {
|
|
|
|
if (formats.samples == 0) return false; // Can't create it.
|
|
|
|
|
2017-12-20 12:09:27 +03:00
|
|
|
MOZ_ASSERT(uint32_t(formats.samples) <= gl->MaxSamples());
|
2014-10-26 04:32:46 +03:00
|
|
|
}
|
2014-08-22 01:59:25 +04:00
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
GLuint colorMSRB = 0;
|
|
|
|
GLuint depthRB = 0;
|
|
|
|
GLuint stencilRB = 0;
|
|
|
|
|
|
|
|
GLuint* pColorMSRB = caps.antialias ? &colorMSRB : nullptr;
|
|
|
|
GLuint* pDepthRB = caps.depth ? &depthRB : nullptr;
|
|
|
|
GLuint* pStencilRB = caps.stencil ? &stencilRB : nullptr;
|
|
|
|
|
|
|
|
if (!formats.color_rbFormat) pColorMSRB = nullptr;
|
|
|
|
|
|
|
|
if (pDepthRB && pStencilRB) {
|
|
|
|
if (!formats.depth && !formats.depthStencil) pDepthRB = nullptr;
|
|
|
|
|
|
|
|
if (!formats.stencil && !formats.depthStencil) pStencilRB = nullptr;
|
|
|
|
} else {
|
|
|
|
if (!formats.depth) pDepthRB = nullptr;
|
|
|
|
|
|
|
|
if (!formats.stencil) pStencilRB = nullptr;
|
|
|
|
}
|
|
|
|
|
2014-10-25 03:52:35 +04:00
|
|
|
GLContext::LocalErrorScope localError(*gl);
|
2014-08-28 03:18:43 +04:00
|
|
|
|
2013-12-06 19:36:13 +04:00
|
|
|
CreateRenderbuffersForOffscreen(gl, formats, size, caps.antialias, pColorMSRB,
|
|
|
|
pDepthRB, pStencilRB);
|
2013-02-14 03:26:24 +04:00
|
|
|
|
|
|
|
GLuint fb = 0;
|
|
|
|
gl->fGenFramebuffers(1, &fb);
|
|
|
|
gl->AttachBuffersToFB(0, colorMSRB, depthRB, stencilRB, fb);
|
|
|
|
|
2016-02-13 06:31:58 +03:00
|
|
|
const GLsizei samples = formats.samples;
|
2015-01-13 02:05:21 +03:00
|
|
|
UniquePtr<DrawBuffer> ret(
|
|
|
|
new DrawBuffer(gl, size, samples, fb, colorMSRB, depthRB, stencilRB));
|
2014-04-29 05:11:14 +04:00
|
|
|
|
2014-10-25 03:52:35 +04:00
|
|
|
GLenum err = localError.GetError();
|
|
|
|
MOZ_ASSERT_IF(err != LOCAL_GL_NO_ERROR, err == LOCAL_GL_OUT_OF_MEMORY);
|
2014-08-28 03:18:43 +04:00
|
|
|
if (err || !gl->IsFramebufferComplete(fb)) return false;
|
2014-04-29 05:11:14 +04:00
|
|
|
|
2018-05-30 22:15:35 +03:00
|
|
|
*out_buffer = std::move(ret);
|
2014-04-29 05:11:14 +04:00
|
|
|
return true;
|
2013-02-14 03:26:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
DrawBuffer::~DrawBuffer() {
|
2016-02-03 12:59:22 +03:00
|
|
|
if (!mGL->MakeCurrent()) return;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
GLuint fb = mFB;
|
|
|
|
GLuint rbs[] = {
|
|
|
|
mColorMSRB, mDepthRB,
|
2017-01-09 18:25:00 +03:00
|
|
|
(mStencilRB != mDepthRB) ? mStencilRB
|
|
|
|
: 0, // Don't double-delete DEPTH_STENCIL RBs.
|
2013-02-14 03:26:24 +04:00
|
|
|
};
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
mGL->fDeleteFramebuffers(1, &fb);
|
|
|
|
mGL->fDeleteRenderbuffers(3, rbs);
|
|
|
|
}
|
|
|
|
|
2014-07-12 02:10:49 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// ReadBuffer
|
2013-02-14 03:26:24 +04:00
|
|
|
|
|
|
|
UniquePtr<ReadBuffer> ReadBuffer::Create(GLContext* gl, const SurfaceCaps& caps,
|
|
|
|
const GLFormats& formats,
|
2014-07-12 02:10:49 +04:00
|
|
|
SharedSurface* surf) {
|
2013-02-14 03:26:24 +04:00
|
|
|
MOZ_ASSERT(surf);
|
|
|
|
|
2014-07-12 02:10:49 +04:00
|
|
|
if (surf->mAttachType == AttachmentType::Screen) {
|
2013-02-14 03:26:24 +04:00
|
|
|
// Don't need anything. Our read buffer will be the 'screen'.
|
2014-08-16 04:38:07 +04:00
|
|
|
return UniquePtr<ReadBuffer>(new ReadBuffer(gl, 0, 0, 0, surf));
|
2013-02-14 03:26:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
GLuint depthRB = 0;
|
|
|
|
GLuint stencilRB = 0;
|
|
|
|
|
|
|
|
GLuint* pDepthRB = caps.depth ? &depthRB : nullptr;
|
|
|
|
GLuint* pStencilRB = caps.stencil ? &stencilRB : nullptr;
|
|
|
|
|
2014-10-25 03:52:35 +04:00
|
|
|
GLContext::LocalErrorScope localError(*gl);
|
2014-08-28 03:18:43 +04:00
|
|
|
|
2014-07-12 02:10:49 +04:00
|
|
|
CreateRenderbuffersForOffscreen(gl, formats, surf->mSize, caps.antialias,
|
2013-12-06 19:36:13 +04:00
|
|
|
nullptr, pDepthRB, pStencilRB);
|
2013-02-14 03:26:24 +04:00
|
|
|
|
|
|
|
GLuint colorTex = 0;
|
|
|
|
GLuint colorRB = 0;
|
2013-07-18 07:24:15 +04:00
|
|
|
GLenum target = 0;
|
2013-02-14 03:26:24 +04:00
|
|
|
|
2014-07-12 02:10:49 +04:00
|
|
|
switch (surf->mAttachType) {
|
2013-02-14 03:26:24 +04:00
|
|
|
case AttachmentType::GLTexture:
|
2014-03-11 06:08:49 +04:00
|
|
|
colorTex = surf->ProdTexture();
|
|
|
|
target = surf->ProdTextureTarget();
|
2013-02-14 03:26:24 +04:00
|
|
|
break;
|
|
|
|
case AttachmentType::GLRenderbuffer:
|
2014-03-11 06:08:49 +04:00
|
|
|
colorRB = surf->ProdRenderbuffer();
|
2013-02-14 03:26:24 +04:00
|
|
|
break;
|
|
|
|
default:
|
2015-12-10 15:01:00 +03:00
|
|
|
MOZ_CRASH("GFX: Unknown attachment type, create?");
|
2013-02-14 03:26:24 +04:00
|
|
|
}
|
|
|
|
MOZ_ASSERT(colorTex || colorRB);
|
|
|
|
|
|
|
|
GLuint fb = 0;
|
|
|
|
gl->fGenFramebuffers(1, &fb);
|
2013-07-18 07:24:15 +04:00
|
|
|
gl->AttachBuffersToFB(colorTex, colorRB, depthRB, stencilRB, fb, target);
|
2013-07-18 07:24:15 +04:00
|
|
|
gl->mFBOMapping[fb] = surf;
|
2013-02-14 03:26:24 +04:00
|
|
|
|
2014-08-16 04:38:07 +04:00
|
|
|
UniquePtr<ReadBuffer> ret(new ReadBuffer(gl, fb, depthRB, stencilRB, surf));
|
2014-08-28 03:18:43 +04:00
|
|
|
|
2014-10-25 03:52:35 +04:00
|
|
|
GLenum err = localError.GetError();
|
|
|
|
MOZ_ASSERT_IF(err != LOCAL_GL_NO_ERROR, err == LOCAL_GL_OUT_OF_MEMORY);
|
2016-07-09 03:44:52 +03:00
|
|
|
if (err) return nullptr;
|
|
|
|
|
|
|
|
const bool needsAcquire = !surf->IsProducerAcquired();
|
|
|
|
if (needsAcquire) {
|
2016-09-14 13:02:37 +03:00
|
|
|
surf->ProducerReadAcquire();
|
2016-07-09 03:44:52 +03:00
|
|
|
}
|
|
|
|
const bool isComplete = gl->IsFramebufferComplete(fb);
|
|
|
|
if (needsAcquire) {
|
2016-09-14 13:02:37 +03:00
|
|
|
surf->ProducerReadRelease();
|
2014-08-16 04:38:08 +04:00
|
|
|
}
|
2014-04-29 05:11:14 +04:00
|
|
|
|
2016-07-09 03:44:52 +03:00
|
|
|
if (!isComplete) return nullptr;
|
|
|
|
|
2018-06-01 18:59:07 +03:00
|
|
|
return ret;
|
2013-02-14 03:26:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
ReadBuffer::~ReadBuffer() {
|
2016-02-03 12:59:22 +03:00
|
|
|
if (!mGL->MakeCurrent()) return;
|
2013-02-14 03:26:24 +04:00
|
|
|
|
|
|
|
GLuint fb = mFB;
|
|
|
|
GLuint rbs[] = {
|
|
|
|
mDepthRB,
|
2017-01-09 18:25:00 +03:00
|
|
|
(mStencilRB != mDepthRB) ? mStencilRB
|
|
|
|
: 0, // Don't double-delete DEPTH_STENCIL RBs.
|
2013-02-14 03:26:24 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
mGL->fDeleteFramebuffers(1, &fb);
|
|
|
|
mGL->fDeleteRenderbuffers(2, rbs);
|
2017-01-09 18:25:00 +03:00
|
|
|
|
2013-07-18 07:24:15 +04:00
|
|
|
mGL->mFBOMapping.erase(mFB);
|
2013-02-14 03:26:24 +04:00
|
|
|
}
|
|
|
|
|
2014-07-12 02:10:49 +04:00
|
|
|
void ReadBuffer::Attach(SharedSurface* surf) {
|
2013-02-14 03:26:24 +04:00
|
|
|
MOZ_ASSERT(surf && mSurf);
|
2014-07-12 02:10:49 +04:00
|
|
|
MOZ_ASSERT(surf->mAttachType == mSurf->mAttachType);
|
|
|
|
MOZ_ASSERT(surf->mSize == mSurf->mSize);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
// Nothing else is needed for AttachType Screen.
|
2014-07-12 02:10:49 +04:00
|
|
|
if (surf->mAttachType != AttachmentType::Screen) {
|
2013-02-14 03:26:24 +04:00
|
|
|
GLuint colorTex = 0;
|
|
|
|
GLuint colorRB = 0;
|
2013-07-18 07:24:15 +04:00
|
|
|
GLenum target = 0;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-07-12 02:10:49 +04:00
|
|
|
switch (surf->mAttachType) {
|
2013-02-14 03:26:24 +04:00
|
|
|
case AttachmentType::GLTexture:
|
2014-03-11 06:08:49 +04:00
|
|
|
colorTex = surf->ProdTexture();
|
|
|
|
target = surf->ProdTextureTarget();
|
2013-02-14 03:26:24 +04:00
|
|
|
break;
|
|
|
|
case AttachmentType::GLRenderbuffer:
|
2014-03-11 06:08:49 +04:00
|
|
|
colorRB = surf->ProdRenderbuffer();
|
2013-02-14 03:26:24 +04:00
|
|
|
break;
|
|
|
|
default:
|
2015-12-10 15:01:00 +03:00
|
|
|
MOZ_CRASH("GFX: Unknown attachment type, attach?");
|
2013-02-14 03:26:24 +04:00
|
|
|
}
|
|
|
|
|
2013-07-18 07:24:15 +04:00
|
|
|
mGL->AttachBuffersToFB(colorTex, colorRB, 0, 0, mFB, target);
|
2013-02-14 03:26:24 +04:00
|
|
|
mGL->mFBOMapping[mFB] = surf;
|
2018-11-27 07:23:19 +03:00
|
|
|
MOZ_GL_ASSERT(mGL, mGL->IsFramebufferComplete(mFB));
|
2013-02-14 03:26:24 +04:00
|
|
|
}
|
|
|
|
|
2014-07-12 02:10:49 +04:00
|
|
|
mSurf = surf;
|
2013-02-14 03:26:24 +04:00
|
|
|
}
|
|
|
|
|
2015-09-25 17:29:53 +03:00
|
|
|
const gfx::IntSize& ReadBuffer::Size() const { return mSurf->mSize; }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-02-19 03:57:05 +03:00
|
|
|
void ReadBuffer::SetReadBuffer(GLenum userMode) const {
|
|
|
|
if (!mGL->IsSupported(GLFeature::read_buffer)) return;
|
|
|
|
|
|
|
|
GLenum internalMode;
|
|
|
|
|
|
|
|
switch (userMode) {
|
|
|
|
case LOCAL_GL_BACK:
|
2015-07-30 19:40:56 +03:00
|
|
|
case LOCAL_GL_FRONT:
|
|
|
|
internalMode = (mFB == 0) ? userMode : LOCAL_GL_COLOR_ATTACHMENT0;
|
2015-02-19 03:57:05 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LOCAL_GL_NONE:
|
|
|
|
internalMode = LOCAL_GL_NONE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2015-12-10 15:01:00 +03:00
|
|
|
MOZ_CRASH("GFX: Bad value.");
|
2015-02-19 03:57:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mGL->MakeCurrent();
|
|
|
|
mGL->fReadBuffer(internalMode);
|
|
|
|
}
|
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
} /* namespace gl */
|
|
|
|
} /* namespace mozilla */
|