rust-windowing / winit

Window handling library in pure Rust
https://docs.rs/winit/
Apache License 2.0
4.71k stars 888 forks source link

iOS. High CPU usage when application does nothing. #3816

Open VladasZ opened 1 month ago

VladasZ commented 1 month ago

Description

I noticed that my iPhone gets really hot when I run my projects. While trying to debug this issue I stripped all my code and only winit code remained but the phone was still hot.

I see that Xcode shows constant CPU load:

image

The same project has 0 CPU load on Android:

image

On Windows:

image

On Linux:

image

And on macOS:

image

What may cause the issue?

Minimal example to reproduce:

Rust code:

#[no_mangle]
pub extern "C" fn start_test_game() -> std::ffi::c_int {
    use winit::{
        application::ApplicationHandler,
        event::WindowEvent,
        event_loop::{ActiveEventLoop, ControlFlow, EventLoop},
        window::WindowId,
    };

    struct App;

    impl ApplicationHandler for App {
        fn resumed(&mut self, event_loop: &ActiveEventLoop) {}
        fn window_event(&mut self, event_loop: &ActiveEventLoop, id: WindowId, event: WindowEvent) {}
    }

    let event_loop = EventLoop::new().unwrap();

    event_loop.set_control_flow(ControlFlow::Wait);

    event_loop.run_app(&mut App).unwrap();
    0
}

main.m in Xcode project:

int start_test_game(void);

int main(int argc, char * argv[]) {
    return start_test_game();
}

Device and iOS version

iPhone 13 Pro running iOS 17.5.1, iPhone 5s running iOS 12.5.7, iPhone 15 Simulator running iOS 17.4

Winit version

0.30.1

lsy969999 commented 1 month ago

src/platform_impl/ios/app_state.rs -> new function in EventLoopWaker impl block

when i changed interval time param that repeated too often, cpu usage down to zero percent but i dont know the function and side effect

test code is same your test device is my real iPhone 15 pro max iOS 17.6 test winit version is 0.30.0

스크린샷 2024-08-09 10 59 27 스크린샷 2024-08-09 11 01 17 스크린샷 2024-08-09 11 01 54

PWhiddy commented 1 month ago

@lsy969999 Sorry if this is a bit off topic, but is your application open source or do you know of a minimal iOS winit example? I've tried setting it up in the obvious way, and have no trouble getting it to work on desktop, but when my application starts up I'm not receiving any touch events.

lsy969999 commented 1 month ago

@PWhiddy bevy repo uses winit, https://github.com/bevyengine/bevy/tree/main/examples test ios example

PWhiddy commented 1 month ago

Thanks. I have built the bevy iOS example and it works fine for me, but the bevy codebase is complicated enough that it seemed like non-trivial work to extract that sample to a standalone minimal example. Are there any projects that use winit on iOS besides bevy?

lsy969999 commented 1 month ago

Sorry, I learned about winit through Bevy, so I don't know about other examples using winit... Maybe tauri, egui, etc. use winit?

PWhiddy commented 1 month ago

I have been using egui though I'm not sure if its meant to support iOS out of the box. Currently investigating this repo which uses a forked egui: https://github.com/lucasmerlin/egui-apple-example

madsmtm commented 2 days ago

Indeed the problem here seems to be our CFRunLoopTimer, I think it gets triggered as-if you were using ControlFlow::Poll. Will assign myself and fix the issue before v0.31