Skip to content

Commit

Permalink
fix: ctx.filter should store in state and be able to save/restore
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn committed Jun 23, 2022
1 parent 462fce5 commit b85ee7b
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 14 deletions.
10 changes: 10 additions & 0 deletions __test__/filter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ test('filter-combine-contrast-brightness', async (t) => {
await snapshotImage(t)
})

test('filter-save-restore', async (t) => {
const { ctx } = t.context
ctx.filter = 'none'
ctx.save()
ctx.filter = 'invert(100%)'
ctx.restore()
ctx.drawImage(await createImage('filter-invert.jpeg'), 0, 0)
await snapshotImage(t)
})

async function createImage(name: string) {
const i = new Image()
i.src = await fs.readFile(join(__dirname, 'fixtures', name))
Expand Down
Binary file added __test__/snapshots/filter-save-restore.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions skia-c/skia_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1331,6 +1331,12 @@ extern "C"
}
}

void skiac_image_filter_ref(skiac_image_filter *c_image_filter)
{
auto image_filter = IMAGE_FILTER_CAST;
image_filter->ref();
}

void skiac_image_filter_destroy(skiac_image_filter *c_image_filter)
{
auto image_filter = IMAGE_FILTER_CAST;
Expand Down
22 changes: 9 additions & 13 deletions src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ pub struct Context {
pub height: u32,
pub color_space: ColorSpace,
pub stream: Option<SkWMemoryStream>,
pub filter: Option<ImageFilter>,
filters_string: String,
}

impl Context {
Expand Down Expand Up @@ -267,8 +265,6 @@ impl Context {
height,
color_space,
stream: Some(stream),
filter: None,
filters_string: "none".to_owned(),
})
}

Expand All @@ -291,8 +287,6 @@ impl Context {
height,
color_space,
stream: None,
filter: None,
filters_string: "none".to_owned(),
})
}

Expand Down Expand Up @@ -571,21 +565,21 @@ impl Context {
.ok_or_else(|| SkError::Generic("Make line dash path effect failed".to_string()))?;
paint.set_path_effect(&path_effect);
}
if let Some(f) = &self.filter {
if let Some(f) = &self.state.filter {
paint.set_image_filter(f);
}
Ok(paint)
}

pub fn set_filter(&mut self, filter_str: &str) -> result::Result<(), SkError> {
if filter_str.trim() == "none" {
self.filters_string = "none".to_owned();
self.filter = None;
self.state.filters_string = "none".to_owned();
self.state.filter = None;
} else {
let (_, filters) =
css_filter(filter_str).map_err(|e| SkError::StringToFillRuleError(format!("{}", e)))?;
self.filter = css_filters_to_image_filter(filters);
self.filters_string = filter_str.to_owned();
self.state.filter = css_filters_to_image_filter(filters);
self.state.filters_string = filter_str.to_owned();
}
Ok(())
}
Expand Down Expand Up @@ -623,7 +617,7 @@ impl Context {
.ok_or_else(|| SkError::Generic("Make line dash path effect failed".to_string()))?;
paint.set_path_effect(&path_effect);
}
if let Some(f) = &self.filter {
if let Some(f) = &self.state.filter {
paint.set_image_filter(f);
}
Ok(paint)
Expand Down Expand Up @@ -2005,7 +1999,9 @@ fn set_filter(ctx: CallContext) -> Result<JsUndefined> {
fn get_filter(ctx: CallContext) -> Result<JsString> {
let this = ctx.this_unchecked::<JsObject>();
let context_2d = ctx.env.unwrap::<Context>(&this)?;
ctx.env.create_string(context_2d.filters_string.as_str())
ctx
.env
.create_string(context_2d.state.filters_string.as_str())
}

#[js_function]
Expand Down
11 changes: 11 additions & 0 deletions src/sk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,8 @@ mod ffi {
c_image_filter: *mut skiac_image_filter,
) -> *mut skiac_image_filter;

pub fn skiac_image_filter_ref(image_filter: *mut skiac_image_filter);

pub fn skiac_image_filter_destroy(image_filter: *mut skiac_image_filter);

pub fn skiac_sk_data_destroy(c_data: *mut skiac_data);
Expand Down Expand Up @@ -3143,6 +3145,15 @@ impl Drop for MaskFilter {
#[derive(Debug)]
pub struct ImageFilter(pub(crate) *mut ffi::skiac_image_filter);

impl Clone for ImageFilter {
fn clone(&self) -> Self {
unsafe {
ffi::skiac_image_filter_ref(self.0);
};
Self(self.0)
}
}

impl ImageFilter {
pub fn make_drop_shadow_only(
dx: f32,
Expand Down
6 changes: 5 additions & 1 deletion src/state.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use cssparser::RGBA;

use crate::sk::Matrix;
use crate::sk::{ImageFilter, Matrix};

use super::{
font::Font,
Expand Down Expand Up @@ -30,6 +30,8 @@ pub struct Context2dRenderingState {
pub text_baseline: TextBaseline,
pub text_direction: TextDirection,
pub transform: Matrix,
pub filter: Option<ImageFilter>,
pub filters_string: String,
}

impl Default for Context2dRenderingState {
Expand Down Expand Up @@ -57,6 +59,8 @@ impl Default for Context2dRenderingState {
text_baseline: TextBaseline::default(),
text_direction: TextDirection::default(),
transform: Matrix::identity(),
filter: None,
filters_string: "none".to_owned(),
}
}
}

0 comments on commit b85ee7b

Please sign in to comment.