From patchwork Tue Nov 12 09:56:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harvey Yang X-Patchwork-Id: 21875 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 22E11C324C for ; Tue, 12 Nov 2024 10:01:04 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 50F39657EB; Tue, 12 Nov 2024 11:01:01 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="ZFenOcEe"; dkim-atps=neutral Received: from mail-il1-x12a.google.com (mail-il1-x12a.google.com [IPv6:2607:f8b0:4864:20::12a]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 4A0F8657DF for ; Tue, 12 Nov 2024 11:00:59 +0100 (CET) Received: by mail-il1-x12a.google.com with SMTP id e9e14a558f8ab-3a6bdc8f7a1so20639655ab.1 for ; Tue, 12 Nov 2024 02:00:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1731405657; x=1732010457; darn=lists.libcamera.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=fqG1hHrjPaJRskza6EpesAQ3sy6sBqFljv05eEnhOaI=; b=ZFenOcEeFK+KkNfjfoadZrcTtFuT7aUGUejRB/k/f/wb/l2tseYjfMvbaWWoPXU+pb c8/oQ922cXRBf7fvv45VdzgYmHy8PchmdT91JysI3G76RHbuxdN5fzIbL/SfC02j53mN RSna095iSSt2qw0x25Z101+7XVJnUuO3xBgWE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731405657; x=1732010457; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=fqG1hHrjPaJRskza6EpesAQ3sy6sBqFljv05eEnhOaI=; b=dloHWTvXcLN4xwmw40oELj1eNPf+/cjp4J3Wh6NQTY2cOV0bNQpweNinT3Iam6nv9g up10Z6LQuDWP5RdqwcPduy86loRbzwyLvCIMqIjPI34WqxiS9sgmNwfT7ouA5jpsBszr yvwDMRx6pFkF/8SRwu9Q5XKbmvknIXCmM+E7XJ3P8V00COcyE8QAVKzz7lRqjj+6ETHz uIS8Dc1bDWp+dfmGfGQI/ZWlAQd2MFIjkj2Q4krQITGsP9aepbcBo75/WSfMxIfq9aQV GEwEXPh/pz1+AMglGwTcriiMRdmCZYt7dBxEBerwHTgP08i133ujxL6AUu+oNuej72b9 Jsmw== X-Gm-Message-State: AOJu0YzVSu7r1q4XU1DS2mv42wUJYFV39PZ1TI4Z54GOIKd7QSHWhRnP RZi/me2+LFN3QjRPrdtUVKEcIfUsqhHMKdr9hCBDDllH0sDYNJUtEk298o87TMlCNSlK3CiAP+E = X-Google-Smtp-Source: AGHT+IE0z8wmNyqEXJN0RGWPku13X9o8Gndfqj464k+tL3z2Ml5PiHYia1o/Z9qYeJAMBVg2Hb7fjQ== X-Received: by 2002:a05:6e02:198f:b0:3a6:b1b0:6799 with SMTP id e9e14a558f8ab-3a6f19e38e6mr150463765ab.10.1731405657454; Tue, 12 Nov 2024 02:00:57 -0800 (PST) Received: from chenghaoyang-low.c.googlers.com.com (27.247.221.35.bc.googleusercontent.com. [35.221.247.27]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7f41f644214sm10157673a12.58.2024.11.12.02.00.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Nov 2024 02:00:57 -0800 (PST) From: Harvey Yang To: libcamera-devel@lists.libcamera.org Cc: Harvey Yang , Han-Lin Chen Subject: [PATCH 1/2] DmaBufAllocator: Add Dma Buffer synchronization function & helper class Date: Tue, 12 Nov 2024 09:56:54 +0000 Message-ID: <20241112100051.4071443-2-chenghaoyang@chromium.org> X-Mailer: git-send-email 2.47.0.277.g8800431eea-goog In-Reply-To: <20241112100051.4071443-1-chenghaoyang@chromium.org> References: <20241112100051.4071443-1-chenghaoyang@chromium.org> MIME-Version: 1.0 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" To synchronize CPU access with mmap and hardware access on DMA buffers, using `DMA_BUF_IOCTL_SYNC` is required. This patch adds a function and a helper class to allow users to sync buffers more easily. Signed-off-by: Han-Lin Chen Co-developed-by: Harvey Yang Signed-off-by: Harvey Yang --- .../libcamera/internal/dma_buf_allocator.h | 33 +++++++ src/libcamera/dma_buf_allocator.cpp | 88 +++++++++++++++++++ 2 files changed, 121 insertions(+) diff --git a/include/libcamera/internal/dma_buf_allocator.h b/include/libcamera/internal/dma_buf_allocator.h index d2a0a0d19..2b9b99678 100644 --- a/include/libcamera/internal/dma_buf_allocator.h +++ b/include/libcamera/internal/dma_buf_allocator.h @@ -23,6 +23,19 @@ public: using DmaBufAllocatorFlags = Flags; + enum class SyncStep { + Start = 0, + End + }; + + enum class SyncType { + Read = 0, + Write, + ReadWrite, + }; + + static void sync(int fd, SyncStep step, SyncType type); + DmaBufAllocator(DmaBufAllocatorFlags flags = DmaBufAllocatorFlag::CmaHeap); ~DmaBufAllocator(); bool isValid() const { return providerHandle_.isValid(); } @@ -35,6 +48,26 @@ private: DmaBufAllocatorFlag type_; }; +class DmaSyncer final +{ +public: + explicit DmaSyncer(int fd, + DmaBufAllocator::SyncType type = DmaBufAllocator::SyncType::ReadWrite) + : fd_(fd), type_(type) + { + DmaBufAllocator::sync(fd_, DmaBufAllocator::SyncStep::Start, type_); + } + + ~DmaSyncer() + { + DmaBufAllocator::sync(fd_, DmaBufAllocator::SyncStep::End, type_); + } + +private: + int fd_; + DmaBufAllocator::SyncType type_; +}; + LIBCAMERA_FLAGS_ENABLE_OPERATORS(DmaBufAllocator::DmaBufAllocatorFlag) } /* namespace libcamera */ diff --git a/src/libcamera/dma_buf_allocator.cpp b/src/libcamera/dma_buf_allocator.cpp index be6efb89f..3ae98c168 100644 --- a/src/libcamera/dma_buf_allocator.cpp +++ b/src/libcamera/dma_buf_allocator.cpp @@ -78,6 +78,79 @@ LOG_DEFINE_CATEGORY(DmaBufAllocator) * \brief A bitwise combination of DmaBufAllocator::DmaBufAllocatorFlag values */ +/** + * \enum DmaBufAllocator::SyncStep + * \brief Either start or end of the synchronization + * \var DmaBufAllocator::Start + * \brief Indicates the start of a map access session + * \var DmaBufAllocator::End + * \brief Indicates the end of a map access session + */ + +/** + * \enum DmaBufAllocator::SyncType + * \brief Read and/or write access via the CPU map + * \var DmaBufAllocator::Read + * \brief Indicates that the mapped dma-buf will be read by the client via the + * CPU map + * \var DmaBufAllocator::Write + * \brief Indicates that the mapped dm-buf will be written by the client via the + * CPU map + * \var DmaBufAllocator::ReadWrite + * \brief Indicates that the mapped dma-buf will be rad andd written by the + * client via the CPU map + */ + +/** + * \brief Synchronize CPU access and hardware access + * \param[in] fd The dma-buf's file descriptor to be synchronized + * \param[in] step Either start or end of the synchronization + * \param[in] type Read and/or write access during CPU mmap + * + * The client is expected to call this function with + * DmaBufAllocator::SyncStep::Start and DmaBufAllocator::SyncStep::End at the + * start and end of buffer access respectively. + */ +void DmaBufAllocator::sync(int fd, DmaBufAllocator::SyncStep step, DmaBufAllocator::SyncType type) +{ + uint64_t flags = 0; + switch (step) { + case DmaBufAllocator::SyncStep::Start: + flags = DMA_BUF_SYNC_START; + break; + case DmaBufAllocator::SyncStep::End: + flags = DMA_BUF_SYNC_END; + break; + } + + switch (type) { + case DmaBufAllocator::SyncType::Read: + flags = flags | DMA_BUF_SYNC_READ; + break; + case DmaBufAllocator::SyncType::Write: + flags = flags | DMA_BUF_SYNC_WRITE; + break; + case DmaBufAllocator::SyncType::ReadWrite: + flags = flags | DMA_BUF_SYNC_RW; + break; + } + + struct dma_buf_sync sync = { + .flags = flags + }; + + int ret; + do { + ret = ioctl(fd, DMA_BUF_IOCTL_SYNC, &sync); + } while (!ret && (errno == EINTR || errno == EAGAIN)); + + if (ret) { + LOG(DmaBufAllocator, Error) << "Unable to sync dma fd: " << fd + << ", step: " << static_cast(step) + << ", type: " << static_cast(type); + } +} + /** * \brief Construct a DmaBufAllocator of a given type * \param[in] type The type(s) of the dma-buf providers to allocate from @@ -205,4 +278,19 @@ UniqueFD DmaBufAllocator::alloc(const char *name, std::size_t size) return allocFromHeap(name, size); } +/** + * \class DmaSyncer + * \brief Helper class for dma-buf's synchronization + * + * This class wraps a userspace dma-buf's synchronization process with an + * object's lifetime. + */ + +/** + * \fn DmaSyncer::DmaSyncer(int fd, DmaBufAllocator::SyncType type = DmaBufAllocator::SyncType::ReadWrite) + * \brief Construct a DmaSyncer with a dma-buf's fd and the access type + * \param[in] fd The dma-buf's file descriptor to synchronize + * \param[in] type Read and/or write access via the CPU map + */ + } /* namespace libcamera */ From patchwork Tue Nov 12 09:56:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harvey Yang X-Patchwork-Id: 21876 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 B0AE9C3259 for ; Tue, 12 Nov 2024 10:01:05 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 6A6C1657E0; Tue, 12 Nov 2024 11:01:02 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="Ge6qavCP"; dkim-atps=neutral Received: from mail-il1-x12a.google.com (mail-il1-x12a.google.com [IPv6:2607:f8b0:4864:20::12a]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 667E665474 for ; Tue, 12 Nov 2024 11:01:00 +0100 (CET) Received: by mail-il1-x12a.google.com with SMTP id e9e14a558f8ab-3a6c1cfcb91so22990425ab.0 for ; Tue, 12 Nov 2024 02:01:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1731405659; x=1732010459; darn=lists.libcamera.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ETSnPQEUGsC+Ye+ja0ckktThpUHtsZEYfIELXy3ebhU=; b=Ge6qavCPjyf/8LLFBcV0WQM0XZhaD1dEDGFHiQmviWkFtkLCs1oKUSpD4vH5BUpvJC 7OwYBukjD37ALrea1xuCSfbOQNrlKrnuhWr/yvWkHpybCNylV+IVK7ABN24ZK17z//RU WFiKnzYyG/gMk05S1Hz82vwzKo9tShfZGth/I= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731405659; x=1732010459; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ETSnPQEUGsC+Ye+ja0ckktThpUHtsZEYfIELXy3ebhU=; b=g3BxPP51yjD6XyvL556XBPMrH+kJCvKFHUWmj1PyAhyOwVrj03JFkEXhxhcWwUvxDz 7XLsA9LIp8rg55fhAvblLGUyDcOF7TLiWV0e7FQwPkEaZY6s1qT2PH1qo7xH3O3aE5kV 8HbH9xELuk4No1qaySFC7FI+MOQuiBu+ZAvkscufmS2U0AhDVP/+EakwFSWXFqQa14NN HAijZglExPPHpbXxkVV52u16f3WbNqPcDqVvTlfQGvQNPJrovymOKX7oysC2qxKTQoX4 dKpZTXr2OLPNC5bkEthHUE27ztzZyfyZJUzqoTWBBfqjvWead0Oka9e9kZfuOKhYQqIS 1HrQ== X-Gm-Message-State: AOJu0YzvJVZdrTnH5joicB3tL8GhknMY3XV+3ptyoI81Sq50n/cg3NYO fZNAJea6V9E87iZwp2Ft7T2SBOPmGb82CRquS2Xh7nHSbYBBSjULrLQATqbqAwFKXxgaNNMBMOE = X-Google-Smtp-Source: AGHT+IHUMM/pkYrfVh69h2zB9MI1iJfRjfSNnvif7oDKQZzxqMNL51QFDbokSawwGxyCzXPcJsc/4A== X-Received: by 2002:a05:6e02:1568:b0:3a3:4175:79da with SMTP id e9e14a558f8ab-3a70c827cb2mr21152985ab.13.1731405658775; Tue, 12 Nov 2024 02:00:58 -0800 (PST) Received: from chenghaoyang-low.c.googlers.com.com (27.247.221.35.bc.googleusercontent.com. [35.221.247.27]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7f41f644214sm10157673a12.58.2024.11.12.02.00.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Nov 2024 02:00:58 -0800 (PST) From: Harvey Yang To: libcamera-devel@lists.libcamera.org Cc: Harvey Yang Subject: [PATCH 2/2] debayer_cpu: Replace syncing DMABUFs with DmaSyncer Date: Tue, 12 Nov 2024 09:56:55 +0000 Message-ID: <20241112100051.4071443-3-chenghaoyang@chromium.org> X-Mailer: git-send-email 2.47.0.277.g8800431eea-goog In-Reply-To: <20241112100051.4071443-1-chenghaoyang@chromium.org> References: <20241112100051.4071443-1-chenghaoyang@chromium.org> MIME-Version: 1.0 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" As there's an existing helper class DmaSyncer that makes synchronizing DMABUFs more easily, this patch removes the self-defined function and reuse DmaSyncer. Signed-off-by: Harvey Yang Reviewed-by: Milan Zamazal --- src/libcamera/software_isp/debayer_cpu.cpp | 29 ++++++---------------- 1 file changed, 8 insertions(+), 21 deletions(-) diff --git a/src/libcamera/software_isp/debayer_cpu.cpp b/src/libcamera/software_isp/debayer_cpu.cpp index cf5ecdf7a..852e14f20 100644 --- a/src/libcamera/software_isp/debayer_cpu.cpp +++ b/src/libcamera/software_isp/debayer_cpu.cpp @@ -20,6 +20,7 @@ #include #include "libcamera/internal/bayer_format.h" +#include "libcamera/internal/dma_buf_allocator.h" #include "libcamera/internal/framebuffer.h" #include "libcamera/internal/mapped_framebuffer.h" @@ -722,23 +723,6 @@ void DebayerCpu::process4(const uint8_t *src, uint8_t *dst) namespace { -void syncBufferForCPU(FrameBuffer *buffer, uint64_t syncFlags) -{ - for (const FrameBuffer::Plane &plane : buffer->planes()) { - const int fd = plane.fd.get(); - struct dma_buf_sync sync = { syncFlags }; - int ret; - - ret = ioctl(fd, DMA_BUF_IOCTL_SYNC, &sync); - if (ret < 0) { - ret = errno; - LOG(Debayer, Error) - << "Syncing buffer FD " << fd << " with flags " - << syncFlags << " failed: " << strerror(ret); - } - } -} - inline int64_t timeDiff(timespec &after, timespec &before) { return (after.tv_sec - before.tv_sec) * 1000000000LL + @@ -756,8 +740,12 @@ void DebayerCpu::process(uint32_t frame, FrameBuffer *input, FrameBuffer *output clock_gettime(CLOCK_MONOTONIC_RAW, &frameStartTime); } - syncBufferForCPU(input, DMA_BUF_SYNC_START | DMA_BUF_SYNC_READ); - syncBufferForCPU(output, DMA_BUF_SYNC_START | DMA_BUF_SYNC_WRITE); + std::vector dmaSyncers; + for (const FrameBuffer::Plane &plane : input->planes()) + dmaSyncers.emplace_back(plane.fd.get(), DmaBufAllocator::SyncType::Read); + + for (const FrameBuffer::Plane &plane : output->planes()) + dmaSyncers.emplace_back(plane.fd.get(), DmaBufAllocator::SyncType::Write); green_ = params.green; red_ = swapRedBlueGains_ ? params.blue : params.red; @@ -786,8 +774,7 @@ void DebayerCpu::process(uint32_t frame, FrameBuffer *input, FrameBuffer *output metadata.planes()[0].bytesused = out.planes()[0].size(); - syncBufferForCPU(output, DMA_BUF_SYNC_END | DMA_BUF_SYNC_WRITE); - syncBufferForCPU(input, DMA_BUF_SYNC_END | DMA_BUF_SYNC_READ); + dmaSyncers.clear(); /* Measure before emitting signals */ if (measuredFrames_ < DebayerCpu::kLastFrameToMeasure &&