mirror of
https://github.com/zed-industries/zed.git
synced 2025-01-01 22:01:32 +03:00
Maintain smooth frame rates when ProMotion and direct mode are enabled (#7305)
This is achieved by starting a `CADisplayLink` that will invoke the `on_request_frame` callback at the refresh interval of the display. We only actually draw frames when the window was dirty, or for 2 extra seconds after the last input event to ensure ProMotion doesn't downclock the refresh rate when the user is actively interacting with the window. Release Notes: - Improved performance when using a ProMotion display with fast key repeat rates. --------- Co-authored-by: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
parent
f2ba969d5b
commit
15edc46827
@ -652,27 +652,20 @@ impl AppContext {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for window in self.windows.values() {
|
|
||||||
if let Some(window) = window.as_ref() {
|
|
||||||
if window.dirty {
|
|
||||||
window.platform_window.invalidate();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(any(test, feature = "test-support"))]
|
#[cfg(any(test, feature = "test-support"))]
|
||||||
for window in self
|
for window in self
|
||||||
.windows
|
.windows
|
||||||
.values()
|
.values()
|
||||||
.filter_map(|window| {
|
.filter_map(|window| {
|
||||||
let window = window.as_ref()?;
|
let window = window.as_ref()?;
|
||||||
(window.dirty || window.focus_invalidated).then_some(window.handle)
|
(window.dirty.get() || window.focus_invalidated).then_some(window.handle)
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
{
|
{
|
||||||
self.update_window(window, |_, cx| cx.draw()).unwrap();
|
self.update_window(window, |_, cx| cx.draw()).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(clippy::collapsible_else_if)]
|
||||||
if self.pending_effects.is_empty() {
|
if self.pending_effects.is_empty() {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -749,7 +742,7 @@ impl AppContext {
|
|||||||
fn apply_refresh_effect(&mut self) {
|
fn apply_refresh_effect(&mut self) {
|
||||||
for window in self.windows.values_mut() {
|
for window in self.windows.values_mut() {
|
||||||
if let Some(window) = window.as_mut() {
|
if let Some(window) = window.as_mut() {
|
||||||
window.dirty = true;
|
window.dirty.set(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -175,7 +175,6 @@ pub(crate) trait PlatformWindow: HasWindowHandle + HasDisplayHandle {
|
|||||||
fn on_close(&self, callback: Box<dyn FnOnce()>);
|
fn on_close(&self, callback: Box<dyn FnOnce()>);
|
||||||
fn on_appearance_changed(&self, callback: Box<dyn FnMut()>);
|
fn on_appearance_changed(&self, callback: Box<dyn FnMut()>);
|
||||||
fn is_topmost_for_position(&self, position: Point<Pixels>) -> bool;
|
fn is_topmost_for_position(&self, position: Point<Pixels>) -> bool;
|
||||||
fn invalidate(&self);
|
|
||||||
fn draw(&self, scene: &Scene);
|
fn draw(&self, scene: &Scene);
|
||||||
|
|
||||||
fn sprite_atlas(&self) -> Arc<dyn PlatformAtlas>;
|
fn sprite_atlas(&self) -> Arc<dyn PlatformAtlas>;
|
||||||
|
@ -16,8 +16,8 @@ use cocoa::{
|
|||||||
},
|
},
|
||||||
base::{id, nil},
|
base::{id, nil},
|
||||||
foundation::{
|
foundation::{
|
||||||
NSArray, NSAutoreleasePool, NSDictionary, NSFastEnumeration, NSInteger, NSPoint, NSRect,
|
NSArray, NSAutoreleasePool, NSDefaultRunLoopMode, NSDictionary, NSFastEnumeration,
|
||||||
NSSize, NSString, NSUInteger,
|
NSInteger, NSPoint, NSRect, NSSize, NSString, NSUInteger,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
use core_graphics::display::CGRect;
|
use core_graphics::display::CGRect;
|
||||||
@ -321,6 +321,7 @@ struct MacWindowState {
|
|||||||
executor: ForegroundExecutor,
|
executor: ForegroundExecutor,
|
||||||
native_window: id,
|
native_window: id,
|
||||||
native_view: NonNull<id>,
|
native_view: NonNull<id>,
|
||||||
|
display_link: id,
|
||||||
renderer: MetalRenderer,
|
renderer: MetalRenderer,
|
||||||
kind: WindowKind,
|
kind: WindowKind,
|
||||||
request_frame_callback: Option<Box<dyn FnMut()>>,
|
request_frame_callback: Option<Box<dyn FnMut()>>,
|
||||||
@ -522,6 +523,10 @@ impl MacWindow {
|
|||||||
let native_view: id = msg_send![VIEW_CLASS, alloc];
|
let native_view: id = msg_send![VIEW_CLASS, alloc];
|
||||||
let native_view = NSView::init(native_view);
|
let native_view = NSView::init(native_view);
|
||||||
|
|
||||||
|
let display_link: id = msg_send![class!(CADisplayLink), displayLinkWithTarget: native_view selector: sel!(displayLayer:)];
|
||||||
|
let main_run_loop: id = msg_send![class!(NSRunLoop), mainRunLoop];
|
||||||
|
let _: () =
|
||||||
|
msg_send![display_link, addToRunLoop: main_run_loop forMode: NSDefaultRunLoopMode];
|
||||||
assert!(!native_view.is_null());
|
assert!(!native_view.is_null());
|
||||||
|
|
||||||
let window = Self(Arc::new(Mutex::new(MacWindowState {
|
let window = Self(Arc::new(Mutex::new(MacWindowState {
|
||||||
@ -529,6 +534,7 @@ impl MacWindow {
|
|||||||
executor,
|
executor,
|
||||||
native_window,
|
native_window,
|
||||||
native_view: NonNull::new_unchecked(native_view as *mut _),
|
native_view: NonNull::new_unchecked(native_view as *mut _),
|
||||||
|
display_link,
|
||||||
renderer: MetalRenderer::new(true),
|
renderer: MetalRenderer::new(true),
|
||||||
kind: options.kind,
|
kind: options.kind,
|
||||||
request_frame_callback: None,
|
request_frame_callback: None,
|
||||||
@ -687,6 +693,9 @@ impl Drop for MacWindow {
|
|||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
let this = self.0.lock();
|
let this = self.0.lock();
|
||||||
let window = this.native_window;
|
let window = this.native_window;
|
||||||
|
unsafe {
|
||||||
|
let _: () = msg_send![this.display_link, invalidate];
|
||||||
|
}
|
||||||
this.executor
|
this.executor
|
||||||
.spawn(async move {
|
.spawn(async move {
|
||||||
unsafe {
|
unsafe {
|
||||||
@ -1000,13 +1009,6 @@ impl PlatformWindow for MacWindow {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn invalidate(&self) {
|
|
||||||
let this = self.0.lock();
|
|
||||||
unsafe {
|
|
||||||
let _: () = msg_send![this.native_window.contentView(), setNeedsDisplay: YES];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn draw(&self, scene: &crate::Scene) {
|
fn draw(&self, scene: &crate::Scene) {
|
||||||
let mut this = self.0.lock();
|
let mut this = self.0.lock();
|
||||||
this.renderer.draw(scene);
|
this.renderer.draw(scene);
|
||||||
|
@ -282,8 +282,6 @@ impl PlatformWindow for TestWindow {
|
|||||||
unimplemented!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn invalidate(&self) {}
|
|
||||||
|
|
||||||
fn draw(&self, _scene: &crate::Scene) {}
|
fn draw(&self, _scene: &crate::Scene) {}
|
||||||
|
|
||||||
fn sprite_atlas(&self) -> sync::Arc<dyn crate::PlatformAtlas> {
|
fn sprite_atlas(&self) -> sync::Arc<dyn crate::PlatformAtlas> {
|
||||||
|
@ -22,7 +22,7 @@ use smallvec::SmallVec;
|
|||||||
use std::{
|
use std::{
|
||||||
any::{Any, TypeId},
|
any::{Any, TypeId},
|
||||||
borrow::{Borrow, BorrowMut},
|
borrow::{Borrow, BorrowMut},
|
||||||
cell::RefCell,
|
cell::{Cell, RefCell},
|
||||||
collections::hash_map::Entry,
|
collections::hash_map::Entry,
|
||||||
fmt::{Debug, Display},
|
fmt::{Debug, Display},
|
||||||
future::Future,
|
future::Future,
|
||||||
@ -34,7 +34,7 @@ use std::{
|
|||||||
atomic::{AtomicUsize, Ordering::SeqCst},
|
atomic::{AtomicUsize, Ordering::SeqCst},
|
||||||
Arc,
|
Arc,
|
||||||
},
|
},
|
||||||
time::Duration,
|
time::{Duration, Instant},
|
||||||
};
|
};
|
||||||
use util::{measure, ResultExt};
|
use util::{measure, ResultExt};
|
||||||
|
|
||||||
@ -272,7 +272,8 @@ pub struct Window {
|
|||||||
appearance: WindowAppearance,
|
appearance: WindowAppearance,
|
||||||
appearance_observers: SubscriberSet<(), AnyObserver>,
|
appearance_observers: SubscriberSet<(), AnyObserver>,
|
||||||
active: bool,
|
active: bool,
|
||||||
pub(crate) dirty: bool,
|
pub(crate) dirty: Rc<Cell<bool>>,
|
||||||
|
pub(crate) last_input_timestamp: Rc<Cell<Instant>>,
|
||||||
pub(crate) refreshing: bool,
|
pub(crate) refreshing: bool,
|
||||||
pub(crate) drawing: bool,
|
pub(crate) drawing: bool,
|
||||||
activation_observers: SubscriberSet<(), AnyObserver>,
|
activation_observers: SubscriberSet<(), AnyObserver>,
|
||||||
@ -342,13 +343,28 @@ impl Window {
|
|||||||
let bounds = platform_window.bounds();
|
let bounds = platform_window.bounds();
|
||||||
let appearance = platform_window.appearance();
|
let appearance = platform_window.appearance();
|
||||||
let text_system = Arc::new(WindowTextSystem::new(cx.text_system().clone()));
|
let text_system = Arc::new(WindowTextSystem::new(cx.text_system().clone()));
|
||||||
|
let dirty = Rc::new(Cell::new(false));
|
||||||
|
let last_input_timestamp = Rc::new(Cell::new(Instant::now()));
|
||||||
|
|
||||||
platform_window.on_request_frame(Box::new({
|
platform_window.on_request_frame(Box::new({
|
||||||
let mut cx = cx.to_async();
|
let mut cx = cx.to_async();
|
||||||
|
let dirty = dirty.clone();
|
||||||
|
let last_input_timestamp = last_input_timestamp.clone();
|
||||||
move || {
|
move || {
|
||||||
measure("frame duration", || {
|
if dirty.get() {
|
||||||
handle.update(&mut cx, |_, cx| cx.draw()).log_err();
|
measure("frame duration", || {
|
||||||
})
|
handle
|
||||||
|
.update(&mut cx, |_, cx| {
|
||||||
|
cx.draw();
|
||||||
|
cx.present();
|
||||||
|
})
|
||||||
|
.log_err();
|
||||||
|
})
|
||||||
|
} else if last_input_timestamp.get().elapsed() < Duration::from_secs(2) {
|
||||||
|
// Keep presenting the current scene for 2 extra seconds since the
|
||||||
|
// last input to prevent the display from underclocking the refresh rate.
|
||||||
|
handle.update(&mut cx, |_, cx| cx.present()).log_err();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
platform_window.on_resize(Box::new({
|
platform_window.on_resize(Box::new({
|
||||||
@ -427,7 +443,8 @@ impl Window {
|
|||||||
appearance,
|
appearance,
|
||||||
appearance_observers: SubscriberSet::new(),
|
appearance_observers: SubscriberSet::new(),
|
||||||
active: false,
|
active: false,
|
||||||
dirty: false,
|
dirty,
|
||||||
|
last_input_timestamp,
|
||||||
refreshing: false,
|
refreshing: false,
|
||||||
drawing: false,
|
drawing: false,
|
||||||
activation_observers: SubscriberSet::new(),
|
activation_observers: SubscriberSet::new(),
|
||||||
@ -488,7 +505,7 @@ impl<'a> WindowContext<'a> {
|
|||||||
pub fn refresh(&mut self) {
|
pub fn refresh(&mut self) {
|
||||||
if !self.window.drawing {
|
if !self.window.drawing {
|
||||||
self.window.refreshing = true;
|
self.window.refreshing = true;
|
||||||
self.window.dirty = true;
|
self.window.dirty.set(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -962,9 +979,10 @@ impl<'a> WindowContext<'a> {
|
|||||||
&self.window.next_frame.z_index_stack
|
&self.window.next_frame.z_index_stack
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Draw pixels to the display for this window based on the contents of its scene.
|
/// Produces a new frame and assigns it to `rendered_frame`. To actually show
|
||||||
|
/// the contents of the new [Scene], use [present].
|
||||||
pub(crate) fn draw(&mut self) {
|
pub(crate) fn draw(&mut self) {
|
||||||
self.window.dirty = false;
|
self.window.dirty.set(false);
|
||||||
self.window.drawing = true;
|
self.window.drawing = true;
|
||||||
|
|
||||||
#[cfg(any(test, feature = "test-support"))]
|
#[cfg(any(test, feature = "test-support"))]
|
||||||
@ -1105,16 +1123,19 @@ impl<'a> WindowContext<'a> {
|
|||||||
.clone()
|
.clone()
|
||||||
.retain(&(), |listener| listener(&event, self));
|
.retain(&(), |listener| listener(&event, self));
|
||||||
}
|
}
|
||||||
|
|
||||||
self.window
|
|
||||||
.platform_window
|
|
||||||
.draw(&self.window.rendered_frame.scene);
|
|
||||||
self.window.refreshing = false;
|
self.window.refreshing = false;
|
||||||
self.window.drawing = false;
|
self.window.drawing = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn present(&self) {
|
||||||
|
self.window
|
||||||
|
.platform_window
|
||||||
|
.draw(&self.window.rendered_frame.scene);
|
||||||
|
}
|
||||||
|
|
||||||
/// Dispatch a mouse or keyboard event on the window.
|
/// Dispatch a mouse or keyboard event on the window.
|
||||||
pub fn dispatch_event(&mut self, event: PlatformInput) -> bool {
|
pub fn dispatch_event(&mut self, event: PlatformInput) -> bool {
|
||||||
|
self.window.last_input_timestamp.set(Instant::now());
|
||||||
// Handlers may set this to false by calling `stop_propagation`.
|
// Handlers may set this to false by calling `stop_propagation`.
|
||||||
self.app.propagate_event = true;
|
self.app.propagate_event = true;
|
||||||
// Handlers may set this to true by calling `prevent_default`.
|
// Handlers may set this to true by calling `prevent_default`.
|
||||||
@ -2058,7 +2079,7 @@ impl<'a, V: 'static> ViewContext<'a, V> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !self.window.drawing {
|
if !self.window.drawing {
|
||||||
self.window_cx.window.dirty = true;
|
self.window_cx.window.dirty.set(true);
|
||||||
self.window_cx.app.push_effect(Effect::Notify {
|
self.window_cx.app.push_effect(Effect::Notify {
|
||||||
emitter: self.view.model.entity_id,
|
emitter: self.view.model.entity_id,
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user