0024123: Create debug OpenGL context when requested (GL_ARB_debug_output)
[occt.git] / src / OpenGl / OpenGl_Window.cxx
index 0f0d37e..415d26a 100644 (file)
@@ -1,7 +1,23 @@
-// File:      OpenGl_Window.cxx
-// Created:   20 September 2011
-// Author:    Sergey ZERCHANINOV
-// Copyright: OPEN CASCADE 2011
+// Created on: 2011-09-20
+// Created by: Sergey ZERCHANINOV
+// Copyright (c) 2011-2012 OPEN CASCADE SAS
+//
+// The content of this file is subject to the Open CASCADE Technology Public
+// License Version 6.5 (the "License"). You may not use the content of this file
+// except in compliance with the License. Please obtain a copy of the License
+// at http://www.opencascade.org and read it completely before using this file.
+//
+// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
+// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
+//
+// The Original Code and all software distributed under the License is
+// distributed on an "AS IS" basis, without warranty of any kind, and the
+// Initial Developer hereby disclaims all such warranties, including without
+// limitation, any warranties of merchantability, fitness for a particular
+// purpose or non-infringement. Please see the License for the specific terms
+// and conditions governing the rights and limitations under the License.
+
+#include <OpenGl_GlCore11.hxx>
 
 #include <InterfaceGraphic.hxx>
 
 
 #include <OpenGl_Context.hxx>
 #include <OpenGl_Display.hxx>
-#include <OpenGl_ResourceCleaner.hxx>
-#include <OpenGl_ResourceTexture.hxx>
 
 #include <Aspect_GraphicDeviceDefinitionError.hxx>
 #include <TCollection_AsciiString.hxx>
 
-#include <GL/glu.h> // gluOrtho2D()
-
 IMPLEMENT_STANDARD_HANDLE(OpenGl_Window,MMgt_TShared)
 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Window,MMgt_TShared)
 
