From patchwork Thu Nov 17 18:52:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xavier Roumegue X-Patchwork-Id: 17811 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 3270EBD16B for ; Thu, 17 Nov 2022 18:52:42 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 9C2686309B; Thu, 17 Nov 2022 19:52:41 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1668711161; bh=8O8U4bTo4NoGlE26T2Tv/aS1WIJg7tL/shel8TE6Qa8=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=xS1dIZ3fEahTvxg4SUoHcNp48mNgNjuTqz/wMhhfYwR4Wl9CHVHduJx9oksY6YqoZ 5Vfo6MNqM7A0i3qoAWPVzU6KwtqSmu7UhniSBfXeB19LWNUrtSgartRCY7qmqKR/Us 6TwJQLlzGzd79lcbNEhSAjNmhZ1CmF/8V80OFbiI3/egkv8IVTluvoHQeD+tqxM5np XbyegiJoUVkpzrhyQxHIRFv+rvBat6duMtOi3tfZOQy135G07TZnbjmXs2TE2QQ9G/ 3fqGIs0zyobfNAHce1vYqxCyccYCy3JNx5YPBKyVZD/a9dADPJ+lGVS6/+LnyBEM4S vImjNFiI887/Q== Received: from EUR05-VI1-obe.outbound.protection.outlook.com (mail-vi1eur05on20623.outbound.protection.outlook.com [IPv6:2a01:111:f400:7d00::623]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 84A0F61F31 for ; Thu, 17 Nov 2022 19:52:39 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=NXP1.onmicrosoft.com header.i=@NXP1.onmicrosoft.com header.b="KAeYnRLs"; dkim-atps=neutral ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=V+/8R7px7Q9V3FSdh9t0MXN2gyXoNcpvd72bNq9sWKx4NMq7IXN68LewQiMRspI6Pb6tJJGlSEvV95YIdH5C/z+etpF1hWMnbBV/Py/uVhRf+oQufa9P72dTAYB/XdkKbOCEDiP0rdjpigMwvCVSAtKQuKdFirgjIJRH1EtiJH5Q3936Gsng0Bhs93DlcuxXdE0yi2mHkinRTWV+urM0c/IIrHTlr9Xd6v/sqE3cP8J6FKObfHzS42pbdvUUZ2WL2Q4xRrkP/ACJ+2lc1qtT1qLtjG+9B5W2VfahGF9rMlE2fTz7eRxOhHWv9HcLLVeCaaj+UuEs3UYfFLJ8BJT2Iw== 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=TTrE46uE/qr5toOQV7XNykZr8uUlV1ej973RN51LeLw=; b=nhc53PkpyFWtv0nwuTptLf34F6lZmPcx+/62iZS+qhlngsD5PrIM8cbFFP7MVD/SpgSwbmzY8POuS8m8yOF3LaUR5feeETPGXHyGsGgrcGFMR53Oq3NJ7/MTv/AeR8gMVCY87TucpyHDaAxODn/QmE12tBynPvnyySaySLs0y0/h0BYGhDC+374UdrQg2lG0/6LCAGQpCt+mFllwD4Bemi8e4ZVzb5p6S3B7MBv+RF5CrwDnuIHMS8QJaZ6D8EkU0fsTRZ+/2hReGxWRZRzZLK57bFT9KSUYA6yyH6XPetBcuRaGCv81QJgkoVY/GYQyVPaVwOeBfIrbUwmcKkefVA== 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=TTrE46uE/qr5toOQV7XNykZr8uUlV1ej973RN51LeLw=; b=KAeYnRLshlMn3fPGzkQypJkTVLSA4d03CGr7FrbIvBdf/SLShGoBy/H++c91A0Zm7xvD3AZTm/roo0X2ExPmnbyAs8MZvBEmPfIAZ4S/Eam2drTXYznTm3zb+24ddTQa/p0GQUro2fs7yPkmh/biZftzX4hAH7wyOWN3K/rw/Y0= 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 DU2PR04MB8678.eurprd04.prod.outlook.com (2603:10a6:10:2dd::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5813.13; Thu, 17 Nov 2022 18:52:37 +0000 Received: from PAXPR04MB8703.eurprd04.prod.outlook.com ([fe80::14d3:8e4:cf07:810d]) by PAXPR04MB8703.eurprd04.prod.outlook.com ([fe80::14d3:8e4:cf07:810d%3]) with mapi id 15.20.5813.020; Thu, 17 Nov 2022 18:52:36 +0000 To: jacopo@jmondi.org, laurent.pinchart@ideasonboard.com Date: Thu, 17 Nov 2022 19:52:03 +0100 Message-Id: <20221117185204.11625-1-xavier.roumegue@oss.nxp.com> X-Mailer: git-send-email 2.38.1 X-ClientProxiedBy: PR3P191CA0006.EURP191.PROD.OUTLOOK.COM (2603:10a6:102:54::11) 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_|DU2PR04MB8678:EE_ X-MS-Office365-Filtering-Correlation-Id: 132bb525-c1e5-4bb0-492b-08dac8cce403 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: pVfdAIuX78ubgqdcV0g0rPj94YaznSNZ4oQcG/M+Yr8BPmFq2FtTsbBeSM4PD6b9oqWlJLGuaD/Hw/XnHLDFv7SOgkQw8eNpyxBg+c3suWMI+0drL01frN+ZPQNr7Bx4HTHo2pnS8hASUxEGvPzNtG/Lbwky0Qt7bDsRbpOIxZyq0i07PZw+zX1uJgXpP+qSozKphYgrKkWa3dFSe9qA9hVMSm1sxQ9aos3T4Ejw/KMfs3Iqe3ZQSx3M24dZhT2kPM3N8hI7N37tZUOdTC7U0IPQ9A6A7/AW9GJENZ8K1chL+qWacE38vtJlexh7junxSjgWamentxpTIVIlkaO9mjNrScQZKjNL7TnJllysOtH0ei8d+F0DItVZRi3qKZjxKaL2hiReoxjQ1N4K+obMvmLGx690+dONxf3HFfX0Aj88xKRjclRdJZfaHXsU9vtHHnyOXt8DzOmeCRLCw6h6j9gOKPmzv9U6FUsN7yvKUqDaLHuFX9P2oMiqJ2u/ku8NrXazGHXHqRML41sB7H94WeSFXi7cDwTSEYxSkqZZXC0S+aCy9NjIxeyRuZV1jDyuf0VXrVpWMUY64MPer8gSyrJMP0FFauGSc4ON5utVSi+1Sc00GfQAYO2fWK43p28yK6i7j9UgBZSIENcO+P/5GQXwPY248TjXeEru0Gl9aQfv9ySiELe16h0jz18jmuQ5 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)(136003)(376002)(366004)(39860400002)(346002)(396003)(451199015)(52116002)(6666004)(6506007)(6512007)(44832011)(30864003)(8676002)(8936002)(4326008)(66476007)(66556008)(66946007)(5660300002)(38100700002)(1076003)(186003)(2616005)(41300700001)(478600001)(6486002)(316002)(86362001)(2906002)(66899015)(83380400001)(41533002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: s99lxbnvuAlAe/5efLM5+pGJtpSsLvgaiSuRnyQ/OwehDAwuM0Fn+dof5Uw5HBVL/lwOrnqRBFU7Z3pxsCPlMF9mDXOdQD1d85b53dLSbOWDZ6B4ezkE79qFQTMI1BFAgv/TB4qhTa/1KAzTAPPZDEUGdDIpm4bLpGAwbH7+aFpiqpygBSKNqJAiazKR5x9Qy0FVdqrWaFr0OXHLv+Av/8ppce6fhsMLGL7L4qQ+j+MUl2KC8TD3PCHb8KnGoIhiP6oMYlmuXZoPrSUc0BUy4y0vpSMKhWPmY1lom8YfgElv/pDYNf1W4kxlnYjQOWg1Khy7lGxCP2Ep87q4GxdUYBDN0EKGoDA2rJrxXCb83fsLFpRIqr1/HL4vDOqDAPiml7wfYXqhXCPRHFy6uV+eFiDxaNDsAkaC4YqhaSzBV524BU67VAxwn+2dIvsR15m9FdfX1m07jl9/071jZCVrzB8WhLz11WMlQjNhD5w3h9+JDMFLuK8Wj/6/91q2LvbadkDlRzLISNLY5PN0SG2rK4oh3LVTXmLA0plGd4CpISCDi468Ad15nVDwY8KA3t+O0J5UqDI0BqDxpZUECixN0MguYpxHYtcLTVlZQJAEkKm0nbCmiL3+NTQecucBB0LU9t/4RhRhYGeuGmWeTn1mUcCxaP6mLfX8adpn25JyD5ZH/8JF3fqqSetmPkB/YIzFUM5/W2ztV62HM5xEyotKsMm+F0SKyPQFujeJvPYIzuuOVDJ8cIo9L0KqVvDysGcE5bWwPF6/93e6hMAhe9X+Hiz9kw/fYl7b1AnD4a39JIYC6wZaUbHUgUq3U61TK+gtYL5cLOIsgOEsjeqSOOGH8VxNi2HCN/WTDtRFKia5+a/T1/YOGOEkpMJcm3vV6SB2PAdA+LCKnsU8G09ohmdpZbrdOa3HJ8a/hUDvNEJwP359Hd79yvEh3x4oHvrrwkdSMQuX4wXfPugutrwIot4j1SaaiAZQO9pnssdXmJ31EvUr6HjoU3IyNPSROn6AK85OYkJumlu1FRH6Xa3Gqn1+qqHU0ZM5au1OPvyjPwAK6li8ZcK/GPLV4eFR74vxNteYRSdtuXk/KaZGIq9UDCT7ww2RUoB5zqr96d9tV16nMpB5BQJXhokRsH7+DfYTxtHFE8g/GCoolLhlSv9hmpc24sxzAWuFZgcb4FSdGA86h8YWx5JmHsWAMHlYb9HK086bXhC6OkpfGs11NzhOmtqgS8T2pprT5MQ0L7y8Jmzf28/A3HkcloQGVv8weiWtO4oVOkhieTw7QQQzxgEOSi561YAR0Rqn/jBmiS6RS52tNCSXvdzBgXsn5vunn29Cxxk/9Jr1RWec0KcpzZe7EubRhy1x4RylVGWPwFTqTGtc0sCRAmYeVtRQ5CkJaHO5dQHW5xGFAZ98yQZANWeHtMeHNsI+EUyjWoU3ZccvdHRFpEeZKfvel9tC2V+gxD4VAShdGqO8VF3E7WfbMsahWg4zoG38wi3/JgM3rc14polRKSSFpOXCL2VgS83EAbJiVpV4FDEK28Jo2AthCwmv1IEYVN/uQOwDtiFOKw+ZeH5BM4kV7qbHyyT2NCIzfkq6Gor94ku8ruQZHn/5T9zO2cIhQzXEITllBm4jwyps1D8UU7FhuEaWQfgmKcYpZ9yXXk6SzdLAyp/oWCTiTUGhNCamnw== X-OriginatorOrg: oss.nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: 132bb525-c1e5-4bb0-492b-08dac8cce403 X-MS-Exchange-CrossTenant-AuthSource: PAXPR04MB8703.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 Nov 2022 18:52:36.8940 (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: DIWgVut8AUK2CWjCdFE/Ll8Ki/IHjak0JDu1S0943zEUyJUaTqCEUyIcbuUR1ku0zdnXJq5KZZ9y6S1Z1AwwJg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DU2PR04MB8678 Subject: [libcamera-devel] [PATCH v3 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 via libcamera-devel From: Xavier Roumegue Reply-To: Xavier Roumegue Cc: libcamera-devel@lists.libcamera.org Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" 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 Reviewed-by: Jacopo Mondi Reviewed-by: Kieran Bingham --- include/libcamera/internal/converter.h | 109 ++++++++ include/libcamera/internal/meson.build | 1 + src/libcamera/converter.cpp | 331 +++++++++++++++++++++++++ src/libcamera/meson.build | 1 + 4 files changed, 442 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..c02911c4 --- /dev/null +++ b/include/libcamera/internal/converter.h @@ -0,0 +1,109 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2020, Laurent Pinchart + * Copyright 2022 NXP + * + * converter.h - Generic format converter interface + */ + +#pragma once + +#include +#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> &outputCfgs) = 0; + virtual int exportBuffers(unsigned int output, 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..1ab3c9f6 --- /dev/null +++ b/src/libcamera/converter.cpp @@ -0,0 +1,331 @@ +/* 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 offer scaling and pixel format 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 supported output pixel formats + */ + +/** + * \fn Converter::sizes() + * \brief Retrieve the range of minimum and maximum output 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[out] outputCfgs 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 + */ + +/** + * \fn Converter::queueBuffers() + * \brief Queue buffers to converter device + * \param[in] input The frame buffer to apply the conversion + * \param[out] outputs 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 outputs map key and retrieve the output frame buffer indicated by the + * 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 + * \param[in] aliases Name aliases 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 + * implementation 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); +} + +/** + * \fn ConverterFactoryBase::aliases() + * \return The names aliases + */ + +/** + * \brief Create an instance of the converter corresponding to a named factory + * \param[in] media 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) { + const 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 + * \details \copydetails ConverterFactoryBase::ConverterFactoryBase + * + */ + +/** + * \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] ... 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 0494e808..e9d0324e 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',