Merge changes from topic "gralloc-drm_hwc-prep" am: 9820de4a40
am: 4d323b37ea

Change-Id: Ib8431b604a5a12f7bd2ab6c434ab198da0df9416
diff --git a/gralloc/Android.mk b/gralloc/Android.mk
index 8c41842..23a5a82 100644
--- a/gralloc/Android.mk
+++ b/gralloc/Android.mk
@@ -29,6 +29,7 @@
 MALI_DDK_TEST_PATH := hardware/arm/
 
 LOCAL_MODULE := gralloc.hikey
+LOCAL_MODULE_RELATIVE_PATH := hw
 #LOCAL_MODULE_TAGS := optional
 
 # Mali-200/300/400MP DDK
@@ -49,4 +50,10 @@
 	framebuffer_device.cpp
 
 #LOCAL_CFLAGS+= -DMALI_VSYNC_EVENT_REPORT_ENABLE
+
+
+ifeq ($(HIKEY_USE_DRM_HWCOMPOSER), true)
+LOCAL_CFLAGS += -DDISABLE_FRAMEBUFFER_HAL
+endif
+
 include $(BUILD_SHARED_LIBRARY)
diff --git a/gralloc/alloc_device.cpp b/gralloc/alloc_device.cpp
index 076cb5c..b6bd69e 100644
--- a/gralloc/alloc_device.cpp
+++ b/gralloc/alloc_device.cpp
@@ -39,9 +39,12 @@
 #endif
 
 #if GRALLOC_ARM_DMA_BUF_MODULE
-#include <linux/ion.h>
 #include <ion/ion.h>
 #include "ion_4.12.h"
+
+#define ION_SYSTEM	(char*)"ion_system_heap"
+#define ION_CMA		(char*)"linux,cma"
+
 #endif
 
 #if GRALLOC_SIMULATE_FAILURES
