From 5b932b08097a397f69a1f03c3f213035499ffe6e Mon Sep 17 00:00:00 2001
From: Ken Thomases <ken@codeweavers.com>
Date: Wed, 12 Mar 2014 08:39:00 -0500
Subject: user32: Track the number of GL surfaces for a window rather than the
 pixel format.
To: wine-patches <wine-patches@winehq.org>

The specific pixel format was never used.  It was effectively just a flag
indicating if the window had a GL surface.  This generalizes it from a flag
to a count, to allow for multiple GL surfaces.  This supports the
WGL_WINE_surface extension.
---
 dlls/opengl32/make_opengl |    2 +-
 dlls/user32/user32.spec   |    2 +-
 dlls/user32/win.c         |    8 ++++----
 dlls/user32/win.h         |    2 +-
 dlls/user32/winpos.c      |    2 +-
 dlls/winemac.drv/opengl.c |    8 +++++---
 dlls/winex11.drv/opengl.c |    8 ++++++--
 7 files changed, 19 insertions(+), 13 deletions(-)

diff --git a/dlls/opengl32/make_opengl b/dlls/opengl32/make_opengl
index ed0a92c..0011a81 100755
--- a/dlls/opengl32/make_opengl
+++ b/dlls/opengl32/make_opengl
@@ -789,7 +789,7 @@ foreach (sort keys %norm_functions)
 print HEADER "\n\n";
 
 print HEADER "extern struct opengl_funcs * CDECL __wine_get_wgl_driver( HDC hdc, UINT version );\n";
-print HEADER "extern BOOL CDECL __wine_set_pixel_format( HWND hwnd, int format );\n\n";
+print HEADER "extern BOOL CDECL __wine_track_gl_surfaces( HWND hwnd, int change );\n\n";
 print HEADER "#endif /* __WINE_WGL_DRIVER_H */\n";
 close HEADER;
 
diff --git a/dlls/user32/user32.spec b/dlls/user32/user32.spec
index fe3906f..5e6c85e 100644
--- a/dlls/user32/user32.spec
+++ b/dlls/user32/user32.spec
@@ -782,4 +782,4 @@
 # or 'wine_' (for user-visible functions) to avoid namespace conflicts.
 #
 @ cdecl __wine_send_input(long ptr)
-@ cdecl __wine_set_pixel_format(long long)
+@ cdecl __wine_track_gl_surfaces(long long)
diff --git a/dlls/user32/win.c b/dlls/user32/win.c
index 9e30321..d54f060 100644
--- a/dlls/user32/win.c
+++ b/dlls/user32/win.c
@@ -3619,18 +3619,18 @@ BOOL WINAPI SwitchDesktop( HDESK hDesktop)
 
 
 /***********************************************************************
- *           __wine_set_pixel_format
+ *           __wine_track_gl_surfaces
  */
