1#[cfg(feature = "gpu")]
2use crate::gpu;
3use crate::{prelude::*, yuva_pixmap_info, Data, ImageInfo, YUVAPixmapInfo};
4use skia_bindings::{self as sb, SkImageGenerator};
5use std::fmt;
6
7pub type ImageGenerator = RefHandle<SkImageGenerator>;
8unsafe_send_sync!(ImageGenerator);
9
10impl NativeDrop for SkImageGenerator {
11 fn drop(&mut self) {
12 unsafe { sb::C_SkImageGenerator_delete(self) }
13 }
14}
15
16impl fmt::Debug for ImageGenerator {
17 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18 f&mut DebugStruct<'_, '_>.debug_struct("ImageGenerator")
19 .field("unique_id", &self.unique_id())
20 .field(name:"info", &self.info())
21 .finish()
22 }
23}
24
25impl ImageGenerator {
26 pub fn unique_id(&self) -> u32 {
27 self.native().fUniqueID
28 }
29
30 pub fn encoded_data(&mut self) -> Option<Data> {
31 Data::from_ptr(unsafe { sb::C_SkImageGenerator_refEncodedData(self.native_mut()) })
32 }
33
34 pub fn info(&self) -> &ImageInfo {
35 ImageInfo::from_native_ref(&self.native().fInfo)
36 }
37
38 #[cfg(feature = "gpu")]
39 pub fn is_valid(&self, mut context: Option<&mut gpu::RecordingContext>) -> bool {
40 unsafe { sb::C_SkImageGenerator_isValid(self.native(), context.native_ptr_or_null_mut()) }
41 }
42
43 pub fn is_protected(self) -> bool {
44 unsafe { sb::C_SkImageGenerator_isProtected(self.native()) }
45 }
46
47 #[must_use]
48 pub fn get_pixels(&mut self, info: &ImageInfo, pixels: &mut [u8], row_bytes: usize) -> bool {
49 assert!(info.valid_pixels(row_bytes, pixels));
50 unsafe {
51 self.native_mut()
52 .getPixels(info.native(), pixels.as_mut_ptr() as _, row_bytes)
53 }
54 }
55
56 // TODO: m86: get_pixels(&Pixmap)
57
58 pub fn query_yuva_info(
59 &self,
60 supported_data_types: &yuva_pixmap_info::SupportedDataTypes,
61 ) -> Option<YUVAPixmapInfo> {
62 YUVAPixmapInfo::new_if_valid(|info| unsafe {
63 self.native()
64 .queryYUVAInfo(supported_data_types.native(), info)
65 })
66 }
67
68 // TODO: getYUVAPlanes()
69
70 pub fn is_texture_generator(&self) -> bool {
71 unsafe { sb::C_SkImageGenerator_isTextureGenerator(self.native()) }
72 }
73
74 #[deprecated(
75 since = "0.64.0",
76 note = "Removed, will return `None`. Use Image::deferred_from_encoded_data() or Codec::from_data()"
77 )]
78 pub fn from_encoded(_encoded: impl Into<Data>) -> Option<Self> {
79 debug_assert!(false, "Removed, will return `None` in release builds");
80 None
81 }
82}
83