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 "SharedSurfaceEGL.h"
|
2014-07-12 02:10:49 +04:00
|
|
|
|
2013-11-29 00:57:19 +04:00
|
|
|
#include "GLBlitHelper.h"
|
2014-07-12 02:10:49 +04:00
|
|
|
#include "GLContextEGL.h"
|
2017-03-04 00:14:27 +03:00
|
|
|
#include "GLContextProvider.h"
|
2013-02-14 03:26:24 +04:00
|
|
|
#include "GLLibraryEGL.h"
|
2014-01-02 19:17:29 +04:00
|
|
|
#include "GLReadTexImageHelper.h"
|
2015-06-05 03:15:38 +03:00
|
|
|
#include "mozilla/layers/LayersSurfaces.h" // for SurfaceDescriptor, etc
|
2014-07-12 02:10:49 +04:00
|
|
|
#include "SharedSurface.h"
|
2013-02-14 03:26:24 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace gl {
|
|
|
|
|
2014-08-16 04:38:08 +04:00
|
|
|
/*static*/ UniquePtr<SharedSurface_EGLImage>
|
2013-02-14 03:26:24 +04:00
|
|
|
SharedSurface_EGLImage::Create(GLContext* prodGL,
|
|
|
|
const GLFormats& formats,
|
2013-12-10 20:11:58 +04:00
|
|
|
const gfx::IntSize& size,
|
2013-02-14 03:26:24 +04:00
|
|
|
bool hasAlpha,
|
|
|
|
EGLContext context)
|
|
|
|
{
|
2018-06-04 05:09:29 +03:00
|
|
|
auto* egl = gl::GLLibraryEGL::Get();
|
2013-02-14 03:26:24 +04:00
|
|
|
MOZ_ASSERT(egl);
|
2014-03-06 01:48:58 +04:00
|
|
|
MOZ_ASSERT(context);
|
2013-02-14 03:26:24 +04:00
|
|
|
|
2014-08-16 04:38:08 +04:00
|
|
|
UniquePtr<SharedSurface_EGLImage> ret;
|
|
|
|
|
2014-03-06 01:48:58 +04:00
|
|
|
if (!HasExtensions(egl, prodGL)) {
|
2018-06-01 18:59:07 +03:00
|
|
|
return ret;
|
2014-03-06 01:48:58 +04:00
|
|
|
}
|
2013-02-14 03:26:24 +04:00
|
|
|
|
|
|
|
MOZ_ALWAYS_TRUE(prodGL->MakeCurrent());
|
2013-12-06 19:36:13 +04:00
|
|
|
GLuint prodTex = CreateTextureForOffscreen(prodGL, formats, size);
|
2014-03-06 01:48:58 +04:00
|
|
|
if (!prodTex) {
|
2018-06-01 18:59:07 +03:00
|
|
|
return ret;
|
2014-03-06 01:48:58 +04:00
|
|
|
}
|
|
|
|
|
2016-03-21 20:49:23 +03:00
|
|
|
EGLClientBuffer buffer = reinterpret_cast<EGLClientBuffer>(uintptr_t(prodTex));
|
2014-03-06 01:48:58 +04:00
|
|
|
EGLImage image = egl->fCreateImage(egl->Display(), context,
|
|
|
|
LOCAL_EGL_GL_TEXTURE_2D, buffer,
|
|
|
|
nullptr);
|
|
|
|
if (!image) {
|
|
|
|
prodGL->fDeleteTextures(1, &prodTex);
|
2018-06-01 18:59:07 +03:00
|
|
|
return ret;
|
2014-03-06 01:48:58 +04:00
|
|
|
}
|
2013-02-14 03:26:24 +04:00
|
|
|
|
2014-08-16 04:38:08 +04:00
|
|
|
ret.reset( new SharedSurface_EGLImage(prodGL, egl, size, hasAlpha,
|
|
|
|
formats, prodTex, image) );
|
2018-06-01 18:59:07 +03:00
|
|
|
return ret;
|
2013-02-14 03:26:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
SharedSurface_EGLImage::HasExtensions(GLLibraryEGL* egl, GLContext* gl)
|
|
|
|
{
|
|
|
|
return egl->HasKHRImageBase() &&
|
|
|
|
egl->IsExtensionSupported(GLLibraryEGL::KHR_gl_texture_2D_image) &&
|
2016-07-13 13:16:54 +03:00
|
|
|
(gl->IsExtensionSupported(GLContext::OES_EGL_image_external) ||
|
|
|
|
gl->IsExtensionSupported(GLContext::OES_EGL_image));
|
2013-02-14 03:26:24 +04:00
|
|
|
}
|
|
|
|
|
2013-09-07 06:13:37 +04:00
|
|
|
SharedSurface_EGLImage::SharedSurface_EGLImage(GLContext* gl,
|
|
|
|
GLLibraryEGL* egl,
|
2013-12-10 20:11:58 +04:00
|
|
|
const gfx::IntSize& size,
|
2013-09-07 06:13:37 +04:00
|
|
|
bool hasAlpha,
|
|
|
|
const GLFormats& formats,
|
2014-03-06 01:48:58 +04:00
|
|
|
GLuint prodTex,
|
|
|
|
EGLImage image)
|
2014-07-12 02:10:49 +04:00
|
|
|
: SharedSurface(SharedSurfaceType::EGLImageShare,
|
|
|
|
AttachmentType::GLTexture,
|
|
|
|
gl,
|
|
|
|
size,
|
2015-06-05 03:15:38 +03:00
|
|
|
hasAlpha,
|
|
|
|
false) // Can't recycle, as mSync changes never update TextureHost.
|
2013-09-07 06:13:37 +04:00
|
|
|
, mMutex("SharedSurface_EGLImage mutex")
|
|
|
|
, mEGL(egl)
|
|
|
|
, mFormats(formats)
|
|
|
|
, mProdTex(prodTex)
|
2014-03-06 01:48:58 +04:00
|
|
|
, mImage(image)
|
2013-09-07 06:13:37 +04:00
|
|
|
, mSync(0)
|
|
|
|
{}
|
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
SharedSurface_EGLImage::~SharedSurface_EGLImage()
|
|
|
|
{
|
|
|
|
mEGL->fDestroyImage(Display(), mImage);
|
|
|
|
|
2016-04-14 23:50:04 +03:00
|
|
|
if (mSync) {
|
|
|
|
// We can't call this unless we have the ext, but we will always have
|
|
|
|
// the ext if we have something to destroy.
|
|
|
|
mEGL->fDestroySync(Display(), mSync);
|
|
|
|
mSync = 0;
|
|
|
|
}
|
2013-02-14 03:26:24 +04:00
|
|
|
|
2016-10-24 18:35:31 +03:00
|
|
|
if (!mGL || !mGL->MakeCurrent())
|
2016-04-14 23:50:04 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
mGL->fDeleteTextures(1, &mProdTex);
|
|
|
|
mProdTex = 0;
|
2013-02-14 03:26:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-01-06 02:57:44 +03:00
|
|
|
SharedSurface_EGLImage::ProducerReleaseImpl()
|
2013-02-14 03:26:24 +04:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
mGL->MakeCurrent();
|
|
|
|
|
|
|
|
if (mEGL->IsExtensionSupported(GLLibraryEGL::KHR_fence_sync) &&
|
|
|
|
mGL->IsExtensionSupported(GLContext::OES_EGL_sync))
|
|
|
|
{
|
|
|
|
if (mSync) {
|
2016-06-06 18:17:23 +03:00
|
|
|
MOZ_RELEASE_ASSERT(false, "GFX: Non-recycleable should not Fence twice.");
|
2013-02-14 03:26:24 +04:00
|
|
|
MOZ_ALWAYS_TRUE( mEGL->fDestroySync(Display(), mSync) );
|
|
|
|
mSync = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
mSync = mEGL->fCreateSync(Display(),
|
|
|
|
LOCAL_EGL_SYNC_FENCE,
|
|
|
|
nullptr);
|
|
|
|
if (mSync) {
|
|
|
|
mGL->fFlush();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(!mSync);
|
|
|
|
mGL->fFinish();
|
|
|
|
}
|
|
|
|
|
2016-01-29 19:44:57 +03:00
|
|
|
void
|
|
|
|
SharedSurface_EGLImage::ProducerReadAcquireImpl()
|
|
|
|
{
|
|
|
|
// Wait on the fence, because presumably we're going to want to read this surface
|
|
|
|
if (mSync) {
|
|
|
|
mEGL->fClientWaitSync(Display(), mSync, 0, LOCAL_EGL_FOREVER);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
EGLDisplay
|
|
|
|
SharedSurface_EGLImage::Display() const
|
|
|
|
{
|
|
|
|
return mEGL->Display();
|
|
|
|
}
|
|
|
|
|
2015-06-05 03:15:38 +03:00
|
|
|
bool
|
|
|
|
SharedSurface_EGLImage::ToSurfaceDescriptor(layers::SurfaceDescriptor* const out_descriptor)
|
|
|
|
{
|
|
|
|
*out_descriptor = layers::EGLImageDescriptor((uintptr_t)mImage, (uintptr_t)mSync,
|
|
|
|
mSize, mHasAlpha);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-10-12 06:21:03 +03:00
|
|
|
bool
|
|
|
|
SharedSurface_EGLImage::ReadbackBySharedHandle(gfx::DataSourceSurface* out_surface)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(out_surface);
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2018-06-04 05:09:29 +03:00
|
|
|
auto* egl = gl::GLLibraryEGL::Get();
|
|
|
|
return egl->ReadbackEGLImage(mImage, out_surface);
|
2015-10-12 06:21:03 +03:00
|
|
|
}
|
|
|
|
|
2015-06-05 03:15:38 +03:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
2013-02-14 03:26:24 +04:00
|
|
|
|
2014-08-16 04:38:08 +04:00
|
|
|
/*static*/ UniquePtr<SurfaceFactory_EGLImage>
|
2015-06-05 03:15:38 +03:00
|
|
|
SurfaceFactory_EGLImage::Create(GLContext* prodGL, const SurfaceCaps& caps,
|
2016-09-27 06:22:20 +03:00
|
|
|
const RefPtr<layers::LayersIPCChannel>& allocator,
|
2015-06-05 03:15:38 +03:00
|
|
|
const layers::TextureFlags& flags)
|
2013-02-14 03:26:24 +04:00
|
|
|
{
|
2015-09-24 22:21:05 +03:00
|
|
|
EGLContext context = GLContextEGL::Cast(prodGL)->mContext;
|
2013-02-14 03:26:24 +04:00
|
|
|
|
2014-08-16 04:38:08 +04:00
|
|
|
typedef SurfaceFactory_EGLImage ptrT;
|
|
|
|
UniquePtr<ptrT> ret;
|
|
|
|
|
2018-06-04 05:09:29 +03:00
|
|
|
auto* egl = gl::GLLibraryEGL::Get();
|
2014-08-16 04:38:08 +04:00
|
|
|
if (SharedSurface_EGLImage::HasExtensions(egl, prodGL)) {
|
2015-06-05 03:15:38 +03:00
|
|
|
ret.reset( new ptrT(prodGL, caps, allocator, flags, context) );
|
2014-03-06 01:48:58 +04:00
|
|
|
}
|
|
|
|
|
2018-06-01 18:59:07 +03:00
|
|
|
return ret;
|
2013-02-14 03:26:24 +04:00
|
|
|
}
|
|
|
|
|
2017-03-04 00:14:27 +03:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
|
|
|
|
|
|
|
/*static*/ UniquePtr<SharedSurface_SurfaceTexture>
|
|
|
|
SharedSurface_SurfaceTexture::Create(GLContext* prodGL,
|
|
|
|
const GLFormats& formats,
|
|
|
|
const gfx::IntSize& size,
|
|
|
|
bool hasAlpha,
|
|
|
|
java::GeckoSurface::Param surface)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(surface);
|
|
|
|
|
|
|
|
UniquePtr<SharedSurface_SurfaceTexture> ret;
|
|
|
|
|
|
|
|
AndroidNativeWindow window(surface);
|
2017-06-07 03:26:49 +03:00
|
|
|
GLContextEGL* egl = GLContextEGL::Cast(prodGL);
|
|
|
|
MOZ_ASSERT(egl);
|
|
|
|
EGLSurface eglSurface = egl->CreateCompatibleSurface(window.NativeWindow());
|
2017-03-04 00:14:27 +03:00
|
|
|
if (!eglSurface) {
|
2018-06-01 18:59:07 +03:00
|
|
|
return ret;
|
2017-03-04 00:14:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ret.reset(new SharedSurface_SurfaceTexture(prodGL, size, hasAlpha,
|
|
|
|
formats, surface, eglSurface));
|
2018-06-01 18:59:07 +03:00
|
|
|
return ret;
|
2017-03-04 00:14:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
SharedSurface_SurfaceTexture::SharedSurface_SurfaceTexture(GLContext* gl,
|
|
|
|
const gfx::IntSize& size,
|
|
|
|
bool hasAlpha,
|
|
|
|
const GLFormats& formats,
|
|
|
|
java::GeckoSurface::Param surface,
|
|
|
|
EGLSurface eglSurface)
|
|
|
|
: SharedSurface(SharedSurfaceType::AndroidSurfaceTexture,
|
|
|
|
AttachmentType::Screen,
|
|
|
|
gl,
|
|
|
|
size,
|
|
|
|
hasAlpha,
|
|
|
|
true)
|
|
|
|
, mSurface(surface)
|
|
|
|
, mEglSurface(eglSurface)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SharedSurface_SurfaceTexture::~SharedSurface_SurfaceTexture()
|
|
|
|
{
|
|
|
|
GLContextProviderEGL::DestroyEGLSurface(mEglSurface);
|
|
|
|
java::SurfaceAllocator::DisposeSurface(mSurface);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SharedSurface_SurfaceTexture::LockProdImpl()
|
|
|
|
{
|
|
|
|
MOZ_RELEASE_ASSERT(mSurface->GetAvailable());
|
|
|
|
|
|
|
|
GLContextEGL *gl = GLContextEGL::Cast(mGL);
|
|
|
|
mOrigEglSurface = gl->GetEGLSurfaceOverride();
|
|
|
|
gl->SetEGLSurfaceOverride(mEglSurface);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SharedSurface_SurfaceTexture::UnlockProdImpl()
|
|
|
|
{
|
|
|
|
MOZ_RELEASE_ASSERT(mSurface->GetAvailable());
|
|
|
|
|
|
|
|
GLContextEGL *gl = GLContextEGL::Cast(mGL);
|
|
|
|
MOZ_ASSERT(gl->GetEGLSurfaceOverride() == mEglSurface);
|
|
|
|
|
|
|
|
gl->SetEGLSurfaceOverride(mOrigEglSurface);
|
|
|
|
mOrigEglSurface = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SharedSurface_SurfaceTexture::Commit()
|
|
|
|
{
|
|
|
|
MOZ_RELEASE_ASSERT(mSurface->GetAvailable());
|
|
|
|
|
|
|
|
LockProdImpl();
|
|
|
|
mGL->SwapBuffers();
|
|
|
|
UnlockProdImpl();
|
|
|
|
mSurface->SetAvailable(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SharedSurface_SurfaceTexture::WaitForBufferOwnership()
|
|
|
|
{
|
|
|
|
MOZ_RELEASE_ASSERT(!mSurface->GetAvailable());
|
|
|
|
mSurface->SetAvailable(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
SharedSurface_SurfaceTexture::ToSurfaceDescriptor(layers::SurfaceDescriptor* const out_descriptor)
|
|
|
|
{
|
2017-10-28 13:59:58 +03:00
|
|
|
*out_descriptor =
|
|
|
|
layers::SurfaceTextureDescriptor(mSurface->GetHandle(),
|
|
|
|
mSize,
|
|
|
|
gfx::SurfaceFormat::R8G8B8A8,
|
|
|
|
false /* NOT continuous */,
|
|
|
|
false /* Do not ignore transform */);
|
2017-03-04 00:14:27 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/*static*/ UniquePtr<SurfaceFactory_SurfaceTexture>
|
|
|
|
SurfaceFactory_SurfaceTexture::Create(GLContext* prodGL, const SurfaceCaps& caps,
|
|
|
|
const RefPtr<layers::LayersIPCChannel>& allocator,
|
|
|
|
const layers::TextureFlags& flags)
|
|
|
|
{
|
|
|
|
UniquePtr<SurfaceFactory_SurfaceTexture> ret(
|
|
|
|
new SurfaceFactory_SurfaceTexture(prodGL, caps, allocator, flags));
|
2018-06-01 18:59:07 +03:00
|
|
|
return ret;
|
2017-03-04 00:14:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
UniquePtr<SharedSurface>
|
|
|
|
SurfaceFactory_SurfaceTexture::CreateShared(const gfx::IntSize& size)
|
|
|
|
{
|
|
|
|
bool hasAlpha = mReadCaps.alpha;
|
|
|
|
|
|
|
|
jni::Object::LocalRef surface = java::SurfaceAllocator::AcquireSurface(size.width, size.height, true);
|
|
|
|
if (!surface) {
|
|
|
|
// Try multi-buffer mode
|
|
|
|
surface = java::SurfaceAllocator::AcquireSurface(size.width, size.height, false);
|
|
|
|
if (!surface) {
|
|
|
|
// Give up
|
|
|
|
NS_WARNING("Failed to allocate SurfaceTexture!");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return SharedSurface_SurfaceTexture::Create(mGL, mFormats, size, hasAlpha,
|
|
|
|
java::GeckoSurface::Ref::From(surface));
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // MOZ_WIDGET_ANDROID
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace gl
|
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
} /* namespace mozilla */
|