(COcoa GL/Cocoa Common) Refactor code for eventual splitting up

of cocoa_gl_ctx.m into two files
This commit is contained in:
Twinaphex 2021-01-18 15:41:30 +01:00
parent 4036cc42e0
commit eea07ea367
6 changed files with 209 additions and 189 deletions

View file

@ -237,8 +237,8 @@ typedef struct gl2_renderchain_data
/* Prototypes */ /* Prototypes */
#ifdef IOS #ifdef IOS
/* There is no default frame buffer on iOS. */ /* There is no default frame buffer on iOS. */
void cocoagl_bind_game_view_fbo(void); void glkitview_bind_fbo(void);
#define gl2_renderchain_bind_backbuffer() cocoagl_bind_game_view_fbo() #define gl2_renderchain_bind_backbuffer() glkitview_bind_fbo()
#else #else
#define gl2_renderchain_bind_backbuffer() gl2_bind_fb(0) #define gl2_renderchain_bind_backbuffer() gl2_bind_fb(0)
#endif #endif

View file

@ -87,7 +87,7 @@ static NSOpenGLPixelFormat* g_format;
#endif #endif
static CocoaView* g_instance; static CocoaView* g_instance;
#if defined(HAVE_COCOATOUCH) #if defined(HAVE_COCOATOUCH)
static GLKView *g_view = NULL; static GLKView *glk_view = NULL;
@interface EAGLContext (OSXCompat) @end @interface EAGLContext (OSXCompat) @end
@implementation EAGLContext (OSXCompat) @implementation EAGLContext (OSXCompat)
@ -118,7 +118,19 @@ void *nsview_get_ptr(void)
void nsview_set_ptr(CocoaView *p) { g_instance = p; } void nsview_set_ptr(CocoaView *p) { g_instance = p; }
static uint32_t cocoagl_gfx_ctx_get_flags(void *data) CocoaView *cocoaview_get(void)
{
#if defined(HAVE_COCOA_METAL)
return (CocoaView*)apple_platform.renderView;
#elif defined(HAVE_COCOA)
return g_instance;
#else
/* TODO/FIXME - implement */
return NULL;
#endif
}
static uint32_t cocoa_gl_gfx_ctx_get_flags(void *data)
{ {
uint32_t flags = 0; uint32_t flags = 0;
cocoa_ctx_data_t *cocoa_ctx = (cocoa_ctx_data_t*)data; cocoa_ctx_data_t *cocoa_ctx = (cocoa_ctx_data_t*)data;
@ -160,7 +172,7 @@ static uint32_t cocoagl_gfx_ctx_get_flags(void *data)
return flags; return flags;
} }
static void cocoagl_gfx_ctx_set_flags(void *data, uint32_t flags) static void cocoa_gl_gfx_ctx_set_flags(void *data, uint32_t flags)
{ {
cocoa_ctx_data_t *cocoa_ctx = (cocoa_ctx_data_t*)data; cocoa_ctx_data_t *cocoa_ctx = (cocoa_ctx_data_t*)data;
@ -172,62 +184,24 @@ static void cocoagl_gfx_ctx_set_flags(void *data, uint32_t flags)
#if defined(HAVE_COCOATOUCH) #if defined(HAVE_COCOATOUCH)
void *glkitview_init(void) void *glkitview_init(void)
{ {
g_view = [GLKView new]; glk_view = [GLKView new];
#if TARGET_OS_IOS #if TARGET_OS_IOS
g_view.multipleTouchEnabled = YES; glk_view.multipleTouchEnabled = YES;
#endif #endif
g_view.enableSetNeedsDisplay = NO; glk_view.enableSetNeedsDisplay = NO;
return (BRIDGE void *)((GLKView*)g_view); return (BRIDGE void *)((GLKView*)glk_view);
} }
void cocoagl_bind_game_view_fbo(void) void glkitview_bind_fbo(void)
{ {
if (g_context) if (g_context)
[g_view bindDrawable]; [glk_view bindDrawable];
} }
#endif #endif
#endif #endif
void *get_chosen_screen(void) void cocoa_gl_gfx_ctx_update(void)
{
unsigned monitor_index;
settings_t *settings = config_get_ptr();
NSArray *screens = [RAScreen screens];
if (!screens || !settings)
return NULL;
monitor_index = settings->uints.video_monitor_index;
if (monitor_index >= screens.count)
{
RARCH_WARN("video_monitor_index is greater than the number of connected monitors; using main screen instead.");
return (BRIDGE void*)screens;
}
return ((BRIDGE void*)[screens objectAtIndex:monitor_index]);
}
#if MAC_OS_X_VERSION_10_7 && defined(OSX)
/* NOTE: backingScaleFactor only available on MacOS X 10.7 and up. */
float get_backing_scale_factor(void)
{
static float
backing_scale_def = 0.0f;
if (backing_scale_def == 0.0f)
{
RAScreen *screen = (BRIDGE RAScreen*)get_chosen_screen();
if (!screen)
return 1.0f;
backing_scale_def = [screen backingScaleFactor];
}
return backing_scale_def;
}
#else
float get_backing_scale_factor(void) { return 1.0f; }
#endif
void cocoagl_gfx_ctx_update(void)
{ {
switch (cocoagl_api) switch (cocoagl_api)
{ {
@ -244,7 +218,7 @@ void cocoagl_gfx_ctx_update(void)
} }
} }
static void cocoagl_gfx_ctx_destroy(void *data) static void cocoa_gl_gfx_ctx_destroy(void *data)
{ {
cocoa_ctx_data_t *cocoa_ctx = (cocoa_ctx_data_t*)data; cocoa_ctx_data_t *cocoa_ctx = (cocoa_ctx_data_t*)data;
@ -286,18 +260,10 @@ static void cocoagl_gfx_ctx_destroy(void *data)
free(cocoa_ctx); free(cocoa_ctx);
} }
static enum gfx_ctx_api cocoagl_gfx_ctx_get_api(void *data) { return cocoagl_api; } static enum gfx_ctx_api cocoa_gl_gfx_ctx_get_api(void *data) { return cocoagl_api; }
#ifdef OSX #ifdef OSX
static void cocoagl_gfx_ctx_show_mouse(void *data, bool state) static void cocoa_gl_gfx_ctx_update_title(void *data)
{
if (state)
[NSCursor unhide];
else
[NSCursor hide];
}
static void cocoagl_gfx_ctx_update_title(void *data)
{ {
const ui_window_t *window = ui_companion_driver_get_window_ptr(); const ui_window_t *window = ui_companion_driver_get_window_ptr();
@ -314,11 +280,11 @@ static void cocoagl_gfx_ctx_update_title(void *data)
} }
} }
static bool cocoagl_gfx_ctx_get_metrics( static bool cocoa_gl_gfx_ctx_get_metrics(
void *data, enum display_metric_types type, void *data, enum display_metric_types type,
float *value) float *value)
{ {
RAScreen *screen = (BRIDGE RAScreen*)get_chosen_screen(); RAScreen *screen = (BRIDGE RAScreen*)cocoa_screen_get_chosen();
NSDictionary *desc = [screen deviceDescription]; NSDictionary *desc = [screen deviceDescription];
CGSize display_physical_size = CGDisplayScreenSize( CGSize display_physical_size = CGDisplayScreenSize(
[[desc objectForKey:@"NSScreenNumber"] unsignedIntValue]); [[desc objectForKey:@"NSScreenNumber"] unsignedIntValue]);
@ -338,7 +304,7 @@ static bool cocoagl_gfx_ctx_get_metrics(
{ {
NSSize disp_pixel_size = [[desc objectForKey:NSDeviceSize] sizeValue]; NSSize disp_pixel_size = [[desc objectForKey:NSDeviceSize] sizeValue];
float dispwidth = disp_pixel_size.width; float dispwidth = disp_pixel_size.width;
float scale = get_backing_scale_factor(); float scale = cocoa_screen_get_backing_scale_factor();
float dpi = (dispwidth / physical_width) * 25.4f * scale; float dpi = (dispwidth / physical_width) * 25.4f * scale;
*value = dpi; *value = dpi;
} }
@ -352,54 +318,12 @@ static bool cocoagl_gfx_ctx_get_metrics(
return true; return true;
} }
#else #else
static float get_from_selector( static bool cocoa_gl_gfx_ctx_get_metrics(
Class obj_class, id obj_id, SEL selector, CGFloat *ret)
{
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:
[obj_class instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:obj_id];
[invocation invoke];
[invocation getReturnValue:ret];
RELEASE(invocation);
return *ret;
}
/* NOTE: nativeScale only available on iOS 8.0 and up. */
float cocoagl_gfx_ctx_get_native_scale(void)
{
SEL selector;
static CGFloat ret = 0.0f;
RAScreen *screen = NULL;
if (ret != 0.0f)
return ret;
screen = (BRIDGE RAScreen*)get_chosen_screen();
if (!screen)
return 0.0f;
selector = NSSelectorFromString(BOXSTRING("nativeScale"));
if ([screen respondsToSelector:selector])
ret = (float)get_from_selector(
[screen class], screen, selector, &ret);
else
{
ret = 1.0f;
selector = NSSelectorFromString(BOXSTRING("scale"));
if ([screen respondsToSelector:selector])
ret = screen.scale;
}
return ret;
}
static bool cocoagl_gfx_ctx_get_metrics(
void *data, enum display_metric_types type, void *data, enum display_metric_types type,
float *value) float *value)
{ {
RAScreen *screen = (BRIDGE RAScreen*)get_chosen_screen(); RAScreen *screen = (BRIDGE RAScreen*)cocoa_screen_get_chosen();
float scale = cocoagl_gfx_ctx_get_native_scale(); float scale = cocoa_screen_get_native_scale();
CGRect screen_rect = [screen bounds]; CGRect screen_rect = [screen bounds];
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;
@ -451,20 +375,9 @@ static bool cocoagl_gfx_ctx_get_metrics(
} }
#endif #endif
static bool cocoa_gl_gfx_ctx_suppress_screensaver(void *data, bool enable) { return false; }
static bool cocoagl_gfx_ctx_has_focus(void *data) static void cocoa_gl_gfx_ctx_input_driver(void *data,
{
#if defined(HAVE_COCOATOUCH)
return ([[UIApplication sharedApplication] applicationState]
== UIApplicationStateActive);
#else
return [NSApp isActive];
#endif
}
static bool cocoagl_gfx_ctx_suppress_screensaver(void *data, bool enable) { return false; }
static void cocoagl_gfx_ctx_input_driver(void *data,
const char *name, const char *name,
input_driver_t **input, void **input_data) input_driver_t **input, void **input_data)
{ {
@ -476,15 +389,10 @@ static void cocoagl_gfx_ctx_input_driver(void *data,
/* NOTE: convertRectToBacking only available on MacOS X 10.7 and up. /* NOTE: convertRectToBacking only available on MacOS X 10.7 and up.
* Therefore, make specialized version of this function instead of * Therefore, make specialized version of this function instead of
* going through a selector for every call. */ * going through a selector for every call. */
static void cocoa_gl_gfx_ctx_get_video_size_osx10_7_and_up(void *data,
static void cocoagl_gfx_ctx_get_video_size_osx10_7_and_up(void *data,
unsigned* width, unsigned* height) unsigned* width, unsigned* height)
{ {
#if defined(HAVE_COCOA_METAL) CocoaView *g_view = cocoaview_get();
NSView *g_view = apple_platform.renderView;
#elif defined(HAVE_COCOA)
CocoaView *g_view = g_instance;
#endif
CGRect cgrect = NSRectToCGRect([g_view convertRectToBacking:[g_view bounds]]); CGRect cgrect = NSRectToCGRect([g_view convertRectToBacking:[g_view bounds]]);
GLsizei backingPixelWidth = CGRectGetWidth(cgrect); GLsizei backingPixelWidth = CGRectGetWidth(cgrect);
GLsizei backingPixelHeight = CGRectGetHeight(cgrect); GLsizei backingPixelHeight = CGRectGetHeight(cgrect);
@ -493,10 +401,10 @@ static void cocoagl_gfx_ctx_get_video_size_osx10_7_and_up(void *data,
*height = CGRectGetHeight(size); *height = CGRectGetHeight(size);
} }
#elif defined(OSX) #elif defined(OSX)
static void cocoagl_gfx_ctx_get_video_size(void *data, static void cocoa_gl_gfx_ctx_get_video_size(void *data,
unsigned* width, unsigned* height) unsigned* width, unsigned* height)
{ {
CocoaView *g_view = g_instance; CocoaView *g_view = cocoaview_get();
CGRect cgrect = NSRectToCGRect([g_view frame]); CGRect cgrect = NSRectToCGRect([g_view frame]);
GLsizei backingPixelWidth = CGRectGetWidth(cgrect); GLsizei backingPixelWidth = CGRectGetWidth(cgrect);
GLsizei backingPixelHeight = CGRectGetHeight(cgrect); GLsizei backingPixelHeight = CGRectGetHeight(cgrect);
@ -506,17 +414,17 @@ static void cocoagl_gfx_ctx_get_video_size(void *data,
} }
#else #else
/* iOS */ /* iOS */
static void cocoagl_gfx_ctx_get_video_size(void *data, static void cocoa_gl_gfx_ctx_get_video_size(void *data,
unsigned* width, unsigned* height) unsigned* width, unsigned* height)
{ {
float screenscale = cocoagl_gfx_ctx_get_native_scale(); float screenscale = cocoa_screen_get_native_scale();
CGRect size = g_view.bounds; CGRect size = glk_view.bounds;
*width = CGRectGetWidth(size) * screenscale; *width = CGRectGetWidth(size) * screenscale;
*height = CGRectGetHeight(size) * screenscale; *height = CGRectGetHeight(size) * screenscale;
} }
#endif #endif
static gfx_ctx_proc_t cocoagl_gfx_ctx_get_proc_address(const char *symbol_name) static gfx_ctx_proc_t cocoa_gl_gfx_ctx_get_proc_address(const char *symbol_name)
{ {
switch (cocoagl_api) switch (cocoagl_api)
{ {
@ -534,7 +442,7 @@ static gfx_ctx_proc_t cocoagl_gfx_ctx_get_proc_address(const char *symbol_name)
return NULL; return NULL;
} }
static void cocoagl_gfx_ctx_bind_hw_render(void *data, bool enable) static void cocoa_gl_gfx_ctx_bind_hw_render(void *data, bool enable)
{ {
cocoa_ctx_data_t *cocoa_ctx = (cocoa_ctx_data_t*)data; cocoa_ctx_data_t *cocoa_ctx = (cocoa_ctx_data_t*)data;
@ -555,7 +463,7 @@ static void cocoagl_gfx_ctx_bind_hw_render(void *data, bool enable)
} }
} }
static void cocoagl_gfx_ctx_check_window(void *data, bool *quit, static void cocoa_gl_gfx_ctx_check_window(void *data, bool *quit,
bool *resize, unsigned *width, unsigned *height) bool *resize, unsigned *width, unsigned *height)
{ {
unsigned new_width, new_height; unsigned new_width, new_height;
@ -581,9 +489,9 @@ static void cocoagl_gfx_ctx_check_window(void *data, bool *quit,
} }
#if MAC_OS_X_VERSION_10_7 && defined(OSX) #if MAC_OS_X_VERSION_10_7 && defined(OSX)
cocoagl_gfx_ctx_get_video_size_osx10_7_and_up(data, &new_width, &new_height); cocoa_gl_gfx_ctx_get_video_size_osx10_7_and_up(data, &new_width, &new_height);
#else #else
cocoagl_gfx_ctx_get_video_size(data, &new_width, &new_height); cocoa_gl_gfx_ctx_get_video_size(data, &new_width, &new_height);
#endif #endif
if (new_width != *width || new_height != *height) if (new_width != *width || new_height != *height)
@ -594,7 +502,7 @@ static void cocoagl_gfx_ctx_check_window(void *data, bool *quit,
} }
} }
static void cocoagl_gfx_ctx_swap_interval(void *data, int i) static void cocoa_gl_gfx_ctx_swap_interval(void *data, int i)
{ {
unsigned interval = (unsigned)i; unsigned interval = (unsigned)i;
cocoa_ctx_data_t *cocoa_ctx = (cocoa_ctx_data_t*)data; cocoa_ctx_data_t *cocoa_ctx = (cocoa_ctx_data_t*)data;
@ -631,7 +539,7 @@ static void cocoagl_gfx_ctx_swap_interval(void *data, int i)
} }
} }
static void cocoagl_gfx_ctx_swap_buffers(void *data) static void cocoa_gl_gfx_ctx_swap_buffers(void *data)
{ {
cocoa_ctx_data_t *cocoa_ctx = (cocoa_ctx_data_t*)data; cocoa_ctx_data_t *cocoa_ctx = (cocoa_ctx_data_t*)data;
@ -645,8 +553,8 @@ static void cocoagl_gfx_ctx_swap_buffers(void *data)
#else #else
if (!(--cocoa_ctx->fast_forward_skips < 0)) if (!(--cocoa_ctx->fast_forward_skips < 0))
return; return;
if (g_view) if (glk_view)
[g_view display]; [glk_view display];
cocoa_ctx->fast_forward_skips = cocoa_ctx->is_syncing ? 0 : 3; cocoa_ctx->fast_forward_skips = cocoa_ctx->is_syncing ? 0 : 3;
#endif #endif
break; break;
@ -669,7 +577,7 @@ 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 cocoa_gl_gfx_ctx_bind_api(void *data, enum gfx_ctx_api api,
unsigned major, unsigned minor) unsigned major, unsigned minor)
{ {
switch (api) switch (api)
@ -698,7 +606,7 @@ static bool cocoagl_gfx_ctx_bind_api(void *data, enum gfx_ctx_api api,
} }
#ifdef HAVE_VULKAN #ifdef HAVE_VULKAN
static void *cocoagl_gfx_ctx_get_context_data(void *data) static void *cocoa_vk_gfx_ctx_get_context_data(void *data)
{ {
cocoa_ctx_data_t *cocoa_ctx = (cocoa_ctx_data_t*)data; cocoa_ctx_data_t *cocoa_ctx = (cocoa_ctx_data_t*)data;
return &cocoa_ctx->vk.context; return &cocoa_ctx->vk.context;
@ -706,7 +614,7 @@ static void *cocoagl_gfx_ctx_get_context_data(void *data)
#endif #endif
#ifdef OSX #ifdef OSX
static bool cocoagl_gfx_ctx_set_video_mode(void *data, static bool cocoa_gl_gfx_ctx_set_video_mode(void *data,
unsigned width, unsigned height, bool fullscreen) unsigned width, unsigned height, bool fullscreen)
{ {
#if defined(HAVE_COCOA_METAL) #if defined(HAVE_COCOA_METAL)
@ -818,8 +726,8 @@ static bool cocoagl_gfx_ctx_set_video_mode(void *data,
{ {
if (!has_went_fullscreen) if (!has_went_fullscreen)
{ {
[g_view enterFullScreenMode:(BRIDGE NSScreen *)get_chosen_screen() withOptions:nil]; [g_view enterFullScreenMode:(BRIDGE NSScreen *)cocoa_screen_get_chosen() withOptions:nil];
cocoagl_gfx_ctx_show_mouse(data, false); cocoa_show_mouse(data, false);
} }
} }
else else
@ -828,7 +736,7 @@ static bool cocoagl_gfx_ctx_set_video_mode(void *data,
{ {
[g_view exitFullScreenModeWithOptions:nil]; [g_view exitFullScreenModeWithOptions:nil];
[[g_view window] makeFirstResponder:g_view]; [[g_view window] makeFirstResponder:g_view];
cocoagl_gfx_ctx_show_mouse(data, true); cocoa_show_mouse(data, true);
} }
[[g_view window] setContentSize:NSMakeSize(width, height)]; [[g_view window] setContentSize:NSMakeSize(width, height)];
@ -839,7 +747,7 @@ static bool cocoagl_gfx_ctx_set_video_mode(void *data,
return true; return true;
} }
static void *cocoagl_gfx_ctx_init(void *video_driver) static void *cocoa_gl_gfx_ctx_init(void *video_driver)
{ {
cocoa_ctx_data_t *cocoa_ctx = (cocoa_ctx_data_t*) cocoa_ctx_data_t *cocoa_ctx = (cocoa_ctx_data_t*)
calloc(1, sizeof(cocoa_ctx_data_t)); calloc(1, sizeof(cocoa_ctx_data_t));
@ -876,7 +784,7 @@ static void *cocoagl_gfx_ctx_init(void *video_driver)
return cocoa_ctx; return cocoa_ctx;
} }
#else #else
static bool cocoagl_gfx_ctx_set_video_mode(void *data, static bool cocoa_gl_gfx_ctx_set_video_mode(void *data,
unsigned width, unsigned height, bool fullscreen) unsigned width, unsigned height, bool fullscreen)
{ {
cocoa_ctx_data_t *cocoa_ctx = (cocoa_ctx_data_t*)data; cocoa_ctx_data_t *cocoa_ctx = (cocoa_ctx_data_t*)data;
@ -886,9 +794,9 @@ static bool cocoagl_gfx_ctx_set_video_mode(void *data,
case GFX_CTX_OPENGL_API: case GFX_CTX_OPENGL_API:
case GFX_CTX_OPENGL_ES_API: case GFX_CTX_OPENGL_ES_API:
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; glk_view.context = g_context;
[g_context makeCurrentContext]; [g_context makeCurrentContext];
break; break;
@ -902,7 +810,7 @@ static bool cocoagl_gfx_ctx_set_video_mode(void *data,
return true; return true;
} }
static void *cocoagl_gfx_ctx_init(void *video_driver) static void *cocoa_gl_gfx_ctx_init(void *video_driver)
{ {
cocoa_ctx_data_t *cocoa_ctx = (cocoa_ctx_data_t*) cocoa_ctx_data_t *cocoa_ctx = (cocoa_ctx_data_t*)
calloc(1, sizeof(cocoa_ctx_data_t)); calloc(1, sizeof(cocoa_ctx_data_t));
@ -933,7 +841,7 @@ static void *cocoagl_gfx_ctx_init(void *video_driver)
#endif #endif
#ifdef HAVE_COCOA_METAL #ifdef HAVE_COCOA_METAL
static bool cocoagl_gfx_ctx_set_resize(void *data, unsigned width, unsigned height) static bool cocoa_gl_gfx_ctx_set_resize(void *data, unsigned width, unsigned height)
{ {
#ifdef HAVE_VULKAN #ifdef HAVE_VULKAN
cocoa_ctx_data_t *cocoa_ctx = (cocoa_ctx_data_t*)data; cocoa_ctx_data_t *cocoa_ctx = (cocoa_ctx_data_t*)data;
@ -973,53 +881,53 @@ static bool cocoagl_gfx_ctx_set_resize(void *data, unsigned width, unsigned heig
#endif #endif
const gfx_ctx_driver_t gfx_ctx_cocoagl = { const gfx_ctx_driver_t gfx_ctx_cocoagl = {
cocoagl_gfx_ctx_init, cocoa_gl_gfx_ctx_init,
cocoagl_gfx_ctx_destroy, cocoa_gl_gfx_ctx_destroy,
cocoagl_gfx_ctx_get_api, cocoa_gl_gfx_ctx_get_api,
cocoagl_gfx_ctx_bind_api, cocoa_gl_gfx_ctx_bind_api,
cocoagl_gfx_ctx_swap_interval, cocoa_gl_gfx_ctx_swap_interval,
cocoagl_gfx_ctx_set_video_mode, cocoa_gl_gfx_ctx_set_video_mode,
#if MAC_OS_X_VERSION_10_7 && defined(OSX) #if MAC_OS_X_VERSION_10_7 && defined(OSX)
cocoagl_gfx_ctx_get_video_size_osx10_7_and_up, cocoa_gl_gfx_ctx_get_video_size_osx10_7_and_up,
#else #else
cocoagl_gfx_ctx_get_video_size, cocoa_gl_gfx_ctx_get_video_size,
#endif #endif
NULL, /* get_refresh_rate */ NULL, /* get_refresh_rate */
NULL, /* get_video_output_size */ NULL, /* get_video_output_size */
NULL, /* get_video_output_prev */ NULL, /* get_video_output_prev */
NULL, /* get_video_output_next */ NULL, /* get_video_output_next */
cocoagl_gfx_ctx_get_metrics, cocoa_gl_gfx_ctx_get_metrics,
NULL, /* translate_aspect */ NULL, /* translate_aspect */
#ifdef OSX #ifdef OSX
cocoagl_gfx_ctx_update_title, cocoa_gl_gfx_ctx_update_title,
#else #else
NULL, /* update_title */ NULL, /* update_title */
#endif #endif
cocoagl_gfx_ctx_check_window, cocoa_gl_gfx_ctx_check_window,
#if defined(HAVE_COCOA_METAL) #if defined(HAVE_COCOA_METAL)
cocoagl_gfx_ctx_set_resize, cocoagl_gfx_ctx_set_resize,
#else #else
NULL, /* set_resize */ NULL, /* set_resize */
#endif #endif
cocoagl_gfx_ctx_has_focus, cocoa_has_focus,
cocoagl_gfx_ctx_suppress_screensaver, cocoa_gl_gfx_ctx_suppress_screensaver,
#if defined(HAVE_COCOATOUCH) #if defined(HAVE_COCOATOUCH)
false, false,
#else #else
true, true,
#endif #endif
cocoagl_gfx_ctx_swap_buffers, cocoa_gl_gfx_ctx_swap_buffers,
cocoagl_gfx_ctx_input_driver, cocoa_gl_gfx_ctx_input_driver,
cocoagl_gfx_ctx_get_proc_address, cocoa_gl_gfx_ctx_get_proc_address,
NULL, /* image_buffer_init */ NULL, /* image_buffer_init */
NULL, /* image_buffer_write */ NULL, /* image_buffer_write */
NULL, /* show_mouse */ NULL, /* show_mouse */
"cocoagl", "cocoagl",
cocoagl_gfx_ctx_get_flags, cocoa_gl_gfx_ctx_get_flags,
cocoagl_gfx_ctx_set_flags, cocoa_gl_gfx_ctx_set_flags,
cocoagl_gfx_ctx_bind_hw_render, cocoa_gl_gfx_ctx_bind_hw_render,
#if defined(HAVE_VULKAN) #if defined(HAVE_VULKAN)
cocoagl_gfx_ctx_get_context_data, cocoa_vk_gfx_ctx_get_context_data,
#else #else
NULL, /* get_context_data */ NULL, /* get_context_data */
#endif #endif

View file

@ -35,8 +35,10 @@
/* TODO/FIXME - /* TODO/FIXME -
* fix game focus toggle */ * fix game focus toggle */
/* Forward declarations */ #ifdef OSX
float get_backing_scale_factor(void); /* Forward declaration */
float cocoa_screen_get_backing_scale_factor(void);
#endif
static bool apple_key_state[MAX_KEYS]; static bool apple_key_state[MAX_KEYS];
#if TARGET_OS_IPHONE #if TARGET_OS_IPHONE
@ -338,7 +340,7 @@ static void cocoa_input_poll(void *data)
uint32_t i; uint32_t i;
cocoa_input_data_t *apple = (cocoa_input_data_t*)data; cocoa_input_data_t *apple = (cocoa_input_data_t*)data;
#ifndef IOS #ifndef IOS
float backing_scale_factor = get_backing_scale_factor(); float backing_scale_factor = cocoa_screen_get_backing_scale_factor();
#endif #endif
if (!apple) if (!apple)
@ -428,7 +430,7 @@ static int16_t cocoa_input_state(
#ifdef IOS #ifdef IOS
return apple->window_pos_x; return apple->window_pos_x;
#else #else
return apple->window_pos_x * get_backing_scale_factor(); return apple->window_pos_x * cocoa_screen_get_backing_scale_factor();
#endif #endif
} }
val = apple->window_pos_x - apple->mouse_x_last; val = apple->window_pos_x - apple->mouse_x_last;
@ -440,7 +442,7 @@ static int16_t cocoa_input_state(
#ifdef IOS #ifdef IOS
return apple->window_pos_y; return apple->window_pos_y;
#else #else
return apple->window_pos_y * get_backing_scale_factor(); return apple->window_pos_y * cocoa_screen_get_backing_scale_factor();
#endif #endif
} }
val = apple->window_pos_y - apple->mouse_y_last; val = apple->window_pos_y - apple->mouse_y_last;

View file

@ -107,6 +107,16 @@ void *nsview_get_ptr(void);
void nsview_set_ptr(CocoaView *ptr); void nsview_set_ptr(CocoaView *ptr);
void *get_chosen_screen(void); bool cocoa_has_focus(void *data);
void cocoa_show_mouse(void *data, bool state);
void *cocoa_screen_get_chosen(void);
#ifdef OSX
float cocoa_screen_get_backing_scale_factor(void);
#else
float cocoa_screen_get_native_scale(void);
#endif
#endif #endif

View file

@ -30,7 +30,7 @@
#endif #endif
/* forward declarations */ /* forward declarations */
void cocoagl_gfx_ctx_update(void); void cocoa_gl_gfx_ctx_update(void);
#ifdef HAVE_COCOATOUCH #ifdef HAVE_COCOATOUCH
void *glkitview_init(void); void *glkitview_init(void);
@ -101,7 +101,7 @@ void *glkitview_init(void);
{ {
[super setFrame:frameRect]; [super setFrame:frameRect];
cocoagl_gfx_ctx_update(); cocoa_gl_gfx_ctx_update();
} }
/* Stop the annoying sound when pressing a key. */ /* Stop the annoying sound when pressing a key. */
@ -168,7 +168,7 @@ void *glkitview_init(void);
* the notch in iPhone X phones */ * the notch in iPhone X phones */
if (@available(iOS 11, *)) if (@available(iOS 11, *))
{ {
RAScreen *screen = (BRIDGE RAScreen*)get_chosen_screen(); RAScreen *screen = (BRIDGE RAScreen*)cocoa_screen_get_chosen();
CGRect screenSize = [screen bounds]; CGRect screenSize = [screen bounds];
UIEdgeInsets inset = [[UIApplication sharedApplication] delegate].window.safeAreaInsets; UIEdgeInsets inset = [[UIApplication sharedApplication] delegate].window.safeAreaInsets;
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
@ -202,7 +202,7 @@ void *glkitview_init(void);
- (void)viewWillLayoutSubviews - (void)viewWillLayoutSubviews
{ {
float width = 0.0f, height = 0.0f; float width = 0.0f, height = 0.0f;
RAScreen *screen = (BRIDGE RAScreen*)get_chosen_screen(); RAScreen *screen = (BRIDGE RAScreen*)cocoa_screen_get_chosen();
UIInterfaceOrientation orientation = self.interfaceOrientation; UIInterfaceOrientation orientation = self.interfaceOrientation;
CGRect screenSize = [screen bounds]; CGRect screenSize = [screen bounds];
SEL selector = NSSelectorFromString(BOXSTRING("coordinateSpace")); SEL selector = NSSelectorFromString(BOXSTRING("coordinateSpace"));
@ -309,3 +309,105 @@ void *glkitview_init(void);
#endif #endif
@end @end
void *cocoa_screen_get_chosen(void)
{
unsigned monitor_index;
settings_t *settings = config_get_ptr();
NSArray *screens = [RAScreen screens];
if (!screens || !settings)
return NULL;
monitor_index = settings->uints.video_monitor_index;
if (monitor_index >= screens.count)
{
RARCH_WARN("video_monitor_index is greater than the number of connected monitors; using main screen instead.");
return (BRIDGE void*)screens;
}
return ((BRIDGE void*)[screens objectAtIndex:monitor_index]);
}
bool cocoa_has_focus(void *data)
{
#if defined(HAVE_COCOATOUCH)
return ([[UIApplication sharedApplication] applicationState]
== UIApplicationStateActive);
#else
return [NSApp isActive];
#endif
}
void cocoa_show_mouse(void *data, bool state)
{
#ifdef OSX
if (state)
[NSCursor unhide];
else
[NSCursor hide];
#endif
}
#ifdef OSX
#if MAC_OS_X_VERSION_10_7
/* NOTE: backingScaleFactor only available on MacOS X 10.7 and up. */
float cocoa_screen_get_backing_scale_factor(void)
{
static float
backing_scale_def = 0.0f;
if (backing_scale_def == 0.0f)
{
RAScreen *screen = (BRIDGE RAScreen*)cocoa_screen_get_chosen();
if (!screen)
return 1.0f;
backing_scale_def = [screen backingScaleFactor];
}
return backing_scale_def;
}
#else
float cocoa_screen_get_backing_scale_factor(void) { return 1.0f; }
#endif
#else
static float get_from_selector(
Class obj_class, id obj_id, SEL selector, CGFloat *ret)
{
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:
[obj_class instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:obj_id];
[invocation invoke];
[invocation getReturnValue:ret];
RELEASE(invocation);
return *ret;
}
/* NOTE: nativeScale only available on iOS 8.0 and up. */
float cocoa_screen_get_native_scale(void)
{
SEL selector;
static CGFloat ret = 0.0f;
RAScreen *screen = NULL;
if (ret != 0.0f)
return ret;
screen = (BRIDGE RAScreen*)cocoa_screen_get_chosen();
if (!screen)
return 0.0f;
selector = NSSelectorFromString(BOXSTRING("nativeScale"));
if ([screen respondsToSelector:selector])
ret = (float)get_from_selector(
[screen class], screen, selector, &ret);
else
{
ret = 1.0f;
selector = NSSelectorFromString(BOXSTRING("scale"));
if ([screen respondsToSelector:selector])
ret = screen.scale;
}
return ret;
}
#endif

View file

@ -107,14 +107,12 @@ void get_ios_version(int *major, int *minor)
*minor = (int)[decomposed_os_version[1] integerValue]; *minor = (int)[decomposed_os_version[1] integerValue];
} }
extern float cocoagl_gfx_ctx_get_native_scale(void);
/* Input helpers: This is kept here because it needs ObjC */ /* Input helpers: This is kept here because it needs ObjC */
static void handle_touch_event(NSArray* touches) static void handle_touch_event(NSArray* touches)
{ {
unsigned i; unsigned i;
cocoa_input_data_t *apple = (cocoa_input_data_t*)input_driver_get_data(); cocoa_input_data_t *apple = (cocoa_input_data_t*)input_driver_get_data();
float scale = cocoagl_gfx_ctx_get_native_scale(); float scale = cocoa_screen_get_native_scale();
if (!apple) if (!apple)
return; return;