IMG HWC: DDK1.5 upgrade for Merrifield

Based on following patches:
https://android.intel.com/#/c/394225/
https://android.intel.com/#/c/408229/
https://android.intel.com/#/c/394226/

Change-Id: Iead2940d0f033b3a0c91a42a58dc9898ae5cb6ec
Orig-Tracked-On: https://jira01.devtools.intel.com/browse/IMINAN-43707
Tracked-On: https://jira.ndg.intel.com/browse/MARVIN-1017
Signed-off-by: Jia, Lin A <lin.a.jia@intel.com>
Signed-off-by: Tong, Bo <box.tong@intel.com>
Signed-off-by: Victor Tasayco Loarte <victorx.tasayco.loarte@intel.com>
diff --git a/merrifield/include/pvr/hal/hal_public.h b/merrifield/include/pvr/hal/hal_public.h
index 9cd6db2..5575719 100644
--- a/merrifield/include/pvr/hal/hal_public.h
+++ b/merrifield/include/pvr/hal/hal_public.h
@@ -21,237 +21,38 @@
  * THE SOFTWARE.
  */
 
-#ifndef HAL_PUBLIC_H
-#define HAL_PUBLIC_H
-
-/* Authors of third party hardware composer (HWC) modules will need to include
- * this header to access functionality in the gralloc HAL.
- */
+#ifndef __HAL_PUBLIC_H
+#define __HAL_PUBLIC_H
 
 #define PVR_ANDROID_NATIVE_WINDOW_HAS_SYNC
 
-#include <hardware/gralloc.h>
-#include <hardware/hwcomposer.h>
+#include "img_gralloc_public.h"
 
-#define ALIGN(x,a)		(((x) + (a) - 1L) & ~((a) - 1L))
-#define HW_ALIGN		32
-#define CAMERA_ALIGN    64
+#undef HAL_PIXEL_FORMAT_NV12
 
