Use explicit sort order instead of comparison impls for gpui prims (#21430)

Found this while looking into adding support for the Surface primitive
on Linux, for rendering video shares. In that case it would be
expensive to compare images for equality. `Eq` and `PartialEq` were
being required but not used here due to use of `Ord` and `PartialOrd`.

Release Notes:

- N/A
This commit is contained in:
Michael Sloan 2024-12-02 16:27:29 -07:00 committed by GitHub
parent 579bc8f015
commit f4dbcb6714
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -128,13 +128,15 @@ impl Scene {
} }
pub fn finish(&mut self) { pub fn finish(&mut self) {
self.shadows.sort(); self.shadows.sort_by_key(|shadow| shadow.order);
self.quads.sort(); self.quads.sort_by_key(|quad| quad.order);
self.paths.sort(); self.paths.sort_by_key(|path| path.order);
self.underlines.sort(); self.underlines.sort_by_key(|underline| underline.order);
self.monochrome_sprites.sort(); self.monochrome_sprites
self.polychrome_sprites.sort(); .sort_by_key(|sprite| (sprite.order, sprite.tile.tile_id));
self.surfaces.sort(); self.polychrome_sprites
.sort_by_key(|sprite| (sprite.order, sprite.tile.tile_id));
self.surfaces.sort_by_key(|surface| surface.order);
} }
#[cfg_attr( #[cfg_attr(
@ -196,7 +198,7 @@ pub(crate) enum PaintOperation {
EndLayer, EndLayer,
} }
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq)] #[derive(Clone)]
pub(crate) enum Primitive { pub(crate) enum Primitive {
Shadow(Shadow), Shadow(Shadow),
Quad(Quad), Quad(Quad),
@ -449,7 +451,7 @@ pub(crate) enum PrimitiveBatch<'a> {
Surfaces(&'a [PaintSurface]), Surfaces(&'a [PaintSurface]),
} }
#[derive(Default, Debug, Clone, Eq, PartialEq)] #[derive(Default, Debug, Clone)]
#[repr(C)] #[repr(C)]
pub(crate) struct Quad { pub(crate) struct Quad {
pub order: DrawOrder, pub order: DrawOrder,
@ -462,25 +464,13 @@ pub(crate) struct Quad {
pub border_widths: Edges<ScaledPixels>, pub border_widths: Edges<ScaledPixels>,
} }
impl Ord for Quad {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.order.cmp(&other.order)
}
}
impl PartialOrd for Quad {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl From<Quad> for Primitive { impl From<Quad> for Primitive {
fn from(quad: Quad) -> Self { fn from(quad: Quad) -> Self {
Primitive::Quad(quad) Primitive::Quad(quad)
} }
} }
#[derive(Debug, Clone, Eq, PartialEq)] #[derive(Debug, Clone)]
#[repr(C)] #[repr(C)]
pub(crate) struct Underline { pub(crate) struct Underline {
pub order: DrawOrder, pub order: DrawOrder,
@ -492,25 +482,13 @@ pub(crate) struct Underline {
pub wavy: bool, pub wavy: bool,
} }
impl Ord for Underline {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.order.cmp(&other.order)
}
}
impl PartialOrd for Underline {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl From<Underline> for Primitive { impl From<Underline> for Primitive {
fn from(underline: Underline) -> Self { fn from(underline: Underline) -> Self {
Primitive::Underline(underline) Primitive::Underline(underline)
} }
} }
#[derive(Debug, Clone, Eq, PartialEq)] #[derive(Debug, Clone)]
#[repr(C)] #[repr(C)]
pub(crate) struct Shadow { pub(crate) struct Shadow {
pub order: DrawOrder, pub order: DrawOrder,
@ -521,18 +499,6 @@ pub(crate) struct Shadow {
pub color: Hsla, pub color: Hsla,
} }
impl Ord for Shadow {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.order.cmp(&other.order)
}
}
impl PartialOrd for Shadow {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl From<Shadow> for Primitive { impl From<Shadow> for Primitive {
fn from(shadow: Shadow) -> Self { fn from(shadow: Shadow) -> Self {
Primitive::Shadow(shadow) Primitive::Shadow(shadow)
@ -642,7 +608,7 @@ impl Default for TransformationMatrix {
} }
} }
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug)]
#[repr(C)] #[repr(C)]
pub(crate) struct MonochromeSprite { pub(crate) struct MonochromeSprite {
pub order: DrawOrder, pub order: DrawOrder,
@ -654,28 +620,13 @@ pub(crate) struct MonochromeSprite {
pub transformation: TransformationMatrix, pub transformation: TransformationMatrix,
} }
impl Ord for MonochromeSprite {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
match self.order.cmp(&other.order) {
std::cmp::Ordering::Equal => self.tile.tile_id.cmp(&other.tile.tile_id),
order => order,
}
}
}
impl PartialOrd for MonochromeSprite {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl From<MonochromeSprite> for Primitive { impl From<MonochromeSprite> for Primitive {
fn from(sprite: MonochromeSprite) -> Self { fn from(sprite: MonochromeSprite) -> Self {
Primitive::MonochromeSprite(sprite) Primitive::MonochromeSprite(sprite)
} }
} }
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug)]
#[repr(C)] #[repr(C)]
pub(crate) struct PolychromeSprite { pub(crate) struct PolychromeSprite {
pub order: DrawOrder, pub order: DrawOrder,
@ -687,22 +638,6 @@ pub(crate) struct PolychromeSprite {
pub corner_radii: Corners<ScaledPixels>, pub corner_radii: Corners<ScaledPixels>,
pub tile: AtlasTile, pub tile: AtlasTile,
} }
impl Eq for PolychromeSprite {}
impl Ord for PolychromeSprite {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
match self.order.cmp(&other.order) {
std::cmp::Ordering::Equal => self.tile.tile_id.cmp(&other.tile.tile_id),
order => order,
}
}
}
impl PartialOrd for PolychromeSprite {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl From<PolychromeSprite> for Primitive { impl From<PolychromeSprite> for Primitive {
fn from(sprite: PolychromeSprite) -> Self { fn from(sprite: PolychromeSprite) -> Self {
@ -710,7 +645,7 @@ impl From<PolychromeSprite> for Primitive {
} }
} }
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug)]
pub(crate) struct PaintSurface { pub(crate) struct PaintSurface {
pub order: DrawOrder, pub order: DrawOrder,
pub bounds: Bounds<ScaledPixels>, pub bounds: Bounds<ScaledPixels>,
@ -719,18 +654,6 @@ pub(crate) struct PaintSurface {
pub image_buffer: media::core_video::CVImageBuffer, pub image_buffer: media::core_video::CVImageBuffer,
} }
impl Ord for PaintSurface {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.order.cmp(&other.order)
}
}
impl PartialOrd for PaintSurface {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl From<PaintSurface> for Primitive { impl From<PaintSurface> for Primitive {
fn from(surface: PaintSurface) -> Self { fn from(surface: PaintSurface) -> Self {
Primitive::Surface(surface) Primitive::Surface(surface)
@ -859,26 +782,6 @@ impl Path<Pixels> {
} }
} }
impl Eq for Path<ScaledPixels> {}
impl PartialEq for Path<ScaledPixels> {
fn eq(&self, other: &Self) -> bool {
self.order == other.order
}
}
impl Ord for Path<ScaledPixels> {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.order.cmp(&other.order)
}
}
impl PartialOrd for Path<ScaledPixels> {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl From<Path<ScaledPixels>> for Primitive { impl From<Path<ScaledPixels>> for Primitive {
fn from(path: Path<ScaledPixels>) -> Self { fn from(path: Path<ScaledPixels>) -> Self {
Primitive::Path(path) Primitive::Path(path)