sl4f_lib/scenic/
types.rs

1// Copyright 2019 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use anyhow::format_err;
6use base64::engine::general_purpose::STANDARD as BASE64_STANDARD;
7use base64::engine::Engine as _;
8use fidl_fuchsia_images::{AlphaFormat, ColorSpace, ImageInfo, PixelFormat, Tiling, Transform};
9use fidl_fuchsia_mem::Buffer;
10use fidl_fuchsia_ui_scenic::ScreenshotData;
11use serde::{Deserialize, Serialize, Serializer};
12
13/// Enum for supported FIDL commands.
14pub enum ScenicMethod {
15    TakeScreenshot,
16}
17
18impl std::str::FromStr for ScenicMethod {
19    type Err = anyhow::Error;
20
21    fn from_str(method: &str) -> Result<Self, Self::Err> {
22        match method {
23            "TakeScreenshot" => Ok(ScenicMethod::TakeScreenshot),
24            _ => return Err(format_err!("invalid Scenic FIDL method: {}", method)),
25        }
26    }
27}
28
29#[derive(Serialize, Deserialize, Debug)]
30#[serde(remote = "Transform")]
31pub enum TransformDef {
32    Normal = 0,
33    FlipHorizontal = 1,
34    FlipVertical = 2,
35    FlipVerticalAndHorizontal = 3,
36}
37
38#[derive(Serialize, Deserialize, Debug)]
39#[serde(remote = "PixelFormat")]
40pub enum PixelFormatDef {
41    Bgra8 = 0,
42    Yuy2 = 1,
43    Nv12 = 2,
44    Yv12 = 3,
45    R8G8B8A8 = 4,
46}
47
48#[derive(Serialize, Deserialize, Debug)]
49#[serde(remote = "ColorSpace")]
50pub enum ColorSpaceDef {
51    Srgb = 0,
52}
53
54#[derive(Serialize, Deserialize, Debug)]
55#[serde(remote = "Tiling")]
56pub enum TilingDef {
57    Linear = 0,
58    GpuOptimal = 1,
59}
60
61#[derive(Serialize, Deserialize, Debug)]
62#[serde(remote = "AlphaFormat")]
63pub enum AlphaFormatDef {
64    Opaque = 0,
65    Premultiplied = 1,
66    NonPremultiplied = 2,
67}
68
69#[derive(Serialize, Deserialize, Debug)]
70#[serde(remote = "ImageInfo")]
71pub struct ImageInfoDef {
72    #[serde(with = "TransformDef")]
73    pub transform: Transform,
74    pub width: u32,
75    pub height: u32,
76    pub stride: u32,
77    #[serde(with = "PixelFormatDef")]
78    pub pixel_format: PixelFormat,
79    #[serde(with = "ColorSpaceDef")]
80    pub color_space: ColorSpace,
81    #[serde(with = "TilingDef")]
82    pub tiling: Tiling,
83    #[serde(with = "AlphaFormatDef")]
84    pub alpha_format: AlphaFormat,
85}
86
87fn serialize_buffer<S>(buffer: &Buffer, serializer: S) -> Result<S::Ok, S::Error>
88where
89    S: Serializer,
90{
91    let mut data = vec![0; buffer.size as usize];
92    use serde::ser::Error;
93    buffer.vmo.read(&mut data, 0).map_err(Error::custom)?;
94    serializer.serialize_str(&BASE64_STANDARD.encode(&data))
95}
96
97#[derive(Serialize, Debug)]
98pub struct ScreenshotDataDef {
99    #[serde(with = "ImageInfoDef")]
100    pub info: ImageInfo,
101    #[serde(serialize_with = "serialize_buffer")]
102    pub data: Buffer,
103}
104
105impl ScreenshotDataDef {
106    pub fn new(screenshot_data: ScreenshotData) -> ScreenshotDataDef {
107        ScreenshotDataDef { info: screenshot_data.info, data: screenshot_data.data }
108    }
109}