blob: 6f050bf83115124043e9949e79eac96a2e46befe [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,
Yiwei Zhang40309962018-10-10 11:37:43 -0700264 char const * const * ref_api,
Jesse Hall94cdba92013-07-11 09:40:35 -0700265 __eglMustCastToProperFunctionPointerType* curr,
266 getProcAddressType getProcAddress)
Mathias Agopiande586972009-05-28 17:39:03 -0700267{
Jesse Hall1508ae62017-01-19 17:43:26 -0800268 ATRACE_CALL();
269
Mathias Agopian7773c432012-02-13 20:06:08 -0800270 const ssize_t SIZE = 256;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700271 char scrap[SIZE];
Mathias Agopiande586972009-05-28 17:39:03 -0700272 while (*api) {
273 char const * name = *api;
Yiwei Zhang40309962018-10-10 11:37:43 -0700274 if (ref_api) {
275 char const * ref_name = *ref_api;
276 if (std::strcmp(name, ref_name) != 0) {
277 *curr++ = nullptr;
278 ref_api++;
279 continue;
280 }
281 }
282
Jesse Hall94cdba92013-07-11 09:40:35 -0700283 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700284 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
285 if (f == NULL) {
286 // couldn't find the entry-point, use eglGetProcAddress()
287 f = getProcAddress(name);
288 }
289 if (f == NULL) {
290 // Try without the OES postfix
291 ssize_t index = ssize_t(strlen(name)) - 3;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700292 if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) {
Mathias Agopiande586972009-05-28 17:39:03 -0700293 strncpy(scrap, name, index);
294 scrap[index] = 0;
295 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000296 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700297 }
298 }
299 if (f == NULL) {
300 // Try with the OES postfix
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700301 ssize_t index = ssize_t(strlen(name)) - 3;
302 if (index>0 && strcmp(name+index, "OES")) {
303 snprintf(scrap, SIZE, "%sOES", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700304 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000305 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700306 }
307 }
308 if (f == NULL) {
Steve Block9d453682011-12-20 16:23:08 +0000309 //ALOGD("%s", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700310 f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented;
Mathias Agopian48d438d2012-01-28 21:44:00 -0800311
312 /*
313 * GL_EXT_debug_label is special, we always report it as
314 * supported, it's handled by GLES_trace. If GLES_trace is not
315 * enabled, then these are no-ops.
316 */
317 if (!strcmp(name, "glInsertEventMarkerEXT")) {
318 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
319 } else if (!strcmp(name, "glPushGroupMarkerEXT")) {
320 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
321 } else if (!strcmp(name, "glPopGroupMarkerEXT")) {
322 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
323 }
Mathias Agopiande586972009-05-28 17:39:03 -0700324 }
325 *curr++ = f;
326 api++;
Yiwei Zhang40309962018-10-10 11:37:43 -0700327 if (ref_api) ref_api++;
Mathias Agopiande586972009-05-28 17:39:03 -0700328 }
329}
330
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800331static void* load_system_driver(const char* kind) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800332 ATRACE_CALL();
Mathias Agopian99381422013-04-23 20:52:29 +0200333 class MatchFile {
334 public:
Mathias Agopian65421432017-03-08 11:49:05 -0800335 static std::string find(const char* kind) {
336 std::string result;
Nicolas Capens776951f2015-11-06 10:10:21 -0500337 int emulationStatus = checkGlesEmulationStatus();
338 switch (emulationStatus) {
339 case 0:
Nicolas Capens776951f2015-11-06 10:10:21 -0500340#if defined(__LP64__)
bohu3adf9e12017-07-02 22:08:13 -0700341 result = "/vendor/lib64/egl/libGLES_android.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500342#else
bohu3adf9e12017-07-02 22:08:13 -0700343 result = "/vendor/lib/egl/libGLES_android.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500344#endif
345 return result;
346 case 1:
347 // Use host-side OpenGL through the "emulation" library
348#if defined(__LP64__)
bohu3adf9e12017-07-02 22:08:13 -0700349 result = std::string("/vendor/lib64/egl/lib") + kind + "_emulation.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500350#else
bohu3adf9e12017-07-02 22:08:13 -0700351 result = std::string("/vendor/lib/egl/lib") + kind + "_emulation.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500352#endif
353 return result;
Lingfeng Yang0f82d872017-08-11 01:18:00 -0700354 case 2:
355 // Use guest side swiftshader library
356#if defined(__LP64__)
357 result = std::string("/vendor/lib64/egl/lib") + kind + "_swiftshader.so";
358#else
359 result = std::string("/vendor/lib/egl/lib") + kind + "_swiftshader.so";
360#endif
361 return result;
Nicolas Capens776951f2015-11-06 10:10:21 -0500362 default:
363 // Not in emulator, or use other guest-side implementation
364 break;
365 }
366
Mathias Agopian65421432017-03-08 11:49:05 -0800367 std::string pattern = std::string("lib") + kind;
Mathias Agopian99381422013-04-23 20:52:29 +0200368 const char* const searchPaths[] = {
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800369#if defined(__LP64__)
370 "/vendor/lib64/egl",
371 "/system/lib64/egl"
372#else
Mathias Agopian99381422013-04-23 20:52:29 +0200373 "/vendor/lib/egl",
374 "/system/lib/egl"
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800375#endif
Mathias Agopian99381422013-04-23 20:52:29 +0200376 };
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700377
Mathias Agopian99381422013-04-23 20:52:29 +0200378 // first, we search for the exact name of the GLES userspace
379 // driver in both locations.
380 // i.e.:
381 // libGLES.so, or:
382 // libEGL.so, libGLESv1_CM.so, libGLESv2.so
383
384 for (size_t i=0 ; i<NELEM(searchPaths) ; i++) {
385 if (find(result, pattern, searchPaths[i], true)) {
386 return result;
387 }
388 }
389
390 // for compatibility with the old "egl.cfg" naming convention
391 // we look for files that match:
392 // libGLES_*.so, or:
393 // libEGL_*.so, libGLESv1_CM_*.so, libGLESv2_*.so
394
395 pattern.append("_");
396 for (size_t i=0 ; i<NELEM(searchPaths) ; i++) {
397 if (find(result, pattern, searchPaths[i], false)) {
398 return result;
399 }
400 }
401
402 // we didn't find the driver. gah.
403 result.clear();
404 return result;
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700405 }
Mathias Agopian99381422013-04-23 20:52:29 +0200406
407 private:
Mathias Agopian65421432017-03-08 11:49:05 -0800408 static bool find(std::string& result,
409 const std::string& pattern, const char* const search, bool exact) {
Mathias Agopian99381422013-04-23 20:52:29 +0200410 if (exact) {
Jesse Hall4aaa9602017-08-29 16:33:54 -0700411 std::string absolutePath = std::string(search) + "/" + pattern + ".so";
Mathias Agopian65421432017-03-08 11:49:05 -0800412 if (!access(absolutePath.c_str(), R_OK)) {
Mathias Agopian99381422013-04-23 20:52:29 +0200413 result = absolutePath;
414 return true;
415 }
416 return false;
417 }
418
419 DIR* d = opendir(search);
420 if (d != NULL) {
Mathias Agopian99381422013-04-23 20:52:29 +0200421 struct dirent* e;
Elliott Hughes2eab3fc2018-01-10 16:35:48 -0800422 while ((e = readdir(d)) != NULL) {
Mathias Agopian99381422013-04-23 20:52:29 +0200423 if (e->d_type == DT_DIR) {
424 continue;
425 }
426 if (!strcmp(e->d_name, "libGLES_android.so")) {
427 // always skip the software renderer
428 continue;
429 }
Mathias Agopian65421432017-03-08 11:49:05 -0800430 if (strstr(e->d_name, pattern.c_str()) == e->d_name) {
Mathias Agopian99381422013-04-23 20:52:29 +0200431 if (!strcmp(e->d_name + strlen(e->d_name) - 3, ".so")) {
Mathias Agopian65421432017-03-08 11:49:05 -0800432 result = std::string(search) + "/" + e->d_name;
Mathias Agopian99381422013-04-23 20:52:29 +0200433 closedir(d);
434 return true;
435 }
436 }
437 }
438 closedir(d);
439 }
440 return false;
441 }
442 };
443
444
Mathias Agopian65421432017-03-08 11:49:05 -0800445 std::string absolutePath = MatchFile::find(kind);
446 if (absolutePath.empty()) {
Mathias Agopian99381422013-04-23 20:52:29 +0200447 // this happens often, we don't want to log an error
448 return 0;
Mathias Agopian8c173842009-09-20 16:01:02 -0700449 }
Mathias Agopian65421432017-03-08 11:49:05 -0800450 const char* const driver_absolute_path = absolutePath.c_str();
Mathias Agopiande586972009-05-28 17:39:03 -0700451
Jiyong Park5910dc72017-04-05 14:23:52 +0900452 // Try to load drivers from the 'sphal' namespace, if it exist. Fall back to
Justin Yunb7320302017-05-22 15:13:40 +0900453 // the original routine when the namespace does not exist.
Jiyong Park5910dc72017-04-05 14:23:52 +0900454 // See /system/core/rootdir/etc/ld.config.txt for the configuration of the
455 // sphal namespace.
Justin Yunb7320302017-05-22 15:13:40 +0900456 void* dso = do_android_load_sphal_library(driver_absolute_path,
457 RTLD_NOW | RTLD_LOCAL);
Mathias Agopian8c173842009-09-20 16:01:02 -0700458 if (dso == 0) {
459 const char* err = dlerror();
Mathias Agopian65421432017-03-08 11:49:05 -0800460 ALOGE("load_driver(%s): %s", driver_absolute_path, err ? err : "unknown");
Mathias Agopian8c173842009-09-20 16:01:02 -0700461 return 0;
462 }
463
Steve Block9d453682011-12-20 16:23:08 +0000464 ALOGD("loaded %s", driver_absolute_path);
Mathias Agopianbaca89c2009-08-20 19:09:34 -0700465
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800466 return dso;
467}
468
Mathias Agopian311b4792017-02-28 15:00:49 -0800469static const char* HAL_SUBNAME_KEY_PROPERTIES[2] = {
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800470 "ro.hardware.egl",
471 "ro.board.platform",
Mathias Agopian311b4792017-02-28 15:00:49 -0800472};
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800473
474static void* load_updated_driver(const char* kind, android_namespace_t* ns) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800475 ATRACE_CALL();
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800476 const android_dlextinfo dlextinfo = {
477 .flags = ANDROID_DLEXT_USE_NAMESPACE,
478 .library_namespace = ns,
479 };
480 void* so = nullptr;
481 char prop[PROPERTY_VALUE_MAX + 1];
482 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
483 if (property_get(key, prop, nullptr) > 0) {
Mathias Agopian65421432017-03-08 11:49:05 -0800484 std::string name = std::string("lib") + kind + "_" + prop + ".so";
485 so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
Mathias Agopian311b4792017-02-28 15:00:49 -0800486 if (so) {
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800487 return so;
Mathias Agopian311b4792017-02-28 15:00:49 -0800488 }
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800489 }
490 }
491 return nullptr;
492}
493
494void *Loader::load_driver(const char* kind,
495 egl_connection_t* cnx, uint32_t mask)
496{
Jesse Hall1508ae62017-01-19 17:43:26 -0800497 ATRACE_CALL();
498
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800499 void* dso = nullptr;
Jiyong Parka243e5d2017-06-21 12:26:51 +0900500#ifndef __ANDROID_VNDK__
Mathias Agopian991d2542017-02-06 13:51:32 -0800501 android_namespace_t* ns = android_getDriverNamespace();
502 if (ns) {
503 dso = load_updated_driver(kind, ns);
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800504 }
Jiyong Parka243e5d2017-06-21 12:26:51 +0900505#endif
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800506 if (!dso) {
507 dso = load_system_driver(kind);
508 if (!dso)
509 return NULL;
510 }
511
Mathias Agopiande586972009-05-28 17:39:03 -0700512 if (mask & EGL) {
513 getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress");
514
Jesse Hall94cdba92013-07-11 09:40:35 -0700515 ALOGE_IF(!getProcAddress,
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800516 "can't find eglGetProcAddress() in EGL driver library");
Mathias Agopiande586972009-05-28 17:39:03 -0700517
Mathias Agopian618fa102009-10-14 02:06:37 -0700518 egl_t* egl = &cnx->egl;
Mathias Agopiande586972009-05-28 17:39:03 -0700519 __eglMustCastToProperFunctionPointerType* curr =
520 (__eglMustCastToProperFunctionPointerType*)egl;
521 char const * const * api = egl_names;
522 while (*api) {
523 char const * name = *api;
Jesse Hall94cdba92013-07-11 09:40:35 -0700524 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700525 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
526 if (f == NULL) {
527 // couldn't find the entry-point, use eglGetProcAddress()
528 f = getProcAddress(name);
529 if (f == NULL) {
530 f = (__eglMustCastToProperFunctionPointerType)0;
531 }
532 }
533 *curr++ = f;
534 api++;
535 }
536 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700537
Mathias Agopiande586972009-05-28 17:39:03 -0700538 if (mask & GLESv1_CM) {
Yiwei Zhang40309962018-10-10 11:37:43 -0700539 init_api(dso, gl_names_1, gl_names,
Mathias Agopian618fa102009-10-14 02:06:37 -0700540 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800541 &cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl,
Mathias Agopian618fa102009-10-14 02:06:37 -0700542 getProcAddress);
Mathias Agopiande586972009-05-28 17:39:03 -0700543 }
544
545 if (mask & GLESv2) {
Yiwei Zhang40309962018-10-10 11:37:43 -0700546 init_api(dso, gl_names, nullptr,
Mathias Agopian618fa102009-10-14 02:06:37 -0700547 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800548 &cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl,
Mathias Agopiande586972009-05-28 17:39:03 -0700549 getProcAddress);
550 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700551
Mathias Agopiande586972009-05-28 17:39:03 -0700552 return dso;
553}
554
555// ----------------------------------------------------------------------------
556}; // namespace android
557// ----------------------------------------------------------------------------