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-11-29 00:57:19 +04:00
|
|
|
#include "GLBlitHelper.h"
|
2014-09-03 02:16:02 +04:00
|
|
|
#include "GLContext.h"
|
2014-01-02 19:17:29 +04:00
|
|
|
#include "GLReadTexImageHelper.h"
|
2014-09-03 02:16:02 +04:00
|
|
|
#include "mozilla/gfx/2D.h"
|
|
|
|
#include "ScopedGLHelpers.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
|
|
|
|
2014-08-16 04:38:08 +04:00
|
|
|
/*static*/ UniquePtr<SharedSurface_Basic>
|
2013-02-14 03:26:24 +04:00
|
|
|
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)
|
|
|
|
{
|
2014-08-28 03:18:46 +04:00
|
|
|
UniquePtr<SharedSurface_Basic> ret;
|
2013-02-14 03:26:24 +04:00
|
|
|
gl->MakeCurrent();
|
2014-08-28 03:18:43 +04:00
|
|
|
|
2014-10-25 03:52:35 +04:00
|
|
|
GLContext::LocalErrorScope localError(*gl);
|
2015-09-30 02:04:17 +03:00
|
|
|
GLuint tex = CreateTextureForOffscreen(gl, formats, size);
|
2014-10-25 03:52:35 +04:00
|
|
|
|
|
|
|
GLenum err = localError.GetError();
|
|
|
|
MOZ_ASSERT_IF(err != LOCAL_GL_NO_ERROR, err == LOCAL_GL_OUT_OF_MEMORY);
|
2014-08-28 03:18:43 +04:00
|
|
|
if (err) {
|
|
|
|
gl->fDeleteTextures(1, &tex);
|
2014-08-28 03:18:46 +04:00
|
|
|
return Move(ret);
|
2014-08-28 03:18:43 +04:00
|
|
|
}
|
2013-02-14 03:26:24 +04:00
|
|
|
|
2015-06-05 03:15:38 +03:00
|
|
|
bool ownsTex = true;
|
|
|
|
ret.reset( new SharedSurface_Basic(gl, size, hasAlpha, tex, ownsTex) );
|
|
|
|
return Move(ret);
|
|
|
|
}
|
|
|
|
|
2014-08-16 04:38:08 +04:00
|
|
|
|
2015-06-05 03:15:38 +03:00
|
|
|
/*static*/ UniquePtr<SharedSurface_Basic>
|
|
|
|
SharedSurface_Basic::Wrap(GLContext* gl,
|
|
|
|
const IntSize& size,
|
|
|
|
bool hasAlpha,
|
|
|
|
GLuint tex)
|
|
|
|
{
|
|
|
|
bool ownsTex = false;
|
|
|
|
UniquePtr<SharedSurface_Basic> ret( new SharedSurface_Basic(gl, size, hasAlpha, tex,
|
|
|
|
ownsTex) );
|
2014-08-16 04:38:08 +04:00
|
|
|
return Move(ret);
|
2013-02-14 03:26:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2015-06-05 03:15:38 +03:00
|
|
|
GLuint tex,
|
|
|
|
bool ownsTex)
|
2014-07-12 02:10:49 +04:00
|
|
|
: SharedSurface(SharedSurfaceType::Basic,
|
|
|
|
AttachmentType::GLTexture,
|
|
|
|
gl,
|
|
|
|
size,
|
2015-06-05 03:15:38 +03:00
|
|
|
hasAlpha,
|
|
|
|
true)
|
2014-07-12 02:10:49 +04:00
|
|
|
, mTex(tex)
|
2015-06-05 03:15:38 +03:00
|
|
|
, mOwnsTex(ownsTex)
|
2014-07-12 02:10:49 +04:00
|
|
|
, 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);
|
|
|
|
|
2014-08-28 03:18:43 +04:00
|
|
|
DebugOnly<GLenum> status = mGL->fCheckFramebufferStatus(LOCAL_GL_FRAMEBUFFER);
|
|
|
|
MOZ_ASSERT(status == LOCAL_GL_FRAMEBUFFER_COMPLETE);
|
2013-02-14 03:26:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
SharedSurface_Basic::~SharedSurface_Basic()
|
|
|
|
{
|
2016-10-24 18:35:31 +03:00
|
|
|
if (!mGL || !mGL->MakeCurrent())
|
2013-02-14 03:26:24 +04:00
|
|
|
return;
|
|
|
|
|
2014-03-06 01:49:37 +04:00
|
|
|
if (mFB)
|
|
|
|
mGL->fDeleteFramebuffers(1, &mFB);
|
|
|
|
|
2015-06-05 03:15:38 +03:00
|
|
|
if (mOwnsTex)
|
2015-06-05 02:50:59 +03:00
|
|
|
mGL->fDeleteTextures(1, &mTex);
|
|
|
|
}
|
|
|
|
|
2015-09-24 17:11:53 +03:00
|
|
|
|
2015-06-05 03:15:38 +03:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
2015-06-05 02:50:59 +03:00
|
|
|
|
2015-06-05 03:15:38 +03:00
|
|
|
SurfaceFactory_Basic::SurfaceFactory_Basic(GLContext* gl, const SurfaceCaps& caps,
|
|
|
|
const layers::TextureFlags& flags)
|
|
|
|
: SurfaceFactory(SharedSurfaceType::Basic, gl, caps, nullptr, flags)
|
|
|
|
{ }
|
2013-06-05 04:05:28 +04:00
|
|
|
|
2015-09-24 17:11:53 +03:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// SharedSurface_GLTexture
|
|
|
|
|
|
|
|
/*static*/ UniquePtr<SharedSurface_GLTexture>
|
|
|
|
SharedSurface_GLTexture::Create(GLContext* prodGL,
|
|
|
|
const GLFormats& formats,
|
|
|
|
const IntSize& size,
|
|
|
|
bool hasAlpha)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(prodGL);
|
|
|
|
|
|
|
|
prodGL->MakeCurrent();
|
|
|
|
|
|
|
|
UniquePtr<SharedSurface_GLTexture> ret;
|
|
|
|
GLContext::LocalErrorScope localError(*prodGL);
|
|
|
|
|
|
|
|
GLuint tex = CreateTextureForOffscreen(prodGL, formats, size);
|
|
|
|
|
|
|
|
GLenum err = localError.GetError();
|
|
|
|
MOZ_ASSERT_IF(err, err == LOCAL_GL_OUT_OF_MEMORY);
|
|
|
|
if (err) {
|
|
|
|
prodGL->fDeleteTextures(1, &tex);
|
|
|
|
return Move(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret.reset(new SharedSurface_GLTexture(prodGL, size,
|
|
|
|
hasAlpha, tex));
|
|
|
|
return Move(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
SharedSurface_GLTexture::~SharedSurface_GLTexture()
|
|
|
|
{
|
|
|
|
if (!mGL->MakeCurrent())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (mTex) {
|
|
|
|
mGL->fDeleteTextures(1, &mTex);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mSync) {
|
|
|
|
mGL->fDeleteSync(mSync);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SharedSurface_GLTexture::ProducerReleaseImpl()
|
|
|
|
{
|
|
|
|
mGL->MakeCurrent();
|
|
|
|
|
2015-09-28 17:41:08 +03:00
|
|
|
if (mGL->IsSupported(GLFeature::sync)) {
|
2015-09-24 17:11:53 +03: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::ToSurfaceDescriptor(layers::SurfaceDescriptor* const out_descriptor)
|
|
|
|
{
|
|
|
|
*out_descriptor = layers::SurfaceDescriptorSharedGLTexture(ProdTexture(),
|
|
|
|
ProdTextureTarget(),
|
|
|
|
(uintptr_t)mSync,
|
|
|
|
mSize,
|
|
|
|
mHasAlpha);
|
|
|
|
|
|
|
|
// Transfer ownership of the fence to the host
|
|
|
|
mSync = nullptr;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace gl
|
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
} /* namespace mozilla */
|