skia_safe/core/
blender.rs

1use crate::{prelude::*, BlendMode, NativeFlattenable};
2use skia_bindings::{self as sb, SkBlender, SkFlattenable, SkRefCntBase};
3use std::fmt;
4
5/// [`Blender`] represents a custom blend function in the Skia pipeline.  A blender combines a source
6/// color (the result of our paint) and destination color (from the canvas) into a final color.
7pub type Blender = RCHandle<SkBlender>;
8unsafe_send_sync!(Blender);
9require_base_type!(SkBlender, SkFlattenable);
10
11impl NativeRefCountedBase for SkBlender {
12    type Base = SkRefCntBase;
13}
14
15impl NativeBase<SkFlattenable> for SkBlender {}
16
17impl Blender {
18    /// Create a blender that implements the specified [`BlendMode`].
19    pub fn mode(mode: BlendMode) -> Blender {
20        Blender::from_ptr(unsafe { sb::C_SkBlender_Mode(mode) }).unwrap()
21    }
22}
23
24impl fmt::Debug for Blender {
25    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26        f.debug_struct("Blender").finish()
27    }
28}
29
30impl NativeFlattenable for SkBlender {
31    fn native_flattenable(&self) -> &SkFlattenable {
32        unsafe { &*(self as *const SkBlender as *const SkFlattenable) }
33    }
34
35    fn native_deserialize(data: &[u8]) -> *mut Self {
36        unsafe { sb::C_SkBlender_Deserialize(data.as_ptr() as _, data.len()) }
37    }
38}
39
40impl From<BlendMode> for Blender {
41    fn from(mode: BlendMode) -> Self {
42        Blender::mode(mode)
43    }
44}