[v3,05/29] libcamera: converter_v4l2_m2m: Add suport for V4L2 requests
diff mbox series

Message ID 20251125162851.2301793-6-stefan.klug@ideasonboard.com
State Accepted
Headers show
Series
  • Full dewarper support on imx8mp
Related show

Commit Message

Stefan Klug Nov. 25, 2025, 4:28 p.m. UTC
To actually use requests with the m2m device, requests need to be
allocated on the underlying media device. This can only be done if the
media device is opened which means acquiring it. Add a function to check
if the m2m device supports requests by acquiring the media device,
asking it and then releasing it again. Also add a function to allocate
requests that acquires the internal media device and releases it after
allocating the requests.

Signed-off-by: Stefan Klug <stefan.klug@ideasonboard.com>

---

ToDo:
- Fix the place where the MediaObject is acquired/released

Changes in v2:
- Improved commit message
- Dropped the change from private to protected as that belongs to a
  different patch
---
 .../internal/converter/converter_v4l2_m2m.h   |  7 ++++
 .../converter/converter_v4l2_m2m.cpp          | 34 ++++++++++++++++++-
 2 files changed, 40 insertions(+), 1 deletion(-)

Comments

Kieran Bingham Nov. 25, 2025, 6:24 p.m. UTC | #1
Quoting Stefan Klug (2025-11-25 16:28:17)
> To actually use requests with the m2m device, requests need to be
> allocated on the underlying media device. This can only be done if the
> media device is opened which means acquiring it. Add a function to check
> if the m2m device supports requests by acquiring the media device,
> asking it and then releasing it again. Also add a function to allocate
> requests that acquires the internal media device and releases it after
> allocating the requests.
> 
> Signed-off-by: Stefan Klug <stefan.klug@ideasonboard.com>
> 
> ---
> 
> ToDo:
> - Fix the place where the MediaObject is acquired/released
> 
> Changes in v2:
> - Improved commit message
> - Dropped the change from private to protected as that belongs to a
>   different patch
> ---
>  .../internal/converter/converter_v4l2_m2m.h   |  7 ++++
>  .../converter/converter_v4l2_m2m.cpp          | 34 ++++++++++++++++++-
>  2 files changed, 40 insertions(+), 1 deletion(-)
> 
> diff --git a/include/libcamera/internal/converter/converter_v4l2_m2m.h b/include/libcamera/internal/converter/converter_v4l2_m2m.h
> index 1b2a88c4a608..bc85bff7a07b 100644
> --- a/include/libcamera/internal/converter/converter_v4l2_m2m.h
> +++ b/include/libcamera/internal/converter/converter_v4l2_m2m.h
> @@ -73,6 +73,11 @@ public:
>         std::pair<Rectangle, Rectangle> inputCropBounds() override { return inputCropBounds_; }
>         std::pair<Rectangle, Rectangle> inputCropBounds(const Stream *stream) override;
>  
> +       int allocateRequests(unsigned int count,
> +                            std::vector<std::unique_ptr<V4L2Request>> *requests);
> +
> +       bool supportsRequests();
> +
>  private:
>         class V4L2M2MStream : protected Loggable
>         {
> @@ -122,6 +127,8 @@ private:
>         std::map<const Stream *, std::unique_ptr<V4L2M2MStream>> streams_;
>         std::map<FrameBuffer *, unsigned int> queue_;
>         std::pair<Rectangle, Rectangle> inputCropBounds_;
> +
> +       std::shared_ptr<MediaDevice> media_;
>  };
>  
>  } /* namespace libcamera */
> diff --git a/src/libcamera/converter/converter_v4l2_m2m.cpp b/src/libcamera/converter/converter_v4l2_m2m.cpp
> index ff11a9735db7..c6153d728c9a 100644
> --- a/src/libcamera/converter/converter_v4l2_m2m.cpp
> +++ b/src/libcamera/converter/converter_v4l2_m2m.cpp
> @@ -9,6 +9,7 @@
>  #include "libcamera/internal/converter/converter_v4l2_m2m.h"
>  
>  #include <algorithm>
> +#include <errno.h>
>  #include <limits.h>
>  #include <memory>
>  #include <set>
> @@ -266,7 +267,7 @@ void V4L2M2MConverter::V4L2M2MStream::captureBufferReady(FrameBuffer *buffer)
>   * \param[in] media The media device implementing the converter
>   */
>  V4L2M2MConverter::V4L2M2MConverter(std::shared_ptr<MediaDevice> media)
> -       : Converter(media)
> +       : Converter(media), media_(media)
>  {
>         if (deviceNode().empty())
>                 return;
> @@ -742,6 +743,37 @@ int V4L2M2MConverter::queueBuffers(FrameBuffer *input,
>         return 0;
>  }
>  
> +/**
> + * \copydoc libcamera::MediaDevice::allocateRequests
> + */
> +int V4L2M2MConverter::allocateRequests(unsigned int count,
> +                                      std::vector<std::unique_ptr<V4L2Request>> *requests)
> +{
> +       /* \todo The acquire() must be moved to the right place. */
> +       media_->acquire();
> +       if (!media_->busy())
> +               LOG(Converter, Error)
> +                       << "MediaDevice must be valid.";
> +       int ret = media_->allocateRequests(count, requests);
> +       media_->release();
> +       return ret;
> +}
> +
> +/**
> + * \copydoc libcamera::MediaDevice::supportsRequests
> + */
> +bool V4L2M2MConverter::supportsRequests()
> +{
> +       /* \todo The acquire() must be moved to the right place. */
> +       media_->acquire();
> +       if (!media_->busy())
> +               LOG(Converter, Error)
> +                       << "MediaDevice must be valid.";

I expect this will report an error while trying to enumerate cameras
when one is already in use ?

What's the impact there? will this return a failure so the camera won't
show up ?

I expect it will just not support requests - and will mean the camera
will show up - but if you try to open a camera separately to use - even
if you stop the camera in process A, process B might have seen the
camera but won't be able to use say the dewarper.

I think we're into corner cases that don't need to be handled 'right
now' but it's probably something we need to resolve somehow.

But you've already got a todo so :


Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>

> +       bool ret = media_->supportsRequests();
> +       media_->release();
> +       return ret;
> +}
> +
>  /*
>   * \todo This should be extended to include Feature::Flag to denote
>   * what each converter supports feature-wise.
> -- 
> 2.51.0
>

Patch
diff mbox series

diff --git a/include/libcamera/internal/converter/converter_v4l2_m2m.h b/include/libcamera/internal/converter/converter_v4l2_m2m.h
index 1b2a88c4a608..bc85bff7a07b 100644
--- a/include/libcamera/internal/converter/converter_v4l2_m2m.h
+++ b/include/libcamera/internal/converter/converter_v4l2_m2m.h
@@ -73,6 +73,11 @@  public:
 	std::pair<Rectangle, Rectangle> inputCropBounds() override { return inputCropBounds_; }
 	std::pair<Rectangle, Rectangle> inputCropBounds(const Stream *stream) override;
 
+	int allocateRequests(unsigned int count,
+			     std::vector<std::unique_ptr<V4L2Request>> *requests);
+
+	bool supportsRequests();
+
 private:
 	class V4L2M2MStream : protected Loggable
 	{
@@ -122,6 +127,8 @@  private:
 	std::map<const Stream *, std::unique_ptr<V4L2M2MStream>> streams_;
 	std::map<FrameBuffer *, unsigned int> queue_;
 	std::pair<Rectangle, Rectangle> inputCropBounds_;
+
+	std::shared_ptr<MediaDevice> media_;
 };
 
 } /* namespace libcamera */
diff --git a/src/libcamera/converter/converter_v4l2_m2m.cpp b/src/libcamera/converter/converter_v4l2_m2m.cpp
index ff11a9735db7..c6153d728c9a 100644
--- a/src/libcamera/converter/converter_v4l2_m2m.cpp
+++ b/src/libcamera/converter/converter_v4l2_m2m.cpp
@@ -9,6 +9,7 @@ 
 #include "libcamera/internal/converter/converter_v4l2_m2m.h"
 
 #include <algorithm>
+#include <errno.h>
 #include <limits.h>
 #include <memory>
 #include <set>
@@ -266,7 +267,7 @@  void V4L2M2MConverter::V4L2M2MStream::captureBufferReady(FrameBuffer *buffer)
  * \param[in] media The media device implementing the converter
  */
 V4L2M2MConverter::V4L2M2MConverter(std::shared_ptr<MediaDevice> media)
-	: Converter(media)
+	: Converter(media), media_(media)
 {
 	if (deviceNode().empty())
 		return;
@@ -742,6 +743,37 @@  int V4L2M2MConverter::queueBuffers(FrameBuffer *input,
 	return 0;
 }
 
+/**
+ * \copydoc libcamera::MediaDevice::allocateRequests
+ */
+int V4L2M2MConverter::allocateRequests(unsigned int count,
+				       std::vector<std::unique_ptr<V4L2Request>> *requests)
+{
+	/* \todo The acquire() must be moved to the right place. */
+	media_->acquire();
+	if (!media_->busy())
+		LOG(Converter, Error)
+			<< "MediaDevice must be valid.";
+	int ret = media_->allocateRequests(count, requests);
+	media_->release();
+	return ret;
+}
+
+/**
+ * \copydoc libcamera::MediaDevice::supportsRequests
+ */
+bool V4L2M2MConverter::supportsRequests()
+{
+	/* \todo The acquire() must be moved to the right place. */
+	media_->acquire();
+	if (!media_->busy())
+		LOG(Converter, Error)
+			<< "MediaDevice must be valid.";
+	bool ret = media_->supportsRequests();
+	media_->release();
+	return ret;
+}
+
 /*
  * \todo This should be extended to include Feature::Flag to denote
  * what each converter supports feature-wise.