Repository: servo/servo
File: ports/servoshell/egl/ohos/mod.rs (Rust)

Code before:

    log::info!("Servo main-thread channel initialized");

    let wakeup = Box::new(WakeupCallback::new(tx));

    let servo = init_app(init_opts, wakeup).expect("Servo initialization failed");

    while let Ok(action) = rx.recv() {
        trace!("Wakeup message received!");
        action.do_action(&servo);
    }

    info!("Sender disconnected - Terminating main surface thread");
}

#[unsafe(no_mangle)]
extern "C" fn on_surface_created_cb(xcomponent: *mut OH_NativeXComponent, window: *mut c_void) {
    static FIRST_WINDOW: AtomicBool = AtomicBool::new(true);
    info!("on_surface_created_cb");
    #[cfg(feature = "tracing-hitrace")]
    let _ = hitrace::ScopedTrace::start_trace(&c"on_surface_created_cb");

    let xc_wrapper = XComponentWrapper(xcomponent);
    let window_wrapper = WindowWrapper(window);

    // TODO: This if will be removed once we add multi-window support in a follow-up PR.
    // This function will always be invoked on the UI thread, so there is no concurrency.
    if FIRST_WINDOW.load(Ordering::Relaxed) {
        FIRST_WINDOW.store(false, Ordering::Relaxed);
        // The servo event loop is initialized before the native window / xcomponent is created,
        // so if this fails, servo has crashed and we don't have a way to recover.
        call(ServoAction::CreatePlatformWindow(
            xc_wrapper,
            window_wrapper,
        ))
        .expect("Servo main thread channel not initialized");

        let native_vsync =
            ohos_vsync::NativeVsync::new("ServoVsync").expect("Failed to create NativeVsync");
        // get_period() returns an error - perhaps we need to wait until the first callback?
        // info!("Native vsync period is {} nanoseconds", native_vsync.get_period().unwrap());
        unsafe {
            native_vsync
                .request_raw_callback_with_self(Some(on_vsync_cb))
                .expect("Failed to request vsync callback")
        }
        info!("Enabled Vsync!");
    } else {
        call(ServoAction::NewWebview(xc_wrapper, window_wrapper))
            .expect("Servo main thread channel not initialized");
    }

Review comment (suggestion):
```suggestion
    // TODO: This will be removed once we add multi-window support in a follow-up PR.
```

Code after:

    log::info!("Servo main-thread channel initialized");

    let wakeup = Box::new(WakeupCallback::new(tx));

    let servo = init_app(init_opts, wakeup).expect("Servo initialization failed");

    while let Ok(action) = rx.recv() {
        trace!("Wakeup message received!");
        action.do_action(&servo);
    }

    info!("Sender disconnected - Terminating main surface thread");
}

#[unsafe(no_mangle)]
extern "C" fn on_surface_created_cb(xcomponent: *mut OH_NativeXComponent, window: *mut c_void) {
    static FIRST_WINDOW: AtomicBool = AtomicBool::new(true);
    info!("on_surface_created_cb");
    #[cfg(feature = "tracing-hitrace")]
    let _ = hitrace::ScopedTrace::start_trace(&c"on_surface_created_cb");

    let xc_wrapper = XComponentWrapper(xcomponent);
    let window_wrapper = WindowWrapper(window);

    // TODO: This if will be removed once we add multi-window support in a follow-up PR.
    // This function will always be invoked on the UI thread, so there is no concurrency.
    if FIRST_WINDOW.load(Ordering::Relaxed) {
        FIRST_WINDOW.store(false, Ordering::Relaxed);
        // The servo event loop is initialized before the native window / xcomponent is created,
        // so if this fails, servo has crashed and we don't have a way to recover.
        call(ServoAction::CreatePlatformWindow(
            xc_wrapper,
            window_wrapper,
        ))
        .expect("Servo main thread channel not initialized");

        let native_vsync =
            ohos_vsync::NativeVsync::new("ServoVsync").expect("Failed to create NativeVsync");
        unsafe {
            native_vsync
                .request_raw_callback_with_self(Some(on_vsync_cb))
                .expect("Failed to request vsync callback")
        }
        info!("Enabled Vsync!");
    } else {
        call(ServoAction::NewWebview(xc_wrapper, window_wrapper))
            .expect("Servo main thread channel not initialized");
    }
    info!("Returning from on_surface_created_cb");
}