Apple's open-source XNU kernel on Android phones: its own loader and drivers, Metal on the Adreno. Runs from RAM; a reboot gives the phone back to Android.
C
Toward 120 frames a second: touch samples every 4 ms, surfaces let go of at once, wallpaper_freeze
yehorkaa66b8e63ee6da
diff --git a/tools/metal-run.sh b/tools/metal-run.shindex d9c1b40..58ab30c 100755--- a/tools/metal-run.sh+++ b/tools/metal-run.sh@@ -28,6 +28,7 @@# fb_hz=120 fb_measure=1 the framebuffer's beat at the panel's 120 Hz, not its default 60, and the panel's own# rate measured once (MiniIOMobileFramebuffer.cpp)# fb_threads=6 six processors scale a frame onto the panel, not three: Settings' scroll 75 -> 84 frames+# touch_poll_us=4000 the touch screen looked at every 4 ms, not 8: a sample in every refresh at 120 Hz# a second (2 November, tools/frames.py)# ufs=2 ufs_write=1 var_store=1 (PERSIST=1, the default) /private/var kept in the partition darwin between boots: the# compiled shader stages in /var/db/hakios-metal-cache survive, so only the first boot@@ -55,7 +56,7 @@ PERSIST=${PERSIST:-1}UPLOAD=${UPLOAD:-1}SHOTS=${SHOTS:-0}CA_METALLIB=${CA_METALLIB:-/redwood/share/ca-share/QuartzCore-default.metallib}-ARGS="gpu=1 metal=1 metal_fps=1 touch_lat=1 touchscreen=1 ca_metallib=$CA_METALLIB gpu_recover=1 anim_max=1 bootcpu=7 cpus=8 cpufreq=1 fb_hz=120 fb_measure=1 fb_threads=6"+ARGS="gpu=1 metal=1 metal_fps=1 touch_lat=1 touchscreen=1 ca_metallib=$CA_METALLIB gpu_recover=1 anim_max=1 bootcpu=7 cpus=8 cpufreq=1 fb_hz=120 fb_measure=1 fb_threads=6 touch_poll_us=4000"[ "$PERSIST" = 1 ] && ARGS="$ARGS ufs=2 ufs_write=1 var_store=1"[ "${VERBOSE:-0}" = 1 ] && ARGS="-v $ARGS"ARGS="$ARGS ${MORE:-}"diff --git a/userland/build.sh b/userland/build.shindex c5d86fe..e7afb01 100755--- a/userland/build.sh+++ b/userland/build.sh@@ -240,7 +240,7 @@ if [ -d $DARWIN/rootfs/usr/lib ]; then# our library for inside stock Darwin programs (the jobs in INJECT are started with it)$DYN -dynamiclib -install_name /redwood/lib/libhakios.dylib -framework CoreFoundation -framework IOKit -lobjc \-o obj/libhakios.dylib src/inject.c- $DYN -dynamiclib -install_name /redwood/lib/libhakios.sb.dylib -framework CoreFoundation -framework IOKit -lobjc \+ $DYN -dynamiclib -install_name /redwood/lib/libhakios.sb.dylib -framework CoreFoundation -framework CoreGraphics -framework IOKit -lobjc \-o obj/libhakios.sb.dylib src/inject_sb.c# ... the tweak loader of the package manager's prefix (src/inject_tweaks.c): it goes into a# program either as the one library the kernel inserts or opened by a library of oursdiff --git a/userland/src/inject_sb.c b/userland/src/inject_sb.cindex 2679398..afc2f54 100644--- a/userland/src/inject_sb.c+++ b/userland/src/inject_sb.c@@ -32,6 +32,7 @@* answered, with that program's layers.*/#include <CoreFoundation/CoreFoundation.h>+#include <CoreGraphics/CoreGraphics.h>#include <time.h>#include <IOKit/IOKitLib.h>#include <dispatch/dispatch.h>@@ -901,6 +902,123 @@ wallpaper_cache_pass(void)});}+/*+ * Boot-arg wallpaper_freeze=1: the wallpaper as one still picture. Rasterizing its window (wallpaper_cache=1) did not+ * keep it from being composed again: the poster is another process's layers (PosterBoard's, through a layer host),+ * and the window server rasterizes them again whenever that process commits -- the Collections poster's gradients+ * (a 256 x 1 half-float ramp and a 32 x 32 dither each) were still drawn 3509 times in a run, at 0.6 ms each, and the+ * rasterized copy 1460 times at 1.2 to 3.2 ms (3 November, metal_fps=draws). So once the poster has been drawn (30 s+ * on), the window is drawn into an image by the window server (-drawViewHierarchyInRect:afterScreenUpdates:, which+ * renders other processes' layers too), the image is put in the window as one opaque view, and what was there is+ * hidden: one full-screen texture a frame instead of eight to twelve layers. What that gives up: the wallpaper does+ * not change any more until the next boot (nothing here changes it), nor do the effects drawn in it.+ */+typedef struct { double x, y, width, height; } hk_rect;+typedef struct { double width, height; } hk_size;++static bool wallpaper_hide_only; /* wallpaper_freeze=2: no picture, the wallpaper hidden (what it costs, measured) */++static void+wallpaper_freeze_pass(unsigned attempt)+{+ void *uikit = dlopen("/System/Library/PrivateFrameworks/UIKitCore.framework/UIKitCore", RTLD_NOW | RTLD_NOLOAD);+ void (*begin)(hk_size, bool, double) = uikit ? (void (*)(hk_size, bool, double))dlsym(uikit, "UIGraphicsBeginImageContextWithOptions") : NULL;+ id (*grab)(void) = uikit ? (id (*)(void))dlsym(uikit, "UIGraphicsGetImageFromCurrentImageContext") : NULL;+ void (*end)(void) = uikit ? (void (*)(void))dlsym(uikit, "UIGraphicsEndImageContext") : NULL;+ id app = SEND(id, objc_getClass("UIApplication"), "sharedApplication");+ id windows = app != NULL ? SEND(id, app, "windows") : NULL;+ unsigned long count = windows != NULL ? SEND(unsigned long, windows, "count") : 0;+ id screen = SEND(id, objc_getClass("UIScreen"), "mainScreen");+ double scale = screen != NULL ? ((double (*)(id, SEL))objc_msgSend)(screen, sel_registerName("scale")) : 3.0;+ unsigned frozen = 0;++ if (begin == NULL || grab == NULL || end == NULL) {+ say("hakios: SpringBoard: wallpaper_freeze=1: no UIGraphics image context\n");+ return;+ }+ for (unsigned long i = 0; i < count; i++) {+ id window = ((id (*)(id, SEL, unsigned long))objc_msgSend)(windows, sel_registerName("objectAtIndex:"), i);+ const char *name = object_getClassName(window);++ if (strcasestr(name, "wallpaper") == NULL && strcasestr(name, "poster") == NULL) {+ continue;+ }+ if (objc_getAssociatedObject(window, (const void *)wallpaper_freeze_pass) != NULL) {+ continue; /* (frozen already) */+ }+ hk_rect bounds = ((hk_rect (*)(id, SEL))objc_msgSend)(window, sel_registerName("bounds"));+ if (bounds.width < 100 || bounds.height < 100) {+ continue;+ }+ /* a secure window is left out of every picture taken of the screen (and so of this one): not this one */+ if (class_respondsToSelector(object_getClass(window), sel_registerName("_setSecure:"))) {+ ((void (*)(id, SEL, BOOL))objc_msgSend)(window, sel_registerName("_setSecure:"), NO);+ }+ begin((hk_size){ bounds.width, bounds.height }, true, scale);+ BOOL drawn = ((BOOL (*)(id, SEL, hk_rect, BOOL))objc_msgSend)(window, sel_registerName("drawViewHierarchyInRect:afterScreenUpdates:"),+ bounds, attempt > 0); /* (from the second on, after the window server has drawn it as not secure) */+ id image = grab();+ end();+ /* a picture that is all one colour is no wallpaper (nothing was drawn yet, or the window server could not): later */+ unsigned long distinct = 0;+ CGImageRef cg = image != NULL ? SEND(CGImageRef, image, "CGImage") : NULL;+ CFDataRef data = cg != NULL ? CGDataProviderCopyData(CGImageGetDataProvider(cg)) : NULL;+ if (data != NULL) {+ const uint32_t *px = (const uint32_t *)CFDataGetBytePtr(data);+ size_t n = (size_t)CFDataGetLength(data) / 4;+ uint32_t first = n ? px[0] : 0;++ for (size_t k = 0; k < n; k += n / 97 + 1) {+ distinct += px[k] != first;+ }+ CFRelease(data);+ }+ say("hakios: SpringBoard: wallpaper_freeze=1: %s (%gx%g) drawn %s, %lu of 97 samples unlike the first\n", name,+ bounds.width, bounds.height, drawn ? "yes" : "no", distinct);+ if (!wallpaper_hide_only && (!drawn || distinct < 10)) {+ continue;+ }+ id view = ((id (*)(id, SEL, id))objc_msgSend)(SEND(id, objc_getClass("UIImageView"), "alloc"), sel_registerName("initWithImage:"), image);+ ((void (*)(id, SEL, hk_rect))objc_msgSend)(view, sel_registerName("setFrame:"), bounds);+ ((void (*)(id, SEL, BOOL))objc_msgSend)(view, sel_registerName("setOpaque:"), YES);+ id layer = SEND(id, window, "layer");+ id sublayers = SEND(id, SEND(id, layer, "sublayers"), "copy");+ unsigned long nsub = sublayers != NULL ? SEND(unsigned long, sublayers, "count") : 0;+ ((void (*)(id, SEL, id))objc_msgSend)(window, sel_registerName("addSubview:"), view);+ for (unsigned long k = 0; k < nsub; k++) {+ id sub = ((id (*)(id, SEL, unsigned long))objc_msgSend)(sublayers, sel_registerName("objectAtIndex:"), k);+ ((void (*)(id, SEL, BOOL))objc_msgSend)(sub, sel_registerName("setHidden:"), YES);+ }+ ((void (*)(id, SEL, BOOL))objc_msgSend)(layer, sel_registerName("setShouldRasterize:"), NO);+ objc_setAssociatedObject(window, (const void *)wallpaper_freeze_pass, view, OBJC_ASSOCIATION_RETAIN);+ say("hakios: SpringBoard: wallpaper_freeze=1: %s is one picture now (%lu layers hidden)\n", name, nsub);+ frozen++;+ }+ if (attempt < 12) {+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 10 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{+ wallpaper_freeze_pass(attempt + 1);+ });+ }+}++static void+wallpaper_freeze(void)+{+ char arguments[1024] = "";+ size_t size = sizeof(arguments) - 1;+ const char *argument;++ if (sysctlbyname("kern.bootargs", arguments, &size, NULL, 0) != 0 ||+ (argument = strstr(arguments, "wallpaper_freeze=")) == NULL || (argument != arguments && argument[-1] != ' ') ||+ (argument[17] != '1' && argument[17] != '2')) {+ return;+ }+ wallpaper_hide_only = argument[17] == '2';+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 30 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{+ wallpaper_freeze_pass(0);+ });+}+static voidwallpaper_cache(void){@@ -1647,6 +1765,7 @@ hakios_springboard_init(void)plain_vibrancy();ui_lite();wallpaper_cache();+ wallpaper_freeze();original_lock_long_press = (void (*)(id, SEL, id))replace_method("SBLockHardwareButton", "longPress:", 0x1c4f6ab44UL,(IMP)lock_long_press);poster_switcher = replace_method("SBMainDisplayPolicyAggregator", "_allowsCapabilityPosterSwitcherWithExplanation:",diff --git a/userland/src/rwtouch.c b/userland/src/rwtouch.cindex 376269d..9dd7411 100644--- a/userland/src/rwtouch.c+++ b/userland/src/rwtouch.c@@ -62,7 +62,14 @@ button(uint32_t which, int ms)write(device, &t, sizeof(t));}-#define SAMPLE_MS 8 /* a sample every so many milliseconds: what the touch screen gives */+/*+ * A sample every so many milliseconds. Once 8, what the touch screen was polled at (touch_poll_us=8000): 125 samples a+ * second for a display of 120 refreshes, each arriving 4.6 to 11.9 ms after the last, and a page dragged by them was+ * drawn only when one had come -- a quarter of the frames of a swipe came a refresh late, 100 frames a second where+ * the window server had time for 120 (3 November, touch_lat=1). 4 (touch_poll_us=4000 for the touch screen itself):+ * a sample in every refresh.+ */+#define SAMPLE_MS 4static mach_timebase_info_data_t timebase;diff --git a/xnu/files/iokit/Kernel/MiniIOMobileFramebuffer.cpp b/xnu/files/iokit/Kernel/MiniIOMobileFramebuffer.cppindex 3768140..dd8e942 100644--- a/xnu/files/iokit/Kernel/MiniIOMobileFramebuffer.cpp+++ b/xnu/files/iokit/Kernel/MiniIOMobileFramebuffer.cpp@@ -1110,7 +1110,19 @@ IOMobileFramebuffer::presenter(void)clock_interval_to_deadline(SETTLE_MS, NSEC_PER_MSEC, &deadline);if (IOLockSleepDeadline(fSwapLock, fSwap, deadline, THREAD_UNINT) == THREAD_TIMED_OUT && fSwapsWaiting == 0) {IOLockUnlock(fSwapLock);+ /* the frame's surfaces again, by their IDs: they were let go of as soon as the frame was on the panel */+ for (unsigned layer = 0; layer < SWAP_LAYERS; layer++) {+ uint32_t sid = swapWord(swap, SWAP_SURFACE(layer));++ surfaces[layer] = sid != 0 ? MiniIOSurfaceCopy(sid) : NULL;+ }settle(swap, surfaces);+ for (unsigned layer = 0; layer < SWAP_LAYERS; layer++) {+ if (surfaces[layer] != NULL) {+ MiniIOSurfaceDrop(surfaces[layer]);+ surfaces[layer] = NULL;+ }+ }unsettled = false;IOLockLock(fSwapLock);}@@ -1129,12 +1141,16 @@ IOMobileFramebuffer::presenter(void)IOLockUnlock(fSwapLock);show(swap, surfaces);unsettled = gFilter == 2;- if (!unsettled) {- for (unsigned layer = 0; layer < SWAP_LAYERS; layer++) {- if (surfaces[layer] != NULL) {- MiniIOSurfaceDrop(surfaces[layer]);- surfaces[layer] = NULL;- }+ /*+ * The surfaces are let go of as soon as their pixels are in the panel's memory, not when the next frame comes:+ * held, they were in use to CoreAnimation, which had no surface to draw the next frame into while that one+ * waited for its beat (up to a refresh) -- its swaps came 9.6 ms apart at the median, not 8.3 (3 November,+ * touch_lat=1). The filtered frame once they stop (fb_filter=2) takes them again by their IDs.+ */+ for (unsigned layer = 0; layer < SWAP_LAYERS; layer++) {+ if (surfaces[layer] != NULL) {+ MiniIOSurfaceDrop(surfaces[layer]);+ surfaces[layer] = NULL;}}MiniIOSurfaceClearKept(); /* (the next frame is some milliseconds away) */