[libcamera-devel,RFC,4/8] buffer: Provide a base MappedBuffer class

Message ID 20200720224232.153717-5-kieran.bingham@ideasonboard.com
State RFC
Headers show
Series
  • RFC MappedBuffers
Related show

Commit Message

Kieran Bingham July 20, 2020, 10:42 p.m. UTC
Move the interface of the MappedFrameBuffer to a MappedBuffer class
to provide a common interface of mapped buffers from different sources.

This allows MappedBuffer instances to be created from Camera3Buffer types.

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

Now we can create our new base class, the MappedBuffer. I can see this
going further to also push the mmap down to a MappedDmaBuf, but that can
be on top/later or if this general automatic mapping approach is desired
at all....


 include/libcamera/buffer.h | 16 +++++---
 src/libcamera/buffer.cpp   | 80 +++++++++++++++++++++++++++++++-------
 2 files changed, 76 insertions(+), 20 deletions(-)

Patch

diff --git a/include/libcamera/buffer.h b/include/libcamera/buffer.h
index 881d736da2db..558204e5a09b 100644
--- a/include/libcamera/buffer.h
+++ b/include/libcamera/buffer.h
@@ -71,13 +71,12 @@  private:
 	unsigned int cookie_;
 };
 
-class MappedFrameBuffer {
+class MappedBuffer {
 public:
-	MappedFrameBuffer(const FrameBuffer *buffer, int flags);
-	~MappedFrameBuffer();
-
+	MappedBuffer();
 	/* Move constructor only, copying is not permitted. */
-	MappedFrameBuffer(MappedFrameBuffer &&rhs);
+	MappedBuffer(MappedBuffer &&rhs);
+	~MappedBuffer();
 
 	struct MappedPlane {
 		void *address;
@@ -88,12 +87,17 @@  public:
 	int error() const { return error_; }
 	const std::vector<MappedPlane> &maps() const { return maps_; }
 
-private:
+protected:
 	int error_;
 	bool valid_;
 	std::vector<MappedPlane> maps_;
 };
 
+class MappedFrameBuffer : public MappedBuffer {
+public:
+	MappedFrameBuffer(const FrameBuffer *buffer, int flags);
+};
+
 } /* namespace libcamera */
 
 #endif /* __LIBCAMERA_BUFFER_H__ */
diff --git a/src/libcamera/buffer.cpp b/src/libcamera/buffer.cpp
index af5345b91195..0fe12ab93b58 100644
--- a/src/libcamera/buffer.cpp
+++ b/src/libcamera/buffer.cpp
@@ -281,6 +281,72 @@  int FrameBuffer::copyFrom(const FrameBuffer *src)
 	return 0;
 }
 
+/**
+ * \brief Construct an empty MappedBuffer
+ *
+ * A default constructor is required to allow subclassing the MappedBuffer
+ * class. Construct an initialised, but invalid MappedBuffer.
+ */
+MappedBuffer::MappedBuffer()
+	: error_(0), valid_(false)
+{
+}
+
+/**
+ * \brief Move constructor, create a MappedBuffer by taking the \a rhs mappings
+ * \param[in] rhs The other MappedBuffer
+ *
+ * Moving a MappedBuffer moves the mappings contained in the \a rhs to the new
+ * MappedBuffer and invalidates the \a rhs. No mappings are unmapped or
+ * destroyed in this process.
+ */
+MappedBuffer::MappedBuffer(MappedBuffer &&rhs)
+{
+	error_ = rhs.error_;
+	valid_ = rhs.valid_;
+	maps_ = std::move(rhs.maps_);
+	rhs.valid_ = false;
+	rhs.error_ = 0;
+}
+
+MappedBuffer::~MappedBuffer()
+{
+	for (MappedPlane map : maps_)
+		munmap(map.address, map.length);
+}
+
+/**
+ * \fn MappedBuffer::isValid()
+ * \brief Check if the MappedBuffer instance is valid
+ * \return True if the MappedBuffer has valid mappings, false otherwise
+ */
+
+/**
+ * \fn MappedBuffer::error()
+ * \brief Retrieve the map error status
+ *
+ * This function retrieves the error status from the MappedBuffer.
+ * The error status is a negative number as defined by errno.h. If
+ * no error occurred, this function returns 0.
+ */
+
+/**
+ * \var MappedBuffer::valid_
+ * \brief Stores the status of the mapping
+ *
+ * MappedBuffer implementations shall set this to represent if the mapping
+ * was successfully completed without errors.
+ */
+
+
+/**
+ * \var MappedBuffer::error_
+ * \brief Stores the error value if present
+ *
+ * MappedBuffer implementations shall set this to a negative value as defined
+ * by errno.h if an error occured during the mapping process
+ */
+
 /**
  * \brief Map all planes of a FrameBuffer
  * \param[in] src Buffer to be mapped
@@ -291,7 +357,6 @@  int FrameBuffer::copyFrom(const FrameBuffer *src)
  * PROT_WRITE, or a bitwise-or combination of both.
  */
 MappedFrameBuffer::MappedFrameBuffer(const FrameBuffer *buffer, int flags)
-	: error_(0)
 {
 	maps_.reserve(buffer->planes().size());
 
@@ -311,17 +376,4 @@  MappedFrameBuffer::MappedFrameBuffer(const FrameBuffer *buffer, int flags)
 	valid_ = buffer->planes().size() == maps_.size();
 }
 
-MappedFrameBuffer::~MappedFrameBuffer()
-{
-	for (MappedPlane map : maps_)
-		munmap(map.address, map.length);
-}
-
-MappedFrameBuffer::MappedFrameBuffer(MappedFrameBuffer &&rhs)
-{
-	error_ = rhs.error_;
-	valid_ = rhs.valid_;
-	maps_ = std::move(rhs.maps_);
-}
-
 } /* namespace libcamera */