blob: 0c43b83fa6fd675dabac5012989f2a04468218a3 [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
Mathias Agopian311b4792017-02-28 15:00:49 -080020#include "Loader.h"
21
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
Jiyong Park5910dc72017-04-05 14:23:52 +090041extern "C" {
42 android_namespace_t* android_get_exported_namespace(const char*);
Ian Elliott34474472018-09-05 10:57:07 -060043
44 // TODO(ianelliott@): Get this from an ANGLE header:
Ian Elliott380bf852018-10-17 14:56:04 -060045 typedef bool (*fpANGLEGetUtilityAPI)(unsigned int* versionToUse);
46
47 // TODO(ianelliott@): Get this from an ANGLE header:
48 typedef bool (*fpAndroidUseANGLEForApplication)(int fd, long offset, long length,
49 const char* appName, const char* deviceMfr,
50 const char* deviceModel);
Jiyong Park5910dc72017-04-05 14:23:52 +090051}
52
Mathias Agopiande586972009-05-28 17:39:03 -070053// ----------------------------------------------------------------------------
54namespace android {
55// ----------------------------------------------------------------------------
56
57
58/*
Mathias Agopian99381422013-04-23 20:52:29 +020059 * EGL userspace drivers must be provided either:
60 * - as a single library:
61 * /vendor/lib/egl/libGLES.so
62 *
63 * - as separate libraries:
64 * /vendor/lib/egl/libEGL.so
65 * /vendor/lib/egl/libGLESv1_CM.so
66 * /vendor/lib/egl/libGLESv2.so
67 *
68 * The software renderer for the emulator must be provided as a single
69 * library at:
70 *
71 * /system/lib/egl/libGLES_android.so
72 *
73 *
74 * For backward compatibility and to facilitate the transition to
75 * this new naming scheme, the loader will additionally look for:
Jesse Hall94cdba92013-07-11 09:40:35 -070076 *
Mathias Agopian99381422013-04-23 20:52:29 +020077 * /{vendor|system}/lib/egl/lib{GLES | [EGL|GLESv1_CM|GLESv2]}_*.so
Jesse Hall94cdba92013-07-11 09:40:35 -070078 *
Mathias Agopiande586972009-05-28 17:39:03 -070079 */
80
Mathias Agopian65421432017-03-08 11:49:05 -080081Loader& Loader::getInstance() {
82 static Loader loader;
83 return loader;
84}
Mathias Agopiande586972009-05-28 17:39:03 -070085
David 'Digit' Turner80b30c22011-08-26 17:38:47 +020086/* This function is called to check whether we run inside the emulator,
87 * and if this is the case whether GLES GPU emulation is supported.
88 *
89 * Returned values are:
90 * -1 -> not running inside the emulator
91 * 0 -> running inside the emulator, but GPU emulation not supported
92 * 1 -> running inside the emulator, GPU emulation is supported
Nicolas Capens776951f2015-11-06 10:10:21 -050093 * through the "emulation" host-side OpenGL ES implementation.
94 * 2 -> running inside the emulator, GPU emulation is supported
95 * through a guest-side vendor driver's OpenGL ES implementation.
David 'Digit' Turner80b30c22011-08-26 17:38:47 +020096 */
97static int
98checkGlesEmulationStatus(void)
99{
100 /* We're going to check for the following kernel parameters:
101 *
102 * qemu=1 -> tells us that we run inside the emulator
103 * android.qemu.gles=<number> -> tells us the GLES GPU emulation status
104 *
105 * Note that we will return <number> if we find it. This let us support
106 * more additionnal emulation modes in the future.
107 */
108 char prop[PROPERTY_VALUE_MAX];
109 int result = -1;
110
111 /* First, check for qemu=1 */
112 property_get("ro.kernel.qemu",prop,"0");
113 if (atoi(prop) != 1)
114 return -1;
115
116 /* We are in the emulator, get GPU status value */
bohu69e5b1a2016-02-19 17:15:20 -0800117 property_get("qemu.gles",prop,"0");
David 'Digit' Turner80b30c22011-08-26 17:38:47 +0200118 return atoi(prop);
119}
120
Jesse Hall1508ae62017-01-19 17:43:26 -0800121static void* do_dlopen(const char* path, int mode) {
122 ATRACE_CALL();
123 return dlopen(path, mode);
124}
125
Jiyong Park5910dc72017-04-05 14:23:52 +0900126static void* do_android_dlopen_ext(const char* path, int mode, const android_dlextinfo* info) {
127 ATRACE_CALL();
128 return android_dlopen_ext(path, mode, info);
129}
130
Justin Yunb7320302017-05-22 15:13:40 +0900131static void* do_android_load_sphal_library(const char* path, int mode) {
132 ATRACE_CALL();
133 return android_load_sphal_library(path, mode);
134}
135
Mathias Agopiande586972009-05-28 17:39:03 -0700136// ----------------------------------------------------------------------------
137
Jesse Hall94cdba92013-07-11 09:40:35 -0700138Loader::driver_t::driver_t(void* gles)
Mathias Agopiande586972009-05-28 17:39:03 -0700139{
140 dso[0] = gles;
141 for (size_t i=1 ; i<NELEM(dso) ; i++)
Yi Kong48a6cd22018-07-18 10:07:09 -0700142 dso[i] = nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700143}
144
Jesse Hall94cdba92013-07-11 09:40:35 -0700145Loader::driver_t::~driver_t()
Mathias Agopiande586972009-05-28 17:39:03 -0700146{
147 for (size_t i=0 ; i<NELEM(dso) ; i++) {
148 if (dso[i]) {
149 dlclose(dso[i]);
Yi Kong48a6cd22018-07-18 10:07:09 -0700150 dso[i] = nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700151 }
152 }
153}
154
Mathias Agopian65421432017-03-08 11:49:05 -0800155int Loader::driver_t::set(void* hnd, int32_t api)
Mathias Agopiande586972009-05-28 17:39:03 -0700156{
157 switch (api) {
158 case EGL:
159 dso[0] = hnd;
160 break;
161 case GLESv1_CM:
162 dso[1] = hnd;
163 break;
164 case GLESv2:
165 dso[2] = hnd;
166 break;
167 default:
Mathias Agopian65421432017-03-08 11:49:05 -0800168 return -EOVERFLOW;
Mathias Agopiande586972009-05-28 17:39:03 -0700169 }
Mathias Agopian65421432017-03-08 11:49:05 -0800170 return 0;
Mathias Agopiande586972009-05-28 17:39:03 -0700171}
172
173// ----------------------------------------------------------------------------
174
Mathias Agopiande586972009-05-28 17:39:03 -0700175Loader::Loader()
Yi Kong48a6cd22018-07-18 10:07:09 -0700176 : getProcAddress(nullptr)
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800177{
Mathias Agopiande586972009-05-28 17:39:03 -0700178}
179
Mathias Agopian99381422013-04-23 20:52:29 +0200180Loader::~Loader() {
Mathias Agopiande586972009-05-28 17:39:03 -0700181}
182
Jesse Hallc07b5202013-07-04 12:08:16 -0700183static void* load_wrapper(const char* path) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800184 void* so = do_dlopen(path, RTLD_NOW | RTLD_LOCAL);
Jesse Hallc07b5202013-07-04 12:08:16 -0700185 ALOGE_IF(!so, "dlopen(\"%s\") failed: %s", path, dlerror());
186 return so;
187}
188
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700189#ifndef EGL_WRAPPER_DIR
190#if defined(__LP64__)
191#define EGL_WRAPPER_DIR "/system/lib64"
192#else
193#define EGL_WRAPPER_DIR "/system/lib"
194#endif
195#endif
196
bohu69e5b1a2016-02-19 17:15:20 -0800197static void setEmulatorGlesValue(void) {
198 char prop[PROPERTY_VALUE_MAX];
199 property_get("ro.kernel.qemu", prop, "0");
200 if (atoi(prop) != 1) return;
201
202 property_get("ro.kernel.qemu.gles",prop,"0");
203 if (atoi(prop) == 1) {
204 ALOGD("Emulator has host GPU support, qemu.gles is set to 1.");
205 property_set("qemu.gles", "1");
206 return;
207 }
208
209 // for now, checking the following
210 // directory is good enough for emulator system images
211 const char* vendor_lib_path =
212#if defined(__LP64__)
213 "/vendor/lib64/egl";
214#else
215 "/vendor/lib/egl";
216#endif
217
218 const bool has_vendor_lib = (access(vendor_lib_path, R_OK) == 0);
219 if (has_vendor_lib) {
220 ALOGD("Emulator has vendor provided software renderer, qemu.gles is set to 2.");
221 property_set("qemu.gles", "2");
222 } else {
223 ALOGD("Emulator without GPU support detected. "
224 "Fallback to legacy software renderer, qemu.gles is set to 0.");
225 property_set("qemu.gles", "0");
226 }
227}
228
Mathias Agopianada798b2012-02-13 17:09:30 -0800229void* Loader::open(egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700230{
Jesse Hall1508ae62017-01-19 17:43:26 -0800231 ATRACE_CALL();
232
Mathias Agopiande586972009-05-28 17:39:03 -0700233 void* dso;
Yi Kong48a6cd22018-07-18 10:07:09 -0700234 driver_t* hnd = nullptr;
Jesse Hall94cdba92013-07-11 09:40:35 -0700235
bohu69e5b1a2016-02-19 17:15:20 -0800236 setEmulatorGlesValue();
237
Cody Northrop9d5d33d2018-10-15 18:32:14 -0600238 dso = load_driver("GLES", cnx, EGL | GLESv1_CM | GLESv2);
Mathias Agopian99381422013-04-23 20:52:29 +0200239 if (dso) {
240 hnd = new driver_t(dso);
241 } else {
242 // Always load EGL first
Cody Northrop9d5d33d2018-10-15 18:32:14 -0600243 dso = load_driver("EGL", cnx, EGL);
Mathias Agopiande586972009-05-28 17:39:03 -0700244 if (dso) {
245 hnd = new driver_t(dso);
Mathias Agopian99381422013-04-23 20:52:29 +0200246 hnd->set( load_driver("GLESv1_CM", cnx, GLESv1_CM), GLESv1_CM );
247 hnd->set( load_driver("GLESv2", cnx, GLESv2), GLESv2 );
Mathias Agopiande586972009-05-28 17:39:03 -0700248 }
249 }
250
Mathias Agopian99381422013-04-23 20:52:29 +0200251 LOG_ALWAYS_FATAL_IF(!hnd, "couldn't find an OpenGL ES implementation");
Jesse Hallc07b5202013-07-04 12:08:16 -0700252
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700253 cnx->libEgl = load_wrapper(EGL_WRAPPER_DIR "/libEGL.so");
254 cnx->libGles2 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv2.so");
255 cnx->libGles1 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv1_CM.so");
256
Michael Chockc0ec5e22014-01-27 08:14:33 -0800257 LOG_ALWAYS_FATAL_IF(!cnx->libEgl,
258 "couldn't load system EGL wrapper libraries");
259
Jesse Hallc07b5202013-07-04 12:08:16 -0700260 LOG_ALWAYS_FATAL_IF(!cnx->libGles2 || !cnx->libGles1,
261 "couldn't load system OpenGL ES wrapper libraries");
262
Mathias Agopiande586972009-05-28 17:39:03 -0700263 return (void*)hnd;
264}
265
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600266void Loader::close(egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700267{
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600268 driver_t* hnd = (driver_t*) cnx->dso;
Mathias Agopiande586972009-05-28 17:39:03 -0700269 delete hnd;
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600270 cnx->dso = nullptr;
271
272 if (cnx->featureSo) {
273 dlclose(cnx->featureSo);
274 cnx->featureSo = nullptr;
275 }
276
277 cnx->angleDecided = false;
278 cnx->useAngle = false;
279
280 if (cnx->vendorEGL) {
281 dlclose(cnx->vendorEGL);
282 cnx->vendorEGL = nullptr;
283 }
Mathias Agopiande586972009-05-28 17:39:03 -0700284}
285
Jesse Hall94cdba92013-07-11 09:40:35 -0700286void Loader::init_api(void* dso,
287 char const * const * api,
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700288 char const * const * ref_api,
Jesse Hall94cdba92013-07-11 09:40:35 -0700289 __eglMustCastToProperFunctionPointerType* curr,
290 getProcAddressType getProcAddress)
Mathias Agopiande586972009-05-28 17:39:03 -0700291{
Jesse Hall1508ae62017-01-19 17:43:26 -0800292 ATRACE_CALL();
293
Mathias Agopian7773c432012-02-13 20:06:08 -0800294 const ssize_t SIZE = 256;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700295 char scrap[SIZE];
Mathias Agopiande586972009-05-28 17:39:03 -0700296 while (*api) {
297 char const * name = *api;
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700298 if (ref_api) {
299 char const * ref_name = *ref_api;
300 if (std::strcmp(name, ref_name) != 0) {
301 *curr++ = nullptr;
302 ref_api++;
303 continue;
304 }
305 }
306
Jesse Hall94cdba92013-07-11 09:40:35 -0700307 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700308 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700309 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700310 // couldn't find the entry-point, use eglGetProcAddress()
311 f = getProcAddress(name);
312 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700313 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700314 // Try without the OES postfix
315 ssize_t index = ssize_t(strlen(name)) - 3;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700316 if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) {
Mathias Agopiande586972009-05-28 17:39:03 -0700317 strncpy(scrap, name, index);
318 scrap[index] = 0;
319 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) {
Mathias Agopiande586972009-05-28 17:39:03 -0700324 // Try with the OES postfix
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700325 ssize_t index = ssize_t(strlen(name)) - 3;
326 if (index>0 && strcmp(name+index, "OES")) {
327 snprintf(scrap, SIZE, "%sOES", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700328 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000329 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700330 }
331 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700332 if (f == nullptr) {
Steve Block9d453682011-12-20 16:23:08 +0000333 //ALOGD("%s", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700334 f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented;
Mathias Agopian48d438d2012-01-28 21:44:00 -0800335
336 /*
337 * GL_EXT_debug_label is special, we always report it as
338 * supported, it's handled by GLES_trace. If GLES_trace is not
339 * enabled, then these are no-ops.
340 */
341 if (!strcmp(name, "glInsertEventMarkerEXT")) {
342 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
343 } else if (!strcmp(name, "glPushGroupMarkerEXT")) {
344 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
345 } else if (!strcmp(name, "glPopGroupMarkerEXT")) {
346 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
347 }
Mathias Agopiande586972009-05-28 17:39:03 -0700348 }
349 *curr++ = f;
350 api++;
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700351 if (ref_api) ref_api++;
Mathias Agopiande586972009-05-28 17:39:03 -0700352 }
353}
354
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800355static void* load_system_driver(const char* kind) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800356 ATRACE_CALL();
Mathias Agopian99381422013-04-23 20:52:29 +0200357 class MatchFile {
358 public:
Mathias Agopian65421432017-03-08 11:49:05 -0800359 static std::string find(const char* kind) {
360 std::string result;
Nicolas Capens776951f2015-11-06 10:10:21 -0500361 int emulationStatus = checkGlesEmulationStatus();
362 switch (emulationStatus) {
363 case 0:
Nicolas Capens776951f2015-11-06 10:10:21 -0500364#if defined(__LP64__)
bohu3adf9e12017-07-02 22:08:13 -0700365 result = "/vendor/lib64/egl/libGLES_android.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500366#else
bohu3adf9e12017-07-02 22:08:13 -0700367 result = "/vendor/lib/egl/libGLES_android.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500368#endif
369 return result;
370 case 1:
371 // Use host-side OpenGL through the "emulation" library
372#if defined(__LP64__)
bohu3adf9e12017-07-02 22:08:13 -0700373 result = std::string("/vendor/lib64/egl/lib") + kind + "_emulation.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500374#else
bohu3adf9e12017-07-02 22:08:13 -0700375 result = std::string("/vendor/lib/egl/lib") + kind + "_emulation.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500376#endif
377 return result;
Lingfeng Yang0f82d872017-08-11 01:18:00 -0700378 case 2:
379 // Use guest side swiftshader library
380#if defined(__LP64__)
381 result = std::string("/vendor/lib64/egl/lib") + kind + "_swiftshader.so";
382#else
383 result = std::string("/vendor/lib/egl/lib") + kind + "_swiftshader.so";
384#endif
385 return result;
Nicolas Capens776951f2015-11-06 10:10:21 -0500386 default:
387 // Not in emulator, or use other guest-side implementation
388 break;
389 }
390
Mathias Agopian65421432017-03-08 11:49:05 -0800391 std::string pattern = std::string("lib") + kind;
Mathias Agopian99381422013-04-23 20:52:29 +0200392 const char* const searchPaths[] = {
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800393#if defined(__LP64__)
394 "/vendor/lib64/egl",
395 "/system/lib64/egl"
396#else
Mathias Agopian99381422013-04-23 20:52:29 +0200397 "/vendor/lib/egl",
398 "/system/lib/egl"
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800399#endif
Mathias Agopian99381422013-04-23 20:52:29 +0200400 };
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700401
Mathias Agopian99381422013-04-23 20:52:29 +0200402 // first, we search for the exact name of the GLES userspace
403 // driver in both locations.
404 // i.e.:
405 // libGLES.so, or:
406 // libEGL.so, libGLESv1_CM.so, libGLESv2.so
407
408 for (size_t i=0 ; i<NELEM(searchPaths) ; i++) {
409 if (find(result, pattern, searchPaths[i], true)) {
410 return result;
411 }
412 }
413
414 // for compatibility with the old "egl.cfg" naming convention
415 // we look for files that match:
416 // libGLES_*.so, or:
417 // libEGL_*.so, libGLESv1_CM_*.so, libGLESv2_*.so
418
419 pattern.append("_");
420 for (size_t i=0 ; i<NELEM(searchPaths) ; i++) {
421 if (find(result, pattern, searchPaths[i], false)) {
422 return result;
423 }
424 }
425
426 // we didn't find the driver. gah.
427 result.clear();
428 return result;
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700429 }
Mathias Agopian99381422013-04-23 20:52:29 +0200430
431 private:
Mathias Agopian65421432017-03-08 11:49:05 -0800432 static bool find(std::string& result,
433 const std::string& pattern, const char* const search, bool exact) {
Mathias Agopian99381422013-04-23 20:52:29 +0200434 if (exact) {
Jesse Hall4aaa9602017-08-29 16:33:54 -0700435 std::string absolutePath = std::string(search) + "/" + pattern + ".so";
Mathias Agopian65421432017-03-08 11:49:05 -0800436 if (!access(absolutePath.c_str(), R_OK)) {
Mathias Agopian99381422013-04-23 20:52:29 +0200437 result = absolutePath;
438 return true;
439 }
440 return false;
441 }
442
443 DIR* d = opendir(search);
Yi Kong48a6cd22018-07-18 10:07:09 -0700444 if (d != nullptr) {
Mathias Agopian99381422013-04-23 20:52:29 +0200445 struct dirent* e;
Yi Kong48a6cd22018-07-18 10:07:09 -0700446 while ((e = readdir(d)) != nullptr) {
Mathias Agopian99381422013-04-23 20:52:29 +0200447 if (e->d_type == DT_DIR) {
448 continue;
449 }
450 if (!strcmp(e->d_name, "libGLES_android.so")) {
451 // always skip the software renderer
452 continue;
453 }
Mathias Agopian65421432017-03-08 11:49:05 -0800454 if (strstr(e->d_name, pattern.c_str()) == e->d_name) {
Mathias Agopian99381422013-04-23 20:52:29 +0200455 if (!strcmp(e->d_name + strlen(e->d_name) - 3, ".so")) {
Mathias Agopian65421432017-03-08 11:49:05 -0800456 result = std::string(search) + "/" + e->d_name;
Mathias Agopian99381422013-04-23 20:52:29 +0200457 closedir(d);
458 return true;
459 }
460 }
461 }
462 closedir(d);
463 }
464 return false;
465 }
466 };
467
468
Mathias Agopian65421432017-03-08 11:49:05 -0800469 std::string absolutePath = MatchFile::find(kind);
470 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 Northropfc94a582018-10-30 15:13:30 -0600513
514static bool check_angle_rules(void* so, const char* app_name) {
515 bool use_angle = false;
516 const int rules_fd = android::GraphicsEnv::getInstance().getAngleRulesFd();
517 const long rules_offset = android::GraphicsEnv::getInstance().getAngleRulesOffset();
518 const long rules_length = android::GraphicsEnv::getInstance().getAngleRulesLength();
519
520 std::string app_name_str = app_name ? app_name : "";
521 char manufacturer[PROPERTY_VALUE_MAX];
522 char model[PROPERTY_VALUE_MAX];
523 property_get("ro.product.manufacturer", manufacturer, "UNSET");
524 property_get("ro.product.model", model, "UNSET");
525
Cody Northropfc94a582018-10-30 15:13:30 -0600526 fpANGLEGetUtilityAPI ANGLEGetUtilityAPI =
527 (fpANGLEGetUtilityAPI)dlsym(so, "ANGLEGetUtilityAPI");
Cody Northrop15666572018-10-30 15:47:20 -0600528
Cody Northropfc94a582018-10-30 15:13:30 -0600529 if (ANGLEGetUtilityAPI) {
Cody Northrop15666572018-10-30 15:47:20 -0600530
531 // Negotiate the interface version by requesting most recent known to the platform
Cody Northropfc94a582018-10-30 15:13:30 -0600532 unsigned int versionToUse = 1;
533 if ((ANGLEGetUtilityAPI)(&versionToUse)) {
Cody Northrop15666572018-10-30 15:47:20 -0600534
535 // Add and remove versions below as needed
536 switch(versionToUse) {
537 case 1: {
538 ALOGV("Using version 1 of ANGLE opt-in/out logic interface");
539 fpAndroidUseANGLEForApplication AndroidUseANGLEForApplication =
540 (fpAndroidUseANGLEForApplication)dlsym(so, "AndroidUseANGLEForApplication");
541
542 if (AndroidUseANGLEForApplication) {
543 use_angle = (AndroidUseANGLEForApplication)(rules_fd, rules_offset,
544 rules_length, app_name_str.c_str(),
545 manufacturer, model);
546 } else {
547 ALOGW("Cannot find AndroidUseANGLEForApplication in ANGLE feature-support library");
548 }
Cody Northropfc94a582018-10-30 15:13:30 -0600549 }
Cody Northrop15666572018-10-30 15:47:20 -0600550 break;
551 default:
552 ALOGW("Cannot find supported version of ANGLE feature-support library, found version %u", versionToUse);
553 }
554 } else {
555 ALOGW("Cannot use ANGLE feature-support library, it is older than supported by EGL, requested version %u", versionToUse);
Cody Northropfc94a582018-10-30 15:13:30 -0600556 }
557 } else {
Cody Northrop15666572018-10-30 15:47:20 -0600558 ALOGW("Cannot find ANGLEGetUtilityAPI function");
Cody Northropfc94a582018-10-30 15:13:30 -0600559 }
Cody Northrop15666572018-10-30 15:47:20 -0600560
Cody Northropfc94a582018-10-30 15:13:30 -0600561 ALOGV("Close temporarily-loaded ANGLE opt-in/out logic");
562 return use_angle;
563}
564
Cody Northrop6d082ef2018-09-11 09:42:31 -0600565static void* load_angle(const char* kind, android_namespace_t* ns, egl_connection_t* cnx) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600566 // Only attempt to load ANGLE libs
567 if (strcmp(kind, "EGL") != 0 && strcmp(kind, "GLESv2") != 0 && strcmp(kind, "GLESv1_CM") != 0)
568 return nullptr;
569
Cody Northrop1f00e172018-04-02 11:23:31 -0600570 std::string name;
Ian Elliottb058aff2018-08-09 12:00:55 -0600571 char prop[PROPERTY_VALUE_MAX];
Cody Northrop1f00e172018-04-02 11:23:31 -0600572
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600573 const char* app_name = android::GraphicsEnv::getInstance().getAngleAppName();
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600574 bool developer_opt_in = android::GraphicsEnv::getInstance().getAngleDeveloperOptIn();
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600575
Ian Elliott34474472018-09-05 10:57:07 -0600576 // Determine whether or not to use ANGLE:
Cody Northropf0874d32018-10-29 10:59:45 -0600577 bool use_angle = developer_opt_in;
Ian Elliott34474472018-09-05 10:57:07 -0600578
579 if (use_angle) {
580 ALOGV("User set \"Developer Options\" to force the use of ANGLE");
Courtney Goeltzenleuchter734a4852018-10-23 16:26:24 -0600581 } else if (cnx->angleDecided) {
582 use_angle = cnx->useAngle;
Ian Elliott34474472018-09-05 10:57:07 -0600583 } else {
584 // The "Developer Options" value wasn't set to force the use of ANGLE. Need to temporarily
585 // load ANGLE and call the updatable opt-in/out logic:
Courtney Goeltzenleuchter28b97dc2018-10-24 17:06:35 -0600586
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600587 cnx->featureSo = load_angle_from_namespace("feature_support", ns);
588 if (cnx->featureSo) {
589 ALOGV("loaded ANGLE's opt-in/out logic from namespace");
Cody Northropfc94a582018-10-30 15:13:30 -0600590 use_angle = check_angle_rules(cnx->featureSo, app_name);
Ian Elliott34474472018-09-05 10:57:07 -0600591 } else {
592 // We weren't able to load and call the updateable opt-in/out logic.
593 // If we can't load the library, there is no ANGLE available.
594 use_angle = false;
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600595 ALOGV("Could not load the ANGLE opt-in/out logic, cannot use ANGLE.");
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600596 }
Courtney Goeltzenleuchter734a4852018-10-23 16:26:24 -0600597 cnx->angleDecided = true;
Cody Northrop1f00e172018-04-02 11:23:31 -0600598 }
Courtney Goeltzenleuchter985bc282018-11-02 14:24:55 -0600599 void* so = nullptr;
Ian Elliott34474472018-09-05 10:57:07 -0600600 if (use_angle) {
601 so = load_angle_from_namespace(kind, ns);
602 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600603
604 if (so) {
Ian Elliott34474472018-09-05 10:57:07 -0600605 ALOGV("Loaded ANGLE %s library for %s (instead of native)",
Ian Elliottb058aff2018-08-09 12:00:55 -0600606 kind, app_name ? app_name : "nullptr");
Ian Elliottb058aff2018-08-09 12:00:55 -0600607 property_get("debug.hwui.renderer", prop, "UNSET");
Ian Elliott34474472018-09-05 10:57:07 -0600608 ALOGV("Skia's renderer set to %s", prop);
Cody Northrop1f00e172018-04-02 11:23:31 -0600609 cnx->useAngle = true;
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600610
611 EGLint angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE;
612
613 char prop[PROPERTY_VALUE_MAX];
614 property_get("debug.angle.backend", prop, "0");
615 switch (atoi(prop)) {
616 case 1:
617 ALOGV("%s: Requesting OpenGLES back-end", __FUNCTION__);
618 angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE;
619 break;
620 case 2:
621 ALOGV("%s: Requesting Vulkan back-end", __FUNCTION__);
622 angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE;
623 break;
624 default:
625 break;
626 }
627
628 cnx->angleBackend = angleBackendDefault;
629 if (!cnx->vendorEGL && (cnx->angleBackend == EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE)) {
630 // Find and load vendor libEGL for ANGLE's GL back-end to use.
Cody Northrop1f00e172018-04-02 11:23:31 -0600631 cnx->vendorEGL = load_system_driver("EGL");
632 }
633 return so;
Ian Elliottb058aff2018-08-09 12:00:55 -0600634 } else {
Ian Elliott34474472018-09-05 10:57:07 -0600635 ALOGV("Loaded native %s library for %s (instead of ANGLE)",
Ian Elliottb058aff2018-08-09 12:00:55 -0600636 kind, app_name ? app_name : "nullptr");
Cody Northrop1f00e172018-04-02 11:23:31 -0600637 }
638
639 return nullptr;
640}
641
Mathias Agopian311b4792017-02-28 15:00:49 -0800642static const char* HAL_SUBNAME_KEY_PROPERTIES[2] = {
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800643 "ro.hardware.egl",
644 "ro.board.platform",
Mathias Agopian311b4792017-02-28 15:00:49 -0800645};
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800646
647static void* load_updated_driver(const char* kind, android_namespace_t* ns) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800648 ATRACE_CALL();
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800649 const android_dlextinfo dlextinfo = {
650 .flags = ANDROID_DLEXT_USE_NAMESPACE,
651 .library_namespace = ns,
652 };
653 void* so = nullptr;
654 char prop[PROPERTY_VALUE_MAX + 1];
655 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
656 if (property_get(key, prop, nullptr) > 0) {
Mathias Agopian65421432017-03-08 11:49:05 -0800657 std::string name = std::string("lib") + kind + "_" + prop + ".so";
658 so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
Mathias Agopian311b4792017-02-28 15:00:49 -0800659 if (so) {
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800660 return so;
Mathias Agopian311b4792017-02-28 15:00:49 -0800661 }
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800662 }
663 }
664 return nullptr;
665}
666
667void *Loader::load_driver(const char* kind,
668 egl_connection_t* cnx, uint32_t mask)
669{
Jesse Hall1508ae62017-01-19 17:43:26 -0800670 ATRACE_CALL();
671
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800672 void* dso = nullptr;
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600673 android_namespace_t* ns = android::GraphicsEnv::getInstance().getAngleNamespace();
Cody Northrop6d082ef2018-09-11 09:42:31 -0600674 if (ns) {
675 dso = load_angle(kind, ns, cnx);
676 }
Jiyong Parka243e5d2017-06-21 12:26:51 +0900677#ifndef __ANDROID_VNDK__
Cody Northrop1f00e172018-04-02 11:23:31 -0600678 if (!dso) {
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600679 android_namespace_t* ns = android::GraphicsEnv::getInstance().getDriverNamespace();
Cody Northrop1f00e172018-04-02 11:23:31 -0600680 if (ns) {
681 dso = load_updated_driver(kind, ns);
682 }
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800683 }
Jiyong Parka243e5d2017-06-21 12:26:51 +0900684#endif
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800685 if (!dso) {
686 dso = load_system_driver(kind);
687 if (!dso)
Yi Kong48a6cd22018-07-18 10:07:09 -0700688 return nullptr;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800689 }
690
Mathias Agopiande586972009-05-28 17:39:03 -0700691 if (mask & EGL) {
692 getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress");
693
Jesse Hall94cdba92013-07-11 09:40:35 -0700694 ALOGE_IF(!getProcAddress,
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800695 "can't find eglGetProcAddress() in EGL driver library");
Mathias Agopiande586972009-05-28 17:39:03 -0700696
Mathias Agopian618fa102009-10-14 02:06:37 -0700697 egl_t* egl = &cnx->egl;
Mathias Agopiande586972009-05-28 17:39:03 -0700698 __eglMustCastToProperFunctionPointerType* curr =
699 (__eglMustCastToProperFunctionPointerType*)egl;
700 char const * const * api = egl_names;
701 while (*api) {
702 char const * name = *api;
Jesse Hall94cdba92013-07-11 09:40:35 -0700703 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700704 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700705 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700706 // couldn't find the entry-point, use eglGetProcAddress()
707 f = getProcAddress(name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700708 if (f == nullptr) {
709 f = (__eglMustCastToProperFunctionPointerType)nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700710 }
711 }
712 *curr++ = f;
713 api++;
714 }
715 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700716
Mathias Agopiande586972009-05-28 17:39:03 -0700717 if (mask & GLESv1_CM) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700718 init_api(dso, gl_names_1, gl_names,
Mathias Agopian618fa102009-10-14 02:06:37 -0700719 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800720 &cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl,
Mathias Agopian618fa102009-10-14 02:06:37 -0700721 getProcAddress);
Mathias Agopiande586972009-05-28 17:39:03 -0700722 }
723
724 if (mask & GLESv2) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700725 init_api(dso, gl_names, nullptr,
Mathias Agopian618fa102009-10-14 02:06:37 -0700726 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800727 &cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl,
Mathias Agopiande586972009-05-28 17:39:03 -0700728 getProcAddress);
729 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700730
Mathias Agopiande586972009-05-28 17:39:03 -0700731 return dso;
732}
733
734// ----------------------------------------------------------------------------
735}; // namespace android
736// ----------------------------------------------------------------------------