gralloc: Import utgard r8p1 gralloc update

This imports the utgard r8p1 gralloc update.

No changes made, so its not expected to build
properly

Change-Id: I7c8948f36fe2d6b265eaad9f23bcb9a1a28be08f
Signed-off-by: John Stultz <john.stultz@linaro.org>
diff --git a/gralloc/gralloc_module.cpp b/gralloc/gralloc_module.cpp
index 38ed3da..20f833d 100644
--- a/gralloc/gralloc_module.cpp
+++ b/gralloc/gralloc_module.cpp
@@ -18,7 +18,7 @@
 
 #include <errno.h>
 #include <pthread.h>
-#include <string.h>
+#include <sync/sync.h>
 
 #include <cutils/log.h>
 #include <cutils/atomic.h>
@@ -64,7 +64,7 @@
 
 	if (private_handle_t::validate(handle) < 0)
 	{
-		AERR("Registering invalid buffer 0x%p, returning error", handle);
+		AERR("Registering invalid buffer %p, returning error", handle);
 		return -EINVAL;
 	}
 
@@ -97,9 +97,8 @@
 
 	if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)
 	{
-		AINF("Register framebuffer 0x%p is no-op", handle);
+		AINF("Register buffer %p although it will be treated as a nop", handle);
 		retval = 0;
-
 	}
 	else if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_UMP)
 	{
@@ -114,6 +113,7 @@
 			{
 				hnd->writeOwner = 0;
 				hnd->lockState &= ~(private_handle_t::LOCK_STATE_UNREGISTERED);
+
 				pthread_mutex_unlock(&s_map_lock);
 				return 0;
 			}
@@ -130,12 +130,13 @@
 		}
 
 #else
-		AERR("Gralloc does not support UMP. Unable to register UMP memory for handle 0x%p", hnd);
+		AERR("Gralloc does not support UMP. Unable to register UMP memory for handle %p", hnd);
 #endif
 	}
 	else if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION)
 	{
 #if GRALLOC_ARM_DMA_BUF_MODULE
+		int ret;
 		unsigned char *mappedAddress;
 		size_t size = hnd->size;
 		hw_module_t *pmodule = NULL;
@@ -147,7 +148,7 @@
 		}
 		else
 		{
-			AERR("Could not get gralloc module for handle: 0x%p", hnd);
+			AERR("Could not get gralloc module for handle: %p", hnd);
 			retval = -errno;
 			goto cleanup;
 		}
@@ -163,7 +164,7 @@
 
 			if (m->ion_client < 0)
 			{
-				AERR("Could not open ion device for handle: 0x%p", hnd);
+				AERR("Could not open ion device for handle: %p", hnd);
 				retval = -errno;
 				goto cleanup;
 			}
@@ -180,6 +181,7 @@
 
 		hnd->base = mappedAddress + hnd->offset;
 		hnd->lockState &= ~(private_handle_t::LOCK_STATE_UNREGISTERED);
+
 		pthread_mutex_unlock(&s_map_lock);
 		return 0;
 #endif
@@ -189,7 +191,9 @@
 		AERR("registering non-UMP buffer not supported. flags = %d", hnd->flags);
 	}
 
+#if GRALLOC_ARM_DMA_BUF_MODULE
 cleanup:
+#endif
 	pthread_mutex_unlock(&s_map_lock);
 	return retval;
 }
@@ -203,7 +207,7 @@
 		ump_reference_release((ump_handle)hnd->ump_mem_handle);
 		hnd->ump_mem_handle = (int)UMP_INVALID_MEMORY_HANDLE;
 #else
-		AERR("Can't unregister UMP buffer for handle 0x%p. Not supported", hnd);
+		AERR("Can't unregister UMP buffer for handle %p. Not supported", hnd);
 #endif
 	}
 	else if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION)
@@ -214,7 +218,7 @@
 
 		if (munmap(base, size) < 0)
 		{
-			AERR("Could not munmap base:0x%p size:%lu '%s'", base, (unsigned long)size, strerror(errno));
+			AERR("Could not munmap base:%p size:%lu '%s'", base, (unsigned long)size, strerror(errno));
 		}
 
 #else
@@ -238,7 +242,7 @@
 
 	if (private_handle_t::validate(handle) < 0)
 	{
-		AERR("unregistering invalid buffer 0x%p, returning error", handle);
+		AERR("unregistering invalid buffer %p, returning error", handle);
 		return -EINVAL;
 	}
 
@@ -248,7 +252,7 @@
 
 	if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)
 	{
-		AERR("Can't unregister buffer 0x%p as it is a framebuffer", handle);
+		AERR("Can't unregister buffer %p as it is a framebuffer", handle);
 	}
 	else if (hnd->pid == getpid()) // never unmap buffers that were not registered in this process
 	{
@@ -268,7 +272,7 @@
 	}
 	else
 	{
-		AERR("Trying to unregister buffer 0x%p from process %d that was not created in current process: %d", hnd, hnd->pid, getpid());
+		AERR("Trying to unregister buffer %p from process %d that was not created in current process: %d", hnd, hnd->pid, getpid());
 	}
 
 	return 0;
