[libcamera-devel,v3,04/11] libcamera: framebuffer: Add Fence to FrameBuffer
diff mbox series

Message ID 20211130233634.34173-5-jacopo@jmondi.org
State Superseded
Headers show
Series
  • libcamera: Add support for Fence
Related show

Commit Message

Jacopo Mondi Nov. 30, 2021, 11:36 p.m. UTC
Add to the FrameBuffer::Private class a unique pointer to a
synchronization Fence.

The Fence will be used to signal the availability of the Framebuffer for
incoming data transfer.

The Fence will be associated to a FrameBuffer at Request::addBuffer()
time, and if correctly signalled, reset by the core at Request queue
time.

If a FrameBuffer completes with errors, due to a Fence wait failure, the
Fence will still be owned by the FrameBuffer and it is application
responsibility to correctly reset it before reusing the buffer.

Signed-off-by: Jacopo Mondi <jacopo@jmondi.org>
---
 include/libcamera/framebuffer.h          |  4 ++
 include/libcamera/internal/framebuffer.h |  7 +++
 src/libcamera/framebuffer.cpp            | 66 ++++++++++++++++++++++++
 3 files changed, 77 insertions(+)

Comments

Laurent Pinchart Dec. 1, 2021, 1:44 a.m. UTC | #1
Hi Jacopo,

Thank you for the patch.

