video buffer using unsafe cell across ffi boundary

This commit is contained in:
Corwin 2023-08-09 21:06:54 +01:00
parent d9a0ab1a9f
commit 5eb240068a
No known key found for this signature in database

View file

@ -1,16 +1,20 @@
mod log; mod log;
mod vfile; mod vfile;
use std::{ptr::NonNull, sync::atomic::{AtomicBool, Ordering}}; use std::{
cell::UnsafeCell,
ptr::NonNull,
sync::atomic::{AtomicBool, Ordering},
};
pub use log::{Logger, LogLevel}; pub use log::{LogLevel, Logger};
pub use vfile::{file::FileBacked, memory::MemoryBacked, shared::Shared, MapFlag, VFile}; pub use vfile::{file::FileBacked, memory::MemoryBacked, shared::Shared, MapFlag, VFile};
use vfile::VFileAlloc; use vfile::VFileAlloc;
pub struct MCore { pub struct MCore {
core: NonNull<mgba_sys::mCore>, core: NonNull<mgba_sys::mCore>,
video_buffer: Box<[u32]>, video_buffer: UnsafeCell<Box<[u32]>>,
} }
impl Drop for MCore { impl Drop for MCore {
@ -51,17 +55,18 @@ impl MCore {
unsafe { call_on_core!(core=>desiredVideoDimensions(&mut width, &mut height)) }; unsafe { call_on_core!(core=>desiredVideoDimensions(&mut width, &mut height)) };
let mut video_buffer = vec![ let mut video_buffer = UnsafeCell::new(
vec![
0; 0;
(width * height * mgba_sys::BYTES_PER_PIXEL) as usize (width * height * mgba_sys::BYTES_PER_PIXEL) as usize / std::mem::size_of::<u32>()
/ std::mem::size_of::<u32>()
] ]
.into_boxed_slice(); .into_boxed_slice(),
);
unsafe { unsafe {
call_on_core!( call_on_core!(
core=>setVideoBuffer( core=>setVideoBuffer(
video_buffer.as_mut_ptr(), video_buffer.get_mut().as_mut_ptr(),
width as usize width as usize
) )
) )
@ -123,7 +128,9 @@ impl MCore {
} }
pub fn video_buffer(&mut self) -> &[u32] { pub fn video_buffer(&mut self) -> &[u32] {
self.video_buffer.as_ref() // Safety: For the duration of this borrow, mgba can't be called into,
// so this reference can be taken.
unsafe { &*self.video_buffer.get() }
} }
pub fn current_cycle(&mut self) -> u64 { pub fn current_cycle(&mut self) -> u64 {