@@ -276,19 +280,27 @@
 
 static int gralloc_lock(gralloc_module_t const *module, buffer_handle_t handle, int usage, int l, int t, int w, int h, void **vaddr)
 {
+
+
 	if (private_handle_t::validate(handle) < 0)
 	{
-		AERR("Locking invalid buffer 0x%p, returning error", handle);
+		AERR("Locking invalid buffer %p, returning error", handle);
 		return -EINVAL;
 	}
 
 	private_handle_t *hnd = (private_handle_t *)handle;
 
+	if (hnd->format == HAL_PIXEL_FORMAT_YCbCr_420_888)
+	{
+		AERR("Buffer with format HAL_PIXEL_FORMAT_YCbCr_*_888 must be locked by lock_ycbcr()");
+		return -EINVAL;
+	}
+
 	pthread_mutex_lock(&s_map_lock);
 
 	if (hnd->lockState & private_handle_t::LOCK_STATE_UNREGISTERED)
 	{
-		AERR("Locking on an unregistered buffer 0x%p, returning error", hnd);
+		AERR("Locking on an unregistered buffer %p, returning error", hnd);
 		pthread_mutex_unlock(&s_map_lock);
 		return -EINVAL;
 	}
@@ -315,13 +327,93 @@
 	return 0;
 }
 
+static int gralloc_lock_ycbcr(gralloc_module_t const *module, buffer_handle_t handle, int usage, int l, int t, int w, int h, struct android_ycbcr *ycbcr)
+{
+	int retval = 0;
+	int ystride, cstride;
+
+	if (private_handle_t::validate(handle) < 0)
+	{
+		AERR("Locking invalid buffer %p, returning error", handle);
+		return -EINVAL;
+	}
+
+	private_handle_t *hnd = (private_handle_t *)handle;
+
+	pthread_mutex_lock(&s_map_lock);
+
+	if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_UMP || hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION)
+	{
+		hnd->writeOwner = usage & GRALLOC_USAGE_SW_WRITE_MASK;
+	}
+
+	hnd->lockState |= private_handle_t::LOCK_STATE_WRITE;
+
+	pthread_mutex_unlock(&s_map_lock);
+
+
+	if (usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK))
+	{
+		switch (hnd->format)
+		{
+			case HAL_PIXEL_FORMAT_YCrCb_420_SP:
+				ystride = cstride = GRALLOC_ALIGN(hnd->width, 16);
+				ycbcr->y  = (void *)hnd->base;
+				ycbcr->cr = (void *)((unsigned char *)hnd->base + ystride * hnd->height);
+				ycbcr->cb = (void *)((unsigned char *)hnd->base + ystride * hnd->height + 1);
+				ycbcr->ystride = ystride;
+				ycbcr->cstride = cstride;
+				ycbcr->chroma_step = 2;
+				break;
+
+			case HAL_PIXEL_FORMAT_YV12:
+				/* Here to keep consistency with YV12 alignment, define the ystride according to image height. */
+				ystride = GRALLOC_ALIGN(hnd->width, (hnd->height % 8 == 0) ? GRALLOC_ALIGN_BASE_16 :
+										  		   ((hnd->height % 4 == 0) ? GRALLOC_ALIGN_BASE_64 : GRALLOC_ALIGN_BASE_128));
+				cstride = GRALLOC_ALIGN(ystride / 2, 16);
+				ycbcr->y  = (void *)hnd->base;
+				/* the ystride calc is assuming the height can at least be divided by 2 */
+				ycbcr->cr = (void *)((unsigned char *)hnd->base + ystride * GRALLOC_ALIGN(hnd->height, 2));
+				ycbcr->cb = (void *)((unsigned char *)hnd->base + ystride * GRALLOC_ALIGN(hnd->height, 2) + cstride * hnd->height / 2);
+				ycbcr->ystride = ystride;
+				ycbcr->cstride = cstride;
+				ycbcr->chroma_step = 1;
+				break;
+
+#ifdef SUPPORT_LEGACY_FORMAT
+
+			case HAL_PIXEL_FORMAT_YCbCr_420_SP:
+				ystride = cstride = GRALLOC_ALIGN(hnd->width, 16);
+				ycbcr->y  = (void *)hnd->base;
+				ycbcr->cb = (void *)((unsigned char *)hnd->base + ystride * hnd->height);
+				ycbcr->cr = (void *)((unsigned char *)hnd->base + ystride * hnd->height + 1);
+				ycbcr->ystride = ystride;
+				ycbcr->cstride = cstride;
+				ycbcr->chroma_step = 2;
+				break;
+#endif
+
+			default:
+				AERR("Can not lock buffer, invalid format: 0x%x", hnd->format);
+				retval = -EINVAL;
+		}
+	}
+
+	MALI_IGNORE(module);
+	MALI_IGNORE(l);
+	MALI_IGNORE(t);
+	MALI_IGNORE(w);
+	MALI_IGNORE(h);
+	return retval;
+}
+
 static int gralloc_unlock(gralloc_module_t const *module, buffer_handle_t handle)
 {
 	MALI_IGNORE(module);
 
 	if (private_handle_t::validate(handle) < 0)
 	{
-		AERR("Unlocking invalid buffer 0x%p, returning error", handle);
+		AERR("Unlocking invalid buffer %p, returning error", handle);
 		return -EINVAL;
 	}
 
@@ -332,18 +424,25 @@
 #if GRALLOC_ARM_UMP_MODULE
 		ump_cpu_msync_now((ump_handle)hnd->ump_mem_handle, UMP_MSYNC_CLEAN_AND_INVALIDATE, (void *)hnd->base, hnd->size);
 #else
-		AERR("Buffer 0x%p is UMP type but it is not supported", hnd);
+		AERR("Buffer %p is UMP type but it is not supported", hnd);
 #endif
 	}
 	else if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION && hnd->writeOwner)
 	{
 #if GRALLOC_ARM_DMA_BUF_MODULE
 		hw_module_t *pmodule = NULL;
+		private_module_t *m = NULL;
 
-		if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, (const hw_module_t **)&pmodule) != 0)
+		if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, (const hw_module_t **)&pmodule) == 0)
 		{
-			AERR("Couldnot get gralloc module for handle 0x%p\n", handle);
+			m = reinterpret_cast<private_module_t *>(pmodule);
+			//ion_sync_fd(m->ion_client, hnd->share_fd);
 		}
