+#define ALIGN(val, align) (((val) + (align) - 1) & ~((align) - 1))
+
+struct droid_yuv_format {
+ /* Lookup keys */
+ int native; /* HAL_PIXEL_FORMAT_ */
+ int is_ycrcb; /* 0 if chroma order is {Cb, Cr}, 1 if {Cr, Cb} */
+ int chroma_step; /* Distance in bytes between subsequent chroma pixels. */
+
+ /* Result */
+ int fourcc; /* __DRI_IMAGE_FOURCC_ */
+};
+
+/* The following table is used to look up a DRI image FourCC based
+ * on native format and information contained in android_ycbcr struct. */
+static const struct droid_yuv_format droid_yuv_formats[] = {
+ /* Native format, YCrCb, Chroma step, DRI image FourCC */
+ { HAL_PIXEL_FORMAT_YCbCr_420_888, 0, 2, __DRI_IMAGE_FOURCC_NV12 },
+ { HAL_PIXEL_FORMAT_YCbCr_420_888, 0, 1, __DRI_IMAGE_FOURCC_YUV420 },
+ { HAL_PIXEL_FORMAT_YCbCr_420_888, 1, 1, __DRI_IMAGE_FOURCC_YVU420 },
+ { HAL_PIXEL_FORMAT_YV12, 1, 1, __DRI_IMAGE_FOURCC_YVU420 },
+};
+
+static int
+get_fourcc_yuv(int native, int is_ycrcb, int chroma_step)
+{
+ int i;
+
+ for (i = 0; i < ARRAY_SIZE(droid_yuv_formats); ++i)
+ if (droid_yuv_formats[i].native == native &&
+ droid_yuv_formats[i].is_ycrcb == is_ycrcb &&
+ droid_yuv_formats[i].chroma_step == chroma_step)
+ return droid_yuv_formats[i].fourcc;
+
+ return -1;
+}
+
+static bool
+is_yuv(int native)
+{
+ int i;
+
+ for (i = 0; i < ARRAY_SIZE(droid_yuv_formats); ++i)
+ if (droid_yuv_formats[i].native == native)
+ return true;
+
+ return false;
+}
+