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 | |
Mathias Agopian | 311b479 | 2017-02-28 15:00:49 -0800 | [diff] [blame] | 20 | #include "Loader.h" |
| 21 | |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 22 | #include <string> |
| 23 | |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 24 | #include <dirent.h> |
Mark Salyzyn | a5e161b | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 25 | #include <dlfcn.h> |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 26 | |
Jesse Hall | 7a8d83e | 2016-12-20 15:24:28 -0800 | [diff] [blame] | 27 | #include <android/dlext.h> |
David 'Digit' Turner | 80b30c2 | 2011-08-26 17:38:47 +0200 | [diff] [blame] | 28 | #include <cutils/properties.h> |
Mark Salyzyn | 7823e12 | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 29 | #include <log/log.h> |
Mathias Agopian | 311b479 | 2017-02-28 15:00:49 -0800 | [diff] [blame] | 30 | |
Mathias Agopian | 991d254 | 2017-02-06 13:51:32 -0800 | [diff] [blame] | 31 | #include <ui/GraphicsEnv.h> |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 32 | |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 33 | #include "egl_trace.h" |
Mathias Agopian | 1cadb25 | 2011-05-23 17:26:14 -0700 | [diff] [blame] | 34 | #include "egldefs.h" |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 35 | |
Jiyong Park | 5910dc7 | 2017-04-05 14:23:52 +0900 | [diff] [blame] | 36 | extern "C" { |
| 37 | android_namespace_t* android_get_exported_namespace(const char*); |
| 38 | } |
| 39 | |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 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 | |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 68 | Loader& Loader::getInstance() { |
| 69 | static Loader loader; |
| 70 | return loader; |
| 71 | } |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 72 | |
David 'Digit' Turner | 80b30c2 | 2011-08-26 17:38:47 +0200 | [diff] [blame] | 73 | /* 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 Capens | 776951f | 2015-11-06 10:10:21 -0500 | [diff] [blame] | 80 | * 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' Turner | 80b30c2 | 2011-08-26 17:38:47 +0200 | [diff] [blame] | 83 | */ |
| 84 | static int |
| 85 | checkGlesEmulationStatus(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 */ |
bohu | 69e5b1a | 2016-02-19 17:15:20 -0800 | [diff] [blame] | 104 | property_get("qemu.gles",prop,"0"); |
David 'Digit' Turner | 80b30c2 | 2011-08-26 17:38:47 +0200 | [diff] [blame] | 105 | return atoi(prop); |
| 106 | } |
| 107 | |
Jesse Hall | 1508ae6 | 2017-01-19 17:43:26 -0800 | [diff] [blame] | 108 | static void* do_dlopen(const char* path, int mode) { |
| 109 | ATRACE_CALL(); |
| 110 | return dlopen(path, mode); |
| 111 | } |
| 112 | |
Jiyong Park | 5910dc7 | 2017-04-05 14:23:52 +0900 | [diff] [blame] | 113 | static 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 Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 118 | // ---------------------------------------------------------------------------- |
| 119 | |
Jesse Hall | 94cdba9 | 2013-07-11 09:40:35 -0700 | [diff] [blame] | 120 | Loader::driver_t::driver_t(void* gles) |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 121 | { |
| 122 | dso[0] = gles; |
| 123 | for (size_t i=1 ; i<NELEM(dso) ; i++) |
| 124 | dso[i] = 0; |
| 125 | } |
| 126 | |
Jesse Hall | 94cdba9 | 2013-07-11 09:40:35 -0700 | [diff] [blame] | 127 | Loader::driver_t::~driver_t() |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 128 | { |
| 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 Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 137 | int Loader::driver_t::set(void* hnd, int32_t api) |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 138 | { |
| 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 Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 150 | return -EOVERFLOW; |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 151 | } |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 152 | return 0; |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | // ---------------------------------------------------------------------------- |
| 156 | |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 157 | Loader::Loader() |
Mathias Agopian | 991d254 | 2017-02-06 13:51:32 -0800 | [diff] [blame] | 158 | : getProcAddress(NULL) |
Jesse Hall | 7a8d83e | 2016-12-20 15:24:28 -0800 | [diff] [blame] | 159 | { |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 162 | Loader::~Loader() { |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Jesse Hall | c07b520 | 2013-07-04 12:08:16 -0700 | [diff] [blame] | 165 | static void* load_wrapper(const char* path) { |
Jesse Hall | 1508ae6 | 2017-01-19 17:43:26 -0800 | [diff] [blame] | 166 | void* so = do_dlopen(path, RTLD_NOW | RTLD_LOCAL); |
Jesse Hall | c07b520 | 2013-07-04 12:08:16 -0700 | [diff] [blame] | 167 | ALOGE_IF(!so, "dlopen(\"%s\") failed: %s", path, dlerror()); |
| 168 | return so; |
| 169 | } |
| 170 | |
Evgenii Stepanov | c2466e6 | 2015-07-08 15:49:52 -0700 | [diff] [blame] | 171 | #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 | |
bohu | 69e5b1a | 2016-02-19 17:15:20 -0800 | [diff] [blame] | 179 | static 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 Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 211 | void* Loader::open(egl_connection_t* cnx) |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 212 | { |
Jesse Hall | 1508ae6 | 2017-01-19 17:43:26 -0800 | [diff] [blame] | 213 | ATRACE_CALL(); |
| 214 | |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 215 | void* dso; |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 216 | driver_t* hnd = 0; |
Jesse Hall | 94cdba9 | 2013-07-11 09:40:35 -0700 | [diff] [blame] | 217 | |
bohu | 69e5b1a | 2016-02-19 17:15:20 -0800 | [diff] [blame] | 218 | setEmulatorGlesValue(); |
| 219 | |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 220 | 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 Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 226 | if (dso) { |
| 227 | hnd = new driver_t(dso); |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 228 | hnd->set( load_driver("GLESv1_CM", cnx, GLESv1_CM), GLESv1_CM ); |
| 229 | hnd->set( load_driver("GLESv2", cnx, GLESv2), GLESv2 ); |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 233 | LOG_ALWAYS_FATAL_IF(!hnd, "couldn't find an OpenGL ES implementation"); |
Jesse Hall | c07b520 | 2013-07-04 12:08:16 -0700 | [diff] [blame] | 234 | |
Evgenii Stepanov | c2466e6 | 2015-07-08 15:49:52 -0700 | [diff] [blame] | 235 | 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 Chock | c0ec5e2 | 2014-01-27 08:14:33 -0800 | [diff] [blame] | 239 | LOG_ALWAYS_FATAL_IF(!cnx->libEgl, |
| 240 | "couldn't load system EGL wrapper libraries"); |
| 241 | |
Jesse Hall | c07b520 | 2013-07-04 12:08:16 -0700 | [diff] [blame] | 242 | LOG_ALWAYS_FATAL_IF(!cnx->libGles2 || !cnx->libGles1, |
| 243 | "couldn't load system OpenGL ES wrapper libraries"); |
| 244 | |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 245 | return (void*)hnd; |
| 246 | } |
| 247 | |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 248 | void Loader::close(void* driver) |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 249 | { |
| 250 | driver_t* hnd = (driver_t*)driver; |
| 251 | delete hnd; |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Jesse Hall | 94cdba9 | 2013-07-11 09:40:35 -0700 | [diff] [blame] | 254 | void Loader::init_api(void* dso, |
| 255 | char const * const * api, |
| 256 | __eglMustCastToProperFunctionPointerType* curr, |
| 257 | getProcAddressType getProcAddress) |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 258 | { |
Jesse Hall | 1508ae6 | 2017-01-19 17:43:26 -0800 | [diff] [blame] | 259 | ATRACE_CALL(); |
| 260 | |
Mathias Agopian | 7773c43 | 2012-02-13 20:06:08 -0800 | [diff] [blame] | 261 | const ssize_t SIZE = 256; |
Mathias Agopian | 0ad71a9 | 2011-05-11 20:37:47 -0700 | [diff] [blame] | 262 | char scrap[SIZE]; |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 263 | while (*api) { |
| 264 | char const * name = *api; |
Jesse Hall | 94cdba9 | 2013-07-11 09:40:35 -0700 | [diff] [blame] | 265 | __eglMustCastToProperFunctionPointerType f = |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 266 | (__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 Agopian | 0ad71a9 | 2011-05-11 20:37:47 -0700 | [diff] [blame] | 274 | if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) { |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 275 | strncpy(scrap, name, index); |
| 276 | scrap[index] = 0; |
| 277 | f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap); |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 278 | //ALOGD_IF(f, "found <%s> instead", scrap); |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | if (f == NULL) { |
| 282 | // Try with the OES postfix |
Mathias Agopian | 0ad71a9 | 2011-05-11 20:37:47 -0700 | [diff] [blame] | 283 | ssize_t index = ssize_t(strlen(name)) - 3; |
| 284 | if (index>0 && strcmp(name+index, "OES")) { |
| 285 | snprintf(scrap, SIZE, "%sOES", name); |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 286 | f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap); |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 287 | //ALOGD_IF(f, "found <%s> instead", scrap); |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | if (f == NULL) { |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 291 | //ALOGD("%s", name); |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 292 | f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented; |
Mathias Agopian | 48d438d | 2012-01-28 21:44:00 -0800 | [diff] [blame] | 293 | |
| 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 Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 306 | } |
| 307 | *curr++ = f; |
| 308 | api++; |
| 309 | } |
| 310 | } |
| 311 | |
Jesse Hall | 7a8d83e | 2016-12-20 15:24:28 -0800 | [diff] [blame] | 312 | static void* load_system_driver(const char* kind) { |
Jesse Hall | 1508ae6 | 2017-01-19 17:43:26 -0800 | [diff] [blame] | 313 | ATRACE_CALL(); |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 314 | class MatchFile { |
| 315 | public: |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 316 | static std::string find(const char* kind) { |
| 317 | std::string result; |
Nicolas Capens | 776951f | 2015-11-06 10:10:21 -0500 | [diff] [blame] | 318 | int emulationStatus = checkGlesEmulationStatus(); |
| 319 | switch (emulationStatus) { |
| 320 | case 0: |
Nicolas Capens | 776951f | 2015-11-06 10:10:21 -0500 | [diff] [blame] | 321 | #if defined(__LP64__) |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 322 | result = "/system/lib64/egl/libGLES_android.so"; |
Nicolas Capens | 776951f | 2015-11-06 10:10:21 -0500 | [diff] [blame] | 323 | #else |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 324 | result = "/system/lib/egl/libGLES_android.so"; |
Nicolas Capens | 776951f | 2015-11-06 10:10:21 -0500 | [diff] [blame] | 325 | #endif |
| 326 | return result; |
| 327 | case 1: |
| 328 | // Use host-side OpenGL through the "emulation" library |
| 329 | #if defined(__LP64__) |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 330 | result = std::string("/system/lib64/egl/lib") + kind + "_emulation.so"; |
Nicolas Capens | 776951f | 2015-11-06 10:10:21 -0500 | [diff] [blame] | 331 | #else |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 332 | result = std::string("/system/lib/egl/lib") + kind + "_emulation.so"; |
Nicolas Capens | 776951f | 2015-11-06 10:10:21 -0500 | [diff] [blame] | 333 | #endif |
| 334 | return result; |
| 335 | default: |
| 336 | // Not in emulator, or use other guest-side implementation |
| 337 | break; |
| 338 | } |
| 339 | |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 340 | std::string pattern = std::string("lib") + kind; |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 341 | const char* const searchPaths[] = { |
Dan Willemsen | 8edb8f5 | 2014-02-16 10:23:54 -0800 | [diff] [blame] | 342 | #if defined(__LP64__) |
| 343 | "/vendor/lib64/egl", |
| 344 | "/system/lib64/egl" |
| 345 | #else |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 346 | "/vendor/lib/egl", |
| 347 | "/system/lib/egl" |
Dan Willemsen | 8edb8f5 | 2014-02-16 10:23:54 -0800 | [diff] [blame] | 348 | #endif |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 349 | }; |
Brian Swetland | 2b9e4f6 | 2010-09-20 12:58:15 -0700 | [diff] [blame] | 350 | |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 351 | // 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 Swetland | 2b9e4f6 | 2010-09-20 12:58:15 -0700 | [diff] [blame] | 378 | } |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 379 | |
| 380 | private: |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 381 | static bool find(std::string& result, |
| 382 | const std::string& pattern, const char* const search, bool exact) { |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 383 | if (exact) { |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 384 | std::string absolutePath = std::string(search) + "/" + pattern; |
| 385 | if (!access(absolutePath.c_str(), R_OK)) { |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 386 | 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 Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 404 | if (strstr(e->d_name, pattern.c_str()) == e->d_name) { |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 405 | if (!strcmp(e->d_name + strlen(e->d_name) - 3, ".so")) { |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 406 | result = std::string(search) + "/" + e->d_name; |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 407 | closedir(d); |
| 408 | return true; |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | closedir(d); |
| 413 | } |
| 414 | return false; |
| 415 | } |
| 416 | }; |
| 417 | |
| 418 | |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 419 | std::string absolutePath = MatchFile::find(kind); |
| 420 | if (absolutePath.empty()) { |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 421 | // this happens often, we don't want to log an error |
| 422 | return 0; |
Mathias Agopian | 8c17384 | 2009-09-20 16:01:02 -0700 | [diff] [blame] | 423 | } |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 424 | const char* const driver_absolute_path = absolutePath.c_str(); |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 425 | |
Jiyong Park | 5910dc7 | 2017-04-05 14:23:52 +0900 | [diff] [blame] | 426 | // 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 Hall | 1508ae6 | 2017-01-19 17:43:26 -0800 | [diff] [blame] | 447 | void* dso = do_dlopen(driver_absolute_path, RTLD_NOW | RTLD_LOCAL); |
Mathias Agopian | 8c17384 | 2009-09-20 16:01:02 -0700 | [diff] [blame] | 448 | if (dso == 0) { |
| 449 | const char* err = dlerror(); |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 450 | ALOGE("load_driver(%s): %s", driver_absolute_path, err ? err : "unknown"); |
Mathias Agopian | 8c17384 | 2009-09-20 16:01:02 -0700 | [diff] [blame] | 451 | return 0; |
| 452 | } |
| 453 | |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 454 | ALOGD("loaded %s", driver_absolute_path); |
Mathias Agopian | baca89c | 2009-08-20 19:09:34 -0700 | [diff] [blame] | 455 | |
Jesse Hall | 7a8d83e | 2016-12-20 15:24:28 -0800 | [diff] [blame] | 456 | return dso; |
| 457 | } |
| 458 | |
Mathias Agopian | 311b479 | 2017-02-28 15:00:49 -0800 | [diff] [blame] | 459 | static const char* HAL_SUBNAME_KEY_PROPERTIES[2] = { |
Jesse Hall | 7a8d83e | 2016-12-20 15:24:28 -0800 | [diff] [blame] | 460 | "ro.hardware.egl", |
| 461 | "ro.board.platform", |
Mathias Agopian | 311b479 | 2017-02-28 15:00:49 -0800 | [diff] [blame] | 462 | }; |
Jesse Hall | 7a8d83e | 2016-12-20 15:24:28 -0800 | [diff] [blame] | 463 | |
| 464 | static void* load_updated_driver(const char* kind, android_namespace_t* ns) { |
Jesse Hall | 1508ae6 | 2017-01-19 17:43:26 -0800 | [diff] [blame] | 465 | ATRACE_CALL(); |
Jesse Hall | 7a8d83e | 2016-12-20 15:24:28 -0800 | [diff] [blame] | 466 | 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 Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 474 | std::string name = std::string("lib") + kind + "_" + prop + ".so"; |
| 475 | so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo); |
Mathias Agopian | 311b479 | 2017-02-28 15:00:49 -0800 | [diff] [blame] | 476 | if (so) { |
Jesse Hall | 7a8d83e | 2016-12-20 15:24:28 -0800 | [diff] [blame] | 477 | return so; |
Mathias Agopian | 311b479 | 2017-02-28 15:00:49 -0800 | [diff] [blame] | 478 | } |
Jesse Hall | 7a8d83e | 2016-12-20 15:24:28 -0800 | [diff] [blame] | 479 | } |
| 480 | } |
| 481 | return nullptr; |
| 482 | } |
| 483 | |
| 484 | void *Loader::load_driver(const char* kind, |
| 485 | egl_connection_t* cnx, uint32_t mask) |
| 486 | { |
Jesse Hall | 1508ae6 | 2017-01-19 17:43:26 -0800 | [diff] [blame] | 487 | ATRACE_CALL(); |
| 488 | |
Jesse Hall | 7a8d83e | 2016-12-20 15:24:28 -0800 | [diff] [blame] | 489 | void* dso = nullptr; |
Mathias Agopian | 991d254 | 2017-02-06 13:51:32 -0800 | [diff] [blame] | 490 | android_namespace_t* ns = android_getDriverNamespace(); |
| 491 | if (ns) { |
| 492 | dso = load_updated_driver(kind, ns); |
Jesse Hall | 7a8d83e | 2016-12-20 15:24:28 -0800 | [diff] [blame] | 493 | } |
| 494 | if (!dso) { |
| 495 | dso = load_system_driver(kind); |
| 496 | if (!dso) |
| 497 | return NULL; |
| 498 | } |
| 499 | |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 500 | if (mask & EGL) { |
| 501 | getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress"); |
| 502 | |
Jesse Hall | 94cdba9 | 2013-07-11 09:40:35 -0700 | [diff] [blame] | 503 | ALOGE_IF(!getProcAddress, |
Jesse Hall | 7a8d83e | 2016-12-20 15:24:28 -0800 | [diff] [blame] | 504 | "can't find eglGetProcAddress() in EGL driver library"); |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 505 | |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 506 | egl_t* egl = &cnx->egl; |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 507 | __eglMustCastToProperFunctionPointerType* curr = |
| 508 | (__eglMustCastToProperFunctionPointerType*)egl; |
| 509 | char const * const * api = egl_names; |
| 510 | while (*api) { |
| 511 | char const * name = *api; |
Jesse Hall | 94cdba9 | 2013-07-11 09:40:35 -0700 | [diff] [blame] | 512 | __eglMustCastToProperFunctionPointerType f = |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 513 | (__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 Hall | 94cdba9 | 2013-07-11 09:40:35 -0700 | [diff] [blame] | 525 | |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 526 | if (mask & GLESv1_CM) { |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 527 | init_api(dso, gl_names, |
| 528 | (__eglMustCastToProperFunctionPointerType*) |
Mathias Agopian | 7773c43 | 2012-02-13 20:06:08 -0800 | [diff] [blame] | 529 | &cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl, |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 530 | getProcAddress); |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | if (mask & GLESv2) { |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 534 | init_api(dso, gl_names, |
| 535 | (__eglMustCastToProperFunctionPointerType*) |
Mathias Agopian | 7773c43 | 2012-02-13 20:06:08 -0800 | [diff] [blame] | 536 | &cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl, |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 537 | getProcAddress); |
| 538 | } |
Jesse Hall | 94cdba9 | 2013-07-11 09:40:35 -0700 | [diff] [blame] | 539 | |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 540 | return dso; |
| 541 | } |
| 542 | |
| 543 | // ---------------------------------------------------------------------------- |
| 544 | }; // namespace android |
| 545 | // ---------------------------------------------------------------------------- |