forge
Explore
Log in

Don't you dare go Hollow

Scale and convert share frames on the gpu

Browse files at this commit

d9eb9638ea95729617a41736d546d1e49e6428d8Ahmet Kaan GümüşParent: c6ed976
  • perf
    • scale and convert share frames on the gpu
Show full message
⚡ perf: scale and convert share frames on the gpu

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.tomlmodified+16 -16
@@ -10,22 +10,22 @@ tokio.workspace = true
10 quinn = { workspace = true, features = ["rustls-ring", "platform-verifier"] }10 quinn = { workspace = true, features = ["rustls-ring", "platform-verifier"] }
11 11
12 gpui-kit = { version = "0.11.0", features = ["syntect"] }12 gpui-kit = { version = "0.11.0", features = ["syntect"] }
13-ec-opus = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" }13+ec-opus = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "bb053746edc1988f9a0842fa56e12195c1259d7b" }
14-ec-core = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" }14+ec-core = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "bb053746edc1988f9a0842fa56e12195c1259d7b" }
15-ec-probe = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" }15+ec-probe = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "bb053746edc1988f9a0842fa56e12195c1259d7b" }
16-ec-mp4 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" }16+ec-mp4 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "bb053746edc1988f9a0842fa56e12195c1259d7b" }
17-ec-matroska = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" }17+ec-matroska = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "bb053746edc1988f9a0842fa56e12195c1259d7b" }
18-ec-ogg = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" }18+ec-ogg = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "bb053746edc1988f9a0842fa56e12195c1259d7b" }
19-ec-h264 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" }19+ec-h264 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "bb053746edc1988f9a0842fa56e12195c1259d7b" }
20-ec-vp8 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" }20+ec-vp8 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "bb053746edc1988f9a0842fa56e12195c1259d7b" }
21-ec-aac = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" }21+ec-aac = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "bb053746edc1988f9a0842fa56e12195c1259d7b" }
22-ec-mp3 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" }22+ec-mp3 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "bb053746edc1988f9a0842fa56e12195c1259d7b" }
23-ec-vorbis = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" }23+ec-vorbis = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "bb053746edc1988f9a0842fa56e12195c1259d7b" }
24-ec-flac = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" }24+ec-flac = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "bb053746edc1988f9a0842fa56e12195c1259d7b" }
25-ec-h265 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" }25+ec-h265 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "bb053746edc1988f9a0842fa56e12195c1259d7b" }
26-ec-hw = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" }26+ec-hw = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "bb053746edc1988f9a0842fa56e12195c1259d7b" }
27-ec-vp9 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" }27+ec-vp9 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "bb053746edc1988f9a0842fa56e12195c1259d7b" }
28-ec-av1 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" }28+ec-av1 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "bb053746edc1988f9a0842fa56e12195c1259d7b" }
29 ureq = "3.5.2"29 ureq = "3.5.2"
30 bytes = "1.12.2"30 bytes = "1.12.2"
31 tracing = "0.1.44"31 tracing = "0.1.44"
crates/client/src/gui/chat.rsmodified+1 -1
@@ -8,7 +8,6 @@ use std::{
8 8
9 use crate::gui::social::InviteLookup;9 use crate::gui::social::InviteLookup;
10 use crate::gui::voice_stats::{SAMPLE_HISTORY, StatsTab};10 use crate::gui::voice_stats::{SAMPLE_HISTORY, StatsTab};
11-use crate::share::session::ShareSample;
12 use crate::gui::{11 use crate::gui::{
13 FIND_HINT, Palette, Tooltip as _, Yumush, avatar, badge,12 FIND_HINT, Palette, Tooltip as _, Yumush, avatar, badge,
14 builder::{BuilderTab, Transfer},13 builder::{BuilderTab, Transfer},
@@ -26,6 +25,7 @@ use crate::gui::{
26 snippet,25 snippet,
27 voice_note::{self, VoiceNote},26 voice_note::{self, VoiceNote},
28 };27 };
28+use crate::share::session::ShareSample;
29 use common::{29 use common::{
30 constant::MESSAGE_PAGE_LIMIT,30 constant::MESSAGE_PAGE_LIMIT,
31 event::Event,31 event::Event,
crates/client/src/gui/share_view.rsmodified+25 -29
@@ -3,8 +3,8 @@ use std::{sync::Arc, time::Duration};
3 use common::share::{Codec, Fps, Quality, Resolution, Source, options};3 use common::share::{Codec, Fps, Quality, Resolution, Source, options};
4 use gpui_kit::{4 use gpui_kit::{
5 Anchor, AnyElement, AppContext, Context, Div, Entity, FontWeight, InteractiveElement,5 Anchor, AnyElement, AppContext, Context, Div, Entity, FontWeight, InteractiveElement,
6- IntoElement, MouseButton, ObjectFit, ParentElement, RenderImage, Styled, StyledImage,6+ IntoElement, MouseButton, ObjectFit, ParentElement, RenderImage, StatefulInteractiveElement,
7- StatefulInteractiveElement, Subscription, Task, Window,7+ Styled, StyledImage, Subscription, Task, Window,
8 assets::IconName,8 assets::IconName,
9 component::{9 component::{
10 Icon,10 Icon,
@@ -636,11 +636,10 @@ fn audio_list(entity: &Entity<Yumush>, yumush: &Yumush, palette: Palette) -> Opt
636 .filter(|app| app.kind == AudioKind::App)636 .filter(|app| app.kind == AudioKind::App)
637 .collect();637 .collect();
638 (!apps.is_empty()).then(|| {638 (!apps.is_empty()).then(|| {
639- let rows = div()639+ let rows = div().flex().flex_col().gap(px(2.)).children(
640- .flex()640+ apps.into_iter()
641- .flex_col()641+ .map(|app| exclude_row(entity, settings, app)),
642- .gap(px(2.))642+ );
643- .children(apps.into_iter().map(|app| exclude_row(entity, settings, app)));
644 field("Exclude", app_list(rows), palette).into_any_element()643 field("Exclude", app_list(rows), palette).into_any_element()
645 })644 })
646 }645 }
@@ -664,12 +663,7 @@ fn app_list(rows: impl IntoElement) -> impl IntoElement {
664 .child(rows)663 .child(rows)
665 }664 }
666 665
667-fn pick_list(666+fn pick_list(entity: &Entity<Yumush>, selected: &str, apps: &[AudioApp], palette: Palette) -> Div {
668- entity: &Entity<Yumush>,
669- selected: &str,
670- apps: &[AudioApp],
671- palette: Palette,
672-) -> Div {
673 let groups = [667 let groups = [
674 (AudioKind::App, "Apps"),668 (AudioKind::App, "Apps"),
675 (AudioKind::Output, "Outputs"),669 (AudioKind::Output, "Outputs"),
@@ -732,26 +726,28 @@ fn name_cell(name: String) -> Div {
732 div().flex_1().min_w_0().truncate().child(name)726 div().flex_1().min_w_0().truncate().child(name)
733 }727 }
734 728
735-fn exclude_row(entity: &Entity<Yumush>, settings: &ShareSettings, app: &AudioApp) -> impl IntoElement {729+fn exclude_row(
730+ entity: &Entity<Yumush>,
731+ settings: &ShareSettings,
732+ app: &AudioApp,
733+) -> impl IntoElement {
736 let entity = entity.clone();734 let entity = entity.clone();
737 let name = app.name.clone();735 let name = app.name.clone();
738 let included = !settings.excluded.contains(&app.name);736 let included = !settings.excluded.contains(&app.name);
739 737
740- row_style(div())738+ row_style(div()).child(name_cell(app.name.clone())).child(
741- .child(name_cell(app.name.clone()))739+ div().flex_shrink_0().child(
742- .child(740+ Switch::new(format!("share_app_{}", app.node))
743- div().flex_shrink_0().child(741+ .checked(included)
744- Switch::new(format!("share_app_{}", app.node))742+ .on_change(move |checked, _, cx| {
745- .checked(included)743+ let name = name.clone();
746- .on_change(move |checked, _, cx| {744+ let included = *checked;
747- let name = name.clone();745+ entity.update(cx, |yumush, cx| {
748- let included = *checked;746+ yumush.set_share_excluded(name, !included, cx)
749- entity.update(cx, |yumush, cx| {747+ });
750- yumush.set_share_excluded(name, !included, cx)748+ }),
751- });749+ ),
752- }),750+ )
753- ),
754- )
755 }751 }
756 752
757 fn pick_row(753 fn pick_row(
crates/client/src/gui/voice_stats.rsmodified+1 -4
@@ -214,10 +214,7 @@ fn voice_body(samples: &VecDeque<VoiceSample>, palette: Palette) -> Div {
214 fn sharing_body(layers: &[LayerStats], palette: Palette) -> Div {214 fn sharing_body(layers: &[LayerStats], palette: Palette) -> Div {
215 let upload: u32 = layers.iter().map(|layer| layer.kbps).sum();215 let upload: u32 = layers.iter().map(|layer| layer.kbps).sum();
216 column()216 column()
217- .child(tile_grid(217+ .child(tile_grid([("Upload", format!("{upload} kbps"))], palette))
218- [("Upload", format!("{upload} kbps"))],
219- palette,
220- ))
221 .children(layers.iter().map(|stat| {218 .children(layers.iter().map(|stat| {
222 row_line(219 row_line(
223 describe(Some(stat.layer), Some(stat.engine)),220 describe(Some(stat.layer), Some(stat.engine)),
crates/client/src/network.rsmodified+2 -2
@@ -1957,8 +1957,8 @@ async fn start_share(
1957 settings.encode(),1957 settings.encode(),
1958 settings.audio_mode(),1958 settings.audio_mode(),
1959 )1959 )
1960- .await1960+ .await
1961- .map_err(Error::Network)?;1961+ .map_err(Error::Network)?;
1962 {1962 {
1963 let mut share = share.lock().unwrap();1963 let mut share = share.lock().unwrap();
1964 share.sharing = Some(sharing);1964 share.sharing = Some(sharing);
crates/client/src/share/encode.rsmodified+35 -17
@@ -6,7 +6,7 @@ use std::thread;
6 6
7 use common::share::{Codec, Layer};7 use common::share::{Codec, Layer};
8 8
9-use super::convert::{self, I420};9+use super::convert;
10 use super::frame::{Encoded, Frame};10 use super::frame::{Encoded, Frame};
11 use crate::media::decode::{Engine, VideoDecoder};11 use crate::media::decode::{Engine, VideoDecoder};
12 12
@@ -106,10 +106,16 @@ impl Backend {
106 }106 }
107 }107 }
108 108
109- fn encode(&mut self, planes: I420, timestamp_ms: u32, force: bool) -> Option<(Vec<u8>, bool)> {109+ fn encode(
110+ &mut self,
111+ frame: &Frame,
112+ settings: &Settings,
113+ force: bool,
114+ ) -> Option<(Vec<u8>, bool)> {
110 match self {115 match self {
111- Self::Hardware(encoder) => encoder.encode(planes, timestamp_ms, force),116+ Self::Hardware(encoder) => encoder.encode(frame, settings, force),
112 Self::H264(encoder) => {117 Self::H264(encoder) => {
118+ let planes = convert::bgra_to_i420(frame, settings.width, settings.height);
113 let picture = ec_h264::PictureView::i420(119 let picture = ec_h264::PictureView::i420(
114 planes.width,120 planes.width,
115 planes.height,121 planes.height,
@@ -134,29 +140,42 @@ mod hardware {
134 use common::share::Codec;140 use common::share::Codec;
135 141
136 use super::{Settings, report};142 use super::{Settings, report};
137- use crate::share::convert::I420;143+ use crate::share::{convert, frame::Frame};
138 144
139 pub struct Encoder(ec_hw::HwEncoder);145 pub struct Encoder(ec_hw::HwEncoder);
140 146
141 impl Encoder {147 impl Encoder {
142 pub fn encode(148 pub fn encode(
143 &mut self,149 &mut self,
144- planes: I420,150+ frame: &Frame,
145- timestamp_ms: u32,151+ settings: &Settings,
146 force: bool,152 force: bool,
147 ) -> Option<(Vec<u8>, bool)> {153 ) -> Option<(Vec<u8>, bool)> {
148- let frame = ec_hw::I420 {
149- y: planes.y,
150- u: planes.u,
151- v: planes.v,
152- width: planes.width,
153- height: planes.height,
154- };
155 let meta = ec_hw::FrameMetadata {154 let meta = ec_hw::FrameMetadata {
156- timestamp: i64::from(timestamp_ms),155+ timestamp: i64::from(frame.timestamp_ms),
157 force_keyframe: force,156 force_keyframe: force,
158 };157 };
159- report(self.0.encode(&frame, meta)).map(|coded| (coded.data, coded.is_keyframe))158+ let coded = if frame.width % 2 == 0 && frame.height % 2 == 0 && frame.stride % 4 == 0 {
159+ let rgb = ec_hw::RgbFrame {
160+ data: &frame.bgra,
161+ width: frame.width,
162+ height: frame.height,
163+ stride: frame.stride,
164+ order: ec_hw::RgbOrder::Bgra,
165+ };
166+ self.0.encode_rgb(&rgb, meta)
167+ } else {
168+ let planes = convert::bgra_to_i420(frame, settings.width, settings.height);
169+ let picture = ec_hw::I420 {
170+ y: planes.y,
171+ u: planes.u,
172+ v: planes.v,
173+ width: planes.width,
174+ height: planes.height,
175+ };
176+ self.0.encode(&picture, meta)
177+ };
178+ report(coded).map(|coded| (coded.data, coded.is_keyframe))
160 }179 }
161 }180 }
162 181
@@ -222,8 +241,7 @@ fn run(
222 }241 }
223 last = Some(frame.timestamp_ms);242 last = Some(frame.timestamp_ms);
224 let force = request.swap(false, Ordering::Relaxed);243 let force = request.swap(false, Ordering::Relaxed);
225- let planes = convert::bgra_to_i420(&frame, settings.width, settings.height);244+ let Some((data, keyframe)) = backend.encode(&frame, &settings, force) else {
226- let Some((data, keyframe)) = backend.encode(planes, frame.timestamp_ms, force) else {
227 continue;245 continue;
228 };246 };
229 let encoded = Encoded {247 let encoded = Encoded {
crates/client/src/share/fake.rsmodified+12 -2
@@ -52,7 +52,11 @@ pub fn start(spec: &str) -> (u32, u32, u32, Receiver<Frame>, Generator) {
52 }52 }
53 if y < 16 {53 if y < 16 {
54 for x in 0..(32 * 16).min(width as usize) {54 for x in 0..(32 * 16).min(width as usize) {
55- let value = if (number >> (x / 16)) & 1 == 1 { 255 } else { 0 };55+ let value = if (number >> (x / 16)) & 1 == 1 {
56+ 255
57+ } else {
58+ 0
59+ };
56 bgra[(row + x) * BYTES_PER_PIXEL..][..3].fill(value);60 bgra[(row + x) * BYTES_PER_PIXEL..][..3].fill(value);
57 }61 }
58 }62 }
@@ -72,5 +76,11 @@ pub fn start(spec: &str) -> (u32, u32, u32, Receiver<Frame>, Generator) {
72 })76 })
73 .ok()77 .ok()
74 };78 };
75- (width, height, refresh, frames_rx, Generator { stop, thread })79+ (
80+ width,
81+ height,
82+ refresh,
83+ frames_rx,
84+ Generator { stop, thread },
85+ )
76 }86 }
crates/client/src/share/mod.rsmodified+2 -2
@@ -9,10 +9,10 @@ pub mod capture;
9 #[cfg(target_os = "windows")]9 #[cfg(target_os = "windows")]
10 #[path = "capture_windows.rs"]10 #[path = "capture_windows.rs"]
11 pub mod capture;11 pub mod capture;
12-#[cfg(any(target_os = "linux", target_os = "windows"))]
13-pub mod fake;
14 pub mod convert;12 pub mod convert;
15 pub mod encode;13 pub mod encode;
14+#[cfg(any(target_os = "linux", target_os = "windows"))]
15+pub mod fake;
16 pub mod frame;16 pub mod frame;
17 pub mod reassembly;17 pub mod reassembly;
18 pub mod session;18 pub mod session;
crates/client/src/share/session.rsmodified+10 -2
@@ -824,7 +824,12 @@ mod tests {
824 }824 }
825 assert_eq!(pictures.len(), 10, "{codec:?} {:?}", decoder.engine());825 assert_eq!(pictures.len(), 10, "{codec:?} {:?}", decoder.engine());
826 let last = pictures.pop().expect("picture");826 let last = pictures.pop().expect("picture");
827- assert_eq!((last.width, last.height), (width, height), "{codec:?} {:?}", decoder.engine());827+ assert_eq!(
828+ (last.width, last.height),
829+ (width, height),
830+ "{codec:?} {:?}",
831+ decoder.engine()
832+ );
828 (0..height as usize)833 (0..height as usize)
829 .flat_map(|row| last.y[row * last.y_stride..][..width as usize].to_vec())834 .flat_map(|row| last.y[row * last.y_stride..][..width as usize].to_vec())
830 .collect()835 .collect()
@@ -836,7 +841,10 @@ mod tests {
836 .map(|(a, b)| u64::from(a.abs_diff(*b)))841 .map(|(a, b)| u64::from(a.abs_diff(*b)))
837 .sum::<u64>()842 .sum::<u64>()
838 / lumas[0].len() as u64;843 / lumas[0].len() as u64;
839- assert!(difference < 4, "{codec:?} hardware vs software luma differs by {difference}");844+ assert!(
845+ difference < 4,
846+ "{codec:?} hardware vs software luma differs by {difference}"
847+ );
840 }848 }
841 }849 }
842 }850 }