[{"id":17668,"web_url":"https://patchwork.libcamera.org/comment/17668/","msgid":"<20210621173715.kqsj4n7s6wpgppec@uno.localdomain>","date":"2021-06-21T17:37:15","subject":"Re: [libcamera-devel] [RFC PATCH 1/4] libcamera: camera_sensor:\n\tEnable to set a test pattern mode","submitter":{"id":3,"url":"https://patchwork.libcamera.org/api/people/3/","name":"Jacopo Mondi","email":"jacopo@jmondi.org"},"content":"Hi Hiro,\n\nOn Fri, Jun 11, 2021 at 05:42:25PM +0900, Hirokazu Honda wrote:\n> Provides a function to set the camera sensor a test pattern mode.\n>\n> Signed-off-by: Hirokazu Honda <hiroh@chromium.org>\n> ---\n>  include/libcamera/internal/camera_sensor.h |  7 ++--\n>  src/libcamera/camera_sensor.cpp            | 40 ++++++++++++++++++++--\n>  2 files changed, 41 insertions(+), 6 deletions(-)\n>\n> diff --git a/include/libcamera/internal/camera_sensor.h b/include/libcamera/internal/camera_sensor.h\n> index e133ebf4..834d3246 100644\n> --- a/include/libcamera/internal/camera_sensor.h\n> +++ b/include/libcamera/internal/camera_sensor.h\n> @@ -39,10 +39,8 @@ public:\n>  \tconst std::vector<unsigned int> &mbusCodes() const { return mbusCodes_; }\n>  \tconst std::vector<Size> &sizes() const { return sizes_; }\n>  \tSize resolution() const;\n> -\tconst std::vector<int32_t> &testPatternModes() const\n> -\t{\n> -\t\treturn testPatternModes_;\n> -\t}\n> +\tstd::vector<int32_t> testPatternModes() const;\n> +\tint setTestPatternMode(uint8_t testPatternMode);\n>\n>  \tV4L2SubdeviceFormat getFormat(const std::vector<unsigned int> &mbusCodes,\n>  \t\t\t\t      const Size &size) const;\n> @@ -84,6 +82,7 @@ private:\n>  \tstd::vector<unsigned int> mbusCodes_;\n>  \tstd::vector<Size> sizes_;\n>  \tstd::vector<int32_t> testPatternModes_;\n\nAre you replacing testPatternModes_ or are you adding a new map\nalongside with it ? Looking at the code I would say your intention is\nto replace it.\n\nOverall, what is the usage for the index-testPattern association ?\n1) Enumeration:\n  - list indexes through the V4L2 API\n  - Use the index to retrieve the control::TestPatternMode from the\n    sensor properties\n  - Fill the map\n  - Expose the collected control::TestPatternMode to pipeline handlers\n\n2) Set a test patter:\n  - Receive a control::TestPatternMode from the pipeline handler\n  - Set the corresponding index on the V4L2 device\n\nI won't add a map here, we already have one in the camera sensor\nproperties and I would not duplicate it, it's all static information\nafter all.\n\nI would rather reverse it and move it from\n        map<v4l2-index, control::TestPatternMode>\nto\n        map<control::TestPatternMode, v4l2-index>\n\nat enumeration time we'll have to pay a little price to search which\nkey (== control::TestPatternMode) corresponds to the index we got from\nthe V4L2, but that only happens once at initialization in a very small\nmap.\n\nThe at runtime when we have to set the test pattern mode we simply\naccess the map and get the index to set on the v4l2 subdev.\n\nSo I would:\n- Reverse the map in camera sensor properties\n  map<control::TestPatternMode, v4l2-index>\n- Initialize the CameraSensor::testPatternModes_ vector by reverse\n  inspecting the map\n- Set the test pattern mode by retrieving from the camera sensor\n  properties map which v4l2-index a control::TestPatternMode\n  corresponds to\n\nTo be honest, if we're diligent and we trust the camera sensor\nproperties, we could skip the enumeration completely, and return the\nlist of supported modes by just\nutils::map_keys(props->testPatternModes) if you agree to reverse the\nmap.\n\nThanks\n   j\n\n> +\tstd::map<int32_t, int32_t> testPatternModeToIndex_;\n>\n>  \tSize pixelArraySize_;\n>  \tRectangle activeArea_;\n> diff --git a/src/libcamera/camera_sensor.cpp b/src/libcamera/camera_sensor.cpp\n> index 3e135353..d2f033a5 100644\n> --- a/src/libcamera/camera_sensor.cpp\n> +++ b/src/libcamera/camera_sensor.cpp\n> @@ -329,7 +329,7 @@ void CameraSensor::initTestPatternModes(\n>  \t\t\tcontinue;\n>  \t\t}\n>\n> -\t\ttestPatternModes_.push_back(it->second);\n> +\t\ttestPatternModeToIndex_[it->second] = index;\n>  \t}\n>  }\n>\n> @@ -496,12 +496,48 @@ Size CameraSensor::resolution() const\n>  }\n>\n>  /**\n> - * \\fn CameraSensor::testPatternModes()\n>   * \\brief Retrieve all the supported test pattern modes of the camera sensor\n>   * The test pattern mode values correspond to the controls::TestPattern control.\n>   *\n>   * \\return The list of test pattern modes\n>   */\n> +std::vector<int32_t> CameraSensor::testPatternModes() const\n> +{\n> +\treturn utils::map_keys(testPatternModeToIndex_);\n> +}\n> +\n> +/**\n> + * \\brief Set the camera sensor a specified controls::TestPatternMode\n> + *\n> + * \\return 0 on success or a negative error code otherwise\n> + */\n> +int CameraSensor::setTestPatternMode(uint8_t testPatternMode)\n> +{\n> +\tauto it = testPatternModeToIndex_.find(testPatternMode);\n> +\tif (it == testPatternModeToIndex_.end()) {\n> +\t\tLOG(CameraSensor, Error) << \"Unsupported test pattern mode: \"\n> +\t\t\t\t\t << testPatternMode;\n> +\t\treturn -EINVAL;\n> +\t}\n> +\n> +\tconst uint8_t index = it->second;\n> +\n> +\tControlList ctrls = getControls({ V4L2_CID_TEST_PATTERN });\n> +\tif (ctrls.empty()) {\n> +\t\tLOG(CameraSensor, Error)\n> +\t\t\t<< \"Failed to retrieve test pattern control\";\n> +\t\treturn -EINVAL;\n> +\t}\n> +\n> +\tControlValue value = ctrls.get(V4L2_CID_TEST_PATTERN);\n> +\tif (value.get<int32_t>() == index)\n> +\t\treturn 0;\n> +\n> +\tvalue.set<int32_t>(index);\n> +\tctrls.set(V4L2_CID_TEST_PATTERN, value);\n> +\n> +\treturn setControls(&ctrls);\n> +}\n>\n>  /**\n>   * \\brief Retrieve the best sensor format for a desired output\n> --\n> 2.32.0.272.g935e593368-goog\n>","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 865BFC3218\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 21 Jun 2021 17:36:30 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id BDDD668935;\n\tMon, 21 Jun 2021 19:36:29 +0200 (CEST)","from relay1-d.mail.gandi.net (relay1-d.mail.gandi.net\n\t[217.70.183.193])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id D7C8360295\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 21 Jun 2021 19:36:27 +0200 (CEST)","(Authenticated sender: jacopo@jmondi.org)\n\tby relay1-d.mail.gandi.net (Postfix) with ESMTPSA id 1911A240002;\n\tMon, 21 Jun 2021 17:36:26 +0000 (UTC)"],"Date":"Mon, 21 Jun 2021 19:37:15 +0200","From":"Jacopo Mondi <jacopo@jmondi.org>","To":"Hirokazu Honda <hiroh@chromium.org>","Message-ID":"<20210621173715.kqsj4n7s6wpgppec@uno.localdomain>","References":"<20210611084228.1830629-1-hiroh@chromium.org>\n\t<20210611084228.1830629-2-hiroh@chromium.org>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20210611084228.1830629-2-hiroh@chromium.org>","Subject":"Re: [libcamera-devel] [RFC PATCH 1/4] libcamera: camera_sensor:\n\tEnable to set a test pattern mode","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":"libcamera-devel@lists.libcamera.org","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]