0025703: Visualization - Decrease number of samplers used in ray-tracing mode
[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
25b97fac 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;
5e27df78 191 HWND aWindow = (HWND )theCWindow.XWindow;
192 HDC aWindowDC = GetDC (aWindow);
193 HGLRC aGContext = (HGLRC )theGContext;
194
3d8969b1 195 PIXELFORMATDESCRIPTOR aPixelFrmt;
abe46077 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);
b5ac8292 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
3d8969b1 226 if (aPixelFrmtId == 0)
5e27df78 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
abe46077 236 DescribePixelFormat (aWindowDC, aPixelFrmtId, sizeof(aPixelFrmt), &aPixelFrmt);
5e27df78 237
58655684 238 HGLRC aSlaveCtx = !theShareCtx.IsNull() ? (HGLRC )theShareCtx->myGContext : NULL;
5e27df78 239 if (aGContext == NULL)
240 {
58655684 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;
abe46077 251 if ((!theCaps->contextDebug && !theCaps->contextNoAccel)
58655684 252 || RegisterClassW (&aClass) == 0)
253 {
254 aClass.lpszClassName = NULL;
255 }
256 if (aClass.lpszClassName != NULL)
257 {
229c0b6a 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,
58655684 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 }
abe46077 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;
58655684 286 if (aRendCtxTmp != NULL)
287 {
288 wglMakeCurrent (aDevCtxTmp, aRendCtxTmp);
289
290 typedef const char* (WINAPI *wglGetExtensionsStringARB_t)(HDC theDeviceContext);
abe46077 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)
58655684 352 {
abe46077 353 aSlaveCtx = NULL;
58655684 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 {
abe46077 376 // create context using obsolete functionality
58655684 377 aGContext = wglCreateContext (aWindowDC);
378 }
5e27df78 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!
58655684 391 if (aSlaveCtx != NULL && wglShareLists (aSlaveCtx, aGContext) != TRUE)
5e27df78 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
eaf5c5e0 401 Window aParent = (Window )theCWindow.XWindow;
402 Window aWindow = 0;
403
25b97fac 404 Display* aDisp = theDriver->GetDisplayConnection()->GetDisplay();
5e27df78 405 GLXContext aGContext = (GLXContext )theGContext;
2166f0fa 406
2166f0fa
SK
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
2166f0fa
SK
421 if (!myOwnGContext)
422 {
423 if (aVis != NULL)
424 {
425 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window::CreateWindow: XGetVisualInfo failed.");
426 return;
427 }
428
5e27df78 429 aWindow = aParent;
2166f0fa
SK
430 }
431 else
432 {
cdc56cc8 433 #if defined(__linux) || defined(Linux) || defined(__APPLE__)
2166f0fa
SK
434 if (aVis != NULL)
435 {
436 // check Visual for OpenGl context's parameters compability
b5ac8292 437 int isGl = 0, isDoubleBuffer = 0, isRGBA = 0, isStereo = 0;
438 int aDepthSize = 0, aStencilSize = 0;
2166f0fa
SK
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
b5ac8292 449 if (glXGetConfig (aDisp, aVis, GLX_STEREO, &isStereo) != 0)
450 isStereo = 0;
451
2166f0fa
SK
452 if (glXGetConfig (aDisp, aVis, GLX_DEPTH_SIZE, &aDepthSize) != 0)
453 aDepthSize = 0;
454
3d8969b1 455 if (glXGetConfig (aDisp, aVis, GLX_STENCIL_SIZE, &aStencilSize) != 0)
456 aStencilSize = 0;
457
b5ac8292 458 if (!isGl || !aDepthSize || !isRGBA || !aStencilSize ||
73192b37 459 (isDoubleBuffer ? 1 : 0) != 1 ||
b5ac8292 460 (isStereo ? 1 : 0) != (theCaps->contextStereo ? 1 : 0))
2166f0fa
SK
461 {
462 XFree (aVis);
463 aVis = NULL;
464 }
465 }
466 #endif
467
468 if (aVis == NULL)
469 {
470 int anIter = 0;
3d8969b1 471 int anAttribs[13];
2166f0fa
SK
472 anAttribs[anIter++] = GLX_RGBA;
473
474 anAttribs[anIter++] = GLX_DEPTH_SIZE;
475 anAttribs[anIter++] = 1;
476
3d8969b1 477 anAttribs[anIter++] = GLX_STENCIL_SIZE;
478 anAttribs[anIter++] = 1;
479
2166f0fa
SK
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
73192b37 489 anAttribs[anIter++] = GLX_DOUBLEBUFFER;
2166f0fa 490
b5ac8292 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
2166f0fa
SK
496 anAttribs[anIter++] = None;
497
498 aVis = glXChooseVisual (aDisp, scr, anAttribs);
b5ac8292 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
2166f0fa
SK
514 if (aVis == NULL)
515 {
516 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window::CreateWindow: glXChooseVisual failed.");
517 return;
518 }
519 }
520
5e27df78 521 if (!theShareCtx.IsNull())
2166f0fa 522 {
5e27df78 523 // ctx est une copie du previous
524 aGContext = glXCreateContext (aDisp, aVis, (GLXContext )theShareCtx->myGContext, GL_TRUE);
2166f0fa
SK
525 }
526 else
527 {
5e27df78 528 aGContext = glXCreateContext (aDisp, aVis, NULL, GL_TRUE);
2166f0fa
SK
529 }
530
5e27df78 531 if (!aGContext)
2166f0fa
SK
532 {
533 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window::CreateWindow: glXCreateContext failed.");
534 return;
535 }
536
2166f0fa
SK
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 {
5e27df78 554 aWindow = aParent;
2166f0fa
SK
555 }
556 else
557 {
558 unsigned long mask = CWBackPixel | CWColormap | CWBorderPixel | CWEventMask;
5e27df78 559 aWindow = XCreateWindow (aDisp, aParent, 0, 0, myWidth, myHeight, 0/*bw*/, aVis->depth, InputOutput, aVis->visual, mask, &cwa);
2166f0fa
SK
560 }
561
5e27df78 562 XSetWindowBackground (aDisp, aWindow, cwa.background_pixel);
563 XClearWindow (aDisp, aWindow);
2166f0fa 564
5e27df78 565 if (aWindow != aParent)
2166f0fa
SK
566 {
567 XEvent anEvent;
5e27df78 568 XMapWindow (aDisp, aWindow);
569 XIfEvent (aDisp, &anEvent, WaitForNotify, (char* )aWindow);
2166f0fa 570 }
2166f0fa
SK
571 }
572
73192b37 573 myGlContext->Init ((Aspect_Drawable )aWindow, (Aspect_Display )aDisp, (Aspect_RenderingContext )aGContext);
2166f0fa 574#endif
5e27df78 575 myGlContext->Share (theShareCtx);
2166f0fa
SK
576
577 Init();
2166f0fa
SK
578}
579
580// =======================================================================
581// function : ~OpenGl_Window
582// purpose :
583// =======================================================================
584OpenGl_Window::~OpenGl_Window()
585{
25b97fac 586 if (!myOwnGContext
587 || myGlContext.IsNull())
fd4a6963 588 {
589 myGlContext.Nullify();
590 return;
591 }
592
593 // release "GL" context if it is owned by window
544da5a4 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.
25b97fac 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)
fd4a6963 603 HWND aWindow = (HWND )myGlContext->myWindow;
604 HDC aWindowDC = (HDC )myGlContext->myWindowDC;
605 HGLRC aWindowGContext = (HGLRC )myGlContext->myGContext;
544da5a4 606 HGLRC aThreadGContext = wglGetCurrentContext();
5e27df78 607 myGlContext.Nullify();
2166f0fa 608
544da5a4 609 if (aThreadGContext != NULL)
2166f0fa 610 {
544da5a4 611 if (aThreadGContext == aWindowGContext)
2166f0fa 612 {
fd4a6963 613 wglMakeCurrent (NULL, NULL);
2166f0fa 614 }
fd4a6963 615
616 wglDeleteContext (aWindowGContext);
2166f0fa 617 }
fd4a6963 618 ReleaseDC (aWindow, aWindowDC);
5e27df78 619#else
544da5a4 620 Display* aDisplay = (Display* )myGlContext->myDisplay;
621 GLXContext aWindowGContext = (GLXContext )myGlContext->myGContext;
622 GLXContext aThreadGContext = glXGetCurrentContext();
5e27df78 623 myGlContext.Nullify();
624
fd4a6963 625 if (aDisplay != NULL)
2166f0fa 626 {
544da5a4 627 if (aThreadGContext == aWindowGContext)
628 {
629 glXMakeCurrent (aDisplay, None, NULL);
630 }
631
5e27df78 632 // FSXXX sync necessary if non-direct rendering
633 glXWaitGL();
544da5a4 634 glXDestroyContext (aDisplay, aWindowGContext);
2166f0fa
SK
635 }
636#endif
637}
638
4fe56619 639#endif // !__APPLE__
640
2166f0fa
SK
641// =======================================================================
642// function : Activate
643// purpose :
644// =======================================================================
645Standard_Boolean OpenGl_Window::Activate()
646{
86fa64d9 647 return myGlContext->MakeCurrent();
2166f0fa
SK
648}
649
4fe56619 650#if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
651
2166f0fa
SK
652// =======================================================================
653// function : Resize
654// purpose : call_subr_resize
655// =======================================================================
656void OpenGl_Window::Resize (const CALL_DEF_WINDOW& theCWindow)
657{
25b97fac 658#if !defined(_WIN32) && !defined(HAVE_EGL) && !defined(__ANDROID__)
73192b37 659 Display* aDisp = (Display* )myGlContext->myDisplay;
2166f0fa
SK
660 if (aDisp == NULL)
661 return;
73192b37 662#endif
2166f0fa
SK
663
664 // If the size is not changed - do nothing
665 if ((myWidth == theCWindow.dx) && (myHeight == theCWindow.dy))
666 return;
667
e3573bb9 668 myWidth = theCWindow.dx;
669 myHeight = theCWindow.dy;
2166f0fa 670
25b97fac 671#if !defined(_WIN32) && !defined(HAVE_EGL) && !defined(__ANDROID__)
5e27df78 672 XResizeWindow (aDisp, myGlContext->myWindow, (unsigned int )myWidth, (unsigned int )myHeight);
2166f0fa
SK
673 XSync (aDisp, False);
674#endif
675
676 Init();
677}
678
4fe56619 679#endif // !__APPLE__
680
2166f0fa
SK
681// =======================================================================
682// function : ReadDepths
683// purpose : TelReadDepths
684// =======================================================================
685void 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
c827ea3a 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();
2166f0fa 698
c827ea3a 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)
2166f0fa
SK
706 glRasterPos2i (theX, theY);
707 DisableFeatures();
708 glReadPixels (theX, theY, theWidth, theHeight, GL_DEPTH_COMPONENT, GL_FLOAT, theDepths);
709 EnableFeatures();
ca3c13d1 710#endif
c827ea3a 711
712 myGlContext->WorldViewState.Pop();
713 myGlContext->ProjectionState.Pop();
714
715 myGlContext->ApplyProjectionMatrix();
2166f0fa
SK
716}
717
718// =======================================================================
719// function : SetBackgroundColor
720// purpose : call_subr_set_background
721// =======================================================================
722void 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
4fe56619 731#if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
732
2166f0fa
SK
733// =======================================================================
734// function : Init
735// purpose :
736// =======================================================================
737void OpenGl_Window::Init()
738{
739 if (!Activate())
740 return;
741
25b97fac 742#if defined(HAVE_EGL) || defined(__ANDROID__)
e3573bb9 743 eglQuerySurface ((EGLDisplay )myGlContext->myDisplay, (EGLSurface )myGlContext->myWindow, EGL_WIDTH, &myWidth);
744 eglQuerySurface ((EGLDisplay )myGlContext->myDisplay, (EGLSurface )myGlContext->myWindow, EGL_HEIGHT, &myHeight);
25b97fac 745#elif defined(_WIN32)
2166f0fa 746 RECT cr;
5e27df78 747 GetClientRect ((HWND )myGlContext->myWindow, &cr);
2166f0fa
SK
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;
73192b37 756 Display* aDisp = (Display* )myGlContext->myDisplay;
5e27df78 757 XGetGeometry (aDisp, myGlContext->myWindow, &aRootWin, &aDummy, &aDummy, &aNewWidth, &aNewHeight, &aDummyU, &aDummyU);
2166f0fa
SK
758 myWidth = aNewWidth;
759 myHeight = aNewHeight;
760#endif
761
ca3c13d1 762 glDisable (GL_SCISSOR_TEST);
e3573bb9 763 glViewport (0, 0, myWidth, myHeight);
ca3c13d1 764#if !defined(GL_ES_VERSION_2_0)
2166f0fa 765 glMatrixMode (GL_MODELVIEW);
2166f0fa 766 glDrawBuffer (GL_BACK);
ca3c13d1 767#endif
2166f0fa
SK
768}
769
4fe56619 770#endif // !__APPLE__
771
2166f0fa
SK
772// =======================================================================
773// function : EnableFeatures
774// purpose :
775// =======================================================================
776void OpenGl_Window::EnableFeatures() const
777{
73192b37 778 //
2166f0fa
SK
779}
780
781// =======================================================================
782// function : DisableFeatures
783// purpose :
784// =======================================================================
785void OpenGl_Window::DisableFeatures() const
786{
ca3c13d1 787#if !defined(GL_ES_VERSION_2_0)
2166f0fa 788 glPixelTransferi (GL_MAP_COLOR, GL_FALSE);
ca3c13d1 789#endif
2166f0fa
SK
790
791 /*
792 * Disable stuff that's likely to slow down glDrawPixels.
793 * (Omit as much of this as possible, when you know in advance
794 * that the OpenGL state will already be set correctly.)
795 */
2166f0fa
SK
796 glDisable(GL_BLEND);
797 glDisable(GL_DEPTH_TEST);
ca3c13d1 798 glDisable(GL_TEXTURE_2D);
799 glDisable(GL_STENCIL_TEST);
2166f0fa 800
ca3c13d1 801#if !defined(GL_ES_VERSION_2_0)
802 glDisable(GL_LIGHTING);
803 glDisable(GL_ALPHA_TEST);
804 glDisable(GL_FOG);
2166f0fa 805 glDisable(GL_LOGIC_OP);
2166f0fa 806 glDisable(GL_TEXTURE_1D);
ca3c13d1 807
2166f0fa
SK
808 glPixelTransferi(GL_MAP_COLOR, GL_FALSE);
809 glPixelTransferi(GL_RED_SCALE, 1);
810 glPixelTransferi(GL_RED_BIAS, 0);
811 glPixelTransferi(GL_GREEN_SCALE, 1);
812 glPixelTransferi(GL_GREEN_BIAS, 0);
813 glPixelTransferi(GL_BLUE_SCALE, 1);
814 glPixelTransferi(GL_BLUE_BIAS, 0);
815 glPixelTransferi(GL_ALPHA_SCALE, 1);
816 glPixelTransferi(GL_ALPHA_BIAS, 0);
817
818 /*
819 * Disable extensions that could slow down glDrawPixels.
820 * (Actually, you should check for the presence of the proper
821 * extension before making these calls. I've omitted that
822 * code for simplicity.)
823 */
824
b5ac8292 825 if ((myGlContext->myGlVerMajor >= 1) && (myGlContext->myGlVerMinor >= 2))
826 {
2166f0fa 827#ifdef GL_EXT_convolution
b5ac8292 828 if (myGlContext->CheckExtension ("GL_CONVOLUTION_1D_EXT"))
829 glDisable(GL_CONVOLUTION_1D_EXT);
830
831 if (myGlContext->CheckExtension ("GL_CONVOLUTION_2D_EXT"))
832 glDisable(GL_CONVOLUTION_2D_EXT);
833
834 if (myGlContext->CheckExtension ("GL_SEPARABLE_2D_EXT"))
835 glDisable(GL_SEPARABLE_2D_EXT);
2166f0fa
SK
836#endif
837
838#ifdef GL_EXT_histogram
b5ac8292 839 if (myGlContext->CheckExtension ("GL_SEPARABLE_2D_EXT"))
840 glDisable(GL_HISTOGRAM_EXT);
841
842 if (myGlContext->CheckExtension ("GL_MINMAX_EXT"))
843 glDisable(GL_MINMAX_EXT);
2166f0fa
SK
844#endif
845
846#ifdef GL_EXT_texture3D
b5ac8292 847 if (myGlContext->CheckExtension ("GL_TEXTURE_3D_EXT"))
848 glDisable(GL_TEXTURE_3D_EXT);
2166f0fa 849#endif
b5ac8292 850 }
ca3c13d1 851#endif
2166f0fa
SK
852}
853
854// =======================================================================
855// function : MakeFrontBufCurrent
856// purpose : TelMakeFrontBufCurrent
857// =======================================================================
858void OpenGl_Window::MakeFrontBufCurrent() const
859{
ca3c13d1 860#if !defined(GL_ES_VERSION_2_0)
2166f0fa 861 glDrawBuffer (GL_FRONT);
ca3c13d1 862#endif
2166f0fa
SK
863}
864
865// =======================================================================
866// function : MakeBackBufCurrent
867// purpose : TelMakeBackBufCurrent
868// =======================================================================
869void OpenGl_Window::MakeBackBufCurrent() const
870{
ca3c13d1 871#if !defined(GL_ES_VERSION_2_0)
2166f0fa 872 glDrawBuffer (GL_BACK);
ca3c13d1 873#endif
2166f0fa 874}