1
0
mirror of https://github.com/mpv-player/mpv synced 2025-01-24 08:33:34 +00:00

cocoa: add option to force dedicated GPU

Fixes #3242
This commit is contained in:
Akemi 2017-02-20 20:34:57 +01:00
parent b5ca8c41cc
commit 2292501533
4 changed files with 44 additions and 15 deletions

View File

@ -4401,6 +4401,12 @@ The following video options are currently all specific to ``--vo=opengl`` and
Windows 8+ with ANGLE only. Windows 8+ with ANGLE only.
``--cocoa-force-dedicated-gpu=<yes|no>``
Deactivates the automatic graphics switching and forces the dedicated GPU.
(default: no)
OS X only.
``--opengl-sw`` ``--opengl-sw``
Continue even if a software renderer is detected. Continue even if a software renderer is detected.

View File

@ -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_obj_list ao_obj_list;
extern const struct m_sub_options angle_conf; 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[] = { const struct m_opt_choice_alternatives mp_hwdec_names[] = {
{"no", HWDEC_NONE}, {"no", HWDEC_NONE},
@ -708,6 +709,10 @@ const m_option_t mp_opts[] = {
OPT_SUBSTRUCT("", angle_opts, angle_conf, 0), OPT_SUBSTRUCT("", angle_opts, angle_conf, 0),
#endif #endif
#if HAVE_GL_COCOA
OPT_SUBSTRUCT("", cocoa_opts, cocoa_conf, 0),
#endif
#if HAVE_GL_WIN32 #if HAVE_GL_WIN32
OPT_CHOICE("opengl-dwmflush", wingl_dwm_flush, 0, OPT_CHOICE("opengl-dwmflush", wingl_dwm_flush, 0,
({"no", -1}, {"auto", 0}, {"windowed", 1}, {"yes", 2})), ({"no", -1}, {"auto", 0}, {"windowed", 1}, {"yes", 2})),

View File

@ -327,6 +327,7 @@ typedef struct MPOpts {
struct gl_video_opts *gl_video_opts; struct gl_video_opts *gl_video_opts;
struct angle_opts *angle_opts; struct angle_opts *angle_opts;
struct cocoa_opts *cocoa_opts;
struct dvd_opts *dvd_opts; struct dvd_opts *dvd_opts;
} MPOpts; } MPOpts;

View File

@ -17,13 +17,29 @@
#include <OpenGL/OpenGL.h> #include <OpenGL/OpenGL.h>
#include <dlfcn.h> #include <dlfcn.h>
#include "options/m_config.h"
#include "video/out/cocoa_common.h" #include "video/out/cocoa_common.h"
#include "osdep/macosx_versions.h" #include "osdep/macosx_versions.h"
#include "context.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; CGLPixelFormatObj pix;
CGLContextObj ctx; CGLContextObj ctx;
struct cocoa_opts *opts;
}; };
static int set_swap_interval(int enabled) static int set_swap_interval(int enabled)
@ -46,14 +62,13 @@ static void *cocoa_glgetaddr(const char *s)
return ret; return ret;
} }
static CGLError test_gl_version(struct vo *vo, static CGLError test_gl_version(struct MPGLContext *ctx, CGLOpenGLProfile ver)
CGLContextObj *ctx,
CGLPixelFormatObj *pix,
CGLOpenGLProfile version)
{ {
struct priv *p = ctx->priv;
CGLPixelFormatAttribute attrs[] = { CGLPixelFormatAttribute attrs[] = {
kCGLPFAOpenGLProfile, kCGLPFAOpenGLProfile,
(CGLPixelFormatAttribute) version, (CGLPixelFormatAttribute) ver,
kCGLPFAAccelerated, kCGLPFAAccelerated,
// leave this as the last entry of the array to not break the fallback // leave this as the last entry of the array to not break the fallback
// code // code
@ -63,21 +78,21 @@ static CGLError test_gl_version(struct vo *vo,
GLint npix; GLint npix;
CGLError err; CGLError err;
err = CGLChoosePixelFormat(attrs, pix, &npix); err = CGLChoosePixelFormat(attrs, &p->pix, &npix);
if (err == kCGLBadAttribute) { if (p->opts->cocoa_force_dedicated_gpu || err == kCGLBadAttribute) {
// kCGLPFASupportsAutomaticGraphicsSwitching is probably not supported // kCGLPFASupportsAutomaticGraphicsSwitching is probably not supported
// by the current hardware. Falling back to not using it. // by the current hardware. Falling back to not using it.
attrs[MP_ARRAY_SIZE(attrs) - 2] = 0; attrs[MP_ARRAY_SIZE(attrs) - 2] = 0;
err = CGLChoosePixelFormat(attrs, pix, &npix); err = CGLChoosePixelFormat(attrs, &p->pix, &npix);
} }
if (err != kCGLNoError) { 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); CGLErrorString(err), err);
goto error_out; goto error_out;
} }
err = CGLCreateContext(*pix, 0, ctx); err = CGLCreateContext(p->pix, 0, &p->ctx);
error_out: error_out:
return err; return err;
@ -85,7 +100,7 @@ error_out:
static bool create_gl_context(struct MPGLContext *ctx, int vo_flags) static bool create_gl_context(struct MPGLContext *ctx, int vo_flags)
{ {
struct cgl_context *p = ctx->priv; struct priv *p = ctx->priv;
CGLError err; CGLError err;
CGLOpenGLProfile gl_versions[] = { 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++) { 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) if (err == kCGLNoError)
break; break;
} }
@ -120,13 +135,15 @@ static bool create_gl_context(struct MPGLContext *ctx, int vo_flags)
static void cocoa_uninit(MPGLContext *ctx) static void cocoa_uninit(MPGLContext *ctx)
{ {
struct cgl_context *p = ctx->priv; struct priv *p = ctx->priv;
CGLReleaseContext(p->ctx); CGLReleaseContext(p->ctx);
vo_cocoa_uninit(ctx->vo); vo_cocoa_uninit(ctx->vo);
} }
static int cocoa_init(MPGLContext *ctx, int vo_flags) 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); vo_cocoa_init(ctx->vo);
if (!create_gl_context(ctx, vo_flags)) 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 = { const struct mpgl_driver mpgl_driver_cocoa = {
.name = "cocoa", .name = "cocoa",
.priv_size = sizeof(struct cgl_context), .priv_size = sizeof(struct priv),
.init = cocoa_init, .init = cocoa_init,
.reconfig = cocoa_reconfig, .reconfig = cocoa_reconfig,
.swap_buffers = cocoa_swap_buffers, .swap_buffers = cocoa_swap_buffers,