blob: bb3b43abb527aca783d36cad15282e1628a71de3 [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
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700150static bool should_unload_system_driver(egl_connection_t* cnx) {
151 // Return false if the system driver has been unloaded once.
152 if (cnx->systemDriverUnloaded) {
153 return false;
154 }
155
156 // Return true if Angle namespace is set.
157 android_namespace_t* ns = android::GraphicsEnv::getInstance().getAngleNamespace();
158 if (ns) {
159 return true;
160 }
161
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700162 // Return true if updated driver namespace is set.
163 ns = android::GraphicsEnv::getInstance().getDriverNamespace();
164 if (ns) {
165 return true;
166 }
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700167
168 return false;
169}
170
171static void uninit_api(char const* const* api, __eglMustCastToProperFunctionPointerType* curr) {
172 while (*api) {
173 *curr++ = nullptr;
174 api++;
175 }
176}
177
178void Loader::unload_system_driver(egl_connection_t* cnx) {
179 ATRACE_CALL();
180
181 uninit_api(gl_names,
182 (__eglMustCastToProperFunctionPointerType*)&cnx
183 ->hooks[egl_connection_t::GLESv2_INDEX]
184 ->gl);
185 uninit_api(gl_names,
186 (__eglMustCastToProperFunctionPointerType*)&cnx
187 ->hooks[egl_connection_t::GLESv1_INDEX]
188 ->gl);
189 uninit_api(egl_names, (__eglMustCastToProperFunctionPointerType*)&cnx->egl);
190
191 if (cnx->dso) {
192 ALOGD("Unload system gl driver.");
193 driver_t* hnd = (driver_t*)cnx->dso;
194 if (hnd->dso[2]) {
195 do_android_unload_sphal_library(hnd->dso[2]);
196 }
197 if (hnd->dso[1]) {
198 do_android_unload_sphal_library(hnd->dso[1]);
199 }
200 if (hnd->dso[0]) {
201 do_android_unload_sphal_library(hnd->dso[0]);
202 }
203 cnx->dso = nullptr;
204 }
205
206 cnx->systemDriverUnloaded = true;
207}
208
Mathias Agopianada798b2012-02-13 17:09:30 -0800209void* Loader::open(egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700210{
Jesse Hall1508ae62017-01-19 17:43:26 -0800211 ATRACE_CALL();
Yiwei Zhangd9861812019-02-13 11:51:55 -0800212 const nsecs_t openTime = systemTime();
Jesse Hall1508ae62017-01-19 17:43:26 -0800213
Peiyong Lin9679a982023-04-10 22:00:30 +0000214 if (should_unload_system_driver(cnx)) {
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700215 unload_system_driver(cnx);
216 }
217
218 // If a driver has been loaded, return the driver directly.
219 if (cnx->dso) {
220 return cnx->dso;
221 }
222
Peiyong Lin9679a982023-04-10 22:00:30 +0000223 // Firstly, try to load ANGLE driver.
224 driver_t* hnd = attempt_to_load_angle(cnx);
Ian Elliott5279f862022-04-21 12:41:03 -0600225
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700226 if (!hnd) {
227 // Secondly, try to load from driver apk.
228 hnd = attempt_to_load_updated_driver(cnx);
229 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700230
231 bool failToLoadFromDriverSuffixProperty = false;
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700232 if (!hnd) {
Peiyong Lindb3ed6e2020-01-09 18:43:27 -0800233 // If updated driver apk is set but fail to load, abort here.
234 if (android::GraphicsEnv::getInstance().getDriverNamespace()) {
235 LOG_ALWAYS_FATAL("couldn't find an OpenGL ES implementation from %s",
236 android::GraphicsEnv::getInstance().getDriverPath().c_str());
237 }
Peiyong Lin98db8e02023-04-05 20:09:16 +0000238 // Finally, try to load system driver.
239 // Start by searching for the library name appended by the system
240 // properties of the GLES userspace driver in both locations.
241 // i.e.:
242 // libGLES_${prop}.so, or:
243 // libEGL_${prop}.so, libGLESv1_CM_${prop}.so, libGLESv2_${prop}.so
244 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
245 auto prop = base::GetProperty(key, "");
246 if (prop.empty()) {
247 continue;
248 }
249 hnd = attempt_to_load_system_driver(cnx, prop.c_str(), true);
Peiyong Linb8aaeaf2023-06-13 20:51:03 +0000250 if (!hnd) {
251 ALOGD("Failed to load drivers from property %s with value %s", key, prop.c_str());
Peiyong Lin98db8e02023-04-05 20:09:16 +0000252 failToLoadFromDriverSuffixProperty = true;
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700253 }
Peiyong Linb8aaeaf2023-06-13 20:51:03 +0000254
255 // Abort regardless of whether subsequent properties are set, the value must be set
256 // correctly with the first property that has a value.
257 break;
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700258 }
259 }
260
261 if (!hnd) {
Peiyong Linb8aaeaf2023-06-13 20:51:03 +0000262 // Can't find graphics driver by appending the value from system properties, now search for
263 // the exact name without any suffix of the GLES userspace driver in both locations.
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700264 // i.e.:
265 // libGLES.so, or:
266 // libEGL.so, libGLESv1_CM.so, libGLESv2.so
Charlie Lao840bd532020-01-16 17:34:37 -0800267 hnd = attempt_to_load_system_driver(cnx, nullptr, true);
Peiyong Lin0d10b252019-04-30 18:03:04 -0700268 }
269
Peiyong Lind43ee402023-05-24 17:59:08 +0000270 if (!hnd && !failToLoadFromDriverSuffixProperty &&
271 property_get_int32("ro.vendor.api_level", 0) < __ANDROID_API_U__) {
272 // Still can't find the graphics drivers with the exact name. This time try to use wildcard
273 // matching if the device is launched before Android 14.
Charlie Lao840bd532020-01-16 17:34:37 -0800274 hnd = attempt_to_load_system_driver(cnx, nullptr, false);
Mathias Agopiande586972009-05-28 17:39:03 -0700275 }
276
Yiwei Zhangd9861812019-02-13 11:51:55 -0800277 if (!hnd) {
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700278 android::GraphicsEnv::getInstance().setDriverLoaded(android::GpuStatsInfo::Api::API_GL,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800279 false, systemTime() - openTime);
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600280 } else {
281 // init_angle_backend will check if loaded driver is ANGLE or not,
282 // will set cnx->useAngle appropriately.
283 // Do this here so that we use ANGLE path when driver is ANGLE (e.g. loaded as native),
284 // not just loading ANGLE as option.
Courtney Goeltzenleuchter8ecb10c2020-04-29 13:16:23 -0600285 init_angle_backend(hnd->dso[2], cnx);
Yiwei Zhangd9861812019-02-13 11:51:55 -0800286 }
287
Peiyong Lin3575b9f2019-04-25 14:26:49 -0700288 LOG_ALWAYS_FATAL_IF(!hnd,
Peiyong Lin9679a982023-04-10 22:00:30 +0000289 "couldn't find an OpenGL ES implementation, make sure one of %s, %s and %s "
290 "is set",
291 HAL_SUBNAME_KEY_PROPERTIES[0], HAL_SUBNAME_KEY_PROPERTIES[1],
292 HAL_SUBNAME_KEY_PROPERTIES[2]);
Jesse Hallc07b5202013-07-04 12:08:16 -0700293
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700294 if (!cnx->libEgl) {
295 cnx->libEgl = load_wrapper(EGL_WRAPPER_DIR "/libEGL.so");
296 }
Charlie Lao840bd532020-01-16 17:34:37 -0800297 if (!cnx->libGles1) {
298 cnx->libGles1 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv1_CM.so");
299 }
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700300 if (!cnx->libGles2) {
301 cnx->libGles2 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv2.so");
302 }
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700303
Charlie Lao840bd532020-01-16 17:34:37 -0800304 if (!cnx->libEgl || !cnx->libGles2 || !cnx->libGles1) {
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700305 android::GraphicsEnv::getInstance().setDriverLoaded(android::GpuStatsInfo::Api::API_GL,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800306 false, systemTime() - openTime);
307 }
308
Michael Chockc0ec5e22014-01-27 08:14:33 -0800309 LOG_ALWAYS_FATAL_IF(!cnx->libEgl,
310 "couldn't load system EGL wrapper libraries");
311
Charlie Lao840bd532020-01-16 17:34:37 -0800312 LOG_ALWAYS_FATAL_IF(!cnx->libGles2 || !cnx->libGles1,
313 "couldn't load system OpenGL ES wrapper libraries");
Jesse Hallc07b5202013-07-04 12:08:16 -0700314
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700315 android::GraphicsEnv::getInstance().setDriverLoaded(android::GpuStatsInfo::Api::API_GL, true,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800316 systemTime() - openTime);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800317
Mathias Agopiande586972009-05-28 17:39:03 -0700318 return (void*)hnd;
319}
320
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600321void Loader::close(egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700322{
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600323 driver_t* hnd = (driver_t*) cnx->dso;
Mathias Agopiande586972009-05-28 17:39:03 -0700324 delete hnd;
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600325 cnx->dso = nullptr;
326
Charlie Lao840bd532020-01-16 17:34:37 -0800327 cnx->useAngle = false;
Mathias Agopiande586972009-05-28 17:39:03 -0700328}
329
Jesse Hall94cdba92013-07-11 09:40:35 -0700330void Loader::init_api(void* dso,
331 char const * const * api,
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700332 char const * const * ref_api,
Jesse Hall94cdba92013-07-11 09:40:35 -0700333 __eglMustCastToProperFunctionPointerType* curr,
334 getProcAddressType getProcAddress)
Mathias Agopiande586972009-05-28 17:39:03 -0700335{
Jesse Hall1508ae62017-01-19 17:43:26 -0800336 ATRACE_CALL();
337
Mathias Agopian7773c432012-02-13 20:06:08 -0800338 const ssize_t SIZE = 256;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700339 char scrap[SIZE];
Mathias Agopiande586972009-05-28 17:39:03 -0700340 while (*api) {
341 char const * name = *api;
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700342 if (ref_api) {
343 char const * ref_name = *ref_api;
344 if (std::strcmp(name, ref_name) != 0) {
345 *curr++ = nullptr;
346 ref_api++;
347 continue;
348 }
349 }
350
Jesse Hall94cdba92013-07-11 09:40:35 -0700351 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700352 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700353 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700354 // couldn't find the entry-point, use eglGetProcAddress()
355 f = getProcAddress(name);
356 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700357 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700358 // Try without the OES postfix
359 ssize_t index = ssize_t(strlen(name)) - 3;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700360 if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) {
Mathias Agopiande586972009-05-28 17:39:03 -0700361 strncpy(scrap, name, index);
362 scrap[index] = 0;
363 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) {
Mathias Agopiande586972009-05-28 17:39:03 -0700368 // Try with the OES postfix
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700369 ssize_t index = ssize_t(strlen(name)) - 3;
370 if (index>0 && strcmp(name+index, "OES")) {
371 snprintf(scrap, SIZE, "%sOES", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700372 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000373 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700374 }
375 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700376 if (f == nullptr) {
Steve Block9d453682011-12-20 16:23:08 +0000377 //ALOGD("%s", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700378 f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented;
Mathias Agopian48d438d2012-01-28 21:44:00 -0800379
380 /*
Tao Wu1cdfe642020-06-23 12:33:02 -0700381 * GL_EXT_debug_marker is special, we always report it as
Mathias Agopian48d438d2012-01-28 21:44:00 -0800382 * supported, it's handled by GLES_trace. If GLES_trace is not
383 * enabled, then these are no-ops.
384 */
385 if (!strcmp(name, "glInsertEventMarkerEXT")) {
386 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
387 } else if (!strcmp(name, "glPushGroupMarkerEXT")) {
388 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
389 } else if (!strcmp(name, "glPopGroupMarkerEXT")) {
390 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
391 }
Mathias Agopiande586972009-05-28 17:39:03 -0700392 }
393 *curr++ = f;
394 api++;
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700395 if (ref_api) ref_api++;
Mathias Agopiande586972009-05-28 17:39:03 -0700396 }
397}
398
Peiyong Lin0d10b252019-04-30 18:03:04 -0700399static void* load_system_driver(const char* kind, const char* suffix, const bool exact) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800400 ATRACE_CALL();
Mathias Agopian99381422013-04-23 20:52:29 +0200401 class MatchFile {
402 public:
Peiyong Lin0d10b252019-04-30 18:03:04 -0700403 static std::string find(const char* libraryName, const bool exact) {
Mathias Agopian99381422013-04-23 20:52:29 +0200404 const char* const searchPaths[] = {
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800405#if defined(__LP64__)
406 "/vendor/lib64/egl",
407 "/system/lib64/egl"
408#else
Mathias Agopian99381422013-04-23 20:52:29 +0200409 "/vendor/lib/egl",
410 "/system/lib/egl"
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800411#endif
Mathias Agopian99381422013-04-23 20:52:29 +0200412 };
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700413
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700414 for (auto dir : searchPaths) {
Peiyong Lin0d10b252019-04-30 18:03:04 -0700415 std::string absolutePath;
416 if (find(absolutePath, libraryName, dir, exact)) {
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700417 return absolutePath;
Mathias Agopian99381422013-04-23 20:52:29 +0200418 }
Mathias Agopian99381422013-04-23 20:52:29 +0200419 }
420
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700421 // Driver not found. gah.
422 return std::string();
Mathias Agopian99381422013-04-23 20:52:29 +0200423 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700424 private:
425 static bool find(std::string& result,
426 const std::string& pattern, const char* const search, bool exact) {
427 if (exact) {
428 std::string absolutePath = std::string(search) + "/" + pattern + ".so";
429 if (!access(absolutePath.c_str(), R_OK)) {
430 result = absolutePath;
431 return true;
432 }
433 return false;
434 }
435
436 DIR* d = opendir(search);
437 if (d != nullptr) {
438 struct dirent* e;
439 while ((e = readdir(d)) != nullptr) {
440 if (e->d_type == DT_DIR) {
441 continue;
442 }
443 if (!strcmp(e->d_name, "libGLES_android.so")) {
444 // always skip the software renderer
445 continue;
446 }
447 if (strstr(e->d_name, pattern.c_str()) == e->d_name) {
448 if (!strcmp(e->d_name + strlen(e->d_name) - 3, ".so")) {
449 result = std::string(search) + "/" + e->d_name;
450 closedir(d);
451 return true;
452 }
453 }
454 }
455 closedir(d);
456 }
457 return false;
458 }
Mathias Agopian99381422013-04-23 20:52:29 +0200459 };
460
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700461 std::string libraryName = std::string("lib") + kind;
462 if (suffix) {
463 libraryName += std::string("_") + suffix;
Peiyong Lin0d10b252019-04-30 18:03:04 -0700464 } else if (!exact) {
465 // Deprecated: we look for files that match
466 // libGLES_*.so, or:
467 // libEGL_*.so, libGLESv1_CM_*.so, libGLESv2_*.so
468 libraryName += std::string("_");
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700469 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700470 std::string absolutePath = MatchFile::find(libraryName.c_str(), exact);
Mathias Agopian65421432017-03-08 11:49:05 -0800471 if (absolutePath.empty()) {
Mathias Agopian99381422013-04-23 20:52:29 +0200472 // this happens often, we don't want to log an error
Yi Kong48a6cd22018-07-18 10:07:09 -0700473 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700474 }
Mathias Agopian65421432017-03-08 11:49:05 -0800475 const char* const driver_absolute_path = absolutePath.c_str();
Mathias Agopiande586972009-05-28 17:39:03 -0700476
Jiyong Park5910dc72017-04-05 14:23:52 +0900477 // Try to load drivers from the 'sphal' namespace, if it exist. Fall back to
Justin Yunb7320302017-05-22 15:13:40 +0900478 // the original routine when the namespace does not exist.
Jiyong Park5910dc72017-04-05 14:23:52 +0900479 // See /system/core/rootdir/etc/ld.config.txt for the configuration of the
480 // sphal namespace.
Justin Yunb7320302017-05-22 15:13:40 +0900481 void* dso = do_android_load_sphal_library(driver_absolute_path,
482 RTLD_NOW | RTLD_LOCAL);
Yi Kong48a6cd22018-07-18 10:07:09 -0700483 if (dso == nullptr) {
Mathias Agopian8c173842009-09-20 16:01:02 -0700484 const char* err = dlerror();
Mathias Agopian65421432017-03-08 11:49:05 -0800485 ALOGE("load_driver(%s): %s", driver_absolute_path, err ? err : "unknown");
Yi Kong48a6cd22018-07-18 10:07:09 -0700486 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700487 }
488
Steve Block9d453682011-12-20 16:23:08 +0000489 ALOGD("loaded %s", driver_absolute_path);
Mathias Agopianbaca89c2009-08-20 19:09:34 -0700490
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800491 return dso;
492}
493
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600494static void* load_angle(const char* kind, android_namespace_t* ns) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600495 const android_dlextinfo dlextinfo = {
496 .flags = ANDROID_DLEXT_USE_NAMESPACE,
497 .library_namespace = ns,
498 };
499
500 std::string name = std::string("lib") + kind + "_angle.so";
501
502 void* so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
503
504 if (so) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600505 return so;
506 } else {
507 ALOGE("dlopen_ext(\"%s\") failed: %s", name.c_str(), dlerror());
508 }
509
510 return nullptr;
511}
512
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800513static void* load_updated_driver(const char* kind, android_namespace_t* ns) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800514 ATRACE_CALL();
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800515 const android_dlextinfo dlextinfo = {
516 .flags = ANDROID_DLEXT_USE_NAMESPACE,
517 .library_namespace = ns,
518 };
519 void* so = nullptr;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800520 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
Michael Hoisie4e0f56b2020-04-30 18:40:55 -0400521 auto prop = base::GetProperty(key, "");
522 if (prop.empty()) {
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700523 continue;
524 }
525 std::string name = std::string("lib") + kind + "_" + prop + ".so";
526 so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
527 if (so) {
528 return so;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800529 }
Yiwei Zhang9058b8f2020-11-12 20:23:00 +0000530 ALOGE("Could not load %s from updatable gfx driver namespace: %s.", name.c_str(),
531 dlerror());
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800532 }
533 return nullptr;
534}
535
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700536Loader::driver_t* Loader::attempt_to_load_angle(egl_connection_t* cnx) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800537 ATRACE_CALL();
Yiwei Zhangb22f0862020-04-23 10:40:24 -0700538
539 if (!android::GraphicsEnv::getInstance().shouldUseAngle()) {
540 return nullptr;
541 }
542
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600543 android_namespace_t* ns = android::GraphicsEnv::getInstance().getAngleNamespace();
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700544 if (!ns) {
545 return nullptr;
Cody Northrop6d082ef2018-09-11 09:42:31 -0600546 }
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700547
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700548 android::GraphicsEnv::getInstance().setDriverToLoad(android::GpuStatsInfo::Driver::ANGLE);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700549 driver_t* hnd = nullptr;
550
551 // ANGLE doesn't ship with GLES library, and thus we skip GLES driver.
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600552 void* dso = load_angle("EGL", ns);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700553 if (dso) {
554 initialize_api(dso, cnx, EGL);
555 hnd = new driver_t(dso);
556
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600557 dso = load_angle("GLESv1_CM", ns);
Charlie Lao840bd532020-01-16 17:34:37 -0800558 initialize_api(dso, cnx, GLESv1_CM);
559 hnd->set(dso, GLESv1_CM);
560
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600561 dso = load_angle("GLESv2", ns);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700562 initialize_api(dso, cnx, GLESv2);
563 hnd->set(dso, GLESv2);
564 }
565 return hnd;
566}
567
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600568void Loader::init_angle_backend(void* dso, egl_connection_t* cnx) {
Courtney Goeltzenleuchter8ecb10c2020-04-29 13:16:23 -0600569 void* pANGLEGetDisplayPlatform = dlsym(dso, "ANGLEGetDisplayPlatform");
570 if (pANGLEGetDisplayPlatform) {
Courtney Goeltzenleuchterd1646cf2020-04-23 08:19:11 -0600571 ALOGV("ANGLE GLES library in use");
572 cnx->useAngle = true;
573 } else {
574 ALOGV("Native GLES library in use");
575 cnx->useAngle = false;
576 }
577}
578
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700579Loader::driver_t* Loader::attempt_to_load_updated_driver(egl_connection_t* cnx) {
580 ATRACE_CALL();
Yiwei Zhangd40aaac2020-06-04 05:26:56 -0700581
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700582 android_namespace_t* ns = android::GraphicsEnv::getInstance().getDriverNamespace();
583 if (!ns) {
584 return nullptr;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800585 }
586
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700587 ALOGD("Load updated gl driver.");
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700588 android::GraphicsEnv::getInstance().setDriverToLoad(android::GpuStatsInfo::Driver::GL_UPDATED);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700589 driver_t* hnd = nullptr;
590 void* dso = load_updated_driver("GLES", ns);
Peiyong Line83b8682019-04-18 17:23:02 -0700591 if (dso) {
Charlie Lao840bd532020-01-16 17:34:37 -0800592 initialize_api(dso, cnx, EGL | GLESv1_CM | GLESv2);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700593 hnd = new driver_t(dso);
594 return hnd;
Peiyong Line83b8682019-04-18 17:23:02 -0700595 }
596
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700597 dso = load_updated_driver("EGL", ns);
598 if (dso) {
599 initialize_api(dso, cnx, EGL);
600 hnd = new driver_t(dso);
601
Charlie Lao840bd532020-01-16 17:34:37 -0800602 dso = load_updated_driver("GLESv1_CM", ns);
603 initialize_api(dso, cnx, GLESv1_CM);
604 hnd->set(dso, GLESv1_CM);
605
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700606 dso = load_updated_driver("GLESv2", ns);
607 initialize_api(dso, cnx, GLESv2);
608 hnd->set(dso, GLESv2);
609 }
610 return hnd;
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700611}
612
Peiyong Lin0d10b252019-04-30 18:03:04 -0700613Loader::driver_t* Loader::attempt_to_load_system_driver(egl_connection_t* cnx, const char* suffix,
614 const bool exact) {
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700615 ATRACE_CALL();
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700616 android::GraphicsEnv::getInstance().setDriverToLoad(android::GpuStatsInfo::Driver::GL);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700617 driver_t* hnd = nullptr;
Peiyong Lin0d10b252019-04-30 18:03:04 -0700618 void* dso = load_system_driver("GLES", suffix, exact);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700619 if (dso) {
Charlie Lao840bd532020-01-16 17:34:37 -0800620 initialize_api(dso, cnx, EGL | GLESv1_CM | GLESv2);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700621 hnd = new driver_t(dso);
622 return hnd;
623 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700624 dso = load_system_driver("EGL", suffix, exact);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700625 if (dso) {
626 initialize_api(dso, cnx, EGL);
627 hnd = new driver_t(dso);
628
Charlie Lao840bd532020-01-16 17:34:37 -0800629 dso = load_system_driver("GLESv1_CM", suffix, exact);
630 initialize_api(dso, cnx, GLESv1_CM);
631 hnd->set(dso, GLESv1_CM);
632
Peiyong Lin0d10b252019-04-30 18:03:04 -0700633 dso = load_system_driver("GLESv2", suffix, exact);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700634 initialize_api(dso, cnx, GLESv2);
635 hnd->set(dso, GLESv2);
636 }
637 return hnd;
Peiyong Line83b8682019-04-18 17:23:02 -0700638}
639
640void Loader::initialize_api(void* dso, egl_connection_t* cnx, uint32_t mask) {
Mathias Agopiande586972009-05-28 17:39:03 -0700641 if (mask & EGL) {
642 getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress");
643
Jesse Hall94cdba92013-07-11 09:40:35 -0700644 ALOGE_IF(!getProcAddress,
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800645 "can't find eglGetProcAddress() in EGL driver library");
Mathias Agopiande586972009-05-28 17:39:03 -0700646
Mathias Agopian618fa102009-10-14 02:06:37 -0700647 egl_t* egl = &cnx->egl;
Mathias Agopiande586972009-05-28 17:39:03 -0700648 __eglMustCastToProperFunctionPointerType* curr =
649 (__eglMustCastToProperFunctionPointerType*)egl;
650 char const * const * api = egl_names;
651 while (*api) {
652 char const * name = *api;
Jesse Hall94cdba92013-07-11 09:40:35 -0700653 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700654 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700655 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700656 // couldn't find the entry-point, use eglGetProcAddress()
657 f = getProcAddress(name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700658 if (f == nullptr) {
659 f = (__eglMustCastToProperFunctionPointerType)nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700660 }
661 }
662 *curr++ = f;
663 api++;
664 }
665 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700666
Mathias Agopiande586972009-05-28 17:39:03 -0700667 if (mask & GLESv1_CM) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700668 init_api(dso, gl_names_1, gl_names,
Mathias Agopian618fa102009-10-14 02:06:37 -0700669 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800670 &cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl,
Mathias Agopian618fa102009-10-14 02:06:37 -0700671 getProcAddress);
Mathias Agopiande586972009-05-28 17:39:03 -0700672 }
673
674 if (mask & GLESv2) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700675 init_api(dso, gl_names, nullptr,
Mathias Agopian618fa102009-10-14 02:06:37 -0700676 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800677 &cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl,
Mathias Agopiande586972009-05-28 17:39:03 -0700678 getProcAddress);
679 }
Mathias Agopiande586972009-05-28 17:39:03 -0700680}
681
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700682} // namespace android