Merge changes I6c489eb4,I552b40c0 am: 75c88ab266
am: 174976cf78

Change-Id: Ie2fdb6db6cfd75929329f92b5fafdd8fd6462531
diff --git a/gralloc/alloc_device.cpp b/gralloc/alloc_device.cpp
index 3ef099f..6773405 100644
--- a/gralloc/alloc_device.cpp
+++ b/gralloc/alloc_device.cpp
@@ -128,17 +128,19 @@
 			return -1;
 		}
 
+		// we do not need ion_hnd once we have shared_fd
+		if (0 != ion_free(m->ion_client, ion_hnd))
+		{
+			AWAR("ion_free( %d ) failed", m->ion_client);
+		}
+		ion_hnd = ION_INVALID_HANDLE;
+
 		cpu_ptr = (unsigned char *)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, shared_fd, 0);
 
 		if (MAP_FAILED == cpu_ptr)
 		{
 			AERR("ion_map( %d ) failed", m->ion_client);
 
-			if (0 != ion_free(m->ion_client, ion_hnd))
-			{
-				AERR("ion_free( %d ) failed", m->ion_client);
-			}
-
 			close(shared_fd);
 			return -1;
 		}
@@ -148,7 +150,6 @@
 		if (NULL != hnd)
 		{
 			hnd->share_fd = shared_fd;
-			hnd->ion_hnd = ion_hnd;
 			*pHandle = hnd;
 			return 0;
 		}
@@ -165,13 +166,6 @@
 			AERR("munmap failed for base:%p size: %lu", cpu_ptr, (unsigned long)size);
 		}
 
-		ret = ion_free(m->ion_client, ion_hnd);
-
-		if (0 != ret)
-		{
-			AERR("ion_free( %d ) failed", m->ion_client);
-		}
-
 		return -1;
 	}
 #endif
@@ -306,9 +300,10 @@
 		vaddr = (void *)((uintptr_t)vaddr + bufferSize);
 	}
 
+	int fbdev_fd = m->framebuffer->shallow_fbdev_fd;
 	// The entire framebuffer memory is already mapped, now create a buffer object for parts of this memory
 	private_handle_t *hnd = new private_handle_t(private_handle_t::PRIV_FLAGS_FRAMEBUFFER, usage, size, vaddr,
-	        0, dup(m->framebuffer->fd), (uintptr_t)vaddr - (uintptr_t) m->framebuffer->base);
+	        0, fbdev_fd, (uintptr_t)vaddr - (uintptr_t) m->framebuffer->base);
 #if GRALLOC_ARM_UMP_MODULE
 	hnd->ump_id = m->framebuffer->ump_id;
 
@@ -330,7 +325,7 @@
 #ifdef FBIOGET_DMABUF
 		struct fb_dmabuf_export fb_dma_buf;
 
