From beffc6063445bb33c357079701fef8011461ae03 Mon Sep 17 00:00:00 2001 From: Martin Fouilleul Date: Mon, 15 Aug 2022 23:11:13 +0200 Subject: [PATCH] redraw during live resize operations --- src/mp_app.h | 3 +++ src/osx_app.mm | 53 +++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/src/mp_app.h b/src/mp_app.h index ab54328..754951e 100644 --- a/src/mp_app.h +++ b/src/mp_app.h @@ -342,6 +342,9 @@ void mp_set_target_fps(u32 fps); // or use wait vblank? void mp_pump_events(f64 timeout); bool mp_next_event(mp_event* event); +typedef void(*mp_live_resize_callback)(mp_event event, void* data); +void mp_set_live_resize_callback(mp_live_resize_callback callback, void* data); + //-------------------------------------------------------------------- // Input state polling //-------------------------------------------------------------------- diff --git a/src/osx_app.mm b/src/osx_app.mm index f9cdcdd..a9893d1 100644 --- a/src/osx_app.mm +++ b/src/osx_app.mm @@ -171,6 +171,11 @@ struct mp_app_data mp_input_state inputState; ringbuffer eventQueue; + mp_live_resize_callback liveResizeCallback; + void* liveResizeData; + + CVDisplayLinkRef displayLink; + mp_frame_stats frameStats; NSTimer* frameTimer; @@ -181,7 +186,6 @@ struct mp_app_data mp_view_data viewPool[MP_APP_MAX_VIEWS]; list_info viewFreeList; - TISInputSourceRef kbLayoutInputSource; void* kbLayoutUnicodeData; id kbLayoutListener; @@ -839,9 +843,26 @@ static void mp_queue_event(mp_event* event) mp_rect viewFrame = {0, 0, contentRect.size.width, contentRect.size.height}; mp_view_set_frame(mpWindow->mainView, viewFrame); + + if(__mpAppData.liveResizeCallback) + { + __mpAppData.liveResizeCallback(event, __mpAppData.liveResizeData); + } + + //TODO: also ensure we don't overflow the queue during live resize... mp_queue_event(&event); } +-(void)windowWillStartLiveResize:(NSNotification *)notification +{ + CVDisplayLinkStart(__mpAppData.displayLink); +} + +-(void)windowDidEndLiveResize:(NSNotification *)notification +{ + CVDisplayLinkStop(__mpAppData.displayLink); +} + - (void)windowWillClose:(NSNotification *)notification { mpWindow->nsWindow = nil; @@ -1297,6 +1318,7 @@ static const NSRange kEmptyRange = { NSNotFound, 0 }; { } + @end //@implementation MPNativeView /* @@ -1326,6 +1348,25 @@ f64 mp_get_elapsed_seconds() } */ +CVReturn DisplayLinkCallback(CVDisplayLinkRef displayLink, + const CVTimeStamp *inNow, + const CVTimeStamp *inOutputTime, + CVOptionFlags flagsIn, + CVOptionFlags *flagsOut, + void *displayLinkContext) +{ + if(__mpAppData.liveResizeCallback) + { + mp_event event = {}; + event.type = MP_EVENT_FRAME; + + __mpAppData.liveResizeCallback(event, __mpAppData.liveResizeData); + } + + return(0); +} + + //*************************************************************** // public API //*************************************************************** @@ -1367,6 +1408,10 @@ void mp_init() LOG_MESSAGE("run application\n"); [NSApp run]; + CGDirectDisplayID displayID = CGMainDisplayID(); + CVDisplayLinkCreateWithCGDisplay(displayID, &__mpAppData.displayLink); + CVDisplayLinkSetOutputCallback(__mpAppData.displayLink, DisplayLinkCallback, 0); + [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular]; [NSApp activateIgnoringOtherApps:YES]; @@ -1993,6 +2038,12 @@ void mp_end_frame() // Events handling //-------------------------------------------------------------------- +void mp_set_live_resize_callback(mp_live_resize_callback callback, void* data) +{ + __mpAppData.liveResizeCallback = callback; + __mpAppData.liveResizeData = data; +} + void mp_pump_events(f64 timeout) { __mpAppData.inputState.frameCounter++;