On Wed, Dec 01, 2021 at 12:36:27AM +0100, Jacopo Mondi wrote:
> Add to the FrameBuffer::Private class a unique pointer to a
> synchronization Fence.
> 
> The Fence will be used to signal the availability of the Framebuffer for
> incoming data transfer.
> 
> The Fence will be associated to a FrameBuffer at Request::addBuffer()
> time, and if correctly signalled, reset by the core at Request queue
> time.
> 
> If a FrameBuffer completes with errors, due to a Fence wait failure, the
> Fence will still be owned by the FrameBuffer and it is application
> responsibility to correctly reset it before reusing the buffer.
> 
> Signed-off-by: Jacopo Mondi <jacopo@jmondi.org>
> ---
>  include/libcamera/framebuffer.h          |  4 ++
>  include/libcamera/internal/framebuffer.h |  7 +++
>  src/libcamera/framebuffer.cpp            | 66 ++++++++++++++++++++++++
>  3 files changed, 77 insertions(+)
> 
> diff --git a/include/libcamera/framebuffer.h b/include/libcamera/framebuffer.h
> index 357bbe189551..2f2ecbda6cb4 100644
> --- a/include/libcamera/framebuffer.h
> +++ b/include/libcamera/framebuffer.h
> @@ -18,6 +18,7 @@
>  
>  namespace libcamera {
>  
> +class Fence;
>  class Request;
>  
>  struct FrameMetadata {
> @@ -65,6 +66,9 @@ public:
>  	unsigned int cookie() const { return cookie_; }
>  	void setCookie(unsigned int cookie) { cookie_ = cookie; }
>  
> +	Fence *fence() const;

Do we need this function in the public API, or could it be moved to the
Private class ? It seems to be used internally only.

> +	std::unique_ptr<Fence> resetFence();

Following the UniqueFD and std::unique_ptr<> naming convention, I think
this should be releaseFence() (sorry for proposing an incorrect name in
the previous version).

> +
>  	void cancel() { metadata_.status = FrameMetadata::FrameCancelled; }
>  
>  private:
> diff --git a/include/libcamera/internal/framebuffer.h b/include/libcamera/internal/framebuffer.h
> index 908b478985e7..e2609300d209 100644
> --- a/include/libcamera/internal/framebuffer.h
> +++ b/include/libcamera/internal/framebuffer.h
> @@ -7,12 +7,16 @@
>  
>  #pragma once
>  
> +#include <memory>
> +
>  #include <libcamera/base/class.h>
>  
>  #include <libcamera/framebuffer.h>
>  
>  namespace libcamera {
>  
> +class Fence;
> +
>  class FrameBuffer::Private : public Extensible::Private
>  {
>  	LIBCAMERA_DECLARE_PUBLIC(FrameBuffer)
> @@ -23,7 +27,10 @@ public:
>  	void setRequest(Request *request) { request_ = request; }
>  	bool isContiguous() const { return isContiguous_; }
>  
> +	void setFence(std::unique_ptr<Fence> fence);
> +
>  private:
> +	std::unique_ptr<Fence> fence_;
>  	Request *request_;
>  	bool isContiguous_;
>  };
> diff --git a/src/libcamera/framebuffer.cpp b/src/libcamera/framebuffer.cpp
> index 701212f3ae21..2e6f07fb0e56 100644
> --- a/src/libcamera/framebuffer.cpp
> +++ b/src/libcamera/framebuffer.cpp
> @@ -13,6 +13,8 @@
>  #include <libcamera/base/file.h>
>  #include <libcamera/base/log.h>
>  
> +#include <libcamera/fence.h>
> +
>  /**
>   * \file libcamera/framebuffer.h
>   * \brief Frame buffer handling
> @@ -129,6 +131,30 @@ FrameBuffer::Private::Private()
>   * handlers, it is called by the pipeline handlers themselves.
>   */
>  
> +/**
> + * \brief Move a \a fence in this buffer
> + * \param[in] fence The synchronization fence
> + *
> + * This function associates a Fence with this Framebuffer. The intended caller
> + * is the Request::addBuffer() function.
> + *
> + * Once a FrameBuffer is associated with a Fence, the FrameBuffer will only be
> + * made available to the hardware device once the synchronization Fence has been
> + * correctly signalled.
> + *
> + * \sa Request::prepare()
> + *
> + * If the FrameBuffer completes successfully the core resets the Fence and the
> + * Buffer can be reused immediately. If handling of the Fence fails during the
> + * request preparation, the Fence is not reset and is left in the FrameBuffer.
> + * It is applications responsibility to correctly reset the fence and handle it

s/applications/the application's/

> + * opportunely before using the buffer again.
> + */
> +void FrameBuffer::Private::setFence(std::unique_ptr<Fence> fence)
> +{
> +	fence_ = std::move(fence);

This could also be an inline function in the Private class, up to you.

> +}
> +
>  /**
>   * \fn FrameBuffer::Private::isContiguous()
>   * \brief Check if the frame buffer stores planes contiguously in memory
> @@ -329,6 +355,46 @@ Request *FrameBuffer::request() const
>   * libcamera core never modifies the buffer cookie.
>   */
>  
> +/**
> + * \brief Retrieve a const pointer to the Fence
> + *
> + * This function does only return a reference to the the fence and does not
> + * change its ownership. The fence is stored in the FrameBuffer and can only be
> + * reset with FrameBuffer::resetFence() in case the buffer has completed with
> + * error due to a Fence wait failure.
> + *
> + * If buffer with a Fence completes with errors due to a failure in handling
> + * the fence, applications are responsible for resetting the Fence before
> + * calling Request::addBuffer() again.
> + *
> + * \sa Request::addBuffer()
> + *
> + * \return A const pointer to the Fence if any, nullptr otherwise
> + */
> +Fence *FrameBuffer::fence() const
> +{
> +	return _d()->fence_.get();
> +}
> +
> +/**
> + * \brief Extract the Fence associated with this Framebuffer
> + *
> + * This function moves the buffer's fence ownership to the caller.
> + * After the fence has been reset, calling this function always return
> + * nullptr.
> + *
> + * If buffer with a Fence completes with errors due to a failure in handling
> + * the fence, applications are responsible for resetting the Fence before
> + * calling Request::addBuffer() again.
> + *
> + * \return A unique pointer to the Fence if set, or nullptr if the fence has
> + * been reset already
> + */
> +std::unique_ptr<Fence> FrameBuffer::resetFence()
> +{
> +	return std::move(_d()->fence_);
> +}
> +
>  /**
>   * \fn FrameBuffer::cancel()
>   * \brief Marks the buffer as cancelled

Patch
diff mbox series

diff --git a/include/libcamera/framebuffer.h b/include/libcamera/framebuffer.h
index 357bbe189551..2f2ecbda6cb4 100644
--- a/include/libcamera/framebuffer.h
+++ b/include/libcamera/framebuffer.h
@@ -18,6 +18,7 @@ 
 
 namespace libcamera {
 
+class Fence;
 class Request;
 
 struct FrameMetadata {
@@ -65,6 +66,9 @@  public:
 	unsigned int cookie() const { return cookie_; }
 	void setCookie(unsigned int cookie) { cookie_ = cookie; }
 
+	Fence *fence() const;
+	std::unique_ptr<Fence> resetFence();
+
 	void cancel() { metadata_.status = FrameMetadata::FrameCancelled; }
 
 private:
diff --git a/include/libcamera/internal/framebuffer.h b/include/libcamera/internal/framebuffer.h
index 908b478985e7..e2609300d209 100644
--- a/include/libcamera/internal/framebuffer.h
+++ b/include/libcamera/internal/framebuffer.h
@@ -7,12 +7,16 @@ 
 
 #pragma once
 
+#include <memory>
+
 #include <libcamera/base/class.h>
 
 #include <libcamera/framebuffer.h>
 
 namespace libcamera {
 
+class Fence;
+
 class FrameBuffer::Private : public Extensible::Private
 {
 	LIBCAMERA_DECLARE_PUBLIC(FrameBuffer)
@@ -23,7 +27,10 @@  public:
 	void setRequest(Request *request) { request_ = request; }
 	bool isContiguous() const { return isContiguous_; }
 
+	void setFence(std::unique_ptr<Fence> fence);
+
 private:
+	std::unique_ptr<Fence> fence_;
 	Request *request_;
 	bool isContiguous_;
 };
diff --git a/src/libcamera/framebuffer.cpp b/src/libcamera/framebuffer.cpp
index 701212f3ae21..2e6f07fb0e56 100644
--- a/src/libcamera/framebuffer.cpp
+++ b/src/libcamera/framebuffer.cpp
@@ -13,6 +13,8 @@ 
 #include <libcamera/base/file.h>
 #include <libcamera/base/log.h>
 
+#include <libcamera/fence.h>
+
 /**
  * \file libcamera/framebuffer.h
  * \brief Frame buffer handling
@@ -129,6 +131,30 @@  FrameBuffer::Private::Private()
  * handlers, it is called by the pipeline handlers themselves.
  */
 
+/**
+ * \brief Move a \a fence in this buffer
+ * \param[in] fence The synchronization fence
+ *
+ * This function associates a Fence with this Framebuffer. The intended caller
+ * is the Request::addBuffer() function.
+ *
+ * Once a FrameBuffer is associated with a Fence, the FrameBuffer will only be
+ * made available to the hardware device once the synchronization Fence has been
+ * correctly signalled.
+ *
+ * \sa Request::prepare()
+ *
+ * If the FrameBuffer completes successfully the core resets the Fence and the
+ * Buffer can be reused immediately. If handling of the Fence fails during the
+ * request preparation, the Fence is not reset and is left in the FrameBuffer.
+ * It is applications responsibility to correctly reset the fence and handle it
+ * opportunely before using the buffer again.
+ */
+void FrameBuffer::Private::setFence(std::unique_ptr<Fence> fence)
+{
+	fence_ = std::move(fence);
+}
+
 /**
  * \fn FrameBuffer::Private::isContiguous()
  * \brief Check if the frame buffer stores planes contiguously in memory
@@ -329,6 +355,46 @@  Request *FrameBuffer::request() const
  * libcamera core never modifies the buffer cookie.
  */
 
+/**
+ * \brief Retrieve a const pointer to the Fence
+ *
+ * This function does only return a reference to the the fence and does not
+ * change its ownership. The fence is stored in the FrameBuffer and can only be
+ * reset with FrameBuffer::resetFence() in case the buffer has completed with
+ * error due to a Fence wait failure.
+ *
+ * If buffer with a Fence completes with errors due to a failure in handling
+ * the fence, applications are responsible for resetting the Fence before
+ * calling Request::addBuffer() again.
+ *
+ * \sa Request::addBuffer()
+ *
+ * \return A const pointer to the Fence if any, nullptr otherwise
+ */
+Fence *FrameBuffer::fence() const
+{
+	return _d()->fence_.get();
+}
+
+/**
+ * \brief Extract the Fence associated with this Framebuffer
+ *
+ * This function moves the buffer's fence ownership to the caller.
+ * After the fence has been reset, calling this function always return
+ * nullptr.
+ *
+ * If buffer with a Fence completes with errors due to a failure in handling
+ * the fence, applications are responsible for resetting the Fence before
+ * calling Request::addBuffer() again.
+ *
+ * \return A unique pointer to the Fence if set, or nullptr if the fence has
+ * been reset already
+ */
+std::unique_ptr<Fence> FrameBuffer::resetFence()
+{
+	return std::move(_d()->fence_);
+}
+
 /**
  * \fn FrameBuffer::cancel()
  * \brief Marks the buffer as cancelled