(cocoa_gl_ctx.m) Style nits

This commit is contained in:
twinaphex 2020-09-15 11:28:04 +02:00
parent c436ac83cd
commit 26b2295759
1 changed files with 112 additions and 122 deletions

View File

@ -52,9 +52,6 @@
typedef struct cocoa_ctx_data typedef struct cocoa_ctx_data
{ {
bool is_syncing;
bool core_hw_context_enable;
bool use_hw_ctx;
#ifdef HAVE_VULKAN #ifdef HAVE_VULKAN
gfx_ctx_vulkan_data_t vk; gfx_ctx_vulkan_data_t vk;
int swap_interval; int swap_interval;
@ -62,6 +59,9 @@ typedef struct cocoa_ctx_data
int fast_forward_skips; int fast_forward_skips;
unsigned width; unsigned width;
unsigned height; unsigned height;
bool is_syncing;
bool core_hw_context_enable;
bool use_hw_ctx;
} cocoa_ctx_data_t; } cocoa_ctx_data_t;
/* TODO/FIXME - static globals */ /* TODO/FIXME - static globals */
@ -97,21 +97,18 @@ static CocoaView* g_instance;
void *nsview_get_ptr(void) void *nsview_get_ptr(void)
{ {
#if defined(HAVE_COCOA) #if defined(HAVE_COCOA)
video_driver_display_type_set(RARCH_DISPLAY_OSX); video_driver_display_type_set(RARCH_DISPLAY_OSX);
video_driver_display_set(0); video_driver_display_set(0);
video_driver_display_userdata_set((uintptr_t)g_instance); video_driver_display_userdata_set((uintptr_t)g_instance);
#elif defined(HAVE_COCOA_METAL) && !defined(HAVE_COCOATOUCH) #elif defined(HAVE_COCOA_METAL) && !defined(HAVE_COCOATOUCH)
video_driver_display_type_set(RARCH_DISPLAY_OSX); video_driver_display_type_set(RARCH_DISPLAY_OSX);
video_driver_display_set(0); video_driver_display_set(0);
video_driver_display_userdata_set((uintptr_t)g_instance); video_driver_display_userdata_set((uintptr_t)g_instance);
#endif #endif
return (BRIDGE void *)g_instance; return (BRIDGE void *)g_instance;
} }
void nsview_set_ptr(CocoaView *p) void nsview_set_ptr(CocoaView *p) { g_instance = p; }
{
g_instance = p;
}
#if TARGET_OS_OSX #if TARGET_OS_OSX
static SEL sel_flushBuffer; static SEL sel_flushBuffer;
@ -168,7 +165,6 @@ static uint32_t cocoagl_gfx_ctx_get_flags(void *data)
static void cocoagl_gfx_ctx_set_flags(void *data, uint32_t flags) static void cocoagl_gfx_ctx_set_flags(void *data, uint32_t flags)
{ {
(void)flags;
cocoa_ctx_data_t *cocoa_ctx = (cocoa_ctx_data_t*)data; cocoa_ctx_data_t *cocoa_ctx = (cocoa_ctx_data_t*)data;
if (BIT32_GET(flags, GFX_CTX_FLAGS_GL_CORE_CONTEXT)) if (BIT32_GET(flags, GFX_CTX_FLAGS_GL_CORE_CONTEXT))
@ -186,7 +182,7 @@ void *glkitview_init(void)
return (BRIDGE void *)((GLKView*)g_view); return (BRIDGE void *)((GLKView*)g_view);
#else #else
return nsview_get_ptr(); return nsview_get_ptr();
#endif #endif
} }
@ -198,7 +194,8 @@ void cocoagl_bind_game_view_fbo(void)
} }
#endif #endif
static float get_from_selector(Class obj_class, id obj_id, SEL selector, CGFloat *ret) static float get_from_selector(
Class obj_class, id obj_id, SEL selector, CGFloat *ret)
{ {
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature: NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:
[obj_class instanceMethodSignatureForSelector:selector]]; [obj_class instanceMethodSignatureForSelector:selector]];
@ -232,15 +229,15 @@ void *get_chosen_screen(void)
float get_backing_scale_factor(void) float get_backing_scale_factor(void)
{ {
static float static float
backing_scale_def = 0.0f; backing_scale_def = 0.0f;
if (backing_scale_def != 0.0f) if (backing_scale_def != 0.0f)
return backing_scale_def; return backing_scale_def;
backing_scale_def = 1.0f; backing_scale_def = 1.0f;
#if TARGET_OS_OSX #if TARGET_OS_OSX
{ {
id nsscreen = objc_getClass("NSScreen"); id nsscreen = objc_getClass("NSScreen");
SEL selector = sel_registerName("backingScaleFactor"); SEL selector = sel_registerName("backingScaleFactor");
if (class_respondsToSelector(nsscreen, selector)) if (class_respondsToSelector(nsscreen, selector))
{ {
CGFloat ret; CGFloat ret;
@ -250,7 +247,7 @@ float get_backing_scale_factor(void)
CocoaView *g_view = g_instance; CocoaView *g_view = g_instance;
#endif #endif
backing_scale_def = (float)get_from_selector backing_scale_def = (float)get_from_selector
([[g_view window] class], [g_view window], selector, &ret); ([[g_view window] class], [g_view window], selector, &ret);
} }
} }
#endif #endif
@ -333,8 +330,6 @@ static enum gfx_ctx_api cocoagl_gfx_ctx_get_api(void *data)
static void cocoagl_gfx_ctx_show_mouse(void *data, bool state) static void cocoagl_gfx_ctx_show_mouse(void *data, bool state)
{ {
(void)data;
#if TARGET_OS_OSX #if TARGET_OS_OSX
if (state) if (state)
[NSCursor unhide]; [NSCursor unhide];
@ -383,94 +378,87 @@ static void cocoagl_gfx_ctx_update_title(void *data)
} }
#endif #endif
static bool cocoagl_gfx_ctx_get_metrics(void *data, enum display_metric_types type, static bool cocoagl_gfx_ctx_get_metrics(
float *value) void *data, enum display_metric_types type,
float *value)
{ {
RAScreen *screen = (BRIDGE RAScreen*)get_chosen_screen(); RAScreen *screen = (BRIDGE RAScreen*)get_chosen_screen();
#if TARGET_OS_OSX #if TARGET_OS_OSX
NSDictionary *description = [screen deviceDescription]; NSDictionary *description = [screen deviceDescription];
NSSize display_pixel_size = [[description objectForKey:NSDeviceSize] sizeValue]; NSSize display_pixel_size = [[description objectForKey:NSDeviceSize] sizeValue];
CGSize display_physical_size = CGDisplayScreenSize( CGSize display_physical_size = CGDisplayScreenSize(
[[description objectForKey:@"NSScreenNumber"] unsignedIntValue]); [[description objectForKey:@"NSScreenNumber"] unsignedIntValue]);
float display_width = display_pixel_size.width; float display_width = display_pixel_size.width;
float display_height = display_pixel_size.height; float display_height = display_pixel_size.height;
float physical_width = display_physical_size.width; float physical_width = display_physical_size.width;
float physical_height = display_physical_size.height; float physical_height = display_physical_size.height;
float scale = get_backing_scale_factor(); float scale = get_backing_scale_factor();
float dpi = (display_width/ physical_width) * 25.4f * scale; float dpi = (display_width/ physical_width) * 25.4f * scale;
#elif defined(HAVE_COCOATOUCH) #elif defined(HAVE_COCOATOUCH)
float scale = cocoagl_gfx_ctx_get_native_scale(); float scale = cocoagl_gfx_ctx_get_native_scale();
CGRect screen_rect = [screen bounds]; CGRect screen_rect = [screen bounds];
float display_height = screen_rect.size.height; float display_height = screen_rect.size.height;
float physical_width = screen_rect.size.width * scale; float physical_width = screen_rect.size.width * scale;
float physical_height = screen_rect.size.height * scale; float physical_height = screen_rect.size.height * scale;
float dpi = 160 * scale; float dpi = 160 * scale;
CGFloat maxSize = fmaxf(physical_width, physical_height); CGFloat maxSize = fmaxf(physical_width, physical_height);
NSInteger idiom_type = UI_USER_INTERFACE_IDIOM(); NSInteger idiom_type = UI_USER_INTERFACE_IDIOM();
switch (idiom_type) switch (idiom_type)
{ {
case -1: /* UIUserInterfaceIdiomUnspecified */ case -1: /* UIUserInterfaceIdiomUnspecified */
/* TODO */ /* TODO */
break; break;
case UIUserInterfaceIdiomPad: case UIUserInterfaceIdiomPad:
dpi = 132 * scale; dpi = 132 * scale;
break; break;
case UIUserInterfaceIdiomPhone: case UIUserInterfaceIdiomPhone:
if (maxSize >= 2208.0) { /* Larger iPhones: iPhone Plus, X, XR, XS, XS Max, 11, 11 Pro Max */
// Larger iPhones: iPhone Plus, X, XR, XS, XS Max, 11, 11 Pro Max if (maxSize >= 2208.0)
dpi = 81 * scale; dpi = 81 * scale;
} else { else
dpi = 163 * scale; dpi = 163 * scale;
} break;
break; case UIUserInterfaceIdiomTV:
case UIUserInterfaceIdiomTV: case UIUserInterfaceIdiomCarPlay:
case UIUserInterfaceIdiomCarPlay: /* TODO */
/* TODO */ break;
break; }
}
#endif #endif
(void)display_height; (void)display_height;
switch (type) switch (type)
{ {
case DISPLAY_METRIC_MM_WIDTH: case DISPLAY_METRIC_MM_WIDTH:
*value = physical_width; *value = physical_width;
break; break;
case DISPLAY_METRIC_MM_HEIGHT: case DISPLAY_METRIC_MM_HEIGHT:
*value = physical_height; *value = physical_height;
break; break;
case DISPLAY_METRIC_DPI: case DISPLAY_METRIC_DPI:
*value = dpi; *value = dpi;
break; break;
case DISPLAY_METRIC_NONE: case DISPLAY_METRIC_NONE:
default: default:
*value = 0; *value = 0;
return false; return false;
} }
return true; return true;
} }
static bool cocoagl_gfx_ctx_has_focus(void *data) static bool cocoagl_gfx_ctx_has_focus(void *data)
{ {
(void)data;
#if defined(HAVE_COCOATOUCH) #if defined(HAVE_COCOATOUCH)
return ([[UIApplication sharedApplication] applicationState] == UIApplicationStateActive); return ([[UIApplication sharedApplication] applicationState] == UIApplicationStateActive);
#else #else
return [NSApp isActive]; return [NSApp isActive];
#endif #endif
} }
static bool cocoagl_gfx_ctx_suppress_screensaver(void *data, bool enable) static bool cocoagl_gfx_ctx_suppress_screensaver(void *data, bool enable) { return false; }
{
(void)data;
(void)enable;
return false;
}
static void cocoagl_gfx_ctx_input_driver(void *data, static void cocoagl_gfx_ctx_input_driver(void *data,
const char *name, const char *name,
@ -480,7 +468,8 @@ static void cocoagl_gfx_ctx_input_driver(void *data,
*input_data = NULL; *input_data = NULL;
} }
static void cocoagl_gfx_ctx_get_video_size(void *data, unsigned* width, unsigned* height) static void cocoagl_gfx_ctx_get_video_size(void *data,
unsigned* width, unsigned* height)
{ {
float screenscale = cocoagl_gfx_ctx_get_native_scale(); float screenscale = cocoagl_gfx_ctx_get_native_scale();
#if TARGET_OS_OSX #if TARGET_OS_OSX
@ -555,7 +544,7 @@ static void cocoagl_gfx_ctx_check_window(void *data, bool *quit,
cocoa_ctx_data_t *cocoa_ctx = (cocoa_ctx_data_t*)data; cocoa_ctx_data_t *cocoa_ctx = (cocoa_ctx_data_t*)data;
#endif #endif
*quit = false; *quit = false;
switch (cocoagl_api) switch (cocoagl_api)
{ {
@ -564,7 +553,7 @@ static void cocoagl_gfx_ctx_check_window(void *data, bool *quit,
break; break;
case GFX_CTX_VULKAN_API: case GFX_CTX_VULKAN_API:
#ifdef HAVE_VULKAN #ifdef HAVE_VULKAN
*resize = cocoa_ctx->vk.need_new_swapchain; *resize = cocoa_ctx->vk.need_new_swapchain;
#endif #endif
break; break;
case GFX_CTX_NONE: case GFX_CTX_NONE:
@ -591,8 +580,9 @@ static void cocoagl_gfx_ctx_swap_interval(void *data, int i)
case GFX_CTX_OPENGL_API: case GFX_CTX_OPENGL_API:
case GFX_CTX_OPENGL_ES_API: case GFX_CTX_OPENGL_ES_API:
{ {
#if defined(HAVE_COCOATOUCH) // < No way to disable Vsync on iOS? #if defined(HAVE_COCOATOUCH)
// Just skip presents so fast forward still works. /* < No way to disable Vsync on iOS? */
/* Just skip presents so fast forward still works. */
cocoa_ctx->is_syncing = interval ? true : false; cocoa_ctx->is_syncing = interval ? true : false;
cocoa_ctx->fast_forward_skips = interval ? 0 : 3; cocoa_ctx->fast_forward_skips = interval ? 0 : 3;
#elif defined(HAVE_COCOA) || defined(HAVE_COCOA_METAL) #elif defined(HAVE_COCOA) || defined(HAVE_COCOA_METAL)
@ -660,7 +650,6 @@ static void cocoagl_gfx_ctx_swap_buffers(void *data)
static bool cocoagl_gfx_ctx_bind_api(void *data, enum gfx_ctx_api api, static bool cocoagl_gfx_ctx_bind_api(void *data, enum gfx_ctx_api api,
unsigned major, unsigned minor) unsigned major, unsigned minor)
{ {
(void)data;
switch (api) switch (api)
{ {
#if defined(HAVE_COCOATOUCH) #if defined(HAVE_COCOATOUCH)
@ -704,6 +693,8 @@ static bool cocoagl_gfx_ctx_set_video_mode(void *data,
#endif #endif
cocoa_ctx_data_t *cocoa_ctx = (cocoa_ctx_data_t*)data; cocoa_ctx_data_t *cocoa_ctx = (cocoa_ctx_data_t*)data;
#if TARGET_OS_OSX #if TARGET_OS_OSX
static bool
has_went_fullscreen = false;
cocoa_ctx->width = width; cocoa_ctx->width = width;
cocoa_ctx->height = height; cocoa_ctx->height = height;
#endif #endif
@ -723,7 +714,7 @@ static bool cocoagl_gfx_ctx_set_video_mode(void *data,
NSOpenGLPFADoubleBuffer, NSOpenGLPFADoubleBuffer,
NSOpenGLPFAAllowOfflineRenderers, NSOpenGLPFAAllowOfflineRenderers,
NSOpenGLPFADepthSize, NSOpenGLPFADepthSize,
(NSOpenGLPixelFormatAttribute)16, // 16 bit depth buffer (NSOpenGLPixelFormatAttribute)16, /* 16 bit depth buffer */
0, /* profile */ 0, /* profile */
0, /* profile enum */ 0, /* profile enum */
(NSOpenGLPixelFormatAttribute)0 (NSOpenGLPixelFormatAttribute)0
@ -753,18 +744,18 @@ static bool cocoagl_gfx_ctx_set_video_mode(void *data,
/* NSOpenGLFPAAllowOfflineRenderers is /* NSOpenGLFPAAllowOfflineRenderers is
not supported on this OS version. */ not supported on this OS version. */
attributes[3] = (NSOpenGLPixelFormatAttribute)0; attributes[3] = (NSOpenGLPixelFormatAttribute)0;
g_format = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes]; g_format = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes];
} }
#endif #endif
if (cocoa_ctx->use_hw_ctx) if (cocoa_ctx->use_hw_ctx)
g_hw_ctx = [[NSOpenGLContext alloc] initWithFormat:g_format shareContext:nil]; g_hw_ctx = [[NSOpenGLContext alloc] initWithFormat:g_format shareContext:nil];
g_context = [[NSOpenGLContext alloc] initWithFormat:g_format shareContext:(cocoa_ctx->use_hw_ctx) ? g_hw_ctx : nil]; g_context = [[NSOpenGLContext alloc] initWithFormat:g_format shareContext:(cocoa_ctx->use_hw_ctx) ? g_hw_ctx : nil];
[g_context setView:g_view]; [g_context setView:g_view];
#else #else
if (cocoa_ctx->use_hw_ctx) if (cocoa_ctx->use_hw_ctx)
g_hw_ctx = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; g_hw_ctx = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
g_context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; g_context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
g_view.context = g_context; g_view.context = g_context;
#endif #endif
@ -790,9 +781,7 @@ static bool cocoagl_gfx_ctx_set_video_mode(void *data,
} }
#if TARGET_OS_OSX #if TARGET_OS_OSX
static bool has_went_fullscreen = false;
/* TODO: Screen mode support. */ /* TODO: Screen mode support. */
if (fullscreen) if (fullscreen)
{ {
if (!has_went_fullscreen) if (!has_went_fullscreen)
@ -820,8 +809,8 @@ static bool cocoagl_gfx_ctx_set_video_mode(void *data,
has_went_fullscreen = fullscreen; has_went_fullscreen = fullscreen;
#endif #endif
/* TODO: Maybe iOS users should be able to show/hide the status bar here? */ /* TODO: Maybe iOS users should be able to
* show/hide the status bar here? */
return true; return true;
} }
@ -840,11 +829,14 @@ static void *cocoagl_gfx_ctx_init(void *video_driver)
#if defined(HAVE_COCOATOUCH) #if defined(HAVE_COCOATOUCH)
case GFX_CTX_OPENGL_ES_API: case GFX_CTX_OPENGL_ES_API:
#if defined(HAVE_COCOA_METAL) #if defined(HAVE_COCOA_METAL)
// the metal build supports both the OpenGL and Metal video drivers /* the metal build supports both the OpenGL
[apple_platform setViewType:APPLE_VIEW_TYPE_OPENGL_ES]; * and Metal video drivers */
[apple_platform setViewType:APPLE_VIEW_TYPE_OPENGL_ES];
#endif
/* setViewType is not (yet?) defined for iOS */
#if 0
[apple_platform setViewType:APPLE_VIEW_TYPE_OPENGL_ES];
#endif #endif
// setViewType is not (yet?) defined for iOS
// [apple_platform setViewType:APPLE_VIEW_TYPE_OPENGL_ES];
break; break;
#elif defined(HAVE_COCOA_METAL) #elif defined(HAVE_COCOA_METAL)
case GFX_CTX_OPENGL_API: case GFX_CTX_OPENGL_API:
@ -891,20 +883,18 @@ static bool cocoagl_gfx_ctx_set_resize(void *data, unsigned width, unsigned heig
cocoa_ctx->width = width; cocoa_ctx->width = width;
cocoa_ctx->height = height; cocoa_ctx->height = height;
if (vulkan_create_swapchain(&cocoa_ctx->vk, if (!vulkan_create_swapchain(&cocoa_ctx->vk,
width, height, cocoa_ctx->swap_interval)) width, height, cocoa_ctx->swap_interval))
{
cocoa_ctx->vk.context.invalid_swapchain = true;
if (cocoa_ctx->vk.created_new_swapchain)
vulkan_acquire_next_image(&cocoa_ctx->vk);
}
else
{ {
RARCH_ERR("[macOS/Vulkan]: Failed to update swapchain.\n"); RARCH_ERR("[macOS/Vulkan]: Failed to update swapchain.\n");
return false; return false;
} }
cocoa_ctx->vk.need_new_swapchain = false; cocoa_ctx->vk.context.invalid_swapchain = true;
if (cocoa_ctx->vk.created_new_swapchain)
vulkan_acquire_next_image(&cocoa_ctx->vk);
cocoa_ctx->vk.need_new_swapchain = false;
#endif #endif
break; break;
case GFX_CTX_NONE: case GFX_CTX_NONE: