mpv/video/out/opengl/context_cocoa.c

192 lines
5.2 KiB
C
Raw Normal View History

/*
* This file is part of mpv.
*
* 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.
*
* mpv 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
*/
#include <OpenGL/OpenGL.h>
#include <dlfcn.h>
#include "options/m_config.h"
#include "video/out/cocoa_common.h"
2014-07-14 05:20:49 +00:00
#include "osdep/macosx_versions.h"
#include "context.h"
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 {
CGLPixelFormatObj pix;
CGLContextObj ctx;
struct cocoa_opts *opts;
};
static int set_swap_interval(int enabled)
{
CGLContextObj ctx = CGLGetCurrentContext();
CGLError err = CGLSetParameter(ctx, kCGLCPSwapInterval, &enabled);
return (err == kCGLNoError) ? 0 : -1;
}
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;
}
static CGLError test_gl_version(struct MPGLContext *ctx, CGLOpenGLProfile ver)
{
struct priv *p = ctx->priv;
CGLPixelFormatAttribute attrs[] = {
// let this array ordered by the inverse order of the most probably
// rejected attribute to preserve the fallback code
kCGLPFAOpenGLProfile,
(CGLPixelFormatAttribute) ver,
kCGLPFAAccelerated,
kCGLPFAAllowOfflineRenderers,
// keep this one last to apply the cocoa-force-dedicated-gpu option
kCGLPFASupportsAutomaticGraphicsSwitching,
0
};
GLint npix;
CGLError err;
int supported_attribute = MP_ARRAY_SIZE(attrs)-1;
if (p->opts->cocoa_force_dedicated_gpu)
attrs[--supported_attribute] = 0;
err = CGLChoosePixelFormat(attrs, &p->pix, &npix);
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;
err = CGLChoosePixelFormat(attrs, &p->pix, &npix);
}
if (err != kCGLNoError) {
MP_ERR(ctx->vo, "error creating CGL pixel format: %s (%d)\n",
CGLErrorString(err), err);
goto error_out;
}
err = CGLCreateContext(p->pix, 0, &p->ctx);
error_out:
return err;
}
static bool create_gl_context(struct MPGLContext *ctx, int vo_flags)
{
struct priv *p = ctx->priv;
CGLError err;
CGLOpenGLProfile gl_versions[] = {
kCGLOGLPVersion_3_2_Core,
kCGLOGLPVersion_Legacy,
};
for (int n = 0; n < MP_ARRAY_SIZE(gl_versions); n++) {
err = test_gl_version(ctx, gl_versions[n]);
if (err == kCGLNoError)
break;
}
if (err != kCGLNoError) {
MP_FATAL(ctx->vo, "error creating CGL context: %s (%d)\n",
CGLErrorString(err), err);
return false;
}
vo_cocoa_set_opengl_ctx(ctx->vo, p->ctx);
CGLSetCurrentContext(p->ctx);
if (vo_flags & VOFLAG_ALPHA)
CGLSetParameter(p->ctx, kCGLCPSurfaceOpacity, &(GLint){0});
mpgl_load_functions(ctx->gl, (void *)cocoa_glgetaddr, NULL, ctx->vo->log);
CGLReleasePixelFormat(p->pix);
return true;
}
static void cocoa_uninit(MPGLContext *ctx)
{
struct priv *p = ctx->priv;
CGLReleaseContext(p->ctx);
vo_cocoa_uninit(ctx->vo);
}
static int cocoa_init(MPGLContext *ctx, int vo_flags)
{
struct priv *p = ctx->priv;
p->opts = mp_get_config_group(ctx, ctx->global, &cocoa_conf);
vo_cocoa_init(ctx->vo);
if (!create_gl_context(ctx, vo_flags))
return -1;
ctx->gl->SwapInterval = set_swap_interval;
return 0;
}
static int cocoa_reconfig(struct MPGLContext *ctx)
{
vo_cocoa_config_window(ctx->vo);
return 0;
}
static int cocoa_control(struct MPGLContext *ctx, int *events, int request,
void *arg)
{
return vo_cocoa_control(ctx->vo, events, request, arg);
}
static void cocoa_swap_buffers(struct MPGLContext *ctx)
{
vo_cocoa_swap_buffers(ctx->vo);
ctx->gl->Flush();
}
const struct mpgl_driver mpgl_driver_cocoa = {
.name = "cocoa",
.priv_size = sizeof(struct priv),
.init = cocoa_init,
.reconfig = cocoa_reconfig,
.swap_buffers = cocoa_swap_buffers,
.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
};