libcamera: controls: Merge ControlInfoMap and V4L2ControlInfoMap

The ControlInfoMap and V4L2ControlInfoMap classes are very similar, with
the latter adding convenience accessors based on numerical IDs for the
former, as well as a cached idmap. Both features can be useful for
ControlInfoMap in the context of serialisation, and merging the two
classes will further simplify the IPA API.

Import all the features of V4L2ControlInfoMap into ControlInfoMap,
turning the latter into a real class. A few new constructors and
assignment operators are added for completeness.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>
This commit is contained in:
Laurent Pinchart 2019-10-14 02:08:44 +03:00
parent c957c8580a
commit 319d6ae8e3
16 changed files with 220 additions and 154 deletions

View file

@ -43,7 +43,7 @@ public:
virtual int init() = 0; virtual int init() = 0;
virtual void configure(const std::map<unsigned int, IPAStream> &streamConfig, virtual void configure(const std::map<unsigned int, IPAStream> &streamConfig,
const std::map<unsigned int, V4L2ControlInfoMap> &entityControls) = 0; const std::map<unsigned int, ControlInfoMap> &entityControls) = 0;
virtual void mapBuffers(const std::vector<IPABuffer> &buffers) = 0; virtual void mapBuffers(const std::vector<IPABuffer> &buffers) = 0;
virtual void unmapBuffers(const std::vector<unsigned int> &ids) = 0; virtual void unmapBuffers(const std::vector<unsigned int> &ids) = 0;

View file

