From patchwork Mon Sep 16 16:12:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Robert Mader X-Patchwork-Id: 21286 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 87906C324C for ; Mon, 16 Sep 2024 16:13:03 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 498DF634FD; Mon, 16 Sep 2024 18:13:02 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=collabora.com header.i=robert.mader@collabora.com header.b="fsfrA7eu"; dkim-atps=neutral Received: from sender4-pp-f112.zoho.com (sender4-pp-f112.zoho.com [136.143.188.112]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id C28DF634F5 for ; Mon, 16 Sep 2024 18:13:00 +0200 (CEST) ARC-Seal: i=1; a=rsa-sha256; t=1726503177; cv=none; d=zohomail.com; s=zohoarc; b=fGQSv8WNWytUemO9HNtxFZcmfSVicspoastwhdCepZ0gMQ9eplrPo8gXFCkJS813yrZ3xzgDyGONCfu8DxhunMakv9OGAzgj6lKrtwqG7Jqh8BCQqLL0FpjfpXK88ihe4doq8Gf108axujVpE7S5+8fn5nmWMIYh4oAyQ0hi6tg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1726503177; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:MIME-Version:Message-ID:Subject:Subject:To:To:Message-Id:Reply-To; bh=GGzkvg0O4b2dghmpBel4ye0Wgb0a4n0UwUb7oLqjiy4=; b=C6xKhajOeavlYXDOsyAoF9GJUtBiyLo8K3WYaHnrMXGrlM4LbkHiSX2NxeaIH75vy9u+Wi7HaS262StzzpVIL0oY92cMX564D/GX/Bwcoa3r/H8vkBZu3gp+UaJRv56suUZmvLQrCUUeF64yu7rIH2b0tDOokWlPpVwbj3/J2Fc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=collabora.com; spf=pass smtp.mailfrom=robert.mader@collabora.com; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1726503177; s=zohomail; d=collabora.com; i=robert.mader@collabora.com; h=From:From:To:To:Cc:Cc:Subject:Subject:Date:Date:Message-ID:MIME-Version:Content-Transfer-Encoding:Message-Id:Reply-To; bh=GGzkvg0O4b2dghmpBel4ye0Wgb0a4n0UwUb7oLqjiy4=; b=fsfrA7eu67Dj2jbsot2TV0T5HtTDM1JhAQo20lWJv3CTzBO3bCJv3540HaLWmzEy KjiObb2U+tCCbVdkDV3EraKvdeGKCFIBtww/8KjfmJgDszfSTpqGQR+5ntSEdWbcSdI lh4tjj4fVi9/kYWs5CCWtJgRpl/4YSh58VriaGro= Received: by mx.zohomail.com with SMTPS id 1726503175338244.25185611963502; Mon, 16 Sep 2024 09:12:55 -0700 (PDT) From: Robert Mader To: libcamera-devel@lists.libcamera.org Cc: Robert Mader Subject: [PATCH v4] libcamera: debayer_cpu: Sync DMABUFs Date: Mon, 16 Sep 2024 18:12:31 +0200 Message-ID: <20240916161231.238116-1-robert.mader@collabora.com> X-Mailer: git-send-email 2.46.0 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" Using `DMA_BUF_IOCTL_SYNC` is required for DMABUFs in order to ensure correct output. Not doing so currently results in occasional tearing and/or backlashes in GL/VK clients that use the buffers directly for rendering. An alternative approach to have the sync code in `MappedFrameBuffer` was considered but rejected for now, in order to allow clients more flexibility. Signed-off-by: Robert Mader Tested-by: Hans de Goede # IPU6 + ov2740 Tested-by: Kieran Bingham # Lenovo X13s + OV5675 Reviewed-by: Kieran Bingham --- Changes in v4: - Fixed errno handling - Added sync flags to error message - Wrapped syncBufferForCPU() and timeDiff() into an anonymous namespace Changes in v3: - add syncBufferForCPU() helper function Changes in v2: - sync input buffer as well - update commit title accordingly - small changes to the commit message - linting fixes, should pass now --- src/libcamera/software_isp/debayer_cpu.cpp | 30 ++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/libcamera/software_isp/debayer_cpu.cpp b/src/libcamera/software_isp/debayer_cpu.cpp index 077f7f4b..96cf2c71 100644 --- a/src/libcamera/software_isp/debayer_cpu.cpp +++ b/src/libcamera/software_isp/debayer_cpu.cpp @@ -12,8 +12,11 @@ #include "debayer_cpu.h" #include +#include #include +#include + #include #include "libcamera/internal/bayer_format.h" @@ -718,12 +721,33 @@ void DebayerCpu::process4(const uint8_t *src, uint8_t *dst) } } +namespace { + +static 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); + } + } +} + static inline int64_t timeDiff(timespec &after, timespec &before) { return (after.tv_sec - before.tv_sec) * 1000000000LL + (int64_t)after.tv_nsec - (int64_t)before.tv_nsec; } +} /* namespace */ + void DebayerCpu::process(FrameBuffer *input, FrameBuffer *output, DebayerParams params) { timespec frameStartTime; @@ -733,6 +757,9 @@ void DebayerCpu::process(FrameBuffer *input, FrameBuffer *output, DebayerParams 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); + green_ = params.green; red_ = swapRedBlueGains_ ? params.blue : params.red; blue_ = swapRedBlueGains_ ? params.red : params.blue; @@ -760,6 +787,9 @@ void DebayerCpu::process(FrameBuffer *input, FrameBuffer *output, DebayerParams 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); + /* Measure before emitting signals */ if (measuredFrames_ < DebayerCpu::kLastFrameToMeasure && ++measuredFrames_ > DebayerCpu::kFramesToSkip) {