Fix zero-copy frame lifecycle

Enable zero-copy through the public API before opening the device and configure a driver-owned zero-copy ring slot.

Return every delivered frame through IOCTL_GIGA_RELEASE_VIDEO_FRAME and validate the complete frame token before release.

Preserve ownership when the release IOCTL fails so stop, close, or the next start can retry instead of discarding the driver pointer.

Stop DMA and join the backend acquire/event thread before draining frames, preventing acquire from racing stop or disable.

Drain caller-held, ready, unread, and pending zero-copy frames before stop, restart, disable, device close, or process destruction.

Block restart and zero-copy disable when outstanding frames cannot be released successfully.

Release unread frames on signal unplug and before format or resolution changes rebuild the frame ring.

Propagate stop and release errors through the public API and report release failures in the standalone preview.

Document zero-copy ownership, lifecycle requirements, diagnostics, and data flow.

Verified with Standalone Debug and Release builds.
This commit is contained in:
Mark.lin
2026-08-19 15:34:37 +08:00
parent 61da467d83
commit 024df690c3
12 changed files with 443 additions and 138 deletions
+17 -3
View File
@@ -32,6 +32,7 @@ bin/gvfg_preview.dll
```text
gvfg_enumerate_devices
-> gvfg_create
-> gvfg_set_zero_copy_enabled (optional, before open)
-> gvfg_open_channel
-> gvfg_start
-> 重複:
@@ -90,8 +91,9 @@ int main(void)
- API 是 pull model;應用程式自行決定在哪個執行緒呼叫
`gvfg_read_frame()`。GUI 程式建議在 worker thread 讀取,再把 UI 更新送回
UI thread。
- `frame.data` 由 SDK 擁有,只在對應的 `gvfg_release_frame()` 前有效。資料若要
長期保存,必須先複製。
- Copy mode 的 `frame.data` 由 SDK 擁有;zero-copy mode 則指向 driver-owned
buffer。兩種模式都只在對應的 `gvfg_release_frame()` 前有效,資料若要長期
保存,必須先複製。
- 每個 handle 同時最多持有一個 frame。尚未 release 又呼叫
`gvfg_read_frame()`,會回傳 `GVFG_ESTATE`。
- release 時必須傳回原本的完整 `gvfg_frame_t`,不可修改欄位。
@@ -149,6 +151,9 @@ int main(void)
- `gvfg_enumerate_devices(out_devices, max_devices)`:列舉裝置。傳入
`NULL, 0` 可只查數量;回傳值是寫入數量或可用裝置數,無裝置時為 `0`。
- `gvfg_create(&handle)`:建立 closed session。
- `gvfg_set_zero_copy_enabled(handle, enabled)`:選用 zero-copy;只能在
`gvfg_open_channel()` 前呼叫,預設為關閉。
- `gvfg_get_zero_copy_enabled(handle, &enabled)`:查詢 session 選擇的模式。
- `gvfg_open_channel(handle, device_index, channel)`:開啟列舉所得裝置,channel
必須為 `GVFG_CHANNEL_0` 或 `GVFG_CHANNEL_1`。
- `gvfg_start(handle)`:開始擷取。若目前無訊號,成功進入訊號監看模式;此時
@@ -156,11 +161,20 @@ int main(void)
- `gvfg_stop(handle)`:停止擷取;重複呼叫仍回傳成功。
- `gvfg_destroy(handle)`:必要時先停止,再銷毀 handle。銷毀後不得再使用。
Zero-copy mode 的 driver lifecycle 由 SDK 管理:open 時 enable,每次成功
`gvfg_read_frame()` 後由 `gvfg_release_frame()` 歸還 driver frame,destroy/close
前 disable。Device open 後不可直接切換;應 destroy session、重新 create、設定模式
後再 open。
### Frame
- `gvfg_read_frame(handle, &frame, timeout_ms)`:取得一個 SDK-owned frame。
- `gvfg_read_frame(handle, &frame, timeout_ms)`:取得一個 frame;ownership 依 copy/
zero-copy mode 而定,但 release contract 相同。
- `gvfg_release_frame(handle, &frame)`:釋放原 frame token。
`gvfg_set_video_format()` 只能在 stream 尚未開始或已 stop 時呼叫;streaming 中
切換會回傳 `GVFG_ESTATE`。應用程式應在 UI 上同步鎖定格式選項。
### 查詢
- `gvfg_get_signal_status(handle, &status)`:查詢 selected channel 的連線、尺寸、
+32 -2
View File
@@ -86,7 +86,7 @@ register 與 DMA 實作不屬於本文件。
| 欄位 | 型別 | 說明 |
| ------------------ | -------------- | -------------------------------------------- |
| `data` | `const void *` | SDK-owned 原生 frame buffer;release 或 stop 後失效 |
| `data` | `const void *` | Copy mode 為 SDK-owned、zero-copy 為 driver-owned;release 或 stop 後失效 |
| `data_size` | `uint64_t` | `data` 指向的總 byte 數 |
| `width` | `int` | frame 寬度,單位 pixel |
| `height` | `int` | frame 高度,單位 pixel |
@@ -191,6 +191,33 @@ gvfg_status_t gvfg_open_channel(gvfg_handle handle,
對同一 handle 再次 open 會先關閉原 backend session;應用程式仍應依正常 lifecycle
先 stop,再切換裝置或 channel。
### Zero-copy mode selection
```c
gvfg_status_t gvfg_set_zero_copy_enabled(gvfg_handle handle, int enabled);
gvfg_status_t gvfg_get_zero_copy_enabled(gvfg_handle handle, int *out_enabled);
```
- `set` 只能在 `gvfg_create()` 後、`gvfg_open_channel()` 前呼叫。
- `enabled` 只接受 0(copy)或 1(zero-copy),預設為 0。
- Device 已 open 時呼叫 `set` 會回傳 `GVFG_ESTATE`。
- `get` 可查詢 session 選定模式;`out_enabled` 不可為 NULL。
- Zero-copy mode 的 `frame.data` 為 driver-owned pointer;仍必須以相同 descriptor
呼叫 `gvfg_release_frame()`,且同一 handle 同時最多持有一張 frame。
- SDK 在 open 時 enable driver zero-copy,在 destroy/close 前 disable。
### `gvfg_set_video_format`
```c
gvfg_status_t gvfg_set_video_format(gvfg_handle handle,
gvfg_pixel_format_t format);
```
- 支援 `GVFG_PIXFMT_YUY2` 與 `GVFG_PIXFMT_Y210`。
- Device 必須已 open,且 stream 必須尚未 start 或已 stop。
- Streaming 中呼叫回傳 `GVFG_ESTATE`;不支援的 format 回傳 `GVFG_ENOTSUP` 或
`GVFG_EINVAL`。
### 1.17 `gvfg_start`
```c
@@ -220,6 +247,9 @@ gvfg_status_t gvfg_read_frame(gvfg_handle handle,
- `GVFG_ETIMEOUT`:期限內沒有 frame。
- `GVFG_EIO`/`GVFG_ENOTSUP`:frame/backend 無效或格式不支援。
Copy mode 回傳 SDK ring buffer;zero-copy mode 回傳 driver-owned buffer。兩者的
pointer 都只保證有效到對應的 `gvfg_release_frame()`。
### 1.19 `gvfg_release_frame`
```c
@@ -229,7 +259,7 @@ gvfg_status_t gvfg_release_frame(gvfg_handle handle,
- `handle`:取得該 frame 的同一個 handle。
- `frame`:`gvfg_read_frame()` 原封不動回傳的完整 descriptor。
- `GVFG_OK`:成功歸還 frame,slot 可再次使用。
- `GVFG_OK`:成功歸還 frame;copy slot 可再次使用,或 zero-copy frame 已歸還 driver。
- `GVFG_EINVAL`:NULL 或 token 內容與 held frame 不符。
- `GVFG_ESTATE`:沒有 backend、目前沒有 held frame,或 backend release 狀態不正確。
+41 -96
View File
@@ -1,106 +1,51 @@
<mxfile host="app.diagrams.net" modified="2026-07-30T00:00:00.000Z" agent="Codex" version="24.7.17" type="device">
<diagram id="gvfg-dma-ring-flow" name="GVFG DMA Ring Data Flow">
<mxGraphModel dx="1600" dy="1000" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="1600" pageHeight="1100" math="0" shadow="0">
<mxfile host="app.diagrams.net" modified="2026-08-19T00:00:00.000Z" agent="Codex" version="24.7.17" type="device">
<diagram id="gvfg-dma-flow" name="GVFG Copy and Zero-Copy Flow">
<mxGraphModel dx="1600" dy="1000" grid="1" gridSize="10" page="1" pageScale="1" pageWidth="1600" pageHeight="1100">
<root>
<mxCell id="0"/>
<mxCell id="1" parent="0"/>
<mxCell id="title" value="GVFG driver DMA buffer to SDK ring data flow" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=28;fontStyle=1" vertex="1" parent="1">
<mxGeometry x="360" y="30" width="880" height="40" as="geometry"/>
</mxCell>
<mxCell id="title" value="GVFG DMA delivery — copy and zero-copy modes" style="text;html=1;strokeColor=none;align=center;fontSize=26;fontStyle=1" vertex="1" parent="1"><mxGeometry x="300" y="25" width="1000" height="45" as="geometry"/></mxCell>
<mxCell id="abi" value="Requires new driver ABI: VIDEO_START/STOP, GET_FRAME(MAXULONG), ZERO-COPY ENABLE/ACQUIRE/RELEASE/DISABLE" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff2cc;strokeColor=#d6b656;fontSize=13" vertex="1" parent="1"><mxGeometry x="300" y="80" width="1000" height="45" as="geometry"/></mxCell>
<mxCell id="hw" value="FPGA / driver DMA buffer&#xa;DMA complete interrupt" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#f8cecc;strokeColor=#b85450;fontSize=14;fontStyle=1" vertex="1" parent="1"><mxGeometry x="100" y="160" width="240" height="80" as="geometry"/></mxCell>
<mxCell id="event" value="dma_event_ wakes&#xa;capture_thread_proc()&#xa;handle_dma_event(channel)" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#dae8fc;strokeColor=#6c8ebf;fontSize=14" vertex="1" parent="1"><mxGeometry x="450" y="150" width="260" height="100" as="geometry"/></mxCell>
<mxCell id="mode" value="zero-copy enabled?" style="rhombus;whiteSpace=wrap;html=1;fillColor=#fff2cc;strokeColor=#d6b656;fontSize=14;fontStyle=1" vertex="1" parent="1"><mxGeometry x="850" y="145" width="220" height="110" as="geometry"/></mxCell>
<mxCell id="hw" value="Video source / FPGA&#xa;writes DMA buffer N&#xa;DMA complete interrupt" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#f8cecc;strokeColor=#b85450;fontSize=14;fontStyle=1" vertex="1" parent="1">
<mxGeometry x="80" y="130" width="210" height="80" as="geometry"/>
</mxCell>
<mxCell id="event" value="Kernel driver&#xa;SetEvent(dma_event_)" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#dae8fc;strokeColor=#6c8ebf;fontSize=14;fontStyle=1" vertex="1" parent="1">
<mxGeometry x="360" y="130" width="230" height="80" as="geometry"/>
</mxCell>
<mxCell id="pending" value="single capture_thread_proc()&#xa;WaitForMultipleObjects() wakes&#xa;dispatches DMA event" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#dae8fc;strokeColor=#6c8ebf;fontSize=14" vertex="1" parent="1">
<mxGeometry x="660" y="130" width="230" height="80" as="geometry"/>
</mxCell>
<mxCell id="datawait" value="handle_dma_event()&#xa;IOCTL_GET_VIDEO_DONE_INDEX" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#d5e8d4;strokeColor=#82b366;fontSize=14;fontStyle=1" vertex="1" parent="1">
<mxGeometry x="960" y="130" width="260" height="80" as="geometry"/>
</mxCell>
<mxCell id="clear" value="frameIndex = doneIndex % 16&#xa;select completed driver DMA buffer N" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#d5e8d4;strokeColor=#82b366;fontSize=14" vertex="1" parent="1">
<mxGeometry x="1280" y="130" width="240" height="90" as="geometry"/>
</mxCell>
<mxCell id="copyLane" value="COPY MODE — SDK 3-slot ring" style="swimlane;html=1;startSize=34;rounded=1;fillColor=#d5e8d4;strokeColor=#82b366;fontSize=17;fontStyle=1" vertex="1" parent="1"><mxGeometry x="60" y="310" width="700" height="500" as="geometry"/></mxCell>
<mxCell id="copySlot" value="Select writable FrameSlot&#xa;ready=false, in_use=false&#xa;ring pressure may drop oldest unread frame" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#82b366;fontSize=13" vertex="1" parent="1"><mxGeometry x="90" y="370" width="280" height="100" as="geometry"/></mxCell>
<mxCell id="copyGet" value="IOCTL_PCIES2MM_GET_FRAME&#xa;input {channel, 0xFFFFFFFF}&#xa;one copy: driver DMA → slot.data&#xa;GET_VIDEO_DONE_INDEX removed" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#82b366;fontSize=13;fontStyle=1" vertex="1" parent="1"><mxGeometry x="440" y="360" width="280" height="120" as="geometry"/></mxCell>
<mxCell id="copyPublish" value="publish_frame()&#xa;slot.ready=true, in_use=false&#xa;frame_cv_.notify_one()" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#82b366;fontSize=13" vertex="1" parent="1"><mxGeometry x="250" y="520" width="300" height="90" as="geometry"/></mxCell>
<mxCell id="copyRead" value="gvfg_read_frame()&#xa;frame.data = slot.data.data()&#xa;SDK-owned until release" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e1d5e7;strokeColor=#9673a6;fontSize=13" vertex="1" parent="1"><mxGeometry x="90" y="665" width="270" height="95" as="geometry"/></mxCell>
<mxCell id="copyRelease" value="gvfg_release_frame()&#xa;validate token&#xa;slot.in_use=false&#xa;slot reusable" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e1d5e7;strokeColor=#9673a6;fontSize=13;fontStyle=1" vertex="1" parent="1"><mxGeometry x="450" y="655" width="270" height="105" as="geometry"/></mxCell>
<mxCell id="findslot" value="SDK 3-slot frame_ring_&#xa;search slot.in_use == false" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#d5e8d4;strokeColor=#82b366;fontSize=14;fontStyle=1" vertex="1" parent="1">
<mxGeometry x="80" y="310" width="240" height="80" as="geometry"/>
</mxCell>
<mxCell id="droptest" value="Free slot available?" style="rhombus;whiteSpace=wrap;html=1;fillColor=#fff2cc;strokeColor=#d6b656;fontSize=13" vertex="1" parent="1">
<mxGeometry x="390" y="300" width="190" height="100" as="geometry"/>
</mxCell>
<mxCell id="drop" value="No: capture thread waits&#xa;data_cv_.wait_for()&#xa;driver buffers may advance" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffe6cc;strokeColor=#d79b00;fontSize=14" vertex="1" parent="1">
<mxGeometry x="650" y="290" width="230" height="70" as="geometry"/>
</mxCell>
<mxCell id="markwriting" value="Mark slot writing&#xa;ready = false&#xa;in_use = true" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#d5e8d4;strokeColor=#82b366;fontSize=14" vertex="1" parent="1">
<mxGeometry x="650" y="390" width="230" height="90" as="geometry"/>
</mxCell>
<mxCell id="readc2h" value="IOCTL_GET_FRAME&#xa;driver DMA buffer N -&gt; one copy -&gt; slot.data&#xa;validate bytesReturned == expected" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#d5e8d4;strokeColor=#82b366;fontSize=14;fontStyle=1" vertex="1" parent="1">
<mxGeometry x="960" y="330" width="260" height="80" as="geometry"/>
</mxCell>
<mxCell id="publish" value="publish_frame(slotIndex, bytes)&#xa;slot.ready = true&#xa;slot.in_use = false&#xa;frame_cv_.notify_one()" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#d5e8d4;strokeColor=#82b366;fontSize=14" vertex="1" parent="1">
<mxGeometry x="1280" y="310" width="260" height="120" as="geometry"/>
</mxCell>
<mxCell id="zcLane" value="ZERO-COPY MODE — one outstanding driver frame" style="swimlane;html=1;startSize=34;rounded=1;fillColor=#dae8fc;strokeColor=#6c8ebf;fontSize=17;fontStyle=1" vertex="1" parent="1"><mxGeometry x="830" y="310" width="700" height="500" as="geometry"/></mxCell>
<mxCell id="zcEnable" value="Open device&#xa;ENABLE_FRAME_ZEROCOPY(channel)" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#6c8ebf;fontSize=13" vertex="1" parent="1"><mxGeometry x="860" y="365" width="260" height="85" as="geometry"/></mxCell>
<mxCell id="zcHeld" value="frame ready or held?" style="rhombus;whiteSpace=wrap;html=1;fillColor=#fff2cc;strokeColor=#d6b656;fontSize=13" vertex="1" parent="1"><mxGeometry x="1220" y="355" width="230" height="105" as="geometry"/></mxCell>
<mxCell id="zcAcquire" value="No: ACQUIRE_VIDEO_FRAME_ZEROCOPY&#xa;input {channel, 0xFFFFFFFF}&#xa;output driver-owned pointer&#xa;no SDK payload copy" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#6c8ebf;fontSize=13;fontStyle=1" vertex="1" parent="1"><mxGeometry x="860" y="520" width="300" height="115" as="geometry"/></mxCell>
<mxCell id="zcSkip" value="Yes: skip this acquire event&#xa;keep held driver pointer valid&#xa;no loss statistic or event" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff2cc;strokeColor=#d6b656;fontSize=13" vertex="1" parent="1"><mxGeometry x="1210" y="525" width="270" height="95" as="geometry"/></mxCell>
<mxCell id="zcRead" value="gvfg_read_frame()&#xa;returns driver pointer&#xa;valid until release" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e1d5e7;strokeColor=#9673a6;fontSize=13" vertex="1" parent="1"><mxGeometry x="860" y="675" width="270" height="85" as="geometry"/></mxCell>
<mxCell id="zcRelease" value="gvfg_release_frame()&#xa;validate token&#xa;RELEASE_VIDEO_FRAME(channel)" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e1d5e7;strokeColor=#9673a6;fontSize=13;fontStyle=1" vertex="1" parent="1"><mxGeometry x="1210" y="665" width="290" height="95" as="geometry"/></mxCell>
<mxCell id="wait" value="gvfg_read_frame()&#xa;backend-&gt;wait_frame(timeout, frame)" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e1d5e7;strokeColor=#9673a6;fontSize=14;fontStyle=1" vertex="1" parent="1">
<mxGeometry x="80" y="560" width="270" height="80" as="geometry"/>
</mxCell>
<mxCell id="select" value="wait_frame() selects newest ready slot&#xa;slot.ready = false&#xa;slot.in_use = true&#xa;out.data = slot.data.data()" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e1d5e7;strokeColor=#9673a6;fontSize=14" vertex="1" parent="1">
<mxGeometry x="430" y="540" width="310" height="120" as="geometry"/>
</mxCell>
<mxCell id="render" value="optional frame consumers before release&#xa;gvfg_preview_render_frame()&#xa;gvfg_gpu_convert_to_buffer()" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e1d5e7;strokeColor=#9673a6;fontSize=14" vertex="1" parent="1">
<mxGeometry x="820" y="540" width="270" height="90" as="geometry"/>
</mxCell>
<mxCell id="appUse" value="app uses frame&#xa;data valid until gvfg_release_frame()" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e1d5e7;strokeColor=#9673a6;fontSize=14" vertex="1" parent="1">
<mxGeometry x="1160" y="540" width="300" height="90" as="geometry"/>
</mxCell>
<mxCell id="release" value="release_frame(frame)&#xa;slot.in_use = false&#xa;data_cv_.notify_all()" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e1d5e7;strokeColor=#9673a6;fontSize=14;fontStyle=1" vertex="1" parent="1">
<mxGeometry x="630" y="740" width="300" height="90" as="geometry"/>
</mxCell>
<mxCell id="consumer" value="Before release: application may preview, convert, record, or copy. Return the frame descriptor unchanged." style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e1d5e7;strokeColor=#9673a6;fontSize=14" vertex="1" parent="1"><mxGeometry x="300" y="855" width="1000" height="60" as="geometry"/></mxCell>
<mxCell id="close" value="Stop / close / exit: release held frame first; zero-copy sends DISABLE_FRAME_ZEROCOPY(channel), then closes device" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#f5f5f5;strokeColor=#666666;fontSize=14;fontStyle=1" vertex="1" parent="1"><mxGeometry x="450" y="960" width="700" height="70" as="geometry"/></mxCell>
<mxCell id="stateTitle" value="FrameSlot state lifecycle" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=20;fontStyle=1" vertex="1" parent="1">
<mxGeometry x="80" y="900" width="280" height="30" as="geometry"/>
</mxCell>
<mxCell id="sfree" value="Free&#xa;ready=false&#xa;in_use=false" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#f5f5f5;strokeColor=#666666;fontSize=13" vertex="1" parent="1">
<mxGeometry x="380" y="875" width="150" height="80" as="geometry"/>
</mxCell>
<mxCell id="swriting" value="Writing&#xa;ready=false&#xa;in_use=true" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#d5e8d4;strokeColor=#82b366;fontSize=13" vertex="1" parent="1">
<mxGeometry x="600" y="875" width="150" height="80" as="geometry"/>
</mxCell>
<mxCell id="sready" value="Ready&#xa;ready=true&#xa;in_use=false" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff2cc;strokeColor=#d6b656;fontSize=13" vertex="1" parent="1">
<mxGeometry x="820" y="875" width="150" height="80" as="geometry"/>
</mxCell>
<mxCell id="sdelivered" value="Delivered&#xa;ready=false&#xa;in_use=true" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e1d5e7;strokeColor=#9673a6;fontSize=13" vertex="1" parent="1">
<mxGeometry x="1040" y="875" width="160" height="80" as="geometry"/>
</mxCell>
<mxCell id="srelease" value="Released&#xa;ready=false&#xa;in_use=false" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#f5f5f5;strokeColor=#666666;fontSize=13" vertex="1" parent="1">
<mxGeometry x="1270" y="875" width="160" height="80" as="geometry"/>
</mxCell>
<mxCell id="e1" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;endArrow=block;strokeWidth=2" edge="1" parent="1" source="hw" target="event"><mxGeometry relative="1" as="geometry"/></mxCell>
<mxCell id="e2" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;endArrow=block;strokeWidth=2" edge="1" parent="1" source="event" target="pending"><mxGeometry relative="1" as="geometry"/></mxCell>
<mxCell id="e3" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;endArrow=block;strokeWidth=2" edge="1" parent="1" source="pending" target="datawait"><mxGeometry relative="1" as="geometry"/></mxCell>
<mxCell id="e4" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;endArrow=block;strokeWidth=2" edge="1" parent="1" source="datawait" target="clear"><mxGeometry relative="1" as="geometry"/></mxCell>
<mxCell id="e5" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;endArrow=block;strokeWidth=2" edge="1" parent="1" source="clear" target="findslot"><mxGeometry relative="1" as="geometry"><Array as="points"><mxPoint x="1400" y="260"/><mxPoint x="200" y="260"/></Array></mxGeometry></mxCell>
<mxCell id="e6" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;endArrow=block;strokeWidth=2" edge="1" parent="1" source="findslot" target="droptest"><mxGeometry relative="1" as="geometry"/></mxCell>
<mxCell id="e7" value="yes" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;endArrow=block;strokeWidth=2" edge="1" parent="1" source="droptest" target="markwriting"><mxGeometry relative="1" as="geometry"/></mxCell>
<mxCell id="e8" value="no" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;endArrow=block;strokeWidth=2" edge="1" parent="1" source="droptest" target="drop"><mxGeometry relative="1" as="geometry"/></mxCell>
<mxCell id="e9" value="release wakes waiter" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;endArrow=block;strokeWidth=2;dashed=1" edge="1" parent="1" source="drop" target="findslot"><mxGeometry relative="1" as="geometry"/></mxCell>
<mxCell id="e10" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;endArrow=block;strokeWidth=2" edge="1" parent="1" source="markwriting" target="readc2h"><mxGeometry relative="1" as="geometry"/></mxCell>
<mxCell id="e11" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;endArrow=block;strokeWidth=2" edge="1" parent="1" source="readc2h" target="publish"><mxGeometry relative="1" as="geometry"/></mxCell>
<mxCell id="e12" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;endArrow=block;strokeWidth=2" edge="1" parent="1" source="publish" target="wait"><mxGeometry relative="1" as="geometry"><Array as="points"><mxPoint x="1410" y="500"/><mxPoint x="220" y="500"/></Array></mxGeometry></mxCell>
<mxCell id="e13" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;endArrow=block;strokeWidth=2" edge="1" parent="1" source="wait" target="select"><mxGeometry relative="1" as="geometry"/></mxCell>
<mxCell id="e14" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;endArrow=block;strokeWidth=2" edge="1" parent="1" source="select" target="render"><mxGeometry relative="1" as="geometry"/></mxCell>
<mxCell id="e15" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;endArrow=block;strokeWidth=2" edge="1" parent="1" source="render" target="appUse"><mxGeometry relative="1" as="geometry"/></mxCell>
<mxCell id="e16" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;endArrow=block;strokeWidth=2" edge="1" parent="1" source="appUse" target="release"><mxGeometry relative="1" as="geometry"><Array as="points"><mxPoint x="1310" y="700"/><mxPoint x="780" y="700"/></Array></mxGeometry></mxCell>
<mxCell id="e17" value="slot reusable" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;endArrow=block;strokeWidth=2;dashed=1" edge="1" parent="1" source="release" target="findslot"><mxGeometry relative="1" as="geometry"><Array as="points"><mxPoint x="780" y="850"/><mxPoint x="40" y="850"/><mxPoint x="40" y="350"/></Array></mxGeometry></mxCell>
<mxCell id="se1" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;endArrow=block" edge="1" parent="1" source="sfree" target="swriting"><mxGeometry relative="1" as="geometry"/></mxCell>
<mxCell id="se2" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;endArrow=block" edge="1" parent="1" source="swriting" target="sready"><mxGeometry relative="1" as="geometry"/></mxCell>
<mxCell id="se3" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;endArrow=block" edge="1" parent="1" source="sready" target="sdelivered"><mxGeometry relative="1" as="geometry"/></mxCell>
<mxCell id="se4" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;endArrow=block" edge="1" parent="1" source="sdelivered" target="srelease"><mxGeometry relative="1" as="geometry"/></mxCell>
<mxCell id="se5" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;endArrow=block;dashed=1" edge="1" parent="1" source="srelease" target="sfree"><mxGeometry relative="1" as="geometry"><Array as="points"><mxPoint x="1350" y="1000"/><mxPoint x="455" y="1000"/></Array></mxGeometry></mxCell>
<mxCell id="e1" style="edgeStyle=orthogonalEdgeStyle;rounded=0;html=1;endArrow=block;strokeWidth=2" edge="1" parent="1" source="hw" target="event"><mxGeometry relative="1" as="geometry"/></mxCell>
<mxCell id="e2" style="edgeStyle=orthogonalEdgeStyle;rounded=0;html=1;endArrow=block;strokeWidth=2" edge="1" parent="1" source="event" target="mode"><mxGeometry relative="1" as="geometry"/></mxCell>
<mxCell id="e3" value="no / copy" style="edgeStyle=orthogonalEdgeStyle;rounded=0;html=1;endArrow=block;strokeWidth=2;strokeColor=#82b366" edge="1" parent="1" source="mode" target="copySlot"><mxGeometry relative="1" as="geometry"><Array as="points"><mxPoint x="960" y="280"/><mxPoint x="230" y="280"/></Array></mxGeometry></mxCell>
<mxCell id="e4" value="yes / zero-copy" style="edgeStyle=orthogonalEdgeStyle;rounded=0;html=1;endArrow=block;strokeWidth=2;strokeColor=#6c8ebf" edge="1" parent="1" source="mode" target="zcHeld"><mxGeometry relative="1" as="geometry"/></mxCell>
<mxCell id="c1" style="edgeStyle=orthogonalEdgeStyle;rounded=0;html=1;endArrow=block;strokeColor=#82b366" edge="1" parent="1" source="copySlot" target="copyGet"><mxGeometry relative="1" as="geometry"/></mxCell>
<mxCell id="c2" style="edgeStyle=orthogonalEdgeStyle;rounded=0;html=1;endArrow=block;strokeColor=#82b366" edge="1" parent="1" source="copyGet" target="copyPublish"><mxGeometry relative="1" as="geometry"/></mxCell>
<mxCell id="c3" style="edgeStyle=orthogonalEdgeStyle;rounded=0;html=1;endArrow=block;strokeColor=#82b366" edge="1" parent="1" source="copyPublish" target="copyRead"><mxGeometry relative="1" as="geometry"/></mxCell>
<mxCell id="c4" style="edgeStyle=orthogonalEdgeStyle;rounded=0;html=1;endArrow=block;strokeColor=#9673a6" edge="1" parent="1" source="copyRead" target="copyRelease"><mxGeometry relative="1" as="geometry"/></mxCell>
<mxCell id="c5" value="slot reusable" style="edgeStyle=orthogonalEdgeStyle;rounded=0;html=1;endArrow=block;dashed=1;strokeColor=#82b366" edge="1" parent="1" source="copyRelease" target="copySlot"><mxGeometry relative="1" as="geometry"><Array as="points"><mxPoint x="585" y="790"/><mxPoint x="40" y="790"/><mxPoint x="40" y="420"/></Array></mxGeometry></mxCell>
<mxCell id="z0" value="enabled at open" style="edgeStyle=orthogonalEdgeStyle;rounded=0;html=1;endArrow=block;dashed=1;strokeColor=#6c8ebf" edge="1" parent="1" source="zcEnable" target="zcHeld"><mxGeometry relative="1" as="geometry"/></mxCell>
<mxCell id="z1" value="no" style="edgeStyle=orthogonalEdgeStyle;rounded=0;html=1;endArrow=block;strokeColor=#6c8ebf" edge="1" parent="1" source="zcHeld" target="zcAcquire"><mxGeometry relative="1" as="geometry"/></mxCell>
<mxCell id="z2" value="yes" style="edgeStyle=orthogonalEdgeStyle;rounded=0;html=1;endArrow=block;strokeColor=#d6b656" edge="1" parent="1" source="zcHeld" target="zcSkip"><mxGeometry relative="1" as="geometry"/></mxCell>
<mxCell id="z3" style="edgeStyle=orthogonalEdgeStyle;rounded=0;html=1;endArrow=block;strokeColor=#6c8ebf" edge="1" parent="1" source="zcAcquire" target="zcRead"><mxGeometry relative="1" as="geometry"/></mxCell>
<mxCell id="z4" style="edgeStyle=orthogonalEdgeStyle;rounded=0;html=1;endArrow=block;strokeColor=#9673a6" edge="1" parent="1" source="zcRead" target="zcRelease"><mxGeometry relative="1" as="geometry"/></mxCell>
<mxCell id="z5" value="driver frame reusable" style="edgeStyle=orthogonalEdgeStyle;rounded=0;html=1;endArrow=block;dashed=1;strokeColor=#6c8ebf" edge="1" parent="1" source="zcRelease" target="zcHeld"><mxGeometry relative="1" as="geometry"><Array as="points"><mxPoint x="1355" y="790"/><mxPoint x="1510" y="790"/><mxPoint x="1510" y="407"/></Array></mxGeometry></mxCell>
<mxCell id="a1" style="edgeStyle=orthogonalEdgeStyle;rounded=0;html=1;endArrow=block;dashed=1;strokeColor=#9673a6" edge="1" parent="1" source="copyRead" target="consumer"><mxGeometry relative="1" as="geometry"/></mxCell>
<mxCell id="a2" style="edgeStyle=orthogonalEdgeStyle;rounded=0;html=1;endArrow=block;dashed=1;strokeColor=#9673a6" edge="1" parent="1" source="zcRead" target="consumer"><mxGeometry relative="1" as="geometry"/></mxCell>
<mxCell id="a3" style="edgeStyle=orthogonalEdgeStyle;rounded=0;html=1;endArrow=block" edge="1" parent="1" source="consumer" target="close"><mxGeometry relative="1" as="geometry"/></mxCell>
</root>
</mxGraphModel>
</diagram>
+37 -3
View File
@@ -65,14 +65,14 @@ Created/Closed -> Opened -> Running -> Opened -> Destroyed
## 4. DMA 與 frame ring
目前資料路徑:
Copy mode 資料路徑:
```text
source frame
-> hardware/driver DMA buffer
-> DMA complete event
-> capture worker 查詢 done index
-> IOCTL_GET_FRAME 複製到 SDK ring free slot
-> capture worker 收到 DMA complete event
-> IOCTL_GET_FRAME(frameIndex=0xFFFFFFFF) 複製到 SDK ring free slot
-> slot ready + sequence 更新 + frame_cv notify
-> wait_frame 選取 ready frame
-> facade 回傳指向 slot.data 的 gvfg_frame_t
@@ -94,6 +94,26 @@ ring 滿時的行為是即時擷取的 drop/backpressure policy,不是 lossles
internal stats 觀察 `frames_dropped`。不要將 slot 數、driver DMA buffer 數或 done-index
演算法暴露為客戶契約。
Zero-copy mode 資料路徑:
```text
gvfg_create
-> gvfg_set_zero_copy_enabled(1)
-> open channel 時 IOCTL_GIGA_ENABLE_FRAME_ZEROCOPY(channel)
-> DMA complete event
-> IOCTL_GIGA_ACQUIRE_VIDEO_FRAME_ZEROCOPY(channel, 0xFFFFFFFF)
-> facade 回傳 driver-owned pointer
-> customer/preview/conversion
-> gvfg_release_frame
-> IOCTL_GIGA_RELEASE_VIDEO_FRAME(channel)
-> close/destroy 時 IOCTL_GIGA_DISABLE_FRAME_ZEROCOPY(channel)
```
Zero-copy 同時只允許一張 outstanding frame。若下一個 DMA event 到達時 caller 尚未
release,backend 只會跳過該次 acquire,不會覆寫仍由 caller 使用的 driver pointer。
這種情況不計入 `frames_dropped`,也不送出 `GVFG_EVENT_FRAME_LOSS`;driver 目前沒有
提供可用來確認實際遺失 frame 的 sequence/drop counter。
## 5. Frame token 與 ABI
Facade 在 release 時驗證原 token 的 data、size、width、height、stride、format、
@@ -103,6 +123,20 @@ x64 `gvfg_frame_t` ABI 已在 `gvfg_capture.cpp` 以 static assertions 固定為
及明確欄位 offsets。修改公開 struct 時必須視為 ABI 變更,不能只重新編譯 DLL。
新增 metadata 優先考慮新 query API 或帶 size/version 的新 struct。
## Driver ABI requirement
目前 SDK 只支援新版 PCIE S2MM driver,必要 private ABI 包含:
- `IOCTL_GIGA_VIDEO_START` (`0x808`) / `IOCTL_GIGA_VIDEO_STOP` (`0x809`)。
- `IOCTL_GIGA_RELEASE_VIDEO_FRAME` (`0x80A`)。
- `IOCTL_GIGA_ACQUIRE_VIDEO_FRAME_ZEROCOPY` (`0x80B`)。
- `IOCTL_GIGA_ENABLE_FRAME_ZEROCOPY` (`0x80C`) /
`IOCTL_GIGA_DISABLE_FRAME_ZEROCOPY` (`0x80D`)。
- Copy mode 的 `IOCTL_PCIES2MM_GET_FRAME` 接受 `frameIndex=0xFFFFFFFF`。
不再呼叫 `GET_VIDEO_DONE_INDEX`,也不再 fallback 直接寫入 video enable、DMA enable
或 IRQ mask registers。舊 driver 不屬於此 revision 的支援範圍。
目前原生格式:
- YUY2:2 bytes/pixel,8-bit packed 4:2:2,byte order 為 Y0 U0 Y1 V0。