gralloc: implement yuv offset query as hw specific function

This patch reverts earlier cca14cfd... and introduces a new hw
specific hook to query yuv components offsets which can vary
between different hw, decoders, cameras etc.

Change-Id: Ib60bc8ee28df7bc9425b6d7934294fe36fc55354
Depends-Change-Id: I1aa5368b21e588d5d711c1005fff2a5296e143a0
Signed-off-by: Tapani Pälli <tapani.palli@intel.com>
diff --git a/gralloc_drm.c b/gralloc_drm.c
index cf0fe84..53d764a 100644
--- a/gralloc_drm.c
+++ b/gralloc_drm.c
@@ -355,6 +355,22 @@
 }
 
 /*
+ * Query YUV component offsets for a buffer handle
+ */
+void gralloc_drm_resolve_format(buffer_handle_t _handle,
+	uint32_t *pitches, uint32_t *offsets, uint32_t *handles)
+{
+	struct gralloc_drm_handle_t *handle = gralloc_drm_handle(_handle);
+	struct gralloc_drm_bo_t *bo = (struct gralloc_drm_bo_t *) handle->data;
+	struct gralloc_drm_t *drm = bo->drm;
+
+	/* if handle exists and driver implements resolve_format */
+	if (handle && drm->drv->resolve_format)
+		drm->drv->resolve_format(drm->drv, bo,
+			pitches, offsets, handles);
+}
+
+/*
  * Lock a bo.  XXX thread-safety?
  */
 int gralloc_drm_bo_lock(struct gralloc_drm_bo_t *bo,
diff --git a/gralloc_drm.h b/gralloc_drm.h
index 6c13138..b06e438 100644
--- a/gralloc_drm.h
+++ b/gralloc_drm.h
@@ -121,6 +121,7 @@
 struct gralloc_drm_bo_t *gralloc_drm_bo_from_handle(buffer_handle_t handle);
 buffer_handle_t gralloc_drm_bo_get_handle(struct gralloc_drm_bo_t *bo, int *stride);
 int gralloc_drm_get_gem_handle(buffer_handle_t handle);
+void gralloc_drm_resolve_format(buffer_handle_t _handle, uint32_t *pitches, uint32_t *offsets, uint32_t *handles);
 
 int gralloc_drm_bo_lock(struct gralloc_drm_bo_t *bo, int x, int y, int w, int h, int enable_write, void **addr);
 void gralloc_drm_bo_unlock(struct gralloc_drm_bo_t *bo);
diff --git a/gralloc_drm_handle.h b/gralloc_drm_handle.h
index 621f500..430b480 100644
--- a/gralloc_drm_handle.h
+++ b/gralloc_drm_handle.h
@@ -61,22 +61,4 @@
 	return handle;
 }
 
-static inline void gralloc_drm_yuv_offsets(buffer_handle_t _handle,
-	int *y, int *u, int *v)
-{
-	struct gralloc_drm_handle_t *handle = gralloc_drm_handle(_handle);
-	if (handle && *y && *u && *v) {
-		*y = 0;
-		switch (handle->format) {
-			case HAL_PIXEL_FORMAT_YV12:
-				*v = handle->stride * handle->height;
-				*u = *v + handle->height/2;
-				break;
-			case HAL_PIXEL_FORMAT_DRM_NV12:
-				*u = *v = handle->stride * handle->height;
-				break;
-		}
-	}
-}
-
 #endif /* _GRALLOC_DRM_HANDLE_H_ */
diff --git a/gralloc_drm_intel.c b/gralloc_drm_intel.c
index 649c6ff..725334e 100644
--- a/gralloc_drm_intel.c
+++ b/gralloc_drm_intel.c
@@ -190,6 +190,52 @@
 	return ret;
 }
 
