aboutsummaryrefslogtreecommitdiff
path: root/gpu_display/examples/simple.rs
blob: afc5b1f3895e611a0154d6bb0e7d99ca4032bde6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Copyright 2019 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use std::process::exit;

#[cfg(any(target_os = "android", target_os = "linux"))]
mod platform {
    use anyhow::Context;
    use anyhow::Result;
    use gpu_display::*;
    use vm_control::gpu::DisplayMode;
    use vm_control::gpu::DisplayParameters;

    pub fn run() -> Result<()> {
        let mut disp = GpuDisplay::open_wayland(None::<&str>).context("open_wayland")?;
        let surface_id = disp
            .create_surface(
                None,
                /* scanout_id= */ Some(0),
                &DisplayParameters::default_with_mode(DisplayMode::Windowed(1280, 1024)),
                SurfaceType::Scanout,
            )
            .context("create_surface")?;
        disp.flip(surface_id);
        disp.commit(surface_id).context("commit")?;
        while !disp.close_requested(surface_id) {
            disp.dispatch_events().context("dispatch_events")?;
        }
        Ok(())
    }
}

#[cfg(not(unix))]
mod platform {
    use anyhow::anyhow;
    use anyhow::Result;

    pub fn run() -> Result<()> {
        Err(anyhow!("Only supported on unix targets"))
    }
}

fn main() {
    if let Err(e) = platform::run() {
        eprintln!("error: {:#}", e);
        exit(1);
    }
}