blob: 7d20ba1fa801444fadf49a7923be9652a59d2083 [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
Mathias Agopian991d2542017-02-06 13:51:32 -080031#include <ui/GraphicsEnv.h>
Mathias Agopiande586972009-05-28 17:39:03 -070032
Mathias Agopian65421432017-03-08 11:49:05 -080033#include "egl_trace.h"
Mathias Agopian1cadb252011-05-23 17:26:14 -070034#include "egldefs.h"
Mathias Agopiande586972009-05-28 17:39:03 -070035
Jiyong Park5910dc72017-04-05 14:23:52 +090036extern "C" {
37 android_namespace_t* android_get_exported_namespace(const char*);
38}
39
Mathias Agopiande586972009-05-28 17:39:03 -070040// ----------------------------------------------------------------------------
41namespace android {
42// ----------------------------------------------------------------------------
43
44
45/*
Mathias Agopian99381422013-04-23 20:52:29 +020046 * EGL userspace drivers must be provided either:
47 * - as a single library:
48 * /vendor/lib/egl/libGLES.so
49 *
50 * - as separate libraries:
51 * /vendor/lib/egl/libEGL.so
52 * /vendor/lib/egl/libGLESv1_CM.so
53 * /vendor/lib/egl/libGLESv2.so
54 *
55 * The software renderer for the emulator must be provided as a single
56 * library at:
57 *
58 * /system/lib/egl/libGLES_android.so
59 *
60 *
61 * For backward compatibility and to facilitate the transition to
62 * this new naming scheme, the loader will additionally look for:
Jesse Hall94cdba92013-07-11 09:40:35 -070063 *
Mathias Agopian99381422013-04-23 20:52:29 +020064 * /{vendor|system}/lib/egl/lib{GLES | [EGL|GLESv1_CM|GLESv2]}_*.so
Jesse Hall94cdba92013-07-11 09:40:35 -070065 *
Mathias Agopiande586972009-05-28 17:39:03 -070066 */
67
Mathias Agopian65421432017-03-08 11:49:05 -080068Loader& Loader::getInstance() {
69 static Loader loader;
70 return loader;
71}
Mathias Agopiande586972009-05-28 17:39:03 -070072
David 'Digit' Turner80b30c22011-08-26 17:38:47 +020073/* This function is called to check whether we run inside the emulator,
74 * and if this is the case whether GLES GPU emulation is supported.
75 *
76 * Returned values are:
77 * -1 -> not running inside the emulator
78 * 0 -> running inside the emulator, but GPU emulation not supported
79 * 1 -> running inside the emulator, GPU emulation is supported
Nicolas Capens776951f2015-11-06 10:10:21 -050080 * through the "emulation" host-side OpenGL ES implementation.
81 * 2 -> running inside the emulator, GPU emulation is supported
82 * through a guest-side vendor driver's OpenGL ES implementation.
David 'Digit' Turner80b30c22011-08-26 17:38:47 +020083 */
84static int
85checkGlesEmulationStatus(void)
86{
87 /* We're going to check for the following kernel parameters:
88 *
89 * qemu=1 -> tells us that we run inside the emulator
90 * android.qemu.gles=<number> -> tells us the GLES GPU emulation status
91 *
92 * Note that we will return <number> if we find it. This let us support
93 * more additionnal emulation modes in the future.
94 */
95 char prop[PROPERTY_VALUE_MAX];
96 int result = -1;
97
98 /* First, check for qemu=1 */
99 property_get("ro.kernel.qemu",prop,"0");
100 if (atoi(prop) != 1)
101 return -1;
102
103 /* We are in the emulator, get GPU status value */
bohu69e5b1a2016-02-19 17:15:20 -0800104 property_get("qemu.gles",prop,"0");
David 'Digit' Turner80b30c22011-08-26 17:38:47 +0200105 return atoi(prop);
106}
107
Jesse Hall1508ae62017-01-19 17:43:26 -0800108static void* do_dlopen(const char* path, int mode) {
109 ATRACE_CALL();
110 return dlopen(path, mode);
111}
112
Jiyong Park5910dc72017-04-05 14:23:52 +0900113static void* do_android_dlopen_ext(const char* path, int mode, const android_dlextinfo* info) {
114 ATRACE_CALL();
115 return android_dlopen_ext(path, mode, info);
116}
117
Mathias Agopiande586972009-05-28 17:39:03 -0700118// ----------------------------------------------------------------------------
119
Jesse Hall94cdba92013-07-11 09:40:35 -0700120Loader::driver_t::driver_t(void* gles)
Mathias Agopiande586972009-05-28 17:39:03 -0700121{
122 dso[0] = gles;
123 for (size_t i=1 ; i<NELEM(dso) ; i++)
124 dso[i] = 0;
125}
126
Jesse Hall94cdba92013-07-11 09:40:35 -0700127Loader::driver_t::~driver_t()
Mathias Agopiande586972009-05-28 17:39:03 -0700128{
129 for (size_t i=0 ; i<NELEM(dso) ; i++) {
130 if (dso[i]) {
131 dlclose(dso[i]);
132 dso[i] = 0;
133 }
134 }
135}
136
Mathias Agopian65421432017-03-08 11:49:05 -0800137int Loader::driver_t::set(void* hnd, int32_t api)
Mathias Agopiande586972009-05-28 17:39:03 -0700138{
139 switch (api) {
140 case EGL:
141 dso[0] = hnd;
142 break;
143 case GLESv1_CM:
144 dso[1] = hnd;
145 break;
146 case GLESv2:
147 dso[2] = hnd;
148 break;
149 default:
Mathias Agopian65421432017-03-08 11:49:05 -0800150 return -EOVERFLOW;
Mathias Agopiande586972009-05-28 17:39:03 -0700151 }
Mathias Agopian65421432017-03-08 11:49:05 -0800152 return 0;
Mathias Agopiande586972009-05-28 17:39:03 -0700153}
154
155// ----------------------------------------------------------------------------
156
Mathias Agopiande586972009-05-28 17:39:03 -0700157Loader::Loader()
Mathias Agopian991d2542017-02-06 13:51:32 -0800158 : getProcAddress(NULL)
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800159{
Mathias Agopiande586972009-05-28 17:39:03 -0700160}
161
Mathias Agopian99381422013-04-23 20:52:29 +0200162Loader::~Loader() {
Mathias Agopiande586972009-05-28 17:39:03 -0700163}
164
Jesse Hallc07b5202013-07-04 12:08:16 -0700165static void* load_wrapper(const char* path) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800166 void* so = do_dlopen(path, RTLD_NOW | RTLD_LOCAL);
Jesse Hallc07b5202013-07-04 12:08:16 -0700167 ALOGE_IF(!so, "dlopen(\"%s\") failed: %s", path, dlerror());
168 return so;
169}
170
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700171#ifndef EGL_WRAPPER_DIR
172#if defined(__LP64__)
173#define EGL_WRAPPER_DIR "/system/lib64"
174#else
175#define EGL_WRAPPER_DIR "/system/lib"
176#endif
177#endif
178
bohu69e5b1a2016-02-19 17:15:20 -0800179static void setEmulatorGlesValue(void) {
180 char prop[PROPERTY_VALUE_MAX];
181 property_get("ro.kernel.qemu", prop, "0");
182 if (atoi(prop) != 1) return;
183
184 property_get("ro.kernel.qemu.gles",prop,"0");
185 if (atoi(prop) == 1) {
186 ALOGD("Emulator has host GPU support, qemu.gles is set to 1.");
187 property_set("qemu.gles", "1");
188 return;
189 }
190
191 // for now, checking the following
192 // directory is good enough for emulator system images
193 const char* vendor_lib_path =
194#if defined(__LP64__)
195 "/vendor/lib64/egl";
196#else
197 "/vendor/lib/egl";
198#endif
199
200 const bool has_vendor_lib = (access(vendor_lib_path, R_OK) == 0);
201 if (has_vendor_lib) {
202 ALOGD("Emulator has vendor provided software renderer, qemu.gles is set to 2.");
203 property_set("qemu.gles", "2");
204 } else {
205 ALOGD("Emulator without GPU support detected. "
206 "Fallback to legacy software renderer, qemu.gles is set to 0.");
207 property_set("qemu.gles", "0");
208 }
209}
210
Mathias Agopianada798b2012-02-13 17:09:30 -0800211void* Loader::open(egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700212{
Jesse Hall1508ae62017-01-19 17:43:26 -0800213 ATRACE_CALL();
214
Mathias Agopiande586972009-05-28 17:39:03 -0700215 void* dso;
Mathias Agopiande586972009-05-28 17:39:03 -0700216 driver_t* hnd = 0;
Jesse Hall94cdba92013-07-11 09:40:35 -0700217
bohu69e5b1a2016-02-19 17:15:20 -0800218 setEmulatorGlesValue();
219
Mathias Agopian99381422013-04-23 20:52:29 +0200220 dso = load_driver("GLES", cnx, EGL | GLESv1_CM | GLESv2);
221 if (dso) {
222 hnd = new driver_t(dso);
223 } else {
224 // Always load EGL first
225 dso = load_driver("EGL", cnx, EGL);
Mathias Agopiande586972009-05-28 17:39:03 -0700226 if (dso) {
227 hnd = new driver_t(dso);
Mathias Agopian99381422013-04-23 20:52:29 +0200228 hnd->set( load_driver("GLESv1_CM", cnx, GLESv1_CM), GLESv1_CM );
229 hnd->set( load_driver("GLESv2", cnx, GLESv2), GLESv2 );
Mathias Agopiande586972009-05-28 17:39:03 -0700230 }
231 }
232
Mathias Agopian99381422013-04-23 20:52:29 +0200233 LOG_ALWAYS_FATAL_IF(!hnd, "couldn't find an OpenGL ES implementation");
Jesse Hallc07b5202013-07-04 12:08:16 -0700234
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700235 cnx->libEgl = load_wrapper(EGL_WRAPPER_DIR "/libEGL.so");
236 cnx->libGles2 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv2.so");
237 cnx->libGles1 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv1_CM.so");
238
Michael Chockc0ec5e22014-01-27 08:14:33 -0800239 LOG_ALWAYS_FATAL_IF(!cnx->libEgl,
240 "couldn't load system EGL wrapper libraries");
241
Jesse Hallc07b5202013-07-04 12:08:16 -0700242 LOG_ALWAYS_FATAL_IF(!cnx->libGles2 || !cnx->libGles1,
243 "couldn't load system OpenGL ES wrapper libraries");
244
Mathias Agopiande586972009-05-28 17:39:03 -0700245 return (void*)hnd;
246}
247
Mathias Agopian65421432017-03-08 11:49:05 -0800248void Loader::close(void* driver)
Mathias Agopiande586972009-05-28 17:39:03 -0700249{
250 driver_t* hnd = (driver_t*)driver;
251 delete hnd;
Mathias Agopiande586972009-05-28 17:39:03 -0700252}
253
Jesse Hall94cdba92013-07-11 09:40:35 -0700254void Loader::init_api(void* dso,
255 char const * const * api,
256 __eglMustCastToProperFunctionPointerType* curr,
257 getProcAddressType getProcAddress)
Mathias Agopiande586972009-05-28 17:39:03 -0700258{
Jesse Hall1508ae62017-01-19 17:43:26 -0800259 ATRACE_CALL();
260
Mathias Agopian7773c432012-02-13 20:06:08 -0800261 const ssize_t SIZE = 256;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700262 char scrap[SIZE];
Mathias Agopiande586972009-05-28 17:39:03 -0700263 while (*api) {
264 char const * name = *api;
Jesse Hall94cdba92013-07-11 09:40:35 -0700265 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700266 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
267 if (f == NULL) {
268 // couldn't find the entry-point, use eglGetProcAddress()
269 f = getProcAddress(name);
270 }
271 if (f == NULL) {
272 // Try without the OES postfix
273 ssize_t index = ssize_t(strlen(name)) - 3;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700274 if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) {
Mathias Agopiande586972009-05-28 17:39:03 -0700275 strncpy(scrap, name, index);
276 scrap[index] = 0;
277 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000278 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700279 }
280 }
281 if (f == NULL) {
282 // Try with the OES postfix
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700283 ssize_t index = ssize_t(strlen(name)) - 3;
284 if (index>0 && strcmp(name+index, "OES")) {
285 snprintf(scrap, SIZE, "%sOES", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700286 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000287 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700288 }
289 }
290 if (f == NULL) {
Steve Block9d453682011-12-20 16:23:08 +0000291 //ALOGD("%s", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700292 f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented;
Mathias Agopian48d438d2012-01-28 21:44:00 -0800293
294 /*
295 * GL_EXT_debug_label is special, we always report it as
296 * supported, it's handled by GLES_trace. If GLES_trace is not
297 * enabled, then these are no-ops.
298 */
299 if (!strcmp(name, "glInsertEventMarkerEXT")) {
300 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
301 } else if (!strcmp(name, "glPushGroupMarkerEXT")) {
302 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
303 } else if (!strcmp(name, "glPopGroupMarkerEXT")) {
304 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
305 }
Mathias Agopiande586972009-05-28 17:39:03 -0700306 }
307 *curr++ = f;
308 api++;
309 }
310}
311
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800312static void* load_system_driver(const char* kind) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800313 ATRACE_CALL();
Mathias Agopian99381422013-04-23 20:52:29 +0200314 class MatchFile {
315 public:
Mathias Agopian65421432017-03-08 11:49:05 -0800316 static std::string find(const char* kind) {
317 std::string result;
Nicolas Capens776951f2015-11-06 10:10:21 -0500318 int emulationStatus = checkGlesEmulationStatus();
319 switch (emulationStatus) {
320 case 0:
Nicolas Capens776951f2015-11-06 10:10:21 -0500321#if defined(__LP64__)
Mathias Agopian65421432017-03-08 11:49:05 -0800322 result = "/system/lib64/egl/libGLES_android.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500323#else
Mathias Agopian65421432017-03-08 11:49:05 -0800324 result = "/system/lib/egl/libGLES_android.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500325#endif
326 return result;
327 case 1:
328 // Use host-side OpenGL through the "emulation" library
329#if defined(__LP64__)
Mathias Agopian65421432017-03-08 11:49:05 -0800330 result = std::string("/system/lib64/egl/lib") + kind + "_emulation.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500331#else
Mathias Agopian65421432017-03-08 11:49:05 -0800332 result = std::string("/system/lib/egl/lib") + kind + "_emulation.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500333#endif
334 return result;
335 default:
336 // Not in emulator, or use other guest-side implementation
337 break;
338 }
339
Mathias Agopian65421432017-03-08 11:49:05 -0800340 std::string pattern = std::string("lib") + kind;
Mathias Agopian99381422013-04-23 20:52:29 +0200341 const char* const searchPaths[] = {
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800342#if defined(__LP64__)
343 "/vendor/lib64/egl",
344 "/system/lib64/egl"
345#else
Mathias Agopian99381422013-04-23 20:52:29 +0200346 "/vendor/lib/egl",
347 "/system/lib/egl"
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800348#endif
Mathias Agopian99381422013-04-23 20:52:29 +0200349 };
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700350
Mathias Agopian99381422013-04-23 20:52:29 +0200351 // first, we search for the exact name of the GLES userspace
352 // driver in both locations.
353 // i.e.:
354 // libGLES.so, or:
355 // libEGL.so, libGLESv1_CM.so, libGLESv2.so
356
357 for (size_t i=0 ; i<NELEM(searchPaths) ; i++) {
358 if (find(result, pattern, searchPaths[i], true)) {
359 return result;
360 }
361 }
362
363 // for compatibility with the old "egl.cfg" naming convention
364 // we look for files that match:
365 // libGLES_*.so, or:
366 // libEGL_*.so, libGLESv1_CM_*.so, libGLESv2_*.so
367
368 pattern.append("_");
369 for (size_t i=0 ; i<NELEM(searchPaths) ; i++) {
370 if (find(result, pattern, searchPaths[i], false)) {
371 return result;
372 }
373 }
374
375 // we didn't find the driver. gah.
376 result.clear();
377 return result;
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700378 }
Mathias Agopian99381422013-04-23 20:52:29 +0200379
380 private:
Mathias Agopian65421432017-03-08 11:49:05 -0800381 static bool find(std::string& result,
382 const std::string& pattern, const char* const search, bool exact) {
Mathias Agopian99381422013-04-23 20:52:29 +0200383 if (exact) {
Mathias Agopian65421432017-03-08 11:49:05 -0800384 std::string absolutePath = std::string(search) + "/" + pattern;
385 if (!access(absolutePath.c_str(), R_OK)) {
Mathias Agopian99381422013-04-23 20:52:29 +0200386 result = absolutePath;
387 return true;
388 }
389 return false;
390 }
391
392 DIR* d = opendir(search);
393 if (d != NULL) {
394 struct dirent cur;
395 struct dirent* e;
396 while (readdir_r(d, &cur, &e) == 0 && e) {
397 if (e->d_type == DT_DIR) {
398 continue;
399 }
400 if (!strcmp(e->d_name, "libGLES_android.so")) {
401 // always skip the software renderer
402 continue;
403 }
Mathias Agopian65421432017-03-08 11:49:05 -0800404 if (strstr(e->d_name, pattern.c_str()) == e->d_name) {
Mathias Agopian99381422013-04-23 20:52:29 +0200405 if (!strcmp(e->d_name + strlen(e->d_name) - 3, ".so")) {
Mathias Agopian65421432017-03-08 11:49:05 -0800406 result = std::string(search) + "/" + e->d_name;
Mathias Agopian99381422013-04-23 20:52:29 +0200407 closedir(d);
408 return true;
409 }
410 }
411 }
412 closedir(d);
413 }
414 return false;
415 }
416 };
417
418
Mathias Agopian65421432017-03-08 11:49:05 -0800419 std::string absolutePath = MatchFile::find(kind);
420 if (absolutePath.empty()) {
Mathias Agopian99381422013-04-23 20:52:29 +0200421 // this happens often, we don't want to log an error
422 return 0;
Mathias Agopian8c173842009-09-20 16:01:02 -0700423 }
Mathias Agopian65421432017-03-08 11:49:05 -0800424 const char* const driver_absolute_path = absolutePath.c_str();
Mathias Agopiande586972009-05-28 17:39:03 -0700425
Jiyong Park5910dc72017-04-05 14:23:52 +0900426 // Try to load drivers from the 'sphal' namespace, if it exist. Fall back to
427 // the original routine when the namespace does not exist or the load from
428 // the namespace fails.
429 // See /system/core/rootdir/etc/ld.config.txt for the configuration of the
430 // sphal namespace.
431 android_namespace_t* sphal_namespace = android_get_exported_namespace("sphal");
432 if (sphal_namespace != NULL) {
433 const android_dlextinfo dlextinfo = {
434 .flags = ANDROID_DLEXT_USE_NAMESPACE,
435 .library_namespace = sphal_namespace,
436 };
437 void* dso = do_android_dlopen_ext(driver_absolute_path, RTLD_LOCAL | RTLD_NOW, &dlextinfo);
438 if (dso) {
439 ALOGD("loaded %s from sphal namespace", driver_absolute_path);
440 return dso;
441 }
442 else {
443 ALOGW("failed to load %s from sphal namespace: %s", driver_absolute_path, dlerror());
444 }
445 }
446
Jesse Hall1508ae62017-01-19 17:43:26 -0800447 void* dso = do_dlopen(driver_absolute_path, 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;
Mathias Agopian991d2542017-02-06 13:51:32 -0800490 android_namespace_t* ns = android_getDriverNamespace();
491 if (ns) {
492 dso = load_updated_driver(kind, ns);
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800493 }
494 if (!dso) {
495 dso = load_system_driver(kind);
496 if (!dso)
497 return NULL;
498 }
499
Mathias Agopiande586972009-05-28 17:39:03 -0700500 if (mask & EGL) {
501 getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress");
502
Jesse Hall94cdba92013-07-11 09:40:35 -0700503 ALOGE_IF(!getProcAddress,
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800504 "can't find eglGetProcAddress() in EGL driver library");
Mathias Agopiande586972009-05-28 17:39:03 -0700505
Mathias Agopian618fa102009-10-14 02:06:37 -0700506 egl_t* egl = &cnx->egl;
Mathias Agopiande586972009-05-28 17:39:03 -0700507 __eglMustCastToProperFunctionPointerType* curr =
508 (__eglMustCastToProperFunctionPointerType*)egl;
509 char const * const * api = egl_names;
510 while (*api) {
511 char const * name = *api;
Jesse Hall94cdba92013-07-11 09:40:35 -0700512 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700513 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
514 if (f == NULL) {
515 // couldn't find the entry-point, use eglGetProcAddress()
516 f = getProcAddress(name);
517 if (f == NULL) {
518 f = (__eglMustCastToProperFunctionPointerType)0;
519 }
520 }
521 *curr++ = f;
522 api++;
523 }
524 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700525
Mathias Agopiande586972009-05-28 17:39:03 -0700526 if (mask & GLESv1_CM) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700527 init_api(dso, gl_names,
528 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800529 &cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl,
Mathias Agopian618fa102009-10-14 02:06:37 -0700530 getProcAddress);
Mathias Agopiande586972009-05-28 17:39:03 -0700531 }
532
533 if (mask & GLESv2) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700534 init_api(dso, gl_names,
535 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800536 &cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl,
Mathias Agopiande586972009-05-28 17:39:03 -0700537 getProcAddress);
538 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700539
Mathias Agopiande586972009-05-28 17:39:03 -0700540 return dso;
541}
542
543// ----------------------------------------------------------------------------
544}; // namespace android
545// ----------------------------------------------------------------------------