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"
|
2014-07-11 15:10:49 -07:00
|
|
|
|
2013-11-28 12:57:19 -08:00
|
|
|
#include "GLBlitHelper.h"
|
2014-09-02 15:16:02 -07:00
|
|
|
#include "GLContext.h"
|
2014-01-02 07:17:29 -08:00
|
|
|
#include "GLReadTexImageHelper.h"
|
2014-09-02 15:16:02 -07:00
|
|
|
#include "mozilla/gfx/2D.h"
|
|
|
|
#include "ScopedGLHelpers.h"
|
2013-02-13 15:26:24 -08:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace gl {
|
|
|
|
|
2014-07-11 15:10:49 -07:00
|
|
|
using gfx::IntSize;
|
|
|
|
using gfx::SurfaceFormat;
|
2013-02-13 15:26:24 -08:00
|
|
|
|
2014-08-15 17:38:08 -07:00
|
|
|
/*static*/ UniquePtr<SharedSurface_Basic>
|
2013-02-13 15:26:24 -08:00
|
|
|
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)
|
|
|
|
{
|
2014-08-27 16:18:46 -07:00
|
|
|
UniquePtr<SharedSurface_Basic> ret;
|
2013-02-13 15:26:24 -08:00
|
|
|
gl->MakeCurrent();
|
2014-08-27 16:18:43 -07:00
|
|
|
|
|
|
|
GLContext::ScopedLocalErrorCheck localError(gl);
|
2013-12-06 07:36:13 -08:00
|
|
|
GLuint tex = CreateTexture(gl, formats.color_texInternalFormat,
|
|
|
|
formats.color_texFormat,
|
|
|
|
formats.color_texType,
|
|
|
|
size);
|
2014-08-27 16:18:43 -07:00
|
|
|
GLenum err = localError.GetLocalError();
|
|
|
|
if (err) {
|
|
|
|
gl->fDeleteTextures(1, &tex);
|
2014-08-27 16:18:46 -07:00
|
|
|
return Move(ret);
|
2014-08-27 16:18:43 -07:00
|
|
|
}
|
2013-02-13 15:26:24 -08:00
|
|
|
|
2014-01-10 11:06:16 -08:00
|
|
|
SurfaceFormat format = SurfaceFormat::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)
|
2014-01-10 11:06:16 -08:00
|
|
|
format = SurfaceFormat::R5G6B5;
|
2013-02-13 15:26:24 -08:00
|
|
|
else
|
2014-01-10 11:06:16 -08:00
|
|
|
format = SurfaceFormat::B8G8R8X8;
|
2013-02-13 15:26:24 -08:00
|
|
|
break;
|
|
|
|
case LOCAL_GL_RGBA:
|
|
|
|
case LOCAL_GL_RGBA8:
|
2014-01-10 11:06:16 -08:00
|
|
|
format = SurfaceFormat::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
|
|
|
}
|
2014-08-15 17:38:08 -07:00
|
|
|
|
2014-08-27 16:18:46 -07:00
|
|
|
ret.reset( new SharedSurface_Basic(gl, size, hasAlpha, format, tex) );
|
2014-08-15 17:38:08 -07:00
|
|
|
return Move(ret);
|
2013-02-13 15:26:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2014-07-11 15:10:49 -07:00
|
|
|
: SharedSurface(SharedSurfaceType::Basic,
|
|
|
|
AttachmentType::GLTexture,
|
|
|
|
gl,
|
|
|
|
size,
|
|
|
|
hasAlpha)
|
|
|
|
, mTex(tex)
|
|
|
|
, mFB(0)
|
2014-09-02 15:16:02 -07:00
|
|
|
, mIsDataCurrent(false)
|
2013-02-13 15:26:24 -08:00
|
|
|
{
|
2014-03-05 13:49:37 -08:00
|
|
|
mGL->MakeCurrent();
|
|
|
|
mGL->fGenFramebuffers(1, &mFB);
|
|
|
|
|
|
|
|
ScopedBindFramebuffer autoFB(mGL, mFB);
|
|
|
|
mGL->fFramebufferTexture2D(LOCAL_GL_FRAMEBUFFER,
|
|
|
|
LOCAL_GL_COLOR_ATTACHMENT0,
|
|
|
|
LOCAL_GL_TEXTURE_2D,
|
|
|
|
mTex,
|
|
|
|
0);
|
|
|
|
|
2014-08-27 16:18:43 -07:00
|
|
|
DebugOnly<GLenum> status = mGL->fCheckFramebufferStatus(LOCAL_GL_FRAMEBUFFER);
|
|
|
|
MOZ_ASSERT(status == LOCAL_GL_FRAMEBUFFER_COMPLETE);
|
2014-03-05 13:49:37 -08:00
|
|
|
|
2014-07-11 15:10:49 -07:00
|
|
|
int32_t stride = gfx::GetAlignedStride<4>(size.width * BytesPerPixel(format));
|
|
|
|
mData = gfx::Factory::CreateDataSourceSurfaceWithStride(size, format, stride);
|
2014-08-27 08:57:43 -07:00
|
|
|
// Leave the extra return for clarity, in case we decide more code should
|
|
|
|
// be added after this check, that should run even if mData is null.
|
|
|
|
if (NS_WARN_IF(!mData)) {
|
|
|
|
return;
|
|
|
|
}
|
2013-02-13 15:26:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
SharedSurface_Basic::~SharedSurface_Basic()
|
|
|
|
{
|
|
|
|
if (!mGL->MakeCurrent())
|
|
|
|
return;
|
|
|
|
|
2014-03-05 13:49:37 -08:00
|
|
|
if (mFB)
|
|
|
|
mGL->fDeleteFramebuffers(1, &mFB);
|
|
|
|
|
2014-02-13 15:02:03 -08:00
|
|
|
mGL->fDeleteTextures(1, &mTex);
|
2013-02-13 15:26:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SharedSurface_Basic::Fence()
|
|
|
|
{
|
2014-08-27 08:57:43 -07:00
|
|
|
// The constructor can fail to get us mData, we should deal with it:
|
|
|
|
if (NS_WARN_IF(!mData)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-20 15:23:52 -08:00
|
|
|
mGL->MakeCurrent();
|
2014-03-05 13:49:37 -08:00
|
|
|
ScopedBindFramebuffer autoFB(mGL, mFB);
|
2014-04-28 16:26:54 -07:00
|
|
|
ReadPixelsIntoDataSurface(mGL, mData);
|
2014-09-02 15:16:02 -07:00
|
|
|
mIsDataCurrent = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
SharedSurface_Basic::WaitSync()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mIsDataCurrent);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
SharedSurface_Basic::PollSync()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mIsDataCurrent);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SharedSurface_Basic::Fence_ContentThread_Impl()
|
|
|
|
{
|
|
|
|
mIsDataCurrent = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
SharedSurface_Basic::WaitSync_ContentThread_Impl()
|
|
|
|
{
|
|
|
|
if (!mIsDataCurrent) {
|
|
|
|
mGL->MakeCurrent();
|
|
|
|
ScopedBindFramebuffer autoFB(mGL, mFB);
|
|
|
|
ReadPixelsIntoDataSurface(mGL, mData);
|
|
|
|
mIsDataCurrent = true;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
SharedSurface_Basic::PollSync_ContentThread_Impl()
|
|
|
|
{
|
|
|
|
if (!mIsDataCurrent) {
|
|
|
|
mGL->MakeCurrent();
|
|
|
|
ScopedBindFramebuffer autoFB(mGL, mFB);
|
|
|
|
ReadPixelsIntoDataSurface(mGL, mData);
|
|
|
|
mIsDataCurrent = true;
|
|
|
|
}
|
|
|
|
return true;
|
2013-02-13 15:26:24 -08:00
|
|
|
}
|
|
|
|
|
2014-08-15 17:38:08 -07:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// SharedSurface_GLTexture
|
2013-02-13 15:26:24 -08:00
|
|
|
|
2014-08-15 17:38:08 -07:00
|
|
|
/*static*/ UniquePtr<SharedSurface_GLTexture>
|
2013-02-13 15:26:24 -08:00
|
|
|
SharedSurface_GLTexture::Create(GLContext* prodGL,
|
2013-12-10 08:11:58 -08:00
|
|
|
GLContext* consGL,
|
|
|
|
const GLFormats& formats,
|
2014-07-11 15:10:49 -07:00
|
|
|
const IntSize& size,
|
2014-03-05 13:49:37 -08:00
|
|
|
bool hasAlpha,
|
|
|
|
GLuint texture)
|
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();
|
|
|
|
|
2014-03-05 13:49:37 -08:00
|
|
|
GLuint tex = texture;
|
|
|
|
|
|
|
|
bool ownsTex = false;
|
|
|
|
|
2014-08-27 16:18:43 -07:00
|
|
|
UniquePtr<SharedSurface_GLTexture> ret;
|
|
|
|
|
2014-03-05 13:49:37 -08:00
|
|
|
if (!tex) {
|
2014-08-27 16:18:43 -07:00
|
|
|
GLContext::ScopedLocalErrorCheck localError(prodGL);
|
|
|
|
|
2014-03-05 13:49:37 -08:00
|
|
|
tex = CreateTextureForOffscreen(prodGL, formats, size);
|
2014-08-27 16:18:43 -07:00
|
|
|
|
|
|
|
GLenum err = localError.GetLocalError();
|
|
|
|
if (err) {
|
|
|
|
prodGL->fDeleteTextures(1, &tex);
|
|
|
|
return Move(ret);
|
|
|
|
}
|
|
|
|
|
2014-03-05 13:49:37 -08:00
|
|
|
ownsTex = true;
|
|
|
|
}
|
|
|
|
|
2014-08-27 16:18:43 -07:00
|
|
|
ret.reset( new SharedSurface_GLTexture(prodGL, consGL, size,
|
|
|
|
hasAlpha, tex, ownsTex) );
|
2014-08-15 17:38:08 -07:00
|
|
|
return Move(ret);
|
2013-02-13 15:26:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
SharedSurface_GLTexture::~SharedSurface_GLTexture()
|
|
|
|
{
|
|
|
|
if (!mGL->MakeCurrent())
|
|
|
|
return;
|
|
|
|
|
2014-03-05 13:49:37 -08:00
|
|
|
if (mOwnsTex) {
|
|
|
|
mGL->fDeleteTextures(1, &mTex);
|
|
|
|
}
|
2013-02-13 15:26:24 -08:00
|
|
|
|
|
|
|
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) {
|
2014-08-04 22:10:47 -07:00
|
|
|
// We either used glFinish, or we passed this fence already.
|
|
|
|
// (PollSync/WaitSync returned true previously)
|
2013-02-13 15:26:24 -08:00
|
|
|
return true;
|
|
|
|
}
|
2013-06-04 17:05:28 -07:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-08-04 22:10:47 -07:00
|
|
|
bool
|
|
|
|
SharedSurface_GLTexture::PollSync()
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
if (!mSync) {
|
|
|
|
// We either used glFinish, or we passed this fence already.
|
|
|
|
// (PollSync/WaitSync returned true previously)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
mConsGL->MakeCurrent();
|
|
|
|
MOZ_ASSERT(mConsGL->IsExtensionSupported(GLContext::ARB_sync));
|
|
|
|
|
|
|
|
GLint status = 0;
|
|
|
|
mConsGL->fGetSynciv(mSync,
|
|
|
|
LOCAL_GL_SYNC_STATUS,
|
|
|
|
1,
|
|
|
|
nullptr,
|
|
|
|
&status);
|
|
|
|
if (status != LOCAL_GL_SIGNALED)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
mConsGL->fDeleteSync(mSync);
|
|
|
|
mSync = 0;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-03-10 19:08:50 -07:00
|
|
|
GLuint
|
|
|
|
SharedSurface_GLTexture::ConsTexture(GLContext* consGL)
|
2013-06-04 17:05:28 -07:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
MOZ_ASSERT(consGL);
|
2013-06-16 19:48:41 -07:00
|
|
|
MOZ_ASSERT(mGL->SharesWith(consGL));
|
2014-03-10 19:08:50 -07:00
|
|
|
MOZ_ASSERT_IF(mConsGL, consGL == mConsGL);
|
|
|
|
|
2013-06-04 17:05:28 -07:00
|
|
|
mConsGL = consGL;
|
2014-03-10 19:08:50 -07:00
|
|
|
|
|
|
|
return mTex;
|
2013-06-04 17:05:28 -07:00
|
|
|
}
|
|
|
|
|
2013-02-13 15:26:24 -08:00
|
|
|
} /* namespace gfx */
|
|
|
|
} /* namespace mozilla */
|