1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
//! The source on macOS, iOS and tvOS: a `CADisplayLink`.
/// Every pacer's display link runs on one thread that the library starts for
/// them, on a run loop that runs for the rest of the process, so a pacer
/// needs no run loop of the host's. A display link and its run loop aren't
/// thread-safe, so once the link is in that run loop, the pacer touches it
/// only on that thread: a call from another thread runs a task there. A link
/// that the main thread makes, as `AppKit` and `UIKit` ask, goes to that
/// thread before anything else uses it. A display link retains its target
/// until it is invalidated, so the target reaches the pacer through
/// [`Vsyncs`], which holds it weakly.
#[cfg(any(target_os = "macos", target_os = "ios", target_os = "tvos"))]
pub(crate) mod display_link {
use core::ffi::{c_int, c_void};
use std::collections::VecDeque;
use std::sync::mpsc;
use std::thread::{self, ThreadId};
use std::time::Duration;
use mln_style::error::Error;
use objc2::rc::{Retained, autoreleasepool};
use objc2::runtime::{AnyObject, NSObject};
use objc2::{AllocAnyThread, DefinedClass, define_class, msg_send, sel};
use objc2_core_foundation::{
CFRetained, CFRunLoop, CFRunLoopSource, CFRunLoopSourceContext, kCFRunLoopDefaultMode,
};
use objc2_foundation::{NSDefaultRunLoopMode, NSRunLoop};
use objc2_quartz_core::{CADisplayLink, CAFrameRateRange, CAFrameRateRangeDefault};
use parking_lot::Mutex;
use crate::frame_pacer::{Limit, VsyncSource, Vsyncs};
// A vsync that waits behind other work delays the frames it demands.
const QOS_CLASS_USER_INTERACTIVE: u32 = 0x21;
unsafe extern "C" {
fn pthread_set_qos_class_self_np(qos_class: u32, relative_priority: c_int) -> c_int;
}
// NSView and NSScreen, which make the display links on macOS, are
// AppKit's.
#[cfg(target_os = "macos")]
#[link(name = "AppKit", kind = "framework")]
unsafe extern "C" {}
type Task = Box<dyn FnOnce() + Send>;
/// The tasks that wait for the link thread.
static TASKS: Mutex<VecDeque<Task>> = Mutex::new(VecDeque::new());
/// The link thread, once it started.
static THREAD: Mutex<Option<&'static LinkThread>> = Mutex::new(None);
/// A Core Foundation object that the link thread and its callers share.
struct Shared<T: ?Sized>(CFRetained<T>);
// SAFETY: the run loop and its source are used from other threads only
// through CFRunLoopSourceSignal() and CFRunLoopWakeUp(), which are
// thread-safe.
unsafe impl<T: ?Sized> Send for Shared<T> {}
// SAFETY: as for `Send`.
unsafe impl<T: ?Sized> Sync for Shared<T> {}
struct LinkThread {
id: ThreadId,
run_loop: Shared<CFRunLoop>,
/// Signalled to run the tasks.
source: Shared<CFRunLoopSource>,
}
impl LinkThread {
/// The thread, which starts on first use.
fn get() -> Result<&'static Self, Error> {
let mut started = THREAD.lock();
if let Some(thread) = *started {
return Ok(thread);
}
let (ready, readied) = mpsc::channel();
thread::Builder::new()
.name("MLN frame pacer".into())
.spawn(move || run(&ready))
.map_err(|error| Error::failed(format!("the frame pacer could not start its thread: {error}")))?;
let thread =
readied.recv().map_err(|_| Error::failed("the frame pacer's thread could not start its run loop"))?;
let thread: &'static Self = Box::leak(Box::new(thread));
*started = Some(thread);
Ok(thread)
}
fn is_current(&self) -> bool {
thread::current().id() == self.id
}
/// Runs `task` on the thread, after the tasks sent before it.
fn send(&self, task: impl FnOnce() + Send + 'static) {
TASKS.lock().push_back(Box::new(task));
self.source.0.signal();
self.run_loop.0.wake_up();
}
/// Runs `task` on the thread and waits for its result, which is
/// `None` when the task panicked.
fn call<R: Send + 'static>(&self, task: impl FnOnce() -> R + Send + 'static) -> Option<R> {
if self.is_current() {
return Some(task());
}
let (done, result) = mpsc::sync_channel(1);
self.send(move || {
let _ = done.send(task());
});
result.recv().ok()
}
}
/// The link thread's body.
fn run(ready: &mpsc::Sender<LinkThread>) {
// SAFETY: sets the calling thread's own class.
let _ = unsafe { pthread_set_qos_class_self_np(QOS_CLASS_USER_INTERACTIVE, 0) };
let Some(run_loop) = CFRunLoop::current() else {
return;
};
let mut context = CFRunLoopSourceContext {
version: 0,
info: core::ptr::null_mut(),
retain: None,
release: None,
copyDescription: None,
equal: None,
hash: None,
schedule: None,
cancel: None,
perform: Some(perform),
};
// SAFETY: a version 0 context whose callbacks take no `info`.
let Some(source) = (unsafe { CFRunLoopSource::new(None, 0, &raw mut context) }) else {
return;
};
// SAFETY: a constant of Core Foundation's.
let mode = unsafe { kCFRunLoopDefaultMode };
// The source also keeps the run loop running: a run loop returns at
// once while its mode has no source, as it has while no pacer has a
// display link.
run_loop.add_source(Some(&source), mode);
let thread = LinkThread { id: thread::current().id(), run_loop: Shared(run_loop), source: Shared(source) };
if ready.send(thread).is_err() {
return;
}
loop {
CFRunLoop::run();
}
}
/// The source's callback, on the link thread: runs the tasks that wait.
unsafe extern "C-unwind" fn perform(_info: *mut c_void) {
loop {
let Some(task) = TASKS.lock().pop_front() else {
return;
};
// A panic stays on this side of the run loop.
let _ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| autoreleasepool(|_| task())));
}
}
/// A display link, which only the link thread uses.
#[derive(Clone)]
struct Link(Retained<CADisplayLink>);
// SAFETY: the link's methods run only on the thread that makes it, until
// it hands the link to the link thread, and on the link thread after;
// other threads only retain and release it, which is thread-safe.
unsafe impl Send for Link {}
// SAFETY: as for `Send`.
unsafe impl Sync for Link {}
impl Link {
/// Resumes the link, which calls back at the next refresh. Runs on
/// the link thread.
fn resume(&self) {
self.0.setPaused(false);
}
/// Lets a display with a variable refresh rate slow down to `limit`,
/// or, without one, run at its own rate again. Runs on the link
/// thread.
fn set_limit(&self, limit: Option<Limit>) {
let range = match limit {
Some(limit) => {
let rate = limit.frames_per_second() as f32;
CAFrameRateRange { minimum: rate, maximum: rate, preferred: rate }
}
// SAFETY: a constant of Core Animation's.
None => unsafe { CAFrameRateRangeDefault },
};
self.0.setPreferredFrameRateRange(range);
}
/// Runs on the link thread.
fn add_to_current_run_loop(&self) {
// SAFETY: the link thread's own run loop.
unsafe { self.0.addToRunLoop_forMode(&NSRunLoop::currentRunLoop(), NSDefaultRunLoopMode) };
}
/// Runs on the link thread.
fn invalidate(&self) {
self.0.invalidate();
}
}
define_class!(
// SAFETY: NSObject has no subclassing requirements, and the class
// doesn't implement `Drop`.
#[unsafe(super(NSObject))]
#[name = "MLNRustFramePacerTarget"]
#[ivars = Vsyncs]
struct Target;
impl Target {
/// The display link's callback, on the link thread.
#[unsafe(method(vsync:))]
fn vsync(&self, link: &CADisplayLink) {
// A paused link calls back no more until the pacer schedules
// another refresh.
link.setPaused(true);
// The timestamp is in seconds on the host's monotonic clock.
let time = Duration::try_from_secs_f64(link.timestamp()).unwrap_or_default();
self.ivars().vsync(time);
}
}
);
impl Target {
fn new(vsyncs: Vsyncs) -> Retained<Self> {
let this = Self::alloc().set_ivars(vsyncs);
// SAFETY: NSObject's initializer.
unsafe { msg_send![super(this), init] }
}
}
/// An `NSView` on macOS, or a `UIView` on iOS and tvOS, whose window's
/// display a pacer follows.
#[derive(Clone, Copy)]
pub(crate) struct View(pub(crate) core::ptr::NonNull<c_void>);
impl View {
/// The view, retained. Runs on the main thread.
fn retain(self) -> Result<Retained<AnyObject>, Error> {
// SAFETY: the caller's view, which `for_view`'s contract makes a
// view.
unsafe { Retained::retain(self.0.as_ptr().cast::<AnyObject>()) }
.ok_or_else(|| Error::invalid_argument("the frame pacer's view is null"))
}
}
/// What makes the link: the view, whose link follows its window between
/// displays, or the main `NSScreen`. Runs on the main thread, or without
/// a view, on any.
#[cfg(target_os = "macos")]
fn link_owner(view: Option<View>) -> Result<Retained<AnyObject>, Error> {
use objc2::runtime::AnyClass;
if let Some(view) = view {
return view.retain();
}
let no_display = || Error::failed("the system has no display for the pacer to follow");
let screens = AnyClass::get(c"NSScreen").ok_or_else(no_display)?;
// SAFETY: `+[NSScreen screens]`, an array of screens, or nil without
// a window server.
let screens: Option<Retained<AnyObject>> = unsafe { msg_send![screens, screens] };
// SAFETY: `-[NSArray firstObject]`, the main display, or nil.
let screen: Option<Retained<AnyObject>> = unsafe { msg_send![&*screens.ok_or_else(no_display)?, firstObject] };
screen.ok_or_else(no_display)
}
/// What makes the link: the view's window's `UIScreen`, or none for the
/// main screen's. Runs on the main thread.
#[cfg(not(target_os = "macos"))]
fn link_owner(view: Option<View>) -> Result<Option<Retained<AnyObject>>, Error> {
let Some(view) = view else {
return Ok(None);
};
let view = view.retain()?;
// SAFETY: `-[UIView window]`, or nil for a view in no window.
let window: Option<Retained<AnyObject>> = unsafe { msg_send![&*view, window] };
let window = window.ok_or_else(|| Error::invalid_state("the frame pacer's view is in no window"))?;
// SAFETY: `-[UIWindow screen]`, which is never nil.
let screen: Retained<AnyObject> = unsafe { msg_send![&*window, screen] };
Ok(Some(screen))
}
/// A display link that calls `target`.
fn display_link(target: &Target, view: Option<View>) -> Result<Retained<CADisplayLink>, Error> {
let owner = link_owner(view)?;
#[cfg(not(target_os = "macos"))]
let Some(owner) = owner else {
// SAFETY: a target that implements the selector.
return Ok(unsafe { CADisplayLink::displayLinkWithTarget_selector(target, sel!(vsync:)) });
};
#[cfg(target_os = "macos")]
{
// SAFETY: `-[NSObject respondsToSelector:]`. macOS 14 added the
// method to NSView and NSScreen.
let available: bool =
unsafe { msg_send![&*owner, respondsToSelector: sel!(displayLinkWithTarget:selector:)] };
if !available {
return Err(Error::unsupported("the frame pacer needs macOS 14 or later"));
}
}
// SAFETY: `-[NSView displayLinkWithTarget:selector:]`,
// `-[NSScreen displayLinkWithTarget:selector:]` or
// `-[UIScreen displayLinkWithTarget:selector:]`, with a target that
// implements the selector.
let link: Option<Retained<CADisplayLink>> =
unsafe { msg_send![&*owner, displayLinkWithTarget: target, selector: sel!(vsync:)] };
link.ok_or_else(|| Error::failed("the system has no display for the pacer to follow"))
}
/// A new display link, paused and at `limit`, that reports to `vsyncs`.
fn new_link(vsyncs: Vsyncs, limit: Option<Limit>, view: Option<View>) -> Result<Link, Error> {
let target = Target::new(vsyncs);
let link = Link(display_link(&target, view)?);
link.0.setPaused(true);
if limit.is_some() {
link.set_limit(limit);
}
Ok(link)
}
/// A pacer's display link.
pub(crate) struct DisplayLinkSource {
thread: &'static LinkThread,
link: Link,
}
impl DisplayLinkSource {
/// A source on a link of `view`'s window's display, or of the main
/// display. A view's link is made on the main thread, which must run
/// this, and on macOS so is the main display's when this runs there.
pub(crate) fn new(vsyncs: Vsyncs, limit: Option<Limit>, view: Option<View>) -> Result<Self, Error> {
let on_main = objc2::MainThreadMarker::new().is_some();
if view.is_some() && !on_main {
return Err(Error::invalid_state("a frame pacer for a view must be made on the main thread"));
}
let thread = LinkThread::get()?;
let failed = || Error::failed("the frame pacer's thread failed to take its display link");
let link = if view.is_some() || (cfg!(target_os = "macos") && on_main) {
let link = new_link(vsyncs, limit, view)?;
let added = link.clone();
// A link calls back on the thread whose run loop it is in.
thread.call(move || added.add_to_current_run_loop()).ok_or_else(failed)?;
link
} else {
thread
.call(move || {
let link = new_link(vsyncs, limit, None)?;
link.add_to_current_run_loop();
Ok::<_, Error>(link)
})
.ok_or_else(failed)??
};
Ok(Self { thread, link })
}
}
impl VsyncSource for DisplayLinkSource {
fn schedule(&self) {
if self.thread.is_current() {
return self.link.resume();
}
let link = self.link.clone();
self.thread.send(move || link.resume());
}
/// Runs later even on the link thread, after the changes sent before
/// it, so that the link ends with the last limit.
fn set_limit(&self, limit: Option<Limit>) {
let link = self.link.clone();
self.thread.send(move || link.set_limit(limit));
}
}
impl Drop for DisplayLinkSource {
/// Invalidates the link, which lets go of its target. It runs later
/// even on the link thread, which can drop the pacer from its own
/// callback.
fn drop(&mut self) {
let link = self.link.clone();
self.thread.send(move || link.invalidate());
}
}
}