From 5b2eb5855a4fad03412986747c2dee4f775f9cc8 Mon Sep 17 00:00:00 2001 From: Julian Orth Date: Sat, 12 Nov 2022 15:05:58 +0100 Subject: [PATCH] tree: update to latest version of wp_fractional_scale --- src/compositor.rs | 6 +-- src/config/handler.rs | 4 +- src/cursor.rs | 25 +++++------ src/fixed.rs | 4 -- src/ifs/wl_output.rs | 4 +- src/ifs/wl_surface.rs | 2 +- src/ifs/wl_surface/cursor.rs | 3 +- src/ifs/wl_surface/wp_fractional_scale_v1.rs | 2 +- src/main.rs | 1 + src/portal/ptr_gui.rs | 8 ++-- src/render/renderer/framebuffer.rs | 9 ++-- src/render/renderer/renderer.rs | 4 +- src/render/renderer/renderer_base.rs | 8 ++-- src/scale.rs | 44 ++++++++++++++++++++ src/screenshoter.rs | 4 +- src/state.rs | 8 ++-- src/tasks/connector.rs | 4 +- src/tree/container.rs | 3 +- src/tree/float.rs | 3 +- src/tree/output.rs | 7 ++-- src/tree/placeholder.rs | 3 +- wire/wp_fractional_scale_v1.txt | 2 +- 22 files changed, 104 insertions(+), 54 deletions(-) create mode 100644 src/scale.rs diff --git a/src/compositor.rs b/src/compositor.rs index 294e10f9..fd0a4da8 100644 --- a/src/compositor.rs +++ b/src/compositor.rs @@ -14,7 +14,6 @@ use { clientmem::{self, ClientMemError}, config::ConfigProxy, dbus::Dbus, - fixed::Fixed, forker, globals::Globals, ifs::{wl_output::WlOutputGlobal, wl_surface::NoneSurfaceExt}, @@ -22,6 +21,7 @@ use { leaks, logger::Logger, render::{self, RenderError}, + scale::Scale, sighand::{self, SighandError}, state::{ConnectorData, IdleState, ScreenlockState, State, XWaylandState}, tasks::{self, idle}, @@ -123,7 +123,7 @@ fn start_compositor2( let (_run_toplevel_future, run_toplevel) = RunToplevel::install(&engine); let node_ids = NodeIds::default(); let scales = RefCounted::default(); - scales.add(Fixed::from_int(1)); + scales.add(Scale::from_int(1)); let state = Rc::new(State { xkb_ctx, backend: CloneCell::new(Rc::new(DummyBackend)), @@ -389,7 +389,7 @@ fn create_dummy_output(state: &Rc) { scroll: Default::default(), pointer_positions: Default::default(), lock_surface: Default::default(), - preferred_scale: Cell::new(Fixed::from_int(1)), + preferred_scale: Cell::new(Scale::from_int(1)), hardware_cursor: Default::default(), update_render_data_scheduled: Cell::new(false), screencasts: Default::default(), diff --git a/src/config/handler.rs b/src/config/handler.rs index 159fec80..11d7590b 100644 --- a/src/config/handler.rs +++ b/src/config/handler.rs @@ -7,8 +7,8 @@ use { }, compositor::MAX_EXTENTS, config::ConfigProxy, - fixed::Fixed, ifs::wl_seat::{SeatId, WlSeatGlobal}, + scale::Scale, state::{ConnectorData, DeviceHandlerData, DrmDevData, OutputData, State}, theme::{Color, ThemeSized, DEFAULT_FONT}, tree::{ContainerNode, ContainerSplit, FloatNode, Node, NodeVisitorBase, OutputNode}, @@ -654,7 +654,7 @@ impl ConfigProxyHandler { if scale > 1000.0 { return Err(CphError::ScaleTooLarge(scale)); } - let scale = Fixed::from_f64(scale); + let scale = Scale::from_f64(scale); let connector = self.get_output(connector)?; connector.node.set_preferred_scale(scale); self.state.damage(); diff --git a/src/cursor.rs b/src/cursor.rs index c4bb76be..aa1dbe9e 100644 --- a/src/cursor.rs +++ b/src/cursor.rs @@ -4,6 +4,7 @@ use { format::ARGB8888, rect::Rect, render::{RenderContext, RenderError, Renderer, Texture}, + scale::Scale, state::State, time::Time, tree::OutputNode, @@ -41,7 +42,7 @@ const HEADER_SIZE: u32 = 16; pub trait Cursor { fn render(&self, renderer: &mut Renderer, x: Fixed, y: Fixed); fn render_hardware_cursor(&self, renderer: &mut Renderer); - fn extents_at_scale(&self, scale: Fixed) -> Rect; + fn extents_at_scale(&self, scale: Scale) -> Rect; fn set_output(&self, output: &Rc) { let _ = output; } @@ -113,7 +114,7 @@ impl ServerCursors { pub struct ServerCursorTemplate { var: ServerCursorTemplateVariant, - pub xcursor: Vec>>, + pub xcursor: Vec>>, } enum ServerCursorTemplateVariant { @@ -125,7 +126,7 @@ impl ServerCursorTemplate { fn load( name: &str, theme: Option<&BStr>, - scales: &[Fixed], + scales: &[Scale], sizes: &[u32], paths: &[BString], ctx: &Rc, @@ -213,12 +214,12 @@ struct CursorImageScaled { struct CursorImage { delay_ns: u64, - sizes: SmallMapMut<(Fixed, u32), Rc, 2>, + sizes: SmallMapMut<(Scale, u32), Rc, 2>, } struct InstantiatedCursorImage { delay_ns: u64, - scales: SmallMapMut, 2>, + scales: SmallMapMut, 2>, } impl CursorImageScaled { @@ -240,7 +241,7 @@ impl CursorImageScaled { impl CursorImage { fn from_sizes( delay_ms: u64, - sizes: SmallMapMut<(Fixed, u32), Rc, 2>, + sizes: SmallMapMut<(Scale, u32), Rc, 2>, ) -> Result { Ok(Self { delay_ns: delay_ms.max(1) * 1_000_000, @@ -306,7 +307,7 @@ impl Cursor for StaticCursor { } } - fn extents_at_scale(&self, scale: Fixed) -> Rect { + fn extents_at_scale(&self, scale: Scale) -> Rect { match self.image.scales.get(&scale) { None => Rect::new_empty(0, 0), Some(i) => i.extents, @@ -336,7 +337,7 @@ impl Cursor for AnimatedCursor { } } - fn extents_at_scale(&self, scale: Fixed) -> Rect { + fn extents_at_scale(&self, scale: Scale) -> Rect { let img = &self.images[self.idx.get()]; match img.scales.get(&scale) { None => Rect::new_empty(0, 0), @@ -368,13 +369,13 @@ impl Cursor for AnimatedCursor { } struct OpenCursorResult { - images: Vec>>, + images: Vec>>, } fn open_cursor( name: &str, theme: Option<&BStr>, - scales: &[Fixed], + scales: &[Scale], sizes: &[u32], paths: &[BString], ) -> Result { @@ -538,7 +539,7 @@ impl Debug for XCursorImage { fn parser_cursor_file( r: &mut R, - scales: &[Fixed], + scales: &[Scale], sizes: &[u32], ) -> Result { let [magic, header] = read_u32_n(r)?; @@ -554,7 +555,7 @@ fn parser_cursor_file( positions: Vec, effective_size: u32, size: u32, - scale: Fixed, + scale: Scale, best_fit: i64, } let mut targets = Vec::new(); diff --git a/src/fixed.rs b/src/fixed.rs index e621f68c..0b32769b 100644 --- a/src/fixed.rs +++ b/src/fixed.rs @@ -39,10 +39,6 @@ impl Fixed { self.0 >> 8 } - pub fn round_up(self) -> i32 { - (self.0 + 255) >> 8 - } - pub fn apply_fract(self, i: i32) -> Self { Self((i << 8) | (self.0 & 255)) } diff --git a/src/ifs/wl_output.rs b/src/ifs/wl_output.rs index 66163441..65373aaf 100644 --- a/src/ifs/wl_output.rs +++ b/src/ifs/wl_output.rs @@ -72,7 +72,7 @@ pub struct WlOutputGlobal { pub unused_captures: LinkedList>, pub pending_captures: LinkedList>, pub destroyed: Cell, - pub legacy_scale: Cell, + pub legacy_scale: Cell, } #[derive(Eq, PartialEq)] @@ -316,7 +316,7 @@ impl WlOutput { fn send_scale(self: &Rc) { let event = Scale { self_id: self.id, - factor: self.global.legacy_scale.get(), + factor: self.global.legacy_scale.get() as _, }; self.client.event(event); } diff --git a/src/ifs/wl_surface.rs b/src/ifs/wl_surface.rs index dba720d4..600f9d06 100644 --- a/src/ifs/wl_surface.rs +++ b/src/ifs/wl_surface.rs @@ -217,7 +217,7 @@ impl SurfaceRole { } } -pub struct SurfaceSendPreferredScaleVisitor(pub Fixed); +pub struct SurfaceSendPreferredScaleVisitor; impl NodeVisitorBase for SurfaceSendPreferredScaleVisitor { fn visit_surface(&mut self, node: &Rc) { node.send_preferred_scale(); diff --git a/src/ifs/wl_surface/cursor.rs b/src/ifs/wl_surface/cursor.rs index 146a4695..f42d8d0c 100644 --- a/src/ifs/wl_surface/cursor.rs +++ b/src/ifs/wl_surface/cursor.rs @@ -6,6 +6,7 @@ use { leaks::Tracker, rect::Rect, render::Renderer, + scale::Scale, tree::{Node, NodeVisitorBase, OutputNode}, }, std::{cell::Cell, ops::Deref, rc::Rc}, @@ -103,7 +104,7 @@ impl Cursor for CursorSurface { FrameRequests.visit_surface(&self.surface); } - fn extents_at_scale(&self, scale: Fixed) -> Rect { + fn extents_at_scale(&self, scale: Scale) -> Rect { let rect = self.extents.get(); if scale == 1 { return rect; diff --git a/src/ifs/wl_surface/wp_fractional_scale_v1.rs b/src/ifs/wl_surface/wp_fractional_scale_v1.rs index b22baf9a..9914bbbc 100644 --- a/src/ifs/wl_surface/wp_fractional_scale_v1.rs +++ b/src/ifs/wl_surface/wp_fractional_scale_v1.rs @@ -39,7 +39,7 @@ impl WpFractionalScaleV1 { pub fn send_preferred_scale(&self) { self.client.event(PreferredScale { self_id: self.id, - scale: self.surface.output.get().preferred_scale.get(), + scale: self.surface.output.get().preferred_scale.get().0, }); } diff --git a/src/main.rs b/src/main.rs index 70950091..43428a35 100644 --- a/src/main.rs +++ b/src/main.rs @@ -72,6 +72,7 @@ mod pipewire; mod portal; mod rect; mod render; +mod scale; mod screenshoter; mod sighand; mod state; diff --git a/src/portal/ptr_gui.rs b/src/portal/ptr_gui.rs index b8025779..589877d1 100644 --- a/src/portal/ptr_gui.rs +++ b/src/portal/ptr_gui.rs @@ -7,6 +7,7 @@ use { ifs::zwlr_layer_shell_v1::OVERLAY, portal::ptl_display::{PortalDisplay, PortalOutput, PortalSeat}, render::{Framebuffer, RenderContext, RendererBase, Texture}, + scale::Scale, text::{self, TextMeasurement}, theme::Color, utils::{ @@ -467,7 +468,7 @@ pub struct WindowData { pub frame_missed: Cell, pub first_scale: Cell, pub have_frame: Cell, - pub scale: Cell, + pub scale: Cell, pub render_trigger: AsyncEvent, pub render_task: Cell>>, pub dpy: Rc, @@ -560,7 +561,7 @@ impl WindowData { content: Default::default(), surface, viewport, - scale: Cell::new(Fixed::from_int(1)), + scale: Cell::new(Scale::from_int(1)), fractional_scale, seats: Default::default(), }); @@ -833,7 +834,8 @@ impl UsrWlBufferOwner for GuiBuffer { impl UsrWpFractionalScaleOwner for WindowData { fn preferred_scale(self: Rc, ev: &PreferredScale) { let mut layout = self.first_scale.replace(false); - layout |= self.scale.replace(ev.scale) != ev.scale; + let scale = Scale(ev.scale); + layout |= self.scale.replace(scale) != scale; if layout { self.layout(); self.allocate_buffers(); diff --git a/src/render/renderer/framebuffer.rs b/src/render/renderer/framebuffer.rs index aa1a74b9..33fd6c72 100644 --- a/src/render/renderer/framebuffer.rs +++ b/src/render/renderer/framebuffer.rs @@ -16,6 +16,7 @@ use { sys::{glBlendFunc, glFlush, glReadnPixels, GL_ONE, GL_ONE_MINUS_SRC_ALPHA}, RenderResult, Texture, }, + scale::Scale, state::State, tree::Node, }, @@ -61,7 +62,7 @@ impl Framebuffer { glViewport(0, 0, self.gl.width, self.gl.height); glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); } - let scale = Fixed::from_int(1); + let scale = Scale::from_int(1); let mut renderer = Renderer { base: RendererBase { ctx: &self.ctx, @@ -114,7 +115,7 @@ impl Framebuffer { }); } - pub fn render_custom(&self, scale: Fixed, f: impl FnOnce(&mut RendererBase)) { + pub fn render_custom(&self, scale: Scale, f: impl FnOnce(&mut RendererBase)) { let _ = self.ctx.ctx.with_current(|| { unsafe { glBindFramebuffer(GL_FRAMEBUFFER, self.gl.fbo); @@ -143,7 +144,7 @@ impl Framebuffer { cursor_rect: Option, on_output: bool, result: &mut RenderResult, - scale: Fixed, + scale: Scale, render_hardware_cursor: bool, ) { let _ = self.ctx.ctx.with_current(|| { @@ -201,7 +202,7 @@ impl Framebuffer { }); } - pub fn render_hardware_cursor(&self, cursor: &dyn Cursor, state: &State, scale: Fixed) { + pub fn render_hardware_cursor(&self, cursor: &dyn Cursor, state: &State, scale: Scale) { let _ = self.ctx.ctx.with_current(|| { unsafe { glBindFramebuffer(GL_FRAMEBUFFER, self.gl.fbo); diff --git a/src/render/renderer/renderer.rs b/src/render/renderer/renderer.rs index 1fac30b6..f7d8557d 100644 --- a/src/render/renderer/renderer.rs +++ b/src/render/renderer/renderer.rs @@ -1,6 +1,5 @@ use { crate::{ - fixed::Fixed, format::ARGB8888, ifs::{ wl_buffer::WlBuffer, @@ -12,6 +11,7 @@ use { }, rect::Rect, render::{gl::frame_buffer::with_scissor, renderer::renderer_base::RendererBase}, + scale::Scale, state::State, theme::Color, tree::{ @@ -48,7 +48,7 @@ pub struct Renderer<'a> { } impl Renderer<'_> { - pub fn scale(&self) -> Fixed { + pub fn scale(&self) -> Scale { self.base.scale } diff --git a/src/render/renderer/renderer_base.rs b/src/render/renderer/renderer_base.rs index 5c08939e..5d04396f 100644 --- a/src/render/renderer/renderer_base.rs +++ b/src/render/renderer/renderer_base.rs @@ -1,6 +1,5 @@ use { crate::{ - fixed::Fixed, format::Format, rect::Rect, render::{ @@ -18,6 +17,7 @@ use { sys::{glClear, glClearColor, glDisable, glEnable, GL_BLEND, GL_COLOR_BUFFER_BIT}, Texture, }, + scale::Scale, theme::Color, utils::rc_eq::rc_eq, }, @@ -28,12 +28,12 @@ pub struct RendererBase<'a> { pub(super) ctx: &'a Rc, pub(super) fb: &'a GlFrameBuffer, pub(super) scaled: bool, - pub(super) scale: Fixed, + pub(super) scale: Scale, pub(super) scalef: f64, } impl RendererBase<'_> { - pub fn scale(&self) -> Fixed { + pub fn scale(&self) -> Scale { self.scale } @@ -188,7 +188,7 @@ impl RendererBase<'_> { format: &Format, tpoints: Option<&[f32; 8]>, tsize: Option<(i32, i32)>, - tscale: Fixed, + tscale: Scale, ) { assert!(rc_eq(&self.ctx.ctx, &texture.ctx.ctx)); unsafe { diff --git a/src/scale.rs b/src/scale.rs new file mode 100644 index 00000000..96900ca8 --- /dev/null +++ b/src/scale.rs @@ -0,0 +1,44 @@ +use std::fmt::{Debug, Display, Formatter}; + +const BASE: u32 = 120; +const BASEF: f64 = BASE as f64; + +#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] +#[repr(transparent)] +pub struct Scale(pub u32); + +impl Scale { + pub fn from_int(f: u32) -> Self { + Self(f.saturating_mul(BASE)) + } + + pub fn from_f64(f: f64) -> Self { + Self((f * BASEF).round() as u32) + } + + pub fn to_f64(self) -> f64 { + self.0 as f64 / BASEF + } + + pub fn round_up(self) -> u32 { + self.0.saturating_add(BASE - 1) / BASE + } +} + +impl PartialEq for Scale { + fn eq(&self, other: &u32) -> bool { + self.0 == other * BASE + } +} + +impl Debug for Scale { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + Debug::fmt(&self.to_f64(), f) + } +} + +impl Display for Scale { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + Display::fmt(&self.to_f64(), f) + } +} diff --git a/src/screenshoter.rs b/src/screenshoter.rs index 258e4186..c1c740ac 100644 --- a/src/screenshoter.rs +++ b/src/screenshoter.rs @@ -1,8 +1,8 @@ use { crate::{ - fixed::Fixed, format::XRGB8888, render::RenderError, + scale::Scale, state::State, video::{ drm::DrmError, @@ -60,7 +60,7 @@ pub fn take_screenshot(state: &State) -> Result Some(state.root.extents.get()), false, &mut Default::default(), - Fixed::from_int(1), + Scale::from_int(1), true, ); let drm = ctx.gbm.drm.dup_render()?.fd().clone(); diff --git a/src/state.rs b/src/state.rs index 5c5a9056..dcf899d2 100644 --- a/src/state.rs +++ b/src/state.rs @@ -12,7 +12,6 @@ use { config::ConfigProxy, cursor::{Cursor, ServerCursors}, dbus::Dbus, - fixed::Fixed, forker::ForkerProxy, globals::{Globals, GlobalsError, WaylandGlobal}, ifs::{ @@ -33,6 +32,7 @@ use { logger::Logger, rect::Rect, render::RenderContext, + scale::Scale, theme::Theme, tree::{ ContainerNode, ContainerSplit, Direction, DisplayNode, FloatNode, Node, NodeIds, @@ -116,7 +116,7 @@ pub struct State { pub data_offer_ids: NumCell, pub ring: Rc, pub lock: ScreenlockState, - pub scales: RefCounted, + pub scales: RefCounted, pub cursor_sizes: RefCounted, pub hardware_tick_cursor: AsyncQueue>>, pub testers: RefCell>>, @@ -235,13 +235,13 @@ impl NodeVisitorBase for UpdateTextTexturesVisitor { } impl State { - pub fn add_output_scale(&self, scale: Fixed) { + pub fn add_output_scale(&self, scale: Scale) { if self.scales.add(scale) { self.output_scales_changed(); } } - pub fn remove_output_scale(&self, scale: Fixed) { + pub fn remove_output_scale(&self, scale: Scale) { if self.scales.remove(&scale) { self.output_scales_changed(); } diff --git a/src/tasks/connector.rs b/src/tasks/connector.rs index d7a811ba..f52078db 100644 --- a/src/tasks/connector.rs +++ b/src/tasks/connector.rs @@ -1,8 +1,8 @@ use { crate::{ backend::{Connector, ConnectorEvent, ConnectorId, MonitorInfo}, - fixed::Fixed, ifs::wl_output::WlOutputGlobal, + scale::Scale, state::{ConnectorData, OutputData, State}, tree::{OutputNode, OutputRenderData}, utils::{asyncevent::AsyncEvent, clonecell::CloneCell}, @@ -122,7 +122,7 @@ impl ConnectorHandler { scroll: Default::default(), pointer_positions: Default::default(), lock_surface: Default::default(), - preferred_scale: Cell::new(Fixed::from_int(1)), + preferred_scale: Cell::new(Scale::from_int(1)), hardware_cursor: Default::default(), jay_outputs: Default::default(), screencasts: Default::default(), diff --git a/src/tree/container.rs b/src/tree/container.rs index c210cc40..b2a9ebd4 100644 --- a/src/tree/container.rs +++ b/src/tree/container.rs @@ -9,6 +9,7 @@ use { }, rect::Rect, render::{Renderer, Texture}, + scale::Scale, state::State, text, tree::{ @@ -85,7 +86,7 @@ pub struct ContainerRenderData { pub last_active_rect: Option, pub border_rects: Vec, pub underline_rects: Vec, - pub titles: SmallMapMut, 2>, + pub titles: SmallMapMut, 2>, } pub struct ContainerNode { diff --git a/src/tree/float.rs b/src/tree/float.rs index eeb3665a..68c3c282 100644 --- a/src/tree/float.rs +++ b/src/tree/float.rs @@ -6,6 +6,7 @@ use { ifs::wl_seat::{NodeSeatState, SeatId, WlSeatGlobal, BTN_LEFT}, rect::Rect, render::{Renderer, Texture}, + scale::Scale, state::State, text, tree::{ @@ -42,7 +43,7 @@ pub struct FloatNode { pub layout_scheduled: Cell, pub render_titles_scheduled: Cell, pub title: RefCell, - pub title_textures: CopyHashMap>, + pub title_textures: CopyHashMap>, seats: RefCell>, } diff --git a/src/tree/output.rs b/src/tree/output.rs index 505dce35..7a3a5bca 100644 --- a/src/tree/output.rs +++ b/src/tree/output.rs @@ -20,6 +20,7 @@ use { }, rect::Rect, render::{Framebuffer, Renderer, Texture}, + scale::Scale, state::State, text, tree::{ @@ -57,7 +58,7 @@ pub struct OutputNode { pub scroll: Scroller, pub pointer_positions: CopyHashMap, pub lock_surface: CloneCell>>, - pub preferred_scale: Cell, + pub preferred_scale: Cell, pub hardware_cursor: CloneCell>>, pub update_render_data_scheduled: Cell, pub screencasts: CopyHashMap<(ClientId, JayScreencastId), Rc>, @@ -102,7 +103,7 @@ impl OutputNode { } } - pub fn set_preferred_scale(self: &Rc, scale: Fixed) { + pub fn set_preferred_scale(self: &Rc, scale: Scale) { let old_scale = self.preferred_scale.replace(scale); if scale == old_scale { return; @@ -115,7 +116,7 @@ impl OutputNode { self.state.add_output_scale(scale); let rect = self.calculate_extents(); self.change_extents_(&rect); - let mut visitor = SurfaceSendPreferredScaleVisitor(scale); + let mut visitor = SurfaceSendPreferredScaleVisitor; self.node_visit_children(&mut visitor); for ws in self.workspaces.iter() { for stacked in ws.stacked.iter() { diff --git a/src/tree/placeholder.rs b/src/tree/placeholder.rs index 94eab6ad..bf882ffb 100644 --- a/src/tree/placeholder.rs +++ b/src/tree/placeholder.rs @@ -6,6 +6,7 @@ use { ifs::wl_seat::{NodeSeatState, WlSeatGlobal}, rect::Rect, render::{Renderer, Texture}, + scale::Scale, state::State, text, tree::{ @@ -23,7 +24,7 @@ pub struct PlaceholderNode { id: PlaceholderNodeId, toplevel: ToplevelData, destroyed: Cell, - pub textures: SmallMap, 2>, + pub textures: SmallMap, 2>, } impl PlaceholderNode { diff --git a/wire/wp_fractional_scale_v1.txt b/wire/wp_fractional_scale_v1.txt index 01e44fe1..11fa69d4 100644 --- a/wire/wp_fractional_scale_v1.txt +++ b/wire/wp_fractional_scale_v1.txt @@ -7,5 +7,5 @@ msg destroy = 0 { # events msg preferred_scale = 0 { - scale: fixed, + scale: u32, }