blob: 6e5c510d8da0bf3330e377a2bfb6aa2b3857c207 [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 Park27c39e12017-05-08 13:00:02 +090031#include <graphicsenv/GraphicsEnv.h>
Justin Yunb7320302017-05-22 15:13:40 +090032#include <vndksupport/linker.h>
Mathias Agopiande586972009-05-28 17:39:03 -070033
Mathias Agopian65421432017-03-08 11:49:05 -080034#include "egl_trace.h"
Mathias Agopian1cadb252011-05-23 17:26:14 -070035#include "egldefs.h"
Mathias Agopiande586972009-05-28 17:39:03 -070036
Jiyong Park5910dc72017-04-05 14:23:52 +090037extern "C" {
38 android_namespace_t* android_get_exported_namespace(const char*);
39}
40
Mathias Agopiande586972009-05-28 17:39:03 -070041// ----------------------------------------------------------------------------
42namespace android {
43// ----------------------------------------------------------------------------
44
45
46/*
Mathias Agopian99381422013-04-23 20:52:29 +020047 * EGL userspace drivers must be provided either:
48 * - as a single library:
49 * /vendor/lib/egl/libGLES.so
50 *
51 * - as separate libraries:
52 * /vendor/lib/egl/libEGL.so
53 * /vendor/lib/egl/libGLESv1_CM.so
54 * /vendor/lib/egl/libGLESv2.so
55 *
56 * The software renderer for the emulator must be provided as a single
57 * library at:
58 *
59 * /system/lib/egl/libGLES_android.so
60 *
61 *
62 * For backward compatibility and to facilitate the transition to
63 * this new naming scheme, the loader will additionally look for:
Jesse Hall94cdba92013-07-11 09:40:35 -070064 *
Mathias Agopian99381422013-04-23 20:52:29 +020065 * /{vendor|system}/lib/egl/lib{GLES | [EGL|GLESv1_CM|GLESv2]}_*.so
Jesse Hall94cdba92013-07-11 09:40:35 -070066 *
Mathias Agopiande586972009-05-28 17:39:03 -070067 */
68
Mathias Agopian65421432017-03-08 11:49:05 -080069Loader& Loader::getInstance() {
70 static Loader loader;
71 return loader;
72}
Mathias Agopiande586972009-05-28 17:39:03 -070073
David 'Digit' Turner80b30c22011-08-26 17:38:47 +020074/* This function is called to check whether we run inside the emulator,
75 * and if this is the case whether GLES GPU emulation is supported.
76 *
77 * Returned values are:
78 * -1 -> not running inside the emulator
79 * 0 -> running inside the emulator, but GPU emulation not supported
80 * 1 -> running inside the emulator, GPU emulation is supported
Nicolas Capens776951f2015-11-06 10:10:21 -050081 * through the "emulation" host-side OpenGL ES implementation.
82 * 2 -> running inside the emulator, GPU emulation is supported
83 * through a guest-side vendor driver's OpenGL ES implementation.
David 'Digit' Turner80b30c22011-08-26 17:38:47 +020084 */
85static int
86checkGlesEmulationStatus(void)
87{
88 /* We're going to check for the following kernel parameters:
89 *
90 * qemu=1 -> tells us that we run inside the emulator
91 * android.qemu.gles=<number> -> tells us the GLES GPU emulation status
92 *
93 * Note that we will return <number> if we find it. This let us support
94 * more additionnal emulation modes in the future.
95 */
96 char prop[PROPERTY_VALUE_MAX];
97 int result = -1;
98
99 /* First, check for qemu=1 */
100 property_get("ro.kernel.qemu",prop,"0");
101 if (atoi(prop) != 1)
102 return -1;
103
104 /* We are in the emulator, get GPU status value */
bohu69e5b1a2016-02-19 17:15:20 -0800105 property_get("qemu.gles",prop,"0");
David 'Digit' Turner80b30c22011-08-26 17:38:47 +0200106 return atoi(prop);
107}
108
Jesse Hall1508ae62017-01-19 17:43:26 -0800109static void* do_dlopen(const char* path, int mode) {
110 ATRACE_CALL();
111 return dlopen(path, mode);
112}
113
Jiyong Park5910dc72017-04-05 14:23:52 +0900114static void* do_android_dlopen_ext(const char* path, int mode, const android_dlextinfo* info) {
115 ATRACE_CALL();
116 return android_dlopen_ext(path, mode, info);
117}
118
Justin Yunb7320302017-05-22 15:13:40 +0900119static void* do_android_load_sphal_library(const char* path, int mode) {
120 ATRACE_CALL();
121 return android_load_sphal_library(path, mode);
122}
123
Mathias Agopiande586972009-05-28 17:39:03 -0700124// ----------------------------------------------------------------------------
125
Jesse Hall94cdba92013-07-11 09:40:35 -0700126Loader::driver_t::driver_t(void* gles)
Mathias Agopiande586972009-05-28 17:39:03 -0700127{
128 dso[0] = gles;
129 for (size_t i=1 ; i<NELEM(dso) ; i++)
130 dso[i] = 0;
131}
132
Jesse Hall94cdba92013-07-11 09:40:35 -0700133Loader::driver_t::~driver_t()
Mathias Agopiande586972009-05-28 17:39:03 -0700134{
135 for (size_t i=0 ; i<NELEM(dso) ; i++) {
136 if (dso[i]) {
137 dlclose(dso[i]);
138 dso[i] = 0;
139 }
140 }
141}
142
Mathias Agopian65421432017-03-08 11:49:05 -0800143int Loader::driver_t::set(void* hnd, int32_t api)
Mathias Agopiande586972009-05-28 17:39:03 -0700144{
145 switch (api) {
146 case EGL:
147 dso[0] = hnd;
148 break;
149 case GLESv1_CM:
150 dso[1] = hnd;
151 break;
152 case GLESv2:
153 dso[2] = hnd;
154 break;
155 default:
Mathias Agopian65421432017-03-08 11:49:05 -0800156 return -EOVERFLOW;
Mathias Agopiande586972009-05-28 17:39:03 -0700157 }
Mathias Agopian65421432017-03-08 11:49:05 -0800158 return 0;
Mathias Agopiande586972009-05-28 17:39:03 -0700159}
160
161// ----------------------------------------------------------------------------
162
Mathias Agopiande586972009-05-28 17:39:03 -0700163Loader::Loader()
Mathias Agopian991d2542017-02-06 13:51:32 -0800164 : getProcAddress(NULL)
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800165{
Mathias Agopiande586972009-05-28 17:39:03 -0700166}
167
Mathias Agopian99381422013-04-23 20:52:29 +0200168Loader::~Loader() {
Mathias Agopiande586972009-05-28 17:39:03 -0700169}
170
Jesse Hallc07b5202013-07-04 12:08:16 -0700171static void* load_wrapper(const char* path) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800172 void* so = do_dlopen(path, RTLD_NOW | RTLD_LOCAL);
Jesse Hallc07b5202013-07-04 12:08:16 -0700173 ALOGE_IF(!so, "dlopen(\"%s\") failed: %s", path, dlerror());
174 return so;
175}
176
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700177#ifndef EGL_WRAPPER_DIR
178#if defined(__LP64__)
179#define EGL_WRAPPER_DIR "/system/lib64"
180#else
181#define EGL_WRAPPER_DIR "/system/lib"
182#endif
183#endif
184
bohu69e5b1a2016-02-19 17:15:20 -0800185static void setEmulatorGlesValue(void) {
186 char prop[PROPERTY_VALUE_MAX];
187 property_get("ro.kernel.qemu", prop, "0");
188 if (atoi(prop) != 1) return;
189
190 property_get("ro.kernel.qemu.gles",prop,"0");
191 if (atoi(prop) == 1) {
192 ALOGD("Emulator has host GPU support, qemu.gles is set to 1.");
193 property_set("qemu.gles", "1");
194 return;
195 }
196
197 // for now, checking the following
198 // directory is good enough for emulator system images
199 const char* vendor_lib_path =
200#if defined(__LP64__)
201 "/vendor/lib64/egl";
202#else
203 "/vendor/lib/egl";
204#endif
205
206 const bool has_vendor_lib = (access(vendor_lib_path, R_OK) == 0);
207 if (has_vendor_lib) {
208 ALOGD("Emulator has vendor provided software renderer, qemu.gles is set to 2.");
209 property_set("qemu.gles", "2");
210 } else {
211 ALOGD("Emulator without GPU support detected. "
212 "Fallback to legacy software renderer, qemu.gles is set to 0.");
213 property_set("qemu.gles", "0");
214 }
215}
216
Mathias Agopianada798b2012-02-13 17:09:30 -0800217void* Loader::open(egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700218{
Jesse Hall1508ae62017-01-19 17:43:26 -0800219 ATRACE_CALL();
220
Mathias Agopiande586972009-05-28 17:39:03 -0700221 void* dso;
Mathias Agopiande586972009-05-28 17:39:03 -0700222 driver_t* hnd = 0;
Jesse Hall94cdba92013-07-11 09:40:35 -0700223
bohu69e5b1a2016-02-19 17:15:20 -0800224 setEmulatorGlesValue();
225
Mathias Agopian99381422013-04-23 20:52:29 +0200226 dso = load_driver("GLES", cnx, EGL | GLESv1_CM | GLESv2);
227 if (dso) {
228 hnd = new driver_t(dso);
229 } else {
230 // Always load EGL first
231 dso = load_driver("EGL", cnx, EGL);
Mathias Agopiande586972009-05-28 17:39:03 -0700232 if (dso) {
233 hnd = new driver_t(dso);
Mathias Agopian99381422013-04-23 20:52:29 +0200234 hnd->set( load_driver("GLESv1_CM", cnx, GLESv1_CM), GLESv1_CM );
235 hnd->set( load_driver("GLESv2", cnx, GLESv2), GLESv2 );
Mathias Agopiande586972009-05-28 17:39:03 -0700236 }
237 }
238
Mathias Agopian99381422013-04-23 20:52:29 +0200239 LOG_ALWAYS_FATAL_IF(!hnd, "couldn't find an OpenGL ES implementation");
Jesse Hallc07b5202013-07-04 12:08:16 -0700240
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700241 cnx->libEgl = load_wrapper(EGL_WRAPPER_DIR "/libEGL.so");
242 cnx->libGles2 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv2.so");
243 cnx->libGles1 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv1_CM.so");
244
Michael Chockc0ec5e22014-01-27 08:14:33 -0800245 LOG_ALWAYS_FATAL_IF(!cnx->libEgl,
246 "couldn't load system EGL wrapper libraries");
247
Jesse Hallc07b5202013-07-04 12:08:16 -0700248 LOG_ALWAYS_FATAL_IF(!cnx->libGles2 || !cnx->libGles1,
249 "couldn't load system OpenGL ES wrapper libraries");
250
Mathias Agopiande586972009-05-28 17:39:03 -0700251 return (void*)hnd;
252}
253
Mathias Agopian65421432017-03-08 11:49:05 -0800254void Loader::close(void* driver)
Mathias Agopiande586972009-05-28 17:39:03 -0700255{
256 driver_t* hnd = (driver_t*)driver;
257 delete hnd;
Mathias Agopiande586972009-05-28 17:39:03 -0700258}
259
Jesse Hall94cdba92013-07-11 09:40:35 -0700260void Loader::init_api(void* dso,
261 char const * const * api,
262 __eglMustCastToProperFunctionPointerType* curr,
263 getProcAddressType getProcAddress)
Mathias Agopiande586972009-05-28 17:39:03 -0700264{
Jesse Hall1508ae62017-01-19 17:43:26 -0800265 ATRACE_CALL();
266
Mathias Agopian7773c432012-02-13 20:06:08 -0800267 const ssize_t SIZE = 256;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700268 char scrap[SIZE];
Mathias Agopiande586972009-05-28 17:39:03 -0700269 while (*api) {
270 char const * name = *api;
Jesse Hall94cdba92013-07-11 09:40:35 -0700271 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700272 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
273 if (f == NULL) {
274 // couldn't find the entry-point, use eglGetProcAddress()
275 f = getProcAddress(name);
276 }
277 if (f == NULL) {
278 // Try without the OES postfix
279 ssize_t index = ssize_t(strlen(name)) - 3;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700280 if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) {
Mathias Agopiande586972009-05-28 17:39:03 -0700281 strncpy(scrap, name, index);
282 scrap[index] = 0;
283 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000284 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700285 }
286 }
287 if (f == NULL) {
288 // Try with the OES postfix
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700289 ssize_t index = ssize_t(strlen(name)) - 3;
290 if (index>0 && strcmp(name+index, "OES")) {
291 snprintf(scrap, SIZE, "%sOES", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700292 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000293 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700294 }
295 }
296 if (f == NULL) {
Steve Block9d453682011-12-20 16:23:08 +0000297 //ALOGD("%s", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700298 f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented;
Mathias Agopian48d438d2012-01-28 21:44:00 -0800299
300 /*
301 * GL_EXT_debug_label is special, we always report it as
302 * supported, it's handled by GLES_trace. If GLES_trace is not
303 * enabled, then these are no-ops.
304 */
305 if (!strcmp(name, "glInsertEventMarkerEXT")) {
306 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
307 } else if (!strcmp(name, "glPushGroupMarkerEXT")) {
308 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
309 } else if (!strcmp(name, "glPopGroupMarkerEXT")) {
310 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
311 }
Mathias Agopiande586972009-05-28 17:39:03 -0700312 }
313 *curr++ = f;
314 api++;
315 }
316}
317
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800318static void* load_system_driver(const char* kind) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800319 ATRACE_CALL();
Mathias Agopian99381422013-04-23 20:52:29 +0200320 class MatchFile {
321 public:
Mathias Agopian65421432017-03-08 11:49:05 -0800322 static std::string find(const char* kind) {
323 std::string result;
Nicolas Capens776951f2015-11-06 10:10:21 -0500324 int emulationStatus = checkGlesEmulationStatus();
325 switch (emulationStatus) {
326 case 0:
Nicolas Capens776951f2015-11-06 10:10:21 -0500327#if defined(__LP64__)
Mathias Agopian65421432017-03-08 11:49:05 -0800328 result = "/system/lib64/egl/libGLES_android.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500329#else
Mathias Agopian65421432017-03-08 11:49:05 -0800330 result = "/system/lib/egl/libGLES_android.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500331#endif
332 return result;
333 case 1:
334 // Use host-side OpenGL through the "emulation" library
335#if defined(__LP64__)
Mathias Agopian65421432017-03-08 11:49:05 -0800336 result = std::string("/system/lib64/egl/lib") + kind + "_emulation.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500337#else
Mathias Agopian65421432017-03-08 11:49:05 -0800338 result = std::string("/system/lib/egl/lib") + kind + "_emulation.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500339#endif
340 return result;
341 default:
342 // Not in emulator, or use other guest-side implementation
343 break;
344 }
345
Mathias Agopian65421432017-03-08 11:49:05 -0800346 std::string pattern = std::string("lib") + kind;
Mathias Agopian99381422013-04-23 20:52:29 +0200347 const char* const searchPaths[] = {
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800348#if defined(__LP64__)
349 "/vendor/lib64/egl",
350 "/system/lib64/egl"
351#else
Mathias Agopian99381422013-04-23 20:52:29 +0200352 "/vendor/lib/egl",
353 "/system/lib/egl"
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800354#endif
Mathias Agopian99381422013-04-23 20:52:29 +0200355 };
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700356
Mathias Agopian99381422013-04-23 20:52:29 +0200357 // first, we search for the exact name of the GLES userspace
358 // driver in both locations.
359 // i.e.:
360 // libGLES.so, or:
361 // libEGL.so, libGLESv1_CM.so, libGLESv2.so
362
363 for (size_t i=0 ; i<NELEM(searchPaths) ; i++) {
364 if (find(result, pattern, searchPaths[i], true)) {
365 return result;
366 }
367 }
368
369 // for compatibility with the old "egl.cfg" naming convention
370 // we look for files that match:
371 // libGLES_*.so, or:
372 // libEGL_*.so, libGLESv1_CM_*.so, libGLESv2_*.so
373
374 pattern.append("_");
375 for (size_t i=0 ; i<NELEM(searchPaths) ; i++) {
376 if (find(result, pattern, searchPaths[i], false)) {
377 return result;
378 }
379 }
380
381 // we didn't find the driver. gah.
382 result.clear();
383 return result;
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700384 }
Mathias Agopian99381422013-04-23 20:52:29 +0200385
386 private:
Mathias Agopian65421432017-03-08 11:49:05 -0800387 static bool find(std::string& result,
388 const std::string& pattern, const char* const search, bool exact) {
Mathias Agopian99381422013-04-23 20:52:29 +0200389 if (exact) {
Mathias Agopian65421432017-03-08 11:49:05 -0800390 std::string absolutePath = std::string(search) + "/" + pattern;
391 if (!access(absolutePath.c_str(), R_OK)) {
Mathias Agopian99381422013-04-23 20:52:29 +0200392 result = absolutePath;
393 return true;
394 }
395 return false;
396 }
397
398 DIR* d = opendir(search);
399 if (d != NULL) {
400 struct dirent cur;
401 struct dirent* e;
402 while (readdir_r(d, &cur, &e) == 0 && e) {
403 if (e->d_type == DT_DIR) {
404 continue;
405 }
406 if (!strcmp(e->d_name, "libGLES_android.so")) {
407 // always skip the software renderer
408 continue;
409 }
Mathias Agopian65421432017-03-08 11:49:05 -0800410 if (strstr(e->d_name, pattern.c_str()) == e->d_name) {
Mathias Agopian99381422013-04-23 20:52:29 +0200411 if (!strcmp(e->d_name + strlen(e->d_name) - 3, ".so")) {
Mathias Agopian65421432017-03-08 11:49:05 -0800412 result = std::string(search) + "/" + e->d_name;
Mathias Agopian99381422013-04-23 20:52:29 +0200413 closedir(d);
414 return true;
415 }
416 }
417 }
418 closedir(d);
419 }
420 return false;
421 }
422 };
423
424
Mathias Agopian65421432017-03-08 11:49:05 -0800425 std::string absolutePath = MatchFile::find(kind);
426 if (absolutePath.empty()) {
Mathias Agopian99381422013-04-23 20:52:29 +0200427 // this happens often, we don't want to log an error
428 return 0;
Mathias Agopian8c173842009-09-20 16:01:02 -0700429 }
Mathias Agopian65421432017-03-08 11:49:05 -0800430 const char* const driver_absolute_path = absolutePath.c_str();
Mathias Agopiande586972009-05-28 17:39:03 -0700431
Jiyong Park5910dc72017-04-05 14:23:52 +0900432 // Try to load drivers from the 'sphal' namespace, if it exist. Fall back to
Justin Yunb7320302017-05-22 15:13:40 +0900433 // the original routine when the namespace does not exist.
Jiyong Park5910dc72017-04-05 14:23:52 +0900434 // See /system/core/rootdir/etc/ld.config.txt for the configuration of the
435 // sphal namespace.
Justin Yunb7320302017-05-22 15:13:40 +0900436 void* dso = do_android_load_sphal_library(driver_absolute_path,
437 RTLD_NOW | RTLD_LOCAL);
Mathias Agopian8c173842009-09-20 16:01:02 -0700438 if (dso == 0) {
439 const char* err = dlerror();
Mathias Agopian65421432017-03-08 11:49:05 -0800440 ALOGE("load_driver(%s): %s", driver_absolute_path, err ? err : "unknown");
Mathias Agopian8c173842009-09-20 16:01:02 -0700441 return 0;
442 }
443
Steve Block9d453682011-12-20 16:23:08 +0000444 ALOGD("loaded %s", driver_absolute_path);
Mathias Agopianbaca89c2009-08-20 19:09:34 -0700445
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800446 return dso;
447}
448
Mathias Agopian311b4792017-02-28 15:00:49 -0800449static const char* HAL_SUBNAME_KEY_PROPERTIES[2] = {
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800450 "ro.hardware.egl",
451 "ro.board.platform",
Mathias Agopian311b4792017-02-28 15:00:49 -0800452};
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800453
454static void* load_updated_driver(const char* kind, android_namespace_t* ns) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800455 ATRACE_CALL();
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800456 const android_dlextinfo dlextinfo = {
457 .flags = ANDROID_DLEXT_USE_NAMESPACE,
458 .library_namespace = ns,
459 };
460 void* so = nullptr;
461 char prop[PROPERTY_VALUE_MAX + 1];
462 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
463 if (property_get(key, prop, nullptr) > 0) {
Mathias Agopian65421432017-03-08 11:49:05 -0800464 std::string name = std::string("lib") + kind + "_" + prop + ".so";
465 so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
Mathias Agopian311b4792017-02-28 15:00:49 -0800466 if (so) {
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800467 return so;
Mathias Agopian311b4792017-02-28 15:00:49 -0800468 }
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800469 }
470 }
471 return nullptr;
472}
473
474void *Loader::load_driver(const char* kind,
475 egl_connection_t* cnx, uint32_t mask)
476{
Jesse Hall1508ae62017-01-19 17:43:26 -0800477 ATRACE_CALL();
478
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800479 void* dso = nullptr;
Mathias Agopian991d2542017-02-06 13:51:32 -0800480 android_namespace_t* ns = android_getDriverNamespace();
481 if (ns) {
482 dso = load_updated_driver(kind, ns);
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800483 }
484 if (!dso) {
485 dso = load_system_driver(kind);
486 if (!dso)
487 return NULL;
488 }
489
Mathias Agopiande586972009-05-28 17:39:03 -0700490 if (mask & EGL) {
491 getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress");
492
Jesse Hall94cdba92013-07-11 09:40:35 -0700493 ALOGE_IF(!getProcAddress,
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800494 "can't find eglGetProcAddress() in EGL driver library");
Mathias Agopiande586972009-05-28 17:39:03 -0700495
Mathias Agopian618fa102009-10-14 02:06:37 -0700496 egl_t* egl = &cnx->egl;
Mathias Agopiande586972009-05-28 17:39:03 -0700497 __eglMustCastToProperFunctionPointerType* curr =
498 (__eglMustCastToProperFunctionPointerType*)egl;
499 char const * const * api = egl_names;
500 while (*api) {
501 char const * name = *api;
Jesse Hall94cdba92013-07-11 09:40:35 -0700502 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700503 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
504 if (f == NULL) {
505 // couldn't find the entry-point, use eglGetProcAddress()
506 f = getProcAddress(name);
507 if (f == NULL) {
508 f = (__eglMustCastToProperFunctionPointerType)0;
509 }
510 }
511 *curr++ = f;
512 api++;
513 }
514 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700515
Mathias Agopiande586972009-05-28 17:39:03 -0700516 if (mask & GLESv1_CM) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700517 init_api(dso, gl_names,
518 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800519 &cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl,
Mathias Agopian618fa102009-10-14 02:06:37 -0700520 getProcAddress);
Mathias Agopiande586972009-05-28 17:39:03 -0700521 }
522
523 if (mask & GLESv2) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700524 init_api(dso, gl_names,
525 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800526 &cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl,
Mathias Agopiande586972009-05-28 17:39:03 -0700527 getProcAddress);
528 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700529
Mathias Agopiande586972009-05-28 17:39:03 -0700530 return dso;
531}
532
533// ----------------------------------------------------------------------------
534}; // namespace android
535// ----------------------------------------------------------------------------