2013-02-13 15:26:24 -08:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40; -*- */
|
|
|
|
/* 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-07 21:11:54 -07:00
|
|
|
#include "CompositorTypes.h"
|
2013-02-13 15:26:24 -08:00
|
|
|
#include "GLContext.h"
|
2013-12-06 07:36:13 -08:00
|
|
|
#include "GLBlitHelper.h"
|
2014-01-02 07:17:29 -08:00
|
|
|
#include "GLReadTexImageHelper.h"
|
2013-02-13 15:26:24 -08:00
|
|
|
#include "SharedSurfaceGL.h"
|
2013-05-27 07:12:13 -07:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
#include "SharedSurfaceGralloc.h"
|
|
|
|
#include "nsXULAppAPI.h"
|
|
|
|
#endif
|
2013-07-17 20:24:15 -07:00
|
|
|
#ifdef XP_MACOSX
|
|
|
|
#include "SharedSurfaceIO.h"
|
|
|
|
#endif
|
2013-11-30 14:20:57 -08:00
|
|
|
#include "ScopedGLHelpers.h"
|
2014-02-12 07:07:46 -08:00
|
|
|
#include "gfx2DGlue.h"
|
2014-08-15 17:38:08 -07:00
|
|
|
#include "../layers/ipc/ShadowLayers.h"
|
2013-02-13 15:26:24 -08:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace gl {
|
|
|
|
|
2014-07-11 15:10:49 -07:00
|
|
|
using gfx::SurfaceFormat;
|
2014-05-22 03:11:45 -07:00
|
|
|
|
2014-08-15 17:38:07 -07:00
|
|
|
UniquePtr<GLScreenBuffer>
|
2013-02-13 15:26:24 -08:00
|
|
|
GLScreenBuffer::Create(GLContext* gl,
|
2014-07-11 15:10:49 -07:00
|
|
|
const gfx::IntSize& size,
|
|
|
|
const SurfaceCaps& caps)
|
2013-02-13 15:26:24 -08:00
|
|
|
{
|
2014-08-15 17:38:08 -07:00
|
|
|
UniquePtr<GLScreenBuffer> ret;
|
2013-02-13 15:26:24 -08:00
|
|
|
if (caps.antialias &&
|
2013-08-22 10:42:05 -07:00
|
|
|
!gl->IsSupported(GLFeature::framebuffer_multisample))
|
2013-02-13 15:26:24 -08:00
|
|
|
{
|
2014-08-15 17:38:08 -07:00
|
|
|
return Move(ret);
|
2013-02-13 15:26:24 -08:00
|
|
|
}
|
|
|
|
|
2014-08-15 17:38:08 -07:00
|
|
|
UniquePtr<SurfaceFactory> factory;
|
2013-05-27 07:12:13 -07:00
|
|
|
|
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
/* On B2G, we want a Gralloc factory, and we want one right at the start */
|
2014-10-08 13:04:19 -07:00
|
|
|
layers::ISurfaceAllocator* allocator = caps.surfaceAllocator;
|
2013-05-27 07:12:13 -07:00
|
|
|
if (!factory &&
|
2014-10-07 21:11:54 -07:00
|
|
|
allocator &&
|
2013-05-27 07:12:13 -07:00
|
|
|
XRE_GetProcessType() != GeckoProcessType_Default)
|
|
|
|
{
|
2014-10-10 13:57:19 -07:00
|
|
|
layers::TextureFlags flags = layers::TextureFlags::DEALLOCATE_CLIENT |
|
|
|
|
layers::TextureFlags::NEEDS_Y_FLIP;
|
2014-10-07 21:11:54 -07:00
|
|
|
if (!caps.premultAlpha) {
|
2014-10-10 13:57:19 -07:00
|
|
|
flags |= layers::TextureFlags::NON_PREMULTIPLIED;
|
2014-10-07 21:11:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
factory = MakeUnique<SurfaceFactory_Gralloc>(gl, caps, flags,
|
|
|
|
allocator);
|
2013-05-27 07:12:13 -07:00
|
|
|
}
|
|
|
|
#endif
|
2013-07-17 20:24:15 -07:00
|
|
|
#ifdef XP_MACOSX
|
|
|
|
/* On OSX, we want an IOSurface factory, and we want one right at the start */
|
2014-07-11 15:10:49 -07:00
|
|
|
if (!factory) {
|
2014-07-11 23:08:54 -07:00
|
|
|
factory = SurfaceFactory_IOSurface::Create(gl, caps);
|
2013-07-17 20:24:15 -07:00
|
|
|
}
|
|
|
|
#endif
|
2013-05-27 07:12:13 -07:00
|
|
|
|
2014-08-15 17:38:08 -07:00
|
|
|
if (!factory) {
|
|
|
|
factory = MakeUnique<SurfaceFactory_Basic>(gl, caps);
|
|
|
|
}
|
2013-05-27 07:12:13 -07:00
|
|
|
|
2014-10-07 21:01:07 -07:00
|
|
|
ret.reset( new GLScreenBuffer(gl, caps, Move(factory)) );
|
2014-08-15 17:38:07 -07:00
|
|
|
return Move(ret);
|
2013-02-13 15:26:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
GLScreenBuffer::~GLScreenBuffer()
|
|
|
|
{
|
2014-08-15 17:38:07 -07:00
|
|
|
mDraw = nullptr;
|
|
|
|
mRead = nullptr;
|
2013-09-25 17:18:30 -07:00
|
|
|
|
|
|
|
// bug 914823: it is crucial to destroy the Factory _after_ we destroy
|
|
|
|
// the SharedSurfaces around here! Reason: the shared surfaces will want
|
|
|
|
// to ask the Allocator (e.g. the ClientLayerManager) to destroy their
|
|
|
|
// buffers, but that Allocator may be kept alive by the Factory,
|
|
|
|
// as it currently the case in SurfaceFactory_Gralloc holding a nsRefPtr
|
|
|
|
// to the Allocator!
|
2014-08-15 17:38:08 -07:00
|
|
|
mFactory = nullptr;
|
2013-02-13 15:26:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
GLScreenBuffer::BindAsFramebuffer(GLContext* const gl, GLenum target) const
|
|
|
|
{
|
|
|
|
GLuint drawFB = DrawFB();
|
|
|
|
GLuint readFB = ReadFB();
|
|
|
|
|
2013-08-22 10:42:05 -07:00
|
|
|
if (!gl->IsSupported(GLFeature::framebuffer_blit)) {
|
2013-02-13 15:26:24 -08: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:
|
2013-08-22 10:42:05 -07:00
|
|
|
if (!gl->IsSupported(GLFeature::framebuffer_blit))
|
2013-02-13 15:26:24 -08:00
|
|
|
NS_WARNING("DRAW_FRAMEBUFFER requested but unavailable.");
|
|
|
|
|
|
|
|
gl->raw_fBindFramebuffer(LOCAL_GL_DRAW_FRAMEBUFFER_EXT, drawFB);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LOCAL_GL_READ_FRAMEBUFFER_EXT:
|
2013-08-22 10:42:05 -07:00
|
|
|
if (!gl->IsSupported(GLFeature::framebuffer_blit))
|
2013-02-13 15:26:24 -08:00
|
|
|
NS_WARNING("READ_FRAMEBUFFER requested but unavailable.");
|
|
|
|
|
|
|
|
gl->raw_fBindFramebuffer(LOCAL_GL_READ_FRAMEBUFFER_EXT, readFB);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2013-06-28 18:38:30 -07:00
|
|
|
MOZ_CRASH("Bad `target` for BindFramebuffer.");
|
2013-02-13 15:26:24 -08: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 {
|
2013-08-22 10:42:05 -07:00
|
|
|
MOZ_ASSERT(mGL->IsSupported(GLFeature::framebuffer_blit));
|
2013-02-13 15:26:24 -08: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)
|
|
|
|
{
|
2014-08-07 19:03:25 -07:00
|
|
|
MOZ_ASSERT(mGL->IsSupported(GLFeature::framebuffer_blit));
|
2013-02-13 15:26:24 -08:00
|
|
|
|
2014-08-07 19:03:25 -07:00
|
|
|
GLuint drawFB = DrawFB();
|
|
|
|
mUserDrawFB = fb;
|
|
|
|
mInternalDrawFB = (fb == 0) ? drawFB : fb;
|
2013-02-13 15:26:24 -08:00
|
|
|
|
2014-08-07 19:03:25 -07:00
|
|
|
mGL->raw_fBindFramebuffer(LOCAL_GL_DRAW_FRAMEBUFFER_EXT, mInternalDrawFB);
|
2013-02-13 15:26:24 -08:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
mInInternalMode_DrawFB = false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GLScreenBuffer::BindReadFB(GLuint fb)
|
|
|
|
{
|
2014-08-07 19:03:25 -07:00
|
|
|
MOZ_ASSERT(mGL->IsSupported(GLFeature::framebuffer_blit));
|
2013-02-13 15:26:24 -08:00
|
|
|
|
2014-08-07 19:03:25 -07:00
|
|
|
GLuint readFB = ReadFB();
|
|
|
|
mUserReadFB = fb;
|
|
|
|
mInternalReadFB = (fb == 0) ? readFB : fb;
|
2013-02-13 15:26:24 -08:00
|
|
|
|
2014-08-07 19:03:25 -07:00
|
|
|
mGL->raw_fBindFramebuffer(LOCAL_GL_READ_FRAMEBUFFER_EXT, mInternalReadFB);
|
2013-02-13 15:26:24 -08:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
mInInternalMode_ReadFB = false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-08-07 19:03:25 -07: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-13 15:26:24 -08:00
|
|
|
void
|
|
|
|
GLScreenBuffer::BindDrawFB_Internal(GLuint fb)
|
|
|
|
{
|
2014-08-07 19:03:25 -07:00
|
|
|
MOZ_ASSERT(mGL->IsSupported(GLFeature::framebuffer_blit));
|
|
|
|
|
|
|
|
mInternalDrawFB = mUserDrawFB = fb;
|
|
|
|
mGL->raw_fBindFramebuffer(LOCAL_GL_DRAW_FRAMEBUFFER_EXT, mInternalDrawFB);
|
2013-02-13 15:26:24 -08:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
mInInternalMode_DrawFB = true;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GLScreenBuffer::BindReadFB_Internal(GLuint fb)
|
|
|
|
{
|
2014-08-07 19:03:25 -07:00
|
|
|
MOZ_ASSERT(mGL->IsSupported(GLFeature::framebuffer_blit));
|
|
|
|
|
|
|
|
mInternalReadFB = mUserReadFB = fb;
|
|
|
|
mGL->raw_fBindFramebuffer(LOCAL_GL_READ_FRAMEBUFFER_EXT, mInternalReadFB);
|
2013-02-13 15:26:24 -08:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
mInInternalMode_ReadFB = true;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
GLuint
|
|
|
|
GLScreenBuffer::GetDrawFB() const
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
MOZ_ASSERT(mGL->IsCurrent());
|
|
|
|
MOZ_ASSERT(!mInInternalMode_DrawFB);
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
GLuint predicted = mInternalDrawFB;
|
|
|
|
if (predicted != actual) {
|
|
|
|
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(mGL->IsCurrent());
|
|
|
|
MOZ_ASSERT(!mInInternalMode_ReadFB);
|
|
|
|
|
|
|
|
// We use raw_ here because this is debug code and we need to see what
|
|
|
|
// the driver thinks.
|
|
|
|
GLuint actual = 0;
|
2013-08-22 10:42:05 -07:00
|
|
|
if (mGL->IsSupported(GLFeature::framebuffer_blit))
|
2013-02-13 15:26:24 -08:00
|
|
|
mGL->raw_fGetIntegerv(LOCAL_GL_READ_FRAMEBUFFER_BINDING_EXT, (GLint*)&actual);
|
|
|
|
else
|
|
|
|
mGL->raw_fGetIntegerv(LOCAL_GL_FRAMEBUFFER_BINDING, (GLint*)&actual);
|
|
|
|
|
|
|
|
GLuint predicted = mInternalReadFB;
|
|
|
|
if (predicted != actual) {
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GLScreenBuffer::BeforeReadCall()
|
|
|
|
{
|
|
|
|
if (mUserReadFB != 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
AssureBlitted();
|
|
|
|
}
|
|
|
|
|
2013-07-17 20:24:15 -07:00
|
|
|
bool
|
2014-07-11 15:10:49 -07:00
|
|
|
GLScreenBuffer::ReadPixels(GLint x, GLint y,
|
|
|
|
GLsizei width, GLsizei height,
|
|
|
|
GLenum format, GLenum type,
|
|
|
|
GLvoid* pixels)
|
2013-07-17 20:24:15 -07:00
|
|
|
{
|
2014-07-11 15:10:49 -07:00
|
|
|
// If the currently bound framebuffer is backed by a SharedSurface
|
2013-07-17 20:24:15 -07:00
|
|
|
// then it might want to override how we read pixel data from it.
|
|
|
|
// This is normally only the default framebuffer, but we can also
|
|
|
|
// have SharedSurfaces bound to other framebuffers when doing
|
|
|
|
// readback for BasicLayers.
|
2014-07-11 15:10:49 -07:00
|
|
|
SharedSurface* surf;
|
2013-07-17 20:24:15 -07:00
|
|
|
if (GetReadFB() == 0) {
|
|
|
|
surf = SharedSurf();
|
|
|
|
} else {
|
|
|
|
surf = mGL->mFBOMapping[GetReadFB()];
|
|
|
|
}
|
|
|
|
if (surf) {
|
|
|
|
return surf->ReadPixels(x, y, width, height, format, type, pixels);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-02-13 15:26:24 -08:00
|
|
|
void
|
|
|
|
GLScreenBuffer::RequireBlit()
|
|
|
|
{
|
|
|
|
mNeedsBlit = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GLScreenBuffer::AssureBlitted()
|
|
|
|
{
|
|
|
|
if (!mNeedsBlit)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (mDraw) {
|
|
|
|
GLuint drawFB = DrawFB();
|
|
|
|
GLuint readFB = ReadFB();
|
|
|
|
|
|
|
|
MOZ_ASSERT(drawFB != 0);
|
|
|
|
MOZ_ASSERT(drawFB != readFB);
|
2013-10-31 06:51:47 -07:00
|
|
|
MOZ_ASSERT(mGL->IsSupported(GLFeature::framebuffer_blit));
|
2014-07-11 15:10:49 -07:00
|
|
|
MOZ_ASSERT(mDraw->mSize == mRead->Size());
|
2013-02-13 15:26:24 -08:00
|
|
|
|
|
|
|
ScopedBindFramebuffer boundFB(mGL);
|
|
|
|
ScopedGLState scissor(mGL, LOCAL_GL_SCISSOR_TEST, false);
|
|
|
|
|
|
|
|
BindReadFB_Internal(drawFB);
|
|
|
|
BindDrawFB_Internal(readFB);
|
|
|
|
|
2014-07-11 15:10:49 -07:00
|
|
|
const gfx::IntSize& srcSize = mDraw->mSize;
|
2013-12-10 08:11:58 -08:00
|
|
|
const gfx::IntSize& destSize = mRead->Size();
|
2013-02-13 15:26:24 -08:00
|
|
|
|
|
|
|
mGL->raw_fBlitFramebuffer(0, 0, srcSize.width, srcSize.height,
|
|
|
|
0, 0, destSize.width, destSize.height,
|
|
|
|
LOCAL_GL_COLOR_BUFFER_BIT,
|
|
|
|
LOCAL_GL_NEAREST);
|
|
|
|
// Done!
|
|
|
|
}
|
|
|
|
|
|
|
|
mNeedsBlit = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-10-07 21:01:07 -07:00
|
|
|
GLScreenBuffer::Morph(UniquePtr<SurfaceFactory> newFactory)
|
2013-02-13 15:26:24 -08:00
|
|
|
{
|
2014-10-07 21:01:07 -07:00
|
|
|
MOZ_ASSERT(newFactory);
|
|
|
|
mFactory = Move(newFactory);
|
2013-02-13 15:26:24 -08:00
|
|
|
}
|
|
|
|
|
2014-04-28 18:11:14 -07:00
|
|
|
bool
|
2014-07-11 15:10:49 -07:00
|
|
|
GLScreenBuffer::Attach(SharedSurface* surf, const gfx::IntSize& size)
|
2013-02-13 15:26:24 -08:00
|
|
|
{
|
|
|
|
ScopedBindFramebuffer autoFB(mGL);
|
|
|
|
|
|
|
|
if (mRead && SharedSurf())
|
|
|
|
SharedSurf()->UnlockProd();
|
|
|
|
|
|
|
|
surf->LockProd();
|
|
|
|
|
|
|
|
if (mRead &&
|
2014-07-11 15:10:49 -07:00
|
|
|
surf->mAttachType == SharedSurf()->mAttachType &&
|
2013-12-10 08:12:18 -08:00
|
|
|
size == Size())
|
2013-02-13 15:26:24 -08:00
|
|
|
{
|
|
|
|
// Same size, same type, ready for reuse!
|
|
|
|
mRead->Attach(surf);
|
|
|
|
} else {
|
|
|
|
// Else something changed, so resize:
|
2014-08-15 17:38:07 -07:00
|
|
|
UniquePtr<DrawBuffer> draw;
|
2014-04-28 18:11:14 -07:00
|
|
|
bool drawOk = CreateDraw(size, &draw); // Can be null.
|
|
|
|
|
2014-08-15 17:38:07 -07:00
|
|
|
UniquePtr<ReadBuffer> read = CreateRead(surf);
|
2014-04-28 18:11:14 -07:00
|
|
|
bool readOk = !!read;
|
|
|
|
|
|
|
|
if (!drawOk || !readOk) {
|
2014-05-13 17:14:33 -07:00
|
|
|
surf->UnlockProd();
|
|
|
|
|
2014-04-28 18:11:14 -07:00
|
|
|
return false;
|
|
|
|
}
|
2013-02-13 15:26:24 -08:00
|
|
|
|
2014-08-15 17:38:07 -07:00
|
|
|
mDraw = Move(draw);
|
|
|
|
mRead = Move(read);
|
2013-02-13 15:26:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check that we're all set up.
|
|
|
|
MOZ_ASSERT(SharedSurf() == surf);
|
|
|
|
|
2014-04-28 18:11:14 -07:00
|
|
|
return true;
|
2013-03-22 12:22:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-12-10 08:12:18 -08:00
|
|
|
GLScreenBuffer::Swap(const gfx::IntSize& size)
|
2013-03-22 12:22:20 -07:00
|
|
|
{
|
2014-10-07 21:01:07 -07:00
|
|
|
RefPtr<ShSurfHandle> newBack = mFactory->NewShSurfHandle(size);
|
|
|
|
if (!newBack)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!Attach(newBack->Surf(), size))
|
|
|
|
return false;
|
|
|
|
// Attach was successful.
|
|
|
|
|
|
|
|
mFront = mBack;
|
|
|
|
mBack = newBack;
|
|
|
|
|
|
|
|
// Fence before copying.
|
|
|
|
if (mFront) {
|
|
|
|
mFront->Surf()->Fence();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ShouldPreserveBuffer() &&
|
|
|
|
mFront &&
|
|
|
|
mBack)
|
|
|
|
{
|
|
|
|
auto src = mFront->Surf();
|
|
|
|
auto dest = mBack->Surf();
|
|
|
|
SharedSurface::ProdCopy(src, dest, mFactory.get());
|
2013-03-22 12:22:20 -07:00
|
|
|
}
|
|
|
|
|
2014-10-07 21:01:07 -07:00
|
|
|
return true;
|
2013-02-13 15:26:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-12-10 08:12:18 -08:00
|
|
|
GLScreenBuffer::PublishFrame(const gfx::IntSize& size)
|
2013-02-13 15:26:24 -08:00
|
|
|
{
|
|
|
|
AssureBlitted();
|
|
|
|
|
|
|
|
bool good = Swap(size);
|
|
|
|
return good;
|
|
|
|
}
|
|
|
|
|
2013-03-22 12:22:20 -07:00
|
|
|
bool
|
2013-12-10 08:12:18 -08:00
|
|
|
GLScreenBuffer::Resize(const gfx::IntSize& size)
|
2013-03-22 12:22:20 -07:00
|
|
|
{
|
2014-10-07 21:01:07 -07:00
|
|
|
RefPtr<ShSurfHandle> newBack = mFactory->NewShSurfHandle(size);
|
|
|
|
if (!newBack)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!Attach(newBack->Surf(), size))
|
2013-03-22 12:22:20 -07:00
|
|
|
return false;
|
|
|
|
|
2014-10-07 21:01:07 -07:00
|
|
|
mBack = newBack;
|
|
|
|
return true;
|
2013-03-22 12:22:20 -07:00
|
|
|
}
|
2013-02-13 15:26:24 -08:00
|
|
|
|
2014-04-28 18:11:14 -07:00
|
|
|
bool
|
2014-08-15 17:38:07 -07:00
|
|
|
GLScreenBuffer::CreateDraw(const gfx::IntSize& size,
|
|
|
|
UniquePtr<DrawBuffer>* out_buffer)
|
2013-02-13 15:26:24 -08:00
|
|
|
{
|
2014-07-11 15:10:49 -07:00
|
|
|
GLContext* gl = mFactory->mGL;
|
|
|
|
const GLFormats& formats = mFactory->mFormats;
|
2013-02-13 15:26:24 -08:00
|
|
|
const SurfaceCaps& caps = mFactory->DrawCaps();
|
|
|
|
|
2014-04-28 18:11:14 -07:00
|
|
|
return DrawBuffer::Create(gl, caps, formats, size, out_buffer);
|
2013-02-13 15:26:24 -08:00
|
|
|
}
|
|
|
|
|
2014-08-15 17:38:07 -07:00
|
|
|
UniquePtr<ReadBuffer>
|
2014-07-11 15:10:49 -07:00
|
|
|
GLScreenBuffer::CreateRead(SharedSurface* surf)
|
2013-02-13 15:26:24 -08:00
|
|
|
{
|
2014-07-11 15:10:49 -07:00
|
|
|
GLContext* gl = mFactory->mGL;
|
|
|
|
const GLFormats& formats = mFactory->mFormats;
|
2013-02-13 15:26:24 -08:00
|
|
|
const SurfaceCaps& caps = mFactory->ReadCaps();
|
|
|
|
|
|
|
|
return ReadBuffer::Create(gl, caps, formats, surf);
|
|
|
|
}
|
|
|
|
|
2014-02-12 07:07:46 -08:00
|
|
|
void
|
2014-07-11 15:10:49 -07:00
|
|
|
GLScreenBuffer::Readback(SharedSurface* src, gfx::DataSourceSurface* dest)
|
2014-02-12 07:07:46 -08:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(src && dest);
|
2014-07-11 15:10:49 -07:00
|
|
|
MOZ_ASSERT(dest->GetSize() == src->mSize);
|
|
|
|
MOZ_ASSERT(dest->GetFormat() == (src->mHasAlpha ? SurfaceFormat::B8G8R8A8
|
|
|
|
: SurfaceFormat::B8G8R8X8));
|
2013-02-13 15:26:24 -08:00
|
|
|
|
2014-04-28 16:26:54 -07:00
|
|
|
mGL->MakeCurrent();
|
2013-02-13 15:26:24 -08:00
|
|
|
|
2014-04-28 16:26:54 -07:00
|
|
|
bool needsSwap = src != SharedSurf();
|
|
|
|
if (needsSwap) {
|
|
|
|
SharedSurf()->UnlockProd();
|
|
|
|
src->LockProd();
|
|
|
|
}
|
2013-02-13 15:26:24 -08:00
|
|
|
|
2014-08-15 17:38:07 -07:00
|
|
|
{
|
|
|
|
UniquePtr<ReadBuffer> buffer = CreateRead(src);
|
|
|
|
MOZ_ASSERT(buffer);
|
2013-02-13 15:26:24 -08:00
|
|
|
|
2014-08-15 17:38:07 -07:00
|
|
|
ScopedBindFramebuffer autoFB(mGL, buffer->mFB);
|
|
|
|
ReadPixelsIntoDataSurface(mGL, dest);
|
|
|
|
}
|
2013-02-13 15:26:24 -08:00
|
|
|
|
2014-04-28 16:26:54 -07:00
|
|
|
if (needsSwap) {
|
|
|
|
src->UnlockProd();
|
|
|
|
SharedSurf()->LockProd();
|
|
|
|
}
|
2013-02-13 15:26:24 -08:00
|
|
|
}
|
|
|
|
|
2014-07-11 15:10:49 -07:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// DrawBuffer
|
|
|
|
|
2014-04-28 18:11:14 -07:00
|
|
|
bool
|
2013-02-13 15:26:24 -08:00
|
|
|
DrawBuffer::Create(GLContext* const gl,
|
|
|
|
const SurfaceCaps& caps,
|
|
|
|
const GLFormats& formats,
|
2014-04-28 18:11:14 -07:00
|
|
|
const gfx::IntSize& size,
|
2014-08-15 17:38:07 -07:00
|
|
|
UniquePtr<DrawBuffer>* out_buffer)
|
2013-02-13 15:26:24 -08:00
|
|
|
{
|
2014-04-28 18:11:14 -07:00
|
|
|
MOZ_ASSERT(out_buffer);
|
|
|
|
*out_buffer = nullptr;
|
|
|
|
|
2013-02-13 15:26:24 -08:00
|
|
|
if (!caps.color) {
|
|
|
|
MOZ_ASSERT(!caps.alpha && !caps.depth && !caps.stencil);
|
|
|
|
|
|
|
|
// Nothing is needed.
|
2014-04-28 18:11:14 -07:00
|
|
|
return true;
|
2013-02-13 15:26:24 -08:00
|
|
|
}
|
|
|
|
|
2014-08-21 14:59:25 -07:00
|
|
|
if (caps.antialias && formats.samples == 0)
|
|
|
|
return false; // Can't create it
|
|
|
|
|
2013-02-13 15:26:24 -08: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-08-27 16:18:43 -07:00
|
|
|
GLContext::ScopedLocalErrorCheck localError(gl);
|
|
|
|
|
2013-12-06 07:36:13 -08:00
|
|
|
CreateRenderbuffersForOffscreen(gl, formats, size, caps.antialias,
|
|
|
|
pColorMSRB, pDepthRB, pStencilRB);
|
2013-02-13 15:26:24 -08:00
|
|
|
|
|
|
|
GLuint fb = 0;
|
|
|
|
gl->fGenFramebuffers(1, &fb);
|
|
|
|
gl->AttachBuffersToFB(0, colorMSRB, depthRB, stencilRB, fb);
|
|
|
|
|
2014-08-15 17:38:07 -07:00
|
|
|
UniquePtr<DrawBuffer> ret( new DrawBuffer(gl, size, fb, colorMSRB,
|
|
|
|
depthRB, stencilRB) );
|
2014-04-28 18:11:14 -07:00
|
|
|
|
2014-08-27 16:18:43 -07:00
|
|
|
GLenum err = localError.GetLocalError();
|
|
|
|
if (err || !gl->IsFramebufferComplete(fb))
|
2014-04-28 18:11:14 -07:00
|
|
|
return false;
|
|
|
|
|
2014-08-15 17:38:07 -07:00
|
|
|
*out_buffer = Move(ret);
|
2014-04-28 18:11:14 -07:00
|
|
|
return true;
|
2013-02-13 15:26:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
DrawBuffer::~DrawBuffer()
|
|
|
|
{
|
|
|
|
mGL->MakeCurrent();
|
|
|
|
|
|
|
|
GLuint fb = mFB;
|
|
|
|
GLuint rbs[] = {
|
|
|
|
mColorMSRB,
|
|
|
|
mDepthRB,
|
|
|
|
mStencilRB
|
|
|
|
};
|
|
|
|
|
|
|
|
mGL->fDeleteFramebuffers(1, &fb);
|
|
|
|
mGL->fDeleteRenderbuffers(3, rbs);
|
|
|
|
}
|
|
|
|
|
2014-07-11 15:10:49 -07:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// ReadBuffer
|
2013-02-13 15:26:24 -08:00
|
|
|
|
2014-08-15 17:38:07 -07:00
|
|
|
UniquePtr<ReadBuffer>
|
2013-02-13 15:26:24 -08:00
|
|
|
ReadBuffer::Create(GLContext* gl,
|
|
|
|
const SurfaceCaps& caps,
|
|
|
|
const GLFormats& formats,
|
2014-07-11 15:10:49 -07:00
|
|
|
SharedSurface* surf)
|
2013-02-13 15:26:24 -08:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(surf);
|
|
|
|
|
2014-07-11 15:10:49 -07:00
|
|
|
if (surf->mAttachType == AttachmentType::Screen) {
|
2013-02-13 15:26:24 -08:00
|
|
|
// Don't need anything. Our read buffer will be the 'screen'.
|
|
|
|
|
2014-08-15 17:38:07 -07:00
|
|
|
return UniquePtr<ReadBuffer>( new ReadBuffer(gl, 0, 0, 0,
|
|
|
|
surf) );
|
2013-02-13 15:26:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
GLuint depthRB = 0;
|
|
|
|
GLuint stencilRB = 0;
|
|
|
|
|
|
|
|
GLuint* pDepthRB = caps.depth ? &depthRB : nullptr;
|
|
|
|
GLuint* pStencilRB = caps.stencil ? &stencilRB : nullptr;
|
|
|
|
|
2014-08-27 16:18:43 -07:00
|
|
|
GLContext::ScopedLocalErrorCheck localError(gl);
|
|
|
|
|
2014-07-11 15:10:49 -07:00
|
|
|
CreateRenderbuffersForOffscreen(gl, formats, surf->mSize, caps.antialias,
|
2013-12-06 07:36:13 -08:00
|
|
|
nullptr, pDepthRB, pStencilRB);
|
2013-02-13 15:26:24 -08:00
|
|
|
|
|
|
|
GLuint colorTex = 0;
|
|
|
|
GLuint colorRB = 0;
|
2013-07-17 20:24:15 -07:00
|
|
|
GLenum target = 0;
|
2013-02-13 15:26:24 -08:00
|
|
|
|
2014-07-11 15:10:49 -07:00
|
|
|
switch (surf->mAttachType) {
|
2013-02-13 15:26:24 -08:00
|
|
|
case AttachmentType::GLTexture:
|
2014-03-10 19:08:49 -07:00
|
|
|
colorTex = surf->ProdTexture();
|
|
|
|
target = surf->ProdTextureTarget();
|
2013-02-13 15:26:24 -08:00
|
|
|
break;
|
|
|
|
case AttachmentType::GLRenderbuffer:
|
2014-03-10 19:08:49 -07:00
|
|
|
colorRB = surf->ProdRenderbuffer();
|
2013-02-13 15:26:24 -08:00
|
|
|
break;
|
|
|
|
default:
|
2013-06-28 18:38:30 -07:00
|
|
|
MOZ_CRASH("Unknown attachment type?");
|
2013-02-13 15:26:24 -08:00
|
|
|
}
|
|
|
|
MOZ_ASSERT(colorTex || colorRB);
|
|
|
|
|
|
|
|
GLuint fb = 0;
|
|
|
|
gl->fGenFramebuffers(1, &fb);
|
2013-07-17 20:24:15 -07:00
|
|
|
gl->AttachBuffersToFB(colorTex, colorRB, depthRB, stencilRB, fb, target);
|
2013-07-17 20:24:15 -07:00
|
|
|
gl->mFBOMapping[fb] = surf;
|
2013-02-13 15:26:24 -08:00
|
|
|
|
2014-08-15 17:38:07 -07:00
|
|
|
UniquePtr<ReadBuffer> ret( new ReadBuffer(gl, fb, depthRB,
|
|
|
|
stencilRB, surf) );
|
2014-08-27 16:18:43 -07:00
|
|
|
|
|
|
|
GLenum err = localError.GetLocalError();
|
|
|
|
if (err || !gl->IsFramebufferComplete(fb)) {
|
2014-08-15 17:38:09 -07:00
|
|
|
ret = nullptr;
|
2014-08-15 17:38:08 -07:00
|
|
|
}
|
2014-04-28 18:11:14 -07:00
|
|
|
|
2014-08-15 17:38:07 -07:00
|
|
|
return Move(ret);
|
2013-02-13 15:26:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
ReadBuffer::~ReadBuffer()
|
|
|
|
{
|
|
|
|
mGL->MakeCurrent();
|
|
|
|
|
|
|
|
GLuint fb = mFB;
|
|
|
|
GLuint rbs[] = {
|
|
|
|
mDepthRB,
|
|
|
|
mStencilRB
|
|
|
|
};
|
|
|
|
|
|
|
|
mGL->fDeleteFramebuffers(1, &fb);
|
|
|
|
mGL->fDeleteRenderbuffers(2, rbs);
|
2013-07-17 20:24:15 -07:00
|
|
|
mGL->mFBOMapping.erase(mFB);
|
2013-02-13 15:26:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-07-11 15:10:49 -07:00
|
|
|
ReadBuffer::Attach(SharedSurface* surf)
|
2013-02-13 15:26:24 -08:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(surf && mSurf);
|
2014-07-11 15:10:49 -07:00
|
|
|
MOZ_ASSERT(surf->mAttachType == mSurf->mAttachType);
|
|
|
|
MOZ_ASSERT(surf->mSize == mSurf->mSize);
|
2013-02-13 15:26:24 -08:00
|
|
|
|
|
|
|
// Nothing else is needed for AttachType Screen.
|
2014-07-11 15:10:49 -07:00
|
|
|
if (surf->mAttachType != AttachmentType::Screen) {
|
2013-02-13 15:26:24 -08:00
|
|
|
GLuint colorTex = 0;
|
|
|
|
GLuint colorRB = 0;
|
2013-07-17 20:24:15 -07:00
|
|
|
GLenum target = 0;
|
2013-02-13 15:26:24 -08:00
|
|
|
|
2014-07-11 15:10:49 -07:00
|
|
|
switch (surf->mAttachType) {
|
2013-02-13 15:26:24 -08:00
|
|
|
case AttachmentType::GLTexture:
|
2014-03-10 19:08:49 -07:00
|
|
|
colorTex = surf->ProdTexture();
|
|
|
|
target = surf->ProdTextureTarget();
|
2013-02-13 15:26:24 -08:00
|
|
|
break;
|
|
|
|
case AttachmentType::GLRenderbuffer:
|
2014-03-10 19:08:49 -07:00
|
|
|
colorRB = surf->ProdRenderbuffer();
|
2013-02-13 15:26:24 -08:00
|
|
|
break;
|
|
|
|
default:
|
2013-06-28 18:38:30 -07:00
|
|
|
MOZ_CRASH("Unknown attachment type?");
|
2013-02-13 15:26:24 -08:00
|
|
|
}
|
|
|
|
|
2013-07-17 20:24:15 -07:00
|
|
|
mGL->AttachBuffersToFB(colorTex, colorRB, 0, 0, mFB, target);
|
2013-07-17 20:24:15 -07:00
|
|
|
mGL->mFBOMapping[mFB] = surf;
|
2013-02-13 15:26:24 -08:00
|
|
|
MOZ_ASSERT(mGL->IsFramebufferComplete(mFB));
|
|
|
|
}
|
|
|
|
|
|
|
|
mSurf = surf;
|
|
|
|
}
|
|
|
|
|
2013-12-10 08:11:58 -08:00
|
|
|
const gfx::IntSize&
|
2013-02-13 15:26:24 -08:00
|
|
|
ReadBuffer::Size() const
|
|
|
|
{
|
2014-07-11 15:10:49 -07:00
|
|
|
return mSurf->mSize;
|
2013-02-13 15:26:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
} /* namespace gl */
|
|
|
|
} /* namespace mozilla */
|