crosvm/gpu_display/examples/simple_open.rs
Daniel Verkamp 87fd7a8d40 gpu_display: add basic error handling to examples
This helps diagnose problems if the examples don't work (for example,
simple_open requires the `x` feature to be enabled).

BUG=None
TEST=run simple and simple_open examples

Change-Id: Ic115d48d93531bf2313948597a49a003538949cd
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4029530
Reviewed-by: Zihan Chen <zihanchen@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2022-11-16 19:33:35 +00:00

45 lines
1.2 KiB
Rust

// Copyright 2022 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;
use anyhow::Context;
use anyhow::Result;
use gpu_display::GpuDisplay;
use gpu_display::SurfaceType;
fn run() -> Result<()> {
let mut disp = GpuDisplay::open_x(None::<&str>).context("open_x")?;
let surface_id = disp
.create_surface(None, 1280, 1024, SurfaceType::Scanout)
.context("create_surface")?;
let mem = disp.framebuffer(surface_id).context("framebuffer")?;
for y in 0..1024 {
let mut row = [0u32; 1280];
for (x, item) in row.iter_mut().enumerate() {
let b = ((x as f32 / 1280.0) * 256.0) as u32;
let g = ((y as f32 / 1024.0) * 256.0) as u32;
*item = b | (g << 8);
}
mem.as_volatile_slice()
.offset(1280 * 4 * y)
.unwrap()
.copy_from(&row);
}
disp.flip(surface_id);
while !disp.close_requested(surface_id) {
disp.dispatch_events().context("dispatch_events")?;
}
Ok(())
}
fn main() {
if let Err(e) = run() {
eprintln!("error: {:#}", e);
exit(1);
}
}