blob: 8a409aeb3660d66e0014c72baa183ca5f283a92b [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>
Mathias Agopian311b4792017-02-28 15:00:49 -080030
Jiyong Parka243e5d2017-06-21 12:26:51 +090031#ifndef __ANDROID_VNDK__
Jiyong Park27c39e12017-05-08 13:00:02 +090032#include <graphicsenv/GraphicsEnv.h>
Jiyong Parka243e5d2017-06-21 12:26:51 +090033#endif
Justin Yunb7320302017-05-22 15:13:40 +090034#include <vndksupport/linker.h>
Mathias Agopiande586972009-05-28 17:39:03 -070035
Cody Northrop68d10352018-10-15 07:22:09 -060036#include "egl_platform_entries.h"
Mathias Agopian65421432017-03-08 11:49:05 -080037#include "egl_trace.h"
Mathias Agopian1cadb252011-05-23 17:26:14 -070038#include "egldefs.h"
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -060039#include <EGL/eglext_angle.h>
Mathias Agopiande586972009-05-28 17:39:03 -070040
41// ----------------------------------------------------------------------------
42namespace android {
43// ----------------------------------------------------------------------------
44
45
46/*
Mathias Agopian99381422013-04-23 20:52:29 +020047 * EGL userspace drivers must be provided either:
48 * - as a single library:
49 * /vendor/lib/egl/libGLES.so
50 *
51 * - as separate libraries:
52 * /vendor/lib/egl/libEGL.so
53 * /vendor/lib/egl/libGLESv1_CM.so
54 * /vendor/lib/egl/libGLESv2.so
55 *
56 * The software renderer for the emulator must be provided as a single
57 * library at:
58 *
59 * /system/lib/egl/libGLES_android.so
60 *
61 *
62 * For backward compatibility and to facilitate the transition to
63 * this new naming scheme, the loader will additionally look for:
Jesse Hall94cdba92013-07-11 09:40:35 -070064 *
Mathias Agopian99381422013-04-23 20:52:29 +020065 * /{vendor|system}/lib/egl/lib{GLES | [EGL|GLESv1_CM|GLESv2]}_*.so
Jesse Hall94cdba92013-07-11 09:40:35 -070066 *
Mathias Agopiande586972009-05-28 17:39:03 -070067 */
68
Mathias Agopian65421432017-03-08 11:49:05 -080069Loader& Loader::getInstance() {
70 static Loader loader;
71 return loader;
72}
Mathias Agopiande586972009-05-28 17:39:03 -070073
David 'Digit' Turner80b30c22011-08-26 17:38:47 +020074/* This function is called to check whether we run inside the emulator,
75 * and if this is the case whether GLES GPU emulation is supported.
76 *
77 * Returned values are:
78 * -1 -> not running inside the emulator
79 * 0 -> running inside the emulator, but GPU emulation not supported
80 * 1 -> running inside the emulator, GPU emulation is supported
Nicolas Capens776951f2015-11-06 10:10:21 -050081 * through the "emulation" host-side OpenGL ES implementation.
82 * 2 -> running inside the emulator, GPU emulation is supported
83 * through a guest-side vendor driver's OpenGL ES implementation.
David 'Digit' Turner80b30c22011-08-26 17:38:47 +020084 */
85static int
86checkGlesEmulationStatus(void)
87{
88 /* We're going to check for the following kernel parameters:
89 *
90 * qemu=1 -> tells us that we run inside the emulator
91 * android.qemu.gles=<number> -> tells us the GLES GPU emulation status
92 *
93 * Note that we will return <number> if we find it. This let us support
94 * more additionnal emulation modes in the future.
95 */
96 char prop[PROPERTY_VALUE_MAX];
97 int result = -1;
98
99 /* First, check for qemu=1 */
100 property_get("ro.kernel.qemu",prop,"0");
101 if (atoi(prop) != 1)
102 return -1;
103
104 /* We are in the emulator, get GPU status value */
bohu69e5b1a2016-02-19 17:15:20 -0800105 property_get("qemu.gles",prop,"0");
David 'Digit' Turner80b30c22011-08-26 17:38:47 +0200106 return atoi(prop);
107}
108
Jesse Hall1508ae62017-01-19 17:43:26 -0800109static void* do_dlopen(const char* path, int mode) {
110 ATRACE_CALL();
111 return dlopen(path, mode);
112}
113
Jiyong Park5910dc72017-04-05 14:23:52 +0900114static void* do_android_dlopen_ext(const char* path, int mode, const android_dlextinfo* info) {
115 ATRACE_CALL();
116 return android_dlopen_ext(path, mode, info);
117}
118
Justin Yunb7320302017-05-22 15:13:40 +0900119static void* do_android_load_sphal_library(const char* path, int mode) {
120 ATRACE_CALL();
121 return android_load_sphal_library(path, mode);
122}
123
Mathias Agopiande586972009-05-28 17:39:03 -0700124// ----------------------------------------------------------------------------
125
Jesse Hall94cdba92013-07-11 09:40:35 -0700126Loader::driver_t::driver_t(void* gles)
Mathias Agopiande586972009-05-28 17:39:03 -0700127{
128 dso[0] = gles;
129 for (size_t i=1 ; i<NELEM(dso) ; i++)
Yi Kong48a6cd22018-07-18 10:07:09 -0700130 dso[i] = nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700131}
132
Jesse Hall94cdba92013-07-11 09:40:35 -0700133Loader::driver_t::~driver_t()
Mathias Agopiande586972009-05-28 17:39:03 -0700134{
135 for (size_t i=0 ; i<NELEM(dso) ; i++) {
136 if (dso[i]) {
137 dlclose(dso[i]);
Yi Kong48a6cd22018-07-18 10:07:09 -0700138 dso[i] = nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700139 }
140 }
141}
142
Mathias Agopian65421432017-03-08 11:49:05 -0800143int Loader::driver_t::set(void* hnd, int32_t api)
Mathias Agopiande586972009-05-28 17:39:03 -0700144{
145 switch (api) {
146 case EGL:
147 dso[0] = hnd;
148 break;
149 case GLESv1_CM:
150 dso[1] = hnd;
151 break;
152 case GLESv2:
153 dso[2] = hnd;
154 break;
155 default:
Mathias Agopian65421432017-03-08 11:49:05 -0800156 return -EOVERFLOW;
Mathias Agopiande586972009-05-28 17:39:03 -0700157 }
Mathias Agopian65421432017-03-08 11:49:05 -0800158 return 0;
Mathias Agopiande586972009-05-28 17:39:03 -0700159}
160
161// ----------------------------------------------------------------------------
162
Mathias Agopiande586972009-05-28 17:39:03 -0700163Loader::Loader()
Yi Kong48a6cd22018-07-18 10:07:09 -0700164 : getProcAddress(nullptr)
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800165{
Mathias Agopiande586972009-05-28 17:39:03 -0700166}
167
Mathias Agopian99381422013-04-23 20:52:29 +0200168Loader::~Loader() {
Mathias Agopiande586972009-05-28 17:39:03 -0700169}
170
Jesse Hallc07b5202013-07-04 12:08:16 -0700171static void* load_wrapper(const char* path) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800172 void* so = do_dlopen(path, RTLD_NOW | RTLD_LOCAL);
Jesse Hallc07b5202013-07-04 12:08:16 -0700173 ALOGE_IF(!so, "dlopen(\"%s\") failed: %s", path, dlerror());
174 return so;
175}
176
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700177#ifndef EGL_WRAPPER_DIR
178#if defined(__LP64__)
179#define EGL_WRAPPER_DIR "/system/lib64"
180#else
181#define EGL_WRAPPER_DIR "/system/lib"
182#endif
183#endif
184
bohu69e5b1a2016-02-19 17:15:20 -0800185static void setEmulatorGlesValue(void) {
186 char prop[PROPERTY_VALUE_MAX];
187 property_get("ro.kernel.qemu", prop, "0");
188 if (atoi(prop) != 1) return;
189
190 property_get("ro.kernel.qemu.gles",prop,"0");
191 if (atoi(prop) == 1) {
192 ALOGD("Emulator has host GPU support, qemu.gles is set to 1.");
193 property_set("qemu.gles", "1");
194 return;
195 }
196
197 // for now, checking the following
198 // directory is good enough for emulator system images
199 const char* vendor_lib_path =
200#if defined(__LP64__)
201 "/vendor/lib64/egl";
202#else
203 "/vendor/lib/egl";
204#endif
205
206 const bool has_vendor_lib = (access(vendor_lib_path, R_OK) == 0);
207 if (has_vendor_lib) {
208 ALOGD("Emulator has vendor provided software renderer, qemu.gles is set to 2.");
209 property_set("qemu.gles", "2");
210 } else {
211 ALOGD("Emulator without GPU support detected. "
212 "Fallback to legacy software renderer, qemu.gles is set to 0.");
213 property_set("qemu.gles", "0");
214 }
215}
216
Mathias Agopianada798b2012-02-13 17:09:30 -0800217void* Loader::open(egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700218{
Jesse Hall1508ae62017-01-19 17:43:26 -0800219 ATRACE_CALL();
220
Mathias Agopiande586972009-05-28 17:39:03 -0700221 void* dso;
Yi Kong48a6cd22018-07-18 10:07:09 -0700222 driver_t* hnd = nullptr;
Jesse Hall94cdba92013-07-11 09:40:35 -0700223
bohu69e5b1a2016-02-19 17:15:20 -0800224 setEmulatorGlesValue();
225
Tim Van Patten5f744f12018-12-12 11:46:21 -0700226 // Check if we should use ANGLE early, so loading each driver doesn't require repeated queries.
227 if (android::GraphicsEnv::getInstance().shouldUseAngle()) {
228 cnx->shouldUseAngle = true;
229 } else {
230 cnx->shouldUseAngle = false;
231 }
232
Cody Northrop9d5d33d2018-10-15 18:32:14 -0600233 dso = load_driver("GLES", cnx, EGL | GLESv1_CM | GLESv2);
Mathias Agopian99381422013-04-23 20:52:29 +0200234 if (dso) {
235 hnd = new driver_t(dso);
236 } else {
237 // Always load EGL first
Cody Northrop9d5d33d2018-10-15 18:32:14 -0600238 dso = load_driver("EGL", cnx, EGL);
Mathias Agopiande586972009-05-28 17:39:03 -0700239 if (dso) {
240 hnd = new driver_t(dso);
Mathias Agopian99381422013-04-23 20:52:29 +0200241 hnd->set( load_driver("GLESv1_CM", cnx, GLESv1_CM), GLESv1_CM );
242 hnd->set( load_driver("GLESv2", cnx, GLESv2), GLESv2 );
Mathias Agopiande586972009-05-28 17:39:03 -0700243 }
244 }
245
Mathias Agopian99381422013-04-23 20:52:29 +0200246 LOG_ALWAYS_FATAL_IF(!hnd, "couldn't find an OpenGL ES implementation");
Jesse Hallc07b5202013-07-04 12:08:16 -0700247
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700248 cnx->libEgl = load_wrapper(EGL_WRAPPER_DIR "/libEGL.so");
249 cnx->libGles2 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv2.so");
250 cnx->libGles1 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv1_CM.so");
251
Michael Chockc0ec5e22014-01-27 08:14:33 -0800252 LOG_ALWAYS_FATAL_IF(!cnx->libEgl,
253 "couldn't load system EGL wrapper libraries");
254
Jesse Hallc07b5202013-07-04 12:08:16 -0700255 LOG_ALWAYS_FATAL_IF(!cnx->libGles2 || !cnx->libGles1,
256 "couldn't load system OpenGL ES wrapper libraries");
257
Mathias Agopiande586972009-05-28 17:39:03 -0700258 return (void*)hnd;
259}
260
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600261void Loader::close(egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700262{
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600263 driver_t* hnd = (driver_t*) cnx->dso;
Mathias Agopiande586972009-05-28 17:39:03 -0700264 delete hnd;
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600265 cnx->dso = nullptr;
266
Tim Van Patten5f744f12018-12-12 11:46:21 -0700267 cnx->shouldUseAngle = false;
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600268 cnx->angleDecided = false;
269 cnx->useAngle = false;
270
271 if (cnx->vendorEGL) {
272 dlclose(cnx->vendorEGL);
273 cnx->vendorEGL = nullptr;
274 }
Mathias Agopiande586972009-05-28 17:39:03 -0700275}
276
Jesse Hall94cdba92013-07-11 09:40:35 -0700277void Loader::init_api(void* dso,
278 char const * const * api,
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700279 char const * const * ref_api,
Jesse Hall94cdba92013-07-11 09:40:35 -0700280 __eglMustCastToProperFunctionPointerType* curr,
281 getProcAddressType getProcAddress)
Mathias Agopiande586972009-05-28 17:39:03 -0700282{
Jesse Hall1508ae62017-01-19 17:43:26 -0800283 ATRACE_CALL();
284
Mathias Agopian7773c432012-02-13 20:06:08 -0800285 const ssize_t SIZE = 256;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700286 char scrap[SIZE];
Mathias Agopiande586972009-05-28 17:39:03 -0700287 while (*api) {
288 char const * name = *api;
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700289 if (ref_api) {
290 char const * ref_name = *ref_api;
291 if (std::strcmp(name, ref_name) != 0) {
292 *curr++ = nullptr;
293 ref_api++;
294 continue;
295 }
296 }
297
Jesse Hall94cdba92013-07-11 09:40:35 -0700298 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700299 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700300 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700301 // couldn't find the entry-point, use eglGetProcAddress()
302 f = getProcAddress(name);
303 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700304 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700305 // Try without the OES postfix
306 ssize_t index = ssize_t(strlen(name)) - 3;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700307 if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) {
Mathias Agopiande586972009-05-28 17:39:03 -0700308 strncpy(scrap, name, index);
309 scrap[index] = 0;
310 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000311 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700312 }
313 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700314 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700315 // Try with the OES postfix
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700316 ssize_t index = ssize_t(strlen(name)) - 3;
317 if (index>0 && strcmp(name+index, "OES")) {
318 snprintf(scrap, SIZE, "%sOES", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700319 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000320 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700321 }
322 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700323 if (f == nullptr) {
Steve Block9d453682011-12-20 16:23:08 +0000324 //ALOGD("%s", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700325 f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented;
Mathias Agopian48d438d2012-01-28 21:44:00 -0800326
327 /*
328 * GL_EXT_debug_label is special, we always report it as
329 * supported, it's handled by GLES_trace. If GLES_trace is not
330 * enabled, then these are no-ops.
331 */
332 if (!strcmp(name, "glInsertEventMarkerEXT")) {
333 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
334 } else if (!strcmp(name, "glPushGroupMarkerEXT")) {
335 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
336 } else if (!strcmp(name, "glPopGroupMarkerEXT")) {
337 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
338 }
Mathias Agopiande586972009-05-28 17:39:03 -0700339 }
340 *curr++ = f;
341 api++;
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700342 if (ref_api) ref_api++;
Mathias Agopiande586972009-05-28 17:39:03 -0700343 }
344}
345
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800346static void* load_system_driver(const char* kind) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800347 ATRACE_CALL();
Mathias Agopian99381422013-04-23 20:52:29 +0200348 class MatchFile {
349 public:
Mathias Agopian65421432017-03-08 11:49:05 -0800350 static std::string find(const char* kind) {
351 std::string result;
Nicolas Capens776951f2015-11-06 10:10:21 -0500352 int emulationStatus = checkGlesEmulationStatus();
353 switch (emulationStatus) {
354 case 0:
Nicolas Capens776951f2015-11-06 10:10:21 -0500355#if defined(__LP64__)
bohu3adf9e12017-07-02 22:08:13 -0700356 result = "/vendor/lib64/egl/libGLES_android.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500357#else
bohu3adf9e12017-07-02 22:08:13 -0700358 result = "/vendor/lib/egl/libGLES_android.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500359#endif
360 return result;
361 case 1:
362 // Use host-side OpenGL through the "emulation" library
363#if defined(__LP64__)
bohu3adf9e12017-07-02 22:08:13 -0700364 result = std::string("/vendor/lib64/egl/lib") + kind + "_emulation.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500365#else
bohu3adf9e12017-07-02 22:08:13 -0700366 result = std::string("/vendor/lib/egl/lib") + kind + "_emulation.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500367#endif
368 return result;
Lingfeng Yang0f82d872017-08-11 01:18:00 -0700369 case 2:
370 // Use guest side swiftshader library
371#if defined(__LP64__)
372 result = std::string("/vendor/lib64/egl/lib") + kind + "_swiftshader.so";
373#else
374 result = std::string("/vendor/lib/egl/lib") + kind + "_swiftshader.so";
375#endif
376 return result;
Nicolas Capens776951f2015-11-06 10:10:21 -0500377 default:
378 // Not in emulator, or use other guest-side implementation
379 break;
380 }
381
Mathias Agopian65421432017-03-08 11:49:05 -0800382 std::string pattern = std::string("lib") + kind;
Mathias Agopian99381422013-04-23 20:52:29 +0200383 const char* const searchPaths[] = {
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800384#if defined(__LP64__)
385 "/vendor/lib64/egl",
386 "/system/lib64/egl"
387#else
Mathias Agopian99381422013-04-23 20:52:29 +0200388 "/vendor/lib/egl",
389 "/system/lib/egl"
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800390#endif
Mathias Agopian99381422013-04-23 20:52:29 +0200391 };
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700392
Mathias Agopian99381422013-04-23 20:52:29 +0200393 // first, we search for the exact name of the GLES userspace
394 // driver in both locations.
395 // i.e.:
396 // libGLES.so, or:
397 // libEGL.so, libGLESv1_CM.so, libGLESv2.so
398
399 for (size_t i=0 ; i<NELEM(searchPaths) ; i++) {
400 if (find(result, pattern, searchPaths[i], true)) {
401 return result;
402 }
403 }
404
405 // for compatibility with the old "egl.cfg" naming convention
406 // we look for files that match:
407 // libGLES_*.so, or:
408 // libEGL_*.so, libGLESv1_CM_*.so, libGLESv2_*.so
409
410 pattern.append("_");
411 for (size_t i=0 ; i<NELEM(searchPaths) ; i++) {
412 if (find(result, pattern, searchPaths[i], false)) {
413 return result;
414 }
415 }
416
417 // we didn't find the driver. gah.
418 result.clear();
419 return result;
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700420 }
Mathias Agopian99381422013-04-23 20:52:29 +0200421
422 private:
Mathias Agopian65421432017-03-08 11:49:05 -0800423 static bool find(std::string& result,
424 const std::string& pattern, const char* const search, bool exact) {
Mathias Agopian99381422013-04-23 20:52:29 +0200425 if (exact) {
Jesse Hall4aaa9602017-08-29 16:33:54 -0700426 std::string absolutePath = std::string(search) + "/" + pattern + ".so";
Mathias Agopian65421432017-03-08 11:49:05 -0800427 if (!access(absolutePath.c_str(), R_OK)) {
Mathias Agopian99381422013-04-23 20:52:29 +0200428 result = absolutePath;
429 return true;
430 }
431 return false;
432 }
433
434 DIR* d = opendir(search);
Yi Kong48a6cd22018-07-18 10:07:09 -0700435 if (d != nullptr) {
Mathias Agopian99381422013-04-23 20:52:29 +0200436 struct dirent* e;
Yi Kong48a6cd22018-07-18 10:07:09 -0700437 while ((e = readdir(d)) != nullptr) {
Mathias Agopian99381422013-04-23 20:52:29 +0200438 if (e->d_type == DT_DIR) {
439 continue;
440 }
441 if (!strcmp(e->d_name, "libGLES_android.so")) {
442 // always skip the software renderer
443 continue;
444 }
Mathias Agopian65421432017-03-08 11:49:05 -0800445 if (strstr(e->d_name, pattern.c_str()) == e->d_name) {
Mathias Agopian99381422013-04-23 20:52:29 +0200446 if (!strcmp(e->d_name + strlen(e->d_name) - 3, ".so")) {
Mathias Agopian65421432017-03-08 11:49:05 -0800447 result = std::string(search) + "/" + e->d_name;
Mathias Agopian99381422013-04-23 20:52:29 +0200448 closedir(d);
449 return true;
450 }
451 }
452 }
453 closedir(d);
454 }
455 return false;
456 }
457 };
458
459
Mathias Agopian65421432017-03-08 11:49:05 -0800460 std::string absolutePath = MatchFile::find(kind);
461 if (absolutePath.empty()) {
Mathias Agopian99381422013-04-23 20:52:29 +0200462 // this happens often, we don't want to log an error
Yi Kong48a6cd22018-07-18 10:07:09 -0700463 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700464 }
Mathias Agopian65421432017-03-08 11:49:05 -0800465 const char* const driver_absolute_path = absolutePath.c_str();
Mathias Agopiande586972009-05-28 17:39:03 -0700466
Jiyong Park5910dc72017-04-05 14:23:52 +0900467 // Try to load drivers from the 'sphal' namespace, if it exist. Fall back to
Justin Yunb7320302017-05-22 15:13:40 +0900468 // the original routine when the namespace does not exist.
Jiyong Park5910dc72017-04-05 14:23:52 +0900469 // See /system/core/rootdir/etc/ld.config.txt for the configuration of the
470 // sphal namespace.
Justin Yunb7320302017-05-22 15:13:40 +0900471 void* dso = do_android_load_sphal_library(driver_absolute_path,
472 RTLD_NOW | RTLD_LOCAL);
Yi Kong48a6cd22018-07-18 10:07:09 -0700473 if (dso == nullptr) {
Mathias Agopian8c173842009-09-20 16:01:02 -0700474 const char* err = dlerror();
Mathias Agopian65421432017-03-08 11:49:05 -0800475 ALOGE("load_driver(%s): %s", driver_absolute_path, err ? err : "unknown");
Yi Kong48a6cd22018-07-18 10:07:09 -0700476 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700477 }
478
Steve Block9d453682011-12-20 16:23:08 +0000479 ALOGD("loaded %s", driver_absolute_path);
Mathias Agopianbaca89c2009-08-20 19:09:34 -0700480
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800481 return dso;
482}
483
Cody Northrop1f00e172018-04-02 11:23:31 -0600484static void* load_angle_from_namespace(const char* kind, android_namespace_t* ns) {
485 const android_dlextinfo dlextinfo = {
486 .flags = ANDROID_DLEXT_USE_NAMESPACE,
487 .library_namespace = ns,
488 };
489
490 std::string name = std::string("lib") + kind + "_angle.so";
491
492 void* so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
493
494 if (so) {
495 ALOGD("dlopen_ext from APK (%s) success at %p", name.c_str(), so);
496 return so;
497 } else {
498 ALOGE("dlopen_ext(\"%s\") failed: %s", name.c_str(), dlerror());
499 }
500
501 return nullptr;
502}
503
Cody Northrop6d082ef2018-09-11 09:42:31 -0600504static void* load_angle(const char* kind, android_namespace_t* ns, egl_connection_t* cnx) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600505 // Only attempt to load ANGLE libs
Tim Van Patten5f744f12018-12-12 11:46:21 -0700506 if (strcmp(kind, "EGL") != 0 && strcmp(kind, "GLESv2") != 0 && strcmp(kind, "GLESv1_CM") != 0) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600507 return nullptr;
Cody Northrop1f00e172018-04-02 11:23:31 -0600508 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700509
Courtney Goeltzenleuchter985bc282018-11-02 14:24:55 -0600510 void* so = nullptr;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700511
512 if ((cnx->shouldUseAngle) || android::GraphicsEnv::getInstance().shouldUseAngle()) {
Ian Elliott34474472018-09-05 10:57:07 -0600513 so = load_angle_from_namespace(kind, ns);
Tim Van Patten5f744f12018-12-12 11:46:21 -0700514 cnx->shouldUseAngle = true;
515 } else {
516 cnx->shouldUseAngle = false;
Ian Elliott34474472018-09-05 10:57:07 -0600517 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600518
519 if (so) {
Tim Van Patten5f744f12018-12-12 11:46:21 -0700520 ALOGV("Loaded ANGLE %s library for '%s' (instead of native)", kind,
521 android::GraphicsEnv::getInstance().getAngleAppName().c_str());
Cody Northrop1f00e172018-04-02 11:23:31 -0600522 cnx->useAngle = true;
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600523
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600524 char prop[PROPERTY_VALUE_MAX];
Tim Van Patten5f744f12018-12-12 11:46:21 -0700525
526 property_get("debug.hwui.renderer", prop, "UNSET");
527 ALOGV("Skia's renderer set to %s", prop);
528
529 EGLint angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE;
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600530 property_get("debug.angle.backend", prop, "0");
531 switch (atoi(prop)) {
532 case 1:
533 ALOGV("%s: Requesting OpenGLES back-end", __FUNCTION__);
534 angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE;
535 break;
536 case 2:
537 ALOGV("%s: Requesting Vulkan back-end", __FUNCTION__);
538 angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE;
539 break;
540 default:
541 break;
542 }
543
544 cnx->angleBackend = angleBackendDefault;
545 if (!cnx->vendorEGL && (cnx->angleBackend == EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE)) {
546 // Find and load vendor libEGL for ANGLE's GL back-end to use.
Cody Northrop1f00e172018-04-02 11:23:31 -0600547 cnx->vendorEGL = load_system_driver("EGL");
548 }
Ian Elliottb058aff2018-08-09 12:00:55 -0600549 } else {
Tim Van Patten5f744f12018-12-12 11:46:21 -0700550 ALOGV("Loaded native %s library for '%s' (instead of ANGLE)", kind,
551 android::GraphicsEnv::getInstance().getAngleAppName().c_str());
552 cnx->useAngle = false;
Cody Northrop1f00e172018-04-02 11:23:31 -0600553 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700554 cnx->angleDecided = true;
Cody Northrop1f00e172018-04-02 11:23:31 -0600555
Tim Van Patten5f744f12018-12-12 11:46:21 -0700556 return so;
Cody Northrop1f00e172018-04-02 11:23:31 -0600557}
558
Mathias Agopian311b4792017-02-28 15:00:49 -0800559static const char* HAL_SUBNAME_KEY_PROPERTIES[2] = {
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800560 "ro.hardware.egl",
561 "ro.board.platform",
Mathias Agopian311b4792017-02-28 15:00:49 -0800562};
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800563
564static void* load_updated_driver(const char* kind, android_namespace_t* ns) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800565 ATRACE_CALL();
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800566 const android_dlextinfo dlextinfo = {
567 .flags = ANDROID_DLEXT_USE_NAMESPACE,
568 .library_namespace = ns,
569 };
570 void* so = nullptr;
571 char prop[PROPERTY_VALUE_MAX + 1];
572 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
573 if (property_get(key, prop, nullptr) > 0) {
Mathias Agopian65421432017-03-08 11:49:05 -0800574 std::string name = std::string("lib") + kind + "_" + prop + ".so";
575 so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
Mathias Agopian311b4792017-02-28 15:00:49 -0800576 if (so) {
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800577 return so;
Mathias Agopian311b4792017-02-28 15:00:49 -0800578 }
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800579 }
580 }
581 return nullptr;
582}
583
584void *Loader::load_driver(const char* kind,
585 egl_connection_t* cnx, uint32_t mask)
586{
Jesse Hall1508ae62017-01-19 17:43:26 -0800587 ATRACE_CALL();
588
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800589 void* dso = nullptr;
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600590 android_namespace_t* ns = android::GraphicsEnv::getInstance().getAngleNamespace();
Cody Northrop6d082ef2018-09-11 09:42:31 -0600591 if (ns) {
592 dso = load_angle(kind, ns, cnx);
593 }
Jiyong Parka243e5d2017-06-21 12:26:51 +0900594#ifndef __ANDROID_VNDK__
Cody Northrop1f00e172018-04-02 11:23:31 -0600595 if (!dso) {
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600596 android_namespace_t* ns = android::GraphicsEnv::getInstance().getDriverNamespace();
Cody Northrop1f00e172018-04-02 11:23:31 -0600597 if (ns) {
598 dso = load_updated_driver(kind, ns);
599 }
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800600 }
Jiyong Parka243e5d2017-06-21 12:26:51 +0900601#endif
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800602 if (!dso) {
603 dso = load_system_driver(kind);
604 if (!dso)
Yi Kong48a6cd22018-07-18 10:07:09 -0700605 return nullptr;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800606 }
607
Mathias Agopiande586972009-05-28 17:39:03 -0700608 if (mask & EGL) {
609 getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress");
610
Jesse Hall94cdba92013-07-11 09:40:35 -0700611 ALOGE_IF(!getProcAddress,
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800612 "can't find eglGetProcAddress() in EGL driver library");
Mathias Agopiande586972009-05-28 17:39:03 -0700613
Mathias Agopian618fa102009-10-14 02:06:37 -0700614 egl_t* egl = &cnx->egl;
Mathias Agopiande586972009-05-28 17:39:03 -0700615 __eglMustCastToProperFunctionPointerType* curr =
616 (__eglMustCastToProperFunctionPointerType*)egl;
617 char const * const * api = egl_names;
618 while (*api) {
619 char const * name = *api;
Jesse Hall94cdba92013-07-11 09:40:35 -0700620 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700621 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700622 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700623 // couldn't find the entry-point, use eglGetProcAddress()
624 f = getProcAddress(name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700625 if (f == nullptr) {
626 f = (__eglMustCastToProperFunctionPointerType)nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700627 }
628 }
629 *curr++ = f;
630 api++;
631 }
632 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700633
Mathias Agopiande586972009-05-28 17:39:03 -0700634 if (mask & GLESv1_CM) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700635 init_api(dso, gl_names_1, gl_names,
Mathias Agopian618fa102009-10-14 02:06:37 -0700636 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800637 &cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl,
Mathias Agopian618fa102009-10-14 02:06:37 -0700638 getProcAddress);
Mathias Agopiande586972009-05-28 17:39:03 -0700639 }
640
641 if (mask & GLESv2) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700642 init_api(dso, gl_names, nullptr,
Mathias Agopian618fa102009-10-14 02:06:37 -0700643 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800644 &cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl,
Mathias Agopiande586972009-05-28 17:39:03 -0700645 getProcAddress);
646 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700647
Mathias Agopiande586972009-05-28 17:39:03 -0700648 return dso;
649}
650
651// ----------------------------------------------------------------------------
652}; // namespace android
653// ----------------------------------------------------------------------------