blob: 95c41bc1f2ff9f65bcafd7356f3088e2f3b4735a [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 Lin9f0c79d2019-04-22 10:14:57 -0700176static const char* HAL_SUBNAME_KEY_PROPERTIES[2] = {
177 "ro.hardware.egl",
178 "ro.board.platform",
179};
180
Mathias Agopianada798b2012-02-13 17:09:30 -0800181void* Loader::open(egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700182{
Jesse Hall1508ae62017-01-19 17:43:26 -0800183 ATRACE_CALL();
Yiwei Zhangd9861812019-02-13 11:51:55 -0800184 const nsecs_t openTime = systemTime();
Jesse Hall1508ae62017-01-19 17:43:26 -0800185
bohu69e5b1a2016-02-19 17:15:20 -0800186 setEmulatorGlesValue();
187
Tim Van Patten5f744f12018-12-12 11:46:21 -0700188 // Check if we should use ANGLE early, so loading each driver doesn't require repeated queries.
189 if (android::GraphicsEnv::getInstance().shouldUseAngle()) {
190 cnx->shouldUseAngle = true;
191 } else {
192 cnx->shouldUseAngle = false;
193 }
194
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700195 // Firstly, try to load ANGLE driver.
196 driver_t* hnd = attempt_to_load_angle(cnx);
197 if (!hnd) {
198 // Secondly, try to load from driver apk.
199 hnd = attempt_to_load_updated_driver(cnx);
200 }
201 if (!hnd) {
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700202 // Finally, try to load system driver, start by searching for the library name appended by
203 // the system properties of the GLES userspace driver in both locations.
204 // i.e.:
205 // libGLES_${prop}.so, or:
206 // libEGL_${prop}.so, libGLESv1_CM_${prop}.so, libGLESv2_${prop}.so
207 char prop[PROPERTY_VALUE_MAX + 1];
208 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
209 if (property_get(key, prop, nullptr) <= 0) {
210 continue;
211 }
212 hnd = attempt_to_load_system_driver(cnx, prop);
213 if (hnd) {
214 break;
215 }
216 }
217 }
218
219 if (!hnd) {
220 // Can't find graphics driver by appending system properties, now search for the exact name
221 // without any suffix of the GLES userspace driver in both locations.
222 // i.e.:
223 // libGLES.so, or:
224 // libEGL.so, libGLESv1_CM.so, libGLESv2.so
225 hnd = attempt_to_load_system_driver(cnx, nullptr);
Mathias Agopiande586972009-05-28 17:39:03 -0700226 }
227
Yiwei Zhangd9861812019-02-13 11:51:55 -0800228 if (!hnd) {
229 android::GraphicsEnv::getInstance().setDriverLoaded(android::GraphicsEnv::Api::API_GL,
230 false, systemTime() - openTime);
231 }
232
Peiyong Lin3575b9f2019-04-25 14:26:49 -0700233 LOG_ALWAYS_FATAL_IF(!hnd,
234 "couldn't find an OpenGL ES implementation, make sure you set %s or %s",
235 HAL_SUBNAME_KEY_PROPERTIES[0], HAL_SUBNAME_KEY_PROPERTIES[1]);
Jesse Hallc07b5202013-07-04 12:08:16 -0700236
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700237 cnx->libEgl = load_wrapper(EGL_WRAPPER_DIR "/libEGL.so");
238 cnx->libGles2 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv2.so");
239 cnx->libGles1 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv1_CM.so");
240
Yiwei Zhangd9861812019-02-13 11:51:55 -0800241 if (!cnx->libEgl || !cnx->libGles2 || !cnx->libGles1) {
242 android::GraphicsEnv::getInstance().setDriverLoaded(android::GraphicsEnv::Api::API_GL,
243 false, systemTime() - openTime);
244 }
245
Michael Chockc0ec5e22014-01-27 08:14:33 -0800246 LOG_ALWAYS_FATAL_IF(!cnx->libEgl,
247 "couldn't load system EGL wrapper libraries");
248
Jesse Hallc07b5202013-07-04 12:08:16 -0700249 LOG_ALWAYS_FATAL_IF(!cnx->libGles2 || !cnx->libGles1,
250 "couldn't load system OpenGL ES wrapper libraries");
251
Yiwei Zhangd9861812019-02-13 11:51:55 -0800252 android::GraphicsEnv::getInstance().setDriverLoaded(android::GraphicsEnv::Api::API_GL, true,
253 systemTime() - openTime);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800254
Mathias Agopiande586972009-05-28 17:39:03 -0700255 return (void*)hnd;
256}
257
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600258void Loader::close(egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700259{
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600260 driver_t* hnd = (driver_t*) cnx->dso;
Mathias Agopiande586972009-05-28 17:39:03 -0700261 delete hnd;
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600262 cnx->dso = nullptr;
263
Tim Van Patten5f744f12018-12-12 11:46:21 -0700264 cnx->shouldUseAngle = false;
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600265 cnx->angleDecided = false;
266 cnx->useAngle = false;
267
268 if (cnx->vendorEGL) {
269 dlclose(cnx->vendorEGL);
270 cnx->vendorEGL = nullptr;
271 }
Mathias Agopiande586972009-05-28 17:39:03 -0700272}
273
Jesse Hall94cdba92013-07-11 09:40:35 -0700274void Loader::init_api(void* dso,
275 char const * const * api,
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700276 char const * const * ref_api,
Jesse Hall94cdba92013-07-11 09:40:35 -0700277 __eglMustCastToProperFunctionPointerType* curr,
278 getProcAddressType getProcAddress)
Mathias Agopiande586972009-05-28 17:39:03 -0700279{
Jesse Hall1508ae62017-01-19 17:43:26 -0800280 ATRACE_CALL();
281
Mathias Agopian7773c432012-02-13 20:06:08 -0800282 const ssize_t SIZE = 256;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700283 char scrap[SIZE];
Mathias Agopiande586972009-05-28 17:39:03 -0700284 while (*api) {
285 char const * name = *api;
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700286 if (ref_api) {
287 char const * ref_name = *ref_api;
288 if (std::strcmp(name, ref_name) != 0) {
289 *curr++ = nullptr;
290 ref_api++;
291 continue;
292 }
293 }
294
Jesse Hall94cdba92013-07-11 09:40:35 -0700295 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700296 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700297 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700298 // couldn't find the entry-point, use eglGetProcAddress()
299 f = getProcAddress(name);
300 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700301 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700302 // Try without the OES postfix
303 ssize_t index = ssize_t(strlen(name)) - 3;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700304 if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) {
Mathias Agopiande586972009-05-28 17:39:03 -0700305 strncpy(scrap, name, index);
306 scrap[index] = 0;
307 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000308 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700309 }
310 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700311 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700312 // Try with the OES postfix
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700313 ssize_t index = ssize_t(strlen(name)) - 3;
314 if (index>0 && strcmp(name+index, "OES")) {
315 snprintf(scrap, SIZE, "%sOES", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700316 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000317 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700318 }
319 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700320 if (f == nullptr) {
Steve Block9d453682011-12-20 16:23:08 +0000321 //ALOGD("%s", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700322 f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented;
Mathias Agopian48d438d2012-01-28 21:44:00 -0800323
324 /*
325 * GL_EXT_debug_label is special, we always report it as
326 * supported, it's handled by GLES_trace. If GLES_trace is not
327 * enabled, then these are no-ops.
328 */
329 if (!strcmp(name, "glInsertEventMarkerEXT")) {
330 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
331 } else if (!strcmp(name, "glPushGroupMarkerEXT")) {
332 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
333 } else if (!strcmp(name, "glPopGroupMarkerEXT")) {
334 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
335 }
Mathias Agopiande586972009-05-28 17:39:03 -0700336 }
337 *curr++ = f;
338 api++;
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700339 if (ref_api) ref_api++;
Mathias Agopiande586972009-05-28 17:39:03 -0700340 }
341}
342
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700343static void* load_system_driver(const char* kind, const char* suffix) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800344 ATRACE_CALL();
Mathias Agopian99381422013-04-23 20:52:29 +0200345 class MatchFile {
346 public:
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700347 static std::string find(const char* libraryName) {
Mathias Agopian99381422013-04-23 20:52:29 +0200348 const char* const searchPaths[] = {
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800349#if defined(__LP64__)
350 "/vendor/lib64/egl",
351 "/system/lib64/egl"
352#else
Mathias Agopian99381422013-04-23 20:52:29 +0200353 "/vendor/lib/egl",
354 "/system/lib/egl"
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800355#endif
Mathias Agopian99381422013-04-23 20:52:29 +0200356 };
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700357
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700358 for (auto dir : searchPaths) {
359 std::string absolutePath = dir + std::string("/") + libraryName + ".so";
Mathias Agopian65421432017-03-08 11:49:05 -0800360 if (!access(absolutePath.c_str(), R_OK)) {
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700361 return absolutePath;
Mathias Agopian99381422013-04-23 20:52:29 +0200362 }
Mathias Agopian99381422013-04-23 20:52:29 +0200363 }
364
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700365 // Driver not found. gah.
366 return std::string();
Mathias Agopian99381422013-04-23 20:52:29 +0200367 }
368 };
369
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700370 std::string libraryName = std::string("lib") + kind;
371 if (suffix) {
372 libraryName += std::string("_") + suffix;
373 }
374 std::string absolutePath = MatchFile::find(libraryName.c_str());
Mathias Agopian65421432017-03-08 11:49:05 -0800375 if (absolutePath.empty()) {
Mathias Agopian99381422013-04-23 20:52:29 +0200376 // this happens often, we don't want to log an error
Yi Kong48a6cd22018-07-18 10:07:09 -0700377 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700378 }
Mathias Agopian65421432017-03-08 11:49:05 -0800379 const char* const driver_absolute_path = absolutePath.c_str();
Mathias Agopiande586972009-05-28 17:39:03 -0700380
Jiyong Park5910dc72017-04-05 14:23:52 +0900381 // Try to load drivers from the 'sphal' namespace, if it exist. Fall back to
Justin Yunb7320302017-05-22 15:13:40 +0900382 // the original routine when the namespace does not exist.
Jiyong Park5910dc72017-04-05 14:23:52 +0900383 // See /system/core/rootdir/etc/ld.config.txt for the configuration of the
384 // sphal namespace.
Justin Yunb7320302017-05-22 15:13:40 +0900385 void* dso = do_android_load_sphal_library(driver_absolute_path,
386 RTLD_NOW | RTLD_LOCAL);
Yi Kong48a6cd22018-07-18 10:07:09 -0700387 if (dso == nullptr) {
Mathias Agopian8c173842009-09-20 16:01:02 -0700388 const char* err = dlerror();
Mathias Agopian65421432017-03-08 11:49:05 -0800389 ALOGE("load_driver(%s): %s", driver_absolute_path, err ? err : "unknown");
Yi Kong48a6cd22018-07-18 10:07:09 -0700390 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700391 }
392
Steve Block9d453682011-12-20 16:23:08 +0000393 ALOGD("loaded %s", driver_absolute_path);
Mathias Agopianbaca89c2009-08-20 19:09:34 -0700394
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800395 return dso;
396}
397
Cody Northrop1f00e172018-04-02 11:23:31 -0600398static void* load_angle_from_namespace(const char* kind, android_namespace_t* ns) {
399 const android_dlextinfo dlextinfo = {
400 .flags = ANDROID_DLEXT_USE_NAMESPACE,
401 .library_namespace = ns,
402 };
403
404 std::string name = std::string("lib") + kind + "_angle.so";
405
406 void* so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
407
408 if (so) {
409 ALOGD("dlopen_ext from APK (%s) success at %p", name.c_str(), so);
410 return so;
411 } else {
412 ALOGE("dlopen_ext(\"%s\") failed: %s", name.c_str(), dlerror());
413 }
414
415 return nullptr;
416}
417
Cody Northrop6d082ef2018-09-11 09:42:31 -0600418static void* load_angle(const char* kind, android_namespace_t* ns, egl_connection_t* cnx) {
Courtney Goeltzenleuchter985bc282018-11-02 14:24:55 -0600419 void* so = nullptr;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700420
421 if ((cnx->shouldUseAngle) || android::GraphicsEnv::getInstance().shouldUseAngle()) {
Ian Elliott34474472018-09-05 10:57:07 -0600422 so = load_angle_from_namespace(kind, ns);
Tim Van Patten5f744f12018-12-12 11:46:21 -0700423 cnx->shouldUseAngle = true;
424 } else {
425 cnx->shouldUseAngle = false;
Ian Elliott34474472018-09-05 10:57:07 -0600426 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600427
428 if (so) {
Tim Van Patten5f744f12018-12-12 11:46:21 -0700429 ALOGV("Loaded ANGLE %s library for '%s' (instead of native)", kind,
430 android::GraphicsEnv::getInstance().getAngleAppName().c_str());
Cody Northrop1f00e172018-04-02 11:23:31 -0600431 cnx->useAngle = true;
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600432
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600433 char prop[PROPERTY_VALUE_MAX];
Tim Van Patten5f744f12018-12-12 11:46:21 -0700434
435 property_get("debug.hwui.renderer", prop, "UNSET");
436 ALOGV("Skia's renderer set to %s", prop);
437
438 EGLint angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE;
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600439 property_get("debug.angle.backend", prop, "0");
440 switch (atoi(prop)) {
441 case 1:
442 ALOGV("%s: Requesting OpenGLES back-end", __FUNCTION__);
443 angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE;
444 break;
445 case 2:
446 ALOGV("%s: Requesting Vulkan back-end", __FUNCTION__);
447 angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE;
448 break;
449 default:
450 break;
451 }
452
453 cnx->angleBackend = angleBackendDefault;
454 if (!cnx->vendorEGL && (cnx->angleBackend == EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE)) {
455 // Find and load vendor libEGL for ANGLE's GL back-end to use.
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700456 char prop[PROPERTY_VALUE_MAX + 1];
457 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
458 if (property_get(key, prop, nullptr) <= 0) {
459 continue;
460 }
461 void* dso = load_system_driver("EGL", prop);
462 if (dso) {
463 cnx->vendorEGL = dso;
464 break;
465 }
466 }
467 if (!cnx->vendorEGL) {
468 cnx->vendorEGL = load_system_driver("EGL", nullptr);
469 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600470 }
Ian Elliottb058aff2018-08-09 12:00:55 -0600471 } else {
Tim Van Patten5f744f12018-12-12 11:46:21 -0700472 ALOGV("Loaded native %s library for '%s' (instead of ANGLE)", kind,
473 android::GraphicsEnv::getInstance().getAngleAppName().c_str());
474 cnx->useAngle = false;
Cody Northrop1f00e172018-04-02 11:23:31 -0600475 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700476 cnx->angleDecided = true;
Cody Northrop1f00e172018-04-02 11:23:31 -0600477
Tim Van Patten5f744f12018-12-12 11:46:21 -0700478 return so;
Cody Northrop1f00e172018-04-02 11:23:31 -0600479}
480
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800481static void* load_updated_driver(const char* kind, android_namespace_t* ns) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800482 ATRACE_CALL();
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800483 const android_dlextinfo dlextinfo = {
484 .flags = ANDROID_DLEXT_USE_NAMESPACE,
485 .library_namespace = ns,
486 };
487 void* so = nullptr;
488 char prop[PROPERTY_VALUE_MAX + 1];
489 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700490 if (property_get(key, prop, nullptr) <= 0) {
491 continue;
492 }
493 std::string name = std::string("lib") + kind + "_" + prop + ".so";
494 so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
495 if (so) {
496 return so;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800497 }
498 }
499 return nullptr;
500}
501
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700502Loader::driver_t* Loader::attempt_to_load_angle(egl_connection_t* cnx) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800503 ATRACE_CALL();
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600504 android_namespace_t* ns = android::GraphicsEnv::getInstance().getAngleNamespace();
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700505 if (!ns) {
506 return nullptr;
Cody Northrop6d082ef2018-09-11 09:42:31 -0600507 }
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700508
509 android::GraphicsEnv::getInstance().setDriverToLoad(android::GraphicsEnv::Driver::ANGLE);
510 driver_t* hnd = nullptr;
511
512 // ANGLE doesn't ship with GLES library, and thus we skip GLES driver.
513 void* dso = load_angle("EGL", ns, cnx);
514 if (dso) {
515 initialize_api(dso, cnx, EGL);
516 hnd = new driver_t(dso);
517
518 dso = load_angle("GLESv1_CM", ns, cnx);
519 initialize_api(dso, cnx, GLESv1_CM);
520 hnd->set(dso, GLESv1_CM);
521
522 dso = load_angle("GLESv2", ns, cnx);
523 initialize_api(dso, cnx, GLESv2);
524 hnd->set(dso, GLESv2);
525 }
526 return hnd;
527}
528
529Loader::driver_t* Loader::attempt_to_load_updated_driver(egl_connection_t* cnx) {
530 ATRACE_CALL();
Jiyong Parka243e5d2017-06-21 12:26:51 +0900531#ifndef __ANDROID_VNDK__
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700532 android_namespace_t* ns = android::GraphicsEnv::getInstance().getDriverNamespace();
533 if (!ns) {
534 return nullptr;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800535 }
536
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700537 android::GraphicsEnv::getInstance().setDriverToLoad(android::GraphicsEnv::Driver::GL_UPDATED);
538 driver_t* hnd = nullptr;
539 void* dso = load_updated_driver("GLES", ns);
Peiyong Line83b8682019-04-18 17:23:02 -0700540 if (dso) {
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700541 initialize_api(dso, cnx, EGL | GLESv1_CM | GLESv2);
542 hnd = new driver_t(dso);
543 return hnd;
Peiyong Line83b8682019-04-18 17:23:02 -0700544 }
545
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700546 dso = load_updated_driver("EGL", ns);
547 if (dso) {
548 initialize_api(dso, cnx, EGL);
549 hnd = new driver_t(dso);
550
551 dso = load_updated_driver("GLESv1_CM", ns);
552 initialize_api(dso, cnx, GLESv1_CM);
553 hnd->set(dso, GLESv1_CM);
554
555 dso = load_updated_driver("GLESv2", ns);
556 initialize_api(dso, cnx, GLESv2);
557 hnd->set(dso, GLESv2);
558 }
559 return hnd;
560#else
Peiyong Line83b8682019-04-18 17:23:02 -0700561 return nullptr;
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700562#endif
563}
564
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700565Loader::driver_t* Loader::attempt_to_load_system_driver(egl_connection_t* cnx, const char* suffix) {
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700566 ATRACE_CALL();
567 android::GraphicsEnv::getInstance().setDriverToLoad(android::GraphicsEnv::Driver::GL);
568 driver_t* hnd = nullptr;
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700569 void* dso = load_system_driver("GLES", suffix);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700570 if (dso) {
571 initialize_api(dso, cnx, EGL | GLESv1_CM | GLESv2);
572 hnd = new driver_t(dso);
573 return hnd;
574 }
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700575 dso = load_system_driver("EGL", suffix);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700576 if (dso) {
577 initialize_api(dso, cnx, EGL);
578 hnd = new driver_t(dso);
579
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700580 dso = load_system_driver("GLESv1_CM", suffix);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700581 initialize_api(dso, cnx, GLESv1_CM);
582 hnd->set(dso, GLESv1_CM);
583
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700584 dso = load_system_driver("GLESv2", suffix);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700585 initialize_api(dso, cnx, GLESv2);
586 hnd->set(dso, GLESv2);
587 }
588 return hnd;
Peiyong Line83b8682019-04-18 17:23:02 -0700589}
590
591void Loader::initialize_api(void* dso, egl_connection_t* cnx, uint32_t mask) {
Mathias Agopiande586972009-05-28 17:39:03 -0700592 if (mask & EGL) {
593 getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress");
594
Jesse Hall94cdba92013-07-11 09:40:35 -0700595 ALOGE_IF(!getProcAddress,
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800596 "can't find eglGetProcAddress() in EGL driver library");
Mathias Agopiande586972009-05-28 17:39:03 -0700597
Mathias Agopian618fa102009-10-14 02:06:37 -0700598 egl_t* egl = &cnx->egl;
Mathias Agopiande586972009-05-28 17:39:03 -0700599 __eglMustCastToProperFunctionPointerType* curr =
600 (__eglMustCastToProperFunctionPointerType*)egl;
601 char const * const * api = egl_names;
602 while (*api) {
603 char const * name = *api;
Jesse Hall94cdba92013-07-11 09:40:35 -0700604 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700605 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700606 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700607 // couldn't find the entry-point, use eglGetProcAddress()
608 f = getProcAddress(name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700609 if (f == nullptr) {
610 f = (__eglMustCastToProperFunctionPointerType)nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700611 }
612 }
613 *curr++ = f;
614 api++;
615 }
616 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700617
Mathias Agopiande586972009-05-28 17:39:03 -0700618 if (mask & GLESv1_CM) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700619 init_api(dso, gl_names_1, gl_names,
Mathias Agopian618fa102009-10-14 02:06:37 -0700620 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800621 &cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl,
Mathias Agopian618fa102009-10-14 02:06:37 -0700622 getProcAddress);
Mathias Agopiande586972009-05-28 17:39:03 -0700623 }
624
625 if (mask & GLESv2) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700626 init_api(dso, gl_names, nullptr,
Mathias Agopian618fa102009-10-14 02:06:37 -0700627 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800628 &cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl,
Mathias Agopiande586972009-05-28 17:39:03 -0700629 getProcAddress);
630 }
Mathias Agopiande586972009-05-28 17:39:03 -0700631}
632
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700633} // namespace android