blob: 0863cd5954e36882947d5ea6ac695f5927c0d7d6 [file] [log] [blame]
Nicolas Capens0bac2852016-05-07 06:09:58 -04001// 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 Capensa2308052015-04-15 16:50:21 -040015#ifndef libEGL_hpp
16#define libEGL_hpp
17
Nicolas Capensa2308052015-04-15 16:50:21 -040018#include <EGL/egl.h>
Nicolas Capensa2308052015-04-15 16:50:21 -040019#include <EGL/eglext.h>
20
21#include "Common/SharedLibrary.hpp"
22
23class LibEGLexports
24{
25public:
26 LibEGLexports();
27
Nicolas Capens38a9b3c2019-09-10 16:35:24 -040028 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 Capensa2308052015-04-15 16:50:21 -040068
69 // Functions that don't change the error code, for use by client APIs
70 egl::Context *(*clientGetCurrentContext)();
Nicolas Capensa2308052015-04-15 16:50:21 -040071};
72
73class LibEGL
74{
75public:
Nicolas Capens5e1520f2018-07-03 16:13:28 -040076 LibEGL()
Nicolas Capensa2308052015-04-15 16:50:21 -040077 {
Nicolas Capensa2308052015-04-15 16:50:21 -040078 }
79
80 ~LibEGL()
81 {
82 freeLibrary(libEGL);
83 }
84
85 LibEGLexports *operator->()
86 {
87 return loadExports();
88 }
89
90private:
91 LibEGLexports *loadExports()
92 {
Alexis Hetu1147fc22018-11-12 14:21:27 -050093 if(!loadLibraryAttempted && !libEGL)
Nicolas Capensa2308052015-04-15 16:50:21 -040094 {
95 #if defined(_WIN32)
Corentin Wallezcd0a4572015-12-10 15:59:28 -050096 #if defined(__LP64__)
Stuart Morgand70129a2019-06-18 12:50:30 -070097 const char *libEGL_lib[] = {"libswiftshader_libEGL.dll", "libEGL.dll", "lib64EGL_translator.dll"};
Corentin Wallezcd0a4572015-12-10 15:59:28 -050098 #else
Stuart Morgand70129a2019-06-18 12:50:30 -070099 const char *libEGL_lib[] = {"libswiftshader_libEGL.dll", "libEGL.dll", "libEGL_translator.dll"};
Corentin Wallezcd0a4572015-12-10 15:59:28 -0500100 #endif
Nicolas Capensa2308052015-04-15 16:50:21 -0400101 #elif defined(__ANDROID__)
Nicolas Capensa9969b22018-06-21 01:42:30 -0400102 const char *libEGL_lib[] = {"libEGL_swiftshader.so", "libEGL_swiftshader.so"};
Corentin Wallezcd0a4572015-12-10 15:59:28 -0500103 #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 Hetu3f22df02018-03-20 14:06:12 -0400111 const char *libEGL_lib[] = {"libswiftshader_libEGL.dylib", "lib64EGL_translator.dylib", "libEGL.so", "libEGL.dylib"};
Corentin Wallezcd0a4572015-12-10 15:59:28 -0500112 #else
Alexis Hetu3f22df02018-03-20 14:06:12 -0400113 const char *libEGL_lib[] = {"libswiftshader_libEGL.dylib", "libEGL_translator.dylib", "libEGL.so", "libEGL.dylib"};
Corentin Wallezcd0a4572015-12-10 15:59:28 -0500114 #endif
Sergey Ulanov53c601e2018-01-10 15:12:42 -0800115 #elif defined(__Fuchsia__)
Alexis Hetudd662d22018-08-09 17:24:51 -0400116 const char *libEGL_lib[] = {"libswiftshader_libEGL.so", "libEGL.so"};
Nicolas Capensb4d30d92015-12-04 12:09:28 -0500117 #else
Corentin Wallezcd0a4572015-12-10 15:59:28 -0500118 #error "libEGL::loadExports unimplemented for this platform"
Nicolas Capensa2308052015-04-15 16:50:21 -0400119 #endif
120
Nicolas Capens5e1520f2018-07-03 16:13:28 -0400121 std::string directory = getModuleDirectory();
122 libEGL = loadLibrary(directory, libEGL_lib, "libEGL_swiftshader");
Nicolas Capensa2308052015-04-15 16:50:21 -0400123
124 if(libEGL)
125 {
Nicolas Capens2a1c5692015-04-17 12:40:04 -0400126 auto libEGL_swiftshader = (LibEGLexports *(*)())getProcAddress(libEGL, "libEGL_swiftshader");
127 libEGLexports = libEGL_swiftshader();
Nicolas Capensa2308052015-04-15 16:50:21 -0400128 }
Alexis Hetu1147fc22018-11-12 14:21:27 -0500129
130 loadLibraryAttempted = true;
Nicolas Capensa2308052015-04-15 16:50:21 -0400131 }
132
133 return libEGLexports;
134 }
135
Alexis Hetu99be3182018-04-16 15:38:40 -0400136 void *libEGL = nullptr;
137 LibEGLexports *libEGLexports = nullptr;
Alexis Hetu1147fc22018-11-12 14:21:27 -0500138 bool loadLibraryAttempted = false;
Nicolas Capensa2308052015-04-15 16:50:21 -0400139};
140
141#endif // libEGL_hpp