+static void intel_resolve_format(struct gralloc_drm_drv_t *drv,
+		struct gralloc_drm_bo_t *bo,
+		uint32_t *pitches, uint32_t *offsets, uint32_t *handles)
+{
+	/*
+	 * TODO - should take account hw specific padding, alignment
+	 * for camera, video decoder etc.
+	 */
+
+	struct intel_buffer *ib = (struct intel_buffer *) bo;
+
+	memset(pitches, 0, 4 * sizeof(uint32_t));
+	memset(offsets, 0, 4 * sizeof(uint32_t));
+	memset(handles, 0, 4 * sizeof(uint32_t));
+
+	pitches[0] = ib->base.handle->stride;
+	handles[0] = ib->base.fb_handle;
+
+	switch(ib->base.handle->format) {
+		case HAL_PIXEL_FORMAT_YV12:
+
+			// U and V stride are half of Y plane
+			pitches[2] = pitches[0]/2;
+			pitches[1] = pitches[0]/2;
+
+			// like I420 but U and V are in reverse order
+			offsets[2] = offsets[0] +
+				pitches[0] * ib->base.handle->height;
+			offsets[1] = offsets[2] +
+				pitches[2] * ib->base.handle->height/2;
+
+			handles[1] = handles[2] = handles[0];
+			break;
+
+		case HAL_PIXEL_FORMAT_DRM_NV12:
+
+			// U and V are interleaved in 2nd plane
+			pitches[1] = pitches[0];
+			offsets[1] = offsets[0] +
+				pitches[0] * ib->base.handle->height;
+
+			handles[1] = handles[0];
+			break;
+	}
+}
+
 static void intel_copy(struct gralloc_drm_drv_t *drv,
 		struct gralloc_drm_bo_t *dst,
 		struct gralloc_drm_bo_t *src,
@@ -593,6 +639,7 @@
 	info->base.map = intel_map;
 	info->base.unmap = intel_unmap;
 	info->base.copy = intel_copy;
+	info->base.resolve_format = intel_resolve_format;
 
 	return &info->base;
 }
diff --git a/gralloc_drm_kms.c b/gralloc_drm_kms.c
index 3310807..a921e6c 100644
--- a/gralloc_drm_kms.c
+++ b/gralloc_drm_kms.c
@@ -73,42 +73,17 @@
 static int resolve_drm_format(struct gralloc_drm_bo_t *bo,
 	uint32_t *pitches, uint32_t *offsets, uint32_t *handles)
 {
-	memset(pitches, 0, 4 * sizeof(uint32_t));
-	memset(offsets, 0, 4 * sizeof(uint32_t));
-	memset(handles, 0, 4 * sizeof(uint32_t));
+	struct gralloc_drm_t *drm = bo->drm;
 
 	pitches[0] = bo->handle->stride;
 	handles[0] = bo->fb_handle;
 
-	int format = drm_format_from_hal(bo->handle->format);
+	/* driver takes care of HW specific padding, alignment etc. */
+	if (drm->drv->resolve_format)
+		drm->drv->resolve_format(drm->drv, bo,
+			pitches, offsets, handles);
 
-	// handle 'special formats'
-	switch(bo->handle->format) {
-		case HAL_PIXEL_FORMAT_YV12:
-
-			// U and V stride are half of Y plane
-			pitches[2] = pitches[0]/2;
-			pitches[1] = pitches[0]/2;
-
-			// like I420 but U and V are in reverse order
-			offsets[2] = offsets[0] +
-				pitches[0] * bo->handle->height;
-			offsets[1] = offsets[2] +
-				pitches[2] * bo->handle->height/2;
-
-			handles[1] = handles[2] = handles[0];
-			break;
-
-		case HAL_PIXEL_FORMAT_DRM_NV12:
-
-			// U and V are interleaved in 2nd plane
-			pitches[1] = pitches[0];
-			offsets[1] = offsets[0] +
-				pitches[0] * bo->handle->height;
-			handles[1] = handles[0];
-			break;
-	}
-	return format;
+	return drm_format_from_hal(bo->handle->format);
 }
 
 /*
@@ -116,9 +91,9 @@
  */
 int gralloc_drm_bo_add_fb(struct gralloc_drm_bo_t *bo)
 {
-	uint32_t pitches[4];
-	uint32_t offsets[4];
-	uint32_t handles[4];
+	uint32_t pitches[4] = { 0, 0, 0, 0 };
+	uint32_t offsets[4] = { 0, 0, 0, 0 };
+	uint32_t handles[4] = { 0, 0, 0, 0 };
 
 	if (bo->fb_id)
 		return 0;
diff --git a/gralloc_drm_priv.h b/gralloc_drm_priv.h
index 7a93a1e..eb2450d 100644
--- a/gralloc_drm_priv.h
+++ b/gralloc_drm_priv.h
@@ -126,6 +126,11 @@
 		     struct gralloc_drm_bo_t *dst,
 		     struct gralloc_drm_bo_t *src,
 		     short x1, short y1, short x2, short y2);
+
+	/* query component offsets, strides and handles for a format */
+	void (*resolve_format)(struct gralloc_drm_drv_t *drv,
+		     struct gralloc_drm_bo_t *bo,
+		     uint32_t *pitches, uint32_t *offsets, uint32_t *handles);
 };
 
 struct gralloc_drm_bo_t {