WIP on macOS resizing support

This commit is contained in:
Daniel Collin 2019-10-01 16:36:33 +02:00
parent 0e3bf4a7e2
commit b4675905d2
5 changed files with 77 additions and 20 deletions

View file

@ -191,7 +191,7 @@ void* mfb_open(const char* name, int width, int height, uint32_t flags, int scal
if (!window)
return 0;
window->draw_buffer = malloc(width * height * 4);
window->draw_buffer = malloc((width * height * 4) * 8);
if (!window->draw_buffer)
return 0;
@ -199,7 +199,6 @@ void* mfb_open(const char* name, int width, int height, uint32_t flags, int scal
// Setup command queue
g_command_queue = [g_metal_device newCommandQueue];
WindowViewController* viewController = [WindowViewController new];
MTLTextureDescriptor* textureDescriptor = [[MTLTextureDescriptor alloc] init];
@ -223,6 +222,7 @@ void* mfb_open(const char* name, int width, int height, uint32_t flags, int scal
viewController->m_draw_buffer = window->draw_buffer;
viewController->m_width = width;
viewController->m_height = height;
viewController->m_delayed_delete_count = 0;
MTKView* view = [[MTKView alloc] initWithFrame:rectangle];
view.device = g_metal_device;
@ -230,6 +230,9 @@ void* mfb_open(const char* name, int width, int height, uint32_t flags, int scal
view.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
[window.contentView addSubview:view];
OSXWindowFrameView* temp_view = window->frame_view;
temp_view->m_view_controller = viewController;
window->width = width;
window->height = height;
window->scale = scale;
@ -451,7 +454,13 @@ int mfb_update(void* window, void* buffer)
int mfb_update_with_buffer(void* window, void* buffer)
{
OSXWindow* win = (OSXWindow*)window;
if (win->shared_data) {
SharedData* shared_data = (SharedData*)win->shared_data;
memcpy(win->draw_buffer, buffer, shared_data->width * shared_data->height * 4);
} else {
memcpy(win->draw_buffer, buffer, win->width * win->height * 4);
}
int state = generic_update(win);

View file

@ -53,6 +53,7 @@ void build_submenu(NSMenu* menu, MenuDesc* desc);
@public int active_menu_id;
@public int prev_cursor;
@public MenuData* menu_data;
@public void* frame_view;
}

View file

@ -144,12 +144,19 @@
object:self];
frameView = [[[OSXWindowFrameView alloc] initWithFrame:bounds] autorelease];
[super setContentView:frameView];
}
frame_view = frameView;
if (childContentView)
[childContentView removeFromSuperview];
printf("osxwindow: setContentFrameView %p\n", frameView);
//printf("osxwindow: setting controller %p\n", view_controller);
//frameView->m_view_controller = view_controller;
//NSRect t = [self contentRectForFrameRect:bounds];
childContentView = aView;

View file

@ -9,10 +9,12 @@ const int MaxBuffersInFlight = 3;
@interface WindowViewController : NSViewController<MTKViewDelegate>
{
@public id<MTLTexture> m_texture_buffers[MaxBuffersInFlight];
@public id<MTLTexture> m_delayed_delete_textures[MaxBuffersInFlight];
@public int m_current_buffer;
@public void* m_draw_buffer;
@public int m_width;
@public int m_height;
@public int m_delayed_delete_count;
// Used for syncing with CPU/GPU
@public dispatch_semaphore_t m_semaphore;
}
@ -23,6 +25,7 @@ const int MaxBuffersInFlight = 3;
@interface OSXWindowFrameView : NSView
{
@public WindowViewController* m_view_controller;
//@public int scale;
//@public int width;
//@public int height;

View file

@ -178,6 +178,51 @@ id<MTLRenderPipelineState> g_pipeline_state;
object:[self window]];
}
- (void)viewDidEndLiveResize
{
//NSRect originalFrame = [win frame];
//NSRect contentRect = [NSWindow contentRectForFrameRect: originalFrame styleMask: NSWindowStyleMaskTitled];
NSSize size = [self bounds].size;
//NSSize size = [[self contentView] frame].size;
OSXWindow* window = (OSXWindow*)[self window];
int width = (int)size.width;
int height = (int)size.height;
// if windows is resized we need to create new textures so we do that here and put the old textures in a
// "to delete" queue and set a frame counter of 3 frames before the gets released
if (window->shared_data) {
if ((width != window->shared_data->width) ||
(height != window->shared_data->height)) {
MTLTextureDescriptor* textureDescriptor = [[MTLTextureDescriptor alloc] init];
// Indicate that each pixel has a blue, green, red, and alpha channel, where each channel is
// an 8-bit unsigned normalized value (i.e. 0 maps to 0.0 and 255 maps to 1.0)
textureDescriptor.pixelFormat = MTLPixelFormatBGRA8Unorm;
// Set the pixel dimensions of the texture
textureDescriptor.width = width;
textureDescriptor.height = height;
// Create the texture from the device by using the descriptor
m_view_controller->m_width = width;
m_view_controller->m_height = height;
for (int i = 0; i < MaxBuffersInFlight; ++i) {
m_view_controller->m_delayed_delete_count = 3;
m_view_controller->m_delayed_delete_textures[i] = m_view_controller->m_texture_buffers[i];
m_view_controller->m_texture_buffers[i] = [g_metal_device newTextureWithDescriptor:textureDescriptor];
}
}
window->shared_data->width = width;
window->shared_data->height = height;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (void)dealloc
@ -190,14 +235,6 @@ id<MTLRenderPipelineState> g_pipeline_state;
- (void)windowResized:(NSNotification *)notification
{
(void)notification;
NSSize size = [self bounds].size;
OSXWindow* window = (OSXWindow*)[self window];
if (window->shared_data) {
window->shared_data->width = (int)(size.width);
window->shared_data->height = (int)(size.height);
}
}
@end