// 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)
// =======================================================================
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),
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);
aVis = XGetVisualInfo (aDisp, aVisInfoMask, &aVisInfo, &aNbItems);
}
- WINDOW win;
-
if (!myOwnGContext)
{
if (aVis != NULL)
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;
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);
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;
}
}
- 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;
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;
}
/*
* (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);
-#if (defined(_WIN32) || defined(__WIN32__))
- myGlContext->Init (myWindow, myWindowDC, myGContext);
-#else
- myGlContext->Init (myWindow, myDisplay->GetDisplay(), myGContext);
-#endif
Init();
}
// =======================================================================
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 (wglGetCurrentContext() != NULL)
- wglDeleteContext (myGContext);
- ReleaseDC (myWindow, myWindowDC);
-
- if (myDisplay->myMapOfWindows.Size() == 0)
- ThePreviousCtx = 0;
-#else
- // FSXXX sync necessary if non-direct rendering
- glXWaitGL();
+#if defined(_WIN32)
+ HWND aWindow = (HWND )myGlContext->myWindow;
+ HDC aWindowDC = (HDC )myGlContext->myWindowDC;
+ HGLRC aGContext = (HGLRC )myGlContext->myGContext;
+ myGlContext.Nullify();
- if (ThePreviousCtx == myGContext)
+ if (myOwnGContext)
{
- ThePreviousCtx = NULL;
- if (myDisplay->myMapOfWindows.Size() > 0)
+ if (wglGetCurrentContext() != NULL)
{
- 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
- {
- 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 :
return myGlContext->MakeCurrent();
}
+#if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
+
// =======================================================================
// function : Resize
// purpose : call_subr_resize
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
myBgColor.rgb[2] = theB;
}
+#if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
+
// =======================================================================
// function : Init
// purpose :
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
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
glDrawBuffer (GL_BACK);
}
+#endif // !__APPLE__
+
// =======================================================================
// function : EnablePolygonOffset
// purpose : call_subr_enable_polygon_offset
{
glDrawBuffer (GL_FRONT_AND_BACK);
}
+
+// =======================================================================
+// function : GetGContext
+// purpose :
+// =======================================================================
+GLCONTEXT OpenGl_Window::GetGContext() const
+{
+ return (GLCONTEXT )myGlContext->myGContext;
+}