From d9eb9638ea95729617a41736d546d1e49e6428d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 25 Nov 2026 09:33:43 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20perf:=20scale=20and=20convert=20sha?= =?UTF-8?q?re=20frames=20on=20the=20gpu?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Hardware layers hand BGRA straight to ec-hw, which scales and converts to NV12 in one Vulkan compute pass. Only the software H.264 fallback still converts on the CPU. --- crates/client/Cargo.toml | 32 ++++++++--------- crates/client/src/gui/chat.rs | 2 +- crates/client/src/gui/share_view.rs | 54 +++++++++++++--------------- crates/client/src/gui/voice_stats.rs | 5 +-- crates/client/src/network.rs | 4 +-- crates/client/src/share/encode.rs | 52 ++++++++++++++++++--------- crates/client/src/share/fake.rs | 14 ++++++-- crates/client/src/share/mod.rs | 4 +-- crates/client/src/share/session.rs | 12 +++++-- 9 files changed, 104 insertions(+), 75 deletions(-) diff --git a/crates/client/Cargo.toml b/crates/client/Cargo.toml index 83aa850..089b257 100644 --- a/crates/client/Cargo.toml +++ b/crates/client/Cargo.toml @@ -10,22 +10,22 @@ tokio.workspace = true quinn = { workspace = true, features = ["rustls-ring", "platform-verifier"] } gpui-kit = { version = "0.11.0", features = ["syntect"] } -ec-opus = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" } -ec-core = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" } -ec-probe = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" } -ec-mp4 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" } -ec-matroska = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" } -ec-ogg = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" } -ec-h264 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" } -ec-vp8 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" } -ec-aac = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" } -ec-mp3 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" } -ec-vorbis = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" } -ec-flac = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" } -ec-h265 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" } -ec-hw = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" } -ec-vp9 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" } -ec-av1 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" } +ec-opus = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "bb053746edc1988f9a0842fa56e12195c1259d7b" } +ec-core = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "bb053746edc1988f9a0842fa56e12195c1259d7b" } +ec-probe = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "bb053746edc1988f9a0842fa56e12195c1259d7b" } +ec-mp4 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "bb053746edc1988f9a0842fa56e12195c1259d7b" } +ec-matroska = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "bb053746edc1988f9a0842fa56e12195c1259d7b" } +ec-ogg = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "bb053746edc1988f9a0842fa56e12195c1259d7b" } +ec-h264 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "bb053746edc1988f9a0842fa56e12195c1259d7b" } +ec-vp8 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "bb053746edc1988f9a0842fa56e12195c1259d7b" } +ec-aac = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "bb053746edc1988f9a0842fa56e12195c1259d7b" } +ec-mp3 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "bb053746edc1988f9a0842fa56e12195c1259d7b" } +ec-vorbis = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "bb053746edc1988f9a0842fa56e12195c1259d7b" } +ec-flac = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "bb053746edc1988f9a0842fa56e12195c1259d7b" } +ec-h265 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "bb053746edc1988f9a0842fa56e12195c1259d7b" } +ec-hw = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "bb053746edc1988f9a0842fa56e12195c1259d7b" } +ec-vp9 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "bb053746edc1988f9a0842fa56e12195c1259d7b" } +ec-av1 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "bb053746edc1988f9a0842fa56e12195c1259d7b" } ureq = "3.5.2" bytes = "1.12.2" tracing = "0.1.44" diff --git a/crates/client/src/gui/chat.rs b/crates/client/src/gui/chat.rs index 0f1dc5b..486b3d7 100644 --- a/crates/client/src/gui/chat.rs +++ b/crates/client/src/gui/chat.rs @@ -8,7 +8,6 @@ use std::{ use crate::gui::social::InviteLookup; use crate::gui::voice_stats::{SAMPLE_HISTORY, StatsTab}; -use crate::share::session::ShareSample; use crate::gui::{ FIND_HINT, Palette, Tooltip as _, Yumush, avatar, badge, builder::{BuilderTab, Transfer}, @@ -26,6 +25,7 @@ use crate::gui::{ snippet, voice_note::{self, VoiceNote}, }; +use crate::share::session::ShareSample; use common::{ constant::MESSAGE_PAGE_LIMIT, event::Event, diff --git a/crates/client/src/gui/share_view.rs b/crates/client/src/gui/share_view.rs index 477d746..224ab5e 100644 --- a/crates/client/src/gui/share_view.rs +++ b/crates/client/src/gui/share_view.rs @@ -3,8 +3,8 @@ use std::{sync::Arc, time::Duration}; use common::share::{Codec, Fps, Quality, Resolution, Source, options}; use gpui_kit::{ Anchor, AnyElement, AppContext, Context, Div, Entity, FontWeight, InteractiveElement, - IntoElement, MouseButton, ObjectFit, ParentElement, RenderImage, Styled, StyledImage, - StatefulInteractiveElement, Subscription, Task, Window, + IntoElement, MouseButton, ObjectFit, ParentElement, RenderImage, StatefulInteractiveElement, + Styled, StyledImage, Subscription, Task, Window, assets::IconName, component::{ Icon, @@ -636,11 +636,10 @@ fn audio_list(entity: &Entity, yumush: &Yumush, palette: Palette) -> Opt .filter(|app| app.kind == AudioKind::App) .collect(); (!apps.is_empty()).then(|| { - let rows = div() - .flex() - .flex_col() - .gap(px(2.)) - .children(apps.into_iter().map(|app| exclude_row(entity, settings, app))); + let rows = div().flex().flex_col().gap(px(2.)).children( + apps.into_iter() + .map(|app| exclude_row(entity, settings, app)), + ); field("Exclude", app_list(rows), palette).into_any_element() }) } @@ -664,12 +663,7 @@ fn app_list(rows: impl IntoElement) -> impl IntoElement { .child(rows) } -fn pick_list( - entity: &Entity, - selected: &str, - apps: &[AudioApp], - palette: Palette, -) -> Div { +fn pick_list(entity: &Entity, selected: &str, apps: &[AudioApp], palette: Palette) -> Div { let groups = [ (AudioKind::App, "Apps"), (AudioKind::Output, "Outputs"), @@ -732,26 +726,28 @@ fn name_cell(name: String) -> Div { div().flex_1().min_w_0().truncate().child(name) } -fn exclude_row(entity: &Entity, settings: &ShareSettings, app: &AudioApp) -> impl IntoElement { +fn exclude_row( + entity: &Entity, + settings: &ShareSettings, + app: &AudioApp, +) -> impl IntoElement { let entity = entity.clone(); let name = app.name.clone(); let included = !settings.excluded.contains(&app.name); - row_style(div()) - .child(name_cell(app.name.clone())) - .child( - div().flex_shrink_0().child( - Switch::new(format!("share_app_{}", app.node)) - .checked(included) - .on_change(move |checked, _, cx| { - let name = name.clone(); - let included = *checked; - entity.update(cx, |yumush, cx| { - yumush.set_share_excluded(name, !included, cx) - }); - }), - ), - ) + row_style(div()).child(name_cell(app.name.clone())).child( + div().flex_shrink_0().child( + Switch::new(format!("share_app_{}", app.node)) + .checked(included) + .on_change(move |checked, _, cx| { + let name = name.clone(); + let included = *checked; + entity.update(cx, |yumush, cx| { + yumush.set_share_excluded(name, !included, cx) + }); + }), + ), + ) } fn pick_row( diff --git a/crates/client/src/gui/voice_stats.rs b/crates/client/src/gui/voice_stats.rs index 16bbb24..6bd461d 100644 --- a/crates/client/src/gui/voice_stats.rs +++ b/crates/client/src/gui/voice_stats.rs @@ -214,10 +214,7 @@ fn voice_body(samples: &VecDeque, palette: Palette) -> Div { fn sharing_body(layers: &[LayerStats], palette: Palette) -> Div { let upload: u32 = layers.iter().map(|layer| layer.kbps).sum(); column() - .child(tile_grid( - [("Upload", format!("{upload} kbps"))], - palette, - )) + .child(tile_grid([("Upload", format!("{upload} kbps"))], palette)) .children(layers.iter().map(|stat| { row_line( describe(Some(stat.layer), Some(stat.engine)), diff --git a/crates/client/src/network.rs b/crates/client/src/network.rs index 4426067..58730bb 100644 --- a/crates/client/src/network.rs +++ b/crates/client/src/network.rs @@ -1957,8 +1957,8 @@ async fn start_share( settings.encode(), settings.audio_mode(), ) - .await - .map_err(Error::Network)?; + .await + .map_err(Error::Network)?; { let mut share = share.lock().unwrap(); share.sharing = Some(sharing); diff --git a/crates/client/src/share/encode.rs b/crates/client/src/share/encode.rs index fe0e27d..7de6a1a 100644 --- a/crates/client/src/share/encode.rs +++ b/crates/client/src/share/encode.rs @@ -6,7 +6,7 @@ use std::thread; use common::share::{Codec, Layer}; -use super::convert::{self, I420}; +use super::convert; use super::frame::{Encoded, Frame}; use crate::media::decode::{Engine, VideoDecoder}; @@ -106,10 +106,16 @@ impl Backend { } } - fn encode(&mut self, planes: I420, timestamp_ms: u32, force: bool) -> Option<(Vec, bool)> { + fn encode( + &mut self, + frame: &Frame, + settings: &Settings, + force: bool, + ) -> Option<(Vec, bool)> { match self { - Self::Hardware(encoder) => encoder.encode(planes, timestamp_ms, force), + Self::Hardware(encoder) => encoder.encode(frame, settings, force), Self::H264(encoder) => { + let planes = convert::bgra_to_i420(frame, settings.width, settings.height); let picture = ec_h264::PictureView::i420( planes.width, planes.height, @@ -134,29 +140,42 @@ mod hardware { use common::share::Codec; use super::{Settings, report}; - use crate::share::convert::I420; + use crate::share::{convert, frame::Frame}; pub struct Encoder(ec_hw::HwEncoder); impl Encoder { pub fn encode( &mut self, - planes: I420, - timestamp_ms: u32, + frame: &Frame, + settings: &Settings, force: bool, ) -> Option<(Vec, bool)> { - let frame = ec_hw::I420 { - y: planes.y, - u: planes.u, - v: planes.v, - width: planes.width, - height: planes.height, - }; let meta = ec_hw::FrameMetadata { - timestamp: i64::from(timestamp_ms), + timestamp: i64::from(frame.timestamp_ms), force_keyframe: force, }; - report(self.0.encode(&frame, meta)).map(|coded| (coded.data, coded.is_keyframe)) + let coded = if frame.width % 2 == 0 && frame.height % 2 == 0 && frame.stride % 4 == 0 { + let rgb = ec_hw::RgbFrame { + data: &frame.bgra, + width: frame.width, + height: frame.height, + stride: frame.stride, + order: ec_hw::RgbOrder::Bgra, + }; + self.0.encode_rgb(&rgb, meta) + } else { + let planes = convert::bgra_to_i420(frame, settings.width, settings.height); + let picture = ec_hw::I420 { + y: planes.y, + u: planes.u, + v: planes.v, + width: planes.width, + height: planes.height, + }; + self.0.encode(&picture, meta) + }; + report(coded).map(|coded| (coded.data, coded.is_keyframe)) } } @@ -222,8 +241,7 @@ fn run( } last = Some(frame.timestamp_ms); let force = request.swap(false, Ordering::Relaxed); - let planes = convert::bgra_to_i420(&frame, settings.width, settings.height); - let Some((data, keyframe)) = backend.encode(planes, frame.timestamp_ms, force) else { + let Some((data, keyframe)) = backend.encode(&frame, &settings, force) else { continue; }; let encoded = Encoded { diff --git a/crates/client/src/share/fake.rs b/crates/client/src/share/fake.rs index bad4235..9fafab8 100644 --- a/crates/client/src/share/fake.rs +++ b/crates/client/src/share/fake.rs @@ -52,7 +52,11 @@ pub fn start(spec: &str) -> (u32, u32, u32, Receiver, Generator) { } if y < 16 { for x in 0..(32 * 16).min(width as usize) { - let value = if (number >> (x / 16)) & 1 == 1 { 255 } else { 0 }; + let value = if (number >> (x / 16)) & 1 == 1 { + 255 + } else { + 0 + }; bgra[(row + x) * BYTES_PER_PIXEL..][..3].fill(value); } } @@ -72,5 +76,11 @@ pub fn start(spec: &str) -> (u32, u32, u32, Receiver, Generator) { }) .ok() }; - (width, height, refresh, frames_rx, Generator { stop, thread }) + ( + width, + height, + refresh, + frames_rx, + Generator { stop, thread }, + ) } diff --git a/crates/client/src/share/mod.rs b/crates/client/src/share/mod.rs index 6e40239..6310801 100644 --- a/crates/client/src/share/mod.rs +++ b/crates/client/src/share/mod.rs @@ -9,10 +9,10 @@ pub mod capture; #[cfg(target_os = "windows")] #[path = "capture_windows.rs"] pub mod capture; -#[cfg(any(target_os = "linux", target_os = "windows"))] -pub mod fake; pub mod convert; pub mod encode; +#[cfg(any(target_os = "linux", target_os = "windows"))] +pub mod fake; pub mod frame; pub mod reassembly; pub mod session; diff --git a/crates/client/src/share/session.rs b/crates/client/src/share/session.rs index de084da..58da60f 100644 --- a/crates/client/src/share/session.rs +++ b/crates/client/src/share/session.rs @@ -824,7 +824,12 @@ mod tests { } assert_eq!(pictures.len(), 10, "{codec:?} {:?}", decoder.engine()); let last = pictures.pop().expect("picture"); - assert_eq!((last.width, last.height), (width, height), "{codec:?} {:?}", decoder.engine()); + assert_eq!( + (last.width, last.height), + (width, height), + "{codec:?} {:?}", + decoder.engine() + ); (0..height as usize) .flat_map(|row| last.y[row * last.y_stride..][..width as usize].to_vec()) .collect() @@ -836,7 +841,10 @@ mod tests { .map(|(a, b)| u64::from(a.abs_diff(*b))) .sum::() / lumas[0].len() as u64; - assert!(difference < 4, "{codec:?} hardware vs software luma differs by {difference}"); + assert!( + difference < 4, + "{codec:?} hardware vs software luma differs by {difference}" + ); } } }