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