Skip to content

Commit 938ccba

Browse files
committed
fix(client_core): 🐛 Fix rendering C API
1 parent 3a060d3 commit 938ccba

File tree

1 file changed

+45
-4
lines changed

1 file changed

+45
-4
lines changed

alvr/client_core/src/c_api.rs

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ pub extern "C" fn alvr_poll_event(out_event: *mut AlvrEvent) -> bool {
382382
}
383383
}
384384

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

706706
#[no_mangle]
707707
pub extern "C" fn alvr_initialize_opengl() {
708-
GRAPHICS_CONTEXT.set(Some(Rc::new(GraphicsContext::new_gl())));
708+
let context = GraphicsContext::new_gl();
709+
context.make_current();
710+
711+
GRAPHICS_CONTEXT.set(Some(Rc::new(context)));
709712
}
710713

714+
/// The GL context might be invalid after this function! Destroy any GL resources before calling
715+
/// this.
711716
#[no_mangle]
712717
pub extern "C" fn alvr_destroy_opengl() {
713718
GRAPHICS_CONTEXT.set(None);
@@ -747,12 +752,24 @@ pub unsafe extern "C" fn alvr_resume_opengl(
747752
convert_swapchain_array(swapchain_textures, swapchain_length),
748753
"",
749754
)));
755+
756+
GRAPHICS_CONTEXT.with_borrow(|context| {
757+
if let Some(ctx) = context {
758+
ctx.make_current();
759+
}
760+
});
750761
}
751762

752763
#[no_mangle]
753764
pub extern "C" fn alvr_pause_opengl() {
754765
STREAM_RENDERER.set(None);
755-
LOBBY_RENDERER.set(None)
766+
LOBBY_RENDERER.set(None);
767+
768+
GRAPHICS_CONTEXT.with_borrow(|context| {
769+
if let Some(ctx) = context {
770+
ctx.make_current();
771+
}
772+
});
756773
}
757774

758775
#[no_mangle]
@@ -762,6 +779,12 @@ pub unsafe extern "C" fn alvr_update_hud_message_opengl(message: *const c_char)
762779
renderer.update_hud_message(CStr::from_ptr(message).to_str().unwrap());
763780
}
764781
});
782+
783+
GRAPHICS_CONTEXT.with_borrow(|context| {
784+
if let Some(ctx) = context {
785+
ctx.make_current();
786+
}
787+
});
765788
}
766789

767790
#[no_mangle]
@@ -790,6 +813,12 @@ pub unsafe extern "C" fn alvr_start_stream_opengl(config: AlvrStreamConfig) {
790813
1.0, // TODO: encoding gamma config
791814
None, // TODO: passthrough config
792815
)));
816+
817+
GRAPHICS_CONTEXT.with_borrow(|context| {
818+
if let Some(ctx) = context {
819+
ctx.make_current();
820+
}
821+
});
793822
}
794823

795824
// todo: support hands
@@ -821,6 +850,12 @@ pub unsafe extern "C" fn alvr_render_lobby_opengl(
821850
);
822851
}
823852
});
853+
854+
GRAPHICS_CONTEXT.with_borrow(|context| {
855+
if let Some(ctx) = context {
856+
ctx.make_current();
857+
}
858+
});
824859
}
825860

826861
/// view_params: array of 2
@@ -850,6 +885,12 @@ pub unsafe extern "C" fn alvr_render_stream_opengl(
850885
);
851886
}
852887
});
888+
889+
GRAPHICS_CONTEXT.with_borrow(|context| {
890+
if let Some(ctx) = context {
891+
ctx.make_current();
892+
}
893+
});
853894
}
854895

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

970-
// Returns true if the timestamp and buffer has been written to
1011+
/// Returns true if the timestamp and buffer has been written to
9711012
#[no_mangle]
9721013
pub extern "C" fn alvr_get_frame(
9731014
out_timestamp_ns: *mut u64,

0 commit comments

Comments
 (0)