@ -118,7 +118,50 @@ private:
}; };
using ControlIdMap = std::unordered_map<unsigned int, const ControlId *>; using ControlIdMap = std::unordered_map<unsigned int, const ControlId *>;
using ControlInfoMap = std::unordered_map<const ControlId *, ControlRange>;
class ControlInfoMap : private std::unordered_map<const ControlId *, ControlRange>
{
public:
using Map = std::unordered_map<const ControlId *, ControlRange>;
ControlInfoMap() = default;
ControlInfoMap(const ControlInfoMap &other) = default;
ControlInfoMap(std::initializer_list<Map::value_type> init);
ControlInfoMap &operator=(const ControlInfoMap &other) = default;
ControlInfoMap &operator=(std::initializer_list<Map::value_type> init);
ControlInfoMap &operator=(Map &&info);
using Map::key_type;
using Map::mapped_type;
using Map::value_type;
using Map::size_type;
using Map::iterator;
using Map::const_iterator;
using Map::begin;
using Map::cbegin;
using Map::end;
using Map::cend;
using Map::at;
using Map::empty;
using Map::size;
using Map::count;
using Map::find;
mapped_type &at(unsigned int key);
const mapped_type &at(unsigned int key) const;
size_type count(unsigned int key) const;
iterator find(unsigned int key);
const_iterator find(unsigned int key) const;
const ControlIdMap &idmap() const { return idmap_; }
private:
void generateIdmap();
ControlIdMap idmap_;
};
class ControlList class ControlList
{ {

View file

@ -31,7 +31,7 @@ public:
int init() override; int init() override;
void configure(const std::map<unsigned int, IPAStream> &streamConfig, void configure(const std::map<unsigned int, IPAStream> &streamConfig,
const std::map<unsigned int, V4L2ControlInfoMap> &entityControls) override {} const std::map<unsigned int, ControlInfoMap> &entityControls) override {}
void mapBuffers(const std::vector<IPABuffer> &buffers) override {} void mapBuffers(const std::vector<IPABuffer> &buffers) override {}
void unmapBuffers(const std::vector<unsigned int> &ids) override {} void unmapBuffers(const std::vector<unsigned int> &ids) override {}
void processEvent(const IPAOperationData &event) override {} void processEvent(const IPAOperationData &event) override {}

View file

@ -33,7 +33,7 @@ public:
int init() override { return 0; } int init() override { return 0; }
void configure(const std::map<unsigned int, IPAStream> &streamConfig, void configure(const std::map<unsigned int, IPAStream> &streamConfig,
const std::map<unsigned int, V4L2ControlInfoMap> &entityControls) override; const std::map<unsigned int, ControlInfoMap> &entityControls) override;
void mapBuffers(const std::vector<IPABuffer> &buffers) override; void mapBuffers(const std::vector<IPABuffer> &buffers) override;
void unmapBuffers(const std::vector<unsigned int> &ids) override; void unmapBuffers(const std::vector<unsigned int> &ids) override;
void processEvent(const IPAOperationData &event) override; void processEvent(const IPAOperationData &event) override;
@ -49,7 +49,7 @@ private:
std::map<unsigned int, BufferMemory> bufferInfo_; std::map<unsigned int, BufferMemory> bufferInfo_;
V4L2ControlInfoMap ctrls_; ControlInfoMap ctrls_;
/* Camera sensor controls. */ /* Camera sensor controls. */
bool autoExposure_; bool autoExposure_;
@ -62,7 +62,7 @@ private:
}; };
void IPARkISP1::configure(const std::map<unsigned int, IPAStream> &streamConfig, void IPARkISP1::configure(const std::map<unsigned int, IPAStream> &streamConfig,
const std::map<unsigned int, V4L2ControlInfoMap> &entityControls) const std::map<unsigned int, ControlInfoMap> &entityControls)
{ {
if (entityControls.empty()) if (entityControls.empty())
return; return;

View file

@ -251,7 +251,7 @@ int CameraSensor::setFormat(V4L2SubdeviceFormat *format)
* \brief Retrieve the supported V4L2 controls and their information * \brief Retrieve the supported V4L2 controls and their information
* \return A map of the V4L2 controls supported by the sensor * \return A map of the V4L2 controls supported by the sensor
*/ */
const V4L2ControlInfoMap &CameraSensor::controls() const const ControlInfoMap &CameraSensor::controls() const
{ {
return subdev_->controls(); return subdev_->controls();
} }

View file

@ -393,10 +393,147 @@ std::string ControlRange::toString() const
*/ */
/** /**
* \typedef ControlInfoMap * \class ControlInfoMap
* \brief A map of ControlId to ControlRange * \brief A map of ControlId to ControlRange
*
* The ControlInfoMap class describes controls supported by an object as an
* unsorted map of ControlId pointers to ControlRange instances. Unlike the
* standard std::unsorted_map<> class, it is designed the be immutable once
* constructed, and thus only exposes the read accessors of the
* std::unsorted_map<> base class.
*
* In addition to the features of the standard unsorted map, this class also
* provides access to the mapped elements using numerical ID keys. It maintains
* an internal map of numerical ID to ControlId for this purpose, and exposes it
* through the idmap() method to help construction of ControlList instances.
*/ */
/**
* \typedef ControlInfoMap::Map
* \brief The base std::unsorted_map<> container
*/
/**
* \fn ControlInfoMap::ControlInfoMap(const ControlInfoMap &other)
* \brief Copy constructor, construct a ControlInfoMap from a copy of \a other
* \param[in] other The other ControlInfoMap
*/
/**
* \brief Construct a ControlInfoMap from an initializer list
* \param[in] init The initializer list
*/
ControlInfoMap::ControlInfoMap(std::initializer_list<Map::value_type> init)
: Map(init)
{
generateIdmap();
}
/**
* \fn ControlInfoMap &ControlInfoMap::operator=(const ControlInfoMap &other)
* \brief Copy assignment operator, replace the contents with a copy of \a other
* \param[in] other The other ControlInfoMap
* \return A reference to the ControlInfoMap
*/
/**
* \brief Replace the contents with those from the initializer list
* \param[in] init The initializer list
* \return A reference to the ControlInfoMap
*/
ControlInfoMap &ControlInfoMap::operator=(std::initializer_list<Map::value_type> init)
{
Map::operator=(init);
generateIdmap();
return *this;
}
/**
* \brief Move assignment operator from a plain map
* \param[in] info The control info plain map
*
* Populate the map by replacing its contents with those of \a info using move
* semantics. Upon return the \a info map will be empty.
*
* \return A reference to the populated ControlInfoMap
*/
ControlInfoMap &ControlInfoMap::operator=(Map &&info)
{
Map::operator=(std::move(info));
generateIdmap();
return *this;
}
/**
* \brief Access specified element by numerical ID
* \param[in] id The numerical ID
* \return A reference to the element whose ID is equal to \a id
*/
ControlInfoMap::mapped_type &ControlInfoMap::at(unsigned int id)
{
return at(idmap_.at(id));
}
/**
* \brief Access specified element by numerical ID
* \param[in] id The numerical ID
* \return A const reference to the element whose ID is equal to \a id
*/
const ControlInfoMap::mapped_type &ControlInfoMap::at(unsigned int id) const
{
return at(idmap_.at(id));
}
/**
* \brief Count the number of elements matching a numerical ID
* \param[in] id The numerical ID
* \return The number of elements matching the numerical \a id
*/
ControlInfoMap::size_type ControlInfoMap::count(unsigned int id) const
{
return count(idmap_.at(id));
}
/**
* \brief Find the element matching a numerical ID
* \param[in] id The numerical ID
* \return An iterator pointing to the element matching the numerical \a id, or
* end() if no such element exists
*/
ControlInfoMap::iterator ControlInfoMap::find(unsigned int id)
{
return find(idmap_.at(id));
}
/**
* \brief Find the element matching a numerical ID
* \param[in] id The numerical ID
* \return A const iterator pointing to the element matching the numerical
* \a id, or end() if no such element exists
*/
ControlInfoMap::const_iterator ControlInfoMap::find(unsigned int id) const
{
return find(idmap_.at(id));
}
/**
* \fn const ControlIdMap &ControlInfoMap::idmap() const
* \brief Retrieve the ControlId map
*
* Constructing ControlList instances for V4L2 controls requires a ControlIdMap
* for the V4L2 device that the control list targets. This helper method
* returns a suitable idmap for that purpose.
*
* \return The ControlId map
*/
void ControlInfoMap::generateIdmap()
{
idmap_.clear();
for (const auto &ctrl : *this)
idmap_[ctrl.first->id()] = ctrl.first;
}
/** /**
* \class ControlList * \class ControlList
* \brief Associate a list of ControlId with their values for an object * \brief Associate a list of ControlId with their values for an object

View file

@ -16,9 +16,9 @@
namespace libcamera { namespace libcamera {
class ControlInfoMap;
class ControlList; class ControlList;
class MediaEntity; class MediaEntity;
class V4L2ControlInfoMap;
class V4L2Subdevice; class V4L2Subdevice;
struct V4L2SubdeviceFormat; struct V4L2SubdeviceFormat;
@ -43,7 +43,7 @@ public:
const Size &size) const; const Size &size) const;
int setFormat(V4L2SubdeviceFormat *format); int setFormat(V4L2SubdeviceFormat *format);
const V4L2ControlInfoMap &controls() const; const ControlInfoMap &controls() const;
int getControls(ControlList *ctrls); int getControls(ControlList *ctrls);
int setControls(ControlList *ctrls); int setControls(ControlList *ctrls);

View file

@ -31,44 +31,10 @@ public:
V4L2ControlRange(const struct v4l2_query_ext_ctrl &ctrl); V4L2ControlRange(const struct v4l2_query_ext_ctrl &ctrl);
}; };
class V4L2ControlInfoMap : private ControlInfoMap
{
public:
V4L2ControlInfoMap &operator=(ControlInfoMap &&info);
using ControlInfoMap::key_type;
using ControlInfoMap::mapped_type;
using ControlInfoMap::value_type;
using ControlInfoMap::size_type;
using ControlInfoMap::iterator;
using ControlInfoMap::const_iterator;
using ControlInfoMap::begin;
using ControlInfoMap::cbegin;
using ControlInfoMap::end;
using ControlInfoMap::cend;
using ControlInfoMap::at;
using ControlInfoMap::empty;
using ControlInfoMap::size;
using ControlInfoMap::count;
using ControlInfoMap::find;
mapped_type &at(unsigned int key);
const mapped_type &at(unsigned int key) const;
size_type count(unsigned int key) const;
iterator find(unsigned int key);
const_iterator find(unsigned int key) const;
const ControlIdMap &idmap() const { return idmap_; }
private:
ControlIdMap idmap_;
};
class V4L2ControlList : public ControlList class V4L2ControlList : public ControlList
{ {
public: public:
V4L2ControlList(const V4L2ControlInfoMap &info) V4L2ControlList(const ControlInfoMap &info)
: ControlList(info.idmap()) : ControlList(info.idmap())
{ {
} }

View file

@ -24,7 +24,7 @@ public:
void close(); void close();
bool isOpen() const { return fd_ != -1; } bool isOpen() const { return fd_ != -1; }
const V4L2ControlInfoMap &controls() const { return controls_; } const ControlInfoMap &controls() const { return controls_; }
int getControls(ControlList *ctrls); int getControls(ControlList *ctrls);
int setControls(ControlList *ctrls); int setControls(ControlList *ctrls);
@ -49,7 +49,7 @@ private:
unsigned int count); unsigned int count);
std::vector<std::unique_ptr<V4L2ControlId>> controlIds_; std::vector<std::unique_ptr<V4L2ControlId>> controlIds_;
V4L2ControlInfoMap controls_; ControlInfoMap controls_;
std::string deviceNode_; std::string deviceNode_;
int fd_; int fd_;
}; };

View file

@ -776,7 +776,7 @@ int PipelineHandlerRkISP1::start(Camera *camera)
.size = data->stream_.configuration().size, .size = data->stream_.configuration().size,
}; };
std::map<unsigned int, V4L2ControlInfoMap> entityControls; std::map<unsigned int, ControlInfoMap> entityControls;
entityControls.emplace(0, data->sensor_->controls()); entityControls.emplace(0, data->sensor_->controls());
data->ipa_->configure(streamConfig, entityControls); data->ipa_->configure(streamConfig, entityControls);
@ -875,10 +875,13 @@ int PipelineHandlerRkISP1::createCamera(MediaEntity *sensor)
std::unique_ptr<RkISP1CameraData> data = std::unique_ptr<RkISP1CameraData> data =
utils::make_unique<RkISP1CameraData>(this); utils::make_unique<RkISP1CameraData>(this);
data->controlInfo_.emplace(std::piecewise_construct, ControlInfoMap::Map ctrls;
ctrls.emplace(std::piecewise_construct,
std::forward_as_tuple(&controls::AeEnable), std::forward_as_tuple(&controls::AeEnable),
std::forward_as_tuple(false, true)); std::forward_as_tuple(false, true));
data->controlInfo_ = std::move(ctrls);
data->sensor_ = new CameraSensor(sensor); data->sensor_ = new CameraSensor(sensor);
ret = data->sensor_->init(); ret = data->sensor_->init();
if (ret) if (ret)

View file

@ -335,7 +335,9 @@ int UVCCameraData::init(MediaEntity *entity)
video_->bufferReady.connect(this, &UVCCameraData::bufferReady); video_->bufferReady.connect(this, &UVCCameraData::bufferReady);
/* Initialise the supported controls. */ /* Initialise the supported controls. */
const V4L2ControlInfoMap &controls = video_->controls(); const ControlInfoMap &controls = video_->controls();
ControlInfoMap::Map ctrls;
for (const auto &ctrl : controls) { for (const auto &ctrl : controls) {
const ControlRange &range = ctrl.second; const ControlRange &range = ctrl.second;
const ControlId *id; const ControlId *id;
@ -360,11 +362,13 @@ int UVCCameraData::init(MediaEntity *entity)
continue; continue;
} }
controlInfo_.emplace(std::piecewise_construct, ctrls.emplace(std::piecewise_construct,
std::forward_as_tuple(id), std::forward_as_tuple(id),
std::forward_as_tuple(range)); std::forward_as_tuple(range));
} }
controlInfo_ = std::move(ctrls);
return 0; return 0;
} }

