Loader.cpp revision c7fb44c8b95e585db29eb19dc1f893426f73b950
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//#define LOG_NDEBUG 0 18#define ATRACE_TAG ATRACE_TAG_GRAPHICS 19 20#include <array> 21#include <ctype.h> 22#include <dirent.h> 23#include <dlfcn.h> 24#include <errno.h> 25#include <limits.h> 26#include <stdio.h> 27#include <stdlib.h> 28#include <string.h> 29 30#include <android/dlext.h> 31#include <cutils/properties.h> 32#include <log/log.h> 33#include <utils/Trace.h> 34 35#include <EGL/egl.h> 36 37#include "egldefs.h" 38#include "Loader.h" 39 40// ---------------------------------------------------------------------------- 41namespace android { 42// ---------------------------------------------------------------------------- 43 44 45/* 46 * EGL userspace drivers must be provided either: 47 * - as a single library: 48 * /vendor/lib/egl/libGLES.so 49 * 50 * - as separate libraries: 51 * /vendor/lib/egl/libEGL.so 52 * /vendor/lib/egl/libGLESv1_CM.so 53 * /vendor/lib/egl/libGLESv2.so 54 * 55 * The software renderer for the emulator must be provided as a single 56 * library at: 57 * 58 * /system/lib/egl/libGLES_android.so 59 * 60 * 61 * For backward compatibility and to facilitate the transition to 62 * this new naming scheme, the loader will additionally look for: 63 * 64 * /{vendor|system}/lib/egl/lib{GLES | [EGL|GLESv1_CM|GLESv2]}_*.so 65 * 66 */ 67 68ANDROID_SINGLETON_STATIC_INSTANCE( Loader ) 69 70/* This function is called to check whether we run inside the emulator, 71 * and if this is the case whether GLES GPU emulation is supported. 72 * 73 * Returned values are: 74 * -1 -> not running inside the emulator 75 * 0 -> running inside the emulator, but GPU emulation not supported 76 * 1 -> running inside the emulator, GPU emulation is supported 77 * through the "emulation" host-side OpenGL ES implementation. 78 * 2 -> running inside the emulator, GPU emulation is supported 79 * through a guest-side vendor driver's OpenGL ES implementation. 80 */ 81static int 82checkGlesEmulationStatus(void) 83{ 84 /* We're going to check for the following kernel parameters: 85 * 86 * qemu=1 -> tells us that we run inside the emulator 87 * android.qemu.gles=<number> -> tells us the GLES GPU emulation status 88 * 89 * Note that we will return <number> if we find it. This let us support 90 * more additionnal emulation modes in the future. 91 */ 92 char prop[PROPERTY_VALUE_MAX]; 93 int result = -1; 94 95 /* First, check for qemu=1 */ 96 property_get("ro.kernel.qemu",prop,"0"); 97 if (atoi(prop) != 1) 98 return -1; 99 100 /* We are in the emulator, get GPU status value */ 101 property_get("qemu.gles",prop,"0"); 102 return atoi(prop); 103} 104 105static void* do_dlopen(const char* path, int mode) { 106 ATRACE_CALL(); 107 return dlopen(path, mode); 108} 109 110// ---------------------------------------------------------------------------- 111 112Loader::driver_t::driver_t(void* gles) 113{ 114 dso[0] = gles; 115 for (size_t i=1 ; i<NELEM(dso) ; i++) 116 dso[i] = 0; 117} 118 119Loader::driver_t::~driver_t() 120{ 121 for (size_t i=0 ; i<NELEM(dso) ; i++) { 122 if (dso[i]) { 123 dlclose(dso[i]); 124 dso[i] = 0; 125 } 126 } 127} 128 129status_t Loader::driver_t::set(void* hnd, int32_t api) 130{ 131 switch (api) { 132 case EGL: 133 dso[0] = hnd; 134 break; 135 case GLESv1_CM: 136 dso[1] = hnd; 137 break; 138 case GLESv2: 139 dso[2] = hnd; 140 break; 141 default: 142 return BAD_INDEX; 143 } 144 return NO_ERROR; 145} 146 147// ---------------------------------------------------------------------------- 148 149Loader::Loader() 150 : getProcAddress(NULL), 151 mLibGui(nullptr), 152 mGetDriverNamespace(nullptr) 153{ 154 // FIXME: See note in GraphicsEnv.h about android_getDriverNamespace(). 155 // libgui should already be loaded in any process that uses libEGL, but 156 // if for some reason it isn't, then we're not going to get a driver 157 // namespace anyway, so don't force it to be loaded. 158 mLibGui = dlopen("libgui.so", RTLD_NOLOAD | RTLD_LOCAL | RTLD_LAZY); 159 if (!mLibGui) { 160 ALOGD("failed to load libgui: %s", dlerror()); 161 return; 162 } 163 mGetDriverNamespace = reinterpret_cast<decltype(mGetDriverNamespace)>( 164 dlsym(mLibGui, "android_getDriverNamespace")); 165} 166 167Loader::~Loader() { 168 if (mLibGui) 169 dlclose(mLibGui); 170} 171 172static void* load_wrapper(const char* path) { 173 void* so = do_dlopen(path, RTLD_NOW | RTLD_LOCAL); 174 ALOGE_IF(!so, "dlopen(\"%s\") failed: %s", path, dlerror()); 175 return so; 176} 177 178#ifndef EGL_WRAPPER_DIR 179#if defined(__LP64__) 180#define EGL_WRAPPER_DIR "/system/lib64" 181#else 182#define EGL_WRAPPER_DIR "/system/lib" 183#endif 184#endif 185 186static void setEmulatorGlesValue(void) { 187 char prop[PROPERTY_VALUE_MAX]; 188 property_get("ro.kernel.qemu", prop, "0"); 189 if (atoi(prop) != 1) return; 190 191 property_get("ro.kernel.qemu.gles",prop,"0"); 192 if (atoi(prop) == 1) { 193 ALOGD("Emulator has host GPU support, qemu.gles is set to 1."); 194 property_set("qemu.gles", "1"); 195 return; 196 } 197 198 // for now, checking the following 199 // directory is good enough for emulator system images 200 const char* vendor_lib_path = 201#if defined(__LP64__) 202 "/vendor/lib64/egl"; 203#else 204 "/vendor/lib/egl"; 205#endif 206 207 const bool has_vendor_lib = (access(vendor_lib_path, R_OK) == 0); 208 if (has_vendor_lib) { 209 ALOGD("Emulator has vendor provided software renderer, qemu.gles is set to 2."); 210 property_set("qemu.gles", "2"); 211 } else { 212 ALOGD("Emulator without GPU support detected. " 213 "Fallback to legacy software renderer, qemu.gles is set to 0."); 214 property_set("qemu.gles", "0"); 215 } 216} 217 218void* Loader::open(egl_connection_t* cnx) 219{ 220 ATRACE_CALL(); 221 222 void* dso; 223 driver_t* hnd = 0; 224 225 setEmulatorGlesValue(); 226 227 dso = load_driver("GLES", cnx, EGL | GLESv1_CM | GLESv2); 228 if (dso) { 229 hnd = new driver_t(dso); 230 } else { 231 // Always load EGL first 232 dso = load_driver("EGL", cnx, EGL); 233 if (dso) { 234 hnd = new driver_t(dso); 235 hnd->set( load_driver("GLESv1_CM", cnx, GLESv1_CM), GLESv1_CM ); 236 hnd->set( load_driver("GLESv2", cnx, GLESv2), GLESv2 ); 237 } 238 } 239 240 LOG_ALWAYS_FATAL_IF(!hnd, "couldn't find an OpenGL ES implementation"); 241 242 cnx->libEgl = load_wrapper(EGL_WRAPPER_DIR "/libEGL.so"); 243 cnx->libGles2 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv2.so"); 244 cnx->libGles1 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv1_CM.so"); 245 246 LOG_ALWAYS_FATAL_IF(!cnx->libEgl, 247 "couldn't load system EGL wrapper libraries"); 248 249 LOG_ALWAYS_FATAL_IF(!cnx->libGles2 || !cnx->libGles1, 250 "couldn't load system OpenGL ES wrapper libraries"); 251 252 return (void*)hnd; 253} 254 255status_t Loader::close(void* driver) 256{ 257 driver_t* hnd = (driver_t*)driver; 258 delete hnd; 259 return NO_ERROR; 260} 261 262void Loader::init_api(void* dso, 263 char const * const * api, 264 __eglMustCastToProperFunctionPointerType* curr, 265 getProcAddressType getProcAddress) 266{ 267 ATRACE_CALL(); 268 269 const ssize_t SIZE = 256; 270 char scrap[SIZE]; 271 while (*api) { 272 char const * name = *api; 273 __eglMustCastToProperFunctionPointerType f = 274 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name); 275 if (f == NULL) { 276 // couldn't find the entry-point, use eglGetProcAddress() 277 f = getProcAddress(name); 278 } 279 if (f == NULL) { 280 // Try without the OES postfix 281 ssize_t index = ssize_t(strlen(name)) - 3; 282 if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) { 283 strncpy(scrap, name, index); 284 scrap[index] = 0; 285 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap); 286 //ALOGD_IF(f, "found <%s> instead", scrap); 287 } 288 } 289 if (f == NULL) { 290 // Try with the OES postfix 291 ssize_t index = ssize_t(strlen(name)) - 3; 292 if (index>0 && strcmp(name+index, "OES")) { 293 snprintf(scrap, SIZE, "%sOES", name); 294 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap); 295 //ALOGD_IF(f, "found <%s> instead", scrap); 296 } 297 } 298 if (f == NULL) { 299 //ALOGD("%s", name); 300 f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented; 301 302 /* 303 * GL_EXT_debug_label is special, we always report it as 304 * supported, it's handled by GLES_trace. If GLES_trace is not 305 * enabled, then these are no-ops. 306 */ 307 if (!strcmp(name, "glInsertEventMarkerEXT")) { 308 f = (__eglMustCastToProperFunctionPointerType)gl_noop; 309 } else if (!strcmp(name, "glPushGroupMarkerEXT")) { 310 f = (__eglMustCastToProperFunctionPointerType)gl_noop; 311 } else if (!strcmp(name, "glPopGroupMarkerEXT")) { 312 f = (__eglMustCastToProperFunctionPointerType)gl_noop; 313 } 314 } 315 *curr++ = f; 316 api++; 317 } 318} 319 320static void* load_system_driver(const char* kind) { 321 ATRACE_CALL(); 322 class MatchFile { 323 public: 324 static String8 find(const char* kind) { 325 String8 result; 326 int emulationStatus = checkGlesEmulationStatus(); 327 switch (emulationStatus) { 328 case 0: 329#if defined(__LP64__) 330 result.setTo("/system/lib64/egl/libGLES_android.so"); 331#else 332 result.setTo("/system/lib/egl/libGLES_android.so"); 333#endif 334 return result; 335 case 1: 336 // Use host-side OpenGL through the "emulation" library 337#if defined(__LP64__) 338 result.appendFormat("/system/lib64/egl/lib%s_emulation.so", kind); 339#else 340 result.appendFormat("/system/lib/egl/lib%s_emulation.so", kind); 341#endif 342 return result; 343 default: 344 // Not in emulator, or use other guest-side implementation 345 break; 346 } 347 348 String8 pattern; 349 pattern.appendFormat("lib%s", kind); 350 const char* const searchPaths[] = { 351#if defined(__LP64__) 352 "/vendor/lib64/egl", 353 "/system/lib64/egl" 354#else 355 "/vendor/lib/egl", 356 "/system/lib/egl" 357#endif 358 }; 359 360 // first, we search for the exact name of the GLES userspace 361 // driver in both locations. 362 // i.e.: 363 // libGLES.so, or: 364 // libEGL.so, libGLESv1_CM.so, libGLESv2.so 365 366 for (size_t i=0 ; i<NELEM(searchPaths) ; i++) { 367 if (find(result, pattern, searchPaths[i], true)) { 368 return result; 369 } 370 } 371 372 // for compatibility with the old "egl.cfg" naming convention 373 // we look for files that match: 374 // libGLES_*.so, or: 375 // libEGL_*.so, libGLESv1_CM_*.so, libGLESv2_*.so 376 377 pattern.append("_"); 378 for (size_t i=0 ; i<NELEM(searchPaths) ; i++) { 379 if (find(result, pattern, searchPaths[i], false)) { 380 return result; 381 } 382 } 383 384 // we didn't find the driver. gah. 385 result.clear(); 386 return result; 387 } 388 389 private: 390 static bool find(String8& result, 391 const String8& pattern, const char* const search, bool exact) { 392 if (exact) { 393 String8 absolutePath; 394 absolutePath.appendFormat("%s/%s.so", search, pattern.string()); 395 if (!access(absolutePath.string(), R_OK)) { 396 result = absolutePath; 397 return true; 398 } 399 return false; 400 } 401 402 DIR* d = opendir(search); 403 if (d != NULL) { 404 struct dirent cur; 405 struct dirent* e; 406 while (readdir_r(d, &cur, &e) == 0 && e) { 407 if (e->d_type == DT_DIR) { 408 continue; 409 } 410 if (!strcmp(e->d_name, "libGLES_android.so")) { 411 // always skip the software renderer 412 continue; 413 } 414 if (strstr(e->d_name, pattern.string()) == e->d_name) { 415 if (!strcmp(e->d_name + strlen(e->d_name) - 3, ".so")) { 416 result.clear(); 417 result.appendFormat("%s/%s", search, e->d_name); 418 closedir(d); 419 return true; 420 } 421 } 422 } 423 closedir(d); 424 } 425 return false; 426 } 427 }; 428 429 430 String8 absolutePath = MatchFile::find(kind); 431 if (absolutePath.isEmpty()) { 432 // this happens often, we don't want to log an error 433 return 0; 434 } 435 const char* const driver_absolute_path = absolutePath.string(); 436 437 void* dso = do_dlopen(driver_absolute_path, RTLD_NOW | RTLD_LOCAL); 438 if (dso == 0) { 439 const char* err = dlerror(); 440 ALOGE("load_driver(%s): %s", driver_absolute_path, err?err:"unknown"); 441 return 0; 442 } 443 444 ALOGD("loaded %s", driver_absolute_path); 445 446 return dso; 447} 448 449static void* do_android_dlopen_ext(const char* path, int mode, const android_dlextinfo* info) { 450 ATRACE_CALL(); 451 return android_dlopen_ext(path, mode, info); 452} 453 454static const std::array<const char*, 2> HAL_SUBNAME_KEY_PROPERTIES = {{ 455 "ro.hardware.egl", 456 "ro.board.platform", 457}}; 458 459static void* load_updated_driver(const char* kind, android_namespace_t* ns) { 460 ATRACE_CALL(); 461 const android_dlextinfo dlextinfo = { 462 .flags = ANDROID_DLEXT_USE_NAMESPACE, 463 .library_namespace = ns, 464 }; 465 void* so = nullptr; 466 char prop[PROPERTY_VALUE_MAX + 1]; 467 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) { 468 if (property_get(key, prop, nullptr) > 0) { 469 String8 name; 470 name.appendFormat("lib%s_%s.so", kind, prop); 471 so = do_android_dlopen_ext(name.string(), RTLD_LOCAL | RTLD_NOW, 472 &dlextinfo); 473 if (so) 474 return so; 475 } 476 } 477 return nullptr; 478} 479 480void *Loader::load_driver(const char* kind, 481 egl_connection_t* cnx, uint32_t mask) 482{ 483 ATRACE_CALL(); 484 485 void* dso = nullptr; 486 if (mGetDriverNamespace) { 487 android_namespace_t* ns = mGetDriverNamespace(); 488 if (ns) { 489 dso = load_updated_driver(kind, ns); 490 } 491 } 492 if (!dso) { 493 dso = load_system_driver(kind); 494 if (!dso) 495 return NULL; 496 } 497 498 if (mask & EGL) { 499 getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress"); 500 501 ALOGE_IF(!getProcAddress, 502 "can't find eglGetProcAddress() in EGL driver library"); 503 504 egl_t* egl = &cnx->egl; 505 __eglMustCastToProperFunctionPointerType* curr = 506 (__eglMustCastToProperFunctionPointerType*)egl; 507 char const * const * api = egl_names; 508 while (*api) { 509 char const * name = *api; 510 __eglMustCastToProperFunctionPointerType f = 511 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name); 512 if (f == NULL) { 513 // couldn't find the entry-point, use eglGetProcAddress() 514 f = getProcAddress(name); 515 if (f == NULL) { 516 f = (__eglMustCastToProperFunctionPointerType)0; 517 } 518 } 519 *curr++ = f; 520 api++; 521 } 522 } 523 524 if (mask & GLESv1_CM) { 525 init_api(dso, gl_names, 526 (__eglMustCastToProperFunctionPointerType*) 527 &cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl, 528 getProcAddress); 529 } 530 531 if (mask & GLESv2) { 532 init_api(dso, gl_names, 533 (__eglMustCastToProperFunctionPointerType*) 534 &cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl, 535 getProcAddress); 536 } 537 538 return dso; 539} 540 541// ---------------------------------------------------------------------------- 542}; // namespace android 543// ---------------------------------------------------------------------------- 544