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 "SharedSurface.h"
|
|
|
|
|
2014-07-11 15:10:49 -07:00
|
|
|
#include "GLBlitHelper.h"
|
2014-09-02 15:15:41 -07:00
|
|
|
#include "GLContext.h"
|
|
|
|
#include "nsThreadUtils.h"
|
2014-07-11 15:10:49 -07:00
|
|
|
#include "ScopedGLHelpers.h"
|
|
|
|
#include "SharedSurfaceGL.h"
|
2013-02-13 15:26:24 -08:00
|
|
|
|
|
|
|
namespace mozilla {
|
2014-07-11 15:10:49 -07:00
|
|
|
namespace gl {
|
|
|
|
|
|
|
|
/*static*/ void
|
|
|
|
SharedSurface::ProdCopy(SharedSurface* src, SharedSurface* dest,
|
|
|
|
SurfaceFactory* factory)
|
|
|
|
{
|
|
|
|
GLContext* gl = src->mGL;
|
|
|
|
|
|
|
|
// If `src` begins locked, it must end locked, though we may
|
|
|
|
// temporarily unlock it if we need to.
|
|
|
|
MOZ_ASSERT((src == gl->GetLockedSurface()) == src->IsLocked());
|
|
|
|
|
|
|
|
gl->MakeCurrent();
|
|
|
|
|
|
|
|
if (src->mAttachType == AttachmentType::Screen &&
|
|
|
|
dest->mAttachType == AttachmentType::Screen)
|
|
|
|
{
|
|
|
|
// Here, we actually need to blit through a temp surface, so let's make one.
|
2014-08-15 17:38:08 -07:00
|
|
|
UniquePtr<SharedSurface_GLTexture> tempSurf;
|
2014-07-11 15:10:49 -07:00
|
|
|
tempSurf = SharedSurface_GLTexture::Create(gl,
|
|
|
|
gl,
|
|
|
|
factory->mFormats,
|
|
|
|
src->mSize,
|
|
|
|
factory->mCaps.alpha);
|
|
|
|
|
2014-08-15 17:38:08 -07:00
|
|
|
ProdCopy(src, tempSurf.get(), factory);
|
|
|
|
ProdCopy(tempSurf.get(), dest, factory);
|
2014-07-11 15:10:49 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (src->mAttachType == AttachmentType::Screen) {
|
|
|
|
SharedSurface* origLocked = gl->GetLockedSurface();
|
|
|
|
bool srcNeedsUnlock = false;
|
|
|
|
bool origNeedsRelock = false;
|
|
|
|
if (origLocked != src) {
|
|
|
|
if (origLocked) {
|
|
|
|
origLocked->UnlockProd();
|
|
|
|
origNeedsRelock = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
src->LockProd();
|
|
|
|
srcNeedsUnlock = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dest->mAttachType == AttachmentType::GLTexture) {
|
|
|
|
GLuint destTex = dest->ProdTexture();
|
|
|
|
GLenum destTarget = dest->ProdTextureTarget();
|
|
|
|
|
2014-08-07 19:03:25 -07:00
|
|
|
gl->BlitHelper()->BlitFramebufferToTexture(0, destTex,
|
|
|
|
src->mSize,
|
|
|
|
dest->mSize,
|
|
|
|
destTarget,
|
|
|
|
true);
|
2014-07-11 15:10:49 -07:00
|
|
|
} else if (dest->mAttachType == AttachmentType::GLRenderbuffer) {
|
|
|
|
GLuint destRB = dest->ProdRenderbuffer();
|
|
|
|
ScopedFramebufferForRenderbuffer destWrapper(gl, destRB);
|
|
|
|
|
2014-08-07 19:03:25 -07:00
|
|
|
gl->BlitHelper()->BlitFramebufferToFramebuffer(0,
|
|
|
|
destWrapper.FB(),
|
|
|
|
src->mSize,
|
|
|
|
dest->mSize,
|
|
|
|
true);
|
2014-07-11 15:10:49 -07:00
|
|
|
} else {
|
|
|
|
MOZ_CRASH("Unhandled dest->mAttachType.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (srcNeedsUnlock)
|
|
|
|
src->UnlockProd();
|
|
|
|
|
|
|
|
if (origNeedsRelock)
|
|
|
|
origLocked->LockProd();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dest->mAttachType == AttachmentType::Screen) {
|
|
|
|
SharedSurface* origLocked = gl->GetLockedSurface();
|
|
|
|
bool destNeedsUnlock = false;
|
|
|
|
bool origNeedsRelock = false;
|
|
|
|
if (origLocked != dest) {
|
|
|
|
if (origLocked) {
|
|
|
|
origLocked->UnlockProd();
|
|
|
|
origNeedsRelock = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
dest->LockProd();
|
|
|
|
destNeedsUnlock = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (src->mAttachType == AttachmentType::GLTexture) {
|
|
|
|
GLuint srcTex = src->ProdTexture();
|
|
|
|
GLenum srcTarget = src->ProdTextureTarget();
|
|
|
|
|
2014-08-07 19:03:25 -07:00
|
|
|
gl->BlitHelper()->BlitTextureToFramebuffer(srcTex, 0,
|
|
|
|
src->mSize,
|
|
|
|
dest->mSize,
|
|
|
|
srcTarget,
|
|
|
|
true);
|
2014-07-11 15:10:49 -07:00
|
|
|
} else if (src->mAttachType == AttachmentType::GLRenderbuffer) {
|
|
|
|
GLuint srcRB = src->ProdRenderbuffer();
|
|
|
|
ScopedFramebufferForRenderbuffer srcWrapper(gl, srcRB);
|
|
|
|
|
2014-08-07 19:03:25 -07:00
|
|
|
gl->BlitHelper()->BlitFramebufferToFramebuffer(srcWrapper.FB(),
|
|
|
|
0,
|
|
|
|
src->mSize,
|
|
|
|
dest->mSize,
|
|
|
|
true);
|
2014-07-11 15:10:49 -07:00
|
|
|
} else {
|
|
|
|
MOZ_CRASH("Unhandled src->mAttachType.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (destNeedsUnlock)
|
|
|
|
dest->UnlockProd();
|
|
|
|
|
|
|
|
if (origNeedsRelock)
|
|
|
|
origLocked->LockProd();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Alright, done with cases involving Screen types.
|
|
|
|
// Only {src,dest}x{texture,renderbuffer} left.
|
|
|
|
|
|
|
|
if (src->mAttachType == AttachmentType::GLTexture) {
|
|
|
|
GLuint srcTex = src->ProdTexture();
|
|
|
|
GLenum srcTarget = src->ProdTextureTarget();
|
|
|
|
|
|
|
|
if (dest->mAttachType == AttachmentType::GLTexture) {
|
|
|
|
GLuint destTex = dest->ProdTexture();
|
|
|
|
GLenum destTarget = dest->ProdTextureTarget();
|
|
|
|
|
|
|
|
gl->BlitHelper()->BlitTextureToTexture(srcTex, destTex,
|
|
|
|
src->mSize, dest->mSize,
|
|
|
|
srcTarget, destTarget);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dest->mAttachType == AttachmentType::GLRenderbuffer) {
|
|
|
|
GLuint destRB = dest->ProdRenderbuffer();
|
|
|
|
ScopedFramebufferForRenderbuffer destWrapper(gl, destRB);
|
|
|
|
|
|
|
|
gl->BlitHelper()->BlitTextureToFramebuffer(srcTex, destWrapper.FB(),
|
|
|
|
src->mSize, dest->mSize, srcTarget);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_CRASH("Unhandled dest->mAttachType.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (src->mAttachType == AttachmentType::GLRenderbuffer) {
|
|
|
|
GLuint srcRB = src->ProdRenderbuffer();
|
|
|
|
ScopedFramebufferForRenderbuffer srcWrapper(gl, srcRB);
|
|
|
|
|
|
|
|
if (dest->mAttachType == AttachmentType::GLTexture) {
|
|
|
|
GLuint destTex = dest->ProdTexture();
|
|
|
|
GLenum destTarget = dest->ProdTextureTarget();
|
|
|
|
|
|
|
|
gl->BlitHelper()->BlitFramebufferToTexture(srcWrapper.FB(), destTex,
|
|
|
|
src->mSize, dest->mSize, destTarget);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dest->mAttachType == AttachmentType::GLRenderbuffer) {
|
|
|
|
GLuint destRB = dest->ProdRenderbuffer();
|
|
|
|
ScopedFramebufferForRenderbuffer destWrapper(gl, destRB);
|
|
|
|
|
|
|
|
gl->BlitHelper()->BlitFramebufferToFramebuffer(srcWrapper.FB(), destWrapper.FB(),
|
|
|
|
src->mSize, dest->mSize);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_CRASH("Unhandled dest->mAttachType.");
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_CRASH("Unhandled src->mAttachType.");
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// SharedSurface
|
|
|
|
|
2014-09-02 15:15:41 -07:00
|
|
|
|
|
|
|
SharedSurface::SharedSurface(SharedSurfaceType type,
|
|
|
|
AttachmentType attachType,
|
|
|
|
GLContext* gl,
|
|
|
|
const gfx::IntSize& size,
|
|
|
|
bool hasAlpha)
|
|
|
|
: mType(type)
|
|
|
|
, mAttachType(attachType)
|
|
|
|
, mGL(gl)
|
|
|
|
, mSize(size)
|
|
|
|
, mHasAlpha(hasAlpha)
|
|
|
|
, mIsLocked(false)
|
|
|
|
#ifdef DEBUG
|
|
|
|
, mOwningThread(NS_GetCurrentThread())
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-07-11 15:10:49 -07:00
|
|
|
void
|
|
|
|
SharedSurface::LockProd()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mIsLocked);
|
|
|
|
|
|
|
|
LockProdImpl();
|
|
|
|
|
|
|
|
mGL->LockSurface(this);
|
|
|
|
mIsLocked = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SharedSurface::UnlockProd()
|
|
|
|
{
|
|
|
|
if (!mIsLocked)
|
|
|
|
return;
|
|
|
|
|
|
|
|
UnlockProdImpl();
|
|
|
|
|
|
|
|
mGL->UnlockSurface(this);
|
|
|
|
mIsLocked = false;
|
|
|
|
}
|
|
|
|
|
2014-09-02 15:15:41 -07:00
|
|
|
void
|
|
|
|
SharedSurface::Fence_ContentThread()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_GetCurrentThread() == mOwningThread);
|
|
|
|
Fence_ContentThread_Impl();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
SharedSurface::WaitSync_ContentThread()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_GetCurrentThread() == mOwningThread);
|
|
|
|
return WaitSync_ContentThread_Impl();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
SharedSurface::PollSync_ContentThread()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_GetCurrentThread() == mOwningThread);
|
|
|
|
return PollSync_ContentThread_Impl();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-07-11 15:10:49 -07:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// SurfaceFactory
|
|
|
|
|
|
|
|
static void
|
|
|
|
ChooseBufferBits(const SurfaceCaps& caps,
|
|
|
|
SurfaceCaps* const out_drawCaps,
|
|
|
|
SurfaceCaps* const out_readCaps)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(out_drawCaps);
|
|
|
|
MOZ_ASSERT(out_readCaps);
|
|
|
|
|
|
|
|
SurfaceCaps screenCaps;
|
|
|
|
|
|
|
|
screenCaps.color = caps.color;
|
|
|
|
screenCaps.alpha = caps.alpha;
|
|
|
|
screenCaps.bpp16 = caps.bpp16;
|
|
|
|
|
|
|
|
screenCaps.depth = caps.depth;
|
|
|
|
screenCaps.stencil = caps.stencil;
|
|
|
|
|
|
|
|
screenCaps.antialias = caps.antialias;
|
|
|
|
screenCaps.preserve = caps.preserve;
|
|
|
|
|
|
|
|
if (caps.antialias) {
|
|
|
|
*out_drawCaps = screenCaps;
|
|
|
|
out_readCaps->Clear();
|
|
|
|
|
|
|
|
// Color caps need to be duplicated in readCaps.
|
|
|
|
out_readCaps->color = caps.color;
|
|
|
|
out_readCaps->alpha = caps.alpha;
|
|
|
|
out_readCaps->bpp16 = caps.bpp16;
|
|
|
|
} else {
|
|
|
|
out_drawCaps->Clear();
|
|
|
|
*out_readCaps = screenCaps;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SurfaceFactory::SurfaceFactory(GLContext* gl,
|
|
|
|
SharedSurfaceType type,
|
|
|
|
const SurfaceCaps& caps)
|
|
|
|
: mGL(gl)
|
|
|
|
, mCaps(caps)
|
|
|
|
, mType(type)
|
|
|
|
, mFormats(gl->ChooseGLFormats(caps))
|
|
|
|
{
|
|
|
|
ChooseBufferBits(mCaps, &mDrawCaps, &mReadCaps);
|
|
|
|
}
|
|
|
|
|
|
|
|
SurfaceFactory::~SurfaceFactory()
|
|
|
|
{
|
2014-08-15 17:38:08 -07:00
|
|
|
while (!mScraps.Empty()) {
|
2014-08-15 17:38:09 -07:00
|
|
|
mScraps.Pop();
|
2014-07-11 15:10:49 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-15 17:38:08 -07:00
|
|
|
UniquePtr<SharedSurface>
|
2014-07-11 15:10:49 -07:00
|
|
|
SurfaceFactory::NewSharedSurface(const gfx::IntSize& size)
|
|
|
|
{
|
|
|
|
// Attempt to reuse an old surface.
|
2014-08-15 17:38:08 -07:00
|
|
|
while (!mScraps.Empty()) {
|
|
|
|
UniquePtr<SharedSurface> cur = mScraps.Pop();
|
2014-08-15 17:38:08 -07:00
|
|
|
|
2014-07-11 15:10:49 -07:00
|
|
|
if (cur->mSize == size)
|
2014-08-15 17:38:08 -07:00
|
|
|
return Move(cur);
|
2014-07-11 15:10:49 -07:00
|
|
|
|
2014-08-15 17:38:08 -07:00
|
|
|
// Let `cur` be destroyed as it falls out of scope, if it wasn't
|
|
|
|
// moved.
|
2014-07-11 15:10:49 -07:00
|
|
|
}
|
|
|
|
|
2014-08-15 17:38:08 -07:00
|
|
|
return CreateShared(size);
|
2014-07-11 15:10:49 -07:00
|
|
|
}
|
2013-02-13 15:26:24 -08:00
|
|
|
|
2014-07-11 15:10:49 -07:00
|
|
|
// Auto-deletes surfs of the wrong type.
|
2013-02-13 15:26:24 -08:00
|
|
|
void
|
2014-08-15 17:38:08 -07:00
|
|
|
SurfaceFactory::Recycle(UniquePtr<SharedSurface> surf)
|
2013-02-13 15:26:24 -08:00
|
|
|
{
|
2014-08-15 17:38:08 -07:00
|
|
|
MOZ_ASSERT(surf);
|
2013-02-13 15:26:24 -08:00
|
|
|
|
2014-07-11 15:10:49 -07:00
|
|
|
if (surf->mType == mType) {
|
2014-08-15 17:38:08 -07:00
|
|
|
mScraps.Push(Move(surf));
|
2014-07-11 15:10:49 -07:00
|
|
|
}
|
2013-02-13 15:26:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
} /* namespace gfx */
|
|
|
|
} /* namespace mozilla */
|