From 8ba32b3f9b40a9d2d813f09567b8d02f798a4a4f Mon Sep 17 00:00:00 2001
From: Alexandre Julliard <julliard@winehq.org>
Date: Wed, 29 Jun 2005 19:28:06 +0000
Subject: [PATCH] Delay OpenGL and GDI initialization until they are needed.

---
 dlls/x11drv/init.c        | 11 ++++++++---
 dlls/x11drv/opengl.c      | 26 ++++++++++++++++----------
 dlls/x11drv/x11drv.h      |  2 --
 dlls/x11drv/x11drv_main.c |  7 +------
 4 files changed, 25 insertions(+), 21 deletions(-)

diff --git a/dlls/x11drv/init.c b/dlls/x11drv/init.c
index d3f7eee5e74..fdadc41e2f4 100644
--- a/dlls/x11drv/init.c
+++ b/dlls/x11drv/init.c
@@ -41,6 +41,7 @@ static int log_pixels_y;  /* pixels per logical inch in y direction */
 static int horz_size;     /* horz. size of screen in millimeters */
 static int vert_size;     /* vert. size of screen in millimeters */
 static int palette_size;
+static int device_init_done;
 unsigned int text_caps = (TC_OP_CHARACTER | TC_OP_STROKE | TC_CP_STROKE |
                           TC_CR_ANY | TC_SA_DOUBLE | TC_SA_INTEGER |
                           TC_SA_CONTIN | TC_UA_ABLE | TC_SO_ABLE | TC_RA_ABLE);
@@ -77,11 +78,13 @@ static DWORD get_dpi( void )
 }
 
 /**********************************************************************
- *	     X11DRV_GDI_Initialize
+ *	     device_init
+ *
+ * Perform initializations needed upon creation of the first device.
  */