-/** YV12 specific (to handle different alignment) ****************************/
-
-/* We must align YV12 to a multiple of 32bytes as NEON optimizations
- * in stagefright require the YV12 planes to be 128bit aligned.
- * while display controller requires 64 bytes alignement
- */
-#define YV12_ALIGN 128
-
-#define HAL_PIXEL_FORMAT_BGRX_8888 0x101 // Keep consistent with android_utils.h 
-enum {
-	HAL_PIXEL_FORMAT_NV12   = 0x3231564E, // YCrCb 4:2:0 SP
-	HAL_PIXEL_FORMAT_NV21   = 0x3132564E, // YCrCb 4:2:0 SP
-	HAL_PIXEL_FORMAT_I420   = 0x30323449,
-	HAL_PIXEL_FORMAT_YUY2   = 0x32595559,
-	HAL_PIXEL_FORMAT_UYVY   = 0x59565955,
-
-	// Intel video decode formats
-	HAL_PIXEL_FORMAT_NV12_VED = 0x7FA00E00, //OMX_INTEL_COLOR_FormatYUV420PackedSemiPlanar
-	HAL_PIXEL_FORMAT_NV12_VEDT = 0x7FA00F00, //OMX_INTEL_COLOR_FormatYUV420PackedSemiPlanar_Tiled
-
-	HAL_PIXEL_FORMAT_YCbCr_422_P        = 0x12, // IYUV
-	HAL_PIXEL_FORMAT_YCbCr_420_P        = 0x13, // YUV9
-	HAL_PIXEL_FORMAT_YCbCr_420_I        = 0x15,
-
-	HAL_PIXEL_FORMAT_INTEL_UYVY  		= 0x107,
-	HAL_PIXEL_FORMAT_YCbCr_420_SP       = 0x108,
-	HAL_PIXEL_FORMAT_ZSL                = 0x109,
-};
-
-/* This can be tuned down as appropriate for the SOC.
- *
- * IMG formats are usually a single sub-alloc.
- * Some OEM video formats are two sub-allocs (Y, UV planes).
- * Future OEM video formats might be three sub-allocs (Y, U, V planes).
- */
-#define MAX_SUB_ALLOCS 3
-
-
-/* This defines the maximum server sync objects used per allocation. */
-
-/* Note: It's unfortunate that we have to change the handle size dependent
- * on a build option, but we have no choice because 'fd' fields must all
- * be utilized so they are valid to be dup'ed, and we don't need some of
- * the extra fds in a native_fence_sync build.
- */
-#if defined(PVR_ANDROID_NATIVE_WINDOW_HAS_SYNC)
-#define MAX_SRV_SYNC_OBJS    2
-#else
-#define MAX_SRV_SYNC_OBJS    4
-#endif
-
-typedef struct
+typedef struct _IMG_gralloc_module_
 {
-	native_handle_t base;
-
-	/* These fields can be sent cross process. They are also valid
-	 * to duplicate within the same process.
-	 *
-	 * A table is stored within psPrivateData on gralloc_module_t (this
-	 * is obviously per-process) which maps stamps to a mapped
-	 * PVRSRV_MEMDESC in that process. Each map entry has a lock
-	 * count associated with it, satisfying the requirements of the
-	 * Android API. This also prevents us from leaking maps/allocations.
-	 *
-	 * This table has entries inserted either by alloc()
-	 * (alloc_device_t) or map() (gralloc_module_t). Entries are removed
-	 * by free() (alloc_device_t) and unmap() (gralloc_module_t).
-	 */
-
-#define IMG_NATIVE_HANDLE_NUMFDS (MAX_SRV_SYNC_OBJS + MAX_SUB_ALLOCS)
-	/* The `syncfd' field is used to export PVRSRV_CLIENT_SYNC_PRIM to
-	 * another process. Its producer/consumer rules should match the
-	 * PVRSRV_MEMDESC handles, except that there is only one sync
-	 * per N memdesc objects.
-	 *
-	 * This should be listed before `fd' because it is not variable
-	 * width. The problem with variable width is that in the case we
-	 * export framebuffer allocations, we may want to patch some of
-	 * the fds to (unused) ints, so we can't leave gaps.
-	 */
-	int aiSyncFD[MAX_SRV_SYNC_OBJS];
-
-	/* The `fd' field is used to "export" a meminfo to another process.
-	 * Therefore, it is allocated by alloc_device_t, and consumed by
-	 * gralloc_module_t.
-	 */
-	int fd[MAX_SUB_ALLOCS];
-
-#define IMG_NATIVE_HANDLE_NUMINTS ((sizeof(unsigned long long) / sizeof(int)) + 5)
-	/* A KERNEL unique identifier for any exported kernel meminfo. Each
-	 * exported kernel meminfo will have a unique stamp, but note that in
-	 * userspace, several meminfos across multiple processes could have
-	 * the same stamp. As the native_handle can be dup(2)'d, there could be
-	 * multiple handles with the same stamp but different file descriptors.
-	 */
-	unsigned long long ui64Stamp;
-
-	/* This is used for buffer usage validation when locking a buffer,
-	 * and also in WSEGL (for the composition bypass feature).
-	 */
-	int usage;
-
-	/* In order to do efficient cache flushes we need the buffer dimensions
-	 * and format. These are available on the ANativeWindowBuffer,
-	 * but the platform doesn't pass them down to the graphics HAL.
-	 *
-	 * These fields are also used in the composition bypass. In this
-	 * capacity, these are the "real" values for the backing allocation.
-	 */
-	int iWidth;
-	int iHeight;
-	int iFormat;
-	unsigned int uiBpp;
+	IMG_gralloc_module_public_t base;
+	void *(*GetDisplayDevice)(struct _IMG_gralloc_module_ *psGrallocModule);
+	int (*GetDisplayStatus)(struct _IMG_gralloc_module_ *psGrallocModule,
+							buffer_handle_t handle, uint32_t *pui32Status);
+	int (*GetBufferCPUAddresses)(gralloc_module_t const* module,
+								 buffer_handle_t buffer,
+								 void **ppvCpuVirtAddr, size_t *puSize);
+	int (*PutBufferCPUAddresses)(gralloc_module_t const* module,
+								 buffer_handle_t buffer);
 }
-__attribute__((aligned(sizeof(int)),packed)) IMG_native_handle_t;
+IMG_gralloc_module_t;
 
-typedef struct
-{
-	int l, t, w, h;
-}
-IMG_write_lock_rect_t;
+#define HAL_PIXEL_FORMAT_UYVY         0x107
+#define HAL_PIXEL_FORMAT_INTEL_ZSL    0x109
+#define HAL_PIXEL_FORMAT_NV12         0x3231564E
+#define HAL_PIXEL_FORMAT_NV21         0x3132564E
+#define HAL_PIXEL_FORMAT_I420         0x30323449
+#define HAL_PIXEL_FORMAT_YUY2         0x32595559
+#define HAL_PIXEL_FORMAT_NV12_VED     0x7FA00E00
+#define HAL_PIXEL_FORMAT_NV12_VEDT    0x7FA00F00
 
-/* Keep this in sync with SGX */
-typedef int (*IMG_buffer_format_compute_params_pfn)(
-	unsigned int uiPlane, int *piWidth, int *piHeight, int *piStride,
-	int *piVStride, unsigned long *pulPlaneOffset);
+#define GRALLOC_MODULE_GET_DISPLAY_STATUS_IMG 1001
 
