1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <OpenGLESDispatch/EGLDispatch.h>
18 
19 #include <dlfcn.h>
20 #include <stdio.h>
21 
22 EGLDispatch s_egl;
23 
24 #define DEFAULT_EGL_LIB "libEGL_swiftshader.so"
25 
26 #define EGL_LOAD_FIELD(return_type, function_name, signature, callargs) \
27     s_egl.function_name = (function_name##_t)dlsym(handle, #function_name);
28 
29 #define EGL_LOAD_FIELD_WITH_EGL(return_type, function_name, signature, callargs) \
30     if ((!s_egl.function_name) && s_egl.eglGetProcAddress)                       \
31         s_egl.function_name = (function_name##_t)s_egl.eglGetProcAddress(#function_name);
32 
33 #define EGL_LOAD_OPTIONAL_FIELD(return_type, function_name, signature, callargs)          \
34     if (s_egl.eglGetProcAddress)                                                          \
35         s_egl.function_name = (function_name##_t)s_egl.eglGetProcAddress(#function_name); \
36     if (!s_egl.function_name || !s_egl.eglGetProcAddress)                                 \
37     EGL_LOAD_FIELD(return_type, function_name, signature, callargs)
38 
egl_dispatch_init()39 bool egl_dispatch_init() {
40     void* handle = dlopen(DEFAULT_EGL_LIB, RTLD_NOW);
41     if (!handle) {
42         printf("Failed to open %s\n", dlerror());
43         return false;
44     }
45 
46     LIST_EGL_FUNCTIONS(EGL_LOAD_FIELD)
47     LIST_EGL_FUNCTIONS(EGL_LOAD_FIELD_WITH_EGL)
48     LIST_EGL_EXTENSIONS_FUNCTIONS(EGL_LOAD_OPTIONAL_FIELD)
49 
50     return true;
51 }
52