1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use crate::prelude::*;
use crate::{
gpu, image, ColorSpace, Data, ISize, ImageInfo, Matrix, Paint, Picture, YUVAIndex,
YUVASizeInfo, YUVColorSpace,
};
use skia_bindings as sb;
use skia_bindings::SkImageGenerator;
use std::ffi::c_void;
pub type ImageGenerator = RefHandle<SkImageGenerator>;
impl NativeDrop for SkImageGenerator {
fn drop(&mut self) {
unsafe { sb::C_SkImageGenerator_delete(self) }
}
}
impl RefHandle<SkImageGenerator> {
pub fn unique_id(&self) -> u32 {
self.native().fUniqueID
}
pub fn encoded_data(&mut self) -> Option<Data> {
Data::from_ptr(unsafe { sb::C_SkImageGenerator_refEncodedData(self.native_mut()) })
}
pub fn info(&self) -> &ImageInfo {
ImageInfo::from_native_ref(&self.native().fInfo)
}
pub fn is_valid(&self, mut context: Option<&mut gpu::Context>) -> bool {
unsafe { sb::C_SkImageGenerator_isValid(self.native(), context.native_ptr_or_null_mut()) }
}
#[must_use]
pub fn get_pixels(&mut self, info: &ImageInfo, pixels: &mut [u8], row_bytes: usize) -> bool {
assert!(info.height() > 0);
assert!(
pixels.len()
>= ((info.height() - 1) as usize) * row_bytes
+ ((info.width() as usize) * info.bytes_per_pixel())
);
unsafe {
self.native_mut()
.getPixels(info.native(), pixels.as_mut_ptr() as _, row_bytes)
}
}
pub fn query_yuva8(
&self,
) -> Option<(
YUVASizeInfo,
[YUVAIndex; YUVAIndex::INDEX_COUNT],
YUVColorSpace,
)> {
let mut size_info = YUVASizeInfo::default();
let mut indices = [YUVAIndex::default(); YUVAIndex::INDEX_COUNT];
let mut cs = YUVColorSpace::Identity;
unsafe {
self.native().queryYUVA8(
size_info.native_mut(),
indices.native_mut().as_mut_ptr(),
cs.native_mut(),
)
}
.if_true_some((size_info, indices, cs))
}
pub fn get_yuva8_planes(
&mut self,
size_info: &YUVASizeInfo,
yuva_indices: &[YUVAIndex; YUVAIndex::INDEX_COUNT],
planes: &mut [&mut [u8]],
) -> bool {
for index in yuva_indices {
if index.is_valid() {
let index = index.index as usize;
let height = size_info.sizes[index].height;
let width_bytes = size_info.width_bytes[index];
let plane_size = width_bytes * height as usize;
if planes[index].len() < plane_size {
return false;
}
}
}
let mut planes: Vec<*mut c_void> = planes
.iter_mut()
.map(|p| p.as_mut_ptr() as *mut c_void)
.collect();
unsafe {
self.native_mut().getYUVA8Planes(
size_info.native(),
yuva_indices.native().as_ptr(),
planes.as_mut_ptr(),
)
}
}
pub fn from_encoded(encoded: Data) -> Option<Self> {
Self::from_ptr(unsafe { sb::C_SkImageGenerator_MakeFromEncoded(encoded.into_ptr()) })
}
pub fn from_picture(
size: ISize,
picture: Picture,
matrix: Option<&Matrix>,
paint: Option<&Paint>,
bit_depth: image::BitDepth,
color_space: impl Into<Option<ColorSpace>>,
) -> Option<Self> {
Self::from_ptr(unsafe {
sb::C_SkImageGenerator_MakeFromPicture(
size.native(),
picture.into_ptr(),
matrix.native_ptr_or_null(),
paint.native_ptr_or_null(),
bit_depth.into_native(),
color_space.into().into_ptr_or_null(),
)
})
}
}