blob: 04e2fffaef1cc4e3edeb6278be25f13f4c682338 [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";
69
70static const char* HAL_SUBNAME_KEY_PROPERTIES[3] = {
71 PERSIST_DRIVER_SUFFIX_PROPERTY,
72 RO_DRIVER_SUFFIX_PROPERTY,
73 RO_BOARD_PLATFORM_PROPERTY,
74};
75
76static const char* const VENDOR_LIB_EGL_DIR =
77#if defined(__LP64__)
78 "/vendor/lib64/egl";
79#else
80 "/vendor/lib/egl";
81#endif
Mathias Agopiande586972009-05-28 17:39:03 -070082
Jesse Hall1508ae62017-01-19 17:43:26 -080083static void* do_dlopen(const char* path, int mode) {
84 ATRACE_CALL();
85 return dlopen(path, mode);
86}
87
Jiyong Park5910dc72017-04-05 14:23:52 +090088static void* do_android_dlopen_ext(const char* path, int mode, const android_dlextinfo* info) {
89 ATRACE_CALL();
90 return android_dlopen_ext(path, mode, info);
91}
92
Justin Yunb7320302017-05-22 15:13:40 +090093static void* do_android_load_sphal_library(const char* path, int mode) {
94 ATRACE_CALL();
95 return android_load_sphal_library(path, mode);
96}
97
Yiwei Zhang5e21eb32019-06-05 00:26:03 -070098static int do_android_unload_sphal_library(void* dso) {
99 ATRACE_CALL();
100 return android_unload_sphal_library(dso);
101}
102
Peiyong Lind30aaad2023-08-03 19:12:49 +0000103static void* load_wrapper(const char* path) {
104 void* so = do_dlopen(path, RTLD_NOW | RTLD_LOCAL);
105 ALOGE_IF(!so, "dlopen(\"%s\") failed: %s", path, dlerror());
106 return so;
107}
108
109Loader& Loader::getInstance() {
110 static Loader loader;
111 return loader;
112}
113
Jesse Hall94cdba92013-07-11 09:40:35 -0700114Loader::driver_t::driver_t(void* gles)
Mathias Agopiande586972009-05-28 17:39:03 -0700115{
116 dso[0] = gles;
117 for (size_t i=1 ; i<NELEM(dso) ; i++)
Yi Kong48a6cd22018-07-18 10:07:09 -0700118 dso[i] = nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700119}
120
Jesse Hall94cdba92013-07-11 09:40:35 -0700121Loader::driver_t::~driver_t()
Mathias Agopiande586972009-05-28 17:39:03 -0700122{
123 for (size_t i=0 ; i<NELEM(dso) ; i++) {
124 if (dso[i]) {
125 dlclose(dso[i]);
Yi Kong48a6cd22018-07-18 10:07:09 -0700126 dso[i] = nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700127 }
128 }
129}
130
Mathias Agopian65421432017-03-08 11:49:05 -0800131int Loader::driver_t::set(void* hnd, int32_t api)
Mathias Agopiande586972009-05-28 17:39:03 -0700132{
133 switch (api) {
134 case EGL:
135 dso[0] = hnd;
136 break;
137 case GLESv1_CM:
138 dso[1] = hnd;
139 break;
140 case GLESv2:
141 dso[2] = hnd;
142 break;
143 default:
Mathias Agopian65421432017-03-08 11:49:05 -0800144 return -EOVERFLOW;
Mathias Agopiande586972009-05-28 17:39:03 -0700145 }
Mathias Agopian65421432017-03-08 11:49:05 -0800146 return 0;
Mathias Agopiande586972009-05-28 17:39:03 -0700147}
148
Mathias Agopiande586972009-05-28 17:39:03 -0700149Loader::Loader()
Yi Kong48a6cd22018-07-18 10:07:09 -0700150 : getProcAddress(nullptr)
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800151{
Mathias Agopiande586972009-05-28 17:39:03 -0700152}
153
Mathias Agopian99381422013-04-23 20:52:29 +0200154Loader::~Loader() {
Mathias Agopiande586972009-05-28 17:39:03 -0700155}
156
Peiyong Lin2f51e752023-06-15 22:35:14 +0000157// Check whether the loaded system drivers should be unloaded in order to
158// load ANGLE or the updatable graphics drivers.
159// If ANGLE namespace is set, it means the application is identified to run on top of ANGLE.
160// If updatable graphics driver namespace is set, it means the application is identified to
161// run on top of updatable graphics drivers.
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700162static bool should_unload_system_driver(egl_connection_t* cnx) {
163 // Return false if the system driver has been unloaded once.
164 if (cnx->systemDriverUnloaded) {
165 return false;
166 }
167
Peiyong Lin2f51e752023-06-15 22:35:14 +0000168 // Return true if ANGLE namespace is set.
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700169 android_namespace_t* ns = android::GraphicsEnv::getInstance().getAngleNamespace();
170 if (ns) {
Peiyong Line88e0ba2023-06-28 15:09:09 +0000171 // Unless the default GLES driver is ANGLE and the process should use system ANGLE, since
172 // the intended GLES driver is already loaded.
173 // This should be updated in a later patch that cleans up namespaces
174 if (!(cnx->angleLoaded && android::GraphicsEnv::getInstance().shouldUseSystemAngle())) {
175 return true;
176 }
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700177 }
178
Peiyong Lin2df5a002023-08-01 23:31:34 +0000179 // Return true if native GLES drivers should be used and ANGLE is already loaded.
180 if (android::GraphicsEnv::getInstance().shouldUseNativeDriver() && cnx->angleLoaded) {
181 return true;
182 }
183
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700184 // Return true if updated driver namespace is set.
185 ns = android::GraphicsEnv::getInstance().getDriverNamespace();
186 if (ns) {
187 return true;
188 }
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700189
190 return false;
191}
192
193static void uninit_api(char const* const* api, __eglMustCastToProperFunctionPointerType* curr) {
194 while (*api) {
195 *curr++ = nullptr;
196 api++;
197 }
198}
199
200void Loader::unload_system_driver(egl_connection_t* cnx) {
201 ATRACE_CALL();
202
203 uninit_api(gl_names,
204 (__eglMustCastToProperFunctionPointerType*)&cnx
205 ->hooks[egl_connection_t::GLESv2_INDEX]
206 ->gl);
207 uninit_api(gl_names,
208 (__eglMustCastToProperFunctionPointerType*)&cnx
209 ->hooks[egl_connection_t::GLESv1_INDEX]
210 ->gl);
211 uninit_api(egl_names, (__eglMustCastToProperFunctionPointerType*)&cnx->egl);
212
213 if (cnx->dso) {
214 ALOGD("Unload system gl driver.");
215 driver_t* hnd = (driver_t*)cnx->dso;
216 if (hnd->dso[2]) {
217 do_android_unload_sphal_library(hnd->dso[2]);
218 }
219 if (hnd->dso[1]) {
220 do_android_unload_sphal_library(hnd->dso[1]);
221 }
222 if (hnd->dso[0]) {
223 do_android_unload_sphal_library(hnd->dso[0]);
224 }
225 cnx->dso = nullptr;
Peiyong Line88e0ba2023-06-28 15:09:09 +0000226 cnx->angleLoaded = false;
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700227 }
228
229 cnx->systemDriverUnloaded = true;
230}
231
Peiyong Line88e0ba2023-06-28 15:09:09 +0000232void* Loader::open(egl_connection_t* cnx) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800233 ATRACE_CALL();
Yiwei Zhangd9861812019-02-13 11:51:55 -0800234 const nsecs_t openTime = systemTime();
Jesse Hall1508ae62017-01-19 17:43:26 -0800235
Peiyong Line88e0ba2023-06-28 15:09:09 +0000236 if (cnx->dso && should_unload_system_driver(cnx)) {
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700237 unload_system_driver(cnx);
238 }
239
240 // If a driver has been loaded, return the driver directly.
241 if (cnx->dso) {
242 return cnx->dso;
243 }
244
Peiyong Line88e0ba2023-06-28 15:09:09 +0000245 driver_t* hnd = nullptr;
246 // Firstly, try to load ANGLE driver, if ANGLE should be loaded and fail, abort.
247 if (android::GraphicsEnv::getInstance().shouldUseAngle()) {
248 hnd = attempt_to_load_angle(cnx);
249 LOG_ALWAYS_FATAL_IF(!hnd, "Failed to load ANGLE.");
250 }
Ian Elliott5279f862022-04-21 12:41:03 -0600251
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700252 if (!hnd) {
253 // Secondly, try to load from driver apk.
254 hnd = attempt_to_load_updated_driver(cnx);
Peiyong Lin2df5a002023-08-01 23:31:34 +0000255
256 // If updated driver apk is set but fail to load, abort here.
257 LOG_ALWAYS_FATAL_IF(android::GraphicsEnv::getInstance().getDriverNamespace(),
258 "couldn't find an OpenGL ES implementation from %s",
259 android::GraphicsEnv::getInstance().getDriverPath().c_str());
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700260 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700261
Peiyong Lin2df5a002023-08-01 23:31:34 +0000262 // Attempt to load native GLES drivers specified by ro.hardware.egl if native is selected.
263 // If native is selected but fail to load, abort.
264 if (!hnd && android::GraphicsEnv::getInstance().shouldUseNativeDriver()) {
265 auto driverSuffix = base::GetProperty(RO_DRIVER_SUFFIX_PROPERTY, "");
266 LOG_ALWAYS_FATAL_IF(driverSuffix.empty(),
267 "Native GLES driver is selected but not specified in %s",
268 RO_DRIVER_SUFFIX_PROPERTY);
269 hnd = attempt_to_load_system_driver(cnx, driverSuffix.c_str(), true);
270 LOG_ALWAYS_FATAL_IF(!hnd, "Native GLES driver is selected but failed to load. %s=%s",
271 RO_DRIVER_SUFFIX_PROPERTY, driverSuffix.c_str());
272 }
273
274 // Finally, try to load default driver.
Peiyong Lin0d10b252019-04-30 18:03:04 -0700275 bool failToLoadFromDriverSuffixProperty = false;
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700276 if (!hnd) {
Peiyong Lin98db8e02023-04-05 20:09:16 +0000277 // Start by searching for the library name appended by the system
278 // properties of the GLES userspace driver in both locations.
279 // i.e.:
280 // libGLES_${prop}.so, or:
281 // libEGL_${prop}.so, libGLESv1_CM_${prop}.so, libGLESv2_${prop}.so
282 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
283 auto prop = base::GetProperty(key, "");
284 if (prop.empty()) {
285 continue;
286 }
287 hnd = attempt_to_load_system_driver(cnx, prop.c_str(), true);
Peiyong Linf7878b72023-06-13 20:51:03 +0000288 if (!hnd) {
289 ALOGD("Failed to load drivers from property %s with value %s", key, prop.c_str());
Peiyong Lin98db8e02023-04-05 20:09:16 +0000290 failToLoadFromDriverSuffixProperty = true;
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700291 }
Peiyong Linf7878b72023-06-13 20:51:03 +0000292
293 // Abort regardless of whether subsequent properties are set, the value must be set
294 // correctly with the first property that has a value.
295 break;
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700296 }
297 }
298
299 if (!hnd) {
Peiyong Linf7878b72023-06-13 20:51:03 +0000300 // Can't find graphics driver by appending the value from system properties, now search for
301 // the exact name without any suffix of the GLES userspace driver in both locations.
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700302 // i.e.:
303 // libGLES.so, or:
304 // libEGL.so, libGLESv1_CM.so, libGLESv2.so
Charlie Lao840bd532020-01-16 17:34:37 -0800305 hnd = attempt_to_load_system_driver(cnx, nullptr, true);
Peiyong Lin0d10b252019-04-30 18:03:04 -0700306 }
307
Peiyong Lin98db8e02023-04-05 20:09:16 +0000308 if (!hnd && !failToLoadFromDriverSuffixProperty &&
309 property_get_int32("ro.vendor.api_level", 0) < __ANDROID_API_U__) {
310 // Still can't find the graphics drivers with the exact name. This time try to use wildcard
311 // matching if the device is launched before Android 14.
Charlie Lao840bd532020-01-16 17:34:37 -0800312 hnd = attempt_to_load_system_driver(cnx, nullptr, false);
Mathias Agopiande586972009-05-28 17:39:03 -0700313 }
314
Yiwei Zhangd9861812019-02-13 11:51:55 -0800315 if (!hnd) {
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700316 android::GraphicsEnv::getInstance().setDriverLoaded(android::GpuStatsInfo::Api::API_GL,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800317 false, systemTime() - openTime);
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600318 } else {
319 // init_angle_backend will check if loaded driver is ANGLE or not,
Peiyong Lin2f51e752023-06-15 22:35:14 +0000320 // will set cnx->angleLoaded appropriately.
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600321 // Do this here so that we use ANGLE path when driver is ANGLE (e.g. loaded as native),
322 // not just loading ANGLE as option.
Peiyong Lin2f51e752023-06-15 22:35:14 +0000323 attempt_to_init_angle_backend(hnd->dso[2], cnx);
Yiwei Zhangd9861812019-02-13 11:51:55 -0800324 }
325
Peiyong Lin3575b9f2019-04-25 14:26:49 -0700326 LOG_ALWAYS_FATAL_IF(!hnd,
Peiyong Lin9679a982023-04-10 22:00:30 +0000327 "couldn't find an OpenGL ES implementation, make sure one of %s, %s and %s "
328 "is set",
329 HAL_SUBNAME_KEY_PROPERTIES[0], HAL_SUBNAME_KEY_PROPERTIES[1],
330 HAL_SUBNAME_KEY_PROPERTIES[2]);
Jesse Hallc07b5202013-07-04 12:08:16 -0700331
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700332 if (!cnx->libEgl) {
Peiyong Lind30aaad2023-08-03 19:12:49 +0000333 cnx->libEgl = load_wrapper(SYSTEM_LIB_PATH "/libEGL.so");
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700334 }
Charlie Lao840bd532020-01-16 17:34:37 -0800335 if (!cnx->libGles1) {
Peiyong Lind30aaad2023-08-03 19:12:49 +0000336 cnx->libGles1 = load_wrapper(SYSTEM_LIB_PATH "/libGLESv1_CM.so");
Charlie Lao840bd532020-01-16 17:34:37 -0800337 }
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700338 if (!cnx->libGles2) {
Peiyong Lind30aaad2023-08-03 19:12:49 +0000339 cnx->libGles2 = load_wrapper(SYSTEM_LIB_PATH "/libGLESv2.so");
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700340 }
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700341
Charlie Lao840bd532020-01-16 17:34:37 -0800342 if (!cnx->libEgl || !cnx->libGles2 || !cnx->libGles1) {
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700343 android::GraphicsEnv::getInstance().setDriverLoaded(android::GpuStatsInfo::Api::API_GL,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800344 false, systemTime() - openTime);
345 }
346
Michael Chockc0ec5e22014-01-27 08:14:33 -0800347 LOG_ALWAYS_FATAL_IF(!cnx->libEgl,
348 "couldn't load system EGL wrapper libraries");
349
Charlie Lao840bd532020-01-16 17:34:37 -0800350 LOG_ALWAYS_FATAL_IF(!cnx->libGles2 || !cnx->libGles1,
351 "couldn't load system OpenGL ES wrapper libraries");
Jesse Hallc07b5202013-07-04 12:08:16 -0700352
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700353 android::GraphicsEnv::getInstance().setDriverLoaded(android::GpuStatsInfo::Api::API_GL, true,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800354 systemTime() - openTime);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800355
Mathias Agopiande586972009-05-28 17:39:03 -0700356 return (void*)hnd;
357}
358
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600359void Loader::close(egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700360{
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600361 driver_t* hnd = (driver_t*) cnx->dso;
Mathias Agopiande586972009-05-28 17:39:03 -0700362 delete hnd;
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600363 cnx->dso = nullptr;
364
Peiyong Lin2f51e752023-06-15 22:35:14 +0000365 cnx->angleLoaded = false;
Mathias Agopiande586972009-05-28 17:39:03 -0700366}
367
Jesse Hall94cdba92013-07-11 09:40:35 -0700368void Loader::init_api(void* dso,
369 char const * const * api,
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700370 char const * const * ref_api,
Jesse Hall94cdba92013-07-11 09:40:35 -0700371 __eglMustCastToProperFunctionPointerType* curr,
372 getProcAddressType getProcAddress)
Mathias Agopiande586972009-05-28 17:39:03 -0700373{
Jesse Hall1508ae62017-01-19 17:43:26 -0800374 ATRACE_CALL();
375
Mathias Agopian7773c432012-02-13 20:06:08 -0800376 const ssize_t SIZE = 256;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700377 char scrap[SIZE];
Mathias Agopiande586972009-05-28 17:39:03 -0700378 while (*api) {
379 char const * name = *api;
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700380 if (ref_api) {
381 char const * ref_name = *ref_api;
382 if (std::strcmp(name, ref_name) != 0) {
383 *curr++ = nullptr;
384 ref_api++;
385 continue;
386 }
387 }
388
Jesse Hall94cdba92013-07-11 09:40:35 -0700389 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700390 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700391 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700392 // couldn't find the entry-point, use eglGetProcAddress()
393 f = getProcAddress(name);
394 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700395 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700396 // Try without the OES postfix
397 ssize_t index = ssize_t(strlen(name)) - 3;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700398 if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) {
Mathias Agopiande586972009-05-28 17:39:03 -0700399 strncpy(scrap, name, index);
400 scrap[index] = 0;
401 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000402 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700403 }
404 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700405 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700406 // Try with the OES postfix
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700407 ssize_t index = ssize_t(strlen(name)) - 3;
408 if (index>0 && strcmp(name+index, "OES")) {
409 snprintf(scrap, SIZE, "%sOES", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700410 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) {
Steve Block9d453682011-12-20 16:23:08 +0000415 //ALOGD("%s", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700416 f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented;
Mathias Agopian48d438d2012-01-28 21:44:00 -0800417
418 /*
Tao Wu1cdfe642020-06-23 12:33:02 -0700419 * GL_EXT_debug_marker is special, we always report it as
Mathias Agopian48d438d2012-01-28 21:44:00 -0800420 * supported, it's handled by GLES_trace. If GLES_trace is not
421 * enabled, then these are no-ops.
422 */
423 if (!strcmp(name, "glInsertEventMarkerEXT")) {
424 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
425 } else if (!strcmp(name, "glPushGroupMarkerEXT")) {
426 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
427 } else if (!strcmp(name, "glPopGroupMarkerEXT")) {
428 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
429 }
Mathias Agopiande586972009-05-28 17:39:03 -0700430 }
431 *curr++ = f;
432 api++;
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700433 if (ref_api) ref_api++;
Mathias Agopiande586972009-05-28 17:39:03 -0700434 }
435}
436
Peiyong Lin0d10b252019-04-30 18:03:04 -0700437static void* load_system_driver(const char* kind, const char* suffix, const bool exact) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800438 ATRACE_CALL();
Mathias Agopian99381422013-04-23 20:52:29 +0200439 class MatchFile {
440 public:
Peiyong Lin0d10b252019-04-30 18:03:04 -0700441 static std::string find(const char* libraryName, const bool exact) {
Peiyong Lind30aaad2023-08-03 19:12:49 +0000442 std::string absolutePath;
443 if (findLibPath(absolutePath, libraryName, exact)) {
444 return absolutePath;
Mathias Agopian99381422013-04-23 20:52:29 +0200445 }
446
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700447 // Driver not found. gah.
448 return std::string();
Mathias Agopian99381422013-04-23 20:52:29 +0200449 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700450 private:
Peiyong Lind30aaad2023-08-03 19:12:49 +0000451 static bool findLibPath(std::string& result, const std::string& pattern, bool exact) {
452 const std::string vendorLibEglDirString = std::string(VENDOR_LIB_EGL_DIR);
Peiyong Lin0d10b252019-04-30 18:03:04 -0700453 if (exact) {
Peiyong Lind30aaad2023-08-03 19:12:49 +0000454 std::string absolutePath = vendorLibEglDirString + "/" + pattern + ".so";
Peiyong Lin0d10b252019-04-30 18:03:04 -0700455 if (!access(absolutePath.c_str(), R_OK)) {
456 result = absolutePath;
457 return true;
458 }
459 return false;
460 }
461
Peiyong Lind30aaad2023-08-03 19:12:49 +0000462 DIR* d = opendir(VENDOR_LIB_EGL_DIR);
Peiyong Lin0d10b252019-04-30 18:03:04 -0700463 if (d != nullptr) {
464 struct dirent* e;
465 while ((e = readdir(d)) != nullptr) {
466 if (e->d_type == DT_DIR) {
467 continue;
468 }
469 if (!strcmp(e->d_name, "libGLES_android.so")) {
470 // always skip the software renderer
471 continue;
472 }
473 if (strstr(e->d_name, pattern.c_str()) == e->d_name) {
474 if (!strcmp(e->d_name + strlen(e->d_name) - 3, ".so")) {
Peiyong Lind30aaad2023-08-03 19:12:49 +0000475 result = vendorLibEglDirString + "/" + e->d_name;
Peiyong Lin0d10b252019-04-30 18:03:04 -0700476 closedir(d);
477 return true;
478 }
479 }
480 }
481 closedir(d);
482 }
483 return false;
484 }
Mathias Agopian99381422013-04-23 20:52:29 +0200485 };
486
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700487 std::string libraryName = std::string("lib") + kind;
488 if (suffix) {
489 libraryName += std::string("_") + suffix;
Peiyong Lin0d10b252019-04-30 18:03:04 -0700490 } else if (!exact) {
491 // Deprecated: we look for files that match
492 // libGLES_*.so, or:
493 // libEGL_*.so, libGLESv1_CM_*.so, libGLESv2_*.so
494 libraryName += std::string("_");
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700495 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700496 std::string absolutePath = MatchFile::find(libraryName.c_str(), exact);
Mathias Agopian65421432017-03-08 11:49:05 -0800497 if (absolutePath.empty()) {
Mathias Agopian99381422013-04-23 20:52:29 +0200498 // this happens often, we don't want to log an error
Yi Kong48a6cd22018-07-18 10:07:09 -0700499 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700500 }
Mathias Agopian65421432017-03-08 11:49:05 -0800501 const char* const driver_absolute_path = absolutePath.c_str();
Mathias Agopiande586972009-05-28 17:39:03 -0700502
Jiyong Park5910dc72017-04-05 14:23:52 +0900503 // Try to load drivers from the 'sphal' namespace, if it exist. Fall back to
Justin Yunb7320302017-05-22 15:13:40 +0900504 // the original routine when the namespace does not exist.
Jiyong Park5910dc72017-04-05 14:23:52 +0900505 // See /system/core/rootdir/etc/ld.config.txt for the configuration of the
506 // sphal namespace.
Justin Yunb7320302017-05-22 15:13:40 +0900507 void* dso = do_android_load_sphal_library(driver_absolute_path,
508 RTLD_NOW | RTLD_LOCAL);
Yi Kong48a6cd22018-07-18 10:07:09 -0700509 if (dso == nullptr) {
Mathias Agopian8c173842009-09-20 16:01:02 -0700510 const char* err = dlerror();
Mathias Agopian65421432017-03-08 11:49:05 -0800511 ALOGE("load_driver(%s): %s", driver_absolute_path, err ? err : "unknown");
Yi Kong48a6cd22018-07-18 10:07:09 -0700512 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700513 }
514
Steve Block9d453682011-12-20 16:23:08 +0000515 ALOGD("loaded %s", driver_absolute_path);
Mathias Agopianbaca89c2009-08-20 19:09:34 -0700516
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800517 return dso;
518}
519
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600520static void* load_angle(const char* kind, android_namespace_t* ns) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600521 const android_dlextinfo dlextinfo = {
522 .flags = ANDROID_DLEXT_USE_NAMESPACE,
523 .library_namespace = ns,
524 };
525
526 std::string name = std::string("lib") + kind + "_angle.so";
527
528 void* so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
529
530 if (so) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600531 return so;
532 } else {
533 ALOGE("dlopen_ext(\"%s\") failed: %s", name.c_str(), dlerror());
534 }
535
536 return nullptr;
537}
538
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800539static void* load_updated_driver(const char* kind, android_namespace_t* ns) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800540 ATRACE_CALL();
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800541 const android_dlextinfo dlextinfo = {
542 .flags = ANDROID_DLEXT_USE_NAMESPACE,
543 .library_namespace = ns,
544 };
545 void* so = nullptr;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800546 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
Michael Hoisie4e0f56b2020-04-30 18:40:55 -0400547 auto prop = base::GetProperty(key, "");
548 if (prop.empty()) {
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700549 continue;
550 }
551 std::string name = std::string("lib") + kind + "_" + prop + ".so";
552 so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
553 if (so) {
554 return so;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800555 }
Yiwei Zhang9058b8f2020-11-12 20:23:00 +0000556 ALOGE("Could not load %s from updatable gfx driver namespace: %s.", name.c_str(),
557 dlerror());
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800558 }
559 return nullptr;
560}
561
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700562Loader::driver_t* Loader::attempt_to_load_angle(egl_connection_t* cnx) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800563 ATRACE_CALL();
Yiwei Zhangb22f0862020-04-23 10:40:24 -0700564
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600565 android_namespace_t* ns = android::GraphicsEnv::getInstance().getAngleNamespace();
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700566 if (!ns) {
567 return nullptr;
Cody Northrop6d082ef2018-09-11 09:42:31 -0600568 }
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700569
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700570 android::GraphicsEnv::getInstance().setDriverToLoad(android::GpuStatsInfo::Driver::ANGLE);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700571 driver_t* hnd = nullptr;
572
573 // ANGLE doesn't ship with GLES library, and thus we skip GLES driver.
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600574 void* dso = load_angle("EGL", ns);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700575 if (dso) {
576 initialize_api(dso, cnx, EGL);
577 hnd = new driver_t(dso);
578
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600579 dso = load_angle("GLESv1_CM", ns);
Charlie Lao840bd532020-01-16 17:34:37 -0800580 initialize_api(dso, cnx, GLESv1_CM);
581 hnd->set(dso, GLESv1_CM);
582
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600583 dso = load_angle("GLESv2", ns);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700584 initialize_api(dso, cnx, GLESv2);
585 hnd->set(dso, GLESv2);
586 }
587 return hnd;
588}
589
Peiyong Lin2f51e752023-06-15 22:35:14 +0000590void Loader::attempt_to_init_angle_backend(void* dso, egl_connection_t* cnx) {
Courtney Goeltzenleuchter8ecb10c2020-04-29 13:16:23 -0600591 void* pANGLEGetDisplayPlatform = dlsym(dso, "ANGLEGetDisplayPlatform");
592 if (pANGLEGetDisplayPlatform) {
Peiyong Lin2f51e752023-06-15 22:35:14 +0000593 ALOGV("ANGLE GLES library loaded");
594 cnx->angleLoaded = true;
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600595 } else {
Peiyong Lin2f51e752023-06-15 22:35:14 +0000596 ALOGV("Native GLES library loaded");
597 cnx->angleLoaded = false;
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600598 }
599}
600
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700601Loader::driver_t* Loader::attempt_to_load_updated_driver(egl_connection_t* cnx) {
602 ATRACE_CALL();
Yiwei Zhangd40aaac2020-06-04 05:26:56 -0700603
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700604 android_namespace_t* ns = android::GraphicsEnv::getInstance().getDriverNamespace();
605 if (!ns) {
606 return nullptr;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800607 }
608
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700609 ALOGD("Load updated gl driver.");
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700610 android::GraphicsEnv::getInstance().setDriverToLoad(android::GpuStatsInfo::Driver::GL_UPDATED);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700611 driver_t* hnd = nullptr;
612 void* dso = load_updated_driver("GLES", ns);
Peiyong Line83b8682019-04-18 17:23:02 -0700613 if (dso) {
Charlie Lao840bd532020-01-16 17:34:37 -0800614 initialize_api(dso, cnx, EGL | GLESv1_CM | GLESv2);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700615 hnd = new driver_t(dso);
616 return hnd;
Peiyong Line83b8682019-04-18 17:23:02 -0700617 }
618
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700619 dso = load_updated_driver("EGL", ns);
620 if (dso) {
621 initialize_api(dso, cnx, EGL);
622 hnd = new driver_t(dso);
623
Charlie Lao840bd532020-01-16 17:34:37 -0800624 dso = load_updated_driver("GLESv1_CM", ns);
625 initialize_api(dso, cnx, GLESv1_CM);
626 hnd->set(dso, GLESv1_CM);
627
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700628 dso = load_updated_driver("GLESv2", ns);
629 initialize_api(dso, cnx, GLESv2);
630 hnd->set(dso, GLESv2);
631 }
632 return hnd;
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700633}
634
Peiyong Lin0d10b252019-04-30 18:03:04 -0700635Loader::driver_t* Loader::attempt_to_load_system_driver(egl_connection_t* cnx, const char* suffix,
636 const bool exact) {
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700637 ATRACE_CALL();
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700638 android::GraphicsEnv::getInstance().setDriverToLoad(android::GpuStatsInfo::Driver::GL);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700639 driver_t* hnd = nullptr;
Peiyong Lin0d10b252019-04-30 18:03:04 -0700640 void* dso = load_system_driver("GLES", suffix, exact);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700641 if (dso) {
Charlie Lao840bd532020-01-16 17:34:37 -0800642 initialize_api(dso, cnx, EGL | GLESv1_CM | GLESv2);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700643 hnd = new driver_t(dso);
644 return hnd;
645 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700646 dso = load_system_driver("EGL", suffix, exact);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700647 if (dso) {
648 initialize_api(dso, cnx, EGL);
649 hnd = new driver_t(dso);
650
Charlie Lao840bd532020-01-16 17:34:37 -0800651 dso = load_system_driver("GLESv1_CM", suffix, exact);
652 initialize_api(dso, cnx, GLESv1_CM);
653 hnd->set(dso, GLESv1_CM);
654
Peiyong Lin0d10b252019-04-30 18:03:04 -0700655 dso = load_system_driver("GLESv2", suffix, exact);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700656 initialize_api(dso, cnx, GLESv2);
657 hnd->set(dso, GLESv2);
658 }
659 return hnd;
Peiyong Line83b8682019-04-18 17:23:02 -0700660}
661
662void Loader::initialize_api(void* dso, egl_connection_t* cnx, uint32_t mask) {
Mathias Agopiande586972009-05-28 17:39:03 -0700663 if (mask & EGL) {
664 getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress");
665
Jesse Hall94cdba92013-07-11 09:40:35 -0700666 ALOGE_IF(!getProcAddress,
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800667 "can't find eglGetProcAddress() in EGL driver library");
Mathias Agopiande586972009-05-28 17:39:03 -0700668
Mathias Agopian618fa102009-10-14 02:06:37 -0700669 egl_t* egl = &cnx->egl;
Mathias Agopiande586972009-05-28 17:39:03 -0700670 __eglMustCastToProperFunctionPointerType* curr =
671 (__eglMustCastToProperFunctionPointerType*)egl;
672 char const * const * api = egl_names;
673 while (*api) {
674 char const * name = *api;
Jesse Hall94cdba92013-07-11 09:40:35 -0700675 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700676 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700677 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700678 // couldn't find the entry-point, use eglGetProcAddress()
679 f = getProcAddress(name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700680 if (f == nullptr) {
681 f = (__eglMustCastToProperFunctionPointerType)nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700682 }
683 }
684 *curr++ = f;
685 api++;
686 }
687 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700688
Mathias Agopiande586972009-05-28 17:39:03 -0700689 if (mask & GLESv1_CM) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700690 init_api(dso, gl_names_1, gl_names,
Mathias Agopian618fa102009-10-14 02:06:37 -0700691 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800692 &cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl,
Mathias Agopian618fa102009-10-14 02:06:37 -0700693 getProcAddress);
Mathias Agopiande586972009-05-28 17:39:03 -0700694 }
695
696 if (mask & GLESv2) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700697 init_api(dso, gl_names, nullptr,
Mathias Agopian618fa102009-10-14 02:06:37 -0700698 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800699 &cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl,
Mathias Agopiande586972009-05-28 17:39:03 -0700700 getProcAddress);
701 }
Mathias Agopiande586972009-05-28 17:39:03 -0700702}
703
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700704} // namespace android