@native-sdk/cli 0.5.3 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (172) hide show
  1. package/build/app.zig +91 -2
  2. package/build.zig +756 -5
  3. package/build.zig.zon +7 -0
  4. package/native-sdk.d.ts +19 -1
  5. package/package.json +9 -9
  6. package/packages/core/package-lock.json +2 -2
  7. package/packages/core/package.json +1 -1
  8. package/packages/core/rt/rt.zig +416 -9
  9. package/packages/core/sdk/core.ts +706 -5
  10. package/packages/core/sdk/events.ts +17 -8
  11. package/packages/core/src/checker.ts +305 -2
  12. package/packages/core/src/diagnostics.ts +12 -0
  13. package/packages/core/src/emitter.ts +3187 -264
  14. package/packages/core/src/typed_ast.ts +51 -4
  15. package/packages/core/src/types.ts +34 -1
  16. package/skill-data/native-ui/SKILL.md +42 -4
  17. package/skill-data/ts-core/SKILL.md +13 -3
  18. package/src/app_runner/root.zig +112 -10
  19. package/src/app_runner/ts_core_main.zig +4 -0
  20. package/src/automation/layout_fingerprint.zig +160 -0
  21. package/src/automation/protocol.zig +82 -33
  22. package/src/automation/root.zig +1 -0
  23. package/src/automation/server.zig +1 -1
  24. package/src/automation/snapshot.zig +68 -9
  25. package/src/automation/watcher.zig +1 -1
  26. package/src/embed/host.zig +5 -0
  27. package/src/embed/tests.zig +119 -1
  28. package/src/embed/ui_host.zig +13 -0
  29. package/src/platform/linux/gtk_host.c +639 -16
  30. package/src/platform/linux/gtk_host.h +25 -0
  31. package/src/platform/linux/root.zig +351 -7
  32. package/src/platform/macos/appkit_host.h +152 -8
  33. package/src/platform/macos/appkit_host.m +1816 -100
  34. package/src/platform/macos/cef_host.mm +372 -11
  35. package/src/platform/macos/root.zig +379 -11
  36. package/src/platform/null_platform.zig +696 -9
  37. package/src/platform/null_platform_tests.zig +35 -0
  38. package/src/platform/root.zig +42 -0
  39. package/src/platform/types.zig +524 -18
  40. package/src/platform/windows/root.zig +432 -9
  41. package/src/platform/windows/webview2_host.cpp +482 -45
  42. package/src/primitives/app_manifest/comptime_scene.zig +5 -0
  43. package/src/primitives/app_manifest/root.zig +1 -0
  44. package/src/primitives/app_manifest/types.zig +32 -0
  45. package/src/primitives/app_manifest/validation.zig +18 -0
  46. package/src/primitives/canvas/a11y_audit.zig +33 -11
  47. package/src/primitives/canvas/a11y_audit_tests.zig +20 -0
  48. package/src/primitives/canvas/drawing.zig +25 -0
  49. package/src/primitives/canvas/equality.zig +8 -0
  50. package/src/primitives/canvas/events.zig +103 -16
  51. package/src/primitives/canvas/frame.zig +125 -1
  52. package/src/primitives/canvas/layout_audit.zig +22 -5
  53. package/src/primitives/canvas/markdown_hostile_tests.zig +8 -4
  54. package/src/primitives/canvas/render_tests.zig +13 -10
  55. package/src/primitives/canvas/root.zig +48 -0
  56. package/src/primitives/canvas/terminal_box.zig +471 -0
  57. package/src/primitives/canvas/terminal_grid.zig +978 -0
  58. package/src/primitives/canvas/terminal_grid_tests.zig +1093 -0
  59. package/src/primitives/canvas/test_support.zig +1 -0
  60. package/src/primitives/canvas/tests.zig +2 -0
  61. package/src/primitives/canvas/text_tests.zig +39 -4
  62. package/src/primitives/canvas/tokens.zig +115 -12
  63. package/src/primitives/canvas/ui.zig +535 -12
  64. package/src/primitives/canvas/ui_markup.zig +253 -36
  65. package/src/primitives/canvas/ui_markup_binary.zig +4 -1
  66. package/src/primitives/canvas/ui_markup_compiled.zig +195 -9
  67. package/src/primitives/canvas/ui_markup_compiled_tests.zig +188 -8
  68. package/src/primitives/canvas/ui_markup_contract.zig +89 -6
  69. package/src/primitives/canvas/ui_markup_contract_tests.zig +65 -1
  70. package/src/primitives/canvas/ui_markup_huge_document_tests.zig +154 -0
  71. package/src/primitives/canvas/ui_markup_huge_model_tests.zig +1 -0
  72. package/src/primitives/canvas/ui_markup_reflect.zig +75 -24
  73. package/src/primitives/canvas/ui_markup_tests.zig +178 -9
  74. package/src/primitives/canvas/ui_markup_view.zig +220 -11
  75. package/src/primitives/canvas/ui_markup_view_tests.zig +553 -28
  76. package/src/primitives/canvas/ui_schema.zig +110 -17
  77. package/src/primitives/canvas/ui_schema_tests.zig +18 -10
  78. package/src/primitives/canvas/ui_tests.zig +141 -9
  79. package/src/primitives/canvas/widget_access.zig +28 -2
  80. package/src/primitives/canvas/widget_anchored_tests.zig +48 -0
  81. package/src/primitives/canvas/widget_invalidation.zig +81 -1
  82. package/src/primitives/canvas/widget_layout.zig +59 -11
  83. package/src/primitives/canvas/widget_layout_tests.zig +125 -4
  84. package/src/primitives/canvas/widget_render.zig +272 -18
  85. package/src/primitives/canvas/widget_render_scroll.zig +108 -27
  86. package/src/primitives/canvas/widget_routing.zig +59 -7
  87. package/src/primitives/canvas/widget_runtime.zig +23 -0
  88. package/src/primitives/canvas/widget_runtime_tests.zig +149 -0
  89. package/src/primitives/canvas/widget_semantics.zig +119 -11
  90. package/src/primitives/canvas/widget_semantics_tests.zig +409 -0
  91. package/src/primitives/canvas/widget_tree.zig +17 -0
  92. package/src/primitives/canvas/widgets.zig +132 -0
  93. package/src/root.zig +23 -0
  94. package/src/runtime/api.zig +76 -5
  95. package/src/runtime/automation_commands.zig +20 -1
  96. package/src/runtime/automation_snapshot.zig +25 -1
  97. package/src/runtime/automation_widget_dispatch.zig +73 -8
  98. package/src/runtime/bridge_payload.zig +8 -0
  99. package/src/runtime/bridge_responses.zig +5 -0
  100. package/src/runtime/builtin_bridge.zig +9 -4
  101. package/src/runtime/canvas_font_tests.zig +439 -2
  102. package/src/runtime/canvas_fonts.zig +61 -6
  103. package/src/runtime/canvas_frame.zig +62 -7
  104. package/src/runtime/canvas_frame_animation_tests.zig +5 -4
  105. package/src/runtime/canvas_frame_helpers.zig +190 -1
  106. package/src/runtime/canvas_frame_invalidation_tests.zig +531 -0
  107. package/src/runtime/canvas_frame_patch_tests.zig +161 -10
  108. package/src/runtime/canvas_frame_retained_tests.zig +5 -2
  109. package/src/runtime/canvas_image_tests.zig +195 -0
  110. package/src/runtime/canvas_images.zig +45 -12
  111. package/src/runtime/canvas_widget_accessibility_tests.zig +1 -1
  112. package/src/runtime/canvas_widget_clipboard_tests.zig +40 -0
  113. package/src/runtime/canvas_widget_context_menu.zig +269 -32
  114. package/src/runtime/canvas_widget_context_menu_tests.zig +640 -14
  115. package/src/runtime/canvas_widget_display.zig +1 -1
  116. package/src/runtime/canvas_widget_event_tests.zig +1823 -0
  117. package/src/runtime/canvas_widget_events.zig +240 -24
  118. package/src/runtime/canvas_widget_runtime.zig +213 -29
  119. package/src/runtime/canvas_widget_scroll_driver_tests.zig +170 -7
  120. package/src/runtime/canvas_widget_scroll_drivers.zig +151 -17
  121. package/src/runtime/canvas_widget_scroll_tests.zig +435 -23
  122. package/src/runtime/canvas_widget_state.zig +6 -0
  123. package/src/runtime/canvas_widget_text_tests.zig +1 -1
  124. package/src/runtime/core.zig +131 -13
  125. package/src/runtime/effects.zig +8136 -180
  126. package/src/runtime/effects_channel_tests.zig +3519 -0
  127. package/src/runtime/effects_fetch_tests.zig +89 -1
  128. package/src/runtime/effects_file_tests.zig +41 -1
  129. package/src/runtime/effects_image_tests.zig +1304 -0
  130. package/src/runtime/effects_pty_tests.zig +1362 -0
  131. package/src/runtime/effects_tests.zig +167 -9
  132. package/src/runtime/effects_video_tests.zig +3609 -0
  133. package/src/runtime/flow.zig +42 -4
  134. package/src/runtime/gpu_surface_events.zig +473 -13
  135. package/src/runtime/media_surface.zig +116 -11
  136. package/src/runtime/media_surface_tests.zig +61 -0
  137. package/src/runtime/pty.zig +1252 -0
  138. package/src/runtime/pty_windows.zig +1780 -0
  139. package/src/runtime/root.zig +36 -0
  140. package/src/runtime/runtime_event_tests.zig +20 -1
  141. package/src/runtime/session_blobs.zig +689 -0
  142. package/src/runtime/session_journal.zig +432 -62
  143. package/src/runtime/session_record.zig +43 -2
  144. package/src/runtime/session_replay.zig +556 -20
  145. package/src/runtime/session_tests.zig +2087 -1
  146. package/src/runtime/shell_layout.zig +7 -0
  147. package/src/runtime/terminal_session.zig +1569 -0
  148. package/src/runtime/terminal_session_tests.zig +450 -0
  149. package/src/runtime/terminal_vt_ghostty.zig +15 -0
  150. package/src/runtime/terminal_vt_stub.zig +13 -0
  151. package/src/runtime/testdata/fonts/NotoSansSC-Receipt.ttf +0 -0
  152. package/src/runtime/testdata/fonts/OFL.txt +93 -0
  153. package/src/runtime/tests.zig +8 -0
  154. package/src/runtime/ts_core_host.zig +1320 -45
  155. package/src/runtime/ts_core_host_tests.zig +1100 -1
  156. package/src/runtime/ts_ui_app.zig +23 -5
  157. package/src/runtime/ui_app.zig +2444 -69
  158. package/src/runtime/ui_app_tests.zig +3171 -744
  159. package/src/runtime/ui_app_virtual_variable_tests.zig +4 -2
  160. package/src/runtime/ui_app_window_tests.zig +736 -0
  161. package/src/runtime/view.zig +190 -2
  162. package/src/runtime/view_widget_scroll.zig +218 -68
  163. package/src/runtime/view_widget_text.zig +11 -2
  164. package/src/runtime/view_widget_tree.zig +111 -3
  165. package/src/runtime/window_command_bridge_tests.zig +117 -0
  166. package/src/runtime/window_storage.zig +77 -2
  167. package/src/runtime/window_view_runtime.zig +34 -2
  168. package/src/runtime/window_views.zig +62 -1
  169. package/src/terminal_session_tests_root.zig +15 -0
  170. package/src/tooling/manifest.zig +75 -0
  171. package/src/tooling/raw_manifest.zig +2 -0
  172. package/src/tooling/templates.zig +244 -8
