Home | History | Annotate | Download | only in libEGL
      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:
     76 	LibEGL()
     77 	{
     78 		libEGL = nullptr;
     79 		libEGLexports = nullptr;
     80 	}
     81 
     82 	~LibEGL()
     83 	{
     84 		freeLibrary(libEGL);
     85 	}
     86 
     87 	LibEGLexports *operator->()
     88 	{
     89 		return loadExports();
     90 	}
     91 
     92 private:
     93 	LibEGLexports *loadExports()
     94 	{
     95 		if(!libEGL)
     96 		{
     97 			#if defined(_WIN32)
     98 				#if defined(__LP64__)
     99 					const char *libEGL_lib[] = {"libEGL.dll", "lib64EGL_translator.dll"};
    100 				#else
    101 					const char *libEGL_lib[] = {"libEGL.dll", "libEGL_translator.dll"};
    102 				#endif
    103 			#elif defined(__ANDROID__)
    104 				#if defined(__LP64__)
    105 					const char *libEGL_lib[] = {"/vendor/lib64/egl/libEGL_swiftshader.so"};
    106 				#else
    107 					const char *libEGL_lib[] = {"/vendor/lib/egl/libEGL_swiftshader.so"};
    108 				#endif
    109 			#elif defined(__linux__)
    110 				#if defined(__LP64__)
    111 					const char *libEGL_lib[] = {"lib64EGL_translator.so", "libEGL.so.1", "libEGL.so"};
    112 				#else
    113 					const char *libEGL_lib[] = {"libEGL_translator.so", "libEGL.so.1", "libEGL.so"};
    114 				#endif
    115 			#elif defined(__APPLE__)
    116 				#if defined(__LP64__)
    117 					const char *libEGL_lib[] = {"lib64EGL_translator.dylib", "libEGL.so", "libEGL.dylib", "libswiftshader_libEGL.dylib"};
    118 				#else
    119 					const char *libEGL_lib[] = {"libEGL_translator.dylib", "libEGL.so", "libEGL.dylib", "libswiftshader_libEGL.dylib"};
    120 				#endif
    121 			#else
    122 				#error "libEGL::loadExports unimplemented for this platform"
    123 			#endif
    124 
    125 			libEGL = loadLibrary(libEGL_lib, "libEGL_swiftshader");
    126 
    127 			if(libEGL)
    128 			{
    129 				auto libEGL_swiftshader = (LibEGLexports *(*)())getProcAddress(libEGL, "libEGL_swiftshader");
    130 				libEGLexports = libEGL_swiftshader();
    131 			}
    132 		}
    133 
    134 		return libEGLexports;
    135 	}
    136 
    137 	void *libEGL;
    138 	LibEGLexports *libEGLexports;
    139 };
    140 
    141 #endif   // libEGL_hpp
    142