2011-05-03 20:07:17 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
* vim: sw=2 ts=8 et :
|
|
|
|
*/
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-05-03 20:07:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Explanation: See bug 639842. Safely getting GL driver info on X11 is hard, because the only way to do
|
|
|
|
// that is to create a GL context and call glGetString(), but with bad drivers,
|
|
|
|
// just creating a GL context may crash.
|
|
|
|
//
|
|
|
|
// This file implements the idea to do that in a separate process.
|
|
|
|
//
|
|
|
|
// The only non-static function here is fire_glxtest_process(). It creates a pipe, publishes its 'read' end as the
|
|
|
|
// mozilla::widget::glxtest_pipe global variable, forks, and runs that GLX probe in the child process,
|
|
|
|
// which runs the glxtest() static function. This creates a X connection, a GLX context, calls glGetString, and writes that
|
|
|
|
// to the 'write' end of the pipe.
|
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#include "nscore.h"
|
2012-02-09 02:55:34 +00:00
|
|
|
#include <fcntl.h>
|
2013-11-18 13:08:24 +00:00
|
|
|
#include "stdint.h"
|
2012-02-09 02:55:34 +00:00
|
|
|
|
2011-08-30 05:27:19 +00:00
|
|
|
#ifdef __SUNPRO_CC
|
|
|
|
#include <stdio.h>
|
|
|
|
#endif
|
|
|
|
|
2013-11-18 13:08:24 +00:00
|
|
|
#include "X11/Xlib.h"
|
|
|
|
#include "X11/Xutil.h"
|
|
|
|
|
2015-01-25 21:50:13 +00:00
|
|
|
#include "mozilla/unused.h"
|
|
|
|
|
2013-11-18 13:08:24 +00:00
|
|
|
// stuff from glx.h
|
|
|
|
typedef struct __GLXcontextRec *GLXContext;
|
|
|
|
typedef XID GLXPixmap;
|
|
|
|
typedef XID GLXDrawable;
|
|
|
|
/* GLX 1.3 and later */
|
|
|
|
typedef struct __GLXFBConfigRec *GLXFBConfig;
|
|
|
|
typedef XID GLXFBConfigID;
|
|
|
|
typedef XID GLXContextID;
|
|
|
|
typedef XID GLXWindow;
|
|
|
|
typedef XID GLXPbuffer;
|
|
|
|
#define GLX_RGBA 4
|
|
|
|
#define GLX_RED_SIZE 8
|
|
|
|
#define GLX_GREEN_SIZE 9
|
|
|
|
#define GLX_BLUE_SIZE 10
|
|
|
|
|
|
|
|
// stuff from gl.h
|
|
|
|
typedef uint8_t GLubyte;
|
|
|
|
typedef uint32_t GLenum;
|
|
|
|
#define GL_VENDOR 0x1F00
|
|
|
|
#define GL_RENDERER 0x1F01
|
|
|
|
#define GL_VERSION 0x1F02
|
|
|
|
|
2011-05-03 20:07:17 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace widget {
|
|
|
|
// the read end of the pipe, which will be used by GfxInfo
|
|
|
|
extern int glxtest_pipe;
|
|
|
|
// the PID of the glxtest process, to pass to waitpid()
|
|
|
|
extern pid_t glxtest_pid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// the write end of the pipe, which we're going to write to
|
|
|
|
static int write_end_of_the_pipe = -1;
|
|
|
|
|
|
|
|
// C++ standard collides with C standard in that it doesn't allow casting void* to function pointer types.
|
|
|
|
// So the work-around is to convert first to size_t.
|
|
|
|
// http://www.trilithium.com/johan/2004/12/problem-with-dlsym/
|
|
|
|
template<typename func_ptr_type>
|
|
|
|
static func_ptr_type cast(void *ptr)
|
|
|
|
{
|
|
|
|
return reinterpret_cast<func_ptr_type>(
|
|
|
|
reinterpret_cast<size_t>(ptr)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fatal_error(const char *str)
|
|
|
|
{
|
2015-11-02 05:53:26 +00:00
|
|
|
mozilla::Unused << write(write_end_of_the_pipe, str, strlen(str));
|
|
|
|
mozilla::Unused << write(write_end_of_the_pipe, "\n", 1);
|
2014-03-28 20:06:18 +00:00
|
|
|
_exit(EXIT_FAILURE);
|
2011-05-03 20:07:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
x_error_handler(Display *, XErrorEvent *ev)
|
|
|
|
{
|
|
|
|
enum { bufsize = 1024 };
|
|
|
|
char buf[bufsize];
|
|
|
|
int length = snprintf(buf, bufsize,
|
|
|
|
"X error occurred in GLX probe, error_code=%d, request_code=%d, minor_code=%d\n",
|
|
|
|
ev->error_code,
|
|
|
|
ev->request_code,
|
|
|
|
ev->minor_code);
|
2015-11-02 05:53:26 +00:00
|
|
|
mozilla::Unused << write(write_end_of_the_pipe, buf, length);
|
2014-03-28 20:06:18 +00:00
|
|
|
_exit(EXIT_FAILURE);
|
2011-05-03 20:07:17 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-28 20:06:18 +00:00
|
|
|
|
|
|
|
// glxtest is declared inside extern "C" so that the name is not mangled.
|
|
|
|
// The name is used in build/valgrind/x86_64-redhat-linux-gnu.sup to suppress
|
|
|
|
// memory leak errors because we run it inside a short lived fork and we don't
|
|
|
|
// care about leaking memory
|
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
void glxtest()
|
2011-05-03 20:07:17 +00:00
|
|
|
{
|
2012-02-09 02:55:34 +00:00
|
|
|
// we want to redirect to /dev/null stdout, stderr, and while we're at it,
|
|
|
|
// any PR logging file descriptors. To that effect, we redirect all positive
|
|
|
|
// file descriptors up to what open() returns here. In particular, 1 is stdout and 2 is stderr.
|
|
|
|
int fd = open("/dev/null", O_WRONLY);
|
|
|
|
for (int i = 1; i < fd; i++)
|
|
|
|
dup2(fd, i);
|
|
|
|
close(fd);
|
|
|
|
|
2012-09-21 17:44:35 +00:00
|
|
|
if (getenv("MOZ_AVOID_OPENGL_ALTOGETHER"))
|
|
|
|
fatal_error("The MOZ_AVOID_OPENGL_ALTOGETHER environment variable is defined");
|
|
|
|
|
2011-05-03 20:07:17 +00:00
|
|
|
///// Open libGL and load needed symbols /////
|
2011-10-25 12:48:39 +00:00
|
|
|
#ifdef __OpenBSD__
|
2012-02-09 02:55:34 +00:00
|
|
|
#define LIBGL_FILENAME "libGL.so"
|
2011-10-25 12:48:39 +00:00
|
|
|
#else
|
2012-02-09 02:55:34 +00:00
|
|
|
#define LIBGL_FILENAME "libGL.so.1"
|
2011-10-25 12:48:39 +00:00
|
|
|
#endif
|
2012-02-09 02:55:34 +00:00
|
|
|
void *libgl = dlopen(LIBGL_FILENAME, RTLD_LAZY);
|
2011-05-03 20:07:17 +00:00
|
|
|
if (!libgl)
|
2012-02-09 02:55:34 +00:00
|
|
|
fatal_error("Unable to load " LIBGL_FILENAME);
|
2011-09-07 21:17:45 +00:00
|
|
|
|
|
|
|
typedef void* (* PFNGLXGETPROCADDRESS) (const char *);
|
|
|
|
PFNGLXGETPROCADDRESS glXGetProcAddress = cast<PFNGLXGETPROCADDRESS>(dlsym(libgl, "glXGetProcAddress"));
|
|
|
|
|
|
|
|
if (!glXGetProcAddress)
|
2012-02-09 02:55:34 +00:00
|
|
|
fatal_error("Unable to find glXGetProcAddress in " LIBGL_FILENAME);
|
2011-05-03 20:07:17 +00:00
|
|
|
|
|
|
|
typedef GLXFBConfig* (* PFNGLXQUERYEXTENSION) (Display *, int *, int *);
|
2011-09-07 21:17:45 +00:00
|
|
|
PFNGLXQUERYEXTENSION glXQueryExtension = cast<PFNGLXQUERYEXTENSION>(glXGetProcAddress("glXQueryExtension"));
|
2011-05-03 20:07:17 +00:00
|
|
|
|
2011-08-19 15:39:00 +00:00
|
|
|
typedef GLXFBConfig* (* PFNGLXQUERYVERSION) (Display *, int *, int *);
|
|
|
|
PFNGLXQUERYVERSION glXQueryVersion = cast<PFNGLXQUERYVERSION>(dlsym(libgl, "glXQueryVersion"));
|
|
|
|
|
2012-08-28 19:38:08 +00:00
|
|
|
typedef XVisualInfo* (* PFNGLXCHOOSEVISUAL) (Display *, int, int *);
|
|
|
|
PFNGLXCHOOSEVISUAL glXChooseVisual = cast<PFNGLXCHOOSEVISUAL>(glXGetProcAddress("glXChooseVisual"));
|
2011-05-03 20:07:17 +00:00
|
|
|
|
2012-08-28 19:38:08 +00:00
|
|
|
typedef GLXContext (* PFNGLXCREATECONTEXT) (Display *, XVisualInfo *, GLXContext, Bool);
|
|
|
|
PFNGLXCREATECONTEXT glXCreateContext = cast<PFNGLXCREATECONTEXT>(glXGetProcAddress("glXCreateContext"));
|
2011-05-03 20:07:17 +00:00
|
|
|
|
|
|
|
typedef Bool (* PFNGLXMAKECURRENT) (Display*, GLXDrawable, GLXContext);
|
2011-09-07 21:17:45 +00:00
|
|
|
PFNGLXMAKECURRENT glXMakeCurrent = cast<PFNGLXMAKECURRENT>(glXGetProcAddress("glXMakeCurrent"));
|
2011-05-03 20:07:17 +00:00
|
|
|
|
|
|
|
typedef void (* PFNGLXDESTROYCONTEXT) (Display*, GLXContext);
|
2011-09-07 21:17:45 +00:00
|
|
|
PFNGLXDESTROYCONTEXT glXDestroyContext = cast<PFNGLXDESTROYCONTEXT>(glXGetProcAddress("glXDestroyContext"));
|
2011-05-03 20:07:17 +00:00
|
|
|
|
|
|
|
typedef GLubyte* (* PFNGLGETSTRING) (GLenum);
|
2011-09-07 21:17:45 +00:00
|
|
|
PFNGLGETSTRING glGetString = cast<PFNGLGETSTRING>(glXGetProcAddress("glGetString"));
|
2011-08-05 01:13:25 +00:00
|
|
|
|
2011-05-03 20:07:17 +00:00
|
|
|
if (!glXQueryExtension ||
|
2011-08-19 15:39:00 +00:00
|
|
|
!glXQueryVersion ||
|
2012-08-28 19:38:08 +00:00
|
|
|
!glXChooseVisual ||
|
|
|
|
!glXCreateContext ||
|
2011-05-03 20:07:17 +00:00
|
|
|
!glXMakeCurrent ||
|
|
|
|
!glXDestroyContext ||
|
2011-09-07 21:17:45 +00:00
|
|
|
!glGetString)
|
2011-05-03 20:07:17 +00:00
|
|
|
{
|
2011-09-07 21:17:45 +00:00
|
|
|
fatal_error("glXGetProcAddress couldn't find required functions");
|
2011-05-03 20:07:17 +00:00
|
|
|
}
|
|
|
|
///// Open a connection to the X server /////
|
2013-10-10 20:36:42 +00:00
|
|
|
Display *dpy = XOpenDisplay(nullptr);
|
2011-05-03 20:07:17 +00:00
|
|
|
if (!dpy)
|
|
|
|
fatal_error("Unable to open a connection to the X server");
|
|
|
|
|
|
|
|
///// Check that the GLX extension is present /////
|
2013-10-10 20:36:42 +00:00
|
|
|
if (!glXQueryExtension(dpy, nullptr, nullptr))
|
2011-05-03 20:07:17 +00:00
|
|
|
fatal_error("GLX extension missing");
|
|
|
|
|
|
|
|
XSetErrorHandler(x_error_handler);
|
|
|
|
|
2012-08-28 19:38:08 +00:00
|
|
|
///// Get a visual /////
|
|
|
|
int attribs[] = {
|
|
|
|
GLX_RGBA,
|
|
|
|
GLX_RED_SIZE, 1,
|
|
|
|
GLX_GREEN_SIZE, 1,
|
|
|
|
GLX_BLUE_SIZE, 1,
|
|
|
|
None };
|
|
|
|
XVisualInfo *vInfo = glXChooseVisual(dpy, DefaultScreen(dpy), attribs);
|
2011-06-10 19:05:10 +00:00
|
|
|
if (!vInfo)
|
2012-08-28 19:38:08 +00:00
|
|
|
fatal_error("No visuals found");
|
2011-05-03 20:07:17 +00:00
|
|
|
|
2012-08-23 03:16:30 +00:00
|
|
|
// using a X11 Window instead of a GLXPixmap does not crash
|
|
|
|
// fglrx in indirect rendering. bug 680644
|
2012-08-28 19:38:08 +00:00
|
|
|
Window window;
|
2012-08-23 03:16:30 +00:00
|
|
|
XSetWindowAttributes swa;
|
|
|
|
swa.colormap = XCreateColormap(dpy, RootWindow(dpy, vInfo->screen),
|
|
|
|
vInfo->visual, AllocNone);
|
2012-08-28 19:38:08 +00:00
|
|
|
|
2012-08-23 03:16:30 +00:00
|
|
|
swa.border_pixel = 0;
|
2012-08-28 19:38:08 +00:00
|
|
|
window = XCreateWindow(dpy, RootWindow(dpy, vInfo->screen),
|
|
|
|
0, 0, 16, 16,
|
2012-08-23 03:16:30 +00:00
|
|
|
0, vInfo->depth, InputOutput, vInfo->visual,
|
|
|
|
CWBorderPixel | CWColormap, &swa);
|
|
|
|
|
2011-05-03 20:07:17 +00:00
|
|
|
///// Get a GL context and make it current //////
|
2013-10-10 20:36:42 +00:00
|
|
|
GLXContext context = glXCreateContext(dpy, vInfo, nullptr, True);
|
2012-08-28 19:38:08 +00:00
|
|
|
glXMakeCurrent(dpy, window, context);
|
2011-05-03 20:07:17 +00:00
|
|
|
|
2011-08-05 01:13:25 +00:00
|
|
|
///// Look for this symbol to determine texture_from_pixmap support /////
|
|
|
|
void* glXBindTexImageEXT = glXGetProcAddress("glXBindTexImageEXT");
|
|
|
|
|
2011-05-03 20:07:17 +00:00
|
|
|
///// Get GL vendor/renderer/versions strings /////
|
|
|
|
enum { bufsize = 1024 };
|
|
|
|
char buf[bufsize];
|
|
|
|
const GLubyte *vendorString = glGetString(GL_VENDOR);
|
|
|
|
const GLubyte *rendererString = glGetString(GL_RENDERER);
|
|
|
|
const GLubyte *versionString = glGetString(GL_VERSION);
|
2014-02-23 23:03:34 +00:00
|
|
|
|
2011-05-03 20:07:17 +00:00
|
|
|
if (!vendorString || !rendererString || !versionString)
|
|
|
|
fatal_error("glGetString returned null");
|
|
|
|
|
|
|
|
int length = snprintf(buf, bufsize,
|
2011-08-05 01:13:25 +00:00
|
|
|
"VENDOR\n%s\nRENDERER\n%s\nVERSION\n%s\nTFP\n%s\n",
|
2011-05-03 20:07:17 +00:00
|
|
|
vendorString,
|
|
|
|
rendererString,
|
2011-08-05 01:13:25 +00:00
|
|
|
versionString,
|
|
|
|
glXBindTexImageEXT ? "TRUE" : "FALSE");
|
2011-05-03 20:07:17 +00:00
|
|
|
if (length >= bufsize)
|
|
|
|
fatal_error("GL strings length too large for buffer size");
|
|
|
|
|
|
|
|
///// Clean up. Indeed, the parent process might fail to kill us (e.g. if it doesn't need to check GL info)
|
|
|
|
///// so we might be staying alive for longer than expected, so it's important to consume as little memory as
|
2011-06-17 15:49:27 +00:00
|
|
|
///// possible. Also we want to check that we're able to do that too without generating X errors.
|
2013-10-10 20:36:42 +00:00
|
|
|
glXMakeCurrent(dpy, None, nullptr); // must release the GL context before destroying it
|
2011-06-18 01:42:31 +00:00
|
|
|
glXDestroyContext(dpy, context);
|
2012-08-28 19:38:08 +00:00
|
|
|
XDestroyWindow(dpy, window);
|
|
|
|
XFreeColormap(dpy, swa.colormap);
|
2014-02-23 23:03:34 +00:00
|
|
|
|
|
|
|
#ifdef NS_FREE_PERMANENT_DATA // conditionally defined in nscore.h, don't forget to #include it above
|
2011-05-03 20:07:17 +00:00
|
|
|
XCloseDisplay(dpy);
|
2014-02-23 23:03:34 +00:00
|
|
|
#else
|
|
|
|
// This XSync call wanted to be instead:
|
|
|
|
// XCloseDisplay(dpy);
|
|
|
|
// but this can cause 1-minute stalls on certain setups using Nouveau, see bug 973192
|
|
|
|
XSync(dpy, False);
|
|
|
|
#endif
|
|
|
|
|
2011-05-03 20:07:17 +00:00
|
|
|
dlclose(libgl);
|
2012-01-31 20:32:05 +00:00
|
|
|
|
|
|
|
///// Finally write data to the pipe
|
2015-11-02 05:53:26 +00:00
|
|
|
mozilla::Unused << write(write_end_of_the_pipe, buf, length);
|
2011-05-03 20:07:17 +00:00
|
|
|
}
|
|
|
|
|
2014-03-28 20:06:18 +00:00
|
|
|
}
|
|
|
|
|
2011-06-10 19:07:54 +00:00
|
|
|
/** \returns true in the child glxtest process, false in the parent process */
|
|
|
|
bool fire_glxtest_process()
|
2011-05-03 20:07:17 +00:00
|
|
|
{
|
|
|
|
int pfd[2];
|
|
|
|
if (pipe(pfd) == -1) {
|
|
|
|
perror("pipe");
|
2011-06-10 19:07:54 +00:00
|
|
|
return false;
|
2011-05-03 20:07:17 +00:00
|
|
|
}
|
|
|
|
pid_t pid = fork();
|
|
|
|
if (pid < 0) {
|
|
|
|
perror("fork");
|
2011-06-10 19:07:54 +00:00
|
|
|
close(pfd[0]);
|
|
|
|
close(pfd[1]);
|
|
|
|
return false;
|
2011-05-03 20:07:17 +00:00
|
|
|
}
|
2014-03-12 20:32:57 +00:00
|
|
|
// The child exits early to avoid running the full shutdown sequence and avoid conflicting with threads
|
|
|
|
// we have already spawned (like the profiler).
|
2011-05-03 20:07:17 +00:00
|
|
|
if (pid == 0) {
|
|
|
|
close(pfd[0]);
|
|
|
|
write_end_of_the_pipe = pfd[1];
|
|
|
|
glxtest();
|
|
|
|
close(pfd[1]);
|
2014-03-28 20:06:18 +00:00
|
|
|
_exit(0);
|
2011-05-03 20:07:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
close(pfd[1]);
|
|
|
|
mozilla::widget::glxtest_pipe = pfd[0];
|
|
|
|
mozilla::widget::glxtest_pid = pid;
|
2011-06-10 19:07:54 +00:00
|
|
|
return false;
|
2011-05-03 20:07:17 +00:00
|
|
|
}
|