-#define IMG_BFF_YUV					(1 << 0)
-#define IMG_BFF_UVCbCrORDERING		(1 << 1)
-#define IMG_BFF_CPU_CLEAR			(1 << 2)
-#define IMG_BFF_DONT_GPU_CLEAR		(1 << 3)
-#define IMG_BFF_PARTIAL_ALLOC		(1 << 4)
-#define IMG_BFF_NEVER_COMPRESS		(1 << 5)
-
-/* Keep this in sync with SGX */
-typedef struct IMG_buffer_format_public_t
-{
-	/* Buffer formats are returned as a linked list */
-	struct IMG_buffer_format_public_t *psNext;
-
-	/* HAL_PIXEL_FORMAT_... enumerant */
-	int iHalPixelFormat;
-
-	/* IMG_PIXFMT_... enumerant */
-	int iIMGPixelFormat;
-
-	/* Friendly name for format */
-	const char *const szName;
-
-	/* Bits (not bytes) per pixel */
-	unsigned int uiBpp;
-
-	/* Supported HW usage bits. If this is GRALLOC_USAGE_HW_MASK, all usages
-	 * are supported. Used for HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED.
-	 */
-	int iSupportedUsage;
-
-	/* Allocation description flags */
-	unsigned int uiFlags;
-
-	/* Utility function for adjusting YUV per-plane parameters */
-	IMG_buffer_format_compute_params_pfn pfnComputeParams;
-}
-IMG_buffer_format_public_t;
-
-typedef struct
-{
-	/* The original hwc layer */
-	hwc_layer_1_t *psLayer;
-
-	/* Custom data for the display engine */
-	unsigned long custom;
-}
-IMG_hwc_layer_t;
-
-typedef struct IMG_display_device_public_t {
-	int (*post)(struct IMG_display_device_public_t *dev, IMG_hwc_layer_t *layers,
-				int num_layers, int *releaseFenceFd);
-} IMG_display_device_public_t;
-
-typedef struct IMG_gralloc_module_public_t
-{
-	gralloc_module_t base;
-	IMG_display_device_public_t *psDisplayDevice;
-
-	/* Gets the head of the linked list of all registered formats */
-	const IMG_buffer_format_public_t *(*GetBufferFormats)(void);
-
-	/* Functionality before this point should be in sync with SGX.
-	 * After this point will be different.
-	 */
-
-	/* Custom-blit components in lieu of overlay hardware */
-	int (*Blit)(struct IMG_gralloc_module_public_t const *module,
-				 buffer_handle_t src, buffer_handle_t dest,
-				 int w, int h, int x, int y,
-				 int filter,
-				 int transform,
-				 int async);
-
-	int (*Blit3)(struct IMG_gralloc_module_public_t const *module,
-				 unsigned long long ui64SrcStamp, int iSrcWidth,
-				 int iSrcHeight, int iSrcFormat, int eSrcRotation,
-				 buffer_handle_t dest, int eDestRotation);
-
-	/* Walk the above list and return only the specified format */
-	const IMG_buffer_format_public_t *(*GetBufferFormat)(int iFormat);
-/* intel hwc extension */
-	int (*getCpuAddress)(struct IMG_gralloc_module_public_t const *module,
-				buffer_handle_t handle,
-				void **virt, uint32_t *size);
-	int (*putCpuAddress)(struct IMG_gralloc_module_public_t const *module,
-			buffer_handle_t handle);
-	IMG_display_device_public_t *(*getDisplayDevice)(struct IMG_gralloc_module_public_t *module);
-}
-IMG_gralloc_module_public_t;
-
-#endif /* HAL_PUBLIC_H */
+#endif /* __HAL_PUBLIC_H */
diff --git a/merrifield/include/pvr/hal/img_gralloc_public.h b/merrifield/include/pvr/hal/img_gralloc_public.h
new file mode 100644
index 0000000..1b6ca50
--- /dev/null
+++ b/merrifield/include/pvr/hal/img_gralloc_public.h
@@ -0,0 +1,302 @@
+/* Copyright (c) Imagination Technologies Ltd.
+ *
+ * The contents of this file are subject to the MIT license as set out below.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef HAL_PUBLIC_H
+#define HAL_PUBLIC_H
+
+/* Authors of third party hardware composer (HWC) modules will need to include
+ * this header to access functionality in the gralloc HAL.
+ */
+
+#include <hardware/gralloc.h>
+
+#define ALIGN(x,a)	(((x) + (a) - 1L) & ~((a) - 1L))
+#define HW_ALIGN	32
+
+/* Use bits [0-3] of "vendor format" bits as real format. Customers should
+ * use *only* the unassigned bits below for custom pixel formats, YUV or RGB.
+ *
+ * If there are no bits set in this part of the field, or other bits are set
+ * in the format outside of the "vendor format" mask, the non-extension format
+ * is used instead. Reserve 0 for this purpose.
+ */
+
+#define HAL_PIXEL_FORMAT_VENDOR_EXT(fmt) (0x100 | (fmt & 0xF))
+
+/*      Reserved ** DO NOT USE **    HAL_PIXEL_FORMAT_VENDOR_EXT(0) */
+#define HAL_PIXEL_FORMAT_BGRX_8888   HAL_PIXEL_FORMAT_VENDOR_EXT(1)
+#define HAL_PIXEL_FORMAT_sBGR_A_8888 HAL_PIXEL_FORMAT_VENDOR_EXT(2)
+#define HAL_PIXEL_FORMAT_sBGR_X_8888 HAL_PIXEL_FORMAT_VENDOR_EXT(3)
+/*      HAL_PIXEL_FORMAT_RGB_565     HAL_PIXEL_FORMAT_VENDOR_EXT(4) */
+/*      HAL_PIXEL_FORMAT_BGRA_8888   HAL_PIXEL_FORMAT_VENDOR_EXT(5) */
+#define HAL_PIXEL_FORMAT_NV12        HAL_PIXEL_FORMAT_VENDOR_EXT(6)
+/*      Free for customer use        HAL_PIXEL_FORMAT_VENDOR_EXT(7) */
+/*      Free for customer use        HAL_PIXEL_FORMAT_VENDOR_EXT(8) */
+/*      Free for customer use        HAL_PIXEL_FORMAT_VENDOR_EXT(9) */
+/*      Free for customer use        HAL_PIXEL_FORMAT_VENDOR_EXT(10) */
+/*      Free for customer use        HAL_PIXEL_FORMAT_VENDOR_EXT(11) */
+/*      Free for customer use        HAL_PIXEL_FORMAT_VENDOR_EXT(12) */
+/*      Free for customer use        HAL_PIXEL_FORMAT_VENDOR_EXT(13) */
+/*      Free for customer use        HAL_PIXEL_FORMAT_VENDOR_EXT(14) */
+/*      Free for customer use        HAL_PIXEL_FORMAT_VENDOR_EXT(15) */
+
+/* One of the below compression modes is OR'ed into bits [4-6] of the 8 bit
+ * "vendor format" field. If no bits are set in this "compression mask", the
+ * normal memory format for the pixel format is used. Otherwise the pixel
+ * data will be compressed in memory with the Rogue framebuffer compressor.
+ */
+
+#define HAL_FB_COMPRESSION_NONE                0
+#define HAL_FB_COMPRESSION_DIRECT_8x8          1
+#define HAL_FB_COMPRESSION_DIRECT_16x4         2
+#define HAL_FB_COMPRESSION_DIRECT_32x2         3
+#define HAL_FB_COMPRESSION_INDIRECT_8x8        4
+#define HAL_FB_COMPRESSION_INDIRECT_16x4       5
+#define HAL_FB_COMPRESSION_INDIRECT_4TILE_8x8  6
+#define HAL_FB_COMPRESSION_INDIRECT_4TILE_16x4 7
+
+/* The memory layout is OR'ed into bit 7 (top bit) of the 8 bit "vendor
+ * format" field. Only STRIDED and TWIDDLED are supported; there is no space
+ * for PAGETILED.
+ */
+#define HAL_FB_MEMLAYOUT_STRIDED               0
+#define HAL_FB_MEMLAYOUT_TWIDDLED              1
+
+/* This can be tuned down as appropriate for the SOC.
+ *
+ * IMG formats are usually a single sub-alloc.
+ * Some OEM video formats are two sub-allocs (Y, UV planes).
+ * Future OEM video formats might be three sub-allocs (Y, U, V planes).
+ */
+#define MAX_SUB_ALLOCS (3)
+
+typedef struct
+{
+	native_handle_t base;
+
+	/* These fields can be sent cross process. They are also valid
+	 * to duplicate within the same process.
+	 *
+	 * A table is stored within psPrivateData on gralloc_module_t (this
+	 * is obviously per-process) which maps stamps to a mapped
+	 * PVRSRV_MEMDESC in that process. Each map entry has a lock
+	 * count associated with it, satisfying the requirements of the
+	 * Android API. This also prevents us from leaking maps/allocations.
+	 *
+	 * This table has entries inserted either by alloc()
+	 * (alloc_device_t) or map() (gralloc_module_t). Entries are removed
+	 * by free() (alloc_device_t) and unmap() (gralloc_module_t).
+	 */
+
+#define IMG_NATIVE_HANDLE_NUMFDS (MAX_SUB_ALLOCS)
+	/* The `fd' field is used to "export" a meminfo to another process.
+	 * Therefore, it is allocated by alloc_device_t, and consumed by
+	 * gralloc_module_t.
+	 */
+	int fd[IMG_NATIVE_HANDLE_NUMFDS];
+
+	/* This define should represent the number of packed 'int's required to
+	 * represent the fields following it. If you add a data type that is
+	 * 64-bit, for example using 'unsigned long long', you should write that
+	 * as "sizeof(unsigned long long) / sizeof(int)". Please keep the order
+	 * of the additions the same as the defined field order.
+	 */
+#define IMG_NATIVE_HANDLE_NUMINTS \
+	(sizeof(unsigned long long) / sizeof(int) + \
+	 6 + MAX_SUB_ALLOCS + MAX_SUB_ALLOCS + \
+	 sizeof(unsigned long long) / sizeof(int) * MAX_SUB_ALLOCS + \
+	 1)
+	/* A KERNEL unique identifier for any exported kernel meminfo. Each
+	 * exported kernel meminfo will have a unique stamp, but note that in
+	 * userspace, several meminfos across multiple processes could have
+	 * the same stamp. As the native_handle can be dup(2)'d, there could be
+	 * multiple handles with the same stamp but different file descriptors.
+	 */
+	unsigned long long ui64Stamp;
+
+	/* This is used for buffer usage validation */
+	int usage;
+
+	/* In order to do efficient cache flushes we need the buffer dimensions,
+	 * format and bits per pixel. There are ANativeWindow queries for the
+	 * width, height and format, but the graphics HAL might have remapped the
+	 * request to different values at allocation time. These are the 'true'
+	 * values of the buffer allocation.
+	 */
+	int iWidth;
+	int iHeight;
+	int iFormat;
+	unsigned int uiBpp;
+
+	/* Planes are not the same as the `fd' suballocs. A multi-planar YUV
+	 * allocation has different planes (interleaved = 1, semi-planar = 2,
+	 * fully-planar = 3) but might be spread across 1, 2 or 3 independent
+	 * memory allocations (or not).
+	 */
+	int iPlanes;
+
+	/* For multi-planar allocations, there will be multiple hstrides */
+	int aiStride[MAX_SUB_ALLOCS];
+
+	/* For multi-planar allocations, there will be multiple vstrides */
+	int aiVStride[MAX_SUB_ALLOCS];
+
+	/* These byte offsets are reconciled with the number of sub-allocs used
+	 * for a multi-planar allocation. If there is a 1:1 mapping between the
+	 * number of planes and the number of sub-allocs, these will all be zero.
+	 *
+	 * Otherwise, normally the zeroth entry will be zero, and the latter
+	 * entries will be non-zero.
+	 */
+	unsigned long long aulPlaneOffset[MAX_SUB_ALLOCS];
+
+	/* This records the number of MAX_SUB_ALLOCS fds actually used by the
+	 * buffer allocation. File descriptors up to fd[iNumSubAllocs - 1] are
+	 * guaranteed to be valid. (This does not have any bearing on the aiStride,
+	 * aiVStride or aulPlaneOffset fields, as `iPlanes' of those arrays should
+	 * be initialized, not `iNumSubAllocs'.)
+	 */
+	int iNumSubAllocs;
+}
+__attribute__((aligned(sizeof(int)),packed)) IMG_native_handle_t;
+
+typedef struct
+{
+	int l, t, w, h;
+}
+IMG_write_lock_rect_t;
+
+#define IMG_BFF_YUV					(1 << 0)
+#define IMG_BFF_UVCbCrORDERING		(1 << 1)
+#define IMG_BFF_CPU_CLEAR			(1 << 2)
+#define IMG_BFF_DONT_GPU_CLEAR		(1 << 3)
+#define IMG_BFF_PARTIAL_ALLOC		(1 << 4)
+#define IMG_BFF_NEVER_COMPRESS		(1 << 5)
+
+/* Keep this in sync with SGX */
+typedef struct IMG_buffer_format_public_t
+{
+	/* Buffer formats are returned as a linked list */
+	struct IMG_buffer_format_public_t *psNext;
+
+	/* HAL_PIXEL_FORMAT_... enumerant */
+	int iHalPixelFormat;
+
+	/* IMG_PIXFMT_... enumerant */
+	int iIMGPixelFormat;
+
+	/* Friendly name for format */
+	const char *const szName;
+
+	/* Bits (not bytes) per pixel */
+	unsigned int uiBpp;
+
+	/* Supported HW usage bits. If this is GRALLOC_USAGE_HW_MASK, all usages
+	 * are supported. Used for HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED.
+	 */
+	int iSupportedUsage;
+
+	/* Allocation description flags */
+	unsigned int uiFlags;
+}
+IMG_buffer_format_public_t;
+
+/* NOTE: This interface is deprecated. Use module->perform() instead. */
+typedef struct IMG_gralloc_module_public_t
+{
+	gralloc_module_t base;
+
+	/* Gets the head of the linked list of all registered formats */
+	const IMG_buffer_format_public_t *(*GetBufferFormats)(void);
+
+	/* Custom-blit components in lieu of overlay hardware */
+	int (*Blit)(struct IMG_gralloc_module_public_t const *module,
+				 buffer_handle_t src, buffer_handle_t dest,
+				 int w, int h, int x, int y, int transform,
+				 int iInputFenceFd, int *piOutputFenceFd);
+
+	int (*Blit3)(struct IMG_gralloc_module_public_t const *module,
+				 unsigned long long ui64SrcStamp, int iSrcWidth,
+				 int iSrcHeight, int iSrcFormat, int iSrcStrideInPixels,
+				 int eSrcRotation, buffer_handle_t dest, int eDestRotation,
+				 int iInputFenceFd, int *piOutputFenceFd);
+
+	/* Walk the above list and return only the specified format */
+	const IMG_buffer_format_public_t *(*GetBufferFormat)(int iFormat);
+}
+IMG_gralloc_module_public_t;
+
+/* Helpers for using the non-type-safe perform() extension functions. Use
+ * these helpers instead of calling perform() directly in your application.
+ */
+
+#define GRALLOC_MODULE_GET_BUFFER_FORMAT_IMG     1
+#define GRALLOC_MODULE_GET_BUFFER_FORMATS_IMG    2
+#define GRALLOC_MODULE_BLIT_HANDLE_TO_HANDLE_IMG 3
+#define GRALLOC_MODULE_BLIT_STAMP_TO_HANDLE_IMG  4
+
+static inline int
+gralloc_module_get_buffer_format_img(const gralloc_module_t *module,
+									 int format,
+									 const IMG_buffer_format_public_t **v)
+{
+	return module->perform(module, GRALLOC_MODULE_GET_BUFFER_FORMAT_IMG,
+						   format, v);
+}
+
+static inline int
+gralloc_module_get_buffer_formats_img(const gralloc_module_t *module,
+									  const IMG_buffer_format_public_t **v)
+{
+	return module->perform(module, GRALLOC_MODULE_GET_BUFFER_FORMATS_IMG, v);
+}
+
+static inline int
+gralloc_module_blit_handle_to_handle_img(const gralloc_module_t *module,
+										 buffer_handle_t src,
+										 buffer_handle_t dest,
+										 int w, int h, int x, int y,
+										 int transform, int input_fence,
+										 int *output_fence)
+{
+	return module->perform(module, GRALLOC_MODULE_BLIT_HANDLE_TO_HANDLE_IMG,
+						   src, dest, w, h, x, y, transform, input_fence,
+						   output_fence);
+}
+
+static inline int
+gralloc_module_blit_stamp_to_handle(const gralloc_module_t *module,
+									unsigned long long src_stamp,
+									int src_width, int src_height,
+									int src_format, int src_stride_in_pixels,
+									int src_rotation, buffer_handle_t dest,
+									int dest_rotation, int input_fence,
+									int *output_fence)
+{
+	return module->perform(module, GRALLOC_MODULE_BLIT_STAMP_TO_HANDLE_IMG,
+						   src_stamp, src_width, src_height, src_format,
+						   src_stride_in_pixels, src_rotation, dest,
+						   dest_rotation, input_fence, output_fence);
+}
+
+#endif /* HAL_PUBLIC_H */
diff --git a/merrifield/ips/tangier/TngDisplayContext.cpp b/merrifield/ips/tangier/TngDisplayContext.cpp
index 90218d4..fc759dc 100644
--- a/merrifield/ips/tangier/TngDisplayContext.cpp
+++ b/merrifield/ips/tangier/TngDisplayContext.cpp
@@ -51,7 +51,7 @@
     }
 
     // init IMG display device
