2013-02-14 03:26:24 +04: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>
|
|
|
|
#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"
|
2013-02-14 03:26:24 +04:00
|
|
|
#include "SharedSurfaceGL.h"
|
|
|
|
#include "SurfaceStream.h"
|
2013-05-27 18:12:13 +04:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
#include "SharedSurfaceGralloc.h"
|
|
|
|
#include "nsXULAppAPI.h"
|
|
|
|
#endif
|
2013-07-18 07:24:15 +04:00
|
|
|
#ifdef XP_MACOSX
|
|
|
|
#include "SharedSurfaceIO.h"
|
|
|
|
#endif
|
2013-12-01 02:20:57 +04:00
|
|
|
#include "ScopedGLHelpers.h"
|
2014-02-12 19:07:46 +04:00
|
|
|
#include "gfx2DGlue.h"
|
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
|
|
|
GLScreenBuffer*
|
|
|
|
GLScreenBuffer::Create(GLContext* gl,
|
2014-07-12 02:10:49 +04:00
|
|
|
const gfx::IntSize& size,
|
|
|
|
const SurfaceCaps& caps)
|
2013-02-14 03:26:24 +04:00
|
|
|
{
|
|
|
|
if (caps.antialias &&
|
2013-08-22 21:42:05 +04:00
|
|
|
!gl->IsSupported(GLFeature::framebuffer_multisample))
|
2013-02-14 03:26:24 +04:00
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-07-12 02:10:49 +04:00
|
|
|
SurfaceFactory* factory = nullptr;
|
2013-05-27 18:12:13 +04:00
|
|
|
|
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
/* On B2G, we want a Gralloc factory, and we want one right at the start */
|
|
|
|
if (!factory &&
|
2014-03-06 01:49:37 +04:00
|
|
|
caps.surfaceAllocator &&
|
2013-05-27 18:12:13 +04:00
|
|
|
XRE_GetProcessType() != GeckoProcessType_Default)
|
|
|
|
{
|
|
|
|
factory = new SurfaceFactory_Gralloc(gl, caps);
|
|
|
|
}
|
|
|
|
#endif
|
2013-07-18 07:24:15 +04:00
|
|
|
#ifdef XP_MACOSX
|
|
|
|
/* On OSX, we want an IOSurface factory, and we want one right at the start */
|
2014-07-12 02:10:49 +04:00
|
|
|
if (!factory) {
|
2014-07-12 10:08:54 +04:00
|
|
|
factory = SurfaceFactory_IOSurface::Create(gl, caps);
|
2013-07-18 07:24:15 +04:00
|
|
|
}
|
|
|
|
#endif
|
2013-05-27 18:12:13 +04:00
|
|
|
|
|
|
|
if (!factory)
|
|
|
|
factory = new SurfaceFactory_Basic(gl, caps);
|
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
SurfaceStream* stream = SurfaceStream::CreateForType(
|
|
|
|
SurfaceStream::ChooseGLStreamType(SurfaceStream::MainThread,
|
|
|
|
caps.preserve),
|
2013-07-17 21:03:18 +04:00
|
|
|
gl,
|
2013-02-14 03:26:24 +04:00
|
|
|
nullptr);
|
|
|
|
|
|
|
|
return new GLScreenBuffer(gl, caps, factory, stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
GLScreenBuffer::~GLScreenBuffer()
|
|
|
|
{
|
|
|
|
delete mDraw;
|
|
|
|
delete mRead;
|
2013-09-26 04:18:30 +04: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!
|
|
|
|
delete mFactory;
|
2013-02-14 03:26:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
GLScreenBuffer::BindAsFramebuffer(GLContext* const gl, GLenum target) const
|
|
|
|
{
|
|
|
|
GLuint drawFB = DrawFB();
|
|
|
|
GLuint readFB = ReadFB();
|
|
|
|
|
2013-08-22 21:42:05 +04:00
|
|
|
if (!gl->IsSupported(GLFeature::framebuffer_blit)) {
|
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:
|
2013-08-22 21:42:05 +04:00
|
|
|
if (!gl->IsSupported(GLFeature::framebuffer_blit))
|
2013-02-14 03:26:24 +04: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 21:42:05 +04:00
|
|
|
if (!gl->IsSupported(GLFeature::framebuffer_blit))
|
2013-02-14 03:26:24 +04:00
|
|
|
NS_WARNING("READ_FRAMEBUFFER requested but unavailable.");
|
|
|
|
|
|
|
|
gl->raw_fBindFramebuffer(LOCAL_GL_READ_FRAMEBUFFER_EXT, readFB);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2013-06-29 05:38:30 +04:00
|
|
|
MOZ_CRASH("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 {
|
2013-08-22 21:42:05 +04:00
|
|
|
MOZ_ASSERT(mGL->IsSupported(GLFeature::framebuffer_blit));
|
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)
|
|
|
|
{
|
2013-08-22 21:42:05 +04:00
|
|
|
if (!mGL->IsSupported(GLFeature::framebuffer_blit)) {
|
2013-02-14 03:26:24 +04:00
|
|
|
NS_WARNING("DRAW_FRAMEBUFFER requested, but unsupported.");
|
|
|
|
|
|
|
|
mGL->raw_fBindFramebuffer(LOCAL_GL_DRAW_FRAMEBUFFER_EXT, fb);
|
|
|
|
} else {
|
|
|
|
GLuint drawFB = DrawFB();
|
|
|
|
mUserDrawFB = fb;
|
|
|
|
mInternalDrawFB = (fb == 0) ? drawFB : fb;
|
|
|
|
|
|
|
|
mGL->raw_fBindFramebuffer(LOCAL_GL_DRAW_FRAMEBUFFER_EXT, mInternalDrawFB);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
mInInternalMode_DrawFB = false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GLScreenBuffer::BindReadFB(GLuint fb)
|
|
|
|
{
|
2013-08-22 21:42:05 +04:00
|
|
|
if (!mGL->IsSupported(GLFeature::framebuffer_blit)) {
|
2013-02-14 03:26:24 +04:00
|
|
|
NS_WARNING("READ_FRAMEBUFFER requested, but unsupported.");
|
|
|
|
|
|
|
|
mGL->raw_fBindFramebuffer(LOCAL_GL_READ_FRAMEBUFFER_EXT, fb);
|
|
|
|
} else {
|
|
|
|
GLuint readFB = ReadFB();
|
|
|
|
mUserReadFB = fb;
|
|
|
|
mInternalReadFB = (fb == 0) ? readFB : fb;
|
|
|
|
|
|
|
|
mGL->raw_fBindFramebuffer(LOCAL_GL_READ_FRAMEBUFFER_EXT, mInternalReadFB);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
mInInternalMode_ReadFB = false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GLScreenBuffer::BindDrawFB_Internal(GLuint fb)
|
|
|
|
{
|
|
|
|
mInternalDrawFB = mUserDrawFB = fb;
|
|
|
|
mGL->raw_fBindFramebuffer(LOCAL_GL_DRAW_FRAMEBUFFER_EXT, mInternalDrawFB);
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
mInInternalMode_DrawFB = true;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GLScreenBuffer::BindReadFB_Internal(GLuint fb)
|
|
|
|
{
|
|
|
|
mInternalReadFB = mUserReadFB = fb;
|
|
|
|
mGL->raw_fBindFramebuffer(LOCAL_GL_READ_FRAMEBUFFER_EXT, mInternalReadFB);
|
|
|
|
|
|
|
|
#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 21:42:05 +04:00
|
|
|
if (mGL->IsSupported(GLFeature::framebuffer_blit))
|
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);
|
|
|
|
|
|
|
|
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-18 07:24:15 +04:00
|
|
|
bool
|
2014-07-12 02:10:49 +04:00
|
|
|
GLScreenBuffer::ReadPixels(GLint x, GLint y,
|
|
|
|
GLsizei width, GLsizei height,
|
|
|
|
GLenum format, GLenum type,
|
|
|
|
GLvoid* pixels)
|
2013-07-18 07:24:15 +04:00
|
|
|
{
|
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.
|
|
|
|
// This is normally only the default framebuffer, but we can also
|
|
|
|
// have SharedSurfaces bound to other framebuffers when doing
|
|
|
|
// readback for BasicLayers.
|
2014-07-12 02:10:49 +04:00
|
|
|
SharedSurface* surf;
|
2013-07-18 07:24:15 +04: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-14 03:26:24 +04: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 17:51:47 +04:00
|
|
|
MOZ_ASSERT(mGL->IsSupported(GLFeature::framebuffer_blit));
|
2014-07-12 02:10:49 +04:00
|
|
|
MOZ_ASSERT(mDraw->mSize == mRead->Size());
|
2013-02-14 03:26:24 +04:00
|
|
|
|
|
|
|
ScopedBindFramebuffer boundFB(mGL);
|
|
|
|
ScopedGLState scissor(mGL, LOCAL_GL_SCISSOR_TEST, false);
|
|
|
|
|
|
|
|
BindReadFB_Internal(drawFB);
|
|
|
|
BindDrawFB_Internal(readFB);
|
|
|
|
|
2014-07-12 02:10:49 +04:00
|
|
|
const gfx::IntSize& srcSize = mDraw->mSize;
|
2013-12-10 20:11:58 +04:00
|
|
|
const gfx::IntSize& destSize = mRead->Size();
|
2013-02-14 03:26:24 +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);
|
|
|
|
// Done!
|
|
|
|
}
|
|
|
|
|
|
|
|
mNeedsBlit = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-07-12 02:10:49 +04:00
|
|
|
GLScreenBuffer::Morph(SurfaceFactory* newFactory, SurfaceStreamType streamType)
|
2013-02-14 03:26:24 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mStream);
|
|
|
|
|
|
|
|
if (newFactory) {
|
|
|
|
delete mFactory;
|
|
|
|
mFactory = newFactory;
|
|
|
|
}
|
|
|
|
|
2013-04-07 17:35:21 +04:00
|
|
|
if (mStream->mType == streamType)
|
2013-02-14 03:26:24 +04:00
|
|
|
return;
|
|
|
|
|
2013-07-17 21:03:18 +04:00
|
|
|
SurfaceStream* newStream = SurfaceStream::CreateForType(streamType, mGL, mStream);
|
2013-02-14 03:26:24 +04:00
|
|
|
MOZ_ASSERT(newStream);
|
|
|
|
|
|
|
|
mStream = newStream;
|
|
|
|
}
|
|
|
|
|
2014-04-29 05:11:14 +04:00
|
|
|
bool
|
2014-07-12 02:10:49 +04:00
|
|
|
GLScreenBuffer::Attach(SharedSurface* surf, const gfx::IntSize& size)
|
2013-02-14 03:26:24 +04:00
|
|
|
{
|
|
|
|
ScopedBindFramebuffer autoFB(mGL);
|
|
|
|
|
|
|
|
if (mRead && SharedSurf())
|
|
|
|
SharedSurf()->UnlockProd();
|
|
|
|
|
|
|
|
surf->LockProd();
|
|
|
|
|
|
|
|
if (mRead &&
|
2014-07-12 02:10:49 +04:00
|
|
|
surf->mAttachType == SharedSurf()->mAttachType &&
|
2013-12-10 20:12:18 +04:00
|
|
|
size == Size())
|
2013-02-14 03:26:24 +04:00
|
|
|
{
|
|
|
|
// Same size, same type, ready for reuse!
|
|
|
|
mRead->Attach(surf);
|
|
|
|
} else {
|
|
|
|
// Else something changed, so resize:
|
2014-04-29 05:11:14 +04:00
|
|
|
DrawBuffer* draw = nullptr;
|
|
|
|
bool drawOk = CreateDraw(size, &draw); // Can be null.
|
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
ReadBuffer* read = CreateRead(surf);
|
2014-04-29 05:11:14 +04:00
|
|
|
bool readOk = !!read;
|
|
|
|
|
|
|
|
if (!drawOk || !readOk) {
|
|
|
|
delete draw;
|
|
|
|
delete read;
|
2014-05-14 04:14:33 +04:00
|
|
|
|
|
|
|
surf->UnlockProd();
|
|
|
|
|
2014-04-29 05:11:14 +04:00
|
|
|
return false;
|
|
|
|
}
|
2013-02-14 03:26:24 +04:00
|
|
|
|
|
|
|
delete mDraw;
|
|
|
|
delete mRead;
|
|
|
|
|
|
|
|
mDraw = draw;
|
|
|
|
mRead = read;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that we're all set up.
|
|
|
|
MOZ_ASSERT(SharedSurf() == surf);
|
|
|
|
|
|
|
|
if (!PreserveBuffer()) {
|
|
|
|
// DiscardFramebuffer here could help perf on some mobile platforms.
|
|
|
|
}
|
2014-04-29 05:11:14 +04:00
|
|
|
|
|
|
|
return true;
|
2013-03-22 23:22:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-12-10 20:12:18 +04:00
|
|
|
GLScreenBuffer::Swap(const gfx::IntSize& size)
|
2013-03-22 23:22:20 +04:00
|
|
|
{
|
|
|
|
SharedSurface* nextSurf = mStream->SwapProducer(mFactory, size);
|
|
|
|
if (!nextSurf) {
|
2014-07-12 02:10:49 +04:00
|
|
|
SurfaceFactory_Basic basicFactory(mGL, mFactory->mCaps);
|
2013-06-07 00:56:12 +04:00
|
|
|
nextSurf = mStream->SwapProducer(&basicFactory, size);
|
|
|
|
if (!nextSurf)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
NS_WARNING("SwapProd failed for sophisticated Factory type, fell back to Basic.");
|
2013-03-22 23:22:20 +04:00
|
|
|
}
|
2013-06-07 00:56:12 +04:00
|
|
|
MOZ_ASSERT(nextSurf);
|
2013-03-22 23:22:20 +04:00
|
|
|
|
2014-04-29 05:11:14 +04:00
|
|
|
return Attach(nextSurf, size);
|
2013-02-14 03:26:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-12-10 20:12:18 +04:00
|
|
|
GLScreenBuffer::PublishFrame(const gfx::IntSize& size)
|
2013-02-14 03:26:24 +04:00
|
|
|
{
|
|
|
|
AssureBlitted();
|
|
|
|
|
|
|
|
bool good = Swap(size);
|
|
|
|
return good;
|
|
|
|
}
|
|
|
|
|
2013-03-22 23:22:20 +04:00
|
|
|
bool
|
2013-12-10 20:12:18 +04:00
|
|
|
GLScreenBuffer::Resize(const gfx::IntSize& size)
|
2013-03-22 23:22:20 +04:00
|
|
|
{
|
|
|
|
SharedSurface* surface = mStream->Resize(mFactory, size);
|
|
|
|
if (!surface)
|
|
|
|
return false;
|
|
|
|
|
2014-04-29 05:11:14 +04:00
|
|
|
return Attach(surface, size);
|
2013-03-22 23:22:20 +04:00
|
|
|
}
|
2013-02-14 03:26:24 +04:00
|
|
|
|
2014-04-29 05:11:14 +04:00
|
|
|
bool
|
|
|
|
GLScreenBuffer::CreateDraw(const gfx::IntSize& size, DrawBuffer** out_buffer)
|
2013-02-14 03:26:24 +04:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
ReadBuffer*
|
2014-07-12 02:10:49 +04:00
|
|
|
GLScreenBuffer::CreateRead(SharedSurface* surf)
|
2013-02-14 03:26:24 +04:00
|
|
|
{
|
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->ReadCaps();
|
|
|
|
|
|
|
|
return ReadBuffer::Create(gl, caps, formats, surf);
|
|
|
|
}
|
|
|
|
|
2014-02-12 19:07:46 +04:00
|
|
|
void
|
2014-07-12 02:10:49 +04:00
|
|
|
GLScreenBuffer::Readback(SharedSurface* src, gfx::DataSourceSurface* dest)
|
2014-02-12 19:07:46 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(src && dest);
|
2014-07-12 02:10:49 +04:00
|
|
|
MOZ_ASSERT(dest->GetSize() == src->mSize);
|
|
|
|
MOZ_ASSERT(dest->GetFormat() == (src->mHasAlpha ? SurfaceFormat::B8G8R8A8
|
|
|
|
: SurfaceFormat::B8G8R8X8));
|
2013-02-14 03:26:24 +04:00
|
|
|
|
2014-04-29 03:26:54 +04:00
|
|
|
mGL->MakeCurrent();
|
2013-02-14 03:26:24 +04:00
|
|
|
|
2014-04-29 03:26:54 +04:00
|
|
|
bool needsSwap = src != SharedSurf();
|
|
|
|
if (needsSwap) {
|
|
|
|
SharedSurf()->UnlockProd();
|
|
|
|
src->LockProd();
|
|
|
|
}
|
2013-02-14 03:26:24 +04:00
|
|
|
|
2014-04-29 03:26:54 +04:00
|
|
|
ReadBuffer* buffer = CreateRead(src);
|
|
|
|
MOZ_ASSERT(buffer);
|
2013-02-14 03:26:24 +04:00
|
|
|
|
2014-07-12 02:10:49 +04:00
|
|
|
ScopedBindFramebuffer autoFB(mGL, buffer->mFB);
|
2014-04-29 03:26:54 +04:00
|
|
|
ReadPixelsIntoDataSurface(mGL, dest);
|
2013-02-14 03:26:24 +04:00
|
|
|
|
2014-04-29 03:26:54 +04:00
|
|
|
delete buffer;
|
2013-02-14 03:26:24 +04:00
|
|
|
|
2014-04-29 03:26:54 +04:00
|
|
|
if (needsSwap) {
|
|
|
|
src->UnlockProd();
|
|
|
|
SharedSurf()->LockProd();
|
|
|
|
}
|
2013-02-14 03:26:24 +04:00
|
|
|
}
|
|
|
|
|
2014-07-12 02:10:49 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// DrawBuffer
|
|
|
|
|
2014-04-29 05:11:14 +04:00
|
|
|
bool
|
2013-02-14 03:26:24 +04:00
|
|
|
DrawBuffer::Create(GLContext* const gl,
|
|
|
|
const SurfaceCaps& caps,
|
|
|
|
const GLFormats& formats,
|
2014-04-29 05:11:14 +04:00
|
|
|
const gfx::IntSize& size,
|
|
|
|
DrawBuffer** out_buffer)
|
2013-02-14 03:26:24 +04:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
2014-04-29 05:11:14 +04:00
|
|
|
ScopedDeletePtr<DrawBuffer> buffer;
|
|
|
|
buffer = new DrawBuffer(gl, size, fb, colorMSRB, depthRB, stencilRB);
|
|
|
|
|
|
|
|
if (!gl->IsFramebufferComplete(fb))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*out_buffer = buffer.forget();
|
|
|
|
return true;
|
2013-02-14 03:26:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
DrawBuffer::~DrawBuffer()
|
|
|
|
{
|
|
|
|
mGL->MakeCurrent();
|
|
|
|
|
|
|
|
GLuint fb = mFB;
|
|
|
|
GLuint rbs[] = {
|
|
|
|
mColorMSRB,
|
|
|
|
mDepthRB,
|
|
|
|
mStencilRB
|
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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'.
|
|
|
|
|
|
|
|
return new ReadBuffer(gl,
|
|
|
|
0, 0, 0,
|
|
|
|
surf);
|
|
|
|
}
|
|
|
|
|
|
|
|
GLuint depthRB = 0;
|
|
|
|
GLuint stencilRB = 0;
|
|
|
|
|
|
|
|
GLuint* pDepthRB = caps.depth ? &depthRB : nullptr;
|
|
|
|
GLuint* pStencilRB = caps.stencil ? &stencilRB : nullptr;
|
|
|
|
|
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:
|
2013-06-29 05:38:30 +04:00
|
|
|
MOZ_CRASH("Unknown attachment type?");
|
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-04-29 05:11:14 +04:00
|
|
|
ScopedDeletePtr<ReadBuffer> buffer;
|
|
|
|
buffer = new ReadBuffer(gl,
|
|
|
|
fb, depthRB, stencilRB,
|
|
|
|
surf);
|
|
|
|
if (!gl->IsFramebufferComplete(fb))
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return buffer.forget();
|
2013-02-14 03:26:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
ReadBuffer::~ReadBuffer()
|
|
|
|
{
|
|
|
|
mGL->MakeCurrent();
|
|
|
|
|
|
|
|
GLuint fb = mFB;
|
|
|
|
GLuint rbs[] = {
|
|
|
|
mDepthRB,
|
|
|
|
mStencilRB
|
|
|
|
};
|
|
|
|
|
|
|
|
mGL->fDeleteFramebuffers(1, &fb);
|
|
|
|
mGL->fDeleteRenderbuffers(2, rbs);
|
2013-07-18 07:24:15 +04:00
|
|
|
mGL->mFBOMapping.erase(mFB);
|
2013-02-14 03:26:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-07-12 02:10:49 +04:00
|
|
|
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);
|
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;
|
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:
|
2013-06-29 05:38:30 +04:00
|
|
|
MOZ_CRASH("Unknown attachment type?");
|
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-07-18 07:24:15 +04:00
|
|
|
mGL->mFBOMapping[mFB] = surf;
|
2013-02-14 03:26:24 +04:00
|
|
|
MOZ_ASSERT(mGL->IsFramebufferComplete(mFB));
|
|
|
|
}
|
|
|
|
|
|
|
|
mSurf = surf;
|
|
|
|
}
|
|
|
|
|
2013-12-10 20:11:58 +04:00
|
|
|
const gfx::IntSize&
|
2013-02-14 03:26:24 +04:00
|
|
|
ReadBuffer::Size() const
|
|
|
|
{
|
2014-07-12 02:10:49 +04:00
|
|
|
return mSurf->mSize;
|
2013-02-14 03:26:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
} /* namespace gl */
|
|
|
|
} /* namespace mozilla */
|