|
1846 | 1846 |
|
1847 | 1847 | p_screen = _get_screen_index(p_screen); |
1848 | 1848 | NSScreen *screen = NSScreen.screens[p_screen]; |
1849 | | - return screen.maximumExtendedDynamicRangeColorComponentValue * 100.0; |
| 1849 | + |
| 1850 | + // Note: this absolute nits value is incorrect when reference luminance is below 100 nits. |
| 1851 | + return screen.maximumPotentialExtendedDynamicRangeColorComponentValue * 100.0f; |
1850 | 1852 | } |
1851 | 1853 |
|
1852 | 1854 | float DisplayServerMacOS::screen_get_max_full_frame_luminance(int p_screen) const { |
1853 | 1855 | _THREAD_SAFE_METHOD_ |
1854 | 1856 |
|
1855 | | - p_screen = _get_screen_index(p_screen); |
1856 | | - NSScreen *screen = NSScreen.screens[p_screen]; |
1857 | | - return screen.maximumExtendedDynamicRangeColorComponentValue * 100.0; |
| 1857 | + return screen_get_reference_luminance(p_screen); // macOS provides no way of reading this information about the screen. |
1858 | 1858 | } |
1859 | 1859 |
|
1860 | 1860 | float DisplayServerMacOS::screen_get_reference_luminance(int p_screen) const { |
1861 | 1861 | // Per https://developer.apple.com/documentation/metal/performing-your-own-tone-mapping?language=objc#Understand-EDR-Pixel-Values, |
1862 | 1862 | // the SDR white level is always 1.0. |
1863 | | - return 100.0f; // 100 nits. |
| 1863 | + |
| 1864 | + p_screen = _get_screen_index(p_screen); |
| 1865 | + NSScreen *screen = NSScreen.screens[p_screen]; |
| 1866 | + |
| 1867 | + // Note: this absolute nits value is incorrect when reference luminance is below 100 nits. |
| 1868 | + float max_luminance = screen.maximumPotentialExtendedDynamicRangeColorComponentValue * 100.0f; |
| 1869 | + |
| 1870 | + float maximum_value = screen.maximumReferenceExtendedDynamicRangeColorComponentValue; |
| 1871 | + if (maximum_value <= 0.0) { |
| 1872 | + maximum_value = screen.maximumExtendedDynamicRangeColorComponentValue; |
| 1873 | + } |
| 1874 | + |
| 1875 | + // Note: this absolute nits value is incorrect when reference luminance is below 100 nits. |
| 1876 | + return max_luminance / maximum_value; |
1864 | 1877 | } |
1865 | 1878 |
|
1866 | 1879 | Vector<DisplayServer::WindowID> DisplayServerMacOS::get_window_list() const { |
|
3011 | 3024 | #pragma clang diagnostic push |
3012 | 3025 | #pragma clang diagnostic ignored "-Wunused-variable" |
3013 | 3026 |
|
3014 | | - CGFloat max_edr_ccv = screen.maximumExtendedDynamicRangeColorComponentValue * 100.0f; // Convert to nits. |
3015 | | - rendering_context->window_set_hdr_output_max_luminance(p_window, max_edr_ccv); |
| 3027 | + // Note: this absolute nits value is incorrect when reference luminance is below 100 nits. |
| 3028 | + float max_luminance = screen.maximumPotentialExtendedDynamicRangeColorComponentValue * 100.0f; |
| 3029 | + |
| 3030 | + float maximum_value = screen.maximumReferenceExtendedDynamicRangeColorComponentValue; |
| 3031 | + if (maximum_value <= 0.0) { |
| 3032 | + maximum_value = screen.maximumExtendedDynamicRangeColorComponentValue; |
| 3033 | + } |
| 3034 | + |
| 3035 | + // Note: this absolute nits value is incorrect when reference luminance is below 100 nits. |
| 3036 | + float reference_luminance = max_luminance / maximum_value; |
| 3037 | + |
| 3038 | + rendering_context->window_set_hdr_output_reference_luminance(p_window, reference_luminance); |
| 3039 | + |
| 3040 | + if (p_wd.hdr_output_auto_adjust_max_luminance) { |
| 3041 | + rendering_context->window_set_hdr_output_max_luminance(p_window, max_luminance); |
| 3042 | + } |
3016 | 3043 |
|
3017 | | - // TODO(sgc): Is this correct? 203 nits is also a common reference luminance. |
3018 | | - rendering_context->window_set_hdr_output_reference_luminance(p_window, 100.0); |
3019 | 3044 | #pragma clang diagnostic pop |
3020 | 3045 | } |
3021 | 3046 | #endif |
|
3083 | 3108 | } |
3084 | 3109 |
|
3085 | 3110 | void DisplayServerMacOS::window_set_hdr_output_auto_adjust_reference_luminance(const bool p_enabled, WindowID p_window) { |
3086 | | - // Apple platforms only support using screen luminance |
| 3111 | + // Apple platforms only support using system reference luminance. |
3087 | 3112 | } |
3088 | 3113 |
|
3089 | 3114 | bool DisplayServerMacOS::window_is_hdr_output_auto_adjusting_reference_luminance(WindowID p_window) const { |
3090 | | - ERR_FAIL_V_MSG(true, "Not implemented"); |
| 3115 | + return true; |
3091 | 3116 | } |
3092 | 3117 |
|
3093 | 3118 | void DisplayServerMacOS::window_set_hdr_output_reference_luminance(const float p_reference_luminance, WindowID p_window) { |
3094 | | - _THREAD_SAFE_METHOD_ |
3095 | | - |
3096 | | -#if defined(RD_ENABLED) |
3097 | | - if (rendering_context) { |
3098 | | - rendering_context->window_set_hdr_output_reference_luminance(p_window, p_reference_luminance); |
3099 | | - } |
3100 | | -#endif |
| 3119 | + // Apple platforms do not support adjusting reference luminance because |
| 3120 | + // there are easy to find user-facing brightness settings built into the OS. |
| 3121 | + ERR_FAIL_MSG("Cannot set reference luminance on Mac OS; use system brightness setting instead."); |
3101 | 3122 | } |
3102 | 3123 |
|
3103 | 3124 | float DisplayServerMacOS::window_get_hdr_output_reference_luminance(WindowID p_window) const { |
|
3112 | 3133 | } |
3113 | 3134 |
|
3114 | 3135 | void DisplayServerMacOS::window_set_hdr_output_auto_adjust_max_luminance(const bool p_enabled, WindowID p_window) { |
3115 | | - // Apple platforms only support using screen luminance |
| 3136 | + _THREAD_SAFE_METHOD_ |
| 3137 | + |
| 3138 | + WindowData &wd = windows[p_window]; |
| 3139 | + if (wd.hdr_output_auto_adjust_max_luminance == p_enabled) { |
| 3140 | + return; |
| 3141 | + } |
| 3142 | + |
| 3143 | + wd.hdr_output_auto_adjust_max_luminance = p_enabled; |
| 3144 | + |
| 3145 | + if (wd.hdr_output_auto_adjust_max_luminance) { |
| 3146 | + WindowData *wd = windows.getptr(p_window); |
| 3147 | + ERR_FAIL_NULL(wd); |
| 3148 | + wd->hdr_output_requested = p_enabled; |
| 3149 | + _update_hdr_output_for_window(p_window, *wd); |
| 3150 | + } else { |
| 3151 | + // Apply the previously set maximum luminance. |
| 3152 | +#if defined(RD_ENABLED) |
| 3153 | + if (rendering_context) { |
| 3154 | + rendering_context->window_set_hdr_output_max_luminance(p_window, wd.hdr_output_max_luminance); |
| 3155 | + } |
| 3156 | +#endif |
| 3157 | + } |
3116 | 3158 | } |
3117 | 3159 |
|
3118 | 3160 | bool DisplayServerMacOS::window_is_hdr_output_auto_adjusting_max_luminance(WindowID p_window) const { |
3119 | | - ERR_FAIL_V_MSG(true, "Not implemented"); |
| 3161 | + _THREAD_SAFE_METHOD_ |
| 3162 | + |
| 3163 | + const WindowData &wd = windows[p_window]; |
| 3164 | + return wd.hdr_output_auto_adjust_max_luminance; |
3120 | 3165 | } |
3121 | 3166 |
|
3122 | 3167 | void DisplayServerMacOS::window_set_hdr_output_max_luminance(const float p_max_luminance, WindowID p_window) { |
| 3168 | + _THREAD_SAFE_METHOD_ |
| 3169 | + |
| 3170 | + ERR_FAIL_COND_MSG(p_max_luminance < 0.0f, "Maximum luminance must be non-negative."); |
| 3171 | + |
| 3172 | + WindowData &wd = windows[p_window]; |
| 3173 | + wd.hdr_output_max_luminance = p_max_luminance; |
| 3174 | + |
| 3175 | + // If the maximum luminance is set to auto-adjust, don't apply it. |
| 3176 | + if (wd.hdr_output_auto_adjust_max_luminance) { |
| 3177 | + return; |
| 3178 | + } |
| 3179 | + |
| 3180 | +#if defined(RD_ENABLED) |
| 3181 | + if (rendering_context) { |
| 3182 | + rendering_context->window_set_hdr_output_max_luminance(p_window, p_max_luminance); |
| 3183 | + } |
| 3184 | +#endif |
3123 | 3185 | } |
3124 | 3186 |
|
3125 | 3187 | float DisplayServerMacOS::window_get_hdr_output_max_luminance(WindowID p_window) const { |
|
3134 | 3196 | } |
3135 | 3197 |
|
3136 | 3198 | float DisplayServerMacOS::window_get_output_max_value(WindowID p_window) const { |
3137 | | - // allenwp: temporary rebase hack (this should return 1.0 when HDR is disabled. |
3138 | | - // Also, this whole function should probably be handled in a driver independent way. |
3139 | | - return window_get_hdr_output_max_luminance(p_window) / window_get_hdr_output_reference_luminance(p_window); |
| 3199 | + _THREAD_SAFE_METHOD_ |
| 3200 | + |
| 3201 | +#if defined(RD_ENABLED) |
| 3202 | + if (rendering_context) { |
| 3203 | + return rendering_context->window_get_output_max_value(p_window); |
| 3204 | + } |
| 3205 | +#endif |
| 3206 | + |
| 3207 | + return 1.0f; // SDR |
3140 | 3208 | } |
3141 | 3209 |
|
3142 | 3210 | int DisplayServerMacOS::accessibility_should_increase_contrast() const { |
|
0 commit comments