mirror of
https://github.com/libretro/RetroArch
synced 2025-01-31 15:32:59 +00:00
546 lines
14 KiB
Objective-C
546 lines
14 KiB
Objective-C
/* RetroArch - A frontend for libretro.
|
|
* Copyright (C) 2018 - Stuart Carnie
|
|
*
|
|
* RetroArch is free software: you can redistribute it and/or modify it under the terms
|
|
* of the GNU General Public License as published by the Free Software Found-
|
|
* ation, either version 3 of the License, or (at your option) any later version.
|
|
*
|
|
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
* PURPOSE. See the GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License along with RetroArch.
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
#include "../../config.h"
|
|
#endif
|
|
|
|
#if TARGET_OS_IPHONE
|
|
#include <CoreGraphics/CoreGraphics.h>
|
|
#else
|
|
#include <ApplicationServices/ApplicationServices.h>
|
|
#endif
|
|
#if defined(HAVE_COCOA_METAL)
|
|
#include <OpenGL/CGLTypes.h>
|
|
#include <OpenGL/OpenGL.h>
|
|
#include <AppKit/NSScreen.h>
|
|
#include <AppKit/NSOpenGL.h>
|
|
#elif defined(HAVE_COCOATOUCH)
|
|
#include <GLKit/GLKit.h>
|
|
#endif
|
|
|
|
#include <retro_assert.h>
|
|
#include <compat/apple_compat.h>
|
|
|
|
#include "../../ui/drivers/ui_cocoa.h"
|
|
#include "../../ui/drivers/cocoa/cocoa_common.h"
|
|
#include "../video_driver.h"
|
|
#include "../../configuration.h"
|
|
#include "../../verbosity.h"
|
|
#ifdef HAVE_VULKAN
|
|
#include "../common/vulkan_common.h"
|
|
#endif
|
|
|
|
#if defined(HAVE_COCOATOUCH)
|
|
#define GLContextClass EAGLContext
|
|
#define GLFrameworkID CFSTR("com.apple.opengles")
|
|
#define RAScreen UIScreen
|
|
|
|
#ifndef UIUserInterfaceIdiomTV
|
|
#define UIUserInterfaceIdiomTV 2
|
|
#endif
|
|
|
|
#ifndef UIUserInterfaceIdiomCarPlay
|
|
#define UIUserInterfaceIdiomCarPlay 3
|
|
#endif
|
|
|
|
|
|
@interface EAGLContext (OSXCompat) @end
|
|
@implementation EAGLContext (OSXCompat)
|
|
+ (void)clearCurrentContext { [EAGLContext setCurrentContext:nil]; }
|
|
- (void)makeCurrentContext { [EAGLContext setCurrentContext:self]; }
|
|
@end
|
|
|
|
#else
|
|
|
|
@interface NSScreen (IOSCompat) @end
|
|
@implementation NSScreen (IOSCompat)
|
|
- (CGRect)bounds
|
|
{
|
|
CGRect cgrect = NSRectToCGRect(self.frame);
|
|
return CGRectMake(0, 0, CGRectGetWidth(cgrect), CGRectGetHeight(cgrect));
|
|
}
|
|
- (float) scale { return 1.0f; }
|
|
@end
|
|
|
|
#define GLContextClass NSOpenGLContext
|
|
#define GLFrameworkID CFSTR("com.apple.opengl")
|
|
#define RAScreen NSScreen
|
|
#endif
|
|
|
|
static enum gfx_ctx_api cocoagl_api = GFX_CTX_NONE;
|
|
|
|
typedef struct cocoa_ctx_data
|
|
{
|
|
bool core_hw_context_enable;
|
|
#ifdef HAVE_VULKAN
|
|
gfx_ctx_vulkan_data_t vk;
|
|
int swap_interval;
|
|
#endif
|
|
unsigned width;
|
|
unsigned height;
|
|
} cocoa_ctx_data_t;
|
|
|
|
#if defined(HAVE_COCOATOUCH)
|
|
|
|
static GLKView *g_view;
|
|
UIView *g_pause_indicator_view;
|
|
#endif
|
|
|
|
static GLContextClass* g_hw_ctx;
|
|
static GLContextClass* g_context;
|
|
|
|
static int g_fast_forward_skips;
|
|
static bool g_is_syncing = true;
|
|
static bool g_use_hw_ctx = false;
|
|
|
|
#if defined(HAVE_COCOA_METAL)
|
|
static NSOpenGLPixelFormat* g_format;
|
|
|
|
void *glcontext_get_ptr(void)
|
|
{
|
|
return (BRIDGE void *)g_context;
|
|
}
|
|
#endif
|
|
|
|
static unsigned g_minor = 0;
|
|
static unsigned g_major = 0;
|
|
|
|
/* forward declaration */
|
|
void *nsview_get_ptr(void);
|
|
|
|
#include "cocoa_gl_shared.h"
|
|
|
|
static void *cocoagl_gfx_ctx_init(video_frame_info_t *video_info, void *video_driver)
|
|
{
|
|
cocoa_ctx_data_t *cocoa_ctx = (cocoa_ctx_data_t*)
|
|
calloc(1, sizeof(cocoa_ctx_data_t));
|
|
|
|
if (!cocoa_ctx)
|
|
return NULL;
|
|
|
|
switch (cocoagl_api)
|
|
{
|
|
#if defined(HAVE_COCOATOUCH)
|
|
case GFX_CTX_OPENGL_ES_API:
|
|
// setViewType is not (yet?) defined for iOS
|
|
// [apple_platform setViewType:APPLE_VIEW_TYPE_OPENGL_ES];
|
|
break;
|
|
#elif defined(HAVE_COCOA_METAL)
|
|
case GFX_CTX_OPENGL_API:
|
|
[apple_platform setViewType:APPLE_VIEW_TYPE_OPENGL];
|
|
break;
|
|
#endif
|
|
case GFX_CTX_VULKAN_API:
|
|
#ifdef HAVE_VULKAN
|
|
[apple_platform setViewType:APPLE_VIEW_TYPE_VULKAN];
|
|
if (!vulkan_context_init(&cocoa_ctx->vk, VULKAN_WSI_MVK_MACOS))
|
|
goto error;
|
|
#endif
|
|
break;
|
|
case GFX_CTX_NONE:
|
|
default:
|
|
break;
|
|
}
|
|
|
|
return cocoa_ctx;
|
|
|
|
error:
|
|
free(cocoa_ctx);
|
|
return NULL;
|
|
}
|
|
|
|
static bool cocoagl_gfx_ctx_bind_api(void *data, enum gfx_ctx_api api,
|
|
unsigned major, unsigned minor)
|
|
{
|
|
(void)data;
|
|
switch (api)
|
|
{
|
|
#if defined(HAVE_COCOATOUCH)
|
|
case GFX_CTX_OPENGL_ES_API:
|
|
break;
|
|
#elif defined(HAVE_COCOA_METAL)
|
|
case GFX_CTX_OPENGL_API:
|
|
break;
|
|
#ifdef HAVE_VULKAN
|
|
case GFX_CTX_VULKAN_API:
|
|
break;
|
|
#endif
|
|
#endif
|
|
case GFX_CTX_NONE:
|
|
default:
|
|
return false;
|
|
}
|
|
|
|
cocoagl_api = api;
|
|
g_minor = minor;
|
|
g_major = major;
|
|
|
|
return true;
|
|
}
|
|
|
|
static void cocoagl_gfx_ctx_swap_interval(void *data, int interval)
|
|
{
|
|
#ifdef HAVE_VULKAN
|
|
cocoa_ctx_data_t *cocoa_ctx = (cocoa_ctx_data_t*)data;
|
|
#endif
|
|
|
|
switch (cocoagl_api)
|
|
{
|
|
case GFX_CTX_OPENGL_API:
|
|
case GFX_CTX_OPENGL_ES_API:
|
|
{
|
|
#if defined(HAVE_COCOATOUCH) // < No way to disable Vsync on iOS?
|
|
// Just skip presents so fast forward still works.
|
|
g_is_syncing = interval ? true : false;
|
|
g_fast_forward_skips = interval ? 0 : 3;
|
|
#elif defined(HAVE_COCOA_METAL)
|
|
GLint value = interval ? 1 : 0;
|
|
[g_context setValues:&value forParameter:NSOpenGLCPSwapInterval];
|
|
#endif
|
|
break;
|
|
}
|
|
case GFX_CTX_VULKAN_API:
|
|
#ifdef HAVE_VULKAN
|
|
if (cocoa_ctx->swap_interval != interval)
|
|
{
|
|
cocoa_ctx->swap_interval = interval;
|
|
if (cocoa_ctx->vk.swapchain)
|
|
cocoa_ctx->vk.need_new_swapchain = true;
|
|
}
|
|
#endif
|
|
break;
|
|
case GFX_CTX_NONE:
|
|
default:
|
|
break;
|
|
}
|
|
|
|
}
|
|
|
|
static bool cocoagl_gfx_ctx_set_video_mode(void *data,
|
|
video_frame_info_t *video_info,
|
|
unsigned width, unsigned height, bool fullscreen)
|
|
{
|
|
#if defined(HAVE_COCOA_METAL)
|
|
NSView *g_view = apple_platform.renderView;
|
|
#elif defined(HAVE_COCOA)
|
|
CocoaView *g_view = (CocoaView*)nsview_get_ptr();
|
|
#endif
|
|
cocoa_ctx_data_t *cocoa_ctx = (cocoa_ctx_data_t*)data;
|
|
|
|
cocoa_ctx->width = width;
|
|
cocoa_ctx->height = height;
|
|
|
|
switch (cocoagl_api)
|
|
{
|
|
case GFX_CTX_OPENGL_API:
|
|
case GFX_CTX_OPENGL_ES_API:
|
|
{
|
|
#if defined(HAVE_COCOA_METAL)
|
|
if ([g_view respondsToSelector: @selector(setWantsBestResolutionOpenGLSurface:)])
|
|
[g_view setWantsBestResolutionOpenGLSurface:YES];
|
|
|
|
NSOpenGLPixelFormatAttribute attributes [] = {
|
|
NSOpenGLPFAColorSize,
|
|
24,
|
|
NSOpenGLPFADoubleBuffer,
|
|
NSOpenGLPFAAllowOfflineRenderers,
|
|
NSOpenGLPFADepthSize,
|
|
(NSOpenGLPixelFormatAttribute)16, // 16 bit depth buffer
|
|
0, /* profile */
|
|
0, /* profile enum */
|
|
(NSOpenGLPixelFormatAttribute)0
|
|
};
|
|
|
|
#if MAC_OS_X_VERSION_10_7
|
|
if (g_major == 3 && (g_minor >= 1 && g_minor <= 3))
|
|
{
|
|
attributes[6] = NSOpenGLPFAOpenGLProfile;
|
|
attributes[7] = NSOpenGLProfileVersion3_2Core;
|
|
}
|
|
#endif
|
|
|
|
#if MAC_OS_X_VERSION_10_10
|
|
if (g_major == 4 && g_minor == 1)
|
|
{
|
|
attributes[6] = NSOpenGLPFAOpenGLProfile;
|
|
attributes[7] = NSOpenGLProfileVersion4_1Core;
|
|
}
|
|
#endif
|
|
|
|
g_format = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes];
|
|
|
|
#if MAC_OS_X_VERSION_MIN_REQUIRED < 1050
|
|
if (g_format == nil)
|
|
{
|
|
/* NSOpenGLFPAAllowOfflineRenderers is
|
|
not supported on this OS version. */
|
|
attributes[3] = (NSOpenGLPixelFormatAttribute)0;
|
|
g_format = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes];
|
|
}
|
|
#endif
|
|
|
|
if (g_use_hw_ctx)
|
|
g_hw_ctx = [[NSOpenGLContext alloc] initWithFormat:g_format shareContext:nil];
|
|
g_context = [[NSOpenGLContext alloc] initWithFormat:g_format shareContext:(g_use_hw_ctx) ? g_hw_ctx : nil];
|
|
[g_context setView:g_view];
|
|
#else
|
|
if (g_use_hw_ctx)
|
|
g_hw_ctx = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
|
|
g_context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
|
|
g_view.context = g_context;
|
|
#endif
|
|
|
|
[g_context makeCurrentContext];
|
|
break;
|
|
}
|
|
case GFX_CTX_VULKAN_API:
|
|
#ifdef HAVE_VULKAN
|
|
RARCH_LOG("[macOS]: Native window size: %u x %u.\n", cocoa_ctx->width, cocoa_ctx->height);
|
|
if (!vulkan_surface_create(&cocoa_ctx->vk,
|
|
VULKAN_WSI_MVK_MACOS, NULL,
|
|
(BRIDGE void *)g_view, cocoa_ctx->width, cocoa_ctx->height,
|
|
cocoa_ctx->swap_interval))
|
|
{
|
|
RARCH_ERR("[macOS]: Failed to create surface.\n");
|
|
return false;
|
|
}
|
|
#endif
|
|
break;
|
|
case GFX_CTX_NONE:
|
|
default:
|
|
break;
|
|
}
|
|
|
|
#if defined(HAVE_COCOA_METAL)
|
|
static bool has_went_fullscreen = false;
|
|
/* TODO: Screen mode support. */
|
|
|
|
if (fullscreen)
|
|
{
|
|
if (!has_went_fullscreen)
|
|
{
|
|
[g_view enterFullScreenMode:(BRIDGE NSScreen *)get_chosen_screen() withOptions:nil];
|
|
cocoagl_gfx_ctx_show_mouse(data, false);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (has_went_fullscreen)
|
|
{
|
|
[g_view exitFullScreenModeWithOptions:nil];
|
|
[[g_view window] makeFirstResponder:g_view];
|
|
cocoagl_gfx_ctx_show_mouse(data, true);
|
|
}
|
|
|
|
[[g_view window] setContentSize:NSMakeSize(width, height)];
|
|
}
|
|
|
|
has_went_fullscreen = fullscreen;
|
|
#endif
|
|
|
|
/* TODO: Maybe iOS users should be able to show/hide the status bar here? */
|
|
|
|
return true;
|
|
}
|
|
|
|
#ifdef HAVE_VULKAN
|
|
static void *cocoagl_gfx_ctx_get_context_data(void *data)
|
|
{
|
|
cocoa_ctx_data_t *cocoa_ctx = (cocoa_ctx_data_t*)data;
|
|
return &cocoa_ctx->vk.context;
|
|
}
|
|
#endif
|
|
|
|
static void cocoagl_gfx_ctx_swap_buffers(void *data, void *data2)
|
|
{
|
|
#ifdef HAVE_VULKAN
|
|
cocoa_ctx_data_t *cocoa_ctx = (cocoa_ctx_data_t*)data;
|
|
#endif
|
|
|
|
switch (cocoagl_api)
|
|
{
|
|
case GFX_CTX_OPENGL_API:
|
|
case GFX_CTX_OPENGL_ES_API:
|
|
if (!(--g_fast_forward_skips < 0))
|
|
return;
|
|
|
|
#if defined(HAVE_COCOA_METAL)
|
|
[g_context flushBuffer];
|
|
[g_hw_ctx flushBuffer];
|
|
#elif defined(HAVE_COCOATOUCH)
|
|
if (g_view)
|
|
[g_view display];
|
|
#endif
|
|
|
|
g_fast_forward_skips = g_is_syncing ? 0 : 3;
|
|
break;
|
|
case GFX_CTX_VULKAN_API:
|
|
#ifdef HAVE_VULKAN
|
|
vulkan_present(&cocoa_ctx->vk, cocoa_ctx->vk.context.current_swapchain_index);
|
|
vulkan_acquire_next_image(&cocoa_ctx->vk);
|
|
#endif
|
|
break;
|
|
case GFX_CTX_NONE:
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
static gfx_ctx_proc_t cocoagl_gfx_ctx_get_proc_address(const char *symbol_name)
|
|
{
|
|
switch (cocoagl_api)
|
|
{
|
|
case GFX_CTX_OPENGL_API:
|
|
case GFX_CTX_OPENGL_ES_API:
|
|
return (gfx_ctx_proc_t)CFBundleGetFunctionPointerForName(
|
|
CFBundleGetBundleWithIdentifier(GLFrameworkID),
|
|
(BRIDGE CFStringRef)BOXSTRING(symbol_name)
|
|
);
|
|
case GFX_CTX_NONE:
|
|
default:
|
|
break;
|
|
}
|
|
|
|
return NULL;
|
|
}
|
|
|
|
static bool cocoagl_gfx_ctx_set_resize(void *data, unsigned width, unsigned height)
|
|
{
|
|
#ifdef HAVE_VULKAN
|
|
cocoa_ctx_data_t *cocoa_ctx = (cocoa_ctx_data_t*)data;
|
|
#endif
|
|
|
|
switch (cocoagl_api)
|
|
{
|
|
case GFX_CTX_OPENGL_API:
|
|
case GFX_CTX_OPENGL_ES_API:
|
|
break;
|
|
case GFX_CTX_VULKAN_API:
|
|
#ifdef HAVE_VULKAN
|
|
cocoa_ctx->width = width;
|
|
cocoa_ctx->height = height;
|
|
|
|
if (vulkan_create_swapchain(&cocoa_ctx->vk,
|
|
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");
|
|
return false;
|
|
}
|
|
|
|
cocoa_ctx->vk.need_new_swapchain = false;
|
|
#endif
|
|
break;
|
|
case GFX_CTX_NONE:
|
|
default:
|
|
break;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
static void cocoagl_gfx_ctx_check_window(void *data, bool *quit,
|
|
bool *resize, unsigned *width, unsigned *height, bool is_shutdown)
|
|
{
|
|
unsigned new_width, new_height;
|
|
#ifdef HAVE_VULKAN
|
|
cocoa_ctx_data_t *cocoa_ctx = (cocoa_ctx_data_t*)data;
|
|
#endif
|
|
|
|
*quit = false;
|
|
|
|
switch (cocoagl_api)
|
|
{
|
|
case GFX_CTX_OPENGL_API:
|
|
case GFX_CTX_OPENGL_ES_API:
|
|
break;
|
|
case GFX_CTX_VULKAN_API:
|
|
#ifdef HAVE_VULKAN
|
|
*resize = cocoa_ctx->vk.need_new_swapchain;
|
|
#endif
|
|
break;
|
|
case GFX_CTX_NONE:
|
|
default:
|
|
break;
|
|
}
|
|
|
|
cocoagl_gfx_ctx_get_video_size(data, &new_width, &new_height);
|
|
if (new_width != *width || new_height != *height)
|
|
{
|
|
*width = new_width;
|
|
*height = new_height;
|
|
*resize = true;
|
|
}
|
|
}
|
|
|
|
static void cocoagl_gfx_ctx_bind_hw_render(void *data, bool enable)
|
|
{
|
|
(void)data;
|
|
switch (cocoagl_api)
|
|
{
|
|
case GFX_CTX_OPENGL_API:
|
|
case GFX_CTX_OPENGL_ES_API:
|
|
g_use_hw_ctx = enable;
|
|
|
|
if (enable)
|
|
[g_hw_ctx makeCurrentContext];
|
|
else
|
|
[g_context makeCurrentContext];
|
|
break;
|
|
case GFX_CTX_NONE:
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
const gfx_ctx_driver_t gfx_ctx_cocoagl = {
|
|
.init = cocoagl_gfx_ctx_init,
|
|
.destroy = cocoagl_gfx_ctx_destroy,
|
|
.get_api = cocoagl_gfx_ctx_get_api,
|
|
.bind_api = cocoagl_gfx_ctx_bind_api,
|
|
.swap_interval = cocoagl_gfx_ctx_swap_interval,
|
|
.set_video_mode = cocoagl_gfx_ctx_set_video_mode,
|
|
.get_video_size = cocoagl_gfx_ctx_get_video_size,
|
|
.get_metrics = cocoagl_gfx_ctx_get_metrics,
|
|
#if defined(HAVE_COCOA_METAL)
|
|
.update_window_title = cocoagl_gfx_ctx_update_title,
|
|
#endif
|
|
.check_window = cocoagl_gfx_ctx_check_window,
|
|
.set_resize = cocoagl_gfx_ctx_set_resize,
|
|
.has_focus = cocoagl_gfx_ctx_has_focus,
|
|
.suppress_screensaver = cocoagl_gfx_ctx_suppress_screensaver,
|
|
#if !defined(HAVE_COCOATOUCH)
|
|
.has_windowed = cocoagl_gfx_ctx_has_windowed,
|
|
#endif
|
|
.swap_buffers = cocoagl_gfx_ctx_swap_buffers,
|
|
.input_driver = cocoagl_gfx_ctx_input_driver,
|
|
.get_proc_address = cocoagl_gfx_ctx_get_proc_address,
|
|
.ident = "macOS",
|
|
.get_flags = cocoagl_gfx_ctx_get_flags,
|
|
.set_flags = cocoagl_gfx_ctx_set_flags,
|
|
.bind_hw_render = cocoagl_gfx_ctx_bind_hw_render,
|
|
#if defined(HAVE_VULKAN)
|
|
.get_context_data = cocoagl_gfx_ctx_get_context_data,
|
|
#else
|
|
.get_context_data = NULL,
|
|
#endif
|
|
};
|