Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve geometry APIs, to be closer to euclid #1621

Merged
merged 5 commits into from
Jan 2, 2025
Merged
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
26 changes: 13 additions & 13 deletions anvil/src/drawing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ where
} else {
3
};
Rectangle::from_loc_and_size((0, 0), (24 * digits, 35)).to_f64()
Rectangle::from_size((24 * digits, 35).into()).to_f64()
}

fn geometry(&self, scale: Scale<f64>) -> Rectangle<i32, Physical> {
Expand All @@ -182,7 +182,7 @@ where
} else {
3
};
Rectangle::from_loc_and_size((0, 0), (24 * digits, 35)).to_physical_precise_round(scale)
Rectangle::from_size((24 * digits, 35).into()).to_physical_precise_round(scale)
}

fn current_commit(&self) -> CommitCounter {
Expand Down Expand Up @@ -211,7 +211,7 @@ where
for digit in value_str.chars().map(|d| d.to_digit(10).unwrap()) {
let digit_location = dst.loc.to_f64() + offset;
let digit_size = Size::<i32, Logical>::from((22, 35)).to_f64().to_physical(scale);
let dst = Rectangle::from_loc_and_size(
let dst = Rectangle::new(
digit_location.to_i32_round(),
((digit_size.to_point() + digit_location).to_i32_round() - digit_location.to_i32_round())
.to_size(),
Expand All @@ -226,16 +226,16 @@ where
})
.collect::<Vec<_>>();
let texture_src: Rectangle<i32, Buffer> = match digit {
9 => Rectangle::from_loc_and_size((0, 0), (22, 35)),
6 => Rectangle::from_loc_and_size((22, 0), (22, 35)),
3 => Rectangle::from_loc_and_size((44, 0), (22, 35)),
1 => Rectangle::from_loc_and_size((66, 0), (22, 35)),
8 => Rectangle::from_loc_and_size((0, 35), (22, 35)),
0 => Rectangle::from_loc_and_size((22, 35), (22, 35)),
2 => Rectangle::from_loc_and_size((44, 35), (22, 35)),
7 => Rectangle::from_loc_and_size((0, 70), (22, 35)),
4 => Rectangle::from_loc_and_size((22, 70), (22, 35)),
5 => Rectangle::from_loc_and_size((44, 70), (22, 35)),
9 => Rectangle::from_size((22, 35).into()),
6 => Rectangle::new((22, 0).into(), (22, 35).into()),
3 => Rectangle::new((44, 0).into(), (22, 35).into()),
1 => Rectangle::new((66, 0).into(), (22, 35).into()),
8 => Rectangle::new((0, 35).into(), (22, 35).into()),
0 => Rectangle::new((22, 35).into(), (22, 35).into()),
2 => Rectangle::new((44, 35).into(), (22, 35).into()),
7 => Rectangle::new((0, 70).into(), (22, 35).into()),
4 => Rectangle::new((22, 70).into(), (22, 35).into()),
5 => Rectangle::new((44, 70).into(), (22, 35).into()),
_ => unreachable!(),
};

Expand Down
2 changes: 1 addition & 1 deletion anvil/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ where
preview_padding + (preview_padding + preview_size.w) * column as i32,
preview_padding + (preview_padding + preview_size.h) * row as i32,
));
let constrain = Rectangle::from_loc_and_size(preview_location, preview_size);
let constrain = Rectangle::new(preview_location, preview_size);
constrain_space_element(
renderer,
window,
Expand Down
8 changes: 4 additions & 4 deletions anvil/src/shell/grabs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ impl<BackendData: Backend> PointerGrab<AnvilState<BackendData>> for PointerResiz
#[cfg(feature = "xwayland")]
WindowSurface::X11(x11) => {
let location = data.space.element_location(&self.window).unwrap();
x11.configure(Rectangle::from_loc_and_size(location, self.last_window_size))
x11.configure(Rectangle::new(location, self.last_window_size))
.unwrap();
}
}
Expand Down Expand Up @@ -505,7 +505,7 @@ impl<BackendData: Backend> PointerGrab<AnvilState<BackendData>> for PointerResiz

