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