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