Display-list based menu slide animation

This commit is contained in:
James Babcock committed 2026-09-23 01:13:03 -07:00
1 parent 92e8def0f0
commit c8d835176f
28 files changed
+1546 -263

No files matched your search

+1 -1
View File
@@ -26,7 +26,7 @@ export type FirmwareInfo = {
};
/** The Faceclaw firmware revision this build of the app needs. */
export const REQUIRED_FACECLAW_FIRMWARE_VERSION = 26;
export const REQUIRED_FACECLAW_FIRMWARE_VERSION = 27;
const FACECLAW_PREFIX = "Faceclaw/";
const LEGACY_PREFIX = "EVENCFW";
+34 -34
View File
@@ -19,12 +19,12 @@ export type FirmwarePatchSet = {
export const CFW_PATCH_SET: FirmwarePatchSet = {
"base": "g2_2.3.0.24.bin",
"baseSha256": "187ccf2bcc5c17a212106e8a376745511e8289c4232b634a7ea94b9bf25a0979",
"outputSha256": "dc9808a442dd475f3cb2c2945a5afa1408458222842cd9fef6288e435920c9b0",
"outputSha256": "2d9f4200b3980156d65732e1d5fe4ba673768baf06683427710ff9a818cfb7ae",
"patches": [
{
"offset": 1045797,
"old": "38b584b0",
"new": "5cf339be",
"new": "5cf31db9",
"desc": "ring receiver entry: timestamped unfiltered reports under framebuffer lease"
},
{
@@ -36,49 +36,49 @@ export const CFW_PATCH_SET: FirmwarePatchSet = {
{
"offset": 1437923,
"old": "fef788ff",
"new": "fdf248fb",
"new": "fcf22cfe",
"desc": "cfw_ancs_event"
},
{
"offset": 1437951,
"old": "fef7e8fb",
"new": "fdf282f8",
"new": "fcf266fb",
"desc": "cfw_ancs_open"
},
{
"offset": 1437969,
"old": "fef7e7fb",
"new": "fdf299f8",
"new": "fcf27dfb",
"desc": "cfw_ancs_close"
},
{
"offset": 1388387,
"old": "39694d00",
"new": "795e7d00",
"new": "41547d00",
"desc": "connection-bound ANCS EUS write callback"
},
{
"offset": 971497,
"old": "fff717f8",
"new": "6ef369ff",
"new": "6ef34dfa",
"desc": "bl faceclaw_input_bridge_received (ring relay and ordered private transport before worker pool)"
},
{
"offset": 971845,
"old": "fef769ff",
"new": "6ef3bbfe",
"new": "6ef39ff9",
"desc": "bl faceclaw_input_bridge_received (ring relay and ordered private transport before worker pool)"
},
{
"offset": 979579,
"old": "fdf74ef8",
"new": "6cf3a0ff",
"new": "6cf384fa",
"desc": "bl faceclaw_input_bridge_received (ring relay and ordered private transport before worker pool)"
},
{
"offset": 1428895,
"old": "f8f76cfd",
"new": "fcf294f8",
"new": "fbf2c4fd",
"desc": "bl cfw_receive_packet (private SID-f0 probe before TPL reassembly)"
},
{
@@ -108,145 +108,145 @@ export const CFW_PATCH_SET: FirmwarePatchSet = {
{
"offset": 1248487,
"old": "d4f712fb",
"new": "2af3c0f8",
"new": "29f354fc",
"desc": "bl settings_send_wrapper (append caps field 100)"
},
{
"offset": 1246183,
"old": "f5f7e2f8",
"new": "2af362fa",
"new": "29f330fe",
"desc": "bl settings_decode_wrapper (Faceclaw lease field 101)"
},
{
"offset": 975219,
"old": "02f083fc",
"new": "6cf3c4fa",
"new": "6bf39cfe",
"desc": "bl faceclaw_display_start @ 0x467d68 (fail-open wake takeover)"
},
{
"offset": 975411,
"old": "02f023fc",
"new": "6cf332fb",
"new": "6bf300ff",
"desc": "bl faceclaw_display_start_headup @ 0x467e28 (fail-open wake takeover)"
},
{
"offset": 975103,
"old": "07f085fb",
"new": "6df372ff",
"new": "6df356fa",
"desc": "bl headup_gate (head-up sensor event -> EvenHub sys event 12 under lease)"
},
{
"offset": 830549,
"old": "19f03efb",
"new": "91f321f9",
"new": "90f305fc",
"desc": "bl gesture_ring_press_mode (observe ring touch-down, preserve stock dispatch)"
},
{
"offset": 830795,
"old": "1ff071ff",
"new": "91f3daf8",
"new": "90f3befb",
"desc": "bl gesture_press (owned event 9 before stock menu gate)"
},
{
"offset": 830887,
"old": "1ff043ff",
"new": "91f3d4f8",
"new": "90f3b8fb",
"desc": "bl gesture_short_long (Faceclaw lease gates event 11 vs stock Menu path)"
},
{
"offset": 831763,
"old": "1ff08dfd",
"new": "90f30aff",
"new": "90f3eef9",
"desc": "bl gesture_release (owned event 10 before stock menu gate)"
},
{
"offset": 1570209,
"old": "7fb50600",
"new": "dbf251bc",
"new": "daf2e5bf",
"desc": "even_ai_display_ctrl entry -> conditional Faceclaw lease trampoline"
},
{
"offset": 1051545,
"old": "f6f787f9",
"new": "55f3fdfb",
"new": "54f3cdff",
"desc": "bl display_copy_hook @ 0x47a78e (640x480 direct framebuffer)"
},
{
"offset": 1051861,
"old": "f6f7e9f8",
"new": "55f35ffb",
"new": "54f32fff",
"desc": "bl display_copy_hook @ 0x47a8ca (640x480 direct framebuffer)"
},
{
"offset": 1262237,
"old": "d9f7d0fd",
"new": "26f335f9",
"new": "25f365fd",
"desc": "bl faceclaw_send_wear_event @ 0x4ade92 (outside onboarding)"
},
{
"offset": 1262337,
"old": "d9f79efd",
"new": "26f303f9",
"new": "25f333fd",
"desc": "bl faceclaw_send_wear_event @ 0x4adef6 (outside onboarding)"
},
{
"offset": 1309405,
"old": "67f02dff",
"new": "1af31bfa",
"new": "19f33dfe",
"desc": "bl compass_decode_capture (sample-matched GAF diagnostics)"
},
{
"offset": 1307865,
"old": "fff7c9fb",
"new": "1af399fd",
"new": "1af3bbf9",
"desc": "bl compass_report_event (stock UI + heading with diagnostics over BLE)"
},
{
"offset": 1300937,
"old": "00210170",
"new": "1cf37ffa",
"new": "1bf3a1fe",
"desc": "bl compass_preserve_accuracy (retain magnetic calibration under Faceclaw framebuffer lease)"
},
{
"offset": 4537963,
"old": "",
"new": "b0b581ea00039b0701d0034637e0c2b110f0030315d000f1010ca2f1010e0023cc5c9e45c45403f1010404d0634413f003032346f4d121440319121b102a14d302e00346102a10d3d1f800c0d1f804e0cc688d68103adc609d60c3f804e043f810cb0f2a01f11001eed8042a07d300bf51f8045b043a032a43f8045bf8d8002a08bfb0bd0139013b11f8015f013a03f8015ff9d1b0bd00bf884218bf002a00d1704710b50b1a38bf431a934207d288420ad281ea00039b0726d0034669e0fff79fffbde81040704781ea000300eb020c9b0701eb020e42d15fea8c732bd0a0f1010c00bf8b1813f801ec8418531e04f801ec04d0624412f003021a46f2d101eb030e00eb030c17e0830736d0a2f1010c00f1010e002300bfcc5c9c45c45403f1010404d0734413f003032346f4d1121b2144031922e01346042b0ed3aef10402acf1040152f80449043b032b41f80449f8d802f1040e01f1040c1a4653b1aef10101acf1010300bf11f80149013a03f80149f9d1bde8104070470346042a06d351f8044b043a032a43f8044bf8d8002af0d00139013b00bf11f8014f013a03f8014ff9d1e6e700bf2de9f04fc7b04ff690454ef66c6b4ff29c591446cff6ff75ccf2ff0bc2f229094ff6ff78002701260f93cde9100106e048f2a351c0f24501bff32f8f8847d9f800a0bff35f8fd9f800000321cff680710140b1f1005fbff35f8f03d10168594500f0ac804ff45c7007f0b6f8002800f0eb8129464218373182f8707382f8717382f8727382f8737382f8747382f8757382f8767382f8777382f8787382f8797382f87a7382f87b7382f87c7382f87d7382f87e7382f87f7382f8807382f8817382f8827382f8837382f8847382f8857382f8867382f8877382f8887382f8897382f88a7382f88b7382f88c7382f88d7382f88e7382f88f7382f8907382f8917382f8927382f8937382f8947382f8957382f8967382f8977382f8987382f8997382f89a7382f89b7382f89c7382f89d7382f89e7382f89f7382f8a07382f8a17382f8a27382f8a37382f8a47382f8a57382f8a6738ed1c0f800b08672a0f81c80a0f81e80a0f82080a0f82280a0f82480a0f82680a0f82880a0f82a80a0f82c80a0f82e80a0f83080a0f83280a0f83480a0f83680a0f83880a0f83a8059e8001f51457ff445afbff35f8f00bf49e8000121b159e8001f5145f8d039e71199012641ea04410024c0f8701206e048f2a351c0f24501bff32f8f8847d9f80070bff35f8fd9f800000321cff680710140b1f1005fbff35f8f03d10168594500f0ac804ff45c7006f0e8ff002800f01d8129464218373182f8704382f8714382f8724382f8734382f8744382f8754382f8764382f8774382f8784382f8794382f87a4382f87b4382f87c4382f87d4382f87e4382f87f4382f8804382f8814382f8824382f8834382f8844382f8854382f8864382f8874382f8884382f8894382f88a4382f88b4382f88c4382f88d4382f88e4382f88f4382f8904382f8914382f8924382f8934382f8944382f8954382f8964382f8974382f8984382f8994382f89a4382f89b4382f89c4382f89d4382f89e4382f89f4382f8a04382f8a14382f8a24382f8a34382f8a44382f8a54382f8a6438ed1c0f800b08672a0f81c80a0f81e80a0f82080a0f82280a0f82480a0f82680a0f82880a0f82a80a0f82c80a0f82e80a0f83080a0f83280a0f83480a0f83680a0f83880a0f83a8059e8001fb9427ff445afbff35f8f00bf49e8000121b159e8001fb942f8d039e7d0f874a242f6f766c0f24406baf1000fbff35f8f4ad006f19a0250464ff0ff314ff0ff379047002840f08f811198002861d010980778a7f1170002285bd811980c2840f07881109e7078b178084300f07281f0780138022800f26d81b279f379747a42ea0322337a307942ea0342717942ea0463b27af67a192f42ea062200f07f83172f40f09583002a40f05481fe2800f0c987812800f0bb87002840f0d08751ea030040f0478101f060b904460020b047e8b104f51d7151e800afbaf1000f0cd1bff35f8f00bf41e80002002a00f0e78051e800afbaf1000ff5d00021bff32f8f0029bff35f8f00f0d680824692e74ff0ff3729e147f6df40c0f24f008047002701261397adf8507006e048f2a351c0f24501bff32f8f8847d9f80040bff35f8fd9f800000321cff680710140b1f1005fbff35f8f03d10168594500f0b3804ff45c7006f09cfe002800f0ab8029464218373182f8707382f8717382f8727382f8737382f8747382f8757382f8767382f8777382f8787382f8797382f87a7382f87b7382f87c7382f87d7382f87e7382f87f7382f8807382f8817382f8827382f8837382f8847382f8857382f8867382f8877382f8887382f8897382f88a7382f88b7382f88c7382f88d7382f88e7382f88f7382f8907382f8917382f8927382f8937382f8947382f8957382f8967382f8977382f8987382f8997382f89a7382f89b7382f89c7382f89d7382f89e7382f89f7382f8a07382f8a17382f8a27382f8a37382f8a47382f8a57382f8a6738ed1c0f800b08672a0f81c80a0f81e80a0f82080a0f82280a0f82480a0f82680a0f82880a0f82a80a0f82c80a0f82e80a0f83080a0f83280a0f83480a0f83680a0f83880a0f83a8059e8001fa1427ff445afbff35f8f00bf49e8000159b159e8001fa142f8d039e706f5a9718847bae601211ee70020109999b1119a8ab1097801f07f02b2f10b0418bf0124b2f11c0618bf01268a2924dc0b2926d01c2924d03fe00f9012a804f0e3f94ff0ff3701269df85100002812a818bf4ff0ff3704f001fb054646b99df8500028b94af21f30c0f247004c3080470f98002818bfc56013e09c2901d08b291bd160b14af21f31c0f247010746884797f8641119b997f80413384669b14ff0ff3742f6f760c0f2440000f5807150468847384647b0bde8f08f0f9012a8264004f0a2f91098007800f07f040f2c2ddd142c40f3a981152c00f01183162c00f01c831d2c40f0da83119801380228c0f04d8510994878897840ea0124b4f5007f00f2448504eb4400032101eb40001199884240f03b8505f031fe002800f036850e9600260127119400f053bd092c00f3d180052c00f00c82072c40f0af83002706e048f2a351c0f24501bff32f8f8847d9f80040bff35f8fd9f800000321cff680710140b1f1005fbff35f8f03d10168594500f083864ff45c7006f035fd002800f07b86294600bf4218373182f8707382f8717382f8727382f8737382f8747382f8757382f8767382f8777382f8787382f8797382f87a7382f87b7382f87c7382f87d7382f87e7382f87f7382f8807382f8817382f8827382f8837382f8847382f8857382f8867382f8877382f8887382f8897382f88a7382f88b7382f88c7382f88d7382f88e7382f88f7382f8907382f8917382f8927382f8937382f8947382f895738Line truncated
"new": "b0b581ea00039b0701d0034637e0c2b110f0030315d000f1010ca2f1010e0023cc5c9e45c45403f1010404d0634413f003032346f4d121440319121b102a14d302e00346102a10d3d1f800c0d1f804e0cc688d68103adc609d60c3f804e043f810cb0f2a01f11001eed8042a07d300bf51f8045b043a032a43f8045bf8d8002a08bfb0bd0139013b11f8015f013a03f8015ff9d1b0bd00bf884218bf002a00d1704710b50b1a38bf431a934207d288420ad281ea00039b0726d0034669e0fff79fffbde81040704781ea000300eb020c9b0701eb020e42d15fea8c732bd0a0f1010c00bf8b1813f801ec8418531e04f801ec04d0624412f003021a46f2d101eb030e00eb030c17e0830736d0a2f1010c00f1010e002300bfcc5c9c45c45403f1010404d0734413f003032346f4d1121b2144031922e01346042b0ed3aef10402acf1040152f80449043b032b41f80449f8d802f1040e01f1040c1a4653b1aef10101acf1010300bf11f80149013a03f80149f9d1bde8104070470346042a06d351f8044b043a032a43f8044bf8d8002af0d00139013b00bf11f8014f013a03f8014ff9d1e6e700bf2de9f04fc5b04ff64c484ef66c6b4ff29c591446cff6ff78ccf2ff0bc2f229094ff6ff75002701260d93cde90e0106e048f2a351c0f24501bff32f8f8847d9f800a0bff35f8fd9f800000321cff680710140b1f1005fbff35f8f02d10168594544d04ff46d7006f04bfc002800f01a81414600bf42180c3182f8b47382f8b57382f8b67382f8b77382f8b87382f8b97382f8ba7382f8bb7382f8bc7382f8bd7382f8be7382f8bf73e4d1c0f800b086728583c583058445848584c584058545858585c585058645868586c5860587458759e8001f5145abd1bff35f8f49e8000121b159e8001f5145f8d0a1e70f99012641ea04410024c0f8701206e048f2a351c0f24501bff32f8f8847d9f80070bff35f8fd9f800000321cff680710140b1f1005fbff35f8f02d10168594544d04ff46d7006f0e5fb002800f0b480414600bf42180c3182f8b44382f8b54382f8b64382f8b74382f8b84382f8b94382f8ba4382f8bb4382f8bc4382f8bd4382f8be4382f8bf43e4d1c0f800b086728583c583058445848584c584058545858585c585058645868586c5860587458759e8001fb942abd1bff35f8f49e8000121b159e8001fb942f8d0a1e7d0f874a242f6f766c0f24406baf1000fbff35f8f4ad006f19a0250464ff0ff314ff0ff379047002840f023810f9800285fd00e980778a7f11700022859d80f980c2840f00c810e9e7078b178084300f00681f0780138022800f20181b279f379747a42ea0322337a307942ea0342717942ea0463b27af67a192f42ea062200f06f83172f40f08483002a40f0e880fe2800f07485812800f06685002840f07b8551ea030040f0db8000f0fabe04460020b047d8b104f51d7151e800afbaf1000f0bd1bff35f8f00bf41e80002002a7bd051e800afbaf1000ff6d00021bff32f8f0029bff35f8f6bd0824694e74ff0ff37bfe047f6df40c0f24f008047002701261197adf8487006e048f2a351c0f24501bff32f8f8847d9f80040bff35f8fd9f800000321cff680710140b1f1005fbff35f8f02d10168594549d04ff46d7006f003fb002842d0414642180c3182f8b47382f8b57382f8b67382f8b77382f8b87382f8b97382f8ba7382f8bb7382f8bc7382f8bd7382f8be7382f8bf73e4d1c0f800b086728583c583058445848584c584058545858585c585058645868586c5860587458759e8001fa142add1bff35f8f49e8000159b159e8001fa142f8d0a3e706f5a971884726e7012189e700200e9999b10f9a8ab1097801f07f02b2f10b0418bf0124b2f11c0618bf01268a2924dc0b2926d01c2924d03fe00d9010a804f0c7f84ff0ff3701269df84900002810a818bf4ff0ff3704f08bf9054646b99df8480028b94af21f30c0f247004c3080470d98002818bfc56013e09c2901d08b291bd160b14af21f31c0f247010746884797f8641119b997f80413384669b14ff0ff3742f6f760c0f2440000f5807150468847384645b0bde8f08f0d9010a8264004f086f80e98007800f07f041d2c00f07e0018bf1a2811d1d9f800100322cff680720a40b2f1005fbff35f8f0ad10a685a4504bf002281f8812303e0a4f115010129e9d90f2c0adc092c00f3cc80052c00f07882072c40f0f282002769e0142c00f37781102c00f07582112c40f0e7820f98022840f00b840e990878112840f006844878002840f0028445f64d50c0f24600804701284ff000077ff475af40f20830c0f210003590ff208df8d00044f25220c0f20100339040f2c960c0f248003446cde93677804798b148f27e30c2f2070000880122c1b264298df8cf2008d80721b0f5807f38bf03218df8cf108df8d00040f2d260adf8d800052035ae8df8da00f01d33a90522fff78afc4ff22507c0f247070120092132460c23b8470746264631e748f2a351c0f24501bff32f8f8847d9f80040bff35f8fd9f800000321cff680710140b1f1005fbff35f8f03d10168594500f04e844ff46d7006f0b4f9002800f04684414642180c3182f8b47382f8b57382f8b67382f8b77382f8b87382f8b97382f8ba7382f8bb7382f8bc7382f8bd7382f8be7382f8bf73e4d10121c0f800b081728583c583058445848584c584058545858585c585058645868586c5860587458759e8001fa142aad1bff35f8f49e80001002900f00c8459e8001fa142f6d09ee70a2c00f0b9810b2c40f02682d9f800500320cff680702840b0f1005fbff35f8f41f059802868002758457ff4baaed5f870030c9642f68d46c0f244060028a5f8807318bfb0470021a5f8b013c5f8ac13c5f8681185f8661185f86411d5f86c01c5f8601185f86511c5f86c11c5f80013a5f86c1320b148f2a351c0f245018847d5f87802002818bf04f02df90024c5f87842d5f8680320b148f2a351c0f245018847c5f86843a5f86c43286ca5f8444040b1b047286c06f166018847002804bf002028644bf63940c0f251008047d9f800400320cff680702040b0f1005fbff35f8f2ad12068584527d194f877010027c4f8807188b149f25962c0f25902002044f2436184f8777190474ef6e378c0f25a08c04708f1bc008047d4f8840184f8707150b1b047d4f8840106f166018847002804bf0020c4f88401d9f800000321cff680710140b1f1005fbff35f8f40f00a860168594540f0068690f8a811002900f0fb8506f0c9fb00f0fdbd152c00f06b81162c40f070810f9801380228c0f093820e994ff0ff374878897840ea0120b0f5007f3ff605ae03210f9a01eb400191427ff4fead002700288df8d4708df8d5708df8d6708df8d7708df8d8708df8d9708df8da708df8db708df8dc708df8dd708df8de708df8df708df8e0708df8e1708df8e2708df8e3708df8e4708df8e5708df8e6708Line truncated
"desc": "append injected blobs to main-app payload"
},
{
"offset": 779123,
"old": "805a3900",
"new": "dc443a00",
"new": "1c483a00",
"desc": "main-app subheader payload size (ps)"
},
{
"offset": 152,
"old": "005b3900",
"new": "5c453a00",
"new": "9c483a00",
"desc": "main-app TOC entry size (ps + 128)"
},
{
"offset": 779243,
"old": "805a3904",
"new": "dc443a04",
"new": "1c483a04",
"desc": "main-app preamble length (low 24 bits)"
},
{
"offset": 779247,
"old": "effdbcea",
"new": "fe2d93a9",
"new": "b66f5542",
"desc": "[5] ota/s200_firmware_ota.bin preamble crc32"
},
{
"offset": 156,
"old": "c0c6322a",
"new": "13f7ac84",
"new": "d0697cd9",
"desc": "[5] ota/s200_firmware_ota.bin component crc32c (TOC)"
},
{
"offset": 779127,
"old": "c0c6322a",
"new": "13f7ac84",
"new": "d0697cd9",
"desc": "[5] ota/s200_firmware_ota.bin component crc32c (subheader)"
}
]
+5 -4
View File
@@ -1,3 +1,4 @@
import type { MenuHighlightAnimation } from "../ui/menu-highlight-motion";
import { Glyph } from "./bdffont";
import { wrapText } from "./textwrap";
@@ -73,7 +74,7 @@ export type PlacedGlyph = {
* from the moment it is placed.
*/
export type PlacedImage = {
presentation?: { mode?: "image" | "masked-image"; radius: number; background: number; border: number; depth: number; occlusions?: readonly { x: number; y: number; width: number; height: number }[] };
presentation?: { animation?: MenuHighlightAnimation; mode?: "image" | "masked-image"; radius: number; background: number; border: number; depth: number; occlusions?: readonly { x: number; y: number; width: number; height: number }[] };
kind: "image";
source: GrayImage;
x: number;
@@ -394,7 +395,7 @@ export class GrayImage {
hash = mixInt(hash, placed.source.width);
hash = mixInt(hash, placed.source.height);
hash = mixInt(hash, placed.source.sourceContentHash32());
if (placed.presentation) { const p = placed.presentation; for (const value of [p.radius, p.background, p.border, p.depth, p.mode === "image" ? 1 : p.mode === "masked-image" ? 2 : 0]) hash = mixInt(hash, value); }
if (placed.presentation) { const p = placed.presentation; for (const value of [p.radius, p.background, p.border, p.depth, p.mode === "image" ? 1 : p.mode === "masked-image" ? 2 : 0, p.animation?.token ?? 0, p.animation?.dx ?? 0, p.animation?.dy ?? 0]) hash = mixInt(hash, value); }
} else {
hash = mixInt(hash, 0xf17e);
hash = mixInt(hash, placed.font.atlasTag);
@@ -506,9 +507,9 @@ export class GrayImage {
}
/** Retain the selected row separately from the menu surface for glasses-side composition. */
drawMenuSelection(source: GrayImage, x: number, y: number, background: number, border: number, radius = 8, depth = 2): void {
drawMenuSelection(source: GrayImage, x: number, y: number, background: number, border: number, radius = 8, depth = 2, animation?: MenuHighlightAnimation): void {
this.drawList.push({ kind: "image", source: source.withDrawsBaked(), x, y,
presentation: { radius, background, border, depth } });
presentation: { radius, background, border, depth, animation } });
}
/** Replay an image at stereo depth. Masked images preserve intentional gray8 black (1). */
+30 -12
View File
@@ -1,20 +1,27 @@
import type { PlacedImage } from './image'
/** Bridge record: tag 3 (selection), 4 (image), 5 (gray8-masked image), signed xy, u16 wh/radius, gray fill/stroke, signed depth, restore-count, gray pixels, restore rectangles. */
/** Bridge record: tag 3 (selection), 4 (image), 5 (gray8-masked image), 6 (animated selection), signed xy, u16 wh/radius, gray fill/stroke, signed depth, restore-count, gray pixels, restore rectangles. */
export function encodePresentation(draw: PlacedImage): Uint8Array {
const p = draw.presentation!
const { width, height, pixels } = draw.source
if (width < 1 || height < 1 || width > 640 || height > 480 || 5 + Math.ceil(width / 2) * height > 65536) throw new Error('Presentation image exceeds the resource size limit')
if (!Number.isInteger(p.depth) || p.depth < -128 || p.depth > 127) throw new Error('Invalid menu depth')
if ((p.occlusions?.length ?? 0) > 2048) throw new Error('Too many menu occlusion rectangles')
const bytes = new Uint8Array(16 + width * height + (p.occlusions?.length ?? 0) * 8), view = new DataView(bytes.buffer)
bytes[0] = p.mode === "image" ? 4 : p.mode === "masked-image" ? 5 : 3
const animation = p.animation;
const header = animation ? 26 : 16;
const bytes = new Uint8Array(header + width * height + (p.occlusions?.length ?? 0) * 8), view = new DataView(bytes.buffer)
bytes[0] = animation ? 6 : p.mode === "image" ? 4 : p.mode === "masked-image" ? 5 : 3
view.setInt16(1, draw.x, true); view.setInt16(3, draw.y, true)
view.setUint16(5, width, true); view.setUint16(7, height, true); view.setUint16(9, p.radius, true)
bytes[11] = p.background; bytes[12] = p.border; view.setInt8(13, p.depth)
view.setUint16(14, p.occlusions?.length ?? 0, true)
bytes.set(pixels, 16)
let offset = 16 + pixels.length
if (animation) {
view.setInt16(16, animation.dx, true); view.setInt16(18, animation.dy, true);
view.setUint16(20, Math.max(0, Math.min(500, Date.now() - animation.startedAt)), true);
view.setUint32(22, animation.token, true);
}
bytes.set(pixels, header)
let offset = header + pixels.length
for (const rect of p.occlusions ?? []) {
view.setUint16(offset, rect.x, true); view.setUint16(offset + 2, rect.y, true)
view.setUint16(offset + 4, rect.width, true); view.setUint16(offset + 6, rect.height, true); offset += 8
@@ -22,13 +29,16 @@ export function encodePresentation(draw: PlacedImage): Uint8Array {
return bytes
}
export type Selection = { mode?: "image" | "masked-image"; x: number; y: number; width: number; height: number; radius: number; background: number; border: number; depth: number; pixels: Uint8Array; occlusions: { x: number; y: number; width: number; height: number }[] }
export type Selection = { animation?: { dx: number; dy: number; startedAt: number; token: number }; mode?: "image" | "masked-image"; x: number; y: number; width: number; height: number; radius: number; background: number; border: number; depth: number; pixels: Uint8Array; occlusions: { x: number; y: number; width: number; height: number }[] }
export function readPresentation(bytes: Uint8Array, offset: number): { selection: Selection; end: number } {
const v = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength), w = v.getUint16(offset + 5, true), h = v.getUint16(offset + 7, true)
const header = bytes[offset] === 6 ? 26 : 16
const selection: Selection = { mode: bytes[offset] === 4 ? "image" : bytes[offset] === 5 ? "masked-image" : undefined, x: v.getInt16(offset+1,true), y: v.getInt16(offset+3,true), width:w, height:h,
radius:v.getUint16(offset+9,true), background:bytes[offset+11]!, border:bytes[offset+12]!, depth:v.getInt8(offset+13),
pixels:bytes.slice(offset+16,offset+16+w*h), occlusions:[] }
let end=offset+16+w*h
pixels:bytes.slice(offset+header,offset+header+w*h), occlusions:[] }
if (header === 26) selection.animation = { dx:v.getInt16(offset+16,true), dy:v.getInt16(offset+18,true),
startedAt:Date.now()-v.getUint16(offset+20,true), token:v.getUint32(offset+22,true) }
let end=offset+header+w*h
for(let i=0;i<v.getUint16(offset+14,true);i++) { selection.occlusions.push({x:v.getUint16(end,true),y:v.getUint16(end+2,true),width:v.getUint16(end+4,true),height:v.getUint16(end+6,true)});end+=8 }
return {selection,end}
}
@@ -36,7 +46,7 @@ export function presentationRecords(buffer: ArrayBuffer | null): Selection[] {
if (!buffer) return []
const bytes=new Uint8Array(buffer), result:Selection[]=[]
for(let p=0;p<bytes.length;) {
if(bytes[p]===3 || bytes[p]===4 || bytes[p]===5) { const record=readPresentation(bytes,p);result.push(record.selection);p=record.end }
if(bytes[p]===3 || bytes[p]===4 || bytes[p]===5 || bytes[p]===6) { const record=readPresentation(bytes,p);result.push(record.selection);p=record.end }
else if(bytes[p]===0) p+=12
else if(bytes[p]===1) p+=9
else if(bytes[p]===2) p+=7+bytes[p+6]!*7
@@ -52,13 +62,21 @@ export function paintPresentation(output: Uint8Array, screen: Uint8Array, width:
const dx=Math.max(0,2*r-(2*Math.min(x,w-1-x)+1)),dy=Math.max(0,2*r-(2*Math.min(y,h-1-y)+1))
return dx*dx+dy*dy<=4*r*r
}
const t = row.animation ? Math.max(0, Math.min(1, (Date.now() - row.animation.startedAt) / 500)) : 1
const remaining = 1 - t * t * (3 - 2 * t)
const highlightX = Math.trunc(row.x + (row.animation?.dx ?? 0) * remaining)
const highlightY = Math.trunc(row.y + (row.animation?.dy ?? 0) * remaining)
if (!row.mode) for(let yy=0;yy<row.height;yy++) for(let xx=0;xx<row.width;xx++) {
const x=highlightX+xx+shift,y=highlightY+yy
if(x<0||y<0||x>=width||y>=height || !inside(xx,yy,row.width,row.height,row.radius)) continue
const i=y*width+x
output[i]=!inside(xx-1,yy-1,row.width-2,row.height-2,Math.max(0,row.radius-1))?q(row.border)*16:Math.max(output[i]!,q(row.background)*16)
}
for(let yy=0;yy<row.height;yy++) for(let xx=0;xx<row.width;xx++) {
const x=row.x+xx+shift,y=row.y+yy
if(x<0||y<0||x>=width||y>=height) continue
const i=y*width+x
if(!row.mode && inside(xx,yy,row.width,row.height,row.radius)) output[i]=!inside(xx-1,yy-1,row.width-2,row.height-2,Math.max(0,row.radius-1))?q(row.border)*16:Math.max(output[i]!,q(row.background)*16)
const value=q(row.pixels[yy*row.width+xx]!)
if(row.mode === "masked-image" ? row.pixels[yy*row.width+xx] !== 0 : value !== 0) output[i]=value*16
if(row.mode === "masked-image" ? row.pixels[yy*row.width+xx] !== 0 : value !== 0) output[y*width+x]=value*16
}
for(const r of row.occlusions) for(let yy=r.y;yy<r.y+r.height;yy++) for(let xx=r.x;xx<r.x+r.width;xx++) if(xx>=0&&yy>=0&&xx<width&&yy<height) output[yy*width+xx]=q(screen[yy*width+xx]!)*16
}
+3 -2
View File
@@ -10,7 +10,8 @@ type Surface = SurfaceConfiguration & { pixels: Uint8Array; visible: boolean; se
export class SurfaceCompositor {
private readonly surfaces = new Map<string, Surface>()
private shellScene: Uint8Array | null = null
setShellScene(bytes: Uint8Array): void { this.shellScene = new Uint8Array(bytes) }
private shellReceivedAt = 0
setShellScene(bytes: Uint8Array): void { this.shellScene = new Uint8Array(bytes); this.shellReceivedAt = Date.now() }
private dimBelow = 0
private dimFactor = 1
private blanked = false
@@ -92,7 +93,7 @@ export class SurfaceCompositor {
if(x+xx+shift>=0 && y+yy>=0 && x+xx+shift<this.width && y+yy<this.height) output[(y+yy)*this.width+x+xx+shift]=quantize(bytes[p+yy*w+xx])*16
}
p+=w*h
for(let j=0;j<selections;j++) { const record=readPresentation(bytes,p);p=record.end;paintPresentation(output,screen,this.width,this.height,record.selection) }
for(let j=0;j<selections;j++) { const record=readPresentation(bytes,p);p=record.end;if(record.selection.animation) record.selection.animation.startedAt -= Date.now()-this.shellReceivedAt;paintPresentation(output,screen,this.width,this.height,record.selection) }
}
}
return output
+34
View File
@@ -0,0 +1,34 @@
export type MenuHighlightAnimation = { dx: number; dy: number; startedAt: number; token: number };
let nextToken = 1;
/** Eligibility is decided by menu navigation, before the display-list bridge. */
export class MenuHighlightMotion {
private previous?: { index: number; scroll: number; x: number; y: number; width: number; height: number };
private fromIndex: number | null = null;
private animation?: MenuHighlightAnimation;
navigate(from: number, wrapped: boolean): void {
this.fromIndex = wrapped ? null : from;
}
paint(index: number, scroll: number, x: number, y: number, width: number, height: number, now: number): MenuHighlightAnimation | undefined {
const previous = this.previous;
if (!previous || previous.index !== index || previous.scroll !== scroll ||
previous.x !== x || previous.y !== y || previous.width !== width || previous.height !== height) {
let fromX = previous?.x ?? x, fromY = previous?.y ?? y;
if (previous && this.animation) {
const t = Math.max(0, Math.min(1, (now - this.animation.startedAt) / 500));
const remaining = 1 - t * t * (3 - 2 * t);
fromX += this.animation.dx * remaining;
fromY += this.animation.dy * remaining;
}
const eligible = previous && previous.index !== index && this.fromIndex === previous.index &&
previous.scroll === scroll && previous.width === width && previous.height === height;
this.animation = eligible ? { dx: Math.round(fromX - x), dy: Math.round(fromY - y), startedAt: now, token: nextToken++ } : undefined;
this.fromIndex = null;
this.previous = { index, scroll, x, y, width, height };
}
return this.animation && now - this.animation.startedAt < 500 ? this.animation : undefined;
}
}
+10 -2
View File
@@ -1,3 +1,4 @@
import { MenuHighlightMotion } from "./menu-highlight-motion";
import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage, type UiFont } from "../graphics/image";
import { wrapText } from "../graphics/textwrap";
import { getDefaultSmallFont } from "../graphics/ui-fonts";
@@ -19,7 +20,7 @@ const MENU_HIGHLIGHT_SELECTED_BACKGROUND_FILL = 15;
const MENU_HIGHLIGHT_SELECTED_BORDER_STROKE = 45;
export type MenuLayout = {
/** Stereo depth of the menu surface; selected rows add +2. */
/** Stereo depth of the menu surface, including the selected row. */
depth?: number;
/** Left edge, or "center" to center horizontally on the painted surface. */
x: number | "center";
@@ -193,6 +194,7 @@ export const CONTEXT_MENU_DIM = 0.25;
export class MenuLayer implements Layer {
private selectedIndex = 0;
private scrollRow = 0;
private readonly highlightMotion = new MenuHighlightMotion();
get depth(): number { return this.layout.depth ?? 0; }
@@ -288,7 +290,11 @@ export class MenuLayer implements Layer {
} else {
row.drawText(font, textX, textY + LIST_ROW_TEXT_INSET, item.label, disabled ? 70 : selected ? 255 : 200);
}
if (selected) image.drawMenuSelection(row, x + 12, rowY, focused ? MENU_HIGHLIGHT_SELECTED_BACKGROUND_FILL : 0, MENU_HIGHLIGHT_SELECTED_BORDER_STROKE, 8, 2);
if (selected) {
const animation = this.highlightMotion.paint(index, this.scrollRow, x + 12, rowY, width - 24, rowHeight - 1, Date.now());
image.drawMenuSelection(row, x + 12, rowY, focused ? MENU_HIGHLIGHT_SELECTED_BACKGROUND_FILL : 0,
MENU_HIGHLIGHT_SELECTED_BORDER_STROKE, 8, 0, animation);
}
}
if (this.items.length > visibleRowCount) {
@@ -315,9 +321,11 @@ export class MenuLayer implements Layer {
}
switch (event.type) {
case "scroll-up":
this.highlightMotion.navigate(this.selectedIndex, this.selectedIndex === 0);
this.selectedIndex = (this.selectedIndex + this.items.length - 1) % this.items.length;
return;
case "scroll-down":
this.highlightMotion.navigate(this.selectedIndex, this.selectedIndex === this.items.length - 1);
this.selectedIndex = (this.selectedIndex + 1) % this.items.length;
return;
case "double-click":
@@ -0,0 +1,15 @@
package com.faceclaw.app
import java.util.concurrent.ScheduledThreadPoolExecutor
import java.util.concurrent.TimeUnit
private object DrawRedrawScheduler {
val executor = ScheduledThreadPoolExecutor(1) { task ->
Thread(task, "Faceclaw draw animation").apply { isDaemon = true }
}.apply { removeOnCancelPolicy = true }
}
internal actual fun scheduleDrawRedraw(delayMs: Int, action: () -> Unit): () -> Unit {
val future = DrawRedrawScheduler.executor.schedule(action, delayMs.toLong(), TimeUnit.MILLISECONDS)
return { future.cancel(false); Unit }
}
@@ -0,0 +1,260 @@
package com.faceclaw.app
/** Literal or branchless expression used by rounded-rectangle x/y in revision 27. */
sealed class DrawValue {
class Integer(val value: Int) : DrawValue()
class Expression(val program: ByteArray) : DrawValue()
companion object {
/** Smoothstep interpolation; time is measured from the next PRESENT. */
fun animate(from: Int, to: Int, durationMs: Int = 500, elapsedMs: Int = 0): DrawValue {
require(durationMs > 0 && elapsedMs >= 0)
if (from == to || elapsedMs >= durationMs) return Integer(to)
val code = ByteSink()
fun integer(value: Int) {
code.write(DrawExpression.PUSH_I32)
code.write(ExtendedVarint.signed(value))
}
integer(from)
code.write(DrawExpression.I2F)
integer(to)
code.write(DrawExpression.I2F)
integer(durationMs - elapsedMs)
code.write(DrawExpression.TIME)
integer(elapsedMs)
code.write(DrawExpression.IADD)
code.write(DrawExpression.I2F)
integer(durationMs)
code.write(DrawExpression.I2F)
code.write(DrawExpression.FDIV)
code.write(DrawExpression.SMOOTHSTEP)
code.write(DrawExpression.LERP)
code.write(DrawExpression.F2I)
return Expression(code.toByteArray())
}
}
}
/** Prefix payload is most-significant first; continuation bytes use all eight bits. */
internal object ExtendedVarint {
private val widths = intArrayOf(7, 14, 21, 28, 32)
private val prefixes = intArrayOf(0, 0x80, 0xc0, 0xe0, 0xf0)
fun signed(value: Int): ByteArray {
val size = widths.indexOfFirst { bits ->
bits == 32 || value.toLong() in -(1L shl (bits - 1)) until (1L shl (bits - 1))
} + 1
return encode(value, size)
}
fun unsigned(value: UInt): ByteArray {
val size = widths.indexOfFirst { it == 32 || value.toLong() < (1L shl it) } + 1
return encode(value.toInt(), size)
}
private fun encode(value: Int, size: Int): ByteArray {
val bytes = ByteArray(size)
if (size == 5) {
bytes[0] = 0xf0.toByte()
} else {
val mask = (1 shl (8 - size)) - 1
bytes[0] = (prefixes[size - 1] or ((value ushr (8 * (size - 1))) and mask)).toByte()
}
for (i in 1 until size) bytes[i] = (value ushr (8 * (size - 1 - i))).toByte()
return bytes
}
fun read(reader: DrawReader, signed: Boolean, first: Int = reader.readU8()): Int {
val size = when {
first < 0x80 -> 1
first < 0xc0 -> 2
first < 0xe0 -> 3
first < 0xf0 -> 4
first == 0xf0 -> 5
else -> error("Reserved extended-varint prefix")
}
var value = if (size == 5) 0 else first and ((1 shl (8 - size)) - 1)
repeat(size - 1) { value = (value shl 8) or reader.readU8() }
val bits = widths[size - 1]
return if (signed && bits < 32) (value shl (32 - bits)) shr (32 - bits) else value
}
fun write(value: DrawValue): ByteArray = when (value) {
is DrawValue.Integer -> signed(value.value)
is DrawValue.Expression -> {
require(value.program.size <= DrawExpression.MAX_BYTES)
byteArrayOf(0xff.toByte()) + unsigned(value.program.size.toUInt()) + value.program
}
}
fun evaluate(reader: DrawReader, frame: DrawEvaluation): Int {
val first = reader.readU8()
if (first != 0xff) return read(reader, true, first)
val length = read(reader, false).toUInt()
// Outer framing errors reject the call; errors inside a framed program yield zero.
require(length <= reader.remaining.toUInt())
val program = reader.readSlice(length.toInt())
if (length > DrawExpression.MAX_BYTES.toUInt()) return 0
return DrawExpression.evaluate(program, frame)
}
}
internal class DrawEvaluation(val elapsedMs: Long) {
var animationPending = false
}
/** Typed i32/f32 stack machine. Keep opcode IDs and semantics in sync with draw_expression.c. */
internal object DrawExpression {
const val MAX_BYTES = 1024
const val MAX_STACK = 32
const val PUSH_I32 = 1
const val PUSH_F32 = 2
const val DUP = 3
const val DROP = 4
const val SWAP = 5
const val IADD = 16
const val ISUB = 17
const val IMUL = 18
const val IDIV = 19
const val IMOD = 20
const val INEG = 21
const val IMIN = 22
const val IMAX = 23
const val FADD = 32
const val FSUB = 33
const val FMUL = 34
const val FDIV = 35
const val FNEG = 36
const val FMIN = 37
const val FMAX = 38
const val I2F = 48
const val F2I = 49
const val TIME = 50
const val LERP = 64
const val SMOOTHSTEP = 65
const val EASE_IN_QUAD = 66
const val EASE_OUT_QUAD = 67
const val EASE_IN_OUT_QUAD = 68
const val EASE_IN_CUBIC = 69
const val EASE_OUT_CUBIC = 70
const val EASE_IN_OUT_CUBIC = 71
fun evaluate(reader: DrawReader, frame: DrawEvaluation): Int {
val bits = IntArray(MAX_STACK)
val floats = BooleanArray(MAX_STACK)
var size = 0
var pending = false
fun push(value: Int, float: Boolean = false) {
require(size < MAX_STACK)
bits[size] = value
floats[size++] = float
}
fun integer(): Int {
require(size > 0 && !floats[size - 1])
return bits[--size]
}
fun float(): Float {
require(size > 0 && floats[size - 1])
return Float.fromBits(bits[--size])
}
fun pushFloat(value: Float) {
require(value.isFinite())
push(value.toBits(), true)
}
fun toInteger(value: Float): Int {
require(value.isFinite() && value >= -2147483648f && value < 2147483648f)
return value.toInt()
}
try {
require(reader.remaining <= MAX_BYTES)
while (reader.remaining > 0) {
when (val op = reader.readU8()) {
PUSH_I32 -> push(ExtendedVarint.read(reader, true))
PUSH_F32 -> {
val value = reader.readU8() or (reader.readU8() shl 8) or
(reader.readU8() shl 16) or (reader.readU8() shl 24)
pushFloat(Float.fromBits(value))
}
DUP -> {
require(size > 0)
push(bits[size - 1], floats[size - 1])
}
DROP -> { require(size > 0); size-- }
SWAP -> {
require(size >= 2)
val value = bits[size - 1]
val type = floats[size - 1]
bits[size - 1] = bits[size - 2]
floats[size - 1] = floats[size - 2]
bits[size - 2] = value
floats[size - 2] = type
}
IADD, ISUB, IMUL, IDIV, IMOD, IMIN, IMAX -> {
val b = integer()
val a = integer()
push(when (op) {
IADD -> a + b
ISUB -> a - b
IMUL -> a * b
IDIV -> { require(b != 0); if (a == Int.MIN_VALUE && b == -1) a else a / b }
IMOD -> { require(b != 0); if (a == Int.MIN_VALUE && b == -1) 0 else a % b }
IMIN -> minOf(a, b)
else -> maxOf(a, b)
})
}
INEG -> push(-integer())
FADD, FSUB, FMUL, FDIV, FMIN, FMAX -> {
val b = float()
val a = float()
pushFloat(when (op) {
FADD -> a + b
FSUB -> a - b
FMUL -> a * b
FDIV -> { require(b != 0f); a / b }
FMIN -> minOf(a, b)
else -> maxOf(a, b)
})
}
FNEG -> pushFloat(-float())
I2F -> pushFloat(integer().toFloat())
F2I -> push(toInteger(float()))
TIME -> {
val maximum = integer()
require(maximum >= 0)
val time = frame.elapsedMs.coerceIn(0, maximum.toLong()).toInt()
if (time != maximum) pending = true
push(time)
}
LERP -> {
val t = float()
val b = float()
val a = float()
pushFloat(a + (b - a) * t)
}
in SMOOTHSTEP..EASE_IN_OUT_CUBIC -> {
val t = float().coerceIn(0f, 1f)
val u = 1f - t
pushFloat(when (op) {
SMOOTHSTEP -> t * t * (3f - 2f * t)
EASE_IN_QUAD -> t * t
EASE_OUT_QUAD -> 1f - u * u
EASE_IN_OUT_QUAD -> if (t < .5f) 2f * t * t else 1f - 2f * u * u
EASE_IN_CUBIC -> t * t * t
EASE_OUT_CUBIC -> 1f - u * u * u
else -> if (t < .5f) 4f * t * t * t else 1f - 4f * u * u * u
})
}
else -> error("Unknown expression opcode")
}
}
require(size > 0)
val result = if (floats[size - 1]) toInteger(float()) else integer()
frame.animationPending = frame.animationPending || pending
return result
} catch (_: IllegalArgumentException) {
return 0
} catch (_: IllegalStateException) {
return 0
}
}
}
@@ -1,6 +1,6 @@
package com.faceclaw.app
/** Revision 26 wire grammar, shared by scene planning and the local renderer. */
/** Revision 27 wire grammar, shared by scene planning and the local renderer. */
object DrawProtocol {
/** Mirrors g2flash/patches/zlib_glue.c. */
const val DRAW = CFW_MSG_DRAW_CALLS
@@ -18,87 +18,274 @@ object DrawProtocol {
/** Mirrors g2flash/patches/display_list.c. */
const val SCREEN = 65535
const val CURRENT = 65534
/** Fixed-width resource/legacy fields, separate from the draw numeric codec. */
fun u16(b: ByteArray, p: Int): Int = (b[p].toInt() and 255) or ((b[p + 1].toInt() and 255) shl 8)
fun word(n: Int) = byteArrayOf(n.toByte(), (n ushr 8).toByte())
private fun encode(write: DrawWriter.() -> Unit): ByteArray = DrawWriter().apply(write).toByteArray()
fun call(op: Int, args: ByteArray, target: Int? = null, depth: Int? = null): ByteArray {
require(depth == null || depth in -128..127)
val flags = (if (target == null) 0 else DRAW_FLAG_RESOURCE_TARGET) or (if (depth == null) 0 else DRAW_FLAG_DEPTH)
return byteArrayOf(op.toByte(), flags.toByte()) + (target?.let { word(it) } ?: byteArrayOf()) +
(depth?.let { byteArrayOf(it.toByte()) } ?: byteArrayOf()) + args
val flags = (if (target == null) 0 else DRAW_FLAG_RESOURCE_TARGET) or
(if (depth == null) 0 else DRAW_FLAG_DEPTH)
return encode {
writeU8(op)
writeU8(flags)
if (target != null) writeU16(target)
if (depth != null) writeS8(depth)
writeBytes(args)
}
}
private fun call(op: Int, target: Int? = null, depth: Int? = null, write: DrawWriter.() -> Unit): ByteArray =
call(op, encode(write), target, depth)
fun depthOffset(depth: Int, right: Boolean): Int =
if (right) -floorHalf(depth + 1) else floorHalf(depth)
private fun floorHalf(n: Int): Int = if (n < 0) (n - 1) / 2 else n / 2
fun roundedRect(x: Int, y: Int, width: Int, height: Int, radius: Int, background: Int, border: Int = DRAW_ROUNDED_RECT_NO_BORDER, depth: Int? = null): ByteArray {
require(width in 1..640 && height in 1..480 && radius in 0..65535 && background in 0..15 && border in 0..DRAW_ROUNDED_RECT_NO_BORDER)
return call(DRAW_OP_ROUNDED_RECT, word(x) + word(y) + word(width) + word(height) + word(radius) + byteArrayOf(background.toByte(), border.toByte()), depth = depth)
fun roundedRect(
x: Int, y: Int, width: Int, height: Int, radius: Int, background: Int,
border: Int = DRAW_ROUNDED_RECT_NO_BORDER, depth: Int? = null,
): ByteArray = roundedRect(DrawValue.Integer(x), DrawValue.Integer(y), width, height, radius, background, border, depth)
fun roundedRect(
x: DrawValue, y: DrawValue, width: Int, height: Int, radius: Int, background: Int,
border: Int = DRAW_ROUNDED_RECT_NO_BORDER, depth: Int? = null,
): ByteArray {
require(width in 1..640 && height in 1..480 && radius in 0..65535)
require(background in 0..15 && border in 0..DRAW_ROUNDED_RECT_NO_BORDER)
return call(DRAW_OP_ROUNDED_RECT, depth = depth) {
writeExtended(x)
writeExtended(y)
writeU16(width)
writeU16(height)
writeU16(radius)
writeU8(background)
writeU8(border)
}
}
fun sequence(calls: List<ByteArray>): ByteArray {
require(calls.size <= 4096)
val out = ByteSink(); out.write(word(calls.size))
for (call in calls) { require(call.size <= 65535); out.write(word(call.size)); out.write(call) }
return out.toByteArray()
return encode {
writeU16(calls.size)
for (call in calls) {
require(call.size <= 65535)
writeU16(call.size)
writeBytes(call)
}
}
}
fun message(calls: List<ByteArray>) = byteArrayOf(DRAW.toByte()) + sequence(calls)
fun displayList(calls: List<ByteArray>) = byteArrayOf(LIST.toByte()) + sequence(calls)
fun root(id: Int) = byteArrayOf(ROOT.toByte()) + word(id)
fun image(id: Int, x: Int, y: Int, options: Int = CFW_TEXTURE_OPT_BRIGHTNESS_MASK, target: Int? = null, depth: Int? = null) =
call(DRAW_OP_IMAGE, word(id) + word(x) + word(y) + byteArrayOf(options.toByte()), target, depth)
fun root(id: Int) = byteArrayOf(ROOT.toByte()) + encode { writeU16(id) }
fun image(
id: Int, x: Int, y: Int, options: Int = CFW_TEXTURE_OPT_BRIGHTNESS_MASK,
target: Int? = null, depth: Int? = null,
): ByteArray = call(DRAW_OP_IMAGE, target, depth) {
writeU16(id)
writeS16(x)
writeS16(y)
writeU8(options)
}
fun rectCopy(
source: Int, x: Int, y: Int, width: Int, height: Int, dx: Int, dy: Int,
target: Int? = null, depth: Int? = null,
): ByteArray = call(DRAW_OP_RECT_COPY, target, depth) {
writeU16(source)
writeU16(x)
writeU16(y)
writeU16(width)
writeU16(height)
writeS16(dx)
writeS16(dy)
}
fun stockText(x: Int, y: Int, options: Int, text: ByteArray): ByteArray {
require(text.size <= 255)
return call(DRAW_OP_STOCK_FONT_STRING) {
writeS16(x)
writeS16(y)
writeU8(options)
writeU8(text.size)
writeBytes(text)
}
}
fun text(id: Int, x: Int, y: Int, options: Int, text: ByteArray): ByteArray {
require(text.size <= 255)
return call(DRAW_OP_TEXT) {
writeU16(id)
writeS16(x)
writeS16(y)
writeU8(options)
writeU8(text.size)
writeBytes(text)
}
}
fun playList(id: Int, target: Int? = null, depth: Int? = null): ByteArray =
call(DRAW_OP_DISPLAY_LIST, target, depth) { writeU16(id) }
fun lut(width: Int, height: Int, factor: Int): ByteArray {
val table = ByteArray(8) { i ->
(((i * 2 * factor / 256).coerceIn(0, 15) shl 4) or ((i * 2 + 1) * factor / 256).coerceIn(0, 15)).toByte()
(((i * 2 * factor / 256).coerceIn(0, 15) shl 4) or
((i * 2 + 1) * factor / 256).coerceIn(0, 15)).toByte()
}
return call(DRAW_OP_REMAP_COLORS) {
writeU16(0)
writeU16(0)
writeU16(width)
writeU16(height)
writeBytes(table)
}
return call(DRAW_OP_REMAP_COLORS, word(0) + word(0) + word(width) + word(height) + table)
}
fun rawImage(width: Int, height: Int, pixels: ByteArray = ByteArray(((width + 1) / 2) * height)): ByteArray {
require(width in 1..640 && height in 1..480 && pixels.size == ((width + 1) / 2) * height)
val result = byteArrayOf(LARGE.toByte()) + word(width) + word(height) + pixels
require(result.size <= ResourceCacheState.MAX_RESOURCE_SIZE)
return result
}
private fun boundingBox(
x: Int, y: Int, width: Int, height: Int, compact: Boolean, rle: ByteArray, target: Int? = null,
): ByteArray = call(DRAW_OP_BOUNDING_BOX, target) {
writeU8(if (compact) 0 else DRAW_BBOX_FLAG_U16)
if (compact) {
writeU8(x / 4)
writeU8(y / 2)
writeU8(width / 4)
writeU8(height / 2)
} else {
writeU16(x)
writeU16(y)
writeU16(width)
writeU16(height)
}
writeBytes(rle)
}
/** Packed rows -> RLE of exactly width*height pixels, omitting odd-row padding. */
fun bbox(packed: ByteArray, stride: Int, x: Int, y: Int, width: Int, height: Int, target: Int? = null): ByteArray {
val compact = x % 4 == 0 && y % 2 == 0 && width % 4 == 0 && height % 2 == 0 && x / 4 < 256 && y / 2 < 256 && width / 4 < 256 && height / 2 < 256
val header = if (compact) byteArrayOf(0, (x / 4).toByte(), (y / 2).toByte(), (width / 4).toByte(), (height / 2).toByte())
else byteArrayOf(DRAW_BBOX_FLAG_U16.toByte()) + word(x) + word(y) + word(width) + word(height)
val out = ByteSink(); out.write(header)
var color = -1; var count = 0
val compact = x % 4 == 0 && y % 2 == 0 && width % 4 == 0 && height % 2 == 0 &&
x / 4 < 256 && y / 2 < 256 && width / 4 < 256 && height / 2 < 256
// Pixel RLE is an opaque byte block, with its own fixed-width run lengths.
val out = ByteSink()
var color = -1
var count = 0
fun flush() {
if (count == 0) return
if (count <= 15) out.write((count shl 4) or color)
else { out.write(color); if (count <= 255) out.write(count) else { out.write(0); out.write(word(count)) } }
if (count <= 15) {
out.write((count shl 4) or color)
} else {
out.write(color)
if (count <= 255) {
out.write(count)
} else {
out.write(0)
out.write(word(count))
}
}
}
for (yy in y until y + height) for (xx in x until x + width) {
val value = (packed[yy * stride + xx / 2].toInt() ushr (if (xx % 2 == 0) 4 else 0)) and 15
if (value != color || count == 65535) { flush(); color = value; count = 0 }
val shift = if (xx % 2 == 0) 4 else 0
val value = (packed[yy * stride + xx / 2].toInt() ushr shift) and 15
if (value != color || count == 65535) {
flush()
color = value
count = 0
}
count++
}
flush()
return call(DRAW_OP_BOUNDING_BOX, out.toByteArray(), target)
return boundingBox(x, y, width, height, compact, out.toByteArray(), target)
}
/** Internal optimizer formats are translated here; they are never sent to revision 26. */
/**
* Optimizer records retain their internal fixed-width format. Decode their
* numeric fields here so every outgoing draw field goes through DrawWriter.
*/
fun fromOptimized(payload: ByteArray, width: Int = 640, height: Int = 480): List<ByteArray> {
return when (val mode = payload[0].toInt() and CFW_MSG_TYPE_MASK) {
val reader = ArrayByteReader(payload)
fun u8() = reader.get().toInt() and 255
fun u16() = reader.getShort().toInt() and 65535
fun s16() = reader.getShort().toInt()
fun bytes(count: Int): ByteArray = ByteArray(count).also { reader.get(it) }
val result = when (val mode = u8() and CFW_MSG_TYPE_MASK) {
CFW_MSG_MULTI_SEGMENT -> {
val result = ArrayList<ByteArray>(); var pos = 2
repeat(payload[1].toInt() and 255) { val n = u16(payload, pos); pos += 2; result.addAll(fromOptimized(payload.copyOfRange(pos, pos + n), width, height)); pos += n }
require(pos == payload.size); result
val calls = ArrayList<ByteArray>()
repeat(u8()) {
calls.addAll(fromOptimized(bytes(u16()), width, height))
}
calls
}
CFW_MSG_BOUNDING_BOX -> {
val x = u8() * 4
val y = u8() * 2
val w = u8() * 4
val h = u8() * 2
u16() // Frame ID belongs only to the optimizer record.
listOf(boundingBox(x, y, w, h, true, bytes(reader.remaining())))
}
CFW_MSG_FULL_FRAME -> listOf(boundingBox(0, 0, width, height, false, bytes(reader.remaining())))
CFW_MSG_STOCK_FONT_STRING -> {
val x = s16()
val y = s16()
val options = u8()
listOf(stockText(x, y, options, bytes(u8())))
}
CFW_MSG_CACHED_IMAGE -> {
val id = u16()
val x = s16()
val y = s16()
val options = u8()
listOf(image(id, x, y, options))
}
CFW_MSG_CACHED_TEXT -> {
val id = u16()
val x = s16()
val y = s16()
val options = u8()
listOf(text(id, x, y, options, bytes(u8())))
}
CFW_MSG_RECT_COPY -> {
val x = u16()
val y = u16()
val w = u16()
val h = u16()
val dx = s16()
val dy = s16()
listOf(rectCopy(CURRENT, x, y, w, h, dx, dy))
}
CFW_MSG_BOUNDING_BOX -> listOf(call(DRAW_OP_BOUNDING_BOX, byteArrayOf(0) + payload.copyOfRange(1, 5) + payload.copyOfRange(7, payload.size)))
CFW_MSG_FULL_FRAME -> listOf(call(DRAW_OP_BOUNDING_BOX, byteArrayOf(DRAW_BBOX_FLAG_U16.toByte()) + word(0) + word(0) + word(width) + word(height) + payload.copyOfRange(1, payload.size)))
CFW_MSG_STOCK_FONT_STRING -> listOf(call(DRAW_OP_STOCK_FONT_STRING, payload.copyOfRange(1, payload.size)))
CFW_MSG_CACHED_IMAGE -> listOf(call(DRAW_OP_IMAGE, payload.copyOfRange(1, payload.size)))
CFW_MSG_CACHED_TEXT -> listOf(call(DRAW_OP_TEXT, payload.copyOfRange(1, payload.size)))
CFW_MSG_RECT_COPY -> listOf(call(DRAW_OP_RECT_COPY, word(CURRENT) + payload.copyOfRange(1, 9) + payload.copyOfRange(9, 13)))
else -> error("Unsupported internal draw format $mode")
}
require(reader.remaining() == 0)
return result
}
fun messages(calls: List<ByteArray>, maxBytes: Int = 65535): List<ByteArray> {
val result = ArrayList<ByteArray>(); var batch = ArrayList<ByteArray>(); var size = 3
fun headerSize(count: Int) = 1 + encode { writeU16(count) }.size
val result = ArrayList<ByteArray>()
var batch = ArrayList<ByteArray>()
var bodySize = 0
for (call in calls) {
require(call.size + 5 <= maxBytes)
if (size + 2 + call.size > maxBytes) { result.add(message(batch)); batch = ArrayList(); size = 3 }
batch.add(call); size += call.size + 2
require(call.size <= 65535)
val framedSize = encode { writeU16(call.size) }.size + call.size
require(headerSize(1) + framedSize <= maxBytes)
if (headerSize(batch.size + 1) + bodySize + framedSize > maxBytes || batch.size == 4096) {
result.add(message(batch))
batch = ArrayList()
bodySize = 0
}
batch.add(call)
bodySize += framedSize
}
if (batch.isNotEmpty()) result.add(message(batch))
return result
@@ -0,0 +1,68 @@
package com.faceclaw.app
/**
* Numeric fields in the draw grammar go through this codec. Most fields still
* uses fixed-width little-endian values; rounded-rectangle x/y use extended varints and expressions. Byte blocks
* (RLE, text, LUTs and already-encoded calls) retain their own formats.
*/
internal class DrawReader(
private val bytes: ByteArray,
start: Int = 0,
private val end: Int = bytes.size,
) {
var position: Int = start
private set
val remaining: Int get() = end - position
init {
require(start >= 0 && start <= end && end <= bytes.size)
}
fun readU8(): Int {
require(remaining >= 1) { "Truncated draw field" }
return bytes[position++].toInt() and 255
}
fun readS8(): Int = readU8().toByte().toInt()
fun readU16(): Int {
val low = readU8()
return low or (readU8() shl 8)
}
fun readS16(): Int = readU16().toShort().toInt()
fun readBytes(count: Int): ByteArray {
require(count >= 0 && count <= remaining) { "Truncated draw data" }
val result = bytes.copyOfRange(position, position + count)
position += count
return result
}
/** A bounded view prevents a truncated call from consuming the next call. */
fun readSlice(count: Int): DrawReader {
require(count >= 0 && count <= remaining) { "Truncated draw call" }
val result = DrawReader(bytes, position, position + count)
position += count
return result
}
fun requireDone() {
require(remaining == 0) { "Trailing draw data" }
}
}
internal class DrawWriter {
private val out = ByteSink()
fun writeU8(value: Int) = out.write(value)
fun writeS8(value: Int) = out.write(value)
fun writeU16(value: Int) {
out.write(value)
out.write(value ushr 8)
}
fun writeS16(value: Int) = writeU16(value)
fun writeBytes(value: ByteArray) = out.write(value)
fun writeExtended(value: DrawValue) = out.write(ExtendedVarint.write(value))
fun toByteArray(): ByteArray = out.toByteArray()
}
@@ -0,0 +1,42 @@
package com.faceclaw.app
/**
* PRESENT resets the clock; timer redraws preserve it. The owner serializes
* calls and supplies a cancellable 45 ms scheduler on its rendering thread.
*/
class DisplayListPlayer(
private val renderer: DisplayListRenderer,
private val screen: DisplayListRenderer.Target,
private val composition: DisplayListRenderer.Target,
private val clock: () -> Long,
private val schedule: (Int, () -> Unit) -> (() -> Unit),
private val displayed: () -> Unit,
) {
private var root = DrawProtocol.SCREEN
private var origin = 0L
private var generation = 0
private var cancel: (() -> Unit)? = null
fun present(root: Int) {
stop()
this.root = root
origin = clock()
draw(generation)
}
fun stop() {
generation++
cancel?.invoke()
cancel = null
}
private fun draw(token: Int) {
if (token != generation) return
cancel = null
renderer.render(root, screen, composition, (clock() - origin).coerceAtLeast(0))
displayed()
if (token == generation && renderer.animationPending) {
cancel = schedule(45) { draw(token) }
}
}
}
@@ -1,68 +1,132 @@
package com.faceclaw.app
/** Reference interpreter. It uses the firmware's packed rows, clipping, LUT and call grammar. */
class DisplayListRenderer(private val resources: Map<Int, ByteArray>, private val builtin: ((Int, Int) -> BuiltinGlyph)? = null, private val rightLens: Boolean = false) {
class DisplayListRenderer(
private val resources: Map<Int, ByteArray>,
private val builtin: ((Int, Int) -> BuiltinGlyph)? = null,
private val rightLens: Boolean = false,
) {
class BuiltinGlyph(val image: ByteArray, val advance: Int, val x: Int = 0, val y: Int = 0)
class Target(val bytes: ByteArray, val width: Int, val height: Int, val offset: Int = 0, val shiftX: Int = 0) {
class Target(
val bytes: ByteArray,
val width: Int,
val height: Int,
val offset: Int = 0,
val shiftX: Int = 0,
) {
val stride = (width + 1) / 2
init { require(width > 0 && height > 0 && offset >= 0 && bytes.size - offset >= stride * height) }
fun get(x: Int, y: Int) = (bytes[offset + y * stride + x / 2].toInt() ushr (if (x % 2 == 0) 4 else 0)) and 15
init {
require(width > 0 && height > 0 && offset >= 0 && bytes.size - offset >= stride * height)
}
fun get(x: Int, y: Int): Int {
val shift = if (x % 2 == 0) 4 else 0
return (bytes[offset + y * stride + x / 2].toInt() ushr shift) and 15
}
fun shifted(dx: Int) = Target(bytes, width, height, offset, shiftX + dx)
fun put(localX: Int, y: Int, v: Int) {
fun put(localX: Int, y: Int, value: Int) {
val x = localX + shiftX
if (x !in 0 until width || y !in 0 until height) return
val index = offset + y * stride + x / 2; val old = bytes[index].toInt()
bytes[index] = (if (x % 2 == 0) (old and 15) or (v shl 4) else (old and 240) or v).toByte()
val index = offset + y * stride + x / 2
val old = bytes[index].toInt()
bytes[index] = if (x % 2 == 0) {
((old and 15) or (value shl 4)).toByte()
} else {
((old and 240) or value).toByte()
}
}
}
private fun roundedContains(x: Int, y: Int, w: Int, h: Int, radius: Int): Boolean {
if (x !in 0 until w || y !in 0 until h) return false
val r = minOf(radius, w / 2, h / 2)
val dx = maxOf(0, 2 * r - (2 * minOf(x, w - 1 - x) + 1))
val dy = maxOf(0, 2 * r - (2 * minOf(y, h - 1 - y) + 1))
return dx * dx + dy * dy <= 4 * r * r
/** Shared by nested calls, and reset between validation and drawing. */
var animationPending: Boolean = false
private set
private class Walk(val screen: Target, val apply: Boolean, val references: MutableSet<Int>, val frame: DrawEvaluation) {
val ancestors = mutableListOf<Int>()
var remainingCalls = 4096
}
private class Image(val width: Int, val height: Int, val pixels: IntArray)
private fun word(b: ByteArray, p: Int): Int { require(p >= 0 && p + 2 <= b.size); return DrawProtocol.u16(b, p) }
private fun signed(b: ByteArray, p: Int) = word(b, p).toShort().toInt()
private fun resource(id: Int) = requireNotNull(resources[id]) { "Missing resource $id" }
private fun decodeRle(b: ByteArray, start: Int, count: Int, exact: Boolean): IntArray {
var p = start; var i = 0; val result = IntArray(count)
while (i < count) {
require(p < b.size); val token = b[p++].toInt() and 255; var n = token ushr 4
if (n == 0) { require(p < b.size); n = b[p++].toInt() and 255
if (n == 0) { n = word(b, p); p += 2 } }
require(n > 0 && n <= count - i); result.fill(token and 15, i, i + n); i += n
// Resource headers, font offsets and RLE have their own fixed-width grammar;
// they do not use the draw-call numeric codec.
private fun readU8(reader: ByteReader): Int = reader.get().toInt() and 255
private fun readU16(reader: ByteReader): Int = reader.getShort().toInt() and 65535
private fun decodeRle(reader: ByteReader, count: Int, exact: Boolean): IntArray {
val pixels = IntArray(count)
var index = 0
while (index < count) {
val token = readU8(reader)
var run = token ushr 4
if (run == 0) {
run = readU8(reader)
if (run == 0) run = readU16(reader)
}
require(run > 0 && run <= count - index)
pixels.fill(token and 15, index, index + run)
index += run
}
require(!exact || p == b.size)
return result
require(!exact || reader.remaining() == 0)
return pixels
}
private fun image(b: ByteArray, start: Int = 0): Image {
require(start in b.indices); val flags = b[start].toInt() and 255
private fun image(bytes: ByteArray, start: Int = 0): Image {
require(start in bytes.indices)
val reader = ArrayByteReader(bytes, start)
val flags = readU8(reader)
require(flags and CFW_RESOURCE_IMAGE_FLAGS_MASK == flags)
val header = if (flags and CFW_RESOURCE_FLAG_LARGE != 0) 5 else 3
require(b.size - start >= header)
val w = if (header == 5) word(b, start + 1) else b[start + 1].toInt() and 255
val h = if (header == 5) word(b, start + 3) else b[start + 2].toInt() and 255
require(w in 1..640 && h in 1..480)
val pixels = if (flags and CFW_RESOURCE_FLAG_RLE != 0) decodeRle(b, start + header, w * h, false)
else { val t = Target(b, w, h, start + header); IntArray(w * h) { t.get(it % w, it / w) } }
return Image(w, h, pixels)
val large = flags and CFW_RESOURCE_FLAG_LARGE != 0
val width = if (large) readU16(reader) else readU8(reader)
val height = if (large) readU16(reader) else readU8(reader)
require(width in 1..640 && height in 1..480)
val pixels = if (flags and CFW_RESOURCE_FLAG_RLE != 0) {
decodeRle(reader, width * height, false)
} else {
val offset = bytes.size - reader.remaining()
val target = Target(bytes, width, height, offset)
IntArray(width * height) { target.get(it % width, it / width) }
}
return Image(width, height, pixels)
}
private fun target(id: Int): Target {
val b = resource(id); require(b.isNotEmpty() && b[0].toInt() and (CFW_RESOURCE_TYPE_MASK or CFW_RESOURCE_FLAG_RLE) == 0)
image(b) // Validate the complete raw image before exposing a write target.
return if (b[0].toInt() and CFW_RESOURCE_FLAG_LARGE != 0) Target(b, word(b, 1), word(b, 3), 5)
else Target(b, b[1].toInt() and 255, b[2].toInt() and 255, 3)
val bytes = resource(id)
val reader = ArrayByteReader(bytes)
val flags = readU8(reader)
require(flags and (CFW_RESOURCE_TYPE_MASK or CFW_RESOURCE_FLAG_RLE) == 0)
image(bytes) // Validate the complete raw image before exposing a write target.
val large = flags and CFW_RESOURCE_FLAG_LARGE != 0
val width = if (large) readU16(reader) else readU8(reader)
val height = if (large) readU16(reader) else readU8(reader)
return Target(bytes, width, height, bytes.size - reader.remaining())
}
private fun draw(image: Image, target: Target, x: Int, y: Int, options: Int, apply: Boolean) {
if (!apply) return
for (i in image.pixels.indices) {
val v = image.pixels[i]; if (v == 0 && options and CFW_TEXTURE_OPT_TRANSPARENT != 0) continue
val source = if (options and CFW_TEXTURE_OPT_INVERSE != 0) 15 - v else v
target.put(x + i % image.width, y + i / image.width, source * (options and CFW_TEXTURE_OPT_BRIGHTNESS_MASK) / 15)
val value = image.pixels[i]
if (value == 0 && options and CFW_TEXTURE_OPT_TRANSPARENT != 0) continue
val source = if (options and CFW_TEXTURE_OPT_INVERSE != 0) 15 - value else value
val brightness = source * (options and CFW_TEXTURE_OPT_BRIGHTNESS_MASK) / 15
target.put(x + i % image.width, y + i / image.width, brightness)
}
}
private fun roundedContains(x: Int, y: Int, width: Int, height: Int, radius: Int): Boolean {
if (x !in 0 until width || y !in 0 until height) return false
val r = minOf(radius, width / 2, height / 2)
val dx = maxOf(0, 2 * r - (2 * minOf(x, width - 1 - x) + 1))
val dy = maxOf(0, 2 * r - (2 * minOf(y, height - 1 - y) + 1))
return dx * dx + dy * dy <= 4 * r * r
}
private fun copyScreen(screen: Target, composition: Target) {
val source = screen.bytes
val destination = composition.bytes
@@ -93,107 +157,230 @@ class DisplayListRenderer(private val resources: Map<Int, ByteArray>, private va
dst += destinationStride
}
}
/** Validate the whole graph before changing any pixels. Resource writes remain call-scoped. */
fun render(root: Int, screen: Target, composition: Target): Set<Int> {
fun render(root: Int, screen: Target, composition: Target, elapsedMs: Long = 0): Set<Int> {
animationPending = false
val frame = DrawEvaluation(elapsedMs)
val references = mutableSetOf<Int>()
for (apply in listOf(false, true)) {
if (apply) copyScreen(screen, composition)
if (root != DrawProtocol.SCREEN) executeList(root, composition, screen, apply, mutableListOf(), intArrayOf(4096), references)
}
return references
}
fun execute(sequence: ByteArray, target: Target, screen: Target = target): Set<Int> {
val references = mutableSetOf<Int>()
for (apply in listOf(false, true)) executeSequence(sequence, target, screen, apply, mutableListOf(), intArrayOf(4096), references)
return references
}
private fun executeList(id: Int, t: Target, screen: Target, apply: Boolean, stack: MutableList<Int>, budget: IntArray, refs: MutableSet<Int>) {
require(stack.size < 8 && id !in stack); val b = resource(id); require(b.size >= 3 && b[0].toInt() == CFW_RESOURCE_TYPE_DISPLAY_LIST)
refs.add(id); stack.add(id); executeSequence(b.copyOfRange(1, b.size), t, screen, apply, stack, budget, refs); stack.removeAt(stack.lastIndex)
}
private fun executeSequence(b: ByteArray, inherited: Target, screen: Target, apply: Boolean, stack: MutableList<Int>, budget: IntArray, refs: MutableSet<Int>) {
val count = word(b, 0); var pos = 2
repeat(count) {
val n = word(b, pos); pos += 2; require(n >= 2 && n <= b.size - pos && budget[0]-- > 0)
val call = b.copyOfRange(pos, pos + n); pos += n
val flags = call[1].toInt() and 255; require(flags and DRAW_FLAGS_MASK.inv() == 0)
var t = inherited; var header = 2
if (flags and DRAW_FLAG_RESOURCE_TARGET != 0) { val id = word(call, 2); refs.add(id); t = target(id).shifted(inherited.shiftX); header += 2 }
if (flags and DRAW_FLAG_DEPTH != 0) { require(header < call.size); t = t.shifted(DrawProtocol.depthOffset(call[header++].toInt(), rightLens)) }
val p = call.copyOfRange(header, call.size)
when (call[0].toInt()) {
DRAW_OP_BOUNDING_BOX -> {
require(p.isNotEmpty() && (p[0].toInt() and 255) and DRAW_BBOX_FLAG_U16.inv() == 0); val wide = p[0].toInt() and DRAW_BBOX_FLAG_U16 != 0
require(p.size >= if (wide) 9 else 5)
val x = if (wide) word(p, 1) else (p[1].toInt() and 255) * 4
val y = if (wide) word(p, 3) else (p[2].toInt() and 255) * 2
val w = if (wide) word(p, 5) else (p[3].toInt() and 255) * 4
val h = if (wide) word(p, 7) else (p[4].toInt() and 255) * 2
require(w > 0 && h > 0 && x + w <= t.width && y + h <= t.height)
val pixels = decodeRle(p, if (wide) 9 else 5, w * h, true)
if (apply) for (i in pixels.indices) t.put(x + i % w, y + i / w, pixels[i])
}
DRAW_OP_RECT_COPY -> {
require(p.size == 14); val id = word(p, 0)
val src = when(id) { DrawProtocol.SCREEN -> screen; DrawProtocol.CURRENT -> t; else -> { refs.add(id); target(id) } }
val x = word(p, 2); val y = word(p, 4); val w = word(p, 6); val h = word(p, 8)
require(w > 0 && h > 0 && x + w <= src.width && y + h <= src.height)
val dx = signed(p, 10); val dy = signed(p, 12)
if (apply) { val copy = IntArray(w * h) { src.get(x + it % w, y + it / w) }
for (i in copy.indices) t.put(dx + i % w, dy + i / w, copy[i]) }
}
DRAW_OP_STOCK_FONT_STRING -> {
require(p.size >= 6 && p.size == 6 + (p[5].toInt() and 255))
val text = p.copyOfRange(6, p.size).decodeToString(throwOnInvalidSequence = true)
val cps = ArrayList<Int>(); var i = 0
while (i < text.length) { val c = text[i++].code
cps.add(if (c in 0xd800..0xdbff) { require(i < text.length); 0x10000 + ((c - 0xd800) shl 10) + text[i++].code - 0xdc00 } else c) }
var x = signed(p, 0); val y = signed(p, 2)
for (j in cps.indices) { val cp = cps[j]
if (cp in 1..31) { x += cp - 11; continue }
val next = cps.drop(j + 1).firstOrNull { it !in 1..31 } ?: 0
val g = requireNotNull(builtin) { "Builtin font provider required" }(cp, next)
draw(image(g.image), t, x + g.x, y + g.y, p[4].toInt() and 255, apply); x += g.advance
}
}
DRAW_OP_IMAGE, DRAW_OP_TEXT -> {
val font = call[0].toInt() == DRAW_OP_TEXT; require(if(font) p.size >= 8 && p.size == 8 + (p[7].toInt() and 255) else p.size == 7)
val id = word(p, 0); refs.add(id); val data = resource(id)
var x = signed(p, 2); val y = signed(p, 4); val options = p[6].toInt() and 255
if (!font) draw(image(data), t, x, y, options, apply)
else { require(data.size >= 193 && data[0].toInt() == CFW_RESOURCE_TYPE_FONT)
for (j in 8 until p.size) { val ch = p[j].toInt() and 255
if(ch in 1..31) { x += ch - 11; continue }; require(ch in 32..127)
val offset = word(data, 1 + (ch - 32) * 2); require(offset >= 193)
val glyph = image(data, offset); draw(glyph, t, x, y, options, apply); x += glyph.width
}
}
}
DRAW_OP_REMAP_COLORS -> {
require(p.size == 16); val x = word(p, 0); val y = word(p, 2); val w = word(p, 4); val h = word(p, 6)
require(w > 0 && h > 0 && x + w <= t.width && y + h <= t.height)
if (apply) for(yy in y until y + h) for(xx in x until x + w) {
if (xx + t.shiftX !in 0 until t.width) continue
val v = t.get(xx + t.shiftX, yy)
t.put(xx, yy, (p[8 + v / 2].toInt() ushr (if(v % 2 == 0) 4 else 0)) and 15) }
}
DRAW_OP_ROUNDED_RECT -> {
require(p.size == 12)
val x = signed(p, 0); val y = signed(p, 2); val w = word(p, 4); val h = word(p, 6)
val radius = word(p, 8); val fill = p[10].toInt() and 255; val border = p[11].toInt() and 255
require(w in 1..640 && h in 1..480 && fill <= 15 && border <= DRAW_ROUNDED_RECT_NO_BORDER)
if (apply) for (yy in 0 until h) for (xx in 0 until w) {
if (!roundedContains(xx, yy, w, h, radius)) continue
val tx = x + xx + t.shiftX; val ty = y + yy
if (tx !in 0 until t.width || ty !in 0 until t.height) continue
val edge = !roundedContains(xx - 1, yy - 1, w - 2, h - 2, maxOf(0, radius - 1))
t.put(x + xx, ty, if (edge && border < DRAW_ROUNDED_RECT_NO_BORDER) border else maxOf(fill, t.get(tx, ty)))
}
}
DRAW_OP_DISPLAY_LIST -> { require(p.size == 2); executeList(word(p, 0), t, screen, apply, stack, budget, refs) }
else -> error("Unknown draw opcode")
if (root != DrawProtocol.SCREEN) {
executeList(root, composition, Walk(screen, apply, references, frame))
}
}
require(pos == b.size)
animationPending = frame.animationPending
return references
}
fun execute(sequence: ByteArray, target: Target, screen: Target = target, elapsedMs: Long = 0): Set<Int> {
animationPending = false
val frame = DrawEvaluation(elapsedMs)
val references = mutableSetOf<Int>()
for (apply in listOf(false, true)) {
executeSequence(DrawReader(sequence), target, Walk(screen, apply, references, frame))
}
animationPending = frame.animationPending
return references
}
private fun executeList(id: Int, target: Target, walk: Walk) {
require(walk.ancestors.size < 8 && id !in walk.ancestors)
val bytes = resource(id)
require(bytes.size >= 3 && bytes[0].toInt() == CFW_RESOURCE_TYPE_DISPLAY_LIST)
walk.references.add(id)
walk.ancestors.add(id)
executeSequence(DrawReader(bytes, start = 1), target, walk)
walk.ancestors.removeAt(walk.ancestors.lastIndex)
}
private fun executeSequence(reader: DrawReader, inherited: Target, walk: Walk) {
val count = reader.readU16()
repeat(count) {
val length = reader.readU16()
require(walk.remainingCalls > 0)
walk.remainingCalls--
executeCall(reader.readSlice(length), inherited, walk)
}
reader.requireDone()
}
private fun executeCall(reader: DrawReader, inherited: Target, walk: Walk) {
val opcode = reader.readU8()
val flags = reader.readU8()
require(flags and DRAW_FLAGS_MASK.inv() == 0)
var target = inherited
if (flags and DRAW_FLAG_RESOURCE_TARGET != 0) {
val id = reader.readU16()
walk.references.add(id)
target = target(id).shifted(inherited.shiftX)
}
if (flags and DRAW_FLAG_DEPTH != 0) {
target = target.shifted(DrawProtocol.depthOffset(reader.readS8(), rightLens))
}
when (opcode) {
DRAW_OP_BOUNDING_BOX -> drawBoundingBox(reader, target, walk)
DRAW_OP_RECT_COPY -> drawRectCopy(reader, target, walk)
DRAW_OP_STOCK_FONT_STRING -> drawStockText(reader, target, walk)
DRAW_OP_IMAGE -> drawImage(reader, target, walk)
DRAW_OP_TEXT -> drawText(reader, target, walk)
DRAW_OP_REMAP_COLORS -> remapColors(reader, target, walk)
DRAW_OP_ROUNDED_RECT -> drawRoundedRect(reader, target, walk)
DRAW_OP_DISPLAY_LIST -> {
val id = reader.readU16()
reader.requireDone()
executeList(id, target, walk)
}
else -> error("Unknown draw opcode $opcode")
}
}
private fun drawBoundingBox(reader: DrawReader, target: Target, walk: Walk) {
val flags = reader.readU8()
require(flags and DRAW_BBOX_FLAG_U16.inv() == 0)
val wide = flags and DRAW_BBOX_FLAG_U16 != 0
val x = if (wide) reader.readU16() else reader.readU8() * 4
val y = if (wide) reader.readU16() else reader.readU8() * 2
val width = if (wide) reader.readU16() else reader.readU8() * 4
val height = if (wide) reader.readU16() else reader.readU8() * 2
require(width > 0 && height > 0 && x + width <= target.width && y + height <= target.height)
val rle = ArrayByteReader(reader.readBytes(reader.remaining))
val pixels = decodeRle(rle, width * height, true)
if (walk.apply) {
for (i in pixels.indices) target.put(x + i % width, y + i / width, pixels[i])
}
}
private fun drawRectCopy(reader: DrawReader, target: Target, walk: Walk) {
val id = reader.readU16()
val x = reader.readU16()
val y = reader.readU16()
val width = reader.readU16()
val height = reader.readU16()
val dx = reader.readS16()
val dy = reader.readS16()
reader.requireDone()
val source = when (id) {
DrawProtocol.SCREEN -> walk.screen
DrawProtocol.CURRENT -> target
else -> {
walk.references.add(id)
target(id)
}
}
require(width > 0 && height > 0 && x + width <= source.width && y + height <= source.height)
if (walk.apply) {
// Snapshot the rectangle so overlapping copies have memmove semantics.
val copy = IntArray(width * height) { source.get(x + it % width, y + it / width) }
for (i in copy.indices) target.put(dx + i % width, dy + i / width, copy[i])
}
}
private fun drawStockText(reader: DrawReader, target: Target, walk: Walk) {
var x = reader.readS16()
val y = reader.readS16()
val options = reader.readU8()
val bytes = reader.readBytes(reader.readU8())
reader.requireDone()
val text = bytes.decodeToString(throwOnInvalidSequence = true)
val codePoints = ArrayList<Int>()
var index = 0
while (index < text.length) {
val char = text[index++].code
val codePoint = if (char in 0xd800..0xdbff) {
require(index < text.length)
0x10000 + ((char - 0xd800) shl 10) + text[index++].code - 0xdc00
} else {
char
}
codePoints.add(codePoint)
}
for (i in codePoints.indices) {
val codePoint = codePoints[i]
if (codePoint in 1..31) {
x += codePoint - 11
continue
}
val next = codePoints.drop(i + 1).firstOrNull { it !in 1..31 } ?: 0
val glyph = requireNotNull(builtin) { "Builtin font provider required" }(codePoint, next)
draw(image(glyph.image), target, x + glyph.x, y + glyph.y, options, walk.apply)
x += glyph.advance
}
}
private fun drawImage(reader: DrawReader, target: Target, walk: Walk) {
val id = reader.readU16()
val x = reader.readS16()
val y = reader.readS16()
val options = reader.readU8()
reader.requireDone()
walk.references.add(id)
draw(image(resource(id)), target, x, y, options, walk.apply)
}
private fun drawText(reader: DrawReader, target: Target, walk: Walk) {
val id = reader.readU16()
var x = reader.readS16()
val y = reader.readS16()
val options = reader.readU8()
val text = reader.readBytes(reader.readU8())
reader.requireDone()
walk.references.add(id)
val data = resource(id)
require(data.size >= 193 && data[0].toInt() == CFW_RESOURCE_TYPE_FONT)
for (byte in text) {
val char = byte.toInt() and 255
if (char in 1..31) {
x += char - 11
continue
}
require(char in 32..127)
val offset = DrawProtocol.u16(data, 1 + (char - 32) * 2)
require(offset >= 193)
val glyph = image(data, offset)
draw(glyph, target, x, y, options, walk.apply)
x += glyph.width
}
}
private fun remapColors(reader: DrawReader, target: Target, walk: Walk) {
val x = reader.readU16()
val y = reader.readU16()
val width = reader.readU16()
val height = reader.readU16()
val table = reader.readBytes(8)
reader.requireDone()
require(width > 0 && height > 0 && x + width <= target.width && y + height <= target.height)
if (!walk.apply) return
for (yy in y until y + height) for (xx in x until x + width) {
if (xx + target.shiftX !in 0 until target.width) continue
val value = target.get(xx + target.shiftX, yy)
val shift = if (value % 2 == 0) 4 else 0
target.put(xx, yy, (table[value / 2].toInt() ushr shift) and 15)
}
}
private fun drawRoundedRect(reader: DrawReader, target: Target, walk: Walk) {
val x = ExtendedVarint.evaluate(reader, walk.frame)
val y = ExtendedVarint.evaluate(reader, walk.frame)
val width = reader.readU16()
val height = reader.readU16()
val radius = reader.readU16()
val fill = reader.readU8()
val border = reader.readU8()
reader.requireDone()
require(width in 1..640 && height in 1..480 && fill <= 15 && border <= DRAW_ROUNDED_RECT_NO_BORDER)
if (!walk.apply) return
if (x < -width - target.shiftX || x >= target.width - target.shiftX ||
y < -height || y >= target.height) return
for (yy in 0 until height) for (xx in 0 until width) {
if (!roundedContains(xx, yy, width, height, radius)) continue
val tx = x + xx + target.shiftX
val ty = y + yy
if (tx !in 0 until target.width || ty !in 0 until target.height) continue
val edge = !roundedContains(xx - 1, yy - 1, width - 2, height - 2, maxOf(0, radius - 1))
val value = if (edge && border < DRAW_ROUNDED_RECT_NO_BORDER) border else maxOf(fill, target.get(tx, ty))
target.put(x + xx, ty, value)
}
}
}
@@ -0,0 +1,7 @@
package com.faceclaw.app
/** One-shot scheduler; the owner acquires its rendering lock in [action]. */
internal expect fun scheduleDrawRedraw(delayMs: Int, action: () -> Unit): () -> Unit
private val drawTimeOrigin = kotlin.time.TimeSource.Monotonic.markNow()
internal fun drawAnimationTimeMs(): Long = drawTimeOrigin.elapsedNow().inWholeMilliseconds
@@ -2,28 +2,36 @@ package com.faceclaw.app
/** A retained selection or image, replayed at stereo depth over the screen and shell surfaces. */
class MenuSelection(val x: Int, val y: Int, val width: Int, val height: Int, val radius: Int,
val background: Int, val border: Int, val depth: Int, val packed: ByteArray, val occlusions: List<IntArray> = emptyList(), val kind: Int = 3, val mask: List<IntArray> = emptyList()) {
val background: Int, val border: Int, val depth: Int, val packed: ByteArray, val occlusions: List<IntArray> = emptyList(), val kind: Int = 3, val mask: List<IntArray> = emptyList(), val animation: Animation? = null) {
class Animation(val dx: Int, val dy: Int, val startedAt: Long, val token: Int)
val resource = CachedResource(DrawProtocol.rawImage(width, height, packed))
val fingerprint = "$kind,${mask.joinToString { it.joinToString() }},$x,$y,$width,$height,$radius,$background,$border,$depth,${resource.hash},${occlusions.joinToString { it.joinToString() }}"
fun translated(dx: Int, dy: Int) = MenuSelection(x + dx, y + dy, width, height, radius, background, border, depth, packed, occlusions.map { intArrayOf(it[0] + dx, it[1] + dy, it[2], it[3]) }, kind, mask)
fun calls(id: Int): List<ByteArray> {
val fingerprint = "${animation?.token},${animation?.dx},${animation?.dy},$kind,${mask.joinToString { it.joinToString() }},$x,$y,$width,$height,$radius,$background,$border,$depth,${resource.hash},${occlusions.joinToString { it.joinToString() }}"
fun translated(dx: Int, dy: Int) = MenuSelection(x + dx, y + dy, width, height, radius, background, border, depth, packed, occlusions.map { intArrayOf(it[0] + dx, it[1] + dy, it[2], it[3]) }, kind, mask, animation)
fun calls(id: Int, nowMs: Long = drawAnimationTimeMs()): List<ByteArray> {
val elapsed = animation?.let { (nowMs - it.startedAt).coerceIn(0, 500).toInt() } ?: 500
val highlightX = animation?.let { DrawValue.animate(x + it.dx, x, 500, elapsed) } ?: DrawValue.Integer(x)
val highlightY = animation?.let { DrawValue.animate(y + it.dy, y, 500, elapsed) } ?: DrawValue.Integer(y)
val image = when (kind) {
3 -> listOf(DrawProtocol.roundedRect(x, y, width, height, radius, background, border, depth),
3 -> listOf(DrawProtocol.roundedRect(highlightX, highlightY, width, height, radius, background, border, depth),
DrawProtocol.image(id, x, y, CFW_TEXTURE_OPT_BRIGHTNESS_MASK or CFW_TEXTURE_OPT_TRANSPARENT, depth = depth))
4 -> listOf(DrawProtocol.image(id, x, y, CFW_TEXTURE_OPT_BRIGHTNESS_MASK or CFW_TEXTURE_OPT_TRANSPARENT, depth = depth))
// Gray8 zero is transparent but gray8 one is opaque black. Rect copies preserve
// that distinction after packing, including the transparent corners of app menus.
5 -> mask.map { r -> DrawProtocol.call(DRAW_OP_RECT_COPY, DrawProtocol.word(id) +
r.flatMap { DrawProtocol.word(it).toList() }.toByteArray() +
DrawProtocol.word(x + r[0]) + DrawProtocol.word(y + r[1]), depth = depth) }
5 -> mask.map { rect ->
DrawProtocol.rectCopy(id, rect[0], rect[1], rect[2], rect[3],
x + rect[0], y + rect[1], depth = depth)
}
else -> error("Invalid presentation kind")
}
return image + occlusions.map { r -> DrawProtocol.call(DRAW_OP_RECT_COPY, DrawProtocol.word(DrawProtocol.SCREEN) + r.flatMap { DrawProtocol.word(it).toList() }.toByteArray() + DrawProtocol.word(r[0]) + DrawProtocol.word(r[1])) }
return image + occlusions.map { rect ->
DrawProtocol.rectCopy(DrawProtocol.SCREEN, rect[0], rect[1], rect[2], rect[3], rect[0], rect[1])
}
}
companion object {
/** The caller has consumed the presentation tag (3, 4 or 5). Colors cross the bridge in gray8, pixels in gray8 rows. */
/** The caller has consumed the presentation tag (3–6). Colors cross the bridge in gray8, pixels in gray8 rows. */
fun read(reader: ByteReader, kind: Int = 3): MenuSelection {
require(kind in 3..5)
require(kind in 3..6)
require(reader.remaining() >= 15)
val x = reader.getShort().toInt(); val y = reader.getShort().toInt()
val w = reader.getShort().toInt() and 65535; val h = reader.getShort().toInt() and 65535
@@ -34,6 +42,14 @@ class MenuSelection(val x: Int, val y: Int, val width: Int, val height: Int, val
val count = reader.getShort().toInt() and 65535
require(count <= 2048)
require(w in 1..640 && h in 1..480 && 5 + (w + 1) / 2 * h <= 65536 && reader.remaining() >= w * h)
val animation = if (kind == 6) {
require(reader.remaining() >= 10)
val dx = reader.getShort().toInt()
val dy = reader.getShort().toInt()
val elapsed = reader.getShort().toInt() and 65535
require(elapsed <= 500)
Animation(dx, dy, drawAnimationTimeMs() - elapsed, reader.getInt())
} else null
val gray = ByteArray(w * h); reader.get(gray)
require(reader.remaining() >= count * 8)
val occlusions = List(count) { IntArray(4) { reader.getShort().toInt() and 65535 } }
@@ -50,7 +66,7 @@ class MenuSelection(val x: Int, val y: Int, val width: Int, val height: Int, val
}
}
}
return MenuSelection(x, y, w, h, radius, background, border, depth, BmpUtil.pack4bppFromGray8(gray, w, h), occlusions, kind, mask)
return MenuSelection(x, y, w, h, radius, background, border, depth, BmpUtil.pack4bppFromGray8(gray, w, h), occlusions, if (kind == 6) 3 else kind, mask, animation)
}
}
}
@@ -7,11 +7,12 @@ class ShellScene(val layers: List<Layer>, val selections: List<MenuSelection> =
val fingerprint: String = layers.joinToString(";") { "${it.key},${it.x},${it.y},${it.width},${it.height},${it.dim},${it.depth},${CachedResource(it.packed).hash},${it.selections.joinToString { row -> row.fingerprint }}" } + selections.joinToString { it.fingerprint }
fun calls(width: Int, height: Int, surfaces: IntArray, selected: IntArray): List<ByteArray> {
val calls = ArrayList<ByteArray>(); var rowId = 0
for (row in selections) calls.addAll(row.calls(selected[rowId++]))
val now = drawAnimationTimeMs()
for (row in selections) calls.addAll(row.calls(selected[rowId++], now))
for ((index, layer) in layers.withIndex()) {
if (layer.dim < 256) calls.add(DrawProtocol.lut(width, height, layer.dim))
calls.add(DrawProtocol.image(surfaces[index], layer.x, layer.y, depth = layer.depth))
for (row in layer.selections) calls.addAll(row.calls(selected[rowId++]))
for (row in layer.selections) calls.addAll(row.calls(selected[rowId++], now))
}
return calls
}
@@ -27,6 +28,36 @@ class ShellScene(val layers: List<Layer>, val selections: List<MenuSelection> =
val target = DisplayListRenderer.Target(output, width, height)
return ByteArray(width * height) { (target.get(it % width, it / width) * 16).toByte() }
}
internal class AnimatedPreview(val player: DisplayListPlayer, var pixels: ByteArray)
internal fun animatedPreview(
screenGray: ByteArray, width: Int, height: Int,
schedule: (Int, () -> Unit) -> (() -> Unit),
): AnimatedPreview {
val screen = BmpUtil.pack4bppFromGray8(screenGray, width, height)
val output = ByteArray(screen.size)
val resources = HashMap<Int, ByteArray>()
val surfaces = IntArray(layers.size) { id ->
val layer = layers[id]
resources[id] = DrawProtocol.rawImage(layer.width, layer.height, layer.packed)
id
}
val rows = IntArray(allSelections.size) { id ->
resources[id + layers.size] = allSelections[id].resource.bytes
id + layers.size
}
resources[511] = DrawProtocol.displayList(calls(width, height, surfaces, rows))
val target = DisplayListRenderer.Target(output, width, height)
lateinit var preview: AnimatedPreview
val player = DisplayListPlayer(DisplayListRenderer(resources),
DisplayListRenderer.Target(screen, width, height), target,
clock = { drawAnimationTimeMs() }, schedule = schedule,
displayed = { preview.pixels = ByteArray(width * height) { (target.get(it % width, it / width) * 16).toByte() } })
preview = AnimatedPreview(player, screenGray)
player.present(511)
return preview
}
companion object {
val EMPTY = ShellScene(emptyList())
fun decode(reader: ByteReader): ShellScene {
@@ -51,7 +51,7 @@ class SurfaceCompositor @JvmOverloads constructor(private val includePreviewInFr
var out: MutableList<ScreenDraw> = ArrayList()
while ((cursor.remaining() >= 1)) {
var kind: Int = (cursor.get() and 0xff)
if (kind in 3..5) { out.add(ScreenDraw.image(0, 0, 0).also { it.selection = MenuSelection.read(cursor, kind) }); continue }
if (kind in 3..6) { out.add(ScreenDraw.image(0, 0, 0).also { it.selection = MenuSelection.read(cursor, kind) }); continue }
if (((kind == ScreenDraw.KIND_GLYPH) && (cursor.remaining() >= 11))) {
var fontId: Int = (cursor.getShort().toInt() and 0xffff)
var encoding: Int = cursor.getInt()
@@ -287,6 +287,18 @@ class SurfaceCompositor @JvmOverloads constructor(private val includePreviewInFr
}
private val lock = protocolPlatform().createLock()
private var animatedPreview: ShellScene.AnimatedPreview? = null
private var previewKey: String? = null
private fun previewScene(scene: ShellScene, gray: ByteArray, key: String): ByteArray {
if (previewKey != key) {
animatedPreview?.player?.stop()
animatedPreview = scene.animatedPreview(gray, screenWidth, screenHeight,
schedule = { delay, action -> scheduleDrawRedraw(delay) { lock.withLock(action) } })
previewKey = key
}
return animatedPreview!!.pixels
}
private var screenWidth: Int = 0
@@ -602,6 +614,9 @@ class SurfaceCompositor @JvmOverloads constructor(private val includePreviewInFr
private fun compositeLocked(includePreview: Boolean = includePreviewInFrames): Composite {
var gray: ByteArray = ByteArray((screenWidth * screenHeight))
if (blanked) {
animatedPreview?.player?.stop()
animatedPreview = null
previewKey = null
return Composite(
gray,
screenWidth,
@@ -667,8 +682,18 @@ class SurfaceCompositor @JvmOverloads constructor(private val includePreviewInFr
}
}
val scene = if (surfaces.values.any { it.visible && it.zOrder > 1 }) ShellScene.EMPTY else ShellScene(shellScene?.layers ?: emptyList(), selections)
val preview = if (includePreview && (shellScene != null || selections.isNotEmpty())) scene.preview(gray, screenWidth, screenHeight) else gray
return Composite(preview, screenWidth, screenHeight, fingerprint.append("|shell:").append(scene.fingerprint).toString(),
val sceneKey = fingerprint.append("|shell:").append(scene.fingerprint).toString()
val preview = if (includePreview && (shellScene != null || selections.isNotEmpty())) {
previewScene(scene, gray, sceneKey)
} else {
if (includePreview) {
animatedPreview?.player?.stop()
animatedPreview = null
previewKey = null
}
gray
}
return Composite(preview, screenWidth, screenHeight, sceneKey,
nextCompositeSeq++, draws.toTypedArray()).also { it.screenGray = gray; it.shellScene = scene }
}
@@ -1,5 +1,7 @@
package com.faceclaw.app
import kotlin.jvm.JvmOverloads
/** Sequential little-endian input. Platform adapters can read native buffers without a copy. */
abstract class ByteReader {
abstract fun remaining(): Int
@@ -23,14 +25,28 @@ abstract class ByteReader {
fun getInt(): Int = (getShort().toInt() and 65535) or (getShort().toInt() shl 16)
}
class ArrayByteReader(private val bytes: ByteArray) : ByteReader() {
private var position = 0
class ArrayByteReader @JvmOverloads constructor(
private val bytes: ByteArray,
private val start: Int = 0,
private val end: Int = bytes.size,
) : ByteReader() {
private var position = start
override fun remaining(): Int = bytes.size - position
init {
require(start >= 0 && start <= end && end <= bytes.size)
}
override fun get(): Byte = bytes[position++]
override fun remaining(): Int = end - position
override fun get(index: Int): Byte = bytes[index]
override fun get(): Byte {
require(remaining() > 0)
return bytes[position++]
}
override fun get(index: Int): Byte {
require(index >= 0 && index < end - start)
return bytes[start + index]
}
override fun get(bytes: ByteArray, offset: Int, length: Int): ByteReader {
require(
@@ -0,0 +1,15 @@
package com.faceclaw.app
import platform.darwin.DISPATCH_TIME_NOW
import platform.darwin.dispatch_after
import platform.darwin.dispatch_get_main_queue
import platform.darwin.dispatch_time
internal actual fun scheduleDrawRedraw(delayMs: Int, action: () -> Unit): () -> Unit {
val lock = protocolPlatform().createLock()
var cancelled = false
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delayMs.toLong() * 1_000_000L), dispatch_get_main_queue()) {
if (lock.withLock { !cancelled }) action()
}
return { lock.withLock { cancelled = true } }
}
+1
View File
@@ -27,6 +27,7 @@ const layers = load('app/ui/layers.ts', {
'../native/frame-timings': { spanCurrent: (_name, paint) => paint() },
});
const menu = load('app/ui/menu.ts', {
'./menu-highlight-motion': require('../.test-build/app/ui/menu-highlight-motion.js'),
'../graphics/image': graphics, '../graphics/ui-fonts': fonts, '../graphics/textwrap': textwrap,
'../util/numeric-util': numeric, './gestures': gestures, './metrics': load('app/ui/metrics.ts'),
});
@@ -75,7 +75,7 @@ class DisplayListTest {
@Test fun roundedRectsAndNestedOddNegativeDepthMatchFirmwareOnBothLenses() {
val resources = mapOf(
40 to hex("0404000200f00f0ff0"),
41 to hex("0202000f00080202010001000c0008000300040a0a000402022800050004001f"),
41 to hex("0202000d0008020201010c0008000300040a0a000402022800050004001f"),
42 to hex("02010005000702fd2900"))
val screen = hex("012345601234560112345601234560122345601234560123345601234560123445601234560123455601234f60123456601234560123456001234560123456011234560123456012234560123456012334560123456012344560123456012345")
val expected = listOf("01234560123456011aaaaaaaaaa56012aa45644444aa0123a4564444456a1234a564f44f564a2345a6444fff644a3456a4444456444a4560aa44456444aa56011aaaaaaaaaa56012234560123456012334560123456012344560123456012345", "012345601234560112aaaaaaaaaa60122aa56444445aa1233a5644444564a2344a644f44f644a3455a4444ff6444a4566a4444564444a5600aa44564444aa60112aaaaaaaaaa6012234560123456012334560123456012344560123456012345")
@@ -0,0 +1,140 @@
package com.faceclaw.app
import kotlin.test.*
class DrawExpressionTest {
private fun hex(s: String) = s.chunked(2).map { it.toInt(16).toByte() }.toByteArray()
private fun evaluate(code: ByteArray, elapsed: Long = 0): Pair<Int, Boolean> {
val frame = DrawEvaluation(elapsed)
return DrawExpression.evaluate(DrawReader(code), frame) to frame.animationPending
}
@Test fun integerWidthsSignExtensionAndReservedPrefixes() {
val signed = mapOf(
0 to "00", 63 to "3f", -64 to "40", -1 to "7f", 64 to "8040", -65 to "bfbf",
8191 to "9fff", -8192 to "a000", 8192 to "c02000", -8193 to "dfdfff",
1048575 to "cfffff", -1048576 to "d00000", 1048576 to "e0100000",
-1048577 to "efefffff", 134217727 to "e7ffffff", -134217728 to "e8000000",
134217728 to "f008000000", Int.MAX_VALUE to "f07fffffff", Int.MIN_VALUE to "f080000000",
)
for ((number, bytes) in signed) {
assertContentEquals(hex(bytes), ExtendedVarint.signed(number))
assertEquals(number, ExtendedVarint.read(DrawReader(hex(bytes)), true))
}
for (value in listOf(0u, 127u, 128u, 16383u, 16384u, 2097151u, 2097152u, 268435455u, 268435456u, UInt.MAX_VALUE)) {
val bytes = ExtendedVarint.unsigned(value)
assertEquals(value, ExtendedVarint.read(DrawReader(bytes), false).toUInt())
for (n in 0 until bytes.size) assertFails { ExtendedVarint.read(DrawReader(bytes.copyOf(n)), false) }
}
for (prefix in 0xf1..0xff) assertFails { ExtendedVarint.read(DrawReader(byteArrayOf(prefix.toByte())), true) }
assertEquals(-1, ExtendedVarint.read(DrawReader(hex("bfff")), true)) // valid non-minimal form
assertFails { ExtendedVarint.evaluate(DrawReader(hex("ff05")), DrawEvaluation(0)) }
}
@Test fun arithmeticTypesStackErrorsAndTimeTracking() {
val vectors = listOf(
"0107010510" to 12, "0107010511" to 2, "0107010512" to 35,
"0179010213" to -3, "0179010214" to -1, "017915" to 7,
"0107010516" to 5, "0107010517" to 7, "01010109" to 9,
"01040310" to 8, "010401070504" to 7,
"010430010230202431" to -6,
"020000c03f02000000402231" to 3,
"01003001806430020000003f4031" to 50,
"01f07fffffff010110" to Int.MIN_VALUE,
"01f080000000017f13" to Int.MIN_VALUE,
"01f080000000017f14" to 0,
)
for ((code, expected) in vectors) assertEquals(expected to false, evaluate(hex(code)), code)
for (bad in listOf("", "10", "0101010013", "010130010110", "020000807f", "ff", "0180", "01013210")) {
assertEquals(0 to false, evaluate(hex(bad)), bad)
}
assertEquals(0 to false, evaluate(ByteArray(66) { if (it % 2 == 0) 1 else 0 })) // stack overflow
assertEquals(0 to false, evaluate(ByteArray(1025)))
for (op in DrawExpression.SMOOTHSTEP..DrawExpression.EASE_IN_OUT_CUBIC) {
assertEquals(0 to false, evaluate(hex("0200000000") + byteArrayOf(op.toByte(), 49)))
assertEquals(1 to false, evaluate(hex("020000803f") + byteArrayOf(op.toByte(), 49)))
}
assertEquals(499 to true, evaluate(hex("0181f432"), 499))
assertEquals(500 to false, evaluate(hex("0181f432"), 500))
assertEquals(500 to false, evaluate(hex("0181f432"), 900))
assertEquals(0 to false, evaluate(hex("0181f432ff"), 250)) // errors discard pending time
}
@Test fun smoothstepCoordinatesMatchFirmwareAndSurvivePresentTimeReset() {
val value = DrawValue.animate(10, 110) as DrawValue.Expression
val code = "010a3001806e300181f432010010300181f43023414031"
assertContentEquals(hex(code), value.program)
for ((time, expected) in listOf(0L to 10, 125L to 25, 250L to 60, 375L to 94, 500L to 110, 900L to 110)) {
assertEquals(expected to (time < 500), evaluate(value.program, time))
}
val continued = DrawValue.animate(10, 110, elapsedMs = 250) as DrawValue.Expression
assertEquals(60 to true, evaluate(continued.program, 0))
assertEquals(110 to false, evaluate(continued.program, 250))
}
@Test fun animatedMenuBridgePreservesElapsedTimeAndTranslation() {
// Tag 6 record emitted by the TypeScript bridge: elapsed=200, delta=(-2,-4).
val wire = hex("06030002000200020001001030020000fefffcffc8002a000000ffffffff")
val input = ArrayByteReader(wire)
assertEquals(6, input.get().toInt())
val row = MenuSelection.read(input, 6).translated(10, 10)
assertEquals(0, input.remaining())
val motion = assertNotNull(row.animation)
assertEquals(42, motion.token)
assertEquals(-2, motion.dx)
assertEquals(-4, motion.dy)
val call = row.calls(7, motion.startedAt + 200).first()
for ((elapsed, expected) in listOf(0L to (11 to 9), 300L to (13 to 12))) {
val reader = DrawReader(call)
assertEquals(8, reader.readU8())
assertEquals(DRAW_FLAG_DEPTH, reader.readU8())
assertEquals(2, reader.readS8())
val frame = DrawEvaluation(elapsed)
assertEquals(expected.first, ExtendedVarint.evaluate(reader, frame))
assertEquals(expected.second, ExtendedVarint.evaluate(reader, frame))
assertEquals(elapsed < 300, frame.animationPending)
}
for (end in 1 until wire.size) assertFails {
MenuSelection.read(ArrayByteReader(wire.copyOf(end), 1), 6)
}
}
@Test fun playerSchedulesOnlyUnfinishedFramesAndCancelsStaleCallbacks() {
val call = DrawProtocol.roundedRect(DrawValue.Integer(0), DrawValue.animate(0, 4), 2, 2, 0, 15)
val renderer = DisplayListRenderer(mapOf(1 to DrawProtocol.displayList(listOf(call))))
val screen = DisplayListRenderer.Target(ByteArray(16), 4, 8)
val output = DisplayListRenderer.Target(ByteArray(16), 4, 8)
var now = 1000L
val queued = mutableListOf<() -> Unit>()
var cancelled = 0
var displayed = 0
val player = DisplayListPlayer(renderer, screen, output, { now }, { delay, callback ->
assertEquals(45, delay)
queued.add(callback);
{ cancelled++ }
}, { displayed++ })
player.present(1)
assertEquals(15, output.get(0, 0))
val first = queued.removeAt(0)
now += 250
first()
assertEquals(15, output.get(0, 2))
assertEquals(0, output.get(0, 0))
val stale = queued.removeAt(0)
player.present(1)
assertEquals(15, output.get(0, 0))
stale()
assertEquals(3, displayed)
now += 500
queued.removeAt(0)()
assertEquals(15, output.get(0, 4))
assertFalse(renderer.animationPending)
assertTrue(queued.isEmpty())
player.present(1)
val stopped = queued.removeAt(0)
player.stop()
stopped()
assertEquals(5, displayed)
assertTrue(cancelled >= 2)
}
}
@@ -0,0 +1,128 @@
package com.faceclaw.app
import kotlin.test.*
class DrawWireTest {
private fun hex(value: String) = value.chunked(2).map { it.toInt(16).toByte() }.toByteArray()
@Test fun encodersUseRevision27CoordinatesAndPreserveOtherWireBytes() {
// Literal vectors pin the firmware grammar independently of the reader.
val calls = listOf(
DrawProtocol.image(0x1234, -32768, 32767, 0x1f, target = 511, depth = -128) to
"0403ff018034120080ff7f1f",
DrawProtocol.rectCopy(DrawProtocol.CURRENT, 1, 2, 3, 4, -5, -6) to
"0200feff0100020003000400fbfffaff",
DrawProtocol.stockText(-1, 2, 15, hex("41c3a9")) to "0300ffff02000f0341c3a9",
DrawProtocol.text(511, -2, 3, 31, hex("01417f")) to "0500ff01feff03001f0301417f",
DrawProtocol.playList(511, depth = 127) to "07027fff01",
DrawProtocol.roundedRect(-1, 2, 640, 480, 65535, 15, 16) to
"08007f028002e001ffff0f10",
DrawProtocol.lut(640, 480, 256) to "0600000000008002e0010123456789abcdef",
DrawProtocol.bbox(hex("ffffffffffffffff"), 4, 0, 0, 8, 2) to "010000000002010f10",
DrawProtocol.bbox(hex("0f"), 1, 1, 0, 1, 1) to "01000101000000010001001f",
)
for ((call, expected) in calls) assertContentEquals(hex(expected), call)
assertContentEquals(hex("0100050007027fff01"), DrawProtocol.sequence(listOf(calls[4].first)))
assertContentEquals(hex("1a0100050007027fff01"), DrawProtocol.message(listOf(calls[4].first)))
assertContentEquals(hex("020100050007027fff01"), DrawProtocol.displayList(listOf(calls[4].first)))
assertContentEquals(hex("1bffff"), DrawProtocol.root(DrawProtocol.SCREEN))
}
@Test fun optimizerFieldsAreReencodedAndOpaqueBytesArePreserved() {
val records = listOf(
"1300020080ff7f1f" to "040000020080ff7f1f",
"14ff01ffff02000f0341017f" to "0500ff01ffff02000f0341017f",
"0fffff02001f0341c3a9" to "0300ffff02001f0341c3a9",
"030001020134120f10" to "010000000102010f10",
"060f10" to "01000100000000080002000f10",
"090100020003000400fbfffaff" to "0200feff0100020003000400fbfffaff",
)
for ((record, expected) in records) {
val calls = DrawProtocol.fromOptimized(hex(record), 8, 2)
assertEquals(1, calls.size)
assertContentEquals(hex(expected), calls.single(), record)
}
val nested = hex("0802") + records.take(2).fold(byteArrayOf()) { bytes, (record, _) ->
val payload = hex(record)
bytes + DrawProtocol.word(payload.size) + payload
}
val calls = DrawProtocol.fromOptimized(nested)
assertEquals(2, calls.size)
for (i in calls.indices) assertContentEquals(hex(records[i].second), calls[i])
assertFails { DrawProtocol.fromOptimized(hex("1300020080ff7f")) }
assertFails { DrawProtocol.fromOptimized(hex("1300020080ff7f1f00")) }
assertFails { DrawProtocol.fromOptimized(hex("0fffff02001f0441c3a9")) }
}
@Test fun everyTruncatedOrOverlongCallIsRejectedBeforeMutation() {
val font = ByteArray(197)
font[0] = 1
font[67] = 193.toByte() // Glyph A.
hex("0801011f").copyInto(font, 193)
val resources = mapOf(
1 to hex("0401000100f0"),
2 to font,
3 to DrawProtocol.displayList(emptyList()),
)
val renderer = DisplayListRenderer(resources, builtin = { _, _ ->
DisplayListRenderer.BuiltinGlyph(hex("0801011f"), 1)
})
val calls = listOf(
DrawProtocol.image(1, 0, 0, target = 1, depth = -1),
DrawProtocol.rectCopy(DrawProtocol.CURRENT, 0, 0, 2, 2, 1, 1),
DrawProtocol.stockText(0, 0, 15, hex("41")),
DrawProtocol.text(2, 0, 0, 15, hex("41")),
DrawProtocol.playList(3),
DrawProtocol.roundedRect(0, 0, 4, 4, 1, 15),
DrawProtocol.lut(8, 4, 128),
DrawProtocol.bbox(ByteArray(16) { -1 }, 4, 0, 0, 8, 4),
)
val screen = ByteArray(16) { 0x12 }
val target = DisplayListRenderer.Target(screen, 8, 4)
val valid = DrawProtocol.roundedRect(0, 0, 4, 4, 0, 15)
for (call in calls) {
// Establish that each fixture is valid before testing malformed forms.
renderer.execute(DrawProtocol.sequence(listOf(call)), target)
screen.fill(0x12)
val savedResources = resources.mapValues { it.value.copyOf() }
val malformed = (0 until call.size).map { call.copyOf(it) } + listOf(call + byteArrayOf(0))
for (bad in malformed) {
assertFails { renderer.execute(DrawProtocol.sequence(listOf(valid, bad, valid)), target) }
assertContentEquals(ByteArray(16) { 0x12 }, screen)
for ((id, bytes) in savedResources) assertContentEquals(bytes, resources[id])
}
}
val sequence = DrawProtocol.sequence(listOf(valid))
for (length in 0 until sequence.size) {
assertFails { renderer.execute(sequence.copyOf(length), target) }
assertContentEquals(ByteArray(16) { 0x12 }, screen)
}
assertFails { renderer.execute(sequence + byteArrayOf(0), target) }
assertContentEquals(ByteArray(16) { 0x12 }, screen)
}
@Test fun batchesRespectEncodedLengthsAndCallBudget() {
val call = DrawProtocol.playList(511)
val messages = DrawProtocol.messages(List(3) { call }, maxBytes = 15)
assertEquals(listOf(15, 9), messages.map { it.size })
assertContentEquals(hex("1a020004000700ff0104000700ff01"), messages[0])
assertFails { DrawProtocol.messages(listOf(call), maxBytes = 8) }
val many = DrawProtocol.messages(List(4097) { call })
assertEquals(listOf(4096, 1), many.map { DrawProtocol.u16(it, 1) })
assertTrue(DrawProtocol.messages(emptyList()).isEmpty())
}
@Test fun resourceReaderViewsUseRelativeOffsetsAndCannotEscapeTheirBounds() {
val reader = ArrayByteReader(hex("ff123456ee"), 1, 4)
assertEquals(0x12.toByte(), reader.get(0))
assertEquals(0x3412.toShort(), reader.getShort())
assertEquals(1, reader.remaining())
assertFails { reader.get(ByteArray(2)) }
assertEquals(0x56.toByte(), reader.get())
assertEquals(0, reader.remaining())
assertFails { reader.get() }
assertFails { reader.get(3) }
assertFails { reader.get(-1) }
assertFails { ArrayByteReader(byteArrayOf(1), 1, 2) }
}
}
@@ -0,0 +1,36 @@
package com.faceclaw.app
import kotlin.test.Test
import kotlin.test.assertTrue
import platform.Foundation.NSDate
import platform.Foundation.NSDefaultRunLoopMode
import platform.Foundation.NSRunLoop
import platform.Foundation.NSThread
import platform.Foundation.dateWithTimeIntervalSinceNow
import platform.Foundation.runMode
class IosBleCentralTest {
/** Registering a state listener must create the central and report a state without any scan. */
@Test
fun scanListenerAloneReceivesABluetoothStateReport() {
val central = IosBleCentral(IosProtocolPlatform)
val states = ArrayList<Int>()
var offMain = 0
central.setScanListener(object : IosBleScanListener {
override fun onBluetoothState(state: Int, authorization: Int) {
if (!NSThread.isMainThread) offMain++
states.add(state)
}
override fun onAdvertisement(identifier: String, name: String, manufacturerDataHex: String, rssi: Int, connectable: Boolean) {}
})
val deadline = IosProtocolPlatform.elapsedRealtimeMs() + 5_000
while (IosProtocolPlatform.elapsedRealtimeMs() < deadline && states.none { it != 0 }) {
NSRunLoop.mainRunLoop.runMode(NSDefaultRunLoopMode, NSDate.dateWithTimeIntervalSinceNow(0.02))
}
// The simulator reports unsupported/powered-off; a real phone powered-on. Any non-unknown state proves the central exists.
assertTrue(states.any { it != 0 }, "no CBManagerState reported: $states")
assertTrue(offMain == 0, "state reports must arrive on the main queue")
central.close()
}
}
+9 -8
View File
@@ -18,7 +18,8 @@ const font={lineHeight:12,ascent:10,descent:2,measureText:text=>text.length*4,
const {MenuLayer}=load('app/ui/menu.ts',{
'../graphics/image':graphics,'../graphics/textwrap':{},'../graphics/ui-fonts':{getDefaultSmallFont:()=>font},
'../util/numeric-util':{clamp:(n,a,b)=>Math.max(a,Math.min(b,n))},'./gestures':{},
'./metrics':require('../.test-build/app/ui/metrics.js'),
'./menu-highlight-motion': require('../.test-build/app/ui/menu-highlight-motion.js'),
'./metrics':require('../.test-build/app/ui/metrics.js'),
});
const {prepareFrameDraws}=load('app/graphics/glyph-wire.ts',{'./presentation-wire':wire,'../native/texture-atlas':{textureAtlasAvailable:()=>false}});
function menu() {
@@ -26,9 +27,9 @@ function menu() {
const context={stack:{getBaseSize:()=>({width:80,height:80}),isFocused:()=>true}};
return {layer,paint:()=>layer.paint(context,()=>new graphics.GrayImage(80,80)),context};
}
test('menu retains the selected text and rounded highlight at depth +2',async()=>{
test('menu retains the selected text and rounded highlight on the menu plane',async()=>{
const m=menu(),image=m.paint(),row=image.draws.find(d=>d.presentation);
assert.equal(row.presentation.depth,2);assert.equal(row.presentation.radius,8);
assert.equal(row.presentation.depth,0);assert.equal(row.presentation.radius,8);
assert.ok(row.source.pixels.some(v=>v===255));assert.ok(!image.withDrawsBaked(false).pixels.some(v=>v===255));
const flat=planes.flattenPlanesWithDraws([{image,x:0,y:0}]);
const records=wire.presentationRecords(prepareFrameDraws(flat.draws));assert.equal(records.length,1);
@@ -37,7 +38,7 @@ test('menu retains the selected text and rounded highlight at depth +2',async()=
const screen=flat.image.pixels.slice(),preview=c.composite();
assert.ok(preview.some(v=>v===240));assert.deepEqual(flat.image.pixels,screen);
await m.layer.handleInput({type:'scroll-down'},m.context);
const next=m.paint().draws.find(d=>d.presentation);assert.ok(next.y>row.y);assert.equal(next.presentation.depth,2);
const next=m.paint().draws.find(d=>d.presentation);assert.ok(next.y>row.y);assert.equal(next.presentation.depth,0);
});
test('shell menu selection survives serialization and is not baked into its surface',()=>{
const image=menu().paint(),bytes=encodeShellScene([{image,x:0,y:0,shellKey:7}]);
@@ -58,14 +59,14 @@ test('later opaque planes occlude a replayed selection without erasing its visib
const {LayerStack,noopLayerActions}=load('app/ui/layers.ts',{
'../graphics/image':graphics,'../graphics/plane':planes,'../native/frame-timings':{spanCurrent:(_name,fn)=>fn()},'./gestures':{},
});
test('context menu surface uses +4 and selected row +6 without changing the app screen',()=>{
test('context menu surface and selected row both use +4 without changing the app screen',()=>{
const base=new graphics.GrayImage(100,90,96);
const stack=new LayerStack({paint:()=>base},noopLayerActions,{width:100,height:90});
stack.push(new MenuLayer(null,[{label:'FIRST',onSelect(){}}],{x:10,y:8,width:80,minHeight:70,maxHeight:70,depth:4}));
const frame=planes.flattenPlanesWithDraws(stack.paint());
assert.deepEqual(frame.image.pixels,base.pixels);
const records=wire.presentationRecords(prepareFrameDraws(frame.draws));
assert.deepEqual(records.map(r=>r.depth),[4,6]);assert.equal(records[0].mode,'masked-image');
assert.deepEqual(records.map(r=>r.depth),[4,4]);assert.equal(records[0].mode,'masked-image');
const c=new SurfaceCompositor(100,90);c.configureSurface('app',{x:0,y:0,width:100,height:90,zOrder:0,transparency:'opaque'});
c.submitSurfaceFrame('app',frame.image.pixels,{x:0,y:0,width:100,height:90},prepareFrameDraws(frame.draws));
const preview=c.composite();
@@ -77,11 +78,11 @@ test('context menu surface uses +4 and selected row +6 without changing the app
c.submitSurfaceFrame('app',closed.image.pixels,{x:0,y:0,width:100,height:90},prepareFrameDraws(closed.draws));
assert.deepEqual(c.composite(),base.pixels);
});
test('system menu bridge shifts its surface +4 and selected row +6',()=>{
test('system menu bridge shifts its surface and selected row together by +4',()=>{
const image=menu().paint(),bytes=encodeShellScene([{image,x:8,y:0,shellKey:7,depth:4}]);
const view=new DataView(bytes.buffer),w=view.getUint16(8,true),h=view.getUint16(10,true);
assert.equal(view.getInt16(16,true),4);
assert.equal(wire.readPresentation(bytes,18+w*h).selection.depth,6);
assert.equal(wire.readPresentation(bytes,18+w*h).selection.depth,4);
const c=new SurfaceCompositor(100,80);c.setShellScene(bytes);const preview=c.composite();
assert.equal(preview[40*100+8],0);assert.equal(preview[40*100+10],80);
});
+44
View File
@@ -0,0 +1,44 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const { MenuHighlightMotion } = require('../.test-build/app/ui/menu-highlight-motion.js');
const { GrayImage } = require('../.test-build/app/graphics/image.js');
const { encodePresentation, readPresentation } = require('../.test-build/app/graphics/presentation-wire.js');
test('highlight animates only navigation that preserves the scroll window', () => {
const motion = new MenuHighlightMotion();
assert.equal(motion.paint(0, 0, 10, 20, 100, 20, 0), undefined);
motion.navigate(0, false);
const moved = motion.paint(1, 0, 10, 40, 100, 20, 100);
assert.equal(moved.dx, 0);
assert.equal(moved.dy, -20);
assert.equal(motion.paint(1, 0, 10, 40, 100, 20, 350).token, moved.token);
motion.navigate(1, false);
const next = motion.paint(2, 0, 10, 60, 100, 20, 350);
assert.equal(next.dy, -30, 'continue from the halfway position');
motion.navigate(2, false);
assert.equal(motion.paint(3, 1, 10, 60, 100, 20, 400), undefined, 'scroll snaps');
motion.navigate(3, true);
assert.equal(motion.paint(0, 0, 10, 20, 100, 20, 500), undefined, 'wrap snaps');
motion.navigate(0, false);
motion.paint(1, 0, 10, 40, 100, 20, 600);
assert.equal(motion.paint(1, 0, 10, 40, 100, 20, 1100), undefined, 'finished after 500 ms');
assert.equal(motion.paint(2, 0, 10, 60, 100, 20, 1200), undefined, 'programmatic changes snap');
});
test('animated selection bridge carries relative start, elapsed time and stable token', () => {
const now = Date.now;
Date.now = () => 1200;
try {
const image = new GrayImage(8, 4);
image.drawMenuSelection(new GrayImage(2, 2, 255), 3, 2, 16, 48, 1, 2,
{ dx: -2, dy: -4, startedAt: 1000, token: 42 });
const bytes = encodePresentation(image.draws[0]);
assert.equal(bytes[0], 6);
assert.equal(bytes.length, 30);
const record = readPresentation(bytes, 0);
assert.equal(record.end, bytes.length);
assert.deepEqual(record.selection.animation, { dx: -2, dy: -4, startedAt: 1000, token: 42 });
assert.equal(record.selection.x, 3);
assert.equal(record.selection.y, 2);
} finally { Date.now = now; }
});
+1
View File
@@ -153,6 +153,7 @@ test('Music settings keeps toggles and selection when a new app arrives and supp
'../graphics/textwrap': textwrap,
'../graphics/ui-fonts': { getDefaultSmallFont: () => font },
'../util/numeric-util': { clamp: (n, lo, hi) => Math.max(lo, Math.min(hi, n)) },
'./menu-highlight-motion': require('../.test-build/app/ui/menu-highlight-motion.js'),
'./metrics': load('app/ui/metrics.ts'), './gestures': {},
};
const menu = load('app/ui/menu.ts', (name) => deps[name]);
+1
View File
@@ -84,6 +84,7 @@ function ui(fontSize = 12) {
'../native/notification-sources': prefs,
'../native/notification-access': { isNotificationListenerEnabled: () => true },
'../util/render-freshness': { renderPassAllowsStaleData: () => false },
'./menu-highlight-motion': require('../.test-build/app/ui/menu-highlight-motion.js'),
'./metrics': load('app/ui/metrics.ts'),
'./gestures': {},
};