@native-sdk/cli 0.7.0 → 0.7.2

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 (81) hide show
  1. package/build/app.zig +8 -0
  2. package/build.zig +114 -103
  3. package/native-sdk.d.ts +6 -3
  4. package/package.json +9 -9
  5. package/packages/core/package-lock.json +2 -2
  6. package/packages/core/package.json +1 -1
  7. package/packages/core/sdk/text.ts +25 -14
  8. package/packages/core/src/checker.ts +39 -0
  9. package/packages/core/src/emitter.ts +125 -1
  10. package/skill-data/native-ui/SKILL.md +11 -7
  11. package/src/platform/macos/appkit_host.m +76 -4
  12. package/src/platform/macos/cef_host.mm +24 -2
  13. package/src/platform/macos/root.zig +13 -0
  14. package/src/platform/types.zig +4 -0
  15. package/src/platform/windows/gpu_surface_renderer.cpp +2280 -0
  16. package/src/platform/windows/gpu_surface_renderer.h +86 -0
  17. package/src/platform/windows/root.zig +298 -20
  18. package/src/platform/windows/webview2_host.cpp +343 -33
  19. package/src/primitives/canvas/code.zig +501 -33
  20. package/src/primitives/canvas/code_tests.zig +1029 -6
  21. package/src/primitives/canvas/events.zig +111 -1
  22. package/src/primitives/canvas/frame.zig +45 -1
  23. package/src/primitives/canvas/gpu.zig +6 -0
  24. package/src/primitives/canvas/markdown_tests.zig +17 -18
  25. package/src/primitives/canvas/render_tests.zig +93 -0
  26. package/src/primitives/canvas/root.zig +6 -0
  27. package/src/primitives/canvas/serialization.zig +150 -21
  28. package/src/primitives/canvas/text.zig +6 -0
  29. package/src/primitives/canvas/themes/geist.zig +51 -5
  30. package/src/primitives/canvas/tokens.zig +33 -4
  31. package/src/primitives/canvas/ui.zig +160 -36
  32. package/src/primitives/canvas/ui_markup.zig +60 -9
  33. package/src/primitives/canvas/ui_markup_compiled.zig +41 -0
  34. package/src/primitives/canvas/ui_markup_compiled_tests.zig +12 -0
  35. package/src/primitives/canvas/ui_markup_contract.zig +17 -1
  36. package/src/primitives/canvas/ui_markup_view.zig +44 -0
  37. package/src/primitives/canvas/ui_markup_view_tests.zig +80 -6
  38. package/src/primitives/canvas/ui_schema.zig +18 -0
  39. package/src/primitives/canvas/ui_schema_tests.zig +10 -7
  40. package/src/primitives/canvas/ui_tests.zig +40 -0
  41. package/src/primitives/canvas/widget_builtin_tests.zig +513 -13
  42. package/src/primitives/canvas/widget_invalidation.zig +12 -1
  43. package/src/primitives/canvas/widget_layout.zig +260 -21
  44. package/src/primitives/canvas/widget_metrics.zig +47 -6
  45. package/src/primitives/canvas/widget_render.zig +849 -78
  46. package/src/primitives/canvas/widget_render_controls.zig +83 -25
  47. package/src/primitives/canvas/widget_render_style.zig +52 -8
  48. package/src/primitives/canvas/widget_runtime.zig +2 -0
  49. package/src/primitives/canvas/widget_text_input.zig +167 -8
  50. package/src/primitives/canvas/widget_tree.zig +6 -5
  51. package/src/primitives/canvas/widgets.zig +78 -2
  52. package/src/runtime/canvas_budget_edge_tests.zig +3 -2
  53. package/src/runtime/canvas_frame.zig +60 -7
  54. package/src/runtime/canvas_frame_invalidation_tests.zig +80 -0
  55. package/src/runtime/canvas_frame_packet_tests.zig +83 -9
  56. package/src/runtime/canvas_frame_patch_tests.zig +16 -0
  57. package/src/runtime/canvas_images.zig +8 -2
  58. package/src/runtime/canvas_limits.zig +13 -6
  59. package/src/runtime/canvas_widget_clipboard_tests.zig +8 -5
  60. package/src/runtime/canvas_widget_context_menu.zig +7 -2
  61. package/src/runtime/canvas_widget_event_tests.zig +18 -14
  62. package/src/runtime/canvas_widget_events.zig +51 -20
  63. package/src/runtime/canvas_widget_runtime.zig +108 -4
  64. package/src/runtime/canvas_widget_scroll_driver_tests.zig +59 -0
  65. package/src/runtime/canvas_widget_split_tree_tests.zig +100 -0
  66. package/src/runtime/canvas_widget_state.zig +27 -0
  67. package/src/runtime/canvas_widget_text_tests.zig +326 -0
  68. package/src/runtime/core.zig +15 -5
  69. package/src/runtime/effects.zig +6 -0
  70. package/src/runtime/effects_tests.zig +30 -0
  71. package/src/runtime/gpu_surface_events.zig +50 -19
  72. package/src/runtime/media_surface_tests.zig +1 -1
  73. package/src/runtime/runtime_event_tests.zig +74 -0
  74. package/src/runtime/ui_app.zig +10 -8
  75. package/src/runtime/ui_app_tests.zig +67 -0
  76. package/src/runtime/view.zig +60 -16
  77. package/src/runtime/view_canvas.zig +15 -0
  78. package/src/runtime/view_widget_scroll.zig +48 -18
  79. package/src/runtime/view_widget_text.zig +59 -16
  80. package/src/runtime/window_view_runtime.zig +10 -0
  81. package/src/tooling/templates.zig +9 -6
