2021-04-28 22:31:49 +00:00
|
|
|
#![allow(clippy::too_many_arguments)]
|
|
|
|
|
2021-05-13 17:59:47 +00:00
|
|
|
use std::{cell::RefCell, rc::Rc};
|
2021-04-06 23:15:03 +00:00
|
|
|
|
|
|
|
use slog::Logger;
|
|
|
|
use smithay::{
|
2021-05-13 17:59:47 +00:00
|
|
|
backend::{
|
|
|
|
egl::display::EGLBufferReader,
|
|
|
|
renderer::{Renderer, Texture, Transform},
|
|
|
|
SwapBuffersError,
|
|
|
|
},
|
2021-05-15 16:17:43 +00:00
|
|
|
reexports::{
|
|
|
|
calloop::LoopHandle,
|
|
|
|
wayland_server::protocol::{wl_buffer, wl_surface},
|
|
|
|
},
|
2021-04-06 23:15:03 +00:00
|
|
|
utils::Rectangle,
|
|
|
|
wayland::{
|
|
|
|
compositor::{roles::Role, SubsurfaceRole, TraversalAction},
|
|
|
|
data_device::DnDIconRole,
|
|
|
|
seat::CursorImageRole,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
use crate::shell::{MyCompositorToken, MyWindowMap, SurfaceData};
|
2021-04-25 21:38:48 +00:00
|
|
|
|
2021-05-13 17:59:47 +00:00
|
|
|
struct BufferTextures<T> {
|
2021-05-16 18:08:10 +00:00
|
|
|
buffer: Option<wl_buffer::WlBuffer>,
|
2021-05-13 17:59:47 +00:00
|
|
|
texture: T,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Drop for BufferTextures<T> {
|
|
|
|
fn drop(&mut self) {
|
2021-05-16 18:08:10 +00:00
|
|
|
if let Some(buffer) = self.buffer.take() {
|
|
|
|
buffer.release();
|
|
|
|
}
|
2021-05-13 17:59:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-25 21:38:48 +00:00
|
|
|
pub fn draw_cursor<R, E, T>(
|
2021-04-06 23:15:03 +00:00
|
|
|
renderer: &mut R,
|
|
|
|
surface: &wl_surface::WlSurface,
|
2021-05-13 17:59:47 +00:00
|
|
|
egl_buffer_reader: Option<&EGLBufferReader>,
|
2021-04-06 23:15:03 +00:00
|
|
|
(x, y): (i32, i32),
|
|
|
|
token: MyCompositorToken,
|
|
|
|
log: &Logger,
|
2021-04-28 22:02:17 +00:00
|
|
|
) -> Result<(), SwapBuffersError>
|
2021-04-28 22:32:47 +00:00
|
|
|
where
|
|
|
|
R: Renderer<Error = E, TextureId = T>,
|
|
|
|
E: std::error::Error + Into<SwapBuffersError>,
|
|
|
|
T: Texture + 'static,
|
2021-04-06 23:15:03 +00:00
|
|
|
{
|
|
|
|
let (dx, dy) = match token.with_role_data::<CursorImageRole, _, _>(surface, |data| data.hotspot) {
|
|
|
|
Ok(h) => h,
|
|
|
|
Err(_) => {
|
|
|
|
warn!(
|
|
|
|
log,
|
|
|
|
"Trying to display as a cursor a surface that does not have the CursorImage role."
|
|
|
|
);
|
|
|
|
(0, 0)
|
|
|
|
}
|
|
|
|
};
|
2021-05-15 16:17:43 +00:00
|
|
|
draw_surface_tree(renderer, surface, egl_buffer_reader, (x - dx, y - dy), token, log)
|
2021-04-06 23:15:03 +00:00
|
|
|
}
|
|
|
|
|
2021-04-25 21:38:48 +00:00
|
|
|
fn draw_surface_tree<R, E, T>(
|
2021-04-06 23:15:03 +00:00
|
|
|
renderer: &mut R,
|
|
|
|
root: &wl_surface::WlSurface,
|
2021-05-13 17:59:47 +00:00
|
|
|
egl_buffer_reader: Option<&EGLBufferReader>,
|
2021-04-06 23:15:03 +00:00
|
|
|
location: (i32, i32),
|
|
|
|
compositor_token: MyCompositorToken,
|
|
|
|
log: &Logger,
|
2021-04-28 22:02:17 +00:00
|
|
|
) -> Result<(), SwapBuffersError>
|
2021-04-28 22:32:47 +00:00
|
|
|
where
|
|
|
|
R: Renderer<Error = E, TextureId = T>,
|
|
|
|
E: std::error::Error + Into<SwapBuffersError>,
|
|
|
|
T: Texture + 'static,
|
2021-04-06 23:15:03 +00:00
|
|
|
{
|
2021-04-28 22:02:17 +00:00
|
|
|
let mut result = Ok(());
|
|
|
|
|
2021-04-06 23:15:03 +00:00
|
|
|
compositor_token.with_surface_tree_upward(
|
|
|
|
root,
|
2021-04-25 21:38:48 +00:00
|
|
|
(),
|
2021-04-26 19:44:40 +00:00
|
|
|
|_surface, attributes, _, _| {
|
2021-04-06 23:15:03 +00:00
|
|
|
// Pull a new buffer if available
|
|
|
|
if let Some(data) = attributes.user_data.get::<RefCell<SurfaceData>>() {
|
|
|
|
let mut data = data.borrow_mut();
|
|
|
|
if data.texture.is_none() {
|
|
|
|
if let Some(buffer) = data.current_state.buffer.take() {
|
2021-05-16 18:00:45 +00:00
|
|
|
match renderer.import_buffer(&buffer, Some(&attributes), egl_buffer_reader) {
|
2021-05-15 16:17:43 +00:00
|
|
|
Ok(m) => {
|
2021-05-16 18:08:10 +00:00
|
|
|
let buffer = if smithay::wayland::shm::with_buffer_contents(&buffer, |_,_| ()).is_ok() {
|
|
|
|
buffer.release();
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(buffer)
|
|
|
|
};
|
2021-05-15 16:17:43 +00:00
|
|
|
data.texture = Some(Box::new(BufferTextures { buffer, texture: m })
|
|
|
|
as Box<dyn std::any::Any + 'static>)
|
|
|
|
}
|
2021-04-06 23:15:03 +00:00
|
|
|
// there was an error reading the buffer, release it, we
|
|
|
|
// already logged the error
|
|
|
|
Err(err) => {
|
2021-04-26 19:44:40 +00:00
|
|
|
warn!(log, "Error loading buffer: {:?}", err);
|
2021-05-13 17:59:47 +00:00
|
|
|
buffer.release();
|
2021-04-28 22:32:47 +00:00
|
|
|
}
|
2021-04-06 23:15:03 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Now, should we be drawn ?
|
2021-04-25 21:38:48 +00:00
|
|
|
if data.texture.is_some() {
|
|
|
|
TraversalAction::DoChildren(())
|
|
|
|
} else {
|
|
|
|
// we are not displayed, so our children are neither
|
|
|
|
TraversalAction::SkipChildren
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// we are not displayed, so our children are neither
|
|
|
|
TraversalAction::SkipChildren
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|_, _, _, _| {},
|
|
|
|
|_, _, _, _| true,
|
|
|
|
);
|
|
|
|
|
|
|
|
compositor_token.with_surface_tree_upward(
|
|
|
|
root,
|
|
|
|
location,
|
|
|
|
|_surface, attributes, role, &(mut x, mut y)| {
|
|
|
|
// Pull a new buffer if available
|
|
|
|
if let Some(data) = attributes.user_data.get::<RefCell<SurfaceData>>() {
|
2021-04-26 19:44:40 +00:00
|
|
|
let data = data.borrow();
|
2021-04-25 21:38:48 +00:00
|
|
|
// Now, should we be drawn ?
|
2021-04-06 23:15:03 +00:00
|
|
|
if data.texture.is_some() {
|
|
|
|
// if yes, also process the children
|
|
|
|
if Role::<SubsurfaceRole>::has(role) {
|
|
|
|
x += data.current_state.sub_location.0;
|
|
|
|
y += data.current_state.sub_location.1;
|
|
|
|
}
|
|
|
|
TraversalAction::DoChildren((x, y))
|
|
|
|
} else {
|
|
|
|
// we are not displayed, so our children are neither
|
|
|
|
TraversalAction::SkipChildren
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// we are not displayed, so our children are neither
|
|
|
|
TraversalAction::SkipChildren
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|_surface, attributes, role, &(mut x, mut y)| {
|
|
|
|
if let Some(ref data) = attributes.user_data.get::<RefCell<SurfaceData>>() {
|
|
|
|
let mut data = data.borrow_mut();
|
|
|
|
let (sub_x, sub_y) = data.current_state.sub_location;
|
2021-05-13 17:59:47 +00:00
|
|
|
if let Some(texture) = data
|
2021-04-28 22:32:47 +00:00
|
|
|
.texture
|
|
|
|
.as_mut()
|
|
|
|
.and_then(|x| x.downcast_mut::<BufferTextures<T>>())
|
|
|
|
{
|
2021-04-06 23:15:03 +00:00
|
|
|
// we need to re-extract the subsurface offset, as the previous closure
|
|
|
|
// only passes it to our children
|
|
|
|
if Role::<SubsurfaceRole>::has(role) {
|
|
|
|
x += sub_x;
|
|
|
|
y += sub_y;
|
|
|
|
}
|
2021-05-15 16:17:43 +00:00
|
|
|
if let Err(err) = renderer.render_texture_at(
|
|
|
|
&texture.texture,
|
|
|
|
(x, y),
|
|
|
|
Transform::Normal, /* TODO */
|
|
|
|
1.0,
|
|
|
|
) {
|
2021-04-28 22:02:17 +00:00
|
|
|
result = Err(err.into());
|
|
|
|
}
|
2021-04-06 23:15:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|_, _, _, _| true,
|
|
|
|
);
|
2021-04-28 22:02:17 +00:00
|
|
|
|
|
|
|
result
|
2021-04-06 23:15:03 +00:00
|
|
|
}
|
|
|
|
|
2021-04-25 21:38:48 +00:00
|
|
|
pub fn draw_windows<R, E, T>(
|
2021-04-06 23:15:03 +00:00
|
|
|
renderer: &mut R,
|
2021-05-13 17:59:47 +00:00
|
|
|
egl_buffer_reader: Option<&EGLBufferReader>,
|
2021-04-06 23:15:03 +00:00
|
|
|
window_map: &MyWindowMap,
|
|
|
|
output_rect: Option<Rectangle>,
|
|
|
|
compositor_token: MyCompositorToken,
|
|
|
|
log: &::slog::Logger,
|
2021-04-28 22:02:17 +00:00
|
|
|
) -> Result<(), SwapBuffersError>
|
2021-04-28 22:32:47 +00:00
|
|
|
where
|
|
|
|
R: Renderer<Error = E, TextureId = T>,
|
|
|
|
E: std::error::Error + Into<SwapBuffersError>,
|
|
|
|
T: Texture + 'static,
|
2021-04-06 23:15:03 +00:00
|
|
|
{
|
2021-04-28 22:02:17 +00:00
|
|
|
let mut result = Ok(());
|
|
|
|
|
2021-04-06 23:15:03 +00:00
|
|
|
// redraw the frame, in a simple but inneficient way
|
2021-04-28 22:32:47 +00:00
|
|
|
window_map.with_windows_from_bottom_to_top(|toplevel_surface, mut initial_place, bounding_box| {
|
|
|
|
// skip windows that do not overlap with a given output
|
|
|
|
if let Some(output) = output_rect {
|
|
|
|
if !output.overlaps(bounding_box) {
|
|
|
|
return;
|
2021-04-28 22:02:17 +00:00
|
|
|
}
|
2021-04-28 22:32:47 +00:00
|
|
|
initial_place.0 -= output.x;
|
|
|
|
}
|
|
|
|
if let Some(wl_surface) = toplevel_surface.get_surface() {
|
|
|
|
// this surface is a root of a subsurface tree that needs to be drawn
|
|
|
|
if let Err(err) = draw_surface_tree(
|
|
|
|
renderer,
|
|
|
|
&wl_surface,
|
2021-05-13 17:59:47 +00:00
|
|
|
egl_buffer_reader,
|
2021-04-28 22:32:47 +00:00
|
|
|
initial_place,
|
|
|
|
compositor_token,
|
|
|
|
log,
|
|
|
|
) {
|
|
|
|
result = Err(err);
|
2021-04-28 22:02:17 +00:00
|
|
|
}
|
2021-04-28 22:32:47 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-04-28 22:02:17 +00:00
|
|
|
result
|
2021-04-06 23:15:03 +00:00
|
|
|
}
|
|
|
|
|
2021-04-25 21:38:48 +00:00
|
|
|
pub fn draw_dnd_icon<R, E, T>(
|
2021-04-06 23:15:03 +00:00
|
|
|
renderer: &mut R,
|
|
|
|
surface: &wl_surface::WlSurface,
|
2021-05-13 17:59:47 +00:00
|
|
|
egl_buffer_reader: Option<&EGLBufferReader>,
|
2021-04-06 23:15:03 +00:00
|
|
|
(x, y): (i32, i32),
|
|
|
|
token: MyCompositorToken,
|
|
|
|
log: &::slog::Logger,
|
2021-04-28 22:02:17 +00:00
|
|
|
) -> Result<(), SwapBuffersError>
|
2021-04-28 22:32:47 +00:00
|
|
|
where
|
|
|
|
R: Renderer<Error = E, TextureId = T>,
|
|
|
|
E: std::error::Error + Into<SwapBuffersError>,
|
|
|
|
T: Texture + 'static,
|
2021-04-06 23:15:03 +00:00
|
|
|
{
|
|
|
|
if !token.has_role::<DnDIconRole>(surface) {
|
|
|
|
warn!(
|
|
|
|
log,
|
|
|
|
"Trying to display as a dnd icon a surface that does not have the DndIcon role."
|
|
|
|
);
|
|
|
|
}
|
2021-05-15 16:17:43 +00:00
|
|
|
draw_surface_tree(renderer, surface, egl_buffer_reader, (x, y), token, log)
|
2021-04-06 23:15:03 +00:00
|
|
|
}
|
|
|
|
|
2021-04-25 21:44:02 +00:00
|
|
|
pub fn schedule_initial_render<R: Renderer + 'static, Data: 'static>(
|
|
|
|
renderer: Rc<RefCell<R>>,
|
2021-04-06 23:15:03 +00:00
|
|
|
evt_handle: &LoopHandle<Data>,
|
2021-04-25 21:44:02 +00:00
|
|
|
logger: ::slog::Logger,
|
2021-04-28 22:32:47 +00:00
|
|
|
) where
|
|
|
|
<R as Renderer>::Error: Into<SwapBuffersError>,
|
2021-04-25 21:44:02 +00:00
|
|
|
{
|
|
|
|
let result = {
|
|
|
|
let mut renderer = renderer.borrow_mut();
|
|
|
|
// Does not matter if we render an empty frame
|
2021-04-28 22:32:47 +00:00
|
|
|
renderer
|
|
|
|
.begin(1, 1, Transform::Normal)
|
|
|
|
.map_err(Into::<SwapBuffersError>::into)
|
|
|
|
.and_then(|_| {
|
|
|
|
renderer
|
|
|
|
.clear([0.8, 0.8, 0.9, 1.0])
|
|
|
|
.map_err(Into::<SwapBuffersError>::into)
|
|
|
|
})
|
|
|
|
.and_then(|_| renderer.finish())
|
2021-04-25 21:44:02 +00:00
|
|
|
};
|
|
|
|
if let Err(err) = result {
|
2021-04-06 23:15:03 +00:00
|
|
|
match err {
|
|
|
|
SwapBuffersError::AlreadySwapped => {}
|
|
|
|
SwapBuffersError::TemporaryFailure(err) => {
|
|
|
|
// TODO dont reschedule after 3(?) retries
|
2021-04-25 21:44:02 +00:00
|
|
|
warn!(logger, "Failed to submit page_flip: {}", err);
|
2021-04-06 23:15:03 +00:00
|
|
|
let handle = evt_handle.clone();
|
2021-04-25 21:44:02 +00:00
|
|
|
evt_handle.insert_idle(move |_| schedule_initial_render(renderer, &handle, logger));
|
2021-04-06 23:15:03 +00:00
|
|
|
}
|
|
|
|
SwapBuffersError::ContextLost(err) => panic!("Rendering loop lost: {}", err),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|