blob: 4b73fe915b453f2c1d7a7baabaebea4b825cf479 [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>
Yiwei Zhangd9861812019-02-13 11:51:55 -080030#include <utils/Timers.h>
Mathias Agopian311b4792017-02-28 15:00:49 -080031
Jiyong Parka243e5d2017-06-21 12:26:51 +090032#ifndef __ANDROID_VNDK__
Jiyong Park27c39e12017-05-08 13:00:02 +090033#include <graphicsenv/GraphicsEnv.h>
Jiyong Parka243e5d2017-06-21 12:26:51 +090034#endif
Justin Yunb7320302017-05-22 15:13:40 +090035#include <vndksupport/linker.h>
Mathias Agopiande586972009-05-28 17:39:03 -070036
Cody Northrop68d10352018-10-15 07:22:09 -060037#include "egl_platform_entries.h"
Mathias Agopian65421432017-03-08 11:49:05 -080038#include "egl_trace.h"
Mathias Agopian1cadb252011-05-23 17:26:14 -070039#include "egldefs.h"
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -060040#include <EGL/eglext_angle.h>
Mathias Agopiande586972009-05-28 17:39:03 -070041
Mathias Agopiande586972009-05-28 17:39:03 -070042namespace android {
Mathias Agopiande586972009-05-28 17:39:03 -070043
44/*
Mathias Agopian99381422013-04-23 20:52:29 +020045 * EGL userspace drivers must be provided either:
46 * - as a single library:
47 * /vendor/lib/egl/libGLES.so
48 *
49 * - as separate libraries:
50 * /vendor/lib/egl/libEGL.so
51 * /vendor/lib/egl/libGLESv1_CM.so
52 * /vendor/lib/egl/libGLESv2.so
53 *
54 * The software renderer for the emulator must be provided as a single
55 * library at:
56 *
57 * /system/lib/egl/libGLES_android.so
58 *
59 *
60 * For backward compatibility and to facilitate the transition to
61 * this new naming scheme, the loader will additionally look for:
Jesse Hall94cdba92013-07-11 09:40:35 -070062 *
Mathias Agopian99381422013-04-23 20:52:29 +020063 * /{vendor|system}/lib/egl/lib{GLES | [EGL|GLESv1_CM|GLESv2]}_*.so
Jesse Hall94cdba92013-07-11 09:40:35 -070064 *
Mathias Agopiande586972009-05-28 17:39:03 -070065 */
66
Mathias Agopian65421432017-03-08 11:49:05 -080067Loader& Loader::getInstance() {
68 static Loader loader;
69 return loader;
70}
Mathias Agopiande586972009-05-28 17:39:03 -070071
David 'Digit' Turner80b30c22011-08-26 17:38:47 +020072/* This function is called to check whether we run inside the emulator,
73 * and if this is the case whether GLES GPU emulation is supported.
74 *
75 * Returned values are:
76 * -1 -> not running inside the emulator
77 * 0 -> running inside the emulator, but GPU emulation not supported
78 * 1 -> running inside the emulator, GPU emulation is supported
Nicolas Capens776951f2015-11-06 10:10:21 -050079 * through the "emulation" host-side OpenGL ES implementation.
80 * 2 -> running inside the emulator, GPU emulation is supported
81 * through a guest-side vendor driver's OpenGL ES implementation.
David 'Digit' Turner80b30c22011-08-26 17:38:47 +020082 */
83static int
84checkGlesEmulationStatus(void)
85{
86 /* We're going to check for the following kernel parameters:
87 *
88 * qemu=1 -> tells us that we run inside the emulator
89 * android.qemu.gles=<number> -> tells us the GLES GPU emulation status
90 *
91 * Note that we will return <number> if we find it. This let us support
92 * more additionnal emulation modes in the future.
93 */
94 char prop[PROPERTY_VALUE_MAX];
95 int result = -1;
96
97 /* First, check for qemu=1 */
98 property_get("ro.kernel.qemu",prop,"0");
99 if (atoi(prop) != 1)
100 return -1;
101
102 /* We are in the emulator, get GPU status value */
bohu69e5b1a2016-02-19 17:15:20 -0800103 property_get("qemu.gles",prop,"0");
David 'Digit' Turner80b30c22011-08-26 17:38:47 +0200104 return atoi(prop);
105}
106
Jesse Hall1508ae62017-01-19 17:43:26 -0800107static void* do_dlopen(const char* path, int mode) {
108 ATRACE_CALL();
109 return dlopen(path, mode);
110}
111
Jiyong Park5910dc72017-04-05 14:23:52 +0900112static void* do_android_dlopen_ext(const char* path, int mode, const android_dlextinfo* info) {
113 ATRACE_CALL();
114 return android_dlopen_ext(path, mode, info);
115}
116
Justin Yunb7320302017-05-22 15:13:40 +0900117static void* do_android_load_sphal_library(const char* path, int mode) {
118 ATRACE_CALL();
119 return android_load_sphal_library(path, mode);
120}
121
Jesse Hall94cdba92013-07-11 09:40:35 -0700122Loader::driver_t::driver_t(void* gles)
Mathias Agopiande586972009-05-28 17:39:03 -0700123{
124 dso[0] = gles;
125 for (size_t i=1 ; i<NELEM(dso) ; i++)
Yi Kong48a6cd22018-07-18 10:07:09 -0700126 dso[i] = nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700127}
128
Jesse Hall94cdba92013-07-11 09:40:35 -0700129Loader::driver_t::~driver_t()
Mathias Agopiande586972009-05-28 17:39:03 -0700130{
131 for (size_t i=0 ; i<NELEM(dso) ; i++) {
132 if (dso[i]) {
133 dlclose(dso[i]);
Yi Kong48a6cd22018-07-18 10:07:09 -0700134 dso[i] = nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700135 }
136 }
137}
138
Mathias Agopian65421432017-03-08 11:49:05 -0800139int Loader::driver_t::set(void* hnd, int32_t api)
Mathias Agopiande586972009-05-28 17:39:03 -0700140{
141 switch (api) {
142 case EGL:
143 dso[0] = hnd;
144 break;
145 case GLESv1_CM:
146 dso[1] = hnd;
147 break;
148 case GLESv2:
149 dso[2] = hnd;
150 break;
151 default:
Mathias Agopian65421432017-03-08 11:49:05 -0800152 return -EOVERFLOW;
Mathias Agopiande586972009-05-28 17:39:03 -0700153 }
Mathias Agopian65421432017-03-08 11:49:05 -0800154 return 0;
Mathias Agopiande586972009-05-28 17:39:03 -0700155}
156
Mathias Agopiande586972009-05-28 17:39:03 -0700157Loader::Loader()
Yi Kong48a6cd22018-07-18 10:07:09 -0700158 : getProcAddress(nullptr)
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800159{
Mathias Agopiande586972009-05-28 17:39:03 -0700160}
161
Mathias Agopian99381422013-04-23 20:52:29 +0200162Loader::~Loader() {
Mathias Agopiande586972009-05-28 17:39:03 -0700163}
164
Jesse Hallc07b5202013-07-04 12:08:16 -0700165static void* load_wrapper(const char* path) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800166 void* so = do_dlopen(path, RTLD_NOW | RTLD_LOCAL);
Jesse Hallc07b5202013-07-04 12:08:16 -0700167 ALOGE_IF(!so, "dlopen(\"%s\") failed: %s", path, dlerror());
168 return so;
169}
170
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700171#ifndef EGL_WRAPPER_DIR
172#if defined(__LP64__)
173#define EGL_WRAPPER_DIR "/system/lib64"
174#else
175#define EGL_WRAPPER_DIR "/system/lib"
176#endif
177#endif
178
bohu69e5b1a2016-02-19 17:15:20 -0800179static void setEmulatorGlesValue(void) {
180 char prop[PROPERTY_VALUE_MAX];
181 property_get("ro.kernel.qemu", prop, "0");
182 if (atoi(prop) != 1) return;
183
184 property_get("ro.kernel.qemu.gles",prop,"0");
185 if (atoi(prop) == 1) {
186 ALOGD("Emulator has host GPU support, qemu.gles is set to 1.");
187 property_set("qemu.gles", "1");
188 return;
189 }
190
191 // for now, checking the following
192 // directory is good enough for emulator system images
193 const char* vendor_lib_path =
194#if defined(__LP64__)
195 "/vendor/lib64/egl";
196#else
197 "/vendor/lib/egl";
198#endif
199
200 const bool has_vendor_lib = (access(vendor_lib_path, R_OK) == 0);
201 if (has_vendor_lib) {
202 ALOGD("Emulator has vendor provided software renderer, qemu.gles is set to 2.");
203 property_set("qemu.gles", "2");
204 } else {
205 ALOGD("Emulator without GPU support detected. "
206 "Fallback to legacy software renderer, qemu.gles is set to 0.");
207 property_set("qemu.gles", "0");
208 }
209}
210
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700211static const char* HAL_SUBNAME_KEY_PROPERTIES[2] = {
212 "ro.hardware.egl",
213 "ro.board.platform",
214};
215
Mathias Agopianada798b2012-02-13 17:09:30 -0800216void* Loader::open(egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700217{
Jesse Hall1508ae62017-01-19 17:43:26 -0800218 ATRACE_CALL();
Yiwei Zhangd9861812019-02-13 11:51:55 -0800219 const nsecs_t openTime = systemTime();
Jesse Hall1508ae62017-01-19 17:43:26 -0800220
bohu69e5b1a2016-02-19 17:15:20 -0800221 setEmulatorGlesValue();
222
Tim Van Patten5f744f12018-12-12 11:46:21 -0700223 // Check if we should use ANGLE early, so loading each driver doesn't require repeated queries.
224 if (android::GraphicsEnv::getInstance().shouldUseAngle()) {
225 cnx->shouldUseAngle = true;
226 } else {
227 cnx->shouldUseAngle = false;
228 }
229
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700230 // Firstly, try to load ANGLE driver.
231 driver_t* hnd = attempt_to_load_angle(cnx);
232 if (!hnd) {
233 // Secondly, try to load from driver apk.
234 hnd = attempt_to_load_updated_driver(cnx);
235 }
236 if (!hnd) {
237 // Thirdly, try to load emulation driver.
238 hnd = attempt_to_load_emulation_driver(cnx);
239 }
240 if (!hnd) {
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700241 // Finally, try to load system driver, start by searching for the library name appended by
242 // the system properties of the GLES userspace driver in both locations.
243 // i.e.:
244 // libGLES_${prop}.so, or:
245 // libEGL_${prop}.so, libGLESv1_CM_${prop}.so, libGLESv2_${prop}.so
246 char prop[PROPERTY_VALUE_MAX + 1];
247 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
248 if (property_get(key, prop, nullptr) <= 0) {
249 continue;
250 }
251 hnd = attempt_to_load_system_driver(cnx, prop);
252 if (hnd) {
253 break;
254 }
255 }
256 }
257
258 if (!hnd) {
259 // Can't find graphics driver by appending system properties, now search for the exact name
260 // without any suffix of the GLES userspace driver in both locations.
261 // i.e.:
262 // libGLES.so, or:
263 // libEGL.so, libGLESv1_CM.so, libGLESv2.so
264 hnd = attempt_to_load_system_driver(cnx, nullptr);
Mathias Agopiande586972009-05-28 17:39:03 -0700265 }
266
Yiwei Zhangd9861812019-02-13 11:51:55 -0800267 if (!hnd) {
268 android::GraphicsEnv::getInstance().setDriverLoaded(android::GraphicsEnv::Api::API_GL,
269 false, systemTime() - openTime);
270 }
271
Mathias Agopian99381422013-04-23 20:52:29 +0200272 LOG_ALWAYS_FATAL_IF(!hnd, "couldn't find an OpenGL ES implementation");
Jesse Hallc07b5202013-07-04 12:08:16 -0700273
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700274 cnx->libEgl = load_wrapper(EGL_WRAPPER_DIR "/libEGL.so");
275 cnx->libGles2 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv2.so");
276 cnx->libGles1 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv1_CM.so");
277
Yiwei Zhangd9861812019-02-13 11:51:55 -0800278 if (!cnx->libEgl || !cnx->libGles2 || !cnx->libGles1) {
279 android::GraphicsEnv::getInstance().setDriverLoaded(android::GraphicsEnv::Api::API_GL,
280 false, systemTime() - openTime);
281 }
282
Michael Chockc0ec5e22014-01-27 08:14:33 -0800283 LOG_ALWAYS_FATAL_IF(!cnx->libEgl,
284 "couldn't load system EGL wrapper libraries");
285
Jesse Hallc07b5202013-07-04 12:08:16 -0700286 LOG_ALWAYS_FATAL_IF(!cnx->libGles2 || !cnx->libGles1,
287 "couldn't load system OpenGL ES wrapper libraries");
288
Yiwei Zhangd9861812019-02-13 11:51:55 -0800289 android::GraphicsEnv::getInstance().setDriverLoaded(android::GraphicsEnv::Api::API_GL, true,
290 systemTime() - openTime);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800291
Mathias Agopiande586972009-05-28 17:39:03 -0700292 return (void*)hnd;
293}
294
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600295void Loader::close(egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700296{
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600297 driver_t* hnd = (driver_t*) cnx->dso;
Mathias Agopiande586972009-05-28 17:39:03 -0700298 delete hnd;
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600299 cnx->dso = nullptr;
300
Tim Van Patten5f744f12018-12-12 11:46:21 -0700301 cnx->shouldUseAngle = false;
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600302 cnx->angleDecided = false;
303 cnx->useAngle = false;
304
305 if (cnx->vendorEGL) {
306 dlclose(cnx->vendorEGL);
307 cnx->vendorEGL = nullptr;
308 }
Mathias Agopiande586972009-05-28 17:39:03 -0700309}
310
Jesse Hall94cdba92013-07-11 09:40:35 -0700311void Loader::init_api(void* dso,
312 char const * const * api,
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700313 char const * const * ref_api,
Jesse Hall94cdba92013-07-11 09:40:35 -0700314 __eglMustCastToProperFunctionPointerType* curr,
315 getProcAddressType getProcAddress)
Mathias Agopiande586972009-05-28 17:39:03 -0700316{
Jesse Hall1508ae62017-01-19 17:43:26 -0800317 ATRACE_CALL();
318
Mathias Agopian7773c432012-02-13 20:06:08 -0800319 const ssize_t SIZE = 256;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700320 char scrap[SIZE];
Mathias Agopiande586972009-05-28 17:39:03 -0700321 while (*api) {
322 char const * name = *api;
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700323 if (ref_api) {
324 char const * ref_name = *ref_api;
325 if (std::strcmp(name, ref_name) != 0) {
326 *curr++ = nullptr;
327 ref_api++;
328 continue;
329 }
330 }
331
Jesse Hall94cdba92013-07-11 09:40:35 -0700332 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700333 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700334 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700335 // couldn't find the entry-point, use eglGetProcAddress()
336 f = getProcAddress(name);
337 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700338 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700339 // Try without the OES postfix
340 ssize_t index = ssize_t(strlen(name)) - 3;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700341 if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) {
Mathias Agopiande586972009-05-28 17:39:03 -0700342 strncpy(scrap, name, index);
343 scrap[index] = 0;
344 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000345 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700346 }
347 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700348 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700349 // Try with the OES postfix
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700350 ssize_t index = ssize_t(strlen(name)) - 3;
351 if (index>0 && strcmp(name+index, "OES")) {
352 snprintf(scrap, SIZE, "%sOES", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700353 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000354 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700355 }
356 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700357 if (f == nullptr) {
Steve Block9d453682011-12-20 16:23:08 +0000358 //ALOGD("%s", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700359 f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented;
Mathias Agopian48d438d2012-01-28 21:44:00 -0800360
361 /*
362 * GL_EXT_debug_label is special, we always report it as
363 * supported, it's handled by GLES_trace. If GLES_trace is not
364 * enabled, then these are no-ops.
365 */
366 if (!strcmp(name, "glInsertEventMarkerEXT")) {
367 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
368 } else if (!strcmp(name, "glPushGroupMarkerEXT")) {
369 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
370 } else if (!strcmp(name, "glPopGroupMarkerEXT")) {
371 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
372 }
Mathias Agopiande586972009-05-28 17:39:03 -0700373 }
374 *curr++ = f;
375 api++;
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700376 if (ref_api) ref_api++;
Mathias Agopiande586972009-05-28 17:39:03 -0700377 }
378}
379
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700380static void* load_emulation_driver(const char* kind) {
Peiyong Line83b8682019-04-18 17:23:02 -0700381 const int emulationStatus = checkGlesEmulationStatus();
382
383 // Invalid emulation status, abort.
384 if (emulationStatus < 0 || emulationStatus > 2) {
385 return nullptr;
386 }
387
388 std::string absolutePath;
389 switch (emulationStatus) {
390 case 0:
391#if defined(__LP64__)
392 absolutePath = "/vendor/lib64/egl/libGLES_android.so";
393#else
394 absolutePath = "/vendor/lib/egl/libGLES_android.so";
395#endif
396 break;
397 case 1:
398 // Use host-side OpenGL through the "emulation" library
399#if defined(__LP64__)
400 absolutePath = std::string("/vendor/lib64/egl/lib") + kind + "_emulation.so";
401#else
402 absolutePath = std::string("/vendor/lib/egl/lib") + kind + "_emulation.so";
403#endif
404 break;
405 case 2:
406 // Use guest side swiftshader library
407#if defined(__LP64__)
408 absolutePath = std::string("/vendor/lib64/egl/lib") + kind + "_swiftshader.so";
409#else
410 absolutePath = std::string("/vendor/lib/egl/lib") + kind + "_swiftshader.so";
411#endif
412 break;
413 default:
414 // Not in emulator, or use other guest-side implementation
415 break;
416 }
417 if (absolutePath.empty()) {
418 // this happens often, we don't want to log an error
419 return nullptr;
420 }
421 const char* const driver_absolute_path = absolutePath.c_str();
422
423 // Try to load drivers from the 'sphal' namespace, if it exist. Fall back to
424 // the original routine when the namespace does not exist.
425 // See /system/core/rootdir/etc/ld.config.txt for the configuration of the
426 // sphal namespace.
427 void* dso = do_android_load_sphal_library(driver_absolute_path,
428 RTLD_NOW | RTLD_LOCAL);
429 if (dso == nullptr) {
430 const char* err = dlerror();
431 ALOGE("load_driver(%s): %s", driver_absolute_path, err ? err : "unknown");
432 return nullptr;
433 }
434 ALOGD("loaded %s", driver_absolute_path);
435 return dso;
436}
437
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700438static void* load_system_driver(const char* kind, const char* suffix) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800439 ATRACE_CALL();
Mathias Agopian99381422013-04-23 20:52:29 +0200440 class MatchFile {
441 public:
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700442 static std::string find(const char* libraryName) {
Mathias Agopian99381422013-04-23 20:52:29 +0200443 const char* const searchPaths[] = {
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800444#if defined(__LP64__)
445 "/vendor/lib64/egl",
446 "/system/lib64/egl"
447#else
Mathias Agopian99381422013-04-23 20:52:29 +0200448 "/vendor/lib/egl",
449 "/system/lib/egl"
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800450#endif
Mathias Agopian99381422013-04-23 20:52:29 +0200451 };
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700452
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700453 for (auto dir : searchPaths) {
454 std::string absolutePath = dir + std::string("/") + libraryName + ".so";
Mathias Agopian65421432017-03-08 11:49:05 -0800455 if (!access(absolutePath.c_str(), R_OK)) {
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700456 return absolutePath;
Mathias Agopian99381422013-04-23 20:52:29 +0200457 }
Mathias Agopian99381422013-04-23 20:52:29 +0200458 }
459
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700460 // Driver not found. gah.
461 return std::string();
Mathias Agopian99381422013-04-23 20:52:29 +0200462 }
463 };
464
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700465 std::string libraryName = std::string("lib") + kind;
466 if (suffix) {
467 libraryName += std::string("_") + suffix;
468 }
469 std::string absolutePath = MatchFile::find(libraryName.c_str());
Mathias Agopian65421432017-03-08 11:49:05 -0800470 if (absolutePath.empty()) {
Mathias Agopian99381422013-04-23 20:52:29 +0200471 // this happens often, we don't want to log an error
Yi Kong48a6cd22018-07-18 10:07:09 -0700472 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700473 }
Mathias Agopian65421432017-03-08 11:49:05 -0800474 const char* const driver_absolute_path = absolutePath.c_str();
Mathias Agopiande586972009-05-28 17:39:03 -0700475
Jiyong Park5910dc72017-04-05 14:23:52 +0900476 // Try to load drivers from the 'sphal' namespace, if it exist. Fall back to
Justin Yunb7320302017-05-22 15:13:40 +0900477 // the original routine when the namespace does not exist.
Jiyong Park5910dc72017-04-05 14:23:52 +0900478 // See /system/core/rootdir/etc/ld.config.txt for the configuration of the
479 // sphal namespace.
Justin Yunb7320302017-05-22 15:13:40 +0900480 void* dso = do_android_load_sphal_library(driver_absolute_path,
481 RTLD_NOW | RTLD_LOCAL);
Yi Kong48a6cd22018-07-18 10:07:09 -0700482 if (dso == nullptr) {
Mathias Agopian8c173842009-09-20 16:01:02 -0700483 const char* err = dlerror();
Mathias Agopian65421432017-03-08 11:49:05 -0800484 ALOGE("load_driver(%s): %s", driver_absolute_path, err ? err : "unknown");
Yi Kong48a6cd22018-07-18 10:07:09 -0700485 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700486 }
487
Steve Block9d453682011-12-20 16:23:08 +0000488 ALOGD("loaded %s", driver_absolute_path);
Mathias Agopianbaca89c2009-08-20 19:09:34 -0700489
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800490 return dso;
491}
492
Cody Northrop1f00e172018-04-02 11:23:31 -0600493static void* load_angle_from_namespace(const char* kind, android_namespace_t* ns) {
494 const android_dlextinfo dlextinfo = {
495 .flags = ANDROID_DLEXT_USE_NAMESPACE,
496 .library_namespace = ns,
497 };
498
499 std::string name = std::string("lib") + kind + "_angle.so";
500
501 void* so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
502
503 if (so) {
504 ALOGD("dlopen_ext from APK (%s) success at %p", name.c_str(), so);
505 return so;
506 } else {
507 ALOGE("dlopen_ext(\"%s\") failed: %s", name.c_str(), dlerror());
508 }
509
510 return nullptr;
511}
512
Cody Northrop6d082ef2018-09-11 09:42:31 -0600513static void* load_angle(const char* kind, android_namespace_t* ns, egl_connection_t* cnx) {
Courtney Goeltzenleuchter985bc282018-11-02 14:24:55 -0600514 void* so = nullptr;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700515
516 if ((cnx->shouldUseAngle) || android::GraphicsEnv::getInstance().shouldUseAngle()) {
Ian Elliott34474472018-09-05 10:57:07 -0600517 so = load_angle_from_namespace(kind, ns);
Tim Van Patten5f744f12018-12-12 11:46:21 -0700518 cnx->shouldUseAngle = true;
519 } else {
520 cnx->shouldUseAngle = false;
Ian Elliott34474472018-09-05 10:57:07 -0600521 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600522
523 if (so) {
Tim Van Patten5f744f12018-12-12 11:46:21 -0700524 ALOGV("Loaded ANGLE %s library for '%s' (instead of native)", kind,
525 android::GraphicsEnv::getInstance().getAngleAppName().c_str());
Cody Northrop1f00e172018-04-02 11:23:31 -0600526 cnx->useAngle = true;
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600527
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600528 char prop[PROPERTY_VALUE_MAX];
Tim Van Patten5f744f12018-12-12 11:46:21 -0700529
530 property_get("debug.hwui.renderer", prop, "UNSET");
531 ALOGV("Skia's renderer set to %s", prop);
532
533 EGLint angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE;
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600534 property_get("debug.angle.backend", prop, "0");
535 switch (atoi(prop)) {
536 case 1:
537 ALOGV("%s: Requesting OpenGLES back-end", __FUNCTION__);
538 angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE;
539 break;
540 case 2:
541 ALOGV("%s: Requesting Vulkan back-end", __FUNCTION__);
542 angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE;
543 break;
544 default:
545 break;
546 }
547
548 cnx->angleBackend = angleBackendDefault;
549 if (!cnx->vendorEGL && (cnx->angleBackend == EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE)) {
550 // Find and load vendor libEGL for ANGLE's GL back-end to use.
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700551 char prop[PROPERTY_VALUE_MAX + 1];
552 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
553 if (property_get(key, prop, nullptr) <= 0) {
554 continue;
555 }
556 void* dso = load_system_driver("EGL", prop);
557 if (dso) {
558 cnx->vendorEGL = dso;
559 break;
560 }
561 }
562 if (!cnx->vendorEGL) {
563 cnx->vendorEGL = load_system_driver("EGL", nullptr);
564 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600565 }
Ian Elliottb058aff2018-08-09 12:00:55 -0600566 } else {
Tim Van Patten5f744f12018-12-12 11:46:21 -0700567 ALOGV("Loaded native %s library for '%s' (instead of ANGLE)", kind,
568 android::GraphicsEnv::getInstance().getAngleAppName().c_str());
569 cnx->useAngle = false;
Cody Northrop1f00e172018-04-02 11:23:31 -0600570 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700571 cnx->angleDecided = true;
Cody Northrop1f00e172018-04-02 11:23:31 -0600572
Tim Van Patten5f744f12018-12-12 11:46:21 -0700573 return so;
Cody Northrop1f00e172018-04-02 11:23:31 -0600574}
575
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800576static void* load_updated_driver(const char* kind, android_namespace_t* ns) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800577 ATRACE_CALL();
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800578 const android_dlextinfo dlextinfo = {
579 .flags = ANDROID_DLEXT_USE_NAMESPACE,
580 .library_namespace = ns,
581 };
582 void* so = nullptr;
583 char prop[PROPERTY_VALUE_MAX + 1];
584 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700585 if (property_get(key, prop, nullptr) <= 0) {
586 continue;
587 }
588 std::string name = std::string("lib") + kind + "_" + prop + ".so";
589 so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
590 if (so) {
591 return so;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800592 }
593 }
594 return nullptr;
595}
596
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700597Loader::driver_t* Loader::attempt_to_load_angle(egl_connection_t* cnx) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800598 ATRACE_CALL();
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600599 android_namespace_t* ns = android::GraphicsEnv::getInstance().getAngleNamespace();
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700600 if (!ns) {
601 return nullptr;
Cody Northrop6d082ef2018-09-11 09:42:31 -0600602 }
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700603
604 android::GraphicsEnv::getInstance().setDriverToLoad(android::GraphicsEnv::Driver::ANGLE);
605 driver_t* hnd = nullptr;
606
607 // ANGLE doesn't ship with GLES library, and thus we skip GLES driver.
608 void* dso = load_angle("EGL", ns, cnx);
609 if (dso) {
610 initialize_api(dso, cnx, EGL);
611 hnd = new driver_t(dso);
612
613 dso = load_angle("GLESv1_CM", ns, cnx);
614 initialize_api(dso, cnx, GLESv1_CM);
615 hnd->set(dso, GLESv1_CM);
616
617 dso = load_angle("GLESv2", ns, cnx);
618 initialize_api(dso, cnx, GLESv2);
619 hnd->set(dso, GLESv2);
620 }
621 return hnd;
622}
623
624Loader::driver_t* Loader::attempt_to_load_updated_driver(egl_connection_t* cnx) {
625 ATRACE_CALL();
Jiyong Parka243e5d2017-06-21 12:26:51 +0900626#ifndef __ANDROID_VNDK__
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700627 android_namespace_t* ns = android::GraphicsEnv::getInstance().getDriverNamespace();
628 if (!ns) {
629 return nullptr;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800630 }
631
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700632 android::GraphicsEnv::getInstance().setDriverToLoad(android::GraphicsEnv::Driver::GL_UPDATED);
633 driver_t* hnd = nullptr;
634 void* dso = load_updated_driver("GLES", ns);
Peiyong Line83b8682019-04-18 17:23:02 -0700635 if (dso) {
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700636 initialize_api(dso, cnx, EGL | GLESv1_CM | GLESv2);
637 hnd = new driver_t(dso);
638 return hnd;
Peiyong Line83b8682019-04-18 17:23:02 -0700639 }
640
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700641 dso = load_updated_driver("EGL", ns);
642 if (dso) {
643 initialize_api(dso, cnx, EGL);
644 hnd = new driver_t(dso);
645
646 dso = load_updated_driver("GLESv1_CM", ns);
647 initialize_api(dso, cnx, GLESv1_CM);
648 hnd->set(dso, GLESv1_CM);
649
650 dso = load_updated_driver("GLESv2", ns);
651 initialize_api(dso, cnx, GLESv2);
652 hnd->set(dso, GLESv2);
653 }
654 return hnd;
655#else
Peiyong Line83b8682019-04-18 17:23:02 -0700656 return nullptr;
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700657#endif
658}
659
660Loader::driver_t* Loader::attempt_to_load_emulation_driver(egl_connection_t* cnx) {
661 ATRACE_CALL();
662 android::GraphicsEnv::getInstance().setDriverToLoad(android::GraphicsEnv::Driver::GL);
663 driver_t* hnd = nullptr;
664 void* dso = load_emulation_driver("GLES");
665 if (dso) {
666 initialize_api(dso, cnx, EGL | GLESv1_CM | GLESv2);
667 hnd = new driver_t(dso);
668 return hnd;
669 }
670 dso = load_emulation_driver("EGL");
671 if (dso) {
672 initialize_api(dso, cnx, EGL);
673 hnd = new driver_t(dso);
674
675 dso = load_emulation_driver("GLESv1_CM");
676 initialize_api(dso, cnx, GLESv1_CM);
677 hnd->set(dso, GLESv1_CM);
678
679 dso = load_emulation_driver("GLESv2");
680 initialize_api(dso, cnx, GLESv2);
681 hnd->set(dso, GLESv2);
682 }
683 return hnd;
684}
685
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700686Loader::driver_t* Loader::attempt_to_load_system_driver(egl_connection_t* cnx, const char* suffix) {
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700687 ATRACE_CALL();
688 android::GraphicsEnv::getInstance().setDriverToLoad(android::GraphicsEnv::Driver::GL);
689 driver_t* hnd = nullptr;
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700690 void* dso = load_system_driver("GLES", suffix);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700691 if (dso) {
692 initialize_api(dso, cnx, EGL | GLESv1_CM | GLESv2);
693 hnd = new driver_t(dso);
694 return hnd;
695 }
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700696 dso = load_system_driver("EGL", suffix);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700697 if (dso) {
698 initialize_api(dso, cnx, EGL);
699 hnd = new driver_t(dso);
700
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700701 dso = load_system_driver("GLESv1_CM", suffix);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700702 initialize_api(dso, cnx, GLESv1_CM);
703 hnd->set(dso, GLESv1_CM);
704
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700705 dso = load_system_driver("GLESv2", suffix);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700706 initialize_api(dso, cnx, GLESv2);
707 hnd->set(dso, GLESv2);
708 }
709 return hnd;
Peiyong Line83b8682019-04-18 17:23:02 -0700710}
711
712void Loader::initialize_api(void* dso, egl_connection_t* cnx, uint32_t mask) {
Mathias Agopiande586972009-05-28 17:39:03 -0700713 if (mask & EGL) {
714 getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress");
715
Jesse Hall94cdba92013-07-11 09:40:35 -0700716 ALOGE_IF(!getProcAddress,
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800717 "can't find eglGetProcAddress() in EGL driver library");
Mathias Agopiande586972009-05-28 17:39:03 -0700718
Mathias Agopian618fa102009-10-14 02:06:37 -0700719 egl_t* egl = &cnx->egl;
Mathias Agopiande586972009-05-28 17:39:03 -0700720 __eglMustCastToProperFunctionPointerType* curr =
721 (__eglMustCastToProperFunctionPointerType*)egl;
722 char const * const * api = egl_names;
723 while (*api) {
724 char const * name = *api;
Jesse Hall94cdba92013-07-11 09:40:35 -0700725 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700726 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700727 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700728 // couldn't find the entry-point, use eglGetProcAddress()
729 f = getProcAddress(name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700730 if (f == nullptr) {
731 f = (__eglMustCastToProperFunctionPointerType)nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700732 }
733 }
734 *curr++ = f;
735 api++;
736 }
737 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700738
Mathias Agopiande586972009-05-28 17:39:03 -0700739 if (mask & GLESv1_CM) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700740 init_api(dso, gl_names_1, gl_names,
Mathias Agopian618fa102009-10-14 02:06:37 -0700741 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800742 &cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl,
Mathias Agopian618fa102009-10-14 02:06:37 -0700743 getProcAddress);
Mathias Agopiande586972009-05-28 17:39:03 -0700744 }
745
746 if (mask & GLESv2) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700747 init_api(dso, gl_names, nullptr,
Mathias Agopian618fa102009-10-14 02:06:37 -0700748 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800749 &cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl,
Mathias Agopiande586972009-05-28 17:39:03 -0700750 getProcAddress);
751 }
Mathias Agopiande586972009-05-28 17:39:03 -0700752}
753
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700754} // namespace android