diff options
Diffstat (limited to 'xorg-server/hw/xwin/glx/indirect.c')
-rw-r--r-- | xorg-server/hw/xwin/glx/indirect.c | 511 |
1 files changed, 349 insertions, 162 deletions
diff --git a/xorg-server/hw/xwin/glx/indirect.c b/xorg-server/hw/xwin/glx/indirect.c index 3d01bed39..605f975c0 100644 --- a/xorg-server/hw/xwin/glx/indirect.c +++ b/xorg-server/hw/xwin/glx/indirect.c @@ -79,13 +79,19 @@ #endif #include "glwindows.h" +#include <glx/glheader.h> #include <glx/glxserver.h> #include <glx/glxutil.h> #include <glx/extension_string.h> +#include <glx/glxext.h> #include <GL/glxtokens.h> +#include <glx/glapitable.h> +#include <glx/glapi.h> #include <winpriv.h> #include <wgl_ext_api.h> +#include "win.h" +#include <winmsg.h> #include <winglobals.h> #define NUM_ELEMENTS(x) (sizeof(x)/ sizeof(x[1])) @@ -114,14 +120,17 @@ typedef struct __GLXWinConfig GLXWinConfig; struct __GLXWinContext { __GLXcontext base; HGLRC ctx; /* Windows GL Context */ + HDC hDC; /* Windows device context */ + HDC hreadDC; /* Windows device read context */ __GLXWinContext *shareContext; /* Context with which we will share display lists and textures */ HWND hwnd; /* For detecting when HWND has changed */ + HWND hreadwnd; + struct _glapi_table *Dispatch; }; struct __GLXWinDrawable { __GLXdrawable base; __GLXWinContext *drawContext; - __GLXWinContext *readContext; /* If this drawable is GLX_DRAWABLE_PBUFFER */ HPBUFFERARB hPbuffer; @@ -147,7 +156,9 @@ struct __GLXWinScreen { /* wrapped screen functions */ RealizeWindowProcPtr RealizeWindow; UnrealizeWindowProcPtr UnrealizeWindow; + DestroyWindowProcPtr DestroyWindow; CopyWindowProcPtr CopyWindow; + PositionWindowProcPtr PositionWindow; }; struct __GLXWinConfig { @@ -160,12 +171,33 @@ struct __GLXWinConfig { * Various debug helpers */ -#define GLWIN_DEBUG_HWND(hwnd) \ - if (glxWinDebugSettings.dumpHWND) { \ - char buffer[1024]; \ - if (GetWindowText(hwnd, buffer, sizeof(buffer))==0) *buffer=0; \ - GLWIN_DEBUG_MSG("Got HWND %p for window '%s'", hwnd, buffer); \ +#ifdef _DEBUG +void GLWIN_DEBUG_HWND(HWND hwnd) +{ + if (glxWinDebugSettings.dumpHWND) { + char buffer[1024]; + RECT Rect; + HDC hDc=GetDC(hwnd); + + if (GetWindowText(hwnd, buffer, sizeof(buffer))==0) *buffer=0; + GetWindowRect(hwnd,&Rect); + + GLWIN_DEBUG_MSG("Got HWND %p (hdc %p) for window '%s' (%d,%d,%d,%d)", hwnd, hDc, buffer, Rect.left, Rect.top, Rect.right, Rect.bottom); + ReleaseDC(hwnd,hDc); } +} + +void GLWIN_HDC_DEBUG_MSG(const char *Message, HDC hDc, HWND hwnd) +{ + char buffer[1024]; + RECT Rect; + + if (GetWindowText(hwnd, buffer, sizeof(buffer))==0) *buffer=0; + GetWindowRect(hwnd,&Rect); + + GLWIN_DEBUG_MSG("Got HDC %p (hwnd %p) for window '%s' (%d,%d,%d,%d)", hDc, hwnd, buffer, Rect.left, Rect.top, Rect.right, Rect.bottom); + +} glxWinDebugSettingsRec glxWinDebugSettings = { 0, 0, 0, 0, 0, 0 }; @@ -213,6 +245,7 @@ glxWinInitDebugSettings(void) glxWinDebugSettings.enableWGLcallTrace = 1; } } +#endif static const char * @@ -239,6 +272,8 @@ glxWinErrorMessage(void) static void pfdOut(const PIXELFORMATDESCRIPTOR * pfd); +#ifdef _DEBUG + #define DUMP_PFD_FLAG(flag) \ if (pfd->dwFlags & flag) { \ ErrorF("%s%s", pipesym, #flag); \ @@ -396,6 +431,7 @@ fbConfigsDump(unsigned int n, __GLXconfig * c) c = c->next; } } +#endif /* ---------------------------------------------------------------------- */ /* @@ -417,11 +453,13 @@ static __GLXdrawable *glxWinCreateDrawable(ClientPtr client, static Bool glxWinRealizeWindow(WindowPtr pWin); static Bool glxWinUnrealizeWindow(WindowPtr pWin); +static Bool glxWinDestroyWindow(WindowPtr pWin); static void glxWinCopyWindow(WindowPtr pWindow, DDXPointRec ptOldOrg, RegionPtr prgnSrc); +static Bool glxWinPositionWindow(WindowPtr pWindow, int x, int y); static HDC glxWinMakeDC(__GLXWinContext * gc, __GLXWinDrawable * draw, - HDC * hdc, HWND * hwnd); + HWND * hwnd); static void glxWinReleaseDC(HWND hwnd, HDC hdc, __GLXWinDrawable * draw); static void glxWinCreateConfigs(HDC dc, glxWinScreen * screen); @@ -447,7 +485,8 @@ __GLXprovider __glXWGLProvider = { void glxWinPushNativeProvider(void) { - GlxPushProvider(&__glXWGLProvider); + if (g_fNativeGl) + GlxPushProvider(&__glXWGLProvider); } /* ---------------------------------------------------------------------- */ @@ -475,6 +514,15 @@ glxWinScreenSwapInterval(__GLXdrawable * drawable, int interval) return ret; } +static LRESULT CALLBACK GlxWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) +{ + if (uMsg== WM_NCHITTEST) { + return HTTRANSPARENT; + } + else + return DefWindowProc(hwnd, uMsg, wParam, lParam); +} + /* Report the extensions split and formatted to avoid overflowing a line */ @@ -493,7 +541,8 @@ glxLogExtensions(const char *prefix, const char *extensions) strl = strtok(str, " "); if (strl == NULL) strl = ""; - ErrorF("%s%s", prefix, strl); + winDebug("%s%s", prefix, strl); + length = strlen(prefix) + strlen(strl); while (1) { @@ -502,20 +551,20 @@ glxLogExtensions(const char *prefix, const char *extensions) break; if (length + strlen(strl) + 1 > 120) { - ErrorF("\n"); - ErrorF("%s", prefix); + winDebug("\n"); + winDebug("%s", prefix); length = strlen(prefix); } else { - ErrorF(" "); + winDebug(" "); length++; } - ErrorF("%s", strl); + winDebug("%s", strl); length = length + strlen(strl); } - ErrorF("\n"); + winDebug("\n"); free(str); } @@ -534,7 +583,9 @@ glxWinScreenProbe(ScreenPtr pScreen) GLWIN_DEBUG_MSG("glxWinScreenProbe"); +#ifdef _DEBUG glxWinInitDebugSettings(); +#endif if (pScreen == NULL) return NULL; @@ -555,24 +606,23 @@ glxWinScreenProbe(ScreenPtr pScreen) return NULL; // create window class -#define WIN_GL_TEST_WINDOW_CLASS "XWinGLTest" { static wATOM glTestWndClass = 0; if (glTestWndClass == 0) { WNDCLASSEX wc; - + glTestWndClass=1; wc.cbSize = sizeof(WNDCLASSEX); - wc.style = CS_HREDRAW | CS_VREDRAW; - wc.lpfnWndProc = DefWindowProc; + wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC ; + wc.lpfnWndProc = GlxWindowProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; - wc.hInstance = GetModuleHandle(NULL); + wc.hInstance = g_hInstance; wc.hIcon = 0; wc.hCursor = 0; - wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH); + wc.hbrBackground = 0; wc.lpszMenuName = NULL; - wc.lpszClassName = WIN_GL_TEST_WINDOW_CLASS; + wc.lpszClassName = WIN_GL_WINDOW_CLASS; wc.hIconSm = 0; RegisterClassEx(&wc); } @@ -580,9 +630,9 @@ glxWinScreenProbe(ScreenPtr pScreen) // create an invisible window for a scratch DC hwnd = CreateWindowExA(0, - WIN_GL_TEST_WINDOW_CLASS, + WIN_GL_WINDOW_CLASS, "XWin GL Renderer Capabilities Test Window", - 0, 0, 0, 0, 0, NULL, NULL, GetModuleHandle(NULL), + 0, 0, 0, 0, 0, NULL, NULL, g_hInstance, NULL); if (hwnd == NULL) LogMessage(X_ERROR, @@ -593,17 +643,20 @@ glxWinScreenProbe(ScreenPtr pScreen) // we must set a pixel format before we can create a context, just use the first one... SetPixelFormat(hdc, 1, NULL); hglrc = wglCreateContext(hdc); - wglMakeCurrent(hdc, hglrc); + if (!wglMakeCurrent(hdc, hglrc)) { + DWORD ErrorCode=GetLastError(); + ErrorF("wglMakeCurrent error: %x dc %p ctx %p\n", ErrorCode,hdc,hglrc); + } // initialize wgl extension proc pointers (don't call them before here...) // (but we need to have a current context for them to be resolvable) wglResolveExtensionProcs(); /* Dump out some useful information about the native renderer */ - ErrorF("GL_VERSION: %s\n", glGetString(GL_VERSION)); - ErrorF("GL_VENDOR: %s\n", glGetString(GL_VENDOR)); + winDebug("GL_VERSION: %s\n", glGetString(GL_VERSION)); + winDebug("GL_VENDOR: %s\n", glGetString(GL_VENDOR)); gl_renderer = (const char *) glGetString(GL_RENDERER); - ErrorF("GL_RENDERER: %s\n", gl_renderer); + winDebug("GL_RENDERER: %s\n", gl_renderer); gl_extensions = (const char *) glGetString(GL_EXTENSIONS); wgl_extensions = wglGetExtensionsStringARBWrapper(hdc); if (!wgl_extensions) @@ -780,8 +833,10 @@ glxWinScreenProbe(ScreenPtr pScreen) ReleaseDC(hwnd, hdc); DestroyWindow(hwnd); +#ifdef _DEBUG // dump out fbConfigs now fbConfigIds and visualIDs have been assigned fbConfigsDump(screen->base.numFBConfigs, screen->base.fbconfigs); +#endif /* Wrap RealizeWindow, UnrealizeWindow and CopyWindow on this screen */ screen->RealizeWindow = pScreen->RealizeWindow; @@ -790,6 +845,10 @@ glxWinScreenProbe(ScreenPtr pScreen) pScreen->UnrealizeWindow = glxWinUnrealizeWindow; screen->CopyWindow = pScreen->CopyWindow; pScreen->CopyWindow = glxWinCopyWindow; + screen->PositionWindow = pScreen->PositionWindow; + pScreen->PositionWindow = glxWinPositionWindow; + screen->DestroyWindow = pScreen->DestroyWindow; + pScreen->DestroyWindow = glxWinDestroyWindow; return &screen->base; @@ -812,6 +871,7 @@ glxWinRealizeWindow(WindowPtr pWin) Bool result; ScreenPtr pScreen = pWin->drawable.pScreen; glxWinScreen *screenPriv = (glxWinScreen *) glxGetScreen(pScreen); + winWindowPriv(pWin); GLWIN_DEBUG_MSG("glxWinRealizeWindow"); @@ -819,7 +879,11 @@ glxWinRealizeWindow(WindowPtr pWin) pScreen->RealizeWindow = screenPriv->RealizeWindow; result = pScreen->RealizeWindow(pWin); pScreen->RealizeWindow = glxWinRealizeWindow; - + + // Check if ze need to move the window\n + if (pWinPriv->fWglUsed && pWinPriv->hWnd) { + ShowWindow(pWinPriv->hWnd,SW_SHOWNOACTIVATE); + } return result; } @@ -855,17 +919,88 @@ glxWinCopyWindow(WindowPtr pWindow, DDXPointRec ptOldOrg, RegionPtr prgnSrc) } static Bool +glxWinPositionWindow(WindowPtr pWin, int x, int y) +{ + Bool result; + ScreenPtr pScreen = pWin->drawable.pScreen; + glxWinScreen *screenPriv = (glxWinScreen *) glxGetScreen(pScreen); + winWindowPriv(pWin); + + pScreen->PositionWindow = screenPriv->PositionWindow; + result = pScreen->PositionWindow(pWin, x, y); + pScreen->PositionWindow = glxWinPositionWindow; + + if (pWinPriv->fWglUsed && pWinPriv->hWnd) + { + MoveWindow(pWinPriv->hWnd, + pWin->drawable.x, + pWin->drawable.y, + pWin->drawable.width, + pWin->drawable.height, + FALSE); + winDebug("Move window %x, %x, %d, %d, %d, %d\n",pWinPriv->hWnd,GetParent(pWinPriv->hWnd), pWin->drawable.x, pWin->drawable.y, pWin->drawable.width, pWin->drawable.height); + } + return result; +} + + +static Bool glxWinUnrealizeWindow(WindowPtr pWin) { Bool result; ScreenPtr pScreen = pWin->drawable.pScreen; glxWinScreen *screenPriv = (glxWinScreen *) glxGetScreen(pScreen); + winWindowPriv(pWin); GLWIN_DEBUG_MSG("glxWinUnrealizeWindow"); - pScreen->UnrealizeWindow = screenPriv->UnrealizeWindow; - result = pScreen->UnrealizeWindow(pWin); - pScreen->UnrealizeWindow = glxWinUnrealizeWindow; + if (pWinPriv->fWglUsed && pWinPriv->hWnd) + { + ShowWindow(pWinPriv->hWnd,SW_HIDE); + result = TRUE; + } + else + { + pScreen->UnrealizeWindow = screenPriv->UnrealizeWindow; + result = pScreen->UnrealizeWindow(pWin); + pScreen->UnrealizeWindow = glxWinUnrealizeWindow; + } + + + return result; +} + +static Bool +glxWinDestroyWindow(WindowPtr pWin) +{ + Bool result; + ScreenPtr pScreen = pWin->drawable.pScreen; + glxWinScreen *screenPriv = (glxWinScreen *)glxGetScreen(pScreen); + __GLXWinDrawable *pGlxDraw; + winWindowPriv(pWin); + + GLWIN_DEBUG_MSG("glxWinDestroyWindow"); + + dixLookupResourceByType((pointer) &pGlxDraw, pWin->drawable.id, __glXDrawableRes, NullClient, DixUnknownAccess); + + if (pGlxDraw && pGlxDraw->drawContext) + { + if (pGlxDraw->drawContext->hwnd!=pWinPriv->hWnd) + ErrorF("Wrong assumption\n"); + glxWinReleaseDC(pGlxDraw->drawContext->hwnd, pGlxDraw->drawContext->hDC, pGlxDraw); + pGlxDraw->drawContext->hDC=NULL; + pGlxDraw->drawContext->hwnd=NULL; + } + if (pWinPriv->fWglUsed && pWinPriv->hWnd) + { + DestroyWindow(pWinPriv->hWnd); + pWinPriv->hWnd=NULL; + pWinPriv->fWglUsed=0; + } + + pScreen->DestroyWindow = screenPriv->DestroyWindow; + result = pScreen->DestroyWindow(pWin); + pScreen->DestroyWindow = glxWinDestroyWindow; return result; } @@ -878,8 +1013,6 @@ glxWinUnrealizeWindow(WindowPtr pWin) static GLboolean glxWinDrawableSwapBuffers(ClientPtr client, __GLXdrawable * base) { - HDC dc; - HWND hwnd; BOOL ret; __GLXWinDrawable *draw = (__GLXWinDrawable *) base; @@ -900,13 +1033,7 @@ glxWinDrawableSwapBuffers(ClientPtr client, __GLXdrawable * base) assert((draw->drawContext->base.drawPriv == NULL) || (draw->drawContext->base.drawPriv == base)); - dc = glxWinMakeDC(draw->drawContext, draw, &dc, &hwnd); - if (dc == NULL) - return GL_FALSE; - - ret = wglSwapLayerBuffers(dc, WGL_SWAP_MAIN_PLANE); - - glxWinReleaseDC(hwnd, dc, draw); + ret = SwapBuffers(draw->drawContext->hDC); if (!ret) { ErrorF("wglSwapBuffers failed: %s\n", glxWinErrorMessage()); @@ -960,6 +1087,7 @@ glxWinDrawableDestroy(__GLXdrawable * base) } ((PixmapPtr) glxPriv->base.pDraw)->devPrivate.ptr = glxPriv->pOldBits; + glxPriv->base.pDraw->pScreen->DestroyPixmap((PixmapPtr)glxPriv->base.pDraw); /* Decrement reference count since we do not use it any more */ } GLWIN_DEBUG_MSG("glxWinDestroyDrawable"); @@ -974,13 +1102,11 @@ glxWinCreateDrawable(ClientPtr client, { __GLXWinDrawable *glxPriv; - glxPriv = malloc(sizeof *glxPriv); + glxPriv = calloc(1, sizeof *glxPriv); if (glxPriv == NULL) return NULL; - memset(glxPriv, 0, sizeof *glxPriv); - if (!__glXDrawableInit (&glxPriv->base, screen, pDraw, type, glxDrawId, conf)) { free(glxPriv); @@ -1050,28 +1176,25 @@ glxWinSetPixelFormat(__GLXWinContext * gc, HDC hdc, int bppOverride, __GLXconfig *config = gc->base.config; GLXWinConfig *winConfig = (GLXWinConfig *) config; - GLWIN_DEBUG_MSG("glxWinSetPixelFormat: pixelFormatIndex %d", - winConfig->pixelFormatIndex); - - /* - Normally, we can just use the the pixelFormatIndex corresponding - to the fbconfig which has been specified by the client - */ - if (! - ((bppOverride && - (bppOverride != - (config->redBits + config->greenBits + config->blueBits))) - || ((config->drawableType & drawableTypeOverride) == 0))) { - if (!SetPixelFormat(hdc, winConfig->pixelFormatIndex, NULL)) { - ErrorF("SetPixelFormat error: %s\n", glxWinErrorMessage()); - return FALSE; + WindowPtr pWin; + __GLXWinDrawable *drawPriv = (__GLXWinDrawable *)gc->base.drawPriv; + pWin = (WindowPtr) drawPriv->base.pDraw; + { + winWindowPriv(pWin); + if (pWinPriv->OpenGlWindow) + { + ErrorF("Not Setting pixel format to %d on hdc %x for window %x (not allowed on windows)\n",winConfig->pixelFormatIndex,hdc,pWinPriv->hWnd); + return TRUE; /* Pixel format is already set on this window so it cannot be changed anymore */ } - - return TRUE; } + GLWIN_DEBUG_MSG("glxWinSetPixelFormat: pixelFormatIndex %d", winConfig->pixelFormatIndex); /* + Normally, we can just use the the pixelFormatIndex corresponding + to the fbconfig which has been specified by the client + */ + /* However, in certain special cases this pixel format will be incompatible with the use we are going to put it to, so we need to re-evaluate the pixel format to use: @@ -1103,8 +1226,10 @@ glxWinSetPixelFormat(__GLXWinContext * gc, HDC hdc, int bppOverride, return FALSE; } +#ifdef _DEBUG if (glxWinDebugSettings.dumpPFD) pfdOut(&pfd); +#endif if (bppOverride) { GLWIN_DEBUG_MSG("glxWinSetPixelFormat: Forcing bpp from %d to %d\n", @@ -1124,6 +1249,7 @@ glxWinSetPixelFormat(__GLXWinContext * gc, HDC hdc, int bppOverride, ("ChoosePixelFormat: chose pixelFormatIndex %d (rather than %d as originally planned)\n", pixelFormat, winConfig->pixelFormatIndex); + ErrorF("Setting pixel format 2 to %d on hdc %x\n",pixelFormat,hdc); if (!SetPixelFormat(hdc, pixelFormat, &pfd)) { ErrorF("SetPixelFormat error: %s\n", glxWinErrorMessage()); return FALSE; @@ -1133,31 +1259,58 @@ glxWinSetPixelFormat(__GLXWinContext * gc, HDC hdc, int bppOverride, int pixelFormat = fbConfigToPixelFormatIndex(hdc, gc->base.config, drawableTypeOverride, winScreen); - if (pixelFormat == 0) { - ErrorF("wglChoosePixelFormat error: %s\n", glxWinErrorMessage()); - return FALSE; - } - - GLWIN_DEBUG_MSG("wglChoosePixelFormat: chose pixelFormatIndex %d", - pixelFormat); - ErrorF - ("wglChoosePixelFormat: chose pixelFormatIndex %d (rather than %d as originally planned)\n", - pixelFormat, winConfig->pixelFormatIndex); + if (pixelFormat != 0) { + GLWIN_DEBUG_MSG("wglChoosePixelFormat: chose pixelFormatIndex %d", pixelFormat); - if (!SetPixelFormat(hdc, pixelFormat, NULL)) { - ErrorF("SetPixelFormat error: %s\n", glxWinErrorMessage()); - return FALSE; + ErrorF("Setting pixel format 3 to %d on hdc %x\n",pixelFormat,hdc); + if (!SetPixelFormat(hdc, pixelFormat, NULL)) { + ErrorF("SetPixelFormat error: %s\n", glxWinErrorMessage()); + return FALSE; + } + return TRUE; + } + else { + /* There was an error choose some default for the moment */ + PIXELFORMATDESCRIPTOR pfd = { + sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd + 1, // version number + PFD_DRAW_TO_WINDOW | // support window + PFD_SUPPORT_OPENGL | // support OpenGL + PFD_DOUBLEBUFFER, // double buffered + PFD_TYPE_RGBA, // RGBA type + 24, // 24-bit color depth + 0, 0, 0, 0, 0, 0, // color bits ignored + 0, // no alpha buffer + 0, // shift bit ignored + 0, // no accumulation buffer + 0, 0, 0, 0, // accum bits ignored + 32, // 32-bit z-buffer + 0, // no stencil buffer + 0, // no auxiliary buffer + PFD_MAIN_PLANE, // main layer + 0, // reserved + 0, 0, 0 // layer masks ignored + }; + int iPixelFormat; + + // get the best available match of pixel format for the device context + iPixelFormat = ChoosePixelFormat(hdc, &pfd); + + ErrorF("Setting pixel format 4 to %d on hdc %x\n",iPixelFormat,hdc); + // make that the pixel format of the device context + if (!SetPixelFormat(hdc, iPixelFormat, &pfd)) { + ErrorF("SetPixelFormat error: %s\n", glxWinErrorMessage()); + return FALSE; + } } } - return TRUE; } static HDC -glxWinMakeDC(__GLXWinContext * gc, __GLXWinDrawable * draw, HDC * hdc, - HWND * hwnd) +glxWinMakeDC(__GLXWinContext *gc, __GLXWinDrawable *draw, HWND *hwnd) { - *hdc = NULL; + HDC hdc = NULL; *hwnd = NULL; if (draw == NULL) { @@ -1184,45 +1337,52 @@ glxWinMakeDC(__GLXWinContext * gc, __GLXWinDrawable * draw, HDC * hdc, return NULL; } - *hdc = GetDC(*hwnd); + if (!gc->hDC) { + winWindowPriv(pWin); - if (*hdc == NULL) - ErrorF("GetDC error: %s\n", glxWinErrorMessage()); + hdc = GetDC(*hwnd); - /* Check if the hwnd has changed... */ - if (*hwnd != gc->hwnd) { - if (glxWinDebugSettings.enableTrace) - GLWIN_DEBUG_HWND(*hwnd); + if (hdc == NULL) + ErrorF("GetDC error: %s: hwnd %x, gc %p, gc->ctx %p ,gc->hwnd %p\n", glxWinErrorMessage(), *hwnd, gc, gc->ctx, gc->hwnd); - GLWIN_TRACE_MSG - ("for context %p (native ctx %p), hWnd changed from %p to %p", - gc, gc->ctx, gc->hwnd, *hwnd); - gc->hwnd = *hwnd; - - /* We must select a pixelformat, but SetPixelFormat can only be called once for a window... */ - if (!glxWinSetPixelFormat(gc, *hdc, 0, GLX_WINDOW_BIT)) { - ErrorF("glxWinSetPixelFormat error: %s\n", - glxWinErrorMessage()); - ReleaseDC(*hwnd, *hdc); - *hdc = NULL; - return NULL; - } + glxWinSetPixelFormat(gc, hdc, 0, GLX_WINDOW_BIT); + pWinPriv->OpenGlWindow=TRUE; /* Identify it as an opengl window, also used to check if the pixel format is already set */ + gc->ctx = wglCreateContext(hdc); } + +#ifdef _DEBUG + if (glxWinDebugSettings.enableTrace) + GLWIN_DEBUG_HWND(*hwnd); + + GLWIN_TRACE_MSG + ("for context %p (native ctx %p), hWnd changed from %p to %p", + gc, gc->ctx, gc->hwnd, *hwnd); +#endif + if (gc->hwnd!=*hwnd) + ErrorF("Window changed handle from %x to %x\n", gc->hwnd, *hwnd); + + gc->hwnd = *hwnd; } break; case GLX_DRAWABLE_PBUFFER: { - *hdc = wglGetPbufferDCARBWrapper(draw->hPbuffer); + hdc = wglGetPbufferDCARBWrapper(draw->hPbuffer); - if (*hdc == NULL) + if (hdc == NULL) ErrorF("GetDC (pbuffer) error: %s\n", glxWinErrorMessage()); + + gc->ctx = wglCreateContext(hdc); } break; case GLX_DRAWABLE_PIXMAP: { - *hdc = draw->dibDC; + hdc = draw->dibDC; +#ifdef _DEBUG + if (glxWinDebugSettings.dumpDC) + GLWIN_DEBUG_MSG("Got PIXMAP HDC %p for window %p", hdc, *hwnd); +#endif } break; @@ -1233,10 +1393,12 @@ glxWinMakeDC(__GLXWinContext * gc, __GLXWinDrawable * draw, HDC * hdc, } } +#ifdef _DEBUG if (glxWinDebugSettings.dumpDC) - GLWIN_DEBUG_MSG("Got HDC %p", *hdc); + GLWIN_HDC_DEBUG_MSG("Got HDC %p for window %p", hdc, *hwnd); +#endif - return *hdc; + return hdc; } static void @@ -1279,7 +1441,6 @@ glxWinReleaseDC(HWND hwnd, HDC hdc, __GLXWinDrawable * draw) static void glxWinDeferredCreateContext(__GLXWinContext * gc, __GLXWinDrawable * draw) { - HDC dc; HWND hwnd; GLWIN_DEBUG_MSG @@ -1312,13 +1473,16 @@ glxWinDeferredCreateContext(__GLXWinContext * gc, __GLXWinDrawable * draw) case GLX_DRAWABLE_PBUFFER: { + WindowPtr pWin = (WindowPtr) draw->base.pDraw; if (draw->hPbuffer == NULL) { __GLXscreen *screen; glxWinScreen *winScreen; int pixelFormat; // XXX: which DC are supposed to use??? - HDC screenDC = GetDC(NULL); + ScreenPtr pScreen = pWin->drawable.pScreen; + winPrivScreenPtr pWinScreen = winGetScreenPriv(pScreen); + HDC screenDC=pWinScreen->hdcScreen; if (!(gc->base.config->drawableType & GLX_PBUFFER_BIT)) { ErrorF @@ -1330,7 +1494,7 @@ glxWinDeferredCreateContext(__GLXWinContext * gc, __GLXWinDrawable * draw) pixelFormat = fbConfigToPixelFormatIndex(screenDC, gc->base.config, - GLX_DRAWABLE_PBUFFER, winScreen); + GLX_PBUFFER_BIT, winScreen); if (pixelFormat == 0) { ErrorF("wglChoosePixelFormat error: %s\n", glxWinErrorMessage()); @@ -1341,7 +1505,6 @@ glxWinDeferredCreateContext(__GLXWinContext * gc, __GLXWinDrawable * draw) wglCreatePbufferARBWrapper(screenDC, pixelFormat, draw->base.pDraw->width, draw->base.pDraw->height, NULL); - ReleaseDC(NULL, screenDC); if (draw->hPbuffer == NULL) { ErrorF("wglCreatePbufferARBWrapper error: %s\n", @@ -1396,6 +1559,8 @@ glxWinDeferredCreateContext(__GLXWinContext * gc, __GLXWinDrawable * draw) draw->pOldBits = ((PixmapPtr) draw->base.pDraw)->devPrivate.ptr; ((PixmapPtr) draw->base.pDraw)->devPrivate.ptr = pBits; + ((PixmapPtr)draw->base.pDraw)->refcnt++; /* Increment reference count to be sure it is not freed before the glxdrawable is destroyed */ + // Select the DIB into the DC draw->hOldDIB = SelectObject(draw->dibDC, draw->hDIB); if (!draw->hOldDIB) { @@ -1423,11 +1588,12 @@ glxWinDeferredCreateContext(__GLXWinContext * gc, __GLXWinDrawable * draw) } } - dc = glxWinMakeDC(gc, draw, &dc, &hwnd); - gc->ctx = wglCreateContext(dc); - glxWinReleaseDC(hwnd, dc, draw); + gc->hDC = glxWinMakeDC(gc, draw, &hwnd); if (gc->ctx == NULL) { + glxWinReleaseDC(hwnd, gc->hDC, draw); + gc->hDC=0; + ErrorF("wglCreateContext error: %s\n", glxWinErrorMessage()); return; } @@ -1437,7 +1603,7 @@ glxWinDeferredCreateContext(__GLXWinContext * gc, __GLXWinDrawable * draw) gc, gc->ctx, draw); // if the native context was created successfully, shareLists if needed - if (gc->ctx && gc->shareContext) { + if (gc->ctx && gc->shareContext && gc->shareContext->ctx) { GLWIN_DEBUG_MSG ("glxWinCreateContextReal shareLists with context %p (native ctx %p)", gc->shareContext, gc->shareContext->ctx); @@ -1459,33 +1625,24 @@ glxWinContextMakeCurrent(__GLXcontext * base) { __GLXWinContext *gc = (__GLXWinContext *) base; BOOL ret; - HDC drawDC; - HDC readDC = NULL; - __GLXdrawable *drawPriv; - __GLXdrawable *readPriv = NULL; - HWND hDrawWnd; - HWND hReadWnd; + __GLXWinDrawable *drawPriv; - GLWIN_TRACE_MSG("glxWinContextMakeCurrent context %p (native ctx %p)", gc, - gc->ctx); +#ifdef _DEBUG + GLWIN_TRACE_MSG("glxWinContextMakeCurrent context %p (native ctx %p)", gc, gc->ctx); +#endif /* Keep a note of the last active context in the drawable */ - drawPriv = gc->base.drawPriv; - ((__GLXWinDrawable *) drawPriv)->drawContext = gc; + drawPriv = (__GLXWinDrawable *)gc->base.drawPriv; + drawPriv->drawContext = gc; if (gc->ctx == NULL) { - glxWinDeferredCreateContext(gc, (__GLXWinDrawable *) drawPriv); + glxWinDeferredCreateContext(gc, drawPriv); } + _glapi_set_dispatch(gc->Dispatch); if (gc->ctx == NULL) { ErrorF("glxWinContextMakeCurrent: Native context is NULL\n"); - return FALSE; - } - - drawDC = - glxWinMakeDC(gc, (__GLXWinDrawable *) drawPriv, &drawDC, &hDrawWnd); - if (drawDC == NULL) { - ErrorF("glxWinMakeDC failed for drawDC\n"); + drawPriv->drawContext = NULL; /* clear last active context because we return error */ return FALSE; } @@ -1496,16 +1653,14 @@ glxWinContextMakeCurrent(__GLXcontext * base) use the wglMakeContextCurrent extension to make the context current drawing to one DC and reading from the other */ - readPriv = gc->base.readPriv; - readDC = - glxWinMakeDC(gc, (__GLXWinDrawable *) readPriv, &readDC, &hReadWnd); - if (readDC == NULL) { + gc->hreadDC = glxWinMakeDC(gc, (__GLXWinDrawable *)gc->base.readPriv, &gc->hreadwnd); + if (gc->hreadDC == NULL) { ErrorF("glxWinMakeDC failed for readDC\n"); - glxWinReleaseDC(hDrawWnd, drawDC, (__GLXWinDrawable *) drawPriv); + drawPriv->drawContext = NULL; /* clear last active context because we return error */ return FALSE; } - ret = wglMakeContextCurrentARBWrapper(drawDC, readDC, gc->ctx); + ret = wglMakeContextCurrentARBWrapper(gc->hDC, gc->hreadDC, gc->ctx); if (!ret) { ErrorF("wglMakeContextCurrentARBWrapper error: %s\n", glxWinErrorMessage()); @@ -1513,18 +1668,25 @@ glxWinContextMakeCurrent(__GLXcontext * base) } else { /* Otherwise, just use wglMakeCurrent */ - ret = wglMakeCurrent(drawDC, gc->ctx); + if (!gc->hDC) { + /* It probably has been release by loseCurrent, so create it again */ + gc->hDC = glxWinMakeDC(gc, drawPriv, &gc->hwnd); + } + ret = wglMakeCurrent(gc->hDC, gc->ctx); if (!ret) { - ErrorF("wglMakeCurrent error: %s\n", glxWinErrorMessage()); + DWORD ErrorCode=GetLastError(); + ErrorF("wglMakeCurrent error: %x dc %p ctx %p\n", ErrorCode,gc->hDC,gc->ctx); + if (!ErrorCode) { + ErrorF("Error code was 0, assuming no error.\n"); + ret=TRUE; + } } } // apparently make current could fail if the context is current in a different thread, // but that shouldn't be able to happen in the current server... - - glxWinReleaseDC(hDrawWnd, drawDC, (__GLXWinDrawable *) drawPriv); - if (readDC) - glxWinReleaseDC(hReadWnd, readDC, (__GLXWinDrawable *) readPriv); + if (!ret) + drawPriv->drawContext = NULL; /* clear last active context because we return error */ return ret; } @@ -1532,24 +1694,33 @@ glxWinContextMakeCurrent(__GLXcontext * base) static int glxWinContextLoseCurrent(__GLXcontext * base) { - BOOL ret; + BOOL ret=TRUE; __GLXWinContext *gc = (__GLXWinContext *) base; + __GLXWinDrawable *drawPriv = (__GLXWinDrawable *)gc->base.drawPriv; - GLWIN_TRACE_MSG("glxWinContextLoseCurrent context %p (native ctx %p)", gc, - gc->ctx); +#ifdef _DEBUG + GLWIN_TRACE_MSG("glxWinContextLoseCurrent context %p (native ctx %p)", gc, gc->ctx); +#endif - /* - An error seems to be reported if we try to make no context current - if there is already no current context, so avoid doing that... - */ - if (__glXLastContext != NULL) { - ret = wglMakeCurrent(NULL, NULL); /* We don't need a DC when setting no current context */ - if (!ret) - ErrorF("glxWinContextLoseCurrent error: %s\n", - glxWinErrorMessage()); + /* Clear the last active context in the drawable */ + if (drawPriv) drawPriv->drawContext = NULL; + + if (wglGetCurrentContext()==gc->ctx) + { + /* Only do this when we are sure we are currently the active, otherwise we are deactivating the wrong one (this is happening!!!) */ + ret = wglMakeCurrent(NULL, NULL); + if (!ret) + ErrorF("glxWinContextLoseCurrent error: %s\n", glxWinErrorMessage()); + } + else + { + return FALSE; } - return TRUE; + base->currentClient=NULL; /* It looks like glx is not doing this */ + _glapi_set_dispatch(NULL); + + return ret; } static int @@ -1576,27 +1747,35 @@ glxWinContextDestroy(__GLXcontext * base) __GLXWinContext *gc = (__GLXWinContext *) base; if (gc != NULL) { + __GLXWinDrawable *drawPriv = (__GLXWinDrawable *)gc->base.drawPriv; + GLWIN_DEBUG_MSG("GLXcontext %p destroyed (native ctx %p)", base, gc->ctx); if (gc->ctx) { + BOOL ret; /* It's bad style to delete the context while it's still current */ if (wglGetCurrentContext() == gc->ctx) { wglMakeCurrent(NULL, NULL); } - { - BOOL ret = wglDeleteContext(gc->ctx); - - if (!ret) - ErrorF("wglDeleteContext error: %s\n", - glxWinErrorMessage()); - } + ret = wglDeleteContext(gc->ctx); + if (!ret) + ErrorF("wglDeleteContext error: %s\n", glxWinErrorMessage()); + if (drawPriv && gc->hDC) glxWinReleaseDC(gc->hwnd, gc->hDC, drawPriv); + if (gc->base.readPriv && gc->hreadDC) glxWinReleaseDC(gc->hreadwnd, gc->hreadDC, (__GLXWinDrawable *)gc->base.readPriv); + gc->hDC=NULL; + gc->hreadDC=NULL; gc->ctx = NULL; } + /* Clear the last active context in the drawable */ + if (drawPriv) drawPriv->drawContext = NULL; + + free(gc->Dispatch); free(gc); + _glapi_set_dispatch(NULL); } } @@ -1628,9 +1807,12 @@ glxWinCreateContext(__GLXscreen * screen, context->base.pGlxScreen = screen; // actual native GL context creation is deferred until attach() - context->ctx = NULL; + //context->ctx = NULL; already done with memset context->shareContext = shareContext; + context->Dispatch=calloc(sizeof(void*), (sizeof(struct _glapi_table) / sizeof(void *) + MAX_EXTENSION_FUNCS)); + _glapi_set_dispatch(context->Dispatch); + GLWIN_DEBUG_MSG("GLXcontext %p created", context); return &(context->base); @@ -1880,8 +2062,10 @@ glxWinCreateConfigs(HDC hdc, glxWinScreen * screen) break; } +#ifdef _DEBUG if (glxWinDebugSettings.dumpPFD) pfdOut(&pfd); +#endif if (!(pfd.dwFlags & (PFD_DRAW_TO_WINDOW | PFD_DRAW_TO_BITMAP)) || !(pfd.dwFlags & PFD_SUPPORT_OPENGL)) { @@ -2166,7 +2350,10 @@ glxWinCreateConfigsExt(HDC hdc, glxWinScreen * screen) /* fill in configs */ for (i = 0; i < numConfigs; i++) { - int values[num_attrs]; + int sizevalues=num_attrs*sizeof(int); + int *values=(int*)_alloca(sizevalues); + + memset(values,0,sizevalues); c = &(result[i]); c->base.next = NULL; |