summaryrefslogtreecommitdiffstats
path: root/osdep/macosx_application.m
diff options
context:
space:
mode:
authorAkemi <der.richter@gmx.de>2018-02-12 12:28:19 +0100
committerKevin Mitchell <kevmitch@gmail.com>2018-02-12 04:49:15 -0800
commitc5e4538bc4b63f02bf89f62aa25a37b9eb0c0316 (patch)
tree443f47ce1d584bb5fbf4fa30918e7c17cb800016 /osdep/macosx_application.m
parent235eb60671c899d55b1174043940763b250fa3b8 (diff)
downloadmpv-c5e4538bc4b63f02bf89f62aa25a37b9eb0c0316.tar.bz2
mpv-c5e4538bc4b63f02bf89f62aa25a37b9eb0c0316.tar.xz
cocoa-cb: initial implementation via opengl-cb API
this is meant to replace the old and not properly working vo_gpu/opengl cocoa backend in the future. the problems are various shortcomings of Apple's opengl implementation and buggy behaviour in certain circumstances that couldn't be properly worked around. there are also certain regressions on newer macOS versions from 10.11 onwards. - awful opengl performance with a none layer backed context - huge amount of dropped frames with an early context flush - flickering of system elements like the dock or volume indicator - double buffering not properly working with a none layer backed context - bad performance in fullscreen because of system optimisations all the problems were caused by using a normal opengl context, that seems somewhat abandoned by apple, and are fixed by using a layer backed opengl context instead. problems that couldn't be fixed could be properly worked around. this has all features our old backend has sans the wid embedding, the possibility to disable the automatic GPU switching and taking screenshots of the window content. the first was deemed unnecessary by me for now, since i just use the libmpv API that others can use anyway. second is technically not possible atm because we have to pre-allocate our opengl context at a time the config isn't read yet, so we can't get the needed property. third one is a bit tricky because of deadlocking and it needed to be in sync, hopefully i can work around that in the future. this also has at least one additional feature or eye-candy. a properly working fullscreen animation with the native fs. also since this is a direct port of the old backend of the parts that could be used, though with adaptions and improvements, this looks a lot cleaner and easier to understand. some credit goes to @pigoz for the initial swift build support which i could improve upon. Fixes: #5478, #5393, #5152, #5151, #4615, #4476, #3978, #3746, #3739, #2392, #2217
Diffstat (limited to 'osdep/macosx_application.m')
-rw-r--r--osdep/macosx_application.m80
1 files changed, 58 insertions, 22 deletions
diff --git a/osdep/macosx_application.m b/osdep/macosx_application.m
index d070401621..a65910ec9e 100644
--- a/osdep/macosx_application.m
+++ b/osdep/macosx_application.m
@@ -33,6 +33,9 @@
#if HAVE_MACOS_TOUCHBAR
#import "osdep/macosx_touchbar.h"
#endif
+#if HAVE_MACOS_COCOA_CB
+#include "osdep/macOS_swift.h"
+#endif
#define MPV_PROTOCOL @"mpv://"
@@ -40,7 +43,13 @@
// running in libmpv mode, and cocoa_main() was never called.
static bool application_instantiated;
+struct playback_thread_ctx {
+ int *argc;
+ char ***argv;
+};
+
static pthread_t playback_thread_id;
+static struct playback_thread_ctx thread_ctx = {0};
@interface Application ()
{
@@ -60,9 +69,22 @@ static void terminate_cocoa_application(void)
[NSApp terminate:NSApp];
}
+static void *playback_thread(void *ctx_obj)
+{
+ mpthread_set_name("playback core (OSX)");
+ @autoreleasepool {
+ struct playback_thread_ctx *ctx = (struct playback_thread_ctx*) ctx_obj;
+ int r = mpv_main(*ctx->argc, *ctx->argv);
+ terminate_cocoa_application();
+ // normally never reached - unless the cocoa mainloop hasn't started yet
+ exit(r);
+ }
+}
+
@implementation Application
@synthesize menuBar = _menu_bar;
@synthesize openCount = _open_count;
+@synthesize cocoaCB = _cocoa_cb;
- (void)sendEvent:(NSEvent *)event
{
@@ -96,6 +118,24 @@ static void terminate_cocoa_application(void)
[super dealloc];
}
+- (void)initMPVCore
+{
+ pthread_create(&playback_thread_id, NULL, playback_thread, &thread_ctx);
+ [[EventsResponder sharedInstance] waitForInputContext];
+}
+
+static const char macosx_icon[] =
+#include "osdep/macosx_icon.inc"
+;
+
+- (NSImage *)getMPVIcon
+{
+ NSData *icon_data = [NSData dataWithBytesNoCopy:(void *)macosx_icon
+ length:sizeof(macosx_icon)
+ freeWhenDone:NO];
+ return [[NSImage alloc] initWithData:icon_data];
+}
+
#if HAVE_MACOS_TOUCHBAR
- (NSTouchBar *)makeTouchBar
{
@@ -117,6 +157,16 @@ static void terminate_cocoa_application(void)
if ([self respondsToSelector:@selector(touchBar)])
[(TouchBar *)self.touchBar processEvent:event];
#endif
+ if (_cocoa_cb) {
+ [_cocoa_cb processEvent:event];
+ }
+}
+
+- (void)setMpvHandle:(struct mpv_handle *)ctx
+{
+ if (_cocoa_cb) {
+ [_cocoa_cb setMpvHandle:ctx];
+ }
}
- (void)queueCommand:(char *)cmd
@@ -182,11 +232,6 @@ static void terminate_cocoa_application(void)
}
@end
-struct playback_thread_ctx {
- int *argc;
- char ***argv;
-};
-
static void cocoa_run_runloop(void)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
@@ -194,18 +239,6 @@ static void cocoa_run_runloop(void)
[pool drain];
}
-static void *playback_thread(void *ctx_obj)
-{
- mpthread_set_name("playback core (OSX)");
- @autoreleasepool {
- struct playback_thread_ctx *ctx = (struct playback_thread_ctx*) ctx_obj;
- int r = mpv_main(*ctx->argc, *ctx->argv);
- terminate_cocoa_application();
- // normally never reached - unless the cocoa mainloop hasn't started yet
- exit(r);
- }
-}
-
void cocoa_register_menu_item_action(MPMenuKey key, void* action)
{
if (application_instantiated)
@@ -218,6 +251,10 @@ static void init_cocoa_application(bool regular)
[NSApp setDelegate:NSApp];
[NSApp setMenuBar:[[MenuBar alloc] init]];
+#if HAVE_MACOS_COCOA_CB
+ [NSApp setCocoaCB:[[CocoaCB alloc] init]];
+#endif
+
// Will be set to Regular from cocoa_common during UI creation so that we
// don't create an icon when playing audio only files.
[NSApp setActivationPolicy: regular ?
@@ -279,9 +316,8 @@ int cocoa_main(int argc, char *argv[])
application_instantiated = true;
[[EventsResponder sharedInstance] setIsApplication:YES];
- struct playback_thread_ctx ctx = {0};
- ctx.argc = &argc;
- ctx.argv = &argv;
+ thread_ctx.argc = &argc;
+ thread_ctx.argv = &argv;
if (bundle_started_from_finder(argv)) {
setup_bundle(&argc, argv);
@@ -294,8 +330,8 @@ int cocoa_main(int argc, char *argv[])
init_cocoa_application(false);
}
- pthread_create(&playback_thread_id, NULL, playback_thread, &ctx);
- [[EventsResponder sharedInstance] waitForInputContext];
+ if (![NSApp cocoaCB])
+ [NSApp initMPVCore];
cocoa_run_runloop();
// This should never be reached: cocoa_run_runloop blocks until the