blob: f8cfacbad519eb467220c38a9796b5a1fb72f03c [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
Mathias Agopian65421432017-03-08 11:49:05 -080036#include "egl_trace.h"
Mathias Agopian1cadb252011-05-23 17:26:14 -070037#include "egldefs.h"
Mathias Agopiande586972009-05-28 17:39:03 -070038
Jiyong Park5910dc72017-04-05 14:23:52 +090039extern "C" {
40 android_namespace_t* android_get_exported_namespace(const char*);
Ian Elliott34474472018-09-05 10:57:07 -060041
42 // TODO(ianelliott@): Get this from an ANGLE header:
43 typedef enum ANGLEPreference {
44 ANGLE_NO_PREFERENCE = 0,
45 ANGLE_PREFER_NATIVE = 1,
46 ANGLE_PREFER_ANGLE = 2,
47 } ANGLEPreference;
48
49 // TODO(ianelliott@): Get this from an ANGLE header:
50 typedef bool (*fpANGLEUseForApplication)(const char* appName, const char* deviceMfr,
51 const char* deviceModel, ANGLEPreference developerOption,
52 ANGLEPreference appPreference);
Jiyong Park5910dc72017-04-05 14:23:52 +090053}
54
Mathias Agopiande586972009-05-28 17:39:03 -070055// ----------------------------------------------------------------------------
56namespace android {
57// ----------------------------------------------------------------------------
58
59
60/*
Mathias Agopian99381422013-04-23 20:52:29 +020061 * EGL userspace drivers must be provided either:
62 * - as a single library:
63 * /vendor/lib/egl/libGLES.so
64 *
65 * - as separate libraries:
66 * /vendor/lib/egl/libEGL.so
67 * /vendor/lib/egl/libGLESv1_CM.so
68 * /vendor/lib/egl/libGLESv2.so
69 *
70 * The software renderer for the emulator must be provided as a single
71 * library at:
72 *
73 * /system/lib/egl/libGLES_android.so
74 *
75 *
76 * For backward compatibility and to facilitate the transition to
77 * this new naming scheme, the loader will additionally look for:
Jesse Hall94cdba92013-07-11 09:40:35 -070078 *
Mathias Agopian99381422013-04-23 20:52:29 +020079 * /{vendor|system}/lib/egl/lib{GLES | [EGL|GLESv1_CM|GLESv2]}_*.so
Jesse Hall94cdba92013-07-11 09:40:35 -070080 *
Mathias Agopiande586972009-05-28 17:39:03 -070081 */
82
Mathias Agopian65421432017-03-08 11:49:05 -080083Loader& Loader::getInstance() {
84 static Loader loader;
85 return loader;
86}
Mathias Agopiande586972009-05-28 17:39:03 -070087
David 'Digit' Turner80b30c22011-08-26 17:38:47 +020088/* This function is called to check whether we run inside the emulator,
89 * and if this is the case whether GLES GPU emulation is supported.
90 *
91 * Returned values are:
92 * -1 -> not running inside the emulator
93 * 0 -> running inside the emulator, but GPU emulation not supported
94 * 1 -> running inside the emulator, GPU emulation is supported
Nicolas Capens776951f2015-11-06 10:10:21 -050095 * through the "emulation" host-side OpenGL ES implementation.
96 * 2 -> running inside the emulator, GPU emulation is supported
97 * through a guest-side vendor driver's OpenGL ES implementation.
David 'Digit' Turner80b30c22011-08-26 17:38:47 +020098 */
99static int
100checkGlesEmulationStatus(void)
101{
102 /* We're going to check for the following kernel parameters:
103 *
104 * qemu=1 -> tells us that we run inside the emulator
105 * android.qemu.gles=<number> -> tells us the GLES GPU emulation status
106 *
107 * Note that we will return <number> if we find it. This let us support
108 * more additionnal emulation modes in the future.
109 */
110 char prop[PROPERTY_VALUE_MAX];
111 int result = -1;
112
113 /* First, check for qemu=1 */
114 property_get("ro.kernel.qemu",prop,"0");
115 if (atoi(prop) != 1)
116 return -1;
117
118 /* We are in the emulator, get GPU status value */
bohu69e5b1a2016-02-19 17:15:20 -0800119 property_get("qemu.gles",prop,"0");
David 'Digit' Turner80b30c22011-08-26 17:38:47 +0200120 return atoi(prop);
121}
122
Jesse Hall1508ae62017-01-19 17:43:26 -0800123static void* do_dlopen(const char* path, int mode) {
124 ATRACE_CALL();
125 return dlopen(path, mode);
126}
127
Jiyong Park5910dc72017-04-05 14:23:52 +0900128static void* do_android_dlopen_ext(const char* path, int mode, const android_dlextinfo* info) {
129 ATRACE_CALL();
130 return android_dlopen_ext(path, mode, info);
131}
132
Justin Yunb7320302017-05-22 15:13:40 +0900133static void* do_android_load_sphal_library(const char* path, int mode) {
134 ATRACE_CALL();
135 return android_load_sphal_library(path, mode);
136}
137
Mathias Agopiande586972009-05-28 17:39:03 -0700138// ----------------------------------------------------------------------------
139
Jesse Hall94cdba92013-07-11 09:40:35 -0700140Loader::driver_t::driver_t(void* gles)
Mathias Agopiande586972009-05-28 17:39:03 -0700141{
142 dso[0] = gles;
143 for (size_t i=1 ; i<NELEM(dso) ; i++)
Yi Kong48a6cd22018-07-18 10:07:09 -0700144 dso[i] = nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700145}
146
Jesse Hall94cdba92013-07-11 09:40:35 -0700147Loader::driver_t::~driver_t()
Mathias Agopiande586972009-05-28 17:39:03 -0700148{
149 for (size_t i=0 ; i<NELEM(dso) ; i++) {
150 if (dso[i]) {
151 dlclose(dso[i]);
Yi Kong48a6cd22018-07-18 10:07:09 -0700152 dso[i] = nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700153 }
154 }
155}
156
Mathias Agopian65421432017-03-08 11:49:05 -0800157int Loader::driver_t::set(void* hnd, int32_t api)
Mathias Agopiande586972009-05-28 17:39:03 -0700158{
159 switch (api) {
160 case EGL:
161 dso[0] = hnd;
162 break;
163 case GLESv1_CM:
164 dso[1] = hnd;
165 break;
166 case GLESv2:
167 dso[2] = hnd;
168 break;
169 default:
Mathias Agopian65421432017-03-08 11:49:05 -0800170 return -EOVERFLOW;
Mathias Agopiande586972009-05-28 17:39:03 -0700171 }
Mathias Agopian65421432017-03-08 11:49:05 -0800172 return 0;
Mathias Agopiande586972009-05-28 17:39:03 -0700173}
174
175// ----------------------------------------------------------------------------
176
Mathias Agopiande586972009-05-28 17:39:03 -0700177Loader::Loader()
Yi Kong48a6cd22018-07-18 10:07:09 -0700178 : getProcAddress(nullptr)
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800179{
Mathias Agopiande586972009-05-28 17:39:03 -0700180}
181
Mathias Agopian99381422013-04-23 20:52:29 +0200182Loader::~Loader() {
Mathias Agopiande586972009-05-28 17:39:03 -0700183}
184
Jesse Hallc07b5202013-07-04 12:08:16 -0700185static void* load_wrapper(const char* path) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800186 void* so = do_dlopen(path, RTLD_NOW | RTLD_LOCAL);
Jesse Hallc07b5202013-07-04 12:08:16 -0700187 ALOGE_IF(!so, "dlopen(\"%s\") failed: %s", path, dlerror());
188 return so;
189}
190
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700191#ifndef EGL_WRAPPER_DIR
192#if defined(__LP64__)
193#define EGL_WRAPPER_DIR "/system/lib64"
194#else
195#define EGL_WRAPPER_DIR "/system/lib"
196#endif
197#endif
198
bohu69e5b1a2016-02-19 17:15:20 -0800199static void setEmulatorGlesValue(void) {
200 char prop[PROPERTY_VALUE_MAX];
201 property_get("ro.kernel.qemu", prop, "0");
202 if (atoi(prop) != 1) return;
203
204 property_get("ro.kernel.qemu.gles",prop,"0");
205 if (atoi(prop) == 1) {
206 ALOGD("Emulator has host GPU support, qemu.gles is set to 1.");
207 property_set("qemu.gles", "1");
208 return;
209 }
210
211 // for now, checking the following
212 // directory is good enough for emulator system images
213 const char* vendor_lib_path =
214#if defined(__LP64__)
215 "/vendor/lib64/egl";
216#else
217 "/vendor/lib/egl";
218#endif
219
220 const bool has_vendor_lib = (access(vendor_lib_path, R_OK) == 0);
221 if (has_vendor_lib) {
222 ALOGD("Emulator has vendor provided software renderer, qemu.gles is set to 2.");
223 property_set("qemu.gles", "2");
224 } else {
225 ALOGD("Emulator without GPU support detected. "
226 "Fallback to legacy software renderer, qemu.gles is set to 0.");
227 property_set("qemu.gles", "0");
228 }
229}
230
Mathias Agopianada798b2012-02-13 17:09:30 -0800231void* Loader::open(egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700232{
Jesse Hall1508ae62017-01-19 17:43:26 -0800233 ATRACE_CALL();
234
Mathias Agopiande586972009-05-28 17:39:03 -0700235 void* dso;
Yi Kong48a6cd22018-07-18 10:07:09 -0700236 driver_t* hnd = nullptr;
Jesse Hall94cdba92013-07-11 09:40:35 -0700237
bohu69e5b1a2016-02-19 17:15:20 -0800238 setEmulatorGlesValue();
239
Mathias Agopian99381422013-04-23 20:52:29 +0200240 dso = load_driver("GLES", cnx, EGL | GLESv1_CM | GLESv2);
241 if (dso) {
242 hnd = new driver_t(dso);
243 } else {
244 // Always load EGL first
245 dso = load_driver("EGL", cnx, EGL);
Mathias Agopiande586972009-05-28 17:39:03 -0700246 if (dso) {
247 hnd = new driver_t(dso);
Mathias Agopian99381422013-04-23 20:52:29 +0200248 hnd->set( load_driver("GLESv1_CM", cnx, GLESv1_CM), GLESv1_CM );
249 hnd->set( load_driver("GLESv2", cnx, GLESv2), GLESv2 );
Mathias Agopiande586972009-05-28 17:39:03 -0700250 }
251 }
252
Mathias Agopian99381422013-04-23 20:52:29 +0200253 LOG_ALWAYS_FATAL_IF(!hnd, "couldn't find an OpenGL ES implementation");
Jesse Hallc07b5202013-07-04 12:08:16 -0700254
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700255 cnx->libEgl = load_wrapper(EGL_WRAPPER_DIR "/libEGL.so");
256 cnx->libGles2 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv2.so");
257 cnx->libGles1 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv1_CM.so");
258
Michael Chockc0ec5e22014-01-27 08:14:33 -0800259 LOG_ALWAYS_FATAL_IF(!cnx->libEgl,
260 "couldn't load system EGL wrapper libraries");
261
Jesse Hallc07b5202013-07-04 12:08:16 -0700262 LOG_ALWAYS_FATAL_IF(!cnx->libGles2 || !cnx->libGles1,
263 "couldn't load system OpenGL ES wrapper libraries");
264
Mathias Agopiande586972009-05-28 17:39:03 -0700265 return (void*)hnd;
266}
267
Mathias Agopian65421432017-03-08 11:49:05 -0800268void Loader::close(void* driver)
Mathias Agopiande586972009-05-28 17:39:03 -0700269{
270 driver_t* hnd = (driver_t*)driver;
271 delete hnd;
Mathias Agopiande586972009-05-28 17:39:03 -0700272}
273
Jesse Hall94cdba92013-07-11 09:40:35 -0700274void Loader::init_api(void* dso,
275 char const * const * api,
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700276 char const * const * ref_api,
Jesse Hall94cdba92013-07-11 09:40:35 -0700277 __eglMustCastToProperFunctionPointerType* curr,
278 getProcAddressType getProcAddress)
Mathias Agopiande586972009-05-28 17:39:03 -0700279{
Jesse Hall1508ae62017-01-19 17:43:26 -0800280 ATRACE_CALL();
281
Mathias Agopian7773c432012-02-13 20:06:08 -0800282 const ssize_t SIZE = 256;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700283 char scrap[SIZE];
Mathias Agopiande586972009-05-28 17:39:03 -0700284 while (*api) {
285 char const * name = *api;
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700286 if (ref_api) {
287 char const * ref_name = *ref_api;
288 if (std::strcmp(name, ref_name) != 0) {
289 *curr++ = nullptr;
290 ref_api++;
291 continue;
292 }
293 }
294
Jesse Hall94cdba92013-07-11 09:40:35 -0700295 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700296 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700297 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700298 // couldn't find the entry-point, use eglGetProcAddress()
299 f = getProcAddress(name);
300 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700301 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700302 // Try without the OES postfix
303 ssize_t index = ssize_t(strlen(name)) - 3;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700304 if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) {
Mathias Agopiande586972009-05-28 17:39:03 -0700305 strncpy(scrap, name, index);
306 scrap[index] = 0;
307 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000308 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700309 }
310 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700311 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700312 // Try with the OES postfix
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700313 ssize_t index = ssize_t(strlen(name)) - 3;
314 if (index>0 && strcmp(name+index, "OES")) {
315 snprintf(scrap, SIZE, "%sOES", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700316 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000317 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700318 }
319 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700320 if (f == nullptr) {
Steve Block9d453682011-12-20 16:23:08 +0000321 //ALOGD("%s", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700322 f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented;
Mathias Agopian48d438d2012-01-28 21:44:00 -0800323
324 /*
325 * GL_EXT_debug_label is special, we always report it as
326 * supported, it's handled by GLES_trace. If GLES_trace is not
327 * enabled, then these are no-ops.
328 */
329 if (!strcmp(name, "glInsertEventMarkerEXT")) {
330 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
331 } else if (!strcmp(name, "glPushGroupMarkerEXT")) {
332 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
333 } else if (!strcmp(name, "glPopGroupMarkerEXT")) {
334 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
335 }
Mathias Agopiande586972009-05-28 17:39:03 -0700336 }
337 *curr++ = f;
338 api++;
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700339 if (ref_api) ref_api++;
Mathias Agopiande586972009-05-28 17:39:03 -0700340 }
341}
342
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800343static void* load_system_driver(const char* kind) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800344 ATRACE_CALL();
Mathias Agopian99381422013-04-23 20:52:29 +0200345 class MatchFile {
346 public:
Mathias Agopian65421432017-03-08 11:49:05 -0800347 static std::string find(const char* kind) {
348 std::string result;
Nicolas Capens776951f2015-11-06 10:10:21 -0500349 int emulationStatus = checkGlesEmulationStatus();
350 switch (emulationStatus) {
351 case 0:
Nicolas Capens776951f2015-11-06 10:10:21 -0500352#if defined(__LP64__)
bohu3adf9e12017-07-02 22:08:13 -0700353 result = "/vendor/lib64/egl/libGLES_android.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500354#else
bohu3adf9e12017-07-02 22:08:13 -0700355 result = "/vendor/lib/egl/libGLES_android.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500356#endif
357 return result;
358 case 1:
359 // Use host-side OpenGL through the "emulation" library
360#if defined(__LP64__)
bohu3adf9e12017-07-02 22:08:13 -0700361 result = std::string("/vendor/lib64/egl/lib") + kind + "_emulation.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500362#else
bohu3adf9e12017-07-02 22:08:13 -0700363 result = std::string("/vendor/lib/egl/lib") + kind + "_emulation.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500364#endif
365 return result;
Lingfeng Yang0f82d872017-08-11 01:18:00 -0700366 case 2:
367 // Use guest side swiftshader library
368#if defined(__LP64__)
369 result = std::string("/vendor/lib64/egl/lib") + kind + "_swiftshader.so";
370#else
371 result = std::string("/vendor/lib/egl/lib") + kind + "_swiftshader.so";
372#endif
373 return result;
Nicolas Capens776951f2015-11-06 10:10:21 -0500374 default:
375 // Not in emulator, or use other guest-side implementation
376 break;
377 }
378
Mathias Agopian65421432017-03-08 11:49:05 -0800379 std::string pattern = std::string("lib") + kind;
Mathias Agopian99381422013-04-23 20:52:29 +0200380 const char* const searchPaths[] = {
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800381#if defined(__LP64__)
382 "/vendor/lib64/egl",
383 "/system/lib64/egl"
384#else
Mathias Agopian99381422013-04-23 20:52:29 +0200385 "/vendor/lib/egl",
386 "/system/lib/egl"
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800387#endif
Mathias Agopian99381422013-04-23 20:52:29 +0200388 };
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700389
Mathias Agopian99381422013-04-23 20:52:29 +0200390 // first, we search for the exact name of the GLES userspace
391 // driver in both locations.
392 // i.e.:
393 // libGLES.so, or:
394 // libEGL.so, libGLESv1_CM.so, libGLESv2.so
395
396 for (size_t i=0 ; i<NELEM(searchPaths) ; i++) {
397 if (find(result, pattern, searchPaths[i], true)) {
398 return result;
399 }
400 }
401
402 // for compatibility with the old "egl.cfg" naming convention
403 // we look for files that match:
404 // libGLES_*.so, or:
405 // libEGL_*.so, libGLESv1_CM_*.so, libGLESv2_*.so
406
407 pattern.append("_");
408 for (size_t i=0 ; i<NELEM(searchPaths) ; i++) {
409 if (find(result, pattern, searchPaths[i], false)) {
410 return result;
411 }
412 }
413
414 // we didn't find the driver. gah.
415 result.clear();
416 return result;
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700417 }
Mathias Agopian99381422013-04-23 20:52:29 +0200418
419 private:
Mathias Agopian65421432017-03-08 11:49:05 -0800420 static bool find(std::string& result,
421 const std::string& pattern, const char* const search, bool exact) {
Mathias Agopian99381422013-04-23 20:52:29 +0200422 if (exact) {
Jesse Hall4aaa9602017-08-29 16:33:54 -0700423 std::string absolutePath = std::string(search) + "/" + pattern + ".so";
Mathias Agopian65421432017-03-08 11:49:05 -0800424 if (!access(absolutePath.c_str(), R_OK)) {
Mathias Agopian99381422013-04-23 20:52:29 +0200425 result = absolutePath;
426 return true;
427 }
428 return false;
429 }
430
431 DIR* d = opendir(search);
Yi Kong48a6cd22018-07-18 10:07:09 -0700432 if (d != nullptr) {
Mathias Agopian99381422013-04-23 20:52:29 +0200433 struct dirent* e;
Yi Kong48a6cd22018-07-18 10:07:09 -0700434 while ((e = readdir(d)) != nullptr) {
Mathias Agopian99381422013-04-23 20:52:29 +0200435 if (e->d_type == DT_DIR) {
436 continue;
437 }
438 if (!strcmp(e->d_name, "libGLES_android.so")) {
439 // always skip the software renderer
440 continue;
441 }
Mathias Agopian65421432017-03-08 11:49:05 -0800442 if (strstr(e->d_name, pattern.c_str()) == e->d_name) {
Mathias Agopian99381422013-04-23 20:52:29 +0200443 if (!strcmp(e->d_name + strlen(e->d_name) - 3, ".so")) {
Mathias Agopian65421432017-03-08 11:49:05 -0800444 result = std::string(search) + "/" + e->d_name;
Mathias Agopian99381422013-04-23 20:52:29 +0200445 closedir(d);
446 return true;
447 }
448 }
449 }
450 closedir(d);
451 }
452 return false;
453 }
454 };
455
456
Mathias Agopian65421432017-03-08 11:49:05 -0800457 std::string absolutePath = MatchFile::find(kind);
458 if (absolutePath.empty()) {
Mathias Agopian99381422013-04-23 20:52:29 +0200459 // this happens often, we don't want to log an error
Yi Kong48a6cd22018-07-18 10:07:09 -0700460 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700461 }
Mathias Agopian65421432017-03-08 11:49:05 -0800462 const char* const driver_absolute_path = absolutePath.c_str();
Mathias Agopiande586972009-05-28 17:39:03 -0700463
Jiyong Park5910dc72017-04-05 14:23:52 +0900464 // Try to load drivers from the 'sphal' namespace, if it exist. Fall back to
Justin Yunb7320302017-05-22 15:13:40 +0900465 // the original routine when the namespace does not exist.
Jiyong Park5910dc72017-04-05 14:23:52 +0900466 // See /system/core/rootdir/etc/ld.config.txt for the configuration of the
467 // sphal namespace.
Justin Yunb7320302017-05-22 15:13:40 +0900468 void* dso = do_android_load_sphal_library(driver_absolute_path,
469 RTLD_NOW | RTLD_LOCAL);
Yi Kong48a6cd22018-07-18 10:07:09 -0700470 if (dso == nullptr) {
Mathias Agopian8c173842009-09-20 16:01:02 -0700471 const char* err = dlerror();
Mathias Agopian65421432017-03-08 11:49:05 -0800472 ALOGE("load_driver(%s): %s", driver_absolute_path, err ? err : "unknown");
Yi Kong48a6cd22018-07-18 10:07:09 -0700473 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700474 }
475
Steve Block9d453682011-12-20 16:23:08 +0000476 ALOGD("loaded %s", driver_absolute_path);
Mathias Agopianbaca89c2009-08-20 19:09:34 -0700477
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800478 return dso;
479}
480
Cody Northrop1f00e172018-04-02 11:23:31 -0600481static void* load_angle_from_namespace(const char* kind, android_namespace_t* ns) {
482 const android_dlextinfo dlextinfo = {
483 .flags = ANDROID_DLEXT_USE_NAMESPACE,
484 .library_namespace = ns,
485 };
486
487 std::string name = std::string("lib") + kind + "_angle.so";
488
489 void* so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
490
491 if (so) {
492 ALOGD("dlopen_ext from APK (%s) success at %p", name.c_str(), so);
493 return so;
494 } else {
495 ALOGE("dlopen_ext(\"%s\") failed: %s", name.c_str(), dlerror());
496 }
497
498 return nullptr;
499}
500
Ian Elliott34474472018-09-05 10:57:07 -0600501static ANGLEPreference getAnglePref(const char* app_pref) {
502 if (app_pref == nullptr)
503 return ANGLE_NO_PREFERENCE;
504
505 if (strcmp(app_pref, "angle") == 0) {
506 return ANGLE_PREFER_ANGLE;
507 } else if (strcmp(app_pref, "native") == 0) {
508 return ANGLE_PREFER_NATIVE;
509 }
510 return ANGLE_NO_PREFERENCE;
511}
512
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600513static void* load_angle(const char* kind, egl_connection_t* cnx) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600514 // Only attempt to load ANGLE libs
515 if (strcmp(kind, "EGL") != 0 && strcmp(kind, "GLESv2") != 0 && strcmp(kind, "GLESv1_CM") != 0)
516 return nullptr;
517
518 void* so = nullptr;
519 std::string name;
Ian Elliottb058aff2018-08-09 12:00:55 -0600520 char prop[PROPERTY_VALUE_MAX];
Cody Northrop1f00e172018-04-02 11:23:31 -0600521
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600522 android_namespace_t* ns = android_getAngleNamespace();
523 const char* app_name = android_getAngleAppName();
Cody Northrop49d51992018-08-29 16:37:09 -0600524 const char* app_pref = android_getAngleAppPref();
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600525 bool developer_opt_in = android_getAngleDeveloperOptIn();
526
Ian Elliott34474472018-09-05 10:57:07 -0600527 // Determine whether or not to use ANGLE:
528 ANGLEPreference developer_option = developer_opt_in ? ANGLE_PREFER_ANGLE : ANGLE_NO_PREFERENCE;
529 bool use_angle = (developer_option == ANGLE_PREFER_ANGLE);
530
531 if (use_angle) {
532 ALOGV("User set \"Developer Options\" to force the use of ANGLE");
533 } else {
534 // The "Developer Options" value wasn't set to force the use of ANGLE. Need to temporarily
535 // load ANGLE and call the updatable opt-in/out logic:
536 std::string app_name_str = app_name ? app_name : "";
537 char manufacturer[PROPERTY_VALUE_MAX];
538 char model[PROPERTY_VALUE_MAX];
539 property_get("ro.product.manufacturer", manufacturer, "UNSET");
540 property_get("ro.product.model", model, "UNSET");
541 ANGLEPreference app_preference = getAnglePref(android_getAngleAppPref());
542
543 so = load_angle_from_namespace("GLESv2", ns);
544 if (so) {
545 ALOGV("Temporarily loaded ANGLE's opt-in/out logic from namespace");
546 fpANGLEUseForApplication fp =
547 (fpANGLEUseForApplication)dlsym(so, "ANGLEUseForApplication");
548 if (fp) {
549 use_angle = (fp)(app_name_str.c_str(), manufacturer, model, developer_option,
550 app_preference);
551 ALOGV("Result of opt-in/out logic is %s", use_angle ? "true" : "false");
552 }
553
554 ALOGV("Close temporarily-loaded ANGLE opt-in/out logic");
555 dlclose(so);
556 so = nullptr;
557 } else {
558 // We weren't able to load and call the updateable opt-in/out logic.
559 // If we can't load the library, there is no ANGLE available.
560 use_angle = false;
561 ALOGV("Could not temporarily-load the ANGLE opt-in/out logic, cannot use ANGLE.");
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600562 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600563 }
Ian Elliott34474472018-09-05 10:57:07 -0600564 if (use_angle) {
565 so = load_angle_from_namespace(kind, ns);
566 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600567
568 if (so) {
Ian Elliott34474472018-09-05 10:57:07 -0600569 ALOGV("Loaded ANGLE %s library for %s (instead of native)",
Ian Elliottb058aff2018-08-09 12:00:55 -0600570 kind, app_name ? app_name : "nullptr");
571 property_get("debug.angle.backend", prop, "UNSET");
Ian Elliott34474472018-09-05 10:57:07 -0600572 ALOGV("ANGLE's backend set to %s", prop);
Ian Elliottb058aff2018-08-09 12:00:55 -0600573 property_get("debug.hwui.renderer", prop, "UNSET");
Ian Elliott34474472018-09-05 10:57:07 -0600574 ALOGV("Skia's renderer set to %s", prop);
Cody Northrop1f00e172018-04-02 11:23:31 -0600575 cnx->useAngle = true;
576 // Find and load vendor libEGL for ANGLE
577 if (!cnx->vendorEGL) {
578 cnx->vendorEGL = load_system_driver("EGL");
579 }
580 return so;
Ian Elliottb058aff2018-08-09 12:00:55 -0600581 } else {
Ian Elliott34474472018-09-05 10:57:07 -0600582 ALOGV("Loaded native %s library for %s (instead of ANGLE)",
Ian Elliottb058aff2018-08-09 12:00:55 -0600583 kind, app_name ? app_name : "nullptr");
Cody Northrop1f00e172018-04-02 11:23:31 -0600584 }
585
586 return nullptr;
587}
588
Mathias Agopian311b4792017-02-28 15:00:49 -0800589static const char* HAL_SUBNAME_KEY_PROPERTIES[2] = {
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800590 "ro.hardware.egl",
591 "ro.board.platform",
Mathias Agopian311b4792017-02-28 15:00:49 -0800592};
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800593
594static void* load_updated_driver(const char* kind, android_namespace_t* ns) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800595 ATRACE_CALL();
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800596 const android_dlextinfo dlextinfo = {
597 .flags = ANDROID_DLEXT_USE_NAMESPACE,
598 .library_namespace = ns,
599 };
600 void* so = nullptr;
601 char prop[PROPERTY_VALUE_MAX + 1];
602 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
603 if (property_get(key, prop, nullptr) > 0) {
Mathias Agopian65421432017-03-08 11:49:05 -0800604 std::string name = std::string("lib") + kind + "_" + prop + ".so";
605 so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
Mathias Agopian311b4792017-02-28 15:00:49 -0800606 if (so) {
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800607 return so;
Mathias Agopian311b4792017-02-28 15:00:49 -0800608 }
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800609 }
610 }
611 return nullptr;
612}
613
614void *Loader::load_driver(const char* kind,
615 egl_connection_t* cnx, uint32_t mask)
616{
Jesse Hall1508ae62017-01-19 17:43:26 -0800617 ATRACE_CALL();
618
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800619 void* dso = nullptr;
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600620 dso = load_angle(kind, cnx);
Jiyong Parka243e5d2017-06-21 12:26:51 +0900621#ifndef __ANDROID_VNDK__
Cody Northrop1f00e172018-04-02 11:23:31 -0600622 if (!dso) {
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600623 android_namespace_t* ns = android_getDriverNamespace();
Cody Northrop1f00e172018-04-02 11:23:31 -0600624 if (ns) {
625 dso = load_updated_driver(kind, ns);
626 }
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800627 }
Jiyong Parka243e5d2017-06-21 12:26:51 +0900628#endif
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800629 if (!dso) {
630 dso = load_system_driver(kind);
631 if (!dso)
Yi Kong48a6cd22018-07-18 10:07:09 -0700632 return nullptr;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800633 }
634
Mathias Agopiande586972009-05-28 17:39:03 -0700635 if (mask & EGL) {
636 getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress");
637
Jesse Hall94cdba92013-07-11 09:40:35 -0700638 ALOGE_IF(!getProcAddress,
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800639 "can't find eglGetProcAddress() in EGL driver library");
Mathias Agopiande586972009-05-28 17:39:03 -0700640
Mathias Agopian618fa102009-10-14 02:06:37 -0700641 egl_t* egl = &cnx->egl;
Mathias Agopiande586972009-05-28 17:39:03 -0700642 __eglMustCastToProperFunctionPointerType* curr =
643 (__eglMustCastToProperFunctionPointerType*)egl;
644 char const * const * api = egl_names;
645 while (*api) {
646 char const * name = *api;
Jesse Hall94cdba92013-07-11 09:40:35 -0700647 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700648 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700649 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700650 // couldn't find the entry-point, use eglGetProcAddress()
651 f = getProcAddress(name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700652 if (f == nullptr) {
653 f = (__eglMustCastToProperFunctionPointerType)nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700654 }
655 }
656 *curr++ = f;
657 api++;
658 }
659 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700660
Mathias Agopiande586972009-05-28 17:39:03 -0700661 if (mask & GLESv1_CM) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700662 init_api(dso, gl_names_1, gl_names,
Mathias Agopian618fa102009-10-14 02:06:37 -0700663 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800664 &cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl,
Mathias Agopian618fa102009-10-14 02:06:37 -0700665 getProcAddress);
Mathias Agopiande586972009-05-28 17:39:03 -0700666 }
667
668 if (mask & GLESv2) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700669 init_api(dso, gl_names, nullptr,
Mathias Agopian618fa102009-10-14 02:06:37 -0700670 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800671 &cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl,
Mathias Agopiande586972009-05-28 17:39:03 -0700672 getProcAddress);
673 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700674
Mathias Agopiande586972009-05-28 17:39:03 -0700675 return dso;
676}
677
678// ----------------------------------------------------------------------------
679}; // namespace android
680// ----------------------------------------------------------------------------