@native-sdk/cli 0.7.1 → 0.8.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 (94) hide show
  1. package/build/app.zig +273 -65
  2. package/build/ts_run.mjs +6 -8
  3. package/build.zig +341 -361
  4. package/native-sdk.d.ts +6 -3
  5. package/package.json +13 -11
  6. package/packages/core/compile-surface/core.ts +559 -0
  7. package/packages/core/package-lock.json +375 -12
  8. package/packages/core/package.json +9 -6
  9. package/packages/core/scripts/compiler_typecheck.mjs +58 -0
  10. package/packages/core/scripts/gen_case_tables.mjs +130 -0
  11. package/packages/core/scripts/gen_declarations.mjs +97 -0
  12. package/packages/core/scripts/run_external_core_compiler.mjs +108 -0
  13. package/packages/core/scripts/stage_external_core.mjs +148 -0
  14. package/packages/core/sdk/core.d.ts +363 -0
  15. package/packages/core/sdk/events.d.ts +65 -0
  16. package/packages/core/sdk/text.d.ts +51 -0
  17. package/packages/core/src/checker.ts +3 -3
  18. package/packages/core/src/cli.ts +40 -38
  19. package/packages/core/src/contract.ts +749 -0
  20. package/packages/core/src/devhost.ts +4 -3
  21. package/packages/core/src/diagnostics.ts +5 -7
  22. package/packages/core/src/{transpile.ts → frontend.ts} +38 -50
  23. package/packages/core/src/infer.ts +1 -1
  24. package/packages/core/src/typed_ast.ts +16 -15
  25. package/packages/core/src/wyhash.ts +79 -0
  26. package/skill-data/native-ui/SKILL.md +4 -2
  27. package/skill-data/ts-core/SKILL.md +29 -29
  28. package/src/platform/macos/appkit_host.m +51 -3
  29. package/src/platform/types.zig +4 -0
  30. package/src/platform/windows/gpu_surface_renderer.cpp +2280 -0
  31. package/src/platform/windows/gpu_surface_renderer.h +86 -0
  32. package/src/platform/windows/root.zig +298 -20
  33. package/src/platform/windows/webview2_host.cpp +343 -33
  34. package/src/primitives/canvas/code.zig +50 -0
  35. package/src/primitives/canvas/code_tests.zig +174 -1
  36. package/src/primitives/canvas/frame.zig +45 -1
  37. package/src/primitives/canvas/gpu.zig +6 -0
  38. package/src/primitives/canvas/render_tests.zig +93 -0
  39. package/src/primitives/canvas/root.zig +2 -0
  40. package/src/primitives/canvas/serialization.zig +150 -21
  41. package/src/primitives/canvas/themes/geist.zig +51 -5
  42. package/src/primitives/canvas/tokens.zig +33 -4
  43. package/src/primitives/canvas/ui.zig +52 -7
  44. package/src/primitives/canvas/ui_markup.zig +38 -1
  45. package/src/primitives/canvas/ui_markup_compiled.zig +28 -0
  46. package/src/primitives/canvas/ui_markup_contract.zig +9 -0
  47. package/src/primitives/canvas/ui_markup_view.zig +28 -0
  48. package/src/primitives/canvas/ui_markup_view_tests.zig +10 -2
  49. package/src/primitives/canvas/ui_schema.zig +5 -0
  50. package/src/primitives/canvas/ui_schema_tests.zig +4 -3
  51. package/src/primitives/canvas/ui_tests.zig +14 -0
  52. package/src/primitives/canvas/widget_builtin_tests.zig +513 -13
  53. package/src/primitives/canvas/widget_invalidation.zig +11 -1
  54. package/src/primitives/canvas/widget_layout.zig +260 -21
  55. package/src/primitives/canvas/widget_metrics.zig +45 -9
  56. package/src/primitives/canvas/widget_render.zig +220 -70
  57. package/src/primitives/canvas/widget_render_controls.zig +79 -21
  58. package/src/primitives/canvas/widget_render_style.zig +52 -8
  59. package/src/primitives/canvas/widget_text_input.zig +2 -0
  60. package/src/primitives/canvas/widgets.zig +53 -2
  61. package/src/runtime/canvas_frame.zig +60 -7
  62. package/src/runtime/canvas_frame_invalidation_tests.zig +80 -0
  63. package/src/runtime/canvas_frame_packet_tests.zig +83 -9
  64. package/src/runtime/canvas_frame_patch_tests.zig +16 -0
  65. package/src/runtime/canvas_images.zig +8 -2
  66. package/src/runtime/canvas_widget_clipboard_tests.zig +1 -1
  67. package/src/runtime/canvas_widget_runtime.zig +11 -8
  68. package/src/runtime/effects.zig +6 -0
  69. package/src/runtime/effects_tests.zig +30 -0
  70. package/src/runtime/gpu_surface_events.zig +38 -7
  71. package/src/runtime/media_surface_tests.zig +1 -1
  72. package/src/runtime/runtime_event_tests.zig +74 -0
  73. package/src/runtime/ts_core_host.zig +38 -38
  74. package/src/runtime/ts_ui_app.zig +12 -12
  75. package/src/runtime/ui_app.zig +10 -8
  76. package/src/runtime/ui_app_tests.zig +67 -0
  77. package/src/runtime/view.zig +6 -0
  78. package/src/runtime/view_canvas.zig +15 -0
  79. package/src/runtime/view_widget_text.zig +6 -4
  80. package/src/runtime/window_view_runtime.zig +1 -0
  81. package/src/tooling/manifest.zig +32 -0
  82. package/src/tooling/raw_manifest.zig +4 -0
  83. package/src/tooling/templates.zig +34 -26
  84. package/src/tooling/ts_core.zig +55 -34
  85. package/src/tooling/verbs.zig +1 -1
  86. package/tools/corewire/core_abi.zig +169 -0
  87. package/tools/corewire/emit.zig +2418 -0
  88. package/tools/corewire/emit_facade.zig +3984 -0
  89. package/tools/corewire/emit_profile.zig +501 -0
  90. package/tools/corewire/main.zig +561 -0
  91. package/tools/corewire/shim_rt.zig +632 -0
  92. package/tools/corewire/sidecar.zig +2507 -0
  93. package/packages/core/rt/rt.zig +0 -1986
  94. package/packages/core/src/emitter.ts +0 -11898
