🐛 Ask before a close turns the VM off in the window itself on Linux
On a Wayland desktop without zenity, KDE's by default, SDL's message box showed nothing and the close went ahead: the VM turned off with no question asked. The library now draws the question in the VM's own window on Linux, with nothing installed on the host and the same on every desktop: macOS's words and buttons in ASCII, in an 8 by 8 font scaled by a whole number of pixels from the window's density, Cancel the default for Return and Escape, Tab and the arrows moving the focus, and Space or a click answering a button. The display's sink draws it over the frame in a pass like the cursor's, over black where the display has nothing to draw. The font's glyphs are copied from SDL 3.4.18's src/render/SDL_render_debug_font.h, which marks them public domain; LICENSE says so. Where the window draws no more (its GPU lost, or no swapchain), SDL's message box asks instead. Where no question can show at all, the close no longer turns the VM off, on any OS: it keeps the VM running and logs why, and the VM is then turned off by the guest's own shutdown or the host's stop (reims-ctl stop). The one case known to reach it is a Linux host whose window draws no more, in a Wayland session without zenity or with no X server. Windows keeps its task dialog. The register's Leaving entry on Linux and Windows says what the code does now, with the tests of the pattern.
This commit is contained in:
1 parent
2992852a4f
commit
426f4cba99
19 files changed
+1474
-152
No files matched your search
@@ -16,6 +16,9 @@ A PARTICULAR PURPOSE. See the licenses for more details.
|
||||
|
||||
This covers everything in the repository but QEMU, the submodule at
|
||||
vendor/qemu, which keeps QEMU's own licenses (its LICENSE and COPYING
|
||||
files). The crates the build takes, from crates.io and from the metal2vulkan
|
||||
files), and the 8 by 8 font's glyphs in
|
||||
crates/reims-vgpu-window/src/native/sdl_thread/question.rs, copied from SDL
|
||||
3.4.18's src/render/SDL_render_debug_font.h: Marcel Sondaar's font8_8, of
|
||||
IBM's VGA font, which that file marks public domain. The crates the build takes, from crates.io and from the metal2vulkan
|
||||
repository, are not part of the repository, and each keeps its
|
||||
own license.
|
||||
@@ -652,31 +652,49 @@ runtime's own device. The switches above apply, and `resolution=` too.
|
||||
`manual` (a VM with two displays)
|
||||
- Leaving: closing a window (its close button, Alt+F4 or the window
|
||||
system's close, the task bar's on Windows) lets every key and button go
|
||||
and ends capture, the key-ups reaching the guest first, then asks, in
|
||||
SDL's message box, with macOS's words and buttons (Leaving, above),
|
||||
Cancel the default for Return and Escape: on Linux zenity's question
|
||||
where zenity is installed, a window of its own, else SDL's own X11
|
||||
dialog, which the window owns on X11 and under XWayland, Cancel left of
|
||||
Turn Off; on Windows a task dialog the window owns, Turn Off left of
|
||||
Cancel. Turn Off asks the host to end the VM; Cancel, or the question
|
||||
dismissed, leaves the window and the VM running, nothing held in the
|
||||
guest and the input released. From the close to the answer nothing of
|
||||
the window's input reaches the guest: not a key that goes down after the
|
||||
close let the keys go (F4 of Alt+F4 on Windows, which SDL hands over
|
||||
after the close), nor the keys and clicks the question takes, nor their
|
||||
ups. While the question is up, the library's UI thread waits for it: the
|
||||
windows' frames keep drawing, and their events wait. When to ask is as on
|
||||
macOS; where SDL shows no question (no zenity and no X server), the close
|
||||
goes ahead as before, and the reason is logged. The window closes at the
|
||||
display's release, once its swapchain is gone.
|
||||
and ends capture, the key-ups reaching the guest first, then asks, with
|
||||
macOS's words and buttons (Leaving, above) in ASCII, Cancel the default
|
||||
for Return and Escape. On Linux the library asks in the window itself,
|
||||
with nothing installed on the host and the same on Wayland and X11 and
|
||||
every desktop: a dark panel centred over the frame, Cancel left of Turn
|
||||
Off, in an 8 by 8 pixel font each of whose pixels is a whole square of
|
||||
the screen's, a line about 12 points high at every scale, fractional
|
||||
ones and 4K included; Tab and the arrows move the focus, Space answers
|
||||
the focused button, a click answers the button it went down and up on,
|
||||
the host's arrow shows over the window, and a minimized window is
|
||||
restored and raised. Where the window draws no more (its GPU lost, or no
|
||||
swapchain), the question shows in SDL's message box instead: zenity's
|
||||
where zenity is installed, else SDL's own X11 dialog. On Windows a task
|
||||
dialog the window owns, Turn Off left of Cancel. Turn Off asks the host
|
||||
to end the VM; Cancel, or the question dismissed, leaves the window and
|
||||
the VM running, nothing held in the guest and the input released. From
|
||||
the close to the answer nothing of the window's input reaches the guest:
|
||||
not a key that goes down after the close let the keys go (F4 of Alt+F4
|
||||
on Windows, which SDL hands over after the close), nor the keys and
|
||||
clicks the question takes, nor their ups. Where no question can show,
|
||||
the close keeps the VM running and logs why; the VM is then turned off
|
||||
by the guest's own shutdown or the host's stop (reims-ctl stop). The one
|
||||
case known to reach it is a Linux host whose window draws no more (its
|
||||
GPU lost, or no swapchain) in a Wayland session without zenity, or with
|
||||
no X server. While a dialog is up, the library's UI thread waits for it:
|
||||
the windows' frames keep drawing, and their events wait. When to ask is
|
||||
as on macOS. The window closes at the display's release, once its
|
||||
swapchain is gone.
|
||||
`test: close::a_close_asks_only_where_the_host_would_hear_of_it`,
|
||||
`test: close::only_turn_off_tells_the_host`,
|
||||
`test: sdl::shell::a_close_asks_where_the_host_hears_and_tells_it_once_turned_off`,
|
||||
`test: native::sdl_thread::input::a_cancelled_close_leaves_nothing_held_and_the_input_goes_on`,
|
||||
`test: native::sdl_thread::input::a_key_down_after_the_close_s_lift_never_reaches_the_guest`,
|
||||
`test: native::sdl_thread::question::the_question_s_words_are_macos_s_in_the_font`,
|
||||
`test: native::sdl_thread::question::the_font_scales_with_the_density`,
|
||||
`test: native::sdl_thread::question::return_and_escape_cancel_and_space_answers_the_focus`,
|
||||
`test: native::sdl_thread::question::a_click_answers_the_button_it_went_down_and_up_on`,
|
||||
`test: native::sdl_thread::render::an_overlay_is_drawn_and_told_unshown_once`,
|
||||
`test: reims-vgpu::graphics::sink::the_window_s_overlay_is_drawn_over_everything_in_a_pass_of_its_own`,
|
||||
`test: native::sdl_thread::sdl::the_question_s_default_is_cancel`,
|
||||
`test: native::sdl_thread::sdl::an_answer_is_turn_off_only_by_its_button`,
|
||||
`manual` (the question on Wayland, X11 and Windows; Cancel, then Turn Off)
|
||||
`manual` (the question on Wayland without zenity, X11 and Windows;
|
||||
Cancel, then Turn Off)
|
||||
- A GPU the driver reports lost, which the library makes no new device for:
|
||||
the window stays open and keeps the last frame it showed, and draws no
|
||||
more. Its swapchain makes no Vulkan call on the lost device from then on,
|
||||
|
||||
@@ -9,9 +9,9 @@
|
||||
//! window lives (the display's release takes the host's callbacks back) and the host gave a close
|
||||
//! callback (without one a close does nothing). One question is open at a time in the process: a
|
||||
//! close asked for while one is open adds none. Once the person has chosen Turn Off, a later close
|
||||
//! tells the host at once, as the VM is ending. Where the OS cannot show the question
|
||||
//! ([`Answer::Unshown`]), the close goes ahead as it did before the question, and the OS's side
|
||||
//! logs why.
|
||||
//! tells the host at once, as the VM is ending. Where no question can show ([`Answer::Unshown`]),
|
||||
//! the close does nothing: a close that cannot ask never turns the VM off. The VM runs on, the
|
||||
//! next close asks again, and the OS's side logs why.
|
||||
//!
|
||||
//! Before the question shows, the window's input lets go as a close's does: every key and button
|
||||
//! held goes up and capture ends, the keys going to the guest first. A cancelled close leaves it
|
||||
@@ -53,7 +53,7 @@ pub(crate) enum Answer {
|
||||
TurnOff,
|
||||
/// Cancel, or the question dismissed: nothing happens.
|
||||
Cancel,
|
||||
/// The OS could not show the question: the host is told, as before the question.
|
||||
/// No question could show: nothing happens, as for Cancel, and the OS's side logs why.
|
||||
Unshown,
|
||||
}
|
||||
|
||||
@@ -62,7 +62,7 @@ pub(crate) enum Answer {
|
||||
pub(crate) struct Asks {
|
||||
/// A question is open.
|
||||
open: bool,
|
||||
/// The person chose Turn Off, or no question could be shown: the VM is ending.
|
||||
/// The person chose Turn Off: the VM is ending.
|
||||
ending: bool,
|
||||
}
|
||||
|
||||
@@ -85,10 +85,8 @@ impl Asks {
|
||||
/// The person answered the open question: whether the host is told.
|
||||
pub(crate) fn answered(&mut self, answer: Answer) -> bool {
|
||||
self.open = false;
|
||||
if answer != Answer::Cancel {
|
||||
self.ending = true;
|
||||
}
|
||||
answer != Answer::Cancel
|
||||
self.ending |= answer == Answer::TurnOff;
|
||||
answer == Answer::TurnOff
|
||||
}
|
||||
}
|
||||
|
||||
@@ -109,7 +107,7 @@ pub(crate) fn answered(answer: Answer) -> bool {
|
||||
let said = match answer {
|
||||
Answer::TurnOff => "the person chose Turn Off: the host turns the VM off",
|
||||
Answer::Cancel => "the close is cancelled, or its window ended: the host is not told",
|
||||
Answer::Unshown => "no question showed: the host turns the VM off",
|
||||
Answer::Unshown => "no question showed: the host is not told, and the VM runs on",
|
||||
};
|
||||
log::info!(target: "reims_vgpu_window", "{said}");
|
||||
tell
|
||||
@@ -130,8 +128,8 @@ mod tests {
|
||||
assert_eq!(asks.request(false), Step::Nothing);
|
||||
}
|
||||
|
||||
/// Cancel tells the host nothing, and the next close asks again; Turn Off, or a question the
|
||||
/// OS could not show, tells the host, and a later close tells it at once
|
||||
/// Cancel, or a question that could not show, tells the host nothing, and the next close asks
|
||||
/// again; Turn Off tells the host, and a later close tells it at once
|
||||
#[test]
|
||||
fn only_turn_off_tells_the_host() {
|
||||
let mut asks = Asks::default();
|
||||
@@ -144,7 +142,7 @@ mod tests {
|
||||
|
||||
let mut unshown = Asks::default();
|
||||
assert_eq!(unshown.request(true), Step::Ask);
|
||||
assert!(unshown.answered(Answer::Unshown));
|
||||
assert_eq!(unshown.request(true), Step::Tell);
|
||||
assert!(!unshown.answered(Answer::Unshown));
|
||||
assert_eq!(unshown.request(true), Step::Ask);
|
||||
}
|
||||
}
|
||||
@@ -194,6 +194,17 @@ pub struct DrawnCursor {
|
||||
pub at: (u16, u16),
|
||||
}
|
||||
|
||||
/// An image the window draws over its frame, a pixel of it to a pixel of the drawable, clipped to
|
||||
/// the drawable, wherever the frame comes from: on Linux the question a close asks
|
||||
/// (`native::sdl_thread::question`).
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct DrawnOverlay {
|
||||
/// Its pixels, opaque or blended over the frame by their alpha; the hot spot is not used.
|
||||
pub image: Arc<Glyph>,
|
||||
/// Its top left, in the drawable's pixels.
|
||||
pub at: (u64, u64),
|
||||
}
|
||||
|
||||
/// What a window posts to its UI thread: `native`'s posts, or a test's.
|
||||
pub(crate) struct UiPosts {
|
||||
/// Starts the window's render thread and posts its opening.
|
||||
|
||||
@@ -26,8 +26,8 @@
|
||||
//! other displays), [`scroll`] (wheel and trackpad deltas to notches or smooth scrolling)
|
||||
//! and [`env`](mod@env) (the `REIMS_VGPU_WINDOW_*` variables);
|
||||
//! - a close the person asks for, which ends the VM, and the question asked before it: [`close`]
|
||||
//! (when the window asks, its words, and what an answer does), with each OS's dialog in its
|
||||
//! `native` side;
|
||||
//! (when the window asks, its words, and what an answer does), with each OS's question in its
|
||||
//! `native` side: on Linux one the library draws in the window (`native/sdl_thread/question`);
|
||||
//! - the host's screens ([`screens`]), as the OS lists them;
|
||||
//! - the window itself: [`handle`] (the [`Window`] the display holds), and its OS side
|
||||
//! (`native`, one directory per OS behind one file). On macOS (`native/macos/`) that is the
|
||||
@@ -71,7 +71,7 @@ pub use config::{
|
||||
MomentumPhase, PointerBeyond, PointerFn, PointerMotion, ScrollEvent, ScrollFn, ScrollPhase,
|
||||
WheelFn, WindowCallbacks, WindowConfig,
|
||||
};
|
||||
pub use handle::{DrawnCursor, FrameSink, Glyph, PixelRect, Window};
|
||||
pub use handle::{DrawnCursor, DrawnOverlay, FrameSink, Glyph, PixelRect, Window};
|
||||
pub use present::{Drawn, Presenter, VulkanTarget};
|
||||
pub use screens::HostScreen;
|
||||
|
||||
|
||||
@@ -29,7 +29,7 @@ pub(crate) type Answered = fn(&[u64], Answer, MainThreadMarker);
|
||||
|
||||
/// Asks the person whether to turn the VM off for windows `ids`, in an alert as a sheet on
|
||||
/// `window` (the module doc): the answer is posted to the main thread, to `answered`. Where
|
||||
/// AppKit refuses the sheet, that is logged, and the answer is Unshown: the close goes ahead.
|
||||
/// AppKit refuses the sheet, that is logged, and the answer is Unshown: the VM runs on.
|
||||
pub(crate) fn ask(window: &NSWindow, ids: Vec<u64>, answered: Answered, mtm: MainThreadMarker) {
|
||||
let alert = msg::get("new", || NSAlert::new(mtm));
|
||||
msg::get("setAlertStyle:", || {
|
||||
@@ -77,7 +77,7 @@ pub(crate) fn ask(window: &NSWindow, ids: Vec<u64>, answered: Answered, mtm: Mai
|
||||
alert.beginSheetModalForWindow_completionHandler(window, Some(&handler));
|
||||
}) {
|
||||
log::warn!(
|
||||
"the question before turning the VM off does not show ({}: {}): the close goes ahead",
|
||||
"the question before turning the VM off does not show ({}: {}): the VM runs on",
|
||||
caught.name,
|
||||
caught.reason
|
||||
);
|
||||
|
||||
@@ -299,6 +299,7 @@ fn run(window: &Window) {
|
||||
work.layout.scale,
|
||||
work.layout.pixels,
|
||||
cursor.as_ref(),
|
||||
None,
|
||||
) {
|
||||
retry = Some(work.layout);
|
||||
}
|
||||
|
||||
@@ -287,6 +287,15 @@ pub(crate) fn screens_changed(shell: &mut Shell) {
|
||||
}
|
||||
}
|
||||
|
||||
/// The window was laid out again: nothing more, as its question is a sheet ([`ask`]).
|
||||
pub(crate) fn laid_out(_shell: &mut Shell) {}
|
||||
|
||||
/// Whether a question is open in the window, over which the host's arrow shows: none, as its
|
||||
/// question is a sheet ([`ask`]).
|
||||
pub(crate) fn asks(_shell: &Shell) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
/// SDL's input events are not taken: AppKit's events are, before the application dispatches
|
||||
/// them (`input`).
|
||||
pub(crate) fn input_event(_shell: &mut Shell, _kind: u32, _event: &SDL_Event) -> bool {
|
||||
|
||||
@@ -325,8 +325,8 @@ fn view_of(id: u64, mtm: MainThreadMarker) -> Option<Retained<DisplayView>> {
|
||||
open.iter().find(|s| s.id == id).map(|s| s.view.clone())
|
||||
}
|
||||
|
||||
/// The person answered the question a close asked (`super::alert`) for windows `ids`: Turn Off,
|
||||
/// or a question AppKit could not show, tells each one's host, which ends the VM; Cancel tells
|
||||
/// The person answered the question a close asked (`super::alert`) for windows `ids`: Turn Off
|
||||
/// tells each one's host, which ends the VM; Cancel, or a question AppKit could not show, tells
|
||||
/// none, and the VM runs on (`crate::close::answered`). A window closed meanwhile tells nothing.
|
||||
pub(crate) fn answered(ids: &[u64], answer: Answer, mtm: MainThreadMarker) {
|
||||
if close::answered(answer) {
|
||||
|
||||
@@ -3,7 +3,8 @@
|
||||
//! (`crate::sdl`), and what the SDL thread does its own way is here: SDL runs on the library's UI
|
||||
//! thread (`ui`), where the window opens, changes and ends, as `sdl` places, sizes and ends it,
|
||||
//! and its events become the host's input (`input`, `keymap`); each window's render thread draws
|
||||
//! into its swapchain (`render`). What the OS under it adds is its own file (`super::linux`: the
|
||||
//! into its swapchain (`render`), on Linux with the question a close asks over the frame
|
||||
//! (`question`). What the OS under it adds is its own file (`super::linux`: the
|
||||
//! GPU a Wayland compositor composites on; `super::windows`). This file is what the shared window
|
||||
//! code asks of it: the render thread (`render`, through the display's
|
||||
//! [`FrameSink`](crate::FrameSink)), the window's opening, changes and end, posted to the UI
|
||||
@@ -11,6 +12,8 @@
|
||||
|
||||
mod input;
|
||||
mod keymap;
|
||||
#[cfg(target_os = "linux")]
|
||||
mod question;
|
||||
mod render;
|
||||
pub(crate) mod sdl;
|
||||
mod ui;
|
||||
|
||||
@@ -0,0 +1,735 @@
|
||||
//! The question a close asks on Linux, drawn by the library in the VM's own window
|
||||
//! (`crate::close`): it needs nothing installed on the host and shows the same on every desktop,
|
||||
//! Wayland's and X11's. [`Question`] lays it out for the window's drawable and pixel density
|
||||
//! ([`shape`]), draws it into an image ([`draw`]) that the window's render thread draws over the
|
||||
//! frame ([`DrawnOverlay`], `super::render`), and takes the window's keys and clicks while it is
|
||||
//! open (`super::sdl`).
|
||||
//!
|
||||
//! **What it shows**: macOS's words and buttons ([`close::QUESTION`], [`close::DETAIL`],
|
||||
//! [`close::TURN_OFF`], [`close::CANCEL`]), in ASCII: the title over the detail, wrapped at a
|
||||
//! word to at most [`MAX_COLUMNS`] characters a line, then the buttons at the right, Cancel left
|
||||
//! of Turn Off as GNOME's and KDE's dialogs place them, on a dark panel centred in the drawable.
|
||||
//! Cancel, the default, is filled with the accent colour, and the button with the focus has a
|
||||
//! ring, Cancel's at first.
|
||||
//!
|
||||
//! **The font** ([`FONT`]) is the 8 by 8 one SDL draws its debug text with, its glyphs `!` to `~`
|
||||
//! copied from SDL 3.4.18's `src/render/SDL_render_debug_font.h`: Marcel Sondaar's `font8_8`, of
|
||||
//! IBM's VGA font, which that file marks public domain. Each of its pixels is a square of a whole
|
||||
//! number of the drawable's pixels ([`scale_for`]): about 1.5 a point of the window's, so a line is
|
||||
//! about 12 points high at every scale, fractional ones included; less where the question would
|
||||
//! not fit the drawable otherwise.
|
||||
//!
|
||||
//! **Its keys** ([`Question::key`]), by their USB usages: Return, the keypad's Enter and Escape
|
||||
//! answer Cancel, the default; Space answers the button with the focus; Tab and the left and right
|
||||
//! arrows move the focus. A click answers the button it went down and up on
|
||||
//! ([`Question::click`]).
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::close::{self, Answer};
|
||||
use crate::handle::{DrawnOverlay, Glyph};
|
||||
|
||||
/// The font's glyphs for `!` (0x21) to `~` (0x7e), 8 rows each from the top, a row's lowest bit
|
||||
/// its leftmost pixel: copied from SDL 3.4.18's `src/render/SDL_render_debug_font.h`, whose font,
|
||||
/// Marcel Sondaar's `font8_8` of IBM's VGA font, is in the public domain (the module doc).
|
||||
#[rustfmt::skip]
|
||||
const FONT: [[u8; 8]; 94] = [
|
||||
[0x18, 0x3c, 0x3c, 0x18, 0x18, 0x00, 0x18, 0x00], // '!'
|
||||
[0x36, 0x36, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], // '"'
|
||||
[0x36, 0x36, 0x7f, 0x36, 0x7f, 0x36, 0x36, 0x00], // '#'
|
||||
[0x0c, 0x3e, 0x03, 0x1e, 0x30, 0x1f, 0x0c, 0x00], // '$'
|
||||
[0x00, 0x63, 0x33, 0x18, 0x0c, 0x66, 0x63, 0x00], // '%'
|
||||
[0x1c, 0x36, 0x1c, 0x6e, 0x3b, 0x33, 0x6e, 0x00], // '&'
|
||||
[0x06, 0x06, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00], // '\''
|
||||
[0x18, 0x0c, 0x06, 0x06, 0x06, 0x0c, 0x18, 0x00], // '('
|
||||
[0x06, 0x0c, 0x18, 0x18, 0x18, 0x0c, 0x06, 0x00], // ')'
|
||||
[0x00, 0x66, 0x3c, 0xff, 0x3c, 0x66, 0x00, 0x00], // '*'
|
||||
[0x00, 0x0c, 0x0c, 0x3f, 0x0c, 0x0c, 0x00, 0x00], // '+'
|
||||
[0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x0c, 0x06], // ','
|
||||
[0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x00], // '-'
|
||||
[0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x0c, 0x00], // '.'
|
||||
[0x60, 0x30, 0x18, 0x0c, 0x06, 0x03, 0x01, 0x00], // '/'
|
||||
[0x3e, 0x63, 0x73, 0x7b, 0x6f, 0x67, 0x3e, 0x00], // '0'
|
||||
[0x0c, 0x0e, 0x0c, 0x0c, 0x0c, 0x0c, 0x3f, 0x00], // '1'
|
||||
[0x1e, 0x33, 0x30, 0x1c, 0x06, 0x33, 0x3f, 0x00], // '2'
|
||||
[0x1e, 0x33, 0x30, 0x1c, 0x30, 0x33, 0x1e, 0x00], // '3'
|
||||
[0x38, 0x3c, 0x36, 0x33, 0x7f, 0x30, 0x78, 0x00], // '4'
|
||||
[0x3f, 0x03, 0x1f, 0x30, 0x30, 0x33, 0x1e, 0x00], // '5'
|
||||
[0x1c, 0x06, 0x03, 0x1f, 0x33, 0x33, 0x1e, 0x00], // '6'
|
||||
[0x3f, 0x33, 0x30, 0x18, 0x0c, 0x0c, 0x0c, 0x00], // '7'
|
||||
[0x1e, 0x33, 0x33, 0x1e, 0x33, 0x33, 0x1e, 0x00], // '8'
|
||||
[0x1e, 0x33, 0x33, 0x3e, 0x30, 0x18, 0x0e, 0x00], // '9'
|
||||
[0x00, 0x0c, 0x0c, 0x00, 0x00, 0x0c, 0x0c, 0x00], // ':'
|
||||
[0x00, 0x0c, 0x0c, 0x00, 0x00, 0x0c, 0x0c, 0x06], // ';'
|
||||
[0x18, 0x0c, 0x06, 0x03, 0x06, 0x0c, 0x18, 0x00], // '<'
|
||||
[0x00, 0x00, 0x3f, 0x00, 0x00, 0x3f, 0x00, 0x00], // '='
|
||||
[0x06, 0x0c, 0x18, 0x30, 0x18, 0x0c, 0x06, 0x00], // '>'
|
||||
[0x1e, 0x33, 0x30, 0x18, 0x0c, 0x00, 0x0c, 0x00], // '?'
|
||||
[0x3e, 0x63, 0x7b, 0x7b, 0x7b, 0x03, 0x1e, 0x00], // '@'
|
||||
[0x0c, 0x1e, 0x33, 0x33, 0x3f, 0x33, 0x33, 0x00], // 'A'
|
||||
[0x3f, 0x66, 0x66, 0x3e, 0x66, 0x66, 0x3f, 0x00], // 'B'
|
||||
[0x3c, 0x66, 0x03, 0x03, 0x03, 0x66, 0x3c, 0x00], // 'C'
|
||||
[0x1f, 0x36, 0x66, 0x66, 0x66, 0x36, 0x1f, 0x00], // 'D'
|
||||
[0x7f, 0x46, 0x16, 0x1e, 0x16, 0x46, 0x7f, 0x00], // 'E'
|
||||
[0x7f, 0x46, 0x16, 0x1e, 0x16, 0x06, 0x0f, 0x00], // 'F'
|
||||
[0x3c, 0x66, 0x03, 0x03, 0x73, 0x66, 0x7c, 0x00], // 'G'
|
||||
[0x33, 0x33, 0x33, 0x3f, 0x33, 0x33, 0x33, 0x00], // 'H'
|
||||
[0x1e, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x1e, 0x00], // 'I'
|
||||
[0x78, 0x30, 0x30, 0x30, 0x33, 0x33, 0x1e, 0x00], // 'J'
|
||||
[0x67, 0x66, 0x36, 0x1e, 0x36, 0x66, 0x67, 0x00], // 'K'
|
||||
[0x0f, 0x06, 0x06, 0x06, 0x46, 0x66, 0x7f, 0x00], // 'L'
|
||||
[0x63, 0x77, 0x7f, 0x7f, 0x6b, 0x63, 0x63, 0x00], // 'M'
|
||||
[0x63, 0x67, 0x6f, 0x7b, 0x73, 0x63, 0x63, 0x00], // 'N'
|
||||
[0x1c, 0x36, 0x63, 0x63, 0x63, 0x36, 0x1c, 0x00], // 'O'
|
||||
[0x3f, 0x66, 0x66, 0x3e, 0x06, 0x06, 0x0f, 0x00], // 'P'
|
||||
[0x1e, 0x33, 0x33, 0x33, 0x3b, 0x1e, 0x38, 0x00], // 'Q'
|
||||
[0x3f, 0x66, 0x66, 0x3e, 0x36, 0x66, 0x67, 0x00], // 'R'
|
||||
[0x1e, 0x33, 0x07, 0x0e, 0x38, 0x33, 0x1e, 0x00], // 'S'
|
||||
[0x3f, 0x2d, 0x0c, 0x0c, 0x0c, 0x0c, 0x1e, 0x00], // 'T'
|
||||
[0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x3f, 0x00], // 'U'
|
||||
[0x33, 0x33, 0x33, 0x33, 0x33, 0x1e, 0x0c, 0x00], // 'V'
|
||||
[0x63, 0x63, 0x63, 0x6b, 0x7f, 0x77, 0x63, 0x00], // 'W'
|
||||
[0x63, 0x63, 0x36, 0x1c, 0x1c, 0x36, 0x63, 0x00], // 'X'
|
||||
[0x33, 0x33, 0x33, 0x1e, 0x0c, 0x0c, 0x1e, 0x00], // 'Y'
|
||||
[0x7f, 0x63, 0x31, 0x18, 0x4c, 0x66, 0x7f, 0x00], // 'Z'
|
||||
[0x1e, 0x06, 0x06, 0x06, 0x06, 0x06, 0x1e, 0x00], // '['
|
||||
[0x03, 0x06, 0x0c, 0x18, 0x30, 0x60, 0x40, 0x00], // '\\'
|
||||
[0x1e, 0x18, 0x18, 0x18, 0x18, 0x18, 0x1e, 0x00], // ']'
|
||||
[0x08, 0x1c, 0x36, 0x63, 0x00, 0x00, 0x00, 0x00], // '^'
|
||||
[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff], // '_'
|
||||
[0x0c, 0x0c, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00], // '`'
|
||||
[0x00, 0x00, 0x1e, 0x30, 0x3e, 0x33, 0x6e, 0x00], // 'a'
|
||||
[0x07, 0x06, 0x06, 0x3e, 0x66, 0x66, 0x3b, 0x00], // 'b'
|
||||
[0x00, 0x00, 0x1e, 0x33, 0x03, 0x33, 0x1e, 0x00], // 'c'
|
||||
[0x38, 0x30, 0x30, 0x3e, 0x33, 0x33, 0x6e, 0x00], // 'd'
|
||||
[0x00, 0x00, 0x1e, 0x33, 0x3f, 0x03, 0x1e, 0x00], // 'e'
|
||||
[0x1c, 0x36, 0x06, 0x0f, 0x06, 0x06, 0x0f, 0x00], // 'f'
|
||||
[0x00, 0x00, 0x6e, 0x33, 0x33, 0x3e, 0x30, 0x1f], // 'g'
|
||||
[0x07, 0x06, 0x36, 0x6e, 0x66, 0x66, 0x67, 0x00], // 'h'
|
||||
[0x0c, 0x00, 0x0e, 0x0c, 0x0c, 0x0c, 0x1e, 0x00], // 'i'
|
||||
[0x30, 0x00, 0x30, 0x30, 0x30, 0x33, 0x33, 0x1e], // 'j'
|
||||
[0x07, 0x06, 0x66, 0x36, 0x1e, 0x36, 0x67, 0x00], // 'k'
|
||||
[0x0e, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x1e, 0x00], // 'l'
|
||||
[0x00, 0x00, 0x33, 0x7f, 0x7f, 0x6b, 0x63, 0x00], // 'm'
|
||||
[0x00, 0x00, 0x1f, 0x33, 0x33, 0x33, 0x33, 0x00], // 'n'
|
||||
[0x00, 0x00, 0x1e, 0x33, 0x33, 0x33, 0x1e, 0x00], // 'o'
|
||||
[0x00, 0x00, 0x3b, 0x66, 0x66, 0x3e, 0x06, 0x0f], // 'p'
|
||||
[0x00, 0x00, 0x6e, 0x33, 0x33, 0x3e, 0x30, 0x78], // 'q'
|
||||
[0x00, 0x00, 0x3b, 0x6e, 0x66, 0x06, 0x0f, 0x00], // 'r'
|
||||
[0x00, 0x00, 0x3e, 0x03, 0x1e, 0x30, 0x1f, 0x00], // 's'
|
||||
[0x08, 0x0c, 0x3e, 0x0c, 0x0c, 0x2c, 0x18, 0x00], // 't'
|
||||
[0x00, 0x00, 0x33, 0x33, 0x33, 0x33, 0x6e, 0x00], // 'u'
|
||||
[0x00, 0x00, 0x33, 0x33, 0x33, 0x1e, 0x0c, 0x00], // 'v'
|
||||
[0x00, 0x00, 0x63, 0x6b, 0x7f, 0x7f, 0x36, 0x00], // 'w'
|
||||
[0x00, 0x00, 0x63, 0x36, 0x1c, 0x36, 0x63, 0x00], // 'x'
|
||||
[0x00, 0x00, 0x33, 0x33, 0x33, 0x3e, 0x30, 0x1f], // 'y'
|
||||
[0x00, 0x00, 0x3f, 0x19, 0x0c, 0x26, 0x3f, 0x00], // 'z'
|
||||
[0x38, 0x0c, 0x0c, 0x07, 0x0c, 0x0c, 0x38, 0x00], // '{'
|
||||
[0x18, 0x18, 0x18, 0x00, 0x18, 0x18, 0x18, 0x00], // '|'
|
||||
[0x07, 0x0c, 0x0c, 0x38, 0x0c, 0x0c, 0x07, 0x00], // '}'
|
||||
[0x6e, 0x3b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], // '~'
|
||||
];
|
||||
|
||||
/// The longest a line of the detail is, in characters, before it wraps at a word.
|
||||
const MAX_COLUMNS: u32 = 40;
|
||||
/// A glyph's side, in the font's pixels.
|
||||
const CELL: u32 = 8;
|
||||
/// The panel's margin around what it shows, in the font's pixels.
|
||||
const PAD: u32 = 16;
|
||||
/// The room between the title and the detail, in the font's pixels.
|
||||
const TITLE_GAP: u32 = 12;
|
||||
/// The room between the detail's lines, in the font's pixels.
|
||||
const LEADING: u32 = 4;
|
||||
/// The room between the detail and the buttons, in the font's pixels.
|
||||
const BUTTONS_GAP: u32 = 16;
|
||||
/// A button's height, then its width, in the font's pixels: room around its label, the longer
|
||||
/// one (Turn Off) setting both buttons' width.
|
||||
const BUTTON: (u32, u32) = (24, 96);
|
||||
/// The room between the buttons, in the font's pixels.
|
||||
const BUTTON_GAP: u32 = 8;
|
||||
/// The fewest characters a line of the detail holds: the buttons' width.
|
||||
const MIN_COLUMNS: u32 = (2 * BUTTON.1 + BUTTON_GAP) / CELL;
|
||||
/// The most drawable pixels a font pixel takes.
|
||||
const MAX_SCALE: u32 = 16;
|
||||
|
||||
/// The panel's colour, RGBA.
|
||||
const PANEL: [u8; 4] = [0x2b, 0x2b, 0x2b, 0xff];
|
||||
/// The panel's edge.
|
||||
const EDGE: [u8; 4] = [0x6b, 0x6b, 0x6b, 0xff];
|
||||
/// The title's and the buttons' labels.
|
||||
const BRIGHT: [u8; 4] = [0xff, 0xff, 0xff, 0xff];
|
||||
/// The detail's.
|
||||
const TEXT: [u8; 4] = [0xd4, 0xd4, 0xd4, 0xff];
|
||||
/// The default button, Cancel.
|
||||
const ACCENT: [u8; 4] = [0x35, 0x74, 0xf0, 0xff];
|
||||
/// The other button.
|
||||
const PLAIN: [u8; 4] = [0x4a, 0x4a, 0x4a, 0xff];
|
||||
/// The ring around the button with the focus.
|
||||
const RING: [u8; 4] = [0xa8, 0xc8, 0xff, 0xff];
|
||||
|
||||
/// Return, the keypad's Enter, Escape, Tab, Space, and the right and left arrows: USB usages.
|
||||
const RETURN: u16 = 0x28;
|
||||
/// The keypad's Enter.
|
||||
const KEYPAD_ENTER: u16 = 0x58;
|
||||
/// Escape.
|
||||
const ESCAPE: u16 = 0x29;
|
||||
/// Tab.
|
||||
const TAB: u16 = 0x2B;
|
||||
/// Space.
|
||||
const SPACE: u16 = 0x2C;
|
||||
/// The right arrow.
|
||||
const RIGHT: u16 = 0x4F;
|
||||
/// The left arrow.
|
||||
const LEFT: u16 = 0x50;
|
||||
|
||||
/// One of the question's buttons.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub(super) enum Button {
|
||||
/// Cancel, the default.
|
||||
Cancel,
|
||||
/// Turn Off.
|
||||
TurnOff,
|
||||
}
|
||||
|
||||
/// The buttons, left to right.
|
||||
const BUTTONS: [Button; 2] = [Button::Cancel, Button::TurnOff];
|
||||
|
||||
impl Button {
|
||||
/// What choosing it answers.
|
||||
fn answer(self) -> Answer {
|
||||
match self {
|
||||
Button::Cancel => Answer::Cancel,
|
||||
Button::TurnOff => Answer::TurnOff,
|
||||
}
|
||||
}
|
||||
|
||||
/// Its label.
|
||||
fn label(self) -> &'static str {
|
||||
match self {
|
||||
Button::Cancel => close::CANCEL,
|
||||
Button::TurnOff => close::TURN_OFF,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The question laid out for a drawable ([`shape`]): its sizes in the font's pixels, each `scale`
|
||||
/// drawable pixels on a side.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub(super) struct Shape {
|
||||
/// The drawable's pixels on a side of a font pixel.
|
||||
pub(super) scale: u32,
|
||||
/// The detail's lines.
|
||||
lines: Vec<String>,
|
||||
/// The panel's width and height, in font pixels.
|
||||
size: (u32, u32),
|
||||
/// The panel's top left in the drawable, in its pixels.
|
||||
at: (u64, u64),
|
||||
}
|
||||
|
||||
impl Shape {
|
||||
/// The panel's width and height in the drawable's pixels.
|
||||
fn pixels(&self) -> (u32, u32) {
|
||||
(self.size.0 * self.scale, self.size.1 * self.scale)
|
||||
}
|
||||
|
||||
/// Where `button` is in the panel, in font pixels: its left, top, width and height.
|
||||
fn button(&self, button: Button) -> (u32, u32, u32, u32) {
|
||||
let top = self.size.1 - PAD - BUTTON.0;
|
||||
let right = self.size.0 - PAD;
|
||||
let left = match button {
|
||||
Button::TurnOff => right - BUTTON.1,
|
||||
Button::Cancel => right - 2 * BUTTON.1 - BUTTON_GAP,
|
||||
};
|
||||
(left, top, BUTTON.1, BUTTON.0)
|
||||
}
|
||||
|
||||
/// The top of the detail's first line, in font pixels.
|
||||
fn detail_top() -> u32 {
|
||||
PAD + CELL + TITLE_GAP
|
||||
}
|
||||
|
||||
/// The button under `point`, in the drawable's pixels.
|
||||
fn button_at(&self, point: (f64, f64)) -> Option<Button> {
|
||||
let scale = f64::from(self.scale);
|
||||
let (x, y) = (
|
||||
(point.0 - self.at.0 as f64) / scale,
|
||||
(point.1 - self.at.1 as f64) / scale,
|
||||
);
|
||||
BUTTONS.into_iter().find(|&button| {
|
||||
let (left, top, width, height) = self.button(button);
|
||||
let (left, top) = (f64::from(left), f64::from(top));
|
||||
(left..left + f64::from(width)).contains(&x)
|
||||
&& (top..top + f64::from(height)).contains(&y)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// The drawable's pixels a font pixel takes at `density` drawable pixels a point (the module
|
||||
/// doc): 1.5 a point, rounded, at least 1.
|
||||
pub(super) fn scale_for(density: f64) -> u32 {
|
||||
let scale = (1.5 * density).round();
|
||||
if scale.is_finite() && scale >= 1.0 {
|
||||
scale.min(f64::from(MAX_SCALE)) as u32
|
||||
} else {
|
||||
1
|
||||
}
|
||||
}
|
||||
|
||||
/// `text`'s paragraphs (its lines), each wrapped at a word to at most `columns` characters a line;
|
||||
/// a word longer than that has a line of its own.
|
||||
fn wrap(text: &str, columns: u32) -> Vec<String> {
|
||||
let columns = usize::try_from(columns).unwrap_or(usize::MAX);
|
||||
let mut lines = Vec::new();
|
||||
for paragraph in text.lines() {
|
||||
let mut line = String::new();
|
||||
for word in paragraph.split_whitespace() {
|
||||
if !line.is_empty() && line.len() + 1 + word.len() > columns {
|
||||
lines.push(std::mem::take(&mut line));
|
||||
}
|
||||
if !line.is_empty() {
|
||||
line.push(' ');
|
||||
}
|
||||
line.push_str(word);
|
||||
}
|
||||
lines.push(line);
|
||||
}
|
||||
lines
|
||||
}
|
||||
|
||||
/// `text`'s width in font pixels.
|
||||
fn width_of(text: &str) -> u32 {
|
||||
u32::try_from(text.len())
|
||||
.unwrap_or(u32::MAX)
|
||||
.saturating_mul(CELL)
|
||||
}
|
||||
|
||||
/// The question laid out for a drawable of `drawable` pixels at `density` pixels a point: at
|
||||
/// [`scale_for`]'s scale, or the largest below it at which the panel fits the drawable, its
|
||||
/// detail as wide as the drawable takes, from [`MIN_COLUMNS`] to [`MAX_COLUMNS`] characters a
|
||||
/// line, centred in the drawable.
|
||||
pub(super) fn shape(drawable: (u64, u64), density: f64) -> Shape {
|
||||
let mut scale = scale_for(density);
|
||||
loop {
|
||||
let across = u32::try_from(drawable.0 / u64::from(scale)).unwrap_or(u32::MAX);
|
||||
let columns = (across.saturating_sub(2 * PAD) / CELL).clamp(MIN_COLUMNS, MAX_COLUMNS);
|
||||
let lines = wrap(close::DETAIL, columns);
|
||||
let text = (lines.iter().map(|l| width_of(l)))
|
||||
.chain([width_of(close::QUESTION), 2 * BUTTON.1 + BUTTON_GAP])
|
||||
.max()
|
||||
.unwrap_or(0);
|
||||
let rows = u32::try_from(lines.len()).unwrap_or(u32::MAX);
|
||||
let size = (
|
||||
text + 2 * PAD,
|
||||
Shape::detail_top() + rows * (CELL + LEADING) - LEADING + BUTTONS_GAP + BUTTON.0 + PAD,
|
||||
);
|
||||
let (width, height) = (u64::from(size.0 * scale), u64::from(size.1 * scale));
|
||||
let fits = width <= drawable.0 && height <= drawable.1;
|
||||
if fits || scale == 1 {
|
||||
let at = (
|
||||
drawable.0.saturating_sub(width) / 2,
|
||||
drawable.1.saturating_sub(height) / 2,
|
||||
);
|
||||
return Shape {
|
||||
scale,
|
||||
lines,
|
||||
size,
|
||||
at,
|
||||
};
|
||||
}
|
||||
scale -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
/// What the question draws on: its pixels, RGBA, in rows of `width`, painted in font pixels of
|
||||
/// `scale` drawable pixels a side.
|
||||
struct Canvas {
|
||||
/// The pixels.
|
||||
rgba: Vec<u8>,
|
||||
/// Its width, in drawable pixels.
|
||||
width: u32,
|
||||
/// The drawable pixels on a side of a font pixel.
|
||||
scale: u32,
|
||||
}
|
||||
|
||||
impl Canvas {
|
||||
/// Fills the rectangle `left`, `top`, `width`, `height` (in font pixels) with `colour`.
|
||||
fn fill(&mut self, (left, top, width, height): (u32, u32, u32, u32), colour: [u8; 4]) {
|
||||
let s = self.scale;
|
||||
let row = usize::try_from(self.width).unwrap_or(0) * 4;
|
||||
let x0 = usize::try_from(left * s).unwrap_or(0) * 4;
|
||||
let x1 = usize::try_from((left + width) * s).unwrap_or(0) * 4;
|
||||
for y in top * s..(top + height) * s {
|
||||
let start = usize::try_from(y).unwrap_or(0) * row;
|
||||
if let Some(span) = self.rgba.get_mut(start + x0..start + x1.min(row)) {
|
||||
for pixel in span.as_chunks_mut::<4>().0 {
|
||||
*pixel = colour;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Draws `text` from `left`, `top` (in font pixels) in `colour`, a glyph a cell: what the
|
||||
/// font has no glyph for, a space among them, leaves its cell as it is.
|
||||
fn text(&mut self, left: u32, top: u32, text: &str, colour: [u8; 4]) {
|
||||
for (column, c) in (0u32..).zip(text.chars()) {
|
||||
let Some(glyph) = glyph_of(c) else {
|
||||
continue;
|
||||
};
|
||||
for (y, bits) in (0u32..).zip(glyph) {
|
||||
for x in 0..CELL {
|
||||
if bits & (1 << x) != 0 {
|
||||
self.fill((left + column * CELL + x, top + y, 1, 1), colour);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The font's glyph of `c`, or `None` where it has none: a space, or what is not `!` to `~`.
|
||||
fn glyph_of(c: char) -> Option<[u8; 8]> {
|
||||
let index = u32::from(c).checked_sub(0x21)?;
|
||||
FONT.get(usize::try_from(index).ok()?).copied()
|
||||
}
|
||||
|
||||
/// The question as `shape` lays it out, with the focus on `focus`: the panel, its edge, the title,
|
||||
/// the detail and the buttons (the module doc). Opaque.
|
||||
pub(super) fn draw(shape: &Shape, focus: Button) -> Glyph {
|
||||
let (width, height) = shape.pixels();
|
||||
let length = usize::try_from(u64::from(width) * u64::from(height) * 4).unwrap_or(0);
|
||||
let mut canvas = Canvas {
|
||||
rgba: vec![0; length],
|
||||
width,
|
||||
scale: shape.scale,
|
||||
};
|
||||
let (w, h) = shape.size;
|
||||
canvas.fill((0, 0, w, h), EDGE);
|
||||
canvas.fill((1, 1, w - 2, h - 2), PANEL);
|
||||
canvas.text(PAD, PAD, close::QUESTION, BRIGHT);
|
||||
for (row, line) in (0u32..).zip(&shape.lines) {
|
||||
canvas.text(
|
||||
PAD,
|
||||
Shape::detail_top() + row * (CELL + LEADING),
|
||||
line,
|
||||
TEXT,
|
||||
);
|
||||
}
|
||||
for button in BUTTONS {
|
||||
let (left, top, bw, bh) = shape.button(button);
|
||||
if button == focus {
|
||||
canvas.fill((left - 2, top - 2, bw + 4, bh + 4), RING);
|
||||
canvas.fill((left - 1, top - 1, bw + 2, bh + 2), PANEL);
|
||||
}
|
||||
let fill = if button == Button::Cancel {
|
||||
ACCENT
|
||||
} else {
|
||||
PLAIN
|
||||
};
|
||||
canvas.fill((left, top, bw, bh), fill);
|
||||
let label = button.label();
|
||||
let x = left + (bw - width_of(label)) / 2;
|
||||
canvas.text(x, top + (bh - CELL) / 2, label, BRIGHT);
|
||||
}
|
||||
Glyph {
|
||||
width,
|
||||
height,
|
||||
hot_spot: (0, 0),
|
||||
rgba: canvas.rgba,
|
||||
}
|
||||
}
|
||||
|
||||
/// What a key or a click did to the question.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub(super) enum Step {
|
||||
/// Nothing.
|
||||
Nothing,
|
||||
/// The focus moved: the question is drawn again ([`Question::overlay`]).
|
||||
Redraw,
|
||||
/// The person answered.
|
||||
Answer(Answer),
|
||||
}
|
||||
|
||||
/// The question open in a window: for which windows, its focus, and its image.
|
||||
pub(super) struct Question {
|
||||
/// The windows the answer is for (`crate::sdl::shell::answered`).
|
||||
pub(super) ids: Vec<u64>,
|
||||
/// The button with the focus.
|
||||
focus: Button,
|
||||
/// The button the left button went down on, until it goes up.
|
||||
pressed: Option<Button>,
|
||||
/// Its layout.
|
||||
shape: Shape,
|
||||
/// Its image, drawn for the shape and the focus.
|
||||
image: Arc<Glyph>,
|
||||
}
|
||||
|
||||
impl Question {
|
||||
/// The question for windows `ids`, laid out for a drawable of `drawable` pixels at `density`
|
||||
/// pixels a point, the focus on Cancel.
|
||||
pub(super) fn new(ids: Vec<u64>, drawable: (u64, u64), density: f64) -> Question {
|
||||
let shape = shape(drawable, density);
|
||||
let image = Arc::new(draw(&shape, Button::Cancel));
|
||||
Question {
|
||||
ids,
|
||||
focus: Button::Cancel,
|
||||
pressed: None,
|
||||
shape,
|
||||
image,
|
||||
}
|
||||
}
|
||||
|
||||
/// What the window's render thread draws over the frame.
|
||||
pub(super) fn overlay(&self) -> DrawnOverlay {
|
||||
DrawnOverlay {
|
||||
image: Arc::clone(&self.image),
|
||||
at: self.shape.at,
|
||||
}
|
||||
}
|
||||
|
||||
/// The window was laid out again, its drawable now `drawable` pixels at `density` pixels a
|
||||
/// point: whether the question changed, and is drawn again.
|
||||
pub(super) fn relayout(&mut self, drawable: (u64, u64), density: f64) -> bool {
|
||||
let shape = shape(drawable, density);
|
||||
if shape == self.shape {
|
||||
return false;
|
||||
}
|
||||
self.shape = shape;
|
||||
self.image = Arc::new(draw(&self.shape, self.focus));
|
||||
true
|
||||
}
|
||||
|
||||
/// Moves the focus to `button`.
|
||||
fn focus(&mut self, button: Button) -> Step {
|
||||
if button == self.focus {
|
||||
return Step::Nothing;
|
||||
}
|
||||
self.focus = button;
|
||||
self.image = Arc::new(draw(&self.shape, self.focus));
|
||||
Step::Redraw
|
||||
}
|
||||
|
||||
/// Key `usage` went down (`down`) or up; `repeat` for the host's repeat of a held key: only
|
||||
/// a first down does anything (the module doc).
|
||||
pub(super) fn key(&mut self, usage: u16, down: bool, repeat: bool) -> Step {
|
||||
if !down || repeat {
|
||||
return Step::Nothing;
|
||||
}
|
||||
match usage {
|
||||
RETURN | KEYPAD_ENTER | ESCAPE => Step::Answer(Answer::Cancel),
|
||||
SPACE => Step::Answer(self.focus.answer()),
|
||||
TAB => match self.focus {
|
||||
Button::Cancel => self.focus(Button::TurnOff),
|
||||
Button::TurnOff => self.focus(Button::Cancel),
|
||||
},
|
||||
LEFT => self.focus(Button::Cancel),
|
||||
RIGHT => self.focus(Button::TurnOff),
|
||||
_ => Step::Nothing,
|
||||
}
|
||||
}
|
||||
|
||||
/// The left button went down (`down`) or up at `point`, in the drawable's pixels: going up
|
||||
/// over the button it went down on answers that button.
|
||||
pub(super) fn click(&mut self, down: bool, point: (f64, f64)) -> Step {
|
||||
let under = self.shape.button_at(point);
|
||||
if down {
|
||||
self.pressed = under;
|
||||
return Step::Nothing;
|
||||
}
|
||||
match (self.pressed.take(), under) {
|
||||
(Some(pressed), Some(under)) if pressed == under => Step::Answer(under.answer()),
|
||||
_ => Step::Nothing,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// A question for window 1 in a 1920 x 1080 drawable at 1 pixel a point.
|
||||
fn question() -> Question {
|
||||
Question::new(vec![1], (1920, 1080), 1.0)
|
||||
}
|
||||
|
||||
/// The centre of `button` in the drawable's pixels.
|
||||
fn centre(q: &Question, button: Button) -> (f64, f64) {
|
||||
let (left, top, width, height) = q.shape.button(button);
|
||||
let s = f64::from(q.shape.scale);
|
||||
(
|
||||
q.shape.at.0 as f64 + (f64::from(left) + f64::from(width) / 2.0) * s,
|
||||
q.shape.at.1 as f64 + (f64::from(top) + f64::from(height) / 2.0) * s,
|
||||
)
|
||||
}
|
||||
|
||||
/// The question says macOS's words, in ASCII, every character of them one the font draws
|
||||
#[test]
|
||||
fn the_question_s_words_are_macos_s_in_the_font() {
|
||||
for text in [
|
||||
close::QUESTION,
|
||||
close::DETAIL,
|
||||
close::TURN_OFF,
|
||||
close::CANCEL,
|
||||
] {
|
||||
for c in text.chars().filter(|&c| c != ' ' && c != '\n') {
|
||||
assert!(c.is_ascii() && glyph_of(c).is_some(), "{c:?} in {text:?}");
|
||||
}
|
||||
}
|
||||
assert_eq!(glyph_of(' '), None);
|
||||
assert_eq!(
|
||||
glyph_of('A'),
|
||||
Some([0x0c, 0x1e, 0x33, 0x33, 0x3f, 0x33, 0x33, 0x00])
|
||||
);
|
||||
assert_eq!(glyph_of('~').map(|g| g[0]), Some(0x6e));
|
||||
let shape = shape((1920, 1080), 1.0);
|
||||
assert_eq!(
|
||||
shape.lines,
|
||||
[
|
||||
"The VM turns off at once, as if its",
|
||||
"power were cut.",
|
||||
"Unsaved work in it is lost."
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/// A font pixel is a whole number of drawable pixels, about 1.5 a point at every scale, so the
|
||||
/// question reads at 4K and at fractional scales; the question shrinks only where it would
|
||||
/// not fit, and is centred
|
||||
#[test]
|
||||
fn the_font_scales_with_the_density() {
|
||||
for (density, scale) in [
|
||||
(1.0, 2),
|
||||
(1.25, 2),
|
||||
(1.5, 2),
|
||||
(1.75, 3),
|
||||
(2.0, 3),
|
||||
(2.1, 3),
|
||||
(3.0, 5),
|
||||
(0.0, 1),
|
||||
(f64::NAN, 1),
|
||||
(100.0, MAX_SCALE),
|
||||
] {
|
||||
assert_eq!(scale_for(density), scale, "{density}");
|
||||
}
|
||||
let wide = shape((3840, 2160), 2.0);
|
||||
assert_eq!(wide.scale, 3);
|
||||
let (width, height) = wide.pixels();
|
||||
assert_eq!(
|
||||
wide.at,
|
||||
(
|
||||
(3840 - u64::from(width)) / 2,
|
||||
(2160 - u64::from(height)) / 2
|
||||
)
|
||||
);
|
||||
// A window of 320 x 200 points at 1 pixel a point: one pixel a font pixel, the detail
|
||||
// wrapped narrower, and the whole question inside the drawable.
|
||||
let small = shape((320, 200), 1.0);
|
||||
assert_eq!(small.scale, 1);
|
||||
let (width, height) = small.pixels();
|
||||
assert!(width <= 320 && height <= 200, "{width} x {height}");
|
||||
assert_eq!(
|
||||
small.at,
|
||||
((320 - u64::from(width)) / 2, (200 - u64::from(height)) / 2)
|
||||
);
|
||||
}
|
||||
|
||||
/// The image is the panel at the shape's size, each font pixel a square of the scale's pixels:
|
||||
/// the edge at the corner, the title's first glyph (T) in its colour, the default button
|
||||
/// filled with the accent and the focus's ring around it
|
||||
#[test]
|
||||
fn the_question_is_drawn_in_squares_of_the_scale() {
|
||||
let q = Question::new(vec![1], (3840, 2160), 2.0);
|
||||
let image = &q.image;
|
||||
let s = q.shape.scale;
|
||||
assert_eq!((image.width, image.height), q.shape.pixels());
|
||||
assert_eq!(image.hot_spot, (0, 0));
|
||||
let pixel = |x: u32, y: u32| {
|
||||
let at = usize::try_from((y * image.width + x) * 4).unwrap();
|
||||
<[u8; 4]>::try_from(&image.rgba[at..at + 4]).unwrap()
|
||||
};
|
||||
assert_eq!(pixel(0, 0), EDGE);
|
||||
assert_eq!(pixel(s, s), PANEL);
|
||||
// T's first row is its left six pixels.
|
||||
for x in 0..6 * s {
|
||||
assert_eq!(pixel(PAD * s + x, PAD * s), BRIGHT, "{x}");
|
||||
}
|
||||
assert_eq!(pixel((PAD + 6) * s, PAD * s), PANEL);
|
||||
let (left, top, ..) = q.shape.button(Button::Cancel);
|
||||
assert_eq!(pixel(left * s, top * s), ACCENT);
|
||||
assert_eq!(pixel((left - 2) * s, (top - 2) * s), RING);
|
||||
assert_eq!(pixel((left - 1) * s, (top - 1) * s), PANEL);
|
||||
let (left, top, ..) = q.shape.button(Button::TurnOff);
|
||||
assert_eq!(pixel(left * s, top * s), PLAIN);
|
||||
assert_eq!(pixel((left - 2) * s, (top - 2) * s), PANEL, "no ring");
|
||||
}
|
||||
|
||||
/// Return, the keypad's Enter and Escape answer Cancel, the default, wherever the focus is;
|
||||
/// Space answers the button with the focus, which Tab and the arrows move; ups and repeats do
|
||||
/// nothing
|
||||
#[test]
|
||||
fn return_and_escape_cancel_and_space_answers_the_focus() {
|
||||
for key in [RETURN, KEYPAD_ENTER, ESCAPE] {
|
||||
let mut q = question();
|
||||
assert_eq!(q.key(TAB, true, false), Step::Redraw);
|
||||
assert_eq!(q.key(key, true, true), Step::Nothing, "a repeat");
|
||||
assert_eq!(q.key(key, false, false), Step::Nothing, "an up");
|
||||
assert_eq!(q.key(key, true, false), Step::Answer(Answer::Cancel));
|
||||
}
|
||||
let mut q = question();
|
||||
let before = Arc::clone(&q.image);
|
||||
assert_eq!(q.key(SPACE, true, false), Step::Answer(Answer::Cancel));
|
||||
assert_eq!(q.key(TAB, true, false), Step::Redraw);
|
||||
assert!(!Arc::ptr_eq(&before, &q.image), "drawn again");
|
||||
assert_eq!(q.key(SPACE, true, false), Step::Answer(Answer::TurnOff));
|
||||
assert_eq!(q.key(RIGHT, true, false), Step::Nothing, "the focus stays");
|
||||
assert_eq!(q.key(LEFT, true, false), Step::Redraw);
|
||||
assert_eq!(q.key(SPACE, true, false), Step::Answer(Answer::Cancel));
|
||||
assert_eq!(q.key(RIGHT, true, false), Step::Redraw);
|
||||
assert_eq!(q.key(TAB, true, false), Step::Redraw);
|
||||
assert_eq!(q.key(SPACE, true, false), Step::Answer(Answer::Cancel));
|
||||
assert_eq!(q.key(0x04, true, false), Step::Nothing, "a");
|
||||
}
|
||||
|
||||
/// A click answers the button it went down and up on; one that goes up off it, or on the
|
||||
/// other, or anywhere else, answers nothing
|
||||
#[test]
|
||||
fn a_click_answers_the_button_it_went_down_and_up_on() {
|
||||
let mut q = question();
|
||||
let (cancel, turn_off) = (centre(&q, Button::Cancel), centre(&q, Button::TurnOff));
|
||||
assert_eq!(q.click(true, turn_off), Step::Nothing);
|
||||
assert_eq!(q.click(false, turn_off), Step::Answer(Answer::TurnOff));
|
||||
assert_eq!(q.click(true, cancel), Step::Nothing);
|
||||
assert_eq!(q.click(false, cancel), Step::Answer(Answer::Cancel));
|
||||
assert_eq!(q.click(true, cancel), Step::Nothing);
|
||||
assert_eq!(q.click(false, turn_off), Step::Nothing, "up on the other");
|
||||
assert_eq!(
|
||||
q.click(false, turn_off),
|
||||
Step::Nothing,
|
||||
"an up with no down"
|
||||
);
|
||||
assert_eq!(q.click(true, (0.0, 0.0)), Step::Nothing);
|
||||
assert_eq!(q.click(false, (0.0, 0.0)), Step::Nothing);
|
||||
let (left, top, ..) = q.shape.button(Button::TurnOff);
|
||||
let s = f64::from(q.shape.scale);
|
||||
let edge = (
|
||||
q.shape.at.0 as f64 + f64::from(left) * s - 0.5,
|
||||
q.shape.at.1 as f64 + f64::from(top) * s,
|
||||
);
|
||||
assert_eq!(q.click(true, edge), Step::Nothing);
|
||||
assert_eq!(q.click(false, edge), Step::Nothing, "just left of Turn Off");
|
||||
}
|
||||
|
||||
/// The question laid out again for the drawable it has changes nothing; for a new drawable or
|
||||
/// density it is placed and drawn again
|
||||
#[test]
|
||||
fn a_new_layout_places_and_draws_the_question_again() {
|
||||
let mut q = question();
|
||||
let image = Arc::clone(&q.image);
|
||||
assert!(!q.relayout((1920, 1080), 1.0));
|
||||
assert!(Arc::ptr_eq(&image, &q.image));
|
||||
assert!(q.relayout((1280, 720), 1.0));
|
||||
assert_eq!(q.overlay().at, q.shape.at);
|
||||
assert!(q.relayout((2560, 1440), 2.0));
|
||||
assert_eq!(q.shape.scale, 3);
|
||||
assert!(!Arc::ptr_eq(&image, &q.overlay().image));
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,11 @@
|
||||
//! window's Vulkan surface ([`VulkanTarget`]), as `crate::present` says: made at the first draw,
|
||||
//! tried again after [`RETRY`] where no image came in time, made again where one ends.
|
||||
//!
|
||||
//! Over the frame it draws the window's overlay where the window shows one
|
||||
//! ([`Surface::set_overlay`]: on Linux the question a close asks, `super::question`), and where
|
||||
//! the window can draw no more ([`Presenting::is_ended`]) it tells the UI thread, once an overlay,
|
||||
//! that the overlay does not show (`super::sdl::overlay_unshown`).
|
||||
//!
|
||||
//! Requests that come while it draws fold into one: it draws the latest frame once more. It
|
||||
//! copies the sink out of the window's lock before asking it, and holds no lock of the window's
|
||||
//! while it draws.
|
||||
@@ -31,7 +36,7 @@ use reims_vgpu_vulkan::os::OsThreads;
|
||||
use sdl3_sys::everything::{SDL_Vulkan_CreateSurface, SDL_Window};
|
||||
|
||||
use crate::geometry::Layout;
|
||||
use crate::handle::Window;
|
||||
use crate::handle::{DrawnOverlay, Window};
|
||||
use crate::present::{Presenting, RETRY, VulkanTarget};
|
||||
|
||||
/// The render thread's stack: 8 MiB, as the process's main thread has. The render thread's
|
||||
@@ -100,10 +105,14 @@ struct SurfaceState {
|
||||
layout: Option<Layout>,
|
||||
/// Whether the window is shown.
|
||||
visible: bool,
|
||||
/// Something new to show: a frame, a layout, the window showing again.
|
||||
/// Something new to show: a frame, a layout, the window showing again, the overlay.
|
||||
dirty: bool,
|
||||
/// The window has ended: the render thread ends.
|
||||
closed: bool,
|
||||
/// What the window draws over the frame, while it shows one.
|
||||
overlay: Option<DrawnOverlay>,
|
||||
/// The UI thread was told this overlay does not show.
|
||||
unshown_told: bool,
|
||||
}
|
||||
|
||||
/// A draw the render thread takes: SDL's window and the layout.
|
||||
@@ -178,6 +187,37 @@ impl Surface {
|
||||
});
|
||||
}
|
||||
|
||||
/// What the window draws over the frame (the module doc), or none: drawn at once.
|
||||
#[cfg_attr(
|
||||
not(target_os = "linux"),
|
||||
expect(dead_code, reason = "only Linux's window draws an overlay")
|
||||
)]
|
||||
pub(crate) fn set_overlay(&self, overlay: Option<DrawnOverlay>) {
|
||||
self.update(|s| {
|
||||
s.overlay = overlay;
|
||||
s.unshown_told = false;
|
||||
s.dirty = true;
|
||||
});
|
||||
}
|
||||
|
||||
/// The overlay to draw over the frame, if any.
|
||||
fn overlay(&self) -> Option<DrawnOverlay> {
|
||||
self.state.lock().overlay.clone()
|
||||
}
|
||||
|
||||
/// Whether to tell the UI thread the overlay does not show: once an overlay, while there is
|
||||
/// one.
|
||||
#[cfg_attr(
|
||||
not(target_os = "linux"),
|
||||
expect(dead_code, reason = "only Linux's window draws an overlay")
|
||||
)]
|
||||
fn tell_unshown(&self) -> bool {
|
||||
let mut state = self.state.lock();
|
||||
let tell = state.overlay.is_some() && !state.unshown_told;
|
||||
state.unshown_told |= tell;
|
||||
tell
|
||||
}
|
||||
|
||||
/// The guest presented a frame: it is drawn.
|
||||
pub(crate) fn request_draw(&self) {
|
||||
self.update(|s| s.dirty = true);
|
||||
@@ -246,14 +286,21 @@ fn run(window: &Window) {
|
||||
presenting.make(|| sink.presenter(&work.target));
|
||||
}
|
||||
let cursor = window.drawn_cursor();
|
||||
let overlay = surface.overlay();
|
||||
if presenting.draw(
|
||||
work.layout.drawable,
|
||||
work.layout.scale,
|
||||
work.layout.pixels,
|
||||
cursor.as_ref(),
|
||||
overlay.as_ref(),
|
||||
) {
|
||||
retry = Some(work);
|
||||
}
|
||||
#[cfg(target_os = "linux")]
|
||||
if presenting.is_ended() && surface.tell_unshown() {
|
||||
let id = window.id();
|
||||
super::ui::post(move || super::sdl::overlay_unshown(id));
|
||||
}
|
||||
}
|
||||
drop(presenting);
|
||||
let id = window.id();
|
||||
@@ -358,6 +405,33 @@ mod tests {
|
||||
assert!(surface.next(None).is_none());
|
||||
}
|
||||
|
||||
/// An overlay set or taken away asks for a draw; the UI thread is told once an overlay that
|
||||
/// it does not show, and not where there is none
|
||||
#[cfg(target_os = "linux")]
|
||||
#[test]
|
||||
fn an_overlay_is_drawn_and_told_unshown_once() {
|
||||
let surface = Surface::new();
|
||||
surface.set_target(a_target());
|
||||
surface.set_layout(a_layout());
|
||||
surface.set_visible(true);
|
||||
assert!(surface.state.lock().take().is_some());
|
||||
assert!(!surface.tell_unshown(), "no overlay");
|
||||
let overlay = DrawnOverlay {
|
||||
image: Arc::new(crate::handle::Glyph::default()),
|
||||
at: (1, 2),
|
||||
};
|
||||
surface.set_overlay(Some(overlay));
|
||||
assert!(surface.state.lock().take().is_some(), "drawn");
|
||||
assert_eq!(surface.overlay().map(|o| o.at), Some((1, 2)));
|
||||
assert!(surface.tell_unshown());
|
||||
assert!(!surface.tell_unshown(), "told once");
|
||||
surface.set_overlay(surface.overlay());
|
||||
assert!(surface.tell_unshown(), "a new overlay is told again");
|
||||
surface.set_overlay(None);
|
||||
assert!(surface.state.lock().take().is_some(), "drawn without it");
|
||||
assert!(surface.overlay().is_none() && !surface.tell_unshown());
|
||||
}
|
||||
|
||||
/// A draw that had no image in time is not made again while the window is hidden: it is
|
||||
/// made once the window shows
|
||||
#[test]
|
||||
|
||||
@@ -71,6 +71,9 @@ pub(crate) struct State {
|
||||
/// The screen it opened on: SDL's answer to which screen a window is on is not used (the
|
||||
/// module doc).
|
||||
display: SDL_DisplayID,
|
||||
/// The question a close asks in the window, while it is open ([`ask`]).
|
||||
#[cfg(target_os = "linux")]
|
||||
question: Option<super::question::Question>,
|
||||
}
|
||||
|
||||
/// A window's input: SDL's events, queued until the shell sends them (`super::input`).
|
||||
@@ -204,7 +207,11 @@ pub(crate) fn open(window: &Arc<Window>, borderless: bool, _taken: &[Frame]) ->
|
||||
)
|
||||
.with_capture(Capture::with_setting(crate::env::settings().capture)),
|
||||
),
|
||||
state: State { display },
|
||||
state: State {
|
||||
display,
|
||||
#[cfg(target_os = "linux")]
|
||||
question: None,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -269,8 +276,13 @@ pub(crate) fn cursor_changed(shell: &mut Shell) {
|
||||
}
|
||||
|
||||
/// An input event of type `kind` for the window: the pointer, a button, the wheel or a key, to
|
||||
/// the window's input. Whether capture changed.
|
||||
/// the window's input, or to the question open in the window ([`ask`]). Whether capture changed.
|
||||
pub(crate) fn input_event(shell: &mut Shell, kind: u32, event: &SDL_Event) -> bool {
|
||||
#[cfg(target_os = "linux")]
|
||||
if shell.os.question.is_some() {
|
||||
question_event(shell, kind, event);
|
||||
return false;
|
||||
}
|
||||
match kind {
|
||||
k if k == SDL_EVENT_MOUSE_MOTION.0 => {
|
||||
// SAFETY: a motion event.
|
||||
@@ -453,23 +465,164 @@ const TURN_OFF_ID: c_int = 1;
|
||||
/// The button of the question's Cancel, the default, and the answer where no button is chosen.
|
||||
const CANCEL_ID: c_int = 0;
|
||||
|
||||
/// Asks the person whether to turn the VM off, for windows `ids` (`crate::close`), in SDL's
|
||||
/// message box over `shell`'s window: on Linux zenity's question where zenity is installed, else
|
||||
/// SDL's own X11 dialog, which the window owns on X11 and under XWayland; on Windows a task dialog
|
||||
/// the window owns. Read from SDL 3.4.18's code: zenity's question is a window of its own, which
|
||||
/// SDL waits for. SDL's message box holds the thread it runs on until it is answered, the UI
|
||||
/// thread here, which made the window, as SDL asks: the other windows' events and the work posted
|
||||
/// wait meanwhile, while the render threads draw on. So it is posted, to run after the events at
|
||||
/// hand with no window borrowed; the answer goes to [`shell::answered`]. A window whose display
|
||||
/// ended before the question showed asks nothing.
|
||||
/// Asks the person whether to turn the VM off, for windows `ids` (`crate::close`), over `shell`'s
|
||||
/// window: on Linux in the window itself, which the library draws (`ask_in_the_window`), and in
|
||||
/// SDL's message box where the window cannot draw it; on Windows in SDL's message box, a task
|
||||
/// dialog the window owns ([`ask_in_a_dialog`]).
|
||||
///
|
||||
/// From the close to the answer, nothing of the window's input reaches the guest
|
||||
/// ([`input::Input::question_opened`]): the events at hand before the question shows, a key that
|
||||
/// went down after the close's lift among them, and the ups of the keys the question's dialog
|
||||
/// takes, which come once it is answered.
|
||||
/// went down after the close's lift among them, the keys and clicks the question takes, and the
|
||||
/// ups of the keys a dialog takes, which come once it is answered.
|
||||
pub(crate) fn ask(shell: &mut Shell, ids: Vec<u64>) {
|
||||
let id = shell.window.id();
|
||||
shell.input.0.question_opened();
|
||||
#[cfg(target_os = "linux")]
|
||||
let Err(ids) = ask_in_the_window(shell, ids) else {
|
||||
return;
|
||||
};
|
||||
ask_in_a_dialog(shell.window.id(), ids);
|
||||
}
|
||||
|
||||
/// Asks the question in `shell`'s window (`super::question`): drawn over the frame by its render
|
||||
/// thread, the window's keys and clicks going to it ([`question_event`]), the host's arrow over it
|
||||
/// ([`asks`]); the window restored where it is minimized, and raised. `ids` back where the window
|
||||
/// has no drawable to draw it in yet. A render thread that can draw no more hands the question to
|
||||
/// SDL's message box ([`overlay_unshown`]).
|
||||
#[cfg(target_os = "linux")]
|
||||
fn ask_in_the_window(shell: &mut Shell, ids: Vec<u64>) -> Result<(), Vec<u64>> {
|
||||
let layout = shell.layout;
|
||||
if layout.drawable.0 == 0 || layout.drawable.1 == 0 {
|
||||
return Err(ids);
|
||||
}
|
||||
let question = super::question::Question::new(ids, layout.drawable, layout.scale);
|
||||
shell.window.surface().set_overlay(Some(question.overlay()));
|
||||
shell.os.question = Some(question);
|
||||
// SAFETY: the window is SDL's, on this thread.
|
||||
unsafe {
|
||||
if (SDL_GetWindowFlags(shell.sdl) & SDL_WINDOW_MINIMIZED).0 != 0 {
|
||||
SDL_RestoreWindow(shell.sdl);
|
||||
}
|
||||
SDL_RaiseWindow(shell.sdl);
|
||||
}
|
||||
shell.refresh_cursor();
|
||||
log::info!(
|
||||
target: "reims_vgpu_window",
|
||||
"window {:?}: the question before turning the VM off shows in the window",
|
||||
shell.window.title()
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// An input event of type `kind` for the question open in `shell`'s window: its keys, and the
|
||||
/// left button's clicks at their pixels of the drawable (`super::question`). A focus moved draws
|
||||
/// it again; an answer ends it ([`answered_in_the_window`]).
|
||||
#[cfg(target_os = "linux")]
|
||||
fn question_event(shell: &mut Shell, kind: u32, event: &SDL_Event) {
|
||||
use super::question::Step;
|
||||
let scale = shell.layout.scale;
|
||||
let Some(question) = shell.os.question.as_mut() else {
|
||||
return;
|
||||
};
|
||||
let step = match kind {
|
||||
k if k == SDL_EVENT_KEY_DOWN.0 || k == SDL_EVENT_KEY_UP.0 => {
|
||||
// SAFETY: a key event.
|
||||
let key = unsafe { event.key };
|
||||
keymap::usb_usage(key.scancode.0).map_or(Step::Nothing, |usage| {
|
||||
question.key(usage, key.down, key.repeat)
|
||||
})
|
||||
}
|
||||
k if k == SDL_EVENT_MOUSE_BUTTON_DOWN.0 || k == SDL_EVENT_MOUSE_BUTTON_UP.0 => {
|
||||
// SAFETY: a button event.
|
||||
let b = unsafe { event.button };
|
||||
if input::button_bit(b.button) == crate::config::BUTTON_LEFT {
|
||||
let point = (f64::from(b.x) * scale, f64::from(b.y) * scale);
|
||||
question.click(b.down, point)
|
||||
} else {
|
||||
Step::Nothing
|
||||
}
|
||||
}
|
||||
_ => Step::Nothing,
|
||||
};
|
||||
match step {
|
||||
Step::Nothing => {}
|
||||
Step::Redraw => shell.window.surface().set_overlay(Some(question.overlay())),
|
||||
Step::Answer(answer) => answered_in_the_window(shell, answer),
|
||||
}
|
||||
}
|
||||
|
||||
/// The person answered the question in `shell`'s window: it goes, the window's input goes on, and
|
||||
/// the answer goes to [`shell::answered`], posted, to run with no window borrowed.
|
||||
#[cfg(target_os = "linux")]
|
||||
fn answered_in_the_window(shell: &mut Shell, answer: Answer) {
|
||||
let Some(question) = shell.os.question.take() else {
|
||||
return;
|
||||
};
|
||||
shell.window.surface().set_overlay(None);
|
||||
shell.input.0.question_answered();
|
||||
shell.refresh_cursor();
|
||||
let ids = question.ids;
|
||||
super::ui::post(move || shell::answered(&ids, answer));
|
||||
}
|
||||
|
||||
/// Window `id`'s render thread can draw no more (its GPU device lost, or no swapchain), so the
|
||||
/// question open in it does not show (`super::render`): it is asked in SDL's message box instead
|
||||
/// ([`ask_in_a_dialog`]), the input held as it was.
|
||||
#[cfg(target_os = "linux")]
|
||||
pub(super) fn overlay_unshown(id: u64) {
|
||||
let ids = shell::with_shell(id, |s| {
|
||||
let question = s.os.question.take()?;
|
||||
s.window.surface().set_overlay(None);
|
||||
s.refresh_cursor();
|
||||
Some(question.ids)
|
||||
})
|
||||
.flatten();
|
||||
if let Some(ids) = ids {
|
||||
log::warn!(
|
||||
target: "reims_vgpu_window",
|
||||
"the window draws no more: the question before turning the VM off shows in SDL's \
|
||||
message box"
|
||||
);
|
||||
ask_in_a_dialog(id, ids);
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether a question is open in `shell`'s window, over which the host's arrow shows.
|
||||
#[cfg(target_os = "linux")]
|
||||
pub(crate) fn asks(shell: &Shell) -> bool {
|
||||
shell.os.question.is_some()
|
||||
}
|
||||
|
||||
/// Whether a question is open in the window: none on Windows, whose question is a dialog.
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
pub(crate) fn asks(_shell: &Shell) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
/// The window was laid out again: the question open in it is laid out for the new drawable and
|
||||
/// drawn again where that changed it.
|
||||
#[cfg(target_os = "linux")]
|
||||
pub(crate) fn laid_out(shell: &mut Shell) {
|
||||
if let Some(question) = shell.os.question.as_mut()
|
||||
&& question.relayout(shell.layout.drawable, shell.layout.scale)
|
||||
{
|
||||
shell.window.surface().set_overlay(Some(question.overlay()));
|
||||
}
|
||||
}
|
||||
|
||||
/// The window was laid out again: nothing more on Windows, whose question is a dialog.
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
pub(crate) fn laid_out(_shell: &mut Shell) {}
|
||||
|
||||
/// Asks the question for windows `ids` in SDL's message box over window `id`: on Linux zenity's
|
||||
/// question where zenity is installed, else SDL's own X11 dialog, which the window owns on X11 and
|
||||
/// under XWayland; on Windows a task dialog the window owns. Read from SDL 3.4.18's code: zenity's
|
||||
/// question is a window of its own, which SDL waits for, and on Wayland SDL tries nothing else.
|
||||
/// SDL's message box holds the thread it runs on until it is answered, the UI thread here, which
|
||||
/// made the window, as SDL asks: the other windows' events and the work posted wait meanwhile,
|
||||
/// while the render threads draw on. So it is posted, to run after the events at hand with no
|
||||
/// window borrowed; the answer goes to [`shell::answered`], after the window's input goes on. A
|
||||
/// window whose display ended before the question showed asks nothing.
|
||||
fn ask_in_a_dialog(id: u64, ids: Vec<u64>) {
|
||||
super::ui::post(move || {
|
||||
let parent = shell::with_shell(id, |s| (!s.closed).then_some(s.sdl)).flatten();
|
||||
let answer = match parent {
|
||||
@@ -482,7 +635,7 @@ pub(crate) fn ask(shell: &mut Shell, ids: Vec<u64>) {
|
||||
}
|
||||
|
||||
/// Shows the question over `parent`, and waits for its answer ([`answer_of`]). Where SDL shows
|
||||
/// none (no zenity and no X server on Linux), that is logged, and the close goes ahead.
|
||||
/// none (on Linux no zenity on Wayland, or no X server), that is logged, and the VM runs on.
|
||||
fn question(parent: *mut SDL_Window) -> Answer {
|
||||
let title = c_string(close::QUESTION);
|
||||
let message = c_string(close::DETAIL);
|
||||
@@ -505,7 +658,7 @@ fn question(parent: *mut SDL_Window) -> Answer {
|
||||
if !shown {
|
||||
log::warn!(
|
||||
target: "reims_vgpu_window",
|
||||
"the question before turning the VM off does not show ({}): the close goes ahead",
|
||||
"the question before turning the VM off does not show ({}): the VM runs on",
|
||||
sdl_error()
|
||||
);
|
||||
}
|
||||
@@ -544,9 +697,15 @@ fn answer_of(shown: bool, chosen: c_int) -> Answer {
|
||||
}
|
||||
}
|
||||
|
||||
/// The display ended: the window hides. SDL's window goes once the render thread has let go
|
||||
/// (`crate::sdl::shell::released`), never before.
|
||||
/// The display ended: the window hides, and a question open in it ends as cancelled. SDL's
|
||||
/// window goes once the render thread has let go (`crate::sdl::shell::released`), never before.
|
||||
pub(crate) fn close(shell: &mut Shell) -> bool {
|
||||
#[cfg(target_os = "linux")]
|
||||
if let Some(question) = shell.os.question.take() {
|
||||
shell.window.surface().set_overlay(None);
|
||||
let ids = question.ids;
|
||||
super::ui::post(move || shell::answered(&ids, Answer::Cancel));
|
||||
}
|
||||
// SAFETY: the window is SDL's, on this thread.
|
||||
unsafe { SDL_HideWindow(shell.sdl) };
|
||||
false
|
||||
|
||||
@@ -22,7 +22,7 @@ use std::time::Duration;
|
||||
|
||||
use ash::vk;
|
||||
|
||||
use crate::handle::{DrawnCursor, PixelRect};
|
||||
use crate::handle::{DrawnCursor, DrawnOverlay, PixelRect};
|
||||
|
||||
/// How long the render thread waits before it draws again where no image came in time: about a
|
||||
/// refresh at 120 Hz. Before it answers [`Drawn::Retry`] for want of an image, the presenter has
|
||||
@@ -58,14 +58,16 @@ pub enum Drawn {
|
||||
pub trait Presenter: Send {
|
||||
/// Draws the display's current frame into the next image of `drawable` pixels (width,
|
||||
/// height), `scale` of them a point of the window's screen, over `region` of it, clearing the
|
||||
/// rest, with `cursor` over it where the window draws the guest's cursor, and presents it.
|
||||
/// Blocks only for the next image, up to a few frames.
|
||||
/// rest, with `cursor` over it where the window draws the guest's cursor, and `overlay` over
|
||||
/// all of it where the window shows one (black under it where the display has nothing to
|
||||
/// draw), and presents it. Blocks only for the next image, up to a few frames.
|
||||
fn draw(
|
||||
&mut self,
|
||||
drawable: (u64, u64),
|
||||
scale: f64,
|
||||
region: PixelRect,
|
||||
cursor: Option<&DrawnCursor>,
|
||||
overlay: Option<&DrawnOverlay>,
|
||||
) -> Drawn;
|
||||
}
|
||||
|
||||
@@ -128,15 +130,29 @@ impl Presenting {
|
||||
self.ended |= self.presenter.is_none();
|
||||
}
|
||||
|
||||
/// Whether the window stays as it is, drawing no more: the sink made no presenter, the
|
||||
/// presenters ended too many times in a row, or the presenter's device is lost.
|
||||
#[cfg_attr(
|
||||
not(target_os = "linux"),
|
||||
expect(
|
||||
dead_code,
|
||||
reason = "only Linux's window draws an overlay it must know shows"
|
||||
)
|
||||
)]
|
||||
pub(crate) fn is_ended(&self) -> bool {
|
||||
self.ended
|
||||
}
|
||||
|
||||
/// Has the presenter draw the current frame into an image of `drawable` pixels, `scale` of
|
||||
/// them a point, over `region`, with `cursor` over it: whether to draw again after [`RETRY`]
|
||||
/// (no image came in time). Nothing without a presenter.
|
||||
/// them a point, over `region`, with `cursor` and `overlay` over it: whether to draw again
|
||||
/// after [`RETRY`] (no image came in time). Nothing without a presenter.
|
||||
pub(crate) fn draw(
|
||||
&mut self,
|
||||
drawable: (u64, u64),
|
||||
scale: f64,
|
||||
region: PixelRect,
|
||||
cursor: Option<&DrawnCursor>,
|
||||
overlay: Option<&DrawnOverlay>,
|
||||
) -> bool {
|
||||
if self.ended {
|
||||
return false;
|
||||
@@ -144,7 +160,7 @@ impl Presenting {
|
||||
let Some(presenter) = self.presenter.as_mut() else {
|
||||
return false;
|
||||
};
|
||||
let drawn = presenter.draw(drawable, scale, region, cursor);
|
||||
let drawn = presenter.draw(drawable, scale, region, cursor, overlay);
|
||||
if drawn == Drawn::Lost {
|
||||
// Kept to the window's end, asked no more, and no other made (the module doc, "A lost
|
||||
// device").
|
||||
@@ -198,6 +214,7 @@ mod tests {
|
||||
_scale: f64,
|
||||
_region: PixelRect,
|
||||
_cursor: Option<&DrawnCursor>,
|
||||
_overlay: Option<&DrawnOverlay>,
|
||||
) -> Drawn {
|
||||
self.0.pop_front().unwrap_or(Drawn::Presented)
|
||||
}
|
||||
@@ -217,6 +234,7 @@ mod tests {
|
||||
_scale: f64,
|
||||
_region: PixelRect,
|
||||
_cursor: Option<&DrawnCursor>,
|
||||
_overlay: Option<&DrawnOverlay>,
|
||||
) -> Drawn {
|
||||
self.draws.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
|
||||
Drawn::Lost
|
||||
@@ -268,16 +286,16 @@ mod tests {
|
||||
assert!(p.needs_presenter());
|
||||
p.make(|| scripted(&[Drawn::Retry, Drawn::Presented, Drawn::Ended]));
|
||||
assert!(!p.needs_presenter());
|
||||
assert!(p.draw((4, 4), 1.0, REGION, None), "no image: a retry");
|
||||
assert!(!p.draw((4, 4), 1.0, REGION, None));
|
||||
assert!(!p.draw((4, 4), 1.0, REGION, None));
|
||||
assert!(p.draw((4, 4), 1.0, REGION, None, None), "no image: a retry");
|
||||
assert!(!p.draw((4, 4), 1.0, REGION, None, None));
|
||||
assert!(!p.draw((4, 4), 1.0, REGION, None, None));
|
||||
assert!(p.needs_presenter(), "an ended presenter is made again");
|
||||
p.make(|| None);
|
||||
assert!(
|
||||
!p.needs_presenter(),
|
||||
"no presenter: the window stays as it is"
|
||||
);
|
||||
assert!(!p.draw((4, 4), 1.0, REGION, None));
|
||||
assert!(!p.draw((4, 4), 1.0, REGION, None, None));
|
||||
}
|
||||
|
||||
/// A presenter whose device is lost is kept to the window's end, so the window keeps the frame
|
||||
@@ -294,10 +312,10 @@ mod tests {
|
||||
dropped: Arc::clone(&dropped),
|
||||
};
|
||||
p.make(|| Some(Box::new(losing)));
|
||||
assert!(!p.draw((4, 4), 1.0, REGION, None), "no retry");
|
||||
assert!(!p.draw((4, 4), 1.0, REGION, None, None), "no retry");
|
||||
for _ in 0..100 {
|
||||
assert!(!p.needs_presenter(), "none made again");
|
||||
assert!(!p.draw((4, 4), 1.0, REGION, None));
|
||||
assert!(!p.draw((4, 4), 1.0, REGION, None, None));
|
||||
}
|
||||
assert_eq!(draws.load(Ordering::SeqCst), 1, "asked once, at the loss");
|
||||
assert!(
|
||||
@@ -315,7 +333,7 @@ mod tests {
|
||||
for _ in 0..=RENEWALS {
|
||||
assert!(p.needs_presenter());
|
||||
p.make(|| scripted(&[Drawn::Ended]));
|
||||
p.draw((4, 4), 1.0, REGION, None);
|
||||
p.draw((4, 4), 1.0, REGION, None, None);
|
||||
}
|
||||
assert!(!p.needs_presenter());
|
||||
}
|
||||
|
||||
@@ -474,6 +474,7 @@ impl Shell {
|
||||
}
|
||||
}
|
||||
self.window.surface().set_layout(layout);
|
||||
os::laid_out(self);
|
||||
}
|
||||
|
||||
/// The pointer moved to `point` (in points): the cursor changes as it enters or leaves the
|
||||
@@ -487,15 +488,17 @@ impl Shell {
|
||||
}
|
||||
|
||||
/// Sets the cursor: over the image the guest's, or none while the guest hides its own or the
|
||||
/// window draws it itself (`handle`'s module doc); the host's arrow elsewhere and before the
|
||||
/// guest has set one. SDL's cursor is the process's, so only the window the pointer is in
|
||||
/// (SDL's mouse focus) sets it ([`cursor_step`]).
|
||||
/// window draws it itself (`handle`'s module doc); the host's arrow elsewhere, before the
|
||||
/// guest has set one, and while a question is open in the window ([`os::asks`]). SDL's cursor
|
||||
/// is the process's, so only the window the pointer is in (SDL's mouse focus) sets it
|
||||
/// ([`cursor_step`]).
|
||||
pub(crate) fn refresh_cursor(&mut self) {
|
||||
let guest = self.window.guest();
|
||||
// SAFETY: SDL's video runs on this thread.
|
||||
let has_pointer = unsafe { SDL_GetMouseFocus() } == self.sdl;
|
||||
let hidden = !guest.host_cursor_visible() && guest.cursor.is_some();
|
||||
let ours = match cursor_step(has_pointer, self.over_image, hidden) {
|
||||
let over_image = self.over_image && !os::asks(self);
|
||||
let ours = match cursor_step(has_pointer, over_image, hidden) {
|
||||
CursorStep::Leave => return,
|
||||
CursorStep::Hide => {
|
||||
// SAFETY: SDL's video runs on this thread.
|
||||
@@ -685,8 +688,8 @@ fn let_go_for_close(c: &mut impl Closing) {
|
||||
}
|
||||
}
|
||||
|
||||
/// The person answered the question a close asked ([`close_asked`]) for windows `ids`: Turn Off,
|
||||
/// or a question the OS could not show, tells each one's host, which ends the VM; Cancel tells
|
||||
/// The person answered the question a close asked ([`close_asked`]) for windows `ids`: Turn Off
|
||||
/// tells each one's host, which ends the VM; Cancel, or a question that could not show, tells
|
||||
/// none, and the VM runs on (`crate::close::answered`). A window whose display has ended tells
|
||||
/// nothing: its host's callbacks are gone.
|
||||
pub(crate) fn answered(ids: &[u64], answer: Answer) {
|
||||
|
||||
@@ -31,7 +31,9 @@ use reims_vgpu_vulkan::core::raise::OnFault;
|
||||
use reims_vgpu_vulkan::rt_platform::swapchain::RtSwapchain;
|
||||
use reims_vgpu_vulkan::rt_platform::window_times;
|
||||
use reims_vgpu_vulkan::swapchain::{Acquired, Refused};
|
||||
use reims_vgpu_window::{Drawn, DrawnCursor, FrameSink, PixelRect, Presenter, VulkanTarget};
|
||||
use reims_vgpu_window::{
|
||||
Drawn, DrawnCursor, DrawnOverlay, FrameSink, PixelRect, Presenter, VulkanTarget,
|
||||
};
|
||||
|
||||
use super::sink::ScanoutDraw;
|
||||
use super::{Presented, Shown};
|
||||
@@ -153,7 +155,8 @@ struct RtPresenter {
|
||||
|
||||
impl Presenter for RtPresenter {
|
||||
/// The next image, the display drawn into it, with `cursor` drawn over it where the window
|
||||
/// draws the guest's cursor (`sink`'s module doc, "The guest's cursor"), and its present once
|
||||
/// draws the guest's cursor (`sink`'s module doc, "The guest's cursor") and `overlay` over all
|
||||
/// of it where the window shows one ("The window's overlay"), and its present once
|
||||
/// rt has scheduled the draw; nothing, the image kept for the next draw, where the draw is
|
||||
/// refused (`sink`'s module doc, "A refused draw"), or rt refuses its present or its commit.
|
||||
fn draw(
|
||||
@@ -162,6 +165,7 @@ impl Presenter for RtPresenter {
|
||||
scale: f64,
|
||||
region: PixelRect,
|
||||
cursor: Option<&DrawnCursor>,
|
||||
overlay: Option<&DrawnOverlay>,
|
||||
) -> Drawn {
|
||||
entry::run(|_| {
|
||||
self.draw.drawn(drawable, scale);
|
||||
@@ -177,7 +181,8 @@ impl Presenter for RtPresenter {
|
||||
};
|
||||
let swapchain = &self.swapchain;
|
||||
let committed = self.draw.scoped(|| {
|
||||
let (command_buffer, presents) = self.draw.encode(texture, region, cursor)?;
|
||||
let (command_buffer, presents) =
|
||||
self.draw.encode(texture, region, cursor, overlay)?;
|
||||
// A draw dropped here answers its presents at the next refresh.
|
||||
if !swapchain.encode_ready(frame, &command_buffer) {
|
||||
return None;
|
||||
|
||||
@@ -52,6 +52,12 @@
|
||||
//! of its own after the frame's, in the frame's command buffer (`cursor`). The host's reads of
|
||||
//! the display draw no cursor.
|
||||
//!
|
||||
//! **The window's overlay.** Where the window shows an overlay (on Linux the question a close
|
||||
//! asks), the draw puts its image over all of that, the cursor included, a pixel to a pixel of
|
||||
//! the drawable, in a pass of the same kind (`cursor`), while the display's panel is off too.
|
||||
//! Where the display has nothing to draw (no scanout and no surface, or its draw refused), the
|
||||
//! overlay is drawn over black, so that it always shows. The host's reads draw no overlay.
|
||||
//!
|
||||
//! **A display the guest does not drive.** The window of a display after display 0
|
||||
//! ([`Shown`]'s `black_undriven`) draws black while the guest shows nothing on it, no scanout and
|
||||
//! no presented surface: a guest whose driver drives display 0 alone leaves the others so, and a
|
||||
@@ -135,11 +141,11 @@ use reims_vgpu_vulkan::core::platform::{Handle, HostRange, Platform};
|
||||
use reims_vgpu_vulkan::core::raise::{OnFault, RaiseResult, refusals};
|
||||
use reims_vgpu_vulkan::core::sync::Mutex;
|
||||
use reims_vgpu_vulkan::os::OsVm;
|
||||
use reims_vgpu_window::{DrawnCursor, PixelRect};
|
||||
use reims_vgpu_window::{DrawnCursor, DrawnOverlay, PixelRect};
|
||||
|
||||
use reims_vgpu_vulkan::core::display::pipeline::{DisplayPipelineCache, PipelineKey};
|
||||
use reims_vgpu_vulkan::core::platform::resource::Buffer as _;
|
||||
use reims_vgpu_vulkan::core::platform::types::MtlViewport;
|
||||
use reims_vgpu_vulkan::core::platform::types::{MtlScissorRect, MtlViewport};
|
||||
|
||||
use self::cursor::CursorPass;
|
||||
use super::color::DisplayColor;
|
||||
@@ -234,6 +240,9 @@ pub(crate) struct ScanoutDraw<P: Platform> {
|
||||
/// What draws the guest's cursor where the window draws it (`cursor`): its pipelines and its
|
||||
/// glyph's texture, made at their first use.
|
||||
cursor: Mutex<CursorPass<P>>,
|
||||
/// What draws the window's overlay (the module doc): its pipelines and its image's texture,
|
||||
/// made at their first use.
|
||||
overlay: Mutex<CursorPass<P>>,
|
||||
/// For the tests: every colour takes the pass, with no table.
|
||||
#[cfg(test)]
|
||||
tables_off: AtomicBool,
|
||||
@@ -557,7 +566,8 @@ impl<P: ApiPlatform> ScanoutDraw<P> {
|
||||
color_table: Mutex::new(None),
|
||||
ramps: Mutex::default(),
|
||||
scanout_ramp: Kept::new(),
|
||||
cursor: Mutex::default(),
|
||||
cursor: Mutex::new(CursorPass::cursor()),
|
||||
overlay: Mutex::new(CursorPass::overlay()),
|
||||
#[cfg(test)]
|
||||
tables_off: AtomicBool::new(false),
|
||||
}
|
||||
@@ -570,7 +580,8 @@ impl<P: ApiPlatform> ScanoutDraw<P> {
|
||||
}
|
||||
|
||||
/// Lets go of the objects the draws made and keep (the converter, the colour's functions,
|
||||
/// pipelines, buffers and table, the ramps, and the cursor's pipelines and glyph texture)
|
||||
/// pipelines, buffers and table, the ramps, and the cursor's and the overlay's pipelines and
|
||||
/// textures)
|
||||
/// where the device recovered since they were made ([`GpuDevice::recoveries`], the module
|
||||
/// doc, "A recovered device"): the next draw that needs one makes it again on the device's
|
||||
/// new GPU device. The scanout's import is a no-copy buffer over the region's pages, which a
|
||||
@@ -585,7 +596,7 @@ impl<P: ApiPlatform> ScanoutDraw<P> {
|
||||
log::info!(
|
||||
target: "reims_vgpu::graphics",
|
||||
"the device recovered: the display's draws make their pipelines, buffers, tables, \
|
||||
ramps and cursor again"
|
||||
ramps, cursor and overlay again"
|
||||
);
|
||||
self.converter.forget();
|
||||
self.color_functions.forget();
|
||||
@@ -594,7 +605,8 @@ impl<P: ApiPlatform> ScanoutDraw<P> {
|
||||
*self.color_table.lock() = None;
|
||||
self.ramps.lock().clear();
|
||||
self.scanout_ramp.forget();
|
||||
*self.cursor.lock() = CursorPass::default();
|
||||
*self.cursor.lock() = CursorPass::cursor();
|
||||
*self.overlay.lock() = CursorPass::overlay();
|
||||
}
|
||||
|
||||
/// Runs `draw`, a draw of the display, in a refusal scope (the module doc, "A refused draw"):
|
||||
@@ -698,7 +710,9 @@ impl<P: ApiPlatform> ScanoutDraw<P> {
|
||||
///
|
||||
/// Where the window draws the guest's cursor itself (`cursor`, the guest moving it on its
|
||||
/// own), `cursor` is drawn over what the display shows, in a pass of its own after it
|
||||
/// ([`Self::encode_cursor`]); never while the display's panel is off.
|
||||
/// ([`Self::encode_cursor`]); never while the display's panel is off. Where the window shows
|
||||
/// an overlay, `overlay` is drawn over all of it, the panel off too, and over black where the
|
||||
/// display has nothing to draw ([`Self::encode_overlay`], the module doc).
|
||||
///
|
||||
/// It runs in the draw's refusal scope ([`Self::scoped`]): `None` too where a death in it
|
||||
/// refused it, nothing committed, and its presents are answered at the next refresh.
|
||||
@@ -707,10 +721,21 @@ impl<P: ApiPlatform> ScanoutDraw<P> {
|
||||
texture: &P::Texture,
|
||||
region: PixelRect,
|
||||
cursor: Option<&DrawnCursor>,
|
||||
overlay: Option<&DrawnOverlay>,
|
||||
) -> Option<(P::CommandBuffer, DrawnPresents<P>)> {
|
||||
self.scoped(|| {
|
||||
let mut presents = self.presented.drawn();
|
||||
let (command_buffer, hold) = self.encode_shown(texture, region)?;
|
||||
let shown = match self.encode_shown(texture, region) {
|
||||
Some(shown) => shown,
|
||||
None if overlay.is_some() => {
|
||||
// Nothing of the display's: the overlay over black (the module doc).
|
||||
let command_buffer = no_catch(self.queue.command_buffer(OnFault::Die))?;
|
||||
self.clear(&command_buffer, texture);
|
||||
(command_buffer, None)
|
||||
}
|
||||
None => return None,
|
||||
};
|
||||
let (command_buffer, hold) = shown;
|
||||
if let Some(hold) = hold {
|
||||
// The surface's block stays until the draw has read it.
|
||||
presents.reading(hold);
|
||||
@@ -720,6 +745,9 @@ impl<P: ApiPlatform> ScanoutDraw<P> {
|
||||
{
|
||||
self.encode_cursor(&command_buffer, texture, region, cursor);
|
||||
}
|
||||
if let Some(overlay) = overlay {
|
||||
self.encode_overlay(&command_buffer, texture, overlay);
|
||||
}
|
||||
Some((command_buffer, presents))
|
||||
})
|
||||
}
|
||||
@@ -734,17 +762,59 @@ impl<P: ApiPlatform> ScanoutDraw<P> {
|
||||
texture: &P::Texture,
|
||||
region: PixelRect,
|
||||
cursor: &DrawnCursor,
|
||||
) {
|
||||
let size = (texture.width(), texture.height());
|
||||
let Some(placed) = cursor::placement(region, size, cursor) else {
|
||||
return;
|
||||
};
|
||||
self.encode_glyph(command_buffer, texture, &self.cursor, placed, &cursor.glyph);
|
||||
}
|
||||
|
||||
/// The window's `overlay` drawn into `texture` over what `command_buffer` drew there, a pixel
|
||||
/// to a pixel at its place, in a render pass of its own as the cursor's
|
||||
/// ([`Self::encode_cursor`]); nothing where it would not show, or where the platform refuses
|
||||
/// the converter, the pipeline or the texture.
|
||||
fn encode_overlay(
|
||||
&self,
|
||||
command_buffer: &P::CommandBuffer,
|
||||
texture: &P::Texture,
|
||||
overlay: &DrawnOverlay,
|
||||
) {
|
||||
let size = (texture.width(), texture.height());
|
||||
let Some(placed) = cursor::overlay_placement(size, overlay) else {
|
||||
return;
|
||||
};
|
||||
self.encode_glyph(
|
||||
command_buffer,
|
||||
texture,
|
||||
&self.overlay,
|
||||
placed,
|
||||
&overlay.image,
|
||||
);
|
||||
}
|
||||
|
||||
/// `glyph` drawn into `texture` at `placed` (its viewport and scissor) with what `kept` keeps
|
||||
/// from frame to frame, in a render pass of its own that loads it, with the converter's strip
|
||||
/// and the kept pipeline and glyph's texture (`cursor`'s module doc); nothing where the
|
||||
/// platform refuses the converter, the pipeline or the texture.
|
||||
fn encode_glyph(
|
||||
&self,
|
||||
command_buffer: &P::CommandBuffer,
|
||||
texture: &P::Texture,
|
||||
kept: &Mutex<CursorPass<P>>,
|
||||
placed: (MtlViewport, MtlScissorRect),
|
||||
glyph: &Arc<reims_vgpu_window::Glyph>,
|
||||
) {
|
||||
let Some(converter) = self.converter() else {
|
||||
return;
|
||||
};
|
||||
let prepared = self.cursor.lock().prepare(
|
||||
let prepared = kept.lock().prepare(
|
||||
&self.platform,
|
||||
&self.device,
|
||||
&converter,
|
||||
texture,
|
||||
region,
|
||||
cursor,
|
||||
placed,
|
||||
glyph,
|
||||
);
|
||||
let Some(prepared) = prepared else {
|
||||
return;
|
||||
@@ -1611,7 +1681,6 @@ impl<P: ApiPlatform> ScanoutDraw<P> {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use reims_vgpu_vulkan::core::platform::ObjId;
|
||||
use reims_vgpu_vulkan::core::platform::types::MtlScissorRect;
|
||||
use reims_vgpu_vulkan::core::testkit::calllog::Call;
|
||||
use reims_vgpu_vulkan::core::testkit::objects::{CompleteOn, kind};
|
||||
use reims_vgpu_vulkan::core::testkit::{Arg, FakePlatform};
|
||||
@@ -1717,7 +1786,7 @@ mod tests {
|
||||
let (platform, draw, presented, drawable) = rig();
|
||||
let surface = texture(&draw, 80);
|
||||
let hold = show_held(&presented, &surface, queued);
|
||||
let (command_buffer, presents) = draw.encode(&drawable, WHOLE, None).unwrap();
|
||||
let (command_buffer, presents) = draw.encode(&drawable, WHOLE, None, None).unwrap();
|
||||
assert_eq!(hold.strong_count(), 2, "the draw holds the surface");
|
||||
presents.after(&command_buffer);
|
||||
no_catch(command_buffer.commit(OnFault::Die));
|
||||
@@ -1798,7 +1867,7 @@ mod tests {
|
||||
});
|
||||
let log = platform.call_log();
|
||||
let mark = log.mark();
|
||||
let _ = draw.encode(&drawable, WHOLE, None).unwrap();
|
||||
let _ = draw.encode(&drawable, WHOLE, None, None).unwrap();
|
||||
let calls = log.since(mark);
|
||||
assert!(
|
||||
!calls
|
||||
@@ -1842,7 +1911,7 @@ mod tests {
|
||||
let cursor = drawn_cursor();
|
||||
|
||||
let mark = log.mark();
|
||||
let _ = draw.encode(&drawable, WHOLE, Some(&cursor)).unwrap();
|
||||
let _ = draw.encode(&drawable, WHOLE, Some(&cursor), None).unwrap();
|
||||
let calls = log.since(mark);
|
||||
let sels: Vec<_> = calls.iter().map(|c| c.sel).collect();
|
||||
let blit = sels.iter().position(|s| *s == COPY_IN).expect("the blit");
|
||||
@@ -1898,7 +1967,7 @@ mod tests {
|
||||
|
||||
// The same glyph again: nothing made.
|
||||
let mark = log.mark();
|
||||
let _ = draw.encode(&drawable, WHOLE, Some(&cursor)).unwrap();
|
||||
let _ = draw.encode(&drawable, WHOLE, Some(&cursor), None).unwrap();
|
||||
for made in [NEW_PIPELINE, NEW_TEXTURE, REPLACE] {
|
||||
assert_eq!(count_since(&platform, mark, made), 0, "{made}");
|
||||
}
|
||||
@@ -1911,7 +1980,7 @@ mod tests {
|
||||
..drawn_cursor()
|
||||
};
|
||||
let mark = log.mark();
|
||||
let _ = draw.encode(&drawable, WHOLE, Some(&moved)).unwrap();
|
||||
let _ = draw.encode(&drawable, WHOLE, Some(&moved), None).unwrap();
|
||||
assert_eq!(count_since(&platform, mark, NEW_PIPELINE), 0);
|
||||
assert_eq!(count_since(&platform, mark, NEW_TEXTURE), 1);
|
||||
assert_eq!(count_since(&platform, mark, REPLACE), 1);
|
||||
@@ -1919,16 +1988,125 @@ mod tests {
|
||||
|
||||
// No cursor, and the panel off.
|
||||
let mark = log.mark();
|
||||
let _ = draw.encode(&drawable, WHOLE, None).unwrap();
|
||||
let _ = draw.encode(&drawable, WHOLE, None, None).unwrap();
|
||||
assert_eq!(count_since(&platform, mark, BEGIN), 0);
|
||||
draw.shown
|
||||
.asleep
|
||||
.store(true, std::sync::atomic::Ordering::Release);
|
||||
let mark = log.mark();
|
||||
let _ = draw.encode(&drawable, WHOLE, Some(&moved)).unwrap();
|
||||
let _ = draw.encode(&drawable, WHOLE, Some(&moved), None).unwrap();
|
||||
assert_eq!(count_since(&platform, mark, DRAW), 0);
|
||||
}
|
||||
|
||||
/// The window's overlay is drawn over the frame and the cursor, in a pass of its own after
|
||||
/// both: a blending pipeline of its own, made once; its image's texture, made and filled once
|
||||
/// for each image; its rectangle at its place, a pixel to a pixel, as the viewport, and the
|
||||
/// scissor. It is drawn while the panel is off too, where the cursor is not, and where the
|
||||
/// display has nothing to draw, over a clear of the drawable
|
||||
#[test]
|
||||
fn the_window_s_overlay_is_drawn_over_everything_in_a_pass_of_its_own() {
|
||||
const BEGIN: &str = "renderCommandEncoderWithDescriptor:";
|
||||
let (platform, draw, _, drawable) = rig();
|
||||
let log = platform.call_log();
|
||||
let cursor = drawn_cursor();
|
||||
let overlay = DrawnOverlay {
|
||||
image: Arc::new(reims_vgpu_window::Glyph {
|
||||
width: 8,
|
||||
height: 4,
|
||||
hot_spot: (0, 0),
|
||||
rgba: vec![0xff; 128],
|
||||
}),
|
||||
at: (2, 3),
|
||||
};
|
||||
|
||||
let mark = log.mark();
|
||||
let _ = draw
|
||||
.encode(&drawable, WHOLE, Some(&cursor), Some(&overlay))
|
||||
.unwrap();
|
||||
let calls = log.since(mark);
|
||||
let sels: Vec<_> = calls.iter().map(|c| c.sel).collect();
|
||||
let passes: Vec<_> = (sels.iter().enumerate())
|
||||
.filter(|(_, s)| **s == BEGIN)
|
||||
.map(|(at, _)| at)
|
||||
.collect();
|
||||
let blit = sels.iter().position(|s| *s == COPY_IN).expect("the blit");
|
||||
assert_eq!(
|
||||
passes.len(),
|
||||
2,
|
||||
"the cursor's, then the overlay's: {sels:?}"
|
||||
);
|
||||
assert!(blit < passes[0]);
|
||||
let pipelines: Vec<_> = (calls.iter())
|
||||
.filter(|c| c.sel == NEW_PIPELINE)
|
||||
.map(|c| format!("{:?}", c.args[0]))
|
||||
.collect();
|
||||
assert_eq!(pipelines.len(), 2, "{pipelines:?}");
|
||||
assert!(pipelines[0].contains("reims-vgpu.display-cursor"));
|
||||
assert!(pipelines[1].contains("reims-vgpu.display-overlay"));
|
||||
assert!(pipelines[1].contains("blending_enabled: true"));
|
||||
assert_eq!(count_since(&platform, mark, NEW_TEXTURE), 2);
|
||||
assert_eq!(count_since(&platform, mark, REPLACE), 2);
|
||||
let arg = |sel: &str| {
|
||||
(calls.iter())
|
||||
.filter(|c| c.sel == sel)
|
||||
.map(|c| c.args[0].clone())
|
||||
.collect::<Vec<_>>()
|
||||
};
|
||||
assert_eq!(
|
||||
arg("setViewport:")[1],
|
||||
Arg::val(&MtlViewport {
|
||||
origin_x: 2.0,
|
||||
origin_y: 3.0,
|
||||
width: 8.0,
|
||||
height: 4.0,
|
||||
znear: 0.0,
|
||||
zfar: 1.0,
|
||||
})
|
||||
);
|
||||
assert_eq!(
|
||||
arg("setScissorRect:")[1],
|
||||
Arg::val(&MtlScissorRect {
|
||||
x: 2,
|
||||
y: 3,
|
||||
width: 8,
|
||||
height: 4,
|
||||
})
|
||||
);
|
||||
assert_eq!(count_since(&platform, mark, DRAW), 2);
|
||||
|
||||
// The same image again: nothing made.
|
||||
let mark = log.mark();
|
||||
let _ = draw.encode(&drawable, WHOLE, None, Some(&overlay)).unwrap();
|
||||
for made in [NEW_PIPELINE, NEW_TEXTURE, REPLACE] {
|
||||
assert_eq!(count_since(&platform, mark, made), 0, "{made}");
|
||||
}
|
||||
assert_eq!(count_since(&platform, mark, DRAW), 1);
|
||||
|
||||
// The panel off: the overlay over black, with no cursor.
|
||||
draw.shown
|
||||
.asleep
|
||||
.store(true, std::sync::atomic::Ordering::Release);
|
||||
let mark = log.mark();
|
||||
let _ = draw
|
||||
.encode(&drawable, WHOLE, Some(&cursor), Some(&overlay))
|
||||
.unwrap();
|
||||
assert_eq!(count_since(&platform, mark, DRAW), 1);
|
||||
|
||||
// Nothing of the display's to draw: a clear, then the overlay; with no overlay, nothing.
|
||||
let region = Arc::new(Region::new(reims_vgpu_graphics::REGION_SIZE as usize).unwrap());
|
||||
let presented = Arc::new(Presented::<FakePlatform>::default());
|
||||
let empty = Arc::new(Shown::default());
|
||||
let draw = ScanoutDraw::new(Arc::clone(&platform), ®ion, &empty, &presented).unwrap();
|
||||
assert!(draw.encode(&drawable, WHOLE, None, None).is_none());
|
||||
let mark = log.mark();
|
||||
let _ = draw
|
||||
.encode(&drawable, WHOLE, None, Some(&overlay))
|
||||
.expect("a draw");
|
||||
let sels = log.sels_since(mark);
|
||||
assert_eq!(sels.iter().filter(|s| **s == BEGIN).count(), 2, "{sels:?}");
|
||||
assert_eq!(count_since(&platform, mark, DRAW), 1);
|
||||
}
|
||||
|
||||
/// A death in the cursor's pass refuses that draw alone: the pass's encoder is ended after
|
||||
/// it, nothing is committed, and the next draw draws the frame and the cursor and commits
|
||||
#[test]
|
||||
@@ -1939,7 +2117,7 @@ mod tests {
|
||||
let cursor = drawn_cursor();
|
||||
platform.script().push(DRAW, Step::crash());
|
||||
let mark = log.mark();
|
||||
assert!(draw.encode(&drawable, WHOLE, Some(&cursor)).is_none());
|
||||
assert!(draw.encode(&drawable, WHOLE, Some(&cursor), None).is_none());
|
||||
let sels = log.sels_since(mark);
|
||||
let died = sels.iter().position(|s| *s == DRAW).expect("the draw");
|
||||
assert_eq!(&sels[died + 1..], ["endEncoding"], "{sels:?}");
|
||||
@@ -1947,7 +2125,7 @@ mod tests {
|
||||
|
||||
let mark = log.mark();
|
||||
let (command_buffer, presents) =
|
||||
(draw.encode(&drawable, WHOLE, Some(&cursor))).expect("a draw");
|
||||
(draw.encode(&drawable, WHOLE, Some(&cursor), None)).expect("a draw");
|
||||
presents.after(&command_buffer);
|
||||
command_buffer.commit(OnFault::Die).unwrap();
|
||||
let sels = log.sels_since(mark);
|
||||
@@ -2040,7 +2218,7 @@ mod tests {
|
||||
width: u64::from(width),
|
||||
height: u64::from(height),
|
||||
};
|
||||
let (command_buffer, presents) = draw.encode(&target, image, Some(&cursor)).unwrap();
|
||||
let (command_buffer, presents) = draw.encode(&target, image, Some(&cursor), None).unwrap();
|
||||
command_buffer.commit(OnFault::Die).unwrap();
|
||||
command_buffer.wait_until_completed(OnFault::Die).unwrap();
|
||||
drop(presents);
|
||||
@@ -2178,7 +2356,7 @@ mod tests {
|
||||
}
|
||||
platform.script().push(at, Step::crash());
|
||||
let mark = log.mark();
|
||||
assert!(draw.encode(&drawable, region, None).is_none());
|
||||
assert!(draw.encode(&drawable, region, None, None).is_none());
|
||||
let sels = log.sels_since(mark);
|
||||
let died = sels
|
||||
.iter()
|
||||
@@ -2188,7 +2366,8 @@ mod tests {
|
||||
assert!(!sels.contains(&"commit"));
|
||||
|
||||
let mark = log.mark();
|
||||
let (command_buffer, presents) = draw.encode(&drawable, region, None).expect("a draw");
|
||||
let (command_buffer, presents) =
|
||||
draw.encode(&drawable, region, None, None).expect("a draw");
|
||||
presents.after(&command_buffer);
|
||||
command_buffer.commit(OnFault::Die).unwrap();
|
||||
let sels = log.sels_since(mark);
|
||||
@@ -2238,7 +2417,7 @@ mod tests {
|
||||
platform.script().push(at, Step::crash());
|
||||
let mark = log.mark();
|
||||
assert!(
|
||||
draw.encode(&drawable, region, None).is_none(),
|
||||
draw.encode(&drawable, region, None, None).is_none(),
|
||||
"{at} {before}"
|
||||
);
|
||||
let sels = log.sels_since(mark);
|
||||
@@ -2261,7 +2440,8 @@ mod tests {
|
||||
assert!(!sels.contains(&"commit"), "{sels:?}");
|
||||
|
||||
let mark = log.mark();
|
||||
let (command_buffer, presents) = draw.encode(&drawable, region, None).expect("a draw");
|
||||
let (command_buffer, presents) =
|
||||
draw.encode(&drawable, region, None, None).expect("a draw");
|
||||
presents.after(&command_buffer);
|
||||
command_buffer.commit(OnFault::Die).unwrap();
|
||||
let sels = log.sels_since(mark);
|
||||
@@ -2291,7 +2471,7 @@ mod tests {
|
||||
};
|
||||
|
||||
let mark = log.mark();
|
||||
let _ = draw.encode(&drawable, region, None).unwrap();
|
||||
let _ = draw.encode(&drawable, region, None, None).unwrap();
|
||||
let blits: Vec<_> = log
|
||||
.since(mark)
|
||||
.into_iter()
|
||||
@@ -2309,7 +2489,7 @@ mod tests {
|
||||
set_color(&presented, 1, [None, None, None, Some(Arc::clone(&post))]);
|
||||
let made = keep_new_buffers(&platform, 16);
|
||||
let mark = log.mark();
|
||||
let _ = draw.encode(&drawable, region, None).unwrap();
|
||||
let _ = draw.encode(&drawable, region, None, None).unwrap();
|
||||
let calls = log.since(mark);
|
||||
let named: Vec<_> = calls
|
||||
.iter()
|
||||
@@ -2349,7 +2529,7 @@ mod tests {
|
||||
|
||||
// The same colour again: only the scanout's place is made.
|
||||
let mark = log.mark();
|
||||
let _ = draw.encode(&drawable, region, None).unwrap();
|
||||
let _ = draw.encode(&drawable, region, None, None).unwrap();
|
||||
assert_eq!(
|
||||
log.sels_since(mark)
|
||||
.iter()
|
||||
@@ -2391,7 +2571,7 @@ mod tests {
|
||||
height: 32,
|
||||
};
|
||||
let mark = log.mark();
|
||||
let _ = draw.encode(&drawable, corner, None).unwrap();
|
||||
let _ = draw.encode(&drawable, corner, None, None).unwrap();
|
||||
assert_eq!(
|
||||
log.sels_since(mark)
|
||||
.iter()
|
||||
@@ -2413,7 +2593,7 @@ mod tests {
|
||||
// Every stage off again: the blit, as before.
|
||||
set_color(&presented, 3, Default::default());
|
||||
let mark = log.mark();
|
||||
let _ = draw.encode(&drawable, region, None).unwrap();
|
||||
let _ = draw.encode(&drawable, region, None, None).unwrap();
|
||||
assert_eq!(
|
||||
log.sels_since(mark).iter().filter(|s| **s == DRAW).count(),
|
||||
0
|
||||
@@ -2454,7 +2634,7 @@ mod tests {
|
||||
let pre: Table = Arc::from(vec![[0.25, 0.25, 0.25, 0.0]]);
|
||||
set_color(&presented, 1, [None, Some(pre), None, None]);
|
||||
let mark = log.mark();
|
||||
let _ = draw.encode(&drawable, region, None).unwrap();
|
||||
let _ = draw.encode(&drawable, region, None, None).unwrap();
|
||||
let calls = log.since(mark);
|
||||
assert_eq!(calls.iter().filter(|c| c.sel == DRAW).count(), 1);
|
||||
assert!(
|
||||
@@ -2519,7 +2699,7 @@ mod tests {
|
||||
let made = keep_new_buffers(&platform, 32);
|
||||
|
||||
let mark = log.mark();
|
||||
let (first, _) = draw.encode(&drawable, region, None).unwrap();
|
||||
let (first, _) = draw.encode(&drawable, region, None, None).unwrap();
|
||||
assert_eq!(
|
||||
count_since(&platform, mark, DRAW),
|
||||
2,
|
||||
@@ -2551,7 +2731,7 @@ mod tests {
|
||||
|
||||
// Not completed yet: the next draw draws the table again, into the same texture.
|
||||
let mark = log.mark();
|
||||
let _ = draw.encode(&drawable, region, None).unwrap();
|
||||
let _ = draw.encode(&drawable, region, None, None).unwrap();
|
||||
assert_eq!(count_since(&platform, mark, DRAW), 2);
|
||||
assert_eq!(count_since(&platform, mark, NEW_TEXTURE), 0);
|
||||
|
||||
@@ -2559,7 +2739,7 @@ mod tests {
|
||||
platform.world().set_auto_complete(Some(CompleteOn::Caller));
|
||||
first.commit(OnFault::Die).unwrap();
|
||||
let mark = log.mark();
|
||||
let _ = draw.encode(&drawable, region, None).unwrap();
|
||||
let _ = draw.encode(&drawable, region, None, None).unwrap();
|
||||
assert_eq!(count_since(&platform, mark, DRAW), 1);
|
||||
assert_eq!(
|
||||
count_since(&platform, mark, NEW_BUFFER),
|
||||
@@ -2581,7 +2761,7 @@ mod tests {
|
||||
let plane: Table = Arc::from(vec![[1.0, 1.0, 1.0, 0.0], [0.0, 0.0, 0.0, 0.0]]);
|
||||
set_color(&presented, 2, [Some(plane), None, None, None]);
|
||||
let mark = log.mark();
|
||||
let _ = draw.encode(&drawable, region, None).unwrap();
|
||||
let _ = draw.encode(&drawable, region, None, None).unwrap();
|
||||
assert_eq!(count_since(&platform, mark, DRAW), 2);
|
||||
assert_eq!(count_since(&platform, mark, NEW_TEXTURE), 1);
|
||||
|
||||
@@ -2593,7 +2773,7 @@ mod tests {
|
||||
]);
|
||||
set_color(&presented, 3, [None, None, Some(rows), Some(post)]);
|
||||
let mark = log.mark();
|
||||
let _ = draw.encode(&drawable, region, None).unwrap();
|
||||
let _ = draw.encode(&drawable, region, None, None).unwrap();
|
||||
assert_eq!(count_since(&platform, mark, DRAW), 1);
|
||||
assert_eq!(count_since(&platform, mark, NEW_TEXTURE), 0);
|
||||
assert_eq!(
|
||||
@@ -2641,7 +2821,7 @@ mod tests {
|
||||
let surface = texture(&draw, format);
|
||||
let view = show(&surface);
|
||||
let mark = log.mark();
|
||||
let _ = draw.encode(&drawable, region, None).unwrap();
|
||||
let _ = draw.encode(&drawable, region, None, None).unwrap();
|
||||
assert_eq!(count_since(&platform, mark, DRAW), 2, "{format}");
|
||||
assert_eq!(count_since(&platform, mark, NEW_VIEW), 1, "{format}");
|
||||
let viewed = view.made().expect("the display's view").clone_ref();
|
||||
@@ -2674,7 +2854,7 @@ mod tests {
|
||||
);
|
||||
// Again: the ramp and the view are kept.
|
||||
let mark = log.mark();
|
||||
let _ = draw.encode(&drawable, region, None).unwrap();
|
||||
let _ = draw.encode(&drawable, region, None, None).unwrap();
|
||||
assert_eq!(count_since(&platform, mark, NEW_TEXTURE), 0, "{format}");
|
||||
assert_eq!(count_since(&platform, mark, NEW_VIEW), 0, "{format}");
|
||||
assert_eq!(
|
||||
@@ -2704,7 +2884,7 @@ mod tests {
|
||||
for (shown, at) in [(&surface, quarter), (&srgb, region)] {
|
||||
let view = show(shown);
|
||||
let mark = log.mark();
|
||||
let _ = draw.encode(&drawable, at, None).unwrap();
|
||||
let _ = draw.encode(&drawable, at, None, None).unwrap();
|
||||
assert_eq!(count_since(&platform, mark, DRAW), 1);
|
||||
let viewed = view.made().expect("the display's view");
|
||||
assert_eq!(fragment_textures(&platform, mark), [(viewed.id(), 0)]);
|
||||
@@ -2744,7 +2924,7 @@ mod tests {
|
||||
}),
|
||||
});
|
||||
let mark = log.mark();
|
||||
let _ = draw.encode(&drawable, WHOLE, None).expect("a draw");
|
||||
let _ = draw.encode(&drawable, WHOLE, None, None).expect("a draw");
|
||||
assert_eq!(count_since(&platform, mark, NEW_VIEW), 1);
|
||||
let viewed = view.made().expect("the display's view").clone_ref();
|
||||
assert_ne!(viewed.resource_index(), 7);
|
||||
@@ -2757,7 +2937,7 @@ mod tests {
|
||||
let next = placed();
|
||||
assert_eq!(next.resource_index(), 7);
|
||||
let mark = log.mark();
|
||||
let _ = draw.encode(&drawable, WHOLE, None).expect("a draw");
|
||||
let _ = draw.encode(&drawable, WHOLE, None, None).expect("a draw");
|
||||
assert_eq!(count_since(&platform, mark, NEW_VIEW), 0);
|
||||
assert_eq!(fragment_textures(&platform, mark), [(viewed.id(), 0)]);
|
||||
}
|
||||
@@ -2789,14 +2969,14 @@ mod tests {
|
||||
let before = refused();
|
||||
platform.script().push(NEW_VIEW, Step::Nil);
|
||||
let mark = log.mark();
|
||||
assert!(draw.encode(&drawable, WHOLE, None).is_none());
|
||||
assert!(draw.encode(&drawable, WHOLE, None, None).is_none());
|
||||
assert_eq!(refused(), before + 1);
|
||||
assert!(view.made().is_none());
|
||||
let sels = log.sels_since(mark);
|
||||
assert_eq!(sels, [NEW_VIEW], "{sels:?}");
|
||||
|
||||
let mark = log.mark();
|
||||
let (command_buffer, presents) = draw.encode(&drawable, WHOLE, None).expect("a draw");
|
||||
let (command_buffer, presents) = draw.encode(&drawable, WHOLE, None, None).expect("a draw");
|
||||
presents.after(&command_buffer);
|
||||
command_buffer.commit(OnFault::Die).unwrap();
|
||||
assert_eq!(refused(), before + 1);
|
||||
|
||||
@@ -23,6 +23,11 @@
|
||||
//! moves for it. It is made again only for a new glyph, and the pipeline once per drawable format;
|
||||
//! a recovered device makes both again (`sink`'s module doc, "A recovered device").
|
||||
//!
|
||||
//! **The window's overlay** ([`DrawnOverlay`]: on Linux the question a close asks) is drawn by a
|
||||
//! pass of the same kind, after the cursor's, a pixel of its image to a pixel of the drawable at
|
||||
//! the overlay's place, clipped to the drawable alone ([`overlay_placement`]), with a pipeline and
|
||||
//! a texture of its own ([`CursorPass::overlay`]), made again only for a new image.
|
||||
//!
|
||||
//! **Refusals.** What the platform refuses of them leaves the frame without the guest's cursor,
|
||||
//! logged (once a kind, `core::logging`); the host's cursor stays hidden over the image while the
|
||||
//! guest leads, so neither shows. The draw's values are ones the platforms take: a scissor inside
|
||||
@@ -43,7 +48,7 @@ use reims_vgpu_vulkan::core::platform::values::{
|
||||
ColorAttachmentDesc, FullRenderPipelineDesc, TextureDesc,
|
||||
};
|
||||
use reims_vgpu_vulkan::core::platform::{Handle as _, HostRange, Platform};
|
||||
use reims_vgpu_window::{DrawnCursor, Glyph, PixelRect};
|
||||
use reims_vgpu_window::{DrawnCursor, DrawnOverlay, Glyph, PixelRect};
|
||||
|
||||
use super::Converter;
|
||||
use crate::entry::no_catch;
|
||||
@@ -60,18 +65,38 @@ const GLYPH_FORMAT: u64 = 70;
|
||||
const GLYPH_USAGE: u64 = 1;
|
||||
/// The cursor's pipelines' label.
|
||||
pub(super) const CURSOR_PIPELINE_LABEL: &str = "reims-vgpu.display-cursor";
|
||||
/// The overlay's pipelines' label.
|
||||
pub(super) const OVERLAY_PIPELINE_LABEL: &str = "reims-vgpu.display-overlay";
|
||||
|
||||
/// What a sink keeps to draw the guest's cursor, from one frame to the next.
|
||||
/// What a sink keeps to draw the guest's cursor, or the window's overlay, from one frame to the
|
||||
/// next.
|
||||
pub(super) struct CursorPass<P: Platform> {
|
||||
/// Its pipelines' label.
|
||||
label: &'static str,
|
||||
/// What it draws, for its refusals' lines.
|
||||
draws: &'static str,
|
||||
/// The blending pipelines made so far, by the drawable's pixel format.
|
||||
pipelines: Vec<(u64, P::RenderPipeline)>,
|
||||
/// The glyph last drawn, and its texture.
|
||||
glyph: Option<(Arc<Glyph>, P::Texture)>,
|
||||
}
|
||||
|
||||
impl<P: Platform> Default for CursorPass<P> {
|
||||
fn default() -> Self {
|
||||
impl<P: Platform> CursorPass<P> {
|
||||
/// The guest's cursor's pass.
|
||||
pub(super) fn cursor() -> Self {
|
||||
CursorPass::new(CURSOR_PIPELINE_LABEL, "guest cursor")
|
||||
}
|
||||
|
||||
/// The window's overlay's pass.
|
||||
pub(super) fn overlay() -> Self {
|
||||
CursorPass::new(OVERLAY_PIPELINE_LABEL, "overlay")
|
||||
}
|
||||
|
||||
/// A pass whose pipelines are labelled `label`, drawing what `draws` names.
|
||||
fn new(label: &'static str, draws: &'static str) -> Self {
|
||||
CursorPass {
|
||||
label,
|
||||
draws,
|
||||
pipelines: Vec::new(),
|
||||
glyph: None,
|
||||
}
|
||||
@@ -92,27 +117,28 @@ pub(super) struct Prepared<P: Platform> {
|
||||
}
|
||||
|
||||
impl<P: ApiPlatform> CursorPass<P> {
|
||||
/// What drawing `cursor` over the frame drawn into `target` over `region` binds, with
|
||||
/// `converter`'s functions on `device` of `platform`: the pipeline for the target's format,
|
||||
/// made the first time, and the glyph's texture, made again only for a new glyph. `None` where
|
||||
/// nothing would show, and where the platform refuses the pipeline or the texture (logged).
|
||||
/// What drawing `glyph` over the frame drawn into `target` binds, at `placed` (its viewport
|
||||
/// and scissor: [`placement`], [`overlay_placement`]), with `converter`'s functions on
|
||||
/// `device` of `platform`: the pipeline for the target's format, made the first time, and the
|
||||
/// glyph's texture, made again only for a new glyph. `None` where the platform refuses the
|
||||
/// pipeline or the texture (logged).
|
||||
pub(super) fn prepare(
|
||||
&mut self,
|
||||
platform: &Arc<P>,
|
||||
device: &P::Device,
|
||||
converter: &Converter<P>,
|
||||
target: &P::Texture,
|
||||
region: PixelRect,
|
||||
cursor: &DrawnCursor,
|
||||
placed: (MtlViewport, MtlScissorRect),
|
||||
glyph: &Arc<Glyph>,
|
||||
) -> Option<Prepared<P>> {
|
||||
let (viewport, scissor) = placement(region, (target.width(), target.height()), cursor)?;
|
||||
let (viewport, scissor) = placed;
|
||||
let format = target.pixel_format();
|
||||
let pipeline = match self.pipelines.iter().find(|(f, _)| *f == format) {
|
||||
Some((_, pipeline)) => pipeline.clone_ref(),
|
||||
None => {
|
||||
let made = device
|
||||
.new_render_pipeline_state_full(&FullRenderPipelineDesc {
|
||||
label: Some(CURSOR_PIPELINE_LABEL),
|
||||
label: Some(self.label),
|
||||
colors: vec![(0, over(format))],
|
||||
..FullRenderPipelineDesc::new(
|
||||
&converter.pipelines.vertex_function,
|
||||
@@ -122,19 +148,19 @@ impl<P: ApiPlatform> CursorPass<P> {
|
||||
.ok()
|
||||
.flatten();
|
||||
let Some(pipeline) = made else {
|
||||
return refused("a pipeline for the drawable's format");
|
||||
return refused(self.draws, "a pipeline for the drawable's format");
|
||||
};
|
||||
self.pipelines.push((format, pipeline.clone_ref()));
|
||||
pipeline
|
||||
}
|
||||
};
|
||||
let texture = match &self.glyph {
|
||||
Some((drawn, texture)) if Arc::ptr_eq(drawn, &cursor.glyph) => texture.clone_ref(),
|
||||
Some((drawn, texture)) if Arc::ptr_eq(drawn, glyph) => texture.clone_ref(),
|
||||
_ => {
|
||||
let Some(texture) = glyph_texture(platform, device, &cursor.glyph) else {
|
||||
return refused("a texture for the glyph");
|
||||
let Some(texture) = glyph_texture(platform, device, glyph) else {
|
||||
return refused(self.draws, "a texture for its image");
|
||||
};
|
||||
self.glyph = Some((Arc::clone(&cursor.glyph), texture.clone_ref()));
|
||||
self.glyph = Some((Arc::clone(glyph), texture.clone_ref()));
|
||||
texture
|
||||
}
|
||||
};
|
||||
@@ -147,12 +173,13 @@ impl<P: ApiPlatform> CursorPass<P> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Logs that the platform refused `what`: the frame shows no guest cursor.
|
||||
fn refused<T>(what: &str) -> Option<T> {
|
||||
/// Logs that the platform refused `what` for the pass that draws `draws`: the frame shows none.
|
||||
fn refused<T>(draws: &str, what: &str) -> Option<T> {
|
||||
let kind = format!("{draws}: {what}");
|
||||
// Once per kind; later ones at debug, counted (`core::logging`).
|
||||
log::warn!(
|
||||
target: "reims_vgpu::graphics", kind:% = what;
|
||||
"the window draws no guest cursor: the platform refused {what}"
|
||||
target: "reims_vgpu::graphics", kind:% = kind;
|
||||
"the window draws no {draws}: the platform refused {what}"
|
||||
);
|
||||
None
|
||||
}
|
||||
@@ -245,6 +272,51 @@ pub(super) fn placement(
|
||||
Some((viewport, scissor))
|
||||
}
|
||||
|
||||
/// Where `overlay` draws over a target of `size` pixels: the viewport, its image's rectangle at
|
||||
/// its place, a pixel of it to a pixel of the target; and the scissor, the target's pixels that
|
||||
/// rectangle covers. `None` where nothing would show: an empty image, or one wholly off the
|
||||
/// target; and where the viewport would reach past [`VIEWPORT_BOUNDS`] or be larger than
|
||||
/// [`VIEWPORT_DIMENSION`] (the module doc).
|
||||
pub(super) fn overlay_placement(
|
||||
size: (u64, u64),
|
||||
overlay: &DrawnOverlay,
|
||||
) -> Option<(MtlViewport, MtlScissorRect)> {
|
||||
let (width, height) = (
|
||||
u64::from(overlay.image.width),
|
||||
u64::from(overlay.image.height),
|
||||
);
|
||||
let (x, y) = overlay.at;
|
||||
let (x1, y1) = (
|
||||
x.saturating_add(width).min(size.0),
|
||||
y.saturating_add(height).min(size.1),
|
||||
);
|
||||
if x1 <= x || y1 <= y {
|
||||
return None;
|
||||
}
|
||||
#[expect(clippy::as_conversions, reason = "u64 to f64 rounds to nearest")]
|
||||
let [left, top, width, height] = [x, y, width, height].map(|v| v as f64);
|
||||
let (_, high) = VIEWPORT_BOUNDS;
|
||||
let within = |start: f64, length: f64| start + length <= high && length <= VIEWPORT_DIMENSION;
|
||||
if !(within(left, width) && within(top, height)) {
|
||||
return None;
|
||||
}
|
||||
let viewport = MtlViewport {
|
||||
origin_x: left,
|
||||
origin_y: top,
|
||||
width,
|
||||
height,
|
||||
znear: 0.0,
|
||||
zfar: 1.0,
|
||||
};
|
||||
let scissor = MtlScissorRect {
|
||||
x,
|
||||
y,
|
||||
width: x1 - x,
|
||||
height: y1 - y,
|
||||
};
|
||||
Some((viewport, scissor))
|
||||
}
|
||||
|
||||
/// `glyph`'s pixels in a texture of the platform's own (the module doc), RGBA8, premultiplied,
|
||||
/// read by shaders, a row its bytes do not reach transparent; `None` for a glyph with no pixels,
|
||||
/// or where the platform makes no texture.
|
||||
@@ -403,6 +475,37 @@ mod tests {
|
||||
assert!(placement(scaled, (1700, 1700), &fits).is_some());
|
||||
}
|
||||
|
||||
/// An overlay is drawn a pixel of its image to a pixel of the target at its place, clipped to
|
||||
/// the target alone; one wholly off the target, an empty one, and one past Vulkan's least
|
||||
/// viewport bounds draw nothing
|
||||
#[test]
|
||||
fn an_overlay_is_drawn_pixel_for_pixel_at_its_place() {
|
||||
let overlay = |(width, height): (u32, u32), at: (u64, u64)| DrawnOverlay {
|
||||
image: Arc::new(Glyph {
|
||||
width,
|
||||
height,
|
||||
hot_spot: (0, 0),
|
||||
rgba: Vec::new(),
|
||||
}),
|
||||
at,
|
||||
};
|
||||
let (v, s) = overlay_placement((1920, 1080), &overlay((624, 248), (648, 416))).unwrap();
|
||||
assert_eq!(
|
||||
(v.origin_x, v.origin_y, v.width, v.height),
|
||||
(648.0, 416.0, 624.0, 248.0)
|
||||
);
|
||||
assert_eq!((s.x, s.y, s.width, s.height), (648, 416, 624, 248));
|
||||
let (v, s) = overlay_placement((320, 200), &overlay((312, 248), (4, 0))).unwrap();
|
||||
assert_eq!((v.width, v.height), (312.0, 248.0));
|
||||
assert_eq!((s.x, s.y, s.width, s.height), (4, 0, 312, 200));
|
||||
let none = |o: &DrawnOverlay| overlay_placement((320, 200), o).is_none();
|
||||
assert!(none(&overlay((10, 10), (320, 0))));
|
||||
assert!(none(&overlay((10, 10), (0, 200))));
|
||||
assert!(none(&overlay((0, 10), (0, 0))));
|
||||
assert!(none(&overlay((4097, 10), (0, 0))));
|
||||
assert!(overlay_placement((9000, 100), &overlay((10, 10), (8190, 0))).is_none());
|
||||
}
|
||||
|
||||
/// Premultiplying scales each colour channel by the alpha, to nearest, and keeps the alpha
|
||||
#[test]
|
||||
fn premultiplying_scales_the_colour_by_the_alpha() {
|
||||
|
||||
@@ -3000,7 +3000,7 @@ fn draws_the_scanout(
|
||||
width: drawable.width(),
|
||||
height: drawable.height(),
|
||||
};
|
||||
if draw.encode(drawable, rect, None).is_none()
|
||||
if draw.encode(drawable, rect, None, None).is_none()
|
||||
|| platform.call_log().count(SCANOUT_BLIT) != blits + 1
|
||||
{
|
||||
return false;
|
||||
@@ -3503,7 +3503,7 @@ fn a_kept_present_s_table_reaches_the_next_drawn_frame() {
|
||||
let drawn_table = || -> Vec<f32> {
|
||||
let log = platform.call_log();
|
||||
let mark = log.mark();
|
||||
let _drawn = draw.encode(&drawable, rect, None).expect("a draw");
|
||||
let _drawn = draw.encode(&drawable, rect, None, None).expect("a draw");
|
||||
let bound = log
|
||||
.since(mark)
|
||||
.into_iter()
|
||||
@@ -9552,7 +9552,9 @@ fn a_presented_surface_s_block_waits_for_the_display() {
|
||||
width: 64,
|
||||
height: 64,
|
||||
};
|
||||
let (command_buffer, presents) = draw.encode(&drawable, rect, None).expect("a draw of it");
|
||||
let (command_buffer, presents) = draw
|
||||
.encode(&drawable, rect, None, None)
|
||||
.expect("a draw of it");
|
||||
presents.after(&command_buffer);
|
||||
no_catch(command_buffer.commit(OnFault::Die));
|
||||
let drawing = committed(&platform, 1)[0];
|
||||
@@ -9576,7 +9578,7 @@ fn a_presented_surface_s_block_waits_for_the_display() {
|
||||
);
|
||||
// A draw from here reads nothing of the block: the guest set no scanout either.
|
||||
assert!(device.control.presented[0].shown().is_none());
|
||||
assert!(draw.encode(&drawable, rect, None).is_none());
|
||||
assert!(draw.encode(&drawable, rect, None, None).is_none());
|
||||
|
||||
let world = platform.world();
|
||||
assert!(world.complete(
|
||||
|
||||
Reference in new issue
Block a user