blob: cceaa8e7bee6e729692a5a79e7243954decccf47 [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"
Mathias Agopiande586972009-05-28 17:39:03 -070039
Jiyong Park5910dc72017-04-05 14:23:52 +090040extern "C" {
41 android_namespace_t* android_get_exported_namespace(const char*);
Ian Elliott34474472018-09-05 10:57:07 -060042
43 // TODO(ianelliott@): Get this from an ANGLE header:
44 typedef enum ANGLEPreference {
45 ANGLE_NO_PREFERENCE = 0,
46 ANGLE_PREFER_NATIVE = 1,
47 ANGLE_PREFER_ANGLE = 2,
48 } ANGLEPreference;
49
50 // TODO(ianelliott@): Get this from an ANGLE header:
51 typedef bool (*fpANGLEUseForApplication)(const char* appName, const char* deviceMfr,
52 const char* deviceModel, ANGLEPreference developerOption,
53 ANGLEPreference appPreference);
Jiyong Park5910dc72017-04-05 14:23:52 +090054}
55
Mathias Agopiande586972009-05-28 17:39:03 -070056// ----------------------------------------------------------------------------
57namespace android {
58// ----------------------------------------------------------------------------
59
60
61/*
Mathias Agopian99381422013-04-23 20:52:29 +020062 * EGL userspace drivers must be provided either:
63 * - as a single library:
64 * /vendor/lib/egl/libGLES.so
65 *
66 * - as separate libraries:
67 * /vendor/lib/egl/libEGL.so
68 * /vendor/lib/egl/libGLESv1_CM.so
69 * /vendor/lib/egl/libGLESv2.so
70 *
71 * The software renderer for the emulator must be provided as a single
72 * library at:
73 *
74 * /system/lib/egl/libGLES_android.so
75 *
76 *
77 * For backward compatibility and to facilitate the transition to
78 * this new naming scheme, the loader will additionally look for:
Jesse Hall94cdba92013-07-11 09:40:35 -070079 *
Mathias Agopian99381422013-04-23 20:52:29 +020080 * /{vendor|system}/lib/egl/lib{GLES | [EGL|GLESv1_CM|GLESv2]}_*.so
Jesse Hall94cdba92013-07-11 09:40:35 -070081 *
Mathias Agopiande586972009-05-28 17:39:03 -070082 */
83
Mathias Agopian65421432017-03-08 11:49:05 -080084Loader& Loader::getInstance() {
85 static Loader loader;
86 return loader;
87}
Mathias Agopiande586972009-05-28 17:39:03 -070088
David 'Digit' Turner80b30c22011-08-26 17:38:47 +020089/* This function is called to check whether we run inside the emulator,
90 * and if this is the case whether GLES GPU emulation is supported.
91 *
92 * Returned values are:
93 * -1 -> not running inside the emulator
94 * 0 -> running inside the emulator, but GPU emulation not supported
95 * 1 -> running inside the emulator, GPU emulation is supported
Nicolas Capens776951f2015-11-06 10:10:21 -050096 * through the "emulation" host-side OpenGL ES implementation.
97 * 2 -> running inside the emulator, GPU emulation is supported
98 * through a guest-side vendor driver's OpenGL ES implementation.
David 'Digit' Turner80b30c22011-08-26 17:38:47 +020099 */
100static int
101checkGlesEmulationStatus(void)
102{
103 /* We're going to check for the following kernel parameters:
104 *
105 * qemu=1 -> tells us that we run inside the emulator
106 * android.qemu.gles=<number> -> tells us the GLES GPU emulation status
107 *
108 * Note that we will return <number> if we find it. This let us support
109 * more additionnal emulation modes in the future.
110 */
111 char prop[PROPERTY_VALUE_MAX];
112 int result = -1;
113
114 /* First, check for qemu=1 */
115 property_get("ro.kernel.qemu",prop,"0");
116 if (atoi(prop) != 1)
117 return -1;
118
119 /* We are in the emulator, get GPU status value */
bohu69e5b1a2016-02-19 17:15:20 -0800120 property_get("qemu.gles",prop,"0");
David 'Digit' Turner80b30c22011-08-26 17:38:47 +0200121 return atoi(prop);
122}
123
Jesse Hall1508ae62017-01-19 17:43:26 -0800124static void* do_dlopen(const char* path, int mode) {
125 ATRACE_CALL();
126 return dlopen(path, mode);
127}
128
Jiyong Park5910dc72017-04-05 14:23:52 +0900129static void* do_android_dlopen_ext(const char* path, int mode, const android_dlextinfo* info) {
130 ATRACE_CALL();
131 return android_dlopen_ext(path, mode, info);
132}
133
Justin Yunb7320302017-05-22 15:13:40 +0900134static void* do_android_load_sphal_library(const char* path, int mode) {
135 ATRACE_CALL();
136 return android_load_sphal_library(path, mode);
137}
138
Mathias Agopiande586972009-05-28 17:39:03 -0700139// ----------------------------------------------------------------------------
140
Jesse Hall94cdba92013-07-11 09:40:35 -0700141Loader::driver_t::driver_t(void* gles)
Mathias Agopiande586972009-05-28 17:39:03 -0700142{
143 dso[0] = gles;
144 for (size_t i=1 ; i<NELEM(dso) ; i++)
Yi Kong48a6cd22018-07-18 10:07:09 -0700145 dso[i] = nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700146}
147
Jesse Hall94cdba92013-07-11 09:40:35 -0700148Loader::driver_t::~driver_t()
Mathias Agopiande586972009-05-28 17:39:03 -0700149{
150 for (size_t i=0 ; i<NELEM(dso) ; i++) {
151 if (dso[i]) {
152 dlclose(dso[i]);
Yi Kong48a6cd22018-07-18 10:07:09 -0700153 dso[i] = nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700154 }
155 }
156}
157
Mathias Agopian65421432017-03-08 11:49:05 -0800158int Loader::driver_t::set(void* hnd, int32_t api)
Mathias Agopiande586972009-05-28 17:39:03 -0700159{
160 switch (api) {
161 case EGL:
162 dso[0] = hnd;
163 break;
164 case GLESv1_CM:
165 dso[1] = hnd;
166 break;
167 case GLESv2:
168 dso[2] = hnd;
169 break;
170 default:
Mathias Agopian65421432017-03-08 11:49:05 -0800171 return -EOVERFLOW;
Mathias Agopiande586972009-05-28 17:39:03 -0700172 }
Mathias Agopian65421432017-03-08 11:49:05 -0800173 return 0;
Mathias Agopiande586972009-05-28 17:39:03 -0700174}
175
176// ----------------------------------------------------------------------------
177
Mathias Agopiande586972009-05-28 17:39:03 -0700178Loader::Loader()
Yi Kong48a6cd22018-07-18 10:07:09 -0700179 : getProcAddress(nullptr)
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800180{
Mathias Agopiande586972009-05-28 17:39:03 -0700181}
182
Mathias Agopian99381422013-04-23 20:52:29 +0200183Loader::~Loader() {
Mathias Agopiande586972009-05-28 17:39:03 -0700184}
185
Jesse Hallc07b5202013-07-04 12:08:16 -0700186static void* load_wrapper(const char* path) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800187 void* so = do_dlopen(path, RTLD_NOW | RTLD_LOCAL);
Jesse Hallc07b5202013-07-04 12:08:16 -0700188 ALOGE_IF(!so, "dlopen(\"%s\") failed: %s", path, dlerror());
189 return so;
190}
191
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700192#ifndef EGL_WRAPPER_DIR
193#if defined(__LP64__)
194#define EGL_WRAPPER_DIR "/system/lib64"
195#else
196#define EGL_WRAPPER_DIR "/system/lib"
197#endif
198#endif
199
bohu69e5b1a2016-02-19 17:15:20 -0800200static void setEmulatorGlesValue(void) {
201 char prop[PROPERTY_VALUE_MAX];
202 property_get("ro.kernel.qemu", prop, "0");
203 if (atoi(prop) != 1) return;
204
205 property_get("ro.kernel.qemu.gles",prop,"0");
206 if (atoi(prop) == 1) {
207 ALOGD("Emulator has host GPU support, qemu.gles is set to 1.");
208 property_set("qemu.gles", "1");
209 return;
210 }
211
212 // for now, checking the following
213 // directory is good enough for emulator system images
214 const char* vendor_lib_path =
215#if defined(__LP64__)
216 "/vendor/lib64/egl";
217#else
218 "/vendor/lib/egl";
219#endif
220
221 const bool has_vendor_lib = (access(vendor_lib_path, R_OK) == 0);
222 if (has_vendor_lib) {
223 ALOGD("Emulator has vendor provided software renderer, qemu.gles is set to 2.");
224 property_set("qemu.gles", "2");
225 } else {
226 ALOGD("Emulator without GPU support detected. "
227 "Fallback to legacy software renderer, qemu.gles is set to 0.");
228 property_set("qemu.gles", "0");
229 }
230}
231
Mathias Agopianada798b2012-02-13 17:09:30 -0800232void* Loader::open(egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700233{
Jesse Hall1508ae62017-01-19 17:43:26 -0800234 ATRACE_CALL();
235
Mathias Agopiande586972009-05-28 17:39:03 -0700236 void* dso;
Yi Kong48a6cd22018-07-18 10:07:09 -0700237 driver_t* hnd = nullptr;
Jesse Hall94cdba92013-07-11 09:40:35 -0700238
bohu69e5b1a2016-02-19 17:15:20 -0800239 setEmulatorGlesValue();
240
Cody Northrop9d5d33d2018-10-15 18:32:14 -0600241 dso = load_driver("GLES", cnx, EGL | GLESv1_CM | GLESv2);
Mathias Agopian99381422013-04-23 20:52:29 +0200242 if (dso) {
243 hnd = new driver_t(dso);
244 } else {
245 // Always load EGL first
Cody Northrop9d5d33d2018-10-15 18:32:14 -0600246 dso = load_driver("EGL", cnx, EGL);
Mathias Agopiande586972009-05-28 17:39:03 -0700247 if (dso) {
248 hnd = new driver_t(dso);
Mathias Agopian99381422013-04-23 20:52:29 +0200249 hnd->set( load_driver("GLESv1_CM", cnx, GLESv1_CM), GLESv1_CM );
250 hnd->set( load_driver("GLESv2", cnx, GLESv2), GLESv2 );
Mathias Agopiande586972009-05-28 17:39:03 -0700251 }
252 }
253
Mathias Agopian99381422013-04-23 20:52:29 +0200254 LOG_ALWAYS_FATAL_IF(!hnd, "couldn't find an OpenGL ES implementation");
Jesse Hallc07b5202013-07-04 12:08:16 -0700255
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700256 cnx->libEgl = load_wrapper(EGL_WRAPPER_DIR "/libEGL.so");
257 cnx->libGles2 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv2.so");
258 cnx->libGles1 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv1_CM.so");
259
Michael Chockc0ec5e22014-01-27 08:14:33 -0800260 LOG_ALWAYS_FATAL_IF(!cnx->libEgl,
261 "couldn't load system EGL wrapper libraries");
262
Jesse Hallc07b5202013-07-04 12:08:16 -0700263 LOG_ALWAYS_FATAL_IF(!cnx->libGles2 || !cnx->libGles1,
264 "couldn't load system OpenGL ES wrapper libraries");
265
Mathias Agopiande586972009-05-28 17:39:03 -0700266 return (void*)hnd;
267}
268
Mathias Agopian65421432017-03-08 11:49:05 -0800269void Loader::close(void* driver)
Mathias Agopiande586972009-05-28 17:39:03 -0700270{
271 driver_t* hnd = (driver_t*)driver;
272 delete hnd;
Mathias Agopiande586972009-05-28 17:39:03 -0700273}
274
Jesse Hall94cdba92013-07-11 09:40:35 -0700275void Loader::init_api(void* dso,
276 char const * const * api,
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700277 char const * const * ref_api,
Jesse Hall94cdba92013-07-11 09:40:35 -0700278 __eglMustCastToProperFunctionPointerType* curr,
279 getProcAddressType getProcAddress)
Mathias Agopiande586972009-05-28 17:39:03 -0700280{
Jesse Hall1508ae62017-01-19 17:43:26 -0800281 ATRACE_CALL();
282
Mathias Agopian7773c432012-02-13 20:06:08 -0800283 const ssize_t SIZE = 256;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700284 char scrap[SIZE];
Mathias Agopiande586972009-05-28 17:39:03 -0700285 while (*api) {
286 char const * name = *api;
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700287 if (ref_api) {
288 char const * ref_name = *ref_api;
289 if (std::strcmp(name, ref_name) != 0) {
290 *curr++ = nullptr;
291 ref_api++;
292 continue;
293 }
294 }
295
Jesse Hall94cdba92013-07-11 09:40:35 -0700296 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700297 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700298 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700299 // couldn't find the entry-point, use eglGetProcAddress()
300 f = getProcAddress(name);
301 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700302 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700303 // Try without the OES postfix
304 ssize_t index = ssize_t(strlen(name)) - 3;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700305 if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) {
Mathias Agopiande586972009-05-28 17:39:03 -0700306 strncpy(scrap, name, index);
307 scrap[index] = 0;
308 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000309 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700310 }
311 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700312 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700313 // Try with the OES postfix
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700314 ssize_t index = ssize_t(strlen(name)) - 3;
315 if (index>0 && strcmp(name+index, "OES")) {
316 snprintf(scrap, SIZE, "%sOES", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700317 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000318 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700319 }
320 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700321 if (f == nullptr) {
Steve Block9d453682011-12-20 16:23:08 +0000322 //ALOGD("%s", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700323 f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented;
Mathias Agopian48d438d2012-01-28 21:44:00 -0800324
325 /*
326 * GL_EXT_debug_label is special, we always report it as
327 * supported, it's handled by GLES_trace. If GLES_trace is not
328 * enabled, then these are no-ops.
329 */
330 if (!strcmp(name, "glInsertEventMarkerEXT")) {
331 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
332 } else if (!strcmp(name, "glPushGroupMarkerEXT")) {
333 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
334 } else if (!strcmp(name, "glPopGroupMarkerEXT")) {
335 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
336 }
Mathias Agopiande586972009-05-28 17:39:03 -0700337 }
338 *curr++ = f;
339 api++;
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700340 if (ref_api) ref_api++;
Mathias Agopiande586972009-05-28 17:39:03 -0700341 }
342}
343
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800344static void* load_system_driver(const char* kind) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800345 ATRACE_CALL();
Mathias Agopian99381422013-04-23 20:52:29 +0200346 class MatchFile {
347 public:
Mathias Agopian65421432017-03-08 11:49:05 -0800348 static std::string find(const char* kind) {
349 std::string result;
Nicolas Capens776951f2015-11-06 10:10:21 -0500350 int emulationStatus = checkGlesEmulationStatus();
351 switch (emulationStatus) {
352 case 0:
Nicolas Capens776951f2015-11-06 10:10:21 -0500353#if defined(__LP64__)
bohu3adf9e12017-07-02 22:08:13 -0700354 result = "/vendor/lib64/egl/libGLES_android.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500355#else
bohu3adf9e12017-07-02 22:08:13 -0700356 result = "/vendor/lib/egl/libGLES_android.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500357#endif
358 return result;
359 case 1:
360 // Use host-side OpenGL through the "emulation" library
361#if defined(__LP64__)
bohu3adf9e12017-07-02 22:08:13 -0700362 result = std::string("/vendor/lib64/egl/lib") + kind + "_emulation.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500363#else
bohu3adf9e12017-07-02 22:08:13 -0700364 result = std::string("/vendor/lib/egl/lib") + kind + "_emulation.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500365#endif
366 return result;
Lingfeng Yang0f82d872017-08-11 01:18:00 -0700367 case 2:
368 // Use guest side swiftshader library
369#if defined(__LP64__)
370 result = std::string("/vendor/lib64/egl/lib") + kind + "_swiftshader.so";
371#else
372 result = std::string("/vendor/lib/egl/lib") + kind + "_swiftshader.so";
373#endif
374 return result;
Nicolas Capens776951f2015-11-06 10:10:21 -0500375 default:
376 // Not in emulator, or use other guest-side implementation
377 break;
378 }
379
Mathias Agopian65421432017-03-08 11:49:05 -0800380 std::string pattern = std::string("lib") + kind;
Mathias Agopian99381422013-04-23 20:52:29 +0200381 const char* const searchPaths[] = {
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800382#if defined(__LP64__)
383 "/vendor/lib64/egl",
384 "/system/lib64/egl"
385#else
Mathias Agopian99381422013-04-23 20:52:29 +0200386 "/vendor/lib/egl",
387 "/system/lib/egl"
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800388#endif
Mathias Agopian99381422013-04-23 20:52:29 +0200389 };
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700390
Mathias Agopian99381422013-04-23 20:52:29 +0200391 // first, we search for the exact name of the GLES userspace
392 // driver in both locations.
393 // i.e.:
394 // libGLES.so, or:
395 // libEGL.so, libGLESv1_CM.so, libGLESv2.so
396
397 for (size_t i=0 ; i<NELEM(searchPaths) ; i++) {
398 if (find(result, pattern, searchPaths[i], true)) {
399 return result;
400 }
401 }
402
403 // for compatibility with the old "egl.cfg" naming convention
404 // we look for files that match:
405 // libGLES_*.so, or:
406 // libEGL_*.so, libGLESv1_CM_*.so, libGLESv2_*.so
407
408 pattern.append("_");
409 for (size_t i=0 ; i<NELEM(searchPaths) ; i++) {
410 if (find(result, pattern, searchPaths[i], false)) {
411 return result;
412 }
413 }
414
415 // we didn't find the driver. gah.
416 result.clear();
417 return result;
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700418 }
Mathias Agopian99381422013-04-23 20:52:29 +0200419
420 private:
Mathias Agopian65421432017-03-08 11:49:05 -0800421 static bool find(std::string& result,
422 const std::string& pattern, const char* const search, bool exact) {
Mathias Agopian99381422013-04-23 20:52:29 +0200423 if (exact) {
Jesse Hall4aaa9602017-08-29 16:33:54 -0700424 std::string absolutePath = std::string(search) + "/" + pattern + ".so";
Mathias Agopian65421432017-03-08 11:49:05 -0800425 if (!access(absolutePath.c_str(), R_OK)) {
Mathias Agopian99381422013-04-23 20:52:29 +0200426 result = absolutePath;
427 return true;
428 }
429 return false;
430 }
431
432 DIR* d = opendir(search);
Yi Kong48a6cd22018-07-18 10:07:09 -0700433 if (d != nullptr) {
Mathias Agopian99381422013-04-23 20:52:29 +0200434 struct dirent* e;
Yi Kong48a6cd22018-07-18 10:07:09 -0700435 while ((e = readdir(d)) != nullptr) {
Mathias Agopian99381422013-04-23 20:52:29 +0200436 if (e->d_type == DT_DIR) {
437 continue;
438 }
439 if (!strcmp(e->d_name, "libGLES_android.so")) {
440 // always skip the software renderer
441 continue;
442 }
Mathias Agopian65421432017-03-08 11:49:05 -0800443 if (strstr(e->d_name, pattern.c_str()) == e->d_name) {
Mathias Agopian99381422013-04-23 20:52:29 +0200444 if (!strcmp(e->d_name + strlen(e->d_name) - 3, ".so")) {
Mathias Agopian65421432017-03-08 11:49:05 -0800445 result = std::string(search) + "/" + e->d_name;
Mathias Agopian99381422013-04-23 20:52:29 +0200446 closedir(d);
447 return true;
448 }
449 }
450 }
451 closedir(d);
452 }
453 return false;
454 }
455 };
456
457
Mathias Agopian65421432017-03-08 11:49:05 -0800458 std::string absolutePath = MatchFile::find(kind);
459 if (absolutePath.empty()) {
Mathias Agopian99381422013-04-23 20:52:29 +0200460 // this happens often, we don't want to log an error
Yi Kong48a6cd22018-07-18 10:07:09 -0700461 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700462 }
Mathias Agopian65421432017-03-08 11:49:05 -0800463 const char* const driver_absolute_path = absolutePath.c_str();
Mathias Agopiande586972009-05-28 17:39:03 -0700464
Jiyong Park5910dc72017-04-05 14:23:52 +0900465 // Try to load drivers from the 'sphal' namespace, if it exist. Fall back to
Justin Yunb7320302017-05-22 15:13:40 +0900466 // the original routine when the namespace does not exist.
Jiyong Park5910dc72017-04-05 14:23:52 +0900467 // See /system/core/rootdir/etc/ld.config.txt for the configuration of the
468 // sphal namespace.
Justin Yunb7320302017-05-22 15:13:40 +0900469 void* dso = do_android_load_sphal_library(driver_absolute_path,
470 RTLD_NOW | RTLD_LOCAL);
Yi Kong48a6cd22018-07-18 10:07:09 -0700471 if (dso == nullptr) {
Mathias Agopian8c173842009-09-20 16:01:02 -0700472 const char* err = dlerror();
Mathias Agopian65421432017-03-08 11:49:05 -0800473 ALOGE("load_driver(%s): %s", driver_absolute_path, err ? err : "unknown");
Yi Kong48a6cd22018-07-18 10:07:09 -0700474 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700475 }
476
Steve Block9d453682011-12-20 16:23:08 +0000477 ALOGD("loaded %s", driver_absolute_path);
Mathias Agopianbaca89c2009-08-20 19:09:34 -0700478
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800479 return dso;
480}
481
Cody Northrop1f00e172018-04-02 11:23:31 -0600482static void* load_angle_from_namespace(const char* kind, android_namespace_t* ns) {
483 const android_dlextinfo dlextinfo = {
484 .flags = ANDROID_DLEXT_USE_NAMESPACE,
485 .library_namespace = ns,
486 };
487
488 std::string name = std::string("lib") + kind + "_angle.so";
489
490 void* so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
491
492 if (so) {
493 ALOGD("dlopen_ext from APK (%s) success at %p", name.c_str(), so);
494 return so;
495 } else {
496 ALOGE("dlopen_ext(\"%s\") failed: %s", name.c_str(), dlerror());
497 }
498
499 return nullptr;
500}
501
Ian Elliott34474472018-09-05 10:57:07 -0600502static ANGLEPreference getAnglePref(const char* app_pref) {
503 if (app_pref == nullptr)
504 return ANGLE_NO_PREFERENCE;
505
506 if (strcmp(app_pref, "angle") == 0) {
507 return ANGLE_PREFER_ANGLE;
508 } else if (strcmp(app_pref, "native") == 0) {
509 return ANGLE_PREFER_NATIVE;
510 }
511 return ANGLE_NO_PREFERENCE;
512}
513
Cody Northrop6d082ef2018-09-11 09:42:31 -0600514static void* load_angle(const char* kind, android_namespace_t* ns, egl_connection_t* cnx) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600515 // Only attempt to load ANGLE libs
516 if (strcmp(kind, "EGL") != 0 && strcmp(kind, "GLESv2") != 0 && strcmp(kind, "GLESv1_CM") != 0)
517 return nullptr;
518
519 void* so = nullptr;
520 std::string name;
Ian Elliottb058aff2018-08-09 12:00:55 -0600521 char prop[PROPERTY_VALUE_MAX];
Cody Northrop1f00e172018-04-02 11:23:31 -0600522
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600523 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
Courtney Goeltzenleuchter075437f2018-10-08 19:35:31 -0600543 so = load_angle_from_namespace("feature_support", ns);
Ian Elliott34474472018-09-05 10:57:07 -0600544 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");
Courtney Goeltzenleuchter075437f2018-10-08 19:35:31 -0600552 } else {
553 ALOGW("Cannot find ANGLEUseForApplication in library");
Ian Elliott34474472018-09-05 10:57:07 -0600554 }
555
556 ALOGV("Close temporarily-loaded ANGLE opt-in/out logic");
557 dlclose(so);
558 so = nullptr;
559 } else {
560 // We weren't able to load and call the updateable opt-in/out logic.
561 // If we can't load the library, there is no ANGLE available.
562 use_angle = false;
563 ALOGV("Could not temporarily-load the ANGLE opt-in/out logic, cannot use ANGLE.");
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600564 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600565 }
Ian Elliott34474472018-09-05 10:57:07 -0600566 if (use_angle) {
567 so = load_angle_from_namespace(kind, ns);
568 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600569
570 if (so) {
Ian Elliott34474472018-09-05 10:57:07 -0600571 ALOGV("Loaded ANGLE %s library for %s (instead of native)",
Ian Elliottb058aff2018-08-09 12:00:55 -0600572 kind, app_name ? app_name : "nullptr");
573 property_get("debug.angle.backend", prop, "UNSET");
Ian Elliott34474472018-09-05 10:57:07 -0600574 ALOGV("ANGLE's backend set to %s", prop);
Ian Elliottb058aff2018-08-09 12:00:55 -0600575 property_get("debug.hwui.renderer", prop, "UNSET");
Ian Elliott34474472018-09-05 10:57:07 -0600576 ALOGV("Skia's renderer set to %s", prop);
Cody Northrop1f00e172018-04-02 11:23:31 -0600577 cnx->useAngle = true;
578 // Find and load vendor libEGL for ANGLE
579 if (!cnx->vendorEGL) {
580 cnx->vendorEGL = load_system_driver("EGL");
581 }
582 return so;
Ian Elliottb058aff2018-08-09 12:00:55 -0600583 } else {
Ian Elliott34474472018-09-05 10:57:07 -0600584 ALOGV("Loaded native %s library for %s (instead of ANGLE)",
Ian Elliottb058aff2018-08-09 12:00:55 -0600585 kind, app_name ? app_name : "nullptr");
Cody Northrop1f00e172018-04-02 11:23:31 -0600586 }
587
588 return nullptr;
589}
590
Mathias Agopian311b4792017-02-28 15:00:49 -0800591static const char* HAL_SUBNAME_KEY_PROPERTIES[2] = {
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800592 "ro.hardware.egl",
593 "ro.board.platform",
Mathias Agopian311b4792017-02-28 15:00:49 -0800594};
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800595
596static void* load_updated_driver(const char* kind, android_namespace_t* ns) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800597 ATRACE_CALL();
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800598 const android_dlextinfo dlextinfo = {
599 .flags = ANDROID_DLEXT_USE_NAMESPACE,
600 .library_namespace = ns,
601 };
602 void* so = nullptr;
603 char prop[PROPERTY_VALUE_MAX + 1];
604 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
605 if (property_get(key, prop, nullptr) > 0) {
Mathias Agopian65421432017-03-08 11:49:05 -0800606 std::string name = std::string("lib") + kind + "_" + prop + ".so";
607 so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
Mathias Agopian311b4792017-02-28 15:00:49 -0800608 if (so) {
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800609 return so;
Mathias Agopian311b4792017-02-28 15:00:49 -0800610 }
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800611 }
612 }
613 return nullptr;
614}
615
616void *Loader::load_driver(const char* kind,
617 egl_connection_t* cnx, uint32_t mask)
618{
Jesse Hall1508ae62017-01-19 17:43:26 -0800619 ATRACE_CALL();
620
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800621 void* dso = nullptr;
Cody Northrop6d082ef2018-09-11 09:42:31 -0600622 android_namespace_t* ns = android_getAngleNamespace();
623 if (ns) {
624 dso = load_angle(kind, ns, cnx);
625 }
Jiyong Parka243e5d2017-06-21 12:26:51 +0900626#ifndef __ANDROID_VNDK__
Cody Northrop1f00e172018-04-02 11:23:31 -0600627 if (!dso) {
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600628 android_namespace_t* ns = android_getDriverNamespace();
Cody Northrop1f00e172018-04-02 11:23:31 -0600629 if (ns) {
630 dso = load_updated_driver(kind, ns);
631 }
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800632 }
Jiyong Parka243e5d2017-06-21 12:26:51 +0900633#endif
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800634 if (!dso) {
635 dso = load_system_driver(kind);
636 if (!dso)
Yi Kong48a6cd22018-07-18 10:07:09 -0700637 return nullptr;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800638 }
639
Mathias Agopiande586972009-05-28 17:39:03 -0700640 if (mask & EGL) {
641 getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress");
642
Jesse Hall94cdba92013-07-11 09:40:35 -0700643 ALOGE_IF(!getProcAddress,
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800644 "can't find eglGetProcAddress() in EGL driver library");
Mathias Agopiande586972009-05-28 17:39:03 -0700645
Mathias Agopian618fa102009-10-14 02:06:37 -0700646 egl_t* egl = &cnx->egl;
Mathias Agopiande586972009-05-28 17:39:03 -0700647 __eglMustCastToProperFunctionPointerType* curr =
648 (__eglMustCastToProperFunctionPointerType*)egl;
649 char const * const * api = egl_names;
650 while (*api) {
651 char const * name = *api;
Jesse Hall94cdba92013-07-11 09:40:35 -0700652 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700653 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700654 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700655 // couldn't find the entry-point, use eglGetProcAddress()
656 f = getProcAddress(name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700657 if (f == nullptr) {
658 f = (__eglMustCastToProperFunctionPointerType)nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700659 }
660 }
661 *curr++ = f;
662 api++;
663 }
664 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700665
Mathias Agopiande586972009-05-28 17:39:03 -0700666 if (mask & GLESv1_CM) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700667 init_api(dso, gl_names_1, gl_names,
Mathias Agopian618fa102009-10-14 02:06:37 -0700668 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800669 &cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl,
Mathias Agopian618fa102009-10-14 02:06:37 -0700670 getProcAddress);
Mathias Agopiande586972009-05-28 17:39:03 -0700671 }
672
673 if (mask & GLESv2) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700674 init_api(dso, gl_names, nullptr,
Mathias Agopian618fa102009-10-14 02:06:37 -0700675 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800676 &cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl,
Mathias Agopiande586972009-05-28 17:39:03 -0700677 getProcAddress);
678 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700679
Mathias Agopiande586972009-05-28 17:39:03 -0700680 return dso;
681}
682
683// ----------------------------------------------------------------------------
684}; // namespace android
685// ----------------------------------------------------------------------------