Patch Detail
Show a patch.
GET /api/1.1/patches/3331/?format=api
{ "id": 3331, "url": "https://patchwork.libcamera.org/api/1.1/patches/3331/?format=api", "web_url": "https://patchwork.libcamera.org/patch/3331/", "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": "<20200326145927.324919-4-jacopo@jmondi.org>", "date": "2020-03-26T14:59:24", "name": "[libcamera-devel,v4,3/6] libcamera: camera_sensor: Introduce CameraSensorFactory", "commit_ref": null, "pull_url": null, "state": "rfc", "archived": false, "hash": "deb9af9883f5506c6d519c1888d2f793878b930b", "submitter": { "id": 3, "url": "https://patchwork.libcamera.org/api/1.1/people/3/?format=api", "name": "Jacopo Mondi", "email": "jacopo@jmondi.org" }, "delegate": { "id": 15, "url": "https://patchwork.libcamera.org/api/1.1/users/15/?format=api", "username": "jmondi", "first_name": "Jacopo", "last_name": "Mondi", "email": "jacopo@jmondi.org" }, "mbox": "https://patchwork.libcamera.org/patch/3331/mbox/", "series": [ { "id": 782, "url": "https://patchwork.libcamera.org/api/1.1/series/782/?format=api", "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=782", "date": "2020-03-26T14:59:21", "name": "Camera properties and camera sensor factory", "version": 4, "mbox": "https://patchwork.libcamera.org/series/782/mbox/" } ], "comments": "https://patchwork.libcamera.org/api/patches/3331/comments/", "check": "pending", "checks": "https://patchwork.libcamera.org/api/patches/3331/checks/", "tags": {}, "headers": { "Return-Path": "<jacopo@jmondi.org>", "Received": [ "from relay7-d.mail.gandi.net (relay7-d.mail.gandi.net\n\t[217.70.183.200])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id AA3F762B90\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 26 Mar 2020 15:56:34 +0100 (CET)", "from localhost.localdomain (2-224-242-101.ip172.fastwebnet.it\n\t[2.224.242.101]) (Authenticated sender: jacopo@jmondi.org)\n\tby relay7-d.mail.gandi.net (Postfix) with ESMTPSA id 615F020009\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 26 Mar 2020 14:56:33 +0000 (UTC)" ], "X-Originating-IP": "2.224.242.101", "From": "Jacopo Mondi <jacopo@jmondi.org>", "To": "libcamera-devel@lists.libcamera.org", "Date": "Thu, 26 Mar 2020 15:59:24 +0100", "Message-Id": "<20200326145927.324919-4-jacopo@jmondi.org>", "X-Mailer": "git-send-email 2.25.1", "In-Reply-To": "<20200326145927.324919-1-jacopo@jmondi.org>", "References": "<20200326145927.324919-1-jacopo@jmondi.org>", "MIME-Version": "1.0", "Content-Transfer-Encoding": "8bit", "Subject": "[libcamera-devel] [PATCH v4 3/6] 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, 26 Mar 2020 14:56:36 -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 camera sensor sub-classes.\n\nReviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\nSigned-off-by: Jacopo Mondi <jacopo@jmondi.org>\n---\n src/libcamera/camera_sensor.cpp | 132 ++++++++++++++++++\n src/libcamera/include/camera_sensor.h | 32 +++++\n src/libcamera/pipeline/ipu3/ipu3.cpp | 9 +-\n src/libcamera/pipeline/rkisp1/rkisp1.cpp | 18 +--\n src/libcamera/pipeline/vimc/vimc.cpp | 6 +-\n test/camera-sensor.cpp | 9 +-\n .../v4l2_videodevice_test.cpp | 3 +-\n test/v4l2_videodevice/v4l2_videodevice_test.h | 6 +-\n 8 files changed, 183 insertions(+), 32 deletions(-)", "diff": "diff --git a/src/libcamera/camera_sensor.cpp b/src/libcamera/camera_sensor.cpp\nindex fc24b3bd547d..c1a29e0b3cfc 100644\n--- a/src/libcamera/camera_sensor.cpp\n+++ b/src/libcamera/camera_sensor.cpp\n@@ -12,6 +12,8 @@\n #include <iomanip>\n #include <limits.h>\n #include <math.h>\n+#include <memory>\n+#include <string.h>\n \n #include <libcamera/property_ids.h>\n \n@@ -366,4 +368,134 @@ std::string CameraSensor::logPrefix() const\n \treturn \"'\" + subdev_->entity()->name() + \"'\";\n }\n \n+/**\n+ * \\class CameraSensorFactory\n+ * \\brief Factory of camera sensors\n+ *\n+ * The class provides camera sensor with the ability to register themselves to\n+ * the factory to allow the creation of their instances by matching the name of\n+ * the media entity which represents the sensor.\n+ *\n+ * Camera sensor subclasses use the REGISTER_CAMERA_SENSOR() macro to register\n+ * themselves to the camera sensor factory. Users of the factory create camera\n+ * sensor instances for a media entity by using the static createInstance()\n+ * method, which returns an instance of the CameraSensor sub-class matching the\n+ * entity, or a newly created instance of the generic CameraSensor class.\n+ */\n+\n+/**\n+ * \\brief Construct a camera sensor factory\n+ * \\param[in] name The name of the media entity representing the sensor\n+ *\n+ * Register a new camera sensor factory which creates CameraSensor instances\n+ * to support camera sensors represented by the media entity with \\a name.\n+ *\n+ * Creating an instance of the factory registers it with the global list of\n+ * factories, accessible through the factories() function.\n+ */\n+CameraSensorFactory::CameraSensorFactory(const char *name)\n+{\n+\tregisterFactory(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 ensure 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 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 camera sensor \\a factory is registered associated with an entity \\a name.\n+ * When a camera sensor is created for a media entity with createSensor(), the\n+ * name of the media entity is used to select the corresponding factory.\n+ *\n+ * The caller is responsible for guaranteeing the uniqueness of the\n+ * camera sensor entity name.\n+ */\n+void CameraSensorFactory::registerFactory(const char *name,\n+\t\t\t\t\t CameraSensorFactory *factory)\n+{\n+\tFactoriesMap &map = factories();\n+\n+\tif (map.find(name) != map.end()) {\n+\t\tLOG(CameraSensor, Error)\n+\t\t\t<< \"Unable to register camera sensor factory '\"\n+\t\t\t<< name << \"': already registered\";\n+\t\treturn;\n+\t}\n+\n+\tmap[name] = factory;\n+}\n+\n+/**\n+ * \\brief Create a camera sensor corresponding to \\a entity\n+ * \\param[in] entity The media entity that represents the camera sensor\n+ *\n+ * Create a new instance of a CameraSensor subclass for the sensor represented\n+ * by \\a entity using one of the registered factories. If no specific class is\n+ * available for the sensor, an instance of the generic CameraSensor class is\n+ * created and returned.\n+ *\n+ * Ownership of the created camera sensor instance is passed to the caller\n+ * which is responsible for deleting the instance.\n+ *\n+ * Ownership of the created camera sensor instance is passed to the caller as\n+ * an unique pointer.\n+ *\n+ * \\return A unique pointer to the newly created camera sensor instance\n+ */\n+std::unique_ptr<CameraSensor> CameraSensorFactory::createSensor(const MediaEntity *entity)\n+{\n+\tconst std::string &name = entity->name();\n+\tfor (const auto &it : factories()) {\n+\t\tconst std::string &key = it.first;\n+\n+\t\t/*\n+\t\t * Search for the key, provided by the CameraSensor sub-class at\n+\t\t * factory registration time, with the sensor entity name, to\n+\t\t * support both canonical sensor entity names (in the \"devname\n+\t\t * i2c-adapt:i2c-addr\" format) and non-canonical sensor names.\n+\t\t *\n+\t\t * \\todo Delegate matching to the CameraSensor sub-class\n+\t\t * to support more complex matching criteria.\n+\t\t */\n+\t\tif (name.find(key) == std::string::npos)\n+\t\t\tcontinue;\n+\n+\t\tLOG(CameraSensor, Debug) << \"Create camera sensor for '\"\n+\t\t\t\t\t << name << \"'\";\n+\n+\t\tCameraSensorFactory *factory = it.second;\n+\t\treturn std::unique_ptr<CameraSensor>(factory->create(entity));\n+\t}\n+\n+\tLOG(CameraSensor, Debug) << \"No specific support for sensor '\"\n+\t\t\t\t << entity->name()\n+\t\t\t\t << \"': using generic implementation\";\n+\treturn std::make_unique<CameraSensor>(entity);\n+}\n+\n+/**\n+ * \\def REGISTER_CAMERA_SENSOR(sensor, sensorName)\n+ * \\brief Register camera sensor \\a name with the sensor factory\n+ * \\param[in] sensor The name of the camera sensor class to register\n+ * \\param[in] sensorName The name of the image sensor supported by the factory\n+ *\n+ * Register camera sensor subclass \\a sensor in the global list of factories.\n+ * \\a sensorName corresponds to the sensor entity name without the bus\n+ * information, for instance \"ov5670\" for a sensor entity named \"ov5645 4-003c\".\n+ */\n+\n } /* namespace libcamera */\ndiff --git a/src/libcamera/include/camera_sensor.h b/src/libcamera/include/camera_sensor.h\nindex 99cff98128dc..8ffc787e740f 100644\n--- a/src/libcamera/include/camera_sensor.h\n+++ b/src/libcamera/include/camera_sensor.h\n@@ -7,6 +7,8 @@\n #ifndef __LIBCAMERA_CAMERA_SENSOR_H__\n #define __LIBCAMERA_CAMERA_SENSOR_H__\n \n+#include <map>\n+#include <memory>\n #include <string>\n #include <vector>\n \n@@ -61,6 +63,36 @@ private:\n \tControlList properties_;\n };\n \n+class CameraSensorFactory\n+{\n+public:\n+\tCameraSensorFactory(const char *name);\n+\tvirtual ~CameraSensorFactory() {}\n+\n+\tstatic std::unique_ptr<CameraSensor> createSensor(const MediaEntity *entity);\n+\n+private:\n+\tusing FactoriesMap = std::map<const std::string, CameraSensorFactory *>;\n+\tstatic FactoriesMap &factories();\n+\tstatic void registerFactory(const char *name,\n+\t\t\t\t CameraSensorFactory *factory);\n+\tvirtual CameraSensor *create(const MediaEntity *entity) = 0;\n+};\n+\n+#define REGISTER_CAMERA_SENSOR(sensor, sensorName)\t\t\t\\\n+class sensor##Factory final : public CameraSensorFactory\t\t\\\n+{\t\t\t\t\t\t\t\t\t\\\n+public:\t\t\t\t\t\t\t\t\t\\\n+\tsensor##Factory() : CameraSensorFactory(sensorName) {}\t\t\\\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 sensor(entity);\t\t\t\t\\\n+\t}\t\t\t\t\t\t\t\t\\\n+};\t\t\t\t\t\t\t\t\t\\\n+static sensor##Factory global_##sensor##Factory\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 1b44460e4d88..df2ab17cd5d6 100644\n--- a/src/libcamera/pipeline/ipu3/ipu3.cpp\n+++ b/src/libcamera/pipeline/ipu3/ipu3.cpp\n@@ -100,7 +100,7 @@ public:\n \tstatic constexpr unsigned int CIO2_BUFFER_COUNT = 4;\n \n \tCIO2Device()\n-\t\t: output_(nullptr), csi2_(nullptr), sensor_(nullptr)\n+\t\t: output_(nullptr), csi2_(nullptr)\n \t{\n \t}\n \n@@ -108,7 +108,6 @@ public:\n \t{\n \t\tdelete output_;\n \t\tdelete csi2_;\n-\t\tdelete sensor_;\n \t}\n \n \tint init(const MediaDevice *media, unsigned int index);\n@@ -125,7 +124,7 @@ public:\n \n \tV4L2VideoDevice *output_;\n \tV4L2Subdevice *csi2_;\n-\tCameraSensor *sensor_;\n+\tstd::unique_ptr<CameraSensor> sensor_;\n \n private:\n \tstd::vector<std::unique_ptr<FrameBuffer>> buffers_;\n@@ -292,7 +291,7 @@ void IPU3CameraConfiguration::adjustStream(StreamConfiguration &cfg, bool scale)\n \n CameraConfiguration::Status IPU3CameraConfiguration::validate()\n {\n-\tconst CameraSensor *sensor = data_->cio2_.sensor_;\n+\tconst CameraSensor *sensor = data_->cio2_.sensor_.get();\n \tStatus status = Valid;\n \n \tif (config_.empty())\n@@ -1313,7 +1312,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 2f909cef7c75..a0e6490909ed 100644\n--- a/src/libcamera/pipeline/rkisp1/rkisp1.cpp\n+++ b/src/libcamera/pipeline/rkisp1/rkisp1.cpp\n@@ -113,20 +113,14 @@ class RkISP1CameraData : public CameraData\n {\n public:\n \tRkISP1CameraData(PipelineHandler *pipe)\n-\t\t: CameraData(pipe), sensor_(nullptr), frame_(0),\n-\t\t frameInfo_(pipe)\n+\t\t: CameraData(pipe), frame_(0), frameInfo_(pipe)\n \t{\n \t}\n \n-\t~RkISP1CameraData()\n-\t{\n-\t\tdelete sensor_;\n-\t}\n-\n \tint loadIPA();\n \n \tStream stream_;\n-\tCameraSensor *sensor_;\n+\tstd::unique_ptr<CameraSensor> sensor_;\n \tunsigned int frame_;\n \tstd::vector<IPABuffer> ipaBuffers_;\n \tRkISP1Frames frameInfo_;\n@@ -386,7 +380,7 @@ void RkISP1CameraData::queueFrameAction(unsigned int frame,\n \tcase RKISP1_IPA_ACTION_V4L2_SET: {\n \t\tconst ControlList &controls = action.controls[0];\n \t\ttimeline_.scheduleAction(std::make_unique<RkISP1ActionSetSensor>(frame,\n-\t\t\t\t\t\t\t\t\t\t sensor_,\n+\t\t\t\t\t\t\t\t\t\t sensor_.get(),\n \t\t\t\t\t\t\t\t\t\t controls));\n \t\tbreak;\n \t}\n@@ -441,7 +435,7 @@ CameraConfiguration::Status RkISP1CameraConfiguration::validate()\n \t\t/* \\todo Add support for 8-bit greyscale to DRM formats */\n \t};\n \n-\tconst CameraSensor *sensor = data_->sensor_;\n+\tconst CameraSensor *sensor = data_->sensor_.get();\n \tStatus status = Valid;\n \n \tif (config_.empty())\n@@ -554,7 +548,7 @@ int PipelineHandlerRkISP1::configure(Camera *camera, CameraConfiguration *c)\n \t\tstatic_cast<RkISP1CameraConfiguration *>(c);\n \tRkISP1CameraData *data = cameraData(camera);\n \tStreamConfiguration &cfg = config->at(0);\n-\tCameraSensor *sensor = data->sensor_;\n+\tCameraSensor *sensor = data->sensor_.get();\n \tint ret;\n \n \t/*\n@@ -889,7 +883,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/vimc.cpp b/src/libcamera/pipeline/vimc/vimc.cpp\nindex b04a9726efa5..805bac0a7377 100644\n--- a/src/libcamera/pipeline/vimc/vimc.cpp\n+++ b/src/libcamera/pipeline/vimc/vimc.cpp\n@@ -8,6 +8,7 @@\n #include <algorithm>\n #include <array>\n #include <iomanip>\n+#include <memory>\n #include <tuple>\n \n #include <linux/media-bus-format.h>\n@@ -46,7 +47,6 @@ public:\n \n \t~VimcCameraData()\n \t{\n-\t\tdelete sensor_;\n \t\tdelete debayer_;\n \t\tdelete scaler_;\n \t\tdelete video_;\n@@ -56,7 +56,7 @@ public:\n \tint init(MediaDevice *media);\n \tvoid bufferReady(FrameBuffer *buffer);\n \n-\tCameraSensor *sensor_;\n+\tstd::unique_ptr<CameraSensor> sensor_;\n \tV4L2Subdevice *debayer_;\n \tV4L2Subdevice *scaler_;\n \tV4L2VideoDevice *video_;\n@@ -398,7 +398,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..8709e481dc7b 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;\n@@ -100,15 +100,10 @@ protected:\n \t\treturn TestPass;\n \t}\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 };\n \n TEST_REGISTER(CameraSensorTest)\ndiff --git a/test/v4l2_videodevice/v4l2_videodevice_test.cpp b/test/v4l2_videodevice/v4l2_videodevice_test.cpp\nindex 93b9e72da5b4..e1c8a886f2db 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@@ -97,6 +97,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 9acaceb84fe0..f635e0929efe 100644\n--- a/test/v4l2_videodevice/v4l2_videodevice_test.h\n+++ b/test/v4l2_videodevice/v4l2_videodevice_test.h\n@@ -25,8 +25,8 @@ class V4L2VideoDeviceTest : public Test\n {\n public:\n \tV4L2VideoDeviceTest(const char *driver, const char *entity)\n-\t\t: driver_(driver), entity_(entity), sensor_(nullptr),\n-\t\t debayer_(nullptr), capture_(nullptr)\n+\t\t: driver_(driver), entity_(entity), debayer_(nullptr),\n+\t\t capture_(nullptr)\n \t{\n \t}\n \n@@ -38,7 +38,7 @@ protected:\n \tstd::string entity_;\n \tstd::unique_ptr<DeviceEnumerator> enumerator_;\n \tstd::shared_ptr<MediaDevice> media_;\n-\tCameraSensor *sensor_;\n+\tstd::unique_ptr<CameraSensor> sensor_;\n \tV4L2Subdevice *debayer_;\n \tV4L2VideoDevice *capture_;\n \tstd::vector<std::unique_ptr<FrameBuffer>> buffers_;\n", "prefixes": [ "libcamera-devel", "v4", "3/6" ] }