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
+16
View File
@@ -67,6 +67,22 @@ SDK 不再呼叫 `IOCTL_PCIES2MM_GET_VIDEO_DONE_INDEX`,也不會在新 IOCTL
`IRQ_MASK_W1S_OFFSET`。若搭配舊版 driver,stream start 或 frame capture `IRQ_MASK_W1S_OFFSET`。若搭配舊版 driver,stream start 或 frame capture
可能失敗。 可能失敗。
### Zero-copy selection
上層程式可在 `gvfg_create()` 後、`gvfg_open_channel()` 前選擇 zero-copy:
```c
gvfg_handle handle = NULL;
gvfg_create(&handle);
gvfg_set_zero_copy_enabled(handle, 1); /* 0: copy, 1: zero-copy */
gvfg_open_channel(handle, device_index, GVFG_CHANNEL_0);
```
預設為 copy mode。Device open 後不可切換模式;如需切換,必須 destroy
並重建 session。Zero-copy mode 仍使用相同的 `gvfg_read_frame()` /
`gvfg_release_frame()` ownership contract,每次成功 read 都必須 release。
SDK 會在 open 時 enable zero-copy,並在 close/destroy 時 disable。
`gvfg_qt_preview.exe` 只使用 public API,主畫面只顯示 input 與 Preview `gvfg_qt_preview.exe` 只使用 public API,主畫面只顯示 input 與 Preview
狀態;IRQ、DMA、ring slot 等資訊只存在 internal diagnostic tool。 狀態;IRQ、DMA、ring slot 等資訊只存在 internal diagnostic tool。
+17 -3
View File
@@ -32,6 +32,7 @@ bin/gvfg_preview.dll
```text ```text
gvfg_enumerate_devices gvfg_enumerate_devices
-> gvfg_create -> gvfg_create
-> gvfg_set_zero_copy_enabled (optional, before open)
-> gvfg_open_channel -> gvfg_open_channel
-> gvfg_start -> gvfg_start
-> 重複: -> 重複:
@@ -90,8 +91,9 @@ int main(void)
- API 是 pull model;應用程式自行決定在哪個執行緒呼叫 - API 是 pull model;應用程式自行決定在哪個執行緒呼叫
`gvfg_read_frame()`。GUI 程式建議在 worker thread 讀取,再把 UI 更新送回 `gvfg_read_frame()`。GUI 程式建議在 worker thread 讀取,再把 UI 更新送回
UI thread。 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 又呼叫 - 每個 handle 同時最多持有一個 frame。尚未 release 又呼叫
`gvfg_read_frame()`,會回傳 `GVFG_ESTATE`。 `gvfg_read_frame()`,會回傳 `GVFG_ESTATE`。
- release 時必須傳回原本的完整 `gvfg_frame_t`,不可修改欄位。 - release 時必須傳回原本的完整 `gvfg_frame_t`,不可修改欄位。
@@ -149,6 +151,9 @@ int main(void)
- `gvfg_enumerate_devices(out_devices, max_devices)`:列舉裝置。傳入 - `gvfg_enumerate_devices(out_devices, max_devices)`:列舉裝置。傳入
`NULL, 0` 可只查數量;回傳值是寫入數量或可用裝置數,無裝置時為 `0`。 `NULL, 0` 可只查數量;回傳值是寫入數量或可用裝置數,無裝置時為 `0`。
- `gvfg_create(&handle)`:建立 closed session。 - `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_open_channel(handle, device_index, channel)`:開啟列舉所得裝置,channel
必須為 `GVFG_CHANNEL_0` 或 `GVFG_CHANNEL_1`。 必須為 `GVFG_CHANNEL_0` 或 `GVFG_CHANNEL_1`。
- `gvfg_start(handle)`:開始擷取。若目前無訊號,成功進入訊號監看模式;此時 - `gvfg_start(handle)`:開始擷取。若目前無訊號,成功進入訊號監看模式;此時
@@ -156,11 +161,20 @@ int main(void)
- `gvfg_stop(handle)`:停止擷取;重複呼叫仍回傳成功。 - `gvfg_stop(handle)`:停止擷取;重複呼叫仍回傳成功。
- `gvfg_destroy(handle)`:必要時先停止,再銷毀 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 ### 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_release_frame(handle, &frame)`:釋放原 frame token。
`gvfg_set_video_format()` 只能在 stream 尚未開始或已 stop 時呼叫;streaming 中
切換會回傳 `GVFG_ESTATE`。應用程式應在 UI 上同步鎖定格式選項。
### 查詢 ### 查詢
- `gvfg_get_signal_status(handle, &status)`:查詢 selected channel 的連線、尺寸、 - `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 數 | | `data_size` | `uint64_t` | `data` 指向的總 byte 數 |
| `width` | `int` | frame 寬度,單位 pixel | | `width` | `int` | frame 寬度,單位 pixel |
| `height` | `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 對同一 handle 再次 open 會先關閉原 backend session;應用程式仍應依正常 lifecycle
先 stop,再切換裝置或 channel。 先 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` ### 1.17 `gvfg_start`
```c ```c
@@ -220,6 +247,9 @@ gvfg_status_t gvfg_read_frame(gvfg_handle handle,
- `GVFG_ETIMEOUT`:期限內沒有 frame。 - `GVFG_ETIMEOUT`:期限內沒有 frame。
- `GVFG_EIO`/`GVFG_ENOTSUP`:frame/backend 無效或格式不支援。 - `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` ### 1.19 `gvfg_release_frame`
```c ```c
@@ -229,7 +259,7 @@ gvfg_status_t gvfg_release_frame(gvfg_handle handle,
- `handle`:取得該 frame 的同一個 handle。 - `handle`:取得該 frame 的同一個 handle。
- `frame`:`gvfg_read_frame()` 原封不動回傳的完整 descriptor。 - `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_EINVAL`:NULL 或 token 內容與 held frame 不符。
- `GVFG_ESTATE`:沒有 backend、目前沒有 held frame,或 backend release 狀態不正確。 - `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"> <mxfile host="app.diagrams.net" modified="2026-08-19T00:00:00.000Z" agent="Codex" version="24.7.17" type="device">
<diagram id="gvfg-dma-ring-flow" name="GVFG DMA Ring Data Flow"> <diagram id="gvfg-dma-flow" name="GVFG Copy and Zero-Copy 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"> <mxGraphModel dx="1600" dy="1000" grid="1" gridSize="10" page="1" pageScale="1" pageWidth="1600" pageHeight="1100">
<root> <root>
<mxCell id="0"/> <mxCell id="0"/>
<mxCell id="1" parent="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"> <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>
<mxGeometry x="360" y="30" width="880" height="40" as="geometry"/> <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> <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"> <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>
<mxGeometry x="80" y="130" width="210" height="80" as="geometry"/> <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> <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="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"> <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>
<mxGeometry x="360" y="130" width="230" height="80" as="geometry"/> <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> <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="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="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"> <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>
<mxGeometry x="80" y="310" width="240" height="80" as="geometry"/> <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> <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="droptest" value="Free slot available?" style="rhombus;whiteSpace=wrap;html=1;fillColor=#fff2cc;strokeColor=#d6b656;fontSize=13" vertex="1" parent="1"> <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>
<mxGeometry x="390" y="300" width="190" height="100" as="geometry"/> <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> <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="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"> <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>
<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="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"> <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>
<mxGeometry x="80" y="560" width="270" height="80" as="geometry"/> <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>
<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="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"> <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>
<mxGeometry x="80" y="900" width="280" height="30" as="geometry"/> <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> <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="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"> <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>
<mxGeometry x="380" y="875" width="150" height="80" as="geometry"/> <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> <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="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"> <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>
<mxGeometry x="600" y="875" width="150" height="80" as="geometry"/> <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> <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="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"> <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>
<mxGeometry x="820" y="875" width="150" height="80" as="geometry"/> <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> <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="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"> <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>
<mxGeometry x="1040" y="875" width="160" height="80" as="geometry"/> <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> <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="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"> <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>
<mxGeometry x="1270" y="875" width="160" height="80" as="geometry"/> <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> <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>
<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>
</root> </root>
</mxGraphModel> </mxGraphModel>
</diagram> </diagram>
+37 -3
View File
@@ -65,14 +65,14 @@ Created/Closed -> Opened -> Running -> Opened -> Destroyed
## 4. DMA 與 frame ring ## 4. DMA 與 frame ring
目前資料路徑: Copy mode 資料路徑:
```text ```text
source frame source frame
-> hardware/driver DMA buffer -> hardware/driver DMA buffer
-> DMA complete event -> DMA complete event
-> capture worker 查詢 done index -> capture worker 收到 DMA complete event
-> IOCTL_GET_FRAME 複製到 SDK ring free slot -> IOCTL_GET_FRAME(frameIndex=0xFFFFFFFF) 複製到 SDK ring free slot
-> slot ready + sequence 更新 + frame_cv notify -> slot ready + sequence 更新 + frame_cv notify
-> wait_frame 選取 ready frame -> wait_frame 選取 ready frame
-> facade 回傳指向 slot.data 的 gvfg_frame_t -> 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 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 ## 5. Frame token 與 ABI
Facade 在 release 時驗證原 token 的 data、size、width、height、stride、format、 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。 及明確欄位 offsets。修改公開 struct 時必須視為 ABI 變更,不能只重新編譯 DLL。
新增 metadata 優先考慮新 query API 或帶 size/version 的新 struct。 新增 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。 - YUY2:2 bytes/pixel,8-bit packed 4:2:2,byte order 為 Y0 U0 Y1 V0。
+27 -3
View File
@@ -317,6 +317,15 @@ bool MainWindow::openDevice()
return false; return false;
} }
const bool zeroCopyEnabled = ui_->zeroCopyCheckBox->isChecked();
st = gvfg_set_zero_copy_enabled(handle_, zeroCopyEnabled ? 1 : 0);
if (st != GVFG_OK)
{
showError(QStringLiteral("gvfg_set_zero_copy_enabled"), st);
closeDevice();
return false;
}
const int selection = ui_->deviceCombo->currentData().toInt(); const int selection = ui_->deviceCombo->currentData().toInt();
const int deviceIndex = selection / 2; const int deviceIndex = selection / 2;
const int channelIndex = selection % 2; const int channelIndex = selection % 2;
@@ -335,7 +344,10 @@ bool MainWindow::openDevice()
} }
lastSignalStatusText_.clear(); lastSignalStatusText_.clear();
appendLog(QStringLiteral("Opened device index %1 CH%2").arg(deviceIndex).arg(channelIndex)); appendLog(QStringLiteral("Opened device index %1 CH%2 | mode=%3")
.arg(deviceIndex)
.arg(channelIndex)
.arg(zeroCopyEnabled ? QStringLiteral("zero-copy") : QStringLiteral("copy")));
appendLog(QStringLiteral("Signal monitoring active")); appendLog(QStringLiteral("Signal monitoring active"));
updateSignalStatus(); updateSignalStatus();
runtimeStatusTimer_->start(); runtimeStatusTimer_->start();
@@ -629,6 +641,10 @@ void MainWindow::updateSignalStatus(bool queryHardware)
.arg(signalFrameText(signal)) .arg(signalFrameText(signal))
: QStringLiteral("Input | CH%1 | No signal") : QStringLiteral("Input | CH%1 | No signal")
.arg(signal.channel)); .arg(signal.channel));
statusLines << QStringLiteral("Mode | %1")
.arg(ui_->zeroCopyCheckBox->isChecked()
? QStringLiteral("Zero-copy")
: QStringLiteral("Copy"));
statusLines << (previewInfoOk statusLines << (previewInfoOk
? previewCallSamples > 0 ? previewCallSamples > 0
? QStringLiteral("Preview | %1 | %2 | GPU call avg=%3 max300=%4 max=%5 ms/frame samples=%6") ? QStringLiteral("Preview | %1 | %2 | GPU call avg=%3 max300=%4 max=%5 ms/frame samples=%6")
@@ -736,7 +752,8 @@ void MainWindow::updateUiState()
ui_->fullscreenPreviewButton->setEnabled(viewAvailable); ui_->fullscreenPreviewButton->setEnabled(viewAvailable);
ui_->refreshButton->setEnabled(!deviceOpen && !captureRunning_); ui_->refreshButton->setEnabled(!deviceOpen && !captureRunning_);
ui_->deviceCombo->setEnabled(!deviceOpen && !captureRunning_); ui_->deviceCombo->setEnabled(!deviceOpen && !captureRunning_);
ui_->outputFormatCombo->setEnabled(deviceOpen && !captureRunning_); ui_->outputFormatCombo->setEnabled(!captureRunning_);
ui_->zeroCopyCheckBox->setEnabled(!deviceOpen && !captureRunning_);
} }
void MainWindow::showError(const QString &apiName, gvfg_status_t status) void MainWindow::showError(const QString &apiName, gvfg_status_t status)
@@ -1023,7 +1040,14 @@ void MainWindow::captureReadLoop()
} }
} }
} }
gvfg_release_frame(handle_, &frame); const gvfg_status_t releaseStatus = gvfg_release_frame(handle_, &frame);
if (releaseStatus != GVFG_OK)
{
QMetaObject::invokeMethod(this, [this, releaseStatus]()
{ showError(QStringLiteral("gvfg_release_frame"), releaseStatus); },
Qt::QueuedConnection);
break;
}
continue; continue;
} }
+10
View File
@@ -126,6 +126,16 @@
</item> </item>
</widget> </widget>
</item> </item>
<item>
<widget class="QCheckBox" name="zeroCopyCheckBox">
<property name="toolTip">
<string>Must be selected before opening the device</string>
</property>
<property name="text">
<string>Zero-copy</string>
</property>
</widget>
</item>
<item> <item>
<spacer name="formatSpacer"> <spacer name="formatSpacer">
<property name="orientation"> <property name="orientation">
+22 -5
View File
@@ -259,9 +259,25 @@ extern "C"
_In_ int channel_index); _In_ int channel_index);
/* /*
* Select the native capture format. This may be called before or during * Select driver zero-copy frame delivery.
* capture. A running stream reports GVFG_EVENT_FORMAT_CHANGE_BEGIN while *
* the new format is applied. * Call after gvfg_create() and before gvfg_open_channel(). The default is
* disabled. Once a device is open the mode cannot be changed. In zero-copy
* mode, gvfg_read_frame() returns driver-owned memory and every successful
* read must be paired with gvfg_release_frame().
*/
GVFG_API gvfg_status_t gvfg_set_zero_copy_enabled(
_In_ gvfg_handle handle,
_In_ int enabled);
GVFG_API gvfg_status_t gvfg_get_zero_copy_enabled(
_In_ gvfg_handle handle,
_Out_ int *out_enabled);
/*
* Select the native capture format. Call after opening the device and
* before gvfg_start(), or after gvfg_stop(). A running stream rejects the
* change with GVFG_ESTATE.
* *
* Currently the SDK implements this through a temporary hardware register; * Currently the SDK implements this through a temporary hardware register;
* applications must use this API so the backend can move to an IOCTL later. * applications must use this API so the backend can move to an IOCTL later.
@@ -307,8 +323,9 @@ extern "C"
* - GVFG_ETIMEOUT if no frame is ready before timeout_ms expires. * - GVFG_ETIMEOUT if no frame is ready before timeout_ms expires.
* - GVFG_EIO for driver/backend failures. * - GVFG_EIO for driver/backend failures.
* *
* The returned data pointer is owned by the SDK and remains valid until * In copy mode the returned data pointer is owned by the SDK. In zero-copy
* gvfg_release_frame() is called. A handle may hold only one frame at a time. * mode it points to driver-owned memory. In both modes it remains valid
* until gvfg_release_frame() is called, and a handle may hold only one frame.
*/ */
GVFG_API gvfg_status_t gvfg_read_frame( GVFG_API gvfg_status_t gvfg_read_frame(
_In_ gvfg_handle handle, _In_ gvfg_handle handle,
@@ -178,11 +178,18 @@ namespace gvfg::internal
pcies2mm_status_t PcieS2mmCaptureSession::close() pcies2mm_status_t PcieS2mmCaptureSession::close()
{ {
stop_stream(); pcies2mm_status_t result = stop_stream();
stop_event_monitoring(); stop_event_monitoring();
if (zero_copy_enabled_ && device_ != INVALID_HANDLE_VALUE)
{
const pcies2mm_status_t disableStatus = set_zero_copy_enabled(false);
if (result == PCIES2MM_OK)
result = disableStatus;
}
close_handles(); close_handles();
opened_ = false; opened_ = false;
configured_ = false; configured_ = false;
zero_copy_enabled_ = false;
signal_presence_known_.store(false, std::memory_order_release); signal_presence_known_.store(false, std::memory_order_release);
signal_present_.store(false, std::memory_order_release); signal_present_.store(false, std::memory_order_release);
signal_probe_active_.store(false, std::memory_order_release); signal_probe_active_.store(false, std::memory_order_release);
@@ -190,7 +197,7 @@ namespace gvfg::internal
base_path_.clear(); base_path_.clear();
friendly_name_.clear(); friendly_name_.clear();
reset_stats(stats_, PCIES2MM_STREAM_STOPPED); reset_stats(stats_, PCIES2MM_STREAM_STOPPED);
return PCIES2MM_OK; return result;
} }
void PcieS2mmCaptureSession::close_handles() void PcieS2mmCaptureSession::close_handles()
@@ -218,6 +225,38 @@ namespace gvfg::internal
return PCIES2MM_OK; return PCIES2MM_OK;
} }
pcies2mm_status_t PcieS2mmCaptureSession::set_zero_copy_enabled(bool enabled)
{
if (!opened_ || device_ == INVALID_HANDLE_VALUE)
return PCIES2MM_ESTATE;
if (running_)
return PCIES2MM_ESTATE;
if (zero_copy_enabled_ == enabled)
return PCIES2MM_OK;
if (!enabled)
{
// Even while the stream is nominally stopped, an earlier failed
// teardown may have left a driver frame in the ring. No disable
// IOCTL is allowed until the acquire thread is joined and every
// outstanding frame has been returned successfully.
stop_event_monitoring();
const pcies2mm_status_t releaseStatus = release_all_zero_copy_frames(true);
if (releaseStatus != PCIES2MM_OK)
return releaseStatus;
}
ULONG channel = active_channel();
DWORD bytesReturned = 0;
const DWORD ioctl = enabled ? IOCTL_GIGA_ENABLE_FRAME_ZEROCOPY
: IOCTL_GIGA_DISABLE_FRAME_ZEROCOPY;
if (!DeviceIoControl(device_, ioctl, &channel, sizeof(channel), nullptr, 0,
&bytesReturned, nullptr))
return fail(PCIES2MM_EIO, enabled ? "enable zero copy" : "disable zero copy");
zero_copy_enabled_ = enabled;
return PCIES2MM_OK;
}
pcies2mm_status_t PcieS2mmCaptureSession::set_video_format(pcies2mm_pixel_format_t format) pcies2mm_status_t PcieS2mmCaptureSession::set_video_format(pcies2mm_pixel_format_t format)
{ {
if (!opened_) if (!opened_)
@@ -246,12 +285,12 @@ namespace gvfg::internal
out.channel = channel_; out.channel = channel_;
uint32_t modeLocked = 0; uint32_t modeLocked = 0;
uint32_t timingLocked = 0; uint32_t rxTLocked = 0;
if (!read_reg(video_base() + VIDEO_RX_MODE_LOCKED_OFFSET, modeLocked) || if (!read_reg(video_base() + VIDEO_RX_MODE_LOCKED_OFFSET, modeLocked) ||
!read_reg(video_base() + VIDEO_RX_T_LOCKED_OFFSET, timingLocked)) !read_reg(video_base() + VIDEO_RX_T_LOCKED_OFFSET, rxTLocked))
return fail(PCIES2MM_EIO, "read signal lock registers"); return fail(PCIES2MM_EIO, "read signal lock registers");
const bool signalPresent = modeLocked == 1u && timingLocked == 1u; const bool signalPresent = modeLocked == 1u && rxTLocked == 1u;
signal_presence_known_.store(true, std::memory_order_release); signal_presence_known_.store(true, std::memory_order_release);
signal_present_.store(signalPresent, std::memory_order_release); signal_present_.store(signalPresent, std::memory_order_release);
if (!signalPresent) if (!signalPresent)
@@ -343,15 +382,22 @@ namespace gvfg::internal
if (running_) if (running_)
return PCIES2MM_OK; return PCIES2MM_OK;
// Do not overwrite a retained token from an earlier failed release.
// Retry it before rebuilding the ring or restarting DMA.
const pcies2mm_status_t pendingReleaseStatus = release_all_zero_copy_frames(true);
if (pendingReleaseStatus != PCIES2MM_OK)
return pendingReleaseStatus;
const size_t bytes = frame_size_bytes(); const size_t bytes = frame_size_bytes();
if (bytes == 0 || bytes > (std::numeric_limits<DWORD>::max)()) if (bytes == 0 || bytes > (std::numeric_limits<DWORD>::max)())
return fail(PCIES2MM_EINVAL, "frame_size_bytes", ERROR_INVALID_PARAMETER); return fail(PCIES2MM_EINVAL, "frame_size_bytes", ERROR_INVALID_PARAMETER);
{ {
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(mutex_);
frame_ring_.assign(stream_desc_.buffer_count, FrameSlot{}); frame_ring_.assign(zero_copy_enabled_ ? 1u : stream_desc_.buffer_count, FrameSlot{});
for (FrameSlot &slot : frame_ring_) for (FrameSlot &slot : frame_ring_)
slot.data.assign(bytes, 0); if (!zero_copy_enabled_)
slot.data.assign(bytes, 0);
next_write_slot_ = 0; next_write_slot_ = 0;
active_delivery_slot_ = static_cast<size_t>(-1); active_delivery_slot_ = static_cast<size_t>(-1);
active_delivery_started_ = {}; active_delivery_started_ = {};
@@ -365,8 +411,11 @@ namespace gvfg::internal
} }
const uint32_t channel = active_channel(); const uint32_t channel = active_channel();
if (!monitoring_.load(std::memory_order_acquire)) // stop_stream() joins the event thread so no zero-copy acquire can race
return fail(PCIES2MM_ESTATE, "event monitoring is not active", ERROR_INVALID_STATE); // teardown. Recreate event monitoring when the same open handle starts
// another stream.
if (!monitoring_.load(std::memory_order_acquire) && !start_event_monitoring())
return fail(PCIES2MM_EIO, "start event monitoring");
// Events are edge-triggered, so a source connected before registration // Events are edge-triggered, so a source connected before registration
// may not produce a plug-in event. Probe DMA using the register layout, // may not produce a plug-in event. Probe DMA using the register layout,
// but do not report connected until a full frame is received. // but do not report connected until a full frame is received.
@@ -390,7 +439,10 @@ namespace gvfg::internal
pcies2mm_status_t PcieS2mmCaptureSession::stop_stream() pcies2mm_status_t PcieS2mmCaptureSession::stop_stream()
{ {
if (!running_) if (!running_)
return PCIES2MM_OK; {
stop_event_monitoring();
return release_all_zero_copy_frames(true);
}
running_ = false; running_ = false;
capture_active_ = false; capture_active_ = false;
@@ -401,6 +453,13 @@ namespace gvfg::internal
if (device_ != INVALID_HANDLE_VALUE) if (device_ != INVALID_HANDLE_VALUE)
stop_video(active_channel()); stop_video(active_channel());
// The event thread owns acquire/publish. Join it before examining the
// ring; otherwise an acquire can complete immediately after the ring
// was drained and leave a driver frame outstanding.
stop_event_monitoring();
const pcies2mm_status_t releaseStatus = release_all_zero_copy_frames(true);
if (dma_event_) if (dma_event_)
SetEvent(dma_event_); SetEvent(dma_event_);
frame_cv_.notify_all(); frame_cv_.notify_all();
@@ -416,7 +475,7 @@ namespace gvfg::internal
} }
frame_cv_.notify_all(); frame_cv_.notify_all();
data_cv_.notify_all(); data_cv_.notify_all();
return PCIES2MM_OK; return releaseStatus;
} }
pcies2mm_status_t PcieS2mmCaptureSession::wait_frame(uint32_t timeoutMs, pcies2mm_frame_t &out) pcies2mm_status_t PcieS2mmCaptureSession::wait_frame(uint32_t timeoutMs, pcies2mm_frame_t &out)
@@ -520,7 +579,7 @@ namespace gvfg::internal
delivered_sequence_ = slot.sequence; delivered_sequence_ = slot.sequence;
++stats_.frames_delivered; ++stats_.frames_delivered;
out.data = slot.data.data(); out.data = slot.zero_copy ? slot.external_data : slot.data.data();
out.data_size_bytes = slot.bytes; out.data_size_bytes = slot.bytes;
out.frame_id = delivered_sequence_; out.frame_id = delivered_sequence_;
out.width = stream_desc_.width; out.width = stream_desc_.width;
@@ -532,12 +591,13 @@ namespace gvfg::internal
pcies2mm_status_t PcieS2mmCaptureSession::release_frame(const pcies2mm_frame_t &frame) pcies2mm_status_t PcieS2mmCaptureSession::release_frame(const pcies2mm_frame_t &frame)
{ {
std::lock_guard<std::mutex> lock(mutex_); std::unique_lock<std::mutex> lock(mutex_);
if (active_delivery_slot_ >= frame_ring_.size()) if (active_delivery_slot_ >= frame_ring_.size())
return PCIES2MM_ESTATE; return PCIES2MM_ESTATE;
const FrameSlot &slot = frame_ring_[active_delivery_slot_]; const FrameSlot &slot = frame_ring_[active_delivery_slot_];
if (frame.data != slot.data.data() || const uint8_t *expectedData = slot.zero_copy ? slot.external_data : slot.data.data();
if (frame.data != expectedData ||
frame.data_size_bytes != slot.bytes || frame.data_size_bytes != slot.bytes ||
frame.frame_id != delivered_sequence_ || frame.frame_id != delivered_sequence_ ||
frame.width != stream_desc_.width || frame.width != stream_desc_.width ||
@@ -546,7 +606,16 @@ namespace gvfg::internal
frame.bit_depth != stream_bit_depth_) frame.bit_depth != stream_bit_depth_)
return PCIES2MM_EINVAL; return PCIES2MM_EINVAL;
frame_ring_[active_delivery_slot_].in_use = false; if (slot.zero_copy && !release_zero_copy_frame(active_channel()))
{
const DWORD err = GetLastError();
lock.unlock();
return fail(PCIES2MM_EIO, "release zero-copy frame", err);
}
FrameSlot &releasedSlot = frame_ring_[active_delivery_slot_];
releasedSlot.in_use = false;
releasedSlot.external_data = nullptr;
releasedSlot.zero_copy = false;
const double heldMs = active_delivery_started_.time_since_epoch().count() != 0 const double heldMs = active_delivery_started_.time_since_epoch().count() != 0
? std::chrono::duration<double, std::milli>( ? std::chrono::duration<double, std::milli>(
std::chrono::steady_clock::now() - active_delivery_started_).count() std::chrono::steady_clock::now() - active_delivery_started_).count()
@@ -659,6 +728,63 @@ namespace gvfg::internal
nullptr) != FALSE; nullptr) != FALSE;
} }
bool PcieS2mmCaptureSession::acquire_zero_copy_frame(uint32_t channelIndex,
const uint8_t *&outData) const
{
ULONG input[2] = {channelIndex, (std::numeric_limits<ULONG>::max)()};
void *buffer = nullptr;
DWORD bytesReturned = 0;
const BOOL ok = DeviceIoControl(device_,
IOCTL_GIGA_ACQUIRE_VIDEO_FRAME_ZEROCOPY,
input,
sizeof(input),
&buffer,
sizeof(buffer),
&bytesReturned,
nullptr);
if (!ok || !buffer)
return false;
outData = static_cast<const uint8_t *>(buffer);
return true;
}
bool PcieS2mmCaptureSession::release_zero_copy_frame(uint32_t channelIndex) const
{
ULONG channel = channelIndex;
DWORD bytesReturned = 0;
return DeviceIoControl(device_,
IOCTL_GIGA_RELEASE_VIDEO_FRAME,
&channel,
sizeof(channel),
nullptr,
0,
&bytesReturned,
nullptr) != FALSE;
}
pcies2mm_status_t PcieS2mmCaptureSession::release_all_zero_copy_frames(bool includeInUse)
{
std::unique_lock<std::mutex> lock(mutex_);
for (FrameSlot &slot : frame_ring_)
{
if (!slot.zero_copy || !slot.external_data || (!includeInUse && slot.in_use))
continue;
if (!release_zero_copy_frame(active_channel()))
{
const DWORD err = GetLastError();
lock.unlock();
return fail(PCIES2MM_EIO, "release pending zero-copy frame", err);
}
slot.external_data = nullptr;
slot.zero_copy = false;
slot.ready = false;
slot.in_use = false;
}
return PCIES2MM_OK;
}
pcies2mm_status_t PcieS2mmCaptureSession::debug_write_register(uint32_t offset, pcies2mm_status_t PcieS2mmCaptureSession::debug_write_register(uint32_t offset,
uint32_t value) const uint32_t value) const
{ {
@@ -951,12 +1077,19 @@ namespace gvfg::internal
{ {
for (const FrameSlot &slot : frame_ring_) for (const FrameSlot &slot : frame_ring_)
{ {
if (!slot.in_use) if (!slot.in_use && (!zero_copy_enabled_ || !slot.ready))
return true; return true;
} }
return false; return false;
}; };
if (zero_copy_enabled_ && !hasWritableSlot())
{
if (pending_events_ > 0)
--pending_events_;
return;
}
while (running_ && !hasWritableSlot()) while (running_ && !hasWritableSlot())
data_cv_.wait_for(lock, std::chrono::seconds(1)); data_cv_.wait_for(lock, std::chrono::seconds(1));
if (!running_) if (!running_)
@@ -973,7 +1106,7 @@ namespace gvfg::internal
break; break;
} }
} }
if (slotIndex == frame_ring_.size()) if (!zero_copy_enabled_ && slotIndex == frame_ring_.size())
{ {
uint64_t oldestSequence = (std::numeric_limits<uint64_t>::max)(); uint64_t oldestSequence = (std::numeric_limits<uint64_t>::max)();
for (size_t i = 0; i < frame_ring_.size(); ++i) for (size_t i = 0; i < frame_ring_.size(); ++i)
@@ -1021,9 +1154,14 @@ namespace gvfg::internal
} }
slot.ready = false; slot.ready = false;
slot.in_use = true; slot.in_use = true;
if (slot.data.size() < bytes) slot.external_data = nullptr;
slot.data.resize(bytes); slot.zero_copy = zero_copy_enabled_;
slotData = slot.data.data(); if (!zero_copy_enabled_)
{
if (slot.data.size() < bytes)
slot.data.resize(bytes);
slotData = slot.data.data();
}
next_write_slot_ = (slotIndex + 1) % frame_ring_.size(); next_write_slot_ = (slotIndex + 1) % frame_ring_.size();
} }
@@ -1032,14 +1170,21 @@ namespace gvfg::internal
constexpr uint32_t frameIndex = (std::numeric_limits<uint32_t>::max)(); constexpr uint32_t frameIndex = (std::numeric_limits<uint32_t>::max)();
const auto getFrameStarted = std::chrono::steady_clock::now(); const auto getFrameStarted = std::chrono::steady_clock::now();
const int ret = get_frame(channel, frameIndex, slotData, bytes); const uint8_t *zeroCopyData = nullptr;
const int ret = zero_copy_enabled_
? (acquire_zero_copy_frame(channel, zeroCopyData) ? static_cast<int>(bytes) : -1)
: get_frame(channel, frameIndex, slotData, bytes);
const double getFrameMs = std::chrono::duration<double, std::milli>( const double getFrameMs = std::chrono::duration<double, std::milli>(
std::chrono::steady_clock::now() - getFrameStarted).count(); std::chrono::steady_clock::now() - getFrameStarted).count();
if (getFrameMs >= 10.0) if (getFrameMs >= 10.0)
PCIES2MM_LOG("slow GET_FRAME: frame_index=0x%08X elapsed_ms=%.3f ret=%d", PCIES2MM_LOG("slow GET_FRAME: frame_index=0x%08X elapsed_ms=%.3f ret=%d",
frameIndex, getFrameMs, ret); frameIndex, getFrameMs, ret);
if (!running_) if (!running_)
{
if (zero_copy_enabled_ && zeroCopyData)
release_zero_copy_frame(channel);
return; return;
}
if (ret < 0 || static_cast<DWORD>(ret) != bytes) if (ret < 0 || static_cast<DWORD>(ret) != bytes)
{ {
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(mutex_);
@@ -1058,6 +1203,13 @@ namespace gvfg::internal
return; return;
} }
if (zero_copy_enabled_)
{
std::lock_guard<std::mutex> lock(mutex_);
if (slotIndex < frame_ring_.size())
frame_ring_[slotIndex].external_data = zeroCopyData;
}
if (signal_probe_active_.exchange(false, std::memory_order_acq_rel)) if (signal_probe_active_.exchange(false, std::memory_order_acq_rel))
{ {
signal_present_.store(true, std::memory_order_release); signal_present_.store(true, std::memory_order_release);
@@ -1123,6 +1275,12 @@ namespace gvfg::internal
signal_presence_known_.store(true, std::memory_order_release); signal_presence_known_.store(true, std::memory_order_release);
stop_video(channel); stop_video(channel);
capture_active_ = false; capture_active_ = false;
// A ready frame is driver-owned even if the application has not read
// it yet. Return it before invalidating the ready queue. A caller-held
// frame remains valid until its matching release_frame().
const pcies2mm_status_t releaseStatus = release_all_zero_copy_frames(false);
if (releaseStatus != PCIES2MM_OK)
PCIES2MM_ERROR_LOG("unplug could not release pending zero-copy frame");
emit_event(PCIES2MM_EVENT_PLUG_OUT); emit_event(PCIES2MM_EVENT_PLUG_OUT);
{ {
@@ -1236,6 +1394,11 @@ namespace gvfg::internal
return false; return false;
} }
// Ring replacement must not discard a ready driver pointer. This runs
// on the single event thread, so no new DMA acquire can interleave.
if (resizeRing && release_all_zero_copy_frames(false) != PCIES2MM_OK)
return false;
std::unique_lock<std::mutex> lock(mutex_); std::unique_lock<std::mutex> lock(mutex_);
const bool wasRunning = running_.load(std::memory_order_acquire); const bool wasRunning = running_.load(std::memory_order_acquire);
if (resizeRing && wasRunning) if (resizeRing && wasRunning)
@@ -1259,9 +1422,10 @@ namespace gvfg::internal
signal_metadata_valid_ = true; signal_metadata_valid_ = true;
if (resizeRing) if (resizeRing)
{ {
frame_ring_.assign(stream_desc_.buffer_count, FrameSlot{}); frame_ring_.assign(zero_copy_enabled_ ? 1u : stream_desc_.buffer_count, FrameSlot{});
for (FrameSlot &slot : frame_ring_) for (FrameSlot &slot : frame_ring_)
slot.data.assign(bytes, 0); if (!zero_copy_enabled_)
slot.data.assign(bytes, 0);
next_write_slot_ = 0; next_write_slot_ = 0;
active_delivery_slot_ = static_cast<size_t>(-1); active_delivery_slot_ = static_cast<size_t>(-1);
pending_events_ = 0; pending_events_ = 0;
@@ -29,6 +29,8 @@ namespace gvfg::internal
pcies2mm_status_t close(); pcies2mm_status_t close();
pcies2mm_status_t set_channel(uint32_t channel); pcies2mm_status_t set_channel(uint32_t channel);
pcies2mm_status_t set_zero_copy_enabled(bool enabled);
bool zero_copy_enabled() const { return zero_copy_enabled_; }
pcies2mm_status_t set_video_format(pcies2mm_pixel_format_t format); pcies2mm_status_t set_video_format(pcies2mm_pixel_format_t format);
pcies2mm_status_t get_signal_status(pcies2mm_signal_status_t &out) const; pcies2mm_status_t get_signal_status(pcies2mm_signal_status_t &out) const;
@@ -54,6 +56,9 @@ namespace gvfg::internal
bool write_reg(uint32_t offset, uint32_t value) const; bool write_reg(uint32_t offset, uint32_t value) const;
bool start_video(uint32_t channelIndex) const; bool start_video(uint32_t channelIndex) const;
bool stop_video(uint32_t channelIndex) const; bool stop_video(uint32_t channelIndex) const;
bool acquire_zero_copy_frame(uint32_t channelIndex, const uint8_t *&outData) const;
bool release_zero_copy_frame(uint32_t channelIndex) const;
pcies2mm_status_t release_all_zero_copy_frames(bool includeInUse);
bool register_event(uint32_t channelIndex, uint32_t eventType, HANDLE eventHandle); bool register_event(uint32_t channelIndex, uint32_t eventType, HANDLE eventHandle);
void unregister_event(uint32_t channelIndex, uint32_t eventType); void unregister_event(uint32_t channelIndex, uint32_t eventType);
bool create_and_register_events(uint32_t channelIndex); bool create_and_register_events(uint32_t channelIndex);
@@ -86,6 +91,8 @@ namespace gvfg::internal
struct FrameSlot struct FrameSlot
{ {
std::vector<uint8_t> data; // Frame byte storage for this ring slot. std::vector<uint8_t> data; // Frame byte storage for this ring slot.
const uint8_t *external_data = nullptr; // Driver-owned zero-copy frame.
bool zero_copy = false;
size_t bytes = 0; // Number of valid bytes read into data. size_t bytes = 0; // Number of valid bytes read into data.
uint64_t sequence = 0; // Monotonic frame sequence assigned on publish. uint64_t sequence = 0; // Monotonic frame sequence assigned on publish.
bool ready = false; // True after the data thread publishes a frame. bool ready = false; // True after the data thread publishes a frame.
@@ -106,6 +113,7 @@ namespace gvfg::internal
uint32_t channel_ = 0; uint32_t channel_ = 0;
bool opened_ = false; bool opened_ = false;
bool configured_ = false; bool configured_ = false;
bool zero_copy_enabled_ = false;
std::atomic<bool> running_{false}; std::atomic<bool> running_{false};
std::atomic<bool> monitoring_{false}; std::atomic<bool> monitoring_{false};
@@ -16,6 +16,10 @@
#define IOCTL_PCIES2MM_UNREGISTER_EVENT PCIES2MM_IOCTL(0x804, METHOD_BUFFERED, FILE_ANY_ACCESS) #define IOCTL_PCIES2MM_UNREGISTER_EVENT PCIES2MM_IOCTL(0x804, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_GIGA_VIDEO_START PCIES2MM_IOCTL(0x808, METHOD_BUFFERED, FILE_ANY_ACCESS) #define IOCTL_GIGA_VIDEO_START PCIES2MM_IOCTL(0x808, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_GIGA_VIDEO_STOP PCIES2MM_IOCTL(0x809, METHOD_BUFFERED, FILE_ANY_ACCESS) #define IOCTL_GIGA_VIDEO_STOP PCIES2MM_IOCTL(0x809, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_GIGA_RELEASE_VIDEO_FRAME PCIES2MM_IOCTL(0x80A, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_GIGA_ACQUIRE_VIDEO_FRAME_ZEROCOPY PCIES2MM_IOCTL(0x80B, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_GIGA_ENABLE_FRAME_ZEROCOPY PCIES2MM_IOCTL(0x80C, METHOD_NEITHER, FILE_ANY_ACCESS)
#define IOCTL_GIGA_DISABLE_FRAME_ZEROCOPY PCIES2MM_IOCTL(0x80D, METHOD_NEITHER, FILE_ANY_ACCESS)
enum PCIES2MM_DRIVER_EVENT_TYPE enum PCIES2MM_DRIVER_EVENT_TYPE
{ {
+42 -3
View File
@@ -228,10 +228,29 @@ struct gvfg_handle_t
return map_status(stChannel); return map_status(stChannel);
} }
if (zeroCopyRequested)
{
const pcies2mm_status_t stZeroCopy = backend->set_zero_copy_enabled(true);
if (stZeroCopy != PCIES2MM_OK)
{
recordError(pcies2mm_error_text(stZeroCopy, backend.get()));
close();
return map_status(stZeroCopy);
}
}
querySignal(); querySignal();
return GVFG_OK; return GVFG_OK;
} }
gvfg_status_t setZeroCopyEnabled(bool enabled)
{
if (backend)
return GVFG_ESTATE;
zeroCopyRequested = enabled;
return GVFG_OK;
}
gvfg_status_t start() gvfg_status_t start()
{ {
if (!backend) if (!backend)
@@ -265,9 +284,13 @@ struct gvfg_handle_t
running = false; running = false;
eventCv.notify_all(); eventCv.notify_all();
releaseHeldFrameForStop(); releaseHeldFrameForStop();
if (backend) if (!backend)
backend->stop_stream(); return GVFG_OK;
return GVFG_OK;
const pcies2mm_status_t st = backend->stop_stream();
if (st != PCIES2MM_OK)
recordError(pcies2mm_error_text(st, backend.get()));
return map_status(st);
} }
void close() void close()
@@ -734,6 +757,7 @@ struct gvfg_handle_t
std::unique_ptr<gvfg::internal::PcieS2mmCaptureSession> backend; std::unique_ptr<gvfg::internal::PcieS2mmCaptureSession> backend;
int currentIndex = -1; int currentIndex = -1;
uint32_t selectedChannel = GVFG_CHANNEL_0; uint32_t selectedChannel = GVFG_CHANNEL_0;
bool zeroCopyRequested = false;
uint32_t width = 0; uint32_t width = 0;
uint32_t height = 0; uint32_t height = 0;
@@ -815,6 +839,21 @@ extern "C"
return handle->open(device_index, channel_index); return handle->open(device_index, channel_index);
} }
gvfg_status_t gvfg_set_zero_copy_enabled(gvfg_handle handle, int enabled)
{
if (!handle || (enabled != 0 && enabled != 1))
return GVFG_EINVAL;
return handle->setZeroCopyEnabled(enabled != 0);
}
gvfg_status_t gvfg_get_zero_copy_enabled(gvfg_handle handle, int *out_enabled)
{
if (!handle || !out_enabled)
return GVFG_EINVAL;
*out_enabled = handle->zeroCopyRequested ? 1 : 0;
return GVFG_OK;
}
gvfg_status_t gvfg_start(gvfg_handle handle) gvfg_status_t gvfg_start(gvfg_handle handle)
{ {
if (!handle) if (!handle)