blob: c1805bede8ca0198680220da60c5ce6206e67c01 [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
Peiyong Line83b8682019-04-18 17:23:02 -0700363static void* attempt_to_load_emulation_driver(const char* kind) {
364 const int emulationStatus = checkGlesEmulationStatus();
365
366 // Invalid emulation status, abort.
367 if (emulationStatus < 0 || emulationStatus > 2) {
368 return nullptr;
369 }
370
371 std::string absolutePath;
372 switch (emulationStatus) {
373 case 0:
374#if defined(__LP64__)
375 absolutePath = "/vendor/lib64/egl/libGLES_android.so";
376#else
377 absolutePath = "/vendor/lib/egl/libGLES_android.so";
378#endif
379 break;
380 case 1:
381 // Use host-side OpenGL through the "emulation" library
382#if defined(__LP64__)
383 absolutePath = std::string("/vendor/lib64/egl/lib") + kind + "_emulation.so";
384#else
385 absolutePath = std::string("/vendor/lib/egl/lib") + kind + "_emulation.so";
386#endif
387 break;
388 case 2:
389 // Use guest side swiftshader library
390#if defined(__LP64__)
391 absolutePath = std::string("/vendor/lib64/egl/lib") + kind + "_swiftshader.so";
392#else
393 absolutePath = std::string("/vendor/lib/egl/lib") + kind + "_swiftshader.so";
394#endif
395 break;
396 default:
397 // Not in emulator, or use other guest-side implementation
398 break;
399 }
400 if (absolutePath.empty()) {
401 // this happens often, we don't want to log an error
402 return nullptr;
403 }
404 const char* const driver_absolute_path = absolutePath.c_str();
405
406 // Try to load drivers from the 'sphal' namespace, if it exist. Fall back to
407 // the original routine when the namespace does not exist.
408 // See /system/core/rootdir/etc/ld.config.txt for the configuration of the
409 // sphal namespace.
410 void* dso = do_android_load_sphal_library(driver_absolute_path,
411 RTLD_NOW | RTLD_LOCAL);
412 if (dso == nullptr) {
413 const char* err = dlerror();
414 ALOGE("load_driver(%s): %s", driver_absolute_path, err ? err : "unknown");
415 return nullptr;
416 }
417 ALOGD("loaded %s", driver_absolute_path);
418 return dso;
419}
420
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800421static void* load_system_driver(const char* kind) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800422 ATRACE_CALL();
Mathias Agopian99381422013-04-23 20:52:29 +0200423 class MatchFile {
424 public:
Mathias Agopian65421432017-03-08 11:49:05 -0800425 static std::string find(const char* kind) {
426 std::string result;
Mathias Agopian65421432017-03-08 11:49:05 -0800427 std::string pattern = std::string("lib") + kind;
Mathias Agopian99381422013-04-23 20:52:29 +0200428 const char* const searchPaths[] = {
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800429#if defined(__LP64__)
430 "/vendor/lib64/egl",
431 "/system/lib64/egl"
432#else
Mathias Agopian99381422013-04-23 20:52:29 +0200433 "/vendor/lib/egl",
434 "/system/lib/egl"
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800435#endif
Mathias Agopian99381422013-04-23 20:52:29 +0200436 };
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700437
Mathias Agopian99381422013-04-23 20:52:29 +0200438 // first, we search for the exact name of the GLES userspace
439 // driver in both locations.
440 // i.e.:
441 // libGLES.so, or:
442 // libEGL.so, libGLESv1_CM.so, libGLESv2.so
443
444 for (size_t i=0 ; i<NELEM(searchPaths) ; i++) {
445 if (find(result, pattern, searchPaths[i], true)) {
446 return result;
447 }
448 }
449
450 // for compatibility with the old "egl.cfg" naming convention
451 // we look for files that match:
452 // libGLES_*.so, or:
453 // libEGL_*.so, libGLESv1_CM_*.so, libGLESv2_*.so
454
455 pattern.append("_");
456 for (size_t i=0 ; i<NELEM(searchPaths) ; i++) {
457 if (find(result, pattern, searchPaths[i], false)) {
458 return result;
459 }
460 }
461
462 // we didn't find the driver. gah.
463 result.clear();
464 return result;
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700465 }
Mathias Agopian99381422013-04-23 20:52:29 +0200466
467 private:
Mathias Agopian65421432017-03-08 11:49:05 -0800468 static bool find(std::string& result,
469 const std::string& pattern, const char* const search, bool exact) {
Mathias Agopian99381422013-04-23 20:52:29 +0200470 if (exact) {
Jesse Hall4aaa9602017-08-29 16:33:54 -0700471 std::string absolutePath = std::string(search) + "/" + pattern + ".so";
Mathias Agopian65421432017-03-08 11:49:05 -0800472 if (!access(absolutePath.c_str(), R_OK)) {
Mathias Agopian99381422013-04-23 20:52:29 +0200473 result = absolutePath;
474 return true;
475 }
476 return false;
477 }
478
479 DIR* d = opendir(search);
Yi Kong48a6cd22018-07-18 10:07:09 -0700480 if (d != nullptr) {
Mathias Agopian99381422013-04-23 20:52:29 +0200481 struct dirent* e;
Yi Kong48a6cd22018-07-18 10:07:09 -0700482 while ((e = readdir(d)) != nullptr) {
Mathias Agopian99381422013-04-23 20:52:29 +0200483 if (e->d_type == DT_DIR) {
484 continue;
485 }
486 if (!strcmp(e->d_name, "libGLES_android.so")) {
487 // always skip the software renderer
488 continue;
489 }
Mathias Agopian65421432017-03-08 11:49:05 -0800490 if (strstr(e->d_name, pattern.c_str()) == e->d_name) {
Mathias Agopian99381422013-04-23 20:52:29 +0200491 if (!strcmp(e->d_name + strlen(e->d_name) - 3, ".so")) {
Mathias Agopian65421432017-03-08 11:49:05 -0800492 result = std::string(search) + "/" + e->d_name;
Mathias Agopian99381422013-04-23 20:52:29 +0200493 closedir(d);
494 return true;
495 }
496 }
497 }
498 closedir(d);
499 }
500 return false;
501 }
502 };
503
504
Mathias Agopian65421432017-03-08 11:49:05 -0800505 std::string absolutePath = MatchFile::find(kind);
506 if (absolutePath.empty()) {
Mathias Agopian99381422013-04-23 20:52:29 +0200507 // this happens often, we don't want to log an error
Yi Kong48a6cd22018-07-18 10:07:09 -0700508 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700509 }
Mathias Agopian65421432017-03-08 11:49:05 -0800510 const char* const driver_absolute_path = absolutePath.c_str();
Mathias Agopiande586972009-05-28 17:39:03 -0700511
Jiyong Park5910dc72017-04-05 14:23:52 +0900512 // Try to load drivers from the 'sphal' namespace, if it exist. Fall back to
Justin Yunb7320302017-05-22 15:13:40 +0900513 // the original routine when the namespace does not exist.
Jiyong Park5910dc72017-04-05 14:23:52 +0900514 // See /system/core/rootdir/etc/ld.config.txt for the configuration of the
515 // sphal namespace.
Justin Yunb7320302017-05-22 15:13:40 +0900516 void* dso = do_android_load_sphal_library(driver_absolute_path,
517 RTLD_NOW | RTLD_LOCAL);
Yi Kong48a6cd22018-07-18 10:07:09 -0700518 if (dso == nullptr) {
Mathias Agopian8c173842009-09-20 16:01:02 -0700519 const char* err = dlerror();
Mathias Agopian65421432017-03-08 11:49:05 -0800520 ALOGE("load_driver(%s): %s", driver_absolute_path, err ? err : "unknown");
Yi Kong48a6cd22018-07-18 10:07:09 -0700521 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700522 }
523
Steve Block9d453682011-12-20 16:23:08 +0000524 ALOGD("loaded %s", driver_absolute_path);
Mathias Agopianbaca89c2009-08-20 19:09:34 -0700525
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800526 return dso;
527}
528
Cody Northrop1f00e172018-04-02 11:23:31 -0600529static void* load_angle_from_namespace(const char* kind, android_namespace_t* ns) {
530 const android_dlextinfo dlextinfo = {
531 .flags = ANDROID_DLEXT_USE_NAMESPACE,
532 .library_namespace = ns,
533 };
534
535 std::string name = std::string("lib") + kind + "_angle.so";
536
537 void* so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
538
539 if (so) {
540 ALOGD("dlopen_ext from APK (%s) success at %p", name.c_str(), so);
541 return so;
542 } else {
543 ALOGE("dlopen_ext(\"%s\") failed: %s", name.c_str(), dlerror());
544 }
545
546 return nullptr;
547}
548
Cody Northrop6d082ef2018-09-11 09:42:31 -0600549static void* load_angle(const char* kind, android_namespace_t* ns, egl_connection_t* cnx) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600550 // Only attempt to load ANGLE libs
Tim Van Patten5f744f12018-12-12 11:46:21 -0700551 if (strcmp(kind, "EGL") != 0 && strcmp(kind, "GLESv2") != 0 && strcmp(kind, "GLESv1_CM") != 0) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600552 return nullptr;
Cody Northrop1f00e172018-04-02 11:23:31 -0600553 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700554
Courtney Goeltzenleuchter985bc282018-11-02 14:24:55 -0600555 void* so = nullptr;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700556
557 if ((cnx->shouldUseAngle) || android::GraphicsEnv::getInstance().shouldUseAngle()) {
Ian Elliott34474472018-09-05 10:57:07 -0600558 so = load_angle_from_namespace(kind, ns);
Tim Van Patten5f744f12018-12-12 11:46:21 -0700559 cnx->shouldUseAngle = true;
560 } else {
561 cnx->shouldUseAngle = false;
Ian Elliott34474472018-09-05 10:57:07 -0600562 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600563
564 if (so) {
Tim Van Patten5f744f12018-12-12 11:46:21 -0700565 ALOGV("Loaded ANGLE %s library for '%s' (instead of native)", kind,
566 android::GraphicsEnv::getInstance().getAngleAppName().c_str());
Cody Northrop1f00e172018-04-02 11:23:31 -0600567 cnx->useAngle = true;
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600568
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600569 char prop[PROPERTY_VALUE_MAX];
Tim Van Patten5f744f12018-12-12 11:46:21 -0700570
571 property_get("debug.hwui.renderer", prop, "UNSET");
572 ALOGV("Skia's renderer set to %s", prop);
573
574 EGLint angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE;
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600575 property_get("debug.angle.backend", prop, "0");
576 switch (atoi(prop)) {
577 case 1:
578 ALOGV("%s: Requesting OpenGLES back-end", __FUNCTION__);
579 angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE;
580 break;
581 case 2:
582 ALOGV("%s: Requesting Vulkan back-end", __FUNCTION__);
583 angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE;
584 break;
585 default:
586 break;
587 }
588
589 cnx->angleBackend = angleBackendDefault;
590 if (!cnx->vendorEGL && (cnx->angleBackend == EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE)) {
591 // Find and load vendor libEGL for ANGLE's GL back-end to use.
Cody Northrop1f00e172018-04-02 11:23:31 -0600592 cnx->vendorEGL = load_system_driver("EGL");
593 }
Ian Elliottb058aff2018-08-09 12:00:55 -0600594 } else {
Tim Van Patten5f744f12018-12-12 11:46:21 -0700595 ALOGV("Loaded native %s library for '%s' (instead of ANGLE)", kind,
596 android::GraphicsEnv::getInstance().getAngleAppName().c_str());
597 cnx->useAngle = false;
Cody Northrop1f00e172018-04-02 11:23:31 -0600598 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700599 cnx->angleDecided = true;
Cody Northrop1f00e172018-04-02 11:23:31 -0600600
Tim Van Patten5f744f12018-12-12 11:46:21 -0700601 return so;
Cody Northrop1f00e172018-04-02 11:23:31 -0600602}
603
Mathias Agopian311b4792017-02-28 15:00:49 -0800604static const char* HAL_SUBNAME_KEY_PROPERTIES[2] = {
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800605 "ro.hardware.egl",
606 "ro.board.platform",
Mathias Agopian311b4792017-02-28 15:00:49 -0800607};
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800608
609static void* load_updated_driver(const char* kind, android_namespace_t* ns) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800610 ATRACE_CALL();
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800611 const android_dlextinfo dlextinfo = {
612 .flags = ANDROID_DLEXT_USE_NAMESPACE,
613 .library_namespace = ns,
614 };
615 void* so = nullptr;
616 char prop[PROPERTY_VALUE_MAX + 1];
617 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
618 if (property_get(key, prop, nullptr) > 0) {
Mathias Agopian65421432017-03-08 11:49:05 -0800619 std::string name = std::string("lib") + kind + "_" + prop + ".so";
620 so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
Mathias Agopian311b4792017-02-28 15:00:49 -0800621 if (so) {
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800622 return so;
Mathias Agopian311b4792017-02-28 15:00:49 -0800623 }
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800624 }
625 }
626 return nullptr;
627}
628
629void *Loader::load_driver(const char* kind,
630 egl_connection_t* cnx, uint32_t mask)
631{
Jesse Hall1508ae62017-01-19 17:43:26 -0800632 ATRACE_CALL();
633
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800634 void* dso = nullptr;
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600635 android_namespace_t* ns = android::GraphicsEnv::getInstance().getAngleNamespace();
Cody Northrop6d082ef2018-09-11 09:42:31 -0600636 if (ns) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800637 android::GraphicsEnv::getInstance().setDriverToLoad(android::GraphicsEnv::Driver::ANGLE);
Cody Northrop6d082ef2018-09-11 09:42:31 -0600638 dso = load_angle(kind, ns, cnx);
Peiyong Line83b8682019-04-18 17:23:02 -0700639 if (dso) {
640 initialize_api(dso, cnx, mask);
641 return dso;
642 }
Cody Northrop6d082ef2018-09-11 09:42:31 -0600643 }
Jiyong Parka243e5d2017-06-21 12:26:51 +0900644#ifndef __ANDROID_VNDK__
Peiyong Line83b8682019-04-18 17:23:02 -0700645 ns = android::GraphicsEnv::getInstance().getDriverNamespace();
646 if (ns) {
647 android::GraphicsEnv::getInstance().setDriverToLoad(android::GraphicsEnv::Driver::GL_UPDATED);
648 dso = load_updated_driver(kind, ns);
649 if (dso) {
650 initialize_api(dso, cnx, mask);
651 return dso;
Cody Northrop1f00e172018-04-02 11:23:31 -0600652 }
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800653 }
Jiyong Parka243e5d2017-06-21 12:26:51 +0900654#endif
Peiyong Line83b8682019-04-18 17:23:02 -0700655 android::GraphicsEnv::getInstance().setDriverToLoad(android::GraphicsEnv::Driver::GL);
656 dso = attempt_to_load_emulation_driver(kind);
657 if (dso) {
658 initialize_api(dso, cnx, mask);
659 return dso;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800660 }
661
Peiyong Line83b8682019-04-18 17:23:02 -0700662 dso = load_system_driver(kind);
663 if (dso) {
664 initialize_api(dso, cnx, mask);
665 return dso;
666 }
667
668 return nullptr;
669}
670
671void Loader::initialize_api(void* dso, egl_connection_t* cnx, uint32_t mask) {
Mathias Agopiande586972009-05-28 17:39:03 -0700672 if (mask & EGL) {
673 getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress");
674
Jesse Hall94cdba92013-07-11 09:40:35 -0700675 ALOGE_IF(!getProcAddress,
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800676 "can't find eglGetProcAddress() in EGL driver library");
Mathias Agopiande586972009-05-28 17:39:03 -0700677
Mathias Agopian618fa102009-10-14 02:06:37 -0700678 egl_t* egl = &cnx->egl;
Mathias Agopiande586972009-05-28 17:39:03 -0700679 __eglMustCastToProperFunctionPointerType* curr =
680 (__eglMustCastToProperFunctionPointerType*)egl;
681 char const * const * api = egl_names;
682 while (*api) {
683 char const * name = *api;
Jesse Hall94cdba92013-07-11 09:40:35 -0700684 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700685 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700686 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700687 // couldn't find the entry-point, use eglGetProcAddress()
688 f = getProcAddress(name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700689 if (f == nullptr) {
690 f = (__eglMustCastToProperFunctionPointerType)nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700691 }
692 }
693 *curr++ = f;
694 api++;
695 }
696 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700697
Mathias Agopiande586972009-05-28 17:39:03 -0700698 if (mask & GLESv1_CM) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700699 init_api(dso, gl_names_1, gl_names,
Mathias Agopian618fa102009-10-14 02:06:37 -0700700 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800701 &cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl,
Mathias Agopian618fa102009-10-14 02:06:37 -0700702 getProcAddress);
Mathias Agopiande586972009-05-28 17:39:03 -0700703 }
704
705 if (mask & GLESv2) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700706 init_api(dso, gl_names, nullptr,
Mathias Agopian618fa102009-10-14 02:06:37 -0700707 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800708 &cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl,
Mathias Agopiande586972009-05-28 17:39:03 -0700709 getProcAddress);
710 }
Mathias Agopiande586972009-05-28 17:39:03 -0700711}
712
713// ----------------------------------------------------------------------------
714}; // namespace android
715// ----------------------------------------------------------------------------