blob: 0e685bc15b9cb46a9ba148081af594e4ba653412 [file] [log] [blame]
Jesse Hall94cdba92013-07-11 09:40:35 -07001/*
Mathias Agopiande586972009-05-28 17:39:03 -07002 ** Copyright 2007, The Android Open Source Project
3 **
Jesse Hall94cdba92013-07-11 09:40:35 -07004 ** 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 Agopiande586972009-05-28 17:39:03 -07007 **
Jesse Hall94cdba92013-07-11 09:40:35 -07008 ** http://www.apache.org/licenses/LICENSE-2.0
Mathias Agopiande586972009-05-28 17:39:03 -07009 **
Jesse Hall94cdba92013-07-11 09:40:35 -070010 ** 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 Agopiande586972009-05-28 17:39:03 -070014 ** limitations under the License.
15 */
16
Jesse Hall7a8d83e2016-12-20 15:24:28 -080017//#define LOG_NDEBUG 0
Jesse Hall1508ae62017-01-19 17:43:26 -080018#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Jesse Hall7a8d83e2016-12-20 15:24:28 -080019
Yiwei Zhangd40aaac2020-06-04 05:26:56 -070020#include "EGL/Loader.h"
Mathias Agopiande586972009-05-28 17:39:03 -070021
Michael Hoisie4e0f56b2020-04-30 18:40:55 -040022#include <android-base/properties.h>
Jesse Hall7a8d83e2016-12-20 15:24:28 -080023#include <android/dlext.h>
Peiyong Lin98db8e02023-04-05 20:09:16 +000024#include <cutils/properties.h>
Yiwei Zhangd40aaac2020-06-04 05:26:56 -070025#include <dirent.h>
26#include <dlfcn.h>
27#include <graphicsenv/GraphicsEnv.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070028#include <log/log.h>
Yiwei Zhangd9861812019-02-13 11:51:55 -080029#include <utils/Timers.h>
Justin Yunb7320302017-05-22 15:13:40 +090030#include <vndksupport/linker.h>
Mathias Agopiande586972009-05-28 17:39:03 -070031
Yiwei Zhangd40aaac2020-06-04 05:26:56 -070032#include <string>
33
Yiwei Zhang8af003e2020-06-04 14:11:30 -070034#include "EGL/eglext_angle.h"
Cody Northrop68d10352018-10-15 07:22:09 -060035#include "egl_platform_entries.h"
Mathias Agopian65421432017-03-08 11:49:05 -080036#include "egl_trace.h"
Mathias Agopian1cadb252011-05-23 17:26:14 -070037#include "egldefs.h"
Mathias Agopiande586972009-05-28 17:39:03 -070038
Mathias Agopiande586972009-05-28 17:39:03 -070039namespace android {
Mathias Agopiande586972009-05-28 17:39:03 -070040
41/*
Mathias Agopian99381422013-04-23 20:52:29 +020042 * EGL userspace drivers must be provided either:
43 * - as a single library:
Peiyong Lind30aaad2023-08-03 19:12:49 +000044 * /vendor/${LIB}/egl/libGLES.so
Mathias Agopian99381422013-04-23 20:52:29 +020045 *
46 * - as separate libraries:
Peiyong Lind30aaad2023-08-03 19:12:49 +000047 * /vendor/${LIB}/egl/libEGL.so
48 * /vendor/${LIB}/egl/libGLESv1_CM.so
49 * /vendor/${LIB}/egl/libGLESv2.so
Mathias Agopian99381422013-04-23 20:52:29 +020050 *
Mathias Agopian99381422013-04-23 20:52:29 +020051 * For backward compatibility and to facilitate the transition to
52 * this new naming scheme, the loader will additionally look for:
Jesse Hall94cdba92013-07-11 09:40:35 -070053 *
Peiyong Lind30aaad2023-08-03 19:12:49 +000054 * /vendor/${LIB}/egl/lib{GLES | [EGL|GLESv1_CM|GLESv2]}_${SUFFIX}.so
Jesse Hall94cdba92013-07-11 09:40:35 -070055 *
Mathias Agopiande586972009-05-28 17:39:03 -070056 */
57
Peiyong Lind30aaad2023-08-03 19:12:49 +000058#ifndef SYSTEM_LIB_PATH
59#if defined(__LP64__)
60#define SYSTEM_LIB_PATH "/system/lib64"
61#else
62#define SYSTEM_LIB_PATH "/system/lib"
63#endif
64#endif
65
66static const char* PERSIST_DRIVER_SUFFIX_PROPERTY = "persist.graphics.egl";
67static const char* RO_DRIVER_SUFFIX_PROPERTY = "ro.hardware.egl";
68static const char* RO_BOARD_PLATFORM_PROPERTY = "ro.board.platform";
Peiyong Line4da7a82023-08-08 20:46:53 +000069static const char* ANGLE_SUFFIX_VALUE = "angle";
Peiyong Lin6b2b28f2023-11-03 04:24:57 +000070static const char* VENDOR_ANGLE_BUILD = "ro.gfx.angle.supported";
Peiyong Lind30aaad2023-08-03 19:12:49 +000071
72static const char* HAL_SUBNAME_KEY_PROPERTIES[3] = {
73 PERSIST_DRIVER_SUFFIX_PROPERTY,
74 RO_DRIVER_SUFFIX_PROPERTY,
75 RO_BOARD_PLATFORM_PROPERTY,
76};
77
78static const char* const VENDOR_LIB_EGL_DIR =
79#if defined(__LP64__)
80 "/vendor/lib64/egl";
81#else
82 "/vendor/lib/egl";
83#endif
Mathias Agopiande586972009-05-28 17:39:03 -070084
Peiyong Line4da7a82023-08-08 20:46:53 +000085static const char* const SYSTEM_LIB_DIR =
86#if defined(__LP64__)
87 "/system/lib64";
88#else
89 "/system/lib";
90#endif
91
Jesse Hall1508ae62017-01-19 17:43:26 -080092static void* do_dlopen(const char* path, int mode) {
93 ATRACE_CALL();
94 return dlopen(path, mode);
95}
96
Jiyong Park5910dc72017-04-05 14:23:52 +090097static void* do_android_dlopen_ext(const char* path, int mode, const android_dlextinfo* info) {
98 ATRACE_CALL();
99 return android_dlopen_ext(path, mode, info);
100}
101
Justin Yunb7320302017-05-22 15:13:40 +0900102static void* do_android_load_sphal_library(const char* path, int mode) {
103 ATRACE_CALL();
104 return android_load_sphal_library(path, mode);
105}
106
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700107static int do_android_unload_sphal_library(void* dso) {
108 ATRACE_CALL();
109 return android_unload_sphal_library(dso);
110}
111
Peiyong Lind30aaad2023-08-03 19:12:49 +0000112static void* load_wrapper(const char* path) {
113 void* so = do_dlopen(path, RTLD_NOW | RTLD_LOCAL);
114 ALOGE_IF(!so, "dlopen(\"%s\") failed: %s", path, dlerror());
115 return so;
116}
117
118Loader& Loader::getInstance() {
119 static Loader loader;
120 return loader;
121}
122
Jesse Hall94cdba92013-07-11 09:40:35 -0700123Loader::driver_t::driver_t(void* gles)
Mathias Agopiande586972009-05-28 17:39:03 -0700124{
125 dso[0] = gles;
126 for (size_t i=1 ; i<NELEM(dso) ; i++)
Yi Kong48a6cd22018-07-18 10:07:09 -0700127 dso[i] = nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700128}
129
Jesse Hall94cdba92013-07-11 09:40:35 -0700130Loader::driver_t::~driver_t()
Mathias Agopiande586972009-05-28 17:39:03 -0700131{
132 for (size_t i=0 ; i<NELEM(dso) ; i++) {
133 if (dso[i]) {
134 dlclose(dso[i]);
Yi Kong48a6cd22018-07-18 10:07:09 -0700135 dso[i] = nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700136 }
137 }
138}
139
Mathias Agopian65421432017-03-08 11:49:05 -0800140int Loader::driver_t::set(void* hnd, int32_t api)
Mathias Agopiande586972009-05-28 17:39:03 -0700141{
142 switch (api) {
143 case EGL:
144 dso[0] = hnd;
145 break;
146 case GLESv1_CM:
147 dso[1] = hnd;
148 break;
149 case GLESv2:
150 dso[2] = hnd;
151 break;
152 default:
Mathias Agopian65421432017-03-08 11:49:05 -0800153 return -EOVERFLOW;
Mathias Agopiande586972009-05-28 17:39:03 -0700154 }
Mathias Agopian65421432017-03-08 11:49:05 -0800155 return 0;
Mathias Agopiande586972009-05-28 17:39:03 -0700156}
157
Mathias Agopiande586972009-05-28 17:39:03 -0700158Loader::Loader()
Yi Kong48a6cd22018-07-18 10:07:09 -0700159 : getProcAddress(nullptr)
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800160{
Mathias Agopiande586972009-05-28 17:39:03 -0700161}
162
Mathias Agopian99381422013-04-23 20:52:29 +0200163Loader::~Loader() {
Mathias Agopiande586972009-05-28 17:39:03 -0700164}
165
Peiyong Lin2f51e752023-06-15 22:35:14 +0000166// Check whether the loaded system drivers should be unloaded in order to
167// load ANGLE or the updatable graphics drivers.
168// If ANGLE namespace is set, it means the application is identified to run on top of ANGLE.
169// If updatable graphics driver namespace is set, it means the application is identified to
170// run on top of updatable graphics drivers.
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700171static bool should_unload_system_driver(egl_connection_t* cnx) {
172 // Return false if the system driver has been unloaded once.
173 if (cnx->systemDriverUnloaded) {
174 return false;
175 }
176
Peiyong Lin2f51e752023-06-15 22:35:14 +0000177 // Return true if ANGLE namespace is set.
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700178 android_namespace_t* ns = android::GraphicsEnv::getInstance().getAngleNamespace();
179 if (ns) {
Peiyong Line88e0ba2023-06-28 15:09:09 +0000180 // Unless the default GLES driver is ANGLE and the process should use system ANGLE, since
181 // the intended GLES driver is already loaded.
182 // This should be updated in a later patch that cleans up namespaces
183 if (!(cnx->angleLoaded && android::GraphicsEnv::getInstance().shouldUseSystemAngle())) {
184 return true;
185 }
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700186 }
187
Peiyong Lin2df5a002023-08-01 23:31:34 +0000188 // Return true if native GLES drivers should be used and ANGLE is already loaded.
189 if (android::GraphicsEnv::getInstance().shouldUseNativeDriver() && cnx->angleLoaded) {
190 return true;
191 }
192
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700193 // Return true if updated driver namespace is set.
194 ns = android::GraphicsEnv::getInstance().getDriverNamespace();
195 if (ns) {
196 return true;
197 }
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700198
199 return false;
200}
201
202static void uninit_api(char const* const* api, __eglMustCastToProperFunctionPointerType* curr) {
203 while (*api) {
204 *curr++ = nullptr;
205 api++;
206 }
207}
208
209void Loader::unload_system_driver(egl_connection_t* cnx) {
210 ATRACE_CALL();
211
212 uninit_api(gl_names,
213 (__eglMustCastToProperFunctionPointerType*)&cnx
214 ->hooks[egl_connection_t::GLESv2_INDEX]
215 ->gl);
216 uninit_api(gl_names,
217 (__eglMustCastToProperFunctionPointerType*)&cnx
218 ->hooks[egl_connection_t::GLESv1_INDEX]
219 ->gl);
220 uninit_api(egl_names, (__eglMustCastToProperFunctionPointerType*)&cnx->egl);
221
222 if (cnx->dso) {
223 ALOGD("Unload system gl driver.");
224 driver_t* hnd = (driver_t*)cnx->dso;
225 if (hnd->dso[2]) {
226 do_android_unload_sphal_library(hnd->dso[2]);
227 }
228 if (hnd->dso[1]) {
229 do_android_unload_sphal_library(hnd->dso[1]);
230 }
231 if (hnd->dso[0]) {
232 do_android_unload_sphal_library(hnd->dso[0]);
233 }
234 cnx->dso = nullptr;
Peiyong Line88e0ba2023-06-28 15:09:09 +0000235 cnx->angleLoaded = false;
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700236 }
237
238 cnx->systemDriverUnloaded = true;
239}
240
Peiyong Line88e0ba2023-06-28 15:09:09 +0000241void* Loader::open(egl_connection_t* cnx) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800242 ATRACE_CALL();
Yiwei Zhangd9861812019-02-13 11:51:55 -0800243 const nsecs_t openTime = systemTime();
Jesse Hall1508ae62017-01-19 17:43:26 -0800244
Peiyong Line88e0ba2023-06-28 15:09:09 +0000245 if (cnx->dso && should_unload_system_driver(cnx)) {
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700246 unload_system_driver(cnx);
247 }
248
249 // If a driver has been loaded, return the driver directly.
250 if (cnx->dso) {
251 return cnx->dso;
252 }
253
Peiyong Line88e0ba2023-06-28 15:09:09 +0000254 driver_t* hnd = nullptr;
255 // Firstly, try to load ANGLE driver, if ANGLE should be loaded and fail, abort.
256 if (android::GraphicsEnv::getInstance().shouldUseAngle()) {
257 hnd = attempt_to_load_angle(cnx);
258 LOG_ALWAYS_FATAL_IF(!hnd, "Failed to load ANGLE.");
259 }
Ian Elliott5279f862022-04-21 12:41:03 -0600260
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700261 if (!hnd) {
262 // Secondly, try to load from driver apk.
263 hnd = attempt_to_load_updated_driver(cnx);
Peiyong Lin2df5a002023-08-01 23:31:34 +0000264
265 // If updated driver apk is set but fail to load, abort here.
266 LOG_ALWAYS_FATAL_IF(android::GraphicsEnv::getInstance().getDriverNamespace(),
267 "couldn't find an OpenGL ES implementation from %s",
268 android::GraphicsEnv::getInstance().getDriverPath().c_str());
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700269 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700270
Peiyong Lin2df5a002023-08-01 23:31:34 +0000271 // Attempt to load native GLES drivers specified by ro.hardware.egl if native is selected.
272 // If native is selected but fail to load, abort.
273 if (!hnd && android::GraphicsEnv::getInstance().shouldUseNativeDriver()) {
274 auto driverSuffix = base::GetProperty(RO_DRIVER_SUFFIX_PROPERTY, "");
275 LOG_ALWAYS_FATAL_IF(driverSuffix.empty(),
276 "Native GLES driver is selected but not specified in %s",
277 RO_DRIVER_SUFFIX_PROPERTY);
278 hnd = attempt_to_load_system_driver(cnx, driverSuffix.c_str(), true);
279 LOG_ALWAYS_FATAL_IF(!hnd, "Native GLES driver is selected but failed to load. %s=%s",
280 RO_DRIVER_SUFFIX_PROPERTY, driverSuffix.c_str());
281 }
282
283 // Finally, try to load default driver.
Peiyong Lin0d10b252019-04-30 18:03:04 -0700284 bool failToLoadFromDriverSuffixProperty = false;
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700285 if (!hnd) {
Peiyong Lin98db8e02023-04-05 20:09:16 +0000286 // Start by searching for the library name appended by the system
287 // properties of the GLES userspace driver in both locations.
288 // i.e.:
289 // libGLES_${prop}.so, or:
290 // libEGL_${prop}.so, libGLESv1_CM_${prop}.so, libGLESv2_${prop}.so
291 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
292 auto prop = base::GetProperty(key, "");
293 if (prop.empty()) {
294 continue;
295 }
296 hnd = attempt_to_load_system_driver(cnx, prop.c_str(), true);
Peiyong Linf7878b72023-06-13 20:51:03 +0000297 if (!hnd) {
298 ALOGD("Failed to load drivers from property %s with value %s", key, prop.c_str());
Peiyong Lin98db8e02023-04-05 20:09:16 +0000299 failToLoadFromDriverSuffixProperty = true;
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700300 }
Peiyong Linf7878b72023-06-13 20:51:03 +0000301
302 // Abort regardless of whether subsequent properties are set, the value must be set
303 // correctly with the first property that has a value.
304 break;
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700305 }
306 }
307
308 if (!hnd) {
Peiyong Linf7878b72023-06-13 20:51:03 +0000309 // Can't find graphics driver by appending the value from system properties, now search for
310 // the exact name without any suffix of the GLES userspace driver in both locations.
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700311 // i.e.:
312 // libGLES.so, or:
313 // libEGL.so, libGLESv1_CM.so, libGLESv2.so
Charlie Lao840bd532020-01-16 17:34:37 -0800314 hnd = attempt_to_load_system_driver(cnx, nullptr, true);
Peiyong Lin0d10b252019-04-30 18:03:04 -0700315 }
316
Peiyong Lin98db8e02023-04-05 20:09:16 +0000317 if (!hnd && !failToLoadFromDriverSuffixProperty &&
318 property_get_int32("ro.vendor.api_level", 0) < __ANDROID_API_U__) {
319 // Still can't find the graphics drivers with the exact name. This time try to use wildcard
320 // matching if the device is launched before Android 14.
Charlie Lao840bd532020-01-16 17:34:37 -0800321 hnd = attempt_to_load_system_driver(cnx, nullptr, false);
Mathias Agopiande586972009-05-28 17:39:03 -0700322 }
323
Yiwei Zhangd9861812019-02-13 11:51:55 -0800324 if (!hnd) {
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700325 android::GraphicsEnv::getInstance().setDriverLoaded(android::GpuStatsInfo::Api::API_GL,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800326 false, systemTime() - openTime);
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600327 } else {
328 // init_angle_backend will check if loaded driver is ANGLE or not,
Peiyong Lin2f51e752023-06-15 22:35:14 +0000329 // will set cnx->angleLoaded appropriately.
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600330 // Do this here so that we use ANGLE path when driver is ANGLE (e.g. loaded as native),
331 // not just loading ANGLE as option.
Peiyong Lin2f51e752023-06-15 22:35:14 +0000332 attempt_to_init_angle_backend(hnd->dso[2], cnx);
Yiwei Zhangd9861812019-02-13 11:51:55 -0800333 }
334
Peiyong Lin3575b9f2019-04-25 14:26:49 -0700335 LOG_ALWAYS_FATAL_IF(!hnd,
Peiyong Lin9679a982023-04-10 22:00:30 +0000336 "couldn't find an OpenGL ES implementation, make sure one of %s, %s and %s "
337 "is set",
338 HAL_SUBNAME_KEY_PROPERTIES[0], HAL_SUBNAME_KEY_PROPERTIES[1],
339 HAL_SUBNAME_KEY_PROPERTIES[2]);
Jesse Hallc07b5202013-07-04 12:08:16 -0700340
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700341 if (!cnx->libEgl) {
Peiyong Lind30aaad2023-08-03 19:12:49 +0000342 cnx->libEgl = load_wrapper(SYSTEM_LIB_PATH "/libEGL.so");
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700343 }
Charlie Lao840bd532020-01-16 17:34:37 -0800344 if (!cnx->libGles1) {
Peiyong Lind30aaad2023-08-03 19:12:49 +0000345 cnx->libGles1 = load_wrapper(SYSTEM_LIB_PATH "/libGLESv1_CM.so");
Charlie Lao840bd532020-01-16 17:34:37 -0800346 }
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700347 if (!cnx->libGles2) {
Peiyong Lind30aaad2023-08-03 19:12:49 +0000348 cnx->libGles2 = load_wrapper(SYSTEM_LIB_PATH "/libGLESv2.so");
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700349 }
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700350
Charlie Lao840bd532020-01-16 17:34:37 -0800351 if (!cnx->libEgl || !cnx->libGles2 || !cnx->libGles1) {
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700352 android::GraphicsEnv::getInstance().setDriverLoaded(android::GpuStatsInfo::Api::API_GL,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800353 false, systemTime() - openTime);
354 }
355
Michael Chockc0ec5e22014-01-27 08:14:33 -0800356 LOG_ALWAYS_FATAL_IF(!cnx->libEgl,
357 "couldn't load system EGL wrapper libraries");
358
Charlie Lao840bd532020-01-16 17:34:37 -0800359 LOG_ALWAYS_FATAL_IF(!cnx->libGles2 || !cnx->libGles1,
360 "couldn't load system OpenGL ES wrapper libraries");
Jesse Hallc07b5202013-07-04 12:08:16 -0700361
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700362 android::GraphicsEnv::getInstance().setDriverLoaded(android::GpuStatsInfo::Api::API_GL, true,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800363 systemTime() - openTime);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800364
Mathias Agopiande586972009-05-28 17:39:03 -0700365 return (void*)hnd;
366}
367
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600368void Loader::close(egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700369{
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600370 driver_t* hnd = (driver_t*) cnx->dso;
Mathias Agopiande586972009-05-28 17:39:03 -0700371 delete hnd;
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600372 cnx->dso = nullptr;
373
Peiyong Lin2f51e752023-06-15 22:35:14 +0000374 cnx->angleLoaded = false;
Mathias Agopiande586972009-05-28 17:39:03 -0700375}
376
Jesse Hall94cdba92013-07-11 09:40:35 -0700377void Loader::init_api(void* dso,
378 char const * const * api,
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700379 char const * const * ref_api,
Jesse Hall94cdba92013-07-11 09:40:35 -0700380 __eglMustCastToProperFunctionPointerType* curr,
381 getProcAddressType getProcAddress)
Mathias Agopiande586972009-05-28 17:39:03 -0700382{
Jesse Hall1508ae62017-01-19 17:43:26 -0800383 ATRACE_CALL();
384
Mathias Agopian7773c432012-02-13 20:06:08 -0800385 const ssize_t SIZE = 256;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700386 char scrap[SIZE];
Mathias Agopiande586972009-05-28 17:39:03 -0700387 while (*api) {
388 char const * name = *api;
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700389 if (ref_api) {
390 char const * ref_name = *ref_api;
391 if (std::strcmp(name, ref_name) != 0) {
392 *curr++ = nullptr;
393 ref_api++;
394 continue;
395 }
396 }
397
Jesse Hall94cdba92013-07-11 09:40:35 -0700398 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700399 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700400 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700401 // couldn't find the entry-point, use eglGetProcAddress()
402 f = getProcAddress(name);
403 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700404 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700405 // Try without the OES postfix
406 ssize_t index = ssize_t(strlen(name)) - 3;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700407 if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) {
Mathias Agopiande586972009-05-28 17:39:03 -0700408 strncpy(scrap, name, index);
409 scrap[index] = 0;
410 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000411 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700412 }
413 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700414 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700415 // Try with the OES postfix
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700416 ssize_t index = ssize_t(strlen(name)) - 3;
417 if (index>0 && strcmp(name+index, "OES")) {
418 snprintf(scrap, SIZE, "%sOES", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700419 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000420 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700421 }
422 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700423 if (f == nullptr) {
Steve Block9d453682011-12-20 16:23:08 +0000424 //ALOGD("%s", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700425 f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented;
Mathias Agopian48d438d2012-01-28 21:44:00 -0800426
427 /*
Tao Wu1cdfe642020-06-23 12:33:02 -0700428 * GL_EXT_debug_marker is special, we always report it as
Mathias Agopian48d438d2012-01-28 21:44:00 -0800429 * supported, it's handled by GLES_trace. If GLES_trace is not
430 * enabled, then these are no-ops.
431 */
432 if (!strcmp(name, "glInsertEventMarkerEXT")) {
433 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
434 } else if (!strcmp(name, "glPushGroupMarkerEXT")) {
435 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
436 } else if (!strcmp(name, "glPopGroupMarkerEXT")) {
437 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
438 }
Mathias Agopiande586972009-05-28 17:39:03 -0700439 }
440 *curr++ = f;
441 api++;
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700442 if (ref_api) ref_api++;
Mathias Agopiande586972009-05-28 17:39:03 -0700443 }
444}
445
Peiyong Line4da7a82023-08-08 20:46:53 +0000446static std::string findLibrary(const std::string libraryName, const std::string searchPath,
447 const bool exact) {
448 if (exact) {
449 std::string absolutePath = searchPath + "/" + libraryName + ".so";
450 if (!access(absolutePath.c_str(), R_OK)) {
451 return absolutePath;
452 }
453 return std::string();
454 }
455
456 DIR* d = opendir(searchPath.c_str());
457 if (d != nullptr) {
458 struct dirent* e;
459 while ((e = readdir(d)) != nullptr) {
460 if (e->d_type == DT_DIR) {
461 continue;
462 }
463 if (!strcmp(e->d_name, "libGLES_android.so")) {
464 // always skip the software renderer
465 continue;
466 }
467 if (strstr(e->d_name, libraryName.c_str()) == e->d_name) {
468 if (!strcmp(e->d_name + strlen(e->d_name) - 3, ".so")) {
469 std::string result = searchPath + "/" + e->d_name;
470 closedir(d);
471 return result;
472 }
473 }
474 }
475 closedir(d);
476 }
477 // Driver not found. gah.
478 return std::string();
479}
480
Peiyong Lin0d10b252019-04-30 18:03:04 -0700481static void* load_system_driver(const char* kind, const char* suffix, const bool exact) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800482 ATRACE_CALL();
Mathias Agopian99381422013-04-23 20:52:29 +0200483
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700484 std::string libraryName = std::string("lib") + kind;
485 if (suffix) {
486 libraryName += std::string("_") + suffix;
Peiyong Lin0d10b252019-04-30 18:03:04 -0700487 } else if (!exact) {
Peiyong Line4da7a82023-08-08 20:46:53 +0000488 // Deprecated for devices launching in Android 14
489 // Look for files that match
490 // libGLES_*.so, or,
Peiyong Lin0d10b252019-04-30 18:03:04 -0700491 // libEGL_*.so, libGLESv1_CM_*.so, libGLESv2_*.so
492 libraryName += std::string("_");
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700493 }
Peiyong Line4da7a82023-08-08 20:46:53 +0000494
495 void* dso = nullptr;
496
Peiyong Lin6b2b28f2023-11-03 04:24:57 +0000497 const bool AngleInVendor = property_get_bool(VENDOR_ANGLE_BUILD, false);
498 const bool isSuffixAngle = suffix != nullptr && strcmp(suffix, ANGLE_SUFFIX_VALUE) == 0;
Peiyong Line4da7a82023-08-08 20:46:53 +0000499 // Only use sphal namespace when system ANGLE binaries are not the default drivers.
Peiyong Lin6b2b28f2023-11-03 04:24:57 +0000500 const bool useSphalNamespace = !isSuffixAngle || AngleInVendor;
Peiyong Line4da7a82023-08-08 20:46:53 +0000501
502 const std::string absolutePath =
503 findLibrary(libraryName, useSphalNamespace ? VENDOR_LIB_EGL_DIR : SYSTEM_LIB_PATH,
504 exact);
Mathias Agopian65421432017-03-08 11:49:05 -0800505 if (absolutePath.empty()) {
Mathias Agopian99381422013-04-23 20:52:29 +0200506 // this happens often, we don't want to log an error
Yi Kong48a6cd22018-07-18 10:07:09 -0700507 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700508 }
Peiyong Line4da7a82023-08-08 20:46:53 +0000509 const char* const driverAbsolutePath = absolutePath.c_str();
Mathias Agopiande586972009-05-28 17:39:03 -0700510
Peiyong Line4da7a82023-08-08 20:46:53 +0000511 // Currently the default driver is unlikely to be ANGLE on most devices,
512 // hence put this first.
513 if (useSphalNamespace) {
514 // Try to load drivers from the 'sphal' namespace, if it exist. Fall back to
515 // the original routine when the namespace does not exist.
516 // See /system/linkerconfig/contents/namespace for the configuration of the
517 // sphal namespace.
518 dso = do_android_load_sphal_library(driverAbsolutePath, RTLD_NOW | RTLD_LOCAL);
519 } else {
520 // Try to load drivers from the default namespace.
521 // See /system/linkerconfig/contents/namespace for the configuration of the
522 // default namespace.
523 dso = do_dlopen(driverAbsolutePath, RTLD_NOW | RTLD_LOCAL);
524 }
525
Yi Kong48a6cd22018-07-18 10:07:09 -0700526 if (dso == nullptr) {
Mathias Agopian8c173842009-09-20 16:01:02 -0700527 const char* err = dlerror();
Peiyong Line4da7a82023-08-08 20:46:53 +0000528 ALOGE("load_driver(%s): %s", driverAbsolutePath, err ? err : "unknown");
Yi Kong48a6cd22018-07-18 10:07:09 -0700529 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700530 }
531
Peiyong Line4da7a82023-08-08 20:46:53 +0000532 ALOGV("loaded %s", driverAbsolutePath);
Mathias Agopianbaca89c2009-08-20 19:09:34 -0700533
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800534 return dso;
535}
536
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600537static void* load_angle(const char* kind, android_namespace_t* ns) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600538 std::string name = std::string("lib") + kind + "_angle.so";
Peiyong Line4da7a82023-08-08 20:46:53 +0000539 void* so = nullptr;
Cody Northrop1f00e172018-04-02 11:23:31 -0600540
Peiyong Line4da7a82023-08-08 20:46:53 +0000541 if (android::GraphicsEnv::getInstance().shouldUseSystemAngle()) {
542 so = do_dlopen(name.c_str(), RTLD_NOW | RTLD_LOCAL);
543 } else {
544 const android_dlextinfo dlextinfo = {
545 .flags = ANDROID_DLEXT_USE_NAMESPACE,
546 .library_namespace = ns,
547 };
548 so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
549 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600550
551 if (so) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600552 return so;
553 } else {
554 ALOGE("dlopen_ext(\"%s\") failed: %s", name.c_str(), dlerror());
555 }
556
557 return nullptr;
558}
559
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800560static void* load_updated_driver(const char* kind, android_namespace_t* ns) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800561 ATRACE_CALL();
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800562 const android_dlextinfo dlextinfo = {
563 .flags = ANDROID_DLEXT_USE_NAMESPACE,
564 .library_namespace = ns,
565 };
566 void* so = nullptr;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800567 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
Michael Hoisie4e0f56b2020-04-30 18:40:55 -0400568 auto prop = base::GetProperty(key, "");
569 if (prop.empty()) {
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700570 continue;
571 }
572 std::string name = std::string("lib") + kind + "_" + prop + ".so";
573 so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
574 if (so) {
575 return so;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800576 }
Yiwei Zhang9058b8f2020-11-12 20:23:00 +0000577 ALOGE("Could not load %s from updatable gfx driver namespace: %s.", name.c_str(),
578 dlerror());
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800579 }
580 return nullptr;
581}
582
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700583Loader::driver_t* Loader::attempt_to_load_angle(egl_connection_t* cnx) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800584 ATRACE_CALL();
Yiwei Zhangb22f0862020-04-23 10:40:24 -0700585
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600586 android_namespace_t* ns = android::GraphicsEnv::getInstance().getAngleNamespace();
Peiyong Line4da7a82023-08-08 20:46:53 +0000587 // ANGLE namespace is used for loading ANGLE from apk, and hence if namespace is not
588 // constructed, it means ANGLE apk is not set to be the OpenGL ES driver.
589 // Hence skip if ANGLE apk and system ANGLE are not set to be the OpenGL ES driver.
590 if (!ns && !android::GraphicsEnv::getInstance().shouldUseSystemAngle()) {
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700591 return nullptr;
Cody Northrop6d082ef2018-09-11 09:42:31 -0600592 }
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700593
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700594 driver_t* hnd = nullptr;
595
596 // ANGLE doesn't ship with GLES library, and thus we skip GLES driver.
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600597 void* dso = load_angle("EGL", ns);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700598 if (dso) {
599 initialize_api(dso, cnx, EGL);
600 hnd = new driver_t(dso);
601
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600602 dso = load_angle("GLESv1_CM", ns);
Charlie Lao840bd532020-01-16 17:34:37 -0800603 initialize_api(dso, cnx, GLESv1_CM);
604 hnd->set(dso, GLESv1_CM);
605
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600606 dso = load_angle("GLESv2", ns);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700607 initialize_api(dso, cnx, GLESv2);
608 hnd->set(dso, GLESv2);
609 }
610 return hnd;
611}
612
Peiyong Lin2f51e752023-06-15 22:35:14 +0000613void Loader::attempt_to_init_angle_backend(void* dso, egl_connection_t* cnx) {
Courtney Goeltzenleuchter8ecb10c2020-04-29 13:16:23 -0600614 void* pANGLEGetDisplayPlatform = dlsym(dso, "ANGLEGetDisplayPlatform");
615 if (pANGLEGetDisplayPlatform) {
Peiyong Lin2f51e752023-06-15 22:35:14 +0000616 ALOGV("ANGLE GLES library loaded");
617 cnx->angleLoaded = true;
Peiyong Line4da7a82023-08-08 20:46:53 +0000618 android::GraphicsEnv::getInstance().setDriverToLoad(android::GpuStatsInfo::Driver::ANGLE);
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600619 } else {
Peiyong Lin2f51e752023-06-15 22:35:14 +0000620 ALOGV("Native GLES library loaded");
621 cnx->angleLoaded = false;
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600622 }
623}
624
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700625Loader::driver_t* Loader::attempt_to_load_updated_driver(egl_connection_t* cnx) {
626 ATRACE_CALL();
Yiwei Zhangd40aaac2020-06-04 05:26:56 -0700627
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700628 android_namespace_t* ns = android::GraphicsEnv::getInstance().getDriverNamespace();
629 if (!ns) {
630 return nullptr;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800631 }
632
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700633 ALOGD("Load updated gl driver.");
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700634 android::GraphicsEnv::getInstance().setDriverToLoad(android::GpuStatsInfo::Driver::GL_UPDATED);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700635 driver_t* hnd = nullptr;
636 void* dso = load_updated_driver("GLES", ns);
Peiyong Line83b8682019-04-18 17:23:02 -0700637 if (dso) {
Charlie Lao840bd532020-01-16 17:34:37 -0800638 initialize_api(dso, cnx, EGL | GLESv1_CM | GLESv2);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700639 hnd = new driver_t(dso);
640 return hnd;
Peiyong Line83b8682019-04-18 17:23:02 -0700641 }
642
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700643 dso = load_updated_driver("EGL", ns);
644 if (dso) {
645 initialize_api(dso, cnx, EGL);
646 hnd = new driver_t(dso);
647
Charlie Lao840bd532020-01-16 17:34:37 -0800648 dso = load_updated_driver("GLESv1_CM", ns);
649 initialize_api(dso, cnx, GLESv1_CM);
650 hnd->set(dso, GLESv1_CM);
651
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700652 dso = load_updated_driver("GLESv2", ns);
653 initialize_api(dso, cnx, GLESv2);
654 hnd->set(dso, GLESv2);
655 }
656 return hnd;
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700657}
658
Peiyong Lin0d10b252019-04-30 18:03:04 -0700659Loader::driver_t* Loader::attempt_to_load_system_driver(egl_connection_t* cnx, const char* suffix,
660 const bool exact) {
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700661 ATRACE_CALL();
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700662 android::GraphicsEnv::getInstance().setDriverToLoad(android::GpuStatsInfo::Driver::GL);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700663 driver_t* hnd = nullptr;
Peiyong Lin0d10b252019-04-30 18:03:04 -0700664 void* dso = load_system_driver("GLES", suffix, exact);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700665 if (dso) {
Charlie Lao840bd532020-01-16 17:34:37 -0800666 initialize_api(dso, cnx, EGL | GLESv1_CM | GLESv2);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700667 hnd = new driver_t(dso);
668 return hnd;
669 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700670 dso = load_system_driver("EGL", suffix, exact);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700671 if (dso) {
672 initialize_api(dso, cnx, EGL);
673 hnd = new driver_t(dso);
674
Charlie Lao840bd532020-01-16 17:34:37 -0800675 dso = load_system_driver("GLESv1_CM", suffix, exact);
676 initialize_api(dso, cnx, GLESv1_CM);
677 hnd->set(dso, GLESv1_CM);
678
Peiyong Lin0d10b252019-04-30 18:03:04 -0700679 dso = load_system_driver("GLESv2", suffix, exact);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700680 initialize_api(dso, cnx, GLESv2);
681 hnd->set(dso, GLESv2);
682 }
683 return hnd;
Peiyong Line83b8682019-04-18 17:23:02 -0700684}
685
686void Loader::initialize_api(void* dso, egl_connection_t* cnx, uint32_t mask) {
Mathias Agopiande586972009-05-28 17:39:03 -0700687 if (mask & EGL) {
688 getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress");
689
Jesse Hall94cdba92013-07-11 09:40:35 -0700690 ALOGE_IF(!getProcAddress,
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800691 "can't find eglGetProcAddress() in EGL driver library");
Mathias Agopiande586972009-05-28 17:39:03 -0700692
Mathias Agopian618fa102009-10-14 02:06:37 -0700693 egl_t* egl = &cnx->egl;
Mathias Agopiande586972009-05-28 17:39:03 -0700694 __eglMustCastToProperFunctionPointerType* curr =
695 (__eglMustCastToProperFunctionPointerType*)egl;
696 char const * const * api = egl_names;
697 while (*api) {
698 char const * name = *api;
Jesse Hall94cdba92013-07-11 09:40:35 -0700699 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700700 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700701 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700702 // couldn't find the entry-point, use eglGetProcAddress()
703 f = getProcAddress(name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700704 if (f == nullptr) {
705 f = (__eglMustCastToProperFunctionPointerType)nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700706 }
707 }
708 *curr++ = f;
709 api++;
710 }
711 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700712
Mathias Agopiande586972009-05-28 17:39:03 -0700713 if (mask & GLESv1_CM) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700714 init_api(dso, gl_names_1, gl_names,
Mathias Agopian618fa102009-10-14 02:06:37 -0700715 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800716 &cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl,
Mathias Agopian618fa102009-10-14 02:06:37 -0700717 getProcAddress);
Mathias Agopiande586972009-05-28 17:39:03 -0700718 }
719
720 if (mask & GLESv2) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700721 init_api(dso, gl_names, nullptr,
Mathias Agopian618fa102009-10-14 02:06:37 -0700722 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800723 &cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl,
Mathias Agopiande586972009-05-28 17:39:03 -0700724 getProcAddress);
725 }
Mathias Agopiande586972009-05-28 17:39:03 -0700726}
727
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700728} // namespace android