253 lines
12 KiB
Ziran
253 lines
12 KiB
Ziran
// The software frame host driven through Android OS events and padded pixels.
|
|
Probe :: #import "android_frame_app";
|
|
#import "android_run"
|
|
#import "android_native"
|
|
#import "android_bundle_images"
|
|
#import "android_touch"
|
|
#import "android_jni"
|
|
#import "android_surface"
|
|
#import "frame_snapshot"
|
|
#import "geometry"
|
|
#import "input_protocol"
|
|
#import "session"
|
|
#import "tree_draw"
|
|
#import "std/c_string"
|
|
Event :: struct { kind: s32; action: s32; key: s32; x: float32; y: float32 }
|
|
watched: Session;
|
|
presents: s32;
|
|
presented_open: bool;
|
|
window_pixels: [64800]u32;
|
|
#program_export
|
|
ALooper_pollAll :: (timeout: s32, file: *s32, events: *s32, data: **void) -> s32 {
|
|
unused timeout; unused file; unused events; unused data; return -1
|
|
}
|
|
#program_export
|
|
ANativeWindow_getWidth :: (window: *void) -> s32 { unused window; return 320 }
|
|
#program_export
|
|
ANativeWindow_getHeight :: (window: *void) -> s32 { unused window; return 200 }
|
|
#program_export
|
|
AConfiguration_getUiModeNight :: (configuration: *void) -> s32 { unused configuration; return 1 }
|
|
#program_export
|
|
ANativeWindow_setBuffersGeometry :: (window: *void, width: s32, height: s32, format: s32) -> s32 {
|
|
unused window; unused width; unused height; unused format; return 0
|
|
}
|
|
#program_export
|
|
ANativeWindow_lock :: (window: *void, output: *NativeWindowBuffer, bounds: *void) -> s32 {
|
|
unused window; unused bounds
|
|
output.* = NativeWindowBuffer.{.width = 320, .height = 200, .stride = 324,
|
|
.format = 1, .bits = cast(*u8)(*window_pixels[0])}
|
|
return 0
|
|
}
|
|
#program_export
|
|
ANativeWindow_unlockAndPost :: (window: *void) -> s32 {
|
|
unused window; presents += 1
|
|
if CaptureFrameInput(watched).available { presented_open = true }
|
|
return 0
|
|
}
|
|
#program_export
|
|
ANativeActivity_finish :: (activity: *void) { unused activity }
|
|
#program_export
|
|
AAssetManager_open :: (manager: *void, path: *u8, mode: s32) -> *void {
|
|
unused manager; unused path; unused mode; return null
|
|
}
|
|
#program_export
|
|
AAsset_getLength :: (asset: *void) -> s32 { unused asset; return 0 }
|
|
#program_export
|
|
AAsset_read :: (asset: *void, output: *void, count: s32) -> s32 {
|
|
unused asset; unused output; unused count; return 0
|
|
}
|
|
#program_export
|
|
AAsset_close :: (asset: *void) { unused asset }
|
|
#program_export
|
|
AInputEvent_getType :: (event: *Event) -> s32 { return event.kind }
|
|
#program_export
|
|
AInputEvent_getSource :: (event: *Event) -> s32 { unused event; return 0x1002 }
|
|
#program_export
|
|
AKeyEvent_getAction :: (event: *Event) -> s32 { return event.action }
|
|
#program_export
|
|
AKeyEvent_getMetaState :: (event: *Event) -> s32 { unused event; return 0 }
|
|
#program_export
|
|
AInputEvent_getDeviceId :: (event: *Event) -> s32 { unused event; return -1 }
|
|
#program_export
|
|
AKeyEvent_getKeyCode :: (event: *Event) -> s32 { return event.key }
|
|
#program_export
|
|
AKeyEvent_getRepeatCount :: (event: *Event) -> s32 { unused event; return 0 }
|
|
#program_export
|
|
AMotionEvent_getAction :: (event: *Event) -> s32 { return event.action }
|
|
#program_export
|
|
AMotionEvent_getButtonState :: (event: *Event) -> s32 { unused event; return 0 }
|
|
#program_export
|
|
AMotionEvent_getX :: (event: *Event, pointer: usize) -> float32 { unused pointer; return event.x }
|
|
#program_export
|
|
AMotionEvent_getY :: (event: *Event, pointer: usize) -> float32 { unused pointer; return event.y }
|
|
#program_export
|
|
__android_log_write :: (priority: s32, tag: *u8, text: *u8) -> s32 {
|
|
unused priority; unused tag; unused text; return 1
|
|
}
|
|
Send :: (kind: s32, action: s32, key: s32 = 0, x: float32 = 0.0, y: float32 = 0.0) -> bool {
|
|
event := Event.{kind, action, key, x, y}
|
|
return AndroidTouchInput(null, cast(*void)(*event)) == 1
|
|
}
|
|
Draw :: (session: Session, reuse: bool = false, paint_unchanged: *bool = null) -> bool {
|
|
viewport := AndroidViewport.{.width = 320, .height = 200, .ready = 1}
|
|
return DrawAndroidFrame(session, viewport, Rectangle.{0.0, 0.0, 320.0, 200.0}, 1.0, reuse, paint_unchanged)
|
|
}
|
|
FramePacingCase :: struct {
|
|
finished_ms: s64
|
|
paint_unchanged: bool
|
|
events: bool
|
|
expected_quiet: s32
|
|
expected_deadline: s64
|
|
}
|
|
|
|
FramePacing :: () -> bool {
|
|
// An animated retained frame resets idle pacing, including an overrun.
|
|
// A looper event restores the active budget even if its paint is stable.
|
|
cases: [8]FramePacingCase = .[
|
|
.{105, true, false, 1, 116},
|
|
.{105, true, false, 2, 205},
|
|
.{105, true, false, 2, 205},
|
|
.{140, false, false, 0, 116},
|
|
.{105, true, false, 1, 116},
|
|
.{105, true, false, 2, 205},
|
|
.{105, true, true, 0, 116},
|
|
.{105, false, false, 0, 116},
|
|
]
|
|
quiet_frames: s32
|
|
for item: cases {
|
|
deadline := AndroidNextFrame(*quiet_frames, 100, item.finished_ms, item.paint_unchanged, item.events)
|
|
if quiet_frames != item.expected_quiet || deadline != item.expected_deadline { return false }
|
|
}
|
|
quiet_frames = 2
|
|
for frame: 0..3 {
|
|
started := 200 + frame * 16
|
|
if AndroidNextFrame(*quiet_frames, started, started + 5, true, false, true) != started + 16 ||
|
|
quiet_frames != 0 { return false }
|
|
}
|
|
if AndroidNextFrame(*quiet_frames, 264, 269, true, true) != 280 { return false }
|
|
if AndroidNextFrame(*quiet_frames, 280, 285, true, false) != 296 { return false }
|
|
if AndroidNextFrame(*quiet_frames, 296, 301, true, false) != 401 { return false }
|
|
return true
|
|
}
|
|
|
|
#program_export
|
|
main :: () -> s32 {
|
|
if !FramePacing() { return Failure(28) }
|
|
app: NativeAndroidApp
|
|
app.window = cast(*void)(*app)
|
|
AndroidNativeStart(*app)
|
|
defer { AndroidNativeClose() }
|
|
AndroidNativeOnCommand(*app, AndroidNativeCommandInitWindow)
|
|
// The frame loop calls the app's frames through kryon/app's Android
|
|
// branch; this test drives it through the same callback slot.
|
|
AndroidUseDraw(Probe.Frame)
|
|
session := SessionOpen()
|
|
defer { unused CloseSession(session) }
|
|
Probe.foreign = SessionOpen()
|
|
defer { unused CloseSession(Probe.foreign) }
|
|
watched = session
|
|
// First frame works before the first event.
|
|
if !Draw(session) || Probe.frames != 1 || !Probe.seen.focused { return Failure(1) }
|
|
if !Send(1, 0, 29) || !Draw(session) { return Failure(2) }
|
|
first := Probe.seen
|
|
if !first.available || first.keys[65] != cast(u8)3 || first.delta_ms < 0 { return Failure(3) }
|
|
if CaptureFrameInput(session).available || presents == 0 || presented_open { return Failure(4) }
|
|
if !Draw(session) || Probe.seen.keys[65] != cast(u8)FrameKeyHeld ||
|
|
Probe.seen.sequence <= first.sequence { return Failure(5) }
|
|
if !Send(1, 1, 29) || !Draw(session) { return Failure(6) }
|
|
if Probe.seen.keys[65] != cast(u8)FrameKeyReleased || CaptureFrameInput(session).available { return Failure(7) }
|
|
if !Send(1, 0, 29) || !Draw(session) { return Failure(8) }
|
|
AndroidNativeOnCommand(*app, AndroidNativeCommandLostFocus)
|
|
if !Draw(session) || Probe.seen.focused || Probe.seen.keys[65] != cast(u8)FrameKeyReleased { return Failure(9) }
|
|
AndroidNativeOnCommand(*app, AndroidNativeCommandGainedFocus)
|
|
// A whole tap between frames produces one press, then one release.
|
|
if !Send(2, 0, 0, 25.0, 30.0) || !Send(2, 1, 0, 35.0, 40.0) || !Draw(session) { return Failure(10) }
|
|
if !Probe.seen.pointer.pressed || !Probe.seen.pointer.down || Probe.seen.pointer.x != 25.0 ||
|
|
!Probe.pointer_matches { return Failure(11) }
|
|
if !Draw(session) || !Probe.seen.pointer.released || Probe.seen.pointer.down ||
|
|
Probe.seen.pointer.x != 35.0 || !Probe.pointer_matches { return Failure(12) }
|
|
if Probe.foreign_seen || CaptureFrameInput(session).available || presented_open { return Failure(13) }
|
|
// Keeping the surface reusable still presents new paint from the same app.
|
|
paint: [4]u8 = .[40, 40, 180, 180]
|
|
for tint, index: paint {
|
|
Probe.paint_red = tint
|
|
before := presents
|
|
unchanged: bool
|
|
if !Draw(session, index != 0, *unchanged) || unchanged != (index % 2 != 0) ||
|
|
(presents == before) != unchanged { return Failure(29) }
|
|
}
|
|
// Surface insets are added to both replayed tap positions, before the
|
|
// host's camera mapping. The Android host's own zoom scales the offset.
|
|
AndroidTouchSetZoom(2.0)
|
|
AndroidTouchSetSurfaceOffset(18.0, 18.0)
|
|
if !Send(2, 0, 0, 10.0, 14.0) || !Send(2, 1, 0, 20.0, 24.0) {
|
|
return Failure(23)
|
|
}
|
|
offset_press := AndroidPointerFrame()
|
|
if !offset_press.pressed || offset_press.x != 14.0 || offset_press.y != 16.0 {
|
|
return Failure(24)
|
|
}
|
|
AndroidTouchFrameEnd()
|
|
offset_release := AndroidPointerFrame()
|
|
if !offset_release.released || offset_release.x != 19.0 || offset_release.y != 21.0 {
|
|
return Failure(25)
|
|
}
|
|
AndroidTouchFrameEnd()
|
|
AndroidTouchSetZoom(1.0)
|
|
AndroidTouchSetSurfaceOffset(0.0, 0.0)
|
|
// Pixel stride includes NDK row padding; ARGB becomes little-endian RGBA.
|
|
for index: 0..64799 { window_pixels[index] = 0x12345678 }
|
|
{
|
|
pixels := AndroidNativePixels()
|
|
for index: 0..63999 { pixels[index] = 0xffe61e28 }
|
|
}
|
|
if !AndroidNativeLock(1, 1, 3, 3) || !AndroidNativePresentDamage(1, 1, 3, 3) || !AndroidNativeUnlock() { return Failure(14) }
|
|
if window_pixels[325] != 0xff281ee6 || window_pixels[650] != 0xff281ee6 ||
|
|
window_pixels[324] != 0x12345678 || window_pixels[323] != 0x12345678 { return Failure(15) }
|
|
// Retained frame damage is already in device pixels at every density.
|
|
// Copy the changed rectangle, leaving its neighbors and row padding alone.
|
|
zooms: [3]float32 = .[0.75, 1.5, 2.0]
|
|
for zoom: zooms {
|
|
for index: 0..64799 { window_pixels[index] = 0x12345678 }
|
|
if !AndroidPresentPixmap(Rectangle.{5.0, 7.0, 2.0, 2.0}, 320, 200, zoom) { return Failure(26) }
|
|
if window_pixels[7 * 324 + 5] != 0xff281ee6 || window_pixels[8 * 324 + 6] != 0xff281ee6 ||
|
|
window_pixels[7 * 324 + 4] != 0x12345678 || window_pixels[8 * 324 + 7] != 0x12345678 ||
|
|
window_pixels[6 * 324 + 5] != 0x12345678 || window_pixels[9 * 324 + 5] != 0x12345678 ||
|
|
window_pixels[7 * 324 + 320] != 0x12345678 { return Failure(27) }
|
|
}
|
|
AndroidNativeOnCommand(*app, AndroidNativeCommandPause)
|
|
if !AndroidNativePaused() { return Failure(16) }
|
|
AndroidNativeOnCommand(*app, AndroidNativeCommandResume)
|
|
if AndroidNativePaused() { return Failure(17) }
|
|
AndroidNativeOnCommand(*app, AndroidNativeCommandTermWindow)
|
|
if AndroidNativeWindowReady() { return Failure(18) }
|
|
AndroidNativeOnCommand(*app, AndroidNativeCommandDestroy)
|
|
if !AndroidNativeDestroyRequested() { return Failure(19) }
|
|
// EXIF rotations must use the source dimensions on non-square images.
|
|
expected: [18]u32 = .[4, 1, 5, 2, 6, 3, 6, 3, 5, 2, 4, 1, 3, 6, 2, 5, 1, 4]
|
|
for orientation: 6..8 {
|
|
source := cast(*u32)TestAllocate(6, 4)
|
|
if source == null { return Failure(20) }
|
|
for i: 0..5 { source[i] = cast(u32)(i + 1) }
|
|
rotated := AndroidImageOrient(source, 3, 2, cast(s32)orientation)
|
|
if rotated == null { return Failure(21) }
|
|
for i: 0..5 { if rotated[i] != expected[(orientation - 6) * 6 + i] { return Failure(22) } }
|
|
TestRelease(cast(*void)rotated)
|
|
}
|
|
return 0
|
|
}
|
|
libc :: #system_library "c";
|
|
TestAllocate :: (count: usize, size: usize) -> *void #foreign libc "calloc";
|
|
TestRelease :: (memory: *void) #foreign libc "free";
|
|
PrintFailure :: (format: *u8, code: s32) -> s32 #foreign libc "printf";
|
|
Failure :: (code: s32) -> s32 {
|
|
format: [40]u8
|
|
unused CopyCString("android frame input failure %d\n", format[:])
|
|
unused PrintFailure(*format[0], code)
|
|
return code
|
|
}
|
|
// Bionic's nonblocking join probe has no glibc symbol. Ask the portable
|
|
// thread adapter to take its ordinary pthread_join path in this OS fixture.
|
|
#program_export
|
|
pthread_gettid_np :: (thread: *void) -> s32 { unused thread; return -1 }
|