carnelian/render/generic/forma/
mod.rs

1// Copyright 2020 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 crate::color::{srgb_to_linear, Color};
6use crate::drawing::DisplayRotation;
7use crate::render::generic::Backend;
8
9use euclid::default::Size2D;
10use fidl::endpoints::ClientEnd;
11use fidl_fuchsia_sysmem2::BufferCollectionTokenMarker;
12
13mod composition;
14mod context;
15mod image;
16mod path;
17mod raster;
18
19pub use composition::FormaComposition;
20pub use context::FormaContext;
21pub use image::FormaImage;
22pub use path::{FormaPath, FormaPathBuilder};
23pub use raster::{FormaRaster, FormaRasterBuilder};
24
25#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
26pub struct Forma;
27
28impl Forma {
29    // Available for tests.
30    pub fn new_context_without_token(
31        size: Size2D<u32>,
32        display_rotation: DisplayRotation,
33    ) -> FormaContext {
34        FormaContext::without_token(size, display_rotation)
35    }
36}
37
38impl Backend for Forma {
39    type Image = FormaImage;
40    type Context = FormaContext;
41    type Path = FormaPath;
42    type PathBuilder = FormaPathBuilder;
43    type Raster = FormaRaster;
44    type RasterBuilder = FormaRasterBuilder;
45    type Composition = FormaComposition;
46
47    fn new_context(
48        token: ClientEnd<BufferCollectionTokenMarker>,
49        size: Size2D<u32>,
50        display_rotation: DisplayRotation,
51    ) -> FormaContext {
52        FormaContext::new(token, size, display_rotation)
53    }
54}
55
56impl From<&Color> for forma::Color {
57    fn from(color: &Color) -> Self {
58        let Color { r, g, b, a } = color;
59        forma::Color {
60            r: srgb_to_linear(*r),
61            g: srgb_to_linear(*g),
62            b: srgb_to_linear(*b),
63            a: *a as f32 * 255.0f32.recip(),
64        }
65    }
66}
67
68#[cfg(test)]
69mod tests {
70    use super::*;
71
72    use fidl::endpoints::create_endpoints;
73
74    use euclid::size2;
75
76    use crate::render::generic;
77
78    #[test]
79    fn forma_init() {
80        generic::tests::run(|| {
81            let (token, _) = create_endpoints::<BufferCollectionTokenMarker>();
82            Forma::new_context(token, size2(100, 100), DisplayRotation::Deg0);
83        });
84    }
85}