@@ -1,4 +1,5 @@
1
1
  const support = @import("test_support.zig");
2
+ const canvas_frame_helpers = @import("canvas_frame_helpers.zig");
2
3
  const std = support.std;
3
4
  const geometry = support.geometry;
4
5
  const trace = support.trace;
@@ -478,6 +479,536 @@ test "runtime requests gpu surface frames for retained canvas changes" {
478
479
  try std.testing.expectEqual(@as(usize, 2), harness.null_platform.gpu_surface_frame_request_count);
479
480
  }
480
481
 
482
+ // Incremental-vs-full pixel oracle for reflow damage: present tree A,
483
+ // swap to tree B through the SAME incremental machinery a selection
484
+ // change drives (keyed subtree replaced, elements removed/shrunk/moved),
485
+ // then compare the incrementally updated buffer against a fresh full
486
+ // render of tree B. Any byte difference is a stale pixel the damage
487
+ // region failed to cover.
488
+ fn expectIncrementalPixelPresentMatchesFullRender(
489
+ comptime name: []const u8,
490
+ retained_baseline: bool,
491
+ scale: f32,
492
+ ) !void {
493
+ const TestApp = struct {
494
+ fn app(self: *@This()) App {
495
+ return .{ .context = self, .name = name, .source = platform.WebViewSource.html("<h1>Hello</h1>") };
496
+ }
497
+ };
498
+
499
+ const surface = geometry.SizeF.init(220, 80);
500
+ const harness = try TestHarness().create(std.testing.allocator, .{});
501
+ defer harness.destroy(std.testing.allocator);
502
+ harness.null_platform.gpu_surfaces = true;
503
+ harness.runtime.options.pixel_present_retained_baseline = retained_baseline;
504
+ var app_state: TestApp = .{};
505
+ try harness.start(app_state.app());
506
+
507
+ _ = try harness.runtime.createView(.{
508
+ .window_id = 1,
509
+ .label = "canvas",
510
+ .kind = .gpu_surface,
511
+ .frame = geometry.RectF.init(0, 0, surface.width, surface.height),
512
+ });
513
+
514
+ // Detail-pane shape: a surface panel behind a row of pills. Tree A
515
+ // shows two wide pills; tree B (another selection) shows one shorter,
516
+ // shifted pill under NEW ids — the keyed replacement a `for`/`if`
517
+ // reconciliation produces.
518
+ const tree_a = [_]canvas.Widget{
519
+ .{ .id = 10, .kind = .panel, .frame = geometry.RectF.init(0, 0, 220, 80) },
520
+ .{ .id = 20, .kind = .badge, .frame = geometry.RectF.init(10, 24, 120, 24), .text = "In progress" },
521
+ .{ .id = 21, .kind = .badge, .frame = geometry.RectF.init(140, 24, 70, 24), .text = "High" },
522
+ };
523
+ const tree_b = [_]canvas.Widget{
524
+ .{ .id = 10, .kind = .panel, .frame = geometry.RectF.init(0, 0, 220, 80) },
525
+ .{ .id = 30, .kind = .badge, .frame = geometry.RectF.init(10, 26, 60, 20), .text = "Open" },
526
+ };
527
+
528
+ const pixel_size = try canvas_frame.canvasSurfacePixelSize(surface, scale);
529
+ const incremental = try std.testing.allocator.alloc(u8, pixel_size.byte_len);
530
+ defer std.testing.allocator.free(incremental);
531
+ const full = try std.testing.allocator.alloc(u8, pixel_size.byte_len);
532
+ defer std.testing.allocator.free(full);
533
+ const scratch = try std.testing.allocator.alloc(u8, pixel_size.byte_len);
534
+ defer std.testing.allocator.free(scratch);
535
+ const clear_color = canvas.Color.rgb8(15, 23, 42);
536
+
537
+ var nodes_a: [4]canvas.WidgetLayoutNode = undefined;
538
+ const layout_a = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &tree_a }, geometry.RectF.init(0, 0, surface.width, surface.height), &nodes_a);
539
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout_a);
540
+ _ = try harness.runtime.emitCanvasWidgetDisplayListWithStoredTokens(1, "canvas");
541
+ _ = try harness.runtime.presentNextCanvasFramePixels(1, "canvas", .{
542
+ .frame_index = 1,
543
+ .timestamp_ns = 16_000_000,
544
+ .surface_size = surface,
545
+ .scale = scale,
546
+ }, canvasFrameScratchStorage(&harness.runtime), incremental, scratch, clear_color);
547
+
548
+ var nodes_b: [4]canvas.WidgetLayoutNode = undefined;
549
+ const layout_b = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &tree_b }, geometry.RectF.init(0, 0, surface.width, surface.height), &nodes_b);
550
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout_b);
551
+ const swapped = try harness.runtime.presentNextCanvasFramePixels(1, "canvas", .{
552
+ .frame_index = 2,
553
+ .timestamp_ns = 32_000_000,
554
+ .surface_size = surface,
555
+ .scale = scale,
556
+ }, canvasFrameScratchStorage(&harness.runtime), incremental, scratch, clear_color);
557
+ // The oracle only proves anything if the swap actually rode the
558
+ // incremental path.
559
+ try std.testing.expect(!swapped.full_repaint);
560
+ try std.testing.expect(swapped.dirty_bounds != null);
561
+
562
+ _ = try harness.runtime.presentNextCanvasFramePixels(1, "canvas", .{
563
+ .frame_index = 3,
564
+ .timestamp_ns = 48_000_000,
565
+ .surface_size = surface,
566
+ .scale = scale,
567
+ .full_repaint = true,
568
+ }, canvasFrameScratchStorage(&harness.runtime), full, scratch, clear_color);
569
+
570
+ try std.testing.expectEqualSlices(u8, full, incremental);
571
+ }
572
+
573
+ test "incremental repaint redraws unchanged neighbors sharing a boundary pixel" {
574
+ // A fractional dirty edge lands mid-pixel: the clear covers the
575
+ // whole boundary pixel, so an UNCHANGED antialiased neighbor that
576
+ // painted partial coverage into that pixel must be redrawn.
577
+ // Damage snapped to the device-pixel grid keeps the cull region
578
+ // identical to the cleared pixels; culled against the unaligned
579
+ // float rect, the neighbor's boundary coverage was erased into a
580
+ // missing fringe.
581
+ const TestApp = struct {
582
+ fn app(self: *@This()) App {
583
+ return .{ .context = self, .name = "gpu-canvas-boundary-pixel", .source = platform.WebViewSource.html("<h1>Hello</h1>") };
584
+ }
585
+ };
586
+
587
+ const surface = geometry.SizeF.init(32, 16);
588
+ const harness = try TestHarness().create(std.testing.allocator, .{});
589
+ defer harness.destroy(std.testing.allocator);
590
+ harness.null_platform.gpu_surfaces = true;
591
+ // The refined dirty path (retained key+fingerprint baseline) is
592
+ // the one that produces a TIGHT rect around the changed command;
593
+ // the summary fallback dirties every keyed command and would mask
594
+ // the boundary-pixel hazard.
595
+ harness.runtime.options.pixel_present_retained_baseline = true;
596
+ var app_state: TestApp = .{};
597
+ try harness.start(app_state.app());
598
+
599
+ _ = try harness.runtime.createView(.{
600
+ .window_id = 1,
601
+ .label = "canvas",
602
+ .kind = .gpu_surface,
603
+ .frame = geometry.RectF.init(0, 0, surface.width, surface.height),
604
+ });
605
+
606
+ // The changing rect ends mid-pixel at x=10.2; the unchanged rounded
607
+ // neighbor begins at x=11.3, feathering antialiased coverage into
608
+ // pixel column 11 — the column the aligned clear wipes.
609
+ const neighbor = canvas.CanvasCommand{ .fill_rounded_rect = .{
610
+ .id = 2,
611
+ .rect = geometry.RectF.init(11.3, 2, 8, 8),
612
+ .radius = canvas.Radius.all(3),
613
+ .fill = .{ .color = canvas.Color.rgb8(148, 163, 184) },
614
+ } };
615
+ const changing = struct {
616
+ fn command(color: canvas.Color) canvas.CanvasCommand {
617
+ return .{ .fill_rect = .{
618
+ .id = 1,
619
+ .rect = geometry.RectF.init(2, 2, 8.2, 8),
620
+ .fill = .{ .color = color },
621
+ } };
622
+ }
623
+ }.command;
624
+
625
+ const byte_len: usize = 32 * 16 * 4;
626
+ var incremental: [byte_len]u8 = undefined;
627
+ var full: [byte_len]u8 = undefined;
628
+ var scratch: [byte_len]u8 = undefined;
629
+ const clear_color = canvas.Color.rgb8(15, 23, 42);
630
+
631
+ _ = try harness.runtime.setCanvasDisplayList(1, "canvas", .{ .commands = &.{ changing(canvas.Color.rgb8(255, 0, 0)), neighbor } });
632
+ _ = try harness.runtime.presentNextCanvasFramePixels(1, "canvas", .{
633
+ .frame_index = 1,
634
+ .timestamp_ns = 16_000_000,
635
+ .surface_size = surface,
636
+ }, canvasFrameScratchStorage(&harness.runtime), &incremental, &scratch, clear_color);
637
+
638
+ _ = try harness.runtime.setCanvasDisplayList(1, "canvas", .{ .commands = &.{ changing(canvas.Color.rgb8(37, 99, 235)), neighbor } });
639
+ const swapped = try harness.runtime.presentNextCanvasFramePixels(1, "canvas", .{
640
+ .frame_index = 2,
641
+ .timestamp_ns = 32_000_000,
642
+ .surface_size = surface,
643
+ }, canvasFrameScratchStorage(&harness.runtime), &incremental, &scratch, clear_color);
644
+ try std.testing.expect(!swapped.full_repaint);
645
+ try std.testing.expect(swapped.dirty_bounds != null);
646
+
647
+ _ = try harness.runtime.presentNextCanvasFramePixels(1, "canvas", .{
648
+ .frame_index = 3,
649
+ .timestamp_ns = 48_000_000,
650
+ .surface_size = surface,
651
+ .full_repaint = true,
652
+ }, canvasFrameScratchStorage(&harness.runtime), &full, &scratch, clear_color);
653
+
654
+ try std.testing.expectEqualSlices(u8, &full, &incremental);
655
+ }
656
+
657
+ test "a finer presentation scale re-aligns damage to its own pixel grid" {
658
+ // Pixel grids need not nest across scales: a plan-grid-aligned
659
+ // dirty edge at 11 points sits mid-pixel at a 1.5x presentation
660
+ // scale, so the present's clear wipes a boundary pixel whose
661
+ // antialiased coverage came from an unchanged neighbor the
662
+ // plan-aligned scissor culled. Damage presented at a DIFFERENT
663
+ // scale must re-snap outward on the presentation grid.
664
+ const TestApp = struct {
665
+ fn app(self: *@This()) App {
666
+ return .{ .context = self, .name = "gpu-canvas-fractional-scale-grid", .source = platform.WebViewSource.html("<h1>Hello</h1>") };
667
+ }
668
+ };
669
+
670
+ const surface = geometry.SizeF.init(32, 16);
671
+ const presentation_scale: f32 = 1.5;
672
+ const harness = try TestHarness().create(std.testing.allocator, .{});
673
+ defer harness.destroy(std.testing.allocator);
674
+ harness.null_platform.gpu_surfaces = true;
675
+ harness.runtime.options.pixel_present_retained_baseline = true;
676
+ var app_state: TestApp = .{};
677
+ try harness.start(app_state.app());
678
+
679
+ _ = try harness.runtime.createView(.{
680
+ .window_id = 1,
681
+ .label = "canvas",
682
+ .kind = .gpu_surface,
683
+ .frame = geometry.RectF.init(0, 0, surface.width, surface.height),
684
+ });
685
+
686
+ // The changing rect ends at x=9.2; with the bleed and plan-grid
687
+ // snap the dirty edge lands at x=11 — device 16.5 at 1.5x, so the
688
+ // clear covers the pixel spanning points [10.667, 11.333). The
689
+ // unchanged rounded neighbor at x=11.1 feathers coverage into that
690
+ // pixel while sitting outside the plan-aligned scissor.
691
+ const neighbor = canvas.CanvasCommand{ .fill_rounded_rect = .{
692
+ .id = 2,
693
+ .rect = geometry.RectF.init(11.1, 2, 8, 8),
694
+ .radius = canvas.Radius.all(3),
695
+ .fill = .{ .color = canvas.Color.rgb8(148, 163, 184) },
696
+ } };
697
+ const changing = struct {
698
+ fn command(color: canvas.Color) canvas.CanvasCommand {
699
+ return .{ .fill_rect = .{
700
+ .id = 1,
701
+ .rect = geometry.RectF.init(2, 2, 7.2, 8),
702
+ .fill = .{ .color = color },
703
+ } };
704
+ }
705
+ }.command;
706
+
707
+ const pixel_size = try canvas_frame_helpers.canvasSurfacePixelSize(surface, presentation_scale);
708
+ const incremental = try std.testing.allocator.alloc(u8, pixel_size.byte_len);
709
+ defer std.testing.allocator.free(incremental);
710
+ const full = try std.testing.allocator.alloc(u8, pixel_size.byte_len);
711
+ defer std.testing.allocator.free(full);
712
+ const scratch = try std.testing.allocator.alloc(u8, pixel_size.byte_len);
713
+ defer std.testing.allocator.free(scratch);
714
+ const clear_color = canvas.Color.rgb8(15, 23, 42);
715
+ var no_gpu_commands: [0]canvas.CanvasGpuCommand = .{};
716
+ var no_packet_bytes: [0]u8 = .{};
717
+
718
+ const presentScaled = struct {
719
+ fn present(h: anytype, frame_index: u64, full_repaint: bool, pixels: []u8, sc: []u8, clear: canvas.Color, gpu: []canvas.CanvasGpuCommand, packet: []u8) !canvas.CanvasFrame {
720
+ const result = try h.runtime.presentNextCanvasFrame(1, "canvas", .{
721
+ .frame_index = frame_index,
722
+ .timestamp_ns = frame_index * 16_000_000,
723
+ .surface_size = geometry.SizeF.init(32, 16),
724
+ .full_repaint = full_repaint,
725
+ }, canvasFrameScratchStorage(&h.runtime), gpu, packet, pixels, sc, clear, 1.5);
726
+ return result.frame;
727
+ }
728
+ }.present;
729
+
730
+ _ = try harness.runtime.setCanvasDisplayList(1, "canvas", .{ .commands = &.{ changing(canvas.Color.rgb8(255, 0, 0)), neighbor } });
731
+ _ = try presentScaled(harness, 1, false, incremental, scratch, clear_color, &no_gpu_commands, &no_packet_bytes);
732
+
733
+ _ = try harness.runtime.setCanvasDisplayList(1, "canvas", .{ .commands = &.{ changing(canvas.Color.rgb8(37, 99, 235)), neighbor } });
734
+ const swapped = try presentScaled(harness, 2, false, incremental, scratch, clear_color, &no_gpu_commands, &no_packet_bytes);
735
+ try std.testing.expect(!swapped.full_repaint);
736
+ try std.testing.expect(swapped.dirty_bounds != null);
737
+
738
+ _ = try presentScaled(harness, 3, true, full, scratch, clear_color, &no_gpu_commands, &no_packet_bytes);
739
+ try std.testing.expectEqualSlices(u8, full, incremental);
740
+ }
741
+
742
+ test "device-grid re-alignment survives the float round-trip" {
743
+ // Snapped edges live in f32 logical points: `ceil(v*s)/s` can land
744
+ // on an f32 whose product with the scale rounds back UP past the
745
+ // device boundary (an edge at 65 points re-snapped at 1.75x becomes
746
+ // 65.14286, whose product is 114.00001 — ceiling to 115), so the
747
+ // consumer would clear one pixel more than culling against the rect
748
+ // admits — erasing an unchanged neighbor's coverage in that extra
749
+ // pixel. The snap must pick edges whose round trip lands on the
750
+ // intended boundary.
751
+ const TestApp = struct {
752
+ fn app(self: *@This()) App {
753
+ return .{ .context = self, .name = "gpu-canvas-grid-roundtrip", .source = platform.WebViewSource.html("<h1>Hello</h1>") };
754
+ }
755
+ };
756
+
757
+ const surface = geometry.SizeF.init(80, 16);
758
+ const presentation_scale: f32 = 1.75;
759
+ const harness = try TestHarness().create(std.testing.allocator, .{});
760
+ defer harness.destroy(std.testing.allocator);
761
+ harness.null_platform.gpu_surfaces = true;
762
+ harness.runtime.options.pixel_present_retained_baseline = true;
763
+ var app_state: TestApp = .{};
764
+ try harness.start(app_state.app());
765
+
766
+ _ = try harness.runtime.createView(.{
767
+ .window_id = 1,
768
+ .label = "canvas",
769
+ .kind = .gpu_surface,
770
+ .frame = geometry.RectF.init(0, 0, surface.width, surface.height),
771
+ });
772
+
773
+ // Changing rect ends at x=63.9: bleed plus the plan-grid snap land
774
+ // the dirty edge exactly on 65 points — the coordinate whose 1.75x
775
+ // re-snap rounds badly. The unchanged rounded neighbor at x=65.3
776
+ // paints device pixel 114, the pixel the bad round trip clears.
777
+ const neighbor = canvas.CanvasCommand{ .fill_rounded_rect = .{
778
+ .id = 2,
779
+ .rect = geometry.RectF.init(65.3, 2, 8, 8),
780
+ .radius = canvas.Radius.all(3),
781
+ .fill = .{ .color = canvas.Color.rgb8(148, 163, 184) },
782
+ } };
783
+ const changing = struct {
784
+ fn command(color: canvas.Color) canvas.CanvasCommand {
785
+ return .{ .fill_rect = .{
786
+ .id = 1,
787
+ .rect = geometry.RectF.init(2, 2, 61.9, 8),
788
+ .fill = .{ .color = color },
789
+ } };
790
+ }
791
+ }.command;
792
+
793
+ const pixel_size = try canvas_frame_helpers.canvasSurfacePixelSize(surface, presentation_scale);
794
+ const incremental = try std.testing.allocator.alloc(u8, pixel_size.byte_len);
795
+ defer std.testing.allocator.free(incremental);
796
+ const full = try std.testing.allocator.alloc(u8, pixel_size.byte_len);
797
+ defer std.testing.allocator.free(full);
798
+ const scratch = try std.testing.allocator.alloc(u8, pixel_size.byte_len);
799
+ defer std.testing.allocator.free(scratch);
800
+ const clear_color = canvas.Color.rgb8(15, 23, 42);
801
+ var no_gpu_commands: [0]canvas.CanvasGpuCommand = .{};
802
+ var no_packet_bytes: [0]u8 = .{};
803
+
804
+ const presentScaled = struct {
805
+ fn present(h: anytype, frame_index: u64, full_repaint: bool, pixels: []u8, sc: []u8, clear: canvas.Color, gpu: []canvas.CanvasGpuCommand, packet: []u8) !canvas.CanvasFrame {
806
+ const result = try h.runtime.presentNextCanvasFrame(1, "canvas", .{
807
+ .frame_index = frame_index,
808
+ .timestamp_ns = frame_index * 16_000_000,
809
+ .surface_size = geometry.SizeF.init(80, 16),
810
+ .full_repaint = full_repaint,
811
+ }, canvasFrameScratchStorage(&h.runtime), gpu, packet, pixels, sc, clear, 1.75);
812
+ return result.frame;
813
+ }
814
+ }.present;
815
+
816
+ _ = try harness.runtime.setCanvasDisplayList(1, "canvas", .{ .commands = &.{ changing(canvas.Color.rgb8(255, 0, 0)), neighbor } });
817
+ _ = try presentScaled(harness, 1, false, incremental, scratch, clear_color, &no_gpu_commands, &no_packet_bytes);
818
+
819
+ _ = try harness.runtime.setCanvasDisplayList(1, "canvas", .{ .commands = &.{ changing(canvas.Color.rgb8(37, 99, 235)), neighbor } });
820
+ const swapped = try presentScaled(harness, 2, false, incremental, scratch, clear_color, &no_gpu_commands, &no_packet_bytes);
821
+ try std.testing.expect(!swapped.full_repaint);
822
+ try std.testing.expect(swapped.dirty_bounds != null);
823
+ // The dirty edge's round trip must land on its device boundary —
824
+ // never past it. The presented-scale rework carries one presented
825
+ // device pixel of bleed past the plan edge's boundary (114), so
826
+ // the reconstructed product may ceil to 115 but never beyond.
827
+ const dirty = swapped.dirty_bounds.?;
828
+ try std.testing.expect(@ceil(dirty.maxX() * presentation_scale) <= 115);
829
+
830
+ _ = try presentScaled(harness, 3, true, full, scratch, clear_color, &no_gpu_commands, &no_packet_bytes);
831
+ try std.testing.expectEqualSlices(u8, full, incremental);
832
+ }
833
+
834
+ test "bleed-aligned dirty edges round-trip onto their device boundaries" {
835
+ // The stored rect's f32 fields must hand consumers products that
836
+ // land exactly on the intended integer device boundaries — one
837
+ // whole device pixel of bleed outside the input's floor/ceil — in
838
+ // BOTH directions: the stored min edge and the RECONSTRUCTED max
839
+ // edge (x + width). A rounded 1/scale inflation falls a whole
840
+ // pixel short at fractional scales, and a re-encoded width can
841
+ // push the reconstructed product past its boundary.
842
+ const scales = [_]f32{ 1, 1.1, 1.2, 1.25, 1.3, 1.5, 1.75, 2, 2.5, 3 };
843
+ var i: usize = 0;
844
+ while (i < 2000) : (i += 1) {
845
+ const x: f32 = @as(f32, @floatFromInt(i)) * 0.173 + 0.07;
846
+ const w: f32 = @as(f32, @floatFromInt(i % 37)) * 0.61 + 0.4;
847
+ const rect = geometry.RectF.init(x, x * 0.5, w, w * 0.8);
848
+ for (scales) |scale| {
849
+ const aligned = canvas_frame.bleedAlignedCanvasDirtyBounds(rect, scale, 1, .{}).?;
850
+ // The surface-clamped path must uphold the same boundaries
851
+ // when the rect fits well inside the surface: clipping
852
+ // happens on the boundaries, never as a re-encoding rect
853
+ // intersection afterwards.
854
+ const clamped = canvas_frame.bleedAlignedCanvasDirtyBounds(rect, scale, 1, geometry.SizeF.init(4096, 4096)).?;
855
+ const min_boundary = @floor(rect.minX() * scale) - 1;
856
+ const max_boundary = @ceil(rect.maxX() * scale) + 1;
857
+ const min_product = aligned.minX() * scale;
858
+ const max_product = aligned.maxX() * scale;
859
+ try std.testing.expect(min_product >= min_boundary);
860
+ try std.testing.expect(@floor(min_product) == min_boundary);
861
+ try std.testing.expect(max_product <= max_boundary);
862
+ try std.testing.expect(@ceil(max_product) == max_boundary);
863
+ // Retained hosts reconstruct and scale the wire rect in
864
+ // DOUBLE precision — the same boundaries must hold there:
865
+ // an f32 product can round onto the boundary while the
866
+ // exact product sits past it, and the host would clear one
867
+ // pixel more than the engine culled for.
868
+ const min_product64 = @as(f64, aligned.minX()) * @as(f64, scale);
869
+ const max_product64 = (@as(f64, aligned.minX()) + @as(f64, aligned.width)) * @as(f64, scale);
870
+ try std.testing.expect(@floor(min_product64) == min_boundary);
871
+ try std.testing.expect(@ceil(max_product64) == max_boundary);
872
+ if (rect.maxX() * scale + 2 < 4096) {
873
+ const clamped_min64 = @as(f64, clamped.minX()) * @as(f64, scale);
874
+ const clamped_max64 = (@as(f64, clamped.minX()) + @as(f64, clamped.width)) * @as(f64, scale);
875
+ try std.testing.expect(@floor(clamped_min64) == @max(0, min_boundary));
876
+ try std.testing.expect(@ceil(clamped_max64) == max_boundary);
877
+ }
878
+ }
879
+ }
880
+
881
+ // The double-precision seam's known bad case: [x=1, width=64] at
882
+ // 1.75x — the f32 product of the snapped edge rounds onto 115
883
+ // while its exact product sits at 115.0000019.
884
+ const seam = canvas_frame.bleedAlignedCanvasDirtyBounds(geometry.RectF.init(1, 1, 64, 8), 1.75, 1, .{}).?;
885
+ const seam_product64 = (@as(f64, seam.minX()) + @as(f64, seam.width)) * 1.75;
886
+ try std.testing.expect(@ceil(seam_product64) <= 115);
887
+ }
888
+
889
+ test "bleed alignment stays finite for far off-screen damage" {
890
+ // Scaling a far off-screen coordinate can floor/ceil to infinity;
891
+ // the boundary must clamp instead of the edge search walking
892
+ // nextAfter from infinity forever. The clamped result only needs
893
+ // to stay finite and OUTSIDE any presentable surface — surface
894
+ // clipping owns the rest.
895
+ const negative = canvas_frame.bleedAlignedCanvasDirtyBounds(geometry.RectF.init(-3.0e38, -3.0e38, 1.0e38, 1.0e38), 2, 1, .{}).?;
896
+ try std.testing.expect(std.math.isFinite(negative.minX()));
897
+ try std.testing.expect(std.math.isFinite(negative.maxX()));
898
+ try std.testing.expect(negative.maxX() <= 0);
899
+
900
+ const positive = canvas_frame.bleedAlignedCanvasDirtyBounds(geometry.RectF.init(1.0e38, 1.0e38, 2.0e38, 2.0e38), 2, 1, .{}).?;
901
+ try std.testing.expect(std.math.isFinite(positive.minX()));
902
+ try std.testing.expect(std.math.isFinite(positive.maxX()));
903
+ try std.testing.expect(positive.minX() >= 16_384);
904
+
905
+ // A SMALL far off-screen rect: the derivation must settle
906
+ // immediately (an offset walk would crawl through denormals
907
+ // without ever moving the sum) into a finite rect on the correct
908
+ // side of the surface.
909
+ const collapsed = canvas_frame.bleedAlignedCanvasDirtyBounds(geometry.RectF.init(-3.0e38, -3.0e38, 1, 1), 2, 1, .{}).?;
910
+ try std.testing.expect(std.math.isFinite(collapsed.minX()));
911
+ try std.testing.expect(std.math.isFinite(collapsed.maxX()));
912
+ try std.testing.expect(collapsed.maxX() <= 0);
913
+
914
+ // Legitimate damage past the exact-walk range must survive as a
915
+ // NON-COLLAPSED superset: with no clipping surface (the public
916
+ // frame planner's default), an empty rect would report "nothing to
917
+ // repaint" for real content.
918
+ const far = canvas_frame.bleedAlignedCanvasDirtyBounds(geometry.RectF.init(20_000_000, 4, 10, 10), 1, 1, .{}).?;
919
+ try std.testing.expect(far.width > 0);
920
+ try std.testing.expect(far.minX() <= 20_000_000);
921
+ try std.testing.expect(far.maxX() >= 20_000_000);
922
+
923
+ // The superset must stay AT far-but-representable content, not
924
+ // clamp toward the origin: a present overriding to a tiny scale
925
+ // makes such coordinates visible, and damage parked orders of
926
+ // magnitude away would leave their old pixels retained.
927
+ const very_far = canvas_frame.bleedAlignedCanvasDirtyBounds(geometry.RectF.init(1.0e38, 4, 1.0e37, 10), 1, 1, .{}).?;
928
+ try std.testing.expect(very_far.width > 0);
929
+ try std.testing.expect(very_far.minX() <= 1.0e38);
930
+ try std.testing.expect(very_far.maxX() >= 1.0e38);
931
+ }
932
+
933
+ test "reflow damage reaches a whole device pixel past changed bounds at fractional scales" {
934
+ // The AA bleed allowance is one DEVICE pixel: at scale 1.3 a pill
935
+ // starting at x=170 occupies device column 220, so removing it must
936
+ // dirty column 219 too — inflating by a rounded 1/scale in logical
937
+ // points multiplies back to exactly 220 and falls a whole pixel
938
+ // short.
939
+ const TestApp = struct {
940
+ fn app(self: *@This()) App {
941
+ return .{ .context = self, .name = "gpu-canvas-fractional-bleed", .source = platform.WebViewSource.html("<h1>Hello</h1>") };
942
+ }
943
+ };
944
+
945
+ const surface = geometry.SizeF.init(320, 240);
946
+ const scale: f32 = 1.3;
947
+ const harness = try TestHarness().create(std.testing.allocator, .{});
948
+ defer harness.destroy(std.testing.allocator);
949
+ harness.null_platform.gpu_surfaces = true;
950
+ harness.runtime.options.pixel_present_retained_baseline = true;
951
+ var app_state: TestApp = .{};
952
+ try harness.start(app_state.app());
953
+
954
+ _ = try harness.runtime.createView(.{
955
+ .window_id = 1,
956
+ .label = "canvas",
957
+ .kind = .gpu_surface,
958
+ .frame = geometry.RectF.init(0, 0, surface.width, surface.height),
959
+ });
960
+
961
+ const anchor = canvas.CanvasCommand{ .fill_rect = .{
962
+ .id = 100,
963
+ .rect = geometry.RectF.init(4, 4, 24, 12),
964
+ .fill = .{ .color = canvas.Color.rgb8(51, 65, 85) },
965
+ } };
966
+ const pill = canvas.CanvasCommand{ .fill_rounded_rect = .{
967
+ .id = 200,
968
+ .rect = geometry.RectF.init(170, 60, 40, 24),
969
+ .radius = canvas.Radius.all(12),
970
+ .fill = .{ .color = canvas.Color.rgb8(148, 163, 184) },
971
+ } };
972
+
973
+ const pixel_size = try canvas_frame_helpers.canvasSurfacePixelSize(surface, scale);
974
+ const pixels = try std.testing.allocator.alloc(u8, pixel_size.byte_len);
975
+ defer std.testing.allocator.free(pixels);
976
+ const scratch = try std.testing.allocator.alloc(u8, pixel_size.byte_len);
977
+ defer std.testing.allocator.free(scratch);
978
+ const clear_color = canvas.Color.rgb8(15, 23, 42);
979
+
980
+ _ = try harness.runtime.setCanvasDisplayList(1, "canvas", .{ .commands = &.{ anchor, pill } });
981
+ _ = try harness.runtime.presentNextCanvasFramePixels(1, "canvas", .{
982
+ .frame_index = 1,
983
+ .timestamp_ns = 16_000_000,
984
+ .surface_size = surface,
985
+ .scale = scale,
986
+ }, canvasFrameScratchStorage(&harness.runtime), pixels, scratch, clear_color);
987
+
988
+ _ = try harness.runtime.setCanvasDisplayList(1, "canvas", .{ .commands = &.{anchor} });
989
+ const removed = try harness.runtime.presentNextCanvasFramePixels(1, "canvas", .{
990
+ .frame_index = 2,
991
+ .timestamp_ns = 32_000_000,
992
+ .surface_size = surface,
993
+ .scale = scale,
994
+ }, canvasFrameScratchStorage(&harness.runtime), pixels, scratch, clear_color);
995
+ try std.testing.expect(!removed.full_repaint);
996
+ // The vacated pill starts in device column 220; its bleed pixel is
997
+ // column 219, which the damage must reach.
998
+ const dirty = removed.dirty_bounds.?;
999
+ try std.testing.expect(@floor(dirty.minX() * scale) <= 219);
1000
+ }
1001
+
1002
+ test "keyed subtree swap leaves no stale pixels on the summary-dirty pixel path" {
1003
+ try expectIncrementalPixelPresentMatchesFullRender("gpu-canvas-reflow-summary", false, 1);
1004
+ try expectIncrementalPixelPresentMatchesFullRender("gpu-canvas-reflow-summary-2x", false, 2);
1005
+ }
1006
+
1007
+ test "keyed subtree swap leaves no stale pixels on the refined-dirty pixel path" {
1008
+ try expectIncrementalPixelPresentMatchesFullRender("gpu-canvas-reflow-refined", true, 1);
1009
+ try expectIncrementalPixelPresentMatchesFullRender("gpu-canvas-reflow-refined-2x", true, 2);
1010
+ }
1011
+
481
1012
  test "runtime rejects duplicate canvas ids before replacing retained scene" {
482
1013
  const TestApp = struct {
483
1014
  fn app(self: *@This()) App {