+#if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
+
 namespace
 {
   static const TEL_COLOUR THE_DEFAULT_BG_COLOR = { { 0.F, 0.F, 0.F, 1.F } };
 
-  static GLCONTEXT ThePreviousCtx = 0; // to share GL resources
-#if (!defined(_WIN32) && !defined(__WIN32__))
-  static GLXContext TheDeadGlxCtx; // Context to be destroyed
-  static Display*   TheDeadGlxDpy; // Display associated with TheDeadGlxCtx
-#endif
-  
-#if (defined(_WIN32) || defined(__WIN32__))
-  static int find_pixel_format (HDC hDC, PIXELFORMATDESCRIPTOR* pfd, const Standard_Boolean dbuff)
+#if defined(_WIN32)
+
+  // WGL_ARB_create_context_profile
+#ifndef WGL_CONTEXT_MAJOR_VERSION_ARB
+  #define WGL_CONTEXT_MAJOR_VERSION_ARB           0x2091
+  #define WGL_CONTEXT_MINOR_VERSION_ARB           0x2092
+  #define WGL_CONTEXT_LAYER_PLANE_ARB             0x2093
+  #define WGL_CONTEXT_FLAGS_ARB                   0x2094
+  #define WGL_CONTEXT_PROFILE_MASK_ARB            0x9126
+
+  // WGL_CONTEXT_FLAGS bits
+  #define WGL_CONTEXT_DEBUG_BIT_ARB               0x0001
+  #define WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB  0x0002
+
+  // WGL_CONTEXT_PROFILE_MASK_ARB bits
+  #define WGL_CONTEXT_CORE_PROFILE_BIT_ARB          0x00000001
+  #define WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB 0x00000002
+#endif // WGL_CONTEXT_MAJOR_VERSION_ARB
+
+  static LRESULT CALLBACK wndProcDummy (HWND theWin, UINT theMsg, WPARAM theParamW, LPARAM theParamL)
   {
-    PIXELFORMATDESCRIPTOR pfd0;
-    memset (&pfd0, 0, sizeof (PIXELFORMATDESCRIPTOR));
-    pfd0.nSize           = sizeof (PIXELFORMATDESCRIPTOR);
-    pfd0.nVersion        = 1;
-    pfd0.dwFlags         = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | (dbuff ? PFD_DOUBLEBUFFER : PFD_SUPPORT_GDI);
-    pfd0.iPixelType      = PFD_TYPE_RGBA;
-    pfd0.iLayerType      = PFD_MAIN_PLANE;
-
-    int       iPixelFormat = 0;
-    int       iGood = 0;
-    const int cBits[] = { 32, 24 };
-    const int dBits[] = { 32, 24, 16 };
-
-    int i, j;
-    for (i = 0; i < sizeof(dBits) / sizeof(int); i++)
+    return DefWindowProcW (theWin, theMsg, theParamW, theParamL);
+  }
+
+  static int find_pixel_format (HDC                    theDevCtx,
+                                PIXELFORMATDESCRIPTOR& thePixelFrmt,
+                                const Standard_Boolean theIsDoubleBuff)
+  {
+    PIXELFORMATDESCRIPTOR aPixelFrmtTmp;
+    memset (&aPixelFrmtTmp, 0, sizeof (PIXELFORMATDESCRIPTOR));
+    aPixelFrmtTmp.nSize      = sizeof (PIXELFORMATDESCRIPTOR);
+    aPixelFrmtTmp.nVersion   = 1;
+    aPixelFrmtTmp.dwFlags    = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | (theIsDoubleBuff ? PFD_DOUBLEBUFFER : PFD_SUPPORT_GDI);
+    aPixelFrmtTmp.iPixelType = PFD_TYPE_RGBA;
+    aPixelFrmtTmp.iLayerType = PFD_MAIN_PLANE;
+
+    const int BUFF_BITS_STENCIL[] = {  8,  1     };
+    const int BUFF_BITS_COLOR[]   = { 32, 24     };
+    const int BUFF_BITS_DEPTH[]   = { 32, 24, 16 };
+
+    int aGoodBits[] = { 0, 0, 0 };
+    int aPixelFrmtIdLast = 0;
+    int aPixelFrmtIdGood = 0;
+    Standard_Size aStencilIter = 0, aColorIter = 0, aDepthIter = 0;
+    for (aStencilIter = 0; aStencilIter < sizeof(BUFF_BITS_STENCIL) / sizeof(int); ++aStencilIter)
     {
-      pfd0.cDepthBits = dBits[i];
-      iGood = 0;
-      for (j = 0; j < sizeof(cBits) / sizeof(int); j++)
+      aPixelFrmtTmp.cStencilBits = (BYTE)(BUFF_BITS_STENCIL[aStencilIter]);
+      for (aDepthIter = 0; aDepthIter < sizeof(BUFF_BITS_DEPTH) / sizeof(int); ++aDepthIter)
       {
-        pfd0.cColorBits = cBits[j];
-        iPixelFormat = ChoosePixelFormat (hDC, &pfd0);
-        if (iPixelFormat)
+        aPixelFrmtTmp.cDepthBits = (BYTE)(BUFF_BITS_DEPTH[aDepthIter]);
+        aPixelFrmtIdGood = 0;
+        for (aColorIter = 0; aColorIter < sizeof(BUFF_BITS_COLOR) / sizeof(int); ++aColorIter)
         {
-          pfd->cDepthBits = 0;
-          pfd->cColorBits = 0;
-          DescribePixelFormat (hDC, iPixelFormat, sizeof (PIXELFORMATDESCRIPTOR), pfd);
-          if (pfd->cColorBits >= cBits[j] && pfd->cDepthBits >= dBits[i])
+          aPixelFrmtTmp.cColorBits = (BYTE)(BUFF_BITS_COLOR[aColorIter]);
+          aPixelFrmtIdLast = ChoosePixelFormat (theDevCtx, &aPixelFrmtTmp);
+          if (aPixelFrmtIdLast == 0)
+          {
+            continue;
+          }
+
+          thePixelFrmt.cDepthBits   = 0;
+          thePixelFrmt.cColorBits   = 0;
+          thePixelFrmt.cStencilBits = 0;
+          DescribePixelFormat (theDevCtx, aPixelFrmtIdLast, sizeof(PIXELFORMATDESCRIPTOR), &thePixelFrmt);
+          if (thePixelFrmt.cColorBits   >= BUFF_BITS_COLOR[aColorIter]
+           && thePixelFrmt.cDepthBits   >= BUFF_BITS_DEPTH[aDepthIter]
+           && thePixelFrmt.cStencilBits >= BUFF_BITS_STENCIL[aStencilIter])
+          {
             break;
-          if (iGood == 0)
-            iGood = iPixelFormat;
+          }
+          if (thePixelFrmt.cColorBits > aGoodBits[0])
+          {
+            aGoodBits[0] = thePixelFrmt.cColorBits;
+            aGoodBits[1] = thePixelFrmt.cDepthBits;
+            aGoodBits[2] = thePixelFrmt.cStencilBits;
+            aPixelFrmtIdGood = aPixelFrmtIdLast;
+          }
+          else if (thePixelFrmt.cColorBits == aGoodBits[0])
+          {
+            if (thePixelFrmt.cDepthBits > aGoodBits[1])
+            {
+              aGoodBits[1] = thePixelFrmt.cDepthBits;
+              aGoodBits[2] = thePixelFrmt.cStencilBits;
+              aPixelFrmtIdGood = aPixelFrmtIdLast;
+            }
+            else if (thePixelFrmt.cDepthBits == aGoodBits[1])
+            {
+              if(thePixelFrmt.cStencilBits > aGoodBits[2])
+              {
+                aGoodBits[2] = thePixelFrmt.cStencilBits;
+                aPixelFrmtIdGood = aPixelFrmtIdLast;
+              }
+            }
+          }
+        }
+        if (aColorIter < sizeof(BUFF_BITS_COLOR) / sizeof(int))
+        {
+          break;
         }
       }
-      if (j < sizeof(cBits) / sizeof(int))
+      if (aDepthIter < sizeof(BUFF_BITS_DEPTH) / sizeof(int))
+      {
         break;
+      }
     }
 
-    if (iPixelFormat == 0)
-      iPixelFormat = iGood;
-
-    return iPixelFormat;
+    return (aPixelFrmtIdLast == 0) ? aPixelFrmtIdGood : aPixelFrmtIdLast;
   }
 #else
   static Bool WaitForNotify (Display* theDisp, XEvent* theEv, char* theArg)
@@ -90,14 +161,13 @@ namespace
 // =======================================================================
 OpenGl_Window::OpenGl_Window (const Handle(OpenGl_Display)& theDisplay,
                               const CALL_DEF_WINDOW&        theCWindow,
-                              Aspect_RenderingContext       theGContext)
+                              Aspect_RenderingContext       theGContext,
+                              const Handle(OpenGl_Caps)&    theCaps,
+                              const Handle(OpenGl_Context)& theShareCtx)
 : myDisplay (theDisplay),
