Merge "Regenerate encoders"
diff --git a/shared/OpenglCodecCommon/gralloc_cb.h b/shared/OpenglCodecCommon/gralloc_cb.h
index 04a7040..f874c12 100644
--- a/shared/OpenglCodecCommon/gralloc_cb.h
+++ b/shared/OpenglCodecCommon/gralloc_cb.h
@@ -22,6 +22,7 @@
 #include <cutils/native_handle.h>
 
 #include "goldfish_dma.h"
+#include "qemu_pipe.h"
 
 #define BUFFER_HANDLE_MAGIC ((int)0xabfabfab)
 #define CB_HANDLE_NUM_INTS(nfds) (int)((sizeof(cb_handle_t) - (nfds)*sizeof(int)) / sizeof(int))
@@ -64,7 +65,7 @@
         emuFrameworkFormat(p_emuFrameworkFormat)
     {
         goldfish_dma.fd = -1;
-        dmafd = -1;
+        refcount_pipe_fd = QEMU_PIPE_INVALID_HANDLE;
         version = sizeof(native_handle);
         numFds = 0;
         numInts = CB_HANDLE_NUM_INTS(numFds);
@@ -82,11 +83,15 @@
         numInts = CB_HANDLE_NUM_INTS(numFds);
     }
 
-    void setDmaFd(int fd) {
-        if (fd >= 0) {
+    bool hasRefcountPipe() {
+        return qemu_pipe_valid(refcount_pipe_fd);
+    }
+
+    void setRefcountPipeFd(QEMU_PIPE_HANDLE fd) {
+        if (qemu_pipe_valid(fd)) {
             numFds++;
         }
-        dmafd = fd;
+        refcount_pipe_fd = fd;
         numInts = CB_HANDLE_NUM_INTS(numFds);
     }
 
@@ -103,7 +108,7 @@
 
     // file-descriptors
     int fd;  // ashmem fd (-1 of ashmem region did not allocated, i.e. no SW access needed)
-    int dmafd; // goldfish dma fd.
+    QEMU_PIPE_HANDLE refcount_pipe_fd; // goldfish pipe service for gralloc refcounting fd.
 
     // ints
     int magic;              // magic number in order to validate a pointer to be a cb_handle_t
diff --git a/shared/OpenglCodecCommon/qemu_pipe.h b/shared/OpenglCodecCommon/qemu_pipe.h
index 4b1aaf2..62de57f 100644
--- a/shared/OpenglCodecCommon/qemu_pipe.h
+++ b/shared/OpenglCodecCommon/qemu_pipe.h
@@ -22,6 +22,8 @@
 
 typedef void* QEMU_PIPE_HANDLE;
 
+#define QEMU_PIPE_INVALID_HANDLE NULL
+
 QEMU_PIPE_HANDLE qemu_pipe_open(const char* pipeName);
 void qemu_pipe_close(QEMU_PIPE_HANDLE pipe);
 
@@ -37,6 +39,8 @@
 
 typedef int QEMU_PIPE_HANDLE;
 
+#define QEMU_PIPE_INVALID_HANDLE (-1)
+
 #include <cutils/log.h>
 #include <sys/cdefs.h>
 #include <unistd.h>
diff --git a/system/egl/egl.cpp b/system/egl/egl.cpp
index 5b645c0..23fb8cc 100644
--- a/system/egl/egl.cpp
+++ b/system/egl/egl.cpp
@@ -29,6 +29,7 @@
 #include "ClientAPIExts.h"
 #include "EGLImage.h"
 #include "ProcessPipe.h"
+#include "qemu_pipe.h"
 
 #include "GLEncoder.h"
 #ifdef WITH_GLES2
@@ -405,6 +406,7 @@
     DEFINE_AND_VALIDATE_HOST_CONNECTION(EGL_FALSE);
     rcSurface = rcEnc->rcCreateWindowSurface(rcEnc, (uintptr_t)config,
             getWidth(), getHeight());
+
     if (!rcSurface) {
         ALOGE("rcCreateWindowSurface returned 0");
         return EGL_FALSE;
@@ -433,6 +435,7 @@
     if (rcSurface && rcEnc) {
         rcEnc->rcDestroyWindowSurface(rcEnc, rcSurface);
     }
+
     if (buffer) {
         nativeWindow->cancelBuffer_DEPRECATED(nativeWindow, buffer);
     }
@@ -599,12 +602,13 @@
     EGLBoolean init(GLenum format);
 
     uint32_t rcColorBuffer;
+    QEMU_PIPE_HANDLE refcountPipeFd;
 };
 
 egl_pbuffer_surface_t::egl_pbuffer_surface_t(EGLDisplay dpy, EGLConfig config,
         EGLint surfType, int32_t w, int32_t h)
 :   egl_surface_t(dpy, config, surfType),
-    rcColorBuffer(0)
+    rcColorBuffer(0), refcountPipeFd(QEMU_PIPE_INVALID_HANDLE)
 {
     setWidth(w);
     setHeight(h);
@@ -614,7 +618,13 @@
 {
     DEFINE_HOST_CONNECTION;
     if (rcEnc) {
-        if (rcColorBuffer) rcEnc->rcCloseColorBuffer(rcEnc, rcColorBuffer);
+        if (rcColorBuffer){
+            if(qemu_pipe_valid(refcountPipeFd)) {
+                qemu_pipe_close(refcountPipeFd);
+            } else {
+                rcEnc->rcCloseColorBuffer(rcEnc, rcColorBuffer);
+            }
+        }
         if (rcSurface)     rcEnc->rcDestroyWindowSurface(rcEnc, rcSurface);
     }
 }
@@ -665,6 +675,12 @@
     if (!rcColorBuffer) {
         ALOGE("rcCreateColorBuffer returned 0");
         return EGL_FALSE;
+    } else {
+        refcountPipeFd = qemu_pipe_open("refcount");
+        //Send color buffer handle in case RefCountPipe feature is turned on.
+        if (qemu_pipe_valid(refcountPipeFd)) {
+            qemu_pipe_write(refcountPipeFd, &rcColorBuffer, 4);
+        }
     }
 
     rcEnc->rcSetWindowColorBuffer(rcEnc, rcSurface, rcColorBuffer);
diff --git a/system/gralloc/gralloc.cpp b/system/gralloc/gralloc.cpp
index e3bf5d6..48b4079 100644
--- a/system/gralloc/gralloc.cpp
+++ b/system/gralloc/gralloc.cpp
@@ -34,6 +34,8 @@
 #include "ProcessPipe.h"
 #include "ThreadInfo.h"
 #include "glUtils.h"
+#include "qemu_pipe.h"
+
 #include <cutils/log.h>
 #include <cutils/properties.h>
 
@@ -744,7 +746,6 @@
             } else {
                 cb->hostHandle = rcEnc->rcCreateColorBuffer(rcEnc, w, h, allocFormat);
             }
-            D("Created host ColorBuffer 0x%x\n", cb->hostHandle);
         }
 
         if (!cb->hostHandle) {
@@ -753,6 +754,13 @@
             delete cb;
             ALOGD("%s: failed to create host cb! -EIO", __FUNCTION__);
             return -EIO;
+        } else {
+            QEMU_PIPE_HANDLE refcountPipeFd = qemu_pipe_open("refcount");
+            if(qemu_pipe_valid(refcountPipeFd)) {
+                cb->setRefcountPipeFd(refcountPipeFd);
+                qemu_pipe_write(refcountPipeFd, &cb->hostHandle, 4);
+            }
+            D("Created host ColorBuffer 0x%x\n", cb->hostHandle);
         }
 
         if (isHidlGralloc) { *getOpenCountPtr(cb) = 0; }
@@ -798,7 +806,7 @@
     D("%s: for buf %p ptr %p size %d\n",
       __FUNCTION__, handle, cb->ashmemBase, cb->ashmemSize);
 
-    if (cb->hostHandle) {
+    if (cb->hostHandle && !cb->hasRefcountPipe()) {
         int32_t openCount = 1;
         int32_t* openCountPtr = &openCount;
 
@@ -828,6 +836,9 @@
         close(cb->fd);
     }
 
+    if(qemu_pipe_valid(cb->refcount_pipe_fd)) {
+        qemu_pipe_close(cb->refcount_pipe_fd);
+    }
     D("%s: done", __FUNCTION__);
     // remove it from the allocated list
     gralloc_device_t *grdev = (gralloc_device_t *)dev;
@@ -988,7 +999,7 @@
     D("gralloc_register_buffer(%p) w %d h %d format 0x%x framworkFormat 0x%x",
         handle, cb->width, cb->height, cb->format, cb->frameworkFormat);
 
-    if (cb->hostHandle != 0) {
+    if (cb->hostHandle != 0 && !cb->hasRefcountPipe()) {
         DEFINE_AND_VALIDATE_HOST_CONNECTION;
         D("Opening host ColorBuffer 0x%x\n", cb->hostHandle);
         rcEnc->rcOpenColorBuffer2(rcEnc, cb->hostHandle);
@@ -1044,7 +1055,7 @@
     }
 
 
-    if (cb->hostHandle) {
+    if (cb->hostHandle && !cb->hasRefcountPipe()) {
         D("Closing host ColorBuffer 0x%x\n", cb->hostHandle);
         DEFINE_AND_VALIDATE_HOST_CONNECTION;
         rcEnc->rcCloseColorBuffer(rcEnc, cb->hostHandle);