Add Lighten and Darken composite ops

This commit is contained in:
Patrick Walton 2020-02-20 20:38:43 -08:00
parent ab55b9509b
commit 4933efb513
8 changed files with 58 additions and 4 deletions

2
Cargo.lock generated
View File

@ -219,7 +219,7 @@ dependencies = [
name = "canvas_metal_minimal"
version = "0.1.0"
dependencies = [
"foreign-types 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"foreign-types 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
"gl 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)",
"metal 0.17.1 (registry+https://github.com/rust-lang/crates.io-index)",
"objc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@ -232,6 +232,7 @@ impl CanvasRenderingContext2D {
fn push_path(&mut self, outline: Outline, paint_id: PaintId, fill_rule: FillRule) {
let clip_path = self.current_state.clip_path;
let blend_mode = self.current_state.global_composite_operation.to_blend_mode();
if !self.current_state.shadow_paint.is_fully_transparent() {
let paint = self.current_state.resolve_paint(&self.current_state.shadow_paint);
@ -243,7 +244,7 @@ impl CanvasRenderingContext2D {
paint_id,
clip_path,
fill_rule,
BlendMode::SourceOver,
blend_mode,
String::new()))
}
@ -251,7 +252,7 @@ impl CanvasRenderingContext2D {
paint_id,
clip_path,
fill_rule,
BlendMode::SourceOver,
blend_mode,
String::new()))
}
@ -284,6 +285,16 @@ impl CanvasRenderingContext2D {
self.current_state.global_alpha = new_global_alpha;
}
#[inline]
pub fn global_composite_operation(&self) -> CompositeOperation {
self.current_state.global_composite_operation
}
#[inline]
pub fn set_global_composite_operation(&mut self, new_composite_operation: CompositeOperation) {
self.current_state.global_composite_operation = new_composite_operation;
}
// The canvas state
#[inline]
@ -316,6 +327,7 @@ struct State {
shadow_offset: Vector2F,
text_align: TextAlign,
global_alpha: f32,
global_composite_operation: CompositeOperation,
clip_path: Option<ClipPathId>,
}
@ -337,6 +349,7 @@ impl State {
shadow_offset: Vector2F::default(),
text_align: TextAlign::Left,
global_alpha: 1.0,
global_composite_operation: CompositeOperation::SourceOver,
clip_path: None,
}
}
@ -507,3 +520,20 @@ pub enum LineJoin {
Bevel,
Round,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum CompositeOperation {
SourceOver,
Lighten,
Darken,
}
impl CompositeOperation {
fn to_blend_mode(self) -> BlendMode {
match self {
CompositeOperation::SourceOver => BlendMode::SourceOver,
CompositeOperation::Lighten => BlendMode::Lighten,
CompositeOperation::Darken => BlendMode::Darken,
}
}
}

View File

@ -66,6 +66,8 @@ pub enum CompositeOp {
pub enum BlendMode {
SourceOver,
Clear,
Lighten,
Darken,
}
#[derive(Clone, Copy, PartialEq, Debug)]
@ -92,6 +94,7 @@ impl BlendMode {
pub fn occludes_backdrop(self) -> bool {
match self {
BlendMode::SourceOver | BlendMode::Clear => true,
BlendMode::Lighten | BlendMode::Darken => false,
}
}
}

View File

@ -5,7 +5,7 @@ authors = ["Patrick Walton <pcwalton@mimiga.net>"]
edition = "2018"
[dependencies]
foreign-types = "0.5"
foreign-types = "0.3"
gl = "0.14"
metal = "0.17"
objc = "0.2"

View File

@ -965,6 +965,7 @@ impl BlendFactorExt for BlendFactor {
BlendFactor::One => gl::ONE,
BlendFactor::SrcAlpha => gl::SRC_ALPHA,
BlendFactor::OneMinusSrcAlpha => gl::ONE_MINUS_SRC_ALPHA,
BlendFactor::DestColor => gl::DST_COLOR,
}
}
}

View File

@ -226,6 +226,7 @@ pub enum BlendFactor {
One,
SrcAlpha,
OneMinusSrcAlpha,
DestColor,
}
#[derive(Clone, Copy, Debug, PartialEq)]

View File

@ -1170,6 +1170,7 @@ impl BlendFactorExt for BlendFactor {
BlendFactor::One => MTLBlendFactor::One,
BlendFactor::SrcAlpha => MTLBlendFactor::SourceAlpha,
BlendFactor::OneMinusSrcAlpha => MTLBlendFactor::OneMinusSourceAlpha,
BlendFactor::DestColor => MTLBlendFactor::DestinationColor,
}
}
}

View File

@ -1151,6 +1151,24 @@ impl BlendModeExt for BlendMode {
..BlendState::default()
}
}
BlendMode::Lighten => {
BlendState {
src_rgb_factor: BlendFactor::SrcAlpha,
dest_rgb_factor: BlendFactor::OneMinusSrcAlpha,
src_alpha_factor: BlendFactor::One,
dest_alpha_factor: BlendFactor::One,
op: BlendOp::Max,
}
}
BlendMode::Darken => {
BlendState {
src_rgb_factor: BlendFactor::SrcAlpha,
dest_rgb_factor: BlendFactor::OneMinusSrcAlpha,
src_alpha_factor: BlendFactor::One,
dest_alpha_factor: BlendFactor::One,
op: BlendOp::Min,
}
}
}
}
}