From 424744cc4d9ef8daa624ed6a3687cb9ebf4c9b7e Mon Sep 17 00:00:00 2001 From: Jaesung Chung Date: Thu, 9 Nov 2017 12:51:32 +0900 Subject: [PATCH] Use RGB24 format instead of BGR32 BGR32 mode is in RPi3's camera has a RB color swapping issue. This CL makes to use stable RGB24 mode instead to avoid the problem without causing any quality regression. Bug: 69075512 Test: RB color swapping problem is gone on preview, recording and stil. Exempt-From-Owner-Approval: HAL is owned by Things team. Change-Id: I109363c0f68cec45f92e739a978f554e70032151 --- modules/camera/3_4/README.md | 5 +---- modules/camera/3_4/stream_format.cpp | 6 +++--- modules/camera/3_4/v4l2_gralloc.cpp | 14 ++++++++++++++ 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/modules/camera/3_4/README.md b/modules/camera/3_4/README.md index 73d0c13b..30f36d7d 100644 --- a/modules/camera/3_4/README.md +++ b/modules/camera/3_4/README.md @@ -28,7 +28,7 @@ to load the V4L2 HAL instead of a default Camera HAL. Devices and cameras wishing to use this HAL must meet the following requirements: -* The camera must support BGR32, YUV420, and JPEG formats. +* The camera must support RGB24, YUV420, and JPEG formats. * The gralloc and other graphics modules used by the device must use `HAL_PIXEL_FORMAT_RGBA_8888` as the `HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED` @@ -146,6 +146,3 @@ is underfeatured compared to the ideal/what is possible. * A variety of features are unimplemented: High speed capture, flash torch mode, hotplugging/unplugging. -* The HAL uses BGR for RGBA. Again, the HAL was designed for the Raspberry Pi -camera, which doesn't support RGB, but RGB is a common default format for -graphics stacks. diff --git a/modules/camera/3_4/stream_format.cpp b/modules/camera/3_4/stream_format.cpp index c85c26be..70900abb 100644 --- a/modules/camera/3_4/stream_format.cpp +++ b/modules/camera/3_4/stream_format.cpp @@ -57,7 +57,7 @@ FormatCategory StreamFormat::Category() const { case V4L2_PIX_FMT_JPEG: return kFormatCategoryStalling; case V4L2_PIX_FMT_YUV420: // Fall through. - case V4L2_PIX_FMT_BGR32: + case V4L2_PIX_FMT_RGB24: return kFormatCategoryNonStalling; default: // Note: currently no supported RAW formats. @@ -87,7 +87,7 @@ int StreamFormat::V4L2ToHalPixelFormat(uint32_t v4l2_pixel_format) { case V4L2_PIX_FMT_YUV420: hal_pixel_format = HAL_PIXEL_FORMAT_YCbCr_420_888; break; - case V4L2_PIX_FMT_BGR32: + case V4L2_PIX_FMT_RGB24: hal_pixel_format = HAL_PIXEL_FORMAT_RGBA_8888; break; default: @@ -106,7 +106,7 @@ uint32_t StreamFormat::HalToV4L2PixelFormat(int hal_pixel_format) { case HAL_PIXEL_FORMAT_RGBA_8888: // Should be RGB32, but RPi doesn't support that. // For now we accept that the colors will be off. - v4l2_pixel_format = V4L2_PIX_FMT_BGR32; + v4l2_pixel_format = V4L2_PIX_FMT_RGB24; break; case HAL_PIXEL_FORMAT_YCbCr_420_888: v4l2_pixel_format = V4L2_PIX_FMT_YUV420; diff --git a/modules/camera/3_4/v4l2_gralloc.cpp b/modules/camera/3_4/v4l2_gralloc.cpp index 7da3c4e2..2fcef355 100644 --- a/modules/camera/3_4/v4l2_gralloc.cpp +++ b/modules/camera/3_4/v4l2_gralloc.cpp @@ -146,6 +146,7 @@ int V4L2Gralloc::lock(const camera3_stream_buffer_t* camera_buffer, return ret; } break; + case V4L2_PIX_FMT_RGB24: // Fall-through. case V4L2_PIX_FMT_BGR32: // Fall-through. case V4L2_PIX_FMT_RGB32: // RGB formats have nice agreed upon representation. Unless using android @@ -204,6 +205,19 @@ int V4L2Gralloc::unlock(const v4l2_buffer* device_buffer) { const camera3_stream_buffer_t* camera_buffer = buffer_data->camera_buffer; const buffer_handle_t buffer = *camera_buffer->buffer; + if (StreamFormat::HalToV4L2PixelFormat(camera_buffer->stream->format) == V4L2_PIX_FMT_RGB24) { + // Convert RGB24 to RGB32. + size_t rgb_size = camera_buffer->stream->width * camera_buffer->stream->height; + uint8_t* tail_rgb24 = (uint8_t*)data + 3 * rgb_size - 1; + uint8_t* tail_rgb32 = (uint8_t*)data + 4 * rgb_size - 1; + for (int i = 0; i < rgb_size; i++) { + *(tail_rgb32--) = 0xff; + *(tail_rgb32--) = *(tail_rgb24--); + *(tail_rgb32--) = *(tail_rgb24--); + *(tail_rgb32--) = *(tail_rgb24--); + } + } + // Check for transform. if (buffer_data->transform_dest) { HAL_LOGV("Transforming V4L2 YUV to gralloc YUV.");