[libcamera-devel,v2,6/6] libcamera: v4l2_videodevice: Empty the V4L2 buffer cache on streamOff()
diff mbox series

Message ID 20220317140827.1835029-7-naush@raspberrypi.com
State Superseded
Headers show
Series
  • Raspberry Pi: Efficient start/stop/start sequences
Related show

Commit Message

Naushir Patuck March 17, 2022, 2:08 p.m. UTC
When streamOff() is called, ensure the cache entires for the remaining queued
buffers are freed since this will not happen via the dequeueBuffer() mechanism.

Additionally, add a V4L2BufferCache::isEmpty() function and assert that the
cache is empty at the end of the streamOff() call.

Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
Tested-by: David Plowman <david.plowman@raspberrypi.com>
---
 include/libcamera/internal/v4l2_videodevice.h |  1 +
 src/libcamera/v4l2_videodevice.cpp            | 16 ++++++++++++++++
 2 files changed, 17 insertions(+)

Comments

Nicolas Dufresne via libcamera-devel March 18, 2022, 2:29 p.m. UTC | #1
Quoting Naushir Patuck via libcamera-devel (2022-03-17 14:08:27)
> When streamOff() is called, ensure the cache entires for the remaining queued

s/entires/entries/

> buffers are freed since this will not happen via the dequeueBuffer() mechanism.
> 
> Additionally, add a V4L2BufferCache::isEmpty() function and assert that the
> cache is empty at the end of the streamOff() call.
> 
> Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
> Tested-by: David Plowman <david.plowman@raspberrypi.com>


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

> ---
>  include/libcamera/internal/v4l2_videodevice.h |  1 +
>  src/libcamera/v4l2_videodevice.cpp            | 16 ++++++++++++++++
>  2 files changed, 17 insertions(+)
> 
> diff --git a/include/libcamera/internal/v4l2_videodevice.h b/include/libcamera/internal/v4l2_videodevice.h
> index 2d2ccc477c91..37747c0b2f27 100644
> --- a/include/libcamera/internal/v4l2_videodevice.h
> +++ b/include/libcamera/internal/v4l2_videodevice.h
> @@ -126,6 +126,7 @@ public:
>  
>         int get(const FrameBuffer &buffer);
>         void put(unsigned int index);
> +       bool isEmpty() const;
>  
>  private:
>         class Entry
> diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp
> index 5f36ee20710d..9da82697e7f0 100644
> --- a/src/libcamera/v4l2_videodevice.cpp
> +++ b/src/libcamera/v4l2_videodevice.cpp
> @@ -262,6 +262,19 @@ void V4L2BufferCache::put(unsigned int index)
>         cache_[index].free_ = true;
>  }
>  
> +/**
> + * \brief Check if all the entries in the cache are unused
> + */
> +bool V4L2BufferCache::isEmpty() const
> +{
> +       for (auto const &entry : cache_) {
> +               if (!entry.free_)
> +                       return false;
> +       }
> +
> +       return true;
> +}
> +
>  V4L2BufferCache::Entry::Entry()
>         : free_(true), lastUsed_(0)
>  {
> @@ -1832,10 +1845,13 @@ int V4L2VideoDevice::streamOff()
>         for (auto it : queuedBuffers_) {
>                 FrameBuffer *buffer = it.second;
>  
> +               cache_->put(it.first);
>                 buffer->metadata_.status = FrameMetadata::FrameCancelled;
>                 bufferReady.emit(buffer);
>         }
>  
> +       ASSERT(cache_->isEmpty());
> +
>         queuedBuffers_.clear();
>         fdBufferNotifier_->setEnabled(false);
>         streaming_ = false;
> -- 
> 2.25.1
>

Patch
diff mbox series

diff --git a/include/libcamera/internal/v4l2_videodevice.h b/include/libcamera/internal/v4l2_videodevice.h
index 2d2ccc477c91..37747c0b2f27 100644
--- a/include/libcamera/internal/v4l2_videodevice.h
+++ b/include/libcamera/internal/v4l2_videodevice.h
@@ -126,6 +126,7 @@  public:
 
 	int get(const FrameBuffer &buffer);
 	void put(unsigned int index);
+	bool isEmpty() const;
 
 private:
 	class Entry
diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp
index 5f36ee20710d..9da82697e7f0 100644
--- a/src/libcamera/v4l2_videodevice.cpp
+++ b/src/libcamera/v4l2_videodevice.cpp
@@ -262,6 +262,19 @@  void V4L2BufferCache::put(unsigned int index)
 	cache_[index].free_ = true;
 }
 
+/**
+ * \brief Check if all the entries in the cache are unused
+ */
+bool V4L2BufferCache::isEmpty() const
+{
+	for (auto const &entry : cache_) {
+		if (!entry.free_)
+			return false;
+	}
+
+	return true;
+}
+
 V4L2BufferCache::Entry::Entry()
 	: free_(true), lastUsed_(0)
 {
@@ -1832,10 +1845,13 @@  int V4L2VideoDevice::streamOff()
 	for (auto it : queuedBuffers_) {
 		FrameBuffer *buffer = it.second;
 
+		cache_->put(it.first);
 		buffer->metadata_.status = FrameMetadata::FrameCancelled;
 		bufferReady.emit(buffer);
 	}
 
+	ASSERT(cache_->isEmpty());
+
 	queuedBuffers_.clear();
 	fdBufferNotifier_->setEnabled(false);
 	streaming_ = false;