util: move debug_memory_{begin,end} to os_memory_debug.h
[mesa.git] / src / gallium / state_trackers / wgl / stw_context.c
index 18273ac3e95e28c89e0d9a0431289dca12d51715..9003f43530a64712311c0694714e15a53dc8a39a 100644 (file)
@@ -132,6 +132,25 @@ DrvCreateLayerContext(HDC hdc, INT iLayerPlane)
 }
 
 
+/**
+ * Return the stw pixel format that most closely matches the pixel format
+ * on HDC.
+ * Used to get a pixel format when SetPixelFormat() hasn't been called before.
+ */
+static int
+get_matching_pixel_format(HDC hdc)
+{
+   int iPixelFormat = GetPixelFormat(hdc);
+   PIXELFORMATDESCRIPTOR pfd;
+
+   if (!iPixelFormat)
+      return 0;
+   if (!DescribePixelFormat(hdc, iPixelFormat, sizeof(pfd), &pfd))
+      return 0;
+   return stw_pixelformat_choose(hdc, &pfd);
+}
+
+
 /**
  * Called via DrvCreateContext(), DrvCreateLayerContext() and
  * wglCreateContextAttribsARB() to actually create a rendering context.
@@ -170,7 +189,13 @@ stw_create_context_attribs(HDC hdc, INT iLayerPlane, DHGLRC hShareContext,
       iPixelFormat = fb->iPixelFormat;
       stw_framebuffer_unlock(fb);
    } else {
-      return 0;
+      /* Applications should call SetPixelFormat before creating a context,
+       * but not all do, and the opengl32 runtime seems to use a default
+       * pixel format in some cases, so use that.
+       */
+      iPixelFormat = get_matching_pixel_format(hdc);
+      if (!iPixelFormat)
+         return 0;
    }
 
    pfi = stw_pixelformat_get_info( iPixelFormat );
@@ -186,7 +211,7 @@ stw_create_context_attribs(HDC hdc, INT iLayerPlane, DHGLRC hShareContext,
    if (ctx == NULL)
       goto no_ctx;
 
-   ctx->hdc = hdc;
+   ctx->hDrawDC = hdc;
    ctx->hReadDC = hdc;
    ctx->iPixelFormat = iPixelFormat;
    ctx->shared = shareCtx != NULL;
@@ -253,7 +278,7 @@ stw_create_context_attribs(HDC hdc, INT iLayerPlane, DHGLRC hShareContext,
    ctx->st->st_manager_private = (void *) ctx;
 
    if (ctx->st->cso_context) {
-      ctx->hud = hud_create(ctx->st->pipe, ctx->st->cso_context);
+      ctx->hud = hud_create(ctx->st->cso_context, NULL);
    }
 
    stw_lock_contexts(stw_dev);
@@ -266,7 +291,7 @@ stw_create_context_attribs(HDC hdc, INT iLayerPlane, DHGLRC hShareContext,
       if (old_ctx) {
          /* free the old context data associated with this handle */
          if (old_ctx->hud) {
-            hud_destroy(old_ctx->hud);
+            hud_destroy(old_ctx->hud, NULL);
          }
          ctx->st->destroy(old_ctx->st);
          FREE(old_ctx);
@@ -291,7 +316,7 @@ stw_create_context_attribs(HDC hdc, INT iLayerPlane, DHGLRC hShareContext,
 
 no_hglrc:
    if (ctx->hud) {
-      hud_destroy(ctx->hud);
+      hud_destroy(ctx->hud, NULL);
    }
    ctx->st->destroy(ctx->st);
 no_st_ctx:
@@ -323,7 +348,7 @@ DrvDeleteContext(DHGLRC dhglrc)
          stw_dev->stapi->make_current(stw_dev->stapi, NULL, NULL, NULL);
 
       if (ctx->hud) {
-         hud_destroy(ctx->hud);
+         hud_destroy(ctx->hud, NULL);
       }
 
       ctx->st->destroy(ctx->st);
@@ -387,7 +412,7 @@ stw_get_current_dc( void )
    if (!ctx)
       return NULL;
 
-   return ctx->hdc;
+   return ctx->hDrawDC;
 }
 
 HDC
@@ -403,7 +428,7 @@ stw_get_current_read_dc( void )
 }
 
 BOOL
-stw_make_current(HDC hdc, HDC hReadDC, DHGLRC dhglrc)
+stw_make_current(HDC hDrawDC, HDC hReadDC, DHGLRC dhglrc)
 {
    struct stw_context *old_ctx = NULL;
    struct stw_context *ctx = NULL;
@@ -415,7 +440,7 @@ stw_make_current(HDC hdc, HDC hReadDC, DHGLRC dhglrc)
    old_ctx = stw_current_context();
    if (old_ctx != NULL) {
       if (old_ctx->dhglrc == dhglrc) {
-         if (old_ctx->hdc == hdc && old_ctx->hReadDC == hReadDC) {
+         if (old_ctx->hDrawDC == hDrawDC && old_ctx->hReadDC == hReadDC) {
             /* Return if already current. */
             return TRUE;
          }
@@ -423,10 +448,11 @@ stw_make_current(HDC hdc, HDC hReadDC, DHGLRC dhglrc)
          if (old_ctx->shared) {
             struct pipe_fence_handle *fence = NULL;
             old_ctx->st->flush(old_ctx->st,
-                               ST_FLUSH_FRONT | ST_FLUSH_WAIT, &fence);
+                               ST_FLUSH_FRONT | ST_FLUSH_WAIT, &fence,
+                               NULL, NULL);
          }
          else {
-            old_ctx->st->flush(old_ctx->st, ST_FLUSH_FRONT, NULL);
+            old_ctx->st->flush(old_ctx->st, ST_FLUSH_FRONT, NULL, NULL, NULL);
          }
       }
    }
@@ -442,7 +468,7 @@ stw_make_current(HDC hdc, HDC hReadDC, DHGLRC dhglrc)
       }
 
       /* This call locks fb's mutex */
-      fb = stw_framebuffer_from_hdc( hdc );
+      fb = stw_framebuffer_from_hdc( hDrawDC );
       if (fb) {
          stw_framebuffer_update(fb);
       }
@@ -452,9 +478,9 @@ stw_make_current(HDC hdc, HDC hReadDC, DHGLRC dhglrc)
           * pixel format in some cases, so we must create a framebuffer for
           * those here.
           */
-         int iPixelFormat = GetPixelFormat(hdc);
+         int iPixelFormat = get_matching_pixel_format(hDrawDC);
          if (iPixelFormat)
-            fb = stw_framebuffer_create( hdc, iPixelFormat );
+            fb = stw_framebuffer_create( hDrawDC, iPixelFormat );
          if (!fb)
             goto fail;
       }
@@ -466,7 +492,7 @@ stw_make_current(HDC hdc, HDC hReadDC, DHGLRC dhglrc)
       }
 
       /* Bind the new framebuffer */
-      ctx->hdc = hdc;
+      ctx->hDrawDC = hDrawDC;
       ctx->hReadDC = hReadDC;
 
       struct stw_framebuffer *old_fb = ctx->current_framebuffer;