From: Gregory Hainaut Date: Mon, 29 May 2017 11:18:25 +0000 (+0200) Subject: dri: Extend __DRIbackgroundCallableExtensionRec to include a callback that checks... X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=fa84f6225b1342dbe564962f639de50c5e9029d3;p=mesa.git dri: Extend __DRIbackgroundCallableExtensionRec to include a callback that checks for thread safety DRI-drivers could call Xlib functions, for example to allocate a new back buffer. When glthread is enabled, the driver runs mostly on a separate thread. Therefore we need to guarantee the thread safety between libX11 calls from the applications (not aware of the extra thread) and the ones from the driver. See discussion thread: https://lists.freedesktop.org/archives/mesa-dev/2017-April/152547.html Fortunately, Xlib allows to lock display to ensure thread safety but XInitThreads must be called first by the application to initialize the lock function pointer. This patch will allow to check XInitThreads was called to allow glthread on GLX or EGL platform. Note: a tentative was done to port libX11 code to XCB but it didn't solve fully thread safety. See discussion thread: https://lists.freedesktop.org/archives/mesa-dev/2017-April/153137.html Note: Nvidia forces the driver to call XInitThreads. Quoting their manpage: "The NVIDIA OpenGL driver will automatically attempt to enable Xlib thread-safe mode if needed. However, it might not be possible in some situations, such as when the NVIDIA OpenGL driver library is dynamically loaded after Xlib has been loaded and initialized. If that is the case, threaded optimizations will stay disabled unless the application is modified to call XInitThreads() before initializing Xlib or to link directly against the NVIDIA OpenGL driver library. Alternatively, using the LD_PRELOAD environment variable to include the NVIDIA OpenGL driver library should also achieve the desired result." v2: based on Nicolai and Matt feedback Use C style comment v3: based on Emil feedback split the patch in 3 s/isGlThreadSafe/isThreadSafe/ v5: based on Marek comment Add a comment that isThreadSafe is supported by extension v2 Signed-off-by: Gregory Hainaut Reviewed-by: Emil Velikov --- diff --git a/include/GL/internal/dri_interface.h b/include/GL/internal/dri_interface.h index c83056aa702..ffe99499fca 100644 --- a/include/GL/internal/dri_interface.h +++ b/include/GL/internal/dri_interface.h @@ -1721,6 +1721,19 @@ struct __DRIbackgroundCallableExtensionRec { * operations (e.g. it should just set a thread-local variable). */ void (*setBackgroundContext)(void *loaderPrivate); + + /** + * Indicate that it is multithread safe to use glthread. For GLX/EGL + * platforms using Xlib, that involves calling XInitThreads, before + * opening an X display. + * + * Note: only supported if extension version is at least 2. + * + * \param loaderPrivate is the value that was passed to to the driver when + * the context was created. This can be used by the loader to identify + * which context any callbacks are associated with. + */ + GLboolean (*isThreadSafe)(void *loaderPrivate); }; #endif