From patchwork Mon Jul 6 22:22:45 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: 27259 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 83314C3308 for ; Mon, 6 Jul 2026 22:23:09 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 91B0666063; Tue, 7 Jul 2026 00:23:08 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="iPF+/l4T"; dkim-atps=neutral Received: from mail-wm1-x336.google.com (mail-wm1-x336.google.com [IPv6:2a00:1450:4864:20::336]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 5D12E6605B for ; Tue, 7 Jul 2026 00:23:03 +0200 (CEST) Received: by mail-wm1-x336.google.com with SMTP id 5b1f17b1804b1-493bab44440so207565e9.0 for ; Mon, 06 Jul 2026 15:23:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1783376583; x=1783981383; 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=SslCStkiHkFC7e1FQGIriVH2RTlbB3ja2yOPPwrhQ6k=; b=iPF+/l4TGt/PV5R9y09GPcKjeTDVNbNNfIz2CaSPPI3KU7nT4u1+CprXsuaZo/fN34 5vonCZWKpbn5LO7L7KzbFKVjdIEB7CwktNFegV/r7WKXyHcIVapMfZyC4/K7F/agPSFM zjfW7Nlc82N66YwmD/nsqQfYhNI4/aClgCVv08G7l+6HESSt8jMoLw5/T6Ke9eKxdCAr 6bzHXyEOK8T/LXJDsN/UMPl1TxNvjIcw13r8hK3d8k0n7lJBWIv02u0VPlJSnO6B55wT +axHadLN5ejfAl9aRMeEL/Tbq8Kcetf0Doo2C8Y6Zyz8eX6tyALibGLS1OIUVuPLhdJ9 NvAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783376583; x=1783981383; 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=SslCStkiHkFC7e1FQGIriVH2RTlbB3ja2yOPPwrhQ6k=; b=Ka9Xl2DXpLQFv8XH3MXxwngpXeAibRzpQ86EmSCSTMmjaOIDgJMotlInwXkySDzKfu tY0sZsC9lKIuIO1mSpPZ5TspOEbWLCla6jFEzxGXgRYpYu+AhOdvTTypgYOdFr6oDYx+ P0tJqzValw1Rs5trAQX27BzEy1uPCfmYCchX/80YQgb76/bw4MLNaoeWT65qPxfzNHTL l/h9eTbjKIaXDfXieXV788NjNE01cBNfTqRVRAMZTrMP5F0GcMMeKsqoitWt1oKua8GU 7OtIqTfCzbSkRiTym58k2S7b/lHxSrnzEGhCBpl7VM8opg4yGpMK5d0vOeXjL5ggjvWC qicg== X-Gm-Message-State: AOJu0Yz+RrfK/CMYAwOAXY3kPS69B1Q/AGAjU0IXDZtwN9ZbIj7xX9u6 JdlOr/OBzfZpAd0s/8R0gNmGjg3vSoHx4QysyrubqAhRZmvEFF39BqcPSyDIN16tzvs+9XcykNt 5Ixtg X-Gm-Gg: AfdE7cnr/N7hwd/Nq6Zo+bH8U/8cfpPLETdkhwuG196TWyhxEfH1s8vFaDtp+2z8jos wcp5Qyl7852QKXexVuq6g/dI2EGWUTLgPMn9G3vyW3Oi2wM4Q8W+inCqkZpKXvT2vIgIFLk/xYz PQuLozjEjR1Ys4SI/uCIdjfc1DYSvV42meeiJ/j/5RDWi5AZl8Q1B6O5MU+FS1UoiChDbesn4YA e1QJbo9saXIM2wLNLtJBJcE0zxky9jGL8tCIZKAwJUrqpjOuGLpFx1NYSAp748/ZR9NYu2Q3Ilf J5JAZ5e5EF57JET1+f0X7l4GGOieJ8kWfQN0i1ooINXN8ibjZH8JMTi1bw/Dmg/mx8vwOnq2DOE u//fcwu6d3+XnYHg6U9XmlVDxqg4D1Hu4A+8Ltjjk+EvLt9jxeygQXCHNeLDEwBZdOeI295TFHT tSSUGhYPQZFFPTO6RfEtXxLIq3+a+N X-Received: by 2002:a05:600c:1f84:b0:492:7025:13fd with SMTP id 5b1f17b1804b1-493e0f7a2b7mr1946195e9.0.1783376582805; Mon, 06 Jul 2026 15:23:02 -0700 (PDT) Received: from inspiron14p-linux ([109.77.44.220]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-47aa039ad21sm27916413f8f.20.2026.07.06.15.23.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Jul 2026 15:23:02 -0700 (PDT) From: Bryan O'Donoghue To: libcamera-devel@lists.libcamera.org Cc: bryan.odonoghue@linaro.org, pavel@ucw.cz Subject: [PATCH v6 6/6] libcamera: software_isp: debayer_egl: Implement input/output frame caching mechanism Date: Mon, 6 Jul 2026 23:22:45 +0100 Message-ID: <20260706222245.247396-7-bryan.odonoghue@linaro.org> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260706222245.247396-1-bryan.odonoghue@linaro.org> References: <20260706222245.247396-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 | 125 ++++++++++++++++----- src/libcamera/software_isp/debayer_egl.h | 10 +- 2 files changed, 104 insertions(+), 31 deletions(-) diff --git a/src/libcamera/software_isp/debayer_egl.cpp b/src/libcamera/software_isp/debayer_egl.cpp index c3a7b7952..a7cca5dde 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,106 @@ 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; + const SharedFD &fd = input->planes()[0].fd; + bool cache_miss = true; + eGLImage *eglImageIn; + + for (auto &[ifd, img] : eglImageInCache_) { + if (ifd == fd) { + eglImageIn = img.get(); + cache_miss = false; + break; + } + } - /* eGL context switch */ - egl_.makeCurrent(); + if (cache_miss) { + if (eglImageInCache_.size() >= inputBufferCount_) { + eglImageInCache_.pop_front(); + LOG(Debayer, Debug) << "Input cache " << inputBufferCount_ << " exceeded evicted entry"; + } + + eglImageInCache_.emplace_back(fd, std::make_unique(glFormat_, inputConfig_.stride / bytesPerPixel_, height_, inputConfig_.stride, GL_TEXTURE0, 0)); + eglImageIn = eglImageInCache_.back().second.get(); + + if (egl_.createInputDMABufTexture2D(*eglImageIn, input->planes()[0].fd.get()) == 0) + return eglImageIn; + + } else if (!eglImageIn->dmabuf_import_failed_) { + egl_.activateBindTexture(*eglImageIn); + return eglImageIn; + } + + /* DMA mode fail create/update an existing texture using the slow path */ + 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"; + if (cache_miss) { + eglImageInCache_.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; +} - /* 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"; +eGLImage *DebayerEGL::getCachedOutputFrameBuffer(FrameBuffer *output) +{ + const SharedFD &fd = output->planes()[0].fd; + bool cache_miss = true; + eGLImage *eglImageOut; + + for (auto &[ifd, img] : eglImageOutCache_) { + if (ifd == fd) { + eglImageOut = img.get(); + cache_miss = false; + break; + } } - /* Otherwise create texture for input buffer via upload from CPU */ - if (!dmabuf_import_succeeded) { - 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) { + if (eglImageOutCache_.size() >= outputBufferCount_) { + eglImageOutCache_.pop_front(); + LOG(Debayer, Debug) << "Output cache " << outputBufferCount_ << " exceeded evicted entry"; + } + + eglImageOutCache_.emplace_back(fd, std::make_unique(GL_RGBA, outputSize_.width, + outputSize_.height, outputConfig_.stride, GL_TEXTURE1, 1)); + eglImageOut = eglImageOutCache_.back().second.get(); + + if (egl_.createOutputDMABufTexture2D(*eglImageOut, output->planes()[0].fd.get())) { + eglImageOutCache_.pop_back(); + return nullptr; } - egl_.createTexture2D(*eglImageBayerIn_, inMapped->value().planes()[0].data()); } - /* Generate the output render framebuffer as render to texture */ - egl_.createOutputDMABufTexture2D(*eglImageBayerOut_, output->planes()[0].fd.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(); + + 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 +688,13 @@ 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(); + eglImageOutCache_.clear(); + eglImageInCache_.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..d6223f3a9 100644 --- a/src/libcamera/software_isp/debayer_egl.h +++ b/src/libcamera/software_isp/debayer_egl.h @@ -9,6 +9,7 @@ #pragma once +#include #include #include #include @@ -68,14 +69,19 @@ 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::deque>> eglImageInCache_; + std::deque>> eglImageOutCache_; + unsigned int inputBufferCount_; + unsigned int outputBufferCount_; /* Shader parameters */ float firstRed_x_;