blob: 3d1eb14d06316a58635436a54622a3fc90e98068 [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
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600278void Loader::close(egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700279{
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600280 driver_t* hnd = (driver_t*) cnx->dso;
Mathias Agopiande586972009-05-28 17:39:03 -0700281 delete hnd;
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600282 cnx->dso = nullptr;
283
284 if (cnx->featureSo) {
285 dlclose(cnx->featureSo);
286 cnx->featureSo = nullptr;
287 }
288
289 cnx->angleDecided = false;
290 cnx->useAngle = false;
291
292 if (cnx->vendorEGL) {
293 dlclose(cnx->vendorEGL);
294 cnx->vendorEGL = nullptr;
295 }
Mathias Agopiande586972009-05-28 17:39:03 -0700296}
297
Jesse Hall94cdba92013-07-11 09:40:35 -0700298void Loader::init_api(void* dso,
299 char const * const * api,
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700300 char const * const * ref_api,
Jesse Hall94cdba92013-07-11 09:40:35 -0700301 __eglMustCastToProperFunctionPointerType* curr,
302 getProcAddressType getProcAddress)
Mathias Agopiande586972009-05-28 17:39:03 -0700303{
Jesse Hall1508ae62017-01-19 17:43:26 -0800304 ATRACE_CALL();
305
Mathias Agopian7773c432012-02-13 20:06:08 -0800306 const ssize_t SIZE = 256;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700307 char scrap[SIZE];
Mathias Agopiande586972009-05-28 17:39:03 -0700308 while (*api) {
309 char const * name = *api;
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700310 if (ref_api) {
311 char const * ref_name = *ref_api;
312 if (std::strcmp(name, ref_name) != 0) {
313 *curr++ = nullptr;
314 ref_api++;
315 continue;
316 }
317 }
318
Jesse Hall94cdba92013-07-11 09:40:35 -0700319 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700320 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700321 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700322 // couldn't find the entry-point, use eglGetProcAddress()
323 f = getProcAddress(name);
324 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700325 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700326 // Try without the OES postfix
327 ssize_t index = ssize_t(strlen(name)) - 3;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700328 if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) {
Mathias Agopiande586972009-05-28 17:39:03 -0700329 strncpy(scrap, name, index);
330 scrap[index] = 0;
331 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000332 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700333 }
334 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700335 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700336 // Try with the OES postfix
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700337 ssize_t index = ssize_t(strlen(name)) - 3;
338 if (index>0 && strcmp(name+index, "OES")) {
339 snprintf(scrap, SIZE, "%sOES", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700340 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000341 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700342 }
343 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700344 if (f == nullptr) {
Steve Block9d453682011-12-20 16:23:08 +0000345 //ALOGD("%s", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700346 f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented;
Mathias Agopian48d438d2012-01-28 21:44:00 -0800347
348 /*
349 * GL_EXT_debug_label is special, we always report it as
350 * supported, it's handled by GLES_trace. If GLES_trace is not
351 * enabled, then these are no-ops.
352 */
353 if (!strcmp(name, "glInsertEventMarkerEXT")) {
354 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
355 } else if (!strcmp(name, "glPushGroupMarkerEXT")) {
356 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
357 } else if (!strcmp(name, "glPopGroupMarkerEXT")) {
358 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
359 }
Mathias Agopiande586972009-05-28 17:39:03 -0700360 }
361 *curr++ = f;
362 api++;
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700363 if (ref_api) ref_api++;
Mathias Agopiande586972009-05-28 17:39:03 -0700364 }
365}
366
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800367static void* load_system_driver(const char* kind) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800368 ATRACE_CALL();
Mathias Agopian99381422013-04-23 20:52:29 +0200369 class MatchFile {
370 public:
Mathias Agopian65421432017-03-08 11:49:05 -0800371 static std::string find(const char* kind) {
372 std::string result;
Nicolas Capens776951f2015-11-06 10:10:21 -0500373 int emulationStatus = checkGlesEmulationStatus();
374 switch (emulationStatus) {
375 case 0:
Nicolas Capens776951f2015-11-06 10:10:21 -0500376#if defined(__LP64__)
bohu3adf9e12017-07-02 22:08:13 -0700377 result = "/vendor/lib64/egl/libGLES_android.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500378#else
bohu3adf9e12017-07-02 22:08:13 -0700379 result = "/vendor/lib/egl/libGLES_android.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500380#endif
381 return result;
382 case 1:
383 // Use host-side OpenGL through the "emulation" library
384#if defined(__LP64__)
bohu3adf9e12017-07-02 22:08:13 -0700385 result = std::string("/vendor/lib64/egl/lib") + kind + "_emulation.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500386#else
bohu3adf9e12017-07-02 22:08:13 -0700387 result = std::string("/vendor/lib/egl/lib") + kind + "_emulation.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500388#endif
389 return result;
Lingfeng Yang0f82d872017-08-11 01:18:00 -0700390 case 2:
391 // Use guest side swiftshader library
392#if defined(__LP64__)
393 result = std::string("/vendor/lib64/egl/lib") + kind + "_swiftshader.so";
394#else
395 result = std::string("/vendor/lib/egl/lib") + kind + "_swiftshader.so";
396#endif
397 return result;
Nicolas Capens776951f2015-11-06 10:10:21 -0500398 default:
399 // Not in emulator, or use other guest-side implementation
400 break;
401 }
402
Mathias Agopian65421432017-03-08 11:49:05 -0800403 std::string pattern = std::string("lib") + kind;
Mathias Agopian99381422013-04-23 20:52:29 +0200404 const char* const searchPaths[] = {
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800405#if defined(__LP64__)
406 "/vendor/lib64/egl",
407 "/system/lib64/egl"
408#else
Mathias Agopian99381422013-04-23 20:52:29 +0200409 "/vendor/lib/egl",
410 "/system/lib/egl"
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800411#endif
Mathias Agopian99381422013-04-23 20:52:29 +0200412 };
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700413
Mathias Agopian99381422013-04-23 20:52:29 +0200414 // first, we search for the exact name of the GLES userspace
415 // driver in both locations.
416 // i.e.:
417 // libGLES.so, or:
418 // libEGL.so, libGLESv1_CM.so, libGLESv2.so
419
420 for (size_t i=0 ; i<NELEM(searchPaths) ; i++) {
421 if (find(result, pattern, searchPaths[i], true)) {
422 return result;
423 }
424 }
425
426 // for compatibility with the old "egl.cfg" naming convention
427 // we look for files that match:
428 // libGLES_*.so, or:
429 // libEGL_*.so, libGLESv1_CM_*.so, libGLESv2_*.so
430
431 pattern.append("_");
432 for (size_t i=0 ; i<NELEM(searchPaths) ; i++) {
433 if (find(result, pattern, searchPaths[i], false)) {
434 return result;
435 }
436 }
437
438 // we didn't find the driver. gah.
439 result.clear();
440 return result;
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700441 }
Mathias Agopian99381422013-04-23 20:52:29 +0200442
443 private:
Mathias Agopian65421432017-03-08 11:49:05 -0800444 static bool find(std::string& result,
445 const std::string& pattern, const char* const search, bool exact) {
Mathias Agopian99381422013-04-23 20:52:29 +0200446 if (exact) {
Jesse Hall4aaa9602017-08-29 16:33:54 -0700447 std::string absolutePath = std::string(search) + "/" + pattern + ".so";
Mathias Agopian65421432017-03-08 11:49:05 -0800448 if (!access(absolutePath.c_str(), R_OK)) {
Mathias Agopian99381422013-04-23 20:52:29 +0200449 result = absolutePath;
450 return true;
451 }
452 return false;
453 }
454
455 DIR* d = opendir(search);
Yi Kong48a6cd22018-07-18 10:07:09 -0700456 if (d != nullptr) {
Mathias Agopian99381422013-04-23 20:52:29 +0200457 struct dirent* e;
Yi Kong48a6cd22018-07-18 10:07:09 -0700458 while ((e = readdir(d)) != nullptr) {
Mathias Agopian99381422013-04-23 20:52:29 +0200459 if (e->d_type == DT_DIR) {
460 continue;
461 }
462 if (!strcmp(e->d_name, "libGLES_android.so")) {
463 // always skip the software renderer
464 continue;
465 }
Mathias Agopian65421432017-03-08 11:49:05 -0800466 if (strstr(e->d_name, pattern.c_str()) == e->d_name) {
Mathias Agopian99381422013-04-23 20:52:29 +0200467 if (!strcmp(e->d_name + strlen(e->d_name) - 3, ".so")) {
Mathias Agopian65421432017-03-08 11:49:05 -0800468 result = std::string(search) + "/" + e->d_name;
Mathias Agopian99381422013-04-23 20:52:29 +0200469 closedir(d);
470 return true;
471 }
472 }
473 }
474 closedir(d);
475 }
476 return false;
477 }
478 };
479
480
Mathias Agopian65421432017-03-08 11:49:05 -0800481 std::string absolutePath = MatchFile::find(kind);
482 if (absolutePath.empty()) {
Mathias Agopian99381422013-04-23 20:52:29 +0200483 // this happens often, we don't want to log an error
Yi Kong48a6cd22018-07-18 10:07:09 -0700484 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700485 }
Mathias Agopian65421432017-03-08 11:49:05 -0800486 const char* const driver_absolute_path = absolutePath.c_str();
Mathias Agopiande586972009-05-28 17:39:03 -0700487
Jiyong Park5910dc72017-04-05 14:23:52 +0900488 // Try to load drivers from the 'sphal' namespace, if it exist. Fall back to
Justin Yunb7320302017-05-22 15:13:40 +0900489 // the original routine when the namespace does not exist.
Jiyong Park5910dc72017-04-05 14:23:52 +0900490 // See /system/core/rootdir/etc/ld.config.txt for the configuration of the
491 // sphal namespace.
Justin Yunb7320302017-05-22 15:13:40 +0900492 void* dso = do_android_load_sphal_library(driver_absolute_path,
493 RTLD_NOW | RTLD_LOCAL);
Yi Kong48a6cd22018-07-18 10:07:09 -0700494 if (dso == nullptr) {
Mathias Agopian8c173842009-09-20 16:01:02 -0700495 const char* err = dlerror();
Mathias Agopian65421432017-03-08 11:49:05 -0800496 ALOGE("load_driver(%s): %s", driver_absolute_path, err ? err : "unknown");
Yi Kong48a6cd22018-07-18 10:07:09 -0700497 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700498 }
499
Steve Block9d453682011-12-20 16:23:08 +0000500 ALOGD("loaded %s", driver_absolute_path);
Mathias Agopianbaca89c2009-08-20 19:09:34 -0700501
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800502 return dso;
503}
504
Cody Northrop1f00e172018-04-02 11:23:31 -0600505static void* load_angle_from_namespace(const char* kind, android_namespace_t* ns) {
506 const android_dlextinfo dlextinfo = {
507 .flags = ANDROID_DLEXT_USE_NAMESPACE,
508 .library_namespace = ns,
509 };
510
511 std::string name = std::string("lib") + kind + "_angle.so";
512
513 void* so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
514
515 if (so) {
516 ALOGD("dlopen_ext from APK (%s) success at %p", name.c_str(), so);
517 return so;
518 } else {
519 ALOGE("dlopen_ext(\"%s\") failed: %s", name.c_str(), dlerror());
520 }
521
522 return nullptr;
523}
524
Ian Elliott34474472018-09-05 10:57:07 -0600525static ANGLEPreference getAnglePref(const char* app_pref) {
526 if (app_pref == nullptr)
527 return ANGLE_NO_PREFERENCE;
528
529 if (strcmp(app_pref, "angle") == 0) {
530 return ANGLE_PREFER_ANGLE;
531 } else if (strcmp(app_pref, "native") == 0) {
532 return ANGLE_PREFER_NATIVE;
533 }
534 return ANGLE_NO_PREFERENCE;
535}
536
Cody Northrop6d082ef2018-09-11 09:42:31 -0600537static void* load_angle(const char* kind, android_namespace_t* ns, egl_connection_t* cnx) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600538 // Only attempt to load ANGLE libs
539 if (strcmp(kind, "EGL") != 0 && strcmp(kind, "GLESv2") != 0 && strcmp(kind, "GLESv1_CM") != 0)
540 return nullptr;
541
542 void* so = nullptr;
543 std::string name;
Ian Elliottb058aff2018-08-09 12:00:55 -0600544 char prop[PROPERTY_VALUE_MAX];
Cody Northrop1f00e172018-04-02 11:23:31 -0600545
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600546 const char* app_name = android::GraphicsEnv::getInstance().getAngleAppName();
547 const char* app_pref = android::GraphicsEnv::getInstance().getAngleAppPref();
548 bool developer_opt_in = android::GraphicsEnv::getInstance().getAngleDeveloperOptIn();
549 const int rules_fd = android::GraphicsEnv::getInstance().getAngleRulesFd();
550 const long rules_offset = android::GraphicsEnv::getInstance().getAngleRulesOffset();
551 const long rules_length = android::GraphicsEnv::getInstance().getAngleRulesLength();
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600552
Ian Elliott34474472018-09-05 10:57:07 -0600553 // Determine whether or not to use ANGLE:
554 ANGLEPreference developer_option = developer_opt_in ? ANGLE_PREFER_ANGLE : ANGLE_NO_PREFERENCE;
555 bool use_angle = (developer_option == ANGLE_PREFER_ANGLE);
556
557 if (use_angle) {
558 ALOGV("User set \"Developer Options\" to force the use of ANGLE");
Courtney Goeltzenleuchter734a4852018-10-23 16:26:24 -0600559 } else if (cnx->angleDecided) {
560 use_angle = cnx->useAngle;
Ian Elliott34474472018-09-05 10:57:07 -0600561 } else {
562 // The "Developer Options" value wasn't set to force the use of ANGLE. Need to temporarily
563 // load ANGLE and call the updatable opt-in/out logic:
564 std::string app_name_str = app_name ? app_name : "";
565 char manufacturer[PROPERTY_VALUE_MAX];
566 char model[PROPERTY_VALUE_MAX];
567 property_get("ro.product.manufacturer", manufacturer, "UNSET");
568 property_get("ro.product.model", model, "UNSET");
Courtney Goeltzenleuchter28b97dc2018-10-24 17:06:35 -0600569
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600570 cnx->featureSo = load_angle_from_namespace("feature_support", ns);
571 if (cnx->featureSo) {
572 ALOGV("loaded ANGLE's opt-in/out logic from namespace");
Ian Elliott380bf852018-10-17 14:56:04 -0600573 bool use_version0_API = false;
574 bool use_version1_API = false;
575 fpANGLEGetUtilityAPI ANGLEGetUtilityAPI =
576 (fpANGLEGetUtilityAPI)dlsym(so, "ANGLEGetUtilityAPI");
577 if (ANGLEGetUtilityAPI) {
578 unsigned int versionToUse = 1;
579 if ((ANGLEGetUtilityAPI)(&versionToUse)) {
580 if (versionToUse == 1) {
581 use_version1_API = true;
582 } else {
583 use_version0_API = true;
584 }
585 }
Courtney Goeltzenleuchter075437f2018-10-08 19:35:31 -0600586 } else {
Ian Elliott380bf852018-10-17 14:56:04 -0600587 use_version0_API = true;
Ian Elliott34474472018-09-05 10:57:07 -0600588 }
Ian Elliott380bf852018-10-17 14:56:04 -0600589 if (use_version1_API) {
590 // Use the new version 1 API to determine if the
591 // application should use the ANGLE or the native driver.
592 fpAndroidUseANGLEForApplication AndroidUseANGLEForApplication =
593 (fpAndroidUseANGLEForApplication)dlsym(so, "AndroidUseANGLEForApplication");
594 if (AndroidUseANGLEForApplication) {
595 use_angle = (AndroidUseANGLEForApplication)(rules_fd, rules_offset,
596 rules_length, app_name_str.c_str(),
597 manufacturer, model);
598 } else {
599 ALOGW("Cannot find AndroidUseANGLEForApplication in library");
600 }
601 } else if (use_version0_API) {
602 // Use the old version 0 API to determine if the
603 // application should use the ANGLE or the native driver.
604 fpANGLEUseForApplication ANGLEUseForApplication =
605 (fpANGLEUseForApplication)dlsym(so, "ANGLEUseForApplication");
606 if (ANGLEUseForApplication) {
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600607 ANGLEPreference app_preference =
608 getAnglePref(android::GraphicsEnv::getInstance().getAngleAppPref());
Ian Elliott380bf852018-10-17 14:56:04 -0600609 use_angle = (ANGLEUseForApplication)(app_name_str.c_str(), manufacturer, model,
610 developer_option, app_preference);
611 ALOGV("Result of opt-in/out logic is %s", use_angle ? "true" : "false");
612 } else {
613 ALOGW("Cannot find ANGLEUseForApplication in library");
614 }
615 }
Ian Elliott34474472018-09-05 10:57:07 -0600616 } else {
617 // We weren't able to load and call the updateable opt-in/out logic.
618 // If we can't load the library, there is no ANGLE available.
619 use_angle = false;
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600620 ALOGV("Could not load the ANGLE opt-in/out logic, cannot use ANGLE.");
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600621 }
Courtney Goeltzenleuchter734a4852018-10-23 16:26:24 -0600622 cnx->angleDecided = true;
Cody Northrop1f00e172018-04-02 11:23:31 -0600623 }
Ian Elliott34474472018-09-05 10:57:07 -0600624 if (use_angle) {
625 so = load_angle_from_namespace(kind, ns);
626 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600627
628 if (so) {
Ian Elliott34474472018-09-05 10:57:07 -0600629 ALOGV("Loaded ANGLE %s library for %s (instead of native)",
Ian Elliottb058aff2018-08-09 12:00:55 -0600630 kind, app_name ? app_name : "nullptr");
Ian Elliottb058aff2018-08-09 12:00:55 -0600631 property_get("debug.hwui.renderer", prop, "UNSET");
Ian Elliott34474472018-09-05 10:57:07 -0600632 ALOGV("Skia's renderer set to %s", prop);
Cody Northrop1f00e172018-04-02 11:23:31 -0600633 cnx->useAngle = true;
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600634
635 EGLint angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE;
636
637 char prop[PROPERTY_VALUE_MAX];
638 property_get("debug.angle.backend", prop, "0");
639 switch (atoi(prop)) {
640 case 1:
641 ALOGV("%s: Requesting OpenGLES back-end", __FUNCTION__);
642 angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE;
643 break;
644 case 2:
645 ALOGV("%s: Requesting Vulkan back-end", __FUNCTION__);
646 angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE;
647 break;
648 default:
649 break;
650 }
651
652 cnx->angleBackend = angleBackendDefault;
653 if (!cnx->vendorEGL && (cnx->angleBackend == EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE)) {
654 // Find and load vendor libEGL for ANGLE's GL back-end to use.
Cody Northrop1f00e172018-04-02 11:23:31 -0600655 cnx->vendorEGL = load_system_driver("EGL");
656 }
657 return so;
Ian Elliottb058aff2018-08-09 12:00:55 -0600658 } else {
Ian Elliott34474472018-09-05 10:57:07 -0600659 ALOGV("Loaded native %s library for %s (instead of ANGLE)",
Ian Elliottb058aff2018-08-09 12:00:55 -0600660 kind, app_name ? app_name : "nullptr");
Cody Northrop1f00e172018-04-02 11:23:31 -0600661 }
662
663 return nullptr;
664}
665
Mathias Agopian311b4792017-02-28 15:00:49 -0800666static const char* HAL_SUBNAME_KEY_PROPERTIES[2] = {
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800667 "ro.hardware.egl",
668 "ro.board.platform",
Mathias Agopian311b4792017-02-28 15:00:49 -0800669};
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800670
671static void* load_updated_driver(const char* kind, android_namespace_t* ns) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800672 ATRACE_CALL();
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800673 const android_dlextinfo dlextinfo = {
674 .flags = ANDROID_DLEXT_USE_NAMESPACE,
675 .library_namespace = ns,
676 };
677 void* so = nullptr;
678 char prop[PROPERTY_VALUE_MAX + 1];
679 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
680 if (property_get(key, prop, nullptr) > 0) {
Mathias Agopian65421432017-03-08 11:49:05 -0800681 std::string name = std::string("lib") + kind + "_" + prop + ".so";
682 so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
Mathias Agopian311b4792017-02-28 15:00:49 -0800683 if (so) {
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800684 return so;
Mathias Agopian311b4792017-02-28 15:00:49 -0800685 }
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800686 }
687 }
688 return nullptr;
689}
690
691void *Loader::load_driver(const char* kind,
692 egl_connection_t* cnx, uint32_t mask)
693{
Jesse Hall1508ae62017-01-19 17:43:26 -0800694 ATRACE_CALL();
695
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800696 void* dso = nullptr;
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600697 android_namespace_t* ns = android::GraphicsEnv::getInstance().getAngleNamespace();
Cody Northrop6d082ef2018-09-11 09:42:31 -0600698 if (ns) {
699 dso = load_angle(kind, ns, cnx);
700 }
Jiyong Parka243e5d2017-06-21 12:26:51 +0900701#ifndef __ANDROID_VNDK__
Cody Northrop1f00e172018-04-02 11:23:31 -0600702 if (!dso) {
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600703 android_namespace_t* ns = android::GraphicsEnv::getInstance().getDriverNamespace();
Cody Northrop1f00e172018-04-02 11:23:31 -0600704 if (ns) {
705 dso = load_updated_driver(kind, ns);
706 }
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800707 }
Jiyong Parka243e5d2017-06-21 12:26:51 +0900708#endif
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800709 if (!dso) {
710 dso = load_system_driver(kind);
711 if (!dso)
Yi Kong48a6cd22018-07-18 10:07:09 -0700712 return nullptr;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800713 }
714
Mathias Agopiande586972009-05-28 17:39:03 -0700715 if (mask & EGL) {
716 getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress");
717
Jesse Hall94cdba92013-07-11 09:40:35 -0700718 ALOGE_IF(!getProcAddress,
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800719 "can't find eglGetProcAddress() in EGL driver library");
Mathias Agopiande586972009-05-28 17:39:03 -0700720
Mathias Agopian618fa102009-10-14 02:06:37 -0700721 egl_t* egl = &cnx->egl;
Mathias Agopiande586972009-05-28 17:39:03 -0700722 __eglMustCastToProperFunctionPointerType* curr =
723 (__eglMustCastToProperFunctionPointerType*)egl;
724 char const * const * api = egl_names;
725 while (*api) {
726 char const * name = *api;
Jesse Hall94cdba92013-07-11 09:40:35 -0700727 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700728 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700729 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700730 // couldn't find the entry-point, use eglGetProcAddress()
731 f = getProcAddress(name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700732 if (f == nullptr) {
733 f = (__eglMustCastToProperFunctionPointerType)nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700734 }
735 }
736 *curr++ = f;
737 api++;
738 }
739 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700740
Mathias Agopiande586972009-05-28 17:39:03 -0700741 if (mask & GLESv1_CM) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700742 init_api(dso, gl_names_1, gl_names,
Mathias Agopian618fa102009-10-14 02:06:37 -0700743 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800744 &cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl,
Mathias Agopian618fa102009-10-14 02:06:37 -0700745 getProcAddress);
Mathias Agopiande586972009-05-28 17:39:03 -0700746 }
747
748 if (mask & GLESv2) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700749 init_api(dso, gl_names, nullptr,
Mathias Agopian618fa102009-10-14 02:06:37 -0700750 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800751 &cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl,
Mathias Agopiande586972009-05-28 17:39:03 -0700752 getProcAddress);
753 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700754
Mathias Agopiande586972009-05-28 17:39:03 -0700755 return dso;
756}
757
758// ----------------------------------------------------------------------------
759}; // namespace android
760// ----------------------------------------------------------------------------