diff --git a/apps/desktop-gpui/src/app_windows.rs b/apps/desktop-gpui/src/app_windows.rs index bd904728d47..5c6759bc9dc 100644 --- a/apps/desktop-gpui/src/app_windows.rs +++ b/apps/desktop-gpui/src/app_windows.rs @@ -5531,12 +5531,14 @@ pub(crate) async fn shared_wgpu_device() -> Option { tracing::warn!(%error, "could not precreate shared wgpu device"); + crate::store::EditorPreviewQuality::note_rendering_adapter(true); None } } diff --git a/apps/desktop-gpui/src/editor_window.rs b/apps/desktop-gpui/src/editor_window.rs index fb4a1c98fc0..0a839cb6f55 100644 --- a/apps/desktop-gpui/src/editor_window.rs +++ b/apps/desktop-gpui/src/editor_window.rs @@ -3647,6 +3647,9 @@ impl EditorWindow { window: &mut Window, cx: &mut Context, ) { + // The render device exists by now, so a software fallback has been + // noted and an unchosen default can step down to Half. + self.preview_quality = crate::store::GeneralSettings::load().editor_preview_quality; transport.set_resolution(self.preview_resolution()); self.transport = Some(transport); self.stats = Some(stats); @@ -4836,11 +4839,11 @@ impl EditorWindow { /// Redraw the paused frame once at the canvas's device-pixel size. /// /// Playback and paused interactions keep rendering at the saved preview - /// quality, so their cost does not change. With the default Half quality - /// that frame is smaller than a Retina canvas and gets magnified, so once - /// the preview has been still for `delay` (a pause, the end of a scrub or - /// an edit, a resize) the same frame is rendered again at the canvas size. - /// Full and Quarter are explicit choices and are drawn as chosen. + /// quality, so their cost does not change. Half and Full frames are + /// smaller than a large Retina canvas and get magnified, so once the + /// preview has been still for `delay` (a pause, the end of a scrub or an + /// edit, a resize) the same frame is rendered again at the canvas size. + /// Quarter is an explicit choice for speed and is drawn as chosen. pub(crate) fn schedule_sharp_preview( &mut self, delay: Duration, @@ -4850,7 +4853,7 @@ impl EditorWindow { if self.playing { return SharpPreview::Playing; } - if self.preview_quality != crate::store::EditorPreviewQuality::Half { + if self.preview_quality == crate::store::EditorPreviewQuality::Quarter { return SharpPreview::ChosenQuality; } if self.preparing_presentation.is_some() @@ -8536,6 +8539,9 @@ impl EditorWindow { quality: crate::store::EditorPreviewQuality, cx: &mut Context, ) { + if !crate::store::set_editor_preview_quality(quality) { + tracing::warn!("failed to persist preview quality"); + } if quality == self.preview_quality { return; } @@ -8546,14 +8552,6 @@ impl EditorWindow { if let Some(transport) = &self.transport { transport.set_resolution(resolution); } - let value = serde_json::Value::String(quality.as_json().to_string()); - if !crate::store::set_store_setting( - crate::store::GENERAL_SETTINGS, - "editorPreviewQuality", - value, - ) { - tracing::warn!("failed to persist preview quality"); - } if was_playing { self.stop_playback(cx); self.start_playback(from, cx); @@ -14664,6 +14662,26 @@ mod tests { }); } + #[gpui::test] + fn full_gets_the_sharp_redraw_and_quarter_is_drawn_as_chosen( + cx: &mut gpui::TestAppContext, + ) { + let (editor, cx) = super::side_panel::fixture(cx); + editor.update(cx, |editor, cx| { + editor.playing = false; + editor.preview_quality = crate::store::EditorPreviewQuality::Full; + assert_eq!( + editor.schedule_sharp_preview(Duration::ZERO, cx), + SharpPreview::NoEngine + ); + editor.preview_quality = crate::store::EditorPreviewQuality::Quarter; + assert_eq!( + editor.schedule_sharp_preview(Duration::ZERO, cx), + SharpPreview::ChosenQuality + ); + }); + } + #[gpui::test] fn a_committed_adoption_asks_for_the_sharp_redraw_when_paused( cx: &mut gpui::TestAppContext, diff --git a/apps/desktop-gpui/src/store.rs b/apps/desktop-gpui/src/store.rs index 6d52a469943..175f40c75bd 100644 --- a/apps/desktop-gpui/src/store.rs +++ b/apps/desktop-gpui/src/store.rs @@ -503,6 +503,20 @@ pub fn set_store_setting(section: &str, key: &str, value: Value) -> bool { }) } +pub fn set_editor_preview_quality(quality: EditorPreviewQuality) -> bool { + update_store_section(GENERAL_SETTINGS, |general| { + general.insert( + "editorPreviewQuality".to_string(), + Value::from(quality.as_json()), + ); + general.insert( + EDITOR_PREVIEW_QUALITY_CHOSEN_KEY.to_string(), + Value::Bool(true), + ); + true + }) +} + /// Replace or remove one top-level store section. `authStore.set(null)` in /// the Tauri app drops the whole `auth` object; writing keys one at a time /// would leave a half-signed-out session behind. @@ -885,8 +899,50 @@ impl EditorPreviewQuality { Self::Quarter => 0.25, } } + + /// Every earlier build wrote the whole settings object, so a stored `half` + /// without [`EDITOR_PREVIEW_QUALITY_CHOSEN_KEY`] is the old default, not a + /// choice. Full and Quarter were only ever picked by hand. + fn from_general(general: &Map) -> Self { + let stored = general + .get("editorPreviewQuality") + .and_then(Value::as_str) + .and_then(Self::from_json); + match stored { + Some(quality) + if quality != Self::Half + || bool_at(general, EDITOR_PREVIEW_QUALITY_CHOSEN_KEY, false) => + { + quality + } + _ => Self::for_this_machine(), + } + } + + /// Full on Apple Silicon, where the GPU draws it at no measurable cost, + /// and Half elsewhere until Full is measured there. A renderer that fell + /// back to (or was forced onto) a software adapter reads back every frame + /// on the CPU, so it stays on Half too. + pub fn for_this_machine() -> Self { + let software = SOFTWARE_RENDERING.load(std::sync::atomic::Ordering::Acquire) + || cap_rendering::force_software_wgpu_adapter(); + if cfg!(all(target_os = "macos", target_arch = "aarch64")) && !software { + Self::Full + } else { + Self::Half + } + } + + /// Called with the shared render device's adapter, or `true` when no + /// hardware device could be made. + pub fn note_rendering_adapter(software: bool) { + SOFTWARE_RENDERING.store(software, std::sync::atomic::Ordering::Release); + } } +static SOFTWARE_RENDERING: std::sync::atomic::AtomicBool = + std::sync::atomic::AtomicBool::new(false); + /// One entry of `general_settings.excludedWindows` (`WindowExclusion`). #[derive(Debug, Clone, PartialEq, Eq, Default)] pub struct WindowExclusion { @@ -1099,6 +1155,7 @@ pub struct GeneralSettings { pub const SHOW_MAIN_WINDOW_AFTER_INSTANT_KEY: &str = "showMainWindowAfterInstantRecording"; pub const EDITOR_PREVIEW_SPEED_KEY: &str = "editorPreviewSpeed"; +pub const EDITOR_PREVIEW_QUALITY_CHOSEN_KEY: &str = "editorPreviewQualityChosen"; pub const EDITOR_PREVIEW_ENHANCE_KEY: &str = "editorPreviewEnhance"; pub const RECORDING_MARKER_SOUND_KEY: &str = "recordingMarkerSound"; @@ -1550,7 +1607,7 @@ impl GeneralSettings { update_download_policy: enum_at(general, "updateDownloadPolicy"), server_url: string_at(general, "serverUrl", DEFAULT_SERVER_URL), enable_telemetry: bool_at(general, "enableTelemetry", true), - editor_preview_quality: enum_at(general, "editorPreviewQuality"), + editor_preview_quality: EditorPreviewQuality::from_general(general), editor_preview_speed: general .get(EDITOR_PREVIEW_SPEED_KEY) .and_then(Value::as_f64) @@ -3738,6 +3795,65 @@ mod tests { assert!(bundle_first.matches(Some("com.cap.desktop"), None, Some("Wrong"))); } + #[test] + fn editor_preview_quality_defaults_per_machine_until_chosen() { + let read = |quality: Option<&str>, chosen: Option| { + let mut general = Map::new(); + if let Some(quality) = quality { + general.insert("editorPreviewQuality".to_string(), Value::from(quality)); + } + if let Some(chosen) = chosen { + general.insert( + EDITOR_PREVIEW_QUALITY_CHOSEN_KEY.to_string(), + Value::from(chosen), + ); + } + GeneralSettings::from_sections(&general, &Map::new(), &Map::new()) + .editor_preview_quality + }; + let machine = EditorPreviewQuality::for_this_machine(); + if cfg!(all(target_os = "macos", target_arch = "aarch64")) + && !cap_rendering::force_software_wgpu_adapter() + { + assert_eq!(machine, EditorPreviewQuality::Full); + } + if cfg!(target_os = "linux") { + assert_eq!(machine, EditorPreviewQuality::Half); + } + assert_eq!(read(None, None), machine); + assert_eq!(read(Some("half"), None), machine); + assert_eq!(read(Some("half"), Some(false)), machine); + assert_eq!(read(Some("bogus"), Some(true)), machine); + assert_eq!(read(Some("half"), Some(true)), EditorPreviewQuality::Half); + assert_eq!(read(Some("full"), None), EditorPreviewQuality::Full); + assert_eq!(read(Some("quarter"), None), EditorPreviewQuality::Quarter); + assert_eq!(read(Some("full"), Some(true)), EditorPreviewQuality::Full); + } + + #[test] + fn choosing_half_keeps_it_over_the_machine_default() { + let store = TempStore::new( + "preview-quality", + Some(r#"{ "general_settings": { "editorPreviewQuality": "half", "theme": "dark" } }"#), + ); + assert_eq!( + GeneralSettings::load().editor_preview_quality, + EditorPreviewQuality::for_this_machine() + ); + assert!(set_editor_preview_quality(EditorPreviewQuality::Half)); + let written = store.read(); + assert_eq!(written["general_settings"]["editorPreviewQuality"], "half"); + assert_eq!( + written["general_settings"][EDITOR_PREVIEW_QUALITY_CHOSEN_KEY], + true + ); + assert_eq!(written["general_settings"]["theme"], "dark"); + assert_eq!( + GeneralSettings::load().editor_preview_quality, + EditorPreviewQuality::Half + ); + } + #[test] fn editor_preview_speed_reads_offered_steps_and_falls_back_to_normal() { let read = |value: Option| {