b7fb9db8ec327eb5ee2ae6f185b7ae2e4911963c
[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 #include <InterfaceGraphic.hxx>
18
19 #include <OpenGl_Context.hxx>
20 #include <OpenGl_GraphicDriver.hxx>
21 #include <OpenGl_Window.hxx>
22 #include <OpenGl_Utils.hxx>
23
24 #include <Aspect_GraphicDeviceDefinitionError.hxx>
25 #include <TCollection_AsciiString.hxx>
26 #include <TCollection_ExtendedString.hxx>
27
28 #if defined(HAVE_EGL) || defined(__ANDROID__)
29   #include <EGL/egl.h>
30 #endif
31
32 IMPLEMENT_STANDARD_HANDLE(OpenGl_Window,MMgt_TShared)
33 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Window,MMgt_TShared)
34
35 #if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
36
37 namespace
38 {
39   static const TEL_COLOUR THE_DEFAULT_BG_COLOR = { { 0.F, 0.F, 0.F, 1.F } };
40
41 #if defined(HAVE_EGL) || defined(__ANDROID__)
42   //
43 #elif defined(_WIN32)
44
45   // WGL_ARB_pixel_format
46 #ifndef WGL_NUMBER_PIXEL_FORMATS_ARB
47   #define WGL_NUMBER_PIXEL_FORMATS_ARB            0x2000
48   #define WGL_DRAW_TO_WINDOW_ARB                  0x2001
49   #define WGL_DRAW_TO_BITMAP_ARB                  0x2002
50   #define WGL_ACCELERATION_ARB                    0x2003
51   #define WGL_NEED_PALETTE_ARB                    0x2004
52   #define WGL_NEED_SYSTEM_PALETTE_ARB             0x2005
53   #define WGL_SWAP_LAYER_BUFFERS_ARB              0x2006
54   #define WGL_SWAP_METHOD_ARB                     0x2007
55   #define WGL_NUMBER_OVERLAYS_ARB                 0x2008
56   #define WGL_NUMBER_UNDERLAYS_ARB                0x2009
57   #define WGL_TRANSPARENT_ARB                     0x200A
58   #define WGL_TRANSPARENT_RED_VALUE_ARB           0x2037
59   #define WGL_TRANSPARENT_GREEN_VALUE_ARB         0x2038
60   #define WGL_TRANSPARENT_BLUE_VALUE_ARB          0x2039
61   #define WGL_TRANSPARENT_ALPHA_VALUE_ARB         0x203A
62   #define WGL_TRANSPARENT_INDEX_VALUE_ARB         0x203B
63   #define WGL_SHARE_DEPTH_ARB                     0x200C
64   #define WGL_SHARE_STENCIL_ARB                   0x200D
65   #define WGL_SHARE_ACCUM_ARB                     0x200E
66   #define WGL_SUPPORT_GDI_ARB                     0x200F
67   #define WGL_SUPPORT_OPENGL_ARB                  0x2010
68   #define WGL_DOUBLE_BUFFER_ARB                   0x2011
69   #define WGL_STEREO_ARB                          0x2012
70   #define WGL_PIXEL_TYPE_ARB                      0x2013
71   #define WGL_COLOR_BITS_ARB                      0x2014
72   #define WGL_RED_BITS_ARB                        0x2015
73   #define WGL_RED_SHIFT_ARB                       0x2016
74   #define WGL_GREEN_BITS_ARB                      0x2017
75   #define WGL_GREEN_SHIFT_ARB                     0x2018
76   #define WGL_BLUE_BITS_ARB                       0x2019
77   #define WGL_BLUE_SHIFT_ARB                      0x201A
78   #define WGL_ALPHA_BITS_ARB                      0x201B
79   #define WGL_ALPHA_SHIFT_ARB                     0x201C
80   #define WGL_ACCUM_BITS_ARB                      0x201D
81   #define WGL_ACCUM_RED_BITS_ARB                  0x201E
82   #define WGL_ACCUM_GREEN_BITS_ARB                0x201F
83   #define WGL_ACCUM_BLUE_BITS_ARB                 0x2020
84   #define WGL_ACCUM_ALPHA_BITS_ARB                0x2021
85   #define WGL_DEPTH_BITS_ARB                      0x2022
86   #define WGL_STENCIL_BITS_ARB                    0x2023
87   #define WGL_AUX_BUFFERS_ARB                     0x2024
88
89   #define WGL_NO_ACCELERATION_ARB                 0x2025
90   #define WGL_GENERIC_ACCELERATION_ARB            0x2026
91   #define WGL_FULL_ACCELERATION_ARB               0x2027
92
93   #define WGL_SWAP_EXCHANGE_ARB                   0x2028
94   #define WGL_SWAP_COPY_ARB                       0x2029
95   #define WGL_SWAP_UNDEFINED_ARB                  0x202A
96
97   #define WGL_TYPE_RGBA_ARB                       0x202B
98   #define WGL_TYPE_COLORINDEX_ARB                 0x202C
99 #endif // WGL_NUMBER_PIXEL_FORMATS_ARB
100
101   // WGL_ARB_create_context_profile
102 #ifndef WGL_CONTEXT_MAJOR_VERSION_ARB
103   #define WGL_CONTEXT_MAJOR_VERSION_ARB           0x2091
104   #define WGL_CONTEXT_MINOR_VERSION_ARB           0x2092
105   #define WGL_CONTEXT_LAYER_PLANE_ARB             0x2093
106   #define WGL_CONTEXT_FLAGS_ARB                   0x2094
107   #define WGL_CONTEXT_PROFILE_MASK_ARB            0x9126
108
109   // WGL_CONTEXT_FLAGS bits
110   #define WGL_CONTEXT_DEBUG_BIT_ARB               0x0001
111   #define WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB  0x0002
112
113   // WGL_CONTEXT_PROFILE_MASK_ARB bits
114   #define WGL_CONTEXT_CORE_PROFILE_BIT_ARB          0x00000001
115   #define WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB 0x00000002
116 #endif // WGL_CONTEXT_MAJOR_VERSION_ARB
117
118   static LRESULT CALLBACK wndProcDummy (HWND theWin, UINT theMsg, WPARAM theParamW, LPARAM theParamL)
119   {
120     return DefWindowProcW (theWin, theMsg, theParamW, theParamL);
121   }
122 #else
123   static Bool WaitForNotify (Display* theDisp, XEvent* theEv, char* theArg)
124   {
125     return (theEv->type == MapNotify) && (theEv->xmap.window == (Window )theArg);
126   }
127 #endif
128
129 }
130
131 // =======================================================================
132 // function : OpenGl_Window
133 // purpose  :
134 // =======================================================================
135 OpenGl_Window::OpenGl_Window (const Handle(OpenGl_GraphicDriver)& theDriver,
136                               const CALL_DEF_WINDOW&        theCWindow,
137                               Aspect_RenderingContext       theGContext,
138                               const Handle(OpenGl_Caps)&    theCaps,
139                               const Handle(OpenGl_Context)& theShareCtx)
140 : myGlContext (new OpenGl_Context (theCaps)),
141   myOwnGContext (theGContext == 0),
142   myWidth   (theCWindow.dx),
143   myHeight  (theCWindow.dy),
144   myBgColor (THE_DEFAULT_BG_COLOR)
145 {
146   myBgColor.rgb[0] = theCWindow.Background.r;
147   myBgColor.rgb[1] = theCWindow.Background.g;
148   myBgColor.rgb[2] = theCWindow.Background.b;
149
150 #if defined(HAVE_EGL) || defined(__ANDROID__)
151   EGLDisplay anEglDisplay = (EGLDisplay )theDriver->getRawGlDisplay();
152   EGLContext anEglContext = (EGLContext )theDriver->getRawGlContext();
153   EGLConfig  anEglConfig  = (EGLConfig  )theDriver->getRawGlConfig();
154   if (anEglDisplay == EGL_NO_DISPLAY
155    || anEglContext == EGL_NO_CONTEXT
156    || anEglConfig == NULL)
157   {
158     Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window, EGL does not provide compatible configurations!");
159     return;
160   }
161
162   EGLSurface anEglSurf = EGL_NO_SURFACE;
163   if (theGContext == (EGLContext )EGL_NO_CONTEXT)
164   {
165     // create new surface
166     anEglSurf = eglCreateWindowSurface (anEglDisplay, anEglConfig, (EGLNativeWindowType )theCWindow.XWindow, NULL);
167     if (anEglSurf == EGL_NO_SURFACE)
168     {
169       Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window, EGL is unable to create surface for window!");
170       return;
171     }
172   }
173   else if (theGContext != anEglContext)
174   {
175     Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window, EGL is used in unsupported combination!");
176     return;
177   }
178   else
179   {
180     anEglSurf = eglGetCurrentSurface(EGL_DRAW);
181     if (anEglSurf == EGL_NO_SURFACE)
182     {
183       Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window, EGL is unable to retrieve current surface!");
184       return;
185     }
186   }
187
188   myGlContext->Init ((Aspect_Drawable )anEglSurf, (Aspect_Display )anEglDisplay, (Aspect_RenderingContext )anEglContext);
189 #elif defined(_WIN32)
190   (void )theDriver;
191   HWND  aWindow   = (HWND )theCWindow.XWindow;
192   HDC   aWindowDC = GetDC (aWindow);
193   HGLRC aGContext = (HGLRC )theGContext;
194
195   PIXELFORMATDESCRIPTOR aPixelFrmt;
196   memset (&aPixelFrmt, 0, sizeof(aPixelFrmt));
197   aPixelFrmt.nSize        = sizeof(PIXELFORMATDESCRIPTOR);
198   aPixelFrmt.nVersion     = 1;
199   aPixelFrmt.dwFlags      = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
200   aPixelFrmt.iPixelType   = PFD_TYPE_RGBA;
201   aPixelFrmt.cColorBits   = 24;
202   aPixelFrmt.cDepthBits   = 24;
203   aPixelFrmt.cStencilBits = 8;
204   aPixelFrmt.iLayerType   = PFD_MAIN_PLANE;
205   if (theCaps->contextStereo)
206   {
207     aPixelFrmt.dwFlags |= PFD_STEREO;
208   }
209
210   int aPixelFrmtId = ChoosePixelFormat (aWindowDC, &aPixelFrmt);
211
212   // in case of failure try without stereo if any
213   if (aPixelFrmtId == 0 && theCaps->contextStereo)
214   {
215     TCollection_ExtendedString aMsg ("OpenGl_Window::CreateWindow: "
216                                      "ChoosePixelFormat is unable to find stereo supported pixel format. "
217                                      "Choosing similar non stereo format.");
218     myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
219                               GL_DEBUG_TYPE_OTHER_ARB,
220                               0, GL_DEBUG_SEVERITY_HIGH_ARB, aMsg);
221
222     aPixelFrmt.dwFlags &= ~PFD_STEREO;
223     aPixelFrmtId = ChoosePixelFormat (aWindowDC, &aPixelFrmt);
224   }
225
226   if (aPixelFrmtId == 0)
227   {
228     ReleaseDC (aWindow, aWindowDC);
229
230     TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: ChoosePixelFormat failed. Error code: ");
231     aMsg += (int )GetLastError();
232     Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
233     return;
234   }
235
236   DescribePixelFormat (aWindowDC, aPixelFrmtId, sizeof(aPixelFrmt), &aPixelFrmt);
237
238   HGLRC aSlaveCtx = !theShareCtx.IsNull() ? (HGLRC )theShareCtx->myGContext : NULL;
239   if (aGContext == NULL)
240   {
241     // create temporary context to retrieve advanced context creation procedures
242     HMODULE aModule = GetModuleHandleW(NULL);
243     WNDCLASSW aClass; memset (&aClass, 0, sizeof(aClass));
244     aClass.style         = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
245     aClass.lpfnWndProc   = wndProcDummy;
246     aClass.hInstance     = aModule;
247     aClass.lpszClassName = L"OpenGl_WindowTmp";
248     HWND  aWinTmp     = NULL;
249     HDC   aDevCtxTmp  = NULL;
250     HGLRC aRendCtxTmp = NULL;
251     if ((!theCaps->contextDebug && !theCaps->contextNoAccel)
252      || RegisterClassW (&aClass) == 0)
253     {
254       aClass.lpszClassName = NULL;
255     }
256     if (aClass.lpszClassName != NULL)
257     {
258       DWORD anExStyle = WS_EX_TOOLWINDOW | WS_EX_WINDOWEDGE;
259     #if (_WIN32_WINNT >= 0x0500)
260       anExStyle |= WS_EX_NOACTIVATE;
261     #endif
262       aWinTmp = CreateWindowExW(anExStyle,
263                                 aClass.lpszClassName, L"OpenGl_WindowTmp",
264                                 WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_DISABLED,
265                                 2, 2, 4, 4,
266                                 NULL, NULL, aModule, NULL);
267     }
268     if (aWinTmp != NULL)
269     {
270       aDevCtxTmp = GetDC (aWinTmp);
271       SetPixelFormat (aDevCtxTmp, aPixelFrmtId, &aPixelFrmt);
272       aRendCtxTmp = wglCreateContext (aDevCtxTmp);
273     }
274
275     typedef BOOL (WINAPI *wglChoosePixelFormatARB_t)(HDC           theDevCtx,
276                                                      const int*    theIntAttribs,
277                                                      const float*  theFloatAttribs,
278                                                      unsigned int  theMaxFormats,
279                                                      int*          theFormatsOut,
280                                                      unsigned int* theNumFormatsOut);
281     typedef HGLRC (WINAPI *wglCreateContextAttribsARB_t)(HDC        theDevCtx,
282                                                          HGLRC      theShareContext,
283                                                          const int* theAttribs);
284     wglChoosePixelFormatARB_t    aChoosePixProc = NULL;
285     wglCreateContextAttribsARB_t aCreateCtxProc = NULL;
286     if (aRendCtxTmp != NULL)
287     {
288       wglMakeCurrent (aDevCtxTmp, aRendCtxTmp);
289
290       typedef const char* (WINAPI *wglGetExtensionsStringARB_t)(HDC theDeviceContext);
291       wglGetExtensionsStringARB_t aGetExtensions = (wglGetExtensionsStringARB_t  )wglGetProcAddress ("wglGetExtensionsStringARB");
292       const char* aWglExts = (aGetExtensions != NULL) ? aGetExtensions (wglGetCurrentDC()) : NULL;
293       if (OpenGl_Context::CheckExtension (aWglExts, "WGL_ARB_pixel_format"))
294       {
295         aChoosePixProc = (wglChoosePixelFormatARB_t    )wglGetProcAddress ("wglChoosePixelFormatARB");
296       }
297       if (OpenGl_Context::CheckExtension (aWglExts, "WGL_ARB_create_context_profile"))
298       {
299         aCreateCtxProc = (wglCreateContextAttribsARB_t )wglGetProcAddress ("wglCreateContextAttribsARB");
300       }
301     }
302
303     // choose extended pixel format
304     if (aChoosePixProc != NULL)
305     {
306       const int aPixAttribs[] =
307       {
308         WGL_DRAW_TO_WINDOW_ARB, GL_TRUE,
309         WGL_SUPPORT_OPENGL_ARB, GL_TRUE,
310         WGL_DOUBLE_BUFFER_ARB,  GL_TRUE,
311         WGL_STEREO_ARB,         theCaps->contextStereo ? GL_TRUE : GL_FALSE,
312         WGL_PIXEL_TYPE_ARB,     WGL_TYPE_RGBA_ARB,
313         //WGL_SAMPLE_BUFFERS_ARB, 1,
314         //WGL_SAMPLES_ARB,        8,
315         WGL_COLOR_BITS_ARB,     24,
316         WGL_DEPTH_BITS_ARB,     24,
317         WGL_STENCIL_BITS_ARB,   8,
318         WGL_ACCELERATION_ARB,   theCaps->contextNoAccel ? WGL_NO_ACCELERATION_ARB : WGL_FULL_ACCELERATION_ARB,
319         0, 0,
320       };
321       unsigned int aFrmtsNb = 0;
322       aChoosePixProc (aWindowDC, aPixAttribs, NULL, 1, &aPixelFrmtId, &aFrmtsNb);
323     }
324
325     // setup pixel format - may be set only once per window
326     if (!SetPixelFormat (aWindowDC, aPixelFrmtId, &aPixelFrmt))
327     {
328       ReleaseDC (aWindow, aWindowDC);
329
330       TCollection_AsciiString aMsg("OpenGl_Window::CreateWindow: SetPixelFormat failed. Error code: ");
331       aMsg += (int )GetLastError();
332       Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
333       return;
334     }
335
336     // create GL context with extra options
337     if (aCreateCtxProc != NULL)
338     {
339       // Beware! NVIDIA drivers reject context creation when WGL_CONTEXT_PROFILE_MASK_ARB are specified
340       // but not WGL_CONTEXT_MAJOR_VERSION_ARB/WGL_CONTEXT_MINOR_VERSION_ARB.
341       int aCtxAttribs[] =
342       {
343         //WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
344         //WGL_CONTEXT_MINOR_VERSION_ARB, 2,
345         //WGL_CONTEXT_PROFILE_MASK_ARB,  WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB, //WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
346         WGL_CONTEXT_FLAGS_ARB,         theCaps->contextDebug ? WGL_CONTEXT_DEBUG_BIT_ARB : 0,
347         0, 0
348       };
349
350       aGContext = aCreateCtxProc (aWindowDC, aSlaveCtx, aCtxAttribs);
351       if (aGContext != NULL)
352       {
353         aSlaveCtx = NULL;
354       }
355     }
356
357     if (aRendCtxTmp != NULL)
358     {
359       wglDeleteContext (aRendCtxTmp);
360     }
361     if (aDevCtxTmp != NULL)
362     {
363       ReleaseDC (aWinTmp, aDevCtxTmp);
364     }
365     if (aWinTmp != NULL)
366     {
367       DestroyWindow (aWinTmp);
368     }
369     if (aClass.lpszClassName != NULL)
370     {
371       UnregisterClassW (aClass.lpszClassName, aModule);
372     }
373
374     if (aGContext == NULL)
375     {
376       // create context using obsolete functionality
377       aGContext = wglCreateContext (aWindowDC);
378     }
379     if (aGContext == NULL)
380     {
381       ReleaseDC (aWindow, aWindowDC);
382
383       TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: wglCreateContext failed. Error code: ");
384       aMsg += (int )GetLastError();
385       Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
386       return;
387     }
388   }
389
390   // all GL context within one OpenGl_GraphicDriver should be shared!
391   if (aSlaveCtx != NULL && wglShareLists (aSlaveCtx, aGContext) != TRUE)
392   {
393     TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: wglShareLists failed. Error code: ");
394     aMsg += (int )GetLastError();
395     Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
396     return;
397   }
398
399   myGlContext->Init ((Aspect_Handle )aWindow, (Aspect_Handle )aWindowDC, (Aspect_RenderingContext )aGContext);
400 #else
401   Window aParent = (Window )theCWindow.XWindow;
402   Window aWindow = 0;
403
404   Display*   aDisp     = theDriver->GetDisplayConnection()->GetDisplay();
405   GLXContext aGContext = (GLXContext )theGContext;
406
407   XWindowAttributes wattr;
408   XGetWindowAttributes (aDisp, aParent, &wattr);
409   const int scr = DefaultScreen (aDisp);
410
411   XVisualInfo* aVis = NULL;
412   {
413     unsigned long aVisInfoMask = VisualIDMask | VisualScreenMask;
414     XVisualInfo aVisInfo;
415     aVisInfo.visualid = wattr.visual->visualid;
416     aVisInfo.screen   = scr;
417     int aNbItems;
418     aVis = XGetVisualInfo (aDisp, aVisInfoMask, &aVisInfo, &aNbItems);
419   }
420
421   if (!myOwnGContext)
422   {
423     if (aVis != NULL)
424     {
425       Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window::CreateWindow: XGetVisualInfo failed.");
426       return;
427     }
428
429     aWindow = aParent;
430   }
431   else
432   {
433   #if defined(__linux) || defined(Linux) || defined(__APPLE__)
434     if (aVis != NULL)
435     {
436       // check Visual for OpenGl context's parameters compability
437       int isGl = 0, isDoubleBuffer = 0, isRGBA = 0, isStereo = 0;
438       int aDepthSize = 0, aStencilSize = 0;
439
440       if (glXGetConfig (aDisp, aVis, GLX_USE_GL, &isGl) != 0)
441         isGl = 0;
442
443       if (glXGetConfig (aDisp, aVis, GLX_RGBA, &isRGBA) != 0)
444         isRGBA = 0;
445
446       if (glXGetConfig (aDisp, aVis, GLX_DOUBLEBUFFER, &isDoubleBuffer) != 0)
447         isDoubleBuffer = 0;
448
449       if (glXGetConfig (aDisp, aVis, GLX_STEREO, &isStereo) != 0)
450         isStereo = 0;
451
452       if (glXGetConfig (aDisp, aVis, GLX_DEPTH_SIZE, &aDepthSize) != 0)
453         aDepthSize = 0;
454
455       if (glXGetConfig (aDisp, aVis, GLX_STENCIL_SIZE, &aStencilSize) != 0)
456         aStencilSize = 0;
457
458       if (!isGl || !aDepthSize || !isRGBA  || !aStencilSize ||
459           (isDoubleBuffer ? 1 : 0) != 1 ||
460           (isStereo       ? 1 : 0) != (theCaps->contextStereo ? 1 : 0))
461       {
462         XFree (aVis);
463         aVis = NULL;
464       }
465     }
466   #endif
467
468     if (aVis == NULL)
469     {
470       int anIter = 0;
471       int anAttribs[13];
472       anAttribs[anIter++] = GLX_RGBA;
473
474       anAttribs[anIter++] = GLX_DEPTH_SIZE;
475       anAttribs[anIter++] = 1;
476
477       anAttribs[anIter++] = GLX_STENCIL_SIZE;
478       anAttribs[anIter++] = 1;
479
480       anAttribs[anIter++] = GLX_RED_SIZE;
481       anAttribs[anIter++] = (wattr.depth <= 8) ? 0 : 1;
482
483       anAttribs[anIter++] = GLX_GREEN_SIZE;
484       anAttribs[anIter++] = (wattr.depth <= 8) ? 0 : 1;
485
486       anAttribs[anIter++] = GLX_BLUE_SIZE;
487       anAttribs[anIter++] = (wattr.depth <= 8) ? 0 : 1;
488
489       anAttribs[anIter++] = GLX_DOUBLEBUFFER;
490
491       // warning: this flag may be set to None, so it need to be last in anAttribs
492       Standard_Integer aStereoFlagPos = anIter;
493       if (theCaps->contextStereo)
494         anAttribs[anIter++] = GLX_STEREO;
495
496       anAttribs[anIter++] = None;
497
498       aVis = glXChooseVisual (aDisp, scr, anAttribs);
499
500       // in case of failure try without stereo if any
501       if (aVis == NULL && theCaps->contextStereo)
502       {
503         TCollection_ExtendedString aMsg ("OpenGl_Window::CreateWindow: "
504                                          "glXChooseVisual is unable to find stereo supported pixel format. "
505                                          "Choosing similar non stereo format.");
506         myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
507                                   GL_DEBUG_TYPE_OTHER_ARB,
508                                   0, GL_DEBUG_SEVERITY_HIGH_ARB, aMsg);
509
510         anAttribs[aStereoFlagPos] = None;
511         aVis = glXChooseVisual (aDisp, scr, anAttribs);
512       }
513
514       if (aVis == NULL)
515       {
516         Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window::CreateWindow: glXChooseVisual failed.");
517         return;
518       }
519     }
520
521     if (!theShareCtx.IsNull())
522     {
523       // ctx est une copie du previous
524       aGContext = glXCreateContext (aDisp, aVis, (GLXContext )theShareCtx->myGContext, GL_TRUE);
525     }
526     else
527     {
528       aGContext = glXCreateContext (aDisp, aVis, NULL, GL_TRUE);
529     }
530
531     if (!aGContext)
532     {
533       Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window::CreateWindow: glXCreateContext failed.");
534       return;
535     }
536
537     Colormap cmap = XCreateColormap (aDisp, aParent, aVis->visual, AllocNone);
538
539     XColor color;
540     color.red   = (unsigned short) (myBgColor.rgb[0] * 0xFFFF);
541     color.green = (unsigned short) (myBgColor.rgb[1] * 0xFFFF);
542     color.blue  = (unsigned short) (myBgColor.rgb[2] * 0xFFFF);
543     color.flags = DoRed | DoGreen | DoBlue;
544     XAllocColor (aDisp, cmap, &color);
545
546     XSetWindowAttributes cwa;
547     cwa.colormap         = cmap;
548     cwa.event_mask       = StructureNotifyMask;
549     cwa.border_pixel     = color.pixel;
550     cwa.background_pixel = color.pixel;
551
552     if (aVis->visualid == wattr.visual->visualid)
553     {
554       aWindow = aParent;
555     }
556     else
557     {
558       unsigned long mask = CWBackPixel | CWColormap | CWBorderPixel | CWEventMask;
559       aWindow = XCreateWindow (aDisp, aParent, 0, 0, myWidth, myHeight, 0/*bw*/, aVis->depth, InputOutput, aVis->visual, mask, &cwa);
560     }
561
562     XSetWindowBackground (aDisp, aWindow, cwa.background_pixel);
563     XClearWindow (aDisp, aWindow);
564
565     if (aWindow != aParent)
566     {
567       XEvent anEvent;
568       XMapWindow (aDisp, aWindow);
569       XIfEvent (aDisp, &anEvent, WaitForNotify, (char* )aWindow);
570     }
571   }
572
573   myGlContext->Init ((Aspect_Drawable )aWindow, (Aspect_Display )aDisp, (Aspect_RenderingContext )aGContext);
574 #endif
575   myGlContext->Share (theShareCtx);
576
577   Init();
578 }
579
580 // =======================================================================
581 // function : ~OpenGl_Window
582 // purpose  :
583 // =======================================================================
584 OpenGl_Window::~OpenGl_Window()
585 {
586   if (!myOwnGContext
587    ||  myGlContext.IsNull())
588   {
589     myGlContext.Nullify();
590     return;
591   }
592
593   // release "GL" context if it is owned by window
594   // Mesa implementation can fail to destroy GL context if it set for current thread.
595   // It should be safer to unset thread GL context before its destruction.
596 #if defined(HAVE_EGL) || defined(__ANDROID__)
597   if ((EGLSurface )myGlContext->myWindow != EGL_NO_SURFACE)
598   {
599     eglDestroySurface ((EGLDisplay )myGlContext->myDisplay,
600                        (EGLSurface )myGlContext->myWindow);
601   }
602 #elif defined(_WIN32)
603   HWND  aWindow          = (HWND  )myGlContext->myWindow;
604   HDC   aWindowDC        = (HDC   )myGlContext->myWindowDC;
605   HGLRC aWindowGContext  = (HGLRC )myGlContext->myGContext;
606   HGLRC aThreadGContext  = wglGetCurrentContext();
607   myGlContext.Nullify();
608
609   if (aThreadGContext != NULL)
610   {
611     if (aThreadGContext == aWindowGContext)
612     {
613       wglMakeCurrent (NULL, NULL);
614     }
615
616     wglDeleteContext (aWindowGContext);
617   }
618   ReleaseDC (aWindow, aWindowDC);
619 #else
620   Display*    aDisplay        = (Display*    )myGlContext->myDisplay;
621   GLXContext  aWindowGContext = (GLXContext  )myGlContext->myGContext;
622   GLXContext  aThreadGContext = glXGetCurrentContext();
623   myGlContext.Nullify();
624
625   if (aDisplay != NULL)
626   {
627     if (aThreadGContext == aWindowGContext)
628     {
629       glXMakeCurrent (aDisplay, None, NULL);
630     }
631
632     // FSXXX sync necessary if non-direct rendering
633     glXWaitGL();
634     glXDestroyContext (aDisplay, aWindowGContext);
635   }
636 #endif
637 }
638
639 #endif // !__APPLE__
640
641 // =======================================================================
642 // function : Activate
643 // purpose  :
644 // =======================================================================
645 Standard_Boolean OpenGl_Window::Activate()
646 {
647   return myGlContext->MakeCurrent();
648 }
649
650 #if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
651
652 // =======================================================================
653 // function : Resize
654 // purpose  : call_subr_resize
655 // =======================================================================
656 void OpenGl_Window::Resize (const CALL_DEF_WINDOW& theCWindow)
657 {
658 #if !defined(_WIN32) && !defined(HAVE_EGL) && !defined(__ANDROID__)
659   Display* aDisp = (Display* )myGlContext->myDisplay;
660   if (aDisp == NULL)
661     return;
662 #endif
663
664   // If the size is not changed - do nothing
665   if ((myWidth == theCWindow.dx) && (myHeight == theCWindow.dy))
666     return;
667
668   myWidth  = theCWindow.dx;
669   myHeight = theCWindow.dy;
670
671 #if !defined(_WIN32) && !defined(HAVE_EGL) && !defined(__ANDROID__)
672   XResizeWindow (aDisp, myGlContext->myWindow, (unsigned int )myWidth, (unsigned int )myHeight);
673   XSync (aDisp, False);
674 #endif
675
676   Init();
677 }
678
679 #endif // !__APPLE__
680
681 // =======================================================================
682 // function : ReadDepths
683 // purpose  : TelReadDepths
684 // =======================================================================
685 void OpenGl_Window::ReadDepths (const Standard_Integer theX,     const Standard_Integer theY,
686                                 const Standard_Integer theWidth, const Standard_Integer theHeight,
687                                 float* theDepths)
688 {
689   if (theDepths == NULL || !Activate())
690     return;
691
692   OpenGl_Mat4 aProjectMat;
693   OpenGl_Utils::Ortho2D<Standard_ShortReal> (aProjectMat,
694     0.f, static_cast<GLfloat> (myWidth), 0.f, static_cast<GLfloat> (myHeight));
695
696   myGlContext->WorldViewState.Push();
697   myGlContext->ProjectionState.Push();
698
699   myGlContext->WorldViewState.SetIdentity();
700   myGlContext->ProjectionState.SetCurrent (aProjectMat);
701
702   myGlContext->ApplyProjectionMatrix();
703   myGlContext->ApplyWorldViewMatrix();
704
705 #if !defined(GL_ES_VERSION_2_0)
706   glRasterPos2i (theX, theY);
707   DisableFeatures();
708   glReadPixels (theX, theY, theWidth, theHeight, GL_DEPTH_COMPONENT, GL_FLOAT, theDepths);
709   EnableFeatures();
710 #endif
711
712   myGlContext->WorldViewState.Pop();
713   myGlContext->ProjectionState.Pop();
714
715   myGlContext->ApplyProjectionMatrix();
716 }
717
718 // =======================================================================
719 // function : SetBackgroundColor
720 // purpose  : call_subr_set_background
721 // =======================================================================
722 void OpenGl_Window::SetBackgroundColor (const Standard_ShortReal theR,
723                                         const Standard_ShortReal theG,
724                                         const Standard_ShortReal theB)
725 {
726   myBgColor.rgb[0] = theR;
727   myBgColor.rgb[1] = theG;
728   myBgColor.rgb[2] = theB;
729 }
730
731 #if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
732
733 // =======================================================================
734 // function : Init
735 // purpose  :
736 // =======================================================================
737 void OpenGl_Window::Init()
738 {
739   if (!Activate())
740     return;
741
742 #if defined(HAVE_EGL) || defined(__ANDROID__)
743   eglQuerySurface ((EGLDisplay )myGlContext->myDisplay, (EGLSurface )myGlContext->myWindow, EGL_WIDTH,  &myWidth);
744   eglQuerySurface ((EGLDisplay )myGlContext->myDisplay, (EGLSurface )myGlContext->myWindow, EGL_HEIGHT, &myHeight);
745 #elif defined(_WIN32)
746   RECT cr;
747   GetClientRect ((HWND )myGlContext->myWindow, &cr);
748   myWidth  = cr.right - cr.left;
749   myHeight = cr.bottom - cr.top;
750 #else
751   Window aRootWin;
752   int aDummy;
753   unsigned int aDummyU;
754   unsigned int aNewWidth  = 0;
755   unsigned int aNewHeight = 0;
756   Display* aDisp = (Display* )myGlContext->myDisplay;
757   XGetGeometry (aDisp, myGlContext->myWindow, &aRootWin, &aDummy, &aDummy, &aNewWidth, &aNewHeight, &aDummyU, &aDummyU);
758   myWidth  = aNewWidth;
759   myHeight = aNewHeight;
760 #endif
761
762   glDisable (GL_DITHER);
763   glDisable (GL_SCISSOR_TEST);
764   glViewport (0, 0, myWidth, myHeight);
765 #if !defined(GL_ES_VERSION_2_0)
766   glMatrixMode (GL_MODELVIEW);
767   glDrawBuffer (GL_BACK);
768 #endif
769 }
770
771 #endif // !__APPLE__
772
773 // =======================================================================
774 // function : EnableFeatures
775 // purpose  :
776 // =======================================================================
777 void OpenGl_Window::EnableFeatures() const
778 {
779   //
780 }
781
782 // =======================================================================
783 // function : DisableFeatures
784 // purpose  :
785 // =======================================================================
786 void OpenGl_Window::DisableFeatures() const
787 {
788 #if !defined(GL_ES_VERSION_2_0)
789   glPixelTransferi (GL_MAP_COLOR, GL_FALSE);
790 #endif
791
792   /*
793   * Disable stuff that's likely to slow down glDrawPixels.
794   * (Omit as much of this as possible, when you know in advance
795   * that the OpenGL state will already be set correctly.)
796   */
797   glDisable(GL_DITHER);
798   glDisable(GL_BLEND);
799   glDisable(GL_DEPTH_TEST);
800   glDisable(GL_TEXTURE_2D);
801   glDisable(GL_STENCIL_TEST);
802
803 #if !defined(GL_ES_VERSION_2_0)
804   glDisable(GL_LIGHTING);
805   glDisable(GL_ALPHA_TEST);
806   glDisable(GL_FOG);
807   glDisable(GL_LOGIC_OP);
808   glDisable(GL_TEXTURE_1D);
809
810   glPixelTransferi(GL_MAP_COLOR, GL_FALSE);
811   glPixelTransferi(GL_RED_SCALE, 1);
812   glPixelTransferi(GL_RED_BIAS, 0);
813   glPixelTransferi(GL_GREEN_SCALE, 1);
814   glPixelTransferi(GL_GREEN_BIAS, 0);
815   glPixelTransferi(GL_BLUE_SCALE, 1);
816   glPixelTransferi(GL_BLUE_BIAS, 0);
817   glPixelTransferi(GL_ALPHA_SCALE, 1);
818   glPixelTransferi(GL_ALPHA_BIAS, 0);
819
820   /*
821   * Disable extensions that could slow down glDrawPixels.
822   * (Actually, you should check for the presence of the proper
823   * extension before making these calls.  I've omitted that
824   * code for simplicity.)
825   */
826
827   if ((myGlContext->myGlVerMajor >= 1) && (myGlContext->myGlVerMinor >= 2))
828   {
829 #ifdef GL_EXT_convolution
830     if (myGlContext->CheckExtension ("GL_CONVOLUTION_1D_EXT"))
831       glDisable(GL_CONVOLUTION_1D_EXT);
832
833     if (myGlContext->CheckExtension ("GL_CONVOLUTION_2D_EXT"))
834       glDisable(GL_CONVOLUTION_2D_EXT);
835
836     if (myGlContext->CheckExtension ("GL_SEPARABLE_2D_EXT"))
837       glDisable(GL_SEPARABLE_2D_EXT);
838 #endif
839
840 #ifdef GL_EXT_histogram
841     if (myGlContext->CheckExtension ("GL_SEPARABLE_2D_EXT"))
842       glDisable(GL_HISTOGRAM_EXT);
843
844     if (myGlContext->CheckExtension ("GL_MINMAX_EXT"))
845       glDisable(GL_MINMAX_EXT);
846 #endif
847
848 #ifdef GL_EXT_texture3D
849     if (myGlContext->CheckExtension ("GL_TEXTURE_3D_EXT"))
850       glDisable(GL_TEXTURE_3D_EXT);
851 #endif
852   }
853 #endif
854 }
855
856 // =======================================================================
857 // function : MakeFrontBufCurrent
858 // purpose  : TelMakeFrontBufCurrent
859 // =======================================================================
860 void OpenGl_Window::MakeFrontBufCurrent() const
861 {
862 #if !defined(GL_ES_VERSION_2_0)
863   glDrawBuffer (GL_FRONT);
864 #endif
865 }
866
867 // =======================================================================
868 // function : MakeBackBufCurrent
869 // purpose  : TelMakeBackBufCurrent
870 // =======================================================================
871 void OpenGl_Window::MakeBackBufCurrent() const
872 {
873 #if !defined(GL_ES_VERSION_2_0)
874   glDrawBuffer (GL_BACK);
875 #endif
876 }