From patchwork Thu Jun 10 07:50:24 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 12540 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id CF3B9BD78E for ; Thu, 10 Jun 2021 07:50:47 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 868A46893F; Thu, 10 Jun 2021 09:50:47 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="jGjthLhx"; dkim-atps=neutral Received: from mail-pl1-x631.google.com (mail-pl1-x631.google.com [IPv6:2607:f8b0:4864:20::631]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 0D13468937 for ; Thu, 10 Jun 2021 09:50:46 +0200 (CEST) Received: by mail-pl1-x631.google.com with SMTP id h1so552685plt.1 for ; Thu, 10 Jun 2021 00:50:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=oQtmMIORVn3nv2jgxMAC+YN9+BInlyeVUONDYOxbT4c=; b=jGjthLhxxXG+cHZYuL00/PWiCJUvk/WHRTybUVMR4PQ3QWdicZQNGdcVNsJBAmdfB0 KLjIXTXLKRzTpfMstQW9hTayhtc0B3+47FErRpXdpeT8vuKH75uiPUzogo0gNDc535lM +cvoh4tejwz2/PnoQYMtmQo2EPiNWzpY+oWf0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=oQtmMIORVn3nv2jgxMAC+YN9+BInlyeVUONDYOxbT4c=; b=dxdFLHN/41o9DnPYSqgc7n+nq9DrQbERthUCiJVaSQYRiAGWDq5PoM6z4vgP1FD+0r J9tnJ08UJ/+nyWSDYI+6+PdX+wSq9NNPDs3awXlgfw2FeERu71ICpgkDpNkYby6c2hf4 rhxiWQkJ4m7RQN+I9QXswWQ0MOtqfZUhvKb34eQ6ftl2UzWzfdnl0is/EPU8wpXB0sTJ jIizIzXLX+6k3CSqMVKsfdXgtqwyFICEF9Uyxbl7mFbNQjBV+xC5Z1FGlISuJABGlFzp ipaBqtUtTqGFj13Zy0o/TrWZSg1w/YoFlOrKn0rqTjReqFPSlfzaUxNOAftq75FPDJrD CJmA== X-Gm-Message-State: AOAM53342/5uYw4S+8xUGijk9dmRlHFBev1jedXtQJrMYdl/9s3fg4VT se3i1KWInyzMEdV4zmq5hWUAp+UJHFZ5Ng== X-Google-Smtp-Source: ABdhPJw89MROU9tHGz+IdgyYP97mkw4gvLX+pQVvGI5hOAqUUUUHi7skxKWAD6U7SDzHUrN6Y479uA== X-Received: by 2002:a17:90a:f48c:: with SMTP id bx12mr2055707pjb.200.1623311444338; Thu, 10 Jun 2021 00:50:44 -0700 (PDT) Received: from hiroh2.tok.corp.google.com ([2401:fa00:8f:203:98e0:b356:1c8a:25d4]) by smtp.gmail.com with ESMTPSA id d66sm1565161pfa.32.2021.06.10.00.50.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Jun 2021 00:50:43 -0700 (PDT) From: Hirokazu Honda To: libcamera-devel@lists.libcamera.org Date: Thu, 10 Jun 2021 16:50:24 +0900 Message-Id: <20210610075027.523672-8-hiroh@chromium.org> X-Mailer: git-send-email 2.32.0.rc1.229.g3e70b5a671-goog In-Reply-To: <20210610075027.523672-1-hiroh@chromium.org> References: <20210610075027.523672-1-hiroh@chromium.org> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 07/10] libcamera: V4L2VideoDevice: Use fd for a file descriptor X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Manages file descriptors owned by V4L2VideoDevice by ScopedFD. This also changes the return type of exportDmabufFd to ScopedFD from FileDescriptor in order to represent a caller owns the returned file file descriptor. Signed-off-by: Hirokazu Honda --- include/libcamera/internal/v4l2_videodevice.h | 4 +-- src/libcamera/v4l2_videodevice.cpp | 33 ++++++++----------- 2 files changed, 15 insertions(+), 22 deletions(-) diff --git a/include/libcamera/internal/v4l2_videodevice.h b/include/libcamera/internal/v4l2_videodevice.h index 7938343b..925a8e82 100644 --- a/include/libcamera/internal/v4l2_videodevice.h +++ b/include/libcamera/internal/v4l2_videodevice.h @@ -30,9 +30,9 @@ namespace libcamera { class EventNotifier; -class FileDescriptor; class MediaDevice; class MediaEntity; +class ScopedFD; struct V4L2Capability final : v4l2_capability { const char *driver() const @@ -235,7 +235,7 @@ private: int createBuffers(unsigned int count, std::vector> *buffers); std::unique_ptr createBuffer(unsigned int index); - FileDescriptor exportDmabufFd(unsigned int index, unsigned int plane); + ScopedFD exportDmabufFd(unsigned int index, unsigned int plane); void bufferAvailable(EventNotifier *notifier); FrameBuffer *dequeueBuffer(); diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp index ce6ad8b5..1dfb011c 100644 --- a/src/libcamera/v4l2_videodevice.cpp +++ b/src/libcamera/v4l2_videodevice.cpp @@ -1274,12 +1274,12 @@ std::unique_ptr V4L2VideoDevice::createBuffer(unsigned int index) std::vector planes; for (unsigned int nplane = 0; nplane < numPlanes; nplane++) { - FileDescriptor fd = exportDmabufFd(buf.index, nplane); + ScopedFD fd = exportDmabufFd(buf.index, nplane); if (!fd.isValid()) return nullptr; FrameBuffer::Plane plane; - plane.fd = std::move(fd); + plane.fd = FileDescriptor(fd.release()); plane.length = multiPlanar ? buf.m.planes[nplane].length : buf.length; @@ -1289,7 +1289,7 @@ std::unique_ptr V4L2VideoDevice::createBuffer(unsigned int index) return std::make_unique(std::move(planes)); } -FileDescriptor V4L2VideoDevice::exportDmabufFd(unsigned int index, +ScopedFD V4L2VideoDevice::exportDmabufFd(unsigned int index, unsigned int plane) { struct v4l2_exportbuffer expbuf = {}; @@ -1304,10 +1304,10 @@ FileDescriptor V4L2VideoDevice::exportDmabufFd(unsigned int index, if (ret < 0) { LOG(V4L2, Error) << "Failed to export buffer: " << strerror(-ret); - return FileDescriptor(); + return ScopedFD(); } - return FileDescriptor(std::move(expbuf.fd)); + return ScopedFD(expbuf.fd); } /** @@ -1699,39 +1699,32 @@ V4L2M2MDevice::~V4L2M2MDevice() */ int V4L2M2MDevice::open() { - int fd; - int ret; - /* * The output and capture V4L2VideoDevice instances use the same file * handle for the same device node. The local file handle can be closed * 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) { - ret = -errno; - LOG(V4L2, Error) - << "Failed to open V4L2 M2M device: " << strerror(-ret); - return ret; + ScopedFD fd(syscall(SYS_openat, AT_FDCWD, deviceNode_.c_str(), + O_RDWR | O_NONBLOCK)); + if (!fd.isValid()) { + LOG(V4L2, Error) << "Failed to open V4L2 M2M device: " + << strerror(errno); + return -errno; } - ret = output_->open(fd, V4L2_BUF_TYPE_VIDEO_OUTPUT); + int 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; }