blob: 39bf329e816231b55eb300317e8a3c3b5254420c [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>
Charlie Lao840bd532020-01-16 17:34:37 -080028#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 *
Mathias Agopian99381422013-04-23 20:52:29 +020054 * For backward compatibility and to facilitate the transition to
55 * this new naming scheme, the loader will additionally look for:
Jesse Hall94cdba92013-07-11 09:40:35 -070056 *
Mathias Agopian99381422013-04-23 20:52:29 +020057 * /{vendor|system}/lib/egl/lib{GLES | [EGL|GLESv1_CM|GLESv2]}_*.so
Jesse Hall94cdba92013-07-11 09:40:35 -070058 *
Mathias Agopiande586972009-05-28 17:39:03 -070059 */
60
Mathias Agopian65421432017-03-08 11:49:05 -080061Loader& Loader::getInstance() {
62 static Loader loader;
63 return loader;
64}
Mathias Agopiande586972009-05-28 17:39:03 -070065
Jesse Hall1508ae62017-01-19 17:43:26 -080066static void* do_dlopen(const char* path, int mode) {
67 ATRACE_CALL();
68 return dlopen(path, mode);
69}
70
Jiyong Park5910dc72017-04-05 14:23:52 +090071static void* do_android_dlopen_ext(const char* path, int mode, const android_dlextinfo* info) {
72 ATRACE_CALL();
73 return android_dlopen_ext(path, mode, info);
74}
75
Justin Yunb7320302017-05-22 15:13:40 +090076static void* do_android_load_sphal_library(const char* path, int mode) {
77 ATRACE_CALL();
78 return android_load_sphal_library(path, mode);
79}
80
Yiwei Zhang5e21eb32019-06-05 00:26:03 -070081static int do_android_unload_sphal_library(void* dso) {
82 ATRACE_CALL();
83 return android_unload_sphal_library(dso);
84}
85
Jesse Hall94cdba92013-07-11 09:40:35 -070086Loader::driver_t::driver_t(void* gles)
Mathias Agopiande586972009-05-28 17:39:03 -070087{
88 dso[0] = gles;
89 for (size_t i=1 ; i<NELEM(dso) ; i++)
Yi Kong48a6cd22018-07-18 10:07:09 -070090 dso[i] = nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -070091}
92
Jesse Hall94cdba92013-07-11 09:40:35 -070093Loader::driver_t::~driver_t()
Mathias Agopiande586972009-05-28 17:39:03 -070094{
95 for (size_t i=0 ; i<NELEM(dso) ; i++) {
96 if (dso[i]) {
97 dlclose(dso[i]);
Yi Kong48a6cd22018-07-18 10:07:09 -070098 dso[i] = nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -070099 }
100 }
101}
102
Mathias Agopian65421432017-03-08 11:49:05 -0800103int Loader::driver_t::set(void* hnd, int32_t api)
Mathias Agopiande586972009-05-28 17:39:03 -0700104{
105 switch (api) {
106 case EGL:
107 dso[0] = hnd;
108 break;
109 case GLESv1_CM:
110 dso[1] = hnd;
111 break;
112 case GLESv2:
113 dso[2] = hnd;
114 break;
115 default:
Mathias Agopian65421432017-03-08 11:49:05 -0800116 return -EOVERFLOW;
Mathias Agopiande586972009-05-28 17:39:03 -0700117 }
Mathias Agopian65421432017-03-08 11:49:05 -0800118 return 0;
Mathias Agopiande586972009-05-28 17:39:03 -0700119}
120
Mathias Agopiande586972009-05-28 17:39:03 -0700121Loader::Loader()
Yi Kong48a6cd22018-07-18 10:07:09 -0700122 : getProcAddress(nullptr)
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800123{
Mathias Agopiande586972009-05-28 17:39:03 -0700124}
125
Mathias Agopian99381422013-04-23 20:52:29 +0200126Loader::~Loader() {
Mathias Agopiande586972009-05-28 17:39:03 -0700127}
128
Jesse Hallc07b5202013-07-04 12:08:16 -0700129static void* load_wrapper(const char* path) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800130 void* so = do_dlopen(path, RTLD_NOW | RTLD_LOCAL);
Jesse Hallc07b5202013-07-04 12:08:16 -0700131 ALOGE_IF(!so, "dlopen(\"%s\") failed: %s", path, dlerror());
132 return so;
133}
134
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700135#ifndef EGL_WRAPPER_DIR
136#if defined(__LP64__)
137#define EGL_WRAPPER_DIR "/system/lib64"
138#else
139#define EGL_WRAPPER_DIR "/system/lib"
140#endif
141#endif
142
Peiyong Lin0d10b252019-04-30 18:03:04 -0700143static const char* DRIVER_SUFFIX_PROPERTY = "ro.hardware.egl";
144
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700145static const char* HAL_SUBNAME_KEY_PROPERTIES[2] = {
Peiyong Lin0d10b252019-04-30 18:03:04 -0700146 DRIVER_SUFFIX_PROPERTY,
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700147 "ro.board.platform",
148};
149
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700150static bool should_unload_system_driver(egl_connection_t* cnx) {
151 // Return false if the system driver has been unloaded once.
152 if (cnx->systemDriverUnloaded) {
153 return false;
154 }
155
156 // Return true if Angle namespace is set.
157 android_namespace_t* ns = android::GraphicsEnv::getInstance().getAngleNamespace();
158 if (ns) {
159 return true;
160 }
161
162#ifndef __ANDROID_VNDK__
163 // Return true if updated driver namespace is set.
164 ns = android::GraphicsEnv::getInstance().getDriverNamespace();
165 if (ns) {
166 return true;
167 }
168#endif
169
170 return false;
171}
172
173static void uninit_api(char const* const* api, __eglMustCastToProperFunctionPointerType* curr) {
174 while (*api) {
175 *curr++ = nullptr;
176 api++;
177 }
178}
179
180void Loader::unload_system_driver(egl_connection_t* cnx) {
181 ATRACE_CALL();
182
183 uninit_api(gl_names,
184 (__eglMustCastToProperFunctionPointerType*)&cnx
185 ->hooks[egl_connection_t::GLESv2_INDEX]
186 ->gl);
187 uninit_api(gl_names,
188 (__eglMustCastToProperFunctionPointerType*)&cnx
189 ->hooks[egl_connection_t::GLESv1_INDEX]
190 ->gl);
191 uninit_api(egl_names, (__eglMustCastToProperFunctionPointerType*)&cnx->egl);
192
193 if (cnx->dso) {
194 ALOGD("Unload system gl driver.");
195 driver_t* hnd = (driver_t*)cnx->dso;
196 if (hnd->dso[2]) {
197 do_android_unload_sphal_library(hnd->dso[2]);
198 }
199 if (hnd->dso[1]) {
200 do_android_unload_sphal_library(hnd->dso[1]);
201 }
202 if (hnd->dso[0]) {
203 do_android_unload_sphal_library(hnd->dso[0]);
204 }
205 cnx->dso = nullptr;
206 }
207
208 cnx->systemDriverUnloaded = true;
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
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700216 if (should_unload_system_driver(cnx)) {
217 unload_system_driver(cnx);
218 }
219
220 // If a driver has been loaded, return the driver directly.
221 if (cnx->dso) {
222 return cnx->dso;
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 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700231
232 bool failToLoadFromDriverSuffixProperty = false;
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700233 if (!hnd) {
Peiyong Lindb3ed6e2020-01-09 18:43:27 -0800234 // If updated driver apk is set but fail to load, abort here.
235 if (android::GraphicsEnv::getInstance().getDriverNamespace()) {
236 LOG_ALWAYS_FATAL("couldn't find an OpenGL ES implementation from %s",
237 android::GraphicsEnv::getInstance().getDriverPath().c_str());
238 }
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700239 // Finally, try to load system driver, start by searching for the library name appended by
240 // the system properties of the GLES userspace driver in both locations.
241 // i.e.:
242 // libGLES_${prop}.so, or:
243 // libEGL_${prop}.so, libGLESv1_CM_${prop}.so, libGLESv2_${prop}.so
Charlie Lao840bd532020-01-16 17:34:37 -0800244 char prop[PROPERTY_VALUE_MAX + 1];
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700245 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
Charlie Lao840bd532020-01-16 17:34:37 -0800246 if (property_get(key, prop, nullptr) <= 0) {
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700247 continue;
248 }
Charlie Lao840bd532020-01-16 17:34:37 -0800249 hnd = attempt_to_load_system_driver(cnx, prop, true);
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700250 if (hnd) {
251 break;
Charlie Lao840bd532020-01-16 17:34:37 -0800252 } else if (strcmp(key, DRIVER_SUFFIX_PROPERTY) == 0) {
253 failToLoadFromDriverSuffixProperty = true;
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700254 }
255 }
256 }
257
258 if (!hnd) {
259 // Can't find graphics driver by appending system properties, now search for the exact name
260 // without any suffix of the GLES userspace driver in both locations.
261 // i.e.:
262 // libGLES.so, or:
263 // libEGL.so, libGLESv1_CM.so, libGLESv2.so
Charlie Lao840bd532020-01-16 17:34:37 -0800264 hnd = attempt_to_load_system_driver(cnx, nullptr, true);
Peiyong Lin0d10b252019-04-30 18:03:04 -0700265 }
266
267 if (!hnd && !failToLoadFromDriverSuffixProperty) {
Charlie Lao840bd532020-01-16 17:34:37 -0800268 hnd = attempt_to_load_system_driver(cnx, nullptr, false);
Mathias Agopiande586972009-05-28 17:39:03 -0700269 }
270
Yiwei Zhangd9861812019-02-13 11:51:55 -0800271 if (!hnd) {
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700272 android::GraphicsEnv::getInstance().setDriverLoaded(android::GpuStatsInfo::Api::API_GL,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800273 false, systemTime() - openTime);
274 }
275
Peiyong Lin3575b9f2019-04-25 14:26:49 -0700276 LOG_ALWAYS_FATAL_IF(!hnd,
277 "couldn't find an OpenGL ES implementation, make sure you set %s or %s",
278 HAL_SUBNAME_KEY_PROPERTIES[0], HAL_SUBNAME_KEY_PROPERTIES[1]);
Jesse Hallc07b5202013-07-04 12:08:16 -0700279
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700280 if (!cnx->libEgl) {
281 cnx->libEgl = load_wrapper(EGL_WRAPPER_DIR "/libEGL.so");
282 }
Charlie Lao840bd532020-01-16 17:34:37 -0800283 if (!cnx->libGles1) {
284 cnx->libGles1 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv1_CM.so");
285 }
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700286 if (!cnx->libGles2) {
287 cnx->libGles2 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv2.so");
288 }
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700289
Charlie Lao840bd532020-01-16 17:34:37 -0800290 if (!cnx->libEgl || !cnx->libGles2 || !cnx->libGles1) {
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700291 android::GraphicsEnv::getInstance().setDriverLoaded(android::GpuStatsInfo::Api::API_GL,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800292 false, systemTime() - openTime);
293 }
294
Michael Chockc0ec5e22014-01-27 08:14:33 -0800295 LOG_ALWAYS_FATAL_IF(!cnx->libEgl,
296 "couldn't load system EGL wrapper libraries");
297
Charlie Lao840bd532020-01-16 17:34:37 -0800298 LOG_ALWAYS_FATAL_IF(!cnx->libGles2 || !cnx->libGles1,
299 "couldn't load system OpenGL ES wrapper libraries");
Jesse Hallc07b5202013-07-04 12:08:16 -0700300
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700301 android::GraphicsEnv::getInstance().setDriverLoaded(android::GpuStatsInfo::Api::API_GL, true,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800302 systemTime() - openTime);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800303
Mathias Agopiande586972009-05-28 17:39:03 -0700304 return (void*)hnd;
305}
306
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600307void Loader::close(egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700308{
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600309 driver_t* hnd = (driver_t*) cnx->dso;
Mathias Agopiande586972009-05-28 17:39:03 -0700310 delete hnd;
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600311 cnx->dso = nullptr;
312
Charlie Lao840bd532020-01-16 17:34:37 -0800313 cnx->useAngle = false;
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600314
315 if (cnx->vendorEGL) {
316 dlclose(cnx->vendorEGL);
317 cnx->vendorEGL = nullptr;
318 }
Mathias Agopiande586972009-05-28 17:39:03 -0700319}
320
Jesse Hall94cdba92013-07-11 09:40:35 -0700321void Loader::init_api(void* dso,
322 char const * const * api,
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700323 char const * const * ref_api,
Jesse Hall94cdba92013-07-11 09:40:35 -0700324 __eglMustCastToProperFunctionPointerType* curr,
325 getProcAddressType getProcAddress)
Mathias Agopiande586972009-05-28 17:39:03 -0700326{
Jesse Hall1508ae62017-01-19 17:43:26 -0800327 ATRACE_CALL();
328
Mathias Agopian7773c432012-02-13 20:06:08 -0800329 const ssize_t SIZE = 256;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700330 char scrap[SIZE];
Mathias Agopiande586972009-05-28 17:39:03 -0700331 while (*api) {
332 char const * name = *api;
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700333 if (ref_api) {
334 char const * ref_name = *ref_api;
335 if (std::strcmp(name, ref_name) != 0) {
336 *curr++ = nullptr;
337 ref_api++;
338 continue;
339 }
340 }
341
Jesse Hall94cdba92013-07-11 09:40:35 -0700342 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700343 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700344 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700345 // couldn't find the entry-point, use eglGetProcAddress()
346 f = getProcAddress(name);
347 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700348 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700349 // Try without the OES postfix
350 ssize_t index = ssize_t(strlen(name)) - 3;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700351 if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) {
Mathias Agopiande586972009-05-28 17:39:03 -0700352 strncpy(scrap, name, index);
353 scrap[index] = 0;
354 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000355 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700356 }
357 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700358 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700359 // Try with the OES postfix
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700360 ssize_t index = ssize_t(strlen(name)) - 3;
361 if (index>0 && strcmp(name+index, "OES")) {
362 snprintf(scrap, SIZE, "%sOES", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700363 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000364 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700365 }
366 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700367 if (f == nullptr) {
Steve Block9d453682011-12-20 16:23:08 +0000368 //ALOGD("%s", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700369 f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented;
Mathias Agopian48d438d2012-01-28 21:44:00 -0800370
371 /*
372 * GL_EXT_debug_label is special, we always report it as
373 * supported, it's handled by GLES_trace. If GLES_trace is not
374 * enabled, then these are no-ops.
375 */
376 if (!strcmp(name, "glInsertEventMarkerEXT")) {
377 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
378 } else if (!strcmp(name, "glPushGroupMarkerEXT")) {
379 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
380 } else if (!strcmp(name, "glPopGroupMarkerEXT")) {
381 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
382 }
Mathias Agopiande586972009-05-28 17:39:03 -0700383 }
384 *curr++ = f;
385 api++;
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700386 if (ref_api) ref_api++;
Mathias Agopiande586972009-05-28 17:39:03 -0700387 }
388}
389
Peiyong Lin0d10b252019-04-30 18:03:04 -0700390static void* load_system_driver(const char* kind, const char* suffix, const bool exact) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800391 ATRACE_CALL();
Mathias Agopian99381422013-04-23 20:52:29 +0200392 class MatchFile {
393 public:
Peiyong Lin0d10b252019-04-30 18:03:04 -0700394 static std::string find(const char* libraryName, const bool exact) {
Mathias Agopian99381422013-04-23 20:52:29 +0200395 const char* const searchPaths[] = {
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800396#if defined(__LP64__)
397 "/vendor/lib64/egl",
398 "/system/lib64/egl"
399#else
Mathias Agopian99381422013-04-23 20:52:29 +0200400 "/vendor/lib/egl",
401 "/system/lib/egl"
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800402#endif
Mathias Agopian99381422013-04-23 20:52:29 +0200403 };
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700404
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700405 for (auto dir : searchPaths) {
Peiyong Lin0d10b252019-04-30 18:03:04 -0700406 std::string absolutePath;
407 if (find(absolutePath, libraryName, dir, exact)) {
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700408 return absolutePath;
Mathias Agopian99381422013-04-23 20:52:29 +0200409 }
Mathias Agopian99381422013-04-23 20:52:29 +0200410 }
411
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700412 // Driver not found. gah.
413 return std::string();
Mathias Agopian99381422013-04-23 20:52:29 +0200414 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700415 private:
416 static bool find(std::string& result,
417 const std::string& pattern, const char* const search, bool exact) {
418 if (exact) {
419 std::string absolutePath = std::string(search) + "/" + pattern + ".so";
420 if (!access(absolutePath.c_str(), R_OK)) {
421 result = absolutePath;
422 return true;
423 }
424 return false;
425 }
426
427 DIR* d = opendir(search);
428 if (d != nullptr) {
429 struct dirent* e;
430 while ((e = readdir(d)) != nullptr) {
431 if (e->d_type == DT_DIR) {
432 continue;
433 }
434 if (!strcmp(e->d_name, "libGLES_android.so")) {
435 // always skip the software renderer
436 continue;
437 }
438 if (strstr(e->d_name, pattern.c_str()) == e->d_name) {
439 if (!strcmp(e->d_name + strlen(e->d_name) - 3, ".so")) {
440 result = std::string(search) + "/" + e->d_name;
441 closedir(d);
442 return true;
443 }
444 }
445 }
446 closedir(d);
447 }
448 return false;
449 }
Mathias Agopian99381422013-04-23 20:52:29 +0200450 };
451
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700452 std::string libraryName = std::string("lib") + kind;
453 if (suffix) {
454 libraryName += std::string("_") + suffix;
Peiyong Lin0d10b252019-04-30 18:03:04 -0700455 } else if (!exact) {
456 // Deprecated: we look for files that match
457 // libGLES_*.so, or:
458 // libEGL_*.so, libGLESv1_CM_*.so, libGLESv2_*.so
459 libraryName += std::string("_");
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700460 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700461 std::string absolutePath = MatchFile::find(libraryName.c_str(), exact);
Mathias Agopian65421432017-03-08 11:49:05 -0800462 if (absolutePath.empty()) {
Mathias Agopian99381422013-04-23 20:52:29 +0200463 // this happens often, we don't want to log an error
Yi Kong48a6cd22018-07-18 10:07:09 -0700464 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700465 }
Mathias Agopian65421432017-03-08 11:49:05 -0800466 const char* const driver_absolute_path = absolutePath.c_str();
Mathias Agopiande586972009-05-28 17:39:03 -0700467
Jiyong Park5910dc72017-04-05 14:23:52 +0900468 // Try to load drivers from the 'sphal' namespace, if it exist. Fall back to
Justin Yunb7320302017-05-22 15:13:40 +0900469 // the original routine when the namespace does not exist.
Jiyong Park5910dc72017-04-05 14:23:52 +0900470 // See /system/core/rootdir/etc/ld.config.txt for the configuration of the
471 // sphal namespace.
Justin Yunb7320302017-05-22 15:13:40 +0900472 void* dso = do_android_load_sphal_library(driver_absolute_path,
473 RTLD_NOW | RTLD_LOCAL);
Yi Kong48a6cd22018-07-18 10:07:09 -0700474 if (dso == nullptr) {
Mathias Agopian8c173842009-09-20 16:01:02 -0700475 const char* err = dlerror();
Mathias Agopian65421432017-03-08 11:49:05 -0800476 ALOGE("load_driver(%s): %s", driver_absolute_path, err ? err : "unknown");
Yi Kong48a6cd22018-07-18 10:07:09 -0700477 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700478 }
479
Steve Block9d453682011-12-20 16:23:08 +0000480 ALOGD("loaded %s", driver_absolute_path);
Mathias Agopianbaca89c2009-08-20 19:09:34 -0700481
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800482 return dso;
483}
484
Cody Northrop1f00e172018-04-02 11:23:31 -0600485static void* load_angle_from_namespace(const char* kind, android_namespace_t* ns) {
486 const android_dlextinfo dlextinfo = {
487 .flags = ANDROID_DLEXT_USE_NAMESPACE,
488 .library_namespace = ns,
489 };
490
491 std::string name = std::string("lib") + kind + "_angle.so";
492
493 void* so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
494
495 if (so) {
496 ALOGD("dlopen_ext from APK (%s) success at %p", name.c_str(), so);
497 return so;
498 } else {
499 ALOGE("dlopen_ext(\"%s\") failed: %s", name.c_str(), dlerror());
500 }
501
502 return nullptr;
503}
504
Cody Northrop6d082ef2018-09-11 09:42:31 -0600505static void* load_angle(const char* kind, android_namespace_t* ns, egl_connection_t* cnx) {
Yiwei Zhang09d27712020-04-23 10:40:24 -0700506 void* so = load_angle_from_namespace(kind, ns);
Cody Northrop1f00e172018-04-02 11:23:31 -0600507
508 if (so) {
Tim Van Patten5f744f12018-12-12 11:46:21 -0700509 ALOGV("Loaded ANGLE %s library for '%s' (instead of native)", kind,
510 android::GraphicsEnv::getInstance().getAngleAppName().c_str());
Charlie Lao840bd532020-01-16 17:34:37 -0800511 cnx->useAngle = true;
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600512
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600513 char prop[PROPERTY_VALUE_MAX];
Tim Van Patten5f744f12018-12-12 11:46:21 -0700514
515 property_get("debug.hwui.renderer", prop, "UNSET");
516 ALOGV("Skia's renderer set to %s", prop);
517
518 EGLint angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE;
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600519 property_get("debug.angle.backend", prop, "0");
520 switch (atoi(prop)) {
521 case 1:
522 ALOGV("%s: Requesting OpenGLES back-end", __FUNCTION__);
523 angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE;
524 break;
525 case 2:
526 ALOGV("%s: Requesting Vulkan back-end", __FUNCTION__);
527 angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE;
528 break;
529 default:
530 break;
531 }
532
533 cnx->angleBackend = angleBackendDefault;
534 if (!cnx->vendorEGL && (cnx->angleBackend == EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE)) {
535 // Find and load vendor libEGL for ANGLE's GL back-end to use.
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700536 char prop[PROPERTY_VALUE_MAX + 1];
537 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
538 if (property_get(key, prop, nullptr) <= 0) {
539 continue;
540 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700541 void* dso = load_system_driver("EGL", prop, true);
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700542 if (dso) {
543 cnx->vendorEGL = dso;
544 break;
545 }
546 }
547 if (!cnx->vendorEGL) {
Peiyong Lin0d10b252019-04-30 18:03:04 -0700548 cnx->vendorEGL = load_system_driver("EGL", nullptr, true);
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700549 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600550 }
Ian Elliottb058aff2018-08-09 12:00:55 -0600551 } else {
Tim Van Patten5f744f12018-12-12 11:46:21 -0700552 ALOGV("Loaded native %s library for '%s' (instead of ANGLE)", kind,
553 android::GraphicsEnv::getInstance().getAngleAppName().c_str());
Charlie Lao840bd532020-01-16 17:34:37 -0800554 cnx->useAngle = false;
Cody Northrop1f00e172018-04-02 11:23:31 -0600555 }
556
Tim Van Patten5f744f12018-12-12 11:46:21 -0700557 return so;
Cody Northrop1f00e172018-04-02 11:23:31 -0600558}
559
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800560static void* load_updated_driver(const char* kind, android_namespace_t* ns) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800561 ATRACE_CALL();
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800562 const android_dlextinfo dlextinfo = {
563 .flags = ANDROID_DLEXT_USE_NAMESPACE,
564 .library_namespace = ns,
565 };
566 void* so = nullptr;
567 char prop[PROPERTY_VALUE_MAX + 1];
568 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700569 if (property_get(key, prop, nullptr) <= 0) {
570 continue;
571 }
572 std::string name = std::string("lib") + kind + "_" + prop + ".so";
573 so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
574 if (so) {
575 return so;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800576 }
577 }
578 return nullptr;
579}
580
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700581Loader::driver_t* Loader::attempt_to_load_angle(egl_connection_t* cnx) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800582 ATRACE_CALL();
Yiwei Zhang09d27712020-04-23 10:40:24 -0700583
584 if (!android::GraphicsEnv::getInstance().shouldUseAngle()) {
585 return nullptr;
586 }
587
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600588 android_namespace_t* ns = android::GraphicsEnv::getInstance().getAngleNamespace();
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700589 if (!ns) {
590 return nullptr;
Cody Northrop6d082ef2018-09-11 09:42:31 -0600591 }
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700592
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700593 android::GraphicsEnv::getInstance().setDriverToLoad(android::GpuStatsInfo::Driver::ANGLE);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700594 driver_t* hnd = nullptr;
595
596 // ANGLE doesn't ship with GLES library, and thus we skip GLES driver.
597 void* dso = load_angle("EGL", ns, cnx);
598 if (dso) {
599 initialize_api(dso, cnx, EGL);
600 hnd = new driver_t(dso);
601
Charlie Lao840bd532020-01-16 17:34:37 -0800602 dso = load_angle("GLESv1_CM", ns, cnx);
603 initialize_api(dso, cnx, GLESv1_CM);
604 hnd->set(dso, GLESv1_CM);
605
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700606 dso = load_angle("GLESv2", ns, cnx);
607 initialize_api(dso, cnx, GLESv2);
608 hnd->set(dso, GLESv2);
609 }
610 return hnd;
611}
612
613Loader::driver_t* Loader::attempt_to_load_updated_driver(egl_connection_t* cnx) {
614 ATRACE_CALL();
Jiyong Parka243e5d2017-06-21 12:26:51 +0900615#ifndef __ANDROID_VNDK__
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700616 android_namespace_t* ns = android::GraphicsEnv::getInstance().getDriverNamespace();
617 if (!ns) {
618 return nullptr;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800619 }
620
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700621 ALOGD("Load updated gl driver.");
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700622 android::GraphicsEnv::getInstance().setDriverToLoad(android::GpuStatsInfo::Driver::GL_UPDATED);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700623 driver_t* hnd = nullptr;
624 void* dso = load_updated_driver("GLES", ns);
Peiyong Line83b8682019-04-18 17:23:02 -0700625 if (dso) {
Charlie Lao840bd532020-01-16 17:34:37 -0800626 initialize_api(dso, cnx, EGL | GLESv1_CM | GLESv2);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700627 hnd = new driver_t(dso);
628 return hnd;
Peiyong Line83b8682019-04-18 17:23:02 -0700629 }
630
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700631 dso = load_updated_driver("EGL", ns);
632 if (dso) {
633 initialize_api(dso, cnx, EGL);
634 hnd = new driver_t(dso);
635
Charlie Lao840bd532020-01-16 17:34:37 -0800636 dso = load_updated_driver("GLESv1_CM", ns);
637 initialize_api(dso, cnx, GLESv1_CM);
638 hnd->set(dso, GLESv1_CM);
639
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700640 dso = load_updated_driver("GLESv2", ns);
641 initialize_api(dso, cnx, GLESv2);
642 hnd->set(dso, GLESv2);
643 }
644 return hnd;
645#else
Peiyong Line83b8682019-04-18 17:23:02 -0700646 return nullptr;
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700647#endif
648}
649
Peiyong Lin0d10b252019-04-30 18:03:04 -0700650Loader::driver_t* Loader::attempt_to_load_system_driver(egl_connection_t* cnx, const char* suffix,
651 const bool exact) {
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700652 ATRACE_CALL();
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700653 android::GraphicsEnv::getInstance().setDriverToLoad(android::GpuStatsInfo::Driver::GL);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700654 driver_t* hnd = nullptr;
Peiyong Lin0d10b252019-04-30 18:03:04 -0700655 void* dso = load_system_driver("GLES", suffix, exact);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700656 if (dso) {
Charlie Lao840bd532020-01-16 17:34:37 -0800657 initialize_api(dso, cnx, EGL | GLESv1_CM | GLESv2);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700658 hnd = new driver_t(dso);
659 return hnd;
660 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700661 dso = load_system_driver("EGL", suffix, exact);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700662 if (dso) {
663 initialize_api(dso, cnx, EGL);
664 hnd = new driver_t(dso);
665
Charlie Lao840bd532020-01-16 17:34:37 -0800666 dso = load_system_driver("GLESv1_CM", suffix, exact);
667 initialize_api(dso, cnx, GLESv1_CM);
668 hnd->set(dso, GLESv1_CM);
669
Peiyong Lin0d10b252019-04-30 18:03:04 -0700670 dso = load_system_driver("GLESv2", suffix, exact);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700671 initialize_api(dso, cnx, GLESv2);
672 hnd->set(dso, GLESv2);
673 }
674 return hnd;
Peiyong Line83b8682019-04-18 17:23:02 -0700675}
676
677void Loader::initialize_api(void* dso, egl_connection_t* cnx, uint32_t mask) {
Mathias Agopiande586972009-05-28 17:39:03 -0700678 if (mask & EGL) {
679 getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress");
680
Jesse Hall94cdba92013-07-11 09:40:35 -0700681 ALOGE_IF(!getProcAddress,
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800682 "can't find eglGetProcAddress() in EGL driver library");
Mathias Agopiande586972009-05-28 17:39:03 -0700683
Mathias Agopian618fa102009-10-14 02:06:37 -0700684 egl_t* egl = &cnx->egl;
Mathias Agopiande586972009-05-28 17:39:03 -0700685 __eglMustCastToProperFunctionPointerType* curr =
686 (__eglMustCastToProperFunctionPointerType*)egl;
687 char const * const * api = egl_names;
688 while (*api) {
689 char const * name = *api;
Jesse Hall94cdba92013-07-11 09:40:35 -0700690 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700691 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700692 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700693 // couldn't find the entry-point, use eglGetProcAddress()
694 f = getProcAddress(name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700695 if (f == nullptr) {
696 f = (__eglMustCastToProperFunctionPointerType)nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700697 }
698 }
699 *curr++ = f;
700 api++;
701 }
702 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700703
Mathias Agopiande586972009-05-28 17:39:03 -0700704 if (mask & GLESv1_CM) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700705 init_api(dso, gl_names_1, gl_names,
Mathias Agopian618fa102009-10-14 02:06:37 -0700706 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800707 &cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl,
Mathias Agopian618fa102009-10-14 02:06:37 -0700708 getProcAddress);
Mathias Agopiande586972009-05-28 17:39:03 -0700709 }
710
711 if (mask & GLESv2) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700712 init_api(dso, gl_names, nullptr,
Mathias Agopian618fa102009-10-14 02:06:37 -0700713 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800714 &cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl,
Mathias Agopiande586972009-05-28 17:39:03 -0700715 getProcAddress);
716 }
Mathias Agopiande586972009-05-28 17:39:03 -0700717}
718
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700719} // namespace android