cocoa: fix the support of multiple renderers (GPU switch)

So far, switching between integrated and discrete GPU would cause the
kernel to kill mpv due to an indecipherable buffer error. The technical
note TN2229 from Apple recommends to enable OpenGL Offline Renderers for
every Mac with more GPUs than displays to handle the switch between GPU.

By ordering the array from the least commonly rejected to the most,
we can sequentially remove PixelFormat attributes to fit the host.

Fixes #2371
This commit is contained in:
Alex Notes 2017-07-27 14:37:58 +02:00 committed by Akemi
parent 80758eda17
commit bda32d99d7
1 changed files with 15 additions and 6 deletions

View File

@ -67,22 +67,31 @@ 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,
// leave this as the last entry of the array to not break the fallback
// code
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);
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;
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);
}