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