1 // Created on: 2011-09-20
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <OpenGl_GlCore12.hxx>
18 #include <OpenGl_Context.hxx>
19 #include <OpenGl_GraphicDriver.hxx>
20 #include <OpenGl_Window.hxx>
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>
28 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Window,MMgt_TShared)
35 #if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
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
88 #define WGL_NO_ACCELERATION_ARB 0x2025
89 #define WGL_GENERIC_ACCELERATION_ARB 0x2026
90 #define WGL_FULL_ACCELERATION_ARB 0x2027
92 #define WGL_SWAP_EXCHANGE_ARB 0x2028
93 #define WGL_SWAP_COPY_ARB 0x2029
94 #define WGL_SWAP_UNDEFINED_ARB 0x202A
96 #define WGL_TYPE_RGBA_ARB 0x202B
97 #define WGL_TYPE_COLORINDEX_ARB 0x202C
98 #endif // WGL_NUMBER_PIXEL_FORMATS_ARB
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
108 // WGL_CONTEXT_FLAGS bits
109 #define WGL_CONTEXT_DEBUG_BIT_ARB 0x0001
110 #define WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB 0x0002
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
117 static LRESULT CALLBACK wndProcDummy (HWND theWin, UINT theMsg, WPARAM theParamW, LPARAM theParamL)
119 return DefWindowProcW (theWin, theMsg, theParamW, theParamL);
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
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
137 //! Dummy XError handler which just skips errors
138 static int xErrorDummyHandler (Display* /*theDisplay*/,
139 XErrorEvent* /*theErrorEvent*/)
144 //! Auxiliary method to format list.
145 static void addMsgToList (TCollection_ExtendedString& theList,
146 const TCollection_ExtendedString& theMsg)
148 if (!theList.IsEmpty())
158 // =======================================================================
159 // function : OpenGl_Window
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)
172 myPlatformWindow->Size (myWidth, myHeight);
174 Standard_Boolean isCoreProfile = Standard_False;
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)
184 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window, EGL does not provide compatible configurations!");
188 EGLSurface anEglSurf = EGL_NO_SURFACE;
189 if (theGContext == (EGLContext )EGL_NO_CONTEXT)
191 // create new surface
192 anEglSurf = eglCreateWindowSurface (anEglDisplay,
194 (EGLNativeWindowType )myPlatformWindow->NativeHandle(),
196 if (anEglSurf == EGL_NO_SURFACE)
198 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window, EGL is unable to create surface for window!");
202 else if (theGContext != anEglContext)
204 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window, EGL is used in unsupported combination!");
209 anEglSurf = eglGetCurrentSurface(EGL_DRAW);
210 if (anEglSurf == EGL_NO_SURFACE)
212 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window, EGL is unable to retrieve current surface!");
217 myGlContext->Init ((Aspect_Drawable )anEglSurf, (Aspect_Display )anEglDisplay, (Aspect_RenderingContext )anEglContext, isCoreProfile);
218 #elif defined(_WIN32)
220 HWND aWindow = (HWND )myPlatformWindow->NativeHandle();
221 HDC aWindowDC = GetDC (aWindow);
222 HGLRC aGContext = (HGLRC )theGContext;
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)
236 aPixelFrmt.dwFlags |= PFD_STEREO;
239 int aPixelFrmtId = ChoosePixelFormat (aWindowDC, &aPixelFrmt);
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)
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,
250 0, GL_DEBUG_SEVERITY_HIGH, aMsg);
252 aPixelFrmt.dwFlags &= ~PFD_STEREO;
253 aPixelFrmtId = ChoosePixelFormat (aWindowDC, &aPixelFrmt);
256 if (aPixelFrmtId == 0)
258 ReleaseDC (aWindow, aWindowDC);
260 TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: ChoosePixelFormat failed. Error code: ");
261 aMsg += (int )GetLastError();
262 Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
266 DescribePixelFormat (aWindowDC, aPixelFrmtId, sizeof(aPixelFrmt), &aPixelFrmt);
268 HGLRC aSlaveCtx = !theShareCtx.IsNull() ? (HGLRC )theShareCtx->myGContext : NULL;
269 if (aGContext == NULL)
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";
279 HDC aDevCtxTmp = NULL;
280 HGLRC aRendCtxTmp = NULL;
281 if ((!theCaps->contextDebug && !theCaps->contextNoAccel && theCaps->contextCompatible)
282 || RegisterClassW (&aClass) == 0)
284 aClass.lpszClassName = NULL;
286 if (aClass.lpszClassName != NULL)
288 DWORD anExStyle = WS_EX_TOOLWINDOW | WS_EX_WINDOWEDGE;
289 #if (_WIN32_WINNT >= 0x0500)
290 anExStyle |= WS_EX_NOACTIVATE;
292 aWinTmp = CreateWindowExW(anExStyle,
293 aClass.lpszClassName, L"OpenGl_WindowTmp",
294 WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_DISABLED,
296 NULL, NULL, aModule, NULL);
300 aDevCtxTmp = GetDC (aWinTmp);
301 SetPixelFormat (aDevCtxTmp, aPixelFrmtId, &aPixelFrmt);
302 aRendCtxTmp = wglCreateContext (aDevCtxTmp);
305 typedef BOOL (WINAPI *wglChoosePixelFormatARB_t)(HDC theDevCtx,
306 const int* theIntAttribs,
307 const float* theFloatAttribs,
308 unsigned int theMaxFormats,
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)
318 wglMakeCurrent (aDevCtxTmp, aRendCtxTmp);
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"))
325 aChoosePixProc = (wglChoosePixelFormatARB_t )wglGetProcAddress ("wglChoosePixelFormatARB");
327 if (OpenGl_Context::CheckExtension (aWglExts, "WGL_ARB_create_context_profile"))
329 aCreateCtxProc = (wglCreateContextAttribsARB_t )wglGetProcAddress ("wglCreateContextAttribsARB");
333 // choose extended pixel format
334 if (aChoosePixProc != NULL)
336 const int aPixAttribs[] =
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,
351 unsigned int aFrmtsNb = 0;
352 aChoosePixProc (aWindowDC, aPixAttribs, NULL, 1, &aPixelFrmtId, &aFrmtsNb);
355 // setup pixel format - may be set only once per window
356 if (!SetPixelFormat (aWindowDC, aPixelFrmtId, &aPixelFrmt))
358 ReleaseDC (aWindow, aWindowDC);
360 TCollection_AsciiString aMsg("OpenGl_Window::CreateWindow: SetPixelFormat failed. Error code: ");
361 aMsg += (int )GetLastError();
362 Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
366 // create GL context with extra options
367 if (aCreateCtxProc != NULL)
369 if (!theCaps->contextCompatible)
371 int aCoreCtxAttribs[] =
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,
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)
385 aCoreCtxAttribs[1] = 4;
386 aCoreCtxAttribs[3] = aLowVer4;
387 aGContext = aCreateCtxProc (aWindowDC, aSlaveCtx, aCoreCtxAttribs);
389 for (int aLowVer3 = 3; aLowVer3 >= 2 && aGContext == NULL; --aLowVer3)
391 aCoreCtxAttribs[1] = 3;
392 aCoreCtxAttribs[3] = aLowVer3;
393 aGContext = aCreateCtxProc (aWindowDC, aSlaveCtx, aCoreCtxAttribs);
395 isCoreProfile = aGContext != NULL;
398 if (aGContext == NULL)
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,
408 isCoreProfile = Standard_False;
409 aGContext = aCreateCtxProc (aWindowDC, aSlaveCtx, aCtxAttribs);
411 if (aGContext != NULL
412 && !theCaps->contextCompatible)
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);
419 if (aGContext != NULL)
425 if (aRendCtxTmp != NULL)
427 wglDeleteContext (aRendCtxTmp);
429 if (aDevCtxTmp != NULL)
431 ReleaseDC (aWinTmp, aDevCtxTmp);
435 DestroyWindow (aWinTmp);
437 if (aClass.lpszClassName != NULL)
439 UnregisterClassW (aClass.lpszClassName, aModule);
442 if (aGContext == NULL)
444 // create context using obsolete functionality
445 aGContext = wglCreateContext (aWindowDC);
447 if (aGContext == NULL)
449 ReleaseDC (aWindow, aWindowDC);
451 TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: wglCreateContext failed. Error code: ");
452 aMsg += (int )GetLastError();
453 Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
458 // all GL context within one OpenGl_GraphicDriver should be shared!
459 if (aSlaveCtx != NULL && wglShareLists (aSlaveCtx, aGContext) != TRUE)
461 TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: wglShareLists failed. Error code: ");
462 aMsg += (int )GetLastError();
463 Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
467 myGlContext->Init ((Aspect_Handle )aWindow, (Aspect_Handle )aWindowDC, (Aspect_RenderingContext )aGContext, isCoreProfile);
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;
474 XWindowAttributes aWinAttribs;
475 XGetWindowAttributes (aDisp, aWindow, &aWinAttribs);
476 XVisualInfo aVisInfo;
477 aVisInfo.visualid = aWinAttribs.visual->visualid;
478 aVisInfo.screen = DefaultScreen (aDisp);
480 XVisualInfo* aVis = XGetVisualInfo (aDisp, VisualIDMask | VisualScreenMask, &aVisInfo, &aNbItems);
484 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window::CreateWindow: XGetVisualInfo is unable to choose needed configuration in existing OpenGL context. ");
487 else if (glXGetConfig (aDisp, aVis, GLX_USE_GL, &isGl) != 0 || !isGl)
489 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window::CreateWindow: window Visual does not support GL rendering!");
493 // create new context
494 GLXFBConfig anFBConfig = myPlatformWindow->NativeFBConfig();
495 const char* aGlxExts = glXQueryExtensionsString (aDisp, aVisInfo.screen);
497 && anFBConfig != NULL
498 && OpenGl_Context::CheckExtension (aGlxExts, "GLX_ARB_create_context_profile"))
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);
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)
511 int aCoreCtxAttribs[] =
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,
520 // try to create the core profile of highest OpenGL version supported by OCCT
521 for (int aLowVer4 = 5; aLowVer4 >= 0 && aGContext == NULL; --aLowVer4)
523 aCoreCtxAttribs[1] = 4;
524 aCoreCtxAttribs[3] = aLowVer4;
525 aGContext = aCreateCtxProc (aDisp, anFBConfig, aSlaveCtx, True, aCoreCtxAttribs);
527 for (int aLowVer3 = 3; aLowVer3 >= 2 && aGContext == NULL; --aLowVer3)
529 aCoreCtxAttribs[1] = 3;
530 aCoreCtxAttribs[3] = aLowVer3;
531 aGContext = aCreateCtxProc (aDisp, anFBConfig, aSlaveCtx, True, aCoreCtxAttribs);
533 isCoreProfile = aGContext != NULL;
536 if (aGContext == NULL)
540 GLX_CONTEXT_FLAGS_ARB, theCaps->contextDebug ? GLX_CONTEXT_DEBUG_BIT_ARB : 0,
543 isCoreProfile = Standard_False;
544 aGContext = aCreateCtxProc (aDisp, anFBConfig, aSlaveCtx, True, aCtxAttribs);
546 if (aGContext != NULL
547 && !theCaps->contextCompatible)
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);
553 XSetErrorHandler(anOldHandler);
557 && aGContext == NULL)
559 aGContext = glXCreateContext (aDisp, aVis, aSlaveCtx, GL_TRUE);
560 if (aGContext == NULL)
562 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window::CreateWindow: glXCreateContext failed.");
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)
582 addMsgToList (aList, "no Quad Buffer");
584 else if (!theCaps->contextStereo && isStereo == 1)
586 addMsgToList (aList, "extra Quad Buffer");
588 if (!aList.IsEmpty())
590 TCollection_ExtendedString aMsg = TCollection_ExtendedString ("OpenGl_Window::CreateWindow: window Visual is incomplete: ") + aList;
591 myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
593 0, GL_DEBUG_SEVERITY_MEDIUM, aMsg);
596 myGlContext->Init ((Aspect_Drawable )aWindow, (Aspect_Display )aDisp, (Aspect_RenderingContext )aGContext, isCoreProfile);
598 myGlContext->Share (theShareCtx);
599 myGlContext->SetSwapInterval (mySwapInterval);
603 // =======================================================================
604 // function : ~OpenGl_Window
606 // =======================================================================
607 OpenGl_Window::~OpenGl_Window()
610 || myGlContext.IsNull())
612 myGlContext.Nullify();
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)
622 eglDestroySurface ((EGLDisplay )myGlContext->myDisplay,
623 (EGLSurface )myGlContext->myWindow);
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();
632 if (aThreadGContext != NULL)
634 if (aThreadGContext == aWindowGContext)
636 wglMakeCurrent (NULL, NULL);
639 wglDeleteContext (aWindowGContext);
641 ReleaseDC (aWindow, aWindowDC);
643 Display* aDisplay = (Display* )myGlContext->myDisplay;
644 GLXContext aWindowGContext = (GLXContext )myGlContext->myGContext;
645 GLXContext aThreadGContext = glXGetCurrentContext();
646 myGlContext.Nullify();
648 if (aDisplay != NULL)
650 if (aThreadGContext == aWindowGContext)
652 glXMakeCurrent (aDisplay, None, NULL);
655 // FSXXX sync necessary if non-direct rendering
657 glXDestroyContext (aDisplay, aWindowGContext);
664 // =======================================================================
665 // function : Activate
667 // =======================================================================
668 Standard_Boolean OpenGl_Window::Activate()
670 return myGlContext->MakeCurrent();
673 #if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
675 // =======================================================================
677 // purpose : call_subr_resize
678 // =======================================================================
679 void OpenGl_Window::Resize()
681 #if !defined(_WIN32) && !defined(HAVE_EGL)
682 Display* aDisp = (Display* )myGlContext->myDisplay;
687 Standard_Integer aWidth = 0;
688 Standard_Integer aHeight = 0;
689 myPlatformWindow->Size (aWidth, aHeight);
691 // If the size is not changed - do nothing
692 if ((myWidth == aWidth) && (myHeight == aHeight))
698 #if !defined(_WIN32) && !defined(HAVE_EGL)
699 XResizeWindow (aDisp, myGlContext->myWindow, (unsigned int )myWidth, (unsigned int )myHeight);
700 XSync (aDisp, False);
706 // =======================================================================
709 // =======================================================================
710 void OpenGl_Window::Init()
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)
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);
729 myHeight = aNewHeight;
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)
740 glMatrixMode (GL_MODELVIEW);
745 // =======================================================================
746 // function : SetSwapInterval
748 // =======================================================================
749 void OpenGl_Window::SetSwapInterval()
751 if (mySwapInterval != myGlContext->caps->swapInterval)
753 mySwapInterval = myGlContext->caps->swapInterval;
754 myGlContext->SetSwapInterval (mySwapInterval);