Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions crates/oak-app/src/panels/program_viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ use crate::oakui::timecode::{format_fps, format_resolution};
use crate::oakui::{AppEngine, Monitor};
use crate::panels::commands::{self as panel_commands, PanelCommandHandler};
use crate::panels::{chip, viewer_title};
use std::sync::Arc;

/// Width of the audio level strip, per the design (26px).
const METER_WIDTH: f32 = 26.0;
Expand All @@ -64,15 +65,15 @@ struct ScopeState {
width: u32,
/// Frame height in pixels.
height: u32,
/// Per-pixel luma of the current frame (`0..=1`).
luma: Vec<f32>,
/// Per-pixel chroma `(Cb, Cr)` of the current frame (`0..=1`).
chroma: Vec<(f32, f32)>,
/// Per-pixel luma of the current frame (`0..=1`), shared via [`Arc`].
luma: Arc<Vec<f32>>,
/// Per-pixel chroma `(Cb, Cr)` of the current frame (`0..=1`), shared via [`Arc`].
chroma: Arc<Vec<(f32, f32)>>,
}

impl LumaDataSource for ScopeState {
fn luma_samples(&self) -> Vec<f32> {
self.luma.clone()
fn luma_samples(&self) -> &[f32] {
&self.luma
}

fn luma_dimensions(&self) -> (usize, usize) {
Expand All @@ -81,8 +82,8 @@ impl LumaDataSource for ScopeState {
}

impl ChromaDataSource for ScopeState {
fn chroma_samples(&self) -> Vec<(f32, f32)> {
self.chroma.clone()
fn chroma_samples(&self) -> &[(f32, f32)] {
&self.chroma
}
}

Expand Down Expand Up @@ -232,8 +233,8 @@ impl<E: AppEngine> ProgramViewerPanel<E> {
let scope_state = cx.new(|_cx| ScopeState {
width: 0,
height: 0,
luma: Vec::new(),
chroma: Vec::new(),
luma: Arc::default(),
chroma: Arc::default(),
});
let histogram = cx.new(|cx| Histogram::new(41, scope_state.clone(), window, cx));
let waveform = cx.new(|cx| Waveform::new(42, scope_state.clone(), window, cx));
Expand Down Expand Up @@ -405,8 +406,8 @@ impl<E: AppEngine> ProgramViewerPanel<E> {
self.scope_state.update(cx, |state, cx| {
state.width = scope.width;
state.height = scope.height;
state.luma = (*scope.luma).clone();
state.chroma = (*scope.chroma).clone();
state.luma = scope.luma;
state.chroma = scope.chroma;
cx.notify();
});
}
Expand Down
37 changes: 36 additions & 1 deletion crates/oak-app/src/panels/program_viewer/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use crate::oakui::graphops;
use crate::widgets::timeline::Frame;
use crate::widgets::viewer::PlaybackClock;
use gpui_kit::{Keystroke, Modifiers, Rgba, TestAppContext, VisualTestContext, size};
use std::ptr;
use std::sync::Arc;

/// Serializes the tests that mutate process-global state (HOME, the
/// debug env var) and the undo stack.
Expand Down Expand Up @@ -378,14 +380,32 @@ async fn scopes_tab_renders_from_the_current_frame(cx: &mut TestAppContext) {
window.draw(cx).clear(cx);
});

let (scope_width, scope_height, luma_len, chroma_len, bins, envelope) = cx.read(|app| {
let (
scope_width,
scope_height,
luma_len,
chroma_len,
luma_arc,
chroma_arc,
engine_scope,
luma_ptrs_match,
chroma_ptrs_match,
bins,
envelope,
) = cx.read(|app| {
let panel = panel.read(app);
let state = panel.scope_state.read(app);
let engine_scope = panel.engine.read(app).scope_data(Monitor::Program, app);
(
state.width,
state.height,
state.luma.len(),
state.chroma.len(),
state.luma.clone(),
state.chroma.clone(),
engine_scope,
ptr::eq(state.luma_samples().as_ptr(), state.luma.as_ptr()),
ptr::eq(state.chroma_samples().as_ptr(), state.chroma.as_ptr()),
panel.histogram.read(app).bins(app),
panel.waveform.read(app).envelope(app),
)
Expand All @@ -396,6 +416,21 @@ async fn scopes_tab_renders_from_the_current_frame(cx: &mut TestAppContext) {
assert_eq!(scope_height, crate::oakui::frames::SYNTH_FRAME_HEIGHT);
assert_eq!(luma_len, pixels);
assert_eq!(chroma_len, pixels);
assert!(Arc::ptr_eq(&luma_arc, &engine_scope.luma));
assert!(Arc::ptr_eq(&chroma_arc, &engine_scope.chroma));
assert!(luma_ptrs_match);
assert!(chroma_ptrs_match);
assert_eq!(bins.iter().sum::<u32>() as usize, pixels);
assert_eq!(envelope.len(), 128);

// Redrawing while paused must keep sharing the exact same buffers without
// allocating or copying samples.
cx.update(|window, cx| {
window.draw(cx).clear(cx);
});
cx.read(|app| {
let state = panel.read(app).scope_state.read(app);
assert!(Arc::ptr_eq(&state.luma, &luma_arc));
assert!(Arc::ptr_eq(&state.chroma, &chroma_arc));
});
}
27 changes: 17 additions & 10 deletions crates/oak-app/src/widgets/scopes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use gpui_kit::{
/// Provides luma samples (`0..1`) for the histogram and waveform scopes.
pub trait LumaDataSource: 'static {
/// Luma samples for the current frame, row-major.
fn luma_samples(&self) -> Vec<f32>;
fn luma_samples(&self) -> &[f32];

/// Dimensions `(width, height)` of the row-major frame in [`Self::luma_samples`].
fn luma_dimensions(&self) -> (usize, usize);
Expand All @@ -26,7 +26,7 @@ pub trait LumaDataSource: 'static {
/// Provides chroma samples for the vectorscope.
pub trait ChromaDataSource: 'static {
/// `(u, v)` samples in `0..1` (centered on `0.5`).
fn chroma_samples(&self) -> Vec<(f32, f32)>;
fn chroma_samples(&self) -> &[(f32, f32)];
}

/// A luminance histogram.
Expand Down Expand Up @@ -63,7 +63,7 @@ impl<D: LumaDataSource> Histogram<D> {
/// than an empty vector, so the render pass and hosts can index it
/// directly. Also used by tests to assert the mapping.
pub fn bins(&self, cx: &App) -> Vec<u32> {
histogram_bins(&self.data.read(cx).luma_samples(), 64)
histogram_bins(self.data.read(cx).luma_samples(), 64)
}
}

Expand Down Expand Up @@ -136,7 +136,7 @@ impl<D: LumaDataSource> Waveform<D> {
pub fn envelope(&self, cx: &App) -> Vec<(f32, f32)> {
let data = self.data.read(cx);
let (width, height) = data.luma_dimensions();
waveform_envelope(&data.luma_samples(), width, height, 128)
waveform_envelope(data.luma_samples(), width, height, 128)
}
}

Expand Down Expand Up @@ -207,7 +207,7 @@ impl<D: ChromaDataSource> Vectorscope<D> {
/// subtracting the 0.5 center and clamping; the render pass maps these
/// onto the canvas. Empty when the source has no samples.
pub fn points(&self, cx: &App) -> Vec<(f32, f32)> {
vectorscope_points(&self.data.read(cx).chroma_samples())
vectorscope_points(self.data.read(cx).chroma_samples())
}
}

Expand Down Expand Up @@ -264,15 +264,16 @@ impl<D: ChromaDataSource> Render for Vectorscope<D> {
mod tests {
use super::*;
use gpui_kit::{Entity, Render, TestAppContext, Window, div, px, size};
use std::ptr;

struct MockLuma {
width: usize,
height: usize,
samples: Vec<f32>,
}
impl LumaDataSource for MockLuma {
fn luma_samples(&self) -> Vec<f32> {
self.samples.clone()
fn luma_samples(&self) -> &[f32] {
&self.samples
}

fn luma_dimensions(&self) -> (usize, usize) {
Expand All @@ -281,8 +282,8 @@ mod tests {
}
struct MockChroma(Vec<(f32, f32)>);
impl ChromaDataSource for MockChroma {
fn chroma_samples(&self) -> Vec<(f32, f32)> {
self.0.clone()
fn chroma_samples(&self) -> &[(f32, f32)] {
&self.0
}
}

Expand Down Expand Up @@ -330,14 +331,20 @@ mod tests {
window.draw(cx).clear(cx);
});

let (bins, envelope, points) = cx.read(|app| {
let (bins, envelope, points, luma_ptrs_match, chroma_ptrs_match) = cx.read(|app| {
let host = host.read(app);
let luma_ref = host.histogram.read(app).data.read(app);
let chroma_ref = host.vectorscope.read(app).data.read(app);
(
host.histogram.read(app).bins(app),
host.waveform.read(app).envelope(app),
host.vectorscope.read(app).points(app),
ptr::eq(luma_ref.luma_samples().as_ptr(), luma_ref.samples.as_ptr()),
ptr::eq(chroma_ref.chroma_samples().as_ptr(), chroma_ref.0.as_ptr()),
)
});
assert!(luma_ptrs_match);
assert!(chroma_ptrs_match);
assert_eq!(bins.len(), 64);
let total: u32 = bins.iter().sum();
assert_eq!(total, 100);
Expand Down
Loading