diff --git a/Plans/02 Verify/S005-gl-painters.md b/Plans/02 Verify/S005-gl-painters.md index fd6ba04..1708f30 100644 --- a/Plans/02 Verify/S005-gl-painters.md +++ b/Plans/02 Verify/S005-gl-painters.md @@ -11,10 +11,10 @@ Settle the riskiest design question (REVIEW.md: several painters on one GL conte - `src/window.rs`, the painter/offscreen parts of `src/hub.rs` and `src/tabs.rs` ## Steps -- [ ] Write `tests/smoke/painters.txt`: 5 tabs with distinct local test pages (colours/text), switch through them taking a shot each, resize window, PiP one tab, close tabs, shot -- [ ] Run it under software GL; check every screenshot shows the right tab with no garbage, black frames or stale content -- [ ] Check GL state isolation: egui chrome still draws right after page blits (scissor, blend, bound framebuffer); fix by saving/restoring state around `render_to_parent_callback` if needed -- [ ] Measure memory and frame time with 5 and 15 tabs (log `perf.rs` numbers); check idle tabs release painters per the perf settings +- [x] Write `tests/smoke/painters.txt`: 5 tabs with distinct local test pages (colours/text), switch through them taking a shot each, resize window, PiP one tab, close tabs, shot +- [x] Run it under software GL; check every screenshot shows the right tab with no garbage, black frames or stale content +- [x] Check GL state isolation: egui chrome still draws right after page blits (scissor, blend, bound framebuffer); fix by saving/restoring state around `render_to_parent_callback` if needed +- [x] Measure memory and frame time with 5 and 15 tabs (log `perf.rs` numbers); check idle tabs release painters per the perf settings - [ ] Decision: keep per-tab painters, or implement the one-painter-per-window fallback (PiP then reuses a dedicated painter); implement it if needed - [ ] Run `painters.txt` and `launch.txt` green; record the decision in `docs/DECISIONS.md` and remove the item from `docs/REVIEW.md` - [ ] PROGRESS entry, INDEX line, commit `S005: …` and push diff --git a/src/actions.rs b/src/actions.rs index 32817b8..e3db100 100644 --- a/src/actions.rs +++ b/src/actions.rs @@ -1093,7 +1093,7 @@ impl App { for kind in closes { if let Some(id) = self.windows.iter().find(|(_, x)| x.kind == kind).map(|(id, _)| *id) { if let Some(mut pip) = self.windows.remove(&id) { - pip.egui.destroy(); + pip.destroy(); } } } diff --git a/src/app.rs b/src/app.rs index 751aac5..dd0a32a 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1408,7 +1408,7 @@ impl App { self.hub.forget(web.webview.id()); } } - win.egui.destroy(); + win.destroy(); if self.last_window == Some(window) { self.last_window = None; } @@ -1458,7 +1458,7 @@ impl App { self.hub.servo.borrow_mut().take(); drop(self.servo.take()); for (_, mut win) in self.windows.drain() { - win.egui.destroy(); + win.destroy(); } let _ = std::fs::remove_file(self.shared.paths.running_marker()); if let Some(server) = self.instance.take() { @@ -1580,6 +1580,7 @@ impl App { fn draw(&mut self, window: WindowId) { let Some(win) = self.windows.get_mut(&window) else { return }; + let started = Instant::now(); win.needs_redraw = false; win.sync_webviews(); win.paint_page(); @@ -1594,6 +1595,10 @@ impl App { } let clear = win.ui.palette.map(|p| p.surface).unwrap_or(egui::Color32::from_gray(24)); win.present(clear); + if win.frame_times.len() >= 120 { + win.frame_times.pop_front(); + } + win.frame_times.push_back(started.elapsed().as_secs_f32() * 1000.0); // The page area moved or changed size: resize the web view and draw again. if win.page_rect != page_rect_before { win.needs_redraw = true; diff --git a/src/inject.rs b/src/inject.rs index b82eaeb..96a2e4e 100644 --- a/src/inject.rs +++ b/src/inject.rs @@ -268,7 +268,7 @@ const PIP_JS: &str = r#"(() => { if (!window.__nacre || window.__nacre.pip) retu window.__nacrePipV = v; v.setAttribute('data-nacre-pip', ''); if (!document.getElementById('__nacre_pip_css')) { const st = document.createElement('style'); st.id = '__nacre_pip_css'; - st.textContent = 'video[data-nacre-pip]{position:fixed!important;inset:0!important;width:100vw!important;height:100vh!important;' + + st.textContent = 'video[data-nacre-pip]{position:fixed!important;inset:0!important;margin:0!important;width:100vw!important;height:100vh!important;' + 'max-width:none!important;max-height:none!important;object-fit:contain!important;background:#000!important;z-index:2147483647!important;transform:none!important}' + 'html.__nacre_pip,html.__nacre_pip body{overflow:hidden!important}'; (document.head || document.documentElement).appendChild(st); diff --git a/src/selftest.rs b/src/selftest.rs index c44e416..ba2fd2f 100644 --- a/src/selftest.rs +++ b/src/selftest.rs @@ -5,7 +5,8 @@ //! waitload [seconds] wait until the current tab finished loading (default limit 30 s) //! open load in the current tab newtab open in a new tab //! page show an internal page -//! shot save the window as .png +//! shot [pip] save the window (or the picture-in-picture window) as .png +//! resize resize the window (logical pixels) //! status save tabs, downloads, VPN, ad blocker and so on as .json //! set change a setting, e.g. `set /privacy/https_only true` //! flyout main, downloads, shield, vpn, media, siteinfo, profiles, apps, qr, none @@ -27,6 +28,7 @@ use crate::actions::{Command, Disposition, DownloadOp}; use crate::app::App; use crate::tabs::Page; use crate::ui::Flyout; +use crate::window::Kind; pub struct SelfTest { lines: Vec, @@ -69,8 +71,8 @@ impl App { } if test.pending_shot { // Written by the window after its next frame. - if self.windows.get(&window).is_some_and(|w| w.shot_request.is_some()) { - self.redraw(window); + if let Some(shooting) = self.windows.iter().find(|(_, w)| w.shot_request.is_some()).map(|(id, _)| *id) { + self.redraw(shooting); return; } if let Some(test) = self.selftest.as_mut() { @@ -130,13 +132,25 @@ impl App { } }, "shot" => { - if let Some(win) = self.windows.get_mut(&window) { - win.shot_request = Some(dir.join(format!("{rest}.png"))); + let (name, which) = rest.split_once(char::is_whitespace).unwrap_or((rest, "")); + let target = if which.trim() == "pip" { self.windows.iter().find(|(_, w)| matches!(w.kind, Kind::Pip { .. })).map(|(id, _)| *id) } else { Some(window) }; + let Some(target) = target else { + self.selftest_log("shot: no such window"); + return; + }; + if let Some(win) = self.windows.get_mut(&target) { + win.shot_request = Some(dir.join(format!("{name}.png"))); } if let Some(t) = self.selftest.as_mut() { t.pending_shot = true; } - self.redraw(window); + self.redraw(target); + }, + "resize" => { + let mut size = rest.split_whitespace().filter_map(|v| v.parse::().ok()); + if let (Some(w), Some(h), Some(win)) = (size.next(), size.next(), self.windows.get(&window)) { + let _ = win.window.request_inner_size(winit::dpi::LogicalSize::new(w, h)); + } }, "status" => { let text = serde_json::to_string_pretty(&self.selftest_status(window)).unwrap_or_default(); @@ -281,8 +295,16 @@ impl App { .unwrap_or_default(); let downloads: Vec = self.shared.downloads.borrow().items.iter().map(|d| serde_json::json!({"id": d.id, "name": d.file_name(), "state": format!("{:?}", d.state), "received": d.received, "total": d.total, "error": d.error, "manager": d.manager})).collect(); let settings = self.shared.settings.borrow(); + // Draw times (ms) of the window's recent frames: median and slowest. + let frames = self.windows.get(&window).map(|w| { + let mut times: Vec = w.frame_times.iter().copied().collect(); + times.sort_by(f32::total_cmp); + serde_json::json!({"count": times.len(), "median_ms": times.get(times.len() / 2), "max_ms": times.last()}) + }); serde_json::json!({ "windows": self.windows.len(), + "frames": frames, + "painters": self.windows.values().flat_map(|w| &w.tabs).filter(|t| t.web.is_some()).count(), "window_kinds": self.windows.values().map(|w| format!("{:?}", w.kind)).collect::>(), "tabs": tabs, "downloads": downloads, @@ -302,6 +324,7 @@ impl App { "media_now": self.shared.media_now.borrow().as_ref().map(|m| m.title.clone()), "import_sources": self.shared.import_sources.borrow().iter().map(|s| s.name()).collect::>(), "memory": crate::perf::own_memory(), + "threads": crate::perf::own_threads(), "user_agent": self.shared.user_agent.borrow().clone(), }) } diff --git a/src/window.rs b/src/window.rs index e0e5ba6..7801074 100644 --- a/src/window.rs +++ b/src/window.rs @@ -58,6 +58,8 @@ pub struct BrowserWindow { pub focused: bool, /// Save the next frame to this file (self-test screenshots). pub shot_request: Option, + /// How long the last frames took to draw, in ms (self-test status). + pub frame_times: std::collections::VecDeque, } /// Choose the winit window attributes for a kind of window. @@ -117,6 +119,7 @@ impl BrowserWindow { pip_size: [0, 0], focused: true, shot_request: None, + frame_times: Default::default(), }) } @@ -236,6 +239,13 @@ impl BrowserWindow { self.rendering.present(); } + /// Free egui's GL objects. Its texture and buffer ids are only valid in this window's context, so make + /// it current first: deleting them while another window's context is current deletes that window's. + pub fn destroy(&mut self) { + let _ = self.rendering.make_current(); + self.egui.destroy(); + } + pub fn resized(&mut self, size: PhysicalSize) { if size.width == 0 || size.height == 0 { return; diff --git a/tests/smoke/pages/clip.webm b/tests/smoke/pages/clip.webm new file mode 100644 index 0000000..d1b04ec Binary files /dev/null and b/tests/smoke/pages/clip.webm differ diff --git a/tests/smoke/pages/color.html b/tests/smoke/pages/color.html new file mode 100644 index 0000000..9aba8ef --- /dev/null +++ b/tests/smoke/pages/color.html @@ -0,0 +1,27 @@ + + + +Tab + + +
+
+
+
+
+ + + diff --git a/tests/smoke/pages/video.html b/tests/smoke/pages/video.html new file mode 100644 index 0000000..5d63f87 --- /dev/null +++ b/tests/smoke/pages/video.html @@ -0,0 +1,13 @@ + + + +Tab 5 video + + +
TAB 5 VIDEO
+ + + diff --git a/tests/smoke/painters-perf.txt b/tests/smoke/painters-perf.txt new file mode 100644 index 0000000..4b96cab --- /dev/null +++ b/tests/smoke/painters-perf.txt @@ -0,0 +1,57 @@ +# Painter cost (S005): memory and frame time with 5 and 15 tabs, then idle tabs releasing their painters. +# The status files carry "memory" (PSS bytes), "painters" (tabs holding a web view = painter) and "frames" +# (median/max draw time of the last frames). Run with SMOKE_TIMEOUT=400 tools/smoke.sh. +wait 3 +set /perf/freeze_min 0 +set /perf/discard_min 0 +open @PAGES@/color.html?n=1&c=%23c01c28 +waitload 30 +wait 2 +status m1-one-tab +newtab @PAGES@/color.html?n=2&c=%2326a269 +waitload 30 +newtab @PAGES@/color.html?n=3&c=%231c71d8 +waitload 30 +newtab @PAGES@/color.html?n=4&c=%23e5a50a +waitload 30 +newtab @PAGES@/color.html?n=5&c=%23613583&anim=1 +waitload 30 +# The animated tab redraws every frame: wait so the frame list holds only those frames. +wait 5 +status m5-five-tabs +newtab @PAGES@/color.html?n=6&c=%23c01c28 +waitload 30 +newtab @PAGES@/color.html?n=7&c=%2326a269 +waitload 30 +newtab @PAGES@/color.html?n=8&c=%231c71d8 +waitload 30 +newtab @PAGES@/color.html?n=9&c=%23e5a50a +waitload 30 +newtab @PAGES@/color.html?n=10&c=%23613583 +waitload 30 +newtab @PAGES@/color.html?n=11&c=%23c01c28 +waitload 30 +newtab @PAGES@/color.html?n=12&c=%2326a269 +waitload 30 +newtab @PAGES@/color.html?n=13&c=%231c71d8 +waitload 30 +newtab @PAGES@/color.html?n=14&c=%23e5a50a +waitload 30 +newtab @PAGES@/color.html?n=15&c=%23613583&anim=1 +waitload 30 +wait 5 +status m15-fifteen-tabs +shot m15-fifteen-tabs +# Idle tabs unload after discard_min minutes (checked by the slow tick): 14 painters should go away. +set /perf/discard_min 1 +wait 90 +status m15-after-discard +shot m15-after-discard +# An unloaded tab loads again when shown. +cmd select 1 +waitload 30 +wait 1 +shot m15-reloaded-tab2 +status m15-reloaded +status done +quit diff --git a/tests/smoke/painters.txt b/tests/smoke/painters.txt new file mode 100644 index 0000000..6981a4e --- /dev/null +++ b/tests/smoke/painters.txt @@ -0,0 +1,79 @@ +# Per-tab painters (S005): 5 tabs, each its own painter on the window's GL context. Every shot must show the +# right tab (colour + "TAB n", four white corner boxes), no black, garbage or stale frames, and the chrome intact. +# Run with tools/smoke.sh. +wait 3 +open @PAGES@/color.html?n=1&c=%23c01c28 +waitload 30 +newtab @PAGES@/color.html?n=2&c=%2326a269 +waitload 30 +newtab @PAGES@/color.html?n=3&c=%231c71d8&anim=1 +waitload 30 +newtab @PAGES@/color.html?n=4&c=%23e5a50a +waitload 30 +newtab @PAGES@/video.html +waitload 30 +wait 2 +status opened +# Switch through every tab, then back to one that was hidden for a while. +cmd select 0 +wait 1 +shot s1-tab1 +cmd select 1 +wait 1 +shot s2-tab2 +cmd select 2 +wait 1 +shot s3-tab3 +cmd select 3 +wait 1 +shot s4-tab4 +cmd select 4 +wait 1 +shot s5-tab5 +cmd select 1 +wait 1 +shot s6-tab2-again +# Resize: the page must fill the new page area (corner boxes visible), then the old size again. +resize 900 600 +wait 2 +shot r1-small-tab2 +cmd select 2 +wait 1 +shot r2-small-tab3 +resize 1280 800 +wait 2 +shot r3-big-tab3 +# Picture in picture: tab 5's video moves to its own window, the tab shows a placeholder. +cmd select 4 +wait 1 +cmd pip +wait 3 +shot p1-main-tab5 +shot p2-pip pip +cmd select 0 +wait 2 +shot p3-main-tab1 +shot p4-pip-while-tab1 pip +status pip +cmd select 4 +wait 1 +cmd exitpip +wait 2 +shot p5-tab5-back +# Close tabs: the tab that takes over must draw its own page. +cmd select 2 +wait 1 +cmd close_tab +wait 1 +shot c1-closed-tab3 +cmd close_tab +wait 1 +shot c2-closed-tab4 +cmd select 0 +wait 1 +cmd close_tab +wait 1 +shot c3-closed-tab1 +status closed +status done +quit