Skip to content

Commit

Permalink
Makes a dedicated error type for screen (#1055)
Browse files Browse the repository at this point in the history
  • Loading branch information
ultimaweapon authored Oct 20, 2024
1 parent 0dfe035 commit db87a86
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 22 deletions.
1 change: 1 addition & 0 deletions gui/src/vmm/hv/windows/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
use self::cpu::WhpCpu;
use self::partition::Partition;
use super::{CpuFeats, Hypervisor};
Expand Down
1 change: 1 addition & 0 deletions gui/src/vmm/hv/windows/partition.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
use super::cpu::WhpCpu;
use std::ffi::c_void;
use std::mem::size_of;
Expand Down
1 change: 1 addition & 0 deletions gui/src/vmm/kernel/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
use std::fs::File;
use std::io::{Read, Seek, SeekFrom, Take};
use std::path::Path;
Expand Down
8 changes: 0 additions & 8 deletions gui/src/vmm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -697,10 +697,6 @@ pub enum DebugResult {
/// Represents an error when [`vmm_new()`] fails.
#[derive(Debug, Error)]
enum VmmError {
#[cfg(not(target_os = "macos"))]
#[error("couldn't create Vulkan device")]
CreateVulkanDeviceFailed(#[source] ash::vk::Result),

#[cfg(target_os = "windows")]
#[error("couldn't create WHP partition object ({0:#x})")]
CreatePartitionFailed(windows_sys::core::HRESULT),
Expand Down Expand Up @@ -736,10 +732,6 @@ enum VmmError {
#[cfg(target_os = "macos")]
#[error("couldn't map memory to the VM ({0:#x})")]
MapRamFailed(NonZero<applevisor_sys::hv_return_t>),

#[cfg(target_os = "macos")]
#[error("couldn't get default MTLDevice")]
GetMetalDeviceFailed,
}

/// Represents an error when [`main_cpu()`] fails to reach event loop.
Expand Down
13 changes: 10 additions & 3 deletions gui/src/vmm/screen/metal/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
use self::buffer::MetalBuffer;
use super::{Screen, ScreenBuffer, VmmError};
use super::{Screen, ScreenBuffer};
use crate::vmm::VmmScreen;
use metal::{CAMetalLayer, Device, MetalLayer};
use objc::runtime::{Object, NO, YES};
Expand All @@ -22,11 +22,11 @@ pub struct Metal {
}

impl Metal {
pub fn new(screen: &VmmScreen) -> Result<Self, VmmError> {
pub fn new(screen: &VmmScreen) -> Result<Self, MetalError> {
// Get Metal device.
let device = match Device::system_default() {
Some(v) => v,
None => return Err(VmmError::GetMetalDeviceFailed),
None => return Err(MetalError::GetDeviceFailed),
};

// Setup Metal layer.
Expand Down Expand Up @@ -70,6 +70,13 @@ impl Screen for Metal {
}
}

/// Represents an error when [`Metal::new()`] fails.
#[derive(Debug, Error)]
pub enum MetalError {
#[error("couldn't get default MTLDevice")]
GetDeviceFailed,
}

/// Implementation of [`Screen::UpdateErr`].
#[derive(Debug, Error)]
pub enum UpdateError {}
13 changes: 6 additions & 7 deletions gui/src/vmm/screen/mod.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
use super::VmmError;
// SPDX-License-Identifier: MIT OR Apache-2.0
use std::error::Error;
use std::sync::Arc;

#[cfg(target_os = "macos")]
mod metal;
#[cfg(not(target_os = "macos"))]
mod vulkan;
#[cfg_attr(target_os = "macos", path = "metal/mod.rs")]
#[cfg_attr(not(target_os = "macos"), path = "vulkan/mod.rs")]
mod engine;

#[cfg(not(target_os = "macos"))]
pub type Default = self::vulkan::Vulkan;
pub type Default = self::engine::Vulkan;

#[cfg(target_os = "macos")]
pub type Default = self::metal::Metal;
pub type Default = self::engine::Metal;

/// Encapsulates a platform-specific surface for drawing a VM screen.
pub trait Screen: 'static {
Expand Down
15 changes: 11 additions & 4 deletions gui/src/vmm/screen/vulkan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use self::ffi::{
get_physical_device_sparse_image_format_properties,
get_physical_device_sparse_image_format_properties2, get_physical_device_tool_properties,
};
use super::{Screen, ScreenBuffer, VmmError};
use super::{Screen, ScreenBuffer};
use crate::vmm::VmmScreen;
use ash::vk::{DeviceCreateInfo, DeviceQueueCreateInfo, Handle, QueueFlags};
use ash::{Device, Instance, InstanceFnV1_0, InstanceFnV1_1, InstanceFnV1_3};
Expand All @@ -31,7 +31,7 @@ pub struct Vulkan {
}

impl Vulkan {
pub fn new(screen: &VmmScreen) -> Result<Self, VmmError> {
pub fn new(screen: &VmmScreen) -> Result<Self, VulkanError> {
// Wrap VkInstance.
let instance = screen.vk_instance.try_into().unwrap();
let instance = ash::vk::Instance::from_raw(instance);
Expand Down Expand Up @@ -87,7 +87,7 @@ impl Vulkan {
let device = DeviceCreateInfo::default().queue_create_infos(&queues);
let device = match unsafe { instance.create_device(physical, &device, None) } {
Ok(v) => v,
Err(e) => return Err(VmmError::CreateVulkanDeviceFailed(e)),
Err(e) => return Err(VulkanError::CreateDeviceFailed(e)),
};

Ok(Self {
Expand All @@ -100,7 +100,7 @@ impl Vulkan {
_: ash::vk::Instance,
_: *const ash::vk::AllocationCallbacks<'_>,
) {
unreachable!();
unimplemented!()
}
}

Expand All @@ -124,6 +124,13 @@ impl Screen for Vulkan {
}
}

/// Represents an error when [`Vulkan::new()`] fails.
#[derive(Debug, Error)]
pub enum VulkanError {
#[error("couldn't create a logical device")]
CreateDeviceFailed(#[source] ash::vk::Result),
}

/// Implementation of [`Screen::UpdateErr`].
#[derive(Debug, Error)]
pub enum UpdateError {}

0 comments on commit db87a86

Please sign in to comment.