From patchwork Tue Nov 17 08:47:19 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Klug X-Patchwork-Id: 28946 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 22A27BDE17 for ; Tue, 17 Nov 2026 08:47:40 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id C59A06A897; Tue, 17 Nov 2026 09:47:39 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="pBypnewz"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 321FA6A88C for ; Tue, 17 Nov 2026 09:47:38 +0100 (CET) Received: from ideasonboard.com (unknown [IPv6:2a00:6020:448c:6c00:27fa:cb4b:1d83:9b64]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id B89EE1CF6; Tue, 17 Nov 2026 09:45:10 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1794905110; bh=gTwJcHfkj8iBMBUuWyEhhNk+v3a8Ar3HDUBfHmkHdzI=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=pBypnewz8taCBNGqK0qUpZpCUCCI0ncphhDFvivavUkd6OoFqhggF45AQKdCz0LBi 1sUcltyMRZQVDOAsu9xlEVuzZWW7w89h4ANAOG+FWjoMec9Kdt52yWl6VnskSwnS7p 23o2j5hWUcrEiA3Vab6GothDfCfV8yKmsg0xmKCo= From: Stefan Klug Date: Tue, 17 Nov 2026 09:47:19 +0100 Subject: [PATCH 09/10] RFC ipa: libipa: Implement algorithm dependencies MIME-Version: 1.0 Message-Id: <20261117-sklug-digital-gain-support-v1-v1-9-f0aa342ca877@ideasonboard.com> References: <20261117-sklug-digital-gain-support-v1-v1-0-f0aa342ca877@ideasonboard.com> In-Reply-To: <20261117-sklug-digital-gain-support-v1-v1-0-f0aa342ca877@ideasonboard.com> To: libcamera-devel@lists.libcamera.org Cc: Stefan Klug X-Mailer: b4 0.14.3 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" Some algorithms have ordering dependencies on each other. As an example on rkisp1 the Compress algorithm is used to implement digital gain. So during computeParams(), Agc::prepare() must be called before Compress::prepare(), so that the Agc algorithm can store the digital gain in the frame context for Compress to pick it up. To make matters worse, the required order during configure() time can be inverse. In the example above, Compress::configure() should be called first, so that Agc::configure() can check the session if Compress (and therefore digital gain) is available. This patch implements a simple, yet effective dependency scheme where an algorithm can specify a list of dependencies. A dependency consists of an algorithm step (Init, Configure, Queue, Prepare, Process), a target and the type which can either be "Exists" meaning that the corresponding algorithm must be in the tuning file and "Before" which means that the target must be run first. Each algorithm can provide a list of dependencies through the newly added dependencies() function. This patch only implements the necessary infrastructure for rkisp1. Necessary dependencies will be added in upcoming patches. Signed-off-by: Stefan Klug --- src/ipa/libipa/algorithm.cpp | 22 ++++++ src/ipa/libipa/algorithm.h | 48 ++++++++++++ src/ipa/libipa/module.h | 178 +++++++++++++++++++++++++++++++++++-------- src/ipa/rkisp1/rkisp1.cpp | 16 ++-- 4 files changed, 225 insertions(+), 39 deletions(-) diff --git a/src/ipa/libipa/algorithm.cpp b/src/ipa/libipa/algorithm.cpp index 757ce3519652..20ead51bf7e0 100644 --- a/src/ipa/libipa/algorithm.cpp +++ b/src/ipa/libipa/algorithm.cpp @@ -16,6 +16,28 @@ namespace libcamera { namespace ipa { +std::ostream &operator<<(std::basic_ostream &out, libcamera::ipa::AlgorithmStep step) +{ + switch (step) { + case AlgorithmStep::Init: + out << "Init"; + break; + case AlgorithmStep::Configure: + out << "Configure"; + break; + case AlgorithmStep::Queue: + out << "Queue"; + break; + case AlgorithmStep::Prepare: + out << "Prepare"; + break; + case AlgorithmStep::Process: + out << "Process"; + break; + } + return out; +} + /** * \class Algorithm * \brief The base class for all IPA algorithms diff --git a/src/ipa/libipa/algorithm.h b/src/ipa/libipa/algorithm.h index b47765700317..97d7130a789d 100644 --- a/src/ipa/libipa/algorithm.h +++ b/src/ipa/libipa/algorithm.h @@ -7,6 +7,7 @@ #pragma once #include +#include #include #include @@ -18,6 +19,48 @@ class ValueNode; namespace ipa { +enum class AlgorithmStep { + Init = 0, + Configure, + Queue, + Prepare, + Process +}; + +std::ostream &operator<<(std::basic_ostream &out, libcamera::ipa::AlgorithmStep step); + +const std::array kAlgorithmAllSteps = { + AlgorithmStep::Init, + AlgorithmStep::Configure, + AlgorithmStep::Queue, + AlgorithmStep::Prepare, + AlgorithmStep::Process +}; + +struct AlgorithmDependency { + enum Type { + /* Step on target needs to come first */ + Before = 0x01, + /* Target must exist */ + Exist = 0x02 + }; + + std::string target; + AlgorithmStep step; + unsigned type = 0; + + bool operator<(const AlgorithmDependency &other) const + { + return (target < other.target) || + ((target == other.target) && + ((step < other.step) || + ((step == other.step) && + (type < other.type)))); + } +}; + +const std::set kNoDependencies = {}; + template class AlgorithmFactoryBase; @@ -63,6 +106,11 @@ public: { } + virtual const std::set &dependencies() const + { + return kNoDependencies; + } + const std::string &name() const { return name_; diff --git a/src/ipa/libipa/module.h b/src/ipa/libipa/module.h index 5644458bdf3d..cb2176dc37a6 100644 --- a/src/ipa/libipa/module.h +++ b/src/ipa/libipa/module.h @@ -7,8 +7,10 @@ #pragma once +#include #include #include +#include #include #include @@ -35,6 +37,7 @@ public: using Config = _Config; using Params = _Params; using Stats = _Stats; + using AlgorithmT = Algorithm; virtual ~Module() {} @@ -43,25 +46,76 @@ public: return algorithms_; } + const std::vector *> &algorithmsOrderedForStep(AlgorithmStep step) const + { + return algorithmsOrderedForStep_.at(step); + } + int createAlgorithms(Context &context, const ValueNode &algorithms) { const auto &list = algorithms.asList(); + std::map dataMap; + + utils::ScopeExitActions actions; + + actions += [this]() { + clearAlgorithms(); + }; for (const auto &[i, algo] : utils::enumerate(list)) { if (!algo.isDictionary()) { LOG(IPAModuleAlgo, Error) << "Invalid tuning data syntax for algorithm " << i; - algorithms_.clear(); return -EINVAL; } - int ret = createAlgorithm(context, algo); + const auto &[name, algoData] = *algo.asDict().begin(); + /* + * Optionally, algorithms can be disabled via the tuning file + * by including enabled: false as a parameter within the + * algorithm tuning data. This is not an error, so we return 0. + */ + if (!algoData["enabled"].get(true)) { + LOG(IPAModuleAlgo, Info) + << "Algorithm '" << name << "' disabled via tuning file"; + return 0; + } + + std::unique_ptr> instance = createAlgorithm(name); + if (!instance) { + LOG(IPAModuleAlgo, Error) + << "Algorithm '" << name << "' not found"; + return -EINVAL; + } + + algorithms_.push_back(std::move(instance)); + + LOG(IPAModuleAlgo, Debug) + << "Instantiated algorithm '" << name << "'"; + + dataMap[algorithms_.back().get()] = &algoData; + } + + for (auto step : kAlgorithmAllSteps) { + std::vector algos; + int ret = createOrderedList(algos, step); + if (ret) + return ret; + + algorithmsOrderedForStep_[step] = std::move(algos); + } + + for (const auto &a : algorithmsOrderedForStep(AlgorithmStep::Init)) { + int ret = a->init(context, *dataMap[a]); if (ret) { - algorithms_.clear(); + LOG(IPAModuleAlgo, Error) + << "Algorithm failed to initialize"; return ret; } } + actions.release(); + return 0; } @@ -71,42 +125,101 @@ public: } private: - int createAlgorithm(Context &context, const ValueNode &data) + int createOrderedList(std::vector *> &orderedList, + AlgorithmStep step) { - const auto &[name, algoData] = *data.asDict().begin(); + using Algo = Algorithm; + std::set handledAlgorithms; - /* - * Optionally, algorithms can be disabled via the tuning file - * by including enabled: false as a parameter within the - * algorithm tuning data. This is not an error, so we return 0. - */ - if (!algoData["enabled"].get(true)) { - LOG(IPAModuleAlgo, Info) - << "Algorithm '" << name << "' disabled via tuning file"; - return 0; + orderedList.clear(); + + for (const auto &a : algorithms()) { + std::vector resolutionStack; + int ret = addToOrderedList(a.get(), orderedList, + resolutionStack, + step); + if (ret) + return ret; } - std::unique_ptr> algo = createAlgorithm(name); - if (!algo) { - LOG(IPAModuleAlgo, Error) - << "Algorithm '" << name << "' not found"; - return -EINVAL; - } - - int ret = algo->init(context, algoData); - if (ret) { - LOG(IPAModuleAlgo, Error) - << "Algorithm '" << name << "' failed to initialize"; - return ret; - } - - LOG(IPAModuleAlgo, Debug) - << "Instantiated algorithm '" << name << "'"; - - algorithms_.push_back(std::move(algo)); return 0; } + int addToOrderedList(Algorithm *algo, + std::vector *> &orderedList, + std::vector &resolutionStack, + AlgorithmStep step) + { + /* nothing to do if it was added already due to dependency resolution */ + if (std::find(orderedList.begin(), orderedList.end(), algo) != orderedList.end()) + return 0; + + auto &deps = algo->dependencies(); + + /* First add the dependent ones */ + for (auto &dep : deps) { + if (dep.step != step) + continue; + + if (!(dep.type & (AlgorithmDependency::Exist | AlgorithmDependency::Before))) { + LOG(IPAModuleAlgo, Error) + << "Unknown dependency flags '" + << dep.type + << "' on dependency '" << dep.target; + return -EINVAL; + } + + if (std::find(resolutionStack.begin(), resolutionStack.end(), dep.target) != resolutionStack.end()) { + LOG(IPAModuleAlgo, Info) + << "Circular dependency detected: " + << dep.target; + return -EINVAL; + } + + /* Find the target */ + auto iter = std::find_if(algorithms_.begin(), algorithms_.end(), + [&](auto &a) { return a->name() == dep.target; }); + if (iter == algorithms_.end()) { + if (dep.type & AlgorithmDependency::Exist) { + LOG(IPAModuleAlgo, Error) + << "Algorithm '" << dep.target + << "' is listed as dependency but not instantiated"; + return -EINVAL; + } + + if (dep.type & AlgorithmDependency::Before) { + LOG(IPAModuleAlgo, Debug) + << "Algorithm '" << dep.target + << "' is listed as optional dependency but not instantiated"; + continue; + } + + ASSERT(false); + } + + Algorithm *dependency = iter->get(); + if (std::find(orderedList.begin(), orderedList.end(), dependency) == orderedList.end()) { + resolutionStack.push_back(dep.target); + int ret = addToOrderedList(dependency, + orderedList, + resolutionStack, + step); + if (ret) + return ret; + } + } + + orderedList.push_back(algo); + LOG(IPAModuleAlgo, Debug) << "Step " << step << " add algo " << algo->name(); + return 0; + } + + void clearAlgorithms() + { + algorithmsOrderedForStep_.clear(); + algorithms_.clear(); + } + static std::unique_ptr> createAlgorithm(const std::string &name) { for (const AlgorithmFactoryBase *factory : factories()) { @@ -129,6 +242,7 @@ private: } std::list>> algorithms_; + std::map> algorithmsOrderedForStep_; }; } /* namespace ipa */ diff --git a/src/ipa/rkisp1/rkisp1.cpp b/src/ipa/rkisp1/rkisp1.cpp index cefaf40aee63..add2e45cfa36 100644 --- a/src/ipa/rkisp1/rkisp1.cpp +++ b/src/ipa/rkisp1/rkisp1.cpp @@ -30,6 +30,7 @@ #include "algorithms/algorithm.h" #include "libipa/agc.h" +#include "libipa/algorithm.h" #include "ipa_context.h" #include "params.h" @@ -237,8 +238,8 @@ int IPARkISP1::configure(const IPAConfigInfo &ipaConfig, return format.colourEncoding == PixelFormatInfo::ColourEncodingRAW; }); - for (const auto &a : algorithms()) { - Algorithm *algo = static_cast(a.get()); + for (const auto &a : algorithmsOrderedForStep(AlgorithmStep::Configure)) { + Algorithm *algo = static_cast(a); /* Disable algorithms that don't support raw formats. */ algo->disabled_ = context_.configuration.raw && !algo->supportsRaw_; @@ -290,8 +291,8 @@ void IPARkISP1::queueRequest(const uint32_t frame, const ControlList &controls) IPAFrameContext &frameContext = context_.frameContexts.alloc(frame); context_.debugMetadata.enableByControl(controls); - for (const auto &a : algorithms()) { - Algorithm *algo = static_cast(a.get()); + for (const auto &a : algorithmsOrderedForStep(AlgorithmStep::Queue)) { + Algorithm *algo = static_cast(a); if (algo->disabled_) continue; algo->queueRequest(context_, frame, frameContext, controls); @@ -305,8 +306,9 @@ void IPARkISP1::computeParams(const uint32_t frame, const uint32_t bufferId) RkISP1Params params(context_.configuration.paramFormat, mappedBuffers_.at(bufferId).planes()[0]); - for (const auto &algo : algorithms()) + for (const auto &algo : algorithmsOrderedForStep(AlgorithmStep::Prepare)) { algo->prepare(context_, frame, frameContext, ¶ms); + } paramsComputed.emit(frame, params.bytesused()); } @@ -330,8 +332,8 @@ void IPARkISP1::processStats(const uint32_t frame, const uint32_t bufferId, ControlList metadata(controls::controls); - for (const auto &a : algorithms()) { - Algorithm *algo = static_cast(a.get()); + for (const auto &a : algorithmsOrderedForStep(AlgorithmStep::Process)) { + Algorithm *algo = static_cast(a); if (algo->disabled_) continue; algo->process(context_, frame, frameContext, stats, metadata);