Skip to content

Commit

Permalink
remove context.get_backend(); add backend::instance()
Browse files Browse the repository at this point in the history
  • Loading branch information
maloel committed Oct 1, 2023
1 parent f84eec6 commit f3a7022
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 43 deletions.
18 changes: 18 additions & 0 deletions src/backend-device-factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
#include <rsutils/json.h>


namespace librealsense {
namespace platform {
std::shared_ptr< backend > create_backend();
} // namespace platform
} // namespace librealsense


namespace {


Expand Down Expand Up @@ -128,6 +135,17 @@ class device_watcher_singleton
static rsutils::shared_ptr_singleton< device_watcher_singleton > backend_device_watcher;


/*static*/ std::shared_ptr< platform::backend > platform::backend::instance()
{
auto singleton = the_backend.get();
if( ! singleton )
// Whoever is calling us, they are expecting a backend to exist, but it does not!
throw std::runtime_error( "backend not created yet!" );

return singleton->get();
}


backend_device_factory::backend_device_factory( context & ctx, callback && cb )
: _context( ctx )
, _device_watcher( backend_device_watcher.instance() )
Expand Down
6 changes: 4 additions & 2 deletions src/backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ namespace librealsense
}

virtual ~backend() = default;
};

std::shared_ptr<backend> create_backend();

// Get an existing backend. Will throw if there's none!
static std::shared_ptr< backend > instance();
};

}

Expand Down
1 change: 0 additions & 1 deletion src/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ namespace librealsense
static unsigned combine_device_masks( unsigned requested_mask, unsigned mask_in_settings );

std::vector<std::shared_ptr<device_info>> query_devices(int mask) const;
const platform::backend& get_backend() const { return *_backend_device_factory.get_backend(); }

