opengl renderer: added option to dump GL stream to file.

This is a debugging tool which enables to dump the guest
command stream to a file which can be later be examined
and "played" using a seperate tool.

Change-Id: I3fec19c1a651f0ed4394c33a0c0cd9ba54384355
diff --git a/tools/emulator/opengl/host/libs/libOpenglRender/RenderThread.cpp b/tools/emulator/opengl/host/libs/libOpenglRender/RenderThread.cpp
index af2363f..be7569b 100644
--- a/tools/emulator/opengl/host/libs/libOpenglRender/RenderThread.cpp
+++ b/tools/emulator/opengl/host/libs/libOpenglRender/RenderThread.cpp
@@ -56,6 +56,22 @@
     int stats_totalBytes = 0;
     long long stats_t0 = GetCurrentTimeMS();
 
+    //
+    // open dump file if RENDER_DUMP_DIR is defined
+    //
+    const char *dump_dir = getenv("RENDERER_DUMP_DIR");
+    FILE *dumpFP = NULL;
+    if (dump_dir) {
+        size_t bsize = strlen(dump_dir) + 32;
+        char *fname = new char[bsize];
+        snprintf(fname,bsize,"%s/stream_%p", dump_dir, this);
+        dumpFP = fopen(fname, "wb");
+        if (!dumpFP) {
+            fprintf(stderr,"Warning: stream dump failed to open file %s\n",fname);
+        }
+        delete [] fname;
+    }
+
     while (1) {
 
         int stat = readBuf.getData();
@@ -76,6 +92,15 @@
             stats_t0 = GetCurrentTimeMS();
         }
 
+        //
+        // dump stream to file if needed
+        //
+        if (dumpFP) {
+            int skip = readBuf.validData() - stat;
+            fwrite(readBuf.buf()+skip, 1, readBuf.validData()-skip, dumpFP);
+            fflush(dumpFP);
+        }
+
         bool progress;
         do {
             progress = false;
@@ -112,5 +137,9 @@
 
     }
 
+    if (dumpFP) {
+        fclose(dumpFP);
+    }
+
     return 0;
 }