Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

Duo: keep each hosted window's last frame while its context is missing

Jackyf680c8ceb4ec
diff --git a/Research/Guest/iphone_duo_bringup.md b/Research/Guest/iphone_duo_bringup.md
index a1b6b8f9..37505ee6 100644
--- a/Research/Guest/iphone_duo_bringup.md
+++ b/Research/Guest/iphone_duo_bringup.md
@@ -4653,3 +4653,47 @@ a fold, the Dock ignored taps until the next restart while the home screen's
own icons kept working. Not yet explained; the next occurrence should be
caught with `logs.syslog` for SpringBoard during a Dock tap ("Sending UIEvent
... to window" and "Allowing tap for icon view").
+
+## 2026-10-29: host zombification against app-launch black frames
+
+**Symptom.** After the fixes above the user still saw an occasional flicker.
+A 120 Hz ScreenCaptureKit capture of the VM window, with per-frame luma,
+found it: one or two frames in which the whole panel is black (luma 0, the
+compositor root's own black), sometimes alternating with a frame in which an
+app is only partly drawn, about 0.4 to 0.6 s after an app launch or on the
+way home. Same rate in portrait and landscape; never on the cover (no
+compositing there). Sequence used: launch Safari, home, Settings, home,
+Safari by `apps.launch`, 2.5 s apart; it flashed in about 18 of 26 runs.
+
+**Ruled out, each by the same measurement:**
+- Host layer changes: a trace of every `CALayerHost` add, remove, frame and
+ zPosition change logged nothing during a flash.
+- SpringBoard fences: `CAFenceHandle` / `-[CAContext addFence:]` bursts
+ (logged with a calibration mark, a one-frame white layer, to align the
+ guest clock with the capture) do not coincide with the flashes.
+- GPU load of the inner virtual display: with SpringBoard keeping the inner
+ display powered off while it owned the panel, content kept updating and the
+ flashes stayed (3 of 5 runs). The inner display's update rate does move the
+ rate (30 Hz 1/5, 60 Hz ~5/7, 120 Hz 5/5), which follows how often apps
+ commit, not load.
+- `copyDisplayTimingsFromDisplay:` (inner timed by Main): no change.
+- `rendersAsynchronously` with an `asynchronousRenderPeriod` of 1/120: the
+ hosts draw nothing at all, as before.
+- A render-server probe in SpringBoard (`CARenderServerRenderDisplay` of
+ both displays) sees Main and the inner display change together, but costs
+ 15 to 80 ms a sample and misses one- and two-frame events.
+
+**Fix.** `CALayerHost.zombificationMode = kCALayerHostZombificationModeAlways`
+on the compositor's hosts: a host whose context the render server briefly
+finds nothing to draw for keeps that context's last frame. The same
+sequence then flashed in 3 of 19 runs (one event each). Setting it on every
+`CALayerHost` SpringBoard makes (its scene hosts too) was no better (4 of
+10), so only the compositor's hosts get it. Going home, the app switcher,
+rotation and folding show no stale content (the switcher's blank snapshot
+cards are the same without it).
+
+**Left.** The remaining flash is still a fully black frame at an app launch.
+Likely, but not shown, it is SpringBoard's new scene host for the launching
+app drawing nothing for its first frame when the primary display renders
+it, the same first-frame blank the compositor's own hosts have; there is
+nothing to keep for a new host.
diff --git a/VPhoneGuestComponents/FoldDisplay/FoldDisplayPanel.m b/VPhoneGuestComponents/FoldDisplay/FoldDisplayPanel.m
index bfd5e593..7d594b4e 100644
--- a/VPhoneGuestComponents/FoldDisplay/FoldDisplayPanel.m
+++ b/VPhoneGuestComponents/FoldDisplay/FoldDisplayPanel.m
@@ -158,7 +158,13 @@ void vpInstallSharedPanel(void) {
// a visible change. (An opacity flip on the container does the same, but
// below 1 the container renders offscreen as a group, and hosted secure
// windows come out as grey blocks on those frames.)
+//
+// Each host keeps its context's last frame (zombification "always") when the
+// render server briefly finds nothing to draw for it, which it does while
+// applications launch and commit: without that, the primary display showed
+// one or two frames of the bare black root, about one in every app launch.
static id vpInnerHostRoot, vpInnerHostContainer, vpInnerHostDamage, vpInnerHostRefresh;
+static NSString *vpInnerHostZombification;
static CFRunLoopObserverRef vpInnerHostObserver;
static NSMutableDictionary<NSNumber *, id> *vpInnerHostLayers;
static CGSize vpInnerHostPanel;
@@ -248,6 +254,8 @@ static void vpSyncInnerHosts(void) {
if (!host) {
host = [objc_getClass("CALayerHost") new];
((void (*)(id, SEL, unsigned int))objc_msgSend)(host, sel_registerName("setContextId:"), contextId);
+ if (vpInnerHostZombification)
+ ((void (*)(id, SEL, id))objc_msgSend)(host, sel_registerName("setZombificationMode:"), vpInnerHostZombification);
((void (*)(id, SEL, id))objc_msgSend)(vpInnerHostContainer, sel_registerName("addSublayer:"), host);
vpInnerHostLayers[key] = host;
}
@@ -291,6 +299,11 @@ static void vpShowInnerDisplay(BOOL shown) {
hostContext ? method_getTypeEncoding(hostContext) : "missing"]);
return;
}
+ NSString *__unsafe_unretained *zombification =
+ (NSString *__unsafe_unretained *)dlsym(RTLD_DEFAULT, "kCALayerHostZombificationModeAlways");
+ Method zombify = class_getInstanceMethod(objc_getClass("CALayerHost"), sel_registerName("setZombificationMode:"));
+ vpInnerHostZombification = zombification && zombify && strcmp(method_getTypeEncoding(zombify), "v24@0:8@16") == 0 ? *zombification : nil;
+ if (!vpInnerHostZombification) vpFoldDisplayLog(@"inner display: host zombification unavailable");
id root = [objc_getClass("CALayer") new];
((void (*)(id, SEL, CGColorRef))objc_msgSend)(root, sel_registerName("setBackgroundColor:"),
((CGColorRef (*)(id, SEL))objc_msgSend)(vpReadObject(objc_getClass("UIColor"), "blackColor"), sel_registerName("CGColor")));