[libcamera-devel,v3,3/9] cam: file_sink: Use offset in mapping FrameBuffer
diff mbox series

Message ID 20210826112539.170694-4-hiroh@chromium.org
State Accepted
Headers show
Series
  • Add offset to FrameBuffer::Plane
Related show

Commit Message

Hirokazu Honda Aug. 26, 2021, 11:25 a.m. UTC
This fixes the way of mapping FrameBuffer in FrameSink by
using offset.

Signed-off-by: Hirokazu Honda <hiroh@chromium.org>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 src/cam/file_sink.cpp | 20 +++++++++++++++-----
 src/cam/file_sink.h   |  1 +
 2 files changed, 16 insertions(+), 5 deletions(-)

Patch
diff mbox series

diff --git a/src/cam/file_sink.cpp b/src/cam/file_sink.cpp
index 2d30694a..0b529e3e 100644
--- a/src/cam/file_sink.cpp
+++ b/src/cam/file_sink.cpp
@@ -51,12 +51,22 @@  int FileSink::configure(const libcamera::CameraConfiguration &config)
 
 void FileSink::mapBuffer(FrameBuffer *buffer)
 {
+	/* \todo use MappedFrameBuffer. */
 	for (const FrameBuffer::Plane &plane : buffer->planes()) {
-		void *memory = mmap(NULL, plane.length, PROT_READ, MAP_SHARED,
-				    plane.fd.fd(), 0);
+		const int fd = plane.fd.fd();
+		if (mappedBuffers_.find(fd) == mappedBuffers_.end()) {
+			/**
+			 * \todo Should we try to only map the portions of the
+			 * dmabuf that are used by planes ?
+			 */
+			size_t length = lseek(fd, 0, SEEK_END);
+			void *memory = mmap(NULL, plane.length, PROT_READ,
+					    MAP_SHARED, fd, 0);
+			mappedBuffers_[fd] = std::make_pair(memory, length);
+		}
 
-		mappedBuffers_[plane.fd.fd()] =
-			std::make_pair(memory, plane.length);
+		void *memory = mappedBuffers_[fd].first;
+		planeData_[&plane] = static_cast<uint8_t *>(memory) + plane.offset;
 	}
 }
 
@@ -102,7 +112,7 @@  void FileSink::writeBuffer(const Stream *stream, FrameBuffer *buffer)
 		const FrameBuffer::Plane &plane = buffer->planes()[i];
 		const FrameMetadata::Plane &meta = buffer->metadata().planes[i];
 
-		void *data = mappedBuffers_[plane.fd.fd()].first;
+		uint8_t *data = planeData_[&plane];
 		unsigned int length = std::min(meta.bytesused, plane.length);
 
 		if (meta.bytesused > plane.length)
diff --git a/src/cam/file_sink.h b/src/cam/file_sink.h
index c3eb230a..c12325d9 100644
--- a/src/cam/file_sink.h
+++ b/src/cam/file_sink.h
@@ -33,6 +33,7 @@  private:
 	std::map<const libcamera::Stream *, std::string> streamNames_;
 	std::string pattern_;
 	std::map<int, std::pair<void *, unsigned int>> mappedBuffers_;
+	std::map<const libcamera::FrameBuffer::Plane *, uint8_t *> planeData_;
 };
 
 #endif /* __CAM_FILE_SINK_H__ */