even more error handling

This commit is contained in:
galister
2024-02-19 03:21:00 +01:00
parent 1d9fa95ea0
commit 5d812c3b09
16 changed files with 541 additions and 499 deletions

View File

@@ -25,26 +25,26 @@ pub(super) struct LinePool {
}
impl LinePool {
pub fn new(graphics: Arc<WlxGraphics>) -> Self {
let mut command_buffer = graphics.create_command_buffer(CommandBufferUsage::OneTimeSubmit);
pub fn new(graphics: Arc<WlxGraphics>) -> anyhow::Result<Self> {
let mut command_buffer =
graphics.create_command_buffer(CommandBufferUsage::OneTimeSubmit)?;
let buf = vec![255; 16];
let texture = command_buffer.texture2d(2, 2, Format::R8G8B8A8_UNORM, &buf);
command_buffer.build_and_execute_now();
let texture = command_buffer.texture2d(2, 2, Format::R8G8B8A8_UNORM, &buf)?;
command_buffer.build_and_execute_now()?;
graphics
.transition_layout(
texture.clone(),
ImageLayout::ShaderReadOnlyOptimal,
ImageLayout::TransferSrcOptimal,
)
.wait(None)
.unwrap();
)?
.wait(None)?;
let view = ImageView::new_default(texture).unwrap();
let view = ImageView::new_default(texture)?;
LinePool {
Ok(LinePool {
lines: IdMap::new(),
view,
colors: [
@@ -54,7 +54,7 @@ impl LinePool {
Vec4::new(0.375, 0., 0.5, 1.),
Vec4::new(1., 0., 0., 1.),
],
}
})
}
pub fn allocate(&mut self) -> usize {
@@ -140,9 +140,13 @@ impl LinePool {
}
}
pub fn update(&mut self, overlay: &mut OverlayManager, app: &mut AppState) {
pub fn update(
&mut self,
overlay: &mut OverlayManager,
app: &mut AppState,
) -> anyhow::Result<()> {
for data in self.lines.values_mut() {
data.after_input(overlay, app);
data.after_input(overlay, app)?;
if data.state.want_visible {
if data.state.dirty {
data.upload_texture(overlay, &app.graphics);
@@ -153,6 +157,7 @@ impl LinePool {
data.upload_color(overlay);
}
}
Ok(())
}
}
@@ -161,10 +166,18 @@ struct StaticRenderer {
}
impl OverlayRenderer for StaticRenderer {
fn init(&mut self, _app: &mut AppState) {}
fn pause(&mut self, _app: &mut AppState) {}
fn resume(&mut self, _app: &mut AppState) {}
fn render(&mut self, _app: &mut AppState) {}
fn init(&mut self, _app: &mut AppState) -> anyhow::Result<()> {
Ok(())
}
fn pause(&mut self, _app: &mut AppState) -> anyhow::Result<()> {
Ok(())
}
fn resume(&mut self, _app: &mut AppState) -> anyhow::Result<()> {
Ok(())
}
fn render(&mut self, _app: &mut AppState) -> anyhow::Result<()> {
Ok(())
}
fn view(&mut self) -> Option<Arc<ImageView>> {
Some(self.view.clone())
}

View File

@@ -83,7 +83,7 @@ pub fn openvr_run(running: Arc<AtomicBool>) -> Result<(), BackendError> {
};
let mut state = {
let graphics = WlxGraphics::new_openvr(instance_extensions, device_extensions_fn);
let graphics = WlxGraphics::new_openvr(instance_extensions, device_extensions_fn)?;
AppState::from_graphics(graphics)?
};
@@ -119,7 +119,7 @@ pub fn openvr_run(running: Arc<AtomicBool>) -> Result<(), BackendError> {
let mut next_device_update = Instant::now();
let mut due_tasks = VecDeque::with_capacity(4);
let mut lines = LinePool::new(state.graphics.clone());
let mut lines = LinePool::new(state.graphics.clone())?;
let pointer_lines = [lines.allocate(), lines.allocate()];
'main_loop: loop {
@@ -198,11 +198,11 @@ pub fn openvr_run(running: Arc<AtomicBool>) -> Result<(), BackendError> {
}
}
lines.update(&mut overlay_mngr, &mut state);
lines.update(&mut overlay_mngr, &mut state)?;
overlays
.iter_mut()
.for_each(|o| o.after_input(&mut overlay_mngr, &mut state));
for o in overlays.iter_mut() {
o.after_input(&mut overlay_mngr, &mut state)?;
}
#[cfg(feature = "osc")]
if let Some(ref mut sender) = osc_sender {
@@ -211,10 +211,11 @@ pub fn openvr_run(running: Arc<AtomicBool>) -> Result<(), BackendError> {
log::debug!("Rendering frame");
overlays
.iter_mut()
.filter(|o| o.state.want_visible)
.for_each(|o| o.render(&mut state));
for o in overlays.iter_mut() {
if o.state.want_visible {
o.render(&mut state)?;
}
}
log::debug!("Rendering overlays");

View File

@@ -32,7 +32,7 @@ impl OverlayData<OpenVrOverlayData> {
&mut self,
overlay: &mut OverlayManager,
app: &mut AppState,
) -> OverlayHandle {
) -> anyhow::Result<OverlayHandle> {
let key = format!("wlx-{}", self.state.name);
log::debug!("Create overlay with key: {}", &key);
let handle = match overlay.create_overlay(&key, &key) {
@@ -51,7 +51,7 @@ impl OverlayData<OpenVrOverlayData> {
self.data.handle = Some(handle);
self.data.color = Vec4::ONE;
self.init(app);
self.init(app)?;
if self.data.width < f32::EPSILON {
self.data.width = 1.0;
@@ -63,15 +63,20 @@ impl OverlayData<OpenVrOverlayData> {
self.upload_curvature(overlay);
self.upload_sort_order(overlay);
handle
Ok(handle)
}
pub(super) fn after_input(&mut self, overlay: &mut OverlayManager, app: &mut AppState) {
pub(super) fn after_input(
&mut self,
overlay: &mut OverlayManager,
app: &mut AppState,
) -> anyhow::Result<()> {
if self.state.want_visible && !self.data.visible {
self.show_internal(overlay, app);
self.show_internal(overlay, app)?;
} else if !self.state.want_visible && self.data.visible {
self.hide_internal(overlay, app);
self.hide_internal(overlay, app)?;
}
Ok(())
}
pub(super) fn after_render(&mut self, overlay: &mut OverlayManager, graphics: &WlxGraphics) {
@@ -85,29 +90,37 @@ impl OverlayData<OpenVrOverlayData> {
}
}
fn show_internal(&mut self, overlay: &mut OverlayManager, app: &mut AppState) {
fn show_internal(
&mut self,
overlay: &mut OverlayManager,
app: &mut AppState,
) -> anyhow::Result<()> {
let handle = match self.data.handle {
Some(handle) => handle,
None => self.initialize(overlay, app),
None => self.initialize(overlay, app)?,
};
log::debug!("{}: show", self.state.name);
if let Err(e) = overlay.set_visibility(handle, true) {
log::error!("{}: Failed to show overlay: {}", self.state.name, e);
}
self.data.visible = true;
self.backend.resume(app);
self.backend.resume(app)
}
fn hide_internal(&mut self, overlay: &mut OverlayManager, app: &mut AppState) {
fn hide_internal(
&mut self,
overlay: &mut OverlayManager,
app: &mut AppState,
) -> anyhow::Result<()> {
let Some(handle) = self.data.handle else {
return;
return Ok(());
};
log::debug!("{}: hide", self.state.name);
if let Err(e) = overlay.set_visibility(handle, false) {
log::error!("{}: Failed to hide overlay: {}", self.state.name, e);
}
self.data.visible = false;
self.backend.pause(app);
self.backend.pause(app)
}
pub(super) fn upload_alpha(&self, overlay: &mut OverlayManager) {