[libcamera-devel,v2,14/14] RFC: libcamera: ipu3: Enable ImgU media links

Message ID 20190312121242.2253-15-jacopo@jmondi.org
State Superseded
Headers show
Series
  • libcamera: ipu3: ImgU support
Related show

Commit Message

Jacopo Mondi March 12, 2019, 12:12 p.m. UTC
As the lenghty comment reports, link enable/disable is not trivial, as
links in one ImgU instance interfere with capture operations in the
other one. As I struggle to find where to disable links selectively,
as of now reset the media graph and only enable the required links at
streamConfiguration time.

Signed-off-by: Jacopo Mondi <jacopo@jmondi.org>
---
 src/libcamera/pipeline/ipu3/ipu3.cpp | 107 +++++++++++++++++++++++++++
 1 file changed, 107 insertions(+)

Comments

Jacopo Mondi March 19, 2019, 5:11 p.m. UTC | #1
Hi,
  one additional note, as I might have confused you.

On Tue, Mar 12, 2019 at 01:12:42PM +0100, Jacopo Mondi wrote:
> As the lenghty comment reports, link enable/disable is not trivial, as
> links in one ImgU instance interfere with capture operations in the
> other one. As I struggle to find where to disable links selectively,
> as of now reset the media graph and only enable the required links at
> streamConfiguration time.
>
> Signed-off-by: Jacopo Mondi <jacopo@jmondi.org>
> ---
>  src/libcamera/pipeline/ipu3/ipu3.cpp | 107 +++++++++++++++++++++++++++
>  1 file changed, 107 insertions(+)
>
> diff --git a/src/libcamera/pipeline/ipu3/ipu3.cpp b/src/libcamera/pipeline/ipu3/ipu3.cpp
> index a113fd6ee74b..1c0d63f912d5 100644
> --- a/src/libcamera/pipeline/ipu3/ipu3.cpp
> +++ b/src/libcamera/pipeline/ipu3/ipu3.cpp
> @@ -51,6 +51,10 @@ public:
>  	}
>
>  	void init(MediaDevice *media, unsigned int index);
> +	int linkSetup(const std::string &source, unsigned int sourcePad,
> +		      const std::string &sink, unsigned int sinkPad,
> +		      bool enable);
> +	int enableLinks(bool enable);
>
>  	unsigned int index_;
>  	std::string imguName_;
> @@ -270,6 +274,35 @@ int PipelineHandlerIPU3::configureStreams(Camera *camera,
>  		return -EINVAL;
>  	}
>
> +	/*
> +	 * FIXME: enabled links in one ImgU instance interfere with capture
> +	 * operations on the other one. This can be easily triggered by
> +	 * capturing from one camera, then trying to capture from the other
> +	 * one right after without disabling media links in the media graph.
> +	 *
> +	 * The tricky part here is where to disable links on the ImgU instance
> +	 * which is not in use:
> +	 * 1) Link enable/disable cannot be done at start/stop time as video
> +	 * devices needs to be linked first before format can be configured on
> +	 * them.
> +	 * 2) As link enable has to be done here, before configuring formats,
> +	 * the only place where to disable links would be 'stop()', but the
> +	 * camera state machine allows start()<->stop() sequences without any
> +	 * streamConfiguration() in between.
> +	 *
> +	 * As of now, disable all links in the media graph before enabling
> +	 * the requested ones only.
> +	 */
> +	data->imgu->mediaDevice_->disableLinks();

I have now noticed doing this here is wrong, as the media device is
close, and I get a failure in the logs which I didn't notice.

The reason my testing work it's because I have retainted the link disabling
at match() time in [5/14] which Laurent suggested to remove not to
interfere with other cameras.

tl;dr: The comment is correct and I think this might require some
thinking, but this specific line is wrong and should be removed.

Sorry about this.