-    mIMGDisplayDevice = (((IMG_gralloc_module_public_t *)module)->getDisplayDevice((IMG_gralloc_module_public_t *)module));
+    mIMGDisplayDevice = (IMG_display_device_public_t *)(((IMG_gralloc_module_t *)module)->GetDisplayDevice((IMG_gralloc_module_t *)module));
     if (!mIMGDisplayDevice) {
         ETRACE("failed to get display device");
         return false;
diff --git a/merrifield/ips/tangier/TngDisplayContext.h b/merrifield/ips/tangier/TngDisplayContext.h
index fa526d7..6a2b1cb 100644
--- a/merrifield/ips/tangier/TngDisplayContext.h
+++ b/merrifield/ips/tangier/TngDisplayContext.h
@@ -19,6 +19,20 @@
 #include <IDisplayContext.h>
 #include <hal_public.h>
 
+typedef struct
+{
+    hwc_layer_1_t *psLayer;
+    uint32_t custom;
+}
+IMG_hwc_layer_t;
+
+typedef struct IMG_display_device_public
+{
+    int (*post)(struct IMG_display_device_public *dev,
+                IMG_hwc_layer_t *layers, int num_layers, int *releaseFenceFd);
+}
+IMG_display_device_public_t;
+
 namespace android {
 namespace intel {
 
diff --git a/merrifield/ips/tangier/TngGrallocBufferMapper.cpp b/merrifield/ips/tangier/TngGrallocBufferMapper.cpp
index fcf40e7..ed43f25 100644
--- a/merrifield/ips/tangier/TngGrallocBufferMapper.cpp
+++ b/merrifield/ips/tangier/TngGrallocBufferMapper.cpp
@@ -25,7 +25,7 @@
 TngGrallocBufferMapper::TngGrallocBufferMapper(IMG_gralloc_module_public_t& module,
                                                     DataBuffer& buffer)
     : GrallocBufferMapperBase(buffer),
-      mIMGGrallocModule(module),
+      mIMGGrallocModule(reinterpret_cast<IMG_gralloc_module_t&>(module)),
       mBufferObject(0)
 {
     CTRACE();
@@ -122,10 +122,11 @@
 
     CTRACE();
     // get virtual address
-    err = mIMGGrallocModule.getCpuAddress(&mIMGGrallocModule,
-                                          (buffer_handle_t)mClonedHandle,
-                                          vaddr,
-                                          size);
+    err = mIMGGrallocModule.GetBufferCPUAddresses(
+                                  (gralloc_module_t const*)&mIMGGrallocModule,
+                                  (buffer_handle_t)mClonedHandle,
+                                  vaddr,
+                                  size);
     if (err) {
         ETRACE("failed to map. err = %d", err);
         return false;
@@ -161,8 +162,9 @@
         }
     }
 
-    err = mIMGGrallocModule.putCpuAddress(&mIMGGrallocModule,
-                                    (buffer_handle_t)mClonedHandle);
+    err = mIMGGrallocModule.PutBufferCPUAddresses(
+                                  (gralloc_module_t const*)&mIMGGrallocModule,
+                                  (buffer_handle_t)mClonedHandle);
     return false;
 }
 
@@ -182,8 +184,9 @@
         mSize[i] = 0;
     }
 
-    err = mIMGGrallocModule.putCpuAddress(&mIMGGrallocModule,
-                                    (buffer_handle_t)mClonedHandle);
+    err = mIMGGrallocModule.PutBufferCPUAddresses(
+                                  (gralloc_module_t const*)&mIMGGrallocModule,
+                                  (buffer_handle_t)mClonedHandle);
     if (err) {
         ETRACE("failed to unmap. err = %d", err);
     }
@@ -236,10 +239,11 @@
     }
 
     // get virtual address
