bool Z3DNetworkEvaluator::initializeNetwork() { if (m_locked) { LDEBUG() << "locked."; return false; } lock(); bool failed = false; for (size_t i = 0; i < m_renderingOrder.size(); ++i) { Z3DProcessor* processor = m_renderingOrder[i]; if (!processor->isInitialized()) { try { getGLFocus(); processor->initialize(); processor->m_initialized = true; CHECK_GL_ERROR; } catch (const Exception& e) { LERROR() << "Failed to initialize processor" << processor->getClassName() << e.what(); // deinitialize processor, in order to make sure that all resources are freed LINFO() << "Deinitializing" << processor->getClassName() << "..."; getGLFocus(); processor->m_initialized = true; processor->deinitialize(); processor->m_initialized = false; // don't break, try to initialize the other processors even if one failed failed = true; } } } // update size sizeChangedFromProcessor(); for (size_t i=0; i<m_reverseSortedRenderProcessors.size(); i++) { QObject::disconnect(m_reverseSortedRenderProcessors[i], SIGNAL(requestUpstreamSizeChange(Z3DRenderProcessor*)), 0, 0); connect(m_reverseSortedRenderProcessors[i], SIGNAL(requestUpstreamSizeChange(Z3DRenderProcessor*)), this, SLOT(sizeChangedFromProcessor(Z3DRenderProcessor*))); } unlock(); CHECK_GL_ERROR; return !failed; }
void GLCanvas::initPainter() { if (painter_) { getGLFocus(); painter_->init(); painter_->sizeChanged(size_); } }
bool Z3DNetworkEvaluator::deinitializeNetwork() { if (m_locked) { LWARN() << "locked."; return false; } if (!m_canvasRenderer) { LWARN() << "no network."; return false; } lock(); bool failed = false; for (size_t i = 0; i < m_renderingOrder.size(); ++i) { Z3DProcessor* processor = m_renderingOrder[i]; if (processor->isInitialized()) { try { getGLFocus(); processor->deinitialize(); processor->m_initialized = false; CHECK_GL_ERROR; } catch (const Exception& e) { LERROR() << "Failed to deinitialize processor" << processor->getClassName() << e.what(); // don't break, try to deinitialize the other processors even if one failed failed = true; } } } unlock(); return !failed; }
void GLUTCanvas::update() { getGLFocus(); glutPostRedisplay(); }
void GLUTCanvas::repaint() { getGLFocus(); display(); }
QString Z3DNetworkEvaluator::process(bool stereo) { if (!m_canvasRenderer) return "No Network"; if (m_locked) { LDEBUG() << "locked. Scheduling."; m_processPending = true; return ""; } lock(); QString error; // notify process wrappers for (size_t j = 0; j < m_processWrappers.size(); ++j) m_processWrappers[j]->beforeNetworkProcess(); CHECK_GL_ERROR; // Iterate over processing in rendering order for (size_t i = 0; i < m_renderingOrder.size(); ++i) { Z3DProcessor* currentProcessor = m_renderingOrder[i]; // all processors should have been initialized at this point if (!currentProcessor->isInitialized()) { LWARN() << "Skipping uninitialized processor" << currentProcessor->getClassName(); continue; } Z3DEye eye = stereo ? LeftEye : CenterEye; // run the processor, if it needs processing and is ready if (!currentProcessor->isValid(eye) && currentProcessor->isReady(eye)) { // notify process wrappers for (size_t j=0; j < m_processWrappers.size(); ++j) m_processWrappers[j]->beforeProcess(currentProcessor); CHECK_GL_ERROR; try { getGLFocus(); currentProcessor->process(eye); currentProcessor->setValid(eye); CHECK_GL_ERROR; } catch (Exception& e) { LERROR() << "Exception from" << currentProcessor->getClassName() << ":" << e.what(); error += e.what(); break; } catch (std::exception& e) { LERROR() << "std exception from" << currentProcessor->getClassName() << ":" << e.what(); error += e.what(); break; } // notify process wrappers getGLFocus(); for (size_t j = 0; j < m_processWrappers.size(); ++j) m_processWrappers[j]->afterProcess(currentProcessor); CHECK_GL_ERROR; } if (stereo && !currentProcessor->isValid(RightEye) && currentProcessor->isReady(RightEye)) { // notify process wrappers for (size_t j=0; j < m_processWrappers.size(); ++j) m_processWrappers[j]->beforeProcess(currentProcessor); CHECK_GL_ERROR; try { getGLFocus(); currentProcessor->process(RightEye); currentProcessor->setValid(RightEye); CHECK_GL_ERROR; } catch (Exception& e) { LERROR() << "Exception from" << currentProcessor->getClassName() << ":" << e.what(); error += e.what(); break; } catch (std::exception& e) { LERROR() << "std exception from" << currentProcessor->getClassName() << ":" << e.what(); error += e.what(); break; } // notify process wrappers getGLFocus(); for (size_t j = 0; j < m_processWrappers.size(); ++j) m_processWrappers[j]->afterProcess(currentProcessor); CHECK_GL_ERROR; } } // notify process wrappers for (size_t j = 0; j < m_processWrappers.size(); ++j) m_processWrappers[j]->afterNetworkProcess(); CHECK_GL_ERROR; unlock(); // make sure that canvases are repainted, if their update has been blocked by the locked evaluator if (m_processPending) { m_processPending = false; m_canvasRenderer->invalidate(); } return error; }
void Z3DNetworkEvaluator::buildNetwork() { std::set<Z3DProcessor*> prevProcessors( m_renderingOrder.begin(), m_renderingOrder.end()); m_renderingOrder.clear(); m_processorToVertexMapper.clear(); m_processorGraph.clear(); m_reverseSortedRenderProcessors.clear(); // nothing more to do, if no network sink is present if (!m_canvasRenderer) return; std::set<Z3DProcessor*> processed; std::queue<Z3DProcessor*> processQueue; processQueue.push(m_canvasRenderer); Vertex v = boost::add_vertex(VertexInfo(m_canvasRenderer), m_processorGraph); m_processorToVertexMapper[m_canvasRenderer] = v; // build graph of all connected processors while (!processQueue.empty()) { Z3DProcessor *processor = processQueue.front(); const std::vector<Z3DInputPortBase*> inports = processor->getInputPorts(); for (size_t i = 0; i < inports.size(); ++i) { const std::vector<Z3DOutputPortBase*> connected = inports[i]->getConnected(); for (size_t j = 0; j < connected.size(); ++j) { Z3DProcessor *outProcessor = connected[j]->getProcessor(); if (m_processorToVertexMapper.find(outProcessor) == m_processorToVertexMapper.end()) { processQueue.push(outProcessor); Vertex v = boost::add_vertex(VertexInfo(outProcessor), m_processorGraph); m_processorToVertexMapper[outProcessor] = v; } boost::add_edge(m_processorToVertexMapper[outProcessor], m_processorToVertexMapper[processor], EdgeInfo(connected[j], inports[i]), m_processorGraph); } } processed.insert(processor); processQueue.pop(); } // sort to get rendering order std::vector<Vertex> sorted; boost::topological_sort(m_processorGraph, std::back_inserter(sorted)); for (std::vector<Vertex>::reverse_iterator rit = sorted.rbegin(); rit != sorted.rend(); rit++) { m_renderingOrder.push_back(m_processorGraph[*rit].processor); } #ifdef _DEBUG_ LINFO() << "Rendering Order: "; for (size_t i=0; i<m_renderingOrder.size(); i++) { LINFO() << " " << i << ": " << m_renderingOrder[i]->getClassName(); } LINFO() << ""; #endif // compare processors in network before and after updating, deinitialize removed processors std::set<Z3DProcessor*> currProcessors(m_renderingOrder.begin(), m_renderingOrder.end()); std::set<Z3DProcessor*> removedProcessors; std::set_difference(prevProcessors.begin(), prevProcessors.end(), currProcessors.begin(), currProcessors.end(), std::inserter(removedProcessors, removedProcessors.end())); if (!removedProcessors.empty()) { #ifdef _DEBUG_ LINFO() << "Deinitialized processors after updating: "; #endif for (std::set<Z3DProcessor*>::iterator it=removedProcessors.begin(); it != removedProcessors.end(); ++it) { #ifdef _DEBUG_ LINFO() << " " << " : " << (*it)->getClassName(); #endif try { getGLFocus(); (*it)->deinitialize(); (*it)->m_initialized = false; CHECK_GL_ERROR; } catch (const Exception& e) { LERROR() << "Failed to deinitialize processor" << (*it)->getClassName() << e.what(); } } #ifdef _DEBUG_ LINFO() << ""; #endif } // update reverse sorted renderprocessors for (std::vector<Z3DProcessor*>::reverse_iterator rit = m_renderingOrder.rbegin(); rit != m_renderingOrder.rend(); rit++) { if (qobject_cast<Z3DRenderProcessor*>(*rit) == 0) continue; m_reverseSortedRenderProcessors.push_back(qobject_cast<Z3DRenderProcessor*>(*rit)); } }