From 098adb5a75c78177d2958eb47e30c889eb613470 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Yao=20Wei=20=28=E9=AD=8F=E9=8A=98=E5=BB=B7=29?=
 <yao.wei@canonical.com>
Date: Mon, 17 Mar 2025 10:23:55 +0800
Subject: [PATCH] about: fix multiple GPU name display with NVIDIA GPU on
 Desktop PC

Fix the issue on Desktop PC where it always launch print-renderer
helper binary with NVIDIA library despite we are assigning
non-NVIDIA gpu. NVIDIA GL library does not take DRI_PRIME, and we
are assuming this library is always used together with parameters
provided by switcheroo.

Fixes: https://gitlab.gnome.org/GNOME/gnome-control-center/-/issues/3375
Origin: upstream, https://gitlab.gnome.org/GNOME/gnome-control-center/-/merge_requests/3092.patch
---
 .../gnome-control-center-print-renderer.c     | 21 +++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)

diff --git a/panels/system/about/gnome-control-center-print-renderer.c b/panels/system/about/gnome-control-center-print-renderer.c
index f3e5d23495..ab72e70883 100644
--- a/panels/system/about/gnome-control-center-print-renderer.c
+++ b/panels/system/about/gnome-control-center-print-renderer.c
@@ -32,7 +32,8 @@ get_gtk_gles_renderer (void)
         GtkNative *native;
         GtkWidget *win;
         GdkGLContext *context;
-        char *renderer = NULL;
+        g_autofree char *renderer = NULL;
+        g_autofree char *gl_version = NULL;
 
         win = gtk_window_new ();
         gtk_widget_realize (win);
@@ -43,10 +44,26 @@ get_gtk_gles_renderer (void)
                 return NULL;
         gdk_gl_context_make_current (context);
         renderer = g_strdup ((char *) glGetString (GL_RENDERER));
+        gl_version = g_strdup ((char *) glGetString (GL_VERSION));
         gdk_gl_context_clear_current ();
         g_object_unref (context);
 
-        return renderer;
+        if (strstr (gl_version, "NVIDIA") != NULL)
+          {
+            const char *glvnd_libname = g_getenv ("__GLX_VENDOR_LIBRARY_NAME");
+            if (g_strcmp0 (glvnd_libname, "nvidia") != 0)
+              {
+                /* This helper is launched with parameters from a
+                 * non-NVIDIA GPU, but is running using a NVIDIA
+                 * library. As such, DRI_PRIME envvar from switcheroo
+                 * does not actually take effect, and the GPU name is
+                 * invalid.
+                 */
+                return NULL;
+              }
+          }
+
+        return g_steal_pointer (&renderer);
 }
 
 int
-- 
GitLab

