blob: ac455cdd70b503dd217bf514e492916bad129923 [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
17#include <ctype.h>
Mathias Agopian99381422013-04-23 20:52:29 +020018#include <dirent.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070019#include <dlfcn.h>
20#include <errno.h>
21#include <limits.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
Mathias Agopiande586972009-05-28 17:39:03 -070025
David 'Digit' Turner80b30c22011-08-26 17:38:47 +020026#include <cutils/properties.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070027#include <log/log.h>
Mathias Agopiande586972009-05-28 17:39:03 -070028
29#include <EGL/egl.h>
30
Mathias Agopian1cadb252011-05-23 17:26:14 -070031#include "egldefs.h"
Mathias Agopian1cadb252011-05-23 17:26:14 -070032#include "Loader.h"
Mathias Agopiande586972009-05-28 17:39:03 -070033
34// ----------------------------------------------------------------------------
35namespace android {
36// ----------------------------------------------------------------------------
37
38
39/*
Mathias Agopian99381422013-04-23 20:52:29 +020040 * EGL userspace drivers must be provided either:
41 * - as a single library:
42 * /vendor/lib/egl/libGLES.so
43 *
44 * - as separate libraries:
45 * /vendor/lib/egl/libEGL.so
46 * /vendor/lib/egl/libGLESv1_CM.so
47 * /vendor/lib/egl/libGLESv2.so
48 *
49 * The software renderer for the emulator must be provided as a single
50 * library at:
51 *
52 * /system/lib/egl/libGLES_android.so
53 *
54 *
55 * For backward compatibility and to facilitate the transition to
56 * this new naming scheme, the loader will additionally look for:
Jesse Hall94cdba92013-07-11 09:40:35 -070057 *
Mathias Agopian99381422013-04-23 20:52:29 +020058 * /{vendor|system}/lib/egl/lib{GLES | [EGL|GLESv1_CM|GLESv2]}_*.so
Jesse Hall94cdba92013-07-11 09:40:35 -070059 *
Mathias Agopiande586972009-05-28 17:39:03 -070060 */
61
62ANDROID_SINGLETON_STATIC_INSTANCE( Loader )
63
David 'Digit' Turner80b30c22011-08-26 17:38:47 +020064/* This function is called to check whether we run inside the emulator,
65 * and if this is the case whether GLES GPU emulation is supported.
66 *
67 * Returned values are:
68 * -1 -> not running inside the emulator
69 * 0 -> running inside the emulator, but GPU emulation not supported
70 * 1 -> running inside the emulator, GPU emulation is supported
Nicolas Capens776951f2015-11-06 10:10:21 -050071 * through the "emulation" host-side OpenGL ES implementation.
72 * 2 -> running inside the emulator, GPU emulation is supported
73 * through a guest-side vendor driver's OpenGL ES implementation.
David 'Digit' Turner80b30c22011-08-26 17:38:47 +020074 */
75static int
76checkGlesEmulationStatus(void)
77{
78 /* We're going to check for the following kernel parameters:
79 *
80 * qemu=1 -> tells us that we run inside the emulator
81 * android.qemu.gles=<number> -> tells us the GLES GPU emulation status
82 *
83 * Note that we will return <number> if we find it. This let us support
84 * more additionnal emulation modes in the future.
85 */
86 char prop[PROPERTY_VALUE_MAX];
87 int result = -1;
88
89 /* First, check for qemu=1 */
90 property_get("ro.kernel.qemu",prop,"0");
91 if (atoi(prop) != 1)
92 return -1;
93
94 /* We are in the emulator, get GPU status value */
bohu69e5b1a2016-02-19 17:15:20 -080095 property_get("qemu.gles",prop,"0");
David 'Digit' Turner80b30c22011-08-26 17:38:47 +020096 return atoi(prop);
97}
98
Mathias Agopiande586972009-05-28 17:39:03 -070099// ----------------------------------------------------------------------------
100
Jesse Hall94cdba92013-07-11 09:40:35 -0700101Loader::driver_t::driver_t(void* gles)
Mathias Agopiande586972009-05-28 17:39:03 -0700102{
103 dso[0] = gles;
104 for (size_t i=1 ; i<NELEM(dso) ; i++)
105 dso[i] = 0;
106}
107
Jesse Hall94cdba92013-07-11 09:40:35 -0700108Loader::driver_t::~driver_t()
Mathias Agopiande586972009-05-28 17:39:03 -0700109{
110 for (size_t i=0 ; i<NELEM(dso) ; i++) {
111 if (dso[i]) {
112 dlclose(dso[i]);
113 dso[i] = 0;
114 }
115 }
116}
117
118status_t Loader::driver_t::set(void* hnd, int32_t api)
119{
120 switch (api) {
121 case EGL:
122 dso[0] = hnd;
123 break;
124 case GLESv1_CM:
125 dso[1] = hnd;
126 break;
127 case GLESv2:
128 dso[2] = hnd;
129 break;
130 default:
131 return BAD_INDEX;
132 }
133 return NO_ERROR;
134}
135
136// ----------------------------------------------------------------------------
137
Mathias Agopiande586972009-05-28 17:39:03 -0700138Loader::Loader()
Mathias Agopian99381422013-04-23 20:52:29 +0200139 : getProcAddress(NULL) {
Mathias Agopiande586972009-05-28 17:39:03 -0700140}
141
Mathias Agopian99381422013-04-23 20:52:29 +0200142Loader::~Loader() {
Mathias Agopiande586972009-05-28 17:39:03 -0700143}
144
Jesse Hallc07b5202013-07-04 12:08:16 -0700145static void* load_wrapper(const char* path) {
146 void* so = dlopen(path, RTLD_NOW | RTLD_LOCAL);
147 ALOGE_IF(!so, "dlopen(\"%s\") failed: %s", path, dlerror());
148 return so;
149}
150
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700151#ifndef EGL_WRAPPER_DIR
152#if defined(__LP64__)
153#define EGL_WRAPPER_DIR "/system/lib64"
154#else
155#define EGL_WRAPPER_DIR "/system/lib"
156#endif
157#endif
158
bohu69e5b1a2016-02-19 17:15:20 -0800159static void setEmulatorGlesValue(void) {
160 char prop[PROPERTY_VALUE_MAX];
161 property_get("ro.kernel.qemu", prop, "0");
162 if (atoi(prop) != 1) return;
163
164 property_get("ro.kernel.qemu.gles",prop,"0");
165 if (atoi(prop) == 1) {
166 ALOGD("Emulator has host GPU support, qemu.gles is set to 1.");
167 property_set("qemu.gles", "1");
168 return;
169 }
170
171 // for now, checking the following
172 // directory is good enough for emulator system images
173 const char* vendor_lib_path =
174#if defined(__LP64__)
175 "/vendor/lib64/egl";
176#else
177 "/vendor/lib/egl";
178#endif
179
180 const bool has_vendor_lib = (access(vendor_lib_path, R_OK) == 0);
181 if (has_vendor_lib) {
182 ALOGD("Emulator has vendor provided software renderer, qemu.gles is set to 2.");
183 property_set("qemu.gles", "2");
184 } else {
185 ALOGD("Emulator without GPU support detected. "
186 "Fallback to legacy software renderer, qemu.gles is set to 0.");
187 property_set("qemu.gles", "0");
188 }
189}
190
Mathias Agopianada798b2012-02-13 17:09:30 -0800191void* Loader::open(egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700192{
Mathias Agopiande586972009-05-28 17:39:03 -0700193 void* dso;
Mathias Agopiande586972009-05-28 17:39:03 -0700194 driver_t* hnd = 0;
Jesse Hall94cdba92013-07-11 09:40:35 -0700195
bohu69e5b1a2016-02-19 17:15:20 -0800196 setEmulatorGlesValue();
197
Mathias Agopian99381422013-04-23 20:52:29 +0200198 dso = load_driver("GLES", cnx, EGL | GLESv1_CM | GLESv2);
199 if (dso) {
200 hnd = new driver_t(dso);
201 } else {
202 // Always load EGL first
203 dso = load_driver("EGL", cnx, EGL);
Mathias Agopiande586972009-05-28 17:39:03 -0700204 if (dso) {
205 hnd = new driver_t(dso);
Mathias Agopian99381422013-04-23 20:52:29 +0200206 hnd->set( load_driver("GLESv1_CM", cnx, GLESv1_CM), GLESv1_CM );
207 hnd->set( load_driver("GLESv2", cnx, GLESv2), GLESv2 );
Mathias Agopiande586972009-05-28 17:39:03 -0700208 }
209 }
210
Mathias Agopian99381422013-04-23 20:52:29 +0200211 LOG_ALWAYS_FATAL_IF(!hnd, "couldn't find an OpenGL ES implementation");
Jesse Hallc07b5202013-07-04 12:08:16 -0700212
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700213 cnx->libEgl = load_wrapper(EGL_WRAPPER_DIR "/libEGL.so");
214 cnx->libGles2 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv2.so");
215 cnx->libGles1 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv1_CM.so");
216
Michael Chockc0ec5e22014-01-27 08:14:33 -0800217 LOG_ALWAYS_FATAL_IF(!cnx->libEgl,
218 "couldn't load system EGL wrapper libraries");
219
Jesse Hallc07b5202013-07-04 12:08:16 -0700220 LOG_ALWAYS_FATAL_IF(!cnx->libGles2 || !cnx->libGles1,
221 "couldn't load system OpenGL ES wrapper libraries");
222
Mathias Agopiande586972009-05-28 17:39:03 -0700223 return (void*)hnd;
224}
225
226status_t Loader::close(void* driver)
227{
228 driver_t* hnd = (driver_t*)driver;
229 delete hnd;
230 return NO_ERROR;
231}
232
Jesse Hall94cdba92013-07-11 09:40:35 -0700233void Loader::init_api(void* dso,
234 char const * const * api,
235 __eglMustCastToProperFunctionPointerType* curr,
236 getProcAddressType getProcAddress)
Mathias Agopiande586972009-05-28 17:39:03 -0700237{
Mathias Agopian7773c432012-02-13 20:06:08 -0800238 const ssize_t SIZE = 256;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700239 char scrap[SIZE];
Mathias Agopiande586972009-05-28 17:39:03 -0700240 while (*api) {
241 char const * name = *api;
Jesse Hall94cdba92013-07-11 09:40:35 -0700242 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700243 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
244 if (f == NULL) {
245 // couldn't find the entry-point, use eglGetProcAddress()
246 f = getProcAddress(name);
247 }
248 if (f == NULL) {
249 // Try without the OES postfix
250 ssize_t index = ssize_t(strlen(name)) - 3;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700251 if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) {
Mathias Agopiande586972009-05-28 17:39:03 -0700252 strncpy(scrap, name, index);
253 scrap[index] = 0;
254 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000255 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700256 }
257 }
258 if (f == NULL) {
259 // Try with the OES postfix
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700260 ssize_t index = ssize_t(strlen(name)) - 3;
261 if (index>0 && strcmp(name+index, "OES")) {
262 snprintf(scrap, SIZE, "%sOES", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700263 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000264 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700265 }
266 }
267 if (f == NULL) {
Steve Block9d453682011-12-20 16:23:08 +0000268 //ALOGD("%s", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700269 f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented;
Mathias Agopian48d438d2012-01-28 21:44:00 -0800270
271 /*
272 * GL_EXT_debug_label is special, we always report it as
273 * supported, it's handled by GLES_trace. If GLES_trace is not
274 * enabled, then these are no-ops.
275 */
276 if (!strcmp(name, "glInsertEventMarkerEXT")) {
277 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
278 } else if (!strcmp(name, "glPushGroupMarkerEXT")) {
279 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
280 } else if (!strcmp(name, "glPopGroupMarkerEXT")) {
281 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
282 }
Mathias Agopiande586972009-05-28 17:39:03 -0700283 }
284 *curr++ = f;
285 api++;
286 }
287}
288
Mathias Agopian99381422013-04-23 20:52:29 +0200289void *Loader::load_driver(const char* kind,
Mathias Agopian618fa102009-10-14 02:06:37 -0700290 egl_connection_t* cnx, uint32_t mask)
Mathias Agopiande586972009-05-28 17:39:03 -0700291{
Mathias Agopian99381422013-04-23 20:52:29 +0200292 class MatchFile {
293 public:
294 static String8 find(const char* kind) {
295 String8 result;
Nicolas Capens776951f2015-11-06 10:10:21 -0500296 int emulationStatus = checkGlesEmulationStatus();
297 switch (emulationStatus) {
298 case 0:
Nicolas Capens776951f2015-11-06 10:10:21 -0500299#if defined(__LP64__)
300 result.setTo("/system/lib64/egl/libGLES_android.so");
301#else
302 result.setTo("/system/lib/egl/libGLES_android.so");
303#endif
304 return result;
305 case 1:
306 // Use host-side OpenGL through the "emulation" library
307#if defined(__LP64__)
308 result.appendFormat("/system/lib64/egl/lib%s_emulation.so", kind);
309#else
310 result.appendFormat("/system/lib/egl/lib%s_emulation.so", kind);
311#endif
312 return result;
313 default:
314 // Not in emulator, or use other guest-side implementation
315 break;
316 }
317
Mathias Agopian99381422013-04-23 20:52:29 +0200318 String8 pattern;
319 pattern.appendFormat("lib%s", kind);
320 const char* const searchPaths[] = {
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800321#if defined(__LP64__)
322 "/vendor/lib64/egl",
323 "/system/lib64/egl"
324#else
Mathias Agopian99381422013-04-23 20:52:29 +0200325 "/vendor/lib/egl",
326 "/system/lib/egl"
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800327#endif
Mathias Agopian99381422013-04-23 20:52:29 +0200328 };
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700329
Mathias Agopian99381422013-04-23 20:52:29 +0200330 // first, we search for the exact name of the GLES userspace
331 // driver in both locations.
332 // i.e.:
333 // libGLES.so, or:
334 // libEGL.so, libGLESv1_CM.so, libGLESv2.so
335
336 for (size_t i=0 ; i<NELEM(searchPaths) ; i++) {
337 if (find(result, pattern, searchPaths[i], true)) {
338 return result;
339 }
340 }
341
342 // for compatibility with the old "egl.cfg" naming convention
343 // we look for files that match:
344 // libGLES_*.so, or:
345 // libEGL_*.so, libGLESv1_CM_*.so, libGLESv2_*.so
346
347 pattern.append("_");
348 for (size_t i=0 ; i<NELEM(searchPaths) ; i++) {
349 if (find(result, pattern, searchPaths[i], false)) {
350 return result;
351 }
352 }
353
354 // we didn't find the driver. gah.
355 result.clear();
356 return result;
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700357 }
Mathias Agopian99381422013-04-23 20:52:29 +0200358
359 private:
360 static bool find(String8& result,
361 const String8& pattern, const char* const search, bool exact) {
Mathias Agopian99381422013-04-23 20:52:29 +0200362 if (exact) {
363 String8 absolutePath;
364 absolutePath.appendFormat("%s/%s.so", search, pattern.string());
365 if (!access(absolutePath.string(), R_OK)) {
366 result = absolutePath;
367 return true;
368 }
369 return false;
370 }
371
372 DIR* d = opendir(search);
373 if (d != NULL) {
374 struct dirent cur;
375 struct dirent* e;
376 while (readdir_r(d, &cur, &e) == 0 && e) {
377 if (e->d_type == DT_DIR) {
378 continue;
379 }
380 if (!strcmp(e->d_name, "libGLES_android.so")) {
381 // always skip the software renderer
382 continue;
383 }
384 if (strstr(e->d_name, pattern.string()) == e->d_name) {
385 if (!strcmp(e->d_name + strlen(e->d_name) - 3, ".so")) {
386 result.clear();
387 result.appendFormat("%s/%s", search, e->d_name);
388 closedir(d);
389 return true;
390 }
391 }
392 }
393 closedir(d);
394 }
395 return false;
396 }
397 };
398
399
400 String8 absolutePath = MatchFile::find(kind);
401 if (absolutePath.isEmpty()) {
402 // this happens often, we don't want to log an error
403 return 0;
Mathias Agopian8c173842009-09-20 16:01:02 -0700404 }
Mathias Agopian99381422013-04-23 20:52:29 +0200405 const char* const driver_absolute_path = absolutePath.string();
Mathias Agopiande586972009-05-28 17:39:03 -0700406
Mathias Agopian8c173842009-09-20 16:01:02 -0700407 void* dso = dlopen(driver_absolute_path, RTLD_NOW | RTLD_LOCAL);
408 if (dso == 0) {
409 const char* err = dlerror();
Steve Blocke6f43dd2012-01-06 19:20:56 +0000410 ALOGE("load_driver(%s): %s", driver_absolute_path, err?err:"unknown");
Mathias Agopian8c173842009-09-20 16:01:02 -0700411 return 0;
412 }
413
Steve Block9d453682011-12-20 16:23:08 +0000414 ALOGD("loaded %s", driver_absolute_path);
Mathias Agopianbaca89c2009-08-20 19:09:34 -0700415
Mathias Agopiande586972009-05-28 17:39:03 -0700416 if (mask & EGL) {
417 getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress");
418
Jesse Hall94cdba92013-07-11 09:40:35 -0700419 ALOGE_IF(!getProcAddress,
Mathias Agopian8c173842009-09-20 16:01:02 -0700420 "can't find eglGetProcAddress() in %s", driver_absolute_path);
Mathias Agopiande586972009-05-28 17:39:03 -0700421
Mathias Agopian618fa102009-10-14 02:06:37 -0700422 egl_t* egl = &cnx->egl;
Mathias Agopiande586972009-05-28 17:39:03 -0700423 __eglMustCastToProperFunctionPointerType* curr =
424 (__eglMustCastToProperFunctionPointerType*)egl;
425 char const * const * api = egl_names;
426 while (*api) {
427 char const * name = *api;
Jesse Hall94cdba92013-07-11 09:40:35 -0700428 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700429 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
430 if (f == NULL) {
431 // couldn't find the entry-point, use eglGetProcAddress()
432 f = getProcAddress(name);
433 if (f == NULL) {
434 f = (__eglMustCastToProperFunctionPointerType)0;
435 }
436 }
437 *curr++ = f;
438 api++;
439 }
440 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700441
Mathias Agopiande586972009-05-28 17:39:03 -0700442 if (mask & GLESv1_CM) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700443 init_api(dso, gl_names,
444 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800445 &cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl,
Mathias Agopian618fa102009-10-14 02:06:37 -0700446 getProcAddress);
Mathias Agopiande586972009-05-28 17:39:03 -0700447 }
448
449 if (mask & GLESv2) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700450 init_api(dso, gl_names,
451 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800452 &cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl,
Mathias Agopiande586972009-05-28 17:39:03 -0700453 getProcAddress);
454 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700455
Mathias Agopiande586972009-05-28 17:39:03 -0700456 return dso;
457}
458
459// ----------------------------------------------------------------------------
460}; // namespace android
461// ----------------------------------------------------------------------------