Show a patch.

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

{
    "id": 3081,
    "url": "https://patchwork.libcamera.org/api/1.1/patches/3081/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/3081/",
    "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": "<20200309180444.725757-4-jacopo@jmondi.org>",
    "date": "2020-03-09T18:04:41",
    "name": "[libcamera-devel,v3,3/6] libcamera: camera_sensor: Introduce CameraSensorFactory",
    "commit_ref": null,
    "pull_url": null,
    "state": "superseded",
    "archived": false,
    "hash": "f0a56b4a3ef71b7328a0ae510a55a5898078ea23",
    "submitter": {
        "id": 3,
        "url": "https://patchwork.libcamera.org/api/1.1/people/3/?format=api",
        "name": "Jacopo Mondi",
        "email": "jacopo@jmondi.org"
    },
    "delegate": null,
    "mbox": "https://patchwork.libcamera.org/patch/3081/mbox/",
    "series": [
        {
            "id": 717,
            "url": "https://patchwork.libcamera.org/api/1.1/series/717/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=717",
            "date": "2020-03-09T18:04:38",
            "name": "Camera properties and camera sensor factory",
            "version": 3,
            "mbox": "https://patchwork.libcamera.org/series/717/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/3081/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/3081/checks/",
    "tags": {},
    "headers": {
        "Return-Path": "<jacopo@jmondi.org>",
        "Received": [
            "from relay2-d.mail.gandi.net (relay2-d.mail.gandi.net\n\t[217.70.183.194])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 86FCB62950\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon,  9 Mar 2020 19:02:09 +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 relay2-d.mail.gandi.net (Postfix) with ESMTPSA id 4284C40009\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon,  9 Mar 2020 18:02:09 +0000 (UTC)"
        ],
        "X-Originating-IP": "93.34.114.233",
        "From": "Jacopo Mondi <jacopo@jmondi.org>",
        "To": "libcamera-devel@lists.libcamera.org",
        "Date": "Mon,  9 Mar 2020 19:04:41 +0100",
        "Message-Id": "<20200309180444.725757-4-jacopo@jmondi.org>",
        "X-Mailer": "git-send-email 2.25.0",
        "In-Reply-To": "<20200309180444.725757-1-jacopo@jmondi.org>",
        "References": "<20200309180444.725757-1-jacopo@jmondi.org>",
        "MIME-Version": "1.0",
        "Content-Transfer-Encoding": "8bit",
        "Subject": "[libcamera-devel] [PATCH v3 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": "Mon, 09 Mar 2020 18:02:11 -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\nSigned-off-by: Jacopo Mondi <jacopo@jmondi.org>\n\n---\nv2 -> v3:\n- Update documentation\n- Register sensor name as REGISTER_CAMERA_SENSOR() second parameter\n- Create camera sensor as unique_ptr\n- Match entity name and key with std::string::find() in\n  CameraSensorFactory::createSensor()\n---\n src/libcamera/camera_sensor.cpp               | 136 ++++++++++++++++++\n src/libcamera/include/camera_sensor.h         |  32 +++++\n src/libcamera/pipeline/ipu3/ipu3.cpp          |   9 +-\n src/libcamera/pipeline/rkisp1/rkisp1.cpp      |  17 +--\n src/libcamera/pipeline/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, 187 insertions(+), 31 deletions(-)",
    "diff": "diff --git a/src/libcamera/camera_sensor.cpp b/src/libcamera/camera_sensor.cpp\nindex 2219a4307436..8bfe02c9e8ff 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,138 @@ 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 to camera sensors 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 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+/**\n+ * \\brief Construct a camera sensor factory\n+ * \\param[in] name The name of the media entity that represents 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+ * 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 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 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 and associated with an entity \\a\n+ * name. When a camera sensor is created for a media entity with the\n+ * createSensor() method, the name of the media entity there provided is used\n+ * 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 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 as\n+ * a 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 * Match the name of the entity with the keys in the factories\n+\t\t * map. The factories map keys are provided by the CameraSensor\n+\t\t * sub-class at factory registration time as the sensor\n+\t\t * entity name.\n+\t\t *\n+\t\t * To support both canonical sensor entity names (in the\n+\t\t * \"devname i2c-adapt:i2c-addr\" format) and non-canonical sensor\n+\t\t * names such (as VIMC's \"Sensor B\" ones), search for the key\n+\t\t * in the entity name.\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, Info) << \"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, Info) << \"Unsupported sensor '\" << entity->name()\n+\t\t\t\t<< \"': using generic camera sensor\";\n+\treturn std::make_unique<CameraSensor>(entity);\n+}\n+\n+/**\n+ * \\def REGISTER_CAMERA_SENSOR(sensor, entityName)\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] entityName The name of the image sensor supported by the factory\n+ *\n+ * Register camera sensor factory \\a sensor with the global list of factories.\n+ * The camera sensor factory supports image sensors identified by \\a entityName.\n+ * The sensor name reported by the media entity that identifies it is usually\n+ * composed by the here provided \\a entityName and the I2C bus and device ids.\n+ */\n+\n } /* namespace libcamera */\ndiff --git a/src/libcamera/include/camera_sensor.h b/src/libcamera/include/camera_sensor.h\nindex 99cff98128dc..633955591b36 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, entityName)\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(entityName) {}\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 387bb070b505..adef6c6703b6 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@@ -294,7 +293,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@@ -1322,7 +1321,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 13433b216747..3e3b22018430 100644\n--- a/src/libcamera/pipeline/rkisp1/rkisp1.cpp\n+++ b/src/libcamera/pipeline/rkisp1/rkisp1.cpp\n@@ -114,20 +114,15 @@ class RkISP1CameraData : public CameraData\n {\n public:\n \tRkISP1CameraData(PipelineHandler *pipe)\n-\t\t: CameraData(pipe), sensor_(nullptr), frame_(0),\n+\t\t: CameraData(pipe), frame_(0),\n \t\t  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@@ -389,7 +384,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@@ -444,7 +439,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@@ -557,7 +552,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@@ -907,7 +902,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 529909714bf5..6f725c6dda98 100644\n--- a/src/libcamera/pipeline/vimc.cpp\n+++ b/src/libcamera/pipeline/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/drm_fourcc.h>\n@@ -47,7 +48,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@@ -57,7 +57,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@@ -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..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 577da4cb601c..ae038de74864 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",
        "v3",
        "3/6"
    ]
}