flatland_frame_scheduling_lib/
lib.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// 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.

mod throughput_scheduler;

pub use throughput_scheduler::ThroughputScheduler;

use async_trait::async_trait;
use std::cell::RefCell;
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll, Waker};
use {fidl_fuchsia_scenic_scheduling as frame_scheduling, fidl_fuchsia_ui_composition as flatland};

#[derive(Debug, PartialEq, Copy, Clone)]
pub struct PresentationInfo {
    pub latch_point: zx::MonotonicInstant,
    pub presentation_time: zx::MonotonicInstant,
}

#[derive(Debug, PartialEq, Copy, Clone)]
pub struct PresentedInfo {
    pub present_received_time: zx::MonotonicInstant,
    pub actual_latch_point: zx::MonotonicInstant,
}

impl From<frame_scheduling::PresentReceivedInfo> for PresentedInfo {
    fn from(item: frame_scheduling::PresentReceivedInfo) -> PresentedInfo {
        PresentedInfo {
            present_received_time: zx::MonotonicInstant::from_nanos(
                item.present_received_time.unwrap(),
            ),
            actual_latch_point: zx::MonotonicInstant::from_nanos(item.latched_time.unwrap()),
        }
    }
}

#[derive(Debug, PartialEq, Copy, Clone)]
pub struct PresentParameters {
    // The latch point we're expecting to make for this update.
    pub expected_latch_point: zx::MonotonicInstant,
    // The time we're expecting to be presented to the display.
    pub expected_presentation_time: zx::MonotonicInstant,
    // The requested_presentation_time to pass into Present().
    pub requested_presentation_time: zx::MonotonicInstant,
    // The unsquashable boolean to pass into Present().
    pub unsquashable: bool,
}

impl From<PresentParameters> for flatland::PresentArgs {
    fn from(item: PresentParameters) -> flatland::PresentArgs {
        flatland::PresentArgs {
            requested_presentation_time: Some(item.requested_presentation_time.into_nanos()),
            unsquashable: Some(item.unsquashable),
            ..Default::default()
        }
    }
}

#[async_trait(?Send)]
pub trait SchedulingLib {
    // Called whenever the client has new content that it wants displayed.
    // Eventually results in wait_to_update() resolving.
    fn request_present(&self) {}
    // Should be called whenever the OnFramePresented event is received.
    fn on_frame_presented(
        &self,
        _actual_presentation_time: zx::MonotonicInstant,
        _presented_infos: Vec<PresentedInfo>,
    ) {
    }
    // Should be called whenever the OnNextFrameBegin event is received.
    fn on_next_frame_begin(
        &self,
        additional_present_credits: u32,
        future_presentation_infos: Vec<PresentationInfo>,
    );
    // Async call that resolves when the caller should render/apply updates and call Present.
    // Caller should always be waiting on wait_to_update() to resolve.
    async fn wait_to_update(&self) -> PresentParameters;
}

struct SchedulingFuture<'a> {
    sched: &'a RefCell<dyn SchedulingFutureState>,
}

impl Future for SchedulingFuture<'_> {
    type Output = ();
    fn poll(self: Pin<&mut Self>, ctx: &mut Context<'_>) -> Poll<()> {
        let mut sched = self.sched.borrow_mut();
        if sched.ready_to_wake_up() {
            Poll::Ready(())
        } else {
            sched.set_waker(ctx.waker().clone());
            Poll::Pending
        }
    }
}
trait SchedulingFutureState {
    fn ready_to_wake_up(&self) -> bool;
    fn set_waker(&mut self, waker: Waker);
    fn get_waker(&self) -> &Option<Waker>;
    fn maybe_wakeup(&self) {
        if let Some(waker) = &self.get_waker() {
            if self.ready_to_wake_up() {
                waker.wake_by_ref();
            }
        }
    }
}