> +
> +	/*
> +	 * \todo: Enable links selectively based on the requested streams.
> +	 * As of now, enable all links unconditionally.
> +	 */
> +	ret = data->imgu->enableLinks(true);
> +	if (ret)
> +		return ret;
> +
>  	/*
>  	 * Pass the requested output image size to the sensor and get back the
>  	 * adjusted one to be propagated to the CIO2 device and to the ImgU
> @@ -631,6 +664,80 @@ void ImgUDevice::init(MediaDevice *mediaDevice, unsigned int index)
>  	mediaDevice_ = mediaDevice;
>  }
>
> +/**
> + * \brief Enable or disable a single link on the ImgU instance
> + *
> + * This method assumes the media device associated with the ImgU instance
> + * is open.
> + *
> + * \return 0 on success or a negative error code otherwise
> + */
> +int ImgUDevice::linkSetup(const std::string &source, unsigned int sourcePad,
> +			  const std::string &sink, unsigned int sinkPad,
> +			  bool enable)
> +{
> +	MediaLink *link = mediaDevice_->link(source, sourcePad, sink, sinkPad);
> +	if (!link) {
> +		LOG(IPU3, Error)
> +			<< "Failed to get link: '" << source << "':"
> +			<< sourcePad << " -> '" << sink << "':" << sinkPad;
> +		return -ENODEV;
> +	}
> +
> +	return link->setEnabled(enable);
> +}
> +
> +/**
> + * \brief Enable or disable all media links in the ImgU instance to prepare
> + * for capture operations
> + *
> + * \todo This method will probably be removed or changed once links will be
> + * enabled or disabled selectively.
> + *
> + * \return 0 on success or a negative error code otherwise
> + */
> +int ImgUDevice::enableLinks(bool enable)
> +{
> +	std::string inputName = imguName_ + " input";
> +	std::string outputName = imguName_ + " output";
> +	std::string viewfinderName = imguName_ + " viewfinder";
> +	std::string statName = imguName_ + " 3a stat";
> +	int ret;
> +
> +	/* \todo Establish rules to handle media devices open/close. */
> +	ret = mediaDevice_->open();
> +	if (ret)
> +		return ret;
> +
> +	ret = linkSetup(inputName, 0, imguName_, PAD_INPUT, enable);
> +	if (ret) {
> +		mediaDevice_->close();
> +		return ret;
> +	}
> +
> +	ret = linkSetup(imguName_, PAD_OUTPUT, outputName, 0, enable);
> +	if (ret) {
> +		mediaDevice_->close();
> +		return ret;
> +	}
> +
> +	ret = linkSetup(imguName_, PAD_VF, viewfinderName, 0, enable);
> +	if (ret) {
> +		mediaDevice_->close();
> +		return ret;
> +	}
> +
> +	ret = linkSetup(imguName_, PAD_STAT, statName, 0, enable);
> +	if (ret) {
> +		mediaDevice_->close();
> +		return ret;
> +	}
> +
> +	mediaDevice_->close();
> +
> +	return 0;
> +}
> +
>  int PipelineHandlerIPU3::mediaBusToCIO2Format(unsigned int code)
>  {
>  	switch(code) {
> --
> 2.20.1
>

Patch

diff --git a/src/libcamera/pipeline/ipu3/ipu3.cpp b/src/libcamera/pipeline/ipu3/ipu3.cpp
index a113fd6ee74b..1c0d63f912d5 100644
--- a/src/libcamera/pipeline/ipu3/ipu3.cpp
+++ b/src/libcamera/pipeline/ipu3/ipu3.cpp
@@ -51,6 +51,10 @@  public:
 	}
 
 	void init(MediaDevice *media, unsigned int index);
+	int linkSetup(const std::string &source, unsigned int sourcePad,
+		      const std::string &sink, unsigned int sinkPad,
+		      bool enable);
+	int enableLinks(bool enable);
 
 	unsigned int index_;
 	std::string imguName_;
@@ -270,6 +274,35 @@  int PipelineHandlerIPU3::configureStreams(Camera *camera,
 		return -EINVAL;
 	}
 
+	/*
+	 * FIXME: enabled links in one ImgU instance interfere with capture
+	 * operations on the other one. This can be easily triggered by
+	 * capturing from one camera, then trying to capture from the other
+	 * one right after without disabling media links in the media graph.
+	 *
+	 * The tricky part here is where to disable links on the ImgU instance
+	 * which is not in use:
+	 * 1) Link enable/disable cannot be done at start/stop time as video
+	 * devices needs to be linked first before format can be configured on
+	 * them.
+	 * 2) As link enable has to be done here, before configuring formats,
+	 * the only place where to disable links would be 'stop()', but the
+	 * camera state machine allows start()<->stop() sequences without any
+	 * streamConfiguration() in between.
+	 *
+	 * As of now, disable all links in the media graph before enabling
+	 * the requested ones only.
+	 */
+	data->imgu->mediaDevice_->disableLinks();
+
+	/*
+	 * \todo: Enable links selectively based on the requested streams.
+	 * As of now, enable all links unconditionally.
+	 */
+	ret = data->imgu->enableLinks(true);
+	if (ret)
+		return ret;
+
 	/*
 	 * Pass the requested output image size to the sensor and get back the
 	 * adjusted one to be propagated to the CIO2 device and to the ImgU
@@ -631,6 +664,80 @@  void ImgUDevice::init(MediaDevice *mediaDevice, unsigned int index)
 	mediaDevice_ = mediaDevice;
 }
 
+/**
+ * \brief Enable or disable a single link on the ImgU instance
+ *
+ * This method assumes the media device associated with the ImgU instance
+ * is open.
+ *
+ * \return 0 on success or a negative error code otherwise
+ */
+int ImgUDevice::linkSetup(const std::string &source, unsigned int sourcePad,
+			  const std::string &sink, unsigned int sinkPad,
+			  bool enable)
+{
+	MediaLink *link = mediaDevice_->link(source, sourcePad, sink, sinkPad);
+	if (!link) {
+		LOG(IPU3, Error)
+			<< "Failed to get link: '" << source << "':"
+			<< sourcePad << " -> '" << sink << "':" << sinkPad;
+		return -ENODEV;
+	}
+
+	return link->setEnabled(enable);
+}
+
+/**
+ * \brief Enable or disable all media links in the ImgU instance to prepare
+ * for capture operations
+ *
+ * \todo This method will probably be removed or changed once links will be
+ * enabled or disabled selectively.
+ *
+ * \return 0 on success or a negative error code otherwise
+ */
+int ImgUDevice::enableLinks(bool enable)
+{
+	std::string inputName = imguName_ + " input";
+	std::string outputName = imguName_ + " output";
+	std::string viewfinderName = imguName_ + " viewfinder";
+	std::string statName = imguName_ + " 3a stat";
+	int ret;
+
+	/* \todo Establish rules to handle media devices open/close. */
+	ret = mediaDevice_->open();
+	if (ret)
+		return ret;
+
+	ret = linkSetup(inputName, 0, imguName_, PAD_INPUT, enable);
+	if (ret) {
+		mediaDevice_->close();
+		return ret;
+	}
+
+	ret = linkSetup(imguName_, PAD_OUTPUT, outputName, 0, enable);
+	if (ret) {
+		mediaDevice_->close();
+		return ret;
+	}
+
+	ret = linkSetup(imguName_, PAD_VF, viewfinderName, 0, enable);
+	if (ret) {
+		mediaDevice_->close();
+		return ret;
+	}
+
+	ret = linkSetup(imguName_, PAD_STAT, statName, 0, enable);
+	if (ret) {
+		mediaDevice_->close();
+		return ret;
+	}
+
+	mediaDevice_->close();
+
+	return 0;
+}
+
 int PipelineHandlerIPU3::mediaBusToCIO2Format(unsigned int code)
 {
 	switch(code) {