From patchwork Mon Jul 6 11:01:20 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bryan O'Donoghue X-Patchwork-Id: 27244 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 13A9FC3318 for ; Mon, 6 Jul 2026 11:01:42 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id AB07F66049; Mon, 6 Jul 2026 13:01:41 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="wxxhdkvD"; dkim-atps=neutral Received: from mail-wm1-x32c.google.com (mail-wm1-x32c.google.com [IPv6:2a00:1450:4864:20::32c]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id A965F6601E for ; Mon, 6 Jul 2026 13:01:37 +0200 (CEST) Received: by mail-wm1-x32c.google.com with SMTP id 5b1f17b1804b1-493d3135f62so9214475e9.2 for ; Mon, 06 Jul 2026 04:01:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1783335697; x=1783940497; 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=iBp5CTAibDRDjJr1C9jbaznxXxt4zBdSgPR4F1nm75g=; b=wxxhdkvDyjBC41/rmAuJXR9g18jaCVLvuuVSUUqWD0Hc/I2Sd6dL2uFjLhpKvT6ahM SsdpCuvH6pqsLH3glMbnMSvQ9H7AyvvsMkXt5M42AtdARHOolXtyIodt2/Ztx0R4uYFt 77R55E4kRedeieX52CvO+ZzocWRwBj1o4c1lzCbg+uHnVsCVBg3we13D5jccz148A5Ht DTdG/44K7zqGtVdZDDQ1bi3urjHOb+OBiSNf25aVc8nDLxZyJMItbALEWz19aTmOf+kd H6vY2v/fiRlC/t6MLmqA3VGlskNHvhE0sQqAf9o41gFg+xi5npH4y8vMF7xWkV76YVh/ sipw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783335697; x=1783940497; 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=iBp5CTAibDRDjJr1C9jbaznxXxt4zBdSgPR4F1nm75g=; b=k008U8OIu+zCJX3uhqD20+NSlHc0mDCgiy97uclfejKCOKOXocUgbdqceNXXiLfeax u4jgex8+s7PO0K9iTT9NUZes5qSCwfgjdbnX6oCW5JVEYQB7MQ3bh8H6VVo/YMG74DQP 6vtu43SwmvbhHUZlp2AUbgyZBgM8oQtpVHbTxvS5TiIspX8MYaqxGOerYyW3Fv+w8OT/ ndzQl2Lzqdk7dwDD/vmZzLTSRbfgoimgVOqqN6WWduRQyKjNqHSXtv31C/+62UwWjVJy p+oTPPT+SHvlNQNHopSarUPDWxsaZoCIN9K2LmTiD6Ui34w4ltQXT3lLEPLjo+h4yxSo LqbQ== X-Gm-Message-State: AOJu0YzVdHt/esGpSuHQlJ+ZrLxPQGemZFO0yZqdgtXB2ym5xJ8m7Q3a UWTSWHKQijwEYIMz3tTVtVDyQ9vAIeOBKJekTbdquyn09NJgRxALSuKlw/sdBxGWGU88NS5+SD2 mqEdL X-Gm-Gg: AfdE7clVvtuo/kf6hzzuOJ85e3QVSqyHmzmxrtG+JFFWmNMT6hb4sXIU4xS1vqS5sIP DUjX+tnttOVaQJJl0jpVftDDgpPPS1QwRctQ1d56hVUIEAs7V9v9OnZGSEWOtKMaZEBargTW2B2 cHvRxRn4Icph1L0yrmKL0WfZuQIhoX4LEsCTwhGUAQsp/kobcjJh/gCgX8nXW3ge9kMMV2kwz7Q 5WOSDfuoqS8dTE0vMXAEYSvl7xjhcS+o+V19KKOUFOT+Cin28PzGGV7nxMIugklDnfjvjm7Z08Y R+t30BUW7VDvO8i7eHcHARb37H93ke6hJKZm+klUrLJTqTyQTIMy+DYj2q6SBgjoOWJ6IhaGE3z hTQKROvgj/21CL8i6J3Sb8+Sk+bqPXSerhBSlFFtikIY8MIad8eeREy659wIQ01XeY8Kpqy6voz bNM/QQyKJt8YZQQ0FXFwN70EsqdHv5 X-Received: by 2002:a05:600c:6992:b0:493:bc4a:fb50 with SMTP id 5b1f17b1804b1-493d11fdfb6mr112574575e9.38.1783335697116; Mon, 06 Jul 2026 04:01:37 -0700 (PDT) Received: from inspiron14p-linux ([109.77.44.220]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-47aa0960634sm25190846f8f.26.2026.07.06.04.01.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Jul 2026 04:01:36 -0700 (PDT) From: Bryan O'Donoghue To: libcamera-devel@lists.libcamera.org Cc: bryan.odonoghue@linaro.org, pavel@ucw.cz Subject: [PATCH v4 7/7] libcamera: software_isp: debayer_egl: Implement input/output frame caching mechanism Date: Mon, 6 Jul 2026 12:01:20 +0100 Message-ID: <20260706110120.2509826-8-bryan.odonoghue@linaro.org> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260706110120.2509826-1-bryan.odonoghue@linaro.org> References: <20260706110120.2509826-1-bryan.odonoghue@linaro.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" Implement a texture caching mechanism for both input and output frames and for both types of input frame. The before/after on a Qualcomm x1e is: 9.737ms per frame 5.691ms per frame The before/after on a Qualcomm sm8250 is: 21.710ms per frame 17.336ms per frame for i in {1..20} do cam -c /base/soc@0/cci@ac16000/i2c-bus@1/camera@10 -s width=1920,height=1080 --capture=60 Interestingly there appears to be an absolute ~ 4.x ms per frame uplift as opposed to what intuition might suggest a proportional. Signed-off-by: Bryan O'Donoghue --- src/libcamera/software_isp/debayer_egl.cpp | 112 ++++++++++++++++----- src/libcamera/software_isp/debayer_egl.h | 13 ++- 2 files changed, 96 insertions(+), 29 deletions(-) diff --git a/src/libcamera/software_isp/debayer_egl.cpp b/src/libcamera/software_isp/debayer_egl.cpp index c3a7b7952..ca3353d9f 100644 --- a/src/libcamera/software_isp/debayer_egl.cpp +++ b/src/libcamera/software_isp/debayer_egl.cpp @@ -345,6 +345,9 @@ int DebayerEGL::configure(const StreamConfiguration &inputCfg, */ stats_->setWindow(Rectangle(window_.size())); + inputBufferCount_ = inputCfg.bufferCount; + outputBufferCount_ = outputCfg.bufferCount; + return 0; } @@ -504,36 +507,95 @@ void DebayerEGL::setShaderVariableValues(eGLImage &eglImageIn, const DebayerPara return; } -int DebayerEGL::debayerGPU(FrameBuffer *input, FrameBuffer *output, const DebayerParams ¶ms, std::optional *inMapped, std::optional *inDmaSyncer) +eGLImage *DebayerEGL::getCachedInputFrameBuffer(FrameBuffer *input, std::optional *inMapped, std::optional *inDmaSyncer) { - bool dmabuf_import_succeeded = false; - - /* eGL context switch */ - egl_.makeCurrent(); - - /* 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) - dmabuf_import_succeeded = true; - else - LOG(Debayer, Info) << "Importing input buffer with DMABuf import failed, falling back to upload"; + const SharedFD &fd = input->planes()[0].fd; + bool use_dmabuf = false; + + auto [input_cache, cache_miss] = eglImageBayerIn_.try_emplace(fd); + if (cache_miss) { + if (eglImageBayerIn_.size() > inputBufferCount_) { + eglImageBayerIn_.erase(inputRing_.front()); + inputRing_.pop_front(); + LOG(Debayer, Error) << "Input cache " << inputBufferCount_ << " exceeded evicted entry"; + } + input_cache->second = std::make_unique(glFormat_, inputConfig_.stride / bytesPerPixel_, + height_, inputConfig_.stride, GL_TEXTURE0, 0); + inputRing_.push_back(fd); + } + eGLImage *eglImageIn = input_cache->second.get(); + + /* Try to create texture for input buffer via dmabuf import only on cache miss */ + if (cache_miss) { + if (egl_.createInputDMABufTexture2D(*eglImageIn, input->planes()[0].fd.get()) == 0) + use_dmabuf = true; + } else if (!eglImageIn->dmabuf_import_failed_) { + use_dmabuf = true; + egl_.activateBindTexture(*eglImageIn); } - /* Otherwise create texture for input buffer via upload from CPU */ - if (!dmabuf_import_succeeded) { + if (!use_dmabuf) { inDmaSyncer->emplace(input->planes()[0].fd, DmaSyncer::SyncType::Read); inMapped->emplace(input, MappedFrameBuffer::MapFlag::Read); if (!inMapped->value().isValid()) { LOG(Debayer, Error) << "mmap-ing buffer(s) failed"; - return -ENODEV; + if (cache_miss) { + eglImageBayerIn_.erase(input_cache); + inputRing_.pop_back(); + } + return nullptr; + } + if (cache_miss) + egl_.createTexture2D(*eglImageIn, inMapped->value().planes()[0].data()); + else + egl_.updateTexture2D(*eglImageIn, inMapped->value().planes()[0].data()); + } + + return eglImageIn; +} + +eGLImage *DebayerEGL::getCachedOutputFrameBuffer(FrameBuffer *output) +{ + const SharedFD &fd = output->planes()[0].fd; + + auto [output_cache, cache_miss] = eglImageBayerOut_.try_emplace(fd); + if (cache_miss) { + if (eglImageBayerOut_.size() > outputBufferCount_) { + eglImageBayerOut_.erase(outputRing_.front()); + outputRing_.pop_front(); + LOG(Debayer, Error) << "Output cache " << outputBufferCount_ << " exceeded evicted entry"; } - egl_.createTexture2D(*eglImageBayerIn_, inMapped->value().planes()[0].data()); + output_cache->second = std::make_unique(GL_RGBA, outputSize_.width, + outputSize_.height, outputConfig_.stride, GL_TEXTURE1, 1); + if (egl_.createOutputDMABufTexture2D(*output_cache->second, output->planes()[0].fd.get())) { + eglImageBayerOut_.erase(output_cache); + return nullptr; + } + outputRing_.push_back(fd); } + eGLImage *eglImageOut = output_cache->second.get(); + + return eglImageOut; +} + +int DebayerEGL::debayerGPU(FrameBuffer *input, FrameBuffer *output, const DebayerParams ¶ms, std::optional *inMapped, std::optional *inDmaSyncer) +{ + eGLImage *eglImageIn; + eGLImage *eglImageOut; + + /* eGL context switch */ + egl_.makeCurrent(); - /* Generate the output render framebuffer as render to texture */ - egl_.createOutputDMABufTexture2D(*eglImageBayerOut_, output->planes()[0].fd.get()); + eglImageIn = getCachedInputFrameBuffer(input, inMapped, inDmaSyncer); + if (!eglImageIn) + return -ENOMEM; + eglImageOut = getCachedOutputFrameBuffer(output); + if (!eglImageOut) + return -ENOMEM; + + egl_.attachTextureToFBO(*eglImageOut); + setShaderVariableValues(*eglImageIn, params); - setShaderVariableValues(*eglImageBayerIn_, params); glViewport(0, 0, width_, height_); glClear(GL_COLOR_BUFFER_BIT); glDrawArrays(GL_TRIANGLE_FAN, 0, DEBAYER_OPENGL_COORDS); @@ -615,19 +677,15 @@ int DebayerEGL::start() if (initBayerShaders(inputPixelFormat_, outputPixelFormat_)) return -EINVAL; - /* Raw bayer input as texture */ - eglImageBayerIn_ = std::make_unique(glFormat_, inputConfig_.stride / bytesPerPixel_, height_, inputConfig_.stride, GL_TEXTURE0, 0); - - /* Texture we will render to */ - eglImageBayerOut_ = std::make_unique(GL_RGBA, outputSize_.width, outputSize_.height, outputConfig_.stride, GL_TEXTURE1, 1); - return 0; } void DebayerEGL::stop() { - eglImageBayerOut_.reset(); - eglImageBayerIn_.reset(); + eglImageBayerOut_.clear(); + eglImageBayerIn_.clear(); + outputRing_.clear(); + inputRing_.clear(); if (programId_) glDeleteProgram(programId_); diff --git a/src/libcamera/software_isp/debayer_egl.h b/src/libcamera/software_isp/debayer_egl.h index 348d7305b..dde6ba9bf 100644 --- a/src/libcamera/software_isp/debayer_egl.h +++ b/src/libcamera/software_isp/debayer_egl.h @@ -9,9 +9,11 @@ #pragma once +#include #include #include #include +#include #include #define GL_GLEXT_PROTOTYPES @@ -68,14 +70,21 @@ private: void setShaderVariableValues(eGLImage &eGLImageIn, const DebayerParams ¶ms); int debayerGPU(FrameBuffer *input, FrameBuffer *output, const DebayerParams ¶ms, std::optional *mappedInputBuffer, std::optional *inputBufferDmaSyncer); + eGLImage *getCachedInputFrameBuffer(FrameBuffer *input, std::optional *inMapped, std::optional *inDmaSyncer); + eGLImage *getCachedOutputFrameBuffer(FrameBuffer *output); + /* Shader program identifiers */ GLuint vertexShaderId_ = 0; GLuint fragmentShaderId_ = 0; GLuint programId_ = 0; /* Pointer to object representing input texture */ - std::unique_ptr eglImageBayerIn_; - std::unique_ptr eglImageBayerOut_; + std::unordered_map> eglImageBayerIn_; + std::unordered_map> eglImageBayerOut_; + std::deque inputRing_; + std::deque outputRing_; + unsigned int inputBufferCount_; + unsigned int outputBufferCount_; /* Shader parameters */ float firstRed_x_;