View file

@ -411,7 +411,9 @@ int VimcCameraData::init(MediaDevice *media)
return -ENODEV; return -ENODEV;
/* Initialise the supported controls. */ /* Initialise the supported controls. */
const V4L2ControlInfoMap &controls = sensor_->controls(); const ControlInfoMap &controls = sensor_->controls();
ControlInfoMap::Map ctrls;
for (const auto &ctrl : controls) { for (const auto &ctrl : controls) {
const ControlRange &range = ctrl.second; const ControlRange &range = ctrl.second;
const ControlId *id; const ControlId *id;
@ -430,11 +432,12 @@ int VimcCameraData::init(MediaDevice *media)
continue; continue;
} }
controlInfo_.emplace(std::piecewise_construct, ctrls.emplace(std::piecewise_construct,
std::forward_as_tuple(id), std::forward_as_tuple(id),
std::forward_as_tuple(range)); std::forward_as_tuple(range));
} }
controlInfo_ = std::move(ctrls);
return 0; return 0;
} }

View file

@ -28,7 +28,7 @@ public:
int init() override { return 0; } int init() override { return 0; }
void configure(const std::map<unsigned int, IPAStream> &streamConfig, void configure(const std::map<unsigned int, IPAStream> &streamConfig,
const std::map<unsigned int, V4L2ControlInfoMap> &entityControls) override {} const std::map<unsigned int, ControlInfoMap> &entityControls) override {}
void mapBuffers(const std::vector<IPABuffer> &buffers) override {} void mapBuffers(const std::vector<IPABuffer> &buffers) override {}
void unmapBuffers(const std::vector<unsigned int> &ids) override {} void unmapBuffers(const std::vector<unsigned int> &ids) override {}
void processEvent(const IPAOperationData &event) override {} void processEvent(const IPAOperationData &event) override {}

