Skip to content

Commit

Permalink
Use explicit sort order instead of comparison impls for gpui prims
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
mgsloan committed Dec 2, 2024
1 parent f3140f5 commit 6ded849
Showing 1 changed file with 16 additions and 113 deletions.
129 changes: 16 additions & 113 deletions crates/gpui/src/scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,15 @@ impl Scene {
}

pub fn finish(&mut self) {
self.shadows.sort();
self.quads.sort();
self.paths.sort();
self.underlines.sort();
self.monochrome_sprites.sort();
self.polychrome_sprites.sort();
self.surfaces.sort();
self.shadows.sort_by_key(|shadow| shadow.order);
self.quads.sort_by_key(|quad| quad.order);
self.paths.sort_by_key(|path| path.order);
self.underlines.sort_by_key(|underline| underline.order);
self.monochrome_sprites
.sort_by_key(|sprite| (sprite.order, sprite.tile.tile_id));
self.polychrome_sprites
.sort_by_key(|sprite| (sprite.order, sprite.tile.tile_id));
self.surfaces.sort_by_key(|surface| surface.order);
}

#[cfg_attr(
Expand Down Expand Up @@ -196,7 +198,7 @@ pub(crate) enum PaintOperation {
EndLayer,
}

#[derive(Clone, Ord, PartialOrd, Eq, PartialEq)]
#[derive(Clone)]
pub(crate) enum Primitive {
Shadow(Shadow),
Quad(Quad),
Expand Down Expand Up @@ -449,7 +451,7 @@ pub(crate) enum PrimitiveBatch<'a> {
Surfaces(&'a [PaintSurface]),
}

#[derive(Default, Debug, Clone, Eq, PartialEq)]
#[derive(Default, Debug, Clone)]
#[repr(C)]
pub(crate) struct Quad {
pub order: DrawOrder,
Expand All @@ -462,25 +464,13 @@ pub(crate) struct Quad {
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 {
fn from(quad: Quad) -> Self {
Primitive::Quad(quad)
}
}

#[derive(Debug, Clone, Eq, PartialEq)]
#[derive(Debug, Clone)]
#[repr(C)]
pub(crate) struct Underline {
pub order: DrawOrder,
Expand All @@ -492,25 +482,13 @@ pub(crate) struct Underline {
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 {
fn from(underline: Underline) -> Self {
Primitive::Underline(underline)
}
}

#[derive(Debug, Clone, Eq, PartialEq)]
#[derive(Debug, Clone)]
#[repr(C)]
pub(crate) struct Shadow {
pub order: DrawOrder,
Expand All @@ -521,18 +499,6 @@ pub(crate) struct Shadow {
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 {
fn from(shadow: Shadow) -> Self {
Primitive::Shadow(shadow)
Expand Down Expand Up @@ -642,7 +608,7 @@ impl Default for TransformationMatrix {
}
}

#[derive(Clone, Debug, Eq, PartialEq)]
#[derive(Clone, Debug)]
#[repr(C)]
pub(crate) struct MonochromeSprite {
pub order: DrawOrder,
Expand All @@ -654,28 +620,13 @@ pub(crate) struct MonochromeSprite {
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 {
fn from(sprite: MonochromeSprite) -> Self {
Primitive::MonochromeSprite(sprite)
}
}

#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug)]
#[repr(C)]
pub(crate) struct PolychromeSprite {
pub order: DrawOrder,
Expand All @@ -687,30 +638,14 @@ pub(crate) struct PolychromeSprite {
pub corner_radii: Corners<ScaledPixels>,
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 {
fn from(sprite: PolychromeSprite) -> Self {
Primitive::PolychromeSprite(sprite)
}
}

#[derive(Clone, Debug, Eq, PartialEq)]
#[derive(Clone, Debug)]
pub(crate) struct PaintSurface {
pub order: DrawOrder,
pub bounds: Bounds<ScaledPixels>,
Expand All @@ -719,18 +654,6 @@ pub(crate) struct PaintSurface {
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 {
fn from(surface: PaintSurface) -> Self {
Primitive::Surface(surface)
Expand Down Expand Up @@ -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 {
fn from(path: Path<ScaledPixels>) -> Self {
Primitive::Path(path)
Expand Down

0 comments on commit 6ded849

Please sign in to comment.