mirror of
https://git.libcamera.org/libcamera/libcamera.git
synced 2025-07-20 10:55:07 +03:00
libcamera: device_enumerator: Convey device ownership through unique_ptr
Replace usage of shared_ptr with unique_ptr to convey media device ownership internally in the enumerators when creating the media device. Once a media device has all its dependencies met, it is converted to a shared_ptr to keep the external API unchanged. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
This commit is contained in:
parent
e75ef59e02
commit
9ab024f7c2
6 changed files with 20 additions and 20 deletions
|
@ -208,9 +208,9 @@ DeviceEnumerator::~DeviceEnumerator()
|
||||||
*
|
*
|
||||||
* \return Created media device instance on success, or nullptr otherwise
|
* \return Created media device instance on success, or nullptr otherwise
|
||||||
*/
|
*/
|
||||||
std::shared_ptr<MediaDevice> DeviceEnumerator::createDevice(const std::string &deviceNode)
|
std::unique_ptr<MediaDevice> DeviceEnumerator::createDevice(const std::string &deviceNode)
|
||||||
{
|
{
|
||||||
std::shared_ptr<MediaDevice> media = std::make_shared<MediaDevice>(deviceNode);
|
std::unique_ptr<MediaDevice> media = std::make_unique<MediaDevice>(deviceNode);
|
||||||
|
|
||||||
int ret = media->populate();
|
int ret = media->populate();
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
|
@ -236,12 +236,12 @@ std::shared_ptr<MediaDevice> DeviceEnumerator::createDevice(const std::string &d
|
||||||
* This method shall be called after all members of the entities of the
|
* This method shall be called after all members of the entities of the
|
||||||
* media graph have been confirmed to be initialized.
|
* media graph have been confirmed to be initialized.
|
||||||
*/
|
*/
|
||||||
void DeviceEnumerator::addDevice(const std::shared_ptr<MediaDevice> &media)
|
void DeviceEnumerator::addDevice(std::unique_ptr<MediaDevice> &&media)
|
||||||
{
|
{
|
||||||
LOG(DeviceEnumerator, Debug)
|
LOG(DeviceEnumerator, Debug)
|
||||||
<< "Added device " << media->deviceNode() << ": " << media->driver();
|
<< "Added device " << media->deviceNode() << ": " << media->driver();
|
||||||
|
|
||||||
devices_.push_back(media);
|
devices_.push_back(std::move(media));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -290,7 +290,7 @@ void DeviceEnumerator::removeDevice(const std::string &deviceNode)
|
||||||
*/
|
*/
|
||||||
std::shared_ptr<MediaDevice> DeviceEnumerator::search(const DeviceMatch &dm)
|
std::shared_ptr<MediaDevice> DeviceEnumerator::search(const DeviceMatch &dm)
|
||||||
{
|
{
|
||||||
for (std::shared_ptr<MediaDevice> media : devices_) {
|
for (std::shared_ptr<MediaDevice> &media : devices_) {
|
||||||
if (media->busy())
|
if (media->busy())
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
|
|
@ -72,11 +72,11 @@ int DeviceEnumeratorSysfs::enumerate()
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<MediaDevice> media = createDevice(devnode);
|
std::unique_ptr<MediaDevice> media = createDevice(devnode);
|
||||||
if (!media)
|
if (!media)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (populateMediaDevice(media) < 0) {
|
if (populateMediaDevice(media.get()) < 0) {
|
||||||
LOG(DeviceEnumerator, Warning)
|
LOG(DeviceEnumerator, Warning)
|
||||||
<< "Failed to populate media device "
|
<< "Failed to populate media device "
|
||||||
<< media->deviceNode()
|
<< media->deviceNode()
|
||||||
|
@ -84,7 +84,7 @@ int DeviceEnumeratorSysfs::enumerate()
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
addDevice(media);
|
addDevice(std::move(media));
|
||||||
}
|
}
|
||||||
|
|
||||||
closedir(dir);
|
closedir(dir);
|
||||||
|
@ -92,7 +92,7 @@ int DeviceEnumeratorSysfs::enumerate()
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int DeviceEnumeratorSysfs::populateMediaDevice(const std::shared_ptr<MediaDevice> &media)
|
int DeviceEnumeratorSysfs::populateMediaDevice(MediaDevice *media)
|
||||||
{
|
{
|
||||||
/* Associate entities to device node paths. */
|
/* Associate entities to device node paths. */
|
||||||
for (MediaEntity *entity : media->entities()) {
|
for (MediaEntity *entity : media->entities()) {
|
||||||
|
|
|
@ -76,7 +76,7 @@ int DeviceEnumeratorUdev::addUdevDevice(struct udev_device *dev)
|
||||||
return -ENODEV;
|
return -ENODEV;
|
||||||
|
|
||||||
if (!strcmp(subsystem, "media")) {
|
if (!strcmp(subsystem, "media")) {
|
||||||
std::shared_ptr<MediaDevice> media =
|
std::unique_ptr<MediaDevice> media =
|
||||||
createDevice(udev_device_get_devnode(dev));
|
createDevice(udev_device_get_devnode(dev));
|
||||||
if (!media)
|
if (!media)
|
||||||
return -ENODEV;
|
return -ENODEV;
|
||||||
|
@ -96,7 +96,7 @@ int DeviceEnumeratorUdev::addUdevDevice(struct udev_device *dev)
|
||||||
<< "Defer media device " << media->deviceNode()
|
<< "Defer media device " << media->deviceNode()
|
||||||
<< " due to " << ret << " missing dependencies";
|
<< " due to " << ret << " missing dependencies";
|
||||||
|
|
||||||
pending_.emplace_back(media, deps);
|
pending_.emplace_back(std::move(media), std::move(deps));
|
||||||
MediaDeviceDeps *mediaDeps = &pending_.back();
|
MediaDeviceDeps *mediaDeps = &pending_.back();
|
||||||
for (const auto &dep : mediaDeps->deps_)
|
for (const auto &dep : mediaDeps->deps_)
|
||||||
devMap_[dep.first] = mediaDeps;
|
devMap_[dep.first] = mediaDeps;
|
||||||
|
@ -104,7 +104,7 @@ int DeviceEnumeratorUdev::addUdevDevice(struct udev_device *dev)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
addDevice(media);
|
addDevice(std::move(media));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -319,7 +319,7 @@ int DeviceEnumeratorUdev::addV4L2Device(dev_t devnum)
|
||||||
LOG(DeviceEnumerator, Debug)
|
LOG(DeviceEnumerator, Debug)
|
||||||
<< "All dependencies for media device "
|
<< "All dependencies for media device "
|
||||||
<< deps->media_->deviceNode() << " found";
|
<< deps->media_->deviceNode() << " found";
|
||||||
addDevice(deps->media_);
|
addDevice(std::move(deps->media_));
|
||||||
pending_.remove(*deps);
|
pending_.remove(*deps);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -44,8 +44,8 @@ public:
|
||||||
std::shared_ptr<MediaDevice> search(const DeviceMatch &dm);
|
std::shared_ptr<MediaDevice> search(const DeviceMatch &dm);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::shared_ptr<MediaDevice> createDevice(const std::string &deviceNode);
|
std::unique_ptr<MediaDevice> createDevice(const std::string &deviceNode);
|
||||||
void addDevice(const std::shared_ptr<MediaDevice> &media);
|
void addDevice(std::unique_ptr<MediaDevice> &&media);
|
||||||
void removeDevice(const std::string &deviceNode);
|
void removeDevice(const std::string &deviceNode);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
@ -23,7 +23,7 @@ public:
|
||||||
int enumerate();
|
int enumerate();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int populateMediaDevice(const std::shared_ptr<MediaDevice> &media);
|
int populateMediaDevice(MediaDevice *media);
|
||||||
std::string lookupDeviceNode(int major, int minor);
|
std::string lookupDeviceNode(int major, int minor);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -43,9 +43,9 @@ private:
|
||||||
using DependencyMap = std::map<dev_t, std::list<MediaEntity *>>;
|
using DependencyMap = std::map<dev_t, std::list<MediaEntity *>>;
|
||||||
|
|
||||||
struct MediaDeviceDeps {
|
struct MediaDeviceDeps {
|
||||||
MediaDeviceDeps(const std::shared_ptr<MediaDevice> &media,
|
MediaDeviceDeps(std::unique_ptr<MediaDevice> &&media,
|
||||||
const DependencyMap &deps)
|
DependencyMap &&deps)
|
||||||
: media_(media), deps_(deps)
|
: media_(std::move(media)), deps_(std::move(deps))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,7 +54,7 @@ private:
|
||||||
return media_ == other.media_;
|
return media_ == other.media_;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<MediaDevice> media_;
|
std::unique_ptr<MediaDevice> media_;
|
||||||
DependencyMap deps_;
|
DependencyMap deps_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue