rusticl/mesa/resource: port to NonNull

Reviewed-by: Adam Jackson <ajax@redhat.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/32821>
This commit is contained in:
Karol Herbst
2024-06-03 16:45:52 +02:00
committed by Marge Bot
parent 0fe59874c0
commit 5f30bc62fe

View File

@@ -1,11 +1,14 @@
use mesa_rust_gen::*; use mesa_rust_gen::*;
use std::{mem, ptr}; use std::{
mem,
ptr::{self, NonNull},
};
#[derive(PartialEq, Eq, Hash)] #[derive(PartialEq, Eq, Hash)]
#[repr(transparent)] #[repr(transparent)]
pub struct PipeResource { pub struct PipeResource {
pipe: *mut pipe_resource, pipe: NonNull<pipe_resource>,
} }
const PIPE_RESOURCE_FLAG_RUSTICL_IS_USER: u32 = PIPE_RESOURCE_FLAG_FRONTEND_PRIV; const PIPE_RESOURCE_FLAG_RUSTICL_IS_USER: u32 = PIPE_RESOURCE_FLAG_FRONTEND_PRIV;
@@ -66,13 +69,11 @@ impl AppImgInfo {
impl PipeResource { impl PipeResource {
pub(super) fn new(res: *mut pipe_resource, is_user: bool) -> Option<Self> { pub(super) fn new(res: *mut pipe_resource, is_user: bool) -> Option<Self> {
if res.is_null() { let mut res = NonNull::new(res)?;
return None;
}
if is_user { if is_user {
unsafe { unsafe {
res.as_mut().unwrap().flags |= PIPE_RESOURCE_FLAG_RUSTICL_IS_USER; res.as_mut().flags |= PIPE_RESOURCE_FLAG_RUSTICL_IS_USER;
} }
} }
@@ -80,27 +81,28 @@ impl PipeResource {
} }
pub(super) fn pipe(&self) -> *mut pipe_resource { pub(super) fn pipe(&self) -> *mut pipe_resource {
self.pipe self.pipe.as_ptr()
} }
fn as_ref(&self) -> &pipe_resource { fn as_ref(&self) -> &pipe_resource {
unsafe { self.pipe.as_ref().unwrap() } // SAFETY: it contains a valid pointer
unsafe { self.pipe.as_ref() }
} }
pub fn width(&self) -> u32 { pub fn width(&self) -> u32 {
unsafe { self.pipe.as_ref().unwrap().width0 } self.as_ref().width0
} }
pub fn height(&self) -> u16 { pub fn height(&self) -> u16 {
unsafe { self.pipe.as_ref().unwrap().height0 } self.as_ref().height0
} }
pub fn depth(&self) -> u16 { pub fn depth(&self) -> u16 {
unsafe { self.pipe.as_ref().unwrap().depth0 } self.as_ref().depth0
} }
pub fn array_size(&self) -> u16 { pub fn array_size(&self) -> u16 {
unsafe { self.pipe.as_ref().unwrap().array_size } self.as_ref().array_size
} }
pub fn is_buffer(&self) -> bool { pub fn is_buffer(&self) -> bool {
@@ -186,7 +188,7 @@ impl PipeResource {
) -> pipe_sampler_view { ) -> pipe_sampler_view {
let mut res = pipe_sampler_view::default(); let mut res = pipe_sampler_view::default();
unsafe { unsafe {
u_sampler_view_default_template(&mut res, self.pipe, format); u_sampler_view_default_template(&mut res, self.pipe(), format);
} }
if let Some(app_img_info) = app_img_info { if let Some(app_img_info) = app_img_info {
@@ -207,6 +209,6 @@ impl PipeResource {
impl Drop for PipeResource { impl Drop for PipeResource {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { pipe_resource_reference(&mut self.pipe, ptr::null_mut()) } unsafe { pipe_resource_reference(&mut self.pipe.as_ptr(), ptr::null_mut()) }
} }
} }