@@ -27,6 +27,8 @@
27
27
  #include <thread>
28
28
  #include <vector>
29
29
 
30
+ #include "gpu_surface_renderer.h"
31
+
30
32
  /* NATIVE_SDK_ALLOW_WEBVIEW2_STUB is the build graph's declaration that
31
33
  * this app uses no web layer, and it wins over header visibility: on a
32
34
  * machine where the WebView2 SDK headers happen to be reachable through
@@ -183,6 +185,8 @@ constexpr int kViewSearchField = 9;
183
185
  constexpr int kViewLabel = 10;
184
186
  constexpr int kViewSpacer = 11;
185
187
  constexpr int kViewGpuSurface = 12;
188
+ constexpr int kGpuBackendRequestAccelerated = 0;
189
+ constexpr int kGpuBackendRequestSoftware = 1;
186
190
  constexpr int kViewCheckbox = 13;
187
191
  constexpr int kViewToggle = 14;
188
192
  constexpr int kViewProgressIndicator = 15;
@@ -225,10 +229,20 @@ struct WindowsEvent {
225
229
  uint64_t frame_interval_ns;
226
230
  int nonblank;
227
231
  uint32_t sample_color;
232
+ /* Concrete presenter and host-side cost for the most recent packet
233
+ * accepted since the prior frame event. backend: 0 software/GDI,
234
+ * 1 retained Direct2D. */
235
+ int gpu_backend;
236
+ uint64_t packet_decode_ns;
237
+ uint64_t packet_draw_ns;
228
238
  /* Nonzero when the frame completed logically while the top-level
229
239
  * window was minimized (heartbeat pacing; nothing painted): its
230
240
  * timestamp is pacing policy, never a latency endpoint. */
231
241
  int occluded;
242
+ /* The presenter lost its retained device resources. The runtime must
243
+ * rebuild this completion as a full frame even when the scene itself
244
+ * is otherwise unchanged. */
245
+ int force_full_repaint;
232
246
  int input_kind;
233
247
  int button;
234
248
  double delta_x;
