1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    vtkXOpenGLRenderWindow.h
5 
6   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7   All rights reserved.
8   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10      This software is distributed WITHOUT ANY WARRANTY; without even
11      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12      PURPOSE.  See the above copyright notice for more information.
13 
14 =========================================================================*/
15 /**
16  * @class   vtkXOpenGLRenderWindow
17  * @brief   OpenGL rendering window
18  *
19  * vtkXOpenGLRenderWindow is a concrete implementation of the abstract class
20  * vtkRenderWindow. vtkOpenGLRenderer interfaces to the OpenGL graphics
21  * library. Application programmers should normally use vtkRenderWindow
22  * instead of the OpenGL specific version.
23  */
24 
25 #ifndef vtkXOpenGLRenderWindow_h
26 #define vtkXOpenGLRenderWindow_h
27 
28 #include "vtkOpenGLRenderWindow.h"
29 #include "vtkRenderingOpenGL2Module.h" // For export macro
30 #include <X11/Xlib.h>                  // Needed for X types used in the public interface
31 #include <stack>                       // for ivar
32 
33 class vtkIdList;
34 class vtkXOpenGLRenderWindowInternal;
35 struct vtkXVisualInfo;
36 
37 class VTKRENDERINGOPENGL2_EXPORT vtkXOpenGLRenderWindow : public vtkOpenGLRenderWindow
38 {
39 public:
40   static vtkXOpenGLRenderWindow* New();
41   vtkTypeMacro(vtkXOpenGLRenderWindow, vtkOpenGLRenderWindow);
42   void PrintSelf(ostream& os, vtkIndent indent) override;
43 
44   /**
45    * Begin the rendering process.
46    */
47   void Start() override;
48 
49   /**
50    * End the rendering process and display the image.
51    */
52   void Frame() override;
53 
54   /**
55    * Initialize the window for rendering.
56    */
57   virtual void WindowInitialize();
58 
59   /**
60    * Initialize the rendering window.  This will setup all system-specific
61    * resources.  This method and Finalize() must be symmetric and it
62    * should be possible to call them multiple times, even changing WindowId
63    * in-between.  This is what WindowRemap does.
64    */
65   void Initialize() override;
66 
67   /**
68    * "Deinitialize" the rendering window.  This will shutdown all system-specific
69    * resources.  After having called this, it should be possible to destroy
70    * a window that was used for a SetWindowId() call without any ill effects.
71    */
72   void Finalize() override;
73 
74   /**
75    * Change the window to fill the entire screen.
76    */
77   void SetFullScreen(vtkTypeBool) override;
78 
79   /**
80    * Resize the window.
81    */
82   void WindowRemap() override;
83 
84   // Call X funcs to map unmap
85   void SetShowWindow(bool val) override;
86 
87   /**
88    * Set the preferred window size to full screen.
89    */
90   virtual void PrefFullScreen();
91 
92   /**
93    * Set the size (width and height) of the rendering window in
94    * screen coordinates (in pixels). This resizes the operating
95    * system's view/window and redraws it.
96    *
97    * If the size has changed, this method will fire
98    * vtkCommand::WindowResizeEvent.
99    */
100   void SetSize(int width, int height) override;
SetSize(int a[2])101   void SetSize(int a[2]) override { this->SetSize(a[0], a[1]); }
102 
103   /**
104    * Specify the size of the rendering window in pixels but do not resize
105    * the XWindow. Useful when resizing is done interactively.
106    */
107   void SetSizeNoXResize(int, int);
108 
109   ///@{
110   /**
111    * Get the X properties of an ideal rendering window.
112    */
113   virtual Colormap GetDesiredColormap();
114   virtual Visual* GetDesiredVisual();
115   virtual vtkXVisualInfo* GetDesiredVisualInfo();
116   virtual int GetDesiredDepth();
117   ///@}
118 
119   /**
120    * Prescribe that the window be created in a stereo-capable mode. This
121    * method must be called before the window is realized. This method
122    * overrides the superclass method since this class can actually check
123    * whether the window has been realized yet.
124    */
125   void SetStereoCapableWindow(vtkTypeBool capable) override;
126 
127   /**
128    * Make this window the current OpenGL context.
129    */
130   void MakeCurrent() override;
131 
132   /**
133    * Tells if this window is the current OpenGL context for the calling thread.
134    */
135   bool IsCurrent() override;
136 
137   /**
138    * Release the current context.
139    */
140   void ReleaseCurrent() override;
141 
142   /**
143    * If called, allow MakeCurrent() to skip cache-check when called.
144    * MakeCurrent() reverts to original behavior of cache-checking
145    * on the next render.
146    */
147   void SetForceMakeCurrent() override;
148 
149   /**
150    * Get report of capabilities for the render window
151    */
152   const char* ReportCapabilities() override;
153 
154   /**
155    * Is this render window using hardware acceleration? 0-false, 1-true
156    */
157   vtkTypeBool IsDirect() override;
158 
159   /**
160    * Xwindow get set functions
161    */
GetGenericDisplayId()162   void* GetGenericDisplayId() override { return this->GetDisplayId(); }
163 
164   void* GetGenericWindowId() override;
GetGenericParentId()165   void* GetGenericParentId() override { return reinterpret_cast<void*>(this->ParentId); }
166 
167   void* GetGenericContext() override;
GetGenericDrawable()168   void* GetGenericDrawable() override { return reinterpret_cast<void*>(this->WindowId); }
169 
170   /**
171    * Get the current size of the screen in pixels.
172    * An HDTV for example would be 1920 x 1080 pixels.
173    */
174   int* GetScreenSize() VTK_SIZEHINT(2) override;
175 
176   /**
177    * Get the position (x and y) of the rendering window in
178    * screen coordinates (in pixels).
179    */
180   int* GetPosition() VTK_SIZEHINT(2) override;
181 
182   /**
183    * Get this RenderWindow's X display id.
184    */
185   Display* GetDisplayId();
186 
187   ///@{
188   /**
189    * Set the X display id for this RenderWindow to use to a pre-existing
190    * X display id.
191    */
192   void SetDisplayId(Display*);
193   void SetDisplayId(void*) override;
194   ///@}
195 
196   /**
197    * Get this RenderWindow's parent X window id.
198    */
199   Window GetParentId();
200 
201   ///@{
202   /**
203    * Sets the parent of the window that WILL BE created.
204    */
205   void SetParentId(Window);
206   void SetParentId(void*) override;
207   ///@}
208 
209   /**
210    * Get this RenderWindow's X window id.
211    */
212   Window GetWindowId();
213 
214   ///@{
215   /**
216    * Set this RenderWindow's X window id to a pre-existing window.
217    */
218   void SetWindowId(Window);
219   void SetWindowId(void*) override;
220   ///@}
221 
222   /**
223    * Specify the X window id to use if a WindowRemap is done.
224    */
225   void SetNextWindowId(Window);
226 
227   /**
228    * Set the window id of the new window once a WindowRemap is done.
229    * This is the generic prototype as required by the vtkRenderWindow
230    * parent.
231    */
232   void SetNextWindowId(void*) override;
233 
234   /**
235    * Set name of rendering window.
236    */
237   void SetWindowName(const char*) override;
238 
239   /**
240    * For window manager that supports it, set the icon displayed
241    * in the taskbar and the title bar.
242    */
243   void SetIcon(vtkImageData* img) override;
244 
245   /**
246    * Initialize the render window from the information associated
247    * with the currently activated OpenGL context.
248    */
249   bool InitializeFromCurrentContext() override;
250 
251   /**
252    * Does this platform support render window data sharing.
253    */
GetPlatformSupportsRenderWindowSharing()254   bool GetPlatformSupportsRenderWindowSharing() override { return true; }
255 
256   ///@{
257   /**
258    * Set the position (x and y) of the rendering window in
259    * screen coordinates (in pixels). This resizes the operating
260    * system's view/window and redraws it.
261    */
262   void SetPosition(int x, int y) override;
SetPosition(int a[2])263   void SetPosition(int a[2]) override { this->SetPosition(a[0], a[1]); }
264   ///@}
265 
266   ///@{
267   /**
268    * Hide or Show the mouse cursor, it is nice to be able to hide the
269    * default cursor if you want VTK to display a 3D cursor instead.
270    */
271   void HideCursor() override;
272   void ShowCursor() override;
273   ///@}
274 
275   /**
276    * Change the shape of the cursor
277    */
278   void SetCurrentCursor(int) override;
279 
280   /**
281    * Check to see if a mouse button has been pressed or mouse wheel activated.
282    * All other events are ignored by this method.
283    * This is a useful check to abort a long render.
284    */
285   vtkTypeBool GetEventPending() override;
286 
287   /**
288    * Set this RenderWindow's X window id to a pre-existing window.
289    */
290   void SetWindowInfo(const char* info) override;
291 
292   /**
293    * Set the window info that will be used after WindowRemap()
294    */
295   void SetNextWindowInfo(const char* info) override;
296 
297   /**
298    * Sets the X window id of the window that WILL BE created.
299    */
300   void SetParentInfo(const char* info) override;
301 
302   /**
303    * This computes the size of the render window
304    * before calling the supper classes render
305    */
306   void Render() override;
307 
308   ///@{
309   /**
310    * Ability to push and pop this window's context
311    * as the current context. The idea being to
312    * if needed make this window's context current
313    * and when done releasing resources restore
314    * the prior context
315    */
316   void PushContext() override;
317   void PopContext() override;
318   ///@}
319 
320   /**
321    * Set the number of vertical syncs required between frames.
322    * A value of 0 means swap buffers as quickly as possible
323    * regardless of the vertical refresh. A value of 1 means swap
324    * buffers in sync with the vertical refresh to eliminate tearing.
325    * A value of -1 means use a value of 1 unless we missed a frame
326    * in which case swap immediately. Returns true if the call
327    * succeeded.
328    */
329   bool SetSwapControl(int i) override;
330 
331 protected:
332   vtkXOpenGLRenderWindow();
333   ~vtkXOpenGLRenderWindow() override;
334 
335   vtkXOpenGLRenderWindowInternal* Internal;
336 
337   Window ParentId;
338   Window WindowId;
339   Window NextWindowId;
340   Display* DisplayId;
341   Colormap ColorMap;
342   vtkTypeBool OwnWindow;
343   vtkTypeBool OwnDisplay;
344   vtkTypeBool CursorHidden;
345   vtkTypeBool ForceMakeCurrent;
346   vtkTypeBool UsingHardware;
347 
348   std::stack<Display*> DisplayStack;
349   std::stack<Drawable> DrawableStack;
350   std::stack<void*> ContextStack;
351 
352   // we must keep track of the cursors we are using
353   Cursor XCCrosshair;
354   Cursor XCArrow;
355   Cursor XCSizeAll;
356   Cursor XCSizeNS;
357   Cursor XCSizeWE;
358   Cursor XCSizeNE;
359   Cursor XCSizeNW;
360   Cursor XCSizeSE;
361   Cursor XCSizeSW;
362   Cursor XCHand;
363   Cursor XCCustom;
364 
365   void CreateAWindow() override;
366   void DestroyWindow() override;
367   void CloseDisplay();
368 
369 private:
370   vtkXOpenGLRenderWindow(const vtkXOpenGLRenderWindow&) = delete;
371   void operator=(const vtkXOpenGLRenderWindow&) = delete;
372 };
373 
374 #endif
375