From 4e391013c8bf8544fb766bee5dbae10cfdbc9d93 Mon Sep 17 00:00:00 2001 From: Billy Messenger Date: Wed, 16 Dec 2020 10:03:51 -0600 Subject: don't panic when swapchain frame is outdated --- graphics/src/window/compositor.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'graphics/src') diff --git a/graphics/src/window/compositor.rs b/graphics/src/window/compositor.rs index 0bc8cbc8..39485153 100644 --- a/graphics/src/window/compositor.rs +++ b/graphics/src/window/compositor.rs @@ -40,6 +40,9 @@ pub trait Compositor: Sized { /// Draws the output primitives to the next frame of the given [`SwapChain`]. /// + /// This will return an error if drawing could not be completed on this frame. + /// If an error occurs, try calling this again on the next frame. + /// /// [`SwapChain`]: Self::SwapChain fn draw>( &mut self, @@ -49,5 +52,5 @@ pub trait Compositor: Sized { background_color: Color, output: &::Output, overlay: &[T], - ) -> mouse::Interaction; + ) -> Result; } -- cgit From a7d2834a6d15466eecca29bb6357d3539cb652cd Mon Sep 17 00:00:00 2001 From: Billy Messenger Date: Thu, 22 Jul 2021 13:08:13 -0500 Subject: add custom error for Compositor::draw() --- graphics/src/window.rs | 2 +- graphics/src/window/compositor.rs | 33 +++++++++++++++++++++++++++++---- 2 files changed, 30 insertions(+), 5 deletions(-) (limited to 'graphics/src') diff --git a/graphics/src/window.rs b/graphics/src/window.rs index 3e74db5f..6813643d 100644 --- a/graphics/src/window.rs +++ b/graphics/src/window.rs @@ -4,7 +4,7 @@ mod compositor; #[cfg(feature = "opengl")] mod gl_compositor; -pub use compositor::Compositor; +pub use compositor::{Compositor, CompositorDrawError}; #[cfg(feature = "opengl")] pub use gl_compositor::GLCompositor; diff --git a/graphics/src/window/compositor.rs b/graphics/src/window/compositor.rs index 7342245c..9f7cb43f 100644 --- a/graphics/src/window/compositor.rs +++ b/graphics/src/window/compositor.rs @@ -43,9 +43,6 @@ pub trait Compositor: Sized { /// Draws the output primitives to the next frame of the given [`SwapChain`]. /// - /// This will return an error if drawing could not be completed on this frame. - /// If an error occurs, try calling this again on the next frame. - /// /// [`SwapChain`]: Self::SwapChain fn draw>( &mut self, @@ -55,5 +52,33 @@ pub trait Compositor: Sized { background_color: Color, output: &::Output, overlay: &[T], - ) -> Result; + ) -> Result; +} + +/// Result of an unsuccessful call to [`Compositor::draw`]. +#[derive(Debug)] +pub enum CompositorDrawError { + /// The swapchain is outdated. Try rendering again next frame. + SwapchainOutdated(Box), + /// A fatal swapchain error occured. Rendering cannot continue. + FatalSwapchainError(Box), +} + +impl std::error::Error for CompositorDrawError {} + +impl std::fmt::Display for CompositorDrawError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + CompositorDrawError::SwapchainOutdated(e) => write!( + f, + "Swapchain is outdated: {}. Try rendering next frame.", + e + ), + CompositorDrawError::FatalSwapchainError(e) => write!( + f, + "Fatal swapchain error: {}. Rendering cannot continue.", + e + ), + } + } } -- cgit From e5010b8ab87b2e30feea366396bc060c8e793d8d Mon Sep 17 00:00:00 2001 From: Billy Messenger Date: Thu, 22 Jul 2021 13:23:36 -0500 Subject: redo custom error for Compositor::draw() --- graphics/src/window.rs | 2 +- graphics/src/window/compositor.rs | 41 +++++++++++++++++++-------------------- 2 files changed, 21 insertions(+), 22 deletions(-) (limited to 'graphics/src') diff --git a/graphics/src/window.rs b/graphics/src/window.rs index 6813643d..365ddfbc 100644 --- a/graphics/src/window.rs +++ b/graphics/src/window.rs @@ -4,7 +4,7 @@ mod compositor; #[cfg(feature = "opengl")] mod gl_compositor; -pub use compositor::{Compositor, CompositorDrawError}; +pub use compositor::{Compositor, SwapChainError}; #[cfg(feature = "opengl")] pub use gl_compositor::GLCompositor; diff --git a/graphics/src/window/compositor.rs b/graphics/src/window/compositor.rs index 9f7cb43f..9811d95d 100644 --- a/graphics/src/window/compositor.rs +++ b/graphics/src/window/compositor.rs @@ -52,33 +52,32 @@ pub trait Compositor: Sized { background_color: Color, output: &::Output, overlay: &[T], - ) -> Result; + ) -> Result; } /// Result of an unsuccessful call to [`Compositor::draw`]. -#[derive(Debug)] -pub enum CompositorDrawError { - /// The swapchain is outdated. Try rendering again next frame. - SwapchainOutdated(Box), - /// A fatal swapchain error occured. Rendering cannot continue. - FatalSwapchainError(Box), +/// Result of an unsuccessful call to [`SwapChain::get_current_frame`]. +#[derive(Clone, PartialEq, Eq, Debug)] +pub enum SwapChainError { + /// A timeout was encountered while trying to acquire the next frame. + Timeout, + /// The underlying surface has changed, and therefore the swap chain must be updated. + Outdated, + /// The swap chain has been lost and needs to be recreated. + Lost, + /// There is no more memory left to allocate a new frame. + OutOfMemory, } -impl std::error::Error for CompositorDrawError {} +impl std::error::Error for SwapChainError {} -impl std::fmt::Display for CompositorDrawError { +impl std::fmt::Display for SwapChainError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - CompositorDrawError::SwapchainOutdated(e) => write!( - f, - "Swapchain is outdated: {}. Try rendering next frame.", - e - ), - CompositorDrawError::FatalSwapchainError(e) => write!( - f, - "Fatal swapchain error: {}. Rendering cannot continue.", - e - ), - } + write!(f, "{}", match self { + Self::Timeout => "A timeout was encountered while trying to acquire the next frame", + Self::Outdated => "The underlying surface has changed, and therefore the swap chain must be updated", + Self::Lost => "The swap chain has been lost and needs to be recreated", + Self::OutOfMemory => "There is no more memory left to allocate a new frame", + }) } } -- cgit From b629a8025426afd5a6a03a5d17f4e28a6bed6e30 Mon Sep 17 00:00:00 2001 From: Billy Messenger Date: Thu, 22 Jul 2021 13:26:27 -0500 Subject: small documentation error --- graphics/src/window/compositor.rs | 1 - 1 file changed, 1 deletion(-) (limited to 'graphics/src') diff --git a/graphics/src/window/compositor.rs b/graphics/src/window/compositor.rs index 9811d95d..e6633293 100644 --- a/graphics/src/window/compositor.rs +++ b/graphics/src/window/compositor.rs @@ -56,7 +56,6 @@ pub trait Compositor: Sized { } /// Result of an unsuccessful call to [`Compositor::draw`]. -/// Result of an unsuccessful call to [`SwapChain::get_current_frame`]. #[derive(Clone, PartialEq, Eq, Debug)] pub enum SwapChainError { /// A timeout was encountered while trying to acquire the next frame. -- cgit From 3e03a42bc69562639784a1b560978bf184576824 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 4 Aug 2021 21:45:38 +0700 Subject: Use `thiserror` to derive `Error` for `SwapChainError` --- graphics/src/window/compositor.rs | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) (limited to 'graphics/src') diff --git a/graphics/src/window/compositor.rs b/graphics/src/window/compositor.rs index e6633293..de2a6990 100644 --- a/graphics/src/window/compositor.rs +++ b/graphics/src/window/compositor.rs @@ -1,6 +1,9 @@ use crate::{Color, Error, Viewport}; + use iced_native::mouse; + use raw_window_handle::HasRawWindowHandle; +use thiserror::Error; /// A graphics compositor that can draw to windows. pub trait Compositor: Sized { @@ -56,27 +59,22 @@ pub trait Compositor: Sized { } /// Result of an unsuccessful call to [`Compositor::draw`]. -#[derive(Clone, PartialEq, Eq, Debug)] +#[derive(Clone, PartialEq, Eq, Debug, Error)] pub enum SwapChainError { /// A timeout was encountered while trying to acquire the next frame. + #[error( + "A timeout was encountered while trying to acquire the next frame" + )] Timeout, /// The underlying surface has changed, and therefore the swap chain must be updated. + #[error( + "The underlying surface has changed, and therefore the swap chain must be updated." + )] Outdated, /// The swap chain has been lost and needs to be recreated. + #[error("The swap chain has been lost and needs to be recreated")] Lost, /// There is no more memory left to allocate a new frame. + #[error("There is no more memory left to allocate a new frame")] OutOfMemory, } - -impl std::error::Error for SwapChainError {} - -impl std::fmt::Display for SwapChainError { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", match self { - Self::Timeout => "A timeout was encountered while trying to acquire the next frame", - Self::Outdated => "The underlying surface has changed, and therefore the swap chain must be updated", - Self::Lost => "The swap chain has been lost and needs to be recreated", - Self::OutOfMemory => "There is no more memory left to allocate a new frame", - }) - } -} -- cgit