Merge branch 'switcheroo-discrete-detection' into 'main'

shell-app: improve discrete GPU detection

See merge request GNOME/gnome-shell!3193
This commit is contained in:
Jan Drögehoff 2024-06-30 06:19:17 +00:00
commit 5793f5fc00
2 changed files with 62 additions and 1 deletions

View file

@ -38,7 +38,8 @@
will contain a user-facing name for the GPU, the "Environment" (as) key will
contain an array of even number of strings, each being an environment
variable to set to use the GPU, followed by its value, the "Default" (b) key
will tag the default (usually integrated) GPU.
will tag the default GPU, the "Discrete" (b) key tags if the GPU is a
dedicated component.
-->
<property name="GPUs" type="aa{sv}" access="read"/>

View file

@ -1271,6 +1271,66 @@ apply_discrete_gpu_env (GAppLaunchContext *context,
}
num_children = g_variant_n_children (variant);
// Check if the Default GPU is Discrete
for (i = 0; i < num_children; i++)
{
g_autoptr(GVariant) gpu = NULL;
g_autoptr(GVariant) env = NULL;
g_autoptr(GVariant) default_variant = NULL;
g_autoptr(GVariant) discrete_variant = NULL;
g_autofree const char **env_s = NULL;
gpu = g_variant_get_child_value (variant, i);
if (!gpu ||
!g_variant_is_of_type (gpu, G_VARIANT_TYPE ("a{s*}")))
continue;
/* Skip over non-default GPUs */
default_variant = g_variant_lookup_value (gpu, "Default", NULL);
if (!default_variant || !g_variant_get_boolean (default_variant))
continue;
/* break out if default GPU is not discrete */
discrete_variant = g_variant_lookup_value (gpu, "Discrete", NULL);
if (!discrete_variant || !g_variant_get_boolean (discrete_variant))
break;
// Default GPU is discrete, no need to do anything
return;
}
// Find the first Discrete GPU
for (i = 0; i < num_children; i++)
{
g_autoptr(GVariant) gpu = NULL;
g_autoptr(GVariant) env = NULL;
g_autoptr(GVariant) discrete_variant = NULL;
g_autofree const char **env_s = NULL;
guint j;
gpu = g_variant_get_child_value (variant, i);
if (!gpu ||
!g_variant_is_of_type (gpu, G_VARIANT_TYPE ("a{s*}")))
continue;
/* Skip over non-discrete GPUs */
discrete_variant = g_variant_lookup_value (gpu, "Discrete", NULL);
if (!discrete_variant || !g_variant_get_boolean (discrete_variant))
continue;
env = g_variant_lookup_value (gpu, "Environment", NULL);
if (!env)
continue;
env_s = g_variant_get_strv (env, NULL);
for (j = 0; env_s[j] != NULL; j = j + 2)
g_app_launch_context_setenv (context, env_s[j], env_s[j+1]);
return;
}
// fallback to old behavior
// find the first non-Default GPU
for (i = 0; i < num_children; i++)
{
g_autoptr(GVariant) gpu = NULL;