From patchwork Wed Nov 13 05:54:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cheng-Hao Yang X-Patchwork-Id: 21879 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 0606ABE173 for ; Wed, 13 Nov 2024 05:55:37 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 82523657FB; Wed, 13 Nov 2024 06:55:36 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="h/EyXBZ4"; dkim-atps=neutral Received: from mail-pf1-x42c.google.com (mail-pf1-x42c.google.com [IPv6:2607:f8b0:4864:20::42c]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 7294A657F8 for ; Wed, 13 Nov 2024 06:55:32 +0100 (CET) Received: by mail-pf1-x42c.google.com with SMTP id d2e1a72fcca58-71e625b00bcso5280647b3a.3 for ; Tue, 12 Nov 2024 21:55:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1731477330; x=1732082130; 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=kHPGmdFKP/nCGpmz205iSjC2QS8UqeRS8piXuDKNZD4=; b=h/EyXBZ4kWuO86/UsWvxdeSiT7CNjufkvZyYih8ZkZEU422U38IspHLlIsUQmN5NVc T5f1WQwxy9M6kBxHegVY0xJjzNEio1QoR3ZB8Mpg9NHgOJcYRoEpaw+PIXnlKZFMHTgC LpAkLhxd3Z/VRwk06EX9pz6K0wVZ42vetZ/d0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731477330; x=1732082130; 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=kHPGmdFKP/nCGpmz205iSjC2QS8UqeRS8piXuDKNZD4=; b=I6SY8Ov+gOg8cpOWW2kdvF3GCkFkmn+VlyQbxQ00a3YBKjhfEYnHEamCuCC6HJcO44 QbuRZvhcxdkZ8xjaMoXNzdvS4kws3BNA+7ZrAhNuPJ4QGZvCuXp5nkVR8hvYg7gDRsXu 8D4A2iKOInfjTBC5iVXZRNA9Mn5GOpcYHmVQSyt46C1mFuRpPbmZn7ee9lYHl2L0tPOB DGeXXlL1tdPTv1ardmFSt5h66XWtoiuauvvWlZ0i9rpKsayuvwbuJfLNBQqHYLYH3HW3 ov1fzkZcVvu9hncHYGSJY6M2KlksVV8yKe+85vZeE5N7expR3J5VeL/Toww0S8GDnajx hpzQ== X-Gm-Message-State: AOJu0YwLIyt+b8fgrvTeF/kbkZ4l0fdl69Ta9iGMa95Q6IJUItH2XE6u YlM4Q7l2QY5d3nuXDrCSgdHFjOkRsD9R5fYVor2TbyEc2S3BbY/StCeqCNpXAG0roi/QWXFbVIw = X-Google-Smtp-Source: AGHT+IE5ZRTcAcp7BopDr66Fkk87+ACKYvcvzTBokuZohIF+4imUWVunorlPz6Zc1063rgEZzOJnrw== X-Received: by 2002:a05:6a00:1794:b0:71e:4cff:2654 with SMTP id d2e1a72fcca58-7245799d6ebmr2462529b3a.6.1731477330511; Tue, 12 Nov 2024 21:55:30 -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 d2e1a72fcca58-7240785ffb6sm12382544b3a.40.2024.11.12.21.55.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Nov 2024 21:55:30 -0800 (PST) From: Harvey Yang To: libcamera-devel@lists.libcamera.org Cc: Harvey Yang , Han-Lin Chen Subject: [PATCH v2 1/2] DmaBufAllocator: Add Dma Buffer synchronization function & helper class Date: Wed, 13 Nov 2024 05:54:32 +0000 Message-ID: <20241113055524.3099340-2-chenghaoyang@chromium.org> X-Mailer: git-send-email 2.47.0.277.g8800431eea-goog In-Reply-To: <20241113055524.3099340-1-chenghaoyang@chromium.org> References: <20241113055524.3099340-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 Reviewed-by: Milan Zamazal --- .../libcamera/internal/dma_buf_allocator.h | 33 +++++++ src/libcamera/dma_buf_allocator.cpp | 90 +++++++++++++++++++ 2 files changed, 123 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..f0f068cc1 100644 --- a/src/libcamera/dma_buf_allocator.cpp +++ b/src/libcamera/dma_buf_allocator.cpp @@ -78,6 +78,81 @@ 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 read and 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) { + ret = errno; + LOG(DmaBufAllocator, Error) << "Unable to sync dma fd: " << fd + << ", err: " << strerror(ret) + << ", 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 +280,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 Wed Nov 13 05:54:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cheng-Hao Yang X-Patchwork-Id: 21880 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 082C9BE173 for ; Wed, 13 Nov 2024 05:55:39 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 9680165802; Wed, 13 Nov 2024 06:55:37 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="fV/VCFMe"; dkim-atps=neutral Received: from mail-pf1-x42a.google.com (mail-pf1-x42a.google.com [IPv6:2607:f8b0:4864:20::42a]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 0426B618BD for ; Wed, 13 Nov 2024 06:55:34 +0100 (CET) Received: by mail-pf1-x42a.google.com with SMTP id d2e1a72fcca58-71ec997ad06so5442149b3a.3 for ; Tue, 12 Nov 2024 21:55:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1731477332; x=1732082132; 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=2qpvh1MLmOWWFrvIQL8+ff1gmj7Ko2p8Cr0m99uxuGc=; b=fV/VCFMeMk3mBGnNUzSFn7CyzKa77KfkFzVnGUUV3XDAPPYy0e04PwLh6C2rO8Ef9H kQHPVzbcPo6hr7Al+ERP1zSmi/DBfm9e0xrzEWf6FaPL2rR08I6WDHcp5MU+gFsb6jVk lzT2VXp8mC7xOqLSm+wRYtcrbR3pH283MxjR8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731477332; x=1732082132; 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=2qpvh1MLmOWWFrvIQL8+ff1gmj7Ko2p8Cr0m99uxuGc=; b=Dc5YBZCRcV1AP9us4JpS0pqBmKjSr6DWGezOOtAUMeEkAC/tiU/ZnwD3kRFQ2ditwi AHJAstb53VANBn3nFXCHaXCjR8nM1seggWrC6w76/KrLnjCTMtSXPJJlh1CqkTP8qNch dS+hX6XAfcj+YFUmyaXHR8poKKAJLcOZwl8uvx78VbDlO1+Cn+oa13r5QpsBw++M++ts HetooAQG66Sj1LmAMmCsfV2jD9wAEEUYcG8BB5WMYawprSfSewGd+TlSyvZQmS6gOauO iUb/5UQllVmuZjjVGHwiHyUAPWKuuV7kwicPENAL4+j/256zEF5cAVrvuqJ0w9b68mPr vtOQ== X-Gm-Message-State: AOJu0Yy4WWYnfH7ycjEthLZeJVq//A3GgxpVmW9QXUj1M+wzGwsabuGN 4PuizquPK90+/5LWXe2nol1kz/1/sajs8GE6ATpdynaS+vTCpp95FSNp7HlTPt9zzsCJqEGq+yk = X-Google-Smtp-Source: AGHT+IGy2eOAjFMPyTCrRPufR/K2eLS0XB9xQo9aTBtSpCJebEKLsmUo+tV5eXsnx1R6XekutJUevQ== X-Received: by 2002:a05:6a20:3945:b0:1db:d8df:8c4e with SMTP id adf61e73a8af0-1dc5f8ed384mr6664276637.12.1731477332237; Tue, 12 Nov 2024 21:55:32 -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 d2e1a72fcca58-7240785ffb6sm12382544b3a.40.2024.11.12.21.55.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Nov 2024 21:55:31 -0800 (PST) From: Harvey Yang To: libcamera-devel@lists.libcamera.org Cc: Harvey Yang , Milan Zamazal Subject: [PATCH v2 2/2] debayer_cpu: Replace syncing DMABUFs with DmaSyncer Date: Wed, 13 Nov 2024 05:54:33 +0000 Message-ID: <20241113055524.3099340-3-chenghaoyang@chromium.org> X-Mailer: git-send-email 2.47.0.277.g8800431eea-goog In-Reply-To: <20241113055524.3099340-1-chenghaoyang@chromium.org> References: <20241113055524.3099340-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 &&