From 92295f2ea47475bfdca4486ca05b32e9f32e37f2 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Thu, 6 Mar 2025 19:41:05 +0100 Subject: [PATCH] graphics/graphics-ipc: Use u32 fields in Damage The fields should never be negative and this saves a couple of casts. --- graphics/console-draw/src/lib.rs | 2 +- graphics/driver-graphics/src/lib.rs | 4 ++-- graphics/graphics-ipc/src/v1.rs | 12 +++++++----- graphics/vesad/src/scheme.rs | 8 ++++---- graphics/virtio-gpud/src/scheme.rs | 12 +++++------- 5 files changed, 19 insertions(+), 19 deletions(-) diff --git a/graphics/console-draw/src/lib.rs b/graphics/console-draw/src/lib.rs index 57820c3c09..59aea00469 100644 --- a/graphics/console-draw/src/lib.rs +++ b/graphics/console-draw/src/lib.rs @@ -221,7 +221,7 @@ impl TextScreen { } else { damage.push(Damage { x: 0, - y: i32::try_from(change).unwrap() * 16, + y: u32::try_from(change).unwrap() * 16, width, height: 16, }); diff --git a/graphics/driver-graphics/src/lib.rs b/graphics/driver-graphics/src/lib.rs index 0b276ff23f..035ceb6a91 100644 --- a/graphics/driver-graphics/src/lib.rs +++ b/graphics/driver-graphics/src/lib.rs @@ -142,8 +142,8 @@ impl GraphicsScheme { &[Damage { x: 0, y: 0, - width: resource.width() as i32, - height: resource.height() as i32, + width: resource.width(), + height: resource.height(), }], ); } diff --git a/graphics/graphics-ipc/src/v1.rs b/graphics/graphics-ipc/src/v1.rs index 7c165f5b00..39226f2b31 100644 --- a/graphics/graphics-ipc/src/v1.rs +++ b/graphics/graphics-ipc/src/v1.rs @@ -111,18 +111,20 @@ impl Drop for DisplayMap { } // Keep synced with orbital's SyncRect +// Technically orbital uses i32 rather than u32, but values larger than i32::MAX +// would be a bug anyway. #[derive(Debug, Copy, Clone)] #[repr(packed)] pub struct Damage { - pub x: i32, - pub y: i32, - pub width: i32, - pub height: i32, + pub x: u32, + pub y: u32, + pub width: u32, + pub height: u32, } impl Damage { #[must_use] - pub fn clip(mut self, width: i32, height: i32) -> Self { + pub fn clip(mut self, width: u32, height: u32) -> Self { // Clip damage self.x = cmp::min(self.x, width); if self.x + self.width > width { diff --git a/graphics/vesad/src/scheme.rs b/graphics/vesad/src/scheme.rs index 229c3cfa8a..938b8a6678 100644 --- a/graphics/vesad/src/scheme.rs +++ b/graphics/vesad/src/scheme.rs @@ -91,10 +91,10 @@ impl GraphicScreen { self.height.try_into().unwrap(), ); - let start_x: usize = sync_rect.x.try_into().unwrap_or(0); - let start_y: usize = sync_rect.y.try_into().unwrap_or(0); - let w: usize = sync_rect.width.try_into().unwrap_or(0); - let h: usize = sync_rect.height.try_into().unwrap_or(0); + let start_x: usize = sync_rect.x.try_into().unwrap(); + let start_y: usize = sync_rect.y.try_into().unwrap(); + let w: usize = sync_rect.width.try_into().unwrap(); + let h: usize = sync_rect.height.try_into().unwrap(); let offscreen_ptr = self.ptr.as_ptr() as *mut u32; let onscreen_ptr = framebuffer.onscreen as *mut u32; // FIXME use as_mut_ptr once stable diff --git a/graphics/virtio-gpud/src/scheme.rs b/graphics/virtio-gpud/src/scheme.rs index 95714e9a77..6bd41c91a7 100644 --- a/graphics/virtio-gpud/src/scheme.rs +++ b/graphics/virtio-gpud/src/scheme.rs @@ -15,10 +15,10 @@ use crate::*; impl Into for Damage { fn into(self) -> GpuRect { GpuRect { - x: self.x as u32, - y: self.y as u32, - width: self.width as u32, - height: self.height as u32, + x: self.x, + y: self.y, + width: self.width, + height: self.height, } } } @@ -202,9 +202,7 @@ impl GraphicsAdapter for VirtGpuAdapter<'_> { for damage in damage { let flush = ResourceFlush::new( resource.id, - damage - .clip(resource.width as i32, resource.height as i32) - .into(), + damage.clip(resource.width, resource.height).into(), ); let header = self.send_request(Dma::new(flush).unwrap()).await.unwrap(); assert_eq!(header.ty, CommandTy::RespOkNodata);