From patchwork Wed Dec 2 16:13:26 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Plowman X-Patchwork-Id: 29153 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 40A77C3361 for ; Thu, 3 Dec 2026 13:01:27 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id B62846AB4D; Thu, 3 Dec 2026 14:01:23 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=raspberrypi.com header.i=@raspberrypi.com header.b="O2Cs+bTY"; dkim-atps=neutral Received: from mail-wm1-x332.google.com (mail-wm1-x332.google.com [IPv6:2a00:1450:4864:20::332]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 80AB66AB3F for ; Thu, 3 Dec 2026 14:01:19 +0100 (CET) Received: by mail-wm1-x332.google.com with SMTP id 5b1f17b1804b1-4bb85e07aadso172667165e9.1 for ; Thu, 03 Dec 2026 05:01:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=raspberrypi.com; s=google; t=1796302879; x=1796907679; 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:content-type; bh=pcGaAXUwDRRs8547/WG9SGJvLxc1ZhSt1tXSILvvO0c=; b=O2Cs+bTYYmE7tM6EgE5/StPEMzYGy2BhKVh93vwu2tTET8FTPJm0o6PaHAJSxUvupn 0SPH0f0n+8AgwMU9X//yGyrE5CKnmJmBey7YriugXhHCifSpX904q/m0rnByAwjGuLIY ApfnW2mrzgx3wXHfIhjFufl213rkYKoXBwZ6v9xi5Zc/6VHPoAg5RYMSdv4NFraiTVdn mvaxSkb/aogrJNQEOoA0Zi4xj8n0dUzBJVVKhHROFndOaAV+0/n2u3D+67KqUus461yP kI7ELp1Kfo7221RTt5jvOHnly55UCqIHGJTJE2m4FujdVbFLcpy3YZAKc/jNSYJOtdfu G50w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796302879; x=1796907679; 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:content-type; bh=pcGaAXUwDRRs8547/WG9SGJvLxc1ZhSt1tXSILvvO0c=; b=Dv9agKUDgLToigG5Kx0RKbHXRk5lZUYqS5WiIe+1N8YA22f51j01lrDaMotf8X3498 jST4MuzIjJ/POrjkifJn1bHeurYfqCxqhqyZJCn2T/ErmlmfhTFAzytKGHEQAGpWnAFr gKHP7KdkDqttIWuyLyFamp0KzkHqsWLr/XWZfBVwq++p9urZ8ergi3woIHoHc2k97FHR gfpzuKcI6Mdq2uF3gs5sf48J3lmb0b4g8PxXI9k2g5Aovn4tnvEoGP9apQnaBmj2C2T+ hnFIP86L+WlkpSpelK3/YivmqZTl9Ie4xQOKOpUp7u7/Q/LqZxvqoljylY/kRX+qBUCd UlcA== X-Gm-Message-State: APuU4Z+Qmf8HiLtaZpDZtlmEnm/576076l8zwUcapMrneClU5wRPyBOe Wto+h6m27YexKOQlEwv+Cw5yGvHxf9rOlWYgHRuKfw9VuqQ/YnB4cr9TENgKDFV4OwHEQ9sH/yn x3z+JcqA= X-Gm-Gg: AZFdTZe9SLKHgSGakpaAYGexlOSxmPYwyh3IxN5FXnYSMUKrUz0uFQakdWfQUfXxhMN oC3j5wvf0SZhCUJElMikxeXEqjsaVna/mNtYRwL4d8bx0zA10svfi/6UZBffDZZCHGJhOk0GKrL 1CekI9regQeefk9rc1IWVnKkTbUeVrXlyoVROAej1n5gQlrFQAdwmz7Vd2nZ0CfgKrDGUry5RoD kV6FBwiZ3OXdHdeGWEEarQJ2y8tAk76mtg7BeLfX+iPD3FrMW8fLUdsmrSCV3YNVNucXF9Liz2c cJNH+E9BmJu+uzvookm9ULF6Er4ZJUQPE4f44zDWYJQmjzk+Rt0VoyAF1ue5Oa1g6FU5KqL6xmB 2OoR30z9W0hfCvC4t3dfXsdZsOJ3GGNMdSt4BFxd/FnxrUeYatgiHPvJG6YgsVh37cIcq64SvHY 90kuoZ+ihrGNuw6VESNjagtAElcu3vkmWyT7XHiZ+whBCki0JZXAkUhHQLBX9oTeh5vxaEqFdmA M2agzM8MDrlrcrIsdU4Ryibf+s/8Ouy+pcIWE+KfwplKIH7ekRN8ye/ad88tNt1SBYGLyQq/Pde zeHJ/lpLv3d+rqeGHRgfAlwhBy3bd2IsQ1EAxiGEW/ybchZ17rFzwVmGQO8LcRNSMugDw2Yvigx 5cAeJh91KX0ZB+2+UnHUI5FDhQh7JjvpIp6idxPmC7NQphSy/jw== X-Received: by 2002:a05:600c:5303:b0:4a2:1655:87db with SMTP id 5b1f17b1804b1-4bb88785d2fmr828797895e9.0.1796302878639; Thu, 03 Dec 2026 05:01:18 -0800 (PST) Received: from davidp-hailo10.pitowers.org ([2a00:1098:3142:1f:9afe:54ff:fe21:ed41]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bbe005543esm81555685e9.5.2026.12.03.05.01.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Dec 2026 05:01:18 -0800 (PST) From: David Plowman To: libcamera-devel@lists.libcamera.org Cc: David Plowman Subject: [PATCH v1 4/5] pipeline: rpi: Add infrastructure for additional pipeline "stages" Date: Wed, 2 Dec 2026 16:13:26 +0000 Message-ID: <20261203130113.47873-5-david.plowman@raspberrypi.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261203130113.47873-1-david.plowman@raspberrypi.com> References: <20261203130113.47873-1-david.plowman@raspberrypi.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 A pipeline stage operates on a raw Bayer buffer after the IPA's prepareIsp and before the main ISP runs. Stages are configured and controlled like other IPAs, but those parameters are passed back to the pipeline handler to create the requested stages, and to run them in a per-camera worker thread. Most of this commit defines the Stage base class and the StageRunner that executes them, trying to follow the pattern of the Software ISP. But we also include the PiSP/vc4 code that allows the pipeline handler to stay in the "busy" state while those stages are running, moving on only when the runner signals completion. Signed-off-by: David Plowman --- src/libcamera/pipeline/rpi/common/meson.build | 1 + .../pipeline/rpi/common/pipeline_base.cpp | 83 ++++++++++ .../pipeline/rpi/common/pipeline_base.h | 36 +++++ src/libcamera/pipeline/rpi/common/stage.h | 75 +++++++++ .../pipeline/rpi/common/stage_runner.cpp | 149 ++++++++++++++++++ .../pipeline/rpi/common/stage_runner.h | 74 +++++++++ src/libcamera/pipeline/rpi/pisp/pisp.cpp | 37 ++++- src/libcamera/pipeline/rpi/vc4/vc4.cpp | 34 +++- 8 files changed, 482 insertions(+), 7 deletions(-) create mode 100644 src/libcamera/pipeline/rpi/common/stage.h create mode 100644 src/libcamera/pipeline/rpi/common/stage_runner.cpp create mode 100644 src/libcamera/pipeline/rpi/common/stage_runner.h diff --git a/src/libcamera/pipeline/rpi/common/meson.build b/src/libcamera/pipeline/rpi/common/meson.build index b2b1a0a6..5411ae84 100644 --- a/src/libcamera/pipeline/rpi/common/meson.build +++ b/src/libcamera/pipeline/rpi/common/meson.build @@ -4,4 +4,5 @@ libcamera_internal_sources += files([ 'delayed_controls.cpp', 'pipeline_base.cpp', 'rpi_stream.cpp', + 'stage_runner.cpp', ]) diff --git a/src/libcamera/pipeline/rpi/common/pipeline_base.cpp b/src/libcamera/pipeline/rpi/common/pipeline_base.cpp index 6944d5c4..2c324035 100644 --- a/src/libcamera/pipeline/rpi/common/pipeline_base.cpp +++ b/src/libcamera/pipeline/rpi/common/pipeline_base.cpp @@ -559,6 +559,12 @@ int PipelineHandlerBase::configure(Camera *camera, CameraConfiguration *config) return ret; } + ret = data->configureStages(result.stageConfig); + if (ret) { + LOG(RPI, Error) << "Failed to configure the pipeline stages: " << ret; + return ret; + } + /* * Update the ScalerCropMaximum to the correct value for this camera mode. * For us, it's the same as the "analogue crop". @@ -700,6 +706,8 @@ int PipelineHandlerBase::start(Camera *camera, const ControlList *controls) /* A good moment to add an initial clock sample. */ data->wallClockRecovery_.addSample(); + data->startStages(); + /* * Reset the delayed controls with the gain and exposure values set by * the IPA. @@ -729,6 +737,10 @@ void PipelineHandlerBase::stopDevice(Camera *camera) CameraData *data = cameraData(camera); data->state_ = CameraData::State::Stopped; + + /* Wait for any stages to finish. Any pending completions will be discarded. */ + data->stopStages(); + data->platformStop(); for (const auto stream : data->streams_) { @@ -1265,6 +1277,77 @@ int CameraData::configureIPA(const CameraConfiguration *config, ipa::RPi::Config return 0; } +/* + * Configure all the pipeline stages enabled in the stage configuration from + * the IPA, and make a list of them for the worker to run. + */ +int CameraData::configureStages(const ipa::RPi::StageConfig &config) +{ + V4L2DeviceFormat format; + int ret = frontendDevice()->getFormat(&format); + if (ret) { + LOG(RPI, Error) << "Failed to get the frontend format"; + return ret; + } + + stageFormat_.pixelFormat = format.fourcc.toPixelFormat(); + stageFormat_.size = format.size; + stageFormat_.stride = format.planes[0].bpl; + + ret = stageRunner_.configure(config, stageFormat_); + if (ret) + return ret; + + if (stagesActive()) + LOG(RPI, Debug) << "Pipeline stages active, raw format " + << stageFormat_.pixelFormat << " " << stageFormat_.size + << " stride " << stageFormat_.stride; + + return 0; +} + +bool CameraData::stagesActive() const +{ + return stageRunner_.hasActiveStages(); +} + +void CameraData::startStages() +{ + if (stagesActive()) + stageRunner_.start(); +} + +/* Wait for the stages to finish and stop them. */ +void CameraData::stopStages() +{ + stageRunner_.stop(); +} + +/* Pass the frame to the stage worker thread, along with all the info it needs. */ +void CameraData::runStages(const ipa::RPi::BufferIds &buffers, const ipa::RPi::StageParams ¶ms, + const BufferObject &bayer) +{ + stageBuffers_ = buffers; + + StageJob job; + job.params = params; + if (bayer.mapped && !bayer.mapped->planes().empty()) + job.bayerData = bayer.mapped->planes()[0]; + job.bayerFd = bayer.buffer->planes()[0].fd; + job.bayerFormat = stageFormat_; + + stageRunner_.run(job); +} + +/* This is what gets signalled when all the stages finish with a frame. */ +void CameraData::stagesComplete() +{ + if (!isRunning()) + return; + + platformStagesComplete(stageBuffers_); +} + void CameraData::metadataReady(const ControlList &metadata) { if (!isRunning()) diff --git a/src/libcamera/pipeline/rpi/common/pipeline_base.h b/src/libcamera/pipeline/rpi/common/pipeline_base.h index 7b872555..408a4b58 100644 --- a/src/libcamera/pipeline/rpi/common/pipeline_base.h +++ b/src/libcamera/pipeline/rpi/common/pipeline_base.h @@ -33,6 +33,8 @@ #include "delayed_controls.h" #include "rpi_stream.h" +#include "stage.h" +#include "stage_runner.h" using namespace std::chrono_literals; @@ -51,6 +53,12 @@ public: : Camera::Private(pipe), state_(State::Stopped), startupFrameCount_(0), invalidFrameCount_(0), buffersAllocated_(false) { + stageRunner_.stagesComplete.connect(this, &CameraData::stagesComplete); + + /* + * Stages that can run on every platform would be added here: + * e.g. addStage(std::make_unique()); + */ } virtual ~CameraData() @@ -99,6 +107,21 @@ public: virtual int platformPipelineConfigure(const std::unique_ptr &root) = 0; + /* + * Pipeline handler "stages" (stage.h) operate on the raw Bayer + * buffer after the IPA's prepareIsp, and before the ISP proper. They + * run in a per-camera worker thread, which is all handled here. + */ + int configureStages(const ipa::RPi::StageConfig &config); + void startStages(); + void stopStages(); + bool stagesActive() const; + void runStages(const ipa::RPi::BufferIds &buffers, const ipa::RPi::StageParams ¶ms, + const BufferObject &bayer); + void stagesComplete(); + /* How the platform carries on with a frame once its stages are done. */ + virtual void platformStagesComplete(const ipa::RPi::BufferIds &buffers) = 0; + std::unique_ptr ipa_; std::unique_ptr sensor_; @@ -196,8 +219,21 @@ protected: virtual void tryRunPipeline() = 0; + /* Register a stage. Platforms add their own stages in their constructors. */ + void addStage(std::unique_ptr stage) { stageRunner_.addStage(std::move(stage)); } + private: void checkRequestCompleted(); + + /* Owns the stages, and runs them in its worker thread. */ + StageRunner stageRunner_; + /* The raw Bayer format (taken from the frontend device), for the stages. */ + StageFormat stageFormat_; + /* + * The buffers of the frame in the stages, for when they've finished (there's + * only ever a single frame in flight). + */ + ipa::RPi::BufferIds stageBuffers_; }; class PipelineHandlerBase : public PipelineHandler diff --git a/src/libcamera/pipeline/rpi/common/stage.h b/src/libcamera/pipeline/rpi/common/stage.h new file mode 100644 index 00000000..d3f1b74f --- /dev/null +++ b/src/libcamera/pipeline/rpi/common/stage.h @@ -0,0 +1,75 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2026, Raspberry Pi Ltd + * + * Base class for pipeline handler "stages" + */ + +#pragma once + +#include +#include + +#include + +#include +#include + +#include + +namespace libcamera { + +namespace RPi { + +/* The format of the raw Bayer buffers. */ +struct StageFormat { + PixelFormat pixelFormat; + Size size; + unsigned int stride = 0; +}; + +/* Everything the stages need to process a frame. */ +struct StageJob { + /* Per-frame parameters for all the stages, from the IPA. */ + ipa::RPi::StageParams params; + + /* The raw Bayer buffer (empty if not mapped) and associated info. */ + std::span bayerData; + SharedFD bayerFd; + StageFormat bayerFormat; +}; + +/* + * A pipeline handler stage operates on the raw Bayer buffer after the IPA's + * prepareIsp and before the ISP proper. To create one, derive from Stage and + * implement the functions below. It gets added in the CameraData constructor + * (or a platform's derived constructor if it can't run everywhere). + * + * An IPA algorithm must also be provided to supply the configuration and + * per-frame parameters. + */ +class Stage +{ +public: + virtual ~Stage() = default; + + virtual const char *name() const = 0; + + /* + * Called when the camera is configured. Return a positive value if + * the stage will run, 0 if it won't, or a negative error code if it + * should run but can't, which causes the camera configuration to fail. + */ + virtual int configure(const ipa::RPi::StageConfig &config, const StageFormat &format) = 0; + + /* Called when the camera starts and stops, for stages that are enabled. */ + virtual void start() {} + virtual void stop() {} + + /* Runs in the stage worker thread for every frame. */ + virtual void run(StageJob &job) = 0; +}; + +} /* namespace RPi */ + +} /* namespace libcamera */ diff --git a/src/libcamera/pipeline/rpi/common/stage_runner.cpp b/src/libcamera/pipeline/rpi/common/stage_runner.cpp new file mode 100644 index 00000000..fb227f3b --- /dev/null +++ b/src/libcamera/pipeline/rpi/common/stage_runner.cpp @@ -0,0 +1,149 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2026, Raspberry Pi Ltd + * + * Worker thread for running pipeline handler stages + * + * The structure broadly follows the Software ISP (software_isp.cpp): an + * object used from the pipeline handler has a worker thread where it runs + * its "stages", signalling back to the pipeline handler when they're done. + */ + +#include "stage_runner.h" + +#include +#include + +namespace libcamera { + +LOG_DECLARE_CATEGORY(RPI) + +namespace RPi { + +void StageWorker::process(StageJob job) +{ + for (Stage *stage : stages_) + stage->run(job); + + done.emit(); +} + +void StageWorker::stop() +{ + /* Nothing to do. A blocking call will wait for everything to finish. */ +} + +StageRunner::StageRunner() + : stageWorkerThread_("RPiStageWorker"), + worker_(std::make_unique()), running_(false) +{ + /* All the actual work has to be done in the worker thread. */ + worker_->done.connect(this, &StageRunner::workerDone); + worker_->moveToThread(&stageWorkerThread_); +} + +StageRunner::~StageRunner() +{ + stop(); + + /* Destroy the worker object before its thread goes. */ + worker_.reset(); +} + +/* Register a stage. Stages run in the order they were added. */ +void StageRunner::addStage(std::unique_ptr stage) +{ + ASSERT(!running_); + + LOG(RPI, Debug) << "Added stage " << stage->name(); + stages_.push_back(std::move(stage)); +} + +/* + * Configure all the stages, and make a list of the enabled ones. Fails if any + * stage should run but can't, in which case none will run. + */ +int StageRunner::configure(const ipa::RPi::StageConfig &config, const StageFormat &format) +{ + ASSERT(!running_); + + worker_->stages_.clear(); + + for (auto &stage : stages_) { + int ret = stage->configure(config, format); + if (ret < 0) { + LOG(RPI, Error) << "Stage " << stage->name() << " failed to configure: " << ret; + worker_->stages_.clear(); + return ret; + } + if (ret > 0) { + worker_->stages_.push_back(stage.get()); + LOG(RPI, Debug) << "Stage " << stage->name() << " is active"; + } + } + + return 0; +} + +/* Start the stages, and the worker thread that runs them. */ +void StageRunner::start() +{ + if (running_) + return; + + for (Stage *stage : worker_->stages_) + stage->start(); + + stageWorkerThread_.start(); + running_ = true; + + LOG(RPI, Debug) << "Stage worker thread started with " + << worker_->stages_.size() << " stage(s)"; +} + +/* + * Wait for any job in progress to finish, and then stop everything + * (don't forget to send completion signals). + */ +void StageRunner::stop() +{ + if (!running_) + return; + + stopThread(); + + for (Stage *stage : worker_->stages_) + stage->stop(); + + running_ = false; + + LOG(RPI, Debug) << "Stage worker thread stopped"; +} + +void StageRunner::stopThread() +{ + if (!stageWorkerThread_.isRunning()) + return; + + worker_->invokeMethod(&StageWorker::stop, ConnectionTypeBlocking); + + stageWorkerThread_.exit(); + stageWorkerThread_.wait(); + + Thread::current()->dispatchMessages(Message::Type::InvokeMessage, this); +} + +void StageRunner::run(const StageJob &job) +{ + ASSERT(running_); + worker_->invokeMethod(&StageWorker::process, ConnectionTypeQueued, job); +} + +void StageRunner::workerDone() +{ + stagesComplete.emit(); +} + +} /* namespace RPi */ + +} /* namespace libcamera */ diff --git a/src/libcamera/pipeline/rpi/common/stage_runner.h b/src/libcamera/pipeline/rpi/common/stage_runner.h new file mode 100644 index 00000000..a5c72434 --- /dev/null +++ b/src/libcamera/pipeline/rpi/common/stage_runner.h @@ -0,0 +1,74 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2026, Raspberry Pi Ltd + * + * Worker thread for running pipeline handler stages + */ + +#pragma once + +#include +#include + +#include +#include +#include + +#include "stage.h" + +namespace libcamera { + +namespace RPi { + +/* + * The StageWorker runs all the active stages in the worker thread, + * and signals when they're done. + */ +class StageWorker : public Object +{ +public: + void process(StageJob job); + void stop(); + + Signal<> done; + + /* The active stages. Only changed while the worker thread isn't running. */ + std::vector stages_; +}; + +/* + * The StageRunner owns all the stages, keeps a list of the active ones, and + * has the worker thread that runs them. + */ +class StageRunner : public Object +{ +public: + StageRunner(); + ~StageRunner(); + + void addStage(std::unique_ptr stage); + int configure(const ipa::RPi::StageConfig &config, const StageFormat &format); + bool hasActiveStages() const { return !worker_->stages_.empty(); } + + void start(); + void stop(); + + void run(const StageJob &job); + + Signal<> stagesComplete; + +private: + void stopThread(); + void workerDone(); + + /* All the stages, in the order they run, whether active or not. */ + std::vector> stages_; + + Thread stageWorkerThread_; + std::unique_ptr worker_; + bool running_; +}; + +} /* namespace RPi */ + +} /* namespace libcamera */ diff --git a/src/libcamera/pipeline/rpi/pisp/pisp.cpp b/src/libcamera/pipeline/rpi/pisp/pisp.cpp index 40ad91c2..fc7b283a 100644 --- a/src/libcamera/pipeline/rpi/pisp/pisp.cpp +++ b/src/libcamera/pipeline/rpi/pisp/pisp.cpp @@ -761,6 +761,7 @@ public: void processStatsComplete(const ipa::RPi::BufferIds &buffers); void prepareIspComplete(const ipa::RPi::BufferIds &buffers, bool stitchSwapBuffers, const ipa::RPi::StageParams &stageParams); + void finishPrepareIsp(unsigned int bayerId, bool stitchSwapBuffers); void setCameraTimeout(uint32_t maxFrameLengthMs); /* Array of CFE and ISP device streams and associated buffers/streams. */ @@ -774,6 +775,12 @@ public: SharedMemObject be_; bool beEnabled_; + /* + * The stitch buffer swap flag for the frame in the pipeline stages, for + * when they've finished. Only one frame is ever in the stages at a time. + */ + bool stagesStitchSwapBuffers_ = false; + std::unique_ptr csi2Subdev_; std::unique_ptr feSubdev_; @@ -807,6 +814,8 @@ public: private: int platformConfigure(const RPi::RPiCameraConfiguration *rpiConfig) override; + void platformStagesComplete(const ipa::RPi::BufferIds &buffers) override; + int platformConfigureIpa([[maybe_unused]] ipa::RPi::ConfigParams ¶ms) override { return 0; @@ -1887,7 +1896,7 @@ void PiSPCameraData::setCameraTimeout(uint32_t maxFrameLengthMs) } void PiSPCameraData::prepareIspComplete(const ipa::RPi::BufferIds &buffers, bool stitchSwapBuffers, - [[maybe_unused]] const ipa::RPi::StageParams &stageParams) + const ipa::RPi::StageParams &stageParams) { unsigned int embeddedId = buffers.embedded & RPi::MaskID; unsigned int bayerId = buffers.bayer & RPi::MaskID; @@ -1901,6 +1910,27 @@ void PiSPCameraData::prepareIspComplete(const ipa::RPi::BufferIds &buffers, bool handleStreamBuffer(buffer, &cfe_[Cfe::Embedded]); } + if (stagesActive()) { + /* + * Run stages on the raw buffer asynchronously, this will signal + * back to platformStagesComplete() once it's done. + */ + const RPi::BufferObject &bayer = cfe_[Cfe::Output0].getBuffers().at(bayerId); + stagesStitchSwapBuffers_ = stitchSwapBuffers; + runStages(buffers, stageParams, bayer); + return; + } + + finishPrepareIsp(bayerId, stitchSwapBuffers); +} + +/* Raw buffer is ready for the Back End, either straight from prepareIspComplete + * or via runStages signalling back to us. + */ +void PiSPCameraData::finishPrepareIsp(unsigned int bayerId, bool stitchSwapBuffers) +{ + FrameBuffer *buffer; + if (!beEnabled_) { /* * If there is no need to run the Backend, just signal that the @@ -1915,6 +1945,11 @@ void PiSPCameraData::prepareIspComplete(const ipa::RPi::BufferIds &buffers, bool handleState(); } +void PiSPCameraData::platformStagesComplete(const ipa::RPi::BufferIds &buffers) +{ + finishPrepareIsp(buffers.bayer & RPi::MaskID, stagesStitchSwapBuffers_); +} + int PiSPCameraData::configureCfe() { V4L2DeviceFormat cfeFormat; diff --git a/src/libcamera/pipeline/rpi/vc4/vc4.cpp b/src/libcamera/pipeline/rpi/vc4/vc4.cpp index 4bcaae54..7a010cf3 100644 --- a/src/libcamera/pipeline/rpi/vc4/vc4.cpp +++ b/src/libcamera/pipeline/rpi/vc4/vc4.cpp @@ -81,6 +81,8 @@ public: void processStatsComplete(const ipa::RPi::BufferIds &buffers); void prepareIspComplete(const ipa::RPi::BufferIds &buffers, bool stitchSwapBuffers, const ipa::RPi::StageParams &stageParams); + void queueIspInput(unsigned int bayerId); + void platformStagesComplete(const ipa::RPi::BufferIds &buffers) override; void setIspControls(const ControlList &controls); void setCameraTimeout(uint32_t maxFrameLengthMs); @@ -866,7 +868,7 @@ void Vc4CameraData::processStatsComplete(const ipa::RPi::BufferIds &buffers) void Vc4CameraData::prepareIspComplete(const ipa::RPi::BufferIds &buffers, [[maybe_unused]] bool stitchSwapBuffers, - [[maybe_unused]] const ipa::RPi::StageParams &stageParams) + const ipa::RPi::StageParams &stageParams) { unsigned int embeddedId = buffers.embedded & RPi::MaskID; unsigned int bayer = buffers.bayer & RPi::MaskID; @@ -875,11 +877,15 @@ void Vc4CameraData::prepareIspComplete(const ipa::RPi::BufferIds &buffers, if (!isRunning()) return; - buffer = unicam_[Unicam::Image].getBuffers().at(bayer & RPi::MaskID).buffer; - LOG(RPI, Debug) << "Input re-queue to ISP, buffer id " << (bayer & RPi::MaskID) - << ", timestamp: " << buffer->metadata().timestamp; - - isp_[Isp::Input].queueBuffer(buffer); + if (stagesActive()) { + /* + * Run stages on the raw buffer asynchronously, this will signal + * back to platformStagesComplete() once it's done. + */ + const RPi::BufferObject &bayerBuffer = unicam_[Unicam::Image].getBuffers().at(bayer); + runStages(buffers, stageParams, bayerBuffer); + } else + queueIspInput(bayer); if (sensorMetadata_ && embeddedId) { buffer = unicam_[Unicam::Embedded].getBuffers().at(embeddedId & RPi::MaskID).buffer; @@ -889,6 +895,22 @@ void Vc4CameraData::prepareIspComplete(const ipa::RPi::BufferIds &buffers, handleState(); } +void Vc4CameraData::queueIspInput(unsigned int bayerId) +{ + FrameBuffer *buffer = unicam_[Unicam::Image].getBuffers().at(bayerId).buffer; + + LOG(RPI, Debug) << "Input re-queue to ISP, buffer id " << bayerId + << ", timestamp: " << buffer->metadata().timestamp; + + isp_[Isp::Input].queueBuffer(buffer); +} + +void Vc4CameraData::platformStagesComplete(const ipa::RPi::BufferIds &buffers) +{ + queueIspInput(buffers.bayer & RPi::MaskID); + handleState(); +} + void Vc4CameraData::setIspControls(const ControlList &controls) { ControlList ctrls = controls;