Message ID | 20211128235752.10836-15-laurent.pinchart@ideasonboard.com |
---|---|
State | Superseded |
Headers | show |
Series |
|
Related | show |
Hi Laurent, On Mon, Nov 29, 2021 at 8:58 AM Laurent Pinchart <laurent.pinchart@ideasonboard.com> wrote: > > From: Hirokazu Honda <hiroh@chromium.org> > > Manages file descriptors owned by V4L2VideoDevice by UniqueFD. > This also changes the return type of exportDmabufFd to UniqueFD > from FileDescriptor in order to represent a caller owns the > returned file file descriptor. > > Signed-off-by: Hirokazu Honda <hiroh@chromium.org> > Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> > --- > Changes since v2: > > - Use FileDescriptor(UniqueFD &&) constructor I think this should be Use FileDescriptor(UniqueFD) constructor, but it doesn't matter absolutely. Reviewed-by: Hirokazu Honda <hiroh@chromium.org> if applicable. > - Fix errno handling > - Return {} > --- > include/libcamera/internal/v4l2_videodevice.h | 4 +-- > src/libcamera/v4l2_videodevice.cpp | 30 ++++++++----------- > 2 files changed, 15 insertions(+), 19 deletions(-) > > diff --git a/include/libcamera/internal/v4l2_videodevice.h b/include/libcamera/internal/v4l2_videodevice.h > index 4a44b7fd53b1..80b665771782 100644 > --- a/include/libcamera/internal/v4l2_videodevice.h > +++ b/include/libcamera/internal/v4l2_videodevice.h > @@ -19,6 +19,7 @@ > #include <libcamera/base/class.h> > #include <libcamera/base/log.h> > #include <libcamera/base/signal.h> > +#include <libcamera/base/unique_fd.h> > > #include <libcamera/framebuffer.h> > #include <libcamera/geometry.h> > @@ -31,7 +32,6 @@ > namespace libcamera { > > class EventNotifier; > -class FileDescriptor; > class MediaDevice; > class MediaEntity; > > @@ -238,7 +238,7 @@ private: > int createBuffers(unsigned int count, > std::vector<std::unique_ptr<FrameBuffer>> *buffers); > std::unique_ptr<FrameBuffer> createBuffer(unsigned int index); > - FileDescriptor exportDmabufFd(unsigned int index, unsigned int plane); > + UniqueFD exportDmabufFd(unsigned int index, unsigned int plane); > > void bufferAvailable(); > FrameBuffer *dequeueBuffer(); > diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp > index c95626d33cc3..3966483a365f 100644 > --- a/src/libcamera/v4l2_videodevice.cpp > +++ b/src/libcamera/v4l2_videodevice.cpp > @@ -1320,12 +1320,12 @@ std::unique_ptr<FrameBuffer> V4L2VideoDevice::createBuffer(unsigned int index) > > std::vector<FrameBuffer::Plane> planes; > for (unsigned int nplane = 0; nplane < numPlanes; nplane++) { > - FileDescriptor fd = exportDmabufFd(buf.index, nplane); > + UniqueFD fd = exportDmabufFd(buf.index, nplane); > if (!fd.isValid()) > return nullptr; > > FrameBuffer::Plane plane; > - plane.fd = std::move(fd); > + plane.fd = FileDescriptor(std::move(fd)); > /* > * V4L2 API doesn't provide dmabuf offset information of plane. > * Set 0 as a placeholder offset. > @@ -1380,8 +1380,8 @@ std::unique_ptr<FrameBuffer> V4L2VideoDevice::createBuffer(unsigned int index) > return std::make_unique<FrameBuffer>(planes); > } > > -FileDescriptor V4L2VideoDevice::exportDmabufFd(unsigned int index, > - unsigned int plane) > +UniqueFD V4L2VideoDevice::exportDmabufFd(unsigned int index, > + unsigned int plane) > { > struct v4l2_exportbuffer expbuf = {}; > int ret; > @@ -1395,10 +1395,10 @@ FileDescriptor V4L2VideoDevice::exportDmabufFd(unsigned int index, > if (ret < 0) { > LOG(V4L2, Error) > << "Failed to export buffer: " << strerror(-ret); > - return FileDescriptor(); > + return {}; > } > > - return FileDescriptor(std::move(expbuf.fd)); > + return UniqueFD(expbuf.fd); > } > > /** > @@ -1896,7 +1896,6 @@ V4L2M2MDevice::~V4L2M2MDevice() > */ > int V4L2M2MDevice::open() > { > - int fd; > int ret; > > /* > @@ -1905,30 +1904,27 @@ int V4L2M2MDevice::open() > * as the V4L2VideoDevice::open() retains a handle by duplicating the > * fd passed in. > */ > - fd = syscall(SYS_openat, AT_FDCWD, deviceNode_.c_str(), > - O_RDWR | O_NONBLOCK); > - if (fd < 0) { > + UniqueFD fd(syscall(SYS_openat, AT_FDCWD, deviceNode_.c_str(), > + O_RDWR | O_NONBLOCK)); > + if (!fd.isValid()) { > ret = -errno; > - LOG(V4L2, Error) > - << "Failed to open V4L2 M2M device: " << strerror(-ret); > + LOG(V4L2, Error) << "Failed to open V4L2 M2M device: " > + << strerror(-ret); > return ret; > } > > - ret = output_->open(fd, V4L2_BUF_TYPE_VIDEO_OUTPUT); > + ret = output_->open(fd.get(), V4L2_BUF_TYPE_VIDEO_OUTPUT); > if (ret) > goto err; > > - ret = capture_->open(fd, V4L2_BUF_TYPE_VIDEO_CAPTURE); > + ret = capture_->open(fd.get(), V4L2_BUF_TYPE_VIDEO_CAPTURE); > if (ret) > goto err; > > - ::close(fd); > - > return 0; > > err: > close(); > - ::close(fd); > > return ret; > } > -- > Regards, > > Laurent Pinchart >
Hi Laurent On Mon, Nov 29, 2021 at 01:57:49AM +0200, Laurent Pinchart wrote: > From: Hirokazu Honda <hiroh@chromium.org> > > Manages file descriptors owned by V4L2VideoDevice by UniqueFD. > This also changes the return type of exportDmabufFd to UniqueFD > from FileDescriptor in order to represent a caller owns the > returned file file descriptor. > > Signed-off-by: Hirokazu Honda <hiroh@chromium.org> > Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Jacopo Mondi <jacopo@jmondi.org> Thanks j > --- > Changes since v2: > > - Use FileDescriptor(UniqueFD &&) constructor > - Fix errno handling > - Return {} > --- > include/libcamera/internal/v4l2_videodevice.h | 4 +-- > src/libcamera/v4l2_videodevice.cpp | 30 ++++++++----------- > 2 files changed, 15 insertions(+), 19 deletions(-) > > diff --git a/include/libcamera/internal/v4l2_videodevice.h b/include/libcamera/internal/v4l2_videodevice.h > index 4a44b7fd53b1..80b665771782 100644 > --- a/include/libcamera/internal/v4l2_videodevice.h > +++ b/include/libcamera/internal/v4l2_videodevice.h > @@ -19,6 +19,7 @@ > #include <libcamera/base/class.h> > #include <libcamera/base/log.h> > #include <libcamera/base/signal.h> > +#include <libcamera/base/unique_fd.h> > > #include <libcamera/framebuffer.h> > #include <libcamera/geometry.h> > @@ -31,7 +32,6 @@ > namespace libcamera { > > class EventNotifier; > -class FileDescriptor; > class MediaDevice; > class MediaEntity; > > @@ -238,7 +238,7 @@ private: > int createBuffers(unsigned int count, > std::vector<std::unique_ptr<FrameBuffer>> *buffers); > std::unique_ptr<FrameBuffer> createBuffer(unsigned int index); > - FileDescriptor exportDmabufFd(unsigned int index, unsigned int plane); > + UniqueFD exportDmabufFd(unsigned int index, unsigned int plane); > > void bufferAvailable(); > FrameBuffer *dequeueBuffer(); > diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp > index c95626d33cc3..3966483a365f 100644 > --- a/src/libcamera/v4l2_videodevice.cpp > +++ b/src/libcamera/v4l2_videodevice.cpp > @@ -1320,12 +1320,12 @@ std::unique_ptr<FrameBuffer> V4L2VideoDevice::createBuffer(unsigned int index) > > std::vector<FrameBuffer::Plane> planes; > for (unsigned int nplane = 0; nplane < numPlanes; nplane++) { > - FileDescriptor fd = exportDmabufFd(buf.index, nplane); > + UniqueFD fd = exportDmabufFd(buf.index, nplane); > if (!fd.isValid()) > return nullptr; > > FrameBuffer::Plane plane; > - plane.fd = std::move(fd); > + plane.fd = FileDescriptor(std::move(fd)); > /* > * V4L2 API doesn't provide dmabuf offset information of plane. > * Set 0 as a placeholder offset. > @@ -1380,8 +1380,8 @@ std::unique_ptr<FrameBuffer> V4L2VideoDevice::createBuffer(unsigned int index) > return std::make_unique<FrameBuffer>(planes); > } > > -FileDescriptor V4L2VideoDevice::exportDmabufFd(unsigned int index, > - unsigned int plane) > +UniqueFD V4L2VideoDevice::exportDmabufFd(unsigned int index, > + unsigned int plane) > { > struct v4l2_exportbuffer expbuf = {}; > int ret; > @@ -1395,10 +1395,10 @@ FileDescriptor V4L2VideoDevice::exportDmabufFd(unsigned int index, > if (ret < 0) { > LOG(V4L2, Error) > << "Failed to export buffer: " << strerror(-ret); > - return FileDescriptor(); > + return {}; > } > > - return FileDescriptor(std::move(expbuf.fd)); > + return UniqueFD(expbuf.fd); > } > > /** > @@ -1896,7 +1896,6 @@ V4L2M2MDevice::~V4L2M2MDevice() > */ > int V4L2M2MDevice::open() > { > - int fd; > int ret; > > /* > @@ -1905,30 +1904,27 @@ int V4L2M2MDevice::open() > * as the V4L2VideoDevice::open() retains a handle by duplicating the > * fd passed in. > */ > - fd = syscall(SYS_openat, AT_FDCWD, deviceNode_.c_str(), > - O_RDWR | O_NONBLOCK); > - if (fd < 0) { > + UniqueFD fd(syscall(SYS_openat, AT_FDCWD, deviceNode_.c_str(), > + O_RDWR | O_NONBLOCK)); > + if (!fd.isValid()) { > ret = -errno; > - LOG(V4L2, Error) > - << "Failed to open V4L2 M2M device: " << strerror(-ret); > + LOG(V4L2, Error) << "Failed to open V4L2 M2M device: " > + << strerror(-ret); > return ret; > } > > - ret = output_->open(fd, V4L2_BUF_TYPE_VIDEO_OUTPUT); > + ret = output_->open(fd.get(), V4L2_BUF_TYPE_VIDEO_OUTPUT); > if (ret) > goto err; > > - ret = capture_->open(fd, V4L2_BUF_TYPE_VIDEO_CAPTURE); > + ret = capture_->open(fd.get(), V4L2_BUF_TYPE_VIDEO_CAPTURE); > if (ret) > goto err; > > - ::close(fd); > - > return 0; > > err: > close(); > - ::close(fd); > > return ret; > } > -- > Regards, > > Laurent Pinchart >
diff --git a/include/libcamera/internal/v4l2_videodevice.h b/include/libcamera/internal/v4l2_videodevice.h index 4a44b7fd53b1..80b665771782 100644 --- a/include/libcamera/internal/v4l2_videodevice.h +++ b/include/libcamera/internal/v4l2_videodevice.h @@ -19,6 +19,7 @@ #include <libcamera/base/class.h> #include <libcamera/base/log.h> #include <libcamera/base/signal.h> +#include <libcamera/base/unique_fd.h> #include <libcamera/framebuffer.h> #include <libcamera/geometry.h> @@ -31,7 +32,6 @@ namespace libcamera { class EventNotifier; -class FileDescriptor; class MediaDevice; class MediaEntity; @@ -238,7 +238,7 @@ private: int createBuffers(unsigned int count, std::vector<std::unique_ptr<FrameBuffer>> *buffers); std::unique_ptr<FrameBuffer> createBuffer(unsigned int index); - FileDescriptor exportDmabufFd(unsigned int index, unsigned int plane); + UniqueFD exportDmabufFd(unsigned int index, unsigned int plane); void bufferAvailable(); FrameBuffer *dequeueBuffer(); diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp index c95626d33cc3..3966483a365f 100644 --- a/src/libcamera/v4l2_videodevice.cpp +++ b/src/libcamera/v4l2_videodevice.cpp @@ -1320,12 +1320,12 @@ std::unique_ptr<FrameBuffer> V4L2VideoDevice::createBuffer(unsigned int index) std::vector<FrameBuffer::Plane> planes; for (unsigned int nplane = 0; nplane < numPlanes; nplane++) { - FileDescriptor fd = exportDmabufFd(buf.index, nplane); + UniqueFD fd = exportDmabufFd(buf.index, nplane); if (!fd.isValid()) return nullptr; FrameBuffer::Plane plane; - plane.fd = std::move(fd); + plane.fd = FileDescriptor(std::move(fd)); /* * V4L2 API doesn't provide dmabuf offset information of plane. * Set 0 as a placeholder offset. @@ -1380,8 +1380,8 @@ std::unique_ptr<FrameBuffer> V4L2VideoDevice::createBuffer(unsigned int index) return std::make_unique<FrameBuffer>(planes); } -FileDescriptor V4L2VideoDevice::exportDmabufFd(unsigned int index, - unsigned int plane) +UniqueFD V4L2VideoDevice::exportDmabufFd(unsigned int index, + unsigned int plane) { struct v4l2_exportbuffer expbuf = {}; int ret; @@ -1395,10 +1395,10 @@ FileDescriptor V4L2VideoDevice::exportDmabufFd(unsigned int index, if (ret < 0) { LOG(V4L2, Error) << "Failed to export buffer: " << strerror(-ret); - return FileDescriptor(); + return {}; } - return FileDescriptor(std::move(expbuf.fd)); + return UniqueFD(expbuf.fd); } /** @@ -1896,7 +1896,6 @@ V4L2M2MDevice::~V4L2M2MDevice() */ int V4L2M2MDevice::open() { - int fd; int ret; /* @@ -1905,30 +1904,27 @@ int V4L2M2MDevice::open() * as the V4L2VideoDevice::open() retains a handle by duplicating the * fd passed in. */ - fd = syscall(SYS_openat, AT_FDCWD, deviceNode_.c_str(), - O_RDWR | O_NONBLOCK); - if (fd < 0) { + UniqueFD fd(syscall(SYS_openat, AT_FDCWD, deviceNode_.c_str(), + O_RDWR | O_NONBLOCK)); + if (!fd.isValid()) { ret = -errno; - LOG(V4L2, Error) - << "Failed to open V4L2 M2M device: " << strerror(-ret); + LOG(V4L2, Error) << "Failed to open V4L2 M2M device: " + << strerror(-ret); return ret; } - ret = output_->open(fd, V4L2_BUF_TYPE_VIDEO_OUTPUT); + ret = output_->open(fd.get(), V4L2_BUF_TYPE_VIDEO_OUTPUT); if (ret) goto err; - ret = capture_->open(fd, V4L2_BUF_TYPE_VIDEO_CAPTURE); + ret = capture_->open(fd.get(), V4L2_BUF_TYPE_VIDEO_CAPTURE); if (ret) goto err; - ::close(fd); - return 0; err: close(); - ::close(fd); return ret; }