use fidl_fuchsia_ui_composition::{ContentId, FlatlandProxy, PresentArgs, TransformId};
use fuchsia_trace as ftrace;
use std::cell::RefCell;
use std::rc::Rc;
use std::sync::atomic::{AtomicUsize, Ordering};
pub type FlatlandInstanceId = usize;
static NEXT_FLATLAND_INSTANCE_ID: AtomicUsize = AtomicUsize::new(1);
pub struct Flatland {
flatland: FlatlandProxy,
id: FlatlandInstanceId,
id_generator: fuchsia_scenic::flatland::IdGenerator,
release_fences: Vec<zx::Event>,
next_trace_id: u64,
}
pub type FlatlandPtr = Rc<RefCell<Flatland>>;
impl Flatland {
pub fn new(flatland: FlatlandProxy) -> FlatlandPtr {
let id = NEXT_FLATLAND_INSTANCE_ID.fetch_add(1, Ordering::SeqCst);
let debug_name = format!("WaylandBridge:{}", id);
flatland.set_debug_name(&debug_name).expect("fidl error");
Rc::new(RefCell::new(Flatland {
flatland,
id,
id_generator: fuchsia_scenic::flatland::IdGenerator::new(),
release_fences: vec![],
next_trace_id: 1,
}))
}
pub fn id(&self) -> FlatlandInstanceId {
self.id
}
pub fn proxy(&self) -> &FlatlandProxy {
&self.flatland
}
pub fn add_release_fence(&mut self, fence: zx::Event) {
self.release_fences.push(fence);
}
pub fn present(&mut self, presentation_time: i64) {
let release_fences: Vec<_> = self.release_fences.drain(..).collect();
self.flatland
.present(PresentArgs {
requested_presentation_time: Some(presentation_time),
acquire_fences: None,
release_fences: Some(release_fences),
unsquashable: Some(false),
..Default::default()
})
.unwrap_or_else(|e| eprintln!("present error: {:?}", e));
}
pub fn alloc_transform_id(&mut self) -> TransformId {
self.id_generator.next_transform_id()
}
pub fn alloc_content_id(&mut self) -> ContentId {
self.id_generator.next_content_id()
}
pub fn alloc_trace_id(&mut self) -> ftrace::Id {
let id = self.next_trace_id;
self.next_trace_id += 1;
id.into()
}
}