From patchwork Sat Oct 12 18:44:00 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 2168 Return-Path: Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 0E54161978 for ; Sat, 12 Oct 2019 20:44:21 +0200 (CEST) Received: from pendragon.bb.dnainternet.fi (dfj612yhrgyx302h3jwwy-3.rev.dnainternet.fi [IPv6:2001:14ba:21f5:5b00:ce28:277f:58d7:3ca4]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 9728B9C0 for ; Sat, 12 Oct 2019 20:44:20 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1570905860; bh=d0p6DvIdtTbSRMT48oSbkp/Qaj5k5oL64j2QfRmA5RQ=; h=From:To:Subject:Date:In-Reply-To:References:From; b=cAD3EgUjHiE4hUWB7OfDyKNG65EyX0ZQ4D5/JL2SyQF7b58+mNT90qNmfojvM6vQb gDCZzhhTUt1ose6X5beR9vGDMMotkHHH4L5bcH/CFv+F2hmCvDuXb6c9fFEERjq25P 9h+sQae2T20/9MARK03XTbWdiH0kSox2hVmix0ZA= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Sat, 12 Oct 2019 21:44:00 +0300 Message-Id: <20191012184407.31684-8-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20191012184407.31684-1-laurent.pinchart@ideasonboard.com> References: <20191012184407.31684-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 07/14] libcamera: controls: Support accessing controls by numerical ID X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 12 Oct 2019 18:44:21 -0000 The ControlList class has template get() and set() methods to get and set control values. The methods require a reference to a Control instance, which is only available when calling them with a hardcoded control. In order to support usage of ControlList for V4L2 controls, as well as serialisation and deserialisation of ControlList, we need a way to get and set control values based on a control numerical ID. Add new contains(), get() and set() overload methods to do so. As this change prepares the ControlList to be used for other objects than camera, update its documentation accordingly. Signed-off-by: Laurent Pinchart Reviewed-by: Jacopo Mondi Reviewed-by: Niklas Söderlund --- include/libcamera/controls.h | 10 ++- src/ipa/rkisp1/rkisp1.cpp | 2 +- src/libcamera/controls.cpp | 143 ++++++++++++++++++++++++++------- src/libcamera/request.cpp | 5 +- test/controls/control_list.cpp | 2 +- 5 files changed, 125 insertions(+), 37 deletions(-) diff --git a/include/libcamera/controls.h b/include/libcamera/controls.h index 999fcf7a3a62..5e6708fe570b 100644 --- a/include/libcamera/controls.h +++ b/include/libcamera/controls.h @@ -126,7 +126,7 @@ private: using ControlListMap = std::unordered_map; public: - ControlList(ControlValidator *validator = nullptr); + ControlList(const ControlIdMap &idmap, ControlValidator *validator = nullptr); using iterator = ControlListMap::iterator; using const_iterator = ControlListMap::const_iterator; @@ -136,11 +136,13 @@ public: const_iterator begin() const { return controls_.begin(); } const_iterator end() const { return controls_.end(); } - bool contains(const ControlId &id) const; bool empty() const { return controls_.empty(); } std::size_t size() const { return controls_.size(); } void clear() { controls_.clear(); } + bool contains(const ControlId &id) const; + bool contains(unsigned int id) const; + template const T &get(const Control &ctrl) const { @@ -163,11 +165,15 @@ public: val->set(value); } + const ControlValue &get(unsigned int id) const; + void set(unsigned int id, const ControlValue &value); + private: const ControlValue *find(const ControlId &id) const; ControlValue *find(const ControlId &id); ControlValidator *validator_; + const ControlIdMap *idmap_; ControlListMap controls_; }; diff --git a/src/ipa/rkisp1/rkisp1.cpp b/src/ipa/rkisp1/rkisp1.cpp index 80138f196184..b0d23dd154be 100644 --- a/src/ipa/rkisp1/rkisp1.cpp +++ b/src/ipa/rkisp1/rkisp1.cpp @@ -220,7 +220,7 @@ void IPARkISP1::setControls(unsigned int frame) void IPARkISP1::metadataReady(unsigned int frame, unsigned int aeState) { - ControlList ctrls; + ControlList ctrls(controls::controls); if (aeState) ctrls.set(controls::AeLocked, aeState == 2); diff --git a/src/libcamera/controls.cpp b/src/libcamera/controls.cpp index 292e48cd6d25..ddd4e6680ce2 100644 --- a/src/libcamera/controls.cpp +++ b/src/libcamera/controls.cpp @@ -7,6 +7,7 @@ #include +#include #include #include @@ -16,13 +17,13 @@ /** * \file controls.h - * \brief Describes control framework and controls supported by a camera + * \brief Framework to handle manage controls related to an object * - * A control is a mean to govern or influence the operation of a camera. Every - * control is defined by a unique numerical ID, a name string and the data type - * of the value it stores. The libcamera API defines a set of standard controls - * in the libcamera::controls namespace, as a set of instances of the Control - * class. + * A control is a mean to govern or influence the operation of an object, and in + * particular of a camera. Every control is defined by a unique numerical ID, a + * name string and the data type of the value it stores. The libcamera API + * defines a set of standard controls in the libcamera::controls namespace, as + * a set of instances of the Control class. * * The main way for applications to interact with controls is through the * ControlList stored in the Request class: @@ -274,7 +275,7 @@ bool ControlValue::operator==(const ControlValue &other) const * \class Control * \brief Describe a control and its intrinsic properties * - * The Control class models a control exposed by a camera. Its template type + * The Control class models a control exposed by an object. Its template type * name T refers to the control data type, and allows methods that operate on * control values to be defined as template methods using the same type T for * the control value. See for instance how the ControlList::get() method @@ -293,8 +294,8 @@ bool ControlValue::operator==(const ControlValue &other) const * long int). * * Controls IDs shall be unique. While nothing prevents multiple instances of - * the Control class to be created with the same ID, this may lead to undefined - * behaviour. + * the Control class to be created with the same ID for the same object, doing + * so may cause undefined behaviour. */ /** @@ -398,18 +399,28 @@ std::string ControlRange::toString() const /** * \class ControlList - * \brief Associate a list of ControlId with their values for a camera + * \brief Associate a list of ControlId with their values for an object * - * A ControlList wraps a map of ControlId to ControlValue and optionally - * validates controls against a ControlValidator. + * The ControlList class stores values of controls exposed by an object. The + * lists returned by the Request::controls() and Request::metadata() methods + * refer to the camera that the request belongs to. + * + * Control lists are constructed with a map of all the controls supported by + * their object, and an optional ControlValidator to further validate the + * controls. */ /** * \brief Construct a ControlList with an optional control validator + * \param[in] idmap The ControlId map for the control list target object * \param[in] validator The validator (may be null) + * + * For ControlList containing libcamera controls, a global map of all libcamera + * controls is provided by controls::controls and can be used as the \a idmap + * argument. */ -ControlList::ControlList(ControlValidator *validator) - : validator_(validator) +ControlList::ControlList(const ControlIdMap &idmap, ControlValidator *validator) + : validator_(validator), idmap_(&idmap) { } @@ -449,20 +460,6 @@ ControlList::ControlList(ControlValidator *validator) * list */ -/** - * \brief Check if the list contains a control with the specified \a id - * \param[in] id The control ID - * - * The behaviour is undefined if the control \a id is not supported by the - * camera that the ControlList refers to. - * - * \return True if the list contains a matching control, false otherwise - */ -bool ControlList::contains(const ControlId &id) const -{ - return controls_.find(&id) != controls_.end(); -} - /** * \fn ControlList::empty() * \brief Identify if the list is empty @@ -481,7 +478,33 @@ bool ControlList::contains(const ControlId &id) const */ /** - * \fn template const T &ControlList::get() const + * \brief Check if the list contains a control with the specified \a id + * \param[in] id The control ID + * + * \return True if the list contains a matching control, false otherwise + */ +bool ControlList::contains(const ControlId &id) const +{ + return controls_.find(&id) != controls_.end(); +} + +/** + * \brief Check if the list contains a control with the specified \a id + * \param[in] id The control numerical ID + * + * \return True if the list contains a matching control, false otherwise + */ +bool ControlList::contains(unsigned int id) const +{ + const auto iter = idmap_->find(id); + if (iter == idmap_->end()) + return false; + + return contains(*iter->second); +} + +/** + * \fn template const T &ControlList::get(const Control &ctrl) const * \brief Get the value of a control * \param[in] ctrl The control * @@ -496,7 +519,7 @@ bool ControlList::contains(const ControlId &id) const */ /** - * \fn template void ControlList::set() + * \fn template void ControlList::set(const Control &ctrl, const T &value) * \brief Set the control value to \a value * \param[in] ctrl The control * \param[in] value The control value @@ -506,9 +529,67 @@ bool ControlList::contains(const ControlId &id) const * to the list. * * The behaviour is undefined if the control \a ctrl is not supported by the - * camera that the list refers to. + * object that the list refers to. */ +/** + * \brief Get the value of control \a id + * \param[in] id The control numerical ID + * + * The behaviour is undefined if the control \a id is not present in the list. + * Use ControlList::contains() to test for the presence of a control in the + * list before retrieving its value. + * + * \return The control value + */ +const ControlValue &ControlList::get(unsigned int id) const +{ + static ControlValue zero; + + const auto ctrl = idmap_->find(id); + if (ctrl == idmap_->end()) { + LOG(Controls, Error) + << std::hex << std::setfill('0') + << "Control 0x" << std::setw(8) << id << " is not valid"; + return zero; + } + + const ControlValue *val = find(*ctrl->second); + if (!val) + return zero; + + return *val; +} + +/** + * \brief Set the value of control \a id to \a value + * \param[in] id The control ID + * \param[in] value The control value + * + * This method sets the value of a control in the control list. If the control + * is already present in the list, its value is updated, otherwise it is added + * to the list. + * + * The behaviour is undefined if the control \a id is not supported by the + * object that the list refers to. + */ +void ControlList::set(unsigned int id, const ControlValue &value) +{ + const auto ctrl = idmap_->find(id); + if (ctrl == idmap_->end()) { + LOG(Controls, Error) + << std::hex << std::setfill('0') + << "Control 0x" << std::setw(8) << id << " is not valid"; + return; + } + + ControlValue *val = find(*ctrl->second); + if (!val) + return; + + *val = value; +} + const ControlValue *ControlList::find(const ControlId &id) const { const auto iter = controls_.find(&id); diff --git a/src/libcamera/request.cpp b/src/libcamera/request.cpp index e800f1449888..c14ed1a4d3ce 100644 --- a/src/libcamera/request.cpp +++ b/src/libcamera/request.cpp @@ -11,6 +11,7 @@ #include #include +#include #include #include "camera_controls.h" @@ -64,12 +65,12 @@ Request::Request(Camera *camera, uint64_t cookie) * creating a new instance for each request? */ validator_ = new CameraControlValidator(camera); - controls_ = new ControlList(validator_); + controls_ = new ControlList(controls::controls, validator_); /** * \todo: Add a validator for metadata controls. */ - metadata_ = new ControlList(); + metadata_ = new ControlList(controls::controls); } Request::~Request() diff --git a/test/controls/control_list.cpp b/test/controls/control_list.cpp index 1bcfecc467b5..5af53f64bb6c 100644 --- a/test/controls/control_list.cpp +++ b/test/controls/control_list.cpp @@ -42,7 +42,7 @@ protected: int run() { CameraControlValidator validator(camera_.get()); - ControlList list(&validator); + ControlList list(controls::controls, &validator); /* Test that the list is initially empty. */ if (!list.empty()) {