Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cosmic-screencopy-v2 #358

Merged
merged 1 commit into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

385 changes: 238 additions & 147 deletions src/backend/kms/mod.rs

Large diffs are not rendered by default.

48 changes: 30 additions & 18 deletions src/backend/render/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ use smithay::{
},
reexports::wayland_server::protocol::wl_surface,
render_elements,
utils::{IsAlive, Logical, Monotonic, Point, Scale, Time, Transform},
utils::{
Buffer as BufferCoords, IsAlive, Logical, Monotonic, Point, Scale, Size, Time, Transform,
},
wayland::compositor::{get_role, with_states},
};
use std::{cell::RefCell, collections::HashMap, io::Read, sync::Mutex, time::Duration};
Expand Down Expand Up @@ -156,12 +158,12 @@ pub fn draw_surface_cursor<R>(
surface: &wl_surface::WlSurface,
location: impl Into<Point<i32, Logical>>,
scale: impl Into<Scale<f64>>,
) -> Vec<CursorRenderElement<R>>
) -> Vec<(CursorRenderElement<R>, Point<i32, BufferCoords>)>
where
R: Renderer + ImportAll,
<R as Renderer>::TextureId: 'static,
{
let mut position = location.into();
let position = location.into();
let scale = scale.into();
let h = with_states(&surface, |states| {
states
Expand All @@ -171,8 +173,12 @@ where
.lock()
.unwrap()
.hotspot
.to_buffer(
1,
Transform::Normal,
&Size::from((1, 1)), /* Size doesn't matter for Transform::Normal */
)
});
position -= h;

render_elements_from_surface_tree(
renderer,
Expand All @@ -182,6 +188,9 @@ where
1.0,
Kind::Cursor,
)
.into_iter()
.map(|elem| (elem, h))
.collect()
}

#[profiling::function]
Expand All @@ -190,7 +199,7 @@ pub fn draw_dnd_icon<R>(
surface: &wl_surface::WlSurface,
location: impl Into<Point<i32, Logical>>,
scale: impl Into<Scale<f64>>,
) -> Vec<CursorRenderElement<R>>
) -> Vec<WaylandSurfaceRenderElement<R>>
where
R: Renderer + ImportAll,
<R as Renderer>::TextureId: 'static,
Expand Down Expand Up @@ -307,7 +316,7 @@ pub fn draw_cursor<R>(
scale: Scale<f64>,
time: Time<Monotonic>,
draw_default: bool,
) -> Vec<CursorRenderElement<R>>
) -> Vec<(CursorRenderElement<R>, Point<i32, BufferCoords>)>
where
R: Renderer + ImportMem + ImportAll,
<R as Renderer>::TextureId: Clone + 'static,
Expand Down Expand Up @@ -367,20 +376,23 @@ where
}
};

let hotspot = Point::<i32, Logical>::from((frame.xhot as i32, frame.yhot as i32)).to_f64();
let hotspot = Point::<i32, BufferCoords>::from((frame.xhot as i32, frame.yhot as i32));
*state.current_image.borrow_mut() = Some(frame);

