blob: 0f58f965475952fe617446de6cc0a6e2eae229bc [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>
18#include <stdlib.h>
19#include <stdio.h>
20#include <string.h>
21#include <errno.h>
22#include <dlfcn.h>
23#include <limits.h>
Mathias Agopian99381422013-04-23 20:52:29 +020024#include <dirent.h>
Mathias Agopiande586972009-05-28 17:39:03 -070025
26#include <cutils/log.h>
David 'Digit' Turner80b30c22011-08-26 17:38:47 +020027#include <cutils/properties.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
71 * through the "emulation" config.
72 */
73static int
74checkGlesEmulationStatus(void)
75{
76 /* We're going to check for the following kernel parameters:
77 *
78 * qemu=1 -> tells us that we run inside the emulator
79 * android.qemu.gles=<number> -> tells us the GLES GPU emulation status
80 *
81 * Note that we will return <number> if we find it. This let us support
82 * more additionnal emulation modes in the future.
83 */
84 char prop[PROPERTY_VALUE_MAX];
85 int result = -1;
86
87 /* First, check for qemu=1 */
88 property_get("ro.kernel.qemu",prop,"0");
89 if (atoi(prop) != 1)
90 return -1;
91
92 /* We are in the emulator, get GPU status value */
93 property_get("ro.kernel.qemu.gles",prop,"0");
94 return atoi(prop);
95}
96
Mathias Agopiande586972009-05-28 17:39:03 -070097// ----------------------------------------------------------------------------
98
Mathias Agopiand75f84d2012-06-05 21:44:43 -070099static char const * getProcessCmdline() {
100 long pid = getpid();
101 char procPath[128];
102 snprintf(procPath, 128, "/proc/%ld/cmdline", pid);
103 FILE * file = fopen(procPath, "r");
104 if (file) {
105 static char cmdline[256];
106 char *str = fgets(cmdline, sizeof(cmdline) - 1, file);
107 fclose(file);
108 if (str) {
109 return cmdline;
110 }
111 }
112 return NULL;
113}
114
115// ----------------------------------------------------------------------------
116
Jesse Hall94cdba92013-07-11 09:40:35 -0700117Loader::driver_t::driver_t(void* gles)
Mathias Agopiande586972009-05-28 17:39:03 -0700118{
119 dso[0] = gles;
120 for (size_t i=1 ; i<NELEM(dso) ; i++)
121 dso[i] = 0;
122}
123
Jesse Hall94cdba92013-07-11 09:40:35 -0700124Loader::driver_t::~driver_t()
Mathias Agopiande586972009-05-28 17:39:03 -0700125{
126 for (size_t i=0 ; i<NELEM(dso) ; i++) {
127 if (dso[i]) {
128 dlclose(dso[i]);
129 dso[i] = 0;
130 }
131 }
132}
133
134status_t Loader::driver_t::set(void* hnd, int32_t api)
135{
136 switch (api) {
137 case EGL:
138 dso[0] = hnd;
139 break;
140 case GLESv1_CM:
141 dso[1] = hnd;
142 break;
143 case GLESv2:
144 dso[2] = hnd;
145 break;
146 default:
147 return BAD_INDEX;
148 }
149 return NO_ERROR;
150}
151
152// ----------------------------------------------------------------------------
153
Mathias Agopiande586972009-05-28 17:39:03 -0700154Loader::Loader()
Mathias Agopian99381422013-04-23 20:52:29 +0200155 : getProcAddress(NULL) {
Mathias Agopiande586972009-05-28 17:39:03 -0700156}
157
Mathias Agopian99381422013-04-23 20:52:29 +0200158Loader::~Loader() {
Mathias Agopiande586972009-05-28 17:39:03 -0700159}
160
Jesse Hallc07b5202013-07-04 12:08:16 -0700161static void* load_wrapper(const char* path) {
162 void* so = dlopen(path, RTLD_NOW | RTLD_LOCAL);
163 ALOGE_IF(!so, "dlopen(\"%s\") failed: %s", path, dlerror());
164 return so;
165}
166
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700167#ifndef EGL_WRAPPER_DIR
168#if defined(__LP64__)
169#define EGL_WRAPPER_DIR "/system/lib64"
170#else
171#define EGL_WRAPPER_DIR "/system/lib"
172#endif
173#endif
174
Mathias Agopianada798b2012-02-13 17:09:30 -0800175void* Loader::open(egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700176{
Mathias Agopiande586972009-05-28 17:39:03 -0700177 void* dso;
Mathias Agopiande586972009-05-28 17:39:03 -0700178 driver_t* hnd = 0;
Jesse Hall94cdba92013-07-11 09:40:35 -0700179
Mathias Agopian99381422013-04-23 20:52:29 +0200180 dso = load_driver("GLES", cnx, EGL | GLESv1_CM | GLESv2);
181 if (dso) {
182 hnd = new driver_t(dso);
183 } else {
184 // Always load EGL first
185 dso = load_driver("EGL", cnx, EGL);
Mathias Agopiande586972009-05-28 17:39:03 -0700186 if (dso) {
187 hnd = new driver_t(dso);
Mathias Agopian99381422013-04-23 20:52:29 +0200188 hnd->set( load_driver("GLESv1_CM", cnx, GLESv1_CM), GLESv1_CM );
189 hnd->set( load_driver("GLESv2", cnx, GLESv2), GLESv2 );
Mathias Agopiande586972009-05-28 17:39:03 -0700190 }
191 }
192
Mathias Agopian99381422013-04-23 20:52:29 +0200193 LOG_ALWAYS_FATAL_IF(!hnd, "couldn't find an OpenGL ES implementation");
Jesse Hallc07b5202013-07-04 12:08:16 -0700194
Evgenii Stepanovc2466e62015-07-08 15:49:52 -0700195 cnx->libEgl = load_wrapper(EGL_WRAPPER_DIR "/libEGL.so");
196 cnx->libGles2 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv2.so");
197 cnx->libGles1 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv1_CM.so");
198
Michael Chockc0ec5e22014-01-27 08:14:33 -0800199 LOG_ALWAYS_FATAL_IF(!cnx->libEgl,
200 "couldn't load system EGL wrapper libraries");
201
Jesse Hallc07b5202013-07-04 12:08:16 -0700202 LOG_ALWAYS_FATAL_IF(!cnx->libGles2 || !cnx->libGles1,
203 "couldn't load system OpenGL ES wrapper libraries");
204
Mathias Agopiande586972009-05-28 17:39:03 -0700205 return (void*)hnd;
206}
207
208status_t Loader::close(void* driver)
209{
210 driver_t* hnd = (driver_t*)driver;
211 delete hnd;
212 return NO_ERROR;
213}
214
Jesse Hall94cdba92013-07-11 09:40:35 -0700215void Loader::init_api(void* dso,
216 char const * const * api,
217 __eglMustCastToProperFunctionPointerType* curr,
218 getProcAddressType getProcAddress)
Mathias Agopiande586972009-05-28 17:39:03 -0700219{
Mathias Agopian7773c432012-02-13 20:06:08 -0800220 const ssize_t SIZE = 256;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700221 char scrap[SIZE];
Mathias Agopiande586972009-05-28 17:39:03 -0700222 while (*api) {
223 char const * name = *api;
Jesse Hall94cdba92013-07-11 09:40:35 -0700224 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700225 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
226 if (f == NULL) {
227 // couldn't find the entry-point, use eglGetProcAddress()
228 f = getProcAddress(name);
229 }
230 if (f == NULL) {
231 // Try without the OES postfix
232 ssize_t index = ssize_t(strlen(name)) - 3;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700233 if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) {
Mathias Agopiande586972009-05-28 17:39:03 -0700234 strncpy(scrap, name, index);
235 scrap[index] = 0;
236 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000237 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700238 }
239 }
240 if (f == NULL) {
241 // Try with the OES postfix
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700242 ssize_t index = ssize_t(strlen(name)) - 3;
243 if (index>0 && strcmp(name+index, "OES")) {
244 snprintf(scrap, SIZE, "%sOES", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700245 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000246 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700247 }
248 }
249 if (f == NULL) {
Steve Block9d453682011-12-20 16:23:08 +0000250 //ALOGD("%s", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700251 f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented;
Mathias Agopian48d438d2012-01-28 21:44:00 -0800252
253 /*
254 * GL_EXT_debug_label is special, we always report it as
255 * supported, it's handled by GLES_trace. If GLES_trace is not
256 * enabled, then these are no-ops.
257 */
258 if (!strcmp(name, "glInsertEventMarkerEXT")) {
259 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
260 } else if (!strcmp(name, "glPushGroupMarkerEXT")) {
261 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
262 } else if (!strcmp(name, "glPopGroupMarkerEXT")) {
263 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
264 }
Mathias Agopiande586972009-05-28 17:39:03 -0700265 }
266 *curr++ = f;
267 api++;
268 }
269}
270
Mathias Agopian99381422013-04-23 20:52:29 +0200271void *Loader::load_driver(const char* kind,
Mathias Agopian618fa102009-10-14 02:06:37 -0700272 egl_connection_t* cnx, uint32_t mask)
Mathias Agopiande586972009-05-28 17:39:03 -0700273{
Mathias Agopian99381422013-04-23 20:52:29 +0200274 class MatchFile {
275 public:
276 static String8 find(const char* kind) {
277 String8 result;
278 String8 pattern;
279 pattern.appendFormat("lib%s", kind);
280 const char* const searchPaths[] = {
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800281#if defined(__LP64__)
282 "/vendor/lib64/egl",
283 "/system/lib64/egl"
284#else
Mathias Agopian99381422013-04-23 20:52:29 +0200285 "/vendor/lib/egl",
286 "/system/lib/egl"
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800287#endif
Mathias Agopian99381422013-04-23 20:52:29 +0200288 };
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700289
Mathias Agopian99381422013-04-23 20:52:29 +0200290 // first, we search for the exact name of the GLES userspace
291 // driver in both locations.
292 // i.e.:
293 // libGLES.so, or:
294 // libEGL.so, libGLESv1_CM.so, libGLESv2.so
295
296 for (size_t i=0 ; i<NELEM(searchPaths) ; i++) {
297 if (find(result, pattern, searchPaths[i], true)) {
298 return result;
299 }
300 }
301
302 // for compatibility with the old "egl.cfg" naming convention
303 // we look for files that match:
304 // libGLES_*.so, or:
305 // libEGL_*.so, libGLESv1_CM_*.so, libGLESv2_*.so
306
307 pattern.append("_");
308 for (size_t i=0 ; i<NELEM(searchPaths) ; i++) {
309 if (find(result, pattern, searchPaths[i], false)) {
310 return result;
311 }
312 }
313
314 // we didn't find the driver. gah.
315 result.clear();
316 return result;
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700317 }
Mathias Agopian99381422013-04-23 20:52:29 +0200318
319 private:
320 static bool find(String8& result,
321 const String8& pattern, const char* const search, bool exact) {
322
323 // in the emulator case, we just return the hardcoded name
324 // of the software renderer.
325 if (checkGlesEmulationStatus() == 0) {
326 ALOGD("Emulator without GPU support detected. "
327 "Fallback to software renderer.");
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800328#if defined(__LP64__)
329 result.setTo("/system/lib64/egl/libGLES_android.so");
330#else
Mathias Agopian99381422013-04-23 20:52:29 +0200331 result.setTo("/system/lib/egl/libGLES_android.so");
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800332#endif
Mathias Agopian99381422013-04-23 20:52:29 +0200333 return true;
334 }
335
336 if (exact) {
337 String8 absolutePath;
338 absolutePath.appendFormat("%s/%s.so", search, pattern.string());
339 if (!access(absolutePath.string(), R_OK)) {
340 result = absolutePath;
341 return true;
342 }
343 return false;
344 }
345
346 DIR* d = opendir(search);
347 if (d != NULL) {
348 struct dirent cur;
349 struct dirent* e;
350 while (readdir_r(d, &cur, &e) == 0 && e) {
351 if (e->d_type == DT_DIR) {
352 continue;
353 }
354 if (!strcmp(e->d_name, "libGLES_android.so")) {
355 // always skip the software renderer
356 continue;
357 }
358 if (strstr(e->d_name, pattern.string()) == e->d_name) {
359 if (!strcmp(e->d_name + strlen(e->d_name) - 3, ".so")) {
360 result.clear();
361 result.appendFormat("%s/%s", search, e->d_name);
362 closedir(d);
363 return true;
364 }
365 }
366 }
367 closedir(d);
368 }
369 return false;
370 }
371 };
372
373
374 String8 absolutePath = MatchFile::find(kind);
375 if (absolutePath.isEmpty()) {
376 // this happens often, we don't want to log an error
377 return 0;
Mathias Agopian8c173842009-09-20 16:01:02 -0700378 }
Mathias Agopian99381422013-04-23 20:52:29 +0200379 const char* const driver_absolute_path = absolutePath.string();
Mathias Agopiande586972009-05-28 17:39:03 -0700380
Mathias Agopian8c173842009-09-20 16:01:02 -0700381 void* dso = dlopen(driver_absolute_path, RTLD_NOW | RTLD_LOCAL);
382 if (dso == 0) {
383 const char* err = dlerror();
Steve Blocke6f43dd2012-01-06 19:20:56 +0000384 ALOGE("load_driver(%s): %s", driver_absolute_path, err?err:"unknown");
Mathias Agopian8c173842009-09-20 16:01:02 -0700385 return 0;
386 }
387
Steve Block9d453682011-12-20 16:23:08 +0000388 ALOGD("loaded %s", driver_absolute_path);
Mathias Agopianbaca89c2009-08-20 19:09:34 -0700389
Mathias Agopiande586972009-05-28 17:39:03 -0700390 if (mask & EGL) {
391 getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress");
392
Jesse Hall94cdba92013-07-11 09:40:35 -0700393 ALOGE_IF(!getProcAddress,
Mathias Agopian8c173842009-09-20 16:01:02 -0700394 "can't find eglGetProcAddress() in %s", driver_absolute_path);
Mathias Agopiande586972009-05-28 17:39:03 -0700395
Mathias Agopian618fa102009-10-14 02:06:37 -0700396 egl_t* egl = &cnx->egl;
Mathias Agopiande586972009-05-28 17:39:03 -0700397 __eglMustCastToProperFunctionPointerType* curr =
398 (__eglMustCastToProperFunctionPointerType*)egl;
399 char const * const * api = egl_names;
400 while (*api) {
401 char const * name = *api;
Jesse Hall94cdba92013-07-11 09:40:35 -0700402 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700403 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
404 if (f == NULL) {
405 // couldn't find the entry-point, use eglGetProcAddress()
406 f = getProcAddress(name);
407 if (f == NULL) {
408 f = (__eglMustCastToProperFunctionPointerType)0;
409 }
410 }
411 *curr++ = f;
412 api++;
413 }
414 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700415
Mathias Agopiande586972009-05-28 17:39:03 -0700416 if (mask & GLESv1_CM) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700417 init_api(dso, gl_names,
418 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800419 &cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl,
Mathias Agopian618fa102009-10-14 02:06:37 -0700420 getProcAddress);
Mathias Agopiande586972009-05-28 17:39:03 -0700421 }
422
423 if (mask & GLESv2) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700424 init_api(dso, gl_names,
425 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800426 &cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl,
Mathias Agopiande586972009-05-28 17:39:03 -0700427 getProcAddress);
428 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700429
Mathias Agopiande586972009-05-28 17:39:03 -0700430 return dso;
431}
432
433// ----------------------------------------------------------------------------
434}; // namespace android
435// ----------------------------------------------------------------------------