[6/6] libcamera: media_device: Delay topology creation at acquire()
diff mbox series

Message ID 20261119-multicontext-prep-v1-6-f68ea7ef960f@ideasonboard.com
State New
Headers show
Series
  • libcamera: Prepare to add MediaContext
Related show

Commit Message

Jacopo Mondi Nov. 19, 2026, 4:39 p.m. UTC
Now that we have removed all external dependencies on the MediaObject
class hierarchy, we can delay its creation at MediaDevice::acquire()
time.

The only remaining user is MediaDevice::match() which runs before
acquire().

As this is an internal user, modify it to use the raw kernel types
to perform matching so that it can be run before MediaObjects are
created from the raw topology.

Add an enumerated_ flag to avoid double enumeration on
acquire/release/acquire cycles, which is effectivelly a use case only
for test units. The additional flag is not an ideal solution but will
only stay until the introduction of the MediaContext class.

Now that MediaObjects are only available after acquire(), update all
tests that create V4L2Devices by entity name to aquire the media
device first.

Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
---
 include/libcamera/internal/media_device.h   |  1 +
 src/libcamera/device_enumerator.cpp         |  2 --
 src/libcamera/media_device.cpp              | 20 ++++++++++++++------
 test/camera-sensor.cpp                      |  8 ++++++++
 test/delayed_controls.cpp                   | 12 ++++++++++++
 test/libtest/buffer_source.cpp              |  6 ++++++
 test/v4l2_subdevice/v4l2_subdevice_test.cpp |  7 +++++++
 test/v4l2_videodevice/v4l2_m2mdevice.cpp    |  8 ++++++++
 8 files changed, 56 insertions(+), 8 deletions(-)

Patch
diff mbox series

diff --git a/include/libcamera/internal/media_device.h b/include/libcamera/internal/media_device.h
index 0e064e3fbcde..72b387926ee9 100644
--- a/include/libcamera/internal/media_device.h
+++ b/include/libcamera/internal/media_device.h
@@ -169,6 +169,7 @@  private:
 	UniqueFD fd_;
 	bool valid_;
 	bool acquired_;
+	bool enumerated_;
 	std::optional<bool> supportsRequests_;
 
 	std::map<unsigned int, MediaObject *> objects_;
diff --git a/src/libcamera/device_enumerator.cpp b/src/libcamera/device_enumerator.cpp
index ded3b41b5f26..2b1056425f61 100644
--- a/src/libcamera/device_enumerator.cpp
+++ b/src/libcamera/device_enumerator.cpp
@@ -248,8 +248,6 @@  void DeviceEnumerator::addDevice(std::unique_ptr<MediaDevice> media)
 	LOG(DeviceEnumerator, Debug)
 		<< "Added device " << media->deviceNode() << ": " << media->driver();
 
-	media->createTopology();
-
 	devices_.push_back(std::move(media));
 
 	/* \todo To batch multiple additions, emit with a small delay here. */
diff --git a/src/libcamera/media_device.cpp b/src/libcamera/media_device.cpp
index 0d5c3a18b471..ad38d7c4078e 100644
--- a/src/libcamera/media_device.cpp
+++ b/src/libcamera/media_device.cpp
@@ -66,7 +66,7 @@  LOG_DEFINE_CATEGORY(MediaDevice)
  * populate() before the media graph can be queried.
  */
 MediaDevice::MediaDevice(const std::string &deviceNode)
-	: deviceNode_(deviceNode), valid_(false), acquired_(false)
+	: deviceNode_(deviceNode), valid_(false), acquired_(false), enumerated_(false)
 {
 }
 
@@ -111,7 +111,13 @@  bool MediaDevice::acquire()
 	if (open())
 		return false;
 
+	if (!enumerated_ && !createTopology()) {
+		close();
+		return false;
+	}
+
 	acquired_ = true;
+	enumerated_ = true;
 	return true;
 }
 
