blob: 2c3ce16f665111f1c8cda0c34778047f989b2a1c [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 Lin9679a982023-04-10 22:00:30 +0000142static const char* HAL_SUBNAME_KEY_PROPERTIES[3] = {
143 "persist.graphics.egl",
144 DRIVER_SUFFIX_PROPERTY,
145 "ro.board.platform",
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700146};
147
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700148static bool should_unload_system_driver(egl_connection_t* cnx) {
149 // Return false if the system driver has been unloaded once.
150 if (cnx->systemDriverUnloaded) {
151 return false;
152 }
153
154 // Return true if Angle namespace is set.
155 android_namespace_t* ns = android::GraphicsEnv::getInstance().getAngleNamespace();
156 if (ns) {
157 return true;
158 }
159
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700160 // Return true if updated driver namespace is set.
161 ns = android::GraphicsEnv::getInstance().getDriverNamespace();
162 if (ns) {
163 return true;
164 }
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700165
166 return false;
167}
168
169static void uninit_api(char const* const* api, __eglMustCastToProperFunctionPointerType* curr) {
170 while (*api) {
171 *curr++ = nullptr;
172 api++;
173 }
174}
175
176void Loader::unload_system_driver(egl_connection_t* cnx) {
177 ATRACE_CALL();
178
179 uninit_api(gl_names,
180 (__eglMustCastToProperFunctionPointerType*)&cnx
181 ->hooks[egl_connection_t::GLESv2_INDEX]
182 ->gl);
183 uninit_api(gl_names,
184 (__eglMustCastToProperFunctionPointerType*)&cnx
185 ->hooks[egl_connection_t::GLESv1_INDEX]
186 ->gl);
187 uninit_api(egl_names, (__eglMustCastToProperFunctionPointerType*)&cnx->egl);
188
189 if (cnx->dso) {
190 ALOGD("Unload system gl driver.");
191 driver_t* hnd = (driver_t*)cnx->dso;
192 if (hnd->dso[2]) {
193 do_android_unload_sphal_library(hnd->dso[2]);
194 }
195 if (hnd->dso[1]) {
196 do_android_unload_sphal_library(hnd->dso[1]);
197 }
198 if (hnd->dso[0]) {
199 do_android_unload_sphal_library(hnd->dso[0]);
200 }
201 cnx->dso = nullptr;
202 }
203
204 cnx->systemDriverUnloaded = true;
205}
206
Mathias Agopianada798b2012-02-13 17:09:30 -0800207void* Loader::open(egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700208{
Jesse Hall1508ae62017-01-19 17:43:26 -0800209 ATRACE_CALL();
Yiwei Zhangd9861812019-02-13 11:51:55 -0800210 const nsecs_t openTime = systemTime();
Jesse Hall1508ae62017-01-19 17:43:26 -0800211
Peiyong Lin9679a982023-04-10 22:00:30 +0000212 if (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
Peiyong Lin9679a982023-04-10 22:00:30 +0000221 // Firstly, try to load ANGLE driver.
222 driver_t* hnd = attempt_to_load_angle(cnx);
Ian Elliott5279f862022-04-21 12:41:03 -0600223
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700224 if (!hnd) {
225 // Secondly, try to load from driver apk.
226 hnd = attempt_to_load_updated_driver(cnx);
227 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700228
229 bool failToLoadFromDriverSuffixProperty = false;
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700230 if (!hnd) {
Peiyong Lindb3ed6e2020-01-09 18:43:27 -0800231 // If updated driver apk is set but fail to load, abort here.
232 if (android::GraphicsEnv::getInstance().getDriverNamespace()) {
233 LOG_ALWAYS_FATAL("couldn't find an OpenGL ES implementation from %s",
234 android::GraphicsEnv::getInstance().getDriverPath().c_str());
235 }
Peiyong Lin98db8e02023-04-05 20:09:16 +0000236 // Finally, try to load system driver.
237 // Start by searching for the library name appended by the system
238 // properties of the GLES userspace driver in both locations.
239 // i.e.:
240 // libGLES_${prop}.so, or:
241 // libEGL_${prop}.so, libGLESv1_CM_${prop}.so, libGLESv2_${prop}.so
242 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
243 auto prop = base::GetProperty(key, "");
244 if (prop.empty()) {
245 continue;
246 }
247 hnd = attempt_to_load_system_driver(cnx, prop.c_str(), true);
248 if (hnd) {
249 break;
250 } else if (strcmp(key, DRIVER_SUFFIX_PROPERTY) == 0) {
251 failToLoadFromDriverSuffixProperty = true;
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700252 }
253 }
254 }
255
256 if (!hnd) {
257 // Can't find graphics driver by appending system properties, now search for the exact name
258 // without any suffix of the GLES userspace driver in both locations.
259 // i.e.:
260 // libGLES.so, or:
261 // libEGL.so, libGLESv1_CM.so, libGLESv2.so
Charlie Lao840bd532020-01-16 17:34:37 -0800262 hnd = attempt_to_load_system_driver(cnx, nullptr, true);
Peiyong Lin0d10b252019-04-30 18:03:04 -0700263 }
264
Peiyong Lin98db8e02023-04-05 20:09:16 +0000265 if (!hnd && !failToLoadFromDriverSuffixProperty &&
266 property_get_int32("ro.vendor.api_level", 0) < __ANDROID_API_U__) {
267 // Still can't find the graphics drivers with the exact name. This time try to use wildcard
268 // matching if the device is launched before Android 14.
Charlie Lao840bd532020-01-16 17:34:37 -0800269 hnd = attempt_to_load_system_driver(cnx, nullptr, false);
Mathias Agopiande586972009-05-28 17:39:03 -0700270 }
271
Yiwei Zhangd9861812019-02-13 11:51:55 -0800272 if (!hnd) {
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700273 android::GraphicsEnv::getInstance().setDriverLoaded(android::GpuStatsInfo::Api::API_GL,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800274 false, systemTime() - openTime);
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600275 } else {
276 // init_angle_backend will check if loaded driver is ANGLE or not,
277 // will set cnx->useAngle appropriately.
278 // Do this here so that we use ANGLE path when driver is ANGLE (e.g. loaded as native),
279 // not just loading ANGLE as option.
Courtney Goeltzenleuchter8ecb10c2020-04-29 13:16:23 -0600280 init_angle_backend(hnd->dso[2], cnx);
Yiwei Zhangd9861812019-02-13 11:51:55 -0800281 }
282
Peiyong Lin3575b9f2019-04-25 14:26:49 -0700283 LOG_ALWAYS_FATAL_IF(!hnd,
Peiyong Lin9679a982023-04-10 22:00:30 +0000284 "couldn't find an OpenGL ES implementation, make sure one of %s, %s and %s "
285 "is set",
286 HAL_SUBNAME_KEY_PROPERTIES[0], HAL_SUBNAME_KEY_PROPERTIES[1],
287 HAL_SUBNAME_KEY_PROPERTIES[2]);
Jesse Hallc07b5202013-07-04 12:08:16 -0700288
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700289 if (!cnx->libEgl) {
290 cnx->libEgl = load_wrapper(EGL_WRAPPER_DIR "/libEGL.so");
291 }
Charlie Lao840bd532020-01-16 17:34:37 -0800292 if (!cnx->libGles1) {
293 cnx->libGles1 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv1_CM.so");
294 }
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700295 if (!cnx->libGles2) {
296 cnx->libGles2 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv2.so");
297 }
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700298
Charlie Lao840bd532020-01-16 17:34:37 -0800299 if (!cnx->libEgl || !cnx->libGles2 || !cnx->libGles1) {
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700300 android::GraphicsEnv::getInstance().setDriverLoaded(android::GpuStatsInfo::Api::API_GL,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800301 false, systemTime() - openTime);
302 }
303
Michael Chockc0ec5e22014-01-27 08:14:33 -0800304 LOG_ALWAYS_FATAL_IF(!cnx->libEgl,
305 "couldn't load system EGL wrapper libraries");
306
Charlie Lao840bd532020-01-16 17:34:37 -0800307 LOG_ALWAYS_FATAL_IF(!cnx->libGles2 || !cnx->libGles1,
308 "couldn't load system OpenGL ES wrapper libraries");
Jesse Hallc07b5202013-07-04 12:08:16 -0700309
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700310 android::GraphicsEnv::getInstance().setDriverLoaded(android::GpuStatsInfo::Api::API_GL, true,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800311 systemTime() - openTime);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800312
Mathias Agopiande586972009-05-28 17:39:03 -0700313 return (void*)hnd;
314}
315
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600316void Loader::close(egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700317{
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600318 driver_t* hnd = (driver_t*) cnx->dso;
Mathias Agopiande586972009-05-28 17:39:03 -0700319 delete hnd;
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600320 cnx->dso = nullptr;
321
Charlie Lao840bd532020-01-16 17:34:37 -0800322 cnx->useAngle = false;
Mathias Agopiande586972009-05-28 17:39:03 -0700323}
324
Jesse Hall94cdba92013-07-11 09:40:35 -0700325void Loader::init_api(void* dso,
326 char const * const * api,
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700327 char const * const * ref_api,
Jesse Hall94cdba92013-07-11 09:40:35 -0700328 __eglMustCastToProperFunctionPointerType* curr,
329 getProcAddressType getProcAddress)
Mathias Agopiande586972009-05-28 17:39:03 -0700330{
Jesse Hall1508ae62017-01-19 17:43:26 -0800331 ATRACE_CALL();
332
Mathias Agopian7773c432012-02-13 20:06:08 -0800333 const ssize_t SIZE = 256;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700334 char scrap[SIZE];
Mathias Agopiande586972009-05-28 17:39:03 -0700335 while (*api) {
336 char const * name = *api;
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700337 if (ref_api) {
338 char const * ref_name = *ref_api;
339 if (std::strcmp(name, ref_name) != 0) {
340 *curr++ = nullptr;
341 ref_api++;
342 continue;
343 }
344 }
345
Jesse Hall94cdba92013-07-11 09:40:35 -0700346 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700347 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700348 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700349 // couldn't find the entry-point, use eglGetProcAddress()
350 f = getProcAddress(name);
351 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700352 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700353 // Try without the OES postfix
354 ssize_t index = ssize_t(strlen(name)) - 3;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700355 if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) {
Mathias Agopiande586972009-05-28 17:39:03 -0700356 strncpy(scrap, name, index);
357 scrap[index] = 0;
358 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000359 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700360 }
361 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700362 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700363 // Try with the OES postfix
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700364 ssize_t index = ssize_t(strlen(name)) - 3;
365 if (index>0 && strcmp(name+index, "OES")) {
366 snprintf(scrap, SIZE, "%sOES", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700367 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000368 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700369 }
370 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700371 if (f == nullptr) {
Steve Block9d453682011-12-20 16:23:08 +0000372 //ALOGD("%s", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700373 f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented;
Mathias Agopian48d438d2012-01-28 21:44:00 -0800374
375 /*
Tao Wu1cdfe642020-06-23 12:33:02 -0700376 * GL_EXT_debug_marker is special, we always report it as
Mathias Agopian48d438d2012-01-28 21:44:00 -0800377 * supported, it's handled by GLES_trace. If GLES_trace is not
378 * enabled, then these are no-ops.
379 */
380 if (!strcmp(name, "glInsertEventMarkerEXT")) {
381 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
382 } else if (!strcmp(name, "glPushGroupMarkerEXT")) {
383 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
384 } else if (!strcmp(name, "glPopGroupMarkerEXT")) {
385 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
386 }
Mathias Agopiande586972009-05-28 17:39:03 -0700387 }
388 *curr++ = f;
389 api++;
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700390 if (ref_api) ref_api++;
Mathias Agopiande586972009-05-28 17:39:03 -0700391 }
392}
393
Peiyong Lin0d10b252019-04-30 18:03:04 -0700394static void* load_system_driver(const char* kind, const char* suffix, const bool exact) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800395 ATRACE_CALL();
Mathias Agopian99381422013-04-23 20:52:29 +0200396 class MatchFile {
397 public:
Peiyong Lin0d10b252019-04-30 18:03:04 -0700398 static std::string find(const char* libraryName, const bool exact) {
Mathias Agopian99381422013-04-23 20:52:29 +0200399 const char* const searchPaths[] = {
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800400#if defined(__LP64__)
401 "/vendor/lib64/egl",
402 "/system/lib64/egl"
403#else
Mathias Agopian99381422013-04-23 20:52:29 +0200404 "/vendor/lib/egl",
405 "/system/lib/egl"
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800406#endif
Mathias Agopian99381422013-04-23 20:52:29 +0200407 };
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700408
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700409 for (auto dir : searchPaths) {
Peiyong Lin0d10b252019-04-30 18:03:04 -0700410 std::string absolutePath;
411 if (find(absolutePath, libraryName, dir, exact)) {
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700412 return absolutePath;
Mathias Agopian99381422013-04-23 20:52:29 +0200413 }
Mathias Agopian99381422013-04-23 20:52:29 +0200414 }
415
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700416 // Driver not found. gah.
417 return std::string();
Mathias Agopian99381422013-04-23 20:52:29 +0200418 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700419 private:
420 static bool find(std::string& result,
421 const std::string& pattern, const char* const search, bool exact) {
422 if (exact) {
423 std::string absolutePath = std::string(search) + "/" + pattern + ".so";
424 if (!access(absolutePath.c_str(), R_OK)) {
425 result = absolutePath;
426 return true;
427 }
428 return false;
429 }
430
431 DIR* d = opendir(search);
432 if (d != nullptr) {
433 struct dirent* e;
434 while ((e = readdir(d)) != nullptr) {
435 if (e->d_type == DT_DIR) {
436 continue;
437 }
438 if (!strcmp(e->d_name, "libGLES_android.so")) {
439 // always skip the software renderer
440 continue;
441 }
442 if (strstr(e->d_name, pattern.c_str()) == e->d_name) {
443 if (!strcmp(e->d_name + strlen(e->d_name) - 3, ".so")) {
444 result = std::string(search) + "/" + e->d_name;
445 closedir(d);
446 return true;
447 }
448 }
449 }
450 closedir(d);
451 }
452 return false;
453 }
Mathias Agopian99381422013-04-23 20:52:29 +0200454 };
455
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700456 std::string libraryName = std::string("lib") + kind;
457 if (suffix) {
458 libraryName += std::string("_") + suffix;
Peiyong Lin0d10b252019-04-30 18:03:04 -0700459 } else if (!exact) {
460 // Deprecated: we look for files that match
461 // libGLES_*.so, or:
462 // libEGL_*.so, libGLESv1_CM_*.so, libGLESv2_*.so
463 libraryName += std::string("_");
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700464 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700465 std::string absolutePath = MatchFile::find(libraryName.c_str(), exact);
Mathias Agopian65421432017-03-08 11:49:05 -0800466 if (absolutePath.empty()) {
Mathias Agopian99381422013-04-23 20:52:29 +0200467 // this happens often, we don't want to log an error
Yi Kong48a6cd22018-07-18 10:07:09 -0700468 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700469 }
Mathias Agopian65421432017-03-08 11:49:05 -0800470 const char* const driver_absolute_path = absolutePath.c_str();
Mathias Agopiande586972009-05-28 17:39:03 -0700471
Jiyong Park5910dc72017-04-05 14:23:52 +0900472 // Try to load drivers from the 'sphal' namespace, if it exist. Fall back to
Justin Yunb7320302017-05-22 15:13:40 +0900473 // the original routine when the namespace does not exist.
Jiyong Park5910dc72017-04-05 14:23:52 +0900474 // See /system/core/rootdir/etc/ld.config.txt for the configuration of the
475 // sphal namespace.
Justin Yunb7320302017-05-22 15:13:40 +0900476 void* dso = do_android_load_sphal_library(driver_absolute_path,
477 RTLD_NOW | RTLD_LOCAL);
Yi Kong48a6cd22018-07-18 10:07:09 -0700478 if (dso == nullptr) {
Mathias Agopian8c173842009-09-20 16:01:02 -0700479 const char* err = dlerror();
Mathias Agopian65421432017-03-08 11:49:05 -0800480 ALOGE("load_driver(%s): %s", driver_absolute_path, err ? err : "unknown");
Yi Kong48a6cd22018-07-18 10:07:09 -0700481 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700482 }
483
Steve Block9d453682011-12-20 16:23:08 +0000484 ALOGD("loaded %s", driver_absolute_path);
Mathias Agopianbaca89c2009-08-20 19:09:34 -0700485
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800486 return dso;
487}
488
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600489static void* load_angle(const char* kind, android_namespace_t* ns) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600490 const android_dlextinfo dlextinfo = {
491 .flags = ANDROID_DLEXT_USE_NAMESPACE,
492 .library_namespace = ns,
493 };
494
495 std::string name = std::string("lib") + kind + "_angle.so";
496
497 void* so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
498
499 if (so) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600500 return so;
501 } else {
502 ALOGE("dlopen_ext(\"%s\") failed: %s", name.c_str(), dlerror());
503 }
504
505 return nullptr;
506}
507
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800508static void* load_updated_driver(const char* kind, android_namespace_t* ns) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800509 ATRACE_CALL();
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800510 const android_dlextinfo dlextinfo = {
511 .flags = ANDROID_DLEXT_USE_NAMESPACE,
512 .library_namespace = ns,
513 };
514 void* so = nullptr;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800515 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
Michael Hoisie4e0f56b2020-04-30 18:40:55 -0400516 auto prop = base::GetProperty(key, "");
517 if (prop.empty()) {
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700518 continue;
519 }
520 std::string name = std::string("lib") + kind + "_" + prop + ".so";
521 so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
522 if (so) {
523 return so;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800524 }
Yiwei Zhang9058b8f2020-11-12 20:23:00 +0000525 ALOGE("Could not load %s from updatable gfx driver namespace: %s.", name.c_str(),
526 dlerror());
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800527 }
528 return nullptr;
529}
530
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700531Loader::driver_t* Loader::attempt_to_load_angle(egl_connection_t* cnx) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800532 ATRACE_CALL();
Yiwei Zhangb22f0862020-04-23 10:40:24 -0700533
534 if (!android::GraphicsEnv::getInstance().shouldUseAngle()) {
535 return nullptr;
536 }
537
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600538 android_namespace_t* ns = android::GraphicsEnv::getInstance().getAngleNamespace();
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700539 if (!ns) {
540 return nullptr;
Cody Northrop6d082ef2018-09-11 09:42:31 -0600541 }
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700542
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700543 android::GraphicsEnv::getInstance().setDriverToLoad(android::GpuStatsInfo::Driver::ANGLE);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700544 driver_t* hnd = nullptr;
545
546 // ANGLE doesn't ship with GLES library, and thus we skip GLES driver.
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600547 void* dso = load_angle("EGL", ns);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700548 if (dso) {
549 initialize_api(dso, cnx, EGL);
550 hnd = new driver_t(dso);
551
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600552 dso = load_angle("GLESv1_CM", ns);
Charlie Lao840bd532020-01-16 17:34:37 -0800553 initialize_api(dso, cnx, GLESv1_CM);
554 hnd->set(dso, GLESv1_CM);
555
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600556 dso = load_angle("GLESv2", ns);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700557 initialize_api(dso, cnx, GLESv2);
558 hnd->set(dso, GLESv2);
559 }
560 return hnd;
561}
562
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600563void Loader::init_angle_backend(void* dso, egl_connection_t* cnx) {
Courtney Goeltzenleuchter8ecb10c2020-04-29 13:16:23 -0600564 void* pANGLEGetDisplayPlatform = dlsym(dso, "ANGLEGetDisplayPlatform");
565 if (pANGLEGetDisplayPlatform) {
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600566 ALOGV("ANGLE GLES library in use");
567 cnx->useAngle = true;
568 } else {
569 ALOGV("Native GLES library in use");
570 cnx->useAngle = false;
571 }
572}
573
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700574Loader::driver_t* Loader::attempt_to_load_updated_driver(egl_connection_t* cnx) {
575 ATRACE_CALL();
Yiwei Zhangd40aaac2020-06-04 05:26:56 -0700576
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700577 android_namespace_t* ns = android::GraphicsEnv::getInstance().getDriverNamespace();
578 if (!ns) {
579 return nullptr;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800580 }
581
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700582 ALOGD("Load updated gl driver.");
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700583 android::GraphicsEnv::getInstance().setDriverToLoad(android::GpuStatsInfo::Driver::GL_UPDATED);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700584 driver_t* hnd = nullptr;
585 void* dso = load_updated_driver("GLES", ns);
Peiyong Line83b8682019-04-18 17:23:02 -0700586 if (dso) {
Charlie Lao840bd532020-01-16 17:34:37 -0800587 initialize_api(dso, cnx, EGL | GLESv1_CM | GLESv2);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700588 hnd = new driver_t(dso);
589 return hnd;
Peiyong Line83b8682019-04-18 17:23:02 -0700590 }
591
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700592 dso = load_updated_driver("EGL", ns);
593 if (dso) {
594 initialize_api(dso, cnx, EGL);
595 hnd = new driver_t(dso);
596
Charlie Lao840bd532020-01-16 17:34:37 -0800597 dso = load_updated_driver("GLESv1_CM", ns);
598 initialize_api(dso, cnx, GLESv1_CM);
599 hnd->set(dso, GLESv1_CM);
600
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700601 dso = load_updated_driver("GLESv2", ns);
602 initialize_api(dso, cnx, GLESv2);
603 hnd->set(dso, GLESv2);
604 }
605 return hnd;
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700606}
607
Peiyong Lin0d10b252019-04-30 18:03:04 -0700608Loader::driver_t* Loader::attempt_to_load_system_driver(egl_connection_t* cnx, const char* suffix,
609 const bool exact) {
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700610 ATRACE_CALL();
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700611 android::GraphicsEnv::getInstance().setDriverToLoad(android::GpuStatsInfo::Driver::GL);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700612 driver_t* hnd = nullptr;
Peiyong Lin0d10b252019-04-30 18:03:04 -0700613 void* dso = load_system_driver("GLES", suffix, exact);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700614 if (dso) {
Charlie Lao840bd532020-01-16 17:34:37 -0800615 initialize_api(dso, cnx, EGL | GLESv1_CM | GLESv2);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700616 hnd = new driver_t(dso);
617 return hnd;
618 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700619 dso = load_system_driver("EGL", suffix, exact);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700620 if (dso) {
621 initialize_api(dso, cnx, EGL);
622 hnd = new driver_t(dso);
623
Charlie Lao840bd532020-01-16 17:34:37 -0800624 dso = load_system_driver("GLESv1_CM", suffix, exact);
625 initialize_api(dso, cnx, GLESv1_CM);
626 hnd->set(dso, GLESv1_CM);
627
Peiyong Lin0d10b252019-04-30 18:03:04 -0700628 dso = load_system_driver("GLESv2", suffix, exact);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700629 initialize_api(dso, cnx, GLESv2);
630 hnd->set(dso, GLESv2);
631 }
632 return hnd;
Peiyong Line83b8682019-04-18 17:23:02 -0700633}
634
635void Loader::initialize_api(void* dso, egl_connection_t* cnx, uint32_t mask) {
Mathias Agopiande586972009-05-28 17:39:03 -0700636 if (mask & EGL) {
637 getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress");
638
Jesse Hall94cdba92013-07-11 09:40:35 -0700639 ALOGE_IF(!getProcAddress,
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800640 "can't find eglGetProcAddress() in EGL driver library");
Mathias Agopiande586972009-05-28 17:39:03 -0700641
Mathias Agopian618fa102009-10-14 02:06:37 -0700642 egl_t* egl = &cnx->egl;
Mathias Agopiande586972009-05-28 17:39:03 -0700643 __eglMustCastToProperFunctionPointerType* curr =
644 (__eglMustCastToProperFunctionPointerType*)egl;
645 char const * const * api = egl_names;
646 while (*api) {
647 char const * name = *api;
Jesse Hall94cdba92013-07-11 09:40:35 -0700648 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700649 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700650 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700651 // couldn't find the entry-point, use eglGetProcAddress()
652 f = getProcAddress(name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700653 if (f == nullptr) {
654 f = (__eglMustCastToProperFunctionPointerType)nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700655 }
656 }
657 *curr++ = f;
658 api++;
659 }
660 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700661
Mathias Agopiande586972009-05-28 17:39:03 -0700662 if (mask & GLESv1_CM) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700663 init_api(dso, gl_names_1, gl_names,
Mathias Agopian618fa102009-10-14 02:06:37 -0700664 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800665 &cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl,
Mathias Agopian618fa102009-10-14 02:06:37 -0700666 getProcAddress);
Mathias Agopiande586972009-05-28 17:39:03 -0700667 }
668
669 if (mask & GLESv2) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700670 init_api(dso, gl_names, nullptr,
Mathias Agopian618fa102009-10-14 02:06:37 -0700671 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800672 &cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl,
Mathias Agopiande586972009-05-28 17:39:03 -0700673 getProcAddress);
674 }
Mathias Agopiande586972009-05-28 17:39:03 -0700675}
676
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700677} // namespace android