-
Notifications
You must be signed in to change notification settings - Fork 52
/
RenderEngineManager.cc
611 lines (516 loc) · 18.4 KB
/
RenderEngineManager.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
/*
* Copyright (C) 2015 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <map>
#include <mutex>
#include <gz/common/Console.hh>
#include <gz/common/SystemPaths.hh>
#include <gz/plugin/Loader.hh>
#include "gz/rendering/InstallationDirectories.hh"
#include "gz/rendering/RenderEngine.hh"
#include "gz/rendering/RenderEngineManager.hh"
#include "gz/rendering/RenderEnginePlugin.hh"
/// \brief Holds information about an engine
struct EngineInfo
{
/// \brief The name of the engine's shared library, default engines can also
/// be specified with their regular name (ogre, optix, etc.).
std::string name;
/// \brief The pointer to the render engine.
gz::rendering::RenderEngine *engine;
};
/// \brief Private implementation of the RenderEngineManager class.
class gz::rendering::RenderEngineManagerPrivate
{
/// \brief EngineMap that maps engine name to an engine pointer.
typedef std::map<std::string, RenderEngine *> EngineMap;
/// \brief EngineMap iterator.
typedef EngineMap::iterator EngineIter;
/// \brief Get a pointer to the render engine from an EngineMap iterator.
/// \param[in] _iter EngineMap iterator
/// \param[in] _path Another search path for rendering engine plugin.
public: RenderEngine *Engine(EngineInfo _info,
const std::map<std::string, std::string> &_params,
const std::string &_path);
/// \brief Unload the given render engine from an EngineMap iterator.
/// The engine will remain registered and can be loaded again later.
/// \param[in] _iter EngineMap iterator
/// \return True if the engine is unloaded
public: bool UnloadEngine(EngineIter _iter);
/// \brief Register default engines supplied by gz-rendering
public: void RegisterDefaultEngines();
/// \brief Unregister an engine using an EngineMap iterator.
/// Once an engine is unregistered, it can no longer be loaded. Use
/// RenderEngineManagerPrivate::UnloadEngine to unload the engine without
/// unregistering it if you wish to load the engine again
/// \param[in] _iter EngineMap iterator
public: void UnregisterEngine(EngineIter _iter);
/// \brief Load a render engine plugin.
/// \param[in] _filename Filename of plugin shared library
/// \param[in] _path Another search path for rendering engine plugin.
/// \return True if the plugin is loaded successfully
public: bool LoadEnginePlugin(const std::string &_filename,
const std::string &_path);
/// \brief Unload a render engine plugin.
/// \param[in] _engineName Name of engine associated with this plugin
/// \return True if the plugin is unloaded successfully
public: bool UnloadEnginePlugin(const std::string &_engineName);
/// \brief Engines that have been registered
public: EngineMap engines;
/// \brief A map of default engine library names to their plugin names.
public: std::map<std::string, std::string> defaultEngines;
/// \brief A map of loaded engine plugins to its plugin name.
public: std::map<std::string, std::string> enginePlugins;
/// \brief Plugin loader for managing render engine plugin libraries.
public: gz::plugin::Loader pluginLoader;
/// \brief Deprecated environment variable which holds paths to look for
/// plugins
public: std::string pluginPathEnvDeprecated = "IGN_RENDERING_PLUGIN_PATH";
/// \brief Environment variable which holds paths to look for plugins
public: std::string pluginPathEnv = "GZ_RENDERING_PLUGIN_PATH";
/// \brief List which holds paths to look for engine plugins.
public: std::list<std::string> pluginPaths;
/// \brief Mutex to protect the engines map.
public: std::recursive_mutex enginesMutex;
};
using namespace gz;
using namespace rendering;
//////////////////////////////////////////////////
// RenderEngineManager
//////////////////////////////////////////////////
RenderEngineManager::RenderEngineManager() :
dataPtr(new RenderEngineManagerPrivate)
{
this->dataPtr->RegisterDefaultEngines();
}
//////////////////////////////////////////////////
RenderEngineManager::~RenderEngineManager()
{
}
//////////////////////////////////////////////////
unsigned int RenderEngineManager::EngineCount() const
{
std::lock_guard<std::recursive_mutex> lock(this->dataPtr->enginesMutex);
return this->dataPtr->engines.size();
}
//////////////////////////////////////////////////
bool RenderEngineManager::HasEngine(const std::string &_name) const
{
std::lock_guard<std::recursive_mutex> lock(this->dataPtr->enginesMutex);
auto iter = this->dataPtr->engines.find(_name);
if (iter == this->dataPtr->engines.end())
{
// Check if the provided name is a name of a default engine, if so,
// translate the name to the shared library name
auto defaultIt = this->dataPtr->defaultEngines.find(_name);
if (defaultIt != this->dataPtr->defaultEngines.end())
iter = this->dataPtr->engines.find(defaultIt->second);
}
return iter != this->dataPtr->engines.end();
}
//////////////////////////////////////////////////
bool RenderEngineManager::IsEngineLoaded(const std::string &_name) const
{
std::lock_guard<std::recursive_mutex> lock(this->dataPtr->enginesMutex);
auto iter = this->dataPtr->engines.find(_name);
if (iter == this->dataPtr->engines.end())
{
// Check if the provided name is a name of a default engine, if so,
// translate the name to the shared library name
auto defaultIt = this->dataPtr->defaultEngines.find(_name);
if (defaultIt != this->dataPtr->defaultEngines.end())
{
iter = this->dataPtr->engines.find(defaultIt->second);
if (iter == this->dataPtr->engines.end())
return false;
}
else
return false;
}
return nullptr != iter->second;
}
//////////////////////////////////////////////////
std::vector<std::string> RenderEngineManager::LoadedEngines() const
{
std::lock_guard<std::recursive_mutex> lock(this->dataPtr->enginesMutex);
std::vector<std::string> engines;
for (auto [name, engine] : // NOLINT(whitespace/braces)
this->dataPtr->engines)
{
std::string n = name;
if (nullptr != engine)
{
// gz-rendering3 changed loaded engine names to the actual lib name.
// For backward compatibility, return engine name if it is one of the
// default engines
for (const auto &it : this->dataPtr->defaultEngines)
{
if (it.second == name)
{
n = it.first;
break;
}
}
engines.push_back(n);
}
}
return engines;
}
//////////////////////////////////////////////////
RenderEngine *RenderEngineManager::Engine(const std::string &_name,
const std::map<std::string, std::string> &_params,
const std::string &_path)
{
EngineInfo info{_name, nullptr};
std::lock_guard<std::recursive_mutex> lock(this->dataPtr->enginesMutex);
// check in the list of available engines
auto iter = this->dataPtr->engines.find(_name);
if (iter != this->dataPtr->engines.end())
{
info.name = iter->first;
info.engine = iter->second;
}
return this->dataPtr->Engine(info, _params, _path);
}
//////////////////////////////////////////////////
RenderEngine *RenderEngineManager::EngineAt(unsigned int _index,
const std::map<std::string, std::string> &_params,
const std::string &_path)
{
if (_index >= this->EngineCount())
{
gzerr << "Invalid render-engine index: " << _index << std::endl;
return nullptr;
}
std::lock_guard<std::recursive_mutex> lock(this->dataPtr->enginesMutex);
auto iter = this->dataPtr->engines.begin();
std::advance(iter, _index);
return this->dataPtr->Engine({iter->first, iter->second}, _params, _path);
}
//////////////////////////////////////////////////
bool RenderEngineManager::UnloadEngine(const std::string &_name)
{
std::lock_guard<std::recursive_mutex> lock(this->dataPtr->enginesMutex);
// check in the list of available engines
auto iter = this->dataPtr->engines.find(_name);
if (iter == this->dataPtr->engines.end())
{
// Check if the provided name is a name of a default engine, if so,
// translate the name to the shared library name
auto defaultIt = this->dataPtr->defaultEngines.find(_name);
if (defaultIt != this->dataPtr->defaultEngines.end())
iter = this->dataPtr->engines.find(defaultIt->second);
if (iter == this->dataPtr->engines.end())
{
gzerr << "No render-engine registered with name: " << _name << std::endl;
return false;
}
}
return this->dataPtr->UnloadEngine(iter);
}
//////////////////////////////////////////////////
bool RenderEngineManager::UnloadEngineAt(unsigned int _index)
{
if (_index >= this->EngineCount())
{
gzerr << "Invalid render-engine index: " << _index << std::endl;
return false;
}
std::lock_guard<std::recursive_mutex> lock(this->dataPtr->enginesMutex);
auto iter = this->dataPtr->engines.begin();
std::advance(iter, _index);
return this->dataPtr->UnloadEngine(iter);
}
//////////////////////////////////////////////////
void RenderEngineManager::RegisterEngine(const std::string &_name,
RenderEngine *_engine)
{
if (!_engine)
{
gzerr << "Render-engine cannot be null" << std::endl;
return;
}
if (this->HasEngine(_name))
{
gzerr << "Render-engine already registered with name: "
<< _name << std::endl;
return;
}
std::lock_guard<std::recursive_mutex> lock(this->dataPtr->enginesMutex);
this->dataPtr->engines[_name] = _engine;
}
//////////////////////////////////////////////////
void RenderEngineManager::UnregisterEngine(const std::string &_name)
{
std::lock_guard<std::recursive_mutex> lock(this->dataPtr->enginesMutex);
auto iter = this->dataPtr->engines.find(_name);
if (iter != this->dataPtr->engines.end())
{
this->dataPtr->UnregisterEngine(iter);
}
}
//////////////////////////////////////////////////
void RenderEngineManager::UnregisterEngine(RenderEngine *_engine)
{
if (!_engine)
return;
std::lock_guard<std::recursive_mutex> lock(this->dataPtr->enginesMutex);
auto begin = this->dataPtr->engines.begin();
auto end = this->dataPtr->engines.end();
for (auto iter = begin; iter != end; ++iter)
{
if (iter->second == _engine)
{
this->dataPtr->UnregisterEngine(iter);
return;
}
}
}
//////////////////////////////////////////////////
void RenderEngineManager::UnregisterEngineAt(unsigned int _index)
{
if (_index >= this->EngineCount())
{
gzerr << "Invalid render-engine index: " << _index << std::endl;
return;
}
std::lock_guard<std::recursive_mutex> lock(this->dataPtr->enginesMutex);
auto iter = this->dataPtr->engines.begin();
std::advance(iter, _index);
this->dataPtr->UnregisterEngine(iter);
}
//////////////////////////////////////////////////
void RenderEngineManager::SetPluginPaths(const std::list<std::string> &_paths)
{
this->dataPtr->pluginPaths = _paths;
}
//////////////////////////////////////////////////
RenderEngineManager *RenderEngineManager::Instance()
{
return gz::common::SingletonT<RenderEngineManager>::Instance();
}
//////////////////////////////////////////////////
// RenderEngineManagerPrivate
//////////////////////////////////////////////////
RenderEngine *RenderEngineManagerPrivate::Engine(EngineInfo _info,
const std::map<std::string, std::string> &_params,
const std::string &_path)
{
RenderEngine *engine = _info.engine;
if (!engine)
{
std::string libName = _info.name;
// Check if the provided name is a name of a default engine, if so,
// translate the name to the shared library name
auto defaultIt = this->defaultEngines.find(_info.name);
if (defaultIt != this->defaultEngines.end())
libName = defaultIt->second;
std::lock_guard<std::recursive_mutex> lock(this->enginesMutex);
// Check to see if we need to load the engine
auto engineIt = this->engines.find(libName);
// Engine is already loaded
if (engineIt != this->engines.end() && engineIt->second)
{
engine = engineIt->second;
}
// Load the engine
else if (this->LoadEnginePlugin(libName, _path))
{
engineIt = this->engines.find(libName);
if (engineIt != this->engines.end())
engine = engineIt->second;
}
}
if (!engine)
return nullptr;
if (!engine->IsInitialized())
{
engine->Load(_params);
engine->Init();
}
return engine;
}
//////////////////////////////////////////////////
bool RenderEngineManagerPrivate::UnloadEngine(EngineIter _iter)
{
RenderEngine *engine = _iter->second;
if (!engine)
return false;
engine->Destroy();
return this->UnloadEnginePlugin(_iter->first);
}
//////////////////////////////////////////////////
void RenderEngineManagerPrivate::RegisterDefaultEngines()
{
// TODO(anyone): Find a cleaner way to get the default engine library name
// cppcheck-suppress unreadVariable
std::string libName = "gz-rendering-";
// cppcheck-suppress unusedVariable
std::string engineName;
std::lock_guard<std::recursive_mutex> lock(this->enginesMutex);
#if GZ_RENDERING_HAVE_OGRE
engineName = "ogre";
this->defaultEngines[engineName] = libName + engineName;
if (this->engines.find(libName + engineName) == this->engines.end())
this->engines[libName + engineName] = nullptr;
#endif
#if GZ_RENDERING_HAVE_OGRE2
engineName = "ogre2";
this->defaultEngines[engineName] = libName + engineName;
if (this->engines.find(libName + engineName) == this->engines.end())
this->engines[libName + engineName] = nullptr;
#endif
#if GZ_RENDERING_HAVE_OPTIX
engineName = "optix";
this->defaultEngines[engineName] = libName + engineName;
if (this->engines.find(libName + engineName) == this->engines.end())
this->engines[libName + engineName] = nullptr;
#endif
}
//////////////////////////////////////////////////
bool RenderEngineManagerPrivate::LoadEnginePlugin(
const std::string &_filename, const std::string &_path)
{
gzmsg << "Loading plugin [" << _filename << "]" << std::endl;
gz::common::SystemPaths systemPaths;
systemPaths.SetPluginPathEnv(this->pluginPathEnv);
// Add default install folder.
systemPaths.AddPluginPaths(std::string(GZ_RENDERING_PLUGIN_PATH));
systemPaths.AddPluginPaths(gz::rendering::getEngineInstallDir());
// Add any preset plugin paths.
for (const auto &path : this->pluginPaths)
systemPaths.AddPluginPaths(path);
// Add extra search path.
systemPaths.AddPluginPaths(_path);
auto pathToLib = systemPaths.FindSharedLibrary(_filename);
if (pathToLib.empty())
{
// Try deprecated environment variable
common::SystemPaths systemPathsDep;
systemPathsDep.SetPluginPathEnv(this->pluginPathEnvDeprecated);
pathToLib = systemPathsDep.FindSharedLibrary(_filename);
if (pathToLib.empty())
{
gzerr << "Failed to load plugin [" << _filename <<
"] : couldn't find shared library." << std::endl;
return false;
}
else
{
gzwarn << "Found plugin [" << _filename
<< "] using deprecated environment variable ["
<< this->pluginPathEnvDeprecated << "]. Please use ["
<< this->pluginPathEnv << "] instead." << std::endl;
}
}
// Load plugin
auto pluginNames = this->pluginLoader.LoadLib(pathToLib);
if (pluginNames.empty())
{
gzerr << "Failed to load plugin [" << _filename <<
"] : couldn't load library on path [" << pathToLib <<
"]." << std::endl;
return false;
}
auto engineNames = pluginLoader.PluginsImplementing<
rendering::RenderEnginePlugin>();
if (engineNames.empty())
{
std::stringstream error;
error << "Found no render engine plugins in ["
<< _filename << "], available interfaces are:"
<< std::endl;
for (auto pluginName : pluginNames)
{
error << "- " << pluginName << std::endl;
}
gzerr << error.str();
return false;
}
auto engineName = *engineNames.begin();
if (engineNames.size() > 1)
{
std::stringstream warn;
warn << "Found multiple render engine plugins in ["
<< _filename << "]:"
<< std::endl;
for (auto pluginName : engineNames)
{
warn << "- " << pluginName << std::endl;
}
warn << "Loading [" << engineName << "]." << std::endl;
gzwarn << warn.str();
}
auto plugin = pluginLoader.Instantiate(engineName);
if (!plugin)
{
gzerr << "Failed to instantiate plugin [" << engineName << "]"
<< std::endl;
return false;
}
auto renderPlugin =
plugin->QueryInterface<rendering::RenderEnginePlugin>();
if (!renderPlugin)
{
gzerr << "Failed to query interface from [" << engineName << "]"
<< std::endl;
return false;
}
// This triggers the engine to be instantiated
{
std::lock_guard<std::recursive_mutex> lock(this->enginesMutex);
this->engines[_filename] = renderPlugin->Engine();
}
// store engine plugin data so plugin can be unloaded later
this->enginePlugins[_filename] = engineName;
return true;
}
//////////////////////////////////////////////////
bool RenderEngineManagerPrivate::UnloadEnginePlugin(
const std::string &_engineName)
{
auto it = this->enginePlugins.find(_engineName);
if (it == this->enginePlugins.end())
{
gzmsg << "Skip unloading engine plugin. [" << _engineName << "] "
<< "not loaded from plugin." << std::endl;
return false;
}
std::string pluginName = it->second;
this->enginePlugins.erase(it);
#ifndef _WIN32
// Unloading the plugin on windows causes tests to crash on exit
// see issue #45
if (!this->pluginLoader.ForgetLibraryOfPlugin(pluginName))
{
gzerr << "Failed to unload plugin: " << pluginName << std::endl;
}
#endif
std::lock_guard<std::recursive_mutex> lock(this->enginesMutex);
auto engineIt = this->engines.find(_engineName);
if (engineIt == this->engines.end())
return false;
// set to null - this means engine is still registered but not loaded
this->engines[_engineName] = nullptr;
return true;
}
//////////////////////////////////////////////////
void RenderEngineManagerPrivate::UnregisterEngine(EngineIter _iter)
{
_iter->second->Destroy();
std::lock_guard<std::recursive_mutex> lock(this->enginesMutex);
this->engines.erase(_iter);
}