From 8e6240cc253f22786410e24e613fa1e10bfb10a1 Mon Sep 17 00:00:00 2001 From: Alexander Gottwald Date: Thu, 27 May 2004 14:11:42 +0000 Subject: moved context creation to attach. Pixelformat is now set for every window and not for mainwindow. should fix error where programs using different visuals could not run together. --- GL/windows/ChangeLog | 7 ++ GL/windows/glwindows.h | 4 +- GL/windows/indirect.c | 187 ++++++++++++++++++++----------------------------- 3 files changed, 85 insertions(+), 113 deletions(-) create mode 100644 GL/windows/ChangeLog diff --git a/GL/windows/ChangeLog b/GL/windows/ChangeLog new file mode 100644 index 000000000..2b049ee73 --- /dev/null +++ b/GL/windows/ChangeLog @@ -0,0 +1,7 @@ +2004-05-27 Alexander Gottwald + + * glwindows.h: write current function and line in debug output + * indirect.c: moved actual setup and creation of windows GL context to + glWinCreateContextReal. + * indirect.c (glWinCreateContext): Deferred context creation to attach. + * indirect.c (glWinMakeCurrent): Check if context is NULL. Fixes segfault. diff --git a/GL/windows/glwindows.h b/GL/windows/glwindows.h index 0c785e9ee..1bfdcb715 100644 --- a/GL/windows/glwindows.h +++ b/GL/windows/glwindows.h @@ -50,8 +50,10 @@ extern glWinScreenRec glWinScreens[MAXSCREENS]; #define glWinScreenPriv(pScreen) glWinScreenRec *pScreenPriv = glWinGetScreenPriv(pScreen); #if 1 -#define GLWIN_DEBUG_MSG if (glWinDebugSettings.enableDebug) ErrorF +#define GLWIN_DEBUG_MSG(msg, args...) if (glWinDebugSettings.enableDebug) ErrorF("%s:%d: " msg, __FUNCTION__, __LINE__, ##args ) +#define GLWIN_DEBUG_MSG2(msg, args...) if (glWinDebugSettings.enableDebug) ErrorF(msg, ##args ) #else #define GLWIN_DEBUG_MSG(a, ...) +#define GLWIN_DEBUG_MSG2(a, ...) #endif diff --git a/GL/windows/indirect.c b/GL/windows/indirect.c index e5aa6fb08..80aec7ee9 100755 --- a/GL/windows/indirect.c +++ b/GL/windows/indirect.c @@ -46,6 +46,14 @@ #include "../../hw/xwin/winpriv.h" +#define GLWIN_DEBUG_HWND(hwnd) \ + if (glWinDebugSettings.dumpHWND) { \ + char buffer[1024]; \ + if (GetWindowText(hwnd, buffer, sizeof(buffer))==0) *buffer=0; \ + GLWIN_DEBUG_MSG("Got HWND %s (%p)\n", buffer, hwnd); \ + } + + /* ggs: needed to call back to glx with visual configs */ extern void GlxSetVisualConfigs(int nconfigs, __GLXvisualConfig *configs, void **configprivs); @@ -235,22 +243,21 @@ typedef struct { struct __GLcontextRec { struct __GLinterfaceRec interface; /* required to be first */ - HDC dc; - HGLRC ctx; - PIXELFORMATDESCRIPTOR pfd; - winWindowInfoRec winInfo; - int pixelFormat; + HGLRC ctx; /* Windows GL Context */ + + HDC dc; /* Windows Device Context */ + winWindowInfoRec winInfo; /* Window info from XWin */ + + PIXELFORMATDESCRIPTOR pfd; /* Pixelformat flags */ + int pixelFormat; /* Pixelformat index */ - /* set when attached */ - /* xp_surface_id sid; */ - - unsigned isAttached :1; + unsigned isAttached :1; /* Flag to track if context is attached */ }; static HDC glWinMakeDC(__GLcontext *gc) { HDC dc; - if (gc->winInfo.hrgn == NULL) + /*if (gc->winInfo.hrgn == NULL) { GLWIN_DEBUG_MSG("Creating region from RECT(%ld,%ld,%ld,%ld):", gc->winInfo.rect.left, @@ -258,13 +265,12 @@ static HDC glWinMakeDC(__GLcontext *gc) gc->winInfo.rect.right, gc->winInfo.rect.bottom); gc->winInfo.hrgn = CreateRectRgnIndirect(&gc->winInfo.rect); - GLWIN_DEBUG_MSG("%p\n", gc->winInfo.hrgn); - } - /*dc = GetDC(GetActiveWindow()); */ + GLWIN_DEBUG_MSG2("%p\n", gc->winInfo.hrgn); + }*/ + dc = GetDC(gc->winInfo.hwnd); /*dc = GetDCEx(gc->winInfo.hwnd, gc->winInfo.hrgn, DCX_WINDOW | DCX_NORESETATTRS ); */ - /*dc = GetWindowDC(gc->winInfo.hwnd);*/ if (dc == NULL) ErrorF("GetDC error: %s\n", glWinErrorMessage()); @@ -273,7 +279,6 @@ static HDC glWinMakeDC(__GLcontext *gc) static void unattach(__GLcontext *gc) { -#if 0 BOOL ret; GLWIN_DEBUG_MSG("unattach (ctx %p)\n", gc->ctx); if (!gc->isAttached) @@ -297,76 +302,89 @@ static void unattach(__GLcontext *gc) ErrorF("DeleteObject error: %s\n", glWinErrorMessage()); gc->winInfo.hrgn = NULL; } -#endif gc->isAttached = 0; } -static void attach(__GLcontext *gc, __GLdrawablePrivate *glPriv) +static BOOL glWinCreateContextReal(__GLcontext *gc, WindowPtr pWin) { HDC dc; -#if 0 - HGLRC old_ctx; -#endif BOOL ret; - __GLXdrawablePrivate *glxPriv = (__GLXdrawablePrivate *)glPriv->other; - GLWIN_DEBUG_MSG("attach (ctx %p)\n", gc->ctx); - - if (gc->isAttached) + if (pWin == NULL) { - ErrorF("called attach on an attached context\n"); - return; + GLWIN_DEBUG_MSG("Deferring until window is created\n"); + return FALSE; } - if (glxPriv->type == DRAWABLE_WINDOW) + winGetWindowInfo(pWin, &gc->winInfo); + + GLWIN_DEBUG_HWND(gc->winInfo.hwnd); + if (gc->winInfo.hwnd == NULL) { - WindowPtr pWin = (WindowPtr) glxPriv->pDraw; - winGetWindowInfo(pWin, &gc->winInfo); + GLWIN_DEBUG_MSG("Deferring until window is created\n"); + return FALSE; } - - if (glWinDebugSettings.dumpHWND) - GLWIN_DEBUG_MSG("Got HWND %p\n", gc->winInfo.hwnd); + dc = glWinMakeDC(gc); - + + if (glWinDebugSettings.dumpDC) + GLWIN_DEBUG_MSG("Got HDC %p\n", dc); + gc->pixelFormat = ChoosePixelFormat(dc, &gc->pfd); if (gc->pixelFormat == 0) { ErrorF("ChoosePixelFormat error: %s\n", glWinErrorMessage()); - return; + ReleaseDC(gc->winInfo.hwnd, dc); + return FALSE; } - + ret = SetPixelFormat(dc, gc->pixelFormat, &gc->pfd); if (!ret) { ErrorF("SetPixelFormat error: %s\n", glWinErrorMessage()); + ReleaseDC(gc->winInfo.hwnd, dc); + return FALSE; } - -#if 0 - old_ctx = gc->ctx; - gc->ctx = wglCreateContext(dc); + gc->ctx = wglCreateContext(dc); if (gc->ctx == NULL) { ErrorF("wglCreateContext error: %s\n", glWinErrorMessage()); ReleaseDC(gc->winInfo.hwnd, dc); + return FALSE; + } + ReleaseDC(gc->winInfo.hwnd, dc); + + return TRUE; +} + +static void attach(__GLcontext *gc, __GLdrawablePrivate *glPriv) +{ + __GLXdrawablePrivate *glxPriv = (__GLXdrawablePrivate *)glPriv->other; + + GLWIN_DEBUG_MSG("attach (ctx %p)\n", gc->ctx); + + if (gc->isAttached) + { + ErrorF("called attach on an attached context\n"); return; } - if (old_ctx != NULL) + if (glxPriv->type == DRAWABLE_WINDOW) { - GLWIN_DEBUG_MSG("Copying context\n"); - /* copy all rendering states to the new context */ - ret = wglCopyContext(old_ctx, gc->ctx, GL_ALL_ATTRIB_BITS); - if (!ret) - ErrorF("wglCopyContext error: %s\n", glWinErrorMessage()); - ret = wglDeleteContext(old_ctx); - if (!ret) - ErrorF("wglDeleteContext error: %s\n", glWinErrorMessage()); + WindowPtr pWin = (WindowPtr) glxPriv->pDraw; + if (pWin == NULL) + { + GLWIN_DEBUG_MSG("Deferring ChoosePixelFormat until window is created\n"); + } else + { + if (glWinCreateContextReal(gc, pWin)) + { + gc->isAttached = TRUE; + GLWIN_DEBUG_MSG("attached\n"); + } + } } -#endif - - ReleaseDC(gc->winInfo.hwnd, dc); - gc->isAttached = 1; } static GLboolean glWinLoseCurrent(__GLcontext *gc) @@ -401,14 +419,15 @@ static GLboolean glWinMakeCurrent(__GLcontext *gc) BOOL ret; HDC dc; - /*GLWIN_DEBUG_MSG("glWinMakeCurrent (ctx %p)\n", gc->ctx);*/ - if (!gc->isAttached) attach(gc, glPriv); + if (gc->ctx == NULL) { + ErrorF("Context is NULL\n"); + return GL_FALSE; + } + dc = glWinMakeDC(gc); - /*if (glWinDebugSettings.dumpDC)*/ - /* GLWIN_DEBUG_MSG("Got HDC %p\n", dc);*/ ret = wglMakeCurrent(dc, gc->ctx); if (!ret) ErrorF("glMakeCurrent error: %s\n", glWinErrorMessage()); @@ -645,8 +664,6 @@ static __GLinterface *glWinCreateContext(__GLimports *imports, __GLinterface *shareGC) { __GLcontext *result; - BOOL ret; - HDC dc; GLWIN_DEBUG_MSG("glWinCreateContext\n"); @@ -657,32 +674,9 @@ static __GLinterface *glWinCreateContext(__GLimports *imports, result->interface.imports = *imports; result->interface.exports = glWinExports; - winGetWindowInfo(NULL, &result->winInfo); - if (glWinDebugSettings.dumpHWND) - GLWIN_DEBUG_MSG("Got HWND %p\n", result->winInfo.hwnd); - -#if 1 - /* get DC of XWin main window */ - dc = GetDC(result->winInfo.hwnd); - result->dc = NULL; -#else - /* create a new DC */ - dc = CreateDC("DISPLAY",NULL,NULL,NULL); - if (dc == NULL) - { - ErrorF("CreateDC error: %s\n", glWinErrorMessage()); - free(result); - return NULL; - } - result->dc = dc; -#endif - if (glWinDebugSettings.dumpDC) - GLWIN_DEBUG_MSG("Got HDC %p\n", dc); - if (makeFormat(mode, &result->pfd)) { ErrorF("makeFormat failed\n"); - ReleaseDC(result->winInfo.hwnd, dc); free(result); return NULL; } @@ -690,37 +684,6 @@ static __GLinterface *glWinCreateContext(__GLimports *imports, if (glWinDebugSettings.dumpPFD) pfdOut(&result->pfd); - result->pixelFormat = ChoosePixelFormat(dc, &result->pfd); - if (result->pixelFormat == 0) - { - ErrorF("ChoosePixelFormat error: %s\n", glWinErrorMessage()); - ReleaseDC(result->winInfo.hwnd, dc); - free(result); - return NULL; - } - - GLWIN_DEBUG_MSG("ChoosePixelFormat done (%d)\n", result->pixelFormat); - - ret = SetPixelFormat(dc, result->pixelFormat, &result->pfd); - if (!ret) { - ErrorF("SetPixelFormat error: %s\n", glWinErrorMessage()); - ReleaseDC(result->winInfo.hwnd, dc); - free(result); - return NULL; - } - - result->ctx = wglCreateContext(dc); - result->isAttached = 0; - - if (result->ctx == NULL) { - ErrorF("wglCreateContext error: %s\n", glWinErrorMessage()); - ReleaseDC(result->winInfo.hwnd, dc); - free(result); - return NULL; - } - - ReleaseDC(result->winInfo.hwnd, dc); - GLWIN_DEBUG_MSG("glWinCreateContext done\n"); return (__GLinterface *)result; } -- cgit v1.2.3