-void X11DRV_GDI_Initialize( Display *display )
+static void device_init(void)
 {
-    gdi_display = display;
+    device_init_done = TRUE;
 
     /* Initialize XRender */
     X11DRV_XRender_Init();
@@ -117,6 +120,8 @@ BOOL X11DRV_CreateDC( HDC hdc, X11DRV_PDEVICE **pdev, LPCWSTR driver, LPCWSTR de
 {
     X11DRV_PDEVICE *physDev;
 
+    if (!device_init_done) device_init();
+
     physDev = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*physDev) );
     if (!physDev) return FALSE;
 
diff --git a/dlls/x11drv/opengl.c b/dlls/x11drv/opengl.c
index 511e8347ce3..a0cb522c9eb 100644
--- a/dlls/x11drv/opengl.c
+++ b/dlls/x11drv/opengl.c
@@ -112,8 +112,6 @@ static void dump_PIXELFORMATDESCRIPTOR(const PIXELFORMATDESCRIPTOR *ppfd) {
 #define SONAME_LIBGL "libGL.so"
 #endif
 
-static void *opengl_handle;
-
 #define MAKE_FUNCPTR(f) static typeof(f) * p##f;
 MAKE_FUNCPTR(glGetError)
 MAKE_FUNCPTR(glXChooseVisual)
@@ -126,11 +124,18 @@ MAKE_FUNCPTR(glXChooseFBConfig)
 MAKE_FUNCPTR(glXGetFBConfigAttrib)
 #undef MAKE_FUNCPTR
 
-void X11DRV_OpenGL_Init(Display *display) {
+static BOOL has_opengl(void)
+{
+    static int init_done;
+    static void *opengl_handle;
+
     int error_base, event_base;
 
+    if (init_done) return (opengl_handle != NULL);
+    init_done = 1;
+
     opengl_handle = wine_dlopen(SONAME_LIBGL, RTLD_NOW|RTLD_GLOBAL, NULL, 0);
-    if (opengl_handle == NULL) return;
+    if (opengl_handle == NULL) return FALSE;
 
 #define LOAD_FUNCPTR(f) if((p##f = wine_dlsym(opengl_handle, #f, NULL, 0)) == NULL) goto sym_not_found;
 LOAD_FUNCPTR(glGetError)
@@ -145,18 +150,19 @@ LOAD_FUNCPTR(glXGetFBConfigAttrib)
 #undef LOAD_FUNCPTR
 
     wine_tsx11_lock();
-    if (pglXQueryExtension(display, &event_base, &error_base) == True) {
+    if (pglXQueryExtension(gdi_display, &event_base, &error_base) == True) {
 	TRACE("GLX is up and running error_base = %d\n", error_base);
     } else {
         wine_dlclose(opengl_handle, NULL, 0);
 	opengl_handle = NULL;
     }
     wine_tsx11_unlock();
-    return;
+    return (opengl_handle != NULL);
 
 sym_not_found:
     wine_dlclose(opengl_handle, NULL, 0);
     opengl_handle = NULL;
+    return FALSE;
 }
 
 #define TEST_AND_ADD1(t,a) if (t) att_list[att_pos++] = (a)
@@ -176,7 +182,7 @@ int X11DRV_ChoosePixelFormat(X11DRV_PDEVICE *physDev,
   GLXFBConfig* cfgs = NULL;
   int ret = 0;
 
-  if (opengl_handle == NULL) {
+  if (!has_opengl()) {
     ERR("No libGL on this box - disabling OpenGL support !\n");
     return 0;
   }
@@ -298,7 +304,7 @@ int X11DRV_DescribePixelFormat(X11DRV_PDEVICE *physDev,
   int nCfgs = 0;
   int ret = 0;
 
-  if (opengl_handle == NULL) {
+  if (!has_opengl()) {
     ERR("No libGL on this box - disabling OpenGL support !\n");
     return 0;
   }
@@ -444,7 +450,7 @@ BOOL X11DRV_SetPixelFormat(X11DRV_PDEVICE *physDev,
  * Swap the buffers of this DC
  */
 BOOL X11DRV_SwapBuffers(X11DRV_PDEVICE *physDev) {
-  if (opengl_handle == NULL) {
+  if (!has_opengl()) {
     ERR("No libGL on this box - disabling OpenGL support !\n");
     return 0;
   }
@@ -470,7 +476,7 @@ XVisualInfo *X11DRV_setup_opengl_visual( Display *display )
     XVisualInfo *visual = NULL;
     int dblBuf[]={GLX_RGBA,GLX_DEPTH_SIZE,16,GLX_DOUBLEBUFFER,None};
 
-    if (opengl_handle == NULL) return NULL;
+    if (!has_opengl()) return NULL;
     
     /* In order to support OpenGL or D3D, we require a double-buffered visual */
     wine_tsx11_lock();
diff --git a/dlls/x11drv/x11drv.h b/dlls/x11drv/x11drv.h
index 154d3406999..8b9413bcd10 100644
--- a/dlls/x11drv/x11drv.h
+++ b/dlls/x11drv/x11drv.h
@@ -274,7 +274,6 @@ extern BOOL X11DRV_XRender_ExtTextOut(X11DRV_PDEVICE *physDev, INT x, INT y, UIN
 				      UINT count, const INT *lpDx, INT breakExtra);
 extern void X11DRV_XRender_UpdateDrawable(X11DRV_PDEVICE *physDev);
 
-extern void X11DRV_OpenGL_Init(Display *display);
 extern XVisualInfo *X11DRV_setup_opengl_visual(Display *display);
 
 /* XIM support */
@@ -441,7 +440,6 @@ extern INT X11DRV_DCICommand(INT cbInput, const struct _DCICMD *lpCmd, LPVOID lp
  * X11 GDI driver
  */
 
-extern void X11DRV_GDI_Initialize( Display *display );
 extern void X11DRV_GDI_Finalize(void);
 
 extern Display *gdi_display;  /* display to use for all GDI functions */
diff --git a/dlls/x11drv/x11drv_main.c b/dlls/x11drv/x11drv_main.c
index dce266ccc0d..888b8c91a24 100644
--- a/dlls/x11drv/x11drv_main.c
+++ b/dlls/x11drv/x11drv_main.c
@@ -336,6 +336,7 @@ static BOOL process_attach(void)
     screen = DefaultScreenOfDisplay( display );
     visual = DefaultVisual( display, DefaultScreen(display) );
     root_window = DefaultRootWindow( display );
+    gdi_display = display;
     old_error_handler = XSetErrorHandler( error_handler );
 
     /* Initialize screen depth */
@@ -355,9 +356,6 @@ static BOOL process_attach(void)
     }
     if (!screen_depth) screen_depth = DefaultDepthOfScreen( screen );
 
-    /* Initialize OpenGL */
-    X11DRV_OpenGL_Init(display);
-
     /* If OpenGL is available, change the default visual, etc as necessary */
     if (desktop_dbl_buf && (desktop_vi = X11DRV_setup_opengl_visual( display )))
     {
@@ -379,9 +377,6 @@ static BOOL process_attach(void)
         using_wine_desktop = 1;
     }
 
-    /* initialize GDI */
-    X11DRV_GDI_Initialize( display );
-
 #ifdef HAVE_LIBXXF86VM
     /* initialize XVidMode */
     X11DRV_XF86VM_Init();
-- 
GitLab