From 77a8f103d8f8519d1a723bbd8b0490b448f1f430 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Camilla=20L=C3=B6wy?= Date: Fri, 27 Jan 2017 12:02:09 +0100 Subject: [PATCH] Add GLFW_COCOA_GRAPHICS_SWICTHING This provides control over whether the context should participate in macOS Automatic Graphics Switching. Closes #377. Closes #935. --- README.md | 2 ++ docs/window.dox | 18 ++++++++++++++++++ include/GLFW/glfw3.h | 1 + src/cocoa_window.m | 8 ++++++++ src/internal.h | 3 +++ src/nsgl_context.m | 12 ++++++++++++ src/window.c | 3 +++ 7 files changed, 47 insertions(+) diff --git a/README.md b/README.md index 5fb5c896..7edba5a2 100644 --- a/README.md +++ b/README.md @@ -133,6 +133,7 @@ information on what to include when reporting a bug. (#749,#842) - Added macOS specific `GLFW_COCOA_RETINA_FRAMEBUFFER` window hint - Added macOS specific `GLFW_COCOA_FRAME_AUTOSAVE` window hint (#195) +- Added macOS specific `GLFW_COCOA_GRAPHICS_SWITCHING` window hint (#377,#935) - Added `GLFW_INCLUDE_ES32` for including the OpenGL ES 3.2 header - Removed `GLFW_USE_RETINA` compile-time option - Bugfix: Calling `glfwMaximizeWindow` on a full screen window was not ignored @@ -236,6 +237,7 @@ skills. - Cameron King - Peter Knut - Christoph Kubisch + - Konstantin Käfer - Eric Larson - Robin Leffmann - Glenn Lewis diff --git a/docs/window.dox b/docs/window.dox index 544ecd79..0240cdfa 100644 --- a/docs/window.dox +++ b/docs/window.dox @@ -421,6 +421,23 @@ __GLFW_COCOA_FRAME_AUTOSAVE__ specifies whether to activate frame autosaving using the window title specified at window creation. This is ignored on other platforms. +@anchor GLFW_COCOA_GRAPHICS_SWITCHING_hint +__GLFW_COCOA_GRAPHICS_SWITCHING__ specifies whether to in Automatic Graphics +Switching, i.e. to allow the system to choose the integrated GPU for the OpenGL +context and move it between GPUs if necessary or whether to force it to always +run on the discrete GPU. This only affects systems with both integrated and +discrete GPUs. This is ignored on other platforms. + +@par +Simpler programs and tools may want to enable this to save power, while games +and other applications performing advanced rendering will want to leave it +disabled. + +@par +A bundled application that wishes to participate in Automatic Graphics Switching +should also declare this in its `Info.plist` by setting the +`NSSupportsAutomaticGraphicsSwitching` key to `true`. + @subsubsection window_hints_values Supported and default values @@ -461,6 +478,7 @@ GLFW_OPENGL_DEBUG_CONTEXT | `GLFW_FALSE` | `GLFW_TRUE` or `GL GLFW_OPENGL_PROFILE | `GLFW_OPENGL_ANY_PROFILE` | `GLFW_OPENGL_ANY_PROFILE`, `GLFW_OPENGL_COMPAT_PROFILE` or `GLFW_OPENGL_CORE_PROFILE` GLFW_COCOA_RETINA_FRAMEBUFFER | `GLFW_TRUE` | `GLFW_TRUE` or `GLFW_FALSE` GLFW_COCOA_FRAME_AUTOSAVE | `GLFW_FALSE` | `GLFW_TRUE` or `GLFW_FALSE` +GLFW_COCOA_GRAPHICS_SWITCHING | `GLFW_FALSE` | `GLFW_TRUE` or `GLFW_FALSE` @section window_events Window event processing diff --git a/include/GLFW/glfw3.h b/include/GLFW/glfw3.h index 280138b7..9bff088d 100644 --- a/include/GLFW/glfw3.h +++ b/include/GLFW/glfw3.h @@ -852,6 +852,7 @@ extern "C" { #define GLFW_COCOA_RETINA_FRAMEBUFFER 0x00023001 #define GLFW_COCOA_FRAME_AUTOSAVE 0x00023002 +#define GLFW_COCOA_GRAPHICS_SWITCHING 0x00023003 /*! @} */ #define GLFW_NO_API 0 diff --git a/src/cocoa_window.m b/src/cocoa_window.m index bd803c02..0f1f245a 100644 --- a/src/cocoa_window.m +++ b/src/cocoa_window.m @@ -340,6 +340,14 @@ static const NSRange kEmptyRange = { NSNotFound, 0 }; - (void)applicationDidChangeScreenParameters:(NSNotification *) notification { + _GLFWwindow* window; + + for (window = _glfw.windowListHead; window; window = window->next) + { + if (window->context.client != GLFW_NO_API) + [window->context.nsgl.object update]; + } + _glfwPollMonitorsNS(); } diff --git a/src/internal.h b/src/internal.h index d9b7e863..5ae0e777 100644 --- a/src/internal.h +++ b/src/internal.h @@ -303,6 +303,9 @@ struct _GLFWctxconfig int robustness; int release; _GLFWwindow* share; + struct { + GLFWbool offline; + } nsgl; }; /*! @brief Framebuffer configuration. diff --git a/src/nsgl_context.m b/src/nsgl_context.m index 5006781a..97663644 100644 --- a/src/nsgl_context.m +++ b/src/nsgl_context.m @@ -160,6 +160,18 @@ GLFWbool _glfwCreateContextNSGL(_GLFWwindow* window, ADD_ATTR(NSOpenGLPFAAccelerated); ADD_ATTR(NSOpenGLPFAClosestPolicy); + if (ctxconfig->nsgl.offline) + { + ADD_ATTR(NSOpenGLPFAAllowOfflineRenderers); + // NOTE: This replaces the NSSupportsAutomaticGraphicsSwitching key in + // Info.plist for unbundled applications + // HACK: This assumes that NSOpenGLPixelFormat will remain + // a straightforward wrapper of its CGL counterpart +#if MAC_OS_X_VERSION_MAX_ALLOWED >= 100800 + ADD_ATTR(kCGLPFASupportsAutomaticGraphicsSwitching); +#endif /*MAC_OS_X_VERSION_MAX_ALLOWED*/ + } + #if MAC_OS_X_VERSION_MAX_ALLOWED >= 101000 if (ctxconfig->major >= 4) { diff --git a/src/window.c b/src/window.c index 3662a4e6..f4cb182d 100644 --- a/src/window.c +++ b/src/window.c @@ -345,6 +345,9 @@ GLFWAPI void glfwWindowHint(int hint, int value) case GLFW_COCOA_FRAME_AUTOSAVE: _glfw.hints.window.ns.frame = value ? GLFW_TRUE : GLFW_FALSE; break; + case GLFW_COCOA_GRAPHICS_SWITCHING: + _glfw.hints.context.nsgl.offline = value ? GLFW_TRUE : GLFW_FALSE; + break; case GLFW_CENTER_CURSOR: _glfw.hints.window.centerCursor = value ? GLFW_TRUE : GLFW_FALSE; break;