backend: Adopt to new wayland-display handling

This commit is contained in:
Victoria Brekenfeld 2022-07-04 15:29:31 +02:00
parent b126dfaf77
commit 270f06182e
7 changed files with 351 additions and 379 deletions

View file

@ -1,11 +1,14 @@
// SPDX-License-Identifier: GPL-3.0-only
use crate::state::get_dnd_icon;
use crate::{
wayland::handlers::data_device::get_dnd_icon,
utils::prelude::*,
};
use smithay::{
backend::renderer::{Frame, ImportAll, ImportMem, Renderer, Texture},
desktop::space::{RenderElement, SpaceOutputTuple, SurfaceTree},
reexports::wayland_server::protocol::wl_surface,
utils::{Logical, Point, Rectangle, Size, Transform},
utils::{IsAlive, Logical, Physical, Point, Rectangle, Size, Scale, Transform},
wayland::{
compositor::{get_role, with_states},
seat::{CursorImageAttributes, CursorImageStatus, Seat},
@ -125,27 +128,16 @@ pub fn draw_surface_cursor(
where
{
let mut position = location.into();
let ret = with_states(&surface, |states| {
Some(
states
.data_map
.get::<Mutex<CursorImageAttributes>>()
.unwrap()
.lock()
.unwrap()
.hotspot,
)
})
.unwrap_or(None);
position -= match ret {
Some(h) => h,
None => {
slog_scope::warn!(
"Trying to display as a cursor a surface that does not have the CursorImage role."
);
(0, 0).into()
}
};
let h = with_states(&surface, |states| {
states
.data_map
.get::<Mutex<CursorImageAttributes>>()
.unwrap()
.lock()
.unwrap()
.hotspot
});
position -= h;
SurfaceTree {
surface,
position,
@ -171,7 +163,7 @@ pub fn draw_dnd_icon(
pub struct PointerElement<T: Texture> {
texture: T,
position: Point<i32, Logical>,
position: Point<f64, Logical>,
size: Size<i32, Logical>,
new_frame: bool,
}
@ -179,7 +171,7 @@ pub struct PointerElement<T: Texture> {
impl<T: Texture> PointerElement<T> {
pub fn new(
texture: T,
relative_pointer_pos: Point<i32, Logical>,
relative_pointer_pos: Point<f64, Logical>,
new_frame: bool,
) -> PointerElement<T> {
let size = texture.size().to_logical(1, Transform::Normal);
@ -201,16 +193,21 @@ where
0
}
fn geometry(&self) -> Rectangle<i32, Logical> {
Rectangle::from_loc_and_size(self.position, self.size)
fn location(&self, scale: impl Into<Scale<f64>>) -> Point<f64, Physical> {
self.position.to_physical(scale)
}
fn geometry(&self, scale: impl Into<Scale<f64>>) -> Rectangle<i32, Physical> {
Rectangle::from_loc_and_size(self.position, self.size.to_f64()).to_physical(scale).to_i32_round()
}
fn accumulated_damage(
&self,
scale: impl Into<Scale<f64>>,
_: Option<SpaceOutputTuple<'_, '_>>,
) -> Vec<Rectangle<i32, Logical>> {
) -> Vec<Rectangle<i32, Physical>> {
if self.new_frame {
vec![Rectangle::from_loc_and_size((0, 0), self.size)]
vec![Rectangle::from_loc_and_size((0, 0), self.size.to_physical_precise_round(scale))]
} else {
vec![]
}
@ -220,21 +217,18 @@ where
&self,
_renderer: &mut R,
frame: &mut <R as Renderer>::Frame,
scale: f64,
position: Point<i32, Logical>,
damage: &[Rectangle<i32, Logical>],
scale: impl Into<Scale<f64>>,
position: Point<f64, Physical>,
damage: &[Rectangle<i32, Physical>],
_log: &slog::Logger,
) -> Result<(), <R as Renderer>::Error> {
frame.render_texture_at(
&self.texture,
position.to_f64().to_physical(scale as f64).to_i32_round(),
position.to_i32_round(),
1,
scale as f64,
scale,
Transform::Normal,
&*damage
.iter()
.map(|rect| rect.to_physical(1).to_f64())
.collect::<Vec<_>>(),
damage,
1.0,
)?;
Ok(())
@ -259,8 +253,8 @@ impl Default for CursorState {
pub fn draw_cursor<R, I>(
renderer: &mut R,
seat: &Seat,
location: Point<i32, Logical>,
seat: &Seat<State>,
location: Point<f64, Logical>,
start_time: &std::time::Instant,
draw_default: bool,
) -> Option<I>
@ -272,8 +266,8 @@ where
// draw the dnd icon if applicable
{
if let Some(wl_surface) = get_dnd_icon(seat) {
if wl_surface.as_ref().is_alive() {
return Some(draw_dnd_icon(wl_surface, location).into());
if wl_surface.alive() {
return Some(draw_dnd_icon(wl_surface, location.to_i32_round()).into());
}
}
}
@ -287,7 +281,7 @@ where
.map(|cell| {
let mut cursor_status = cell.borrow_mut();
if let CursorImageStatus::Image(ref surface) = *cursor_status {
if !surface.as_ref().is_alive() {
if !surface.alive() {
*cursor_status = CursorImageStatus::Default;
}
}
@ -296,7 +290,7 @@ where
.unwrap_or(CursorImageStatus::Default);
if let CursorImageStatus::Image(wl_surface) = cursor_status {
Some(draw_surface_cursor(wl_surface.clone(), location).into())
Some(draw_surface_cursor(wl_surface.clone(), location.to_i32_round()).into())
} else if draw_default {
let seat_userdata = seat.user_data();
seat_userdata.insert_if_missing(CursorState::default);
@ -329,7 +323,7 @@ where
pointer_images.push((frame.clone(), Box::new(texture.clone())));
texture
});
let hotspot = Point::<i32, Logical>::from((frame.xhot as i32, frame.yhot as i32));
let hotspot = Point::<i32, Logical>::from((frame.xhot as i32, frame.yhot as i32)).to_f64();
*state.current_image.borrow_mut() = Some(frame);
Some(PointerElement::new(pointer_image.clone(), location - hotspot, new_frame).into())

View file

@ -18,12 +18,12 @@ use smithay::{
},
},
desktop::{
draw_layer_surface, draw_window, layer_map_for_output,
draw_layer_surface, draw_layer_popups, draw_window, draw_window_popups, layer_map_for_output,
space::{RenderElement, RenderError, SpaceOutputTuple, SurfaceTree},
utils::damage_from_surface_tree,
Window,
},
utils::{Logical, Point, Rectangle, Transform},
utils::{Physical, Point, Rectangle, Scale, Transform},
wayland::{output::Output, shell::wlr_layer::Layer as WlrLayer},
};
@ -44,7 +44,7 @@ smithay::custom_elements! {
EguiFrame=EguiFrame,
}
// TODO: due to the lifetime, we cannot be generic over CustomElem's renderer
// TODO: due to the lifetime of MultiRenderer, we cannot be generic over CustomElem's renderer
// util after GATs land. So we generate with the macro for Gles2 and then
// do a manual impl for MultiRenderer.
impl RenderElement<GlMultiRenderer<'_>> for CustomElem {
@ -52,24 +52,29 @@ impl RenderElement<GlMultiRenderer<'_>> for CustomElem {
RenderElement::<Gles2Renderer>::id(self)
}
fn geometry(&self) -> Rectangle<i32, Logical> {
RenderElement::<Gles2Renderer>::geometry(self)
fn location(&self, scale: impl Into<Scale<f64>>) -> Point<f64, Physical> {
RenderElement::<Gles2Renderer>::location(self, scale)
}
fn geometry(&self, scale: impl Into<Scale<f64>>) -> Rectangle<i32, Physical> {
RenderElement::<Gles2Renderer>::geometry(self, scale)
}
fn accumulated_damage(
&self,
scale: impl Into<Scale<f64>>,
for_values: Option<SpaceOutputTuple<'_, '_>>,
) -> Vec<Rectangle<i32, Logical>> {
RenderElement::<Gles2Renderer>::accumulated_damage(self, for_values)
) -> Vec<Rectangle<i32, Physical>> {
RenderElement::<Gles2Renderer>::accumulated_damage(self, scale, for_values)
}
fn draw(
&self,
renderer: &mut GlMultiRenderer<'_>,
frame: &mut GlMultiFrame,
scale: f64,
location: Point<i32, Logical>,
damage: &[Rectangle<i32, Logical>],
scale: impl Into<Scale<f64>>,
location: Point<f64, Physical>,
damage: &[Rectangle<i32, Physical>],
log: &Logger,
) -> Result<(), MultiError<EglGlesBackend, EglGlesBackend>> {
RenderElement::<Gles2Renderer>::draw(
@ -130,7 +135,7 @@ pub fn render_output<R>(
output: &Output,
hardware_cursor: bool,
#[cfg(feature = "debug")] fps: &mut Fps,
) -> Result<Option<Vec<Rectangle<i32, Logical>>>, RenderError<R>>
) -> Result<Option<Vec<Rectangle<i32, Physical>>>, RenderError<R>>
where
R: Renderer + ImportAll + AsGles2Renderer,
<R as Renderer>::TextureId: Clone + 'static,
@ -140,7 +145,6 @@ where
{
fps.start();
}
let workspace = state.shell.active_space(output);
let maybe_fullscreen_window = workspace.get_fullscreen(output).cloned();
let res = if let Some(window) = maybe_fullscreen_window {
@ -179,7 +183,7 @@ fn render_desktop<R>(
output: &Output,
hardware_cursor: bool,
#[cfg(feature = "debug")] fps: &mut Fps,
) -> Result<Option<Vec<Rectangle<i32, Logical>>>, RenderError<R>>
) -> Result<Option<Vec<Rectangle<i32, Physical>>>, RenderError<R>>
where
R: Renderer + ImportAll + AsGles2Renderer,
<R as Renderer>::TextureId: Clone + 'static,
@ -246,7 +250,7 @@ fn render_fullscreen<R>(
output: &Output,
hardware_cursor: bool,
#[cfg(feature = "debug")] fps: &mut Fps,
) -> Result<Option<Vec<Rectangle<i32, Logical>>>, RenderError<R>>
) -> Result<Option<Vec<Rectangle<i32, Physical>>>, RenderError<R>>
where
R: Renderer + ImportAll + AsGles2Renderer,
<R as Renderer>::TextureId: Clone + 'static,
@ -293,20 +297,32 @@ where
renderer
.render(mode.size, transform, |renderer, frame| {
let mut damage = window.accumulated_damage(None);
let mut damage = window.accumulated_damage((0.0, 0.0), scale, None);
frame.clear(
CLEAR_COLOR,
&[Rectangle::from_loc_and_size((0, 0), mode.size).to_f64()],
&[Rectangle::from_loc_and_size((0, 0), mode.size)],
)?;
draw_window(
renderer,
frame,
&window,
scale,
(0, 0),
(0.0, 0.0),
&[Rectangle::from_loc_and_size(
(0, 0),
mode.size.to_f64().to_logical(scale).to_i32_round(),
mode.size,
)],
&slog_scope::logger(),
)?;
draw_window_popups(
renderer,
frame,
&window,
scale,
(0.0, 0.0),
&[Rectangle::from_loc_and_size(
(0, 0),
mode.size,
)],
&slog_scope::logger(),
)?;
@ -318,22 +334,30 @@ where
frame,
layer_surface,
scale,
geo.loc,
&[Rectangle::from_loc_and_size((0, 0), geo.size)],
geo.loc.to_f64().to_physical(scale),
&[Rectangle::from_loc_and_size((0, 0), geo.size.to_physical_precise_round(scale))],
&slog_scope::logger(),
)?;
if let Some(surface) = layer_surface.get_surface() {
damage.extend(damage_from_surface_tree(surface, geo.loc, None));
}
draw_layer_popups(
renderer,
frame,
layer_surface,
scale,
geo.loc.to_f64().to_physical(scale),
&[Rectangle::from_loc_and_size((0, 0), geo.size.to_physical_precise_round(scale))],
&slog_scope::logger(),
)?;
damage.extend(damage_from_surface_tree(layer_surface.wl_surface(), geo.loc.to_f64().to_physical(scale), scale, None));
}
for elem in custom_elements {
let geo = elem.geometry();
let elem_damage = elem.accumulated_damage(None);
let loc = elem.location(scale);
let geo = elem.geometry(scale);
let elem_damage = elem.accumulated_damage(scale, None);
elem.draw(
renderer,
frame,
scale,
geo.loc,
loc,
&[Rectangle::from_loc_and_size((0, 0), geo.size)],
&slog_scope::logger(),
)?;