[{"id":2007,"web_url":"https://patchwork.libcamera.org/comment/2007/","msgid":"<20190624155539.GM5737@pendragon.ideasonboard.com>","date":"2019-06-24T15:55:39","subject":"Re: [libcamera-devel] [PATCH v6 1/6] libcamera: Add V4L2Controls","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Jacopo,\n\nThank you for the patch.\n\nOn Mon, Jun 24, 2019 at 04:28:54PM +0200, Jacopo Mondi wrote:\n> Add libcamera V4L2 control support, implemented using the V4L2 Extended\n> Control APIs. This patch defines the types used to create and manage\n> controls.\n> \n> Signed-off-by: Jacopo Mondi <jacopo@jmondi.org>\n> ---\n>  src/libcamera/include/v4l2_controls.h |  79 ++++++++\n>  src/libcamera/meson.build             |   1 +\n>  src/libcamera/v4l2_controls.cpp       | 281 ++++++++++++++++++++++++++\n>  3 files changed, 361 insertions(+)\n>  create mode 100644 src/libcamera/include/v4l2_controls.h\n>  create mode 100644 src/libcamera/v4l2_controls.cpp\n> \n> diff --git a/src/libcamera/include/v4l2_controls.h b/src/libcamera/include/v4l2_controls.h\n> new file mode 100644\n> index 000000000000..aad4ef14ed31\n> --- /dev/null\n> +++ b/src/libcamera/include/v4l2_controls.h\n> @@ -0,0 +1,79 @@\n> +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> +/*\n> + * Copyright (C) 2019, Google Inc.\n> + *\n> + * v4l2_controls.h - V4L2 Controls Support\n> + */\n> +\n> +#ifndef __LIBCAMERA_V4L2_CONTROLS_H__\n> +#define __LIBCAMERA_V4L2_CONTROLS_H__\n> +\n> +#include <string>\n> +#include <vector>\n> +\n> +#include <stdint.h>\n> +\n> +#include <linux/v4l2-controls.h>\n> +#include <linux/videodev2.h>\n> +\n> +namespace libcamera {\n> +\n> +class V4L2ControlInfo\n> +{\n> +public:\n> +\tV4L2ControlInfo(const struct v4l2_query_ext_ctrl &ctrl);\n> +\n> +\tunsigned int id() const { return id_; }\n> +\tunsigned int type() const { return type_; }\n> +\tsize_t size() const { return size_; }\n> +\tconst std::string &name() const { return name_; }\n> +\n> +private:\n> +\tunsigned int id_;\n> +\tunsigned int type_;\n> +\tsize_t size_;\n> +\tstd::string name_;\n> +};\n> +\n> +class V4L2Control\n> +{\n> +public:\n> +\tV4L2Control(unsigned int id, int value = 0)\n> +\t\t: id_(id), value_(value) {}\n> +\n> +\tint64_t value() const { return value_; }\n> +\tvoid setValue(int64_t value) { value_ = value; }\n> +\n> +\tunsigned int id() const { return id_; }\n> +\n> +private:\n> +\tunsigned int id_;\n> +\tint64_t value_;\n> +};\n> +\n> +class V4L2ControlList\n> +{\n> +public:\n> +\tusing iterator = std::vector<V4L2Control>::iterator;\n> +\tusing const_iterator = std::vector<V4L2Control>::const_iterator;\n> +\n> +\titerator begin() { return controls_.begin(); }\n> +\tconst_iterator begin() const { return controls_.begin(); }\n> +\titerator end() { return controls_.end(); }\n> +\tconst_iterator end() const { return controls_.end(); }\n> +\n> +\tbool empty() const { return controls_.empty(); }\n> +\tstd::size_t size() const { return controls_.size(); }\n> +\n> +\tvoid clear() { controls_.clear(); }\n> +\tint add(unsigned int id, int64_t value = 0);\n> +\n> +\tV4L2Control *getControl(unsigned int id);\n> +\n> +private:\n> +\tstd::vector<V4L2Control> controls_;\n> +};\n> +\n> +} /* namespace libcamera */\n> +\n> +#endif /* __LIBCAMERA_V4L2_CONTROLS_H__ */\n> diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build\n> index f26ad5b2dc57..985aa7e8ab0e 100644\n> --- a/src/libcamera/meson.build\n> +++ b/src/libcamera/meson.build\n> @@ -23,6 +23,7 @@ libcamera_sources = files([\n>      'stream.cpp',\n>      'timer.cpp',\n>      'utils.cpp',\n> +    'v4l2_controls.cpp',\n>      'v4l2_device.cpp',\n>      'v4l2_subdevice.cpp',\n>      'v4l2_videodevice.cpp',\n> diff --git a/src/libcamera/v4l2_controls.cpp b/src/libcamera/v4l2_controls.cpp\n> new file mode 100644\n> index 000000000000..ac932d855dd6\n> --- /dev/null\n> +++ b/src/libcamera/v4l2_controls.cpp\n> @@ -0,0 +1,281 @@\n> +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> +/*\n> + * Copyright (C) 2019, Google Inc.\n> + *\n> + * v4l2_controls.cpp - V4L2 Controls Support\n> + */\n> +\n> +#include \"v4l2_controls.h\"\n> +\n> +/**\n> + * \\file v4l2_controls.h\n> + * \\brief Support for V4L2 Controls using the V4L2 Extended Controls APIs\n> + *\n> + * The V4L2 Control API allows application to inspect and modify sets of\n> + * configurable parameters on the video device or subdevice of interest. The\n> + * nature of the parameters an application could modify using the control\n> + * framework depends on what the driver implements support for, and on the\n> + * characteristics of the underlying hardware platform. Generally controls are\n> + * used to modify user visible settings, such as the image brightness and\n> + * exposure time, or non-standard parameters which cannot be controlled through\n> + * the V4L2 format negotiation API.\n> + *\n> + * Controls are identified by a numerical ID, defined by the V4L2 kernel headers\n> + * and have an associated type. Each control has a value, which is the data that\n> + * can be modified with V4L2Device::setControls() or retrieved with\n> + * V4L2Device::getControls().\n> + *\n> + * The control's type along with the control's flags define the type of the\n> + * control's value content. Controls can transport a single data value stored in\n> + * variable inside the control, or they might as well deal with more complex\n> + * data types, such as arrays of matrices, stored in a contiguous memory\n> + * locations associated with the control and called 'the payload'. Such controls\n> + * are called 'compound controls' and are currently not supported by the\n> + * libcamera V4L2 control framework.\n> + *\n> + * libcamera implements support for controls using the V4L2 Extended Control\n> + * API, which allows future handling of controls with payloads of arbitrary\n> + * sizes.\n> + *\n> + * The libcamera V4L2 Controls framework operates on lists of controls, wrapped\n> + * by the V4L2ControlList class, to match the V4L2 extended controls API. The\n> + * interface to set and get control is implemented by the V4L2Device class, and\n> + * this file only provides the data type definitions.\n> + *\n> + * \\todo Add support for compound controls\n> + */\n> +\n> +namespace libcamera {\n> +\n> +/**\n> + * \\class V4L2ControlInfo\n> + * \\brief Information on a V4L2 control\n> + *\n> + * The V4L2ControlInfo class represents all the information related to a V4L2\n> + * control, such as its ID, its type, its user-readable name and the expected\n> + * size of its value data.\n> + *\n> + * V4L2ControlInfo instances are created by inspecting the fieldS of a struct\n> + * v4l2_query_ext_ctrl structure, after it has been filled by the device driver\n> + * as a consequence of a VIDIOC_QUERY_EXT_CTRL ioctl call.\n> + *\n> + * This class does not contain the control value, but only static information on\n> + * the control, which shall be cached by the caller at initialisation time or\n> + * the first time the control information is accessed.\n> + */\n> +\n> +/**\n> + * \\brief Construct a V4L2ControlInfo from a struct v4l2_query_ext_ctrl\n> + * \\param ctrl The struct v4l2_query_ext_ctrl as returned by the kernel\n> + */\n> +V4L2ControlInfo::V4L2ControlInfo(const struct v4l2_query_ext_ctrl &ctrl)\n> +{\n> +\tid_ = ctrl.id;\n> +\ttype_ = ctrl.type;\n> +\tname_ = static_cast<const char *>(ctrl.name);\n> +\tsize_ = ctrl.elem_size * ctrl.elems;\n> +}\n> +\n> +/**\n> + * \\fn V4L2ControlInfo::id()\n> + * \\brief Retrieve the control ID\n> + * \\return The V4L2 control ID\n> + */\n> +\n> +/**\n> + * \\fn V4L2ControlInfo::type()\n> + * \\brief Retrieve the control type as defined by V4L2_CTRL_TYPE_*\n> + * \\return The V4L2 control type\n> + */\n> +\n> +/**\n> + * \\fn V4L2ControlInfo::size()\n> + * \\brief Retrieve the control value data size (in bytes)\n> + * \\return The V4L2 control value data size\n> + */\n> +\n> +/**\n> + * \\fn V4L2ControlInfo::name()\n> + * \\brief Retrieve the control user readable name\n> + * \\return The V4L2 control user readable name\n> + */\n> +\n> +/**\n> + * \\class V4L2Control\n> + * \\brief A V4L2 control value\n> + *\n> + * The V4L2Control class represent the value of a V4L2 control. The class\n> + * stores values that have been read from or will be applied to a V4L2 device.\n> + *\n> + * The value stored in the class instances does not reflect what is actually\n> + * applied to the hardware but is a pure software cache optionally initialized\n> + * at control creation time and modified by a control read or write operation.\n> + *\n> + * The write and read controls the V4L2Control class instances are not meant\n> + * to be directly used but are instead intended to be grouped in\n> + * V4L2ControlList instances, which are then passed as parameters to\n> + * V4L2Device::setControls() and V4L2Device::getControls() operations.\n> + */\n> +\n> +/**\n> + * \\fn V4L2Control::V4L2Control\n> + * \\brief Construct a V4L2 control with \\a id and \\a value\n> + * \\param id The V4L2 control ID\n> + * \\param value The control value\n> + */\n> +\n> +/**\n> + * \\fn V4L2Control::value()\n> + * \\brief Retrieve the value of the control\n> + *\n> + * This method returns the cached control value, initially set by\n> + * V4L2ControlList::add() and then updated when the controls are read or\n> + * written with V4L2Device::getControls() and V4L2Device::setControls().\n> + *\n> + * \\return The V4L2 control value\n> + */\n> +\n> +/**\n> + * \\fn V4L2Control::setValue()\n> + * \\brief Set the value of the control\n> + * \\param value The new V4L2 control value\n> + *\n> + * This method stores the control value, which will be applied to the\n> + * device when calling V4L2Device::setControls().\n> + */\n> +\n> +/**\n> + * \\fn V4L2Control::id()\n> + * \\brief Retrieve the control ID this instance refers to\n> + * \\return The V4L2Control ID\n> + */\n> +\n> +/**\n> + * \\class V4L2ControlList\n> + * \\brief Container of V4L2Control instances\n> + *\n> + * The V4L2ControlList class works as a container for a list of V4L2Control\n> + * instances. The class provides operations to add a new control to the list,\n> + * get back a control value, and reset the list of controls it contains.\n> + *\n> + * In order to set and get controls, user of the libcamera V4L2 control\n> + * framework should operate on instances of the V4L2ControlList class, and use\n> + * them as argument for the V4L2Device::setControls() and\n> + * V4L2Device::getControls() operations, which write and read a list of\n> + * controls to or from a V4L2 device (a video device or a subdevice).\n> + *\n> + * Controls are added to a V4L2ControlList instance with the add() method, with\n> + * or without a value.\n> + *\n> + * To write controls to a device, the controls of interest shall be added with\n> + * an initial value by calling V4L2ControlList::add(unsigned int id, int64_t\n> + * value) to prepare for a write operation. Once the values of all controls of\n> + * interest have been added, the V4L2ControlList instance is passed to the\n> + * V4L2Device::setControls(), which sets the controls on the device.\n> + *\n> + * To read controls from a device, the desired controls are added with\n> + * V4L2ControlList::add(unsigned int id) to prepare for a read operation. The\n> + * V4L2ControlList instance is then passed to V4L2Device::getControls(), which\n> + * reads the controls from the device and updates the values stored in\n> + * V4L2ControlList.\n> + *\n> + * V4L2ControlList instances can be reset to remove all controls they contain\n> + * and prepare to be re-used for a new control write/read sequence.\n> + */\n> +\n> +/**\n> + * \\typedef V4L2ControlList::iterator\n> + * \\brief Iterator on the V4L2 controls contained in the instance\n> + */\n> +\n> +/**\n> + * \\typedef V4L2ControlList::const_iterator\n> + * \\brief Const iterator on the V4L2 controls contained in the instance\n> + */\n> +\n> +/**\n> + * \\fn iterator V4L2ControlList::begin()\n> + * \\brief Retrieve an iterator to the first V4L2Control in the instance\n> + * \\return An iterator to the first V4L2 control\n> + */\n> +\n> +/**\n> + * \\fn const_iterator V4L2ControlList::begin() const\n> + * \\brief Retrieve a constant iterator to the first V4L2Control in the instance\n> + * \\return A constant iterator to the first V4L2 control\n> + */\n> +\n> +/**\n> + * \\fn iterator V4L2ControlList::end()\n> + * \\brief Retrieve an iterator pointing to the past-the-end V4L2Control in the\n> + * instance\n> + * \\return An iterator to the element following the last V4L2 control in the\n> + * instance\n> + */\n> +\n> +/**\n> + * \\fn const_iterator V4L2ControlList::end() const\n> + * \\brief Retrieve a constant iterator pointing to the past-the-end V4L2Control\n> + * in the instance\n> + * \\return A constant iterator to the element following the last V4L2 control\n> + * in the instance\n> + */\n> +\n> +/**\n> + * \\fn V4L2ControlList::empty()\n> + * \\brief Verify if the instance does not contain any control\n> + * \\return True if the instance does not contain any control, false otherwise\n> + */\n> +\n> +/**\n> + * \\fn V4L2ControlList::size()\n> + * \\brief Retrieve the number on controls in the instance\n> + * \\return The number of V4L2Control stored in the instance\n> + */\n> +\n> +/**\n> + * \\fn V4L2ControlList::clear()\n> + * \\brief Remove all controls in the instance\n> + */\n> +\n> +/**\n> + * \\brief Add control with \\a id and optional \\a value to the instance\n> + * \\param id The V4L2 control ID (V4L2_CID_*)\n> + * \\param value The V4L2 control value\n> + *\n> + * This methods add a new V4L2 control to the V4L2ControlList. The newly\n\ns/methods add/method adds/\n\n> + * inserted control should be unique (no other control with \\a id should be\n> + * part of the V4L2 control list) otherwise a negative error code is returned.\n> + *\n> + * \\return 0 on success or a negative error code otherwise\n> + * \\retval -EBUSY A control with \\a id already exists\n> + */\n> +int V4L2ControlList::add(unsigned int id, int64_t value)\n> +{\n> +\tfor (const V4L2Control &ctrl : controls_) {\n> +\t\tif (ctrl.id() == id)\n> +\t\t\treturn -EBUSY;\n> +\t}\n\nThis makes adding controls a O(n^2) operation :-/ What it be better to\njust say that adding two controls with the same ID leads to undefined\nresults ?\n\n> +\n> +\tcontrols_.emplace_back(id, value);\n> +\n> +\treturn 0;\n> +}\n> +\n> +/**\n> + * \\brief Get a pointer the control with \\a id\n> + * \\param id The V4L2 control ID (V4L2_CID_xx)\n> + * \\return A pointer to the V4L2Control with \\a id or nullptr if the\n> + * control ID is not part of this instance.\n> + */\n> +V4L2Control *V4L2ControlList::getControl(unsigned int id)\n> +{\n> +\tfor (V4L2Control &ctrl : controls_) {\n> +\t\tif (ctrl.id() == id)\n> +\t\t\treturn &ctrl;\n> +\t}\n> +\n> +\treturn nullptr;\n> +}\n> +\n> +} /* namespace libcamera */","headers":{"Return-Path":"<laurent.pinchart@ideasonboard.com>","Received":["from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id A14F761580\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 24 Jun 2019 17:58:10 +0200 (CEST)","from pendragon.ideasonboard.com (81-175-216-236.bb.dnainternet.fi\n\t[81.175.216.236])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 0EE982AF;\n\tMon, 24 Jun 2019 17:58:10 +0200 (CEST)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1561391890;\n\tbh=cGO1rKwm+kx08S6rJgOVMdRtXwnHM76qlW1UtokP19Q=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=InMVeEz1V/WTqHQKsvHbNj3S9q18TZlM7MHb/D9YxHdPhDvSPdYuwQeYKE/EIFR+w\n\tfIcWN821MEnt9a6Mc8WSCo1X8VKOQDw5h4MVQ3nyTOsIzcy2Jymeg5JwA97eXD5FQN\n\tQmLkxn3i54Bb97C/ax91+JmT2r63ZaxG3d7AI7s0=","Date":"Mon, 24 Jun 2019 18:55:39 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Jacopo Mondi <jacopo@jmondi.org>","Cc":"libcamera-devel@lists.libcamera.org","Message-ID":"<20190624155539.GM5737@pendragon.ideasonboard.com>","References":"<20190624142859.19313-1-jacopo@jmondi.org>\n\t<20190624142859.19313-2-jacopo@jmondi.org>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20190624142859.19313-2-jacopo@jmondi.org>","User-Agent":"Mutt/1.10.1 (2018-07-13)","Subject":"Re: [libcamera-devel] [PATCH v6 1/6] libcamera: Add V4L2Controls","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.23","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, 24 Jun 2019 15:58:10 -0000"}}]