data.space.map_element(self.window.clone(), location, true);
}
x11.configure(Rectangle::from_loc_and_size(location, self.last_window_size))
x11.configure(Rectangle::new(location, self.last_window_size))
.unwrap();

let Some(surface) = self.window.wl_surface() else {
Expand Down Expand Up @@ -718,7 +718,7 @@ impl<BackendData: Backend> TouchGrab<AnvilState<BackendData>> for TouchResizeSur

data.space.map_element(self.window.clone(), location, true);
}
x11.configure(Rectangle::from_loc_and_size(location, self.last_window_size))
x11.configure(Rectangle::new(location, self.last_window_size))
.unwrap();

let Some(surface) = self.window.wl_surface() else {
Expand Down Expand Up @@ -817,7 +817,7 @@ impl<BackendData: Backend> TouchGrab<AnvilState<BackendData>> for TouchResizeSur
#[cfg(feature = "xwayland")]
WindowSurface::X11(x11) => {
let location = data.space.element_location(&self.window).unwrap();
x11.configure(Rectangle::from_loc_and_size(location, self.last_window_size))
x11.configure(Rectangle::new(location, self.last_window_size))
.unwrap();
}
}
Expand Down
6 changes: 3 additions & 3 deletions anvil/src/shell/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,9 +411,9 @@ fn place_new_window(
let geo = space.output_geometry(&o)?;
let map = layer_map_for_output(&o);
let zone = map.non_exclusive_zone();
Some(Rectangle::from_loc_and_size(geo.loc + zone.loc, zone.size))
Some(Rectangle::new(geo.loc + zone.loc, zone.size))
})
.unwrap_or_else(|| Rectangle::from_loc_and_size((0, 0), (800, 800)));
.unwrap_or_else(|| Rectangle::from_size((800, 800).into()));

