1 /*
2  ** Copyright 2007, 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 //#define LOG_NDEBUG 0
18 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
19 
20 #include <EGL/Loader.h>
21 
22 #include <string>
23 
24 #include <dirent.h>
25 #include <dlfcn.h>
26 
27 #include <android-base/properties.h>
28 #include <android/dlext.h>
29 #include <log/log.h>
30 #include <utils/Timers.h>
31 
32 #ifndef __ANDROID_VNDK__
33 #include <graphicsenv/GraphicsEnv.h>
34 #endif
35 #include <vndksupport/linker.h>
36 
37 #include "egl_platform_entries.h"
38 #include "egl_trace.h"
39 #include "egldefs.h"
40 #include <EGL/eglext_angle.h>
41 
42 namespace android {
43 
44 /*
45  * EGL userspace drivers must be provided either:
46  * - as a single library:
47  *      /vendor/lib/egl/libGLES.so
48  *
49  * - as separate libraries:
50  *      /vendor/lib/egl/libEGL.so
51  *      /vendor/lib/egl/libGLESv1_CM.so
52  *      /vendor/lib/egl/libGLESv2.so
53  *
54  * For backward compatibility and to facilitate the transition to
55  * this new naming scheme, the loader will additionally look for:
56  *
57  *      /{vendor|system}/lib/egl/lib{GLES | [EGL|GLESv1_CM|GLESv2]}_*.so
58  *
59  */
60 
getInstance()61 Loader& Loader::getInstance() {
62     static Loader loader;
63     return loader;
64 }
65 
do_dlopen(const char * path,int mode)66 static void* do_dlopen(const char* path, int mode) {
67     ATRACE_CALL();
68     return dlopen(path, mode);
69 }
70 
do_android_dlopen_ext(const char * path,int mode,const android_dlextinfo * info)71 static void* do_android_dlopen_ext(const char* path, int mode, const android_dlextinfo* info) {
72     ATRACE_CALL();
73     return android_dlopen_ext(path, mode, info);
74 }
75 
do_android_load_sphal_library(const char * path,int mode)76 static void* do_android_load_sphal_library(const char* path, int mode) {
77     ATRACE_CALL();
78     return android_load_sphal_library(path, mode);
79 }
80 
do_android_unload_sphal_library(void * dso)81 static int do_android_unload_sphal_library(void* dso) {
82     ATRACE_CALL();
83     return android_unload_sphal_library(dso);
84 }
85 
driver_t(void * gles)86 Loader::driver_t::driver_t(void* gles)
87 {
88     dso[0] = gles;
89     for (size_t i=1 ; i<NELEM(dso) ; i++)
90         dso[i] = nullptr;
91 }
92 
~driver_t()93 Loader::driver_t::~driver_t()
94 {
95     for (size_t i=0 ; i<NELEM(dso) ; i++) {
96         if (dso[i]) {
97             dlclose(dso[i]);
98             dso[i] = nullptr;
99         }
100     }
101 }
102 
set(void * hnd,int32_t api)103 int Loader::driver_t::set(void* hnd, int32_t api)
104 {
105     switch (api) {
106         case EGL:
107             dso[0] = hnd;
108             break;
109         case GLESv1_CM:
110             dso[1] = hnd;
111             break;
112         case GLESv2:
113             dso[2] = hnd;
114             break;
115         default:
116             return -EOVERFLOW;
117     }
118     return 0;
119 }
120 
Loader()121 Loader::Loader()
122     : getProcAddress(nullptr)
123 {
124 }
125 
~Loader()126 Loader::~Loader() {
127 }
128 
load_wrapper(const char * path)129 static void* load_wrapper(const char* path) {
130     void* so = do_dlopen(path, RTLD_NOW | RTLD_LOCAL);
131     ALOGE_IF(!so, "dlopen(\"%s\") failed: %s", path, dlerror());
132     return so;
133 }
134 
135 #ifndef EGL_WRAPPER_DIR
136 #if defined(__LP64__)
137 #define EGL_WRAPPER_DIR "/system/lib64"
138 #else
139 #define EGL_WRAPPER_DIR "/system/lib"
140 #endif
141 #endif
142 
143 static const char* DRIVER_SUFFIX_PROPERTY = "ro.hardware.egl";
144 
145 static const char* HAL_SUBNAME_KEY_PROPERTIES[2] = {
146     DRIVER_SUFFIX_PROPERTY,
147     "ro.board.platform",
148 };
149 
should_unload_system_driver(egl_connection_t * cnx)150 static bool should_unload_system_driver(egl_connection_t* cnx) {
151     // Return false if the system driver has been unloaded once.
152     if (cnx->systemDriverUnloaded) {
153         return false;
154     }
155 
156     // Return true if Angle namespace is set.
157     android_namespace_t* ns = android::GraphicsEnv::getInstance().getAngleNamespace();
158     if (ns) {
159         return true;
160     }
161 
162 #ifndef __ANDROID_VNDK__
163     // Return true if updated driver namespace is set.
164     ns = android::GraphicsEnv::getInstance().getDriverNamespace();
165     if (ns) {
166         return true;
167     }
168 #endif
169 
170     return false;
171 }
172 
uninit_api(char const * const * api,__eglMustCastToProperFunctionPointerType * curr)173 static void uninit_api(char const* const* api, __eglMustCastToProperFunctionPointerType* curr) {
174     while (*api) {
175         *curr++ = nullptr;
176         api++;
177     }
178 }
179 
unload_system_driver(egl_connection_t * cnx)180 void Loader::unload_system_driver(egl_connection_t* cnx) {
181     ATRACE_CALL();
182 
183     uninit_api(gl_names,
184                (__eglMustCastToProperFunctionPointerType*)&cnx
185                        ->hooks[egl_connection_t::GLESv2_INDEX]
186                        ->gl);
187     uninit_api(gl_names,
188                (__eglMustCastToProperFunctionPointerType*)&cnx
189                        ->hooks[egl_connection_t::GLESv1_INDEX]
190                        ->gl);
191     uninit_api(egl_names, (__eglMustCastToProperFunctionPointerType*)&cnx->egl);
192 
193     if (cnx->dso) {
194         ALOGD("Unload system gl driver.");
195         driver_t* hnd = (driver_t*)cnx->dso;
196         if (hnd->dso[2]) {
197             do_android_unload_sphal_library(hnd->dso[2]);
198         }
199         if (hnd->dso[1]) {
200             do_android_unload_sphal_library(hnd->dso[1]);
201         }
202         if (hnd->dso[0]) {
203             do_android_unload_sphal_library(hnd->dso[0]);
204         }
205         cnx->dso = nullptr;
206     }
207 
208     cnx->systemDriverUnloaded = true;
209 }
210 
open(egl_connection_t * cnx)211 void* Loader::open(egl_connection_t* cnx)
212 {
213     ATRACE_CALL();
214     const nsecs_t openTime = systemTime();
215 
216     if (should_unload_system_driver(cnx)) {
217         unload_system_driver(cnx);
218     }
219 
220     // If a driver has been loaded, return the driver directly.
221     if (cnx->dso) {
222         return cnx->dso;
223     }
224 
225     // Firstly, try to load ANGLE driver.
226     driver_t* hnd = attempt_to_load_angle(cnx);
227     if (!hnd) {
228         // Secondly, try to load from driver apk.
229         hnd = attempt_to_load_updated_driver(cnx);
230     }
231 
232     bool failToLoadFromDriverSuffixProperty = false;
233     if (!hnd) {
234         // If updated driver apk is set but fail to load, abort here.
235         if (android::GraphicsEnv::getInstance().getDriverNamespace()) {
236             LOG_ALWAYS_FATAL("couldn't find an OpenGL ES implementation from %s",
237                              android::GraphicsEnv::getInstance().getDriverPath().c_str());
238         }
239         // Finally, try to load system driver, start by searching for the library name appended by
240         // the system properties of the GLES userspace driver in both locations.
241         // i.e.:
242         //      libGLES_${prop}.so, or:
243         //      libEGL_${prop}.so, libGLESv1_CM_${prop}.so, libGLESv2_${prop}.so
244         for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
245             auto prop = base::GetProperty(key, "");
246             if (prop.empty()) {
247                 continue;
248             }
249             hnd = attempt_to_load_system_driver(cnx, prop.c_str(), true);
250             if (hnd) {
251                 break;
252             } else if (strcmp(key, DRIVER_SUFFIX_PROPERTY) == 0) {
253                 failToLoadFromDriverSuffixProperty = true;
254             }
255         }
256     }
257 
258     if (!hnd) {
259         // Can't find graphics driver by appending system properties, now search for the exact name
260         // without any suffix of the GLES userspace driver in both locations.
261         // i.e.:
262         //      libGLES.so, or:
263         //      libEGL.so, libGLESv1_CM.so, libGLESv2.so
264         hnd = attempt_to_load_system_driver(cnx, nullptr, true);
265     }
266 
267     if (!hnd && !failToLoadFromDriverSuffixProperty) {
268         hnd = attempt_to_load_system_driver(cnx, nullptr, false);
269     }
270 
271     if (!hnd) {
272         android::GraphicsEnv::getInstance().setDriverLoaded(android::GpuStatsInfo::Api::API_GL,
273                                                             false, systemTime() - openTime);
274     } else {
275         // init_angle_backend will check if loaded driver is ANGLE or not,
276         // will set cnx->useAngle appropriately.
277         // Do this here so that we use ANGLE path when driver is ANGLE (e.g. loaded as native),
278         // not just loading ANGLE as option.
279         init_angle_backend(hnd->dso[0], cnx);
280     }
281 
282     LOG_ALWAYS_FATAL_IF(!hnd,
283                         "couldn't find an OpenGL ES implementation, make sure you set %s or %s",
284                         HAL_SUBNAME_KEY_PROPERTIES[0], HAL_SUBNAME_KEY_PROPERTIES[1]);
285 
286     if (!cnx->libEgl) {
287         cnx->libEgl = load_wrapper(EGL_WRAPPER_DIR "/libEGL.so");
288     }
289     if (!cnx->libGles1) {
290         cnx->libGles1 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv1_CM.so");
291     }
292     if (!cnx->libGles2) {
293         cnx->libGles2 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv2.so");
294     }
295 
296     if (!cnx->libEgl || !cnx->libGles2 || !cnx->libGles1) {
297         android::GraphicsEnv::getInstance().setDriverLoaded(android::GpuStatsInfo::Api::API_GL,
298                                                             false, systemTime() - openTime);
299     }
300 
301     LOG_ALWAYS_FATAL_IF(!cnx->libEgl,
302             "couldn't load system EGL wrapper libraries");
303 
304     LOG_ALWAYS_FATAL_IF(!cnx->libGles2 || !cnx->libGles1,
305                         "couldn't load system OpenGL ES wrapper libraries");
306 
307     android::GraphicsEnv::getInstance().setDriverLoaded(android::GpuStatsInfo::Api::API_GL, true,
308                                                         systemTime() - openTime);
309 
310     return (void*)hnd;
311 }
312 
close(egl_connection_t * cnx)313 void Loader::close(egl_connection_t* cnx)
314 {
315     driver_t* hnd = (driver_t*) cnx->dso;
316     delete hnd;
317     cnx->dso = nullptr;
318 
319     cnx->useAngle = false;
320 }
321 
init_api(void * dso,char const * const * api,char const * const * ref_api,__eglMustCastToProperFunctionPointerType * curr,getProcAddressType getProcAddress)322 void Loader::init_api(void* dso,
323         char const * const * api,
324         char const * const * ref_api,
325         __eglMustCastToProperFunctionPointerType* curr,
326         getProcAddressType getProcAddress)
327 {
328     ATRACE_CALL();
329 
330     const ssize_t SIZE = 256;
331     char scrap[SIZE];
332     while (*api) {
333         char const * name = *api;
334         if (ref_api) {
335             char const * ref_name = *ref_api;
336             if (std::strcmp(name, ref_name) != 0) {
337                 *curr++ = nullptr;
338                 ref_api++;
339                 continue;
340             }
341         }
342 
343         __eglMustCastToProperFunctionPointerType f =
344             (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
345         if (f == nullptr) {
346             // couldn't find the entry-point, use eglGetProcAddress()
347             f = getProcAddress(name);
348         }
349         if (f == nullptr) {
350             // Try without the OES postfix
351             ssize_t index = ssize_t(strlen(name)) - 3;
352             if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) {
353                 strncpy(scrap, name, index);
354                 scrap[index] = 0;
355                 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
356                 //ALOGD_IF(f, "found <%s> instead", scrap);
357             }
358         }
359         if (f == nullptr) {
360             // Try with the OES postfix
361             ssize_t index = ssize_t(strlen(name)) - 3;
362             if (index>0 && strcmp(name+index, "OES")) {
363                 snprintf(scrap, SIZE, "%sOES", name);
364                 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
365                 //ALOGD_IF(f, "found <%s> instead", scrap);
366             }
367         }
368         if (f == nullptr) {
369             //ALOGD("%s", name);
370             f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented;
371 
372             /*
373              * GL_EXT_debug_label is special, we always report it as
374              * supported, it's handled by GLES_trace. If GLES_trace is not
375              * enabled, then these are no-ops.
376              */
377             if (!strcmp(name, "glInsertEventMarkerEXT")) {
378                 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
379             } else if (!strcmp(name, "glPushGroupMarkerEXT")) {
380                 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
381             } else if (!strcmp(name, "glPopGroupMarkerEXT")) {
382                 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
383             }
384         }
385         *curr++ = f;
386         api++;
387         if (ref_api) ref_api++;
388     }
389 }
390 
load_system_driver(const char * kind,const char * suffix,const bool exact)391 static void* load_system_driver(const char* kind, const char* suffix, const bool exact) {
392     ATRACE_CALL();
393     class MatchFile {
394     public:
395         static std::string find(const char* libraryName, const bool exact) {
396             const char* const searchPaths[] = {
397 #if defined(__LP64__)
398                     "/vendor/lib64/egl",
399                     "/system/lib64/egl"
400 #else
401                     "/vendor/lib/egl",
402                     "/system/lib/egl"
403 #endif
404             };
405 
406             for (auto dir : searchPaths) {
407                 std::string absolutePath;
408                 if (find(absolutePath, libraryName, dir, exact)) {
409                     return absolutePath;
410                 }
411             }
412 
413             // Driver not found. gah.
414             return std::string();
415         }
416     private:
417         static bool find(std::string& result,
418                 const std::string& pattern, const char* const search, bool exact) {
419             if (exact) {
420                 std::string absolutePath = std::string(search) + "/" + pattern + ".so";
421                 if (!access(absolutePath.c_str(), R_OK)) {
422                     result = absolutePath;
423                     return true;
424                 }
425                 return false;
426             }
427 
428             DIR* d = opendir(search);
429             if (d != nullptr) {
430                 struct dirent* e;
431                 while ((e = readdir(d)) != nullptr) {
432                     if (e->d_type == DT_DIR) {
433                         continue;
434                     }
435                     if (!strcmp(e->d_name, "libGLES_android.so")) {
436                         // always skip the software renderer
437                         continue;
438                     }
439                     if (strstr(e->d_name, pattern.c_str()) == e->d_name) {
440                         if (!strcmp(e->d_name + strlen(e->d_name) - 3, ".so")) {
441                             result = std::string(search) + "/" + e->d_name;
442                             closedir(d);
443                             return true;
444                         }
445                     }
446                 }
447                 closedir(d);
448             }
449             return false;
450         }
451     };
452 
453     std::string libraryName = std::string("lib") + kind;
454     if (suffix) {
455         libraryName += std::string("_") + suffix;
456     } else if (!exact) {
457         // Deprecated: we look for files that match
458         //      libGLES_*.so, or:
459         //      libEGL_*.so, libGLESv1_CM_*.so, libGLESv2_*.so
460         libraryName += std::string("_");
461     }
462     std::string absolutePath = MatchFile::find(libraryName.c_str(), exact);
463     if (absolutePath.empty()) {
464         // this happens often, we don't want to log an error
465         return nullptr;
466     }
467     const char* const driver_absolute_path = absolutePath.c_str();
468 
469     // Try to load drivers from the 'sphal' namespace, if it exist. Fall back to
470     // the original routine when the namespace does not exist.
471     // See /system/core/rootdir/etc/ld.config.txt for the configuration of the
472     // sphal namespace.
473     void* dso = do_android_load_sphal_library(driver_absolute_path,
474                                               RTLD_NOW | RTLD_LOCAL);
475     if (dso == nullptr) {
476         const char* err = dlerror();
477         ALOGE("load_driver(%s): %s", driver_absolute_path, err ? err : "unknown");
478         return nullptr;
479     }
480 
481     ALOGD("loaded %s", driver_absolute_path);
482 
483     return dso;
484 }
485 
load_angle(const char * kind,android_namespace_t * ns)486 static void* load_angle(const char* kind, android_namespace_t* ns) {
487     const android_dlextinfo dlextinfo = {
488             .flags = ANDROID_DLEXT_USE_NAMESPACE,
489             .library_namespace = ns,
490     };
491 
492     std::string name = std::string("lib") + kind + "_angle.so";
493 
494     void* so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
495 
496     if (so) {
497         ALOGD("dlopen_ext from APK (%s) success at %p", name.c_str(), so);
498         return so;
499     } else {
500         ALOGE("dlopen_ext(\"%s\") failed: %s", name.c_str(), dlerror());
501     }
502 
503     return nullptr;
504 }
505 
load_updated_driver(const char * kind,android_namespace_t * ns)506 static void* load_updated_driver(const char* kind, android_namespace_t* ns) {
507     ATRACE_CALL();
508     const android_dlextinfo dlextinfo = {
509         .flags = ANDROID_DLEXT_USE_NAMESPACE,
510         .library_namespace = ns,
511     };
512     void* so = nullptr;
513     for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
514         auto prop = base::GetProperty(key, "");
515         if (prop.empty()) {
516             continue;
517         }
518         std::string name = std::string("lib") + kind + "_" + prop + ".so";
519         so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
520         if (so) {
521             return so;
522         }
523     }
524     return nullptr;
525 }
526 
attempt_to_load_angle(egl_connection_t * cnx)527 Loader::driver_t* Loader::attempt_to_load_angle(egl_connection_t* cnx) {
528     ATRACE_CALL();
529 
530     if (!android::GraphicsEnv::getInstance().shouldUseAngle()) {
531         return nullptr;
532     }
533 
534     android_namespace_t* ns = android::GraphicsEnv::getInstance().getAngleNamespace();
535     if (!ns) {
536         return nullptr;
537     }
538 
539     android::GraphicsEnv::getInstance().setDriverToLoad(android::GpuStatsInfo::Driver::ANGLE);
540     driver_t* hnd = nullptr;
541 
542     // ANGLE doesn't ship with GLES library, and thus we skip GLES driver.
543     void* dso = load_angle("EGL", ns);
544     if (dso) {
545         initialize_api(dso, cnx, EGL);
546         hnd = new driver_t(dso);
547 
548         dso = load_angle("GLESv1_CM", ns);
549         initialize_api(dso, cnx, GLESv1_CM);
550         hnd->set(dso, GLESv1_CM);
551 
552         dso = load_angle("GLESv2", ns);
553         initialize_api(dso, cnx, GLESv2);
554         hnd->set(dso, GLESv2);
555     }
556     return hnd;
557 }
558 
init_angle_backend(void * dso,egl_connection_t * cnx)559 void Loader::init_angle_backend(void* dso, egl_connection_t* cnx) {
560     void* eglCreateDeviceANGLE = nullptr;
561 
562     ALOGV("dso: %p", dso);
563     eglCreateDeviceANGLE = dlsym(dso, "eglCreateDeviceANGLE");
564     ALOGV("eglCreateDeviceANGLE: %p", eglCreateDeviceANGLE);
565     if (eglCreateDeviceANGLE) {
566         ALOGV("ANGLE GLES library in use");
567         cnx->useAngle = true;
568     } else {
569         ALOGV("Native GLES library in use");
570         cnx->useAngle = false;
571     }
572 }
573 
attempt_to_load_updated_driver(egl_connection_t * cnx)574 Loader::driver_t* Loader::attempt_to_load_updated_driver(egl_connection_t* cnx) {
575     ATRACE_CALL();
576 #ifndef __ANDROID_VNDK__
577     android_namespace_t* ns = android::GraphicsEnv::getInstance().getDriverNamespace();
578     if (!ns) {
579         return nullptr;
580     }
581 
582     ALOGD("Load updated gl driver.");
583     android::GraphicsEnv::getInstance().setDriverToLoad(android::GpuStatsInfo::Driver::GL_UPDATED);
584     driver_t* hnd = nullptr;
585     void* dso = load_updated_driver("GLES", ns);
586     if (dso) {
587         initialize_api(dso, cnx, EGL | GLESv1_CM | GLESv2);
588         hnd = new driver_t(dso);
589         return hnd;
590     }
591 
592     dso = load_updated_driver("EGL", ns);
593     if (dso) {
594         initialize_api(dso, cnx, EGL);
595         hnd = new driver_t(dso);
596 
597         dso = load_updated_driver("GLESv1_CM", ns);
598         initialize_api(dso, cnx, GLESv1_CM);
599         hnd->set(dso, GLESv1_CM);
600 
601         dso = load_updated_driver("GLESv2", ns);
602         initialize_api(dso, cnx, GLESv2);
603         hnd->set(dso, GLESv2);
604     }
605     return hnd;
606 #else
607     return nullptr;
608 #endif
609 }
610 
attempt_to_load_system_driver(egl_connection_t * cnx,const char * suffix,const bool exact)611 Loader::driver_t* Loader::attempt_to_load_system_driver(egl_connection_t* cnx, const char* suffix,
612                                                         const bool exact) {
613     ATRACE_CALL();
614     android::GraphicsEnv::getInstance().setDriverToLoad(android::GpuStatsInfo::Driver::GL);
615     driver_t* hnd = nullptr;
616     void* dso = load_system_driver("GLES", suffix, exact);
617     if (dso) {
618         initialize_api(dso, cnx, EGL | GLESv1_CM | GLESv2);
619         hnd = new driver_t(dso);
620         return hnd;
621     }
622     dso = load_system_driver("EGL", suffix, exact);
623     if (dso) {
624         initialize_api(dso, cnx, EGL);
625         hnd = new driver_t(dso);
626 
627         dso = load_system_driver("GLESv1_CM", suffix, exact);
628         initialize_api(dso, cnx, GLESv1_CM);
629         hnd->set(dso, GLESv1_CM);
630 
631         dso = load_system_driver("GLESv2", suffix, exact);
632         initialize_api(dso, cnx, GLESv2);
633         hnd->set(dso, GLESv2);
634     }
635     return hnd;
636 }
637 
initialize_api(void * dso,egl_connection_t * cnx,uint32_t mask)638 void Loader::initialize_api(void* dso, egl_connection_t* cnx, uint32_t mask) {
639     if (mask & EGL) {
640         getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress");
641 
642         ALOGE_IF(!getProcAddress,
643                 "can't find eglGetProcAddress() in EGL driver library");
644 
645         egl_t* egl = &cnx->egl;
646         __eglMustCastToProperFunctionPointerType* curr =
647             (__eglMustCastToProperFunctionPointerType*)egl;
648         char const * const * api = egl_names;
649         while (*api) {
650             char const * name = *api;
651             __eglMustCastToProperFunctionPointerType f =
652                 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
653             if (f == nullptr) {
654                 // couldn't find the entry-point, use eglGetProcAddress()
655                 f = getProcAddress(name);
656                 if (f == nullptr) {
657                     f = (__eglMustCastToProperFunctionPointerType)nullptr;
658                 }
659             }
660             *curr++ = f;
661             api++;
662         }
663     }
664 
665     if (mask & GLESv1_CM) {
666         init_api(dso, gl_names_1, gl_names,
667             (__eglMustCastToProperFunctionPointerType*)
668                 &cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl,
669             getProcAddress);
670     }
671 
672     if (mask & GLESv2) {
673         init_api(dso, gl_names, nullptr,
674             (__eglMustCastToProperFunctionPointerType*)
675                 &cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl,
676             getProcAddress);
677     }
678 }
679 
680 } // namespace android
681