glx: Lift sending the MakeCurrent request to top-level code
authorAdam Jackson <ajax@redhat.com>
Tue, 14 Nov 2017 20:13:04 +0000 (15:13 -0500)
committerMarge Bot <eric+marge@anholt.net>
Tue, 13 Apr 2021 22:58:58 +0000 (22:58 +0000)
commit80b67a3b444f31462890a8e390650fa77c4d2010
tree8d40c7a4d2e09bbfff84c8076f80bdcccacfa0f0
parentb8236bcb7444157c0acbf9b775856be7075e97e1
glx: Lift sending the MakeCurrent request to top-level code

Somewhat terrifyingly, we never sent this for direct contexts, which
means the server never knew the context/drawable bindings. To handle
this sanely, pull the request code up out of the indirect backend, and
rewrite the context switch path to call it as appropriate.  This
attempts to preserve the existing behavior of not calling unbind() on
the context if its refcount would not drop to zero.

Of course, you can't just do this indiscriminately, because this is GLX
and extant X servers have bugs and everything is terrible. To wit:

- For 1.20.x prior to 1.20.6, you can bind a direct context once, but
the second time you try to modify the context's binding you will get
GLXBadContextTag. This includes unbinding the context. And "deleting"
the context will leak memory, because it will still appear to be
current.

- For 1.19 and earlier, glXMakeCurrent(dpy, None, ctx) should be legal
for GL 3.0+ contexts, but the server will throw BadMatch.

To guard against this, we only send the request for indirect contexts
unless the server is known good, and only mention one context at a time
in such a request; if switching between contexts, we first unbind the
old, and then bind the new. Note that the second VendorRelease() version
is to catch XFree86 4.x and Xorg [67].x, which almost certainly have the
above bugs. Other servers might report different version numbers here,
but we can't do direct rendering against them, so this should be safe.

Fixes: mesa/mesa#4418
Acked-By: Mike Blumenkrantz <michael.blumenkrantz@gmail.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/9992>
src/broadcom/ci/piglit-v3d-rpi4-fails.txt
src/broadcom/ci/piglit-vc4-rpi3-fails.txt
src/broadcom/ci/piglit-vc4-rpi3-flakes.txt
src/gallium/drivers/freedreno/ci/piglit-freedreno-a530-fails.txt
src/gallium/drivers/freedreno/ci/piglit-freedreno-a630-fails.txt
src/gallium/drivers/llvmpipe/ci/llvmpipe-quick_gl.txt
src/gallium/drivers/softpipe/ci/softpipe-quick.txt
src/gallium/drivers/zink/ci/piglit-zink-lvp-fails.txt
src/glx/glxcurrent.c
src/glx/indirect_glx.c