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.
25 #include <cutils/log.h>
26 #include <cutils/properties.h>
30 #include "../glestrace.h"
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
41 * EGL drivers are called
43 * /system/lib/egl/lib{[EGL|GLESv1_CM|GLESv2] | GLES}_$TAG.so
47 ANDROID_SINGLETON_STATIC_INSTANCE( Loader )
49 /* This function is called to check whether we run inside the emulator,
50 * and if this is the case whether GLES GPU emulation is supported.
52 * Returned values are:
53 * -1 -> not running inside the emulator
54 * 0 -> running inside the emulator, but GPU emulation not supported
55 * 1 -> running inside the emulator, GPU emulation is supported
56 * through the "emulation" config.
59 checkGlesEmulationStatus(void)
61 /* We're going to check for the following kernel parameters:
63 * qemu=1 -> tells us that we run inside the emulator
64 * android.qemu.gles=<number> -> tells us the GLES GPU emulation status
66 * Note that we will return <number> if we find it. This let us support
67 * more additionnal emulation modes in the future.
69 char prop[PROPERTY_VALUE_MAX];
72 /* First, check for qemu=1 */
73 property_get("ro.kernel.qemu",prop,"0");
77 /* We are in the emulator, get GPU status value */
78 property_get("ro.kernel.qemu.gles",prop,"0");
82 // ----------------------------------------------------------------------------
84 static char const * getProcessCmdline() {
87 snprintf(procPath, 128, "/proc/%ld/cmdline", pid);
88 FILE * file = fopen(procPath, "r");
90 static char cmdline[256];
91 char *str = fgets(cmdline, sizeof(cmdline) - 1, file);
100 // ----------------------------------------------------------------------------
102 Loader::driver_t::driver_t(void* gles)
105 for (size_t i=1 ; i<NELEM(dso) ; i++)
109 Loader::driver_t::~driver_t()
111 for (size_t i=0 ; i<NELEM(dso) ; i++) {
119 status_t Loader::driver_t::set(void* hnd, int32_t api)
137 // ----------------------------------------------------------------------------
144 /* Special case for GLES emulation */
145 if (checkGlesEmulationStatus() == 0) {
146 ALOGD("Emulator without GPU support detected. "
147 "Fallback to software renderer.");
148 mDriverTag.setTo("android");
152 /* Otherwise, use egl.cfg */
153 FILE* cfg = fopen("/system/lib/egl/egl.cfg", "r");
156 ALOGD("egl.cfg not found, using default config");
157 mDriverTag.setTo("android");
159 while (fgets(line, 256, cfg)) {
161 if (sscanf(line, "%u %u %s", &dpy, &impl, tag) == 3) {
162 //ALOGD(">>> %u %u %s", dpy, impl, tag);
163 // We only load the h/w accelerated implementation
164 if (tag != String8("android")) {
178 static void* load_wrapper(const char* path) {
179 void* so = dlopen(path, RTLD_NOW | RTLD_LOCAL);
180 ALOGE_IF(!so, "dlopen(\"%s\") failed: %s", path, dlerror());
184 void* Loader::open(egl_connection_t* cnx)
189 char const* tag = mDriverTag.string();
191 dso = load_driver("GLES", tag, cnx, EGL | GLESv1_CM | GLESv2);
193 hnd = new driver_t(dso);
195 // Always load EGL first
196 dso = load_driver("EGL", tag, cnx, EGL);
198 hnd = new driver_t(dso);
199 // TODO: make this more automated
200 hnd->set( load_driver("GLESv1_CM", tag, cnx, GLESv1_CM), GLESv1_CM );
201 hnd->set( load_driver("GLESv2", tag, cnx, GLESv2), GLESv2 );
206 LOG_FATAL_IF(!index && !hnd,
207 "couldn't find the default OpenGL ES implementation "
208 "for default display");
210 cnx->libGles2 = load_wrapper("system/lib/libGLESv2.so");
211 cnx->libGles1 = load_wrapper("system/lib/libGLESv1_CM.so");
212 LOG_ALWAYS_FATAL_IF(!cnx->libGles2 || !cnx->libGles1,
213 "couldn't load system OpenGL ES wrapper libraries");
218 status_t Loader::close(void* driver)
220 driver_t* hnd = (driver_t*)driver;
225 void Loader::init_api(void* dso,
226 char const * const * api,
227 __eglMustCastToProperFunctionPointerType* curr,
228 getProcAddressType getProcAddress)
230 const ssize_t SIZE = 256;
233 char const * name = *api;
234 __eglMustCastToProperFunctionPointerType f =
235 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
237 // couldn't find the entry-point, use eglGetProcAddress()
238 f = getProcAddress(name);
241 // Try without the OES postfix
242 ssize_t index = ssize_t(strlen(name)) - 3;
243 if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) {
244 strncpy(scrap, name, index);
246 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
247 //ALOGD_IF(f, "found <%s> instead", scrap);
251 // Try with the OES postfix
252 ssize_t index = ssize_t(strlen(name)) - 3;
253 if (index>0 && strcmp(name+index, "OES")) {
254 snprintf(scrap, SIZE, "%sOES", name);
255 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
256 //ALOGD_IF(f, "found <%s> instead", scrap);
261 f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented;
264 * GL_EXT_debug_label is special, we always report it as
265 * supported, it's handled by GLES_trace. If GLES_trace is not
266 * enabled, then these are no-ops.
268 if (!strcmp(name, "glInsertEventMarkerEXT")) {
269 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
270 } else if (!strcmp(name, "glPushGroupMarkerEXT")) {
271 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
272 } else if (!strcmp(name, "glPopGroupMarkerEXT")) {
273 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
281 void *Loader::load_driver(const char* kind, const char *tag,
282 egl_connection_t* cnx, uint32_t mask)
284 char driver_absolute_path[PATH_MAX];
285 const char* const search1 = "/vendor/lib/egl/lib%s_%s.so";
286 const char* const search2 = "/system/lib/egl/lib%s_%s.so";
288 snprintf(driver_absolute_path, PATH_MAX, search1, kind, tag);
289 if (access(driver_absolute_path, R_OK)) {
290 snprintf(driver_absolute_path, PATH_MAX, search2, kind, tag);
291 if (access(driver_absolute_path, R_OK)) {
292 // this happens often, we don't want to log an error
297 void* dso = dlopen(driver_absolute_path, RTLD_NOW | RTLD_LOCAL);
299 const char* err = dlerror();
300 ALOGE("load_driver(%s): %s", driver_absolute_path, err?err:"unknown");
304 ALOGD("loaded %s", driver_absolute_path);
307 getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress");
309 ALOGE_IF(!getProcAddress,
310 "can't find eglGetProcAddress() in %s", driver_absolute_path);
312 #ifdef SYSTEMUI_PBSIZE_HACK
313 #warning "SYSTEMUI_PBSIZE_HACK enabled"
315 * TODO: replace SYSTEMUI_PBSIZE_HACK by something less hackish
317 * Here we adjust the PB size from its default value to 512KB which
318 * is the minimum acceptable for the systemui process.
319 * We do this on low-end devices only because it allows us to enable
320 * h/w acceleration in the systemui process while keeping the
323 * Obviously, this is the wrong place and wrong way to make this
324 * adjustment, but at the time of this writing this was the safest
327 const char *cmdline = getProcessCmdline();
328 if (strstr(cmdline, "systemui")) {
329 void *imgegl = dlopen("/vendor/lib/libIMGegl.so", RTLD_LAZY);
331 unsigned int *PVRDefaultPBS =
332 (unsigned int *)dlsym(imgegl, "PVRDefaultPBS");
334 ALOGD("setting default PBS to 512KB, was %d KB", *PVRDefaultPBS / 1024);
335 *PVRDefaultPBS = 512*1024;
341 egl_t* egl = &cnx->egl;
342 __eglMustCastToProperFunctionPointerType* curr =
343 (__eglMustCastToProperFunctionPointerType*)egl;
344 char const * const * api = egl_names;
346 char const * name = *api;
347 __eglMustCastToProperFunctionPointerType f =
348 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
350 // couldn't find the entry-point, use eglGetProcAddress()
351 f = getProcAddress(name);
353 f = (__eglMustCastToProperFunctionPointerType)0;
361 if (mask & GLESv1_CM) {
362 init_api(dso, gl_names,
363 (__eglMustCastToProperFunctionPointerType*)
364 &cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl,
369 init_api(dso, gl_names,
370 (__eglMustCastToProperFunctionPointerType*)
371 &cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl,
378 // ----------------------------------------------------------------------------
379 }; // namespace android
380 // ----------------------------------------------------------------------------