blob: 6624976c4f940d63f2d022a4b37354b5c41a4d1b [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:
45 typedef enum ANGLEPreference {
46 ANGLE_NO_PREFERENCE = 0,
47 ANGLE_PREFER_NATIVE = 1,
48 ANGLE_PREFER_ANGLE = 2,
49 } ANGLEPreference;
50
51 // TODO(ianelliott@): Get this from an ANGLE header:
52 typedef bool (*fpANGLEUseForApplication)(const char* appName, const char* deviceMfr,
53 const char* deviceModel, ANGLEPreference developerOption,
54 ANGLEPreference appPreference);
Ian Elliott380bf852018-10-17 14:56:04 -060055
56 // TODO(ianelliott@): Get this from an ANGLE header:
57 typedef bool (*fpANGLEGetUtilityAPI)(unsigned int* versionToUse);
58
59 // TODO(ianelliott@): Get this from an ANGLE header:
60 typedef bool (*fpAndroidUseANGLEForApplication)(int fd, long offset, long length,
61 const char* appName, const char* deviceMfr,
62 const char* deviceModel);
Jiyong Park5910dc72017-04-05 14:23:52 +090063}
64
Mathias Agopiande586972009-05-28 17:39:03 -070065// ----------------------------------------------------------------------------
66namespace android {
67// ----------------------------------------------------------------------------
68
69
70/*
Mathias Agopian99381422013-04-23 20:52:29 +020071 * EGL userspace drivers must be provided either:
72 * - as a single library:
73 * /vendor/lib/egl/libGLES.so
74 *
75 * - as separate libraries:
76 * /vendor/lib/egl/libEGL.so
77 * /vendor/lib/egl/libGLESv1_CM.so
78 * /vendor/lib/egl/libGLESv2.so
79 *
80 * The software renderer for the emulator must be provided as a single
81 * library at:
82 *
83 * /system/lib/egl/libGLES_android.so
84 *
85 *
86 * For backward compatibility and to facilitate the transition to
87 * this new naming scheme, the loader will additionally look for:
Jesse Hall94cdba92013-07-11 09:40:35 -070088 *
Mathias Agopian99381422013-04-23 20:52:29 +020089 * /{vendor|system}/lib/egl/lib{GLES | [EGL|GLESv1_CM|GLESv2]}_*.so
Jesse Hall94cdba92013-07-11 09:40:35 -070090 *
Mathias Agopiande586972009-05-28 17:39:03 -070091 */
92
Mathias Agopian65421432017-03-08 11:49:05 -080093Loader& Loader::getInstance() {
94 static Loader loader;
95 return loader;
96}
Mathias Agopiande586972009-05-28 17:39:03 -070097
David 'Digit' Turner80b30c22011-08-26 17:38:47 +020098/* This function is called to check whether we run inside the emulator,
99 * and if this is the case whether GLES GPU emulation is supported.
100 *
101 * Returned values are:
102 * -1 -> not running inside the emulator
103 * 0 -> running inside the emulator, but GPU emulation not supported
104 * 1 -> running inside the emulator, GPU emulation is supported
Nicolas Capens776951f2015-11-06 10:10:21 -0500105 * through the "emulation" host-side OpenGL ES implementation.
106 * 2 -> running inside the emulator, GPU emulation is supported
107 * through a guest-side vendor driver's OpenGL ES implementation.
David 'Digit' Turner80b30c22011-08-26 17:38:47 +0200108 */
109static int
110checkGlesEmulationStatus(void)
111{
112 /* We're going to check for the following kernel parameters:
113 *
114 * qemu=1 -> tells us that we run inside the emulator
115 * android.qemu.gles=<number> -> tells us the GLES GPU emulation status
116 *
117 * Note that we will return <number> if we find it. This let us support
118 * more additionnal emulation modes in the future.
119 */
120 char prop[PROPERTY_VALUE_MAX];
121 int result = -1;
122
123 /* First, check for qemu=1 */
124 property_get("ro.kernel.qemu",prop,"0");
125 if (atoi(prop) != 1)
126 return -1;
127
128 /* We are in the emulator, get GPU status value */
bohu69e5b1a2016-02-19 17:15:20 -0800129 property_get("qemu.gles",prop,"0");
David 'Digit' Turner80b30c22011-08-26 17:38:47 +0200130 return atoi(prop);
131}
132
Jesse Hall1508ae62017-01-19 17:43:26 -0800133static void* do_dlopen(const char* path, int mode) {
134 ATRACE_CALL();
135 return dlopen(path, mode);
136}
137
Jiyong Park5910dc72017-04-05 14:23:52 +0900138static void* do_android_dlopen_ext(const char* path, int mode, const android_dlextinfo* info) {
139 ATRACE_CALL();
140 return android_dlopen_ext(path, mode, info);
141}
142
Justin Yunb7320302017-05-22 15:13:40 +0900143static void* do_android_load_sphal_library(const char* path, int mode) {
144 ATRACE_CALL();
145 return android_load_sphal_library(path, mode);
146}
147
Mathias Agopiande586972009-05-28 17:39:03 -0700148// ----------------------------------------------------------------------------
149
Jesse Hall94cdba92013-07-11 09:40:35 -0700150Loader::driver_t::driver_t(void* gles)
Mathias Agopiande586972009-05-28 17:39:03 -0700151{
152 dso[0] = gles;
153 for (size_t i=1 ; i<NELEM(dso) ; i++)
Yi Kong48a6cd22018-07-18 10:07:09 -0700154 dso[i] = nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700155}
156
Jesse Hall94cdba92013-07-11 09:40:35 -0700157Loader::driver_t::~driver_t()
Mathias Agopiande586972009-05-28 17:39:03 -0700158{
159 for (size_t i=0 ; i<NELEM(dso) ; i++) {
160 if (dso[i]) {
161 dlclose(dso[i]);
Yi Kong48a6cd22018-07-18 10:07:09 -0700162 dso[i] = nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700163 }
164 }
165}
166
Mathias Agopian65421432017-03-08 11:49:05 -0800167int Loader::driver_t::set(void* hnd, int32_t api)
Mathias Agopiande586972009-05-28 17:39:03 -0700168{
169 switch (api) {
170 case EGL:
171 dso[0] = hnd;
172 break;
173 case GLESv1_CM:
174 dso[1] = hnd;
175 break;
176 case GLESv2:
177 dso[2] = hnd;
178 break;
179 default:
Mathias Agopian65421432017-03-08 11:49:05 -0800180 return -EOVERFLOW;
Mathias Agopiande586972009-05-28 17:39:03 -0700181 }
Mathias Agopian65421432017-03-08 11:49:05 -0800182 return 0;
Mathias Agopiande586972009-05-28 17:39:03 -0700183}
184
185// ----------------------------------------------------------------------------
186
Mathias Agopiande586972009-05-28 17:39:03 -0700187Loader::Loader()
Yi Kong48a6cd22018-07-18 10:07:09 -0700188 : getProcAddress(nullptr)
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800189{
Mathias Agopiande586972009-05-28 17:39:03 -0700190}
191
Mathias Agopian99381422013-04-23 20:52:29 +0200192Loader::~Loader() {
Mathias Agopiande586972009-05-28 17:39:03 -0700193}
194
Jesse Hallc07b5202013-07-04 12:08:16 -0700195static void* load_wrapper(const char* path) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800196 void* so = do_dlopen(path, RTLD_NOW | RTLD_LOCAL);
Jesse Hallc07b5202013-07-04 12:08:16 -0700197 ALOGE_IF(!so, "dlopen(\"%s\") failed: %s", path, dlerror());
198 return so;
199}
200
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700201#ifndef EGL_WRAPPER_DIR
202#if defined(__LP64__)
203#define EGL_WRAPPER_DIR "/system/lib64"
204#else
205#define EGL_WRAPPER_DIR "/system/lib"
206#endif
207#endif
208
bohu69e5b1a2016-02-19 17:15:20 -0800209static void setEmulatorGlesValue(void) {
210 char prop[PROPERTY_VALUE_MAX];
211 property_get("ro.kernel.qemu", prop, "0");
212 if (atoi(prop) != 1) return;
213
214 property_get("ro.kernel.qemu.gles",prop,"0");
215 if (atoi(prop) == 1) {
216 ALOGD("Emulator has host GPU support, qemu.gles is set to 1.");
217 property_set("qemu.gles", "1");
218 return;
219 }
220
221 // for now, checking the following
222 // directory is good enough for emulator system images
223 const char* vendor_lib_path =
224#if defined(__LP64__)
225 "/vendor/lib64/egl";
226#else
227 "/vendor/lib/egl";
228#endif
229
230 const bool has_vendor_lib = (access(vendor_lib_path, R_OK) == 0);
231 if (has_vendor_lib) {
232 ALOGD("Emulator has vendor provided software renderer, qemu.gles is set to 2.");
233 property_set("qemu.gles", "2");
234 } else {
235 ALOGD("Emulator without GPU support detected. "
236 "Fallback to legacy software renderer, qemu.gles is set to 0.");
237 property_set("qemu.gles", "0");
238 }
239}
240
Mathias Agopianada798b2012-02-13 17:09:30 -0800241void* Loader::open(egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700242{
Jesse Hall1508ae62017-01-19 17:43:26 -0800243 ATRACE_CALL();
244
Mathias Agopiande586972009-05-28 17:39:03 -0700245 void* dso;
Yi Kong48a6cd22018-07-18 10:07:09 -0700246 driver_t* hnd = nullptr;
Jesse Hall94cdba92013-07-11 09:40:35 -0700247
bohu69e5b1a2016-02-19 17:15:20 -0800248 setEmulatorGlesValue();
249
Cody Northrop9d5d33d2018-10-15 18:32:14 -0600250 dso = load_driver("GLES", cnx, EGL | GLESv1_CM | GLESv2);
Mathias Agopian99381422013-04-23 20:52:29 +0200251 if (dso) {
252 hnd = new driver_t(dso);
253 } else {
254 // Always load EGL first
Cody Northrop9d5d33d2018-10-15 18:32:14 -0600255 dso = load_driver("EGL", cnx, EGL);
Mathias Agopiande586972009-05-28 17:39:03 -0700256 if (dso) {
257 hnd = new driver_t(dso);
Mathias Agopian99381422013-04-23 20:52:29 +0200258 hnd->set( load_driver("GLESv1_CM", cnx, GLESv1_CM), GLESv1_CM );
259 hnd->set( load_driver("GLESv2", cnx, GLESv2), GLESv2 );
Mathias Agopiande586972009-05-28 17:39:03 -0700260 }
261 }
262
Mathias Agopian99381422013-04-23 20:52:29 +0200263 LOG_ALWAYS_FATAL_IF(!hnd, "couldn't find an OpenGL ES implementation");
Jesse Hallc07b5202013-07-04 12:08:16 -0700264
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700265 cnx->libEgl = load_wrapper(EGL_WRAPPER_DIR "/libEGL.so");
266 cnx->libGles2 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv2.so");
267 cnx->libGles1 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv1_CM.so");
268
Michael Chockc0ec5e22014-01-27 08:14:33 -0800269 LOG_ALWAYS_FATAL_IF(!cnx->libEgl,
270 "couldn't load system EGL wrapper libraries");
271
Jesse Hallc07b5202013-07-04 12:08:16 -0700272 LOG_ALWAYS_FATAL_IF(!cnx->libGles2 || !cnx->libGles1,
273 "couldn't load system OpenGL ES wrapper libraries");
274
Mathias Agopiande586972009-05-28 17:39:03 -0700275 return (void*)hnd;
276}
277
Mathias Agopian65421432017-03-08 11:49:05 -0800278void Loader::close(void* driver)
Mathias Agopiande586972009-05-28 17:39:03 -0700279{
280 driver_t* hnd = (driver_t*)driver;
281 delete hnd;
Mathias Agopiande586972009-05-28 17:39:03 -0700282}
283
Jesse Hall94cdba92013-07-11 09:40:35 -0700284void Loader::init_api(void* dso,
285 char const * const * api,
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700286 char const * const * ref_api,
Jesse Hall94cdba92013-07-11 09:40:35 -0700287 __eglMustCastToProperFunctionPointerType* curr,
288 getProcAddressType getProcAddress)
Mathias Agopiande586972009-05-28 17:39:03 -0700289{
Jesse Hall1508ae62017-01-19 17:43:26 -0800290 ATRACE_CALL();
291
Mathias Agopian7773c432012-02-13 20:06:08 -0800292 const ssize_t SIZE = 256;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700293 char scrap[SIZE];
Mathias Agopiande586972009-05-28 17:39:03 -0700294 while (*api) {
295 char const * name = *api;
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700296 if (ref_api) {
297 char const * ref_name = *ref_api;
298 if (std::strcmp(name, ref_name) != 0) {
299 *curr++ = nullptr;
300 ref_api++;
301 continue;
302 }
303 }
304
Jesse Hall94cdba92013-07-11 09:40:35 -0700305 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700306 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700307 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700308 // couldn't find the entry-point, use eglGetProcAddress()
309 f = getProcAddress(name);
310 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700311 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700312 // Try without the OES postfix
313 ssize_t index = ssize_t(strlen(name)) - 3;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700314 if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) {
Mathias Agopiande586972009-05-28 17:39:03 -0700315 strncpy(scrap, name, index);
316 scrap[index] = 0;
317 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) {
Mathias Agopiande586972009-05-28 17:39:03 -0700322 // Try with the OES postfix
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700323 ssize_t index = ssize_t(strlen(name)) - 3;
324 if (index>0 && strcmp(name+index, "OES")) {
325 snprintf(scrap, SIZE, "%sOES", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700326 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000327 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700328 }
329 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700330 if (f == nullptr) {
Steve Block9d453682011-12-20 16:23:08 +0000331 //ALOGD("%s", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700332 f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented;
Mathias Agopian48d438d2012-01-28 21:44:00 -0800333
334 /*
335 * GL_EXT_debug_label is special, we always report it as
336 * supported, it's handled by GLES_trace. If GLES_trace is not
337 * enabled, then these are no-ops.
338 */
339 if (!strcmp(name, "glInsertEventMarkerEXT")) {
340 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
341 } else if (!strcmp(name, "glPushGroupMarkerEXT")) {
342 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
343 } else if (!strcmp(name, "glPopGroupMarkerEXT")) {
344 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
345 }
Mathias Agopiande586972009-05-28 17:39:03 -0700346 }
347 *curr++ = f;
348 api++;
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700349 if (ref_api) ref_api++;
Mathias Agopiande586972009-05-28 17:39:03 -0700350 }
351}
352
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800353static void* load_system_driver(const char* kind) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800354 ATRACE_CALL();
Mathias Agopian99381422013-04-23 20:52:29 +0200355 class MatchFile {
356 public:
Mathias Agopian65421432017-03-08 11:49:05 -0800357 static std::string find(const char* kind) {
358 std::string result;
Nicolas Capens776951f2015-11-06 10:10:21 -0500359 int emulationStatus = checkGlesEmulationStatus();
360 switch (emulationStatus) {
361 case 0:
Nicolas Capens776951f2015-11-06 10:10:21 -0500362#if defined(__LP64__)
bohu3adf9e12017-07-02 22:08:13 -0700363 result = "/vendor/lib64/egl/libGLES_android.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500364#else
bohu3adf9e12017-07-02 22:08:13 -0700365 result = "/vendor/lib/egl/libGLES_android.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500366#endif
367 return result;
368 case 1:
369 // Use host-side OpenGL through the "emulation" library
370#if defined(__LP64__)
bohu3adf9e12017-07-02 22:08:13 -0700371 result = std::string("/vendor/lib64/egl/lib") + kind + "_emulation.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500372#else
bohu3adf9e12017-07-02 22:08:13 -0700373 result = std::string("/vendor/lib/egl/lib") + kind + "_emulation.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500374#endif
375 return result;
Lingfeng Yang0f82d872017-08-11 01:18:00 -0700376 case 2:
377 // Use guest side swiftshader library
378#if defined(__LP64__)
379 result = std::string("/vendor/lib64/egl/lib") + kind + "_swiftshader.so";
380#else
381 result = std::string("/vendor/lib/egl/lib") + kind + "_swiftshader.so";
382#endif
383 return result;
Nicolas Capens776951f2015-11-06 10:10:21 -0500384 default:
385 // Not in emulator, or use other guest-side implementation
386 break;
387 }
388
Mathias Agopian65421432017-03-08 11:49:05 -0800389 std::string pattern = std::string("lib") + kind;
Mathias Agopian99381422013-04-23 20:52:29 +0200390 const char* const searchPaths[] = {
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800391#if defined(__LP64__)
392 "/vendor/lib64/egl",
393 "/system/lib64/egl"
394#else
Mathias Agopian99381422013-04-23 20:52:29 +0200395 "/vendor/lib/egl",
396 "/system/lib/egl"
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800397#endif
Mathias Agopian99381422013-04-23 20:52:29 +0200398 };
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700399
Mathias Agopian99381422013-04-23 20:52:29 +0200400 // first, we search for the exact name of the GLES userspace
401 // driver in both locations.
402 // i.e.:
403 // libGLES.so, or:
404 // libEGL.so, libGLESv1_CM.so, libGLESv2.so
405
406 for (size_t i=0 ; i<NELEM(searchPaths) ; i++) {
407 if (find(result, pattern, searchPaths[i], true)) {
408 return result;
409 }
410 }
411
412 // for compatibility with the old "egl.cfg" naming convention
413 // we look for files that match:
414 // libGLES_*.so, or:
415 // libEGL_*.so, libGLESv1_CM_*.so, libGLESv2_*.so
416
417 pattern.append("_");
418 for (size_t i=0 ; i<NELEM(searchPaths) ; i++) {
419 if (find(result, pattern, searchPaths[i], false)) {
420 return result;
421 }
422 }
423
424 // we didn't find the driver. gah.
425 result.clear();
426 return result;
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700427 }
Mathias Agopian99381422013-04-23 20:52:29 +0200428
429 private:
Mathias Agopian65421432017-03-08 11:49:05 -0800430 static bool find(std::string& result,
431 const std::string& pattern, const char* const search, bool exact) {
Mathias Agopian99381422013-04-23 20:52:29 +0200432 if (exact) {
Jesse Hall4aaa9602017-08-29 16:33:54 -0700433 std::string absolutePath = std::string(search) + "/" + pattern + ".so";
Mathias Agopian65421432017-03-08 11:49:05 -0800434 if (!access(absolutePath.c_str(), R_OK)) {
Mathias Agopian99381422013-04-23 20:52:29 +0200435 result = absolutePath;
436 return true;
437 }
438 return false;
439 }
440
441 DIR* d = opendir(search);
Yi Kong48a6cd22018-07-18 10:07:09 -0700442 if (d != nullptr) {
Mathias Agopian99381422013-04-23 20:52:29 +0200443 struct dirent* e;
Yi Kong48a6cd22018-07-18 10:07:09 -0700444 while ((e = readdir(d)) != nullptr) {
Mathias Agopian99381422013-04-23 20:52:29 +0200445 if (e->d_type == DT_DIR) {
446 continue;
447 }
448 if (!strcmp(e->d_name, "libGLES_android.so")) {
449 // always skip the software renderer
450 continue;
451 }
Mathias Agopian65421432017-03-08 11:49:05 -0800452 if (strstr(e->d_name, pattern.c_str()) == e->d_name) {
Mathias Agopian99381422013-04-23 20:52:29 +0200453 if (!strcmp(e->d_name + strlen(e->d_name) - 3, ".so")) {
Mathias Agopian65421432017-03-08 11:49:05 -0800454 result = std::string(search) + "/" + e->d_name;
Mathias Agopian99381422013-04-23 20:52:29 +0200455 closedir(d);
456 return true;
457 }
458 }
459 }
460 closedir(d);
461 }
462 return false;
463 }
464 };
465
466
Mathias Agopian65421432017-03-08 11:49:05 -0800467 std::string absolutePath = MatchFile::find(kind);
468 if (absolutePath.empty()) {
Mathias Agopian99381422013-04-23 20:52:29 +0200469 // this happens often, we don't want to log an error
Yi Kong48a6cd22018-07-18 10:07:09 -0700470 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700471 }
Mathias Agopian65421432017-03-08 11:49:05 -0800472 const char* const driver_absolute_path = absolutePath.c_str();
Mathias Agopiande586972009-05-28 17:39:03 -0700473
Jiyong Park5910dc72017-04-05 14:23:52 +0900474 // Try to load drivers from the 'sphal' namespace, if it exist. Fall back to
Justin Yunb7320302017-05-22 15:13:40 +0900475 // the original routine when the namespace does not exist.
Jiyong Park5910dc72017-04-05 14:23:52 +0900476 // See /system/core/rootdir/etc/ld.config.txt for the configuration of the
477 // sphal namespace.
Justin Yunb7320302017-05-22 15:13:40 +0900478 void* dso = do_android_load_sphal_library(driver_absolute_path,
479 RTLD_NOW | RTLD_LOCAL);
Yi Kong48a6cd22018-07-18 10:07:09 -0700480 if (dso == nullptr) {
Mathias Agopian8c173842009-09-20 16:01:02 -0700481 const char* err = dlerror();
Mathias Agopian65421432017-03-08 11:49:05 -0800482 ALOGE("load_driver(%s): %s", driver_absolute_path, err ? err : "unknown");
Yi Kong48a6cd22018-07-18 10:07:09 -0700483 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700484 }
485
Steve Block9d453682011-12-20 16:23:08 +0000486 ALOGD("loaded %s", driver_absolute_path);
Mathias Agopianbaca89c2009-08-20 19:09:34 -0700487
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800488 return dso;
489}
490
Cody Northrop1f00e172018-04-02 11:23:31 -0600491static void* load_angle_from_namespace(const char* kind, android_namespace_t* ns) {
492 const android_dlextinfo dlextinfo = {
493 .flags = ANDROID_DLEXT_USE_NAMESPACE,
494 .library_namespace = ns,
495 };
496
497 std::string name = std::string("lib") + kind + "_angle.so";
498
499 void* so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
500
501 if (so) {
502 ALOGD("dlopen_ext from APK (%s) success at %p", name.c_str(), so);
503 return so;
504 } else {
505 ALOGE("dlopen_ext(\"%s\") failed: %s", name.c_str(), dlerror());
506 }
507
508 return nullptr;
509}
510
Ian Elliott34474472018-09-05 10:57:07 -0600511static ANGLEPreference getAnglePref(const char* app_pref) {
512 if (app_pref == nullptr)
513 return ANGLE_NO_PREFERENCE;
514
515 if (strcmp(app_pref, "angle") == 0) {
516 return ANGLE_PREFER_ANGLE;
517 } else if (strcmp(app_pref, "native") == 0) {
518 return ANGLE_PREFER_NATIVE;
519 }
520 return ANGLE_NO_PREFERENCE;
521}
522
Cody Northrop6d082ef2018-09-11 09:42:31 -0600523static void* load_angle(const char* kind, android_namespace_t* ns, egl_connection_t* cnx) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600524 // Only attempt to load ANGLE libs
525 if (strcmp(kind, "EGL") != 0 && strcmp(kind, "GLESv2") != 0 && strcmp(kind, "GLESv1_CM") != 0)
526 return nullptr;
527
528 void* so = nullptr;
529 std::string name;
Ian Elliottb058aff2018-08-09 12:00:55 -0600530 char prop[PROPERTY_VALUE_MAX];
Cody Northrop1f00e172018-04-02 11:23:31 -0600531
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600532 const char* app_name = android_getAngleAppName();
Cody Northrop49d51992018-08-29 16:37:09 -0600533 const char* app_pref = android_getAngleAppPref();
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600534 bool developer_opt_in = android_getAngleDeveloperOptIn();
Cody Northrop04e70432018-09-06 10:34:58 -0600535 const int rules_fd = android_getAngleRulesFd();
536 const long rules_offset = android_getAngleRulesOffset();
537 const long rules_length = android_getAngleRulesLength();
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600538
Ian Elliott34474472018-09-05 10:57:07 -0600539 // Determine whether or not to use ANGLE:
540 ANGLEPreference developer_option = developer_opt_in ? ANGLE_PREFER_ANGLE : ANGLE_NO_PREFERENCE;
541 bool use_angle = (developer_option == ANGLE_PREFER_ANGLE);
542
543 if (use_angle) {
544 ALOGV("User set \"Developer Options\" to force the use of ANGLE");
Courtney Goeltzenleuchter734a4852018-10-23 16:26:24 -0600545 } else if (cnx->angleDecided) {
546 use_angle = cnx->useAngle;
Ian Elliott34474472018-09-05 10:57:07 -0600547 } else {
548 // The "Developer Options" value wasn't set to force the use of ANGLE. Need to temporarily
549 // load ANGLE and call the updatable opt-in/out logic:
550 std::string app_name_str = app_name ? app_name : "";
551 char manufacturer[PROPERTY_VALUE_MAX];
552 char model[PROPERTY_VALUE_MAX];
553 property_get("ro.product.manufacturer", manufacturer, "UNSET");
554 property_get("ro.product.model", model, "UNSET");
Courtney Goeltzenleuchter28b97dc2018-10-24 17:06:35 -0600555
556 // Check if ANGLE is enabled. Workaround for b/118375731
557 // We suspect that loading & unloading a library somehow corrupts
558 // the process.
559 property_get("debug.angle.enable", prop, "0");
560 if (atoi(prop)) {
561 so = load_angle_from_namespace("feature_support", ns);
562 }
Ian Elliott34474472018-09-05 10:57:07 -0600563 if (so) {
564 ALOGV("Temporarily loaded ANGLE's opt-in/out logic from namespace");
Ian Elliott380bf852018-10-17 14:56:04 -0600565 bool use_version0_API = false;
566 bool use_version1_API = false;
567 fpANGLEGetUtilityAPI ANGLEGetUtilityAPI =
568 (fpANGLEGetUtilityAPI)dlsym(so, "ANGLEGetUtilityAPI");
569 if (ANGLEGetUtilityAPI) {
570 unsigned int versionToUse = 1;
571 if ((ANGLEGetUtilityAPI)(&versionToUse)) {
572 if (versionToUse == 1) {
573 use_version1_API = true;
574 } else {
575 use_version0_API = true;
576 }
577 }
Courtney Goeltzenleuchter075437f2018-10-08 19:35:31 -0600578 } else {
Ian Elliott380bf852018-10-17 14:56:04 -0600579 use_version0_API = true;
Ian Elliott34474472018-09-05 10:57:07 -0600580 }
Ian Elliott380bf852018-10-17 14:56:04 -0600581 if (use_version1_API) {
582 // Use the new version 1 API to determine if the
583 // application should use the ANGLE or the native driver.
584 fpAndroidUseANGLEForApplication AndroidUseANGLEForApplication =
585 (fpAndroidUseANGLEForApplication)dlsym(so, "AndroidUseANGLEForApplication");
586 if (AndroidUseANGLEForApplication) {
587 use_angle = (AndroidUseANGLEForApplication)(rules_fd, rules_offset,
588 rules_length, app_name_str.c_str(),
589 manufacturer, model);
590 } else {
591 ALOGW("Cannot find AndroidUseANGLEForApplication in library");
592 }
593 } else if (use_version0_API) {
594 // Use the old version 0 API to determine if the
595 // application should use the ANGLE or the native driver.
596 fpANGLEUseForApplication ANGLEUseForApplication =
597 (fpANGLEUseForApplication)dlsym(so, "ANGLEUseForApplication");
598 if (ANGLEUseForApplication) {
599 ANGLEPreference app_preference = getAnglePref(android_getAngleAppPref());
600 use_angle = (ANGLEUseForApplication)(app_name_str.c_str(), manufacturer, model,
601 developer_option, app_preference);
602 ALOGV("Result of opt-in/out logic is %s", use_angle ? "true" : "false");
603 } else {
604 ALOGW("Cannot find ANGLEUseForApplication in library");
605 }
606 }
Ian Elliott34474472018-09-05 10:57:07 -0600607 ALOGV("Close temporarily-loaded ANGLE opt-in/out logic");
608 dlclose(so);
609 so = nullptr;
610 } else {
611 // We weren't able to load and call the updateable opt-in/out logic.
612 // If we can't load the library, there is no ANGLE available.
613 use_angle = false;
614 ALOGV("Could not temporarily-load the ANGLE opt-in/out logic, cannot use ANGLE.");
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600615 }
Courtney Goeltzenleuchter734a4852018-10-23 16:26:24 -0600616 cnx->angleDecided = true;
Cody Northrop1f00e172018-04-02 11:23:31 -0600617 }
Ian Elliott34474472018-09-05 10:57:07 -0600618 if (use_angle) {
619 so = load_angle_from_namespace(kind, ns);
620 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600621
622 if (so) {
Ian Elliott34474472018-09-05 10:57:07 -0600623 ALOGV("Loaded ANGLE %s library for %s (instead of native)",
Ian Elliottb058aff2018-08-09 12:00:55 -0600624 kind, app_name ? app_name : "nullptr");
Ian Elliottb058aff2018-08-09 12:00:55 -0600625 property_get("debug.hwui.renderer", prop, "UNSET");
Ian Elliott34474472018-09-05 10:57:07 -0600626 ALOGV("Skia's renderer set to %s", prop);
Cody Northrop1f00e172018-04-02 11:23:31 -0600627 cnx->useAngle = true;
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600628
629 EGLint angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE;
630
631 char prop[PROPERTY_VALUE_MAX];
632 property_get("debug.angle.backend", prop, "0");
633 switch (atoi(prop)) {
634 case 1:
635 ALOGV("%s: Requesting OpenGLES back-end", __FUNCTION__);
636 angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE;
637 break;
638 case 2:
639 ALOGV("%s: Requesting Vulkan back-end", __FUNCTION__);
640 angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE;
641 break;
642 default:
643 break;
644 }
645
646 cnx->angleBackend = angleBackendDefault;
647 if (!cnx->vendorEGL && (cnx->angleBackend == EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE)) {
648 // Find and load vendor libEGL for ANGLE's GL back-end to use.
Cody Northrop1f00e172018-04-02 11:23:31 -0600649 cnx->vendorEGL = load_system_driver("EGL");
650 }
651 return so;
Ian Elliottb058aff2018-08-09 12:00:55 -0600652 } else {
Ian Elliott34474472018-09-05 10:57:07 -0600653 ALOGV("Loaded native %s library for %s (instead of ANGLE)",
Ian Elliottb058aff2018-08-09 12:00:55 -0600654 kind, app_name ? app_name : "nullptr");
Cody Northrop1f00e172018-04-02 11:23:31 -0600655 }
656
657 return nullptr;
658}
659
Mathias Agopian311b4792017-02-28 15:00:49 -0800660static const char* HAL_SUBNAME_KEY_PROPERTIES[2] = {
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800661 "ro.hardware.egl",
662 "ro.board.platform",
Mathias Agopian311b4792017-02-28 15:00:49 -0800663};
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800664
665static void* load_updated_driver(const char* kind, android_namespace_t* ns) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800666 ATRACE_CALL();
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800667 const android_dlextinfo dlextinfo = {
668 .flags = ANDROID_DLEXT_USE_NAMESPACE,
669 .library_namespace = ns,
670 };
671 void* so = nullptr;
672 char prop[PROPERTY_VALUE_MAX + 1];
673 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
674 if (property_get(key, prop, nullptr) > 0) {
Mathias Agopian65421432017-03-08 11:49:05 -0800675 std::string name = std::string("lib") + kind + "_" + prop + ".so";
676 so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
Mathias Agopian311b4792017-02-28 15:00:49 -0800677 if (so) {
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800678 return so;
Mathias Agopian311b4792017-02-28 15:00:49 -0800679 }
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800680 }
681 }
682 return nullptr;
683}
684
685void *Loader::load_driver(const char* kind,
686 egl_connection_t* cnx, uint32_t mask)
687{
Jesse Hall1508ae62017-01-19 17:43:26 -0800688 ATRACE_CALL();
689
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800690 void* dso = nullptr;
Cody Northrop6d082ef2018-09-11 09:42:31 -0600691 android_namespace_t* ns = android_getAngleNamespace();
692 if (ns) {
693 dso = load_angle(kind, ns, cnx);
694 }
Jiyong Parka243e5d2017-06-21 12:26:51 +0900695#ifndef __ANDROID_VNDK__
Cody Northrop1f00e172018-04-02 11:23:31 -0600696 if (!dso) {
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600697 android_namespace_t* ns = android_getDriverNamespace();
Cody Northrop1f00e172018-04-02 11:23:31 -0600698 if (ns) {
699 dso = load_updated_driver(kind, ns);
700 }
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800701 }
Jiyong Parka243e5d2017-06-21 12:26:51 +0900702#endif
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800703 if (!dso) {
704 dso = load_system_driver(kind);
705 if (!dso)
Yi Kong48a6cd22018-07-18 10:07:09 -0700706 return nullptr;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800707 }
708
Mathias Agopiande586972009-05-28 17:39:03 -0700709 if (mask & EGL) {
710 getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress");
711
Jesse Hall94cdba92013-07-11 09:40:35 -0700712 ALOGE_IF(!getProcAddress,
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800713 "can't find eglGetProcAddress() in EGL driver library");
Mathias Agopiande586972009-05-28 17:39:03 -0700714
Mathias Agopian618fa102009-10-14 02:06:37 -0700715 egl_t* egl = &cnx->egl;
Mathias Agopiande586972009-05-28 17:39:03 -0700716 __eglMustCastToProperFunctionPointerType* curr =
717 (__eglMustCastToProperFunctionPointerType*)egl;
718 char const * const * api = egl_names;
719 while (*api) {
720 char const * name = *api;
Jesse Hall94cdba92013-07-11 09:40:35 -0700721 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700722 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700723 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700724 // couldn't find the entry-point, use eglGetProcAddress()
725 f = getProcAddress(name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700726 if (f == nullptr) {
727 f = (__eglMustCastToProperFunctionPointerType)nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700728 }
729 }
730 *curr++ = f;
731 api++;
732 }
733 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700734
Mathias Agopiande586972009-05-28 17:39:03 -0700735 if (mask & GLESv1_CM) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700736 init_api(dso, gl_names_1, gl_names,
Mathias Agopian618fa102009-10-14 02:06:37 -0700737 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800738 &cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl,
Mathias Agopian618fa102009-10-14 02:06:37 -0700739 getProcAddress);
Mathias Agopiande586972009-05-28 17:39:03 -0700740 }
741
742 if (mask & GLESv2) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700743 init_api(dso, gl_names, nullptr,
Mathias Agopian618fa102009-10-14 02:06:37 -0700744 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800745 &cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl,
Mathias Agopiande586972009-05-28 17:39:03 -0700746 getProcAddress);
747 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700748
Mathias Agopiande586972009-05-28 17:39:03 -0700749 return dso;
750}
751
752// ----------------------------------------------------------------------------
753}; // namespace android
754// ----------------------------------------------------------------------------