From patchwork Tue Mar 3 11:17:40 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hans de Goede X-Patchwork-Id: 26246 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 8A662C32EA for ; Tue, 3 Mar 2026 11:17:56 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id BD928623A2; Tue, 3 Mar 2026 12:17:55 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=qualcomm.com header.i=@qualcomm.com header.b="pJXKCwHp"; dkim=pass (2048-bit key; unprotected) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.b="LwpMicCV"; dkim-atps=neutral Received: from mx0a-0031df01.pphosted.com (mx0a-0031df01.pphosted.com [205.220.168.131]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 3547662395 for ; Tue, 3 Mar 2026 12:17:52 +0100 (CET) Received: from pps.filterd (m0279867.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 6239moKV2733596 for ; Tue, 3 Mar 2026 11:17:50 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qualcomm.com; h= cc:content-transfer-encoding:date:from:in-reply-to:message-id :mime-version:references:subject:to; s=qcppdkim1; bh=61zLKSb3u/2 Bf+JVWEk65x3S2OYd22ZVRPj0PFmBxuE=; b=pJXKCwHpTsuNuQtSW3Asgq/Fxm2 NkHLpg83OQp0NZg78EqVzjaBUA3i7ucq7DSLjaE1eZr5E/opZK88iL3IFKMKtjQU j7pBBhIZCwfiGnvrwiub/ShVhfudTwb0Ot1Cynjex3WcUZJL62Z/2v3XubaUfa39 0sf0JVyab1DPfO6yV5+fE/zgqAJS+SnOt6o1Avd5x2dISvuQnmjbVWcK6wuDFt7/ AM4R+VCIni7f3TO6WiOoRnBFtqjAdwAuAqlqf3HhNh/LNKDuCM1qbpsnvl7t2Arr CkW9IDk4xZkpuw7TDRjLkUtkX7djUrW/8PYHQTUrV5hOkpSCvakCSqzY9KA== Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 4cnswe129k-1 (version=TLSv1.3 cipher=TLS_AES_128_GCM_SHA256 bits=128 verify=NOT) for ; Tue, 03 Mar 2026 11:17:50 +0000 (GMT) Received: by mail-qk1-f199.google.com with SMTP id af79cd13be357-8cb3ff05c73so3645742785a.0 for ; Tue, 03 Mar 2026 03:17:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oss.qualcomm.com; s=google; t=1772536669; x=1773141469; 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=61zLKSb3u/2Bf+JVWEk65x3S2OYd22ZVRPj0PFmBxuE=; b=LwpMicCVH4M4BviWWDbSjQU9X9Z3epjOdgkPChjhgY4jg73zp8gJllJnta0d+SFuiG BEkRxPTOI6xkHft94P3wy0MJR9kcekXq/pi2oodew1sMPWibeLTUiSFZQzxQ2BxUSUQV Y7peqIxqfQ0Uw+xr76GCF4fqNGQaIfrGS1zWiZHx4Z/Pssx8TB2ehDuAkA4OzCMVKKWn Vwt5KrgJmHwB5zy3D+KZbMGz0qbiwcSPW4pmgNwNj15hseOicHe4/bo78wonrFMpNA+f P8U5c2gTT+Wx5jHLZYmp/SUzyQyxYjSTaqgfWR6HvlRgO82CgtcPd/a6/UUnSQiswNai Yu0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772536669; x=1773141469; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=61zLKSb3u/2Bf+JVWEk65x3S2OYd22ZVRPj0PFmBxuE=; b=KT8/mhAU4EIKqMjn0nd/OdD2AT2rwZu+b3SUXX2iFpn2ytJnHDDsNSpc7UpcUUnbnP t/gedDMveij3dH90lH6B0Pk9M69886rpGhqE5MniYX2uAsDVqpddWX/ZtKaXLU4lh1k4 DlzPdesDVFyLbvqtnL2WW5XPWO/uRXksL/jLNWEPiK+Udh9BKCpsm8t3ZcUtlXUQ4Gxg Hvrv5XVlaCdc4MVbP04z//GwMkbnTJaq8uYQUXdfujZ6+qMLZZjAqphVFjPuFcjolv4U oqcsmsFtkavjdmssZ9iG+JB+CKtvXWqtRXT8AuEtwLL9jnvRwwWWC91iH52w/XVtlhLQ 9b9A== X-Gm-Message-State: AOJu0Yx0+Wph8b8CoPczeMosApoElvibolqXyNdvHuLgSs5sYB3dcXsR 3N8eDrCJfZ7IdrrsArRFesKib2xGm4Z8/bT3bhCCD9X67jCAKB8vJVrB/P8W+Y96zO5zDjuD5vV TVa3tjjDA2wn7pBT8KXpyPKuePl0152rRzLtr4viEyFp0U1QKx8LtDdpXEK2XGZD0vlrnOEF1SX WN17wyfuZA X-Gm-Gg: ATEYQzyIzHe+ilRDMIIjtIWAzXMbzJ+Zof6B7wI3yi0cn1eNRWflKLs0BsjvohRhDoy DqDGP1QTBNe84dSSSpoPUyTszAGyWuE58GMY2ALmCir/Ci/oAC4P0fNo9hznQTg1smPxmlCXZrt zB1z6aMiOPVocoqopu4ZM/PtKcNDeOtIWqqDt/q1lt/Gl731Sm8i40m9WjczFCLgw9TvzXwyYY+ ANXt56AwWc4N3LMyOjuQKngN0gW8qfZON0waiymonogjJNWaxw0DUBoh48Y6tevun8X0Bu6V3Sc bTkqIDMt2HNtRAVx97GFETxuITFzambnmTkr48LpGUIJzfbXzoD60lF6MiSLcQjv/StXsZlv+Pl I0FT9r5gVs73qZtpKZuMGLruIwl+sR5KsJvisOeuX0oEiXKnpu01AofLdheuBl/vuRbK4FfR2qo IOglny79Ep9dx8iAOB9JGAZxk8aa8GwrANSQ== X-Received: by 2002:a05:620a:25c7:b0:8ca:3c67:891d with SMTP id af79cd13be357-8cbc8f1af5emr1859077585a.54.1772536669088; Tue, 03 Mar 2026 03:17:49 -0800 (PST) X-Received: by 2002:a05:620a:25c7:b0:8ca:3c67:891d with SMTP id af79cd13be357-8cbc8f1af5emr1859074685a.54.1772536668537; Tue, 03 Mar 2026 03:17:48 -0800 (PST) Received: from t14s (2001-1c00-0c32-7800-beb3-9058-f5fe-3f2e.cable.dynamic.v6.ziggo.nl. [2001:1c00:c32:7800:beb3:9058:f5fe:3f2e]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-66003479178sm2958144a12.9.2026.03.03.03.17.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Mar 2026 03:17:47 -0800 (PST) From: Hans de Goede To: libcamera-devel@lists.libcamera.org Cc: Milan Zamazal , Hans de Goede Subject: [PATCH v4 3/4] software_isp: debayer_cpu: Add multi-threading support Date: Tue, 3 Mar 2026 12:17:40 +0100 Message-ID: <20260303111741.17417-4-johannes.goede@oss.qualcomm.com> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20260303111741.17417-1-johannes.goede@oss.qualcomm.com> References: <20260303111741.17417-1-johannes.goede@oss.qualcomm.com> MIME-Version: 1.0 X-Proofpoint-ORIG-GUID: WPnRoZim_gyi0BujCbYdFpjfD0LbFb00 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwMzAzMDA4NiBTYWx0ZWRfX94nF592oVPcr 2p7Ue/HOZiimTJFor5DZYX2GA962AQ+46pwssysULQrblnTjUbhcNsBJ07RWQ2lX6lOSZiMNiRP tAz5a7rF4k7IBtTVbmhw09UiPQ7KDwXH1nev+hEFx6kVeapNoC+4Ki4nFKwjovCKF7/JgO1ZVeA WG8zIy+jGLrh/kbta2o77K2LtuPJ12OlUlFQlsmR4sMkFzHSJj7TlsRen/QPbI+XTH4M6qYBPjd L2YCA7Y55NRuuSarKBXNJxXodZCpI2uy3K/fzvBnmMc/OY1V/XgS1dbK/bepHp/Q6JR6rJxIE/B 2HrBncoSOIuCih27EPdF001CIuL/2sDWIWNhlL/FpL7CYS3/W/3DC/vbR5PlR3O5fsYXiafV7Ss rQhDBIdmcGvMVp4RnZumQkBTz3eHSV1LR8pThcRVuVRZXEMPo9FJi5S3OqnNKvHPKiXndqLtz0v uo7oUikJhGGYyLSseMg== X-Authority-Analysis: v=2.4 cv=TtHrRTXh c=1 sm=1 tr=0 ts=69a6c35e cx=c_pps a=HLyN3IcIa5EE8TELMZ618Q==:117 a=xqWC_Br6kY4A:10 a=Yq5XynenixoA:10 a=s4-Qcg_JpJYA:10 a=VkNPw1HP01LnGYTKEx00:22 a=u7WPNUs3qKkmUXheDGA7:22 a=eoimf2acIAo5FJnRuUoq:22 a=EUspDBNiAAAA:8 a=wcwPUnDRDUmv0zfwghEA:9 a=bTQJ7kPSJx9SKPbeHEYW:22 X-Proofpoint-GUID: WPnRoZim_gyi0BujCbYdFpjfD0LbFb00 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293, Aquarius:18.0.1121, Hydra:6.1.51, FMLib:17.12.100.49 definitions=2026-03-02_05,2026-03-03_01,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 lowpriorityscore=0 malwarescore=0 phishscore=0 suspectscore=0 adultscore=0 priorityscore=1501 spamscore=0 impostorscore=0 bulkscore=0 clxscore=1015 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2602130000 definitions=main-2603030086 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" Add CPU soft ISP multi-threading support. Benchmark results for the Arduino Uno-Q with a weak CPU which is good for performance testing, all numbers with an IMX219 running at 3280x2464 -> 3272x2464: 1 thread : 147ms / frame, ~6.5 fps 2 threads: 80ms / frame, ~12.5 fps 3 threads: 65ms / frame, ~15 fps Adding a 4th thread does not improve performance. Signed-off-by: Hans de Goede --- Changes in v4: - Document "software_isp", "threads" option in runtime_configuration.rst - Add an use constants for min/max/default number of threads Changes in v3: - Adjust for DebayerCpuThread now inheriting from Thread - Use for (auto &thread : threads_) Changes in v2: - Adjust to use the new DebayerCpuThread class introduced in the v2 patch-series - Re-use threads instead of starting new threads every frame --- Documentation/runtime_configuration.rst | 1 + src/libcamera/software_isp/debayer_cpu.cpp | 45 ++++++++++++++++++++-- src/libcamera/software_isp/debayer_cpu.h | 10 +++++ 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/Documentation/runtime_configuration.rst b/Documentation/runtime_configuration.rst index e99ef2fb9..07d1a9123 100644 --- a/Documentation/runtime_configuration.rst +++ b/Documentation/runtime_configuration.rst @@ -51,6 +51,7 @@ file structure: measure: skip: # non-negative integer, frames to skip initially number: # non-negative integer, frames to measure + threads: # integer >= 1, number of render threads to use, default 2 Configuration file example -------------------------- diff --git a/src/libcamera/software_isp/debayer_cpu.cpp b/src/libcamera/software_isp/debayer_cpu.cpp index d57d640df..f7c3e1751 100644 --- a/src/libcamera/software_isp/debayer_cpu.cpp +++ b/src/libcamera/software_isp/debayer_cpu.cpp @@ -76,6 +76,7 @@ DebayerCpuThread::DebayerCpuThread(DebayerCpu *debayer, unsigned int threadIndex debayer_(debayer), threadIndex_(threadIndex), enableInputMemcpy_(enableInputMemcpy) { + moveToThread(this); } /** @@ -107,8 +108,10 @@ DebayerCpu::DebayerCpu(std::unique_ptr stats, const GlobalConfigurat bool enableInputMemcpy = configuration.option({ "software_isp", "copy_input_buffer" }).value_or(true); - /* Just one thread object for now, which will be called inline rather than async */ - threads_.resize(1); + unsigned int threadCount = + configuration.option({ "software_isp", "threads" }).value_or(kDefaultThreads); + threadCount = std::clamp(threadCount, kMinThreads, kMaxThreads); + threads_.resize(threadCount); for (unsigned int i = 0; i < threads_.size(); i++) threads_[i] = std::make_unique(this, i, enableInputMemcpy); @@ -746,6 +749,11 @@ void DebayerCpuThread::process(uint32_t frame, const uint8_t *src, uint8_t *dst) process2(frame, src, dst); else process4(frame, src, dst); + + debayer_->workPendingMutex_.lock(); + debayer_->workPending_ &= ~(1 << threadIndex_); + debayer_->workPendingMutex_.unlock(); + debayer_->workPendingCv_.notify_one(); } void DebayerCpuThread::process2(uint32_t frame, const uint8_t *src, uint8_t *dst) @@ -985,7 +993,21 @@ void DebayerCpu::process(uint32_t frame, FrameBuffer *input, FrameBuffer *output stats_->startFrame(frame); - threads_[0]->process(frame, in.planes()[0].data(), out.planes()[0].data()); + workPendingMutex_.lock(); + workPending_ = (1 << threads_.size()) - 1; + workPendingMutex_.unlock(); + + for (auto &thread : threads_) + thread->invokeMethod(&DebayerCpuThread::process, + ConnectionTypeQueued, frame, + in.planes()[0].data(), out.planes()[0].data()); + + { + MutexLocker locker(workPendingMutex_); + workPendingCv_.wait(locker, [&]() LIBCAMERA_TSA_REQUIRES(workPendingMutex_) { + return workPending_ == 0; + }); + } metadata.planes()[0].bytesused = out.planes()[0].size(); @@ -1004,6 +1026,23 @@ void DebayerCpu::process(uint32_t frame, FrameBuffer *input, FrameBuffer *output inputBufferReady.emit(input); } +int DebayerCpu::start() +{ + for (auto &thread : threads_) + thread->start(); + + return 0; +} + +void DebayerCpu::stop() +{ + for (auto &thread : threads_) + thread->exit(); + + for (auto &thread : threads_) + thread->wait(); +} + SizeRange DebayerCpu::sizes(PixelFormat inputFormat, const Size &inputSize) { Size patternSize = this->patternSize(inputFormat); diff --git a/src/libcamera/software_isp/debayer_cpu.h b/src/libcamera/software_isp/debayer_cpu.h index 780576090..11280c0a1 100644 --- a/src/libcamera/software_isp/debayer_cpu.h +++ b/src/libcamera/software_isp/debayer_cpu.h @@ -16,6 +16,7 @@ #include #include +#include #include "libcamera/internal/bayer_format.h" #include "libcamera/internal/global_configuration.h" @@ -41,6 +42,8 @@ public: std::tuple strideAndFrameSize(const PixelFormat &outputFormat, const Size &size); void process(uint32_t frame, FrameBuffer *input, FrameBuffer *output, const DebayerParams ¶ms); + int start(); + void stop(); SizeRange sizes(PixelFormat inputFormat, const Size &inputSize); const SharedFD &getStatsFD() { return stats_->getStatsFD(); } @@ -144,6 +147,13 @@ private: std::unique_ptr stats_; unsigned int xShift_; /* Offset of 0/1 applied to window_.x */ + static constexpr unsigned int kMinThreads = 1; + static constexpr unsigned int kMaxThreads = 8; + static constexpr unsigned int kDefaultThreads = 2; + + unsigned int workPending_ LIBCAMERA_TSA_GUARDED_BY(workPendingMutex_); + Mutex workPendingMutex_; + ConditionVariable workPendingCv_; std::vector>threads_; };