[gnome-boxes/allow-to-disable-accel3d-notification] libvirt-machine: Offer option to disable 3D acceleration



commit 145b313132892e5988ff1dbd74aa479e626805d9
Author: Felipe Borges <felipeborges gnome org>
Date:   Fri Mar 15 15:07:38 2019 +0100

    libvirt-machine: Offer option to disable 3D acceleration
    
    Unfortunately not all the hosts support 3D acceleration with the
    current machinery we provide. Therefore VMs can become unusable
    by enabling 3D acceleration.
    
    We throw a notification for OSes that support 3D acceleration
    offering the user to DISABLE it manually in case they want.
    If the notification is dismissed, we assume the user is not
    having any problems and therefore we don't show the notification
    ever again.
    
    A workaround in order to bring the notification back is to edit
    the broker source file (usually $HOME/.config/gnome-boxes/sources/
    QEMU\ Session) and change the "tweaked-accel3d" key from "true" to
    "false".
    
    See https://github.com/flathub/org.gnome.Boxes/issues/15

 src/box-config.vala      |  5 ++++
 src/libvirt-machine.vala | 78 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 83 insertions(+)
---
diff --git a/src/box-config.vala b/src/box-config.vala
index 15843b55..6710c17b 100644
--- a/src/box-config.vala
+++ b/src/box-config.vala
@@ -37,6 +37,11 @@
         set { keyfile.set_string_list (group, "categories", value); }
     }
 
+    public bool tweaked_accel3d {
+        get { return get_boolean (group, "tweaked-accel3d", false); }
+        set { keyfile.set_boolean (group, "tweaked-accel3d", value); }
+    }
+
     public int64 access_last_time { set; get; }
     public int64 access_first_time { set; get; }
     public int64 access_total_time { set; get; } // in seconds
diff --git a/src/libvirt-machine.vala b/src/libvirt-machine.vala
index cd8d87f9..0f1b8290 100644
--- a/src/libvirt-machine.vala
+++ b/src/libvirt-machine.vala
@@ -23,6 +23,7 @@
             update_status ();
         }
     }
+
     // If this machine is currently being imported
     public bool importing { get { return vm_creator != null && vm_creator is VMImporter; } }
 
@@ -615,6 +616,59 @@ else if (state == MachineState.SLEEPING) {
                 }
             }
         }
+
+        /* Some users might have issues with 3D acceleration due to various setups
+         * with different video drivers in the host. So we should offer an option
+         * to run these VMs without 3D acceleration.
+         */
+        if (!config.tweaked_accel3d && yield supports_accel3d ()) {
+            Boxes.Notification notification = null;
+
+            Notification.OKFunc disable_accel3d = () => {
+                notification = null;
+
+                GLib.List<GVirConfig.DomainDevice> devices = null;
+                foreach (var device in domain_config.get_devices ()) {
+                    if (device is GVirConfig.DomainGraphicsSpice) {
+                        var graphics_device = VMConfigurator.create_graphics_device (false);
+
+                        devices.prepend (graphics_device);
+                    } else if (device is GVirConfig.DomainVideo) {
+                        var video_device = device as GVirConfig.DomainVideo;
+
+                        video_device.set_accel3d (false);
+                        devices.prepend (device);
+                    } else {
+                        devices.prepend (device);
+                    }
+                }
+
+                devices.reverse ();
+                domain_config.set_devices (devices);
+
+                try {
+                    domain.set_config (domain_config);
+                    config.tweaked_accel3d = true;
+                } catch (GLib.Error error) {
+                    debug ("Failed to disable 3d acceleration!\n");
+                }
+
+                force_shutdown ();
+            };
+
+            Notification.DismissFunc dismiss_notification = () => {
+                notification = null;
+
+                config.tweaked_accel3d = true;
+            };
+
+            var message = _("Experiencing graphics problems?");
+            notification = window.notificationbar.display_for_action (message,
+                                                                      _("Disable 3D acceleration"),
+                                                                      (owned) disable_accel3d,
+                                                                      (owned) dismiss_notification,
+                                                                      -1);
+        }
     }
 
     public override void restart () {
@@ -722,6 +776,30 @@ public override async void clone () {
         }
     }
 
+    private async bool supports_accel3d () {
+        var os = yield get_os ();
+        if (os == null)
+            return false;
+
+        var devices = os.get_all_devices (null);
+
+        return (find_device_by_prop (devices, Osinfo.DEVICE_PROP_NAME, "virtio1.0-gpu") != null);
+    }
+
+    public async Osinfo.Os? get_os () {
+        var os_id = VMConfigurator.get_os_id (domain_config);
+        if (os_id == null)
+            return null;
+
+        var os_db = MediaManager.get_instance ().os_db;
+        try {
+            return yield os_db.get_os_by_id (os_id);
+        } catch (OSDatabaseError error) {
+            warning ("Failed to find OS with ID '%s': %s", os_id, error.message);
+            return null;
+        }
+    }
+
     public string? get_ip_address () {
         if (system_virt_connection == null || !is_on)
             return null;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]