blob: 32f8caa9890319ddeea0eb2aa880aec9307fb713 [file] [log] [blame]
Jesse Hall94cdba92013-07-11 09:40:35 -07001/*
Mathias Agopiande586972009-05-28 17:39:03 -07002 ** Copyright 2007, The Android Open Source Project
3 **
Jesse Hall94cdba92013-07-11 09:40:35 -07004 ** 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
Mathias Agopiande586972009-05-28 17:39:03 -07007 **
Jesse Hall94cdba92013-07-11 09:40:35 -07008 ** http://www.apache.org/licenses/LICENSE-2.0
Mathias Agopiande586972009-05-28 17:39:03 -07009 **
Jesse Hall94cdba92013-07-11 09:40:35 -070010 ** 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
Mathias Agopiande586972009-05-28 17:39:03 -070014 ** limitations under the License.
15 */
16
Jesse Hall7a8d83e2016-12-20 15:24:28 -080017//#define LOG_NDEBUG 0
Jesse Hall1508ae62017-01-19 17:43:26 -080018#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Jesse Hall7a8d83e2016-12-20 15:24:28 -080019
Mathias Agopian311b4792017-02-28 15:00:49 -080020#include "Loader.h"
21
Mathias Agopian65421432017-03-08 11:49:05 -080022#include <string>
23
Mathias Agopian99381422013-04-23 20:52:29 +020024#include <dirent.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070025#include <dlfcn.h>
Mathias Agopiande586972009-05-28 17:39:03 -070026
Jesse Hall7a8d83e2016-12-20 15:24:28 -080027#include <android/dlext.h>
David 'Digit' Turner80b30c22011-08-26 17:38:47 +020028#include <cutils/properties.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070029#include <log/log.h>
Mathias Agopian311b4792017-02-28 15:00:49 -080030
Jiyong Parka243e5d2017-06-21 12:26:51 +090031#ifndef __ANDROID_VNDK__
Jiyong Park27c39e12017-05-08 13:00:02 +090032#include <graphicsenv/GraphicsEnv.h>
Jiyong Parka243e5d2017-06-21 12:26:51 +090033#endif
Justin Yunb7320302017-05-22 15:13:40 +090034#include <vndksupport/linker.h>
Mathias Agopiande586972009-05-28 17:39:03 -070035
Mathias Agopian65421432017-03-08 11:49:05 -080036#include "egl_trace.h"
Mathias Agopian1cadb252011-05-23 17:26:14 -070037#include "egldefs.h"
Mathias Agopiande586972009-05-28 17:39:03 -070038
Jiyong Park5910dc72017-04-05 14:23:52 +090039extern "C" {
40 android_namespace_t* android_get_exported_namespace(const char*);
41}
42
Mathias Agopiande586972009-05-28 17:39:03 -070043// ----------------------------------------------------------------------------
44namespace android {
45// ----------------------------------------------------------------------------
46
47
48/*
Mathias Agopian99381422013-04-23 20:52:29 +020049 * EGL userspace drivers must be provided either:
50 * - as a single library:
51 * /vendor/lib/egl/libGLES.so
52 *
53 * - as separate libraries:
54 * /vendor/lib/egl/libEGL.so
55 * /vendor/lib/egl/libGLESv1_CM.so
56 * /vendor/lib/egl/libGLESv2.so
57 *
58 * The software renderer for the emulator must be provided as a single
59 * library at:
60 *
61 * /system/lib/egl/libGLES_android.so
62 *
63 *
64 * For backward compatibility and to facilitate the transition to
65 * this new naming scheme, the loader will additionally look for:
Jesse Hall94cdba92013-07-11 09:40:35 -070066 *
Mathias Agopian99381422013-04-23 20:52:29 +020067 * /{vendor|system}/lib/egl/lib{GLES | [EGL|GLESv1_CM|GLESv2]}_*.so
Jesse Hall94cdba92013-07-11 09:40:35 -070068 *
Mathias Agopiande586972009-05-28 17:39:03 -070069 */
70
Mathias Agopian65421432017-03-08 11:49:05 -080071Loader& Loader::getInstance() {
72 static Loader loader;
73 return loader;
74}
Mathias Agopiande586972009-05-28 17:39:03 -070075
David 'Digit' Turner80b30c22011-08-26 17:38:47 +020076/* This function is called to check whether we run inside the emulator,
77 * and if this is the case whether GLES GPU emulation is supported.
78 *
79 * Returned values are:
80 * -1 -> not running inside the emulator
81 * 0 -> running inside the emulator, but GPU emulation not supported
82 * 1 -> running inside the emulator, GPU emulation is supported
Nicolas Capens776951f2015-11-06 10:10:21 -050083 * through the "emulation" host-side OpenGL ES implementation.
84 * 2 -> running inside the emulator, GPU emulation is supported
85 * through a guest-side vendor driver's OpenGL ES implementation.
David 'Digit' Turner80b30c22011-08-26 17:38:47 +020086 */
87static int
88checkGlesEmulationStatus(void)
89{
90 /* We're going to check for the following kernel parameters:
91 *
92 * qemu=1 -> tells us that we run inside the emulator
93 * android.qemu.gles=<number> -> tells us the GLES GPU emulation status
94 *
95 * Note that we will return <number> if we find it. This let us support
96 * more additionnal emulation modes in the future.
97 */
98 char prop[PROPERTY_VALUE_MAX];
99 int result = -1;
100
101 /* First, check for qemu=1 */
102 property_get("ro.kernel.qemu",prop,"0");
103 if (atoi(prop) != 1)
104 return -1;
105
106 /* We are in the emulator, get GPU status value */
bohu69e5b1a2016-02-19 17:15:20 -0800107 property_get("qemu.gles",prop,"0");
David 'Digit' Turner80b30c22011-08-26 17:38:47 +0200108 return atoi(prop);
109}
110
Jesse Hall1508ae62017-01-19 17:43:26 -0800111static void* do_dlopen(const char* path, int mode) {
112 ATRACE_CALL();
113 return dlopen(path, mode);
114}
115
Jiyong Park5910dc72017-04-05 14:23:52 +0900116static void* do_android_dlopen_ext(const char* path, int mode, const android_dlextinfo* info) {
117 ATRACE_CALL();
118 return android_dlopen_ext(path, mode, info);
119}
120
Justin Yunb7320302017-05-22 15:13:40 +0900121static void* do_android_load_sphal_library(const char* path, int mode) {
122 ATRACE_CALL();
123 return android_load_sphal_library(path, mode);
124}
125
Mathias Agopiande586972009-05-28 17:39:03 -0700126// ----------------------------------------------------------------------------
127
Jesse Hall94cdba92013-07-11 09:40:35 -0700128Loader::driver_t::driver_t(void* gles)
Mathias Agopiande586972009-05-28 17:39:03 -0700129{
130 dso[0] = gles;
131 for (size_t i=1 ; i<NELEM(dso) ; i++)
132 dso[i] = 0;
133}
134
Jesse Hall94cdba92013-07-11 09:40:35 -0700135Loader::driver_t::~driver_t()
Mathias Agopiande586972009-05-28 17:39:03 -0700136{
137 for (size_t i=0 ; i<NELEM(dso) ; i++) {
138 if (dso[i]) {
139 dlclose(dso[i]);
140 dso[i] = 0;
141 }
142 }
143}
144
Mathias Agopian65421432017-03-08 11:49:05 -0800145int Loader::driver_t::set(void* hnd, int32_t api)
Mathias Agopiande586972009-05-28 17:39:03 -0700146{
147 switch (api) {
148 case EGL:
149 dso[0] = hnd;
150 break;
151 case GLESv1_CM:
152 dso[1] = hnd;
153 break;
154 case GLESv2:
155 dso[2] = hnd;
156 break;
157 default:
Mathias Agopian65421432017-03-08 11:49:05 -0800158 return -EOVERFLOW;
Mathias Agopiande586972009-05-28 17:39:03 -0700159 }
Mathias Agopian65421432017-03-08 11:49:05 -0800160 return 0;
Mathias Agopiande586972009-05-28 17:39:03 -0700161}
162
163// ----------------------------------------------------------------------------
164
Mathias Agopiande586972009-05-28 17:39:03 -0700165Loader::Loader()
Mathias Agopian991d2542017-02-06 13:51:32 -0800166 : getProcAddress(NULL)
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800167{
Mathias Agopiande586972009-05-28 17:39:03 -0700168}
169
Mathias Agopian99381422013-04-23 20:52:29 +0200170Loader::~Loader() {
Mathias Agopiande586972009-05-28 17:39:03 -0700171}
172
Jesse Hallc07b5202013-07-04 12:08:16 -0700173static void* load_wrapper(const char* path) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800174 void* so = do_dlopen(path, RTLD_NOW | RTLD_LOCAL);
Jesse Hallc07b5202013-07-04 12:08:16 -0700175 ALOGE_IF(!so, "dlopen(\"%s\") failed: %s", path, dlerror());
176 return so;
177}
178
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700179#ifndef EGL_WRAPPER_DIR
180#if defined(__LP64__)
181#define EGL_WRAPPER_DIR "/system/lib64"
182#else
183#define EGL_WRAPPER_DIR "/system/lib"
184#endif
185#endif
186
bohu69e5b1a2016-02-19 17:15:20 -0800187static void setEmulatorGlesValue(void) {
188 char prop[PROPERTY_VALUE_MAX];
189 property_get("ro.kernel.qemu", prop, "0");
190 if (atoi(prop) != 1) return;
191
192 property_get("ro.kernel.qemu.gles",prop,"0");
193 if (atoi(prop) == 1) {
194 ALOGD("Emulator has host GPU support, qemu.gles is set to 1.");
195 property_set("qemu.gles", "1");
196 return;
197 }
198
199 // for now, checking the following
200 // directory is good enough for emulator system images
201 const char* vendor_lib_path =
202#if defined(__LP64__)
203 "/vendor/lib64/egl";
204#else
205 "/vendor/lib/egl";
206#endif
207
208 const bool has_vendor_lib = (access(vendor_lib_path, R_OK) == 0);
209 if (has_vendor_lib) {
210 ALOGD("Emulator has vendor provided software renderer, qemu.gles is set to 2.");
211 property_set("qemu.gles", "2");
212 } else {
213 ALOGD("Emulator without GPU support detected. "
214 "Fallback to legacy software renderer, qemu.gles is set to 0.");
215 property_set("qemu.gles", "0");
216 }
217}
218
Mathias Agopianada798b2012-02-13 17:09:30 -0800219void* Loader::open(egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700220{
Jesse Hall1508ae62017-01-19 17:43:26 -0800221 ATRACE_CALL();
222
Mathias Agopiande586972009-05-28 17:39:03 -0700223 void* dso;
Mathias Agopiande586972009-05-28 17:39:03 -0700224 driver_t* hnd = 0;
Jesse Hall94cdba92013-07-11 09:40:35 -0700225
bohu69e5b1a2016-02-19 17:15:20 -0800226 setEmulatorGlesValue();
227
Mathias Agopian99381422013-04-23 20:52:29 +0200228 dso = load_driver("GLES", cnx, EGL | GLESv1_CM | GLESv2);
229 if (dso) {
230 hnd = new driver_t(dso);
231 } else {
232 // Always load EGL first
233 dso = load_driver("EGL", cnx, EGL);
Mathias Agopiande586972009-05-28 17:39:03 -0700234 if (dso) {
235 hnd = new driver_t(dso);
Mathias Agopian99381422013-04-23 20:52:29 +0200236 hnd->set( load_driver("GLESv1_CM", cnx, GLESv1_CM), GLESv1_CM );
237 hnd->set( load_driver("GLESv2", cnx, GLESv2), GLESv2 );
Mathias Agopiande586972009-05-28 17:39:03 -0700238 }
239 }
240
Mathias Agopian99381422013-04-23 20:52:29 +0200241 LOG_ALWAYS_FATAL_IF(!hnd, "couldn't find an OpenGL ES implementation");
Jesse Hallc07b5202013-07-04 12:08:16 -0700242
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700243 cnx->libEgl = load_wrapper(EGL_WRAPPER_DIR "/libEGL.so");
244 cnx->libGles2 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv2.so");
245 cnx->libGles1 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv1_CM.so");
246
Michael Chockc0ec5e22014-01-27 08:14:33 -0800247 LOG_ALWAYS_FATAL_IF(!cnx->libEgl,
248 "couldn't load system EGL wrapper libraries");
249
Jesse Hallc07b5202013-07-04 12:08:16 -0700250 LOG_ALWAYS_FATAL_IF(!cnx->libGles2 || !cnx->libGles1,
251 "couldn't load system OpenGL ES wrapper libraries");
252
Mathias Agopiande586972009-05-28 17:39:03 -0700253 return (void*)hnd;
254}
255
Mathias Agopian65421432017-03-08 11:49:05 -0800256void Loader::close(void* driver)
Mathias Agopiande586972009-05-28 17:39:03 -0700257{
258 driver_t* hnd = (driver_t*)driver;
259 delete hnd;
Mathias Agopiande586972009-05-28 17:39:03 -0700260}
261
Jesse Hall94cdba92013-07-11 09:40:35 -0700262void Loader::init_api(void* dso,
263 char const * const * api,
264 __eglMustCastToProperFunctionPointerType* curr,
265 getProcAddressType getProcAddress)
Mathias Agopiande586972009-05-28 17:39:03 -0700266{
Jesse Hall1508ae62017-01-19 17:43:26 -0800267 ATRACE_CALL();
268
Mathias Agopian7773c432012-02-13 20:06:08 -0800269 const ssize_t SIZE = 256;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700270 char scrap[SIZE];
Mathias Agopiande586972009-05-28 17:39:03 -0700271 while (*api) {
272 char const * name = *api;
Jesse Hall94cdba92013-07-11 09:40:35 -0700273 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700274 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
275 if (f == NULL) {
276 // couldn't find the entry-point, use eglGetProcAddress()
277 f = getProcAddress(name);
278 }
279 if (f == NULL) {
280 // Try without the OES postfix
281 ssize_t index = ssize_t(strlen(name)) - 3;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700282 if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) {
Mathias Agopiande586972009-05-28 17:39:03 -0700283 strncpy(scrap, name, index);
284 scrap[index] = 0;
285 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000286 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700287 }
288 }
289 if (f == NULL) {
290 // Try with the OES postfix
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700291 ssize_t index = ssize_t(strlen(name)) - 3;
292 if (index>0 && strcmp(name+index, "OES")) {
293 snprintf(scrap, SIZE, "%sOES", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700294 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000295 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700296 }
297 }
298 if (f == NULL) {
Steve Block9d453682011-12-20 16:23:08 +0000299 //ALOGD("%s", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700300 f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented;
Mathias Agopian48d438d2012-01-28 21:44:00 -0800301
302 /*
303 * GL_EXT_debug_label is special, we always report it as
304 * supported, it's handled by GLES_trace. If GLES_trace is not
305 * enabled, then these are no-ops.
306 */
307 if (!strcmp(name, "glInsertEventMarkerEXT")) {
308 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
309 } else if (!strcmp(name, "glPushGroupMarkerEXT")) {
310 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
311 } else if (!strcmp(name, "glPopGroupMarkerEXT")) {
312 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
313 }
Mathias Agopiande586972009-05-28 17:39:03 -0700314 }
315 *curr++ = f;
316 api++;
317 }
318}
319
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800320static void* load_system_driver(const char* kind) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800321 ATRACE_CALL();
Mathias Agopian99381422013-04-23 20:52:29 +0200322 class MatchFile {
323 public:
Mathias Agopian65421432017-03-08 11:49:05 -0800324 static std::string find(const char* kind) {
325 std::string result;
Nicolas Capens776951f2015-11-06 10:10:21 -0500326 int emulationStatus = checkGlesEmulationStatus();
327 switch (emulationStatus) {
328 case 0:
Nicolas Capens776951f2015-11-06 10:10:21 -0500329#if defined(__LP64__)
Mathias Agopian65421432017-03-08 11:49:05 -0800330 result = "/system/lib64/egl/libGLES_android.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500331#else
Mathias Agopian65421432017-03-08 11:49:05 -0800332 result = "/system/lib/egl/libGLES_android.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500333#endif
334 return result;
335 case 1:
336 // Use host-side OpenGL through the "emulation" library
337#if defined(__LP64__)
Mathias Agopian65421432017-03-08 11:49:05 -0800338 result = std::string("/system/lib64/egl/lib") + kind + "_emulation.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500339#else
Mathias Agopian65421432017-03-08 11:49:05 -0800340 result = std::string("/system/lib/egl/lib") + kind + "_emulation.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500341#endif
342 return result;
343 default:
344 // Not in emulator, or use other guest-side implementation
345 break;
346 }
347
Mathias Agopian65421432017-03-08 11:49:05 -0800348 std::string pattern = std::string("lib") + kind;
Mathias Agopian99381422013-04-23 20:52:29 +0200349 const char* const searchPaths[] = {
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800350#if defined(__LP64__)
351 "/vendor/lib64/egl",
352 "/system/lib64/egl"
353#else
Mathias Agopian99381422013-04-23 20:52:29 +0200354 "/vendor/lib/egl",
355 "/system/lib/egl"
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800356#endif
Mathias Agopian99381422013-04-23 20:52:29 +0200357 };
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700358
Mathias Agopian99381422013-04-23 20:52:29 +0200359 // first, we search for the exact name of the GLES userspace
360 // driver in both locations.
361 // i.e.:
362 // libGLES.so, or:
363 // libEGL.so, libGLESv1_CM.so, libGLESv2.so
364
365 for (size_t i=0 ; i<NELEM(searchPaths) ; i++) {
366 if (find(result, pattern, searchPaths[i], true)) {
367 return result;
368 }
369 }
370
371 // for compatibility with the old "egl.cfg" naming convention
372 // we look for files that match:
373 // libGLES_*.so, or:
374 // libEGL_*.so, libGLESv1_CM_*.so, libGLESv2_*.so
375
376 pattern.append("_");
377 for (size_t i=0 ; i<NELEM(searchPaths) ; i++) {
378 if (find(result, pattern, searchPaths[i], false)) {
379 return result;
380 }
381 }
382
383 // we didn't find the driver. gah.
384 result.clear();
385 return result;
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700386 }
Mathias Agopian99381422013-04-23 20:52:29 +0200387
388 private:
Mathias Agopian65421432017-03-08 11:49:05 -0800389 static bool find(std::string& result,
390 const std::string& pattern, const char* const search, bool exact) {
Mathias Agopian99381422013-04-23 20:52:29 +0200391 if (exact) {
Mathias Agopian65421432017-03-08 11:49:05 -0800392 std::string absolutePath = std::string(search) + "/" + pattern;
393 if (!access(absolutePath.c_str(), R_OK)) {
Mathias Agopian99381422013-04-23 20:52:29 +0200394 result = absolutePath;
395 return true;
396 }
397 return false;
398 }
399
400 DIR* d = opendir(search);
401 if (d != NULL) {
402 struct dirent cur;
403 struct dirent* e;
404 while (readdir_r(d, &cur, &e) == 0 && e) {
405 if (e->d_type == DT_DIR) {
406 continue;
407 }
408 if (!strcmp(e->d_name, "libGLES_android.so")) {
409 // always skip the software renderer
410 continue;
411 }
Mathias Agopian65421432017-03-08 11:49:05 -0800412 if (strstr(e->d_name, pattern.c_str()) == e->d_name) {
Mathias Agopian99381422013-04-23 20:52:29 +0200413 if (!strcmp(e->d_name + strlen(e->d_name) - 3, ".so")) {
Mathias Agopian65421432017-03-08 11:49:05 -0800414 result = std::string(search) + "/" + e->d_name;
Mathias Agopian99381422013-04-23 20:52:29 +0200415 closedir(d);
416 return true;
417 }
418 }
419 }
420 closedir(d);
421 }
422 return false;
423 }
424 };
425
426
Mathias Agopian65421432017-03-08 11:49:05 -0800427 std::string absolutePath = MatchFile::find(kind);
428 if (absolutePath.empty()) {
Mathias Agopian99381422013-04-23 20:52:29 +0200429 // this happens often, we don't want to log an error
430 return 0;
Mathias Agopian8c173842009-09-20 16:01:02 -0700431 }
Mathias Agopian65421432017-03-08 11:49:05 -0800432 const char* const driver_absolute_path = absolutePath.c_str();
Mathias Agopiande586972009-05-28 17:39:03 -0700433
Jiyong Park5910dc72017-04-05 14:23:52 +0900434 // Try to load drivers from the 'sphal' namespace, if it exist. Fall back to
Justin Yunb7320302017-05-22 15:13:40 +0900435 // the original routine when the namespace does not exist.
Jiyong Park5910dc72017-04-05 14:23:52 +0900436 // See /system/core/rootdir/etc/ld.config.txt for the configuration of the
437 // sphal namespace.
Justin Yunb7320302017-05-22 15:13:40 +0900438 void* dso = do_android_load_sphal_library(driver_absolute_path,
439 RTLD_NOW | RTLD_LOCAL);
Mathias Agopian8c173842009-09-20 16:01:02 -0700440 if (dso == 0) {
441 const char* err = dlerror();
Mathias Agopian65421432017-03-08 11:49:05 -0800442 ALOGE("load_driver(%s): %s", driver_absolute_path, err ? err : "unknown");
Mathias Agopian8c173842009-09-20 16:01:02 -0700443 return 0;
444 }
445
Steve Block9d453682011-12-20 16:23:08 +0000446 ALOGD("loaded %s", driver_absolute_path);
Mathias Agopianbaca89c2009-08-20 19:09:34 -0700447
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800448 return dso;
449}
450
Mathias Agopian311b4792017-02-28 15:00:49 -0800451static const char* HAL_SUBNAME_KEY_PROPERTIES[2] = {
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800452 "ro.hardware.egl",
453 "ro.board.platform",
Mathias Agopian311b4792017-02-28 15:00:49 -0800454};
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800455
456static void* load_updated_driver(const char* kind, android_namespace_t* ns) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800457 ATRACE_CALL();
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800458 const android_dlextinfo dlextinfo = {
459 .flags = ANDROID_DLEXT_USE_NAMESPACE,
460 .library_namespace = ns,
461 };
462 void* so = nullptr;
463 char prop[PROPERTY_VALUE_MAX + 1];
464 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
465 if (property_get(key, prop, nullptr) > 0) {
Mathias Agopian65421432017-03-08 11:49:05 -0800466 std::string name = std::string("lib") + kind + "_" + prop + ".so";
467 so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
Mathias Agopian311b4792017-02-28 15:00:49 -0800468 if (so) {
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800469 return so;
Mathias Agopian311b4792017-02-28 15:00:49 -0800470 }
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800471 }
472 }
473 return nullptr;
474}
475
476void *Loader::load_driver(const char* kind,
477 egl_connection_t* cnx, uint32_t mask)
478{
Jesse Hall1508ae62017-01-19 17:43:26 -0800479 ATRACE_CALL();
480
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800481 void* dso = nullptr;
Jiyong Parka243e5d2017-06-21 12:26:51 +0900482#ifndef __ANDROID_VNDK__
Mathias Agopian991d2542017-02-06 13:51:32 -0800483 android_namespace_t* ns = android_getDriverNamespace();
484 if (ns) {
485 dso = load_updated_driver(kind, ns);
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800486 }
Jiyong Parka243e5d2017-06-21 12:26:51 +0900487#endif
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800488 if (!dso) {
489 dso = load_system_driver(kind);
490 if (!dso)
491 return NULL;
492 }
493
Mathias Agopiande586972009-05-28 17:39:03 -0700494 if (mask & EGL) {
495 getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress");
496
Jesse Hall94cdba92013-07-11 09:40:35 -0700497 ALOGE_IF(!getProcAddress,
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800498 "can't find eglGetProcAddress() in EGL driver library");
Mathias Agopiande586972009-05-28 17:39:03 -0700499
Mathias Agopian618fa102009-10-14 02:06:37 -0700500 egl_t* egl = &cnx->egl;
Mathias Agopiande586972009-05-28 17:39:03 -0700501 __eglMustCastToProperFunctionPointerType* curr =
502 (__eglMustCastToProperFunctionPointerType*)egl;
503 char const * const * api = egl_names;
504 while (*api) {
505 char const * name = *api;
Jesse Hall94cdba92013-07-11 09:40:35 -0700506 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700507 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
508 if (f == NULL) {
509 // couldn't find the entry-point, use eglGetProcAddress()
510 f = getProcAddress(name);
511 if (f == NULL) {
512 f = (__eglMustCastToProperFunctionPointerType)0;
513 }
514 }
515 *curr++ = f;
516 api++;
517 }
518 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700519
Mathias Agopiande586972009-05-28 17:39:03 -0700520 if (mask & GLESv1_CM) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700521 init_api(dso, gl_names,
522 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800523 &cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl,
Mathias Agopian618fa102009-10-14 02:06:37 -0700524 getProcAddress);
Mathias Agopiande586972009-05-28 17:39:03 -0700525 }
526
527 if (mask & GLESv2) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700528 init_api(dso, gl_names,
529 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800530 &cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl,
Mathias Agopiande586972009-05-28 17:39:03 -0700531 getProcAddress);
532 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700533
Mathias Agopiande586972009-05-28 17:39:03 -0700534 return dso;
535}
536
537// ----------------------------------------------------------------------------
538}; // namespace android
539// ----------------------------------------------------------------------------