carnelian/render/generic/forma/
mod.rs

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright 2020 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use crate::color::{srgb_to_linear, Color};
use crate::drawing::DisplayRotation;
use crate::render::generic::Backend;

use euclid::default::Size2D;
use fidl::endpoints::ClientEnd;
use fidl_fuchsia_sysmem2::BufferCollectionTokenMarker;

mod composition;
mod context;
mod image;
mod path;
mod raster;

pub use composition::FormaComposition;
pub use context::FormaContext;
pub use image::FormaImage;
pub use path::{FormaPath, FormaPathBuilder};
pub use raster::{FormaRaster, FormaRasterBuilder};

#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Forma;

impl Forma {
    // Available for tests.
    pub fn new_context_without_token(
        size: Size2D<u32>,
        display_rotation: DisplayRotation,
    ) -> FormaContext {
        FormaContext::without_token(size, display_rotation)
    }
}

impl Backend for Forma {
    type Image = FormaImage;
    type Context = FormaContext;
    type Path = FormaPath;
    type PathBuilder = FormaPathBuilder;
    type Raster = FormaRaster;
    type RasterBuilder = FormaRasterBuilder;
    type Composition = FormaComposition;

    fn new_context(
        token: ClientEnd<BufferCollectionTokenMarker>,
        size: Size2D<u32>,
        display_rotation: DisplayRotation,
    ) -> FormaContext {
        FormaContext::new(token, size, display_rotation)
    }
}

impl From<&Color> for forma::Color {
    fn from(color: &Color) -> Self {
        let Color { r, g, b, a } = color;
        forma::Color {
            r: srgb_to_linear(*r),
            g: srgb_to_linear(*g),
            b: srgb_to_linear(*b),
            a: *a as f32 * 255.0f32.recip(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use fidl::endpoints::create_endpoints;

    use euclid::size2;

    use crate::render::generic;

    #[test]
    fn forma_init() {
        generic::tests::run(|| {
            let (token, _) = create_endpoints::<BufferCollectionTokenMarker>();
            Forma::new_context(token, size2(100, 100), DisplayRotation::Deg0);
        });
    }
}