-BOOL CDECL __wine_set_pixel_format( HWND hwnd, int format )
+BOOL CDECL __wine_track_gl_surfaces( HWND hwnd, int change )
 {
     WND *win = WIN_GetPtr( hwnd );
 
     if (!win || win == WND_DESKTOP || win == WND_OTHER_PROCESS)
     {
-        WARN( "setting format %d on win %p not supported\n", format, hwnd );
+        WARN( "tracking GL surfaces on win %p not supported\n", hwnd );
         return FALSE;
     }
-    win->pixel_format = format;
+    win->gl_surfaces += change;
     WIN_ReleasePtr( win );
 
     update_window_state( hwnd );
diff --git a/dlls/user32/win.h b/dlls/user32/win.h
index a19ebe9..06c0acb 100644
--- a/dlls/user32/win.h
+++ b/dlls/user32/win.h
@@ -62,7 +62,7 @@ typedef struct tagWND
     HICON          hIconSmall;    /* window's small icon */
     struct window_surface *surface; /* Window surface if any */
     struct tagDIALOGINFO *dlgInfo;/* Dialog additional info (dialogs only) */
-    int            pixel_format;  /* Pixel format set by the graphics driver */
+    int            gl_surfaces;   /* Count of gl_surfaces created by the graphics driver */
     int            cbWndExtra;    /* class cbWndExtra at window creation */
     DWORD_PTR      userdata;      /* User private data */
     DWORD          wExtra[1];     /* Window extra bytes */
diff --git a/dlls/user32/winpos.c b/dlls/user32/winpos.c
index 57fefe3..20b51d0 100644
--- a/dlls/user32/winpos.c
+++ b/dlls/user32/winpos.c
@@ -2082,7 +2082,7 @@ BOOL set_window_pos( HWND hwnd, HWND insert_after, UINT swp_flags,
                 wine_server_add_data( req, valid_rects, 2 * sizeof(*valid_rects) );
         }
         if (new_surface) req->paint_flags |= SET_WINPOS_PAINT_SURFACE;
-        if (win->pixel_format) req->paint_flags |= SET_WINPOS_PIXEL_FORMAT;
+        if (win->gl_surfaces) req->paint_flags |= SET_WINPOS_PIXEL_FORMAT;
 
         if ((ret = !wine_server_call( req )))
         {
diff --git a/dlls/winemac.drv/opengl.c b/dlls/winemac.drv/opengl.c
index 0187822..4255de7 100644
--- a/dlls/winemac.drv/opengl.c
+++ b/dlls/winemac.drv/opengl.c
@@ -1350,7 +1350,7 @@ static BOOL set_pixel_format(HDC hdc, int fmt, BOOL allow_reset)
     struct macdrv_win_data *data;
     const pixel_format *pf;
     HWND hwnd = WindowFromDC(hdc);
-    BOOL ret = FALSE;
+    BOOL ret = FALSE, had_format = FALSE;
 
     TRACE("hdc %p format %d\n", hdc, fmt);
 
@@ -1386,6 +1386,7 @@ static BOOL set_pixel_format(HDC hdc, int fmt, BOOL allow_reset)
         goto done;
     }
 
+    had_format = data->pixel_format != 0;
     if (!set_win_format(data, fmt))
     {
         WARN("Couldn't set format of the window, returning failure\n");
@@ -1411,7 +1412,8 @@ static BOOL set_pixel_format(HDC hdc, int fmt, BOOL allow_reset)
 
 done:
     release_win_data(data);
-    if (ret) __wine_set_pixel_format(hwnd, fmt);
+    if (ret && !had_format)
+        __wine_track_gl_surfaces(hwnd, 1);
     return ret;
 }
 
@@ -1437,7 +1439,7 @@ void set_gl_view_parent(HWND hwnd, HWND parent)
             macdrv_dispose_view(data->gl_view);
             data->gl_view = NULL;
             release_win_data(data);
-            __wine_set_pixel_format( hwnd, 0 );
+            __wine_track_gl_surfaces(hwnd, -1);
             return;
         }
 
diff --git a/dlls/winex11.drv/opengl.c b/dlls/winex11.drv/opengl.c
index b3bd45b..3588c2c 100644
--- a/dlls/winex11.drv/opengl.c
+++ b/dlls/winex11.drv/opengl.c
@@ -1324,6 +1324,7 @@ static BOOL create_gl_drawable( HWND hwnd, struct gl_drawable *gl )
 static BOOL set_win_format( HWND hwnd, const struct wgl_pixel_format *format )
 {
     struct gl_drawable *gl, *prev;
+    BOOL had_format = FALSE;
 
     gl = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*gl) );
     /* Default GLX and WGL swap interval is 1, but in case of glXSwapIntervalSGI
@@ -1355,11 +1356,14 @@ static BOOL set_win_format( HWND hwnd, const struct wgl_pixel_format *format )
 
     EnterCriticalSection( &context_section );
     if (!XFindContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char **)&prev ))
+    {
+        had_format = TRUE;
         free_gl_drawable( prev );
+    }
     XSaveContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char *)gl );
     LeaveCriticalSection( &context_section );
 
-    __wine_set_pixel_format( hwnd, pixel_format_index( format ));
+    if (!had_format) __wine_track_gl_surfaces( hwnd, 1 );
     return TRUE;
 }
 
@@ -1455,7 +1459,7 @@ void set_gl_drawable_parent( HWND hwnd, HWND parent )
         release_gl_drawable( gl );
         XFree( gl->visual );
         HeapFree( GetProcessHeap(), 0, gl );
-        __wine_set_pixel_format( hwnd, 0 );
+        __wine_track_gl_surfaces( hwnd, -1 );
         return;
     }
     mark_drawable_dirty( old_drawable, gl->drawable );
-- 
1.7.7.4

