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
1010 quinn = { workspace = true, features = ["rustls-ring", "platform-verifier"] }
1111
1212 gpui-kit = { version = "0.11.0", features = ["syntect"] }
13-ec-opus = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" }
14-ec-core = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" }
15-ec-probe = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" }
16-ec-mp4 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" }
17-ec-matroska = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" }
18-ec-ogg = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" }
19-ec-h264 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" }
20-ec-vp8 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" }
21-ec-aac = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" }
22-ec-mp3 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" }
23-ec-vorbis = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" }
24-ec-flac = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" }
25-ec-h265 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" }
26-ec-hw = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" }
27-ec-vp9 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "274b9cb95d91511f24b2ddf82e02073ee31a0568" }
28-ec-av1 = { 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 = "bb053746edc1988f9a0842fa56e12195c1259d7b" }
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 = "bb053746edc1988f9a0842fa56e12195c1259d7b" }
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 = "bb053746edc1988f9a0842fa56e12195c1259d7b" }
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 = "bb053746edc1988f9a0842fa56e12195c1259d7b" }
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 = "bb053746edc1988f9a0842fa56e12195c1259d7b" }
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 = "bb053746edc1988f9a0842fa56e12195c1259d7b" }
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 = "bb053746edc1988f9a0842fa56e12195c1259d7b" }
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 = "bb053746edc1988f9a0842fa56e12195c1259d7b" }
2929 ureq = "3.5.2"
3030 bytes = "1.12.2"
3131 tracing = "0.1.44"
crates/client/src/gui/chat.rsmodified+1 -1
@@ -8,7 +8,6 @@ use std::{
88
99 use crate::gui::social::InviteLookup;
1010 use crate::gui::voice_stats::{SAMPLE_HISTORY, StatsTab};
11-use crate::share::session::ShareSample;
1211 use crate::gui::{
1312 FIND_HINT, Palette, Tooltip as _, Yumush, avatar, badge,
1413 builder::{BuilderTab, Transfer},
@@ -26,6 +25,7 @@ use crate::gui::{
2625 snippet,
2726 voice_note::{self, VoiceNote},
2827 };
28+use crate::share::session::ShareSample;
2929 use common::{
3030 constant::MESSAGE_PAGE_LIMIT,
3131 event::Event,
crates/client/src/gui/share_view.rsmodified+25 -29
@@ -3,8 +3,8 @@ use std::{sync::Arc, time::Duration};
33 use common::share::{Codec, Fps, Quality, Resolution, Source, options};
44 use gpui_kit::{
55 Anchor, AnyElement, AppContext, Context, Div, Entity, FontWeight, InteractiveElement,
6- IntoElement, MouseButton, ObjectFit, ParentElement, RenderImage, Styled, StyledImage,
7- StatefulInteractiveElement, Subscription, Task, Window,
6+ IntoElement, MouseButton, ObjectFit, ParentElement, RenderImage, StatefulInteractiveElement,
7+ Styled, StyledImage, Subscription, Task, Window,
88 assets::IconName,
99 component::{
1010 Icon,
@@ -636,11 +636,10 @@ fn audio_list(entity: &Entity<Yumush>, yumush: &Yumush, palette: Palette) -> Opt
636636 .filter(|app| app.kind == AudioKind::App)
637637 .collect();
638638 (!apps.is_empty()).then(|| {
639- let rows = div()
640- .flex()
641- .flex_col()
642- .gap(px(2.))
643- .children(apps.into_iter().map(|app| exclude_row(entity, settings, app)));
639+ let rows = div().flex().flex_col().gap(px(2.)).children(
640+ apps.into_iter()
641+ .map(|app| exclude_row(entity, settings, app)),
642+ );
644643 field("Exclude", app_list(rows), palette).into_any_element()
645644 })
646645 }
@@ -664,12 +663,7 @@ fn app_list(rows: impl IntoElement) -> impl IntoElement {
664663 .child(rows)
665664 }
666665
667-fn pick_list(
668- entity: &Entity<Yumush>,
669- selected: &str,
670- apps: &[AudioApp],
671- palette: Palette,
672-) -> Div {
666+fn pick_list(entity: &Entity<Yumush>, selected: &str, apps: &[AudioApp], palette: Palette) -> Div {
673667 let groups = [
674668 (AudioKind::App, "Apps"),
675669 (AudioKind::Output, "Outputs"),
@@ -732,26 +726,28 @@ fn name_cell(name: String) -> Div {
732726 div().flex_1().min_w_0().truncate().child(name)
733727 }
734728
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 {
736734 let entity = entity.clone();
737735 let name = app.name.clone();
738736 let included = !settings.excluded.contains(&app.name);
739737
740- row_style(div())
741- .child(name_cell(app.name.clone()))
742- .child(
743- div().flex_shrink_0().child(
744- Switch::new(format!("share_app_{}", app.node))
745- .checked(included)
746- .on_change(move |checked, _, cx| {
747- let name = name.clone();
748- let included = *checked;
749- entity.update(cx, |yumush, cx| {
750- yumush.set_share_excluded(name, !included, cx)
751- });
752- }),
753- ),
754- )
738+ row_style(div()).child(name_cell(app.name.clone())).child(
739+ div().flex_shrink_0().child(
740+ Switch::new(format!("share_app_{}", app.node))
741+ .checked(included)
742+ .on_change(move |checked, _, cx| {
743+ let name = name.clone();
744+ let included = *checked;
745+ entity.update(cx, |yumush, cx| {
746+ yumush.set_share_excluded(name, !included, cx)
747+ });
748+ }),
749+ ),
750+ )
755751 }
756752
757753 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 {
214214 fn sharing_body(layers: &[LayerStats], palette: Palette) -> Div {
215215 let upload: u32 = layers.iter().map(|layer| layer.kbps).sum();
216216 column()
217- .child(tile_grid(
218- [("Upload", format!("{upload} kbps"))],
219- palette,
220- ))
217+ .child(tile_grid([("Upload", format!("{upload} kbps"))], palette))
221218 .children(layers.iter().map(|stat| {
222219 row_line(
223220 describe(Some(stat.layer), Some(stat.engine)),
crates/client/src/network.rsmodified+2 -2
@@ -1957,8 +1957,8 @@ async fn start_share(
19571957 settings.encode(),
19581958 settings.audio_mode(),
19591959 )
1960- .await
1961- .map_err(Error::Network)?;
1960+ .await
1961+ .map_err(Error::Network)?;
19621962 {
19631963 let mut share = share.lock().unwrap();
19641964 share.sharing = Some(sharing);
crates/client/src/share/encode.rsmodified+35 -17
@@ -6,7 +6,7 @@ use std::thread;
66
77 use common::share::{Codec, Layer};
88
9-use super::convert::{self, I420};
9+use super::convert;
1010 use super::frame::{Encoded, Frame};
1111 use crate::media::decode::{Engine, VideoDecoder};
1212
@@ -106,10 +106,16 @@ impl Backend {
106106 }
107107 }
108108
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)> {
110115 match self {
111- Self::Hardware(encoder) => encoder.encode(planes, timestamp_ms, force),
116+ Self::Hardware(encoder) => encoder.encode(frame, settings, force),
112117 Self::H264(encoder) => {
118+ let planes = convert::bgra_to_i420(frame, settings.width, settings.height);
113119 let picture = ec_h264::PictureView::i420(
114120 planes.width,
115121 planes.height,
@@ -134,29 +140,42 @@ mod hardware {
134140 use common::share::Codec;
135141
136142 use super::{Settings, report};
137- use crate::share::convert::I420;
143+ use crate::share::{convert, frame::Frame};
138144
139145 pub struct Encoder(ec_hw::HwEncoder);
140146
141147 impl Encoder {
142148 pub fn encode(
143149 &mut self,
144- planes: I420,
145- timestamp_ms: u32,
150+ frame: &Frame,
151+ settings: &Settings,
146152 force: bool,
147153 ) -> 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- };
155154 let meta = ec_hw::FrameMetadata {
156- timestamp: i64::from(timestamp_ms),
155+ timestamp: i64::from(frame.timestamp_ms),
157156 force_keyframe: force,
158157 };
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))
160179 }
161180 }
162181
@@ -222,8 +241,7 @@ fn run(
222241 }
223242 last = Some(frame.timestamp_ms);
224243 let force = request.swap(false, Ordering::Relaxed);
225- let planes = convert::bgra_to_i420(&frame, settings.width, settings.height);
226- let Some((data, keyframe)) = backend.encode(planes, frame.timestamp_ms, force) else {
244+ let Some((data, keyframe)) = backend.encode(&frame, &settings, force) else {
227245 continue;
228246 };
229247 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) {
5252 }
5353 if y < 16 {
5454 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+ };
5660 bgra[(row + x) * BYTES_PER_PIXEL..][..3].fill(value);
5761 }
5862 }
@@ -72,5 +76,11 @@ pub fn start(spec: &str) -> (u32, u32, u32, Receiver<Frame>, Generator) {
7276 })
7377 .ok()
7478 };
75- (width, height, refresh, frames_rx, Generator { stop, thread })
79+ (
80+ width,
81+ height,
82+ refresh,
83+ frames_rx,
84+ Generator { stop, thread },
85+ )
7686 }
crates/client/src/share/mod.rsmodified+2 -2
@@ -9,10 +9,10 @@ pub mod capture;
99 #[cfg(target_os = "windows")]
1010 #[path = "capture_windows.rs"]
1111 pub mod capture;
12-#[cfg(any(target_os = "linux", target_os = "windows"))]
13-pub mod fake;
1412 pub mod convert;
1513 pub mod encode;
14+#[cfg(any(target_os = "linux", target_os = "windows"))]
15+pub mod fake;
1616 pub mod frame;
1717 pub mod reassembly;
1818 pub mod session;
crates/client/src/share/session.rsmodified+10 -2
@@ -824,7 +824,12 @@ mod tests {
824824 }
825825 assert_eq!(pictures.len(), 10, "{codec:?} {:?}", decoder.engine());
826826 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+ );
828833 (0..height as usize)
829834 .flat_map(|row| last.y[row * last.y_stride..][..width as usize].to_vec())
830835 .collect()
@@ -836,7 +841,10 @@ mod tests {
836841 .map(|(a, b)| u64::from(a.abs_diff(*b)))
837842 .sum::<u64>()
838843 / 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+ );
840848 }
841849 }
842850 }