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