blob: 3297fe037ef2c6133dd734b03afbbf43d1d7db22 [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 Lind30aaad2023-08-03 19:12:49 +000070
71static const char* HAL_SUBNAME_KEY_PROPERTIES[3] = {
72 PERSIST_DRIVER_SUFFIX_PROPERTY,
73 RO_DRIVER_SUFFIX_PROPERTY,
74 RO_BOARD_PLATFORM_PROPERTY,
75};
76
77static const char* const VENDOR_LIB_EGL_DIR =
78#if defined(__LP64__)
79 "/vendor/lib64/egl";
80#else
81 "/vendor/lib/egl";
82#endif
Mathias Agopiande586972009-05-28 17:39:03 -070083
Peiyong Line4da7a82023-08-08 20:46:53 +000084static const char* const SYSTEM_LIB_DIR =
85#if defined(__LP64__)
86 "/system/lib64";
87#else
88 "/system/lib";
89#endif
90
Jesse Hall1508ae62017-01-19 17:43:26 -080091static void* do_dlopen(const char* path, int mode) {
92 ATRACE_CALL();
93 return dlopen(path, mode);
94}
95
Jiyong Park5910dc72017-04-05 14:23:52 +090096static void* do_android_dlopen_ext(const char* path, int mode, const android_dlextinfo* info) {
97 ATRACE_CALL();
98 return android_dlopen_ext(path, mode, info);
99}
100
Justin Yunb7320302017-05-22 15:13:40 +0900101static void* do_android_load_sphal_library(const char* path, int mode) {
102 ATRACE_CALL();
103 return android_load_sphal_library(path, mode);
104}
105
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700106static int do_android_unload_sphal_library(void* dso) {
107 ATRACE_CALL();
108 return android_unload_sphal_library(dso);
109}
110
Peiyong Lind30aaad2023-08-03 19:12:49 +0000111static void* load_wrapper(const char* path) {
112 void* so = do_dlopen(path, RTLD_NOW | RTLD_LOCAL);
113 ALOGE_IF(!so, "dlopen(\"%s\") failed: %s", path, dlerror());
114 return so;
115}
116
117Loader& Loader::getInstance() {
118 static Loader loader;
119 return loader;
120}
121
Jesse Hall94cdba92013-07-11 09:40:35 -0700122Loader::driver_t::driver_t(void* gles)
Mathias Agopiande586972009-05-28 17:39:03 -0700123{
124 dso[0] = gles;
125 for (size_t i=1 ; i<NELEM(dso) ; i++)
Yi Kong48a6cd22018-07-18 10:07:09 -0700126 dso[i] = nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700127}
128
Jesse Hall94cdba92013-07-11 09:40:35 -0700129Loader::driver_t::~driver_t()
Mathias Agopiande586972009-05-28 17:39:03 -0700130{
131 for (size_t i=0 ; i<NELEM(dso) ; i++) {
132 if (dso[i]) {
133 dlclose(dso[i]);
Yi Kong48a6cd22018-07-18 10:07:09 -0700134 dso[i] = nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700135 }
136 }
137}
138
Mathias Agopian65421432017-03-08 11:49:05 -0800139int Loader::driver_t::set(void* hnd, int32_t api)
Mathias Agopiande586972009-05-28 17:39:03 -0700140{
141 switch (api) {
142 case EGL:
143 dso[0] = hnd;
144 break;
145 case GLESv1_CM:
146 dso[1] = hnd;
147 break;
148 case GLESv2:
149 dso[2] = hnd;
150 break;
151 default:
Mathias Agopian65421432017-03-08 11:49:05 -0800152 return -EOVERFLOW;
Mathias Agopiande586972009-05-28 17:39:03 -0700153 }
Mathias Agopian65421432017-03-08 11:49:05 -0800154 return 0;
Mathias Agopiande586972009-05-28 17:39:03 -0700155}
156
Mathias Agopiande586972009-05-28 17:39:03 -0700157Loader::Loader()
Yi Kong48a6cd22018-07-18 10:07:09 -0700158 : getProcAddress(nullptr)
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800159{
Mathias Agopiande586972009-05-28 17:39:03 -0700160}
161
Mathias Agopian99381422013-04-23 20:52:29 +0200162Loader::~Loader() {
Mathias Agopiande586972009-05-28 17:39:03 -0700163}
164
Peiyong Lin2f51e752023-06-15 22:35:14 +0000165// Check whether the loaded system drivers should be unloaded in order to
166// load ANGLE or the updatable graphics drivers.
167// If ANGLE namespace is set, it means the application is identified to run on top of ANGLE.
168// If updatable graphics driver namespace is set, it means the application is identified to
169// run on top of updatable graphics drivers.
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700170static bool should_unload_system_driver(egl_connection_t* cnx) {
171 // Return false if the system driver has been unloaded once.
172 if (cnx->systemDriverUnloaded) {
173 return false;
174 }
175
Peiyong Lin2f51e752023-06-15 22:35:14 +0000176 // Return true if ANGLE namespace is set.
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700177 android_namespace_t* ns = android::GraphicsEnv::getInstance().getAngleNamespace();
178 if (ns) {
Peiyong Line88e0ba2023-06-28 15:09:09 +0000179 // Unless the default GLES driver is ANGLE and the process should use system ANGLE, since
180 // the intended GLES driver is already loaded.
181 // This should be updated in a later patch that cleans up namespaces
182 if (!(cnx->angleLoaded && android::GraphicsEnv::getInstance().shouldUseSystemAngle())) {
183 return true;
184 }
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700185 }
186
Peiyong Lin2df5a002023-08-01 23:31:34 +0000187 // Return true if native GLES drivers should be used and ANGLE is already loaded.
188 if (android::GraphicsEnv::getInstance().shouldUseNativeDriver() && cnx->angleLoaded) {
189 return true;
190 }
191
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700192 // Return true if updated driver namespace is set.
193 ns = android::GraphicsEnv::getInstance().getDriverNamespace();
194 if (ns) {
195 return true;
196 }
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700197
198 return false;
199}
200
201static void uninit_api(char const* const* api, __eglMustCastToProperFunctionPointerType* curr) {
202 while (*api) {
203 *curr++ = nullptr;
204 api++;
205 }
206}
207
208void Loader::unload_system_driver(egl_connection_t* cnx) {
209 ATRACE_CALL();
210
211 uninit_api(gl_names,
212 (__eglMustCastToProperFunctionPointerType*)&cnx
213 ->hooks[egl_connection_t::GLESv2_INDEX]
214 ->gl);
215 uninit_api(gl_names,
216 (__eglMustCastToProperFunctionPointerType*)&cnx
217 ->hooks[egl_connection_t::GLESv1_INDEX]
218 ->gl);
219 uninit_api(egl_names, (__eglMustCastToProperFunctionPointerType*)&cnx->egl);
220
221 if (cnx->dso) {
222 ALOGD("Unload system gl driver.");
223 driver_t* hnd = (driver_t*)cnx->dso;
224 if (hnd->dso[2]) {
225 do_android_unload_sphal_library(hnd->dso[2]);
226 }
227 if (hnd->dso[1]) {
228 do_android_unload_sphal_library(hnd->dso[1]);
229 }
230 if (hnd->dso[0]) {
231 do_android_unload_sphal_library(hnd->dso[0]);
232 }
233 cnx->dso = nullptr;
Peiyong Line88e0ba2023-06-28 15:09:09 +0000234 cnx->angleLoaded = false;
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700235 }
236
237 cnx->systemDriverUnloaded = true;
238}
239
Peiyong Line88e0ba2023-06-28 15:09:09 +0000240void* Loader::open(egl_connection_t* cnx) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800241 ATRACE_CALL();
Yiwei Zhangd9861812019-02-13 11:51:55 -0800242 const nsecs_t openTime = systemTime();
Jesse Hall1508ae62017-01-19 17:43:26 -0800243
Peiyong Line88e0ba2023-06-28 15:09:09 +0000244 if (cnx->dso && should_unload_system_driver(cnx)) {
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700245 unload_system_driver(cnx);
246 }
247
248 // If a driver has been loaded, return the driver directly.
249 if (cnx->dso) {
250 return cnx->dso;
251 }
252
Peiyong Line88e0ba2023-06-28 15:09:09 +0000253 driver_t* hnd = nullptr;
254 // Firstly, try to load ANGLE driver, if ANGLE should be loaded and fail, abort.
255 if (android::GraphicsEnv::getInstance().shouldUseAngle()) {
256 hnd = attempt_to_load_angle(cnx);
257 LOG_ALWAYS_FATAL_IF(!hnd, "Failed to load ANGLE.");
258 }
Ian Elliott5279f862022-04-21 12:41:03 -0600259
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700260 if (!hnd) {
261 // Secondly, try to load from driver apk.
262 hnd = attempt_to_load_updated_driver(cnx);
Peiyong Lin2df5a002023-08-01 23:31:34 +0000263
264 // If updated driver apk is set but fail to load, abort here.
Rob VanReenen45ccd132024-08-01 12:02:58 -0700265 LOG_ALWAYS_FATAL_IF(android::GraphicsEnv::getInstance().getDriverNamespace() && !hnd,
Peiyong Lin2df5a002023-08-01 23:31:34 +0000266 "couldn't find an OpenGL ES implementation from %s",
267 android::GraphicsEnv::getInstance().getDriverPath().c_str());
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700268 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700269
Peiyong Lin2df5a002023-08-01 23:31:34 +0000270 // Attempt to load native GLES drivers specified by ro.hardware.egl if native is selected.
271 // If native is selected but fail to load, abort.
272 if (!hnd && android::GraphicsEnv::getInstance().shouldUseNativeDriver()) {
273 auto driverSuffix = base::GetProperty(RO_DRIVER_SUFFIX_PROPERTY, "");
274 LOG_ALWAYS_FATAL_IF(driverSuffix.empty(),
275 "Native GLES driver is selected but not specified in %s",
276 RO_DRIVER_SUFFIX_PROPERTY);
277 hnd = attempt_to_load_system_driver(cnx, driverSuffix.c_str(), true);
278 LOG_ALWAYS_FATAL_IF(!hnd, "Native GLES driver is selected but failed to load. %s=%s",
279 RO_DRIVER_SUFFIX_PROPERTY, driverSuffix.c_str());
280 }
281
282 // Finally, try to load default driver.
Peiyong Lin0d10b252019-04-30 18:03:04 -0700283 bool failToLoadFromDriverSuffixProperty = false;
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700284 if (!hnd) {
Peiyong Lin98db8e02023-04-05 20:09:16 +0000285 // Start by searching for the library name appended by the system
286 // properties of the GLES userspace driver in both locations.
287 // i.e.:
288 // libGLES_${prop}.so, or:
289 // libEGL_${prop}.so, libGLESv1_CM_${prop}.so, libGLESv2_${prop}.so
290 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
291 auto prop = base::GetProperty(key, "");
292 if (prop.empty()) {
293 continue;
294 }
295 hnd = attempt_to_load_system_driver(cnx, prop.c_str(), true);
Peiyong Linf7878b72023-06-13 20:51:03 +0000296 if (!hnd) {
297 ALOGD("Failed to load drivers from property %s with value %s", key, prop.c_str());
Peiyong Lin98db8e02023-04-05 20:09:16 +0000298 failToLoadFromDriverSuffixProperty = true;
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700299 }
Peiyong Linf7878b72023-06-13 20:51:03 +0000300
301 // Abort regardless of whether subsequent properties are set, the value must be set
302 // correctly with the first property that has a value.
303 break;
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700304 }
305 }
306
307 if (!hnd) {
Peiyong Linf7878b72023-06-13 20:51:03 +0000308 // Can't find graphics driver by appending the value from system properties, now search for
309 // the exact name without any suffix of the GLES userspace driver in both locations.
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700310 // i.e.:
311 // libGLES.so, or:
312 // libEGL.so, libGLESv1_CM.so, libGLESv2.so
Charlie Lao840bd532020-01-16 17:34:37 -0800313 hnd = attempt_to_load_system_driver(cnx, nullptr, true);
Peiyong Lin0d10b252019-04-30 18:03:04 -0700314 }
315
Peiyong Lin98db8e02023-04-05 20:09:16 +0000316 if (!hnd && !failToLoadFromDriverSuffixProperty &&
317 property_get_int32("ro.vendor.api_level", 0) < __ANDROID_API_U__) {
318 // Still can't find the graphics drivers with the exact name. This time try to use wildcard
319 // matching if the device is launched before Android 14.
Charlie Lao840bd532020-01-16 17:34:37 -0800320 hnd = attempt_to_load_system_driver(cnx, nullptr, false);
Mathias Agopiande586972009-05-28 17:39:03 -0700321 }
322
Yiwei Zhangd9861812019-02-13 11:51:55 -0800323 if (!hnd) {
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700324 android::GraphicsEnv::getInstance().setDriverLoaded(android::GpuStatsInfo::Api::API_GL,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800325 false, systemTime() - openTime);
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600326 } else {
327 // init_angle_backend will check if loaded driver is ANGLE or not,
Peiyong Lin2f51e752023-06-15 22:35:14 +0000328 // will set cnx->angleLoaded appropriately.
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600329 // Do this here so that we use ANGLE path when driver is ANGLE (e.g. loaded as native),
330 // not just loading ANGLE as option.
Peiyong Lin2f51e752023-06-15 22:35:14 +0000331 attempt_to_init_angle_backend(hnd->dso[2], cnx);
Yiwei Zhangd9861812019-02-13 11:51:55 -0800332 }
333
Peiyong Lin3575b9f2019-04-25 14:26:49 -0700334 LOG_ALWAYS_FATAL_IF(!hnd,
Peiyong Lin9679a982023-04-10 22:00:30 +0000335 "couldn't find an OpenGL ES implementation, make sure one of %s, %s and %s "
336 "is set",
337 HAL_SUBNAME_KEY_PROPERTIES[0], HAL_SUBNAME_KEY_PROPERTIES[1],
338 HAL_SUBNAME_KEY_PROPERTIES[2]);
Jesse Hallc07b5202013-07-04 12:08:16 -0700339
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700340 if (!cnx->libEgl) {
Peiyong Lind30aaad2023-08-03 19:12:49 +0000341 cnx->libEgl = load_wrapper(SYSTEM_LIB_PATH "/libEGL.so");
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700342 }
Charlie Lao840bd532020-01-16 17:34:37 -0800343 if (!cnx->libGles1) {
Peiyong Lind30aaad2023-08-03 19:12:49 +0000344 cnx->libGles1 = load_wrapper(SYSTEM_LIB_PATH "/libGLESv1_CM.so");
Charlie Lao840bd532020-01-16 17:34:37 -0800345 }
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700346 if (!cnx->libGles2) {
Peiyong Lind30aaad2023-08-03 19:12:49 +0000347 cnx->libGles2 = load_wrapper(SYSTEM_LIB_PATH "/libGLESv2.so");
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700348 }
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700349
Charlie Lao840bd532020-01-16 17:34:37 -0800350 if (!cnx->libEgl || !cnx->libGles2 || !cnx->libGles1) {
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700351 android::GraphicsEnv::getInstance().setDriverLoaded(android::GpuStatsInfo::Api::API_GL,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800352 false, systemTime() - openTime);
353 }
354
Michael Chockc0ec5e22014-01-27 08:14:33 -0800355 LOG_ALWAYS_FATAL_IF(!cnx->libEgl,
356 "couldn't load system EGL wrapper libraries");
357
Charlie Lao840bd532020-01-16 17:34:37 -0800358 LOG_ALWAYS_FATAL_IF(!cnx->libGles2 || !cnx->libGles1,
359 "couldn't load system OpenGL ES wrapper libraries");
Jesse Hallc07b5202013-07-04 12:08:16 -0700360
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700361 android::GraphicsEnv::getInstance().setDriverLoaded(android::GpuStatsInfo::Api::API_GL, true,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800362 systemTime() - openTime);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800363
Mathias Agopiande586972009-05-28 17:39:03 -0700364 return (void*)hnd;
365}
366
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600367void Loader::close(egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700368{
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600369 driver_t* hnd = (driver_t*) cnx->dso;
Mathias Agopiande586972009-05-28 17:39:03 -0700370 delete hnd;
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600371 cnx->dso = nullptr;
372
Peiyong Lin2f51e752023-06-15 22:35:14 +0000373 cnx->angleLoaded = false;
Mathias Agopiande586972009-05-28 17:39:03 -0700374}
375
Jesse Hall94cdba92013-07-11 09:40:35 -0700376void Loader::init_api(void* dso,
377 char const * const * api,
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700378 char const * const * ref_api,
Jesse Hall94cdba92013-07-11 09:40:35 -0700379 __eglMustCastToProperFunctionPointerType* curr,
380 getProcAddressType getProcAddress)
Mathias Agopiande586972009-05-28 17:39:03 -0700381{
Jesse Hall1508ae62017-01-19 17:43:26 -0800382 ATRACE_CALL();
383
Mathias Agopian7773c432012-02-13 20:06:08 -0800384 const ssize_t SIZE = 256;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700385 char scrap[SIZE];
Mathias Agopiande586972009-05-28 17:39:03 -0700386 while (*api) {
387 char const * name = *api;
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700388 if (ref_api) {
389 char const * ref_name = *ref_api;
390 if (std::strcmp(name, ref_name) != 0) {
391 *curr++ = nullptr;
392 ref_api++;
393 continue;
394 }
395 }
396
Jesse Hall94cdba92013-07-11 09:40:35 -0700397 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700398 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700399 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700400 // couldn't find the entry-point, use eglGetProcAddress()
401 f = getProcAddress(name);
402 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700403 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700404 // Try without the OES postfix
405 ssize_t index = ssize_t(strlen(name)) - 3;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700406 if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) {
Mathias Agopiande586972009-05-28 17:39:03 -0700407 strncpy(scrap, name, index);
408 scrap[index] = 0;
409 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000410 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700411 }
412 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700413 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700414 // Try with the OES postfix
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700415 ssize_t index = ssize_t(strlen(name)) - 3;
416 if (index>0 && strcmp(name+index, "OES")) {
417 snprintf(scrap, SIZE, "%sOES", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700418 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000419 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700420 }
421 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700422 if (f == nullptr) {
Steve Block9d453682011-12-20 16:23:08 +0000423 //ALOGD("%s", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700424 f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented;
Mathias Agopian48d438d2012-01-28 21:44:00 -0800425
426 /*
Tao Wu1cdfe642020-06-23 12:33:02 -0700427 * GL_EXT_debug_marker is special, we always report it as
Mathias Agopian48d438d2012-01-28 21:44:00 -0800428 * supported, it's handled by GLES_trace. If GLES_trace is not
429 * enabled, then these are no-ops.
430 */
431 if (!strcmp(name, "glInsertEventMarkerEXT")) {
432 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
433 } else if (!strcmp(name, "glPushGroupMarkerEXT")) {
434 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
435 } else if (!strcmp(name, "glPopGroupMarkerEXT")) {
436 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
437 }
Mathias Agopiande586972009-05-28 17:39:03 -0700438 }
439 *curr++ = f;
440 api++;
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700441 if (ref_api) ref_api++;
Mathias Agopiande586972009-05-28 17:39:03 -0700442 }
443}
444
Peiyong Line4da7a82023-08-08 20:46:53 +0000445static std::string findLibrary(const std::string libraryName, const std::string searchPath,
446 const bool exact) {
447 if (exact) {
448 std::string absolutePath = searchPath + "/" + libraryName + ".so";
449 if (!access(absolutePath.c_str(), R_OK)) {
450 return absolutePath;
451 }
452 return std::string();
453 }
454
455 DIR* d = opendir(searchPath.c_str());
456 if (d != nullptr) {
457 struct dirent* e;
458 while ((e = readdir(d)) != nullptr) {
459 if (e->d_type == DT_DIR) {
460 continue;
461 }
462 if (!strcmp(e->d_name, "libGLES_android.so")) {
463 // always skip the software renderer
464 continue;
465 }
466 if (strstr(e->d_name, libraryName.c_str()) == e->d_name) {
467 if (!strcmp(e->d_name + strlen(e->d_name) - 3, ".so")) {
468 std::string result = searchPath + "/" + e->d_name;
469 closedir(d);
470 return result;
471 }
472 }
473 }
474 closedir(d);
475 }
476 // Driver not found. gah.
477 return std::string();
478}
479
Peiyong Lin0d10b252019-04-30 18:03:04 -0700480static void* load_system_driver(const char* kind, const char* suffix, const bool exact) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800481 ATRACE_CALL();
Mathias Agopian99381422013-04-23 20:52:29 +0200482
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700483 std::string libraryName = std::string("lib") + kind;
484 if (suffix) {
485 libraryName += std::string("_") + suffix;
Peiyong Lin0d10b252019-04-30 18:03:04 -0700486 } else if (!exact) {
Peiyong Line4da7a82023-08-08 20:46:53 +0000487 // Deprecated for devices launching in Android 14
488 // Look for files that match
489 // libGLES_*.so, or,
Peiyong Lin0d10b252019-04-30 18:03:04 -0700490 // libEGL_*.so, libGLESv1_CM_*.so, libGLESv2_*.so
491 libraryName += std::string("_");
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700492 }
Peiyong Line4da7a82023-08-08 20:46:53 +0000493
494 void* dso = nullptr;
495
Peiyong Lin6b2b28f2023-11-03 04:24:57 +0000496 const bool isSuffixAngle = suffix != nullptr && strcmp(suffix, ANGLE_SUFFIX_VALUE) == 0;
Peiyong Line4da7a82023-08-08 20:46:53 +0000497 const std::string absolutePath =
Peiyong Lin64bb9f62024-02-29 01:39:31 +0000498 findLibrary(libraryName, isSuffixAngle ? SYSTEM_LIB_PATH : VENDOR_LIB_EGL_DIR, exact);
Mathias Agopian65421432017-03-08 11:49:05 -0800499 if (absolutePath.empty()) {
Mathias Agopian99381422013-04-23 20:52:29 +0200500 // this happens often, we don't want to log an error
Yi Kong48a6cd22018-07-18 10:07:09 -0700501 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700502 }
Peiyong Line4da7a82023-08-08 20:46:53 +0000503 const char* const driverAbsolutePath = absolutePath.c_str();
Mathias Agopiande586972009-05-28 17:39:03 -0700504
Peiyong Line4da7a82023-08-08 20:46:53 +0000505 // Currently the default driver is unlikely to be ANGLE on most devices,
Peiyong Lin64bb9f62024-02-29 01:39:31 +0000506 // hence put this first. Only use sphal namespace when system ANGLE binaries
507 // are not the default drivers.
508 if (!isSuffixAngle) {
Peiyong Line4da7a82023-08-08 20:46:53 +0000509 // Try to load drivers from the 'sphal' namespace, if it exist. Fall back to
510 // the original routine when the namespace does not exist.
511 // See /system/linkerconfig/contents/namespace for the configuration of the
512 // sphal namespace.
513 dso = do_android_load_sphal_library(driverAbsolutePath, RTLD_NOW | RTLD_LOCAL);
514 } else {
515 // Try to load drivers from the default namespace.
516 // See /system/linkerconfig/contents/namespace for the configuration of the
517 // default namespace.
518 dso = do_dlopen(driverAbsolutePath, RTLD_NOW | RTLD_LOCAL);
519 }
520
Yi Kong48a6cd22018-07-18 10:07:09 -0700521 if (dso == nullptr) {
Mathias Agopian8c173842009-09-20 16:01:02 -0700522 const char* err = dlerror();
Peiyong Line4da7a82023-08-08 20:46:53 +0000523 ALOGE("load_driver(%s): %s", driverAbsolutePath, err ? err : "unknown");
Yi Kong48a6cd22018-07-18 10:07:09 -0700524 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700525 }
526
Peiyong Line4da7a82023-08-08 20:46:53 +0000527 ALOGV("loaded %s", driverAbsolutePath);
Mathias Agopianbaca89c2009-08-20 19:09:34 -0700528
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800529 return dso;
530}
531
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600532static void* load_angle(const char* kind, android_namespace_t* ns) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600533 std::string name = std::string("lib") + kind + "_angle.so";
Peiyong Line4da7a82023-08-08 20:46:53 +0000534 void* so = nullptr;
Cody Northrop1f00e172018-04-02 11:23:31 -0600535
Peiyong Line4da7a82023-08-08 20:46:53 +0000536 if (android::GraphicsEnv::getInstance().shouldUseSystemAngle()) {
537 so = do_dlopen(name.c_str(), RTLD_NOW | RTLD_LOCAL);
538 } else {
539 const android_dlextinfo dlextinfo = {
540 .flags = ANDROID_DLEXT_USE_NAMESPACE,
541 .library_namespace = ns,
542 };
543 so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
544 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600545
546 if (so) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600547 return so;
548 } else {
549 ALOGE("dlopen_ext(\"%s\") failed: %s", name.c_str(), dlerror());
550 }
551
552 return nullptr;
553}
554
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800555static void* load_updated_driver(const char* kind, android_namespace_t* ns) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800556 ATRACE_CALL();
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800557 const android_dlextinfo dlextinfo = {
558 .flags = ANDROID_DLEXT_USE_NAMESPACE,
559 .library_namespace = ns,
560 };
561 void* so = nullptr;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800562 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
Michael Hoisie4e0f56b2020-04-30 18:40:55 -0400563 auto prop = base::GetProperty(key, "");
564 if (prop.empty()) {
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700565 continue;
566 }
567 std::string name = std::string("lib") + kind + "_" + prop + ".so";
568 so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
569 if (so) {
570 return so;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800571 }
Yiwei Zhang9058b8f2020-11-12 20:23:00 +0000572 ALOGE("Could not load %s from updatable gfx driver namespace: %s.", name.c_str(),
573 dlerror());
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800574 }
575 return nullptr;
576}
577
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700578Loader::driver_t* Loader::attempt_to_load_angle(egl_connection_t* cnx) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800579 ATRACE_CALL();
Yiwei Zhangb22f0862020-04-23 10:40:24 -0700580
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600581 android_namespace_t* ns = android::GraphicsEnv::getInstance().getAngleNamespace();
Peiyong Line4da7a82023-08-08 20:46:53 +0000582 // ANGLE namespace is used for loading ANGLE from apk, and hence if namespace is not
583 // constructed, it means ANGLE apk is not set to be the OpenGL ES driver.
584 // Hence skip if ANGLE apk and system ANGLE are not set to be the OpenGL ES driver.
585 if (!ns && !android::GraphicsEnv::getInstance().shouldUseSystemAngle()) {
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700586 return nullptr;
Cody Northrop6d082ef2018-09-11 09:42:31 -0600587 }
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700588
Solti54161bf2023-11-09 21:09:53 +0000589 // use ANGLE APK driver
590 android::GraphicsEnv::getInstance().setDriverToLoad(android::GpuStatsInfo::Driver::ANGLE);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700591 driver_t* hnd = nullptr;
592
593 // ANGLE doesn't ship with GLES library, and thus we skip GLES driver.
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600594 void* dso = load_angle("EGL", ns);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700595 if (dso) {
596 initialize_api(dso, cnx, EGL);
597 hnd = new driver_t(dso);
598
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600599 dso = load_angle("GLESv1_CM", ns);
Charlie Lao840bd532020-01-16 17:34:37 -0800600 initialize_api(dso, cnx, GLESv1_CM);
601 hnd->set(dso, GLESv1_CM);
602
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600603 dso = load_angle("GLESv2", ns);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700604 initialize_api(dso, cnx, GLESv2);
605 hnd->set(dso, GLESv2);
606 }
607 return hnd;
608}
609
Peiyong Lin2f51e752023-06-15 22:35:14 +0000610void Loader::attempt_to_init_angle_backend(void* dso, egl_connection_t* cnx) {
Peiyong Lin9fc4cf32023-11-03 05:18:53 +0000611 cnx->angleGetDisplayPlatformFunc = dlsym(dso, "ANGLEGetDisplayPlatform");
612 cnx->angleResetDisplayPlatformFunc = dlsym(dso, "ANGLEResetDisplayPlatform");
613
614 if (cnx->angleGetDisplayPlatformFunc) {
Peiyong Lin2f51e752023-06-15 22:35:14 +0000615 ALOGV("ANGLE GLES library loaded");
616 cnx->angleLoaded = true;
Peiyong Line4da7a82023-08-08 20:46:53 +0000617 android::GraphicsEnv::getInstance().setDriverToLoad(android::GpuStatsInfo::Driver::ANGLE);
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600618 } else {
Peiyong Lin2f51e752023-06-15 22:35:14 +0000619 ALOGV("Native GLES library loaded");
620 cnx->angleLoaded = false;
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600621 }
622}
623
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700624Loader::driver_t* Loader::attempt_to_load_updated_driver(egl_connection_t* cnx) {
625 ATRACE_CALL();
Yiwei Zhangd40aaac2020-06-04 05:26:56 -0700626
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700627 android_namespace_t* ns = android::GraphicsEnv::getInstance().getDriverNamespace();
628 if (!ns) {
629 return nullptr;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800630 }
631
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700632 ALOGD("Load updated gl driver.");
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700633 android::GraphicsEnv::getInstance().setDriverToLoad(android::GpuStatsInfo::Driver::GL_UPDATED);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700634 driver_t* hnd = nullptr;
635 void* dso = load_updated_driver("GLES", ns);
Peiyong Line83b8682019-04-18 17:23:02 -0700636 if (dso) {
Charlie Lao840bd532020-01-16 17:34:37 -0800637 initialize_api(dso, cnx, EGL | GLESv1_CM | GLESv2);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700638 hnd = new driver_t(dso);
639 return hnd;
Peiyong Line83b8682019-04-18 17:23:02 -0700640 }
641
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700642 dso = load_updated_driver("EGL", ns);
643 if (dso) {
644 initialize_api(dso, cnx, EGL);
645 hnd = new driver_t(dso);
646
Charlie Lao840bd532020-01-16 17:34:37 -0800647 dso = load_updated_driver("GLESv1_CM", ns);
648 initialize_api(dso, cnx, GLESv1_CM);
649 hnd->set(dso, GLESv1_CM);
650
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700651 dso = load_updated_driver("GLESv2", ns);
652 initialize_api(dso, cnx, GLESv2);
653 hnd->set(dso, GLESv2);
654 }
655 return hnd;
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700656}
657
Peiyong Lin0d10b252019-04-30 18:03:04 -0700658Loader::driver_t* Loader::attempt_to_load_system_driver(egl_connection_t* cnx, const char* suffix,
659 const bool exact) {
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700660 ATRACE_CALL();
Solti54161bf2023-11-09 21:09:53 +0000661 if (suffix && strcmp(suffix, "angle") == 0) {
662 // use system ANGLE driver
663 android::GraphicsEnv::getInstance().setDriverToLoad(android::GpuStatsInfo::Driver::ANGLE);
664 } else {
665 android::GraphicsEnv::getInstance().setDriverToLoad(android::GpuStatsInfo::Driver::GL);
666 }
667
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700668 driver_t* hnd = nullptr;
Peiyong Lin0d10b252019-04-30 18:03:04 -0700669 void* dso = load_system_driver("GLES", suffix, exact);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700670 if (dso) {
Charlie Lao840bd532020-01-16 17:34:37 -0800671 initialize_api(dso, cnx, EGL | GLESv1_CM | GLESv2);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700672 hnd = new driver_t(dso);
673 return hnd;
674 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700675 dso = load_system_driver("EGL", suffix, exact);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700676 if (dso) {
677 initialize_api(dso, cnx, EGL);
678 hnd = new driver_t(dso);
679
Charlie Lao840bd532020-01-16 17:34:37 -0800680 dso = load_system_driver("GLESv1_CM", suffix, exact);
681 initialize_api(dso, cnx, GLESv1_CM);
682 hnd->set(dso, GLESv1_CM);
683
Peiyong Lin0d10b252019-04-30 18:03:04 -0700684 dso = load_system_driver("GLESv2", suffix, exact);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700685 initialize_api(dso, cnx, GLESv2);
686 hnd->set(dso, GLESv2);
687 }
688 return hnd;
Peiyong Line83b8682019-04-18 17:23:02 -0700689}
690
691void Loader::initialize_api(void* dso, egl_connection_t* cnx, uint32_t mask) {
Mathias Agopiande586972009-05-28 17:39:03 -0700692 if (mask & EGL) {
693 getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress");
694
Jesse Hall94cdba92013-07-11 09:40:35 -0700695 ALOGE_IF(!getProcAddress,
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800696 "can't find eglGetProcAddress() in EGL driver library");
Mathias Agopiande586972009-05-28 17:39:03 -0700697
Mathias Agopian618fa102009-10-14 02:06:37 -0700698 egl_t* egl = &cnx->egl;
Mathias Agopiande586972009-05-28 17:39:03 -0700699 __eglMustCastToProperFunctionPointerType* curr =
700 (__eglMustCastToProperFunctionPointerType*)egl;
701 char const * const * api = egl_names;
702 while (*api) {
703 char const * name = *api;
Jesse Hall94cdba92013-07-11 09:40:35 -0700704 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700705 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700706 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700707 // couldn't find the entry-point, use eglGetProcAddress()
708 f = getProcAddress(name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700709 if (f == nullptr) {
710 f = (__eglMustCastToProperFunctionPointerType)nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700711 }
712 }
713 *curr++ = f;
714 api++;
715 }
716 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700717
Mathias Agopiande586972009-05-28 17:39:03 -0700718 if (mask & GLESv1_CM) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700719 init_api(dso, gl_names_1, gl_names,
Mathias Agopian618fa102009-10-14 02:06:37 -0700720 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800721 &cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl,
Mathias Agopian618fa102009-10-14 02:06:37 -0700722 getProcAddress);
Mathias Agopiande586972009-05-28 17:39:03 -0700723 }
724
725 if (mask & GLESv2) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700726 init_api(dso, gl_names, nullptr,
Mathias Agopian618fa102009-10-14 02:06:37 -0700727 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800728 &cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl,
Mathias Agopiande586972009-05-28 17:39:03 -0700729 getProcAddress);
730 }
Mathias Agopiande586972009-05-28 17:39:03 -0700731}
732
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700733} // namespace android