-  myWindow (0),
-  myGContext ((GLCONTEXT )theGContext),
-  myGlContext (new OpenGl_Context()),
+  myGlContext (new OpenGl_Context (theCaps)),
   myOwnGContext (theGContext == 0),
-#if (defined(_WIN32) || defined(__WIN32__))
-  myWindowDC (0),
+#if defined(_WIN32)
   mySysPalInUse (FALSE),
 #endif
   myWidth ((Standard_Integer )theCWindow.dx),
@@ -110,10 +180,164 @@ OpenGl_Window::OpenGl_Window (const Handle(OpenGl_Display)& theDisplay,
   myBgColor.rgb[1] = theCWindow.Background.g;
   myBgColor.rgb[2] = theCWindow.Background.b;
 
+#if defined(_WIN32)
+  HWND  aWindow   = (HWND )theCWindow.XWindow;
+  HDC   aWindowDC = GetDC (aWindow);
+  HGLRC aGContext = (HGLRC )theGContext;
+
+  PIXELFORMATDESCRIPTOR aPixelFrmt;
+  const int aPixelFrmtId = find_pixel_format (aWindowDC, aPixelFrmt, myDisplay->DBuffer());
+  if (aPixelFrmtId == 0)
+  {
+    ReleaseDC (aWindow, aWindowDC);
+
+    TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: ChoosePixelFormat failed. Error code: ");
+    aMsg += (int )GetLastError();
+    Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
+    return;
+  }
+
+  if (aPixelFrmt.dwFlags & PFD_NEED_PALETTE)
+  {
+    WINDOW_DATA* aWndData = (WINDOW_DATA* )GetWindowLongPtr (aWindow, GWLP_USERDATA);
+
+    mySysPalInUse = (aPixelFrmt.dwFlags & PFD_NEED_SYSTEM_PALETTE) ? TRUE : FALSE;
+    InterfaceGraphic_RealizePalette (aWindowDC, aWndData->hPal, FALSE, mySysPalInUse);
+  }
+
+  if (myDither)
+  {
+    myDither = (aPixelFrmt.cColorBits <= 8);
+  }
+
+  if (myBackDither)
+  {
+    myBackDither = (aPixelFrmt.cColorBits <= 8);
+  }
+
+  if (!SetPixelFormat (aWindowDC, aPixelFrmtId, &aPixelFrmt))
+  {
+    ReleaseDC (aWindow, aWindowDC);
+
+    TCollection_AsciiString aMsg("OpenGl_Window::CreateWindow: SetPixelFormat failed. Error code: ");
+    aMsg += (int )GetLastError();
+    Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
+    return;
+  }
+
+  HGLRC aSlaveCtx = !theShareCtx.IsNull() ? (HGLRC )theShareCtx->myGContext : NULL;
+  if (aGContext == NULL)
+  {
+    // create temporary context to retrieve advanced context creation procedures
+    HMODULE aModule = GetModuleHandleW(NULL);
+    WNDCLASSW aClass; memset (&aClass, 0, sizeof(aClass));
+    aClass.style         = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
+    aClass.lpfnWndProc   = wndProcDummy;
+    aClass.hInstance     = aModule;
+    aClass.lpszClassName = L"OpenGl_WindowTmp";
+    HWND  aWinTmp     = NULL;
+    HDC   aDevCtxTmp  = NULL;
+    HGLRC aRendCtxTmp = NULL;
+    if (!theCaps->contextDebug
+     || RegisterClassW (&aClass) == 0)
+    {
+      aClass.lpszClassName = NULL;
+    }
+    if (aClass.lpszClassName != NULL)
+    {
+      aWinTmp = CreateWindowExW(WS_EX_TOOLWINDOW | WS_EX_WINDOWEDGE | WS_EX_NOACTIVATE,
+                                aClass.lpszClassName, L"OpenGl_WindowTmp",
+                                WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_DISABLED,
+                                2, 2, 4, 4,
+                                NULL, NULL, aModule, NULL);
+    }
+    if (aWinTmp != NULL)
+    {
+      aDevCtxTmp = GetDC (aWinTmp);
+      SetPixelFormat (aDevCtxTmp, aPixelFrmtId, &aPixelFrmt);
+      aRendCtxTmp = wglCreateContext (aDevCtxTmp);
+    }
+    if (aRendCtxTmp != NULL)
+    {
+      wglMakeCurrent (aDevCtxTmp, aRendCtxTmp);
+
+      typedef const char* (WINAPI *wglGetExtensionsStringARB_t)(HDC theDeviceContext);
+      typedef HGLRC (WINAPI *wglCreateContextAttribsARB_t)(HDC        theDevCtx,
+                                                           HGLRC      theShareContext,
+                                                           const int* theAttribs);
+      wglGetExtensionsStringARB_t  aGetExtensions = (wglGetExtensionsStringARB_t  )wglGetProcAddress ("wglGetExtensionsStringARB");
+      wglCreateContextAttribsARB_t aCreateCtxProc = (wglCreateContextAttribsARB_t )wglGetProcAddress ("wglCreateContextAttribsARB");
+      const char* aWglExts = aGetExtensions (wglGetCurrentDC());
+      if (aCreateCtxProc != NULL
+       && OpenGl_Context::CheckExtension (aWglExts, "WGL_ARB_create_context_profile"))
+      {
+        // Beware! NVIDIA drivers reject context creation when WGL_CONTEXT_PROFILE_MASK_ARB are specified
+        // but not WGL_CONTEXT_MAJOR_VERSION_ARB/WGL_CONTEXT_MINOR_VERSION_ARB.
+        int aCtxAttribs[] =
+        {
+          //WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
+          //WGL_CONTEXT_MINOR_VERSION_ARB, 2,
+          //WGL_CONTEXT_PROFILE_MASK_ARB,  WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB, //WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
+          WGL_CONTEXT_FLAGS_ARB,         theCaps->contextDebug ? WGL_CONTEXT_DEBUG_BIT_ARB : 0,
+          0, 0
+        };
+
+        aGContext = aCreateCtxProc (aWindowDC, aSlaveCtx, aCtxAttribs);
+        if (aGContext != NULL)
+        {
+          aSlaveCtx = NULL;
+        }
+      }
+    }
+
+    if (aRendCtxTmp != NULL)
+    {
+      wglDeleteContext (aRendCtxTmp);
+    }
+    if (aDevCtxTmp != NULL)
+    {
+      ReleaseDC (aWinTmp, aDevCtxTmp);
+    }
+    if (aWinTmp != NULL)
+    {
+      DestroyWindow (aWinTmp);
+    }
+    if (aClass.lpszClassName != NULL)
+    {
+      UnregisterClassW (aClass.lpszClassName, aModule);
+    }
+
+    if (aGContext == NULL)
+    {
+      aGContext = wglCreateContext (aWindowDC);
+    }
+    if (aGContext == NULL)
+    {
+      ReleaseDC (aWindow, aWindowDC);
+
+      TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: wglCreateContext failed. Error code: ");
+      aMsg += (int )GetLastError();
+      Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
+      return;
+    }
+  }
+
+  // all GL context within one OpenGl_GraphicDriver should be shared!
+  if (aSlaveCtx != NULL && wglShareLists (aSlaveCtx, aGContext) != TRUE)
+  {
+    TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: wglShareLists failed. Error code: ");
+    aMsg += (int )GetLastError();
+    Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
+    return;
+  }
+
+  myGlContext->Init ((Aspect_Handle )aWindow, (Aspect_Handle )aWindowDC, (Aspect_RenderingContext )aGContext);
+#else
   WINDOW aParent = (WINDOW )theCWindow.XWindow;
+  WINDOW aWindow = 0;
   DISPLAY* aDisp = (DISPLAY* )myDisplay->GetDisplay();
+  GLXContext aGContext = (GLXContext )theGContext;
 
-#if (!defined(_WIN32) && !defined(__WIN32__))
   XWindowAttributes wattr;
   XGetWindowAttributes (aDisp, aParent, &wattr);
   const int scr = DefaultScreen (aDisp);
@@ -128,8 +352,6 @@ OpenGl_Window::OpenGl_Window (const Handle(OpenGl_Display)& theDisplay,
     aVis = XGetVisualInfo (aDisp, aVisInfoMask, &aVisInfo, &aNbItems);
   }
 
-  WINDOW win;
-
   if (!myOwnGContext)
   {
     if (aVis != NULL)
@@ -138,17 +360,15 @@ OpenGl_Window::OpenGl_Window (const Handle(OpenGl_Display)& theDisplay,
       return;
     }
 
-    win = aParent;
+    aWindow = aParent;
   }
   else
   {
-    GLCONTEXT ctx;
-
-  #if defined(__linux) || defined(Linux)
+  #if defined(__linux) || defined(Linux) || defined(__APPLE__)
     if (aVis != NULL)
     {
       // check Visual for OpenGl context's parameters compability
-      int isGl = 0, isDoubleBuffer = 0, isRGBA = 0, aDepthSize = 0;
+      int isGl = 0, isDoubleBuffer = 0, isRGBA = 0, aDepthSize = 0, aStencilSize = 0;
 
       if (glXGetConfig (aDisp, aVis, GLX_USE_GL, &isGl) != 0)
         isGl = 0;
@@ -162,6 +382,9 @@ OpenGl_Window::OpenGl_Window (const Handle(OpenGl_Display)& theDisplay,
       if (glXGetConfig (aDisp, aVis, GLX_DEPTH_SIZE, &aDepthSize) != 0)
         aDepthSize = 0;
 
+      if (glXGetConfig (aDisp, aVis, GLX_STENCIL_SIZE, &aStencilSize) != 0)
+        aStencilSize = 0;
+
       if (!isGl || !aDepthSize || !isRGBA  || (isDoubleBuffer ? 1 : 0) != (myDisplay->DBuffer()? 1 : 0))
       {
         XFree (aVis);
@@ -173,12 +396,15 @@ OpenGl_Window::OpenGl_Window (const Handle(OpenGl_Display)& theDisplay,
     if (aVis == NULL)
     {
       int anIter = 0;
-      int anAttribs[11];
+      int anAttribs[13];
       anAttribs[anIter++] = GLX_RGBA;
 
       anAttribs[anIter++] = GLX_DEPTH_SIZE;
       anAttribs[anIter++] = 1;
 
+      anAttribs[anIter++] = GLX_STENCIL_SIZE;
+      anAttribs[anIter++] = 1;
+
       anAttribs[anIter++] = GLX_RED_SIZE;
       anAttribs[anIter++] = (wattr.depth <= 8) ? 0 : 1;
 
@@ -201,36 +427,22 @@ OpenGl_Window::OpenGl_Window (const Handle(OpenGl_Display)& theDisplay,
       }
     }
 
-    if (TheDeadGlxCtx)
+    if (!theShareCtx.IsNull())
     {
-      // recover display lists from TheDeadGlxCtx, then destroy it
-      ctx = glXCreateContext (aDisp, aVis, TheDeadGlxCtx, GL_TRUE);
-
-      OpenGl_ResourceCleaner::GetInstance()->RemoveContext (TheDeadGlxCtx);
-      glXDestroyContext (TheDeadGlxDpy, TheDeadGlxCtx);
-
-      TheDeadGlxCtx = 0;
-    }
-    else if (ThePreviousCtx == 0)
-    {
-      ctx = glXCreateContext (aDisp, aVis, NULL, GL_TRUE);
+      // ctx est une copie du previous
+      aGContext = glXCreateContext (aDisp, aVis, (GLXContext )theShareCtx->myGContext, GL_TRUE);
     }
     else
     {
-      // ctx est une copie du previous
-      ctx = glXCreateContext (aDisp, aVis, ThePreviousCtx, GL_TRUE);
+      aGContext = glXCreateContext (aDisp, aVis, NULL, GL_TRUE);
     }
 
-    if (!ctx)
+    if (!aGContext)
     {
       Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window::CreateWindow: glXCreateContext failed.");
       return;
     }
 
-    OpenGl_ResourceCleaner::GetInstance()->AppendContext (ctx, true);
-
-    ThePreviousCtx = ctx;
-
     Colormap cmap = XCreateColormap (aDisp, aParent, aVis->visual, AllocNone);
 
     XColor color;
@@ -248,25 +460,23 @@ OpenGl_Window::OpenGl_Window (const Handle(OpenGl_Display)& theDisplay,
 
     if (aVis->visualid == wattr.visual->visualid)
     {
-      win = aParent;
+      aWindow = aParent;
     }
     else
     {
       unsigned long mask = CWBackPixel | CWColormap | CWBorderPixel | CWEventMask;
-      win = XCreateWindow (aDisp, aParent, 0, 0, myWidth, myHeight, 0/*bw*/, aVis->depth, InputOutput, aVis->visual, mask, &cwa);
+      aWindow = XCreateWindow (aDisp, aParent, 0, 0, myWidth, myHeight, 0/*bw*/, aVis->depth, InputOutput, aVis->visual, mask, &cwa);
     }
 
-    XSetWindowBackground (aDisp, win, cwa.background_pixel);
-    XClearWindow (aDisp, win);
+    XSetWindowBackground (aDisp, aWindow, cwa.background_pixel);
+    XClearWindow (aDisp, aWindow);
 
-    if (win != aParent)
+    if (aWindow != aParent)
     {
       XEvent anEvent;
-      XMapWindow (aDisp, win);
-      XIfEvent (aDisp, &anEvent, WaitForNotify, (char* )win);
+      XMapWindow (aDisp, aWindow);
+      XIfEvent (aDisp, &anEvent, WaitForNotify, (char* )aWindow);
     }
-
-    myGContext = ctx;
   }
 
   /*
@@ -282,111 +492,22 @@ OpenGl_Window::OpenGl_Window (const Handle(OpenGl_Display)& theDisplay,
   * (Carte Impact avec GLX_RED_SIZE a 5 par exemple)
   */
 
-  int value;
-  glXGetConfig (aDisp, aVis, GLX_RED_SIZE, &value);
+  int aValue;
+  glXGetConfig (aDisp, aVis, GLX_RED_SIZE, &aValue);
 
   if (myDither)
-    myDither = (value < 8);
+    myDither = (aValue < 8);
 
   if (myBackDither)
     myBackDither = (aVis->depth <= 8);
 
   XFree ((char* )aVis);
 
-  myWindow = win;
-
-#else
-
-  myWindowDC = GetDC (aParent);
-
-  PIXELFORMATDESCRIPTOR pfd;
-  int iPixelFormat = find_pixel_format (myWindowDC, &pfd, myDisplay->DBuffer());
-  if (iPixelFormat == 0)
-  {
-    ReleaseDC (aParent, myWindowDC);
-    myWindowDC = 0;
-
-    TCollection_AsciiString msg ("OpenGl_Window::CreateWindow: ChoosePixelFormat failed. Error code: ");
-    msg += (int )GetLastError();
-    Aspect_GraphicDeviceDefinitionError::Raise (msg.ToCString());
-    return;
-  }
-
-  if (pfd.dwFlags & PFD_NEED_PALETTE)
-  {
-    WINDOW_DATA* wd = (WINDOW_DATA* )GetWindowLongPtr (aParent, GWLP_USERDATA);
-
-    mySysPalInUse = (pfd.dwFlags & PFD_NEED_SYSTEM_PALETTE) ? TRUE : FALSE;
-    InterfaceGraphic_RealizePalette (myWindowDC, wd->hPal, FALSE, mySysPalInUse);
-  }
-
-  if (myDither)
-    myDither = (pfd.cColorBits <= 8);
-
-  if (myBackDither)
-    myBackDither = (pfd.cColorBits <= 8);
-
-  if (!SetPixelFormat (myWindowDC, iPixelFormat, &pfd))
-  {
-    ReleaseDC (aParent, myWindowDC);
-    myWindowDC = NULL;
-
-    TCollection_AsciiString msg("OpenGl_Window::CreateWindow: SetPixelFormat failed. Error code: ");
-    msg += (int)GetLastError();
-    Aspect_GraphicDeviceDefinitionError::Raise (msg.ToCString());
-    return;
-  }
-
-  if (!myOwnGContext)
-  {
-    ThePreviousCtx = myGContext;
-  }
-  else
-  {
-    myGContext = wglCreateContext (myWindowDC);
-    if (myGContext == NULL)
-    {
-      ReleaseDC (aParent, myWindowDC);
-      myWindowDC = NULL;
-
-      TCollection_AsciiString msg ("OpenGl_Window::CreateWindow: wglCreateContext failed. Error code: ");
-      msg += (int )GetLastError();
-      Aspect_GraphicDeviceDefinitionError::Raise (msg.ToCString());
-      return;
-    }
-
-    Standard_Boolean isShared = Standard_True;
-    if (ThePreviousCtx == NULL)
-    {
-      ThePreviousCtx = myGContext;
-    }
-    else
-    {
-      // if we already have some shared context
-      GLCONTEXT shareCtx = OpenGl_ResourceCleaner::GetInstance()->GetSharedContext();
-      if (shareCtx != NULL)
-      {
-        // try to share context with one from resource cleaner list
-        isShared = (Standard_Boolean )wglShareLists (shareCtx, myGContext);
-      }
-      else
-      {
-        isShared = (Standard_Boolean )wglShareLists (ThePreviousCtx, myGContext);
-        // add shared ThePreviousCtx to a control list if it's not there
-        if (isShared)
-          OpenGl_ResourceCleaner::GetInstance()->AppendContext (ThePreviousCtx, isShared);
-      }
-    }
-
-    // add the context to OpenGl_ResourceCleaner control list
-    OpenGl_ResourceCleaner::GetInstance()->AppendContext (myGContext, isShared);
-  }
-
-  myWindow = aParent;
+  myGlContext->Init ((Aspect_Drawable )aWindow, (Aspect_Display )myDisplay->GetDisplay(), (Aspect_RenderingContext )aGContext);
 #endif
+  myGlContext->Share (theShareCtx);
 
   Init();
-  myGlContext->Init();
 }
 
 // =======================================================================
@@ -395,83 +516,48 @@ OpenGl_Window::OpenGl_Window (const Handle(OpenGl_Display)& theDisplay,
 // =======================================================================
 OpenGl_Window::~OpenGl_Window()
 {
-  DISPLAY* aDisp = (DISPLAY* )myDisplay->GetDisplay();
-  if (aDisp == NULL || !myOwnGContext)
-    return;
-
-#if (defined(_WIN32) || defined(__WIN32__))
-  OpenGl_ResourceCleaner::GetInstance()->RemoveContext (myGContext);
+#if defined(_WIN32)
+  HWND  aWindow   = (HWND  )myGlContext->myWindow;
+  HDC   aWindowDC = (HDC   )myGlContext->myWindowDC;
+  HGLRC aGContext = (HGLRC )myGlContext->myGContext;
+  myGlContext.Nullify();
 
-  if (wglGetCurrentContext() != NULL)
-    wglDeleteContext (myGContext);
-  ReleaseDC (myWindow, myWindowDC);
-
-  if (myDisplay->myMapOfWindows.Size() == 0)
-    ThePreviousCtx = 0;
-#else
-  // FSXXX sync necessary if non-direct rendering
-  glXWaitGL();
-
-  if (ThePreviousCtx == myGContext)
+  if (myOwnGContext)
   {
-    ThePreviousCtx = NULL;
-    if (myDisplay->myMapOfWindows.Size() > 0)
-    {
-      NCollection_DataMap<Standard_Integer, Handle(OpenGl_Window)>::Iterator it (myDisplay->myMapOfWindows);
-      ThePreviousCtx = it.Value()->myGContext;
-    }
-
-    // if this is the last remaining context, do not destroy it yet, to avoid
-    // losing any shared display lists (fonts...)
-    if (ThePreviousCtx)
-    {
-      OpenGl_ResourceCleaner::GetInstance()->RemoveContext(myGContext);
-      glXDestroyContext(aDisp, myGContext);
-    }
-    else
+    if (wglGetCurrentContext() != NULL)
     {
-      TheDeadGlxCtx = myGContext;
-      TheDeadGlxDpy = aDisp;
+      wglDeleteContext (aGContext);
     }
+    ReleaseDC (aWindow, aWindowDC);
   }
-  else
+#else
+  GLXDrawable aWindow   = (GLXDrawable )myGlContext->myWindow;
+  Display*    aDisplay  = (Display*    )myGlContext->myDisplay;
+  GLXContext  aGContext = (GLXContext  )myGlContext->myGContext;
+  myGlContext.Nullify();
+
+  if (aDisplay != NULL && myOwnGContext)
   {
-    OpenGl_ResourceCleaner::GetInstance()->RemoveContext (myGContext);
-    glXDestroyContext (aDisp, myGContext);
+    // FSXXX sync necessary if non-direct rendering
+    glXWaitGL();
+    glXDestroyContext (aDisplay, aGContext);
   }
 #endif
 }
 
+#endif // !__APPLE__
+
 // =======================================================================
 // function : Activate
 // purpose  :
 // =======================================================================
 Standard_Boolean OpenGl_Window::Activate()
 {
-  DISPLAY* aDisp = (DISPLAY* )myDisplay->GetDisplay();
-  if (aDisp == NULL)
-    return Standard_False;
-
-#if (defined(_WIN32) || defined(__WIN32__))
-  if (!wglMakeCurrent (myWindowDC, myGContext))
-  {
-    //GLenum errorcode = glGetError();
-    //const GLubyte *errorstring = gluErrorString(errorcode);
-    //printf("wglMakeCurrent failed: %d %s\n", errorcode, errorstring);
-    return Standard_False;
-  }
-#else
-  if (!glXMakeCurrent (aDisp, myWindow, myGContext))
-  {
-    // if there is no current context it might be impossible to use glGetError correctly
-    //printf("glXMakeCurrent failed!\n");
-    return Standard_False;
-  }
-#endif
-
-  return Standard_True;
+  return myGlContext->MakeCurrent();
 }
 
+#if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
+
 // =======================================================================
 // function : Resize
 // purpose  : call_subr_resize
@@ -489,14 +575,16 @@ void OpenGl_Window::Resize (const CALL_DEF_WINDOW& theCWindow)
   myWidth  = (Standard_Integer )theCWindow.dx;
   myHeight = (Standard_Integer )theCWindow.dy;
 
-#if (!defined(_WIN32) && !defined(__WIN32__))
-  XResizeWindow (aDisp, myWindow, (unsigned int )myWidth, (unsigned int )myHeight);
+#if !defined(_WIN32)
+  XResizeWindow (aDisp, myGlContext->myWindow, (unsigned int )myWidth, (unsigned int )myHeight);
   XSync (aDisp, False);
 #endif
 
   Init();
 }
 
+#endif // !__APPLE__
+
 // =======================================================================
 // function : ReadDepths
 // purpose  : TelReadDepths
@@ -533,6 +621,8 @@ void OpenGl_Window::SetBackgroundColor (const Standard_ShortReal theR,
   myBgColor.rgb[2] = theB;
 }
 
+#if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
+
 // =======================================================================
 // function : Init
 // purpose  :
@@ -542,9 +632,9 @@ void OpenGl_Window::Init()
   if (!Activate())
     return;
 
-#if (defined(_WIN32) || defined(__WIN32__))
+#if defined(_WIN32)
   RECT cr;
-  GetClientRect (myWindow, &cr);
+  GetClientRect ((HWND )myGlContext->myWindow, &cr);
   myWidth  = cr.right - cr.left;
   myHeight = cr.bottom - cr.top;
 #else
@@ -554,7 +644,7 @@ void OpenGl_Window::Init()
   unsigned int aNewWidth  = 0;
   unsigned int aNewHeight = 0;
   DISPLAY* aDisp = (DISPLAY* )myDisplay->GetDisplay();
-  XGetGeometry (aDisp, myWindow, &aRootWin, &aDummy, &aDummy, &aNewWidth, &aNewHeight, &aDummyU, &aDummyU);
+  XGetGeometry (aDisp, myGlContext->myWindow, &aRootWin, &aDummy, &aDummy, &aNewWidth, &aNewHeight, &aDummyU, &aDummyU);
   myWidth  = aNewWidth;
   myHeight = aNewHeight;
 #endif
@@ -566,6 +656,8 @@ void OpenGl_Window::Init()
   glDrawBuffer (GL_BACK);
 }
 
+#endif // !__APPLE__
+
 // =======================================================================
 // function : EnablePolygonOffset
 // purpose  : call_subr_enable_polygon_offset
@@ -684,3 +776,12 @@ void OpenGl_Window::MakeFrontAndBackBufCurrent() const
 {
   glDrawBuffer (GL_FRONT_AND_BACK);
 }
+
+// =======================================================================
+// function : GetGContext
+// purpose  :
+// =======================================================================
+GLCONTEXT OpenGl_Window::GetGContext() const
+{
+  return (GLCONTEXT )myGlContext->myGContext;
+}