blob: e1432608c91482c75ad5f75e5893bf87a9789e60 [file] [log] [blame]
Jesse Hall94cdba92013-07-11 09:40:35 -07001/*
Mathias Agopiande586972009-05-28 17:39:03 -07002 ** Copyright 2007, The Android Open Source Project
3 **
Jesse Hall94cdba92013-07-11 09:40:35 -07004 ** Licensed under the Apache License, Version 2.0 (the "License");
5 ** you may not use this file except in compliance with the License.
6 ** You may obtain a copy of the License at
Mathias Agopiande586972009-05-28 17:39:03 -07007 **
Jesse Hall94cdba92013-07-11 09:40:35 -07008 ** http://www.apache.org/licenses/LICENSE-2.0
Mathias Agopiande586972009-05-28 17:39:03 -07009 **
Jesse Hall94cdba92013-07-11 09:40:35 -070010 ** Unless required by applicable law or agreed to in writing, software
11 ** distributed under the License is distributed on an "AS IS" BASIS,
12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ** See the License for the specific language governing permissions and
Mathias Agopiande586972009-05-28 17:39:03 -070014 ** limitations under the License.
15 */
16
Jesse Hall7a8d83e2016-12-20 15:24:28 -080017//#define LOG_NDEBUG 0
Jesse Hall1508ae62017-01-19 17:43:26 -080018#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Jesse Hall7a8d83e2016-12-20 15:24:28 -080019
Tim Van Patten5f744f12018-12-12 11:46:21 -070020#include <EGL/Loader.h>
Mathias Agopian311b4792017-02-28 15:00:49 -080021
Mathias Agopian65421432017-03-08 11:49:05 -080022#include <string>
23
Mathias Agopian99381422013-04-23 20:52:29 +020024#include <dirent.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070025#include <dlfcn.h>
Mathias Agopiande586972009-05-28 17:39:03 -070026
Jesse Hall7a8d83e2016-12-20 15:24:28 -080027#include <android/dlext.h>
David 'Digit' Turner80b30c22011-08-26 17:38:47 +020028#include <cutils/properties.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070029#include <log/log.h>
Yiwei Zhangd9861812019-02-13 11:51:55 -080030#include <utils/Timers.h>
Mathias Agopian311b4792017-02-28 15:00:49 -080031
Jiyong Parka243e5d2017-06-21 12:26:51 +090032#ifndef __ANDROID_VNDK__
Jiyong Park27c39e12017-05-08 13:00:02 +090033#include <graphicsenv/GraphicsEnv.h>
Jiyong Parka243e5d2017-06-21 12:26:51 +090034#endif
Justin Yunb7320302017-05-22 15:13:40 +090035#include <vndksupport/linker.h>
Mathias Agopiande586972009-05-28 17:39:03 -070036
Cody Northrop68d10352018-10-15 07:22:09 -060037#include "egl_platform_entries.h"
Mathias Agopian65421432017-03-08 11:49:05 -080038#include "egl_trace.h"
Mathias Agopian1cadb252011-05-23 17:26:14 -070039#include "egldefs.h"
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -060040#include <EGL/eglext_angle.h>
Mathias Agopiande586972009-05-28 17:39:03 -070041
Mathias Agopiande586972009-05-28 17:39:03 -070042namespace android {
Mathias Agopiande586972009-05-28 17:39:03 -070043
44/*
Mathias Agopian99381422013-04-23 20:52:29 +020045 * EGL userspace drivers must be provided either:
46 * - as a single library:
47 * /vendor/lib/egl/libGLES.so
48 *
49 * - as separate libraries:
50 * /vendor/lib/egl/libEGL.so
51 * /vendor/lib/egl/libGLESv1_CM.so
52 * /vendor/lib/egl/libGLESv2.so
53 *
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
Tim Van Patten5f744f12018-12-12 11:46:21 -0700225 // Check if we should use ANGLE early, so loading each driver doesn't require repeated queries.
226 if (android::GraphicsEnv::getInstance().shouldUseAngle()) {
227 cnx->shouldUseAngle = true;
228 } else {
229 cnx->shouldUseAngle = false;
230 }
231
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700232 // Firstly, try to load ANGLE driver.
233 driver_t* hnd = attempt_to_load_angle(cnx);
234 if (!hnd) {
235 // Secondly, try to load from driver apk.
236 hnd = attempt_to_load_updated_driver(cnx);
237 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700238
239 bool failToLoadFromDriverSuffixProperty = false;
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700240 if (!hnd) {
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700241 // Finally, try to load system driver, start by searching for the library name appended by
242 // the system properties of the GLES userspace driver in both locations.
243 // i.e.:
244 // libGLES_${prop}.so, or:
245 // libEGL_${prop}.so, libGLESv1_CM_${prop}.so, libGLESv2_${prop}.so
246 char prop[PROPERTY_VALUE_MAX + 1];
247 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
248 if (property_get(key, prop, nullptr) <= 0) {
249 continue;
250 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700251 hnd = attempt_to_load_system_driver(cnx, prop, true);
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700252 if (hnd) {
253 break;
Peiyong Lin0d10b252019-04-30 18:03:04 -0700254 } else if (strcmp(key, DRIVER_SUFFIX_PROPERTY) == 0) {
255 failToLoadFromDriverSuffixProperty = true;
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700256 }
257 }
258 }
259
260 if (!hnd) {
261 // Can't find graphics driver by appending system properties, now search for the exact name
262 // without any suffix of the GLES userspace driver in both locations.
263 // i.e.:
264 // libGLES.so, or:
265 // libEGL.so, libGLESv1_CM.so, libGLESv2.so
Peiyong Lin0d10b252019-04-30 18:03:04 -0700266 hnd = attempt_to_load_system_driver(cnx, nullptr, true);
267 }
268
269 if (!hnd && !failToLoadFromDriverSuffixProperty) {
270 hnd = attempt_to_load_system_driver(cnx, nullptr, false);
Mathias Agopiande586972009-05-28 17:39:03 -0700271 }
272
Yiwei Zhangd9861812019-02-13 11:51:55 -0800273 if (!hnd) {
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700274 android::GraphicsEnv::getInstance().setDriverLoaded(android::GpuStatsInfo::Api::API_GL,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800275 false, systemTime() - openTime);
276 }
277
Peiyong Lin3575b9f2019-04-25 14:26:49 -0700278 LOG_ALWAYS_FATAL_IF(!hnd,
279 "couldn't find an OpenGL ES implementation, make sure you set %s or %s",
280 HAL_SUBNAME_KEY_PROPERTIES[0], HAL_SUBNAME_KEY_PROPERTIES[1]);
Jesse Hallc07b5202013-07-04 12:08:16 -0700281
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700282 if (!cnx->libEgl) {
283 cnx->libEgl = load_wrapper(EGL_WRAPPER_DIR "/libEGL.so");
284 }
285 if (!cnx->libGles1) {
286 cnx->libGles1 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv1_CM.so");
287 }
288 if (!cnx->libGles2) {
289 cnx->libGles2 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv2.so");
290 }
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700291
Yiwei Zhangd9861812019-02-13 11:51:55 -0800292 if (!cnx->libEgl || !cnx->libGles2 || !cnx->libGles1) {
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700293 android::GraphicsEnv::getInstance().setDriverLoaded(android::GpuStatsInfo::Api::API_GL,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800294 false, systemTime() - openTime);
295 }
296
Michael Chockc0ec5e22014-01-27 08:14:33 -0800297 LOG_ALWAYS_FATAL_IF(!cnx->libEgl,
298 "couldn't load system EGL wrapper libraries");
299
Jesse Hallc07b5202013-07-04 12:08:16 -0700300 LOG_ALWAYS_FATAL_IF(!cnx->libGles2 || !cnx->libGles1,
301 "couldn't load system OpenGL ES wrapper libraries");
302
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700303 android::GraphicsEnv::getInstance().setDriverLoaded(android::GpuStatsInfo::Api::API_GL, true,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800304 systemTime() - openTime);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800305
Mathias Agopiande586972009-05-28 17:39:03 -0700306 return (void*)hnd;
307}
308
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600309void Loader::close(egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700310{
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600311 driver_t* hnd = (driver_t*) cnx->dso;
Mathias Agopiande586972009-05-28 17:39:03 -0700312 delete hnd;
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600313 cnx->dso = nullptr;
314
Tim Van Patten5f744f12018-12-12 11:46:21 -0700315 cnx->shouldUseAngle = false;
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600316 cnx->angleDecided = false;
317 cnx->useAngle = false;
318
319 if (cnx->vendorEGL) {
320 dlclose(cnx->vendorEGL);
321 cnx->vendorEGL = nullptr;
322 }
Mathias Agopiande586972009-05-28 17:39:03 -0700323}
324
Jesse Hall94cdba92013-07-11 09:40:35 -0700325void Loader::init_api(void* dso,
326 char const * const * api,
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700327 char const * const * ref_api,
Jesse Hall94cdba92013-07-11 09:40:35 -0700328 __eglMustCastToProperFunctionPointerType* curr,
329 getProcAddressType getProcAddress)
Mathias Agopiande586972009-05-28 17:39:03 -0700330{
Jesse Hall1508ae62017-01-19 17:43:26 -0800331 ATRACE_CALL();
332
Mathias Agopian7773c432012-02-13 20:06:08 -0800333 const ssize_t SIZE = 256;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700334 char scrap[SIZE];
Mathias Agopiande586972009-05-28 17:39:03 -0700335 while (*api) {
336 char const * name = *api;
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700337 if (ref_api) {
338 char const * ref_name = *ref_api;
339 if (std::strcmp(name, ref_name) != 0) {
340 *curr++ = nullptr;
341 ref_api++;
342 continue;
343 }
344 }
345
Jesse Hall94cdba92013-07-11 09:40:35 -0700346 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700347 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700348 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700349 // couldn't find the entry-point, use eglGetProcAddress()
350 f = getProcAddress(name);
351 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700352 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700353 // Try without the OES postfix
354 ssize_t index = ssize_t(strlen(name)) - 3;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700355 if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) {
Mathias Agopiande586972009-05-28 17:39:03 -0700356 strncpy(scrap, name, index);
357 scrap[index] = 0;
358 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000359 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700360 }
361 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700362 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700363 // Try with the OES postfix
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700364 ssize_t index = ssize_t(strlen(name)) - 3;
365 if (index>0 && strcmp(name+index, "OES")) {
366 snprintf(scrap, SIZE, "%sOES", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700367 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000368 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700369 }
370 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700371 if (f == nullptr) {
Steve Block9d453682011-12-20 16:23:08 +0000372 //ALOGD("%s", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700373 f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented;
Mathias Agopian48d438d2012-01-28 21:44:00 -0800374
375 /*
376 * GL_EXT_debug_label is special, we always report it as
377 * supported, it's handled by GLES_trace. If GLES_trace is not
378 * enabled, then these are no-ops.
379 */
380 if (!strcmp(name, "glInsertEventMarkerEXT")) {
381 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
382 } else if (!strcmp(name, "glPushGroupMarkerEXT")) {
383 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
384 } else if (!strcmp(name, "glPopGroupMarkerEXT")) {
385 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
386 }
Mathias Agopiande586972009-05-28 17:39:03 -0700387 }
388 *curr++ = f;
389 api++;
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700390 if (ref_api) ref_api++;
Mathias Agopiande586972009-05-28 17:39:03 -0700391 }
392}
393
Peiyong Lin0d10b252019-04-30 18:03:04 -0700394static void* load_system_driver(const char* kind, const char* suffix, const bool exact) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800395 ATRACE_CALL();
Mathias Agopian99381422013-04-23 20:52:29 +0200396 class MatchFile {
397 public:
Peiyong Lin0d10b252019-04-30 18:03:04 -0700398 static std::string find(const char* libraryName, const bool exact) {
Mathias Agopian99381422013-04-23 20:52:29 +0200399 const char* const searchPaths[] = {
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800400#if defined(__LP64__)
401 "/vendor/lib64/egl",
402 "/system/lib64/egl"
403#else
Mathias Agopian99381422013-04-23 20:52:29 +0200404 "/vendor/lib/egl",
405 "/system/lib/egl"
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800406#endif
Mathias Agopian99381422013-04-23 20:52:29 +0200407 };
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700408
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700409 for (auto dir : searchPaths) {
Peiyong Lin0d10b252019-04-30 18:03:04 -0700410 std::string absolutePath;
411 if (find(absolutePath, libraryName, dir, exact)) {
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700412 return absolutePath;
Mathias Agopian99381422013-04-23 20:52:29 +0200413 }
Mathias Agopian99381422013-04-23 20:52:29 +0200414 }
415
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700416 // Driver not found. gah.
417 return std::string();
Mathias Agopian99381422013-04-23 20:52:29 +0200418 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700419 private:
420 static bool find(std::string& result,
421 const std::string& pattern, const char* const search, bool exact) {
422 if (exact) {
423 std::string absolutePath = std::string(search) + "/" + pattern + ".so";
424 if (!access(absolutePath.c_str(), R_OK)) {
425 result = absolutePath;
426 return true;
427 }
428 return false;
429 }
430
431 DIR* d = opendir(search);
432 if (d != nullptr) {
433 struct dirent* e;
434 while ((e = readdir(d)) != nullptr) {
435 if (e->d_type == DT_DIR) {
436 continue;
437 }
438 if (!strcmp(e->d_name, "libGLES_android.so")) {
439 // always skip the software renderer
440 continue;
441 }
442 if (strstr(e->d_name, pattern.c_str()) == e->d_name) {
443 if (!strcmp(e->d_name + strlen(e->d_name) - 3, ".so")) {
444 result = std::string(search) + "/" + e->d_name;
445 closedir(d);
446 return true;
447 }
448 }
449 }
450 closedir(d);
451 }
452 return false;
453 }
Mathias Agopian99381422013-04-23 20:52:29 +0200454 };
455
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700456 std::string libraryName = std::string("lib") + kind;
457 if (suffix) {
458 libraryName += std::string("_") + suffix;
Peiyong Lin0d10b252019-04-30 18:03:04 -0700459 } else if (!exact) {
460 // Deprecated: we look for files that match
461 // libGLES_*.so, or:
462 // libEGL_*.so, libGLESv1_CM_*.so, libGLESv2_*.so
463 libraryName += std::string("_");
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700464 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700465 std::string absolutePath = MatchFile::find(libraryName.c_str(), exact);
Mathias Agopian65421432017-03-08 11:49:05 -0800466 if (absolutePath.empty()) {
Mathias Agopian99381422013-04-23 20:52:29 +0200467 // this happens often, we don't want to log an error
Yi Kong48a6cd22018-07-18 10:07:09 -0700468 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700469 }
Mathias Agopian65421432017-03-08 11:49:05 -0800470 const char* const driver_absolute_path = absolutePath.c_str();
Mathias Agopiande586972009-05-28 17:39:03 -0700471
Jiyong Park5910dc72017-04-05 14:23:52 +0900472 // Try to load drivers from the 'sphal' namespace, if it exist. Fall back to
Justin Yunb7320302017-05-22 15:13:40 +0900473 // the original routine when the namespace does not exist.
Jiyong Park5910dc72017-04-05 14:23:52 +0900474 // See /system/core/rootdir/etc/ld.config.txt for the configuration of the
475 // sphal namespace.
Justin Yunb7320302017-05-22 15:13:40 +0900476 void* dso = do_android_load_sphal_library(driver_absolute_path,
477 RTLD_NOW | RTLD_LOCAL);
Yi Kong48a6cd22018-07-18 10:07:09 -0700478 if (dso == nullptr) {
Mathias Agopian8c173842009-09-20 16:01:02 -0700479 const char* err = dlerror();
Mathias Agopian65421432017-03-08 11:49:05 -0800480 ALOGE("load_driver(%s): %s", driver_absolute_path, err ? err : "unknown");
Yi Kong48a6cd22018-07-18 10:07:09 -0700481 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700482 }
483
Steve Block9d453682011-12-20 16:23:08 +0000484 ALOGD("loaded %s", driver_absolute_path);
Mathias Agopianbaca89c2009-08-20 19:09:34 -0700485
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800486 return dso;
487}
488
Cody Northrop1f00e172018-04-02 11:23:31 -0600489static void* load_angle_from_namespace(const char* kind, android_namespace_t* ns) {
490 const android_dlextinfo dlextinfo = {
491 .flags = ANDROID_DLEXT_USE_NAMESPACE,
492 .library_namespace = ns,
493 };
494
495 std::string name = std::string("lib") + kind + "_angle.so";
496
497 void* so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
498
499 if (so) {
500 ALOGD("dlopen_ext from APK (%s) success at %p", name.c_str(), so);
501 return so;
502 } else {
503 ALOGE("dlopen_ext(\"%s\") failed: %s", name.c_str(), dlerror());
504 }
505
506 return nullptr;
507}
508
Cody Northrop6d082ef2018-09-11 09:42:31 -0600509static void* load_angle(const char* kind, android_namespace_t* ns, egl_connection_t* cnx) {
Courtney Goeltzenleuchter985bc282018-11-02 14:24:55 -0600510 void* so = nullptr;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700511
512 if ((cnx->shouldUseAngle) || android::GraphicsEnv::getInstance().shouldUseAngle()) {
Ian Elliott34474472018-09-05 10:57:07 -0600513 so = load_angle_from_namespace(kind, ns);
Tim Van Patten5f744f12018-12-12 11:46:21 -0700514 cnx->shouldUseAngle = true;
515 } else {
516 cnx->shouldUseAngle = false;
Ian Elliott34474472018-09-05 10:57:07 -0600517 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600518
519 if (so) {
Tim Van Patten5f744f12018-12-12 11:46:21 -0700520 ALOGV("Loaded ANGLE %s library for '%s' (instead of native)", kind,
521 android::GraphicsEnv::getInstance().getAngleAppName().c_str());
Cody Northrop1f00e172018-04-02 11:23:31 -0600522 cnx->useAngle = true;
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600523
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600524 char prop[PROPERTY_VALUE_MAX];
Tim Van Patten5f744f12018-12-12 11:46:21 -0700525
526 property_get("debug.hwui.renderer", prop, "UNSET");
527 ALOGV("Skia's renderer set to %s", prop);
528
529 EGLint angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE;
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600530 property_get("debug.angle.backend", prop, "0");
531 switch (atoi(prop)) {
532 case 1:
533 ALOGV("%s: Requesting OpenGLES back-end", __FUNCTION__);
534 angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE;
535 break;
536 case 2:
537 ALOGV("%s: Requesting Vulkan back-end", __FUNCTION__);
538 angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE;
539 break;
540 default:
541 break;
542 }
543
544 cnx->angleBackend = angleBackendDefault;
545 if (!cnx->vendorEGL && (cnx->angleBackend == EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE)) {
546 // Find and load vendor libEGL for ANGLE's GL back-end to use.
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700547 char prop[PROPERTY_VALUE_MAX + 1];
548 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
549 if (property_get(key, prop, nullptr) <= 0) {
550 continue;
551 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700552 void* dso = load_system_driver("EGL", prop, true);
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700553 if (dso) {
554 cnx->vendorEGL = dso;
555 break;
556 }
557 }
558 if (!cnx->vendorEGL) {
Peiyong Lin0d10b252019-04-30 18:03:04 -0700559 cnx->vendorEGL = load_system_driver("EGL", nullptr, true);
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700560 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600561 }
Ian Elliottb058aff2018-08-09 12:00:55 -0600562 } else {
Tim Van Patten5f744f12018-12-12 11:46:21 -0700563 ALOGV("Loaded native %s library for '%s' (instead of ANGLE)", kind,
564 android::GraphicsEnv::getInstance().getAngleAppName().c_str());
565 cnx->useAngle = false;
Cody Northrop1f00e172018-04-02 11:23:31 -0600566 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700567 cnx->angleDecided = true;
Cody Northrop1f00e172018-04-02 11:23:31 -0600568
Tim Van Patten5f744f12018-12-12 11:46:21 -0700569 return so;
Cody Northrop1f00e172018-04-02 11:23:31 -0600570}
571
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800572static void* load_updated_driver(const char* kind, android_namespace_t* ns) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800573 ATRACE_CALL();
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800574 const android_dlextinfo dlextinfo = {
575 .flags = ANDROID_DLEXT_USE_NAMESPACE,
576 .library_namespace = ns,
577 };
578 void* so = nullptr;
579 char prop[PROPERTY_VALUE_MAX + 1];
580 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700581 if (property_get(key, prop, nullptr) <= 0) {
582 continue;
583 }
584 std::string name = std::string("lib") + kind + "_" + prop + ".so";
585 so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
586 if (so) {
587 return so;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800588 }
589 }
590 return nullptr;
591}
592
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700593Loader::driver_t* Loader::attempt_to_load_angle(egl_connection_t* cnx) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800594 ATRACE_CALL();
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600595 android_namespace_t* ns = android::GraphicsEnv::getInstance().getAngleNamespace();
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700596 if (!ns) {
597 return nullptr;
Cody Northrop6d082ef2018-09-11 09:42:31 -0600598 }
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700599
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700600 android::GraphicsEnv::getInstance().setDriverToLoad(android::GpuStatsInfo::Driver::ANGLE);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700601 driver_t* hnd = nullptr;
602
603 // ANGLE doesn't ship with GLES library, and thus we skip GLES driver.
604 void* dso = load_angle("EGL", ns, cnx);
605 if (dso) {
606 initialize_api(dso, cnx, EGL);
607 hnd = new driver_t(dso);
608
609 dso = load_angle("GLESv1_CM", ns, cnx);
610 initialize_api(dso, cnx, GLESv1_CM);
611 hnd->set(dso, GLESv1_CM);
612
613 dso = load_angle("GLESv2", ns, cnx);
614 initialize_api(dso, cnx, GLESv2);
615 hnd->set(dso, GLESv2);
616 }
617 return hnd;
618}
619
620Loader::driver_t* Loader::attempt_to_load_updated_driver(egl_connection_t* cnx) {
621 ATRACE_CALL();
Jiyong Parka243e5d2017-06-21 12:26:51 +0900622#ifndef __ANDROID_VNDK__
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700623 android_namespace_t* ns = android::GraphicsEnv::getInstance().getDriverNamespace();
624 if (!ns) {
625 return nullptr;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800626 }
627
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700628 ALOGD("Load updated gl driver.");
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700629 android::GraphicsEnv::getInstance().setDriverToLoad(android::GpuStatsInfo::Driver::GL_UPDATED);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700630 driver_t* hnd = nullptr;
631 void* dso = load_updated_driver("GLES", ns);
Peiyong Line83b8682019-04-18 17:23:02 -0700632 if (dso) {
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700633 initialize_api(dso, cnx, EGL | GLESv1_CM | GLESv2);
634 hnd = new driver_t(dso);
635 return hnd;
Peiyong Line83b8682019-04-18 17:23:02 -0700636 }
637
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700638 dso = load_updated_driver("EGL", ns);
639 if (dso) {
640 initialize_api(dso, cnx, EGL);
641 hnd = new driver_t(dso);
642
643 dso = load_updated_driver("GLESv1_CM", ns);
644 initialize_api(dso, cnx, GLESv1_CM);
645 hnd->set(dso, GLESv1_CM);
646
647 dso = load_updated_driver("GLESv2", ns);
648 initialize_api(dso, cnx, GLESv2);
649 hnd->set(dso, GLESv2);
650 }
651 return hnd;
652#else
Peiyong Line83b8682019-04-18 17:23:02 -0700653 return nullptr;
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700654#endif
655}
656
Peiyong Lin0d10b252019-04-30 18:03:04 -0700657Loader::driver_t* Loader::attempt_to_load_system_driver(egl_connection_t* cnx, const char* suffix,
658 const bool exact) {
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700659 ATRACE_CALL();
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700660 android::GraphicsEnv::getInstance().setDriverToLoad(android::GpuStatsInfo::Driver::GL);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700661 driver_t* hnd = nullptr;
Peiyong Lin0d10b252019-04-30 18:03:04 -0700662 void* dso = load_system_driver("GLES", suffix, exact);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700663 if (dso) {
664 initialize_api(dso, cnx, EGL | GLESv1_CM | GLESv2);
665 hnd = new driver_t(dso);
666 return hnd;
667 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700668 dso = load_system_driver("EGL", suffix, exact);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700669 if (dso) {
670 initialize_api(dso, cnx, EGL);
671 hnd = new driver_t(dso);
672
Peiyong Lin0d10b252019-04-30 18:03:04 -0700673 dso = load_system_driver("GLESv1_CM", suffix, exact);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700674 initialize_api(dso, cnx, GLESv1_CM);
675 hnd->set(dso, GLESv1_CM);
676
Peiyong Lin0d10b252019-04-30 18:03:04 -0700677 dso = load_system_driver("GLESv2", suffix, exact);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700678 initialize_api(dso, cnx, GLESv2);
679 hnd->set(dso, GLESv2);
680 }
681 return hnd;
Peiyong Line83b8682019-04-18 17:23:02 -0700682}
683
684void Loader::initialize_api(void* dso, egl_connection_t* cnx, uint32_t mask) {
Mathias Agopiande586972009-05-28 17:39:03 -0700685 if (mask & EGL) {
686 getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress");
687
Jesse Hall94cdba92013-07-11 09:40:35 -0700688 ALOGE_IF(!getProcAddress,
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800689 "can't find eglGetProcAddress() in EGL driver library");
Mathias Agopiande586972009-05-28 17:39:03 -0700690
Mathias Agopian618fa102009-10-14 02:06:37 -0700691 egl_t* egl = &cnx->egl;
Mathias Agopiande586972009-05-28 17:39:03 -0700692 __eglMustCastToProperFunctionPointerType* curr =
693 (__eglMustCastToProperFunctionPointerType*)egl;
694 char const * const * api = egl_names;
695 while (*api) {
696 char const * name = *api;
Jesse Hall94cdba92013-07-11 09:40:35 -0700697 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700698 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700699 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700700 // couldn't find the entry-point, use eglGetProcAddress()
701 f = getProcAddress(name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700702 if (f == nullptr) {
703 f = (__eglMustCastToProperFunctionPointerType)nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700704 }
705 }
706 *curr++ = f;
707 api++;
708 }
709 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700710
Mathias Agopiande586972009-05-28 17:39:03 -0700711 if (mask & GLESv1_CM) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700712 init_api(dso, gl_names_1, gl_names,
Mathias Agopian618fa102009-10-14 02:06:37 -0700713 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800714 &cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl,
Mathias Agopian618fa102009-10-14 02:06:37 -0700715 getProcAddress);
Mathias Agopiande586972009-05-28 17:39:03 -0700716 }
717
718 if (mask & GLESv2) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700719 init_api(dso, gl_names, nullptr,
Mathias Agopian618fa102009-10-14 02:06:37 -0700720 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800721 &cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl,
Mathias Agopiande586972009-05-28 17:39:03 -0700722 getProcAddress);
723 }
Mathias Agopiande586972009-05-28 17:39:03 -0700724}
725
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700726} // namespace android