return vec![CursorRenderElement::Static(
MemoryRenderBufferRenderElement::from_buffer(
renderer,
(location - hotspot).to_physical(scale),
pointer_image,
None,
None,
None,
Kind::Cursor,
)
.expect("Failed to import cursor bitmap"),
return vec![(
CursorRenderElement::Static(
MemoryRenderBufferRenderElement::from_buffer(
renderer,
location.to_physical(scale),
pointer_image,
None,
None,
None,
Kind::Cursor,
)
.expect("Failed to import cursor bitmap"),
),
hotspot,
)];
} else {
Vec::new()
Expand Down
113 changes: 101 additions & 12 deletions src/backend/render/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ use crate::shell::{CosmicMappedRenderElement, WorkspaceRenderElement};
use smithay::{
backend::renderer::{
element::{
surface::WaylandSurfaceRenderElement,
utils::{Relocate, RelocateRenderElement},
Element, RenderElement, UnderlyingStorage,
Element, Id, RenderElement, UnderlyingStorage,
},
glow::{GlowFrame, GlowRenderer},
utils::CommitCounter,
Frame, ImportAll, ImportMem, Renderer,
},
utils::{Buffer as BufferCoords, Physical, Point, Rectangle, Scale},
utils::{Buffer as BufferCoords, Logical, Physical, Point, Rectangle, Scale},
};

#[cfg(feature = "debug")]
Expand All @@ -24,8 +26,10 @@ where
CosmicMappedRenderElement<R>: RenderElement<R>,
{
Workspace(RelocateRenderElement<WorkspaceRenderElement<R>>),
Cursor(CursorRenderElement<R>),
Cursor(RelocateRenderElement<CursorRenderElement<R>>),
Dnd(WaylandSurfaceRenderElement<R>),
MoveGrab(CosmicMappedRenderElement<R>),
AdditionalDamage(DamageElement),
#[cfg(feature = "debug")]
Egui(TextureRenderElement<GlesTexture>),
}
Expand All @@ -36,21 +40,25 @@ where
<R as Renderer>::TextureId: 'static,
CosmicMappedRenderElement<R>: RenderElement<R>,
{
fn id(&self) -> &smithay::backend::renderer::element::Id {
fn id(&self) -> &Id {
match self {
CosmicElement::Workspace(elem) => elem.id(),
CosmicElement::Cursor(elem) => elem.id(),
CosmicElement::Dnd(elem) => elem.id(),
CosmicElement::MoveGrab(elem) => elem.id(),
CosmicElement::AdditionalDamage(elem) => elem.id(),
#[cfg(feature = "debug")]
CosmicElement::Egui(elem) => elem.id(),
}
}

fn current_commit(&self) -> smithay::backend::renderer::utils::CommitCounter {
fn current_commit(&self) -> CommitCounter {
match self {
CosmicElement::Workspace(elem) => elem.current_commit(),
CosmicElement::Cursor(elem) => elem.current_commit(),
CosmicElement::Dnd(elem) => elem.current_commit(),
CosmicElement::MoveGrab(elem) => elem.current_commit(),
CosmicElement::AdditionalDamage(elem) => elem.current_commit(),
#[cfg(feature = "debug")]
CosmicElement::Egui(elem) => elem.current_commit(),
}
Expand All @@ -60,7 +68,9 @@ where
match self {
CosmicElement::Workspace(elem) => elem.src(),
CosmicElement::Cursor(elem) => elem.src(),
CosmicElement::Dnd(elem) => elem.src(),
CosmicElement::MoveGrab(elem) => elem.src(),
CosmicElement::AdditionalDamage(elem) => elem.src(),
#[cfg(feature = "debug")]
CosmicElement::Egui(elem) => elem.src(),
}
Expand All @@ -70,7 +80,9 @@ where
match self {
CosmicElement::Workspace(elem) => elem.geometry(scale),
CosmicElement::Cursor(elem) => elem.geometry(scale),
CosmicElement::Dnd(elem) => elem.geometry(scale),
CosmicElement::MoveGrab(elem) => elem.geometry(scale),
CosmicElement::AdditionalDamage(elem) => elem.geometry(scale),
#[cfg(feature = "debug")]
CosmicElement::Egui(elem) => elem.geometry(scale),
}
Expand All @@ -80,7 +92,9 @@ where
match self {
CosmicElement::Workspace(elem) => elem.location(scale),
CosmicElement::Cursor(elem) => elem.location(scale),
CosmicElement::Dnd(elem) => elem.location(scale),
CosmicElement::MoveGrab(elem) => elem.location(scale),
CosmicElement::AdditionalDamage(elem) => elem.location(scale),
#[cfg(feature = "debug")]
CosmicElement::Egui(elem) => elem.location(scale),
}
Expand All @@ -90,7 +104,9 @@ where
match self {
CosmicElement::Workspace(elem) => elem.transform(),
CosmicElement::Cursor(elem) => elem.transform(),
CosmicElement::Dnd(elem) => elem.transform(),
CosmicElement::MoveGrab(elem) => elem.transform(),
CosmicElement::AdditionalDamage(elem) => elem.transform(),
#[cfg(feature = "debug")]
CosmicElement::Egui(elem) => elem.transform(),
}
Expand All @@ -99,12 +115,14 @@ where
fn damage_since(
&self,
scale: Scale<f64>,
commit: Option<smithay::backend::renderer::utils::CommitCounter>,
commit: Option<CommitCounter>,
) -> Vec<Rectangle<i32, Physical>> {
match self {
CosmicElement::Workspace(elem) => elem.damage_since(scale, commit),
CosmicElement::Cursor(elem) => elem.damage_since(scale, commit),
CosmicElement::Dnd(elem) => elem.damage_since(scale, commit),
CosmicElement::MoveGrab(elem) => elem.damage_since(scale, commit),
CosmicElement::AdditionalDamage(elem) => elem.damage_since(scale, commit),
#[cfg(feature = "debug")]
CosmicElement::Egui(elem) => elem.damage_since(scale, commit),
}
Expand All @@ -114,7 +132,9 @@ where
match self {
CosmicElement::Workspace(elem) => elem.opaque_regions(scale),
CosmicElement::Cursor(elem) => elem.opaque_regions(scale),
CosmicElement::Dnd(elem) => elem.opaque_regions(scale),
CosmicElement::MoveGrab(elem) => elem.opaque_regions(scale),
CosmicElement::AdditionalDamage(elem) => elem.opaque_regions(scale),
#[cfg(feature = "debug")]
CosmicElement::Egui(elem) => elem.opaque_regions(scale),
}
Expand All @@ -124,7 +144,9 @@ where
match self {
CosmicElement::Workspace(elem) => elem.alpha(),
CosmicElement::Cursor(elem) => elem.alpha(),
CosmicElement::Dnd(elem) => elem.alpha(),
CosmicElement::MoveGrab(elem) => elem.alpha(),
CosmicElement::AdditionalDamage(elem) => elem.alpha(),
#[cfg(feature = "debug")]
CosmicElement::Egui(elem) => elem.alpha(),
}
Expand All @@ -142,7 +164,11 @@ impl RenderElement<GlowRenderer> for CosmicElement<GlowRenderer> {
match self {
CosmicElement::Workspace(elem) => elem.draw(frame, src, dst, damage),
CosmicElement::Cursor(elem) => elem.draw(frame, src, dst, damage),
CosmicElement::Dnd(elem) => elem.draw(frame, src, dst, damage),
CosmicElement::MoveGrab(elem) => elem.draw(frame, src, dst, damage),
CosmicElement::AdditionalDamage(elem) => {
RenderElement::<GlowRenderer>::draw(elem, frame, src, dst, damage)
}
#[cfg(feature = "debug")]
CosmicElement::Egui(elem) => {
RenderElement::<GlowRenderer>::draw(elem, frame, src, dst, damage)
Expand All @@ -154,7 +180,9 @@ impl RenderElement<GlowRenderer> for CosmicElement<GlowRenderer> {
match self {
CosmicElement::Workspace(elem) => elem.underlying_storage(renderer),
CosmicElement::Cursor(elem) => elem.underlying_storage(renderer),
CosmicElement::Dnd(elem) => elem.underlying_storage(renderer),
CosmicElement::MoveGrab(elem) => elem.underlying_storage(renderer),
CosmicElement::AdditionalDamage(elem) => elem.underlying_storage(renderer),
#[cfg(feature = "debug")]
CosmicElement::Egui(elem) => elem.underlying_storage(renderer),
}
Expand All @@ -172,7 +200,11 @@ impl<'a> RenderElement<GlMultiRenderer<'a>> for CosmicElement<GlMultiRenderer<'a
match self {
CosmicElement::Workspace(elem) => elem.draw(frame, src, dst, damage),
CosmicElement::Cursor(elem) => elem.draw(frame, src, dst, damage),
CosmicElement::Dnd(elem) => elem.draw(frame, src, dst, damage),
CosmicElement::MoveGrab(elem) => elem.draw(frame, src, dst, damage),
CosmicElement::AdditionalDamage(elem) => {
RenderElement::<GlMultiRenderer<'a>>::draw(elem, frame, src, dst, damage)
}
#[cfg(feature = "debug")]
CosmicElement::Egui(elem) => {
let elem = {
Expand All @@ -189,7 +221,9 @@ impl<'a> RenderElement<GlMultiRenderer<'a>> for CosmicElement<GlMultiRenderer<'a
match self {
CosmicElement::Workspace(elem) => elem.underlying_storage(renderer),
CosmicElement::Cursor(elem) => elem.underlying_storage(renderer),
CosmicElement::Dnd(elem) => elem.underlying_storage(renderer),
CosmicElement::MoveGrab(elem) => elem.underlying_storage(renderer),
CosmicElement::AdditionalDamage(elem) => elem.underlying_storage(renderer),
#[cfg(feature = "debug")]
CosmicElement::Egui(elem) => {
let glow_renderer = renderer.glow_renderer_mut();
Expand Down Expand Up @@ -219,25 +253,25 @@ where
}
}

impl<R> From<CursorRenderElement<R>> for CosmicElement<R>
impl<R> From<CosmicMappedRenderElement<R>> for CosmicElement<R>
where
R: Renderer + ImportAll + ImportMem + AsGlowRenderer,
<R as Renderer>::TextureId: 'static,
CosmicMappedRenderElement<R>: RenderElement<R>,
{
fn from(elem: CursorRenderElement<R>) -> Self {
Self::Cursor(elem)
fn from(elem: CosmicMappedRenderElement<R>) -> Self {
Self::MoveGrab(elem)
}
}

impl<R> From<CosmicMappedRenderElement<R>> for CosmicElement<R>
impl<R> From<DamageElement> for CosmicElement<R>
where
R: Renderer + ImportAll + ImportMem + AsGlowRenderer,
<R as Renderer>::TextureId: 'static,
CosmicMappedRenderElement<R>: RenderElement<R>,
{
fn from(elem: CosmicMappedRenderElement<R>) -> Self {
Self::MoveGrab(elem)
fn from(elem: DamageElement) -> Self {
Self::AdditionalDamage(elem)
}
}

Expand Down Expand Up @@ -304,3 +338,58 @@ impl<'renderer, 'frame> AsGlowFrame<'frame> for GlMultiFrame<'renderer, 'frame>
self.as_mut()
}
}

pub struct DamageElement {
id: Id,
geometry: Rectangle<i32, Logical>,
}

impl DamageElement {
pub fn new(geometry: Rectangle<i32, Logical>) -> DamageElement {
DamageElement {
id: Id::new(),
geometry,
}
}
}

impl Element for DamageElement {
fn id(&self) -> &Id {
&self.id
}

fn current_commit(&self) -> CommitCounter {
CommitCounter::default()
}

fn src(&self) -> Rectangle<f64, BufferCoords> {
Rectangle::from_loc_and_size((0.0, 0.0), (1.0, 1.0))
}

fn geometry(&self, scale: Scale<f64>) -> Rectangle<i32, Physical> {
self.geometry.to_f64().to_physical(scale).to_i32_round()
}

fn damage_since(
&self,
scale: Scale<f64>,
_commit: Option<CommitCounter>,
) -> Vec<Rectangle<i32, Physical>> {
vec![Rectangle::from_loc_and_size(
(0, 0),
self.geometry(scale).size,
)]
}
}

impl<R: Renderer> RenderElement<R> for DamageElement {
fn draw(
&self,
_frame: &mut <R as Renderer>::Frame<'_>,
_src: Rectangle<f64, BufferCoords>,
_dst: Rectangle<i32, Physical>,
_damage: &[Rectangle<i32, Physical>],
) -> Result<(), <R as Renderer>::Error> {
Ok(())
}
}
Loading