Skip to content

Commit

Permalink
fixup! Choreographer
Browse files Browse the repository at this point in the history
  • Loading branch information
MarijnS95 committed Oct 8, 2024
1 parent 3109dfe commit d6eaac2
Showing 1 changed file with 45 additions and 43 deletions.
88 changes: 45 additions & 43 deletions ndk/src/choreographer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,36 +339,15 @@ pub struct ChoreographerFrameCallbackData {
#[cfg(feature = "api-level-33")]
impl fmt::Debug for ChoreographerFrameCallbackData {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// TODO: Make struct public and hide function calls
// #[derive(Eq)]
struct FrameTimeline<'a>(usize, &'a ChoreographerFrameCallbackData);
impl<'a> fmt::Debug for FrameTimeline<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("FrameTimeline")
.field("index", &self.0)
.field("vsync_id()", &self.1.frame_timeline_vsync_id(self.0))
.field(
"expected_presentation_time()",
&self.1.frame_timeline_expected_presentation_time(self.0),
)
.field("deadline()", &self.1.frame_timeline_deadline(self.0))
.field(
"calculated latch time",
&(self.1.frame_timeline_expected_presentation_time(self.0)
- self.1.frame_timeline_deadline(self.0)),
)
.finish()
}
}
let x = (0..self.frame_timelines_length())
.map(|i| FrameTimeline(i, self))
.collect::<Vec<_>>();
f.debug_struct("ChoreographerFrameCallbackData")
.field("frame_time()", &self.frame_time())
.field("frame_timelines", &x)
.field(
"preferred_frame_timeline",
&FrameTimeline(self.preferred_frame_timeline_index(), self),
"frame_timelines()",
&self.frame_timelines().collect::<Vec<_>>(),
)
.field(
"preferred_frame_timeline()",
&self.preferred_frame_timeline(),
)
.finish()
}
Expand All @@ -391,33 +370,56 @@ impl ChoreographerFrameCallbackData {
)
}

/// The number of possible frame timelines.
/// Returns an iterator over possible frame timelines.
#[doc(alias = "AChoreographerFrameCallbackData_getFrameTimelinesLength")]
// TODO: Java just returns an array here with the three by-index-getters hidden away
pub fn frame_timelines_length(&self) -> usize {
unsafe { ffi::AChoreographerFrameCallbackData_getFrameTimelinesLength(self.ptr.as_ptr()) }
pub fn frame_timelines(&self) -> impl Iterator<Item = FrameTimeline<'_>> {
let length = unsafe {
ffi::AChoreographerFrameCallbackData_getFrameTimelinesLength(self.ptr.as_ptr())
};
(0..length).map(|i| FrameTimeline(i, self))
}

/// Gets the index of the platform-preferred frame timeline.
/// Gets the platform-preferred frame timeline.
///
/// The preferred frame timeline is the default by which the platform scheduled the app, based
/// on the device configuration.
#[doc(alias = "AChoreographerFrameCallbackData_getPreferredFrameTimelineIndex")]
pub fn preferred_frame_timeline_index(&self) -> usize {
unsafe {
pub fn preferred_frame_timeline(&self) -> FrameTimeline<'_> {
let index = unsafe {
ffi::AChoreographerFrameCallbackData_getPreferredFrameTimelineIndex(self.ptr.as_ptr())
}
};
FrameTimeline(index, self)
}
}

pub struct FrameTimeline<'a>(usize, &'a ChoreographerFrameCallbackData);
impl fmt::Debug for FrameTimeline<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("FrameTimeline")
.field("index", &self.0)
.field("vsync_id()", &self.vsync_id())
.field(
"expected_presentation_time()",
&self.expected_presentation_time(),
)
.field("deadline()", &self.deadline())
.finish()
}
}

impl FrameTimeline<'_> {
/// Gets the token used by the platform to identify the frame timeline at the given \c index.
///
/// # Parameters
/// - `index`: index of a frame timeline, in `[0, frame_timelines_length)`. See
/// [`ChoreographerFrameCallbackData::frame_timelines_length()`].
#[doc(alias = "AChoreographerFrameCallbackData_getFrameTimelineVsyncId")]
pub fn frame_timeline_vsync_id(&self, index: usize) -> ffi::AVsyncId {
pub fn vsync_id(&self) -> ffi::AVsyncId {
unsafe {
ffi::AChoreographerFrameCallbackData_getFrameTimelineVsyncId(self.ptr.as_ptr(), index)
ffi::AChoreographerFrameCallbackData_getFrameTimelineVsyncId(
self.1.ptr.as_ptr(),
self.0,
)
}
}
/// Gets the time at which the frame described at the given `index` is expected to be presented.
Expand All @@ -427,11 +429,11 @@ impl ChoreographerFrameCallbackData {
/// - `index`: index of a frame timeline, in `[0, frame_timelines_length)`. See
/// [`ChoreographerFrameCallbackData::frame_timelines_length()`].
#[doc(alias = "AChoreographerFrameCallbackData_getFrameTimelineExpectedPresentationTimeNanos")]
pub fn frame_timeline_expected_presentation_time(&self, index: usize) -> Duration {
pub fn expected_presentation_time(&self) -> Duration {
let nanos = unsafe {
ffi::AChoreographerFrameCallbackData_getFrameTimelineExpectedPresentationTimeNanos(
self.ptr.as_ptr(),
index,
self.1.ptr.as_ptr(),
self.0,
)
};
Duration::from_nanos(
Expand All @@ -448,11 +450,11 @@ impl ChoreographerFrameCallbackData {
/// - `index`: index of a frame timeline, in `[0, frame_timelines_length)`. See
/// [`ChoreographerFrameCallbackData::frame_timelines_length()`].
#[doc(alias = "AChoreographerFrameCallbackData_getFrameTimelineDeadlineNanos")]
pub fn frame_timeline_deadline(&self, index: usize) -> Duration {
pub fn deadline(&self) -> Duration {
let nanos = unsafe {
ffi::AChoreographerFrameCallbackData_getFrameTimelineDeadlineNanos(
self.ptr.as_ptr(),
index,
self.1.ptr.as_ptr(),
self.0,
)
};
Duration::from_nanos(
Expand Down

0 comments on commit d6eaac2

Please sign in to comment.