Patch Detail
Show a patch.
GET /api/patches/2796/?format=api
{ "id": 2796, "url": "https://patchwork.libcamera.org/api/patches/2796/?format=api", "web_url": "https://patchwork.libcamera.org/patch/2796/", "project": { "id": 1, "url": "https://patchwork.libcamera.org/api/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": "<20200206185247.202233-4-jacopo@jmondi.org>", "date": "2020-02-06T18:52:43", "name": "[libcamera-devel,v2,3/7] libcamera: camera_sensor: Introduce CameraSensorFactory", "commit_ref": null, "pull_url": null, "state": "superseded", "archived": false, "hash": "f52bea93e1f96f8693ec934be8708ccd99a97388", "submitter": { "id": 3, "url": "https://patchwork.libcamera.org/api/people/3/?format=api", "name": "Jacopo Mondi", "email": "jacopo@jmondi.org" }, "delegate": null, "mbox": "https://patchwork.libcamera.org/patch/2796/mbox/", "series": [ { "id": 664, "url": "https://patchwork.libcamera.org/api/series/664/?format=api", "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=664", "date": "2020-02-06T18:52:40", "name": "Camera sensor factory", "version": 2, "mbox": "https://patchwork.libcamera.org/series/664/mbox/" } ], "comments": "https://patchwork.libcamera.org/api/patches/2796/comments/", "check": "pending", "checks": "https://patchwork.libcamera.org/api/patches/2796/checks/", "tags": {}, "headers": { "Return-Path": "<jacopo@jmondi.org>", "Received": [ "from relay9-d.mail.gandi.net (relay9-d.mail.gandi.net\n\t[217.70.183.199])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id C941960864\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 6 Feb 2020 19:50:19 +0100 (CET)", "from uno.lan (93-34-114-233.ip49.fastwebnet.it [93.34.114.233])\n\t(Authenticated sender: jacopo@jmondi.org)\n\tby relay9-d.mail.gandi.net (Postfix) with ESMTPSA id 6F974FF804\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 6 Feb 2020 18:50:19 +0000 (UTC)" ], "X-Originating-IP": "93.34.114.233", "From": "Jacopo Mondi <jacopo@jmondi.org>", "To": "libcamera-devel@lists.libcamera.org", "Date": "Thu, 6 Feb 2020 19:52:43 +0100", "Message-Id": "<20200206185247.202233-4-jacopo@jmondi.org>", "X-Mailer": "git-send-email 2.24.1", "In-Reply-To": "<20200206185247.202233-1-jacopo@jmondi.org>", "References": "<20200206185247.202233-1-jacopo@jmondi.org>", "MIME-Version": "1.0", "Content-Transfer-Encoding": "8bit", "Subject": "[libcamera-devel] [PATCH v2 3/7] libcamera: camera_sensor:\n\tIntroduce CameraSensorFactory", "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>", "X-List-Received-Date": "Thu, 06 Feb 2020 18:50:20 -0000" }, "content": "Introduce 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>\n---\n src/libcamera/camera_sensor.cpp | 134 ++++++++++++++++++\n src/libcamera/include/camera_sensor.h | 39 ++++-\n src/libcamera/pipeline/ipu3/ipu3.cpp | 2 +-\n src/libcamera/pipeline/rkisp1/rkisp1.cpp | 2 +-\n src/libcamera/pipeline/vimc.cpp | 2 +-\n test/camera-sensor.cpp | 2 +-\n .../v4l2_videodevice_test.cpp | 2 +-\n 7 files changed, 177 insertions(+), 6 deletions(-)", "diff": "diff --git a/src/libcamera/camera_sensor.cpp b/src/libcamera/camera_sensor.cpp\nindex 2219a4307436..fc8452b607a0 100644\n--- a/src/libcamera/camera_sensor.cpp\n+++ b/src/libcamera/camera_sensor.cpp\n@@ -11,7 +11,9 @@\n #include <float.h>\n #include <iomanip>\n #include <limits.h>\n+#include <map>\n #include <math.h>\n+#include <string.h>\n \n #include <libcamera/property_ids.h>\n \n@@ -42,6 +44,18 @@ LOG_DEFINE_CATEGORY(CameraSensor);\n * devices as the needs arise.\n */\n \n+/**\n+ * \\fn CameraSensor::entityName()\n+ * \\brief Retrieve the name of the entity which identifies the supported sensor\n+ *\n+ * Camera sensor handlers have to report with this function the name of the\n+ * media entity which represents the camera sensor they support. The here\n+ * reported name is then matched against the name of the MediaEntity provided\n+ * at createSensor() time to identify the correct CameraSensorFactory.\n+ *\n+ * \\return The name of the media entity that identifies the sensor\n+ */\n+\n /**\n * \\brief Construct a CameraSensor\n * \\param[in] entity The media entity backing the camera sensor\n@@ -366,4 +380,124 @@ std::string CameraSensor::logPrefix() const\n \treturn \"'\" + subdev_->entity()->name() + \"'\";\n }\n \n+/**\n+ * \\class CameraSensorFactory\n+ * \\brief Factory of camera sensor handlers\n+ *\n+ * The class provides to camera sensor handlers the ability to register\n+ * themselves to the factory to allow the creation of their instances by\n+ * matching the name of the media entity which represents the sensor.\n+ *\n+ * Camera sensor handlers use the REGISTER_CAMERA_SENSOR() macro to\n+ * add themselves to the camera sensor factory. Users of the factory\n+ * creates camera sensor handler instances by using the static\n+ * CameraSensorFactory::createInstance() method, which returns a pointer\n+ * to the opportune CameraSensor sub-class if any, or a newly created instance\n+ * of the generic CameraSensor class.\n+ */\n+\n+LOG_DEFINE_CATEGORY(CameraSensorFactory);\n+\n+/**\n+ * \\typedef CameraSensorFactory::factoriesMap\n+ * \\brief An std::map of sensor handler factories\n+ *\n+ * The registered sensor handler factories are associated in a map with the\n+ * names of the media entity that represent the sensor\n+ */\n+\n+/**\n+ * \\brief Register a camera sensor handler factory\n+ * \\param[in] name The name of the media entity representing the sensor\n+ *\n+ * Register a new camera sensor factory which creates sensor handler instances\n+ * that support camera sensors represented by the media entity with \\a name.\n+ */\n+CameraSensorFactory::CameraSensorFactory(const char *name)\n+{\n+\tCameraSensorFactory::registerSensorFactory(name, this);\n+}\n+\n+/**\n+ * \\brief Retrieve the list of registered camera sensor factories\n+ *\n+ * The static factories map is defined inside the function to ensures it gets\n+ * initialized on first use, without any dependency on link order.\n+ *\n+ * \\return The static list of registered camera sensor factories\n+ */\n+CameraSensorFactory::factoriesMap &CameraSensorFactory::factories()\n+{\n+\tstatic factoriesMap factories;\n+\treturn factories;\n+}\n+\n+/**\n+ * \\brief Register a camera sensor handler factory\n+ * \\param[in] name The name of the media entity that represents the sensor\n+ * \\param[in] factory The factory instance to register\n+ *\n+ * The newly registered sensor handler \\a factory is associated with \\a name,\n+ * when a new sensor handler is instantiated with createSensor() the name of\n+ * the media entity is matched against the \\a name registered here to\n+ * retrieve the correct factory.\n+ */\n+void CameraSensorFactory::registerSensorFactory(const char *name,\n+\t\t\t\t\t\tCameraSensorFactory *factory)\n+{\n+\tfactoriesMap &map = CameraSensorFactory::factories();\n+\tmap[name] = factory;\n+}\n+\n+/**\n+ * \\brief Create and return a CameraSensor\n+ * \\param[in] entity The media entity that represents the camera sensor\n+ *\n+ * Create a new instance of an handler for the sensor represented by \\a\n+ * entity using one of the registered factories. If no specific handler is\n+ * available for the sensor, or creating the handler fails, a newly created\n+ * instance of the generic CameraSensor base class is returned.\n+ *\n+ * Ownership of the created camera sensor instance is passed to the caller\n+ * which is reponsible for deleting the instance.\n+ * FIXME: is it worth using a smart pointer here ?\n+ *\n+ * \\return A newly created camera sensor handler instance\n+ */\n+CameraSensor *CameraSensorFactory::createSensor(const MediaEntity *entity)\n+{\n+\t/*\n+\t * The entity name contains both the sensor name and the I2C bus ID.\n+\t *\n+\t * Remove the i2c bus part and use the sensor name only as key to\n+\t * search for on the sensor handlers map.\n+\t */\n+\tconst char *entityName = entity->name().c_str();\n+\tconst char *delim = strchrnul(entityName, ' ');\n+\tstd::string sensorName(entityName, delim);\n+\n+\tauto it = CameraSensorFactory::factories().find(sensorName);\n+\tif (it == CameraSensorFactory::factories().end()) {\n+\t\tLOG(CameraSensorFactory, Info)\n+\t\t\t<< \"Unsupported sensor '\" << entity->name()\n+\t\t\t<< \"': using generic sensor handler\";\n+\t\treturn new CameraSensor(entity);\n+\t}\n+\n+\tLOG(CameraSensorFactory, Info) << \"Create handler for '\"\n+\t\t\t\t << entity->name() << \"' sensor\";\n+\n+\tCameraSensorFactory *factory = it->second;\n+\treturn factory->create(entity);\n+}\n+\n+/**\n+ * \\def REGISTER_CAMERA_SENSOR(handler)\n+ * \\brief Register a camera sensor handler to the sensor factory\n+ * \\param[in] handler The name of the sensor handler\n+ *\n+ * Register a camera sensor handler to the sensor factory to make it available\n+ * to the factory users.\n+ */\n+\n } /* namespace libcamera */\ndiff --git a/src/libcamera/include/camera_sensor.h b/src/libcamera/include/camera_sensor.h\nindex 99cff98128dc..2f4a0cc8ad3f 100644\n--- a/src/libcamera/include/camera_sensor.h\n+++ b/src/libcamera/include/camera_sensor.h\n@@ -7,6 +7,7 @@\n #ifndef __LIBCAMERA_CAMERA_SENSOR_H__\n #define __LIBCAMERA_CAMERA_SENSOR_H__\n \n+#include <map>\n #include <string>\n #include <vector>\n \n@@ -25,7 +26,8 @@ struct V4L2SubdeviceFormat;\n class CameraSensor : protected Loggable\n {\n public:\n-\texplicit CameraSensor(const MediaEntity *entity);\n+\tstatic const char *entityName();\n+\n \t~CameraSensor();\n \n \tCameraSensor(const CameraSensor &) = delete;\n@@ -49,6 +51,8 @@ public:\n \tconst ControlList &properties() const { return properties_; }\n \n protected:\n+\tfriend class CameraSensorFactory;\n+\texplicit CameraSensor(const MediaEntity *entity);\n \tstd::string logPrefix() const;\n \n private:\n@@ -61,6 +65,39 @@ private:\n \tControlList properties_;\n };\n \n+class CameraSensorFactory\n+{\n+public:\n+\tusing factoriesMap = std::map<const std::string, CameraSensorFactory *>;\n+\n+\tCameraSensorFactory(const char *name);\n+\tvirtual ~CameraSensorFactory() {}\n+\n+\tstatic CameraSensor *createSensor(const MediaEntity *entity = nullptr);\n+\n+private:\n+\tstatic factoriesMap &factories();\n+\tstatic void registerSensorFactory(const char *name,\n+\t\t\t\t\t CameraSensorFactory *factory);\n+\tvirtual CameraSensor *create(const MediaEntity *entity) = 0;\n+\n+};\n+\n+#define REGISTER_CAMERA_SENSOR(handler)\t\t\t\t\t\\\n+class handler##CameraSensorFactory final : public CameraSensorFactory\t\\\n+{\t\t\t\t\t\t\t\t\t\\\n+public:\t\t\t\t\t\t\t\t\t\\\n+\thandler##CameraSensorFactory()\t\t\t\t\t\\\n+\t\t: CameraSensorFactory(handler##CameraSensor::entityName()) {}\\\n+\t\t\t\t\t\t\t\t\t\\\n+private:\t\t\t\t\t\t\t\t\\\n+\tCameraSensor *create(const MediaEntity *entity)\t\t\t\\\n+\t{\t\t\t\t\t\t\t\t\\\n+\t\treturn new handler##CameraSensor(entity);\t\t\\\n+\t}\t\t\t\t\t\t\t\t\\\n+};\t\t\t\t\t\t\t\t\t\\\n+static handler##CameraSensorFactory global_##handler##CameraSensorFactory\n+\n } /* namespace libcamera */\n \n #endif /* __LIBCAMERA_CAMERA_SENSOR_H__ */\ndiff --git a/src/libcamera/pipeline/ipu3/ipu3.cpp b/src/libcamera/pipeline/ipu3/ipu3.cpp\nindex 387bb070b505..21934e72eba7 100644\n--- a/src/libcamera/pipeline/ipu3/ipu3.cpp\n+++ b/src/libcamera/pipeline/ipu3/ipu3.cpp\n@@ -1322,7 +1322,7 @@ int CIO2Device::init(const MediaDevice *media, unsigned int index)\n \n \tMediaLink *link = links[0];\n \tMediaEntity *sensorEntity = link->source()->entity();\n-\tsensor_ = new CameraSensor(sensorEntity);\n+\tsensor_ = CameraSensorFactory::createSensor(sensorEntity);\n \tret = sensor_->init();\n \tif (ret)\n \t\treturn ret;\ndiff --git a/src/libcamera/pipeline/rkisp1/rkisp1.cpp b/src/libcamera/pipeline/rkisp1/rkisp1.cpp\nindex 68f16f03a81e..f2f054596257 100644\n--- a/src/libcamera/pipeline/rkisp1/rkisp1.cpp\n+++ b/src/libcamera/pipeline/rkisp1/rkisp1.cpp\n@@ -901,7 +901,7 @@ int PipelineHandlerRkISP1::createCamera(MediaEntity *sensor)\n \n \tdata->controlInfo_ = std::move(ctrls);\n \n-\tdata->sensor_ = new CameraSensor(sensor);\n+\tdata->sensor_ = CameraSensorFactory::createSensor(sensor);\n \tret = data->sensor_->init();\n \tif (ret)\n \t\treturn ret;\ndiff --git a/src/libcamera/pipeline/vimc.cpp b/src/libcamera/pipeline/vimc.cpp\nindex fd4df0b03c26..cfeec1aac751 100644\n--- a/src/libcamera/pipeline/vimc.cpp\n+++ b/src/libcamera/pipeline/vimc.cpp\n@@ -403,7 +403,7 @@ int VimcCameraData::init(MediaDevice *media)\n \t\treturn ret;\n \n \t/* Create and open the camera sensor, debayer, scaler and video device. */\n-\tsensor_ = new CameraSensor(media->getEntityByName(\"Sensor B\"));\n+\tsensor_ = CameraSensorFactory::createSensor(media->getEntityByName(\"Sensor B\"));\n \tret = sensor_->init();\n \tif (ret)\n \t\treturn ret;\ndiff --git a/test/camera-sensor.cpp b/test/camera-sensor.cpp\nindex 27c190fe7ace..24b83a45b656 100644\n--- a/test/camera-sensor.cpp\n+++ b/test/camera-sensor.cpp\n@@ -50,7 +50,7 @@ protected:\n \t\t\treturn TestFail;\n \t\t}\n \n-\t\tsensor_ = new CameraSensor(entity);\n+\t\tsensor_ = CameraSensorFactory::createSensor(entity);\n \t\tif (sensor_->init() < 0) {\n \t\t\tcerr << \"Unable to initialise camera sensor\" << endl;\n \t\t\treturn TestFail;\ndiff --git a/test/v4l2_videodevice/v4l2_videodevice_test.cpp b/test/v4l2_videodevice/v4l2_videodevice_test.cpp\nindex 577da4cb601c..102a8b1b6c1c 100644\n--- a/test/v4l2_videodevice/v4l2_videodevice_test.cpp\n+++ b/test/v4l2_videodevice/v4l2_videodevice_test.cpp\n@@ -61,7 +61,7 @@ int V4L2VideoDeviceTest::init()\n \t\treturn TestFail;\n \n \tif (driver_ == \"vimc\") {\n-\t\tsensor_ = new CameraSensor(media_->getEntityByName(\"Sensor A\"));\n+\t\tsensor_ = CameraSensorFactory::createSensor(media_->getEntityByName(\"Sensor A\"));\n \t\tif (sensor_->init())\n \t\t\treturn TestFail;\n \n", "prefixes": [ "libcamera-devel", "v2", "3/7" ] }