2 ** Copyright 2007, The Android Open Source Project
4 ** 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
8 ** http://www.apache.org/licenses/LICENSE-2.0
10 ** 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
14 ** limitations under the License.
26 #include <cutils/log.h>
27 #include <cutils/properties.h>
34 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
40 * EGL userspace drivers must be provided either:
41 * - as a single library:
42 * /vendor/lib/egl/libGLES.so
44 * - as separate libraries:
45 * /vendor/lib/egl/libEGL.so
46 * /vendor/lib/egl/libGLESv1_CM.so
47 * /vendor/lib/egl/libGLESv2.so
49 * The software renderer for the emulator must be provided as a single
52 * /system/lib/egl/libGLES_android.so
55 * For backward compatibility and to facilitate the transition to
56 * this new naming scheme, the loader will additionally look for:
58 * /{vendor|system}/lib/egl/lib{GLES | [EGL|GLESv1_CM|GLESv2]}_*.so
62 ANDROID_SINGLETON_STATIC_INSTANCE( Loader )
64 /* 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.
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
71 * 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.
76 checkGlesEmulationStatus(void)
78 /* We're going to check for the following kernel parameters:
80 * qemu=1 -> tells us that we run inside the emulator
81 * android.qemu.gles=<number> -> tells us the GLES GPU emulation status
83 * Note that we will return <number> if we find it. This let us support
84 * more additionnal emulation modes in the future.
86 char prop[PROPERTY_VALUE_MAX];
89 /* First, check for qemu=1 */
90 property_get("ro.kernel.qemu",prop,"0");
94 /* We are in the emulator, get GPU status value */
95 property_get("qemu.gles",prop,"0");
99 // ----------------------------------------------------------------------------
101 static char const * getProcessCmdline() {
104 snprintf(procPath, 128, "/proc/%ld/cmdline", pid);
105 FILE * file = fopen(procPath, "r");
107 static char cmdline[256];
108 char *str = fgets(cmdline, sizeof(cmdline) - 1, file);
117 // ----------------------------------------------------------------------------
119 Loader::driver_t::driver_t(void* gles)
122 for (size_t i=1 ; i<NELEM(dso) ; i++)
126 Loader::driver_t::~driver_t()
128 for (size_t i=0 ; i<NELEM(dso) ; i++) {
136 status_t Loader::driver_t::set(void* hnd, int32_t api)
154 // ----------------------------------------------------------------------------
157 : getProcAddress(NULL) {
163 static void* load_wrapper(const char* path) {
164 void* so = dlopen(path, RTLD_NOW | RTLD_LOCAL);
165 ALOGE_IF(!so, "dlopen(\"%s\") failed: %s", path, dlerror());
169 #ifndef EGL_WRAPPER_DIR
170 #if defined(__LP64__)
171 #define EGL_WRAPPER_DIR "/system/lib64"
173 #define EGL_WRAPPER_DIR "/system/lib"
177 static void setEmulatorGlesValue(void) {
178 char prop[PROPERTY_VALUE_MAX];
179 property_get("ro.kernel.qemu", prop, "0");
180 if (atoi(prop) != 1) return;
182 property_get("ro.kernel.qemu.gles",prop,"0");
183 if (atoi(prop) == 1) {
184 ALOGD("Emulator has host GPU support, qemu.gles is set to 1.");
185 property_set("qemu.gles", "1");
189 // for now, checking the following
190 // directory is good enough for emulator system images
191 const char* vendor_lib_path =
192 #if defined(__LP64__)
198 const bool has_vendor_lib = (access(vendor_lib_path, R_OK) == 0);
199 if (has_vendor_lib) {
200 ALOGD("Emulator has vendor provided software renderer, qemu.gles is set to 2.");
201 property_set("qemu.gles", "2");
203 ALOGD("Emulator without GPU support detected. "
204 "Fallback to legacy software renderer, qemu.gles is set to 0.");
205 property_set("qemu.gles", "0");
209 void* Loader::open(egl_connection_t* cnx)
214 setEmulatorGlesValue();
216 dso = load_driver("GLES", cnx, EGL | GLESv1_CM | GLESv2);
218 hnd = new driver_t(dso);
220 // Always load EGL first
221 dso = load_driver("EGL", cnx, EGL);
223 hnd = new driver_t(dso);
224 hnd->set( load_driver("GLESv1_CM", cnx, GLESv1_CM), GLESv1_CM );
225 hnd->set( load_driver("GLESv2", cnx, GLESv2), GLESv2 );
229 LOG_ALWAYS_FATAL_IF(!hnd, "couldn't find an OpenGL ES implementation");
231 cnx->libEgl = load_wrapper(EGL_WRAPPER_DIR "/libEGL.so");
232 cnx->libGles2 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv2.so");
233 cnx->libGles1 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv1_CM.so");
235 LOG_ALWAYS_FATAL_IF(!cnx->libEgl,
236 "couldn't load system EGL wrapper libraries");
238 LOG_ALWAYS_FATAL_IF(!cnx->libGles2 || !cnx->libGles1,
239 "couldn't load system OpenGL ES wrapper libraries");
244 status_t Loader::close(void* driver)
246 driver_t* hnd = (driver_t*)driver;
251 void Loader::init_api(void* dso,
252 char const * const * api,
253 __eglMustCastToProperFunctionPointerType* curr,
254 getProcAddressType getProcAddress)
256 const ssize_t SIZE = 256;
259 char const * name = *api;
260 __eglMustCastToProperFunctionPointerType f =
261 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
263 // couldn't find the entry-point, use eglGetProcAddress()
264 f = getProcAddress(name);
267 // Try without the OES postfix
268 ssize_t index = ssize_t(strlen(name)) - 3;
269 if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) {
270 strncpy(scrap, name, index);
272 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
273 //ALOGD_IF(f, "found <%s> instead", scrap);
277 // Try with the OES postfix
278 ssize_t index = ssize_t(strlen(name)) - 3;
279 if (index>0 && strcmp(name+index, "OES")) {
280 snprintf(scrap, SIZE, "%sOES", name);
281 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
282 //ALOGD_IF(f, "found <%s> instead", scrap);
287 f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented;
290 * GL_EXT_debug_label is special, we always report it as
291 * supported, it's handled by GLES_trace. If GLES_trace is not
292 * enabled, then these are no-ops.
294 if (!strcmp(name, "glInsertEventMarkerEXT")) {
295 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
296 } else if (!strcmp(name, "glPushGroupMarkerEXT")) {
297 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
298 } else if (!strcmp(name, "glPopGroupMarkerEXT")) {
299 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
307 void *Loader::load_driver(const char* kind,
308 egl_connection_t* cnx, uint32_t mask)
312 static String8 find(const char* kind) {
314 int emulationStatus = checkGlesEmulationStatus();
315 switch (emulationStatus) {
317 #if defined(__LP64__)
318 result.setTo("/system/lib64/egl/libGLES_android.so");
320 result.setTo("/system/lib/egl/libGLES_android.so");
324 // Use host-side OpenGL through the "emulation" library
325 #if defined(__LP64__)
326 result.appendFormat("/system/lib64/egl/lib%s_emulation.so", kind);
328 result.appendFormat("/system/lib/egl/lib%s_emulation.so", kind);
332 // Not in emulator, or use other guest-side implementation
337 pattern.appendFormat("lib%s", kind);
338 const char* const searchPaths[] = {
339 #if defined(__LP64__)
348 // first, we search for the exact name of the GLES userspace
349 // driver in both locations.
352 // libEGL.so, libGLESv1_CM.so, libGLESv2.so
354 for (size_t i=0 ; i<NELEM(searchPaths) ; i++) {
355 if (find(result, pattern, searchPaths[i], true)) {
360 // for compatibility with the old "egl.cfg" naming convention
361 // we look for files that match:
363 // libEGL_*.so, libGLESv1_CM_*.so, libGLESv2_*.so
366 for (size_t i=0 ; i<NELEM(searchPaths) ; i++) {
367 if (find(result, pattern, searchPaths[i], false)) {
372 // we didn't find the driver. gah.
378 static bool find(String8& result,
379 const String8& pattern, const char* const search, bool exact) {
381 String8 absolutePath;
382 absolutePath.appendFormat("%s/%s.so", search, pattern.string());
383 if (!access(absolutePath.string(), R_OK)) {
384 result = absolutePath;
390 DIR* d = opendir(search);
394 while (readdir_r(d, &cur, &e) == 0 && e) {
395 if (e->d_type == DT_DIR) {
398 if (!strcmp(e->d_name, "libGLES_android.so")) {
399 // always skip the software renderer
402 if (strstr(e->d_name, pattern.string()) == e->d_name) {
403 if (!strcmp(e->d_name + strlen(e->d_name) - 3, ".so")) {
405 result.appendFormat("%s/%s", search, e->d_name);
418 String8 absolutePath = MatchFile::find(kind);
419 if (absolutePath.isEmpty()) {
420 // this happens often, we don't want to log an error
423 const char* const driver_absolute_path = absolutePath.string();
425 void* dso = dlopen(driver_absolute_path, RTLD_NOW | RTLD_LOCAL);
427 const char* err = dlerror();
428 ALOGE("load_driver(%s): %s", driver_absolute_path, err?err:"unknown");
432 ALOGD("loaded %s", driver_absolute_path);
435 getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress");
437 ALOGE_IF(!getProcAddress,
438 "can't find eglGetProcAddress() in %s", driver_absolute_path);
440 egl_t* egl = &cnx->egl;
441 __eglMustCastToProperFunctionPointerType* curr =
442 (__eglMustCastToProperFunctionPointerType*)egl;
443 char const * const * api = egl_names;
445 char const * name = *api;
446 __eglMustCastToProperFunctionPointerType f =
447 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
449 // couldn't find the entry-point, use eglGetProcAddress()
450 f = getProcAddress(name);
452 f = (__eglMustCastToProperFunctionPointerType)0;
460 if (mask & GLESv1_CM) {
461 init_api(dso, gl_names,
462 (__eglMustCastToProperFunctionPointerType*)
463 &cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl,
468 init_api(dso, gl_names,
469 (__eglMustCastToProperFunctionPointerType*)
470 &cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl,
477 // ----------------------------------------------------------------------------
478 }; // namespace android
479 // ----------------------------------------------------------------------------