Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 1 | // Copyright 2016 The SwiftShader Authors. All Rights Reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Nicolas Capens | a230805 | 2015-04-15 16:50:21 -0400 | [diff] [blame] | 15 | #ifndef libEGL_hpp |
| 16 | #define libEGL_hpp |
| 17 | |
Nicolas Capens | a230805 | 2015-04-15 16:50:21 -0400 | [diff] [blame] | 18 | #include <EGL/egl.h> |
Nicolas Capens | a230805 | 2015-04-15 16:50:21 -0400 | [diff] [blame] | 19 | #include <EGL/eglext.h> |
| 20 | |
| 21 | #include "Common/SharedLibrary.hpp" |
| 22 | |
| 23 | class LibEGLexports |
| 24 | { |
| 25 | public: |
| 26 | LibEGLexports(); |
| 27 | |
Nicolas Capens | 38a9b3c | 2019-09-10 16:35:24 -0400 | [diff] [blame] | 28 | EGLint (EGLAPIENTRY* eglGetError)(void); |
| 29 | EGLDisplay (EGLAPIENTRY* eglGetDisplay)(EGLNativeDisplayType display_id); |
| 30 | EGLBoolean (EGLAPIENTRY* eglInitialize)(EGLDisplay dpy, EGLint *major, EGLint *minor); |
| 31 | EGLBoolean (EGLAPIENTRY* eglTerminate)(EGLDisplay dpy); |
| 32 | const char *(EGLAPIENTRY* eglQueryString)(EGLDisplay dpy, EGLint name); |
| 33 | EGLBoolean (EGLAPIENTRY* eglGetConfigs)(EGLDisplay dpy, EGLConfig *configs, EGLint config_size, EGLint *num_config); |
| 34 | EGLBoolean (EGLAPIENTRY* eglChooseConfig)(EGLDisplay dpy, const EGLint *attrib_list, EGLConfig *configs, EGLint config_size, EGLint *num_config); |
| 35 | EGLBoolean (EGLAPIENTRY* eglGetConfigAttrib)(EGLDisplay dpy, EGLConfig config, EGLint attribute, EGLint *value); |
| 36 | EGLSurface (EGLAPIENTRY* eglCreateWindowSurface)(EGLDisplay dpy, EGLConfig config, EGLNativeWindowType window, const EGLint *attrib_list); |
| 37 | EGLSurface (EGLAPIENTRY* eglCreatePbufferSurface)(EGLDisplay dpy, EGLConfig config, const EGLint *attrib_list); |
| 38 | EGLSurface (EGLAPIENTRY* eglCreatePixmapSurface)(EGLDisplay dpy, EGLConfig config, EGLNativePixmapType pixmap, const EGLint *attrib_list); |
| 39 | EGLBoolean (EGLAPIENTRY* eglDestroySurface)(EGLDisplay dpy, EGLSurface surface); |
| 40 | EGLBoolean (EGLAPIENTRY* eglQuerySurface)(EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint *value); |
| 41 | EGLBoolean (EGLAPIENTRY* eglBindAPI)(EGLenum api); |
| 42 | EGLenum (EGLAPIENTRY* eglQueryAPI)(void); |
| 43 | EGLBoolean (EGLAPIENTRY* eglWaitClient)(void); |
| 44 | EGLBoolean (EGLAPIENTRY* eglReleaseThread)(void); |
| 45 | EGLSurface (EGLAPIENTRY* eglCreatePbufferFromClientBuffer)(EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer, EGLConfig config, const EGLint *attrib_list); |
| 46 | EGLBoolean (EGLAPIENTRY* eglSurfaceAttrib)(EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value); |
| 47 | EGLBoolean (EGLAPIENTRY* eglBindTexImage)(EGLDisplay dpy, EGLSurface surface, EGLint buffer); |
| 48 | EGLBoolean (EGLAPIENTRY* eglReleaseTexImage)(EGLDisplay dpy, EGLSurface surface, EGLint buffer); |
| 49 | EGLBoolean (EGLAPIENTRY* eglSwapInterval)(EGLDisplay dpy, EGLint interval); |
| 50 | EGLContext (EGLAPIENTRY* eglCreateContext)(EGLDisplay dpy, EGLConfig config, EGLContext share_context, const EGLint *attrib_list); |
| 51 | EGLBoolean (EGLAPIENTRY* eglDestroyContext)(EGLDisplay dpy, EGLContext ctx); |
| 52 | EGLBoolean (EGLAPIENTRY* eglMakeCurrent)(EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLContext ctx); |
| 53 | EGLContext (EGLAPIENTRY* eglGetCurrentContext)(void); |
| 54 | EGLSurface (EGLAPIENTRY* eglGetCurrentSurface)(EGLint readdraw); |
| 55 | EGLDisplay (EGLAPIENTRY* eglGetCurrentDisplay)(void); |
| 56 | EGLBoolean (EGLAPIENTRY* eglQueryContext)(EGLDisplay dpy, EGLContext ctx, EGLint attribute, EGLint *value); |
| 57 | EGLBoolean (EGLAPIENTRY* eglWaitGL)(void); |
| 58 | EGLBoolean (EGLAPIENTRY* eglWaitNative)(EGLint engine); |
| 59 | EGLBoolean (EGLAPIENTRY* eglSwapBuffers)(EGLDisplay dpy, EGLSurface surface); |
| 60 | EGLBoolean (EGLAPIENTRY* eglCopyBuffers)(EGLDisplay dpy, EGLSurface surface, EGLNativePixmapType target); |
| 61 | EGLImageKHR (EGLAPIENTRY* eglCreateImageKHR)(EGLDisplay dpy, EGLContext ctx, EGLenum target, EGLClientBuffer buffer, const EGLint *attrib_list); |
| 62 | EGLBoolean (EGLAPIENTRY* eglDestroyImageKHR)(EGLDisplay dpy, EGLImageKHR image); |
| 63 | __eglMustCastToProperFunctionPointerType (EGLAPIENTRY* eglGetProcAddress)(const char*); |
| 64 | EGLSyncKHR (EGLAPIENTRY* eglCreateSyncKHR)(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list); |
| 65 | EGLBoolean (EGLAPIENTRY* eglDestroySyncKHR)(EGLDisplay dpy, EGLSyncKHR sync); |
| 66 | EGLint (EGLAPIENTRY* eglClientWaitSyncKHR)(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags, EGLTimeKHR timeout); |
| 67 | EGLBoolean (EGLAPIENTRY* eglGetSyncAttribKHR)(EGLDisplay dpy, EGLSyncKHR sync, EGLint attribute, EGLint *value); |
Nicolas Capens | a230805 | 2015-04-15 16:50:21 -0400 | [diff] [blame] | 68 | |
| 69 | // Functions that don't change the error code, for use by client APIs |
| 70 | egl::Context *(*clientGetCurrentContext)(); |
Nicolas Capens | a230805 | 2015-04-15 16:50:21 -0400 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | class LibEGL |
| 74 | { |
| 75 | public: |
Nicolas Capens | 5e1520f | 2018-07-03 16:13:28 -0400 | [diff] [blame] | 76 | LibEGL() |
Nicolas Capens | a230805 | 2015-04-15 16:50:21 -0400 | [diff] [blame] | 77 | { |
Nicolas Capens | a230805 | 2015-04-15 16:50:21 -0400 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | ~LibEGL() |
| 81 | { |
| 82 | freeLibrary(libEGL); |
| 83 | } |
| 84 | |
| 85 | LibEGLexports *operator->() |
| 86 | { |
| 87 | return loadExports(); |
| 88 | } |
| 89 | |
| 90 | private: |
| 91 | LibEGLexports *loadExports() |
| 92 | { |
Alexis Hetu | 1147fc2 | 2018-11-12 14:21:27 -0500 | [diff] [blame] | 93 | if(!loadLibraryAttempted && !libEGL) |
Nicolas Capens | a230805 | 2015-04-15 16:50:21 -0400 | [diff] [blame] | 94 | { |
| 95 | #if defined(_WIN32) |
Corentin Wallez | cd0a457 | 2015-12-10 15:59:28 -0500 | [diff] [blame] | 96 | #if defined(__LP64__) |
Stuart Morgan | d70129a | 2019-06-18 12:50:30 -0700 | [diff] [blame] | 97 | const char *libEGL_lib[] = {"libswiftshader_libEGL.dll", "libEGL.dll", "lib64EGL_translator.dll"}; |
Corentin Wallez | cd0a457 | 2015-12-10 15:59:28 -0500 | [diff] [blame] | 98 | #else |
Stuart Morgan | d70129a | 2019-06-18 12:50:30 -0700 | [diff] [blame] | 99 | const char *libEGL_lib[] = {"libswiftshader_libEGL.dll", "libEGL.dll", "libEGL_translator.dll"}; |
Corentin Wallez | cd0a457 | 2015-12-10 15:59:28 -0500 | [diff] [blame] | 100 | #endif |
Nicolas Capens | a230805 | 2015-04-15 16:50:21 -0400 | [diff] [blame] | 101 | #elif defined(__ANDROID__) |
Nicolas Capens | a9969b2 | 2018-06-21 01:42:30 -0400 | [diff] [blame] | 102 | const char *libEGL_lib[] = {"libEGL_swiftshader.so", "libEGL_swiftshader.so"}; |
Corentin Wallez | cd0a457 | 2015-12-10 15:59:28 -0500 | [diff] [blame] | 103 | #elif defined(__linux__) |
| 104 | #if defined(__LP64__) |
| 105 | const char *libEGL_lib[] = {"lib64EGL_translator.so", "libEGL.so.1", "libEGL.so"}; |
| 106 | #else |
| 107 | const char *libEGL_lib[] = {"libEGL_translator.so", "libEGL.so.1", "libEGL.so"}; |
| 108 | #endif |
| 109 | #elif defined(__APPLE__) |
| 110 | #if defined(__LP64__) |
Alexis Hetu | 3f22df0 | 2018-03-20 14:06:12 -0400 | [diff] [blame] | 111 | const char *libEGL_lib[] = {"libswiftshader_libEGL.dylib", "lib64EGL_translator.dylib", "libEGL.so", "libEGL.dylib"}; |
Corentin Wallez | cd0a457 | 2015-12-10 15:59:28 -0500 | [diff] [blame] | 112 | #else |
Alexis Hetu | 3f22df0 | 2018-03-20 14:06:12 -0400 | [diff] [blame] | 113 | const char *libEGL_lib[] = {"libswiftshader_libEGL.dylib", "libEGL_translator.dylib", "libEGL.so", "libEGL.dylib"}; |
Corentin Wallez | cd0a457 | 2015-12-10 15:59:28 -0500 | [diff] [blame] | 114 | #endif |
Sergey Ulanov | 53c601e | 2018-01-10 15:12:42 -0800 | [diff] [blame] | 115 | #elif defined(__Fuchsia__) |
Alexis Hetu | dd662d2 | 2018-08-09 17:24:51 -0400 | [diff] [blame] | 116 | const char *libEGL_lib[] = {"libswiftshader_libEGL.so", "libEGL.so"}; |
Nicolas Capens | b4d30d9 | 2015-12-04 12:09:28 -0500 | [diff] [blame] | 117 | #else |
Corentin Wallez | cd0a457 | 2015-12-10 15:59:28 -0500 | [diff] [blame] | 118 | #error "libEGL::loadExports unimplemented for this platform" |
Nicolas Capens | a230805 | 2015-04-15 16:50:21 -0400 | [diff] [blame] | 119 | #endif |
| 120 | |
Nicolas Capens | 5e1520f | 2018-07-03 16:13:28 -0400 | [diff] [blame] | 121 | std::string directory = getModuleDirectory(); |
| 122 | libEGL = loadLibrary(directory, libEGL_lib, "libEGL_swiftshader"); |
Nicolas Capens | a230805 | 2015-04-15 16:50:21 -0400 | [diff] [blame] | 123 | |
| 124 | if(libEGL) |
| 125 | { |
Nicolas Capens | 2a1c569 | 2015-04-17 12:40:04 -0400 | [diff] [blame] | 126 | auto libEGL_swiftshader = (LibEGLexports *(*)())getProcAddress(libEGL, "libEGL_swiftshader"); |
| 127 | libEGLexports = libEGL_swiftshader(); |
Nicolas Capens | a230805 | 2015-04-15 16:50:21 -0400 | [diff] [blame] | 128 | } |
Alexis Hetu | 1147fc2 | 2018-11-12 14:21:27 -0500 | [diff] [blame] | 129 | |
| 130 | loadLibraryAttempted = true; |
Nicolas Capens | a230805 | 2015-04-15 16:50:21 -0400 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | return libEGLexports; |
| 134 | } |
| 135 | |
Alexis Hetu | 99be318 | 2018-04-16 15:38:40 -0400 | [diff] [blame] | 136 | void *libEGL = nullptr; |
| 137 | LibEGLexports *libEGLexports = nullptr; |
Alexis Hetu | 1147fc2 | 2018-11-12 14:21:27 -0500 | [diff] [blame] | 138 | bool loadLibraryAttempted = false; |
Nicolas Capens | a230805 | 2015-04-15 16:50:21 -0400 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | #endif // libEGL_hpp |