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 file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #ifndef GLLIBRARYEGL_H_
6 #define GLLIBRARYEGL_H_
9 #include "mozilla/X11Util.h"
12 #include "GLLibraryLoader.h"
13 #include "mozilla/ThreadLocal.h"
20 #ifndef WIN32_LEAN_AND_MEAN
21 #define WIN32_LEAN_AND_MEAN 1
26 typedef HDC EGLNativeDisplayType
;
27 typedef HBITMAP EGLNativePixmapType
;
28 typedef HWND EGLNativeWindowType
;
30 #define GET_NATIVE_WINDOW(aWidget) ((EGLNativeWindowType)aWidget->GetNativeData(NS_NATIVE_WINDOW))
33 typedef void *EGLNativeDisplayType
;
34 typedef void *EGLNativePixmapType
;
35 typedef void *EGLNativeWindowType
;
38 // We only need to explicitly dlopen egltrace
39 // on android as we can use LD_PRELOAD or other tricks
40 // on other platforms. We look for it in /data/local
41 // as that's writeable by all users
43 // This should really go in GLLibraryEGL.cpp but we currently reference
44 // APITRACE_LIB in GLContextProviderEGL.cpp. Further refactoring
45 // will come in subsequent patches on Bug 732865
46 #define APITRACE_LIB "/data/local/tmp/egltrace.so"
48 #ifdef MOZ_WIDGET_ANDROID
50 #endif // MOZ_WIDGET_ANDROID
55 #define EGL_DEFAULT_DISPLAY ((EGLNativeDisplayType)mozilla::DefaultXDisplay())
57 #define EGL_DEFAULT_DISPLAY ((EGLNativeDisplayType)0)
68 #ifndef MOZ_FUNCTION_NAME
70 # define MOZ_FUNCTION_NAME __PRETTY_FUNCTION__
71 # elif defined(_MSC_VER)
72 # define MOZ_FUNCTION_NAME __FUNCTION__
74 # define MOZ_FUNCTION_NAME __func__ // defined in C99, supported in various C++ compilers. Just raw function name.
78 #define BEFORE_GL_CALL do { \
79 BeforeGLCall(MOZ_FUNCTION_NAME); \
82 #define AFTER_GL_CALL do { \
83 AfterGLCall(MOZ_FUNCTION_NAME); \
85 // We rely on the fact that GLLibraryEGL.h #defines BEFORE_GL_CALL and
86 // AFTER_GL_CALL to nothing if !defined(DEBUG).
88 #define BEFORE_GL_CALL
96 : mInitialized(false),
102 void InitExtensions();
105 * Known GL extensions that can be queried by
106 * IsExtensionSupported. The results of this are cached, and as
107 * such it's safe to use this even in performance critical code.
108 * If you add to this array, remember to add to the string names
114 KHR_gl_texture_2D_image
,
116 ANGLE_surface_d3d_texture_2d_share_handle
,
117 EXT_create_context_robustness
,
120 ANDROID_native_fence_sync
,
124 bool IsExtensionSupported(EGLExtensions aKnownExtension
) const {
125 return mAvailableExtensions
[aKnownExtension
];
128 void MarkExtensionUnsupported(EGLExtensions aKnownExtension
) {
129 mAvailableExtensions
[aKnownExtension
] = false;
133 std::bitset
<Extensions_Max
> mAvailableExtensions
;
137 EGLDisplay
fGetDisplay(void* display_id
)
140 EGLDisplay disp
= mSymbols
.fGetDisplay(display_id
);
145 EGLSurface
fGetCurrentSurface(EGLint id
)
148 EGLSurface surf
= mSymbols
.fGetCurrentSurface(id
);
153 EGLContext
fGetCurrentContext()
156 EGLContext context
= mSymbols
.fGetCurrentContext();
161 EGLBoolean
fMakeCurrent(EGLDisplay dpy
, EGLSurface draw
, EGLSurface read
, EGLContext ctx
)
164 EGLBoolean b
= mSymbols
.fMakeCurrent(dpy
, draw
, read
, ctx
);
169 EGLBoolean
fDestroyContext(EGLDisplay dpy
, EGLContext ctx
)
172 EGLBoolean b
= mSymbols
.fDestroyContext(dpy
, ctx
);
177 EGLContext
fCreateContext(EGLDisplay dpy
, EGLConfig config
, EGLContext share_context
, const EGLint
*attrib_list
)
180 EGLContext ctx
= mSymbols
.fCreateContext(dpy
, config
, share_context
, attrib_list
);
185 EGLBoolean
fDestroySurface(EGLDisplay dpy
, EGLSurface surface
)
188 EGLBoolean b
= mSymbols
.fDestroySurface(dpy
, surface
);
193 EGLSurface
fCreateWindowSurface(EGLDisplay dpy
, EGLConfig config
, EGLNativeWindowType win
, const EGLint
*attrib_list
)
196 EGLSurface surf
= mSymbols
.fCreateWindowSurface(dpy
, config
, win
, attrib_list
);
201 EGLSurface
fCreatePbufferSurface(EGLDisplay dpy
, EGLConfig config
, const EGLint
*attrib_list
)
204 EGLSurface surf
= mSymbols
.fCreatePbufferSurface(dpy
, config
, attrib_list
);
209 EGLSurface
fCreatePixmapSurface(EGLDisplay dpy
, EGLConfig config
, EGLNativePixmapType pixmap
, const EGLint
*attrib_list
)
212 EGLSurface surf
= mSymbols
.fCreatePixmapSurface(dpy
, config
, pixmap
, attrib_list
);
217 EGLBoolean
fBindAPI(EGLenum api
)
220 EGLBoolean b
= mSymbols
.fBindAPI(api
);
225 EGLBoolean
fInitialize(EGLDisplay dpy
, EGLint
* major
, EGLint
* minor
)
228 EGLBoolean b
= mSymbols
.fInitialize(dpy
, major
, minor
);
233 EGLBoolean
fChooseConfig(EGLDisplay dpy
, const EGLint
*attrib_list
, EGLConfig
*configs
, EGLint config_size
, EGLint
*num_config
)
236 EGLBoolean b
= mSymbols
.fChooseConfig(dpy
, attrib_list
, configs
, config_size
, num_config
);
244 EGLint i
= mSymbols
.fGetError();
249 EGLBoolean
fGetConfigAttrib(EGLDisplay dpy
, EGLConfig config
, EGLint attribute
, EGLint
*value
)
252 EGLBoolean b
= mSymbols
.fGetConfigAttrib(dpy
, config
, attribute
, value
);
257 EGLBoolean
fGetConfigs(EGLDisplay dpy
, EGLConfig
*configs
, EGLint config_size
, EGLint
*num_config
)
260 EGLBoolean b
= mSymbols
.fGetConfigs(dpy
, configs
, config_size
, num_config
);
265 EGLBoolean
fWaitNative(EGLint engine
)
268 EGLBoolean b
= mSymbols
.fWaitNative(engine
);
273 EGLCastToRelevantPtr
fGetProcAddress(const char *procname
)
276 EGLCastToRelevantPtr p
= mSymbols
.fGetProcAddress(procname
);
281 EGLBoolean
fSwapBuffers(EGLDisplay dpy
, EGLSurface surface
)
284 EGLBoolean b
= mSymbols
.fSwapBuffers(dpy
, surface
);
289 EGLBoolean
fCopyBuffers(EGLDisplay dpy
, EGLSurface surface
, EGLNativePixmapType target
)
292 EGLBoolean b
= mSymbols
.fCopyBuffers(dpy
, surface
, target
);
297 const GLubyte
* fQueryString(EGLDisplay dpy
, EGLint name
)
301 if (mSymbols
.fQueryStringImplementationANDROID
) {
302 b
= mSymbols
.fQueryStringImplementationANDROID(dpy
, name
);
304 b
= mSymbols
.fQueryString(dpy
, name
);
310 EGLBoolean
fQueryContext(EGLDisplay dpy
, EGLContext ctx
, EGLint attribute
, EGLint
*value
)
313 EGLBoolean b
= mSymbols
.fQueryContext(dpy
, ctx
, attribute
, value
);
318 EGLBoolean
fBindTexImage(EGLDisplay dpy
, EGLSurface surface
, EGLint buffer
)
321 EGLBoolean b
= mSymbols
.fBindTexImage(dpy
, surface
, buffer
);
326 EGLBoolean
fReleaseTexImage(EGLDisplay dpy
, EGLSurface surface
, EGLint buffer
)
329 EGLBoolean b
= mSymbols
.fReleaseTexImage(dpy
, surface
, buffer
);
334 EGLImage
fCreateImage(EGLDisplay dpy
, EGLContext ctx
, EGLenum target
, EGLClientBuffer buffer
, const EGLint
*attrib_list
)
337 EGLImage i
= mSymbols
.fCreateImage(dpy
, ctx
, target
, buffer
, attrib_list
);
342 EGLBoolean
fDestroyImage(EGLDisplay dpy
, EGLImage image
)
345 EGLBoolean b
= mSymbols
.fDestroyImage(dpy
, image
);
350 // New extension which allow us to lock texture and get raw image pointer
351 EGLBoolean
fLockSurface(EGLDisplay dpy
, EGLSurface surface
, const EGLint
*attrib_list
)
354 EGLBoolean b
= mSymbols
.fLockSurface(dpy
, surface
, attrib_list
);
359 EGLBoolean
fUnlockSurface(EGLDisplay dpy
, EGLSurface surface
)
362 EGLBoolean b
= mSymbols
.fUnlockSurface(dpy
, surface
);
367 EGLBoolean
fQuerySurface(EGLDisplay dpy
, EGLSurface surface
, EGLint attribute
, EGLint
*value
)
370 EGLBoolean b
= mSymbols
.fQuerySurface(dpy
, surface
, attribute
, value
);
375 EGLBoolean
fQuerySurfacePointerANGLE(EGLDisplay dpy
, EGLSurface surface
, EGLint attribute
, void **value
)
378 EGLBoolean b
= mSymbols
.fQuerySurfacePointerANGLE(dpy
, surface
, attribute
, value
);
383 EGLSync
fCreateSync(EGLDisplay dpy
, EGLenum type
, const EGLint
*attrib_list
)
386 EGLSync ret
= mSymbols
.fCreateSync(dpy
, type
, attrib_list
);
391 EGLBoolean
fDestroySync(EGLDisplay dpy
, EGLSync sync
)
394 EGLBoolean b
= mSymbols
.fDestroySync(dpy
, sync
);
399 EGLint
fClientWaitSync(EGLDisplay dpy
, EGLSync sync
, EGLint flags
, EGLTime timeout
)
402 EGLint ret
= mSymbols
.fClientWaitSync(dpy
, sync
, flags
, timeout
);
407 EGLBoolean
fGetSyncAttrib(EGLDisplay dpy
, EGLSync sync
, EGLint attribute
, EGLint
*value
)
410 EGLBoolean b
= mSymbols
.fGetSyncAttrib(dpy
, sync
, attribute
, value
);
415 EGLint
fDupNativeFenceFDANDROID(EGLDisplay dpy
, EGLSync sync
)
417 MOZ_ASSERT(mSymbols
.fDupNativeFenceFDANDROID
);
419 EGLint ret
= mSymbols
.fDupNativeFenceFDANDROID(dpy
, sync
);
424 EGLDisplay
Display() {
428 bool IsANGLE() const {
432 bool HasKHRImageBase() {
433 return IsExtensionSupported(KHR_image
) || IsExtensionSupported(KHR_image_base
);
436 bool HasKHRImagePixmap() {
437 return IsExtensionSupported(KHR_image
) || IsExtensionSupported(KHR_image_pixmap
);
440 bool HasKHRImageTexture2D() {
441 return IsExtensionSupported(KHR_gl_texture_2D_image
);
444 bool HasANGLESurfaceD3DTexture2DShareHandle() {
445 return IsExtensionSupported(ANGLE_surface_d3d_texture_2d_share_handle
);
448 bool HasRobustness() const {
449 return IsExtensionSupported(EXT_create_context_robustness
);
452 bool EnsureInitialized();
454 void DumpEGLConfig(EGLConfig cfg
);
455 void DumpEGLConfigs();
458 typedef EGLDisplay (GLAPIENTRY
* pfnGetDisplay
)(void *display_id
);
459 pfnGetDisplay fGetDisplay
;
460 typedef EGLSurface (GLAPIENTRY
* pfnGetCurrentSurface
)(EGLint
);
461 pfnGetCurrentSurface fGetCurrentSurface
;
462 typedef EGLContext (GLAPIENTRY
* pfnGetCurrentContext
)(void);
463 pfnGetCurrentContext fGetCurrentContext
;
464 typedef EGLBoolean (GLAPIENTRY
* pfnMakeCurrent
)(EGLDisplay dpy
, EGLSurface draw
, EGLSurface read
, EGLContext ctx
);
465 pfnMakeCurrent fMakeCurrent
;
466 typedef EGLBoolean (GLAPIENTRY
* pfnDestroyContext
)(EGLDisplay dpy
, EGLContext ctx
);
467 pfnDestroyContext fDestroyContext
;
468 typedef EGLContext (GLAPIENTRY
* pfnCreateContext
)(EGLDisplay dpy
, EGLConfig config
, EGLContext share_context
, const EGLint
*attrib_list
);
469 pfnCreateContext fCreateContext
;
470 typedef EGLBoolean (GLAPIENTRY
* pfnDestroySurface
)(EGLDisplay dpy
, EGLSurface surface
);
471 pfnDestroySurface fDestroySurface
;
472 typedef EGLSurface (GLAPIENTRY
* pfnCreateWindowSurface
)(EGLDisplay dpy
, EGLConfig config
, EGLNativeWindowType win
, const EGLint
*attrib_list
);
473 pfnCreateWindowSurface fCreateWindowSurface
;
474 typedef EGLSurface (GLAPIENTRY
* pfnCreatePbufferSurface
)(EGLDisplay dpy
, EGLConfig config
, const EGLint
*attrib_list
);
475 pfnCreatePbufferSurface fCreatePbufferSurface
;
476 typedef EGLSurface (GLAPIENTRY
* pfnCreatePixmapSurface
)(EGLDisplay dpy
, EGLConfig config
, EGLNativePixmapType pixmap
, const EGLint
*attrib_list
);
477 pfnCreatePixmapSurface fCreatePixmapSurface
;
478 typedef EGLBoolean (GLAPIENTRY
* pfnBindAPI
)(EGLenum api
);
480 typedef EGLBoolean (GLAPIENTRY
* pfnInitialize
)(EGLDisplay dpy
, EGLint
*major
, EGLint
*minor
);
481 pfnInitialize fInitialize
;
482 typedef EGLBoolean (GLAPIENTRY
* pfnChooseConfig
)(EGLDisplay dpy
, const EGLint
*attrib_list
, EGLConfig
*configs
, EGLint config_size
, EGLint
*num_config
);
483 pfnChooseConfig fChooseConfig
;
484 typedef EGLint (GLAPIENTRY
* pfnGetError
)(void);
485 pfnGetError fGetError
;
486 typedef EGLBoolean (GLAPIENTRY
* pfnGetConfigAttrib
)(EGLDisplay dpy
, EGLConfig config
, EGLint attribute
, EGLint
*value
);
487 pfnGetConfigAttrib fGetConfigAttrib
;
488 typedef EGLBoolean (GLAPIENTRY
* pfnGetConfigs
)(EGLDisplay dpy
, EGLConfig
*configs
, EGLint config_size
, EGLint
*num_config
);
489 pfnGetConfigs fGetConfigs
;
490 typedef EGLBoolean (GLAPIENTRY
* pfnWaitNative
)(EGLint engine
);
491 pfnWaitNative fWaitNative
;
492 typedef EGLCastToRelevantPtr (GLAPIENTRY
* pfnGetProcAddress
)(const char *procname
);
493 pfnGetProcAddress fGetProcAddress
;
494 typedef EGLBoolean (GLAPIENTRY
* pfnSwapBuffers
)(EGLDisplay dpy
, EGLSurface surface
);
495 pfnSwapBuffers fSwapBuffers
;
496 typedef EGLBoolean (GLAPIENTRY
* pfnCopyBuffers
)(EGLDisplay dpy
, EGLSurface surface
,
497 EGLNativePixmapType target
);
498 pfnCopyBuffers fCopyBuffers
;
499 typedef const GLubyte
* (GLAPIENTRY
* pfnQueryString
)(EGLDisplay
, EGLint name
);
500 pfnQueryString fQueryString
;
501 pfnQueryString fQueryStringImplementationANDROID
;
502 typedef EGLBoolean (GLAPIENTRY
* pfnQueryContext
)(EGLDisplay dpy
, EGLContext ctx
,
503 EGLint attribute
, EGLint
*value
);
504 pfnQueryContext fQueryContext
;
505 typedef EGLBoolean (GLAPIENTRY
* pfnBindTexImage
)(EGLDisplay
, EGLSurface surface
, EGLint buffer
);
506 pfnBindTexImage fBindTexImage
;
507 typedef EGLBoolean (GLAPIENTRY
* pfnReleaseTexImage
)(EGLDisplay
, EGLSurface surface
, EGLint buffer
);
508 pfnReleaseTexImage fReleaseTexImage
;
509 typedef EGLImage (GLAPIENTRY
* pfnCreateImage
)(EGLDisplay dpy
, EGLContext ctx
, EGLenum target
, EGLClientBuffer buffer
, const EGLint
*attrib_list
);
510 pfnCreateImage fCreateImage
;
511 typedef EGLBoolean (GLAPIENTRY
* pfnDestroyImage
)(EGLDisplay dpy
, EGLImage image
);
512 pfnDestroyImage fDestroyImage
;
514 // New extension which allow us to lock texture and get raw image pointer
515 typedef EGLBoolean (GLAPIENTRY
* pfnLockSurface
)(EGLDisplay dpy
, EGLSurface surface
, const EGLint
*attrib_list
);
516 pfnLockSurface fLockSurface
;
517 typedef EGLBoolean (GLAPIENTRY
* pfnUnlockSurface
)(EGLDisplay dpy
, EGLSurface surface
);
518 pfnUnlockSurface fUnlockSurface
;
519 typedef EGLBoolean (GLAPIENTRY
* pfnQuerySurface
)(EGLDisplay dpy
, EGLSurface surface
, EGLint attribute
, EGLint
*value
);
520 pfnQuerySurface fQuerySurface
;
522 typedef EGLBoolean (GLAPIENTRY
* pfnQuerySurfacePointerANGLE
)(EGLDisplay dpy
, EGLSurface surface
, EGLint attribute
, void **value
);
523 pfnQuerySurfacePointerANGLE fQuerySurfacePointerANGLE
;
525 typedef EGLSync (GLAPIENTRY
* pfnCreateSync
)(EGLDisplay dpy
, EGLenum type
, const EGLint
*attrib_list
);
526 pfnCreateSync fCreateSync
;
527 typedef EGLBoolean (GLAPIENTRY
* pfnDestroySync
)(EGLDisplay dpy
, EGLSync sync
);
528 pfnDestroySync fDestroySync
;
529 typedef EGLint (GLAPIENTRY
* pfnClientWaitSync
)(EGLDisplay dpy
, EGLSync sync
, EGLint flags
, EGLTime timeout
);
530 pfnClientWaitSync fClientWaitSync
;
531 typedef EGLBoolean (GLAPIENTRY
* pfnGetSyncAttrib
)(EGLDisplay dpy
, EGLSync sync
, EGLint attribute
, EGLint
*value
);
532 pfnGetSyncAttrib fGetSyncAttrib
;
533 typedef EGLint (GLAPIENTRY
* pfnDupNativeFenceFDANDROID
)(EGLDisplay dpy
, EGLSync sync
);
534 pfnDupNativeFenceFDANDROID fDupNativeFenceFDANDROID
;
538 static void BeforeGLCall(const char* glFunction
);
539 static void AfterGLCall(const char* glFunction
);
543 EGLContext
CachedCurrentContext() {
544 return sCurrentContext
.get();
546 void UnsetCachedCurrentContext() {
547 sCurrentContext
.set(nullptr);
549 void SetCachedCurrentContext(EGLContext aCtx
) {
550 sCurrentContext
.set(aCtx
);
552 bool CachedCurrentContextMatches() {
553 return sCurrentContext
.get() == fGetCurrentContext();
557 static ThreadLocal
<EGLContext
> sCurrentContext
;
561 EGLContext
CachedCurrentContext() {
564 void UnsetCachedCurrentContext() {}
565 void SetCachedCurrentContext(EGLContext aCtx
) { }
566 bool CachedCurrentContextMatches() { return true; }
571 PRLibrary
* mEGLLibrary
;
572 EGLDisplay mEGLDisplay
;
577 extern GLLibraryEGL sEGLLibrary
;
578 #define EGL_DISPLAY() sEGLLibrary.Display()
581 } /* namespace mozilla */
583 #endif /* GLLIBRARYEGL_H_ */