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>
17 #include <InterfaceGraphic.hxx>
19 #include <OpenGl_Context.hxx>
20 #include <OpenGl_GraphicDriver.hxx>
21 #include <OpenGl_Window.hxx>
22 #include <OpenGl_Utils.hxx>
24 #include <Aspect_GraphicDeviceDefinitionError.hxx>
25 #include <TCollection_AsciiString.hxx>
26 #include <TCollection_ExtendedString.hxx>
28 #if defined(HAVE_EGL) || defined(__ANDROID__)
32 IMPLEMENT_STANDARD_HANDLE(OpenGl_Window,MMgt_TShared)
33 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Window,MMgt_TShared)
35 #if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
39 static const TEL_COLOUR THE_DEFAULT_BG_COLOR = { { 0.F, 0.F, 0.F, 1.F } };
41 #if defined(HAVE_EGL) || defined(__ANDROID__)
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
89 #define WGL_NO_ACCELERATION_ARB 0x2025
90 #define WGL_GENERIC_ACCELERATION_ARB 0x2026
91 #define WGL_FULL_ACCELERATION_ARB 0x2027
93 #define WGL_SWAP_EXCHANGE_ARB 0x2028
94 #define WGL_SWAP_COPY_ARB 0x2029
95 #define WGL_SWAP_UNDEFINED_ARB 0x202A
97 #define WGL_TYPE_RGBA_ARB 0x202B
98 #define WGL_TYPE_COLORINDEX_ARB 0x202C
99 #endif // WGL_NUMBER_PIXEL_FORMATS_ARB
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
109 // WGL_CONTEXT_FLAGS bits
110 #define WGL_CONTEXT_DEBUG_BIT_ARB 0x0001
111 #define WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB 0x0002
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
118 static LRESULT CALLBACK wndProcDummy (HWND theWin, UINT theMsg, WPARAM theParamW, LPARAM theParamL)
120 return DefWindowProcW (theWin, theMsg, theParamW, theParamL);
123 static Bool WaitForNotify (Display* theDisp, XEvent* theEv, char* theArg)
125 return (theEv->type == MapNotify) && (theEv->xmap.window == (Window )theArg);
131 // =======================================================================
132 // function : OpenGl_Window
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 mySwapInterval (theCaps->swapInterval)
147 myBgColor.rgb[0] = theCWindow.Background.r;
148 myBgColor.rgb[1] = theCWindow.Background.g;
149 myBgColor.rgb[2] = theCWindow.Background.b;
151 Standard_Boolean isCoreProfile = Standard_False;
153 #if defined(HAVE_EGL) || defined(__ANDROID__)
154 EGLDisplay anEglDisplay = (EGLDisplay )theDriver->getRawGlDisplay();
155 EGLContext anEglContext = (EGLContext )theDriver->getRawGlContext();
156 EGLConfig anEglConfig = (EGLConfig )theDriver->getRawGlConfig();
157 if (anEglDisplay == EGL_NO_DISPLAY
158 || anEglContext == EGL_NO_CONTEXT
159 || anEglConfig == NULL)
161 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window, EGL does not provide compatible configurations!");
165 EGLSurface anEglSurf = EGL_NO_SURFACE;
166 if (theGContext == (EGLContext )EGL_NO_CONTEXT)
168 // create new surface
169 anEglSurf = eglCreateWindowSurface (anEglDisplay, anEglConfig, (EGLNativeWindowType )theCWindow.XWindow, NULL);
170 if (anEglSurf == EGL_NO_SURFACE)
172 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window, EGL is unable to create surface for window!");
176 else if (theGContext != anEglContext)
178 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window, EGL is used in unsupported combination!");
183 anEglSurf = eglGetCurrentSurface(EGL_DRAW);
184 if (anEglSurf == EGL_NO_SURFACE)
186 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window, EGL is unable to retrieve current surface!");
191 myGlContext->Init ((Aspect_Drawable )anEglSurf, (Aspect_Display )anEglDisplay, (Aspect_RenderingContext )anEglContext, isCoreProfile);
192 #elif defined(_WIN32)
194 HWND aWindow = (HWND )theCWindow.XWindow;
195 HDC aWindowDC = GetDC (aWindow);
196 HGLRC aGContext = (HGLRC )theGContext;
198 PIXELFORMATDESCRIPTOR aPixelFrmt;
199 memset (&aPixelFrmt, 0, sizeof(aPixelFrmt));
200 aPixelFrmt.nSize = sizeof(PIXELFORMATDESCRIPTOR);
201 aPixelFrmt.nVersion = 1;
202 aPixelFrmt.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
203 aPixelFrmt.iPixelType = PFD_TYPE_RGBA;
204 aPixelFrmt.cColorBits = 24;
205 aPixelFrmt.cDepthBits = 24;
206 aPixelFrmt.cStencilBits = 8;
207 aPixelFrmt.iLayerType = PFD_MAIN_PLANE;
208 if (theCaps->contextStereo)
210 aPixelFrmt.dwFlags |= PFD_STEREO;
213 int aPixelFrmtId = ChoosePixelFormat (aWindowDC, &aPixelFrmt);
215 // in case of failure try without stereo if any
216 const Standard_Boolean hasStereo = aPixelFrmtId != 0 && theCaps->contextStereo;
217 if (aPixelFrmtId == 0 && theCaps->contextStereo)
219 TCollection_ExtendedString aMsg ("OpenGl_Window::CreateWindow: "
220 "ChoosePixelFormat is unable to find stereo supported pixel format. "
221 "Choosing similar non stereo format.");
222 myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
223 GL_DEBUG_TYPE_OTHER_ARB,
224 0, GL_DEBUG_SEVERITY_HIGH_ARB, aMsg);
226 aPixelFrmt.dwFlags &= ~PFD_STEREO;
227 aPixelFrmtId = ChoosePixelFormat (aWindowDC, &aPixelFrmt);
230 if (aPixelFrmtId == 0)
232 ReleaseDC (aWindow, aWindowDC);
234 TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: ChoosePixelFormat failed. Error code: ");
235 aMsg += (int )GetLastError();
236 Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
240 DescribePixelFormat (aWindowDC, aPixelFrmtId, sizeof(aPixelFrmt), &aPixelFrmt);
242 HGLRC aSlaveCtx = !theShareCtx.IsNull() ? (HGLRC )theShareCtx->myGContext : NULL;
243 if (aGContext == NULL)
245 // create temporary context to retrieve advanced context creation procedures
246 HMODULE aModule = GetModuleHandleW(NULL);
247 WNDCLASSW aClass; memset (&aClass, 0, sizeof(aClass));
248 aClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
249 aClass.lpfnWndProc = wndProcDummy;
250 aClass.hInstance = aModule;
251 aClass.lpszClassName = L"OpenGl_WindowTmp";
253 HDC aDevCtxTmp = NULL;
254 HGLRC aRendCtxTmp = NULL;
255 if ((!theCaps->contextDebug && !theCaps->contextNoAccel && theCaps->contextCompatible)
256 || RegisterClassW (&aClass) == 0)
258 aClass.lpszClassName = NULL;
260 if (aClass.lpszClassName != NULL)
262 DWORD anExStyle = WS_EX_TOOLWINDOW | WS_EX_WINDOWEDGE;
263 #if (_WIN32_WINNT >= 0x0500)
264 anExStyle |= WS_EX_NOACTIVATE;
266 aWinTmp = CreateWindowExW(anExStyle,
267 aClass.lpszClassName, L"OpenGl_WindowTmp",
268 WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_DISABLED,
270 NULL, NULL, aModule, NULL);
274 aDevCtxTmp = GetDC (aWinTmp);
275 SetPixelFormat (aDevCtxTmp, aPixelFrmtId, &aPixelFrmt);
276 aRendCtxTmp = wglCreateContext (aDevCtxTmp);
279 typedef BOOL (WINAPI *wglChoosePixelFormatARB_t)(HDC theDevCtx,
280 const int* theIntAttribs,
281 const float* theFloatAttribs,
282 unsigned int theMaxFormats,
284 unsigned int* theNumFormatsOut);
285 typedef HGLRC (WINAPI *wglCreateContextAttribsARB_t)(HDC theDevCtx,
286 HGLRC theShareContext,
287 const int* theAttribs);
288 wglChoosePixelFormatARB_t aChoosePixProc = NULL;
289 wglCreateContextAttribsARB_t aCreateCtxProc = NULL;
290 if (aRendCtxTmp != NULL)
292 wglMakeCurrent (aDevCtxTmp, aRendCtxTmp);
294 typedef const char* (WINAPI *wglGetExtensionsStringARB_t)(HDC theDeviceContext);
295 wglGetExtensionsStringARB_t aGetExtensions = (wglGetExtensionsStringARB_t )wglGetProcAddress ("wglGetExtensionsStringARB");
296 const char* aWglExts = (aGetExtensions != NULL) ? aGetExtensions (wglGetCurrentDC()) : NULL;
297 if (OpenGl_Context::CheckExtension (aWglExts, "WGL_ARB_pixel_format"))
299 aChoosePixProc = (wglChoosePixelFormatARB_t )wglGetProcAddress ("wglChoosePixelFormatARB");
301 if (OpenGl_Context::CheckExtension (aWglExts, "WGL_ARB_create_context_profile"))
303 aCreateCtxProc = (wglCreateContextAttribsARB_t )wglGetProcAddress ("wglCreateContextAttribsARB");
307 // choose extended pixel format
308 if (aChoosePixProc != NULL)
310 const int aPixAttribs[] =
312 WGL_DRAW_TO_WINDOW_ARB, GL_TRUE,
313 WGL_SUPPORT_OPENGL_ARB, GL_TRUE,
314 WGL_DOUBLE_BUFFER_ARB, GL_TRUE,
315 WGL_STEREO_ARB, hasStereo ? GL_TRUE : GL_FALSE,
316 WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB,
317 //WGL_SAMPLE_BUFFERS_ARB, 1,
318 //WGL_SAMPLES_ARB, 8,
319 WGL_COLOR_BITS_ARB, 24,
320 WGL_DEPTH_BITS_ARB, 24,
321 WGL_STENCIL_BITS_ARB, 8,
322 WGL_ACCELERATION_ARB, theCaps->contextNoAccel ? WGL_NO_ACCELERATION_ARB : WGL_FULL_ACCELERATION_ARB,
325 unsigned int aFrmtsNb = 0;
326 aChoosePixProc (aWindowDC, aPixAttribs, NULL, 1, &aPixelFrmtId, &aFrmtsNb);
329 // setup pixel format - may be set only once per window
330 if (!SetPixelFormat (aWindowDC, aPixelFrmtId, &aPixelFrmt))
332 ReleaseDC (aWindow, aWindowDC);
334 TCollection_AsciiString aMsg("OpenGl_Window::CreateWindow: SetPixelFormat failed. Error code: ");
335 aMsg += (int )GetLastError();
336 Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
340 // create GL context with extra options
341 if (aCreateCtxProc != NULL)
343 if (!theCaps->contextCompatible)
345 int aCoreCtxAttribs[] =
347 WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
348 WGL_CONTEXT_MINOR_VERSION_ARB, 2,
349 WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
350 WGL_CONTEXT_FLAGS_ARB, theCaps->contextDebug ? WGL_CONTEXT_DEBUG_BIT_ARB : 0,
354 // Try to create the core profile of highest OpenGL version supported by OCCT
355 // (this will be done automatically by some drivers when requesting 3.2,
356 // but some will not (e.g. AMD Catalyst) since WGL_ARB_create_context_profile specification allows both implementations).
357 for (int aLowVer4 = 5; aLowVer4 >= 0 && aGContext == NULL; --aLowVer4)
359 aCoreCtxAttribs[1] = 4;
360 aCoreCtxAttribs[3] = aLowVer4;
361 aGContext = aCreateCtxProc (aWindowDC, aSlaveCtx, aCoreCtxAttribs);
363 for (int aLowVer3 = 3; aLowVer3 >= 2 && aGContext == NULL; --aLowVer3)
365 aCoreCtxAttribs[1] = 3;
366 aCoreCtxAttribs[3] = aLowVer3;
367 aGContext = aCreateCtxProc (aWindowDC, aSlaveCtx, aCoreCtxAttribs);
369 isCoreProfile = aGContext != NULL;
372 if (aGContext == NULL)
376 // Beware! NVIDIA drivers reject context creation when WGL_CONTEXT_PROFILE_MASK_ARB are specified
377 // but not WGL_CONTEXT_MAJOR_VERSION_ARB/WGL_CONTEXT_MINOR_VERSION_ARB.
378 //WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB,
379 WGL_CONTEXT_FLAGS_ARB, theCaps->contextDebug ? WGL_CONTEXT_DEBUG_BIT_ARB : 0,
382 isCoreProfile = Standard_False;
383 aGContext = aCreateCtxProc (aWindowDC, aSlaveCtx, aCtxAttribs);
385 if (aGContext != NULL
386 && !theCaps->contextCompatible)
388 TCollection_ExtendedString aMsg("OpenGl_Window::CreateWindow: core profile creation failed.");
389 myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB, GL_DEBUG_TYPE_PORTABILITY_ARB, 0, GL_DEBUG_SEVERITY_LOW_ARB, aMsg);
393 if (aGContext != NULL)
399 if (aRendCtxTmp != NULL)
401 wglDeleteContext (aRendCtxTmp);
403 if (aDevCtxTmp != NULL)
405 ReleaseDC (aWinTmp, aDevCtxTmp);
409 DestroyWindow (aWinTmp);
411 if (aClass.lpszClassName != NULL)
413 UnregisterClassW (aClass.lpszClassName, aModule);
416 if (aGContext == NULL)
418 // create context using obsolete functionality
419 aGContext = wglCreateContext (aWindowDC);
421 if (aGContext == NULL)
423 ReleaseDC (aWindow, aWindowDC);
425 TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: wglCreateContext failed. Error code: ");
426 aMsg += (int )GetLastError();
427 Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
432 // all GL context within one OpenGl_GraphicDriver should be shared!
433 if (aSlaveCtx != NULL && wglShareLists (aSlaveCtx, aGContext) != TRUE)
435 TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: wglShareLists failed. Error code: ");
436 aMsg += (int )GetLastError();
437 Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
441 myGlContext->Init ((Aspect_Handle )aWindow, (Aspect_Handle )aWindowDC, (Aspect_RenderingContext )aGContext, isCoreProfile);
443 Window aParent = (Window )theCWindow.XWindow;
446 Display* aDisp = theDriver->GetDisplayConnection()->GetDisplay();
447 GLXContext aGContext = (GLXContext )theGContext;
449 XWindowAttributes wattr;
450 XGetWindowAttributes (aDisp, aParent, &wattr);
451 const int scr = DefaultScreen (aDisp);
453 XVisualInfo* aVis = NULL;
455 unsigned long aVisInfoMask = VisualIDMask | VisualScreenMask;
456 XVisualInfo aVisInfo;
457 aVisInfo.visualid = wattr.visual->visualid;
458 aVisInfo.screen = scr;
460 aVis = XGetVisualInfo (aDisp, aVisInfoMask, &aVisInfo, &aNbItems);
463 #if defined(__linux) || defined(Linux) || defined(__APPLE__)
466 // check Visual for OpenGl context's parameters compatibility
467 int isGl = 0, isDoubleBuffer = 0, isRGBA = 0, isStereo = 0;
468 int aDepthSize = 0, aStencilSize = 0;
470 if (glXGetConfig (aDisp, aVis, GLX_USE_GL, &isGl) != 0)
473 if (glXGetConfig (aDisp, aVis, GLX_RGBA, &isRGBA) != 0)
476 if (glXGetConfig (aDisp, aVis, GLX_DOUBLEBUFFER, &isDoubleBuffer) != 0)
479 if (glXGetConfig (aDisp, aVis, GLX_STEREO, &isStereo) != 0)
482 if (glXGetConfig (aDisp, aVis, GLX_DEPTH_SIZE, &aDepthSize) != 0)
485 if (glXGetConfig (aDisp, aVis, GLX_STENCIL_SIZE, &aStencilSize) != 0)
494 TCollection_ExtendedString aMsg ("OpenGl_Window::CreateWindow: window Visual does not support GL rendering!");
495 myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
496 GL_DEBUG_TYPE_OTHER_ARB,
497 0, GL_DEBUG_SEVERITY_HIGH_ARB, aMsg);
502 TCollection_ExtendedString aList;
505 if (!aList.IsEmpty()) aList += ", ";
506 aList += "no depth buffer";
508 if (aStencilSize < 1)
510 if (!aList.IsEmpty()) aList += ", ";
511 aList += "no stencil buffer";
515 if (!aList.IsEmpty()) aList += ", ";
516 aList += "no RGBA color buffer";
518 if (isDoubleBuffer == 0)
520 if (!aList.IsEmpty()) aList += ", ";
521 aList += "no Double Buffer";
523 if (theCaps->contextStereo && isStereo == 0)
525 if (!aList.IsEmpty()) aList += ", ";
526 aList += "no Quad Buffer";
528 if (!theCaps->contextStereo && isStereo == 1)
530 if (!aList.IsEmpty()) aList += ", ";
531 aList += "extra Quad Buffer";
533 if (!aList.IsEmpty())
535 TCollection_ExtendedString aMsg = TCollection_ExtendedString ("OpenGl_Window::CreateWindow: window Visual is incomplete: ") + aList;
541 myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
542 GL_DEBUG_TYPE_OTHER_ARB,
543 0, GL_DEBUG_SEVERITY_MEDIUM_ARB, aMsg);
553 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window::CreateWindow: XGetVisualInfo is unable to choose needed configuration in existing OpenGL context. ");
565 anAttribs[anIter++] = GLX_RGBA;
567 anAttribs[anIter++] = GLX_DEPTH_SIZE;
568 anAttribs[anIter++] = 1;
570 anAttribs[anIter++] = GLX_STENCIL_SIZE;
571 anAttribs[anIter++] = 1;
573 anAttribs[anIter++] = GLX_RED_SIZE;
574 anAttribs[anIter++] = (wattr.depth <= 8) ? 0 : 1;
576 anAttribs[anIter++] = GLX_GREEN_SIZE;
577 anAttribs[anIter++] = (wattr.depth <= 8) ? 0 : 1;
579 anAttribs[anIter++] = GLX_BLUE_SIZE;
580 anAttribs[anIter++] = (wattr.depth <= 8) ? 0 : 1;
582 anAttribs[anIter++] = GLX_DOUBLEBUFFER;
584 // warning: this flag may be set to None, so it need to be last in anAttribs
585 Standard_Integer aStereoFlagPos = anIter;
586 if (theCaps->contextStereo)
587 anAttribs[anIter++] = GLX_STEREO;
589 anAttribs[anIter++] = None;
591 aVis = glXChooseVisual (aDisp, scr, anAttribs);
593 // in case of failure try without stereo if any
594 if (aVis == NULL && theCaps->contextStereo)
596 TCollection_ExtendedString aMsg ("OpenGl_Window::CreateWindow: "
597 "glXChooseVisual is unable to find stereo supported pixel format. "
598 "Choosing similar non stereo format.");
599 myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
600 GL_DEBUG_TYPE_OTHER_ARB,
601 0, GL_DEBUG_SEVERITY_HIGH_ARB, aMsg);
603 anAttribs[aStereoFlagPos] = None;
604 aVis = glXChooseVisual (aDisp, scr, anAttribs);
609 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window::CreateWindow: glXChooseVisual failed.");
614 TCollection_ExtendedString aMsg ("OpenGl_Window::CreateWindow: child window has been created with better Visual.");
615 myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
616 GL_DEBUG_TYPE_OTHER_ARB,
617 0, GL_DEBUG_SEVERITY_MEDIUM_ARB, aMsg);
621 if (!theShareCtx.IsNull())
623 // ctx est une copie du previous
624 aGContext = glXCreateContext (aDisp, aVis, (GLXContext )theShareCtx->myGContext, GL_TRUE);
628 aGContext = glXCreateContext (aDisp, aVis, NULL, GL_TRUE);
633 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window::CreateWindow: glXCreateContext failed.");
637 Colormap cmap = XCreateColormap (aDisp, aParent, aVis->visual, AllocNone);
640 color.red = (unsigned short) (myBgColor.rgb[0] * 0xFFFF);
641 color.green = (unsigned short) (myBgColor.rgb[1] * 0xFFFF);
642 color.blue = (unsigned short) (myBgColor.rgb[2] * 0xFFFF);
643 color.flags = DoRed | DoGreen | DoBlue;
644 XAllocColor (aDisp, cmap, &color);
646 XSetWindowAttributes cwa;
648 cwa.event_mask = StructureNotifyMask;
649 cwa.border_pixel = color.pixel;
650 cwa.background_pixel = color.pixel;
652 if (aVis->visualid == wattr.visual->visualid)
658 unsigned long mask = CWBackPixel | CWColormap | CWBorderPixel | CWEventMask;
659 aWindow = XCreateWindow (aDisp, aParent, 0, 0, myWidth, myHeight, 0/*bw*/, aVis->depth, InputOutput, aVis->visual, mask, &cwa);
662 XSetWindowBackground (aDisp, aWindow, cwa.background_pixel);
663 XClearWindow (aDisp, aWindow);
665 if (aWindow != aParent)
668 XMapWindow (aDisp, aWindow);
669 XIfEvent (aDisp, &anEvent, WaitForNotify, (char* )aWindow);
673 myGlContext->Init ((Aspect_Drawable )aWindow, (Aspect_Display )aDisp, (Aspect_RenderingContext )aGContext, isCoreProfile);
675 myGlContext->Share (theShareCtx);
676 myGlContext->SetSwapInterval (mySwapInterval);
680 // =======================================================================
681 // function : ~OpenGl_Window
683 // =======================================================================
684 OpenGl_Window::~OpenGl_Window()
687 || myGlContext.IsNull())
689 myGlContext.Nullify();
693 // release "GL" context if it is owned by window
694 // Mesa implementation can fail to destroy GL context if it set for current thread.
695 // It should be safer to unset thread GL context before its destruction.
696 #if defined(HAVE_EGL) || defined(__ANDROID__)
697 if ((EGLSurface )myGlContext->myWindow != EGL_NO_SURFACE)
699 eglDestroySurface ((EGLDisplay )myGlContext->myDisplay,
700 (EGLSurface )myGlContext->myWindow);
702 #elif defined(_WIN32)
703 HWND aWindow = (HWND )myGlContext->myWindow;
704 HDC aWindowDC = (HDC )myGlContext->myWindowDC;
705 HGLRC aWindowGContext = (HGLRC )myGlContext->myGContext;
706 HGLRC aThreadGContext = wglGetCurrentContext();
707 myGlContext.Nullify();
709 if (aThreadGContext != NULL)
711 if (aThreadGContext == aWindowGContext)
713 wglMakeCurrent (NULL, NULL);
716 wglDeleteContext (aWindowGContext);
718 ReleaseDC (aWindow, aWindowDC);
720 Display* aDisplay = (Display* )myGlContext->myDisplay;
721 GLXContext aWindowGContext = (GLXContext )myGlContext->myGContext;
722 GLXContext aThreadGContext = glXGetCurrentContext();
723 myGlContext.Nullify();
725 if (aDisplay != NULL)
727 if (aThreadGContext == aWindowGContext)
729 glXMakeCurrent (aDisplay, None, NULL);
732 // FSXXX sync necessary if non-direct rendering
734 glXDestroyContext (aDisplay, aWindowGContext);
741 // =======================================================================
742 // function : Activate
744 // =======================================================================
745 Standard_Boolean OpenGl_Window::Activate()
747 return myGlContext->MakeCurrent();
750 #if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
752 // =======================================================================
754 // purpose : call_subr_resize
755 // =======================================================================
756 void OpenGl_Window::Resize (const CALL_DEF_WINDOW& theCWindow)
758 #if !defined(_WIN32) && !defined(HAVE_EGL) && !defined(__ANDROID__)
759 Display* aDisp = (Display* )myGlContext->myDisplay;
764 // If the size is not changed - do nothing
765 if ((myWidth == theCWindow.dx) && (myHeight == theCWindow.dy))
768 myWidth = theCWindow.dx;
769 myHeight = theCWindow.dy;
771 #if !defined(_WIN32) && !defined(HAVE_EGL) && !defined(__ANDROID__)
772 XResizeWindow (aDisp, myGlContext->myWindow, (unsigned int )myWidth, (unsigned int )myHeight);
773 XSync (aDisp, False);
781 // =======================================================================
782 // function : ReadDepths
783 // purpose : TelReadDepths
784 // =======================================================================
785 void OpenGl_Window::ReadDepths (const Standard_Integer theX, const Standard_Integer theY,
786 const Standard_Integer theWidth, const Standard_Integer theHeight,
789 if (theDepths == NULL || !Activate())
792 OpenGl_Mat4 aProjectMat;
793 OpenGl_Utils::Ortho2D<Standard_ShortReal> (aProjectMat,
794 0.f, static_cast<GLfloat> (myWidth), 0.f, static_cast<GLfloat> (myHeight));
796 myGlContext->WorldViewState.Push();
797 myGlContext->ProjectionState.Push();
799 myGlContext->WorldViewState.SetIdentity();
800 myGlContext->ProjectionState.SetCurrent (aProjectMat);
802 myGlContext->ApplyProjectionMatrix();
803 myGlContext->ApplyWorldViewMatrix();
805 #if !defined(GL_ES_VERSION_2_0)
806 glRasterPos2i (theX, theY);
808 glReadPixels (theX, theY, theWidth, theHeight, GL_DEPTH_COMPONENT, GL_FLOAT, theDepths);
812 myGlContext->WorldViewState.Pop();
813 myGlContext->ProjectionState.Pop();
815 myGlContext->ApplyProjectionMatrix();
818 // =======================================================================
819 // function : SetBackgroundColor
820 // purpose : call_subr_set_background
821 // =======================================================================
822 void OpenGl_Window::SetBackgroundColor (const Standard_ShortReal theR,
823 const Standard_ShortReal theG,
824 const Standard_ShortReal theB)
826 myBgColor.rgb[0] = theR;
827 myBgColor.rgb[1] = theG;
828 myBgColor.rgb[2] = theB;
831 #if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
833 // =======================================================================
836 // =======================================================================
837 void OpenGl_Window::Init()
842 #if defined(HAVE_EGL) || defined(__ANDROID__)
843 eglQuerySurface ((EGLDisplay )myGlContext->myDisplay, (EGLSurface )myGlContext->myWindow, EGL_WIDTH, &myWidth);
844 eglQuerySurface ((EGLDisplay )myGlContext->myDisplay, (EGLSurface )myGlContext->myWindow, EGL_HEIGHT, &myHeight);
845 #elif defined(_WIN32)
847 GetClientRect ((HWND )myGlContext->myWindow, &cr);
848 myWidth = cr.right - cr.left;
849 myHeight = cr.bottom - cr.top;
853 unsigned int aDummyU;
854 unsigned int aNewWidth = 0;
855 unsigned int aNewHeight = 0;
856 Display* aDisp = (Display* )myGlContext->myDisplay;
857 XGetGeometry (aDisp, myGlContext->myWindow, &aRootWin, &aDummy, &aDummy, &aNewWidth, &aNewHeight, &aDummyU, &aDummyU);
859 myHeight = aNewHeight;
862 glDisable (GL_DITHER);
863 glDisable (GL_SCISSOR_TEST);
864 glViewport (0, 0, myWidth, myHeight);
865 #if !defined(GL_ES_VERSION_2_0)
866 glDrawBuffer (GL_BACK);
867 if (myGlContext->core11 != NULL)
869 glMatrixMode (GL_MODELVIEW);
876 // =======================================================================
877 // function : EnableFeatures
879 // =======================================================================
880 void OpenGl_Window::EnableFeatures() const
885 // =======================================================================
886 // function : DisableFeatures
888 // =======================================================================
889 void OpenGl_Window::DisableFeatures() const
891 #if !defined(GL_ES_VERSION_2_0)
892 glPixelTransferi (GL_MAP_COLOR, GL_FALSE);
896 * Disable stuff that's likely to slow down glDrawPixels.
897 * (Omit as much of this as possible, when you know in advance
898 * that the OpenGL state will already be set correctly.)
900 glDisable(GL_DITHER);
902 glDisable(GL_DEPTH_TEST);
903 glDisable(GL_TEXTURE_2D);
904 glDisable(GL_STENCIL_TEST);
906 #if !defined(GL_ES_VERSION_2_0)
907 glDisable(GL_LIGHTING);
908 glDisable(GL_ALPHA_TEST);
910 glDisable(GL_LOGIC_OP);
911 glDisable(GL_TEXTURE_1D);
913 glPixelTransferi(GL_MAP_COLOR, GL_FALSE);
914 glPixelTransferi(GL_RED_SCALE, 1);
915 glPixelTransferi(GL_RED_BIAS, 0);
916 glPixelTransferi(GL_GREEN_SCALE, 1);
917 glPixelTransferi(GL_GREEN_BIAS, 0);
918 glPixelTransferi(GL_BLUE_SCALE, 1);
919 glPixelTransferi(GL_BLUE_BIAS, 0);
920 glPixelTransferi(GL_ALPHA_SCALE, 1);
921 glPixelTransferi(GL_ALPHA_BIAS, 0);
924 * Disable extensions that could slow down glDrawPixels.
925 * (Actually, you should check for the presence of the proper
926 * extension before making these calls. I've omitted that
927 * code for simplicity.)
930 if ((myGlContext->myGlVerMajor >= 1) && (myGlContext->myGlVerMinor >= 2))
932 #ifdef GL_EXT_convolution
933 if (myGlContext->CheckExtension ("GL_CONVOLUTION_1D_EXT"))
934 glDisable(GL_CONVOLUTION_1D_EXT);
936 if (myGlContext->CheckExtension ("GL_CONVOLUTION_2D_EXT"))
937 glDisable(GL_CONVOLUTION_2D_EXT);
939 if (myGlContext->CheckExtension ("GL_SEPARABLE_2D_EXT"))
940 glDisable(GL_SEPARABLE_2D_EXT);
943 #ifdef GL_EXT_histogram
944 if (myGlContext->CheckExtension ("GL_SEPARABLE_2D_EXT"))
945 glDisable(GL_HISTOGRAM_EXT);
947 if (myGlContext->CheckExtension ("GL_MINMAX_EXT"))
948 glDisable(GL_MINMAX_EXT);
951 #ifdef GL_EXT_texture3D
952 if (myGlContext->CheckExtension ("GL_TEXTURE_3D_EXT"))
953 glDisable(GL_TEXTURE_3D_EXT);