Index: directx.c
===================================================================
RCS file: /home/wine/wine/dlls/wined3d/directx.c,v
retrieving revision 1.100
diff -u -r1.100 directx.c
--- directx.c	13 Jun 2006 11:00:30 -0000	1.100
+++ directx.c	15 Jun 2006 00:50:00 -0000
@@ -72,11 +72,17 @@
  * ie there is no GL Context - Get a default rendering context to enable the
  * function query some info from GL
  */
-static WineD3D_Context* WineD3D_CreateFakeGLContext(void) {
+static WineD3D_Context* WineD3D_CreateFakeGLContextOnNeed(void) {
     static WineD3D_Context ctx = { NULL, NULL, NULL, 0, 0 };
+
     WineD3D_Context* ret = NULL;
+    GLXContext gl_current_ctx = NULL;
 
-    if (glXGetCurrentContext() == NULL) {
+    ENTER_GL();
+    gl_current_ctx = glXGetCurrentContext();
+    LEAVE_GL();
+
+    if (NULL == gl_current_ctx) {
        BOOL         gotContext  = FALSE;
        BOOL         created     = FALSE;
        XVisualInfo  template;
@@ -139,22 +145,35 @@
        }
 
    } else {
-     if (ctx.ref > 0) ret = &ctx;
+      if (ctx.ref > 0) ret = &ctx;
+      else {
+        ret = &ctx;
+        /* refill infos as current context may be changed by application between calls */
+        ctx.ref = 0; 
+        ctx.glCtx = gl_current_ctx;
+        ENTER_GL();
+        ctx.display = glXGetCurrentDisplay();
+        /* others WineD3D_Context attributes don't need to be provided for local use */
+        ctx.visInfo = NULL;
+        ctx.drawable = (Drawable) NULL;
+      }
    }
 
    if (NULL != ret) InterlockedIncrement(&ret->ref);
    return ret;
 }
 
-static void WineD3D_ReleaseFakeGLContext(WineD3D_Context* ctx) {
+static void WineD3D_ReleaseFakeGLContextOnNeed(WineD3D_Context* ctx) {
     /* If we created a dummy context, throw it away */
     if (NULL != ctx) {
         if (0 == InterlockedDecrement(&ctx->ref)) {
+          if (NULL != ctx->visInfo) { /* NULL if fake ctx was filled using a valid current ctx */
             glXMakeCurrent(ctx->display, None, NULL);
             glXDestroyContext(ctx->display, ctx->glCtx);
-            ctx->display = NULL;
-            ctx->glCtx = NULL;
-            LEAVE_GL();
+          }
+          ctx->display = NULL;
+          ctx->glCtx = NULL;
+          LEAVE_GL();
         }
     }
 }
@@ -219,7 +238,7 @@
     /* Make sure that we've got a context */
     if (glXGetCurrentContext() == NULL) {
         /* TODO: CreateFakeGLContext should really take a display as a parameter  */
-        fake_ctx = WineD3D_CreateFakeGLContext();
+        fake_ctx = WineD3D_CreateFakeGLContextOnNeed();
         if (NULL != fake_ctx) gotContext = TRUE;
     } else {
         gotContext = TRUE;
@@ -758,7 +777,7 @@
 
 
     /* If we created a dummy context, throw it away */
-    if (NULL != fake_ctx) WineD3D_ReleaseFakeGLContext(fake_ctx);
+    WineD3D_ReleaseFakeGLContextOnNeed(fake_ctx);
 
     /* Only save the values obtained when a display is provided */
     if (fake_ctx == NULL) {
@@ -1032,11 +1051,11 @@
            reuse the values once we have a context which is valid. Values from
            a temporary context may differ from the final ones                 */
         if (isGLInfoValid == FALSE) {
-            WineD3D_Context *fake_ctx = NULL;
-            if (glXGetCurrentContext() == NULL) fake_ctx = WineD3D_CreateFakeGLContext();
+            WineD3D_Context* fake_ctx = NULL;
+            fake_ctx = WineD3D_CreateFakeGLContextOnNeed();
             /* If we don't know the device settings, go query them now */
             isGLInfoValid = IWineD3DImpl_FillGLCaps(&This->gl_info, IWineD3DImpl_GetAdapterDisplay(iface, Adapter));
-            if (fake_ctx != NULL) WineD3D_ReleaseFakeGLContext(fake_ctx);
+            WineD3D_ReleaseFakeGLContextOnNeed(fake_ctx);
         }
 
         /* If it worked, return the information requested */
@@ -1115,6 +1134,9 @@
   case WINED3DFMT_A1R5G5B5:
     if (5 == rb && 5 == gb && 5 == bb && 1 == ab) return TRUE;
     break;
+  case WINED3DFMT_A4R4G4B4:
+    if (4 == rb && 4 == gb && 4 == bb && 4 == ab) return TRUE;
+    break;
   case WINED3DFMT_X4R4G4B4:
     if (16 == buf_sz && 4 == rb && 4 == gb && 4 == bb) return TRUE;
     break;
@@ -1124,6 +1146,9 @@
   case WINED3DFMT_R3G3B2:
     if (3 == rb && 3 == gb && 2 == bb) return TRUE;
     break;
+  case WINED3DFMT_A8:
+    if (8 == ab) return TRUE;
+    break;
   case WINED3DFMT_A8P8:
     if (type & GLX_COLOR_INDEX_BIT && 8 == buf_sz && 8 == ab) return TRUE;
     break;
@@ -1143,11 +1168,14 @@
   case WINED3DFMT_A2R10G10B10:
   case WINED3DFMT_X1R5G5B5:
   case WINED3DFMT_A1R5G5B5:
+  case WINED3DFMT_A4R4G4B4:
+  case WINED3DFMT_X4R4G4B4:
   case WINED3DFMT_R5G6B5:
   case WINED3DFMT_R3G3B2:
+  case WINED3DFMT_A8:
   case WINED3DFMT_A8P8:
   case WINED3DFMT_P8:
-return TRUE;
+    return TRUE;
   default:
     ERR("unsupported format %s\n", debug_d3dformat(Format));
     break;
@@ -1226,7 +1254,7 @@
     int nCfgs = 0;
     int it;
 
-    WARN_(d3d_caps)("(%p)-> (STUB) (Adptr:%d, DevType:(%x,%s), AdptFmt:(%x,%s), RendrTgtFmt:(%x,%s), DepthStencilFmt:(%x,%s))\n",
+    TRACE_(d3d_caps)("(%p)-> (STUB) (Adptr:%d, DevType:(%x,%s), AdptFmt:(%x,%s), RendrTgtFmt:(%x,%s), DepthStencilFmt:(%x,%s)) ",
            This, Adapter,
            DeviceType, debug_d3ddevicetype(DeviceType),
            AdapterFormat, debug_d3dformat(AdapterFormat),
@@ -1234,12 +1262,12 @@
            DepthStencilFormat, debug_d3dformat(DepthStencilFormat));
 
     if (Adapter >= IWineD3D_GetAdapterCount(iface)) {
-        TRACE("(%p) Failed: Atapter (%u) higher than supported adapters (%u) returning WINED3DERR_INVALIDCALL\n", This, Adapter, IWineD3D_GetAdapterCount(iface));
+	TRACE_(d3d_caps)("[FAILED: Atapter (%u) higher than supported adapters (%u)]\n", Adapter, IWineD3D_GetAdapterCount(iface));
         return WINED3DERR_INVALIDCALL;
     }
     /* TODO: use the real context if it's available */
-    ctx = WineD3D_CreateFakeGLContext();
-    if(NULL !=  ctx) {
+    ctx = WineD3D_CreateFakeGLContextOnNeed();
+    if (NULL !=  ctx) {
         cfgs = glXGetFBConfigs(ctx->display, DefaultScreen(ctx->display), &nCfgs);
     } else {
         TRACE_(d3d_caps)("(%p) : Unable to create a fake context at this time (there may already be an active context)\n", This);
@@ -1260,14 +1288,13 @@
         /* If there's a corrent context then we cannot create a fake one so pass everything */
         hr = WINED3D_OK;
     }
+    WineD3D_ReleaseFakeGLContextOnNeed(ctx);
 
-    if (ctx != NULL)
-        WineD3D_ReleaseFakeGLContext(ctx);
-
-    if (hr != WINED3D_OK)
-        TRACE_(d3d_caps)("Failed to match stencil format to device\b");
-
-    TRACE_(d3d_caps)("(%p) : Returning %lx\n", This, hr);
+    if (hr != WINED3D_OK) { 
+        TRACE_(d3d_caps)("[FAILED]\n");
+    } else {
+        TRACE_(d3d_caps)("[OK]\n");
+    }
     return hr;
 }
 
@@ -1323,19 +1350,20 @@
       int nCfgs = 0;
       int it;
       HRESULT hr = WINED3DERR_NOTAVAILABLE;
+      WineD3D_Context* ctx = WineD3D_CreateFakeGLContextOnNeed();
 
-      WineD3D_Context* ctx = WineD3D_CreateFakeGLContext();
       if (NULL != ctx) {
-        cfgs = glXGetFBConfigs(ctx->display, DefaultScreen(ctx->display), &nCfgs);
-        for (it = 0; it < nCfgs; ++it) {
+	cfgs = glXGetFBConfigs(ctx->display, DefaultScreen(ctx->display), &nCfgs);
+	if (NULL != cfgs) {
+	  for (it = 0; it < nCfgs; ++it) {
             if (IWineD3DImpl_IsGLXFBConfigCompatibleWithRenderFmt(ctx, cfgs[it], DisplayFormat)) {
                 hr = WINED3D_OK;
                 break ;
             }
-        }
-        XFree(cfgs);
-
-        WineD3D_ReleaseFakeGLContext(ctx);
+          }
+	  XFree(cfgs);
+	}
+        WineD3D_ReleaseFakeGLContextOnNeed(ctx);
         return hr;
       }
     }
