[libcamera-devel] [PATCH v5 3/7] libcamera: v4l2_subdevice: Implement getSelection()

Jacopo Mondi jacopo at jmondi.org
Tue Apr 28 11:19:30 CEST 2020


Implement V4L2Subdevice::getSelection() to support retrieving selection
rectangles from the V4L2 subdevice.

Reviewed-by: Niklas Söderlund <niklas.soderlund at ragnatech.se>
Reviewed-by: Laurent Pinchart <laurent.pinchart at ideasonboard.com>
Signed-off-by: Jacopo Mondi <jacopo at jmondi.org>
---
 src/libcamera/include/v4l2_subdevice.h |  2 ++
 src/libcamera/v4l2_subdevice.cpp       | 33 ++++++++++++++++++++++++++
 2 files changed, 35 insertions(+)

diff --git a/src/libcamera/include/v4l2_subdevice.h b/src/libcamera/include/v4l2_subdevice.h
index 9a5c812db9b6..27ba5b17f61e 100644
--- a/src/libcamera/include/v4l2_subdevice.h
+++ b/src/libcamera/include/v4l2_subdevice.h
@@ -46,6 +46,8 @@ public:
 
 	const MediaEntity *entity() const { return entity_; }
 
+	int getSelection(unsigned int pad, unsigned int target,
+			 Rectangle *rect);
 	int setSelection(unsigned int pad, unsigned int target,
 			 Rectangle *rect);
 
diff --git a/src/libcamera/v4l2_subdevice.cpp b/src/libcamera/v4l2_subdevice.cpp
index 432e89eacbd3..74788ce7cf4f 100644
--- a/src/libcamera/v4l2_subdevice.cpp
+++ b/src/libcamera/v4l2_subdevice.cpp
@@ -133,6 +133,39 @@ int V4L2Subdevice::open()
  * \return The subdevice's associated media entity.
  */
 
+/**
+ * \brief Get selection rectangle \a rect for \a target
+ * \param[in] pad The 0-indexed pad number the rectangle is retrieved from
+ * \param[in] target The selection target defined by the V4L2_SEL_TGT_* flags
+ * \param[out] rect The retrieved selection rectangle
+ * \return 0 on success or a negative error code otherwise
+ */
+int V4L2Subdevice::getSelection(unsigned int pad, unsigned int target,
+				Rectangle *rect)
+{
+	struct v4l2_subdev_selection sel = {};
+
+	sel.which = V4L2_SUBDEV_FORMAT_ACTIVE;
+	sel.pad = pad;
+	sel.target = target;
+	sel.flags = 0;
+
+	int ret = ioctl(VIDIOC_SUBDEV_G_SELECTION, &sel);
+	if (ret < 0) {
+		LOG(V4L2, Error)
+			<< "Unable to get rectangle " << target << " on pad "
+			<< pad << ": " << strerror(-ret);
+		return ret;
+	}
+
+	rect->x = sel.r.left;
+	rect->y = sel.r.top;
+	rect->width = sel.r.width;
+	rect->height = sel.r.height;
+
+	return 0;
+}
+
 /**
  * \brief Set selection rectangle \a rect for \a target
  * \param[in] pad The 0-indexed pad number the rectangle is to be applied to
-- 
2.26.1



More information about the libcamera-devel mailing list