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 
15 #ifndef libEGL_hpp
16 #define libEGL_hpp
17 
18 #include <EGL/egl.h>
19 #include <EGL/eglext.h>
20 
21 #include "Common/SharedLibrary.hpp"
22 
23 class LibEGLexports
24 {
25 public:
26 	LibEGLexports();
27 
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);
68 
69 	// Functions that don't change the error code, for use by client APIs
70 	egl::Context *(*clientGetCurrentContext)();
71 };
72 
73 class LibEGL
74 {
75 public:
LibEGL()76 	LibEGL()
77 	{
78 	}
79 
~LibEGL()80 	~LibEGL()
81 	{
82 		freeLibrary(libEGL);
83 	}
84 
operator ->()85 	LibEGLexports *operator->()
86 	{
87 		return loadExports();
88 	}
89 
90 private:
loadExports()91 	LibEGLexports *loadExports()
92 	{
93 		if(!loadLibraryAttempted && !libEGL)
94 		{
95 			#if defined(_WIN32)
96 				#if defined(__LP64__)
97 					const char *libEGL_lib[] = {"libswiftshader_libEGL.dll", "libEGL.dll", "lib64EGL_translator.dll"};
98 				#else
99 					const char *libEGL_lib[] = {"libswiftshader_libEGL.dll", "libEGL.dll", "libEGL_translator.dll"};
100 				#endif
101 			#elif defined(__ANDROID__)
102 				const char *libEGL_lib[] = {"libEGL_swiftshader.so", "libEGL_swiftshader.so"};
103 			#elif defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__)
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__)
111 					const char *libEGL_lib[] = {"libswiftshader_libEGL.dylib", "lib64EGL_translator.dylib", "libEGL.so", "libEGL.dylib"};
112 				#else
113 					const char *libEGL_lib[] = {"libswiftshader_libEGL.dylib", "libEGL_translator.dylib", "libEGL.so", "libEGL.dylib"};
114 				#endif
115 			#elif defined(__Fuchsia__)
116 				const char *libEGL_lib[] = {"libswiftshader_libEGL.so", "libEGL.so"};
117 			#else
118 				#error "libEGL::loadExports unimplemented for this platform"
119 			#endif
120 
121 			std::string directory = getModuleDirectory();
122 			libEGL = loadLibrary(directory, libEGL_lib, "libEGL_swiftshader");
123 
124 			if(libEGL)
125 			{
126 				auto libEGL_swiftshader = (LibEGLexports *(*)())getProcAddress(libEGL, "libEGL_swiftshader");
127 				libEGLexports = libEGL_swiftshader();
128 			}
129 
130 			loadLibraryAttempted = true;
131 		}
132 
133 		return libEGLexports;
134 	}
135 
136 	void *libEGL = nullptr;
137 	LibEGLexports *libEGLexports = nullptr;
138 	bool loadLibraryAttempted = false;
139 };
140 
141 #endif   // libEGL_hpp
142