uint64_t register_internal_device_callback(devices_changed_callback_ptr callback);
void unregister_internal_device_callback(uint64_t cb_id);
Expand Down
3 changes: 1 addition & 2 deletions src/ds/d400/d400-color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ namespace librealsense
void d400_color::create_color_device(std::shared_ptr<context> ctx, const platform::backend_device_group& group)
{
using namespace ds;
auto&& backend = ctx->get_backend();

_color_calib_table_raw = [this]()
{
Expand Down Expand Up @@ -82,7 +81,7 @@ namespace librealsense
info = color_devs_info[1];
else
info = color_devs_info.front();
auto uvcd = backend.create_uvc_device(info);
auto uvcd = platform::backend::instance()->create_uvc_device( info );
//auto ftr = std::unique_ptr<frame_timestamp_reader>(new global_timestamp_reader(std::move(d400_timestamp_reader_metadata), _tf_keeper, enable_global_time_option));
auto raw_color_ep = std::make_shared<uvc_sensor>("Raw RGB Camera",
uvcd,
Expand Down
17 changes: 6 additions & 11 deletions src/ds/d400/d400-device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -462,11 +462,9 @@ namespace librealsense
{
using namespace ds;

auto&& backend = ctx->get_backend();

std::vector<std::shared_ptr<platform::uvc_device>> depth_devices;
for (auto&& info : filter_by_mi(all_device_infos, 0)) // Filter just mi=0, DEPTH
depth_devices.push_back(backend.create_uvc_device(info));
depth_devices.push_back( platform::backend::instance()->create_uvc_device( info ) );

std::unique_ptr< frame_timestamp_reader > timestamp_reader_backup( new ds_timestamp_reader() );
frame_timestamp_reader* timestamp_reader_from_metadata;
Expand Down Expand Up @@ -516,7 +514,6 @@ namespace librealsense
{
using namespace ds;

auto&& backend = ctx->get_backend();
auto& raw_sensor = get_raw_depth_sensor();
_pid = group.uvc_devices.front().pid;
// to be changed for D457
Expand All @@ -537,10 +534,10 @@ namespace librealsense
}
else
{
if (!mipi_sensor)
_hw_monitor = std::make_shared<hw_monitor>(
std::make_shared<locked_transfer>(
backend.create_usb_device(group.usb_devices.front()), raw_sensor));
if( ! mipi_sensor )
_hw_monitor = std::make_shared< hw_monitor >( std::make_shared< locked_transfer >(
platform::backend::instance()->create_usb_device( group.usb_devices.front() ),
raw_sensor ) );
}
set_hw_monitor_for_auto_calib(_hw_monitor);

Expand Down Expand Up @@ -1212,11 +1209,9 @@ namespace librealsense
{
using namespace ds;

auto&& backend = ctx->get_backend();

std::vector<std::shared_ptr<platform::uvc_device>> depth_devices;
for (auto&& info : filter_by_mi(all_device_infos, 0)) // Filter just mi=0, DEPTH
depth_devices.push_back(backend.create_uvc_device(info));
depth_devices.push_back( platform::backend::instance()->create_uvc_device( info ) );

std::unique_ptr< frame_timestamp_reader > d400_timestamp_reader_backup( new ds_timestamp_reader() );
std::unique_ptr<frame_timestamp_reader> d400_timestamp_reader_metadata(new ds_timestamp_reader_from_metadata(std::move(d400_timestamp_reader_backup)));
Expand Down
15 changes: 9 additions & 6 deletions src/ds/d400/d400-motion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,9 @@ namespace librealsense
return nullptr;
}

auto&& backend = ctx->get_backend();

std::vector<std::shared_ptr<platform::uvc_device>> imu_devices;
for (auto&& info : filter_by_mi(all_uvc_infos, 4)) // Filter just mi=4, IMU
imu_devices.push_back(backend.create_uvc_device(info));
imu_devices.push_back( platform::backend::instance()->create_uvc_device( info ) );

std::unique_ptr< frame_timestamp_reader > timestamp_reader_backup( new ds_timestamp_reader() );
std::unique_ptr<frame_timestamp_reader> timestamp_reader_metadata(new ds_timestamp_reader_from_metadata_mipi_motion(std::move(timestamp_reader_backup)));
Expand Down Expand Up @@ -170,11 +168,16 @@ namespace librealsense
return;

std::unique_ptr< frame_timestamp_reader > ds_timestamp_reader_backup( new ds_timestamp_reader() );
auto&& backend = ctx->get_backend();
std::unique_ptr<frame_timestamp_reader> ds_timestamp_reader_metadata(new ds_timestamp_reader_from_metadata(std::move(ds_timestamp_reader_backup)));
auto enable_global_time_option = std::shared_ptr<global_time_option>(new global_time_option());
auto raw_fisheye_ep = std::make_shared<uvc_sensor>("FishEye Sensor", backend.create_uvc_device(fisheye_infos.front()),
std::unique_ptr<frame_timestamp_reader>(new global_timestamp_reader(std::move(ds_timestamp_reader_metadata), _tf_keeper, enable_global_time_option)), this);
auto raw_fisheye_ep
= std::make_shared< uvc_sensor >( "FishEye Sensor",
platform::backend::instance()->create_uvc_device( fisheye_infos.front() ),
std::unique_ptr< frame_timestamp_reader >( new global_timestamp_reader(
std::move( ds_timestamp_reader_metadata ),
_tf_keeper,
enable_global_time_option ) ),
this );
auto fisheye_ep = std::make_shared<ds_fisheye_sensor>(raw_fisheye_ep, this);

_ds_motion_common->assign_fisheye_ep(raw_fisheye_ep, fisheye_ep, enable_global_time_option);
Expand Down
13 changes: 8 additions & 5 deletions src/ds/d500/d500-color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ namespace librealsense
void d500_color::create_color_device(std::shared_ptr<context> ctx, const platform::backend_device_group& group)
{
using namespace ds;
auto&& backend = ctx->get_backend();

_color_calib_table_raw = [this]()
{
Expand All @@ -61,10 +60,14 @@ namespace librealsense
std::unique_ptr<frame_timestamp_reader> ds_timestamp_reader_metadata(new ds_timestamp_reader_from_metadata(std::move(ds_timestamp_reader_backup)));

auto enable_global_time_option = std::shared_ptr<global_time_option>(new global_time_option());
auto raw_color_ep = std::make_shared<uvc_sensor>("Raw RGB Camera",
backend.create_uvc_device(color_devs_info.front()),
std::unique_ptr<frame_timestamp_reader>(new global_timestamp_reader(std::move(ds_timestamp_reader_metadata), _tf_keeper, enable_global_time_option)),
this);
auto raw_color_ep = std::make_shared< uvc_sensor >(
"Raw RGB Camera",
platform::backend::instance()->create_uvc_device( color_devs_info.front() ),
std::unique_ptr< frame_timestamp_reader >(
new global_timestamp_reader( std::move( ds_timestamp_reader_metadata ),
_tf_keeper,
enable_global_time_option ) ),
this );

auto color_ep = std::make_shared<d500_color_sensor>(this,
raw_color_ep,
Expand Down
11 changes: 4 additions & 7 deletions src/ds/d500/d500-device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,11 +346,9 @@ namespace librealsense
{
using namespace ds;

auto&& backend = ctx->get_backend();

std::vector<std::shared_ptr<platform::uvc_device>> depth_devices;
for (auto&& info : filter_by_mi(all_device_infos, 0)) // Filter just mi=0, DEPTH
depth_devices.push_back(backend.create_uvc_device(info));
depth_devices.push_back( platform::backend::instance()->create_uvc_device( info ) );

std::unique_ptr< frame_timestamp_reader > timestamp_reader_backup( new ds_timestamp_reader() );
std::unique_ptr<frame_timestamp_reader> timestamp_reader_metadata(new ds_timestamp_reader_from_metadata(std::move(timestamp_reader_backup)));
Expand Down Expand Up @@ -393,7 +391,6 @@ namespace librealsense
{
using namespace ds;

auto&& backend = ctx->get_backend();
auto& raw_sensor = get_raw_depth_sensor();
_pid = group.uvc_devices.front().pid;

Expand All @@ -412,9 +409,9 @@ namespace librealsense
}
else
{
_hw_monitor = std::make_shared<hw_monitor_extended_buffers>(
std::make_shared<locked_transfer>(
backend.create_usb_device(group.usb_devices.front()), raw_sensor));
_hw_monitor = std::make_shared< hw_monitor_extended_buffers >( std::make_shared< locked_transfer >(
platform::backend::instance()->create_usb_device( group.usb_devices.front() ),
raw_sensor ) );
}

_ds_device_common = std::make_shared<ds_device_common>(this, _hw_monitor);
Expand Down
11 changes: 8 additions & 3 deletions src/ds/d500/d500-motion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,16 @@ namespace librealsense
return;

std::unique_ptr< frame_timestamp_reader > ds_timestamp_reader_backup( new ds_timestamp_reader() );
auto&& backend = ctx->get_backend();
std::unique_ptr<frame_timestamp_reader> ds_timestamp_reader_metadata(new ds_timestamp_reader_from_metadata(std::move(ds_timestamp_reader_backup)));
auto enable_global_time_option = std::shared_ptr<global_time_option>(new global_time_option());
auto raw_fisheye_ep = std::make_shared<uvc_sensor>("FishEye Sensor", backend.create_uvc_device(fisheye_infos.front()),
std::unique_ptr<frame_timestamp_reader>(new global_timestamp_reader(std::move(ds_timestamp_reader_metadata), _tf_keeper, enable_global_time_option)), this);
auto raw_fisheye_ep
= std::make_shared< uvc_sensor >( "FishEye Sensor",
platform::backend::instance()->create_uvc_device( fisheye_infos.front() ),
std::unique_ptr< frame_timestamp_reader >( new global_timestamp_reader(
std::move( ds_timestamp_reader_metadata ),
_tf_keeper,
enable_global_time_option ) ),
this );
auto fisheye_ep = std::make_shared<ds_fisheye_sensor>(raw_fisheye_ep, this);

_ds_motion_common->assign_fisheye_ep(raw_fisheye_ep, fisheye_ep, enable_global_time_option);
Expand Down
12 changes: 8 additions & 4 deletions src/ds/ds-motion-common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -472,12 +472,16 @@ namespace librealsense
}
_fps_and_sampling_frequency_per_rs2_stream[RS2_STREAM_ACCEL] = fps_and_frequency_map;

auto raw_hid_ep = std::make_shared<hid_sensor>(ctx->get_backend().create_hid_device(all_hid_infos.front()),
std::unique_ptr<frame_timestamp_reader>(new global_timestamp_reader(std::move(iio_hid_ts_reader), tf_keeper, enable_global_time_option)),
std::unique_ptr<frame_timestamp_reader>(new global_timestamp_reader(std::move(custom_hid_ts_reader), tf_keeper, enable_global_time_option)),
auto raw_hid_ep = std::make_shared< hid_sensor >(
platform::backend::instance()->create_hid_device( all_hid_infos.front() ),
std::unique_ptr< frame_timestamp_reader >(
new global_timestamp_reader( std::move( iio_hid_ts_reader ), tf_keeper, enable_global_time_option ) ),
std::unique_ptr< frame_timestamp_reader >( new global_timestamp_reader( std::move( custom_hid_ts_reader ),
tf_keeper,
enable_global_time_option ) ),
_fps_and_sampling_frequency_per_rs2_stream,
_sensor_name_and_hid_profiles,
_owner);
_owner );

auto hid_ep = std::make_shared<ds_motion_sensor>("Motion Module", raw_hid_ep, _owner);

Expand Down
5 changes: 3 additions & 2 deletions src/platform-camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ platform_camera::platform_camera( std::shared_ptr< const device_info > const & d
: device( dev_info, register_device_notifications )
{
std::vector< std::shared_ptr< platform::uvc_device > > devs;
for( auto && info : uvc_infos )
devs.push_back( dev_info->get_context()->get_backend().create_uvc_device( info ) );
auto backend = platform::backend::instance();
for( auto & info : uvc_infos )
devs.push_back( backend->create_uvc_device( info ) );

std::unique_ptr< frame_timestamp_reader > host_timestamp_reader_backup( new ds_timestamp_reader() );
auto raw_color_ep = std::make_shared< uvc_sensor >(
Expand Down

0 comments on commit f3a7022

Please sign in to comment.