blob: 259242b45902d075290447fc65841b9068fe0fa6 [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
42// ----------------------------------------------------------------------------
43namespace android {
44// ----------------------------------------------------------------------------
45
46
47/*
Mathias Agopian99381422013-04-23 20:52:29 +020048 * EGL userspace drivers must be provided either:
49 * - as a single library:
50 * /vendor/lib/egl/libGLES.so
51 *
52 * - as separate libraries:
53 * /vendor/lib/egl/libEGL.so
54 * /vendor/lib/egl/libGLESv1_CM.so
55 * /vendor/lib/egl/libGLESv2.so
56 *
57 * The software renderer for the emulator must be provided as a single
58 * library at:
59 *
60 * /system/lib/egl/libGLES_android.so
61 *
62 *
63 * For backward compatibility and to facilitate the transition to
64 * this new naming scheme, the loader will additionally look for:
Jesse Hall94cdba92013-07-11 09:40:35 -070065 *
Mathias Agopian99381422013-04-23 20:52:29 +020066 * /{vendor|system}/lib/egl/lib{GLES | [EGL|GLESv1_CM|GLESv2]}_*.so
Jesse Hall94cdba92013-07-11 09:40:35 -070067 *
Mathias Agopiande586972009-05-28 17:39:03 -070068 */
69
Mathias Agopian65421432017-03-08 11:49:05 -080070Loader& Loader::getInstance() {
71 static Loader loader;
72 return loader;
73}
Mathias Agopiande586972009-05-28 17:39:03 -070074
David 'Digit' Turner80b30c22011-08-26 17:38:47 +020075/* This function is called to check whether we run inside the emulator,
76 * and if this is the case whether GLES GPU emulation is supported.
77 *
78 * Returned values are:
79 * -1 -> not running inside the emulator
80 * 0 -> running inside the emulator, but GPU emulation not supported
81 * 1 -> running inside the emulator, GPU emulation is supported
Nicolas Capens776951f2015-11-06 10:10:21 -050082 * through the "emulation" host-side OpenGL ES implementation.
83 * 2 -> running inside the emulator, GPU emulation is supported
84 * through a guest-side vendor driver's OpenGL ES implementation.
David 'Digit' Turner80b30c22011-08-26 17:38:47 +020085 */
86static int
87checkGlesEmulationStatus(void)
88{
89 /* We're going to check for the following kernel parameters:
90 *
91 * qemu=1 -> tells us that we run inside the emulator
92 * android.qemu.gles=<number> -> tells us the GLES GPU emulation status
93 *
94 * Note that we will return <number> if we find it. This let us support
95 * more additionnal emulation modes in the future.
96 */
97 char prop[PROPERTY_VALUE_MAX];
98 int result = -1;
99
100 /* First, check for qemu=1 */
101 property_get("ro.kernel.qemu",prop,"0");
102 if (atoi(prop) != 1)
103 return -1;
104
105 /* We are in the emulator, get GPU status value */
bohu69e5b1a2016-02-19 17:15:20 -0800106 property_get("qemu.gles",prop,"0");
David 'Digit' Turner80b30c22011-08-26 17:38:47 +0200107 return atoi(prop);
108}
109
Jesse Hall1508ae62017-01-19 17:43:26 -0800110static void* do_dlopen(const char* path, int mode) {
111 ATRACE_CALL();
112 return dlopen(path, mode);
113}
114
Jiyong Park5910dc72017-04-05 14:23:52 +0900115static void* do_android_dlopen_ext(const char* path, int mode, const android_dlextinfo* info) {
116 ATRACE_CALL();
117 return android_dlopen_ext(path, mode, info);
118}
119
Justin Yunb7320302017-05-22 15:13:40 +0900120static void* do_android_load_sphal_library(const char* path, int mode) {
121 ATRACE_CALL();
122 return android_load_sphal_library(path, mode);
123}
124
Mathias Agopiande586972009-05-28 17:39:03 -0700125// ----------------------------------------------------------------------------
126
Jesse Hall94cdba92013-07-11 09:40:35 -0700127Loader::driver_t::driver_t(void* gles)
Mathias Agopiande586972009-05-28 17:39:03 -0700128{
129 dso[0] = gles;
130 for (size_t i=1 ; i<NELEM(dso) ; i++)
Yi Kong48a6cd22018-07-18 10:07:09 -0700131 dso[i] = nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700132}
133
Jesse Hall94cdba92013-07-11 09:40:35 -0700134Loader::driver_t::~driver_t()
Mathias Agopiande586972009-05-28 17:39:03 -0700135{
136 for (size_t i=0 ; i<NELEM(dso) ; i++) {
137 if (dso[i]) {
138 dlclose(dso[i]);
Yi Kong48a6cd22018-07-18 10:07:09 -0700139 dso[i] = nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700140 }
141 }
142}
143
Mathias Agopian65421432017-03-08 11:49:05 -0800144int Loader::driver_t::set(void* hnd, int32_t api)
Mathias Agopiande586972009-05-28 17:39:03 -0700145{
146 switch (api) {
147 case EGL:
148 dso[0] = hnd;
149 break;
150 case GLESv1_CM:
151 dso[1] = hnd;
152 break;
153 case GLESv2:
154 dso[2] = hnd;
155 break;
156 default:
Mathias Agopian65421432017-03-08 11:49:05 -0800157 return -EOVERFLOW;
Mathias Agopiande586972009-05-28 17:39:03 -0700158 }
Mathias Agopian65421432017-03-08 11:49:05 -0800159 return 0;
Mathias Agopiande586972009-05-28 17:39:03 -0700160}
161
162// ----------------------------------------------------------------------------
163
Mathias Agopiande586972009-05-28 17:39:03 -0700164Loader::Loader()
Yi Kong48a6cd22018-07-18 10:07:09 -0700165 : getProcAddress(nullptr)
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800166{
Mathias Agopiande586972009-05-28 17:39:03 -0700167}
168
Mathias Agopian99381422013-04-23 20:52:29 +0200169Loader::~Loader() {
Mathias Agopiande586972009-05-28 17:39:03 -0700170}
171
Jesse Hallc07b5202013-07-04 12:08:16 -0700172static void* load_wrapper(const char* path) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800173 void* so = do_dlopen(path, RTLD_NOW | RTLD_LOCAL);
Jesse Hallc07b5202013-07-04 12:08:16 -0700174 ALOGE_IF(!so, "dlopen(\"%s\") failed: %s", path, dlerror());
175 return so;
176}
177
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700178#ifndef EGL_WRAPPER_DIR
179#if defined(__LP64__)
180#define EGL_WRAPPER_DIR "/system/lib64"
181#else
182#define EGL_WRAPPER_DIR "/system/lib"
183#endif
184#endif
185
bohu69e5b1a2016-02-19 17:15:20 -0800186static void setEmulatorGlesValue(void) {
187 char prop[PROPERTY_VALUE_MAX];
188 property_get("ro.kernel.qemu", prop, "0");
189 if (atoi(prop) != 1) return;
190
191 property_get("ro.kernel.qemu.gles",prop,"0");
192 if (atoi(prop) == 1) {
193 ALOGD("Emulator has host GPU support, qemu.gles is set to 1.");
194 property_set("qemu.gles", "1");
195 return;
196 }
197
198 // for now, checking the following
199 // directory is good enough for emulator system images
200 const char* vendor_lib_path =
201#if defined(__LP64__)
202 "/vendor/lib64/egl";
203#else
204 "/vendor/lib/egl";
205#endif
206
207 const bool has_vendor_lib = (access(vendor_lib_path, R_OK) == 0);
208 if (has_vendor_lib) {
209 ALOGD("Emulator has vendor provided software renderer, qemu.gles is set to 2.");
210 property_set("qemu.gles", "2");
211 } else {
212 ALOGD("Emulator without GPU support detected. "
213 "Fallback to legacy software renderer, qemu.gles is set to 0.");
214 property_set("qemu.gles", "0");
215 }
216}
217
Mathias Agopianada798b2012-02-13 17:09:30 -0800218void* Loader::open(egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700219{
Jesse Hall1508ae62017-01-19 17:43:26 -0800220 ATRACE_CALL();
Yiwei Zhangd9861812019-02-13 11:51:55 -0800221 const nsecs_t openTime = systemTime();
Jesse Hall1508ae62017-01-19 17:43:26 -0800222
Mathias Agopiande586972009-05-28 17:39:03 -0700223 void* dso;
Yi Kong48a6cd22018-07-18 10:07:09 -0700224 driver_t* hnd = nullptr;
Jesse Hall94cdba92013-07-11 09:40:35 -0700225
bohu69e5b1a2016-02-19 17:15:20 -0800226 setEmulatorGlesValue();
227
Tim Van Patten5f744f12018-12-12 11:46:21 -0700228 // Check if we should use ANGLE early, so loading each driver doesn't require repeated queries.
229 if (android::GraphicsEnv::getInstance().shouldUseAngle()) {
230 cnx->shouldUseAngle = true;
231 } else {
232 cnx->shouldUseAngle = false;
233 }
234
Cody Northrop9d5d33d2018-10-15 18:32:14 -0600235 dso = load_driver("GLES", cnx, EGL | GLESv1_CM | GLESv2);
Mathias Agopian99381422013-04-23 20:52:29 +0200236 if (dso) {
237 hnd = new driver_t(dso);
238 } else {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800239 android::GraphicsEnv::getInstance().clearDriverLoadingInfo(
240 android::GraphicsEnv::Api::API_GL);
Mathias Agopian99381422013-04-23 20:52:29 +0200241 // Always load EGL first
Cody Northrop9d5d33d2018-10-15 18:32:14 -0600242 dso = load_driver("EGL", cnx, EGL);
Mathias Agopiande586972009-05-28 17:39:03 -0700243 if (dso) {
244 hnd = new driver_t(dso);
Mathias Agopian99381422013-04-23 20:52:29 +0200245 hnd->set( load_driver("GLESv1_CM", cnx, GLESv1_CM), GLESv1_CM );
246 hnd->set( load_driver("GLESv2", cnx, GLESv2), GLESv2 );
Mathias Agopiande586972009-05-28 17:39:03 -0700247 }
248 }
249
Yiwei Zhangd9861812019-02-13 11:51:55 -0800250 if (!hnd) {
251 android::GraphicsEnv::getInstance().setDriverLoaded(android::GraphicsEnv::Api::API_GL,
252 false, systemTime() - openTime);
253 }
254
Mathias Agopian99381422013-04-23 20:52:29 +0200255 LOG_ALWAYS_FATAL_IF(!hnd, "couldn't find an OpenGL ES implementation");
Jesse Hallc07b5202013-07-04 12:08:16 -0700256
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700257 cnx->libEgl = load_wrapper(EGL_WRAPPER_DIR "/libEGL.so");
258 cnx->libGles2 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv2.so");
259 cnx->libGles1 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv1_CM.so");
260
Yiwei Zhangd9861812019-02-13 11:51:55 -0800261 if (!cnx->libEgl || !cnx->libGles2 || !cnx->libGles1) {
262 android::GraphicsEnv::getInstance().setDriverLoaded(android::GraphicsEnv::Api::API_GL,
263 false, systemTime() - openTime);
264 }
265
Michael Chockc0ec5e22014-01-27 08:14:33 -0800266 LOG_ALWAYS_FATAL_IF(!cnx->libEgl,
267 "couldn't load system EGL wrapper libraries");
268
Jesse Hallc07b5202013-07-04 12:08:16 -0700269 LOG_ALWAYS_FATAL_IF(!cnx->libGles2 || !cnx->libGles1,
270 "couldn't load system OpenGL ES wrapper libraries");
271
Yiwei Zhangd9861812019-02-13 11:51:55 -0800272 android::GraphicsEnv::getInstance().setDriverLoaded(android::GraphicsEnv::Api::API_GL, true,
273 systemTime() - openTime);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800274
Mathias Agopiande586972009-05-28 17:39:03 -0700275 return (void*)hnd;
276}
277
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600278void Loader::close(egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700279{
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600280 driver_t* hnd = (driver_t*) cnx->dso;
Mathias Agopiande586972009-05-28 17:39:03 -0700281 delete hnd;
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600282 cnx->dso = nullptr;
283
Tim Van Patten5f744f12018-12-12 11:46:21 -0700284 cnx->shouldUseAngle = false;
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600285 cnx->angleDecided = false;
286 cnx->useAngle = false;
287
288 if (cnx->vendorEGL) {
289 dlclose(cnx->vendorEGL);
290 cnx->vendorEGL = nullptr;
291 }
Mathias Agopiande586972009-05-28 17:39:03 -0700292}
293
Jesse Hall94cdba92013-07-11 09:40:35 -0700294void Loader::init_api(void* dso,
295 char const * const * api,
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700296 char const * const * ref_api,
Jesse Hall94cdba92013-07-11 09:40:35 -0700297 __eglMustCastToProperFunctionPointerType* curr,
298 getProcAddressType getProcAddress)
Mathias Agopiande586972009-05-28 17:39:03 -0700299{
Jesse Hall1508ae62017-01-19 17:43:26 -0800300 ATRACE_CALL();
301
Mathias Agopian7773c432012-02-13 20:06:08 -0800302 const ssize_t SIZE = 256;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700303 char scrap[SIZE];
Mathias Agopiande586972009-05-28 17:39:03 -0700304 while (*api) {
305 char const * name = *api;
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700306 if (ref_api) {
307 char const * ref_name = *ref_api;
308 if (std::strcmp(name, ref_name) != 0) {
309 *curr++ = nullptr;
310 ref_api++;
311 continue;
312 }
313 }
314
Jesse Hall94cdba92013-07-11 09:40:35 -0700315 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700316 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700317 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700318 // couldn't find the entry-point, use eglGetProcAddress()
319 f = getProcAddress(name);
320 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700321 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700322 // Try without the OES postfix
323 ssize_t index = ssize_t(strlen(name)) - 3;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700324 if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) {
Mathias Agopiande586972009-05-28 17:39:03 -0700325 strncpy(scrap, name, index);
326 scrap[index] = 0;
327 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000328 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700329 }
330 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700331 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700332 // Try with the OES postfix
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700333 ssize_t index = ssize_t(strlen(name)) - 3;
334 if (index>0 && strcmp(name+index, "OES")) {
335 snprintf(scrap, SIZE, "%sOES", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700336 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000337 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700338 }
339 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700340 if (f == nullptr) {
Steve Block9d453682011-12-20 16:23:08 +0000341 //ALOGD("%s", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700342 f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented;
Mathias Agopian48d438d2012-01-28 21:44:00 -0800343
344 /*
345 * GL_EXT_debug_label is special, we always report it as
346 * supported, it's handled by GLES_trace. If GLES_trace is not
347 * enabled, then these are no-ops.
348 */
349 if (!strcmp(name, "glInsertEventMarkerEXT")) {
350 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
351 } else if (!strcmp(name, "glPushGroupMarkerEXT")) {
352 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
353 } else if (!strcmp(name, "glPopGroupMarkerEXT")) {
354 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
355 }
Mathias Agopiande586972009-05-28 17:39:03 -0700356 }
357 *curr++ = f;
358 api++;
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700359 if (ref_api) ref_api++;
Mathias Agopiande586972009-05-28 17:39:03 -0700360 }
361}
362
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800363static void* load_system_driver(const char* kind) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800364 ATRACE_CALL();
Mathias Agopian99381422013-04-23 20:52:29 +0200365 class MatchFile {
366 public:
Mathias Agopian65421432017-03-08 11:49:05 -0800367 static std::string find(const char* kind) {
368 std::string result;
Nicolas Capens776951f2015-11-06 10:10:21 -0500369 int emulationStatus = checkGlesEmulationStatus();
370 switch (emulationStatus) {
371 case 0:
Nicolas Capens776951f2015-11-06 10:10:21 -0500372#if defined(__LP64__)
bohu3adf9e12017-07-02 22:08:13 -0700373 result = "/vendor/lib64/egl/libGLES_android.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500374#else
bohu3adf9e12017-07-02 22:08:13 -0700375 result = "/vendor/lib/egl/libGLES_android.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500376#endif
377 return result;
378 case 1:
379 // Use host-side OpenGL through the "emulation" library
380#if defined(__LP64__)
bohu3adf9e12017-07-02 22:08:13 -0700381 result = std::string("/vendor/lib64/egl/lib") + kind + "_emulation.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500382#else
bohu3adf9e12017-07-02 22:08:13 -0700383 result = std::string("/vendor/lib/egl/lib") + kind + "_emulation.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500384#endif
385 return result;
Lingfeng Yang0f82d872017-08-11 01:18:00 -0700386 case 2:
387 // Use guest side swiftshader library
388#if defined(__LP64__)
389 result = std::string("/vendor/lib64/egl/lib") + kind + "_swiftshader.so";
390#else
391 result = std::string("/vendor/lib/egl/lib") + kind + "_swiftshader.so";
392#endif
393 return result;
Nicolas Capens776951f2015-11-06 10:10:21 -0500394 default:
395 // Not in emulator, or use other guest-side implementation
396 break;
397 }
398
Mathias Agopian65421432017-03-08 11:49:05 -0800399 std::string pattern = std::string("lib") + kind;
Mathias Agopian99381422013-04-23 20:52:29 +0200400 const char* const searchPaths[] = {
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800401#if defined(__LP64__)
402 "/vendor/lib64/egl",
403 "/system/lib64/egl"
404#else
Mathias Agopian99381422013-04-23 20:52:29 +0200405 "/vendor/lib/egl",
406 "/system/lib/egl"
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800407#endif
Mathias Agopian99381422013-04-23 20:52:29 +0200408 };
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700409
Mathias Agopian99381422013-04-23 20:52:29 +0200410 // first, we search for the exact name of the GLES userspace
411 // driver in both locations.
412 // i.e.:
413 // libGLES.so, or:
414 // libEGL.so, libGLESv1_CM.so, libGLESv2.so
415
416 for (size_t i=0 ; i<NELEM(searchPaths) ; i++) {
417 if (find(result, pattern, searchPaths[i], true)) {
418 return result;
419 }
420 }
421
422 // for compatibility with the old "egl.cfg" naming convention
423 // we look for files that match:
424 // libGLES_*.so, or:
425 // libEGL_*.so, libGLESv1_CM_*.so, libGLESv2_*.so
426
427 pattern.append("_");
428 for (size_t i=0 ; i<NELEM(searchPaths) ; i++) {
429 if (find(result, pattern, searchPaths[i], false)) {
430 return result;
431 }
432 }
433
434 // we didn't find the driver. gah.
435 result.clear();
436 return result;
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700437 }
Mathias Agopian99381422013-04-23 20:52:29 +0200438
439 private:
Mathias Agopian65421432017-03-08 11:49:05 -0800440 static bool find(std::string& result,
441 const std::string& pattern, const char* const search, bool exact) {
Mathias Agopian99381422013-04-23 20:52:29 +0200442 if (exact) {
Jesse Hall4aaa9602017-08-29 16:33:54 -0700443 std::string absolutePath = std::string(search) + "/" + pattern + ".so";
Mathias Agopian65421432017-03-08 11:49:05 -0800444 if (!access(absolutePath.c_str(), R_OK)) {
Mathias Agopian99381422013-04-23 20:52:29 +0200445 result = absolutePath;
446 return true;
447 }
448 return false;
449 }
450
451 DIR* d = opendir(search);
Yi Kong48a6cd22018-07-18 10:07:09 -0700452 if (d != nullptr) {
Mathias Agopian99381422013-04-23 20:52:29 +0200453 struct dirent* e;
Yi Kong48a6cd22018-07-18 10:07:09 -0700454 while ((e = readdir(d)) != nullptr) {
Mathias Agopian99381422013-04-23 20:52:29 +0200455 if (e->d_type == DT_DIR) {
456 continue;
457 }
458 if (!strcmp(e->d_name, "libGLES_android.so")) {
459 // always skip the software renderer
460 continue;
461 }
Mathias Agopian65421432017-03-08 11:49:05 -0800462 if (strstr(e->d_name, pattern.c_str()) == e->d_name) {
Mathias Agopian99381422013-04-23 20:52:29 +0200463 if (!strcmp(e->d_name + strlen(e->d_name) - 3, ".so")) {
Mathias Agopian65421432017-03-08 11:49:05 -0800464 result = std::string(search) + "/" + e->d_name;
Mathias Agopian99381422013-04-23 20:52:29 +0200465 closedir(d);
466 return true;
467 }
468 }
469 }
470 closedir(d);
471 }
472 return false;
473 }
474 };
475
476
Mathias Agopian65421432017-03-08 11:49:05 -0800477 std::string absolutePath = MatchFile::find(kind);
478 if (absolutePath.empty()) {
Mathias Agopian99381422013-04-23 20:52:29 +0200479 // this happens often, we don't want to log an error
Yi Kong48a6cd22018-07-18 10:07:09 -0700480 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700481 }
Mathias Agopian65421432017-03-08 11:49:05 -0800482 const char* const driver_absolute_path = absolutePath.c_str();
Mathias Agopiande586972009-05-28 17:39:03 -0700483
Jiyong Park5910dc72017-04-05 14:23:52 +0900484 // Try to load drivers from the 'sphal' namespace, if it exist. Fall back to
Justin Yunb7320302017-05-22 15:13:40 +0900485 // the original routine when the namespace does not exist.
Jiyong Park5910dc72017-04-05 14:23:52 +0900486 // See /system/core/rootdir/etc/ld.config.txt for the configuration of the
487 // sphal namespace.
Justin Yunb7320302017-05-22 15:13:40 +0900488 void* dso = do_android_load_sphal_library(driver_absolute_path,
489 RTLD_NOW | RTLD_LOCAL);
Yi Kong48a6cd22018-07-18 10:07:09 -0700490 if (dso == nullptr) {
Mathias Agopian8c173842009-09-20 16:01:02 -0700491 const char* err = dlerror();
Mathias Agopian65421432017-03-08 11:49:05 -0800492 ALOGE("load_driver(%s): %s", driver_absolute_path, err ? err : "unknown");
Yi Kong48a6cd22018-07-18 10:07:09 -0700493 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700494 }
495
Steve Block9d453682011-12-20 16:23:08 +0000496 ALOGD("loaded %s", driver_absolute_path);
Mathias Agopianbaca89c2009-08-20 19:09:34 -0700497
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800498 return dso;
499}
500
Cody Northrop1f00e172018-04-02 11:23:31 -0600501static void* load_angle_from_namespace(const char* kind, android_namespace_t* ns) {
502 const android_dlextinfo dlextinfo = {
503 .flags = ANDROID_DLEXT_USE_NAMESPACE,
504 .library_namespace = ns,
505 };
506
507 std::string name = std::string("lib") + kind + "_angle.so";
508
509 void* so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
510
511 if (so) {
512 ALOGD("dlopen_ext from APK (%s) success at %p", name.c_str(), so);
513 return so;
514 } else {
515 ALOGE("dlopen_ext(\"%s\") failed: %s", name.c_str(), dlerror());
516 }
517
518 return nullptr;
519}
520
Cody Northrop6d082ef2018-09-11 09:42:31 -0600521static void* load_angle(const char* kind, android_namespace_t* ns, egl_connection_t* cnx) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600522 // Only attempt to load ANGLE libs
Tim Van Patten5f744f12018-12-12 11:46:21 -0700523 if (strcmp(kind, "EGL") != 0 && strcmp(kind, "GLESv2") != 0 && strcmp(kind, "GLESv1_CM") != 0) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600524 return nullptr;
Cody Northrop1f00e172018-04-02 11:23:31 -0600525 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700526
Courtney Goeltzenleuchter985bc282018-11-02 14:24:55 -0600527 void* so = nullptr;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700528
529 if ((cnx->shouldUseAngle) || android::GraphicsEnv::getInstance().shouldUseAngle()) {
Ian Elliott34474472018-09-05 10:57:07 -0600530 so = load_angle_from_namespace(kind, ns);
Tim Van Patten5f744f12018-12-12 11:46:21 -0700531 cnx->shouldUseAngle = true;
532 } else {
533 cnx->shouldUseAngle = false;
Ian Elliott34474472018-09-05 10:57:07 -0600534 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600535
536 if (so) {
Tim Van Patten5f744f12018-12-12 11:46:21 -0700537 ALOGV("Loaded ANGLE %s library for '%s' (instead of native)", kind,
538 android::GraphicsEnv::getInstance().getAngleAppName().c_str());
Cody Northrop1f00e172018-04-02 11:23:31 -0600539 cnx->useAngle = true;
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600540
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600541 char prop[PROPERTY_VALUE_MAX];
Tim Van Patten5f744f12018-12-12 11:46:21 -0700542
543 property_get("debug.hwui.renderer", prop, "UNSET");
544 ALOGV("Skia's renderer set to %s", prop);
545
546 EGLint angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE;
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600547 property_get("debug.angle.backend", prop, "0");
548 switch (atoi(prop)) {
549 case 1:
550 ALOGV("%s: Requesting OpenGLES back-end", __FUNCTION__);
551 angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE;
552 break;
553 case 2:
554 ALOGV("%s: Requesting Vulkan back-end", __FUNCTION__);
555 angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE;
556 break;
557 default:
558 break;
559 }
560
561 cnx->angleBackend = angleBackendDefault;
562 if (!cnx->vendorEGL && (cnx->angleBackend == EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE)) {
563 // Find and load vendor libEGL for ANGLE's GL back-end to use.
Cody Northrop1f00e172018-04-02 11:23:31 -0600564 cnx->vendorEGL = load_system_driver("EGL");
565 }
Ian Elliottb058aff2018-08-09 12:00:55 -0600566 } else {
Tim Van Patten5f744f12018-12-12 11:46:21 -0700567 ALOGV("Loaded native %s library for '%s' (instead of ANGLE)", kind,
568 android::GraphicsEnv::getInstance().getAngleAppName().c_str());
569 cnx->useAngle = false;
Cody Northrop1f00e172018-04-02 11:23:31 -0600570 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700571 cnx->angleDecided = true;
Cody Northrop1f00e172018-04-02 11:23:31 -0600572
Tim Van Patten5f744f12018-12-12 11:46:21 -0700573 return so;
Cody Northrop1f00e172018-04-02 11:23:31 -0600574}
575
Mathias Agopian311b4792017-02-28 15:00:49 -0800576static const char* HAL_SUBNAME_KEY_PROPERTIES[2] = {
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800577 "ro.hardware.egl",
578 "ro.board.platform",
Mathias Agopian311b4792017-02-28 15:00:49 -0800579};
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800580
581static void* load_updated_driver(const char* kind, android_namespace_t* ns) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800582 ATRACE_CALL();
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800583 const android_dlextinfo dlextinfo = {
584 .flags = ANDROID_DLEXT_USE_NAMESPACE,
585 .library_namespace = ns,
586 };
587 void* so = nullptr;
588 char prop[PROPERTY_VALUE_MAX + 1];
589 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
590 if (property_get(key, prop, nullptr) > 0) {
Mathias Agopian65421432017-03-08 11:49:05 -0800591 std::string name = std::string("lib") + kind + "_" + prop + ".so";
592 so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
Mathias Agopian311b4792017-02-28 15:00:49 -0800593 if (so) {
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800594 return so;
Mathias Agopian311b4792017-02-28 15:00:49 -0800595 }
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800596 }
597 }
598 return nullptr;
599}
600
601void *Loader::load_driver(const char* kind,
602 egl_connection_t* cnx, uint32_t mask)
603{
Jesse Hall1508ae62017-01-19 17:43:26 -0800604 ATRACE_CALL();
605
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800606 void* dso = nullptr;
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600607 android_namespace_t* ns = android::GraphicsEnv::getInstance().getAngleNamespace();
Cody Northrop6d082ef2018-09-11 09:42:31 -0600608 if (ns) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800609 android::GraphicsEnv::getInstance().setDriverToLoad(android::GraphicsEnv::Driver::ANGLE);
Cody Northrop6d082ef2018-09-11 09:42:31 -0600610 dso = load_angle(kind, ns, cnx);
611 }
Jiyong Parka243e5d2017-06-21 12:26:51 +0900612#ifndef __ANDROID_VNDK__
Cody Northrop1f00e172018-04-02 11:23:31 -0600613 if (!dso) {
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600614 android_namespace_t* ns = android::GraphicsEnv::getInstance().getDriverNamespace();
Cody Northrop1f00e172018-04-02 11:23:31 -0600615 if (ns) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800616 android::GraphicsEnv::getInstance().setDriverToLoad(
617 android::GraphicsEnv::Driver::GL_UPDATED);
Cody Northrop1f00e172018-04-02 11:23:31 -0600618 dso = load_updated_driver(kind, ns);
619 }
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800620 }
Jiyong Parka243e5d2017-06-21 12:26:51 +0900621#endif
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800622 if (!dso) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800623 android::GraphicsEnv::getInstance().setDriverToLoad(android::GraphicsEnv::Driver::GL);
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800624 dso = load_system_driver(kind);
625 if (!dso)
Yi Kong48a6cd22018-07-18 10:07:09 -0700626 return nullptr;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800627 }
628
Mathias Agopiande586972009-05-28 17:39:03 -0700629 if (mask & EGL) {
630 getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress");
631
Jesse Hall94cdba92013-07-11 09:40:35 -0700632 ALOGE_IF(!getProcAddress,
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800633 "can't find eglGetProcAddress() in EGL driver library");
Mathias Agopiande586972009-05-28 17:39:03 -0700634
Mathias Agopian618fa102009-10-14 02:06:37 -0700635 egl_t* egl = &cnx->egl;
Mathias Agopiande586972009-05-28 17:39:03 -0700636 __eglMustCastToProperFunctionPointerType* curr =
637 (__eglMustCastToProperFunctionPointerType*)egl;
638 char const * const * api = egl_names;
639 while (*api) {
640 char const * name = *api;
Jesse Hall94cdba92013-07-11 09:40:35 -0700641 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700642 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700643 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700644 // couldn't find the entry-point, use eglGetProcAddress()
645 f = getProcAddress(name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700646 if (f == nullptr) {
647 f = (__eglMustCastToProperFunctionPointerType)nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700648 }
649 }
650 *curr++ = f;
651 api++;
652 }
653 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700654
Mathias Agopiande586972009-05-28 17:39:03 -0700655 if (mask & GLESv1_CM) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700656 init_api(dso, gl_names_1, gl_names,
Mathias Agopian618fa102009-10-14 02:06:37 -0700657 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800658 &cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl,
Mathias Agopian618fa102009-10-14 02:06:37 -0700659 getProcAddress);
Mathias Agopiande586972009-05-28 17:39:03 -0700660 }
661
662 if (mask & GLESv2) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700663 init_api(dso, gl_names, nullptr,
Mathias Agopian618fa102009-10-14 02:06:37 -0700664 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800665 &cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl,
Mathias Agopiande586972009-05-28 17:39:03 -0700666 getProcAddress);
667 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700668
Mathias Agopiande586972009-05-28 17:39:03 -0700669 return dso;
670}
671
672// ----------------------------------------------------------------------------
673}; // namespace android
674// ----------------------------------------------------------------------------