Abstract V4L2 controls into wrapper class.
This abstraction will both clean up the code in V4L2Camera.cpp, and make it
easier to implement tests in the future (by mocking over the class).
This CL only adds the class and implementation, it does not yet replace
the logic in V4L2Camera.cpp.
Also gates building the module, to prevent checkbuilds without the
proper v4l2 kernel headers from trying to build it.
BUG: 30140438
Change-Id: I4e5feee30fdf896b1f71f0df492ca313f5581e78
diff --git a/modules/camera/3_4/StreamFormat.cpp b/modules/camera/3_4/StreamFormat.cpp
new file mode 100644
index 0000000..ec1c1cd
--- /dev/null
+++ b/modules/camera/3_4/StreamFormat.cpp
@@ -0,0 +1,120 @@
+/*
+ * Copyright 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "StreamFormat.h"
+
+#include <linux/videodev2.h>
+
+#include "Common.h"
+#include "Stream.h"
+
+namespace v4l2_camera_hal {
+
+StreamFormat::StreamFormat(const default_camera_hal::Stream& stream)
+ // TODO(b/30000211): multiplanar support.
+ : type_(V4L2_BUF_TYPE_VIDEO_CAPTURE),
+ v4l2_pixel_format_(
+ StreamFormat::HalToV4L2PixelFormat(stream.getFormat())),
+ width_(stream.getWidth()),
+ height_(stream.getHeight()),
+ bytes_per_line_(0),
+ min_buffer_size_(0) {
+ HAL_LOG_ENTER();
+}
+
+StreamFormat::StreamFormat(const v4l2_format& format)
+ : type_(format.type),
+ // TODO(b/30000211): multiplanar support.
+ v4l2_pixel_format_(format.fmt.pix.pixelformat),
+ width_(format.fmt.pix.width),
+ height_(format.fmt.pix.height),
+ bytes_per_line_(format.fmt.pix.bytesperline),
+ min_buffer_size_(format.fmt.pix.sizeimage) {
+ HAL_LOG_ENTER();
+}
+
+void StreamFormat::FillFormatRequest(v4l2_format* format) const {
+ HAL_LOG_ENTER();
+
+ memset(format, 0, sizeof(*format));
+ format->type = type_;
+ format->fmt.pix.pixelformat = v4l2_pixel_format_;
+ format->fmt.pix.width = width_;
+ format->fmt.pix.height = height_;
+ // Bytes per line and min buffer size are outputs set by the driver,
+ // not part of the request.
+}
+
+FormatCategory StreamFormat::Category() const {
+ switch (v4l2_pixel_format_) {
+ case V4L2_PIX_FMT_JPEG:
+ return kFormatCategoryStalling;
+ case V4L2_PIX_FMT_YUV420:
+ return kFormatCategoryNonStalling;
+ default:
+ // Note: currently no supported RAW formats.
+ return kFormatCategoryUnknown;
+ }
+}
+
+bool StreamFormat::operator==(const StreamFormat& other) const {
+ // Used to check that a requested format was actually set, so
+ // don't compare bytes per line or min buffer size.
+ return (type_ == other.type_ &&
+ v4l2_pixel_format_ == other.v4l2_pixel_format_ &&
+ width_ == other.width_ && height_ == other.height_);
+}
+
+bool StreamFormat::operator!=(const StreamFormat& other) const {
+ return !(*this == other);
+}
+
+int StreamFormat::V4L2ToHalPixelFormat(uint32_t v4l2_pixel_format) {
+ // Translate V4L2 format to HAL format.
+ int hal_pixel_format = -1;
+ switch (v4l2_pixel_format) {
+ case V4L2_PIX_FMT_JPEG:
+ hal_pixel_format = HAL_PIXEL_FORMAT_BLOB;
+ break;
+ case V4L2_PIX_FMT_YUV420:
+ hal_pixel_format = HAL_PIXEL_FORMAT_YCbCr_420_888;
+ break;
+ default:
+ // Unrecognized format.
+ break;
+ }
+ return hal_pixel_format;
+}
+
+uint32_t StreamFormat::HalToV4L2PixelFormat(int hal_pixel_format) {
+ // Translate HAL format to V4L2 format.
+ uint32_t v4l2_pixel_format = 0;
+ switch (hal_pixel_format) {
+ case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED: // fall-through.
+ case HAL_PIXEL_FORMAT_YCbCr_420_888:
+ v4l2_pixel_format = V4L2_PIX_FMT_YUV420;
+ break;
+ case HAL_PIXEL_FORMAT_BLOB:
+ v4l2_pixel_format = V4L2_PIX_FMT_JPEG;
+ break;
+ default:
+ // Unrecognized format.
+ break;
+ }
+ return v4l2_pixel_format;
+}
+
+} // namespace v4l2_camera_hal