Jesse Hall | 94cdba9 | 2013-07-11 09:40:35 -0700 | [diff] [blame] | 1 | /* |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 2 | ** Copyright 2007, The Android Open Source Project |
| 3 | ** |
Jesse Hall | 94cdba9 | 2013-07-11 09:40:35 -0700 | [diff] [blame] | 4 | ** 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 Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 7 | ** |
Jesse Hall | 94cdba9 | 2013-07-11 09:40:35 -0700 | [diff] [blame] | 8 | ** http://www.apache.org/licenses/LICENSE-2.0 |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 9 | ** |
Jesse Hall | 94cdba9 | 2013-07-11 09:40:35 -0700 | [diff] [blame] | 10 | ** 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 Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 14 | ** limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <ctype.h> |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 18 | #include <dirent.h> |
Mark Salyzyn | a5e161b | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 19 | #include <dlfcn.h> |
| 20 | #include <errno.h> |
| 21 | #include <limits.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 25 | |
David 'Digit' Turner | 80b30c2 | 2011-08-26 17:38:47 +0200 | [diff] [blame] | 26 | #include <cutils/properties.h> |
Mark Salyzyn | 7823e12 | 2016-09-29 08:08:05 -0700 | [diff] [blame^] | 27 | #include <log/log.h> |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 28 | |
| 29 | #include <EGL/egl.h> |
| 30 | |
Mathias Agopian | 1cadb25 | 2011-05-23 17:26:14 -0700 | [diff] [blame] | 31 | #include "egldefs.h" |
Mathias Agopian | 1cadb25 | 2011-05-23 17:26:14 -0700 | [diff] [blame] | 32 | #include "Loader.h" |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 33 | |
| 34 | // ---------------------------------------------------------------------------- |
| 35 | namespace android { |
| 36 | // ---------------------------------------------------------------------------- |
| 37 | |
| 38 | |
| 39 | /* |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 40 | * EGL userspace drivers must be provided either: |
| 41 | * - as a single library: |
| 42 | * /vendor/lib/egl/libGLES.so |
| 43 | * |
| 44 | * - as separate libraries: |
| 45 | * /vendor/lib/egl/libEGL.so |
| 46 | * /vendor/lib/egl/libGLESv1_CM.so |
| 47 | * /vendor/lib/egl/libGLESv2.so |
| 48 | * |
| 49 | * The software renderer for the emulator must be provided as a single |
| 50 | * library at: |
| 51 | * |
| 52 | * /system/lib/egl/libGLES_android.so |
| 53 | * |
| 54 | * |
| 55 | * For backward compatibility and to facilitate the transition to |
| 56 | * this new naming scheme, the loader will additionally look for: |
Jesse Hall | 94cdba9 | 2013-07-11 09:40:35 -0700 | [diff] [blame] | 57 | * |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 58 | * /{vendor|system}/lib/egl/lib{GLES | [EGL|GLESv1_CM|GLESv2]}_*.so |
Jesse Hall | 94cdba9 | 2013-07-11 09:40:35 -0700 | [diff] [blame] | 59 | * |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 60 | */ |
| 61 | |
| 62 | ANDROID_SINGLETON_STATIC_INSTANCE( Loader ) |
| 63 | |
David 'Digit' Turner | 80b30c2 | 2011-08-26 17:38:47 +0200 | [diff] [blame] | 64 | /* This function is called to check whether we run inside the emulator, |
| 65 | * and if this is the case whether GLES GPU emulation is supported. |
| 66 | * |
| 67 | * Returned values are: |
| 68 | * -1 -> not running inside the emulator |
| 69 | * 0 -> running inside the emulator, but GPU emulation not supported |
| 70 | * 1 -> running inside the emulator, GPU emulation is supported |
Nicolas Capens | 776951f | 2015-11-06 10:10:21 -0500 | [diff] [blame] | 71 | * through the "emulation" host-side OpenGL ES implementation. |
| 72 | * 2 -> running inside the emulator, GPU emulation is supported |
| 73 | * through a guest-side vendor driver's OpenGL ES implementation. |
David 'Digit' Turner | 80b30c2 | 2011-08-26 17:38:47 +0200 | [diff] [blame] | 74 | */ |
| 75 | static int |
| 76 | checkGlesEmulationStatus(void) |
| 77 | { |
| 78 | /* We're going to check for the following kernel parameters: |
| 79 | * |
| 80 | * qemu=1 -> tells us that we run inside the emulator |
| 81 | * android.qemu.gles=<number> -> tells us the GLES GPU emulation status |
| 82 | * |
| 83 | * Note that we will return <number> if we find it. This let us support |
| 84 | * more additionnal emulation modes in the future. |
| 85 | */ |
| 86 | char prop[PROPERTY_VALUE_MAX]; |
| 87 | int result = -1; |
| 88 | |
| 89 | /* First, check for qemu=1 */ |
| 90 | property_get("ro.kernel.qemu",prop,"0"); |
| 91 | if (atoi(prop) != 1) |
| 92 | return -1; |
| 93 | |
| 94 | /* We are in the emulator, get GPU status value */ |
bohu | 69e5b1a | 2016-02-19 17:15:20 -0800 | [diff] [blame] | 95 | property_get("qemu.gles",prop,"0"); |
David 'Digit' Turner | 80b30c2 | 2011-08-26 17:38:47 +0200 | [diff] [blame] | 96 | return atoi(prop); |
| 97 | } |
| 98 | |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 99 | // ---------------------------------------------------------------------------- |
| 100 | |
Mathias Agopian | d75f84d | 2012-06-05 21:44:43 -0700 | [diff] [blame] | 101 | static char const * getProcessCmdline() { |
| 102 | long pid = getpid(); |
| 103 | char procPath[128]; |
| 104 | snprintf(procPath, 128, "/proc/%ld/cmdline", pid); |
| 105 | FILE * file = fopen(procPath, "r"); |
| 106 | if (file) { |
| 107 | static char cmdline[256]; |
| 108 | char *str = fgets(cmdline, sizeof(cmdline) - 1, file); |
| 109 | fclose(file); |
| 110 | if (str) { |
| 111 | return cmdline; |
| 112 | } |
| 113 | } |
| 114 | return NULL; |
| 115 | } |
| 116 | |
| 117 | // ---------------------------------------------------------------------------- |
| 118 | |
Jesse Hall | 94cdba9 | 2013-07-11 09:40:35 -0700 | [diff] [blame] | 119 | Loader::driver_t::driver_t(void* gles) |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 120 | { |
| 121 | dso[0] = gles; |
| 122 | for (size_t i=1 ; i<NELEM(dso) ; i++) |
| 123 | dso[i] = 0; |
| 124 | } |
| 125 | |
Jesse Hall | 94cdba9 | 2013-07-11 09:40:35 -0700 | [diff] [blame] | 126 | Loader::driver_t::~driver_t() |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 127 | { |
| 128 | for (size_t i=0 ; i<NELEM(dso) ; i++) { |
| 129 | if (dso[i]) { |
| 130 | dlclose(dso[i]); |
| 131 | dso[i] = 0; |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | status_t Loader::driver_t::set(void* hnd, int32_t api) |
| 137 | { |
| 138 | switch (api) { |
| 139 | case EGL: |
| 140 | dso[0] = hnd; |
| 141 | break; |
| 142 | case GLESv1_CM: |
| 143 | dso[1] = hnd; |
| 144 | break; |
| 145 | case GLESv2: |
| 146 | dso[2] = hnd; |
| 147 | break; |
| 148 | default: |
| 149 | return BAD_INDEX; |
| 150 | } |
| 151 | return NO_ERROR; |
| 152 | } |
| 153 | |
| 154 | // ---------------------------------------------------------------------------- |
| 155 | |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 156 | Loader::Loader() |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 157 | : getProcAddress(NULL) { |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 160 | Loader::~Loader() { |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Jesse Hall | c07b520 | 2013-07-04 12:08:16 -0700 | [diff] [blame] | 163 | static void* load_wrapper(const char* path) { |
| 164 | void* so = dlopen(path, RTLD_NOW | RTLD_LOCAL); |
| 165 | ALOGE_IF(!so, "dlopen(\"%s\") failed: %s", path, dlerror()); |
| 166 | return so; |
| 167 | } |
| 168 | |
Evgenii Stepanov | c2466e6 | 2015-07-08 15:49:52 -0700 | [diff] [blame] | 169 | #ifndef EGL_WRAPPER_DIR |
| 170 | #if defined(__LP64__) |
| 171 | #define EGL_WRAPPER_DIR "/system/lib64" |
| 172 | #else |
| 173 | #define EGL_WRAPPER_DIR "/system/lib" |
| 174 | #endif |
| 175 | #endif |
| 176 | |
bohu | 69e5b1a | 2016-02-19 17:15:20 -0800 | [diff] [blame] | 177 | static void setEmulatorGlesValue(void) { |
| 178 | char prop[PROPERTY_VALUE_MAX]; |
| 179 | property_get("ro.kernel.qemu", prop, "0"); |
| 180 | if (atoi(prop) != 1) return; |
| 181 | |
| 182 | property_get("ro.kernel.qemu.gles",prop,"0"); |
| 183 | if (atoi(prop) == 1) { |
| 184 | ALOGD("Emulator has host GPU support, qemu.gles is set to 1."); |
| 185 | property_set("qemu.gles", "1"); |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | // for now, checking the following |
| 190 | // directory is good enough for emulator system images |
| 191 | const char* vendor_lib_path = |
| 192 | #if defined(__LP64__) |
| 193 | "/vendor/lib64/egl"; |
| 194 | #else |
| 195 | "/vendor/lib/egl"; |
| 196 | #endif |
| 197 | |
| 198 | const bool has_vendor_lib = (access(vendor_lib_path, R_OK) == 0); |
| 199 | if (has_vendor_lib) { |
| 200 | ALOGD("Emulator has vendor provided software renderer, qemu.gles is set to 2."); |
| 201 | property_set("qemu.gles", "2"); |
| 202 | } else { |
| 203 | ALOGD("Emulator without GPU support detected. " |
| 204 | "Fallback to legacy software renderer, qemu.gles is set to 0."); |
| 205 | property_set("qemu.gles", "0"); |
| 206 | } |
| 207 | } |
| 208 | |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 209 | void* Loader::open(egl_connection_t* cnx) |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 210 | { |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 211 | void* dso; |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 212 | driver_t* hnd = 0; |
Jesse Hall | 94cdba9 | 2013-07-11 09:40:35 -0700 | [diff] [blame] | 213 | |
bohu | 69e5b1a | 2016-02-19 17:15:20 -0800 | [diff] [blame] | 214 | setEmulatorGlesValue(); |
| 215 | |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 216 | dso = load_driver("GLES", cnx, EGL | GLESv1_CM | GLESv2); |
| 217 | if (dso) { |
| 218 | hnd = new driver_t(dso); |
| 219 | } else { |
| 220 | // Always load EGL first |
| 221 | dso = load_driver("EGL", cnx, EGL); |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 222 | if (dso) { |
| 223 | hnd = new driver_t(dso); |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 224 | hnd->set( load_driver("GLESv1_CM", cnx, GLESv1_CM), GLESv1_CM ); |
| 225 | hnd->set( load_driver("GLESv2", cnx, GLESv2), GLESv2 ); |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 229 | LOG_ALWAYS_FATAL_IF(!hnd, "couldn't find an OpenGL ES implementation"); |
Jesse Hall | c07b520 | 2013-07-04 12:08:16 -0700 | [diff] [blame] | 230 | |
Evgenii Stepanov | c2466e6 | 2015-07-08 15:49:52 -0700 | [diff] [blame] | 231 | cnx->libEgl = load_wrapper(EGL_WRAPPER_DIR "/libEGL.so"); |
| 232 | cnx->libGles2 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv2.so"); |
| 233 | cnx->libGles1 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv1_CM.so"); |
| 234 | |
Michael Chock | c0ec5e2 | 2014-01-27 08:14:33 -0800 | [diff] [blame] | 235 | LOG_ALWAYS_FATAL_IF(!cnx->libEgl, |
| 236 | "couldn't load system EGL wrapper libraries"); |
| 237 | |
Jesse Hall | c07b520 | 2013-07-04 12:08:16 -0700 | [diff] [blame] | 238 | LOG_ALWAYS_FATAL_IF(!cnx->libGles2 || !cnx->libGles1, |
| 239 | "couldn't load system OpenGL ES wrapper libraries"); |
| 240 | |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 241 | return (void*)hnd; |
| 242 | } |
| 243 | |
| 244 | status_t Loader::close(void* driver) |
| 245 | { |
| 246 | driver_t* hnd = (driver_t*)driver; |
| 247 | delete hnd; |
| 248 | return NO_ERROR; |
| 249 | } |
| 250 | |
Jesse Hall | 94cdba9 | 2013-07-11 09:40:35 -0700 | [diff] [blame] | 251 | void Loader::init_api(void* dso, |
| 252 | char const * const * api, |
| 253 | __eglMustCastToProperFunctionPointerType* curr, |
| 254 | getProcAddressType getProcAddress) |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 255 | { |
Mathias Agopian | 7773c43 | 2012-02-13 20:06:08 -0800 | [diff] [blame] | 256 | const ssize_t SIZE = 256; |
Mathias Agopian | 0ad71a9 | 2011-05-11 20:37:47 -0700 | [diff] [blame] | 257 | char scrap[SIZE]; |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 258 | while (*api) { |
| 259 | char const * name = *api; |
Jesse Hall | 94cdba9 | 2013-07-11 09:40:35 -0700 | [diff] [blame] | 260 | __eglMustCastToProperFunctionPointerType f = |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 261 | (__eglMustCastToProperFunctionPointerType)dlsym(dso, name); |
| 262 | if (f == NULL) { |
| 263 | // couldn't find the entry-point, use eglGetProcAddress() |
| 264 | f = getProcAddress(name); |
| 265 | } |
| 266 | if (f == NULL) { |
| 267 | // Try without the OES postfix |
| 268 | ssize_t index = ssize_t(strlen(name)) - 3; |
Mathias Agopian | 0ad71a9 | 2011-05-11 20:37:47 -0700 | [diff] [blame] | 269 | if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) { |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 270 | strncpy(scrap, name, index); |
| 271 | scrap[index] = 0; |
| 272 | f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap); |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 273 | //ALOGD_IF(f, "found <%s> instead", scrap); |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | if (f == NULL) { |
| 277 | // Try with the OES postfix |
Mathias Agopian | 0ad71a9 | 2011-05-11 20:37:47 -0700 | [diff] [blame] | 278 | ssize_t index = ssize_t(strlen(name)) - 3; |
| 279 | if (index>0 && strcmp(name+index, "OES")) { |
| 280 | snprintf(scrap, SIZE, "%sOES", name); |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 281 | f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap); |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 282 | //ALOGD_IF(f, "found <%s> instead", scrap); |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | if (f == NULL) { |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 286 | //ALOGD("%s", name); |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 287 | f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented; |
Mathias Agopian | 48d438d | 2012-01-28 21:44:00 -0800 | [diff] [blame] | 288 | |
| 289 | /* |
| 290 | * GL_EXT_debug_label is special, we always report it as |
| 291 | * supported, it's handled by GLES_trace. If GLES_trace is not |
| 292 | * enabled, then these are no-ops. |
| 293 | */ |
| 294 | if (!strcmp(name, "glInsertEventMarkerEXT")) { |
| 295 | f = (__eglMustCastToProperFunctionPointerType)gl_noop; |
| 296 | } else if (!strcmp(name, "glPushGroupMarkerEXT")) { |
| 297 | f = (__eglMustCastToProperFunctionPointerType)gl_noop; |
| 298 | } else if (!strcmp(name, "glPopGroupMarkerEXT")) { |
| 299 | f = (__eglMustCastToProperFunctionPointerType)gl_noop; |
| 300 | } |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 301 | } |
| 302 | *curr++ = f; |
| 303 | api++; |
| 304 | } |
| 305 | } |
| 306 | |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 307 | void *Loader::load_driver(const char* kind, |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 308 | egl_connection_t* cnx, uint32_t mask) |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 309 | { |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 310 | class MatchFile { |
| 311 | public: |
| 312 | static String8 find(const char* kind) { |
| 313 | String8 result; |
Nicolas Capens | 776951f | 2015-11-06 10:10:21 -0500 | [diff] [blame] | 314 | int emulationStatus = checkGlesEmulationStatus(); |
| 315 | switch (emulationStatus) { |
| 316 | case 0: |
Nicolas Capens | 776951f | 2015-11-06 10:10:21 -0500 | [diff] [blame] | 317 | #if defined(__LP64__) |
| 318 | result.setTo("/system/lib64/egl/libGLES_android.so"); |
| 319 | #else |
| 320 | result.setTo("/system/lib/egl/libGLES_android.so"); |
| 321 | #endif |
| 322 | return result; |
| 323 | case 1: |
| 324 | // Use host-side OpenGL through the "emulation" library |
| 325 | #if defined(__LP64__) |
| 326 | result.appendFormat("/system/lib64/egl/lib%s_emulation.so", kind); |
| 327 | #else |
| 328 | result.appendFormat("/system/lib/egl/lib%s_emulation.so", kind); |
| 329 | #endif |
| 330 | return result; |
| 331 | default: |
| 332 | // Not in emulator, or use other guest-side implementation |
| 333 | break; |
| 334 | } |
| 335 | |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 336 | String8 pattern; |
| 337 | pattern.appendFormat("lib%s", kind); |
| 338 | const char* const searchPaths[] = { |
Dan Willemsen | 8edb8f5 | 2014-02-16 10:23:54 -0800 | [diff] [blame] | 339 | #if defined(__LP64__) |
| 340 | "/vendor/lib64/egl", |
| 341 | "/system/lib64/egl" |
| 342 | #else |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 343 | "/vendor/lib/egl", |
| 344 | "/system/lib/egl" |
Dan Willemsen | 8edb8f5 | 2014-02-16 10:23:54 -0800 | [diff] [blame] | 345 | #endif |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 346 | }; |
Brian Swetland | 2b9e4f6 | 2010-09-20 12:58:15 -0700 | [diff] [blame] | 347 | |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 348 | // first, we search for the exact name of the GLES userspace |
| 349 | // driver in both locations. |
| 350 | // i.e.: |
| 351 | // libGLES.so, or: |
| 352 | // libEGL.so, libGLESv1_CM.so, libGLESv2.so |
| 353 | |
| 354 | for (size_t i=0 ; i<NELEM(searchPaths) ; i++) { |
| 355 | if (find(result, pattern, searchPaths[i], true)) { |
| 356 | return result; |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | // for compatibility with the old "egl.cfg" naming convention |
| 361 | // we look for files that match: |
| 362 | // libGLES_*.so, or: |
| 363 | // libEGL_*.so, libGLESv1_CM_*.so, libGLESv2_*.so |
| 364 | |
| 365 | pattern.append("_"); |
| 366 | for (size_t i=0 ; i<NELEM(searchPaths) ; i++) { |
| 367 | if (find(result, pattern, searchPaths[i], false)) { |
| 368 | return result; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | // we didn't find the driver. gah. |
| 373 | result.clear(); |
| 374 | return result; |
Brian Swetland | 2b9e4f6 | 2010-09-20 12:58:15 -0700 | [diff] [blame] | 375 | } |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 376 | |
| 377 | private: |
| 378 | static bool find(String8& result, |
| 379 | const String8& pattern, const char* const search, bool exact) { |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 380 | if (exact) { |
| 381 | String8 absolutePath; |
| 382 | absolutePath.appendFormat("%s/%s.so", search, pattern.string()); |
| 383 | if (!access(absolutePath.string(), R_OK)) { |
| 384 | result = absolutePath; |
| 385 | return true; |
| 386 | } |
| 387 | return false; |
| 388 | } |
| 389 | |
| 390 | DIR* d = opendir(search); |
| 391 | if (d != NULL) { |
| 392 | struct dirent cur; |
| 393 | struct dirent* e; |
| 394 | while (readdir_r(d, &cur, &e) == 0 && e) { |
| 395 | if (e->d_type == DT_DIR) { |
| 396 | continue; |
| 397 | } |
| 398 | if (!strcmp(e->d_name, "libGLES_android.so")) { |
| 399 | // always skip the software renderer |
| 400 | continue; |
| 401 | } |
| 402 | if (strstr(e->d_name, pattern.string()) == e->d_name) { |
| 403 | if (!strcmp(e->d_name + strlen(e->d_name) - 3, ".so")) { |
| 404 | result.clear(); |
| 405 | result.appendFormat("%s/%s", search, e->d_name); |
| 406 | closedir(d); |
| 407 | return true; |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | closedir(d); |
| 412 | } |
| 413 | return false; |
| 414 | } |
| 415 | }; |
| 416 | |
| 417 | |
| 418 | String8 absolutePath = MatchFile::find(kind); |
| 419 | if (absolutePath.isEmpty()) { |
| 420 | // this happens often, we don't want to log an error |
| 421 | return 0; |
Mathias Agopian | 8c17384 | 2009-09-20 16:01:02 -0700 | [diff] [blame] | 422 | } |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 423 | const char* const driver_absolute_path = absolutePath.string(); |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 424 | |
Mathias Agopian | 8c17384 | 2009-09-20 16:01:02 -0700 | [diff] [blame] | 425 | void* dso = dlopen(driver_absolute_path, RTLD_NOW | RTLD_LOCAL); |
| 426 | if (dso == 0) { |
| 427 | const char* err = dlerror(); |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 428 | ALOGE("load_driver(%s): %s", driver_absolute_path, err?err:"unknown"); |
Mathias Agopian | 8c17384 | 2009-09-20 16:01:02 -0700 | [diff] [blame] | 429 | return 0; |
| 430 | } |
| 431 | |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 432 | ALOGD("loaded %s", driver_absolute_path); |
Mathias Agopian | baca89c | 2009-08-20 19:09:34 -0700 | [diff] [blame] | 433 | |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 434 | if (mask & EGL) { |
| 435 | getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress"); |
| 436 | |
Jesse Hall | 94cdba9 | 2013-07-11 09:40:35 -0700 | [diff] [blame] | 437 | ALOGE_IF(!getProcAddress, |
Mathias Agopian | 8c17384 | 2009-09-20 16:01:02 -0700 | [diff] [blame] | 438 | "can't find eglGetProcAddress() in %s", driver_absolute_path); |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 439 | |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 440 | egl_t* egl = &cnx->egl; |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 441 | __eglMustCastToProperFunctionPointerType* curr = |
| 442 | (__eglMustCastToProperFunctionPointerType*)egl; |
| 443 | char const * const * api = egl_names; |
| 444 | while (*api) { |
| 445 | char const * name = *api; |
Jesse Hall | 94cdba9 | 2013-07-11 09:40:35 -0700 | [diff] [blame] | 446 | __eglMustCastToProperFunctionPointerType f = |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 447 | (__eglMustCastToProperFunctionPointerType)dlsym(dso, name); |
| 448 | if (f == NULL) { |
| 449 | // couldn't find the entry-point, use eglGetProcAddress() |
| 450 | f = getProcAddress(name); |
| 451 | if (f == NULL) { |
| 452 | f = (__eglMustCastToProperFunctionPointerType)0; |
| 453 | } |
| 454 | } |
| 455 | *curr++ = f; |
| 456 | api++; |
| 457 | } |
| 458 | } |
Jesse Hall | 94cdba9 | 2013-07-11 09:40:35 -0700 | [diff] [blame] | 459 | |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 460 | if (mask & GLESv1_CM) { |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 461 | init_api(dso, gl_names, |
| 462 | (__eglMustCastToProperFunctionPointerType*) |
Mathias Agopian | 7773c43 | 2012-02-13 20:06:08 -0800 | [diff] [blame] | 463 | &cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl, |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 464 | getProcAddress); |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | if (mask & GLESv2) { |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 468 | init_api(dso, gl_names, |
| 469 | (__eglMustCastToProperFunctionPointerType*) |
Mathias Agopian | 7773c43 | 2012-02-13 20:06:08 -0800 | [diff] [blame] | 470 | &cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl, |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 471 | getProcAddress); |
| 472 | } |
Jesse Hall | 94cdba9 | 2013-07-11 09:40:35 -0700 | [diff] [blame] | 473 | |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 474 | return dso; |
| 475 | } |
| 476 | |
| 477 | // ---------------------------------------------------------------------------- |
| 478 | }; // namespace android |
| 479 | // ---------------------------------------------------------------------------- |