Skip to content

Commit

Permalink
fix examples
Browse files Browse the repository at this point in the history
  • Loading branch information
wkozyra95 committed Nov 4, 2024
1 parent 5b9917e commit 8d05e68
Show file tree
Hide file tree
Showing 9 changed files with 260 additions and 88 deletions.
7 changes: 2 additions & 5 deletions compositor_render/src/scene/rescaler_component/layout.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::time::Duration;

use log::info;

use crate::{
scene::{
layout::StatefulLayoutComponent, BorderRadius, HorizontalAlign, RGBAColor, RescaleMode,
Expand Down Expand Up @@ -76,7 +74,6 @@ impl RescalerComponentParam {
}
ref _non_layout => (StatefulLayoutComponent::layout_content(child, 0), vec![], 1),
};
info!("Rescaler child {content:#?} {children:#?}");

let top = match self.vertical_align {
VerticalAlign::Top => 0.0,
Expand Down Expand Up @@ -130,8 +127,8 @@ impl RescalerComponentParam {
children: vec![NestedLayout {
top: top + self.border_width,
left: left + self.border_width,
width: width,
height: height,
width,
height,
rotation_degrees: 0.0,
scale_x: scale,
scale_y: scale,
Expand Down
18 changes: 14 additions & 4 deletions compositor_render/src/scene/view_component/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ struct StaticChildLayoutOpts {
/// For direction=column defines height of a static component
static_child_size: f32,
parent_size: Size,
/// border width before rescaling, it is used to calculate top/left offset correctly
/// when `overflow: fit` is set
parent_border_width: f32,
}

impl ViewComponentParam {
Expand Down Expand Up @@ -52,13 +55,19 @@ impl ViewComponentParam {
Overflow::Fit => (
self.scale_factor_for_overflow_fit(content_size, children, pts),
None,
None,
Some(Mask {
radius: self.border_radius - self.border_width,
top: self.border_width,
left: self.border_width,
width: content_size.width,
height: content_size.height,
}),
),
};

// offset along x or y direction (depends on self.direction) where next
// child component should be placed
let mut static_offset = self.border_width;
let mut static_offset = self.border_width / scale;

let children: Vec<_> = children
.iter_mut()
Expand All @@ -80,6 +89,7 @@ impl ViewComponentParam {
static_offset,
static_child_size,
parent_size: content_size,
parent_border_width: self.border_width / scale,
},
pts,
);
Expand Down Expand Up @@ -126,7 +136,7 @@ impl ViewComponentParam {
ViewChildrenDirection::Row => {
let width = opts.width.unwrap_or(opts.static_child_size);
let height = opts.height.unwrap_or(opts.parent_size.height);
let top = self.border_width;
let top = opts.parent_border_width;
let left = static_offset;
static_offset += width;
(top, left, width, height)
Expand All @@ -135,7 +145,7 @@ impl ViewComponentParam {
let height = opts.height.unwrap_or(opts.static_child_size);
let width = opts.width.unwrap_or(opts.parent_size.width);
let top = static_offset;
let left = self.border_width;
let left = opts.parent_border_width;
static_offset += height;
(top, left, width, height)
}
Expand Down
12 changes: 8 additions & 4 deletions compositor_render/src/transformations/layout/flatten.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,14 @@ impl NestedLayout {
// - Remove masks that don't do anything
fn fix_final_render_layout(mut layout: RenderLayout) -> RenderLayout {
fn filter_mask(layout: &RenderLayout, mask: Mask) -> Option<Mask> {
let should_skip = mask.top <= layout.top
&& mask.left <= layout.left
&& mask.left + mask.width >= layout.left + layout.width
&& mask.top + mask.height >= layout.top + layout.height;
let max_top_border = f32::max(mask.radius.top_left, mask.radius.top_right);
let max_bottom_border = f32::max(mask.radius.bottom_left, mask.radius.bottom_right);
let max_left_border = f32::max(mask.radius.top_left, mask.radius.bottom_left);
let max_right_border = f32::max(mask.radius.top_right, mask.radius.bottom_right);
let should_skip = mask.top + max_top_border <= layout.top
&& mask.left + max_left_border <= layout.left
&& mask.left + mask.width - max_right_border >= layout.left + layout.width
&& mask.top + mask.height - max_bottom_border >= layout.top + layout.height;
match should_skip {
true => None,
false => Some(mask),
Expand Down
52 changes: 7 additions & 45 deletions integration_tests/examples/raw_channel_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ use compositor_pipeline::{
use compositor_render::{
create_wgpu_ctx,
error::ErrorStack,
scene::{
AbsolutePosition, BorderRadius, BoxShadow, Component, HorizontalPosition, Overflow,
Position, RGBAColor, VerticalPosition, ViewChildrenDirection, ViewComponent,
},
scene::{Component, InputStreamComponent},
Frame, FrameData, InputId, OutputId, Resolution,
};
use crossbeam_channel::bounded;
Expand Down Expand Up @@ -71,8 +68,8 @@ fn main() {
download_root: config.download_root,
output_sample_rate: config.output_sample_rate,
wgpu_features: config.required_wgpu_features,
wgpu_ctx: Some((wgpu_device.clone(), wgpu_queue.clone())),
load_system_fonts: Some(true),
wgpu_ctx: Some((wgpu_device.clone(), wgpu_queue.clone())),
})
.unwrap_or_else(|err| {
panic!(
Expand All @@ -90,51 +87,16 @@ fn main() {
output_options: RawDataOutputOptions {
video: Some(RawVideoOptions {
resolution: Resolution {
width: 640,
height: 360,
width: 1280,
height: 720,
},
}),
audio: Some(RawAudioOptions),
},
video: Some(compositor_pipeline::pipeline::OutputVideoOptions {
initial: Component::View(ViewComponent {
initial: Component::InputStream(InputStreamComponent {
id: None,
children: vec![Component::View(ViewComponent {
id: None,
children: vec![],
direction: ViewChildrenDirection::Row,
position: Position::Absolute(AbsolutePosition {
width: Some(300.0),
height: Some(200.0),
position_horizontal: HorizontalPosition::LeftOffset(20.0),
position_vertical: VerticalPosition::TopOffset(20.0),
rotation_degrees: 0.0,
}),
transition: None,
overflow: Overflow::Hidden,
background_color: RGBAColor(255, 0, 0, 255),
border_radius: BorderRadius::new_with_radius(70.0),
border_width: 50.0,
border_color: RGBAColor(0, 255, 0, 255),
box_shadow: vec![BoxShadow {
offset_x: 60.0,
offset_y: 60.0,
blur_radius: 60.0,
color: RGBAColor(0, 255, 0, 255),
}],
})],
direction: ViewChildrenDirection::Row,
position: Position::Static {
width: None,
height: None,
},
transition: None,
overflow: Overflow::Hidden,
background_color: RGBAColor(0, 255, 255, 255),
border_radius: BorderRadius::ZERO,
border_width: 0.0,
border_color: RGBAColor(0, 0, 0, 0),
box_shadow: vec![],
input_id: input_id.clone(),
}),
end_condition: PipelineOutputEndCondition::Never,
}),
Expand Down Expand Up @@ -181,7 +143,7 @@ fn main() {
if [0, 200, 400, 600, 800, 1000].contains(&index) {
write_frame(index, frame, &wgpu_device, &wgpu_queue);
}
if index > 1 {
if index > 1000 {
send_done.send(()).unwrap();
return;
}
Expand Down
154 changes: 154 additions & 0 deletions integration_tests/examples/rescaler_border_transition.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
use anyhow::Result;
use compositor_api::types::Resolution;
use serde_json::json;
use std::{
thread::{self},
time::Duration,
};

use integration_tests::{
examples::{self, run_example, TestSample},
ffmpeg::{start_ffmpeg_receive, start_ffmpeg_send},
};

const VIDEO_RESOLUTION: Resolution = Resolution {
width: 1280,
height: 720,
};

const IP: &str = "127.0.0.1";
const INPUT_PORT: u16 = 8002;
const OUTPUT_PORT: u16 = 8004;

fn main() {
run_example(client_code);
}

fn client_code() -> Result<()> {
start_ffmpeg_receive(Some(OUTPUT_PORT), None)?;

examples::post(
"input/input_1/register",
&json!({
"type": "rtp_stream",
"port": INPUT_PORT,
"video": {
"decoder": "ffmpeg_h264"
}
}),
)?;

examples::post(
"image/example_image/register",
&json!({
"asset_type": "gif",
"url": "https://gifdb.com/images/high/rust-logo-on-fire-o41c0v9om8drr8dv.gif",
}),
)?;

let scene1 = json!({
"type": "view",
"background_color_rgba": "#42daf5ff",
"children": [
{
"type": "rescaler",
"id": "resized",
"width": VIDEO_RESOLUTION.width,
"height": VIDEO_RESOLUTION.height,
"top": 0.0,
"right": 0.0,
"mode": "fill",
"border_color_rgba": "#FFFFFFFF",
"box_shadow": [
{
"offset_y": 40,
"offset_x": 0,
"blur_radius": 40,
"color_rgba": "#00000088",
}
],
"child": {
"type": "input_stream",
"input_id": "input_1"
}
}
]
});

let scene2 = json!({
"type": "view",
"background_color_rgba": "#42daf5ff",
"children": [
{
"type": "rescaler",
"id": "resized",
"width": 300,
"height": 300,
"top": (VIDEO_RESOLUTION.height as f32 - 330.0) / 2.0 ,
"right": (VIDEO_RESOLUTION.width as f32 - 330.0) / 2.0,
"mode": "fill",
"border_radius": 50,
"border_width": 15,
"border_color_rgba": "#FFFFFFFF",
"box_shadow": [
{
"offset_y": 40,
"offset_x": 0,
"blur_radius": 40,
"color_rgba": "#00000088",
}
],
"transition": {
"duration_ms": 1500,
"easing_function": {
"function_name": "cubic_bezier",
"points": [0.33, 1, 0.68, 1]
}
},
"child": {
"type": "input_stream",
"input_id": "input_1"
}
}
]
});

examples::post(
"output/output_1/register",
&json!({
"type": "rtp_stream",
"ip": IP,
"port": OUTPUT_PORT,
"video": {
"resolution": {
"width": VIDEO_RESOLUTION.width,
"height": VIDEO_RESOLUTION.height,
},
"encoder": {
"type": "ffmpeg_h264",
"preset": "ultrafast"
},
"initial": {
"root": scene1
}
}
}),
)?;

examples::post("start", &json!({}))?;

start_ffmpeg_send(IP, Some(INPUT_PORT), None, TestSample::TestPattern)?;

thread::sleep(Duration::from_secs(5));

examples::post(
"output/output_1/update",
&json!({
"video": {
"root": scene2,
}
}),
)?;

Ok(())
}
Loading

0 comments on commit 8d05e68

Please sign in to comment.