blob: 6edadcd6ed997bb97f7d6f460319e14ceb82f03c [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
Ian Elliottb6125f52018-11-16 13:40:51 -070044 // TODO(ianelliott@): Get the following from an ANGLE header:
45 // Version-1 API:
Ian Elliott380bf852018-10-17 14:56:04 -060046 typedef bool (*fpANGLEGetUtilityAPI)(unsigned int* versionToUse);
Ian Elliott380bf852018-10-17 14:56:04 -060047 typedef bool (*fpAndroidUseANGLEForApplication)(int fd, long offset, long length,
48 const char* appName, const char* deviceMfr,
49 const char* deviceModel);
Ian Elliottb6125f52018-11-16 13:40:51 -070050 // Version-2 API:
51 typedef bool (*fpANGLEGetFeatureSupportUtilAPIVersion)(unsigned int* versionToUse);
52 typedef bool (*fpANGLEAndroidParseRulesString)(const char *rulesString,
53 void** rulesHandle, int* rulesVersion);
54 typedef bool (*fpANGLEGetSystemInfo)(void** handle);
55 typedef bool (*fpANGLEAddDeviceInfoToSystemInfo)(const char* deviceMfr,
56 const char* deviceModel,
57 void* handle);
58 typedef bool (*fpANGLEShouldBeUsedForApplication)(void* rules_handle,
59 int rules_version,
60 void* system_info_handle,
61 const char *appName);
62 typedef bool (*fpANGLEFreeRulesHandle)(void* handle);
63 typedef bool (*fpANGLEFreeSystemInfoHandle)(void* handle);
64
Jiyong Park5910dc72017-04-05 14:23:52 +090065}
66
Mathias Agopiande586972009-05-28 17:39:03 -070067// ----------------------------------------------------------------------------
68namespace android {
69// ----------------------------------------------------------------------------
70
71
72/*
Mathias Agopian99381422013-04-23 20:52:29 +020073 * EGL userspace drivers must be provided either:
74 * - as a single library:
75 * /vendor/lib/egl/libGLES.so
76 *
77 * - as separate libraries:
78 * /vendor/lib/egl/libEGL.so
79 * /vendor/lib/egl/libGLESv1_CM.so
80 * /vendor/lib/egl/libGLESv2.so
81 *
82 * The software renderer for the emulator must be provided as a single
83 * library at:
84 *
85 * /system/lib/egl/libGLES_android.so
86 *
87 *
88 * For backward compatibility and to facilitate the transition to
89 * this new naming scheme, the loader will additionally look for:
Jesse Hall94cdba92013-07-11 09:40:35 -070090 *
Mathias Agopian99381422013-04-23 20:52:29 +020091 * /{vendor|system}/lib/egl/lib{GLES | [EGL|GLESv1_CM|GLESv2]}_*.so
Jesse Hall94cdba92013-07-11 09:40:35 -070092 *
Mathias Agopiande586972009-05-28 17:39:03 -070093 */
94
Mathias Agopian65421432017-03-08 11:49:05 -080095Loader& Loader::getInstance() {
96 static Loader loader;
97 return loader;
98}
Mathias Agopiande586972009-05-28 17:39:03 -070099
David 'Digit' Turner80b30c22011-08-26 17:38:47 +0200100/* This function is called to check whether we run inside the emulator,
101 * and if this is the case whether GLES GPU emulation is supported.
102 *
103 * Returned values are:
104 * -1 -> not running inside the emulator
105 * 0 -> running inside the emulator, but GPU emulation not supported
106 * 1 -> running inside the emulator, GPU emulation is supported
Nicolas Capens776951f2015-11-06 10:10:21 -0500107 * through the "emulation" host-side OpenGL ES implementation.
108 * 2 -> running inside the emulator, GPU emulation is supported
109 * through a guest-side vendor driver's OpenGL ES implementation.
David 'Digit' Turner80b30c22011-08-26 17:38:47 +0200110 */
111static int
112checkGlesEmulationStatus(void)
113{
114 /* We're going to check for the following kernel parameters:
115 *
116 * qemu=1 -> tells us that we run inside the emulator
117 * android.qemu.gles=<number> -> tells us the GLES GPU emulation status
118 *
119 * Note that we will return <number> if we find it. This let us support
120 * more additionnal emulation modes in the future.
121 */
122 char prop[PROPERTY_VALUE_MAX];
123 int result = -1;
124
125 /* First, check for qemu=1 */
126 property_get("ro.kernel.qemu",prop,"0");
127 if (atoi(prop) != 1)
128 return -1;
129
130 /* We are in the emulator, get GPU status value */
bohu69e5b1a2016-02-19 17:15:20 -0800131 property_get("qemu.gles",prop,"0");
David 'Digit' Turner80b30c22011-08-26 17:38:47 +0200132 return atoi(prop);
133}
134
Jesse Hall1508ae62017-01-19 17:43:26 -0800135static void* do_dlopen(const char* path, int mode) {
136 ATRACE_CALL();
137 return dlopen(path, mode);
138}
139
Jiyong Park5910dc72017-04-05 14:23:52 +0900140static void* do_android_dlopen_ext(const char* path, int mode, const android_dlextinfo* info) {
141 ATRACE_CALL();
142 return android_dlopen_ext(path, mode, info);
143}
144
Justin Yunb7320302017-05-22 15:13:40 +0900145static void* do_android_load_sphal_library(const char* path, int mode) {
146 ATRACE_CALL();
147 return android_load_sphal_library(path, mode);
148}
149
Mathias Agopiande586972009-05-28 17:39:03 -0700150// ----------------------------------------------------------------------------
151
Jesse Hall94cdba92013-07-11 09:40:35 -0700152Loader::driver_t::driver_t(void* gles)
Mathias Agopiande586972009-05-28 17:39:03 -0700153{
154 dso[0] = gles;
155 for (size_t i=1 ; i<NELEM(dso) ; i++)
Yi Kong48a6cd22018-07-18 10:07:09 -0700156 dso[i] = nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700157}
158
Jesse Hall94cdba92013-07-11 09:40:35 -0700159Loader::driver_t::~driver_t()
Mathias Agopiande586972009-05-28 17:39:03 -0700160{
161 for (size_t i=0 ; i<NELEM(dso) ; i++) {
162 if (dso[i]) {
163 dlclose(dso[i]);
Yi Kong48a6cd22018-07-18 10:07:09 -0700164 dso[i] = nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700165 }
166 }
167}
168
Mathias Agopian65421432017-03-08 11:49:05 -0800169int Loader::driver_t::set(void* hnd, int32_t api)
Mathias Agopiande586972009-05-28 17:39:03 -0700170{
171 switch (api) {
172 case EGL:
173 dso[0] = hnd;
174 break;
175 case GLESv1_CM:
176 dso[1] = hnd;
177 break;
178 case GLESv2:
179 dso[2] = hnd;
180 break;
181 default:
Mathias Agopian65421432017-03-08 11:49:05 -0800182 return -EOVERFLOW;
Mathias Agopiande586972009-05-28 17:39:03 -0700183 }
Mathias Agopian65421432017-03-08 11:49:05 -0800184 return 0;
Mathias Agopiande586972009-05-28 17:39:03 -0700185}
186
187// ----------------------------------------------------------------------------
188
Mathias Agopiande586972009-05-28 17:39:03 -0700189Loader::Loader()
Yi Kong48a6cd22018-07-18 10:07:09 -0700190 : getProcAddress(nullptr)
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800191{
Mathias Agopiande586972009-05-28 17:39:03 -0700192}
193
Mathias Agopian99381422013-04-23 20:52:29 +0200194Loader::~Loader() {
Mathias Agopiande586972009-05-28 17:39:03 -0700195}
196
Jesse Hallc07b5202013-07-04 12:08:16 -0700197static void* load_wrapper(const char* path) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800198 void* so = do_dlopen(path, RTLD_NOW | RTLD_LOCAL);
Jesse Hallc07b5202013-07-04 12:08:16 -0700199 ALOGE_IF(!so, "dlopen(\"%s\") failed: %s", path, dlerror());
200 return so;
201}
202
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700203#ifndef EGL_WRAPPER_DIR
204#if defined(__LP64__)
205#define EGL_WRAPPER_DIR "/system/lib64"
206#else
207#define EGL_WRAPPER_DIR "/system/lib"
208#endif
209#endif
210
bohu69e5b1a2016-02-19 17:15:20 -0800211static void setEmulatorGlesValue(void) {
212 char prop[PROPERTY_VALUE_MAX];
213 property_get("ro.kernel.qemu", prop, "0");
214 if (atoi(prop) != 1) return;
215
216 property_get("ro.kernel.qemu.gles",prop,"0");
217 if (atoi(prop) == 1) {
218 ALOGD("Emulator has host GPU support, qemu.gles is set to 1.");
219 property_set("qemu.gles", "1");
220 return;
221 }
222
223 // for now, checking the following
224 // directory is good enough for emulator system images
225 const char* vendor_lib_path =
226#if defined(__LP64__)
227 "/vendor/lib64/egl";
228#else
229 "/vendor/lib/egl";
230#endif
231
232 const bool has_vendor_lib = (access(vendor_lib_path, R_OK) == 0);
233 if (has_vendor_lib) {
234 ALOGD("Emulator has vendor provided software renderer, qemu.gles is set to 2.");
235 property_set("qemu.gles", "2");
236 } else {
237 ALOGD("Emulator without GPU support detected. "
238 "Fallback to legacy software renderer, qemu.gles is set to 0.");
239 property_set("qemu.gles", "0");
240 }
241}
242
Mathias Agopianada798b2012-02-13 17:09:30 -0800243void* Loader::open(egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700244{
Jesse Hall1508ae62017-01-19 17:43:26 -0800245 ATRACE_CALL();
246
Mathias Agopiande586972009-05-28 17:39:03 -0700247 void* dso;
Yi Kong48a6cd22018-07-18 10:07:09 -0700248 driver_t* hnd = nullptr;
Jesse Hall94cdba92013-07-11 09:40:35 -0700249
bohu69e5b1a2016-02-19 17:15:20 -0800250 setEmulatorGlesValue();
251
Cody Northrop9d5d33d2018-10-15 18:32:14 -0600252 dso = load_driver("GLES", cnx, EGL | GLESv1_CM | GLESv2);
Mathias Agopian99381422013-04-23 20:52:29 +0200253 if (dso) {
254 hnd = new driver_t(dso);
255 } else {
256 // Always load EGL first
Cody Northrop9d5d33d2018-10-15 18:32:14 -0600257 dso = load_driver("EGL", cnx, EGL);
Mathias Agopiande586972009-05-28 17:39:03 -0700258 if (dso) {
259 hnd = new driver_t(dso);
Mathias Agopian99381422013-04-23 20:52:29 +0200260 hnd->set( load_driver("GLESv1_CM", cnx, GLESv1_CM), GLESv1_CM );
261 hnd->set( load_driver("GLESv2", cnx, GLESv2), GLESv2 );
Mathias Agopiande586972009-05-28 17:39:03 -0700262 }
263 }
264
Mathias Agopian99381422013-04-23 20:52:29 +0200265 LOG_ALWAYS_FATAL_IF(!hnd, "couldn't find an OpenGL ES implementation");
Jesse Hallc07b5202013-07-04 12:08:16 -0700266
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700267 cnx->libEgl = load_wrapper(EGL_WRAPPER_DIR "/libEGL.so");
268 cnx->libGles2 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv2.so");
269 cnx->libGles1 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv1_CM.so");
270
Michael Chockc0ec5e22014-01-27 08:14:33 -0800271 LOG_ALWAYS_FATAL_IF(!cnx->libEgl,
272 "couldn't load system EGL wrapper libraries");
273
Jesse Hallc07b5202013-07-04 12:08:16 -0700274 LOG_ALWAYS_FATAL_IF(!cnx->libGles2 || !cnx->libGles1,
275 "couldn't load system OpenGL ES wrapper libraries");
276
Mathias Agopiande586972009-05-28 17:39:03 -0700277 return (void*)hnd;
278}
279
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600280void Loader::close(egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700281{
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600282 driver_t* hnd = (driver_t*) cnx->dso;
Mathias Agopiande586972009-05-28 17:39:03 -0700283 delete hnd;
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600284 cnx->dso = nullptr;
285
286 if (cnx->featureSo) {
287 dlclose(cnx->featureSo);
288 cnx->featureSo = nullptr;
289 }
290
291 cnx->angleDecided = false;
292 cnx->useAngle = false;
293
294 if (cnx->vendorEGL) {
295 dlclose(cnx->vendorEGL);
296 cnx->vendorEGL = nullptr;
297 }
Mathias Agopiande586972009-05-28 17:39:03 -0700298}
299
Jesse Hall94cdba92013-07-11 09:40:35 -0700300void Loader::init_api(void* dso,
301 char const * const * api,
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700302 char const * const * ref_api,
Jesse Hall94cdba92013-07-11 09:40:35 -0700303 __eglMustCastToProperFunctionPointerType* curr,
304 getProcAddressType getProcAddress)
Mathias Agopiande586972009-05-28 17:39:03 -0700305{
Jesse Hall1508ae62017-01-19 17:43:26 -0800306 ATRACE_CALL();
307
Mathias Agopian7773c432012-02-13 20:06:08 -0800308 const ssize_t SIZE = 256;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700309 char scrap[SIZE];
Mathias Agopiande586972009-05-28 17:39:03 -0700310 while (*api) {
311 char const * name = *api;
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700312 if (ref_api) {
313 char const * ref_name = *ref_api;
314 if (std::strcmp(name, ref_name) != 0) {
315 *curr++ = nullptr;
316 ref_api++;
317 continue;
318 }
319 }
320
Jesse Hall94cdba92013-07-11 09:40:35 -0700321 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700322 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700323 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700324 // couldn't find the entry-point, use eglGetProcAddress()
325 f = getProcAddress(name);
326 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700327 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700328 // Try without the OES postfix
329 ssize_t index = ssize_t(strlen(name)) - 3;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700330 if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) {
Mathias Agopiande586972009-05-28 17:39:03 -0700331 strncpy(scrap, name, index);
332 scrap[index] = 0;
333 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000334 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700335 }
336 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700337 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700338 // Try with the OES postfix
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700339 ssize_t index = ssize_t(strlen(name)) - 3;
340 if (index>0 && strcmp(name+index, "OES")) {
341 snprintf(scrap, SIZE, "%sOES", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700342 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000343 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700344 }
345 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700346 if (f == nullptr) {
Steve Block9d453682011-12-20 16:23:08 +0000347 //ALOGD("%s", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700348 f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented;
Mathias Agopian48d438d2012-01-28 21:44:00 -0800349
350 /*
351 * GL_EXT_debug_label is special, we always report it as
352 * supported, it's handled by GLES_trace. If GLES_trace is not
353 * enabled, then these are no-ops.
354 */
355 if (!strcmp(name, "glInsertEventMarkerEXT")) {
356 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
357 } else if (!strcmp(name, "glPushGroupMarkerEXT")) {
358 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
359 } else if (!strcmp(name, "glPopGroupMarkerEXT")) {
360 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
361 }
Mathias Agopiande586972009-05-28 17:39:03 -0700362 }
363 *curr++ = f;
364 api++;
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700365 if (ref_api) ref_api++;
Mathias Agopiande586972009-05-28 17:39:03 -0700366 }
367}
368
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800369static void* load_system_driver(const char* kind) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800370 ATRACE_CALL();
Mathias Agopian99381422013-04-23 20:52:29 +0200371 class MatchFile {
372 public:
Mathias Agopian65421432017-03-08 11:49:05 -0800373 static std::string find(const char* kind) {
374 std::string result;
Nicolas Capens776951f2015-11-06 10:10:21 -0500375 int emulationStatus = checkGlesEmulationStatus();
376 switch (emulationStatus) {
377 case 0:
Nicolas Capens776951f2015-11-06 10:10:21 -0500378#if defined(__LP64__)
bohu3adf9e12017-07-02 22:08:13 -0700379 result = "/vendor/lib64/egl/libGLES_android.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500380#else
bohu3adf9e12017-07-02 22:08:13 -0700381 result = "/vendor/lib/egl/libGLES_android.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500382#endif
383 return result;
384 case 1:
385 // Use host-side OpenGL through the "emulation" library
386#if defined(__LP64__)
bohu3adf9e12017-07-02 22:08:13 -0700387 result = std::string("/vendor/lib64/egl/lib") + kind + "_emulation.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500388#else
bohu3adf9e12017-07-02 22:08:13 -0700389 result = std::string("/vendor/lib/egl/lib") + kind + "_emulation.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500390#endif
391 return result;
Lingfeng Yang0f82d872017-08-11 01:18:00 -0700392 case 2:
393 // Use guest side swiftshader library
394#if defined(__LP64__)
395 result = std::string("/vendor/lib64/egl/lib") + kind + "_swiftshader.so";
396#else
397 result = std::string("/vendor/lib/egl/lib") + kind + "_swiftshader.so";
398#endif
399 return result;
Nicolas Capens776951f2015-11-06 10:10:21 -0500400 default:
401 // Not in emulator, or use other guest-side implementation
402 break;
403 }
404
Mathias Agopian65421432017-03-08 11:49:05 -0800405 std::string pattern = std::string("lib") + kind;
Mathias Agopian99381422013-04-23 20:52:29 +0200406 const char* const searchPaths[] = {
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800407#if defined(__LP64__)
408 "/vendor/lib64/egl",
409 "/system/lib64/egl"
410#else
Mathias Agopian99381422013-04-23 20:52:29 +0200411 "/vendor/lib/egl",
412 "/system/lib/egl"
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800413#endif
Mathias Agopian99381422013-04-23 20:52:29 +0200414 };
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700415
Mathias Agopian99381422013-04-23 20:52:29 +0200416 // first, we search for the exact name of the GLES userspace
417 // driver in both locations.
418 // i.e.:
419 // libGLES.so, or:
420 // libEGL.so, libGLESv1_CM.so, libGLESv2.so
421
422 for (size_t i=0 ; i<NELEM(searchPaths) ; i++) {
423 if (find(result, pattern, searchPaths[i], true)) {
424 return result;
425 }
426 }
427
428 // for compatibility with the old "egl.cfg" naming convention
429 // we look for files that match:
430 // libGLES_*.so, or:
431 // libEGL_*.so, libGLESv1_CM_*.so, libGLESv2_*.so
432
433 pattern.append("_");
434 for (size_t i=0 ; i<NELEM(searchPaths) ; i++) {
435 if (find(result, pattern, searchPaths[i], false)) {
436 return result;
437 }
438 }
439
440 // we didn't find the driver. gah.
441 result.clear();
442 return result;
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700443 }
Mathias Agopian99381422013-04-23 20:52:29 +0200444
445 private:
Mathias Agopian65421432017-03-08 11:49:05 -0800446 static bool find(std::string& result,
447 const std::string& pattern, const char* const search, bool exact) {
Mathias Agopian99381422013-04-23 20:52:29 +0200448 if (exact) {
Jesse Hall4aaa9602017-08-29 16:33:54 -0700449 std::string absolutePath = std::string(search) + "/" + pattern + ".so";
Mathias Agopian65421432017-03-08 11:49:05 -0800450 if (!access(absolutePath.c_str(), R_OK)) {
Mathias Agopian99381422013-04-23 20:52:29 +0200451 result = absolutePath;
452 return true;
453 }
454 return false;
455 }
456
457 DIR* d = opendir(search);
Yi Kong48a6cd22018-07-18 10:07:09 -0700458 if (d != nullptr) {
Mathias Agopian99381422013-04-23 20:52:29 +0200459 struct dirent* e;
Yi Kong48a6cd22018-07-18 10:07:09 -0700460 while ((e = readdir(d)) != nullptr) {
Mathias Agopian99381422013-04-23 20:52:29 +0200461 if (e->d_type == DT_DIR) {
462 continue;
463 }
464 if (!strcmp(e->d_name, "libGLES_android.so")) {
465 // always skip the software renderer
466 continue;
467 }
Mathias Agopian65421432017-03-08 11:49:05 -0800468 if (strstr(e->d_name, pattern.c_str()) == e->d_name) {
Mathias Agopian99381422013-04-23 20:52:29 +0200469 if (!strcmp(e->d_name + strlen(e->d_name) - 3, ".so")) {
Mathias Agopian65421432017-03-08 11:49:05 -0800470 result = std::string(search) + "/" + e->d_name;
Mathias Agopian99381422013-04-23 20:52:29 +0200471 closedir(d);
472 return true;
473 }
474 }
475 }
476 closedir(d);
477 }
478 return false;
479 }
480 };
481
482
Mathias Agopian65421432017-03-08 11:49:05 -0800483 std::string absolutePath = MatchFile::find(kind);
484 if (absolutePath.empty()) {
Mathias Agopian99381422013-04-23 20:52:29 +0200485 // this happens often, we don't want to log an error
Yi Kong48a6cd22018-07-18 10:07:09 -0700486 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700487 }
Mathias Agopian65421432017-03-08 11:49:05 -0800488 const char* const driver_absolute_path = absolutePath.c_str();
Mathias Agopiande586972009-05-28 17:39:03 -0700489
Jiyong Park5910dc72017-04-05 14:23:52 +0900490 // Try to load drivers from the 'sphal' namespace, if it exist. Fall back to
Justin Yunb7320302017-05-22 15:13:40 +0900491 // the original routine when the namespace does not exist.
Jiyong Park5910dc72017-04-05 14:23:52 +0900492 // See /system/core/rootdir/etc/ld.config.txt for the configuration of the
493 // sphal namespace.
Justin Yunb7320302017-05-22 15:13:40 +0900494 void* dso = do_android_load_sphal_library(driver_absolute_path,
495 RTLD_NOW | RTLD_LOCAL);
Yi Kong48a6cd22018-07-18 10:07:09 -0700496 if (dso == nullptr) {
Mathias Agopian8c173842009-09-20 16:01:02 -0700497 const char* err = dlerror();
Mathias Agopian65421432017-03-08 11:49:05 -0800498 ALOGE("load_driver(%s): %s", driver_absolute_path, err ? err : "unknown");
Yi Kong48a6cd22018-07-18 10:07:09 -0700499 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700500 }
501
Steve Block9d453682011-12-20 16:23:08 +0000502 ALOGD("loaded %s", driver_absolute_path);
Mathias Agopianbaca89c2009-08-20 19:09:34 -0700503
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800504 return dso;
505}
506
Cody Northrop1f00e172018-04-02 11:23:31 -0600507static void* load_angle_from_namespace(const char* kind, android_namespace_t* ns) {
508 const android_dlextinfo dlextinfo = {
509 .flags = ANDROID_DLEXT_USE_NAMESPACE,
510 .library_namespace = ns,
511 };
512
513 std::string name = std::string("lib") + kind + "_angle.so";
514
515 void* so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
516
517 if (so) {
518 ALOGD("dlopen_ext from APK (%s) success at %p", name.c_str(), so);
519 return so;
520 } else {
521 ALOGE("dlopen_ext(\"%s\") failed: %s", name.c_str(), dlerror());
522 }
523
524 return nullptr;
525}
526
Cody Northropfc94a582018-10-30 15:13:30 -0600527
528static bool check_angle_rules(void* so, const char* app_name) {
529 bool use_angle = false;
530 const int rules_fd = android::GraphicsEnv::getInstance().getAngleRulesFd();
531 const long rules_offset = android::GraphicsEnv::getInstance().getAngleRulesOffset();
532 const long rules_length = android::GraphicsEnv::getInstance().getAngleRulesLength();
533
534 std::string app_name_str = app_name ? app_name : "";
535 char manufacturer[PROPERTY_VALUE_MAX];
536 char model[PROPERTY_VALUE_MAX];
537 property_get("ro.product.manufacturer", manufacturer, "UNSET");
538 property_get("ro.product.model", model, "UNSET");
539
Ian Elliottb6125f52018-11-16 13:40:51 -0700540 // TODO: Replace this with the new function name once the version-1 API is removed:
Cody Northropfc94a582018-10-30 15:13:30 -0600541 fpANGLEGetUtilityAPI ANGLEGetUtilityAPI =
542 (fpANGLEGetUtilityAPI)dlsym(so, "ANGLEGetUtilityAPI");
Cody Northrop15666572018-10-30 15:47:20 -0600543
Cody Northropfc94a582018-10-30 15:13:30 -0600544 if (ANGLEGetUtilityAPI) {
Cody Northrop15666572018-10-30 15:47:20 -0600545
546 // Negotiate the interface version by requesting most recent known to the platform
Ian Elliottb6125f52018-11-16 13:40:51 -0700547 unsigned int versionToUse = 2;
548 // TODO: Replace this with the new function name once the version-1 API is removed:
Cody Northropfc94a582018-10-30 15:13:30 -0600549 if ((ANGLEGetUtilityAPI)(&versionToUse)) {
Cody Northrop15666572018-10-30 15:47:20 -0600550
551 // Add and remove versions below as needed
552 switch(versionToUse) {
553 case 1: {
Ian Elliottb6125f52018-11-16 13:40:51 -0700554 ALOGV("Using version 1 of ANGLE feature-support library");
Cody Northrop15666572018-10-30 15:47:20 -0600555 fpAndroidUseANGLEForApplication AndroidUseANGLEForApplication =
556 (fpAndroidUseANGLEForApplication)dlsym(so, "AndroidUseANGLEForApplication");
557
558 if (AndroidUseANGLEForApplication) {
559 use_angle = (AndroidUseANGLEForApplication)(rules_fd, rules_offset,
560 rules_length, app_name_str.c_str(),
561 manufacturer, model);
562 } else {
563 ALOGW("Cannot find AndroidUseANGLEForApplication in ANGLE feature-support library");
564 }
Cody Northropfc94a582018-10-30 15:13:30 -0600565 }
Cody Northrop15666572018-10-30 15:47:20 -0600566 break;
Ian Elliottb6125f52018-11-16 13:40:51 -0700567 case 2: {
568 ALOGV("Using version 2 of ANGLE feature-support library");
569 void* rules_handle = nullptr;
570 int rules_version = 0;
571 void* system_info_handle = nullptr;
572
573 // Get the symbols for the feature-support-utility library:
574#define GET_SYMBOL(symbol) \
575 fp##symbol symbol = (fp##symbol)dlsym(so, #symbol); \
576 if (!symbol) { \
577 ALOGW("Cannot find "#symbol" in ANGLE feature-support library"); \
578 break; \
579 }
580 GET_SYMBOL(ANGLEAndroidParseRulesString);
581 GET_SYMBOL(ANGLEGetSystemInfo);
582 GET_SYMBOL(ANGLEAddDeviceInfoToSystemInfo);
583 GET_SYMBOL(ANGLEShouldBeUsedForApplication);
584 GET_SYMBOL(ANGLEFreeRulesHandle);
585 GET_SYMBOL(ANGLEFreeSystemInfoHandle);
586
587 // Read the contents of the file into a string:
588 off_t fileSize = rules_length;
589 off_t startOfContent = rules_offset;
590 lseek(rules_fd, startOfContent, SEEK_SET);
591 char *buffer = new char[fileSize + 1];
592 ssize_t numBytesRead = read(rules_fd, buffer, fileSize);
593 if (numBytesRead < 0) {
594 ALOGW("Cannot read rules file");
595 break;
596 }
597 if (numBytesRead == 0) {
598 ALOGW("Empty rules file");
599 break;
600 }
601 buffer[numBytesRead] = '\0';
602 std::string rule_file_contents = std::string(buffer);
603 delete[] buffer;
604
605 // Parse the rules, obtain the SystemInfo, and evaluate the
606 // application against the rules:
607 if (!(ANGLEAndroidParseRulesString)(rule_file_contents.c_str(),
608 &rules_handle, &rules_version)) {
609 ALOGW("ANGLE feature-support library cannot parse rules file");
610 break;
611 }
612 if (!(ANGLEGetSystemInfo)(&system_info_handle)) {
613 ALOGW("ANGLE feature-support library cannot obtain SystemInfo");
614 break;
615 }
616 if (!(ANGLEAddDeviceInfoToSystemInfo)(manufacturer, model, system_info_handle)) {
617 ALOGW("ANGLE feature-support library cannot add device info to SystemInfo");
618 break;
619 }
620 use_angle = (ANGLEShouldBeUsedForApplication)(rules_handle, rules_version,
621 system_info_handle, app_name_str.c_str());
622 (ANGLEFreeRulesHandle)(rules_handle);
623 (ANGLEFreeSystemInfoHandle)(system_info_handle);
624 }
625 break;
Cody Northrop15666572018-10-30 15:47:20 -0600626 default:
627 ALOGW("Cannot find supported version of ANGLE feature-support library, found version %u", versionToUse);
628 }
629 } else {
630 ALOGW("Cannot use ANGLE feature-support library, it is older than supported by EGL, requested version %u", versionToUse);
Cody Northropfc94a582018-10-30 15:13:30 -0600631 }
632 } else {
Cody Northrop15666572018-10-30 15:47:20 -0600633 ALOGW("Cannot find ANGLEGetUtilityAPI function");
Cody Northropfc94a582018-10-30 15:13:30 -0600634 }
Cody Northrop15666572018-10-30 15:47:20 -0600635
Cody Northropfc94a582018-10-30 15:13:30 -0600636 ALOGV("Close temporarily-loaded ANGLE opt-in/out logic");
637 return use_angle;
638}
639
Cody Northrop6d082ef2018-09-11 09:42:31 -0600640static void* load_angle(const char* kind, android_namespace_t* ns, egl_connection_t* cnx) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600641 // Only attempt to load ANGLE libs
642 if (strcmp(kind, "EGL") != 0 && strcmp(kind, "GLESv2") != 0 && strcmp(kind, "GLESv1_CM") != 0)
643 return nullptr;
644
Cody Northrop1f00e172018-04-02 11:23:31 -0600645 std::string name;
Ian Elliottb058aff2018-08-09 12:00:55 -0600646 char prop[PROPERTY_VALUE_MAX];
Cody Northrop1f00e172018-04-02 11:23:31 -0600647
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600648 const char* app_name = android::GraphicsEnv::getInstance().getAngleAppName();
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600649 bool developer_opt_in = android::GraphicsEnv::getInstance().getAngleDeveloperOptIn();
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600650
Ian Elliott34474472018-09-05 10:57:07 -0600651 // Determine whether or not to use ANGLE:
Cody Northropf0874d32018-10-29 10:59:45 -0600652 bool use_angle = developer_opt_in;
Ian Elliott34474472018-09-05 10:57:07 -0600653
654 if (use_angle) {
655 ALOGV("User set \"Developer Options\" to force the use of ANGLE");
Courtney Goeltzenleuchter734a4852018-10-23 16:26:24 -0600656 } else if (cnx->angleDecided) {
657 use_angle = cnx->useAngle;
Ian Elliott34474472018-09-05 10:57:07 -0600658 } else {
659 // The "Developer Options" value wasn't set to force the use of ANGLE. Need to temporarily
660 // load ANGLE and call the updatable opt-in/out logic:
Courtney Goeltzenleuchter28b97dc2018-10-24 17:06:35 -0600661
Cody Northropdb444a92018-11-09 17:17:51 -0700662 // Check if ANGLE is enabled. Workaround for several bugs:
663 // b/119305693 b/119322355 b/119305887
664 // Something is not working correctly in the feature library
665 property_get("debug.angle.enable", prop, "0");
666 if (atoi(prop)) {
667 cnx->featureSo = load_angle_from_namespace("feature_support", ns);
668 }
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600669 if (cnx->featureSo) {
670 ALOGV("loaded ANGLE's opt-in/out logic from namespace");
Cody Northropfc94a582018-10-30 15:13:30 -0600671 use_angle = check_angle_rules(cnx->featureSo, app_name);
Ian Elliott34474472018-09-05 10:57:07 -0600672 } else {
673 // We weren't able to load and call the updateable opt-in/out logic.
674 // If we can't load the library, there is no ANGLE available.
675 use_angle = false;
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600676 ALOGV("Could not load the ANGLE opt-in/out logic, cannot use ANGLE.");
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600677 }
Courtney Goeltzenleuchter734a4852018-10-23 16:26:24 -0600678 cnx->angleDecided = true;
Cody Northrop1f00e172018-04-02 11:23:31 -0600679 }
Courtney Goeltzenleuchter985bc282018-11-02 14:24:55 -0600680 void* so = nullptr;
Ian Elliott34474472018-09-05 10:57:07 -0600681 if (use_angle) {
682 so = load_angle_from_namespace(kind, ns);
683 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600684
685 if (so) {
Ian Elliott34474472018-09-05 10:57:07 -0600686 ALOGV("Loaded ANGLE %s library for %s (instead of native)",
Ian Elliottb058aff2018-08-09 12:00:55 -0600687 kind, app_name ? app_name : "nullptr");
Ian Elliottb058aff2018-08-09 12:00:55 -0600688 property_get("debug.hwui.renderer", prop, "UNSET");
Ian Elliott34474472018-09-05 10:57:07 -0600689 ALOGV("Skia's renderer set to %s", prop);
Cody Northrop1f00e172018-04-02 11:23:31 -0600690 cnx->useAngle = true;
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600691
692 EGLint angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE;
693
694 char prop[PROPERTY_VALUE_MAX];
695 property_get("debug.angle.backend", prop, "0");
696 switch (atoi(prop)) {
697 case 1:
698 ALOGV("%s: Requesting OpenGLES back-end", __FUNCTION__);
699 angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE;
700 break;
701 case 2:
702 ALOGV("%s: Requesting Vulkan back-end", __FUNCTION__);
703 angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE;
704 break;
705 default:
706 break;
707 }
708
709 cnx->angleBackend = angleBackendDefault;
710 if (!cnx->vendorEGL && (cnx->angleBackend == EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE)) {
711 // Find and load vendor libEGL for ANGLE's GL back-end to use.
Cody Northrop1f00e172018-04-02 11:23:31 -0600712 cnx->vendorEGL = load_system_driver("EGL");
713 }
714 return so;
Ian Elliottb058aff2018-08-09 12:00:55 -0600715 } else {
Ian Elliott34474472018-09-05 10:57:07 -0600716 ALOGV("Loaded native %s library for %s (instead of ANGLE)",
Ian Elliottb058aff2018-08-09 12:00:55 -0600717 kind, app_name ? app_name : "nullptr");
Cody Northrop1f00e172018-04-02 11:23:31 -0600718 }
719
720 return nullptr;
721}
722
Mathias Agopian311b4792017-02-28 15:00:49 -0800723static const char* HAL_SUBNAME_KEY_PROPERTIES[2] = {
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800724 "ro.hardware.egl",
725 "ro.board.platform",
Mathias Agopian311b4792017-02-28 15:00:49 -0800726};
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800727
728static void* load_updated_driver(const char* kind, android_namespace_t* ns) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800729 ATRACE_CALL();
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800730 const android_dlextinfo dlextinfo = {
731 .flags = ANDROID_DLEXT_USE_NAMESPACE,
732 .library_namespace = ns,
733 };
734 void* so = nullptr;
735 char prop[PROPERTY_VALUE_MAX + 1];
736 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
737 if (property_get(key, prop, nullptr) > 0) {
Mathias Agopian65421432017-03-08 11:49:05 -0800738 std::string name = std::string("lib") + kind + "_" + prop + ".so";
739 so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
Mathias Agopian311b4792017-02-28 15:00:49 -0800740 if (so) {
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800741 return so;
Mathias Agopian311b4792017-02-28 15:00:49 -0800742 }
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800743 }
744 }
745 return nullptr;
746}
747
748void *Loader::load_driver(const char* kind,
749 egl_connection_t* cnx, uint32_t mask)
750{
Jesse Hall1508ae62017-01-19 17:43:26 -0800751 ATRACE_CALL();
752
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800753 void* dso = nullptr;
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600754 android_namespace_t* ns = android::GraphicsEnv::getInstance().getAngleNamespace();
Cody Northrop6d082ef2018-09-11 09:42:31 -0600755 if (ns) {
756 dso = load_angle(kind, ns, cnx);
757 }
Jiyong Parka243e5d2017-06-21 12:26:51 +0900758#ifndef __ANDROID_VNDK__
Cody Northrop1f00e172018-04-02 11:23:31 -0600759 if (!dso) {
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600760 android_namespace_t* ns = android::GraphicsEnv::getInstance().getDriverNamespace();
Cody Northrop1f00e172018-04-02 11:23:31 -0600761 if (ns) {
762 dso = load_updated_driver(kind, ns);
763 }
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800764 }
Jiyong Parka243e5d2017-06-21 12:26:51 +0900765#endif
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800766 if (!dso) {
767 dso = load_system_driver(kind);
768 if (!dso)
Yi Kong48a6cd22018-07-18 10:07:09 -0700769 return nullptr;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800770 }
771
Mathias Agopiande586972009-05-28 17:39:03 -0700772 if (mask & EGL) {
773 getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress");
774
Jesse Hall94cdba92013-07-11 09:40:35 -0700775 ALOGE_IF(!getProcAddress,
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800776 "can't find eglGetProcAddress() in EGL driver library");
Mathias Agopiande586972009-05-28 17:39:03 -0700777
Mathias Agopian618fa102009-10-14 02:06:37 -0700778 egl_t* egl = &cnx->egl;
Mathias Agopiande586972009-05-28 17:39:03 -0700779 __eglMustCastToProperFunctionPointerType* curr =
780 (__eglMustCastToProperFunctionPointerType*)egl;
781 char const * const * api = egl_names;
782 while (*api) {
783 char const * name = *api;
Jesse Hall94cdba92013-07-11 09:40:35 -0700784 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700785 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700786 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700787 // couldn't find the entry-point, use eglGetProcAddress()
788 f = getProcAddress(name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700789 if (f == nullptr) {
790 f = (__eglMustCastToProperFunctionPointerType)nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700791 }
792 }
793 *curr++ = f;
794 api++;
795 }
796 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700797
Mathias Agopiande586972009-05-28 17:39:03 -0700798 if (mask & GLESv1_CM) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700799 init_api(dso, gl_names_1, gl_names,
Mathias Agopian618fa102009-10-14 02:06:37 -0700800 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800801 &cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl,
Mathias Agopian618fa102009-10-14 02:06:37 -0700802 getProcAddress);
Mathias Agopiande586972009-05-28 17:39:03 -0700803 }
804
805 if (mask & GLESv2) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700806 init_api(dso, gl_names, nullptr,
Mathias Agopian618fa102009-10-14 02:06:37 -0700807 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800808 &cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl,
Mathias Agopiande586972009-05-28 17:39:03 -0700809 getProcAddress);
810 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700811
Mathias Agopiande586972009-05-28 17:39:03 -0700812 return dso;
813}
814
815// ----------------------------------------------------------------------------
816}; // namespace android
817// ----------------------------------------------------------------------------