From patchwork Mon Oct 10 13:17:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xavier Roumegue X-Patchwork-Id: 17568 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 7ACB6C0DA4 for ; Mon, 10 Oct 2022 13:18:17 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 22B4962D63; Mon, 10 Oct 2022 15:18:17 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1665407897; bh=V4myELna32D2dvKewJ1RdEpheMNDl+02s0XfoGx2gR4=; 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=yFOFYdL77U+6KbjeKIZruqAL3UiSKFe3UwVITmBHLwk5+R7AVjsZrQmYsntodxpXA Atb3tNb+CJY2rIYFmt/MNVa7floD+uC7YAUuWygeEzWJksr7tEYOwcTaViO18TZlHk vNAJ7+Sn1ekNdbQaLt7WZrYtCyI7PPbrlJZ5/3V//nAdIVEe93OwT/udXacB2vcuMv ouRC8eQwst3DaXAZjrqWK43pYIAgtofev0vZspcRkIzausGFaDpTYVMiaoe0TijtYA 0/gEIelugM2FU7yLqZWlxPTW7ZbleZdeaeJZ5YFj3FHrmm2wgVtS3qFrSkZOJlBlte eUL0BUQAXE/uA== Received: from EUR05-VI1-obe.outbound.protection.outlook.com (mail-vi1eur05on2077.outbound.protection.outlook.com [40.107.21.77]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 7C38662D5C for ; Mon, 10 Oct 2022 15:18:15 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=NXP1.onmicrosoft.com header.i=@NXP1.onmicrosoft.com header.b="gzYQ1y2x"; dkim-atps=neutral ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=cGwgzefYOhzX+tlKUeBRbp1ilLxLTQJEalfZZwSgCGPtPrvZ8ZIbmvFW66a1NIFbSKRc1wxZbMRfd9o3f/m4RVdRdcruk44PZ/771U8YwjSDdAeSclytVHPiWyQOY5bEnwinPR6sNALVH8S0VnVrYz8/w/+FuIzpLcdxALMgGhUh0M83W56/K8mvYaayjNVDfywTIII1BipEsGVap3ttoK3hhjyf9fqf1gKf+FqyRhcUS/7nVKMNqbNS5WIMKHFv5bIkrxqPtpiHV7EBp+pIXIljWRzF0u93BMZoAddl4AMx0mPgBMXOEzS8nAp0LRO2YAYGElAA6UZgSekllgzPUQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=ee/yQdw6vxpXUg3VAftRTVN1GG8sGOCDnqD3ipIYtCg=; b=ORrWDJLk3YZQgb6A7i/sXIfUTjd7T0dJ11WEq9eaGZNnP/QP/Ew0/f2DXCjKt+gljW4fzGAELgaEWnOrAfYZ66D61MOcv7gWPBn9cNPONuX/M3dJH5Eh3xiYf5LT96FYPAXXGmquiBORtwXG1ZYd+YyiWdRIE605Gm/9e6pOSF6PyvClGuDLVN+lzFpa0VgIZWHaLXGFq/2OTGMg7XDnAsawhmQVycj0hFFLvZxamP6Q1Y2a5rhOIP51ZoEybHGpaffL+JEXriRuaMf/bNy+V09d5yn5zPszFKzVnJHyeV/YMWtrUhXHo3L8FBAOjrsX7s5fv6SVXmjuq88on7LQMQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oss.nxp.com; dmarc=pass action=none header.from=oss.nxp.com; dkim=pass header.d=oss.nxp.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=NXP1.onmicrosoft.com; s=selector2-NXP1-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=ee/yQdw6vxpXUg3VAftRTVN1GG8sGOCDnqD3ipIYtCg=; b=gzYQ1y2xtGkLMc5/BqimBZRLI5RJhcVyjm6QAMXzbpX1sMp8QZ5Aq61qOjkXUx7/LAHYD6B9cSzj74TP6PF2ShKF0tetyfGtHDGvmnum8sCTuU3ptmact39Rc074bNuJbfwKKKkRTNgHR8bQskZ7PGSWbABWlTVK3DCQBSLdmB0= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=oss.nxp.com; Received: from PAXPR04MB8703.eurprd04.prod.outlook.com (2603:10a6:102:21e::22) by PA4PR04MB7933.eurprd04.prod.outlook.com (2603:10a6:102:b9::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5676.34; Mon, 10 Oct 2022 13:18:13 +0000 Received: from PAXPR04MB8703.eurprd04.prod.outlook.com ([fe80::4f72:a35a:8c60:63f1]) by PAXPR04MB8703.eurprd04.prod.outlook.com ([fe80::4f72:a35a:8c60:63f1%6]) with mapi id 15.20.5709.015; Mon, 10 Oct 2022 13:18:13 +0000 To: jacopo@jmondi.org, laurent.pinchart@ideasonboard.com, libcamera-devel@lists.libcamera.org Date: Mon, 10 Oct 2022 15:17:43 +0200 Message-Id: <20221010131744.513261-2-xavier.roumegue@oss.nxp.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20221010131744.513261-1-xavier.roumegue@oss.nxp.com> References: <20221010131744.513261-1-xavier.roumegue@oss.nxp.com> X-ClientProxiedBy: BY5PR17CA0058.namprd17.prod.outlook.com (2603:10b6:a03:167::35) To PAXPR04MB8703.eurprd04.prod.outlook.com (2603:10a6:102:21e::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PAXPR04MB8703:EE_|PA4PR04MB7933:EE_ X-MS-Office365-Filtering-Correlation-Id: 3fbecb04-95b9-475e-c755-08daaac1e234 X-MS-Exchange-SharedMailbox-RoutingAgent-Processed: True X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: OUSxGis7P0mCtamrCGVs/g3LEMb6dRNCON9ArAQ/uO/cWQtZi8gykxsUWFUR6scqJ4iNREfJB67Q0j3rC1G5hScwVaENTi0ccnuDPX4k4erf3xfadrxTCJFxV+BhOWa+FY2zKu/tgT5E1graHqOgaAMVQyB0fKIgIYyh6Ft4nKhvKr812q88wqDDfbNTdgVlP1kNxrfgJ8AWicwa2zqOE2crD+hfX2XRxco4fp+yuxsr9U7Wk7ZzDmaKSbGY1bIr3jTi8axBtb3FvBdeYGqkZPrv9qrgP+pYVJVww5BeN3Ahsdxbn+jcv3lBmloKnAZIPeMIPIKcd0glblQzyTt3s9TQvcrWZBphB4WiaacEPKFu5zKpyBVUpr6IAH9GdFvqb4zttGM3p1JaRdwaHGT/+to06nVf1xc5zQG8w9hZWlb5NGmt+2TxKU6CRBp8iUx3BiLotKEF6ekFOMmHozKMsl+LPNXcfwMtkFwCtSl8JMoNWjN6ISDnalB9HoPN9ekrbdp0qgSGp9gk+639nxt9bSBlxsihkKTl3ZDnhhrRDQVth2+87WM5q0jGCuTkAFp0EGLnMlEaq2nNmiBw5Gh+ypCygx2bmu6V9gpNpzFNwKPcr2YiRxFGDQnIMd0z2+MfrQe/GoWA659/D5io6UgrnCCIU+DdPvVyc12NvB0f3fzci7OfC7H7csQLGm5MM2k5pGpaaRKV7R9GASWH+kUBnSmG1AIV4mgmnDyeqcvW8T5pO/nVCe3d/vpHd4LqfuJ56vLGlFyGwitztjbzHxZ2yXe3ocVXHSYJ1E/FcB3/5qI= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:PAXPR04MB8703.eurprd04.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230022)(4636009)(376002)(396003)(366004)(136003)(39860400002)(346002)(451199015)(66899015)(55236004)(66476007)(8676002)(5660300002)(66556008)(4326008)(66946007)(316002)(38350700002)(38100700002)(41300700001)(8936002)(30864003)(2906002)(6486002)(478600001)(6666004)(1076003)(186003)(6512007)(26005)(52116002)(83380400001)(6506007)(86362001)(2616005)(41533002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 1CzQ7i+H8DM7DOvUPw9Wdko0BT+5PVTtLqz382OgBJjfsmnUewP9XA9Befr4KrPip9Y8YjpnMbF6rTI/LlP+aQfWOtFFbFdUWbEppBEgwPp4KURnXFJMapngXWUCBY2oamTVfa+78HQ+rdWekMPat5VQMRuZRJtxI/BIMf//P4Cmvj1G39RiyT0jkVFLAoHlbjuS/gPJgxKv/9KlgHDloyDeKAS75El/VsDUPxRKBDkI8cRObfkMIYf/xWaU8mjaj1rGHPGYatq4FxvY8K5tRq2ExjGfXYebaYI8EQZ3jy7Gk0zjADcFUkiw3AZC/vrfeKJ2hFhNmuJkkETf8nVQRVcz990SBOWN5BoHROLk6FxVIntQuScpOH/A7YlOQgeuXGDX0IPQiDLktGXwrFo3++TN6O5c9e8lqm5Pcv93XGrD/RZ5zQFLm8CJusftI594DT0QigkhtWYJLLcREEbu1MwveVlsvG6Q1Fibr/d5uzedZcXTcZXEcW0J+sjkCKconrnDMumbi4rPsHhxVNSHWeFUcZDQaldtQhSpgmBMkzZ9x2W11toAt0Ur71EpYzLjfK/BHw1tbUi8sgh4QUtd3ux4m2cEjX5yTYqjDzvtR9hOiZZqf0mC+oa7lZAh6ZrP4XfEYNRQedPYFciI5/Q/M2f8Sctjr9bwq56Hpu+HEORE5w1EbDnUwxXFymtVvTui/vLeWygNarg5LGblHyenEszXxOWHzoyNj8KwFZUkd4fQcQfIjnOWLbuwzdVq+OmLyXNqMlhuQl5inG8nYlZolIDE9hsLRTNqaqlavh71/f7VwPlmwJFGxcmz6ROOiOVMSHttYvus4YPLqL1kqsAy2qUN0/6QcH/1cxtQ12Yt59KbsMIb1wlurtaeL4AA456eBjVoTSJ3ZYEC9ljSszq8rSqOnuMrdFhqnuoQe7Qqd+bzc0jDXBIHF8fEU6K1nOt+ZEua1inTL5+45Cz/x0/hIHyI1E3PX5xYeaqFuW176bc6lvon5Qm7qp4GwEIAX2X7P7lonJ1cIH6DGWhnsKx+N8eAiifk7mq2DWSJszbvKtNL5d8D05X2Awx1X5zJJxuQtKJjBwlYtqZqttTWsaoU+Hf+anvunpQUAmIsyg4p2frIHUPYgAwtaSdN9O6KQw4jQowekpreExvHIiwU/p/njxP9PhVVj/qlFIiCEmayR/WuagXy2kUQt3ramUDK/VfPWBamTqMrVGnGB5ShBY1fXn+AOLD3hX6k4LFbNq6Bj9DEsQKKRULAxFqFohyio2c+cnoZdxRW5Znr61gkrBG0e2Nhp0cf8IBsTTyNaOfdwwrRCMdgmvCJiLYqOe/MsxnW8yb4tOiCECoqSmdtey3baXNkkz6u5eAVa5LbohAlpR++OfQIpEgsNUpSGL2WukDWkxTg8MAXBbdgKVBQvHFuObUNopw8f0ydr+OqcLVUgtiGi72RpA/UNxNCP3gvebBoZ6N0nhVFWdJurQ0HtHZida3PHRnSHCuHAnm6JptE3gncE5LDwN7ZEJE88FENz8ahjSdH/suR1249t7iFrelQwFsVorBMEuiPlNwUBjN0XUFououAq/oi5BjjvhlsdYJP8Eas5rJ5ZYnbfwusXbe/ww== X-OriginatorOrg: oss.nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3fbecb04-95b9-475e-c755-08daaac1e234 X-MS-Exchange-CrossTenant-AuthSource: PAXPR04MB8703.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Oct 2022 13:18:13.0771 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 686ea1d3-bc2b-4c6f-a92c-d99c5c301635 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 6EIH65THl6ybXVd5IVNdoLNgBFwqT9/SgvfqMUpS6AxkBQsyHS9LGWjOeLDKS1nWZbBD6VygmBT8Fivd5CDSVQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PA4PR04MB7933 Subject: [libcamera-devel] [PATCH 1/2] libcamera: Declare generic converter interface 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: "Xavier Roumegue \(OSS\) via libcamera-devel" From: Xavier Roumegue Reply-To: "Xavier Roumegue \(OSS\)" Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" From: Xavier Roumegue Declare a converter Abstract Base Class intended to provide generic interfaces to hardware offering size and format conversion services on streams. This is mainly based on the public interfaces of the current converter class implementation found in the simple pipeline handler. The main change is the introduction of loadConfiguration() function which can be used by the concrete implementation to load hardware specific runtime parameters defined by the application. Signed-off-by: Xavier Roumegue --- include/libcamera/internal/converter.h | 108 ++++++++ include/libcamera/internal/meson.build | 1 + src/libcamera/converter.cpp | 337 +++++++++++++++++++++++++ src/libcamera/meson.build | 1 + 4 files changed, 447 insertions(+) create mode 100644 include/libcamera/internal/converter.h create mode 100644 src/libcamera/converter.cpp diff --git a/include/libcamera/internal/converter.h b/include/libcamera/internal/converter.h new file mode 100644 index 00000000..38c05ee9 --- /dev/null +++ b/include/libcamera/internal/converter.h @@ -0,0 +1,108 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2020, Laurent Pinchart + * Copyright 2022 NXP + * + * converter.h - Generic stream converter infrastructure + */ + +#pragma once + +#include +#include +#include +#include +#include + +#include +#include + +#include +#include + +namespace libcamera { + +class FrameBuffer; +class MediaDevice; +class Size; +class SizeRange; +struct StreamConfiguration; + +class Converter +{ +public: + Converter(MediaDevice *media); + virtual ~Converter(); + + virtual int loadConfiguration(const std::string &filename) = 0; + + virtual bool isValid() const = 0; + + virtual std::vector formats(PixelFormat input) = 0; + virtual SizeRange sizes(const Size &input) = 0; + + virtual std::tuple + strideAndFrameSize(const PixelFormat &pixelFormat, const Size &size) = 0; + + virtual int configure(const StreamConfiguration &inputCfg, + const std::vector> &outputCfg) = 0; + virtual int exportBuffers(unsigned int ouput, unsigned int count, + std::vector> *buffers) = 0; + + virtual int start() = 0; + virtual void stop() = 0; + + virtual int queueBuffers(FrameBuffer *input, + const std::map &outputs) = 0; + + Signal inputBufferReady; + Signal outputBufferReady; + + const std::string &deviceNode() const { return deviceNode_; }; + +private: + std::string deviceNode_; +}; + +class ConverterFactoryBase +{ +public: + ConverterFactoryBase(const std::string name, std::initializer_list aliases); + virtual ~ConverterFactoryBase() = default; + + const std::vector &aliases() const { return aliases_; } + + static std::unique_ptr create(MediaDevice *media); + static std::vector &factories(); + static std::vector names(); + +private: + LIBCAMERA_DISABLE_COPY_AND_MOVE(ConverterFactoryBase) + + static void registerType(ConverterFactoryBase *factory); + + virtual std::unique_ptr createInstance(MediaDevice *media) const = 0; + + std::string name_; + std::vector aliases_; +}; + +template +class ConverterFactory : public ConverterFactoryBase +{ +public: + ConverterFactory(const char *name, std::initializer_list aliases) + : ConverterFactoryBase(name, aliases) + { + } + + std::unique_ptr createInstance(MediaDevice *media) const override + { + return std::make_unique<_Converter>(media); + } +}; + +#define REGISTER_CONVERTER(name, converter, ...) \ + static ConverterFactory global_##converter##Factory(name, { __VA_ARGS__ }); + +} /* namespace libcamera */ diff --git a/include/libcamera/internal/meson.build b/include/libcamera/internal/meson.build index 7a780d48..8f50d755 100644 --- a/include/libcamera/internal/meson.build +++ b/include/libcamera/internal/meson.build @@ -19,6 +19,7 @@ libcamera_internal_headers = files([ 'camera_sensor_properties.h', 'control_serializer.h', 'control_validator.h', + 'converter.h', 'delayed_controls.h', 'device_enumerator.h', 'device_enumerator_sysfs.h', diff --git a/src/libcamera/converter.cpp b/src/libcamera/converter.cpp new file mode 100644 index 00000000..5e444f5f --- /dev/null +++ b/src/libcamera/converter.cpp @@ -0,0 +1,337 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright 2022 NXP + * + * converter.cpp - Generic Format converter interface + */ + +#include "libcamera/internal/converter.h" + +#include + +#include + +#include "libcamera/internal/media_device.h" + +/** + * \file internal/converter.h + * \brief Abstract converter + */ + +namespace libcamera { + +LOG_DEFINE_CATEGORY(Converter) + +/** + * \class Converter + * \brief Abstract Base Class for converter + * + * The Converter class is an Abstract Base Class defining the interfaces of + * converter implementations. + * + * Converters offers scaling and pixel formats conversion services on a input + * stream. The converter can output multiple streams with individual conversion + * parameters from the same input stream. + */ + +/** + * \brief Construct a Converter instance + * \param[in] media The media device implementing the converter + * + * This seeks for the entity implementing data streaming function in the media + * graph entities and use its device node as converter device node. + */ +Converter::Converter(MediaDevice *media) +{ + const std::vector &entities = media->entities(); + auto it = std::find_if(entities.begin(), entities.end(), + [](MediaEntity *entity) { + return entity->function() == MEDIA_ENT_F_IO_V4L; + }); + if (it == entities.end()) { + LOG(Converter, Error) + << "No entity suitable for implementing a converter in " + << media->driver() << " entities list."; + return; + } + + deviceNode_ = (*it)->deviceNode(); +} + +Converter::~Converter() +{ +} + +/** + * \fn Converter::loadConfiguration() + * \brief Load converter configuration from file + * \param[in] filename The file name path + * + * Load converter dependent configuration parameters to apply on the hardware. + * + * \return 0 on success or a negative error code otherwise + */ + +/** + * \fn Converter::isValid() + * \brief Check if the converter configuration is valid + * \return True is the converter is valid, false otherwise + */ + +/** + * \fn Converter::formats() + * \brief Retrieve the list of supported pixel formats for an input pixel format + * \param[in] input Input pixel format to retrieve output pixel format list for + * \return The list of output supported pixel formats + */ + +/** + * \fn Converter::sizes() + * \brief Retrieve the output range of minimum and maximum sizes for an input size + * \param[in] input Input stream size to retrieve range for + * \return A range of output image sizes + */ + +/** + * \fn Converter::strideAndFrameSize() + * \brief Retrieve the output stride and frame size for an input configutation + * \param[in] pixelFormat Input stream pixel format + * \param[in] size Input stream size + * \return A tuple indicating the stride and frame size or an empty tuple on error + */ + +/** + * \fn Converter::configure() + * \brief Configure a set of output stream conversion from an input stream + * \param[in] inputCfg Input stream configuration + * \param[in] outputCfg A list of output stream configurations + * \return 0 on success or a negative error code otherwise + */ + +/** + * \fn Converter::exportBuffers() + * \brief Export buffers from the converter device + * \param[in] output Output stream index exporting the buffers + * \param[in] count Number of buffers to allocate + * \param[out] buffers Vector to store allocated buffers + * + * This function operates similarly as V4L2VideoDevice::exportBuffers() on the + * output stream indicated by the \a output index. + * + * \return The number of allocated buffers on success or a negative error code + * otherwise + */ + +/** + * \fn Converter::start() + * \brief Start the converter streaming operation + * \return 0 on success or a negative error code otherwise + */ + +/** + * \fn Converter::stop() + * \brief Stop the converter streaming operation + * \return 0 on success or a negative error code otherwise + */ + +/** + * \fn Converter::queueBuffers() + * \brief Queue buffers to converter device + * \param[in] input The frame buffer to apply the conversion + * \param[out] buffers The container holding the output stream indexes and + * their respective frame buffer outputs. + * + * This function queues the \a input frame buffer on the output streams of the + * \a buffers map key and retrieve the output frame buffer indicated by the + * \a buffer map value. + * + * \return 0 on success or a negative error code otherwise + */ + +/** + * \var Converter::inputBufferReady + * \brief A signal emitted when the input frame buffer completes + */ + +/** + * \var Converter::outputBufferReady + * \brief A signal emitted when the output frame buffer completes + */ + +/** + * \fn Converter::deviceNode() + * \brief The converter device node attribute accessor + * \return The converter device node string + */ + +/** + * \class ConverterFactoryBase + * \brief Base class for converter factories + * + * The ConverterFactoryBase class is the base of all specializations of the + * ConverterFactory class template. It implements the factory registration, + * maintains a registry of factories, and provides access to the registered + * factories. + */ + +/** + * \brief Construct a converter factory base + * \param[in] name Name of the converter class + * + * Creating an instance of the factory base registers it with the global list of + * factories, accessible through the factories() function. + * + * The factory \a name is used as unique identifier. + * If the converter implemententation fully relies on a generic framework, the + * name should be the same as the framework. + * Otherwise, if the implementation is specialized, the factory name should match + * the driver name implementing the function. + * The factory \a aliases holds a list of driver names implementing a generic + * subsystem without any personalizations. + */ +ConverterFactoryBase::ConverterFactoryBase(const std::string name, std::initializer_list aliases) + : name_(name), aliases_(aliases) +{ + registerType(this); +} + +/** + * \brief Create an instance of the converter corresponding to a named factory + * \param[in] name Name of the factory + * + * \return A unique pointer to a new instance of the converter subclass + * corresponding to the named factory or one of its alias. Otherwise a null + * pointer if no such factory exists + */ +std::unique_ptr ConverterFactoryBase::create(MediaDevice *media) +{ + const std::vector &factories = + ConverterFactoryBase::factories(); + + for (const ConverterFactoryBase *factory : factories) { + std::vector aliases = factory->aliases(); + auto it = std::find(aliases.begin(), aliases.end(), media->driver()); + + if (it == aliases.end() && media->driver() != factory->name_) + continue; + + LOG(Converter, Debug) + << "Creating converter from " + << factory->name_ << " factory with " + << (it == aliases.end() ? "no" : media->driver()) << " alias."; + + return factory->createInstance(media); + } + + return nullptr; +} + +/** + * \brief Add a converter class to the registry + * \param[in] factory Factory to use to construct the converter class + * + * The caller is responsible to guarantee the uniqueness of the converter name. + */ +void ConverterFactoryBase::registerType(ConverterFactoryBase *factory) +{ + std::vector &factories = + ConverterFactoryBase::factories(); + + factories.push_back(factory); +} + +/** + * \brief Retrieve the list of all converter factory names + * \return The list of all converter factory names + */ +std::vector ConverterFactoryBase::names() +{ + std::vector list; + + std::vector &factories = + ConverterFactoryBase::factories(); + + for (ConverterFactoryBase *factory : factories) { + list.push_back(factory->name_); + for (auto alias : factory->aliases()) + list.push_back(alias); + } + + return list; +} + +/** + * \brief Retrieve the list of all converter factories + * \return The list of converter factories + */ +std::vector &ConverterFactoryBase::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; +} + +/** + * \var ConverterFactoryBase::name_ + * \brief The name of the factory + */ + +/** + * \var ConverterFactoryBase::aliases_ + * \brief The list holding the factory aliases + */ + +/** + * \class ConverterFactory + * \brief Registration of ConverterFactory classes and creation of instances + * \param _Converter The converter class type for this factory + * + * To facilitate discovery and instantiation of Converter classes, the + * ConverterFactory class implements auto-registration of converter helpers. + * Each Converter subclass shall register itself using the REGISTER_CONVERTER() + * macro, which will create a corresponding instance of a ConverterFactory + * subclass and register it with the static list of factories. + */ + +/** + * \fn ConverterFactory::ConverterFactory(const char *name, std::initializer_list aliases) + * \brief Construct a converter factory + * \param[in] name Name of the converter class + * \param[in] aliases Aliases of the converter class + * + * Creating an instance of the factory registers it with the global list of + * factories, accessible through the factories() function. + * + * The factory \a name is used as unique identifier. + * If the converter implemententation fully relies on a generic framework, the + * name should be the same as the framework. + * Otherwise, if the implementation is specialized, the factory name should match + * the driver name implementing the function. + * The factory \a aliases holds a list of driver names implementing a generic + * subsystem without any personalizations. + */ + +/** + * \fn ConverterFactory::createInstance() const + * \brief Create an instance of the Converter corresponding to the factory + * \param[in] media Media device pointer + * \return A unique pointer to a newly constructed instance of the Converter + * subclass corresponding to the factory + */ + +/** + * \def REGISTER_CONVERTER + * \brief Register a converter with the Converter factory + * \param[in] name Converter name used to register the class + * \param[in] converter Class name of Converter derived class to register + * \param[in] aliases Optional list of alias names + * + * Register a Converter subclass with the factory and make it available to try + * and match converters. + */ + +} /* namespace libcamera */ diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build index 7fcbb2dd..3a9fc31f 100644 --- a/src/libcamera/meson.build +++ b/src/libcamera/meson.build @@ -13,6 +13,7 @@ libcamera_sources = files([ 'controls.cpp', 'control_serializer.cpp', 'control_validator.cpp', + 'converter.cpp', 'delayed_controls.cpp', 'device_enumerator.cpp', 'device_enumerator_sysfs.cpp',