Add reformat to the CI (#21)

This commit is contained in:
John Nunley 2022-12-22 12:35:18 -08:00 committed by GitHub
parent df091d59dd
commit 5674886dfa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 112 additions and 78 deletions

View file

@ -1,12 +1,14 @@
use crate::SwBufError;
use raw_window_handle::AppKitWindowHandle;
use core_graphics::base::{kCGBitmapByteOrder32Little, kCGImageAlphaNoneSkipFirst, kCGRenderingIntentDefault};
use core_graphics::base::{
kCGBitmapByteOrder32Little, kCGImageAlphaNoneSkipFirst, kCGRenderingIntentDefault,
};
use core_graphics::color_space::CGColorSpace;
use core_graphics::data_provider::CGDataProvider;
use core_graphics::image::CGImage;
use raw_window_handle::AppKitWindowHandle;
use cocoa::appkit::{NSView, NSViewHeightSizable, NSViewWidthSizable, NSWindow};
use cocoa::base::{id, nil};
use cocoa::appkit::{NSView, NSViewWidthSizable, NSViewHeightSizable, NSWindow};
use cocoa::quartzcore::{CALayer, ContentsGravity};
use foreign_types::ForeignType;
@ -30,14 +32,13 @@ impl CGImpl {
view.addSubview_(subview); // retains subview (+1) = 2
let _: () = msg_send![subview, release]; // releases subview (-1) = 1
Ok(Self{layer})
Ok(Self { layer })
}
pub(crate) unsafe fn set_buffer(&mut self, buffer: &[u32], width: u16, height: u16) {
let color_space = CGColorSpace::create_device_rgb();
let data = std::slice::from_raw_parts(
buffer.as_ptr() as *const u8,
buffer.len() * 4).to_vec();
let data =
std::slice::from_raw_parts(buffer.as_ptr() as *const u8, buffer.len() * 4).to_vec();
let data_provider = CGDataProvider::from_buffer(Arc::new(data));
let image = CGImage::new(
width as usize,

View file

@ -1,5 +1,5 @@
use std::error::Error;
use raw_window_handle::{RawDisplayHandle, RawWindowHandle};
use std::error::Error;
use thiserror::Error;
#[derive(Error, Debug)]
@ -12,7 +12,7 @@ pub enum SwBufError {
human_readable_window_platform_name: &'static str,
human_readable_display_platform_name: &'static str,
window_handle: RawWindowHandle,
display_handle: RawDisplayHandle
display_handle: RawDisplayHandle,
},
#[error("The provided window handle is null.")]
@ -22,13 +22,19 @@ pub enum SwBufError {
IncompleteDisplayHandle,
#[error("Platform error")]
PlatformError(Option<String>, Option<Box<dyn Error>>)
PlatformError(Option<String>, Option<Box<dyn Error>>),
}
#[allow(unused)] // This isn't used on all platforms
pub(crate) fn unwrap<T, E: std::error::Error + 'static>(res: Result<T, E>, str: &str) -> Result<T, SwBufError>{
match res{
pub(crate) fn unwrap<T, E: std::error::Error + 'static>(
res: Result<T, E>,
str: &str,
) -> Result<T, SwBufError> {
match res {
Ok(t) => Ok(t),
Err(e) => Err(SwBufError::PlatformError(Some(str.into()), Some(Box::new(e))))
Err(e) => Err(SwBufError::PlatformError(
Some(str.into()),
Some(Box::new(e)),
)),
}
}

View file

@ -5,33 +5,35 @@
extern crate objc;
extern crate core;
#[cfg(target_os = "windows")]
mod win32;
#[cfg(target_os = "macos")]
mod cg;
#[cfg(all(feature = "x11", any(target_os = "linux", target_os = "freebsd")))]
mod x11;
#[cfg(target_os = "redox")]
mod orbital;
#[cfg(all(feature = "wayland", any(target_os = "linux", target_os = "freebsd")))]
mod wayland;
#[cfg(target_arch = "wasm32")]
mod web;
#[cfg(target_os = "redox")]
mod orbital;
#[cfg(target_os = "windows")]
mod win32;
#[cfg(all(feature = "x11", any(target_os = "linux", target_os = "freebsd")))]
mod x11;
mod error;
pub use error::SwBufError;
use raw_window_handle::{HasRawDisplayHandle, HasRawWindowHandle, RawDisplayHandle, RawWindowHandle};
use raw_window_handle::{
HasRawDisplayHandle, HasRawWindowHandle, RawDisplayHandle, RawWindowHandle,
};
/// An instance of this struct contains the platform-specific data that must be managed in order to
/// write to a window on that platform.
pub struct GraphicsContext {
/// The inner static dispatch object.
///
///
/// This is boxed so that `GraphicsContext` is the same size on every platform, which should
/// hopefully prevent surprises.
graphics_context_impl: Box<Dispatch>
graphics_context_impl: Box<Dispatch>,
}
/// A macro for creating the enum used to statically dispatch to the platform-specific implementation.
@ -84,7 +86,10 @@ impl GraphicsContext {
///
/// - Ensure that the provided objects are valid to draw a 2D buffer to, and are valid for the
/// lifetime of the GraphicsContext
pub unsafe fn new<W: HasRawWindowHandle, D: HasRawDisplayHandle>(window: &W, display: &D) -> Result<Self, SwBufError> {
pub unsafe fn new<W: HasRawWindowHandle, D: HasRawDisplayHandle>(
window: &W,
display: &D,
) -> Result<Self, SwBufError> {
Self::from_raw(window.raw_window_handle(), display.raw_display_handle())
}
@ -94,30 +99,54 @@ impl GraphicsContext {
///
/// - Ensure that the provided handles are valid to draw a 2D buffer to, and are valid for the
/// lifetime of the GraphicsContext
pub unsafe fn from_raw(raw_window_handle: RawWindowHandle, raw_display_handle: RawDisplayHandle) -> Result<Self, SwBufError> {
pub unsafe fn from_raw(
raw_window_handle: RawWindowHandle,
raw_display_handle: RawDisplayHandle,
) -> Result<Self, SwBufError> {
let imple: Dispatch = match (raw_window_handle, raw_display_handle) {
#[cfg(all(feature = "x11", any(target_os = "linux", target_os = "freebsd")))]
(RawWindowHandle::Xlib(xlib_window_handle), RawDisplayHandle::Xlib(xlib_display_handle)) => Dispatch::X11(x11::X11Impl::new(xlib_window_handle, xlib_display_handle)?),
(
RawWindowHandle::Xlib(xlib_window_handle),
RawDisplayHandle::Xlib(xlib_display_handle),
) => Dispatch::X11(x11::X11Impl::new(xlib_window_handle, xlib_display_handle)?),
#[cfg(all(feature = "wayland", any(target_os = "linux", target_os = "freebsd")))]
(RawWindowHandle::Wayland(wayland_window_handle), RawDisplayHandle::Wayland(wayland_display_handle)) => Dispatch::Wayland(wayland::WaylandImpl::new(wayland_window_handle, wayland_display_handle)?),
(
RawWindowHandle::Wayland(wayland_window_handle),
RawDisplayHandle::Wayland(wayland_display_handle),
) => Dispatch::Wayland(wayland::WaylandImpl::new(
wayland_window_handle,
wayland_display_handle,
)?),
#[cfg(target_os = "windows")]
(RawWindowHandle::Win32(win32_handle), _) => Dispatch::Win32(win32::Win32Impl::new(&win32_handle)?),
(RawWindowHandle::Win32(win32_handle), _) => {
Dispatch::Win32(win32::Win32Impl::new(&win32_handle)?)
}
#[cfg(target_os = "macos")]
(RawWindowHandle::AppKit(appkit_handle), _) => Dispatch::CG(cg::CGImpl::new(appkit_handle)?),
(RawWindowHandle::AppKit(appkit_handle), _) => {
Dispatch::CG(cg::CGImpl::new(appkit_handle)?)
}
#[cfg(target_arch = "wasm32")]
(RawWindowHandle::Web(web_handle), _) => Dispatch::Web(web::WebImpl::new(web_handle)?),
#[cfg(target_os = "redox")]
(RawWindowHandle::Orbital(orbital_handle), _) => Dispatch::Orbital(orbital::OrbitalImpl::new(orbital_handle)?),
(unimplemented_window_handle, unimplemented_display_handle) => return Err(SwBufError::UnsupportedPlatform {
human_readable_window_platform_name: window_handle_type_name(&unimplemented_window_handle),
human_readable_display_platform_name: display_handle_type_name(&unimplemented_display_handle),
window_handle: unimplemented_window_handle,
display_handle: unimplemented_display_handle
}),
(RawWindowHandle::Orbital(orbital_handle), _) => {
Dispatch::Orbital(orbital::OrbitalImpl::new(orbital_handle)?)
}
(unimplemented_window_handle, unimplemented_display_handle) => {
return Err(SwBufError::UnsupportedPlatform {
human_readable_window_platform_name: window_handle_type_name(
&unimplemented_window_handle,
),
human_readable_display_platform_name: display_handle_type_name(
&unimplemented_display_handle,
),
window_handle: unimplemented_window_handle,
display_handle: unimplemented_display_handle,
})
}
};
Ok(Self {
graphics_context_impl: Box::new(imple)
graphics_context_impl: Box::new(imple),
})
}

View file

@ -1,9 +1,5 @@
use raw_window_handle::OrbitalWindowHandle;
use std::{
cmp,
slice,
str,
};
use std::{cmp, slice, str};
use crate::SwBufError;
@ -19,12 +15,15 @@ impl OrbitalMap {
let size = pages * syscall::PAGE_SIZE;
// Map window buffer
let address = syscall::fmap(fd, &syscall::Map {
offset: 0,
size,
flags: syscall::PROT_READ | syscall::PROT_WRITE,
address: 0,
})?;
let address = syscall::fmap(
fd,
&syscall::Map {
offset: 0,
size,
flags: syscall::PROT_READ | syscall::PROT_WRITE,
address: 0,
},
)?;
Ok(Self { address, size })
}
@ -34,8 +33,7 @@ impl Drop for OrbitalMap {
fn drop(&mut self) {
unsafe {
// Unmap window buffer on drop
syscall::funmap(self.address, self.size)
.expect("failed to unmap orbital window");
syscall::funmap(self.address, self.size).expect("failed to unmap orbital window");
}
}
}
@ -71,15 +69,13 @@ impl OrbitalImpl {
{
// Map window buffer
let window_map = OrbitalMap::new(
window_fd,
window_width * window_height * 4
).expect("failed to map orbital window");
let window_map = OrbitalMap::new(window_fd, window_width * window_height * 4)
.expect("failed to map orbital window");
// Window buffer is u32 color data in 0xAABBGGRR format
let window_data = slice::from_raw_parts_mut(
window_map.address as *mut u32,
window_width * window_height
window_width * window_height,
);
// Copy each line, cropping to fit
@ -90,9 +86,8 @@ impl OrbitalImpl {
for y in 0..min_height {
let offset_buffer = y * width;
let offset_data = y * window_width;
window_data[offset_data..offset_data + min_width].copy_from_slice(
&buffer[offset_buffer..offset_buffer + min_width]
);
window_data[offset_data..offset_data + min_width]
.copy_from_slice(&buffer[offset_buffer..offset_buffer + min_width]);
}
// Window buffer map is dropped here

View file

@ -32,10 +32,7 @@ impl WebImpl {
// `querySelector` only throws an error if the selector is invalid.
.unwrap()
.ok_or_else(|| {
SwBufError::PlatformError(
Some("No canvas found with the given id".into()),
None,
)
SwBufError::PlatformError(Some("No canvas found with the given id".into()), None)
})?
// We already made sure this was a canvas in `querySelector`.
.unchecked_into();