1use fidl_fuchsia_ui_composition::{ContentId, FlatlandProxy, PresentArgs, TransformId};
6use fuchsia_trace as ftrace;
7use std::cell::RefCell;
8use std::rc::Rc;
9use std::sync::atomic::{AtomicUsize, Ordering};
10
11pub type FlatlandInstanceId = usize;
12
13static NEXT_FLATLAND_INSTANCE_ID: AtomicUsize = AtomicUsize::new(1);
14
15pub struct Flatland {
16 flatland: FlatlandProxy,
17 id: FlatlandInstanceId,
18 id_generator: fuchsia_scenic::flatland::IdGenerator,
19 release_fences: Vec<zx::Event>,
20 next_trace_id: u64,
21}
22
23pub type FlatlandPtr = Rc<RefCell<Flatland>>;
24
25impl Flatland {
33 pub fn new(flatland: FlatlandProxy) -> FlatlandPtr {
34 let id = NEXT_FLATLAND_INSTANCE_ID.fetch_add(1, Ordering::SeqCst);
35 let debug_name = format!("WaylandBridge:{}", id);
36 flatland.set_debug_name(&debug_name).expect("fidl error");
37
38 Rc::new(RefCell::new(Flatland {
39 flatland,
40 id,
41 id_generator: fuchsia_scenic::flatland::IdGenerator::new(),
42 release_fences: vec![],
43 next_trace_id: 1,
44 }))
45 }
46
47 pub fn id(&self) -> FlatlandInstanceId {
48 self.id
49 }
50
51 pub fn proxy(&self) -> &FlatlandProxy {
52 &self.flatland
53 }
54
55 pub fn add_release_fence(&mut self, fence: zx::Event) {
56 self.release_fences.push(fence);
57 }
58
59 pub fn present(&mut self, presentation_time: i64) {
60 let release_fences: Vec<_> = self.release_fences.drain(..).collect();
61 self.flatland
62 .present(PresentArgs {
63 requested_presentation_time: Some(presentation_time),
64 acquire_fences: None,
65 release_fences: Some(release_fences),
66 unsquashable: Some(false),
68 ..Default::default()
69 })
70 .unwrap_or_else(|e| eprintln!("present error: {:?}", e));
71 }
72
73 pub fn alloc_transform_id(&mut self) -> TransformId {
74 self.id_generator.next_transform_id()
75 }
76
77 pub fn alloc_content_id(&mut self) -> ContentId {
78 self.id_generator.next_content_id()
79 }
80
81 pub fn alloc_trace_id(&mut self) -> ftrace::Id {
82 let id = self.next_trace_id;
83 self.next_trace_id += 1;
84 id.into()
85 }
86}