From d6eaac2df982f091dde061f8c3e0f36cb495e788 Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Wed, 9 Oct 2024 00:35:47 +0200 Subject: [PATCH] fixup! Choreographer --- ndk/src/choreographer.rs | 88 ++++++++++++++++++++-------------------- 1 file changed, 45 insertions(+), 43 deletions(-) diff --git a/ndk/src/choreographer.rs b/ndk/src/choreographer.rs index 5a5ed9f1..4e381722 100644 --- a/ndk/src/choreographer.rs +++ b/ndk/src/choreographer.rs @@ -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::>(); 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::>(), + ) + .field( + "preferred_frame_timeline()", + &self.preferred_frame_timeline(), ) .finish() } @@ -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> { + 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. @@ -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( @@ -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(