blob: 415e8eab7040f31a80d028a0870e9d8cd9fd6bb2 [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:
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 Lin0d10b252019-04-30 18:03:04 -0700140static const char* DRIVER_SUFFIX_PROPERTY = "ro.hardware.egl";
141
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700142static const char* HAL_SUBNAME_KEY_PROPERTIES[2] = {
Peiyong Lin0d10b252019-04-30 18:03:04 -0700143 DRIVER_SUFFIX_PROPERTY,
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700144 "ro.board.platform",
145};
146
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700147static bool should_unload_system_driver(egl_connection_t* cnx) {
148 // Return false if the system driver has been unloaded once.
149 if (cnx->systemDriverUnloaded) {
150 return false;
151 }
152
153 // Return true if Angle namespace is set.
154 android_namespace_t* ns = android::GraphicsEnv::getInstance().getAngleNamespace();
155 if (ns) {
156 return true;
157 }
158
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700159 // Return true if updated driver namespace is set.
160 ns = android::GraphicsEnv::getInstance().getDriverNamespace();
161 if (ns) {
162 return true;
163 }
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700164
165 return false;
166}
167
168static void uninit_api(char const* const* api, __eglMustCastToProperFunctionPointerType* curr) {
169 while (*api) {
170 *curr++ = nullptr;
171 api++;
172 }
173}
174
175void Loader::unload_system_driver(egl_connection_t* cnx) {
176 ATRACE_CALL();
177
178 uninit_api(gl_names,
179 (__eglMustCastToProperFunctionPointerType*)&cnx
180 ->hooks[egl_connection_t::GLESv2_INDEX]
181 ->gl);
182 uninit_api(gl_names,
183 (__eglMustCastToProperFunctionPointerType*)&cnx
184 ->hooks[egl_connection_t::GLESv1_INDEX]
185 ->gl);
186 uninit_api(egl_names, (__eglMustCastToProperFunctionPointerType*)&cnx->egl);
187
188 if (cnx->dso) {
189 ALOGD("Unload system gl driver.");
190 driver_t* hnd = (driver_t*)cnx->dso;
191 if (hnd->dso[2]) {
192 do_android_unload_sphal_library(hnd->dso[2]);
193 }
194 if (hnd->dso[1]) {
195 do_android_unload_sphal_library(hnd->dso[1]);
196 }
197 if (hnd->dso[0]) {
198 do_android_unload_sphal_library(hnd->dso[0]);
199 }
200 cnx->dso = nullptr;
201 }
202
203 cnx->systemDriverUnloaded = true;
204}
205
Mathias Agopianada798b2012-02-13 17:09:30 -0800206void* Loader::open(egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700207{
Jesse Hall1508ae62017-01-19 17:43:26 -0800208 ATRACE_CALL();
Yiwei Zhangd9861812019-02-13 11:51:55 -0800209 const nsecs_t openTime = systemTime();
Jesse Hall1508ae62017-01-19 17:43:26 -0800210
Ian Elliott5279f862022-04-21 12:41:03 -0600211 if (!android::GraphicsEnv::getInstance().angleIsSystemDriver() &&
212 should_unload_system_driver(cnx)) {
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700213 unload_system_driver(cnx);
214 }
215
216 // If a driver has been loaded, return the driver directly.
217 if (cnx->dso) {
218 return cnx->dso;
219 }
220
Ian Elliott5279f862022-04-21 12:41:03 -0600221 // Firstly, try to load ANGLE driver, unless we know that we shouldn't.
222 bool shouldForceLegacyDriver = android::GraphicsEnv::getInstance().shouldForceLegacyDriver();
223 driver_t* hnd = nullptr;
224 if (!shouldForceLegacyDriver) {
225 hnd = attempt_to_load_angle(cnx);
226 }
227
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700228 if (!hnd) {
229 // Secondly, try to load from driver apk.
230 hnd = attempt_to_load_updated_driver(cnx);
231 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700232
233 bool failToLoadFromDriverSuffixProperty = false;
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700234 if (!hnd) {
Peiyong Lindb3ed6e2020-01-09 18:43:27 -0800235 // If updated driver apk is set but fail to load, abort here.
236 if (android::GraphicsEnv::getInstance().getDriverNamespace()) {
237 LOG_ALWAYS_FATAL("couldn't find an OpenGL ES implementation from %s",
238 android::GraphicsEnv::getInstance().getDriverPath().c_str());
239 }
Peiyong Lin98db8e02023-04-05 20:09:16 +0000240 // Finally, try to load system driver.
241 // Start by searching for the library name appended by the system
242 // properties of the GLES userspace driver in both locations.
243 // i.e.:
244 // libGLES_${prop}.so, or:
245 // libEGL_${prop}.so, libGLESv1_CM_${prop}.so, libGLESv2_${prop}.so
246 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
247 auto prop = base::GetProperty(key, "");
248 if (prop.empty()) {
249 continue;
250 }
251 hnd = attempt_to_load_system_driver(cnx, prop.c_str(), true);
252 if (hnd) {
253 break;
254 } else if (strcmp(key, DRIVER_SUFFIX_PROPERTY) == 0) {
255 failToLoadFromDriverSuffixProperty = true;
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700256 }
257 }
258 }
259
260 if (!hnd) {
261 // Can't find graphics driver by appending system properties, now search for the exact name
262 // without any suffix of the GLES userspace driver in both locations.
263 // i.e.:
264 // libGLES.so, or:
265 // libEGL.so, libGLESv1_CM.so, libGLESv2.so
Charlie Lao840bd532020-01-16 17:34:37 -0800266 hnd = attempt_to_load_system_driver(cnx, nullptr, true);
Peiyong Lin0d10b252019-04-30 18:03:04 -0700267 }
268
Peiyong Lin98db8e02023-04-05 20:09:16 +0000269 if (!hnd && !failToLoadFromDriverSuffixProperty &&
270 property_get_int32("ro.vendor.api_level", 0) < __ANDROID_API_U__) {
271 // Still can't find the graphics drivers with the exact name. This time try to use wildcard
272 // matching if the device is launched before Android 14.
Charlie Lao840bd532020-01-16 17:34:37 -0800273 hnd = attempt_to_load_system_driver(cnx, nullptr, false);
Mathias Agopiande586972009-05-28 17:39:03 -0700274 }
275
Yiwei Zhangd9861812019-02-13 11:51:55 -0800276 if (!hnd) {
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700277 android::GraphicsEnv::getInstance().setDriverLoaded(android::GpuStatsInfo::Api::API_GL,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800278 false, systemTime() - openTime);
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600279 } else {
280 // init_angle_backend will check if loaded driver is ANGLE or not,
281 // will set cnx->useAngle appropriately.
282 // Do this here so that we use ANGLE path when driver is ANGLE (e.g. loaded as native),
283 // not just loading ANGLE as option.
Courtney Goeltzenleuchter8ecb10c2020-04-29 13:16:23 -0600284 init_angle_backend(hnd->dso[2], cnx);
Yiwei Zhangd9861812019-02-13 11:51:55 -0800285 }
286
Peiyong Lin3575b9f2019-04-25 14:26:49 -0700287 LOG_ALWAYS_FATAL_IF(!hnd,
288 "couldn't find an OpenGL ES implementation, make sure you set %s or %s",
289 HAL_SUBNAME_KEY_PROPERTIES[0], HAL_SUBNAME_KEY_PROPERTIES[1]);
Jesse Hallc07b5202013-07-04 12:08:16 -0700290
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700291 if (!cnx->libEgl) {
292 cnx->libEgl = load_wrapper(EGL_WRAPPER_DIR "/libEGL.so");
293 }
Charlie Lao840bd532020-01-16 17:34:37 -0800294 if (!cnx->libGles1) {
295 cnx->libGles1 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv1_CM.so");
296 }
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700297 if (!cnx->libGles2) {
298 cnx->libGles2 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv2.so");
299 }
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700300
Charlie Lao840bd532020-01-16 17:34:37 -0800301 if (!cnx->libEgl || !cnx->libGles2 || !cnx->libGles1) {
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700302 android::GraphicsEnv::getInstance().setDriverLoaded(android::GpuStatsInfo::Api::API_GL,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800303 false, systemTime() - openTime);
304 }
305
Michael Chockc0ec5e22014-01-27 08:14:33 -0800306 LOG_ALWAYS_FATAL_IF(!cnx->libEgl,
307 "couldn't load system EGL wrapper libraries");
308
Charlie Lao840bd532020-01-16 17:34:37 -0800309 LOG_ALWAYS_FATAL_IF(!cnx->libGles2 || !cnx->libGles1,
310 "couldn't load system OpenGL ES wrapper libraries");
Jesse Hallc07b5202013-07-04 12:08:16 -0700311
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700312 android::GraphicsEnv::getInstance().setDriverLoaded(android::GpuStatsInfo::Api::API_GL, true,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800313 systemTime() - openTime);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800314
Mathias Agopiande586972009-05-28 17:39:03 -0700315 return (void*)hnd;
316}
317
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600318void Loader::close(egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700319{
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600320 driver_t* hnd = (driver_t*) cnx->dso;
Mathias Agopiande586972009-05-28 17:39:03 -0700321 delete hnd;
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600322 cnx->dso = nullptr;
323
Charlie Lao840bd532020-01-16 17:34:37 -0800324 cnx->useAngle = false;
Mathias Agopiande586972009-05-28 17:39:03 -0700325}
326
Jesse Hall94cdba92013-07-11 09:40:35 -0700327void Loader::init_api(void* dso,
328 char const * const * api,
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700329 char const * const * ref_api,
Jesse Hall94cdba92013-07-11 09:40:35 -0700330 __eglMustCastToProperFunctionPointerType* curr,
331 getProcAddressType getProcAddress)
Mathias Agopiande586972009-05-28 17:39:03 -0700332{
Jesse Hall1508ae62017-01-19 17:43:26 -0800333 ATRACE_CALL();
334
Mathias Agopian7773c432012-02-13 20:06:08 -0800335 const ssize_t SIZE = 256;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700336 char scrap[SIZE];
Mathias Agopiande586972009-05-28 17:39:03 -0700337 while (*api) {
338 char const * name = *api;
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700339 if (ref_api) {
340 char const * ref_name = *ref_api;
341 if (std::strcmp(name, ref_name) != 0) {
342 *curr++ = nullptr;
343 ref_api++;
344 continue;
345 }
346 }
347
Jesse Hall94cdba92013-07-11 09:40:35 -0700348 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700349 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700350 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700351 // couldn't find the entry-point, use eglGetProcAddress()
352 f = getProcAddress(name);
353 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700354 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700355 // Try without the OES postfix
356 ssize_t index = ssize_t(strlen(name)) - 3;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700357 if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) {
Mathias Agopiande586972009-05-28 17:39:03 -0700358 strncpy(scrap, name, index);
359 scrap[index] = 0;
360 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000361 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700362 }
363 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700364 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700365 // Try with the OES postfix
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700366 ssize_t index = ssize_t(strlen(name)) - 3;
367 if (index>0 && strcmp(name+index, "OES")) {
368 snprintf(scrap, SIZE, "%sOES", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700369 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000370 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700371 }
372 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700373 if (f == nullptr) {
Steve Block9d453682011-12-20 16:23:08 +0000374 //ALOGD("%s", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700375 f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented;
Mathias Agopian48d438d2012-01-28 21:44:00 -0800376
377 /*
Tao Wu1cdfe642020-06-23 12:33:02 -0700378 * GL_EXT_debug_marker is special, we always report it as
Mathias Agopian48d438d2012-01-28 21:44:00 -0800379 * supported, it's handled by GLES_trace. If GLES_trace is not
380 * enabled, then these are no-ops.
381 */
382 if (!strcmp(name, "glInsertEventMarkerEXT")) {
383 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
384 } else if (!strcmp(name, "glPushGroupMarkerEXT")) {
385 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
386 } else if (!strcmp(name, "glPopGroupMarkerEXT")) {
387 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
388 }
Mathias Agopiande586972009-05-28 17:39:03 -0700389 }
390 *curr++ = f;
391 api++;
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700392 if (ref_api) ref_api++;
Mathias Agopiande586972009-05-28 17:39:03 -0700393 }
394}
395
Peiyong Lin0d10b252019-04-30 18:03:04 -0700396static void* load_system_driver(const char* kind, const char* suffix, const bool exact) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800397 ATRACE_CALL();
Mathias Agopian99381422013-04-23 20:52:29 +0200398 class MatchFile {
399 public:
Peiyong Lin0d10b252019-04-30 18:03:04 -0700400 static std::string find(const char* libraryName, const bool exact) {
Mathias Agopian99381422013-04-23 20:52:29 +0200401 const char* const searchPaths[] = {
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800402#if defined(__LP64__)
403 "/vendor/lib64/egl",
404 "/system/lib64/egl"
405#else
Mathias Agopian99381422013-04-23 20:52:29 +0200406 "/vendor/lib/egl",
407 "/system/lib/egl"
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800408#endif
Mathias Agopian99381422013-04-23 20:52:29 +0200409 };
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700410
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700411 for (auto dir : searchPaths) {
Peiyong Lin0d10b252019-04-30 18:03:04 -0700412 std::string absolutePath;
413 if (find(absolutePath, libraryName, dir, exact)) {
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700414 return absolutePath;
Mathias Agopian99381422013-04-23 20:52:29 +0200415 }
Mathias Agopian99381422013-04-23 20:52:29 +0200416 }
417
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700418 // Driver not found. gah.
419 return std::string();
Mathias Agopian99381422013-04-23 20:52:29 +0200420 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700421 private:
422 static bool find(std::string& result,
423 const std::string& pattern, const char* const search, bool exact) {
424 if (exact) {
425 std::string absolutePath = std::string(search) + "/" + pattern + ".so";
426 if (!access(absolutePath.c_str(), R_OK)) {
427 result = absolutePath;
428 return true;
429 }
430 return false;
431 }
432
433 DIR* d = opendir(search);
434 if (d != nullptr) {
435 struct dirent* e;
436 while ((e = readdir(d)) != nullptr) {
437 if (e->d_type == DT_DIR) {
438 continue;
439 }
440 if (!strcmp(e->d_name, "libGLES_android.so")) {
441 // always skip the software renderer
442 continue;
443 }
444 if (strstr(e->d_name, pattern.c_str()) == e->d_name) {
445 if (!strcmp(e->d_name + strlen(e->d_name) - 3, ".so")) {
446 result = std::string(search) + "/" + e->d_name;
447 closedir(d);
448 return true;
449 }
450 }
451 }
452 closedir(d);
453 }
454 return false;
455 }
Mathias Agopian99381422013-04-23 20:52:29 +0200456 };
457
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700458 std::string libraryName = std::string("lib") + kind;
459 if (suffix) {
460 libraryName += std::string("_") + suffix;
Peiyong Lin0d10b252019-04-30 18:03:04 -0700461 } else if (!exact) {
462 // Deprecated: we look for files that match
463 // libGLES_*.so, or:
464 // libEGL_*.so, libGLESv1_CM_*.so, libGLESv2_*.so
465 libraryName += std::string("_");
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700466 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700467 std::string absolutePath = MatchFile::find(libraryName.c_str(), exact);
Mathias Agopian65421432017-03-08 11:49:05 -0800468 if (absolutePath.empty()) {
Mathias Agopian99381422013-04-23 20:52:29 +0200469 // this happens often, we don't want to log an error
Yi Kong48a6cd22018-07-18 10:07:09 -0700470 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700471 }
Mathias Agopian65421432017-03-08 11:49:05 -0800472 const char* const driver_absolute_path = absolutePath.c_str();
Mathias Agopiande586972009-05-28 17:39:03 -0700473
Jiyong Park5910dc72017-04-05 14:23:52 +0900474 // Try to load drivers from the 'sphal' namespace, if it exist. Fall back to
Justin Yunb7320302017-05-22 15:13:40 +0900475 // the original routine when the namespace does not exist.
Jiyong Park5910dc72017-04-05 14:23:52 +0900476 // See /system/core/rootdir/etc/ld.config.txt for the configuration of the
477 // sphal namespace.
Justin Yunb7320302017-05-22 15:13:40 +0900478 void* dso = do_android_load_sphal_library(driver_absolute_path,
479 RTLD_NOW | RTLD_LOCAL);
Yi Kong48a6cd22018-07-18 10:07:09 -0700480 if (dso == nullptr) {
Mathias Agopian8c173842009-09-20 16:01:02 -0700481 const char* err = dlerror();
Mathias Agopian65421432017-03-08 11:49:05 -0800482 ALOGE("load_driver(%s): %s", driver_absolute_path, err ? err : "unknown");
Yi Kong48a6cd22018-07-18 10:07:09 -0700483 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700484 }
485
Steve Block9d453682011-12-20 16:23:08 +0000486 ALOGD("loaded %s", driver_absolute_path);
Mathias Agopianbaca89c2009-08-20 19:09:34 -0700487
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800488 return dso;
489}
490
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600491static void* load_angle(const char* kind, android_namespace_t* ns) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600492 const android_dlextinfo dlextinfo = {
493 .flags = ANDROID_DLEXT_USE_NAMESPACE,
494 .library_namespace = ns,
495 };
496
497 std::string name = std::string("lib") + kind + "_angle.so";
498
499 void* so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
500
501 if (so) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600502 return so;
503 } else {
504 ALOGE("dlopen_ext(\"%s\") failed: %s", name.c_str(), dlerror());
505 }
506
507 return nullptr;
508}
509
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800510static void* load_updated_driver(const char* kind, android_namespace_t* ns) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800511 ATRACE_CALL();
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800512 const android_dlextinfo dlextinfo = {
513 .flags = ANDROID_DLEXT_USE_NAMESPACE,
514 .library_namespace = ns,
515 };
516 void* so = nullptr;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800517 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
Michael Hoisie4e0f56b2020-04-30 18:40:55 -0400518 auto prop = base::GetProperty(key, "");
519 if (prop.empty()) {
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700520 continue;
521 }
522 std::string name = std::string("lib") + kind + "_" + prop + ".so";
523 so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
524 if (so) {
525 return so;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800526 }
Yiwei Zhang9058b8f2020-11-12 20:23:00 +0000527 ALOGE("Could not load %s from updatable gfx driver namespace: %s.", name.c_str(),
528 dlerror());
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800529 }
530 return nullptr;
531}
532
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700533Loader::driver_t* Loader::attempt_to_load_angle(egl_connection_t* cnx) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800534 ATRACE_CALL();
Yiwei Zhangb22f0862020-04-23 10:40:24 -0700535
536 if (!android::GraphicsEnv::getInstance().shouldUseAngle()) {
537 return nullptr;
538 }
539
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600540 android_namespace_t* ns = android::GraphicsEnv::getInstance().getAngleNamespace();
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700541 if (!ns) {
542 return nullptr;
Cody Northrop6d082ef2018-09-11 09:42:31 -0600543 }
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700544
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700545 android::GraphicsEnv::getInstance().setDriverToLoad(android::GpuStatsInfo::Driver::ANGLE);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700546 driver_t* hnd = nullptr;
547
548 // ANGLE doesn't ship with GLES library, and thus we skip GLES driver.
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600549 void* dso = load_angle("EGL", ns);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700550 if (dso) {
551 initialize_api(dso, cnx, EGL);
552 hnd = new driver_t(dso);
553
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600554 dso = load_angle("GLESv1_CM", ns);
Charlie Lao840bd532020-01-16 17:34:37 -0800555 initialize_api(dso, cnx, GLESv1_CM);
556 hnd->set(dso, GLESv1_CM);
557
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600558 dso = load_angle("GLESv2", ns);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700559 initialize_api(dso, cnx, GLESv2);
560 hnd->set(dso, GLESv2);
561 }
562 return hnd;
563}
564
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600565void Loader::init_angle_backend(void* dso, egl_connection_t* cnx) {
Courtney Goeltzenleuchter8ecb10c2020-04-29 13:16:23 -0600566 void* pANGLEGetDisplayPlatform = dlsym(dso, "ANGLEGetDisplayPlatform");
567 if (pANGLEGetDisplayPlatform) {
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600568 ALOGV("ANGLE GLES library in use");
569 cnx->useAngle = true;
570 } else {
571 ALOGV("Native GLES library in use");
572 cnx->useAngle = false;
573 }
574}
575
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700576Loader::driver_t* Loader::attempt_to_load_updated_driver(egl_connection_t* cnx) {
577 ATRACE_CALL();
Yiwei Zhangd40aaac2020-06-04 05:26:56 -0700578
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700579 android_namespace_t* ns = android::GraphicsEnv::getInstance().getDriverNamespace();
580 if (!ns) {
581 return nullptr;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800582 }
583
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700584 ALOGD("Load updated gl driver.");
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700585 android::GraphicsEnv::getInstance().setDriverToLoad(android::GpuStatsInfo::Driver::GL_UPDATED);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700586 driver_t* hnd = nullptr;
587 void* dso = load_updated_driver("GLES", ns);
Peiyong Line83b8682019-04-18 17:23:02 -0700588 if (dso) {
Charlie Lao840bd532020-01-16 17:34:37 -0800589 initialize_api(dso, cnx, EGL | GLESv1_CM | GLESv2);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700590 hnd = new driver_t(dso);
591 return hnd;
Peiyong Line83b8682019-04-18 17:23:02 -0700592 }
593
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700594 dso = load_updated_driver("EGL", ns);
595 if (dso) {
596 initialize_api(dso, cnx, EGL);
597 hnd = new driver_t(dso);
598
Charlie Lao840bd532020-01-16 17:34:37 -0800599 dso = load_updated_driver("GLESv1_CM", ns);
600 initialize_api(dso, cnx, GLESv1_CM);
601 hnd->set(dso, GLESv1_CM);
602
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700603 dso = load_updated_driver("GLESv2", ns);
604 initialize_api(dso, cnx, GLESv2);
605 hnd->set(dso, GLESv2);
606 }
607 return hnd;
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700608}
609
Peiyong Lin0d10b252019-04-30 18:03:04 -0700610Loader::driver_t* Loader::attempt_to_load_system_driver(egl_connection_t* cnx, const char* suffix,
611 const bool exact) {
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700612 ATRACE_CALL();
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700613 android::GraphicsEnv::getInstance().setDriverToLoad(android::GpuStatsInfo::Driver::GL);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700614 driver_t* hnd = nullptr;
Peiyong Lin0d10b252019-04-30 18:03:04 -0700615 void* dso = load_system_driver("GLES", suffix, exact);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700616 if (dso) {
Charlie Lao840bd532020-01-16 17:34:37 -0800617 initialize_api(dso, cnx, EGL | GLESv1_CM | GLESv2);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700618 hnd = new driver_t(dso);
619 return hnd;
620 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700621 dso = load_system_driver("EGL", suffix, exact);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700622 if (dso) {
623 initialize_api(dso, cnx, EGL);
624 hnd = new driver_t(dso);
625
Charlie Lao840bd532020-01-16 17:34:37 -0800626 dso = load_system_driver("GLESv1_CM", suffix, exact);
627 initialize_api(dso, cnx, GLESv1_CM);
628 hnd->set(dso, GLESv1_CM);
629
Peiyong Lin0d10b252019-04-30 18:03:04 -0700630 dso = load_system_driver("GLESv2", suffix, exact);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700631 initialize_api(dso, cnx, GLESv2);
632 hnd->set(dso, GLESv2);
633 }
634 return hnd;
Peiyong Line83b8682019-04-18 17:23:02 -0700635}
636
637void Loader::initialize_api(void* dso, egl_connection_t* cnx, uint32_t mask) {
Mathias Agopiande586972009-05-28 17:39:03 -0700638 if (mask & EGL) {
639 getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress");
640
Jesse Hall94cdba92013-07-11 09:40:35 -0700641 ALOGE_IF(!getProcAddress,
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800642 "can't find eglGetProcAddress() in EGL driver library");
Mathias Agopiande586972009-05-28 17:39:03 -0700643
Mathias Agopian618fa102009-10-14 02:06:37 -0700644 egl_t* egl = &cnx->egl;
Mathias Agopiande586972009-05-28 17:39:03 -0700645 __eglMustCastToProperFunctionPointerType* curr =
646 (__eglMustCastToProperFunctionPointerType*)egl;
647 char const * const * api = egl_names;
648 while (*api) {
649 char const * name = *api;
Jesse Hall94cdba92013-07-11 09:40:35 -0700650 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700651 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700652 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700653 // couldn't find the entry-point, use eglGetProcAddress()
654 f = getProcAddress(name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700655 if (f == nullptr) {
656 f = (__eglMustCastToProperFunctionPointerType)nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700657 }
658 }
659 *curr++ = f;
660 api++;
661 }
662 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700663
Mathias Agopiande586972009-05-28 17:39:03 -0700664 if (mask & GLESv1_CM) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700665 init_api(dso, gl_names_1, gl_names,
Mathias Agopian618fa102009-10-14 02:06:37 -0700666 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800667 &cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl,
Mathias Agopian618fa102009-10-14 02:06:37 -0700668 getProcAddress);
Mathias Agopiande586972009-05-28 17:39:03 -0700669 }
670
671 if (mask & GLESv2) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700672 init_api(dso, gl_names, nullptr,
Mathias Agopian618fa102009-10-14 02:06:37 -0700673 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800674 &cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl,
Mathias Agopiande586972009-05-28 17:39:03 -0700675 getProcAddress);
676 }
Mathias Agopiande586972009-05-28 17:39:03 -0700677}
678
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700679} // namespace android