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