@native-sdk/cli 0.5.2 → 0.5.4
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 +41 -1
- package/build.zig +264 -3
- package/native-sdk.d.ts +17 -1
- package/package.json +9 -9
- package/packages/core/package-lock.json +2 -2
- package/packages/core/package.json +1 -1
- package/packages/core/rt/rt.zig +138 -3
- package/packages/core/sdk/core.ts +225 -3
- package/packages/core/sdk/events.ts +39 -2
- package/packages/core/src/checker.ts +1 -1
- package/packages/core/src/emitter.ts +2625 -262
- 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 +4 -2
- package/skill-data/ts-core/SKILL.md +9 -1
- package/src/app_runner/root.zig +106 -1
- package/src/app_runner/ts_core_main.zig +4 -0
- package/src/automation/protocol.zig +20 -1
- package/src/embed/host.zig +27 -0
- package/src/embed/tests.zig +187 -0
- package/src/embed/ui_host.zig +4 -0
- package/src/platform/linux/gtk_host.c +112 -3
- package/src/platform/linux/root.zig +71 -0
- package/src/platform/macos/appkit_host.h +59 -2
- package/src/platform/macos/appkit_host.m +663 -34
- package/src/platform/macos/cef_host.mm +262 -10
- package/src/platform/macos/root.zig +126 -4
- package/src/platform/null_platform.zig +298 -5
- package/src/platform/null_platform_tests.zig +39 -0
- package/src/platform/root.zig +36 -0
- package/src/platform/types.zig +247 -8
- package/src/platform/windows/root.zig +134 -0
- package/src/platform/windows/webview2_host.cpp +152 -40
- package/src/primitives/app_manifest/comptime_scene.zig +5 -0
- package/src/primitives/app_manifest/root.zig +1 -0
- package/src/primitives/app_manifest/types.zig +32 -0
- 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 +25 -0
- package/src/primitives/canvas/tests.zig +1 -0
- package/src/primitives/canvas/ui.zig +46 -9
- package/src/primitives/canvas/ui_markup.zig +82 -34
- package/src/primitives/canvas/ui_markup_binary.zig +4 -1
- package/src/primitives/canvas/ui_markup_binary_tests.zig +25 -0
- package/src/primitives/canvas/ui_markup_compiled.zig +63 -11
- package/src/primitives/canvas/ui_markup_compiled_tests.zig +88 -0
- package/src/primitives/canvas/ui_markup_contract.zig +10 -1
- package/src/primitives/canvas/ui_markup_contract_tests.zig +50 -0
- package/src/primitives/canvas/ui_markup_huge_document_tests.zig +154 -0
- package/src/primitives/canvas/ui_markup_tests.zig +55 -9
- package/src/primitives/canvas/ui_markup_view.zig +81 -14
- package/src/primitives/canvas/ui_markup_view_tests.zig +219 -7
- package/src/primitives/canvas/ui_schema.zig +29 -0
- 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 +129 -0
- package/src/primitives/canvas/widget_layout.zig +9 -4
- 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/widgets.zig +16 -0
- package/src/root.zig +12 -0
- package/src/runtime/api.zig +15 -0
- package/src/runtime/automation_commands.zig +33 -0
- package/src/runtime/automation_widget_dispatch.zig +64 -0
- package/src/runtime/bridge_payload.zig +7 -0
- package/src/runtime/bridge_responses.zig +5 -0
- package/src/runtime/builtin_bridge.zig +9 -4
- package/src/runtime/canvas_font_tests.zig +711 -5
- package/src/runtime/canvas_fonts.zig +120 -25
- package/src/runtime/canvas_frame_helpers.zig +15 -0
- package/src/runtime/canvas_image_tests.zig +221 -0
- package/src/runtime/canvas_images.zig +78 -19
- package/src/runtime/canvas_limits.zig +44 -8
- package/src/runtime/canvas_widget_clipboard_tests.zig +123 -0
- package/src/runtime/canvas_widget_context_menu_tests.zig +55 -0
- package/src/runtime/canvas_widget_events.zig +24 -4
- package/src/runtime/canvas_widget_runtime.zig +24 -7
- package/src/runtime/canvas_widget_state.zig +6 -1
- package/src/runtime/canvas_widget_text_tests.zig +277 -0
- package/src/runtime/core.zig +148 -9
- package/src/runtime/effects.zig +1664 -43
- package/src/runtime/effects_fetch_tests.zig +89 -1
- package/src/runtime/effects_file_tests.zig +41 -1
- package/src/runtime/effects_image_tests.zig +1304 -0
- package/src/runtime/effects_tests.zig +153 -0
- package/src/runtime/flow.zig +44 -0
- package/src/runtime/gpu_surface_events.zig +7 -0
- package/src/runtime/media_surface.zig +726 -0
- package/src/runtime/media_surface_tests.zig +1476 -0
- package/src/runtime/root.zig +20 -0
- package/src/runtime/session_blobs.zig +689 -0
- package/src/runtime/session_journal.zig +111 -20
- package/src/runtime/session_record.zig +27 -2
- package/src/runtime/session_replay.zig +168 -16
- package/src/runtime/session_tests.zig +2118 -4
- package/src/runtime/shell_layout.zig +7 -0
- package/src/runtime/testdata/fonts/NotoSansSC-Receipt.ttf +0 -0
- package/src/runtime/testdata/fonts/OFL.txt +93 -0
- package/src/runtime/tests.zig +3 -0
- package/src/runtime/ts_core_host.zig +439 -4
- package/src/runtime/ts_core_host_tests.zig +41 -0
- package/src/runtime/ts_ui_app.zig +63 -0
- package/src/runtime/ui_app.zig +169 -6
- package/src/runtime/ui_app_tests.zig +334 -0
- package/src/runtime/ui_app_window_tests.zig +468 -0
- package/src/runtime/window_command_bridge_tests.zig +117 -0
- package/src/runtime/window_storage.zig +32 -0
- package/src/runtime/window_views.zig +61 -1
- package/src/tooling/manifest.zig +75 -0
- package/src/tooling/raw_manifest.zig +2 -0
- package/src/tooling/templates.zig +219 -1
|
@@ -0,0 +1,726 @@
|
|
|
1
|
+
//! The media-surface texture channel: the dynamic-texture primitive
|
|
2
|
+
//! under video playback, camera preview, and external renderers (mpv).
|
|
3
|
+
//!
|
|
4
|
+
//! A `media_surface` widget binds a model-owned u64 SURFACE id (markup
|
|
5
|
+
//! `<media-surface surface={id}>`, builder `ui.mediaSurface(.{ .image =
|
|
6
|
+
//! id })`). A producer — typically living on its own thread: a decoder,
|
|
7
|
+
//! a capture pipeline, an engine's render loop — acquires a handle for
|
|
8
|
+
//! that id (`Runtime.acquireMediaSurfaceProducer`) and pushes
|
|
9
|
+
//! tightly-packed straight-alpha RGBA8 frames. Pushes are LATEST-WINS:
|
|
10
|
+
//! an unpresented staged frame is replaced, never queued, so a fast
|
|
11
|
+
//! producer can never build a backlog. The compositor's presented-frame
|
|
12
|
+
//! clock paces consumption: staged frames are adopted at frame dispatch
|
|
13
|
+
//! (`adoptMediaSurfaceFrames`), and adoption is damage-tracked — a
|
|
14
|
+
//! frame whose content fingerprint matches the adopted texture costs no
|
|
15
|
+
//! copy and no invalidation, and repeat pushes of identical bytes
|
|
16
|
+
//! short-circuit at the push boundary too.
|
|
17
|
+
//!
|
|
18
|
+
//! THREAD CONTRACT. `acquireMediaSurfaceProducer` is loop-thread-only
|
|
19
|
+
//! (it touches the runtime); `MediaSurfaceProducer.pushFrame` and
|
|
20
|
+
//! `.release` are callable from ANY thread. The cross-thread half is a
|
|
21
|
+
//! mailbox of process-lifetime slots (the effects executor's storage
|
|
22
|
+
//! doctrine): everything a producer thread can ever reach — the slot
|
|
23
|
+
//! struct and its staging buffer — is allocated from the process-lived
|
|
24
|
+
//! page allocator at first claim and NEVER freed, holds no pointer to
|
|
25
|
+
//! the runtime or the platform, and is fenced by a per-slot spin mutex
|
|
26
|
+
//! plus owner-tag/generation stamps. A producer that outlives its view,
|
|
27
|
+
//! its runtime, or the whole app therefore cannot use-after-free by
|
|
28
|
+
//! construction: a push after teardown lands in inert process-lived
|
|
29
|
+
//! memory that no loop ever adopts again.
|
|
30
|
+
//!
|
|
31
|
+
//! WAKE CONTRACT. Adoption rides the compositor's frame clock (the
|
|
32
|
+
//! 60 Hz host timer, a prompt requested frame, a test-driven frame
|
|
33
|
+
//! event) — but native hosts are demand-driven one-shot schedulers, so
|
|
34
|
+
//! an IDLE app has no clock at all: a 24/30 fps video in an app nobody
|
|
35
|
+
//! touches would stall the moment the scheduler slept, and a producer
|
|
36
|
+
//! that starts after the last frame would never be adopted. A push that
|
|
37
|
+
//! stages NEW bytes therefore requests one frame itself, through the
|
|
38
|
+
//! platform's thread-safe cross-thread frame request (`PlatformServices
|
|
39
|
+
//! .request_frame_fn` — the automation arrival watcher's wake path;
|
|
40
|
+
//! requests coalesce at the platform AND at the slot's `pending` flag,
|
|
41
|
+
//! so a burst of pushes costs at most one). The binding lives in the
|
|
42
|
+
//! slot's `wake` half behind its OWN spin mutex, and the platform call
|
|
43
|
+
//! happens UNDER that mutex — the effects executor's abandon-fence
|
|
44
|
+
//! doctrine: the runtime disarms the binding at teardown under the same
|
|
45
|
+
//! mutex (`disarmMediaSurfaceWakes`, called from the run loop's exit
|
|
46
|
+
//! path and `Runtime.deinit`/TestHarness destroy), so after disarm
|
|
47
|
+
//! returns no producer is inside the call and none can start one — a
|
|
48
|
+
//! stale handle's push can never wake a dead host. Damage-skipped
|
|
49
|
+
//! pushes and pushes through a released/stale generation wake nothing.
|
|
50
|
+
//!
|
|
51
|
+
//! FORMAT AXIS. Slice 0 is tightly-packed RGBA8 only. The push call is
|
|
52
|
+
//! shaped so later formats (BGRA, planar YUV, zero-copy GPU handles)
|
|
53
|
+
//! can join as new entry points without disturbing this one.
|
|
54
|
+
//!
|
|
55
|
+
//! REPLAY POLICY. Texture CONTENTS are presentation chrome: adopted
|
|
56
|
+
//! textures enter the frame pipeline as `presentation_only` image
|
|
57
|
+
//! resources, so the deterministic reference renderer (goldens,
|
|
58
|
+
//! screenshots, replay pixel marks) never sees them — it renders the
|
|
59
|
+
//! surface's id-derived placeholder — and session fingerprints (hashed
|
|
60
|
+
//! over the a11y tree) never contained them to begin with. A recorded
|
|
61
|
+
//! session replays fingerprint-identical with NO producer attached.
|
|
62
|
+
|
|
63
|
+
const std = @import("std");
|
|
64
|
+
const builtin = @import("builtin");
|
|
65
|
+
const canvas = @import("canvas");
|
|
66
|
+
const canvas_limits = @import("canvas_limits.zig");
|
|
67
|
+
const runtime_canvas_images = @import("canvas_images.zig");
|
|
68
|
+
|
|
69
|
+
pub const max_media_surface_channels = canvas_limits.max_media_surface_channels;
|
|
70
|
+
pub const max_media_surface_pixel_bytes = canvas_limits.max_media_surface_pixel_bytes;
|
|
71
|
+
|
|
72
|
+
/// Process-lifetime allocator for everything a producer thread can
|
|
73
|
+
/// still reach after its runtime died: page_allocator is process-lived
|
|
74
|
+
/// by construction (no deinit), thread-safe, and available on every
|
|
75
|
+
/// target — the effects executor's storage doctrine exactly.
|
|
76
|
+
const process_allocator = std.heap.page_allocator;
|
|
77
|
+
|
|
78
|
+
/// Atomic accessors for the slot ownership triple the reclaim scan
|
|
79
|
+
/// reads WITHOUT a lock (`mediaSurfaceHasActiveSlot`): on threaded
|
|
80
|
+
/// targets a plain access racing an atomic one is a data race in the
|
|
81
|
+
/// Zig/LLVM memory model, so both sides go through these. On
|
|
82
|
+
/// single-threaded targets (the docs wasm preview's
|
|
83
|
+
/// wasm32-freestanding, which also lacks 64-bit atomics) there is no
|
|
84
|
+
/// second thread to race and the plain access is exact.
|
|
85
|
+
inline fn ownershipStore(comptime T: type, ptr: *T, value: T, comptime ordering: std.builtin.AtomicOrder) void {
|
|
86
|
+
if (builtin.single_threaded) {
|
|
87
|
+
ptr.* = value;
|
|
88
|
+
} else {
|
|
89
|
+
@atomicStore(T, ptr, value, ordering);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
inline fn ownershipLoad(comptime T: type, ptr: *const T, comptime ordering: std.builtin.AtomicOrder) T {
|
|
94
|
+
if (builtin.single_threaded) return ptr.*;
|
|
95
|
+
return @atomicLoad(T, ptr, ordering);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/// Debug-only lock-discipline sentinel: how many media-surface spin
|
|
99
|
+
/// mutexes (slot data or wake) THIS thread currently holds. The
|
|
100
|
+
/// invariant `lock` asserts — a thread never holds two — is what makes
|
|
101
|
+
/// every spin mutex in this module deadlock-free BY CONSTRUCTION: with
|
|
102
|
+
/// no nesting there is no lock order to violate, on one runtime or
|
|
103
|
+
/// across several adopting concurrently. The invariant died once
|
|
104
|
+
/// exactly here: the retained-entry reclaim scan locked OTHER slots
|
|
105
|
+
/// while the adoption loop held the drained one — self-deadlock on one
|
|
106
|
+
/// runtime (the spin mutex is not reentrant), ABBA between two runtimes
|
|
107
|
+
/// draining different slots and scanning each other's. The scan now
|
|
108
|
+
/// snapshots ownership lock-free (`mediaSurfaceHasActiveSlot`), and
|
|
109
|
+
/// this assertion turns any reintroduction of nested slot locking into
|
|
110
|
+
/// an immediate loud failure in every Debug test run instead of a
|
|
111
|
+
/// stress-timing deadlock.
|
|
112
|
+
threadlocal var debug_held_media_surface_mutexes: usize = 0;
|
|
113
|
+
|
|
114
|
+
/// Tiny spin lock over `std.atomic.Mutex` (0.16 has no blocking thread
|
|
115
|
+
/// mutex outside `Io`). Guarded sections are bounded: a memcpy of at
|
|
116
|
+
/// most one staged frame (data half) or one enqueue-only platform call
|
|
117
|
+
/// (wake half); a colliding push spins for one of those, never blocks
|
|
118
|
+
/// on I/O. In Debug builds, `lock` asserts the module's no-nesting
|
|
119
|
+
/// discipline (see `debug_held_media_surface_mutexes`).
|
|
120
|
+
const SpinMutex = struct {
|
|
121
|
+
inner: std.atomic.Mutex = .unlocked,
|
|
122
|
+
|
|
123
|
+
fn lock(self: *SpinMutex) void {
|
|
124
|
+
if (std.debug.runtime_safety) {
|
|
125
|
+
std.debug.assert(debug_held_media_surface_mutexes == 0);
|
|
126
|
+
}
|
|
127
|
+
while (!self.inner.tryLock()) std.atomic.spinLoopHint();
|
|
128
|
+
if (std.debug.runtime_safety) debug_held_media_surface_mutexes += 1;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
fn unlock(self: *SpinMutex) void {
|
|
132
|
+
if (std.debug.runtime_safety) debug_held_media_surface_mutexes -= 1;
|
|
133
|
+
self.inner.unlock();
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
/// The producer-to-compositor wake binding, one per slot, guarded by
|
|
138
|
+
/// its OWN spin mutex — never the slot's data mutex, whose guarded
|
|
139
|
+
/// sections must stay bounded memcpys (the thread contract): the wake
|
|
140
|
+
/// path calls into the platform, and holding the data mutex across
|
|
141
|
+
/// that call would make every colliding push spin behind a host call.
|
|
142
|
+
///
|
|
143
|
+
/// Ownership story (the effects executor's abandon-fence doctrine,
|
|
144
|
+
/// matched exactly): `request_frame_fn`/`context` point into the
|
|
145
|
+
/// claiming runtime's PLATFORM host — the same pair the automation
|
|
146
|
+
/// arrival watcher carries to its own thread — and the platform call
|
|
147
|
+
/// happens UNDER `mutex`. Teardown disarms under the same mutex before
|
|
148
|
+
/// the host dies (`disarmMediaSurfaceWakes`, from the run loop's exit
|
|
149
|
+
/// defer, `TestHarness.destroy`, and the embed host's destroy), so
|
|
150
|
+
/// after disarm returns no producer thread is inside the call and none
|
|
151
|
+
/// can start one. The implementations behind `request_frame_fn` are
|
|
152
|
+
/// enqueue-only (macOS: main-queue dispatch, GTK: `g_idle_add`, Win32:
|
|
153
|
+
/// `PostMessage`, null platform: an atomic counter), so the section
|
|
154
|
+
/// stays bounded — a colliding push spins for one enqueue, never I/O.
|
|
155
|
+
const MediaSurfaceWake = struct {
|
|
156
|
+
mutex: SpinMutex = .{},
|
|
157
|
+
/// The claim generation this binding serves (see the slot's
|
|
158
|
+
/// `generation`): a push wakes only when its handle's generation
|
|
159
|
+
/// matches, so a stale producer of a released — or re-claimed —
|
|
160
|
+
/// slot can never wake the slot's new owner spuriously.
|
|
161
|
+
generation: u64 = 0,
|
|
162
|
+
/// The arming runtime's process-unique tag, for the teardown
|
|
163
|
+
/// disarm sweep (`disarmMediaSurfaceWakes` disarms exactly the
|
|
164
|
+
/// bindings its runtime armed).
|
|
165
|
+
owner_tag: u64 = 0,
|
|
166
|
+
/// The platform's thread-safe cross-thread frame request, or null
|
|
167
|
+
/// when the host has none (then pacing stays purely demand-driven,
|
|
168
|
+
/// the pre-wake behavior) or the binding is disarmed.
|
|
169
|
+
request_frame_fn: ?*const fn (context: ?*anyopaque) anyerror!void = null,
|
|
170
|
+
context: ?*anyopaque = null,
|
|
171
|
+
/// A requested frame has not yet reached adoption: the wake
|
|
172
|
+
/// coalescer. Set when a push requests a frame, cleared by
|
|
173
|
+
/// `adoptMediaSurfaceFrames` BEFORE it samples the slot, so a burst
|
|
174
|
+
/// of pushes costs at most one platform call and a push landing
|
|
175
|
+
/// after the clear requests the next frame itself.
|
|
176
|
+
pending: bool = false,
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
/// One process-lived mailbox slot: the ONLY memory a producer handle
|
|
180
|
+
/// ever touches. Claimed by a runtime at acquire (owner tag + fresh
|
|
181
|
+
/// generation), staged into by producer pushes, drained by loop-thread
|
|
182
|
+
/// adoption, reusable after release. The staging buffer is allocated
|
|
183
|
+
/// once and kept for the life of the process, so a stale handle's push
|
|
184
|
+
/// can never touch freed memory — it is fenced off by the generation
|
|
185
|
+
/// check instead.
|
|
186
|
+
const MediaSurfaceSlot = struct {
|
|
187
|
+
mutex: SpinMutex = .{},
|
|
188
|
+
/// The claiming runtime's process-unique tag (see
|
|
189
|
+
/// `media_surface_runtime_tag`); adoption scans match on it, so a
|
|
190
|
+
/// recycled runtime address can never inherit a dead runtime's
|
|
191
|
+
/// frames. 0 = never claimed.
|
|
192
|
+
owner_tag: u64 = 0,
|
|
193
|
+
surface_id: u64 = 0,
|
|
194
|
+
/// Bumped on every claim AND every release: a handle carries the
|
|
195
|
+
/// claim's generation, so pushes through a released handle (or a
|
|
196
|
+
/// copy of one) are refused loudly instead of landing in a slot
|
|
197
|
+
/// some other producer now owns.
|
|
198
|
+
generation: u64 = 0,
|
|
199
|
+
/// Claimed and not yet released.
|
|
200
|
+
active: bool = false,
|
|
201
|
+
/// Process-lived staging buffer (capacity
|
|
202
|
+
/// `max_media_surface_pixel_bytes`), allocated at first claim,
|
|
203
|
+
/// never freed.
|
|
204
|
+
staging: []u8 = &.{},
|
|
205
|
+
/// A staged frame awaits adoption (latest-wins: a new push
|
|
206
|
+
/// overwrites it in place).
|
|
207
|
+
staged: bool = false,
|
|
208
|
+
staged_width: usize = 0,
|
|
209
|
+
staged_height: usize = 0,
|
|
210
|
+
staged_byte_len: usize = 0,
|
|
211
|
+
staged_fingerprint: u64 = 0,
|
|
212
|
+
/// Fingerprint of the most recent push (staged or already adopted):
|
|
213
|
+
/// the push-boundary damage short-circuit — identical bytes pushed
|
|
214
|
+
/// again cost one hash, no copy, no staging, no invalidation.
|
|
215
|
+
last_push_fingerprint: u64 = 0,
|
|
216
|
+
/// The idle-compositor wake binding (its own mutex; see
|
|
217
|
+
/// `MediaSurfaceWake` for the ownership story).
|
|
218
|
+
wake: MediaSurfaceWake = .{},
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
var media_surface_slots: [max_media_surface_channels]MediaSurfaceSlot =
|
|
222
|
+
[_]MediaSurfaceSlot{.{}} ** max_media_surface_channels;
|
|
223
|
+
|
|
224
|
+
/// Process-wide monotonic runtime tags: unique per runtime instance for
|
|
225
|
+
/// the life of the process, so slot ownership survives allocator
|
|
226
|
+
/// address reuse across test harnesses.
|
|
227
|
+
var media_surface_tag_counter = std.atomic.Value(u64).init(1);
|
|
228
|
+
|
|
229
|
+
/// Adopted-texture entry on the RUNTIME (the loop-thread half); pixels
|
|
230
|
+
/// live in the runtime's lazily allocated per-entry buffer at the same
|
|
231
|
+
/// index (`media_surface_pixels`, one frame-budget block at first
|
|
232
|
+
/// adoption from the runtime's `owned_allocator` — the ownership
|
|
233
|
+
/// identity frozen from `Options.allocator` at init — freed by
|
|
234
|
+
/// `Runtime.deinit`).
|
|
235
|
+
pub const MediaSurfaceTextureEntry = struct {
|
|
236
|
+
surface_id: u64 = 0,
|
|
237
|
+
width: usize = 0,
|
|
238
|
+
height: usize = 0,
|
|
239
|
+
byte_len: usize = 0,
|
|
240
|
+
/// Content fingerprint of the adopted pixels (never 0 once
|
|
241
|
+
/// adopted): the adoption damage gate, and the precomputed
|
|
242
|
+
/// `ReferenceImage.content_fingerprint` the GPU cache planner keys
|
|
243
|
+
/// uploads by — so a pushed frame is hashed exactly once, on the
|
|
244
|
+
/// producer's thread.
|
|
245
|
+
fingerprint: u64 = 0,
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
/// Content fingerprint of a frame: dims + pixel bytes, computed on the
|
|
249
|
+
/// PRODUCER's thread at push. Mapped away from 0 so 0 stays the "no
|
|
250
|
+
/// content" sentinel everywhere.
|
|
251
|
+
fn frameFingerprint(width: usize, height: usize, rgba8: []const u8) u64 {
|
|
252
|
+
var hasher = std.hash.Wyhash.init(0x6d65646961); // "media"
|
|
253
|
+
hasher.update(std.mem.asBytes(&width));
|
|
254
|
+
hasher.update(std.mem.asBytes(&height));
|
|
255
|
+
hasher.update(rgba8);
|
|
256
|
+
const value = hasher.final();
|
|
257
|
+
return if (value == 0) 1 else value;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/// The producer handle: a slot pointer plus the claim generation.
|
|
261
|
+
/// Copyable; every copy dies with the generation when any copy calls
|
|
262
|
+
/// `release`. Both methods are callable from any thread and touch only
|
|
263
|
+
/// process-lived slot memory (see the module header's thread contract).
|
|
264
|
+
pub const MediaSurfaceProducer = struct {
|
|
265
|
+
slot: *MediaSurfaceSlot,
|
|
266
|
+
generation: u64,
|
|
267
|
+
surface_id: u64,
|
|
268
|
+
|
|
269
|
+
/// Stage one tightly-packed straight-alpha RGBA8 frame,
|
|
270
|
+
/// latest-wins. The pixels are copied before this returns; the
|
|
271
|
+
/// caller's buffer is immediately reusable. Pushing bytes identical
|
|
272
|
+
/// to the previous push is free past the hash (no copy, no
|
|
273
|
+
/// staging). Errors: `error.InvalidFrameDimensions` (zero or
|
|
274
|
+
/// overflowing dims, or a slice that is not exactly
|
|
275
|
+
/// width*height*4), `error.FrameTooLarge` (over
|
|
276
|
+
/// `max_media_surface_pixel_bytes`), `error.MediaSurfaceReleased`
|
|
277
|
+
/// (the handle's claim was released).
|
|
278
|
+
pub fn pushFrame(self: MediaSurfaceProducer, width: usize, height: usize, rgba8: []const u8) anyerror!void {
|
|
279
|
+
if (width == 0 or height == 0) return error.InvalidFrameDimensions;
|
|
280
|
+
const row_len = std.math.mul(usize, width, 4) catch return error.InvalidFrameDimensions;
|
|
281
|
+
const byte_len = std.math.mul(usize, row_len, height) catch return error.InvalidFrameDimensions;
|
|
282
|
+
if (rgba8.len != byte_len) return error.InvalidFrameDimensions;
|
|
283
|
+
if (byte_len > max_media_surface_pixel_bytes) return error.FrameTooLarge;
|
|
284
|
+
const fingerprint = frameFingerprint(width, height, rgba8);
|
|
285
|
+
|
|
286
|
+
const slot = self.slot;
|
|
287
|
+
{
|
|
288
|
+
slot.mutex.lock();
|
|
289
|
+
defer slot.mutex.unlock();
|
|
290
|
+
if (!slot.active or slot.generation != self.generation) return error.MediaSurfaceReleased;
|
|
291
|
+
// Push-boundary damage short-circuit: same bytes as the last
|
|
292
|
+
// push (adopted or still staged) change nothing downstream —
|
|
293
|
+
// including the wake below: unchanged pixels never stir an
|
|
294
|
+
// idle compositor.
|
|
295
|
+
if (slot.last_push_fingerprint == fingerprint) return;
|
|
296
|
+
@memcpy(slot.staging[0..byte_len], rgba8);
|
|
297
|
+
slot.staged = true;
|
|
298
|
+
slot.staged_width = width;
|
|
299
|
+
slot.staged_height = height;
|
|
300
|
+
slot.staged_byte_len = byte_len;
|
|
301
|
+
slot.staged_fingerprint = fingerprint;
|
|
302
|
+
slot.last_push_fingerprint = fingerprint;
|
|
303
|
+
}
|
|
304
|
+
// NEW bytes are staged: make sure a frame is coming to adopt
|
|
305
|
+
// them, even in an idle app whose demand-driven scheduler is
|
|
306
|
+
// asleep (the wake contract in the module header). Outside the
|
|
307
|
+
// data mutex — the wake half has its own lock — and gated on the
|
|
308
|
+
// handle's generation again, so a release/re-claim racing this
|
|
309
|
+
// gap wakes nobody spuriously.
|
|
310
|
+
requestWake(slot, self.generation);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/// Request ONE coalesced frame from the claiming runtime's platform
|
|
314
|
+
/// loop. Any-thread; touches only the slot's process-lived wake
|
|
315
|
+
/// half. The platform call runs UNDER the wake mutex (the abandon-
|
|
316
|
+
/// fence doctrine — see `MediaSurfaceWake`): teardown's disarm takes
|
|
317
|
+
/// the same mutex, so it can never complete while a call into the
|
|
318
|
+
/// host is in flight, and after it returns no call can start.
|
|
319
|
+
fn requestWake(slot: *MediaSurfaceSlot, generation: u64) void {
|
|
320
|
+
const wake = &slot.wake;
|
|
321
|
+
wake.mutex.lock();
|
|
322
|
+
defer wake.mutex.unlock();
|
|
323
|
+
// Stale claim (released, re-claimed, or torn down): no wake.
|
|
324
|
+
if (wake.generation != generation) return;
|
|
325
|
+
// Coalesce: a burst of pushes rides the one already-requested
|
|
326
|
+
// frame; `adoptMediaSurfaceFrames` clears the flag before it
|
|
327
|
+
// samples, so nothing staged after the clear is left frameless.
|
|
328
|
+
if (wake.pending) return;
|
|
329
|
+
// Unarmed: no thread-safe frame request on this host (or the
|
|
330
|
+
// binding was disarmed) — pacing stays purely demand-driven.
|
|
331
|
+
const request_fn = wake.request_frame_fn orelse return;
|
|
332
|
+
// A refused request leaves `pending` clear so the next push
|
|
333
|
+
// retries instead of latching a wake that never comes.
|
|
334
|
+
request_fn(wake.context) catch return;
|
|
335
|
+
wake.pending = true;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/// End this claim: later pushes through this handle (or any copy)
|
|
339
|
+
/// report `error.MediaSurfaceReleased`, and the slot becomes
|
|
340
|
+
/// claimable again. The runtime keeps the last ADOPTED texture — a
|
|
341
|
+
/// paused player keeps showing its final frame — and never touches
|
|
342
|
+
/// the slot after this. Idempotent.
|
|
343
|
+
pub fn release(self: MediaSurfaceProducer) void {
|
|
344
|
+
const slot = self.slot;
|
|
345
|
+
{
|
|
346
|
+
slot.mutex.lock();
|
|
347
|
+
defer slot.mutex.unlock();
|
|
348
|
+
if (!slot.active or slot.generation != self.generation) return;
|
|
349
|
+
// Atomic for the lock-free reclaim scan's benefit, exactly
|
|
350
|
+
// like the claim stores in acquire.
|
|
351
|
+
ownershipStore(bool, &slot.active, false, .release);
|
|
352
|
+
slot.staged = false;
|
|
353
|
+
slot.last_push_fingerprint = 0;
|
|
354
|
+
slot.generation +%= 1;
|
|
355
|
+
}
|
|
356
|
+
// The ended claim's wake binding is already fenced off by the
|
|
357
|
+
// generation bump above; clearing it too keeps no host pointer
|
|
358
|
+
// parked in process-lived memory longer than its claim. Taken
|
|
359
|
+
// AFTER the data mutex dropped — the two locks never nest.
|
|
360
|
+
const wake = &slot.wake;
|
|
361
|
+
wake.mutex.lock();
|
|
362
|
+
defer wake.mutex.unlock();
|
|
363
|
+
if (wake.generation != self.generation) return;
|
|
364
|
+
wake.generation = 0;
|
|
365
|
+
wake.owner_tag = 0;
|
|
366
|
+
wake.request_frame_fn = null;
|
|
367
|
+
wake.context = null;
|
|
368
|
+
wake.pending = false;
|
|
369
|
+
}
|
|
370
|
+
};
|
|
371
|
+
|
|
372
|
+
pub fn RuntimeMediaSurfaces(comptime Runtime: type) type {
|
|
373
|
+
return struct {
|
|
374
|
+
/// Claim the texture channel for `surface_id` and hand back the
|
|
375
|
+
/// producer handle. Loop-thread-only (the runtime is touched);
|
|
376
|
+
/// the returned handle's methods are any-thread. One live
|
|
377
|
+
/// producer per (runtime, surface id): a second acquire while
|
|
378
|
+
/// one is unreleased reports `error.MediaSurfaceInUse`. Errors:
|
|
379
|
+
/// `error.InvalidSurfaceId` (0, or the reserved namespace bit
|
|
380
|
+
/// `canvas.media_surface_image_id_bit` set),
|
|
381
|
+
/// `error.MediaSurfaceChannelsExhausted` (all
|
|
382
|
+
/// `max_media_surface_channels` process-wide slots hold live
|
|
383
|
+
/// claims).
|
|
384
|
+
pub fn acquireMediaSurfaceProducer(self: *Runtime, surface_id: u64) anyerror!MediaSurfaceProducer {
|
|
385
|
+
if (surface_id == 0 or (surface_id & canvas.media_surface_image_id_bit) != 0) {
|
|
386
|
+
return error.InvalidSurfaceId;
|
|
387
|
+
}
|
|
388
|
+
if (self.media_surface_runtime_tag == 0) {
|
|
389
|
+
self.media_surface_runtime_tag = media_surface_tag_counter.fetchAdd(1, .monotonic);
|
|
390
|
+
}
|
|
391
|
+
const tag = self.media_surface_runtime_tag;
|
|
392
|
+
for (&media_surface_slots) |*slot| {
|
|
393
|
+
slot.mutex.lock();
|
|
394
|
+
const duplicate = slot.active and slot.owner_tag == tag and slot.surface_id == surface_id;
|
|
395
|
+
slot.mutex.unlock();
|
|
396
|
+
if (duplicate) return error.MediaSurfaceInUse;
|
|
397
|
+
}
|
|
398
|
+
for (&media_surface_slots) |*slot| {
|
|
399
|
+
slot.mutex.lock();
|
|
400
|
+
if (slot.active) {
|
|
401
|
+
slot.mutex.unlock();
|
|
402
|
+
continue;
|
|
403
|
+
}
|
|
404
|
+
if (slot.staging.len == 0) {
|
|
405
|
+
// First claim of this slot in the process: the
|
|
406
|
+
// staging buffer is allocated once and kept forever
|
|
407
|
+
// (process-lived, like everything a producer thread
|
|
408
|
+
// can reach).
|
|
409
|
+
slot.staging = process_allocator.alloc(u8, max_media_surface_pixel_bytes) catch {
|
|
410
|
+
slot.mutex.unlock();
|
|
411
|
+
return error.OutOfMemory;
|
|
412
|
+
};
|
|
413
|
+
}
|
|
414
|
+
// The ownership triple is written atomically (still
|
|
415
|
+
// under the data mutex, which serializes WRITERS): the
|
|
416
|
+
// reclaim scan reads it lock-free from other threads
|
|
417
|
+
// (`mediaSurfaceHasActiveSlot`), and the Zig/LLVM
|
|
418
|
+
// memory model calls a plain store racing an atomic
|
|
419
|
+
// load a data race — atomic on both sides is what it
|
|
420
|
+
// honestly supports. `active` is stored LAST so a
|
|
421
|
+
// lock-free reader that sees it true sees a fully
|
|
422
|
+
// stamped claim on this slot's other two fields (the
|
|
423
|
+
// .release/.acquire pair orders them).
|
|
424
|
+
ownershipStore(u64, &slot.owner_tag, tag, .monotonic);
|
|
425
|
+
ownershipStore(u64, &slot.surface_id, surface_id, .monotonic);
|
|
426
|
+
slot.generation +%= 1;
|
|
427
|
+
ownershipStore(bool, &slot.active, true, .release);
|
|
428
|
+
slot.staged = false;
|
|
429
|
+
slot.last_push_fingerprint = 0;
|
|
430
|
+
const generation = slot.generation;
|
|
431
|
+
slot.mutex.unlock();
|
|
432
|
+
// Arm the wake binding for this claim (after the data
|
|
433
|
+
// mutex dropped — the locks never nest). No producer of
|
|
434
|
+
// THIS generation exists before we return the handle,
|
|
435
|
+
// so nothing races the arm; a stale generation taking
|
|
436
|
+
// the wake mutex fails its generation gate.
|
|
437
|
+
const services = &self.options.platform.services;
|
|
438
|
+
const wake = &slot.wake;
|
|
439
|
+
wake.mutex.lock();
|
|
440
|
+
wake.generation = generation;
|
|
441
|
+
wake.owner_tag = tag;
|
|
442
|
+
wake.request_frame_fn = services.request_frame_fn;
|
|
443
|
+
wake.context = if (services.request_frame_fn != null) services.context else null;
|
|
444
|
+
wake.pending = false;
|
|
445
|
+
wake.mutex.unlock();
|
|
446
|
+
return .{ .slot = slot, .generation = generation, .surface_id = surface_id };
|
|
447
|
+
}
|
|
448
|
+
return error.MediaSurfaceChannelsExhausted;
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
/// Adopt staged producer frames, paced by the compositor: the
|
|
452
|
+
/// gpu-surface frame dispatch calls this once per frame event,
|
|
453
|
+
/// so a burst of pushes between presents collapses to ONE
|
|
454
|
+
/// adoption of the newest frame (latest-wins made observable).
|
|
455
|
+
/// Damage-tracked: a staged frame whose fingerprint matches the
|
|
456
|
+
/// adopted texture is dropped without a copy or an invalidation;
|
|
457
|
+
/// a changed frame is copied into the runtime's texture pool and
|
|
458
|
+
/// repaints exactly like a registered-image swap.
|
|
459
|
+
pub fn adoptMediaSurfaceFrames(self: *Runtime) void {
|
|
460
|
+
const tag = self.media_surface_runtime_tag;
|
|
461
|
+
if (tag == 0) return;
|
|
462
|
+
var changed = false;
|
|
463
|
+
for (&media_surface_slots) |*slot| {
|
|
464
|
+
// Drain the wake coalescer BEFORE sampling the staged
|
|
465
|
+
// flag: a push that staged bytes before this clear is
|
|
466
|
+
// adopted by this very pass (its stage happened before
|
|
467
|
+
// its wake attempt, which is what we are answering); a
|
|
468
|
+
// push landing after the clear requests its own frame.
|
|
469
|
+
// Either way nothing staged is ever left frameless.
|
|
470
|
+
slot.wake.mutex.lock();
|
|
471
|
+
if (slot.wake.owner_tag == tag) slot.wake.pending = false;
|
|
472
|
+
slot.wake.mutex.unlock();
|
|
473
|
+
|
|
474
|
+
slot.mutex.lock();
|
|
475
|
+
if (!slot.active or slot.owner_tag != tag or !slot.staged) {
|
|
476
|
+
slot.mutex.unlock();
|
|
477
|
+
continue;
|
|
478
|
+
}
|
|
479
|
+
var reclaimed_texture_id: u64 = 0;
|
|
480
|
+
const entry_index = mediaSurfaceEntryIndex(self, slot.surface_id, &reclaimed_texture_id) orelse {
|
|
481
|
+
// Registry saturated by retained textures of
|
|
482
|
+
// released channels; loud, never silent. Dropping
|
|
483
|
+
// the stage must also forget the push-boundary
|
|
484
|
+
// fingerprint (0 = no previous push, unreachable
|
|
485
|
+
// by frameFingerprint): a producer re-pushing the
|
|
486
|
+
// SAME bytes — a paused video's frame, album art —
|
|
487
|
+
// must stage and wake again so the retry can adopt
|
|
488
|
+
// once the registry heals, not die in the
|
|
489
|
+
// push-boundary dedup gate.
|
|
490
|
+
slot.staged = false;
|
|
491
|
+
slot.last_push_fingerprint = 0;
|
|
492
|
+
slot.mutex.unlock();
|
|
493
|
+
// No stderr on freestanding targets (the docs' wasm
|
|
494
|
+
// preview host): analyzing the print would drag
|
|
495
|
+
// `std.Io.Threaded` in — the session recorder's
|
|
496
|
+
// fail() guard, mirrored.
|
|
497
|
+
if (comptime builtin.os.tag != .freestanding) {
|
|
498
|
+
std.debug.print(
|
|
499
|
+
"[native-sdk] media-surface texture registry full: dropping frames for surface {d} (max_media_surface_channels = {d})\n",
|
|
500
|
+
.{ slot.surface_id, max_media_surface_channels },
|
|
501
|
+
);
|
|
502
|
+
}
|
|
503
|
+
continue;
|
|
504
|
+
};
|
|
505
|
+
if (self.media_surface_entries[entry_index].fingerprint == slot.staged_fingerprint) {
|
|
506
|
+
// Adoption-boundary damage short-circuit: the staged
|
|
507
|
+
// frame IS the adopted texture.
|
|
508
|
+
slot.staged = false;
|
|
509
|
+
slot.mutex.unlock();
|
|
510
|
+
continue;
|
|
511
|
+
}
|
|
512
|
+
if (self.media_surface_pixels[entry_index].len == 0) {
|
|
513
|
+
// The entry's texture buffer, allocated LAZILY at
|
|
514
|
+
// first adoption (one frame-budget block from the
|
|
515
|
+
// runtime's FROZEN `owned_allocator` — never the
|
|
516
|
+
// live `options.allocator`, which is public and
|
|
517
|
+
// mutable, so a swap between this allocation and
|
|
518
|
+
// `Runtime.deinit`'s free must not split the
|
|
519
|
+
// alloc/free pair across allocators): a runtime
|
|
520
|
+
// that never adopts a producer frame carries zero
|
|
521
|
+
// media-texture bytes — an embedded pool at this
|
|
522
|
+
// budget was 32 MiB in every Runtime, the
|
|
523
|
+
// registered-font-pool regression's twin.
|
|
524
|
+
self.media_surface_pixels[entry_index] = self.owned_allocator.alloc(u8, max_media_surface_pixel_bytes) catch {
|
|
525
|
+
// OOM degrades like the saturated registry:
|
|
526
|
+
// this frame drops loudly, the channel stays
|
|
527
|
+
// healthy, and the producer's NEXT PUSH stages
|
|
528
|
+
// and wakes a retry — even a byte-identical
|
|
529
|
+
// one, because the fingerprint reset below
|
|
530
|
+
// (0 = no previous push, unreachable by
|
|
531
|
+
// frameFingerprint) reopens the push-boundary
|
|
532
|
+
// dedup gate a static frame would otherwise
|
|
533
|
+
// die in. An entry reclaim that already
|
|
534
|
+
// happened still removes the reclaimed host
|
|
535
|
+
// texture (below, outside the lock) — the
|
|
536
|
+
// entry now names the new surface either way.
|
|
537
|
+
slot.staged = false;
|
|
538
|
+
slot.last_push_fingerprint = 0;
|
|
539
|
+
slot.mutex.unlock();
|
|
540
|
+
if (comptime builtin.os.tag != .freestanding) {
|
|
541
|
+
std.debug.print(
|
|
542
|
+
"[native-sdk] media-surface texture buffer allocation failed: dropping frames for surface {d} ({d} bytes requested)\n",
|
|
543
|
+
.{ slot.surface_id, max_media_surface_pixel_bytes },
|
|
544
|
+
);
|
|
545
|
+
}
|
|
546
|
+
if (reclaimed_texture_id != 0) {
|
|
547
|
+
self.options.platform.services.removeGpuSurfaceImage(reclaimed_texture_id) catch {};
|
|
548
|
+
}
|
|
549
|
+
continue;
|
|
550
|
+
};
|
|
551
|
+
}
|
|
552
|
+
@memcpy(
|
|
553
|
+
self.media_surface_pixels[entry_index][0..slot.staged_byte_len],
|
|
554
|
+
slot.staging[0..slot.staged_byte_len],
|
|
555
|
+
);
|
|
556
|
+
self.media_surface_entries[entry_index] = .{
|
|
557
|
+
.surface_id = slot.surface_id,
|
|
558
|
+
.width = slot.staged_width,
|
|
559
|
+
.height = slot.staged_height,
|
|
560
|
+
.byte_len = slot.staged_byte_len,
|
|
561
|
+
.fingerprint = slot.staged_fingerprint,
|
|
562
|
+
};
|
|
563
|
+
slot.staged = false;
|
|
564
|
+
slot.mutex.unlock();
|
|
565
|
+
// The reclaimed texture's host-side removal runs OUTSIDE
|
|
566
|
+
// the slot lock: guarded sections stay bounded memcpys
|
|
567
|
+
// (the thread contract), never platform calls a pushing
|
|
568
|
+
// producer would spin behind.
|
|
569
|
+
if (reclaimed_texture_id != 0) {
|
|
570
|
+
self.options.platform.services.removeGpuSurfaceImage(reclaimed_texture_id) catch {};
|
|
571
|
+
}
|
|
572
|
+
changed = true;
|
|
573
|
+
}
|
|
574
|
+
if (changed) {
|
|
575
|
+
// Same repaint contract as a registered-image swap: the
|
|
576
|
+
// content fingerprint changed, caches re-upload, views
|
|
577
|
+
// re-render their next frame.
|
|
578
|
+
runtime_canvas_images.RuntimeCanvasImages(Runtime).noteCanvasImagesChanged(self);
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
/// Disarm every wake binding this runtime armed: after this
|
|
583
|
+
/// returns, no producer push can call into the runtime's
|
|
584
|
+
/// platform host — not even one already past its generation
|
|
585
|
+
/// check, because the platform call happens under the wake
|
|
586
|
+
/// mutex this sweep takes (the abandon-fence doctrine; see
|
|
587
|
+
/// `MediaSurfaceWake`). MUST run before the platform host dies
|
|
588
|
+
/// when a producer may still hold an unreleased handle: the run
|
|
589
|
+
/// loop's exit defer covers real apps (the same ordering that
|
|
590
|
+
/// stops the automation arrival watcher), `TestHarness.destroy`
|
|
591
|
+
/// covers tests, and the embed host's destroy covers embedded
|
|
592
|
+
/// runtimes. Idempotent; a pending (already requested) frame is
|
|
593
|
+
/// simply never answered — the host may still deliver or drop
|
|
594
|
+
/// it, and an unclaimed `frame_requested` on a dying loop is a
|
|
595
|
+
/// no-op, so teardown loses nothing. The slots' DATA halves are
|
|
596
|
+
/// deliberately untouched: pushes keep landing in inert
|
|
597
|
+
/// process-lived memory, the existing UAF-safety story.
|
|
598
|
+
pub fn disarmMediaSurfaceWakes(self: *Runtime) void {
|
|
599
|
+
const tag = self.media_surface_runtime_tag;
|
|
600
|
+
if (tag == 0) return;
|
|
601
|
+
for (&media_surface_slots) |*slot| {
|
|
602
|
+
const wake = &slot.wake;
|
|
603
|
+
wake.mutex.lock();
|
|
604
|
+
defer wake.mutex.unlock();
|
|
605
|
+
if (wake.owner_tag != tag) continue;
|
|
606
|
+
wake.generation = 0;
|
|
607
|
+
wake.owner_tag = 0;
|
|
608
|
+
wake.request_frame_fn = null;
|
|
609
|
+
wake.context = null;
|
|
610
|
+
wake.pending = false;
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
/// The adopted textures as `presentation_only` ReferenceImages
|
|
615
|
+
/// (the frame planner appends them to the registered-image set):
|
|
616
|
+
/// GPU/packet hosts upload and composite them, the deterministic
|
|
617
|
+
/// reference renderer skips them (the placeholder policy in
|
|
618
|
+
/// canvas.reference.findReferenceImage).
|
|
619
|
+
pub fn adoptedMediaSurfaceTextures(self: *Runtime, scratch: []canvas.ReferenceImage) []const canvas.ReferenceImage {
|
|
620
|
+
var len: usize = 0;
|
|
621
|
+
for (self.media_surface_entries[0..self.media_surface_count], 0..) |entry, index| {
|
|
622
|
+
if (entry.fingerprint == 0) continue;
|
|
623
|
+
scratch[len] = .{
|
|
624
|
+
.id = canvas.mediaSurfaceTextureImageId(entry.surface_id),
|
|
625
|
+
.width = entry.width,
|
|
626
|
+
.height = entry.height,
|
|
627
|
+
.pixels = self.media_surface_pixels[index][0..entry.byte_len],
|
|
628
|
+
.content_fingerprint = entry.fingerprint,
|
|
629
|
+
.presentation_only = true,
|
|
630
|
+
};
|
|
631
|
+
len += 1;
|
|
632
|
+
}
|
|
633
|
+
return scratch[0..len];
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
/// Dimensions of the adopted texture for `surface_id`, or null
|
|
637
|
+
/// while no producer frame has been adopted (the placeholder
|
|
638
|
+
/// state). Test and diagnostics seam.
|
|
639
|
+
pub fn adoptedMediaSurfaceTexture(self: *const Runtime, surface_id: u64) ?MediaSurfaceTextureEntry {
|
|
640
|
+
for (self.media_surface_entries[0..self.media_surface_count]) |entry| {
|
|
641
|
+
if (entry.surface_id == surface_id and entry.fingerprint != 0) return entry;
|
|
642
|
+
}
|
|
643
|
+
return null;
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
/// Find or claim the runtime texture entry for `surface_id`.
|
|
647
|
+
/// When every entry is live, entries whose surface no longer has
|
|
648
|
+
/// an ACTIVE slot on this runtime are reclaimed oldest-first (a
|
|
649
|
+
/// released player's retained last frame yields to a live one).
|
|
650
|
+
/// Called by the adoption loop with the drained slot's data
|
|
651
|
+
/// mutex HELD — the ownership scan is lock-free (see
|
|
652
|
+
/// `mediaSurfaceHasActiveSlot`) so this never takes a second
|
|
653
|
+
/// slot lock. When a retained entry is reclaimed,
|
|
654
|
+
/// `reclaimed_texture_id` receives its derived texture id so the
|
|
655
|
+
/// CALLER can remove the host-side image after the slot lock
|
|
656
|
+
/// drops (hosts that retain copied side-channel textures — the
|
|
657
|
+
/// AppKit NSImage store — hold one per uploaded id, and this
|
|
658
|
+
/// reclaim is the only live-runtime path where a retained
|
|
659
|
+
/// texture's id leaves the resource set for good: without the
|
|
660
|
+
/// removal the host store grows unboundedly as surface ids
|
|
661
|
+
/// rotate, and a widget still drawing the reclaimed id could
|
|
662
|
+
/// resolve the stale host image instead of its placeholder).
|
|
663
|
+
fn mediaSurfaceEntryIndex(self: *Runtime, surface_id: u64, reclaimed_texture_id: *u64) ?usize {
|
|
664
|
+
for (self.media_surface_entries[0..self.media_surface_count], 0..) |entry, index| {
|
|
665
|
+
if (entry.surface_id == surface_id) return index;
|
|
666
|
+
}
|
|
667
|
+
if (self.media_surface_count < max_media_surface_channels) {
|
|
668
|
+
const index = self.media_surface_count;
|
|
669
|
+
self.media_surface_count += 1;
|
|
670
|
+
self.media_surface_entries[index] = .{ .surface_id = surface_id };
|
|
671
|
+
return index;
|
|
672
|
+
}
|
|
673
|
+
for (self.media_surface_entries[0..self.media_surface_count], 0..) |entry, index| {
|
|
674
|
+
if (!mediaSurfaceHasActiveSlot(self.media_surface_runtime_tag, entry.surface_id)) {
|
|
675
|
+
// Mirror unregisterCanvasImage's teardown: hand the
|
|
676
|
+
// reclaimed texture id up for a best-effort host
|
|
677
|
+
// removal (hosts without the seam report
|
|
678
|
+
// UnsupportedService, hosts that never uploaded the
|
|
679
|
+
// id treat it as a no-op — both are swallowed at
|
|
680
|
+
// the call, exactly like unregister).
|
|
681
|
+
reclaimed_texture_id.* = canvas.mediaSurfaceTextureImageId(entry.surface_id);
|
|
682
|
+
self.media_surface_entries[index] = .{ .surface_id = surface_id };
|
|
683
|
+
return index;
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
return null;
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
/// Whether `surface_id` has a live claim on this runtime — a
|
|
690
|
+
/// LOCK-FREE snapshot, taking NO slot mutex, because the caller
|
|
691
|
+
/// (the adoption loop, via `mediaSurfaceEntryIndex`) holds the
|
|
692
|
+
/// drained slot's data mutex and this module's locks never
|
|
693
|
+
/// nest. Both failure modes of the locking scan it replaced
|
|
694
|
+
/// were real: relocking the HELD slot self-deadlocked the
|
|
695
|
+
/// moment one runtime's entry table filled (the spin mutex is
|
|
696
|
+
/// not reentrant), and locking OTHER slots deadlocked ABBA when
|
|
697
|
+
/// two runtimes with full tables drained different slots and
|
|
698
|
+
/// scanned each other's. The `SpinMutex.lock` debug assertion
|
|
699
|
+
/// now enforces the no-nesting discipline module-wide.
|
|
700
|
+
///
|
|
701
|
+
/// Memory-model honesty: each field is read with a monotonic
|
|
702
|
+
/// atomic load (tear-free per field; the claim/release stores
|
|
703
|
+
/// are atomic under the data mutex, so no access is a data
|
|
704
|
+
/// race), but the TRIPLE is not a consistent snapshot — a claim
|
|
705
|
+
/// or release racing this scan may show a half-stamped state.
|
|
706
|
+
/// That is acceptable BY the caller's contract: this feeds only
|
|
707
|
+
/// the retained-entry reclaim heuristic, whose worst raced
|
|
708
|
+
/// outcomes are reclaiming a texture whose surface is being
|
|
709
|
+
/// re-claimed this very instant (the next adoption of that
|
|
710
|
+
/// surface re-creates the entry and re-uploads) or keeping a
|
|
711
|
+
/// retained texture one extra frame (it re-qualifies on the
|
|
712
|
+
/// next full-table adoption). `active` is stored last with
|
|
713
|
+
/// .release on claim and checked first with .acquire here, so
|
|
714
|
+
/// an observed-true claim reads its own tag and surface id,
|
|
715
|
+
/// never a predecessor's torn remains.
|
|
716
|
+
fn mediaSurfaceHasActiveSlot(tag: u64, surface_id: u64) bool {
|
|
717
|
+
for (&media_surface_slots) |*slot| {
|
|
718
|
+
if (!ownershipLoad(bool, &slot.active, .acquire)) continue;
|
|
719
|
+
if (ownershipLoad(u64, &slot.owner_tag, .monotonic) != tag) continue;
|
|
720
|
+
if (ownershipLoad(u64, &slot.surface_id, .monotonic) != surface_id) continue;
|
|
721
|
+
return true;
|
|
722
|
+
}
|
|
723
|
+
return false;
|
|
724
|
+
}
|
|
725
|
+
};
|
|
726
|
+
}
|