Show a patch.

GET /api/1.1/patches/21737/?format=api
HTTP 200 OK
Allow: GET, PUT, PATCH, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "id": 21737,
    "url": "https://patchwork.libcamera.org/api/1.1/patches/21737/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/21737/",
    "project": {
        "id": 1,
        "url": "https://patchwork.libcamera.org/api/1.1/projects/1/?format=api",
        "name": "libcamera",
        "link_name": "libcamera",
        "list_id": "libcamera_core",
        "list_email": "libcamera-devel@lists.libcamera.org",
        "web_url": "",
        "scm_url": "",
        "webscm_url": ""
    },
    "msgid": "<20241022145321.25923-2-jacopo.mondi@ideasonboard.com>",
    "date": "2024-10-22T14:53:13",
    "name": "[1/3] libcamera: camera_sensor: Introduce CameraSensorFactory",
    "commit_ref": null,
    "pull_url": null,
    "state": "accepted",
    "archived": false,
    "hash": "8161ab79369f34d90358efc458ecc3fa712f9b45",
    "submitter": {
        "id": 143,
        "url": "https://patchwork.libcamera.org/api/1.1/people/143/?format=api",
        "name": "Jacopo Mondi",
        "email": "jacopo.mondi@ideasonboard.com"
    },
    "delegate": null,
    "mbox": "https://patchwork.libcamera.org/patch/21737/mbox/",
    "series": [
        {
            "id": 4736,
            "url": "https://patchwork.libcamera.org/api/1.1/series/4736/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=4736",
            "date": "2024-10-22T14:53:12",
            "name": "Introduce CameraSensorFactory",
            "version": 1,
            "mbox": "https://patchwork.libcamera.org/series/4736/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/21737/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/21737/checks/",
    "tags": {},
    "headers": {
        "Return-Path": "<libcamera-devel-bounces@lists.libcamera.org>",
        "X-Original-To": "parsemail@patchwork.libcamera.org",
        "Delivered-To": "parsemail@patchwork.libcamera.org",
        "Received": [
            "from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id C665EBD160\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 22 Oct 2024 14:53:35 +0000 (UTC)",
            "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 73EFB6539A;\n\tTue, 22 Oct 2024 16:53:32 +0200 (CEST)",
            "from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 1BB5365388\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 22 Oct 2024 16:53:30 +0200 (CEST)",
            "from ideasonboard.com (93-61-96-190.ip145.fastwebnet.it\n\t[93.61.96.190])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 2E36AA47;\n\tTue, 22 Oct 2024 16:51:43 +0200 (CEST)"
        ],
        "Authentication-Results": "lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"K5f/QOsm\"; dkim-atps=neutral",
        "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1729608703;\n\tbh=5N91qP8czPcDe1wMoL+UhnEI7D2pyPXPm4wtarLxn0A=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=K5f/QOsmmXF3JpgXX3qgiIwR66SGh1sfO6BiiKTIbsc3vh+zt4NYSP9Un/7W+cf+z\n\tmjEgc/cF5CwTl95V6Et8fm+IeygRyqlCCXnDkjQnzp+czcoqEnLCOxXFvvh/PJv3o9\n\tUJwScPRW+XRlWYs/kSIChPGUXmFFtEQr2zyHPEQw=",
        "From": "Jacopo Mondi <jacopo.mondi@ideasonboard.com>",
        "To": "libcamera-devel@lists.libcamera.org",
        "Cc": "Jacopo Mondi <jacopo.mondi@ideasonboard.com>,\n\tJacopo Mondi <jacopo@jmondi.org>,\n\tStefan Klug <stefan.klug@ideasonboard.com>",
        "Subject": "[PATCH 1/3] libcamera: camera_sensor: Introduce CameraSensorFactory",
        "Date": "Tue, 22 Oct 2024 16:53:13 +0200",
        "Message-ID": "<20241022145321.25923-2-jacopo.mondi@ideasonboard.com>",
        "X-Mailer": "git-send-email 2.47.0",
        "In-Reply-To": "<20241022145321.25923-1-jacopo.mondi@ideasonboard.com>",
        "References": "<20241022145321.25923-1-jacopo.mondi@ideasonboard.com>",
        "MIME-Version": "1.0",
        "Content-Transfer-Encoding": "8bit",
        "X-BeenThere": "libcamera-devel@lists.libcamera.org",
        "X-Mailman-Version": "2.1.29",
        "Precedence": "list",
        "List-Id": "<libcamera-devel.lists.libcamera.org>",
        "List-Unsubscribe": "<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>",
        "List-Archive": "<https://lists.libcamera.org/pipermail/libcamera-devel/>",
        "List-Post": "<mailto:libcamera-devel@lists.libcamera.org>",
        "List-Help": "<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>",
        "List-Subscribe": "<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>",
        "Errors-To": "libcamera-devel-bounces@lists.libcamera.org",
        "Sender": "\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"
    },
    "content": "From: Jacopo Mondi <jacopo@jmondi.org>\n\nIntroduce a factory to create CameraSensor derived classes instances by\ninspecting the sensor media entity name and provide a convenience macro\nto register specialized sensor handlers.\n\nSigned-off-by: Jacopo Mondi <jacopo@jmondi.org>\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\nReviewed-by: Stefan Klug <stefan.klug@ideasonboard.com>\n---\n include/libcamera/internal/camera_sensor.h    |  48 +++++-\n src/libcamera/pipeline/imx8-isi/imx8-isi.cpp  |   9 +-\n src/libcamera/pipeline/ipu3/cio2.cpp          |   7 +-\n src/libcamera/pipeline/mali-c55/mali-c55.cpp  |   5 +-\n src/libcamera/pipeline/rkisp1/rkisp1.cpp      |   7 +-\n .../pipeline/rpi/common/pipeline_base.cpp     |   5 +-\n src/libcamera/pipeline/simple/simple.cpp      |   9 +-\n src/libcamera/pipeline/vimc/vimc.cpp          |   7 +-\n src/libcamera/sensor/camera_sensor.cpp        | 162 ++++++++++++++++++\n test/camera-sensor.cpp                        |   7 +-\n .../v4l2_videodevice_test.cpp                 |   5 +-\n test/v4l2_videodevice/v4l2_videodevice_test.h |   2 +-\n 12 files changed, 233 insertions(+), 40 deletions(-)",
    "diff": "diff --git a/include/libcamera/internal/camera_sensor.h b/include/libcamera/internal/camera_sensor.h\nindex a42c15fa37ce..2d6cb697a870 100644\n--- a/include/libcamera/internal/camera_sensor.h\n+++ b/include/libcamera/internal/camera_sensor.h\n@@ -38,7 +38,6 @@ enum class Orientation;\n class CameraSensor : protected Loggable\n {\n public:\n-\texplicit CameraSensor(const MediaEntity *entity);\n \t~CameraSensor();\n \n \tint init();\n@@ -81,6 +80,7 @@ public:\n \tint setTestPatternMode(controls::draft::TestPatternModeEnum mode);\n \n protected:\n+\texplicit CameraSensor(const MediaEntity *entity);\n \tstd::string logPrefix() const override;\n \n private:\n@@ -122,4 +122,50 @@ private:\n \tstd::unique_ptr<CameraLens> focusLens_;\n };\n \n+class CameraSensorFactoryBase\n+{\n+public:\n+\tCameraSensorFactoryBase();\n+\tvirtual ~CameraSensorFactoryBase() = default;\n+\n+\tstatic std::unique_ptr<CameraSensor> create(MediaEntity *entity);\n+\n+private:\n+\tLIBCAMERA_DISABLE_COPY_AND_MOVE(CameraSensorFactoryBase)\n+\n+\tstatic std::vector<CameraSensorFactoryBase *> &factories();\n+\n+\tstatic void registerFactory(CameraSensorFactoryBase *factory);\n+\n+\tvirtual bool match(const MediaEntity *entity) const = 0;\n+\n+\tvirtual std::unique_ptr<CameraSensor>\n+\tcreateInstance(MediaEntity *entity) const = 0;\n+};\n+\n+template<typename _CameraSensor>\n+class CameraSensorFactory final : public CameraSensorFactoryBase\n+{\n+public:\n+\tCameraSensorFactory()\n+\t\t: CameraSensorFactoryBase()\n+\t{\n+\t}\n+\n+private:\n+\tbool match(const MediaEntity *entity) const override\n+\t{\n+\t\treturn _CameraSensor::match(entity);\n+\t}\n+\n+\tstd::unique_ptr<CameraSensor>\n+\tcreateInstance(MediaEntity *entity) const override\n+\t{\n+\t\treturn _CameraSensor::create(entity);\n+\t}\n+};\n+\n+#define REGISTER_CAMERA_SENSOR(sensor) \\\n+static CameraSensorFactory<sensor> global_##sensor##Factory{};\n+\n } /* namespace libcamera */\ndiff --git a/src/libcamera/pipeline/imx8-isi/imx8-isi.cpp b/src/libcamera/pipeline/imx8-isi/imx8-isi.cpp\nindex 72aa6c75608a..4e66b3368d5a 100644\n--- a/src/libcamera/pipeline/imx8-isi/imx8-isi.cpp\n+++ b/src/libcamera/pipeline/imx8-isi/imx8-isi.cpp\n@@ -157,11 +157,10 @@ PipelineHandlerISI *ISICameraData::pipe()\n /* Open and initialize pipe components. */\n int ISICameraData::init()\n {\n-\tint ret = sensor_->init();\n-\tif (ret)\n-\t\treturn ret;\n+\tif (!sensor_)\n+\t\treturn -ENODEV;\n \n-\tret = csis_->open();\n+\tint ret = csis_->open();\n \tif (ret)\n \t\treturn ret;\n \n@@ -1057,7 +1056,7 @@ bool PipelineHandlerISI::match(DeviceEnumerator *enumerator)\n \t\tstd::unique_ptr<ISICameraData> data =\n \t\t\tstd::make_unique<ISICameraData>(this);\n \n-\t\tdata->sensor_ = std::make_unique<CameraSensor>(sensor);\n+\t\tdata->sensor_ = CameraSensorFactoryBase::create(sensor);\n \t\tdata->csis_ = std::make_unique<V4L2Subdevice>(csi);\n \t\tdata->xbarSink_ = sink;\n \ndiff --git a/src/libcamera/pipeline/ipu3/cio2.cpp b/src/libcamera/pipeline/ipu3/cio2.cpp\nindex 74a5d93f88ae..aa544d7b0303 100644\n--- a/src/libcamera/pipeline/ipu3/cio2.cpp\n+++ b/src/libcamera/pipeline/ipu3/cio2.cpp\n@@ -134,10 +134,9 @@ int CIO2Device::init(const MediaDevice *media, unsigned int index)\n \n \tMediaLink *link = links[0];\n \tMediaEntity *sensorEntity = link->source()->entity();\n-\tsensor_ = std::make_unique<CameraSensor>(sensorEntity);\n-\tret = sensor_->init();\n-\tif (ret)\n-\t\treturn ret;\n+\tsensor_ = CameraSensorFactoryBase::create(sensorEntity);\n+\tif (!sensor_)\n+\t\treturn -ENODEV;\n \n \tret = link->setEnabled(true);\n \tif (ret)\ndiff --git a/src/libcamera/pipeline/mali-c55/mali-c55.cpp b/src/libcamera/pipeline/mali-c55/mali-c55.cpp\nindex 45c71c1dd619..4b71e0dad15e 100644\n--- a/src/libcamera/pipeline/mali-c55/mali-c55.cpp\n+++ b/src/libcamera/pipeline/mali-c55/mali-c55.cpp\n@@ -141,9 +141,8 @@ int MaliC55CameraData::init()\n \t * Register a CameraSensor if we connect to a sensor and create\n \t * an entity for the connected CSI-2 receiver.\n \t */\n-\tsensor_ = std::make_unique<CameraSensor>(entity_);\n-\tret = sensor_->init();\n-\tif (ret)\n+\tsensor_ = CameraSensorFactoryBase::create(entity_);\n+\tif (!sensor_)\n \t\treturn ret;\n \n \tconst MediaPad *sourcePad = entity_->getPadByIndex(0);\ndiff --git a/src/libcamera/pipeline/rkisp1/rkisp1.cpp b/src/libcamera/pipeline/rkisp1/rkisp1.cpp\nindex d8d1d65fbbf9..95c25e976015 100644\n--- a/src/libcamera/pipeline/rkisp1/rkisp1.cpp\n+++ b/src/libcamera/pipeline/rkisp1/rkisp1.cpp\n@@ -1122,10 +1122,9 @@ int PipelineHandlerRkISP1::createCamera(MediaEntity *sensor)\n \t\tstd::make_unique<RkISP1CameraData>(this, &mainPath_,\n \t\t\t\t\t\t   hasSelfPath_ ? &selfPath_ : nullptr);\n \n-\tdata->sensor_ = std::make_unique<CameraSensor>(sensor);\n-\tret = data->sensor_->init();\n-\tif (ret)\n-\t\treturn ret;\n+\tdata->sensor_ = CameraSensorFactoryBase::create(sensor);\n+\tif (!data->sensor_)\n+\t\treturn -ENODEV;\n \n \t/* Initialize the camera properties. */\n \tdata->properties_ = data->sensor_->properties();\ndiff --git a/src/libcamera/pipeline/rpi/common/pipeline_base.cpp b/src/libcamera/pipeline/rpi/common/pipeline_base.cpp\nindex 3041fd1ed9fd..4f56bd33df05 100644\n--- a/src/libcamera/pipeline/rpi/common/pipeline_base.cpp\n+++ b/src/libcamera/pipeline/rpi/common/pipeline_base.cpp\n@@ -774,13 +774,10 @@ int PipelineHandlerBase::registerCamera(std::unique_ptr<RPi::CameraData> &camera\n \tCameraData *data = cameraData.get();\n \tint ret;\n \n-\tdata->sensor_ = std::make_unique<CameraSensor>(sensorEntity);\n+\tdata->sensor_ = CameraSensorFactoryBase::create(sensorEntity);\n \tif (!data->sensor_)\n \t\treturn -EINVAL;\n \n-\tif (data->sensor_->init())\n-\t\treturn -EINVAL;\n-\n \t/* Populate the map of sensor supported formats and sizes. */\n \tfor (auto const mbusCode : data->sensor_->mbusCodes())\n \t\tdata->sensorFormats_.emplace(mbusCode,\ndiff --git a/src/libcamera/pipeline/simple/simple.cpp b/src/libcamera/pipeline/simple/simple.cpp\nindex 3ddce71d3757..67f583b8a22c 100644\n--- a/src/libcamera/pipeline/simple/simple.cpp\n+++ b/src/libcamera/pipeline/simple/simple.cpp\n@@ -388,8 +388,6 @@ SimpleCameraData::SimpleCameraData(SimplePipelineHandler *pipe,\n \t\t\t\t   MediaEntity *sensor)\n \t: Camera::Private(pipe), streams_(numStreams)\n {\n-\tint ret;\n-\n \t/*\n \t * Find the shortest path from the camera sensor to a video capture\n \t * device using the breadth-first search algorithm. This heuristic will\n@@ -480,12 +478,9 @@ SimpleCameraData::SimpleCameraData(SimplePipelineHandler *pipe,\n \t}\n \n \t/* Finally also remember the sensor. */\n-\tsensor_ = std::make_unique<CameraSensor>(sensor);\n-\tret = sensor_->init();\n-\tif (ret) {\n-\t\tsensor_.reset();\n+\tsensor_ = CameraSensorFactoryBase::create(sensor);\n+\tif (!sensor_)\n \t\treturn;\n-\t}\n \n \tLOG(SimplePipeline, Debug)\n \t\t<< \"Found pipeline: \"\ndiff --git a/src/libcamera/pipeline/vimc/vimc.cpp b/src/libcamera/pipeline/vimc/vimc.cpp\nindex 2165bae890cb..2e474133439c 100644\n--- a/src/libcamera/pipeline/vimc/vimc.cpp\n+++ b/src/libcamera/pipeline/vimc/vimc.cpp\n@@ -532,10 +532,9 @@ int VimcCameraData::init()\n \t\treturn ret;\n \n \t/* Create and open the camera sensor, debayer, scaler and video device. */\n-\tsensor_ = std::make_unique<CameraSensor>(media_->getEntityByName(\"Sensor B\"));\n-\tret = sensor_->init();\n-\tif (ret)\n-\t\treturn ret;\n+\tsensor_ = CameraSensorFactoryBase::create(media_->getEntityByName(\"Sensor B\"));\n+\tif (!sensor_)\n+\t\treturn -ENODEV;\n \n \tdebayer_ = V4L2Subdevice::fromEntityName(media_, \"Debayer B\");\n \tif (debayer_->open())\ndiff --git a/src/libcamera/sensor/camera_sensor.cpp b/src/libcamera/sensor/camera_sensor.cpp\nindex 1b224f1989fe..025c9eefdd12 100644\n--- a/src/libcamera/sensor/camera_sensor.cpp\n+++ b/src/libcamera/sensor/camera_sensor.cpp\n@@ -11,6 +11,7 @@\n #include <cmath>\n #include <float.h>\n #include <limits.h>\n+#include <map>\n #include <string.h>\n \n #include <libcamera/base/utils.h>\n@@ -1202,4 +1203,165 @@ std::string CameraSensor::logPrefix() const\n \treturn \"'\" + entity_->name() + \"'\";\n }\n \n+namespace {\n+\n+/* Transitory default camera sensor implementation */\n+class CameraSensorDefault : public CameraSensor\n+{\n+public:\n+\tCameraSensorDefault(MediaEntity *entity)\n+\t\t: CameraSensor(entity)\n+\t{\n+\t}\n+\n+\tstatic bool match([[maybe_unused]] const MediaEntity *entity)\n+\t{\n+\t\treturn true;\n+\t}\n+\n+\tstatic std::unique_ptr<CameraSensorDefault> create(MediaEntity *entity)\n+\t{\n+\t\tstd::unique_ptr<CameraSensorDefault> sensor =\n+\t\t\tstd::make_unique<CameraSensorDefault>(entity);\n+\n+\t\tif (sensor->init())\n+\t\t\treturn nullptr;\n+\n+\t\treturn sensor;\n+\t}\n+};\n+\n+REGISTER_CAMERA_SENSOR(CameraSensorDefault)\n+\n+}; /* namespace */\n+\n+/**\n+ * \\class CameraSensorFactoryBase\n+ * \\brief Base class for camera sensor factories\n+ *\n+ * The CameraSensorFactoryBase class is the base of all specializations of\n+ * the CameraSensorFactory class template. It implements the factory\n+ * registration, maintains a registry of factories, and provides access to the\n+ * registered factories.\n+ */\n+\n+/**\n+ * \\brief Construct a camera sensor factory base\n+ *\n+ * Creating an instance of the factory base registers it with the global list of\n+ * factories, accessible through the factories() function.\n+ */\n+CameraSensorFactoryBase::CameraSensorFactoryBase()\n+{\n+\tregisterFactory(this);\n+}\n+\n+/**\n+ * \\brief Create an instance of the CameraSensor corresponding to a media entity\n+ * \\param[in] entity The media entity on the source end of the sensor\n+ *\n+ * \\return A unique pointer to a new instance of the CameraSensor subclass\n+ * matching the entity, or a null pointer if no such factory exists\n+ */\n+std::unique_ptr<CameraSensor> CameraSensorFactoryBase::create(MediaEntity *entity)\n+{\n+\tconst std::vector<CameraSensorFactoryBase *> &factories =\n+\t\tCameraSensorFactoryBase::factories();\n+\n+\tfor (const CameraSensorFactoryBase *factory : factories) {\n+\t\tif (!factory->match(entity))\n+\t\t\tcontinue;\n+\n+\t\tstd::unique_ptr<CameraSensor> sensor = factory->createInstance(entity);\n+\t\tif (!sensor) {\n+\t\t\tLOG(CameraSensor, Error)\n+\t\t\t\t<< \"Failed to create sensor for '\"\n+\t\t\t\t<< entity->name();\n+\t\t\treturn nullptr;\n+\t\t}\n+\n+\t\treturn sensor;\n+\t}\n+\n+\treturn nullptr;\n+}\n+\n+/**\n+ * \\brief Retrieve the list of all camera sensor factories\n+ * \\return The list of camera sensor factories\n+ */\n+std::vector<CameraSensorFactoryBase *> &CameraSensorFactoryBase::factories()\n+{\n+\t/*\n+\t * The static factories map is defined inside the function to ensure\n+\t * it gets initialized on first use, without any dependency on link\n+\t * order.\n+\t */\n+\tstatic std::vector<CameraSensorFactoryBase *> factories;\n+\treturn factories;\n+}\n+\n+/**\n+ * \\brief Add a camera sensor class to the registry\n+ * \\param[in] factory Factory to use to construct the camera sensor\n+ */\n+void CameraSensorFactoryBase::registerFactory(CameraSensorFactoryBase *factory)\n+{\n+\tstd::vector<CameraSensorFactoryBase *> &factories =\n+\t\tCameraSensorFactoryBase::factories();\n+\n+\tfactories.push_back(factory);\n+}\n+\n+/**\n+ * \\class CameraSensorFactory\n+ * \\brief Registration of CameraSensorFactory classes and creation of instances\n+ * \\tparam _CameraSensor The camera sensor class type for this factory\n+ *\n+ * To facilitate discovery and instantiation of CameraSensor classes, the\n+ * CameraSensorFactory class implements auto-registration of camera sensors.\n+ * Each CameraSensor subclass shall register itself using the\n+ * REGISTER_CAMERA_SENSOR() macro, which will create a corresponding instance\n+ * of a CameraSensorFactory subclass and register it with the static list of\n+ * factories.\n+ */\n+\n+/**\n+ * \\fn CameraSensorFactory::CameraSensorFactory()\n+ * \\brief Construct a camera sensor factory\n+ *\n+ * Creating an instance of the factory registers it with the global list of\n+ * factories, accessible through the CameraSensorFactoryBase::factories()\n+ * function.\n+ */\n+\n+/**\n+ * \\fn CameraSensorFactory::createInstance() const\n+ * \\brief Create an instance of the CameraSensor corresponding to the factory\n+ *\n+ * \\return A unique pointer to a newly constructed instance of the CameraSensor\n+ * subclass corresponding to the factory\n+ */\n+\n+/**\n+ * \\def REGISTER_CAMERA_SENSOR(sensor)\n+ * \\brief Register a camera sensor type to the sensor factory\n+ * \\param[in] sensor Class name of the CameraSensor derived class to register\n+ *\n+ * Register a CameraSensor subclass with the factory and make it available to\n+ * try and match sensors. The subclass needs to implement two static functions:\n+ *\n+ * \\code{.cpp}\n+ * static bool match(const MediaEntity *entity);\n+ * static std::unique_ptr<sensor> create(MediaEntity *entity);\n+ * \\endcode\n+ *\n+ * The match() function tests if the sensor class supports the camera sensor\n+ * identified by a MediaEntity.\n+ *\n+ * The create() function creates a new instance of the sensor class. It may\n+ * return a null pointer if initialization of the instance fails. It will only\n+ * be called if the match() function has returned true for the given entity.\n+ */\n+\n } /* namespace libcamera */\ndiff --git a/test/camera-sensor.cpp b/test/camera-sensor.cpp\nindex 1d402c43355b..869c788965da 100644\n--- a/test/camera-sensor.cpp\n+++ b/test/camera-sensor.cpp\n@@ -52,8 +52,8 @@ protected:\n \t\t\treturn TestFail;\n \t\t}\n \n-\t\tsensor_ = new CameraSensor(entity);\n-\t\tif (sensor_->init() < 0) {\n+\t\tsensor_ = CameraSensorFactoryBase::create(entity);\n+\t\tif (!sensor_) {\n \t\t\tcerr << \"Unable to initialise camera sensor\" << endl;\n \t\t\treturn TestFail;\n \t\t}\n@@ -118,13 +118,12 @@ protected:\n \n \tvoid cleanup()\n \t{\n-\t\tdelete sensor_;\n \t}\n \n private:\n \tstd::unique_ptr<DeviceEnumerator> enumerator_;\n \tstd::shared_ptr<MediaDevice> media_;\n-\tCameraSensor *sensor_;\n+\tstd::unique_ptr<CameraSensor> sensor_;\n \tCameraLens *lens_;\n };\n \ndiff --git a/test/v4l2_videodevice/v4l2_videodevice_test.cpp b/test/v4l2_videodevice/v4l2_videodevice_test.cpp\nindex 1113cf5bf8cf..9fbd24cc91ea 100644\n--- a/test/v4l2_videodevice/v4l2_videodevice_test.cpp\n+++ b/test/v4l2_videodevice/v4l2_videodevice_test.cpp\n@@ -64,8 +64,8 @@ int V4L2VideoDeviceTest::init()\n \tformat.size.height = 480;\n \n \tif (driver_ == \"vimc\") {\n-\t\tsensor_ = new CameraSensor(media_->getEntityByName(\"Sensor A\"));\n-\t\tif (sensor_->init())\n+\t\tsensor_ = CameraSensorFactoryBase::create(media_->getEntityByName(\"Sensor A\"));\n+\t\tif (!sensor_)\n \t\t\treturn TestFail;\n \n \t\tdebayer_ = new V4L2Subdevice(media_->getEntityByName(\"Debayer A\"));\n@@ -98,6 +98,5 @@ void V4L2VideoDeviceTest::cleanup()\n \tcapture_->close();\n \n \tdelete debayer_;\n-\tdelete sensor_;\n \tdelete capture_;\n }\ndiff --git a/test/v4l2_videodevice/v4l2_videodevice_test.h b/test/v4l2_videodevice/v4l2_videodevice_test.h\nindex b5871ce69e18..7c9003ec4c4d 100644\n--- a/test/v4l2_videodevice/v4l2_videodevice_test.h\n+++ b/test/v4l2_videodevice/v4l2_videodevice_test.h\n@@ -36,7 +36,7 @@ protected:\n \tstd::string entity_;\n \tstd::unique_ptr<libcamera::DeviceEnumerator> enumerator_;\n \tstd::shared_ptr<libcamera::MediaDevice> media_;\n-\tlibcamera::CameraSensor *sensor_;\n+\tstd::unique_ptr<libcamera::CameraSensor> sensor_;\n \tlibcamera::V4L2Subdevice *debayer_;\n \tlibcamera::V4L2VideoDevice *capture_;\n \tstd::vector<std::unique_ptr<libcamera::FrameBuffer>> buffers_;\n",
    "prefixes": [
        "1/3"
    ]
}