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 | |
Jesse Hall | 7a8d83e | 2016-12-20 15:24:28 -0800 | [diff] [blame] | 17 | //#define LOG_NDEBUG 0 |
Jesse Hall | 1508ae6 | 2017-01-19 17:43:26 -0800 | [diff] [blame] | 18 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
Jesse Hall | 7a8d83e | 2016-12-20 15:24:28 -0800 | [diff] [blame] | 19 | |
| 20 | #include <array> |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 21 | #include <ctype.h> |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 22 | #include <dirent.h> |
Mark Salyzyn | a5e161b | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 23 | #include <dlfcn.h> |
| 24 | #include <errno.h> |
| 25 | #include <limits.h> |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 29 | |
Jesse Hall | 7a8d83e | 2016-12-20 15:24:28 -0800 | [diff] [blame] | 30 | #include <android/dlext.h> |
David 'Digit' Turner | 80b30c2 | 2011-08-26 17:38:47 +0200 | [diff] [blame] | 31 | #include <cutils/properties.h> |
Mark Salyzyn | 7823e12 | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 32 | #include <log/log.h> |
Jesse Hall | 1508ae6 | 2017-01-19 17:43:26 -0800 | [diff] [blame] | 33 | #include <utils/Trace.h> |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 34 | |
| 35 | #include <EGL/egl.h> |
| 36 | |
Mathias Agopian | 1cadb25 | 2011-05-23 17:26:14 -0700 | [diff] [blame] | 37 | #include "egldefs.h" |
Mathias Agopian | 1cadb25 | 2011-05-23 17:26:14 -0700 | [diff] [blame] | 38 | #include "Loader.h" |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 39 | |
| 40 | // ---------------------------------------------------------------------------- |
| 41 | namespace android { |
| 42 | // ---------------------------------------------------------------------------- |
| 43 | |
| 44 | |
| 45 | /* |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 46 | * 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 Hall | 94cdba9 | 2013-07-11 09:40:35 -0700 | [diff] [blame] | 63 | * |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 64 | * /{vendor|system}/lib/egl/lib{GLES | [EGL|GLESv1_CM|GLESv2]}_*.so |
Jesse Hall | 94cdba9 | 2013-07-11 09:40:35 -0700 | [diff] [blame] | 65 | * |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 66 | */ |
| 67 | |
| 68 | ANDROID_SINGLETON_STATIC_INSTANCE( Loader ) |
| 69 | |
David 'Digit' Turner | 80b30c2 | 2011-08-26 17:38:47 +0200 | [diff] [blame] | 70 | /* This function is called to check whether we run inside the emulator, |
| 71 | * and if this is the case whether GLES GPU emulation is supported. |
| 72 | * |
| 73 | * Returned values are: |
| 74 | * -1 -> not running inside the emulator |
| 75 | * 0 -> running inside the emulator, but GPU emulation not supported |
| 76 | * 1 -> running inside the emulator, GPU emulation is supported |
Nicolas Capens | 776951f | 2015-11-06 10:10:21 -0500 | [diff] [blame] | 77 | * through the "emulation" host-side OpenGL ES implementation. |
| 78 | * 2 -> running inside the emulator, GPU emulation is supported |
| 79 | * through a guest-side vendor driver's OpenGL ES implementation. |
David 'Digit' Turner | 80b30c2 | 2011-08-26 17:38:47 +0200 | [diff] [blame] | 80 | */ |
| 81 | static int |
| 82 | checkGlesEmulationStatus(void) |
| 83 | { |
| 84 | /* We're going to check for the following kernel parameters: |
| 85 | * |
| 86 | * qemu=1 -> tells us that we run inside the emulator |
| 87 | * android.qemu.gles=<number> -> tells us the GLES GPU emulation status |
| 88 | * |
| 89 | * Note that we will return <number> if we find it. This let us support |
| 90 | * more additionnal emulation modes in the future. |
| 91 | */ |
| 92 | char prop[PROPERTY_VALUE_MAX]; |
| 93 | int result = -1; |
| 94 | |
| 95 | /* First, check for qemu=1 */ |
| 96 | property_get("ro.kernel.qemu",prop,"0"); |
| 97 | if (atoi(prop) != 1) |
| 98 | return -1; |
| 99 | |
| 100 | /* We are in the emulator, get GPU status value */ |
bohu | 69e5b1a | 2016-02-19 17:15:20 -0800 | [diff] [blame] | 101 | property_get("qemu.gles",prop,"0"); |
David 'Digit' Turner | 80b30c2 | 2011-08-26 17:38:47 +0200 | [diff] [blame] | 102 | return atoi(prop); |
| 103 | } |
| 104 | |
Jesse Hall | 1508ae6 | 2017-01-19 17:43:26 -0800 | [diff] [blame] | 105 | static void* do_dlopen(const char* path, int mode) { |
| 106 | ATRACE_CALL(); |
| 107 | return dlopen(path, mode); |
| 108 | } |
| 109 | |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 110 | // ---------------------------------------------------------------------------- |
| 111 | |
Jesse Hall | 94cdba9 | 2013-07-11 09:40:35 -0700 | [diff] [blame] | 112 | Loader::driver_t::driver_t(void* gles) |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 113 | { |
| 114 | dso[0] = gles; |
| 115 | for (size_t i=1 ; i<NELEM(dso) ; i++) |
| 116 | dso[i] = 0; |
| 117 | } |
| 118 | |
Jesse Hall | 94cdba9 | 2013-07-11 09:40:35 -0700 | [diff] [blame] | 119 | Loader::driver_t::~driver_t() |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 120 | { |
| 121 | for (size_t i=0 ; i<NELEM(dso) ; i++) { |
| 122 | if (dso[i]) { |
| 123 | dlclose(dso[i]); |
| 124 | dso[i] = 0; |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | status_t Loader::driver_t::set(void* hnd, int32_t api) |
| 130 | { |
| 131 | switch (api) { |
| 132 | case EGL: |
| 133 | dso[0] = hnd; |
| 134 | break; |
| 135 | case GLESv1_CM: |
| 136 | dso[1] = hnd; |
| 137 | break; |
| 138 | case GLESv2: |
| 139 | dso[2] = hnd; |
| 140 | break; |
| 141 | default: |
| 142 | return BAD_INDEX; |
| 143 | } |
| 144 | return NO_ERROR; |
| 145 | } |
| 146 | |
| 147 | // ---------------------------------------------------------------------------- |
| 148 | |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 149 | Loader::Loader() |
Jesse Hall | 7a8d83e | 2016-12-20 15:24:28 -0800 | [diff] [blame] | 150 | : getProcAddress(NULL), |
| 151 | mLibGui(nullptr), |
| 152 | mGetDriverNamespace(nullptr) |
| 153 | { |
| 154 | // FIXME: See note in GraphicsEnv.h about android_getDriverNamespace(). |
| 155 | // libgui should already be loaded in any process that uses libEGL, but |
| 156 | // if for some reason it isn't, then we're not going to get a driver |
| 157 | // namespace anyway, so don't force it to be loaded. |
| 158 | mLibGui = dlopen("libgui.so", RTLD_NOLOAD | RTLD_LOCAL | RTLD_LAZY); |
| 159 | if (!mLibGui) { |
| 160 | ALOGD("failed to load libgui: %s", dlerror()); |
| 161 | return; |
| 162 | } |
| 163 | mGetDriverNamespace = reinterpret_cast<decltype(mGetDriverNamespace)>( |
| 164 | dlsym(mLibGui, "android_getDriverNamespace")); |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 167 | Loader::~Loader() { |
Jesse Hall | 7a8d83e | 2016-12-20 15:24:28 -0800 | [diff] [blame] | 168 | if (mLibGui) |
| 169 | dlclose(mLibGui); |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Jesse Hall | c07b520 | 2013-07-04 12:08:16 -0700 | [diff] [blame] | 172 | static void* load_wrapper(const char* path) { |
Jesse Hall | 1508ae6 | 2017-01-19 17:43:26 -0800 | [diff] [blame] | 173 | void* so = do_dlopen(path, RTLD_NOW | RTLD_LOCAL); |
Jesse Hall | c07b520 | 2013-07-04 12:08:16 -0700 | [diff] [blame] | 174 | ALOGE_IF(!so, "dlopen(\"%s\") failed: %s", path, dlerror()); |
| 175 | return so; |
| 176 | } |
| 177 | |
Evgenii Stepanov | c2466e6 | 2015-07-08 15:49:52 -0700 | [diff] [blame] | 178 | #ifndef EGL_WRAPPER_DIR |
| 179 | #if defined(__LP64__) |
| 180 | #define EGL_WRAPPER_DIR "/system/lib64" |
| 181 | #else |
| 182 | #define EGL_WRAPPER_DIR "/system/lib" |
| 183 | #endif |
| 184 | #endif |
| 185 | |
bohu | 69e5b1a | 2016-02-19 17:15:20 -0800 | [diff] [blame] | 186 | static void setEmulatorGlesValue(void) { |
| 187 | char prop[PROPERTY_VALUE_MAX]; |
| 188 | property_get("ro.kernel.qemu", prop, "0"); |
| 189 | if (atoi(prop) != 1) return; |
| 190 | |
| 191 | property_get("ro.kernel.qemu.gles",prop,"0"); |
| 192 | if (atoi(prop) == 1) { |
| 193 | ALOGD("Emulator has host GPU support, qemu.gles is set to 1."); |
| 194 | property_set("qemu.gles", "1"); |
| 195 | return; |
| 196 | } |
| 197 | |
| 198 | // for now, checking the following |
| 199 | // directory is good enough for emulator system images |
| 200 | const char* vendor_lib_path = |
| 201 | #if defined(__LP64__) |
| 202 | "/vendor/lib64/egl"; |
| 203 | #else |
| 204 | "/vendor/lib/egl"; |
| 205 | #endif |
| 206 | |
| 207 | const bool has_vendor_lib = (access(vendor_lib_path, R_OK) == 0); |
| 208 | if (has_vendor_lib) { |
| 209 | ALOGD("Emulator has vendor provided software renderer, qemu.gles is set to 2."); |
| 210 | property_set("qemu.gles", "2"); |
| 211 | } else { |
| 212 | ALOGD("Emulator without GPU support detected. " |
| 213 | "Fallback to legacy software renderer, qemu.gles is set to 0."); |
| 214 | property_set("qemu.gles", "0"); |
| 215 | } |
| 216 | } |
| 217 | |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 218 | void* Loader::open(egl_connection_t* cnx) |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 219 | { |
Jesse Hall | 1508ae6 | 2017-01-19 17:43:26 -0800 | [diff] [blame] | 220 | ATRACE_CALL(); |
| 221 | |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 222 | void* dso; |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 223 | driver_t* hnd = 0; |
Jesse Hall | 94cdba9 | 2013-07-11 09:40:35 -0700 | [diff] [blame] | 224 | |
bohu | 69e5b1a | 2016-02-19 17:15:20 -0800 | [diff] [blame] | 225 | setEmulatorGlesValue(); |
| 226 | |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 227 | dso = load_driver("GLES", cnx, EGL | GLESv1_CM | GLESv2); |
| 228 | if (dso) { |
| 229 | hnd = new driver_t(dso); |
| 230 | } else { |
| 231 | // Always load EGL first |
| 232 | dso = load_driver("EGL", cnx, EGL); |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 233 | if (dso) { |
| 234 | hnd = new driver_t(dso); |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 235 | hnd->set( load_driver("GLESv1_CM", cnx, GLESv1_CM), GLESv1_CM ); |
| 236 | hnd->set( load_driver("GLESv2", cnx, GLESv2), GLESv2 ); |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 240 | LOG_ALWAYS_FATAL_IF(!hnd, "couldn't find an OpenGL ES implementation"); |
Jesse Hall | c07b520 | 2013-07-04 12:08:16 -0700 | [diff] [blame] | 241 | |
Evgenii Stepanov | c2466e6 | 2015-07-08 15:49:52 -0700 | [diff] [blame] | 242 | cnx->libEgl = load_wrapper(EGL_WRAPPER_DIR "/libEGL.so"); |
| 243 | cnx->libGles2 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv2.so"); |
| 244 | cnx->libGles1 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv1_CM.so"); |
| 245 | |
Michael Chock | c0ec5e2 | 2014-01-27 08:14:33 -0800 | [diff] [blame] | 246 | LOG_ALWAYS_FATAL_IF(!cnx->libEgl, |
| 247 | "couldn't load system EGL wrapper libraries"); |
| 248 | |
Jesse Hall | c07b520 | 2013-07-04 12:08:16 -0700 | [diff] [blame] | 249 | LOG_ALWAYS_FATAL_IF(!cnx->libGles2 || !cnx->libGles1, |
| 250 | "couldn't load system OpenGL ES wrapper libraries"); |
| 251 | |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 252 | return (void*)hnd; |
| 253 | } |
| 254 | |
| 255 | status_t Loader::close(void* driver) |
| 256 | { |
| 257 | driver_t* hnd = (driver_t*)driver; |
| 258 | delete hnd; |
| 259 | return NO_ERROR; |
| 260 | } |
| 261 | |
Jesse Hall | 94cdba9 | 2013-07-11 09:40:35 -0700 | [diff] [blame] | 262 | void Loader::init_api(void* dso, |
| 263 | char const * const * api, |
| 264 | __eglMustCastToProperFunctionPointerType* curr, |
| 265 | getProcAddressType getProcAddress) |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 266 | { |
Jesse Hall | 1508ae6 | 2017-01-19 17:43:26 -0800 | [diff] [blame] | 267 | ATRACE_CALL(); |
| 268 | |
Mathias Agopian | 7773c43 | 2012-02-13 20:06:08 -0800 | [diff] [blame] | 269 | const ssize_t SIZE = 256; |
Mathias Agopian | 0ad71a9 | 2011-05-11 20:37:47 -0700 | [diff] [blame] | 270 | char scrap[SIZE]; |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 271 | while (*api) { |
| 272 | char const * name = *api; |
Jesse Hall | 94cdba9 | 2013-07-11 09:40:35 -0700 | [diff] [blame] | 273 | __eglMustCastToProperFunctionPointerType f = |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 274 | (__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 Agopian | 0ad71a9 | 2011-05-11 20:37:47 -0700 | [diff] [blame] | 282 | if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) { |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 283 | strncpy(scrap, name, index); |
| 284 | scrap[index] = 0; |
| 285 | f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap); |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 286 | //ALOGD_IF(f, "found <%s> instead", scrap); |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | if (f == NULL) { |
| 290 | // Try with the OES postfix |
Mathias Agopian | 0ad71a9 | 2011-05-11 20:37:47 -0700 | [diff] [blame] | 291 | ssize_t index = ssize_t(strlen(name)) - 3; |
| 292 | if (index>0 && strcmp(name+index, "OES")) { |
| 293 | snprintf(scrap, SIZE, "%sOES", name); |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 294 | f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap); |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 295 | //ALOGD_IF(f, "found <%s> instead", scrap); |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | if (f == NULL) { |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 299 | //ALOGD("%s", name); |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 300 | f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented; |
Mathias Agopian | 48d438d | 2012-01-28 21:44:00 -0800 | [diff] [blame] | 301 | |
| 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 Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 314 | } |
| 315 | *curr++ = f; |
| 316 | api++; |
| 317 | } |
| 318 | } |
| 319 | |
Jesse Hall | 7a8d83e | 2016-12-20 15:24:28 -0800 | [diff] [blame] | 320 | static void* load_system_driver(const char* kind) { |
Jesse Hall | 1508ae6 | 2017-01-19 17:43:26 -0800 | [diff] [blame] | 321 | ATRACE_CALL(); |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 322 | class MatchFile { |
| 323 | public: |
| 324 | static String8 find(const char* kind) { |
| 325 | String8 result; |
Nicolas Capens | 776951f | 2015-11-06 10:10:21 -0500 | [diff] [blame] | 326 | int emulationStatus = checkGlesEmulationStatus(); |
| 327 | switch (emulationStatus) { |
| 328 | case 0: |
Nicolas Capens | 776951f | 2015-11-06 10:10:21 -0500 | [diff] [blame] | 329 | #if defined(__LP64__) |
| 330 | result.setTo("/system/lib64/egl/libGLES_android.so"); |
| 331 | #else |
| 332 | result.setTo("/system/lib/egl/libGLES_android.so"); |
| 333 | #endif |
| 334 | return result; |
| 335 | case 1: |
| 336 | // Use host-side OpenGL through the "emulation" library |
| 337 | #if defined(__LP64__) |
| 338 | result.appendFormat("/system/lib64/egl/lib%s_emulation.so", kind); |
| 339 | #else |
| 340 | result.appendFormat("/system/lib/egl/lib%s_emulation.so", kind); |
| 341 | #endif |
| 342 | return result; |
| 343 | default: |
| 344 | // Not in emulator, or use other guest-side implementation |
| 345 | break; |
| 346 | } |
| 347 | |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 348 | String8 pattern; |
| 349 | pattern.appendFormat("lib%s", kind); |
| 350 | const char* const searchPaths[] = { |
Dan Willemsen | 8edb8f5 | 2014-02-16 10:23:54 -0800 | [diff] [blame] | 351 | #if defined(__LP64__) |
| 352 | "/vendor/lib64/egl", |
| 353 | "/system/lib64/egl" |
| 354 | #else |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 355 | "/vendor/lib/egl", |
| 356 | "/system/lib/egl" |
Dan Willemsen | 8edb8f5 | 2014-02-16 10:23:54 -0800 | [diff] [blame] | 357 | #endif |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 358 | }; |
Brian Swetland | 2b9e4f6 | 2010-09-20 12:58:15 -0700 | [diff] [blame] | 359 | |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 360 | // first, we search for the exact name of the GLES userspace |
| 361 | // driver in both locations. |
| 362 | // i.e.: |
| 363 | // libGLES.so, or: |
| 364 | // libEGL.so, libGLESv1_CM.so, libGLESv2.so |
| 365 | |
| 366 | for (size_t i=0 ; i<NELEM(searchPaths) ; i++) { |
| 367 | if (find(result, pattern, searchPaths[i], true)) { |
| 368 | return result; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | // for compatibility with the old "egl.cfg" naming convention |
| 373 | // we look for files that match: |
| 374 | // libGLES_*.so, or: |
| 375 | // libEGL_*.so, libGLESv1_CM_*.so, libGLESv2_*.so |
| 376 | |
| 377 | pattern.append("_"); |
| 378 | for (size_t i=0 ; i<NELEM(searchPaths) ; i++) { |
| 379 | if (find(result, pattern, searchPaths[i], false)) { |
| 380 | return result; |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | // we didn't find the driver. gah. |
| 385 | result.clear(); |
| 386 | return result; |
Brian Swetland | 2b9e4f6 | 2010-09-20 12:58:15 -0700 | [diff] [blame] | 387 | } |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 388 | |
| 389 | private: |
| 390 | static bool find(String8& result, |
| 391 | const String8& pattern, const char* const search, bool exact) { |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 392 | if (exact) { |
| 393 | String8 absolutePath; |
| 394 | absolutePath.appendFormat("%s/%s.so", search, pattern.string()); |
| 395 | if (!access(absolutePath.string(), R_OK)) { |
| 396 | result = absolutePath; |
| 397 | return true; |
| 398 | } |
| 399 | return false; |
| 400 | } |
| 401 | |
| 402 | DIR* d = opendir(search); |
| 403 | if (d != NULL) { |
| 404 | struct dirent cur; |
| 405 | struct dirent* e; |
| 406 | while (readdir_r(d, &cur, &e) == 0 && e) { |
| 407 | if (e->d_type == DT_DIR) { |
| 408 | continue; |
| 409 | } |
| 410 | if (!strcmp(e->d_name, "libGLES_android.so")) { |
| 411 | // always skip the software renderer |
| 412 | continue; |
| 413 | } |
| 414 | if (strstr(e->d_name, pattern.string()) == e->d_name) { |
| 415 | if (!strcmp(e->d_name + strlen(e->d_name) - 3, ".so")) { |
| 416 | result.clear(); |
| 417 | result.appendFormat("%s/%s", search, e->d_name); |
| 418 | closedir(d); |
| 419 | return true; |
| 420 | } |
| 421 | } |
| 422 | } |
| 423 | closedir(d); |
| 424 | } |
| 425 | return false; |
| 426 | } |
| 427 | }; |
| 428 | |
| 429 | |
| 430 | String8 absolutePath = MatchFile::find(kind); |
| 431 | if (absolutePath.isEmpty()) { |
| 432 | // this happens often, we don't want to log an error |
| 433 | return 0; |
Mathias Agopian | 8c17384 | 2009-09-20 16:01:02 -0700 | [diff] [blame] | 434 | } |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 435 | const char* const driver_absolute_path = absolutePath.string(); |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 436 | |
Jesse Hall | 1508ae6 | 2017-01-19 17:43:26 -0800 | [diff] [blame] | 437 | void* dso = do_dlopen(driver_absolute_path, RTLD_NOW | RTLD_LOCAL); |
Mathias Agopian | 8c17384 | 2009-09-20 16:01:02 -0700 | [diff] [blame] | 438 | if (dso == 0) { |
| 439 | const char* err = dlerror(); |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 440 | ALOGE("load_driver(%s): %s", driver_absolute_path, err?err:"unknown"); |
Mathias Agopian | 8c17384 | 2009-09-20 16:01:02 -0700 | [diff] [blame] | 441 | return 0; |
| 442 | } |
| 443 | |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 444 | ALOGD("loaded %s", driver_absolute_path); |
Mathias Agopian | baca89c | 2009-08-20 19:09:34 -0700 | [diff] [blame] | 445 | |
Jesse Hall | 7a8d83e | 2016-12-20 15:24:28 -0800 | [diff] [blame] | 446 | return dso; |
| 447 | } |
| 448 | |
Jesse Hall | 1508ae6 | 2017-01-19 17:43:26 -0800 | [diff] [blame] | 449 | static void* do_android_dlopen_ext(const char* path, int mode, const android_dlextinfo* info) { |
| 450 | ATRACE_CALL(); |
| 451 | return android_dlopen_ext(path, mode, info); |
| 452 | } |
| 453 | |
Jesse Hall | 7a8d83e | 2016-12-20 15:24:28 -0800 | [diff] [blame] | 454 | static const std::array<const char*, 2> HAL_SUBNAME_KEY_PROPERTIES = {{ |
| 455 | "ro.hardware.egl", |
| 456 | "ro.board.platform", |
| 457 | }}; |
| 458 | |
| 459 | static void* load_updated_driver(const char* kind, android_namespace_t* ns) { |
Jesse Hall | 1508ae6 | 2017-01-19 17:43:26 -0800 | [diff] [blame] | 460 | ATRACE_CALL(); |
Jesse Hall | 7a8d83e | 2016-12-20 15:24:28 -0800 | [diff] [blame] | 461 | const android_dlextinfo dlextinfo = { |
| 462 | .flags = ANDROID_DLEXT_USE_NAMESPACE, |
| 463 | .library_namespace = ns, |
| 464 | }; |
| 465 | void* so = nullptr; |
| 466 | char prop[PROPERTY_VALUE_MAX + 1]; |
| 467 | for (auto key : HAL_SUBNAME_KEY_PROPERTIES) { |
| 468 | if (property_get(key, prop, nullptr) > 0) { |
| 469 | String8 name; |
| 470 | name.appendFormat("lib%s_%s.so", kind, prop); |
Jesse Hall | 1508ae6 | 2017-01-19 17:43:26 -0800 | [diff] [blame] | 471 | so = do_android_dlopen_ext(name.string(), RTLD_LOCAL | RTLD_NOW, |
Jesse Hall | 7a8d83e | 2016-12-20 15:24:28 -0800 | [diff] [blame] | 472 | &dlextinfo); |
| 473 | if (so) |
| 474 | return so; |
| 475 | } |
| 476 | } |
| 477 | return nullptr; |
| 478 | } |
| 479 | |
| 480 | void *Loader::load_driver(const char* kind, |
| 481 | egl_connection_t* cnx, uint32_t mask) |
| 482 | { |
Jesse Hall | 1508ae6 | 2017-01-19 17:43:26 -0800 | [diff] [blame] | 483 | ATRACE_CALL(); |
| 484 | |
Jesse Hall | 7a8d83e | 2016-12-20 15:24:28 -0800 | [diff] [blame] | 485 | void* dso = nullptr; |
| 486 | if (mGetDriverNamespace) { |
| 487 | android_namespace_t* ns = mGetDriverNamespace(); |
| 488 | if (ns) { |
| 489 | dso = load_updated_driver(kind, ns); |
| 490 | } |
| 491 | } |
| 492 | if (!dso) { |
| 493 | dso = load_system_driver(kind); |
| 494 | if (!dso) |
| 495 | return NULL; |
| 496 | } |
| 497 | |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 498 | if (mask & EGL) { |
| 499 | getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress"); |
| 500 | |
Jesse Hall | 94cdba9 | 2013-07-11 09:40:35 -0700 | [diff] [blame] | 501 | ALOGE_IF(!getProcAddress, |
Jesse Hall | 7a8d83e | 2016-12-20 15:24:28 -0800 | [diff] [blame] | 502 | "can't find eglGetProcAddress() in EGL driver library"); |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 503 | |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 504 | egl_t* egl = &cnx->egl; |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 505 | __eglMustCastToProperFunctionPointerType* curr = |
| 506 | (__eglMustCastToProperFunctionPointerType*)egl; |
| 507 | char const * const * api = egl_names; |
| 508 | while (*api) { |
| 509 | char const * name = *api; |
Jesse Hall | 94cdba9 | 2013-07-11 09:40:35 -0700 | [diff] [blame] | 510 | __eglMustCastToProperFunctionPointerType f = |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 511 | (__eglMustCastToProperFunctionPointerType)dlsym(dso, name); |
| 512 | if (f == NULL) { |
| 513 | // couldn't find the entry-point, use eglGetProcAddress() |
| 514 | f = getProcAddress(name); |
| 515 | if (f == NULL) { |
| 516 | f = (__eglMustCastToProperFunctionPointerType)0; |
| 517 | } |
| 518 | } |
| 519 | *curr++ = f; |
| 520 | api++; |
| 521 | } |
| 522 | } |
Jesse Hall | 94cdba9 | 2013-07-11 09:40:35 -0700 | [diff] [blame] | 523 | |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 524 | if (mask & GLESv1_CM) { |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 525 | init_api(dso, gl_names, |
| 526 | (__eglMustCastToProperFunctionPointerType*) |
Mathias Agopian | 7773c43 | 2012-02-13 20:06:08 -0800 | [diff] [blame] | 527 | &cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl, |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 528 | getProcAddress); |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | if (mask & GLESv2) { |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 532 | init_api(dso, gl_names, |
| 533 | (__eglMustCastToProperFunctionPointerType*) |
Mathias Agopian | 7773c43 | 2012-02-13 20:06:08 -0800 | [diff] [blame] | 534 | &cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl, |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 535 | getProcAddress); |
| 536 | } |
Jesse Hall | 94cdba9 | 2013-07-11 09:40:35 -0700 | [diff] [blame] | 537 | |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 538 | return dso; |
| 539 | } |
| 540 | |
| 541 | // ---------------------------------------------------------------------------- |
| 542 | }; // namespace android |
| 543 | // ---------------------------------------------------------------------------- |