blob: 57441824012a53c1c8cfd5806097426810c64d86 [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
Tim Van Patten5f744f12018-12-12 11:46:21 -070020#include <EGL/Loader.h>
Mathias Agopian311b4792017-02-28 15:00:49 -080021
Mathias Agopian65421432017-03-08 11:49:05 -080022#include <string>
23
Mathias Agopian99381422013-04-23 20:52:29 +020024#include <dirent.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070025#include <dlfcn.h>
Mathias Agopiande586972009-05-28 17:39:03 -070026
Jesse Hall7a8d83e2016-12-20 15:24:28 -080027#include <android/dlext.h>
Charlie Lao840bd532020-01-16 17:34:37 -080028#include <cutils/properties.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070029#include <log/log.h>
Yiwei Zhangd9861812019-02-13 11:51:55 -080030#include <utils/Timers.h>
Mathias Agopian311b4792017-02-28 15:00:49 -080031
Jiyong Parka243e5d2017-06-21 12:26:51 +090032#ifndef __ANDROID_VNDK__
Jiyong Park27c39e12017-05-08 13:00:02 +090033#include <graphicsenv/GraphicsEnv.h>
Jiyong Parka243e5d2017-06-21 12:26:51 +090034#endif
Justin Yunb7320302017-05-22 15:13:40 +090035#include <vndksupport/linker.h>
Mathias Agopiande586972009-05-28 17:39:03 -070036
Cody Northrop68d10352018-10-15 07:22:09 -060037#include "egl_platform_entries.h"
Mathias Agopian65421432017-03-08 11:49:05 -080038#include "egl_trace.h"
Mathias Agopian1cadb252011-05-23 17:26:14 -070039#include "egldefs.h"
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -060040#include <EGL/eglext_angle.h>
Mathias Agopiande586972009-05-28 17:39:03 -070041
Mathias Agopiande586972009-05-28 17:39:03 -070042namespace android {
Mathias Agopiande586972009-05-28 17:39:03 -070043
44/*
Mathias Agopian99381422013-04-23 20:52:29 +020045 * EGL userspace drivers must be provided either:
46 * - as a single library:
47 * /vendor/lib/egl/libGLES.so
48 *
49 * - as separate libraries:
50 * /vendor/lib/egl/libEGL.so
51 * /vendor/lib/egl/libGLESv1_CM.so
52 * /vendor/lib/egl/libGLESv2.so
53 *
Mathias Agopian99381422013-04-23 20:52:29 +020054 * For backward compatibility and to facilitate the transition to
55 * this new naming scheme, the loader will additionally look for:
Jesse Hall94cdba92013-07-11 09:40:35 -070056 *
Mathias Agopian99381422013-04-23 20:52:29 +020057 * /{vendor|system}/lib/egl/lib{GLES | [EGL|GLESv1_CM|GLESv2]}_*.so
Jesse Hall94cdba92013-07-11 09:40:35 -070058 *
Mathias Agopiande586972009-05-28 17:39:03 -070059 */
60
Mathias Agopian65421432017-03-08 11:49:05 -080061Loader& Loader::getInstance() {
62 static Loader loader;
63 return loader;
64}
Mathias Agopiande586972009-05-28 17:39:03 -070065
Jesse Hall1508ae62017-01-19 17:43:26 -080066static void* do_dlopen(const char* path, int mode) {
67 ATRACE_CALL();
68 return dlopen(path, mode);
69}
70
Jiyong Park5910dc72017-04-05 14:23:52 +090071static void* do_android_dlopen_ext(const char* path, int mode, const android_dlextinfo* info) {
72 ATRACE_CALL();
73 return android_dlopen_ext(path, mode, info);
74}
75
Justin Yunb7320302017-05-22 15:13:40 +090076static void* do_android_load_sphal_library(const char* path, int mode) {
77 ATRACE_CALL();
78 return android_load_sphal_library(path, mode);
79}
80
Yiwei Zhang5e21eb32019-06-05 00:26:03 -070081static int do_android_unload_sphal_library(void* dso) {
82 ATRACE_CALL();
83 return android_unload_sphal_library(dso);
84}
85
Jesse Hall94cdba92013-07-11 09:40:35 -070086Loader::driver_t::driver_t(void* gles)
Mathias Agopiande586972009-05-28 17:39:03 -070087{
88 dso[0] = gles;
89 for (size_t i=1 ; i<NELEM(dso) ; i++)
Yi Kong48a6cd22018-07-18 10:07:09 -070090 dso[i] = nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -070091}
92
Jesse Hall94cdba92013-07-11 09:40:35 -070093Loader::driver_t::~driver_t()
Mathias Agopiande586972009-05-28 17:39:03 -070094{
95 for (size_t i=0 ; i<NELEM(dso) ; i++) {
96 if (dso[i]) {
97 dlclose(dso[i]);
Yi Kong48a6cd22018-07-18 10:07:09 -070098 dso[i] = nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -070099 }
100 }
101}
102
Mathias Agopian65421432017-03-08 11:49:05 -0800103int Loader::driver_t::set(void* hnd, int32_t api)
Mathias Agopiande586972009-05-28 17:39:03 -0700104{
105 switch (api) {
106 case EGL:
107 dso[0] = hnd;
108 break;
109 case GLESv1_CM:
110 dso[1] = hnd;
111 break;
112 case GLESv2:
113 dso[2] = hnd;
114 break;
115 default:
Mathias Agopian65421432017-03-08 11:49:05 -0800116 return -EOVERFLOW;
Mathias Agopiande586972009-05-28 17:39:03 -0700117 }
Mathias Agopian65421432017-03-08 11:49:05 -0800118 return 0;
Mathias Agopiande586972009-05-28 17:39:03 -0700119}
120
Mathias Agopiande586972009-05-28 17:39:03 -0700121Loader::Loader()
Yi Kong48a6cd22018-07-18 10:07:09 -0700122 : getProcAddress(nullptr)
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800123{
Mathias Agopiande586972009-05-28 17:39:03 -0700124}
125
Mathias Agopian99381422013-04-23 20:52:29 +0200126Loader::~Loader() {
Mathias Agopiande586972009-05-28 17:39:03 -0700127}
128
Jesse Hallc07b5202013-07-04 12:08:16 -0700129static void* load_wrapper(const char* path) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800130 void* so = do_dlopen(path, RTLD_NOW | RTLD_LOCAL);
Jesse Hallc07b5202013-07-04 12:08:16 -0700131 ALOGE_IF(!so, "dlopen(\"%s\") failed: %s", path, dlerror());
132 return so;
133}
134
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700135#ifndef EGL_WRAPPER_DIR
136#if defined(__LP64__)
137#define EGL_WRAPPER_DIR "/system/lib64"
138#else
139#define EGL_WRAPPER_DIR "/system/lib"
140#endif
141#endif
142
Peiyong Lin0d10b252019-04-30 18:03:04 -0700143static const char* DRIVER_SUFFIX_PROPERTY = "ro.hardware.egl";
144
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700145static const char* HAL_SUBNAME_KEY_PROPERTIES[2] = {
Peiyong Lin0d10b252019-04-30 18:03:04 -0700146 DRIVER_SUFFIX_PROPERTY,
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700147 "ro.board.platform",
148};
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
162#ifndef __ANDROID_VNDK__
163 // Return true if updated driver namespace is set.
164 ns = android::GraphicsEnv::getInstance().getDriverNamespace();
165 if (ns) {
166 return true;
167 }
168#endif
169
170 return false;
171}
172
173static void uninit_api(char const* const* api, __eglMustCastToProperFunctionPointerType* curr) {
174 while (*api) {
175 *curr++ = nullptr;
176 api++;
177 }
178}
179
180void Loader::unload_system_driver(egl_connection_t* cnx) {
181 ATRACE_CALL();
182
183 uninit_api(gl_names,
184 (__eglMustCastToProperFunctionPointerType*)&cnx
185 ->hooks[egl_connection_t::GLESv2_INDEX]
186 ->gl);
187 uninit_api(gl_names,
188 (__eglMustCastToProperFunctionPointerType*)&cnx
189 ->hooks[egl_connection_t::GLESv1_INDEX]
190 ->gl);
191 uninit_api(egl_names, (__eglMustCastToProperFunctionPointerType*)&cnx->egl);
192
193 if (cnx->dso) {
194 ALOGD("Unload system gl driver.");
195 driver_t* hnd = (driver_t*)cnx->dso;
196 if (hnd->dso[2]) {
197 do_android_unload_sphal_library(hnd->dso[2]);
198 }
199 if (hnd->dso[1]) {
200 do_android_unload_sphal_library(hnd->dso[1]);
201 }
202 if (hnd->dso[0]) {
203 do_android_unload_sphal_library(hnd->dso[0]);
204 }
205 cnx->dso = nullptr;
206 }
207
208 cnx->systemDriverUnloaded = true;
209}
210
Mathias Agopianada798b2012-02-13 17:09:30 -0800211void* Loader::open(egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700212{
Jesse Hall1508ae62017-01-19 17:43:26 -0800213 ATRACE_CALL();
Yiwei Zhangd9861812019-02-13 11:51:55 -0800214 const nsecs_t openTime = systemTime();
Jesse Hall1508ae62017-01-19 17:43:26 -0800215
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700216 if (should_unload_system_driver(cnx)) {
217 unload_system_driver(cnx);
218 }
219
220 // If a driver has been loaded, return the driver directly.
221 if (cnx->dso) {
222 return cnx->dso;
223 }
224
Tim Van Patten5f744f12018-12-12 11:46:21 -0700225 // Check if we should use ANGLE early, so loading each driver doesn't require repeated queries.
226 if (android::GraphicsEnv::getInstance().shouldUseAngle()) {
227 cnx->shouldUseAngle = true;
228 } else {
229 cnx->shouldUseAngle = false;
230 }
231
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700232 // Firstly, try to load ANGLE driver.
233 driver_t* hnd = attempt_to_load_angle(cnx);
234 if (!hnd) {
235 // Secondly, try to load from driver apk.
236 hnd = attempt_to_load_updated_driver(cnx);
237 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700238
239 bool failToLoadFromDriverSuffixProperty = false;
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700240 if (!hnd) {
Peiyong Lindb3ed6e2020-01-09 18:43:27 -0800241 // If updated driver apk is set but fail to load, abort here.
242 if (android::GraphicsEnv::getInstance().getDriverNamespace()) {
243 LOG_ALWAYS_FATAL("couldn't find an OpenGL ES implementation from %s",
244 android::GraphicsEnv::getInstance().getDriverPath().c_str());
245 }
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700246 // Finally, try to load system driver, start by searching for the library name appended by
247 // the system properties of the GLES userspace driver in both locations.
248 // i.e.:
249 // libGLES_${prop}.so, or:
250 // libEGL_${prop}.so, libGLESv1_CM_${prop}.so, libGLESv2_${prop}.so
Charlie Lao840bd532020-01-16 17:34:37 -0800251 char prop[PROPERTY_VALUE_MAX + 1];
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700252 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
Charlie Lao840bd532020-01-16 17:34:37 -0800253 if (property_get(key, prop, nullptr) <= 0) {
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700254 continue;
255 }
Charlie Lao840bd532020-01-16 17:34:37 -0800256 hnd = attempt_to_load_system_driver(cnx, prop, true);
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700257 if (hnd) {
258 break;
Charlie Lao840bd532020-01-16 17:34:37 -0800259 } else if (strcmp(key, DRIVER_SUFFIX_PROPERTY) == 0) {
260 failToLoadFromDriverSuffixProperty = true;
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700261 }
262 }
263 }
264
265 if (!hnd) {
266 // Can't find graphics driver by appending system properties, now search for the exact name
267 // without any suffix of the GLES userspace driver in both locations.
268 // i.e.:
269 // libGLES.so, or:
270 // libEGL.so, libGLESv1_CM.so, libGLESv2.so
Charlie Lao840bd532020-01-16 17:34:37 -0800271 hnd = attempt_to_load_system_driver(cnx, nullptr, true);
Peiyong Lin0d10b252019-04-30 18:03:04 -0700272 }
273
274 if (!hnd && !failToLoadFromDriverSuffixProperty) {
Charlie Lao840bd532020-01-16 17:34:37 -0800275 hnd = attempt_to_load_system_driver(cnx, nullptr, false);
Mathias Agopiande586972009-05-28 17:39:03 -0700276 }
277
Yiwei Zhangd9861812019-02-13 11:51:55 -0800278 if (!hnd) {
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700279 android::GraphicsEnv::getInstance().setDriverLoaded(android::GpuStatsInfo::Api::API_GL,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800280 false, systemTime() - openTime);
281 }
282
Peiyong Lin3575b9f2019-04-25 14:26:49 -0700283 LOG_ALWAYS_FATAL_IF(!hnd,
284 "couldn't find an OpenGL ES implementation, make sure you set %s or %s",
285 HAL_SUBNAME_KEY_PROPERTIES[0], HAL_SUBNAME_KEY_PROPERTIES[1]);
Jesse Hallc07b5202013-07-04 12:08:16 -0700286
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700287 if (!cnx->libEgl) {
288 cnx->libEgl = load_wrapper(EGL_WRAPPER_DIR "/libEGL.so");
289 }
Charlie Lao840bd532020-01-16 17:34:37 -0800290 if (!cnx->libGles1) {
291 cnx->libGles1 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv1_CM.so");
292 }
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700293 if (!cnx->libGles2) {
294 cnx->libGles2 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv2.so");
295 }
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700296
Charlie Lao840bd532020-01-16 17:34:37 -0800297 if (!cnx->libEgl || !cnx->libGles2 || !cnx->libGles1) {
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700298 android::GraphicsEnv::getInstance().setDriverLoaded(android::GpuStatsInfo::Api::API_GL,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800299 false, systemTime() - openTime);
300 }
301
Michael Chockc0ec5e22014-01-27 08:14:33 -0800302 LOG_ALWAYS_FATAL_IF(!cnx->libEgl,
303 "couldn't load system EGL wrapper libraries");
304
Charlie Lao840bd532020-01-16 17:34:37 -0800305 LOG_ALWAYS_FATAL_IF(!cnx->libGles2 || !cnx->libGles1,
306 "couldn't load system OpenGL ES wrapper libraries");
Jesse Hallc07b5202013-07-04 12:08:16 -0700307
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700308 android::GraphicsEnv::getInstance().setDriverLoaded(android::GpuStatsInfo::Api::API_GL, true,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800309 systemTime() - openTime);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800310
Mathias Agopiande586972009-05-28 17:39:03 -0700311 return (void*)hnd;
312}
313
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600314void Loader::close(egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700315{
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600316 driver_t* hnd = (driver_t*) cnx->dso;
Mathias Agopiande586972009-05-28 17:39:03 -0700317 delete hnd;
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600318 cnx->dso = nullptr;
319
Tim Van Patten5f744f12018-12-12 11:46:21 -0700320 cnx->shouldUseAngle = false;
Charlie Lao840bd532020-01-16 17:34:37 -0800321 cnx->useAngle = false;
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600322
323 if (cnx->vendorEGL) {
324 dlclose(cnx->vendorEGL);
325 cnx->vendorEGL = nullptr;
326 }
Mathias Agopiande586972009-05-28 17:39:03 -0700327}
328
Jesse Hall94cdba92013-07-11 09:40:35 -0700329void Loader::init_api(void* dso,
330 char const * const * api,
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700331 char const * const * ref_api,
Jesse Hall94cdba92013-07-11 09:40:35 -0700332 __eglMustCastToProperFunctionPointerType* curr,
333 getProcAddressType getProcAddress)
Mathias Agopiande586972009-05-28 17:39:03 -0700334{
Jesse Hall1508ae62017-01-19 17:43:26 -0800335 ATRACE_CALL();
336
Mathias Agopian7773c432012-02-13 20:06:08 -0800337 const ssize_t SIZE = 256;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700338 char scrap[SIZE];
Mathias Agopiande586972009-05-28 17:39:03 -0700339 while (*api) {
340 char const * name = *api;
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700341 if (ref_api) {
342 char const * ref_name = *ref_api;
343 if (std::strcmp(name, ref_name) != 0) {
344 *curr++ = nullptr;
345 ref_api++;
346 continue;
347 }
348 }
349
Jesse Hall94cdba92013-07-11 09:40:35 -0700350 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700351 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700352 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700353 // couldn't find the entry-point, use eglGetProcAddress()
354 f = getProcAddress(name);
355 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700356 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700357 // Try without the OES postfix
358 ssize_t index = ssize_t(strlen(name)) - 3;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700359 if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) {
Mathias Agopiande586972009-05-28 17:39:03 -0700360 strncpy(scrap, name, index);
361 scrap[index] = 0;
362 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000363 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700364 }
365 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700366 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700367 // Try with the OES postfix
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700368 ssize_t index = ssize_t(strlen(name)) - 3;
369 if (index>0 && strcmp(name+index, "OES")) {
370 snprintf(scrap, SIZE, "%sOES", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700371 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000372 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700373 }
374 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700375 if (f == nullptr) {
Steve Block9d453682011-12-20 16:23:08 +0000376 //ALOGD("%s", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700377 f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented;
Mathias Agopian48d438d2012-01-28 21:44:00 -0800378
379 /*
380 * GL_EXT_debug_label is special, we always report it as
381 * supported, it's handled by GLES_trace. If GLES_trace is not
382 * enabled, then these are no-ops.
383 */
384 if (!strcmp(name, "glInsertEventMarkerEXT")) {
385 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
386 } else if (!strcmp(name, "glPushGroupMarkerEXT")) {
387 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
388 } else if (!strcmp(name, "glPopGroupMarkerEXT")) {
389 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
390 }
Mathias Agopiande586972009-05-28 17:39:03 -0700391 }
392 *curr++ = f;
393 api++;
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700394 if (ref_api) ref_api++;
Mathias Agopiande586972009-05-28 17:39:03 -0700395 }
396}
397
Peiyong Lin0d10b252019-04-30 18:03:04 -0700398static void* load_system_driver(const char* kind, const char* suffix, const bool exact) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800399 ATRACE_CALL();
Mathias Agopian99381422013-04-23 20:52:29 +0200400 class MatchFile {
401 public:
Peiyong Lin0d10b252019-04-30 18:03:04 -0700402 static std::string find(const char* libraryName, const bool exact) {
Mathias Agopian99381422013-04-23 20:52:29 +0200403 const char* const searchPaths[] = {
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800404#if defined(__LP64__)
405 "/vendor/lib64/egl",
406 "/system/lib64/egl"
407#else
Mathias Agopian99381422013-04-23 20:52:29 +0200408 "/vendor/lib/egl",
409 "/system/lib/egl"
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800410#endif
Mathias Agopian99381422013-04-23 20:52:29 +0200411 };
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700412
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700413 for (auto dir : searchPaths) {
Peiyong Lin0d10b252019-04-30 18:03:04 -0700414 std::string absolutePath;
415 if (find(absolutePath, libraryName, dir, exact)) {
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700416 return absolutePath;
Mathias Agopian99381422013-04-23 20:52:29 +0200417 }
Mathias Agopian99381422013-04-23 20:52:29 +0200418 }
419
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700420 // Driver not found. gah.
421 return std::string();
Mathias Agopian99381422013-04-23 20:52:29 +0200422 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700423 private:
424 static bool find(std::string& result,
425 const std::string& pattern, const char* const search, bool exact) {
426 if (exact) {
427 std::string absolutePath = std::string(search) + "/" + pattern + ".so";
428 if (!access(absolutePath.c_str(), R_OK)) {
429 result = absolutePath;
430 return true;
431 }
432 return false;
433 }
434
435 DIR* d = opendir(search);
436 if (d != nullptr) {
437 struct dirent* e;
438 while ((e = readdir(d)) != nullptr) {
439 if (e->d_type == DT_DIR) {
440 continue;
441 }
442 if (!strcmp(e->d_name, "libGLES_android.so")) {
443 // always skip the software renderer
444 continue;
445 }
446 if (strstr(e->d_name, pattern.c_str()) == e->d_name) {
447 if (!strcmp(e->d_name + strlen(e->d_name) - 3, ".so")) {
448 result = std::string(search) + "/" + e->d_name;
449 closedir(d);
450 return true;
451 }
452 }
453 }
454 closedir(d);
455 }
456 return false;
457 }
Mathias Agopian99381422013-04-23 20:52:29 +0200458 };
459
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700460 std::string libraryName = std::string("lib") + kind;
461 if (suffix) {
462 libraryName += std::string("_") + suffix;
Peiyong Lin0d10b252019-04-30 18:03:04 -0700463 } else if (!exact) {
464 // Deprecated: we look for files that match
465 // libGLES_*.so, or:
466 // libEGL_*.so, libGLESv1_CM_*.so, libGLESv2_*.so
467 libraryName += std::string("_");
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700468 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700469 std::string absolutePath = MatchFile::find(libraryName.c_str(), exact);
Mathias Agopian65421432017-03-08 11:49:05 -0800470 if (absolutePath.empty()) {
Mathias Agopian99381422013-04-23 20:52:29 +0200471 // this happens often, we don't want to log an error
Yi Kong48a6cd22018-07-18 10:07:09 -0700472 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700473 }
Mathias Agopian65421432017-03-08 11:49:05 -0800474 const char* const driver_absolute_path = absolutePath.c_str();
Mathias Agopiande586972009-05-28 17:39:03 -0700475
Jiyong Park5910dc72017-04-05 14:23:52 +0900476 // Try to load drivers from the 'sphal' namespace, if it exist. Fall back to
Justin Yunb7320302017-05-22 15:13:40 +0900477 // the original routine when the namespace does not exist.
Jiyong Park5910dc72017-04-05 14:23:52 +0900478 // See /system/core/rootdir/etc/ld.config.txt for the configuration of the
479 // sphal namespace.
Justin Yunb7320302017-05-22 15:13:40 +0900480 void* dso = do_android_load_sphal_library(driver_absolute_path,
481 RTLD_NOW | RTLD_LOCAL);
Yi Kong48a6cd22018-07-18 10:07:09 -0700482 if (dso == nullptr) {
Mathias Agopian8c173842009-09-20 16:01:02 -0700483 const char* err = dlerror();
Mathias Agopian65421432017-03-08 11:49:05 -0800484 ALOGE("load_driver(%s): %s", driver_absolute_path, err ? err : "unknown");
Yi Kong48a6cd22018-07-18 10:07:09 -0700485 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700486 }
487
Steve Block9d453682011-12-20 16:23:08 +0000488 ALOGD("loaded %s", driver_absolute_path);
Mathias Agopianbaca89c2009-08-20 19:09:34 -0700489
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800490 return dso;
491}
492
Cody Northrop1f00e172018-04-02 11:23:31 -0600493static void* load_angle_from_namespace(const char* kind, android_namespace_t* ns) {
494 const android_dlextinfo dlextinfo = {
495 .flags = ANDROID_DLEXT_USE_NAMESPACE,
496 .library_namespace = ns,
497 };
498
499 std::string name = std::string("lib") + kind + "_angle.so";
500
501 void* so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
502
503 if (so) {
504 ALOGD("dlopen_ext from APK (%s) success at %p", name.c_str(), so);
505 return so;
506 } else {
507 ALOGE("dlopen_ext(\"%s\") failed: %s", name.c_str(), dlerror());
508 }
509
510 return nullptr;
511}
512
Cody Northrop6d082ef2018-09-11 09:42:31 -0600513static void* load_angle(const char* kind, android_namespace_t* ns, egl_connection_t* cnx) {
Courtney Goeltzenleuchter985bc282018-11-02 14:24:55 -0600514 void* so = nullptr;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700515
516 if ((cnx->shouldUseAngle) || android::GraphicsEnv::getInstance().shouldUseAngle()) {
Ian Elliott34474472018-09-05 10:57:07 -0600517 so = load_angle_from_namespace(kind, ns);
Tim Van Patten5f744f12018-12-12 11:46:21 -0700518 cnx->shouldUseAngle = true;
519 } else {
520 cnx->shouldUseAngle = false;
Ian Elliott34474472018-09-05 10:57:07 -0600521 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600522
523 if (so) {
Tim Van Patten5f744f12018-12-12 11:46:21 -0700524 ALOGV("Loaded ANGLE %s library for '%s' (instead of native)", kind,
525 android::GraphicsEnv::getInstance().getAngleAppName().c_str());
Charlie Lao840bd532020-01-16 17:34:37 -0800526 cnx->useAngle = true;
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600527
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600528 char prop[PROPERTY_VALUE_MAX];
Tim Van Patten5f744f12018-12-12 11:46:21 -0700529
530 property_get("debug.hwui.renderer", prop, "UNSET");
531 ALOGV("Skia's renderer set to %s", prop);
532
533 EGLint angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE;
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600534 property_get("debug.angle.backend", prop, "0");
535 switch (atoi(prop)) {
536 case 1:
537 ALOGV("%s: Requesting OpenGLES back-end", __FUNCTION__);
538 angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE;
539 break;
540 case 2:
541 ALOGV("%s: Requesting Vulkan back-end", __FUNCTION__);
542 angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE;
543 break;
544 default:
545 break;
546 }
547
548 cnx->angleBackend = angleBackendDefault;
549 if (!cnx->vendorEGL && (cnx->angleBackend == EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE)) {
550 // Find and load vendor libEGL for ANGLE's GL back-end to use.
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700551 char prop[PROPERTY_VALUE_MAX + 1];
552 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
553 if (property_get(key, prop, nullptr) <= 0) {
554 continue;
555 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700556 void* dso = load_system_driver("EGL", prop, true);
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700557 if (dso) {
558 cnx->vendorEGL = dso;
559 break;
560 }
561 }
562 if (!cnx->vendorEGL) {
Peiyong Lin0d10b252019-04-30 18:03:04 -0700563 cnx->vendorEGL = load_system_driver("EGL", nullptr, true);
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700564 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600565 }
Ian Elliottb058aff2018-08-09 12:00:55 -0600566 } else {
Tim Van Patten5f744f12018-12-12 11:46:21 -0700567 ALOGV("Loaded native %s library for '%s' (instead of ANGLE)", kind,
568 android::GraphicsEnv::getInstance().getAngleAppName().c_str());
Charlie Lao840bd532020-01-16 17:34:37 -0800569 cnx->useAngle = false;
Cody Northrop1f00e172018-04-02 11:23:31 -0600570 }
571
Tim Van Patten5f744f12018-12-12 11:46:21 -0700572 return so;
Cody Northrop1f00e172018-04-02 11:23:31 -0600573}
574
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800575static void* load_updated_driver(const char* kind, android_namespace_t* ns) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800576 ATRACE_CALL();
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800577 const android_dlextinfo dlextinfo = {
578 .flags = ANDROID_DLEXT_USE_NAMESPACE,
579 .library_namespace = ns,
580 };
581 void* so = nullptr;
582 char prop[PROPERTY_VALUE_MAX + 1];
583 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700584 if (property_get(key, prop, nullptr) <= 0) {
585 continue;
586 }
587 std::string name = std::string("lib") + kind + "_" + prop + ".so";
588 so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
589 if (so) {
590 return so;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800591 }
592 }
593 return nullptr;
594}
595
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700596Loader::driver_t* Loader::attempt_to_load_angle(egl_connection_t* cnx) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800597 ATRACE_CALL();
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600598 android_namespace_t* ns = android::GraphicsEnv::getInstance().getAngleNamespace();
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700599 if (!ns) {
600 return nullptr;
Cody Northrop6d082ef2018-09-11 09:42:31 -0600601 }
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700602
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700603 android::GraphicsEnv::getInstance().setDriverToLoad(android::GpuStatsInfo::Driver::ANGLE);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700604 driver_t* hnd = nullptr;
605
606 // ANGLE doesn't ship with GLES library, and thus we skip GLES driver.
607 void* dso = load_angle("EGL", ns, cnx);
608 if (dso) {
609 initialize_api(dso, cnx, EGL);
610 hnd = new driver_t(dso);
611
Charlie Lao840bd532020-01-16 17:34:37 -0800612 dso = load_angle("GLESv1_CM", ns, cnx);
613 initialize_api(dso, cnx, GLESv1_CM);
614 hnd->set(dso, GLESv1_CM);
615
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700616 dso = load_angle("GLESv2", ns, cnx);
617 initialize_api(dso, cnx, GLESv2);
618 hnd->set(dso, GLESv2);
619 }
620 return hnd;
621}
622
623Loader::driver_t* Loader::attempt_to_load_updated_driver(egl_connection_t* cnx) {
624 ATRACE_CALL();
Jiyong Parka243e5d2017-06-21 12:26:51 +0900625#ifndef __ANDROID_VNDK__
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700626 android_namespace_t* ns = android::GraphicsEnv::getInstance().getDriverNamespace();
627 if (!ns) {
628 return nullptr;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800629 }
630
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700631 ALOGD("Load updated gl driver.");
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700632 android::GraphicsEnv::getInstance().setDriverToLoad(android::GpuStatsInfo::Driver::GL_UPDATED);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700633 driver_t* hnd = nullptr;
634 void* dso = load_updated_driver("GLES", ns);
Peiyong Line83b8682019-04-18 17:23:02 -0700635 if (dso) {
Charlie Lao840bd532020-01-16 17:34:37 -0800636 initialize_api(dso, cnx, EGL | GLESv1_CM | GLESv2);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700637 hnd = new driver_t(dso);
638 return hnd;
Peiyong Line83b8682019-04-18 17:23:02 -0700639 }
640
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700641 dso = load_updated_driver("EGL", ns);
642 if (dso) {
643 initialize_api(dso, cnx, EGL);
644 hnd = new driver_t(dso);
645
Charlie Lao840bd532020-01-16 17:34:37 -0800646 dso = load_updated_driver("GLESv1_CM", ns);
647 initialize_api(dso, cnx, GLESv1_CM);
648 hnd->set(dso, GLESv1_CM);
649
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700650 dso = load_updated_driver("GLESv2", ns);
651 initialize_api(dso, cnx, GLESv2);
652 hnd->set(dso, GLESv2);
653 }
654 return hnd;
655#else
Peiyong Line83b8682019-04-18 17:23:02 -0700656 return nullptr;
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700657#endif
658}
659
Peiyong Lin0d10b252019-04-30 18:03:04 -0700660Loader::driver_t* Loader::attempt_to_load_system_driver(egl_connection_t* cnx, const char* suffix,
661 const bool exact) {
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700662 ATRACE_CALL();
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700663 android::GraphicsEnv::getInstance().setDriverToLoad(android::GpuStatsInfo::Driver::GL);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700664 driver_t* hnd = nullptr;
Peiyong Lin0d10b252019-04-30 18:03:04 -0700665 void* dso = load_system_driver("GLES", suffix, exact);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700666 if (dso) {
Charlie Lao840bd532020-01-16 17:34:37 -0800667 initialize_api(dso, cnx, EGL | GLESv1_CM | GLESv2);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700668 hnd = new driver_t(dso);
669 return hnd;
670 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700671 dso = load_system_driver("EGL", suffix, exact);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700672 if (dso) {
673 initialize_api(dso, cnx, EGL);
674 hnd = new driver_t(dso);
675
Charlie Lao840bd532020-01-16 17:34:37 -0800676 dso = load_system_driver("GLESv1_CM", suffix, exact);
677 initialize_api(dso, cnx, GLESv1_CM);
678 hnd->set(dso, GLESv1_CM);
679
Peiyong Lin0d10b252019-04-30 18:03:04 -0700680 dso = load_system_driver("GLESv2", suffix, exact);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700681 initialize_api(dso, cnx, GLESv2);
682 hnd->set(dso, GLESv2);
683 }
684 return hnd;
Peiyong Line83b8682019-04-18 17:23:02 -0700685}
686
687void Loader::initialize_api(void* dso, egl_connection_t* cnx, uint32_t mask) {
Mathias Agopiande586972009-05-28 17:39:03 -0700688 if (mask & EGL) {
689 getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress");
690
Jesse Hall94cdba92013-07-11 09:40:35 -0700691 ALOGE_IF(!getProcAddress,
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800692 "can't find eglGetProcAddress() in EGL driver library");
Mathias Agopiande586972009-05-28 17:39:03 -0700693
Mathias Agopian618fa102009-10-14 02:06:37 -0700694 egl_t* egl = &cnx->egl;
Mathias Agopiande586972009-05-28 17:39:03 -0700695 __eglMustCastToProperFunctionPointerType* curr =
696 (__eglMustCastToProperFunctionPointerType*)egl;
697 char const * const * api = egl_names;
698 while (*api) {
699 char const * name = *api;
Jesse Hall94cdba92013-07-11 09:40:35 -0700700 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700701 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700702 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700703 // couldn't find the entry-point, use eglGetProcAddress()
704 f = getProcAddress(name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700705 if (f == nullptr) {
706 f = (__eglMustCastToProperFunctionPointerType)nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700707 }
708 }
709 *curr++ = f;
710 api++;
711 }
712 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700713
Mathias Agopiande586972009-05-28 17:39:03 -0700714 if (mask & GLESv1_CM) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700715 init_api(dso, gl_names_1, gl_names,
Mathias Agopian618fa102009-10-14 02:06:37 -0700716 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800717 &cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl,
Mathias Agopian618fa102009-10-14 02:06:37 -0700718 getProcAddress);
Mathias Agopiande586972009-05-28 17:39:03 -0700719 }
720
721 if (mask & GLESv2) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700722 init_api(dso, gl_names, nullptr,
Mathias Agopian618fa102009-10-14 02:06:37 -0700723 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800724 &cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl,
Mathias Agopiande586972009-05-28 17:39:03 -0700725 getProcAddress);
726 }
Mathias Agopiande586972009-05-28 17:39:03 -0700727}
728
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700729} // namespace android