From patchwork Thu May 21 15:59:06 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Robert Mader X-Patchwork-Id: 26795 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 A6F65BDCBD for ; Thu, 21 May 2026 15:59:37 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 57EBF6302F; Thu, 21 May 2026 17:59:37 +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="i2n1OJJ1"; 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 0741D6301A for ; Thu, 21 May 2026 17:59:31 +0200 (CEST) ARC-Seal: i=1; a=rsa-sha256; t=1779379168; cv=none; d=zohomail.com; s=zohoarc; b=FC+079oULQk0ZhXGvGlKHtzcNi3mWWYzLOEEs/qCUbVSfT6OC0U1FMCM4KOG0cDBO1rSwtHAWBJ4vDpHgUAcyYzKF1D6G5A9gDhi1NcxYz1SwXYG2l9TngiKUVbre6P74uw6cgF9lciW1KXlAyeiQSGye8CAyTTtbBFKD9jvo2w= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1779379168; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:Subject:To:To:Message-Id:Reply-To; bh=3ZVdjT7etSZl8Jh8qHUgc71mWn4BhMzSBY4rQpI1ORw=; b=Wo/11MOLvGvjmAgOvAWvnOSWDhcIDhBTEoWCM3NPwgv6a1H3JPR0IhF2GJJpNKs3OplWgMOYBCqp/cv43eCHo9hPVh63ArYwZ77Iw7IsKA8Qx/W3JVqesLd2johoRa3AbhAyXIBSnV1HlIpp/VJUDVETKqoRXCyNEvvtVuAzM90= 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=1779379168; s=zohomail; d=collabora.com; i=robert.mader@collabora.com; h=From:From:To:To:Cc:Cc:Subject:Subject:Date:Date:Message-ID:In-Reply-To:References:MIME-Version:Content-Transfer-Encoding:Message-Id:Reply-To; bh=3ZVdjT7etSZl8Jh8qHUgc71mWn4BhMzSBY4rQpI1ORw=; b=i2n1OJJ1DwjGxnTIr/8sth3Cokr2m6bVM/eljng/gC78Pe2PYAdWoUjuKBuAYHCx 9VCDhXHhwrRVO3rRe/S8l2IFM7gkNgFlrbFnMODknEIP8okNNvv50S2pbO/BQAJN734 lxkpQBCABXagKhkzGb9gq8PJX3/4oTqCFZOtgKxg= Received: by mx.zohomail.com with SMTPS id 1779379166773721.2561804678149; Thu, 21 May 2026 08:59:26 -0700 (PDT) From: Robert Mader To: libcamera-devel@lists.libcamera.org Cc: Robert Mader Subject: [PATCH v4 5/5] debayer_egl: Implement dmabuf import for input buffers Date: Thu, 21 May 2026 17:59:06 +0200 Message-ID: <20260521155906.120373-6-robert.mader@collabora.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260521155906.120373-1-robert.mader@collabora.com> References: <20260521155906.120373-1-robert.mader@collabora.com> 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" In many cases we can import the GPU-ISP input buffers, dmabufs from v4l2, directly into EGL instead of mapping and uploading - i.e. copying - them. Doing so can have positive effects in multiple areas, including reducing memory bandwidth and CPU usage, as well as avoiding expensive dmabuf syncs and syscalls. The main reason direct imports may not work are the more demanding stride alignment requirements many GPUs have - often 128 or 256 bytes - compared to ISPs - apparently often closer to 32 bytes. Thus we first try to import buffers directly and - if that fails - fall back to the previous upload path. Failing imports should come at low cost as drivers know the limitations and can bail out early, without causing additional IO or context switches. In the future we might be able to request buffers with a matching stride from v4l2 drivers in many cases, making direct import the norm instead of a hit-or-miss. An optional kernel API for that exists, but doesn't seem to be implemented by any driver tested so far. Note that passing around MappedFrameBuffer and DmaSyncer variables ensures we don't do unnecessary mappings and dmabuf syncs. Below are some benchmark results. All where done using postmarketOS edge with updates from 21th May 2026 (Mesa 26.1.1). The mentioned pipelines where run five times each, with the mean value included here, which should be quite representive as the variance was rather small. All devices where using the powersave governor. - FairPhone 5 -- Back camera cam -c /base/soc@0/cci@ac4a000/i2c-bus@1/camera@29 -s width=1920,height=1080 --capture=60 Before: 14027 us/frame After: 12122 us/frame - OnePlus 6 -- Back camera (imx519) cam -c /base/soc@0/cci@ac4a000/i2c-bus@0/camera@10 -s width=1920,height=1080 --capture=60 Before: 30091 us/frame After: 19878 us/frame - Librem 5 -- Back camera cam -c /base/soc@0/bus@30800000/i2c@30a50000/camera@2d -s width=1280,height=720 --capture=60 Before: 69092 us/frame After: 41250 us/frame - PinePhone -- Front Camera cam -c /base/i2c-csi/front-camera@3c -s width=1280,height=720 --capture=60 Before: 173769 us/frame After: 143274 us/frame -- Back camera cam -c /base/i2c-csi/rear-camera@4c -s width=1280,height=720 --capture=60 Before: 174833 us/frame After: 144476 us/frame There is one case where performance regresses: - Pixel 3a -- Back camera cam -c /base/soc@0/cci@ac4a000/i2c-bus@1/camera@1a -s width=1920,height=1080 --capture=60 Before: 14257 us/frame After: 15161 us/frame To my knowledge this is likely caused by bad sampling performance from linear buffers. IMO this is a driver issue - if a copy to tiled format makes sampling faster, drivers should do so implicitly (like e.g. v3d already does). Signed-off-by: Robert Mader --- src/libcamera/software_isp/debayer_egl.cpp | 45 ++++++++++++++-------- src/libcamera/software_isp/debayer_egl.h | 2 +- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/src/libcamera/software_isp/debayer_egl.cpp b/src/libcamera/software_isp/debayer_egl.cpp index d15f3cd48..c1075ac39 100644 --- a/src/libcamera/software_isp/debayer_egl.cpp +++ b/src/libcamera/software_isp/debayer_egl.cpp @@ -500,16 +500,30 @@ void DebayerEGL::setShaderVariableValues(const DebayerParams ¶ms) return; } -int DebayerEGL::debayerGPU(MappedFrameBuffer &in, int out_fd, const DebayerParams ¶ms) +int DebayerEGL::debayerGPU(FrameBuffer *input, FrameBuffer *output, const DebayerParams ¶ms, std::optional *inMapped, std::optional *inDmaSyncer) { /* eGL context switch */ egl_.makeCurrent(); - /* Create a standard texture input */ - egl_.createTexture2D(*eglImageBayerIn_, in.planes()[0].data()); + /* Try to create texture for input buffer via dmabuf import */ + if (!eglImageBayerIn_->dmabuf_import_failed_) { + if (egl_.createInputDMABufTexture2D(*eglImageBayerIn_, input->planes()[0].fd.get()) != 0) + LOG(Debayer, Info) << "Importing input buffer with DMABuf import failed, falling back to upload"; + } + + /* Otherwise create texture for input buffer via upload from CPU */ + if (eglImageBayerIn_->dmabuf_import_failed_) { + inMapped->emplace(input, MappedFrameBuffer::MapFlag::Read); + inDmaSyncer->emplace(input->planes()[0].fd, DmaSyncer::SyncType::Read); + if (!inMapped->value().isValid()) { + LOG(Debayer, Error) << "mmap-ing buffer(s) failed"; + return -ENODEV; + } + egl_.createTexture2D(*eglImageBayerIn_, inMapped->value().planes()[0].data()); + } /* Generate the output render framebuffer as render to texture */ - egl_.createOutputDMABufTexture2D(*eglImageBayerOut_, out_fd); + egl_.createOutputDMABufTexture2D(*eglImageBayerOut_, output->planes()[0].fd.get()); setShaderVariableValues(params); glViewport(0, 0, width_, height_); @@ -531,23 +545,16 @@ void DebayerEGL::process(uint32_t frame, FrameBuffer *input, FrameBuffer *output { bench_.startFrame(); - std::vector dmaSyncers; - - dmaSyncBegin(dmaSyncers, input, nullptr); - /* Copy metadata from the input buffer */ FrameMetadata &metadata = output->_d()->metadata(); metadata.status = input->metadata().status; metadata.sequence = input->metadata().sequence; metadata.timestamp = input->metadata().timestamp; - MappedFrameBuffer in(input, MappedFrameBuffer::MapFlag::Read); - if (!in.isValid()) { - LOG(Debayer, Error) << "mmap-ing buffer(s) failed"; - goto error; - } + std::optional inMapped; + std::optional inDmaSyncer; - if (debayerGPU(in, output->planes()[0].fd.get(), params)) { + if (debayerGPU(input, output, params, &inMapped, &inDmaSyncer)) { LOG(Debayer, Error) << "debayerGPU failed"; goto error; } @@ -555,8 +562,14 @@ void DebayerEGL::process(uint32_t frame, FrameBuffer *input, FrameBuffer *output metadata.planes()[0].bytesused = output->planes()[0].length; /* Calculate stats for the whole frame */ - stats_->processFrame(frame, 0, &in); - dmaSyncers.clear(); + if ((frame % SwStatsCpu::kStatPerNumFrames) == 0) { + if (!inMapped) + inMapped.emplace(input, MappedFrameBuffer::MapFlag::Read); + if (!inDmaSyncer) + inDmaSyncer.emplace(input->planes()[0].fd, DmaSyncer::SyncType::Read); + } + stats_->processFrame(frame, 0, inMapped ? &inMapped.value() : nullptr); + inDmaSyncer.reset(); egl_.syncOutput(); bench_.finishFrame(); diff --git a/src/libcamera/software_isp/debayer_egl.h b/src/libcamera/software_isp/debayer_egl.h index 141fb288f..875e7cfc5 100644 --- a/src/libcamera/software_isp/debayer_egl.h +++ b/src/libcamera/software_isp/debayer_egl.h @@ -65,7 +65,7 @@ private: int initBayerShaders(PixelFormat inputFormat, PixelFormat outputFormat); int getShaderVariableLocations(); void setShaderVariableValues(const DebayerParams ¶ms); - int debayerGPU(MappedFrameBuffer &in, int out_fd, const DebayerParams ¶ms); + int debayerGPU(FrameBuffer *input, FrameBuffer *output, const DebayerParams ¶ms, std::optional *mappedInputBuffer, std::optional *inputBufferDmaSyncer); /* Shader program identifiers */ GLuint vertexShaderId_ = 0;