// set the initial toplevel bounds
#[allow(irrefutable_let_patterns)]
Expand Down Expand Up @@ -455,7 +455,7 @@ pub fn fixup_positions(space: &mut Space<WindowElement>, pointer_location: Point
let geo = space.output_geometry(o)?;
let map = layer_map_for_output(o);
let zone = map.non_exclusive_zone();
Some(Rectangle::from_loc_and_size(geo.loc + zone.loc, zone.size))
Some(Rectangle::new(geo.loc + zone.loc, zone.size))
})
.collect::<Vec<_>>();
for window in space.elements() {
Expand Down
10 changes: 2 additions & 8 deletions anvil/src/shell/x11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,10 +367,7 @@ impl<BackendData: Backend> AnvilState<BackendData> {
.and_then(|data| data.restore())
{
window
.configure(Rectangle::from_loc_and_size(
initial_window_location,
old_geo.size,
))
.configure(Rectangle::new(initial_window_location, old_geo.size))
.unwrap();
}
}
Expand Down Expand Up @@ -413,10 +410,7 @@ impl<BackendData: Backend> AnvilState<BackendData> {
.and_then(|data| data.restore())
{
window
.configure(Rectangle::from_loc_and_size(
initial_window_location,
old_geo.size,
))
.configure(Rectangle::new(initial_window_location, old_geo.size))
.unwrap();
}
}
Expand Down
4 changes: 2 additions & 2 deletions benches/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn criterion_benchmark(c: &mut Criterion) {
let mut rand = rand::thread_rng();
let x = rand.gen_range(0..max_x);
let y = rand.gen_range(0..max_y);
let test_element = Rectangle::from_loc_and_size((x, y), element_size);
let test_element = Rectangle::new((x, y).into(), element_size);

let x_min = (test_element.loc.x - element_size.w) + 1;
let x_max = (test_element.loc.x + element_size.w) - 1;
Expand All @@ -35,7 +35,7 @@ fn criterion_benchmark(c: &mut Criterion) {
.map(|_| {
let x = rand.gen_range(x_min..=x_max);
let y = rand.gen_range(y_min..=y_max);
Rectangle::from_loc_and_size((x, y), element_size)
Rectangle::new((x, y).into(), element_size)
})
.collect::<Vec<_>>();

Expand Down
12 changes: 6 additions & 6 deletions examples/buffer_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,25 +287,25 @@ where
frame
.clear(
Color32F::new(1.0, 0.0, 0.0, 1.0),
&[Rectangle::from_loc_and_size((0, 0), (w / 2, h / 2))],
&[Rectangle::from_size((w / 2, h / 2).into())],
)
.expect("Render error");
frame
.clear(
Color32F::new(0.0, 1.0, 0.0, 1.0),
&[Rectangle::from_loc_and_size((w / 2, 0), (w / 2, h / 2))],
&[Rectangle::new((w / 2, 0).into(), (w / 2, h / 2).into())],
)
.expect("Render error");
frame
.clear(
Color32F::new(0.0, 0.0, 1.0, 1.0),
&[Rectangle::from_loc_and_size((0, h / 2), (w / 2, h / 2))],
&[Rectangle::new((0, h / 2).into(), (w / 2, h / 2).into())],
)
.expect("Render error");
frame
.clear(
Color32F::new(1.0, 1.0, 0.0, 1.0),
&[Rectangle::from_loc_and_size((w / 2, h / 2), (w / 2, h / 2))],
&[Rectangle::new((w / 2, h / 2).into(), (w / 2, h / 2).into())],
)
.expect("Render error");
frame
Expand Down Expand Up @@ -335,7 +335,7 @@ where
1,
1.,
Transform::Normal,
&[Rectangle::from_loc_and_size((0, 0), (w, h))],
&[Rectangle::from_size((w, h).into())],
&[],
1.0,
)
Expand All @@ -348,7 +348,7 @@ where

if let Some(path) = dump {
let mapping = renderer
.copy_framebuffer(Rectangle::from_loc_and_size((0, 0), (w, h)), Fourcc::Abgr8888)
.copy_framebuffer(Rectangle::from_size((w, h).into()), Fourcc::Abgr8888)
.expect("Failed to map framebuffer");
let copy = renderer.map_texture(&mapping).expect("Failed to read mapping");
image::save_buffer(path, copy, w as u32, h as u32, image::ColorType::Rgba8)
Expand Down
2 changes: 1 addition & 1 deletion examples/minimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ pub fn run_winit() -> Result<(), Box<dyn std::error::Error>> {
backend.bind().unwrap();

let size = backend.window_size();
let damage = Rectangle::from_loc_and_size((0, 0), size);
let damage = Rectangle::from_size(size);

let elements = state
.xdg_shell_state
Expand Down
2 changes: 1 addition & 1 deletion smallvil/src/handlers/xdg_shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl XdgShellHandler for Smallvil {
start_data,
window,
edges.into(),
Rectangle::from_loc_and_size(initial_window_location, initial_window_size),
Rectangle::new(initial_window_location, initial_window_size),
);

pointer.set_grab(self, grab, serial, Focus::Clear);
Expand Down
2 changes: 1 addition & 1 deletion smallvil/src/winit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub fn init_winit(
WinitEvent::Input(event) => state.process_input_event(event),
WinitEvent::Redraw => {
let size = backend.window_size();
let damage = Rectangle::from_loc_and_size((0, 0), size);
let damage = Rectangle::from_size(size);

backend.bind().unwrap();
smithay::desktop::space::render_output::<_, WaylandSurfaceRenderElement<GlesRenderer>, _, _>(
Expand Down
4 changes: 2 additions & 2 deletions src/backend/drm/compositor/elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
Color32F, Frame, Renderer,
},
render_elements,
utils::{Buffer, Physical, Point, Rectangle, Scale, Transform},
utils::{Buffer, Physical, Rectangle, Scale, Transform},
};

render_elements! {
Expand Down Expand Up @@ -81,7 +81,7 @@ impl Element for HolepunchRenderElement {
}

fn opaque_regions(&self, _scale: Scale<f64>) -> OpaqueRegions<i32, Physical> {
OpaqueRegions::from_slice(&[Rectangle::from_loc_and_size(Point::default(), self.geometry.size)])
OpaqueRegions::from_slice(&[Rectangle::from_size(self.geometry.size)])
}
}

Expand Down
12 changes: 3 additions & 9 deletions src/backend/drm/compositor/frame_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,11 @@ impl<B: Buffer> Element for SwapchainElement<'_, '_, B> {
}

fn src(&self) -> Rectangle<f64, BufferCoords> {
Rectangle::from_loc_and_size((0, 0), self.slot.size()).to_f64()
Rectangle::from_size(self.slot.size()).to_f64()
}

fn geometry(&self, _scale: Scale<f64>) -> Rectangle<i32, Physical> {
Rectangle::from_loc_and_size(
(0, 0),
self.slot.size().to_logical(1, self.transform).to_physical(1),
)
Rectangle::from_size(self.slot.size().to_logical(1, self.transform).to_physical(1))
}

fn transform(&self) -> Transform {
Expand Down Expand Up @@ -319,10 +316,7 @@ where
_ => unreachable!(),
};
let size = dmabuf.size();
let geometry = Rectangle::from_loc_and_size(
(0, 0),
size.to_logical(1, Transform::Normal).to_physical(1),
);
let geometry = Rectangle::from_size(size.to_logical(1, Transform::Normal).to_physical(1));
opaque_regions.push(geometry);
Some((sync.clone(), dmabuf, geometry))
}
Expand Down
21 changes: 9 additions & 12 deletions src/backend/drm/compositor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1547,8 +1547,8 @@ where
element_state: None,
config: Some(PlaneConfig {
properties: PlaneProperties {
src: Rectangle::from_loc_and_size(Point::default(), dmabuf.size()).to_f64(),
dst: Rectangle::from_loc_and_size(Point::default(), mode_size),
src: Rectangle::from_size(dmabuf.size()).to_f64(),
dst: Rectangle::from_size(mode_size),
transform: Transform::Normal,
alpha: 1.0,
format: buffer.format(),
Expand Down Expand Up @@ -1717,7 +1717,7 @@ where
// The renderer (and also the logic for direct scan-out) will take care of the
// actual transform during rendering
let output_geometry: Rectangle<_, Physical> =
Rectangle::from_loc_and_size((0, 0), output_transform.transform_size(current_size));
Rectangle::from_size(output_transform.transform_size(current_size));

// We always acquire a buffer from the swapchain even
// if we could end up doing direct scan-out on the primary plane.
Expand Down Expand Up @@ -1813,8 +1813,8 @@ where
element_state: None,
config: Some(PlaneConfig {
properties: PlaneProperties {
src: Rectangle::from_loc_and_size(Point::default(), dmabuf.size()).to_f64(),
dst: Rectangle::from_loc_and_size(Point::default(), current_size),
src: Rectangle::from_size(dmabuf.size()).to_f64(),
dst: Rectangle::from_size(current_size),
// NOTE: We do not apply the transform to the primary plane as this is handled by the dtr/renderer
transform: Transform::Normal,
alpha: 1.0,
Expand Down Expand Up @@ -3318,12 +3318,9 @@ where
pixman_renderer.bind(PixmanRenderBuffer::from(cursor_dst))?;

let mut frame = pixman_renderer.render(cursor_plane_size, output_transform)?;
frame.clear(
Color32F::TRANSPARENT,
&[Rectangle::from_loc_and_size((0, 0), cursor_plane_size)],
)?;
frame.clear(Color32F::TRANSPARENT, &[Rectangle::from_size(cursor_plane_size)])?;
let src = element.src();
let dst = Rectangle::from_loc_and_size((0, 0), element_geometry.size);
let dst = Rectangle::from_size(element_geometry.size);
frame.render_texture_from_to(
&cursor_texture,
src,
Expand All @@ -3347,8 +3344,8 @@ where
}
};

let src = Rectangle::from_loc_and_size(Point::default(), cursor_buffer_size).to_f64();
let dst = Rectangle::from_loc_and_size(cursor_plane_location, cursor_plane_size);
let src = Rectangle::from_size(cursor_buffer_size).to_f64();
let dst = Rectangle::new(cursor_plane_location, cursor_plane_size);

let config = PlaneConfig {
properties: PlaneProperties {
Expand Down
Loading
Loading