@@ -322,13 +328,14 @@  bool MediaDevice::match(const DeviceMatch &device) const
 					const auto &searchFn, bool breakEarly) {
 		bool found = false;
 
-		for (const MediaEntity *entity : entities()) {
-			if (!searchFn(searchPattern, entity->name()))
+		for (const auto &ent : mediaV2Topology_.entities()) {
+			if (!searchFn(searchPattern, ent.name))
 				continue;
 
-			if (entity->deviceNode().empty()) {
+			const auto &it = devnodesMap_.find(ent.id);
+			if (it == devnodesMap_.end()) {
 				LOG(MediaDevice, Debug)
-					<< "Skip " << entity->name()
+					<< "Skip " << ent.name
 					<< ": no device node";
 				continue;
 			}
@@ -336,7 +343,7 @@  bool MediaDevice::match(const DeviceMatch &device) const
 			if (found) {
 				LOG(MediaDevice, Error)
 					<< "Multiple entities match "
-					<< entity->name();
+					<< ent.name;
 				return false;
 			}
 
@@ -710,6 +717,7 @@  void MediaDevice::clear()
 	objects_.clear();
 	entities_.clear();
 	valid_ = false;
+	enumerated_ = false;
 }
 
 /**
diff --git a/test/camera-sensor.cpp b/test/camera-sensor.cpp
index c30a2212e362..04f01d2a14c7 100644
--- a/test/camera-sensor.cpp
+++ b/test/camera-sensor.cpp
@@ -46,6 +46,12 @@  protected:
 			return TestSkip;
 		}
 
+		if (!media_->acquire()) {
+			cerr << "Unable to acquire media device "
+			     << media_->driver() << endl;
+			return TestFail;
+		}
+
 		MediaEntity *entity = media_->getEntityByName("Sensor A");
 		if (!entity) {
 			cerr << "Unable to find media entity 'Sensor A'" << endl;
@@ -121,6 +127,8 @@  protected:
 
 	void cleanup()
 	{
+		if (media_)
+			media_->release();
 	}
 
 private:
diff --git a/test/delayed_controls.cpp b/test/delayed_controls.cpp
index 7bd30e7aead8..36bb522e43b3 100644
--- a/test/delayed_controls.cpp
+++ b/test/delayed_controls.cpp
@@ -47,6 +47,12 @@  protected:
 			return TestSkip;
 		}
 
+		if (!media_->acquire()) {
+			cerr << "Unable to acquire media device "
+			     << media_->driver() << endl;
+			return TestFail;
+		}
+
 		dev_ = V4L2VideoDevice::fromEntityName(media_.get(), "vivid-000-vid-cap");
 		if (dev_->open()) {
 			cerr << "Failed to open video device" << endl;
@@ -70,6 +76,12 @@  protected:
 		return TestPass;
 	}
 
+	void cleanup() override
+	{
+		if (media_)
+			media_->release();
+	}
+
 	int singleControlNoDelay()
 	{
 		std::unordered_map<uint32_t, DelayedControls::ControlParams> delays = {
diff --git a/test/libtest/buffer_source.cpp b/test/libtest/buffer_source.cpp
index 97c8ae1cbd31..756931918871 100644
--- a/test/libtest/buffer_source.cpp
+++ b/test/libtest/buffer_source.cpp
@@ -52,6 +52,12 @@  int BufferSource::allocate(const StreamConfiguration &config)
 		return TestSkip;
 	}
 
+	if (!media_->acquire()) {
+		std::cerr << "Unable to acquire media device "
+			  << media_->driver() << std::endl;
+		return TestFail;
+	}
+
 	std::unique_ptr<V4L2VideoDevice> video = V4L2VideoDevice::fromEntityName(media_.get(),
 										 videoDeviceName);
 	if (!video) {
diff --git a/test/v4l2_subdevice/v4l2_subdevice_test.cpp b/test/v4l2_subdevice/v4l2_subdevice_test.cpp
index c349c9e31c40..59f44a3bc124 100644
--- a/test/v4l2_subdevice/v4l2_subdevice_test.cpp
+++ b/test/v4l2_subdevice/v4l2_subdevice_test.cpp
@@ -46,6 +46,11 @@  int V4L2SubdeviceTest::init()
 		return TestSkip;
 	}
 
+	if (!media_->acquire()) {
+		cerr << "Unable to acquire media device " << media_->driver() << endl;
+		return TestFail;
+	}
+
 	MediaEntity *videoEntity = media_->getEntityByName("Scaler");
 	if (!videoEntity) {
 		cerr << "Unable to find media entity 'Scaler'" << endl;
@@ -64,5 +69,7 @@  int V4L2SubdeviceTest::init()
 
 void V4L2SubdeviceTest::cleanup()
 {
+	if (media_)
+		media_->release();
 	delete scaler_;
 }
diff --git a/test/v4l2_videodevice/v4l2_m2mdevice.cpp b/test/v4l2_videodevice/v4l2_m2mdevice.cpp
index c45f581a8653..90dc87366746 100644
--- a/test/v4l2_videodevice/v4l2_m2mdevice.cpp
+++ b/test/v4l2_videodevice/v4l2_m2mdevice.cpp
@@ -75,6 +75,12 @@  protected:
 			return TestSkip;
 		}
 
+		if (!media_->acquire()) {
+			cerr << "Unable to acquire media device "
+			     << media_->driver() << endl;
+			return TestFail;
+		}
+
 		return TestPass;
 	}
 
@@ -193,6 +199,8 @@  protected:
 
 	void cleanup()
 	{
+		if (media_)
+			media_->release();
 		delete vim2m_;
 	}