Skip to content

Commit

Permalink
Merge pull request #58 from parasyte/fix/debug_bitmap_ub
Browse files Browse the repository at this point in the history
  • Loading branch information
boozook authored Sep 14, 2023
2 parents 05067d3 + 2794c85 commit 6e64867
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ pub struct BitmapData {
#[derive(Debug)]
pub struct BitmapInner {
pub(crate) raw_bitmap: *mut crankstart_sys::LCDBitmap,
owned: bool,
}

impl BitmapInner {
Expand Down Expand Up @@ -133,7 +134,10 @@ impl BitmapInner {
// No documentation on this anywhere, but null works in testing.
ptr::null_mut(), // allocedSize
)?;
Ok(Self { raw_bitmap })
Ok(Self {
raw_bitmap,
owned: true,
})
}

pub fn tile(
Expand Down Expand Up @@ -165,7 +169,10 @@ impl BitmapInner {
pub fn duplicate(&self) -> Result<Self, Error> {
let raw_bitmap = pd_func_caller!((*Graphics::get_ptr()).copyBitmap, self.raw_bitmap)?;

Ok(Self { raw_bitmap })
Ok(Self {
raw_bitmap,
owned: self.owned,
})
}

pub fn transform(&self, rotation: f32, scale: Vector2D<f32>) -> Result<Self, Error> {
Expand Down Expand Up @@ -244,7 +251,9 @@ impl BitmapInner {

impl Drop for BitmapInner {
fn drop(&mut self) {
pd_func_caller_log!((*Graphics::get_ptr()).freeBitmap, self.raw_bitmap);
if self.owned {
pd_func_caller_log!((*Graphics::get_ptr()).freeBitmap, self.raw_bitmap);
}
}
}

Expand All @@ -256,9 +265,9 @@ pub struct Bitmap {
}

impl Bitmap {
fn new(raw_bitmap: *mut crankstart_sys::LCDBitmap) -> Self {
fn new(raw_bitmap: *mut crankstart_sys::LCDBitmap, owned: bool) -> Self {
Bitmap {
inner: Rc::new(RefCell::new(BitmapInner { raw_bitmap })),
inner: Rc::new(RefCell::new(BitmapInner { raw_bitmap, owned })),
}
}

Expand Down Expand Up @@ -392,7 +401,7 @@ impl BitmapTableInner {
index,
self.raw_bitmap_table
);
let bitmap = Bitmap::new(raw_bitmap);
let bitmap = Bitmap::new(raw_bitmap, true);
self.bitmaps.insert(index, bitmap.clone());
Ok(bitmap)
}
Expand Down Expand Up @@ -522,7 +531,7 @@ impl Graphics {
raw_bitmap != ptr::null_mut(),
"Null pointer returned from getDebugImage"
);
Ok(Bitmap::new(raw_bitmap))
Ok(Bitmap::new(raw_bitmap, false))
}

pub fn get_framebuffer_bitmap(&self) -> Result<Bitmap, Error> {
Expand All @@ -531,7 +540,7 @@ impl Graphics {
raw_bitmap != ptr::null_mut(),
"Null pointer returned from getFrameBufferBitmap"
);
Ok(Bitmap::new(raw_bitmap))
Ok(Bitmap::new(raw_bitmap, true))
}

pub fn set_background_color(&self, color: LCDSolidColor) -> Result<(), Error> {
Expand Down Expand Up @@ -566,7 +575,7 @@ impl Graphics {
raw_bitmap != ptr::null_mut(),
"Null pointer returned from new_bitmap"
);
Ok(Bitmap::new(raw_bitmap))
Ok(Bitmap::new(raw_bitmap, true))
}

pub fn load_bitmap(&self, path: &str) -> Result<Bitmap, Error> {
Expand All @@ -583,7 +592,7 @@ impl Graphics {
))
}
} else {
Ok(Bitmap::new(raw_bitmap))
Ok(Bitmap::new(raw_bitmap, true))
}
}

Expand Down

0 comments on commit 6e64867

Please sign in to comment.