View file

@ -126,102 +126,12 @@ V4L2ControlRange::V4L2ControlRange(const struct v4l2_query_ext_ctrl &ctrl)
static_cast<int32_t>(ctrl.maximum))); static_cast<int32_t>(ctrl.maximum)));
} }
/**
* \class V4L2ControlInfoMap
* \brief A map of controlID to ControlRange for V4L2 controls
*/
/**
* \brief Move assignment operator from a ControlInfoMap
* \param[in] info The control info map
*
* Populate the map by replacing its contents with those of \a info using move
* semantics. Upon return the \a info map will be empty.
*
* This is the only supported way to populate a V4L2ControlInfoMap.
*
* \return The populated V4L2ControlInfoMap
*/
V4L2ControlInfoMap &V4L2ControlInfoMap::operator=(ControlInfoMap &&info)
{
ControlInfoMap::operator=(std::move(info));
idmap_.clear();
for (const auto &ctrl : *this)
idmap_[ctrl.first->id()] = ctrl.first;
return *this;
}
/**
* \brief Access specified element by numerical ID
* \param[in] id The numerical ID
* \return A reference to the element whose ID is equal to \a id
*/
V4L2ControlInfoMap::mapped_type &V4L2ControlInfoMap::at(unsigned int id)
{
return at(idmap_.at(id));
}
/**
* \brief Access specified element by numerical ID
* \param[in] id The numerical ID
* \return A const reference to the element whose ID is equal to \a id
*/
const V4L2ControlInfoMap::mapped_type &V4L2ControlInfoMap::at(unsigned int id) const
{
return at(idmap_.at(id));
}
/**
* \brief Count the number of elements matching a numerical ID
* \param[in] id The numerical ID
* \return The number of elements matching the numerical \a id
*/
V4L2ControlInfoMap::size_type V4L2ControlInfoMap::count(unsigned int id) const
{
return count(idmap_.at(id));
}
/**
* \brief Find the element matching a numerical ID
* \param[in] id The numerical ID
* \return An iterator pointing to the element matching the numerical \a id, or
* end() if no such element exists
*/
V4L2ControlInfoMap::iterator V4L2ControlInfoMap::find(unsigned int id)
{
return find(idmap_.at(id));
}
/**
* \brief Find the element matching a numerical ID
* \param[in] id The numerical ID
* \return A const iterator pointing to the element matching the numerical
* \a id, or end() if no such element exists
*/
V4L2ControlInfoMap::const_iterator V4L2ControlInfoMap::find(unsigned int id) const
{
return find(idmap_.at(id));
}
/**
* \fn const ControlIdMap &V4L2ControlInfoMap::idmap() const
* \brief Retrieve the ControlId map
*
* Constructing ControlList instances for V4L2 controls requires a ControlIdMap
* for the V4L2 device that the control list targets. This helper method
* returns a suitable idmap for that purpose.
*
* \return The ControlId map
*/
/** /**
* \class V4L2ControlList * \class V4L2ControlList
* \brief A list of controls for a V4L2 device * \brief A list of controls for a V4L2 device
* *
* This class specialises the ControList class for use with V4L2 devices. It * This class specialises the ControList class for use with V4L2 devices. It
* offers a convenience API to create a ControlList from a V4L2ControlInfoMap. * offers a convenience API to create a ControlList from a ControlInfoMap.
* *
* V4L2ControlList allows easy construction of a ControlList containing V4L2 * V4L2ControlList allows easy construction of a ControlList containing V4L2
* controls for a device. It can be used to construct the list of controls * controls for a device. It can be used to construct the list of controls
@ -231,7 +141,7 @@ V4L2ControlInfoMap::const_iterator V4L2ControlInfoMap::find(unsigned int id) con
*/ */
/** /**
* \fn V4L2ControlList::V4L2ControlList(const V4L2ControlInfoMap &info) * \fn V4L2ControlList::V4L2ControlList(const ControlInfoMap &info)
* \brief Construct a V4L2ControlList associated with a V4L2 device * \brief Construct a V4L2ControlList associated with a V4L2 device
* \param[in] info The V4L2 device control info map * \param[in] info The V4L2 device control info map
*/ */

View file

@ -342,7 +342,7 @@ int V4L2Device::ioctl(unsigned long request, void *argp)
*/ */
void V4L2Device::listControls() void V4L2Device::listControls()
{ {
ControlInfoMap ctrls; ControlInfoMap::Map ctrls;
struct v4l2_query_ext_ctrl ctrl = {}; struct v4l2_query_ext_ctrl ctrl = {};
/* \todo Add support for menu and compound controls. */ /* \todo Add support for menu and compound controls. */

View file

@ -26,7 +26,7 @@ public:
protected: protected:
int run() int run()
{ {
const V4L2ControlInfoMap &info = capture_->controls(); const ControlInfoMap &info = capture_->controls();
/* Test control enumeration. */ /* Test control enumeration. */
if (info.empty()) { if (info.empty()) {