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 <InterfaceGraphic.hxx>
20 #include <OpenGl_Window.hxx>
22 #include <OpenGl_Context.hxx>
23 #include <OpenGl_GraphicDriver.hxx>
25 #include <Aspect_GraphicDeviceDefinitionError.hxx>
26 #include <TCollection_AsciiString.hxx>
27 #include <TCollection_ExtendedString.hxx>
29 #if defined(HAVE_EGL) || defined(__ANDROID__)
33 IMPLEMENT_STANDARD_HANDLE(OpenGl_Window,MMgt_TShared)
34 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Window,MMgt_TShared)
36 #if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
40 static const TEL_COLOUR THE_DEFAULT_BG_COLOR = { { 0.F, 0.F, 0.F, 1.F } };
42 #if defined(HAVE_EGL) || defined(__ANDROID__)
46 // WGL_ARB_pixel_format
47 #ifndef WGL_NUMBER_PIXEL_FORMATS_ARB
48 #define WGL_NUMBER_PIXEL_FORMATS_ARB 0x2000
49 #define WGL_DRAW_TO_WINDOW_ARB 0x2001
50 #define WGL_DRAW_TO_BITMAP_ARB 0x2002
51 #define WGL_ACCELERATION_ARB 0x2003
52 #define WGL_NEED_PALETTE_ARB 0x2004
53 #define WGL_NEED_SYSTEM_PALETTE_ARB 0x2005
54 #define WGL_SWAP_LAYER_BUFFERS_ARB 0x2006
55 #define WGL_SWAP_METHOD_ARB 0x2007
56 #define WGL_NUMBER_OVERLAYS_ARB 0x2008
57 #define WGL_NUMBER_UNDERLAYS_ARB 0x2009
58 #define WGL_TRANSPARENT_ARB 0x200A
59 #define WGL_TRANSPARENT_RED_VALUE_ARB 0x2037
60 #define WGL_TRANSPARENT_GREEN_VALUE_ARB 0x2038
61 #define WGL_TRANSPARENT_BLUE_VALUE_ARB 0x2039
62 #define WGL_TRANSPARENT_ALPHA_VALUE_ARB 0x203A
63 #define WGL_TRANSPARENT_INDEX_VALUE_ARB 0x203B
64 #define WGL_SHARE_DEPTH_ARB 0x200C
65 #define WGL_SHARE_STENCIL_ARB 0x200D
66 #define WGL_SHARE_ACCUM_ARB 0x200E
67 #define WGL_SUPPORT_GDI_ARB 0x200F
68 #define WGL_SUPPORT_OPENGL_ARB 0x2010
69 #define WGL_DOUBLE_BUFFER_ARB 0x2011
70 #define WGL_STEREO_ARB 0x2012
71 #define WGL_PIXEL_TYPE_ARB 0x2013
72 #define WGL_COLOR_BITS_ARB 0x2014
73 #define WGL_RED_BITS_ARB 0x2015
74 #define WGL_RED_SHIFT_ARB 0x2016
75 #define WGL_GREEN_BITS_ARB 0x2017
76 #define WGL_GREEN_SHIFT_ARB 0x2018
77 #define WGL_BLUE_BITS_ARB 0x2019
78 #define WGL_BLUE_SHIFT_ARB 0x201A
79 #define WGL_ALPHA_BITS_ARB 0x201B
80 #define WGL_ALPHA_SHIFT_ARB 0x201C
81 #define WGL_ACCUM_BITS_ARB 0x201D
82 #define WGL_ACCUM_RED_BITS_ARB 0x201E
83 #define WGL_ACCUM_GREEN_BITS_ARB 0x201F
84 #define WGL_ACCUM_BLUE_BITS_ARB 0x2020
85 #define WGL_ACCUM_ALPHA_BITS_ARB 0x2021
86 #define WGL_DEPTH_BITS_ARB 0x2022
87 #define WGL_STENCIL_BITS_ARB 0x2023
88 #define WGL_AUX_BUFFERS_ARB 0x2024
90 #define WGL_NO_ACCELERATION_ARB 0x2025
91 #define WGL_GENERIC_ACCELERATION_ARB 0x2026
92 #define WGL_FULL_ACCELERATION_ARB 0x2027
94 #define WGL_SWAP_EXCHANGE_ARB 0x2028
95 #define WGL_SWAP_COPY_ARB 0x2029
96 #define WGL_SWAP_UNDEFINED_ARB 0x202A
98 #define WGL_TYPE_RGBA_ARB 0x202B
99 #define WGL_TYPE_COLORINDEX_ARB 0x202C
100 #endif // WGL_NUMBER_PIXEL_FORMATS_ARB
102 // WGL_ARB_create_context_profile
103 #ifndef WGL_CONTEXT_MAJOR_VERSION_ARB
104 #define WGL_CONTEXT_MAJOR_VERSION_ARB 0x2091
105 #define WGL_CONTEXT_MINOR_VERSION_ARB 0x2092
106 #define WGL_CONTEXT_LAYER_PLANE_ARB 0x2093
107 #define WGL_CONTEXT_FLAGS_ARB 0x2094
108 #define WGL_CONTEXT_PROFILE_MASK_ARB 0x9126
110 // WGL_CONTEXT_FLAGS bits
111 #define WGL_CONTEXT_DEBUG_BIT_ARB 0x0001
112 #define WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB 0x0002
114 // WGL_CONTEXT_PROFILE_MASK_ARB bits
115 #define WGL_CONTEXT_CORE_PROFILE_BIT_ARB 0x00000001
116 #define WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB 0x00000002
117 #endif // WGL_CONTEXT_MAJOR_VERSION_ARB
119 static LRESULT CALLBACK wndProcDummy (HWND theWin, UINT theMsg, WPARAM theParamW, LPARAM theParamL)
121 return DefWindowProcW (theWin, theMsg, theParamW, theParamL);
124 static Bool WaitForNotify (Display* theDisp, XEvent* theEv, char* theArg)
126 return (theEv->type == MapNotify) && (theEv->xmap.window == (Window )theArg);
132 // =======================================================================
133 // function : OpenGl_Window
135 // =======================================================================
136 OpenGl_Window::OpenGl_Window (const Handle(OpenGl_GraphicDriver)& theDriver,
137 const CALL_DEF_WINDOW& theCWindow,
138 Aspect_RenderingContext theGContext,
139 const Handle(OpenGl_Caps)& theCaps,
140 const Handle(OpenGl_Context)& theShareCtx)
141 : myGlContext (new OpenGl_Context (theCaps)),
142 myOwnGContext (theGContext == 0),
143 myWidth ((Standard_Integer )theCWindow.dx),
144 myHeight ((Standard_Integer )theCWindow.dy),
145 myBgColor (THE_DEFAULT_BG_COLOR)
147 myBgColor.rgb[0] = theCWindow.Background.r;
148 myBgColor.rgb[1] = theCWindow.Background.g;
149 myBgColor.rgb[2] = theCWindow.Background.b;
151 #if defined(HAVE_EGL) || defined(__ANDROID__)
152 EGLDisplay anEglDisplay = (EGLDisplay )theDriver->getRawGlDisplay();
153 EGLContext anEglContext = (EGLContext )theDriver->getRawGlContext();
154 EGLConfig anEglConfig = (EGLConfig )theDriver->getRawGlConfig();
155 if (anEglDisplay == EGL_NO_DISPLAY
156 || anEglContext == EGL_NO_CONTEXT
157 || anEglConfig == NULL)
159 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window, EGL does not provide compatible configurations!");
163 EGLSurface anEglSurf = EGL_NO_SURFACE;
164 if (theGContext == (EGLContext )EGL_NO_CONTEXT)
166 // create new surface
167 anEglSurf = eglCreateWindowSurface (anEglDisplay, anEglConfig, (EGLNativeWindowType )theCWindow.XWindow, NULL);
168 if (anEglSurf == EGL_NO_SURFACE)
170 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window, EGL is unable to create surface for window!");
174 else if (theGContext != anEglContext)
176 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window, EGL is used in unsupported combination!");
181 anEglSurf = eglGetCurrentSurface(EGL_DRAW);
182 if (anEglSurf == EGL_NO_SURFACE)
184 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window, EGL is unable to retrieve current surface!");
189 myGlContext->Init ((Aspect_Drawable )anEglSurf, (Aspect_Display )anEglDisplay, (Aspect_RenderingContext )anEglContext);
190 #elif defined(_WIN32)
192 HWND aWindow = (HWND )theCWindow.XWindow;
193 HDC aWindowDC = GetDC (aWindow);
194 HGLRC aGContext = (HGLRC )theGContext;
196 PIXELFORMATDESCRIPTOR aPixelFrmt;
197 memset (&aPixelFrmt, 0, sizeof(aPixelFrmt));
198 aPixelFrmt.nSize = sizeof(PIXELFORMATDESCRIPTOR);
199 aPixelFrmt.nVersion = 1;
200 aPixelFrmt.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
201 aPixelFrmt.iPixelType = PFD_TYPE_RGBA;
202 aPixelFrmt.cColorBits = 24;
203 aPixelFrmt.cDepthBits = 24;
204 aPixelFrmt.cStencilBits = 8;
205 aPixelFrmt.iLayerType = PFD_MAIN_PLANE;
206 if (theCaps->contextStereo)
208 aPixelFrmt.dwFlags |= PFD_STEREO;
211 int aPixelFrmtId = ChoosePixelFormat (aWindowDC, &aPixelFrmt);
213 // in case of failure try without stereo if any
214 if (aPixelFrmtId == 0 && theCaps->contextStereo)
216 TCollection_ExtendedString aMsg ("OpenGl_Window::CreateWindow: "
217 "ChoosePixelFormat is unable to find stereo supported pixel format. "
218 "Choosing similar non stereo format.");
219 myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
220 GL_DEBUG_TYPE_OTHER_ARB,
221 0, GL_DEBUG_SEVERITY_HIGH_ARB, aMsg);
223 aPixelFrmt.dwFlags &= ~PFD_STEREO;
224 aPixelFrmtId = ChoosePixelFormat (aWindowDC, &aPixelFrmt);
227 if (aPixelFrmtId == 0)
229 ReleaseDC (aWindow, aWindowDC);
231 TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: ChoosePixelFormat failed. Error code: ");
232 aMsg += (int )GetLastError();
233 Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
237 DescribePixelFormat (aWindowDC, aPixelFrmtId, sizeof(aPixelFrmt), &aPixelFrmt);
239 HGLRC aSlaveCtx = !theShareCtx.IsNull() ? (HGLRC )theShareCtx->myGContext : NULL;
240 if (aGContext == NULL)
242 // create temporary context to retrieve advanced context creation procedures
243 HMODULE aModule = GetModuleHandleW(NULL);
244 WNDCLASSW aClass; memset (&aClass, 0, sizeof(aClass));
245 aClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
246 aClass.lpfnWndProc = wndProcDummy;
247 aClass.hInstance = aModule;
248 aClass.lpszClassName = L"OpenGl_WindowTmp";
250 HDC aDevCtxTmp = NULL;
251 HGLRC aRendCtxTmp = NULL;
252 if ((!theCaps->contextDebug && !theCaps->contextNoAccel)
253 || RegisterClassW (&aClass) == 0)
255 aClass.lpszClassName = NULL;
257 if (aClass.lpszClassName != NULL)
259 DWORD anExStyle = WS_EX_TOOLWINDOW | WS_EX_WINDOWEDGE;
260 #if (_WIN32_WINNT >= 0x0500)
261 anExStyle |= WS_EX_NOACTIVATE;
263 aWinTmp = CreateWindowExW(anExStyle,
264 aClass.lpszClassName, L"OpenGl_WindowTmp",
265 WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_DISABLED,
267 NULL, NULL, aModule, NULL);
271 aDevCtxTmp = GetDC (aWinTmp);
272 SetPixelFormat (aDevCtxTmp, aPixelFrmtId, &aPixelFrmt);
273 aRendCtxTmp = wglCreateContext (aDevCtxTmp);
276 typedef BOOL (WINAPI *wglChoosePixelFormatARB_t)(HDC theDevCtx,
277 const int* theIntAttribs,
278 const float* theFloatAttribs,
279 unsigned int theMaxFormats,
281 unsigned int* theNumFormatsOut);
282 typedef HGLRC (WINAPI *wglCreateContextAttribsARB_t)(HDC theDevCtx,
283 HGLRC theShareContext,
284 const int* theAttribs);
285 wglChoosePixelFormatARB_t aChoosePixProc = NULL;
286 wglCreateContextAttribsARB_t aCreateCtxProc = NULL;
287 if (aRendCtxTmp != NULL)
289 wglMakeCurrent (aDevCtxTmp, aRendCtxTmp);
291 typedef const char* (WINAPI *wglGetExtensionsStringARB_t)(HDC theDeviceContext);
292 wglGetExtensionsStringARB_t aGetExtensions = (wglGetExtensionsStringARB_t )wglGetProcAddress ("wglGetExtensionsStringARB");
293 const char* aWglExts = (aGetExtensions != NULL) ? aGetExtensions (wglGetCurrentDC()) : NULL;
294 if (OpenGl_Context::CheckExtension (aWglExts, "WGL_ARB_pixel_format"))
296 aChoosePixProc = (wglChoosePixelFormatARB_t )wglGetProcAddress ("wglChoosePixelFormatARB");
298 if (OpenGl_Context::CheckExtension (aWglExts, "WGL_ARB_create_context_profile"))
300 aCreateCtxProc = (wglCreateContextAttribsARB_t )wglGetProcAddress ("wglCreateContextAttribsARB");
304 // choose extended pixel format
305 if (aChoosePixProc != NULL)
307 const int aPixAttribs[] =
309 WGL_DRAW_TO_WINDOW_ARB, GL_TRUE,
310 WGL_SUPPORT_OPENGL_ARB, GL_TRUE,
311 WGL_DOUBLE_BUFFER_ARB, GL_TRUE,
312 WGL_STEREO_ARB, theCaps->contextStereo ? GL_TRUE : GL_FALSE,
313 WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB,
314 //WGL_SAMPLE_BUFFERS_ARB, 1,
315 //WGL_SAMPLES_ARB, 8,
316 WGL_COLOR_BITS_ARB, 24,
317 WGL_DEPTH_BITS_ARB, 24,
318 WGL_STENCIL_BITS_ARB, 8,
319 WGL_ACCELERATION_ARB, theCaps->contextNoAccel ? WGL_NO_ACCELERATION_ARB : WGL_FULL_ACCELERATION_ARB,
322 unsigned int aFrmtsNb = 0;
323 aChoosePixProc (aWindowDC, aPixAttribs, NULL, 1, &aPixelFrmtId, &aFrmtsNb);
326 // setup pixel format - may be set only once per window
327 if (!SetPixelFormat (aWindowDC, aPixelFrmtId, &aPixelFrmt))
329 ReleaseDC (aWindow, aWindowDC);
331 TCollection_AsciiString aMsg("OpenGl_Window::CreateWindow: SetPixelFormat failed. Error code: ");
332 aMsg += (int )GetLastError();
333 Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
337 // create GL context with extra options
338 if (aCreateCtxProc != NULL)
340 // Beware! NVIDIA drivers reject context creation when WGL_CONTEXT_PROFILE_MASK_ARB are specified
341 // but not WGL_CONTEXT_MAJOR_VERSION_ARB/WGL_CONTEXT_MINOR_VERSION_ARB.
344 //WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
345 //WGL_CONTEXT_MINOR_VERSION_ARB, 2,
346 //WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB, //WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
347 WGL_CONTEXT_FLAGS_ARB, theCaps->contextDebug ? WGL_CONTEXT_DEBUG_BIT_ARB : 0,
351 aGContext = aCreateCtxProc (aWindowDC, aSlaveCtx, aCtxAttribs);
352 if (aGContext != NULL)
358 if (aRendCtxTmp != NULL)
360 wglDeleteContext (aRendCtxTmp);
362 if (aDevCtxTmp != NULL)
364 ReleaseDC (aWinTmp, aDevCtxTmp);
368 DestroyWindow (aWinTmp);
370 if (aClass.lpszClassName != NULL)
372 UnregisterClassW (aClass.lpszClassName, aModule);
375 if (aGContext == NULL)
377 // create context using obsolete functionality
378 aGContext = wglCreateContext (aWindowDC);
380 if (aGContext == NULL)
382 ReleaseDC (aWindow, aWindowDC);
384 TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: wglCreateContext failed. Error code: ");
385 aMsg += (int )GetLastError();
386 Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
391 // all GL context within one OpenGl_GraphicDriver should be shared!
392 if (aSlaveCtx != NULL && wglShareLists (aSlaveCtx, aGContext) != TRUE)
394 TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: wglShareLists failed. Error code: ");
395 aMsg += (int )GetLastError();
396 Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
400 myGlContext->Init ((Aspect_Handle )aWindow, (Aspect_Handle )aWindowDC, (Aspect_RenderingContext )aGContext);
402 Window aParent = (Window )theCWindow.XWindow;
405 Display* aDisp = theDriver->GetDisplayConnection()->GetDisplay();
406 GLXContext aGContext = (GLXContext )theGContext;
408 XWindowAttributes wattr;
409 XGetWindowAttributes (aDisp, aParent, &wattr);
410 const int scr = DefaultScreen (aDisp);
412 XVisualInfo* aVis = NULL;
414 unsigned long aVisInfoMask = VisualIDMask | VisualScreenMask;
415 XVisualInfo aVisInfo;
416 aVisInfo.visualid = wattr.visual->visualid;
417 aVisInfo.screen = scr;
419 aVis = XGetVisualInfo (aDisp, aVisInfoMask, &aVisInfo, &aNbItems);
426 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window::CreateWindow: XGetVisualInfo failed.");
434 #if defined(__linux) || defined(Linux) || defined(__APPLE__)
437 // check Visual for OpenGl context's parameters compability
438 int isGl = 0, isDoubleBuffer = 0, isRGBA = 0, isStereo = 0;
439 int aDepthSize = 0, aStencilSize = 0;
441 if (glXGetConfig (aDisp, aVis, GLX_USE_GL, &isGl) != 0)
444 if (glXGetConfig (aDisp, aVis, GLX_RGBA, &isRGBA) != 0)
447 if (glXGetConfig (aDisp, aVis, GLX_DOUBLEBUFFER, &isDoubleBuffer) != 0)
450 if (glXGetConfig (aDisp, aVis, GLX_STEREO, &isStereo) != 0)
453 if (glXGetConfig (aDisp, aVis, GLX_DEPTH_SIZE, &aDepthSize) != 0)
456 if (glXGetConfig (aDisp, aVis, GLX_STENCIL_SIZE, &aStencilSize) != 0)
459 if (!isGl || !aDepthSize || !isRGBA || !aStencilSize ||
460 (isDoubleBuffer ? 1 : 0) != 1 ||
461 (isStereo ? 1 : 0) != (theCaps->contextStereo ? 1 : 0))
473 anAttribs[anIter++] = GLX_RGBA;
475 anAttribs[anIter++] = GLX_DEPTH_SIZE;
476 anAttribs[anIter++] = 1;
478 anAttribs[anIter++] = GLX_STENCIL_SIZE;
479 anAttribs[anIter++] = 1;
481 anAttribs[anIter++] = GLX_RED_SIZE;
482 anAttribs[anIter++] = (wattr.depth <= 8) ? 0 : 1;
484 anAttribs[anIter++] = GLX_GREEN_SIZE;
485 anAttribs[anIter++] = (wattr.depth <= 8) ? 0 : 1;
487 anAttribs[anIter++] = GLX_BLUE_SIZE;
488 anAttribs[anIter++] = (wattr.depth <= 8) ? 0 : 1;
490 anAttribs[anIter++] = GLX_DOUBLEBUFFER;
492 // warning: this flag may be set to None, so it need to be last in anAttribs
493 Standard_Integer aStereoFlagPos = anIter;
494 if (theCaps->contextStereo)
495 anAttribs[anIter++] = GLX_STEREO;
497 anAttribs[anIter++] = None;
499 aVis = glXChooseVisual (aDisp, scr, anAttribs);
501 // in case of failure try without stereo if any
502 if (aVis == NULL && theCaps->contextStereo)
504 TCollection_ExtendedString aMsg ("OpenGl_Window::CreateWindow: "
505 "glXChooseVisual is unable to find stereo supported pixel format. "
506 "Choosing similar non stereo format.");
507 myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
508 GL_DEBUG_TYPE_OTHER_ARB,
509 0, GL_DEBUG_SEVERITY_HIGH_ARB, aMsg);
511 anAttribs[aStereoFlagPos] = None;
512 aVis = glXChooseVisual (aDisp, scr, anAttribs);
517 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window::CreateWindow: glXChooseVisual failed.");
522 if (!theShareCtx.IsNull())
524 // ctx est une copie du previous
525 aGContext = glXCreateContext (aDisp, aVis, (GLXContext )theShareCtx->myGContext, GL_TRUE);
529 aGContext = glXCreateContext (aDisp, aVis, NULL, GL_TRUE);
534 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window::CreateWindow: glXCreateContext failed.");
538 Colormap cmap = XCreateColormap (aDisp, aParent, aVis->visual, AllocNone);
541 color.red = (unsigned short) (myBgColor.rgb[0] * 0xFFFF);
542 color.green = (unsigned short) (myBgColor.rgb[1] * 0xFFFF);
543 color.blue = (unsigned short) (myBgColor.rgb[2] * 0xFFFF);
544 color.flags = DoRed | DoGreen | DoBlue;
545 XAllocColor (aDisp, cmap, &color);
547 XSetWindowAttributes cwa;
549 cwa.event_mask = StructureNotifyMask;
550 cwa.border_pixel = color.pixel;
551 cwa.background_pixel = color.pixel;
553 if (aVis->visualid == wattr.visual->visualid)
559 unsigned long mask = CWBackPixel | CWColormap | CWBorderPixel | CWEventMask;
560 aWindow = XCreateWindow (aDisp, aParent, 0, 0, myWidth, myHeight, 0/*bw*/, aVis->depth, InputOutput, aVis->visual, mask, &cwa);
563 XSetWindowBackground (aDisp, aWindow, cwa.background_pixel);
564 XClearWindow (aDisp, aWindow);
566 if (aWindow != aParent)
569 XMapWindow (aDisp, aWindow);
570 XIfEvent (aDisp, &anEvent, WaitForNotify, (char* )aWindow);
574 myGlContext->Init ((Aspect_Drawable )aWindow, (Aspect_Display )aDisp, (Aspect_RenderingContext )aGContext);
576 myGlContext->Share (theShareCtx);
581 // =======================================================================
582 // function : ~OpenGl_Window
584 // =======================================================================
585 OpenGl_Window::~OpenGl_Window()
588 || myGlContext.IsNull())
590 myGlContext.Nullify();
594 // release "GL" context if it is owned by window
595 // Mesa implementation can fail to destroy GL context if it set for current thread.
596 // It should be safer to unset thread GL context before its destruction.
597 #if defined(HAVE_EGL) || defined(__ANDROID__)
598 if ((EGLSurface )myGlContext->myWindow != EGL_NO_SURFACE)
600 eglDestroySurface ((EGLDisplay )myGlContext->myDisplay,
601 (EGLSurface )myGlContext->myWindow);
603 #elif defined(_WIN32)
604 HWND aWindow = (HWND )myGlContext->myWindow;
605 HDC aWindowDC = (HDC )myGlContext->myWindowDC;
606 HGLRC aWindowGContext = (HGLRC )myGlContext->myGContext;
607 HGLRC aThreadGContext = wglGetCurrentContext();
608 myGlContext.Nullify();
610 if (aThreadGContext != NULL)
612 if (aThreadGContext == aWindowGContext)
614 wglMakeCurrent (NULL, NULL);
617 wglDeleteContext (aWindowGContext);
619 ReleaseDC (aWindow, aWindowDC);
621 Display* aDisplay = (Display* )myGlContext->myDisplay;
622 GLXContext aWindowGContext = (GLXContext )myGlContext->myGContext;
623 GLXContext aThreadGContext = glXGetCurrentContext();
624 myGlContext.Nullify();
626 if (aDisplay != NULL)
628 if (aThreadGContext == aWindowGContext)
630 glXMakeCurrent (aDisplay, None, NULL);
633 // FSXXX sync necessary if non-direct rendering
635 glXDestroyContext (aDisplay, aWindowGContext);
642 // =======================================================================
643 // function : Activate
645 // =======================================================================
646 Standard_Boolean OpenGl_Window::Activate()
648 return myGlContext->MakeCurrent();
651 #if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
653 // =======================================================================
655 // purpose : call_subr_resize
656 // =======================================================================
657 void OpenGl_Window::Resize (const CALL_DEF_WINDOW& theCWindow)
659 #if !defined(_WIN32) && !defined(HAVE_EGL) && !defined(__ANDROID__)
660 Display* aDisp = (Display* )myGlContext->myDisplay;
665 // If the size is not changed - do nothing
666 if ((myWidth == theCWindow.dx) && (myHeight == theCWindow.dy))
669 myWidth = (Standard_Integer )theCWindow.dx;
670 myHeight = (Standard_Integer )theCWindow.dy;
672 #if !defined(_WIN32) && !defined(HAVE_EGL) && !defined(__ANDROID__)
673 XResizeWindow (aDisp, myGlContext->myWindow, (unsigned int )myWidth, (unsigned int )myHeight);
674 XSync (aDisp, False);
682 // =======================================================================
683 // function : ReadDepths
684 // purpose : TelReadDepths
685 // =======================================================================
686 void OpenGl_Window::ReadDepths (const Standard_Integer theX, const Standard_Integer theY,
687 const Standard_Integer theWidth, const Standard_Integer theHeight,
690 if (theDepths == NULL || !Activate())
693 #if !defined(GL_ES_VERSION_2_0)
694 glMatrixMode (GL_PROJECTION);
696 gluOrtho2D (0.0, (GLdouble )myWidth, 0.0, (GLdouble )myHeight);
697 glMatrixMode (GL_MODELVIEW);
700 glRasterPos2i (theX, theY);
702 glReadPixels (theX, theY, theWidth, theHeight, GL_DEPTH_COMPONENT, GL_FLOAT, theDepths);
707 // =======================================================================
708 // function : SetBackgroundColor
709 // purpose : call_subr_set_background
710 // =======================================================================
711 void OpenGl_Window::SetBackgroundColor (const Standard_ShortReal theR,
712 const Standard_ShortReal theG,
713 const Standard_ShortReal theB)
715 myBgColor.rgb[0] = theR;
716 myBgColor.rgb[1] = theG;
717 myBgColor.rgb[2] = theB;
720 #if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
722 // =======================================================================
725 // =======================================================================
726 void OpenGl_Window::Init()
731 #if defined(HAVE_EGL) || defined(__ANDROID__)
732 eglQuerySurface ((EGLDisplay )myGlContext->myDisplay, (EGLSurface )myGlContext->myWindow, EGL_WIDTH, &myWidth);
733 eglQuerySurface ((EGLDisplay )myGlContext->myDisplay, (EGLSurface )myGlContext->myWindow, EGL_HEIGHT, &myHeight);
734 #elif defined(_WIN32)
736 GetClientRect ((HWND )myGlContext->myWindow, &cr);
737 myWidth = cr.right - cr.left;
738 myHeight = cr.bottom - cr.top;
742 unsigned int aDummyU;
743 unsigned int aNewWidth = 0;
744 unsigned int aNewHeight = 0;
745 Display* aDisp = (Display* )myGlContext->myDisplay;
746 XGetGeometry (aDisp, myGlContext->myWindow, &aRootWin, &aDummy, &aDummy, &aNewWidth, &aNewHeight, &aDummyU, &aDummyU);
748 myHeight = aNewHeight;
751 glDisable (GL_SCISSOR_TEST);
752 #if !defined(GL_ES_VERSION_2_0)
753 glMatrixMode (GL_MODELVIEW);
754 glViewport (0, 0, myWidth, myHeight);
755 glDrawBuffer (GL_BACK);
761 // =======================================================================
762 // function : EnableFeatures
764 // =======================================================================
765 void OpenGl_Window::EnableFeatures() const
770 // =======================================================================
771 // function : DisableFeatures
773 // =======================================================================
774 void OpenGl_Window::DisableFeatures() const
776 #if !defined(GL_ES_VERSION_2_0)
777 glPixelTransferi (GL_MAP_COLOR, GL_FALSE);
781 * Disable stuff that's likely to slow down glDrawPixels.
782 * (Omit as much of this as possible, when you know in advance
783 * that the OpenGL state will already be set correctly.)
786 glDisable(GL_DEPTH_TEST);
787 glDisable(GL_TEXTURE_2D);
788 glDisable(GL_STENCIL_TEST);
790 #if !defined(GL_ES_VERSION_2_0)
791 glDisable(GL_LIGHTING);
792 glDisable(GL_ALPHA_TEST);
794 glDisable(GL_LOGIC_OP);
795 glDisable(GL_TEXTURE_1D);
797 glPixelTransferi(GL_MAP_COLOR, GL_FALSE);
798 glPixelTransferi(GL_RED_SCALE, 1);
799 glPixelTransferi(GL_RED_BIAS, 0);
800 glPixelTransferi(GL_GREEN_SCALE, 1);
801 glPixelTransferi(GL_GREEN_BIAS, 0);
802 glPixelTransferi(GL_BLUE_SCALE, 1);
803 glPixelTransferi(GL_BLUE_BIAS, 0);
804 glPixelTransferi(GL_ALPHA_SCALE, 1);
805 glPixelTransferi(GL_ALPHA_BIAS, 0);
808 * Disable extensions that could slow down glDrawPixels.
809 * (Actually, you should check for the presence of the proper
810 * extension before making these calls. I've omitted that
811 * code for simplicity.)
814 if ((myGlContext->myGlVerMajor >= 1) && (myGlContext->myGlVerMinor >= 2))
816 #ifdef GL_EXT_convolution
817 if (myGlContext->CheckExtension ("GL_CONVOLUTION_1D_EXT"))
818 glDisable(GL_CONVOLUTION_1D_EXT);
820 if (myGlContext->CheckExtension ("GL_CONVOLUTION_2D_EXT"))
821 glDisable(GL_CONVOLUTION_2D_EXT);
823 if (myGlContext->CheckExtension ("GL_SEPARABLE_2D_EXT"))
824 glDisable(GL_SEPARABLE_2D_EXT);
827 #ifdef GL_EXT_histogram
828 if (myGlContext->CheckExtension ("GL_SEPARABLE_2D_EXT"))
829 glDisable(GL_HISTOGRAM_EXT);
831 if (myGlContext->CheckExtension ("GL_MINMAX_EXT"))
832 glDisable(GL_MINMAX_EXT);
835 #ifdef GL_EXT_texture3D
836 if (myGlContext->CheckExtension ("GL_TEXTURE_3D_EXT"))
837 glDisable(GL_TEXTURE_3D_EXT);
843 // =======================================================================
844 // function : MakeFrontBufCurrent
845 // purpose : TelMakeFrontBufCurrent
846 // =======================================================================
847 void OpenGl_Window::MakeFrontBufCurrent() const
849 #if !defined(GL_ES_VERSION_2_0)
850 glDrawBuffer (GL_FRONT);
854 // =======================================================================
855 // function : MakeBackBufCurrent
856 // purpose : TelMakeBackBufCurrent
857 // =======================================================================
858 void OpenGl_Window::MakeBackBufCurrent() const
860 #if !defined(GL_ES_VERSION_2_0)
861 glDrawBuffer (GL_BACK);