Loader.cpp revision c2466e6f005e9cbeea7764e9d8864fa6bca17633
1/* 2 ** Copyright 2007, The Android Open Source Project 3 ** 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 7 ** 8 ** http://www.apache.org/licenses/LICENSE-2.0 9 ** 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. 15 */ 16 17#include <ctype.h> 18#include <stdlib.h> 19#include <stdio.h> 20#include <string.h> 21#include <errno.h> 22#include <dlfcn.h> 23#include <limits.h> 24#include <dirent.h> 25 26#include <cutils/log.h> 27#include <cutils/properties.h> 28 29#include <EGL/egl.h> 30 31#include "../glestrace.h" 32 33#include "egldefs.h" 34#include "Loader.h" 35 36// ---------------------------------------------------------------------------- 37namespace android { 38// ---------------------------------------------------------------------------- 39 40 41/* 42 * 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: 59 * 60 * /{vendor|system}/lib/egl/lib{GLES | [EGL|GLESv1_CM|GLESv2]}_*.so 61 * 62 */ 63 64ANDROID_SINGLETON_STATIC_INSTANCE( Loader ) 65 66/* This function is called to check whether we run inside the emulator, 67 * and if this is the case whether GLES GPU emulation is supported. 68 * 69 * Returned values are: 70 * -1 -> not running inside the emulator 71 * 0 -> running inside the emulator, but GPU emulation not supported 72 * 1 -> running inside the emulator, GPU emulation is supported 73 * through the "emulation" config. 74 */ 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 */ 95 property_get("ro.kernel.qemu.gles",prop,"0"); 96 return atoi(prop); 97} 98 99// ---------------------------------------------------------------------------- 100 101static char const * getProcessCmdline() { 102 long pid = getpid(); 103 char procPath[128]; 104 snprintf(procPath, 128, "/proc/%ld/cmdline", pid); 105 FILE * file = fopen(procPath, "r"); 106 if (file) { 107 static char cmdline[256]; 108 char *str = fgets(cmdline, sizeof(cmdline) - 1, file); 109 fclose(file); 110 if (str) { 111 return cmdline; 112 } 113 } 114 return NULL; 115} 116 117// ---------------------------------------------------------------------------- 118 119Loader::driver_t::driver_t(void* gles) 120{ 121 dso[0] = gles; 122 for (size_t i=1 ; i<NELEM(dso) ; i++) 123 dso[i] = 0; 124} 125 126Loader::driver_t::~driver_t() 127{ 128 for (size_t i=0 ; i<NELEM(dso) ; i++) { 129 if (dso[i]) { 130 dlclose(dso[i]); 131 dso[i] = 0; 132 } 133 } 134} 135 136status_t Loader::driver_t::set(void* hnd, int32_t api) 137{ 138 switch (api) { 139 case EGL: 140 dso[0] = hnd; 141 break; 142 case GLESv1_CM: 143 dso[1] = hnd; 144 break; 145 case GLESv2: 146 dso[2] = hnd; 147 break; 148 default: 149 return BAD_INDEX; 150 } 151 return NO_ERROR; 152} 153 154// ---------------------------------------------------------------------------- 155 156Loader::Loader() 157 : getProcAddress(NULL) { 158} 159 160Loader::~Loader() { 161 GLTrace_stop(); 162} 163 164static void* load_wrapper(const char* path) { 165 void* so = dlopen(path, RTLD_NOW | RTLD_LOCAL); 166 ALOGE_IF(!so, "dlopen(\"%s\") failed: %s", path, dlerror()); 167 return so; 168} 169 170#ifndef EGL_WRAPPER_DIR 171#if defined(__LP64__) 172#define EGL_WRAPPER_DIR "/system/lib64" 173#else 174#define EGL_WRAPPER_DIR "/system/lib" 175#endif 176#endif 177 178void* Loader::open(egl_connection_t* cnx) 179{ 180 void* dso; 181 driver_t* hnd = 0; 182 183 dso = load_driver("GLES", cnx, EGL | GLESv1_CM | GLESv2); 184 if (dso) { 185 hnd = new driver_t(dso); 186 } else { 187 // Always load EGL first 188 dso = load_driver("EGL", cnx, EGL); 189 if (dso) { 190 hnd = new driver_t(dso); 191 hnd->set( load_driver("GLESv1_CM", cnx, GLESv1_CM), GLESv1_CM ); 192 hnd->set( load_driver("GLESv2", cnx, GLESv2), GLESv2 ); 193 } 194 } 195 196 LOG_ALWAYS_FATAL_IF(!hnd, "couldn't find an OpenGL ES implementation"); 197 198 cnx->libEgl = load_wrapper(EGL_WRAPPER_DIR "/libEGL.so"); 199 cnx->libGles2 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv2.so"); 200 cnx->libGles1 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv1_CM.so"); 201 202 LOG_ALWAYS_FATAL_IF(!cnx->libEgl, 203 "couldn't load system EGL wrapper libraries"); 204 205 LOG_ALWAYS_FATAL_IF(!cnx->libGles2 || !cnx->libGles1, 206 "couldn't load system OpenGL ES wrapper libraries"); 207 208 return (void*)hnd; 209} 210 211status_t Loader::close(void* driver) 212{ 213 driver_t* hnd = (driver_t*)driver; 214 delete hnd; 215 return NO_ERROR; 216} 217 218void Loader::init_api(void* dso, 219 char const * const * api, 220 __eglMustCastToProperFunctionPointerType* curr, 221 getProcAddressType getProcAddress) 222{ 223 const ssize_t SIZE = 256; 224 char scrap[SIZE]; 225 while (*api) { 226 char const * name = *api; 227 __eglMustCastToProperFunctionPointerType f = 228 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name); 229 if (f == NULL) { 230 // couldn't find the entry-point, use eglGetProcAddress() 231 f = getProcAddress(name); 232 } 233 if (f == NULL) { 234 // Try without the OES postfix 235 ssize_t index = ssize_t(strlen(name)) - 3; 236 if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) { 237 strncpy(scrap, name, index); 238 scrap[index] = 0; 239 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap); 240 //ALOGD_IF(f, "found <%s> instead", scrap); 241 } 242 } 243 if (f == NULL) { 244 // Try with the OES postfix 245 ssize_t index = ssize_t(strlen(name)) - 3; 246 if (index>0 && strcmp(name+index, "OES")) { 247 snprintf(scrap, SIZE, "%sOES", name); 248 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap); 249 //ALOGD_IF(f, "found <%s> instead", scrap); 250 } 251 } 252 if (f == NULL) { 253 //ALOGD("%s", name); 254 f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented; 255 256 /* 257 * GL_EXT_debug_label is special, we always report it as 258 * supported, it's handled by GLES_trace. If GLES_trace is not 259 * enabled, then these are no-ops. 260 */ 261 if (!strcmp(name, "glInsertEventMarkerEXT")) { 262 f = (__eglMustCastToProperFunctionPointerType)gl_noop; 263 } else if (!strcmp(name, "glPushGroupMarkerEXT")) { 264 f = (__eglMustCastToProperFunctionPointerType)gl_noop; 265 } else if (!strcmp(name, "glPopGroupMarkerEXT")) { 266 f = (__eglMustCastToProperFunctionPointerType)gl_noop; 267 } 268 } 269 *curr++ = f; 270 api++; 271 } 272} 273 274void *Loader::load_driver(const char* kind, 275 egl_connection_t* cnx, uint32_t mask) 276{ 277 class MatchFile { 278 public: 279 static String8 find(const char* kind) { 280 String8 result; 281 String8 pattern; 282 pattern.appendFormat("lib%s", kind); 283 const char* const searchPaths[] = { 284#if defined(__LP64__) 285 "/vendor/lib64/egl", 286 "/system/lib64/egl" 287#else 288 "/vendor/lib/egl", 289 "/system/lib/egl" 290#endif 291 }; 292 293 // first, we search for the exact name of the GLES userspace 294 // driver in both locations. 295 // i.e.: 296 // libGLES.so, or: 297 // libEGL.so, libGLESv1_CM.so, libGLESv2.so 298 299 for (size_t i=0 ; i<NELEM(searchPaths) ; i++) { 300 if (find(result, pattern, searchPaths[i], true)) { 301 return result; 302 } 303 } 304 305 // for compatibility with the old "egl.cfg" naming convention 306 // we look for files that match: 307 // libGLES_*.so, or: 308 // libEGL_*.so, libGLESv1_CM_*.so, libGLESv2_*.so 309 310 pattern.append("_"); 311 for (size_t i=0 ; i<NELEM(searchPaths) ; i++) { 312 if (find(result, pattern, searchPaths[i], false)) { 313 return result; 314 } 315 } 316 317 // we didn't find the driver. gah. 318 result.clear(); 319 return result; 320 } 321 322 private: 323 static bool find(String8& result, 324 const String8& pattern, const char* const search, bool exact) { 325 326 // in the emulator case, we just return the hardcoded name 327 // of the software renderer. 328 if (checkGlesEmulationStatus() == 0) { 329 ALOGD("Emulator without GPU support detected. " 330 "Fallback to software renderer."); 331#if defined(__LP64__) 332 result.setTo("/system/lib64/egl/libGLES_android.so"); 333#else 334 result.setTo("/system/lib/egl/libGLES_android.so"); 335#endif 336 return true; 337 } 338 339 if (exact) { 340 String8 absolutePath; 341 absolutePath.appendFormat("%s/%s.so", search, pattern.string()); 342 if (!access(absolutePath.string(), R_OK)) { 343 result = absolutePath; 344 return true; 345 } 346 return false; 347 } 348 349 DIR* d = opendir(search); 350 if (d != NULL) { 351 struct dirent cur; 352 struct dirent* e; 353 while (readdir_r(d, &cur, &e) == 0 && e) { 354 if (e->d_type == DT_DIR) { 355 continue; 356 } 357 if (!strcmp(e->d_name, "libGLES_android.so")) { 358 // always skip the software renderer 359 continue; 360 } 361 if (strstr(e->d_name, pattern.string()) == e->d_name) { 362 if (!strcmp(e->d_name + strlen(e->d_name) - 3, ".so")) { 363 result.clear(); 364 result.appendFormat("%s/%s", search, e->d_name); 365 closedir(d); 366 return true; 367 } 368 } 369 } 370 closedir(d); 371 } 372 return false; 373 } 374 }; 375 376 377 String8 absolutePath = MatchFile::find(kind); 378 if (absolutePath.isEmpty()) { 379 // this happens often, we don't want to log an error 380 return 0; 381 } 382 const char* const driver_absolute_path = absolutePath.string(); 383 384 void* dso = dlopen(driver_absolute_path, RTLD_NOW | RTLD_LOCAL); 385 if (dso == 0) { 386 const char* err = dlerror(); 387 ALOGE("load_driver(%s): %s", driver_absolute_path, err?err:"unknown"); 388 return 0; 389 } 390 391 ALOGD("loaded %s", driver_absolute_path); 392 393 if (mask & EGL) { 394 getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress"); 395 396 ALOGE_IF(!getProcAddress, 397 "can't find eglGetProcAddress() in %s", driver_absolute_path); 398 399 egl_t* egl = &cnx->egl; 400 __eglMustCastToProperFunctionPointerType* curr = 401 (__eglMustCastToProperFunctionPointerType*)egl; 402 char const * const * api = egl_names; 403 while (*api) { 404 char const * name = *api; 405 __eglMustCastToProperFunctionPointerType f = 406 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name); 407 if (f == NULL) { 408 // couldn't find the entry-point, use eglGetProcAddress() 409 f = getProcAddress(name); 410 if (f == NULL) { 411 f = (__eglMustCastToProperFunctionPointerType)0; 412 } 413 } 414 *curr++ = f; 415 api++; 416 } 417 } 418 419 if (mask & GLESv1_CM) { 420 init_api(dso, gl_names, 421 (__eglMustCastToProperFunctionPointerType*) 422 &cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl, 423 getProcAddress); 424 } 425 426 if (mask & GLESv2) { 427 init_api(dso, gl_names, 428 (__eglMustCastToProperFunctionPointerType*) 429 &cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl, 430 getProcAddress); 431 } 432 433 return dso; 434} 435 436// ---------------------------------------------------------------------------- 437}; // namespace android 438// ---------------------------------------------------------------------------- 439