[libcamera-devel,3/6] libcamera: v4l2_device: Add setTransform method to set a device's flip controls
diff mbox series

Message ID 20221110144556.7858-4-david.plowman@raspberrypi.com
State Superseded
Headers show
Series
  • Resolve invalid attempts to set sensor flip controls
Related show

Commit Message

David Plowman Nov. 10, 2022, 2:45 p.m. UTC
The setTransform method provides a convenient way of setting the V4L2
device's horizontal and vertical flip controls (where these are
available) according to a libcamera Transform. Additionally, the
caller can ask not to set the controls but merely find out whether
the transform is supported by the device.

Signed-off-by: David Plowman <david.plowman@raspberrypi.com>
---
 include/libcamera/internal/v4l2_device.h |  3 ++
 src/libcamera/v4l2_device.cpp            | 37 ++++++++++++++++++++++++
 2 files changed, 40 insertions(+)

Patch
diff mbox series

diff --git a/include/libcamera/internal/v4l2_device.h b/include/libcamera/internal/v4l2_device.h
index 75304be1..09f627de 100644
--- a/include/libcamera/internal/v4l2_device.h
+++ b/include/libcamera/internal/v4l2_device.h
@@ -21,6 +21,7 @@ 
 
 #include <libcamera/color_space.h>
 #include <libcamera/controls.h>
+#include <libcamera/transform.h>
 
 #include "libcamera/internal/formats.h"
 
@@ -49,6 +50,8 @@  public:
 
 	void updateControlInfo();
 
+	Transform setTransform(Transform transform, bool test = false);
+
 protected:
 	V4L2Device(const std::string &deviceNode);
 	~V4L2Device();
diff --git a/src/libcamera/v4l2_device.cpp b/src/libcamera/v4l2_device.cpp
index c17b323f..34b17079 100644
--- a/src/libcamera/v4l2_device.cpp
+++ b/src/libcamera/v4l2_device.cpp
@@ -674,6 +674,43 @@  void V4L2Device::updateControlInfo()
 	}
 }
 
+/*
+ * \brief Set or test setting the device's flip controls according to \a transform
+ * \param[in] transform Transform to request
+ * \param[in] test If true, merely test what would be set, otherwise actually set it
+ *
+ * This function checks whether a given \a transform is supported by the device's
+ * V4L2_CID_HFLIP and V4L2_CID_VFLIP controls and optionally sets them.
+ *
+ * If \a test is true, it merely checks what the device can accept and returns what
+ * would be programmed into the device. If \a test is false, then the device is
+ * actually programmed with updated flip values too.
+ *
+ * \return The transform that has been or would be set
+ */
+Transform V4L2Device::setTransform(Transform transform, bool test)
+{
+	Transform actualTransform = Transform::Identity;
+	ControlList controls;
+
+	if (controlInfo(V4L2_CID_HFLIP)) {
+		Transform hflip = transform & Transform::HFlip;
+		actualTransform |= hflip;
+		controls.set(V4L2_CID_HFLIP, static_cast<int32_t>(!!hflip));
+	}
+
+	if (controlInfo(V4L2_CID_VFLIP)) {
+		Transform vflip = transform & Transform::VFlip;
+		actualTransform |= vflip;
+		controls.set(V4L2_CID_HFLIP, static_cast<int32_t>(!!vflip));
+	}
+
+	if (!test)
+		setControls(&controls);
+
+	return actualTransform;
+}
+
 /*
  * \brief Update the value of the first \a count V4L2 controls in \a ctrls using
  * values in \a v4l2Ctrls