blob: e19802b32f1473cbe81b1a71f819c3c7b83ea688 [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
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 Lindb3ed6e2020-01-09 18:43:27 -0800241 // If updated driver apk is set but fail to load, abort here.
242 if (android::GraphicsEnv::getInstance().getDriverNamespace()) {
243 LOG_ALWAYS_FATAL("couldn't find an OpenGL ES implementation from %s",
244 android::GraphicsEnv::getInstance().getDriverPath().c_str());
245 }
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700246 // Finally, try to load system driver, start by searching for the library name appended by
247 // the system properties of the GLES userspace driver in both locations.
248 // i.e.:
249 // libGLES_${prop}.so, or:
250 // libEGL_${prop}.so, libGLESv1_CM_${prop}.so, libGLESv2_${prop}.so
Charlie Lao840bd532020-01-16 17:34:37 -0800251 char prop[PROPERTY_VALUE_MAX + 1];
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700252 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
Charlie Lao840bd532020-01-16 17:34:37 -0800253 if (property_get(key, prop, nullptr) <= 0) {
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700254 continue;
255 }
Charlie Lao840bd532020-01-16 17:34:37 -0800256 hnd = attempt_to_load_system_driver(cnx, prop, true);
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700257 if (hnd) {
258 break;
Charlie Lao840bd532020-01-16 17:34:37 -0800259 } else if (strcmp(key, DRIVER_SUFFIX_PROPERTY) == 0) {
260 failToLoadFromDriverSuffixProperty = true;
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700261 }
262 }
263 }
264
265 if (!hnd) {
266 // Can't find graphics driver by appending system properties, now search for the exact name
267 // without any suffix of the GLES userspace driver in both locations.
268 // i.e.:
269 // libGLES.so, or:
270 // libEGL.so, libGLESv1_CM.so, libGLESv2.so
Charlie Lao840bd532020-01-16 17:34:37 -0800271 hnd = attempt_to_load_system_driver(cnx, nullptr, true);
Peiyong Lin0d10b252019-04-30 18:03:04 -0700272 }
273
274 if (!hnd && !failToLoadFromDriverSuffixProperty) {
Charlie Lao840bd532020-01-16 17:34:37 -0800275 hnd = attempt_to_load_system_driver(cnx, nullptr, false);
Mathias Agopiande586972009-05-28 17:39:03 -0700276 }
277
Yiwei Zhangd9861812019-02-13 11:51:55 -0800278 if (!hnd) {
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700279 android::GraphicsEnv::getInstance().setDriverLoaded(android::GpuStatsInfo::Api::API_GL,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800280 false, systemTime() - openTime);
281 }
282
Peiyong Lin3575b9f2019-04-25 14:26:49 -0700283 LOG_ALWAYS_FATAL_IF(!hnd,
284 "couldn't find an OpenGL ES implementation, make sure you set %s or %s",
285 HAL_SUBNAME_KEY_PROPERTIES[0], HAL_SUBNAME_KEY_PROPERTIES[1]);
Jesse Hallc07b5202013-07-04 12:08:16 -0700286
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700287 if (!cnx->libEgl) {
288 cnx->libEgl = load_wrapper(EGL_WRAPPER_DIR "/libEGL.so");
289 }
Charlie Lao840bd532020-01-16 17:34:37 -0800290 if (!cnx->libGles1) {
291 cnx->libGles1 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv1_CM.so");
292 }
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700293 if (!cnx->libGles2) {
294 cnx->libGles2 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv2.so");
295 }
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700296
Charlie Lao840bd532020-01-16 17:34:37 -0800297 if (!cnx->libEgl || !cnx->libGles2 || !cnx->libGles1) {
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700298 android::GraphicsEnv::getInstance().setDriverLoaded(android::GpuStatsInfo::Api::API_GL,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800299 false, systemTime() - openTime);
300 }
301
Michael Chockc0ec5e22014-01-27 08:14:33 -0800302 LOG_ALWAYS_FATAL_IF(!cnx->libEgl,
303 "couldn't load system EGL wrapper libraries");
304
Charlie Lao840bd532020-01-16 17:34:37 -0800305 LOG_ALWAYS_FATAL_IF(!cnx->libGles2 || !cnx->libGles1,
306 "couldn't load system OpenGL ES wrapper libraries");
Jesse Hallc07b5202013-07-04 12:08:16 -0700307
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700308 android::GraphicsEnv::getInstance().setDriverLoaded(android::GpuStatsInfo::Api::API_GL, true,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800309 systemTime() - openTime);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800310
Mathias Agopiande586972009-05-28 17:39:03 -0700311 return (void*)hnd;
312}
313
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600314void Loader::close(egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700315{
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600316 driver_t* hnd = (driver_t*) cnx->dso;
Mathias Agopiande586972009-05-28 17:39:03 -0700317 delete hnd;
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600318 cnx->dso = nullptr;
319
Tim Van Patten5f744f12018-12-12 11:46:21 -0700320 cnx->shouldUseAngle = false;
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600321 cnx->angleDecided = false;
Charlie Lao840bd532020-01-16 17:34:37 -0800322 cnx->useAngle = false;
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600323
324 if (cnx->vendorEGL) {
325 dlclose(cnx->vendorEGL);
326 cnx->vendorEGL = nullptr;
327 }
Mathias Agopiande586972009-05-28 17:39:03 -0700328}
329
Jesse Hall94cdba92013-07-11 09:40:35 -0700330void Loader::init_api(void* dso,
331 char const * const * api,
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700332 char const * const * ref_api,
Jesse Hall94cdba92013-07-11 09:40:35 -0700333 __eglMustCastToProperFunctionPointerType* curr,
334 getProcAddressType getProcAddress)
Mathias Agopiande586972009-05-28 17:39:03 -0700335{
Jesse Hall1508ae62017-01-19 17:43:26 -0800336 ATRACE_CALL();
337
Mathias Agopian7773c432012-02-13 20:06:08 -0800338 const ssize_t SIZE = 256;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700339 char scrap[SIZE];
Mathias Agopiande586972009-05-28 17:39:03 -0700340 while (*api) {
341 char const * name = *api;
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700342 if (ref_api) {
343 char const * ref_name = *ref_api;
344 if (std::strcmp(name, ref_name) != 0) {
345 *curr++ = nullptr;
346 ref_api++;
347 continue;
348 }
349 }
350
Jesse Hall94cdba92013-07-11 09:40:35 -0700351 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700352 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700353 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700354 // couldn't find the entry-point, use eglGetProcAddress()
355 f = getProcAddress(name);
356 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700357 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700358 // Try without the OES postfix
359 ssize_t index = ssize_t(strlen(name)) - 3;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700360 if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) {
Mathias Agopiande586972009-05-28 17:39:03 -0700361 strncpy(scrap, name, index);
362 scrap[index] = 0;
363 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) {
Mathias Agopiande586972009-05-28 17:39:03 -0700368 // Try with the OES postfix
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700369 ssize_t index = ssize_t(strlen(name)) - 3;
370 if (index>0 && strcmp(name+index, "OES")) {
371 snprintf(scrap, SIZE, "%sOES", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700372 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000373 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700374 }
375 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700376 if (f == nullptr) {
Steve Block9d453682011-12-20 16:23:08 +0000377 //ALOGD("%s", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700378 f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented;
Mathias Agopian48d438d2012-01-28 21:44:00 -0800379
380 /*
381 * GL_EXT_debug_label is special, we always report it as
382 * supported, it's handled by GLES_trace. If GLES_trace is not
383 * enabled, then these are no-ops.
384 */
385 if (!strcmp(name, "glInsertEventMarkerEXT")) {
386 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
387 } else if (!strcmp(name, "glPushGroupMarkerEXT")) {
388 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
389 } else if (!strcmp(name, "glPopGroupMarkerEXT")) {
390 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
391 }
Mathias Agopiande586972009-05-28 17:39:03 -0700392 }
393 *curr++ = f;
394 api++;
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700395 if (ref_api) ref_api++;
Mathias Agopiande586972009-05-28 17:39:03 -0700396 }
397}
398
Peiyong Lin0d10b252019-04-30 18:03:04 -0700399static void* load_system_driver(const char* kind, const char* suffix, const bool exact) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800400 ATRACE_CALL();
Mathias Agopian99381422013-04-23 20:52:29 +0200401 class MatchFile {
402 public:
Peiyong Lin0d10b252019-04-30 18:03:04 -0700403 static std::string find(const char* libraryName, const bool exact) {
Mathias Agopian99381422013-04-23 20:52:29 +0200404 const char* const searchPaths[] = {
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800405#if defined(__LP64__)
406 "/vendor/lib64/egl",
407 "/system/lib64/egl"
408#else
Mathias Agopian99381422013-04-23 20:52:29 +0200409 "/vendor/lib/egl",
410 "/system/lib/egl"
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800411#endif
Mathias Agopian99381422013-04-23 20:52:29 +0200412 };
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700413
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700414 for (auto dir : searchPaths) {
Peiyong Lin0d10b252019-04-30 18:03:04 -0700415 std::string absolutePath;
416 if (find(absolutePath, libraryName, dir, exact)) {
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700417 return absolutePath;
Mathias Agopian99381422013-04-23 20:52:29 +0200418 }
Mathias Agopian99381422013-04-23 20:52:29 +0200419 }
420
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700421 // Driver not found. gah.
422 return std::string();
Mathias Agopian99381422013-04-23 20:52:29 +0200423 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700424 private:
425 static bool find(std::string& result,
426 const std::string& pattern, const char* const search, bool exact) {
427 if (exact) {
428 std::string absolutePath = std::string(search) + "/" + pattern + ".so";
429 if (!access(absolutePath.c_str(), R_OK)) {
430 result = absolutePath;
431 return true;
432 }
433 return false;
434 }
435
436 DIR* d = opendir(search);
437 if (d != nullptr) {
438 struct dirent* e;
439 while ((e = readdir(d)) != nullptr) {
440 if (e->d_type == DT_DIR) {
441 continue;
442 }
443 if (!strcmp(e->d_name, "libGLES_android.so")) {
444 // always skip the software renderer
445 continue;
446 }
447 if (strstr(e->d_name, pattern.c_str()) == e->d_name) {
448 if (!strcmp(e->d_name + strlen(e->d_name) - 3, ".so")) {
449 result = std::string(search) + "/" + e->d_name;
450 closedir(d);
451 return true;
452 }
453 }
454 }
455 closedir(d);
456 }
457 return false;
458 }
Mathias Agopian99381422013-04-23 20:52:29 +0200459 };
460
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700461 std::string libraryName = std::string("lib") + kind;
462 if (suffix) {
463 libraryName += std::string("_") + suffix;
Peiyong Lin0d10b252019-04-30 18:03:04 -0700464 } else if (!exact) {
465 // Deprecated: we look for files that match
466 // libGLES_*.so, or:
467 // libEGL_*.so, libGLESv1_CM_*.so, libGLESv2_*.so
468 libraryName += std::string("_");
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700469 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700470 std::string absolutePath = MatchFile::find(libraryName.c_str(), exact);
Mathias Agopian65421432017-03-08 11:49:05 -0800471 if (absolutePath.empty()) {
Mathias Agopian99381422013-04-23 20:52:29 +0200472 // this happens often, we don't want to log an error
Yi Kong48a6cd22018-07-18 10:07:09 -0700473 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700474 }
Mathias Agopian65421432017-03-08 11:49:05 -0800475 const char* const driver_absolute_path = absolutePath.c_str();
Mathias Agopiande586972009-05-28 17:39:03 -0700476
Jiyong Park5910dc72017-04-05 14:23:52 +0900477 // Try to load drivers from the 'sphal' namespace, if it exist. Fall back to
Justin Yunb7320302017-05-22 15:13:40 +0900478 // the original routine when the namespace does not exist.
Jiyong Park5910dc72017-04-05 14:23:52 +0900479 // See /system/core/rootdir/etc/ld.config.txt for the configuration of the
480 // sphal namespace.
Justin Yunb7320302017-05-22 15:13:40 +0900481 void* dso = do_android_load_sphal_library(driver_absolute_path,
482 RTLD_NOW | RTLD_LOCAL);
Yi Kong48a6cd22018-07-18 10:07:09 -0700483 if (dso == nullptr) {
Mathias Agopian8c173842009-09-20 16:01:02 -0700484 const char* err = dlerror();
Mathias Agopian65421432017-03-08 11:49:05 -0800485 ALOGE("load_driver(%s): %s", driver_absolute_path, err ? err : "unknown");
Yi Kong48a6cd22018-07-18 10:07:09 -0700486 return nullptr;
Mathias Agopian8c173842009-09-20 16:01:02 -0700487 }
488
Steve Block9d453682011-12-20 16:23:08 +0000489 ALOGD("loaded %s", driver_absolute_path);
Mathias Agopianbaca89c2009-08-20 19:09:34 -0700490
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800491 return dso;
492}
493
Cody Northrop1f00e172018-04-02 11:23:31 -0600494static void* load_angle_from_namespace(const char* kind, android_namespace_t* ns) {
495 const android_dlextinfo dlextinfo = {
496 .flags = ANDROID_DLEXT_USE_NAMESPACE,
497 .library_namespace = ns,
498 };
499
500 std::string name = std::string("lib") + kind + "_angle.so";
501
502 void* so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
503
504 if (so) {
505 ALOGD("dlopen_ext from APK (%s) success at %p", name.c_str(), so);
506 return so;
507 } else {
508 ALOGE("dlopen_ext(\"%s\") failed: %s", name.c_str(), dlerror());
509 }
510
511 return nullptr;
512}
513
Cody Northrop6d082ef2018-09-11 09:42:31 -0600514static void* load_angle(const char* kind, android_namespace_t* ns, egl_connection_t* cnx) {
Courtney Goeltzenleuchter985bc282018-11-02 14:24:55 -0600515 void* so = nullptr;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700516
517 if ((cnx->shouldUseAngle) || android::GraphicsEnv::getInstance().shouldUseAngle()) {
Ian Elliott34474472018-09-05 10:57:07 -0600518 so = load_angle_from_namespace(kind, ns);
Tim Van Patten5f744f12018-12-12 11:46:21 -0700519 cnx->shouldUseAngle = true;
520 } else {
521 cnx->shouldUseAngle = false;
Ian Elliott34474472018-09-05 10:57:07 -0600522 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600523
524 if (so) {
Tim Van Patten5f744f12018-12-12 11:46:21 -0700525 ALOGV("Loaded ANGLE %s library for '%s' (instead of native)", kind,
526 android::GraphicsEnv::getInstance().getAngleAppName().c_str());
Charlie Lao840bd532020-01-16 17:34:37 -0800527 cnx->useAngle = true;
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600528
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600529 char prop[PROPERTY_VALUE_MAX];
Tim Van Patten5f744f12018-12-12 11:46:21 -0700530
531 property_get("debug.hwui.renderer", prop, "UNSET");
532 ALOGV("Skia's renderer set to %s", prop);
533
534 EGLint angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE;
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600535 property_get("debug.angle.backend", prop, "0");
536 switch (atoi(prop)) {
537 case 1:
538 ALOGV("%s: Requesting OpenGLES back-end", __FUNCTION__);
539 angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE;
540 break;
541 case 2:
542 ALOGV("%s: Requesting Vulkan back-end", __FUNCTION__);
543 angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE;
544 break;
545 default:
546 break;
547 }
548
549 cnx->angleBackend = angleBackendDefault;
550 if (!cnx->vendorEGL && (cnx->angleBackend == EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE)) {
551 // Find and load vendor libEGL for ANGLE's GL back-end to use.
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700552 char prop[PROPERTY_VALUE_MAX + 1];
553 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
554 if (property_get(key, prop, nullptr) <= 0) {
555 continue;
556 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700557 void* dso = load_system_driver("EGL", prop, true);
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700558 if (dso) {
559 cnx->vendorEGL = dso;
560 break;
561 }
562 }
563 if (!cnx->vendorEGL) {
Peiyong Lin0d10b252019-04-30 18:03:04 -0700564 cnx->vendorEGL = load_system_driver("EGL", nullptr, true);
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700565 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600566 }
Ian Elliottb058aff2018-08-09 12:00:55 -0600567 } else {
Tim Van Patten5f744f12018-12-12 11:46:21 -0700568 ALOGV("Loaded native %s library for '%s' (instead of ANGLE)", kind,
569 android::GraphicsEnv::getInstance().getAngleAppName().c_str());
Charlie Lao840bd532020-01-16 17:34:37 -0800570 cnx->useAngle = false;
Cody Northrop1f00e172018-04-02 11:23:31 -0600571 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700572 cnx->angleDecided = true;
Cody Northrop1f00e172018-04-02 11:23:31 -0600573
Tim Van Patten5f744f12018-12-12 11:46:21 -0700574 return so;
Cody Northrop1f00e172018-04-02 11:23:31 -0600575}
576
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800577static void* load_updated_driver(const char* kind, android_namespace_t* ns) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800578 ATRACE_CALL();
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800579 const android_dlextinfo dlextinfo = {
580 .flags = ANDROID_DLEXT_USE_NAMESPACE,
581 .library_namespace = ns,
582 };
583 void* so = nullptr;
584 char prop[PROPERTY_VALUE_MAX + 1];
585 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
Peiyong Lin9f0c79d2019-04-22 10:14:57 -0700586 if (property_get(key, prop, nullptr) <= 0) {
587 continue;
588 }
589 std::string name = std::string("lib") + kind + "_" + prop + ".so";
590 so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
591 if (so) {
592 return so;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800593 }
594 }
595 return nullptr;
596}
597
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700598Loader::driver_t* Loader::attempt_to_load_angle(egl_connection_t* cnx) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800599 ATRACE_CALL();
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600600 android_namespace_t* ns = android::GraphicsEnv::getInstance().getAngleNamespace();
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700601 if (!ns) {
602 return nullptr;
Cody Northrop6d082ef2018-09-11 09:42:31 -0600603 }
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700604
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700605 android::GraphicsEnv::getInstance().setDriverToLoad(android::GpuStatsInfo::Driver::ANGLE);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700606 driver_t* hnd = nullptr;
607
608 // ANGLE doesn't ship with GLES library, and thus we skip GLES driver.
609 void* dso = load_angle("EGL", ns, cnx);
610 if (dso) {
611 initialize_api(dso, cnx, EGL);
612 hnd = new driver_t(dso);
613
Charlie Lao840bd532020-01-16 17:34:37 -0800614 dso = load_angle("GLESv1_CM", ns, cnx);
615 initialize_api(dso, cnx, GLESv1_CM);
616 hnd->set(dso, GLESv1_CM);
617
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700618 dso = load_angle("GLESv2", ns, cnx);
619 initialize_api(dso, cnx, GLESv2);
620 hnd->set(dso, GLESv2);
621 }
622 return hnd;
623}
624
625Loader::driver_t* Loader::attempt_to_load_updated_driver(egl_connection_t* cnx) {
626 ATRACE_CALL();
Jiyong Parka243e5d2017-06-21 12:26:51 +0900627#ifndef __ANDROID_VNDK__
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700628 android_namespace_t* ns = android::GraphicsEnv::getInstance().getDriverNamespace();
629 if (!ns) {
630 return nullptr;
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800631 }
632
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700633 ALOGD("Load updated gl driver.");
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700634 android::GraphicsEnv::getInstance().setDriverToLoad(android::GpuStatsInfo::Driver::GL_UPDATED);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700635 driver_t* hnd = nullptr;
636 void* dso = load_updated_driver("GLES", ns);
Peiyong Line83b8682019-04-18 17:23:02 -0700637 if (dso) {
Charlie Lao840bd532020-01-16 17:34:37 -0800638 initialize_api(dso, cnx, EGL | GLESv1_CM | GLESv2);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700639 hnd = new driver_t(dso);
640 return hnd;
Peiyong Line83b8682019-04-18 17:23:02 -0700641 }
642
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700643 dso = load_updated_driver("EGL", ns);
644 if (dso) {
645 initialize_api(dso, cnx, EGL);
646 hnd = new driver_t(dso);
647
Charlie Lao840bd532020-01-16 17:34:37 -0800648 dso = load_updated_driver("GLESv1_CM", ns);
649 initialize_api(dso, cnx, GLESv1_CM);
650 hnd->set(dso, GLESv1_CM);
651
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700652 dso = load_updated_driver("GLESv2", ns);
653 initialize_api(dso, cnx, GLESv2);
654 hnd->set(dso, GLESv2);
655 }
656 return hnd;
657#else
Peiyong Line83b8682019-04-18 17:23:02 -0700658 return nullptr;
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700659#endif
660}
661
Peiyong Lin0d10b252019-04-30 18:03:04 -0700662Loader::driver_t* Loader::attempt_to_load_system_driver(egl_connection_t* cnx, const char* suffix,
663 const bool exact) {
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700664 ATRACE_CALL();
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700665 android::GraphicsEnv::getInstance().setDriverToLoad(android::GpuStatsInfo::Driver::GL);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700666 driver_t* hnd = nullptr;
Peiyong Lin0d10b252019-04-30 18:03:04 -0700667 void* dso = load_system_driver("GLES", suffix, exact);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700668 if (dso) {
Charlie Lao840bd532020-01-16 17:34:37 -0800669 initialize_api(dso, cnx, EGL | GLESv1_CM | GLESv2);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700670 hnd = new driver_t(dso);
671 return hnd;
672 }
Peiyong Lin0d10b252019-04-30 18:03:04 -0700673 dso = load_system_driver("EGL", suffix, exact);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700674 if (dso) {
675 initialize_api(dso, cnx, EGL);
676 hnd = new driver_t(dso);
677
Charlie Lao840bd532020-01-16 17:34:37 -0800678 dso = load_system_driver("GLESv1_CM", suffix, exact);
679 initialize_api(dso, cnx, GLESv1_CM);
680 hnd->set(dso, GLESv1_CM);
681
Peiyong Lin0d10b252019-04-30 18:03:04 -0700682 dso = load_system_driver("GLESv2", suffix, exact);
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700683 initialize_api(dso, cnx, GLESv2);
684 hnd->set(dso, GLESv2);
685 }
686 return hnd;
Peiyong Line83b8682019-04-18 17:23:02 -0700687}
688
689void Loader::initialize_api(void* dso, egl_connection_t* cnx, uint32_t mask) {
Mathias Agopiande586972009-05-28 17:39:03 -0700690 if (mask & EGL) {
691 getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress");
692
Jesse Hall94cdba92013-07-11 09:40:35 -0700693 ALOGE_IF(!getProcAddress,
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800694 "can't find eglGetProcAddress() in EGL driver library");
Mathias Agopiande586972009-05-28 17:39:03 -0700695
Mathias Agopian618fa102009-10-14 02:06:37 -0700696 egl_t* egl = &cnx->egl;
Mathias Agopiande586972009-05-28 17:39:03 -0700697 __eglMustCastToProperFunctionPointerType* curr =
698 (__eglMustCastToProperFunctionPointerType*)egl;
699 char const * const * api = egl_names;
700 while (*api) {
701 char const * name = *api;
Jesse Hall94cdba92013-07-11 09:40:35 -0700702 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700703 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700704 if (f == nullptr) {
Mathias Agopiande586972009-05-28 17:39:03 -0700705 // couldn't find the entry-point, use eglGetProcAddress()
706 f = getProcAddress(name);
Yi Kong48a6cd22018-07-18 10:07:09 -0700707 if (f == nullptr) {
708 f = (__eglMustCastToProperFunctionPointerType)nullptr;
Mathias Agopiande586972009-05-28 17:39:03 -0700709 }
710 }
711 *curr++ = f;
712 api++;
713 }
714 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700715
Mathias Agopiande586972009-05-28 17:39:03 -0700716 if (mask & GLESv1_CM) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700717 init_api(dso, gl_names_1, gl_names,
Mathias Agopian618fa102009-10-14 02:06:37 -0700718 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800719 &cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl,
Mathias Agopian618fa102009-10-14 02:06:37 -0700720 getProcAddress);
Mathias Agopiande586972009-05-28 17:39:03 -0700721 }
722
723 if (mask & GLESv2) {
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700724 init_api(dso, gl_names, nullptr,
Mathias Agopian618fa102009-10-14 02:06:37 -0700725 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800726 &cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl,
Mathias Agopiande586972009-05-28 17:39:03 -0700727 getProcAddress);
728 }
Mathias Agopiande586972009-05-28 17:39:03 -0700729}
730
Peiyong Lin8cd204d2019-04-19 14:05:17 -0700731} // namespace android