0027860: Visualization - clean up Transformation Persistence API
[occt.git] / src / OpenGl / OpenGl_Window.cxx
1 // Created on: 2011-09-20
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #include <OpenGl_GlCore12.hxx>
17
18 #include <OpenGl_Context.hxx>
19 #include <OpenGl_GraphicDriver.hxx>
20 #include <OpenGl_Window.hxx>
21
22 #include <Aspect_GraphicDeviceDefinitionError.hxx>
23 #include <Graphic3d_TransformUtils.hxx>
24 #include <TCollection_AsciiString.hxx>
25 #include <TCollection_ExtendedString.hxx>
26 #include <Graphic3d_GraphicDriver.hxx>
27
28 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Window,MMgt_TShared)
29
30 #if defined(HAVE_EGL)
31   #include <EGL/egl.h>
32 #endif
33
34
35 #if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
36
37 namespace
38 {
39
40 #if defined(HAVE_EGL)
41   //
42 #elif defined(_WIN32)
43
44   // WGL_ARB_pixel_format
45 #ifndef WGL_NUMBER_PIXEL_FORMATS_ARB
46   #define WGL_NUMBER_PIXEL_FORMATS_ARB            0x2000
47   #define WGL_DRAW_TO_WINDOW_ARB                  0x2001
48   #define WGL_DRAW_TO_BITMAP_ARB                  0x2002
49   #define WGL_ACCELERATION_ARB                    0x2003
50   #define WGL_NEED_PALETTE_ARB                    0x2004
51   #define WGL_NEED_SYSTEM_PALETTE_ARB             0x2005
52   #define WGL_SWAP_LAYER_BUFFERS_ARB              0x2006
53   #define WGL_SWAP_METHOD_ARB                     0x2007
54   #define WGL_NUMBER_OVERLAYS_ARB                 0x2008
55   #define WGL_NUMBER_UNDERLAYS_ARB                0x2009
56   #define WGL_TRANSPARENT_ARB                     0x200A
57   #define WGL_TRANSPARENT_RED_VALUE_ARB           0x2037
58   #define WGL_TRANSPARENT_GREEN_VALUE_ARB         0x2038
59   #define WGL_TRANSPARENT_BLUE_VALUE_ARB          0x2039
60   #define WGL_TRANSPARENT_ALPHA_VALUE_ARB         0x203A
61   #define WGL_TRANSPARENT_INDEX_VALUE_ARB         0x203B
62   #define WGL_SHARE_DEPTH_ARB                     0x200C
63   #define WGL_SHARE_STENCIL_ARB                   0x200D
64   #define WGL_SHARE_ACCUM_ARB                     0x200E
65   #define WGL_SUPPORT_GDI_ARB                     0x200F
66   #define WGL_SUPPORT_OPENGL_ARB                  0x2010
67   #define WGL_DOUBLE_BUFFER_ARB                   0x2011
68   #define WGL_STEREO_ARB                          0x2012
69   #define WGL_PIXEL_TYPE_ARB                      0x2013
70   #define WGL_COLOR_BITS_ARB                      0x2014
71   #define WGL_RED_BITS_ARB                        0x2015
72   #define WGL_RED_SHIFT_ARB                       0x2016
73   #define WGL_GREEN_BITS_ARB                      0x2017
74   #define WGL_GREEN_SHIFT_ARB                     0x2018
75   #define WGL_BLUE_BITS_ARB                       0x2019
76   #define WGL_BLUE_SHIFT_ARB                      0x201A
77   #define WGL_ALPHA_BITS_ARB                      0x201B
78   #define WGL_ALPHA_SHIFT_ARB                     0x201C
79   #define WGL_ACCUM_BITS_ARB                      0x201D
80   #define WGL_ACCUM_RED_BITS_ARB                  0x201E
81   #define WGL_ACCUM_GREEN_BITS_ARB                0x201F
82   #define WGL_ACCUM_BLUE_BITS_ARB                 0x2020
83   #define WGL_ACCUM_ALPHA_BITS_ARB                0x2021
84   #define WGL_DEPTH_BITS_ARB                      0x2022
85   #define WGL_STENCIL_BITS_ARB                    0x2023
86   #define WGL_AUX_BUFFERS_ARB                     0x2024
87
88   #define WGL_NO_ACCELERATION_ARB                 0x2025
89   #define WGL_GENERIC_ACCELERATION_ARB            0x2026
90   #define WGL_FULL_ACCELERATION_ARB               0x2027
91
92   #define WGL_SWAP_EXCHANGE_ARB                   0x2028
93   #define WGL_SWAP_COPY_ARB                       0x2029
94   #define WGL_SWAP_UNDEFINED_ARB                  0x202A
95
96   #define WGL_TYPE_RGBA_ARB                       0x202B
97   #define WGL_TYPE_COLORINDEX_ARB                 0x202C
98 #endif // WGL_NUMBER_PIXEL_FORMATS_ARB
99
100   // WGL_ARB_create_context_profile
101 #ifndef WGL_CONTEXT_MAJOR_VERSION_ARB
102   #define WGL_CONTEXT_MAJOR_VERSION_ARB           0x2091
103   #define WGL_CONTEXT_MINOR_VERSION_ARB           0x2092
104   #define WGL_CONTEXT_LAYER_PLANE_ARB             0x2093
105   #define WGL_CONTEXT_FLAGS_ARB                   0x2094
106   #define WGL_CONTEXT_PROFILE_MASK_ARB            0x9126
107
108   // WGL_CONTEXT_FLAGS bits
109   #define WGL_CONTEXT_DEBUG_BIT_ARB               0x0001
110   #define WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB  0x0002
111
112   // WGL_CONTEXT_PROFILE_MASK_ARB bits
113   #define WGL_CONTEXT_CORE_PROFILE_BIT_ARB          0x00000001
114   #define WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB 0x00000002
115 #endif // WGL_CONTEXT_MAJOR_VERSION_ARB
116
117   static LRESULT CALLBACK wndProcDummy (HWND theWin, UINT theMsg, WPARAM theParamW, LPARAM theParamL)
118   {
119     return DefWindowProcW (theWin, theMsg, theParamW, theParamL);
120   }
121 #else
122
123   // GLX_ARB_create_context
124 #ifndef GLX_CONTEXT_MAJOR_VERSION_ARB
125   #define GLX_CONTEXT_DEBUG_BIT_ARB         0x00000001
126   #define GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB 0x00000002
127   #define GLX_CONTEXT_MAJOR_VERSION_ARB     0x2091
128   #define GLX_CONTEXT_MINOR_VERSION_ARB     0x2092
129   #define GLX_CONTEXT_FLAGS_ARB             0x2094
130
131   // GLX_ARB_create_context_profile
132   #define GLX_CONTEXT_CORE_PROFILE_BIT_ARB  0x00000001
133   #define GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB 0x00000002
134   #define GLX_CONTEXT_PROFILE_MASK_ARB      0x9126
135 #endif
136
137   //! Dummy XError handler which just skips errors
138   static int xErrorDummyHandler (Display*     /*theDisplay*/,
139                                  XErrorEvent* /*theErrorEvent*/)
140   {
141     return 0;
142   }
143
144   //! Auxiliary method to format list.
145   static void addMsgToList (TCollection_ExtendedString&       theList,
146                             const TCollection_ExtendedString& theMsg)
147   {
148     if (!theList.IsEmpty())
149     {
150       theList += ", ";
151     }
152     theList += theMsg;
153   }
154 #endif
155
156 }
157
158 // =======================================================================
159 // function : OpenGl_Window
160 // purpose  :
161 // =======================================================================
162 OpenGl_Window::OpenGl_Window (const Handle(OpenGl_GraphicDriver)& theDriver,
163                               const Handle(Aspect_Window)&  thePlatformWindow,
164                               Aspect_RenderingContext       theGContext,
165                               const Handle(OpenGl_Caps)&    theCaps,
166                               const Handle(OpenGl_Context)& theShareCtx)
167 : myGlContext (new OpenGl_Context (theCaps)),
168   myOwnGContext (theGContext == 0),
169   myPlatformWindow (thePlatformWindow),
170   mySwapInterval (theCaps->swapInterval)
171 {
172   myPlatformWindow->Size (myWidth, myHeight);
173
174   Standard_Boolean isCoreProfile = Standard_False;
175
176 #if defined(HAVE_EGL)
177   EGLDisplay anEglDisplay = (EGLDisplay )theDriver->getRawGlDisplay();
178   EGLContext anEglContext = (EGLContext )theDriver->getRawGlContext();
179   EGLConfig  anEglConfig  = (EGLConfig  )theDriver->getRawGlConfig();
180   if (anEglDisplay == EGL_NO_DISPLAY
181    || anEglContext == EGL_NO_CONTEXT
182    || anEglConfig == NULL)
183   {
184     Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window, EGL does not provide compatible configurations!");
185     return;
186   }
187
188   EGLSurface anEglSurf = EGL_NO_SURFACE;
189   if (theGContext == (EGLContext )EGL_NO_CONTEXT)
190   {
191     // create new surface
192     anEglSurf = eglCreateWindowSurface (anEglDisplay,
193                                         anEglConfig,
194                                         (EGLNativeWindowType )myPlatformWindow->NativeHandle(),
195                                         NULL);
196     if (anEglSurf == EGL_NO_SURFACE)
197     {
198       Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window, EGL is unable to create surface for window!");
199       return;
200     }
201   }
202   else if (theGContext != anEglContext)
203   {
204     Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window, EGL is used in unsupported combination!");
205     return;
206   }
207   else
208   {
209     anEglSurf = eglGetCurrentSurface(EGL_DRAW);
210     if (anEglSurf == EGL_NO_SURFACE)
211     {
212       Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window, EGL is unable to retrieve current surface!");
213       return;
214     }
215   }
216
217   myGlContext->Init ((Aspect_Drawable )anEglSurf, (Aspect_Display )anEglDisplay, (Aspect_RenderingContext )anEglContext, isCoreProfile);
218 #elif defined(_WIN32)
219   (void )theDriver;
220   HWND  aWindow   = (HWND )myPlatformWindow->NativeHandle();
221   HDC   aWindowDC = GetDC (aWindow);
222   HGLRC aGContext = (HGLRC )theGContext;
223
224   PIXELFORMATDESCRIPTOR aPixelFrmt;
225   memset (&aPixelFrmt, 0, sizeof(aPixelFrmt));
226   aPixelFrmt.nSize        = sizeof(PIXELFORMATDESCRIPTOR);
227   aPixelFrmt.nVersion     = 1;
228   aPixelFrmt.dwFlags      = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
229   aPixelFrmt.iPixelType   = PFD_TYPE_RGBA;
230   aPixelFrmt.cColorBits   = 24;
231   aPixelFrmt.cDepthBits   = 24;
232   aPixelFrmt.cStencilBits = 8;
233   aPixelFrmt.iLayerType   = PFD_MAIN_PLANE;
234   if (theCaps->contextStereo)
235   {
236     aPixelFrmt.dwFlags |= PFD_STEREO;
237   }
238
239   int aPixelFrmtId = ChoosePixelFormat (aWindowDC, &aPixelFrmt);
240
241   // in case of failure try without stereo if any
242   const Standard_Boolean hasStereo = aPixelFrmtId != 0 && theCaps->contextStereo;
243   if (aPixelFrmtId == 0 && theCaps->contextStereo)
244   {
245     TCollection_ExtendedString aMsg ("OpenGl_Window::CreateWindow: "
246                                      "ChoosePixelFormat is unable to find stereo supported pixel format. "
247                                      "Choosing similar non stereo format.");
248     myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
249                               GL_DEBUG_TYPE_OTHER,
250                               0, GL_DEBUG_SEVERITY_HIGH, aMsg);
251
252     aPixelFrmt.dwFlags &= ~PFD_STEREO;
253     aPixelFrmtId = ChoosePixelFormat (aWindowDC, &aPixelFrmt);
254   }
255
256   if (aPixelFrmtId == 0)
257   {
258     ReleaseDC (aWindow, aWindowDC);
259
260     TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: ChoosePixelFormat failed. Error code: ");
261     aMsg += (int )GetLastError();
262     Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
263     return;
264   }
265
266   DescribePixelFormat (aWindowDC, aPixelFrmtId, sizeof(aPixelFrmt), &aPixelFrmt);
267
268   HGLRC aSlaveCtx = !theShareCtx.IsNull() ? (HGLRC )theShareCtx->myGContext : NULL;
269   if (aGContext == NULL)
270   {
271     // create temporary context to retrieve advanced context creation procedures
272     HMODULE aModule = GetModuleHandleW(NULL);
273     WNDCLASSW aClass; memset (&aClass, 0, sizeof(aClass));
274     aClass.style         = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
275     aClass.lpfnWndProc   = wndProcDummy;
276     aClass.hInstance     = aModule;
277     aClass.lpszClassName = L"OpenGl_WindowTmp";
278     HWND  aWinTmp     = NULL;
279     HDC   aDevCtxTmp  = NULL;
280     HGLRC aRendCtxTmp = NULL;
281     if ((!theCaps->contextDebug && !theCaps->contextNoAccel && theCaps->contextCompatible)
282      || RegisterClassW (&aClass) == 0)
283     {
284       aClass.lpszClassName = NULL;
285     }
286     if (aClass.lpszClassName != NULL)
287     {
288       DWORD anExStyle = WS_EX_TOOLWINDOW | WS_EX_WINDOWEDGE;
289     #if (_WIN32_WINNT >= 0x0500)
290       anExStyle |= WS_EX_NOACTIVATE;
291     #endif
292       aWinTmp = CreateWindowExW(anExStyle,
293                                 aClass.lpszClassName, L"OpenGl_WindowTmp",
294                                 WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_DISABLED,
295                                 2, 2, 4, 4,
296                                 NULL, NULL, aModule, NULL);
297     }
298     if (aWinTmp != NULL)
299     {
300       aDevCtxTmp = GetDC (aWinTmp);
301       SetPixelFormat (aDevCtxTmp, aPixelFrmtId, &aPixelFrmt);
302       aRendCtxTmp = wglCreateContext (aDevCtxTmp);
303     }
304
305     typedef BOOL (WINAPI *wglChoosePixelFormatARB_t)(HDC           theDevCtx,
306                                                      const int*    theIntAttribs,
307                                                      const float*  theFloatAttribs,
308                                                      unsigned int  theMaxFormats,
309                                                      int*          theFormatsOut,
310                                                      unsigned int* theNumFormatsOut);
311     typedef HGLRC (WINAPI *wglCreateContextAttribsARB_t)(HDC        theDevCtx,
312                                                          HGLRC      theShareContext,
313                                                          const int* theAttribs);
314     wglChoosePixelFormatARB_t    aChoosePixProc = NULL;
315     wglCreateContextAttribsARB_t aCreateCtxProc = NULL;
316     if (aRendCtxTmp != NULL)
317     {
318       wglMakeCurrent (aDevCtxTmp, aRendCtxTmp);
319
320       typedef const char* (WINAPI *wglGetExtensionsStringARB_t)(HDC theDeviceContext);
321       wglGetExtensionsStringARB_t aGetExtensions = (wglGetExtensionsStringARB_t  )wglGetProcAddress ("wglGetExtensionsStringARB");
322       const char* aWglExts = (aGetExtensions != NULL) ? aGetExtensions (wglGetCurrentDC()) : NULL;
323       if (OpenGl_Context::CheckExtension (aWglExts, "WGL_ARB_pixel_format"))
324       {
325         aChoosePixProc = (wglChoosePixelFormatARB_t    )wglGetProcAddress ("wglChoosePixelFormatARB");
326       }
327       if (OpenGl_Context::CheckExtension (aWglExts, "WGL_ARB_create_context_profile"))
328       {
329         aCreateCtxProc = (wglCreateContextAttribsARB_t )wglGetProcAddress ("wglCreateContextAttribsARB");
330       }
331     }
332
333     // choose extended pixel format
334     if (aChoosePixProc != NULL)
335     {
336       const int aPixAttribs[] =
337       {
338         WGL_DRAW_TO_WINDOW_ARB, GL_TRUE,
339         WGL_SUPPORT_OPENGL_ARB, GL_TRUE,
340         WGL_DOUBLE_BUFFER_ARB,  GL_TRUE,
341         WGL_STEREO_ARB,         hasStereo ? GL_TRUE : GL_FALSE,
342         WGL_PIXEL_TYPE_ARB,     WGL_TYPE_RGBA_ARB,
343         //WGL_SAMPLE_BUFFERS_ARB, 1,
344         //WGL_SAMPLES_ARB,        8,
345         WGL_COLOR_BITS_ARB,     24,
346         WGL_DEPTH_BITS_ARB,     24,
347         WGL_STENCIL_BITS_ARB,   8,
348         WGL_ACCELERATION_ARB,   theCaps->contextNoAccel ? WGL_NO_ACCELERATION_ARB : WGL_FULL_ACCELERATION_ARB,
349         0, 0,
350       };
351       unsigned int aFrmtsNb = 0;
352       aChoosePixProc (aWindowDC, aPixAttribs, NULL, 1, &aPixelFrmtId, &aFrmtsNb);
353     }
354
355     // setup pixel format - may be set only once per window
356     if (!SetPixelFormat (aWindowDC, aPixelFrmtId, &aPixelFrmt))
357     {
358       ReleaseDC (aWindow, aWindowDC);
359
360       TCollection_AsciiString aMsg("OpenGl_Window::CreateWindow: SetPixelFormat failed. Error code: ");
361       aMsg += (int )GetLastError();
362       Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
363       return;
364     }
365
366     // create GL context with extra options
367     if (aCreateCtxProc != NULL)
368     {
369       if (!theCaps->contextCompatible)
370       {
371         int aCoreCtxAttribs[] =
372         {
373           WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
374           WGL_CONTEXT_MINOR_VERSION_ARB, 2,
375           WGL_CONTEXT_PROFILE_MASK_ARB,  WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
376           WGL_CONTEXT_FLAGS_ARB,         theCaps->contextDebug ? WGL_CONTEXT_DEBUG_BIT_ARB : 0,
377           0, 0
378         };
379
380         // Try to create the core profile of highest OpenGL version supported by OCCT
381         // (this will be done automatically by some drivers when requesting 3.2,
382         //  but some will not (e.g. AMD Catalyst) since WGL_ARB_create_context_profile specification allows both implementations).
383         for (int aLowVer4 = 5; aLowVer4 >= 0 && aGContext == NULL; --aLowVer4)
384         {
385           aCoreCtxAttribs[1] = 4;
386           aCoreCtxAttribs[3] = aLowVer4;
387           aGContext = aCreateCtxProc (aWindowDC, aSlaveCtx, aCoreCtxAttribs);
388         }
389         for (int aLowVer3 = 3; aLowVer3 >= 2 && aGContext == NULL; --aLowVer3)
390         {
391           aCoreCtxAttribs[1] = 3;
392           aCoreCtxAttribs[3] = aLowVer3;
393           aGContext = aCreateCtxProc (aWindowDC, aSlaveCtx, aCoreCtxAttribs);
394         }
395         isCoreProfile = aGContext != NULL;
396       }
397
398       if (aGContext == NULL)
399       {
400         int aCtxAttribs[] =
401         {
402           // Beware! NVIDIA drivers reject context creation when WGL_CONTEXT_PROFILE_MASK_ARB are specified
403           // but not WGL_CONTEXT_MAJOR_VERSION_ARB/WGL_CONTEXT_MINOR_VERSION_ARB.
404           //WGL_CONTEXT_PROFILE_MASK_ARB,  WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB,
405           WGL_CONTEXT_FLAGS_ARB,         theCaps->contextDebug ? WGL_CONTEXT_DEBUG_BIT_ARB : 0,
406           0, 0
407         };
408         isCoreProfile = Standard_False;
409         aGContext = aCreateCtxProc (aWindowDC, aSlaveCtx, aCtxAttribs);
410
411         if (aGContext != NULL
412         && !theCaps->contextCompatible)
413         {
414           TCollection_ExtendedString aMsg("OpenGl_Window::CreateWindow: core profile creation failed.");
415           myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_PORTABILITY, 0, GL_DEBUG_SEVERITY_LOW, aMsg);
416         }
417       }
418
419       if (aGContext != NULL)
420       {
421         aSlaveCtx = NULL;
422       }
423     }
424
425     if (aRendCtxTmp != NULL)
426     {
427       wglDeleteContext (aRendCtxTmp);
428     }
429     if (aDevCtxTmp != NULL)
430     {
431       ReleaseDC (aWinTmp, aDevCtxTmp);
432     }
433     if (aWinTmp != NULL)
434     {
435       DestroyWindow (aWinTmp);
436     }
437     if (aClass.lpszClassName != NULL)
438     {
439       UnregisterClassW (aClass.lpszClassName, aModule);
440     }
441
442     if (aGContext == NULL)
443     {
444       // create context using obsolete functionality
445       aGContext = wglCreateContext (aWindowDC);
446     }
447     if (aGContext == NULL)
448     {
449       ReleaseDC (aWindow, aWindowDC);
450
451       TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: wglCreateContext failed. Error code: ");
452       aMsg += (int )GetLastError();
453       Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
454       return;
455     }
456   }
457
458   // all GL context within one OpenGl_GraphicDriver should be shared!
459   if (aSlaveCtx != NULL && wglShareLists (aSlaveCtx, aGContext) != TRUE)
460   {
461     TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: wglShareLists failed. Error code: ");
462     aMsg += (int )GetLastError();
463     Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
464     return;
465   }
466
467   myGlContext->Init ((Aspect_Handle )aWindow, (Aspect_Handle )aWindowDC, (Aspect_RenderingContext )aGContext, isCoreProfile);
468 #else
469   Window     aWindow   = (Window )myPlatformWindow->NativeHandle();
470   Display*   aDisp     = theDriver->GetDisplayConnection()->GetDisplay();
471   GLXContext aGContext = (GLXContext )theGContext;
472   GLXContext aSlaveCtx = !theShareCtx.IsNull() ? (GLXContext )theShareCtx->myGContext : NULL;
473
474   XWindowAttributes aWinAttribs;
475   XGetWindowAttributes (aDisp, aWindow, &aWinAttribs);
476   XVisualInfo aVisInfo;
477   aVisInfo.visualid = aWinAttribs.visual->visualid;
478   aVisInfo.screen   = DefaultScreen (aDisp);
479   int aNbItems;
480   XVisualInfo* aVis = XGetVisualInfo (aDisp, VisualIDMask | VisualScreenMask, &aVisInfo, &aNbItems);
481   int isGl = 0;
482   if (aVis == NULL)
483   {
484     Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window::CreateWindow: XGetVisualInfo is unable to choose needed configuration in existing OpenGL context. ");
485     return;
486   }
487   else if (glXGetConfig (aDisp, aVis, GLX_USE_GL, &isGl) != 0 || !isGl)
488   {
489     Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window::CreateWindow: window Visual does not support GL rendering!");
490     return;
491   }
492
493   // create new context
494   GLXFBConfig anFBConfig = myPlatformWindow->NativeFBConfig();
495   const char* aGlxExts   = glXQueryExtensionsString (aDisp, aVisInfo.screen);
496   if (myOwnGContext
497    && anFBConfig != NULL
498    && OpenGl_Context::CheckExtension (aGlxExts, "GLX_ARB_create_context_profile"))
499   {
500     // Replace default XError handler to ignore errors.
501     // Warning - this is global for all threads!
502     typedef int (*xerrorhandler_t)(Display* , XErrorEvent* );
503     xerrorhandler_t anOldHandler = XSetErrorHandler(xErrorDummyHandler);
504
505     typedef GLXContext (*glXCreateContextAttribsARB_t)(Display* dpy, GLXFBConfig config,
506                                                        GLXContext share_context, Bool direct,
507                                                        const int* attrib_list);
508     glXCreateContextAttribsARB_t aCreateCtxProc = (glXCreateContextAttribsARB_t )glXGetProcAddress((const GLubyte* )"glXCreateContextAttribsARB");
509     if (!theCaps->contextCompatible)
510     {
511       int aCoreCtxAttribs[] =
512       {
513         GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
514         GLX_CONTEXT_MINOR_VERSION_ARB, 2,
515         GLX_CONTEXT_PROFILE_MASK_ARB,  GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
516         GLX_CONTEXT_FLAGS_ARB,         theCaps->contextDebug ? GLX_CONTEXT_DEBUG_BIT_ARB : 0,
517         0, 0
518       };
519
520       // try to create the core profile of highest OpenGL version supported by OCCT
521       for (int aLowVer4 = 5; aLowVer4 >= 0 && aGContext == NULL; --aLowVer4)
522       {
523         aCoreCtxAttribs[1] = 4;
524         aCoreCtxAttribs[3] = aLowVer4;
525         aGContext = aCreateCtxProc (aDisp, anFBConfig, aSlaveCtx, True, aCoreCtxAttribs);
526       }
527       for (int aLowVer3 = 3; aLowVer3 >= 2 && aGContext == NULL; --aLowVer3)
528       {
529         aCoreCtxAttribs[1] = 3;
530         aCoreCtxAttribs[3] = aLowVer3;
531         aGContext = aCreateCtxProc (aDisp, anFBConfig, aSlaveCtx, True, aCoreCtxAttribs);
532       }
533       isCoreProfile = aGContext != NULL;
534     }
535
536     if (aGContext == NULL)
537     {
538       int aCtxAttribs[] =
539       {
540         GLX_CONTEXT_FLAGS_ARB, theCaps->contextDebug ? GLX_CONTEXT_DEBUG_BIT_ARB : 0,
541         0, 0
542       };
543       isCoreProfile = Standard_False;
544       aGContext = aCreateCtxProc (aDisp, anFBConfig, aSlaveCtx, True, aCtxAttribs);
545
546       if (aGContext != NULL
547       && !theCaps->contextCompatible)
548       {
549         TCollection_ExtendedString aMsg("OpenGl_Window::CreateWindow: core profile creation failed.");
550         myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_PORTABILITY, 0, GL_DEBUG_SEVERITY_LOW, aMsg);
551       }
552     }
553     XSetErrorHandler(anOldHandler);
554   }
555
556   if (myOwnGContext
557    && aGContext == NULL)
558   {
559     aGContext = glXCreateContext (aDisp, aVis, aSlaveCtx, GL_TRUE);
560     if (aGContext == NULL)
561     {
562       Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window::CreateWindow: glXCreateContext failed.");
563       return;
564     }
565   }
566
567   // check Visual for OpenGl context's parameters compatibility
568   TCollection_ExtendedString aList;
569   int isDoubleBuffer = 0, isRGBA = 0, isStereo = 0;
570   int aDepthSize = 0, aStencilSize = 0;
571   glXGetConfig (aDisp, aVis, GLX_RGBA,         &isRGBA);
572   glXGetConfig (aDisp, aVis, GLX_DOUBLEBUFFER, &isDoubleBuffer);
573   glXGetConfig (aDisp, aVis, GLX_STEREO,       &isStereo);
574   glXGetConfig (aDisp, aVis, GLX_DEPTH_SIZE,   &aDepthSize);
575   glXGetConfig (aDisp, aVis, GLX_STENCIL_SIZE, &aStencilSize);
576   if (aDepthSize < 1)      addMsgToList (aList, "no depth buffer");
577   if (aStencilSize < 1)    addMsgToList (aList, "no stencil buffer");
578   if (isRGBA == 0)         addMsgToList (aList, "no RGBA color buffer");
579   if (isDoubleBuffer == 0) addMsgToList (aList, "no Double Buffer");
580   if (theCaps->contextStereo && isStereo == 0)
581   {
582     addMsgToList (aList, "no Quad Buffer");
583   }
584   else if (!theCaps->contextStereo && isStereo == 1)
585   {
586     addMsgToList (aList, "extra Quad Buffer");
587   }
588   if (!aList.IsEmpty())
589   {
590     TCollection_ExtendedString aMsg = TCollection_ExtendedString ("OpenGl_Window::CreateWindow: window Visual is incomplete: ") + aList;
591     myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
592                               GL_DEBUG_TYPE_OTHER,
593                               0, GL_DEBUG_SEVERITY_MEDIUM, aMsg);
594   }
595
596   myGlContext->Init ((Aspect_Drawable )aWindow, (Aspect_Display )aDisp, (Aspect_RenderingContext )aGContext, isCoreProfile);
597 #endif
598   myGlContext->Share (theShareCtx);
599   myGlContext->SetSwapInterval (mySwapInterval);
600   Init();
601 }
602
603 // =======================================================================
604 // function : ~OpenGl_Window
605 // purpose  :
606 // =======================================================================
607 OpenGl_Window::~OpenGl_Window()
608 {
609   if (!myOwnGContext
610    ||  myGlContext.IsNull())
611   {
612     myGlContext.Nullify();
613     return;
614   }
615
616   // release "GL" context if it is owned by window
617   // Mesa implementation can fail to destroy GL context if it set for current thread.
618   // It should be safer to unset thread GL context before its destruction.
619 #if defined(HAVE_EGL)
620   if ((EGLSurface )myGlContext->myWindow != EGL_NO_SURFACE)
621   {
622     eglDestroySurface ((EGLDisplay )myGlContext->myDisplay,
623                        (EGLSurface )myGlContext->myWindow);
624   }
625 #elif defined(_WIN32)
626   HWND  aWindow          = (HWND  )myGlContext->myWindow;
627   HDC   aWindowDC        = (HDC   )myGlContext->myWindowDC;
628   HGLRC aWindowGContext  = (HGLRC )myGlContext->myGContext;
629   HGLRC aThreadGContext  = wglGetCurrentContext();
630   myGlContext.Nullify();
631
632   if (aThreadGContext != NULL)
633   {
634     if (aThreadGContext == aWindowGContext)
635     {
636       wglMakeCurrent (NULL, NULL);
637     }
638
639     wglDeleteContext (aWindowGContext);
640   }
641   ReleaseDC (aWindow, aWindowDC);
642 #else
643   Display*    aDisplay        = (Display*    )myGlContext->myDisplay;
644   GLXContext  aWindowGContext = (GLXContext  )myGlContext->myGContext;
645   GLXContext  aThreadGContext = glXGetCurrentContext();
646   myGlContext.Nullify();
647
648   if (aDisplay != NULL)
649   {
650     if (aThreadGContext == aWindowGContext)
651     {
652       glXMakeCurrent (aDisplay, None, NULL);
653     }
654
655     // FSXXX sync necessary if non-direct rendering
656     glXWaitGL();
657     glXDestroyContext (aDisplay, aWindowGContext);
658   }
659 #endif
660 }
661
662 #endif // !__APPLE__
663
664 // =======================================================================
665 // function : Activate
666 // purpose  :
667 // =======================================================================
668 Standard_Boolean OpenGl_Window::Activate()
669 {
670   return myGlContext->MakeCurrent();
671 }
672
673 #if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
674
675 // =======================================================================
676 // function : Resize
677 // purpose  : call_subr_resize
678 // =======================================================================
679 void OpenGl_Window::Resize()
680 {
681 #if !defined(_WIN32) && !defined(HAVE_EGL)
682   Display* aDisp = (Display* )myGlContext->myDisplay;
683   if (aDisp == NULL)
684     return;
685 #endif
686
687   Standard_Integer aWidth  = 0;
688   Standard_Integer aHeight = 0;
689   myPlatformWindow->Size (aWidth, aHeight);
690
691   // If the size is not changed - do nothing
692   if ((myWidth == aWidth) && (myHeight == aHeight))
693     return;
694
695   myWidth  = aWidth;
696   myHeight = aHeight;
697
698 #if !defined(_WIN32) && !defined(HAVE_EGL)
699   XResizeWindow (aDisp, myGlContext->myWindow, (unsigned int )myWidth, (unsigned int )myHeight);
700   XSync (aDisp, False);
701 #endif
702
703   Init();
704 }
705
706 // =======================================================================
707 // function : Init
708 // purpose  :
709 // =======================================================================
710 void OpenGl_Window::Init()
711 {
712   if (!Activate())
713     return;
714
715 #if defined(HAVE_EGL)
716   eglQuerySurface ((EGLDisplay )myGlContext->myDisplay, (EGLSurface )myGlContext->myWindow, EGL_WIDTH,  &myWidth);
717   eglQuerySurface ((EGLDisplay )myGlContext->myDisplay, (EGLSurface )myGlContext->myWindow, EGL_HEIGHT, &myHeight);
718 #elif defined(_WIN32)
719   //
720 #else
721   Window aRootWin;
722   int aDummy;
723   unsigned int aDummyU;
724   unsigned int aNewWidth  = 0;
725   unsigned int aNewHeight = 0;
726   Display* aDisp = (Display* )myGlContext->myDisplay;
727   XGetGeometry (aDisp, myGlContext->myWindow, &aRootWin, &aDummy, &aDummy, &aNewWidth, &aNewHeight, &aDummyU, &aDummyU);
728   myWidth  = aNewWidth;
729   myHeight = aNewHeight;
730 #endif
731
732   glDisable (GL_DITHER);
733   glDisable (GL_SCISSOR_TEST);
734   const Standard_Integer aViewport[4] = { 0, 0, myWidth, myHeight };
735   myGlContext->ResizeViewport (aViewport);
736 #if !defined(GL_ES_VERSION_2_0)
737   myGlContext->SetDrawBuffer (GL_BACK);
738   if (myGlContext->core11 != NULL)
739   {
740     glMatrixMode (GL_MODELVIEW);
741   }
742 #endif
743 }
744
745 // =======================================================================
746 // function : SetSwapInterval
747 // purpose  :
748 // =======================================================================
749 void OpenGl_Window::SetSwapInterval()
750 {
751   if (mySwapInterval != myGlContext->caps->swapInterval)
752   {
753     mySwapInterval = myGlContext->caps->swapInterval;
754     myGlContext->SetSwapInterval (mySwapInterval);
755   }
756 }
757
758 #endif // !__APPLE__