@native-sdk/cli 0.5.1 → 0.5.3
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.zig +41 -0
- package/package.json +9 -9
- package/packages/core/package-lock.json +2 -2
- package/packages/core/package.json +1 -1
- package/packages/core/sdk/events.ts +39 -2
- package/packages/core/src/checker.ts +1 -1
- package/packages/core/src/emitter.ts +39 -3
- package/packages/core/src/infer.ts +53 -7
- package/skill-data/automation/SKILL.md +7 -4
- package/skill-data/core/references/app-model-runtime.md +3 -0
- package/skill-data/native-ui/SKILL.md +3 -2
- package/src/app_runner/root.zig +25 -0
- package/src/automation/protocol.zig +20 -1
- package/src/embed/host.zig +27 -0
- package/src/embed/tests.zig +69 -0
- package/src/embed/ui_host.zig +4 -0
- package/src/platform/macos/appkit_host.h +11 -0
- package/src/platform/macos/appkit_host.m +130 -0
- package/src/platform/macos/root.zig +34 -0
- package/src/platform/null_platform_tests.zig +39 -0
- package/src/platform/root.zig +3 -0
- package/src/platform/types.zig +118 -4
- package/src/primitives/canvas/chart.zig +7 -7
- package/src/primitives/canvas/chart_tests.zig +66 -0
- package/src/primitives/canvas/commands.zig +48 -0
- package/src/primitives/canvas/events.zig +161 -0
- package/src/primitives/canvas/font_ttf.zig +155 -8
- package/src/primitives/canvas/font_ttf_tests.zig +686 -0
- package/src/primitives/canvas/reference.zig +67 -8
- package/src/primitives/canvas/render_fingerprints.zig +8 -0
- package/src/primitives/canvas/root.zig +26 -0
- package/src/primitives/canvas/tokens.zig +170 -7
- package/src/primitives/canvas/ui.zig +62 -7
- package/src/primitives/canvas/ui_markup.zig +51 -4
- package/src/primitives/canvas/ui_markup_binary_tests.zig +25 -0
- package/src/primitives/canvas/ui_markup_compiled.zig +49 -4
- package/src/primitives/canvas/ui_markup_compiled_tests.zig +160 -0
- package/src/primitives/canvas/ui_markup_contract.zig +9 -0
- package/src/primitives/canvas/ui_markup_contract_tests.zig +24 -0
- package/src/primitives/canvas/ui_markup_tests.zig +73 -0
- package/src/primitives/canvas/ui_markup_view.zig +63 -5
- package/src/primitives/canvas/ui_markup_view_tests.zig +151 -2
- package/src/primitives/canvas/ui_schema.zig +31 -1
- package/src/primitives/canvas/ui_schema_tests.zig +11 -11
- package/src/primitives/canvas/ui_tests.zig +31 -0
- package/src/primitives/canvas/vector.zig +258 -141
- package/src/primitives/canvas/widget_access.zig +1 -1
- package/src/primitives/canvas/widget_builtin_tests.zig +259 -0
- package/src/primitives/canvas/widget_layout.zig +13 -5
- package/src/primitives/canvas/widget_layout_tests.zig +82 -0
- package/src/primitives/canvas/widget_render.zig +78 -40
- package/src/primitives/canvas/widget_render_controls.zig +15 -4
- package/src/primitives/canvas/widget_semantics.zig +6 -1
- package/src/primitives/canvas/widget_semantics_tests.zig +168 -0
- package/src/primitives/canvas/widget_text_input.zig +118 -10
- package/src/primitives/canvas/widget_tree.zig +14 -2
- package/src/primitives/canvas/widgets.zig +28 -0
- package/src/root.zig +5 -0
- package/src/runtime/api.zig +13 -0
- package/src/runtime/automation_commands.zig +33 -0
- package/src/runtime/automation_widget_dispatch.zig +159 -28
- package/src/runtime/canvas_font_tests.zig +272 -3
- package/src/runtime/canvas_fonts.zig +60 -20
- package/src/runtime/canvas_frame.zig +15 -0
- package/src/runtime/canvas_frame_helpers.zig +15 -0
- package/src/runtime/canvas_image_tests.zig +26 -0
- package/src/runtime/canvas_images.zig +33 -7
- package/src/runtime/canvas_limits.zig +44 -8
- package/src/runtime/canvas_widget_anchored_tests.zig +253 -65
- package/src/runtime/canvas_widget_clipboard_tests.zig +123 -0
- package/src/runtime/canvas_widget_context_menu.zig +3 -2
- package/src/runtime/canvas_widget_context_menu_tests.zig +55 -0
- package/src/runtime/canvas_widget_events.zig +1295 -7
- package/src/runtime/canvas_widget_floating_tests.zig +2801 -0
- package/src/runtime/canvas_widget_runtime.zig +24 -7
- package/src/runtime/canvas_widget_scroll_drivers.zig +1 -1
- package/src/runtime/canvas_widget_state.zig +88 -6
- package/src/runtime/canvas_widget_text_tests.zig +277 -0
- package/src/runtime/core.zig +112 -6
- package/src/runtime/flow.zig +51 -1
- package/src/runtime/gpu_surface_events.zig +51 -5
- package/src/runtime/media_surface.zig +726 -0
- package/src/runtime/media_surface_tests.zig +1476 -0
- package/src/runtime/root.zig +9 -0
- package/src/runtime/session_journal.zig +52 -13
- package/src/runtime/session_record.zig +166 -0
- package/src/runtime/session_tests.zig +575 -0
- package/src/runtime/tests.zig +1 -0
- package/src/runtime/ts_ui_app.zig +56 -0
- package/src/runtime/ui_app.zig +64 -1
- package/src/runtime/ui_app_tests.zig +794 -0
- package/src/runtime/view.zig +86 -0
- package/src/runtime/view_widget_text.zig +18 -1
- package/src/runtime/view_widget_tree.zig +339 -20
- package/src/runtime/widget_bridge.zig +26 -0
- package/src/runtime/window_storage.zig +61 -15
- package/src/runtime/window_view_runtime.zig +13 -0
- package/src/runtime/window_views.zig +6 -0
|
@@ -0,0 +1,1476 @@
|
|
|
1
|
+
//! Media-surface texture channel tests: the synthetic producer battery.
|
|
2
|
+
//! Latest-wins staging under burst pushes, damage short-circuits at the
|
|
3
|
+
//! push and adoption boundaries, layout/clip compositing through the
|
|
4
|
+
//! widget tree, the reference renderer's deterministic placeholder (and
|
|
5
|
+
//! its independence from producer output), record/replay fingerprint
|
|
6
|
+
//! identity with NO producer attached, cross-thread pushes, and the
|
|
7
|
+
//! teardown discipline (a producer outliving its runtime pushes into
|
|
8
|
+
//! inert process-lived memory, never freed runtime state).
|
|
9
|
+
|
|
10
|
+
const std = @import("std");
|
|
11
|
+
const geometry = @import("geometry");
|
|
12
|
+
const canvas = @import("canvas");
|
|
13
|
+
const app_manifest = @import("app_manifest");
|
|
14
|
+
const core = @import("core.zig");
|
|
15
|
+
const canvas_limits = @import("canvas_limits.zig");
|
|
16
|
+
const media_surface = @import("media_surface.zig");
|
|
17
|
+
const ui_app_mod = @import("ui_app.zig");
|
|
18
|
+
const session_record = @import("session_record.zig");
|
|
19
|
+
const session_replay = @import("session_replay.zig");
|
|
20
|
+
const support = @import("test_support.zig");
|
|
21
|
+
|
|
22
|
+
const platform = support.platform;
|
|
23
|
+
const App = support.App;
|
|
24
|
+
const TestHarness = support.TestHarness;
|
|
25
|
+
|
|
26
|
+
const surface_id: u64 = 5;
|
|
27
|
+
|
|
28
|
+
fn startedGpuHarness(allocator: std.mem.Allocator) !*TestHarness() {
|
|
29
|
+
const harness = try TestHarness().create(allocator, .{ .size = geometry.SizeF.init(240, 140) });
|
|
30
|
+
errdefer harness.destroy(allocator);
|
|
31
|
+
harness.null_platform.gpu_surfaces = true;
|
|
32
|
+
return harness;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const ProbeApp = struct {
|
|
36
|
+
fn app(self: *@This()) App {
|
|
37
|
+
return .{ .context = self, .name = "media-surface-probe", .source = platform.WebViewSource.html("<h1>Media</h1>") };
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/// A tiny solid RGBA8 frame (2x2) in one color.
|
|
42
|
+
fn solidFrame(rgba: [4]u8) [16]u8 {
|
|
43
|
+
var frame: [16]u8 = undefined;
|
|
44
|
+
inline for (0..4) |pixel| @memcpy(frame[pixel * 4 .. pixel * 4 + 4], &rgba);
|
|
45
|
+
return frame;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
fn dispatchFrame(harness: *TestHarness(), app: App, frame_index: u64) !void {
|
|
49
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
|
|
50
|
+
.label = "canvas",
|
|
51
|
+
.size = geometry.SizeF.init(240, 140),
|
|
52
|
+
.scale_factor = 1,
|
|
53
|
+
.frame_index = frame_index,
|
|
54
|
+
.timestamp_ns = frame_index * 1_000_000,
|
|
55
|
+
.nonblank = true,
|
|
56
|
+
} });
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
test "burst pushes stage latest-wins and the frame clock adopts the newest" {
|
|
60
|
+
const harness = try startedGpuHarness(std.testing.allocator);
|
|
61
|
+
defer harness.destroy(std.testing.allocator);
|
|
62
|
+
var app_state: ProbeApp = .{};
|
|
63
|
+
const app = app_state.app();
|
|
64
|
+
try harness.start(app);
|
|
65
|
+
_ = try harness.runtime.createView(.{
|
|
66
|
+
.window_id = 1,
|
|
67
|
+
.label = "canvas",
|
|
68
|
+
.kind = .gpu_surface,
|
|
69
|
+
.frame = geometry.RectF.init(0, 0, 240, 140),
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
const producer = try harness.runtime.acquireMediaSurfaceProducer(surface_id);
|
|
73
|
+
defer producer.release();
|
|
74
|
+
|
|
75
|
+
// A burst of three distinct frames between presents: nothing adopts
|
|
76
|
+
// before the frame clock ticks, and the tick adopts ONLY the newest.
|
|
77
|
+
const red = solidFrame(.{ 255, 0, 0, 255 });
|
|
78
|
+
const green = solidFrame(.{ 0, 255, 0, 255 });
|
|
79
|
+
const blue = solidFrame(.{ 0, 0, 255, 255 });
|
|
80
|
+
try producer.pushFrame(2, 2, &red);
|
|
81
|
+
try producer.pushFrame(2, 2, &green);
|
|
82
|
+
try producer.pushFrame(2, 2, &blue);
|
|
83
|
+
try std.testing.expect(harness.runtime.adoptedMediaSurfaceTexture(surface_id) == null);
|
|
84
|
+
|
|
85
|
+
try dispatchFrame(harness, app, 1);
|
|
86
|
+
const adopted = harness.runtime.adoptedMediaSurfaceTexture(surface_id).?;
|
|
87
|
+
try std.testing.expectEqual(@as(usize, 2), adopted.width);
|
|
88
|
+
try std.testing.expectEqual(@as(usize, 2), adopted.height);
|
|
89
|
+
try std.testing.expect(adopted.fingerprint != 0);
|
|
90
|
+
|
|
91
|
+
// The adopted texture rides the frame planner's resource set as a
|
|
92
|
+
// presentation-only entry under the derived texture id, carrying
|
|
93
|
+
// the newest (blue) pixels and its precomputed fingerprint.
|
|
94
|
+
const resources = harness.runtime.registeredCanvasImages();
|
|
95
|
+
try std.testing.expectEqual(@as(usize, 1), resources.len);
|
|
96
|
+
try std.testing.expectEqual(canvas.mediaSurfaceTextureImageId(surface_id), resources[0].id);
|
|
97
|
+
try std.testing.expect(resources[0].presentation_only);
|
|
98
|
+
try std.testing.expectEqual(adopted.fingerprint, resources[0].content_fingerprint);
|
|
99
|
+
try std.testing.expectEqualSlices(u8, &blue, resources[0].pixels);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
test "unchanged frames short-circuit damage; changed frames invalidate and repaint" {
|
|
103
|
+
const harness = try startedGpuHarness(std.testing.allocator);
|
|
104
|
+
defer harness.destroy(std.testing.allocator);
|
|
105
|
+
var app_state: ProbeApp = .{};
|
|
106
|
+
const app = app_state.app();
|
|
107
|
+
try harness.start(app);
|
|
108
|
+
_ = try harness.runtime.createView(.{
|
|
109
|
+
.window_id = 1,
|
|
110
|
+
.label = "canvas",
|
|
111
|
+
.kind = .gpu_surface,
|
|
112
|
+
.frame = geometry.RectF.init(0, 0, 240, 140),
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
const producer = try harness.runtime.acquireMediaSurfaceProducer(surface_id);
|
|
116
|
+
defer producer.release();
|
|
117
|
+
|
|
118
|
+
const gray = solidFrame(.{ 40, 40, 40, 255 });
|
|
119
|
+
try producer.pushFrame(2, 2, &gray);
|
|
120
|
+
try dispatchFrame(harness, app, 1);
|
|
121
|
+
const first = harness.runtime.adoptedMediaSurfaceTexture(surface_id).?;
|
|
122
|
+
|
|
123
|
+
// Identical bytes pushed again: adopted state, the invalidation
|
|
124
|
+
// flag, and the prompt-frame request count all stay untouched.
|
|
125
|
+
harness.runtime.invalidated = false;
|
|
126
|
+
const requests_before = harness.null_platform.gpu_surface_frame_request_count;
|
|
127
|
+
try producer.pushFrame(2, 2, &gray);
|
|
128
|
+
try dispatchFrame(harness, app, 2);
|
|
129
|
+
try std.testing.expectEqual(first.fingerprint, harness.runtime.adoptedMediaSurfaceTexture(surface_id).?.fingerprint);
|
|
130
|
+
try std.testing.expect(!harness.runtime.invalidated);
|
|
131
|
+
try std.testing.expectEqual(requests_before, harness.null_platform.gpu_surface_frame_request_count);
|
|
132
|
+
|
|
133
|
+
// The ADOPTION boundary has its own gate: a fresh claim (whose
|
|
134
|
+
// push-boundary memory reset with the claim) staging bytes that
|
|
135
|
+
// match the adopted texture is dropped at adoption — no copy, no
|
|
136
|
+
// invalidation.
|
|
137
|
+
producer.release();
|
|
138
|
+
const reclaimed = try harness.runtime.acquireMediaSurfaceProducer(surface_id);
|
|
139
|
+
defer reclaimed.release();
|
|
140
|
+
harness.runtime.invalidated = false;
|
|
141
|
+
try reclaimed.pushFrame(2, 2, &gray);
|
|
142
|
+
try dispatchFrame(harness, app, 3);
|
|
143
|
+
try std.testing.expectEqual(first.fingerprint, harness.runtime.adoptedMediaSurfaceTexture(surface_id).?.fingerprint);
|
|
144
|
+
try std.testing.expect(!harness.runtime.invalidated);
|
|
145
|
+
try std.testing.expectEqual(requests_before, harness.null_platform.gpu_surface_frame_request_count);
|
|
146
|
+
|
|
147
|
+
// A changed frame invalidates, requests a prompt repaint, and
|
|
148
|
+
// re-fingerprints — the registered-image-swap contract.
|
|
149
|
+
const white = solidFrame(.{ 255, 255, 255, 255 });
|
|
150
|
+
try reclaimed.pushFrame(2, 2, &white);
|
|
151
|
+
try dispatchFrame(harness, app, 4);
|
|
152
|
+
const second = harness.runtime.adoptedMediaSurfaceTexture(surface_id).?;
|
|
153
|
+
try std.testing.expect(second.fingerprint != first.fingerprint);
|
|
154
|
+
try std.testing.expect(harness.runtime.invalidated);
|
|
155
|
+
try std.testing.expect(harness.null_platform.gpu_surface_frame_request_count > requests_before);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
test "the producer channel is loud about ids, dimensions, claims, and exhaustion" {
|
|
159
|
+
const harness = try startedGpuHarness(std.testing.allocator);
|
|
160
|
+
defer harness.destroy(std.testing.allocator);
|
|
161
|
+
var app_state: ProbeApp = .{};
|
|
162
|
+
try harness.start(app_state.app());
|
|
163
|
+
|
|
164
|
+
// Surface ids: 0 and the reserved texture-namespace bit are refused.
|
|
165
|
+
try std.testing.expectError(error.InvalidSurfaceId, harness.runtime.acquireMediaSurfaceProducer(0));
|
|
166
|
+
try std.testing.expectError(error.InvalidSurfaceId, harness.runtime.acquireMediaSurfaceProducer(canvas.media_surface_image_id_bit | 3));
|
|
167
|
+
// ...and the same bit is fenced off from canvas image registration,
|
|
168
|
+
// so producer textures can never be shadowed by a registered image.
|
|
169
|
+
const pixel = [_]u8{ 1, 2, 3, 255 };
|
|
170
|
+
try std.testing.expectError(error.InvalidImageId, harness.runtime.registerCanvasImage(canvas.media_surface_image_id_bit | 3, 1, 1, &pixel));
|
|
171
|
+
|
|
172
|
+
const producer = try harness.runtime.acquireMediaSurfaceProducer(surface_id);
|
|
173
|
+
// One live producer per (runtime, surface id).
|
|
174
|
+
try std.testing.expectError(error.MediaSurfaceInUse, harness.runtime.acquireMediaSurfaceProducer(surface_id));
|
|
175
|
+
|
|
176
|
+
// Frame validation mirrors the image registry's contract.
|
|
177
|
+
try std.testing.expectError(error.InvalidFrameDimensions, producer.pushFrame(0, 1, &pixel));
|
|
178
|
+
try std.testing.expectError(error.InvalidFrameDimensions, producer.pushFrame(2, 1, &pixel));
|
|
179
|
+
const oversized_bytes = canvas_limits.max_media_surface_pixel_bytes + 4;
|
|
180
|
+
const oversized = try std.testing.allocator.alloc(u8, oversized_bytes);
|
|
181
|
+
defer std.testing.allocator.free(oversized);
|
|
182
|
+
@memset(oversized, 0);
|
|
183
|
+
try std.testing.expectError(error.FrameTooLarge, producer.pushFrame(oversized_bytes / 4, 1, oversized));
|
|
184
|
+
|
|
185
|
+
// Exhaustion: every remaining process-wide slot claimed, the next
|
|
186
|
+
// acquire fails loudly, and a release frees a slot for reuse.
|
|
187
|
+
var extras: [canvas_limits.max_media_surface_channels - 1]media_surface.MediaSurfaceProducer = undefined;
|
|
188
|
+
for (&extras, 0..) |*extra, index| {
|
|
189
|
+
extra.* = try harness.runtime.acquireMediaSurfaceProducer(100 + index);
|
|
190
|
+
}
|
|
191
|
+
try std.testing.expectError(error.MediaSurfaceChannelsExhausted, harness.runtime.acquireMediaSurfaceProducer(999));
|
|
192
|
+
extras[0].release();
|
|
193
|
+
const reclaimed = try harness.runtime.acquireMediaSurfaceProducer(999);
|
|
194
|
+
reclaimed.release();
|
|
195
|
+
for (extras[1..]) |extra| extra.release();
|
|
196
|
+
|
|
197
|
+
// A released handle (and any copy of it) is refused, idempotently.
|
|
198
|
+
producer.release();
|
|
199
|
+
const after = solidFrame(.{ 9, 9, 9, 255 });
|
|
200
|
+
try std.testing.expectError(error.MediaSurfaceReleased, producer.pushFrame(2, 2, &after));
|
|
201
|
+
producer.release();
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// ----------------------------------------------- video-scale frames e2e
|
|
205
|
+
|
|
206
|
+
test "the producer frame budget and the host upload bound move in lockstep" {
|
|
207
|
+
// The upload validation (platform.types.uploadGpuSurfaceImage) is
|
|
208
|
+
// the one gate in front of every host's image store; if its
|
|
209
|
+
// media-namespace bound ever drifted below the producer's frame
|
|
210
|
+
// budget, a frame the producer accepted would be adopted and then
|
|
211
|
+
// refused at presentation — the silent-downstream failure this pin
|
|
212
|
+
// exists to prevent.
|
|
213
|
+
try std.testing.expectEqual(canvas_limits.max_media_surface_pixel_bytes, platform.max_gpu_surface_media_image_pixel_bytes);
|
|
214
|
+
// The budget's flagship claim: one 1080p RGBA8 frame fits.
|
|
215
|
+
try std.testing.expect(1920 * 1080 * 4 <= canvas_limits.max_media_surface_pixel_bytes);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
fn presentProbeFrame(harness: *TestHarness(), frame_index: u64) !core.CanvasPresentationResult {
|
|
219
|
+
var gpu_commands: [64]canvas.CanvasGpuCommand = undefined;
|
|
220
|
+
const packet_json_buffer = try std.testing.allocator.alloc(u8, platform.max_gpu_surface_packet_json_bytes);
|
|
221
|
+
defer std.testing.allocator.free(packet_json_buffer);
|
|
222
|
+
const pixels = try std.testing.allocator.alloc(u8, 240 * 140 * 4);
|
|
223
|
+
defer std.testing.allocator.free(pixels);
|
|
224
|
+
const scratch = try std.testing.allocator.alloc(u8, 240 * 140 * 4);
|
|
225
|
+
defer std.testing.allocator.free(scratch);
|
|
226
|
+
return harness.runtime.presentNextCanvasFrame(1, "canvas", .{
|
|
227
|
+
.frame_index = frame_index,
|
|
228
|
+
.timestamp_ns = frame_index * 1_000_000,
|
|
229
|
+
.surface_size = geometry.SizeF.init(240, 140),
|
|
230
|
+
.scale = 1,
|
|
231
|
+
}, support.canvasFrameScratchStorage(&harness.runtime), &gpu_commands, packet_json_buffer, pixels, scratch, canvas.Color.rgb8(0, 0, 0), null);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
fn setMediaSurfaceDisplayList(harness: *TestHarness(), bound_surface_id: u64) !void {
|
|
235
|
+
const commands = [_]canvas.CanvasCommand{
|
|
236
|
+
.{ .fill_rect = .{
|
|
237
|
+
.id = 1,
|
|
238
|
+
.rect = geometry.RectF.init(0, 0, 240, 140),
|
|
239
|
+
.fill = .{ .color = canvas.mediaSurfacePlaceholderColor(bound_surface_id) },
|
|
240
|
+
} },
|
|
241
|
+
.{ .draw_image = .{
|
|
242
|
+
.id = 2,
|
|
243
|
+
.image_id = canvas.mediaSurfaceTextureImageId(bound_surface_id),
|
|
244
|
+
.dst = geometry.RectF.init(0, 0, 240, 140),
|
|
245
|
+
} },
|
|
246
|
+
};
|
|
247
|
+
_ = try harness.runtime.setCanvasDisplayList(1, "canvas", .{ .commands = &commands });
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
test "a real 1080p frame pushes, adopts, and presents through the packet upload path" {
|
|
251
|
+
const harness = try startedGpuHarness(std.testing.allocator);
|
|
252
|
+
defer harness.destroy(std.testing.allocator);
|
|
253
|
+
var app_state: ProbeApp = .{};
|
|
254
|
+
const app = app_state.app();
|
|
255
|
+
try harness.start(app);
|
|
256
|
+
_ = try harness.runtime.createView(.{
|
|
257
|
+
.window_id = 1,
|
|
258
|
+
.label = "canvas",
|
|
259
|
+
.kind = .gpu_surface,
|
|
260
|
+
.frame = geometry.RectF.init(0, 0, 240, 140),
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
const producer = try harness.runtime.acquireMediaSurfaceProducer(surface_id);
|
|
264
|
+
defer producer.release();
|
|
265
|
+
|
|
266
|
+
// The budget's headline case, end to end: a full 1920x1080 RGBA8
|
|
267
|
+
// frame (8,294,400 bytes) — not a synthetic 2x2 — through push,
|
|
268
|
+
// adoption, and the packet present's host upload.
|
|
269
|
+
const frame_bytes = 1920 * 1080 * 4;
|
|
270
|
+
const frame = try std.testing.allocator.alloc(u8, frame_bytes);
|
|
271
|
+
defer std.testing.allocator.free(frame);
|
|
272
|
+
@memset(frame, 0);
|
|
273
|
+
frame[0] = 12;
|
|
274
|
+
frame[1] = 34;
|
|
275
|
+
frame[2] = 56;
|
|
276
|
+
frame[3] = 255;
|
|
277
|
+
try producer.pushFrame(1920, 1080, frame);
|
|
278
|
+
try dispatchFrame(harness, app, 1);
|
|
279
|
+
|
|
280
|
+
const adopted = harness.runtime.adoptedMediaSurfaceTexture(surface_id).?;
|
|
281
|
+
try std.testing.expectEqual(@as(usize, 1920), adopted.width);
|
|
282
|
+
try std.testing.expectEqual(@as(usize, 1080), adopted.height);
|
|
283
|
+
try std.testing.expectEqual(frame_bytes, adopted.byte_len);
|
|
284
|
+
|
|
285
|
+
try setMediaSurfaceDisplayList(harness, surface_id);
|
|
286
|
+
const result = try presentProbeFrame(harness, 2);
|
|
287
|
+
|
|
288
|
+
// The frame PRESENTED — as a packet, never the pixel fallback, and
|
|
289
|
+
// never an InvalidGpuSurfaceImage refusal after adoption.
|
|
290
|
+
try std.testing.expectEqual(core.CanvasPresentationMode.gpu_packet, result.mode);
|
|
291
|
+
try std.testing.expect(result.packet_representable);
|
|
292
|
+
|
|
293
|
+
// The full frame rode the binary upload side-channel to the host
|
|
294
|
+
// store under the derived texture id.
|
|
295
|
+
try std.testing.expectEqual(@as(usize, 1), harness.null_platform.gpu_surface_image_upload_count);
|
|
296
|
+
try std.testing.expectEqual(canvas.mediaSurfaceTextureImageId(surface_id), harness.null_platform.gpu_surface_image_upload_id);
|
|
297
|
+
try std.testing.expectEqual(@as(usize, 1920), harness.null_platform.gpu_surface_image_upload_width);
|
|
298
|
+
try std.testing.expectEqual(@as(usize, 1080), harness.null_platform.gpu_surface_image_upload_height);
|
|
299
|
+
try std.testing.expectEqual(frame_bytes, harness.null_platform.gpu_surface_image_upload_byte_len);
|
|
300
|
+
try std.testing.expectEqualDeep([4]u8{ 12, 34, 56, 255 }, harness.null_platform.gpuSurfaceImage(canvas.mediaSurfaceTextureImageId(surface_id)).?.sample_rgba);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
test "the frame budget's boundary is enforced at the producer, never downstream" {
|
|
304
|
+
const harness = try startedGpuHarness(std.testing.allocator);
|
|
305
|
+
defer harness.destroy(std.testing.allocator);
|
|
306
|
+
var app_state: ProbeApp = .{};
|
|
307
|
+
const app = app_state.app();
|
|
308
|
+
try harness.start(app);
|
|
309
|
+
_ = try harness.runtime.createView(.{
|
|
310
|
+
.window_id = 1,
|
|
311
|
+
.label = "canvas",
|
|
312
|
+
.kind = .gpu_surface,
|
|
313
|
+
.frame = geometry.RectF.init(0, 0, 240, 140),
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
const producer = try harness.runtime.acquireMediaSurfaceProducer(surface_id);
|
|
317
|
+
defer producer.release();
|
|
318
|
+
|
|
319
|
+
const budget = canvas_limits.max_media_surface_pixel_bytes;
|
|
320
|
+
const buffer = try std.testing.allocator.alloc(u8, budget + 4);
|
|
321
|
+
defer std.testing.allocator.free(buffer);
|
|
322
|
+
@memset(buffer, 200);
|
|
323
|
+
|
|
324
|
+
// One pixel past the budget refuses LOUDLY at the producer — the
|
|
325
|
+
// push boundary is where a too-large frame must die, so nothing
|
|
326
|
+
// over-budget can ever be staged, adopted, or handed to a host.
|
|
327
|
+
try std.testing.expectError(error.FrameTooLarge, producer.pushFrame(budget / 4 + 1, 1, buffer));
|
|
328
|
+
try dispatchFrame(harness, app, 1);
|
|
329
|
+
try std.testing.expect(harness.runtime.adoptedMediaSurfaceTexture(surface_id) == null);
|
|
330
|
+
|
|
331
|
+
// A frame at EXACTLY the budget passes the whole path: push, adopt,
|
|
332
|
+
// packet present, host upload of every byte.
|
|
333
|
+
try producer.pushFrame(2048, 1024, buffer[0..budget]);
|
|
334
|
+
try dispatchFrame(harness, app, 2);
|
|
335
|
+
try std.testing.expectEqual(budget, harness.runtime.adoptedMediaSurfaceTexture(surface_id).?.byte_len);
|
|
336
|
+
|
|
337
|
+
try setMediaSurfaceDisplayList(harness, surface_id);
|
|
338
|
+
const result = try presentProbeFrame(harness, 3);
|
|
339
|
+
try std.testing.expectEqual(core.CanvasPresentationMode.gpu_packet, result.mode);
|
|
340
|
+
try std.testing.expectEqual(@as(usize, 1), harness.null_platform.gpu_surface_image_upload_count);
|
|
341
|
+
try std.testing.expectEqual(budget, harness.null_platform.gpu_surface_image_upload_byte_len);
|
|
342
|
+
|
|
343
|
+
// The refused frame reached NO downstream layer: the only upload the
|
|
344
|
+
// host ever saw is the exact-budget frame. (The producer-side check
|
|
345
|
+
// firing FIRST is what the ordering above pins — the refusal
|
|
346
|
+
// happened before adoption could stage anything.)
|
|
347
|
+
try std.testing.expectEqual(canvas.mediaSurfaceTextureImageId(surface_id), harness.null_platform.gpu_surface_image_upload_id);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
// ------------------------------------------------------------- widget app
|
|
351
|
+
|
|
352
|
+
const media_canvas_label = "media-canvas";
|
|
353
|
+
|
|
354
|
+
const MediaModel = struct {
|
|
355
|
+
surface: u64 = surface_id,
|
|
356
|
+
count: u32 = 0,
|
|
357
|
+
};
|
|
358
|
+
|
|
359
|
+
const MediaMsg = union(enum) {
|
|
360
|
+
increment,
|
|
361
|
+
};
|
|
362
|
+
|
|
363
|
+
const MediaApp = ui_app_mod.UiApp(MediaModel, MediaMsg);
|
|
364
|
+
|
|
365
|
+
fn mediaUpdate(model: *MediaModel, msg: MediaMsg) void {
|
|
366
|
+
switch (msg) {
|
|
367
|
+
.increment => model.count += 1,
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
fn mediaView(ui: *MediaApp.Ui, model: *const MediaModel) MediaApp.Ui.Node {
|
|
372
|
+
// The surface sits inside a CLIPPING parent (scroll viewports
|
|
373
|
+
// always clip) that is shorter than the surface's declared height,
|
|
374
|
+
// so the clip test below can probe pixels outside the viewport:
|
|
375
|
+
// composited like any widget means the parent's clip crops it. The
|
|
376
|
+
// surface also carries its own corner radius, masked on the draw.
|
|
377
|
+
return ui.column(.{ .gap = 8, .padding = 10 }, .{
|
|
378
|
+
ui.el(.scroll_view, .{ .width = 160, .height = 60 }, .{
|
|
379
|
+
ui.mediaSurface(.{
|
|
380
|
+
.image = model.surface,
|
|
381
|
+
.width = 140,
|
|
382
|
+
.height = 120,
|
|
383
|
+
.style = .{ .radius = 12 },
|
|
384
|
+
.semantics = .{ .label = "Synthetic preview" },
|
|
385
|
+
}),
|
|
386
|
+
}),
|
|
387
|
+
ui.text(.{}, ui.fmt("Count {d}", .{model.count})),
|
|
388
|
+
ui.button(.{ .on_press = .increment }, "Increment"),
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
const media_views = [_]app_manifest.ShellView{
|
|
393
|
+
.{ .label = media_canvas_label, .kind = .gpu_surface, .fill = true, .gpu_backend = .metal },
|
|
394
|
+
};
|
|
395
|
+
const media_windows = [_]app_manifest.ShellWindow{.{
|
|
396
|
+
.label = "main",
|
|
397
|
+
.title = "Media",
|
|
398
|
+
.width = 240,
|
|
399
|
+
.height = 220,
|
|
400
|
+
.views = &media_views,
|
|
401
|
+
}};
|
|
402
|
+
const media_scene: app_manifest.ShellConfig = .{ .windows = &media_windows };
|
|
403
|
+
|
|
404
|
+
fn mediaOptions() MediaApp.Options {
|
|
405
|
+
return .{
|
|
406
|
+
.name = "media-surface-app",
|
|
407
|
+
.scene = media_scene,
|
|
408
|
+
.canvas_label = media_canvas_label,
|
|
409
|
+
.update = mediaUpdate,
|
|
410
|
+
.view = mediaView,
|
|
411
|
+
};
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
fn mediaAppFrame(harness: *TestHarness(), app: App, frame_index: u64) !void {
|
|
415
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
|
|
416
|
+
.label = media_canvas_label,
|
|
417
|
+
.size = geometry.SizeF.init(240, 220),
|
|
418
|
+
.scale_factor = 1,
|
|
419
|
+
.frame_index = frame_index,
|
|
420
|
+
.timestamp_ns = frame_index * 1_000_000,
|
|
421
|
+
.nonblank = true,
|
|
422
|
+
} });
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
fn mediaScreenshot(harness: *TestHarness(), out: []u8, scratch: []u8) !core.CanvasScreenshot {
|
|
426
|
+
return harness.runtime.renderCanvasScreenshot(1, media_canvas_label, null, out, scratch);
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
test "the widget composites the placeholder deterministically, clipped like any widget" {
|
|
430
|
+
const harness = try TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(240, 220) });
|
|
431
|
+
defer harness.destroy(std.testing.allocator);
|
|
432
|
+
harness.null_platform.gpu_surfaces = true;
|
|
433
|
+
|
|
434
|
+
const app_state = try std.testing.allocator.create(MediaApp);
|
|
435
|
+
defer std.testing.allocator.destroy(app_state);
|
|
436
|
+
app_state.* = MediaApp.init(std.testing.allocator, .{}, mediaOptions());
|
|
437
|
+
defer app_state.deinit();
|
|
438
|
+
const app = app_state.app();
|
|
439
|
+
try harness.start(app);
|
|
440
|
+
try mediaAppFrame(harness, app, 1);
|
|
441
|
+
try std.testing.expect(app_state.installed);
|
|
442
|
+
|
|
443
|
+
const pixel_size = try harness.runtime.canvasScreenshotPixelSize(1, media_canvas_label, null);
|
|
444
|
+
const pixels = try std.testing.allocator.alloc(u8, pixel_size.byte_len);
|
|
445
|
+
defer std.testing.allocator.free(pixels);
|
|
446
|
+
const scratch = try std.testing.allocator.alloc(u8, pixel_size.byte_len);
|
|
447
|
+
defer std.testing.allocator.free(scratch);
|
|
448
|
+
|
|
449
|
+
// Locate the surface's laid-out frame and its clipping parent.
|
|
450
|
+
const layout = try harness.runtime.canvasWidgetLayout(1, media_canvas_label);
|
|
451
|
+
var surface_frame: ?geometry.RectF = null;
|
|
452
|
+
var viewport_frame: ?geometry.RectF = null;
|
|
453
|
+
for (layout.nodes) |node| {
|
|
454
|
+
if (node.widget.kind == .media_surface) surface_frame = node.frame;
|
|
455
|
+
if (node.widget.kind == .scroll_view) viewport_frame = node.frame;
|
|
456
|
+
}
|
|
457
|
+
const media_frame = surface_frame.?;
|
|
458
|
+
const clip_frame = viewport_frame.?;
|
|
459
|
+
// The declared height (120) overflows the 60-tall clipping viewport.
|
|
460
|
+
try std.testing.expect(media_frame.maxY() > clip_frame.maxY());
|
|
461
|
+
|
|
462
|
+
const placeholder = canvas.mediaSurfacePlaceholderColor(surface_id);
|
|
463
|
+
const expected = [4]u8{
|
|
464
|
+
@intFromFloat(@round(placeholder.r * 255)),
|
|
465
|
+
@intFromFloat(@round(placeholder.g * 255)),
|
|
466
|
+
@intFromFloat(@round(placeholder.b * 255)),
|
|
467
|
+
255,
|
|
468
|
+
};
|
|
469
|
+
|
|
470
|
+
const before = try mediaScreenshot(harness, pixels, scratch);
|
|
471
|
+
const inside_x: usize = @intFromFloat(media_frame.x + 8);
|
|
472
|
+
const inside_y: usize = @intFromFloat(media_frame.y + 8);
|
|
473
|
+
const inside = (inside_y * before.width + inside_x) * 4;
|
|
474
|
+
try std.testing.expectEqualSlices(u8, &expected, before.rgba8[inside .. inside + 4]);
|
|
475
|
+
// Outside the clipping parent (but inside the surface's declared
|
|
476
|
+
// frame): the parent's clip crops the surface like any widget.
|
|
477
|
+
const clipped_y: usize = @intFromFloat(clip_frame.maxY() + 4);
|
|
478
|
+
const clipped = (clipped_y * before.width + inside_x) * 4;
|
|
479
|
+
try std.testing.expect(!std.mem.eql(u8, &expected, before.rgba8[clipped .. clipped + 4]));
|
|
480
|
+
// The surface's own corner radius masks the draw: the rounded
|
|
481
|
+
// corner pixel is not placeholder-colored while the face is.
|
|
482
|
+
const corner_x: usize = @intFromFloat(media_frame.x + 1);
|
|
483
|
+
const corner_y: usize = @intFromFloat(media_frame.y + 1);
|
|
484
|
+
const corner = (corner_y * before.width + corner_x) * 4;
|
|
485
|
+
try std.testing.expect(!std.mem.eql(u8, &expected, before.rgba8[corner .. corner + 4]));
|
|
486
|
+
|
|
487
|
+
// Reference-placeholder determinism: a producer pushing REAL frames
|
|
488
|
+
// changes nothing in the reference render — goldens can never
|
|
489
|
+
// depend on producer output. Byte-identical before/after.
|
|
490
|
+
const golden = try std.testing.allocator.dupe(u8, before.rgba8);
|
|
491
|
+
defer std.testing.allocator.free(golden);
|
|
492
|
+
|
|
493
|
+
const producer = try harness.runtime.acquireMediaSurfaceProducer(surface_id);
|
|
494
|
+
defer producer.release();
|
|
495
|
+
const magenta = solidFrame(.{ 255, 0, 255, 255 });
|
|
496
|
+
try producer.pushFrame(2, 2, &magenta);
|
|
497
|
+
try mediaAppFrame(harness, app, 2);
|
|
498
|
+
try std.testing.expect(harness.runtime.adoptedMediaSurfaceTexture(surface_id) != null);
|
|
499
|
+
|
|
500
|
+
const after = try mediaScreenshot(harness, pixels, scratch);
|
|
501
|
+
try std.testing.expectEqualSlices(u8, golden, after.rgba8);
|
|
502
|
+
|
|
503
|
+
// ...while the GPU/packet side of the same plan sees the texture:
|
|
504
|
+
// the resource set carries the adopted pixels for upload.
|
|
505
|
+
const resources = harness.runtime.registeredCanvasImages();
|
|
506
|
+
try std.testing.expectEqual(@as(usize, 1), resources.len);
|
|
507
|
+
try std.testing.expectEqualSlices(u8, &magenta, resources[0].pixels);
|
|
508
|
+
try std.testing.expect(resources[0].presentation_only);
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
test "the surface's a11y line is producer-independent, so fingerprints exclude texture contents" {
|
|
512
|
+
const harness = try TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(240, 220) });
|
|
513
|
+
defer harness.destroy(std.testing.allocator);
|
|
514
|
+
harness.null_platform.gpu_surfaces = true;
|
|
515
|
+
|
|
516
|
+
const app_state = try std.testing.allocator.create(MediaApp);
|
|
517
|
+
defer std.testing.allocator.destroy(app_state);
|
|
518
|
+
app_state.* = MediaApp.init(std.testing.allocator, .{}, mediaOptions());
|
|
519
|
+
defer app_state.deinit();
|
|
520
|
+
const app = app_state.app();
|
|
521
|
+
try harness.start(app);
|
|
522
|
+
try mediaAppFrame(harness, app, 1);
|
|
523
|
+
|
|
524
|
+
const before = harness.runtime.sessionStateFingerprint();
|
|
525
|
+
try std.testing.expect(before != 0);
|
|
526
|
+
|
|
527
|
+
const producer = try harness.runtime.acquireMediaSurfaceProducer(surface_id);
|
|
528
|
+
defer producer.release();
|
|
529
|
+
const cyan = solidFrame(.{ 0, 255, 255, 255 });
|
|
530
|
+
try producer.pushFrame(2, 2, &cyan);
|
|
531
|
+
try mediaAppFrame(harness, app, 2);
|
|
532
|
+
try std.testing.expect(harness.runtime.adoptedMediaSurfaceTexture(surface_id) != null);
|
|
533
|
+
|
|
534
|
+
// Texture contents are presentation chrome: the session fingerprint
|
|
535
|
+
// (hashed over the a11y tree) is identical with frames flowing.
|
|
536
|
+
try std.testing.expectEqual(before, harness.runtime.sessionStateFingerprint());
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
// ------------------------------------------------------- entry reclaim
|
|
540
|
+
|
|
541
|
+
test "reclaiming a retained texture removes the host image and the widget serves the placeholder" {
|
|
542
|
+
const harness = try TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(240, 220) });
|
|
543
|
+
defer harness.destroy(std.testing.allocator);
|
|
544
|
+
harness.null_platform.gpu_surfaces = true;
|
|
545
|
+
|
|
546
|
+
const app_state = try std.testing.allocator.create(MediaApp);
|
|
547
|
+
defer std.testing.allocator.destroy(app_state);
|
|
548
|
+
app_state.* = MediaApp.init(std.testing.allocator, .{}, mediaOptions());
|
|
549
|
+
defer app_state.deinit();
|
|
550
|
+
const app = app_state.app();
|
|
551
|
+
try harness.start(app);
|
|
552
|
+
try mediaAppFrame(harness, app, 1);
|
|
553
|
+
|
|
554
|
+
const pixel_size = try harness.runtime.canvasScreenshotPixelSize(1, media_canvas_label, null);
|
|
555
|
+
const pixels = try std.testing.allocator.alloc(u8, pixel_size.byte_len);
|
|
556
|
+
defer std.testing.allocator.free(pixels);
|
|
557
|
+
const scratch = try std.testing.allocator.alloc(u8, pixel_size.byte_len);
|
|
558
|
+
defer std.testing.allocator.free(scratch);
|
|
559
|
+
const before = try mediaScreenshot(harness, pixels, scratch);
|
|
560
|
+
const golden = try std.testing.allocator.dupe(u8, before.rgba8);
|
|
561
|
+
defer std.testing.allocator.free(golden);
|
|
562
|
+
|
|
563
|
+
// A producer pushes; the app's frame presents the packet, so the
|
|
564
|
+
// texture rides the upload side-channel into the HOST-WIDE store —
|
|
565
|
+
// the store AppKit's packet draw resolves images from.
|
|
566
|
+
const texture_id = canvas.mediaSurfaceTextureImageId(surface_id);
|
|
567
|
+
const producer = try harness.runtime.acquireMediaSurfaceProducer(surface_id);
|
|
568
|
+
const magenta = solidFrame(.{ 255, 0, 255, 255 });
|
|
569
|
+
try producer.pushFrame(2, 2, &magenta);
|
|
570
|
+
try mediaAppFrame(harness, app, 2);
|
|
571
|
+
try std.testing.expect(harness.null_platform.gpuSurfaceImage(texture_id) != null);
|
|
572
|
+
|
|
573
|
+
// Release retains the last adopted frame (a paused player keeps its
|
|
574
|
+
// final picture): the entry, the resource, and the host copy all
|
|
575
|
+
// stay — release is NOT a reclaim.
|
|
576
|
+
producer.release();
|
|
577
|
+
try mediaAppFrame(harness, app, 3);
|
|
578
|
+
try std.testing.expect(harness.runtime.adoptedMediaSurfaceTexture(surface_id) != null);
|
|
579
|
+
try std.testing.expect(harness.null_platform.gpuSurfaceImage(texture_id) != null);
|
|
580
|
+
try std.testing.expectEqual(@as(usize, 0), harness.null_platform.gpu_surface_image_remove_count);
|
|
581
|
+
|
|
582
|
+
// Fill every runtime texture entry with LIVE channels, then adopt
|
|
583
|
+
// one more: the retained entry is the only inactive one, so it is
|
|
584
|
+
// reclaimed — and the host-side image must go with it.
|
|
585
|
+
var live: [canvas_limits.max_media_surface_channels]media_surface.MediaSurfaceProducer = undefined;
|
|
586
|
+
for (&live, 0..) |*extra, index| {
|
|
587
|
+
extra.* = try harness.runtime.acquireMediaSurfaceProducer(101 + index);
|
|
588
|
+
const shade: u8 = @intCast(index + 1);
|
|
589
|
+
const frame = solidFrame(.{ shade, shade, shade, 255 });
|
|
590
|
+
try extra.pushFrame(2, 2, &frame);
|
|
591
|
+
}
|
|
592
|
+
defer for (live) |extra| extra.release();
|
|
593
|
+
try mediaAppFrame(harness, app, 4);
|
|
594
|
+
|
|
595
|
+
// The reclaim removed the host texture through the platform seam.
|
|
596
|
+
try std.testing.expect(harness.null_platform.gpu_surface_image_remove_count >= 1);
|
|
597
|
+
try std.testing.expectEqual(texture_id, harness.null_platform.gpu_surface_image_remove_id);
|
|
598
|
+
try std.testing.expect(harness.null_platform.gpuSurfaceImage(texture_id) == null);
|
|
599
|
+
|
|
600
|
+
// Both engines now resolve the id to NOTHING: the runtime resource
|
|
601
|
+
// set no longer carries it (packet hosts skip the draw — no stale
|
|
602
|
+
// store image left to resolve), and the reference render of the
|
|
603
|
+
// still-bound widget is byte-identical to the placeholder golden.
|
|
604
|
+
try std.testing.expect(harness.runtime.adoptedMediaSurfaceTexture(surface_id) == null);
|
|
605
|
+
for (harness.runtime.registeredCanvasImages()) |resource| {
|
|
606
|
+
try std.testing.expect(resource.id != texture_id);
|
|
607
|
+
}
|
|
608
|
+
try mediaAppFrame(harness, app, 5);
|
|
609
|
+
const after = try mediaScreenshot(harness, pixels, scratch);
|
|
610
|
+
try std.testing.expectEqualSlices(u8, golden, after.rgba8);
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
test "reclaim is safe on hosts without the upload seam" {
|
|
614
|
+
const harness = try startedGpuHarness(std.testing.allocator);
|
|
615
|
+
defer harness.destroy(std.testing.allocator);
|
|
616
|
+
// Model a platform that never wired image uploads: the reclaim's
|
|
617
|
+
// best-effort removal must swallow UnsupportedService, exactly like
|
|
618
|
+
// unregisterCanvasImage does.
|
|
619
|
+
harness.null_platform.gpu_surface_image_uploads = false;
|
|
620
|
+
var app_state: ProbeApp = .{};
|
|
621
|
+
const app = app_state.app();
|
|
622
|
+
try harness.start(app);
|
|
623
|
+
_ = try harness.runtime.createView(.{
|
|
624
|
+
.window_id = 1,
|
|
625
|
+
.label = "canvas",
|
|
626
|
+
.kind = .gpu_surface,
|
|
627
|
+
.frame = geometry.RectF.init(0, 0, 240, 140),
|
|
628
|
+
});
|
|
629
|
+
|
|
630
|
+
const retained = try harness.runtime.acquireMediaSurfaceProducer(surface_id);
|
|
631
|
+
const first = solidFrame(.{ 1, 1, 1, 255 });
|
|
632
|
+
try retained.pushFrame(2, 2, &first);
|
|
633
|
+
try dispatchFrame(harness, app, 1);
|
|
634
|
+
retained.release();
|
|
635
|
+
|
|
636
|
+
var live: [canvas_limits.max_media_surface_channels]media_surface.MediaSurfaceProducer = undefined;
|
|
637
|
+
for (&live, 0..) |*extra, index| {
|
|
638
|
+
extra.* = try harness.runtime.acquireMediaSurfaceProducer(301 + index);
|
|
639
|
+
const shade: u8 = @intCast(index + 1);
|
|
640
|
+
const frame = solidFrame(.{ shade, 0, 0, 255 });
|
|
641
|
+
try extra.pushFrame(2, 2, &frame);
|
|
642
|
+
}
|
|
643
|
+
defer for (live) |extra| extra.release();
|
|
644
|
+
// The adoption reclaims the retained entry; the platform refuses the
|
|
645
|
+
// removal with UnsupportedService and adoption proceeds anyway.
|
|
646
|
+
try dispatchFrame(harness, app, 2);
|
|
647
|
+
try std.testing.expect(harness.runtime.adoptedMediaSurfaceTexture(surface_id) == null);
|
|
648
|
+
try std.testing.expect(harness.runtime.adoptedMediaSurfaceTexture(301) != null);
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
test "surface-id rotation never grows the host image store" {
|
|
652
|
+
const harness = try startedGpuHarness(std.testing.allocator);
|
|
653
|
+
defer harness.destroy(std.testing.allocator);
|
|
654
|
+
var app_state: ProbeApp = .{};
|
|
655
|
+
const app = app_state.app();
|
|
656
|
+
try harness.start(app);
|
|
657
|
+
_ = try harness.runtime.createView(.{
|
|
658
|
+
.window_id = 1,
|
|
659
|
+
.label = "canvas",
|
|
660
|
+
.kind = .gpu_surface,
|
|
661
|
+
.frame = geometry.RectF.init(0, 0, 240, 140),
|
|
662
|
+
});
|
|
663
|
+
|
|
664
|
+
// The rotating-player shape: every rotation binds a NEW surface id,
|
|
665
|
+
// pushes a frame, presents it (uploading the texture to the host
|
|
666
|
+
// store), and releases. Ids rotate far past the channel budget; the
|
|
667
|
+
// host store must stay bounded by it — before the reclaim removed
|
|
668
|
+
// host images, every rotation left one stale NSImage-analog behind.
|
|
669
|
+
const rotations: usize = canvas_limits.max_media_surface_channels * 3;
|
|
670
|
+
var rotation: usize = 0;
|
|
671
|
+
while (rotation < rotations) : (rotation += 1) {
|
|
672
|
+
const rotating_id: u64 = 400 + rotation;
|
|
673
|
+
const producer = try harness.runtime.acquireMediaSurfaceProducer(rotating_id);
|
|
674
|
+
const shade: u8 = @intCast(rotation + 1);
|
|
675
|
+
const frame = solidFrame(.{ shade, shade, 0, 255 });
|
|
676
|
+
try producer.pushFrame(2, 2, &frame);
|
|
677
|
+
try setMediaSurfaceDisplayList(harness, rotating_id);
|
|
678
|
+
try dispatchFrame(harness, app, rotation + 1);
|
|
679
|
+
_ = try presentProbeFrame(harness, rotation + 1);
|
|
680
|
+
try std.testing.expect(harness.null_platform.gpu_surface_image_count <= canvas_limits.max_media_surface_channels);
|
|
681
|
+
producer.release();
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
// Every rotation past the entry budget reclaimed one retained
|
|
685
|
+
// texture and removed its host image; the store ends bounded.
|
|
686
|
+
try std.testing.expectEqual(rotations - canvas_limits.max_media_surface_channels, harness.null_platform.gpu_surface_image_remove_count);
|
|
687
|
+
try std.testing.expect(harness.null_platform.gpu_surface_image_count <= canvas_limits.max_media_surface_channels);
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
// ------------------------------------------------------- record / replay
|
|
691
|
+
|
|
692
|
+
const JournalBuffer = struct {
|
|
693
|
+
bytes: [256 * 1024]u8 = undefined,
|
|
694
|
+
len: usize = 0,
|
|
695
|
+
|
|
696
|
+
fn sink(self: *JournalBuffer) session_record.RecorderSink {
|
|
697
|
+
return .{ .context = self, .write_fn = write };
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
fn write(context: *anyopaque, bytes: []const u8) anyerror!void {
|
|
701
|
+
const self: *JournalBuffer = @ptrCast(@alignCast(context));
|
|
702
|
+
if (self.len + bytes.len > self.bytes.len) return error.NoSpaceLeft;
|
|
703
|
+
@memcpy(self.bytes[self.len .. self.len + bytes.len], bytes);
|
|
704
|
+
self.len += bytes.len;
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
fn journalBytes(self: *const JournalBuffer) []const u8 {
|
|
708
|
+
return self.bytes[0..self.len];
|
|
709
|
+
}
|
|
710
|
+
};
|
|
711
|
+
|
|
712
|
+
test "a session recorded with a live producer replays fingerprint-identical with NO producer" {
|
|
713
|
+
const gpa = std.testing.allocator;
|
|
714
|
+
const buffer = try std.heap.page_allocator.create(JournalBuffer);
|
|
715
|
+
defer std.heap.page_allocator.destroy(buffer);
|
|
716
|
+
buffer.len = 0;
|
|
717
|
+
|
|
718
|
+
// Record: a producer pushes distinct frames between presented
|
|
719
|
+
// frames while the app also does model work (increments), with
|
|
720
|
+
// per-frame fingerprint checkpoints.
|
|
721
|
+
var recorded_model: MediaModel = undefined;
|
|
722
|
+
var recorded_fingerprint: u64 = 0;
|
|
723
|
+
{
|
|
724
|
+
const recorder = try std.heap.page_allocator.create(session_record.SessionRecorder);
|
|
725
|
+
defer std.heap.page_allocator.destroy(recorder);
|
|
726
|
+
recorder.* = session_record.SessionRecorder.init(buffer.sink());
|
|
727
|
+
recorder.begin(.{ .platform_name = "test", .app_name = "media-surface-app", .window_width = 240, .window_height = 220 });
|
|
728
|
+
|
|
729
|
+
const harness = try TestHarness().create(gpa, .{ .size = geometry.SizeF.init(240, 220) });
|
|
730
|
+
defer harness.destroy(gpa);
|
|
731
|
+
harness.null_platform.gpu_surfaces = true;
|
|
732
|
+
harness.runtime.options.session_recorder = recorder;
|
|
733
|
+
|
|
734
|
+
const app_state = try gpa.create(MediaApp);
|
|
735
|
+
defer gpa.destroy(app_state);
|
|
736
|
+
app_state.* = MediaApp.init(std.heap.page_allocator, .{}, mediaOptions());
|
|
737
|
+
defer app_state.deinit();
|
|
738
|
+
const app = app_state.app();
|
|
739
|
+
try harness.start(app);
|
|
740
|
+
try mediaAppFrame(harness, app, 1);
|
|
741
|
+
try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
|
|
742
|
+
|
|
743
|
+
const producer = try harness.runtime.acquireMediaSurfaceProducer(surface_id);
|
|
744
|
+
defer producer.release();
|
|
745
|
+
|
|
746
|
+
const layout = try harness.runtime.canvasWidgetLayout(1, media_canvas_label);
|
|
747
|
+
var button_frame: ?geometry.RectF = null;
|
|
748
|
+
for (layout.nodes) |node| {
|
|
749
|
+
if (node.widget.kind == .button) button_frame = node.frame;
|
|
750
|
+
}
|
|
751
|
+
const press_frame = button_frame.?;
|
|
752
|
+
|
|
753
|
+
var frame_index: u64 = 2;
|
|
754
|
+
var shade: u8 = 10;
|
|
755
|
+
while (frame_index < 6) : (frame_index += 1) {
|
|
756
|
+
const frame = solidFrame(.{ shade, shade, 0, 255 });
|
|
757
|
+
try producer.pushFrame(2, 2, &frame);
|
|
758
|
+
shade +%= 40;
|
|
759
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
|
|
760
|
+
.window_id = 1,
|
|
761
|
+
.label = media_canvas_label,
|
|
762
|
+
.kind = .pointer_down,
|
|
763
|
+
.x = press_frame.x + press_frame.width * 0.5,
|
|
764
|
+
.y = press_frame.y + press_frame.height * 0.5,
|
|
765
|
+
} });
|
|
766
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
|
|
767
|
+
.window_id = 1,
|
|
768
|
+
.label = media_canvas_label,
|
|
769
|
+
.kind = .pointer_up,
|
|
770
|
+
.x = press_frame.x + press_frame.width * 0.5,
|
|
771
|
+
.y = press_frame.y + press_frame.height * 0.5,
|
|
772
|
+
} });
|
|
773
|
+
try mediaAppFrame(harness, app, frame_index);
|
|
774
|
+
try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
|
|
775
|
+
}
|
|
776
|
+
try std.testing.expect(app_state.model.count > 0);
|
|
777
|
+
try std.testing.expect(harness.runtime.adoptedMediaSurfaceTexture(surface_id) != null);
|
|
778
|
+
|
|
779
|
+
recorder.finish();
|
|
780
|
+
try std.testing.expect(!recorder.failed);
|
|
781
|
+
recorded_model = app_state.model;
|
|
782
|
+
recorded_fingerprint = harness.runtime.sessionStateFingerprint();
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
// Replay into a fresh runtime and app with NO producer attached:
|
|
786
|
+
// every checkpoint verifies and the final fingerprint matches —
|
|
787
|
+
// texture contents were never part of the session's identity.
|
|
788
|
+
{
|
|
789
|
+
const harness = try TestHarness().create(gpa, .{ .size = geometry.SizeF.init(240, 220) });
|
|
790
|
+
defer harness.destroy(gpa);
|
|
791
|
+
harness.null_platform.gpu_surfaces = true;
|
|
792
|
+
|
|
793
|
+
const app_state = try gpa.create(MediaApp);
|
|
794
|
+
defer gpa.destroy(app_state);
|
|
795
|
+
app_state.* = MediaApp.init(std.heap.page_allocator, .{}, mediaOptions());
|
|
796
|
+
defer app_state.deinit();
|
|
797
|
+
|
|
798
|
+
const report = try session_replay.replaySession(&harness.runtime, app_state.app(), buffer.journalBytes(), .{
|
|
799
|
+
.verify = true,
|
|
800
|
+
.require_same_platform = false,
|
|
801
|
+
});
|
|
802
|
+
try std.testing.expect(report.ok());
|
|
803
|
+
try std.testing.expect(report.events_replayed > 0);
|
|
804
|
+
try std.testing.expect(report.checkpoints_verified > 0);
|
|
805
|
+
try std.testing.expect(harness.runtime.adoptedMediaSurfaceTexture(surface_id) == null);
|
|
806
|
+
try std.testing.expectEqualDeep(recorded_model, app_state.model);
|
|
807
|
+
try std.testing.expectEqual(recorded_fingerprint, harness.runtime.sessionStateFingerprint());
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
// ---------------------------------------------------- docs example pin
|
|
812
|
+
|
|
813
|
+
/// The public SDK root, exactly as an app or a toolkit extension
|
|
814
|
+
/// imports it: the docs' producer recipe must be writable against THESE
|
|
815
|
+
/// exports, not module-internal paths.
|
|
816
|
+
const media = @import("../root.zig");
|
|
817
|
+
|
|
818
|
+
test "docs example: the typed producer callback is writable against the public exports" {
|
|
819
|
+
// docs/src/app/media-producers/page.mdx, "The mpv shape": the docs
|
|
820
|
+
// machinery checks prose patterns, not Zig fences (the
|
|
821
|
+
// test-docs-media-producer-contracts step pins the signature in the
|
|
822
|
+
// page AND in this file), so this test is the compile-shaped mirror
|
|
823
|
+
// of the example — the same callback signature, driven against a
|
|
824
|
+
// real claim so the pin is behavior, not just types.
|
|
825
|
+
const Mpv = struct {
|
|
826
|
+
pixels: [16]u8,
|
|
827
|
+
stopped: bool = false,
|
|
828
|
+
|
|
829
|
+
const Frame = struct { width: usize, height: usize, pixels: []const u8 };
|
|
830
|
+
|
|
831
|
+
fn renderFrameRgba8(self: *@This()) Frame {
|
|
832
|
+
return .{ .width = 2, .height = 2, .pixels = &self.pixels };
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
fn stop(self: *@This()) void {
|
|
836
|
+
self.stopped = true;
|
|
837
|
+
}
|
|
838
|
+
};
|
|
839
|
+
const Glue = struct {
|
|
840
|
+
// The docs example's exact signature (`media` being the app's
|
|
841
|
+
// alias for the SDK root).
|
|
842
|
+
fn onMpvFrame(player: *Mpv, producer: media.MediaSurfaceProducer) void {
|
|
843
|
+
const frame = player.renderFrameRgba8();
|
|
844
|
+
producer.pushFrame(frame.width, frame.height, frame.pixels) catch |err| switch (err) {
|
|
845
|
+
error.MediaSurfaceReleased => player.stop(), // the app tore the surface down
|
|
846
|
+
else => {},
|
|
847
|
+
};
|
|
848
|
+
}
|
|
849
|
+
};
|
|
850
|
+
|
|
851
|
+
// The handle `Runtime.acquireMediaSurfaceProducer` returns IS the
|
|
852
|
+
// exported type, at the SDK root and the runtime root alike (and
|
|
853
|
+
// the channel budgets ride beside it, like the image registry's).
|
|
854
|
+
comptime std.debug.assert(media.MediaSurfaceProducer == media_surface.MediaSurfaceProducer);
|
|
855
|
+
comptime std.debug.assert(media.runtime.MediaSurfaceProducer == media.MediaSurfaceProducer);
|
|
856
|
+
comptime std.debug.assert(media.max_media_surface_channels == canvas_limits.max_media_surface_channels);
|
|
857
|
+
comptime std.debug.assert(media.max_media_surface_pixel_bytes == canvas_limits.max_media_surface_pixel_bytes);
|
|
858
|
+
|
|
859
|
+
const harness = try startedGpuHarness(std.testing.allocator);
|
|
860
|
+
defer harness.destroy(std.testing.allocator);
|
|
861
|
+
var app_state: ProbeApp = .{};
|
|
862
|
+
const app = app_state.app();
|
|
863
|
+
try harness.start(app);
|
|
864
|
+
_ = try harness.runtime.createView(.{
|
|
865
|
+
.window_id = 1,
|
|
866
|
+
.label = "canvas",
|
|
867
|
+
.kind = .gpu_surface,
|
|
868
|
+
.frame = geometry.RectF.init(0, 0, 240, 140),
|
|
869
|
+
});
|
|
870
|
+
|
|
871
|
+
var player: Mpv = .{ .pixels = solidFrame(.{ 9, 8, 7, 255 }) };
|
|
872
|
+
const producer: media.MediaSurfaceProducer = try harness.runtime.acquireMediaSurfaceProducer(surface_id);
|
|
873
|
+
Glue.onMpvFrame(&player, producer);
|
|
874
|
+
try dispatchFrame(harness, app, 1);
|
|
875
|
+
try std.testing.expect(harness.runtime.adoptedMediaSurfaceTexture(surface_id) != null);
|
|
876
|
+
try std.testing.expect(!player.stopped);
|
|
877
|
+
|
|
878
|
+
// A released handle drives the example's teardown arm.
|
|
879
|
+
producer.release();
|
|
880
|
+
Glue.onMpvFrame(&player, producer);
|
|
881
|
+
try std.testing.expect(player.stopped);
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
// -------------------------------------------------- lazy texture buffers
|
|
885
|
+
|
|
886
|
+
/// A started GPU harness whose runtime froze `runtime_allocator` at
|
|
887
|
+
/// init. The runtime's `owned_allocator` captures `Options.allocator`
|
|
888
|
+
/// in `initAt` and mutating `options.allocator` on a live runtime
|
|
889
|
+
/// deliberately retargets nothing, so a test allocator must be injected
|
|
890
|
+
/// through the real capture site: re-initialize the runtime in place
|
|
891
|
+
/// with the harness's own platform and trace wiring (nothing is
|
|
892
|
+
/// heap-owned yet, so the re-init leaks nothing).
|
|
893
|
+
fn startedGpuHarnessWithRuntimeAllocator(gpa: std.mem.Allocator, runtime_allocator: std.mem.Allocator) !*TestHarness() {
|
|
894
|
+
const harness = try startedGpuHarness(gpa);
|
|
895
|
+
errdefer harness.destroy(gpa);
|
|
896
|
+
core.Runtime.initAt(&harness.runtime, .{
|
|
897
|
+
.platform = harness.null_platform.platform(),
|
|
898
|
+
.trace_sink = harness.trace_sink.sink(),
|
|
899
|
+
.allocator = runtime_allocator,
|
|
900
|
+
.environ = std.testing.environ,
|
|
901
|
+
});
|
|
902
|
+
// Match TestHarness().init: tests fail loud on handler/update errors.
|
|
903
|
+
harness.runtime.dispatch_error_policy = .propagate;
|
|
904
|
+
return harness;
|
|
905
|
+
}
|
|
906
|
+
|
|
907
|
+
test "a fresh runtime allocates zero media-texture bytes until an adoption happens" {
|
|
908
|
+
// Count every runtime-allocator call: construction, startup, frames,
|
|
909
|
+
// and even a live CLAIM with staged pushes perform NONE — texture
|
|
910
|
+
// buffer storage is on-demand at first adoption. (The regression
|
|
911
|
+
// pinned here: an embedded pool at the frame budget put 4 x 8 MiB =
|
|
912
|
+
// 32 MiB in every Runtime — the docs wasm preview host carries one
|
|
913
|
+
// Runtime per component tile — before any producer existed; the
|
|
914
|
+
// registered-font-pool regression's twin.)
|
|
915
|
+
var counting = std.testing.FailingAllocator.init(std.testing.allocator, .{});
|
|
916
|
+
const harness = try startedGpuHarnessWithRuntimeAllocator(std.testing.allocator, counting.allocator());
|
|
917
|
+
defer harness.destroy(std.testing.allocator);
|
|
918
|
+
var app_state: ProbeApp = .{};
|
|
919
|
+
const app = app_state.app();
|
|
920
|
+
try harness.start(app);
|
|
921
|
+
_ = try harness.runtime.createView(.{
|
|
922
|
+
.window_id = 1,
|
|
923
|
+
.label = "canvas",
|
|
924
|
+
.kind = .gpu_surface,
|
|
925
|
+
.frame = geometry.RectF.init(0, 0, 240, 140),
|
|
926
|
+
});
|
|
927
|
+
const producer = try harness.runtime.acquireMediaSurfaceProducer(surface_id);
|
|
928
|
+
defer producer.release();
|
|
929
|
+
const red = solidFrame(.{ 255, 0, 0, 255 });
|
|
930
|
+
try producer.pushFrame(2, 2, &red);
|
|
931
|
+
try std.testing.expectEqual(@as(usize, 0), counting.allocations);
|
|
932
|
+
|
|
933
|
+
// Even an allocator that refuses everything leaves the channel
|
|
934
|
+
// operational: the adoption drops THIS frame loudly and the next
|
|
935
|
+
// one retries — never a torn entry, never a crash. The FROZEN
|
|
936
|
+
// allocator itself turns refusing (ownership never moves; only the
|
|
937
|
+
// backing behavior changes), because swapping options.allocator
|
|
938
|
+
// would retarget nothing.
|
|
939
|
+
counting.fail_index = 0;
|
|
940
|
+
try dispatchFrame(harness, app, 1);
|
|
941
|
+
try std.testing.expect(harness.runtime.adoptedMediaSurfaceTexture(surface_id) == null);
|
|
942
|
+
|
|
943
|
+
// The first adoption is the first allocation: exactly one, exactly
|
|
944
|
+
// the frame budget (freed by Runtime.deinit through
|
|
945
|
+
// harness.destroy — the leak-checked test allocator backs it).
|
|
946
|
+
counting.fail_index = std.math.maxInt(usize);
|
|
947
|
+
const green = solidFrame(.{ 0, 255, 0, 255 });
|
|
948
|
+
try producer.pushFrame(2, 2, &green);
|
|
949
|
+
try dispatchFrame(harness, app, 2);
|
|
950
|
+
try std.testing.expect(harness.runtime.adoptedMediaSurfaceTexture(surface_id) != null);
|
|
951
|
+
try std.testing.expectEqual(@as(usize, 1), counting.allocations);
|
|
952
|
+
try std.testing.expectEqual(canvas_limits.max_media_surface_pixel_bytes, counting.allocated_bytes);
|
|
953
|
+
|
|
954
|
+
// A second adoption of the SAME entry reuses the buffer: the count
|
|
955
|
+
// stays one — the allocation is per used channel, not per frame.
|
|
956
|
+
const blue = solidFrame(.{ 0, 0, 255, 255 });
|
|
957
|
+
try producer.pushFrame(2, 2, &blue);
|
|
958
|
+
try dispatchFrame(harness, app, 3);
|
|
959
|
+
try std.testing.expectEqual(@as(usize, 1), counting.allocations);
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
test "media buffer ownership freezes at init: a mutated options.allocator sees zero activity" {
|
|
963
|
+
// The hazard pinned here: `Runtime.options` is public and mutable,
|
|
964
|
+
// so if the lazy adoption allocation and deinit's free both read
|
|
965
|
+
// `options.allocator` LIVE, swapping it between adoption and deinit
|
|
966
|
+
// frees through the wrong allocator — silent UB. Ownership must
|
|
967
|
+
// freeze into `owned_allocator` at init instead.
|
|
968
|
+
var frozen = std.testing.FailingAllocator.init(std.testing.allocator, .{});
|
|
969
|
+
const harness = try startedGpuHarnessWithRuntimeAllocator(std.testing.allocator, frozen.allocator());
|
|
970
|
+
defer harness.destroy(std.testing.allocator);
|
|
971
|
+
var app_state: ProbeApp = .{};
|
|
972
|
+
const app = app_state.app();
|
|
973
|
+
try harness.start(app);
|
|
974
|
+
_ = try harness.runtime.createView(.{
|
|
975
|
+
.window_id = 1,
|
|
976
|
+
.label = "canvas",
|
|
977
|
+
.kind = .gpu_surface,
|
|
978
|
+
.frame = geometry.RectF.init(0, 0, 240, 140),
|
|
979
|
+
});
|
|
980
|
+
const producer = try harness.runtime.acquireMediaSurfaceProducer(surface_id);
|
|
981
|
+
defer producer.release();
|
|
982
|
+
const red = solidFrame(.{ 255, 0, 0, 255 });
|
|
983
|
+
try producer.pushFrame(2, 2, &red);
|
|
984
|
+
try dispatchFrame(harness, app, 1);
|
|
985
|
+
try std.testing.expect(harness.runtime.adoptedMediaSurfaceTexture(surface_id) != null);
|
|
986
|
+
try std.testing.expectEqual(@as(usize, 1), frozen.allocations);
|
|
987
|
+
|
|
988
|
+
// Sabotage: swap options.allocator AFTER the adoption allocated.
|
|
989
|
+
// fail_index = 0 poisons the swapped-in allocator (any allocation
|
|
990
|
+
// through it refuses), and its counters pin that deinit routes
|
|
991
|
+
// NOTHING here — neither an alloc nor, critically, the free.
|
|
992
|
+
var poisoned = std.testing.FailingAllocator.init(std.testing.allocator, .{ .fail_index = 0 });
|
|
993
|
+
harness.runtime.options.allocator = poisoned.allocator();
|
|
994
|
+
|
|
995
|
+
harness.runtime.deinit();
|
|
996
|
+
try std.testing.expectEqual(@as(usize, 1), frozen.deallocations);
|
|
997
|
+
try std.testing.expectEqual(canvas_limits.max_media_surface_pixel_bytes, frozen.freed_bytes);
|
|
998
|
+
try std.testing.expectEqual(@as(usize, 0), poisoned.allocations);
|
|
999
|
+
try std.testing.expectEqual(@as(usize, 0), poisoned.deallocations);
|
|
1000
|
+
try std.testing.expectEqual(@as(usize, 0), poisoned.freed_bytes);
|
|
1001
|
+
// harness.destroy's second deinit finds the buffers already
|
|
1002
|
+
// returned (deinit resets them to empty) — no double free through
|
|
1003
|
+
// either allocator.
|
|
1004
|
+
}
|
|
1005
|
+
|
|
1006
|
+
// ------------------------------------------------ dropped-frame retries
|
|
1007
|
+
|
|
1008
|
+
test "an adoption-OOM drop retries with byte-identical pixels on the next push" {
|
|
1009
|
+
// The static-frame shape: a paused video (or album art) producer
|
|
1010
|
+
// re-pushes the SAME bytes after its frame was dropped. The drop
|
|
1011
|
+
// must forget the push-boundary fingerprint, or the identical
|
|
1012
|
+
// retry dies in the dedup gate — no stage, no wake, the surface
|
|
1013
|
+
// stays blank until the pixels happen to change.
|
|
1014
|
+
var counting = std.testing.FailingAllocator.init(std.testing.allocator, .{});
|
|
1015
|
+
const harness = try startedGpuHarnessWithRuntimeAllocator(std.testing.allocator, counting.allocator());
|
|
1016
|
+
defer harness.destroy(std.testing.allocator);
|
|
1017
|
+
var app_state: ProbeApp = .{};
|
|
1018
|
+
const app = app_state.app();
|
|
1019
|
+
try harness.start(app);
|
|
1020
|
+
_ = try harness.runtime.createView(.{
|
|
1021
|
+
.window_id = 1,
|
|
1022
|
+
.label = "canvas",
|
|
1023
|
+
.kind = .gpu_surface,
|
|
1024
|
+
.frame = geometry.RectF.init(0, 0, 240, 140),
|
|
1025
|
+
});
|
|
1026
|
+
const producer = try harness.runtime.acquireMediaSurfaceProducer(surface_id);
|
|
1027
|
+
defer producer.release();
|
|
1028
|
+
|
|
1029
|
+
// The lazy texture-buffer allocation fails at first adoption: this
|
|
1030
|
+
// frame drops loudly, nothing is adopted.
|
|
1031
|
+
const still = solidFrame(.{ 21, 22, 23, 255 });
|
|
1032
|
+
try producer.pushFrame(2, 2, &still);
|
|
1033
|
+
counting.fail_index = 0;
|
|
1034
|
+
try dispatchFrame(harness, app, 1);
|
|
1035
|
+
try std.testing.expect(harness.runtime.adoptedMediaSurfaceTexture(surface_id) == null);
|
|
1036
|
+
|
|
1037
|
+
// Memory recovers and the producer re-pushes the SAME bytes: the
|
|
1038
|
+
// push must stage AND wake again (the drop reopened both gates),
|
|
1039
|
+
// and the next adoption lands the frame.
|
|
1040
|
+
counting.fail_index = std.math.maxInt(usize);
|
|
1041
|
+
const requests_before = harness.null_platform.pendingFrameRequestCount();
|
|
1042
|
+
try producer.pushFrame(2, 2, &still);
|
|
1043
|
+
try std.testing.expectEqual(requests_before + 1, harness.null_platform.pendingFrameRequestCount());
|
|
1044
|
+
try dispatchFrame(harness, app, 2);
|
|
1045
|
+
try std.testing.expect(harness.runtime.adoptedMediaSurfaceTexture(surface_id) != null);
|
|
1046
|
+
const resources = harness.runtime.registeredCanvasImages();
|
|
1047
|
+
try std.testing.expectEqual(@as(usize, 1), resources.len);
|
|
1048
|
+
try std.testing.expectEqualSlices(u8, &still, resources[0].pixels);
|
|
1049
|
+
}
|
|
1050
|
+
|
|
1051
|
+
test "a registry-full drop retries with byte-identical pixels on the next push" {
|
|
1052
|
+
const harness = try startedGpuHarness(std.testing.allocator);
|
|
1053
|
+
defer harness.destroy(std.testing.allocator);
|
|
1054
|
+
var app_state: ProbeApp = .{};
|
|
1055
|
+
const app = app_state.app();
|
|
1056
|
+
try harness.start(app);
|
|
1057
|
+
_ = try harness.runtime.createView(.{
|
|
1058
|
+
.window_id = 1,
|
|
1059
|
+
.label = "canvas",
|
|
1060
|
+
.kind = .gpu_surface,
|
|
1061
|
+
.frame = geometry.RectF.init(0, 0, 240, 140),
|
|
1062
|
+
});
|
|
1063
|
+
|
|
1064
|
+
// Fill the runtime's texture registry from claims that stay LIVE,
|
|
1065
|
+
// so no entry is reclaimable.
|
|
1066
|
+
var live: [canvas_limits.max_media_surface_channels]media_surface.MediaSurfaceProducer = undefined;
|
|
1067
|
+
for (&live, 0..) |*extra, index| {
|
|
1068
|
+
extra.* = try harness.runtime.acquireMediaSurfaceProducer(501 + index);
|
|
1069
|
+
const shade: u8 = @intCast(index + 1);
|
|
1070
|
+
const frame = solidFrame(.{ shade, 0, shade, 255 });
|
|
1071
|
+
try extra.pushFrame(2, 2, &frame);
|
|
1072
|
+
}
|
|
1073
|
+
defer for (live[1..]) |extra| extra.release();
|
|
1074
|
+
try dispatchFrame(harness, app, 1);
|
|
1075
|
+
try std.testing.expectEqual(canvas_limits.max_media_surface_channels, harness.runtime.media_surface_count);
|
|
1076
|
+
|
|
1077
|
+
// Free ONE slot for the new producer, then rename the released
|
|
1078
|
+
// surface's entry to a still-live surface id. That manufactures
|
|
1079
|
+
// the registry-full drop's shape, which no single-threaded API
|
|
1080
|
+
// sequence can reach (the entry budget equals the process-wide
|
|
1081
|
+
// slot budget, so a full table of actively claimed entries plus a
|
|
1082
|
+
// fifth staged surface requires a release racing the lock-free
|
|
1083
|
+
// ownership snapshot): every entry reads as actively claimed
|
|
1084
|
+
// while a staged frame has nowhere to land.
|
|
1085
|
+
live[0].release();
|
|
1086
|
+
var renamed_index: usize = 0;
|
|
1087
|
+
for (harness.runtime.media_surface_entries[0..harness.runtime.media_surface_count], 0..) |entry, index| {
|
|
1088
|
+
if (entry.surface_id == 501) renamed_index = index;
|
|
1089
|
+
}
|
|
1090
|
+
harness.runtime.media_surface_entries[renamed_index].surface_id = 502;
|
|
1091
|
+
|
|
1092
|
+
const producer = try harness.runtime.acquireMediaSurfaceProducer(999);
|
|
1093
|
+
defer producer.release();
|
|
1094
|
+
const still = solidFrame(.{ 77, 78, 79, 255 });
|
|
1095
|
+
try producer.pushFrame(2, 2, &still);
|
|
1096
|
+
try dispatchFrame(harness, app, 2);
|
|
1097
|
+
// Registry full of live surfaces: this frame dropped loudly.
|
|
1098
|
+
try std.testing.expect(harness.runtime.adoptedMediaSurfaceTexture(999) == null);
|
|
1099
|
+
|
|
1100
|
+
// The release becomes visible (the entry is reclaimable again) and
|
|
1101
|
+
// the producer re-pushes the SAME bytes: the drop must have
|
|
1102
|
+
// reopened the push-boundary gate, so the retry stages, wakes, and
|
|
1103
|
+
// the next adoption reclaims the entry and lands the frame.
|
|
1104
|
+
harness.runtime.media_surface_entries[renamed_index].surface_id = 501;
|
|
1105
|
+
const requests_before = harness.null_platform.pendingFrameRequestCount();
|
|
1106
|
+
try producer.pushFrame(2, 2, &still);
|
|
1107
|
+
try std.testing.expectEqual(requests_before + 1, harness.null_platform.pendingFrameRequestCount());
|
|
1108
|
+
try dispatchFrame(harness, app, 3);
|
|
1109
|
+
try std.testing.expect(harness.runtime.adoptedMediaSurfaceTexture(999) != null);
|
|
1110
|
+
const texture_id = canvas.mediaSurfaceTextureImageId(999);
|
|
1111
|
+
var landed = false;
|
|
1112
|
+
for (harness.runtime.registeredCanvasImages()) |resource| {
|
|
1113
|
+
if (resource.id != texture_id) continue;
|
|
1114
|
+
landed = true;
|
|
1115
|
+
try std.testing.expectEqualSlices(u8, &still, resource.pixels);
|
|
1116
|
+
}
|
|
1117
|
+
try std.testing.expect(landed);
|
|
1118
|
+
}
|
|
1119
|
+
|
|
1120
|
+
// ------------------------------------------------------- producer wakes
|
|
1121
|
+
|
|
1122
|
+
test "a push wakes an idle compositor: one coalesced frame request that adopts on dispatch" {
|
|
1123
|
+
const harness = try startedGpuHarness(std.testing.allocator);
|
|
1124
|
+
defer harness.destroy(std.testing.allocator);
|
|
1125
|
+
var app_state: ProbeApp = .{};
|
|
1126
|
+
const app = app_state.app();
|
|
1127
|
+
try harness.start(app);
|
|
1128
|
+
_ = try harness.runtime.createView(.{
|
|
1129
|
+
.window_id = 1,
|
|
1130
|
+
.label = "canvas",
|
|
1131
|
+
.kind = .gpu_surface,
|
|
1132
|
+
.frame = geometry.RectF.init(0, 0, 240, 140),
|
|
1133
|
+
});
|
|
1134
|
+
|
|
1135
|
+
const producer = try harness.runtime.acquireMediaSurfaceProducer(surface_id);
|
|
1136
|
+
defer producer.release();
|
|
1137
|
+
|
|
1138
|
+
// The idle shape: NO frame is scheduled (the demand-driven host is
|
|
1139
|
+
// asleep), and a producer pushes. The push itself must request one
|
|
1140
|
+
// cross-thread frame — the same `request_frame_fn` channel the
|
|
1141
|
+
// automation arrival watcher wakes the loop through.
|
|
1142
|
+
try std.testing.expectEqual(@as(usize, 0), harness.null_platform.pendingFrameRequestCount());
|
|
1143
|
+
const red = solidFrame(.{ 255, 0, 0, 255 });
|
|
1144
|
+
try producer.pushFrame(2, 2, &red);
|
|
1145
|
+
try std.testing.expectEqual(@as(usize, 1), harness.null_platform.pendingFrameRequestCount());
|
|
1146
|
+
|
|
1147
|
+
// Burst pushes coalesce: the wake already in flight carries them
|
|
1148
|
+
// (latest-wins), no second platform call.
|
|
1149
|
+
const green = solidFrame(.{ 0, 255, 0, 255 });
|
|
1150
|
+
const blue = solidFrame(.{ 0, 0, 255, 255 });
|
|
1151
|
+
try producer.pushFrame(2, 2, &green);
|
|
1152
|
+
try producer.pushFrame(2, 2, &blue);
|
|
1153
|
+
try std.testing.expectEqual(@as(usize, 1), harness.null_platform.pendingFrameRequestCount());
|
|
1154
|
+
|
|
1155
|
+
// The loop drains the request exactly like a live host marshals it:
|
|
1156
|
+
// one `.frame_requested` on the loop thread. That frame ADOPTS the
|
|
1157
|
+
// newest staged bytes (no gpu_surface_frame ever dispatched), and
|
|
1158
|
+
// adoption's invalidation arms the prompt gpu frame that will
|
|
1159
|
+
// composite them.
|
|
1160
|
+
const gpu_requests_before = harness.null_platform.gpu_surface_frame_request_count;
|
|
1161
|
+
const wake_event = harness.null_platform.takeFrameRequest().?;
|
|
1162
|
+
try harness.runtime.dispatchPlatformEvent(app, wake_event);
|
|
1163
|
+
const adopted = harness.runtime.adoptedMediaSurfaceTexture(surface_id).?;
|
|
1164
|
+
try std.testing.expect(adopted.fingerprint != 0);
|
|
1165
|
+
const resources = harness.runtime.registeredCanvasImages();
|
|
1166
|
+
try std.testing.expectEqual(@as(usize, 1), resources.len);
|
|
1167
|
+
try std.testing.expectEqualSlices(u8, &blue, resources[0].pixels);
|
|
1168
|
+
try std.testing.expect(harness.null_platform.gpu_surface_frame_request_count > gpu_requests_before);
|
|
1169
|
+
|
|
1170
|
+
// The adoption drained the coalescer: the NEXT changed push wakes
|
|
1171
|
+
// again — a paced producer gets one wake per adopted frame, never
|
|
1172
|
+
// fewer (the stall this channel exists to prevent) and never a
|
|
1173
|
+
// backlog of platform calls.
|
|
1174
|
+
try std.testing.expectEqual(@as(usize, 0), harness.null_platform.pendingFrameRequestCount());
|
|
1175
|
+
const white = solidFrame(.{ 255, 255, 255, 255 });
|
|
1176
|
+
try producer.pushFrame(2, 2, &white);
|
|
1177
|
+
try std.testing.expectEqual(@as(usize, 1), harness.null_platform.pendingFrameRequestCount());
|
|
1178
|
+
}
|
|
1179
|
+
|
|
1180
|
+
test "damage-skipped and refused pushes wake nothing" {
|
|
1181
|
+
const harness = try startedGpuHarness(std.testing.allocator);
|
|
1182
|
+
defer harness.destroy(std.testing.allocator);
|
|
1183
|
+
var app_state: ProbeApp = .{};
|
|
1184
|
+
const app = app_state.app();
|
|
1185
|
+
try harness.start(app);
|
|
1186
|
+
_ = try harness.runtime.createView(.{
|
|
1187
|
+
.window_id = 1,
|
|
1188
|
+
.label = "canvas",
|
|
1189
|
+
.kind = .gpu_surface,
|
|
1190
|
+
.frame = geometry.RectF.init(0, 0, 240, 140),
|
|
1191
|
+
});
|
|
1192
|
+
|
|
1193
|
+
const producer = try harness.runtime.acquireMediaSurfaceProducer(surface_id);
|
|
1194
|
+
|
|
1195
|
+
// Adopt one frame through the wake path so the channel is warm.
|
|
1196
|
+
const gray = solidFrame(.{ 40, 40, 40, 255 });
|
|
1197
|
+
try producer.pushFrame(2, 2, &gray);
|
|
1198
|
+
while (harness.null_platform.takeFrameRequest()) |event| {
|
|
1199
|
+
try harness.runtime.dispatchPlatformEvent(app, event);
|
|
1200
|
+
}
|
|
1201
|
+
try std.testing.expect(harness.runtime.adoptedMediaSurfaceTexture(surface_id) != null);
|
|
1202
|
+
|
|
1203
|
+
// Identical bytes: the push-boundary damage gate fires BEFORE the
|
|
1204
|
+
// wake — unchanged video never stirs an idle scheduler.
|
|
1205
|
+
try producer.pushFrame(2, 2, &gray);
|
|
1206
|
+
try std.testing.expectEqual(@as(usize, 0), harness.null_platform.pendingFrameRequestCount());
|
|
1207
|
+
|
|
1208
|
+
// A released handle's push is refused before staging AND wakes
|
|
1209
|
+
// nothing — a stale producer cannot burn the loop awake.
|
|
1210
|
+
producer.release();
|
|
1211
|
+
const after = solidFrame(.{ 9, 9, 9, 255 });
|
|
1212
|
+
try std.testing.expectError(error.MediaSurfaceReleased, producer.pushFrame(2, 2, &after));
|
|
1213
|
+
try std.testing.expectEqual(@as(usize, 0), harness.null_platform.pendingFrameRequestCount());
|
|
1214
|
+
|
|
1215
|
+
// An invalid frame is refused at validation, long before the wake.
|
|
1216
|
+
const reclaimed = try harness.runtime.acquireMediaSurfaceProducer(surface_id);
|
|
1217
|
+
defer reclaimed.release();
|
|
1218
|
+
const pixel = [_]u8{ 1, 2, 3, 255 };
|
|
1219
|
+
try std.testing.expectError(error.InvalidFrameDimensions, reclaimed.pushFrame(2, 1, &pixel));
|
|
1220
|
+
try std.testing.expectEqual(@as(usize, 0), harness.null_platform.pendingFrameRequestCount());
|
|
1221
|
+
}
|
|
1222
|
+
|
|
1223
|
+
test "disarmed wake bindings never touch the platform again; pushes stay functional" {
|
|
1224
|
+
const harness = try startedGpuHarness(std.testing.allocator);
|
|
1225
|
+
defer harness.destroy(std.testing.allocator);
|
|
1226
|
+
var app_state: ProbeApp = .{};
|
|
1227
|
+
const app = app_state.app();
|
|
1228
|
+
try harness.start(app);
|
|
1229
|
+
_ = try harness.runtime.createView(.{
|
|
1230
|
+
.window_id = 1,
|
|
1231
|
+
.label = "canvas",
|
|
1232
|
+
.kind = .gpu_surface,
|
|
1233
|
+
.frame = geometry.RectF.init(0, 0, 240, 140),
|
|
1234
|
+
});
|
|
1235
|
+
|
|
1236
|
+
const producer = try harness.runtime.acquireMediaSurfaceProducer(surface_id);
|
|
1237
|
+
defer producer.release();
|
|
1238
|
+
|
|
1239
|
+
// Armed: a fresh push wakes.
|
|
1240
|
+
const red = solidFrame(.{ 255, 0, 0, 255 });
|
|
1241
|
+
try producer.pushFrame(2, 2, &red);
|
|
1242
|
+
try std.testing.expectEqual(@as(usize, 1), harness.null_platform.pendingFrameRequestCount());
|
|
1243
|
+
while (harness.null_platform.takeFrameRequest()) |event| {
|
|
1244
|
+
try harness.runtime.dispatchPlatformEvent(app, event);
|
|
1245
|
+
}
|
|
1246
|
+
|
|
1247
|
+
// Disarmed (what the run loop's exit defer, TestHarness.destroy,
|
|
1248
|
+
// and the embed host's destroy invoke before the platform dies):
|
|
1249
|
+
// pushes still stage — and the compositor's own frame clock still
|
|
1250
|
+
// adopts them — but NOTHING calls into the platform anymore. This
|
|
1251
|
+
// is the whole teardown-safety contract: after disarm returns, a
|
|
1252
|
+
// producer thread cannot reach the host, so the host may die.
|
|
1253
|
+
harness.runtime.disarmMediaSurfaceWakes();
|
|
1254
|
+
const green = solidFrame(.{ 0, 255, 0, 255 });
|
|
1255
|
+
try producer.pushFrame(2, 2, &green);
|
|
1256
|
+
try std.testing.expectEqual(@as(usize, 0), harness.null_platform.pendingFrameRequestCount());
|
|
1257
|
+
try dispatchFrame(harness, app, 2);
|
|
1258
|
+
const resources = harness.runtime.registeredCanvasImages();
|
|
1259
|
+
try std.testing.expectEqual(@as(usize, 1), resources.len);
|
|
1260
|
+
try std.testing.expectEqualSlices(u8, &green, resources[0].pixels);
|
|
1261
|
+
|
|
1262
|
+
// Re-acquiring re-arms: the disarm ends BINDINGS, not the channel.
|
|
1263
|
+
producer.release();
|
|
1264
|
+
const rearmed = try harness.runtime.acquireMediaSurfaceProducer(surface_id);
|
|
1265
|
+
defer rearmed.release();
|
|
1266
|
+
const blue = solidFrame(.{ 0, 0, 255, 255 });
|
|
1267
|
+
try rearmed.pushFrame(2, 2, &blue);
|
|
1268
|
+
try std.testing.expectEqual(@as(usize, 1), harness.null_platform.pendingFrameRequestCount());
|
|
1269
|
+
while (harness.null_platform.takeFrameRequest()) |event| {
|
|
1270
|
+
try harness.runtime.dispatchPlatformEvent(app, event);
|
|
1271
|
+
}
|
|
1272
|
+
}
|
|
1273
|
+
|
|
1274
|
+
// -------------------------------------------------- threads and teardown
|
|
1275
|
+
|
|
1276
|
+
test "cross-thread pushes stage safely and the loop adopts the newest" {
|
|
1277
|
+
const harness = try startedGpuHarness(std.testing.allocator);
|
|
1278
|
+
defer harness.destroy(std.testing.allocator);
|
|
1279
|
+
var app_state: ProbeApp = .{};
|
|
1280
|
+
const app = app_state.app();
|
|
1281
|
+
try harness.start(app);
|
|
1282
|
+
_ = try harness.runtime.createView(.{
|
|
1283
|
+
.window_id = 1,
|
|
1284
|
+
.label = "canvas",
|
|
1285
|
+
.kind = .gpu_surface,
|
|
1286
|
+
.frame = geometry.RectF.init(0, 0, 240, 140),
|
|
1287
|
+
});
|
|
1288
|
+
|
|
1289
|
+
const producer = try harness.runtime.acquireMediaSurfaceProducer(surface_id);
|
|
1290
|
+
defer producer.release();
|
|
1291
|
+
|
|
1292
|
+
// A real producer thread pushes a burst of distinct frames; the
|
|
1293
|
+
// loop thread adopts on its own frame clock. Joining first makes
|
|
1294
|
+
// the assertion deterministic: latest-wins leaves exactly the final
|
|
1295
|
+
// frame staged.
|
|
1296
|
+
const Worker = struct {
|
|
1297
|
+
fn run(handle: media_surface.MediaSurfaceProducer) void {
|
|
1298
|
+
var shade: u8 = 1;
|
|
1299
|
+
while (shade <= 50) : (shade += 1) {
|
|
1300
|
+
const frame = solidFrame(.{ shade, 0, shade, 255 });
|
|
1301
|
+
handle.pushFrame(2, 2, &frame) catch return;
|
|
1302
|
+
}
|
|
1303
|
+
}
|
|
1304
|
+
};
|
|
1305
|
+
const thread = try std.Thread.spawn(.{}, Worker.run, .{producer});
|
|
1306
|
+
thread.join();
|
|
1307
|
+
|
|
1308
|
+
// Fifty distinct cross-thread pushes with no adoption in between:
|
|
1309
|
+
// the wake coalescer latched after the first, so the idle loop was
|
|
1310
|
+
// asked for exactly ONE frame.
|
|
1311
|
+
try std.testing.expectEqual(@as(usize, 1), harness.null_platform.pendingFrameRequestCount());
|
|
1312
|
+
|
|
1313
|
+
try dispatchFrame(harness, app, 1);
|
|
1314
|
+
const final = solidFrame(.{ 50, 0, 50, 255 });
|
|
1315
|
+
const resources = harness.runtime.registeredCanvasImages();
|
|
1316
|
+
try std.testing.expectEqual(@as(usize, 1), resources.len);
|
|
1317
|
+
try std.testing.expectEqualSlices(u8, &final, resources[0].pixels);
|
|
1318
|
+
}
|
|
1319
|
+
|
|
1320
|
+
test "two runtimes with full retained tables adopt concurrently without deadlock" {
|
|
1321
|
+
// The retained-entry reclaim scan reads slot ownership LOCK-FREE
|
|
1322
|
+
// (media_surface.mediaSurfaceHasActiveSlot); before that, it locked
|
|
1323
|
+
// OTHER mailbox slots while the adoption loop held the drained one:
|
|
1324
|
+
// self-deadlock the moment one runtime's table filled (the spin
|
|
1325
|
+
// mutex is not reentrant), ABBA between two runtimes draining
|
|
1326
|
+
// different slots and scanning each other's. This stress drives
|
|
1327
|
+
// exactly that shape — two runtimes, both texture tables FULL, both
|
|
1328
|
+
// reclaiming on every adoption, concurrently on real threads — for
|
|
1329
|
+
// a bounded iteration count. Completion is the deadlock assertion;
|
|
1330
|
+
// the SpinMutex debug lock-discipline assertion (no thread ever
|
|
1331
|
+
// holds two media-surface mutexes) turns any reintroduced nesting
|
|
1332
|
+
// into an instant loud failure in ANY debug test run, so the fix
|
|
1333
|
+
// cannot regress silently between stress runs.
|
|
1334
|
+
const gpa = std.testing.allocator;
|
|
1335
|
+
|
|
1336
|
+
var harnesses: [2]*TestHarness() = undefined;
|
|
1337
|
+
harnesses[0] = try TestHarness().create(gpa, .{ .size = geometry.SizeF.init(64, 64) });
|
|
1338
|
+
defer harnesses[0].destroy(gpa);
|
|
1339
|
+
harnesses[1] = try TestHarness().create(gpa, .{ .size = geometry.SizeF.init(64, 64) });
|
|
1340
|
+
defer harnesses[1].destroy(gpa);
|
|
1341
|
+
|
|
1342
|
+
// Fill each runtime's texture table with RETAINED entries (claim ->
|
|
1343
|
+
// push -> adopt -> release, rotating ids), so every raced adoption
|
|
1344
|
+
// below finds the table full and runs the reclaim scan while it
|
|
1345
|
+
// holds the drained slot's mutex.
|
|
1346
|
+
for (harnesses, 0..) |harness, harness_index| {
|
|
1347
|
+
var rotation: usize = 0;
|
|
1348
|
+
while (rotation < canvas_limits.max_media_surface_channels) : (rotation += 1) {
|
|
1349
|
+
const seed_id: u64 = 10_000 * (@as(u64, harness_index) + 1) + rotation;
|
|
1350
|
+
const producer = try harness.runtime.acquireMediaSurfaceProducer(seed_id);
|
|
1351
|
+
const shade: u8 = @intCast(rotation + 1);
|
|
1352
|
+
const frame = solidFrame(.{ shade, shade, shade, 255 });
|
|
1353
|
+
try producer.pushFrame(2, 2, &frame);
|
|
1354
|
+
harness.runtime.adoptMediaSurfaceFrames();
|
|
1355
|
+
producer.release();
|
|
1356
|
+
}
|
|
1357
|
+
try std.testing.expectEqual(canvas_limits.max_media_surface_channels, harness.runtime.media_surface_count);
|
|
1358
|
+
}
|
|
1359
|
+
|
|
1360
|
+
const iterations: usize = 200;
|
|
1361
|
+
const Driver = struct {
|
|
1362
|
+
fn drive(harness: *TestHarness(), id_base: u64) !void {
|
|
1363
|
+
var iteration: usize = 0;
|
|
1364
|
+
while (iteration < iterations) : (iteration += 1) {
|
|
1365
|
+
// Each thread is its own runtime's only driver, so the
|
|
1366
|
+
// loop-thread-only calls (acquire, adopt) are honest;
|
|
1367
|
+
// the SLOTS are process-wide shared state, which is the
|
|
1368
|
+
// contention under test. Two threads hold at most one
|
|
1369
|
+
// claim each — four slots never exhaust.
|
|
1370
|
+
const rotating_id = id_base + iteration;
|
|
1371
|
+
const producer = try harness.runtime.acquireMediaSurfaceProducer(rotating_id);
|
|
1372
|
+
defer producer.release();
|
|
1373
|
+
const shade: u8 = @truncate(iteration +% 1);
|
|
1374
|
+
const frame = solidFrame(.{ shade, @truncate(id_base), shade, 255 });
|
|
1375
|
+
try producer.pushFrame(2, 2, &frame);
|
|
1376
|
+
// Table full + fresh surface id = reclaim scan while
|
|
1377
|
+
// the drained slot's data mutex is held, every time.
|
|
1378
|
+
harness.runtime.adoptMediaSurfaceFrames();
|
|
1379
|
+
if (harness.runtime.adoptedMediaSurfaceTexture(rotating_id) == null) return error.AdoptionLost;
|
|
1380
|
+
}
|
|
1381
|
+
}
|
|
1382
|
+
|
|
1383
|
+
fn run(harness: *TestHarness(), id_base: u64, failed: *std.atomic.Value(bool)) void {
|
|
1384
|
+
drive(harness, id_base) catch failed.store(true, .release);
|
|
1385
|
+
}
|
|
1386
|
+
};
|
|
1387
|
+
|
|
1388
|
+
var failed = std.atomic.Value(bool).init(false);
|
|
1389
|
+
const first = try std.Thread.spawn(.{}, Driver.run, .{ harnesses[0], @as(u64, 20_000), &failed });
|
|
1390
|
+
const second = try std.Thread.spawn(.{}, Driver.run, .{ harnesses[1], @as(u64, 30_000), &failed });
|
|
1391
|
+
first.join();
|
|
1392
|
+
second.join();
|
|
1393
|
+
try std.testing.expect(!failed.load(.acquire));
|
|
1394
|
+
}
|
|
1395
|
+
|
|
1396
|
+
test "a producer outliving its runtime pushes into inert process-lived memory" {
|
|
1397
|
+
const gpa = std.testing.allocator;
|
|
1398
|
+
|
|
1399
|
+
// First life: a runtime claims the channel and adopts one frame.
|
|
1400
|
+
var orphan: media_surface.MediaSurfaceProducer = undefined;
|
|
1401
|
+
{
|
|
1402
|
+
const harness = try startedGpuHarness(gpa);
|
|
1403
|
+
defer harness.destroy(gpa);
|
|
1404
|
+
var app_state: ProbeApp = .{};
|
|
1405
|
+
const app = app_state.app();
|
|
1406
|
+
try harness.start(app);
|
|
1407
|
+
_ = try harness.runtime.createView(.{
|
|
1408
|
+
.window_id = 1,
|
|
1409
|
+
.label = "canvas",
|
|
1410
|
+
.kind = .gpu_surface,
|
|
1411
|
+
.frame = geometry.RectF.init(0, 0, 240, 140),
|
|
1412
|
+
});
|
|
1413
|
+
orphan = try harness.runtime.acquireMediaSurfaceProducer(surface_id);
|
|
1414
|
+
const first = solidFrame(.{ 1, 2, 3, 255 });
|
|
1415
|
+
try orphan.pushFrame(2, 2, &first);
|
|
1416
|
+
try dispatchFrame(harness, app, 1);
|
|
1417
|
+
try std.testing.expect(harness.runtime.adoptedMediaSurfaceTexture(surface_id) != null);
|
|
1418
|
+
// Leave a WAKE PENDING at teardown: the staged-but-never-adopted
|
|
1419
|
+
// push below arms the coalescer, and the harness dies without
|
|
1420
|
+
// ever answering it — teardown with a pending wake must be safe.
|
|
1421
|
+
const requests_before_parting = harness.null_platform.pendingFrameRequestCount();
|
|
1422
|
+
const parting = solidFrame(.{ 4, 5, 6, 255 });
|
|
1423
|
+
try orphan.pushFrame(2, 2, &parting);
|
|
1424
|
+
try std.testing.expectEqual(requests_before_parting + 1, harness.null_platform.pendingFrameRequestCount());
|
|
1425
|
+
// The harness (and the whole Runtime, and its PLATFORM) dies
|
|
1426
|
+
// here with the producer NOT released — the decoder-outlives-
|
|
1427
|
+
// the-view shape. destroy disarms the wake bindings first, so
|
|
1428
|
+
// the orphan's later pushes can never call into the freed host.
|
|
1429
|
+
}
|
|
1430
|
+
|
|
1431
|
+
// The orphan handle keeps pushing after its runtime is gone: the
|
|
1432
|
+
// pushes land in the process-lived mailbox slot (never freed, never
|
|
1433
|
+
// adopted again) and its wake binding is DISARMED — no use-after-
|
|
1434
|
+
// free by construction on either half.
|
|
1435
|
+
const after_teardown = solidFrame(.{ 200, 100, 50, 255 });
|
|
1436
|
+
try orphan.pushFrame(2, 2, &after_teardown);
|
|
1437
|
+
|
|
1438
|
+
// Second life: a NEW runtime acquires the SAME surface id. It gets
|
|
1439
|
+
// its own claim; the orphan's staged frame never crosses over.
|
|
1440
|
+
{
|
|
1441
|
+
const harness = try startedGpuHarness(gpa);
|
|
1442
|
+
defer harness.destroy(gpa);
|
|
1443
|
+
var app_state: ProbeApp = .{};
|
|
1444
|
+
const app = app_state.app();
|
|
1445
|
+
try harness.start(app);
|
|
1446
|
+
_ = try harness.runtime.createView(.{
|
|
1447
|
+
.window_id = 1,
|
|
1448
|
+
.label = "canvas",
|
|
1449
|
+
.kind = .gpu_surface,
|
|
1450
|
+
.frame = geometry.RectF.init(0, 0, 240, 140),
|
|
1451
|
+
});
|
|
1452
|
+
const producer = try harness.runtime.acquireMediaSurfaceProducer(surface_id);
|
|
1453
|
+
defer producer.release();
|
|
1454
|
+
|
|
1455
|
+
// Before the new producer pushes: nothing to adopt (the orphan's
|
|
1456
|
+
// frame belongs to the dead runtime's claim).
|
|
1457
|
+
try dispatchFrame(harness, app, 1);
|
|
1458
|
+
try std.testing.expect(harness.runtime.adoptedMediaSurfaceTexture(surface_id) == null);
|
|
1459
|
+
|
|
1460
|
+
const fresh = solidFrame(.{ 7, 7, 7, 255 });
|
|
1461
|
+
try producer.pushFrame(2, 2, &fresh);
|
|
1462
|
+
try dispatchFrame(harness, app, 2);
|
|
1463
|
+
const resources = harness.runtime.registeredCanvasImages();
|
|
1464
|
+
try std.testing.expectEqual(@as(usize, 1), resources.len);
|
|
1465
|
+
try std.testing.expectEqualSlices(u8, &fresh, resources[0].pixels);
|
|
1466
|
+
|
|
1467
|
+
// The orphan still pushes into its own (dead) claim harmlessly
|
|
1468
|
+
// while the new channel is live.
|
|
1469
|
+
try orphan.pushFrame(2, 2, &after_teardown);
|
|
1470
|
+
try dispatchFrame(harness, app, 3);
|
|
1471
|
+
try std.testing.expectEqualSlices(u8, &fresh, harness.runtime.registeredCanvasImages()[0].pixels);
|
|
1472
|
+
}
|
|
1473
|
+
|
|
1474
|
+
// Free the orphan's slot for later tests in this binary.
|
|
1475
|
+
orphan.release();
|
|
1476
|
+
}
|