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