[RFC,v1,30/54] libcamera: camera: acquireBuffer(): Add
diff mbox series

Message ID 20260629163017.863145-31-barnabas.pocze@ideasonboard.com
State New
Headers show
Series
  • libcamera: Split requests and buffers
Related show

Commit Message

Barnabás Pőcze June 29, 2026, 4:29 p.m. UTC
Add an internal function for pipeeline handlers that can be used to
retrieve a buffer from the pool of a specific stream.

Signed-off-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>
---
 include/libcamera/internal/camera.h | 11 +++++++++
 src/libcamera/camera.cpp            | 36 +++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+)

Patch
diff mbox series

diff --git a/include/libcamera/internal/camera.h b/include/libcamera/internal/camera.h
index 6ba77eb882..31914075f0 100644
--- a/include/libcamera/internal/camera.h
+++ b/include/libcamera/internal/camera.h
@@ -49,6 +49,17 @@  public:
 
 	const CameraControlValidator *validator() const { return validator_.get(); }
 
+#ifndef __DOXYGEN__
+	struct FrameBufferPoolDeleter {
+		std::vector<FrameBuffer *> *pool = nullptr;
+		void operator()(FrameBuffer *buffer) const;
+	};
+#endif
+
+	using PooledFrameBuffer = std::unique_ptr<FrameBuffer, FrameBufferPoolDeleter>;
+
+	[[nodiscard]] PooledFrameBuffer acquireBuffer(const Stream *stream);
+
 private:
 	enum State {
 		CameraAvailable,
diff --git a/src/libcamera/camera.cpp b/src/libcamera/camera.cpp
index 27dae3a403..016f36e796 100644
--- a/src/libcamera/camera.cpp
+++ b/src/libcamera/camera.cpp
@@ -754,6 +754,42 @@  Camera::Private::PendingFence::PendingFence(const Stream *s, FrameBuffer *b)
 	  buffer(b)
 {
 }
+
+/**
+ * \typedef Camera::Private::PooledFrameBuffer
+ * \brief An std::unique_ptr for automatically returning the FrameBuffer to the pool to simplify error handling.
+ */
+
+#ifndef __DOXYGEN__
+void Camera::Private::FrameBufferPoolDeleter::operator()(FrameBuffer *buffer) const
+{
+	pool->push_back(buffer);
+}
+#endif
+
+/**
+ * \brief Acquire a buffer for a stream
+ * \param[in] stream The stream
+ *
+ * \return A buffer or an empty handle if not available
+ */
+Camera::Private::PooledFrameBuffer
+Camera::Private::acquireBuffer(const Stream *stream)
+{
+	auto it = streamData_.find(stream);
+	if (it == streamData_.end() || !it->second.active || it->second.buffers.empty())
+		return {};
+
+	FrameBuffer *buffer = it->second.buffers.back();
+	buffer->_d()->stream_ = stream;
+
+	LOG(Camera, Debug)
+		<< "Camera:" << LIBCAMERA_O_PTR() << " acquired buffer:"
+		<< buffer << " for stream:" << stream;
+
+	it->second.buffers.pop_back();
+	return { buffer, { &it->second.buffers } };
+}
 #endif /* __DOXYGEN_PUBLIC__ */
 
 /**