blob: 683e6ca2c6bc9f97924019c8bf82d778821bd1fb [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
Mathias Agopian311b4792017-02-28 15:00:49 -080020#include "Loader.h"
21
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>
Mathias Agopian311b4792017-02-28 15:00:49 -080030
Mathias Agopian991d2542017-02-06 13:51:32 -080031#include <ui/GraphicsEnv.h>
Mathias Agopiande586972009-05-28 17:39:03 -070032
Mathias Agopian65421432017-03-08 11:49:05 -080033#include "egl_trace.h"
Mathias Agopian1cadb252011-05-23 17:26:14 -070034#include "egldefs.h"
Mathias Agopiande586972009-05-28 17:39:03 -070035
36// ----------------------------------------------------------------------------
37namespace android {
38// ----------------------------------------------------------------------------
39
40
41/*
Mathias Agopian99381422013-04-23 20:52:29 +020042 * EGL userspace drivers must be provided either:
43 * - as a single library:
44 * /vendor/lib/egl/libGLES.so
45 *
46 * - as separate libraries:
47 * /vendor/lib/egl/libEGL.so
48 * /vendor/lib/egl/libGLESv1_CM.so
49 * /vendor/lib/egl/libGLESv2.so
50 *
51 * The software renderer for the emulator must be provided as a single
52 * library at:
53 *
54 * /system/lib/egl/libGLES_android.so
55 *
56 *
57 * For backward compatibility and to facilitate the transition to
58 * this new naming scheme, the loader will additionally look for:
Jesse Hall94cdba92013-07-11 09:40:35 -070059 *
Mathias Agopian99381422013-04-23 20:52:29 +020060 * /{vendor|system}/lib/egl/lib{GLES | [EGL|GLESv1_CM|GLESv2]}_*.so
Jesse Hall94cdba92013-07-11 09:40:35 -070061 *
Mathias Agopiande586972009-05-28 17:39:03 -070062 */
63
Mathias Agopian65421432017-03-08 11:49:05 -080064Loader& Loader::getInstance() {
65 static Loader loader;
66 return loader;
67}
Mathias Agopiande586972009-05-28 17:39:03 -070068
David 'Digit' Turner80b30c22011-08-26 17:38:47 +020069/* This function is called to check whether we run inside the emulator,
70 * and if this is the case whether GLES GPU emulation is supported.
71 *
72 * Returned values are:
73 * -1 -> not running inside the emulator
74 * 0 -> running inside the emulator, but GPU emulation not supported
75 * 1 -> running inside the emulator, GPU emulation is supported
Nicolas Capens776951f2015-11-06 10:10:21 -050076 * through the "emulation" host-side OpenGL ES implementation.
77 * 2 -> running inside the emulator, GPU emulation is supported
78 * through a guest-side vendor driver's OpenGL ES implementation.
David 'Digit' Turner80b30c22011-08-26 17:38:47 +020079 */
80static int
81checkGlesEmulationStatus(void)
82{
83 /* We're going to check for the following kernel parameters:
84 *
85 * qemu=1 -> tells us that we run inside the emulator
86 * android.qemu.gles=<number> -> tells us the GLES GPU emulation status
87 *
88 * Note that we will return <number> if we find it. This let us support
89 * more additionnal emulation modes in the future.
90 */
91 char prop[PROPERTY_VALUE_MAX];
92 int result = -1;
93
94 /* First, check for qemu=1 */
95 property_get("ro.kernel.qemu",prop,"0");
96 if (atoi(prop) != 1)
97 return -1;
98
99 /* We are in the emulator, get GPU status value */
bohu69e5b1a2016-02-19 17:15:20 -0800100 property_get("qemu.gles",prop,"0");
David 'Digit' Turner80b30c22011-08-26 17:38:47 +0200101 return atoi(prop);
102}
103
Jesse Hall1508ae62017-01-19 17:43:26 -0800104static void* do_dlopen(const char* path, int mode) {
105 ATRACE_CALL();
106 return dlopen(path, mode);
107}
108
Mathias Agopiande586972009-05-28 17:39:03 -0700109// ----------------------------------------------------------------------------
110
Jesse Hall94cdba92013-07-11 09:40:35 -0700111Loader::driver_t::driver_t(void* gles)
Mathias Agopiande586972009-05-28 17:39:03 -0700112{
113 dso[0] = gles;
114 for (size_t i=1 ; i<NELEM(dso) ; i++)
115 dso[i] = 0;
116}
117
Jesse Hall94cdba92013-07-11 09:40:35 -0700118Loader::driver_t::~driver_t()
Mathias Agopiande586972009-05-28 17:39:03 -0700119{
120 for (size_t i=0 ; i<NELEM(dso) ; i++) {
121 if (dso[i]) {
122 dlclose(dso[i]);
123 dso[i] = 0;
124 }
125 }
126}
127
Mathias Agopian65421432017-03-08 11:49:05 -0800128int Loader::driver_t::set(void* hnd, int32_t api)
Mathias Agopiande586972009-05-28 17:39:03 -0700129{
130 switch (api) {
131 case EGL:
132 dso[0] = hnd;
133 break;
134 case GLESv1_CM:
135 dso[1] = hnd;
136 break;
137 case GLESv2:
138 dso[2] = hnd;
139 break;
140 default:
Mathias Agopian65421432017-03-08 11:49:05 -0800141 return -EOVERFLOW;
Mathias Agopiande586972009-05-28 17:39:03 -0700142 }
Mathias Agopian65421432017-03-08 11:49:05 -0800143 return 0;
Mathias Agopiande586972009-05-28 17:39:03 -0700144}
145
146// ----------------------------------------------------------------------------
147
Mathias Agopiande586972009-05-28 17:39:03 -0700148Loader::Loader()
Mathias Agopian991d2542017-02-06 13:51:32 -0800149 : getProcAddress(NULL)
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800150{
Mathias Agopiande586972009-05-28 17:39:03 -0700151}
152
Mathias Agopian99381422013-04-23 20:52:29 +0200153Loader::~Loader() {
Mathias Agopiande586972009-05-28 17:39:03 -0700154}
155
Jesse Hallc07b5202013-07-04 12:08:16 -0700156static void* load_wrapper(const char* path) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800157 void* so = do_dlopen(path, RTLD_NOW | RTLD_LOCAL);
Jesse Hallc07b5202013-07-04 12:08:16 -0700158 ALOGE_IF(!so, "dlopen(\"%s\") failed: %s", path, dlerror());
159 return so;
160}
161
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700162#ifndef EGL_WRAPPER_DIR
163#if defined(__LP64__)
164#define EGL_WRAPPER_DIR "/system/lib64"
165#else
166#define EGL_WRAPPER_DIR "/system/lib"
167#endif
168#endif
169
bohu69e5b1a2016-02-19 17:15:20 -0800170static void setEmulatorGlesValue(void) {
171 char prop[PROPERTY_VALUE_MAX];
172 property_get("ro.kernel.qemu", prop, "0");
173 if (atoi(prop) != 1) return;
174
175 property_get("ro.kernel.qemu.gles",prop,"0");
176 if (atoi(prop) == 1) {
177 ALOGD("Emulator has host GPU support, qemu.gles is set to 1.");
178 property_set("qemu.gles", "1");
179 return;
180 }
181
182 // for now, checking the following
183 // directory is good enough for emulator system images
184 const char* vendor_lib_path =
185#if defined(__LP64__)
186 "/vendor/lib64/egl";
187#else
188 "/vendor/lib/egl";
189#endif
190
191 const bool has_vendor_lib = (access(vendor_lib_path, R_OK) == 0);
192 if (has_vendor_lib) {
193 ALOGD("Emulator has vendor provided software renderer, qemu.gles is set to 2.");
194 property_set("qemu.gles", "2");
195 } else {
196 ALOGD("Emulator without GPU support detected. "
197 "Fallback to legacy software renderer, qemu.gles is set to 0.");
198 property_set("qemu.gles", "0");
199 }
200}
201
Mathias Agopianada798b2012-02-13 17:09:30 -0800202void* Loader::open(egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700203{
Jesse Hall1508ae62017-01-19 17:43:26 -0800204 ATRACE_CALL();
205
Mathias Agopiande586972009-05-28 17:39:03 -0700206 void* dso;
Mathias Agopiande586972009-05-28 17:39:03 -0700207 driver_t* hnd = 0;
Jesse Hall94cdba92013-07-11 09:40:35 -0700208
bohu69e5b1a2016-02-19 17:15:20 -0800209 setEmulatorGlesValue();
210
Mathias Agopian99381422013-04-23 20:52:29 +0200211 dso = load_driver("GLES", cnx, EGL | GLESv1_CM | GLESv2);
212 if (dso) {
213 hnd = new driver_t(dso);
214 } else {
215 // Always load EGL first
216 dso = load_driver("EGL", cnx, EGL);
Mathias Agopiande586972009-05-28 17:39:03 -0700217 if (dso) {
218 hnd = new driver_t(dso);
Mathias Agopian99381422013-04-23 20:52:29 +0200219 hnd->set( load_driver("GLESv1_CM", cnx, GLESv1_CM), GLESv1_CM );
220 hnd->set( load_driver("GLESv2", cnx, GLESv2), GLESv2 );
Mathias Agopiande586972009-05-28 17:39:03 -0700221 }
222 }
223
Mathias Agopian99381422013-04-23 20:52:29 +0200224 LOG_ALWAYS_FATAL_IF(!hnd, "couldn't find an OpenGL ES implementation");
Jesse Hallc07b5202013-07-04 12:08:16 -0700225
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700226 cnx->libEgl = load_wrapper(EGL_WRAPPER_DIR "/libEGL.so");
227 cnx->libGles2 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv2.so");
228 cnx->libGles1 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv1_CM.so");
229
Michael Chockc0ec5e22014-01-27 08:14:33 -0800230 LOG_ALWAYS_FATAL_IF(!cnx->libEgl,
231 "couldn't load system EGL wrapper libraries");
232
Jesse Hallc07b5202013-07-04 12:08:16 -0700233 LOG_ALWAYS_FATAL_IF(!cnx->libGles2 || !cnx->libGles1,
234 "couldn't load system OpenGL ES wrapper libraries");
235
Mathias Agopiande586972009-05-28 17:39:03 -0700236 return (void*)hnd;
237}
238
Mathias Agopian65421432017-03-08 11:49:05 -0800239void Loader::close(void* driver)
Mathias Agopiande586972009-05-28 17:39:03 -0700240{
241 driver_t* hnd = (driver_t*)driver;
242 delete hnd;
Mathias Agopiande586972009-05-28 17:39:03 -0700243}
244
Jesse Hall94cdba92013-07-11 09:40:35 -0700245void Loader::init_api(void* dso,
246 char const * const * api,
247 __eglMustCastToProperFunctionPointerType* curr,
248 getProcAddressType getProcAddress)
Mathias Agopiande586972009-05-28 17:39:03 -0700249{
Jesse Hall1508ae62017-01-19 17:43:26 -0800250 ATRACE_CALL();
251
Mathias Agopian7773c432012-02-13 20:06:08 -0800252 const ssize_t SIZE = 256;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700253 char scrap[SIZE];
Mathias Agopiande586972009-05-28 17:39:03 -0700254 while (*api) {
255 char const * name = *api;
Jesse Hall94cdba92013-07-11 09:40:35 -0700256 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700257 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
258 if (f == NULL) {
259 // couldn't find the entry-point, use eglGetProcAddress()
260 f = getProcAddress(name);
261 }
262 if (f == NULL) {
263 // Try without the OES postfix
264 ssize_t index = ssize_t(strlen(name)) - 3;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700265 if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) {
Mathias Agopiande586972009-05-28 17:39:03 -0700266 strncpy(scrap, name, index);
267 scrap[index] = 0;
268 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000269 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700270 }
271 }
272 if (f == NULL) {
273 // Try with the OES postfix
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700274 ssize_t index = ssize_t(strlen(name)) - 3;
275 if (index>0 && strcmp(name+index, "OES")) {
276 snprintf(scrap, SIZE, "%sOES", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700277 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000278 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700279 }
280 }
281 if (f == NULL) {
Steve Block9d453682011-12-20 16:23:08 +0000282 //ALOGD("%s", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700283 f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented;
Mathias Agopian48d438d2012-01-28 21:44:00 -0800284
285 /*
286 * GL_EXT_debug_label is special, we always report it as
287 * supported, it's handled by GLES_trace. If GLES_trace is not
288 * enabled, then these are no-ops.
289 */
290 if (!strcmp(name, "glInsertEventMarkerEXT")) {
291 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
292 } else if (!strcmp(name, "glPushGroupMarkerEXT")) {
293 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
294 } else if (!strcmp(name, "glPopGroupMarkerEXT")) {
295 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
296 }
Mathias Agopiande586972009-05-28 17:39:03 -0700297 }
298 *curr++ = f;
299 api++;
300 }
301}
302
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800303static void* load_system_driver(const char* kind) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800304 ATRACE_CALL();
Mathias Agopian99381422013-04-23 20:52:29 +0200305 class MatchFile {
306 public:
Mathias Agopian65421432017-03-08 11:49:05 -0800307 static std::string find(const char* kind) {
308 std::string result;
Nicolas Capens776951f2015-11-06 10:10:21 -0500309 int emulationStatus = checkGlesEmulationStatus();
310 switch (emulationStatus) {
311 case 0:
Nicolas Capens776951f2015-11-06 10:10:21 -0500312#if defined(__LP64__)
Mathias Agopian65421432017-03-08 11:49:05 -0800313 result = "/system/lib64/egl/libGLES_android.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500314#else
Mathias Agopian65421432017-03-08 11:49:05 -0800315 result = "/system/lib/egl/libGLES_android.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500316#endif
317 return result;
318 case 1:
319 // Use host-side OpenGL through the "emulation" library
320#if defined(__LP64__)
Mathias Agopian65421432017-03-08 11:49:05 -0800321 result = std::string("/system/lib64/egl/lib") + kind + "_emulation.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500322#else
Mathias Agopian65421432017-03-08 11:49:05 -0800323 result = std::string("/system/lib/egl/lib") + kind + "_emulation.so";
Nicolas Capens776951f2015-11-06 10:10:21 -0500324#endif
325 return result;
326 default:
327 // Not in emulator, or use other guest-side implementation
328 break;
329 }
330
Mathias Agopian65421432017-03-08 11:49:05 -0800331 std::string pattern = std::string("lib") + kind;
Mathias Agopian99381422013-04-23 20:52:29 +0200332 const char* const searchPaths[] = {
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800333#if defined(__LP64__)
334 "/vendor/lib64/egl",
335 "/system/lib64/egl"
336#else
Mathias Agopian99381422013-04-23 20:52:29 +0200337 "/vendor/lib/egl",
338 "/system/lib/egl"
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800339#endif
Mathias Agopian99381422013-04-23 20:52:29 +0200340 };
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700341
Mathias Agopian99381422013-04-23 20:52:29 +0200342 // first, we search for the exact name of the GLES userspace
343 // driver in both locations.
344 // i.e.:
345 // libGLES.so, or:
346 // libEGL.so, libGLESv1_CM.so, libGLESv2.so
347
348 for (size_t i=0 ; i<NELEM(searchPaths) ; i++) {
349 if (find(result, pattern, searchPaths[i], true)) {
350 return result;
351 }
352 }
353
354 // for compatibility with the old "egl.cfg" naming convention
355 // we look for files that match:
356 // libGLES_*.so, or:
357 // libEGL_*.so, libGLESv1_CM_*.so, libGLESv2_*.so
358
359 pattern.append("_");
360 for (size_t i=0 ; i<NELEM(searchPaths) ; i++) {
361 if (find(result, pattern, searchPaths[i], false)) {
362 return result;
363 }
364 }
365
366 // we didn't find the driver. gah.
367 result.clear();
368 return result;
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700369 }
Mathias Agopian99381422013-04-23 20:52:29 +0200370
371 private:
Mathias Agopian65421432017-03-08 11:49:05 -0800372 static bool find(std::string& result,
373 const std::string& pattern, const char* const search, bool exact) {
Mathias Agopian99381422013-04-23 20:52:29 +0200374 if (exact) {
Mathias Agopian65421432017-03-08 11:49:05 -0800375 std::string absolutePath = std::string(search) + "/" + pattern;
376 if (!access(absolutePath.c_str(), R_OK)) {
Mathias Agopian99381422013-04-23 20:52:29 +0200377 result = absolutePath;
378 return true;
379 }
380 return false;
381 }
382
383 DIR* d = opendir(search);
384 if (d != NULL) {
385 struct dirent cur;
386 struct dirent* e;
387 while (readdir_r(d, &cur, &e) == 0 && e) {
388 if (e->d_type == DT_DIR) {
389 continue;
390 }
391 if (!strcmp(e->d_name, "libGLES_android.so")) {
392 // always skip the software renderer
393 continue;
394 }
Mathias Agopian65421432017-03-08 11:49:05 -0800395 if (strstr(e->d_name, pattern.c_str()) == e->d_name) {
Mathias Agopian99381422013-04-23 20:52:29 +0200396 if (!strcmp(e->d_name + strlen(e->d_name) - 3, ".so")) {
Mathias Agopian65421432017-03-08 11:49:05 -0800397 result = std::string(search) + "/" + e->d_name;
Mathias Agopian99381422013-04-23 20:52:29 +0200398 closedir(d);
399 return true;
400 }
401 }
402 }
403 closedir(d);
404 }
405 return false;
406 }
407 };
408
409
Mathias Agopian65421432017-03-08 11:49:05 -0800410 std::string absolutePath = MatchFile::find(kind);
411 if (absolutePath.empty()) {
Mathias Agopian99381422013-04-23 20:52:29 +0200412 // this happens often, we don't want to log an error
413 return 0;
Mathias Agopian8c173842009-09-20 16:01:02 -0700414 }
Mathias Agopian65421432017-03-08 11:49:05 -0800415 const char* const driver_absolute_path = absolutePath.c_str();
Mathias Agopiande586972009-05-28 17:39:03 -0700416
Jesse Hall1508ae62017-01-19 17:43:26 -0800417 void* dso = do_dlopen(driver_absolute_path, RTLD_NOW | RTLD_LOCAL);
Mathias Agopian8c173842009-09-20 16:01:02 -0700418 if (dso == 0) {
419 const char* err = dlerror();
Mathias Agopian65421432017-03-08 11:49:05 -0800420 ALOGE("load_driver(%s): %s", driver_absolute_path, err ? err : "unknown");
Mathias Agopian8c173842009-09-20 16:01:02 -0700421 return 0;
422 }
423
Steve Block9d453682011-12-20 16:23:08 +0000424 ALOGD("loaded %s", driver_absolute_path);
Mathias Agopianbaca89c2009-08-20 19:09:34 -0700425
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800426 return dso;
427}
428
Jesse Hall1508ae62017-01-19 17:43:26 -0800429static void* do_android_dlopen_ext(const char* path, int mode, const android_dlextinfo* info) {
430 ATRACE_CALL();
431 return android_dlopen_ext(path, mode, info);
432}
433
Mathias Agopian311b4792017-02-28 15:00:49 -0800434static const char* HAL_SUBNAME_KEY_PROPERTIES[2] = {
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800435 "ro.hardware.egl",
436 "ro.board.platform",
Mathias Agopian311b4792017-02-28 15:00:49 -0800437};
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800438
439static void* load_updated_driver(const char* kind, android_namespace_t* ns) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800440 ATRACE_CALL();
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800441 const android_dlextinfo dlextinfo = {
442 .flags = ANDROID_DLEXT_USE_NAMESPACE,
443 .library_namespace = ns,
444 };
445 void* so = nullptr;
446 char prop[PROPERTY_VALUE_MAX + 1];
447 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
448 if (property_get(key, prop, nullptr) > 0) {
Mathias Agopian65421432017-03-08 11:49:05 -0800449 std::string name = std::string("lib") + kind + "_" + prop + ".so";
450 so = do_android_dlopen_ext(name.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
Mathias Agopian311b4792017-02-28 15:00:49 -0800451 if (so) {
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800452 return so;
Mathias Agopian311b4792017-02-28 15:00:49 -0800453 }
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800454 }
455 }
456 return nullptr;
457}
458
459void *Loader::load_driver(const char* kind,
460 egl_connection_t* cnx, uint32_t mask)
461{
Jesse Hall1508ae62017-01-19 17:43:26 -0800462 ATRACE_CALL();
463
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800464 void* dso = nullptr;
Mathias Agopian991d2542017-02-06 13:51:32 -0800465 android_namespace_t* ns = android_getDriverNamespace();
466 if (ns) {
467 dso = load_updated_driver(kind, ns);
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800468 }
469 if (!dso) {
470 dso = load_system_driver(kind);
471 if (!dso)
472 return NULL;
473 }
474
Mathias Agopiande586972009-05-28 17:39:03 -0700475 if (mask & EGL) {
476 getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress");
477
Jesse Hall94cdba92013-07-11 09:40:35 -0700478 ALOGE_IF(!getProcAddress,
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800479 "can't find eglGetProcAddress() in EGL driver library");
Mathias Agopiande586972009-05-28 17:39:03 -0700480
Mathias Agopian618fa102009-10-14 02:06:37 -0700481 egl_t* egl = &cnx->egl;
Mathias Agopiande586972009-05-28 17:39:03 -0700482 __eglMustCastToProperFunctionPointerType* curr =
483 (__eglMustCastToProperFunctionPointerType*)egl;
484 char const * const * api = egl_names;
485 while (*api) {
486 char const * name = *api;
Jesse Hall94cdba92013-07-11 09:40:35 -0700487 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700488 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
489 if (f == NULL) {
490 // couldn't find the entry-point, use eglGetProcAddress()
491 f = getProcAddress(name);
492 if (f == NULL) {
493 f = (__eglMustCastToProperFunctionPointerType)0;
494 }
495 }
496 *curr++ = f;
497 api++;
498 }
499 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700500
Mathias Agopiande586972009-05-28 17:39:03 -0700501 if (mask & GLESv1_CM) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700502 init_api(dso, gl_names,
503 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800504 &cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl,
Mathias Agopian618fa102009-10-14 02:06:37 -0700505 getProcAddress);
Mathias Agopiande586972009-05-28 17:39:03 -0700506 }
507
508 if (mask & GLESv2) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700509 init_api(dso, gl_names,
510 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800511 &cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl,
Mathias Agopiande586972009-05-28 17:39:03 -0700512 getProcAddress);
513 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700514
Mathias Agopiande586972009-05-28 17:39:03 -0700515 return dso;
516}
517
518// ----------------------------------------------------------------------------
519}; // namespace android
520// ----------------------------------------------------------------------------