瀏覽代碼

Add GLFW_COCOA_GRAPHICS_SWICTHING

This provides control over whether the context should participate in
macOS Automatic Graphics Switching.

Closes #377.
Closes #935.
Camilla Löwy 8 年之前
父節點
當前提交
77a8f103d8
共有 7 個文件被更改,包括 47 次插入0 次删除
  1. 2 0
      README.md
  2. 18 0
      docs/window.dox
  3. 1 0
      include/GLFW/glfw3.h
  4. 8 0
      src/cocoa_window.m
  5. 3 0
      src/internal.h
  6. 12 0
      src/nsgl_context.m
  7. 3 0
      src/window.c

+ 2 - 0
README.md

@@ -133,6 +133,7 @@ information on what to include when reporting a bug.
   (#749,#842)
   (#749,#842)
 - Added macOS specific `GLFW_COCOA_RETINA_FRAMEBUFFER` window hint
 - Added macOS specific `GLFW_COCOA_RETINA_FRAMEBUFFER` window hint
 - Added macOS specific `GLFW_COCOA_FRAME_AUTOSAVE` window hint (#195)
 - 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
 - Added `GLFW_INCLUDE_ES32` for including the OpenGL ES 3.2 header
 - Removed `GLFW_USE_RETINA` compile-time option
 - Removed `GLFW_USE_RETINA` compile-time option
 - Bugfix: Calling `glfwMaximizeWindow` on a full screen window was not ignored
 - Bugfix: Calling `glfwMaximizeWindow` on a full screen window was not ignored
@@ -236,6 +237,7 @@ skills.
  - Cameron King
  - Cameron King
  - Peter Knut
  - Peter Knut
  - Christoph Kubisch
  - Christoph Kubisch
+ - Konstantin Käfer
  - Eric Larson
  - Eric Larson
  - Robin Leffmann
  - Robin Leffmann
  - Glenn Lewis
  - Glenn Lewis

+ 18 - 0
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
 using the window title specified at window creation.  This is ignored on other
 platforms.
 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
 @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_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_RETINA_FRAMEBUFFER | `GLFW_TRUE`                 | `GLFW_TRUE` or `GLFW_FALSE`
 GLFW_COCOA_FRAME_AUTOSAVE     | `GLFW_FALSE`                | `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
 @section window_events Window event processing

+ 1 - 0
include/GLFW/glfw3.h

@@ -852,6 +852,7 @@ extern "C" {
 
 
 #define GLFW_COCOA_RETINA_FRAMEBUFFER 0x00023001
 #define GLFW_COCOA_RETINA_FRAMEBUFFER 0x00023001
 #define GLFW_COCOA_FRAME_AUTOSAVE     0x00023002
 #define GLFW_COCOA_FRAME_AUTOSAVE     0x00023002
+#define GLFW_COCOA_GRAPHICS_SWITCHING 0x00023003
 /*! @} */
 /*! @} */
 
 
 #define GLFW_NO_API                          0
 #define GLFW_NO_API                          0

+ 8 - 0
src/cocoa_window.m

@@ -340,6 +340,14 @@ static const NSRange kEmptyRange = { NSNotFound, 0 };
 
 
 - (void)applicationDidChangeScreenParameters:(NSNotification *) notification
 - (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();
     _glfwPollMonitorsNS();
 }
 }
 
 

+ 3 - 0
src/internal.h

@@ -303,6 +303,9 @@ struct _GLFWctxconfig
     int           robustness;
     int           robustness;
     int           release;
     int           release;
     _GLFWwindow*  share;
     _GLFWwindow*  share;
+    struct {
+        GLFWbool  offline;
+    } nsgl;
 };
 };
 
 
 /*! @brief Framebuffer configuration.
 /*! @brief Framebuffer configuration.

+ 12 - 0
src/nsgl_context.m

@@ -160,6 +160,18 @@ GLFWbool _glfwCreateContextNSGL(_GLFWwindow* window,
     ADD_ATTR(NSOpenGLPFAAccelerated);
     ADD_ATTR(NSOpenGLPFAAccelerated);
     ADD_ATTR(NSOpenGLPFAClosestPolicy);
     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 MAC_OS_X_VERSION_MAX_ALLOWED >= 101000
     if (ctxconfig->major >= 4)
     if (ctxconfig->major >= 4)
     {
     {

+ 3 - 0
src/window.c

@@ -345,6 +345,9 @@ GLFWAPI void glfwWindowHint(int hint, int value)
         case GLFW_COCOA_FRAME_AUTOSAVE:
         case GLFW_COCOA_FRAME_AUTOSAVE:
             _glfw.hints.window.ns.frame = value ? GLFW_TRUE : GLFW_FALSE;
             _glfw.hints.window.ns.frame = value ? GLFW_TRUE : GLFW_FALSE;
             break;
             break;
+        case GLFW_COCOA_GRAPHICS_SWITCHING:
+            _glfw.hints.context.nsgl.offline = value ? GLFW_TRUE : GLFW_FALSE;
+            break;
         case GLFW_CENTER_CURSOR:
         case GLFW_CENTER_CURSOR:
             _glfw.hints.window.centerCursor = value ? GLFW_TRUE : GLFW_FALSE;
             _glfw.hints.window.centerCursor = value ? GLFW_TRUE : GLFW_FALSE;
             break;
             break;