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"
|
2015-12-19 11:59:07 +00:00
|
|
|
#include "context.h"
|
2020-01-19 15:23:59 +00:00
|
|
|
#include "osdep/macosx_application.h"
|
2013-03-01 14:55:08 +00:00
|
|
|
|
2017-02-20 19:34:57 +00:00
|
|
|
struct cocoa_opts {
|
|
|
|
};
|
|
|
|
|
|
|
|
#define OPT_BASE_STRUCT struct cocoa_opts
|
|
|
|
const struct m_sub_options cocoa_conf = {
|
|
|
|
.opts = (const struct m_option[]) {
|
2020-01-19 15:23:59 +00:00
|
|
|
OPT_REPLACED("cocoa-force-dedicated-gpu", "macos-force-dedicated-gpu"),
|
2017-02-20 19:34:57 +00:00
|
|
|
{0}
|
|
|
|
},
|
|
|
|
.size = sizeof(struct cocoa_opts),
|
|
|
|
};
|
|
|
|
|
|
|
|
struct priv {
|
2017-09-15 15:37:28 +00:00
|
|
|
GL gl;
|
2017-12-28 11:42:02 +00:00
|
|
|
void (GLAPIENTRY *Flush)(void);
|
2014-07-08 06:17:15 +00:00
|
|
|
CGLPixelFormatObj pix;
|
|
|
|
CGLContextObj ctx;
|
2017-02-20 19:34:57 +00:00
|
|
|
|
|
|
|
struct cocoa_opts *opts;
|
2020-01-19 15:23:59 +00:00
|
|
|
struct macos_opts *macos_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;
|
|
|
|
}
|
|
|
|
|
2017-12-28 11:42:02 +00:00
|
|
|
static void glFlushDummy(void) { }
|
|
|
|
|
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-09-15 15:37:28 +00:00
|
|
|
static CGLError test_gl_version(struct ra_ctx *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[] = {
|
2017-07-27 12:37:58 +00:00
|
|
|
// let this array ordered by the inverse order of the most probably
|
|
|
|
// rejected attribute to preserve the fallback code
|
2014-07-08 06:17:15 +00:00
|
|
|
kCGLPFAOpenGLProfile,
|
2017-02-20 19:34:57 +00:00
|
|
|
(CGLPixelFormatAttribute) ver,
|
2014-07-08 06:17:15 +00:00
|
|
|
kCGLPFAAccelerated,
|
2017-07-27 12:37:58 +00:00
|
|
|
kCGLPFAAllowOfflineRenderers,
|
|
|
|
// keep this one last to apply the cocoa-force-dedicated-gpu option
|
2014-07-08 06:17:15 +00:00
|
|
|
kCGLPFASupportsAutomaticGraphicsSwitching,
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
GLint npix;
|
2015-01-24 15:26:11 +00:00
|
|
|
CGLError err;
|
2017-07-27 12:37:58 +00:00
|
|
|
int supported_attribute = MP_ARRAY_SIZE(attrs)-1;
|
|
|
|
|
2020-01-19 15:23:59 +00:00
|
|
|
if (p->macos_opts->macos_force_dedicated_gpu)
|
2017-07-27 12:37:58 +00:00
|
|
|
attrs[--supported_attribute] = 0;
|
|
|
|
|
2017-02-20 19:34:57 +00:00
|
|
|
err = CGLChoosePixelFormat(attrs, &p->pix, &npix);
|
2017-07-27 12:37:58 +00:00
|
|
|
while (err == kCGLBadAttribute && supported_attribute > 3) {
|
|
|
|
// kCGLPFASupportsAutomaticGraphicsSwitching is probably not
|
|
|
|
// supported by the current hardware. Falling back to not using
|
|
|
|
// it and disallowing Offline Renderers if further restrictions
|
|
|
|
// apply
|
|
|
|
attrs[--supported_attribute] = 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;
|
|
|
|
}
|
|
|
|
|
2017-09-15 15:37:28 +00:00
|
|
|
static bool create_gl_context(struct ra_ctx *ctx)
|
2015-01-24 15:26:11 +00:00
|
|
|
{
|
2017-02-20 19:34:57 +00:00
|
|
|
struct priv *p = ctx->priv;
|
2017-09-15 15:37:28 +00:00
|
|
|
GL *gl = &p->gl;
|
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);
|
|
|
|
|
2017-09-15 15:37:28 +00:00
|
|
|
if (ctx->opts.want_alpha)
|
2015-12-19 08:30:20 +00:00
|
|
|
CGLSetParameter(p->ctx, kCGLCPSurfaceOpacity, &(GLint){0});
|
|
|
|
|
2017-09-15 15:37:28 +00:00
|
|
|
mpgl_load_functions(gl, (void *)cocoa_glgetaddr, NULL, ctx->vo->log);
|
|
|
|
gl->SwapInterval = set_swap_interval;
|
2017-12-28 11:42:02 +00:00
|
|
|
p->Flush = gl->Flush;
|
|
|
|
gl->Flush = glFlushDummy;
|
2013-03-01 14:55:08 +00:00
|
|
|
|
2014-07-08 06:17:15 +00:00
|
|
|
CGLReleasePixelFormat(p->pix);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-09-15 15:37:28 +00:00
|
|
|
static void cocoa_uninit(struct ra_ctx *ctx)
|
2014-07-08 06:17:15 +00:00
|
|
|
{
|
2017-02-20 19:34:57 +00:00
|
|
|
struct priv *p = ctx->priv;
|
2017-09-15 15:37:28 +00:00
|
|
|
ra_gl_ctx_uninit(ctx);
|
2015-10-01 20:25:12 +00:00
|
|
|
CGLReleaseContext(p->ctx);
|
|
|
|
vo_cocoa_uninit(ctx->vo);
|
|
|
|
}
|
2014-07-08 06:17:15 +00:00
|
|
|
|
2017-09-15 15:37:28 +00:00
|
|
|
static void cocoa_swap_buffers(struct ra_ctx *ctx)
|
2015-10-01 20:25:12 +00:00
|
|
|
{
|
2017-09-22 13:08:46 +00:00
|
|
|
struct priv *p = ctx->priv;
|
2017-09-15 15:37:28 +00:00
|
|
|
vo_cocoa_swap_buffers(ctx->vo);
|
2017-12-28 11:42:02 +00:00
|
|
|
p->Flush();
|
2017-09-15 15:37:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool cocoa_init(struct ra_ctx *ctx)
|
|
|
|
{
|
|
|
|
struct priv *p = ctx->priv = talloc_zero(ctx, struct priv);
|
|
|
|
GL *gl = &p->gl;
|
2017-02-20 19:34:57 +00:00
|
|
|
p->opts = mp_get_config_group(ctx, ctx->global, &cocoa_conf);
|
2020-01-19 15:23:59 +00:00
|
|
|
p->macos_opts = mp_get_config_group(ctx, ctx->global, &macos_conf);
|
2015-10-01 20:25:12 +00:00
|
|
|
vo_cocoa_init(ctx->vo);
|
2013-03-01 14:55:08 +00:00
|
|
|
|
2018-04-18 14:04:27 +00:00
|
|
|
MP_WARN(ctx->vo, "opengl cocoa backend is deprecated, use vo=libmpv instead\n");
|
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
2018-02-12 11:28:19 +00:00
|
|
|
|
2017-09-15 15:37:28 +00:00
|
|
|
if (!create_gl_context(ctx))
|
|
|
|
goto fail;
|
2013-09-28 11:51:29 +00:00
|
|
|
|
2017-09-15 15:37:28 +00:00
|
|
|
struct ra_gl_ctx_params params = {
|
|
|
|
.swap_buffers = cocoa_swap_buffers,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!ra_gl_ctx_init(ctx, gl, params))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
cocoa_uninit(ctx);
|
|
|
|
return false;
|
2013-03-01 14:55:08 +00:00
|
|
|
}
|
|
|
|
|
2017-09-15 15:37:28 +00:00
|
|
|
static void resize(struct ra_ctx *ctx)
|
2013-03-01 14:55:08 +00:00
|
|
|
{
|
2017-09-15 15:37:28 +00:00
|
|
|
ra_gl_ctx_resize(ctx->swapchain, ctx->vo->dwidth, ctx->vo->dheight, 0);
|
2013-03-01 14:55:08 +00:00
|
|
|
}
|
|
|
|
|
2017-09-15 15:37:28 +00:00
|
|
|
static bool cocoa_reconfig(struct ra_ctx *ctx)
|
2013-03-01 14:55:08 +00:00
|
|
|
{
|
2017-09-15 15:37:28 +00:00
|
|
|
vo_cocoa_config_window(ctx->vo);
|
|
|
|
resize(ctx);
|
|
|
|
return true;
|
2013-03-01 14:55:08 +00:00
|
|
|
}
|
|
|
|
|
2017-09-15 15:37:28 +00:00
|
|
|
static int cocoa_control(struct ra_ctx *ctx, int *events, int request,
|
|
|
|
void *arg)
|
2013-03-01 14:55:08 +00:00
|
|
|
{
|
2017-09-15 15:37:28 +00:00
|
|
|
int ret = vo_cocoa_control(ctx->vo, events, request, arg);
|
|
|
|
if (*events & VO_EVENT_RESIZE)
|
|
|
|
resize(ctx);
|
|
|
|
return ret;
|
2013-03-01 14:55:08 +00:00
|
|
|
}
|
2015-10-01 20:25:12 +00:00
|
|
|
|
2017-09-15 15:37:28 +00:00
|
|
|
const struct ra_ctx_fns ra_ctx_cocoa = {
|
|
|
|
.type = "opengl",
|
2015-10-01 20:25:12 +00:00
|
|
|
.name = "cocoa",
|
|
|
|
.init = cocoa_init,
|
|
|
|
.reconfig = cocoa_reconfig,
|
|
|
|
.control = cocoa_control,
|
|
|
|
.uninit = cocoa_uninit,
|
vo_opengl: refactor into vo_gpu
This is done in several steps:
1. refactor MPGLContext -> struct ra_ctx
2. move GL-specific stuff in vo_opengl into opengl/context.c
3. generalize context creation to support other APIs, and add --gpu-api
4. rename all of the --opengl- options that are no longer opengl-specific
5. move all of the stuff from opengl/* that isn't GL-specific into gpu/
(note: opengl/gl_utils.h became opengl/utils.h)
6. rename vo_opengl to vo_gpu
7. to handle window screenshots, the short-term approach was to just add
it to ra_swchain_fns. Long term (and for vulkan) this has to be moved to
ra itself (and vo_gpu altered to compensate), but this was a stop-gap
measure to prevent this commit from getting too big
8. move ra->fns->flush to ra_gl_ctx instead
9. some other minor changes that I've probably already forgotten
Note: This is one half of a major refactor, the other half of which is
provided by rossy's following commit. This commit enables support for
all linux platforms, while his version enables support for all non-linux
platforms.
Note 2: vo_opengl_cb.c also re-uses ra_gl_ctx so it benefits from the
--opengl- options like --opengl-early-flush, --opengl-finish etc. Should
be a strict superset of the old functionality.
Disclaimer: Since I have no way of compiling mpv on all platforms, some
of these ports were done blindly. Specifically, the blind ports included
context_mali_fbdev.c and context_rpi.c. Since they're both based on
egl_helpers, the port should have gone smoothly without any major
changes required. But if somebody complains about a compile error on
those platforms (assuming anybody actually uses them), you know where to
complain.
2017-09-14 06:04:55 +00:00
|
|
|
};
|