+		else
+		{
+			AERR("Couldnot get gralloc module for handle %p\n", handle);
+		}
+
 #endif
 	}
 
@@ -362,11 +461,48 @@
 	return 0;
 }
 
+#if defined(GRALLOC_MODULE_API_VERSION_0_3)
+static int gralloc_lock_async (gralloc_module_t const *module, buffer_handle_t handle, int usage, int l, int t, int w, int h, void **vaddr, int fenceFD)
+{
+	if (fenceFD >= 0)
+	{
+		sync_wait(fenceFD, -1);
+		close(fenceFD);
+	}
+
+	return gralloc_lock(module, handle, usage, l, t, w, h, vaddr);
+}
+
+static int gralloc_unlock_async(gralloc_module_t const *module, buffer_handle_t handle, int *fenceFD)
+{
+	*fenceFD = -1;
+
+	if (gralloc_unlock(module, handle) < 0)
+	{
+		return -EINVAL;
+	}
+
+	return 0;
+
+}
+
+static int gralloc_lock_async_ycbcr(gralloc_module_t const *module, buffer_handle_t handle, int usage, int l, int t, int w, int h, struct android_ycbcr *ycbcr, int fenceFD)
+{
+	if (fenceFD >= 0)
+	{
+		sync_wait(fenceFD, -1);
+		close(fenceFD);
+	}
+
+	return gralloc_lock_ycbcr(module, handle, usage, l, t, w, h, ycbcr);
+}
+#endif
+
 // There is one global instance of the module
 
 static struct hw_module_methods_t gralloc_module_methods =
 {
-	.open =	gralloc_device_open,
+	.open = gralloc_device_open
 };
 
 private_module_t::private_module_t()
@@ -374,7 +510,11 @@
 #define INIT_ZERO(obj) (memset(&(obj),0,sizeof((obj))))
 
 	base.common.tag = HARDWARE_MODULE_TAG;
-	base.common.version_major = 1;
+#if defined(GRALLOC_MODULE_API_VERSION_0_3)
+	base.common.version_major = GRALLOC_MODULE_API_VERSION_0_3;
+#else
+	base.common.version_major = GRALLOC_MODULE_API_VERSION_0_2;
+#endif
 	base.common.version_minor = 0;
 	base.common.id = GRALLOC_HARDWARE_MODULE_ID;
 	base.common.name = "Graphics Memory Allocator Module";
@@ -388,6 +528,12 @@
 	base.lock = gralloc_lock;
 	base.unlock = gralloc_unlock;
 	base.perform = NULL;
+	base.lock_ycbcr = gralloc_lock_ycbcr;
+#if defined(GRALLOC_MODULE_API_VERSION_0_3)
+	base.lockAsync = gralloc_lock_async;
+	base.unlockAsync = gralloc_unlock_async;
+	base.lockAsync_ycbcr = gralloc_lock_async_ycbcr;
+#endif
 	INIT_ZERO(base.reserved_proc);
 
 	framebuffer = NULL;