Skip to content

Commit

Permalink
clamp the damage rect to the destination rect
Browse files Browse the repository at this point in the history
this fixes issues when the damage rect is greater
than the destination rect, like providing i32::Max as
the damage size
  • Loading branch information
cmeissl authored and Drakulix committed Jan 7, 2022
1 parent 7592991 commit 9f5bf25
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/backend/renderer/gles2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1286,11 +1286,18 @@ impl Frame for Gles2Frame {
let damage = damage
.iter()
.map(|rect| {
let rect = rect.to_f64();

let rect_constrained_loc = rect
.loc
.constrain(Rectangle::from_extemities((0f64, 0f64), dest.size.to_point()));
let rect_clamped_size = rect.size.clamp((0f64, 0f64), dest.size);

[
rect.loc.x as f32 / dest.size.w as f32,
rect.loc.y as f32 / dest.size.h as f32,
rect.size.w as f32 / dest.size.w as f32,
rect.size.h as f32 / dest.size.h as f32,
(rect_constrained_loc.x / dest.size.w) as f32,
(rect_constrained_loc.y / dest.size.h) as f32,
(rect_clamped_size.w / dest.size.w) as f32,
(rect_clamped_size.h / dest.size.h) as f32,
]
})
.flatten()
Expand Down
31 changes: 31 additions & 0 deletions src/utils/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,23 @@ impl<N: Coordinate, Kind> Point<N, Kind> {
}
}

impl<N: Coordinate, Kind> Point<N, Kind> {
/// Constrain this [`Point`] within a [`Rectangle`] with the same coordinates
///
/// The [`Point`] returned is guaranteed to be not smaller than the [`Rectangle`]
/// location and not greater than the [`Rectangle`] size.
#[inline]
pub fn constrain(self, rect: impl Into<Rectangle<N, Kind>>) -> Point<N, Kind> {
let rect = rect.into();

Point {
x: self.x.max(rect.loc.x).min(rect.size.w),
y: self.y.max(rect.loc.y).min(rect.size.h),
_kind: std::marker::PhantomData,
}
}
}

impl<N: Coordinate, Kind> Point<N, Kind> {
/// Convert the underlying numerical type to f64 for floating point manipulations
#[inline]
Expand Down Expand Up @@ -543,6 +560,20 @@ impl<N: Coordinate, Kind> Size<N, Kind> {
}
}

impl<N: Coordinate, Kind> Size<N, Kind> {
/// Restrict this [`Size`] to min and max [`Size`] with the same coordinates
pub fn clamp(self, min: impl Into<Size<N, Kind>>, max: impl Into<Size<N, Kind>>) -> Size<N, Kind> {
let min = min.into();
let max = max.into();

Size {
w: self.w.max(min.w).min(max.w),
h: self.h.max(min.h).min(max.h),
_kind: std::marker::PhantomData,
}
}
}

impl<N: Coordinate, Kind> Size<N, Kind> {
/// Convert the underlying numerical type to f64 for floating point manipulations
#[inline]
Expand Down

0 comments on commit 9f5bf25

Please sign in to comment.