blob: 654e5b7c03e63056979886ec9ad1c68e687cacab [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 Lind43ee402023-05-24 17:59:08 +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:
44 * /vendor/lib/egl/libGLES.so
45 *
46 * - as separate libraries:
47 * /vendor/lib/egl/libEGL.so
48 * /vendor/lib/egl/libGLESv1_CM.so
49 * /vendor/lib/egl/libGLESv2.so
50 *
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 *
Mathias Agopian99381422013-04-23 20:52:29 +020054 * /{vendor|system}/lib/egl/lib{GLES | [EGL|GLESv1_CM|GLESv2]}_*.so
Jesse Hall94cdba92013-07-11 09:40:35 -070055 *
Mathias Agopiande586972009-05-28 17:39:03 -070056 */
57
Mathias Agopian65421432017-03-08 11:49:05 -080058Loader& Loader::getInstance() {
59 static Loader loader;
60 return loader;
61}
Mathias Agopiande586972009-05-28 17:39:03 -070062
Jesse Hall1508ae62017-01-19 17:43:26 -080063static void* do_dlopen(const char* path, int mode) {
64 ATRACE_CALL();
65 return dlopen(path, mode);
66}
67
Jiyong Park5910dc72017-04-05 14:23:52 +090068static void* do_android_dlopen_ext(const char* path, int mode, const android_dlextinfo* info) {
69 ATRACE_CALL();
70 return android_dlopen_ext(path, mode, info);
71}
72
Justin Yunb7320302017-05-22 15:13:40 +090073static void* do_android_load_sphal_library(const char* path, int mode) {
74 ATRACE_CALL();
75 return android_load_sphal_library(path, mode);
76}
77
Yiwei Zhang5e21eb32019-06-05 00:26:03 -070078static int do_android_unload_sphal_library(void* dso) {
79 ATRACE_CALL();
80 return android_unload_sphal_library(dso);
81}
82
Jesse Hall94cdba92013-07-11 09:40:35 -070083Loader::driver_t::driver_t(void* gles)
Mathias Agopiande586972009-05-28 17:39:03 -070084{
85 dso[0] = gles;
86 for (size_t i=1 ; i<NELEM(dso) ; i++)
Yi Kong48a6cd22018-07-18 10:07:09 -070087 dso[i] = nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -070088}
89
Jesse Hall94cdba92013-07-11 09:40:35 -070090Loader::driver_t::~driver_t()
Mathias Agopiande586972009-05-28 17:39:03 -070091{
92 for (size_t i=0 ; i<NELEM(dso) ; i++) {
93 if (dso[i]) {
94 dlclose(dso[i]);
Yi Kong48a6cd22018-07-18 10:07:09 -070095 dso[i] = nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -070096 }
97 }
98}
99
Mathias Agopian65421432017-03-08 11:49:05 -0800100int Loader::driver_t::set(void* hnd, int32_t api)
Mathias Agopiande586972009-05-28 17:39:03 -0700101{
102 switch (api) {
103 case EGL:
104 dso[0] = hnd;
105 break;
106 case GLESv1_CM:
107 dso[1] = hnd;
108 break;
109 case GLESv2:
110 dso[2] = hnd;
111 break;
112 default:
Mathias Agopian65421432017-03-08 11:49:05 -0800113 return -EOVERFLOW;
Mathias Agopiande586972009-05-28 17:39:03 -0700114 }
Mathias Agopian65421432017-03-08 11:49:05 -0800115 return 0;
Mathias Agopiande586972009-05-28 17:39:03 -0700116}
117
Mathias Agopiande586972009-05-28 17:39:03 -0700118Loader::Loader()
Yi Kong48a6cd22018-07-18 10:07:09 -0700119 : getProcAddress(nullptr)
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800120{
Mathias Agopiande586972009-05-28 17:39:03 -0700121}
122
Mathias Agopian99381422013-04-23 20:52:29 +0200123Loader::~Loader() {
Mathias Agopiande586972009-05-28 17:39:03 -0700124}
125
Jesse Hallc07b5202013-07-04 12:08:16 -0700126static void* load_wrapper(const char* path) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800127 void* so = do_dlopen(path, RTLD_NOW | RTLD_LOCAL);
Jesse Hallc07b5202013-07-04 12:08:16 -0700128 ALOGE_IF(!so, "dlopen(\"%s\") failed: %s", path, dlerror());
129 return so;
130}
131
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700132#ifndef EGL_WRAPPER_DIR
133#if defined(__LP64__)
134#define EGL_WRAPPER_DIR "/system/lib64"
135#else
136#define EGL_WRAPPER_DIR "/system/lib"
137#endif
138#endif
139
Peiyong Linb8aaeaf2023-06-13 20:51:03 +0000140static const char* PERSIST_DRIVER_SUFFIX_PROPERTY = "persist.graphics.egl";
141static const char* RO_DRIVER_SUFFIX_PROPERTY = "ro.hardware.egl";
142static const char* RO_BOARD_PLATFORM_PROPERTY = "ro.board.platform";
Peiyong Lin0d10b252019-04-30 18:03:04 -0700143
Peiyong Lin9679a982023-04-10 22:00:30 +0000144static const char* HAL_SUBNAME_KEY_PROPERTIES[3] = {
Peiyong Linb8aaeaf2023-06-13 20:51:03 +0000145 PERSIST_DRIVER_SUFFIX_PROPERTY,
146 RO_DRIVER_SUFFIX_PROPERTY,
147 RO_BOARD_PLATFORM_PROPERTY,
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700148};
149
Peiyong Lin54926182023-06-15 22:35:14 +0000150// Check whether the loaded system drivers should be unloaded in order to
151// load ANGLE or the updatable graphics drivers.
152// If ANGLE namespace is set, it means the application is identified to run on top of ANGLE.
153// If updatable graphics driver namespace is set, it means the application is identified to
154// run on top of updatable graphics drivers.
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700155static bool should_unload_system_driver(egl_connection_t* cnx) {
156 // Return false if the system driver has been unloaded once.
157 if (cnx->systemDriverUnloaded) {
158 return false;
159 }
160
Peiyong Lin54926182023-06-15 22:35:14 +0000161 // Return true if ANGLE namespace is set.
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700162 android_namespace_t* ns = android::GraphicsEnv::getInstance().getAngleNamespace();
163 if (ns) {
Peiyong Lin1224faa2023-06-28 15:09:09 +0000164 // Unless the default GLES driver is ANGLE and the process should use system ANGLE, since
165 // the intended GLES driver is already loaded.
166 // This should be updated in a later patch that cleans up namespaces
167 if (!(cnx->angleLoaded && android::GraphicsEnv::getInstance().shouldUseSystemAngle())) {
168 return true;
169 }
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700170 }
171
Peiyong Lin0d60e802023-06-30 00:35:07 +0000172 // Return true if native GLES drivers should be used and ANGLE is already loaded.
173 if (android::GraphicsEnv::getInstance().shouldUseNativeDriver() && cnx->angleLoaded) {
174 return true;
175 }
176
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700177 // Return true if updated driver namespace is set.
178 ns = android::GraphicsEnv::getInstance().getDriverNamespace();
179 if (ns) {
180 return true;
181 }
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700182
183 return false;
184}
185
186static void uninit_api(char const* const* api, __eglMustCastToProperFunctionPointerType* curr) {
187 while (*api) {
188 *curr++ = nullptr;
189 api++;
190 }
191}
192
193void Loader::unload_system_driver(egl_connection_t* cnx) {
194 ATRACE_CALL();
195
196 uninit_api(gl_names,
197 (__eglMustCastToProperFunctionPointerType*)&cnx
198 ->hooks[egl_connection_t::GLESv2_INDEX]
199 ->gl);
200 uninit_api(gl_names,
201 (__eglMustCastToProperFunctionPointerType*)&cnx
202 ->hooks[egl_connection_t::GLESv1_INDEX]
203 ->gl);
204 uninit_api(egl_names, (__eglMustCastToProperFunctionPointerType*)&cnx->egl);
205
206 if (cnx->dso) {
207 ALOGD("Unload system gl driver.");
208 driver_t* hnd = (driver_t*)cnx->dso;
209 if (hnd->dso[2]) {
210 do_android_unload_sphal_library(hnd->dso[2]);
211 }
212 if (hnd->dso[1]) {
213 do_android_unload_sphal_library(hnd->dso[1]);
214 }
215 if (hnd->dso[0]) {
216 do_android_unload_sphal_library(hnd->dso[0]);
217 }
218 cnx->dso = nullptr;
Peiyong Lin1224faa2023-06-28 15:09:09 +0000219 cnx->angleLoaded = false;
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700220 }
221
222 cnx->systemDriverUnloaded = true;
223}
224
Peiyong Lin1224faa2023-06-28 15:09:09 +0000225void* Loader::open(egl_connection_t* cnx) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800226 ATRACE_CALL();
Yiwei Zhangd9861812019-02-13 11:51:55 -0800227 const nsecs_t openTime = systemTime();
Jesse Hall1508ae62017-01-19 17:43:26 -0800228
Peiyong Lin1224faa2023-06-28 15:09:09 +0000229 if (cnx->dso && should_unload_system_driver(cnx)) {
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700230 unload_system_driver(cnx);
231 }
232
233 // If a driver has been loaded, return the driver directly.
234 if (cnx->dso) {
235 return cnx->dso;
236 }
237
Peiyong Lin1224faa2023-06-28 15:09:09 +0000238 driver_t* hnd = nullptr;
239 // Firstly, try to load ANGLE driver, if ANGLE should be loaded and fail, abort.
240 if (android::GraphicsEnv::getInstance().shouldUseAngle()) {
241 hnd = attempt_to_load_angle(cnx);
242 LOG_ALWAYS_FATAL_IF(!hnd, "Failed to load ANGLE.");
243 }
Ian Elliott5279f862022-04-21 12:41:03 -0600244
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700245 if (!hnd) {
246 // Secondly, try to load from driver apk.
247 hnd = attempt_to_load_updated_driver(cnx);
Peiyong Lin0d60e802023-06-30 00:35:07 +0000248
249 // If updated driver apk is set but fail to load, abort here.
250 LOG_ALWAYS_FATAL_IF(android::GraphicsEnv::getInstance().getDriverNamespace(),
251 "couldn't find an OpenGL ES implementation from %s",
252 android::GraphicsEnv::getInstance().getDriverPath().c_str());
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700253 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700254
Peiyong Lin0d60e802023-06-30 00:35:07 +0000255 // Attempt to load native GLES drivers specified by ro.hardware.egl if native is selected.
256 // If native is selected but fail to load, abort.
257 if (!hnd && android::GraphicsEnv::getInstance().shouldUseNativeDriver()) {
258 auto driverSuffix = base::GetProperty(RO_DRIVER_SUFFIX_PROPERTY, "");
259 LOG_ALWAYS_FATAL_IF(driverSuffix.empty(),
260 "Native GLES driver is selected but not specified in %s",
261 RO_DRIVER_SUFFIX_PROPERTY);
262 hnd = attempt_to_load_system_driver(cnx, driverSuffix.c_str(), true);
263 LOG_ALWAYS_FATAL_IF(!hnd, "Native GLES driver is selected but failed to load. %s=%s",
264 RO_DRIVER_SUFFIX_PROPERTY, driverSuffix.c_str());
265 }
266
267 // Finally, try to load default driver.
Peiyong Lin0d10b252019-04-30 18:03:04 -0700268 bool failToLoadFromDriverSuffixProperty = false;
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700269 if (!hnd) {
Peiyong Lin98db8e02023-04-05 20:09:16 +0000270 // Start by searching for the library name appended by the system
271 // properties of the GLES userspace driver in both locations.
272 // i.e.:
273 // libGLES_${prop}.so, or:
274 // libEGL_${prop}.so, libGLESv1_CM_${prop}.so, libGLESv2_${prop}.so
275 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
276 auto prop = base::GetProperty(key, "");
277 if (prop.empty()) {
278 continue;
279 }
280 hnd = attempt_to_load_system_driver(cnx, prop.c_str(), true);
Peiyong Linb8aaeaf2023-06-13 20:51:03 +0000281 if (!hnd) {
282 ALOGD("Failed to load drivers from property %s with value %s", key, prop.c_str());
Peiyong Lin98db8e02023-04-05 20:09:16 +0000283 failToLoadFromDriverSuffixProperty = true;
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700284 }
Peiyong Linb8aaeaf2023-06-13 20:51:03 +0000285
286 // Abort regardless of whether subsequent properties are set, the value must be set
287 // correctly with the first property that has a value.
288 break;
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700289 }
290 }
291
292 if (!hnd) {
Peiyong Linb8aaeaf2023-06-13 20:51:03 +0000293 // Can't find graphics driver by appending the value from system properties, now search for
294 // the exact name without any suffix of the GLES userspace driver in both locations.
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700295 // i.e.:
296 // libGLES.so, or:
297 // libEGL.so, libGLESv1_CM.so, libGLESv2.so
Charlie Lao840bd532020-01-16 17:34:37 -0800298 hnd = attempt_to_load_system_driver(cnx, nullptr, true);
Peiyong Lin0d10b252019-04-30 18:03:04 -0700299 }
300
Peiyong Lind43ee402023-05-24 17:59:08 +0000301 if (!hnd && !failToLoadFromDriverSuffixProperty &&
302 property_get_int32("ro.vendor.api_level", 0) < __ANDROID_API_U__) {
303 // Still can't find the graphics drivers with the exact name. This time try to use wildcard
304 // matching if the device is launched before Android 14.
Charlie Lao840bd532020-01-16 17:34:37 -0800305 hnd = attempt_to_load_system_driver(cnx, nullptr, false);
Mathias Agopiande586972009-05-28 17:39:03 -0700306 }
307
Yiwei Zhangd9861812019-02-13 11:51:55 -0800308 if (!hnd) {
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700309 android::GraphicsEnv::getInstance().setDriverLoaded(android::GpuStatsInfo::Api::API_GL,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800310 false, systemTime() - openTime);
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600311 } else {
312 // init_angle_backend will check if loaded driver is ANGLE or not,
Peiyong Lin54926182023-06-15 22:35:14 +0000313 // will set cnx->angleLoaded appropriately.
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600314 // Do this here so that we use ANGLE path when driver is ANGLE (e.g. loaded as native),
315 // not just loading ANGLE as option.
Peiyong Lin54926182023-06-15 22:35:14 +0000316 attempt_to_init_angle_backend(hnd->dso[2], cnx);
Yiwei Zhangd9861812019-02-13 11:51:55 -0800317 }
318
Peiyong Lin3575b9f2019-04-25 14:26:49 -0700319 LOG_ALWAYS_FATAL_IF(!hnd,
Peiyong Lin9679a982023-04-10 22:00:30 +0000320 "couldn't find an OpenGL ES implementation, make sure one of %s, %s and %s "
321 "is set",
322 HAL_SUBNAME_KEY_PROPERTIES[0], HAL_SUBNAME_KEY_PROPERTIES[1],
323 HAL_SUBNAME_KEY_PROPERTIES[2]);
Jesse Hallc07b5202013-07-04 12:08:16 -0700324
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700325 if (!cnx->libEgl) {
326 cnx->libEgl = load_wrapper(EGL_WRAPPER_DIR "/libEGL.so");
327 }
Charlie Lao840bd532020-01-16 17:34:37 -0800328 if (!cnx->libGles1) {
329 cnx->libGles1 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv1_CM.so");
330 }
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700331 if (!cnx->libGles2) {
332 cnx->libGles2 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv2.so");
333 }
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700334
Charlie Lao840bd532020-01-16 17:34:37 -0800335 if (!cnx->libEgl || !cnx->libGles2 || !cnx->libGles1) {
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700336 android::GraphicsEnv::getInstance().setDriverLoaded(android::GpuStatsInfo::Api::API_GL,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800337 false, systemTime() - openTime);
338 }
339
Michael Chockc0ec5e22014-01-27 08:14:33 -0800340 LOG_ALWAYS_FATAL_IF(!cnx->libEgl,
341 "couldn't load system EGL wrapper libraries");
342
Charlie Lao840bd532020-01-16 17:34:37 -0800343 LOG_ALWAYS_FATAL_IF(!cnx->libGles2 || !cnx->libGles1,
344 "couldn't load system OpenGL ES wrapper libraries");
Jesse Hallc07b5202013-07-04 12:08:16 -0700345
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700346 android::GraphicsEnv::getInstance().setDriverLoaded(android::GpuStatsInfo::Api::API_GL, true,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800347 systemTime() - openTime);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800348
Mathias Agopiande586972009-05-28 17:39:03 -0700349 return (void*)hnd;
350}
351
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600352void Loader::close(egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700353{
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600354 driver_t* hnd = (driver_t*) cnx->dso;
Mathias Agopiande586972009-05-28 17:39:03 -0700355 delete hnd;
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600356 cnx->dso = nullptr;
357
Peiyong Lin54926182023-06-15 22:35:14 +0000358 cnx->angleLoaded = false;
Mathias Agopiande586972009-05-28 17:39:03 -0700359}
360
Jesse Hall94cdba92013-07-11 09:40:35 -0700361void Loader::init_api(void* dso,
362 char const * const * api,
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700363 char const * const * ref_api,
Jesse Hall94cdba92013-07-11 09:40:35 -0700364 __eglMustCastToProperFunctionPointerType* curr,
365 getProcAddressType getProcAddress)
Mathias Agopiande586972009-05-28 17:39:03 -0700366{
Jesse Hall1508ae62017-01-19 17:43:26 -0800367 ATRACE_CALL();
368
Mathias Agopian7773c432012-02-13 20:06:08 -0800369 const ssize_t SIZE = 256;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700370 char scrap[SIZE];
Mathias Agopiande586972009-05-28 17:39:03 -0700371 while (*api) {
372 char const * name = *api;
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700373 if (ref_api) {
374 char const * ref_name = *ref_api;
375 if (std::strcmp(name, ref_name) != 0) {
376 *curr++ = nullptr;
377 ref_api++;
378 continue;
379 }
380 }
381
Jesse Hall94cdba92013-07-11 09:40:35 -0700382 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700383 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700384 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700385 // couldn't find the entry-point, use eglGetProcAddress()
386 f = getProcAddress(name);
387 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700388 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700389 // Try without the OES postfix
390 ssize_t index = ssize_t(strlen(name)) - 3;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700391 if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) {
Mathias Agopiande586972009-05-28 17:39:03 -0700392 strncpy(scrap, name, index);
393 scrap[index] = 0;
394 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000395 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700396 }
397 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700398 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700399 // Try with the OES postfix
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700400 ssize_t index = ssize_t(strlen(name)) - 3;
401 if (index>0 && strcmp(name+index, "OES")) {
402 snprintf(scrap, SIZE, "%sOES", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700403 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000404 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700405 }
406 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700407 if (f == nullptr) {
Steve Block9d453682011-12-20 16:23:08 +0000408 //ALOGD("%s", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700409 f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented;
Mathias Agopian48d438d2012-01-28 21:44:00 -0800410
411 /*
Tao Wu1cdfe642020-06-23 12:33:02 -0700412 * GL_EXT_debug_marker is special, we always report it as
Mathias Agopian48d438d2012-01-28 21:44:00 -0800413 * supported, it's handled by GLES_trace. If GLES_trace is not
414 * enabled, then these are no-ops.
415 */
416 if (!strcmp(name, "glInsertEventMarkerEXT")) {
417 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
418 } else if (!strcmp(name, "glPushGroupMarkerEXT")) {
419 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
420 } else if (!strcmp(name, "glPopGroupMarkerEXT")) {
421 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
422 }
Mathias Agopiande586972009-05-28 17:39:03 -0700423 }
424 *curr++ = f;
425 api++;
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700426 if (ref_api) ref_api++;
Mathias Agopiande586972009-05-28 17:39:03 -0700427 }
428}
429
Peiyong Lin0d10b252019-04-30 18:03:04 -0700430static void* load_system_driver(const char* kind, const char* suffix, const bool exact) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800431 ATRACE_CALL();
Mathias Agopian99381422013-04-23 20:52:29 +0200432 class MatchFile {
433 public:
Peiyong Lin0d10b252019-04-30 18:03:04 -0700434 static std::string find(const char* libraryName, const bool exact) {
Mathias Agopian99381422013-04-23 20:52:29 +0200435 const char* const searchPaths[] = {
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800436#if defined(__LP64__)
437 "/vendor/lib64/egl",
438 "/system/lib64/egl"
439#else
Mathias Agopian99381422013-04-23 20:52:29 +0200440 "/vendor/lib/egl",
441 "/system/lib/egl"
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800442#endif
Mathias Agopian99381422013-04-23 20:52:29 +0200443 };
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700444
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700445 for (auto dir : searchPaths) {
Peiyong Lin0d10b252019-04-30 18:03:04 -0700446 std::string absolutePath;
447 if (find(absolutePath, libraryName, dir, exact)) {
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700448 return absolutePath;
Mathias Agopian99381422013-04-23 20:52:29 +0200449 }
Mathias Agopian99381422013-04-23 20:52:29 +0200450 }
451
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700452 // Driver not found. gah.
453 return std::string();
Mathias Agopian99381422013-04-23 20:52:29 +0200454 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700455 private:
456 static bool find(std::string& result,
457 const std::string& pattern, const char* const search, bool exact) {
458 if (exact) {
459 std::string absolutePath = std::string(search) + "/" + pattern + ".so";
460 if (!access(absolutePath.c_str(), R_OK)) {
461 result = absolutePath;
462 return true;
463 }
464 return false;
465 }
466
467 DIR* d = opendir(search);
468 if (d != nullptr) {
469 struct dirent* e;
470 while ((e = readdir(d)) != nullptr) {
471 if (e->d_type == DT_DIR) {
472 continue;
473 }
474 if (!strcmp(e->d_name, "libGLES_android.so")) {
475 // always skip the software renderer
476 continue;
477 }
478 if (strstr(e->d_name, pattern.c_str()) == e->d_name) {
479 if (!strcmp(e->d_name + strlen(e->d_name) - 3, ".so")) {
480 result = std::string(search) + "/" + e->d_name;
481 closedir(d);
482 return true;
483 }
484 }
485 }
486 closedir(d);
487 }
488 return false;
489 }
Mathias Agopian99381422013-04-23 20:52:29 +0200490 };
491
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700492 std::string libraryName = std::string("lib") + kind;
493 if (suffix) {
494 libraryName += std::string("_") + suffix;
Peiyong Lin0d10b252019-04-30 18:03:04 -0700495 } else if (!exact) {
496 // Deprecated: we look for files that match
497 // libGLES_*.so, or:
498 // libEGL_*.so, libGLESv1_CM_*.so, libGLESv2_*.so
499 libraryName += std::string("_");
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700500 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700501 std::string absolutePath = MatchFile::find(libraryName.c_str(), exact);
Mathias Agopian65421432017-03-08 11:49:05 -0800502 if (absolutePath.empty()) {
Mathias Agopian99381422013-04-23 20:52:29 +0200503 // this happens often, we don't want to log an error
Yi Kong48a6cd22018-07-18 10:07:09 -0700504 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700505 }
Mathias Agopian65421432017-03-08 11:49:05 -0800506 const char* const driver_absolute_path = absolutePath.c_str();
Mathias Agopiande586972009-05-28 17:39:03 -0700507
Jiyong Park5910dc72017-04-05 14:23:52 +0900508 // Try to load drivers from the 'sphal' namespace, if it exist. Fall back to
Justin Yunb7320302017-05-22 15:13:40 +0900509 // the original routine when the namespace does not exist.
Jiyong Park5910dc72017-04-05 14:23:52 +0900510 // See /system/core/rootdir/etc/ld.config.txt for the configuration of the
511 // sphal namespace.
Justin Yunb7320302017-05-22 15:13:40 +0900512 void* dso = do_android_load_sphal_library(driver_absolute_path,
513 RTLD_NOW | RTLD_LOCAL);
Yi Kong48a6cd22018-07-18 10:07:09 -0700514 if (dso == nullptr) {
Mathias Agopian8c173842009-09-20 16:01:02 -0700515 const char* err = dlerror();
Mathias Agopian65421432017-03-08 11:49:05 -0800516 ALOGE("load_driver(%s): %s", driver_absolute_path, err ? err : "unknown");
Yi Kong48a6cd22018-07-18 10:07:09 -0700517 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700518 }
519
Steve Block9d453682011-12-20 16:23:08 +0000520 ALOGD("loaded %s", driver_absolute_path);
Mathias Agopianbaca89c2009-08-20 19:09:34 -0700521
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800522 return dso;
523}
524
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600525static void* load_angle(const char* kind, android_namespace_t* ns) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600526 const android_dlextinfo dlextinfo = {
527 .flags = ANDROID_DLEXT_USE_NAMESPACE,
528 .library_namespace = ns,
529 };
530
531 std::string name = std::string("lib") + kind + "_angle.so";
532
533 void* so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
534
535 if (so) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600536 return so;
537 } else {
538 ALOGE("dlopen_ext(\"%s\") failed: %s", name.c_str(), dlerror());
539 }
540
541 return nullptr;
542}
543
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800544static void* load_updated_driver(const char* kind, android_namespace_t* ns) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800545 ATRACE_CALL();
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800546 const android_dlextinfo dlextinfo = {
547 .flags = ANDROID_DLEXT_USE_NAMESPACE,
548 .library_namespace = ns,
549 };
550 void* so = nullptr;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800551 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
Michael Hoisie4e0f56b2020-04-30 18:40:55 -0400552 auto prop = base::GetProperty(key, "");
553 if (prop.empty()) {
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700554 continue;
555 }
556 std::string name = std::string("lib") + kind + "_" + prop + ".so";
557 so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
558 if (so) {
559 return so;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800560 }
Yiwei Zhang9058b8f2020-11-12 20:23:00 +0000561 ALOGE("Could not load %s from updatable gfx driver namespace: %s.", name.c_str(),
562 dlerror());
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800563 }
564 return nullptr;
565}
566
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700567Loader::driver_t* Loader::attempt_to_load_angle(egl_connection_t* cnx) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800568 ATRACE_CALL();
Yiwei Zhangb22f0862020-04-23 10:40:24 -0700569
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600570 android_namespace_t* ns = android::GraphicsEnv::getInstance().getAngleNamespace();
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700571 if (!ns) {
572 return nullptr;
Cody Northrop6d082ef2018-09-11 09:42:31 -0600573 }
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700574
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700575 android::GraphicsEnv::getInstance().setDriverToLoad(android::GpuStatsInfo::Driver::ANGLE);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700576 driver_t* hnd = nullptr;
577
578 // ANGLE doesn't ship with GLES library, and thus we skip GLES driver.
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600579 void* dso = load_angle("EGL", ns);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700580 if (dso) {
581 initialize_api(dso, cnx, EGL);
582 hnd = new driver_t(dso);
583
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600584 dso = load_angle("GLESv1_CM", ns);
Charlie Lao840bd532020-01-16 17:34:37 -0800585 initialize_api(dso, cnx, GLESv1_CM);
586 hnd->set(dso, GLESv1_CM);
587
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600588 dso = load_angle("GLESv2", ns);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700589 initialize_api(dso, cnx, GLESv2);
590 hnd->set(dso, GLESv2);
591 }
592 return hnd;
593}
594
Peiyong Lin54926182023-06-15 22:35:14 +0000595void Loader::attempt_to_init_angle_backend(void* dso, egl_connection_t* cnx) {
Courtney Goeltzenleuchter8ecb10c2020-04-29 13:16:23 -0600596 void* pANGLEGetDisplayPlatform = dlsym(dso, "ANGLEGetDisplayPlatform");
597 if (pANGLEGetDisplayPlatform) {
Peiyong Lin54926182023-06-15 22:35:14 +0000598 ALOGV("ANGLE GLES library loaded");
599 cnx->angleLoaded = true;
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600600 } else {
Peiyong Lin54926182023-06-15 22:35:14 +0000601 ALOGV("Native GLES library loaded");
602 cnx->angleLoaded = false;
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600603 }
604}
605
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700606Loader::driver_t* Loader::attempt_to_load_updated_driver(egl_connection_t* cnx) {
607 ATRACE_CALL();
Yiwei Zhangd40aaac2020-06-04 05:26:56 -0700608
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700609 android_namespace_t* ns = android::GraphicsEnv::getInstance().getDriverNamespace();
610 if (!ns) {
611 return nullptr;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800612 }
613
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700614 ALOGD("Load updated gl driver.");
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700615 android::GraphicsEnv::getInstance().setDriverToLoad(android::GpuStatsInfo::Driver::GL_UPDATED);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700616 driver_t* hnd = nullptr;
617 void* dso = load_updated_driver("GLES", ns);
Peiyong Line83b8682019-04-18 17:23:02 -0700618 if (dso) {
Charlie Lao840bd532020-01-16 17:34:37 -0800619 initialize_api(dso, cnx, EGL | GLESv1_CM | GLESv2);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700620 hnd = new driver_t(dso);
621 return hnd;
Peiyong Line83b8682019-04-18 17:23:02 -0700622 }
623
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700624 dso = load_updated_driver("EGL", ns);
625 if (dso) {
626 initialize_api(dso, cnx, EGL);
627 hnd = new driver_t(dso);
628
Charlie Lao840bd532020-01-16 17:34:37 -0800629 dso = load_updated_driver("GLESv1_CM", ns);
630 initialize_api(dso, cnx, GLESv1_CM);
631 hnd->set(dso, GLESv1_CM);
632
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700633 dso = load_updated_driver("GLESv2", ns);
634 initialize_api(dso, cnx, GLESv2);
635 hnd->set(dso, GLESv2);
636 }
637 return hnd;
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700638}
639
Peiyong Lin0d10b252019-04-30 18:03:04 -0700640Loader::driver_t* Loader::attempt_to_load_system_driver(egl_connection_t* cnx, const char* suffix,
641 const bool exact) {
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700642 ATRACE_CALL();
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700643 android::GraphicsEnv::getInstance().setDriverToLoad(android::GpuStatsInfo::Driver::GL);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700644 driver_t* hnd = nullptr;
Peiyong Lin0d10b252019-04-30 18:03:04 -0700645 void* dso = load_system_driver("GLES", suffix, exact);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700646 if (dso) {
Charlie Lao840bd532020-01-16 17:34:37 -0800647 initialize_api(dso, cnx, EGL | GLESv1_CM | GLESv2);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700648 hnd = new driver_t(dso);
649 return hnd;
650 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700651 dso = load_system_driver("EGL", suffix, exact);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700652 if (dso) {
653 initialize_api(dso, cnx, EGL);
654 hnd = new driver_t(dso);
655
Charlie Lao840bd532020-01-16 17:34:37 -0800656 dso = load_system_driver("GLESv1_CM", suffix, exact);
657 initialize_api(dso, cnx, GLESv1_CM);
658 hnd->set(dso, GLESv1_CM);
659
Peiyong Lin0d10b252019-04-30 18:03:04 -0700660 dso = load_system_driver("GLESv2", suffix, exact);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700661 initialize_api(dso, cnx, GLESv2);
662 hnd->set(dso, GLESv2);
663 }
664 return hnd;
Peiyong Line83b8682019-04-18 17:23:02 -0700665}
666
667void Loader::initialize_api(void* dso, egl_connection_t* cnx, uint32_t mask) {
Mathias Agopiande586972009-05-28 17:39:03 -0700668 if (mask & EGL) {
669 getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress");
670
Jesse Hall94cdba92013-07-11 09:40:35 -0700671 ALOGE_IF(!getProcAddress,
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800672 "can't find eglGetProcAddress() in EGL driver library");
Mathias Agopiande586972009-05-28 17:39:03 -0700673
Mathias Agopian618fa102009-10-14 02:06:37 -0700674 egl_t* egl = &cnx->egl;
Mathias Agopiande586972009-05-28 17:39:03 -0700675 __eglMustCastToProperFunctionPointerType* curr =
676 (__eglMustCastToProperFunctionPointerType*)egl;
677 char const * const * api = egl_names;
678 while (*api) {
679 char const * name = *api;
Jesse Hall94cdba92013-07-11 09:40:35 -0700680 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700681 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700682 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700683 // couldn't find the entry-point, use eglGetProcAddress()
684 f = getProcAddress(name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700685 if (f == nullptr) {
686 f = (__eglMustCastToProperFunctionPointerType)nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700687 }
688 }
689 *curr++ = f;
690 api++;
691 }
692 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700693
Mathias Agopiande586972009-05-28 17:39:03 -0700694 if (mask & GLESv1_CM) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700695 init_api(dso, gl_names_1, gl_names,
Mathias Agopian618fa102009-10-14 02:06:37 -0700696 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800697 &cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl,
Mathias Agopian618fa102009-10-14 02:06:37 -0700698 getProcAddress);
Mathias Agopiande586972009-05-28 17:39:03 -0700699 }
700
701 if (mask & GLESv2) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700702 init_api(dso, gl_names, nullptr,
Mathias Agopian618fa102009-10-14 02:06:37 -0700703 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800704 &cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl,
Mathias Agopiande586972009-05-28 17:39:03 -0700705 getProcAddress);
706 }
Mathias Agopiande586972009-05-28 17:39:03 -0700707}
708
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700709} // namespace android