0025854: Visualization, TKOpenGl - add option to request Core profile 3.2+
[occt.git] / src / OpenGl / OpenGl_Window.cxx
CommitLineData
b311480e 1// Created on: 2011-09-20
2// Created by: Sergey ZERCHANINOV
973c2be1 3// Copyright (c) 2011-2014 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
d5f74e42 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
973c2be1 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.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
b311480e 15
b5ac8292 16#include <OpenGl_GlCore12.hxx>
2166f0fa
SK
17#include <InterfaceGraphic.hxx>
18
2166f0fa 19#include <OpenGl_Context.hxx>
25b97fac 20#include <OpenGl_GraphicDriver.hxx>
c827ea3a 21#include <OpenGl_Window.hxx>
22#include <OpenGl_Utils.hxx>
2166f0fa
SK
23
24#include <Aspect_GraphicDeviceDefinitionError.hxx>
25#include <TCollection_AsciiString.hxx>
b5ac8292 26#include <TCollection_ExtendedString.hxx>
2166f0fa 27
25b97fac 28#if defined(HAVE_EGL) || defined(__ANDROID__)
29 #include <EGL/egl.h>
30#endif
31
2166f0fa
SK
32IMPLEMENT_STANDARD_HANDLE(OpenGl_Window,MMgt_TShared)
33IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Window,MMgt_TShared)
34
4fe56619 35#if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
36
2166f0fa
SK
37namespace
38{
39 static const TEL_COLOUR THE_DEFAULT_BG_COLOR = { { 0.F, 0.F, 0.F, 1.F } };
40
25b97fac 41#if defined(HAVE_EGL) || defined(__ANDROID__)
42 //
43#elif defined(_WIN32)
58655684 44
abe46077 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
58655684 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 }
2166f0fa
SK
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
5640d653 129}
2166f0fa
SK
130
131// =======================================================================
132// function : OpenGl_Window
133// purpose :
134// =======================================================================
25b97fac 135OpenGl_Window::OpenGl_Window (const Handle(OpenGl_GraphicDriver)& theDriver,
2166f0fa 136 const CALL_DEF_WINDOW& theCWindow,
5e27df78 137 Aspect_RenderingContext theGContext,
58655684 138 const Handle(OpenGl_Caps)& theCaps,
5e27df78 139 const Handle(OpenGl_Context)& theShareCtx)
73192b37 140: myGlContext (new OpenGl_Context (theCaps)),
2166f0fa 141 myOwnGContext (theGContext == 0),
e3573bb9 142 myWidth (theCWindow.dx),
143 myHeight (theCWindow.dy),
73192b37 144 myBgColor (THE_DEFAULT_BG_COLOR)
2166f0fa
SK
145{
146 myBgColor.rgb[0] = theCWindow.Background.r;
147 myBgColor.rgb[1] = theCWindow.Background.g;
148 myBgColor.rgb[2] = theCWindow.Background.b;
149
4e1523ef 150 Standard_Boolean isCoreProfile = Standard_False;
151
25b97fac 152#if defined(HAVE_EGL) || defined(__ANDROID__)
153 EGLDisplay anEglDisplay = (EGLDisplay )theDriver->getRawGlDisplay();
154 EGLContext anEglContext = (EGLContext )theDriver->getRawGlContext();
155 EGLConfig anEglConfig = (EGLConfig )theDriver->getRawGlConfig();
156 if (anEglDisplay == EGL_NO_DISPLAY
157 || anEglContext == EGL_NO_CONTEXT
158 || anEglConfig == NULL)
159 {
160 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window, EGL does not provide compatible configurations!");
161 return;
162 }
163
164 EGLSurface anEglSurf = EGL_NO_SURFACE;
165 if (theGContext == (EGLContext )EGL_NO_CONTEXT)
166 {
167 // create new surface
168 anEglSurf = eglCreateWindowSurface (anEglDisplay, anEglConfig, (EGLNativeWindowType )theCWindow.XWindow, NULL);
169 if (anEglSurf == EGL_NO_SURFACE)
170 {
171 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window, EGL is unable to create surface for window!");
172 return;
173 }
174 }
175 else if (theGContext != anEglContext)
176 {
177 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window, EGL is used in unsupported combination!");
178 return;
179 }
180 else
181 {
182 anEglSurf = eglGetCurrentSurface(EGL_DRAW);
183 if (anEglSurf == EGL_NO_SURFACE)
184 {
185 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window, EGL is unable to retrieve current surface!");
186 return;
187 }
188 }
189
4e1523ef 190 myGlContext->Init ((Aspect_Drawable )anEglSurf, (Aspect_Display )anEglDisplay, (Aspect_RenderingContext )anEglContext, isCoreProfile);
25b97fac 191#elif defined(_WIN32)
192 (void )theDriver;
5e27df78 193 HWND aWindow = (HWND )theCWindow.XWindow;
194 HDC aWindowDC = GetDC (aWindow);
195 HGLRC aGContext = (HGLRC )theGContext;
196
3d8969b1 197 PIXELFORMATDESCRIPTOR aPixelFrmt;
abe46077 198 memset (&aPixelFrmt, 0, sizeof(aPixelFrmt));
199 aPixelFrmt.nSize = sizeof(PIXELFORMATDESCRIPTOR);
200 aPixelFrmt.nVersion = 1;
201 aPixelFrmt.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
202 aPixelFrmt.iPixelType = PFD_TYPE_RGBA;
203 aPixelFrmt.cColorBits = 24;
204 aPixelFrmt.cDepthBits = 24;
205 aPixelFrmt.cStencilBits = 8;
206 aPixelFrmt.iLayerType = PFD_MAIN_PLANE;
207 if (theCaps->contextStereo)
208 {
209 aPixelFrmt.dwFlags |= PFD_STEREO;
210 }
211
212 int aPixelFrmtId = ChoosePixelFormat (aWindowDC, &aPixelFrmt);
b5ac8292 213
214 // in case of failure try without stereo if any
4e1523ef 215 const Standard_Boolean hasStereo = aPixelFrmtId != 0 && theCaps->contextStereo;
b5ac8292 216 if (aPixelFrmtId == 0 && theCaps->contextStereo)
217 {
218 TCollection_ExtendedString aMsg ("OpenGl_Window::CreateWindow: "
219 "ChoosePixelFormat is unable to find stereo supported pixel format. "
220 "Choosing similar non stereo format.");
221 myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
222 GL_DEBUG_TYPE_OTHER_ARB,
223 0, GL_DEBUG_SEVERITY_HIGH_ARB, aMsg);
224
225 aPixelFrmt.dwFlags &= ~PFD_STEREO;
226 aPixelFrmtId = ChoosePixelFormat (aWindowDC, &aPixelFrmt);
227 }
228
3d8969b1 229 if (aPixelFrmtId == 0)
5e27df78 230 {
231 ReleaseDC (aWindow, aWindowDC);
232
233 TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: ChoosePixelFormat failed. Error code: ");
234 aMsg += (int )GetLastError();
235 Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
236 return;
237 }
238
abe46077 239 DescribePixelFormat (aWindowDC, aPixelFrmtId, sizeof(aPixelFrmt), &aPixelFrmt);
5e27df78 240
58655684 241 HGLRC aSlaveCtx = !theShareCtx.IsNull() ? (HGLRC )theShareCtx->myGContext : NULL;
5e27df78 242 if (aGContext == NULL)
243 {
58655684 244 // create temporary context to retrieve advanced context creation procedures
245 HMODULE aModule = GetModuleHandleW(NULL);
246 WNDCLASSW aClass; memset (&aClass, 0, sizeof(aClass));
247 aClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
248 aClass.lpfnWndProc = wndProcDummy;
249 aClass.hInstance = aModule;
250 aClass.lpszClassName = L"OpenGl_WindowTmp";
251 HWND aWinTmp = NULL;
252 HDC aDevCtxTmp = NULL;
253 HGLRC aRendCtxTmp = NULL;
4e1523ef 254 if ((!theCaps->contextDebug && !theCaps->contextNoAccel && theCaps->contextCompatible)
58655684 255 || RegisterClassW (&aClass) == 0)
256 {
257 aClass.lpszClassName = NULL;
258 }
259 if (aClass.lpszClassName != NULL)
260 {
229c0b6a 261 DWORD anExStyle = WS_EX_TOOLWINDOW | WS_EX_WINDOWEDGE;
262 #if (_WIN32_WINNT >= 0x0500)
263 anExStyle |= WS_EX_NOACTIVATE;
264 #endif
265 aWinTmp = CreateWindowExW(anExStyle,
58655684 266 aClass.lpszClassName, L"OpenGl_WindowTmp",
267 WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_DISABLED,
268 2, 2, 4, 4,
269 NULL, NULL, aModule, NULL);
270 }
271 if (aWinTmp != NULL)
272 {
273 aDevCtxTmp = GetDC (aWinTmp);
274 SetPixelFormat (aDevCtxTmp, aPixelFrmtId, &aPixelFrmt);
275 aRendCtxTmp = wglCreateContext (aDevCtxTmp);
276 }
abe46077 277
278 typedef BOOL (WINAPI *wglChoosePixelFormatARB_t)(HDC theDevCtx,
279 const int* theIntAttribs,
280 const float* theFloatAttribs,
281 unsigned int theMaxFormats,
282 int* theFormatsOut,
283 unsigned int* theNumFormatsOut);
284 typedef HGLRC (WINAPI *wglCreateContextAttribsARB_t)(HDC theDevCtx,
285 HGLRC theShareContext,
286 const int* theAttribs);
287 wglChoosePixelFormatARB_t aChoosePixProc = NULL;
288 wglCreateContextAttribsARB_t aCreateCtxProc = NULL;
58655684 289 if (aRendCtxTmp != NULL)
290 {
291 wglMakeCurrent (aDevCtxTmp, aRendCtxTmp);
292
293 typedef const char* (WINAPI *wglGetExtensionsStringARB_t)(HDC theDeviceContext);
abe46077 294 wglGetExtensionsStringARB_t aGetExtensions = (wglGetExtensionsStringARB_t )wglGetProcAddress ("wglGetExtensionsStringARB");
295 const char* aWglExts = (aGetExtensions != NULL) ? aGetExtensions (wglGetCurrentDC()) : NULL;
296 if (OpenGl_Context::CheckExtension (aWglExts, "WGL_ARB_pixel_format"))
297 {
298 aChoosePixProc = (wglChoosePixelFormatARB_t )wglGetProcAddress ("wglChoosePixelFormatARB");
299 }
300 if (OpenGl_Context::CheckExtension (aWglExts, "WGL_ARB_create_context_profile"))
301 {
302 aCreateCtxProc = (wglCreateContextAttribsARB_t )wglGetProcAddress ("wglCreateContextAttribsARB");
303 }
304 }
305
306 // choose extended pixel format
307 if (aChoosePixProc != NULL)
308 {
309 const int aPixAttribs[] =
310 {
311 WGL_DRAW_TO_WINDOW_ARB, GL_TRUE,
312 WGL_SUPPORT_OPENGL_ARB, GL_TRUE,
313 WGL_DOUBLE_BUFFER_ARB, GL_TRUE,
4e1523ef 314 WGL_STEREO_ARB, hasStereo ? GL_TRUE : GL_FALSE,
abe46077 315 WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB,
316 //WGL_SAMPLE_BUFFERS_ARB, 1,
317 //WGL_SAMPLES_ARB, 8,
318 WGL_COLOR_BITS_ARB, 24,
319 WGL_DEPTH_BITS_ARB, 24,
320 WGL_STENCIL_BITS_ARB, 8,
321 WGL_ACCELERATION_ARB, theCaps->contextNoAccel ? WGL_NO_ACCELERATION_ARB : WGL_FULL_ACCELERATION_ARB,
322 0, 0,
323 };
324 unsigned int aFrmtsNb = 0;
325 aChoosePixProc (aWindowDC, aPixAttribs, NULL, 1, &aPixelFrmtId, &aFrmtsNb);
326 }
327
328 // setup pixel format - may be set only once per window
329 if (!SetPixelFormat (aWindowDC, aPixelFrmtId, &aPixelFrmt))
330 {
331 ReleaseDC (aWindow, aWindowDC);
332
333 TCollection_AsciiString aMsg("OpenGl_Window::CreateWindow: SetPixelFormat failed. Error code: ");
334 aMsg += (int )GetLastError();
335 Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
336 return;
337 }
338
339 // create GL context with extra options
340 if (aCreateCtxProc != NULL)
341 {
4e1523ef 342 if (!theCaps->contextCompatible)
abe46077 343 {
4e1523ef 344 int aCoreCtxAttribs[] =
345 {
346 WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
347 WGL_CONTEXT_MINOR_VERSION_ARB, 2,
348 WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
349 WGL_CONTEXT_FLAGS_ARB, theCaps->contextDebug ? WGL_CONTEXT_DEBUG_BIT_ARB : 0,
350 0, 0
351 };
352
353 // Try to create the core profile of highest OpenGL version supported by OCCT
354 // (this will be done automatically by some drivers when requesting 3.2,
355 // but some will not (e.g. AMD Catalyst) since WGL_ARB_create_context_profile specification allows both implementations).
356 for (int aLowVer4 = 5; aLowVer4 >= 0 && aGContext == NULL; --aLowVer4)
357 {
358 aCoreCtxAttribs[1] = 4;
359 aCoreCtxAttribs[3] = aLowVer4;
360 aGContext = aCreateCtxProc (aWindowDC, aSlaveCtx, aCoreCtxAttribs);
361 }
362 for (int aLowVer3 = 3; aLowVer3 >= 2 && aGContext == NULL; --aLowVer3)
363 {
364 aCoreCtxAttribs[1] = 3;
365 aCoreCtxAttribs[3] = aLowVer3;
366 aGContext = aCreateCtxProc (aWindowDC, aSlaveCtx, aCoreCtxAttribs);
367 }
368 isCoreProfile = aGContext != NULL;
369 }
370
371 if (aGContext == NULL)
372 {
373 int aCtxAttribs[] =
374 {
375 // Beware! NVIDIA drivers reject context creation when WGL_CONTEXT_PROFILE_MASK_ARB are specified
376 // but not WGL_CONTEXT_MAJOR_VERSION_ARB/WGL_CONTEXT_MINOR_VERSION_ARB.
377 //WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB,
378 WGL_CONTEXT_FLAGS_ARB, theCaps->contextDebug ? WGL_CONTEXT_DEBUG_BIT_ARB : 0,
379 0, 0
380 };
381 isCoreProfile = Standard_False;
382 aGContext = aCreateCtxProc (aWindowDC, aSlaveCtx, aCtxAttribs);
383
384 if (aGContext != NULL
385 && !theCaps->contextCompatible)
386 {
387 TCollection_ExtendedString aMsg("OpenGl_Window::CreateWindow: core profile creation failed.");
388 myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB, GL_DEBUG_TYPE_PORTABILITY_ARB, 0, GL_DEBUG_SEVERITY_LOW_ARB, aMsg);
389 }
390 }
abe46077 391
abe46077 392 if (aGContext != NULL)
58655684 393 {
abe46077 394 aSlaveCtx = NULL;
58655684 395 }
396 }
397
398 if (aRendCtxTmp != NULL)
399 {
400 wglDeleteContext (aRendCtxTmp);
401 }
402 if (aDevCtxTmp != NULL)
403 {
404 ReleaseDC (aWinTmp, aDevCtxTmp);
405 }
406 if (aWinTmp != NULL)
407 {
408 DestroyWindow (aWinTmp);
409 }
410 if (aClass.lpszClassName != NULL)
411 {
412 UnregisterClassW (aClass.lpszClassName, aModule);
413 }
414
415 if (aGContext == NULL)
416 {
abe46077 417 // create context using obsolete functionality
58655684 418 aGContext = wglCreateContext (aWindowDC);
419 }
5e27df78 420 if (aGContext == NULL)
421 {
422 ReleaseDC (aWindow, aWindowDC);
423
424 TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: wglCreateContext failed. Error code: ");
425 aMsg += (int )GetLastError();
426 Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
427 return;
428 }
429 }
430
431 // all GL context within one OpenGl_GraphicDriver should be shared!
58655684 432 if (aSlaveCtx != NULL && wglShareLists (aSlaveCtx, aGContext) != TRUE)
5e27df78 433 {
434 TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: wglShareLists failed. Error code: ");
435 aMsg += (int )GetLastError();
436 Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
437 return;
438 }
439
4e1523ef 440 myGlContext->Init ((Aspect_Handle )aWindow, (Aspect_Handle )aWindowDC, (Aspect_RenderingContext )aGContext, isCoreProfile);
5e27df78 441#else
eaf5c5e0 442 Window aParent = (Window )theCWindow.XWindow;
443 Window aWindow = 0;
444
25b97fac 445 Display* aDisp = theDriver->GetDisplayConnection()->GetDisplay();
5e27df78 446 GLXContext aGContext = (GLXContext )theGContext;
2166f0fa 447
2166f0fa
SK
448 XWindowAttributes wattr;
449 XGetWindowAttributes (aDisp, aParent, &wattr);
450 const int scr = DefaultScreen (aDisp);
451
452 XVisualInfo* aVis = NULL;
453 {
454 unsigned long aVisInfoMask = VisualIDMask | VisualScreenMask;
455 XVisualInfo aVisInfo;
456 aVisInfo.visualid = wattr.visual->visualid;
457 aVisInfo.screen = scr;
458 int aNbItems;
459 aVis = XGetVisualInfo (aDisp, aVisInfoMask, &aVisInfo, &aNbItems);
460 }
461
2166f0fa
SK
462 if (!myOwnGContext)
463 {
464 if (aVis != NULL)
465 {
466 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window::CreateWindow: XGetVisualInfo failed.");
467 return;
468 }
469
5e27df78 470 aWindow = aParent;
2166f0fa
SK
471 }
472 else
473 {
cdc56cc8 474 #if defined(__linux) || defined(Linux) || defined(__APPLE__)
2166f0fa
SK
475 if (aVis != NULL)
476 {
4e1523ef 477 // check Visual for OpenGl context's parameters compatibility
b5ac8292 478 int isGl = 0, isDoubleBuffer = 0, isRGBA = 0, isStereo = 0;
479 int aDepthSize = 0, aStencilSize = 0;
2166f0fa
SK
480
481 if (glXGetConfig (aDisp, aVis, GLX_USE_GL, &isGl) != 0)
482 isGl = 0;
483
484 if (glXGetConfig (aDisp, aVis, GLX_RGBA, &isRGBA) != 0)
485 isRGBA = 0;
486
487 if (glXGetConfig (aDisp, aVis, GLX_DOUBLEBUFFER, &isDoubleBuffer) != 0)
488 isDoubleBuffer = 0;
489
b5ac8292 490 if (glXGetConfig (aDisp, aVis, GLX_STEREO, &isStereo) != 0)
491 isStereo = 0;
492
2166f0fa
SK
493 if (glXGetConfig (aDisp, aVis, GLX_DEPTH_SIZE, &aDepthSize) != 0)
494 aDepthSize = 0;
495
3d8969b1 496 if (glXGetConfig (aDisp, aVis, GLX_STENCIL_SIZE, &aStencilSize) != 0)
497 aStencilSize = 0;
498
b5ac8292 499 if (!isGl || !aDepthSize || !isRGBA || !aStencilSize ||
73192b37 500 (isDoubleBuffer ? 1 : 0) != 1 ||
b5ac8292 501 (isStereo ? 1 : 0) != (theCaps->contextStereo ? 1 : 0))
2166f0fa
SK
502 {
503 XFree (aVis);
504 aVis = NULL;
505 }
506 }
507 #endif
508
509 if (aVis == NULL)
510 {
511 int anIter = 0;
3d8969b1 512 int anAttribs[13];
2166f0fa
SK
513 anAttribs[anIter++] = GLX_RGBA;
514
515 anAttribs[anIter++] = GLX_DEPTH_SIZE;
516 anAttribs[anIter++] = 1;
517
3d8969b1 518 anAttribs[anIter++] = GLX_STENCIL_SIZE;
519 anAttribs[anIter++] = 1;
520
2166f0fa
SK
521 anAttribs[anIter++] = GLX_RED_SIZE;
522 anAttribs[anIter++] = (wattr.depth <= 8) ? 0 : 1;
523
524 anAttribs[anIter++] = GLX_GREEN_SIZE;
525 anAttribs[anIter++] = (wattr.depth <= 8) ? 0 : 1;
526
527 anAttribs[anIter++] = GLX_BLUE_SIZE;
528 anAttribs[anIter++] = (wattr.depth <= 8) ? 0 : 1;
529
73192b37 530 anAttribs[anIter++] = GLX_DOUBLEBUFFER;
2166f0fa 531
b5ac8292 532 // warning: this flag may be set to None, so it need to be last in anAttribs
533 Standard_Integer aStereoFlagPos = anIter;
534 if (theCaps->contextStereo)
535 anAttribs[anIter++] = GLX_STEREO;
536
2166f0fa
SK
537 anAttribs[anIter++] = None;
538
539 aVis = glXChooseVisual (aDisp, scr, anAttribs);
b5ac8292 540
541 // in case of failure try without stereo if any
542 if (aVis == NULL && theCaps->contextStereo)
543 {
544 TCollection_ExtendedString aMsg ("OpenGl_Window::CreateWindow: "
545 "glXChooseVisual is unable to find stereo supported pixel format. "
546 "Choosing similar non stereo format.");
547 myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
548 GL_DEBUG_TYPE_OTHER_ARB,
549 0, GL_DEBUG_SEVERITY_HIGH_ARB, aMsg);
550
551 anAttribs[aStereoFlagPos] = None;
552 aVis = glXChooseVisual (aDisp, scr, anAttribs);
553 }
554
2166f0fa
SK
555 if (aVis == NULL)
556 {
557 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window::CreateWindow: glXChooseVisual failed.");
558 return;
559 }
560 }
561
5e27df78 562 if (!theShareCtx.IsNull())
2166f0fa 563 {
5e27df78 564 // ctx est une copie du previous
565 aGContext = glXCreateContext (aDisp, aVis, (GLXContext )theShareCtx->myGContext, GL_TRUE);
2166f0fa
SK
566 }
567 else
568 {
5e27df78 569 aGContext = glXCreateContext (aDisp, aVis, NULL, GL_TRUE);
2166f0fa
SK
570 }
571
5e27df78 572 if (!aGContext)
2166f0fa
SK
573 {
574 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window::CreateWindow: glXCreateContext failed.");
575 return;
576 }
577
2166f0fa
SK
578 Colormap cmap = XCreateColormap (aDisp, aParent, aVis->visual, AllocNone);
579
580 XColor color;
581 color.red = (unsigned short) (myBgColor.rgb[0] * 0xFFFF);
582 color.green = (unsigned short) (myBgColor.rgb[1] * 0xFFFF);
583 color.blue = (unsigned short) (myBgColor.rgb[2] * 0xFFFF);
584 color.flags = DoRed | DoGreen | DoBlue;
585 XAllocColor (aDisp, cmap, &color);
586
587 XSetWindowAttributes cwa;
588 cwa.colormap = cmap;
589 cwa.event_mask = StructureNotifyMask;
590 cwa.border_pixel = color.pixel;
591 cwa.background_pixel = color.pixel;
592
593 if (aVis->visualid == wattr.visual->visualid)
594 {
5e27df78 595 aWindow = aParent;
2166f0fa
SK
596 }
597 else
598 {
599 unsigned long mask = CWBackPixel | CWColormap | CWBorderPixel | CWEventMask;
5e27df78 600 aWindow = XCreateWindow (aDisp, aParent, 0, 0, myWidth, myHeight, 0/*bw*/, aVis->depth, InputOutput, aVis->visual, mask, &cwa);
2166f0fa
SK
601 }
602
5e27df78 603 XSetWindowBackground (aDisp, aWindow, cwa.background_pixel);
604 XClearWindow (aDisp, aWindow);
2166f0fa 605
5e27df78 606 if (aWindow != aParent)
2166f0fa
SK
607 {
608 XEvent anEvent;
5e27df78 609 XMapWindow (aDisp, aWindow);
610 XIfEvent (aDisp, &anEvent, WaitForNotify, (char* )aWindow);
2166f0fa 611 }
2166f0fa
SK
612 }
613
4e1523ef 614 myGlContext->Init ((Aspect_Drawable )aWindow, (Aspect_Display )aDisp, (Aspect_RenderingContext )aGContext, isCoreProfile);
2166f0fa 615#endif
5e27df78 616 myGlContext->Share (theShareCtx);
2166f0fa
SK
617
618 Init();
2166f0fa
SK
619}
620
621// =======================================================================
622// function : ~OpenGl_Window
623// purpose :
624// =======================================================================
625OpenGl_Window::~OpenGl_Window()
626{
25b97fac 627 if (!myOwnGContext
628 || myGlContext.IsNull())
fd4a6963 629 {
630 myGlContext.Nullify();
631 return;
632 }
633
634 // release "GL" context if it is owned by window
544da5a4 635 // Mesa implementation can fail to destroy GL context if it set for current thread.
636 // It should be safer to unset thread GL context before its destruction.
25b97fac 637#if defined(HAVE_EGL) || defined(__ANDROID__)
638 if ((EGLSurface )myGlContext->myWindow != EGL_NO_SURFACE)
639 {
640 eglDestroySurface ((EGLDisplay )myGlContext->myDisplay,
641 (EGLSurface )myGlContext->myWindow);
642 }
643#elif defined(_WIN32)
fd4a6963 644 HWND aWindow = (HWND )myGlContext->myWindow;
645 HDC aWindowDC = (HDC )myGlContext->myWindowDC;
646 HGLRC aWindowGContext = (HGLRC )myGlContext->myGContext;
544da5a4 647 HGLRC aThreadGContext = wglGetCurrentContext();
5e27df78 648 myGlContext.Nullify();
2166f0fa 649
544da5a4 650 if (aThreadGContext != NULL)
2166f0fa 651 {
544da5a4 652 if (aThreadGContext == aWindowGContext)
2166f0fa 653 {
fd4a6963 654 wglMakeCurrent (NULL, NULL);
2166f0fa 655 }
fd4a6963 656
657 wglDeleteContext (aWindowGContext);
2166f0fa 658 }
fd4a6963 659 ReleaseDC (aWindow, aWindowDC);
5e27df78 660#else
544da5a4 661 Display* aDisplay = (Display* )myGlContext->myDisplay;
662 GLXContext aWindowGContext = (GLXContext )myGlContext->myGContext;
663 GLXContext aThreadGContext = glXGetCurrentContext();
5e27df78 664 myGlContext.Nullify();
665
fd4a6963 666 if (aDisplay != NULL)
2166f0fa 667 {
544da5a4 668 if (aThreadGContext == aWindowGContext)
669 {
670 glXMakeCurrent (aDisplay, None, NULL);
671 }
672
5e27df78 673 // FSXXX sync necessary if non-direct rendering
674 glXWaitGL();
544da5a4 675 glXDestroyContext (aDisplay, aWindowGContext);
2166f0fa
SK
676 }
677#endif
678}
679
4fe56619 680#endif // !__APPLE__
681
2166f0fa
SK
682// =======================================================================
683// function : Activate
684// purpose :
685// =======================================================================
686Standard_Boolean OpenGl_Window::Activate()
687{
86fa64d9 688 return myGlContext->MakeCurrent();
2166f0fa
SK
689}
690
4fe56619 691#if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
692
2166f0fa
SK
693// =======================================================================
694// function : Resize
695// purpose : call_subr_resize
696// =======================================================================
697void OpenGl_Window::Resize (const CALL_DEF_WINDOW& theCWindow)
698{
25b97fac 699#if !defined(_WIN32) && !defined(HAVE_EGL) && !defined(__ANDROID__)
73192b37 700 Display* aDisp = (Display* )myGlContext->myDisplay;
2166f0fa
SK
701 if (aDisp == NULL)
702 return;
73192b37 703#endif
2166f0fa
SK
704
705 // If the size is not changed - do nothing
706 if ((myWidth == theCWindow.dx) && (myHeight == theCWindow.dy))
707 return;
708
e3573bb9 709 myWidth = theCWindow.dx;
710 myHeight = theCWindow.dy;
2166f0fa 711
25b97fac 712#if !defined(_WIN32) && !defined(HAVE_EGL) && !defined(__ANDROID__)
5e27df78 713 XResizeWindow (aDisp, myGlContext->myWindow, (unsigned int )myWidth, (unsigned int )myHeight);
2166f0fa
SK
714 XSync (aDisp, False);
715#endif
716
717 Init();
718}
719
4fe56619 720#endif // !__APPLE__
721
2166f0fa
SK
722// =======================================================================
723// function : ReadDepths
724// purpose : TelReadDepths
725// =======================================================================
726void OpenGl_Window::ReadDepths (const Standard_Integer theX, const Standard_Integer theY,
727 const Standard_Integer theWidth, const Standard_Integer theHeight,
728 float* theDepths)
729{
730 if (theDepths == NULL || !Activate())
731 return;
732
c827ea3a 733 OpenGl_Mat4 aProjectMat;
734 OpenGl_Utils::Ortho2D<Standard_ShortReal> (aProjectMat,
735 0.f, static_cast<GLfloat> (myWidth), 0.f, static_cast<GLfloat> (myHeight));
736
737 myGlContext->WorldViewState.Push();
738 myGlContext->ProjectionState.Push();
2166f0fa 739
c827ea3a 740 myGlContext->WorldViewState.SetIdentity();
741 myGlContext->ProjectionState.SetCurrent (aProjectMat);
742
743 myGlContext->ApplyProjectionMatrix();
744 myGlContext->ApplyWorldViewMatrix();
745
746#if !defined(GL_ES_VERSION_2_0)
2166f0fa
SK
747 glRasterPos2i (theX, theY);
748 DisableFeatures();
749 glReadPixels (theX, theY, theWidth, theHeight, GL_DEPTH_COMPONENT, GL_FLOAT, theDepths);
750 EnableFeatures();
ca3c13d1 751#endif
c827ea3a 752
753 myGlContext->WorldViewState.Pop();
754 myGlContext->ProjectionState.Pop();
755
756 myGlContext->ApplyProjectionMatrix();
2166f0fa
SK
757}
758
759// =======================================================================
760// function : SetBackgroundColor
761// purpose : call_subr_set_background
762// =======================================================================
763void OpenGl_Window::SetBackgroundColor (const Standard_ShortReal theR,
764 const Standard_ShortReal theG,
765 const Standard_ShortReal theB)
766{
767 myBgColor.rgb[0] = theR;
768 myBgColor.rgb[1] = theG;
769 myBgColor.rgb[2] = theB;
770}
771
4fe56619 772#if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
773
2166f0fa
SK
774// =======================================================================
775// function : Init
776// purpose :
777// =======================================================================
778void OpenGl_Window::Init()
779{
780 if (!Activate())
781 return;
782
25b97fac 783#if defined(HAVE_EGL) || defined(__ANDROID__)
e3573bb9 784 eglQuerySurface ((EGLDisplay )myGlContext->myDisplay, (EGLSurface )myGlContext->myWindow, EGL_WIDTH, &myWidth);
785 eglQuerySurface ((EGLDisplay )myGlContext->myDisplay, (EGLSurface )myGlContext->myWindow, EGL_HEIGHT, &myHeight);
25b97fac 786#elif defined(_WIN32)
2166f0fa 787 RECT cr;
5e27df78 788 GetClientRect ((HWND )myGlContext->myWindow, &cr);
2166f0fa
SK
789 myWidth = cr.right - cr.left;
790 myHeight = cr.bottom - cr.top;
791#else
792 Window aRootWin;
793 int aDummy;
794 unsigned int aDummyU;
795 unsigned int aNewWidth = 0;
796 unsigned int aNewHeight = 0;
73192b37 797 Display* aDisp = (Display* )myGlContext->myDisplay;
5e27df78 798 XGetGeometry (aDisp, myGlContext->myWindow, &aRootWin, &aDummy, &aDummy, &aNewWidth, &aNewHeight, &aDummyU, &aDummyU);
2166f0fa
SK
799 myWidth = aNewWidth;
800 myHeight = aNewHeight;
801#endif
802
5bd9ed93 803 glDisable (GL_DITHER);
ca3c13d1 804 glDisable (GL_SCISSOR_TEST);
e3573bb9 805 glViewport (0, 0, myWidth, myHeight);
ca3c13d1 806#if !defined(GL_ES_VERSION_2_0)
2166f0fa 807 glDrawBuffer (GL_BACK);
4e1523ef 808 if (myGlContext->core11 != NULL)
809 {
810 glMatrixMode (GL_MODELVIEW);
811 }
ca3c13d1 812#endif
2166f0fa
SK
813}
814
4fe56619 815#endif // !__APPLE__
816
2166f0fa
SK
817// =======================================================================
818// function : EnableFeatures
819// purpose :
820// =======================================================================
821void OpenGl_Window::EnableFeatures() const
822{
73192b37 823 //
2166f0fa
SK
824}
825
826// =======================================================================
827// function : DisableFeatures
828// purpose :
829// =======================================================================
830void OpenGl_Window::DisableFeatures() const
831{
ca3c13d1 832#if !defined(GL_ES_VERSION_2_0)
2166f0fa 833 glPixelTransferi (GL_MAP_COLOR, GL_FALSE);
ca3c13d1 834#endif
2166f0fa
SK
835
836 /*
837 * Disable stuff that's likely to slow down glDrawPixels.
838 * (Omit as much of this as possible, when you know in advance
839 * that the OpenGL state will already be set correctly.)
840 */
5bd9ed93 841 glDisable(GL_DITHER);
2166f0fa
SK
842 glDisable(GL_BLEND);
843 glDisable(GL_DEPTH_TEST);
ca3c13d1 844 glDisable(GL_TEXTURE_2D);
845 glDisable(GL_STENCIL_TEST);
2166f0fa 846
ca3c13d1 847#if !defined(GL_ES_VERSION_2_0)
848 glDisable(GL_LIGHTING);
849 glDisable(GL_ALPHA_TEST);
850 glDisable(GL_FOG);
2166f0fa 851 glDisable(GL_LOGIC_OP);
2166f0fa 852 glDisable(GL_TEXTURE_1D);
ca3c13d1 853
2166f0fa
SK
854 glPixelTransferi(GL_MAP_COLOR, GL_FALSE);
855 glPixelTransferi(GL_RED_SCALE, 1);
856 glPixelTransferi(GL_RED_BIAS, 0);
857 glPixelTransferi(GL_GREEN_SCALE, 1);
858 glPixelTransferi(GL_GREEN_BIAS, 0);
859 glPixelTransferi(GL_BLUE_SCALE, 1);
860 glPixelTransferi(GL_BLUE_BIAS, 0);
861 glPixelTransferi(GL_ALPHA_SCALE, 1);
862 glPixelTransferi(GL_ALPHA_BIAS, 0);
863
864 /*
865 * Disable extensions that could slow down glDrawPixels.
866 * (Actually, you should check for the presence of the proper
867 * extension before making these calls. I've omitted that
868 * code for simplicity.)
869 */
870
b5ac8292 871 if ((myGlContext->myGlVerMajor >= 1) && (myGlContext->myGlVerMinor >= 2))
872 {
2166f0fa 873#ifdef GL_EXT_convolution
b5ac8292 874 if (myGlContext->CheckExtension ("GL_CONVOLUTION_1D_EXT"))
875 glDisable(GL_CONVOLUTION_1D_EXT);
876
877 if (myGlContext->CheckExtension ("GL_CONVOLUTION_2D_EXT"))
878 glDisable(GL_CONVOLUTION_2D_EXT);
879
880 if (myGlContext->CheckExtension ("GL_SEPARABLE_2D_EXT"))
881 glDisable(GL_SEPARABLE_2D_EXT);
2166f0fa
SK
882#endif
883
884#ifdef GL_EXT_histogram
b5ac8292 885 if (myGlContext->CheckExtension ("GL_SEPARABLE_2D_EXT"))
886 glDisable(GL_HISTOGRAM_EXT);
887
888 if (myGlContext->CheckExtension ("GL_MINMAX_EXT"))
889 glDisable(GL_MINMAX_EXT);
2166f0fa
SK
890#endif
891
892#ifdef GL_EXT_texture3D
b5ac8292 893 if (myGlContext->CheckExtension ("GL_TEXTURE_3D_EXT"))
894 glDisable(GL_TEXTURE_3D_EXT);
2166f0fa 895#endif
b5ac8292 896 }
ca3c13d1 897#endif
2166f0fa
SK
898}
899
900// =======================================================================
901// function : MakeFrontBufCurrent
902// purpose : TelMakeFrontBufCurrent
903// =======================================================================
904void OpenGl_Window::MakeFrontBufCurrent() const
905{
ca3c13d1 906#if !defined(GL_ES_VERSION_2_0)
2166f0fa 907 glDrawBuffer (GL_FRONT);
ca3c13d1 908#endif
2166f0fa
SK
909}
910
911// =======================================================================
912// function : MakeBackBufCurrent
913// purpose : TelMakeBackBufCurrent
914// =======================================================================
915void OpenGl_Window::MakeBackBufCurrent() const
916{
ca3c13d1 917#if !defined(GL_ES_VERSION_2_0)
2166f0fa 918 glDrawBuffer (GL_BACK);
ca3c13d1 919#endif
2166f0fa 920}