{"id":21290,"url":"https://patchwork.libcamera.org/api/1.1/patches/21290/?format=json","web_url":"https://patchwork.libcamera.org/patch/21290/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20240920114915.32728-1-kieran.bingham@ideasonboard.com>","date":"2024-09-20T11:49:15","name":"[v5] libcamera: debayer_cpu: Sync DMABUFs","commit_ref":"cbfe04f77ca6ee46fcb6635294c8dbe9be26c5d1","pull_url":null,"state":"accepted","archived":false,"hash":"d722e39c886446b0c14b7f55febb3ac233ae996f","submitter":{"id":4,"url":"https://patchwork.libcamera.org/api/1.1/people/4/?format=json","name":"Kieran Bingham","email":"kieran.bingham@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/21290/mbox/","series":[{"id":4607,"url":"https://patchwork.libcamera.org/api/1.1/series/4607/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=4607","date":"2024-09-20T11:49:15","name":"[v5] libcamera: debayer_cpu: Sync DMABUFs","version":5,"mbox":"https://patchwork.libcamera.org/series/4607/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/21290/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/21290/checks/","tags":{},"headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 0C9FFC3257\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 20 Sep 2024 11:49:23 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 27A8F634F8;\n\tFri, 20 Sep 2024 13:49:22 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id A64C5634F5\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 20 Sep 2024 13:49:20 +0200 (CEST)","from charm.tail69b4.ts.net\n\t(cpc89244-aztw30-2-0-cust6594.18-1.cable.virginm.net [86.31.185.195])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 4FE094CE;\n\tFri, 20 Sep 2024 13:47:56 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"KZCb00Ce\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1726832876;\n\tbh=DD6ubMVcAVHM/GBRyNXvV+bOC+IpmVvjCmpSd+VUjEI=;\n\th=From:To:Cc:Subject:Date:From;\n\tb=KZCb00CebykeKAsjjRQiFNgkCH36qBuv0hg0VcidYO7atG3AZ2Ol404nAQw1KGoBk\n\tCwzTmNBYJ0xVdsN1lP/Whsst9No7SS1MYgiKzl0xXNceehuwQJrU2msrknn1+BrqhB\n\tkuE0pvYbCUuOh/rmxGsOKloSMyt96U4ybceeadeM=","From":"Kieran Bingham <kieran.bingham@ideasonboard.com>","To":"libcamera devel <libcamera-devel@lists.libcamera.org>","Cc":"Robert Mader <robert.mader@collabora.com>,\n\tMilan Zamazal <mzamazal@redhat.com>,\n\tHans de Goede <hdegoede@redhat.com>, \n\tKieran Bingham <kieran.bingham@ideasonboard.com>","Subject":"[PATCH v5] libcamera: debayer_cpu: Sync DMABUFs","Date":"Fri, 20 Sep 2024 12:49:15 +0100","Message-ID":"<20240920114915.32728-1-kieran.bingham@ideasonboard.com>","X-Mailer":"git-send-email 2.46.0","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"From: Robert Mader <robert.mader@collabora.com>\n\nUsing `DMA_BUF_IOCTL_SYNC` is required for DMABUFs in order to ensure\ncorrect output. Not doing so currently results in occasional tearing\nand/or backlashes in GL/VK clients that use the buffers directly for\nrendering.\n\nAn alternative approach to have the sync code in `MappedFrameBuffer` was\nconsidered but rejected for now, in order to allow clients more\nflexibility.\n\nWhile the new helper is added to an annoymous namespace, add\ntimeDiff to the same namespace and remove the static definition as a\ndrive by.\n\nSigned-off-by: Robert Mader <robert.mader@collabora.com>\nTested-by: Milan Zamazal <mzamazal@redhat.com> # Debix\nTested-by: Hans de Goede <hdegoede@redhat.com> # IPU6 + ov2740\nTested-by: Kieran Bingham <kieran.bingham@ideasonboard.com> # Lenovo X13s + OV5675\nReviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\nSigned-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n\n---\nv5: Kieran:\n - Remove static\n - Fix ret negation that I suggested incorrectly.\n\n src/libcamera/software_isp/debayer_cpu.cpp | 32 +++++++++++++++++++++-\n 1 file changed, 31 insertions(+), 1 deletion(-)","diff":"diff --git a/src/libcamera/software_isp/debayer_cpu.cpp b/src/libcamera/software_isp/debayer_cpu.cpp\nindex 077f7f4bc45b..8a757fe9e02d 100644\n--- a/src/libcamera/software_isp/debayer_cpu.cpp\n+++ b/src/libcamera/software_isp/debayer_cpu.cpp\n@@ -12,8 +12,11 @@\n #include \"debayer_cpu.h\"\n \n #include <stdlib.h>\n+#include <sys/ioctl.h>\n #include <time.h>\n \n+#include <linux/dma-buf.h>\n+\n #include <libcamera/formats.h>\n \n #include \"libcamera/internal/bayer_format.h\"\n@@ -718,12 +721,33 @@ void DebayerCpu::process4(const uint8_t *src, uint8_t *dst)\n \t}\n }\n \n-static inline int64_t timeDiff(timespec &after, timespec &before)\n+namespace {\n+\n+void syncBufferForCPU(FrameBuffer *buffer, uint64_t syncFlags)\n+{\n+\tfor (const FrameBuffer::Plane &plane : buffer->planes()) {\n+\t\tconst int fd = plane.fd.get();\n+\t\tstruct dma_buf_sync sync = { syncFlags };\n+\t\tint ret;\n+\n+\t\tret = ioctl(fd, DMA_BUF_IOCTL_SYNC, &sync);\n+\t\tif (ret < 0) {\n+\t\t\tret = errno;\n+\t\t\tLOG(Debayer, Error)\n+\t\t\t\t<< \"Syncing buffer FD \" << fd << \" with flags \"\n+\t\t\t\t<< syncFlags << \" failed: \" << strerror(ret);\n+\t\t}\n+\t}\n+}\n+\n+inline int64_t timeDiff(timespec &after, timespec &before)\n {\n \treturn (after.tv_sec - before.tv_sec) * 1000000000LL +\n \t       (int64_t)after.tv_nsec - (int64_t)before.tv_nsec;\n }\n \n+} /* namespace */\n+\n void DebayerCpu::process(FrameBuffer *input, FrameBuffer *output, DebayerParams params)\n {\n \ttimespec frameStartTime;\n@@ -733,6 +757,9 @@ void DebayerCpu::process(FrameBuffer *input, FrameBuffer *output, DebayerParams\n \t\tclock_gettime(CLOCK_MONOTONIC_RAW, &frameStartTime);\n \t}\n \n+\tsyncBufferForCPU(input, DMA_BUF_SYNC_START | DMA_BUF_SYNC_READ);\n+\tsyncBufferForCPU(output, DMA_BUF_SYNC_START | DMA_BUF_SYNC_WRITE);\n+\n \tgreen_ = params.green;\n \tred_ = swapRedBlueGains_ ? params.blue : params.red;\n \tblue_ = swapRedBlueGains_ ? params.red : params.blue;\n@@ -760,6 +787,9 @@ void DebayerCpu::process(FrameBuffer *input, FrameBuffer *output, DebayerParams\n \n \tmetadata.planes()[0].bytesused = out.planes()[0].size();\n \n+\tsyncBufferForCPU(output, DMA_BUF_SYNC_END | DMA_BUF_SYNC_WRITE);\n+\tsyncBufferForCPU(input, DMA_BUF_SYNC_END | DMA_BUF_SYNC_READ);\n+\n \t/* Measure before emitting signals */\n \tif (measuredFrames_ < DebayerCpu::kLastFrameToMeasure &&\n \t    ++measuredFrames_ > DebayerCpu::kFramesToSkip) {\n","prefixes":["v5"]}