1use crate::{prelude::*, ISize};
2use skia_bindings::{self as sb, SkPixelRef, SkRefCntBase};
3use std::{fmt, os::raw::c_void};
4
5pub type PixelRef = RCHandle<SkPixelRef>;
6unsafe_send_sync!(PixelRef);
7require_type_equality!(sb::SkPixelRef_INHERITED, sb::SkRefCnt);
8
9impl NativeRefCountedBase for SkPixelRef {
10 type Base = SkRefCntBase;
11}
12
13impl fmt::Debug for PixelRef {
14 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
15 f&mut DebugStruct<'_, '_>.debug_struct("PixelRef")
16 .field("dimensions", &self.dimensions())
17 .field("row_bytes", &self.row_bytes())
18 .field("generation_id", &self.generation_id())
19 .field(name:"is_immutable", &self.is_immutable())
20 .finish()
21 }
22}
23
24impl PixelRef {
25 // TODO: wrap constructor with pixels borrowed.
26
27 pub fn dimensions(&self) -> ISize {
28 ISize::new(self.width(), self.height())
29 }
30
31 pub fn width(&self) -> i32 {
32 unsafe { sb::C_SkPixelRef_width(self.native()) }
33 }
34
35 pub fn height(&self) -> i32 {
36 unsafe { sb::C_SkPixelRef_height(self.native()) }
37 }
38
39 #[allow(clippy::missing_safety_doc)]
40 pub unsafe fn pixels(&self) -> *mut c_void {
41 sb::C_SkPixelRef_pixels(self.native())
42 }
43
44 pub fn row_bytes(&self) -> usize {
45 unsafe { sb::C_SkPixelRef_rowBytes(self.native()) }
46 }
47
48 pub fn generation_id(&self) -> u32 {
49 unsafe { self.native().getGenerationID() }
50 }
51
52 pub fn notify_pixels_changed(&mut self) {
53 unsafe { self.native_mut().notifyPixelsChanged() }
54 }
55
56 pub fn is_immutable(&self) -> bool {
57 unsafe { sb::C_SkPixelRef_isImmutable(self.native()) }
58 }
59
60 pub fn set_immutable(&mut self) {
61 unsafe { self.native_mut().setImmutable() }
62 }
63
64 // TODO addGenIDChangeListener()
65
66 pub fn notify_added_to_cache(&mut self) {
67 unsafe { sb::C_SkPixelRef_notifyAddedToCache(self.native_mut()) }
68 }
69}
70