Add Lighten and Darken composite ops
This commit is contained in:
parent
ab55b9509b
commit
4933efb513
|
@ -219,7 +219,7 @@ dependencies = [
|
||||||
name = "canvas_metal_minimal"
|
name = "canvas_metal_minimal"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
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)",
|
"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)",
|
"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)",
|
"objc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
|
|
@ -232,6 +232,7 @@ impl CanvasRenderingContext2D {
|
||||||
|
|
||||||
fn push_path(&mut self, outline: Outline, paint_id: PaintId, fill_rule: FillRule) {
|
fn push_path(&mut self, outline: Outline, paint_id: PaintId, fill_rule: FillRule) {
|
||||||
let clip_path = self.current_state.clip_path;
|
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() {
|
if !self.current_state.shadow_paint.is_fully_transparent() {
|
||||||
let paint = self.current_state.resolve_paint(&self.current_state.shadow_paint);
|
let paint = self.current_state.resolve_paint(&self.current_state.shadow_paint);
|
||||||
|
@ -243,7 +244,7 @@ impl CanvasRenderingContext2D {
|
||||||
paint_id,
|
paint_id,
|
||||||
clip_path,
|
clip_path,
|
||||||
fill_rule,
|
fill_rule,
|
||||||
BlendMode::SourceOver,
|
blend_mode,
|
||||||
String::new()))
|
String::new()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -251,7 +252,7 @@ impl CanvasRenderingContext2D {
|
||||||
paint_id,
|
paint_id,
|
||||||
clip_path,
|
clip_path,
|
||||||
fill_rule,
|
fill_rule,
|
||||||
BlendMode::SourceOver,
|
blend_mode,
|
||||||
String::new()))
|
String::new()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -284,6 +285,16 @@ impl CanvasRenderingContext2D {
|
||||||
self.current_state.global_alpha = new_global_alpha;
|
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
|
// The canvas state
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
@ -316,6 +327,7 @@ struct State {
|
||||||
shadow_offset: Vector2F,
|
shadow_offset: Vector2F,
|
||||||
text_align: TextAlign,
|
text_align: TextAlign,
|
||||||
global_alpha: f32,
|
global_alpha: f32,
|
||||||
|
global_composite_operation: CompositeOperation,
|
||||||
clip_path: Option<ClipPathId>,
|
clip_path: Option<ClipPathId>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -337,6 +349,7 @@ impl State {
|
||||||
shadow_offset: Vector2F::default(),
|
shadow_offset: Vector2F::default(),
|
||||||
text_align: TextAlign::Left,
|
text_align: TextAlign::Left,
|
||||||
global_alpha: 1.0,
|
global_alpha: 1.0,
|
||||||
|
global_composite_operation: CompositeOperation::SourceOver,
|
||||||
clip_path: None,
|
clip_path: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -507,3 +520,20 @@ pub enum LineJoin {
|
||||||
Bevel,
|
Bevel,
|
||||||
Round,
|
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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -66,6 +66,8 @@ pub enum CompositeOp {
|
||||||
pub enum BlendMode {
|
pub enum BlendMode {
|
||||||
SourceOver,
|
SourceOver,
|
||||||
Clear,
|
Clear,
|
||||||
|
Lighten,
|
||||||
|
Darken,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, PartialEq, Debug)]
|
#[derive(Clone, Copy, PartialEq, Debug)]
|
||||||
|
@ -92,6 +94,7 @@ impl BlendMode {
|
||||||
pub fn occludes_backdrop(self) -> bool {
|
pub fn occludes_backdrop(self) -> bool {
|
||||||
match self {
|
match self {
|
||||||
BlendMode::SourceOver | BlendMode::Clear => true,
|
BlendMode::SourceOver | BlendMode::Clear => true,
|
||||||
|
BlendMode::Lighten | BlendMode::Darken => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@ authors = ["Patrick Walton <pcwalton@mimiga.net>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
foreign-types = "0.5"
|
foreign-types = "0.3"
|
||||||
gl = "0.14"
|
gl = "0.14"
|
||||||
metal = "0.17"
|
metal = "0.17"
|
||||||
objc = "0.2"
|
objc = "0.2"
|
||||||
|
|
|
@ -965,6 +965,7 @@ impl BlendFactorExt for BlendFactor {
|
||||||
BlendFactor::One => gl::ONE,
|
BlendFactor::One => gl::ONE,
|
||||||
BlendFactor::SrcAlpha => gl::SRC_ALPHA,
|
BlendFactor::SrcAlpha => gl::SRC_ALPHA,
|
||||||
BlendFactor::OneMinusSrcAlpha => gl::ONE_MINUS_SRC_ALPHA,
|
BlendFactor::OneMinusSrcAlpha => gl::ONE_MINUS_SRC_ALPHA,
|
||||||
|
BlendFactor::DestColor => gl::DST_COLOR,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -226,6 +226,7 @@ pub enum BlendFactor {
|
||||||
One,
|
One,
|
||||||
SrcAlpha,
|
SrcAlpha,
|
||||||
OneMinusSrcAlpha,
|
OneMinusSrcAlpha,
|
||||||
|
DestColor,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||||
|
|
|
@ -1170,6 +1170,7 @@ impl BlendFactorExt for BlendFactor {
|
||||||
BlendFactor::One => MTLBlendFactor::One,
|
BlendFactor::One => MTLBlendFactor::One,
|
||||||
BlendFactor::SrcAlpha => MTLBlendFactor::SourceAlpha,
|
BlendFactor::SrcAlpha => MTLBlendFactor::SourceAlpha,
|
||||||
BlendFactor::OneMinusSrcAlpha => MTLBlendFactor::OneMinusSourceAlpha,
|
BlendFactor::OneMinusSrcAlpha => MTLBlendFactor::OneMinusSourceAlpha,
|
||||||
|
BlendFactor::DestColor => MTLBlendFactor::DestinationColor,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1151,6 +1151,24 @@ impl BlendModeExt for BlendMode {
|
||||||
..BlendState::default()
|
..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,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue