From patchwork Mon Jun 20 01:42:56 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 16273 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 20B5ABE173 for ; Mon, 20 Jun 2022 01:43:33 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 6A3D365641; Mon, 20 Jun 2022 03:43:30 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1655689410; bh=rIw0/l9qlrC1FD0Xq8PBMm5KK4qQtlgKRmBJ8tSg8Eg=; 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=SzdwS8VN0b28sexQlTKqkquVGBmPA5o8vbBYMGojINeKufDZdl3bnBKQ4e7vzHl5X hjeg5z1/BTLnJj3a69GtJU/uubguD8C4g/5EvDbfl43ILxrnSqLJLrEH94rFQsHl0Q zHTxzwbo85BT11P4akV8ZcnIjID8rK4xnpHA9IQAEXYmH8nP8X/ypzIs7cAjQrL9xZ bs4DTxukLtuFfDK2mnqFDaI/wy/zU0v3aRSEgJZp7qeFjG/3vKeAhUGiCz09GMnmX0 nHQgMy8XSUFLP3UiwXCj3UcfS3MWHzElc78YxyH1VG7aTfSNPmKAIFq4MANJTjKWUt 6iyGVXi2hcKKg== Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 48309601F1 for ; Mon, 20 Jun 2022 03:43:26 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="q1hOo3VJ"; 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 CE42425E; Mon, 20 Jun 2022 03:43:25 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1655689406; bh=rIw0/l9qlrC1FD0Xq8PBMm5KK4qQtlgKRmBJ8tSg8Eg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=q1hOo3VJzQWvpJ7PNKKGw1Kn5qw+CFPjfbHGRQGmES6cLBN0IkhbEjdz4h/JAxv1T 3BNX9gwxjfOOL7ffX48I8GRIlOt1ezUWu8kYgCcbGhL8tI3wQMStf43zOB7iF7seSE 0DxWnV9QsQiDo58NBU2GTgseyjmXZ4QF5hxSOs/U= To: libcamera-devel@lists.libcamera.org Date: Mon, 20 Jun 2022 04:42:56 +0300 Message-Id: <20220620014305.26778-4-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 03/12] ipa: libipa: algorithm: Add an algorithm registration mechanism 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" In order to allow dynamic instantiation of algorithms based on tuning data files, add a mechanism to register algorithms with the IPA module. The implementation relies on an AlgorithmFactory class and a registration macro, similar to the pipeline handler registration mechanism. The main difference is that the algorithm registration and instantiation are implemented in the Module class instead of the AlgorithmFactory class, making the factory an internal implementation detail. Signed-off-by: Laurent Pinchart Reviewed-by: Paul Elder --- src/ipa/libipa/algorithm.cpp | 54 ++++++++++++++++++++++++++++++++++++ src/ipa/libipa/algorithm.h | 41 ++++++++++++++++++++++++++- src/ipa/libipa/module.cpp | 24 ++++++++++++++++ src/ipa/libipa/module.h | 33 ++++++++++++++++++++++ 4 files changed, 151 insertions(+), 1 deletion(-) diff --git a/src/ipa/libipa/algorithm.cpp b/src/ipa/libipa/algorithm.cpp index 2df91e5d8fed..6e0bba56ecb2 100644 --- a/src/ipa/libipa/algorithm.cpp +++ b/src/ipa/libipa/algorithm.cpp @@ -32,6 +32,11 @@ namespace ipa { * argument. */ +/** + * \typedef Algorithm::Module + * \brief The IPA module type for this class of algorithms + */ + /** * \fn Algorithm::configure() * \brief Configure the Algorithm given an IPAConfigInfo @@ -94,6 +99,55 @@ namespace ipa { * such that the algorithms use up to date state as required. */ +/** + * \class AlgorithmFactory + * \brief Registration of Algorithm classes and creation of instances + * + * To facilitate instantiation of Algorithm classes, the AlgorithmFactory class + * implements auto-registration of algorithms with the IPA Module class. Each + * Algorithm subclass shall register itself using the REGISTER_IPA_ALGORITHM() + * macro, which will create a corresponding instance of an AlgorithmFactory + * subclass and register it with the IPA Module. + */ + +/** + * \fn AlgorithmFactory::AlgorithmFactory() + * \brief Construct an algorithm factory + * \param[in] name Name of the algorithm class + * + * Creating an instance of the factory automatically registers is with the IPA + * Module class, enabling creation of algorithm instances through + * Module::createAlgorithm(). + * + * The factory \a name identifies the algorithm and shall be unique. + */ + +/** + * \fn AlgorithmFactory::name() + * \brief Retrieve the factory name + * \return The factory name + */ + +/** + * \fn AlgorithmFactory::create() + * \brief Create an instance of the Algorithm corresponding to the factory + * + * This virtual function is implemented by the REGISTER_IPA_ALGORITHM() + * macro. It creates an algorithm instance. + * + * \return A pointer to a newly constructed instance of the Algorithm subclass + * corresponding to the factory + */ + +/** + * \def REGISTER_IPA_ALGORITHM + * \brief Register an algorithm with the IPA module + * \param[in] algorithm Class name of Algorithm derived class to register + * + * Register an Algorithm subclass with the IPA module to make it available for + * instantiation through Module::createAlgorithm(). + */ + } /* namespace ipa */ } /* namespace libcamera */ diff --git a/src/ipa/libipa/algorithm.h b/src/ipa/libipa/algorithm.h index fd2ffcfbc900..356c094976d4 100644 --- a/src/ipa/libipa/algorithm.h +++ b/src/ipa/libipa/algorithm.h @@ -6,14 +6,19 @@ */ #pragma once +#include +#include + namespace libcamera { namespace ipa { -template +template class Algorithm { public: + using Module = _Module; + virtual ~Algorithm() {} virtual int configure([[maybe_unused]] typename Module::Context &context, @@ -34,6 +39,40 @@ public: } }; +template +class AlgorithmFactory +{ +public: + AlgorithmFactory(const char *name) + : name_(name) + { + Module::registerAlgorithm(this); + } + + virtual ~AlgorithmFactory() = default; + + const std::string &name() const { return name_; } + + virtual std::unique_ptr> create() const = 0; + +private: + std::string name_; +}; + +#define REGISTER_IPA_ALGORITHM(algorithm) \ +class algorithm##Factory final : public AlgorithmFactory \ +{ \ +public: \ + algorithm##Factory() : AlgorithmFactory(#algorithm) {} \ + \ + std::unique_ptr> create() const \ + { \ + return std::make_unique(); \ + } \ +}; \ + \ +static algorithm##Factory global_##algorithm##Factory; + } /* namespace ipa */ } /* namespace libcamera */ diff --git a/src/ipa/libipa/module.cpp b/src/ipa/libipa/module.cpp index d03cc8ef03ed..451614fd04da 100644 --- a/src/ipa/libipa/module.cpp +++ b/src/ipa/libipa/module.cpp @@ -75,6 +75,30 @@ namespace ipa { * \brief The type of the IPA statistics and ISP results */ +/** + * \fn Module::createAlgorithm() + * \brief Create an instance of an Algorithm by name + * \param[in] name The algorithm name + * + * This function is the entry point to algorithm instantiation for the IPA + * module. It creates and returns an instance of an algorithm identified by its + * \a name. If no such algorithm exists, the function returns nullptr. + * + * To make an algorithm available to the IPA module, it shall be registered with + * the REGISTER_IPA_ALGORITHM() macro. + * + * \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 c4d778120408..f30fc33711bb 100644 --- a/src/ipa/libipa/module.h +++ b/src/ipa/libipa/module.h @@ -7,6 +7,12 @@ #pragma once +#include +#include +#include + +#include "algorithm.h" + namespace libcamera { namespace ipa { @@ -23,6 +29,33 @@ public: using Stats = _Stats; virtual ~Module() {} + + static std::unique_ptr> createAlgorithm(const std::string &name) + { + for (const AlgorithmFactory *factory : factories()) { + if (factory->name() == name) + return factory->create(); + } + + return nullptr; + } + + static void registerAlgorithm(AlgorithmFactory *factory) + { + factories().push_back(factory); + } + +private: + static std::vector *> &factories() + { + /* + * The static factories map is defined inside the function to ensure + * it gets initialized on first use, without any dependency on + * link order. + */ + static std::vector *> factories; + return factories; + } }; } /* namespace ipa */