NorMIT-nav  22.09
An IGT application
cxRenderWindowFactory.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
12 #include "cxRenderWindowFactory.h"
13 
14 #include <QString>
15 #include <vtkRenderWindow.h>
16 #include <vtkOpenGLRenderWindow.h>
17 #include "QVTKWidget.h"
18 #include "cxSharedOpenGLContext.h"
19 #include "cxLogger.h"
20 
21 namespace cx
22 {
23 bool RenderWindowFactory::mInstanceExisting = false;
24 
25 
27 {
28  if(mInstanceExisting)
29  CX_LOG_ERROR() << "There can only be one instance of RenderWindowFactory <--------";
30  else
31  mInstanceExisting = true;
32 
33  //Note: Setting offScreenRendering to true gives crash in render
34  vtkRenderWindowPtr renderWindow = createRenderWindow("cx_shared_context", false);
35  this->preventSharedContextRenderWindowFromBeingShownOnScreen(renderWindow);
36  renderWindow->Render();
37 }
38 
39 void RenderWindowFactory::preventSharedContextRenderWindowFromBeingShownOnScreen(vtkRenderWindowPtr renderWindow)
40 {
41  mQvtkWidgetForHidingSharedContextRenderWindow = new QVTKWidget();
42  mQvtkWidgetForHidingSharedContextRenderWindow->SetRenderWindow(renderWindow);
43 }
44 
45 vtkRenderWindowPtr RenderWindowFactory::getRenderWindow(QString uid, bool offScreenRendering)
46 {
47  if(this->renderWindowExists(uid))
48  {
49  return mRenderWindows[uid];
50  }
51 
52  vtkRenderWindowPtr renderWindow = this->createRenderWindow(uid, offScreenRendering);
53  return renderWindow;
54 }
55 
57 {
58  return (mRenderWindows.count(uid) > 0);
59 }
60 
62 {
63  if(!mSharedRenderWindow)
64  {
65  CX_LOG_WARNING() << "RenderWindowFactory::getSharedRenderWindow(): No shared opengl renderwindow available.";
66  }
67  return mSharedRenderWindow;
68 }
69 
70 void RenderWindowFactory::setSharedRenderWindow(vtkRenderWindowPtr sharedRenderWindow)
71 {
72  if(mSharedRenderWindow == sharedRenderWindow)
73  {
74  return;
75  }
76  mSharedRenderWindow = sharedRenderWindow;
77 
78  vtkOpenGLRenderWindowPtr opengl_renderwindow = vtkOpenGLRenderWindow::SafeDownCast(mSharedRenderWindow);
79  if(opengl_renderwindow)
80  {
81  mSharedOpenGLContext = SharedOpenGLContextPtr(new SharedOpenGLContext(opengl_renderwindow));
82  }
83  else
84  {
85  CX_LOG_WARNING() << "RenderWindowFactory::setSharedRenderWindow(): got no opengl render window.";
86  }
87 }
88 
90 {
91  return mSharedOpenGLContext;
92 }
93 
94 vtkRenderWindowPtr RenderWindowFactory::createRenderWindow(QString uid, bool offScreenRendering)
95 {
96  vtkRenderWindowPtr renderWindow = vtkRenderWindowPtr::New();
97  renderWindow->SetOffScreenRendering(offScreenRendering);
98 
99  mSharedContextCreatedCallback = SharedContextCreatedCallbackPtr::New();
100  mSharedContextCreatedCallback->setRenderWindowFactory(this);
101 
102  vtkOpenGLRenderWindowPtr opengl_renderwindow = vtkOpenGLRenderWindow::SafeDownCast(renderWindow);
103  if(!opengl_renderwindow)
104  {
105  CX_LOG_ERROR() << "The created renderwindow is not an opengl renderwindow.";
106  }
107  opengl_renderwindow->AddObserver(vtkCommand::CXSharedContextCreatedEvent, mSharedContextCreatedCallback);
108 
109  mRenderWindows[uid] = renderWindow;
110 
111  return renderWindow;
112 }
113 
114 }//cx
cxSharedOpenGLContext.h
cxLogger.h
vtkRenderWindowPtr
vtkSmartPointer< class vtkRenderWindow > vtkRenderWindowPtr
Definition: vtkForwardDeclarations.h:124
cx
Namespace for all CustusX production code.
Definition: cx_dev_group_definitions.h:13
vtkOpenGLRenderWindowPtr
vtkSmartPointer< class vtkOpenGLRenderWindow > vtkOpenGLRenderWindowPtr
Definition: vtkForwardDeclarations.h:101
cx::RenderWindowFactory::renderWindowExists
bool renderWindowExists(QString uid)
Definition: cxRenderWindowFactory.cpp:56
cx::RenderWindowFactory::RenderWindowFactory
RenderWindowFactory()
Definition: cxRenderWindowFactory.cpp:26
cx::RenderWindowFactory::getSharedRenderWindow
vtkRenderWindowPtr getSharedRenderWindow() const
Definition: cxRenderWindowFactory.cpp:61
cx::RenderWindowFactory::getRenderWindow
vtkRenderWindowPtr getRenderWindow(QString uid, bool offScreenRendering=false)
Definition: cxRenderWindowFactory.cpp:45
cxRenderWindowFactory.h
CX_LOG_ERROR
#define CX_LOG_ERROR
Definition: cxLogger.h:99
cx::RenderWindowFactory::getSharedOpenGLContext
SharedOpenGLContextPtr getSharedOpenGLContext() const
Definition: cxRenderWindowFactory.cpp:89
CX_LOG_WARNING
#define CX_LOG_WARNING
Definition: cxLogger.h:98
cx::SharedOpenGLContextPtr
boost::shared_ptr< class SharedOpenGLContext > SharedOpenGLContextPtr
Definition: cxForwardDeclarations.h:164