sl4f_lib/scenic/
facade.rs1use crate::scenic::types::ScreenshotDataDef;
6use anyhow::{Context as _, Error};
7use fidl_fuchsia_ui_composition::{self as ui_comp, ScreenshotMarker};
8use fidl_fuchsia_ui_scenic::ScreenshotData;
9use fuchsia_component::{self as app};
10use serde_json::{to_value, Value};
11
12#[derive(Debug)]
16pub struct ScenicFacade {}
17
18impl ScenicFacade {
19 pub fn new() -> ScenicFacade {
20 ScenicFacade {}
21 }
22 pub async fn take_screenshot(&self) -> Result<Value, Error> {
23 let screenshotter = app::client::connect_to_protocol::<ScreenshotMarker>()
25 .context("Failed to connect to screenshot")?;
26
27 let gnf_args = ui_comp::ScreenshotTakeRequest {
28 format: Some(ui_comp::ScreenshotFormat::BgraRaw),
29 ..Default::default()
30 };
31
32 let screenshot_response: ui_comp::ScreenshotTakeResponse;
33 let data = screenshotter.take(gnf_args).await;
34
35 match data {
36 Err(e) => {
37 return Err(format_err!("Screenshot.Take() failed with FIDL err: {}", e));
38 }
39 Ok(val) => screenshot_response = val,
40 }
41
42 let image_vmo = screenshot_response
43 .vmo
44 .ok_or_else(|| format_err!("invalid vmo format in ScreenshotTakeResponse"))?;
45
46 let image_size = screenshot_response
47 .size
48 .ok_or_else(|| format_err!("invalid size format in ScreenshotTakeResponse"))?;
49
50 let image_info = fidl_fuchsia_images::ImageInfo {
51 transform: fidl_fuchsia_images::Transform::Normal,
52 width: image_size.width,
53 height: image_size.height,
54 stride: image_size.width * 4,
55 pixel_format: fidl_fuchsia_images::PixelFormat::Bgra8,
56 color_space: fidl_fuchsia_images::ColorSpace::Srgb,
57 tiling: fidl_fuchsia_images::Tiling::Linear,
58 alpha_format: fidl_fuchsia_images::AlphaFormat::Opaque,
59 };
60
61 let buffer = fidl_fuchsia_mem::Buffer {
62 vmo: image_vmo,
63 size: u64::from(image_size.height * image_size.width * 4),
64 };
65
66 let screenshot: ScreenshotData = ScreenshotData { info: image_info, data: buffer };
67
68 return Ok(to_value(ScreenshotDataDef::new(screenshot))?);
69 }
70}