Fraxinus  2023.01.05-dev+develop.0da12
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
Shared OpenGL context.
vtkRenderWindowPtr getRenderWindow(QString uid, bool offScreenRendering=false)
vtkSmartPointer< class vtkRenderWindow > vtkRenderWindowPtr
SharedOpenGLContextPtr getSharedOpenGLContext() const
vtkSmartPointer< class vtkOpenGLRenderWindow > vtkOpenGLRenderWindowPtr
boost::shared_ptr< class SharedOpenGLContext > SharedOpenGLContextPtr
#define CX_LOG_ERROR
Definition: cxLogger.h:99
vtkRenderWindowPtr getSharedRenderWindow() const
bool renderWindowExists(QString uid)
#define CX_LOG_WARNING
Definition: cxLogger.h:98
Namespace for all CustusX production code.