opengl: EGL: special case for GLES emulation

This patch modifies the library loaded in libEGL.so to
handle the case of GLES emulation as follows:

- if we detect that we run inside the emulator, check the
  GPU emulation status through ro.kernel.qemu.gles, which
  will be set to 1 if supported, or 0 otherwise.

  When trying to run on an older version of the emulator,
  the kernel parameter will not be defined at all.

- if GPU emulation is supported, use egl.cfg as usual.
  It will contain a line like "0 0 emulation" that will
  load libEGL_android.so appropriately.

- nothing is changed if we don't run inside the emulator.

NOTE: Ideally, we would modify libEGL_emulation.so to
      redirect all calls to libEGL_android.so in this case.

      However, this turns out to be extremely tedious to implement
      (too many functions with different signatures).

      As such,  it is much simpler to make the check before
      loading the library.

Change-Id: I9930bc168d9013cc8700feedc57b979384467c37
diff --git a/opengl/libs/EGL/Loader.cpp b/opengl/libs/EGL/Loader.cpp
index e94e50e..325193c 100644
--- a/opengl/libs/EGL/Loader.cpp
+++ b/opengl/libs/EGL/Loader.cpp
@@ -23,6 +23,7 @@
 #include <limits.h>
 
 #include <cutils/log.h>
+#include <cutils/properties.h>
 
 #include <EGL/egl.h>
 
@@ -45,6 +46,39 @@
 
 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);
+}
+
 // ----------------------------------------------------------------------------
 
 Loader::driver_t::driver_t(void* gles) 
@@ -94,6 +128,15 @@
 {
     char line[256];
     char tag[256];
+
+    /* Special case for GLES emulation */
+    if (checkGlesEmulationStatus() == 0) {
+        LOGD("Emulator without GPU support detected. Fallback to software renderer.");
+        gConfig.add( entry_t(0, 0, "android") );
+        return;
+    }
+
+    /* Otherwise, use egl.cfg */
     FILE* cfg = fopen("/system/lib/egl/egl.cfg", "r");
     if (cfg == NULL) {
         // default config