From 9d6eef94f47ff50bbe6999abe78a0e3d5b420efd Mon Sep 17 00:00:00 2001 From: Denis Tsai Date: Sun, 17 Mar 2024 11:19:45 +0800 Subject: [PATCH] feat: optimize some ifs in hotpath (#23) * feat: optimize some ifs in hotpath * fix: optimize to unwrap_or_default for dark color and set 0.0 case for premultiplied alpha * fix ci --------- Co-authored-by: jabu <46233424+jabuwu@users.noreply.github.com> --- src/draw/combined.rs | 28 +++++++++++----------------- src/draw/simple.rs | 14 +++++--------- 2 files changed, 16 insertions(+), 26 deletions(-) diff --git a/src/draw/combined.rs b/src/draw/combined.rs index 673ba41..372458c 100644 --- a/src/draw/combined.rs +++ b/src/draw/combined.rs @@ -1,4 +1,4 @@ -use crate::{c::c_void, BlendMode, Color, Skeleton, SkeletonClipping}; +use crate::{c::c_void, BlendMode, Skeleton, SkeletonClipping}; use super::{ColorSpace, CullDirection}; @@ -186,21 +186,19 @@ impl CombinedDrawer { slot.attachment().and_then(|a| a.as_mesh()) { let mut color = mesh_attachment.color() * slot.color() * skeleton.color(); + let mut dark_color = slot.dark_color().unwrap_or_default(); if self.premultiplied_alpha { color.premultiply_alpha(); + dark_color *= color.a; + dark_color.a = 1.0; + } else { + dark_color.a = 0.; } color = match self.color_space { ColorSpace::SRGB => color, ColorSpace::Linear => color.nonlinear_to_linear(), }; - let mut dark_color = slot - .dark_color() - .unwrap_or_else(|| Color::new_rgba(0.0, 0.0, 0.0, 0.0)); - if self.premultiplied_alpha { - dark_color *= color.a; - dark_color.a = 1.0; - } dark_color = match self.color_space { ColorSpace::SRGB => dark_color, ColorSpace::Linear => dark_color.nonlinear_to_linear(), @@ -273,23 +271,19 @@ impl CombinedDrawer { (color, dark_color) } else if let Some(region_attachment) = slot.attachment().and_then(|a| a.as_region()) { let mut color = region_attachment.color() * slot.color() * skeleton.color(); + let mut dark_color = slot.dark_color().unwrap_or_default(); if self.premultiplied_alpha { color.premultiply_alpha(); + dark_color *= color.a; + dark_color.a = 1.0; + } else { + dark_color.a = 0.; } color = match self.color_space { ColorSpace::SRGB => color, ColorSpace::Linear => color.nonlinear_to_linear(), }; - let mut dark_color = slot - .dark_color() - .unwrap_or_else(|| Color::new_rgba(0.0, 0.0, 0.0, 0.0)); - if self.premultiplied_alpha { - dark_color *= color.a; - dark_color.a = 1.0; - } else { - dark_color.a = 0.0; - } dark_color = match self.color_space { ColorSpace::SRGB => dark_color, ColorSpace::Linear => dark_color.nonlinear_to_linear(), diff --git a/src/draw/simple.rs b/src/draw/simple.rs index df9117f..316e9a8 100644 --- a/src/draw/simple.rs +++ b/src/draw/simple.rs @@ -284,23 +284,19 @@ impl SimpleDrawer { ); color *= slot.color() * skeleton.color(); + let mut dark_color = slot.dark_color().unwrap_or_default(); if self.premultiplied_alpha { color.premultiply_alpha(); + dark_color *= color.a; + dark_color.a = 1.0; + } else { + dark_color.a = 0.; } color = match self.color_space { ColorSpace::SRGB => color, ColorSpace::Linear => color.nonlinear_to_linear(), }; - let mut dark_color = slot - .dark_color() - .unwrap_or_else(|| Color::new_rgba(0.0, 0.0, 0.0, 0.0)); - if self.premultiplied_alpha { - dark_color *= color.a; - dark_color.a = 1.0; - } else { - dark_color.a = 0.0; - } dark_color = match self.color_space { ColorSpace::SRGB => dark_color, ColorSpace::Linear => dark_color.nonlinear_to_linear(),