Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

gpui_apple: Display P3 option for MetalRenderer's colorspace (#64394)

Matthew2d4c48319431
diff --git a/assets/settings/default.json b/assets/settings/default.json
index 2ba29d6f92d..bbe25523d24 100644
--- a/assets/settings/default.json
+++ b/assets/settings/default.json
@@ -240,6 +240,8 @@
// 3. Use grayscale text rendering:
// "text_rendering_mode": "grayscale"
"text_rendering_mode": "platform_default",
+ // Whether to render windows using the Display P3 color space (macOS only).
+ "use_display_p3": false,
// Whether to show padding for zoomed panels.
// When enabled, zoomed center panels (e.g. code editor) will have padding all around,
// while zoomed bottom/left/right panels will have padding to the top/right/left (respectively).
diff --git a/crates/gpui/src/platform.rs b/crates/gpui/src/platform.rs
index 193f94c601c..42841ac541b 100644
--- a/crates/gpui/src/platform.rs
+++ b/crates/gpui/src/platform.rs
@@ -2618,6 +2618,9 @@ pub struct WindowOptions {
/// Tab group name, allows opening the window as a native tab on macOS 10.12+. Windows with the same tabbing identifier will be grouped together.
pub tabbing_identifier: Option<String>,
+
+ /// Whether to render the window using the Display P3 color space on macOS.
+ pub use_display_p3: bool,
}
/// The variables that can be configured when creating a new window
@@ -2683,6 +2686,9 @@ pub struct WindowParams {
#[cfg(target_os = "macos")]
pub tabbing_identifier: Option<String>,
+
+ #[cfg(target_os = "macos")]
+ pub use_display_p3: bool,
}
/// Represents the status of how a window should be opened.
@@ -2744,6 +2750,7 @@ impl Default for WindowOptions {
window_min_size: None,
window_decorations: None,
tabbing_identifier: None,
+ use_display_p3: false,
}
}
}
diff --git a/crates/gpui/src/window.rs b/crates/gpui/src/window.rs
index 33fa14bfea6..9d861155bcd 100644
--- a/crates/gpui/src/window.rs
+++ b/crates/gpui/src/window.rs
@@ -1607,6 +1607,8 @@ impl Window {
icon,
#[cfg_attr(not(target_os = "macos"), allow(unused_variables))]
tabbing_identifier,
+ #[cfg_attr(not(target_os = "macos"), allow(unused_variables))]
+ use_display_p3,
} = options;
let initial_window_title = titlebar
@@ -1632,6 +1634,8 @@ impl Window {
icon,
#[cfg(target_os = "macos")]
tabbing_identifier,
+ #[cfg(target_os = "macos")]
+ use_display_p3,
},
)?;
diff --git a/crates/gpui_apple/src/metal_renderer.rs b/crates/gpui_apple/src/metal_renderer.rs
index e29d61c537b..862e62a54ba 100644
--- a/crates/gpui_apple/src/metal_renderer.rs
+++ b/crates/gpui_apple/src/metal_renderer.rs
@@ -10,6 +10,7 @@ use gpui::{
use image::RgbaImage;
use objc2::runtime::AnyObject;
+use core_graphics::color_space::{CGColorSpace, kCGColorSpaceDisplayP3};
use foreign_types::{ForeignType, ForeignTypeRef};
use metal::{
CAMetalLayer, CommandQueue, MTLGPUFamily, MTLPixelFormat, MTLResourceOptions, NSRange,
@@ -48,8 +49,9 @@ pub unsafe fn new_renderer(
_native_view: *mut c_void,
_bounds: gpui::Size<f32>,
transparent: bool,
+ use_display_p3: bool,
) -> Renderer {
- MetalRenderer::new(context, transparent)
+ MetalRenderer::new(context, transparent, use_display_p3)
}
pub struct InstanceBufferPool {
@@ -148,10 +150,14 @@ pub struct PathRasterizationVertex {
impl MetalRenderer {
/// Creates a new MetalRenderer with a CAMetalLayer for window-based rendering.
- pub fn new(instance_buffer_pool: Arc<Mutex<InstanceBufferPool>>, transparent: bool) -> Self {
+ pub fn new(
+ instance_buffer_pool: Arc<Mutex<InstanceBufferPool>>,
+ transparent: bool,
+ use_display_p3: bool,
+ ) -> Self {
let device = Self::create_device();
let layer = metal::MetalLayer::new();
- Self::configure_layer(&layer, &device, transparent);
+ Self::configure_layer(&layer, &device, transparent, use_display_p3);
Self::new_internal(device, Some(layer), !transparent, instance_buffer_pool)
}
@@ -170,13 +176,38 @@ impl MetalRenderer {
metal::MetalLayerRef::from_ptr(ptr::from_ref(layer).cast_mut().cast::<CAMetalLayer>())
}
.to_owned();
- Self::configure_layer(&layer, &device, transparent);
+ Self::configure_layer(&layer, &device, transparent, false);
Self::new_internal(device, Some(layer), !transparent, instance_buffer_pool)
}
- fn configure_layer(layer: &metal::MetalLayerRef, device: &metal::DeviceRef, transparent: bool) {
+ fn configure_layer(
+ layer: &metal::MetalLayerRef,
+ device: &metal::DeviceRef,
+ transparent: bool,
+ use_display_p3: bool,
+ ) {
layer.set_device(device);
layer.set_pixel_format(MTLPixelFormat::BGRA8Unorm);
+ // metal-rs doesn't bind these setters, so view the same object through
+ // objc2's typed CAMetalLayer binding.
+ let objc2_layer: &objc2_quartz_core::CAMetalLayer = unsafe { &*layer.as_ptr().cast() };
+ if use_display_p3 {
+ // SAFETY: The constant is extern, from Core Graphics
+ let display_p3_name = unsafe { kCGColorSpaceDisplayP3 };
+ if let Some(display_p3_color_space) = CGColorSpace::create_with_name(display_p3_name) {
+ // SAFETY: `layer` is a CAMetalLayer and the color space remains valid for the call.
+ // 'metal-rs' does not expose CAMetalLayer's typed `colorspace` setter.
+ // Could be replaced with a native binding via objc2-* in future
+ unsafe {
+ let _: () = objc2::msg_send![
+ objc2_layer,
+ setColorspace: display_p3_color_space.as_ptr().cast::<c_void>()
+ ];
+ }
+ } else {
+ log::error!("unable to create the Display P3 color space");
+ }
+ }
// Support direct-to-display rendering if the window is not transparent
// https://developer.apple.com/documentation/metal/managing-your-game-window-for-metal-in-macos
layer.set_opaque(!transparent);
@@ -186,9 +217,6 @@ impl MetalRenderer {
// presentation cost, even when `test-support` is compiled in.
#[cfg(all(feature = "test-support", debug_assertions))]
layer.set_framebuffer_only(false);
- // metal-rs doesn't bind these setters, so view the same object through
- // objc2's typed CAMetalLayer binding.
- let objc2_layer: &objc2_quartz_core::CAMetalLayer = unsafe { &*layer.as_ptr().cast() };
objc2_layer.setAllowsNextDrawableTimeout(false);
objc2_layer.setNeedsDisplayOnBoundsChange(true);
// UIKit sizes a view's backing layer itself; only AppKit-hosted
diff --git a/crates/gpui_macos/src/window.rs b/crates/gpui_macos/src/window.rs
index b04282552d0..dbbf295b693 100644
--- a/crates/gpui_macos/src/window.rs
+++ b/crates/gpui_macos/src/window.rs
@@ -973,6 +973,7 @@ impl MacWindow {
display_id,
window_min_size,
tabbing_identifier,
+ use_display_p3,
..
}: WindowParams,
cursor_visible: Arc<AtomicBool>,
@@ -1106,6 +1107,7 @@ impl MacWindow {
native_view as *mut _,
bounds.size.map(|pixels| pixels.as_f32()),
false,
+ use_display_p3,
),
request_frame_callback: None,
event_callback: None,
diff --git a/crates/settings/src/vscode_import.rs b/crates/settings/src/vscode_import.rs
index 1b5c2fd660e..3cc073635e9 100644
--- a/crates/settings/src/vscode_import.rs
+++ b/crates/settings/src/vscode_import.rs
@@ -1053,6 +1053,7 @@ impl VsCodeSettings {
active_pane_modifiers: self.active_pane_modifiers(),
accessible_mode: None,
text_rendering_mode: None,
+ use_display_p3: None,
autosave: self.read_enum("files.autoSave", |s| match s {
"off" => Some(AutosaveSetting::Off),
"afterDelay" => Some(AutosaveSetting::AfterDelay {
diff --git a/crates/settings_content/src/workspace.rs b/crates/settings_content/src/workspace.rs
index dbcb6fc4143..0a6429ec2e5 100644
--- a/crates/settings_content/src/workspace.rs
+++ b/crates/settings_content/src/workspace.rs
@@ -19,6 +19,10 @@ pub struct WorkspaceSettingsContent {
///
/// Default: platform_default
pub text_rendering_mode: Option<TextRenderingMode>,
+ /// Whether to render windows using the Display P3 color space (macOS only).
+ ///
+ /// Default: false
+ pub use_display_p3: Option<bool>,
/// Layout mode for the bottom dock
///
/// Default: contained
diff --git a/crates/settings_ui/src/page_data.rs b/crates/settings_ui/src/page_data.rs
index 24b82e56d0e..138acacbc81 100644
--- a/crates/settings_ui/src/page_data.rs
+++ b/crates/settings_ui/src/page_data.rs
@@ -1393,7 +1393,7 @@ fn appearance_page() -> SettingsPage {
]
}
- fn text_rendering_section() -> [SettingsPageItem; 2] {
+ fn text_rendering_section() -> [SettingsPageItem; 3] {
[
SettingsPageItem::SectionHeader("Text Rendering"),
SettingsPageItem::SettingItem(SettingItem {
@@ -1412,6 +1412,20 @@ fn appearance_page() -> SettingsPage {
metadata: None,
files: USER,
}),
+ SettingsPageItem::SettingItem(SettingItem {
+ title: "Display P3",
+ description: "(macOS only) render windows using the Display P3 color space.",
+ field: Box::new(SettingField {
+ organization_override: None,
+ json_path: Some("use_display_p3"),
+ pick: |settings_content| settings_content.workspace.use_display_p3.as_ref(),
+ write: |settings_content, value, _| {
+ settings_content.workspace.use_display_p3 = value;
+ },
+ }),
+ metadata: None,
+ files: USER,
+ }),
]
}
diff --git a/crates/workspace/src/workspace_settings.rs b/crates/workspace/src/workspace_settings.rs
index eff3a0a93cc..7bb4189d990 100644
--- a/crates/workspace/src/workspace_settings.rs
+++ b/crates/workspace/src/workspace_settings.rs
@@ -36,6 +36,7 @@ pub struct WorkspaceSettings {
pub on_new_window: settings::OnNewWindow,
pub on_last_window_closed: settings::OnLastWindowClosed,
pub text_rendering_mode: settings::TextRenderingMode,
+ pub use_display_p3: bool,
pub resize_all_panels_in_dock: Vec<DockPosition>,
pub close_on_file_delete: bool,
pub close_panel_on_toggle: bool,
@@ -135,6 +136,7 @@ impl Settings for WorkspaceSettings {
on_new_window: workspace.on_new_window.unwrap(),
on_last_window_closed: workspace.on_last_window_closed.unwrap(),
text_rendering_mode: workspace.text_rendering_mode.unwrap(),
+ use_display_p3: workspace.use_display_p3.unwrap(),
resize_all_panels_in_dock: workspace
.resize_all_panels_in_dock
.clone()
diff --git a/crates/zed/src/zed.rs b/crates/zed/src/zed.rs
index 3f20c8cf24f..960f4d692c5 100644
--- a/crates/zed/src/zed.rs
+++ b/crates/zed/src/zed.rs
@@ -422,6 +422,7 @@ pub fn build_window_options(display_uuid: Option<Uuid>, cx: &mut App) -> WindowO
} else {
None
},
+ use_display_p3: WorkspaceSettings::get_global(cx).use_display_p3,
..Default::default()
}
}