blob: 1421a4867a77f4af97150fa27a4302c6b531508d [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
Mathias Agopian65421432017-03-08 11:49:05 -080036#include "egl_trace.h"
Mathias Agopian1cadb252011-05-23 17:26:14 -070037#include "egldefs.h"
Mathias Agopiande586972009-05-28 17:39:03 -070038
Jiyong Park5910dc72017-04-05 14:23:52 +090039extern "C" {
40 android_namespace_t* android_get_exported_namespace(const char*);
Ian Elliott34474472018-09-05 10:57:07 -060041
42 // TODO(ianelliott@): Get this from an ANGLE header:
43 typedef enum ANGLEPreference {
44 ANGLE_NO_PREFERENCE = 0,
45 ANGLE_PREFER_NATIVE = 1,
46 ANGLE_PREFER_ANGLE = 2,
47 } ANGLEPreference;
48
49 // TODO(ianelliott@): Get this from an ANGLE header:
50 typedef bool (*fpANGLEUseForApplication)(const char* appName, const char* deviceMfr,
51 const char* deviceModel, ANGLEPreference developerOption,
52 ANGLEPreference appPreference);
Jiyong Park5910dc72017-04-05 14:23:52 +090053}
54
Mathias Agopiande586972009-05-28 17:39:03 -070055// ----------------------------------------------------------------------------
56namespace android {
57// ----------------------------------------------------------------------------
58
59
60/*
Mathias Agopian99381422013-04-23 20:52:29 +020061 * EGL userspace drivers must be provided either:
62 * - as a single library:
63 * /vendor/lib/egl/libGLES.so
64 *
65 * - as separate libraries:
66 * /vendor/lib/egl/libEGL.so
67 * /vendor/lib/egl/libGLESv1_CM.so
68 * /vendor/lib/egl/libGLESv2.so
69 *
70 * The software renderer for the emulator must be provided as a single
71 * library at:
72 *
73 * /system/lib/egl/libGLES_android.so
74 *
75 *
76 * For backward compatibility and to facilitate the transition to
77 * this new naming scheme, the loader will additionally look for:
Jesse Hall94cdba92013-07-11 09:40:35 -070078 *
Mathias Agopian99381422013-04-23 20:52:29 +020079 * /{vendor|system}/lib/egl/lib{GLES | [EGL|GLESv1_CM|GLESv2]}_*.so
Jesse Hall94cdba92013-07-11 09:40:35 -070080 *
Mathias Agopiande586972009-05-28 17:39:03 -070081 */
82
Mathias Agopian65421432017-03-08 11:49:05 -080083Loader& Loader::getInstance() {
84 static Loader loader;
85 return loader;
86}
Mathias Agopiande586972009-05-28 17:39:03 -070087
David 'Digit' Turner80b30c22011-08-26 17:38:47 +020088/* This function is called to check whether we run inside the emulator,
89 * and if this is the case whether GLES GPU emulation is supported.
90 *
91 * Returned values are:
92 * -1 -> not running inside the emulator
93 * 0 -> running inside the emulator, but GPU emulation not supported
94 * 1 -> running inside the emulator, GPU emulation is supported
Nicolas Capens776951f2015-11-06 10:10:21 -050095 * through the "emulation" host-side OpenGL ES implementation.
96 * 2 -> running inside the emulator, GPU emulation is supported
97 * through a guest-side vendor driver's OpenGL ES implementation.
David 'Digit' Turner80b30c22011-08-26 17:38:47 +020098 */
99static int
100checkGlesEmulationStatus(void)
101{
102 /* We're going to check for the following kernel parameters:
103 *
104 * qemu=1 -> tells us that we run inside the emulator
105 * android.qemu.gles=<number> -> tells us the GLES GPU emulation status
106 *
107 * Note that we will return <number> if we find it. This let us support
108 * more additionnal emulation modes in the future.
109 */
110 char prop[PROPERTY_VALUE_MAX];
111 int result = -1;
112
113 /* First, check for qemu=1 */
114 property_get("ro.kernel.qemu",prop,"0");
115 if (atoi(prop) != 1)
116 return -1;
117
118 /* We are in the emulator, get GPU status value */
bohu69e5b1a2016-02-19 17:15:20 -0800119 property_get("qemu.gles",prop,"0");
David 'Digit' Turner80b30c22011-08-26 17:38:47 +0200120 return atoi(prop);
121}
122
Jesse Hall1508ae62017-01-19 17:43:26 -0800123static void* do_dlopen(const char* path, int mode) {
124 ATRACE_CALL();
125 return dlopen(path, mode);
126}
127
Jiyong Park5910dc72017-04-05 14:23:52 +0900128static void* do_android_dlopen_ext(const char* path, int mode, const android_dlextinfo* info) {
129 ATRACE_CALL();
130 return android_dlopen_ext(path, mode, info);
131}
132
Justin Yunb7320302017-05-22 15:13:40 +0900133static void* do_android_load_sphal_library(const char* path, int mode) {
134 ATRACE_CALL();
135 return android_load_sphal_library(path, mode);
136}
137
Mathias Agopiande586972009-05-28 17:39:03 -0700138// ----------------------------------------------------------------------------
139
Jesse Hall94cdba92013-07-11 09:40:35 -0700140Loader::driver_t::driver_t(void* gles)
Mathias Agopiande586972009-05-28 17:39:03 -0700141{
142 dso[0] = gles;
143 for (size_t i=1 ; i<NELEM(dso) ; i++)
Yi Kong48a6cd22018-07-18 10:07:09 -0700144 dso[i] = nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700145}
146
Jesse Hall94cdba92013-07-11 09:40:35 -0700147Loader::driver_t::~driver_t()
Mathias Agopiande586972009-05-28 17:39:03 -0700148{
149 for (size_t i=0 ; i<NELEM(dso) ; i++) {
150 if (dso[i]) {
151 dlclose(dso[i]);
Yi Kong48a6cd22018-07-18 10:07:09 -0700152 dso[i] = nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700153 }
154 }
155}
156
Mathias Agopian65421432017-03-08 11:49:05 -0800157int Loader::driver_t::set(void* hnd, int32_t api)
Mathias Agopiande586972009-05-28 17:39:03 -0700158{
159 switch (api) {
160 case EGL:
161 dso[0] = hnd;
162 break;
163 case GLESv1_CM:
164 dso[1] = hnd;
165 break;
166 case GLESv2:
167 dso[2] = hnd;
168 break;
169 default:
Mathias Agopian65421432017-03-08 11:49:05 -0800170 return -EOVERFLOW;
Mathias Agopiande586972009-05-28 17:39:03 -0700171 }
Mathias Agopian65421432017-03-08 11:49:05 -0800172 return 0;
Mathias Agopiande586972009-05-28 17:39:03 -0700173}
174
175// ----------------------------------------------------------------------------
176
Mathias Agopiande586972009-05-28 17:39:03 -0700177Loader::Loader()
Yi Kong48a6cd22018-07-18 10:07:09 -0700178 : getProcAddress(nullptr)
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800179{
Mathias Agopiande586972009-05-28 17:39:03 -0700180}
181
Mathias Agopian99381422013-04-23 20:52:29 +0200182Loader::~Loader() {
Mathias Agopiande586972009-05-28 17:39:03 -0700183}
184
Jesse Hallc07b5202013-07-04 12:08:16 -0700185static void* load_wrapper(const char* path) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800186 void* so = do_dlopen(path, RTLD_NOW | RTLD_LOCAL);
Jesse Hallc07b5202013-07-04 12:08:16 -0700187 ALOGE_IF(!so, "dlopen(\"%s\") failed: %s", path, dlerror());
188 return so;
189}
190
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700191#ifndef EGL_WRAPPER_DIR
192#if defined(__LP64__)
193#define EGL_WRAPPER_DIR "/system/lib64"
194#else
195#define EGL_WRAPPER_DIR "/system/lib"
196#endif
197#endif
198
bohu69e5b1a2016-02-19 17:15:20 -0800199static void setEmulatorGlesValue(void) {
200 char prop[PROPERTY_VALUE_MAX];
201 property_get("ro.kernel.qemu", prop, "0");
202 if (atoi(prop) != 1) return;
203
204 property_get("ro.kernel.qemu.gles",prop,"0");
205 if (atoi(prop) == 1) {
206 ALOGD("Emulator has host GPU support, qemu.gles is set to 1.");
207 property_set("qemu.gles", "1");
208 return;
209 }
210
211 // for now, checking the following
212 // directory is good enough for emulator system images
213 const char* vendor_lib_path =
214#if defined(__LP64__)
215 "/vendor/lib64/egl";
216#else
217 "/vendor/lib/egl";
218#endif
219
220 const bool has_vendor_lib = (access(vendor_lib_path, R_OK) == 0);
221 if (has_vendor_lib) {
222 ALOGD("Emulator has vendor provided software renderer, qemu.gles is set to 2.");
223 property_set("qemu.gles", "2");
224 } else {
225 ALOGD("Emulator without GPU support detected. "
226 "Fallback to legacy software renderer, qemu.gles is set to 0.");
227 property_set("qemu.gles", "0");
228 }
229}
230
Mathias Agopianada798b2012-02-13 17:09:30 -0800231void* Loader::open(egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700232{
Jesse Hall1508ae62017-01-19 17:43:26 -0800233 ATRACE_CALL();
234
Mathias Agopiande586972009-05-28 17:39:03 -0700235 void* dso;
Yi Kong48a6cd22018-07-18 10:07:09 -0700236 driver_t* hnd = nullptr;
Jesse Hall94cdba92013-07-11 09:40:35 -0700237
bohu69e5b1a2016-02-19 17:15:20 -0800238 setEmulatorGlesValue();
239
Mathias Agopian99381422013-04-23 20:52:29 +0200240 dso = load_driver("GLES", cnx, EGL | GLESv1_CM | GLESv2);
241 if (dso) {
242 hnd = new driver_t(dso);
243 } else {
244 // Always load EGL first
245 dso = load_driver("EGL", cnx, EGL);
Mathias Agopiande586972009-05-28 17:39:03 -0700246 if (dso) {
247 hnd = new driver_t(dso);
Mathias Agopian99381422013-04-23 20:52:29 +0200248 hnd->set( load_driver("GLESv1_CM", cnx, GLESv1_CM), GLESv1_CM );
249 hnd->set( load_driver("GLESv2", cnx, GLESv2), GLESv2 );
Mathias Agopiande586972009-05-28 17:39:03 -0700250 }
251 }
252
Mathias Agopian99381422013-04-23 20:52:29 +0200253 LOG_ALWAYS_FATAL_IF(!hnd, "couldn't find an OpenGL ES implementation");
Jesse Hallc07b5202013-07-04 12:08:16 -0700254
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700255 cnx->libEgl = load_wrapper(EGL_WRAPPER_DIR "/libEGL.so");
256 cnx->libGles2 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv2.so");
257 cnx->libGles1 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv1_CM.so");
258
Michael Chockc0ec5e22014-01-27 08:14:33 -0800259 LOG_ALWAYS_FATAL_IF(!cnx->libEgl,
260 "couldn't load system EGL wrapper libraries");
261
Jesse Hallc07b5202013-07-04 12:08:16 -0700262 LOG_ALWAYS_FATAL_IF(!cnx->libGles2 || !cnx->libGles1,
263 "couldn't load system OpenGL ES wrapper libraries");
264
Mathias Agopiande586972009-05-28 17:39:03 -0700265 return (void*)hnd;
266}
267
Mathias Agopian65421432017-03-08 11:49:05 -0800268void Loader::close(void* driver)
Mathias Agopiande586972009-05-28 17:39:03 -0700269{
270 driver_t* hnd = (driver_t*)driver;
271 delete hnd;
Mathias Agopiande586972009-05-28 17:39:03 -0700272}
273
Jesse Hall94cdba92013-07-11 09:40:35 -0700274void Loader::init_api(void* dso,
275 char const * const * api,
276 __eglMustCastToProperFunctionPointerType* curr,
277 getProcAddressType getProcAddress)
Mathias Agopiande586972009-05-28 17:39:03 -0700278{
Jesse Hall1508ae62017-01-19 17:43:26 -0800279 ATRACE_CALL();
280
Mathias Agopian7773c432012-02-13 20:06:08 -0800281 const ssize_t SIZE = 256;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700282 char scrap[SIZE];
Mathias Agopiande586972009-05-28 17:39:03 -0700283 while (*api) {
284 char const * name = *api;
Jesse Hall94cdba92013-07-11 09:40:35 -0700285 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700286 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700287 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700288 // couldn't find the entry-point, use eglGetProcAddress()
289 f = getProcAddress(name);
290 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700291 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700292 // Try without the OES postfix
293 ssize_t index = ssize_t(strlen(name)) - 3;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700294 if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) {
Mathias Agopiande586972009-05-28 17:39:03 -0700295 strncpy(scrap, name, index);
296 scrap[index] = 0;
297 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000298 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700299 }
300 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700301 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700302 // Try with the OES postfix
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700303 ssize_t index = ssize_t(strlen(name)) - 3;
304 if (index>0 && strcmp(name+index, "OES")) {
305 snprintf(scrap, SIZE, "%sOES", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700306 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000307 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700308 }
309 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700310 if (f == nullptr) {
Steve Block9d453682011-12-20 16:23:08 +0000311 //ALOGD("%s", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700312 f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented;
Mathias Agopian48d438d2012-01-28 21:44:00 -0800313
314 /*
315 * GL_EXT_debug_label is special, we always report it as
316 * supported, it's handled by GLES_trace. If GLES_trace is not
317 * enabled, then these are no-ops.
318 */
319 if (!strcmp(name, "glInsertEventMarkerEXT")) {
320 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
321 } else if (!strcmp(name, "glPushGroupMarkerEXT")) {
322 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
323 } else if (!strcmp(name, "glPopGroupMarkerEXT")) {
324 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
325 }
Mathias Agopiande586972009-05-28 17:39:03 -0700326 }
327 *curr++ = f;
328 api++;
329 }
330}
331
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800332static void* load_system_driver(const char* kind) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800333 ATRACE_CALL();
Mathias Agopian99381422013-04-23 20:52:29 +0200334 class MatchFile {
335 public:
Mathias Agopian65421432017-03-08 11:49:05 -0800336 static std::string find(const char* kind) {
337 std::string result;
Nicolas Capens776951f2015-11-06 10:10:21 -0500338 int emulationStatus = checkGlesEmulationStatus();
339 switch (emulationStatus) {
340 case 0:
Nicolas Capens776951f2015-11-06 10:10:21 -0500341#if defined(__LP64__)
bohu3adf9e12017-07-02 22:08:13 -0700342 result = "/vendor/lib64/egl/libGLES_android.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500343#else
bohu3adf9e12017-07-02 22:08:13 -0700344 result = "/vendor/lib/egl/libGLES_android.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500345#endif
346 return result;
347 case 1:
348 // Use host-side OpenGL through the "emulation" library
349#if defined(__LP64__)
bohu3adf9e12017-07-02 22:08:13 -0700350 result = std::string("/vendor/lib64/egl/lib") + kind + "_emulation.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500351#else
bohu3adf9e12017-07-02 22:08:13 -0700352 result = std::string("/vendor/lib/egl/lib") + kind + "_emulation.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500353#endif
354 return result;
Lingfeng Yang0f82d872017-08-11 01:18:00 -0700355 case 2:
356 // Use guest side swiftshader library
357#if defined(__LP64__)
358 result = std::string("/vendor/lib64/egl/lib") + kind + "_swiftshader.so";
359#else
360 result = std::string("/vendor/lib/egl/lib") + kind + "_swiftshader.so";
361#endif
362 return result;
Nicolas Capens776951f2015-11-06 10:10:21 -0500363 default:
364 // Not in emulator, or use other guest-side implementation
365 break;
366 }
367
Mathias Agopian65421432017-03-08 11:49:05 -0800368 std::string pattern = std::string("lib") + kind;
Mathias Agopian99381422013-04-23 20:52:29 +0200369 const char* const searchPaths[] = {
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800370#if defined(__LP64__)
371 "/vendor/lib64/egl",
372 "/system/lib64/egl"
373#else
Mathias Agopian99381422013-04-23 20:52:29 +0200374 "/vendor/lib/egl",
375 "/system/lib/egl"
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800376#endif
Mathias Agopian99381422013-04-23 20:52:29 +0200377 };
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700378
Mathias Agopian99381422013-04-23 20:52:29 +0200379 // first, we search for the exact name of the GLES userspace
380 // driver in both locations.
381 // i.e.:
382 // libGLES.so, or:
383 // libEGL.so, libGLESv1_CM.so, libGLESv2.so
384
385 for (size_t i=0 ; i<NELEM(searchPaths) ; i++) {
386 if (find(result, pattern, searchPaths[i], true)) {
387 return result;
388 }
389 }
390
391 // for compatibility with the old "egl.cfg" naming convention
392 // we look for files that match:
393 // libGLES_*.so, or:
394 // libEGL_*.so, libGLESv1_CM_*.so, libGLESv2_*.so
395
396 pattern.append("_");
397 for (size_t i=0 ; i<NELEM(searchPaths) ; i++) {
398 if (find(result, pattern, searchPaths[i], false)) {
399 return result;
400 }
401 }
402
403 // we didn't find the driver. gah.
404 result.clear();
405 return result;
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700406 }
Mathias Agopian99381422013-04-23 20:52:29 +0200407
408 private:
Mathias Agopian65421432017-03-08 11:49:05 -0800409 static bool find(std::string& result,
410 const std::string& pattern, const char* const search, bool exact) {
Mathias Agopian99381422013-04-23 20:52:29 +0200411 if (exact) {
Jesse Hall4aaa9602017-08-29 16:33:54 -0700412 std::string absolutePath = std::string(search) + "/" + pattern + ".so";
Mathias Agopian65421432017-03-08 11:49:05 -0800413 if (!access(absolutePath.c_str(), R_OK)) {
Mathias Agopian99381422013-04-23 20:52:29 +0200414 result = absolutePath;
415 return true;
416 }
417 return false;
418 }
419
420 DIR* d = opendir(search);
Yi Kong48a6cd22018-07-18 10:07:09 -0700421 if (d != nullptr) {
Mathias Agopian99381422013-04-23 20:52:29 +0200422 struct dirent* e;
Yi Kong48a6cd22018-07-18 10:07:09 -0700423 while ((e = readdir(d)) != nullptr) {
Mathias Agopian99381422013-04-23 20:52:29 +0200424 if (e->d_type == DT_DIR) {
425 continue;
426 }
427 if (!strcmp(e->d_name, "libGLES_android.so")) {
428 // always skip the software renderer
429 continue;
430 }
Mathias Agopian65421432017-03-08 11:49:05 -0800431 if (strstr(e->d_name, pattern.c_str()) == e->d_name) {
Mathias Agopian99381422013-04-23 20:52:29 +0200432 if (!strcmp(e->d_name + strlen(e->d_name) - 3, ".so")) {
Mathias Agopian65421432017-03-08 11:49:05 -0800433 result = std::string(search) + "/" + e->d_name;
Mathias Agopian99381422013-04-23 20:52:29 +0200434 closedir(d);
435 return true;
436 }
437 }
438 }
439 closedir(d);
440 }
441 return false;
442 }
443 };
444
445
Mathias Agopian65421432017-03-08 11:49:05 -0800446 std::string absolutePath = MatchFile::find(kind);
447 if (absolutePath.empty()) {
Mathias Agopian99381422013-04-23 20:52:29 +0200448 // this happens often, we don't want to log an error
Yi Kong48a6cd22018-07-18 10:07:09 -0700449 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700450 }
Mathias Agopian65421432017-03-08 11:49:05 -0800451 const char* const driver_absolute_path = absolutePath.c_str();
Mathias Agopiande586972009-05-28 17:39:03 -0700452
Jiyong Park5910dc72017-04-05 14:23:52 +0900453 // Try to load drivers from the 'sphal' namespace, if it exist. Fall back to
Justin Yunb7320302017-05-22 15:13:40 +0900454 // the original routine when the namespace does not exist.
Jiyong Park5910dc72017-04-05 14:23:52 +0900455 // See /system/core/rootdir/etc/ld.config.txt for the configuration of the
456 // sphal namespace.
Justin Yunb7320302017-05-22 15:13:40 +0900457 void* dso = do_android_load_sphal_library(driver_absolute_path,
458 RTLD_NOW | RTLD_LOCAL);
Yi Kong48a6cd22018-07-18 10:07:09 -0700459 if (dso == nullptr) {
Mathias Agopian8c173842009-09-20 16:01:02 -0700460 const char* err = dlerror();
Mathias Agopian65421432017-03-08 11:49:05 -0800461 ALOGE("load_driver(%s): %s", driver_absolute_path, err ? err : "unknown");
Yi Kong48a6cd22018-07-18 10:07:09 -0700462 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700463 }
464
Steve Block9d453682011-12-20 16:23:08 +0000465 ALOGD("loaded %s", driver_absolute_path);
Mathias Agopianbaca89c2009-08-20 19:09:34 -0700466
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800467 return dso;
468}
469
Cody Northrop1f00e172018-04-02 11:23:31 -0600470static void* load_angle_from_namespace(const char* kind, android_namespace_t* ns) {
471 const android_dlextinfo dlextinfo = {
472 .flags = ANDROID_DLEXT_USE_NAMESPACE,
473 .library_namespace = ns,
474 };
475
476 std::string name = std::string("lib") + kind + "_angle.so";
477
478 void* so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
479
480 if (so) {
481 ALOGD("dlopen_ext from APK (%s) success at %p", name.c_str(), so);
482 return so;
483 } else {
484 ALOGE("dlopen_ext(\"%s\") failed: %s", name.c_str(), dlerror());
485 }
486
487 return nullptr;
488}
489
Ian Elliott34474472018-09-05 10:57:07 -0600490static ANGLEPreference getAnglePref(const char* app_pref) {
491 if (app_pref == nullptr)
492 return ANGLE_NO_PREFERENCE;
493
494 if (strcmp(app_pref, "angle") == 0) {
495 return ANGLE_PREFER_ANGLE;
496 } else if (strcmp(app_pref, "native") == 0) {
497 return ANGLE_PREFER_NATIVE;
498 }
499 return ANGLE_NO_PREFERENCE;
500}
501
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600502static void* load_angle(const char* kind, egl_connection_t* cnx) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600503 // Only attempt to load ANGLE libs
504 if (strcmp(kind, "EGL") != 0 && strcmp(kind, "GLESv2") != 0 && strcmp(kind, "GLESv1_CM") != 0)
505 return nullptr;
506
507 void* so = nullptr;
508 std::string name;
Ian Elliottb058aff2018-08-09 12:00:55 -0600509 char prop[PROPERTY_VALUE_MAX];
Cody Northrop1f00e172018-04-02 11:23:31 -0600510
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600511 android_namespace_t* ns = android_getAngleNamespace();
512 const char* app_name = android_getAngleAppName();
Cody Northrop49d51992018-08-29 16:37:09 -0600513 const char* app_pref = android_getAngleAppPref();
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600514 bool developer_opt_in = android_getAngleDeveloperOptIn();
515
Ian Elliott34474472018-09-05 10:57:07 -0600516 // Determine whether or not to use ANGLE:
517 ANGLEPreference developer_option = developer_opt_in ? ANGLE_PREFER_ANGLE : ANGLE_NO_PREFERENCE;
518 bool use_angle = (developer_option == ANGLE_PREFER_ANGLE);
519
520 if (use_angle) {
521 ALOGV("User set \"Developer Options\" to force the use of ANGLE");
522 } else {
523 // The "Developer Options" value wasn't set to force the use of ANGLE. Need to temporarily
524 // load ANGLE and call the updatable opt-in/out logic:
525 std::string app_name_str = app_name ? app_name : "";
526 char manufacturer[PROPERTY_VALUE_MAX];
527 char model[PROPERTY_VALUE_MAX];
528 property_get("ro.product.manufacturer", manufacturer, "UNSET");
529 property_get("ro.product.model", model, "UNSET");
530 ANGLEPreference app_preference = getAnglePref(android_getAngleAppPref());
531
532 so = load_angle_from_namespace("GLESv2", ns);
533 if (so) {
534 ALOGV("Temporarily loaded ANGLE's opt-in/out logic from namespace");
535 fpANGLEUseForApplication fp =
536 (fpANGLEUseForApplication)dlsym(so, "ANGLEUseForApplication");
537 if (fp) {
538 use_angle = (fp)(app_name_str.c_str(), manufacturer, model, developer_option,
539 app_preference);
540 ALOGV("Result of opt-in/out logic is %s", use_angle ? "true" : "false");
541 }
542
543 ALOGV("Close temporarily-loaded ANGLE opt-in/out logic");
544 dlclose(so);
545 so = nullptr;
546 } else {
547 // We weren't able to load and call the updateable opt-in/out logic.
548 // If we can't load the library, there is no ANGLE available.
549 use_angle = false;
550 ALOGV("Could not temporarily-load the ANGLE opt-in/out logic, cannot use ANGLE.");
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600551 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600552 }
Ian Elliott34474472018-09-05 10:57:07 -0600553 if (use_angle) {
554 so = load_angle_from_namespace(kind, ns);
555 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600556
557 if (so) {
Ian Elliott34474472018-09-05 10:57:07 -0600558 ALOGV("Loaded ANGLE %s library for %s (instead of native)",
Ian Elliottb058aff2018-08-09 12:00:55 -0600559 kind, app_name ? app_name : "nullptr");
560 property_get("debug.angle.backend", prop, "UNSET");
Ian Elliott34474472018-09-05 10:57:07 -0600561 ALOGV("ANGLE's backend set to %s", prop);
Ian Elliottb058aff2018-08-09 12:00:55 -0600562 property_get("debug.hwui.renderer", prop, "UNSET");
Ian Elliott34474472018-09-05 10:57:07 -0600563 ALOGV("Skia's renderer set to %s", prop);
Cody Northrop1f00e172018-04-02 11:23:31 -0600564 cnx->useAngle = true;
565 // Find and load vendor libEGL for ANGLE
566 if (!cnx->vendorEGL) {
567 cnx->vendorEGL = load_system_driver("EGL");
568 }
569 return so;
Ian Elliottb058aff2018-08-09 12:00:55 -0600570 } else {
Ian Elliott34474472018-09-05 10:57:07 -0600571 ALOGV("Loaded native %s library for %s (instead of ANGLE)",
Ian Elliottb058aff2018-08-09 12:00:55 -0600572 kind, app_name ? app_name : "nullptr");
Cody Northrop1f00e172018-04-02 11:23:31 -0600573 }
574
575 return nullptr;
576}
577
Mathias Agopian311b4792017-02-28 15:00:49 -0800578static const char* HAL_SUBNAME_KEY_PROPERTIES[2] = {
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800579 "ro.hardware.egl",
580 "ro.board.platform",
Mathias Agopian311b4792017-02-28 15:00:49 -0800581};
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800582
583static void* load_updated_driver(const char* kind, android_namespace_t* ns) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800584 ATRACE_CALL();
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800585 const android_dlextinfo dlextinfo = {
586 .flags = ANDROID_DLEXT_USE_NAMESPACE,
587 .library_namespace = ns,
588 };
589 void* so = nullptr;
590 char prop[PROPERTY_VALUE_MAX + 1];
591 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
592 if (property_get(key, prop, nullptr) > 0) {
Mathias Agopian65421432017-03-08 11:49:05 -0800593 std::string name = std::string("lib") + kind + "_" + prop + ".so";
594 so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
Mathias Agopian311b4792017-02-28 15:00:49 -0800595 if (so) {
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800596 return so;
Mathias Agopian311b4792017-02-28 15:00:49 -0800597 }
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800598 }
599 }
600 return nullptr;
601}
602
603void *Loader::load_driver(const char* kind,
604 egl_connection_t* cnx, uint32_t mask)
605{
Jesse Hall1508ae62017-01-19 17:43:26 -0800606 ATRACE_CALL();
607
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800608 void* dso = nullptr;
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600609 dso = load_angle(kind, cnx);
Jiyong Parka243e5d2017-06-21 12:26:51 +0900610#ifndef __ANDROID_VNDK__
Cody Northrop1f00e172018-04-02 11:23:31 -0600611 if (!dso) {
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600612 android_namespace_t* ns = android_getDriverNamespace();
Cody Northrop1f00e172018-04-02 11:23:31 -0600613 if (ns) {
614 dso = load_updated_driver(kind, ns);
615 }
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800616 }
Jiyong Parka243e5d2017-06-21 12:26:51 +0900617#endif
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800618 if (!dso) {
619 dso = load_system_driver(kind);
620 if (!dso)
Yi Kong48a6cd22018-07-18 10:07:09 -0700621 return nullptr;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800622 }
623
Mathias Agopiande586972009-05-28 17:39:03 -0700624 if (mask & EGL) {
625 getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress");
626
Jesse Hall94cdba92013-07-11 09:40:35 -0700627 ALOGE_IF(!getProcAddress,
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800628 "can't find eglGetProcAddress() in EGL driver library");
Mathias Agopiande586972009-05-28 17:39:03 -0700629
Mathias Agopian618fa102009-10-14 02:06:37 -0700630 egl_t* egl = &cnx->egl;
Mathias Agopiande586972009-05-28 17:39:03 -0700631 __eglMustCastToProperFunctionPointerType* curr =
632 (__eglMustCastToProperFunctionPointerType*)egl;
633 char const * const * api = egl_names;
634 while (*api) {
635 char const * name = *api;
Jesse Hall94cdba92013-07-11 09:40:35 -0700636 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700637 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700638 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700639 // couldn't find the entry-point, use eglGetProcAddress()
640 f = getProcAddress(name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700641 if (f == nullptr) {
642 f = (__eglMustCastToProperFunctionPointerType)nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700643 }
644 }
645 *curr++ = f;
646 api++;
647 }
648 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700649
Mathias Agopiande586972009-05-28 17:39:03 -0700650 if (mask & GLESv1_CM) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700651 init_api(dso, gl_names,
652 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800653 &cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl,
Mathias Agopian618fa102009-10-14 02:06:37 -0700654 getProcAddress);
Mathias Agopiande586972009-05-28 17:39:03 -0700655 }
656
657 if (mask & GLESv2) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700658 init_api(dso, gl_names,
659 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800660 &cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl,
Mathias Agopiande586972009-05-28 17:39:03 -0700661 getProcAddress);
662 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700663
Mathias Agopiande586972009-05-28 17:39:03 -0700664 return dso;
665}
666
667// ----------------------------------------------------------------------------
668}; // namespace android
669// ----------------------------------------------------------------------------