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 "SharedSurfaceGL.h"
|
2014-07-12 02:10:49 +04:00
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
#include "GLContext.h"
|
2013-11-29 00:57:19 +04:00
|
|
|
#include "GLBlitHelper.h"
|
2013-11-26 07:25:25 +04:00
|
|
|
#include "ScopedGLHelpers.h"
|
2013-12-10 20:11:58 +04:00
|
|
|
#include "mozilla/gfx/2D.h"
|
2014-01-02 19:17:29 +04:00
|
|
|
#include "GLReadTexImageHelper.h"
|
2013-02-14 03:26:24 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace gl {
|
|
|
|
|
2014-07-12 02:10:49 +04:00
|
|
|
using gfx::IntSize;
|
|
|
|
using gfx::SurfaceFormat;
|
2013-02-14 03:26:24 +04:00
|
|
|
|
|
|
|
SharedSurface_Basic*
|
|
|
|
SharedSurface_Basic::Create(GLContext* gl,
|
|
|
|
const GLFormats& formats,
|
2013-12-10 20:11:58 +04:00
|
|
|
const IntSize& size,
|
2013-02-14 03:26:24 +04:00
|
|
|
bool hasAlpha)
|
|
|
|
{
|
|
|
|
gl->MakeCurrent();
|
2013-12-06 19:36:13 +04:00
|
|
|
GLuint tex = CreateTexture(gl, formats.color_texInternalFormat,
|
|
|
|
formats.color_texFormat,
|
|
|
|
formats.color_texType,
|
|
|
|
size);
|
2013-02-14 03:26:24 +04:00
|
|
|
|
2014-01-10 23:06:16 +04:00
|
|
|
SurfaceFormat format = SurfaceFormat::B8G8R8X8;
|
2013-02-14 03:26:24 +04: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 23:06:16 +04:00
|
|
|
format = SurfaceFormat::R5G6B5;
|
2013-02-14 03:26:24 +04:00
|
|
|
else
|
2014-01-10 23:06:16 +04:00
|
|
|
format = SurfaceFormat::B8G8R8X8;
|
2013-02-14 03:26:24 +04:00
|
|
|
break;
|
|
|
|
case LOCAL_GL_RGBA:
|
|
|
|
case LOCAL_GL_RGBA8:
|
2014-01-10 23:06:16 +04:00
|
|
|
format = SurfaceFormat::B8G8R8A8;
|
2013-02-14 03:26:24 +04:00
|
|
|
break;
|
|
|
|
default:
|
2013-06-29 05:38:30 +04:00
|
|
|
MOZ_CRASH("Unhandled Tex format.");
|
2013-02-14 03:26:24 +04:00
|
|
|
}
|
|
|
|
return new SharedSurface_Basic(gl, size, hasAlpha, format, tex);
|
|
|
|
}
|
|
|
|
|
|
|
|
SharedSurface_Basic::SharedSurface_Basic(GLContext* gl,
|
2013-12-10 20:11:58 +04:00
|
|
|
const IntSize& size,
|
2013-02-14 03:26:24 +04:00
|
|
|
bool hasAlpha,
|
2013-12-10 20:11:58 +04:00
|
|
|
SurfaceFormat format,
|
2013-02-14 03:26:24 +04:00
|
|
|
GLuint tex)
|
2014-07-12 02:10:49 +04:00
|
|
|
: SharedSurface(SharedSurfaceType::Basic,
|
|
|
|
AttachmentType::GLTexture,
|
|
|
|
gl,
|
|
|
|
size,
|
|
|
|
hasAlpha)
|
|
|
|
, mTex(tex)
|
|
|
|
, mFB(0)
|
2013-02-14 03:26:24 +04:00
|
|
|
{
|
2014-03-06 01:49:37 +04: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);
|
|
|
|
|
|
|
|
GLenum status = mGL->fCheckFramebufferStatus(LOCAL_GL_FRAMEBUFFER);
|
|
|
|
if (status != LOCAL_GL_FRAMEBUFFER_COMPLETE) {
|
|
|
|
mGL->fDeleteFramebuffers(1, &mFB);
|
|
|
|
mFB = 0;
|
|
|
|
}
|
|
|
|
|
2014-07-12 02:10:49 +04:00
|
|
|
int32_t stride = gfx::GetAlignedStride<4>(size.width * BytesPerPixel(format));
|
|
|
|
mData = gfx::Factory::CreateDataSourceSurfaceWithStride(size, format, stride);
|
2013-02-14 03:26:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
SharedSurface_Basic::~SharedSurface_Basic()
|
|
|
|
{
|
|
|
|
if (!mGL->MakeCurrent())
|
|
|
|
return;
|
|
|
|
|
2014-03-06 01:49:37 +04:00
|
|
|
if (mFB)
|
|
|
|
mGL->fDeleteFramebuffers(1, &mFB);
|
|
|
|
|
2014-02-14 03:02:03 +04:00
|
|
|
mGL->fDeleteTextures(1, &mTex);
|
2013-02-14 03:26:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SharedSurface_Basic::Fence()
|
|
|
|
{
|
2014-02-21 03:23:52 +04:00
|
|
|
mGL->MakeCurrent();
|
2014-03-06 01:49:37 +04:00
|
|
|
ScopedBindFramebuffer autoFB(mGL, mFB);
|
2014-04-29 03:26:54 +04:00
|
|
|
ReadPixelsIntoDataSurface(mGL, mData);
|
2013-02-14 03:26:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SharedSurface_GLTexture*
|
|
|
|
SharedSurface_GLTexture::Create(GLContext* prodGL,
|
2013-12-10 20:11:58 +04:00
|
|
|
GLContext* consGL,
|
|
|
|
const GLFormats& formats,
|
2014-07-12 02:10:49 +04:00
|
|
|
const IntSize& size,
|
2014-03-06 01:49:37 +04:00
|
|
|
bool hasAlpha,
|
|
|
|
GLuint texture)
|
2013-02-14 03:26:24 +04:00
|
|
|
{
|
2013-06-17 06:48:41 +04:00
|
|
|
MOZ_ASSERT(prodGL);
|
|
|
|
MOZ_ASSERT(!consGL || prodGL->SharesWith(consGL));
|
2013-02-14 03:26:24 +04:00
|
|
|
|
|
|
|
prodGL->MakeCurrent();
|
|
|
|
|
2014-03-06 01:49:37 +04:00
|
|
|
GLuint tex = texture;
|
|
|
|
|
|
|
|
bool ownsTex = false;
|
|
|
|
|
|
|
|
if (!tex) {
|
|
|
|
tex = CreateTextureForOffscreen(prodGL, formats, size);
|
|
|
|
ownsTex = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new SharedSurface_GLTexture(prodGL, consGL, size, hasAlpha, tex, ownsTex);
|
2013-02-14 03:26:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
SharedSurface_GLTexture::~SharedSurface_GLTexture()
|
|
|
|
{
|
|
|
|
if (!mGL->MakeCurrent())
|
|
|
|
return;
|
|
|
|
|
2014-03-06 01:49:37 +04:00
|
|
|
if (mOwnsTex) {
|
|
|
|
mGL->fDeleteTextures(1, &mTex);
|
|
|
|
}
|
2013-02-14 03:26:24 +04:00
|
|
|
|
|
|
|
if (mSync) {
|
|
|
|
mGL->fDeleteSync(mSync);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SharedSurface_GLTexture::Fence()
|
|
|
|
{
|
2013-06-05 04:05:28 +04:00
|
|
|
MutexAutoLock lock(mMutex);
|
2013-02-14 03:26:24 +04:00
|
|
|
mGL->MakeCurrent();
|
|
|
|
|
2013-06-05 04:05:28 +04:00
|
|
|
if (mConsGL && mGL->IsExtensionSupported(GLContext::ARB_sync)) {
|
2013-02-14 03:26:24 +04: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-05 04:05:28 +04:00
|
|
|
MutexAutoLock lock(mMutex);
|
2013-02-14 03:26:24 +04:00
|
|
|
if (!mSync) {
|
2014-08-05 09:10:47 +04:00
|
|
|
// We either used glFinish, or we passed this fence already.
|
|
|
|
// (PollSync/WaitSync returned true previously)
|
2013-02-14 03:26:24 +04:00
|
|
|
return true;
|
|
|
|
}
|
2013-06-05 04:05:28 +04:00
|
|
|
|
2013-03-27 09:29:07 +04:00
|
|
|
mConsGL->MakeCurrent();
|
|
|
|
MOZ_ASSERT(mConsGL->IsExtensionSupported(GLContext::ARB_sync));
|
2013-02-14 03:26:24 +04:00
|
|
|
|
2013-03-27 09:29:07 +04:00
|
|
|
mConsGL->fWaitSync(mSync,
|
|
|
|
0,
|
|
|
|
LOCAL_GL_TIMEOUT_IGNORED);
|
|
|
|
mConsGL->fDeleteSync(mSync);
|
2013-02-14 03:26:24 +04:00
|
|
|
mSync = 0;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-08-05 09:10:47 +04: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-11 06:08:50 +04:00
|
|
|
GLuint
|
|
|
|
SharedSurface_GLTexture::ConsTexture(GLContext* consGL)
|
2013-06-05 04:05:28 +04:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
MOZ_ASSERT(consGL);
|
2013-06-17 06:48:41 +04:00
|
|
|
MOZ_ASSERT(mGL->SharesWith(consGL));
|
2014-03-11 06:08:50 +04:00
|
|
|
MOZ_ASSERT_IF(mConsGL, consGL == mConsGL);
|
|
|
|
|
2013-06-05 04:05:28 +04:00
|
|
|
mConsGL = consGL;
|
2014-03-11 06:08:50 +04:00
|
|
|
|
|
|
|
return mTex;
|
2013-06-05 04:05:28 +04:00
|
|
|
}
|
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
} /* namespace gfx */
|
|
|
|
} /* namespace mozilla */
|