Bug 1508381 - remove now-unnecessary TASKCLUSTER_* variables r=tomprince
[gecko.git] / gfx / gl / EGLUtils.cpp
blob78e9b261dcdad20ed232af2cb490948542c88c04
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/. */
5 #include "EGLUtils.h"
7 #include "GLContextEGL.h"
8 #include "GLLibraryEGL.h"
10 namespace mozilla {
11 namespace gl {
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;
27 EGLImage image =
28 egl->fCreateImage(EGL_DISPLAY(), eglContext, LOCAL_EGL_GL_TEXTURE_2D,
29 clientBuffer, nullptr);
30 return image;
33 ////////////////////////////////////////////////////////////////////////
34 // EGLImageWrapper
36 /*static*/
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);
47 if (!image) {
48 #ifdef DEBUG
49 printf_stderr("Could not create EGL images: ERROR (0x%04x)\n",
50 egl->fGetError());
51 #endif
52 return nullptr;
55 return new EGLImageWrapper(egl, display, image);
58 EGLImageWrapper::EGLImageWrapper(GLLibraryEGL* library, EGLDisplay display,
59 EGLImage image)
60 : mLibrary(library), mDisplay(display), mImage(image), mSync(0) {
61 MOZ_ASSERT(mImage);
64 EGLImageWrapper::~EGLImageWrapper() {
65 mLibrary->fDestroyImage(mDisplay, mImage);
68 bool EGLImageWrapper::FenceSync(GLContext* gl) {
69 MOZ_ASSERT(!mSync);
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.
76 gl->fFlush();
79 if (!mSync) {
80 // we failed to create one, so just do a finish
81 gl->fFinish();
84 return true;
87 bool EGLImageWrapper::ClientWaitSync() {
88 if (!mSync) {
89 // if we have no sync object, then we did a Finish() earlier
90 return true;
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);
99 mSync = nullptr;
101 return result == LOCAL_EGL_CONDITION_SATISFIED;
104 } // namespace gl
105 } // namespace mozilla