2013-03-01 14:55:08 +00:00
|
|
|
/*
|
2015-04-13 07:36:54 +00:00
|
|
|
* This file is part of mpv.
|
|
|
|
*
|
2016-01-19 17:36:34 +00:00
|
|
|
* mpv is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2013-03-01 14:55:08 +00:00
|
|
|
*
|
2015-04-13 07:36:54 +00:00
|
|
|
* mpv is distributed in the hope that it will be useful,
|
2013-03-01 14:55:08 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2016-01-19 17:36:34 +00:00
|
|
|
* GNU Lesser General Public License for more details.
|
2013-03-01 14:55:08 +00:00
|
|
|
*
|
2016-01-19 17:36:34 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
|
2013-03-01 14:55:08 +00:00
|
|
|
*/
|
|
|
|
|
2014-07-08 06:17:15 +00:00
|
|
|
#include <OpenGL/OpenGL.h>
|
2014-12-28 18:10:44 +00:00
|
|
|
#include <dlfcn.h>
|
2017-02-20 19:34:57 +00:00
|
|
|
#include "options/m_config.h"
|
2015-08-28 23:10:30 +00:00
|
|
|
#include "video/out/cocoa_common.h"
|
2014-07-14 05:20:49 +00:00
|
|
|
#include "osdep/macosx_versions.h"
|
2015-12-19 11:59:07 +00:00
|
|
|
#include "context.h"
|
2013-03-01 14:55:08 +00:00
|
|
|
|
2017-02-20 19:34:57 +00:00
|
|
|
struct cocoa_opts {
|
|
|
|
int cocoa_force_dedicated_gpu;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define OPT_BASE_STRUCT struct cocoa_opts
|
|
|
|
const struct m_sub_options cocoa_conf = {
|
|
|
|
.opts = (const struct m_option[]) {
|
|
|
|
OPT_FLAG("cocoa-force-dedicated-gpu", cocoa_force_dedicated_gpu, 0),
|
|
|
|
{0}
|
|
|
|
},
|
|
|
|
.size = sizeof(struct cocoa_opts),
|
|
|
|
};
|
|
|
|
|
|
|
|
struct priv {
|
2014-07-08 06:17:15 +00:00
|
|
|
CGLPixelFormatObj pix;
|
|
|
|
CGLContextObj ctx;
|
2017-02-20 19:34:57 +00:00
|
|
|
|
|
|
|
struct cocoa_opts *opts;
|
2014-07-08 06:17:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int set_swap_interval(int enabled)
|
|
|
|
{
|
|
|
|
CGLContextObj ctx = CGLGetCurrentContext();
|
|
|
|
CGLError err = CGLSetParameter(ctx, kCGLCPSwapInterval, &enabled);
|
|
|
|
return (err == kCGLNoError) ? 0 : -1;
|
|
|
|
}
|
|
|
|
|
2014-12-28 18:10:44 +00:00
|
|
|
static void *cocoa_glgetaddr(const char *s)
|
|
|
|
{
|
|
|
|
void *ret = NULL;
|
|
|
|
void *handle = dlopen(
|
|
|
|
"/System/Library/Frameworks/OpenGL.framework/OpenGL",
|
|
|
|
RTLD_LAZY | RTLD_LOCAL);
|
|
|
|
if (!handle)
|
|
|
|
return NULL;
|
|
|
|
ret = dlsym(handle, s);
|
|
|
|
dlclose(handle);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-02-20 19:34:57 +00:00
|
|
|
static CGLError test_gl_version(struct MPGLContext *ctx, CGLOpenGLProfile ver)
|
2014-07-08 06:17:15 +00:00
|
|
|
{
|
2017-02-20 19:34:57 +00:00
|
|
|
struct priv *p = ctx->priv;
|
|
|
|
|
2014-07-08 06:17:15 +00:00
|
|
|
CGLPixelFormatAttribute attrs[] = {
|
|
|
|
kCGLPFAOpenGLProfile,
|
2017-02-20 19:34:57 +00:00
|
|
|
(CGLPixelFormatAttribute) ver,
|
2014-07-08 06:17:15 +00:00
|
|
|
kCGLPFAAccelerated,
|
|
|
|
// leave this as the last entry of the array to not break the fallback
|
|
|
|
// code
|
|
|
|
kCGLPFASupportsAutomaticGraphicsSwitching,
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
GLint npix;
|
2015-01-24 15:26:11 +00:00
|
|
|
CGLError err;
|
2017-02-20 19:34:57 +00:00
|
|
|
err = CGLChoosePixelFormat(attrs, &p->pix, &npix);
|
|
|
|
if (p->opts->cocoa_force_dedicated_gpu || err == kCGLBadAttribute) {
|
2014-07-08 06:17:15 +00:00
|
|
|
// kCGLPFASupportsAutomaticGraphicsSwitching is probably not supported
|
|
|
|
// by the current hardware. Falling back to not using it.
|
|
|
|
attrs[MP_ARRAY_SIZE(attrs) - 2] = 0;
|
2017-02-20 19:34:57 +00:00
|
|
|
err = CGLChoosePixelFormat(attrs, &p->pix, &npix);
|
2015-01-24 15:26:11 +00:00
|
|
|
}
|
|
|
|
|
2015-01-24 15:54:24 +00:00
|
|
|
if (err != kCGLNoError) {
|
2017-02-20 19:34:57 +00:00
|
|
|
MP_ERR(ctx->vo, "error creating CGL pixel format: %s (%d)\n",
|
2015-01-24 15:54:24 +00:00
|
|
|
CGLErrorString(err), err);
|
|
|
|
goto error_out;
|
|
|
|
}
|
|
|
|
|
2017-02-20 19:34:57 +00:00
|
|
|
err = CGLCreateContext(p->pix, 0, &p->ctx);
|
2015-01-24 15:54:24 +00:00
|
|
|
|
|
|
|
error_out:
|
2015-01-24 15:26:11 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2015-12-19 08:30:20 +00:00
|
|
|
static bool create_gl_context(struct MPGLContext *ctx, int vo_flags)
|
2015-01-24 15:26:11 +00:00
|
|
|
{
|
2017-02-20 19:34:57 +00:00
|
|
|
struct priv *p = ctx->priv;
|
2015-01-24 15:26:11 +00:00
|
|
|
CGLError err;
|
|
|
|
|
|
|
|
CGLOpenGLProfile gl_versions[] = {
|
|
|
|
kCGLOGLPVersion_3_2_Core,
|
|
|
|
kCGLOGLPVersion_Legacy,
|
|
|
|
};
|
|
|
|
|
|
|
|
for (int n = 0; n < MP_ARRAY_SIZE(gl_versions); n++) {
|
2017-02-20 19:34:57 +00:00
|
|
|
err = test_gl_version(ctx, gl_versions[n]);
|
2015-01-24 15:26:11 +00:00
|
|
|
if (err == kCGLNoError)
|
|
|
|
break;
|
2014-07-08 06:17:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (err != kCGLNoError) {
|
|
|
|
MP_FATAL(ctx->vo, "error creating CGL context: %s (%d)\n",
|
|
|
|
CGLErrorString(err), err);
|
2013-03-01 14:55:08 +00:00
|
|
|
return false;
|
2014-07-08 06:17:15 +00:00
|
|
|
}
|
2013-03-01 14:55:08 +00:00
|
|
|
|
cocoa: redo synchronization
Before this change, Cocoa state was accessed from both the VO and the
Cocoa main thread. This was probably not a good idea. There was some
locking as well as implicit synchronization using the dispatch
mechanism, but it wasn't watertight.
Change this completely. Now Cocoa things are always accessed from the
main thread only. The old mutex falls away, as well as the
vo_cocoa_set_current_context() function, which implicitly used the lock
to coordinate VO accesses. With the new code, the VO thread generally
has to wait for the main thread, while the main thread never waits for
the VO and rarely accesses it. Fortunately, this is rather straight
forward, and most of this is achieved by making vo_cocoa_control() run
on the main thread. The logic of the code does generally not change.
Some aspects are trickier. Apparently we can't access the
NSOpenGLContext from the VO thread, because this object is not thread-
safe. We use some CGLContextObj functions instead, such as for making
the context current and swapping the buffers.
2015-05-13 20:00:34 +00:00
|
|
|
vo_cocoa_set_opengl_ctx(ctx->vo, p->ctx);
|
|
|
|
CGLSetCurrentContext(p->ctx);
|
|
|
|
|
2015-12-19 08:30:20 +00:00
|
|
|
if (vo_flags & VOFLAG_ALPHA)
|
|
|
|
CGLSetParameter(p->ctx, kCGLCPSurfaceOpacity, &(GLint){0});
|
|
|
|
|
2014-12-28 18:10:44 +00:00
|
|
|
mpgl_load_functions(ctx->gl, (void *)cocoa_glgetaddr, NULL, ctx->vo->log);
|
2013-03-01 14:55:08 +00:00
|
|
|
|
2014-07-08 06:17:15 +00:00
|
|
|
CGLReleasePixelFormat(p->pix);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-10-01 20:25:12 +00:00
|
|
|
static void cocoa_uninit(MPGLContext *ctx)
|
2014-07-08 06:17:15 +00:00
|
|
|
{
|
2017-02-20 19:34:57 +00:00
|
|
|
struct priv *p = ctx->priv;
|
2015-10-01 20:25:12 +00:00
|
|
|
CGLReleaseContext(p->ctx);
|
|
|
|
vo_cocoa_uninit(ctx->vo);
|
|
|
|
}
|
2014-07-08 06:17:15 +00:00
|
|
|
|
2015-10-01 20:25:12 +00:00
|
|
|
static int cocoa_init(MPGLContext *ctx, int vo_flags)
|
|
|
|
{
|
2017-02-20 19:34:57 +00:00
|
|
|
struct priv *p = ctx->priv;
|
|
|
|
p->opts = mp_get_config_group(ctx, ctx->global, &cocoa_conf);
|
2015-10-01 20:25:12 +00:00
|
|
|
vo_cocoa_init(ctx->vo);
|
2013-03-01 14:55:08 +00:00
|
|
|
|
2015-12-19 08:30:20 +00:00
|
|
|
if (!create_gl_context(ctx, vo_flags))
|
2015-10-01 20:25:12 +00:00
|
|
|
return -1;
|
2013-09-28 11:51:29 +00:00
|
|
|
|
2015-10-01 20:25:12 +00:00
|
|
|
ctx->gl->SwapInterval = set_swap_interval;
|
|
|
|
return 0;
|
2013-03-01 14:55:08 +00:00
|
|
|
}
|
|
|
|
|
2015-10-03 16:20:16 +00:00
|
|
|
static int cocoa_reconfig(struct MPGLContext *ctx)
|
2013-03-01 14:55:08 +00:00
|
|
|
{
|
2015-10-01 20:25:12 +00:00
|
|
|
vo_cocoa_config_window(ctx->vo);
|
|
|
|
return 0;
|
2013-03-01 14:55:08 +00:00
|
|
|
}
|
|
|
|
|
2015-10-01 20:25:12 +00:00
|
|
|
static int cocoa_control(struct MPGLContext *ctx, int *events, int request,
|
|
|
|
void *arg)
|
2013-03-01 14:55:08 +00:00
|
|
|
{
|
2015-10-01 20:25:12 +00:00
|
|
|
return vo_cocoa_control(ctx->vo, events, request, arg);
|
2013-03-01 14:55:08 +00:00
|
|
|
}
|
|
|
|
|
2015-10-01 20:25:12 +00:00
|
|
|
static void cocoa_swap_buffers(struct MPGLContext *ctx)
|
2013-03-01 14:55:08 +00:00
|
|
|
{
|
2015-10-01 20:25:12 +00:00
|
|
|
vo_cocoa_swap_buffers(ctx->vo);
|
2016-10-06 17:21:21 +00:00
|
|
|
ctx->gl->Flush();
|
2013-03-01 14:55:08 +00:00
|
|
|
}
|
2015-10-01 20:25:12 +00:00
|
|
|
|
|
|
|
const struct mpgl_driver mpgl_driver_cocoa = {
|
|
|
|
.name = "cocoa",
|
2017-02-20 19:34:57 +00:00
|
|
|
.priv_size = sizeof(struct priv),
|
2015-10-01 20:25:12 +00:00
|
|
|
.init = cocoa_init,
|
|
|
|
.reconfig = cocoa_reconfig,
|
|
|
|
.swap_buffers = cocoa_swap_buffers,
|
|
|
|
.control = cocoa_control,
|
|
|
|
.uninit = cocoa_uninit,
|
|
|
|
};
|