gralloc: Update gralloc source to e219158a67fbfbedbac9

This patch updates the gralloc source to:
   Subject: Add support for hikey
   Sha1:    e219158a67fbfbedbac99e15809b948313aeb5fa

From Xinliang's tree here:
https://github.com/xin3liang/gralloc-mali.git

Also inlcudes minor whitespace/style fixes and corrected
LOCK_STATE_READ_MASK value, noticed in review on Gerrit.

These changes are needed to support the r6p0 version of
the binary mali graphics libraries.

Change-Id: Ia33671447b36fd83da544af6da8ec6ce48e80b05
Signed-off-by: John Stultz <john.stultz@linaro.org>
diff --git a/gralloc/Android.mk b/gralloc/Android.mk
index 7dcec5c..82c6f8f 100644
--- a/gralloc/Android.mk
+++ b/gralloc/Android.mk
@@ -1,6 +1,4 @@
-#
 # Copyright (C) 2010 ARM Limited. All rights reserved.
-#
 # Copyright (C) 2008 The Android Open Source Project
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
@@ -21,29 +19,10 @@
 # HAL module implemenation, not prelinked and stored in
 # hw/<OVERLAY_HARDWARE_MODULE_ID>.<ro.product.board>.so
 include $(CLEAR_VARS)
-LOCAL_PRELINK_MODULE := false
 
-ifeq ($(shell test $(PLATFORM_SDK_VERSION) -ge 21 && echo OK),OK)
-	LOCAL_MODULE_RELATIVE_PATH := hw
-else
-	LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hw
-endif
-
-MALI_DDK_TEST_PATH := hardware/arm/
-
-LOCAL_MODULE := gralloc.$(TARGET_BOARD_PLATFORM)
-#LOCAL_MODULE_TAGS := optional
-
-# Mali-200/300/400MP DDK
-MALI_DDK_PATH := hardware/arm/mali
-#SHARED_MEM_LIBS := libUMP
 SHARED_MEM_LIBS := libion libhardware
 LOCAL_SHARED_LIBRARIES := liblog libcutils libGLESv1_CM $(SHARED_MEM_LIBS)
-
-LOCAL_C_INCLUDES := system/core/include/ $(MALI_DDK_PATH)/include
-# Include the UMP header files
-# LOCAL_C_INCLUDES += $(MALI_DDK_PATH)/src/ump/include
-
+LOCAL_C_INCLUDES := system/core/include/
 LOCAL_CFLAGS := -DLOG_TAG=\"gralloc\" -DGRALLOC_32_BITS -DSTANDARD_LINUX_SCREEN -DPLATFORM_SDK_VERSION=$(PLATFORM_SDK_VERSION)
 
 LOCAL_SRC_FILES := \
@@ -51,5 +30,7 @@
 	alloc_device.cpp \
 	framebuffer_device.cpp
 
-#LOCAL_CFLAGS+= -DMALI_VSYNC_EVENT_REPORT_ENABLE
+LOCAL_MODULE := gralloc.$(TARGET_BOARD_PLATFORM)
+LOCAL_MODULE_RELATIVE_PATH := hw
+
 include $(BUILD_SHARED_LIBRARY)
diff --git a/gralloc/framebuffer_device.cpp b/gralloc/framebuffer_device.cpp
index 48e58f8..cfc480f 100644
--- a/gralloc/framebuffer_device.cpp
+++ b/gralloc/framebuffer_device.cpp
@@ -420,7 +420,7 @@
 #if GRALLOC_ARM_UMP_MODULE
 		ump_close();
 #endif
-		delete dev;
+		free(dev);
 	}
 
 	return 0;
@@ -468,7 +468,7 @@
 	}
 
 	/* initialize our state here */
-	framebuffer_device_t *dev = (framebuffer_device_t*)malloc(sizeof(framebuffer_device_t));
+	framebuffer_device_t *dev = (framebuffer_device_t *)malloc(sizeof(framebuffer_device_t));
 	memset(dev, 0, sizeof(*dev));
 
 	/* initialize the procs */
diff --git a/gralloc/gralloc_module.cpp b/gralloc/gralloc_module.cpp
index 6dcba15..cdcbc5b 100644
--- a/gralloc/gralloc_module.cpp
+++ b/gralloc/gralloc_module.cpp
@@ -109,10 +109,8 @@
 
 			if (0 != hnd->base)
 			{
-				hnd->lockState = private_handle_t::LOCK_STATE_MAPPED;
 				hnd->writeOwner = 0;
-				hnd->lockState = 0;
-
+				hnd->lockState &= ~(private_handle_t::LOCK_STATE_UNREGISTERED);
 				pthread_mutex_unlock(&s_map_lock);
 				return 0;
 			}
