blob: 6cfb608dd5e800a0f53e916b949dfcaaf571c13c [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>
Yiwei Zhangd40aaac2020-06-04 05:26:56 -070024#include <dirent.h>
25#include <dlfcn.h>
26#include <graphicsenv/GraphicsEnv.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070027#include <log/log.h>
Yiwei Zhangd9861812019-02-13 11:51:55 -080028#include <utils/Timers.h>
Justin Yunb7320302017-05-22 15:13:40 +090029#include <vndksupport/linker.h>
Mathias Agopiande586972009-05-28 17:39:03 -070030
Yiwei Zhangd40aaac2020-06-04 05:26:56 -070031#include <string>
32
Yiwei Zhang8af003e2020-06-04 14:11:30 -070033#include "EGL/eglext_angle.h"
Cody Northrop68d10352018-10-15 07:22:09 -060034#include "egl_platform_entries.h"
Mathias Agopian65421432017-03-08 11:49:05 -080035#include "egl_trace.h"
Mathias Agopian1cadb252011-05-23 17:26:14 -070036#include "egldefs.h"
Mathias Agopiande586972009-05-28 17:39:03 -070037
Mathias Agopiande586972009-05-28 17:39:03 -070038namespace android {
Mathias Agopiande586972009-05-28 17:39:03 -070039
40/*
Mathias Agopian99381422013-04-23 20:52:29 +020041 * EGL userspace drivers must be provided either:
42 * - as a single library:
43 * /vendor/lib/egl/libGLES.so
44 *
45 * - as separate libraries:
46 * /vendor/lib/egl/libEGL.so
47 * /vendor/lib/egl/libGLESv1_CM.so
48 * /vendor/lib/egl/libGLESv2.so
49 *
Mathias Agopian99381422013-04-23 20:52:29 +020050 * For backward compatibility and to facilitate the transition to
51 * this new naming scheme, the loader will additionally look for:
Jesse Hall94cdba92013-07-11 09:40:35 -070052 *
Mathias Agopian99381422013-04-23 20:52:29 +020053 * /{vendor|system}/lib/egl/lib{GLES | [EGL|GLESv1_CM|GLESv2]}_*.so
Jesse Hall94cdba92013-07-11 09:40:35 -070054 *
Mathias Agopiande586972009-05-28 17:39:03 -070055 */
56
Mathias Agopian65421432017-03-08 11:49:05 -080057Loader& Loader::getInstance() {
58 static Loader loader;
59 return loader;
60}
Mathias Agopiande586972009-05-28 17:39:03 -070061
Jesse Hall1508ae62017-01-19 17:43:26 -080062static void* do_dlopen(const char* path, int mode) {
63 ATRACE_CALL();
64 return dlopen(path, mode);
65}
66
Jiyong Park5910dc72017-04-05 14:23:52 +090067static void* do_android_dlopen_ext(const char* path, int mode, const android_dlextinfo* info) {
68 ATRACE_CALL();
69 return android_dlopen_ext(path, mode, info);
70}
71
Justin Yunb7320302017-05-22 15:13:40 +090072static void* do_android_load_sphal_library(const char* path, int mode) {
73 ATRACE_CALL();
74 return android_load_sphal_library(path, mode);
75}
76
Yiwei Zhang5e21eb32019-06-05 00:26:03 -070077static int do_android_unload_sphal_library(void* dso) {
78 ATRACE_CALL();
79 return android_unload_sphal_library(dso);
80}
81
Jesse Hall94cdba92013-07-11 09:40:35 -070082Loader::driver_t::driver_t(void* gles)
Mathias Agopiande586972009-05-28 17:39:03 -070083{
84 dso[0] = gles;
85 for (size_t i=1 ; i<NELEM(dso) ; i++)
Yi Kong48a6cd22018-07-18 10:07:09 -070086 dso[i] = nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -070087}
88
Jesse Hall94cdba92013-07-11 09:40:35 -070089Loader::driver_t::~driver_t()
Mathias Agopiande586972009-05-28 17:39:03 -070090{
91 for (size_t i=0 ; i<NELEM(dso) ; i++) {
92 if (dso[i]) {
93 dlclose(dso[i]);
Yi Kong48a6cd22018-07-18 10:07:09 -070094 dso[i] = nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -070095 }
96 }
97}
98
Mathias Agopian65421432017-03-08 11:49:05 -080099int Loader::driver_t::set(void* hnd, int32_t api)
Mathias Agopiande586972009-05-28 17:39:03 -0700100{
101 switch (api) {
102 case EGL:
103 dso[0] = hnd;
104 break;
105 case GLESv1_CM:
106 dso[1] = hnd;
107 break;
108 case GLESv2:
109 dso[2] = hnd;
110 break;
111 default:
Mathias Agopian65421432017-03-08 11:49:05 -0800112 return -EOVERFLOW;
Mathias Agopiande586972009-05-28 17:39:03 -0700113 }
Mathias Agopian65421432017-03-08 11:49:05 -0800114 return 0;
Mathias Agopiande586972009-05-28 17:39:03 -0700115}
116
Mathias Agopiande586972009-05-28 17:39:03 -0700117Loader::Loader()
Yi Kong48a6cd22018-07-18 10:07:09 -0700118 : getProcAddress(nullptr)
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800119{
Mathias Agopiande586972009-05-28 17:39:03 -0700120}
121
Mathias Agopian99381422013-04-23 20:52:29 +0200122Loader::~Loader() {
Mathias Agopiande586972009-05-28 17:39:03 -0700123}
124
Jesse Hallc07b5202013-07-04 12:08:16 -0700125static void* load_wrapper(const char* path) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800126 void* so = do_dlopen(path, RTLD_NOW | RTLD_LOCAL);
Jesse Hallc07b5202013-07-04 12:08:16 -0700127 ALOGE_IF(!so, "dlopen(\"%s\") failed: %s", path, dlerror());
128 return so;
129}
130
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700131#ifndef EGL_WRAPPER_DIR
132#if defined(__LP64__)
133#define EGL_WRAPPER_DIR "/system/lib64"
134#else
135#define EGL_WRAPPER_DIR "/system/lib"
136#endif
137#endif
138
Peiyong Lin0d10b252019-04-30 18:03:04 -0700139static const char* DRIVER_SUFFIX_PROPERTY = "ro.hardware.egl";
140
Peiyong Lin9679a982023-04-10 22:00:30 +0000141static const char* HAL_SUBNAME_KEY_PROPERTIES[3] = {
142 "persist.graphics.egl",
143 DRIVER_SUFFIX_PROPERTY,
144 "ro.board.platform",
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700145};
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
Peiyong Lin9679a982023-04-10 22:00:30 +0000211 if (should_unload_system_driver(cnx)) {
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700212 unload_system_driver(cnx);
213 }
214
215 // If a driver has been loaded, return the driver directly.
216 if (cnx->dso) {
217 return cnx->dso;
218 }
219
Peiyong Lin9679a982023-04-10 22:00:30 +0000220 // Firstly, try to load ANGLE driver.
221 driver_t* hnd = attempt_to_load_angle(cnx);
Ian Elliott5279f862022-04-21 12:41:03 -0600222
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700223 if (!hnd) {
224 // Secondly, try to load from driver apk.
225 hnd = attempt_to_load_updated_driver(cnx);
226 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700227
228 bool failToLoadFromDriverSuffixProperty = false;
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700229 if (!hnd) {
Peiyong Lindb3ed6e2020-01-09 18:43:27 -0800230 // If updated driver apk is set but fail to load, abort here.
231 if (android::GraphicsEnv::getInstance().getDriverNamespace()) {
232 LOG_ALWAYS_FATAL("couldn't find an OpenGL ES implementation from %s",
233 android::GraphicsEnv::getInstance().getDriverPath().c_str());
234 }
Peiyong Lin98db8e02023-04-05 20:09:16 +0000235 // Finally, try to load system driver.
236 // Start by searching for the library name appended by the system
237 // properties of the GLES userspace driver in both locations.
238 // i.e.:
239 // libGLES_${prop}.so, or:
240 // libEGL_${prop}.so, libGLESv1_CM_${prop}.so, libGLESv2_${prop}.so
241 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
242 auto prop = base::GetProperty(key, "");
243 if (prop.empty()) {
244 continue;
245 }
246 hnd = attempt_to_load_system_driver(cnx, prop.c_str(), true);
247 if (hnd) {
248 break;
249 } else if (strcmp(key, DRIVER_SUFFIX_PROPERTY) == 0) {
250 failToLoadFromDriverSuffixProperty = true;
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700251 }
252 }
253 }
254
255 if (!hnd) {
256 // Can't find graphics driver by appending system properties, now search for the exact name
257 // without any suffix of the GLES userspace driver in both locations.
258 // i.e.:
259 // libGLES.so, or:
260 // libEGL.so, libGLESv1_CM.so, libGLESv2.so
Charlie Lao840bd532020-01-16 17:34:37 -0800261 hnd = attempt_to_load_system_driver(cnx, nullptr, true);
Peiyong Lin0d10b252019-04-30 18:03:04 -0700262 }
263
Peiyong Lin237ab082023-05-19 21:20:28 +0000264 if (!hnd && !failToLoadFromDriverSuffixProperty) {
Charlie Lao840bd532020-01-16 17:34:37 -0800265 hnd = attempt_to_load_system_driver(cnx, nullptr, false);
Mathias Agopiande586972009-05-28 17:39:03 -0700266 }
267
Yiwei Zhangd9861812019-02-13 11:51:55 -0800268 if (!hnd) {
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700269 android::GraphicsEnv::getInstance().setDriverLoaded(android::GpuStatsInfo::Api::API_GL,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800270 false, systemTime() - openTime);
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600271 } else {
272 // init_angle_backend will check if loaded driver is ANGLE or not,
273 // will set cnx->useAngle appropriately.
274 // Do this here so that we use ANGLE path when driver is ANGLE (e.g. loaded as native),
275 // not just loading ANGLE as option.
Courtney Goeltzenleuchter8ecb10c2020-04-29 13:16:23 -0600276 init_angle_backend(hnd->dso[2], cnx);
Yiwei Zhangd9861812019-02-13 11:51:55 -0800277 }
278
Peiyong Lin3575b9f2019-04-25 14:26:49 -0700279 LOG_ALWAYS_FATAL_IF(!hnd,
Peiyong Lin9679a982023-04-10 22:00:30 +0000280 "couldn't find an OpenGL ES implementation, make sure one of %s, %s and %s "
281 "is set",
282 HAL_SUBNAME_KEY_PROPERTIES[0], HAL_SUBNAME_KEY_PROPERTIES[1],
283 HAL_SUBNAME_KEY_PROPERTIES[2]);
Jesse Hallc07b5202013-07-04 12:08:16 -0700284
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700285 if (!cnx->libEgl) {
286 cnx->libEgl = load_wrapper(EGL_WRAPPER_DIR "/libEGL.so");
287 }
Charlie Lao840bd532020-01-16 17:34:37 -0800288 if (!cnx->libGles1) {
289 cnx->libGles1 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv1_CM.so");
290 }
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700291 if (!cnx->libGles2) {
292 cnx->libGles2 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv2.so");
293 }
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700294
Charlie Lao840bd532020-01-16 17:34:37 -0800295 if (!cnx->libEgl || !cnx->libGles2 || !cnx->libGles1) {
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700296 android::GraphicsEnv::getInstance().setDriverLoaded(android::GpuStatsInfo::Api::API_GL,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800297 false, systemTime() - openTime);
298 }
299
Michael Chockc0ec5e22014-01-27 08:14:33 -0800300 LOG_ALWAYS_FATAL_IF(!cnx->libEgl,
301 "couldn't load system EGL wrapper libraries");
302
Charlie Lao840bd532020-01-16 17:34:37 -0800303 LOG_ALWAYS_FATAL_IF(!cnx->libGles2 || !cnx->libGles1,
304 "couldn't load system OpenGL ES wrapper libraries");
Jesse Hallc07b5202013-07-04 12:08:16 -0700305
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700306 android::GraphicsEnv::getInstance().setDriverLoaded(android::GpuStatsInfo::Api::API_GL, true,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800307 systemTime() - openTime);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800308
Mathias Agopiande586972009-05-28 17:39:03 -0700309 return (void*)hnd;
310}
311
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600312void Loader::close(egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700313{
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600314 driver_t* hnd = (driver_t*) cnx->dso;
Mathias Agopiande586972009-05-28 17:39:03 -0700315 delete hnd;
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600316 cnx->dso = nullptr;
317
Charlie Lao840bd532020-01-16 17:34:37 -0800318 cnx->useAngle = false;
Mathias Agopiande586972009-05-28 17:39:03 -0700319}
320
Jesse Hall94cdba92013-07-11 09:40:35 -0700321void Loader::init_api(void* dso,
322 char const * const * api,
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700323 char const * const * ref_api,
Jesse Hall94cdba92013-07-11 09:40:35 -0700324 __eglMustCastToProperFunctionPointerType* curr,
325 getProcAddressType getProcAddress)
Mathias Agopiande586972009-05-28 17:39:03 -0700326{
Jesse Hall1508ae62017-01-19 17:43:26 -0800327 ATRACE_CALL();
328
Mathias Agopian7773c432012-02-13 20:06:08 -0800329 const ssize_t SIZE = 256;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700330 char scrap[SIZE];
Mathias Agopiande586972009-05-28 17:39:03 -0700331 while (*api) {
332 char const * name = *api;
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700333 if (ref_api) {
334 char const * ref_name = *ref_api;
335 if (std::strcmp(name, ref_name) != 0) {
336 *curr++ = nullptr;
337 ref_api++;
338 continue;
339 }
340 }
341
Jesse Hall94cdba92013-07-11 09:40:35 -0700342 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700343 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700344 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700345 // couldn't find the entry-point, use eglGetProcAddress()
346 f = getProcAddress(name);
347 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700348 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700349 // Try without the OES postfix
350 ssize_t index = ssize_t(strlen(name)) - 3;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700351 if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) {
Mathias Agopiande586972009-05-28 17:39:03 -0700352 strncpy(scrap, name, index);
353 scrap[index] = 0;
354 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000355 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700356 }
357 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700358 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700359 // Try with the OES postfix
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700360 ssize_t index = ssize_t(strlen(name)) - 3;
361 if (index>0 && strcmp(name+index, "OES")) {
362 snprintf(scrap, SIZE, "%sOES", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700363 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000364 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700365 }
366 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700367 if (f == nullptr) {
Steve Block9d453682011-12-20 16:23:08 +0000368 //ALOGD("%s", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700369 f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented;
Mathias Agopian48d438d2012-01-28 21:44:00 -0800370
371 /*
Tao Wu1cdfe642020-06-23 12:33:02 -0700372 * GL_EXT_debug_marker is special, we always report it as
Mathias Agopian48d438d2012-01-28 21:44:00 -0800373 * supported, it's handled by GLES_trace. If GLES_trace is not
374 * enabled, then these are no-ops.
375 */
376 if (!strcmp(name, "glInsertEventMarkerEXT")) {
377 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
378 } else if (!strcmp(name, "glPushGroupMarkerEXT")) {
379 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
380 } else if (!strcmp(name, "glPopGroupMarkerEXT")) {
381 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
382 }
Mathias Agopiande586972009-05-28 17:39:03 -0700383 }
384 *curr++ = f;
385 api++;
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700386 if (ref_api) ref_api++;
Mathias Agopiande586972009-05-28 17:39:03 -0700387 }
388}
389
Peiyong Lin0d10b252019-04-30 18:03:04 -0700390static void* load_system_driver(const char* kind, const char* suffix, const bool exact) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800391 ATRACE_CALL();
Mathias Agopian99381422013-04-23 20:52:29 +0200392 class MatchFile {
393 public:
Peiyong Lin0d10b252019-04-30 18:03:04 -0700394 static std::string find(const char* libraryName, const bool exact) {
Mathias Agopian99381422013-04-23 20:52:29 +0200395 const char* const searchPaths[] = {
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800396#if defined(__LP64__)
397 "/vendor/lib64/egl",
398 "/system/lib64/egl"
399#else
Mathias Agopian99381422013-04-23 20:52:29 +0200400 "/vendor/lib/egl",
401 "/system/lib/egl"
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800402#endif
Mathias Agopian99381422013-04-23 20:52:29 +0200403 };
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700404
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700405 for (auto dir : searchPaths) {
Peiyong Lin0d10b252019-04-30 18:03:04 -0700406 std::string absolutePath;
407 if (find(absolutePath, libraryName, dir, exact)) {
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700408 return absolutePath;
Mathias Agopian99381422013-04-23 20:52:29 +0200409 }
Mathias Agopian99381422013-04-23 20:52:29 +0200410 }
411
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700412 // Driver not found. gah.
413 return std::string();
Mathias Agopian99381422013-04-23 20:52:29 +0200414 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700415 private:
416 static bool find(std::string& result,
417 const std::string& pattern, const char* const search, bool exact) {
418 if (exact) {
419 std::string absolutePath = std::string(search) + "/" + pattern + ".so";
420 if (!access(absolutePath.c_str(), R_OK)) {
421 result = absolutePath;
422 return true;
423 }
424 return false;
425 }
426
427 DIR* d = opendir(search);
428 if (d != nullptr) {
429 struct dirent* e;
430 while ((e = readdir(d)) != nullptr) {
431 if (e->d_type == DT_DIR) {
432 continue;
433 }
434 if (!strcmp(e->d_name, "libGLES_android.so")) {
435 // always skip the software renderer
436 continue;
437 }
438 if (strstr(e->d_name, pattern.c_str()) == e->d_name) {
439 if (!strcmp(e->d_name + strlen(e->d_name) - 3, ".so")) {
440 result = std::string(search) + "/" + e->d_name;
441 closedir(d);
442 return true;
443 }
444 }
445 }
446 closedir(d);
447 }
448 return false;
449 }
Mathias Agopian99381422013-04-23 20:52:29 +0200450 };
451
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700452 std::string libraryName = std::string("lib") + kind;
453 if (suffix) {
454 libraryName += std::string("_") + suffix;
Peiyong Lin0d10b252019-04-30 18:03:04 -0700455 } else if (!exact) {
456 // Deprecated: we look for files that match
457 // libGLES_*.so, or:
458 // libEGL_*.so, libGLESv1_CM_*.so, libGLESv2_*.so
459 libraryName += std::string("_");
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700460 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700461 std::string absolutePath = MatchFile::find(libraryName.c_str(), exact);
Mathias Agopian65421432017-03-08 11:49:05 -0800462 if (absolutePath.empty()) {
Mathias Agopian99381422013-04-23 20:52:29 +0200463 // this happens often, we don't want to log an error
Yi Kong48a6cd22018-07-18 10:07:09 -0700464 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700465 }
Mathias Agopian65421432017-03-08 11:49:05 -0800466 const char* const driver_absolute_path = absolutePath.c_str();
Mathias Agopiande586972009-05-28 17:39:03 -0700467
Jiyong Park5910dc72017-04-05 14:23:52 +0900468 // Try to load drivers from the 'sphal' namespace, if it exist. Fall back to
Justin Yunb7320302017-05-22 15:13:40 +0900469 // the original routine when the namespace does not exist.
Jiyong Park5910dc72017-04-05 14:23:52 +0900470 // See /system/core/rootdir/etc/ld.config.txt for the configuration of the
471 // sphal namespace.
Justin Yunb7320302017-05-22 15:13:40 +0900472 void* dso = do_android_load_sphal_library(driver_absolute_path,
473 RTLD_NOW | RTLD_LOCAL);
Yi Kong48a6cd22018-07-18 10:07:09 -0700474 if (dso == nullptr) {
Mathias Agopian8c173842009-09-20 16:01:02 -0700475 const char* err = dlerror();
Mathias Agopian65421432017-03-08 11:49:05 -0800476 ALOGE("load_driver(%s): %s", driver_absolute_path, err ? err : "unknown");
Yi Kong48a6cd22018-07-18 10:07:09 -0700477 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700478 }
479
Steve Block9d453682011-12-20 16:23:08 +0000480 ALOGD("loaded %s", driver_absolute_path);
Mathias Agopianbaca89c2009-08-20 19:09:34 -0700481
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800482 return dso;
483}
484
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600485static void* load_angle(const char* kind, android_namespace_t* ns) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600486 const android_dlextinfo dlextinfo = {
487 .flags = ANDROID_DLEXT_USE_NAMESPACE,
488 .library_namespace = ns,
489 };
490
491 std::string name = std::string("lib") + kind + "_angle.so";
492
493 void* so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
494
495 if (so) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600496 return so;
497 } else {
498 ALOGE("dlopen_ext(\"%s\") failed: %s", name.c_str(), dlerror());
499 }
500
501 return nullptr;
502}
503
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800504static void* load_updated_driver(const char* kind, android_namespace_t* ns) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800505 ATRACE_CALL();
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800506 const android_dlextinfo dlextinfo = {
507 .flags = ANDROID_DLEXT_USE_NAMESPACE,
508 .library_namespace = ns,
509 };
510 void* so = nullptr;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800511 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
Michael Hoisie4e0f56b2020-04-30 18:40:55 -0400512 auto prop = base::GetProperty(key, "");
513 if (prop.empty()) {
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700514 continue;
515 }
516 std::string name = std::string("lib") + kind + "_" + prop + ".so";
517 so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
518 if (so) {
519 return so;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800520 }
Yiwei Zhang9058b8f2020-11-12 20:23:00 +0000521 ALOGE("Could not load %s from updatable gfx driver namespace: %s.", name.c_str(),
522 dlerror());
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800523 }
524 return nullptr;
525}
526
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700527Loader::driver_t* Loader::attempt_to_load_angle(egl_connection_t* cnx) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800528 ATRACE_CALL();
Yiwei Zhangb22f0862020-04-23 10:40:24 -0700529
530 if (!android::GraphicsEnv::getInstance().shouldUseAngle()) {
531 return nullptr;
532 }
533
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600534 android_namespace_t* ns = android::GraphicsEnv::getInstance().getAngleNamespace();
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700535 if (!ns) {
536 return nullptr;
Cody Northrop6d082ef2018-09-11 09:42:31 -0600537 }
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700538
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700539 android::GraphicsEnv::getInstance().setDriverToLoad(android::GpuStatsInfo::Driver::ANGLE);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700540 driver_t* hnd = nullptr;
541
542 // ANGLE doesn't ship with GLES library, and thus we skip GLES driver.
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600543 void* dso = load_angle("EGL", ns);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700544 if (dso) {
545 initialize_api(dso, cnx, EGL);
546 hnd = new driver_t(dso);
547
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600548 dso = load_angle("GLESv1_CM", ns);
Charlie Lao840bd532020-01-16 17:34:37 -0800549 initialize_api(dso, cnx, GLESv1_CM);
550 hnd->set(dso, GLESv1_CM);
551
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600552 dso = load_angle("GLESv2", ns);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700553 initialize_api(dso, cnx, GLESv2);
554 hnd->set(dso, GLESv2);
555 }
556 return hnd;
557}
558
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600559void Loader::init_angle_backend(void* dso, egl_connection_t* cnx) {
Courtney Goeltzenleuchter8ecb10c2020-04-29 13:16:23 -0600560 void* pANGLEGetDisplayPlatform = dlsym(dso, "ANGLEGetDisplayPlatform");
561 if (pANGLEGetDisplayPlatform) {
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600562 ALOGV("ANGLE GLES library in use");
563 cnx->useAngle = true;
564 } else {
565 ALOGV("Native GLES library in use");
566 cnx->useAngle = false;
567 }
568}
569
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700570Loader::driver_t* Loader::attempt_to_load_updated_driver(egl_connection_t* cnx) {
571 ATRACE_CALL();
Yiwei Zhangd40aaac2020-06-04 05:26:56 -0700572
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700573 android_namespace_t* ns = android::GraphicsEnv::getInstance().getDriverNamespace();
574 if (!ns) {
575 return nullptr;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800576 }
577
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700578 ALOGD("Load updated gl driver.");
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700579 android::GraphicsEnv::getInstance().setDriverToLoad(android::GpuStatsInfo::Driver::GL_UPDATED);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700580 driver_t* hnd = nullptr;
581 void* dso = load_updated_driver("GLES", ns);
Peiyong Line83b8682019-04-18 17:23:02 -0700582 if (dso) {
Charlie Lao840bd532020-01-16 17:34:37 -0800583 initialize_api(dso, cnx, EGL | GLESv1_CM | GLESv2);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700584 hnd = new driver_t(dso);
585 return hnd;
Peiyong Line83b8682019-04-18 17:23:02 -0700586 }
587
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700588 dso = load_updated_driver("EGL", ns);
589 if (dso) {
590 initialize_api(dso, cnx, EGL);
591 hnd = new driver_t(dso);
592
Charlie Lao840bd532020-01-16 17:34:37 -0800593 dso = load_updated_driver("GLESv1_CM", ns);
594 initialize_api(dso, cnx, GLESv1_CM);
595 hnd->set(dso, GLESv1_CM);
596
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700597 dso = load_updated_driver("GLESv2", ns);
598 initialize_api(dso, cnx, GLESv2);
599 hnd->set(dso, GLESv2);
600 }
601 return hnd;
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700602}
603
Peiyong Lin0d10b252019-04-30 18:03:04 -0700604Loader::driver_t* Loader::attempt_to_load_system_driver(egl_connection_t* cnx, const char* suffix,
605 const bool exact) {
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700606 ATRACE_CALL();
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700607 android::GraphicsEnv::getInstance().setDriverToLoad(android::GpuStatsInfo::Driver::GL);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700608 driver_t* hnd = nullptr;
Peiyong Lin0d10b252019-04-30 18:03:04 -0700609 void* dso = load_system_driver("GLES", suffix, exact);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700610 if (dso) {
Charlie Lao840bd532020-01-16 17:34:37 -0800611 initialize_api(dso, cnx, EGL | GLESv1_CM | GLESv2);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700612 hnd = new driver_t(dso);
613 return hnd;
614 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700615 dso = load_system_driver("EGL", suffix, exact);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700616 if (dso) {
617 initialize_api(dso, cnx, EGL);
618 hnd = new driver_t(dso);
619
Charlie Lao840bd532020-01-16 17:34:37 -0800620 dso = load_system_driver("GLESv1_CM", suffix, exact);
621 initialize_api(dso, cnx, GLESv1_CM);
622 hnd->set(dso, GLESv1_CM);
623
Peiyong Lin0d10b252019-04-30 18:03:04 -0700624 dso = load_system_driver("GLESv2", suffix, exact);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700625 initialize_api(dso, cnx, GLESv2);
626 hnd->set(dso, GLESv2);
627 }
628 return hnd;
Peiyong Line83b8682019-04-18 17:23:02 -0700629}
630
631void Loader::initialize_api(void* dso, egl_connection_t* cnx, uint32_t mask) {
Mathias Agopiande586972009-05-28 17:39:03 -0700632 if (mask & EGL) {
633 getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress");
634
Jesse Hall94cdba92013-07-11 09:40:35 -0700635 ALOGE_IF(!getProcAddress,
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800636 "can't find eglGetProcAddress() in EGL driver library");
Mathias Agopiande586972009-05-28 17:39:03 -0700637
Mathias Agopian618fa102009-10-14 02:06:37 -0700638 egl_t* egl = &cnx->egl;
Mathias Agopiande586972009-05-28 17:39:03 -0700639 __eglMustCastToProperFunctionPointerType* curr =
640 (__eglMustCastToProperFunctionPointerType*)egl;
641 char const * const * api = egl_names;
642 while (*api) {
643 char const * name = *api;
Jesse Hall94cdba92013-07-11 09:40:35 -0700644 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700645 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700646 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700647 // couldn't find the entry-point, use eglGetProcAddress()
648 f = getProcAddress(name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700649 if (f == nullptr) {
650 f = (__eglMustCastToProperFunctionPointerType)nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700651 }
652 }
653 *curr++ = f;
654 api++;
655 }
656 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700657
Mathias Agopiande586972009-05-28 17:39:03 -0700658 if (mask & GLESv1_CM) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700659 init_api(dso, gl_names_1, gl_names,
Mathias Agopian618fa102009-10-14 02:06:37 -0700660 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800661 &cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl,
Mathias Agopian618fa102009-10-14 02:06:37 -0700662 getProcAddress);
Mathias Agopiande586972009-05-28 17:39:03 -0700663 }
664
665 if (mask & GLESv2) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700666 init_api(dso, gl_names, nullptr,
Mathias Agopian618fa102009-10-14 02:06:37 -0700667 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800668 &cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl,
Mathias Agopiande586972009-05-28 17:39:03 -0700669 getProcAddress);
670 }
Mathias Agopiande586972009-05-28 17:39:03 -0700671}
672
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700673} // namespace android