@@ -421,7 +435,16 @@ struct NativeView {
421
435
  bool visible = true;
422
436
  bool enabled = true;
423
437
  bool explicit_text = false;
424
- /* gpu_surface (software canvas) state */
438
+ /* Portable creation request: accelerated maps to Direct2D on Windows;
439
+ * software is an explicit opt-out that keeps this view on the reference
440
+ * renderer plus GDI pixel presenter. */
441
+ int gpu_backend_request = kGpuBackendRequestAccelerated;
442
+ /* Retained Direct2D packet presenter. Transparent top-level windows
443
+ * intentionally leave this unused and take the alpha-correct pixel
444
+ * fallback because UpdateLayeredWindow cannot compose child HWNDs. */
445
+ std::shared_ptr<WindowsGpuSurface> gpu_surface;
446
+ /* Software-pixel fallback state (unrepresentable packet commands,
447
+ * transparent top-level windows, or Direct2D device loss/resync). */
425
448
  std::vector<uint8_t> gpu_bgra;
426
449
  int gpu_buf_width = 0;
427
450
  int gpu_buf_height = 0;
@@ -447,6 +470,11 @@ struct NativeView {
447
470
  * (its emission carries the nonblank verdict automation reads).
448
471
  * Neither can sustain a spin. Cleared when the emission fires. */
449
472
  bool gpu_prompt_frame_pending = false;
473
+ /* One-shot recovery request set by WM_PAINT after Direct2D target
474
+ * loss. It rides the next frame event and forces a fresh full packet;
475
+ * scheduling an ordinary idle completion would otherwise skip before
476
+ * the presenter can reject a patch and request a resync. */
477
+ bool gpu_force_full_repaint_pending = false;
450
478
  uint64_t gpu_last_emit_ns = 0;
451
479
  uint64_t gpu_frame_index = 0;
452
480
  double gpu_emitted_width = 0;
@@ -454,6 +482,16 @@ struct NativeView {
454
482
  double gpu_emitted_scale = 0;
455
483
  int gpu_nonblank = 0;
456
484
  uint32_t gpu_sample_color = 0;
485
+ int gpu_backend = 0;
486
+ uint64_t gpu_packet_decode_ns = 0;
487
+ uint64_t gpu_packet_draw_ns = 0;
488
+ /* Last device-pixel location sampled from the retained Direct2D
489
+ * backing for hidden-titlebar caption contrast. Dirty packets that
490
+ * do not cover this point must not synchronize the GPU target with
491
+ * GDI merely to rediscover the cached window color. */
492
+ bool gpu_caption_sample_valid = false;
493
+ LONG gpu_caption_sample_x = 0;
494
+ LONG gpu_caption_sample_y = 0;
457
495
  int gpu_pointer_down = 0;
458
496
  /* TrackMouseEvent(TME_LEAVE) armed for the current hover session:
459
497
  * set on the first WM_MOUSEMOVE, cleared by the WM_MOUSELEAVE it
@@ -635,6 +673,7 @@ struct Host {
635
673
  bool running = false;
636
674
  std::map<uint64_t, Window> windows;
637
675
  std::map<std::string, ChildWebView> webviews;
676
+ std::shared_ptr<WindowsGpuRenderer> gpu_renderer;
638
677
  std::map<std::string, NativeView> native_views;
639
678
  uint64_t next_child_order = 1;
640
679
  std::vector<std::string> allowed_origins;
@@ -1902,12 +1941,14 @@ static void destroyNativeViewsForWindow(Host *host, uint64_t window_id) {
1902
1941
 
1903
1942
  /* ---------------------------------------------------------------- gpu surface
1904
1943
  *
1905
- * A gpu_surface view is a plain Win32 child HWND driven by the CPU pixel
1906
- * path: the runtime rasterizes canvas frames with the reference renderer
1907
- * and hands RGBA8 buffers to native_sdk_windows_present_gpu_surface_pixels,
1908
- * which swizzles them into a top-down 32bpp BGRA DIB and invalidates the
1909
- * child; WM_PAINT blits with SetDIBitsToDevice (StretchDIBits while a
1910
- * resize is in flight). Frame events are DEMAND-DRIVEN through one
1944
+ * A gpu_surface view is a plain Win32 child HWND with a retained hardware
1945
+ * Direct2D presenter. The normal path decodes compact binary draw packets,
1946
+ * patches a compatible GPU bitmap inside the packet's dirty regions, and
1947
+ * lets WM_PAINT copy that bitmap to the child target. Unrepresentable
1948
+ * commands, transparent layered top-level windows, and unavailable GPU
1949
+ * devices retain the exact reference-rendered pixel path; that fallback
1950
+ * swizzles and invalidates only the dirty device-pixel rows when its prior
1951
+ * buffer is still valid. Frame events are DEMAND-DRIVEN through one
1911
1952
  * scheduler per surface (the macOS host's design): runtime frame
1912
1953
  * requests and pixel presents each arm a single grid-anchored one-shot
1913
1954
  * WM_TIMER emission, so an armed animation loop sees one
@@ -2373,20 +2414,18 @@ static void punchHiddenCaptionButtonHole(Host *host, const NativeView &view, HWN
2373
2414
  * caption color (plus the immersive dark flag, which picks the button
2374
2415
  * glyph/hover palette). Older builds reject the attribute and keep the
2375
2416
  * system caption material — the buttons still draw and work. */
2376
- static void syncHiddenCaptionColor(Host *host, Window &window, const NativeView &view, const uint8_t *rgba8, size_t width, size_t height) {
2417
+ static void syncHiddenCaptionColor(Window &window, const NativeView &view, const uint8_t *rgba8, size_t width, size_t height) {
2377
2418
  if (!windowUsesHiddenTitlebar(window) || !window.hwnd || !view.hwnd) return;
2378
2419
  const DwmApi &dwm = dwmApi();
2379
2420
  if (!dwm.set_window_attribute) return;
2380
2421
  RECT cluster = {};
2381
2422
  if (!captionButtonsClientRect(window.hwnd, &cluster)) return;
2382
2423
  const POINT origin = childOriginInParentClient(view.hwnd, window.hwnd);
2383
- long sample_x = (long)(cluster.left - origin.x) - 8;
2384
- long sample_y = (long)((cluster.top + cluster.bottom) / 2 - origin.y);
2385
- if (sample_x < 0) sample_x = 0;
2386
- if (sample_x >= (long)width) sample_x = (long)width - 1;
2387
- if (sample_y < 0) sample_y = 0;
2388
- if (sample_y >= (long)height) sample_y = (long)height - 1;
2389
- const uint8_t *pixel = rgba8 + ((size_t)sample_y * width + (size_t)sample_x) * 4;
2424
+ const LONG sample_x = cluster.left - origin.x - 8;
2425
+ const LONG sample_y = (cluster.top + cluster.bottom) / 2 - origin.y;
2426
+ if (sample_x < 0 || sample_y < 0 ||
2427
+ static_cast<size_t>(sample_x) >= width || static_cast<size_t>(sample_y) >= height) return;
2428
+ const uint8_t *pixel = rgba8 + (static_cast<size_t>(sample_y) * width + static_cast<size_t>(sample_x)) * 4;
2390
2429
  const COLORREF color = RGB(pixel[0], pixel[1], pixel[2]);
2391
2430
  if (window.hidden_caption_color_set && window.hidden_caption_color == color) return;
2392
2431
  window.hidden_caption_color = color;
@@ -2396,6 +2435,63 @@ static void syncHiddenCaptionColor(Host *host, Window &window, const NativeView
2396
2435
  dwm.set_window_attribute(window.hwnd, kDwmwaUseImmersiveDarkMode, &dark, sizeof(dark));
2397
2436
  }
2398
2437
 
2438
+ /* Hidden-titlebar caption buttons must use the color that actually reached
2439
+ * the backing target: gradients, images, blur, and path geometry can all
2440
+ * differ from a retained command's bounding-box approximation. This is the
2441
+ * packet path's only GPU readback; it runs only when this surface covers the
2442
+ * caption sample and the packet dirtied that pixel (or established a fresh
2443
+ * backing). A failed sample falls back to the diagnostic heuristic. */
2444
+ static void syncHiddenCaptionColorFromPacket(
2445
+ Host *host, Window &window, NativeView &view, const WindowsGpuPresentInfo &info,
2446
+ double surface_width, double surface_height) {
2447
+ if (!host || !view.gpu_surface || !windowUsesHiddenTitlebar(window) || !window.hwnd || !view.hwnd) return;
2448
+ const DwmApi &dwm = dwmApi();
2449
+ if (!dwm.set_window_attribute) return;
2450
+ RECT cluster = {};
2451
+ if (!captionButtonsClientRect(window.hwnd, &cluster)) return;
2452
+ const POINT origin = childOriginInParentClient(view.hwnd, window.hwnd);
2453
+ const POINT sample = {
2454
+ cluster.left - origin.x - 8,
2455
+ (cluster.top + cluster.bottom) / 2 - origin.y,
2456
+ };
2457
+ RECT client = {};
2458
+ if (!GetClientRect(view.hwnd, &client) || !PtInRect(&client, sample)) return;
2459
+ const bool same_sample = view.gpu_caption_sample_valid &&
2460
+ view.gpu_caption_sample_x == sample.x && view.gpu_caption_sample_y == sample.y;
2461
+ if (same_sample && info.dirty_rect_count > 0) {
2462
+ bool sample_changed = false;
2463
+ for (size_t index = 0; index < info.dirty_rect_count; ++index) {
2464
+ if (PtInRect(&info.dirty_rects[index], sample)) {
2465
+ sample_changed = true;
2466
+ break;
2467
+ }
2468
+ }
2469
+ if (!sample_changed) return;
2470
+ }
2471
+ const double scale = gpuSurfaceScale(view.hwnd);
2472
+ if (!(scale > 0)) return;
2473
+ const double sample_x = static_cast<double>(sample.x) / scale;
2474
+ const double sample_y = static_cast<double>(sample.y) / scale;
2475
+ if (sample_x >= surface_width || sample_y >= surface_height) return;
2476
+ uint32_t packed = 0;
2477
+ if (!view.gpu_surface->readColorAt(sample_x, sample_y, &packed)) {
2478
+ packed = view.gpu_surface->representativeColorAt(sample_x, sample_y);
2479
+ }
2480
+ view.gpu_caption_sample_valid = true;
2481
+ view.gpu_caption_sample_x = sample.x;
2482
+ view.gpu_caption_sample_y = sample.y;
2483
+ const uint8_t red = (uint8_t)((packed >> 16) & 0xff);
2484
+ const uint8_t green = (uint8_t)((packed >> 8) & 0xff);
2485
+ const uint8_t blue = (uint8_t)(packed & 0xff);
2486
+ const COLORREF color = RGB(red, green, blue);
2487
+ if (window.hidden_caption_color_set && window.hidden_caption_color == color) return;
2488
+ window.hidden_caption_color = color;
2489
+ window.hidden_caption_color_set = true;
2490
+ dwm.set_window_attribute(window.hwnd, kDwmwaCaptionColor, &color, sizeof(color));
2491
+ const BOOL dark = (299 * red + 587 * green + 114 * blue) / 1000 < 128 ? TRUE : FALSE;
2492
+ dwm.set_window_attribute(window.hwnd, kDwmwaUseImmersiveDarkMode, &dark, sizeof(dark));
2493
+ }
2494
+
2399
2495
  static uint32_t gpuModifierFlags() {
2400
2496
  uint32_t flags = 0;
2401
2497
  if (keyDown(VK_CONTROL)) flags |= kShortcutModifierPrimary | kShortcutModifierControl;
@@ -2627,6 +2723,7 @@ static void gpuSurfaceEmitFrame(Host *host, NativeView &view, HWND hwnd) {
2627
2723
  * (an armed animation re-requesting) returns to the minimized
2628
2724
  * heartbeat unless another input lands. */
2629
2725
  view.gpu_prompt_frame_pending = false;
2726
+ const bool force_full_repaint = view.gpu_force_full_repaint_pending;
2630
2727
  const double scale = gpuSurfaceScale(hwnd);
2631
2728
  double width = 0;
2632
2729
  double height = 0;
@@ -2635,6 +2732,13 @@ static void gpuSurfaceEmitFrame(Host *host, NativeView &view, HWND hwnd) {
2635
2732
  gpuSurfaceAdvancePacingClock(view);
2636
2733
 
2637
2734
  view.gpu_frame_index += 1;
2735
+ /* Capture-and-clear before dispatch: the engine can synchronously
2736
+ * present its next packet while handling this event. Clearing after
2737
+ * dispatch would erase that new packet's timing stamps. */
2738
+ const uint64_t packet_decode_ns = view.gpu_packet_decode_ns;
2739
+ const uint64_t packet_draw_ns = view.gpu_packet_draw_ns;
2740
+ view.gpu_packet_decode_ns = 0;
2741
+ view.gpu_packet_draw_ns = 0;
2638
2742
  WindowsEvent event = {};
2639
2743
  event.kind = kGpuSurfaceFrame;
2640
2744
  event.width = width;
@@ -2645,10 +2749,18 @@ static void gpuSurfaceEmitFrame(Host *host, NativeView &view, HWND hwnd) {
2645
2749
  event.frame_interval_ns = kGpuFrameIntervalNs;
2646
2750
  event.nonblank = view.gpu_nonblank;
2647
2751
  event.sample_color = view.gpu_sample_color;
2752
+ event.gpu_backend = view.gpu_backend;
2753
+ event.packet_decode_ns = packet_decode_ns;
2754
+ event.packet_draw_ns = packet_draw_ns;
2648
2755
  /* Heartbeat-paced completions are not latency endpoints: their
2649
2756
  * timestamp measures the deliberate minimized cadence, not a paint
2650
2757
  * — the runtime skips input-latency stamping for them. */
2651
2758
  event.occluded = gpuSurfaceOccludedPacingActive(host, view) ? 1 : 0;
2759
+ event.force_full_repaint = force_full_repaint ? 1 : 0;
2760
+ /* Clear immediately before the callback: presentation is synchronous
2761
+ * and a new loss reported during that dispatch must survive for the
2762
+ * next event. Geometry failure above keeps the recovery request armed. */
2763
+ view.gpu_force_full_repaint_pending = false;
2652
2764
  emitGpuSurfaceEvent(host, view, event);
2653
2765
  }
2654
2766
 
@@ -2710,6 +2822,49 @@ static void paintGpuSurface(NativeView &view, HWND hwnd, HDC dc) {
2710
2822
  StretchDIBits(dc, 0, 0, client_width, client_height, 0, 0, view.gpu_buf_width, view.gpu_buf_height, view.gpu_bgra.data(), &info, DIB_RGB_COLORS, SRCCOPY);
2711
2823
  }
2712
2824
 
2825
+ /* Preserve a complex Win32 update region as individual rectangles for the
2826
+ * Direct2D backing copy. PAINTSTRUCT.rcPaint is only the region's bounding
2827
+ * box, which would fuse two far-apart packet patches back into a nearly
2828
+ * window-sized blit. Packet presents contribute at most eight rectangles;
2829
+ * the larger fixed cap also handles ordinary system/exposure invalidations.
2830
+ * Pathological regions safely fall back to their bounding box. */
2831
+ constexpr size_t kGpuPaintRegionRectCap = 64;
2832
+
2833
+ struct GpuPaintRegionData {
2834
+ RGNDATAHEADER header = {};
2835
+ RECT rects[kGpuPaintRegionRectCap] = {};
2836
+ };
2837
+
2838
+ static_assert(offsetof(GpuPaintRegionData, rects) == sizeof(RGNDATAHEADER));
2839
+
2840
+ static size_t gpuSurfaceUpdateRegionRects(HWND hwnd, RECT *rects, size_t rect_cap) {
2841
+ if (!hwnd || !rects || rect_cap == 0) return 0;
2842
+ HRGN region = CreateRectRgn(0, 0, 0, 0);
2843
+ if (!region) return 0;
2844
+ const int region_kind = GetUpdateRgn(hwnd, region, FALSE);
2845
+ size_t count = 0;
2846
+ if (region_kind == SIMPLEREGION) {
2847
+ RECT bounds = {};
2848
+ if (GetRgnBox(region, &bounds) != NULLREGION) rects[count++] = bounds;
2849
+ } else if (region_kind == COMPLEXREGION) {
2850
+ const DWORD required = GetRegionData(region, 0, nullptr);
2851
+ if (required > 0 && required <= sizeof(GpuPaintRegionData)) {
2852
+ GpuPaintRegionData data;
2853
+ if (GetRegionData(region, static_cast<DWORD>(sizeof(data)), reinterpret_cast<RGNDATA *>(&data)) > 0 &&
2854
+ data.header.nCount > 0 && data.header.nCount <= rect_cap) {
2855
+ count = data.header.nCount;
2856
+ for (size_t index = 0; index < count; ++index) rects[index] = data.rects[index];
2857
+ }
2858
+ }
2859
+ if (count == 0) {
2860
+ RECT bounds = {};
2861
+ if (GetRgnBox(region, &bounds) != NULLREGION) rects[count++] = bounds;
2862
+ }
2863
+ }
2864
+ DeleteObject(region);
2865
+ return count;
2866
+ }
2867
+
2713
2868
  static constexpr uint8_t compositePremultipliedChannel(uint8_t source, uint8_t source_alpha, uint8_t destination) {
2714
2869
  return static_cast<uint8_t>(source +
2715
2870
  (static_cast<uint32_t>(destination) * (255u - source_alpha) + 127u) / 255u);
@@ -2979,10 +3134,29 @@ static LRESULT CALLBACK gpuSurfaceProc(HWND hwnd, UINT message, WPARAM wparam, L
2979
3134
  }
2980
3135
  break;
2981
3136
  case WM_PAINT: {
3137
+ RECT paint_rects[kGpuPaintRegionRectCap] = {};
3138
+ size_t paint_rect_count = gpuSurfaceUpdateRegionRects(
3139
+ hwnd, paint_rects, kGpuPaintRegionRectCap);
2982
3140
  PAINTSTRUCT paint = {};
2983
3141
  HDC dc = BeginPaint(hwnd, &paint);
2984
3142
  if (dc) {
2985
- paintGpuSurface(*view, hwnd, dc);
3143
+ if (view->gpu_surface && view->gpu_surface->hasContent()) {
3144
+ if (paint_rect_count == 0 && !IsRectEmpty(&paint.rcPaint)) {
3145
+ paint_rects[0] = paint.rcPaint;
3146
+ paint_rect_count = 1;
3147
+ }
3148
+ if (!view->gpu_surface->paint(paint_rects, paint_rect_count)) {
3149
+ /* Direct2D resource loss invalidates the retained
3150
+ * backing. Wake the runtime and explicitly force a
3151
+ * full packet: an unchanged scene would otherwise
3152
+ * plan an idle frame and never call the presenter. */
3153
+ view->gpu_force_full_repaint_pending = true;
3154
+ view->gpu_prompt_frame_pending = true;
3155
+ gpuSurfaceScheduleFrameEmission(host, *view);
3156
+ }
3157
+ } else {
3158
+ paintGpuSurface(*view, hwnd, dc);
3159
+ }
2986
3160
  /* Hidden-titlebar parents: yield the caption-button
2987
3161
  * cluster back to the DWM (see the helper's comment). */
2988
3162
  punchHiddenCaptionButtonHole(host, *view, hwnd, dc);
@@ -5634,6 +5808,11 @@ Host *native_sdk_windows_create(const char *app_name, size_t app_name_len, const
5634
5808
  * RPC_E_CHANGED_MODE (an MTA got there first) leaves nothing to
5635
5809
  * balance. */
5636
5810
  host->com_initialized = SUCCEEDED(CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE));
5811
+ /* One hardware Direct2D/DirectWrite resource domain for every canvas
5812
+ * child. Creation also requires the in-memory font and constrained
5813
+ * fallback seams: without them packet text cannot match engine layout,
5814
+ * so the renderer refuses cleanly into the pixel fallback. */
5815
+ host->gpu_renderer = createWindowsGpuRenderer();
5637
5816
  host->app_name = slice(app_name, app_name_len);
5638
5817
  host->window_title = slice(window_title, window_title_len);
5639
5818
  host->bundle_id = slice(bundle_id, bundle_id_len);
@@ -6270,8 +6449,10 @@ int native_sdk_windows_set_window_close_policy(Host *host, uint64_t window_id, i
6270
6449
  return 1;
6271
6450
  }
6272
6451
 
6273
- int native_sdk_windows_create_view(Host *host, uint64_t window_id, const char *label, size_t label_len, int kind, const char *parent, size_t parent_len, double x, double y, double width, double height, int layer, int visible, int enabled, const char *role, size_t role_len, const char *accessibility_label, size_t accessibility_label_len, const char *text, size_t text_len, const char *command, size_t command_len) {
6452
+ int native_sdk_windows_create_view(Host *host, uint64_t window_id, const char *label, size_t label_len, int kind, int gpu_backend_request, const char *parent, size_t parent_len, double x, double y, double width, double height, int layer, int visible, int enabled, const char *role, size_t role_len, const char *accessibility_label, size_t accessibility_label_len, const char *text, size_t text_len, const char *command, size_t command_len) {
6274
6453
  if (!host || label_len == 0 || !isSupportedNativeViewKind(kind) || !validNativeViewFrame(x, y, width, height)) return 0;
6454
+ if (kind == kViewGpuSurface && gpu_backend_request != kGpuBackendRequestAccelerated &&
6455
+ gpu_backend_request != kGpuBackendRequestSoftware) return 0;
6275
6456
  auto window = host->windows.find(window_id);
6276
6457
  if (window == host->windows.end() || !window->second.hwnd) return 0;
6277
6458
  /* UpdateLayeredWindow owns the complete top-level bitmap and cannot
@@ -6308,6 +6489,7 @@ int native_sdk_windows_create_view(Host *host, uint64_t window_id, const char *l
6308
6489
  view.visible = visible != 0;
6309
6490
  view.enabled = enabled != 0;
6310
6491
  view.explicit_text = text_len > 0;
6492
+ view.gpu_backend_request = kind == kViewGpuSurface ? gpu_backend_request : kGpuBackendRequestAccelerated;
6311
6493
 
6312
6494
  const std::string display_text = nativeViewDisplayText(view);
6313
6495
  std::wstring wide_text = widen(display_text);
@@ -6395,6 +6577,11 @@ int native_sdk_windows_create_view(Host *host, uint64_t window_id, const char *l
6395
6577
  if (!hwnd) return 0;
6396
6578
 
6397
6579
  view.hwnd = hwnd;
6580
+ if (view.kind == kViewGpuSurface &&
6581
+ view.gpu_backend_request != kGpuBackendRequestSoftware &&
6582
+ host->gpu_renderer && !window->second.transparent) {
6583
+ view.gpu_surface = host->gpu_renderer->createSurface(hwnd);
6584
+ }
6398
6585
  applyNativeViewState(view, true, display_text);
6399
6586
  if (view.kind == kViewProgressIndicator) {
6400
6587
  SendMessageW(view.hwnd, PBM_SETMARQUEE, TRUE, 30);
@@ -6478,13 +6665,106 @@ int native_sdk_windows_show_context_menu(Host *host, uint64_t window_id, const c
6478
6665
  return 1;
6479
6666
  }
6480
6667
 
6668
+ int native_sdk_windows_present_gpu_surface_packet_binary(Host *host, uint64_t window_id, const char *label, size_t label_len, double surface_width, double surface_height, double scale, uint8_t clear_r, uint8_t clear_g, uint8_t clear_b, uint8_t clear_a, int requires_render, size_t command_count, size_t unsupported_command_count, int representable, const uint8_t *packet, size_t packet_len) {
6669
+ if (!host || label_len == 0) return -1;
6670
+ auto found = host->native_views.find(nativeViewKey(window_id, slice(label, label_len)));
6671
+ if (found == host->native_views.end() || found->second.kind != kViewGpuSurface || !found->second.hwnd) return -1;
6672
+ NativeView &view = found->second;
6673
+ if (view.gpu_backend_request == kGpuBackendRequestSoftware) return 0;
6674
+ auto owner = host->windows.find(view.window_id);
6675
+ /* WS_EX_LAYERED top-level windows are presented as one alpha bitmap;
6676
+ * Windows does not redirect child HWNDs into that bitmap. Preserve
6677
+ * exact alpha semantics through the existing pixel compositor. */
6678
+ if (owner != host->windows.end() && owner->second.transparent) return 0;
6679
+ if (!view.gpu_surface && host->gpu_renderer) view.gpu_surface = host->gpu_renderer->createSurface(view.hwnd);
6680
+ if (!view.gpu_surface) return 0;
6681
+
6682
+ WindowsGpuPacketPresent request;
6683
+ request.surface_width = surface_width;
6684
+ request.surface_height = surface_height;
6685
+ request.scale = scale;
6686
+ request.clear_rgba[0] = clear_r;
6687
+ request.clear_rgba[1] = clear_g;
6688
+ request.clear_rgba[2] = clear_b;
6689
+ request.clear_rgba[3] = clear_a;
6690
+ request.requires_render = requires_render != 0;
6691
+ request.command_count = command_count;
6692
+ request.unsupported_command_count = unsupported_command_count;
6693
+ request.representable = representable != 0;
6694
+ request.packet = packet;
6695
+ request.packet_len = packet_len;
6696
+ WindowsGpuPresentInfo info;
6697
+ const int result = view.gpu_surface->present(request, &info);
6698
+ if (result != 1) return result;
6699
+ view.gpu_backend = 1;
6700
+ view.gpu_packet_decode_ns = info.decode_ns;
6701
+ view.gpu_packet_draw_ns = info.draw_ns;
6702
+
6703
+ if (info.did_render) {
6704
+ /* The GPU target is now the sole glass baseline. Retire any old
6705
+ * software fallback buffer so device loss cannot repaint stale
6706
+ * pixels while the runtime is arranging a full resync. */
6707
+ view.gpu_bgra.clear();
6708
+ view.gpu_buf_width = 0;
6709
+ view.gpu_buf_height = 0;
6710
+ if (info.nonblank) {
6711
+ view.gpu_nonblank = 1;
6712
+ view.gpu_sample_color = info.sample_color;
6713
+ }
6714
+ if (owner != host->windows.end()) {
6715
+ syncHiddenCaptionColorFromPacket(
6716
+ host, owner->second, view, info, request.surface_width, request.surface_height);
6717
+ }
6718
+ if (info.dirty_rect_count == 0) {
6719
+ InvalidateRect(view.hwnd, nullptr, FALSE);
6720
+ } else {
6721
+ for (size_t index = 0; index < info.dirty_rect_count; ++index) {
6722
+ InvalidateRect(view.hwnd, &info.dirty_rects[index], FALSE);
6723
+ }
6724
+ }
6725
+ }
6726
+
6727
+ const bool first_present = !view.gpu_presented;
6728
+ view.gpu_presented = true;
6729
+ if (owner != host->windows.end() && !owner->second.shown) showWindowImplicit(owner->second);
6730
+ if (first_present) view.gpu_prompt_frame_pending = true;
6731
+ gpuSurfaceScheduleFrameEmission(host, view);
6732
+ return 1;
6733
+ }
6734
+
6735
+ int native_sdk_windows_upload_gpu_surface_image(Host *host, uint64_t id, size_t width, size_t height, const uint8_t *rgba8, size_t rgba8_len) {
6736
+ if (!host || width > UINT32_MAX || height > UINT32_MAX) return -1;
6737
+ /* Zero means the packet service is unavailable, matching the present
6738
+ * ABI. The Zig seam turns that into UnsupportedService so image-bearing
6739
+ * frames take the same CPU fallback as shape-only frames. */
6740
+ if (!host->gpu_renderer) return 0;
6741
+ return host->gpu_renderer->uploadImage(id, static_cast<uint32_t>(width), static_cast<uint32_t>(height), rgba8, rgba8_len) ? 1 : -1;
6742
+ }
6743
+
6744
+ int native_sdk_windows_remove_gpu_surface_image(Host *host, uint64_t id) {
6745
+ if (!host || !host->gpu_renderer) return 0;
6746
+ return host->gpu_renderer->removeImage(id) ? 1 : 0;
6747
+ }
6748
+
6749
+ int native_sdk_windows_register_gpu_surface_font(Host *host, uint64_t id, const uint8_t *ttf, size_t ttf_len, uint64_t *token) {
6750
+ if (!host) return -1;
6751
+ if (!host->gpu_renderer) return 0;
6752
+ if (host->gpu_renderer->registerFont(id, ttf, ttf_len, token)) return 1;
6753
+ /* IDs 1 and 2 are the required bundled faces registered during host
6754
+ * initialization, before any surface exists. If either is rejected,
6755
+ * packet text cannot match engine layout; retire the whole renderer so
6756
+ * later presents and custom registrations negotiate pixel fallback. */
6757
+ if (id == 1 || id == 2) host->gpu_renderer.reset();
6758
+ return -1;
6759
+ }
6760
+
6761
+ int native_sdk_windows_unregister_gpu_surface_font(Host *host, uint64_t id, uint64_t token) {
6762
+ if (!host) return -1;
6763
+ if (!host->gpu_renderer) return 0;
6764
+ return host->gpu_renderer->unregisterFont(id, token) ? 1 : -1;
6765
+ }
6766
+
6481
6767
  int native_sdk_windows_present_gpu_surface_pixels(Host *host, uint64_t window_id, const char *label, size_t label_len, size_t width, size_t height, double scale, int has_dirty_rect, double dirty_x, double dirty_y, double dirty_width, double dirty_height, const uint8_t *rgba8, size_t rgba8_len) {
6482
- (void)scale;
6483
- (void)has_dirty_rect;
6484
- (void)dirty_x;
6485
- (void)dirty_y;
6486
- (void)dirty_width;
6487
- (void)dirty_height;
6488
6768
  if (!host || label_len == 0) return 0;
6489
6769
  auto found = host->native_views.find(nativeViewKey(window_id, slice(label, label_len)));
6490
6770
  if (found == host->native_views.end() || found->second.kind != kViewGpuSurface || !found->second.hwnd) return 0;
@@ -6492,6 +6772,11 @@ int native_sdk_windows_present_gpu_surface_pixels(Host *host, uint64_t window_id
6492
6772
  if (width > INT_MAX || height > INT_MAX) return 0;
6493
6773
  if (rgba8_len != width * height * 4) return 0;
6494
6774
  NativeView &view = found->second;
6775
+ if (view.gpu_surface) view.gpu_surface->abandonContent();
6776
+ view.gpu_backend = 0;
6777
+ view.gpu_packet_decode_ns = 0;
6778
+ view.gpu_packet_draw_ns = 0;
6779
+ view.gpu_caption_sample_valid = false;
6495
6780
  auto owner = host->windows.find(view.window_id);
6496
6781
  const bool transparent_window = owner != host->windows.end() && owner->second.transparent;
6497
6782
 
@@ -6503,23 +6788,48 @@ int native_sdk_windows_present_gpu_surface_pixels(Host *host, uint64_t window_id
6503
6788
  * the redirection surface's alpha is 0 — the app's pixels must read
6504
6789
  * as opaque there or the whole header band would vanish under DWM
6505
6790
  * chrome (only the punched button hole yields on purpose). */
6791
+ const bool same_buffer = view.gpu_buf_width == (int)width && view.gpu_buf_height == (int)height &&
6792
+ view.gpu_bgra.size() == width * height * 4;
6793
+ const bool partial_update = has_dirty_rect != 0 && same_buffer && scale > 0 && std::isfinite(scale) &&
6794
+ std::isfinite(dirty_x) && std::isfinite(dirty_y) && std::isfinite(dirty_width) && std::isfinite(dirty_height);
6506
6795
  view.gpu_bgra.resize(width * height * 4);
6507
6796
  uint8_t *dst = view.gpu_bgra.data();
6508
- const size_t pixel_count = width * height;
6509
- for (size_t index = 0; index < pixel_count; index++) {
6510
- const uint8_t *src = rgba8 + index * 4;
6511
- const uint32_t alpha = transparent_window ? src[3] : 255;
6512
- dst[index * 4 + 0] = transparent_window ? (uint8_t)((src[2] * alpha + 127) / 255) : src[2];
6513
- dst[index * 4 + 1] = transparent_window ? (uint8_t)((src[1] * alpha + 127) / 255) : src[1];
6514
- dst[index * 4 + 2] = transparent_window ? (uint8_t)((src[0] * alpha + 127) / 255) : src[0];
6515
- dst[index * 4 + 3] = (uint8_t)alpha;
6797
+ size_t x0 = 0;
6798
+ size_t y0 = 0;
6799
+ size_t x1 = width;
6800
+ size_t y1 = height;
6801
+ RECT dirty_pixels = {};
6802
+ if (partial_update) {
6803
+ const double normalized_x0 = std::min(dirty_x, dirty_x + dirty_width);
6804
+ const double normalized_y0 = std::min(dirty_y, dirty_y + dirty_height);
6805
+ const double normalized_x1 = std::max(dirty_x, dirty_x + dirty_width);
6806
+ const double normalized_y1 = std::max(dirty_y, dirty_y + dirty_height);
6807
+ x0 = (size_t)std::max(0.0, std::min((double)width, std::floor(normalized_x0 * scale)));
6808
+ y0 = (size_t)std::max(0.0, std::min((double)height, std::floor(normalized_y0 * scale)));
6809
+ x1 = (size_t)std::max(0.0, std::min((double)width, std::ceil(normalized_x1 * scale)));
6810
+ y1 = (size_t)std::max(0.0, std::min((double)height, std::ceil(normalized_y1 * scale)));
6811
+ dirty_pixels = { (LONG)x0, (LONG)y0, (LONG)x1, (LONG)y1 };
6812
+ }
6813
+ /* Swizzle only the device-pixel rows touched by the retained CPU
6814
+ * raster. Unrepresentable commands still have an exact fallback,
6815
+ * but a 20x40 hover no longer converts a multi-megapixel surface. */
6816
+ for (size_t y_index = y0; y_index < y1; ++y_index) {
6817
+ for (size_t x_index = x0; x_index < x1; ++x_index) {
6818
+ const size_t index = y_index * width + x_index;
6819
+ const uint8_t *src = rgba8 + index * 4;
6820
+ const uint32_t alpha = transparent_window ? src[3] : 255;
6821
+ dst[index * 4 + 0] = transparent_window ? (uint8_t)((src[2] * alpha + 127) / 255) : src[2];
6822
+ dst[index * 4 + 1] = transparent_window ? (uint8_t)((src[1] * alpha + 127) / 255) : src[1];
6823
+ dst[index * 4 + 2] = transparent_window ? (uint8_t)((src[0] * alpha + 127) / 255) : src[0];
6824
+ dst[index * 4 + 3] = (uint8_t)alpha;
6825
+ }
6516
6826
  }
6517
6827
  view.gpu_buf_width = (int)width;
6518
6828
  view.gpu_buf_height = (int)height;
6519
6829
 
6520
6830
  /* Hidden-titlebar windows: keep the DWM caption material behind the
6521
6831
  * button cluster matched to the header the app just presented. */
6522
- if (owner != host->windows.end()) syncHiddenCaptionColor(host, owner->second, view, rgba8, width, height);
6832
+ if (owner != host->windows.end()) syncHiddenCaptionColor(owner->second, view, rgba8, width, height);
6523
6833
 
6524
6834
  const size_t sample_index = ((height / 2) * width + width / 2) * 4;
6525
6835
  const uint8_t sr = rgba8[sample_index + 0];
@@ -6532,7 +6842,7 @@ int native_sdk_windows_present_gpu_surface_pixels(Host *host, uint64_t window_id
6532
6842
  }
6533
6843
 
6534
6844
  const bool layered_presented = owner != host->windows.end() && presentTransparentWindow(host, owner->second);
6535
- if (!layered_presented) InvalidateRect(view.hwnd, nullptr, FALSE);
6845
+ if (!layered_presented) InvalidateRect(view.hwnd, partial_update ? &dirty_pixels : nullptr, FALSE);
6536
6846
  /* A present is the completion producer on the surface's single
6537
6847
  * frame-event scheduler: the completion event it arms is what
6538
6848
  * drives the runtime's frame loop (an armed animation presents,
@@ -145,6 +145,56 @@ pub fn languageFromFence(opening: []const u8) Language {
145
145
  return languageFromName(info[0..end]);
146
146
  }
147
147
 
148
+ /// Diff annotations share the read-only code paragraph's bounded logical
149
+ /// line model. A fixed ceiling keeps line metadata inline on the widget and
150
+ /// makes malformed markup fail before it can silently decorate the wrong
151
+ /// source row.
152
+ pub const max_diff_lines: usize = 128;
153
+
154
+ /// Parse a one-based comma/range list such as `2-4, 7`. Results are unique
155
+ /// and retain author order. Empty text means no annotated lines; zero,
156
+ /// descending ranges, values above `max_diff_lines`, and malformed pieces
157
+ /// are rejected.
158
+ pub fn parseLineNumberSpec(spec_raw: []const u8, storage: *[max_diff_lines]usize) ?[]const usize {
159
+ const spec = std.mem.trim(u8, spec_raw, " \t\r\n");
160
+ if (spec.len == 0) return storage[0..0];
161
+
162
+ var len: usize = 0;
163
+ var pieces = std.mem.splitScalar(u8, spec, ',');
164
+ while (pieces.next()) |piece_raw| {
165
+ const piece = std.mem.trim(u8, piece_raw, " \t\r\n");
166
+ if (piece.len == 0) return null;
167
+
168
+ const dash = std.mem.indexOfScalar(u8, piece, '-');
169
+ const first_text = std.mem.trim(u8, if (dash) |index| piece[0..index] else piece, " \t");
170
+ const last_text = if (dash) |index| std.mem.trim(u8, piece[index + 1 ..], " \t") else first_text;
171
+ if (first_text.len == 0 or last_text.len == 0) return null;
172
+ if (dash) |index| {
173
+ if (std.mem.indexOfScalar(u8, piece[index + 1 ..], '-') != null) return null;
174
+ }
175
+
176
+ const first = std.fmt.parseInt(usize, first_text, 10) catch return null;
177
+ const last = std.fmt.parseInt(usize, last_text, 10) catch return null;
178
+ if (first == 0 or last < first or last > max_diff_lines) return null;
179
+
180
+ var line = first;
181
+ while (line <= last) : (line += 1) {
182
+ var duplicate = false;
183
+ for (storage[0..len]) |existing| {
184
+ if (existing == line) {
185
+ duplicate = true;
186
+ break;
187
+ }
188
+ }
189
+ if (duplicate) continue;
190
+ if (len == storage.len) return null;
191
+ storage[len] = line;
192
+ len += 1;
193
+ }
194
+ }
195
+ return storage[0..len];
196
+ }
197
+
148
198
  fn wordInList(word: []const u8, list: []const u8, ignore_case: bool) bool {
149
199
  var words = std.mem.tokenizeScalar(u8, list, ' ');
150
200
  while (words.next()) |candidate| {