-		if (ioctl(m->framebuffer->fd, FBIOGET_DMABUF, &fb_dma_buf) == 0)
+		if (ioctl(fbdev_fd, FBIOGET_DMABUF, &fb_dma_buf) == 0)
 		{
 			AINF("framebuffer accessed with dma buf (fd 0x%x)\n", (int)fb_dma_buf.fd);
 			hnd->share_fd = fb_dma_buf.fd;
@@ -518,7 +513,6 @@
 		const size_t bufferSize = m->finfo.line_length * m->info.yres;
 		int index = ((uintptr_t)hnd->base - (uintptr_t)m->framebuffer->base) / bufferSize;
 		m->bufferMask &= ~(1 << index);
-		close(hnd->fd);
 
 #if GRALLOC_ARM_UMP_MODULE
 
@@ -560,11 +554,6 @@
 
 		close(hnd->share_fd);
 
-		if (0 != ion_free(m->ion_client, hnd->ion_hnd))
-		{
-			AERR("Failed to ion_free( ion_client: %d ion_hnd: %p )", m->ion_client, (void *)(uintptr_t)hnd->ion_hnd);
-		}
-
 		memset((void *)hnd, 0, sizeof(*hnd));
 #else
 		AERR("Can't free dma_buf memory for handle:0x%x. Not supported.", (unsigned int)hnd);
diff --git a/gralloc/framebuffer_device.cpp b/gralloc/framebuffer_device.cpp
index 3004778..bacefee 100644
--- a/gralloc/framebuffer_device.cpp
+++ b/gralloc/framebuffer_device.cpp
@@ -94,9 +94,11 @@
 #define FBIO_WAITFORVSYNC       _IOW('F', 0x20, __u32)
 #define S3CFB_SET_VSYNC_INT _IOW('F', 206, unsigned int)
 
-		if (ioctl(m->framebuffer->fd, FBIOPAN_DISPLAY, &m->info) == -1)
+		int fbdev_fd = m->framebuffer->shallow_fbdev_fd;
+
+		if (ioctl(fbdev_fd, FBIOPAN_DISPLAY, &m->info) == -1)
 		{
-			AERR("FBIOPAN_DISPLAY failed for fd: %d", m->framebuffer->fd);
+			AERR("FBIOPAN_DISPLAY failed for fd: %d", fbdev_fd);
 			m->base.unlock(&m->base, buffer);
 			return 0;
 		}
@@ -106,9 +108,9 @@
 			// enable VSYNC
 			interrupt = 1;
 
-			if (ioctl(m->framebuffer->fd, S3CFB_SET_VSYNC_INT, &interrupt) < 0)
+			if (ioctl(fbdev_fd, S3CFB_SET_VSYNC_INT, &interrupt) < 0)
 			{
-				//      AERR("S3CFB_SET_VSYNC_INT enable failed for fd: %d", m->framebuffer->fd);
+				//      AERR("S3CFB_SET_VSYNC_INT enable failed for fd: %d", fbdev_fd);
 				return 0;
 			}
 
@@ -118,9 +120,9 @@
 #endif
 			int crtc = 0;
 
-			if (ioctl(m->framebuffer->fd, FBIO_WAITFORVSYNC, &crtc) < 0)
+			if (ioctl(fbdev_fd, FBIO_WAITFORVSYNC, &crtc) < 0)
 			{
-				AERR("FBIO_WAITFORVSYNC failed for fd: %d", m->framebuffer->fd);
+				AERR("FBIO_WAITFORVSYNC failed for fd: %d", fbdev_fd);
 #ifdef MALI_VSYNC_EVENT_REPORT_ENABLE
 				gralloc_mali_vsync_report(MALI_VSYNC_EVENT_END_WAIT);
 #endif
@@ -133,9 +135,9 @@
 			// disable VSYNC
 			interrupt = 0;
 
-			if (ioctl(m->framebuffer->fd, S3CFB_SET_VSYNC_INT, &interrupt) < 0)
+			if (ioctl(fbdev_fd, S3CFB_SET_VSYNC_INT, &interrupt) < 0)
 			{
-				AERR("S3CFB_SET_VSYNC_INT disable failed for fd: %d", m->framebuffer->fd);
+				AERR("S3CFB_SET_VSYNC_INT disable failed for fd: %d", fbdev_fd);
 				return 0;
 			}
 		}
@@ -146,9 +148,9 @@
 		gralloc_mali_vsync_report(MALI_VSYNC_EVENT_BEGIN_WAIT);
 #endif
 
-		if (ioctl(m->framebuffer->fd, FBIOPUT_VSCREENINFO, &m->info) == -1)
+		if (ioctl(fbdev_fd, FBIOPUT_VSCREENINFO, &m->info) == -1)
 		{
-			AERR("FBIOPUT_VSCREENINFO failed for fd: %d", m->framebuffer->fd);
+			AERR("FBIOPUT_VSCREENINFO failed for fd: %d", fbdev_fd);
 #ifdef MALI_VSYNC_EVENT_REPORT_ENABLE
 			gralloc_mali_vsync_report(MALI_VSYNC_EVENT_END_WAIT);
 #endif
@@ -383,7 +385,7 @@
 
 	// Create a "fake" buffer object for the entire frame buffer memory, and store it in the module
 	module->framebuffer = new private_handle_t(private_handle_t::PRIV_FLAGS_FRAMEBUFFER, 0, fbSize, vaddr,
-	        0, dup(fd), 0);
+	        0, fd, 0);
 
 	module->numBuffers = info.yres_virtual / info.yres;
 	module->bufferMask = 0;
diff --git a/gralloc/gralloc_priv.h b/gralloc/gralloc_priv.h
index 5797725..547027e 100644
--- a/gralloc/gralloc_priv.h
+++ b/gralloc/gralloc_priv.h
@@ -182,11 +182,11 @@
 #endif
 
 	// Following members is for framebuffer only
-	int     fd;
+	int     shallow_fbdev_fd; // shallow copy, not dup'ed
 	int     offset;
 
 #if GRALLOC_ARM_DMA_BUF_MODULE
-	ion_user_handle_t ion_hnd;
+	ion_user_handle_t ion_hnd_UNUSED;
 #endif
 
 #if GRALLOC_ARM_DMA_BUF_MODULE
@@ -219,11 +219,11 @@
 		yuv_info(MALI_YUV_NO_INFO),
 		ump_id((int)secure_id),
 		ump_mem_handle((int)handle),
-		fd(0),
+		shallow_fbdev_fd(0),
 		offset(0)
 #if GRALLOC_ARM_DMA_BUF_MODULE
 		,
-		ion_hnd(ION_INVALID_HANDLE)
+		ion_hnd_UNUSED(ION_INVALID_HANDLE)
 #endif
 
 	{
@@ -253,9 +253,9 @@
 		ump_id((int)UMP_INVALID_SECURE_ID),
 		ump_mem_handle((int)UMP_INVALID_MEMORY_HANDLE),
 #endif
-		fd(0),
+		shallow_fbdev_fd(0),
 		offset(0),
-		ion_hnd(ION_INVALID_HANDLE)
+		ion_hnd_UNUSED(ION_INVALID_HANDLE)
 
 	{
 		version = sizeof(native_handle);
@@ -286,11 +286,11 @@
 		ump_id((int)UMP_INVALID_SECURE_ID),
 		ump_mem_handle((int)UMP_INVALID_MEMORY_HANDLE),
 #endif
-		fd(fb_file),
+		shallow_fbdev_fd(fb_file),
 		offset(fb_offset)
 #if GRALLOC_ARM_DMA_BUF_MODULE
 		,
-		ion_hnd(ION_INVALID_HANDLE)
+		ion_hnd_UNUSED(ION_INVALID_HANDLE)
 #endif
 
 	{