@@ -179,6 +177,7 @@
 		}
 
 		hnd->base = mappedAddress + hnd->offset;
+		hnd->lockState &= ~(private_handle_t::LOCK_STATE_UNREGISTERED);
 		pthread_mutex_unlock(&s_map_lock);
 		return 0;
 #endif
@@ -193,6 +192,44 @@
 	return retval;
 }
 
+static void unmap_buffer(private_handle_t *hnd)
+{
+	if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_UMP)
+	{
+#if GRALLOC_ARM_UMP_MODULE
+		ump_mapped_pointer_release((ump_handle)hnd->ump_mem_handle);
+		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);
+#endif
+	}
+	else if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION)
+	{
+#if GRALLOC_ARM_DMA_BUF_MODULE
+		void *base = (void *)hnd->base;
+		size_t size = hnd->size;
+
+		if (munmap(base, size) < 0)
+		{
+			AERR("Could not munmap base:0x%p size:%lu '%s'", base, (unsigned long)size, strerror(errno));
+		}
+
+#else
+		AERR("Can't unregister DMA_BUF buffer for hnd %p. Not supported", hnd);
+#endif
+
+	}
+	else
+	{
+		AERR("Unregistering unknown buffer is not supported. Flags = %d", hnd->flags);
+	}
+
+	hnd->base = 0;
+	hnd->lockState = 0;
+	hnd->writeOwner = 0;
+}
+
 static int gralloc_unregister_buffer(gralloc_module_t const *module, buffer_handle_t handle)
 {
 	MALI_IGNORE(module);
@@ -215,40 +252,15 @@
 	{
 		pthread_mutex_lock(&s_map_lock);
 
-		if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_UMP)
-		{
-#if GRALLOC_ARM_UMP_MODULE
-			ump_mapped_pointer_release((ump_handle)hnd->ump_mem_handle);
-			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", handle);
-#endif
-		}
-		else if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION)
-		{
-#if GRALLOC_ARM_DMA_BUF_MODULE
-			void *base = (void *)hnd->base;
-			size_t size = hnd->size;
+		hnd->lockState &= ~(private_handle_t::LOCK_STATE_MAPPED);
 
-			if (munmap(base, size) < 0)
-			{
-				AERR("Could not munmap base:0x%p size:%lu '%s'", base, (unsigned long)size, strerror(errno));
-			}
-
-#else
-			AERR("Can't unregister DMA_BUF buffer for hnd %p. Not supported", hnd);
-#endif
-
-		}
-		else
+		/* if handle is still locked, the unmapping would not happen until unlocked*/
+		if (!(hnd->lockState & private_handle_t::LOCK_STATE_WRITE))
 		{
-			AERR("Unregistering unknown buffer is not supported. Flags = %d", hnd->flags);
+			unmap_buffer(hnd);
 		}
 
-		hnd->base = 0;
-		hnd->lockState  = 0;
-		hnd->writeOwner = 0;
+		hnd->lockState |= private_handle_t::LOCK_STATE_UNREGISTERED;
 
 		pthread_mutex_unlock(&s_map_lock);
 	}
@@ -270,11 +282,24 @@
 
 	private_handle_t *hnd = (private_handle_t *)handle;
 
+	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);
+		pthread_mutex_unlock(&s_map_lock);
+		return -EINVAL;
+	}
+
 	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))
 	{
 		*vaddr = (void *)hnd->base;
@@ -330,6 +355,18 @@
 #endif
 	}
 
+	pthread_mutex_lock(&s_map_lock);
+
+	hnd->lockState &= ~(private_handle_t::LOCK_STATE_WRITE);
+
+	/* if the handle has already been unregistered, unmap it here*/
+	if (hnd->lockState & private_handle_t::LOCK_STATE_UNREGISTERED)
+	{
+		unmap_buffer(hnd);
+	}
+
+	pthread_mutex_unlock(&s_map_lock);
+
 	return 0;
 }
 
@@ -337,8 +374,7 @@
 
 static struct hw_module_methods_t gralloc_module_methods =
 {
-open:
-	gralloc_device_open
+	.open =	gralloc_device_open,
 };
 
 private_module_t::private_module_t()
diff --git a/gralloc/gralloc_priv.h b/gralloc/gralloc_priv.h
index 7b33ae2..5797725 100644
--- a/gralloc/gralloc_priv.h
+++ b/gralloc/gralloc_priv.h
@@ -147,7 +147,8 @@
 	{
 		LOCK_STATE_WRITE     =   1 << 31,
 		LOCK_STATE_MAPPED    =   1 << 30,
-		LOCK_STATE_READ_MASK =   0x3FFFFFFF
+		LOCK_STATE_UNREGISTERED  =   1 << 29,
+		LOCK_STATE_READ_MASK =   0x1FFFFFFF
 	};
 
 	// ints