2014-09-17 17:13:29 +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 "EGLUtils.h"
|
|
|
|
|
|
|
|
#include "GLContextEGL.h"
|
2018-06-04 05:09:29 +03:00
|
|
|
#include "GLLibraryEGL.h"
|
2014-09-17 17:13:29 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace gl {
|
|
|
|
|
|
|
|
bool DoesEGLContextSupportSharingWithEGLImage(GLContext* gl) {
|
2019-06-15 08:21:12 +03:00
|
|
|
const auto& gle = GLContextEGL::Cast(gl);
|
|
|
|
const auto& egl = gle->mEgl;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-06-04 05:09:29 +03:00
|
|
|
return egl->HasKHRImageBase() && egl->HasKHRImageTexture2D() &&
|
2014-09-17 17:13:29 +04:00
|
|
|
gl->IsExtensionSupported(GLContext::OES_EGL_image);
|
|
|
|
}
|
|
|
|
|
|
|
|
EGLImage CreateEGLImage(GLContext* gl, GLuint tex) {
|
|
|
|
MOZ_ASSERT(DoesEGLContextSupportSharingWithEGLImage(gl));
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2019-06-15 08:21:12 +03:00
|
|
|
const auto& gle = GLContextEGL::Cast(gl);
|
|
|
|
const auto& egl = gle->mEgl;
|
2014-09-17 17:13:29 +04:00
|
|
|
EGLClientBuffer clientBuffer = (EGLClientBuffer)((uint64_t)tex);
|
2018-06-04 05:09:29 +03:00
|
|
|
EGLImage image =
|
2019-06-15 08:21:12 +03:00
|
|
|
egl->fCreateImage(egl->Display(), gle->mContext, LOCAL_EGL_GL_TEXTURE_2D,
|
2018-06-04 05:09:29 +03:00
|
|
|
clientBuffer, nullptr);
|
2014-09-17 17:13:29 +04:00
|
|
|
return image;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// EGLImageWrapper
|
|
|
|
|
2019-02-26 01:07:19 +03:00
|
|
|
/*static*/
|
|
|
|
EGLImageWrapper* EGLImageWrapper::Create(GLContext* gl, GLuint tex) {
|
2014-09-17 17:13:29 +04:00
|
|
|
MOZ_ASSERT(DoesEGLContextSupportSharingWithEGLImage(gl));
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2019-06-15 08:21:12 +03:00
|
|
|
const auto& gle = GLContextEGL::Cast(gl);
|
|
|
|
const auto& egl = gle->mEgl;
|
2019-06-15 08:14:53 +03:00
|
|
|
const auto& display = egl->Display();
|
2014-09-17 17:13:29 +04:00
|
|
|
EGLClientBuffer clientBuffer = (EGLClientBuffer)((uint64_t)tex);
|
2018-06-04 05:09:29 +03:00
|
|
|
EGLImage image = egl->fCreateImage(
|
2019-06-15 08:21:12 +03:00
|
|
|
display, gle->mContext, LOCAL_EGL_GL_TEXTURE_2D, clientBuffer, nullptr);
|
2014-09-17 17:13:29 +04:00
|
|
|
if (!image) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf_stderr("Could not create EGL images: ERROR (0x%04x)\n",
|
2018-06-04 05:09:29 +03:00
|
|
|
egl->fGetError());
|
2014-09-17 17:13:29 +04:00
|
|
|
#endif
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-06-04 05:09:29 +03:00
|
|
|
return new EGLImageWrapper(egl, display, image);
|
|
|
|
}
|
|
|
|
|
|
|
|
EGLImageWrapper::EGLImageWrapper(GLLibraryEGL* library, EGLDisplay display,
|
|
|
|
EGLImage image)
|
|
|
|
: mLibrary(library), mDisplay(display), mImage(image), mSync(0) {
|
|
|
|
MOZ_ASSERT(mImage);
|
2014-09-17 17:13:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
EGLImageWrapper::~EGLImageWrapper() {
|
2018-06-04 05:09:29 +03:00
|
|
|
mLibrary->fDestroyImage(mDisplay, mImage);
|
2014-09-17 17:13:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool EGLImageWrapper::FenceSync(GLContext* gl) {
|
|
|
|
MOZ_ASSERT(!mSync);
|
|
|
|
|
2018-06-04 05:09:29 +03:00
|
|
|
if (mLibrary->IsExtensionSupported(GLLibraryEGL::KHR_fence_sync)) {
|
|
|
|
mSync = mLibrary->fCreateSync(mDisplay, LOCAL_EGL_SYNC_FENCE, nullptr);
|
2014-09-17 17:13:29 +04:00
|
|
|
// We need to flush to make sure the sync object enters the command stream;
|
|
|
|
// we can't use EGL_SYNC_FLUSH_COMMANDS_BIT at wait time, because the wait
|
|
|
|
// happens on a different thread/context.
|
|
|
|
gl->fFlush();
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2014-09-17 17:13:29 +04:00
|
|
|
if (!mSync) {
|
|
|
|
// we failed to create one, so just do a finish
|
|
|
|
gl->fFinish();
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2014-09-17 17:13:29 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EGLImageWrapper::ClientWaitSync() {
|
|
|
|
if (!mSync) {
|
|
|
|
// if we have no sync object, then we did a Finish() earlier
|
|
|
|
return true;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-09-17 17:13:29 +04:00
|
|
|
// wait at most 1 second; this should really be never/rarely hit
|
|
|
|
const uint64_t ns_per_ms = 1000 * 1000;
|
|
|
|
EGLTime timeout = 1000 * ns_per_ms;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-06-04 05:09:29 +03:00
|
|
|
EGLint result = mLibrary->fClientWaitSync(mDisplay, mSync, 0, timeout);
|
|
|
|
mLibrary->fDestroySync(mDisplay, mSync);
|
2014-09-17 17:13:29 +04:00
|
|
|
mSync = nullptr;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-09-17 17:13:29 +04:00
|
|
|
return result == LOCAL_EGL_CONDITION_SATISFIED;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace gl
|
|
|
|
} // namespace mozilla
|