fuchsia_scenic/
flatland.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
// Copyright 2021 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 anyhow::Error;

pub use fidl_fuchsia_scenic_scheduling::PresentationInfo;
pub use fidl_fuchsia_ui_composition::{
    ChildViewWatcherMarker, ContentId, FlatlandDisplayMarker, FlatlandDisplayProxy, FlatlandError,
    FlatlandEvent, FlatlandEventStream, FlatlandMarker, FlatlandProxy, LayoutInfo,
    ParentViewportWatcherMarker, ParentViewportWatcherProxy, PresentArgs, TransformId,
    ViewBoundProtocols, ViewportProperties,
};
pub use fidl_fuchsia_ui_views::{ViewCreationToken, ViewportCreationToken};

// Pair of tokens used to link two Flatland sessions together.
pub struct ViewCreationTokenPair {
    pub view_creation_token: ViewCreationToken,
    pub viewport_creation_token: ViewportCreationToken,
}

impl ViewCreationTokenPair {
    pub fn new() -> Result<ViewCreationTokenPair, Error> {
        let (view_creation_token, viewport_creation_token) = zx::Channel::create();
        Ok(ViewCreationTokenPair {
            view_creation_token: ViewCreationToken { value: view_creation_token },
            viewport_creation_token: ViewportCreationToken { value: viewport_creation_token },
        })
    }
}

/// IdGenerator generates a monotonically-increasing sequence of IDs, either TransformIds or
/// ContentIds, depending on what the caller asks for.  The ID values are unique both across and
/// within ID types, e.g. a given IdGenerator will not generate two TransformIds with the same
/// value, nor a TransformId and a ContentId with the same value.
pub struct IdGenerator {
    next_id: u64,
}

impl IdGenerator {
    pub fn new() -> Self {
        IdGenerator { next_id: 1 }
    }

    pub fn new_with_first_id(first_id: u64) -> Self {
        IdGenerator { next_id: first_id }
    }

    pub fn next_transform_id(&mut self) -> TransformId {
        let id = self.next_id;
        self.next_id += 1;
        TransformId { value: id }
    }

    pub fn next_content_id(&mut self) -> ContentId {
        let id = self.next_id;
        self.next_id += 1;
        ContentId { value: id }
    }
}

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

    #[test]
    fn id_generator_basic() {
        let mut generator = IdGenerator::new();
        assert_eq!(generator.next_content_id(), ContentId { value: 1 });
        assert_eq!(generator.next_content_id(), ContentId { value: 2 });
        assert_eq!(generator.next_transform_id(), TransformId { value: 3 });
        assert_eq!(generator.next_transform_id(), TransformId { value: 4 });
    }

    #[test]
    fn id_generator_with_first_id() {
        let mut generator = IdGenerator::new_with_first_id(11);
        assert_eq!(generator.next_transform_id(), TransformId { value: 11 });
    }
}