Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Extract shared Apple renderer from gpui_macos (#62649)
Anthony Eid52b2418110ee
diff --git a/Cargo.lock b/Cargo.lockindex 06575e38333..ec12c526500 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -7917,6 +7917,28 @@ dependencies = ["ztracing",]+[[package]]+name = "gpui_apple"+version = "0.1.0"+dependencies = [+ "anyhow",+ "block",+ "cbindgen",+ "cocoa 0.26.0",+ "collections",+ "core-foundation 0.10.0",+ "core-video",+ "derive_more",+ "etagere",+ "foreign-types 0.5.0",+ "gpui",+ "image",+ "log",+ "metal",+ "objc",+ "parking_lot",+]+[[package]]name = "gpui_linux"version = "0.1.0"@@ -7978,21 +8000,18 @@ dependencies = ["async-task","block","block2 0.6.2",- "cbindgen","cocoa 0.26.0","collections","core-foundation 0.10.0","core-foundation-sys","core-graphics 0.24.0","core-text",- "core-video","ctor",- "derive_more","dispatch2",- "etagere","foreign-types 0.5.0","futures 0.3.32","gpui",+ "gpui_apple","gpui_util","image","itertools 0.14.0",diff --git a/Cargo.toml b/Cargo.tomlindex 9af94b2395a..96717ccea25 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -91,6 +91,7 @@ members = ["crates/google_ai","crates/grammars","crates/gpui",+ "crates/gpui_apple","crates/gpui_shared_string","crates/gpui_linux","crates/gpui_macos",@@ -353,6 +354,7 @@ go_to_line = { path = "crates/go_to_line" }google_ai = { path = "crates/google_ai" }grammars = { path = "crates/grammars" }gpui = { path = "crates/gpui", default-features = false }+gpui_apple = { path = "crates/gpui_apple" }gpui_shared_string = { path = "crates/gpui_shared_string" }gpui_linux = { path = "crates/gpui_linux", default-features = false }gpui_macos = { path = "crates/gpui_macos", default-features = false }diff --git a/crates/gpui_apple/Cargo.toml b/crates/gpui_apple/Cargo.tomlnew file mode 100644index 00000000000..fb5f07a2cda--- /dev/null+++ b/crates/gpui_apple/Cargo.toml@@ -0,0 +1,43 @@+[package]+name = "gpui_apple"+version = "0.1.0"+edition.workspace = true+publish.workspace = true+license = "Apache-2.0"++[lints]+workspace = true++[lib]+path = "src/gpui_apple.rs"++[features]+default = []+test-support = ["gpui/test-support"]+runtime_shaders = []++[dependencies]+gpui.workspace = true++[target.'cfg(target_os = "macos")'.dependencies]+anyhow.workspace = true+block.workspace = true+cocoa.workspace = true+collections.workspace = true+core-foundation.workspace = true+core-video.workspace = true+derive_more.workspace = true+etagere.workspace = true+foreign-types.workspace = true+image.workspace = true+log.workspace = true+metal.workspace = true+objc.workspace = true+parking_lot.workspace = true++[target.'cfg(target_os = "macos")'.build-dependencies]+cbindgen.workspace = true+gpui.workspace = true++[target.'cfg(target_os = "macos")'.dev-dependencies]+gpui = { workspace = true, features = ["test-support"] }diff --git a/crates/gpui_apple/LICENSE-APACHE b/crates/gpui_apple/LICENSE-APACHEnew file mode 120000index 00000000000..1cd601d0a3a--- /dev/null+++ b/crates/gpui_apple/LICENSE-APACHE@@ -0,0 +1 @@+../../LICENSE-APACHE\ No newline at end of filediff --git a/crates/gpui_macos/build.rs b/crates/gpui_apple/build.rssimilarity index 100%rename from crates/gpui_macos/build.rsrename to crates/gpui_apple/build.rsdiff --git a/crates/gpui_apple/src/gpui_apple.rs b/crates/gpui_apple/src/gpui_apple.rsnew file mode 100644index 00000000000..208d297ea10--- /dev/null+++ b/crates/gpui_apple/src/gpui_apple.rs@@ -0,0 +1,8 @@+#![cfg(target_os = "macos")]+//! Shared Apple platform support for GPUI.+//!+//! This crate contains the Metal renderer and GPU resource management shared+//! by GPUI's Apple platform backends.++mod metal_atlas;+pub mod metal_renderer;diff --git a/crates/gpui_macos/src/metal_atlas.rs b/crates/gpui_apple/src/metal_atlas.rssimilarity index 99%rename from crates/gpui_macos/src/metal_atlas.rsrename to crates/gpui_apple/src/metal_atlas.rsindex ce5664d778a..9cfd445ba80 100644--- a/crates/gpui_macos/src/metal_atlas.rs+++ b/crates/gpui_apple/src/metal_atlas.rs@@ -10,7 +10,7 @@ use metal::Device;use parking_lot::Mutex;use std::borrow::Cow;-pub(crate) struct MetalAtlas(Mutex<MetalAtlasState>);+pub struct MetalAtlas(Mutex<MetalAtlasState>);impl MetalAtlas {pub(crate) fn new(device: Device, is_apple_gpu: bool) -> Self {diff --git a/crates/gpui_macos/src/metal_renderer.rs b/crates/gpui_apple/src/metal_renderer.rssimilarity index 99%rename from crates/gpui_macos/src/metal_renderer.rsrename to crates/gpui_apple/src/metal_renderer.rsindex 4f6b0fee78a..0f41297297c 100644--- a/crates/gpui_macos/src/metal_renderer.rs+++ b/crates/gpui_apple/src/metal_renderer.rs@@ -41,10 +41,10 @@ const PATH_SAMPLE_COUNT: u32 = 4;const INSTANCE_BUFFER_ALIGNMENT: usize = 256;const MAX_INSTANCE_BUFFER_SIZE: usize = 256 * 1024 * 1024;-pub(crate) type Context = Arc<Mutex<InstanceBufferPool>>;-pub(crate) type Renderer = MetalRenderer;+pub type Context = Arc<Mutex<InstanceBufferPool>>;+pub type Renderer = MetalRenderer;-pub(crate) unsafe fn new_renderer(+pub unsafe fn new_renderer(context: self::Context,_native_window: *mut c_void,_native_view: *mut c_void,@@ -54,7 +54,7 @@ pub(crate) unsafe fn new_renderer(MetalRenderer::new(context, transparent)}-pub(crate) struct InstanceBufferPool {+pub struct InstanceBufferPool {buffer_size: usize,buffers: Vec<metal::Buffer>,}@@ -109,7 +109,7 @@ impl InstanceBufferPool {}}-pub(crate) struct MetalRenderer {+pub struct MetalRenderer {device: metal::Device,layer: Option<metal::MetalLayer>,is_apple_gpu: bool,diff --git a/crates/gpui_macos/src/shaders.metal b/crates/gpui_apple/src/shaders.metalsimilarity index 100%rename from crates/gpui_macos/src/shaders.metalrename to crates/gpui_apple/src/shaders.metaldiff --git a/crates/gpui_macos/Cargo.toml b/crates/gpui_macos/Cargo.tomlindex 5e0dbf57805..ff10902a37c 100644--- a/crates/gpui_macos/Cargo.toml+++ b/crates/gpui_macos/Cargo.toml@@ -13,8 +13,8 @@ path = "src/gpui_macos.rs"[features]default = ["gpui/default"]-test-support = ["gpui/test-support"]-runtime_shaders = []+test-support = ["gpui/test-support", "gpui_apple/test-support"]+runtime_shaders = ["gpui_apple/runtime_shaders"]font-kit = ["dep:font-kit"]screen-capture = ["gpui/screen-capture"]@@ -34,15 +34,13 @@ core-foundation.workspace = truecore-foundation-sys.workspace = truecore-graphics.workspace = truecore-text.workspace = true-core-video.workspace = truector.workspace = true-derive_more.workspace = truedispatch2 = "0.3.1"-etagere.workspace = true# WARNING: If you change this, you must also publish a new version of zed-font-kit to crates.iofont-kit = { git = "https://github.com/zed-industries/font-kit", rev = "94b0f28166665e8fd2f53ff6d268a14955c82269", package = "zed-font-kit", version = "0.14.1-zed", optional = true }foreign-types.workspace = truefutures.workspace = true+gpui_apple.workspace = truegpui_util.workspace = trueimage.workspace = trueitertools.workspace = true@@ -64,16 +62,9 @@ smallvec.workspace = truestrum.workspace = trueuuid.workspace = true-[target.'cfg(target_os = "macos")'.build-dependencies]-cbindgen.workspace = true-gpui.workspace = true--# When this crate is itself being tested (cargo test -p gpui_macos), its own-# cfg(test) flag enables impls of test-only traits like PlatformHeadlessRenderer-# and PlatformWindow::render_to_image. Those traits/methods only exist in gpui-# when gpui's `test-support` feature is on, so we have to turn that feature on-# as a dev-dependency. The `cfg(test)` flag of a dependent crate doesn't-# propagate to its dependencies, but dev-dependencies do, so this is the-# correct way to enable the feature exactly when needed.+# The macOS test-only window implementation depends on test-only APIs from both+# GPUI and the extracted renderer. A dependency does not inherit this crate's+# `cfg(test)`, so dev-dependencies enable those APIs only for test builds.[target.'cfg(target_os = "macos")'.dev-dependencies]gpui = { workspace = true, features = ["test-support"] }+gpui_apple = { workspace = true, features = ["test-support"] }diff --git a/crates/gpui_macos/src/gpui_macos.rs b/crates/gpui_macos/src/gpui_macos.rsindex 9a9e4d7ae80..1ee18729944 100644--- a/crates/gpui_macos/src/gpui_macos.rs+++ b/crates/gpui_macos/src/gpui_macos.rs@@ -15,10 +15,14 @@ mod system_notifications;#[cfg(feature = "screen-capture")]mod screen_capture;-mod metal_atlas;-pub mod metal_renderer;+use gpui_apple::metal_renderer as renderer;-use metal_renderer as renderer;+pub mod metal_renderer {+ pub use gpui_apple::metal_renderer::{PathRasterizationVertex, PathSprite, SurfaceBounds};++ #[cfg(any(test, feature = "test-support"))]+ pub use gpui_apple::metal_renderer::MetalHeadlessRenderer;+}#[cfg(feature = "font-kit")]mod open_type;