From 2292501533f0f5241573a6117be2c719a6740806 Mon Sep 17 00:00:00 2001 From: Akemi Date: Mon, 20 Feb 2017 20:34:57 +0100 Subject: cocoa: add option to force dedicated GPU Fixes #3242 --- DOCS/man/options.rst | 6 +++++ options/options.c | 5 +++++ options/options.h | 1 + video/out/opengl/context_cocoa.c | 47 +++++++++++++++++++++++++++------------- 4 files changed, 44 insertions(+), 15 deletions(-) diff --git a/DOCS/man/options.rst b/DOCS/man/options.rst index 093fb2b31e..3c2a404189 100644 --- a/DOCS/man/options.rst +++ b/DOCS/man/options.rst @@ -4401,6 +4401,12 @@ The following video options are currently all specific to ``--vo=opengl`` and Windows 8+ with ANGLE only. +``--cocoa-force-dedicated-gpu=`` + Deactivates the automatic graphics switching and forces the dedicated GPU. + (default: no) + + OS X only. + ``--opengl-sw`` Continue even if a software renderer is detected. diff --git a/options/options.c b/options/options.c index d33c2e05d1..7723ddec50 100644 --- a/options/options.c +++ b/options/options.c @@ -92,6 +92,7 @@ extern const struct m_obj_list vo_obj_list; extern const struct m_obj_list ao_obj_list; extern const struct m_sub_options angle_conf; +extern const struct m_sub_options cocoa_conf; const struct m_opt_choice_alternatives mp_hwdec_names[] = { {"no", HWDEC_NONE}, @@ -708,6 +709,10 @@ const m_option_t mp_opts[] = { OPT_SUBSTRUCT("", angle_opts, angle_conf, 0), #endif +#if HAVE_GL_COCOA + OPT_SUBSTRUCT("", cocoa_opts, cocoa_conf, 0), +#endif + #if HAVE_GL_WIN32 OPT_CHOICE("opengl-dwmflush", wingl_dwm_flush, 0, ({"no", -1}, {"auto", 0}, {"windowed", 1}, {"yes", 2})), diff --git a/options/options.h b/options/options.h index 926b238b06..e8014d5ac0 100644 --- a/options/options.h +++ b/options/options.h @@ -327,6 +327,7 @@ typedef struct MPOpts { struct gl_video_opts *gl_video_opts; struct angle_opts *angle_opts; + struct cocoa_opts *cocoa_opts; struct dvd_opts *dvd_opts; } MPOpts; diff --git a/video/out/opengl/context_cocoa.c b/video/out/opengl/context_cocoa.c index 86a25e9a1f..c61f9e282d 100644 --- a/video/out/opengl/context_cocoa.c +++ b/video/out/opengl/context_cocoa.c @@ -17,13 +17,29 @@ #include #include +#include "options/m_config.h" #include "video/out/cocoa_common.h" #include "osdep/macosx_versions.h" #include "context.h" -struct cgl_context { +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) @@ -46,14 +62,13 @@ static void *cocoa_glgetaddr(const char *s) return ret; } -static CGLError test_gl_version(struct vo *vo, - CGLContextObj *ctx, - CGLPixelFormatObj *pix, - CGLOpenGLProfile version) +static CGLError test_gl_version(struct MPGLContext *ctx, CGLOpenGLProfile ver) { + struct priv *p = ctx->priv; + CGLPixelFormatAttribute attrs[] = { kCGLPFAOpenGLProfile, - (CGLPixelFormatAttribute) version, + (CGLPixelFormatAttribute) ver, kCGLPFAAccelerated, // leave this as the last entry of the array to not break the fallback // code @@ -63,21 +78,21 @@ static CGLError test_gl_version(struct vo *vo, GLint npix; CGLError err; - err = CGLChoosePixelFormat(attrs, pix, &npix); - if (err == kCGLBadAttribute) { + err = CGLChoosePixelFormat(attrs, &p->pix, &npix); + if (p->opts->cocoa_force_dedicated_gpu || err == kCGLBadAttribute) { // kCGLPFASupportsAutomaticGraphicsSwitching is probably not supported // by the current hardware. Falling back to not using it. attrs[MP_ARRAY_SIZE(attrs) - 2] = 0; - err = CGLChoosePixelFormat(attrs, pix, &npix); + err = CGLChoosePixelFormat(attrs, &p->pix, &npix); } if (err != kCGLNoError) { - MP_ERR(vo, "error creating CGL pixel format: %s (%d)\n", + MP_ERR(ctx->vo, "error creating CGL pixel format: %s (%d)\n", CGLErrorString(err), err); goto error_out; } - err = CGLCreateContext(*pix, 0, ctx); + err = CGLCreateContext(p->pix, 0, &p->ctx); error_out: return err; @@ -85,7 +100,7 @@ error_out: static bool create_gl_context(struct MPGLContext *ctx, int vo_flags) { - struct cgl_context *p = ctx->priv; + struct priv *p = ctx->priv; CGLError err; CGLOpenGLProfile gl_versions[] = { @@ -94,7 +109,7 @@ static bool create_gl_context(struct MPGLContext *ctx, int vo_flags) }; for (int n = 0; n < MP_ARRAY_SIZE(gl_versions); n++) { - err = test_gl_version(ctx->vo, &p->ctx, &p->pix, gl_versions[n]); + err = test_gl_version(ctx, gl_versions[n]); if (err == kCGLNoError) break; } @@ -120,13 +135,15 @@ static bool create_gl_context(struct MPGLContext *ctx, int vo_flags) static void cocoa_uninit(MPGLContext *ctx) { - struct cgl_context *p = ctx->priv; + 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)) @@ -156,7 +173,7 @@ static void cocoa_swap_buffers(struct MPGLContext *ctx) const struct mpgl_driver mpgl_driver_cocoa = { .name = "cocoa", - .priv_size = sizeof(struct cgl_context), + .priv_size = sizeof(struct priv), .init = cocoa_init, .reconfig = cocoa_reconfig, .swap_buffers = cocoa_swap_buffers, -- cgit v1.2.3