From patchwork Mon Oct 3 21:21:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 17518 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 A5F94C3286 for ; Mon, 3 Oct 2022 21:21:45 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 4FC6C60A86; Mon, 3 Oct 2022 23:21:45 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1664832105; bh=Y+UrL7/BUdya+hm4IVrhHl5+vkkku68PkiPAjiycm0c=; 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=LgdxIbTT0mCLfMbDcwaiM+jFNceeZ4A1qpdD4vbOmTSy5rkjDIkx+0ZHQCgKTFiNk kBj9y+ysZ4gaP+tkR2P9RUfCYBkkTdNM0JItgDwNMkZvLyM20OMQON3+kgCrbGp9IK PsQyFaQqh/pNq3oYoi9OERwBs0RedbnP9gSrLmmuJFa4f8Tg27tBn3T70Hw4Dfoql8 spj3cIkuz0jtklty+jWyJ6KuUrSxIUUQ75BP0ERLrpZIZ9woNkLAr0Q+0UHy6Ug0zI 3gP4/RGyu0KwiYcwpACtgTJeYfdiDfoZzlSUsNvTr4r0iz/wGbwQqn1Jcharp558eN MSe2lJnqaiNuA== 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 78EE060A73 for ; Mon, 3 Oct 2022 23:21:43 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="RVgrMY9p"; dkim-atps=neutral Received: from pendragon.ideasonboard.com (62-78-145-57.bb.dnainternet.fi [62.78.145.57]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 053A4519 for ; Mon, 3 Oct 2022 23:21:42 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1664832103; bh=Y+UrL7/BUdya+hm4IVrhHl5+vkkku68PkiPAjiycm0c=; h=From:To:Subject:Date:In-Reply-To:References:From; b=RVgrMY9pISbl6RsEgejJfrJzD9MxtG2xmr80LO+wiYxLWl4648ZuFE/Ft5+RxXanj eLDJfrSCi1Hw6nLOQ2L36B2OfyII9NjXRCnZ1khhSqwyWMqr97bTGOqbx0drQUVHYv Z+RjNHFYtwvfFxYNvXU+OqAlEmFzJFih5BwyTniA= To: libcamera-devel@lists.libcamera.org Date: Tue, 4 Oct 2022 00:21:28 +0300 Message-Id: <20221003212128.32429-9-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20221003212128.32429-1-laurent.pinchart@ideasonboard.com> References: <20221003212128.32429-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 8/8] libcamera: pipeline_handler: Implement factories through class templates 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" The REGISTER_PIPELINE_HANDLER() macro defines a class type that inherits from the PipelineHandlerFactory class, and implements a constructor and a createInstance() function. Replace the code generation through macro with the C++ equivalent, a class template, as done in libipa with the Algorithm and CameraSensorHelper factories. Signed-off-by: Laurent Pinchart Reviewed-by: Xavier Roumegue Reviewed-by: Jacopo Mondi --- include/libcamera/internal/pipeline_handler.h | 44 +++++++------ src/libcamera/camera_manager.cpp | 6 +- src/libcamera/pipeline_handler.cpp | 65 ++++++++++++------- test/ipa/ipa_interface_test.cpp | 6 +- 4 files changed, 71 insertions(+), 50 deletions(-) diff --git a/include/libcamera/internal/pipeline_handler.h b/include/libcamera/internal/pipeline_handler.h index ad74dc823290..b6139a88d421 100644 --- a/include/libcamera/internal/pipeline_handler.h +++ b/include/libcamera/internal/pipeline_handler.h @@ -95,23 +95,23 @@ private: Mutex lock_; unsigned int useCount_ LIBCAMERA_TSA_GUARDED_BY(lock_); - friend class PipelineHandlerFactory; + friend class PipelineHandlerFactoryBase; }; -class PipelineHandlerFactory +class PipelineHandlerFactoryBase { public: - PipelineHandlerFactory(const char *name); - virtual ~PipelineHandlerFactory() = default; + PipelineHandlerFactoryBase(const char *name); + virtual ~PipelineHandlerFactoryBase() = default; std::shared_ptr create(CameraManager *manager) const; const std::string &name() const { return name_; } - static std::vector &factories(); + static std::vector &factories(); private: - static void registerType(PipelineHandlerFactory *factory); + static void registerType(PipelineHandlerFactoryBase *factory); virtual std::unique_ptr createInstance(CameraManager *manager) const = 0; @@ -119,19 +119,23 @@ private: std::string name_; }; -#define REGISTER_PIPELINE_HANDLER(handler) \ -class handler##Factory final : public PipelineHandlerFactory \ -{ \ -public: \ - handler##Factory() : PipelineHandlerFactory(#handler) {} \ - \ -private: \ - std::unique_ptr \ - createInstance(CameraManager *manager) const \ - { \ - return std::make_unique(manager); \ - } \ -}; \ -static handler##Factory global_##handler##Factory; +template +class PipelineHandlerFactory final : public PipelineHandlerFactoryBase +{ +public: + PipelineHandlerFactory(const char *name) + : PipelineHandlerFactoryBase(name) + { + } + + std::unique_ptr + createInstance(CameraManager *manager) const override + { + return std::make_unique<_PipelineHandler>(manager); + } +}; + +#define REGISTER_PIPELINE_HANDLER(handler) \ +static PipelineHandlerFactory global_##handler##Factory(#handler); } /* namespace libcamera */ diff --git a/src/libcamera/camera_manager.cpp b/src/libcamera/camera_manager.cpp index 2c3f2d86c469..2c100c24af4d 100644 --- a/src/libcamera/camera_manager.cpp +++ b/src/libcamera/camera_manager.cpp @@ -142,10 +142,10 @@ void CameraManager::Private::createPipelineHandlers() * file and only fallback on all handlers if there is no * configuration file. */ - const std::vector &factories = - PipelineHandlerFactory::factories(); + const std::vector &factories = + PipelineHandlerFactoryBase::factories(); - for (const PipelineHandlerFactory *factory : factories) { + for (const PipelineHandlerFactoryBase *factory : factories) { LOG(Camera, Debug) << "Found registered pipeline handler '" << factory->name() << "'"; diff --git a/src/libcamera/pipeline_handler.cpp b/src/libcamera/pipeline_handler.cpp index 488dd8d32cdf..44f6fc531ad7 100644 --- a/src/libcamera/pipeline_handler.cpp +++ b/src/libcamera/pipeline_handler.cpp @@ -64,8 +64,7 @@ LOG_DEFINE_CATEGORY(Pipeline) * * In order to honour the std::enable_shared_from_this<> contract, * PipelineHandler instances shall never be constructed manually, but always - * through the PipelineHandlerFactory::create() function implemented by the - * respective factories. + * through the PipelineHandlerFactoryBase::create() function. */ PipelineHandler::PipelineHandler(CameraManager *manager) : manager_(manager), useCount_(0) @@ -643,27 +642,25 @@ void PipelineHandler::disconnect() */ /** - * \class PipelineHandlerFactory - * \brief Registration of PipelineHandler classes and creation of instances + * \class PipelineHandlerFactoryBase + * \brief Base class for pipeline handler factories * - * To facilitate discovery and instantiation of PipelineHandler classes, the - * PipelineHandlerFactory class maintains a registry of pipeline handler - * classes. Each PipelineHandler subclass shall register itself using the - * REGISTER_PIPELINE_HANDLER() macro, which will create a corresponding - * instance of a PipelineHandlerFactory subclass and register it with the - * static list of factories. + * The PipelineHandlerFactoryBase class is the base of all specializations of + * the PipelineHandlerFactory class template. It implements the factory + * registration, maintains a registry of factories, and provides access to the + * registered factories. */ /** - * \brief Construct a pipeline handler factory + * \brief Construct a pipeline handler factory base * \param[in] name Name of the pipeline handler class * - * Creating an instance of the factory registers is with the global list of + * Creating an instance of the factory base registers is with the global list of * factories, accessible through the factories() function. * * The factory \a name is used for debug purpose and shall be unique. */ -PipelineHandlerFactory::PipelineHandlerFactory(const char *name) +PipelineHandlerFactoryBase::PipelineHandlerFactoryBase(const char *name) : name_(name) { registerType(this); @@ -676,7 +673,7 @@ PipelineHandlerFactory::PipelineHandlerFactory(const char *name) * \return A shared pointer to a new instance of the PipelineHandler subclass * corresponding to the factory */ -std::shared_ptr PipelineHandlerFactory::create(CameraManager *manager) const +std::shared_ptr PipelineHandlerFactoryBase::create(CameraManager *manager) const { std::unique_ptr handler = createInstance(manager); handler->name_ = name_.c_str(); @@ -684,7 +681,7 @@ std::shared_ptr PipelineHandlerFactory::create(CameraManager *m } /** - * \fn PipelineHandlerFactory::name() + * \fn PipelineHandlerFactoryBase::name() * \brief Retrieve the factory name * \return The factory name */ @@ -696,9 +693,10 @@ std::shared_ptr PipelineHandlerFactory::create(CameraManager *m * The caller is responsible to guarantee the uniqueness of the pipeline handler * name. */ -void PipelineHandlerFactory::registerType(PipelineHandlerFactory *factory) +void PipelineHandlerFactoryBase::registerType(PipelineHandlerFactoryBase *factory) { - std::vector &factories = PipelineHandlerFactory::factories(); + std::vector &factories = + PipelineHandlerFactoryBase::factories(); factories.push_back(factory); } @@ -707,26 +705,45 @@ void PipelineHandlerFactory::registerType(PipelineHandlerFactory *factory) * \brief Retrieve the list of all pipeline handler factories * \return the list of pipeline handler factories */ -std::vector &PipelineHandlerFactory::factories() +std::vector &PipelineHandlerFactoryBase::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; + static std::vector factories; return factories; } +/** + * \class PipelineHandlerFactory + * \brief Registration of PipelineHandler classes and creation of instances + * \tparam _PipelineHandler The pipeline handler class type for this factory + * + * To facilitate discovery and instantiation of PipelineHandler classes, the + * PipelineHandlerFactory class implements auto-registration of pipeline + * handlers. Each PipelineHandler subclass shall register itself using the + * REGISTER_PIPELINE_HANDLER() macro, which will create a corresponding + * instance of a PipelineHandlerFactory and register it with the static list of + * factories. + */ + +/** + * \fn PipelineHandlerFactory::PipelineHandlerFactory(const char *name) + * \brief Construct a pipeline handler factory + * \param[in] name Name of the pipeline handler class + * + * Creating an instance of the factory registers is with the global list of + * factories, accessible through the factories() function. + * + * The factory \a name is used for debug purpose and shall be unique. + */ + /** * \fn PipelineHandlerFactory::createInstance() const * \brief Create an instance of the PipelineHandler corresponding to the factory * \param[in] manager The camera manager - * - * This virtual function is implemented by the REGISTER_PIPELINE_HANDLER() - * macro. It creates a pipeline handler instance associated with the camera - * \a manager. - * * \return A unique pointer to a newly constructed instance of the * PipelineHandler subclass corresponding to the factory */ diff --git a/test/ipa/ipa_interface_test.cpp b/test/ipa/ipa_interface_test.cpp index a629abc28da7..6b93e976a587 100644 --- a/test/ipa/ipa_interface_test.cpp +++ b/test/ipa/ipa_interface_test.cpp @@ -52,9 +52,9 @@ protected: ipaManager_ = make_unique(); /* Create a pipeline handler for vimc. */ - const std::vector &factories = - PipelineHandlerFactory::factories(); - for (const PipelineHandlerFactory *factory : factories) { + const std::vector &factories = + PipelineHandlerFactoryBase::factories(); + for (const PipelineHandlerFactoryBase *factory : factories) { if (factory->name() == "PipelineHandlerVimc") { pipe_ = factory->create(nullptr); break;