Skip to content

A fast file manager for Windows and macOS that never freezes: tabs, dual panes, archives as folders, Quick Look-style previews. A File Explorer and Finder alternative written in Rust.

fix(glass): accept unrotated scanout frames on rotated DXGI outputs

jokwa135993ced62dbcf
diff --git a/crates/rf-win/src/glass/capture.rs b/crates/rf-win/src/glass/capture.rs
index 76ba8de2..cc9e03e4 100644
--- a/crates/rf-win/src/glass/capture.rs
+++ b/crates/rf-win/src/glass/capture.rs
@@ -93,6 +93,23 @@ pub(super) fn unrotate_rect(r: RECT, rot: Rotation, tex_w: i32, tex_h: i32) -> R
}
}
+/// Validates a duplicated frame's dimensions against `ModeDesc` and returns the rotation the crop pass needs to undo.
+pub(super) fn dup_frame_rotation(tex_w: u32, tex_h: u32, mode_w: u32, mode_h: u32, rot: Rotation) -> Option<Rotation> {
+ if rot.swaps_axes() {
+ if (tex_w, tex_h) == (mode_h, mode_w) {
+ Some(rot)
+ } else if (tex_w, tex_h) == (mode_w, mode_h) {
+ Some(Rotation::Identity)
+ } else {
+ None
+ }
+ } else if (tex_w, tex_h) == (mode_w, mode_h) {
+ Some(rot)
+ } else {
+ None
+ }
+}
+
/// Whether the desktop that takes the user's input is the normal one. On the lock screen, the Ctrl+Alt+Del screen and a UAC prompt it is
/// Winlogon's: a process of the user cannot open it, and Desktop Duplication answers ACCESS_LOST or E_ACCESSDENIED for as long as it
/// stays. That is a pause, not a failure: a PC that was locked for a minute must come back with its glass.
@@ -403,6 +420,8 @@ pub(super) struct DupMonitor {
dup: Option<IDXGIOutputDuplication>,
pub mirror: Option<Source>,
rotation: Rotation,
+ mode_w: u32,
+ mode_h: u32,
/// The mirror holds a picture of the desktop.
pub has_image: bool,
broken_since: Option<Instant>,
@@ -441,6 +460,8 @@ impl DupMonitor {
dup: None,
mirror: None,
rotation: Rotation::Identity,
+ mode_w: 0,
+ mode_h: 0,
has_image: false,
broken_since: None,
fresh: false,
@@ -518,7 +539,9 @@ impl DupMonitor {
}
let rot = rotation_from_dxgi(dd.Rotation);
self.rotation = rot;
- let (mw, mh) = (dd.ModeDesc.Width, dd.ModeDesc.Height);
+ self.mode_w = dd.ModeDesc.Width;
+ self.mode_h = dd.ModeDesc.Height;
+ let (mw, mh) = if rot.swaps_axes() { (self.mode_h, self.mode_w) } else { (self.mode_w, self.mode_h) };
if self.mirror.as_ref().is_none_or(|m| (m.w, m.h, m.rotation) != (mw, mh, rot)) {
match Source::new(g, mw, mh, None) {
Ok(mut s) => {
@@ -553,25 +576,27 @@ impl DupMonitor {
// the first picture of a monitor, or of a duplication made anew, says nothing about what changed
let whole = !self.has_image || self.fresh;
if want {
- match (res.take().and_then(|x| x.cast::<ID3D11Texture2D>().ok()), self.mirror.as_ref().map(|m| (m.w, m.h, m.hdr.is_some()))) {
- (Some(tex), Some((mw, mh, mirror_hdr))) => {
+ match res.take().and_then(|x| x.cast::<ID3D11Texture2D>().ok()) {
+ Some(tex) => {
let mut td = D3D11_TEXTURE2D_DESC::default();
tex.GetDesc(&mut td);
// An HDR desktop hands over half floats (linear scRGB) even when 8 bits were asked for; the mirror then holds
// them as they are and the crop pass tone maps (until 1.0.7 the glass gave up on such a desktop and fell
// back).
let hdr = td.Format == DXGI_FORMAT_R16G16B16A16_FLOAT;
- if !(hdr || td.Format == DXGI_FORMAT_B8G8R8A8_UNORM) || (td.Width, td.Height) != (mw, mh) {
+ let eff_rot = dup_frame_rotation(td.Width, td.Height, self.mode_w, self.mode_h, self.rotation);
+ if !(hdr || td.Format == DXGI_FORMAT_B8G8R8A8_UNORM) || eff_rot.is_none() {
bad = Some(format!(
"the duplicated frame is {}x{} {:?}, not {}x{} B8G8R8A8 or FP16",
- td.Width, td.Height, td.Format, mw, mh
+ td.Width, td.Height, td.Format, self.mode_w, self.mode_h
));
} else {
- if hdr != mirror_hdr {
- let made = if hdr { Source::new_hdr(g, mw, mh, sdr_white_level(&self.output)) } else { Source::new(g, mw, mh, None) };
+ let eff_rot = eff_rot.unwrap();
+ if self.mirror.as_ref().is_none_or(|m| (m.w, m.h, m.hdr.is_some(), m.rotation) != (td.Width, td.Height, hdr, eff_rot)) {
+ let made = if hdr { Source::new_hdr(g, td.Width, td.Height, sdr_white_level(&self.output)) } else { Source::new(g, td.Width, td.Height, None) };
match made {
Ok(mut m) => {
- m.rotation = self.rotation;
+ m.rotation = eff_rot;
self.mirror = Some(m);
}
Err(e) => bad = Some(format!("the mirror for this desktop's colour format could not be made: {e}")),
@@ -589,16 +614,16 @@ impl DupMonitor {
}
}
// frames that arrive but never become a picture would leave the window "starting" for ever
- (None, _) => bad = Some("Desktop Duplication delivered a frame without a GPU texture".into()),
- (_, None) => bad = Some("no mirror of the monitor".into()),
+ None => bad = Some("Desktop Duplication delivered a frame without a GPU texture".into()),
}
}
drop(res);
// where it changed: only while the frame is still held, and only for a frame that became the picture
+ let m_rot = self.mirror.as_ref().map_or(Rotation::Identity, |m| m.rotation);
let dirty = match dup_dirty(image, whole, info.AccumulatedFrames, || frame_dirty(&dup, &info)) {
- Dirty::Rects(rs) if self.rotation != Rotation::Identity => {
+ Dirty::Rects(rs) if m_rot != Rotation::Identity => {
let (tw, th) = self.mirror.as_ref().map_or((0, 0), |m| (m.w as i32, m.h as i32));
- Dirty::Rects(rs.into_iter().map(|r| unrotate_rect(r, self.rotation, tw, th)).collect())
+ Dirty::Rects(rs.into_iter().map(|r| unrotate_rect(r, m_rot, tw, th)).collect())
}
other => other,
};
@@ -654,4 +679,18 @@ mod tests {
assert_eq!(unrotate_rect(r, Rotation::Deg180, 1920, 1080), RECT { left: 1890, top: 1030, right: 1910, bottom: 1060 });
assert_eq!(unrotate_rect(r, Rotation::Deg270, 1920, 1080), RECT { left: 20, top: 1890, right: 50, bottom: 1910 });
}
+
+ #[test]
+ fn dup_frame_rotation_accepts_scanout_and_upright_rotated_frames() {
+ assert_eq!(dup_frame_rotation(1600, 900, 900, 1600, Rotation::Deg90), Some(Rotation::Deg90));
+ assert_eq!(dup_frame_rotation(900, 1600, 900, 1600, Rotation::Deg90), Some(Rotation::Identity));
+ assert_eq!(dup_frame_rotation(800, 600, 900, 1600, Rotation::Deg90), None);
+
+ assert_eq!(dup_frame_rotation(1920, 1080, 1080, 1920, Rotation::Deg270), Some(Rotation::Deg270));
+ assert_eq!(dup_frame_rotation(1080, 1920, 1080, 1920, Rotation::Deg270), Some(Rotation::Identity));
+
+ assert_eq!(dup_frame_rotation(1920, 1080, 1920, 1080, Rotation::Identity), Some(Rotation::Identity));
+ assert_eq!(dup_frame_rotation(1920, 1080, 1920, 1080, Rotation::Deg180), Some(Rotation::Deg180));
+ assert_eq!(dup_frame_rotation(1080, 1920, 1920, 1080, Rotation::Identity), None);
+ }
}