{"id":19612,"url":"https://patchwork.libcamera.org/api/patches/19612/?format=json","web_url":"https://patchwork.libcamera.org/patch/19612/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20240301212121.9072-22-laurent.pinchart@ideasonboard.com>","date":"2024-03-01T21:21:10","name":"[PATCH/RFC,21/32] libcamera: camera_sensor: Sort factories by priority","commit_ref":null,"pull_url":null,"state":"rfc","archived":false,"hash":"1167069ec675566806772823dc2fb12962eb29e0","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/19612/mbox/","series":[{"id":4197,"url":"https://patchwork.libcamera.org/api/series/4197/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=4197","date":"2024-03-01T21:20:49","name":"libcamera: Support the upstream Unicam driver","version":1,"mbox":"https://patchwork.libcamera.org/series/4197/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/19612/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/19612/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 B0ADFBD160\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri,  1 Mar 2024 21:21:53 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 678E862C82;\n\tFri,  1 Mar 2024 22:21:53 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id C58E6628AC\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri,  1 Mar 2024 22:21:50 +0100 (CET)","from pendragon.ideasonboard.com (89-27-53-110.bb.dnainternet.fi\n\t[89.27.53.110])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 5E882293D;\n\tFri,  1 Mar 2024 22:21:36 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"vZNL0+Wr\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1709328096;\n\tbh=HTen9yhdwhNj3A4+larhQckfxT8mZx8SBdgV+NXMWkY=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=vZNL0+WrW3Eoy+VW4s8eXtxUawTzl3KdWrIK4gRuiNkBxHJN/qoxBVNbqZT8UgTAb\n\tyFM33iIukXHqDDCko0g5TRZGgXjzNYPkkA8XpBy/QEJ6G+zKuB1AAuw60x2KaZ2I9b\n\tKFJa6K9auV+XPqqTrjELTwaCeuJNshQerHjEmYLI=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Subject":"[PATCH/RFC 21/32] libcamera: camera_sensor: Sort factories by\n\tpriority","Date":"Fri,  1 Mar 2024 23:21:10 +0200","Message-ID":"<20240301212121.9072-22-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.43.0","In-Reply-To":"<20240301212121.9072-1-laurent.pinchart@ideasonboard.com>","References":"<20240301212121.9072-1-laurent.pinchart@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>","Cc":"Sakari Ailus <sakari.ailus@iki.fi>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"In order to support a default implementation for camera sensors when no\nbetter implementation matches, libcamera needs to try \"specialized\"\nimplementations first and pick the default last. Make this possible by\nadding a priority value for factories. Newly registered factories are\ninserted in the factories list sorted by descending priority, and the\ndefault factory uses a negative priority to be inserted as the last\nelement.\n\nThis mechanism may be a bit overkill in the sense that there is no\nexpected use cases for priorities other than trying the default last,\nbut the implementation is simple and easy to understand.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n include/libcamera/internal/camera_sensor.h    | 18 +++++++---\n src/libcamera/sensor/camera_sensor.cpp        | 34 +++++++++++++++++--\n src/libcamera/sensor/camera_sensor_legacy.cpp |  2 +-\n 3 files changed, 45 insertions(+), 9 deletions(-)","diff":"diff --git a/include/libcamera/internal/camera_sensor.h b/include/libcamera/internal/camera_sensor.h\nindex d37e66773195..59785ff62019 100644\n--- a/include/libcamera/internal/camera_sensor.h\n+++ b/include/libcamera/internal/camera_sensor.h\n@@ -79,11 +79,13 @@ public:\n class CameraSensorFactoryBase\n {\n public:\n-\tCameraSensorFactoryBase();\n+\tCameraSensorFactoryBase(int priority);\n \tvirtual ~CameraSensorFactoryBase() = default;\n \n \tstatic std::unique_ptr<CameraSensor> create(MediaEntity *entity);\n \n+\tint priority() const { return priority_; }\n+\n private:\n \tLIBCAMERA_DISABLE_COPY_AND_MOVE(CameraSensorFactoryBase)\n \n@@ -93,14 +95,16 @@ private:\n \n \tvirtual std::variant<std::unique_ptr<CameraSensor>, int>\n \tmatch(MediaEntity *entity) const = 0;\n+\n+\tint priority_;\n };\n \n template<typename _CameraSensor>\n class CameraSensorFactory final : public CameraSensorFactoryBase\n {\n public:\n-\tCameraSensorFactory()\n-\t\t: CameraSensorFactoryBase()\n+\tCameraSensorFactory(int priority = 0)\n+\t\t: CameraSensorFactoryBase(priority)\n \t{\n \t}\n \n@@ -112,7 +116,11 @@ private:\n \t}\n };\n \n-#define REGISTER_CAMERA_SENSOR(sensor) \\\n-\tstatic CameraSensorFactory<sensor> global_##sensor##Factory{};\n+#ifndef __DOXYGEN__\n+#define REGISTER_CAMERA_SENSOR(sensor, ...) \\\n+\tstatic CameraSensorFactory<sensor> global_##sensor##Factory{ __VA_ARGS__ };\n+#else\n+#define REGISTER_CAMERA_SENSOR(sensor, priority)\n+#endif\n \n } /* namespace libcamera */\ndiff --git a/src/libcamera/sensor/camera_sensor.cpp b/src/libcamera/sensor/camera_sensor.cpp\nindex 7abbe2c76596..e7b526c2f050 100644\n--- a/src/libcamera/sensor/camera_sensor.cpp\n+++ b/src/libcamera/sensor/camera_sensor.cpp\n@@ -348,11 +348,13 @@ CameraSensor::~CameraSensor() = default;\n \n /**\n  * \\brief Construct a camera sensor factory base\n+ * \\param[in] priority Priority order for factory selection\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+CameraSensorFactoryBase::CameraSensorFactoryBase(int priority)\n+\t: priority_(priority)\n {\n \tregisterFactory(this);\n }\n@@ -361,6 +363,12 @@ CameraSensorFactoryBase::CameraSensorFactoryBase()\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+ * When multiple factories match the same \\a entity, this function selects the\n+ * matching factory with the highest priority as specified to the\n+ * REGISTER_CAMERA_SENSOR() macro at factory registration time. If multiple\n+ * matching factories have the same highest priority value, which factory gets\n+ * selected is undefined and may vary between runs.\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@@ -387,8 +395,17 @@ std::unique_ptr<CameraSensor> CameraSensorFactoryBase::create(MediaEntity *entit\n \treturn nullptr;\n }\n \n+/**\n+ * \\fn CameraSensorFactoryBase::priority()\n+ * \\brief Retrieve the priority value for the factory\n+ * \\return The priority value for the factory\n+ */\n+\n /**\n  * \\brief Retrieve the list of all camera sensor factories\n+ *\n+ * The factories are sorted in decreasing priority order.\n+ *\n  * \\return The list of camera sensor factories\n  */\n std::vector<CameraSensorFactoryBase *> &CameraSensorFactoryBase::factories()\n@@ -411,7 +428,12 @@ void CameraSensorFactoryBase::registerFactory(CameraSensorFactoryBase *factory)\n \tstd::vector<CameraSensorFactoryBase *> &factories =\n \t\tCameraSensorFactoryBase::factories();\n \n-\tfactories.push_back(factory);\n+\tauto pos = std::upper_bound(factories.begin(), factories.end(), factory,\n+\t\t\t\t    [](const CameraSensorFactoryBase *value,\n+\t\t\t\t       const CameraSensorFactoryBase *elem) {\n+\t\t\t\t\t    return value->priority() > elem->priority();\n+\t\t\t\t    });\n+\tfactories.insert(pos, factory);\n }\n \n /**\n@@ -437,9 +459,10 @@ void CameraSensorFactoryBase::registerFactory(CameraSensorFactoryBase *factory)\n  */\n \n /**\n- * \\def REGISTER_CAMERA_SENSOR(sensor)\n+ * \\def REGISTER_CAMERA_SENSOR(sensor, priority)\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+ * \\param[in] priority Priority order for factory selection\n  *\n  * Register a CameraSensor subclass with the factory and make it available to\n  * try and match sensors. The subclass needs to implement a static match\n@@ -457,6 +480,11 @@ void CameraSensorFactoryBase::registerFactory(CameraSensorFactoryBase *factory)\n  *   creation succeeded ;\n  * - A non-zero error code if the entity matched and the creation failed ; or\n  * - A zero error code if the entity didn't match.\n+ *\n+ * When multiple factories can support the same MediaEntity (as in the match()\n+ * function of multiple factories returning true for the same entity), the \\a\n+ * priority argument selects which factory will be used. See\n+ * CameraSensorFactoryBase::create() for more information.\n  */\n \n } /* namespace libcamera */\ndiff --git a/src/libcamera/sensor/camera_sensor_legacy.cpp b/src/libcamera/sensor/camera_sensor_legacy.cpp\nindex 34677339241c..f9f889a125d0 100644\n--- a/src/libcamera/sensor/camera_sensor_legacy.cpp\n+++ b/src/libcamera/sensor/camera_sensor_legacy.cpp\n@@ -1010,6 +1010,6 @@ std::string CameraSensorLegacy::logPrefix() const\n \treturn \"'\" + entity_->name() + \"'\";\n }\n \n-REGISTER_CAMERA_SENSOR(CameraSensorLegacy)\n+REGISTER_CAMERA_SENSOR(CameraSensorLegacy, -100)\n \n } /* namespace libcamera */\n","prefixes":["PATCH/RFC","21/32"]}