diff --git a/alvr/client_core/src/c_api.rs b/alvr/client_core/src/c_api.rs index 6fa5f61ebe..c50f89ba3d 100644 --- a/alvr/client_core/src/c_api.rs +++ b/alvr/client_core/src/c_api.rs @@ -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(); @@ -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); @@ -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] @@ -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] @@ -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 @@ -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 @@ -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 @@ -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,