blob: 4cafe2b8190881c6da133e323be0a4eef529c8e9 [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
Tim Van Patten5f744f12018-12-12 11:46:21 -070020#include <EGL/Loader.h>
Mathias Agopian311b4792017-02-28 15:00:49 -080021
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
41// ----------------------------------------------------------------------------
42namespace android {
43// ----------------------------------------------------------------------------
44
45
46/*
Mathias Agopian99381422013-04-23 20:52:29 +020047 * EGL userspace drivers must be provided either:
48 * - as a single library:
49 * /vendor/lib/egl/libGLES.so
50 *
51 * - as separate libraries:
52 * /vendor/lib/egl/libEGL.so
53 * /vendor/lib/egl/libGLESv1_CM.so
54 * /vendor/lib/egl/libGLESv2.so
55 *
56 * The software renderer for the emulator must be provided as a single
57 * library at:
58 *
59 * /system/lib/egl/libGLES_android.so
60 *
61 *
62 * For backward compatibility and to facilitate the transition to
63 * this new naming scheme, the loader will additionally look for:
Jesse Hall94cdba92013-07-11 09:40:35 -070064 *
Mathias Agopian99381422013-04-23 20:52:29 +020065 * /{vendor|system}/lib/egl/lib{GLES | [EGL|GLESv1_CM|GLESv2]}_*.so
Jesse Hall94cdba92013-07-11 09:40:35 -070066 *
Mathias Agopiande586972009-05-28 17:39:03 -070067 */
68
Mathias Agopian65421432017-03-08 11:49:05 -080069Loader& Loader::getInstance() {
70 static Loader loader;
71 return loader;
72}
Mathias Agopiande586972009-05-28 17:39:03 -070073
David 'Digit' Turner80b30c22011-08-26 17:38:47 +020074/* This function is called to check whether we run inside the emulator,
75 * and if this is the case whether GLES GPU emulation is supported.
76 *
77 * Returned values are:
78 * -1 -> not running inside the emulator
79 * 0 -> running inside the emulator, but GPU emulation not supported
80 * 1 -> running inside the emulator, GPU emulation is supported
Nicolas Capens776951f2015-11-06 10:10:21 -050081 * through the "emulation" host-side OpenGL ES implementation.
82 * 2 -> running inside the emulator, GPU emulation is supported
83 * through a guest-side vendor driver's OpenGL ES implementation.
David 'Digit' Turner80b30c22011-08-26 17:38:47 +020084 */
85static int
86checkGlesEmulationStatus(void)
87{
88 /* We're going to check for the following kernel parameters:
89 *
90 * qemu=1 -> tells us that we run inside the emulator
91 * android.qemu.gles=<number> -> tells us the GLES GPU emulation status
92 *
93 * Note that we will return <number> if we find it. This let us support
94 * more additionnal emulation modes in the future.
95 */
96 char prop[PROPERTY_VALUE_MAX];
97 int result = -1;
98
99 /* First, check for qemu=1 */
100 property_get("ro.kernel.qemu",prop,"0");
101 if (atoi(prop) != 1)
102 return -1;
103
104 /* We are in the emulator, get GPU status value */
bohu69e5b1a2016-02-19 17:15:20 -0800105 property_get("qemu.gles",prop,"0");
David 'Digit' Turner80b30c22011-08-26 17:38:47 +0200106 return atoi(prop);
107}
108
Jesse Hall1508ae62017-01-19 17:43:26 -0800109static void* do_dlopen(const char* path, int mode) {
110 ATRACE_CALL();
111 return dlopen(path, mode);
112}
113
Jiyong Park5910dc72017-04-05 14:23:52 +0900114static void* do_android_dlopen_ext(const char* path, int mode, const android_dlextinfo* info) {
115 ATRACE_CALL();
116 return android_dlopen_ext(path, mode, info);
117}
118
Justin Yunb7320302017-05-22 15:13:40 +0900119static void* do_android_load_sphal_library(const char* path, int mode) {
120 ATRACE_CALL();
121 return android_load_sphal_library(path, mode);
122}
123
Mathias Agopiande586972009-05-28 17:39:03 -0700124// ----------------------------------------------------------------------------
125
Jesse Hall94cdba92013-07-11 09:40:35 -0700126Loader::driver_t::driver_t(void* gles)
Mathias Agopiande586972009-05-28 17:39:03 -0700127{
128 dso[0] = gles;
129 for (size_t i=1 ; i<NELEM(dso) ; i++)
Yi Kong48a6cd22018-07-18 10:07:09 -0700130 dso[i] = nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700131}
132
Jesse Hall94cdba92013-07-11 09:40:35 -0700133Loader::driver_t::~driver_t()
Mathias Agopiande586972009-05-28 17:39:03 -0700134{
135 for (size_t i=0 ; i<NELEM(dso) ; i++) {
136 if (dso[i]) {
137 dlclose(dso[i]);
Yi Kong48a6cd22018-07-18 10:07:09 -0700138 dso[i] = nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700139 }
140 }
141}
142
Mathias Agopian65421432017-03-08 11:49:05 -0800143int Loader::driver_t::set(void* hnd, int32_t api)
Mathias Agopiande586972009-05-28 17:39:03 -0700144{
145 switch (api) {
146 case EGL:
147 dso[0] = hnd;
148 break;
149 case GLESv1_CM:
150 dso[1] = hnd;
151 break;
152 case GLESv2:
153 dso[2] = hnd;
154 break;
155 default:
Mathias Agopian65421432017-03-08 11:49:05 -0800156 return -EOVERFLOW;
Mathias Agopiande586972009-05-28 17:39:03 -0700157 }
Mathias Agopian65421432017-03-08 11:49:05 -0800158 return 0;
Mathias Agopiande586972009-05-28 17:39:03 -0700159}
160
161// ----------------------------------------------------------------------------
162
Mathias Agopiande586972009-05-28 17:39:03 -0700163Loader::Loader()
Yi Kong48a6cd22018-07-18 10:07:09 -0700164 : getProcAddress(nullptr)
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800165{
Mathias Agopiande586972009-05-28 17:39:03 -0700166}
167
Mathias Agopian99381422013-04-23 20:52:29 +0200168Loader::~Loader() {
Mathias Agopiande586972009-05-28 17:39:03 -0700169}
170
Jesse Hallc07b5202013-07-04 12:08:16 -0700171static void* load_wrapper(const char* path) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800172 void* so = do_dlopen(path, RTLD_NOW | RTLD_LOCAL);
Jesse Hallc07b5202013-07-04 12:08:16 -0700173 ALOGE_IF(!so, "dlopen(\"%s\") failed: %s", path, dlerror());
174 return so;
175}
176
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700177#ifndef EGL_WRAPPER_DIR
178#if defined(__LP64__)
179#define EGL_WRAPPER_DIR "/system/lib64"
180#else
181#define EGL_WRAPPER_DIR "/system/lib"
182#endif
183#endif
184
bohu69e5b1a2016-02-19 17:15:20 -0800185static void setEmulatorGlesValue(void) {
186 char prop[PROPERTY_VALUE_MAX];
187 property_get("ro.kernel.qemu", prop, "0");
188 if (atoi(prop) != 1) return;
189
190 property_get("ro.kernel.qemu.gles",prop,"0");
191 if (atoi(prop) == 1) {
192 ALOGD("Emulator has host GPU support, qemu.gles is set to 1.");
193 property_set("qemu.gles", "1");
194 return;
195 }
196
197 // for now, checking the following
198 // directory is good enough for emulator system images
199 const char* vendor_lib_path =
200#if defined(__LP64__)
201 "/vendor/lib64/egl";
202#else
203 "/vendor/lib/egl";
204#endif
205
206 const bool has_vendor_lib = (access(vendor_lib_path, R_OK) == 0);
207 if (has_vendor_lib) {
208 ALOGD("Emulator has vendor provided software renderer, qemu.gles is set to 2.");
209 property_set("qemu.gles", "2");
210 } else {
211 ALOGD("Emulator without GPU support detected. "
212 "Fallback to legacy software renderer, qemu.gles is set to 0.");
213 property_set("qemu.gles", "0");
214 }
215}
216
Mathias Agopianada798b2012-02-13 17:09:30 -0800217void* Loader::open(egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700218{
Jesse Hall1508ae62017-01-19 17:43:26 -0800219 ATRACE_CALL();
220
Mathias Agopiande586972009-05-28 17:39:03 -0700221 void* dso;
Yi Kong48a6cd22018-07-18 10:07:09 -0700222 driver_t* hnd = nullptr;
Jesse Hall94cdba92013-07-11 09:40:35 -0700223
bohu69e5b1a2016-02-19 17:15:20 -0800224 setEmulatorGlesValue();
225
Tim Van Patten5f744f12018-12-12 11:46:21 -0700226 // Check if we should use ANGLE early, so loading each driver doesn't require repeated queries.
227 if (android::GraphicsEnv::getInstance().shouldUseAngle()) {
228 cnx->shouldUseAngle = true;
229 } else {
230 cnx->shouldUseAngle = false;
231 }
232
Cody Northrop9d5d33d2018-10-15 18:32:14 -0600233 dso = load_driver("GLES", cnx, EGL | GLESv1_CM | GLESv2);
Mathias Agopian99381422013-04-23 20:52:29 +0200234 if (dso) {
235 hnd = new driver_t(dso);
236 } else {
237 // Always load EGL first
Cody Northrop9d5d33d2018-10-15 18:32:14 -0600238 dso = load_driver("EGL", cnx, EGL);
Mathias Agopiande586972009-05-28 17:39:03 -0700239 if (dso) {
240 hnd = new driver_t(dso);
Mathias Agopian99381422013-04-23 20:52:29 +0200241 hnd->set( load_driver("GLESv1_CM", cnx, GLESv1_CM), GLESv1_CM );
242 hnd->set( load_driver("GLESv2", cnx, GLESv2), GLESv2 );
Mathias Agopiande586972009-05-28 17:39:03 -0700243 }
244 }
245
Mathias Agopian99381422013-04-23 20:52:29 +0200246 LOG_ALWAYS_FATAL_IF(!hnd, "couldn't find an OpenGL ES implementation");
Jesse Hallc07b5202013-07-04 12:08:16 -0700247
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700248 cnx->libEgl = load_wrapper(EGL_WRAPPER_DIR "/libEGL.so");
249 cnx->libGles2 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv2.so");
250 cnx->libGles1 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv1_CM.so");
251
Michael Chockc0ec5e22014-01-27 08:14:33 -0800252 LOG_ALWAYS_FATAL_IF(!cnx->libEgl,
253 "couldn't load system EGL wrapper libraries");
254
Jesse Hallc07b5202013-07-04 12:08:16 -0700255 LOG_ALWAYS_FATAL_IF(!cnx->libGles2 || !cnx->libGles1,
256 "couldn't load system OpenGL ES wrapper libraries");
257
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800258 android::GraphicsEnv::getInstance().sendGpuStats();
259
Mathias Agopiande586972009-05-28 17:39:03 -0700260 return (void*)hnd;
261}
262
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600263void Loader::close(egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700264{
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600265 driver_t* hnd = (driver_t*) cnx->dso;
Mathias Agopiande586972009-05-28 17:39:03 -0700266 delete hnd;
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600267 cnx->dso = nullptr;
268
Tim Van Patten5f744f12018-12-12 11:46:21 -0700269 cnx->shouldUseAngle = false;
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600270 cnx->angleDecided = false;
271 cnx->useAngle = false;
272
273 if (cnx->vendorEGL) {
274 dlclose(cnx->vendorEGL);
275 cnx->vendorEGL = nullptr;
276 }
Mathias Agopiande586972009-05-28 17:39:03 -0700277}
278
Jesse Hall94cdba92013-07-11 09:40:35 -0700279void Loader::init_api(void* dso,
280 char const * const * api,
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700281 char const * const * ref_api,
Jesse Hall94cdba92013-07-11 09:40:35 -0700282 __eglMustCastToProperFunctionPointerType* curr,
283 getProcAddressType getProcAddress)
Mathias Agopiande586972009-05-28 17:39:03 -0700284{
Jesse Hall1508ae62017-01-19 17:43:26 -0800285 ATRACE_CALL();
286
Mathias Agopian7773c432012-02-13 20:06:08 -0800287 const ssize_t SIZE = 256;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700288 char scrap[SIZE];
Mathias Agopiande586972009-05-28 17:39:03 -0700289 while (*api) {
290 char const * name = *api;
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700291 if (ref_api) {
292 char const * ref_name = *ref_api;
293 if (std::strcmp(name, ref_name) != 0) {
294 *curr++ = nullptr;
295 ref_api++;
296 continue;
297 }
298 }
299
Jesse Hall94cdba92013-07-11 09:40:35 -0700300 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700301 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700302 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700303 // couldn't find the entry-point, use eglGetProcAddress()
304 f = getProcAddress(name);
305 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700306 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700307 // Try without the OES postfix
308 ssize_t index = ssize_t(strlen(name)) - 3;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700309 if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) {
Mathias Agopiande586972009-05-28 17:39:03 -0700310 strncpy(scrap, name, index);
311 scrap[index] = 0;
312 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000313 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700314 }
315 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700316 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700317 // Try with the OES postfix
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700318 ssize_t index = ssize_t(strlen(name)) - 3;
319 if (index>0 && strcmp(name+index, "OES")) {
320 snprintf(scrap, SIZE, "%sOES", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700321 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000322 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700323 }
324 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700325 if (f == nullptr) {
Steve Block9d453682011-12-20 16:23:08 +0000326 //ALOGD("%s", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700327 f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented;
Mathias Agopian48d438d2012-01-28 21:44:00 -0800328
329 /*
330 * GL_EXT_debug_label is special, we always report it as
331 * supported, it's handled by GLES_trace. If GLES_trace is not
332 * enabled, then these are no-ops.
333 */
334 if (!strcmp(name, "glInsertEventMarkerEXT")) {
335 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
336 } else if (!strcmp(name, "glPushGroupMarkerEXT")) {
337 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
338 } else if (!strcmp(name, "glPopGroupMarkerEXT")) {
339 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
340 }
Mathias Agopiande586972009-05-28 17:39:03 -0700341 }
342 *curr++ = f;
343 api++;
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700344 if (ref_api) ref_api++;
Mathias Agopiande586972009-05-28 17:39:03 -0700345 }
346}
347
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800348static void* load_system_driver(const char* kind) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800349 ATRACE_CALL();
Mathias Agopian99381422013-04-23 20:52:29 +0200350 class MatchFile {
351 public:
Mathias Agopian65421432017-03-08 11:49:05 -0800352 static std::string find(const char* kind) {
353 std::string result;
Nicolas Capens776951f2015-11-06 10:10:21 -0500354 int emulationStatus = checkGlesEmulationStatus();
355 switch (emulationStatus) {
356 case 0:
Nicolas Capens776951f2015-11-06 10:10:21 -0500357#if defined(__LP64__)
bohu3adf9e12017-07-02 22:08:13 -0700358 result = "/vendor/lib64/egl/libGLES_android.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500359#else
bohu3adf9e12017-07-02 22:08:13 -0700360 result = "/vendor/lib/egl/libGLES_android.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500361#endif
362 return result;
363 case 1:
364 // Use host-side OpenGL through the "emulation" library
365#if defined(__LP64__)
bohu3adf9e12017-07-02 22:08:13 -0700366 result = std::string("/vendor/lib64/egl/lib") + kind + "_emulation.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500367#else
bohu3adf9e12017-07-02 22:08:13 -0700368 result = std::string("/vendor/lib/egl/lib") + kind + "_emulation.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500369#endif
370 return result;
Lingfeng Yang0f82d872017-08-11 01:18:00 -0700371 case 2:
372 // Use guest side swiftshader library
373#if defined(__LP64__)
374 result = std::string("/vendor/lib64/egl/lib") + kind + "_swiftshader.so";
375#else
376 result = std::string("/vendor/lib/egl/lib") + kind + "_swiftshader.so";
377#endif
378 return result;
Nicolas Capens776951f2015-11-06 10:10:21 -0500379 default:
380 // Not in emulator, or use other guest-side implementation
381 break;
382 }
383
Mathias Agopian65421432017-03-08 11:49:05 -0800384 std::string pattern = std::string("lib") + kind;
Mathias Agopian99381422013-04-23 20:52:29 +0200385 const char* const searchPaths[] = {
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800386#if defined(__LP64__)
387 "/vendor/lib64/egl",
388 "/system/lib64/egl"
389#else
Mathias Agopian99381422013-04-23 20:52:29 +0200390 "/vendor/lib/egl",
391 "/system/lib/egl"
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800392#endif
Mathias Agopian99381422013-04-23 20:52:29 +0200393 };
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700394
Mathias Agopian99381422013-04-23 20:52:29 +0200395 // first, we search for the exact name of the GLES userspace
396 // driver in both locations.
397 // i.e.:
398 // libGLES.so, or:
399 // libEGL.so, libGLESv1_CM.so, libGLESv2.so
400
401 for (size_t i=0 ; i<NELEM(searchPaths) ; i++) {
402 if (find(result, pattern, searchPaths[i], true)) {
403 return result;
404 }
405 }
406
407 // for compatibility with the old "egl.cfg" naming convention
408 // we look for files that match:
409 // libGLES_*.so, or:
410 // libEGL_*.so, libGLESv1_CM_*.so, libGLESv2_*.so
411
412 pattern.append("_");
413 for (size_t i=0 ; i<NELEM(searchPaths) ; i++) {
414 if (find(result, pattern, searchPaths[i], false)) {
415 return result;
416 }
417 }
418
419 // we didn't find the driver. gah.
420 result.clear();
421 return result;
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700422 }
Mathias Agopian99381422013-04-23 20:52:29 +0200423
424 private:
Mathias Agopian65421432017-03-08 11:49:05 -0800425 static bool find(std::string& result,
426 const std::string& pattern, const char* const search, bool exact) {
Mathias Agopian99381422013-04-23 20:52:29 +0200427 if (exact) {
Jesse Hall4aaa9602017-08-29 16:33:54 -0700428 std::string absolutePath = std::string(search) + "/" + pattern + ".so";
Mathias Agopian65421432017-03-08 11:49:05 -0800429 if (!access(absolutePath.c_str(), R_OK)) {
Mathias Agopian99381422013-04-23 20:52:29 +0200430 result = absolutePath;
431 return true;
432 }
433 return false;
434 }
435
436 DIR* d = opendir(search);
Yi Kong48a6cd22018-07-18 10:07:09 -0700437 if (d != nullptr) {
Mathias Agopian99381422013-04-23 20:52:29 +0200438 struct dirent* e;
Yi Kong48a6cd22018-07-18 10:07:09 -0700439 while ((e = readdir(d)) != nullptr) {
Mathias Agopian99381422013-04-23 20:52:29 +0200440 if (e->d_type == DT_DIR) {
441 continue;
442 }
443 if (!strcmp(e->d_name, "libGLES_android.so")) {
444 // always skip the software renderer
445 continue;
446 }
Mathias Agopian65421432017-03-08 11:49:05 -0800447 if (strstr(e->d_name, pattern.c_str()) == e->d_name) {
Mathias Agopian99381422013-04-23 20:52:29 +0200448 if (!strcmp(e->d_name + strlen(e->d_name) - 3, ".so")) {
Mathias Agopian65421432017-03-08 11:49:05 -0800449 result = std::string(search) + "/" + e->d_name;
Mathias Agopian99381422013-04-23 20:52:29 +0200450 closedir(d);
451 return true;
452 }
453 }
454 }
455 closedir(d);
456 }
457 return false;
458 }
459 };
460
461
Mathias Agopian65421432017-03-08 11:49:05 -0800462 std::string absolutePath = MatchFile::find(kind);
463 if (absolutePath.empty()) {
Mathias Agopian99381422013-04-23 20:52:29 +0200464 // this happens often, we don't want to log an error
Yi Kong48a6cd22018-07-18 10:07:09 -0700465 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700466 }
Mathias Agopian65421432017-03-08 11:49:05 -0800467 const char* const driver_absolute_path = absolutePath.c_str();
Mathias Agopiande586972009-05-28 17:39:03 -0700468
Jiyong Park5910dc72017-04-05 14:23:52 +0900469 // Try to load drivers from the 'sphal' namespace, if it exist. Fall back to
Justin Yunb7320302017-05-22 15:13:40 +0900470 // the original routine when the namespace does not exist.
Jiyong Park5910dc72017-04-05 14:23:52 +0900471 // See /system/core/rootdir/etc/ld.config.txt for the configuration of the
472 // sphal namespace.
Justin Yunb7320302017-05-22 15:13:40 +0900473 void* dso = do_android_load_sphal_library(driver_absolute_path,
474 RTLD_NOW | RTLD_LOCAL);
Yi Kong48a6cd22018-07-18 10:07:09 -0700475 if (dso == nullptr) {
Mathias Agopian8c173842009-09-20 16:01:02 -0700476 const char* err = dlerror();
Mathias Agopian65421432017-03-08 11:49:05 -0800477 ALOGE("load_driver(%s): %s", driver_absolute_path, err ? err : "unknown");
Yi Kong48a6cd22018-07-18 10:07:09 -0700478 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700479 }
480
Steve Block9d453682011-12-20 16:23:08 +0000481 ALOGD("loaded %s", driver_absolute_path);
Mathias Agopianbaca89c2009-08-20 19:09:34 -0700482
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800483 return dso;
484}
485
Cody Northrop1f00e172018-04-02 11:23:31 -0600486static void* load_angle_from_namespace(const char* kind, android_namespace_t* ns) {
487 const android_dlextinfo dlextinfo = {
488 .flags = ANDROID_DLEXT_USE_NAMESPACE,
489 .library_namespace = ns,
490 };
491
492 std::string name = std::string("lib") + kind + "_angle.so";
493
494 void* so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
495
496 if (so) {
497 ALOGD("dlopen_ext from APK (%s) success at %p", name.c_str(), so);
498 return so;
499 } else {
500 ALOGE("dlopen_ext(\"%s\") failed: %s", name.c_str(), dlerror());
501 }
502
503 return nullptr;
504}
505
Cody Northrop6d082ef2018-09-11 09:42:31 -0600506static void* load_angle(const char* kind, android_namespace_t* ns, egl_connection_t* cnx) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600507 // Only attempt to load ANGLE libs
Tim Van Patten5f744f12018-12-12 11:46:21 -0700508 if (strcmp(kind, "EGL") != 0 && strcmp(kind, "GLESv2") != 0 && strcmp(kind, "GLESv1_CM") != 0) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600509 return nullptr;
Cody Northrop1f00e172018-04-02 11:23:31 -0600510 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700511
Courtney Goeltzenleuchter985bc282018-11-02 14:24:55 -0600512 void* so = nullptr;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700513
514 if ((cnx->shouldUseAngle) || android::GraphicsEnv::getInstance().shouldUseAngle()) {
Ian Elliott34474472018-09-05 10:57:07 -0600515 so = load_angle_from_namespace(kind, ns);
Tim Van Patten5f744f12018-12-12 11:46:21 -0700516 cnx->shouldUseAngle = true;
517 } else {
518 cnx->shouldUseAngle = false;
Ian Elliott34474472018-09-05 10:57:07 -0600519 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600520
521 if (so) {
Tim Van Patten5f744f12018-12-12 11:46:21 -0700522 ALOGV("Loaded ANGLE %s library for '%s' (instead of native)", kind,
523 android::GraphicsEnv::getInstance().getAngleAppName().c_str());
Cody Northrop1f00e172018-04-02 11:23:31 -0600524 cnx->useAngle = true;
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600525
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600526 char prop[PROPERTY_VALUE_MAX];
Tim Van Patten5f744f12018-12-12 11:46:21 -0700527
528 property_get("debug.hwui.renderer", prop, "UNSET");
529 ALOGV("Skia's renderer set to %s", prop);
530
531 EGLint angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE;
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600532 property_get("debug.angle.backend", prop, "0");
533 switch (atoi(prop)) {
534 case 1:
535 ALOGV("%s: Requesting OpenGLES back-end", __FUNCTION__);
536 angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE;
537 break;
538 case 2:
539 ALOGV("%s: Requesting Vulkan back-end", __FUNCTION__);
540 angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE;
541 break;
542 default:
543 break;
544 }
545
546 cnx->angleBackend = angleBackendDefault;
547 if (!cnx->vendorEGL && (cnx->angleBackend == EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE)) {
548 // Find and load vendor libEGL for ANGLE's GL back-end to use.
Cody Northrop1f00e172018-04-02 11:23:31 -0600549 cnx->vendorEGL = load_system_driver("EGL");
550 }
Ian Elliottb058aff2018-08-09 12:00:55 -0600551 } else {
Tim Van Patten5f744f12018-12-12 11:46:21 -0700552 ALOGV("Loaded native %s library for '%s' (instead of ANGLE)", kind,
553 android::GraphicsEnv::getInstance().getAngleAppName().c_str());
554 cnx->useAngle = false;
Cody Northrop1f00e172018-04-02 11:23:31 -0600555 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700556 cnx->angleDecided = true;
Cody Northrop1f00e172018-04-02 11:23:31 -0600557
Tim Van Patten5f744f12018-12-12 11:46:21 -0700558 return so;
Cody Northrop1f00e172018-04-02 11:23:31 -0600559}
560
Mathias Agopian311b4792017-02-28 15:00:49 -0800561static const char* HAL_SUBNAME_KEY_PROPERTIES[2] = {
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800562 "ro.hardware.egl",
563 "ro.board.platform",
Mathias Agopian311b4792017-02-28 15:00:49 -0800564};
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800565
566static void* load_updated_driver(const char* kind, android_namespace_t* ns) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800567 ATRACE_CALL();
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800568 const android_dlextinfo dlextinfo = {
569 .flags = ANDROID_DLEXT_USE_NAMESPACE,
570 .library_namespace = ns,
571 };
572 void* so = nullptr;
573 char prop[PROPERTY_VALUE_MAX + 1];
574 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
575 if (property_get(key, prop, nullptr) > 0) {
Mathias Agopian65421432017-03-08 11:49:05 -0800576 std::string name = std::string("lib") + kind + "_" + prop + ".so";
577 so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
Mathias Agopian311b4792017-02-28 15:00:49 -0800578 if (so) {
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800579 return so;
Mathias Agopian311b4792017-02-28 15:00:49 -0800580 }
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800581 }
582 }
583 return nullptr;
584}
585
586void *Loader::load_driver(const char* kind,
587 egl_connection_t* cnx, uint32_t mask)
588{
Jesse Hall1508ae62017-01-19 17:43:26 -0800589 ATRACE_CALL();
590
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800591 void* dso = nullptr;
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600592 android_namespace_t* ns = android::GraphicsEnv::getInstance().getAngleNamespace();
Cody Northrop6d082ef2018-09-11 09:42:31 -0600593 if (ns) {
594 dso = load_angle(kind, ns, cnx);
595 }
Jiyong Parka243e5d2017-06-21 12:26:51 +0900596#ifndef __ANDROID_VNDK__
Cody Northrop1f00e172018-04-02 11:23:31 -0600597 if (!dso) {
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600598 android_namespace_t* ns = android::GraphicsEnv::getInstance().getDriverNamespace();
Cody Northrop1f00e172018-04-02 11:23:31 -0600599 if (ns) {
600 dso = load_updated_driver(kind, ns);
601 }
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800602 }
Jiyong Parka243e5d2017-06-21 12:26:51 +0900603#endif
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800604 if (!dso) {
605 dso = load_system_driver(kind);
606 if (!dso)
Yi Kong48a6cd22018-07-18 10:07:09 -0700607 return nullptr;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800608 }
609
Mathias Agopiande586972009-05-28 17:39:03 -0700610 if (mask & EGL) {
611 getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress");
612
Jesse Hall94cdba92013-07-11 09:40:35 -0700613 ALOGE_IF(!getProcAddress,
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800614 "can't find eglGetProcAddress() in EGL driver library");
Mathias Agopiande586972009-05-28 17:39:03 -0700615
Mathias Agopian618fa102009-10-14 02:06:37 -0700616 egl_t* egl = &cnx->egl;
Mathias Agopiande586972009-05-28 17:39:03 -0700617 __eglMustCastToProperFunctionPointerType* curr =
618 (__eglMustCastToProperFunctionPointerType*)egl;
619 char const * const * api = egl_names;
620 while (*api) {
621 char const * name = *api;
Jesse Hall94cdba92013-07-11 09:40:35 -0700622 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700623 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700624 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700625 // couldn't find the entry-point, use eglGetProcAddress()
626 f = getProcAddress(name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700627 if (f == nullptr) {
628 f = (__eglMustCastToProperFunctionPointerType)nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700629 }
630 }
631 *curr++ = f;
632 api++;
633 }
634 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700635
Mathias Agopiande586972009-05-28 17:39:03 -0700636 if (mask & GLESv1_CM) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700637 init_api(dso, gl_names_1, gl_names,
Mathias Agopian618fa102009-10-14 02:06:37 -0700638 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800639 &cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl,
Mathias Agopian618fa102009-10-14 02:06:37 -0700640 getProcAddress);
Mathias Agopiande586972009-05-28 17:39:03 -0700641 }
642
643 if (mask & GLESv2) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700644 init_api(dso, gl_names, nullptr,
Mathias Agopian618fa102009-10-14 02:06:37 -0700645 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800646 &cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl,
Mathias Agopiande586972009-05-28 17:39:03 -0700647 getProcAddress);
648 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700649
Mathias Agopiande586972009-05-28 17:39:03 -0700650 return dso;
651}
652
653// ----------------------------------------------------------------------------
654}; // namespace android
655// ----------------------------------------------------------------------------