15
15
*
16
16
*/
17
17
18
+ #include < cstddef>
18
19
#include < map>
19
20
#include < mutex>
21
+ #include < string>
20
22
21
23
#include < gz/common/Console.hh>
22
24
#include < gz/common/SystemPaths.hh>
@@ -48,6 +50,11 @@ class gz::rendering::RenderEngineManagerPrivate
48
50
// / \brief EngineMap iterator.
49
51
typedef EngineMap::iterator EngineIter;
50
52
53
+ // / \brief Expected filename prefix for plugin to be loaded from the static
54
+ // / registry.
55
+ public: static constexpr std::string_view kStaticPluginFilenamePrefix =
56
+ " static://" ;
57
+
51
58
// / \brief Get a pointer to the render engine from an EngineMap iterator.
52
59
// / \param[in] _iter EngineMap iterator
53
60
// / \param[in] _path Another search path for rendering engine plugin.
@@ -78,6 +85,11 @@ class gz::rendering::RenderEngineManagerPrivate
78
85
public: bool LoadEnginePlugin (const std::string &_filename,
79
86
const std::string &_path);
80
87
88
+ // / \brief Load a render engine plugin from the static plugin registry.
89
+ // / \param[in] _filename Filename of plugin
90
+ // / \return True if the plugin is loaded successfully
91
+ public: bool LoadStaticEnginePlugin (const std::string &_filename);
92
+
81
93
// / \brief Unload a render engine plugin.
82
94
// / \param[in] _engineName Name of engine associated with this plugin
83
95
// / \return True if the plugin is unloaded successfully
@@ -431,32 +443,67 @@ bool RenderEngineManagerPrivate::UnloadEngine(EngineIter _iter)
431
443
// ////////////////////////////////////////////////
432
444
void RenderEngineManagerPrivate::RegisterDefaultEngines ()
433
445
{
434
- // TODO(anyone): Find a cleaner way to get the default engine library name
435
446
436
- // cppcheck-suppress unreadVariable
437
- std::string libName = " gz-rendering-" ;
447
+ auto registerStaticOrSolibPlugin = [this ](const std::string& _engineName,
448
+ const std::string& _staticFilename, const std::string& _solibFilename)
449
+ {
450
+ std::lock_guard<std::recursive_mutex> lock (this ->enginesMutex );
451
+ // Check static plugin registry if the plugin was statically linked in.
452
+ constexpr size_t prefixLen = kStaticPluginFilenamePrefix .size ();
453
+ const std::string staticPluginAlias = _staticFilename.substr (prefixLen);
454
+ if (!this ->pluginLoader .PluginsWithAlias (staticPluginAlias).empty ())
455
+ {
456
+ this ->defaultEngines [_engineName] = _staticFilename;
457
+ if (this ->engines .find (_staticFilename) == this ->engines .end ())
458
+ this ->engines [_staticFilename] = nullptr ;
459
+ return ;
460
+ }
461
+ // Else if a .so lib plugin file is expected, register that as the default.
462
+ if (!_solibFilename.empty ())
463
+ {
464
+ this ->defaultEngines [_engineName] = _solibFilename;
465
+ if (this ->engines .find (_solibFilename) == this ->engines .end ())
466
+ this ->engines [_solibFilename] = nullptr ;
467
+ }
468
+ };
438
469
439
- // cppcheck-suppress unusedVariable
440
- std::string engineName;
470
+ // TODO(anyone): Find a cleaner way to get the default engine .so library name
471
+ // cppcheck-suppress unreadVariable
472
+ const std::string libNamePrefix = " gz-rendering-" ;
441
473
442
- std::lock_guard<std::recursive_mutex> lock (this ->enginesMutex );
474
+ // Register Ogre
475
+ const std::string ogreEngineName = " ogre" ;
476
+ const std::string ogreStaticFilename = " static://gz::rendering::ogre::Plugin" ;
443
477
#if GZ_RENDERING_HAVE_OGRE
444
- engineName = " ogre" ;
445
- this ->defaultEngines [engineName] = libName + engineName;
446
- if (this ->engines .find (libName + engineName) == this ->engines .end ())
447
- this ->engines [libName + engineName] = nullptr ;
478
+ registerStaticOrSolibPlugin (ogreEngineName, ogreStaticFilename,
479
+ libNamePrefix + ogreEngineName);
480
+ #else
481
+ registerStaticOrSolibPlugin (ogreEngineName, ogreStaticFilename,
482
+ /* _solibFilename=*/ " " );
448
483
#endif
484
+
485
+ // Register Ogre2
486
+ const std::string ogre2EngineName = " ogre2" ;
487
+ const std::string ogre2StaticFilename =
488
+ " static://gz::rendering::ogre2::Plugin" ;
449
489
#if GZ_RENDERING_HAVE_OGRE2
450
- engineName = " ogre2" ;
451
- this ->defaultEngines [engineName] = libName + engineName;
452
- if (this ->engines .find (libName + engineName) == this ->engines .end ())
453
- this ->engines [libName + engineName] = nullptr ;
490
+ registerStaticOrSolibPlugin (ogre2EngineName, ogre2StaticFilename,
491
+ libNamePrefix + ogre2EngineName);
492
+ #else
493
+ registerStaticOrSolibPlugin (ogre2EngineName, ogre2StaticFilename,
494
+ /* _solibFilename=*/ " " );
454
495
#endif
496
+
497
+ // Register Optix
498
+ const std::string optixEngineName = " optix" ;
499
+ const std::string optixStaticFilename =
500
+ " static://gz::rendering::optix::Plugin" ;
455
501
#if GZ_RENDERING_HAVE_OPTIX
456
- engineName = " optix" ;
457
- this ->defaultEngines [engineName] = libName + engineName;
458
- if (this ->engines .find (libName + engineName) == this ->engines .end ())
459
- this ->engines [libName + engineName] = nullptr ;
502
+ registerStaticOrSolibPlugin (optixEngineName, optixStaticFilename,
503
+ libNamePrefix + optixEngineName);
504
+ #else
505
+ registerStaticOrSolibPlugin (optixEngineName, optixStaticFilename,
506
+ /* _solibFilename=*/ " " );
460
507
#endif
461
508
}
462
509
@@ -466,6 +513,12 @@ bool RenderEngineManagerPrivate::LoadEnginePlugin(
466
513
{
467
514
gzmsg << " Loading plugin [" << _filename << " ]" << std::endl;
468
515
516
+ constexpr size_t prefixLen = kStaticPluginFilenamePrefix .size ();
517
+ if (_filename.substr (0 , prefixLen) == kStaticPluginFilenamePrefix )
518
+ {
519
+ return this ->LoadStaticEnginePlugin (_filename);
520
+ }
521
+
469
522
gz::common::SystemPaths systemPaths;
470
523
systemPaths.SetPluginPathEnv (this ->pluginPathEnv );
471
524
@@ -560,10 +613,52 @@ bool RenderEngineManagerPrivate::LoadEnginePlugin(
560
613
return true ;
561
614
}
562
615
616
+ // ////////////////////////////////////////////////
617
+ bool RenderEngineManagerPrivate::LoadStaticEnginePlugin (
618
+ const std::string &_filename)
619
+ {
620
+ constexpr size_t prefixLen = kStaticPluginFilenamePrefix .size ();
621
+ const std::string filenameWoPrefix = _filename.substr (prefixLen);
622
+
623
+ gzmsg << " Loading plugin [" << filenameWoPrefix << " ] from static registry"
624
+ << std::endl;
625
+
626
+ auto plugin = this ->pluginLoader .Instantiate (filenameWoPrefix);
627
+ if (!plugin)
628
+ {
629
+ gzerr << " Failed to load static render engine plugin [" << filenameWoPrefix
630
+ << " ]. Static plugin registry does not contain this plugin."
631
+ << std::endl;
632
+ return false ;
633
+ }
634
+
635
+ auto renderPlugin = plugin->QueryInterface <rendering::RenderEnginePlugin>();
636
+ if (!renderPlugin)
637
+ {
638
+ gzerr << " Failed to find RenderEnginePlugin interface in static render "
639
+ << " engine plugin [" << _filename << " ]" << std::endl;
640
+ return false ;
641
+ }
642
+
643
+ // Instantiate the engine
644
+ {
645
+ std::lock_guard<std::recursive_mutex> lock (this ->enginesMutex );
646
+ this ->engines [_filename] = renderPlugin->Engine ();
647
+ }
648
+
649
+ return true ;
650
+ }
651
+
563
652
// ////////////////////////////////////////////////
564
653
bool RenderEngineManagerPrivate::UnloadEnginePlugin (
565
654
const std::string &_engineName)
566
655
{
656
+ const size_t prefix_len = kStaticPluginFilenamePrefix .length ();
657
+ if (_engineName.substr (0 , prefix_len) == kStaticPluginFilenamePrefix )
658
+ {
659
+ return true ;
660
+ }
661
+
567
662
auto it = this ->enginePlugins .find (_engineName);
568
663
if (it == this ->enginePlugins .end ())
569
664
{
0 commit comments