• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 (*eglGetError)(void);
29 	EGLDisplay (*eglGetDisplay)(EGLNativeDisplayType display_id);
30 	EGLBoolean (*eglInitialize)(EGLDisplay dpy, EGLint *major, EGLint *minor);
31 	EGLBoolean (*eglTerminate)(EGLDisplay dpy);
32 	const char *(*eglQueryString)(EGLDisplay dpy, EGLint name);
33 	EGLBoolean (*eglGetConfigs)(EGLDisplay dpy, EGLConfig *configs, EGLint config_size, EGLint *num_config);
34 	EGLBoolean (*eglChooseConfig)(EGLDisplay dpy, const EGLint *attrib_list, EGLConfig *configs, EGLint config_size, EGLint *num_config);
35 	EGLBoolean (*eglGetConfigAttrib)(EGLDisplay dpy, EGLConfig config, EGLint attribute, EGLint *value);
36 	EGLSurface (*eglCreateWindowSurface)(EGLDisplay dpy, EGLConfig config, EGLNativeWindowType window, const EGLint *attrib_list);
37 	EGLSurface (*eglCreatePbufferSurface)(EGLDisplay dpy, EGLConfig config, const EGLint *attrib_list);
38 	EGLSurface (*eglCreatePixmapSurface)(EGLDisplay dpy, EGLConfig config, EGLNativePixmapType pixmap, const EGLint *attrib_list);
39 	EGLBoolean (*eglDestroySurface)(EGLDisplay dpy, EGLSurface surface);
40 	EGLBoolean (*eglQuerySurface)(EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint *value);
41 	EGLBoolean (*eglBindAPI)(EGLenum api);
42 	EGLenum (*eglQueryAPI)(void);
43 	EGLBoolean (*eglWaitClient)(void);
44 	EGLBoolean (*eglReleaseThread)(void);
45 	EGLSurface (*eglCreatePbufferFromClientBuffer)(EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer, EGLConfig config, const EGLint *attrib_list);
46 	EGLBoolean (*eglSurfaceAttrib)(EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value);
47 	EGLBoolean (*eglBindTexImage)(EGLDisplay dpy, EGLSurface surface, EGLint buffer);
48 	EGLBoolean (*eglReleaseTexImage)(EGLDisplay dpy, EGLSurface surface, EGLint buffer);
49 	EGLBoolean (*eglSwapInterval)(EGLDisplay dpy, EGLint interval);
50 	EGLContext (*eglCreateContext)(EGLDisplay dpy, EGLConfig config, EGLContext share_context, const EGLint *attrib_list);
51 	EGLBoolean (*eglDestroyContext)(EGLDisplay dpy, EGLContext ctx);
52 	EGLBoolean (*eglMakeCurrent)(EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLContext ctx);
53 	EGLContext (*eglGetCurrentContext)(void);
54 	EGLSurface (*eglGetCurrentSurface)(EGLint readdraw);
55 	EGLDisplay (*eglGetCurrentDisplay)(void);
56 	EGLBoolean (*eglQueryContext)(EGLDisplay dpy, EGLContext ctx, EGLint attribute, EGLint *value);
57 	EGLBoolean (*eglWaitGL)(void);
58 	EGLBoolean (*eglWaitNative)(EGLint engine);
59 	EGLBoolean (*eglSwapBuffers)(EGLDisplay dpy, EGLSurface surface);
60 	EGLBoolean (*eglCopyBuffers)(EGLDisplay dpy, EGLSurface surface, EGLNativePixmapType target);
61 	EGLImageKHR (*eglCreateImageKHR)(EGLDisplay dpy, EGLContext ctx, EGLenum target, EGLClientBuffer buffer, const EGLint *attrib_list);
62 	EGLBoolean (*eglDestroyImageKHR)(EGLDisplay dpy, EGLImageKHR image);
63 	__eglMustCastToProperFunctionPointerType (*eglGetProcAddress)(const char*);
64 	EGLSyncKHR (*eglCreateSyncKHR)(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list);
65 	EGLBoolean (*eglDestroySyncKHR)(EGLDisplay dpy, EGLSyncKHR sync);
66 	EGLint (*eglClientWaitSyncKHR)(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags, EGLTimeKHR timeout);
67 	EGLBoolean (*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[] = {"libEGL.dll", "lib64EGL_translator.dll"};
98 				#else
99 					const char *libEGL_lib[] = {"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__)
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