ec6b610d7c481bf69ef5be5d0590bc7b32b7830e
[iec.git] / src / type3_AndroidCloud / anbox-master / external / android-emugl / host / libs / libOpenGLESDispatch / GLESv2Dispatch.cpp
1 /*
2 * Copyright (C) 2011 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 #include "OpenGLESDispatch/GLESv2Dispatch.h"
17 #include "OpenGLESDispatch/EGLDispatch.h"
18
19 #include <stdio.h>
20 #include <stdlib.h>
21
22 #include "emugl/common/shared_library.h"
23
24 extern EGLDispatch s_egl;
25
26 static emugl::SharedLibrary *s_gles2_lib = NULL;
27
28 namespace {
29 constexpr const char *glesv2_lib_env_var{"ANDROID_GLESv2_LIB"};
30 }
31
32 // An unimplemented function which prints out an error message.
33 // To make it consistent with the guest, all GLES2 functions not supported by
34 // the driver should be redirected to this function.
35
36 static void gles2_unimplemented() {
37     fprintf(stderr, "Called unimplemented GLESv2 API\n");
38 }
39
40 //
41 // This function is called only once during initialiation before
42 // any thread has been created - hence it should NOT be thread safe.
43 //
44 bool gles2_dispatch_init(const char *path, GLESv2Dispatch *dispatch_table)
45 {
46     const char *libName = getenv(glesv2_lib_env_var);
47     if (!libName)
48       libName = path;
49     if (!libName)
50       return false;
51
52     char error[256];
53     s_gles2_lib = emugl::SharedLibrary::open(libName, error, sizeof(error));
54     if (!s_gles2_lib) {
55         fprintf(stderr, "%s: Could not load %s [%s]\n", __FUNCTION__,
56                 libName, error);
57         return false;
58     }
59
60     //
61     // init the GLES dispatch table
62     //
63 #define LOOKUP_SYMBOL(return_type,function_name,signature,callargs) \
64     dispatch_table-> function_name = reinterpret_cast< function_name ## _t >( \
65             s_gles2_lib->findSymbol(#function_name));
66
67 #define LOOKUP_EXT_SYMBOL(return_type,function_name,signature,callargs) \
68     dispatch_table-> function_name = reinterpret_cast< function_name ## _t >( \
69             s_egl.eglGetProcAddress(#function_name));
70
71     LIST_GLES2_FUNCTIONS(LOOKUP_SYMBOL,LOOKUP_EXT_SYMBOL)
72
73     dispatch_table->initialized = true;
74
75     return true;
76 }
77
78 //
79 // This function is called only during initialization before
80 // any thread has been created - hence it should NOT be thread safe.
81 //
82 void *gles2_dispatch_get_proc_func(const char *name, void *userData)
83 {
84     void* func = NULL;
85
86     if (s_gles2_lib && !func) {
87         func = (void *)s_gles2_lib->findSymbol(name);
88     }
89
90     if (!func) {
91         func = (void *)s_egl.eglGetProcAddress(name);
92     }
93
94     // To make it consistent with the guest, redirect any unsupported functions
95     // to gles2_unimplemented.
96     if (!func) {
97         func = (void *)gles2_unimplemented;
98     }
99     return func;
100 }