summaryrefslogtreecommitdiff
path: root/cad/plugins/NanoVision-1/src/Interface/NXGraphicsManager.cpp
blob: dc8cd6913fb1af4150d90eed873fccf741bf59f0 (plain)
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
// Copyright 2008 Nanorex, Inc.  See LICENSE file for details.

#include <Nanorex/Interface/NXGraphicsManager.h>
#include <Nanorex/Utility/NXStringTokenizer.h>

#include <QPluginLoader>

#include <sstream>

#ifdef NX_DEBUG
#define NX_DEBUG_FAIL  assert(0);
#else
#define NX_DEBUG_FAIL
#endif


using namespace std;

namespace Nanorex {

/* CONSTRUCTOR */
NXGraphicsManager::NXGraphicsManager()
	: properties(),
	renderingEngine(NULL),
	renderStyleRendererPluginTable(),
	defaultRenderer(NULL),
	renderStyleNameTable(),
	renderStyleFileNameTable()
{
	reset();
}

/* DESTRUCTOR */
NXGraphicsManager::~NXGraphicsManager()
{
}

// .............................................................................

void NXGraphicsManager::reset(void)
{
	properties.clear();
	if(renderingEngine != (NXRenderingEngine*)NULL) {
		delete renderingEngine;
		renderingEngine = (NXRenderingEngine*) NULL;
	}
	renderStyleRendererPluginTable.clear();
	if(defaultRenderer != (NXRendererPlugin*) NULL) {
		delete defaultRenderer;
		defaultRenderer = (NXRendererPlugin*) NULL;
	}
	renderStyleNameTable.clear();
	renderStyleFileNameTable.clear();
}

// .............................................................................

bool NXGraphicsManager::loadPlugins(NXProperties *const props)
{
	reset();
	bool success = true;
	success = loadRenderingEngine(props);
	if(success)
		success = loadRendererPlugins(props);
	if(success) {
		detectDefaultRenderer(props);
		printDiagnosticLogs();
	}
	else
		NX_DEBUG_FAIL;
	return success;
}

// .............................................................................

void NXGraphicsManager::printDiagnosticLogs(void)
{
	/// @todo
}

// .............................................................................

/// Called by findAndLoadPlugin() to load a rendering-engine from an existing file.
/// Returns false if qobject_cast() failed indicating that plugin does not
/// match the expected interface. If successful, then the pointer referenced
/// by pluginStore is updated to point to the loaded instance.
/// This function is called to load both the rendering-engine and renderer-plugins
bool NXGraphicsManager::loadPlugin(NXRenderingEngine **pluginStore,
                                   QFileInfo const& fileInfo)
{
	/// @todo log-messages
	QPluginLoader pluginLoader(fileInfo.absoluteFilePath());
	bool pluginLoaded = pluginLoader.load();
	if(!pluginLoaded)
		return false;
	QObject *pluginObject = pluginLoader.instance();
	if(pluginObject != (QObject*) 0)
		*pluginStore = qobject_cast<NXRenderingEngine*>(pluginObject);
	else 
		*pluginStore = (NXRenderingEngine*) 0;
	
	bool success = ((*pluginStore) != (NXRenderingEngine*)0);
	return success;
}

// .............................................................................

/// Called by findAndLoadPlugin() to load a renderer-plugin from an existing file.
/// Returns false if qobject_cast() failed indicating that plugin does not
/// match the expected interface or if loaded plugin was incompatible with the
/// rendering-engine. If successful, then the pointer referenced by pluginStore
/// is updated to point to the loaded instance. This function is called to load
/// both the rendering-engine and renderer-plugins
bool NXGraphicsManager::loadPlugin(NXRendererPlugin **pluginStore,
                                   QFileInfo const& fileInfo)
{
	QPluginLoader pluginLoader(fileInfo.absoluteFilePath());
	bool pluginLoaded = pluginLoader.load();
	if(!pluginLoaded)
		return false;
	QObject *pluginObject = pluginLoader.instance();
	
	/// @fixme Uncomment and commit after testing builds with NXRenderingEngine
	/// Qt-plugin modifications go through
	string pluginFileName = qPrintable(fileInfo.fileName());
	if(pluginObject != (QObject*) 0) {
		NXLOG_INFO("NXGraphicsManager",
				   "Plugin loaded: " + pluginFileName);
		*pluginStore = renderingEngine->renderer_cast(pluginObject);
		if(*pluginStore != (NXRendererPlugin*)0) {
			NXLOG_INFO("NXGraphicsManager",
					   "Plugin is compatible: " + pluginFileName);
		}
		else {
			NXLOG_SEVERE("NXGraphicsManager",
						 "Plugin is incompatible: " + pluginFileName);
		}
	}
	else  {
		*pluginStore = (NXRendererPlugin*) 0;
		NXLOG_SEVERE("NXGraphicsManager",
					 "Failed to load plugin: " + pluginFileName);
	}
	
	bool success = ((*pluginStore) != (NXRendererPlugin*)0);
	return success;
}

// .............................................................................

/// Locates and loads a plugin given the basename of the file and an initial
/// path to examine. If a load from the initial path is unsuccessful or if the
/// path provided is empty, then it retrieves the plugins-search-path
/// from application-settings and searches it in order for the first file it
/// could successfully find and load. If an engine could be successfully loaded
/// then it returns true and the plugin-pointer referenced by pluginStore is
/// updated to point to the loaded instance. cleanPath then holds the absolute
/// directory specification and absPath holds the full path to the library file.
template<typename PluginType>
	bool NXGraphicsManager::findAndLoadPlugin(string const& baseName,
	                                          string const& path,
	                                          string const& pluginsSearchPath,
	                                          PluginType **pluginStore,
	                                          QDir *cleanPath,
	                                          string *absPath)
{
#if defined(WIN32)
	string const fileExt = ".dll";
#elif defined(__APPLE__)
	string const fileExt = ".dylib";
#else
	string const fileExt = ".so";
#endif
	
	string const fileName = baseName + fileExt;
	bool pluginFoundAndLoaded = false;
	
	if(!path.empty()) {
		// user-defined specific path for this plugin - search this first
		*cleanPath = QDir(path.c_str()).absolutePath();
		QFileInfo fileInfo(cleanPath->absoluteFilePath(fileName.c_str()));
		*absPath = qPrintable(fileInfo.absoluteFilePath());
		bool const pluginFound = fileInfo.exists();
		bool pluginLoaded = false;
		if(pluginFound)
			pluginLoaded = loadPlugin(pluginStore, fileInfo);
		pluginFoundAndLoaded = pluginFound && pluginLoaded;
		if(!pluginFoundAndLoaded) {
			string const pluginNotFoundMsg = "Could not find renderer-plugin " +
				fileName + " in specific path " +
				qPrintable(cleanPath->absolutePath()) +
				" - examining plugins-search-path from application-settings";
			NXLOG_WARNING("NXGraphicsManager", pluginNotFoundMsg);
		}
	}
	
	if(!pluginFoundAndLoaded) {
		// either plugin was not found in given path or no specific path was
		// provided for the plugin - search in plugin-paths list
		NXStringTokenizer tokenizer(pluginsSearchPath, ";");
		while(!pluginFoundAndLoaded && tokenizer.hasMoreTokens()) {
			string const token = tokenizer.getNextToken();
			*cleanPath = QDir(token.c_str()).absolutePath();
			QFileInfo fileInfo(cleanPath->absoluteFilePath(fileName.c_str()));
			*absPath = qPrintable(fileInfo.absoluteFilePath());
			bool const pluginFound = fileInfo.exists();
			bool pluginLoaded = false;
			if(pluginFound)
				pluginLoaded = loadPlugin(pluginStore, fileInfo);
			pluginFoundAndLoaded = pluginFound && pluginLoaded;
		}
		if(!pluginFoundAndLoaded) {
			string const pluginNotFoundMsg = "Could not find renderer-plugin " +
				fileName + " in paths " + pluginsSearchPath;
			NXLOG_SEVERE("NXGraphicsManager", pluginNotFoundMsg);
		}
	}
	
	return pluginFoundAndLoaded;
}

// .............................................................................

/// Loads the rendering-engine from application settings
bool NXGraphicsManager::loadRenderingEngine(NXProperties *const props)
{
	/// @fixme What if pluginsSearchPath is empty?
	
	string baseName = props->getProperty("RenderingEngine.plugin");
	string filePath = props->getProperty("RenderingEngine.pluginPath");
	string const pluginsSearchPath = props->getProperty("PluginsSearchPath");
	
	QDir cleanPath;
	string absPath;
	bool renderingEngineLoaded = findAndLoadPlugin(baseName, filePath,
	                                               pluginsSearchPath,
	                                               &renderingEngine,
	                                               &cleanPath, &absPath);
	
	if(renderingEngineLoaded) {
		properties.setProperty("RenderingEngine.plugin",
		                       qPrintable(cleanPath.absolutePath()));
		string const msg = "Rendering-engine loaded from file " + absPath;
		NXLOG_INFO("NXGraphicsManager", msg);
	}
	
	return renderingEngineLoaded;
}

// .............................................................................

/// Pre-condition: rendering-engine should have loaded successfully
bool NXGraphicsManager::loadRendererPlugins(NXProperties *const props)
{
	string const pluginsSearchPath = props->getProperty("PluginsSearchPath");
	
	int pluginNum = 0; // counter
	bool success = true;
	
	NXLOG_INFO("NXGraphicsManager", "Loading renderer-plugins");
	
	while (true) {
		ostringstream pluginKeyStream;
		pluginKeyStream << "RenderStyle." << pluginNum;
		++pluginNum;
		
		string const pluginKey = pluginKeyStream.str();
		string const pluginCode = props->getProperty(pluginKey + ".code");
		string pluginBaseName = props->getProperty(pluginKey + ".plugin");
		string const pluginPath = props->getProperty(pluginKey + ".pluginPath");
		string pluginName = props->getProperty(pluginKey + ".name");
		if(pluginName.empty())
			pluginName = pluginCode;
		
		NXLOG_DEBUG("NXGraphicsManager", "Parsing " + pluginKey);
		
		if (pluginCode == "def") {
			NXLOG_WARNING("NXGraphicsManager",
			              "Render-style code cannot be 'def' (reserved "
			              "internally for default) - ignoring");
			continue;
		}

		if (pluginName != "")		
			NXLOG_INFO("NXGraphicsManager",
				   	   "Attempting to discover renderer-plugin for rendering-style: "
				   			+ pluginName);
		
		// if code and plugin filename are absent then there are no more plugins
		if (pluginCode == "" && pluginBaseName == "") {
			break;
		
		// else if only one of them is mentioned then error condition
		} else if (pluginCode=="" || pluginBaseName=="") {
			// error condition, both are required, only one is present
			string msg = "Both render-style code and plugin library filename "
				"must be provided for " + pluginKey + " (check app-settings file)";
			NXLOG_SEVERE("NXGraphicsManager", msg);
			// this could be a mistake with just this plugin-spec, continue
			// with next plugin - maybe that one is ok
			success = false;
			continue;
		}
		
		// both code and plugin file have values - try to load
		else {
			NXLOG_DEBUG("NXGraphicsManager", "\tcode = " + pluginCode);
			NXLOG_DEBUG("NXGraphicsManager", "\tplugin = " + pluginBaseName);
			NXLOG_DEBUG("NXGraphicsManager", "\tpluginPath = " + pluginPath);
			NXLOG_DEBUG("NXGraphicsManager", "\tname = " + pluginName);
			
			QDir cleanPath;
			string absPath;
			NXRendererPlugin *rendererPlugin = NULL;
			bool const rendererPluginLoaded =
				findAndLoadPlugin(pluginBaseName, pluginPath, pluginsSearchPath,
				                  &rendererPlugin, &cleanPath, &absPath);
			
			// if successful, record entries in tables
			if (rendererPluginLoaded) {
				// compatibility test
				properties.setProperty("Renderer."+pluginCode+".plugin",
				                       absPath);
				properties.setProperty("Renderer."+pluginCode+".name",
				                       pluginName);
				renderStyleRendererPluginTable[pluginCode] = rendererPlugin;
				renderStyleNameTable[pluginCode] = pluginName;
				renderStyleFileNameTable[pluginCode] = absPath;
				NXLOG_INFO("NXGraphicsManager",
						   "Renderer-plugin loaded: " + pluginName);
			}
			else {
				NXLOG_SEVERE("NXGraphicsManager",
				             "Failed to load renderer-plugin");
				success = false;
			}
		}
	}
	
	NXLOG_INFO("NXGraphicsManager", "Renderer-plugins loaded");
	return success;
}

// .............................................................................

void NXGraphicsManager::detectDefaultRenderer(NXProperties *const props)
{
	if(defaultRenderer != (NXRendererPlugin*) NULL)
		return;
	
	string const defaultRendererCode =
		props->getProperty("RenderStyle.default");
	
	if(!defaultRendererCode.empty()) {
		RenderStyleRendererPluginTable::iterator rendererFinder =
			renderStyleRendererPluginTable.find(defaultRendererCode);
		if(rendererFinder == renderStyleRendererPluginTable.end()) {
			defaultRenderer = renderStyleRendererPluginTable.begin()->second;
			NXLOG_WARNING("NXGraphicsManager",
			              "Invalid default render-style code - "
			              "assigning arbitrarily");
		}
		else {
			defaultRenderer = rendererFinder->second;
		}
	}
	else {
		if(!renderStyleRendererPluginTable.empty()) {
			defaultRenderer = renderStyleRendererPluginTable.begin()->second;
			NXLOG_WARNING("NXGraphicsManager",
			              "No default render-style code - assigning arbitrarily");
		}
		else {
			NXLOG_SEVERE("NXGraphicsManager", "Default renderer not specified -"
			             " and no renderers to assign arbitrarily as default");
		}
	}
}

// .............................................................................

vector<string> NXGraphicsManager::getRenderStyles(void)
{
	vector<string> renderStyles;
	RenderStyleRendererPluginTable::const_iterator renderStyleTableIter;
	for(renderStyleTableIter = renderStyleRendererPluginTable.begin();
	    renderStyleTableIter != renderStyleRendererPluginTable.end();
	    ++renderStyleTableIter)
	{
		renderStyles.push_back(renderStyleTableIter->first);
	}
	return renderStyles;
}

// .............................................................................

NXRendererPlugin*
	NXGraphicsManager::getRenderer(string const& renderStyleCode) const
{
	RenderStyleRendererPluginTable::const_iterator renderStyleCodeIter =
		renderStyleRendererPluginTable.find(renderStyleCode);
	if(renderStyleCodeIter == renderStyleRendererPluginTable.end())
		return (NXRendererPlugin*) NULL;
	else
		return renderStyleCodeIter->second;
}

// .............................................................................

string NXGraphicsManager::getRenderStyleName(string const& renderStyleCode)
{
	StringMap::const_iterator renderStyleCodeIter =
		renderStyleNameTable.find(renderStyleCode);
	if(renderStyleCodeIter == renderStyleNameTable.end())
		return string();
	else
		return renderStyleCodeIter->second;
}

// .............................................................................

/// Instantiates a new rendering-engine and sets up its plugins and initializes
/// the whole package. Returns a pointer the new engine which is then owned by
/// the caller.
NXRenderingEngine* NXGraphicsManager::newGraphicsInstance(QWidget *parent)
{
	NXRenderingEngine *newEngine = renderingEngine->newInstance(parent);
	if(newEngine == (NXRenderingEngine*) NULL) {
		NXLOG_SEVERE("NXGraphicsManager",
		             "Failed to create new graphics instance");
		return (NXRenderingEngine*) NULL;
	}
	
	RenderStyleRendererPluginTable::const_iterator renderStyleCodeIter;
	for(renderStyleCodeIter = renderStyleRendererPluginTable.begin();
	    renderStyleCodeIter != renderStyleRendererPluginTable.end();
	    ++renderStyleCodeIter)
	{
		string const& renderStyleCode = renderStyleCodeIter->first;
		assert(renderStyleCode != "def");
		string const& renderStyleName = renderStyleNameTable[renderStyleCode];
		NXRendererPlugin *plugin = renderStyleCodeIter->second;
		NXRendererPlugin *newPlugin = plugin->newInstance(newEngine);
		if(newPlugin == (NXRendererPlugin*)NULL) {
			NXLOG_WARNING("NXGraphicsManager", "Failed to create new instance "
			              "of plugin for " + renderStyleName + " render-style");
		}
		newEngine->setRenderer(renderStyleCode, newPlugin);
		
		if(defaultRenderer == plugin)
			newEngine->setRenderer("def", newPlugin);
	}
	
	bool pluginsInitialized = newEngine->initializePlugins();
	if(!pluginsInitialized) {
		NXLOG_SEVERE("NXGraphicsManager",
		             "Failed to initialize renderer-plugins in new rendering-"
		             "engine context");
	}
	
	return newEngine;
}


} // namespace Nanorex