2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 4; -*- */
|
2013-02-14 03:26:24 +04:00
|
|
|
/* 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"
|
2020-06-15 21:25:55 +03:00
|
|
|
#include "MozFramebuffer.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
|
|
|
|
2020-05-15 20:05:59 +03:00
|
|
|
#if defined(MOZ_WIDGET_ANDROID)
|
2020-06-24 21:59:14 +03:00
|
|
|
# include "AndroidNativeWindow.h"
|
2020-05-15 20:05:59 +03:00
|
|
|
# include "mozilla/java/SurfaceAllocatorWrappers.h"
|
2020-07-09 21:31:12 +03:00
|
|
|
# include "mozilla/java/GeckoSurfaceTextureWrappers.h"
|
2020-05-15 20:05:59 +03:00
|
|
|
#endif // defined(MOZ_WIDGET_ANDROID)
|
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace gl {
|
|
|
|
|
2020-06-15 21:25:55 +03:00
|
|
|
static bool HasEglImageExtensions(const GLContextEGL& gl) {
|
|
|
|
const auto& egl = *(gl.mEgl);
|
|
|
|
return egl.HasKHRImageBase() &&
|
2020-08-07 10:14:46 +03:00
|
|
|
egl.IsExtensionSupported(EGLExtension::KHR_gl_texture_2D_image) &&
|
2020-06-15 21:25:55 +03:00
|
|
|
(gl.IsExtensionSupported(GLContext::OES_EGL_image_external) ||
|
|
|
|
gl.IsExtensionSupported(GLContext::OES_EGL_image));
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:07:19 +03:00
|
|
|
/*static*/
|
2020-06-15 21:25:55 +03:00
|
|
|
UniquePtr<SurfaceFactory_EGLImage> SurfaceFactory_EGLImage::Create(
|
|
|
|
GLContext& gl_) {
|
|
|
|
auto& gl = *GLContextEGL::Cast(&gl_);
|
|
|
|
if (!HasEglImageExtensions(gl)) return nullptr;
|
2013-02-14 03:26:24 +04:00
|
|
|
|
2020-06-15 21:25:55 +03:00
|
|
|
const auto partialDesc = PartialSharedSurfaceDesc{
|
|
|
|
&gl, SharedSurfaceType::EGLImageShare, layers::TextureType::EGLImage,
|
|
|
|
false, // Can't recycle, as mSync changes never update TextureHost.
|
|
|
|
};
|
|
|
|
return AsUnique(new SurfaceFactory_EGLImage(partialDesc));
|
|
|
|
}
|
2014-03-06 01:48:58 +04:00
|
|
|
|
2020-06-15 21:25:55 +03:00
|
|
|
// -
|
2013-02-14 03:26:24 +04:00
|
|
|
|
2020-06-15 21:25:55 +03:00
|
|
|
/*static*/
|
|
|
|
UniquePtr<SharedSurface_EGLImage> SharedSurface_EGLImage::Create(
|
|
|
|
const SharedSurfaceDesc& desc) {
|
|
|
|
const auto& gle = GLContextEGL::Cast(desc.gl);
|
|
|
|
const auto& context = gle->mContext;
|
|
|
|
const auto& egl = *(gle->mEgl);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2020-06-15 21:25:55 +03:00
|
|
|
auto fb = MozFramebuffer::Create(desc.gl, desc.size, 0, false);
|
|
|
|
if (!fb) return nullptr;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2020-06-15 21:25:55 +03:00
|
|
|
const auto buffer = reinterpret_cast<EGLClientBuffer>(fb->ColorTex());
|
2020-08-07 10:14:46 +03:00
|
|
|
const auto image =
|
|
|
|
egl.fCreateImage(context, LOCAL_EGL_GL_TEXTURE_2D, buffer, nullptr);
|
2020-06-15 21:25:55 +03:00
|
|
|
if (!image) return nullptr;
|
2013-02-14 03:26:24 +04:00
|
|
|
|
2020-06-15 21:25:55 +03:00
|
|
|
return AsUnique(new SharedSurface_EGLImage(desc, std::move(fb), image));
|
2013-02-14 03:26:24 +04:00
|
|
|
}
|
|
|
|
|
2020-06-15 21:25:55 +03:00
|
|
|
SharedSurface_EGLImage::SharedSurface_EGLImage(const SharedSurfaceDesc& desc,
|
|
|
|
UniquePtr<MozFramebuffer>&& fb,
|
|
|
|
const EGLImage image)
|
|
|
|
: SharedSurface(desc, std::move(fb)),
|
2013-09-07 06:13:37 +04:00
|
|
|
mMutex("SharedSurface_EGLImage mutex"),
|
2020-06-15 21:25:55 +03:00
|
|
|
mImage(image) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
SharedSurface_EGLImage::~SharedSurface_EGLImage() {
|
2020-06-15 21:25:55 +03:00
|
|
|
const auto& gle = GLContextEGL::Cast(mDesc.gl);
|
2019-06-15 08:21:12 +03:00
|
|
|
const auto& egl = gle->mEgl;
|
2020-08-07 10:14:46 +03:00
|
|
|
egl->fDestroyImage(mImage);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
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.
|
2020-08-07 10:14:46 +03:00
|
|
|
egl->fDestroySync(mSync);
|
2016-04-14 23:50:04 +03:00
|
|
|
mSync = 0;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-06 02:57:44 +03:00
|
|
|
void SharedSurface_EGLImage::ProducerReleaseImpl() {
|
2020-06-15 21:25:55 +03:00
|
|
|
const auto& gl = GLContextEGL::Cast(mDesc.gl);
|
|
|
|
const auto& egl = gl->mEgl;
|
2019-06-15 08:21:12 +03:00
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
MutexAutoLock lock(mMutex);
|
2020-06-15 21:25:55 +03:00
|
|
|
gl->MakeCurrent();
|
2013-02-14 03:26:24 +04:00
|
|
|
|
2020-08-07 10:14:46 +03:00
|
|
|
if (egl->IsExtensionSupported(EGLExtension::KHR_fence_sync) &&
|
2020-06-15 21:25:55 +03:00
|
|
|
gl->IsExtensionSupported(GLContext::OES_EGL_sync)) {
|
2016-04-14 23:50:04 +03:00
|
|
|
if (mSync) {
|
|
|
|
MOZ_RELEASE_ASSERT(false, "GFX: Non-recycleable should not Fence twice.");
|
2020-08-07 10:14:46 +03:00
|
|
|
MOZ_ALWAYS_TRUE(egl->fDestroySync(mSync));
|
2016-04-14 23:50:04 +03:00
|
|
|
mSync = 0;
|
|
|
|
}
|
2013-02-14 03:26:24 +04:00
|
|
|
|
2020-08-07 10:14:46 +03:00
|
|
|
mSync = egl->fCreateSync(LOCAL_EGL_SYNC_FENCE, nullptr);
|
2013-02-14 03:26:24 +04:00
|
|
|
if (mSync) {
|
2020-06-15 21:25:55 +03:00
|
|
|
gl->fFlush();
|
2013-02-14 03:26:24 +04:00
|
|
|
return;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2013-02-14 03:26:24 +04:00
|
|
|
|
|
|
|
MOZ_ASSERT(!mSync);
|
2020-06-15 21:25:55 +03:00
|
|
|
gl->fFinish();
|
2013-02-14 03:26:24 +04:00
|
|
|
}
|
|
|
|
|
2016-01-29 19:44:57 +03:00
|
|
|
void SharedSurface_EGLImage::ProducerReadAcquireImpl() {
|
2020-06-15 21:25:55 +03:00
|
|
|
const auto& gle = GLContextEGL::Cast(mDesc.gl);
|
2019-06-15 08:21:12 +03:00
|
|
|
const auto& egl = gle->mEgl;
|
2016-01-29 19:44:57 +03:00
|
|
|
// Wait on the fence, because presumably we're going to want to read this
|
|
|
|
// surface
|
|
|
|
if (mSync) {
|
2020-08-07 10:14:46 +03:00
|
|
|
egl->fClientWaitSync(mSync, 0, LOCAL_EGL_FOREVER);
|
2016-01-29 19:44:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-15 21:25:55 +03:00
|
|
|
Maybe<layers::SurfaceDescriptor> SharedSurface_EGLImage::ToSurfaceDescriptor() {
|
|
|
|
return Some(layers::EGLImageDescriptor((uintptr_t)mImage, (uintptr_t)mSync,
|
|
|
|
mDesc.size, true));
|
2015-06-05 03:15:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
2013-02-14 03:26:24 +04:00
|
|
|
|
2017-03-04 00:14:27 +03:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
|
|
|
|
2019-02-26 01:07:19 +03:00
|
|
|
/*static*/
|
|
|
|
UniquePtr<SharedSurface_SurfaceTexture> SharedSurface_SurfaceTexture::Create(
|
2020-06-15 21:25:55 +03:00
|
|
|
const SharedSurfaceDesc& desc) {
|
|
|
|
const auto& size = desc.size;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2020-09-02 03:13:15 +03:00
|
|
|
jni::Object::LocalRef surfaceObj;
|
|
|
|
const bool useSingleBuffer =
|
|
|
|
desc.gl->Renderer() != GLRenderer::AndroidEmulator;
|
|
|
|
|
|
|
|
if (useSingleBuffer) {
|
|
|
|
surfaceObj =
|
|
|
|
java::SurfaceAllocator::AcquireSurface(size.width, size.height, true);
|
|
|
|
}
|
|
|
|
|
2020-06-15 21:25:55 +03:00
|
|
|
if (!surfaceObj) {
|
|
|
|
// Try multi-buffer mode
|
|
|
|
surfaceObj =
|
|
|
|
java::SurfaceAllocator::AcquireSurface(size.width, size.height, false);
|
|
|
|
}
|
2020-09-02 03:13:15 +03:00
|
|
|
|
2020-06-15 21:25:55 +03:00
|
|
|
if (!surfaceObj) {
|
|
|
|
// Give up
|
|
|
|
NS_WARNING("Failed to allocate SurfaceTexture!");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
const auto surface = java::GeckoSurface::Ref::From(surfaceObj);
|
2017-03-04 00:14:27 +03:00
|
|
|
|
|
|
|
AndroidNativeWindow window(surface);
|
2020-06-15 21:25:55 +03:00
|
|
|
const auto& gle = GLContextEGL::Cast(desc.gl);
|
2019-06-15 08:21:12 +03:00
|
|
|
MOZ_ASSERT(gle);
|
2020-06-15 21:25:55 +03:00
|
|
|
const auto eglSurface = gle->CreateCompatibleSurface(window.NativeWindow());
|
|
|
|
if (!eglSurface) return nullptr;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2020-06-15 21:25:55 +03:00
|
|
|
return AsUnique(new SharedSurface_SurfaceTexture(desc, surface, eglSurface));
|
2017-03-04 00:14:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
SharedSurface_SurfaceTexture::SharedSurface_SurfaceTexture(
|
2020-06-15 21:25:55 +03:00
|
|
|
const SharedSurfaceDesc& desc, java::GeckoSurface::Param surface,
|
|
|
|
const EGLSurface eglSurface)
|
|
|
|
: SharedSurface(desc, nullptr),
|
2017-03-04 00:14:27 +03:00
|
|
|
mSurface(surface),
|
2020-09-01 16:00:39 +03:00
|
|
|
mEglSurface(eglSurface),
|
|
|
|
mEglDisplay(GLContextEGL::Cast(desc.gl)->mEgl) {}
|
2017-03-04 00:14:27 +03:00
|
|
|
|
|
|
|
SharedSurface_SurfaceTexture::~SharedSurface_SurfaceTexture() {
|
2020-05-15 01:50:02 +03:00
|
|
|
if (mOrigEglSurface) {
|
|
|
|
// We are about to destroy mEglSurface.
|
|
|
|
// Make sure gl->SetEGLSurfaceOverride() doesn't keep a reference
|
|
|
|
// to the surface.
|
|
|
|
UnlockProd();
|
|
|
|
}
|
2020-09-01 16:00:39 +03:00
|
|
|
|
|
|
|
std::shared_ptr<EglDisplay> display = mEglDisplay.lock();
|
|
|
|
if (display) {
|
|
|
|
display->fDestroySurface(mEglSurface);
|
|
|
|
}
|
2017-03-04 00:14:27 +03:00
|
|
|
java::SurfaceAllocator::DisposeSurface(mSurface);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SharedSurface_SurfaceTexture::LockProdImpl() {
|
|
|
|
MOZ_RELEASE_ASSERT(mSurface->GetAvailable());
|
|
|
|
|
2020-06-15 21:25:55 +03:00
|
|
|
GLContextEGL* gl = GLContextEGL::Cast(mDesc.gl);
|
2017-03-04 00:14:27 +03:00
|
|
|
mOrigEglSurface = gl->GetEGLSurfaceOverride();
|
|
|
|
gl->SetEGLSurfaceOverride(mEglSurface);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SharedSurface_SurfaceTexture::UnlockProdImpl() {
|
|
|
|
MOZ_RELEASE_ASSERT(mSurface->GetAvailable());
|
|
|
|
|
2020-06-15 21:25:55 +03:00
|
|
|
GLContextEGL* gl = GLContextEGL::Cast(mDesc.gl);
|
2017-03-04 00:14:27 +03:00
|
|
|
MOZ_ASSERT(gl->GetEGLSurfaceOverride() == mEglSurface);
|
|
|
|
|
|
|
|
gl->SetEGLSurfaceOverride(mOrigEglSurface);
|
|
|
|
mOrigEglSurface = nullptr;
|
|
|
|
}
|
|
|
|
|
2020-07-09 21:31:12 +03:00
|
|
|
void SharedSurface_SurfaceTexture::ProducerReadReleaseImpl() {
|
2020-09-10 20:19:15 +03:00
|
|
|
// This GeckoSurfaceTexture is not SurfaceTexture of this class's GeckoSurface
|
|
|
|
// when current process is content process. In this case, SurfaceTexture of
|
|
|
|
// this class's GeckoSurface does not exist in this process. It exists in
|
|
|
|
// compositor's process. Then GeckoSurfaceTexture in this process is a sync
|
|
|
|
// surface that copies back the SurfaceTextrure from compositor's process. It
|
|
|
|
// was added by Bug 1486659. Then SurfaceTexture::UpdateTexImage() becomes
|
|
|
|
// very heavy weight, since it does copy back the SurfaceTextrure from
|
|
|
|
// compositor's process.
|
2020-07-09 21:31:12 +03:00
|
|
|
java::GeckoSurfaceTexture::LocalRef surfaceTexture =
|
|
|
|
java::GeckoSurfaceTexture::Lookup(mSurface->GetHandle());
|
|
|
|
if (!surfaceTexture) {
|
|
|
|
NS_ERROR("Didn't find GeckoSurfaceTexture in ProducerReadReleaseImpl");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
surfaceTexture->UpdateTexImage();
|
2020-09-10 20:19:15 +03:00
|
|
|
// Non single buffer mode Surface does not need ReleaseTexImage() call.
|
|
|
|
// When SurfaceTexture is sync Surface, it might not be single buffer mode.
|
|
|
|
if (surfaceTexture->IsSingleBuffer()) {
|
|
|
|
surfaceTexture->ReleaseTexImage();
|
|
|
|
}
|
2020-07-09 21:31:12 +03:00
|
|
|
}
|
|
|
|
|
2017-03-04 00:14:27 +03:00
|
|
|
void SharedSurface_SurfaceTexture::Commit() {
|
|
|
|
MOZ_RELEASE_ASSERT(mSurface->GetAvailable());
|
|
|
|
|
|
|
|
LockProdImpl();
|
2020-06-15 21:25:55 +03:00
|
|
|
mDesc.gl->SwapBuffers();
|
2017-03-04 00:14:27 +03:00
|
|
|
UnlockProdImpl();
|
|
|
|
mSurface->SetAvailable(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SharedSurface_SurfaceTexture::WaitForBufferOwnership() {
|
|
|
|
mSurface->SetAvailable(true);
|
|
|
|
}
|
|
|
|
|
2018-07-28 13:33:55 +03:00
|
|
|
bool SharedSurface_SurfaceTexture::IsBufferAvailable() const {
|
|
|
|
return mSurface->GetAvailable();
|
|
|
|
}
|
|
|
|
|
2020-06-15 21:25:55 +03:00
|
|
|
Maybe<layers::SurfaceDescriptor>
|
|
|
|
SharedSurface_SurfaceTexture::ToSurfaceDescriptor() {
|
|
|
|
return Some(layers::SurfaceTextureDescriptor(
|
|
|
|
mSurface->GetHandle(), mDesc.size, gfx::SurfaceFormat::R8G8B8A8,
|
|
|
|
false /* NOT continuous */, false /* Do not ignore transform */));
|
2017-03-04 00:14:27 +03:00
|
|
|
}
|
|
|
|
|
2020-06-15 21:25:55 +03:00
|
|
|
SurfaceFactory_SurfaceTexture::SurfaceFactory_SurfaceTexture(GLContext& gl)
|
|
|
|
: SurfaceFactory({&gl, SharedSurfaceType::AndroidSurfaceTexture,
|
|
|
|
layers::TextureType::AndroidNativeWindow, true}) {}
|
2017-03-04 00:14:27 +03:00
|
|
|
|
|
|
|
#endif // MOZ_WIDGET_ANDROID
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace gl
|
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
} /* namespace mozilla */
|