1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "GLContextEGL.h"
8 #include "GLLibraryEGL.h"
13 bool DoesEGLContextSupportSharingWithEGLImage(GLContext
* gl
) {
14 auto* egl
= gl::GLLibraryEGL::Get();
16 return egl
->HasKHRImageBase() && egl
->HasKHRImageTexture2D() &&
17 gl
->IsExtensionSupported(GLContext::OES_EGL_image
);
20 EGLImage
CreateEGLImage(GLContext
* gl
, GLuint tex
) {
21 MOZ_ASSERT(DoesEGLContextSupportSharingWithEGLImage(gl
));
23 auto* egl
= gl::GLLibraryEGL::Get();
25 EGLClientBuffer clientBuffer
= (EGLClientBuffer
)((uint64_t)tex
);
26 EGLContext eglContext
= GLContextEGL::Cast(gl
)->mContext
;
28 egl
->fCreateImage(EGL_DISPLAY(), eglContext
, LOCAL_EGL_GL_TEXTURE_2D
,
29 clientBuffer
, nullptr);
33 ////////////////////////////////////////////////////////////////////////
37 EGLImageWrapper
* EGLImageWrapper::Create(GLContext
* gl
, GLuint tex
) {
38 MOZ_ASSERT(DoesEGLContextSupportSharingWithEGLImage(gl
));
40 auto* egl
= gl::GLLibraryEGL::Get();
42 EGLDisplay display
= EGL_DISPLAY();
43 EGLContext eglContext
= GLContextEGL::Cast(gl
)->mContext
;
44 EGLClientBuffer clientBuffer
= (EGLClientBuffer
)((uint64_t)tex
);
45 EGLImage image
= egl
->fCreateImage(
46 display
, eglContext
, LOCAL_EGL_GL_TEXTURE_2D
, clientBuffer
, nullptr);
49 printf_stderr("Could not create EGL images: ERROR (0x%04x)\n",
55 return new EGLImageWrapper(egl
, display
, image
);
58 EGLImageWrapper::EGLImageWrapper(GLLibraryEGL
* library
, EGLDisplay display
,
60 : mLibrary(library
), mDisplay(display
), mImage(image
), mSync(0) {
64 EGLImageWrapper::~EGLImageWrapper() {
65 mLibrary
->fDestroyImage(mDisplay
, mImage
);
68 bool EGLImageWrapper::FenceSync(GLContext
* gl
) {
71 if (mLibrary
->IsExtensionSupported(GLLibraryEGL::KHR_fence_sync
)) {
72 mSync
= mLibrary
->fCreateSync(mDisplay
, LOCAL_EGL_SYNC_FENCE
, nullptr);
73 // We need to flush to make sure the sync object enters the command stream;
74 // we can't use EGL_SYNC_FLUSH_COMMANDS_BIT at wait time, because the wait
75 // happens on a different thread/context.
80 // we failed to create one, so just do a finish
87 bool EGLImageWrapper::ClientWaitSync() {
89 // if we have no sync object, then we did a Finish() earlier
93 // wait at most 1 second; this should really be never/rarely hit
94 const uint64_t ns_per_ms
= 1000 * 1000;
95 EGLTime timeout
= 1000 * ns_per_ms
;
97 EGLint result
= mLibrary
->fClientWaitSync(mDisplay
, mSync
, 0, timeout
);
98 mLibrary
->fDestroySync(mDisplay
, mSync
);
101 return result
== LOCAL_EGL_CONDITION_SATISFIED
;
105 } // namespace mozilla