Skip to content

Commit

Permalink
fix: dont overload xr runtime with notify spam
Browse files Browse the repository at this point in the history
  • Loading branch information
galister committed Apr 24, 2024
1 parent 1fa842b commit 065ce8f
Show file tree
Hide file tree
Showing 12 changed files with 161 additions and 126 deletions.
93 changes: 2 additions & 91 deletions src/backend/common.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
use std::{
collections::{BinaryHeap, VecDeque},
f32::consts::PI,
sync::Arc,
time::Instant,
};
use std::{f32::consts::PI, sync::Arc};

use once_cell::sync::Lazy;
#[cfg(feature = "openxr")]
Expand All @@ -25,7 +20,7 @@ use crate::{
state::AppState,
};

use super::overlay::{OverlayBackend, OverlayData, OverlayState};
use super::overlay::OverlayData;

#[derive(Error, Debug)]
pub enum BackendError {
Expand Down Expand Up @@ -351,90 +346,6 @@ pub enum OverlaySelector {
Name(Arc<str>),
}

struct AppTask {
pub not_before: Instant,
pub task: TaskType,
}

impl PartialEq<AppTask> for AppTask {
fn eq(&self, other: &Self) -> bool {
self.not_before == other.not_before
}
}
impl PartialOrd<AppTask> for AppTask {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Eq for AppTask {}
impl Ord for AppTask {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.not_before.cmp(&other.not_before).reverse()
}
}

pub enum SystemTask {
ColorGain(ColorChannel, f32),
ResetPlayspace,
FixFloor,
}

pub type OverlayTask = dyn FnOnce(&mut AppState, &mut OverlayState) + Send;
pub type CreateOverlayTask =
dyn FnOnce(&mut AppState) -> Option<(OverlayState, Box<dyn OverlayBackend>)> + Send;

pub enum TaskType {
Global(Box<dyn FnOnce(&mut AppState) + Send>),
Overlay(OverlaySelector, Box<OverlayTask>),
CreateOverlay(OverlaySelector, Box<CreateOverlayTask>),
DropOverlay(OverlaySelector),
System(SystemTask),
}

#[derive(Deserialize, Clone, Copy)]
pub enum ColorChannel {
R,
G,
B,
All,
}

pub struct TaskContainer {
tasks: BinaryHeap<AppTask>,
}

impl TaskContainer {
pub fn new() -> Self {
Self {
tasks: BinaryHeap::new(),
}
}

pub fn enqueue(&mut self, task: TaskType) {
self.tasks.push(AppTask {
not_before: Instant::now(),
task,
});
}

pub fn enqueue_at(&mut self, task: TaskType, not_before: Instant) {
self.tasks.push(AppTask { not_before, task });
}

pub fn retrieve_due(&mut self, dest_buf: &mut VecDeque<TaskType>) {
let now = Instant::now();

while let Some(task) = self.tasks.peek() {
if task.not_before > now {
break;
}

// Safe unwrap because we peeked.
dest_buf.push_back(self.tasks.pop().unwrap().task);
}
}
}

pub fn raycast_plane(
source: &Affine3A,
source_fwd: Vec3A,
Expand Down
4 changes: 2 additions & 2 deletions src/backend/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ use glam::{Affine3A, Vec2, Vec3, Vec3A};

use smallvec::{smallvec, SmallVec};

use crate::backend::common::{snap_upright, OverlaySelector, TaskType};
use crate::backend::common::{snap_upright, OverlaySelector};
use crate::config::{save_state, AStrMapExt, GeneralConfig};
use crate::overlays::anchor::ANCHOR_NAME;
use crate::state::AppState;

use super::common::TaskContainer;
use super::task::{TaskContainer, TaskType};
use super::{
common::{raycast_cylinder, raycast_plane, OverlayContainer},
overlay::OverlayData,
Expand Down
2 changes: 2 additions & 0 deletions src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ pub mod uidev;
pub mod osc;

pub mod overlay;

pub mod task;
2 changes: 1 addition & 1 deletion src/backend/openvr/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use glam::Affine3A;
use ovr_overlay::{pose::Matrix3x4, settings::SettingsManager, sys::HmdMatrix34_t};
use thiserror::Error;

use crate::backend::common::{BackendError, ColorChannel};
use crate::backend::{common::BackendError, task::ColorChannel};

pub trait Affine3AConvert {
fn from_affine(affine: &Affine3A) -> Self;
Expand Down
5 changes: 2 additions & 3 deletions src/backend/openvr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use vulkano::{

use crate::{
backend::{
common::SystemTask,
common::{BackendError, OverlayContainer},
input::interact,
notifications::NotificationManager,
openvr::{
Expand All @@ -31,6 +31,7 @@ use crate::{
overlay::OpenVrOverlayData,
},
overlay::OverlayData,
task::{SystemTask, TaskType},
},
graphics::WlxGraphics,
overlays::{
Expand All @@ -40,8 +41,6 @@ use crate::{
state::AppState,
};

use super::common::{BackendError, OverlayContainer, TaskType};

pub mod helpers;
pub mod input;
pub mod lines;
Expand Down
5 changes: 2 additions & 3 deletions src/backend/openxr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ use vulkano::{command_buffer::CommandBufferUsage, Handle, VulkanObject};

use crate::{
backend::{
common::{OverlayContainer, TaskType},
common::{BackendError, OverlayContainer},
input::interact,
notifications::NotificationManager,
openxr::{input::DoubleClickCounter, lines::LinePool, overlay::OpenXrOverlayData},
overlay::OverlayData,
task::TaskType,
},
graphics::WlxGraphics,
overlays::{
Expand All @@ -27,8 +28,6 @@ use crate::{
state::AppState,
};

use super::common::BackendError;

mod helpers;
mod input;
mod lines;
Expand Down
2 changes: 1 addition & 1 deletion src/backend/overlay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ impl OverlayState {
self.saved_transform = None;
}

self.transform = app.anchor * self.get_transform();
self.transform = self.parent_transform(app).unwrap_or(app.anchor) * self.get_transform();

if self.grabbable && hard_reset {
self.realign(&app.input_state.hmd);
Expand Down
113 changes: 113 additions & 0 deletions src/backend/task.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
use std::{
cmp,
collections::{BinaryHeap, VecDeque},
sync::atomic::{self, AtomicUsize},
time::Instant,
};

use serde::Deserialize;

use crate::state::AppState;

use super::{
common::OverlaySelector,
overlay::{OverlayBackend, OverlayState},
};

static TASK_AUTO_INCREMENT: AtomicUsize = AtomicUsize::new(0);

struct AppTask {
pub not_before: Instant,
pub id: usize,
pub task: TaskType,
}

impl PartialEq<AppTask> for AppTask {
fn eq(&self, other: &Self) -> bool {
self.cmp(other) == cmp::Ordering::Equal
}
}
impl PartialOrd<AppTask> for AppTask {
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Eq for AppTask {}
impl Ord for AppTask {
fn cmp(&self, other: &Self) -> cmp::Ordering {
self.not_before
.cmp(&other.not_before)
.then(self.id.cmp(&other.id))
.reverse()
}
}

pub enum SystemTask {
ColorGain(ColorChannel, f32),
ResetPlayspace,
FixFloor,
}

pub type OverlayTask = dyn FnOnce(&mut AppState, &mut OverlayState) + Send;
pub type CreateOverlayTask =
dyn FnOnce(&mut AppState) -> Option<(OverlayState, Box<dyn OverlayBackend>)> + Send;

pub enum TaskType {
Global(Box<dyn FnOnce(&mut AppState) + Send>),
Overlay(OverlaySelector, Box<OverlayTask>),
CreateOverlay(OverlaySelector, Box<CreateOverlayTask>),
DropOverlay(OverlaySelector),
System(SystemTask),
}

#[derive(Deserialize, Clone, Copy)]
pub enum ColorChannel {
R,
G,
B,
All,
}

pub struct TaskContainer {
tasks: BinaryHeap<AppTask>,
}

impl TaskContainer {
pub fn new() -> Self {
Self {
tasks: BinaryHeap::new(),
}
}

pub fn enqueue(&mut self, task: TaskType) {
self.tasks.push(AppTask {
not_before: Instant::now(),
id: TASK_AUTO_INCREMENT.fetch_add(1, atomic::Ordering::Relaxed),
task,
});
}

/// Enqueue a task to be executed at a specific time.
/// If the time is in the past, the task will be executed immediately.
/// Multiple tasks enqueued for the same instant will be executed in order of submission.
pub fn enqueue_at(&mut self, task: TaskType, not_before: Instant) {
self.tasks.push(AppTask {
not_before,
id: TASK_AUTO_INCREMENT.fetch_add(1, atomic::Ordering::Relaxed),
task,
});
}

pub fn retrieve_due(&mut self, dest_buf: &mut VecDeque<TaskType>) {
let now = Instant::now();

while let Some(task) = self.tasks.peek() {
if task.not_before > now {
break;
}

// Safe unwrap because we peeked.
dest_buf.push_back(self.tasks.pop().unwrap().task);
}
}
}
3 changes: 2 additions & 1 deletion src/gui/modular/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ use serde::Deserialize;

use crate::{
backend::{
common::{ColorChannel, OverlaySelector, SystemTask, TaskType},
common::OverlaySelector,
input::PointerMode,
overlay::RelativeTo,
task::{ColorChannel, SystemTask, TaskType},
},
config::{save_settings, save_state, AStrSetExt},
overlays::{
Expand Down
3 changes: 2 additions & 1 deletion src/overlays/mirror.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ use wlx_capture::pipewire::{pipewire_select_screen, PipewireCapture, PipewireSel

use crate::{
backend::{
common::{OverlaySelector, TaskType},
common::OverlaySelector,
overlay::{
ui_transform, OverlayBackend, OverlayRenderer, OverlayState, SplitOverlayBackend,
},
task::TaskType,
},
state::{AppSession, AppState},
};
Expand Down
Loading

0 comments on commit 065ce8f

Please sign in to comment.