blob: 5aa03a7647581f04bec00db57d3dbdfab99ecd7f [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 Northrop87a562c2018-10-04 14:44:11 -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"
Mathias Agopiande586972009-05-28 17:39:03 -070039
Jiyong Park5910dc72017-04-05 14:23:52 +090040extern "C" {
41 android_namespace_t* android_get_exported_namespace(const char*);
Ian Elliott34474472018-09-05 10:57:07 -060042
43 // TODO(ianelliott@): Get this from an ANGLE header:
44 typedef enum ANGLEPreference {
45 ANGLE_NO_PREFERENCE = 0,
46 ANGLE_PREFER_NATIVE = 1,
47 ANGLE_PREFER_ANGLE = 2,
48 } ANGLEPreference;
49
50 // TODO(ianelliott@): Get this from an ANGLE header:
51 typedef bool (*fpANGLEUseForApplication)(const char* appName, const char* deviceMfr,
52 const char* deviceModel, ANGLEPreference developerOption,
53 ANGLEPreference appPreference);
Jiyong Park5910dc72017-04-05 14:23:52 +090054}
55
Mathias Agopiande586972009-05-28 17:39:03 -070056// ----------------------------------------------------------------------------
57namespace android {
58// ----------------------------------------------------------------------------
59
60
61/*
Mathias Agopian99381422013-04-23 20:52:29 +020062 * EGL userspace drivers must be provided either:
63 * - as a single library:
64 * /vendor/lib/egl/libGLES.so
65 *
66 * - as separate libraries:
67 * /vendor/lib/egl/libEGL.so
68 * /vendor/lib/egl/libGLESv1_CM.so
69 * /vendor/lib/egl/libGLESv2.so
70 *
71 * The software renderer for the emulator must be provided as a single
72 * library at:
73 *
74 * /system/lib/egl/libGLES_android.so
75 *
76 *
77 * For backward compatibility and to facilitate the transition to
78 * this new naming scheme, the loader will additionally look for:
Jesse Hall94cdba92013-07-11 09:40:35 -070079 *
Mathias Agopian99381422013-04-23 20:52:29 +020080 * /{vendor|system}/lib/egl/lib{GLES | [EGL|GLESv1_CM|GLESv2]}_*.so
Jesse Hall94cdba92013-07-11 09:40:35 -070081 *
Mathias Agopiande586972009-05-28 17:39:03 -070082 */
83
Mathias Agopian65421432017-03-08 11:49:05 -080084Loader& Loader::getInstance() {
85 static Loader loader;
86 return loader;
87}
Mathias Agopiande586972009-05-28 17:39:03 -070088
David 'Digit' Turner80b30c22011-08-26 17:38:47 +020089/* This function is called to check whether we run inside the emulator,
90 * and if this is the case whether GLES GPU emulation is supported.
91 *
92 * Returned values are:
93 * -1 -> not running inside the emulator
94 * 0 -> running inside the emulator, but GPU emulation not supported
95 * 1 -> running inside the emulator, GPU emulation is supported
Nicolas Capens776951f2015-11-06 10:10:21 -050096 * through the "emulation" host-side OpenGL ES implementation.
97 * 2 -> running inside the emulator, GPU emulation is supported
98 * through a guest-side vendor driver's OpenGL ES implementation.
David 'Digit' Turner80b30c22011-08-26 17:38:47 +020099 */
100static int
101checkGlesEmulationStatus(void)
102{
103 /* We're going to check for the following kernel parameters:
104 *
105 * qemu=1 -> tells us that we run inside the emulator
106 * android.qemu.gles=<number> -> tells us the GLES GPU emulation status
107 *
108 * Note that we will return <number> if we find it. This let us support
109 * more additionnal emulation modes in the future.
110 */
111 char prop[PROPERTY_VALUE_MAX];
112 int result = -1;
113
114 /* First, check for qemu=1 */
115 property_get("ro.kernel.qemu",prop,"0");
116 if (atoi(prop) != 1)
117 return -1;
118
119 /* We are in the emulator, get GPU status value */
bohu69e5b1a2016-02-19 17:15:20 -0800120 property_get("qemu.gles",prop,"0");
David 'Digit' Turner80b30c22011-08-26 17:38:47 +0200121 return atoi(prop);
122}
123
Jesse Hall1508ae62017-01-19 17:43:26 -0800124static void* do_dlopen(const char* path, int mode) {
125 ATRACE_CALL();
126 return dlopen(path, mode);
127}
128
Jiyong Park5910dc72017-04-05 14:23:52 +0900129static void* do_android_dlopen_ext(const char* path, int mode, const android_dlextinfo* info) {
130 ATRACE_CALL();
131 return android_dlopen_ext(path, mode, info);
132}
133
Justin Yunb7320302017-05-22 15:13:40 +0900134static void* do_android_load_sphal_library(const char* path, int mode) {
135 ATRACE_CALL();
136 return android_load_sphal_library(path, mode);
137}
138
Mathias Agopiande586972009-05-28 17:39:03 -0700139// ----------------------------------------------------------------------------
140
Jesse Hall94cdba92013-07-11 09:40:35 -0700141Loader::driver_t::driver_t(void* gles)
Mathias Agopiande586972009-05-28 17:39:03 -0700142{
143 dso[0] = gles;
144 for (size_t i=1 ; i<NELEM(dso) ; i++)
Yi Kong48a6cd22018-07-18 10:07:09 -0700145 dso[i] = nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700146}
147
Jesse Hall94cdba92013-07-11 09:40:35 -0700148Loader::driver_t::~driver_t()
Mathias Agopiande586972009-05-28 17:39:03 -0700149{
150 for (size_t i=0 ; i<NELEM(dso) ; i++) {
151 if (dso[i]) {
152 dlclose(dso[i]);
Yi Kong48a6cd22018-07-18 10:07:09 -0700153 dso[i] = nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700154 }
155 }
156}
157
Mathias Agopian65421432017-03-08 11:49:05 -0800158int Loader::driver_t::set(void* hnd, int32_t api)
Mathias Agopiande586972009-05-28 17:39:03 -0700159{
160 switch (api) {
161 case EGL:
162 dso[0] = hnd;
163 break;
164 case GLESv1_CM:
165 dso[1] = hnd;
166 break;
167 case GLESv2:
168 dso[2] = hnd;
169 break;
170 default:
Mathias Agopian65421432017-03-08 11:49:05 -0800171 return -EOVERFLOW;
Mathias Agopiande586972009-05-28 17:39:03 -0700172 }
Mathias Agopian65421432017-03-08 11:49:05 -0800173 return 0;
Mathias Agopiande586972009-05-28 17:39:03 -0700174}
175
176// ----------------------------------------------------------------------------
177
Mathias Agopiande586972009-05-28 17:39:03 -0700178Loader::Loader()
Yi Kong48a6cd22018-07-18 10:07:09 -0700179 : getProcAddress(nullptr)
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800180{
Mathias Agopiande586972009-05-28 17:39:03 -0700181}
182
Mathias Agopian99381422013-04-23 20:52:29 +0200183Loader::~Loader() {
Mathias Agopiande586972009-05-28 17:39:03 -0700184}
185
Jesse Hallc07b5202013-07-04 12:08:16 -0700186static void* load_wrapper(const char* path) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800187 void* so = do_dlopen(path, RTLD_NOW | RTLD_LOCAL);
Jesse Hallc07b5202013-07-04 12:08:16 -0700188 ALOGE_IF(!so, "dlopen(\"%s\") failed: %s", path, dlerror());
189 return so;
190}
191
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700192#ifndef EGL_WRAPPER_DIR
193#if defined(__LP64__)
194#define EGL_WRAPPER_DIR "/system/lib64"
195#else
196#define EGL_WRAPPER_DIR "/system/lib"
197#endif
198#endif
199
bohu69e5b1a2016-02-19 17:15:20 -0800200static void setEmulatorGlesValue(void) {
201 char prop[PROPERTY_VALUE_MAX];
202 property_get("ro.kernel.qemu", prop, "0");
203 if (atoi(prop) != 1) return;
204
205 property_get("ro.kernel.qemu.gles",prop,"0");
206 if (atoi(prop) == 1) {
207 ALOGD("Emulator has host GPU support, qemu.gles is set to 1.");
208 property_set("qemu.gles", "1");
209 return;
210 }
211
212 // for now, checking the following
213 // directory is good enough for emulator system images
214 const char* vendor_lib_path =
215#if defined(__LP64__)
216 "/vendor/lib64/egl";
217#else
218 "/vendor/lib/egl";
219#endif
220
221 const bool has_vendor_lib = (access(vendor_lib_path, R_OK) == 0);
222 if (has_vendor_lib) {
223 ALOGD("Emulator has vendor provided software renderer, qemu.gles is set to 2.");
224 property_set("qemu.gles", "2");
225 } else {
226 ALOGD("Emulator without GPU support detected. "
227 "Fallback to legacy software renderer, qemu.gles is set to 0.");
228 property_set("qemu.gles", "0");
229 }
230}
231
Mathias Agopianada798b2012-02-13 17:09:30 -0800232void* Loader::open(egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700233{
Jesse Hall1508ae62017-01-19 17:43:26 -0800234 ATRACE_CALL();
235
Mathias Agopiande586972009-05-28 17:39:03 -0700236 void* dso;
Yi Kong48a6cd22018-07-18 10:07:09 -0700237 driver_t* hnd = nullptr;
Jesse Hall94cdba92013-07-11 09:40:35 -0700238
bohu69e5b1a2016-02-19 17:15:20 -0800239 setEmulatorGlesValue();
240
Cody Northrop87a562c2018-10-04 14:44:11 -0600241 dso = load_driver("GLES", cnx, EGL | GLESv1_CM | GLESv2 | PLATFORM);
Mathias Agopian99381422013-04-23 20:52:29 +0200242 if (dso) {
243 hnd = new driver_t(dso);
244 } else {
245 // Always load EGL first
Cody Northrop87a562c2018-10-04 14:44:11 -0600246 dso = load_driver("EGL", cnx, EGL | PLATFORM);
Mathias Agopiande586972009-05-28 17:39:03 -0700247 if (dso) {
248 hnd = new driver_t(dso);
Mathias Agopian99381422013-04-23 20:52:29 +0200249 hnd->set( load_driver("GLESv1_CM", cnx, GLESv1_CM), GLESv1_CM );
250 hnd->set( load_driver("GLESv2", cnx, GLESv2), GLESv2 );
Mathias Agopiande586972009-05-28 17:39:03 -0700251 }
252 }
253
Mathias Agopian99381422013-04-23 20:52:29 +0200254 LOG_ALWAYS_FATAL_IF(!hnd, "couldn't find an OpenGL ES implementation");
Jesse Hallc07b5202013-07-04 12:08:16 -0700255
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700256 cnx->libEgl = load_wrapper(EGL_WRAPPER_DIR "/libEGL.so");
257 cnx->libGles2 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv2.so");
258 cnx->libGles1 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv1_CM.so");
259
Michael Chockc0ec5e22014-01-27 08:14:33 -0800260 LOG_ALWAYS_FATAL_IF(!cnx->libEgl,
261 "couldn't load system EGL wrapper libraries");
262
Jesse Hallc07b5202013-07-04 12:08:16 -0700263 LOG_ALWAYS_FATAL_IF(!cnx->libGles2 || !cnx->libGles1,
264 "couldn't load system OpenGL ES wrapper libraries");
265
Mathias Agopiande586972009-05-28 17:39:03 -0700266 return (void*)hnd;
267}
268
Mathias Agopian65421432017-03-08 11:49:05 -0800269void Loader::close(void* driver)
Mathias Agopiande586972009-05-28 17:39:03 -0700270{
271 driver_t* hnd = (driver_t*)driver;
272 delete hnd;
Mathias Agopiande586972009-05-28 17:39:03 -0700273}
274
Jesse Hall94cdba92013-07-11 09:40:35 -0700275void Loader::init_api(void* dso,
276 char const * const * api,
277 __eglMustCastToProperFunctionPointerType* curr,
278 getProcAddressType getProcAddress)
Mathias Agopiande586972009-05-28 17:39:03 -0700279{
Jesse Hall1508ae62017-01-19 17:43:26 -0800280 ATRACE_CALL();
281
Mathias Agopian7773c432012-02-13 20:06:08 -0800282 const ssize_t SIZE = 256;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700283 char scrap[SIZE];
Mathias Agopiande586972009-05-28 17:39:03 -0700284 while (*api) {
285 char const * name = *api;
Jesse Hall94cdba92013-07-11 09:40:35 -0700286 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700287 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700288 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700289 // couldn't find the entry-point, use eglGetProcAddress()
290 f = getProcAddress(name);
291 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700292 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700293 // Try without the OES postfix
294 ssize_t index = ssize_t(strlen(name)) - 3;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700295 if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) {
Mathias Agopiande586972009-05-28 17:39:03 -0700296 strncpy(scrap, name, index);
297 scrap[index] = 0;
298 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000299 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700300 }
301 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700302 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700303 // Try with the OES postfix
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700304 ssize_t index = ssize_t(strlen(name)) - 3;
305 if (index>0 && strcmp(name+index, "OES")) {
306 snprintf(scrap, SIZE, "%sOES", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700307 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000308 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700309 }
310 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700311 if (f == nullptr) {
Steve Block9d453682011-12-20 16:23:08 +0000312 //ALOGD("%s", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700313 f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented;
Mathias Agopian48d438d2012-01-28 21:44:00 -0800314
315 /*
316 * GL_EXT_debug_label is special, we always report it as
317 * supported, it's handled by GLES_trace. If GLES_trace is not
318 * enabled, then these are no-ops.
319 */
320 if (!strcmp(name, "glInsertEventMarkerEXT")) {
321 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
322 } else if (!strcmp(name, "glPushGroupMarkerEXT")) {
323 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
324 } else if (!strcmp(name, "glPopGroupMarkerEXT")) {
325 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
326 }
Mathias Agopiande586972009-05-28 17:39:03 -0700327 }
328 *curr++ = f;
329 api++;
330 }
331}
332
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800333static void* load_system_driver(const char* kind) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800334 ATRACE_CALL();
Mathias Agopian99381422013-04-23 20:52:29 +0200335 class MatchFile {
336 public:
Mathias Agopian65421432017-03-08 11:49:05 -0800337 static std::string find(const char* kind) {
338 std::string result;
Nicolas Capens776951f2015-11-06 10:10:21 -0500339 int emulationStatus = checkGlesEmulationStatus();
340 switch (emulationStatus) {
341 case 0:
Nicolas Capens776951f2015-11-06 10:10:21 -0500342#if defined(__LP64__)
bohu3adf9e12017-07-02 22:08:13 -0700343 result = "/vendor/lib64/egl/libGLES_android.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500344#else
bohu3adf9e12017-07-02 22:08:13 -0700345 result = "/vendor/lib/egl/libGLES_android.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500346#endif
347 return result;
348 case 1:
349 // Use host-side OpenGL through the "emulation" library
350#if defined(__LP64__)
bohu3adf9e12017-07-02 22:08:13 -0700351 result = std::string("/vendor/lib64/egl/lib") + kind + "_emulation.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500352#else
bohu3adf9e12017-07-02 22:08:13 -0700353 result = std::string("/vendor/lib/egl/lib") + kind + "_emulation.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500354#endif
355 return result;
Lingfeng Yang0f82d872017-08-11 01:18:00 -0700356 case 2:
357 // Use guest side swiftshader library
358#if defined(__LP64__)
359 result = std::string("/vendor/lib64/egl/lib") + kind + "_swiftshader.so";
360#else
361 result = std::string("/vendor/lib/egl/lib") + kind + "_swiftshader.so";
362#endif
363 return result;
Nicolas Capens776951f2015-11-06 10:10:21 -0500364 default:
365 // Not in emulator, or use other guest-side implementation
366 break;
367 }
368
Mathias Agopian65421432017-03-08 11:49:05 -0800369 std::string pattern = std::string("lib") + kind;
Mathias Agopian99381422013-04-23 20:52:29 +0200370 const char* const searchPaths[] = {
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800371#if defined(__LP64__)
372 "/vendor/lib64/egl",
373 "/system/lib64/egl"
374#else
Mathias Agopian99381422013-04-23 20:52:29 +0200375 "/vendor/lib/egl",
376 "/system/lib/egl"
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800377#endif
Mathias Agopian99381422013-04-23 20:52:29 +0200378 };
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700379
Mathias Agopian99381422013-04-23 20:52:29 +0200380 // first, we search for the exact name of the GLES userspace
381 // driver in both locations.
382 // i.e.:
383 // libGLES.so, or:
384 // libEGL.so, libGLESv1_CM.so, libGLESv2.so
385
386 for (size_t i=0 ; i<NELEM(searchPaths) ; i++) {
387 if (find(result, pattern, searchPaths[i], true)) {
388 return result;
389 }
390 }
391
392 // for compatibility with the old "egl.cfg" naming convention
393 // we look for files that match:
394 // libGLES_*.so, or:
395 // libEGL_*.so, libGLESv1_CM_*.so, libGLESv2_*.so
396
397 pattern.append("_");
398 for (size_t i=0 ; i<NELEM(searchPaths) ; i++) {
399 if (find(result, pattern, searchPaths[i], false)) {
400 return result;
401 }
402 }
403
404 // we didn't find the driver. gah.
405 result.clear();
406 return result;
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700407 }
Mathias Agopian99381422013-04-23 20:52:29 +0200408
409 private:
Mathias Agopian65421432017-03-08 11:49:05 -0800410 static bool find(std::string& result,
411 const std::string& pattern, const char* const search, bool exact) {
Mathias Agopian99381422013-04-23 20:52:29 +0200412 if (exact) {
Jesse Hall4aaa9602017-08-29 16:33:54 -0700413 std::string absolutePath = std::string(search) + "/" + pattern + ".so";
Mathias Agopian65421432017-03-08 11:49:05 -0800414 if (!access(absolutePath.c_str(), R_OK)) {
Mathias Agopian99381422013-04-23 20:52:29 +0200415 result = absolutePath;
416 return true;
417 }
418 return false;
419 }
420
421 DIR* d = opendir(search);
Yi Kong48a6cd22018-07-18 10:07:09 -0700422 if (d != nullptr) {
Mathias Agopian99381422013-04-23 20:52:29 +0200423 struct dirent* e;
Yi Kong48a6cd22018-07-18 10:07:09 -0700424 while ((e = readdir(d)) != nullptr) {
Mathias Agopian99381422013-04-23 20:52:29 +0200425 if (e->d_type == DT_DIR) {
426 continue;
427 }
428 if (!strcmp(e->d_name, "libGLES_android.so")) {
429 // always skip the software renderer
430 continue;
431 }
Mathias Agopian65421432017-03-08 11:49:05 -0800432 if (strstr(e->d_name, pattern.c_str()) == e->d_name) {
Mathias Agopian99381422013-04-23 20:52:29 +0200433 if (!strcmp(e->d_name + strlen(e->d_name) - 3, ".so")) {
Mathias Agopian65421432017-03-08 11:49:05 -0800434 result = std::string(search) + "/" + e->d_name;
Mathias Agopian99381422013-04-23 20:52:29 +0200435 closedir(d);
436 return true;
437 }
438 }
439 }
440 closedir(d);
441 }
442 return false;
443 }
444 };
445
446
Mathias Agopian65421432017-03-08 11:49:05 -0800447 std::string absolutePath = MatchFile::find(kind);
448 if (absolutePath.empty()) {
Mathias Agopian99381422013-04-23 20:52:29 +0200449 // this happens often, we don't want to log an error
Yi Kong48a6cd22018-07-18 10:07:09 -0700450 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700451 }
Mathias Agopian65421432017-03-08 11:49:05 -0800452 const char* const driver_absolute_path = absolutePath.c_str();
Mathias Agopiande586972009-05-28 17:39:03 -0700453
Jiyong Park5910dc72017-04-05 14:23:52 +0900454 // Try to load drivers from the 'sphal' namespace, if it exist. Fall back to
Justin Yunb7320302017-05-22 15:13:40 +0900455 // the original routine when the namespace does not exist.
Jiyong Park5910dc72017-04-05 14:23:52 +0900456 // See /system/core/rootdir/etc/ld.config.txt for the configuration of the
457 // sphal namespace.
Justin Yunb7320302017-05-22 15:13:40 +0900458 void* dso = do_android_load_sphal_library(driver_absolute_path,
459 RTLD_NOW | RTLD_LOCAL);
Yi Kong48a6cd22018-07-18 10:07:09 -0700460 if (dso == nullptr) {
Mathias Agopian8c173842009-09-20 16:01:02 -0700461 const char* err = dlerror();
Mathias Agopian65421432017-03-08 11:49:05 -0800462 ALOGE("load_driver(%s): %s", driver_absolute_path, err ? err : "unknown");
Yi Kong48a6cd22018-07-18 10:07:09 -0700463 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700464 }
465
Steve Block9d453682011-12-20 16:23:08 +0000466 ALOGD("loaded %s", driver_absolute_path);
Mathias Agopianbaca89c2009-08-20 19:09:34 -0700467
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800468 return dso;
469}
470
Cody Northrop1f00e172018-04-02 11:23:31 -0600471static void* load_angle_from_namespace(const char* kind, android_namespace_t* ns) {
472 const android_dlextinfo dlextinfo = {
473 .flags = ANDROID_DLEXT_USE_NAMESPACE,
474 .library_namespace = ns,
475 };
476
477 std::string name = std::string("lib") + kind + "_angle.so";
478
479 void* so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
480
481 if (so) {
482 ALOGD("dlopen_ext from APK (%s) success at %p", name.c_str(), so);
483 return so;
484 } else {
485 ALOGE("dlopen_ext(\"%s\") failed: %s", name.c_str(), dlerror());
486 }
487
488 return nullptr;
489}
490
Ian Elliott34474472018-09-05 10:57:07 -0600491static ANGLEPreference getAnglePref(const char* app_pref) {
492 if (app_pref == nullptr)
493 return ANGLE_NO_PREFERENCE;
494
495 if (strcmp(app_pref, "angle") == 0) {
496 return ANGLE_PREFER_ANGLE;
497 } else if (strcmp(app_pref, "native") == 0) {
498 return ANGLE_PREFER_NATIVE;
499 }
500 return ANGLE_NO_PREFERENCE;
501}
502
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600503static void* load_angle(const char* kind, egl_connection_t* cnx) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600504 // Only attempt to load ANGLE libs
505 if (strcmp(kind, "EGL") != 0 && strcmp(kind, "GLESv2") != 0 && strcmp(kind, "GLESv1_CM") != 0)
506 return nullptr;
507
508 void* so = nullptr;
509 std::string name;
Ian Elliottb058aff2018-08-09 12:00:55 -0600510 char prop[PROPERTY_VALUE_MAX];
Cody Northrop1f00e172018-04-02 11:23:31 -0600511
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600512 android_namespace_t* ns = android_getAngleNamespace();
513 const char* app_name = android_getAngleAppName();
Cody Northrop49d51992018-08-29 16:37:09 -0600514 const char* app_pref = android_getAngleAppPref();
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600515 bool developer_opt_in = android_getAngleDeveloperOptIn();
516
Ian Elliott34474472018-09-05 10:57:07 -0600517 // Determine whether or not to use ANGLE:
518 ANGLEPreference developer_option = developer_opt_in ? ANGLE_PREFER_ANGLE : ANGLE_NO_PREFERENCE;
519 bool use_angle = (developer_option == ANGLE_PREFER_ANGLE);
520
521 if (use_angle) {
522 ALOGV("User set \"Developer Options\" to force the use of ANGLE");
523 } else {
524 // The "Developer Options" value wasn't set to force the use of ANGLE. Need to temporarily
525 // load ANGLE and call the updatable opt-in/out logic:
526 std::string app_name_str = app_name ? app_name : "";
527 char manufacturer[PROPERTY_VALUE_MAX];
528 char model[PROPERTY_VALUE_MAX];
529 property_get("ro.product.manufacturer", manufacturer, "UNSET");
530 property_get("ro.product.model", model, "UNSET");
531 ANGLEPreference app_preference = getAnglePref(android_getAngleAppPref());
532
Courtney Goeltzenleuchter075437f2018-10-08 19:35:31 -0600533 so = load_angle_from_namespace("feature_support", ns);
Ian Elliott34474472018-09-05 10:57:07 -0600534 if (so) {
535 ALOGV("Temporarily loaded ANGLE's opt-in/out logic from namespace");
536 fpANGLEUseForApplication fp =
537 (fpANGLEUseForApplication)dlsym(so, "ANGLEUseForApplication");
538 if (fp) {
539 use_angle = (fp)(app_name_str.c_str(), manufacturer, model, developer_option,
540 app_preference);
541 ALOGV("Result of opt-in/out logic is %s", use_angle ? "true" : "false");
Courtney Goeltzenleuchter075437f2018-10-08 19:35:31 -0600542 } else {
543 ALOGW("Cannot find ANGLEUseForApplication in library");
Ian Elliott34474472018-09-05 10:57:07 -0600544 }
545
546 ALOGV("Close temporarily-loaded ANGLE opt-in/out logic");
547 dlclose(so);
548 so = nullptr;
549 } else {
550 // We weren't able to load and call the updateable opt-in/out logic.
551 // If we can't load the library, there is no ANGLE available.
552 use_angle = false;
553 ALOGV("Could not temporarily-load the ANGLE opt-in/out logic, cannot use ANGLE.");
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600554 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600555 }
Ian Elliott34474472018-09-05 10:57:07 -0600556 if (use_angle) {
557 so = load_angle_from_namespace(kind, ns);
558 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600559
560 if (so) {
Ian Elliott34474472018-09-05 10:57:07 -0600561 ALOGV("Loaded ANGLE %s library for %s (instead of native)",
Ian Elliottb058aff2018-08-09 12:00:55 -0600562 kind, app_name ? app_name : "nullptr");
563 property_get("debug.angle.backend", prop, "UNSET");
Ian Elliott34474472018-09-05 10:57:07 -0600564 ALOGV("ANGLE's backend set to %s", prop);
Ian Elliottb058aff2018-08-09 12:00:55 -0600565 property_get("debug.hwui.renderer", prop, "UNSET");
Ian Elliott34474472018-09-05 10:57:07 -0600566 ALOGV("Skia's renderer set to %s", prop);
Cody Northrop1f00e172018-04-02 11:23:31 -0600567 cnx->useAngle = true;
568 // Find and load vendor libEGL for ANGLE
569 if (!cnx->vendorEGL) {
570 cnx->vendorEGL = load_system_driver("EGL");
571 }
572 return so;
Ian Elliottb058aff2018-08-09 12:00:55 -0600573 } else {
Ian Elliott34474472018-09-05 10:57:07 -0600574 ALOGV("Loaded native %s library for %s (instead of ANGLE)",
Ian Elliottb058aff2018-08-09 12:00:55 -0600575 kind, app_name ? app_name : "nullptr");
Cody Northrop1f00e172018-04-02 11:23:31 -0600576 }
577
578 return nullptr;
579}
580
Mathias Agopian311b4792017-02-28 15:00:49 -0800581static const char* HAL_SUBNAME_KEY_PROPERTIES[2] = {
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800582 "ro.hardware.egl",
583 "ro.board.platform",
Mathias Agopian311b4792017-02-28 15:00:49 -0800584};
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800585
586static void* load_updated_driver(const char* kind, android_namespace_t* ns) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800587 ATRACE_CALL();
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800588 const android_dlextinfo dlextinfo = {
589 .flags = ANDROID_DLEXT_USE_NAMESPACE,
590 .library_namespace = ns,
591 };
592 void* so = nullptr;
593 char prop[PROPERTY_VALUE_MAX + 1];
594 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
595 if (property_get(key, prop, nullptr) > 0) {
Mathias Agopian65421432017-03-08 11:49:05 -0800596 std::string name = std::string("lib") + kind + "_" + prop + ".so";
597 so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
Mathias Agopian311b4792017-02-28 15:00:49 -0800598 if (so) {
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800599 return so;
Mathias Agopian311b4792017-02-28 15:00:49 -0800600 }
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800601 }
602 }
603 return nullptr;
604}
605
606void *Loader::load_driver(const char* kind,
607 egl_connection_t* cnx, uint32_t mask)
608{
Jesse Hall1508ae62017-01-19 17:43:26 -0800609 ATRACE_CALL();
610
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800611 void* dso = nullptr;
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600612 dso = load_angle(kind, cnx);
Jiyong Parka243e5d2017-06-21 12:26:51 +0900613#ifndef __ANDROID_VNDK__
Cody Northrop1f00e172018-04-02 11:23:31 -0600614 if (!dso) {
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600615 android_namespace_t* ns = android_getDriverNamespace();
Cody Northrop1f00e172018-04-02 11:23:31 -0600616 if (ns) {
617 dso = load_updated_driver(kind, ns);
618 }
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800619 }
Jiyong Parka243e5d2017-06-21 12:26:51 +0900620#endif
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800621 if (!dso) {
622 dso = load_system_driver(kind);
623 if (!dso)
Yi Kong48a6cd22018-07-18 10:07:09 -0700624 return nullptr;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800625 }
626
Cody Northrop87a562c2018-10-04 14:44:11 -0600627 if (mask & PLATFORM) {
628 // For each entrypoint tracked by the platform
629 char const* const* entries = platform_names;
630 EGLFuncPointer* curr = reinterpret_cast<EGLFuncPointer*>(&cnx->platform);
631
632 while (*entries) {
633 const char* name = *entries;
634 EGLFuncPointer f = FindPlatformImplAddr(name);
635
636 if (f == nullptr) {
637 // If no entry found, update the lookup table: sPlatformImplMap
638 ALOGE("No entry found in platform lookup table for %s", name);
639 }
640
641 *curr++ = f;
642 entries++;
643 }
644 }
645
Mathias Agopiande586972009-05-28 17:39:03 -0700646 if (mask & EGL) {
647 getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress");
648
Jesse Hall94cdba92013-07-11 09:40:35 -0700649 ALOGE_IF(!getProcAddress,
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800650 "can't find eglGetProcAddress() in EGL driver library");
Mathias Agopiande586972009-05-28 17:39:03 -0700651
Mathias Agopian618fa102009-10-14 02:06:37 -0700652 egl_t* egl = &cnx->egl;
Mathias Agopiande586972009-05-28 17:39:03 -0700653 __eglMustCastToProperFunctionPointerType* curr =
654 (__eglMustCastToProperFunctionPointerType*)egl;
655 char const * const * api = egl_names;
656 while (*api) {
657 char const * name = *api;
Jesse Hall94cdba92013-07-11 09:40:35 -0700658 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700659 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700660 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700661 // couldn't find the entry-point, use eglGetProcAddress()
662 f = getProcAddress(name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700663 if (f == nullptr) {
664 f = (__eglMustCastToProperFunctionPointerType)nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700665 }
666 }
667 *curr++ = f;
668 api++;
669 }
670 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700671
Mathias Agopiande586972009-05-28 17:39:03 -0700672 if (mask & GLESv1_CM) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700673 init_api(dso, gl_names,
674 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800675 &cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl,
Mathias Agopian618fa102009-10-14 02:06:37 -0700676 getProcAddress);
Mathias Agopiande586972009-05-28 17:39:03 -0700677 }
678
679 if (mask & GLESv2) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700680 init_api(dso, gl_names,
681 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800682 &cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl,
Mathias Agopiande586972009-05-28 17:39:03 -0700683 getProcAddress);
684 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700685
Mathias Agopiande586972009-05-28 17:39:03 -0700686 return dso;
687}
688
689// ----------------------------------------------------------------------------
690}; // namespace android
691// ----------------------------------------------------------------------------