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 | |
Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 20 | #include <EGL/Loader.h> |
Mathias Agopian | 311b479 | 2017-02-28 15:00:49 -0800 | [diff] [blame] | 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> |
Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 30 | #include <utils/Timers.h> |
Mathias Agopian | 311b479 | 2017-02-28 15:00:49 -0800 | [diff] [blame] | 31 | |
Jiyong Park | a243e5d | 2017-06-21 12:26:51 +0900 | [diff] [blame] | 32 | #ifndef __ANDROID_VNDK__ |
Jiyong Park | 27c39e1 | 2017-05-08 13:00:02 +0900 | [diff] [blame] | 33 | #include <graphicsenv/GraphicsEnv.h> |
Jiyong Park | a243e5d | 2017-06-21 12:26:51 +0900 | [diff] [blame] | 34 | #endif |
Justin Yun | b732030 | 2017-05-22 15:13:40 +0900 | [diff] [blame] | 35 | #include <vndksupport/linker.h> |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 36 | |
Cody Northrop | 68d1035 | 2018-10-15 07:22:09 -0600 | [diff] [blame] | 37 | #include "egl_platform_entries.h" |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 38 | #include "egl_trace.h" |
Mathias Agopian | 1cadb25 | 2011-05-23 17:26:14 -0700 | [diff] [blame] | 39 | #include "egldefs.h" |
Courtney Goeltzenleuchter | b91b989 | 2018-10-23 16:28:00 -0600 | [diff] [blame] | 40 | #include <EGL/eglext_angle.h> |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 41 | |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 42 | namespace android { |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 43 | |
| 44 | /* |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 45 | * EGL userspace drivers must be provided either: |
| 46 | * - as a single library: |
| 47 | * /vendor/lib/egl/libGLES.so |
| 48 | * |
| 49 | * - as separate libraries: |
| 50 | * /vendor/lib/egl/libEGL.so |
| 51 | * /vendor/lib/egl/libGLESv1_CM.so |
| 52 | * /vendor/lib/egl/libGLESv2.so |
| 53 | * |
| 54 | * The software renderer for the emulator must be provided as a single |
| 55 | * library at: |
| 56 | * |
| 57 | * /system/lib/egl/libGLES_android.so |
| 58 | * |
| 59 | * |
| 60 | * For backward compatibility and to facilitate the transition to |
| 61 | * this new naming scheme, the loader will additionally look for: |
Jesse Hall | 94cdba9 | 2013-07-11 09:40:35 -0700 | [diff] [blame] | 62 | * |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 63 | * /{vendor|system}/lib/egl/lib{GLES | [EGL|GLESv1_CM|GLESv2]}_*.so |
Jesse Hall | 94cdba9 | 2013-07-11 09:40:35 -0700 | [diff] [blame] | 64 | * |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 65 | */ |
| 66 | |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 67 | Loader& Loader::getInstance() { |
| 68 | static Loader loader; |
| 69 | return loader; |
| 70 | } |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 71 | |
Jesse Hall | 1508ae6 | 2017-01-19 17:43:26 -0800 | [diff] [blame] | 72 | static void* do_dlopen(const char* path, int mode) { |
| 73 | ATRACE_CALL(); |
| 74 | return dlopen(path, mode); |
| 75 | } |
| 76 | |
Jiyong Park | 5910dc7 | 2017-04-05 14:23:52 +0900 | [diff] [blame] | 77 | static void* do_android_dlopen_ext(const char* path, int mode, const android_dlextinfo* info) { |
| 78 | ATRACE_CALL(); |
| 79 | return android_dlopen_ext(path, mode, info); |
| 80 | } |
| 81 | |
Justin Yun | b732030 | 2017-05-22 15:13:40 +0900 | [diff] [blame] | 82 | static void* do_android_load_sphal_library(const char* path, int mode) { |
| 83 | ATRACE_CALL(); |
| 84 | return android_load_sphal_library(path, mode); |
| 85 | } |
| 86 | |
Yiwei Zhang | 5e21eb3 | 2019-06-05 00:26:03 -0700 | [diff] [blame^] | 87 | static int do_android_unload_sphal_library(void* dso) { |
| 88 | ATRACE_CALL(); |
| 89 | return android_unload_sphal_library(dso); |
| 90 | } |
| 91 | |
Jesse Hall | 94cdba9 | 2013-07-11 09:40:35 -0700 | [diff] [blame] | 92 | Loader::driver_t::driver_t(void* gles) |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 93 | { |
| 94 | dso[0] = gles; |
| 95 | for (size_t i=1 ; i<NELEM(dso) ; i++) |
Yi Kong | 48a6cd2 | 2018-07-18 10:07:09 -0700 | [diff] [blame] | 96 | dso[i] = nullptr; |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Jesse Hall | 94cdba9 | 2013-07-11 09:40:35 -0700 | [diff] [blame] | 99 | Loader::driver_t::~driver_t() |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 100 | { |
| 101 | for (size_t i=0 ; i<NELEM(dso) ; i++) { |
| 102 | if (dso[i]) { |
| 103 | dlclose(dso[i]); |
Yi Kong | 48a6cd2 | 2018-07-18 10:07:09 -0700 | [diff] [blame] | 104 | dso[i] = nullptr; |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 109 | int Loader::driver_t::set(void* hnd, int32_t api) |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 110 | { |
| 111 | switch (api) { |
| 112 | case EGL: |
| 113 | dso[0] = hnd; |
| 114 | break; |
| 115 | case GLESv1_CM: |
| 116 | dso[1] = hnd; |
| 117 | break; |
| 118 | case GLESv2: |
| 119 | dso[2] = hnd; |
| 120 | break; |
| 121 | default: |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 122 | return -EOVERFLOW; |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 123 | } |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 124 | return 0; |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 127 | Loader::Loader() |
Yi Kong | 48a6cd2 | 2018-07-18 10:07:09 -0700 | [diff] [blame] | 128 | : getProcAddress(nullptr) |
Jesse Hall | 7a8d83e | 2016-12-20 15:24:28 -0800 | [diff] [blame] | 129 | { |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 132 | Loader::~Loader() { |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Jesse Hall | c07b520 | 2013-07-04 12:08:16 -0700 | [diff] [blame] | 135 | static void* load_wrapper(const char* path) { |
Jesse Hall | 1508ae6 | 2017-01-19 17:43:26 -0800 | [diff] [blame] | 136 | void* so = do_dlopen(path, RTLD_NOW | RTLD_LOCAL); |
Jesse Hall | c07b520 | 2013-07-04 12:08:16 -0700 | [diff] [blame] | 137 | ALOGE_IF(!so, "dlopen(\"%s\") failed: %s", path, dlerror()); |
| 138 | return so; |
| 139 | } |
| 140 | |
Evgenii Stepanov | c2466e6 | 2015-07-08 15:49:52 -0700 | [diff] [blame] | 141 | #ifndef EGL_WRAPPER_DIR |
| 142 | #if defined(__LP64__) |
| 143 | #define EGL_WRAPPER_DIR "/system/lib64" |
| 144 | #else |
| 145 | #define EGL_WRAPPER_DIR "/system/lib" |
| 146 | #endif |
| 147 | #endif |
| 148 | |
bohu | 69e5b1a | 2016-02-19 17:15:20 -0800 | [diff] [blame] | 149 | static void setEmulatorGlesValue(void) { |
| 150 | char prop[PROPERTY_VALUE_MAX]; |
| 151 | property_get("ro.kernel.qemu", prop, "0"); |
| 152 | if (atoi(prop) != 1) return; |
| 153 | |
| 154 | property_get("ro.kernel.qemu.gles",prop,"0"); |
| 155 | if (atoi(prop) == 1) { |
| 156 | ALOGD("Emulator has host GPU support, qemu.gles is set to 1."); |
| 157 | property_set("qemu.gles", "1"); |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | // for now, checking the following |
| 162 | // directory is good enough for emulator system images |
| 163 | const char* vendor_lib_path = |
| 164 | #if defined(__LP64__) |
| 165 | "/vendor/lib64/egl"; |
| 166 | #else |
| 167 | "/vendor/lib/egl"; |
| 168 | #endif |
| 169 | |
| 170 | const bool has_vendor_lib = (access(vendor_lib_path, R_OK) == 0); |
| 171 | if (has_vendor_lib) { |
| 172 | ALOGD("Emulator has vendor provided software renderer, qemu.gles is set to 2."); |
| 173 | property_set("qemu.gles", "2"); |
| 174 | } else { |
| 175 | ALOGD("Emulator without GPU support detected. " |
| 176 | "Fallback to legacy software renderer, qemu.gles is set to 0."); |
| 177 | property_set("qemu.gles", "0"); |
| 178 | } |
| 179 | } |
| 180 | |
Peiyong Lin | 0d10b25 | 2019-04-30 18:03:04 -0700 | [diff] [blame] | 181 | static const char* DRIVER_SUFFIX_PROPERTY = "ro.hardware.egl"; |
| 182 | |
Peiyong Lin | 9f0c79d | 2019-04-22 10:14:57 -0700 | [diff] [blame] | 183 | static const char* HAL_SUBNAME_KEY_PROPERTIES[2] = { |
Peiyong Lin | 0d10b25 | 2019-04-30 18:03:04 -0700 | [diff] [blame] | 184 | DRIVER_SUFFIX_PROPERTY, |
Peiyong Lin | 9f0c79d | 2019-04-22 10:14:57 -0700 | [diff] [blame] | 185 | "ro.board.platform", |
| 186 | }; |
| 187 | |
Yiwei Zhang | 5e21eb3 | 2019-06-05 00:26:03 -0700 | [diff] [blame^] | 188 | static bool should_unload_system_driver(egl_connection_t* cnx) { |
| 189 | // Return false if the system driver has been unloaded once. |
| 190 | if (cnx->systemDriverUnloaded) { |
| 191 | return false; |
| 192 | } |
| 193 | |
| 194 | // Return true if Angle namespace is set. |
| 195 | android_namespace_t* ns = android::GraphicsEnv::getInstance().getAngleNamespace(); |
| 196 | if (ns) { |
| 197 | return true; |
| 198 | } |
| 199 | |
| 200 | #ifndef __ANDROID_VNDK__ |
| 201 | // Return true if updated driver namespace is set. |
| 202 | ns = android::GraphicsEnv::getInstance().getDriverNamespace(); |
| 203 | if (ns) { |
| 204 | return true; |
| 205 | } |
| 206 | #endif |
| 207 | |
| 208 | return false; |
| 209 | } |
| 210 | |
| 211 | static void uninit_api(char const* const* api, __eglMustCastToProperFunctionPointerType* curr) { |
| 212 | while (*api) { |
| 213 | *curr++ = nullptr; |
| 214 | api++; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | void Loader::unload_system_driver(egl_connection_t* cnx) { |
| 219 | ATRACE_CALL(); |
| 220 | |
| 221 | uninit_api(gl_names, |
| 222 | (__eglMustCastToProperFunctionPointerType*)&cnx |
| 223 | ->hooks[egl_connection_t::GLESv2_INDEX] |
| 224 | ->gl); |
| 225 | uninit_api(gl_names, |
| 226 | (__eglMustCastToProperFunctionPointerType*)&cnx |
| 227 | ->hooks[egl_connection_t::GLESv1_INDEX] |
| 228 | ->gl); |
| 229 | uninit_api(egl_names, (__eglMustCastToProperFunctionPointerType*)&cnx->egl); |
| 230 | |
| 231 | if (cnx->dso) { |
| 232 | ALOGD("Unload system gl driver."); |
| 233 | driver_t* hnd = (driver_t*)cnx->dso; |
| 234 | if (hnd->dso[2]) { |
| 235 | do_android_unload_sphal_library(hnd->dso[2]); |
| 236 | } |
| 237 | if (hnd->dso[1]) { |
| 238 | do_android_unload_sphal_library(hnd->dso[1]); |
| 239 | } |
| 240 | if (hnd->dso[0]) { |
| 241 | do_android_unload_sphal_library(hnd->dso[0]); |
| 242 | } |
| 243 | cnx->dso = nullptr; |
| 244 | } |
| 245 | |
| 246 | cnx->systemDriverUnloaded = true; |
| 247 | } |
| 248 | |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 249 | void* Loader::open(egl_connection_t* cnx) |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 250 | { |
Jesse Hall | 1508ae6 | 2017-01-19 17:43:26 -0800 | [diff] [blame] | 251 | ATRACE_CALL(); |
Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 252 | const nsecs_t openTime = systemTime(); |
Jesse Hall | 1508ae6 | 2017-01-19 17:43:26 -0800 | [diff] [blame] | 253 | |
Yiwei Zhang | 5e21eb3 | 2019-06-05 00:26:03 -0700 | [diff] [blame^] | 254 | if (should_unload_system_driver(cnx)) { |
| 255 | unload_system_driver(cnx); |
| 256 | } |
| 257 | |
| 258 | // If a driver has been loaded, return the driver directly. |
| 259 | if (cnx->dso) { |
| 260 | return cnx->dso; |
| 261 | } |
| 262 | |
bohu | 69e5b1a | 2016-02-19 17:15:20 -0800 | [diff] [blame] | 263 | setEmulatorGlesValue(); |
| 264 | |
Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 265 | // Check if we should use ANGLE early, so loading each driver doesn't require repeated queries. |
| 266 | if (android::GraphicsEnv::getInstance().shouldUseAngle()) { |
| 267 | cnx->shouldUseAngle = true; |
| 268 | } else { |
| 269 | cnx->shouldUseAngle = false; |
| 270 | } |
| 271 | |
Peiyong Lin | 8cd204d | 2019-04-19 14:05:17 -0700 | [diff] [blame] | 272 | // Firstly, try to load ANGLE driver. |
| 273 | driver_t* hnd = attempt_to_load_angle(cnx); |
| 274 | if (!hnd) { |
| 275 | // Secondly, try to load from driver apk. |
| 276 | hnd = attempt_to_load_updated_driver(cnx); |
| 277 | } |
Peiyong Lin | 0d10b25 | 2019-04-30 18:03:04 -0700 | [diff] [blame] | 278 | |
| 279 | bool failToLoadFromDriverSuffixProperty = false; |
Peiyong Lin | 8cd204d | 2019-04-19 14:05:17 -0700 | [diff] [blame] | 280 | if (!hnd) { |
Peiyong Lin | 9f0c79d | 2019-04-22 10:14:57 -0700 | [diff] [blame] | 281 | // Finally, try to load system driver, start by searching for the library name appended by |
| 282 | // the system properties of the GLES userspace driver in both locations. |
| 283 | // i.e.: |
| 284 | // libGLES_${prop}.so, or: |
| 285 | // libEGL_${prop}.so, libGLESv1_CM_${prop}.so, libGLESv2_${prop}.so |
| 286 | char prop[PROPERTY_VALUE_MAX + 1]; |
| 287 | for (auto key : HAL_SUBNAME_KEY_PROPERTIES) { |
| 288 | if (property_get(key, prop, nullptr) <= 0) { |
| 289 | continue; |
| 290 | } |
Peiyong Lin | 0d10b25 | 2019-04-30 18:03:04 -0700 | [diff] [blame] | 291 | hnd = attempt_to_load_system_driver(cnx, prop, true); |
Peiyong Lin | 9f0c79d | 2019-04-22 10:14:57 -0700 | [diff] [blame] | 292 | if (hnd) { |
| 293 | break; |
Peiyong Lin | 0d10b25 | 2019-04-30 18:03:04 -0700 | [diff] [blame] | 294 | } else if (strcmp(key, DRIVER_SUFFIX_PROPERTY) == 0) { |
| 295 | failToLoadFromDriverSuffixProperty = true; |
Peiyong Lin | 9f0c79d | 2019-04-22 10:14:57 -0700 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | if (!hnd) { |
| 301 | // Can't find graphics driver by appending system properties, now search for the exact name |
| 302 | // without any suffix of the GLES userspace driver in both locations. |
| 303 | // i.e.: |
| 304 | // libGLES.so, or: |
| 305 | // libEGL.so, libGLESv1_CM.so, libGLESv2.so |
Peiyong Lin | 0d10b25 | 2019-04-30 18:03:04 -0700 | [diff] [blame] | 306 | hnd = attempt_to_load_system_driver(cnx, nullptr, true); |
| 307 | } |
| 308 | |
| 309 | if (!hnd && !failToLoadFromDriverSuffixProperty) { |
| 310 | hnd = attempt_to_load_system_driver(cnx, nullptr, false); |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 313 | if (!hnd) { |
| 314 | android::GraphicsEnv::getInstance().setDriverLoaded(android::GraphicsEnv::Api::API_GL, |
| 315 | false, systemTime() - openTime); |
| 316 | } |
| 317 | |
Peiyong Lin | 3575b9f | 2019-04-25 14:26:49 -0700 | [diff] [blame] | 318 | LOG_ALWAYS_FATAL_IF(!hnd, |
| 319 | "couldn't find an OpenGL ES implementation, make sure you set %s or %s", |
| 320 | HAL_SUBNAME_KEY_PROPERTIES[0], HAL_SUBNAME_KEY_PROPERTIES[1]); |
Jesse Hall | c07b520 | 2013-07-04 12:08:16 -0700 | [diff] [blame] | 321 | |
Yiwei Zhang | 5e21eb3 | 2019-06-05 00:26:03 -0700 | [diff] [blame^] | 322 | if (!cnx->libEgl) { |
| 323 | cnx->libEgl = load_wrapper(EGL_WRAPPER_DIR "/libEGL.so"); |
| 324 | } |
| 325 | if (!cnx->libGles1) { |
| 326 | cnx->libGles1 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv1_CM.so"); |
| 327 | } |
| 328 | if (!cnx->libGles2) { |
| 329 | cnx->libGles2 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv2.so"); |
| 330 | } |
Evgenii Stepanov | c2466e6 | 2015-07-08 15:49:52 -0700 | [diff] [blame] | 331 | |
Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 332 | if (!cnx->libEgl || !cnx->libGles2 || !cnx->libGles1) { |
| 333 | android::GraphicsEnv::getInstance().setDriverLoaded(android::GraphicsEnv::Api::API_GL, |
| 334 | false, systemTime() - openTime); |
| 335 | } |
| 336 | |
Michael Chock | c0ec5e2 | 2014-01-27 08:14:33 -0800 | [diff] [blame] | 337 | LOG_ALWAYS_FATAL_IF(!cnx->libEgl, |
| 338 | "couldn't load system EGL wrapper libraries"); |
| 339 | |
Jesse Hall | c07b520 | 2013-07-04 12:08:16 -0700 | [diff] [blame] | 340 | LOG_ALWAYS_FATAL_IF(!cnx->libGles2 || !cnx->libGles1, |
| 341 | "couldn't load system OpenGL ES wrapper libraries"); |
| 342 | |
Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 343 | android::GraphicsEnv::getInstance().setDriverLoaded(android::GraphicsEnv::Api::API_GL, true, |
| 344 | systemTime() - openTime); |
Yiwei Zhang | cb9d4e4 | 2019-02-06 20:22:59 -0800 | [diff] [blame] | 345 | |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 346 | return (void*)hnd; |
| 347 | } |
| 348 | |
Courtney Goeltzenleuchter | ee768c2 | 2018-10-25 11:43:52 -0600 | [diff] [blame] | 349 | void Loader::close(egl_connection_t* cnx) |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 350 | { |
Courtney Goeltzenleuchter | ee768c2 | 2018-10-25 11:43:52 -0600 | [diff] [blame] | 351 | driver_t* hnd = (driver_t*) cnx->dso; |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 352 | delete hnd; |
Courtney Goeltzenleuchter | ee768c2 | 2018-10-25 11:43:52 -0600 | [diff] [blame] | 353 | cnx->dso = nullptr; |
| 354 | |
Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 355 | cnx->shouldUseAngle = false; |
Courtney Goeltzenleuchter | ee768c2 | 2018-10-25 11:43:52 -0600 | [diff] [blame] | 356 | cnx->angleDecided = false; |
| 357 | cnx->useAngle = false; |
| 358 | |
| 359 | if (cnx->vendorEGL) { |
| 360 | dlclose(cnx->vendorEGL); |
| 361 | cnx->vendorEGL = nullptr; |
| 362 | } |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 363 | } |
| 364 | |
Jesse Hall | 94cdba9 | 2013-07-11 09:40:35 -0700 | [diff] [blame] | 365 | void Loader::init_api(void* dso, |
| 366 | char const * const * api, |
Yiwei Zhang | 7cc5892 | 2018-10-10 11:37:43 -0700 | [diff] [blame] | 367 | char const * const * ref_api, |
Jesse Hall | 94cdba9 | 2013-07-11 09:40:35 -0700 | [diff] [blame] | 368 | __eglMustCastToProperFunctionPointerType* curr, |
| 369 | getProcAddressType getProcAddress) |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 370 | { |
Jesse Hall | 1508ae6 | 2017-01-19 17:43:26 -0800 | [diff] [blame] | 371 | ATRACE_CALL(); |
| 372 | |
Mathias Agopian | 7773c43 | 2012-02-13 20:06:08 -0800 | [diff] [blame] | 373 | const ssize_t SIZE = 256; |
Mathias Agopian | 0ad71a9 | 2011-05-11 20:37:47 -0700 | [diff] [blame] | 374 | char scrap[SIZE]; |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 375 | while (*api) { |
| 376 | char const * name = *api; |
Yiwei Zhang | 7cc5892 | 2018-10-10 11:37:43 -0700 | [diff] [blame] | 377 | if (ref_api) { |
| 378 | char const * ref_name = *ref_api; |
| 379 | if (std::strcmp(name, ref_name) != 0) { |
| 380 | *curr++ = nullptr; |
| 381 | ref_api++; |
| 382 | continue; |
| 383 | } |
| 384 | } |
| 385 | |
Jesse Hall | 94cdba9 | 2013-07-11 09:40:35 -0700 | [diff] [blame] | 386 | __eglMustCastToProperFunctionPointerType f = |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 387 | (__eglMustCastToProperFunctionPointerType)dlsym(dso, name); |
Yi Kong | 48a6cd2 | 2018-07-18 10:07:09 -0700 | [diff] [blame] | 388 | if (f == nullptr) { |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 389 | // couldn't find the entry-point, use eglGetProcAddress() |
| 390 | f = getProcAddress(name); |
| 391 | } |
Yi Kong | 48a6cd2 | 2018-07-18 10:07:09 -0700 | [diff] [blame] | 392 | if (f == nullptr) { |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 393 | // Try without the OES postfix |
| 394 | ssize_t index = ssize_t(strlen(name)) - 3; |
Mathias Agopian | 0ad71a9 | 2011-05-11 20:37:47 -0700 | [diff] [blame] | 395 | if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) { |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 396 | strncpy(scrap, name, index); |
| 397 | scrap[index] = 0; |
| 398 | f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap); |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 399 | //ALOGD_IF(f, "found <%s> instead", scrap); |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 400 | } |
| 401 | } |
Yi Kong | 48a6cd2 | 2018-07-18 10:07:09 -0700 | [diff] [blame] | 402 | if (f == nullptr) { |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 403 | // Try with the OES postfix |
Mathias Agopian | 0ad71a9 | 2011-05-11 20:37:47 -0700 | [diff] [blame] | 404 | ssize_t index = ssize_t(strlen(name)) - 3; |
| 405 | if (index>0 && strcmp(name+index, "OES")) { |
| 406 | snprintf(scrap, SIZE, "%sOES", name); |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 407 | f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap); |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 408 | //ALOGD_IF(f, "found <%s> instead", scrap); |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 409 | } |
| 410 | } |
Yi Kong | 48a6cd2 | 2018-07-18 10:07:09 -0700 | [diff] [blame] | 411 | if (f == nullptr) { |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 412 | //ALOGD("%s", name); |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 413 | f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented; |
Mathias Agopian | 48d438d | 2012-01-28 21:44:00 -0800 | [diff] [blame] | 414 | |
| 415 | /* |
| 416 | * GL_EXT_debug_label is special, we always report it as |
| 417 | * supported, it's handled by GLES_trace. If GLES_trace is not |
| 418 | * enabled, then these are no-ops. |
| 419 | */ |
| 420 | if (!strcmp(name, "glInsertEventMarkerEXT")) { |
| 421 | f = (__eglMustCastToProperFunctionPointerType)gl_noop; |
| 422 | } else if (!strcmp(name, "glPushGroupMarkerEXT")) { |
| 423 | f = (__eglMustCastToProperFunctionPointerType)gl_noop; |
| 424 | } else if (!strcmp(name, "glPopGroupMarkerEXT")) { |
| 425 | f = (__eglMustCastToProperFunctionPointerType)gl_noop; |
| 426 | } |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 427 | } |
| 428 | *curr++ = f; |
| 429 | api++; |
Yiwei Zhang | 7cc5892 | 2018-10-10 11:37:43 -0700 | [diff] [blame] | 430 | if (ref_api) ref_api++; |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | |
Peiyong Lin | 0d10b25 | 2019-04-30 18:03:04 -0700 | [diff] [blame] | 434 | static void* load_system_driver(const char* kind, const char* suffix, const bool exact) { |
Jesse Hall | 1508ae6 | 2017-01-19 17:43:26 -0800 | [diff] [blame] | 435 | ATRACE_CALL(); |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 436 | class MatchFile { |
| 437 | public: |
Peiyong Lin | 0d10b25 | 2019-04-30 18:03:04 -0700 | [diff] [blame] | 438 | static std::string find(const char* libraryName, const bool exact) { |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 439 | const char* const searchPaths[] = { |
Dan Willemsen | 8edb8f5 | 2014-02-16 10:23:54 -0800 | [diff] [blame] | 440 | #if defined(__LP64__) |
| 441 | "/vendor/lib64/egl", |
| 442 | "/system/lib64/egl" |
| 443 | #else |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 444 | "/vendor/lib/egl", |
| 445 | "/system/lib/egl" |
Dan Willemsen | 8edb8f5 | 2014-02-16 10:23:54 -0800 | [diff] [blame] | 446 | #endif |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 447 | }; |
Brian Swetland | 2b9e4f6 | 2010-09-20 12:58:15 -0700 | [diff] [blame] | 448 | |
Peiyong Lin | 9f0c79d | 2019-04-22 10:14:57 -0700 | [diff] [blame] | 449 | for (auto dir : searchPaths) { |
Peiyong Lin | 0d10b25 | 2019-04-30 18:03:04 -0700 | [diff] [blame] | 450 | std::string absolutePath; |
| 451 | if (find(absolutePath, libraryName, dir, exact)) { |
Peiyong Lin | 9f0c79d | 2019-04-22 10:14:57 -0700 | [diff] [blame] | 452 | return absolutePath; |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 453 | } |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 454 | } |
| 455 | |
Peiyong Lin | 9f0c79d | 2019-04-22 10:14:57 -0700 | [diff] [blame] | 456 | // Driver not found. gah. |
| 457 | return std::string(); |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 458 | } |
Peiyong Lin | 0d10b25 | 2019-04-30 18:03:04 -0700 | [diff] [blame] | 459 | private: |
| 460 | static bool find(std::string& result, |
| 461 | const std::string& pattern, const char* const search, bool exact) { |
| 462 | if (exact) { |
| 463 | std::string absolutePath = std::string(search) + "/" + pattern + ".so"; |
| 464 | if (!access(absolutePath.c_str(), R_OK)) { |
| 465 | result = absolutePath; |
| 466 | return true; |
| 467 | } |
| 468 | return false; |
| 469 | } |
| 470 | |
| 471 | DIR* d = opendir(search); |
| 472 | if (d != nullptr) { |
| 473 | struct dirent* e; |
| 474 | while ((e = readdir(d)) != nullptr) { |
| 475 | if (e->d_type == DT_DIR) { |
| 476 | continue; |
| 477 | } |
| 478 | if (!strcmp(e->d_name, "libGLES_android.so")) { |
| 479 | // always skip the software renderer |
| 480 | continue; |
| 481 | } |
| 482 | if (strstr(e->d_name, pattern.c_str()) == e->d_name) { |
| 483 | if (!strcmp(e->d_name + strlen(e->d_name) - 3, ".so")) { |
| 484 | result = std::string(search) + "/" + e->d_name; |
| 485 | closedir(d); |
| 486 | return true; |
| 487 | } |
| 488 | } |
| 489 | } |
| 490 | closedir(d); |
| 491 | } |
| 492 | return false; |
| 493 | } |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 494 | }; |
| 495 | |
Peiyong Lin | 9f0c79d | 2019-04-22 10:14:57 -0700 | [diff] [blame] | 496 | std::string libraryName = std::string("lib") + kind; |
| 497 | if (suffix) { |
| 498 | libraryName += std::string("_") + suffix; |
Peiyong Lin | 0d10b25 | 2019-04-30 18:03:04 -0700 | [diff] [blame] | 499 | } else if (!exact) { |
| 500 | // Deprecated: we look for files that match |
| 501 | // libGLES_*.so, or: |
| 502 | // libEGL_*.so, libGLESv1_CM_*.so, libGLESv2_*.so |
| 503 | libraryName += std::string("_"); |
Peiyong Lin | 9f0c79d | 2019-04-22 10:14:57 -0700 | [diff] [blame] | 504 | } |
Peiyong Lin | 0d10b25 | 2019-04-30 18:03:04 -0700 | [diff] [blame] | 505 | std::string absolutePath = MatchFile::find(libraryName.c_str(), exact); |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 506 | if (absolutePath.empty()) { |
Mathias Agopian | 9938142 | 2013-04-23 20:52:29 +0200 | [diff] [blame] | 507 | // this happens often, we don't want to log an error |
Yi Kong | 48a6cd2 | 2018-07-18 10:07:09 -0700 | [diff] [blame] | 508 | return nullptr; |
Mathias Agopian | 8c17384 | 2009-09-20 16:01:02 -0700 | [diff] [blame] | 509 | } |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 510 | const char* const driver_absolute_path = absolutePath.c_str(); |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 511 | |
Jiyong Park | 5910dc7 | 2017-04-05 14:23:52 +0900 | [diff] [blame] | 512 | // 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] | 513 | // the original routine when the namespace does not exist. |
Jiyong Park | 5910dc7 | 2017-04-05 14:23:52 +0900 | [diff] [blame] | 514 | // See /system/core/rootdir/etc/ld.config.txt for the configuration of the |
| 515 | // sphal namespace. |
Justin Yun | b732030 | 2017-05-22 15:13:40 +0900 | [diff] [blame] | 516 | void* dso = do_android_load_sphal_library(driver_absolute_path, |
| 517 | RTLD_NOW | RTLD_LOCAL); |
Yi Kong | 48a6cd2 | 2018-07-18 10:07:09 -0700 | [diff] [blame] | 518 | if (dso == nullptr) { |
Mathias Agopian | 8c17384 | 2009-09-20 16:01:02 -0700 | [diff] [blame] | 519 | const char* err = dlerror(); |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 520 | ALOGE("load_driver(%s): %s", driver_absolute_path, err ? err : "unknown"); |
Yi Kong | 48a6cd2 | 2018-07-18 10:07:09 -0700 | [diff] [blame] | 521 | return nullptr; |
Mathias Agopian | 8c17384 | 2009-09-20 16:01:02 -0700 | [diff] [blame] | 522 | } |
| 523 | |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 524 | ALOGD("loaded %s", driver_absolute_path); |
Mathias Agopian | baca89c | 2009-08-20 19:09:34 -0700 | [diff] [blame] | 525 | |
Jesse Hall | 7a8d83e | 2016-12-20 15:24:28 -0800 | [diff] [blame] | 526 | return dso; |
| 527 | } |
| 528 | |
Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 529 | static void* load_angle_from_namespace(const char* kind, android_namespace_t* ns) { |
| 530 | const android_dlextinfo dlextinfo = { |
| 531 | .flags = ANDROID_DLEXT_USE_NAMESPACE, |
| 532 | .library_namespace = ns, |
| 533 | }; |
| 534 | |
| 535 | std::string name = std::string("lib") + kind + "_angle.so"; |
| 536 | |
| 537 | void* so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo); |
| 538 | |
| 539 | if (so) { |
| 540 | ALOGD("dlopen_ext from APK (%s) success at %p", name.c_str(), so); |
| 541 | return so; |
| 542 | } else { |
| 543 | ALOGE("dlopen_ext(\"%s\") failed: %s", name.c_str(), dlerror()); |
| 544 | } |
| 545 | |
| 546 | return nullptr; |
| 547 | } |
| 548 | |
Cody Northrop | 6d082ef | 2018-09-11 09:42:31 -0600 | [diff] [blame] | 549 | static void* load_angle(const char* kind, android_namespace_t* ns, egl_connection_t* cnx) { |
Courtney Goeltzenleuchter | 985bc28 | 2018-11-02 14:24:55 -0600 | [diff] [blame] | 550 | void* so = nullptr; |
Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 551 | |
| 552 | if ((cnx->shouldUseAngle) || android::GraphicsEnv::getInstance().shouldUseAngle()) { |
Ian Elliott | 3447447 | 2018-09-05 10:57:07 -0600 | [diff] [blame] | 553 | so = load_angle_from_namespace(kind, ns); |
Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 554 | cnx->shouldUseAngle = true; |
| 555 | } else { |
| 556 | cnx->shouldUseAngle = false; |
Ian Elliott | 3447447 | 2018-09-05 10:57:07 -0600 | [diff] [blame] | 557 | } |
Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 558 | |
| 559 | if (so) { |
Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 560 | ALOGV("Loaded ANGLE %s library for '%s' (instead of native)", kind, |
| 561 | android::GraphicsEnv::getInstance().getAngleAppName().c_str()); |
Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 562 | cnx->useAngle = true; |
Courtney Goeltzenleuchter | b91b989 | 2018-10-23 16:28:00 -0600 | [diff] [blame] | 563 | |
Courtney Goeltzenleuchter | b91b989 | 2018-10-23 16:28:00 -0600 | [diff] [blame] | 564 | char prop[PROPERTY_VALUE_MAX]; |
Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 565 | |
| 566 | property_get("debug.hwui.renderer", prop, "UNSET"); |
| 567 | ALOGV("Skia's renderer set to %s", prop); |
| 568 | |
| 569 | EGLint angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE; |
Courtney Goeltzenleuchter | b91b989 | 2018-10-23 16:28:00 -0600 | [diff] [blame] | 570 | property_get("debug.angle.backend", prop, "0"); |
| 571 | switch (atoi(prop)) { |
| 572 | case 1: |
| 573 | ALOGV("%s: Requesting OpenGLES back-end", __FUNCTION__); |
| 574 | angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE; |
| 575 | break; |
| 576 | case 2: |
| 577 | ALOGV("%s: Requesting Vulkan back-end", __FUNCTION__); |
| 578 | angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE; |
| 579 | break; |
| 580 | default: |
| 581 | break; |
| 582 | } |
| 583 | |
| 584 | cnx->angleBackend = angleBackendDefault; |
| 585 | if (!cnx->vendorEGL && (cnx->angleBackend == EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE)) { |
| 586 | // Find and load vendor libEGL for ANGLE's GL back-end to use. |
Peiyong Lin | 9f0c79d | 2019-04-22 10:14:57 -0700 | [diff] [blame] | 587 | char prop[PROPERTY_VALUE_MAX + 1]; |
| 588 | for (auto key : HAL_SUBNAME_KEY_PROPERTIES) { |
| 589 | if (property_get(key, prop, nullptr) <= 0) { |
| 590 | continue; |
| 591 | } |
Peiyong Lin | 0d10b25 | 2019-04-30 18:03:04 -0700 | [diff] [blame] | 592 | void* dso = load_system_driver("EGL", prop, true); |
Peiyong Lin | 9f0c79d | 2019-04-22 10:14:57 -0700 | [diff] [blame] | 593 | if (dso) { |
| 594 | cnx->vendorEGL = dso; |
| 595 | break; |
| 596 | } |
| 597 | } |
| 598 | if (!cnx->vendorEGL) { |
Peiyong Lin | 0d10b25 | 2019-04-30 18:03:04 -0700 | [diff] [blame] | 599 | cnx->vendorEGL = load_system_driver("EGL", nullptr, true); |
Peiyong Lin | 9f0c79d | 2019-04-22 10:14:57 -0700 | [diff] [blame] | 600 | } |
Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 601 | } |
Ian Elliott | b058aff | 2018-08-09 12:00:55 -0600 | [diff] [blame] | 602 | } else { |
Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 603 | ALOGV("Loaded native %s library for '%s' (instead of ANGLE)", kind, |
| 604 | android::GraphicsEnv::getInstance().getAngleAppName().c_str()); |
| 605 | cnx->useAngle = false; |
Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 606 | } |
Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 607 | cnx->angleDecided = true; |
Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 608 | |
Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 609 | return so; |
Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 610 | } |
| 611 | |
Jesse Hall | 7a8d83e | 2016-12-20 15:24:28 -0800 | [diff] [blame] | 612 | static void* load_updated_driver(const char* kind, android_namespace_t* ns) { |
Jesse Hall | 1508ae6 | 2017-01-19 17:43:26 -0800 | [diff] [blame] | 613 | ATRACE_CALL(); |
Jesse Hall | 7a8d83e | 2016-12-20 15:24:28 -0800 | [diff] [blame] | 614 | const android_dlextinfo dlextinfo = { |
| 615 | .flags = ANDROID_DLEXT_USE_NAMESPACE, |
| 616 | .library_namespace = ns, |
| 617 | }; |
| 618 | void* so = nullptr; |
| 619 | char prop[PROPERTY_VALUE_MAX + 1]; |
| 620 | for (auto key : HAL_SUBNAME_KEY_PROPERTIES) { |
Peiyong Lin | 9f0c79d | 2019-04-22 10:14:57 -0700 | [diff] [blame] | 621 | if (property_get(key, prop, nullptr) <= 0) { |
| 622 | continue; |
| 623 | } |
| 624 | std::string name = std::string("lib") + kind + "_" + prop + ".so"; |
| 625 | so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo); |
| 626 | if (so) { |
| 627 | return so; |
Jesse Hall | 7a8d83e | 2016-12-20 15:24:28 -0800 | [diff] [blame] | 628 | } |
| 629 | } |
| 630 | return nullptr; |
| 631 | } |
| 632 | |
Peiyong Lin | 8cd204d | 2019-04-19 14:05:17 -0700 | [diff] [blame] | 633 | Loader::driver_t* Loader::attempt_to_load_angle(egl_connection_t* cnx) { |
Jesse Hall | 1508ae6 | 2017-01-19 17:43:26 -0800 | [diff] [blame] | 634 | ATRACE_CALL(); |
Courtney Goeltzenleuchter | 30ad2ab | 2018-10-30 08:20:44 -0600 | [diff] [blame] | 635 | android_namespace_t* ns = android::GraphicsEnv::getInstance().getAngleNamespace(); |
Peiyong Lin | 8cd204d | 2019-04-19 14:05:17 -0700 | [diff] [blame] | 636 | if (!ns) { |
| 637 | return nullptr; |
Cody Northrop | 6d082ef | 2018-09-11 09:42:31 -0600 | [diff] [blame] | 638 | } |
Peiyong Lin | 8cd204d | 2019-04-19 14:05:17 -0700 | [diff] [blame] | 639 | |
| 640 | android::GraphicsEnv::getInstance().setDriverToLoad(android::GraphicsEnv::Driver::ANGLE); |
| 641 | driver_t* hnd = nullptr; |
| 642 | |
| 643 | // ANGLE doesn't ship with GLES library, and thus we skip GLES driver. |
| 644 | void* dso = load_angle("EGL", ns, cnx); |
| 645 | if (dso) { |
| 646 | initialize_api(dso, cnx, EGL); |
| 647 | hnd = new driver_t(dso); |
| 648 | |
| 649 | dso = load_angle("GLESv1_CM", ns, cnx); |
| 650 | initialize_api(dso, cnx, GLESv1_CM); |
| 651 | hnd->set(dso, GLESv1_CM); |
| 652 | |
| 653 | dso = load_angle("GLESv2", ns, cnx); |
| 654 | initialize_api(dso, cnx, GLESv2); |
| 655 | hnd->set(dso, GLESv2); |
| 656 | } |
| 657 | return hnd; |
| 658 | } |
| 659 | |
| 660 | Loader::driver_t* Loader::attempt_to_load_updated_driver(egl_connection_t* cnx) { |
| 661 | ATRACE_CALL(); |
Jiyong Park | a243e5d | 2017-06-21 12:26:51 +0900 | [diff] [blame] | 662 | #ifndef __ANDROID_VNDK__ |
Peiyong Lin | 8cd204d | 2019-04-19 14:05:17 -0700 | [diff] [blame] | 663 | android_namespace_t* ns = android::GraphicsEnv::getInstance().getDriverNamespace(); |
| 664 | if (!ns) { |
| 665 | return nullptr; |
Jesse Hall | 7a8d83e | 2016-12-20 15:24:28 -0800 | [diff] [blame] | 666 | } |
| 667 | |
Yiwei Zhang | 5e21eb3 | 2019-06-05 00:26:03 -0700 | [diff] [blame^] | 668 | ALOGD("Load updated gl driver."); |
Peiyong Lin | 8cd204d | 2019-04-19 14:05:17 -0700 | [diff] [blame] | 669 | android::GraphicsEnv::getInstance().setDriverToLoad(android::GraphicsEnv::Driver::GL_UPDATED); |
| 670 | driver_t* hnd = nullptr; |
| 671 | void* dso = load_updated_driver("GLES", ns); |
Peiyong Lin | e83b868 | 2019-04-18 17:23:02 -0700 | [diff] [blame] | 672 | if (dso) { |
Peiyong Lin | 8cd204d | 2019-04-19 14:05:17 -0700 | [diff] [blame] | 673 | initialize_api(dso, cnx, EGL | GLESv1_CM | GLESv2); |
| 674 | hnd = new driver_t(dso); |
| 675 | return hnd; |
Peiyong Lin | e83b868 | 2019-04-18 17:23:02 -0700 | [diff] [blame] | 676 | } |
| 677 | |
Peiyong Lin | 8cd204d | 2019-04-19 14:05:17 -0700 | [diff] [blame] | 678 | dso = load_updated_driver("EGL", ns); |
| 679 | if (dso) { |
| 680 | initialize_api(dso, cnx, EGL); |
| 681 | hnd = new driver_t(dso); |
| 682 | |
| 683 | dso = load_updated_driver("GLESv1_CM", ns); |
| 684 | initialize_api(dso, cnx, GLESv1_CM); |
| 685 | hnd->set(dso, GLESv1_CM); |
| 686 | |
| 687 | dso = load_updated_driver("GLESv2", ns); |
| 688 | initialize_api(dso, cnx, GLESv2); |
| 689 | hnd->set(dso, GLESv2); |
| 690 | } |
| 691 | return hnd; |
| 692 | #else |
Peiyong Lin | e83b868 | 2019-04-18 17:23:02 -0700 | [diff] [blame] | 693 | return nullptr; |
Peiyong Lin | 8cd204d | 2019-04-19 14:05:17 -0700 | [diff] [blame] | 694 | #endif |
| 695 | } |
| 696 | |
Peiyong Lin | 0d10b25 | 2019-04-30 18:03:04 -0700 | [diff] [blame] | 697 | Loader::driver_t* Loader::attempt_to_load_system_driver(egl_connection_t* cnx, const char* suffix, |
| 698 | const bool exact) { |
Peiyong Lin | 8cd204d | 2019-04-19 14:05:17 -0700 | [diff] [blame] | 699 | ATRACE_CALL(); |
| 700 | android::GraphicsEnv::getInstance().setDriverToLoad(android::GraphicsEnv::Driver::GL); |
| 701 | driver_t* hnd = nullptr; |
Peiyong Lin | 0d10b25 | 2019-04-30 18:03:04 -0700 | [diff] [blame] | 702 | void* dso = load_system_driver("GLES", suffix, exact); |
Peiyong Lin | 8cd204d | 2019-04-19 14:05:17 -0700 | [diff] [blame] | 703 | if (dso) { |
| 704 | initialize_api(dso, cnx, EGL | GLESv1_CM | GLESv2); |
| 705 | hnd = new driver_t(dso); |
| 706 | return hnd; |
| 707 | } |
Peiyong Lin | 0d10b25 | 2019-04-30 18:03:04 -0700 | [diff] [blame] | 708 | dso = load_system_driver("EGL", suffix, exact); |
Peiyong Lin | 8cd204d | 2019-04-19 14:05:17 -0700 | [diff] [blame] | 709 | if (dso) { |
| 710 | initialize_api(dso, cnx, EGL); |
| 711 | hnd = new driver_t(dso); |
| 712 | |
Peiyong Lin | 0d10b25 | 2019-04-30 18:03:04 -0700 | [diff] [blame] | 713 | dso = load_system_driver("GLESv1_CM", suffix, exact); |
Peiyong Lin | 8cd204d | 2019-04-19 14:05:17 -0700 | [diff] [blame] | 714 | initialize_api(dso, cnx, GLESv1_CM); |
| 715 | hnd->set(dso, GLESv1_CM); |
| 716 | |
Peiyong Lin | 0d10b25 | 2019-04-30 18:03:04 -0700 | [diff] [blame] | 717 | dso = load_system_driver("GLESv2", suffix, exact); |
Peiyong Lin | 8cd204d | 2019-04-19 14:05:17 -0700 | [diff] [blame] | 718 | initialize_api(dso, cnx, GLESv2); |
| 719 | hnd->set(dso, GLESv2); |
| 720 | } |
| 721 | return hnd; |
Peiyong Lin | e83b868 | 2019-04-18 17:23:02 -0700 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | void Loader::initialize_api(void* dso, egl_connection_t* cnx, uint32_t mask) { |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 725 | if (mask & EGL) { |
| 726 | getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress"); |
| 727 | |
Jesse Hall | 94cdba9 | 2013-07-11 09:40:35 -0700 | [diff] [blame] | 728 | ALOGE_IF(!getProcAddress, |
Jesse Hall | 7a8d83e | 2016-12-20 15:24:28 -0800 | [diff] [blame] | 729 | "can't find eglGetProcAddress() in EGL driver library"); |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 730 | |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 731 | egl_t* egl = &cnx->egl; |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 732 | __eglMustCastToProperFunctionPointerType* curr = |
| 733 | (__eglMustCastToProperFunctionPointerType*)egl; |
| 734 | char const * const * api = egl_names; |
| 735 | while (*api) { |
| 736 | char const * name = *api; |
Jesse Hall | 94cdba9 | 2013-07-11 09:40:35 -0700 | [diff] [blame] | 737 | __eglMustCastToProperFunctionPointerType f = |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 738 | (__eglMustCastToProperFunctionPointerType)dlsym(dso, name); |
Yi Kong | 48a6cd2 | 2018-07-18 10:07:09 -0700 | [diff] [blame] | 739 | if (f == nullptr) { |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 740 | // couldn't find the entry-point, use eglGetProcAddress() |
| 741 | f = getProcAddress(name); |
Yi Kong | 48a6cd2 | 2018-07-18 10:07:09 -0700 | [diff] [blame] | 742 | if (f == nullptr) { |
| 743 | f = (__eglMustCastToProperFunctionPointerType)nullptr; |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 744 | } |
| 745 | } |
| 746 | *curr++ = f; |
| 747 | api++; |
| 748 | } |
| 749 | } |
Jesse Hall | 94cdba9 | 2013-07-11 09:40:35 -0700 | [diff] [blame] | 750 | |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 751 | if (mask & GLESv1_CM) { |
Yiwei Zhang | 7cc5892 | 2018-10-10 11:37:43 -0700 | [diff] [blame] | 752 | init_api(dso, gl_names_1, gl_names, |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 753 | (__eglMustCastToProperFunctionPointerType*) |
Mathias Agopian | 7773c43 | 2012-02-13 20:06:08 -0800 | [diff] [blame] | 754 | &cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl, |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 755 | getProcAddress); |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | if (mask & GLESv2) { |
Yiwei Zhang | 7cc5892 | 2018-10-10 11:37:43 -0700 | [diff] [blame] | 759 | init_api(dso, gl_names, nullptr, |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 760 | (__eglMustCastToProperFunctionPointerType*) |
Mathias Agopian | 7773c43 | 2012-02-13 20:06:08 -0800 | [diff] [blame] | 761 | &cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl, |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 762 | getProcAddress); |
| 763 | } |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 764 | } |
| 765 | |
Peiyong Lin | 8cd204d | 2019-04-19 14:05:17 -0700 | [diff] [blame] | 766 | } // namespace android |