blob: d5c46c6683e4b7f7cd1f30ff83d41de71b2096a6 [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
Jesse Hall1508ae62017-01-19 17:43:26 -080072static void* do_dlopen(const char* path, int mode) {
73 ATRACE_CALL();
74 return dlopen(path, mode);
75}
76
Jiyong Park5910dc72017-04-05 14:23:52 +090077static void* do_android_dlopen_ext(const char* path, int mode, const android_dlextinfo* info) {
78 ATRACE_CALL();
79 return android_dlopen_ext(path, mode, info);
80}
81
Justin Yunb7320302017-05-22 15:13:40 +090082static void* do_android_load_sphal_library(const char* path, int mode) {
83 ATRACE_CALL();
84 return android_load_sphal_library(path, mode);
85}
86
Jesse Hall94cdba92013-07-11 09:40:35 -070087Loader::driver_t::driver_t(void* gles)
Mathias Agopiande586972009-05-28 17:39:03 -070088{
89 dso[0] = gles;
90 for (size_t i=1 ; i<NELEM(dso) ; i++)
Yi Kong48a6cd22018-07-18 10:07:09 -070091 dso[i] = nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -070092}
93
Jesse Hall94cdba92013-07-11 09:40:35 -070094Loader::driver_t::~driver_t()
Mathias Agopiande586972009-05-28 17:39:03 -070095{
96 for (size_t i=0 ; i<NELEM(dso) ; i++) {
97 if (dso[i]) {
98 dlclose(dso[i]);
Yi Kong48a6cd22018-07-18 10:07:09 -070099 dso[i] = nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700100 }
101 }
102}
103
Mathias Agopian65421432017-03-08 11:49:05 -0800104int Loader::driver_t::set(void* hnd, int32_t api)
Mathias Agopiande586972009-05-28 17:39:03 -0700105{
106 switch (api) {
107 case EGL:
108 dso[0] = hnd;
109 break;
110 case GLESv1_CM:
111 dso[1] = hnd;
112 break;
113 case GLESv2:
114 dso[2] = hnd;
115 break;
116 default:
Mathias Agopian65421432017-03-08 11:49:05 -0800117 return -EOVERFLOW;
Mathias Agopiande586972009-05-28 17:39:03 -0700118 }
Mathias Agopian65421432017-03-08 11:49:05 -0800119 return 0;
Mathias Agopiande586972009-05-28 17:39:03 -0700120}
121
Mathias Agopiande586972009-05-28 17:39:03 -0700122Loader::Loader()
Yi Kong48a6cd22018-07-18 10:07:09 -0700123 : getProcAddress(nullptr)
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800124{
Mathias Agopiande586972009-05-28 17:39:03 -0700125}
126
Mathias Agopian99381422013-04-23 20:52:29 +0200127Loader::~Loader() {
Mathias Agopiande586972009-05-28 17:39:03 -0700128}
129
Jesse Hallc07b5202013-07-04 12:08:16 -0700130static void* load_wrapper(const char* path) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800131 void* so = do_dlopen(path, RTLD_NOW | RTLD_LOCAL);
Jesse Hallc07b5202013-07-04 12:08:16 -0700132 ALOGE_IF(!so, "dlopen(\"%s\") failed: %s", path, dlerror());
133 return so;
134}
135
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700136#ifndef EGL_WRAPPER_DIR
137#if defined(__LP64__)
138#define EGL_WRAPPER_DIR "/system/lib64"
139#else
140#define EGL_WRAPPER_DIR "/system/lib"
141#endif
142#endif
143
bohu69e5b1a2016-02-19 17:15:20 -0800144static void setEmulatorGlesValue(void) {
145 char prop[PROPERTY_VALUE_MAX];
146 property_get("ro.kernel.qemu", prop, "0");
147 if (atoi(prop) != 1) return;
148
149 property_get("ro.kernel.qemu.gles",prop,"0");
150 if (atoi(prop) == 1) {
151 ALOGD("Emulator has host GPU support, qemu.gles is set to 1.");
152 property_set("qemu.gles", "1");
153 return;
154 }
155
156 // for now, checking the following
157 // directory is good enough for emulator system images
158 const char* vendor_lib_path =
159#if defined(__LP64__)
160 "/vendor/lib64/egl";
161#else
162 "/vendor/lib/egl";
163#endif
164
165 const bool has_vendor_lib = (access(vendor_lib_path, R_OK) == 0);
166 if (has_vendor_lib) {
167 ALOGD("Emulator has vendor provided software renderer, qemu.gles is set to 2.");
168 property_set("qemu.gles", "2");
169 } else {
170 ALOGD("Emulator without GPU support detected. "
171 "Fallback to legacy software renderer, qemu.gles is set to 0.");
172 property_set("qemu.gles", "0");
173 }
174}
175
Peiyong Lin0d10b252019-04-30 18:03:04 -0700176static const char* DRIVER_SUFFIX_PROPERTY = "ro.hardware.egl";
177
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700178static const char* HAL_SUBNAME_KEY_PROPERTIES[2] = {
Peiyong Lin0d10b252019-04-30 18:03:04 -0700179 DRIVER_SUFFIX_PROPERTY,
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700180 "ro.board.platform",
181};
182
Mathias Agopianada798b2012-02-13 17:09:30 -0800183void* Loader::open(egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700184{
Jesse Hall1508ae62017-01-19 17:43:26 -0800185 ATRACE_CALL();
Yiwei Zhangd9861812019-02-13 11:51:55 -0800186 const nsecs_t openTime = systemTime();
Jesse Hall1508ae62017-01-19 17:43:26 -0800187
bohu69e5b1a2016-02-19 17:15:20 -0800188 setEmulatorGlesValue();
189
Tim Van Patten5f744f12018-12-12 11:46:21 -0700190 // Check if we should use ANGLE early, so loading each driver doesn't require repeated queries.
191 if (android::GraphicsEnv::getInstance().shouldUseAngle()) {
192 cnx->shouldUseAngle = true;
193 } else {
194 cnx->shouldUseAngle = false;
195 }
196
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700197 // Firstly, try to load ANGLE driver.
198 driver_t* hnd = attempt_to_load_angle(cnx);
199 if (!hnd) {
200 // Secondly, try to load from driver apk.
201 hnd = attempt_to_load_updated_driver(cnx);
202 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700203
204 bool failToLoadFromDriverSuffixProperty = false;
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700205 if (!hnd) {
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700206 // Finally, try to load system driver, start by searching for the library name appended by
207 // the system properties of the GLES userspace driver in both locations.
208 // i.e.:
209 // libGLES_${prop}.so, or:
210 // libEGL_${prop}.so, libGLESv1_CM_${prop}.so, libGLESv2_${prop}.so
211 char prop[PROPERTY_VALUE_MAX + 1];
212 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
213 if (property_get(key, prop, nullptr) <= 0) {
214 continue;
215 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700216 hnd = attempt_to_load_system_driver(cnx, prop, true);
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700217 if (hnd) {
218 break;
Peiyong Lin0d10b252019-04-30 18:03:04 -0700219 } else if (strcmp(key, DRIVER_SUFFIX_PROPERTY) == 0) {
220 failToLoadFromDriverSuffixProperty = true;
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700221 }
222 }
223 }
224
225 if (!hnd) {
226 // Can't find graphics driver by appending system properties, now search for the exact name
227 // without any suffix of the GLES userspace driver in both locations.
228 // i.e.:
229 // libGLES.so, or:
230 // libEGL.so, libGLESv1_CM.so, libGLESv2.so
Peiyong Lin0d10b252019-04-30 18:03:04 -0700231 hnd = attempt_to_load_system_driver(cnx, nullptr, true);
232 }
233
234 if (!hnd && !failToLoadFromDriverSuffixProperty) {
235 hnd = attempt_to_load_system_driver(cnx, nullptr, false);
Mathias Agopiande586972009-05-28 17:39:03 -0700236 }
237
Yiwei Zhangd9861812019-02-13 11:51:55 -0800238 if (!hnd) {
239 android::GraphicsEnv::getInstance().setDriverLoaded(android::GraphicsEnv::Api::API_GL,
240 false, systemTime() - openTime);
241 }
242
Peiyong Lin3575b9f2019-04-25 14:26:49 -0700243 LOG_ALWAYS_FATAL_IF(!hnd,
244 "couldn't find an OpenGL ES implementation, make sure you set %s or %s",
245 HAL_SUBNAME_KEY_PROPERTIES[0], HAL_SUBNAME_KEY_PROPERTIES[1]);
Jesse Hallc07b5202013-07-04 12:08:16 -0700246
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700247 cnx->libEgl = load_wrapper(EGL_WRAPPER_DIR "/libEGL.so");
248 cnx->libGles2 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv2.so");
249 cnx->libGles1 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv1_CM.so");
250
Yiwei Zhangd9861812019-02-13 11:51:55 -0800251 if (!cnx->libEgl || !cnx->libGles2 || !cnx->libGles1) {
252 android::GraphicsEnv::getInstance().setDriverLoaded(android::GraphicsEnv::Api::API_GL,
253 false, systemTime() - openTime);
254 }
255
Michael Chockc0ec5e22014-01-27 08:14:33 -0800256 LOG_ALWAYS_FATAL_IF(!cnx->libEgl,
257 "couldn't load system EGL wrapper libraries");
258
Jesse Hallc07b5202013-07-04 12:08:16 -0700259 LOG_ALWAYS_FATAL_IF(!cnx->libGles2 || !cnx->libGles1,
260 "couldn't load system OpenGL ES wrapper libraries");
261
Yiwei Zhangd9861812019-02-13 11:51:55 -0800262 android::GraphicsEnv::getInstance().setDriverLoaded(android::GraphicsEnv::Api::API_GL, true,
263 systemTime() - openTime);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800264
Mathias Agopiande586972009-05-28 17:39:03 -0700265 return (void*)hnd;
266}
267
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600268void Loader::close(egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700269{
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600270 driver_t* hnd = (driver_t*) cnx->dso;
Mathias Agopiande586972009-05-28 17:39:03 -0700271 delete hnd;
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600272 cnx->dso = nullptr;
273
Tim Van Patten5f744f12018-12-12 11:46:21 -0700274 cnx->shouldUseAngle = false;
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600275 cnx->angleDecided = false;
276 cnx->useAngle = false;
277
278 if (cnx->vendorEGL) {
279 dlclose(cnx->vendorEGL);
280 cnx->vendorEGL = nullptr;
281 }
Mathias Agopiande586972009-05-28 17:39:03 -0700282}
283
Jesse Hall94cdba92013-07-11 09:40:35 -0700284void Loader::init_api(void* dso,
285 char const * const * api,
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700286 char const * const * ref_api,
Jesse Hall94cdba92013-07-11 09:40:35 -0700287 __eglMustCastToProperFunctionPointerType* curr,
288 getProcAddressType getProcAddress)
Mathias Agopiande586972009-05-28 17:39:03 -0700289{
Jesse Hall1508ae62017-01-19 17:43:26 -0800290 ATRACE_CALL();
291
Mathias Agopian7773c432012-02-13 20:06:08 -0800292 const ssize_t SIZE = 256;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700293 char scrap[SIZE];
Mathias Agopiande586972009-05-28 17:39:03 -0700294 while (*api) {
295 char const * name = *api;
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700296 if (ref_api) {
297 char const * ref_name = *ref_api;
298 if (std::strcmp(name, ref_name) != 0) {
299 *curr++ = nullptr;
300 ref_api++;
301 continue;
302 }
303 }
304
Jesse Hall94cdba92013-07-11 09:40:35 -0700305 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700306 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700307 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700308 // couldn't find the entry-point, use eglGetProcAddress()
309 f = getProcAddress(name);
310 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700311 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700312 // Try without the OES postfix
313 ssize_t index = ssize_t(strlen(name)) - 3;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700314 if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) {
Mathias Agopiande586972009-05-28 17:39:03 -0700315 strncpy(scrap, name, index);
316 scrap[index] = 0;
317 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000318 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700319 }
320 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700321 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700322 // Try with the OES postfix
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700323 ssize_t index = ssize_t(strlen(name)) - 3;
324 if (index>0 && strcmp(name+index, "OES")) {
325 snprintf(scrap, SIZE, "%sOES", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700326 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000327 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700328 }
329 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700330 if (f == nullptr) {
Steve Block9d453682011-12-20 16:23:08 +0000331 //ALOGD("%s", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700332 f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented;
Mathias Agopian48d438d2012-01-28 21:44:00 -0800333
334 /*
335 * GL_EXT_debug_label is special, we always report it as
336 * supported, it's handled by GLES_trace. If GLES_trace is not
337 * enabled, then these are no-ops.
338 */
339 if (!strcmp(name, "glInsertEventMarkerEXT")) {
340 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
341 } else if (!strcmp(name, "glPushGroupMarkerEXT")) {
342 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
343 } else if (!strcmp(name, "glPopGroupMarkerEXT")) {
344 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
345 }
Mathias Agopiande586972009-05-28 17:39:03 -0700346 }
347 *curr++ = f;
348 api++;
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700349 if (ref_api) ref_api++;
Mathias Agopiande586972009-05-28 17:39:03 -0700350 }
351}
352
Peiyong Lin0d10b252019-04-30 18:03:04 -0700353static void* load_system_driver(const char* kind, const char* suffix, const bool exact) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800354 ATRACE_CALL();
Mathias Agopian99381422013-04-23 20:52:29 +0200355 class MatchFile {
356 public:
Peiyong Lin0d10b252019-04-30 18:03:04 -0700357 static std::string find(const char* libraryName, const bool exact) {
Mathias Agopian99381422013-04-23 20:52:29 +0200358 const char* const searchPaths[] = {
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800359#if defined(__LP64__)
360 "/vendor/lib64/egl",
361 "/system/lib64/egl"
362#else
Mathias Agopian99381422013-04-23 20:52:29 +0200363 "/vendor/lib/egl",
364 "/system/lib/egl"
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800365#endif
Mathias Agopian99381422013-04-23 20:52:29 +0200366 };
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700367
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700368 for (auto dir : searchPaths) {
Peiyong Lin0d10b252019-04-30 18:03:04 -0700369 std::string absolutePath;
370 if (find(absolutePath, libraryName, dir, exact)) {
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700371 return absolutePath;
Mathias Agopian99381422013-04-23 20:52:29 +0200372 }
Mathias Agopian99381422013-04-23 20:52:29 +0200373 }
374
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700375 // Driver not found. gah.
376 return std::string();
Mathias Agopian99381422013-04-23 20:52:29 +0200377 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700378 private:
379 static bool find(std::string& result,
380 const std::string& pattern, const char* const search, bool exact) {
381 if (exact) {
382 std::string absolutePath = std::string(search) + "/" + pattern + ".so";
383 if (!access(absolutePath.c_str(), R_OK)) {
384 result = absolutePath;
385 return true;
386 }
387 return false;
388 }
389
390 DIR* d = opendir(search);
391 if (d != nullptr) {
392 struct dirent* e;
393 while ((e = readdir(d)) != nullptr) {
394 if (e->d_type == DT_DIR) {
395 continue;
396 }
397 if (!strcmp(e->d_name, "libGLES_android.so")) {
398 // always skip the software renderer
399 continue;
400 }
401 if (strstr(e->d_name, pattern.c_str()) == e->d_name) {
402 if (!strcmp(e->d_name + strlen(e->d_name) - 3, ".so")) {
403 result = std::string(search) + "/" + e->d_name;
404 closedir(d);
405 return true;
406 }
407 }
408 }
409 closedir(d);
410 }
411 return false;
412 }
Mathias Agopian99381422013-04-23 20:52:29 +0200413 };
414
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700415 std::string libraryName = std::string("lib") + kind;
416 if (suffix) {
417 libraryName += std::string("_") + suffix;
Peiyong Lin0d10b252019-04-30 18:03:04 -0700418 } else if (!exact) {
419 // Deprecated: we look for files that match
420 // libGLES_*.so, or:
421 // libEGL_*.so, libGLESv1_CM_*.so, libGLESv2_*.so
422 libraryName += std::string("_");
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700423 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700424 std::string absolutePath = MatchFile::find(libraryName.c_str(), exact);
Mathias Agopian65421432017-03-08 11:49:05 -0800425 if (absolutePath.empty()) {
Mathias Agopian99381422013-04-23 20:52:29 +0200426 // this happens often, we don't want to log an error
Yi Kong48a6cd22018-07-18 10:07:09 -0700427 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700428 }
Mathias Agopian65421432017-03-08 11:49:05 -0800429 const char* const driver_absolute_path = absolutePath.c_str();
Mathias Agopiande586972009-05-28 17:39:03 -0700430
Jiyong Park5910dc72017-04-05 14:23:52 +0900431 // Try to load drivers from the 'sphal' namespace, if it exist. Fall back to
Justin Yunb7320302017-05-22 15:13:40 +0900432 // the original routine when the namespace does not exist.
Jiyong Park5910dc72017-04-05 14:23:52 +0900433 // See /system/core/rootdir/etc/ld.config.txt for the configuration of the
434 // sphal namespace.
Justin Yunb7320302017-05-22 15:13:40 +0900435 void* dso = do_android_load_sphal_library(driver_absolute_path,
436 RTLD_NOW | RTLD_LOCAL);
Yi Kong48a6cd22018-07-18 10:07:09 -0700437 if (dso == nullptr) {
Mathias Agopian8c173842009-09-20 16:01:02 -0700438 const char* err = dlerror();
Mathias Agopian65421432017-03-08 11:49:05 -0800439 ALOGE("load_driver(%s): %s", driver_absolute_path, err ? err : "unknown");
Yi Kong48a6cd22018-07-18 10:07:09 -0700440 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700441 }
442
Steve Block9d453682011-12-20 16:23:08 +0000443 ALOGD("loaded %s", driver_absolute_path);
Mathias Agopianbaca89c2009-08-20 19:09:34 -0700444
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800445 return dso;
446}
447
Cody Northrop1f00e172018-04-02 11:23:31 -0600448static void* load_angle_from_namespace(const char* kind, android_namespace_t* ns) {
449 const android_dlextinfo dlextinfo = {
450 .flags = ANDROID_DLEXT_USE_NAMESPACE,
451 .library_namespace = ns,
452 };
453
454 std::string name = std::string("lib") + kind + "_angle.so";
455
456 void* so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
457
458 if (so) {
459 ALOGD("dlopen_ext from APK (%s) success at %p", name.c_str(), so);
460 return so;
461 } else {
462 ALOGE("dlopen_ext(\"%s\") failed: %s", name.c_str(), dlerror());
463 }
464
465 return nullptr;
466}
467
Cody Northrop6d082ef2018-09-11 09:42:31 -0600468static void* load_angle(const char* kind, android_namespace_t* ns, egl_connection_t* cnx) {
Courtney Goeltzenleuchter985bc282018-11-02 14:24:55 -0600469 void* so = nullptr;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700470
471 if ((cnx->shouldUseAngle) || android::GraphicsEnv::getInstance().shouldUseAngle()) {
Ian Elliott34474472018-09-05 10:57:07 -0600472 so = load_angle_from_namespace(kind, ns);
Tim Van Patten5f744f12018-12-12 11:46:21 -0700473 cnx->shouldUseAngle = true;
474 } else {
475 cnx->shouldUseAngle = false;
Ian Elliott34474472018-09-05 10:57:07 -0600476 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600477
478 if (so) {
Tim Van Patten5f744f12018-12-12 11:46:21 -0700479 ALOGV("Loaded ANGLE %s library for '%s' (instead of native)", kind,
480 android::GraphicsEnv::getInstance().getAngleAppName().c_str());
Cody Northrop1f00e172018-04-02 11:23:31 -0600481 cnx->useAngle = true;
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600482
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600483 char prop[PROPERTY_VALUE_MAX];
Tim Van Patten5f744f12018-12-12 11:46:21 -0700484
485 property_get("debug.hwui.renderer", prop, "UNSET");
486 ALOGV("Skia's renderer set to %s", prop);
487
488 EGLint angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE;
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600489 property_get("debug.angle.backend", prop, "0");
490 switch (atoi(prop)) {
491 case 1:
492 ALOGV("%s: Requesting OpenGLES back-end", __FUNCTION__);
493 angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE;
494 break;
495 case 2:
496 ALOGV("%s: Requesting Vulkan back-end", __FUNCTION__);
497 angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE;
498 break;
499 default:
500 break;
501 }
502
503 cnx->angleBackend = angleBackendDefault;
504 if (!cnx->vendorEGL && (cnx->angleBackend == EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE)) {
505 // Find and load vendor libEGL for ANGLE's GL back-end to use.
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700506 char prop[PROPERTY_VALUE_MAX + 1];
507 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
508 if (property_get(key, prop, nullptr) <= 0) {
509 continue;
510 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700511 void* dso = load_system_driver("EGL", prop, true);
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700512 if (dso) {
513 cnx->vendorEGL = dso;
514 break;
515 }
516 }
517 if (!cnx->vendorEGL) {
Peiyong Lin0d10b252019-04-30 18:03:04 -0700518 cnx->vendorEGL = load_system_driver("EGL", nullptr, true);
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700519 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600520 }
Ian Elliottb058aff2018-08-09 12:00:55 -0600521 } else {
Tim Van Patten5f744f12018-12-12 11:46:21 -0700522 ALOGV("Loaded native %s library for '%s' (instead of ANGLE)", kind,
523 android::GraphicsEnv::getInstance().getAngleAppName().c_str());
524 cnx->useAngle = false;
Cody Northrop1f00e172018-04-02 11:23:31 -0600525 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700526 cnx->angleDecided = true;
Cody Northrop1f00e172018-04-02 11:23:31 -0600527
Tim Van Patten5f744f12018-12-12 11:46:21 -0700528 return so;
Cody Northrop1f00e172018-04-02 11:23:31 -0600529}
530
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800531static void* load_updated_driver(const char* kind, android_namespace_t* ns) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800532 ATRACE_CALL();
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800533 const android_dlextinfo dlextinfo = {
534 .flags = ANDROID_DLEXT_USE_NAMESPACE,
535 .library_namespace = ns,
536 };
537 void* so = nullptr;
538 char prop[PROPERTY_VALUE_MAX + 1];
539 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700540 if (property_get(key, prop, nullptr) <= 0) {
541 continue;
542 }
543 std::string name = std::string("lib") + kind + "_" + prop + ".so";
544 so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
545 if (so) {
546 return so;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800547 }
548 }
549 return nullptr;
550}
551
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700552Loader::driver_t* Loader::attempt_to_load_angle(egl_connection_t* cnx) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800553 ATRACE_CALL();
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600554 android_namespace_t* ns = android::GraphicsEnv::getInstance().getAngleNamespace();
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700555 if (!ns) {
556 return nullptr;
Cody Northrop6d082ef2018-09-11 09:42:31 -0600557 }
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700558
559 android::GraphicsEnv::getInstance().setDriverToLoad(android::GraphicsEnv::Driver::ANGLE);
560 driver_t* hnd = nullptr;
561
562 // ANGLE doesn't ship with GLES library, and thus we skip GLES driver.
563 void* dso = load_angle("EGL", ns, cnx);
564 if (dso) {
565 initialize_api(dso, cnx, EGL);
566 hnd = new driver_t(dso);
567
568 dso = load_angle("GLESv1_CM", ns, cnx);
569 initialize_api(dso, cnx, GLESv1_CM);
570 hnd->set(dso, GLESv1_CM);
571
572 dso = load_angle("GLESv2", ns, cnx);
573 initialize_api(dso, cnx, GLESv2);
574 hnd->set(dso, GLESv2);
575 }
576 return hnd;
577}
578
579Loader::driver_t* Loader::attempt_to_load_updated_driver(egl_connection_t* cnx) {
580 ATRACE_CALL();
Jiyong Parka243e5d2017-06-21 12:26:51 +0900581#ifndef __ANDROID_VNDK__
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700582 android_namespace_t* ns = android::GraphicsEnv::getInstance().getDriverNamespace();
583 if (!ns) {
584 return nullptr;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800585 }
586
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700587 android::GraphicsEnv::getInstance().setDriverToLoad(android::GraphicsEnv::Driver::GL_UPDATED);
588 driver_t* hnd = nullptr;
589 void* dso = load_updated_driver("GLES", ns);
Peiyong Line83b8682019-04-18 17:23:02 -0700590 if (dso) {
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700591 initialize_api(dso, cnx, EGL | GLESv1_CM | GLESv2);
592 hnd = new driver_t(dso);
593 return hnd;
Peiyong Line83b8682019-04-18 17:23:02 -0700594 }
595
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700596 dso = load_updated_driver("EGL", ns);
597 if (dso) {
598 initialize_api(dso, cnx, EGL);
599 hnd = new driver_t(dso);
600
601 dso = load_updated_driver("GLESv1_CM", ns);
602 initialize_api(dso, cnx, GLESv1_CM);
603 hnd->set(dso, GLESv1_CM);
604
605 dso = load_updated_driver("GLESv2", ns);
606 initialize_api(dso, cnx, GLESv2);
607 hnd->set(dso, GLESv2);
608 }
609 return hnd;
610#else
Peiyong Line83b8682019-04-18 17:23:02 -0700611 return nullptr;
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700612#endif
613}
614
Peiyong Lin0d10b252019-04-30 18:03:04 -0700615Loader::driver_t* Loader::attempt_to_load_system_driver(egl_connection_t* cnx, const char* suffix,
616 const bool exact) {
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700617 ATRACE_CALL();
618 android::GraphicsEnv::getInstance().setDriverToLoad(android::GraphicsEnv::Driver::GL);
619 driver_t* hnd = nullptr;
Peiyong Lin0d10b252019-04-30 18:03:04 -0700620 void* dso = load_system_driver("GLES", suffix, exact);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700621 if (dso) {
622 initialize_api(dso, cnx, EGL | GLESv1_CM | GLESv2);
623 hnd = new driver_t(dso);
624 return hnd;
625 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700626 dso = load_system_driver("EGL", suffix, exact);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700627 if (dso) {
628 initialize_api(dso, cnx, EGL);
629 hnd = new driver_t(dso);
630
Peiyong Lin0d10b252019-04-30 18:03:04 -0700631 dso = load_system_driver("GLESv1_CM", suffix, exact);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700632 initialize_api(dso, cnx, GLESv1_CM);
633 hnd->set(dso, GLESv1_CM);
634
Peiyong Lin0d10b252019-04-30 18:03:04 -0700635 dso = load_system_driver("GLESv2", suffix, exact);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700636 initialize_api(dso, cnx, GLESv2);
637 hnd->set(dso, GLESv2);
638 }
639 return hnd;
Peiyong Line83b8682019-04-18 17:23:02 -0700640}
641
642void Loader::initialize_api(void* dso, egl_connection_t* cnx, uint32_t mask) {
Mathias Agopiande586972009-05-28 17:39:03 -0700643 if (mask & EGL) {
644 getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress");
645
Jesse Hall94cdba92013-07-11 09:40:35 -0700646 ALOGE_IF(!getProcAddress,
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800647 "can't find eglGetProcAddress() in EGL driver library");
Mathias Agopiande586972009-05-28 17:39:03 -0700648
Mathias Agopian618fa102009-10-14 02:06:37 -0700649 egl_t* egl = &cnx->egl;
Mathias Agopiande586972009-05-28 17:39:03 -0700650 __eglMustCastToProperFunctionPointerType* curr =
651 (__eglMustCastToProperFunctionPointerType*)egl;
652 char const * const * api = egl_names;
653 while (*api) {
654 char const * name = *api;
Jesse Hall94cdba92013-07-11 09:40:35 -0700655 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700656 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700657 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700658 // couldn't find the entry-point, use eglGetProcAddress()
659 f = getProcAddress(name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700660 if (f == nullptr) {
661 f = (__eglMustCastToProperFunctionPointerType)nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700662 }
663 }
664 *curr++ = f;
665 api++;
666 }
667 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700668
Mathias Agopiande586972009-05-28 17:39:03 -0700669 if (mask & GLESv1_CM) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700670 init_api(dso, gl_names_1, gl_names,
Mathias Agopian618fa102009-10-14 02:06:37 -0700671 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800672 &cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl,
Mathias Agopian618fa102009-10-14 02:06:37 -0700673 getProcAddress);
Mathias Agopiande586972009-05-28 17:39:03 -0700674 }
675
676 if (mask & GLESv2) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700677 init_api(dso, gl_names, nullptr,
Mathias Agopian618fa102009-10-14 02:06:37 -0700678 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800679 &cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl,
Mathias Agopiande586972009-05-28 17:39:03 -0700680 getProcAddress);
681 }
Mathias Agopiande586972009-05-28 17:39:03 -0700682}
683
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700684} // namespace android