blob: f5cf3dc98b0c0b1f5eecf17e508b2a272054f227 [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
Tim Van Pattena2a60a02018-11-09 16:51:15 -070044 typedef enum ANGLEPreference {
45 ANGLE_PREFER_DEFAULT = 0,
46 ANGLE_PREFER_NATIVE = 1,
47 ANGLE_PREFER_ANGLE = 2,
48 } ANGLEPreference;
49
Ian Elliottb6125f52018-11-16 13:40:51 -070050 // TODO(ianelliott@): Get the following from an ANGLE header:
51 // Version-1 API:
Ian Elliott380bf852018-10-17 14:56:04 -060052 typedef bool (*fpANGLEGetUtilityAPI)(unsigned int* versionToUse);
Ian Elliott380bf852018-10-17 14:56:04 -060053 typedef bool (*fpAndroidUseANGLEForApplication)(int fd, long offset, long length,
54 const char* appName, const char* deviceMfr,
55 const char* deviceModel);
Ian Elliottb6125f52018-11-16 13:40:51 -070056 // Version-2 API:
57 typedef bool (*fpANGLEGetFeatureSupportUtilAPIVersion)(unsigned int* versionToUse);
58 typedef bool (*fpANGLEAndroidParseRulesString)(const char *rulesString,
59 void** rulesHandle, int* rulesVersion);
60 typedef bool (*fpANGLEGetSystemInfo)(void** handle);
61 typedef bool (*fpANGLEAddDeviceInfoToSystemInfo)(const char* deviceMfr,
62 const char* deviceModel,
63 void* handle);
64 typedef bool (*fpANGLEShouldBeUsedForApplication)(void* rules_handle,
65 int rules_version,
66 void* system_info_handle,
67 const char *appName);
68 typedef bool (*fpANGLEFreeRulesHandle)(void* handle);
69 typedef bool (*fpANGLEFreeSystemInfoHandle)(void* handle);
70
Jiyong Park5910dc72017-04-05 14:23:52 +090071}
72
Mathias Agopiande586972009-05-28 17:39:03 -070073// ----------------------------------------------------------------------------
74namespace android {
75// ----------------------------------------------------------------------------
76
77
78/*
Mathias Agopian99381422013-04-23 20:52:29 +020079 * EGL userspace drivers must be provided either:
80 * - as a single library:
81 * /vendor/lib/egl/libGLES.so
82 *
83 * - as separate libraries:
84 * /vendor/lib/egl/libEGL.so
85 * /vendor/lib/egl/libGLESv1_CM.so
86 * /vendor/lib/egl/libGLESv2.so
87 *
88 * The software renderer for the emulator must be provided as a single
89 * library at:
90 *
91 * /system/lib/egl/libGLES_android.so
92 *
93 *
94 * For backward compatibility and to facilitate the transition to
95 * this new naming scheme, the loader will additionally look for:
Jesse Hall94cdba92013-07-11 09:40:35 -070096 *
Mathias Agopian99381422013-04-23 20:52:29 +020097 * /{vendor|system}/lib/egl/lib{GLES | [EGL|GLESv1_CM|GLESv2]}_*.so
Jesse Hall94cdba92013-07-11 09:40:35 -070098 *
Mathias Agopiande586972009-05-28 17:39:03 -070099 */
100
Mathias Agopian65421432017-03-08 11:49:05 -0800101Loader& Loader::getInstance() {
102 static Loader loader;
103 return loader;
104}
Mathias Agopiande586972009-05-28 17:39:03 -0700105
David 'Digit' Turner80b30c22011-08-26 17:38:47 +0200106/* This function is called to check whether we run inside the emulator,
107 * and if this is the case whether GLES GPU emulation is supported.
108 *
109 * Returned values are:
110 * -1 -> not running inside the emulator
111 * 0 -> running inside the emulator, but GPU emulation not supported
112 * 1 -> running inside the emulator, GPU emulation is supported
Nicolas Capens776951f2015-11-06 10:10:21 -0500113 * through the "emulation" host-side OpenGL ES implementation.
114 * 2 -> running inside the emulator, GPU emulation is supported
115 * through a guest-side vendor driver's OpenGL ES implementation.
David 'Digit' Turner80b30c22011-08-26 17:38:47 +0200116 */
117static int
118checkGlesEmulationStatus(void)
119{
120 /* We're going to check for the following kernel parameters:
121 *
122 * qemu=1 -> tells us that we run inside the emulator
123 * android.qemu.gles=<number> -> tells us the GLES GPU emulation status
124 *
125 * Note that we will return <number> if we find it. This let us support
126 * more additionnal emulation modes in the future.
127 */
128 char prop[PROPERTY_VALUE_MAX];
129 int result = -1;
130
131 /* First, check for qemu=1 */
132 property_get("ro.kernel.qemu",prop,"0");
133 if (atoi(prop) != 1)
134 return -1;
135
136 /* We are in the emulator, get GPU status value */
bohu69e5b1a2016-02-19 17:15:20 -0800137 property_get("qemu.gles",prop,"0");
David 'Digit' Turner80b30c22011-08-26 17:38:47 +0200138 return atoi(prop);
139}
140
Jesse Hall1508ae62017-01-19 17:43:26 -0800141static void* do_dlopen(const char* path, int mode) {
142 ATRACE_CALL();
143 return dlopen(path, mode);
144}
145
Jiyong Park5910dc72017-04-05 14:23:52 +0900146static void* do_android_dlopen_ext(const char* path, int mode, const android_dlextinfo* info) {
147 ATRACE_CALL();
148 return android_dlopen_ext(path, mode, info);
149}
150
Justin Yunb7320302017-05-22 15:13:40 +0900151static void* do_android_load_sphal_library(const char* path, int mode) {
152 ATRACE_CALL();
153 return android_load_sphal_library(path, mode);
154}
155
Mathias Agopiande586972009-05-28 17:39:03 -0700156// ----------------------------------------------------------------------------
157
Jesse Hall94cdba92013-07-11 09:40:35 -0700158Loader::driver_t::driver_t(void* gles)
Mathias Agopiande586972009-05-28 17:39:03 -0700159{
160 dso[0] = gles;
161 for (size_t i=1 ; i<NELEM(dso) ; i++)
Yi Kong48a6cd22018-07-18 10:07:09 -0700162 dso[i] = nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700163}
164
Jesse Hall94cdba92013-07-11 09:40:35 -0700165Loader::driver_t::~driver_t()
Mathias Agopiande586972009-05-28 17:39:03 -0700166{
167 for (size_t i=0 ; i<NELEM(dso) ; i++) {
168 if (dso[i]) {
169 dlclose(dso[i]);
Yi Kong48a6cd22018-07-18 10:07:09 -0700170 dso[i] = nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700171 }
172 }
173}
174
Mathias Agopian65421432017-03-08 11:49:05 -0800175int Loader::driver_t::set(void* hnd, int32_t api)
Mathias Agopiande586972009-05-28 17:39:03 -0700176{
177 switch (api) {
178 case EGL:
179 dso[0] = hnd;
180 break;
181 case GLESv1_CM:
182 dso[1] = hnd;
183 break;
184 case GLESv2:
185 dso[2] = hnd;
186 break;
187 default:
Mathias Agopian65421432017-03-08 11:49:05 -0800188 return -EOVERFLOW;
Mathias Agopiande586972009-05-28 17:39:03 -0700189 }
Mathias Agopian65421432017-03-08 11:49:05 -0800190 return 0;
Mathias Agopiande586972009-05-28 17:39:03 -0700191}
192
193// ----------------------------------------------------------------------------
194
Mathias Agopiande586972009-05-28 17:39:03 -0700195Loader::Loader()
Yi Kong48a6cd22018-07-18 10:07:09 -0700196 : getProcAddress(nullptr)
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800197{
Mathias Agopiande586972009-05-28 17:39:03 -0700198}
199
Mathias Agopian99381422013-04-23 20:52:29 +0200200Loader::~Loader() {
Mathias Agopiande586972009-05-28 17:39:03 -0700201}
202
Jesse Hallc07b5202013-07-04 12:08:16 -0700203static void* load_wrapper(const char* path) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800204 void* so = do_dlopen(path, RTLD_NOW | RTLD_LOCAL);
Jesse Hallc07b5202013-07-04 12:08:16 -0700205 ALOGE_IF(!so, "dlopen(\"%s\") failed: %s", path, dlerror());
206 return so;
207}
208
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700209#ifndef EGL_WRAPPER_DIR
210#if defined(__LP64__)
211#define EGL_WRAPPER_DIR "/system/lib64"
212#else
213#define EGL_WRAPPER_DIR "/system/lib"
214#endif
215#endif
216
bohu69e5b1a2016-02-19 17:15:20 -0800217static void setEmulatorGlesValue(void) {
218 char prop[PROPERTY_VALUE_MAX];
219 property_get("ro.kernel.qemu", prop, "0");
220 if (atoi(prop) != 1) return;
221
222 property_get("ro.kernel.qemu.gles",prop,"0");
223 if (atoi(prop) == 1) {
224 ALOGD("Emulator has host GPU support, qemu.gles is set to 1.");
225 property_set("qemu.gles", "1");
226 return;
227 }
228
229 // for now, checking the following
230 // directory is good enough for emulator system images
231 const char* vendor_lib_path =
232#if defined(__LP64__)
233 "/vendor/lib64/egl";
234#else
235 "/vendor/lib/egl";
236#endif
237
238 const bool has_vendor_lib = (access(vendor_lib_path, R_OK) == 0);
239 if (has_vendor_lib) {
240 ALOGD("Emulator has vendor provided software renderer, qemu.gles is set to 2.");
241 property_set("qemu.gles", "2");
242 } else {
243 ALOGD("Emulator without GPU support detected. "
244 "Fallback to legacy software renderer, qemu.gles is set to 0.");
245 property_set("qemu.gles", "0");
246 }
247}
248
Mathias Agopianada798b2012-02-13 17:09:30 -0800249void* Loader::open(egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700250{
Jesse Hall1508ae62017-01-19 17:43:26 -0800251 ATRACE_CALL();
252
Mathias Agopiande586972009-05-28 17:39:03 -0700253 void* dso;
Yi Kong48a6cd22018-07-18 10:07:09 -0700254 driver_t* hnd = nullptr;
Jesse Hall94cdba92013-07-11 09:40:35 -0700255
bohu69e5b1a2016-02-19 17:15:20 -0800256 setEmulatorGlesValue();
257
Cody Northrop9d5d33d2018-10-15 18:32:14 -0600258 dso = load_driver("GLES", cnx, EGL | GLESv1_CM | GLESv2);
Mathias Agopian99381422013-04-23 20:52:29 +0200259 if (dso) {
260 hnd = new driver_t(dso);
261 } else {
262 // Always load EGL first
Cody Northrop9d5d33d2018-10-15 18:32:14 -0600263 dso = load_driver("EGL", cnx, EGL);
Mathias Agopiande586972009-05-28 17:39:03 -0700264 if (dso) {
265 hnd = new driver_t(dso);
Mathias Agopian99381422013-04-23 20:52:29 +0200266 hnd->set( load_driver("GLESv1_CM", cnx, GLESv1_CM), GLESv1_CM );
267 hnd->set( load_driver("GLESv2", cnx, GLESv2), GLESv2 );
Mathias Agopiande586972009-05-28 17:39:03 -0700268 }
269 }
270
Mathias Agopian99381422013-04-23 20:52:29 +0200271 LOG_ALWAYS_FATAL_IF(!hnd, "couldn't find an OpenGL ES implementation");
Jesse Hallc07b5202013-07-04 12:08:16 -0700272
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700273 cnx->libEgl = load_wrapper(EGL_WRAPPER_DIR "/libEGL.so");
274 cnx->libGles2 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv2.so");
275 cnx->libGles1 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv1_CM.so");
276
Michael Chockc0ec5e22014-01-27 08:14:33 -0800277 LOG_ALWAYS_FATAL_IF(!cnx->libEgl,
278 "couldn't load system EGL wrapper libraries");
279
Jesse Hallc07b5202013-07-04 12:08:16 -0700280 LOG_ALWAYS_FATAL_IF(!cnx->libGles2 || !cnx->libGles1,
281 "couldn't load system OpenGL ES wrapper libraries");
282
Mathias Agopiande586972009-05-28 17:39:03 -0700283 return (void*)hnd;
284}
285
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600286void Loader::close(egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700287{
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600288 driver_t* hnd = (driver_t*) cnx->dso;
Mathias Agopiande586972009-05-28 17:39:03 -0700289 delete hnd;
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600290 cnx->dso = nullptr;
291
292 if (cnx->featureSo) {
293 dlclose(cnx->featureSo);
294 cnx->featureSo = nullptr;
295 }
296
297 cnx->angleDecided = false;
298 cnx->useAngle = false;
299
300 if (cnx->vendorEGL) {
301 dlclose(cnx->vendorEGL);
302 cnx->vendorEGL = nullptr;
303 }
Mathias Agopiande586972009-05-28 17:39:03 -0700304}
305
Jesse Hall94cdba92013-07-11 09:40:35 -0700306void Loader::init_api(void* dso,
307 char const * const * api,
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700308 char const * const * ref_api,
Jesse Hall94cdba92013-07-11 09:40:35 -0700309 __eglMustCastToProperFunctionPointerType* curr,
310 getProcAddressType getProcAddress)
Mathias Agopiande586972009-05-28 17:39:03 -0700311{
Jesse Hall1508ae62017-01-19 17:43:26 -0800312 ATRACE_CALL();
313
Mathias Agopian7773c432012-02-13 20:06:08 -0800314 const ssize_t SIZE = 256;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700315 char scrap[SIZE];
Mathias Agopiande586972009-05-28 17:39:03 -0700316 while (*api) {
317 char const * name = *api;
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700318 if (ref_api) {
319 char const * ref_name = *ref_api;
320 if (std::strcmp(name, ref_name) != 0) {
321 *curr++ = nullptr;
322 ref_api++;
323 continue;
324 }
325 }
326
Jesse Hall94cdba92013-07-11 09:40:35 -0700327 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700328 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700329 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700330 // couldn't find the entry-point, use eglGetProcAddress()
331 f = getProcAddress(name);
332 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700333 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700334 // Try without the OES postfix
335 ssize_t index = ssize_t(strlen(name)) - 3;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700336 if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) {
Mathias Agopiande586972009-05-28 17:39:03 -0700337 strncpy(scrap, name, index);
338 scrap[index] = 0;
339 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000340 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700341 }
342 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700343 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700344 // Try with the OES postfix
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700345 ssize_t index = ssize_t(strlen(name)) - 3;
346 if (index>0 && strcmp(name+index, "OES")) {
347 snprintf(scrap, SIZE, "%sOES", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700348 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000349 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700350 }
351 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700352 if (f == nullptr) {
Steve Block9d453682011-12-20 16:23:08 +0000353 //ALOGD("%s", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700354 f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented;
Mathias Agopian48d438d2012-01-28 21:44:00 -0800355
356 /*
357 * GL_EXT_debug_label is special, we always report it as
358 * supported, it's handled by GLES_trace. If GLES_trace is not
359 * enabled, then these are no-ops.
360 */
361 if (!strcmp(name, "glInsertEventMarkerEXT")) {
362 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
363 } else if (!strcmp(name, "glPushGroupMarkerEXT")) {
364 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
365 } else if (!strcmp(name, "glPopGroupMarkerEXT")) {
366 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
367 }
Mathias Agopiande586972009-05-28 17:39:03 -0700368 }
369 *curr++ = f;
370 api++;
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700371 if (ref_api) ref_api++;
Mathias Agopiande586972009-05-28 17:39:03 -0700372 }
373}
374
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800375static void* load_system_driver(const char* kind) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800376 ATRACE_CALL();
Mathias Agopian99381422013-04-23 20:52:29 +0200377 class MatchFile {
378 public:
Mathias Agopian65421432017-03-08 11:49:05 -0800379 static std::string find(const char* kind) {
380 std::string result;
Nicolas Capens776951f2015-11-06 10:10:21 -0500381 int emulationStatus = checkGlesEmulationStatus();
382 switch (emulationStatus) {
383 case 0:
Nicolas Capens776951f2015-11-06 10:10:21 -0500384#if defined(__LP64__)
bohu3adf9e12017-07-02 22:08:13 -0700385 result = "/vendor/lib64/egl/libGLES_android.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500386#else
bohu3adf9e12017-07-02 22:08:13 -0700387 result = "/vendor/lib/egl/libGLES_android.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500388#endif
389 return result;
390 case 1:
391 // Use host-side OpenGL through the "emulation" library
392#if defined(__LP64__)
bohu3adf9e12017-07-02 22:08:13 -0700393 result = std::string("/vendor/lib64/egl/lib") + kind + "_emulation.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500394#else
bohu3adf9e12017-07-02 22:08:13 -0700395 result = std::string("/vendor/lib/egl/lib") + kind + "_emulation.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500396#endif
397 return result;
Lingfeng Yang0f82d872017-08-11 01:18:00 -0700398 case 2:
399 // Use guest side swiftshader library
400#if defined(__LP64__)
401 result = std::string("/vendor/lib64/egl/lib") + kind + "_swiftshader.so";
402#else
403 result = std::string("/vendor/lib/egl/lib") + kind + "_swiftshader.so";
404#endif
405 return result;
Nicolas Capens776951f2015-11-06 10:10:21 -0500406 default:
407 // Not in emulator, or use other guest-side implementation
408 break;
409 }
410
Mathias Agopian65421432017-03-08 11:49:05 -0800411 std::string pattern = std::string("lib") + kind;
Mathias Agopian99381422013-04-23 20:52:29 +0200412 const char* const searchPaths[] = {
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800413#if defined(__LP64__)
414 "/vendor/lib64/egl",
415 "/system/lib64/egl"
416#else
Mathias Agopian99381422013-04-23 20:52:29 +0200417 "/vendor/lib/egl",
418 "/system/lib/egl"
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800419#endif
Mathias Agopian99381422013-04-23 20:52:29 +0200420 };
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700421
Mathias Agopian99381422013-04-23 20:52:29 +0200422 // first, we search for the exact name of the GLES userspace
423 // driver in both locations.
424 // i.e.:
425 // libGLES.so, or:
426 // libEGL.so, libGLESv1_CM.so, libGLESv2.so
427
428 for (size_t i=0 ; i<NELEM(searchPaths) ; i++) {
429 if (find(result, pattern, searchPaths[i], true)) {
430 return result;
431 }
432 }
433
434 // for compatibility with the old "egl.cfg" naming convention
435 // we look for files that match:
436 // libGLES_*.so, or:
437 // libEGL_*.so, libGLESv1_CM_*.so, libGLESv2_*.so
438
439 pattern.append("_");
440 for (size_t i=0 ; i<NELEM(searchPaths) ; i++) {
441 if (find(result, pattern, searchPaths[i], false)) {
442 return result;
443 }
444 }
445
446 // we didn't find the driver. gah.
447 result.clear();
448 return result;
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700449 }
Mathias Agopian99381422013-04-23 20:52:29 +0200450
451 private:
Mathias Agopian65421432017-03-08 11:49:05 -0800452 static bool find(std::string& result,
453 const std::string& pattern, const char* const search, bool exact) {
Mathias Agopian99381422013-04-23 20:52:29 +0200454 if (exact) {
Jesse Hall4aaa9602017-08-29 16:33:54 -0700455 std::string absolutePath = std::string(search) + "/" + pattern + ".so";
Mathias Agopian65421432017-03-08 11:49:05 -0800456 if (!access(absolutePath.c_str(), R_OK)) {
Mathias Agopian99381422013-04-23 20:52:29 +0200457 result = absolutePath;
458 return true;
459 }
460 return false;
461 }
462
463 DIR* d = opendir(search);
Yi Kong48a6cd22018-07-18 10:07:09 -0700464 if (d != nullptr) {
Mathias Agopian99381422013-04-23 20:52:29 +0200465 struct dirent* e;
Yi Kong48a6cd22018-07-18 10:07:09 -0700466 while ((e = readdir(d)) != nullptr) {
Mathias Agopian99381422013-04-23 20:52:29 +0200467 if (e->d_type == DT_DIR) {
468 continue;
469 }
470 if (!strcmp(e->d_name, "libGLES_android.so")) {
471 // always skip the software renderer
472 continue;
473 }
Mathias Agopian65421432017-03-08 11:49:05 -0800474 if (strstr(e->d_name, pattern.c_str()) == e->d_name) {
Mathias Agopian99381422013-04-23 20:52:29 +0200475 if (!strcmp(e->d_name + strlen(e->d_name) - 3, ".so")) {
Mathias Agopian65421432017-03-08 11:49:05 -0800476 result = std::string(search) + "/" + e->d_name;
Mathias Agopian99381422013-04-23 20:52:29 +0200477 closedir(d);
478 return true;
479 }
480 }
481 }
482 closedir(d);
483 }
484 return false;
485 }
486 };
487
488
Mathias Agopian65421432017-03-08 11:49:05 -0800489 std::string absolutePath = MatchFile::find(kind);
490 if (absolutePath.empty()) {
Mathias Agopian99381422013-04-23 20:52:29 +0200491 // this happens often, we don't want to log an error
Yi Kong48a6cd22018-07-18 10:07:09 -0700492 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700493 }
Mathias Agopian65421432017-03-08 11:49:05 -0800494 const char* const driver_absolute_path = absolutePath.c_str();
Mathias Agopiande586972009-05-28 17:39:03 -0700495
Jiyong Park5910dc72017-04-05 14:23:52 +0900496 // Try to load drivers from the 'sphal' namespace, if it exist. Fall back to
Justin Yunb7320302017-05-22 15:13:40 +0900497 // the original routine when the namespace does not exist.
Jiyong Park5910dc72017-04-05 14:23:52 +0900498 // See /system/core/rootdir/etc/ld.config.txt for the configuration of the
499 // sphal namespace.
Justin Yunb7320302017-05-22 15:13:40 +0900500 void* dso = do_android_load_sphal_library(driver_absolute_path,
501 RTLD_NOW | RTLD_LOCAL);
Yi Kong48a6cd22018-07-18 10:07:09 -0700502 if (dso == nullptr) {
Mathias Agopian8c173842009-09-20 16:01:02 -0700503 const char* err = dlerror();
Mathias Agopian65421432017-03-08 11:49:05 -0800504 ALOGE("load_driver(%s): %s", driver_absolute_path, err ? err : "unknown");
Yi Kong48a6cd22018-07-18 10:07:09 -0700505 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700506 }
507
Steve Block9d453682011-12-20 16:23:08 +0000508 ALOGD("loaded %s", driver_absolute_path);
Mathias Agopianbaca89c2009-08-20 19:09:34 -0700509
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800510 return dso;
511}
512
Cody Northrop1f00e172018-04-02 11:23:31 -0600513static void* load_angle_from_namespace(const char* kind, android_namespace_t* ns) {
514 const android_dlextinfo dlextinfo = {
515 .flags = ANDROID_DLEXT_USE_NAMESPACE,
516 .library_namespace = ns,
517 };
518
519 std::string name = std::string("lib") + kind + "_angle.so";
520
521 void* so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
522
523 if (so) {
524 ALOGD("dlopen_ext from APK (%s) success at %p", name.c_str(), so);
525 return so;
526 } else {
527 ALOGE("dlopen_ext(\"%s\") failed: %s", name.c_str(), dlerror());
528 }
529
530 return nullptr;
531}
532
Tim Van Pattena2a60a02018-11-09 16:51:15 -0700533static ANGLEPreference getAngleDevOption(const char* devOption) {
534 if (devOption == nullptr)
535 return ANGLE_PREFER_DEFAULT;
536
537 if (strcmp(devOption, "angle") == 0) {
538 return ANGLE_PREFER_ANGLE;
539 } else if (strcmp(devOption, "native") == 0) {
540 return ANGLE_PREFER_NATIVE;
541 }
542 return ANGLE_PREFER_DEFAULT;
543}
Cody Northropfc94a582018-10-30 15:13:30 -0600544
545static bool check_angle_rules(void* so, const char* app_name) {
546 bool use_angle = false;
547 const int rules_fd = android::GraphicsEnv::getInstance().getAngleRulesFd();
548 const long rules_offset = android::GraphicsEnv::getInstance().getAngleRulesOffset();
549 const long rules_length = android::GraphicsEnv::getInstance().getAngleRulesLength();
550
551 std::string app_name_str = app_name ? app_name : "";
552 char manufacturer[PROPERTY_VALUE_MAX];
553 char model[PROPERTY_VALUE_MAX];
554 property_get("ro.product.manufacturer", manufacturer, "UNSET");
555 property_get("ro.product.model", model, "UNSET");
556
Ian Elliott302bc772018-12-17 09:46:25 -0700557 fpANGLEGetFeatureSupportUtilAPIVersion ANGLEGetFeatureSupportUtilAPIVersion =
558 (fpANGLEGetFeatureSupportUtilAPIVersion)dlsym(so, "ANGLEGetFeatureSupportUtilAPIVersion");
Cody Northrop15666572018-10-30 15:47:20 -0600559
Ian Elliott302bc772018-12-17 09:46:25 -0700560 if (ANGLEGetFeatureSupportUtilAPIVersion) {
Cody Northrop15666572018-10-30 15:47:20 -0600561
562 // Negotiate the interface version by requesting most recent known to the platform
Ian Elliottb6125f52018-11-16 13:40:51 -0700563 unsigned int versionToUse = 2;
Ian Elliott302bc772018-12-17 09:46:25 -0700564 if ((ANGLEGetFeatureSupportUtilAPIVersion)(&versionToUse)) {
Cody Northrop15666572018-10-30 15:47:20 -0600565
566 // Add and remove versions below as needed
567 switch(versionToUse) {
Ian Elliottb6125f52018-11-16 13:40:51 -0700568 case 2: {
569 ALOGV("Using version 2 of ANGLE feature-support library");
570 void* rules_handle = nullptr;
571 int rules_version = 0;
572 void* system_info_handle = nullptr;
573
574 // Get the symbols for the feature-support-utility library:
575#define GET_SYMBOL(symbol) \
576 fp##symbol symbol = (fp##symbol)dlsym(so, #symbol); \
577 if (!symbol) { \
578 ALOGW("Cannot find "#symbol" in ANGLE feature-support library"); \
579 break; \
580 }
581 GET_SYMBOL(ANGLEAndroidParseRulesString);
582 GET_SYMBOL(ANGLEGetSystemInfo);
583 GET_SYMBOL(ANGLEAddDeviceInfoToSystemInfo);
584 GET_SYMBOL(ANGLEShouldBeUsedForApplication);
585 GET_SYMBOL(ANGLEFreeRulesHandle);
586 GET_SYMBOL(ANGLEFreeSystemInfoHandle);
587
588 // Read the contents of the file into a string:
589 off_t fileSize = rules_length;
590 off_t startOfContent = rules_offset;
591 lseek(rules_fd, startOfContent, SEEK_SET);
592 char *buffer = new char[fileSize + 1];
593 ssize_t numBytesRead = read(rules_fd, buffer, fileSize);
594 if (numBytesRead < 0) {
595 ALOGW("Cannot read rules file");
596 break;
597 }
598 if (numBytesRead == 0) {
599 ALOGW("Empty rules file");
600 break;
601 }
602 buffer[numBytesRead] = '\0';
603 std::string rule_file_contents = std::string(buffer);
604 delete[] buffer;
605
606 // Parse the rules, obtain the SystemInfo, and evaluate the
607 // application against the rules:
608 if (!(ANGLEAndroidParseRulesString)(rule_file_contents.c_str(),
609 &rules_handle, &rules_version)) {
610 ALOGW("ANGLE feature-support library cannot parse rules file");
611 break;
612 }
613 if (!(ANGLEGetSystemInfo)(&system_info_handle)) {
614 ALOGW("ANGLE feature-support library cannot obtain SystemInfo");
615 break;
616 }
617 if (!(ANGLEAddDeviceInfoToSystemInfo)(manufacturer, model, system_info_handle)) {
618 ALOGW("ANGLE feature-support library cannot add device info to SystemInfo");
619 break;
620 }
621 use_angle = (ANGLEShouldBeUsedForApplication)(rules_handle, rules_version,
622 system_info_handle, app_name_str.c_str());
623 (ANGLEFreeRulesHandle)(rules_handle);
624 (ANGLEFreeSystemInfoHandle)(system_info_handle);
625 }
626 break;
Cody Northrop15666572018-10-30 15:47:20 -0600627 default:
628 ALOGW("Cannot find supported version of ANGLE feature-support library, found version %u", versionToUse);
629 }
630 } else {
631 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 -0600632 }
633 } else {
Ian Elliott302bc772018-12-17 09:46:25 -0700634 ALOGW("Cannot find ANGLEGetFeatureSupportUtilAPIVersion function");
Cody Northropfc94a582018-10-30 15:13:30 -0600635 }
Cody Northrop15666572018-10-30 15:47:20 -0600636
Cody Northropfc94a582018-10-30 15:13:30 -0600637 ALOGV("Close temporarily-loaded ANGLE opt-in/out logic");
638 return use_angle;
639}
640
Cody Northrop6d082ef2018-09-11 09:42:31 -0600641static void* load_angle(const char* kind, android_namespace_t* ns, egl_connection_t* cnx) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600642 // Only attempt to load ANGLE libs
643 if (strcmp(kind, "EGL") != 0 && strcmp(kind, "GLESv2") != 0 && strcmp(kind, "GLESv1_CM") != 0)
644 return nullptr;
645
Cody Northrop1f00e172018-04-02 11:23:31 -0600646 std::string name;
Ian Elliottb058aff2018-08-09 12:00:55 -0600647 char prop[PROPERTY_VALUE_MAX];
Cody Northrop1f00e172018-04-02 11:23:31 -0600648
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600649 const char* app_name = android::GraphicsEnv::getInstance().getAngleAppName();
Tim Van Pattena2a60a02018-11-09 16:51:15 -0700650 const char* developer_opt_in = android::GraphicsEnv::getInstance().getAngleDeveloperOptIn();
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600651
Ian Elliott34474472018-09-05 10:57:07 -0600652 // Determine whether or not to use ANGLE:
Tim Van Pattena2a60a02018-11-09 16:51:15 -0700653 ANGLEPreference developer_option = getAngleDevOption(developer_opt_in);
654 bool use_angle = (developer_option == ANGLE_PREFER_ANGLE);
Ian Elliott34474472018-09-05 10:57:07 -0600655
656 if (use_angle) {
657 ALOGV("User set \"Developer Options\" to force the use of ANGLE");
Courtney Goeltzenleuchter734a4852018-10-23 16:26:24 -0600658 } else if (cnx->angleDecided) {
659 use_angle = cnx->useAngle;
Tim Van Pattena2a60a02018-11-09 16:51:15 -0700660 } else if (developer_option == ANGLE_PREFER_NATIVE) {
661 ALOGV("User set \"Developer Options\" to force the use of Native");
662 use_angle = false;
Ian Elliott34474472018-09-05 10:57:07 -0600663 } else {
664 // The "Developer Options" value wasn't set to force the use of ANGLE. Need to temporarily
665 // load ANGLE and call the updatable opt-in/out logic:
Courtney Goeltzenleuchter28b97dc2018-10-24 17:06:35 -0600666
Cody Northropdb444a92018-11-09 17:17:51 -0700667 // Check if ANGLE is enabled. Workaround for several bugs:
668 // b/119305693 b/119322355 b/119305887
669 // Something is not working correctly in the feature library
670 property_get("debug.angle.enable", prop, "0");
671 if (atoi(prop)) {
672 cnx->featureSo = load_angle_from_namespace("feature_support", ns);
673 }
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600674 if (cnx->featureSo) {
675 ALOGV("loaded ANGLE's opt-in/out logic from namespace");
Cody Northropfc94a582018-10-30 15:13:30 -0600676 use_angle = check_angle_rules(cnx->featureSo, app_name);
Ian Elliott34474472018-09-05 10:57:07 -0600677 } else {
678 // We weren't able to load and call the updateable opt-in/out logic.
679 // If we can't load the library, there is no ANGLE available.
680 use_angle = false;
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600681 ALOGV("Could not load the ANGLE opt-in/out logic, cannot use ANGLE.");
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600682 }
Courtney Goeltzenleuchter734a4852018-10-23 16:26:24 -0600683 cnx->angleDecided = true;
Cody Northrop1f00e172018-04-02 11:23:31 -0600684 }
Courtney Goeltzenleuchter985bc282018-11-02 14:24:55 -0600685 void* so = nullptr;
Ian Elliott34474472018-09-05 10:57:07 -0600686 if (use_angle) {
687 so = load_angle_from_namespace(kind, ns);
688 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600689
690 if (so) {
Ian Elliott34474472018-09-05 10:57:07 -0600691 ALOGV("Loaded ANGLE %s library for %s (instead of native)",
Ian Elliottb058aff2018-08-09 12:00:55 -0600692 kind, app_name ? app_name : "nullptr");
Ian Elliottb058aff2018-08-09 12:00:55 -0600693 property_get("debug.hwui.renderer", prop, "UNSET");
Ian Elliott34474472018-09-05 10:57:07 -0600694 ALOGV("Skia's renderer set to %s", prop);
Cody Northrop1f00e172018-04-02 11:23:31 -0600695 cnx->useAngle = true;
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600696
697 EGLint angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE;
698
699 char prop[PROPERTY_VALUE_MAX];
700 property_get("debug.angle.backend", prop, "0");
701 switch (atoi(prop)) {
702 case 1:
703 ALOGV("%s: Requesting OpenGLES back-end", __FUNCTION__);
704 angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE;
705 break;
706 case 2:
707 ALOGV("%s: Requesting Vulkan back-end", __FUNCTION__);
708 angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE;
709 break;
710 default:
711 break;
712 }
713
714 cnx->angleBackend = angleBackendDefault;
715 if (!cnx->vendorEGL && (cnx->angleBackend == EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE)) {
716 // Find and load vendor libEGL for ANGLE's GL back-end to use.
Cody Northrop1f00e172018-04-02 11:23:31 -0600717 cnx->vendorEGL = load_system_driver("EGL");
718 }
719 return so;
Ian Elliottb058aff2018-08-09 12:00:55 -0600720 } else {
Ian Elliott34474472018-09-05 10:57:07 -0600721 ALOGV("Loaded native %s library for %s (instead of ANGLE)",
Ian Elliottb058aff2018-08-09 12:00:55 -0600722 kind, app_name ? app_name : "nullptr");
Cody Northrop1f00e172018-04-02 11:23:31 -0600723 }
724
725 return nullptr;
726}
727
Mathias Agopian311b4792017-02-28 15:00:49 -0800728static const char* HAL_SUBNAME_KEY_PROPERTIES[2] = {
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800729 "ro.hardware.egl",
730 "ro.board.platform",
Mathias Agopian311b4792017-02-28 15:00:49 -0800731};
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800732
733static void* load_updated_driver(const char* kind, android_namespace_t* ns) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800734 ATRACE_CALL();
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800735 const android_dlextinfo dlextinfo = {
736 .flags = ANDROID_DLEXT_USE_NAMESPACE,
737 .library_namespace = ns,
738 };
739 void* so = nullptr;
740 char prop[PROPERTY_VALUE_MAX + 1];
741 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
742 if (property_get(key, prop, nullptr) > 0) {
Mathias Agopian65421432017-03-08 11:49:05 -0800743 std::string name = std::string("lib") + kind + "_" + prop + ".so";
744 so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
Mathias Agopian311b4792017-02-28 15:00:49 -0800745 if (so) {
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800746 return so;
Mathias Agopian311b4792017-02-28 15:00:49 -0800747 }
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800748 }
749 }
750 return nullptr;
751}
752
753void *Loader::load_driver(const char* kind,
754 egl_connection_t* cnx, uint32_t mask)
755{
Jesse Hall1508ae62017-01-19 17:43:26 -0800756 ATRACE_CALL();
757
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800758 void* dso = nullptr;
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600759 android_namespace_t* ns = android::GraphicsEnv::getInstance().getAngleNamespace();
Cody Northrop6d082ef2018-09-11 09:42:31 -0600760 if (ns) {
761 dso = load_angle(kind, ns, cnx);
762 }
Jiyong Parka243e5d2017-06-21 12:26:51 +0900763#ifndef __ANDROID_VNDK__
Cody Northrop1f00e172018-04-02 11:23:31 -0600764 if (!dso) {
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600765 android_namespace_t* ns = android::GraphicsEnv::getInstance().getDriverNamespace();
Cody Northrop1f00e172018-04-02 11:23:31 -0600766 if (ns) {
767 dso = load_updated_driver(kind, ns);
768 }
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800769 }
Jiyong Parka243e5d2017-06-21 12:26:51 +0900770#endif
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800771 if (!dso) {
772 dso = load_system_driver(kind);
773 if (!dso)
Yi Kong48a6cd22018-07-18 10:07:09 -0700774 return nullptr;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800775 }
776
Mathias Agopiande586972009-05-28 17:39:03 -0700777 if (mask & EGL) {
778 getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress");
779
Jesse Hall94cdba92013-07-11 09:40:35 -0700780 ALOGE_IF(!getProcAddress,
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800781 "can't find eglGetProcAddress() in EGL driver library");
Mathias Agopiande586972009-05-28 17:39:03 -0700782
Mathias Agopian618fa102009-10-14 02:06:37 -0700783 egl_t* egl = &cnx->egl;
Mathias Agopiande586972009-05-28 17:39:03 -0700784 __eglMustCastToProperFunctionPointerType* curr =
785 (__eglMustCastToProperFunctionPointerType*)egl;
786 char const * const * api = egl_names;
787 while (*api) {
788 char const * name = *api;
Jesse Hall94cdba92013-07-11 09:40:35 -0700789 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700790 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700791 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700792 // couldn't find the entry-point, use eglGetProcAddress()
793 f = getProcAddress(name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700794 if (f == nullptr) {
795 f = (__eglMustCastToProperFunctionPointerType)nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700796 }
797 }
798 *curr++ = f;
799 api++;
800 }
801 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700802
Mathias Agopiande586972009-05-28 17:39:03 -0700803 if (mask & GLESv1_CM) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700804 init_api(dso, gl_names_1, gl_names,
Mathias Agopian618fa102009-10-14 02:06:37 -0700805 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800806 &cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl,
Mathias Agopian618fa102009-10-14 02:06:37 -0700807 getProcAddress);
Mathias Agopiande586972009-05-28 17:39:03 -0700808 }
809
810 if (mask & GLESv2) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700811 init_api(dso, gl_names, nullptr,
Mathias Agopian618fa102009-10-14 02:06:37 -0700812 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800813 &cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl,
Mathias Agopiande586972009-05-28 17:39:03 -0700814 getProcAddress);
815 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700816
Mathias Agopiande586972009-05-28 17:39:03 -0700817 return dso;
818}
819
820// ----------------------------------------------------------------------------
821}; // namespace android
822// ----------------------------------------------------------------------------