blob: b4fc5f0dd29371cc53b1386424de44b7878e58ef [file] [log] [blame]
Jesse Hall94cdba92013-07-11 09:40:35 -07001/*
Mathias Agopiande586972009-05-28 17:39:03 -07002 ** Copyright 2007, The Android Open Source Project
3 **
Jesse Hall94cdba92013-07-11 09:40:35 -07004 ** Licensed under the Apache License, Version 2.0 (the "License");
5 ** you may not use this file except in compliance with the License.
6 ** You may obtain a copy of the License at
Mathias Agopiande586972009-05-28 17:39:03 -07007 **
Jesse Hall94cdba92013-07-11 09:40:35 -07008 ** http://www.apache.org/licenses/LICENSE-2.0
Mathias Agopiande586972009-05-28 17:39:03 -07009 **
Jesse Hall94cdba92013-07-11 09:40:35 -070010 ** Unless required by applicable law or agreed to in writing, software
11 ** distributed under the License is distributed on an "AS IS" BASIS,
12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ** See the License for the specific language governing permissions and
Mathias Agopiande586972009-05-28 17:39:03 -070014 ** limitations under the License.
15 */
16
Jesse Hall7a8d83e2016-12-20 15:24:28 -080017//#define LOG_NDEBUG 0
Jesse Hall1508ae62017-01-19 17:43:26 -080018#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Jesse Hall7a8d83e2016-12-20 15:24:28 -080019
Yiwei Zhangd40aaac2020-06-04 05:26:56 -070020#include "EGL/Loader.h"
Mathias Agopiande586972009-05-28 17:39:03 -070021
Michael Hoisie4e0f56b2020-04-30 18:40:55 -040022#include <android-base/properties.h>
Jesse Hall7a8d83e2016-12-20 15:24:28 -080023#include <android/dlext.h>
Peiyong Lind43ee402023-05-24 17:59:08 +000024#include <cutils/properties.h>
Yiwei Zhangd40aaac2020-06-04 05:26:56 -070025#include <dirent.h>
26#include <dlfcn.h>
27#include <graphicsenv/GraphicsEnv.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070028#include <log/log.h>
Yiwei Zhangd9861812019-02-13 11:51:55 -080029#include <utils/Timers.h>
Justin Yunb7320302017-05-22 15:13:40 +090030#include <vndksupport/linker.h>
Mathias Agopiande586972009-05-28 17:39:03 -070031
Yiwei Zhangd40aaac2020-06-04 05:26:56 -070032#include <string>
33
Yiwei Zhang8af003e2020-06-04 14:11:30 -070034#include "EGL/eglext_angle.h"
Cody Northrop68d10352018-10-15 07:22:09 -060035#include "egl_platform_entries.h"
Mathias Agopian65421432017-03-08 11:49:05 -080036#include "egl_trace.h"
Mathias Agopian1cadb252011-05-23 17:26:14 -070037#include "egldefs.h"
Mathias Agopiande586972009-05-28 17:39:03 -070038
Mathias Agopiande586972009-05-28 17:39:03 -070039namespace android {
Mathias Agopiande586972009-05-28 17:39:03 -070040
41/*
Mathias Agopian99381422013-04-23 20:52:29 +020042 * EGL userspace drivers must be provided either:
43 * - as a single library:
44 * /vendor/lib/egl/libGLES.so
45 *
46 * - as separate libraries:
47 * /vendor/lib/egl/libEGL.so
48 * /vendor/lib/egl/libGLESv1_CM.so
49 * /vendor/lib/egl/libGLESv2.so
50 *
Mathias Agopian99381422013-04-23 20:52:29 +020051 * For backward compatibility and to facilitate the transition to
52 * this new naming scheme, the loader will additionally look for:
Jesse Hall94cdba92013-07-11 09:40:35 -070053 *
Mathias Agopian99381422013-04-23 20:52:29 +020054 * /{vendor|system}/lib/egl/lib{GLES | [EGL|GLESv1_CM|GLESv2]}_*.so
Jesse Hall94cdba92013-07-11 09:40:35 -070055 *
Mathias Agopiande586972009-05-28 17:39:03 -070056 */
57
Mathias Agopian65421432017-03-08 11:49:05 -080058Loader& Loader::getInstance() {
59 static Loader loader;
60 return loader;
61}
Mathias Agopiande586972009-05-28 17:39:03 -070062
Jesse Hall1508ae62017-01-19 17:43:26 -080063static void* do_dlopen(const char* path, int mode) {
64 ATRACE_CALL();
65 return dlopen(path, mode);
66}
67
Jiyong Park5910dc72017-04-05 14:23:52 +090068static void* do_android_dlopen_ext(const char* path, int mode, const android_dlextinfo* info) {
69 ATRACE_CALL();
70 return android_dlopen_ext(path, mode, info);
71}
72
Justin Yunb7320302017-05-22 15:13:40 +090073static void* do_android_load_sphal_library(const char* path, int mode) {
74 ATRACE_CALL();
75 return android_load_sphal_library(path, mode);
76}
77
Yiwei Zhang5e21eb32019-06-05 00:26:03 -070078static int do_android_unload_sphal_library(void* dso) {
79 ATRACE_CALL();
80 return android_unload_sphal_library(dso);
81}
82
Jesse Hall94cdba92013-07-11 09:40:35 -070083Loader::driver_t::driver_t(void* gles)
Mathias Agopiande586972009-05-28 17:39:03 -070084{
85 dso[0] = gles;
86 for (size_t i=1 ; i<NELEM(dso) ; i++)
Yi Kong48a6cd22018-07-18 10:07:09 -070087 dso[i] = nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -070088}
89
Jesse Hall94cdba92013-07-11 09:40:35 -070090Loader::driver_t::~driver_t()
Mathias Agopiande586972009-05-28 17:39:03 -070091{
92 for (size_t i=0 ; i<NELEM(dso) ; i++) {
93 if (dso[i]) {
94 dlclose(dso[i]);
Yi Kong48a6cd22018-07-18 10:07:09 -070095 dso[i] = nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -070096 }
97 }
98}
99
Mathias Agopian65421432017-03-08 11:49:05 -0800100int Loader::driver_t::set(void* hnd, int32_t api)
Mathias Agopiande586972009-05-28 17:39:03 -0700101{
102 switch (api) {
103 case EGL:
104 dso[0] = hnd;
105 break;
106 case GLESv1_CM:
107 dso[1] = hnd;
108 break;
109 case GLESv2:
110 dso[2] = hnd;
111 break;
112 default:
Mathias Agopian65421432017-03-08 11:49:05 -0800113 return -EOVERFLOW;
Mathias Agopiande586972009-05-28 17:39:03 -0700114 }
Mathias Agopian65421432017-03-08 11:49:05 -0800115 return 0;
Mathias Agopiande586972009-05-28 17:39:03 -0700116}
117
Mathias Agopiande586972009-05-28 17:39:03 -0700118Loader::Loader()
Yi Kong48a6cd22018-07-18 10:07:09 -0700119 : getProcAddress(nullptr)
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800120{
Mathias Agopiande586972009-05-28 17:39:03 -0700121}
122
Mathias Agopian99381422013-04-23 20:52:29 +0200123Loader::~Loader() {
Mathias Agopiande586972009-05-28 17:39:03 -0700124}
125
Jesse Hallc07b5202013-07-04 12:08:16 -0700126static void* load_wrapper(const char* path) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800127 void* so = do_dlopen(path, RTLD_NOW | RTLD_LOCAL);
Jesse Hallc07b5202013-07-04 12:08:16 -0700128 ALOGE_IF(!so, "dlopen(\"%s\") failed: %s", path, dlerror());
129 return so;
130}
131
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700132#ifndef EGL_WRAPPER_DIR
133#if defined(__LP64__)
134#define EGL_WRAPPER_DIR "/system/lib64"
135#else
136#define EGL_WRAPPER_DIR "/system/lib"
137#endif
138#endif
139
Peiyong Linb8aaeaf2023-06-13 20:51:03 +0000140static const char* PERSIST_DRIVER_SUFFIX_PROPERTY = "persist.graphics.egl";
141static const char* RO_DRIVER_SUFFIX_PROPERTY = "ro.hardware.egl";
142static const char* RO_BOARD_PLATFORM_PROPERTY = "ro.board.platform";
Peiyong Lin0d10b252019-04-30 18:03:04 -0700143
Peiyong Lin9679a982023-04-10 22:00:30 +0000144static const char* HAL_SUBNAME_KEY_PROPERTIES[3] = {
Peiyong Linb8aaeaf2023-06-13 20:51:03 +0000145 PERSIST_DRIVER_SUFFIX_PROPERTY,
146 RO_DRIVER_SUFFIX_PROPERTY,
147 RO_BOARD_PLATFORM_PROPERTY,
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700148};
149
Peiyong Lin54926182023-06-15 22:35:14 +0000150// Check whether the loaded system drivers should be unloaded in order to
151// load ANGLE or the updatable graphics drivers.
152// If ANGLE namespace is set, it means the application is identified to run on top of ANGLE.
153// If updatable graphics driver namespace is set, it means the application is identified to
154// run on top of updatable graphics drivers.
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700155static bool should_unload_system_driver(egl_connection_t* cnx) {
156 // Return false if the system driver has been unloaded once.
157 if (cnx->systemDriverUnloaded) {
158 return false;
159 }
160
Peiyong Lin54926182023-06-15 22:35:14 +0000161 // Return true if ANGLE namespace is set.
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700162 android_namespace_t* ns = android::GraphicsEnv::getInstance().getAngleNamespace();
163 if (ns) {
164 return true;
165 }
166
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700167 // Return true if updated driver namespace is set.
168 ns = android::GraphicsEnv::getInstance().getDriverNamespace();
169 if (ns) {
170 return true;
171 }
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700172
173 return false;
174}
175
176static void uninit_api(char const* const* api, __eglMustCastToProperFunctionPointerType* curr) {
177 while (*api) {
178 *curr++ = nullptr;
179 api++;
180 }
181}
182
183void Loader::unload_system_driver(egl_connection_t* cnx) {
184 ATRACE_CALL();
185
186 uninit_api(gl_names,
187 (__eglMustCastToProperFunctionPointerType*)&cnx
188 ->hooks[egl_connection_t::GLESv2_INDEX]
189 ->gl);
190 uninit_api(gl_names,
191 (__eglMustCastToProperFunctionPointerType*)&cnx
192 ->hooks[egl_connection_t::GLESv1_INDEX]
193 ->gl);
194 uninit_api(egl_names, (__eglMustCastToProperFunctionPointerType*)&cnx->egl);
195
196 if (cnx->dso) {
197 ALOGD("Unload system gl driver.");
198 driver_t* hnd = (driver_t*)cnx->dso;
199 if (hnd->dso[2]) {
200 do_android_unload_sphal_library(hnd->dso[2]);
201 }
202 if (hnd->dso[1]) {
203 do_android_unload_sphal_library(hnd->dso[1]);
204 }
205 if (hnd->dso[0]) {
206 do_android_unload_sphal_library(hnd->dso[0]);
207 }
208 cnx->dso = nullptr;
209 }
210
211 cnx->systemDriverUnloaded = true;
212}
213
Mathias Agopianada798b2012-02-13 17:09:30 -0800214void* Loader::open(egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700215{
Jesse Hall1508ae62017-01-19 17:43:26 -0800216 ATRACE_CALL();
Yiwei Zhangd9861812019-02-13 11:51:55 -0800217 const nsecs_t openTime = systemTime();
Jesse Hall1508ae62017-01-19 17:43:26 -0800218
Peiyong Lin9679a982023-04-10 22:00:30 +0000219 if (should_unload_system_driver(cnx)) {
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700220 unload_system_driver(cnx);
221 }
222
223 // If a driver has been loaded, return the driver directly.
224 if (cnx->dso) {
225 return cnx->dso;
226 }
227
Peiyong Lin9679a982023-04-10 22:00:30 +0000228 // Firstly, try to load ANGLE driver.
229 driver_t* hnd = attempt_to_load_angle(cnx);
Ian Elliott5279f862022-04-21 12:41:03 -0600230
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700231 if (!hnd) {
232 // Secondly, try to load from driver apk.
233 hnd = attempt_to_load_updated_driver(cnx);
234 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700235
236 bool failToLoadFromDriverSuffixProperty = false;
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700237 if (!hnd) {
Peiyong Lindb3ed6e2020-01-09 18:43:27 -0800238 // If updated driver apk is set but fail to load, abort here.
239 if (android::GraphicsEnv::getInstance().getDriverNamespace()) {
240 LOG_ALWAYS_FATAL("couldn't find an OpenGL ES implementation from %s",
241 android::GraphicsEnv::getInstance().getDriverPath().c_str());
242 }
Peiyong Lin98db8e02023-04-05 20:09:16 +0000243 // Finally, try to load system driver.
244 // Start by searching for the library name appended by the system
245 // properties of the GLES userspace driver in both locations.
246 // i.e.:
247 // libGLES_${prop}.so, or:
248 // libEGL_${prop}.so, libGLESv1_CM_${prop}.so, libGLESv2_${prop}.so
249 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
250 auto prop = base::GetProperty(key, "");
251 if (prop.empty()) {
252 continue;
253 }
254 hnd = attempt_to_load_system_driver(cnx, prop.c_str(), true);
Peiyong Linb8aaeaf2023-06-13 20:51:03 +0000255 if (!hnd) {
256 ALOGD("Failed to load drivers from property %s with value %s", key, prop.c_str());
Peiyong Lin98db8e02023-04-05 20:09:16 +0000257 failToLoadFromDriverSuffixProperty = true;
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700258 }
Peiyong Linb8aaeaf2023-06-13 20:51:03 +0000259
260 // Abort regardless of whether subsequent properties are set, the value must be set
261 // correctly with the first property that has a value.
262 break;
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700263 }
264 }
265
266 if (!hnd) {
Peiyong Linb8aaeaf2023-06-13 20:51:03 +0000267 // Can't find graphics driver by appending the value from system properties, now search for
268 // the exact name without any suffix of the GLES userspace driver in both locations.
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700269 // i.e.:
270 // libGLES.so, or:
271 // libEGL.so, libGLESv1_CM.so, libGLESv2.so
Charlie Lao840bd532020-01-16 17:34:37 -0800272 hnd = attempt_to_load_system_driver(cnx, nullptr, true);
Peiyong Lin0d10b252019-04-30 18:03:04 -0700273 }
274
Peiyong Lind43ee402023-05-24 17:59:08 +0000275 if (!hnd && !failToLoadFromDriverSuffixProperty &&
276 property_get_int32("ro.vendor.api_level", 0) < __ANDROID_API_U__) {
277 // Still can't find the graphics drivers with the exact name. This time try to use wildcard
278 // matching if the device is launched before Android 14.
Charlie Lao840bd532020-01-16 17:34:37 -0800279 hnd = attempt_to_load_system_driver(cnx, nullptr, false);
Mathias Agopiande586972009-05-28 17:39:03 -0700280 }
281
Yiwei Zhangd9861812019-02-13 11:51:55 -0800282 if (!hnd) {
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700283 android::GraphicsEnv::getInstance().setDriverLoaded(android::GpuStatsInfo::Api::API_GL,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800284 false, systemTime() - openTime);
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600285 } else {
286 // init_angle_backend will check if loaded driver is ANGLE or not,
Peiyong Lin54926182023-06-15 22:35:14 +0000287 // will set cnx->angleLoaded appropriately.
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600288 // Do this here so that we use ANGLE path when driver is ANGLE (e.g. loaded as native),
289 // not just loading ANGLE as option.
Peiyong Lin54926182023-06-15 22:35:14 +0000290 attempt_to_init_angle_backend(hnd->dso[2], cnx);
Yiwei Zhangd9861812019-02-13 11:51:55 -0800291 }
292
Peiyong Lin3575b9f2019-04-25 14:26:49 -0700293 LOG_ALWAYS_FATAL_IF(!hnd,
Peiyong Lin9679a982023-04-10 22:00:30 +0000294 "couldn't find an OpenGL ES implementation, make sure one of %s, %s and %s "
295 "is set",
296 HAL_SUBNAME_KEY_PROPERTIES[0], HAL_SUBNAME_KEY_PROPERTIES[1],
297 HAL_SUBNAME_KEY_PROPERTIES[2]);
Jesse Hallc07b5202013-07-04 12:08:16 -0700298
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700299 if (!cnx->libEgl) {
300 cnx->libEgl = load_wrapper(EGL_WRAPPER_DIR "/libEGL.so");
301 }
Charlie Lao840bd532020-01-16 17:34:37 -0800302 if (!cnx->libGles1) {
303 cnx->libGles1 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv1_CM.so");
304 }
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700305 if (!cnx->libGles2) {
306 cnx->libGles2 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv2.so");
307 }
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700308
Charlie Lao840bd532020-01-16 17:34:37 -0800309 if (!cnx->libEgl || !cnx->libGles2 || !cnx->libGles1) {
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700310 android::GraphicsEnv::getInstance().setDriverLoaded(android::GpuStatsInfo::Api::API_GL,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800311 false, systemTime() - openTime);
312 }
313
Michael Chockc0ec5e22014-01-27 08:14:33 -0800314 LOG_ALWAYS_FATAL_IF(!cnx->libEgl,
315 "couldn't load system EGL wrapper libraries");
316
Charlie Lao840bd532020-01-16 17:34:37 -0800317 LOG_ALWAYS_FATAL_IF(!cnx->libGles2 || !cnx->libGles1,
318 "couldn't load system OpenGL ES wrapper libraries");
Jesse Hallc07b5202013-07-04 12:08:16 -0700319
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700320 android::GraphicsEnv::getInstance().setDriverLoaded(android::GpuStatsInfo::Api::API_GL, true,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800321 systemTime() - openTime);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800322
Mathias Agopiande586972009-05-28 17:39:03 -0700323 return (void*)hnd;
324}
325
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600326void Loader::close(egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700327{
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600328 driver_t* hnd = (driver_t*) cnx->dso;
Mathias Agopiande586972009-05-28 17:39:03 -0700329 delete hnd;
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600330 cnx->dso = nullptr;
331
Peiyong Lin54926182023-06-15 22:35:14 +0000332 cnx->angleLoaded = false;
Mathias Agopiande586972009-05-28 17:39:03 -0700333}
334
Jesse Hall94cdba92013-07-11 09:40:35 -0700335void Loader::init_api(void* dso,
336 char const * const * api,
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700337 char const * const * ref_api,
Jesse Hall94cdba92013-07-11 09:40:35 -0700338 __eglMustCastToProperFunctionPointerType* curr,
339 getProcAddressType getProcAddress)
Mathias Agopiande586972009-05-28 17:39:03 -0700340{
Jesse Hall1508ae62017-01-19 17:43:26 -0800341 ATRACE_CALL();
342
Mathias Agopian7773c432012-02-13 20:06:08 -0800343 const ssize_t SIZE = 256;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700344 char scrap[SIZE];
Mathias Agopiande586972009-05-28 17:39:03 -0700345 while (*api) {
346 char const * name = *api;
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700347 if (ref_api) {
348 char const * ref_name = *ref_api;
349 if (std::strcmp(name, ref_name) != 0) {
350 *curr++ = nullptr;
351 ref_api++;
352 continue;
353 }
354 }
355
Jesse Hall94cdba92013-07-11 09:40:35 -0700356 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700357 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700358 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700359 // couldn't find the entry-point, use eglGetProcAddress()
360 f = getProcAddress(name);
361 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700362 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700363 // Try without the OES postfix
364 ssize_t index = ssize_t(strlen(name)) - 3;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700365 if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) {
Mathias Agopiande586972009-05-28 17:39:03 -0700366 strncpy(scrap, name, index);
367 scrap[index] = 0;
368 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000369 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700370 }
371 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700372 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700373 // Try with the OES postfix
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700374 ssize_t index = ssize_t(strlen(name)) - 3;
375 if (index>0 && strcmp(name+index, "OES")) {
376 snprintf(scrap, SIZE, "%sOES", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700377 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000378 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700379 }
380 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700381 if (f == nullptr) {
Steve Block9d453682011-12-20 16:23:08 +0000382 //ALOGD("%s", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700383 f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented;
Mathias Agopian48d438d2012-01-28 21:44:00 -0800384
385 /*
Tao Wu1cdfe642020-06-23 12:33:02 -0700386 * GL_EXT_debug_marker is special, we always report it as
Mathias Agopian48d438d2012-01-28 21:44:00 -0800387 * supported, it's handled by GLES_trace. If GLES_trace is not
388 * enabled, then these are no-ops.
389 */
390 if (!strcmp(name, "glInsertEventMarkerEXT")) {
391 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
392 } else if (!strcmp(name, "glPushGroupMarkerEXT")) {
393 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
394 } else if (!strcmp(name, "glPopGroupMarkerEXT")) {
395 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
396 }
Mathias Agopiande586972009-05-28 17:39:03 -0700397 }
398 *curr++ = f;
399 api++;
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700400 if (ref_api) ref_api++;
Mathias Agopiande586972009-05-28 17:39:03 -0700401 }
402}
403
Peiyong Lin0d10b252019-04-30 18:03:04 -0700404static void* load_system_driver(const char* kind, const char* suffix, const bool exact) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800405 ATRACE_CALL();
Mathias Agopian99381422013-04-23 20:52:29 +0200406 class MatchFile {
407 public:
Peiyong Lin0d10b252019-04-30 18:03:04 -0700408 static std::string find(const char* libraryName, const bool exact) {
Mathias Agopian99381422013-04-23 20:52:29 +0200409 const char* const searchPaths[] = {
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800410#if defined(__LP64__)
411 "/vendor/lib64/egl",
412 "/system/lib64/egl"
413#else
Mathias Agopian99381422013-04-23 20:52:29 +0200414 "/vendor/lib/egl",
415 "/system/lib/egl"
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800416#endif
Mathias Agopian99381422013-04-23 20:52:29 +0200417 };
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700418
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700419 for (auto dir : searchPaths) {
Peiyong Lin0d10b252019-04-30 18:03:04 -0700420 std::string absolutePath;
421 if (find(absolutePath, libraryName, dir, exact)) {
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700422 return absolutePath;
Mathias Agopian99381422013-04-23 20:52:29 +0200423 }
Mathias Agopian99381422013-04-23 20:52:29 +0200424 }
425
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700426 // Driver not found. gah.
427 return std::string();
Mathias Agopian99381422013-04-23 20:52:29 +0200428 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700429 private:
430 static bool find(std::string& result,
431 const std::string& pattern, const char* const search, bool exact) {
432 if (exact) {
433 std::string absolutePath = std::string(search) + "/" + pattern + ".so";
434 if (!access(absolutePath.c_str(), R_OK)) {
435 result = absolutePath;
436 return true;
437 }
438 return false;
439 }
440
441 DIR* d = opendir(search);
442 if (d != nullptr) {
443 struct dirent* e;
444 while ((e = readdir(d)) != nullptr) {
445 if (e->d_type == DT_DIR) {
446 continue;
447 }
448 if (!strcmp(e->d_name, "libGLES_android.so")) {
449 // always skip the software renderer
450 continue;
451 }
452 if (strstr(e->d_name, pattern.c_str()) == e->d_name) {
453 if (!strcmp(e->d_name + strlen(e->d_name) - 3, ".so")) {
454 result = std::string(search) + "/" + e->d_name;
455 closedir(d);
456 return true;
457 }
458 }
459 }
460 closedir(d);
461 }
462 return false;
463 }
Mathias Agopian99381422013-04-23 20:52:29 +0200464 };
465
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700466 std::string libraryName = std::string("lib") + kind;
467 if (suffix) {
468 libraryName += std::string("_") + suffix;
Peiyong Lin0d10b252019-04-30 18:03:04 -0700469 } else if (!exact) {
470 // Deprecated: we look for files that match
471 // libGLES_*.so, or:
472 // libEGL_*.so, libGLESv1_CM_*.so, libGLESv2_*.so
473 libraryName += std::string("_");
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700474 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700475 std::string absolutePath = MatchFile::find(libraryName.c_str(), exact);
Mathias Agopian65421432017-03-08 11:49:05 -0800476 if (absolutePath.empty()) {
Mathias Agopian99381422013-04-23 20:52:29 +0200477 // this happens often, we don't want to log an error
Yi Kong48a6cd22018-07-18 10:07:09 -0700478 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700479 }
Mathias Agopian65421432017-03-08 11:49:05 -0800480 const char* const driver_absolute_path = absolutePath.c_str();
Mathias Agopiande586972009-05-28 17:39:03 -0700481
Jiyong Park5910dc72017-04-05 14:23:52 +0900482 // Try to load drivers from the 'sphal' namespace, if it exist. Fall back to
Justin Yunb7320302017-05-22 15:13:40 +0900483 // the original routine when the namespace does not exist.
Jiyong Park5910dc72017-04-05 14:23:52 +0900484 // See /system/core/rootdir/etc/ld.config.txt for the configuration of the
485 // sphal namespace.
Justin Yunb7320302017-05-22 15:13:40 +0900486 void* dso = do_android_load_sphal_library(driver_absolute_path,
487 RTLD_NOW | RTLD_LOCAL);
Yi Kong48a6cd22018-07-18 10:07:09 -0700488 if (dso == nullptr) {
Mathias Agopian8c173842009-09-20 16:01:02 -0700489 const char* err = dlerror();
Mathias Agopian65421432017-03-08 11:49:05 -0800490 ALOGE("load_driver(%s): %s", driver_absolute_path, err ? err : "unknown");
Yi Kong48a6cd22018-07-18 10:07:09 -0700491 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700492 }
493
Steve Block9d453682011-12-20 16:23:08 +0000494 ALOGD("loaded %s", driver_absolute_path);
Mathias Agopianbaca89c2009-08-20 19:09:34 -0700495
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800496 return dso;
497}
498
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600499static void* load_angle(const char* kind, android_namespace_t* ns) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600500 const android_dlextinfo dlextinfo = {
501 .flags = ANDROID_DLEXT_USE_NAMESPACE,
502 .library_namespace = ns,
503 };
504
505 std::string name = std::string("lib") + kind + "_angle.so";
506
507 void* so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
508
509 if (so) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600510 return so;
511 } else {
512 ALOGE("dlopen_ext(\"%s\") failed: %s", name.c_str(), dlerror());
513 }
514
515 return nullptr;
516}
517
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800518static void* load_updated_driver(const char* kind, android_namespace_t* ns) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800519 ATRACE_CALL();
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800520 const android_dlextinfo dlextinfo = {
521 .flags = ANDROID_DLEXT_USE_NAMESPACE,
522 .library_namespace = ns,
523 };
524 void* so = nullptr;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800525 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
Michael Hoisie4e0f56b2020-04-30 18:40:55 -0400526 auto prop = base::GetProperty(key, "");
527 if (prop.empty()) {
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700528 continue;
529 }
530 std::string name = std::string("lib") + kind + "_" + prop + ".so";
531 so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
532 if (so) {
533 return so;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800534 }
Yiwei Zhang9058b8f2020-11-12 20:23:00 +0000535 ALOGE("Could not load %s from updatable gfx driver namespace: %s.", name.c_str(),
536 dlerror());
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800537 }
538 return nullptr;
539}
540
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700541Loader::driver_t* Loader::attempt_to_load_angle(egl_connection_t* cnx) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800542 ATRACE_CALL();
Yiwei Zhangb22f0862020-04-23 10:40:24 -0700543
544 if (!android::GraphicsEnv::getInstance().shouldUseAngle()) {
545 return nullptr;
546 }
547
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600548 android_namespace_t* ns = android::GraphicsEnv::getInstance().getAngleNamespace();
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700549 if (!ns) {
550 return nullptr;
Cody Northrop6d082ef2018-09-11 09:42:31 -0600551 }
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700552
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700553 android::GraphicsEnv::getInstance().setDriverToLoad(android::GpuStatsInfo::Driver::ANGLE);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700554 driver_t* hnd = nullptr;
555
556 // ANGLE doesn't ship with GLES library, and thus we skip GLES driver.
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600557 void* dso = load_angle("EGL", ns);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700558 if (dso) {
559 initialize_api(dso, cnx, EGL);
560 hnd = new driver_t(dso);
561
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600562 dso = load_angle("GLESv1_CM", ns);
Charlie Lao840bd532020-01-16 17:34:37 -0800563 initialize_api(dso, cnx, GLESv1_CM);
564 hnd->set(dso, GLESv1_CM);
565
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600566 dso = load_angle("GLESv2", ns);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700567 initialize_api(dso, cnx, GLESv2);
568 hnd->set(dso, GLESv2);
569 }
570 return hnd;
571}
572
Peiyong Lin54926182023-06-15 22:35:14 +0000573void Loader::attempt_to_init_angle_backend(void* dso, egl_connection_t* cnx) {
Courtney Goeltzenleuchter8ecb10c2020-04-29 13:16:23 -0600574 void* pANGLEGetDisplayPlatform = dlsym(dso, "ANGLEGetDisplayPlatform");
575 if (pANGLEGetDisplayPlatform) {
Peiyong Lin54926182023-06-15 22:35:14 +0000576 ALOGV("ANGLE GLES library loaded");
577 cnx->angleLoaded = true;
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600578 } else {
Peiyong Lin54926182023-06-15 22:35:14 +0000579 ALOGV("Native GLES library loaded");
580 cnx->angleLoaded = false;
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600581 }
582}
583
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700584Loader::driver_t* Loader::attempt_to_load_updated_driver(egl_connection_t* cnx) {
585 ATRACE_CALL();
Yiwei Zhangd40aaac2020-06-04 05:26:56 -0700586
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700587 android_namespace_t* ns = android::GraphicsEnv::getInstance().getDriverNamespace();
588 if (!ns) {
589 return nullptr;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800590 }
591
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700592 ALOGD("Load updated gl driver.");
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700593 android::GraphicsEnv::getInstance().setDriverToLoad(android::GpuStatsInfo::Driver::GL_UPDATED);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700594 driver_t* hnd = nullptr;
595 void* dso = load_updated_driver("GLES", ns);
Peiyong Line83b8682019-04-18 17:23:02 -0700596 if (dso) {
Charlie Lao840bd532020-01-16 17:34:37 -0800597 initialize_api(dso, cnx, EGL | GLESv1_CM | GLESv2);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700598 hnd = new driver_t(dso);
599 return hnd;
Peiyong Line83b8682019-04-18 17:23:02 -0700600 }
601
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700602 dso = load_updated_driver("EGL", ns);
603 if (dso) {
604 initialize_api(dso, cnx, EGL);
605 hnd = new driver_t(dso);
606
Charlie Lao840bd532020-01-16 17:34:37 -0800607 dso = load_updated_driver("GLESv1_CM", ns);
608 initialize_api(dso, cnx, GLESv1_CM);
609 hnd->set(dso, GLESv1_CM);
610
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700611 dso = load_updated_driver("GLESv2", ns);
612 initialize_api(dso, cnx, GLESv2);
613 hnd->set(dso, GLESv2);
614 }
615 return hnd;
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700616}
617
Peiyong Lin0d10b252019-04-30 18:03:04 -0700618Loader::driver_t* Loader::attempt_to_load_system_driver(egl_connection_t* cnx, const char* suffix,
619 const bool exact) {
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700620 ATRACE_CALL();
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700621 android::GraphicsEnv::getInstance().setDriverToLoad(android::GpuStatsInfo::Driver::GL);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700622 driver_t* hnd = nullptr;
Peiyong Lin0d10b252019-04-30 18:03:04 -0700623 void* dso = load_system_driver("GLES", suffix, exact);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700624 if (dso) {
Charlie Lao840bd532020-01-16 17:34:37 -0800625 initialize_api(dso, cnx, EGL | GLESv1_CM | GLESv2);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700626 hnd = new driver_t(dso);
627 return hnd;
628 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700629 dso = load_system_driver("EGL", suffix, exact);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700630 if (dso) {
631 initialize_api(dso, cnx, EGL);
632 hnd = new driver_t(dso);
633
Charlie Lao840bd532020-01-16 17:34:37 -0800634 dso = load_system_driver("GLESv1_CM", suffix, exact);
635 initialize_api(dso, cnx, GLESv1_CM);
636 hnd->set(dso, GLESv1_CM);
637
Peiyong Lin0d10b252019-04-30 18:03:04 -0700638 dso = load_system_driver("GLESv2", suffix, exact);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700639 initialize_api(dso, cnx, GLESv2);
640 hnd->set(dso, GLESv2);
641 }
642 return hnd;
Peiyong Line83b8682019-04-18 17:23:02 -0700643}
644
645void Loader::initialize_api(void* dso, egl_connection_t* cnx, uint32_t mask) {
Mathias Agopiande586972009-05-28 17:39:03 -0700646 if (mask & EGL) {
647 getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress");
648
Jesse Hall94cdba92013-07-11 09:40:35 -0700649 ALOGE_IF(!getProcAddress,
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800650 "can't find eglGetProcAddress() in EGL driver library");
Mathias Agopiande586972009-05-28 17:39:03 -0700651
Mathias Agopian618fa102009-10-14 02:06:37 -0700652 egl_t* egl = &cnx->egl;
Mathias Agopiande586972009-05-28 17:39:03 -0700653 __eglMustCastToProperFunctionPointerType* curr =
654 (__eglMustCastToProperFunctionPointerType*)egl;
655 char const * const * api = egl_names;
656 while (*api) {
657 char const * name = *api;
Jesse Hall94cdba92013-07-11 09:40:35 -0700658 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700659 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700660 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700661 // couldn't find the entry-point, use eglGetProcAddress()
662 f = getProcAddress(name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700663 if (f == nullptr) {
664 f = (__eglMustCastToProperFunctionPointerType)nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700665 }
666 }
667 *curr++ = f;
668 api++;
669 }
670 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700671
Mathias Agopiande586972009-05-28 17:39:03 -0700672 if (mask & GLESv1_CM) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700673 init_api(dso, gl_names_1, gl_names,
Mathias Agopian618fa102009-10-14 02:06:37 -0700674 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800675 &cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl,
Mathias Agopian618fa102009-10-14 02:06:37 -0700676 getProcAddress);
Mathias Agopiande586972009-05-28 17:39:03 -0700677 }
678
679 if (mask & GLESv2) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700680 init_api(dso, gl_names, nullptr,
Mathias Agopian618fa102009-10-14 02:06:37 -0700681 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800682 &cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl,
Mathias Agopiande586972009-05-28 17:39:03 -0700683 getProcAddress);
684 }
Mathias Agopiande586972009-05-28 17:39:03 -0700685}
686
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700687} // namespace android