Makes a dedicated error type for screen (#1055)

This commit is contained in:
Putta Khunchalee 2024-10-21 01:43:45 +07:00 committed by GitHub
parent 0dfe0354dd
commit db87a860c8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 30 additions and 22 deletions

View File

@ -1,3 +1,4 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
use self::cpu::WhpCpu;
use self::partition::Partition;
use super::{CpuFeats, Hypervisor};

View File

@ -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;

View File

@ -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;

View File

@ -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),
@ -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.

View File

@ -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};
@ -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.
@ -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 {}

View File

@ -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 {

View File

@ -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};
@ -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);
@ -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 {
@ -100,7 +100,7 @@ impl Vulkan {
_: ash::vk::Instance,
_: *const ash::vk::AllocationCallbacks<'_>,
) {
unreachable!();
unimplemented!()
}
}
@ -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 {}