Skip to content

Commit

Permalink
fix(client_core): 🐛 Fix rendering C API
Browse files Browse the repository at this point in the history
  • Loading branch information
zmerp committed Dec 10, 2024
1 parent 3a060d3 commit 938ccba
Showing 1 changed file with 45 additions and 4 deletions.
49 changes: 45 additions & 4 deletions alvr/client_core/src/c_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ pub extern "C" fn alvr_poll_event(out_event: *mut AlvrEvent) -> bool {
}
}

// Returns the length of the message. message_buffer can be null.
/// Returns the length of the message. message_buffer can be null.
#[no_mangle]
pub extern "C" fn alvr_hud_message(message_buffer: *mut c_char) -> u64 {
let cstring = CString::new(HUD_MESSAGE.lock().clone()).unwrap();
Expand Down Expand Up @@ -705,9 +705,14 @@ pub struct AlvrStreamConfig {

#[no_mangle]
pub extern "C" fn alvr_initialize_opengl() {
GRAPHICS_CONTEXT.set(Some(Rc::new(GraphicsContext::new_gl())));
let context = GraphicsContext::new_gl();
context.make_current();

GRAPHICS_CONTEXT.set(Some(Rc::new(context)));
}

/// The GL context might be invalid after this function! Destroy any GL resources before calling
/// this.
#[no_mangle]
pub extern "C" fn alvr_destroy_opengl() {
GRAPHICS_CONTEXT.set(None);
Expand Down Expand Up @@ -747,12 +752,24 @@ pub unsafe extern "C" fn alvr_resume_opengl(
convert_swapchain_array(swapchain_textures, swapchain_length),
"",
)));

GRAPHICS_CONTEXT.with_borrow(|context| {
if let Some(ctx) = context {
ctx.make_current();
}
});
}

#[no_mangle]
pub extern "C" fn alvr_pause_opengl() {
STREAM_RENDERER.set(None);
LOBBY_RENDERER.set(None)
LOBBY_RENDERER.set(None);

GRAPHICS_CONTEXT.with_borrow(|context| {
if let Some(ctx) = context {
ctx.make_current();
}
});
}

#[no_mangle]
Expand All @@ -762,6 +779,12 @@ pub unsafe extern "C" fn alvr_update_hud_message_opengl(message: *const c_char)
renderer.update_hud_message(CStr::from_ptr(message).to_str().unwrap());
}
});

GRAPHICS_CONTEXT.with_borrow(|context| {
if let Some(ctx) = context {
ctx.make_current();
}
});
}

#[no_mangle]
Expand Down Expand Up @@ -790,6 +813,12 @@ pub unsafe extern "C" fn alvr_start_stream_opengl(config: AlvrStreamConfig) {
1.0, // TODO: encoding gamma config
None, // TODO: passthrough config
)));

GRAPHICS_CONTEXT.with_borrow(|context| {
if let Some(ctx) = context {
ctx.make_current();
}
});
}

// todo: support hands
Expand Down Expand Up @@ -821,6 +850,12 @@ pub unsafe extern "C" fn alvr_render_lobby_opengl(
);
}
});

GRAPHICS_CONTEXT.with_borrow(|context| {
if let Some(ctx) = context {
ctx.make_current();
}
});
}

/// view_params: array of 2
Expand Down Expand Up @@ -850,6 +885,12 @@ pub unsafe extern "C" fn alvr_render_stream_opengl(
);
}
});

GRAPHICS_CONTEXT.with_borrow(|context| {
if let Some(ctx) = context {
ctx.make_current();
}
});
}

// Decoder-related interface
Expand Down Expand Up @@ -967,7 +1008,7 @@ pub extern "C" fn alvr_destroy_decoder() {
*DECODER_SOURCE.lock() = None;
}

// Returns true if the timestamp and buffer has been written to
/// Returns true if the timestamp and buffer has been written to
#[no_mangle]
pub extern "C" fn alvr_get_frame(
out_timestamp_ns: *mut u64,
Expand Down

0 comments on commit 938ccba

Please sign in to comment.