1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
4 /* This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 //////////////////////////////////////////////////////////////////////////////
10 // Explanation: See bug 639842. Safely getting GL driver info on X11 is hard, because the only way to do
11 // that is to create a GL context and call glGetString(), but with bad drivers,
12 // just creating a GL context may crash.
14 // This file implements the idea to do that in a separate process.
16 // The only non-static function here is fire_glxtest_process(). It creates a pipe, publishes its 'read' end as the
17 // mozilla::widget::glxtest_pipe global variable, forks, and runs that GLX probe in the child process,
18 // which runs the glxtest() static function. This creates a X connection, a GLX context, calls glGetString, and writes that
19 // to the 'write' end of the pipe.
29 #include <vcl/opengl/glxtest.hxx>
36 #include "X11/Xutil.h"
39 typedef struct __GLXcontextRec
*GLXContext
;
40 typedef XID GLXPixmap
;
41 typedef XID GLXDrawable
;
42 /* GLX 1.3 and later */
43 typedef struct __GLXFBConfigRec
*GLXFBConfig
;
44 typedef XID GLXFBConfigID
;
45 typedef XID GLXContextID
;
46 typedef XID GLXWindow
;
47 typedef XID GLXPbuffer
;
49 #define GLX_RED_SIZE 8
50 #define GLX_GREEN_SIZE 9
51 #define GLX_BLUE_SIZE 10
54 typedef uint8_t GLubyte
;
55 typedef uint32_t GLenum
;
56 #define GL_VENDOR 0x1F00
57 #define GL_RENDERER 0x1F01
58 #define GL_VERSION 0x1F02
60 // the write end of the pipe, which we're going to write to
61 static int write_end_of_the_pipe
= -1;
63 // C++ standard collides with C standard in that it doesn't allow casting void* to function pointer types.
64 // So the work-around is to convert first to size_t.
65 // http://www.trilithium.com/johan/2004/12/problem-with-dlsym/
66 template<typename func_ptr_type
>
67 static func_ptr_type
cast(void *ptr
)
69 return reinterpret_cast<func_ptr_type
>(
70 reinterpret_cast<size_t>(ptr
)
74 static void fatal_error(const char *str
)
76 int length
= strlen(str
);
77 if (write(write_end_of_the_pipe
, str
, length
) != length
78 || write(write_end_of_the_pipe
, "\n", 1) != 1)
80 /* Cannot write to pipe. Fall through to call _exit */
86 x_error_handler(Display
*, XErrorEvent
*ev
)
88 enum { bufsize
= 1024 };
90 int length
= snprintf(buf
, bufsize
,
91 "X error occurred in GLX probe, error_code=%d, request_code=%d, minor_code=%d\n",
95 if (write(write_end_of_the_pipe
, buf
, length
) != length
)
97 /* Cannot write to pipe. Fall through to call _exit */
105 // we want to redirect to /dev/null stdout, stderr, and while we're at it,
106 // any PR logging file descriptors. To that effect, we redirect all positive
107 // file descriptors up to what open() returns here. In particular, 1 is stdout and 2 is stderr.
108 int fd
= open("/dev/null", O_WRONLY
);
110 fatal_error("could not redirect stdout+stderr");
111 for (int i
= 1; i
< fd
; i
++)
115 ///// Open libGL and load needed symbols /////
117 #define LIBGL_FILENAME "libGL.so"
119 #define LIBGL_FILENAME "libGL.so.1"
121 void *libgl
= dlopen(LIBGL_FILENAME
, RTLD_LAZY
);
123 fatal_error("Unable to load " LIBGL_FILENAME
);
125 typedef void* (* PFNGLXGETPROCADDRESS
) (const char *);
126 PFNGLXGETPROCADDRESS glXGetProcAddress
= cast
<PFNGLXGETPROCADDRESS
>(dlsym(libgl
, "glXGetProcAddress"));
128 if (!glXGetProcAddress
)
129 fatal_error("Unable to find glXGetProcAddress in " LIBGL_FILENAME
);
131 typedef GLXFBConfig
* (* PFNGLXQUERYEXTENSION
) (Display
*, int *, int *);
132 PFNGLXQUERYEXTENSION glXQueryExtension
= cast
<PFNGLXQUERYEXTENSION
>(glXGetProcAddress("glXQueryExtension"));
134 typedef GLXFBConfig
* (* PFNGLXQUERYVERSION
) (Display
*, int *, int *);
135 PFNGLXQUERYVERSION glXQueryVersion
= cast
<PFNGLXQUERYVERSION
>(dlsym(libgl
, "glXQueryVersion"));
137 typedef XVisualInfo
* (* PFNGLXCHOOSEVISUAL
) (Display
*, int, int *);
138 PFNGLXCHOOSEVISUAL glXChooseVisual
= cast
<PFNGLXCHOOSEVISUAL
>(glXGetProcAddress("glXChooseVisual"));
140 typedef GLXContext (* PFNGLXCREATECONTEXT
) (Display
*, XVisualInfo
*, GLXContext
, Bool
);
141 PFNGLXCREATECONTEXT glXCreateContext
= cast
<PFNGLXCREATECONTEXT
>(glXGetProcAddress("glXCreateContext"));
143 typedef Bool (* PFNGLXMAKECURRENT
) (Display
*, GLXDrawable
, GLXContext
);
144 PFNGLXMAKECURRENT glXMakeCurrent
= cast
<PFNGLXMAKECURRENT
>(glXGetProcAddress("glXMakeCurrent"));
146 typedef void (* PFNGLXDESTROYCONTEXT
) (Display
*, GLXContext
);
147 PFNGLXDESTROYCONTEXT glXDestroyContext
= cast
<PFNGLXDESTROYCONTEXT
>(glXGetProcAddress("glXDestroyContext"));
149 typedef GLubyte
* (* PFNGLGETSTRING
) (GLenum
);
150 PFNGLGETSTRING glGetString
= cast
<PFNGLGETSTRING
>(glXGetProcAddress("glGetString"));
152 if (!glXQueryExtension
||
157 !glXDestroyContext
||
160 fatal_error("glXGetProcAddress couldn't find required functions");
162 ///// Open a connection to the X server /////
163 Display
*dpy
= XOpenDisplay(nullptr);
165 fatal_error("Unable to open a connection to the X server");
167 ///// Check that the GLX extension is present /////
168 if (!glXQueryExtension(dpy
, nullptr, nullptr))
169 fatal_error("GLX extension missing");
171 XSetErrorHandler(x_error_handler
);
173 ///// Get a visual /////
180 XVisualInfo
*vInfo
= glXChooseVisual(dpy
, DefaultScreen(dpy
), attribs
);
182 fatal_error("No visuals found");
184 // using a X11 Window instead of a GLXPixmap does not crash
185 // fglrx in indirect rendering. bug 680644
187 XSetWindowAttributes swa
;
188 swa
.colormap
= XCreateColormap(dpy
, RootWindow(dpy
, vInfo
->screen
),
189 vInfo
->visual
, AllocNone
);
191 swa
.border_pixel
= 0;
192 window
= XCreateWindow(dpy
, RootWindow(dpy
, vInfo
->screen
),
194 0, vInfo
->depth
, InputOutput
, vInfo
->visual
,
195 CWBorderPixel
| CWColormap
, &swa
);
197 ///// Get a GL context and make it current //////
198 GLXContext context
= glXCreateContext(dpy
, vInfo
, nullptr, True
);
199 glXMakeCurrent(dpy
, window
, context
);
201 ///// Look for this symbol to determine texture_from_pixmap support /////
202 void* glXBindTexImageEXT
= glXGetProcAddress("glXBindTexImageEXT");
204 ///// Get GL vendor/renderer/versions strings /////
205 enum { bufsize
= 1024 };
207 const GLubyte
*vendorString
= glGetString(GL_VENDOR
);
208 const GLubyte
*rendererString
= glGetString(GL_RENDERER
);
209 const GLubyte
*versionString
= glGetString(GL_VERSION
);
211 if (!vendorString
|| !rendererString
|| !versionString
)
212 fatal_error("glGetString returned null");
214 int length
= snprintf(buf
, bufsize
,
215 "VENDOR\n%s\nRENDERER\n%s\nVERSION\n%s\nTFP\n%s\n",
219 glXBindTexImageEXT
? "TRUE" : "FALSE");
220 if (length
>= bufsize
)
221 fatal_error("GL strings length too large for buffer size");
223 ///// Clean up. Indeed, the parent process might fail to kill us (e.g. if it doesn't need to check GL info)
224 ///// so we might be staying alive for longer than expected, so it's important to consume as little memory as
225 ///// possible. Also we want to check that we're able to do that too without generating X errors.
226 glXMakeCurrent(dpy
, None
, nullptr); // must release the GL context before destroying it
227 glXDestroyContext(dpy
, context
);
228 XDestroyWindow(dpy
, window
);
229 XFreeColormap(dpy
, swa
.colormap
);
232 #ifdef NS_FREE_PERMANENT_DATA // conditionally defined in nscore.h, don't forget to #include it above
235 // This XSync call wanted to be instead:
236 // XCloseDisplay(dpy);
237 // but this can cause 1-minute stalls on certain setups using Nouveau, see bug 973192
243 ///// Finally write data to the pipe
244 if (write(write_end_of_the_pipe
, buf
, length
) != length
)
245 fatal_error("Could not write to pipe");
248 /** \returns true in the child glxtest process, false in the parent process */
249 bool fire_glxtest_process()
252 if (pipe(pfd
) == -1) {
263 // The child exits early to avoid running the full shutdown sequence and avoid conflicting with threads
264 // we have already spawned (like the profiler).
267 write_end_of_the_pipe
= pfd
[1];
274 int* glxtest_pipe
= getGlxPipe();
275 *glxtest_pipe
= pfd
[0];
276 pid_t
* glxtest_pid
= getGlxPid();