@@ -0,0 +1,86 @@
1
+ #pragma once
2
+
3
+ #include <windows.h>
4
+
5
+ #include <stddef.h>
6
+ #include <stdint.h>
7
+
8
+ #include <memory>
9
+
10
+ /*
11
+ * The Win32 window/input host deliberately sees the canvas compositor
12
+ * through this small interface. Direct2D/DirectWrite objects and the
13
+ * retained binary-packet model stay in gpu_surface_renderer.cpp, while
14
+ * webview2_host.cpp owns only one shared renderer and one surface per
15
+ * gpu_surface HWND.
16
+ */
17
+
18
+ struct WindowsGpuPacketPresent {
19
+ double surface_width = 0;
20
+ double surface_height = 0;
21
+ double scale = 1;
22
+ uint8_t clear_rgba[4] = {};
23
+ bool requires_render = false;
24
+ size_t command_count = 0;
25
+ size_t unsupported_command_count = 0;
26
+ bool representable = false;
27
+ const uint8_t *packet = nullptr;
28
+ size_t packet_len = 0;
29
+ };
30
+
31
+ constexpr size_t kWindowsGpuDirtyRectCap = 8;
32
+
33
+ struct WindowsGpuPresentInfo {
34
+ bool did_render = false;
35
+ bool nonblank = false;
36
+ uint32_t sample_color = 0;
37
+ uint64_t decode_ns = 0;
38
+ uint64_t draw_ns = 0;
39
+ size_t dirty_rect_count = 0;
40
+ RECT dirty_rects[kWindowsGpuDirtyRectCap] = {};
41
+ };
42
+
43
+ class WindowsGpuSurface {
44
+ public:
45
+ virtual ~WindowsGpuSurface() = default;
46
+
47
+ /* 1 accepted, 0 refused (runtime resync/pixel fallback), -1 device
48
+ * presentation failure. The packet is bounds-checked before any
49
+ * retained state or GPU content is mutated. */
50
+ virtual int present(const WindowsGpuPacketPresent &present, WindowsGpuPresentInfo *info) = 0;
51
+
52
+ /* Paint the retained GPU bitmap into the exact invalid rectangles.
53
+ * The Win32 update region may remain disjoint after several packet
54
+ * invalidations; keeping that shape avoids turning two small patches
55
+ * into one window-sized copy. false means the Direct2D resource domain
56
+ * was lost and the next packet must be a full resync. */
57
+ virtual bool paint(const RECT *paint_rects, size_t paint_rect_count) = 0;
58
+
59
+ /* A software fallback became the glass baseline. Drop both GPU
60
+ * content and retained packet state so a later packet must resync. */
61
+ virtual void abandonContent() = 0;
62
+
63
+ virtual bool hasContent() const = 0;
64
+ /* Read the actual opaque backing pixel at a logical point. This is an
65
+ * explicit synchronization point and is reserved for the hidden-titlebar
66
+ * caption sample; ordinary presentation diagnostics stay on the retained
67
+ * command heuristic below. */
68
+ virtual bool readColorAt(double logical_x, double logical_y, uint32_t *color) = 0;
69
+ virtual uint32_t representativeColorAt(double logical_x, double logical_y) const = 0;
70
+ };
71
+
72
+ class WindowsGpuRenderer {
73
+ public:
74
+ virtual ~WindowsGpuRenderer() = default;
75
+
76
+ virtual std::shared_ptr<WindowsGpuSurface> createSurface(HWND hwnd) = 0;
77
+ virtual bool uploadImage(uint64_t id, uint32_t width, uint32_t height, const uint8_t *rgba, size_t rgba_len) = 0;
78
+ virtual bool removeImage(uint64_t id) = 0;
79
+
80
+ /* A token makes replacement/unregister ordering safe when multiple
81
+ * runtimes reuse an id, matching the platform service contract. */
82
+ virtual bool registerFont(uint64_t id, const uint8_t *ttf, size_t ttf_len, uint64_t *token) = 0;
83
+ virtual bool unregisterFont(uint64_t id, uint64_t token) = 0;
84
+ };
85
+
86
+ std::shared_ptr<WindowsGpuRenderer> createWindowsGpuRenderer();
@@ -4,6 +4,9 @@ const platform_mod = @import("../root.zig");
4
4
  const policy_values = @import("../policy_values.zig");
