blob: 038a43233774a48797b19d62ba701daf4b5b15bd [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>
David 'Digit' Turner80b30c22011-08-26 17:38:47 +020028#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 *
54 * The software renderer for the emulator must be provided as a single
55 * library at:
56 *
57 * /system/lib/egl/libGLES_android.so
58 *
59 *
60 * For backward compatibility and to facilitate the transition to
61 * this new naming scheme, the loader will additionally look for:
Jesse Hall94cdba92013-07-11 09:40:35 -070062 *
Mathias Agopian99381422013-04-23 20:52:29 +020063 * /{vendor|system}/lib/egl/lib{GLES | [EGL|GLESv1_CM|GLESv2]}_*.so
Jesse Hall94cdba92013-07-11 09:40:35 -070064 *
Mathias Agopiande586972009-05-28 17:39:03 -070065 */
66
Mathias Agopian65421432017-03-08 11:49:05 -080067Loader& Loader::getInstance() {
68 static Loader loader;
69 return loader;
70}
Mathias Agopiande586972009-05-28 17:39:03 -070071
Jesse Hall1508ae62017-01-19 17:43:26 -080072static void* do_dlopen(const char* path, int mode) {
73 ATRACE_CALL();
74 return dlopen(path, mode);
75}
76
Jiyong Park5910dc72017-04-05 14:23:52 +090077static void* do_android_dlopen_ext(const char* path, int mode, const android_dlextinfo* info) {
78 ATRACE_CALL();
79 return android_dlopen_ext(path, mode, info);
80}
81
Justin Yunb7320302017-05-22 15:13:40 +090082static void* do_android_load_sphal_library(const char* path, int mode) {
83 ATRACE_CALL();
84 return android_load_sphal_library(path, mode);
85}
86
Yiwei Zhang5e21eb32019-06-05 00:26:03 -070087static int do_android_unload_sphal_library(void* dso) {
88 ATRACE_CALL();
89 return android_unload_sphal_library(dso);
90}
91
Jesse Hall94cdba92013-07-11 09:40:35 -070092Loader::driver_t::driver_t(void* gles)
Mathias Agopiande586972009-05-28 17:39:03 -070093{
94 dso[0] = gles;
95 for (size_t i=1 ; i<NELEM(dso) ; i++)
Yi Kong48a6cd22018-07-18 10:07:09 -070096 dso[i] = nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -070097}
98
Jesse Hall94cdba92013-07-11 09:40:35 -070099Loader::driver_t::~driver_t()
Mathias Agopiande586972009-05-28 17:39:03 -0700100{
101 for (size_t i=0 ; i<NELEM(dso) ; i++) {
102 if (dso[i]) {
103 dlclose(dso[i]);
Yi Kong48a6cd22018-07-18 10:07:09 -0700104 dso[i] = nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700105 }
106 }
107}
108
Mathias Agopian65421432017-03-08 11:49:05 -0800109int Loader::driver_t::set(void* hnd, int32_t api)
Mathias Agopiande586972009-05-28 17:39:03 -0700110{
111 switch (api) {
112 case EGL:
113 dso[0] = hnd;
114 break;
115 case GLESv1_CM:
116 dso[1] = hnd;
117 break;
118 case GLESv2:
119 dso[2] = hnd;
120 break;
121 default:
Mathias Agopian65421432017-03-08 11:49:05 -0800122 return -EOVERFLOW;
Mathias Agopiande586972009-05-28 17:39:03 -0700123 }
Mathias Agopian65421432017-03-08 11:49:05 -0800124 return 0;
Mathias Agopiande586972009-05-28 17:39:03 -0700125}
126
Mathias Agopiande586972009-05-28 17:39:03 -0700127Loader::Loader()
Yi Kong48a6cd22018-07-18 10:07:09 -0700128 : getProcAddress(nullptr)
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800129{
Mathias Agopiande586972009-05-28 17:39:03 -0700130}
131
Mathias Agopian99381422013-04-23 20:52:29 +0200132Loader::~Loader() {
Mathias Agopiande586972009-05-28 17:39:03 -0700133}
134
Jesse Hallc07b5202013-07-04 12:08:16 -0700135static void* load_wrapper(const char* path) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800136 void* so = do_dlopen(path, RTLD_NOW | RTLD_LOCAL);
Jesse Hallc07b5202013-07-04 12:08:16 -0700137 ALOGE_IF(!so, "dlopen(\"%s\") failed: %s", path, dlerror());
138 return so;
139}
140
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700141#ifndef EGL_WRAPPER_DIR
142#if defined(__LP64__)
143#define EGL_WRAPPER_DIR "/system/lib64"
144#else
145#define EGL_WRAPPER_DIR "/system/lib"
146#endif
147#endif
148
bohu69e5b1a2016-02-19 17:15:20 -0800149static void setEmulatorGlesValue(void) {
150 char prop[PROPERTY_VALUE_MAX];
151 property_get("ro.kernel.qemu", prop, "0");
152 if (atoi(prop) != 1) return;
153
154 property_get("ro.kernel.qemu.gles",prop,"0");
155 if (atoi(prop) == 1) {
156 ALOGD("Emulator has host GPU support, qemu.gles is set to 1.");
157 property_set("qemu.gles", "1");
158 return;
159 }
160
161 // for now, checking the following
162 // directory is good enough for emulator system images
163 const char* vendor_lib_path =
164#if defined(__LP64__)
165 "/vendor/lib64/egl";
166#else
167 "/vendor/lib/egl";
168#endif
169
170 const bool has_vendor_lib = (access(vendor_lib_path, R_OK) == 0);
171 if (has_vendor_lib) {
172 ALOGD("Emulator has vendor provided software renderer, qemu.gles is set to 2.");
173 property_set("qemu.gles", "2");
174 } else {
175 ALOGD("Emulator without GPU support detected. "
176 "Fallback to legacy software renderer, qemu.gles is set to 0.");
177 property_set("qemu.gles", "0");
178 }
179}
180
Peiyong Lin0d10b252019-04-30 18:03:04 -0700181static const char* DRIVER_SUFFIX_PROPERTY = "ro.hardware.egl";
182
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700183static const char* HAL_SUBNAME_KEY_PROPERTIES[2] = {
Peiyong Lin0d10b252019-04-30 18:03:04 -0700184 DRIVER_SUFFIX_PROPERTY,
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700185 "ro.board.platform",
186};
187
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700188static bool should_unload_system_driver(egl_connection_t* cnx) {
189 // Return false if the system driver has been unloaded once.
190 if (cnx->systemDriverUnloaded) {
191 return false;
192 }
193
194 // Return true if Angle namespace is set.
195 android_namespace_t* ns = android::GraphicsEnv::getInstance().getAngleNamespace();
196 if (ns) {
197 return true;
198 }
199
200#ifndef __ANDROID_VNDK__
201 // Return true if updated driver namespace is set.
202 ns = android::GraphicsEnv::getInstance().getDriverNamespace();
203 if (ns) {
204 return true;
205 }
206#endif
207
208 return false;
209}
210
211static void uninit_api(char const* const* api, __eglMustCastToProperFunctionPointerType* curr) {
212 while (*api) {
213 *curr++ = nullptr;
214 api++;
215 }
216}
217
218void Loader::unload_system_driver(egl_connection_t* cnx) {
219 ATRACE_CALL();
220
221 uninit_api(gl_names,
222 (__eglMustCastToProperFunctionPointerType*)&cnx
223 ->hooks[egl_connection_t::GLESv2_INDEX]
224 ->gl);
225 uninit_api(gl_names,
226 (__eglMustCastToProperFunctionPointerType*)&cnx
227 ->hooks[egl_connection_t::GLESv1_INDEX]
228 ->gl);
229 uninit_api(egl_names, (__eglMustCastToProperFunctionPointerType*)&cnx->egl);
230
231 if (cnx->dso) {
232 ALOGD("Unload system gl driver.");
233 driver_t* hnd = (driver_t*)cnx->dso;
234 if (hnd->dso[2]) {
235 do_android_unload_sphal_library(hnd->dso[2]);
236 }
237 if (hnd->dso[1]) {
238 do_android_unload_sphal_library(hnd->dso[1]);
239 }
240 if (hnd->dso[0]) {
241 do_android_unload_sphal_library(hnd->dso[0]);
242 }
243 cnx->dso = nullptr;
244 }
245
246 cnx->systemDriverUnloaded = true;
247}
248
Mathias Agopianada798b2012-02-13 17:09:30 -0800249void* Loader::open(egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700250{
Jesse Hall1508ae62017-01-19 17:43:26 -0800251 ATRACE_CALL();
Yiwei Zhangd9861812019-02-13 11:51:55 -0800252 const nsecs_t openTime = systemTime();
Jesse Hall1508ae62017-01-19 17:43:26 -0800253
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700254 if (should_unload_system_driver(cnx)) {
255 unload_system_driver(cnx);
256 }
257
258 // If a driver has been loaded, return the driver directly.
259 if (cnx->dso) {
260 return cnx->dso;
261 }
262
bohu69e5b1a2016-02-19 17:15:20 -0800263 setEmulatorGlesValue();
264
Tim Van Patten5f744f12018-12-12 11:46:21 -0700265 // Check if we should use ANGLE early, so loading each driver doesn't require repeated queries.
266 if (android::GraphicsEnv::getInstance().shouldUseAngle()) {
267 cnx->shouldUseAngle = true;
268 } else {
269 cnx->shouldUseAngle = false;
270 }
271
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700272 // Firstly, try to load ANGLE driver.
273 driver_t* hnd = attempt_to_load_angle(cnx);
274 if (!hnd) {
275 // Secondly, try to load from driver apk.
276 hnd = attempt_to_load_updated_driver(cnx);
277 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700278
279 bool failToLoadFromDriverSuffixProperty = false;
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700280 if (!hnd) {
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700281 // Finally, try to load system driver, start by searching for the library name appended by
282 // the system properties of the GLES userspace driver in both locations.
283 // i.e.:
284 // libGLES_${prop}.so, or:
285 // libEGL_${prop}.so, libGLESv1_CM_${prop}.so, libGLESv2_${prop}.so
286 char prop[PROPERTY_VALUE_MAX + 1];
287 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
288 if (property_get(key, prop, nullptr) <= 0) {
289 continue;
290 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700291 hnd = attempt_to_load_system_driver(cnx, prop, true);
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700292 if (hnd) {
293 break;
Peiyong Lin0d10b252019-04-30 18:03:04 -0700294 } else if (strcmp(key, DRIVER_SUFFIX_PROPERTY) == 0) {
295 failToLoadFromDriverSuffixProperty = true;
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700296 }
297 }
298 }
299
300 if (!hnd) {
301 // Can't find graphics driver by appending system properties, now search for the exact name
302 // without any suffix of the GLES userspace driver in both locations.
303 // i.e.:
304 // libGLES.so, or:
305 // libEGL.so, libGLESv1_CM.so, libGLESv2.so
Peiyong Lin0d10b252019-04-30 18:03:04 -0700306 hnd = attempt_to_load_system_driver(cnx, nullptr, true);
307 }
308
309 if (!hnd && !failToLoadFromDriverSuffixProperty) {
310 hnd = attempt_to_load_system_driver(cnx, nullptr, false);
Mathias Agopiande586972009-05-28 17:39:03 -0700311 }
312
Yiwei Zhangd9861812019-02-13 11:51:55 -0800313 if (!hnd) {
314 android::GraphicsEnv::getInstance().setDriverLoaded(android::GraphicsEnv::Api::API_GL,
315 false, systemTime() - openTime);
316 }
317
Peiyong Lin3575b9f2019-04-25 14:26:49 -0700318 LOG_ALWAYS_FATAL_IF(!hnd,
319 "couldn't find an OpenGL ES implementation, make sure you set %s or %s",
320 HAL_SUBNAME_KEY_PROPERTIES[0], HAL_SUBNAME_KEY_PROPERTIES[1]);
Jesse Hallc07b5202013-07-04 12:08:16 -0700321
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700322 if (!cnx->libEgl) {
323 cnx->libEgl = load_wrapper(EGL_WRAPPER_DIR "/libEGL.so");
324 }
325 if (!cnx->libGles1) {
326 cnx->libGles1 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv1_CM.so");
327 }
328 if (!cnx->libGles2) {
329 cnx->libGles2 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv2.so");
330 }
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700331
Yiwei Zhangd9861812019-02-13 11:51:55 -0800332 if (!cnx->libEgl || !cnx->libGles2 || !cnx->libGles1) {
333 android::GraphicsEnv::getInstance().setDriverLoaded(android::GraphicsEnv::Api::API_GL,
334 false, systemTime() - openTime);
335 }
336
Michael Chockc0ec5e22014-01-27 08:14:33 -0800337 LOG_ALWAYS_FATAL_IF(!cnx->libEgl,
338 "couldn't load system EGL wrapper libraries");
339
Jesse Hallc07b5202013-07-04 12:08:16 -0700340 LOG_ALWAYS_FATAL_IF(!cnx->libGles2 || !cnx->libGles1,
341 "couldn't load system OpenGL ES wrapper libraries");
342
Yiwei Zhangd9861812019-02-13 11:51:55 -0800343 android::GraphicsEnv::getInstance().setDriverLoaded(android::GraphicsEnv::Api::API_GL, true,
344 systemTime() - openTime);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800345
Mathias Agopiande586972009-05-28 17:39:03 -0700346 return (void*)hnd;
347}
348
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600349void Loader::close(egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700350{
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600351 driver_t* hnd = (driver_t*) cnx->dso;
Mathias Agopiande586972009-05-28 17:39:03 -0700352 delete hnd;
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600353 cnx->dso = nullptr;
354
Tim Van Patten5f744f12018-12-12 11:46:21 -0700355 cnx->shouldUseAngle = false;
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600356 cnx->angleDecided = false;
357 cnx->useAngle = false;
358
359 if (cnx->vendorEGL) {
360 dlclose(cnx->vendorEGL);
361 cnx->vendorEGL = nullptr;
362 }
Mathias Agopiande586972009-05-28 17:39:03 -0700363}
364
Jesse Hall94cdba92013-07-11 09:40:35 -0700365void Loader::init_api(void* dso,
366 char const * const * api,
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700367 char const * const * ref_api,
Jesse Hall94cdba92013-07-11 09:40:35 -0700368 __eglMustCastToProperFunctionPointerType* curr,
369 getProcAddressType getProcAddress)
Mathias Agopiande586972009-05-28 17:39:03 -0700370{
Jesse Hall1508ae62017-01-19 17:43:26 -0800371 ATRACE_CALL();
372
Mathias Agopian7773c432012-02-13 20:06:08 -0800373 const ssize_t SIZE = 256;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700374 char scrap[SIZE];
Mathias Agopiande586972009-05-28 17:39:03 -0700375 while (*api) {
376 char const * name = *api;
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700377 if (ref_api) {
378 char const * ref_name = *ref_api;
379 if (std::strcmp(name, ref_name) != 0) {
380 *curr++ = nullptr;
381 ref_api++;
382 continue;
383 }
384 }
385
Jesse Hall94cdba92013-07-11 09:40:35 -0700386 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700387 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700388 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700389 // couldn't find the entry-point, use eglGetProcAddress()
390 f = getProcAddress(name);
391 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700392 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700393 // Try without the OES postfix
394 ssize_t index = ssize_t(strlen(name)) - 3;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700395 if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) {
Mathias Agopiande586972009-05-28 17:39:03 -0700396 strncpy(scrap, name, index);
397 scrap[index] = 0;
398 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000399 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700400 }
401 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700402 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700403 // Try with the OES postfix
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700404 ssize_t index = ssize_t(strlen(name)) - 3;
405 if (index>0 && strcmp(name+index, "OES")) {
406 snprintf(scrap, SIZE, "%sOES", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700407 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000408 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700409 }
410 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700411 if (f == nullptr) {
Steve Block9d453682011-12-20 16:23:08 +0000412 //ALOGD("%s", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700413 f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented;
Mathias Agopian48d438d2012-01-28 21:44:00 -0800414
415 /*
416 * GL_EXT_debug_label is special, we always report it as
417 * supported, it's handled by GLES_trace. If GLES_trace is not
418 * enabled, then these are no-ops.
419 */
420 if (!strcmp(name, "glInsertEventMarkerEXT")) {
421 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
422 } else if (!strcmp(name, "glPushGroupMarkerEXT")) {
423 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
424 } else if (!strcmp(name, "glPopGroupMarkerEXT")) {
425 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
426 }
Mathias Agopiande586972009-05-28 17:39:03 -0700427 }
428 *curr++ = f;
429 api++;
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700430 if (ref_api) ref_api++;
Mathias Agopiande586972009-05-28 17:39:03 -0700431 }
432}
433
Peiyong Lin0d10b252019-04-30 18:03:04 -0700434static void* load_system_driver(const char* kind, const char* suffix, const bool exact) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800435 ATRACE_CALL();
Mathias Agopian99381422013-04-23 20:52:29 +0200436 class MatchFile {
437 public:
Peiyong Lin0d10b252019-04-30 18:03:04 -0700438 static std::string find(const char* libraryName, const bool exact) {
Mathias Agopian99381422013-04-23 20:52:29 +0200439 const char* const searchPaths[] = {
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800440#if defined(__LP64__)
441 "/vendor/lib64/egl",
442 "/system/lib64/egl"
443#else
Mathias Agopian99381422013-04-23 20:52:29 +0200444 "/vendor/lib/egl",
445 "/system/lib/egl"
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800446#endif
Mathias Agopian99381422013-04-23 20:52:29 +0200447 };
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700448
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700449 for (auto dir : searchPaths) {
Peiyong Lin0d10b252019-04-30 18:03:04 -0700450 std::string absolutePath;
451 if (find(absolutePath, libraryName, dir, exact)) {
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700452 return absolutePath;
Mathias Agopian99381422013-04-23 20:52:29 +0200453 }
Mathias Agopian99381422013-04-23 20:52:29 +0200454 }
455
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700456 // Driver not found. gah.
457 return std::string();
Mathias Agopian99381422013-04-23 20:52:29 +0200458 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700459 private:
460 static bool find(std::string& result,
461 const std::string& pattern, const char* const search, bool exact) {
462 if (exact) {
463 std::string absolutePath = std::string(search) + "/" + pattern + ".so";
464 if (!access(absolutePath.c_str(), R_OK)) {
465 result = absolutePath;
466 return true;
467 }
468 return false;
469 }
470
471 DIR* d = opendir(search);
472 if (d != nullptr) {
473 struct dirent* e;
474 while ((e = readdir(d)) != nullptr) {
475 if (e->d_type == DT_DIR) {
476 continue;
477 }
478 if (!strcmp(e->d_name, "libGLES_android.so")) {
479 // always skip the software renderer
480 continue;
481 }
482 if (strstr(e->d_name, pattern.c_str()) == e->d_name) {
483 if (!strcmp(e->d_name + strlen(e->d_name) - 3, ".so")) {
484 result = std::string(search) + "/" + e->d_name;
485 closedir(d);
486 return true;
487 }
488 }
489 }
490 closedir(d);
491 }
492 return false;
493 }
Mathias Agopian99381422013-04-23 20:52:29 +0200494 };
495
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700496 std::string libraryName = std::string("lib") + kind;
497 if (suffix) {
498 libraryName += std::string("_") + suffix;
Peiyong Lin0d10b252019-04-30 18:03:04 -0700499 } else if (!exact) {
500 // Deprecated: we look for files that match
501 // libGLES_*.so, or:
502 // libEGL_*.so, libGLESv1_CM_*.so, libGLESv2_*.so
503 libraryName += std::string("_");
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700504 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700505 std::string absolutePath = MatchFile::find(libraryName.c_str(), exact);
Mathias Agopian65421432017-03-08 11:49:05 -0800506 if (absolutePath.empty()) {
Mathias Agopian99381422013-04-23 20:52:29 +0200507 // this happens often, we don't want to log an error
Yi Kong48a6cd22018-07-18 10:07:09 -0700508 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700509 }
Mathias Agopian65421432017-03-08 11:49:05 -0800510 const char* const driver_absolute_path = absolutePath.c_str();
Mathias Agopiande586972009-05-28 17:39:03 -0700511
Jiyong Park5910dc72017-04-05 14:23:52 +0900512 // Try to load drivers from the 'sphal' namespace, if it exist. Fall back to
Justin Yunb7320302017-05-22 15:13:40 +0900513 // the original routine when the namespace does not exist.
Jiyong Park5910dc72017-04-05 14:23:52 +0900514 // See /system/core/rootdir/etc/ld.config.txt for the configuration of the
515 // sphal namespace.
Justin Yunb7320302017-05-22 15:13:40 +0900516 void* dso = do_android_load_sphal_library(driver_absolute_path,
517 RTLD_NOW | RTLD_LOCAL);
Yi Kong48a6cd22018-07-18 10:07:09 -0700518 if (dso == nullptr) {
Mathias Agopian8c173842009-09-20 16:01:02 -0700519 const char* err = dlerror();
Mathias Agopian65421432017-03-08 11:49:05 -0800520 ALOGE("load_driver(%s): %s", driver_absolute_path, err ? err : "unknown");
Yi Kong48a6cd22018-07-18 10:07:09 -0700521 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700522 }
523
Steve Block9d453682011-12-20 16:23:08 +0000524 ALOGD("loaded %s", driver_absolute_path);
Mathias Agopianbaca89c2009-08-20 19:09:34 -0700525
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800526 return dso;
527}
528
Cody Northrop1f00e172018-04-02 11:23:31 -0600529static void* load_angle_from_namespace(const char* kind, android_namespace_t* ns) {
530 const android_dlextinfo dlextinfo = {
531 .flags = ANDROID_DLEXT_USE_NAMESPACE,
532 .library_namespace = ns,
533 };
534
535 std::string name = std::string("lib") + kind + "_angle.so";
536
537 void* so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
538
539 if (so) {
540 ALOGD("dlopen_ext from APK (%s) success at %p", name.c_str(), so);
541 return so;
542 } else {
543 ALOGE("dlopen_ext(\"%s\") failed: %s", name.c_str(), dlerror());
544 }
545
546 return nullptr;
547}
548
Cody Northrop6d082ef2018-09-11 09:42:31 -0600549static void* load_angle(const char* kind, android_namespace_t* ns, egl_connection_t* cnx) {
Courtney Goeltzenleuchter985bc282018-11-02 14:24:55 -0600550 void* so = nullptr;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700551
552 if ((cnx->shouldUseAngle) || android::GraphicsEnv::getInstance().shouldUseAngle()) {
Ian Elliott34474472018-09-05 10:57:07 -0600553 so = load_angle_from_namespace(kind, ns);
Tim Van Patten5f744f12018-12-12 11:46:21 -0700554 cnx->shouldUseAngle = true;
555 } else {
556 cnx->shouldUseAngle = false;
Ian Elliott34474472018-09-05 10:57:07 -0600557 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600558
559 if (so) {
Tim Van Patten5f744f12018-12-12 11:46:21 -0700560 ALOGV("Loaded ANGLE %s library for '%s' (instead of native)", kind,
561 android::GraphicsEnv::getInstance().getAngleAppName().c_str());
Cody Northrop1f00e172018-04-02 11:23:31 -0600562 cnx->useAngle = true;
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600563
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600564 char prop[PROPERTY_VALUE_MAX];
Tim Van Patten5f744f12018-12-12 11:46:21 -0700565
566 property_get("debug.hwui.renderer", prop, "UNSET");
567 ALOGV("Skia's renderer set to %s", prop);
568
569 EGLint angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE;
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600570 property_get("debug.angle.backend", prop, "0");
571 switch (atoi(prop)) {
572 case 1:
573 ALOGV("%s: Requesting OpenGLES back-end", __FUNCTION__);
574 angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE;
575 break;
576 case 2:
577 ALOGV("%s: Requesting Vulkan back-end", __FUNCTION__);
578 angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE;
579 break;
580 default:
581 break;
582 }
583
584 cnx->angleBackend = angleBackendDefault;
585 if (!cnx->vendorEGL && (cnx->angleBackend == EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE)) {
586 // Find and load vendor libEGL for ANGLE's GL back-end to use.
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700587 char prop[PROPERTY_VALUE_MAX + 1];
588 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
589 if (property_get(key, prop, nullptr) <= 0) {
590 continue;
591 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700592 void* dso = load_system_driver("EGL", prop, true);
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700593 if (dso) {
594 cnx->vendorEGL = dso;
595 break;
596 }
597 }
598 if (!cnx->vendorEGL) {
Peiyong Lin0d10b252019-04-30 18:03:04 -0700599 cnx->vendorEGL = load_system_driver("EGL", nullptr, true);
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700600 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600601 }
Ian Elliottb058aff2018-08-09 12:00:55 -0600602 } else {
Tim Van Patten5f744f12018-12-12 11:46:21 -0700603 ALOGV("Loaded native %s library for '%s' (instead of ANGLE)", kind,
604 android::GraphicsEnv::getInstance().getAngleAppName().c_str());
605 cnx->useAngle = false;
Cody Northrop1f00e172018-04-02 11:23:31 -0600606 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700607 cnx->angleDecided = true;
Cody Northrop1f00e172018-04-02 11:23:31 -0600608
Tim Van Patten5f744f12018-12-12 11:46:21 -0700609 return so;
Cody Northrop1f00e172018-04-02 11:23:31 -0600610}
611
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800612static void* load_updated_driver(const char* kind, android_namespace_t* ns) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800613 ATRACE_CALL();
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800614 const android_dlextinfo dlextinfo = {
615 .flags = ANDROID_DLEXT_USE_NAMESPACE,
616 .library_namespace = ns,
617 };
618 void* so = nullptr;
619 char prop[PROPERTY_VALUE_MAX + 1];
620 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700621 if (property_get(key, prop, nullptr) <= 0) {
622 continue;
623 }
624 std::string name = std::string("lib") + kind + "_" + prop + ".so";
625 so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
626 if (so) {
627 return so;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800628 }
629 }
630 return nullptr;
631}
632
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700633Loader::driver_t* Loader::attempt_to_load_angle(egl_connection_t* cnx) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800634 ATRACE_CALL();
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600635 android_namespace_t* ns = android::GraphicsEnv::getInstance().getAngleNamespace();
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700636 if (!ns) {
637 return nullptr;
Cody Northrop6d082ef2018-09-11 09:42:31 -0600638 }
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700639
640 android::GraphicsEnv::getInstance().setDriverToLoad(android::GraphicsEnv::Driver::ANGLE);
641 driver_t* hnd = nullptr;
642
643 // ANGLE doesn't ship with GLES library, and thus we skip GLES driver.
644 void* dso = load_angle("EGL", ns, cnx);
645 if (dso) {
646 initialize_api(dso, cnx, EGL);
647 hnd = new driver_t(dso);
648
649 dso = load_angle("GLESv1_CM", ns, cnx);
650 initialize_api(dso, cnx, GLESv1_CM);
651 hnd->set(dso, GLESv1_CM);
652
653 dso = load_angle("GLESv2", ns, cnx);
654 initialize_api(dso, cnx, GLESv2);
655 hnd->set(dso, GLESv2);
656 }
657 return hnd;
658}
659
660Loader::driver_t* Loader::attempt_to_load_updated_driver(egl_connection_t* cnx) {
661 ATRACE_CALL();
Jiyong Parka243e5d2017-06-21 12:26:51 +0900662#ifndef __ANDROID_VNDK__
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700663 android_namespace_t* ns = android::GraphicsEnv::getInstance().getDriverNamespace();
664 if (!ns) {
665 return nullptr;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800666 }
667
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700668 ALOGD("Load updated gl driver.");
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700669 android::GraphicsEnv::getInstance().setDriverToLoad(android::GraphicsEnv::Driver::GL_UPDATED);
670 driver_t* hnd = nullptr;
671 void* dso = load_updated_driver("GLES", ns);
Peiyong Line83b8682019-04-18 17:23:02 -0700672 if (dso) {
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700673 initialize_api(dso, cnx, EGL | GLESv1_CM | GLESv2);
674 hnd = new driver_t(dso);
675 return hnd;
Peiyong Line83b8682019-04-18 17:23:02 -0700676 }
677
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700678 dso = load_updated_driver("EGL", ns);
679 if (dso) {
680 initialize_api(dso, cnx, EGL);
681 hnd = new driver_t(dso);
682
683 dso = load_updated_driver("GLESv1_CM", ns);
684 initialize_api(dso, cnx, GLESv1_CM);
685 hnd->set(dso, GLESv1_CM);
686
687 dso = load_updated_driver("GLESv2", ns);
688 initialize_api(dso, cnx, GLESv2);
689 hnd->set(dso, GLESv2);
690 }
691 return hnd;
692#else
Peiyong Line83b8682019-04-18 17:23:02 -0700693 return nullptr;
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700694#endif
695}
696
Peiyong Lin0d10b252019-04-30 18:03:04 -0700697Loader::driver_t* Loader::attempt_to_load_system_driver(egl_connection_t* cnx, const char* suffix,
698 const bool exact) {
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700699 ATRACE_CALL();
700 android::GraphicsEnv::getInstance().setDriverToLoad(android::GraphicsEnv::Driver::GL);
701 driver_t* hnd = nullptr;
Peiyong Lin0d10b252019-04-30 18:03:04 -0700702 void* dso = load_system_driver("GLES", suffix, exact);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700703 if (dso) {
704 initialize_api(dso, cnx, EGL | GLESv1_CM | GLESv2);
705 hnd = new driver_t(dso);
706 return hnd;
707 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700708 dso = load_system_driver("EGL", suffix, exact);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700709 if (dso) {
710 initialize_api(dso, cnx, EGL);
711 hnd = new driver_t(dso);
712
Peiyong Lin0d10b252019-04-30 18:03:04 -0700713 dso = load_system_driver("GLESv1_CM", suffix, exact);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700714 initialize_api(dso, cnx, GLESv1_CM);
715 hnd->set(dso, GLESv1_CM);
716
Peiyong Lin0d10b252019-04-30 18:03:04 -0700717 dso = load_system_driver("GLESv2", suffix, exact);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700718 initialize_api(dso, cnx, GLESv2);
719 hnd->set(dso, GLESv2);
720 }
721 return hnd;
Peiyong Line83b8682019-04-18 17:23:02 -0700722}
723
724void Loader::initialize_api(void* dso, egl_connection_t* cnx, uint32_t mask) {
Mathias Agopiande586972009-05-28 17:39:03 -0700725 if (mask & EGL) {
726 getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress");
727
Jesse Hall94cdba92013-07-11 09:40:35 -0700728 ALOGE_IF(!getProcAddress,
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800729 "can't find eglGetProcAddress() in EGL driver library");
Mathias Agopiande586972009-05-28 17:39:03 -0700730
Mathias Agopian618fa102009-10-14 02:06:37 -0700731 egl_t* egl = &cnx->egl;
Mathias Agopiande586972009-05-28 17:39:03 -0700732 __eglMustCastToProperFunctionPointerType* curr =
733 (__eglMustCastToProperFunctionPointerType*)egl;
734 char const * const * api = egl_names;
735 while (*api) {
736 char const * name = *api;
Jesse Hall94cdba92013-07-11 09:40:35 -0700737 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700738 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700739 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700740 // couldn't find the entry-point, use eglGetProcAddress()
741 f = getProcAddress(name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700742 if (f == nullptr) {
743 f = (__eglMustCastToProperFunctionPointerType)nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700744 }
745 }
746 *curr++ = f;
747 api++;
748 }
749 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700750
Mathias Agopiande586972009-05-28 17:39:03 -0700751 if (mask & GLESv1_CM) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700752 init_api(dso, gl_names_1, gl_names,
Mathias Agopian618fa102009-10-14 02:06:37 -0700753 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800754 &cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl,
Mathias Agopian618fa102009-10-14 02:06:37 -0700755 getProcAddress);
Mathias Agopiande586972009-05-28 17:39:03 -0700756 }
757
758 if (mask & GLESv2) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700759 init_api(dso, gl_names, nullptr,
Mathias Agopian618fa102009-10-14 02:06:37 -0700760 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800761 &cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl,
Mathias Agopiande586972009-05-28 17:39:03 -0700762 getProcAddress);
763 }
Mathias Agopiande586972009-05-28 17:39:03 -0700764}
765
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700766} // namespace android