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)
146 myBgColor.rgb[0] = theCWindow.Background.r;
147 myBgColor.rgb[1] = theCWindow.Background.g;
148 myBgColor.rgb[2] = theCWindow.Background.b;
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)
158 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window, EGL does not provide compatible configurations!");
162 EGLSurface anEglSurf = EGL_NO_SURFACE;
163 if (theGContext == (EGLContext )EGL_NO_CONTEXT)
165 // create new surface
166 anEglSurf = eglCreateWindowSurface (anEglDisplay, anEglConfig, (EGLNativeWindowType )theCWindow.XWindow, NULL);
167 if (anEglSurf == EGL_NO_SURFACE)
169 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window, EGL is unable to create surface for window!");
173 else if (theGContext != anEglContext)
175 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window, EGL is used in unsupported combination!");
180 anEglSurf = eglGetCurrentSurface(EGL_DRAW);
181 if (anEglSurf == EGL_NO_SURFACE)
183 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window, EGL is unable to retrieve current surface!");
188 myGlContext->Init ((Aspect_Drawable )anEglSurf, (Aspect_Display )anEglDisplay, (Aspect_RenderingContext )anEglContext);
189 #elif defined(_WIN32)
191 HWND aWindow = (HWND )theCWindow.XWindow;
192 HDC aWindowDC = GetDC (aWindow);
193 HGLRC aGContext = (HGLRC )theGContext;
195 PIXELFORMATDESCRIPTOR aPixelFrmt;
196 memset (&aPixelFrmt, 0, sizeof(aPixelFrmt));
197 aPixelFrmt.nSize = sizeof(PIXELFORMATDESCRIPTOR);
198 aPixelFrmt.nVersion = 1;
199 aPixelFrmt.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
200 aPixelFrmt.iPixelType = PFD_TYPE_RGBA;
201 aPixelFrmt.cColorBits = 24;
202 aPixelFrmt.cDepthBits = 24;
203 aPixelFrmt.cStencilBits = 8;
204 aPixelFrmt.iLayerType = PFD_MAIN_PLANE;
205 if (theCaps->contextStereo)
207 aPixelFrmt.dwFlags |= PFD_STEREO;
210 int aPixelFrmtId = ChoosePixelFormat (aWindowDC, &aPixelFrmt);
212 // in case of failure try without stereo if any
213 if (aPixelFrmtId == 0 && theCaps->contextStereo)
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);
222 aPixelFrmt.dwFlags &= ~PFD_STEREO;
223 aPixelFrmtId = ChoosePixelFormat (aWindowDC, &aPixelFrmt);
226 if (aPixelFrmtId == 0)
228 ReleaseDC (aWindow, aWindowDC);
230 TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: ChoosePixelFormat failed. Error code: ");
231 aMsg += (int )GetLastError();
232 Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
236 DescribePixelFormat (aWindowDC, aPixelFrmtId, sizeof(aPixelFrmt), &aPixelFrmt);
238 HGLRC aSlaveCtx = !theShareCtx.IsNull() ? (HGLRC )theShareCtx->myGContext : NULL;
239 if (aGContext == NULL)
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";
249 HDC aDevCtxTmp = NULL;
250 HGLRC aRendCtxTmp = NULL;
251 if ((!theCaps->contextDebug && !theCaps->contextNoAccel)
252 || RegisterClassW (&aClass) == 0)
254 aClass.lpszClassName = NULL;
256 if (aClass.lpszClassName != NULL)
258 DWORD anExStyle = WS_EX_TOOLWINDOW | WS_EX_WINDOWEDGE;
259 #if (_WIN32_WINNT >= 0x0500)
260 anExStyle |= WS_EX_NOACTIVATE;
262 aWinTmp = CreateWindowExW(anExStyle,
263 aClass.lpszClassName, L"OpenGl_WindowTmp",
264 WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_DISABLED,
266 NULL, NULL, aModule, NULL);
270 aDevCtxTmp = GetDC (aWinTmp);
271 SetPixelFormat (aDevCtxTmp, aPixelFrmtId, &aPixelFrmt);
272 aRendCtxTmp = wglCreateContext (aDevCtxTmp);
275 typedef BOOL (WINAPI *wglChoosePixelFormatARB_t)(HDC theDevCtx,
276 const int* theIntAttribs,
277 const float* theFloatAttribs,
278 unsigned int theMaxFormats,
280 unsigned int* theNumFormatsOut);
281 typedef HGLRC (WINAPI *wglCreateContextAttribsARB_t)(HDC theDevCtx,
282 HGLRC theShareContext,
283 const int* theAttribs);
284 wglChoosePixelFormatARB_t aChoosePixProc = NULL;
285 wglCreateContextAttribsARB_t aCreateCtxProc = NULL;
286 if (aRendCtxTmp != NULL)
288 wglMakeCurrent (aDevCtxTmp, aRendCtxTmp);
290 typedef const char* (WINAPI *wglGetExtensionsStringARB_t)(HDC theDeviceContext);
291 wglGetExtensionsStringARB_t aGetExtensions = (wglGetExtensionsStringARB_t )wglGetProcAddress ("wglGetExtensionsStringARB");
292 const char* aWglExts = (aGetExtensions != NULL) ? aGetExtensions (wglGetCurrentDC()) : NULL;
293 if (OpenGl_Context::CheckExtension (aWglExts, "WGL_ARB_pixel_format"))
295 aChoosePixProc = (wglChoosePixelFormatARB_t )wglGetProcAddress ("wglChoosePixelFormatARB");
297 if (OpenGl_Context::CheckExtension (aWglExts, "WGL_ARB_create_context_profile"))
299 aCreateCtxProc = (wglCreateContextAttribsARB_t )wglGetProcAddress ("wglCreateContextAttribsARB");
303 // choose extended pixel format
304 if (aChoosePixProc != NULL)
306 const int aPixAttribs[] =
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,
321 unsigned int aFrmtsNb = 0;
322 aChoosePixProc (aWindowDC, aPixAttribs, NULL, 1, &aPixelFrmtId, &aFrmtsNb);
325 // setup pixel format - may be set only once per window
326 if (!SetPixelFormat (aWindowDC, aPixelFrmtId, &aPixelFrmt))
328 ReleaseDC (aWindow, aWindowDC);
330 TCollection_AsciiString aMsg("OpenGl_Window::CreateWindow: SetPixelFormat failed. Error code: ");
331 aMsg += (int )GetLastError();
332 Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
336 // create GL context with extra options
337 if (aCreateCtxProc != NULL)
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.
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,
350 aGContext = aCreateCtxProc (aWindowDC, aSlaveCtx, aCtxAttribs);
351 if (aGContext != NULL)
357 if (aRendCtxTmp != NULL)
359 wglDeleteContext (aRendCtxTmp);
361 if (aDevCtxTmp != NULL)
363 ReleaseDC (aWinTmp, aDevCtxTmp);
367 DestroyWindow (aWinTmp);
369 if (aClass.lpszClassName != NULL)
371 UnregisterClassW (aClass.lpszClassName, aModule);
374 if (aGContext == NULL)
376 // create context using obsolete functionality
377 aGContext = wglCreateContext (aWindowDC);
379 if (aGContext == NULL)
381 ReleaseDC (aWindow, aWindowDC);
383 TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: wglCreateContext failed. Error code: ");
384 aMsg += (int )GetLastError();
385 Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
390 // all GL context within one OpenGl_GraphicDriver should be shared!
391 if (aSlaveCtx != NULL && wglShareLists (aSlaveCtx, aGContext) != TRUE)
393 TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: wglShareLists failed. Error code: ");
394 aMsg += (int )GetLastError();
395 Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
399 myGlContext->Init ((Aspect_Handle )aWindow, (Aspect_Handle )aWindowDC, (Aspect_RenderingContext )aGContext);
401 Window aParent = (Window )theCWindow.XWindow;
404 Display* aDisp = theDriver->GetDisplayConnection()->GetDisplay();
405 GLXContext aGContext = (GLXContext )theGContext;
407 XWindowAttributes wattr;
408 XGetWindowAttributes (aDisp, aParent, &wattr);
409 const int scr = DefaultScreen (aDisp);
411 XVisualInfo* aVis = NULL;
413 unsigned long aVisInfoMask = VisualIDMask | VisualScreenMask;
414 XVisualInfo aVisInfo;
415 aVisInfo.visualid = wattr.visual->visualid;
416 aVisInfo.screen = scr;
418 aVis = XGetVisualInfo (aDisp, aVisInfoMask, &aVisInfo, &aNbItems);
425 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window::CreateWindow: XGetVisualInfo failed.");
433 #if defined(__linux) || defined(Linux) || defined(__APPLE__)
436 // check Visual for OpenGl context's parameters compability
437 int isGl = 0, isDoubleBuffer = 0, isRGBA = 0, isStereo = 0;
438 int aDepthSize = 0, aStencilSize = 0;
440 if (glXGetConfig (aDisp, aVis, GLX_USE_GL, &isGl) != 0)
443 if (glXGetConfig (aDisp, aVis, GLX_RGBA, &isRGBA) != 0)
446 if (glXGetConfig (aDisp, aVis, GLX_DOUBLEBUFFER, &isDoubleBuffer) != 0)
449 if (glXGetConfig (aDisp, aVis, GLX_STEREO, &isStereo) != 0)
452 if (glXGetConfig (aDisp, aVis, GLX_DEPTH_SIZE, &aDepthSize) != 0)
455 if (glXGetConfig (aDisp, aVis, GLX_STENCIL_SIZE, &aStencilSize) != 0)
458 if (!isGl || !aDepthSize || !isRGBA || !aStencilSize ||
459 (isDoubleBuffer ? 1 : 0) != 1 ||
460 (isStereo ? 1 : 0) != (theCaps->contextStereo ? 1 : 0))
472 anAttribs[anIter++] = GLX_RGBA;
474 anAttribs[anIter++] = GLX_DEPTH_SIZE;
475 anAttribs[anIter++] = 1;
477 anAttribs[anIter++] = GLX_STENCIL_SIZE;
478 anAttribs[anIter++] = 1;
480 anAttribs[anIter++] = GLX_RED_SIZE;
481 anAttribs[anIter++] = (wattr.depth <= 8) ? 0 : 1;
483 anAttribs[anIter++] = GLX_GREEN_SIZE;
484 anAttribs[anIter++] = (wattr.depth <= 8) ? 0 : 1;
486 anAttribs[anIter++] = GLX_BLUE_SIZE;
487 anAttribs[anIter++] = (wattr.depth <= 8) ? 0 : 1;
489 anAttribs[anIter++] = GLX_DOUBLEBUFFER;
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;
496 anAttribs[anIter++] = None;
498 aVis = glXChooseVisual (aDisp, scr, anAttribs);
500 // in case of failure try without stereo if any
501 if (aVis == NULL && theCaps->contextStereo)
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);
510 anAttribs[aStereoFlagPos] = None;
511 aVis = glXChooseVisual (aDisp, scr, anAttribs);
516 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window::CreateWindow: glXChooseVisual failed.");
521 if (!theShareCtx.IsNull())
523 // ctx est une copie du previous
524 aGContext = glXCreateContext (aDisp, aVis, (GLXContext )theShareCtx->myGContext, GL_TRUE);
528 aGContext = glXCreateContext (aDisp, aVis, NULL, GL_TRUE);
533 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window::CreateWindow: glXCreateContext failed.");
537 Colormap cmap = XCreateColormap (aDisp, aParent, aVis->visual, AllocNone);
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);
546 XSetWindowAttributes cwa;
548 cwa.event_mask = StructureNotifyMask;
549 cwa.border_pixel = color.pixel;
550 cwa.background_pixel = color.pixel;
552 if (aVis->visualid == wattr.visual->visualid)
558 unsigned long mask = CWBackPixel | CWColormap | CWBorderPixel | CWEventMask;
559 aWindow = XCreateWindow (aDisp, aParent, 0, 0, myWidth, myHeight, 0/*bw*/, aVis->depth, InputOutput, aVis->visual, mask, &cwa);
562 XSetWindowBackground (aDisp, aWindow, cwa.background_pixel);
563 XClearWindow (aDisp, aWindow);
565 if (aWindow != aParent)
568 XMapWindow (aDisp, aWindow);
569 XIfEvent (aDisp, &anEvent, WaitForNotify, (char* )aWindow);
573 myGlContext->Init ((Aspect_Drawable )aWindow, (Aspect_Display )aDisp, (Aspect_RenderingContext )aGContext);
575 myGlContext->Share (theShareCtx);
580 // =======================================================================
581 // function : ~OpenGl_Window
583 // =======================================================================
584 OpenGl_Window::~OpenGl_Window()
587 || myGlContext.IsNull())
589 myGlContext.Nullify();
593 // release "GL" context if it is owned by window
594 // Mesa implementation can fail to destroy GL context if it set for current thread.
595 // It should be safer to unset thread GL context before its destruction.
596 #if defined(HAVE_EGL) || defined(__ANDROID__)
597 if ((EGLSurface )myGlContext->myWindow != EGL_NO_SURFACE)
599 eglDestroySurface ((EGLDisplay )myGlContext->myDisplay,
600 (EGLSurface )myGlContext->myWindow);
602 #elif defined(_WIN32)
603 HWND aWindow = (HWND )myGlContext->myWindow;
604 HDC aWindowDC = (HDC )myGlContext->myWindowDC;
605 HGLRC aWindowGContext = (HGLRC )myGlContext->myGContext;
606 HGLRC aThreadGContext = wglGetCurrentContext();
607 myGlContext.Nullify();
609 if (aThreadGContext != NULL)
611 if (aThreadGContext == aWindowGContext)
613 wglMakeCurrent (NULL, NULL);
616 wglDeleteContext (aWindowGContext);
618 ReleaseDC (aWindow, aWindowDC);
620 Display* aDisplay = (Display* )myGlContext->myDisplay;
621 GLXContext aWindowGContext = (GLXContext )myGlContext->myGContext;
622 GLXContext aThreadGContext = glXGetCurrentContext();
623 myGlContext.Nullify();
625 if (aDisplay != NULL)
627 if (aThreadGContext == aWindowGContext)
629 glXMakeCurrent (aDisplay, None, NULL);
632 // FSXXX sync necessary if non-direct rendering
634 glXDestroyContext (aDisplay, aWindowGContext);
641 // =======================================================================
642 // function : Activate
644 // =======================================================================
645 Standard_Boolean OpenGl_Window::Activate()
647 return myGlContext->MakeCurrent();
650 #if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
652 // =======================================================================
654 // purpose : call_subr_resize
655 // =======================================================================
656 void OpenGl_Window::Resize (const CALL_DEF_WINDOW& theCWindow)
658 #if !defined(_WIN32) && !defined(HAVE_EGL) && !defined(__ANDROID__)
659 Display* aDisp = (Display* )myGlContext->myDisplay;
664 // If the size is not changed - do nothing
665 if ((myWidth == theCWindow.dx) && (myHeight == theCWindow.dy))
668 myWidth = theCWindow.dx;
669 myHeight = theCWindow.dy;
671 #if !defined(_WIN32) && !defined(HAVE_EGL) && !defined(__ANDROID__)
672 XResizeWindow (aDisp, myGlContext->myWindow, (unsigned int )myWidth, (unsigned int )myHeight);
673 XSync (aDisp, False);
681 // =======================================================================
682 // function : ReadDepths
683 // purpose : TelReadDepths
684 // =======================================================================
685 void OpenGl_Window::ReadDepths (const Standard_Integer theX, const Standard_Integer theY,
686 const Standard_Integer theWidth, const Standard_Integer theHeight,
689 if (theDepths == NULL || !Activate())
692 OpenGl_Mat4 aProjectMat;
693 OpenGl_Utils::Ortho2D<Standard_ShortReal> (aProjectMat,
694 0.f, static_cast<GLfloat> (myWidth), 0.f, static_cast<GLfloat> (myHeight));
696 myGlContext->WorldViewState.Push();
697 myGlContext->ProjectionState.Push();
699 myGlContext->WorldViewState.SetIdentity();
700 myGlContext->ProjectionState.SetCurrent (aProjectMat);
702 myGlContext->ApplyProjectionMatrix();
703 myGlContext->ApplyWorldViewMatrix();
705 #if !defined(GL_ES_VERSION_2_0)
706 glRasterPos2i (theX, theY);
708 glReadPixels (theX, theY, theWidth, theHeight, GL_DEPTH_COMPONENT, GL_FLOAT, theDepths);
712 myGlContext->WorldViewState.Pop();
713 myGlContext->ProjectionState.Pop();
715 myGlContext->ApplyProjectionMatrix();
718 // =======================================================================
719 // function : SetBackgroundColor
720 // purpose : call_subr_set_background
721 // =======================================================================
722 void OpenGl_Window::SetBackgroundColor (const Standard_ShortReal theR,
723 const Standard_ShortReal theG,
724 const Standard_ShortReal theB)
726 myBgColor.rgb[0] = theR;
727 myBgColor.rgb[1] = theG;
728 myBgColor.rgb[2] = theB;
731 #if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
733 // =======================================================================
736 // =======================================================================
737 void OpenGl_Window::Init()
742 #if defined(HAVE_EGL) || defined(__ANDROID__)
743 eglQuerySurface ((EGLDisplay )myGlContext->myDisplay, (EGLSurface )myGlContext->myWindow, EGL_WIDTH, &myWidth);
744 eglQuerySurface ((EGLDisplay )myGlContext->myDisplay, (EGLSurface )myGlContext->myWindow, EGL_HEIGHT, &myHeight);
745 #elif defined(_WIN32)
747 GetClientRect ((HWND )myGlContext->myWindow, &cr);
748 myWidth = cr.right - cr.left;
749 myHeight = cr.bottom - cr.top;
753 unsigned int aDummyU;
754 unsigned int aNewWidth = 0;
755 unsigned int aNewHeight = 0;
756 Display* aDisp = (Display* )myGlContext->myDisplay;
757 XGetGeometry (aDisp, myGlContext->myWindow, &aRootWin, &aDummy, &aDummy, &aNewWidth, &aNewHeight, &aDummyU, &aDummyU);
759 myHeight = aNewHeight;
762 glDisable (GL_SCISSOR_TEST);
763 glViewport (0, 0, myWidth, myHeight);
764 #if !defined(GL_ES_VERSION_2_0)
765 glMatrixMode (GL_MODELVIEW);
766 glDrawBuffer (GL_BACK);
772 // =======================================================================
773 // function : EnableFeatures
775 // =======================================================================
776 void OpenGl_Window::EnableFeatures() const
781 // =======================================================================
782 // function : DisableFeatures
784 // =======================================================================
785 void OpenGl_Window::DisableFeatures() const
787 #if !defined(GL_ES_VERSION_2_0)
788 glPixelTransferi (GL_MAP_COLOR, GL_FALSE);
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.)
797 glDisable(GL_DEPTH_TEST);
798 glDisable(GL_TEXTURE_2D);
799 glDisable(GL_STENCIL_TEST);
801 #if !defined(GL_ES_VERSION_2_0)
802 glDisable(GL_LIGHTING);
803 glDisable(GL_ALPHA_TEST);
805 glDisable(GL_LOGIC_OP);
806 glDisable(GL_TEXTURE_1D);
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);
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.)
825 if ((myGlContext->myGlVerMajor >= 1) && (myGlContext->myGlVerMinor >= 2))
827 #ifdef GL_EXT_convolution
828 if (myGlContext->CheckExtension ("GL_CONVOLUTION_1D_EXT"))
829 glDisable(GL_CONVOLUTION_1D_EXT);
831 if (myGlContext->CheckExtension ("GL_CONVOLUTION_2D_EXT"))
832 glDisable(GL_CONVOLUTION_2D_EXT);
834 if (myGlContext->CheckExtension ("GL_SEPARABLE_2D_EXT"))
835 glDisable(GL_SEPARABLE_2D_EXT);
838 #ifdef GL_EXT_histogram
839 if (myGlContext->CheckExtension ("GL_SEPARABLE_2D_EXT"))
840 glDisable(GL_HISTOGRAM_EXT);
842 if (myGlContext->CheckExtension ("GL_MINMAX_EXT"))
843 glDisable(GL_MINMAX_EXT);
846 #ifdef GL_EXT_texture3D
847 if (myGlContext->CheckExtension ("GL_TEXTURE_3D_EXT"))
848 glDisable(GL_TEXTURE_3D_EXT);
854 // =======================================================================
855 // function : MakeFrontBufCurrent
856 // purpose : TelMakeFrontBufCurrent
857 // =======================================================================
858 void OpenGl_Window::MakeFrontBufCurrent() const
860 #if !defined(GL_ES_VERSION_2_0)
861 glDrawBuffer (GL_FRONT);
865 // =======================================================================
866 // function : MakeBackBufCurrent
867 // purpose : TelMakeBackBufCurrent
868 // =======================================================================
869 void OpenGl_Window::MakeBackBufCurrent() const
871 #if !defined(GL_ES_VERSION_2_0)
872 glDrawBuffer (GL_BACK);