crosvm: add x-display argument for choosing the X11 gpu display

TEST=cargo run -- run --gpu --x-display :0
BUG=None

Change-Id: I76b4b33a6b14cb6fad322ffa95f00cce976f81a3
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1670550
Reviewed-by: Zach Reizner <zachr@chromium.org>
Commit-Queue: Zach Reizner <zachr@chromium.org>
Tested-by: Zach Reizner <zachr@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
diff --git a/src/linux.rs b/src/linux.rs
index 56750ae..b579e8a 100644
--- a/src/linux.rs
+++ b/src/linux.rs
@@ -11,6 +11,7 @@
 use std::fs::{File, OpenOptions};
 use std::io::{self, stdin, Read};
 use std::net::Ipv4Addr;
+use std::num::NonZeroU8;
 use std::os::unix::io::{AsRawFd, FromRawFd, RawFd};
 use std::os::unix::net::UnixStream;
 use std::path::{Path, PathBuf};
@@ -562,19 +563,33 @@
     exit_evt: &EventFd,
     gpu_device_socket: VmMemoryControlRequestSocket,
     gpu_sockets: Vec<virtio::resource_bridge::ResourceResponseSocket>,
-    wayland_socket_path: &Path,
+    wayland_socket_path: Option<PathBuf>,
+    x_display: Option<String>,
 ) -> DeviceResult {
     let jailed_wayland_path = Path::new("/wayland-0");
 
+    let mut display_backends = vec![
+        virtio::DisplayBackend::X(x_display),
+        virtio::DisplayBackend::Null,
+    ];
+
+    if let Some(socket_path) = wayland_socket_path.as_ref() {
+        display_backends.insert(
+            0,
+            virtio::DisplayBackend::Wayland(if cfg.sandbox {
+                Some(jailed_wayland_path.to_owned())
+            } else {
+                Some(socket_path.to_owned())
+            }),
+        );
+    }
+
     let dev = virtio::Gpu::new(
         exit_evt.try_clone().map_err(Error::CloneEventFd)?,
         Some(gpu_device_socket),
+        NonZeroU8::new(1).unwrap(), // number of scanouts
         gpu_sockets,
-        if cfg.sandbox {
-            &jailed_wayland_path
-        } else {
-            wayland_socket_path
-        },
+        display_backends,
     );
 
     let jail = match simple_jail(&cfg, "gpu_device.policy")? {
@@ -619,7 +634,9 @@
 
             // Bind mount the wayland socket into jail's root. This is necessary since each
             // new wayland context must open() the socket.
-            jail.mount_bind(wayland_socket_path, jailed_wayland_path, true)?;
+            if let Some(path) = wayland_socket_path {
+                jail.mount_bind(path.as_ref(), jailed_wayland_path, true)?;
+            }
 
             add_crosvm_user_to_jail(&mut jail, "gpu")?;
 
@@ -894,15 +911,14 @@
     #[cfg(feature = "gpu")]
     {
         if cfg.gpu {
-            if let Some(wayland_socket_path) = &cfg.wayland_socket_path {
-                devs.push(create_gpu_device(
-                    cfg,
-                    _exit_evt,
-                    gpu_device_socket,
-                    resource_bridges,
-                    wayland_socket_path,
-                )?);
-            }
+            devs.push(create_gpu_device(
+                cfg,
+                _exit_evt,
+                gpu_device_socket,
+                resource_bridges,
+                cfg.wayland_socket_path.clone(),
+                cfg.x_display.clone(),
+            )?);
         }
     }