@@ -145,7 +148,10 @@
 
 		if (m->gralloc_legacy_ion)
 		{
-			ret = ion_alloc(m->ion_client, size, 0, ION_HEAP_SYSTEM_MASK, 0, &(ion_hnd));
+			if (usage & GRALLOC_USAGE_HW_FB)
+				ret = ion_alloc(m->ion_client, size, 0, ION_HEAP_TYPE_DMA_MASK, 0, &(ion_hnd));
+			else
+				ret = ion_alloc(m->ion_client, size, 0, ION_HEAP_SYSTEM_MASK, 0, &(ion_hnd));
 
 			if (ret != 0)
 			{
@@ -176,7 +182,10 @@
 		}
 		else
 		{
-			ret = ion_alloc_fd(m->ion_client, size, 0, 1 << m->system_heap_id, 0, &(shared_fd));
+			if (usage & GRALLOC_USAGE_HW_FB)
+				ret = ion_alloc_fd(m->ion_client, size, 0, 1 << m->cma_heap_id, 0, &(shared_fd));
+			else
+				ret = ion_alloc_fd(m->ion_client, size, 0, 1 << m->system_heap_id, 0, &(shared_fd));
 
 			if (ret != 0)
 			{
@@ -319,6 +328,7 @@
 
 }
 
+#ifndef DISABLE_FRAMEBUFFER_HAL
 static int gralloc_alloc_framebuffer_locked(alloc_device_t *dev, size_t size, int usage, buffer_handle_t *pHandle)
 {
 	private_module_t *m = reinterpret_cast<private_module_t *>(dev->common.module);
@@ -428,6 +438,7 @@
 	pthread_mutex_unlock(&m->lock);
 	return err;
 }
+#endif /* DISABLE_FRAMEBUFFER_HAL */
 
 static int alloc_device_alloc(alloc_device_t *dev, int w, int h, int format, int usage, buffer_handle_t *pHandle, int *pStride)
 {
@@ -438,6 +449,7 @@
 
 	size_t size;
 	size_t stride;
+	int bpp = 1;
 
 	if (format == HAL_PIXEL_FORMAT_YCrCb_420_SP || format == HAL_PIXEL_FORMAT_YV12
 	        /* HAL_PIXEL_FORMAT_YCbCr_420_SP, HAL_PIXEL_FORMAT_YCbCr_420_P, HAL_PIXEL_FORMAT_YCbCr_422_I are not defined in Android.
@@ -489,7 +501,6 @@
 	}
 	else
 	{
-		int bpp = 0;
 
 		switch (format)
 		{
@@ -535,7 +546,7 @@
 
 	int err;
 
-#ifndef MALI_600
+#ifndef DISABLE_FRAMEBUFFER_HAL
 
 	if (usage & GRALLOC_USAGE_HW_FB)
 	{
@@ -590,7 +601,7 @@
 	hnd->height = h;
 	hnd->format = format;
 	hnd->stride = stride;
-
+	hnd->byte_stride = GRALLOC_ALIGN(w*bpp,64);
 	*pStride = stride;
 	return 0;
 }
@@ -682,9 +693,9 @@
 }
 
 #if GRALLOC_ARM_DMA_BUF_MODULE
-static int find_system_heap_id(int ion_client)
+static int find_ion_heap_id(int ion_client, char* name)
 {
-	int i, ret, cnt, system_heap_id = -1;
+	int i, ret, cnt, heap_id = -1;
 	struct ion_heap_data *data;
 
 	ret = ion_query_heap_cnt(ion_client, &cnt);
@@ -711,8 +722,8 @@
 	{
 		for (i = 0; i < cnt; i++) {
 			struct ion_heap_data *dat = (struct ion_heap_data *)data;
-			if (strcmp(dat[i].name, "ion_system_heap") == 0) {
-				system_heap_id = dat[i].heap_id;
+			if (strcmp(dat[i].name, name) == 0) {
+				heap_id = dat[i].heap_id;
 				break;
 			}
 		}
@@ -720,12 +731,12 @@
 		if (i > cnt)
 		{
 			AERR("No System Heap Found amongst %d heaps\n", cnt);
-			system_heap_id = -1;
+			heap_id = -1;
 		}
 	}
 
 	free(data);
-	return system_heap_id;
+	return heap_id;
 }
 #endif
 
@@ -779,8 +790,9 @@
 
 	if (!m->gralloc_legacy_ion)
 	{
-		m->system_heap_id = find_system_heap_id(m->ion_client);
-		if (m->system_heap_id < 0)
+		m->system_heap_id = find_ion_heap_id(m->ion_client, ION_SYSTEM);
+		m->cma_heap_id = find_ion_heap_id(m->ion_client, ION_CMA);
+		if (m->system_heap_id < 0 || m->cma_heap_id < 0)
 		{
 			delete dev;
 			ion_close(m->ion_client);
diff --git a/gralloc/gralloc_module.cpp b/gralloc/gralloc_module.cpp
index 8405473..4937b59 100644
--- a/gralloc/gralloc_module.cpp
+++ b/gralloc/gralloc_module.cpp
@@ -37,7 +37,6 @@
 #endif
 
 #if GRALLOC_ARM_DMA_BUF_MODULE
-#include <linux/ion.h>
 #include <ion/ion.h>
 #include <sys/mman.h>
 #endif
diff --git a/gralloc/gralloc_priv.h b/gralloc/gralloc_priv.h
index 71920e0..3330eae 100644
--- a/gralloc/gralloc_priv.h
+++ b/gralloc/gralloc_priv.h
@@ -28,7 +28,7 @@
 
 #include <hardware/gralloc.h>
 #include <cutils/native_handle.h>
-#include <alloc_device.h>
+#include "alloc_device.h"
 #include <utils/Log.h>
 
 #ifdef MALI_600
@@ -113,6 +113,7 @@
 	buffer_handle_t currentBuffer;
 	int ion_client;
 	int system_heap_id;
+	int cma_heap_id;
 	bool gralloc_legacy_ion;
 
 	struct fb_var_screeninfo info;
@@ -166,7 +167,10 @@
 	int     size;
 	int     width;
 	int     height;
-	int     format;
+	union {
+		int     format;
+		int	req_format; /* same name as gralloc960 */
+	};
 	int     stride;
 	union
 	{
@@ -193,10 +197,10 @@
 		void *fb_paddr;
 		uint64_t fb_paddr_padding;
 	};
+	int	byte_stride;
 #if GRALLOC_ARM_DMA_BUF_MODULE
 	ion_user_handle_t ion_hnd;
 #endif
-
 #if GRALLOC_ARM_DMA_BUF_MODULE
 #define GRALLOC_ARM_NUM_FDS 1
 #else
diff --git a/gralloc960/Android.hikey960.mk b/gralloc960/Android.hikey960.mk
index 5a1a958..8b0b1be 100644
--- a/gralloc960/Android.hikey960.mk
+++ b/gralloc960/Android.hikey960.mk
@@ -51,3 +51,9 @@
 GRALLOC_DISP_H=0
 # Vsync backend(not used)
 GRALLOC_VSYNC_BACKEND=default
+
+ifeq ($(HIKEY_USE_DRM_HWCOMPOSER), true)
+ GRALLOC_USE_ION_DMA_HEAP=1
+ GRALLOC_DISABLE_FRAMEBUFFER_HAL=1
+endif
+
diff --git a/gralloc960/gralloc_priv.h b/gralloc960/gralloc_priv.h
index 82538bc..3a54c18 100644
--- a/gralloc960/gralloc_priv.h
+++ b/gralloc960/gralloc_priv.h
@@ -23,7 +23,6 @@
 #include <pthread.h>
 #include <errno.h>
 #include <linux/fb.h>
-#include <linux/ion.h>
 #include <sys/types.h>
 #include <unistd.h>
 #include <sys/mman.h>
diff --git a/gralloc960/mali_gralloc_buffer.h b/gralloc960/mali_gralloc_buffer.h
index 959653f..7e1c836 100644
--- a/gralloc960/mali_gralloc_buffer.h
+++ b/gralloc960/mali_gralloc_buffer.h
@@ -21,7 +21,6 @@
 #include <errno.h>
 #include <sys/types.h>
 #include <unistd.h>
-#include <linux/ion.h>
 #include <sys/mman.h>
 
 #include "mali_gralloc_private_interface_types.h"
@@ -117,7 +116,10 @@
 		void *base;
 		uint64_t padding;
 	};
-	uint64_t consumer_usage;
+	union {
+		uint64_t consumer_usage;
+		uint64_t usage;
+	};
 	uint64_t producer_usage;
 	uint64_t backing_store_id;
 	int backing_store_size;
diff --git a/gralloc960/mali_gralloc_ion.cpp b/gralloc960/mali_gralloc_ion.cpp
index 63d9e18..d051054 100644
--- a/gralloc960/mali_gralloc_ion.cpp
+++ b/gralloc960/mali_gralloc_ion.cpp
@@ -25,7 +25,6 @@
 #include <log/log.h>
 #include <cutils/atomic.h>
 
-#include <linux/ion.h>
 #include <ion/ion.h>
 #include <sys/ioctl.h>
 
@@ -275,7 +274,7 @@
 	}
 
 #elif defined(ION_HEAP_TYPE_DMA_MASK) && GRALLOC_USE_ION_DMA_HEAP
-	else if (!(usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) && (usage & (GRALLOC_USAGE_HW_FB | GRALLOC_USAGE_HW_COMPOSER)))
+	else if (!(usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) && (usage & (GRALLOC_USAGE_HW_FB)))
 	{
 		heap_mask = ION_HEAP_TYPE_DMA_MASK;
 	}