From bd40b36f01e2f114b0647ec7365550fb9f610326 Mon Sep 17 00:00:00 2001
From: Richard Grenville <pyxlcy@gmail.com>
Date: Fri, 5 Jul 2013 23:21:05 +0800
Subject: Bug fix #124: GLX: Missing check on FBConfig X visual depth

- Check FBConfig X visual depth, like Compiz, to fix issues with
  nvidia-drivers-325.08 . Thanks to guotsuan for reporting.
---
 opengl.c | 28 ++++++++++++++++++++++------
 1 file changed, 22 insertions(+), 6 deletions(-)

(limited to 'opengl.c')

diff --git a/opengl.c b/opengl.c
index c9be5beec..a788cd509 100644
--- a/opengl.c
+++ b/opengl.c
@@ -383,18 +383,31 @@ glx_update_fbconfig(session_t *ps) {
       .texture_tgts = 0,
       .y_inverted = false,
     };
+    int id = (int) (pcur - pfbcfgs);
     int depth = 0, depth_alpha = 0, val = 0;
 
     if (Success != glXGetFBConfigAttrib(ps->dpy, *pcur, GLX_BUFFER_SIZE, &depth)
         || Success != glXGetFBConfigAttrib(ps->dpy, *pcur, GLX_ALPHA_SIZE, &depth_alpha)) {
-      printf_errf("(): Failed to retrieve buffer size and alpha size of FBConfig %d.", (int) (pcur - pfbcfgs));
+      printf_errf("(): Failed to retrieve buffer size and alpha size of FBConfig %d.", id);
       continue;
     }
     if (Success != glXGetFBConfigAttrib(ps->dpy, *pcur, GLX_BIND_TO_TEXTURE_TARGETS_EXT, &fbinfo.texture_tgts)) {
-      printf_errf("(): Failed to retrieve BIND_TO_TEXTURE_TARGETS_EXT of FBConfig %d.", (int) (pcur - pfbcfgs));
+      printf_errf("(): Failed to retrieve BIND_TO_TEXTURE_TARGETS_EXT of FBConfig %d.", id);
       continue;
     }
 
+    int visualdepth = 0;
+    {
+      XVisualInfo *pvi = glXGetVisualFromFBConfig(ps->dpy, *pcur);
+      if (!pvi) {
+        // On nvidia-drivers-325.08 this happens slightly too often...
+        // printf_errf("(): Failed to retrieve X Visual of FBConfig %d.", id);
+        continue;
+      }
+      visualdepth = pvi->depth;
+	  cxfree(pvi);
+    }
+
     bool rgb = false;
     bool rgba = false;
 
@@ -407,12 +420,15 @@ glx_update_fbconfig(session_t *ps) {
     if (Success == glXGetFBConfigAttrib(ps->dpy, *pcur, GLX_Y_INVERTED_EXT, &val))
       fbinfo.y_inverted = val;
 
-    if ((depth - depth_alpha) < 32 && rgb) {
-      fbinfo.texture_fmt = GLX_TEXTURE_FORMAT_RGB_EXT;
-      glx_update_fbconfig_bydepth(ps, depth - depth_alpha, &fbinfo);
+    {
+      int tgtdpt = depth - depth_alpha;
+      if (tgtdpt == visualdepth && tgtdpt < 32 && rgb) {
+        fbinfo.texture_fmt = GLX_TEXTURE_FORMAT_RGB_EXT;
+        glx_update_fbconfig_bydepth(ps, tgtdpt, &fbinfo);
+      }
     }
 
-    if (rgba) {
+    if (depth == visualdepth && rgba) {
       fbinfo.texture_fmt = GLX_TEXTURE_FORMAT_RGBA_EXT;
       glx_update_fbconfig_bydepth(ps, depth, &fbinfo);
     }
-- 
cgit v1.2.1