Skip to content

Commit

Permalink
WayVR: Redraw only if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
olekolek1000 committed Oct 26, 2024
1 parent cf5eabd commit 9195a33
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
13 changes: 11 additions & 2 deletions src/backend/wayvr/comp.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use smithay::backend::renderer::utils::on_commit_buffer_handler;
use smithay::input::{Seat, SeatHandler, SeatState};
use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel;
use smithay::reexports::wayland_server;
use smithay::reexports::wayland_server::protocol::{wl_buffer, wl_seat, wl_surface};
use smithay::reexports::wayland_server::{self, Resource};
use smithay::reexports::wayland_server::Resource;
use smithay::wayland::buffer::BufferHandler;
use smithay::wayland::shm::{ShmHandler, ShmState};
use smithay::{
delegate_compositor, delegate_data_device, delegate_seat, delegate_shm, delegate_xdg_shell,
};
use std::collections::HashSet;
use std::os::fd::OwnedFd;

use smithay::utils::Serial;
Expand Down Expand Up @@ -35,8 +37,14 @@ pub struct Application {
pub seat_state: SeatState<Application>,
pub shm: ShmState,
pub data_device: DataDeviceState,

pub wayvr_tasks: SyncEventQueue<WayVRTask>,
pub redraw_requests: HashSet<wayland_server::backend::ObjectId>,
}

impl Application {
pub fn check_redraw(&mut self, surface: &WlSurface) -> bool {
self.redraw_requests.remove(&surface.id())
}
}

impl compositor::CompositorHandler for Application {
Expand All @@ -53,6 +61,7 @@ impl compositor::CompositorHandler for Application {

fn commit(&mut self, surface: &WlSurface) {
on_commit_buffer_handler::<Self>(surface);
self.redraw_requests.insert(surface.id());
}
}

Expand Down
10 changes: 6 additions & 4 deletions src/backend/wayvr/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ fn generate_auth_key() -> String {
uuid.to_string()
}

struct DisplayWindow {
window_handle: window::WindowHandle,
pub struct DisplayWindow {
pub window_handle: window::WindowHandle,
pub toplevel: ToplevelSurface,
process_handle: process::ProcessHandle,
toplevel: ToplevelSurface,
}

pub struct SpawnProcessResult {
Expand All @@ -49,8 +49,9 @@ pub struct Display {
pub name: String,
pub visible: bool,
pub overlay_id: Option<OverlayID>,
pub wants_redraw: bool,
wm: Rc<RefCell<window::WindowManager>>,
displayed_windows: Vec<DisplayWindow>,
pub displayed_windows: Vec<DisplayWindow>,
wayland_env: super::WaylandEnv,

// Render data stuff
Expand Down Expand Up @@ -108,6 +109,7 @@ impl Display {
height,
name: String::from(name),
displayed_windows: Vec::new(),
wants_redraw: true,
egl_data,
dmabuf_data,
egl_image,
Expand Down
26 changes: 23 additions & 3 deletions src/backend/wayvr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod smithay_wrapper;
mod time;
mod window;

use std::{cell::RefCell, rc::Rc};
use std::{cell::RefCell, collections::HashSet, rc::Rc};

use comp::Application;
use display::DisplayVec;
Expand Down Expand Up @@ -94,6 +94,7 @@ impl WayVR {
shm,
data_device,
wayvr_tasks: tasks.clone(),
redraw_requests: HashSet::new(),
};

let time_start = get_millis();
Expand All @@ -118,22 +119,41 @@ impl WayVR {
// millis since the start of wayvr
let display = self
.displays
.get(&display)
.get_mut(&display)
.ok_or(anyhow::anyhow!(STR_INVALID_HANDLE_DISP))?;

let time_ms = get_millis() - self.time_start;
if !display.wants_redraw {
// Nothing changed, do not render
return Ok(());
}

if !display.visible {
// Display is invisible, do not render
return Ok(());
}

let time_ms = get_millis() - self.time_start;

display.tick_render(&mut self.gles_renderer, time_ms)?;
display.wants_redraw = false;

Ok(())
}

pub fn tick_events(&mut self) -> anyhow::Result<()> {
// Check for redraw events
self.displays.iter_mut(&mut |_, disp| {
for disp_window in &disp.displayed_windows {
if self
.manager
.state
.check_redraw(disp_window.toplevel.wl_surface())
{
disp.wants_redraw = true;
}
}
});

// Tick all child processes
let mut to_remove: SmallVec<[(process::ProcessHandle, display::DisplayHandle); 2]> =
SmallVec::new();
Expand Down

0 comments on commit 9195a33

Please sign in to comment.