Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions docs/performance.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,8 @@ __postFrameCallback((frameTimeNanos, performanceMillis) => { … });
frames on the very clock the time origin is captured on, so the mapping is
exact rather than an approximation resampled in JS.

Two implementations sit behind that one surface: the NDK's `AChoreographer`
(API 24+, resolved with `dlsym`) and `android.view.Choreographer` through
`com.tns.FrameCallbacks` for API 21–23, where the NDK API does not exist.
Scheduling is per calling thread, so a worker schedules against its own looper.
Both paths produce the same two arguments with the same exactness.
Frames come from the NDK's `AChoreographer`. Scheduling is per calling thread,
so a worker schedules against its own looper.

## Deviations from the specs

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
"gradle": "8.14.3",
"gradleAndroid": "8.12.1",
"ndk": "r29",
"ndkApiLevel": "21",
"minSdk": "21",
"ndkApiLevel": "24",
"minSdk": "24",
"compileSdk": "35",
"buildTools": "35.0.0",
"kotlin": "2.0.0"
Expand Down
131 changes: 49 additions & 82 deletions test-app/app/src/main/assets/app/tests/testPostFrameCallback.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,97 +235,64 @@ describe("requestAnimationFrame", function () {
});
});

// The two implementations behind __postFrameCallback (NDK AChoreographer,
// android.view.Choreographer for API < 24) must be indistinguishable from JS.
// A modern device always selects the NDK one, so the Java bridge is only
// reachable through __setFrameCallbackImpl, which debug runtimes expose for
// exactly this.
describe("frame callback timestamps", function () {
const defaultWaitTime = 300;
const impls = ["native", "java"];

afterEach(() => {
if (typeof global.__setFrameCallbackImpl === "function") {
global.__setFrameCallbackImpl("auto");
}
});

function withImpl(impl) {
if (typeof global.__setFrameCallbackImpl !== "function") {
return impl === "native";
}
return global.__setFrameCallbackImpl(impl) === impl;
}

impls.forEach((impl) => {
describe(impl + " implementation", function () {
it("passes the raw frame time and a performance-timeline timestamp", (done) => {
if (!withImpl(impl)) {
pending("this runtime cannot select the " + impl + " implementation");
return;
}

global.__postFrameCallback((frameTimeNanos, performanceMillis) => {
const now = performance.now();

expect(typeof frameTimeNanos).toBe("number");
expect(typeof performanceMillis).toBe("number");
it("passes the raw frame time and a performance-timeline timestamp", (done) => {
global.__postFrameCallback((frameTimeNanos, performanceMillis) => {
const now = performance.now();

// Uptime-scale nanoseconds, not epoch-scale milliseconds.
expect(frameTimeNanos).toBeGreaterThan(1e9);
expect(frameTimeNanos / 1e6).toBeLessThan(Date.now());
expect(typeof frameTimeNanos).toBe("number");
expect(typeof performanceMillis).toBe("number");

// The frame is stamped just before the callback runs, so its
// timeline position sits a frame or two behind the reading taken
// inside it, never ahead of it.
expect(performanceMillis).toBeGreaterThan(0);
expect(performanceMillis).not.toBeGreaterThan(now);
expect(now - performanceMillis).toBeLessThan(250);
// Uptime-scale nanoseconds, not epoch-scale milliseconds.
expect(frameTimeNanos).toBeGreaterThan(1e9);
expect(frameTimeNanos / 1e6).toBeLessThan(Date.now());

// Both arguments describe the same instant, so their difference is
// the timeline's monotonic origin. System.nanoTime() is on that same
// clock, so the pair (nanoTime, now) must yield the same origin --
// this is what would break if either argument moved off the base.
// The two clocks cannot be read at once, so nanoTime is bracketed
// and compared against the midpoint: the tolerance then only has to
// cover the sampling window, not whatever pause lands between them.
const beforeNow = performance.now();
const sampledNanos = java.lang.System.nanoTime();
const afterNow = performance.now();
const originFromFrame = frameTimeNanos / 1e6 - performanceMillis;
const originFromClock = sampledNanos / 1e6 - (beforeNow + afterNow) / 2;
expect(Math.abs(originFromFrame - originFromClock)).toBeLessThan(
5 + (afterNow - beforeNow)
);
done();
});
});
// The frame is stamped just before the callback runs, so its
// timeline position sits a frame or two behind the reading taken
// inside it, never ahead of it.
expect(performanceMillis).toBeGreaterThan(0);
expect(performanceMillis).not.toBeGreaterThan(now);
expect(now - performanceMillis).toBeLessThan(250);

it("advances across consecutive frames", (done) => {
if (!withImpl(impl)) {
pending("this runtime cannot select the " + impl + " implementation");
return;
}
// Both arguments describe the same instant, so their difference is
// the timeline's monotonic origin. System.nanoTime() is on that same
// clock, so the pair (nanoTime, now) must yield the same origin --
// this is what would break if either argument moved off the base.
// The two clocks cannot be read at once, so nanoTime is bracketed
// and compared against the midpoint: the tolerance then only has to
// cover the sampling window, not whatever pause lands between them.
const beforeNow = performance.now();
const sampledNanos = java.lang.System.nanoTime();
const afterNow = performance.now();
const originFromFrame = frameTimeNanos / 1e6 - performanceMillis;
const originFromClock = sampledNanos / 1e6 - (beforeNow + afterNow) / 2;
expect(Math.abs(originFromFrame - originFromClock)).toBeLessThan(
5 + (afterNow - beforeNow)
);
done();
});
});

const frames = [];
const callback = (frameTimeNanos, performanceMillis) => {
frames.push({ nanos: frameTimeNanos, millis: performanceMillis });
if (frames.length === 1) {
global.__postFrameCallback(callback);
}
};
it("advances across consecutive frames", (done) => {
const frames = [];
const callback = (frameTimeNanos, performanceMillis) => {
frames.push({ nanos: frameTimeNanos, millis: performanceMillis });
if (frames.length === 1) {
global.__postFrameCallback(callback);
}
};
global.__postFrameCallback(callback);

setTimeout(() => {
expect(frames.length).toBe(2);
expect(frames[1].nanos).not.toBeLessThan(frames[0].nanos);
expect(frames[1].millis).not.toBeLessThan(frames[0].millis);
// The origin the two arguments imply is a constant of the isolate.
const origin = (f) => f.nanos / 1e6 - f.millis;
expect(Math.abs(origin(frames[1]) - origin(frames[0]))).toBeLessThan(1);
done();
}, defaultWaitTime);
});
});
setTimeout(() => {
expect(frames.length).toBe(2);
expect(frames[1].nanos).not.toBeLessThan(frames[0].nanos);
expect(frames[1].millis).not.toBeLessThan(frames[0].millis);
// The origin the two arguments imply is a constant of the isolate.
const origin = (f) => f.nanos / 1e6 - f.millis;
expect(Math.abs(origin(frames[1]) - origin(frames[0]))).toBeLessThan(1);
done();
}, defaultWaitTime);
});
});
2 changes: 1 addition & 1 deletion test-app/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ android.useAndroidX=true
# Default versions used throughout the gradle configurations
NS_DEFAULT_BUILD_TOOLS_VERSION=35.0.0
NS_DEFAULT_COMPILE_SDK_VERSION=35
NS_DEFAULT_MIN_SDK_VERSION=21
NS_DEFAULT_MIN_SDK_VERSION=24
NS_DEFAULT_ANDROID_BUILD_TOOLS_VERSION=8.13.2
# R8 8.x cannot read Kotlin 2.4 metadata (needs 9.1.29+), so the AGP-bundled R8 is overridden.
NS_DEFAULT_R8_VERSION=9.1.43
Expand Down
7 changes: 6 additions & 1 deletion test-app/runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -317,4 +317,9 @@ find_library(system-android android)

# Command info: https://cmake.org/cmake/help/v3.4/command/target_link_libraries.html
# Specifies libraries CMake should link to your target library.
target_link_libraries(NativeScript ${system-log} ${system-z} ${system-android})
target_link_libraries(NativeScript ${system-log} ${system-z} ${system-android})

if (OPTIMIZED_BUILD OR OPTIMIZED_WITH_INSPECTOR_BUILD)
# Android's packed relocation format, which the loader reads from API 23.
target_link_options(NativeScript PRIVATE "-Wl,--pack-dyn-relocs=android")
endif ()
Loading