5
5
  const security = @import("../../security/root.zig");
6
6
 
7
+ const bundled_geist_regular = @embedFile("../../primitives/canvas/fonts/Geist-Regular.ttf");
8
+ const bundled_geist_mono = @embedFile("../../primitives/canvas/fonts/GeistMono-Regular.ttf");
9
+
7
10
  pub const Error = error{
8
11
  CallbackFailed,
9
12
  CreateFailed,
@@ -73,10 +76,19 @@ const WindowsEvent = extern struct {
73
76
  frame_interval_ns: u64,
74
77
  nonblank: c_int,
75
78
  sample_color: u32,
79
+ /// Concrete Windows presenter plus the most recent accepted packet's
80
+ /// decode/draw durations. The host clears timings before dispatch so
81
+ /// a synchronous next present cannot be clobbered after the callback.
82
+ gpu_backend: c_int,
83
+ packet_decode_ns: u64,
84
+ packet_draw_ns: u64,
76
85
  /// Nonzero when the frame completed logically while the top-level
77
86
  /// window was minimized (heartbeat pacing; nothing painted) — its
78
87
  /// timestamp is pacing policy, never a latency endpoint.
79
88
  occluded: c_int,
89
+ /// Nonzero when the Direct2D presenter lost its retained resources and
90
+ /// this completion must rebuild the canvas from a full packet.
91
+ force_full_repaint: c_int,
80
92
  input_kind: c_int,
81
93
  button: c_int,
82
94
  delta_x: f64,
@@ -149,7 +161,7 @@ extern fn native_sdk_windows_close_window(host: *WindowsHost, window_id: u64) c_
149
161
  extern fn native_sdk_windows_minimize_window(host: *WindowsHost, window_id: u64) c_int;
150
162
  extern fn native_sdk_windows_show_window(host: *WindowsHost, window_id: u64) c_int;
151
163
  extern fn native_sdk_windows_set_window_close_policy(host: *WindowsHost, window_id: u64, close_policy: c_int) c_int;
152
- extern fn native_sdk_windows_create_view(host: *WindowsHost, window_id: u64, label: [*]const u8, label_len: usize, kind: c_int, parent: [*]const u8, parent_len: usize, x: f64, y: f64, width: f64, height: f64, layer: c_int, visible: c_int, enabled: c_int, role: [*]const u8, role_len: usize, accessibility_label: [*]const u8, accessibility_label_len: usize, text: [*]const u8, text_len: usize, command: [*]const u8, command_len: usize) c_int;
164
+ extern fn native_sdk_windows_create_view(host: *WindowsHost, window_id: u64, label: [*]const u8, label_len: usize, kind: c_int, gpu_backend_request: c_int, parent: [*]const u8, parent_len: usize, x: f64, y: f64, width: f64, height: f64, layer: c_int, visible: c_int, enabled: c_int, role: [*]const u8, role_len: usize, accessibility_label: [*]const u8, accessibility_label_len: usize, text: [*]const u8, text_len: usize, command: [*]const u8, command_len: usize) c_int;
153
165
  extern fn native_sdk_windows_update_view(host: *WindowsHost, window_id: u64, label: [*]const u8, label_len: usize, has_frame: c_int, x: f64, y: f64, width: f64, height: f64, has_layer: c_int, layer: c_int, has_visible: c_int, visible: c_int, has_enabled: c_int, enabled: c_int, has_role: c_int, role: [*]const u8, role_len: usize, has_accessibility_label: c_int, accessibility_label: [*]const u8, accessibility_label_len: usize, has_text: c_int, text: [*]const u8, text_len: usize, has_command: c_int, command: [*]const u8, command_len: usize) c_int;
154
166
  extern fn native_sdk_windows_set_view_frame(host: *WindowsHost, window_id: u64, label: [*]const u8, label_len: usize, x: f64, y: f64, width: f64, height: f64) c_int;
155
167
  extern fn native_sdk_windows_set_view_visible(host: *WindowsHost, window_id: u64, label: [*]const u8, label_len: usize, visible: c_int) c_int;
@@ -158,6 +170,11 @@ extern fn native_sdk_windows_close_view(host: *WindowsHost, window_id: u64, labe
158
170
  extern fn native_sdk_windows_request_gpu_surface_frame(host: *WindowsHost, window_id: u64, label: [*]const u8, label_len: usize) c_int;
159
171
  extern fn native_sdk_windows_note_gpu_surface_input(host: *WindowsHost, window_id: u64, label: [*]const u8, label_len: usize) c_int;
160
172
  extern fn native_sdk_windows_present_gpu_surface_pixels(host: *WindowsHost, window_id: u64, label: [*]const u8, label_len: usize, width: usize, height: usize, scale: f64, has_dirty_rect: c_int, dirty_x: f64, dirty_y: f64, dirty_width: f64, dirty_height: f64, rgba8: [*]const u8, rgba8_len: usize) c_int;
173
+ extern fn native_sdk_windows_present_gpu_surface_packet_binary(host: *WindowsHost, window_id: u64, label: [*]const u8, label_len: usize, surface_width: f64, surface_height: f64, scale: f64, clear_r: u8, clear_g: u8, clear_b: u8, clear_a: u8, requires_render: c_int, command_count: usize, unsupported_command_count: usize, representable: c_int, packet: [*]const u8, packet_len: usize) c_int;
174
+ extern fn native_sdk_windows_upload_gpu_surface_image(host: *WindowsHost, id: u64, width: usize, height: usize, rgba8: [*]const u8, rgba8_len: usize) c_int;
175
+ extern fn native_sdk_windows_remove_gpu_surface_image(host: *WindowsHost, id: u64) c_int;
176
+ extern fn native_sdk_windows_register_gpu_surface_font(host: *WindowsHost, id: u64, ttf: [*]const u8, ttf_len: usize, token: *u64) c_int;
177
+ extern fn native_sdk_windows_unregister_gpu_surface_font(host: *WindowsHost, id: u64, token: u64) c_int;
161
178
  extern fn native_sdk_windows_create_webview(host: *WindowsHost, window_id: u64, label: [*]const u8, label_len: usize, url: [*]const u8, url_len: usize, x: f64, y: f64, width: f64, height: f64, layer: c_int, transparent: c_int, bridge_enabled: c_int) c_int;
162
179
  extern fn native_sdk_windows_set_webview_frame(host: *WindowsHost, window_id: u64, label: [*]const u8, label_len: usize, x: f64, y: f64, width: f64, height: f64) c_int;
163
180
  extern fn native_sdk_windows_navigate_webview(host: *WindowsHost, window_id: u64, label: [*]const u8, label_len: usize, url: [*]const u8, url_len: usize) c_int;
@@ -321,6 +338,18 @@ pub const WindowsPlatform = struct {
321
338
  const window_title = window_options.resolvedTitle(app_info.app_name);
322
339
  const frame = window_options.default_frame;
323
340
  const host = native_sdk_windows_create(app_info.app_name.ptr, app_info.app_name.len, window_title.ptr, window_title.len, app_info.bundle_id.ptr, app_info.bundle_id.len, app_info.icon_path.ptr, app_info.icon_path.len, window_options.label.ptr, window_options.label.len, frame.x, frame.y, frame.width, frame.height, if (window_options.restore_state) 1 else 0, if (window_options.resizable) 1 else 0, titlebarStyleInt(window_options.titlebar), minSizeFloor(window_options.min_width), minSizeFloor(window_options.min_height), showModeInt(window_options.show), windowFlags(window_options)) orelse return error.CreateFailed;
341
+ // Packet text must use the same Geist metrics the engine planned
342
+ // against. Register both built-in faces directly from their
343
+ // compile-time bytes; custom application fonts keep the public
344
+ // side-channel and ids >= 64. If either required face cannot be
345
+ // registered, disable packet rendering as one capability and keep
346
+ // the exact reference-pixel fallback — never draw system faces
347
+ // against Geist-planned layout.
348
+ if (web_engine == .system) {
349
+ var font_token: u64 = 0;
350
+ _ = native_sdk_windows_register_gpu_surface_font(host, 1, bundled_geist_regular.ptr, bundled_geist_regular.len, &font_token);
351
+ _ = native_sdk_windows_register_gpu_surface_font(host, 2, bundled_geist_mono.ptr, bundled_geist_mono.len, &font_token);
352
+ }
324
353
  // The manifest's declared close policy rides right after the
325
354
  // create, like the min-size floor: close handling is host
326
355
  // window state fixed for the window's life.
@@ -414,6 +443,11 @@ pub const WindowsPlatform = struct {
414
443
  .request_gpu_surface_frame_fn = requestGpuSurfaceFrame,
415
444
  .note_gpu_surface_input_fn = noteGpuSurfaceInput,
416
445
  .present_gpu_surface_pixels_fn = presentGpuSurfacePixels,
446
+ .present_gpu_surface_packet_binary_fn = presentGpuSurfacePacketBinary,
447
+ .upload_gpu_surface_image_fn = uploadGpuSurfaceImage,
448
+ .remove_gpu_surface_image_fn = removeGpuSurfaceImage,
449
+ .register_gpu_surface_font_fn = registerGpuSurfaceFont,
450
+ .unregister_gpu_surface_font_fn = unregisterGpuSurfaceFont,
417
451
  .show_context_menu_fn = showContextMenu,
418
452
  .create_webview_fn = createWebView,
419
453
  .set_webview_frame_fn = setWebViewFrame,
@@ -627,25 +661,7 @@ fn windowsCallback(context: ?*anyopaque, event: *const WindowsEvent) callconv(.c
627
661
  .window_id = event.window_id,
628
662
  } }),
629
663
  .tray_action => state.emit(.{ .tray_action = event.tray_item_id }),
630
- .gpu_surface_frame => state.emit(.{ .gpu_surface_frame = .{
631
- .window_id = event.window_id,
632
- .label = event.view_label[0..event.view_label_len],
633
- .size = geometry.SizeF.init(@floatCast(event.width), @floatCast(event.height)),
634
- .scale_factor = @floatCast(event.scale),
635
- .frame_index = event.frame_index,
636
- .timestamp_ns = event.timestamp_ns,
637
- .frame_interval_ns = event.frame_interval_ns,
638
- .nonblank = event.nonblank != 0,
639
- .sample_color = event.sample_color,
640
- .occluded = event.occluded != 0,
641
- .backend = .software,
642
- .pixel_format = .bgra8_unorm,
643
- .present_mode = .timer,
644
- .alpha_mode = .@"opaque",
645
- .color_space = .srgb,
646
- .vsync = true,
647
- .status = .ready,
648
- } }),
664
+ .gpu_surface_frame => state.emit(.{ .gpu_surface_frame = gpuSurfaceFrameEventFromWindowsEvent(event) }),
649
665
  .gpu_surface_resize => state.emit(.{ .gpu_surface_resized = .{
650
666
  .window_id = event.window_id,
651
667
  .label = event.view_label[0..event.view_label_len],
@@ -675,6 +691,31 @@ fn windowsCallback(context: ?*anyopaque, event: *const WindowsEvent) callconv(.c
675
691
  }
676
692
  }
677
693
 
694
+ fn gpuSurfaceFrameEventFromWindowsEvent(event: *const WindowsEvent) platform_mod.GpuSurfaceFrameEvent {
695
+ return .{
696
+ .window_id = event.window_id,
697
+ .label = event.view_label[0..event.view_label_len],
698
+ .size = geometry.SizeF.init(@floatCast(event.width), @floatCast(event.height)),
699
+ .scale_factor = @floatCast(event.scale),
700
+ .frame_index = event.frame_index,
701
+ .timestamp_ns = event.timestamp_ns,
702
+ .frame_interval_ns = event.frame_interval_ns,
703
+ .nonblank = event.nonblank != 0,
704
+ .sample_color = event.sample_color,
705
+ .packet_decode_ns = event.packet_decode_ns,
706
+ .packet_draw_ns = event.packet_draw_ns,
707
+ .occluded = event.occluded != 0,
708
+ .backend = if (event.gpu_backend == 1) .direct2d else .software,
709
+ .pixel_format = .bgra8_unorm,
710
+ .present_mode = .timer,
711
+ .alpha_mode = .@"opaque",
712
+ .color_space = .srgb,
713
+ .vsync = true,
714
+ .status = .ready,
715
+ .canvas_frame_full_repaint = event.force_full_repaint != 0,
716
+ };
717
+ }
718
+
678
719
  /// Pure event mapping (no host calls), unit-testable on every build
679
720
  /// host: the C event's `widget_id` is the request's correlation token
680
721
  /// and `menu_item_id` the selected item (0 = dismissed) — the same
@@ -1040,6 +1081,7 @@ fn createView(context: ?*anyopaque, options: platform_mod.ViewOptions) anyerror!
1040
1081
  options.label.ptr,
1041
1082
  options.label.len,
1042
1083
  viewKindInt(options.kind),
1084
+ if (options.kind == .gpu_surface) gpuSurfaceBackendRequestInt(options.gpu_surface.backend) else 0,
1043
1085
  parent.ptr,
1044
1086
  parent.len,
1045
1087
  frame.x,
@@ -1158,6 +1200,91 @@ fn presentGpuSurfacePixels(context: ?*anyopaque, pixels: platform_mod.GpuSurface
1158
1200
  ) == 0) return error.ViewNotFound;
1159
1201
  }
1160
1202
 
1203
+ fn presentGpuSurfacePacketBinary(context: ?*anyopaque, packet: platform_mod.GpuSurfacePacket) anyerror!void {
1204
+ const self: *WindowsPlatform = @ptrCast(@alignCast(context.?));
1205
+ if (self.web_engine != .system) return error.UnsupportedService;
1206
+ const result = native_sdk_windows_present_gpu_surface_packet_binary(
1207
+ self.host,
1208
+ packet.window_id,
1209
+ packet.label.ptr,
1210
+ packet.label.len,
1211
+ packet.surface_size.width,
1212
+ packet.surface_size.height,
1213
+ packet.scale_factor,
1214
+ packet.clear_color_rgba8[0],
1215
+ packet.clear_color_rgba8[1],
1216
+ packet.clear_color_rgba8[2],
1217
+ packet.clear_color_rgba8[3],
1218
+ if (packet.requires_render) 1 else 0,
1219
+ packet.command_count,
1220
+ packet.unsupported_command_count,
1221
+ if (packet.representable) 1 else 0,
1222
+ packet.binary.ptr,
1223
+ packet.binary.len,
1224
+ );
1225
+ switch (result) {
1226
+ 1 => return,
1227
+ 0 => return error.UnsupportedService,
1228
+ -1 => return error.ViewNotFound,
1229
+ else => return error.InvalidGpuSurfacePacket,
1230
+ }
1231
+ }
1232
+
1233
+ fn uploadGpuSurfaceImage(context: ?*anyopaque, image: platform_mod.GpuSurfaceImagePixels) anyerror!void {
1234
+ const self: *WindowsPlatform = @ptrCast(@alignCast(context.?));
1235
+ if (self.web_engine != .system) return error.UnsupportedService;
1236
+ return gpuSurfaceImageUploadResult(native_sdk_windows_upload_gpu_surface_image(
1237
+ self.host,
1238
+ image.id,
1239
+ image.width,
1240
+ image.height,
1241
+ image.rgba8.ptr,
1242
+ image.rgba8.len,
1243
+ ));
1244
+ }
1245
+
1246
+ fn gpuSurfaceImageUploadResult(result: c_int) anyerror!void {
1247
+ switch (result) {
1248
+ 1 => return,
1249
+ 0 => return error.UnsupportedService,
1250
+ else => return error.InvalidGpuSurfaceImage,
1251
+ }
1252
+ }
1253
+
1254
+ fn removeGpuSurfaceImage(context: ?*anyopaque, id: u64) anyerror!void {
1255
+ const self: *WindowsPlatform = @ptrCast(@alignCast(context.?));
1256
+ if (self.web_engine != .system) return error.UnsupportedService;
1257
+ if (native_sdk_windows_remove_gpu_surface_image(self.host, id) == 0) return error.InvalidGpuSurfaceImage;
1258
+ }
1259
+
1260
+ fn registerGpuSurfaceFont(context: ?*anyopaque, font: platform_mod.GpuSurfaceFontData) anyerror!u64 {
1261
+ const self: *WindowsPlatform = @ptrCast(@alignCast(context.?));
1262
+ if (self.web_engine != .system) return error.UnsupportedService;
1263
+ var token: u64 = 0;
1264
+ return gpuSurfaceFontRegistrationResult(
1265
+ native_sdk_windows_register_gpu_surface_font(self.host, font.id, font.ttf.ptr, font.ttf.len, &token),
1266
+ token,
1267
+ );
1268
+ }
1269
+
1270
+ fn gpuSurfaceFontRegistrationResult(result: c_int, token: u64) anyerror!u64 {
1271
+ return switch (result) {
1272
+ 1 => token,
1273
+ 0 => error.UnsupportedService,
1274
+ else => error.InvalidGpuSurfaceFont,
1275
+ };
1276
+ }
1277
+
1278
+ fn unregisterGpuSurfaceFont(context: ?*anyopaque, id: u64, token: u64) anyerror!void {
1279
+ const self: *WindowsPlatform = @ptrCast(@alignCast(context.?));
1280
+ if (self.web_engine != .system) return error.UnsupportedService;
1281
+ switch (native_sdk_windows_unregister_gpu_surface_font(self.host, id, token)) {
1282
+ 1 => return,
1283
+ 0 => return error.UnsupportedService,
1284
+ else => return error.InvalidGpuSurfaceFont,
1285
+ }
1286
+ }
1287
+
1161
1288
  /// Win32 menus treat `&` in an item label as a mnemonic marker —
1162
1289
  /// AppendMenuW eats the ampersand and underlines the next character —
1163
1290
  /// so an app-authored label like "R&D" must cross the ABI with the
@@ -1668,6 +1795,25 @@ test "windows supports native container and control kinds" {
1668
1795
  try std.testing.expect(isSupportedNativeViewKind(.gpu_surface));
1669
1796
  }
1670
1797
 
1798
+ test "windows GPU frame maps Direct2D recovery to a full repaint" {
1799
+ const label = "canvas";
1800
+ var event = std.mem.zeroes(WindowsEvent);
1801
+ event.window_id = 7;
1802
+ event.view_label = label.ptr;
1803
+ event.view_label_len = label.len;
1804
+ event.width = 640;
1805
+ event.height = 360;
1806
+ event.scale = 2;
1807
+ event.gpu_backend = 1;
1808
+ event.force_full_repaint = 1;
1809
+
1810
+ const frame = gpuSurfaceFrameEventFromWindowsEvent(&event);
1811
+ try std.testing.expectEqual(@as(platform_mod.WindowId, 7), frame.window_id);
1812
+ try std.testing.expectEqualStrings(label, frame.label);
1813
+ try std.testing.expectEqual(platform_mod.GpuSurfaceBackend.direct2d, frame.backend);
1814
+ try std.testing.expect(frame.canvas_frame_full_repaint);
1815
+ }
1816
+
1671
1817
  test "windows gpu surface input preserves key and text" {
1672
1818
  const label = "canvas";
1673
1819
  const key = "enter";
@@ -1919,6 +2065,134 @@ test "windows passive canvas creation does not focus its child hwnd" {
1919
2065
  ) != null);
1920
2066
  }
1921
2067
 
2068
+ test "windows software GPU backend request stays on the pixel presenter" {
2069
+ try std.testing.expectEqual(@as(c_int, 0), gpuSurfaceBackendRequestInt(.metal));
2070
+ try std.testing.expectEqual(@as(c_int, 1), gpuSurfaceBackendRequestInt(.software));
2071
+
2072
+ const host_source = @embedFile("webview2_host.cpp");
2073
+ try std.testing.expect(std.mem.indexOf(
2074
+ u8,
2075
+ host_source,
2076
+ "view.gpu_backend_request != kGpuBackendRequestSoftware",
2077
+ ) != null);
2078
+ try std.testing.expect(std.mem.indexOf(
2079
+ u8,
2080
+ host_source,
2081
+ "if (view.gpu_backend_request == kGpuBackendRequestSoftware) return 0;",
2082
+ ) != null);
2083
+ }
2084
+
2085
+ test "windows unavailable image uploader negotiates pixel fallback" {
2086
+ try gpuSurfaceImageUploadResult(1);
2087
+ try std.testing.expectError(error.UnsupportedService, gpuSurfaceImageUploadResult(0));
2088
+ try std.testing.expectError(error.InvalidGpuSurfaceImage, gpuSurfaceImageUploadResult(-1));
2089
+
2090
+ const host_source = @embedFile("webview2_host.cpp");
2091
+ try std.testing.expect(std.mem.indexOf(
2092
+ u8,
2093
+ host_source,
2094
+ "if (!host->gpu_renderer) return 0;",
2095
+ ) != null);
2096
+ }
2097
+
2098
+ test "windows packet renderer requires deterministic font and caption seams" {
2099
+ try std.testing.expectEqual(@as(u64, 37), try gpuSurfaceFontRegistrationResult(1, 37));
2100
+ try std.testing.expectError(error.UnsupportedService, gpuSurfaceFontRegistrationResult(0, 0));
2101
+ try std.testing.expectError(error.InvalidGpuSurfaceFont, gpuSurfaceFontRegistrationResult(-1, 0));
2102
+
2103
+ const renderer_source = @embedFile("gpu_surface_renderer.cpp");
2104
+ try std.testing.expect(std.mem.indexOf(
2105
+ u8,
2106
+ renderer_source,
2107
+ "fallback_builder->CreateFontFallback(&font_fallback_)",
2108
+ ) != null);
2109
+ try std.testing.expect(std.mem.indexOf(
2110
+ u8,
2111
+ renderer_source,
2112
+ "layout2->SetFontFallback(renderer_->fontFallback())",
2113
+ ) != null);
2114
+ try std.testing.expect(std.mem.indexOf(
2115
+ u8,
2116
+ renderer_source,
2117
+ "D2D1_COMPATIBLE_RENDER_TARGET_OPTIONS_GDI_COMPATIBLE",
2118
+ ) != null);
2119
+
2120
+ const host_source = @embedFile("webview2_host.cpp");
2121
+ try std.testing.expect(std.mem.indexOf(
2122
+ u8,
2123
+ host_source,
2124
+ "if (id == 1 || id == 2) host->gpu_renderer.reset();",
2125
+ ) != null);
2126
+ try std.testing.expect(std.mem.indexOf(
2127
+ u8,
2128
+ host_source,
2129
+ "view.gpu_surface->readColorAt(sample_x, sample_y, &packed)",
2130
+ ) != null);
2131
+ }
2132
+
2133
+ test "windows packet renderer preserves text baselines and disjoint dirty regions" {
2134
+ const renderer_source = @embedFile("gpu_surface_renderer.cpp");
2135
+ try std.testing.expect(std.mem.indexOf(
2136
+ u8,
2137
+ renderer_source,
2138
+ "draw_line(text.text, text.origin.x, text.origin.y)",
2139
+ ) != null);
2140
+ try std.testing.expect(std.mem.indexOf(
2141
+ u8,
2142
+ renderer_source,
2143
+ "backing_target_->DrawGlyphRun(\n D2D1::Point2F(glyph.x, glyph.baseline)",
2144
+ ) != null);
2145
+
2146
+ const draw_list_at = std.mem.indexOf(
2147
+ u8,
2148
+ renderer_source,
2149
+ "bool drawCommandList(const std::vector<const Command *> &commands",
2150
+ ) orelse return error.TestExpectedEqual;
2151
+ const draw_list = renderer_source[draw_list_at..];
2152
+ const blur_target_at = std.mem.indexOf(
2153
+ u8,
2154
+ draw_list,
2155
+ "const Rect target = blurTarget(*command, outer_clip);",
2156
+ ) orelse return error.TestExpectedEqual;
2157
+ const segment_end_at = std.mem.indexOf(
2158
+ u8,
2159
+ draw_list,
2160
+ "const HRESULT segment = backing_target_->EndDraw();",
2161
+ ) orelse return error.TestExpectedEqual;
2162
+ try std.testing.expect(blur_target_at < segment_end_at);
2163
+
2164
+ const host_source = @embedFile("webview2_host.cpp");
2165
+ try std.testing.expect(std.mem.indexOf(
2166
+ u8,
2167
+ host_source,
2168
+ "gpuSurfaceUpdateRegionRects(\n hwnd, paint_rects, kGpuPaintRegionRectCap)",
2169
+ ) != null);
2170
+ try std.testing.expect(std.mem.indexOf(
2171
+ u8,
2172
+ host_source,
2173
+ "view->gpu_surface->paint(paint_rects, paint_rect_count)",
2174
+ ) != null);
2175
+ try std.testing.expect(std.mem.indexOf(
2176
+ u8,
2177
+ host_source,
2178
+ "InvalidateRect(view.hwnd, &info.dirty_rects[index], FALSE)",
2179
+ ) != null);
2180
+ }
2181
+
2182
+ test "windows packet renderer keeps square rectangle stroke joins" {
2183
+ const renderer_source = @embedFile("gpu_surface_renderer.cpp");
2184
+ try std.testing.expect(std.mem.indexOf(
2185
+ u8,
2186
+ renderer_source,
2187
+ "style.lineJoin = D2D1_LINE_JOIN_MITER;",
2188
+ ) != null);
2189
+ try std.testing.expect(std.mem.indexOf(
2190
+ u8,
2191
+ renderer_source,
2192
+ "command.shape.kind == Shape::Kind::stroke_rect\n ? rect_stroke_",
2193
+ ) != null);
2194
+ }
2195
+
1922
2196
  test "windows click-through uses a layered surface even when visually opaque" {
1923
2197
  const host_source = @embedFile("webview2_host.cpp");
1924
2198
  try std.testing.expect(std.mem.indexOf(
@@ -2131,6 +2405,10 @@ fn viewKindInt(kind: platform_mod.ViewKind) c_int {
2131
2405
  };
2132
2406
  }
2133
2407
 
2408
+ fn gpuSurfaceBackendRequestInt(backend: platform_mod.GpuSurfaceBackend) c_int {
2409
+ return if (backend == .software) 1 else 0;
2410
+ }
2411
+
2134
2412
  fn flattenFilters(filters: []const platform_mod.FileFilter, buffer: []u8) []const u8 {
2135
2413
  var offset: usize = 0;
2136
2414
  for (filters) |filter| {