blob: 10dd8cbf52a9cfb395adf99531f5dfbf157591c1 [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
Mathias Agopianada798b2012-02-13 17:09:30 -0800211void* Loader::open(egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700212{
Jesse Hall1508ae62017-01-19 17:43:26 -0800213 ATRACE_CALL();
Yiwei Zhangd9861812019-02-13 11:51:55 -0800214 const nsecs_t openTime = systemTime();
Jesse Hall1508ae62017-01-19 17:43:26 -0800215
bohu69e5b1a2016-02-19 17:15:20 -0800216 setEmulatorGlesValue();
217
Tim Van Patten5f744f12018-12-12 11:46:21 -0700218 // Check if we should use ANGLE early, so loading each driver doesn't require repeated queries.
219 if (android::GraphicsEnv::getInstance().shouldUseAngle()) {
220 cnx->shouldUseAngle = true;
221 } else {
222 cnx->shouldUseAngle = false;
223 }
224
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700225 // Firstly, try to load ANGLE driver.
226 driver_t* hnd = attempt_to_load_angle(cnx);
227 if (!hnd) {
228 // Secondly, try to load from driver apk.
229 hnd = attempt_to_load_updated_driver(cnx);
230 }
231 if (!hnd) {
232 // Thirdly, try to load emulation driver.
233 hnd = attempt_to_load_emulation_driver(cnx);
234 }
235 if (!hnd) {
236 // Finally, load system driver.
237 hnd = attempt_to_load_system_driver(cnx);
Mathias Agopiande586972009-05-28 17:39:03 -0700238 }
239
Yiwei Zhangd9861812019-02-13 11:51:55 -0800240 if (!hnd) {
241 android::GraphicsEnv::getInstance().setDriverLoaded(android::GraphicsEnv::Api::API_GL,
242 false, systemTime() - openTime);
243 }
244
Mathias Agopian99381422013-04-23 20:52:29 +0200245 LOG_ALWAYS_FATAL_IF(!hnd, "couldn't find an OpenGL ES implementation");
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 Lin8cd204d2019-04-19 14:05:17 -0700353static void* load_emulation_driver(const char* kind) {
Peiyong Line83b8682019-04-18 17:23:02 -0700354 const int emulationStatus = checkGlesEmulationStatus();
355
356 // Invalid emulation status, abort.
357 if (emulationStatus < 0 || emulationStatus > 2) {
358 return nullptr;
359 }
360
361 std::string absolutePath;
362 switch (emulationStatus) {
363 case 0:
364#if defined(__LP64__)
365 absolutePath = "/vendor/lib64/egl/libGLES_android.so";
366#else
367 absolutePath = "/vendor/lib/egl/libGLES_android.so";
368#endif
369 break;
370 case 1:
371 // Use host-side OpenGL through the "emulation" library
372#if defined(__LP64__)
373 absolutePath = std::string("/vendor/lib64/egl/lib") + kind + "_emulation.so";
374#else
375 absolutePath = std::string("/vendor/lib/egl/lib") + kind + "_emulation.so";
376#endif
377 break;
378 case 2:
379 // Use guest side swiftshader library
380#if defined(__LP64__)
381 absolutePath = std::string("/vendor/lib64/egl/lib") + kind + "_swiftshader.so";
382#else
383 absolutePath = std::string("/vendor/lib/egl/lib") + kind + "_swiftshader.so";
384#endif
385 break;
386 default:
387 // Not in emulator, or use other guest-side implementation
388 break;
389 }
390 if (absolutePath.empty()) {
391 // this happens often, we don't want to log an error
392 return nullptr;
393 }
394 const char* const driver_absolute_path = absolutePath.c_str();
395
396 // Try to load drivers from the 'sphal' namespace, if it exist. Fall back to
397 // the original routine when the namespace does not exist.
398 // See /system/core/rootdir/etc/ld.config.txt for the configuration of the
399 // sphal namespace.
400 void* dso = do_android_load_sphal_library(driver_absolute_path,
401 RTLD_NOW | RTLD_LOCAL);
402 if (dso == nullptr) {
403 const char* err = dlerror();
404 ALOGE("load_driver(%s): %s", driver_absolute_path, err ? err : "unknown");
405 return nullptr;
406 }
407 ALOGD("loaded %s", driver_absolute_path);
408 return dso;
409}
410
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800411static void* load_system_driver(const char* kind) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800412 ATRACE_CALL();
Mathias Agopian99381422013-04-23 20:52:29 +0200413 class MatchFile {
414 public:
Mathias Agopian65421432017-03-08 11:49:05 -0800415 static std::string find(const char* kind) {
416 std::string result;
Mathias Agopian65421432017-03-08 11:49:05 -0800417 std::string pattern = std::string("lib") + kind;
Mathias Agopian99381422013-04-23 20:52:29 +0200418 const char* const searchPaths[] = {
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800419#if defined(__LP64__)
420 "/vendor/lib64/egl",
421 "/system/lib64/egl"
422#else
Mathias Agopian99381422013-04-23 20:52:29 +0200423 "/vendor/lib/egl",
424 "/system/lib/egl"
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800425#endif
Mathias Agopian99381422013-04-23 20:52:29 +0200426 };
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700427
Mathias Agopian99381422013-04-23 20:52:29 +0200428 // first, we search for the exact name of the GLES userspace
429 // driver in both locations.
430 // i.e.:
431 // libGLES.so, or:
432 // libEGL.so, libGLESv1_CM.so, libGLESv2.so
433
434 for (size_t i=0 ; i<NELEM(searchPaths) ; i++) {
435 if (find(result, pattern, searchPaths[i], true)) {
436 return result;
437 }
438 }
439
440 // for compatibility with the old "egl.cfg" naming convention
441 // we look for files that match:
442 // libGLES_*.so, or:
443 // libEGL_*.so, libGLESv1_CM_*.so, libGLESv2_*.so
444
445 pattern.append("_");
446 for (size_t i=0 ; i<NELEM(searchPaths) ; i++) {
447 if (find(result, pattern, searchPaths[i], false)) {
448 return result;
449 }
450 }
451
452 // we didn't find the driver. gah.
453 result.clear();
454 return result;
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700455 }
Mathias Agopian99381422013-04-23 20:52:29 +0200456
457 private:
Mathias Agopian65421432017-03-08 11:49:05 -0800458 static bool find(std::string& result,
459 const std::string& pattern, const char* const search, bool exact) {
Mathias Agopian99381422013-04-23 20:52:29 +0200460 if (exact) {
Jesse Hall4aaa9602017-08-29 16:33:54 -0700461 std::string absolutePath = std::string(search) + "/" + pattern + ".so";
Mathias Agopian65421432017-03-08 11:49:05 -0800462 if (!access(absolutePath.c_str(), R_OK)) {
Mathias Agopian99381422013-04-23 20:52:29 +0200463 result = absolutePath;
464 return true;
465 }
466 return false;
467 }
468
469 DIR* d = opendir(search);
Yi Kong48a6cd22018-07-18 10:07:09 -0700470 if (d != nullptr) {
Mathias Agopian99381422013-04-23 20:52:29 +0200471 struct dirent* e;
Yi Kong48a6cd22018-07-18 10:07:09 -0700472 while ((e = readdir(d)) != nullptr) {
Mathias Agopian99381422013-04-23 20:52:29 +0200473 if (e->d_type == DT_DIR) {
474 continue;
475 }
476 if (!strcmp(e->d_name, "libGLES_android.so")) {
477 // always skip the software renderer
478 continue;
479 }
Mathias Agopian65421432017-03-08 11:49:05 -0800480 if (strstr(e->d_name, pattern.c_str()) == e->d_name) {
Mathias Agopian99381422013-04-23 20:52:29 +0200481 if (!strcmp(e->d_name + strlen(e->d_name) - 3, ".so")) {
Mathias Agopian65421432017-03-08 11:49:05 -0800482 result = std::string(search) + "/" + e->d_name;
Mathias Agopian99381422013-04-23 20:52:29 +0200483 closedir(d);
484 return true;
485 }
486 }
487 }
488 closedir(d);
489 }
490 return false;
491 }
492 };
493
494
Mathias Agopian65421432017-03-08 11:49:05 -0800495 std::string absolutePath = MatchFile::find(kind);
496 if (absolutePath.empty()) {
Mathias Agopian99381422013-04-23 20:52:29 +0200497 // this happens often, we don't want to log an error
Yi Kong48a6cd22018-07-18 10:07:09 -0700498 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700499 }
Mathias Agopian65421432017-03-08 11:49:05 -0800500 const char* const driver_absolute_path = absolutePath.c_str();
Mathias Agopiande586972009-05-28 17:39:03 -0700501
Jiyong Park5910dc72017-04-05 14:23:52 +0900502 // Try to load drivers from the 'sphal' namespace, if it exist. Fall back to
Justin Yunb7320302017-05-22 15:13:40 +0900503 // the original routine when the namespace does not exist.
Jiyong Park5910dc72017-04-05 14:23:52 +0900504 // See /system/core/rootdir/etc/ld.config.txt for the configuration of the
505 // sphal namespace.
Justin Yunb7320302017-05-22 15:13:40 +0900506 void* dso = do_android_load_sphal_library(driver_absolute_path,
507 RTLD_NOW | RTLD_LOCAL);
Yi Kong48a6cd22018-07-18 10:07:09 -0700508 if (dso == nullptr) {
Mathias Agopian8c173842009-09-20 16:01:02 -0700509 const char* err = dlerror();
Mathias Agopian65421432017-03-08 11:49:05 -0800510 ALOGE("load_driver(%s): %s", driver_absolute_path, err ? err : "unknown");
Yi Kong48a6cd22018-07-18 10:07:09 -0700511 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700512 }
513
Steve Block9d453682011-12-20 16:23:08 +0000514 ALOGD("loaded %s", driver_absolute_path);
Mathias Agopianbaca89c2009-08-20 19:09:34 -0700515
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800516 return dso;
517}
518
Cody Northrop1f00e172018-04-02 11:23:31 -0600519static void* load_angle_from_namespace(const char* kind, android_namespace_t* ns) {
520 const android_dlextinfo dlextinfo = {
521 .flags = ANDROID_DLEXT_USE_NAMESPACE,
522 .library_namespace = ns,
523 };
524
525 std::string name = std::string("lib") + kind + "_angle.so";
526
527 void* so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
528
529 if (so) {
530 ALOGD("dlopen_ext from APK (%s) success at %p", name.c_str(), so);
531 return so;
532 } else {
533 ALOGE("dlopen_ext(\"%s\") failed: %s", name.c_str(), dlerror());
534 }
535
536 return nullptr;
537}
538
Cody Northrop6d082ef2018-09-11 09:42:31 -0600539static void* load_angle(const char* kind, android_namespace_t* ns, egl_connection_t* cnx) {
Courtney Goeltzenleuchter985bc282018-11-02 14:24:55 -0600540 void* so = nullptr;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700541
542 if ((cnx->shouldUseAngle) || android::GraphicsEnv::getInstance().shouldUseAngle()) {
Ian Elliott34474472018-09-05 10:57:07 -0600543 so = load_angle_from_namespace(kind, ns);
Tim Van Patten5f744f12018-12-12 11:46:21 -0700544 cnx->shouldUseAngle = true;
545 } else {
546 cnx->shouldUseAngle = false;
Ian Elliott34474472018-09-05 10:57:07 -0600547 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600548
549 if (so) {
Tim Van Patten5f744f12018-12-12 11:46:21 -0700550 ALOGV("Loaded ANGLE %s library for '%s' (instead of native)", kind,
551 android::GraphicsEnv::getInstance().getAngleAppName().c_str());
Cody Northrop1f00e172018-04-02 11:23:31 -0600552 cnx->useAngle = true;
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600553
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600554 char prop[PROPERTY_VALUE_MAX];
Tim Van Patten5f744f12018-12-12 11:46:21 -0700555
556 property_get("debug.hwui.renderer", prop, "UNSET");
557 ALOGV("Skia's renderer set to %s", prop);
558
559 EGLint angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE;
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600560 property_get("debug.angle.backend", prop, "0");
561 switch (atoi(prop)) {
562 case 1:
563 ALOGV("%s: Requesting OpenGLES back-end", __FUNCTION__);
564 angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE;
565 break;
566 case 2:
567 ALOGV("%s: Requesting Vulkan back-end", __FUNCTION__);
568 angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE;
569 break;
570 default:
571 break;
572 }
573
574 cnx->angleBackend = angleBackendDefault;
575 if (!cnx->vendorEGL && (cnx->angleBackend == EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE)) {
576 // Find and load vendor libEGL for ANGLE's GL back-end to use.
Cody Northrop1f00e172018-04-02 11:23:31 -0600577 cnx->vendorEGL = load_system_driver("EGL");
578 }
Ian Elliottb058aff2018-08-09 12:00:55 -0600579 } else {
Tim Van Patten5f744f12018-12-12 11:46:21 -0700580 ALOGV("Loaded native %s library for '%s' (instead of ANGLE)", kind,
581 android::GraphicsEnv::getInstance().getAngleAppName().c_str());
582 cnx->useAngle = false;
Cody Northrop1f00e172018-04-02 11:23:31 -0600583 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700584 cnx->angleDecided = true;
Cody Northrop1f00e172018-04-02 11:23:31 -0600585
Tim Van Patten5f744f12018-12-12 11:46:21 -0700586 return so;
Cody Northrop1f00e172018-04-02 11:23:31 -0600587}
588
Mathias Agopian311b4792017-02-28 15:00:49 -0800589static const char* HAL_SUBNAME_KEY_PROPERTIES[2] = {
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800590 "ro.hardware.egl",
591 "ro.board.platform",
Mathias Agopian311b4792017-02-28 15:00:49 -0800592};
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800593
594static void* load_updated_driver(const char* kind, android_namespace_t* ns) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800595 ATRACE_CALL();
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800596 const android_dlextinfo dlextinfo = {
597 .flags = ANDROID_DLEXT_USE_NAMESPACE,
598 .library_namespace = ns,
599 };
600 void* so = nullptr;
601 char prop[PROPERTY_VALUE_MAX + 1];
602 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
603 if (property_get(key, prop, nullptr) > 0) {
Mathias Agopian65421432017-03-08 11:49:05 -0800604 std::string name = std::string("lib") + kind + "_" + prop + ".so";
605 so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
Mathias Agopian311b4792017-02-28 15:00:49 -0800606 if (so) {
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800607 return so;
Mathias Agopian311b4792017-02-28 15:00:49 -0800608 }
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800609 }
610 }
611 return nullptr;
612}
613
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700614Loader::driver_t* Loader::attempt_to_load_angle(egl_connection_t* cnx) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800615 ATRACE_CALL();
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600616 android_namespace_t* ns = android::GraphicsEnv::getInstance().getAngleNamespace();
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700617 if (!ns) {
618 return nullptr;
Cody Northrop6d082ef2018-09-11 09:42:31 -0600619 }
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700620
621 android::GraphicsEnv::getInstance().setDriverToLoad(android::GraphicsEnv::Driver::ANGLE);
622 driver_t* hnd = nullptr;
623
624 // ANGLE doesn't ship with GLES library, and thus we skip GLES driver.
625 void* dso = load_angle("EGL", ns, cnx);
626 if (dso) {
627 initialize_api(dso, cnx, EGL);
628 hnd = new driver_t(dso);
629
630 dso = load_angle("GLESv1_CM", ns, cnx);
631 initialize_api(dso, cnx, GLESv1_CM);
632 hnd->set(dso, GLESv1_CM);
633
634 dso = load_angle("GLESv2", ns, cnx);
635 initialize_api(dso, cnx, GLESv2);
636 hnd->set(dso, GLESv2);
637 }
638 return hnd;
639}
640
641Loader::driver_t* Loader::attempt_to_load_updated_driver(egl_connection_t* cnx) {
642 ATRACE_CALL();
Jiyong Parka243e5d2017-06-21 12:26:51 +0900643#ifndef __ANDROID_VNDK__
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700644 android_namespace_t* ns = android::GraphicsEnv::getInstance().getDriverNamespace();
645 if (!ns) {
646 return nullptr;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800647 }
648
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700649 android::GraphicsEnv::getInstance().setDriverToLoad(android::GraphicsEnv::Driver::GL_UPDATED);
650 driver_t* hnd = nullptr;
651 void* dso = load_updated_driver("GLES", ns);
Peiyong Line83b8682019-04-18 17:23:02 -0700652 if (dso) {
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700653 initialize_api(dso, cnx, EGL | GLESv1_CM | GLESv2);
654 hnd = new driver_t(dso);
655 return hnd;
Peiyong Line83b8682019-04-18 17:23:02 -0700656 }
657
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700658 dso = load_updated_driver("EGL", ns);
659 if (dso) {
660 initialize_api(dso, cnx, EGL);
661 hnd = new driver_t(dso);
662
663 dso = load_updated_driver("GLESv1_CM", ns);
664 initialize_api(dso, cnx, GLESv1_CM);
665 hnd->set(dso, GLESv1_CM);
666
667 dso = load_updated_driver("GLESv2", ns);
668 initialize_api(dso, cnx, GLESv2);
669 hnd->set(dso, GLESv2);
670 }
671 return hnd;
672#else
Peiyong Line83b8682019-04-18 17:23:02 -0700673 return nullptr;
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700674#endif
675}
676
677Loader::driver_t* Loader::attempt_to_load_emulation_driver(egl_connection_t* cnx) {
678 ATRACE_CALL();
679 android::GraphicsEnv::getInstance().setDriverToLoad(android::GraphicsEnv::Driver::GL);
680 driver_t* hnd = nullptr;
681 void* dso = load_emulation_driver("GLES");
682 if (dso) {
683 initialize_api(dso, cnx, EGL | GLESv1_CM | GLESv2);
684 hnd = new driver_t(dso);
685 return hnd;
686 }
687 dso = load_emulation_driver("EGL");
688 if (dso) {
689 initialize_api(dso, cnx, EGL);
690 hnd = new driver_t(dso);
691
692 dso = load_emulation_driver("GLESv1_CM");
693 initialize_api(dso, cnx, GLESv1_CM);
694 hnd->set(dso, GLESv1_CM);
695
696 dso = load_emulation_driver("GLESv2");
697 initialize_api(dso, cnx, GLESv2);
698 hnd->set(dso, GLESv2);
699 }
700 return hnd;
701}
702
703Loader::driver_t* Loader::attempt_to_load_system_driver(egl_connection_t* cnx) {
704 ATRACE_CALL();
705 android::GraphicsEnv::getInstance().setDriverToLoad(android::GraphicsEnv::Driver::GL);
706 driver_t* hnd = nullptr;
707 void* dso = load_system_driver("GLES");
708 if (dso) {
709 initialize_api(dso, cnx, EGL | GLESv1_CM | GLESv2);
710 hnd = new driver_t(dso);
711 return hnd;
712 }
713 dso = load_system_driver("EGL");
714 if (dso) {
715 initialize_api(dso, cnx, EGL);
716 hnd = new driver_t(dso);
717
718 dso = load_system_driver("GLESv1_CM");
719 initialize_api(dso, cnx, GLESv1_CM);
720 hnd->set(dso, GLESv1_CM);
721
722 dso = load_system_driver("GLESv2");
723 initialize_api(dso, cnx, GLESv2);
724 hnd->set(dso, GLESv2);
725 }
726 return hnd;
Peiyong Line83b8682019-04-18 17:23:02 -0700727}
728
729void Loader::initialize_api(void* dso, egl_connection_t* cnx, uint32_t mask) {
Mathias Agopiande586972009-05-28 17:39:03 -0700730 if (mask & EGL) {
731 getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress");
732
Jesse Hall94cdba92013-07-11 09:40:35 -0700733 ALOGE_IF(!getProcAddress,
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800734 "can't find eglGetProcAddress() in EGL driver library");
Mathias Agopiande586972009-05-28 17:39:03 -0700735
Mathias Agopian618fa102009-10-14 02:06:37 -0700736 egl_t* egl = &cnx->egl;
Mathias Agopiande586972009-05-28 17:39:03 -0700737 __eglMustCastToProperFunctionPointerType* curr =
738 (__eglMustCastToProperFunctionPointerType*)egl;
739 char const * const * api = egl_names;
740 while (*api) {
741 char const * name = *api;
Jesse Hall94cdba92013-07-11 09:40:35 -0700742 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700743 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700744 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700745 // couldn't find the entry-point, use eglGetProcAddress()
746 f = getProcAddress(name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700747 if (f == nullptr) {
748 f = (__eglMustCastToProperFunctionPointerType)nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700749 }
750 }
751 *curr++ = f;
752 api++;
753 }
754 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700755
Mathias Agopiande586972009-05-28 17:39:03 -0700756 if (mask & GLESv1_CM) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700757 init_api(dso, gl_names_1, gl_names,
Mathias Agopian618fa102009-10-14 02:06:37 -0700758 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800759 &cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl,
Mathias Agopian618fa102009-10-14 02:06:37 -0700760 getProcAddress);
Mathias Agopiande586972009-05-28 17:39:03 -0700761 }
762
763 if (mask & GLESv2) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700764 init_api(dso, gl_names, nullptr,
Mathias Agopian618fa102009-10-14 02:06:37 -0700765 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800766 &cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl,
Mathias Agopiande586972009-05-28 17:39:03 -0700767 getProcAddress);
768 }
Mathias Agopiande586972009-05-28 17:39:03 -0700769}
770
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700771} // namespace android