From 9217f274f64f690b768d332663e1731a3ee5ef15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niklas=20S=C3=B6derlund?= Date: Fri, 22 Nov 2019 16:22:56 +0100 Subject: libcamera: Switch to FrameBuffer interface MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Switch to the FrameBuffer interface where all buffers are treated as external buffers and are allocated outside the camera. Applications allocating buffers using libcamera are switched to use the FrameBufferAllocator helper. Follow-up changes to this one will finalize the transition to the new FrameBuffer interface by removing code that is left unused after this change. Signed-off-by: Niklas Söderlund Reviewed-by: Laurent Pinchart --- src/v4l2/v4l2_camera.cpp | 60 ++++++++++++++++++++++-------------------- src/v4l2/v4l2_camera.h | 39 ++++++++++----------------- src/v4l2/v4l2_camera_proxy.cpp | 17 ++++++------ 3 files changed, 55 insertions(+), 61 deletions(-) (limited to 'src/v4l2') diff --git a/src/v4l2/v4l2_camera.cpp b/src/v4l2/v4l2_camera.cpp index e4a03c41..10db15d6 100644 --- a/src/v4l2/v4l2_camera.cpp +++ b/src/v4l2/v4l2_camera.cpp @@ -16,24 +16,15 @@ using namespace libcamera; LOG_DECLARE_CATEGORY(V4L2Compat); -V4L2FrameMetadata::V4L2FrameMetadata(Buffer *buffer) - : index_(buffer->index()), - bytesused_(buffer->metadata().planes[0].bytesused), - timestamp_(buffer->metadata().timestamp), - sequence_(buffer->metadata().sequence), - status_(buffer->metadata().status) -{ -} - V4L2Camera::V4L2Camera(std::shared_ptr camera) - : camera_(camera), isRunning_(false) + : camera_(camera), isRunning_(false), bufferAllocator_(nullptr) { camera_->requestCompleted.connect(this, &V4L2Camera::requestComplete); } V4L2Camera::~V4L2Camera() { - camera_->release(); + close(); } int V4L2Camera::open() @@ -50,11 +41,16 @@ int V4L2Camera::open() return -EINVAL; } + bufferAllocator_ = FrameBufferAllocator::create(camera_); + return 0; } void V4L2Camera::close() { + delete bufferAllocator_; + bufferAllocator_ = nullptr; + camera_->release(); } @@ -63,12 +59,12 @@ void V4L2Camera::getStreamConfig(StreamConfiguration *streamConfig) *streamConfig = config_->at(0); } -std::vector V4L2Camera::completedBuffers() +std::vector V4L2Camera::completedBuffers() { - std::vector v; + std::vector v; bufferLock_.lock(); - for (std::unique_ptr &metadata : completedBuffers_) + for (std::unique_ptr &metadata : completedBuffers_) v.push_back(*metadata.get()); completedBuffers_.clear(); bufferLock_.unlock(); @@ -83,9 +79,9 @@ void V4L2Camera::requestComplete(Request *request) /* We only have one stream at the moment. */ bufferLock_.lock(); - Buffer *buffer = request->buffers().begin()->second; - std::unique_ptr metadata = - utils::make_unique(buffer); + FrameBuffer *buffer = request->buffers().begin()->second; + std::unique_ptr metadata = + utils::make_unique(request->cookie(), buffer->metadata()); completedBuffers_.push_back(std::move(metadata)); bufferLock_.unlock(); @@ -126,18 +122,31 @@ int V4L2Camera::configure(StreamConfiguration *streamConfigOut, int V4L2Camera::allocBuffers(unsigned int count) { int ret = camera_->allocateBuffers(); - return ret == -EACCES ? -EBUSY : ret; + if (ret) + return ret == -EACCES ? -EBUSY : ret; + + Stream *stream = *camera_->streams().begin(); + + return bufferAllocator_->allocate(stream); } void V4L2Camera::freeBuffers() { + Stream *stream = *camera_->streams().begin(); + bufferAllocator_->free(stream); camera_->freeBuffers(); } FileDescriptor V4L2Camera::getBufferFd(unsigned int index) { Stream *stream = *camera_->streams().begin(); - return stream->buffers()[index].planes()[0].fd; + const std::vector> &buffers = + bufferAllocator_->buffers(stream); + + if (buffers.size() <= index) + return FileDescriptor(); + + return buffers[index]->planes()[0].fd; } int V4L2Camera::streamOn() @@ -180,21 +189,16 @@ int V4L2Camera::streamOff() int V4L2Camera::qbuf(unsigned int index) { - Stream *stream = config_->at(0).stream(); - std::unique_ptr buffer = stream->createBuffer(index); - if (!buffer) { - LOG(V4L2Compat, Error) << "Can't create buffer"; - return -ENOMEM; - } - std::unique_ptr request = - std::unique_ptr(camera_->createRequest()); + std::unique_ptr(camera_->createRequest(index)); if (!request) { LOG(V4L2Compat, Error) << "Can't create request"; return -ENOMEM; } - int ret = request->addBuffer(stream, std::move(buffer)); + Stream *stream = config_->at(0).stream(); + FrameBuffer *buffer = bufferAllocator_->buffers(stream)[index].get(); + int ret = request->addBuffer(stream, buffer); if (ret < 0) { LOG(V4L2Compat, Error) << "Can't set buffer for request"; return -ENOMEM; diff --git a/src/v4l2/v4l2_camera.h b/src/v4l2/v4l2_camera.h index 06eab0e1..f1f04d9e 100644 --- a/src/v4l2/v4l2_camera.h +++ b/src/v4l2/v4l2_camera.h @@ -9,50 +9,38 @@ #define __V4L2_CAMERA_H__ #include -#include #include +#include #include #include #include +#include #include "semaphore.h" using namespace libcamera; -class V4L2FrameMetadata +class V4L2Camera : public Object { public: - V4L2FrameMetadata(Buffer *buffer); - - int index() const { return index_; } - - unsigned int bytesused() const { return bytesused_; } - uint64_t timestamp() const { return timestamp_; } - unsigned int sequence() const { return sequence_; } - - FrameMetadata::Status status() const { return status_; } - -private: - int index_; + struct Buffer { + Buffer(unsigned int index, const FrameMetadata &data) + : index(index), data(data) + { + } - unsigned int bytesused_; - uint64_t timestamp_; - unsigned int sequence_; + unsigned int index; + FrameMetadata data; + }; - FrameMetadata::Status status_; -}; - -class V4L2Camera : public Object -{ -public: V4L2Camera(std::shared_ptr camera); ~V4L2Camera(); int open(); void close(); void getStreamConfig(StreamConfiguration *streamConfig); - std::vector completedBuffers(); + std::vector completedBuffers(); int configure(StreamConfiguration *streamConfigOut, const Size &size, PixelFormat pixelformat, @@ -78,9 +66,10 @@ private: bool isRunning_; std::mutex bufferLock_; + FrameBufferAllocator *bufferAllocator_; std::deque> pendingRequests_; - std::deque> completedBuffers_; + std::deque> completedBuffers_; }; #endif /* __V4L2_CAMERA_H__ */ diff --git a/src/v4l2/v4l2_camera_proxy.cpp b/src/v4l2/v4l2_camera_proxy.cpp index 5158eac4..28e58722 100644 --- a/src/v4l2/v4l2_camera_proxy.cpp +++ b/src/v4l2/v4l2_camera_proxy.cpp @@ -187,17 +187,18 @@ void V4L2CameraProxy::querycap(std::shared_ptr camera) void V4L2CameraProxy::updateBuffers() { - std::vector completedBuffers = vcam_->completedBuffers(); - for (V4L2FrameMetadata &fmd : completedBuffers) { - struct v4l2_buffer &buf = buffers_[fmd.index()]; + std::vector completedBuffers = vcam_->completedBuffers(); + for (const V4L2Camera::Buffer &buffer : completedBuffers) { + const FrameMetadata &fmd = buffer.data; + struct v4l2_buffer &buf = buffers_[buffer.index]; - switch (fmd.status()) { + switch (fmd.status) { case FrameMetadata::FrameSuccess: - buf.bytesused = fmd.bytesused(); + buf.bytesused = fmd.planes[0].bytesused; buf.field = V4L2_FIELD_NONE; - buf.timestamp.tv_sec = fmd.timestamp() / 1000000000; - buf.timestamp.tv_usec = fmd.timestamp() % 1000000; - buf.sequence = fmd.sequence(); + buf.timestamp.tv_sec = fmd.timestamp / 1000000000; + buf.timestamp.tv_usec = fmd.timestamp % 1000000; + buf.sequence = fmd.sequence; buf.flags |= V4L2_BUF_FLAG_DONE; break; -- cgit v1.2.1