@native-sdk/cli 0.9.0 → 0.9.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.
- package/build/app.zig +357 -33
- package/build.zig +234 -11
- package/native-sdk.d.ts +8 -0
- package/package.json +10 -10
- package/packages/core/compile-surface/core.ts +70 -1
- package/packages/core/package-lock.json +11 -11
- package/packages/core/package.json +2 -2
- package/packages/core/scripts/merge_static_archives.mjs +78 -0
- package/packages/core/scripts/run_external_core_compiler.mjs +52 -7
- package/packages/core/scripts/run_external_service_compiler.mjs +53 -13
- package/packages/core/scripts/stage_external_services.mjs +1 -1
- package/packages/core/sdk/core.d.ts +75 -0
- package/packages/core/sdk/core.ts +170 -13
- package/packages/core/sdk/events.d.ts +53 -0
- package/packages/core/sdk/events.ts +78 -7
- package/packages/core/src/checker.ts +470 -2
- package/packages/core/src/cli.ts +13 -0
- package/packages/core/src/contract.ts +3 -1
- package/packages/core/src/devhost.ts +30 -1
- package/packages/core/src/devhost_journal.mjs +10 -8
- package/packages/core/src/diagnostics.ts +99 -8
- package/packages/core/src/frontend.ts +6 -0
- package/packages/core/src/infer.ts +24 -0
- package/skill-data/automation/SKILL.md +1 -1
- package/skill-data/native-ui/SKILL.md +18 -12
- package/skill-data/ts-core/SKILL.md +54 -21
- package/skill-data/ts-services/SKILL.md +4 -4
- package/skill-data/ts-services/references/service-surface.md +2 -1
- package/skill-data/zig/SKILL.md +1 -1
- package/src/app_runner/root.zig +122 -6
- package/src/app_runner/ts_core_main.zig +9 -4
- package/src/app_runner/ts_core_mobile.zig +241 -0
- package/src/app_runner/window_placement.zig +63 -0
- package/src/automation/protocol.zig +5 -1
- package/src/automation/snapshot.zig +8 -6
- package/src/embed/app_exports.zig +3 -1
- package/src/embed/conversions.zig +1 -0
- package/src/embed/host.zig +10 -3
- package/src/embed/root.zig +2 -0
- package/src/embed/tests.zig +41 -1
- package/src/embed/types.zig +2 -0
- package/src/embed/ui_host.zig +94 -4
- package/src/platform/android/NativeSdkActivity.java +49 -4
- package/src/platform/android/android_host.c +3 -3
- package/src/platform/android/native_sdk_app.h +1 -1
- package/src/platform/ios/apple_image_fit.h +38 -0
- package/src/platform/ios/files.zig +1 -0
- package/src/platform/ios/native_sdk_app.h +1 -1
- package/src/platform/ios/uikit_host.m +28 -5
- package/src/platform/linux/cef_host.cpp +17 -3
- package/src/platform/linux/gtk_host.c +243 -15
- package/src/platform/linux/gtk_host.h +5 -4
- package/src/platform/linux/root.zig +91 -11
- package/src/platform/macos/appkit_host.h +18 -11
- package/src/platform/macos/appkit_host.m +402 -128
- package/src/platform/macos/cef_host.mm +342 -122
- package/src/platform/macos/image_fit_test.m +44 -0
- package/src/platform/macos/root.zig +231 -39
- package/src/platform/null_platform.zig +251 -54
- package/src/platform/null_platform_tests.zig +97 -4
- package/src/platform/root.zig +12 -0
- package/src/platform/types.zig +174 -35
- package/src/platform/windows/root.zig +42 -13
- package/src/platform/windows/webview2_host.cpp +72 -11
- package/src/primitives/app_manifest/root.zig +3 -0
- package/src/primitives/app_manifest/tests.zig +24 -0
- package/src/primitives/app_manifest/types.zig +13 -0
- package/src/primitives/app_manifest/validation.zig +24 -0
- package/src/primitives/canvas/a11y_audit.zig +9 -7
- package/src/primitives/canvas/a11y_audit_tests.zig +24 -0
- package/src/primitives/canvas/events.zig +50 -0
- package/src/primitives/canvas/layout_audit.zig +6 -6
- package/src/primitives/canvas/markdown.zig +4 -0
- package/src/primitives/canvas/markdown_tests.zig +1 -0
- package/src/primitives/canvas/png.zig +138 -0
- package/src/primitives/canvas/root.zig +13 -2
- package/src/primitives/canvas/text_span_tests.zig +5 -3
- package/src/primitives/canvas/text_spans.zig +2 -0
- package/src/primitives/canvas/text_tests.zig +20 -0
- package/src/primitives/canvas/ui.zig +55 -2
- package/src/primitives/canvas/ui_markup.zig +40 -13
- package/src/primitives/canvas/ui_markup_binary.zig +8 -6
- package/src/primitives/canvas/ui_markup_binary_tests.zig +14 -4
- package/src/primitives/canvas/ui_markup_compiled.zig +5 -1
- package/src/primitives/canvas/ui_markup_compiled_tests.zig +52 -0
- package/src/primitives/canvas/ui_markup_tests.zig +11 -2
- package/src/primitives/canvas/ui_markup_view.zig +8 -2
- package/src/primitives/canvas/ui_markup_view_tests.zig +49 -4
- package/src/primitives/canvas/ui_schema.zig +17 -13
- package/src/primitives/canvas/ui_tests.zig +47 -0
- package/src/primitives/canvas/widget_access.zig +3 -3
- package/src/primitives/canvas/widget_anchored_tests.zig +106 -0
- package/src/primitives/canvas/widget_builtin_tests.zig +580 -32
- package/src/primitives/canvas/widget_invalidation.zig +33 -6
- package/src/primitives/canvas/widget_layout.zig +333 -126
- package/src/primitives/canvas/widget_layout_tests.zig +169 -0
- package/src/primitives/canvas/widget_metrics.zig +7 -8
- package/src/primitives/canvas/widget_render.zig +76 -44
- package/src/primitives/canvas/widget_render_controls.zig +110 -32
- package/src/primitives/canvas/widget_render_style.zig +58 -18
- package/src/primitives/canvas/widget_render_surfaces.zig +45 -14
- package/src/primitives/canvas/widget_routing.zig +40 -34
- package/src/primitives/canvas/widget_runtime.zig +11 -2
- package/src/primitives/canvas/widget_runtime_tests.zig +10 -14
- package/src/primitives/canvas/widget_semantics.zig +8 -3
- package/src/primitives/canvas/widget_semantics_tests.zig +6 -2
- package/src/primitives/canvas/widget_tree.zig +99 -5
- package/src/primitives/canvas/widgets.zig +20 -7
- package/src/root.zig +10 -0
- package/src/runtime/api.zig +14 -0
- package/src/runtime/automation_commands.zig +35 -2
- package/src/runtime/automation_snapshot.zig +32 -24
- package/src/runtime/automation_widget_dispatch.zig +48 -6
- package/src/runtime/bridge_permission_tests.zig +35 -1
- package/src/runtime/bridge_responses.zig +1 -1
- package/src/runtime/builtin_bridge.zig +16 -3
- package/src/runtime/canvas_frame_packet_tests.zig +22 -13
- package/src/runtime/canvas_image_tests.zig +146 -2
- package/src/runtime/canvas_images.zig +35 -16
- package/src/runtime/canvas_limits.zig +13 -3
- package/src/runtime/canvas_widget_accessibility_tests.zig +279 -4
- package/src/runtime/canvas_widget_anchored_tests.zig +54 -0
- package/src/runtime/canvas_widget_control_tests.zig +31 -1
- package/src/runtime/canvas_widget_events.zig +151 -26
- package/src/runtime/canvas_widget_floating_tests.zig +48 -0
- package/src/runtime/canvas_widget_runtime.zig +322 -102
- package/src/runtime/canvas_widget_scroll_driver_tests.zig +34 -0
- package/src/runtime/canvas_widget_scroll_drivers.zig +12 -10
- package/src/runtime/canvas_widget_scroll_tests.zig +193 -2
- package/src/runtime/canvas_widget_semantics_tests.zig +337 -3
- package/src/runtime/canvas_widget_split_tree_tests.zig +55 -0
- package/src/runtime/canvas_widget_state.zig +33 -7
- package/src/runtime/core.zig +28 -6
- package/src/runtime/effects.zig +1337 -49
- package/src/runtime/effects_file_tests.zig +587 -0
- package/src/runtime/effects_image_tests.zig +91 -0
- package/src/runtime/effects_notification_tests.zig +11 -0
- package/src/runtime/file_access.zig +400 -0
- package/src/runtime/file_effect_crash_helper.zig +28 -0
- package/src/runtime/file_effect_crash_tests.zig +37 -0
- package/src/runtime/flow.zig +21 -9
- package/src/runtime/gpu_surface_events.zig +25 -2
- package/src/runtime/native_command_event_tests.zig +44 -2
- package/src/runtime/pty.zig +5 -2
- package/src/runtime/root.zig +7 -0
- package/src/runtime/session_blobs.zig +4 -1
- package/src/runtime/session_journal.zig +56 -5
- package/src/runtime/session_record.zig +38 -0
- package/src/runtime/session_replay.zig +110 -15
- package/src/runtime/session_tests.zig +192 -4
- package/src/runtime/shell_layout_tests.zig +12 -0
- package/src/runtime/state.zig +10 -0
- package/src/runtime/system_services.zig +129 -36
- package/src/runtime/ts_core_host.zig +281 -11
- package/src/runtime/ts_core_host_tests.zig +142 -1
- package/src/runtime/ts_ui_app.zig +461 -8
- package/src/runtime/ui_app.zig +330 -112
- package/src/runtime/ui_app_tests.zig +251 -25
- package/src/runtime/ui_app_window_tests.zig +74 -8
- package/src/runtime/validation.zig +20 -0
- package/src/runtime/view.zig +3 -0
- package/src/runtime/view_widget_control.zig +18 -0
- package/src/runtime/view_widget_scroll.zig +112 -18
- package/src/runtime/view_widget_tree.zig +110 -13
- package/src/runtime/widget_bridge.zig +2 -0
- package/src/runtime/window_command_bridge_tests.zig +86 -0
- package/src/runtime/window_storage.zig +16 -1
- package/src/runtime/window_views.zig +1 -0
- package/src/tooling/ios.zig +3 -0
- package/src/tooling/manifest.zig +90 -2
- package/src/tooling/package.zig +67 -2
- package/src/tooling/raw_manifest.zig +10 -0
- package/src/tooling/templates.zig +156 -18
- package/src/tooling/ts_core.zig +83 -0
- package/src/tooling/web_engine.zig +1 -1
- package/tools/corewire/emit.zig +3 -3
- package/tools/corewire/emit_facade.zig +65 -5
- package/tools/corewire/emit_profile.zig +2 -2
- package/tools/corewire/emit_service.zig +2 -2
- package/tools/corewire/service_contract.zig +4 -4
- package/tools/corewire/sidecar.zig +4 -4
|
@@ -32,7 +32,7 @@ pub fn RuntimeAutomationSnapshot(comptime Runtime: type) type {
|
|
|
32
32
|
.widgets = &.{},
|
|
33
33
|
.diagnostics = automationDiagnostics(self),
|
|
34
34
|
.frame_profile = automationFrameProfile(self),
|
|
35
|
-
.
|
|
35
|
+
.trays = automationTrays(self),
|
|
36
36
|
.audio = automationAudio(self),
|
|
37
37
|
.video = automationVideo(self),
|
|
38
38
|
.errors = self.dispatchErrors(),
|
|
@@ -66,7 +66,7 @@ pub fn RuntimeAutomationSnapshot(comptime Runtime: type) type {
|
|
|
66
66
|
.widgets = self.automation_widgets[0..widget_count],
|
|
67
67
|
.diagnostics = automationDiagnostics(self),
|
|
68
68
|
.frame_profile = automationFrameProfile(self),
|
|
69
|
-
.
|
|
69
|
+
.trays = automationTrays(self),
|
|
70
70
|
.audio = automationAudio(self),
|
|
71
71
|
.video = automationVideo(self),
|
|
72
72
|
.errors = self.dispatchErrors(),
|
|
@@ -79,30 +79,38 @@ pub fn RuntimeAutomationSnapshot(comptime Runtime: type) type {
|
|
|
79
79
|
};
|
|
80
80
|
}
|
|
81
81
|
|
|
82
|
-
///
|
|
83
|
-
///
|
|
84
|
-
///
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
self.automation_tray_items
|
|
91
|
-
|
|
92
|
-
.
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
82
|
+
/// Every live status item as the runtime last applied it. Menu
|
|
83
|
+
/// bar contents live outside window captures, so identifiers
|
|
84
|
+
/// are explicit in both snapshots and automation commands.
|
|
85
|
+
fn automationTrays(self: *Runtime) []const automation.snapshot.Tray {
|
|
86
|
+
var tray_count: usize = 0;
|
|
87
|
+
var item_offset: usize = 0;
|
|
88
|
+
for (self.status_items) |status_item| {
|
|
89
|
+
if (!status_item.active or tray_count >= self.automation_trays.len) continue;
|
|
90
|
+
const count = @min(status_item.item_count, self.automation_tray_items.len - item_offset);
|
|
91
|
+
for (status_item.items[0..count], 0..) |item, index| {
|
|
92
|
+
self.automation_tray_items[item_offset + index] = .{
|
|
93
|
+
.id = item.id,
|
|
94
|
+
.label = item.label,
|
|
95
|
+
.command = item.command,
|
|
96
|
+
.separator = item.separator,
|
|
97
|
+
.enabled = item.enabled,
|
|
98
|
+
.detail = item.detail,
|
|
99
|
+
.role = item.role,
|
|
100
|
+
.key = item.key,
|
|
101
|
+
.modifiers = item.modifiers,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
self.automation_trays[tray_count] = .{
|
|
105
|
+
.id = status_item.id,
|
|
106
|
+
.visible = status_item.visible,
|
|
107
|
+
.title = status_item.title,
|
|
108
|
+
.items = self.automation_tray_items[item_offset .. item_offset + count],
|
|
100
109
|
};
|
|
110
|
+
tray_count += 1;
|
|
111
|
+
item_offset += count;
|
|
101
112
|
}
|
|
102
|
-
return .
|
|
103
|
-
.title = self.tray_title,
|
|
104
|
-
.items = self.automation_tray_items[0..count],
|
|
105
|
-
};
|
|
113
|
+
return self.automation_trays[0..tray_count];
|
|
106
114
|
}
|
|
107
115
|
|
|
108
116
|
/// Live audio playback as the ui-app layer last mirrored it
|
|
@@ -476,8 +476,16 @@ pub fn RuntimeAutomationWidgetDispatch(comptime Runtime: type) type {
|
|
|
476
476
|
|
|
477
477
|
pub fn focusAutomationCanvasWidget(self: *Runtime, view_index: usize, id: canvas.ObjectId) anyerror!void {
|
|
478
478
|
if (view_index >= self.view_count) return error.ViewNotFound;
|
|
479
|
-
|
|
479
|
+
// Programmatic focus names logical targets before applying the
|
|
480
|
+
// pointer path's geometry clip. The shared keyboard/autofocus
|
|
481
|
+
// reveal seam scrolls every runtime-owned ancestor. Preflight
|
|
482
|
+
// the reveal before platform focus, then focus the view before
|
|
483
|
+
// committing any retained scroll mutation: a rejected platform
|
|
484
|
+
// focus leaves the canvas byte-for-byte where it was.
|
|
485
|
+
if (!CanvasWidgetEventMethods().canRevealCanvasWidgetFocusTarget(self, view_index, id)) return error.InvalidCommand;
|
|
480
486
|
try self.focusView(self.views[view_index].window_id, self.views[view_index].label);
|
|
487
|
+
const reveal = try CanvasWidgetEventMethods().revealCanvasWidgetFocusTarget(self, view_index, id) orelse return error.InvalidCommand;
|
|
488
|
+
const target = reveal.target;
|
|
481
489
|
// Programmatic focus (autofocus, automation `focus`) follows
|
|
482
490
|
// the pointer contract, not the keyboard one: buttons and
|
|
483
491
|
// rows take focus QUIETLY (no ring), editable text kinds show
|
|
@@ -486,8 +494,9 @@ pub fn RuntimeAutomationWidgetDispatch(comptime Runtime: type) type {
|
|
|
486
494
|
// window-level default focus landing on a button dressed an
|
|
487
495
|
// idle control in the focus ring.
|
|
488
496
|
const focus_visible_id: canvas.ObjectId = if (canvas_widget_runtime.canvasWidgetShowsPointerFocusRing(target.kind)) target.id else 0;
|
|
489
|
-
|
|
490
|
-
|
|
497
|
+
const previous_state = self.views[view_index].canvasWidgetRenderState();
|
|
498
|
+
const focus_changed = self.views[view_index].canvas_widget_focused_id != target.id or self.views[view_index].canvas_widget_focus_visible_id != focus_visible_id;
|
|
499
|
+
if (focus_changed) {
|
|
491
500
|
self.views[view_index].canvas_widget_focused_id = target.id;
|
|
492
501
|
self.views[view_index].canvas_widget_focus_visible_id = focus_visible_id;
|
|
493
502
|
// Pointer-contract provenance: a programmatic ring
|
|
@@ -501,14 +510,47 @@ pub fn RuntimeAutomationWidgetDispatch(comptime Runtime: type) type {
|
|
|
501
510
|
// widget whose focus-shown tooltip is up with the ring
|
|
502
511
|
// intact is not a move, and leaves it alone.
|
|
503
512
|
try CanvasWidgetEventMethods().updateCanvasTooltipIntentForProgrammaticFocusMove(self, view_index);
|
|
504
|
-
|
|
505
|
-
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
// The established programmatic-selection contract is a collapsed
|
|
516
|
+
// caret at text.len when no source/retained selection exists.
|
|
517
|
+
// Preserve an existing selection, then reveal its focus endpoint
|
|
518
|
+
// inside the editor after the outer ancestor reveal has made the
|
|
519
|
+
// editability/visibility gate truthful.
|
|
520
|
+
const caret_initialized = try self.views[view_index].ensureCanvasWidgetFocusedTextCaret();
|
|
521
|
+
var inner_scroll_changed = false;
|
|
522
|
+
if (self.views[view_index].canEditCanvasWidgetText(target.id)) {
|
|
523
|
+
if (self.views[view_index].canvasWidgetNodeIndexById(target.id)) |node_index| {
|
|
524
|
+
const before = self.views[view_index].widget_layout_nodes[node_index].widget;
|
|
525
|
+
self.views[view_index].scrollCanvasTextInputCaretIntoView(node_index);
|
|
526
|
+
const after = self.views[view_index].widget_layout_nodes[node_index].widget;
|
|
527
|
+
inner_scroll_changed = before.value != after.value or before.value_x != after.value_x;
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
if (inner_scroll_changed) {
|
|
531
|
+
try self.views[view_index].refreshCanvasWidgetSemantics();
|
|
532
|
+
if (!caret_initialized) self.views[view_index].widget_revision += 1;
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
if (focus_changed or caret_initialized or inner_scroll_changed or reveal.scroll_dirty != null) {
|
|
536
|
+
// A focus/reveal change repaints; record the automation input
|
|
537
|
+
// so the completing frame publishes (same contract as select
|
|
506
538
|
// and text edits). Callers that dispatch a follow-up input
|
|
507
539
|
// event simply overwrite this with their own timestamp.
|
|
508
540
|
self.views[view_index].recordGpuSurfaceInputTimestamp(automationInputTimestampNs());
|
|
541
|
+
}
|
|
542
|
+
if (focus_changed) {
|
|
509
543
|
try CanvasWidgetEventMethods().invalidateForCanvasWidgetRenderStateChange(self, view_index, previous_state, self.views[view_index].canvasWidgetRenderState());
|
|
544
|
+
} else if (caret_initialized or inner_scroll_changed) {
|
|
545
|
+
if (self.views[view_index].canvasWidgetNodeIndexById(target.id)) |node_index| {
|
|
546
|
+
if (self.views[view_index].canvasWidgetDirtyBounds(node_index, self.views[view_index].widget_layout_nodes[node_index].frame)) |dirty| {
|
|
547
|
+
try CanvasWidgetEventMethods().invalidateForCanvasWidgetDirty(self, view_index, dirty);
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
if (reveal.scroll_dirty != null) {
|
|
552
|
+
_ = try CanvasWidgetDisplayMethods().refreshCanvasWidgetDisplayListIfOwned(self, view_index);
|
|
510
553
|
}
|
|
511
|
-
_ = try self.views[view_index].ensureCanvasWidgetFocusedTextCaret();
|
|
512
554
|
}
|
|
513
555
|
|
|
514
556
|
pub fn dispatchAutomationWidgetKey(self: *Runtime, app: runtime_api.App(Runtime), view_index: usize, id: canvas.ObjectId, key: []const u8) anyerror!void {
|
|
@@ -377,10 +377,11 @@ test "runtime gates built-in OS bridge commands through explicit policy" {
|
|
|
377
377
|
try std.testing.expect(std.mem.indexOf(u8, denied.null_platform.lastBridgeResponse(), "OS API is not permitted") != null);
|
|
378
378
|
try std.testing.expect(std.mem.indexOf(u8, denied.null_platform.lastBridgeResponse(), "\"permission_denied\"") != null);
|
|
379
379
|
|
|
380
|
-
const grants = [_][]const u8{ security.permission_network, security.permission_filesystem, security.permission_notifications };
|
|
380
|
+
const grants = [_][]const u8{ security.permission_network, security.permission_filesystem, security.permission_notifications, security.permission_command };
|
|
381
381
|
const network_permission = [_][]const u8{security.permission_network};
|
|
382
382
|
const filesystem_permission = [_][]const u8{security.permission_filesystem};
|
|
383
383
|
const notifications_permission = [_][]const u8{security.permission_notifications};
|
|
384
|
+
const command_permission = [_][]const u8{security.permission_command};
|
|
384
385
|
const origins = [_][]const u8{"zero://inline"};
|
|
385
386
|
const policies = [_]bridge.CommandPolicy{
|
|
386
387
|
.{ .name = "native-sdk.os.openUrl", .permissions = &network_permission, .origins = &origins },
|
|
@@ -389,6 +390,14 @@ test "runtime gates built-in OS bridge commands through explicit policy" {
|
|
|
389
390
|
.{ .name = "native-sdk.os.addRecentDocument", .permissions = &filesystem_permission, .origins = &origins },
|
|
390
391
|
.{ .name = "native-sdk.os.clearRecentDocuments", .permissions = &filesystem_permission, .origins = &origins },
|
|
391
392
|
};
|
|
393
|
+
const action_policies = [_]bridge.CommandPolicy{
|
|
394
|
+
.{ .name = "native-sdk.os.openUrl", .permissions = &network_permission, .origins = &origins },
|
|
395
|
+
.{ .name = "native-sdk.os.showNotification", .permissions = ¬ifications_permission, .origins = &origins },
|
|
396
|
+
.{ .name = "native-sdk.os.revealPath", .permissions = &filesystem_permission, .origins = &origins },
|
|
397
|
+
.{ .name = "native-sdk.os.addRecentDocument", .permissions = &filesystem_permission, .origins = &origins },
|
|
398
|
+
.{ .name = "native-sdk.os.clearRecentDocuments", .permissions = &filesystem_permission, .origins = &origins },
|
|
399
|
+
.{ .name = "native-sdk.command.invoke", .permissions = &command_permission, .origins = &origins },
|
|
400
|
+
};
|
|
392
401
|
const allowed_urls = [_][]const u8{"https://example.com/*"};
|
|
393
402
|
|
|
394
403
|
const allowed = try TestHarness().create(std.testing.allocator, .{});
|
|
@@ -408,6 +417,31 @@ test "runtime gates built-in OS bridge commands through explicit policy" {
|
|
|
408
417
|
try std.testing.expectEqual(@as(usize, 1), allowed.null_platform.notificationCount());
|
|
409
418
|
try std.testing.expectEqualStrings("Build finished", allowed.null_platform.lastNotificationTitle());
|
|
410
419
|
|
|
420
|
+
try allowed.runtime.dispatchPlatformEvent(app, .{ .bridge_message = .{
|
|
421
|
+
.bytes = "{\"id\":\"action-denied\",\"command\":\"native-sdk.os.showNotification\",\"payload\":{\"id\":\"build\",\"title\":\"Build finished\",\"actionLabel\":\"Open\",\"actionCommand\":\"build.open\"}}",
|
|
422
|
+
.origin = "zero://inline",
|
|
423
|
+
} });
|
|
424
|
+
try std.testing.expect(std.mem.indexOf(u8, allowed.null_platform.lastBridgeResponse(), "Notification actions require command invoke permission") != null);
|
|
425
|
+
try std.testing.expectEqual(@as(usize, 1), allowed.null_platform.notificationCount());
|
|
426
|
+
|
|
427
|
+
allowed.runtime.options.builtin_bridge = .{ .enabled = true, .commands = &action_policies };
|
|
428
|
+
try allowed.runtime.dispatchPlatformEvent(app, .{ .bridge_message = .{
|
|
429
|
+
.bytes = "{\"id\":\"action-allowed\",\"command\":\"native-sdk.os.showNotification\",\"payload\":{\"id\":\"build\",\"title\":\"Build finished\",\"actionLabel\":\"Open\",\"actionCommand\":\"build.open\"}}",
|
|
430
|
+
.origin = "zero://inline",
|
|
431
|
+
} });
|
|
432
|
+
try std.testing.expect(std.mem.indexOf(u8, allowed.null_platform.lastBridgeResponse(), "\"ok\":true") != null);
|
|
433
|
+
try std.testing.expectEqual(@as(usize, 2), allowed.null_platform.notificationCount());
|
|
434
|
+
try std.testing.expectEqualStrings("build", allowed.null_platform.lastNotificationId());
|
|
435
|
+
try std.testing.expectEqualStrings("Open", allowed.null_platform.lastNotificationActionLabel());
|
|
436
|
+
try std.testing.expectEqualStrings("build.open", allowed.null_platform.lastNotificationActionCommand());
|
|
437
|
+
|
|
438
|
+
try allowed.runtime.dispatchPlatformEvent(app, .{ .bridge_message = .{
|
|
439
|
+
.bytes = "{\"id\":\"invalid-notification\",\"command\":\"native-sdk.os.showNotification\",\"payload\":{\"id\":42,\"title\":\"Build finished\"}}",
|
|
440
|
+
.origin = "zero://inline",
|
|
441
|
+
} });
|
|
442
|
+
try std.testing.expect(std.mem.indexOf(u8, allowed.null_platform.lastBridgeResponse(), "\"invalid_request\"") != null);
|
|
443
|
+
try std.testing.expectEqual(@as(usize, 2), allowed.null_platform.notificationCount());
|
|
444
|
+
|
|
411
445
|
try allowed.runtime.dispatchPlatformEvent(app, .{ .bridge_message = .{
|
|
412
446
|
.bytes = "{\"id\":\"open\",\"command\":\"native-sdk.os.openUrl\",\"payload\":{\"url\":\"https://example.com/docs\"}}",
|
|
413
447
|
.origin = "zero://inline",
|
|
@@ -49,7 +49,7 @@ pub fn writeCommandEventJson(event_value: anytype, output: []u8) ![]const u8 {
|
|
|
49
49
|
try json.writeString(&writer, @tagName(event_value.source));
|
|
50
50
|
try writer.print(",\"windowId\":{d},\"viewLabel\":", .{event_value.window_id});
|
|
51
51
|
try json.writeString(&writer, event_value.view_label);
|
|
52
|
-
try writer.print(",\"trayItemId\":{d}", .{event_value.tray_item_id});
|
|
52
|
+
try writer.print(",\"statusItemId\":{d},\"trayItemId\":{d}", .{ event_value.status_item_id, event_value.tray_item_id });
|
|
53
53
|
try writer.writeByte('}');
|
|
54
54
|
return writer.buffered();
|
|
55
55
|
}
|
|
@@ -186,7 +186,17 @@ pub fn RuntimeBuiltinBridge(comptime Runtime: type) type {
|
|
|
186
186
|
return bridge.writeSuccessResponse(response_buffer, request.id, result);
|
|
187
187
|
}
|
|
188
188
|
|
|
189
|
-
pub fn dispatchOsBridgeCommand(self: *Runtime, request: bridge.Request, result_buffer: []u8, response_buffer: []u8) []const u8 {
|
|
189
|
+
pub fn dispatchOsBridgeCommand(self: *Runtime, request: bridge.Request, origin: []const u8, result_buffer: []u8, response_buffer: []u8) []const u8 {
|
|
190
|
+
if (std.mem.eql(u8, request.command, "native-sdk.os.showNotification")) {
|
|
191
|
+
var action_scratch: [platform.max_notification_action_command_bytes]u8 = undefined;
|
|
192
|
+
var action_storage = json.StringStorage.init(&action_scratch);
|
|
193
|
+
const action_command = jsonStringField(request.payload, "actionCommand", &action_storage) orelse "";
|
|
194
|
+
if (action_command.len > 0 and
|
|
195
|
+
!Self.allowsBuiltinBridgeCommand(self, "native-sdk.command.invoke", origin, security.permission_command))
|
|
196
|
+
{
|
|
197
|
+
return bridge.writeErrorResponse(response_buffer, request.id, .permission_denied, "Notification actions require command invoke permission");
|
|
198
|
+
}
|
|
199
|
+
}
|
|
190
200
|
const result = if (std.mem.eql(u8, request.command, "native-sdk.os.openUrl"))
|
|
191
201
|
SystemServiceMethods.openExternalUrlFromJson(self, request.payload, result_buffer) catch |err| return bridge.writeErrorResponse(response_buffer, request.id, builtinBridgeErrorCode(err), builtinBridgeErrorMessage(err))
|
|
192
202
|
else if (std.mem.eql(u8, request.command, "native-sdk.os.showNotification"))
|
|
@@ -234,8 +244,10 @@ pub fn RuntimeBuiltinBridge(comptime Runtime: type) type {
|
|
|
234
244
|
const title = jsonStringField(payload, "title", &storage) orelse "";
|
|
235
245
|
const width = jsonNumberField(payload, "width") orelse 720;
|
|
236
246
|
const height = jsonNumberField(payload, "height") orelse 480;
|
|
237
|
-
const
|
|
238
|
-
const
|
|
247
|
+
const requested_x = jsonNumberField(payload, "x");
|
|
248
|
+
const requested_y = jsonNumberField(payload, "y");
|
|
249
|
+
const x = requested_x orelse 0;
|
|
250
|
+
const y = requested_y orelse 0;
|
|
239
251
|
const source = if (jsonStringField(payload, "url", &storage)) |url| platform.WebViewSource.url(url) else null;
|
|
240
252
|
const titlebar = if (jsonStringField(payload, "titlebar", &storage)) |value|
|
|
241
253
|
windowTitlebarStyleFromString(value) orelse return error.InvalidWindowOptions
|
|
@@ -246,6 +258,7 @@ pub fn RuntimeBuiltinBridge(comptime Runtime: type) type {
|
|
|
246
258
|
.title = title,
|
|
247
259
|
.default_frame = geometry.RectF.init(x, y, width, height),
|
|
248
260
|
.restore_state = jsonBoolField(payload, "restoreState") orelse true,
|
|
261
|
+
.initial_placement = if (requested_x != null or requested_y != null) .explicit else .default,
|
|
249
262
|
.titlebar = titlebar,
|
|
250
263
|
.transparent = jsonBoolField(payload, "transparent") orelse false,
|
|
251
264
|
.always_on_top = jsonBoolField(payload, "alwaysOnTop") orelse false,
|
|
@@ -725,7 +725,9 @@ test "runtime pixel fallback honors presentation scale without invalidating reta
|
|
|
725
725
|
try std.testing.expect(!presented_frame.canvas_frame_requires_render);
|
|
726
726
|
}
|
|
727
727
|
|
|
728
|
-
test "runtime keeps
|
|
728
|
+
test "runtime keeps raised-budget registered images on the packet path" {
|
|
729
|
+
comptime std.debug.assert(platform.max_gpu_surface_image_pixel_bytes == runtime_module.max_registered_canvas_image_pixel_bytes_ceiling);
|
|
730
|
+
|
|
729
731
|
const TestApp = struct {
|
|
730
732
|
fn app(self: *@This()) App {
|
|
731
733
|
return .{ .context = self, .name = "gpu-canvas-image-packet", .source = platform.WebViewSource.html("<h1>Hello</h1>") };
|
|
@@ -735,6 +737,13 @@ test "runtime keeps frames with registered images on the packet path" {
|
|
|
735
737
|
const harness = try TestHarness().create(std.testing.allocator, .{});
|
|
736
738
|
defer harness.destroy(std.testing.allocator);
|
|
737
739
|
harness.null_platform.gpu_surfaces = true;
|
|
740
|
+
Runtime.initAt(&harness.runtime, .{
|
|
741
|
+
.platform = harness.null_platform.platform(),
|
|
742
|
+
.trace_sink = harness.trace_sink.sink(),
|
|
743
|
+
.max_image_pixel_bytes = platform.max_gpu_surface_image_pixel_bytes,
|
|
744
|
+
.environ = std.testing.environ,
|
|
745
|
+
});
|
|
746
|
+
harness.runtime.dispatch_error_policy = .propagate;
|
|
738
747
|
var app_state: TestApp = .{};
|
|
739
748
|
try harness.start(app_state.app());
|
|
740
749
|
|
|
@@ -745,14 +754,14 @@ test "runtime keeps frames with registered images on the packet path" {
|
|
|
745
754
|
.frame = geometry.RectF.init(0, 0, 256, 160),
|
|
746
755
|
});
|
|
747
756
|
|
|
748
|
-
// A
|
|
749
|
-
//
|
|
750
|
-
//
|
|
751
|
-
//
|
|
752
|
-
|
|
753
|
-
const
|
|
754
|
-
const
|
|
755
|
-
const avatar = try std.testing.allocator.alloc(u8,
|
|
757
|
+
// A 1024x512 image is 2 MiB: above the default registry tier and the
|
|
758
|
+
// upload seam's former fixed 1 MiB bound, but inside this runtime's
|
|
759
|
+
// declared budget. As an id + fingerprint reference over the binary
|
|
760
|
+
// side-channel it must upload and keep the frame on the packet path.
|
|
761
|
+
const image_width: usize = 1024;
|
|
762
|
+
const image_height: usize = 512;
|
|
763
|
+
const image_bytes = image_width * image_height * 4;
|
|
764
|
+
const avatar = try std.testing.allocator.alloc(u8, image_bytes);
|
|
756
765
|
defer std.testing.allocator.free(avatar);
|
|
757
766
|
var offset: usize = 0;
|
|
758
767
|
while (offset < avatar.len) : (offset += 4) {
|
|
@@ -761,7 +770,7 @@ test "runtime keeps frames with registered images on the packet path" {
|
|
|
761
770
|
avatar[offset + 2] = 32;
|
|
762
771
|
avatar[offset + 3] = 255;
|
|
763
772
|
}
|
|
764
|
-
try harness.runtime.registerCanvasImage(42,
|
|
773
|
+
try harness.runtime.registerCanvasImage(42, image_width, image_height, avatar);
|
|
765
774
|
|
|
766
775
|
const commands = [_]canvas.CanvasCommand{
|
|
767
776
|
.{ .fill_rect = .{
|
|
@@ -807,9 +816,9 @@ test "runtime keeps frames with registered images on the packet path" {
|
|
|
807
816
|
// The pixel bytes rode the binary side-channel instead.
|
|
808
817
|
try std.testing.expectEqual(@as(usize, 1), harness.null_platform.gpu_surface_image_upload_count);
|
|
809
818
|
try std.testing.expectEqual(@as(u64, 42), harness.null_platform.gpu_surface_image_upload_id);
|
|
810
|
-
try std.testing.expectEqual(
|
|
811
|
-
try std.testing.expectEqual(
|
|
812
|
-
try std.testing.expectEqual(
|
|
819
|
+
try std.testing.expectEqual(image_width, harness.null_platform.gpu_surface_image_upload_width);
|
|
820
|
+
try std.testing.expectEqual(image_height, harness.null_platform.gpu_surface_image_upload_height);
|
|
821
|
+
try std.testing.expectEqual(image_bytes, harness.null_platform.gpu_surface_image_upload_byte_len);
|
|
813
822
|
try std.testing.expectEqualDeep([4]u8{ 200, 64, 32, 255 }, harness.null_platform.gpuSurfaceImage(42).?.sample_rgba);
|
|
814
823
|
|
|
815
824
|
// A clean second frame retains the image (no re-upload) and skips.
|
|
@@ -109,8 +109,8 @@ test "registered images draw through the reference renderer screenshot" {
|
|
|
109
109
|
// Raw RGBA fixture: 2x2 quadrant colors drawn with nearest sampling,
|
|
110
110
|
// so each destination quadrant is one exact color.
|
|
111
111
|
const fixture = [_]u8{
|
|
112
|
-
255, 0,
|
|
113
|
-
0, 0,
|
|
112
|
+
255, 0, 0, 255, 0, 255, 0, 255,
|
|
113
|
+
0, 0, 255, 255, 255, 255, 0, 255,
|
|
114
114
|
};
|
|
115
115
|
try harness.runtime.registerCanvasImage(42, 2, 2, &fixture);
|
|
116
116
|
|
|
@@ -207,6 +207,128 @@ test "registerCanvasImageBytes decodes through the platform seam" {
|
|
|
207
207
|
try std.testing.expectError(error.InvalidImageId, harness.runtime.registerCanvasImageBytes(0, encoded));
|
|
208
208
|
}
|
|
209
209
|
|
|
210
|
+
test "direct encoded image registration enforces the source bound before decode" {
|
|
211
|
+
const harness = try startedGpuHarness(std.testing.allocator);
|
|
212
|
+
defer harness.destroy(std.testing.allocator);
|
|
213
|
+
var app_state: RegistryApp = .{};
|
|
214
|
+
try harness.start(app_state.app());
|
|
215
|
+
harness.null_platform.image_decode = true;
|
|
216
|
+
|
|
217
|
+
const oversized = try std.testing.allocator.alloc(u8, effects_mod.max_effect_image_source_bytes + 1);
|
|
218
|
+
defer std.testing.allocator.free(oversized);
|
|
219
|
+
@memset(oversized, 0x5A);
|
|
220
|
+
|
|
221
|
+
try std.testing.expectError(error.ImageTooLarge, harness.runtime.registerCanvasImageBytes(9, oversized));
|
|
222
|
+
try std.testing.expectEqual(@as(usize, 0), harness.null_platform.image_decode_count);
|
|
223
|
+
try std.testing.expectEqual(@as(usize, 0), harness.runtime.registeredCanvasImageCount());
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
fn encodedPng(allocator: std.mem.Allocator, width: usize, height: usize, pixels: []const u8) ![]u8 {
|
|
227
|
+
const capacity = try canvas.png.encodedRgba8ByteLen(width, height);
|
|
228
|
+
const encoded = try allocator.alloc(u8, capacity);
|
|
229
|
+
errdefer allocator.free(encoded);
|
|
230
|
+
var writer = std.Io.Writer.fixed(encoded);
|
|
231
|
+
try canvas.png.writeRgba8(&writer, width, height, pixels);
|
|
232
|
+
return encoded[0..writer.buffered().len];
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
test "null image codec fits oversized photos deterministically and preserves in-budget geometry" {
|
|
236
|
+
const harness = try startedGpuHarness(std.testing.allocator);
|
|
237
|
+
defer harness.destroy(std.testing.allocator);
|
|
238
|
+
var app_state: RegistryApp = .{};
|
|
239
|
+
try harness.start(app_state.app());
|
|
240
|
+
harness.null_platform.image_decode = true;
|
|
241
|
+
|
|
242
|
+
// No-op below the budget.
|
|
243
|
+
const tiny_pixels = [_]u8{
|
|
244
|
+
1, 2, 3, 255, 4, 5, 6, 255, 7, 8, 9, 255,
|
|
245
|
+
10, 11, 12, 255, 13, 14, 15, 255, 16, 17, 18, 255,
|
|
246
|
+
};
|
|
247
|
+
const tiny_encoded = try encodedPng(std.testing.allocator, 3, 2, &tiny_pixels);
|
|
248
|
+
defer std.testing.allocator.free(tiny_encoded);
|
|
249
|
+
const tiny = try harness.runtime.registerCanvasImageBytes(20, tiny_encoded);
|
|
250
|
+
try std.testing.expectEqual(@as(usize, 3), tiny.width);
|
|
251
|
+
try std.testing.expectEqual(@as(usize, 2), tiny.height);
|
|
252
|
+
try std.testing.expectEqualSlices(u8, &tiny_pixels, registeredPixelsById(&harness.runtime, 20).?);
|
|
253
|
+
|
|
254
|
+
// A wide exact-boundary image remains 1024x256; this proves the cap is
|
|
255
|
+
// pixel count, not a 512x512 box.
|
|
256
|
+
const wide_pixels = try std.testing.allocator.alloc(u8, 1024 * 256 * 4);
|
|
257
|
+
defer std.testing.allocator.free(wide_pixels);
|
|
258
|
+
@memset(wide_pixels, 0x5A);
|
|
259
|
+
const wide_encoded = try encodedPng(std.testing.allocator, 1024, 256, wide_pixels);
|
|
260
|
+
defer std.testing.allocator.free(wide_encoded);
|
|
261
|
+
const wide = try harness.runtime.registerCanvasImageBytes(21, wide_encoded);
|
|
262
|
+
try std.testing.expectEqual(@as(usize, 1024), wide.width);
|
|
263
|
+
try std.testing.expectEqual(@as(usize, 256), wide.height);
|
|
264
|
+
|
|
265
|
+
// 1024x512 is over budget and fits to the exact null-codec target.
|
|
266
|
+
// Uniform source pixels make the integer box-average golden exact.
|
|
267
|
+
const photo_pixels = try std.testing.allocator.alloc(u8, 1024 * 512 * 4);
|
|
268
|
+
defer std.testing.allocator.free(photo_pixels);
|
|
269
|
+
var index: usize = 0;
|
|
270
|
+
while (index < photo_pixels.len) : (index += 4) {
|
|
271
|
+
photo_pixels[index..][0..4].* = .{ 17, 91, 203, 247 };
|
|
272
|
+
}
|
|
273
|
+
const photo_encoded = try encodedPng(std.testing.allocator, 1024, 512, photo_pixels);
|
|
274
|
+
defer std.testing.allocator.free(photo_encoded);
|
|
275
|
+
const first = try harness.runtime.registerCanvasImageBytes(22, photo_encoded);
|
|
276
|
+
try std.testing.expectEqual(@as(usize, 724), first.width);
|
|
277
|
+
try std.testing.expectEqual(@as(usize, 362), first.height);
|
|
278
|
+
try std.testing.expect(first.width * first.height * 4 <= harness.runtime.max_image_pixel_bytes);
|
|
279
|
+
const first_pixels = registeredPixelsById(&harness.runtime, 22).?;
|
|
280
|
+
const fingerprint = std.hash.Wyhash.hash(0, first_pixels);
|
|
281
|
+
var probe: usize = 0;
|
|
282
|
+
while (probe < @min(first_pixels.len, 4096)) : (probe += 4) {
|
|
283
|
+
try std.testing.expectEqualSlices(u8, &[_]u8{ 17, 91, 203, 247 }, first_pixels[probe..][0..4]);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
const second = try harness.runtime.registerCanvasImageBytes(23, photo_encoded);
|
|
287
|
+
try std.testing.expectEqual(first.width, second.width);
|
|
288
|
+
try std.testing.expectEqual(first.height, second.height);
|
|
289
|
+
try std.testing.expectEqual(fingerprint, std.hash.Wyhash.hash(0, registeredPixelsById(&harness.runtime, 23).?));
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
test "null image codec uses exact integer area weights for fractional boxes" {
|
|
293
|
+
const pixels = [_]u8{
|
|
294
|
+
0, 0, 0, 255, 60, 60, 60, 255, 120, 120, 120, 255,
|
|
295
|
+
};
|
|
296
|
+
const encoded = try encodedPng(std.testing.allocator, 3, 1, &pixels);
|
|
297
|
+
defer std.testing.allocator.free(encoded);
|
|
298
|
+
var output: [16]u8 = undefined;
|
|
299
|
+
const decoded = try canvas.png.decodeRgba8Fitted(encoded, &output, 2, platform.max_decoded_image_dimension);
|
|
300
|
+
try std.testing.expectEqual(@as(usize, 2), decoded.width);
|
|
301
|
+
try std.testing.expectEqual(@as(usize, 1), decoded.height);
|
|
302
|
+
try std.testing.expectEqualSlices(u8, &[_]u8{
|
|
303
|
+
20, 20, 20, 255,
|
|
304
|
+
100, 100, 100, 255,
|
|
305
|
+
}, decoded.rgba8);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
test "null image codec fits a source panorama past the decoded-axis ceiling" {
|
|
309
|
+
const source_width = platform.max_decoded_image_dimension + 1;
|
|
310
|
+
const pixels = try std.testing.allocator.alloc(u8, source_width * 4);
|
|
311
|
+
defer std.testing.allocator.free(pixels);
|
|
312
|
+
var offset: usize = 0;
|
|
313
|
+
while (offset < pixels.len) : (offset += 4) pixels[offset..][0..4].* = .{ 41, 83, 127, 255 };
|
|
314
|
+
|
|
315
|
+
const encoded = try encodedPng(std.testing.allocator, source_width, 1, pixels);
|
|
316
|
+
defer std.testing.allocator.free(encoded);
|
|
317
|
+
const output = try std.testing.allocator.alloc(u8, platform.max_decoded_image_dimension * 4);
|
|
318
|
+
defer std.testing.allocator.free(output);
|
|
319
|
+
|
|
320
|
+
const decoded = try canvas.png.decodeRgba8Fitted(
|
|
321
|
+
encoded,
|
|
322
|
+
output,
|
|
323
|
+
platform.max_decoded_image_dimension,
|
|
324
|
+
platform.max_decoded_image_dimension,
|
|
325
|
+
);
|
|
326
|
+
try std.testing.expectEqual(platform.max_decoded_image_dimension, decoded.width);
|
|
327
|
+
try std.testing.expectEqual(@as(usize, 1), decoded.height);
|
|
328
|
+
try std.testing.expectEqualSlices(u8, &[_]u8{ 41, 83, 127, 255 }, decoded.rgba8[0..4]);
|
|
329
|
+
try std.testing.expectEqualSlices(u8, &[_]u8{ 41, 83, 127, 255 }, decoded.rgba8[decoded.rgba8.len - 4 ..]);
|
|
330
|
+
}
|
|
331
|
+
|
|
210
332
|
test "registerCanvasImageBytes refuses reserved media-surface ids before decoding" {
|
|
211
333
|
const harness = try startedGpuHarness(std.testing.allocator);
|
|
212
334
|
defer harness.destroy(std.testing.allocator);
|
|
@@ -313,6 +435,28 @@ test "a fresh runtime allocates zero registered-image bytes until the first regi
|
|
|
313
435
|
try std.testing.expectEqual(@as(usize, 2), counting.allocations);
|
|
314
436
|
}
|
|
315
437
|
|
|
438
|
+
test "raised registered-image budget stays lazy and sizes each used slot" {
|
|
439
|
+
var counting = std.testing.FailingAllocator.init(std.testing.allocator, .{});
|
|
440
|
+
const harness = try startedGpuHarness(std.testing.allocator);
|
|
441
|
+
defer harness.destroy(std.testing.allocator);
|
|
442
|
+
core.Runtime.initAt(&harness.runtime, .{
|
|
443
|
+
.platform = harness.null_platform.platform(),
|
|
444
|
+
.trace_sink = harness.trace_sink.sink(),
|
|
445
|
+
.allocator = counting.allocator(),
|
|
446
|
+
.max_image_pixel_bytes = canvas_limits.max_registered_canvas_image_pixel_bytes_ceiling,
|
|
447
|
+
.environ = std.testing.environ,
|
|
448
|
+
});
|
|
449
|
+
harness.runtime.dispatch_error_policy = .propagate;
|
|
450
|
+
var app_state: RegistryApp = .{};
|
|
451
|
+
try harness.start(app_state.app());
|
|
452
|
+
try std.testing.expectEqual(@as(usize, 0), counting.allocations);
|
|
453
|
+
|
|
454
|
+
const pixel = [_]u8{ 1, 2, 3, 255 };
|
|
455
|
+
try harness.runtime.registerCanvasImage(1, 1, 1, &pixel);
|
|
456
|
+
try std.testing.expectEqual(@as(usize, 1), counting.allocations);
|
|
457
|
+
try std.testing.expectEqual(canvas_limits.max_registered_canvas_image_pixel_bytes_ceiling, counting.allocated_bytes);
|
|
458
|
+
}
|
|
459
|
+
|
|
316
460
|
test "image slot buffer ownership freezes at init: a mutated options.allocator sees zero activity" {
|
|
317
461
|
// The hazard pinned here: `Runtime.options` is public and mutable,
|
|
318
462
|
// so if the lazy slot allocation and deinit's free both read
|
|
@@ -24,8 +24,9 @@
|
|
|
24
24
|
//! re-upload without any explicit invalidation call.
|
|
25
25
|
//!
|
|
26
26
|
//! Capacities follow `canvas_limits`: `max_registered_canvas_images`
|
|
27
|
-
//! slots of
|
|
28
|
-
//!
|
|
27
|
+
//! slots of the runtime-frozen image budget (the 1 MiB default through
|
|
28
|
+
//! the 8 MiB ceiling). Raw-pixel overflow is `error.ImageTooLarge`;
|
|
29
|
+
//! encoded inputs decode-to-fit — never silent.
|
|
29
30
|
|
|
30
31
|
const std = @import("std");
|
|
31
32
|
const canvas = @import("canvas");
|
|
@@ -36,6 +37,7 @@ const runtime_media_surface = @import("media_surface.zig");
|
|
|
36
37
|
|
|
37
38
|
pub const max_registered_canvas_images = canvas_limits.max_registered_canvas_images;
|
|
38
39
|
pub const max_registered_canvas_image_pixel_bytes = canvas_limits.max_registered_canvas_image_pixel_bytes;
|
|
40
|
+
pub const max_registered_canvas_image_pixel_bytes_ceiling = canvas_limits.max_registered_canvas_image_pixel_bytes_ceiling;
|
|
39
41
|
|
|
40
42
|
/// One registered image's metadata; pixels live in the runtime's slot
|
|
41
43
|
/// buffer at the same index.
|
|
@@ -53,19 +55,25 @@ pub const RegisteredCanvasImage = struct {
|
|
|
53
55
|
height: usize = 0,
|
|
54
56
|
};
|
|
55
57
|
|
|
56
|
-
///
|
|
57
|
-
///
|
|
58
|
-
///
|
|
59
|
-
/// byte per row: raw stream <= pixels + pixels/4 since a row is at least
|
|
60
|
-
/// 4 pixel bytes). Loop-thread only, like the frame scratch — and
|
|
61
|
-
/// lazily heap-allocated per thread (1.25 MiB) on the first decode, so
|
|
62
|
-
/// threads that never register image bytes never carry it in their
|
|
63
|
-
/// static TLS block.
|
|
58
|
+
/// Runtime-sized decode scratch for `registerCanvasImageBytes`. The TLS
|
|
59
|
+
/// slot itself is one pointer; its backing grows lazily to the largest app
|
|
60
|
+
/// budget used by this loop thread and lives for the thread's lifetime.
|
|
64
61
|
const CanvasImageDecodeScratch = struct {
|
|
65
|
-
bytes: [
|
|
62
|
+
bytes: []u8 = &.{},
|
|
66
63
|
};
|
|
67
64
|
const canvas_image_decode_scratch = canvas.lazy_tls.LazyTls(CanvasImageDecodeScratch);
|
|
68
65
|
|
|
66
|
+
fn imageDecodeScratch(required: usize) error{OutOfMemory}![]u8 {
|
|
67
|
+
const scratch = canvas_image_decode_scratch.get();
|
|
68
|
+
if (scratch.bytes.len < required) {
|
|
69
|
+
scratch.bytes = if (scratch.bytes.len == 0)
|
|
70
|
+
try std.heap.page_allocator.alloc(u8, required)
|
|
71
|
+
else
|
|
72
|
+
try std.heap.page_allocator.realloc(scratch.bytes, required);
|
|
73
|
+
}
|
|
74
|
+
return scratch.bytes[0..required];
|
|
75
|
+
}
|
|
76
|
+
|
|
69
77
|
pub fn RuntimeCanvasImages(comptime Runtime: type) type {
|
|
70
78
|
return struct {
|
|
71
79
|
/// Register (or replace) decoded pixels under `id`: tightly
|
|
@@ -97,7 +105,7 @@ pub fn RuntimeCanvasImages(comptime Runtime: type) type {
|
|
|
97
105
|
const row_len = std.math.mul(usize, width, 4) catch return error.InvalidImageDimensions;
|
|
98
106
|
const byte_len = std.math.mul(usize, row_len, height) catch return error.InvalidImageDimensions;
|
|
99
107
|
if (rgba8.len != byte_len) return error.InvalidImageDimensions;
|
|
100
|
-
if (byte_len >
|
|
108
|
+
if (byte_len > self.max_image_pixel_bytes) return error.ImageTooLarge;
|
|
101
109
|
|
|
102
110
|
const index = findCanvasImageIndex(self, id) orelse blk: {
|
|
103
111
|
if (self.canvas_image_count >= max_registered_canvas_images) return error.ImageRegistryFull;
|
|
@@ -119,7 +127,7 @@ pub fn RuntimeCanvasImages(comptime Runtime: type) type {
|
|
|
119
127
|
// failable step past validation and it runs BEFORE any
|
|
120
128
|
// registry mutation, so an OOM refusal leaves the
|
|
121
129
|
// registry exactly as it was and the caller can retry.
|
|
122
|
-
self.canvas_image_pixels[index] = try self.owned_allocator.alloc(u8,
|
|
130
|
+
self.canvas_image_pixels[index] = try self.owned_allocator.alloc(u8, self.max_image_pixel_bytes);
|
|
123
131
|
}
|
|
124
132
|
@memcpy(self.canvas_image_pixels[index][0..byte_len], rgba8);
|
|
125
133
|
self.canvas_image_entries[index] = .{
|
|
@@ -147,7 +155,10 @@ pub fn RuntimeCanvasImages(comptime Runtime: type) type {
|
|
|
147
155
|
/// id in the model. On top of `registerCanvasImage`'s errors:
|
|
148
156
|
/// `error.UnsupportedService` (platform has no codec),
|
|
149
157
|
/// `error.ImageDecodeFailed` (undecodable bytes),
|
|
150
|
-
///
|
|
158
|
+
/// Encoded images decode aspect-preservingly to fit the runtime's
|
|
159
|
+
/// frozen slot budget. `error.ImageTooLarge` means the encoded source
|
|
160
|
+
/// exceeded the fixed image-source bound or a platform codec violated
|
|
161
|
+
/// the decode cap, not an ordinary photo whose pixels need fitting.
|
|
151
162
|
/// `error.InvalidImageId` covers the same ids
|
|
152
163
|
/// `registerCanvasImage` refuses (0 and the reserved
|
|
153
164
|
/// media-surface namespace) and fires before any decode work.
|
|
@@ -159,8 +170,16 @@ pub fn RuntimeCanvasImages(comptime Runtime: type) type {
|
|
|
159
170
|
// gets `error.InvalidImageId`, not a codec error, and pays
|
|
160
171
|
// no decode cost for it.
|
|
161
172
|
if ((id & canvas.media_surface_image_id_bit) != 0) return error.InvalidImageId;
|
|
162
|
-
|
|
163
|
-
|
|
173
|
+
// Every encoded-registration entry point shares imageLoad's
|
|
174
|
+
// fixed source contract. Direct fx.registerImageBytes and
|
|
175
|
+
// Runtime.registerCanvasImageBytes calls must not hand an
|
|
176
|
+
// arbitrarily large allocation to a host codec merely because
|
|
177
|
+
// they bypassed the file/fetch executor.
|
|
178
|
+
if (bytes.len > effects_mod.max_effect_image_source_bytes) return error.ImageTooLarge;
|
|
179
|
+
const scratch_len = self.max_image_pixel_bytes + self.max_image_pixel_bytes / 4;
|
|
180
|
+
const scratch = try imageDecodeScratch(scratch_len);
|
|
181
|
+
const decoded = try self.options.platform.services.decodeImage(bytes, scratch, self.max_image_pixel_bytes / 4);
|
|
182
|
+
if (decoded.rgba8.len > self.max_image_pixel_bytes) return error.ImageTooLarge;
|
|
164
183
|
try registerCanvasImage(self, id, decoded.width, decoded.height, decoded.rgba8);
|
|
165
184
|
return .{ .width = decoded.width, .height = decoded.height };
|
|
166
185
|
}
|