-    err = mIMGGrallocModule.getCpuAddress(&mIMGGrallocModule,
-                                          (buffer_handle_t)mClonedHandle,
-                                          vaddr,
-                                          size);
+    err = mIMGGrallocModule.GetBufferCPUAddresses(
+                                  (gralloc_module_t const*)&mIMGGrallocModule,
+                                  (buffer_handle_t)mClonedHandle,
+                                  vaddr,
+                                  size);
     if (err) {
         ETRACE("failed to map. err = %d", err);
         return 0;
@@ -250,8 +254,9 @@
 
 void TngGrallocBufferMapper::putFbHandle()
 {
-    int err = mIMGGrallocModule.putCpuAddress(&mIMGGrallocModule,
-                                    (buffer_handle_t)mClonedHandle);
+    int err = mIMGGrallocModule.PutBufferCPUAddresses(
+                                  (gralloc_module_t const*)&mIMGGrallocModule,
+                                  (buffer_handle_t)mClonedHandle);
     if (err) {
         ETRACE("failed to unmap. err = %d", err);
     }
diff --git a/merrifield/ips/tangier/TngGrallocBufferMapper.h b/merrifield/ips/tangier/TngGrallocBufferMapper.h
index d72005e..8360e02 100644
--- a/merrifield/ips/tangier/TngGrallocBufferMapper.h
+++ b/merrifield/ips/tangier/TngGrallocBufferMapper.h
@@ -41,7 +41,7 @@
     bool mapKhandle();
 
 private:
-    IMG_gralloc_module_public_t& mIMGGrallocModule;
+    IMG_gralloc_module_t& mIMGGrallocModule;
     void* mBufferObject;
 	native_handle_t* mClonedHandle;
 };
diff --git a/merrifield/platforms/merrifield/Android.mk b/merrifield/platforms/merrifield/Android.mk
index 6db9243..f671d82 100644
--- a/merrifield/platforms/merrifield/Android.mk
+++ b/merrifield/platforms/merrifield/Android.mk
@@ -126,7 +126,9 @@
    LOCAL_CFLAGS += -DTARGET_HAS_MULTIPLE_DISPLAY
 endif
 
-LOCAL_COPY_HEADERS := ../../include/pvr/hal/hal_public.h
+LOCAL_COPY_HEADERS := \
+    ../../include/pvr/hal/hal_public.h \
+    ../../include/pvr/hal/img_gralloc_public.h
 LOCAL_COPY_HEADERS_TO := pvr/hal
 
 ifneq ($(TARGET_BUILD_VARIANT),user)
diff --git a/merrifield/platforms/merrifield/PlatfBufferManager.cpp b/merrifield/platforms/merrifield/PlatfBufferManager.cpp
index 1dd72c6..13df1a5 100644
--- a/merrifield/platforms/merrifield/PlatfBufferManager.cpp
+++ b/merrifield/platforms/merrifield/PlatfBufferManager.cpp
@@ -17,6 +17,7 @@
 #include <PlatfBufferManager.h>
 #include <tangier/TngGrallocBuffer.h>
 #include <tangier/TngGrallocBufferMapper.h>
+#include <sync/sync.h>
 
 namespace android {
 namespace intel {
@@ -63,13 +64,21 @@
 
 {
     IMG_gralloc_module_public_t *imgGrallocModule = (IMG_gralloc_module_public_t *) mGrallocModule;
+    int fenceFd;
+
     if (imgGrallocModule->Blit(imgGrallocModule, srcHandle,
                                 destHandle,
                                 destRect.w, destRect.h, destRect.x,
-                                destRect.y, filter, 0, async)) {
+                                destRect.y, 0, -1, &fenceFd)) {
         ETRACE("Blit failed");
         return false;
     }
+
+    if (!async) {
+        sync_wait(fenceFd, -1);
+    }
+    close(fenceFd);
+
     return true;
 }
 
diff --git a/merrifield/platforms/merrifield_plus/Android.mk b/merrifield/platforms/merrifield_plus/Android.mk
index 0a85355..3f8b886 100644
--- a/merrifield/platforms/merrifield_plus/Android.mk
+++ b/merrifield/platforms/merrifield_plus/Android.mk
@@ -129,7 +129,9 @@
    LOCAL_CFLAGS += -DTARGET_HAS_MULTIPLE_DISPLAY
 endif
 
-LOCAL_COPY_HEADERS := ../../include/pvr/hal/hal_public.h
+LOCAL_COPY_HEADERS := \
+    ../../include/pvr/hal/hal_public.h \
+    ../../include/pvr/hal/img_gralloc_public.h
 LOCAL_COPY_HEADERS_TO := pvr/hal
 
 ifneq ($(TARGET_BUILD_VARIANT),user)
diff --git a/merrifield/platforms/merrifield_plus/PlatfBufferManager.cpp b/merrifield/platforms/merrifield_plus/PlatfBufferManager.cpp
index 1dd72c6..13df1a5 100644
--- a/merrifield/platforms/merrifield_plus/PlatfBufferManager.cpp
+++ b/merrifield/platforms/merrifield_plus/PlatfBufferManager.cpp
@@ -17,6 +17,7 @@
 #include <PlatfBufferManager.h>
 #include <tangier/TngGrallocBuffer.h>
 #include <tangier/TngGrallocBufferMapper.h>
+#include <sync/sync.h>
 
 namespace android {
 namespace intel {
@@ -63,13 +64,21 @@
 
 {
     IMG_gralloc_module_public_t *imgGrallocModule = (IMG_gralloc_module_public_t *) mGrallocModule;
+    int fenceFd;
+
     if (imgGrallocModule->Blit(imgGrallocModule, srcHandle,
                                 destHandle,
                                 destRect.w, destRect.h, destRect.x,
-                                destRect.y, filter, 0, async)) {
+                                destRect.y, 0, -1, &fenceFd)) {
         ETRACE("Blit failed");
         return false;
     }
+
+    if (!async) {
+        sync_wait(fenceFd, -1);
+    }
+    close(fenceFd);
+
     return true;
 }