@@ -97,6 +97,46 @@ private:
unsigned int version_;
unsigned int hwRevision_;
+ class MediaV2Topology
+ {
+ public:
+ uint64_t version() const { return topology_.topology_version; }
+
+ void allocate()
+ {
+ if (!topology_.num_entities)
+ return;
+
+ clear();
+
+ ents_ = new struct media_v2_entity[topology_.num_entities]();
+ interfaces_ = new struct media_v2_interface[topology_.num_interfaces]();
+ links_ = new struct media_v2_link[topology_.num_links]();
+ pads_ = new struct media_v2_pad[topology_.num_pads]();
+
+ topology_.ptr_entities = reinterpret_cast<uintptr_t>(ents_);
+ topology_.ptr_interfaces = reinterpret_cast<uintptr_t>(interfaces_);
+ topology_.ptr_links = reinterpret_cast<uintptr_t>(links_);
+ topology_.ptr_pads = reinterpret_cast<uintptr_t>(pads_);
+ }
+
+ void clear()
+ {
+ delete[] ents_;
+ delete[] interfaces_;
+ delete[] pads_;
+ delete[] links_;
+ }
+
+ struct media_v2_topology topology_ = {};
+
+ private:
+ struct media_v2_entity *ents_ = nullptr;
+ struct media_v2_interface *interfaces_ = nullptr;
+ struct media_v2_link *links_ = nullptr;
+ struct media_v2_pad *pads_ = nullptr;
+ } mediaV2Topology_;
+
UniqueFD fd_;
bool valid_;
bool acquired_;
@@ -195,11 +195,6 @@ void MediaDevice::unlock()
*/
int MediaDevice::populate()
{
- struct media_v2_topology topology = {};
- struct media_v2_entity *ents = nullptr;
- struct media_v2_interface *interfaces = nullptr;
- struct media_v2_link *links = nullptr;
- struct media_v2_pad *pads = nullptr;
__u64 version = -1;
int ret;
@@ -227,13 +222,7 @@ int MediaDevice::populate()
* Keep calling G_TOPOLOGY until the version number stays stable.
*/
while (true) {
- topology.topology_version = 0;
- topology.ptr_entities = reinterpret_cast<uintptr_t>(ents);
- topology.ptr_interfaces = reinterpret_cast<uintptr_t>(interfaces);
- topology.ptr_links = reinterpret_cast<uintptr_t>(links);
- topology.ptr_pads = reinterpret_cast<uintptr_t>(pads);
-
- ret = ioctl(fd_.get(), MEDIA_IOC_G_TOPOLOGY, &topology);
+ ret = ioctl(fd_.get(), MEDIA_IOC_G_TOPOLOGY, &mediaV2Topology_.topology_);
if (ret < 0) {
ret = -errno;
LOG(MediaDevice, Error)
@@ -242,37 +231,23 @@ int MediaDevice::populate()
goto done;
}
- if (version == topology.topology_version)
+ if (version == mediaV2Topology_.version())
break;
- delete[] ents;
- delete[] interfaces;
- delete[] pads;
- delete[] links;
-
- ents = new struct media_v2_entity[topology.num_entities]();
- interfaces = new struct media_v2_interface[topology.num_interfaces]();
- links = new struct media_v2_link[topology.num_links]();
- pads = new struct media_v2_pad[topology.num_pads]();
-
- version = topology.topology_version;
+ mediaV2Topology_.allocate();
+ version = mediaV2Topology_.version();
}
/* Populate entities, pads and links. */
- if (populateEntities(topology) &&
- populatePads(topology) &&
- populateLinks(topology))
+ if (populateEntities(mediaV2Topology_.topology_) &&
+ populatePads(mediaV2Topology_.topology_) &&
+ populateLinks(mediaV2Topology_.topology_))
valid_ = true;
ret = 0;
done:
close();
- delete[] ents;
- delete[] interfaces;
- delete[] pads;
- delete[] links;
-
if (!valid_) {
clear();
return -EINVAL;
@@ -657,6 +632,8 @@ bool MediaDevice::addObject(MediaObject *object)
*/
void MediaDevice::clear()
{
+ mediaV2Topology_.clear();
+
for (const auto &o : objects_)
delete o.second;
Create an helper class in MediaDevice that wraps the kernel type struct media_v2_topology and its backing memory instead of manually allocating it in MediaDevice::populate(). Make an instance of the new MediaV2Topology a class member. This means the kernel types and their backing memory remain valid after MediaDevice::populate() returns. This will be useful to delay the creation of MediaObject to a later point in time, and have the MediaDevice class users that need to only enumerate entities to operate using the raw kernel structures. In the long run, this will allow us to split the MediaDevice class into a static version and one populated version where MediaObjects are accessible from. Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com> --- include/libcamera/internal/media_device.h | 40 ++++++++++++++++++++++++++++++ src/libcamera/media_device.cpp | 41 +++++++------------------------ 2 files changed, 49 insertions(+), 32 deletions(-)