blob: 9822849e3fe08d4ff006cb55d5989e2f11398b46 [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__)
bohu3adf9e12017-07-02 22:08:13 -0700330 result = "/vendor/lib64/egl/libGLES_android.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500331#else
bohu3adf9e12017-07-02 22:08:13 -0700332 result = "/vendor/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__)
bohu3adf9e12017-07-02 22:08:13 -0700338 result = std::string("/vendor/lib64/egl/lib") + kind + "_emulation.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500339#else
bohu3adf9e12017-07-02 22:08:13 -0700340 result = std::string("/vendor/lib/egl/lib") + kind + "_emulation.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500341#endif
342 return result;
Lingfeng Yang0f82d872017-08-11 01:18:00 -0700343 case 2:
344 // Use guest side swiftshader library
345#if defined(__LP64__)
346 result = std::string("/vendor/lib64/egl/lib") + kind + "_swiftshader.so";
347#else
348 result = std::string("/vendor/lib/egl/lib") + kind + "_swiftshader.so";
349#endif
350 return result;
Nicolas Capens776951f2015-11-06 10:10:21 -0500351 default:
352 // Not in emulator, or use other guest-side implementation
353 break;
354 }
355
Mathias Agopian65421432017-03-08 11:49:05 -0800356 std::string pattern = std::string("lib") + kind;
Mathias Agopian99381422013-04-23 20:52:29 +0200357 const char* const searchPaths[] = {
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800358#if defined(__LP64__)
359 "/vendor/lib64/egl",
360 "/system/lib64/egl"
361#else
Mathias Agopian99381422013-04-23 20:52:29 +0200362 "/vendor/lib/egl",
363 "/system/lib/egl"
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800364#endif
Mathias Agopian99381422013-04-23 20:52:29 +0200365 };
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700366
Mathias Agopian99381422013-04-23 20:52:29 +0200367 // first, we search for the exact name of the GLES userspace
368 // driver in both locations.
369 // i.e.:
370 // libGLES.so, or:
371 // libEGL.so, libGLESv1_CM.so, libGLESv2.so
372
373 for (size_t i=0 ; i<NELEM(searchPaths) ; i++) {
374 if (find(result, pattern, searchPaths[i], true)) {
375 return result;
376 }
377 }
378
379 // for compatibility with the old "egl.cfg" naming convention
380 // we look for files that match:
381 // libGLES_*.so, or:
382 // libEGL_*.so, libGLESv1_CM_*.so, libGLESv2_*.so
383
384 pattern.append("_");
385 for (size_t i=0 ; i<NELEM(searchPaths) ; i++) {
386 if (find(result, pattern, searchPaths[i], false)) {
387 return result;
388 }
389 }
390
391 // we didn't find the driver. gah.
392 result.clear();
393 return result;
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700394 }
Mathias Agopian99381422013-04-23 20:52:29 +0200395
396 private:
Mathias Agopian65421432017-03-08 11:49:05 -0800397 static bool find(std::string& result,
398 const std::string& pattern, const char* const search, bool exact) {
Mathias Agopian99381422013-04-23 20:52:29 +0200399 if (exact) {
Jesse Hall4aaa9602017-08-29 16:33:54 -0700400 std::string absolutePath = std::string(search) + "/" + pattern + ".so";
Mathias Agopian65421432017-03-08 11:49:05 -0800401 if (!access(absolutePath.c_str(), R_OK)) {
Mathias Agopian99381422013-04-23 20:52:29 +0200402 result = absolutePath;
403 return true;
404 }
405 return false;
406 }
407
408 DIR* d = opendir(search);
409 if (d != NULL) {
410 struct dirent cur;
411 struct dirent* e;
412 while (readdir_r(d, &cur, &e) == 0 && e) {
413 if (e->d_type == DT_DIR) {
414 continue;
415 }
416 if (!strcmp(e->d_name, "libGLES_android.so")) {
417 // always skip the software renderer
418 continue;
419 }
Mathias Agopian65421432017-03-08 11:49:05 -0800420 if (strstr(e->d_name, pattern.c_str()) == e->d_name) {
Mathias Agopian99381422013-04-23 20:52:29 +0200421 if (!strcmp(e->d_name + strlen(e->d_name) - 3, ".so")) {
Mathias Agopian65421432017-03-08 11:49:05 -0800422 result = std::string(search) + "/" + e->d_name;
Mathias Agopian99381422013-04-23 20:52:29 +0200423 closedir(d);
424 return true;
425 }
426 }
427 }
428 closedir(d);
429 }
430 return false;
431 }
432 };
433
434
Mathias Agopian65421432017-03-08 11:49:05 -0800435 std::string absolutePath = MatchFile::find(kind);
436 if (absolutePath.empty()) {
Mathias Agopian99381422013-04-23 20:52:29 +0200437 // this happens often, we don't want to log an error
438 return 0;
Mathias Agopian8c173842009-09-20 16:01:02 -0700439 }
Mathias Agopian65421432017-03-08 11:49:05 -0800440 const char* const driver_absolute_path = absolutePath.c_str();
Mathias Agopiande586972009-05-28 17:39:03 -0700441
Jiyong Park5910dc72017-04-05 14:23:52 +0900442 // Try to load drivers from the 'sphal' namespace, if it exist. Fall back to
Justin Yunb7320302017-05-22 15:13:40 +0900443 // the original routine when the namespace does not exist.
Jiyong Park5910dc72017-04-05 14:23:52 +0900444 // See /system/core/rootdir/etc/ld.config.txt for the configuration of the
445 // sphal namespace.
Justin Yunb7320302017-05-22 15:13:40 +0900446 void* dso = do_android_load_sphal_library(driver_absolute_path,
447 RTLD_NOW | RTLD_LOCAL);
Mathias Agopian8c173842009-09-20 16:01:02 -0700448 if (dso == 0) {
449 const char* err = dlerror();
Mathias Agopian65421432017-03-08 11:49:05 -0800450 ALOGE("load_driver(%s): %s", driver_absolute_path, err ? err : "unknown");
Mathias Agopian8c173842009-09-20 16:01:02 -0700451 return 0;
452 }
453
Steve Block9d453682011-12-20 16:23:08 +0000454 ALOGD("loaded %s", driver_absolute_path);
Mathias Agopianbaca89c2009-08-20 19:09:34 -0700455
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800456 return dso;
457}
458
Mathias Agopian311b4792017-02-28 15:00:49 -0800459static const char* HAL_SUBNAME_KEY_PROPERTIES[2] = {
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800460 "ro.hardware.egl",
461 "ro.board.platform",
Mathias Agopian311b4792017-02-28 15:00:49 -0800462};
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800463
464static void* load_updated_driver(const char* kind, android_namespace_t* ns) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800465 ATRACE_CALL();
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800466 const android_dlextinfo dlextinfo = {
467 .flags = ANDROID_DLEXT_USE_NAMESPACE,
468 .library_namespace = ns,
469 };
470 void* so = nullptr;
471 char prop[PROPERTY_VALUE_MAX + 1];
472 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
473 if (property_get(key, prop, nullptr) > 0) {
Mathias Agopian65421432017-03-08 11:49:05 -0800474 std::string name = std::string("lib") + kind + "_" + prop + ".so";
475 so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
Mathias Agopian311b4792017-02-28 15:00:49 -0800476 if (so) {
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800477 return so;
Mathias Agopian311b4792017-02-28 15:00:49 -0800478 }
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800479 }
480 }
481 return nullptr;
482}
483
484void *Loader::load_driver(const char* kind,
485 egl_connection_t* cnx, uint32_t mask)
486{
Jesse Hall1508ae62017-01-19 17:43:26 -0800487 ATRACE_CALL();
488
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800489 void* dso = nullptr;
Jiyong Parka243e5d2017-06-21 12:26:51 +0900490#ifndef __ANDROID_VNDK__
Mathias Agopian991d2542017-02-06 13:51:32 -0800491 android_namespace_t* ns = android_getDriverNamespace();
492 if (ns) {
493 dso = load_updated_driver(kind, ns);
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800494 }
Jiyong Parka243e5d2017-06-21 12:26:51 +0900495#endif
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800496 if (!dso) {
497 dso = load_system_driver(kind);
498 if (!dso)
499 return NULL;
500 }
501
Mathias Agopiande586972009-05-28 17:39:03 -0700502 if (mask & EGL) {
503 getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress");
504
Jesse Hall94cdba92013-07-11 09:40:35 -0700505 ALOGE_IF(!getProcAddress,
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800506 "can't find eglGetProcAddress() in EGL driver library");
Mathias Agopiande586972009-05-28 17:39:03 -0700507
Mathias Agopian618fa102009-10-14 02:06:37 -0700508 egl_t* egl = &cnx->egl;
Mathias Agopiande586972009-05-28 17:39:03 -0700509 __eglMustCastToProperFunctionPointerType* curr =
510 (__eglMustCastToProperFunctionPointerType*)egl;
511 char const * const * api = egl_names;
512 while (*api) {
513 char const * name = *api;
Jesse Hall94cdba92013-07-11 09:40:35 -0700514 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700515 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
516 if (f == NULL) {
517 // couldn't find the entry-point, use eglGetProcAddress()
518 f = getProcAddress(name);
519 if (f == NULL) {
520 f = (__eglMustCastToProperFunctionPointerType)0;
521 }
522 }
523 *curr++ = f;
524 api++;
525 }
526 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700527
Mathias Agopiande586972009-05-28 17:39:03 -0700528 if (mask & GLESv1_CM) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700529 init_api(dso, gl_names,
530 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800531 &cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl,
Mathias Agopian618fa102009-10-14 02:06:37 -0700532 getProcAddress);
Mathias Agopiande586972009-05-28 17:39:03 -0700533 }
534
535 if (mask & GLESv2) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700536 init_api(dso, gl_names,
537 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800538 &cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl,
Mathias Agopiande586972009-05-28 17:39:03 -0700539 getProcAddress);
540 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700541
Mathias Agopiande586972009-05-28 17:39:03 -0700542 return dso;
543}
544
545// ----------------------------------------------------------------------------
546}; // namespace android
547// ----------------------------------------------------------------------------