Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iOS: fix request_redraw from RedrawRequested #3378

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Unreleased` header.
- **Breaking:** Rename `VideoMode` to `VideoModeHandle` to represent that it doesn't hold static data.
- **Breaking:** No longer export `platform::x11::XNotSupported`.
- **Breaking:** Renamed `platform::x11::XWindowType` to `platform::x11::WindowType`.
- On iOS, fix calling `Window::request_redraw()` from `WindowEvent:RedrawRequested`.

# 0.29.9

Expand Down
117 changes: 68 additions & 49 deletions src/platform_impl/ios/app_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,16 @@ enum AppStateImpl {
ProcessingRedraws {
event_handler: Box<dyn EventHandler>,
active_control_flow: ControlFlow,
queued_gpu_redraws: HashSet<Id<WinitUIWindow>>,
},
Waiting {
waiting_event_handler: Box<dyn EventHandler>,
start: Instant,
queued_gpu_redraws: HashSet<Id<WinitUIWindow>>,
},
PollFinished {
waiting_event_handler: Box<dyn EventHandler>,
queued_gpu_redraws: HashSet<Id<WinitUIWindow>>,
},
Terminated,
}
Expand Down Expand Up @@ -251,55 +254,61 @@ impl AppState {
return None;
}

let (event_handler, event) = match (self.control_flow, self.take_state()) {
(
ControlFlow::Poll,
AppStateImpl::PollFinished {
waiting_event_handler,
},
) => (
waiting_event_handler,
EventWrapper::StaticEvent(Event::NewEvents(StartCause::Poll)),
),
(
ControlFlow::Wait,
AppStateImpl::Waiting {
waiting_event_handler,
start,
},
) => (
waiting_event_handler,
EventWrapper::StaticEvent(Event::NewEvents(StartCause::WaitCancelled {
start,
requested_resume: None,
})),
),
(
ControlFlow::WaitUntil(requested_resume),
AppStateImpl::Waiting {
let (event_handler, event, queued_gpu_redraws) =
match (self.control_flow, self.take_state()) {
(
ControlFlow::Poll,
AppStateImpl::PollFinished {
waiting_event_handler,
queued_gpu_redraws,
},
) => (
waiting_event_handler,
start,
},
) => {
let event = if Instant::now() >= requested_resume {
EventWrapper::StaticEvent(Event::NewEvents(StartCause::ResumeTimeReached {
EventWrapper::StaticEvent(Event::NewEvents(StartCause::Poll)),
queued_gpu_redraws,
),
(
ControlFlow::Wait,
AppStateImpl::Waiting {
waiting_event_handler,
start,
requested_resume,
}))
} else {
queued_gpu_redraws,
},
) => (
waiting_event_handler,
EventWrapper::StaticEvent(Event::NewEvents(StartCause::WaitCancelled {
start,
requested_resume: Some(requested_resume),
}))
};
(waiting_event_handler, event)
}
s => bug!("`EventHandler` unexpectedly woke up {:?}", s),
};
requested_resume: None,
})),
queued_gpu_redraws,
),
(
ControlFlow::WaitUntil(requested_resume),
AppStateImpl::Waiting {
waiting_event_handler,
start,
queued_gpu_redraws,
},
) => {
let event = if Instant::now() >= requested_resume {
EventWrapper::StaticEvent(Event::NewEvents(StartCause::ResumeTimeReached {
start,
requested_resume,
}))
} else {
EventWrapper::StaticEvent(Event::NewEvents(StartCause::WaitCancelled {
start,
requested_resume: Some(requested_resume),
}))
};
(waiting_event_handler, event, queued_gpu_redraws)
}
s => bug!("`EventHandler` unexpectedly woke up {:?}", s),
};

self.set_state(AppStateImpl::ProcessingEvents {
event_handler,
queued_gpu_redraws: Default::default(),
queued_gpu_redraws,
active_control_flow: self.control_flow,
});
Some(event)
Expand Down Expand Up @@ -361,7 +370,8 @@ impl AppState {
AppStateImpl::ProcessingRedraws {
event_handler,
active_control_flow,
} => (event_handler, Default::default(), active_control_flow, true),
queued_gpu_redraws,
} => (event_handler, queued_gpu_redraws, active_control_flow, true),
AppStateImpl::PollFinished { .. }
| AppStateImpl::Waiting { .. }
| AppStateImpl::Terminated => unreachable!(),
Expand Down Expand Up @@ -389,6 +399,7 @@ impl AppState {
self.set_state(AppStateImpl::ProcessingRedraws {
event_handler,
active_control_flow,
queued_gpu_redraws: HashSet::default(),
});
queued_gpu_redraws
}
Expand All @@ -397,13 +408,15 @@ impl AppState {
if !self.has_launched() || self.has_terminated() {
return;
}
let (waiting_event_handler, old) = match self.take_state() {
let (waiting_event_handler, old, queued_gpu_redraws) = match self.take_state() {
AppStateImpl::ProcessingRedraws {
event_handler,
active_control_flow,
} => (event_handler, active_control_flow),
queued_gpu_redraws,
} => (event_handler, active_control_flow, queued_gpu_redraws),
s => bug!("unexpected state {:?}", s),
};
let has_queued_gpu_redraws = !queued_gpu_redraws.is_empty();

let new = self.control_flow;
match (old, new) {
Expand All @@ -412,6 +425,7 @@ impl AppState {
self.set_state(AppStateImpl::Waiting {
waiting_event_handler,
start,
queued_gpu_redraws,
});
}
(ControlFlow::WaitUntil(old_instant), ControlFlow::WaitUntil(new_instant))
Expand All @@ -421,13 +435,15 @@ impl AppState {
self.set_state(AppStateImpl::Waiting {
waiting_event_handler,
start,
queued_gpu_redraws,
});
}
(_, ControlFlow::Wait) => {
let start = Instant::now();
self.set_state(AppStateImpl::Waiting {
waiting_event_handler,
start,
queued_gpu_redraws,
});
self.waker.stop()
}
Expand All @@ -436,17 +452,23 @@ impl AppState {
self.set_state(AppStateImpl::Waiting {
waiting_event_handler,
start,
queued_gpu_redraws,
});
self.waker.start_at(new_instant)
}
// Unlike on macOS, handle Poll to Poll transition here to call the waker
(_, ControlFlow::Poll) => {
self.set_state(AppStateImpl::PollFinished {
waiting_event_handler,
queued_gpu_redraws,
});
self.waker.start()
}
}

if has_queued_gpu_redraws {
self.waker.start();
}
}

fn terminated_transition(&mut self) -> Box<dyn EventHandler> {
Expand Down Expand Up @@ -645,13 +667,10 @@ pub(crate) fn handle_nonuser_events<I: IntoIterator<Item = EventWrapper>>(
_ => unreachable!(),
};
this.app_state = Some(if processing_redraws {
bug_assert!(
queued_gpu_redraws.is_empty(),
"redraw queued while processing redraws"
);
AppStateImpl::ProcessingRedraws {
event_handler,
active_control_flow,
queued_gpu_redraws,
}
} else {
AppStateImpl::ProcessingEvents {
Expand Down