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 "SharedSurfaceGL.h"
|
|
|
|
#include "GLContext.h"
|
2013-11-28 12:57:19 -08:00
|
|
|
#include "GLBlitHelper.h"
|
2013-11-25 19:25:25 -08:00
|
|
|
#include "ScopedGLHelpers.h"
|
2013-02-13 15:26:24 -08:00
|
|
|
#include "gfxImageSurface.h"
|
2013-12-10 08:11:58 -08:00
|
|
|
#include "mozilla/gfx/2D.h"
|
2013-02-13 15:26:24 -08:00
|
|
|
|
|
|
|
using namespace mozilla::gfx;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace gl {
|
|
|
|
|
|
|
|
// |src| must begin and end locked, though we may
|
|
|
|
// temporarily unlock it if we need to.
|
|
|
|
void
|
|
|
|
SharedSurface_GL::Copy(SharedSurface_GL* src, SharedSurface_GL* dest,
|
|
|
|
SurfaceFactory_GL* factory)
|
|
|
|
{
|
|
|
|
GLContext* gl = src->GL();
|
|
|
|
|
|
|
|
if (src->AttachType() == AttachmentType::Screen &&
|
|
|
|
dest->AttachType() == AttachmentType::Screen)
|
|
|
|
{
|
|
|
|
// Here, we actually need to blit through a temp surface, so let's make one.
|
|
|
|
nsAutoPtr<SharedSurface_GLTexture> tempSurf(
|
|
|
|
SharedSurface_GLTexture::Create(gl, gl,
|
|
|
|
factory->Formats(),
|
|
|
|
src->Size(),
|
|
|
|
factory->Caps().alpha));
|
|
|
|
|
|
|
|
Copy(src, tempSurf, factory);
|
|
|
|
Copy(tempSurf, dest, factory);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (src->AttachType() == 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->AttachType() == AttachmentType::GLTexture) {
|
|
|
|
GLuint destTex = dest->Texture();
|
2013-07-17 20:24:15 -07:00
|
|
|
GLenum destTarget = dest->TextureTarget();
|
2013-02-13 15:26:24 -08:00
|
|
|
|
2013-11-28 12:57:19 -08:00
|
|
|
gl->BlitHelper()->BlitFramebufferToTexture(0, destTex, src->Size(), dest->Size(), destTarget);
|
2013-02-13 15:26:24 -08:00
|
|
|
} else if (dest->AttachType() == AttachmentType::GLRenderbuffer) {
|
|
|
|
GLuint destRB = dest->Renderbuffer();
|
|
|
|
ScopedFramebufferForRenderbuffer destWrapper(gl, destRB);
|
|
|
|
|
2013-11-28 12:57:19 -08:00
|
|
|
gl->BlitHelper()->BlitFramebufferToFramebuffer(0, destWrapper.FB(),
|
|
|
|
src->Size(), dest->Size());
|
2013-02-13 15:26:24 -08:00
|
|
|
} else {
|
2013-06-28 18:38:30 -07:00
|
|
|
MOZ_CRASH("Unhandled dest->AttachType().");
|
2013-02-13 15:26:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (srcNeedsUnlock)
|
|
|
|
src->UnlockProd();
|
|
|
|
|
|
|
|
if (origNeedsRelock)
|
|
|
|
origLocked->LockProd();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dest->AttachType() == 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->AttachType() == AttachmentType::GLTexture) {
|
|
|
|
GLuint srcTex = src->Texture();
|
2013-07-17 20:24:15 -07:00
|
|
|
GLenum srcTarget = src->TextureTarget();
|
2013-02-13 15:26:24 -08:00
|
|
|
|
2013-11-28 12:57:19 -08:00
|
|
|
gl->BlitHelper()->BlitTextureToFramebuffer(srcTex, 0, src->Size(), dest->Size(), srcTarget);
|
2013-02-13 15:26:24 -08:00
|
|
|
} else if (src->AttachType() == AttachmentType::GLRenderbuffer) {
|
|
|
|
GLuint srcRB = src->Renderbuffer();
|
|
|
|
ScopedFramebufferForRenderbuffer srcWrapper(gl, srcRB);
|
|
|
|
|
2013-11-28 12:57:19 -08:00
|
|
|
gl->BlitHelper()->BlitFramebufferToFramebuffer(srcWrapper.FB(), 0,
|
|
|
|
src->Size(), dest->Size());
|
2013-02-13 15:26:24 -08:00
|
|
|
} else {
|
2013-06-28 18:38:30 -07:00
|
|
|
MOZ_CRASH("Unhandled src->AttachType().");
|
2013-02-13 15:26:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
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->AttachType() == AttachmentType::GLTexture) {
|
|
|
|
GLuint srcTex = src->Texture();
|
2013-07-17 20:24:15 -07:00
|
|
|
GLenum srcTarget = src->TextureTarget();
|
2013-02-13 15:26:24 -08:00
|
|
|
|
|
|
|
if (dest->AttachType() == AttachmentType::GLTexture) {
|
|
|
|
GLuint destTex = dest->Texture();
|
2013-07-17 20:24:15 -07:00
|
|
|
GLenum destTarget = dest->TextureTarget();
|
2013-02-13 15:26:24 -08:00
|
|
|
|
2013-11-28 12:57:19 -08:00
|
|
|
gl->BlitHelper()->BlitTextureToTexture(srcTex, destTex,
|
|
|
|
src->Size(), dest->Size(),
|
|
|
|
srcTarget, destTarget);
|
2013-02-13 15:26:24 -08:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dest->AttachType() == AttachmentType::GLRenderbuffer) {
|
|
|
|
GLuint destRB = dest->Renderbuffer();
|
|
|
|
ScopedFramebufferForRenderbuffer destWrapper(gl, destRB);
|
|
|
|
|
2013-11-28 12:57:19 -08:00
|
|
|
gl->BlitHelper()->BlitTextureToFramebuffer(srcTex, destWrapper.FB(),
|
|
|
|
src->Size(), dest->Size(), srcTarget);
|
2013-02-13 15:26:24 -08:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-28 18:38:30 -07:00
|
|
|
MOZ_CRASH("Unhandled dest->AttachType().");
|
2013-02-13 15:26:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (src->AttachType() == AttachmentType::GLRenderbuffer) {
|
|
|
|
GLuint srcRB = src->Renderbuffer();
|
|
|
|
ScopedFramebufferForRenderbuffer srcWrapper(gl, srcRB);
|
|
|
|
|
|
|
|
if (dest->AttachType() == AttachmentType::GLTexture) {
|
|
|
|
GLuint destTex = dest->Texture();
|
2013-07-17 20:24:15 -07:00
|
|
|
GLenum destTarget = dest->TextureTarget();
|
2013-02-13 15:26:24 -08:00
|
|
|
|
2013-11-28 12:57:19 -08:00
|
|
|
gl->BlitHelper()->BlitFramebufferToTexture(srcWrapper.FB(), destTex,
|
|
|
|
src->Size(), dest->Size(), destTarget);
|
2013-02-13 15:26:24 -08:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dest->AttachType() == AttachmentType::GLRenderbuffer) {
|
|
|
|
GLuint destRB = dest->Renderbuffer();
|
|
|
|
ScopedFramebufferForRenderbuffer destWrapper(gl, destRB);
|
|
|
|
|
2013-11-28 12:57:19 -08:00
|
|
|
gl->BlitHelper()->BlitFramebufferToFramebuffer(srcWrapper.FB(), destWrapper.FB(),
|
|
|
|
src->Size(), dest->Size());
|
2013-02-13 15:26:24 -08:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-28 18:38:30 -07:00
|
|
|
MOZ_CRASH("Unhandled dest->AttachType().");
|
2013-02-13 15:26:24 -08:00
|
|
|
}
|
|
|
|
|
2013-06-28 18:38:30 -07:00
|
|
|
MOZ_CRASH("Unhandled src->AttachType().");
|
2013-02-13 15:26:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SharedSurface_GL::LockProd()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mIsLocked);
|
|
|
|
|
|
|
|
LockProdImpl();
|
|
|
|
|
|
|
|
mGL->LockSurface(this);
|
|
|
|
mIsLocked = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SharedSurface_GL::UnlockProd()
|
|
|
|
{
|
|
|
|
if (!mIsLocked)
|
|
|
|
return;
|
|
|
|
|
|
|
|
UnlockProdImpl();
|
|
|
|
|
|
|
|
mGL->UnlockSurface(this);
|
|
|
|
mIsLocked = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SurfaceFactory_GL::SurfaceFactory_GL(GLContext* gl,
|
|
|
|
SharedSurfaceType type,
|
|
|
|
const SurfaceCaps& caps)
|
|
|
|
: SurfaceFactory(type, caps)
|
|
|
|
, mGL(gl)
|
|
|
|
, mFormats(gl->ChooseGLFormats(caps))
|
|
|
|
{
|
|
|
|
ChooseBufferBits(caps, mDrawCaps, mReadCaps);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SurfaceFactory_GL::ChooseBufferBits(const SurfaceCaps& caps,
|
|
|
|
SurfaceCaps& drawCaps,
|
|
|
|
SurfaceCaps& readCaps) const
|
|
|
|
{
|
|
|
|
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) {
|
|
|
|
drawCaps = screenCaps;
|
|
|
|
readCaps.Clear();
|
|
|
|
|
|
|
|
// Color caps need to be duplicated in readCaps.
|
|
|
|
readCaps.color = caps.color;
|
|
|
|
readCaps.alpha = caps.alpha;
|
|
|
|
readCaps.bpp16 = caps.bpp16;
|
|
|
|
} else {
|
|
|
|
drawCaps.Clear();
|
|
|
|
readCaps = screenCaps;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SharedSurface_Basic*
|
|
|
|
SharedSurface_Basic::Create(GLContext* gl,
|
|
|
|
const GLFormats& formats,
|
2013-12-10 08:11:58 -08:00
|
|
|
const IntSize& size,
|
2013-02-13 15:26:24 -08:00
|
|
|
bool hasAlpha)
|
|
|
|
{
|
|
|
|
gl->MakeCurrent();
|
2013-12-06 07:36:13 -08:00
|
|
|
GLuint tex = CreateTexture(gl, formats.color_texInternalFormat,
|
|
|
|
formats.color_texFormat,
|
|
|
|
formats.color_texType,
|
|
|
|
size);
|
2013-02-13 15:26:24 -08:00
|
|
|
|
2013-12-10 08:11:58 -08:00
|
|
|
SurfaceFormat format = FORMAT_B8G8R8X8;
|
2013-02-13 15:26:24 -08:00
|
|
|
switch (formats.color_texInternalFormat) {
|
|
|
|
case LOCAL_GL_RGB:
|
|
|
|
case LOCAL_GL_RGB8:
|
|
|
|
if (formats.color_texType == LOCAL_GL_UNSIGNED_SHORT_5_6_5)
|
2013-12-10 08:11:58 -08:00
|
|
|
format = FORMAT_R5G6B5;
|
2013-02-13 15:26:24 -08:00
|
|
|
else
|
2013-12-10 08:11:58 -08:00
|
|
|
format = FORMAT_B8G8R8X8;
|
2013-02-13 15:26:24 -08:00
|
|
|
break;
|
|
|
|
case LOCAL_GL_RGBA:
|
|
|
|
case LOCAL_GL_RGBA8:
|
2013-12-10 08:11:58 -08:00
|
|
|
format = FORMAT_B8G8R8A8;
|
2013-02-13 15:26:24 -08:00
|
|
|
break;
|
|
|
|
default:
|
2013-06-28 18:38:30 -07:00
|
|
|
MOZ_CRASH("Unhandled Tex format.");
|
2013-02-13 15:26:24 -08:00
|
|
|
}
|
|
|
|
return new SharedSurface_Basic(gl, size, hasAlpha, format, tex);
|
|
|
|
}
|
|
|
|
|
|
|
|
SharedSurface_Basic::SharedSurface_Basic(GLContext* gl,
|
2013-12-10 08:11:58 -08:00
|
|
|
const IntSize& size,
|
2013-02-13 15:26:24 -08:00
|
|
|
bool hasAlpha,
|
2013-12-10 08:11:58 -08:00
|
|
|
SurfaceFormat format,
|
2013-02-13 15:26:24 -08:00
|
|
|
GLuint tex)
|
|
|
|
: SharedSurface_GL(SharedSurfaceType::Basic,
|
|
|
|
AttachmentType::GLTexture,
|
|
|
|
gl,
|
|
|
|
size,
|
|
|
|
hasAlpha)
|
|
|
|
, mTex(tex)
|
|
|
|
{
|
2013-12-10 08:11:58 -08:00
|
|
|
mData = Factory::CreateDataSourceSurface(size, format);
|
2013-02-13 15:26:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
SharedSurface_Basic::~SharedSurface_Basic()
|
|
|
|
{
|
|
|
|
if (!mGL->MakeCurrent())
|
|
|
|
return;
|
|
|
|
|
|
|
|
GLuint tex = mTex;
|
|
|
|
mGL->fDeleteTextures(1, &tex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SharedSurface_Basic::Fence()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mData->GetSize() == mGL->OffscreenSize());
|
|
|
|
|
|
|
|
mGL->MakeCurrent();
|
2013-12-10 08:11:58 -08:00
|
|
|
nsRefPtr<gfxImageSurface> wrappedData =
|
|
|
|
new gfxImageSurface(mData->GetData(),
|
|
|
|
ThebesIntSize(mData->GetSize()),
|
|
|
|
mData->Stride(),
|
|
|
|
SurfaceFormatToImageFormat(mData->GetFormat()));
|
|
|
|
mGL->ReadScreenIntoImageSurface(wrappedData);
|
2013-02-13 15:26:24 -08:00
|
|
|
mData->MarkDirty();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SharedSurface_GLTexture*
|
|
|
|
SharedSurface_GLTexture::Create(GLContext* prodGL,
|
2013-12-10 08:11:58 -08:00
|
|
|
GLContext* consGL,
|
|
|
|
const GLFormats& formats,
|
|
|
|
const gfx::IntSize& size,
|
|
|
|
bool hasAlpha)
|
2013-02-13 15:26:24 -08:00
|
|
|
{
|
2013-06-16 19:48:41 -07:00
|
|
|
MOZ_ASSERT(prodGL);
|
|
|
|
MOZ_ASSERT(!consGL || prodGL->SharesWith(consGL));
|
2013-02-13 15:26:24 -08:00
|
|
|
|
|
|
|
prodGL->MakeCurrent();
|
2013-12-06 07:36:13 -08:00
|
|
|
GLuint tex = CreateTextureForOffscreen(prodGL, formats, size);
|
2013-02-13 15:26:24 -08:00
|
|
|
|
|
|
|
return new SharedSurface_GLTexture(prodGL, consGL, size, hasAlpha, tex);
|
|
|
|
}
|
|
|
|
|
|
|
|
SharedSurface_GLTexture::~SharedSurface_GLTexture()
|
|
|
|
{
|
|
|
|
if (!mGL->MakeCurrent())
|
|
|
|
return;
|
|
|
|
|
|
|
|
GLuint tex = mTex;
|
|
|
|
mGL->fDeleteTextures(1, &tex);
|
|
|
|
|
|
|
|
if (mSync) {
|
|
|
|
mGL->fDeleteSync(mSync);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SharedSurface_GLTexture::Fence()
|
|
|
|
{
|
2013-06-04 17:05:28 -07:00
|
|
|
MutexAutoLock lock(mMutex);
|
2013-02-13 15:26:24 -08:00
|
|
|
mGL->MakeCurrent();
|
|
|
|
|
2013-06-04 17:05:28 -07:00
|
|
|
if (mConsGL && mGL->IsExtensionSupported(GLContext::ARB_sync)) {
|
2013-02-13 15:26:24 -08:00
|
|
|
if (mSync) {
|
|
|
|
mGL->fDeleteSync(mSync);
|
|
|
|
mSync = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
mSync = mGL->fFenceSync(LOCAL_GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
|
|
|
|
if (mSync) {
|
|
|
|
mGL->fFlush();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(!mSync);
|
|
|
|
|
|
|
|
mGL->fFinish();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
SharedSurface_GLTexture::WaitSync()
|
|
|
|
{
|
2013-06-04 17:05:28 -07:00
|
|
|
MutexAutoLock lock(mMutex);
|
2013-02-13 15:26:24 -08:00
|
|
|
if (!mSync) {
|
|
|
|
// We must have used glFinish instead of glFenceSync.
|
|
|
|
return true;
|
|
|
|
}
|
2013-06-04 17:05:28 -07:00
|
|
|
|
|
|
|
MOZ_ASSERT(mConsGL, "Did you forget to call a deferred `SetConsumerGL()`?");
|
2013-03-26 22:29:07 -07:00
|
|
|
mConsGL->MakeCurrent();
|
|
|
|
MOZ_ASSERT(mConsGL->IsExtensionSupported(GLContext::ARB_sync));
|
2013-02-13 15:26:24 -08:00
|
|
|
|
2013-03-26 22:29:07 -07:00
|
|
|
mConsGL->fWaitSync(mSync,
|
|
|
|
0,
|
|
|
|
LOCAL_GL_TIMEOUT_IGNORED);
|
|
|
|
mConsGL->fDeleteSync(mSync);
|
2013-02-13 15:26:24 -08:00
|
|
|
mSync = 0;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-04 17:05:28 -07:00
|
|
|
void
|
|
|
|
SharedSurface_GLTexture::SetConsumerGL(GLContext* consGL)
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
MOZ_ASSERT(consGL);
|
2013-06-16 19:48:41 -07:00
|
|
|
MOZ_ASSERT(mGL->SharesWith(consGL));
|
2013-06-04 17:05:28 -07:00
|
|
|
mConsGL = consGL;
|
|
|
|
}
|
|
|
|
|
2013-02-13 15:26:24 -08:00
|
|
|
} /* namespace gfx */
|
|
|
|
} /* namespace mozilla */
|