From patchwork Mon Jun 20 01:42:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 16275 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 2A215BE173 for ; Mon, 20 Jun 2022 01:43:35 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 7B6776564D; Mon, 20 Jun 2022 03:43:33 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1655689413; bh=iiTYs60suiRgZIa1iHO3GmUnotFsLAIBsnwonLylSjA=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=czDUGEv2lNsOQzyzpiMoqDjDgeC5vvX3o8QMYY7HMlkB4xZTv5dwJZ0Jcaw1OHAJm hb5QDgVx0RXeHrGQMJqJ8N8MyF1//78lbzMAdDzoSq4bT+MmoSOeyG6Paxu8LZ5BzW 2dyAbLzYTA1zGY9WF9fk/rT8tal9F5rQF6Qr4+Du3GOyaMXdvLceNbp3n8INTc7gvs 47Bo6bGt6k9UJ1S32yFx9munO7uvPHmJGe1Mfn38jG/ljtGwuU27/4OY7avVJGmIWF hwBPvRaTKv/eNv11NrBV7Rn3HcQwababUTN2QL6YnS7f0v7j6kVVIaXMfOE85wVs8H 3aJcxgWuCkdCQ== Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 4529E6563B for ; Mon, 20 Jun 2022 03:43:27 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="lB6NE2jw"; dkim-atps=neutral Received: from pendragon.lan (62-78-145-57.bb.dnainternet.fi [62.78.145.57]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id C530A25E; Mon, 20 Jun 2022 03:43:26 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1655689407; bh=iiTYs60suiRgZIa1iHO3GmUnotFsLAIBsnwonLylSjA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lB6NE2jwKOgKqjigca1uug5aCUL2yFTHT9eWYqNRcf2A3yqaefY7K6or9kwi3zfjC LsViq6/8kbBuh1HNaJ7XYV026nZw9J+Fassfm7BTK9T+HZtKk0lykZKMAtXbDhWCM6 QuHJULMVoupO6LnClD4+dvXlkUqROvkgTpPHVFic= To: libcamera-devel@lists.libcamera.org Date: Mon, 20 Jun 2022 04:42:58 +0300 Message-Id: <20220620014305.26778-6-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220620014305.26778-1-laurent.pinchart@ideasonboard.com> References: <20220620014305.26778-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v4 05/12] ipa: libipa: module: Add support for instantiation from YAML 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: , X-Patchwork-Original-From: Laurent Pinchart via libcamera-devel From: Laurent Pinchart Reply-To: Laurent Pinchart Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Add a Module::createAlgorithms() function to instantiate algorithms from a YamlObject. The instantiated algorithms are stored in a private member variable list, exposed through the Module::algorithms() function. Signed-off-by: Laurent Pinchart Reviewed-by: Paul Elder --- src/ipa/libipa/module.cpp | 42 ++++++++++++++++------ src/ipa/libipa/module.h | 73 +++++++++++++++++++++++++++++++++++---- 2 files changed, 99 insertions(+), 16 deletions(-) diff --git a/src/ipa/libipa/module.cpp b/src/ipa/libipa/module.cpp index 451614fd04da..7735210444b6 100644 --- a/src/ipa/libipa/module.cpp +++ b/src/ipa/libipa/module.cpp @@ -14,6 +14,8 @@ namespace libcamera { +LOG_DEFINE_CATEGORY(IPAModuleAlgo) + /** * \brief The IPA namespace * @@ -76,7 +78,36 @@ namespace ipa { */ /** - * \fn Module::createAlgorithm() + * \fn Module::algorithms() + * \brief Retrieve the list of instantiated algorithms + * \return The list of instantiated algorithms + */ + +/** + * \fn Module::createAlgorithms() + * \brief Create algorithms from YAML configuration data + * \param[in] context The IPA context + * \param[in] algorithms Algorithms configuration data as a parsed YamlObject + * + * This function iterates over the list of \a algorithms parsed from the YAML + * configuration file, and instantiates and initializes the corresponding + * algorithms. The configuration data is expected to be correct, any error + * causes the function to fail and return immediately. + * + * \return 0 on success, or a negative error code on failure + */ + +/** + * \fn Module::registerAlgorithm() + * \brief Add an algorithm factory class to the list of available algorithms + * \param[in] factory Factory to use to construct the algorithm + * + * This function registers an algorithm factory. It is meant to be called by the + * AlgorithmFactory constructor only. + */ + +/** + * \fn Module::createAlgorithm(const std::string &name) * \brief Create an instance of an Algorithm by name * \param[in] name The algorithm name * @@ -90,15 +121,6 @@ namespace ipa { * \return A new instance of the Algorithm subclass corresponding to the \a name */ -/** - * \fn Module::registerAlgorithm() - * \brief Add an algorithm factory class to the list of available algorithms - * \param[in] factory Factory to use to construct the algorithm - * - * This function registers an algorithm factory. It is meant to be called by the - * AlgorithmFactory constructor only. - */ - } /* namespace ipa */ } /* namespace libcamera */ diff --git a/src/ipa/libipa/module.h b/src/ipa/libipa/module.h index f30fc33711bb..00d5785e1aa0 100644 --- a/src/ipa/libipa/module.h +++ b/src/ipa/libipa/module.h @@ -7,14 +7,22 @@ #pragma once +#include #include #include #include +#include +#include + +#include "libcamera/internal/yaml_parser.h" + #include "algorithm.h" namespace libcamera { +LOG_DECLARE_CATEGORY(IPAModuleAlgo) + namespace ipa { template>> &algorithms() const + { + return algorithms_; + } + + int createAlgorithms(Context &context, const YamlObject &algorithms) + { + const auto &list = algorithms.asList(); + + for (const auto &[i, algo] : utils::enumerate(list)) { + if (!algo.isDictionary()) { + LOG(IPAModuleAlgo, Error) + << "Invalid YAML syntax for algorithm " << i; + algorithms_.clear(); + return -EINVAL; + } + + int ret = createAlgorithm(context, algo); + if (ret) { + algorithms_.clear(); + return ret; + } + } + + return 0; + } + + static void registerAlgorithm(AlgorithmFactory *factory) + { + factories().push_back(factory); + } + +private: + int createAlgorithm(Context &context, const YamlObject &data) + { + const auto &[name, algoData] = *data.asDict().begin(); + 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; + } + static std::unique_ptr> createAlgorithm(const std::string &name) { for (const AlgorithmFactory *factory : factories()) { @@ -40,12 +105,6 @@ public: return nullptr; } - static void registerAlgorithm(AlgorithmFactory *factory) - { - factories().push_back(factory); - } - -private: static std::vector *> &factories() { /* @@ -56,6 +115,8 @@ private: static std::vector *> factories; return factories; } + + std::list>> algorithms_; }; } /* namespace ipa */