From patchwork Thu Nov 21 05:51:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harvey Yang X-Patchwork-Id: 22052 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 AB51DC32F9 for ; Thu, 21 Nov 2024 05:54:54 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 64A8865F9F; Thu, 21 Nov 2024 06:54:54 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="Eq6mzEpx"; 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 88F9265FA8 for ; Thu, 21 Nov 2024 06:54:45 +0100 (CET) Received: by mail-pf1-x42c.google.com with SMTP id d2e1a72fcca58-720cb6ac25aso447358b3a.3 for ; Wed, 20 Nov 2024 21:54:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1732168483; x=1732773283; 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=w3/doozRmHoESE9I/LoHUzPmNKrwEmnNFS2T9EoQZxg=; b=Eq6mzEpxJh9BrlSmXgbsqgRDqi/Q3VWEtO64t7E/yq4YOaX8bnqS1jcw4PttYWrvZ/ 6JENy5i6cVg1GYdkEn13XOrRHoVgQV877PCBFitRISXOW3hgJYC463UXYUDnUplYnAfm XcRfndfkZAqZtUQkuShDh4POaS3axP7Ef7i+U= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732168483; x=1732773283; 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=w3/doozRmHoESE9I/LoHUzPmNKrwEmnNFS2T9EoQZxg=; b=q9UST39Yo/iHSRM+Lx7TbpDwwgdg0y9uVKwdBFeNv3BbvQPNq3hJyK0r9pQmRkCfrR 0+ftKWTyr4OqrGsR0HvY0NnbyneJfZBpnR7vT1CyIVM/DDwLOT6hlkOPRVplkmYUH5Qu z+ksI7Y4GmmALTwbCjQkpE8JB28D0kQ/y1lAfh/ubxlWsRrGlG2kRxL/rbTxYKGoRpa1 tgilVFaKJi51KCQhoU5/avg5Kn52+uaP038S1xtSHbU9f0RcvxjfZoGZOs5tFksVODbd tT0gItxt6TD3hMU5f3I4tcLV9ifW9KYCxfq/JvCdlij6zK9VKh7KGmTYmwKAoD4kPI8u 5/uA== X-Gm-Message-State: AOJu0YykAB8J44fE4OOS+ZTT9XrEWjKK50QtX5Xr6ydPN1WSv6p6DC7n r92QTHwCwfZQAU2/EK8oARJ1piNseWPP7k1d+muomDq0MUuGrnNKM7DD1N8jHLiugcltlCovkBQ = X-Gm-Gg: ASbGncuZ2tNrpt/w959qIdxnM42PBnMqdzsh38mrpJHjiT2wYE6A/zGvjXTwKVRnC5c fbrGpcQnwDsKuPr05nUv5lvPuEzofqnqZDeAnHnhRWhUYKy3kovPDRAMhStBJeT0gKSLQcf1y0s Roj80heMa0A3l8+h798k7nQkbtWf4DVxSC7r1pF+vtu11x+2S3cyoDXXUdWLBeNGV/GDq8onRzL g4UbWpOASZfQNMVQ8x7q/lZaZisPbOlJqzq0AZYTGK7ejKTfc3gvXbwyMaaY9GsXy43tA1b1UMd 1Fgu9UZNUiP2ZBauKvQ32I7ELvO/HsUEfl2N0o916bwa51VBpJW8RA== X-Google-Smtp-Source: AGHT+IHrABqNNlU2liHLn1y7dxIvbtkYVsvXUhkeQJiUlw+iDCx/pTwX1Mj5osC0zu8CV/8zoW3kRw== X-Received: by 2002:a05:6a00:3a12:b0:71e:5d1d:1aa2 with SMTP id d2e1a72fcca58-724bec9ad56mr6330702b3a.7.1732168482743; Wed, 20 Nov 2024 21:54:42 -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-724bef8da82sm2815815b3a.124.2024.11.20.21.54.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Nov 2024 21:54:42 -0800 (PST) From: Harvey Yang To: libcamera-devel@lists.libcamera.org Cc: Harvey Yang , Han-Lin Chen Subject: [PATCH v3 1/2] DmaBufAllocator: Add Dma Buffer synchronization function & helper class Date: Thu, 21 Nov 2024 05:51:31 +0000 Message-ID: <20241121055436.2502314-2-chenghaoyang@chromium.org> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog In-Reply-To: <20241121055436.2502314-1-chenghaoyang@chromium.org> References: <20241121055436.2502314-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 Reviewed-by: Kieran Bingham --- .../libcamera/internal/dma_buf_allocator.h | 21 ++++++ src/libcamera/dma_buf_allocator.cpp | 75 +++++++++++++++++++ 2 files changed, 96 insertions(+) diff --git a/include/libcamera/internal/dma_buf_allocator.h b/include/libcamera/internal/dma_buf_allocator.h index d2a0a0d19..e4073f668 100644 --- a/include/libcamera/internal/dma_buf_allocator.h +++ b/include/libcamera/internal/dma_buf_allocator.h @@ -8,6 +8,7 @@ #pragma once #include +#include #include namespace libcamera { @@ -35,6 +36,26 @@ private: DmaBufAllocatorFlag type_; }; +class DmaSyncer final +{ +public: + enum class SyncType { + Read = 0, + Write, + ReadWrite, + }; + + explicit DmaSyncer(SharedFD fd, SyncType type = SyncType::ReadWrite); + + ~DmaSyncer(); + +private: + void sync(uint64_t step); + + SharedFD fd_; + uint64_t flags_ = 0; +}; + 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..c1c2103d6 100644 --- a/src/libcamera/dma_buf_allocator.cpp +++ b/src/libcamera/dma_buf_allocator.cpp @@ -205,4 +205,79 @@ 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. + * + * It's used when the user needs to access a dma-buf with CPU, mostly mapped + * with MappedFrameBuffer, so that the buffer is synchronized between CPU and + * ISP. + */ + +/** + * \enum DmaSyncer::SyncType + * \brief Read and/or write access via the CPU map + * \var DmaSyncer::Read + * \brief Indicates that the mapped dma-buf will be read by the client via the + * CPU map + * \var DmaSyncer::Write + * \brief Indicates that the mapped dm-buf will be written by the client via the + * CPU map + * \var DmaSyncer::ReadWrite + * \brief Indicates that the mapped dma-buf will be read and written by the + * client via the CPU map + */ + +/** + * \fn DmaSyncer::DmaSyncer(SharedFD fd, SyncType type = 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 + */ +DmaSyncer::DmaSyncer(SharedFD fd, SyncType type) + : fd_(fd) +{ + switch (type) { + case SyncType::Read: + flags_ = DMA_BUF_SYNC_READ; + break; + case SyncType::Write: + flags_ = DMA_BUF_SYNC_WRITE; + break; + case SyncType::ReadWrite: + flags_ = DMA_BUF_SYNC_RW; + break; + } + + sync(DMA_BUF_SYNC_START); +} + +DmaSyncer::~DmaSyncer() +{ + sync(DMA_BUF_SYNC_END); +} + +void DmaSyncer::sync(uint64_t step) +{ + struct dma_buf_sync sync = { + .flags = flags_ | step + }; + + int ret; + do { + ret = ioctl(fd_.get(), DMA_BUF_IOCTL_SYNC, &sync); + } while (ret && (errno == EINTR || errno == EAGAIN)); + + if (ret) { + ret = errno; + LOG(DmaBufAllocator, Error) + << "Unable to sync dma fd: " << fd_.get() + << ", err: " << strerror(ret) + << ", flags: " << sync.flags; + } +} + } /* namespace libcamera */ From patchwork Thu Nov 21 05:51:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harvey Yang X-Patchwork-Id: 22053 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 B0051C32FB for ; Thu, 21 Nov 2024 05:54:55 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id EA46A65FAA; Thu, 21 Nov 2024 06:54:54 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="bZQNtpja"; dkim-atps=neutral Received: from mail-pg1-x529.google.com (mail-pg1-x529.google.com [IPv6:2607:f8b0:4864:20::529]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id B35D565FA1 for ; Thu, 21 Nov 2024 06:54:46 +0100 (CET) Received: by mail-pg1-x529.google.com with SMTP id 41be03b00d2f7-7f12ba78072so463545a12.2 for ; Wed, 20 Nov 2024 21:54:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1732168485; x=1732773285; 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=36dmZHlgBtkx0ifRAWdxsru0TSO9A+QrGioh6Y9/Qzg=; b=bZQNtpjaORtCxZnMqAa/Z5KYRI28MuQtGNJo4wwPTpizX79PO8O5ALAu2kdaMmrsV0 VQfqcYH+ivRx3HgJEKHd4dFVfkFqqJ/BNnv6YGMr1YIJngJMH+Q/0zzHMcLgmEoz2Mqt KeQ7xDm3wBBi2KCEqLcqs85oNtj5Sj0QR7Zts= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732168485; x=1732773285; 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=36dmZHlgBtkx0ifRAWdxsru0TSO9A+QrGioh6Y9/Qzg=; b=MRlp7u4S8YM6xk1bKdBpczXegBqBleYmAhslHoARaS1BWZgXVAFjK6JoHNbDXoDLcL aSaCLiHxa8NW7NtdGZqBCQHID1j822VEr5pDRYYVpGvjvDakRyaZ0KEFmkOxWJIs57Da hM1bUwjY/9Bq+TL96ANoBKj6RifdnKuB6u1sqSoGlGUHUFFpl3ALbe4q3qcHjT4vz5b3 35Pj7a/SGkwnpaYShr2KG2qZI5rt7a18+8fitSb+iuk2lVTZ3BiL6smW5RBUkrKbK2RX Blnc3Rx+4MS/UVTb6TqvwWxQFuPRQg0XiHBswWdM/YHNufLeX6gsOSfDUGR/ReBSsLXF MYHQ== X-Gm-Message-State: AOJu0YzuQayzfcLOcd50+KpfxycdrrR5NtQwfjEzXRSJbXAyaY1m0+3r UsoT97iCxpq8n7rPgfPX9XG9fpAxjLLaX9EEyrz4pMWmHkMWlMUcuAc3t2l45dq4V1Sd5yUxKew = X-Gm-Gg: ASbGncslVxfeSs6zj2ZFEATEvA6TQpppjPqX9FW1O6oWVfLrWxKPOZ3MFGwQZknFtvB MqJqD2YE/PY22HiAPNq0XBYy7RP33HtPzhoI8WRQsKzW2wfCIqgncpGj2P8ZA+HDybdAxid2vyZ jRF+PL8BMPbDzyFCL0zzeG8DLPvD+Y+wX363J5ctWxlcJTxUcz/lIhfi/f9eX2twKj3tPK3Hz1e hcV5efNm3SzquqUVQO4NjRaFbuOsUIZyfCOBlYaCpkCmXDHEhs+zlxqJK0nQ5A2+HWTGAO1ZobH Qftc2nkDLmiQQDQv8pa+DOde5Lo4Gu6NCalnCVPIHA20iav2IX3mRw== X-Google-Smtp-Source: AGHT+IEck272V8QSYgGaT0IP53mYOxFHxodGWIyvnrurE6XAZ5SQsV2iksO5jDDTV3DrMyBfUPs5wQ== X-Received: by 2002:a05:6a20:7347:b0:1dc:77fc:1cdb with SMTP id adf61e73a8af0-1ddae4e291dmr6824895637.13.1732168484752; Wed, 20 Nov 2024 21:54:44 -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-724bef8da82sm2815815b3a.124.2024.11.20.21.54.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Nov 2024 21:54:43 -0800 (PST) From: Harvey Yang To: libcamera-devel@lists.libcamera.org Cc: Harvey Yang , Milan Zamazal Subject: [PATCH v3 2/2] debayer_cpu: Replace syncing DMABUFs with DmaSyncer Date: Thu, 21 Nov 2024 05:51:32 +0000 Message-ID: <20241121055436.2502314-3-chenghaoyang@chromium.org> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog In-Reply-To: <20241121055436.2502314-1-chenghaoyang@chromium.org> References: <20241121055436.2502314-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 Reviewed-by: Kieran Bingham --- 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..31ab96ab3 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, DmaSyncer::SyncType::Read); + + for (const FrameBuffer::Plane &plane : output->planes()) + dmaSyncers.emplace_back(plane.fd, DmaSyncer::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 &&