Simplify capture to direct frame delivery

- support only the new vfg100_0821c driver; legacy driver behavior, done-index handling, and SDK frame-ring compatibility are intentionally unsupported
- replace the backend frame ring, slots, pending indices, and DMA acquisition worker with direct wait-event then GetFrame/Acquire delivery
- keep one copy buffer or one driver-owned zero-copy frame outstanding per channel and synchronize read completion during stop, format change, and teardown
- remove obsolete lost_frames, FRAME_LOSS events, dropped-frame counters, sample warnings, and related public/internal documentation
- remove periodic diagnostic register polling and let the event thread handle only format and plug state changes
- add separate GetFrame total, DMA event wait, driver IOCTL, and SDK processing timing statistics to the diagnostic status view
- clear stale DMA events before video start, preserve exact Win32 errors, and retry only documented transient not-ready/busy conditions within the caller timeout
- delete the obsolete DMA ring data-flow diagram and update architecture/API documentation for the direct frame path

Validation: user build exposed the Windows max macro conflict; corrected time_point::max with the macro-safe form. Final validation used static searches and git diff --check; no additional build was run.
This commit is contained in:
Mark.lin
2026-08-21 17:52:22 +08:00
parent 1574e90b49
commit ca19b4f276
14 changed files with 454 additions and 735 deletions
+8 -3
View File
@@ -5,6 +5,11 @@
這個 repo 以 GVFG 為 source of truth。其他專案應該透過 public header、 這個 repo 以 GVFG 為 source of truth。其他專案應該透過 public header、
import library、runtime DLL 來使用 GVFG,不要直接把 GVFG source 編進去。 import library、runtime DLL 來使用 GVFG,不要直接把 GVFG source 編進去。
## Driver compatibility
此開發版本僅支援新 driver `vfg100_0821c`。舊版 driver、舊 IOCTL、
done-index 與舊 SDK frame-ring 流程不在相容範圍內。
## 內容 ## 內容
- `sdk/gvfg`:GVFG customer C API、internal debug API、PCIES2MM backend。 - `sdk/gvfg`:GVFG customer C API、internal debug API、PCIES2MM backend。
@@ -85,7 +90,7 @@ gvfg_open_channel(handle, device_index, GVFG_CHANNEL_0);
```c ```c
uint32_t events = GVFG_EVENT_MASK_SIGNAL_CONNECTED | uint32_t events = GVFG_EVENT_MASK_SIGNAL_CONNECTED |
GVFG_EVENT_MASK_SIGNAL_DISCONNECTED | GVFG_EVENT_MASK_SIGNAL_DISCONNECTED |
GVFG_EVENT_MASK_FRAME_LOSS; GVFG_EVENT_MASK_FORMAT_CHANGE_BEGIN;
gvfg_set_channel_event_mask(handle, GVFG_CHANNEL_0, events); gvfg_set_channel_event_mask(handle, GVFG_CHANNEL_0, events);
gvfg_open_channel(handle, device_index, GVFG_CHANNEL_0); gvfg_open_channel(handle, device_index, GVFG_CHANNEL_0);
``` ```
@@ -101,7 +106,7 @@ SDK 會在 open 時 enable zero-copy,並在 close/destroy 時 disable。
### 同一裝置雙 channel ### 同一裝置雙 channel
同一個 `gvfg_handle` 可對同一個 device index 開啟 CH0、CH1。SDK 只建立一個 同一個 `gvfg_handle` 可對同一個 device index 開啟 CH0、CH1。SDK 只建立一個
Windows device handle;兩個 channel 各自保有 capture thread、event、frame ring Windows device handle;兩個 channel 各自保有 event、單一 frame buffer
以及 held-frame/zero-copy ownership: 以及 held-frame/zero-copy ownership:
```c ```c
@@ -121,7 +126,7 @@ gvfg_stop(handle); /* stop both channels */
runtime API 都明確要求 `channel_index`;不再保留隱含 selected-channel 的舊 API。 runtime API 都明確要求 `channel_index`;不再保留隱含 selected-channel 的舊 API。
`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 等資訊只存在 internal diagnostic tool。
## Consumer Layout ## Consumer Layout
+2 -2
View File
@@ -36,7 +36,7 @@
<mxCell id="workers" parent="backendLayer" style="rounded=1;whiteSpace=wrap;html=1;arcSize=8;fillColor=#ffffff;strokeColor=#d6b656;fontSize=14;" value="single capture thread&lt;br&gt;WaitForMultipleObjects: DMA / format / plug events" vertex="1"> <mxCell id="workers" parent="backendLayer" style="rounded=1;whiteSpace=wrap;html=1;arcSize=8;fillColor=#ffffff;strokeColor=#d6b656;fontSize=14;" value="single capture thread&lt;br&gt;WaitForMultipleObjects: DMA / format / plug events" vertex="1">
<mxGeometry height="52" width="280" x="510" y="173" as="geometry" /> <mxGeometry height="52" width="280" x="510" y="173" as="geometry" />
</mxCell> </mxCell>
<mxCell id="pcies2mmSession" parent="backendLayer" style="rounded=1;whiteSpace=wrap;html=1;arcSize=8;fillColor=#ffffff;strokeColor=#d6b656;fontSize=14;" value="sdk/gvfg/src/backend/pcies2mm/pcies2mm_capture_session.cpp&lt;br&gt;internal session / workers / frame ring" vertex="1"> <mxCell id="pcies2mmSession" parent="backendLayer" style="rounded=1;whiteSpace=wrap;html=1;arcSize=8;fillColor=#ffffff;strokeColor=#d6b656;fontSize=14;" value="sdk/gvfg/src/backend/pcies2mm/pcies2mm_capture_session.cpp&lt;br&gt;internal session / events / direct frame read" vertex="1">
<mxGeometry height="58" width="340" x="420" y="65" as="geometry" /> <mxGeometry height="58" width="340" x="420" y="65" as="geometry" />
</mxCell> </mxCell>
<mxCell id="e6" edge="1" parent="backendLayer" source="pcies2mmSession" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;endArrow=block;strokeWidth=2;" target="backendTypes" value="uses"> <mxCell id="e6" edge="1" parent="backendLayer" source="pcies2mmSession" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;endArrow=block;strokeWidth=2;" target="backendTypes" value="uses">
@@ -56,7 +56,7 @@
<mxCell id="facade" parent="1" style="rounded=1;whiteSpace=wrap;html=1;arcSize=8;fillColor=#ffffff;strokeColor=#82b366;fontSize=14;" value="sdk/gvfg/src/gvfg_capture.cpp&lt;br&gt;session facade / lifecycle / pull API" vertex="1"> <mxCell id="facade" parent="1" style="rounded=1;whiteSpace=wrap;html=1;arcSize=8;fillColor=#ffffff;strokeColor=#82b366;fontSize=14;" value="sdk/gvfg/src/gvfg_capture.cpp&lt;br&gt;session facade / lifecycle / pull API" vertex="1">
<mxGeometry height="58" width="340" x="470" y="385" as="geometry" /> <mxGeometry height="58" width="340" x="470" y="385" as="geometry" />
</mxCell> </mxCell>
<mxCell id="ring" parent="1" style="rounded=1;whiteSpace=wrap;html=1;arcSize=8;fillColor=#ffffff;strokeColor=#d6b656;fontSize=14;" value="frame_ring_&lt;br&gt;ready / in_use / sequence" vertex="1"> <mxCell id="ring" parent="1" style="rounded=1;whiteSpace=wrap;html=1;arcSize=8;fillColor=#ffffff;strokeColor=#d6b656;fontSize=14;" value="single frame buffer&lt;br&gt;read / held / release" vertex="1">
<mxGeometry height="58" width="270" x="880" y="610" as="geometry" /> <mxGeometry height="58" width="270" x="880" y="610" as="geometry" />
</mxCell> </mxCell>
<mxCell id="eventFlow" parent="1" style="rounded=1;whiteSpace=wrap;html=1;arcSize=8;fillColor=#ffffff;strokeColor=#d6b656;fontSize=13;" value="Public event flow&lt;br&gt;driver format IRQ -&gt; FORMAT_CHANGE_BEGIN&lt;br&gt;first new frame published -&gt; STREAM_READY" vertex="1"> <mxCell id="eventFlow" parent="1" style="rounded=1;whiteSpace=wrap;html=1;arcSize=8;fillColor=#ffffff;strokeColor=#d6b656;fontSize=13;" value="Public event flow&lt;br&gt;driver format IRQ -&gt; FORMAT_CHANGE_BEGIN&lt;br&gt;first new frame published -&gt; STREAM_READY" vertex="1">
+2 -3
View File
@@ -183,8 +183,8 @@ Zero-copy mode 的 driver lifecycle 由 SDK 管理:open 時 enable,每次成
- `gvfg_get_channel_signal_status(handle, channel, &status)`:查詢指定 channel 的連線、尺寸、 - `gvfg_get_channel_signal_status(handle, channel, &status)`:查詢指定 channel 的連線、尺寸、
原生格式與 bit depth。沒有輸入訊號是正常狀態:回傳 `GVFG_OK` 且 原生格式與 bit depth。沒有輸入訊號是正常狀態:回傳 `GVFG_OK` 且
`connected == 0`。 `connected == 0`。
- `gvfg_get_channel_runtime_info(handle, channel, &info)`:取得指定 channel 已交付 frame 數、SDK 能確定的 - `gvfg_get_channel_runtime_info(handle, channel, &info)`:取得指定 channel 已交付 frame 數與
`lost_frames` 與依 read 間隔估算的 `capture_fps`。start 時統計值重設。 依 read 間隔估算的 `capture_fps`。start 時統計值重設。
### Event ### Event
@@ -197,7 +197,6 @@ Zero-copy mode 的 driver lifecycle 由 SDK 管理:open 時 enable,每次成
| `GVFG_EVENT_SIGNAL_DISCONNECTED` | 停止使用目前影像內容 | | `GVFG_EVENT_SIGNAL_DISCONNECTED` | 停止使用目前影像內容 |
| `GVFG_EVENT_FORMAT_CHANGE_BEGIN` | 暫停使用依賴舊尺寸/格式的資源 | | `GVFG_EVENT_FORMAT_CHANGE_BEGIN` | 暫停使用依賴舊尺寸/格式的資源 |
| `GVFG_EVENT_STREAM_READY` | 第一個完整 frame 已就緒,可依新格式重建資源 | | `GVFG_EVENT_STREAM_READY` | 第一個完整 frame 已就緒,可依新格式重建資源 |
| `GVFG_EVENT_FRAME_LOSS` | 記錄錄影內容可能不完整;`event.count` 是本次已知 loss 數量 |
呼叫前應將 `gvfg_event_t` 清零並設定 `struct_size = sizeof(gvfg_event_t)`。 呼叫前應將 `gvfg_event_t` 清零並設定 `struct_size = sizeof(gvfg_event_t)`。
事件是狀態通知,不取代 `gvfg_get_channel_signal_status()`;需要完整 metadata 時應重新查詢。 事件是狀態通知,不取代 `gvfg_get_channel_signal_status()`;需要完整 metadata 時應重新查詢。
+3 -6
View File
@@ -77,7 +77,6 @@ register 與 DMA 實作不屬於本文件。
| ------------------ | ---------- | ------------------------------------------------------- | | ------------------ | ---------- | ------------------------------------------------------- |
| `capture_fps` | `double` | 依 `gvfg_read_channel_frame()` 成功交付時間估算並平滑化的 FPS;尚無足夠 frame 時為 0 | | `capture_fps` | `double` | 依 `gvfg_read_channel_frame()` 成功交付時間估算並平滑化的 FPS;尚無足夠 frame 時為 0 |
| `delivered_frames` | `uint64_t` | 此次 running session 中成功交付給 caller 的 frame 數 | | `delivered_frames` | `uint64_t` | 此次 running session 中成功交付給 caller 的 frame 數 |
| `lost_frames` | `uint64_t` | SDK 能確定在交付前遺失的 frame 數;目前包含 capture FIFO 覆蓋 |
### 1.8 `gvfg_frame_t` ### 1.8 `gvfg_frame_t`
@@ -124,11 +123,9 @@ Caller 不得修改任何欄位再 release。SDK 會將完整 token 與目前 he
| `GVFG_EVENT_SIGNAL_DISCONNECTED` | 2 | 輸入訊號中斷 | | `GVFG_EVENT_SIGNAL_DISCONNECTED` | 2 | 輸入訊號中斷 |
| `GVFG_EVENT_STREAM_READY` | 3 | 啟動、重新接線或格式恢復後,第一個完整 frame 已就緒 | | `GVFG_EVENT_STREAM_READY` | 3 | 啟動、重新接線或格式恢復後,第一個完整 frame 已就緒 |
| `GVFG_EVENT_FORMAT_CHANGE_BEGIN` | 4 | 輸入格式正在改變,應暫停使用舊格式資源 | | `GVFG_EVENT_FORMAT_CHANGE_BEGIN` | 4 | 輸入格式正在改變,應暫停使用舊格式資源 |
| `GVFG_EVENT_FRAME_LOSS` | 5 | SDK 確定一個或多個 frame 在交付前遺失 |
`gvfg_event_t` 是 `gvfg_poll_channel_event()` 的輸出。呼叫前將結構清零並把 `gvfg_event_t` 是 `gvfg_poll_channel_event()` 的輸出。呼叫前將結構清零並把
`struct_size` 設為 `sizeof(gvfg_event_t)`。`type` 是上述事件型別;只有 `struct_size` 設為 `sizeof(gvfg_event_t)`。`type` 是上述事件型別。
`GVFG_EVENT_FRAME_LOSS` 使用 `count`,表示這筆事件合併的已知 loss 數量。
`gvfg_event_mask_t` 提供對應的 `GVFG_EVENT_MASK_*` bit,可在 channel open 前用 `gvfg_event_mask_t` 提供對應的 `GVFG_EVENT_MASK_*` bit,可在 channel open 前用
`gvfg_set_channel_event_mask()` 組合。預設為 `GVFG_EVENT_MASK_ALL`;mask 為 0 `gvfg_set_channel_event_mask()` 組合。預設為 `GVFG_EVENT_MASK_ALL`;mask 為 0
@@ -252,7 +249,7 @@ gvfg_status_t gvfg_read_channel_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。兩者的 Copy mode 回傳 SDK 的單一 frame buffer;zero-copy mode 回傳 driver-owned buffer。兩者的
pointer 都只保證有效到對應的 `gvfg_release_channel_frame()`。 pointer 都只保證有效到對應的 `gvfg_release_channel_frame()`。
### 1.19 `gvfg_release_channel_frame` ### 1.19 `gvfg_release_channel_frame`
@@ -265,7 +262,7 @@ gvfg_status_t gvfg_release_channel_frame(gvfg_handle handle,
- `handle`:取得該 frame 的同一個 handle。 - `handle`:取得該 frame 的同一個 handle。
- `frame`:`gvfg_read_channel_frame()` 原封不動回傳的完整 descriptor。 - `frame`:`gvfg_read_channel_frame()` 原封不動回傳的完整 descriptor。
- `GVFG_OK`:成功歸還 frame;copy slot 可再次使用,或 zero-copy frame 已歸還 driver。 - `GVFG_OK`:成功歸還 frame;copy buffer 可再次使用,或 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 狀態不正確。
-52
View File
@@ -1,52 +0,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 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="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_channel_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_channel_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="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_channel_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_channel_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="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="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>
</mxfile>
+12 -24
View File
@@ -3,7 +3,7 @@
## Driver IOCTL DLL 邊界 ## Driver IOCTL DLL 邊界
`gvfg.dll` 保留裝置列舉、`CreateFile`/`CloseHandle`、channel session、event、 `gvfg.dll` 保留裝置列舉、`CreateFile`/`CloseHandle`、channel session、event、
thread 與 frame ring。Driver IOCTL code、request layout 及所有 event thread 與單一 frame buffer。Driver IOCTL code、request layout 及所有
`DeviceIoControl` 呼叫集中在內部 `giga_ioctl.dll`;GVFG 僅呼叫其具名 C API, `DeviceIoControl` 呼叫集中在內部 `giga_ioctl.dll`;GVFG 僅呼叫其具名 C API,
失敗時沿用 `GetLastError()`。`giga_ioctl.h` 是內部相依,不屬於客戶公開 API。 失敗時沿用 `GetLastError()`。`giga_ioctl.h` 是內部相依,不屬於客戶公開 API。
@@ -70,7 +70,7 @@ Created/Closed -> Opened -> Running -> Opened -> Destroyed
由 caller 序列化;若日後要宣告完整 thread-safe,必須先補足 open/start/stop/destroy 由 caller 序列化;若日後要宣告完整 thread-safe,必須先補足 open/start/stop/destroy
彼此的同步與 handle lifetime 保護。 彼此的同步與 handle lifetime 保護。
## 4. DMA 與 frame ring ## 4. DMA 與單一 frame buffer
Copy mode 資料路徑: Copy mode 資料路徑:
@@ -78,28 +78,17 @@ Copy mode 資料路徑:
source frame source frame
-> hardware/driver DMA buffer -> hardware/driver DMA buffer
-> DMA complete event -> DMA complete event
-> capture worker 收到 DMA complete event -> 呼叫 gvfg_read_channel_frame() 的 thread 等待 DMA complete event
-> IOCTL_GET_FRAME(frameIndex=0xFFFFFFFF) 複製到 SDK ring free slot -> IOCTL_GET_FRAME(frameIndex=0xFFFFFFFF) 直接複製到 SDK 的單一 buffer
-> slot ready + sequence 更新 + frame_cv notify -> facade 回傳指向該 buffer 的 gvfg_frame_t
-> wait_frame 選取 ready frame
-> facade 回傳指向 slot.data 的 gvfg_frame_t
-> customer/preview/conversion -> customer/preview/conversion
-> release_frame -> release_frame
-> slot 回到 free -> buffer 可供下一次 read 使用
``` ```
Backend stream descriptor 目前配置 3 個 SDK slots。Slot 邏輯狀態: Backend 不做 frame queue、slot 切換或 done-index 查詢。每個 channel 同一時間只允許
一個 read,且成功取得的 frame 必須 release 後才能讀下一張。SDK 不把 driver DMA
```text buffer 數或 done-index 演算法暴露為客戶契約。
Free ready=false, in_use=false
Writing ready=false, in_use=true
Ready ready=true, in_use=false
Delivered ready=false, in_use=true
```
ring 滿時的行為是即時擷取的 drop/backpressure policy,不是 lossless queue;應透過
internal stats 觀察 `frames_dropped`。不要將 slot 數、driver DMA buffer 數或 done-index
演算法暴露為客戶契約。
Zero-copy mode 資料路徑: Zero-copy mode 資料路徑:
@@ -116,10 +105,9 @@ gvfg_create
-> close/destroy 時 IOCTL_GIGA_DISABLE_FRAME_ZEROCOPY(channel) -> close/destroy 時 IOCTL_GIGA_DISABLE_FRAME_ZEROCOPY(channel)
``` ```
Zero-copy 同時只允許一張 outstanding frame。若下一個 DMA event 到達時 caller 尚未 Zero-copy 同時只允許一張 outstanding frame。Caller 必須 release 目前的
release,backend 只會跳過該次 acquire,不會覆寫仍由 caller 使用的 driver pointer。 driver pointer,才能再次 read。SDK 不根據 read 間隔推算 frame loss;driver 目前也
這種情況不計入 `frames_dropped`,也不送出 `GVFG_EVENT_FRAME_LOSS`;driver 目前沒有 沒有提供可用來確認實際遺失 frame 的 sequence/drop counter。
提供可用來確認實際遺失 frame 的 sequence/drop counter。
## 5. Frame token 與 ABI ## 5. Frame token 與 ABI
+63 -24
View File
@@ -93,10 +93,6 @@ namespace
return QStringLiteral("streaming"); return QStringLiteral("streaming");
} }
QString appHoldingText(uint64_t value)
{
return value == UINT64_MAX ? QStringLiteral("none") : QStringLiteral("slot %1").arg(valueOrDash(value));
}
#endif #endif
QString frameText(bool valid, int width, int height, const char *pixelFormat, int bitDepth) QString frameText(bool valid, int width, int height, const char *pixelFormat, int bitDepth)
@@ -148,8 +144,6 @@ namespace
return QStringLiteral("STREAM_READY"); return QStringLiteral("STREAM_READY");
case GVFG_EVENT_FORMAT_CHANGE_BEGIN: case GVFG_EVENT_FORMAT_CHANGE_BEGIN:
return QStringLiteral("FORMAT_CHANGE_BEGIN"); return QStringLiteral("FORMAT_CHANGE_BEGIN");
case GVFG_EVENT_FRAME_LOSS:
return QStringLiteral("FRAME_LOSS");
default: default:
return QStringLiteral("UNKNOWN"); return QStringLiteral("UNKNOWN");
} }
@@ -451,12 +445,13 @@ void MainWindow::startCapture()
previewCallMaximumMs_.store(0.0, std::memory_order_relaxed); previewCallMaximumMs_.store(0.0, std::memory_order_relaxed);
previewCallWindowMaximumMs_.store(0.0, std::memory_order_relaxed); previewCallWindowMaximumMs_.store(0.0, std::memory_order_relaxed);
previewCallSamples_.store(0, std::memory_order_relaxed); previewCallSamples_.store(0, std::memory_order_relaxed);
getFrameAverageMs_.store(0.0, std::memory_order_relaxed);
getFrameMaximumMs_.store(0.0, std::memory_order_relaxed);
getFrameWindowMaximumMs_.store(0.0, std::memory_order_relaxed);
getFrameSamples_.store(0, std::memory_order_relaxed);
#if GVFG_INTERNAL_DIAGNOSTICS #if GVFG_INTERNAL_DIAGNOSTICS
haveDebugBaseline_ = false; haveDebugBaseline_ = false;
lastDebugDmaErrors_ = 0; lastDebugDmaErrors_ = 0;
lastDebugDroppedFrames_ = 0;
pendingDroppedFrames_ = 0;
lastDroppedWarningMs_ = 0;
#endif #endif
captureStop_.store(false, std::memory_order_release); captureStop_.store(false, std::memory_order_release);
captureRunning_ = true; captureRunning_ = true;
@@ -536,10 +531,7 @@ void MainWindow::processPendingEvents()
while (gvfg_poll_channel_event(handle_, selectedChannel_, &event, 0) == GVFG_OK) while (gvfg_poll_channel_event(handle_, selectedChannel_, &event, 0) == GVFG_OK)
{ {
const auto eventType = static_cast<gvfg_event_type_t>(event.type); const auto eventType = static_cast<gvfg_event_type_t>(event.type);
if (eventType == GVFG_EVENT_FRAME_LOSS) appendLog(QStringLiteral("EVENT %1").arg(eventTypeText(eventType)));
appendLog(QStringLiteral("WARNING dropped frames +%1").arg(event.count));
else
appendLog(QStringLiteral("EVENT %1").arg(eventTypeText(eventType)));
if (eventType == GVFG_EVENT_SIGNAL_CONNECTED || if (eventType == GVFG_EVENT_SIGNAL_CONNECTED ||
eventType == GVFG_EVENT_SIGNAL_DISCONNECTED || eventType == GVFG_EVENT_SIGNAL_DISCONNECTED ||
@@ -618,6 +610,10 @@ void MainWindow::updateSignalStatus(bool queryHardware)
const double previewCallMaximumMs = previewCallMaximumMs_.load(std::memory_order_relaxed); const double previewCallMaximumMs = previewCallMaximumMs_.load(std::memory_order_relaxed);
const double previewCallWindowMaximumMs = previewCallWindowMaximumMs_.load(std::memory_order_relaxed); const double previewCallWindowMaximumMs = previewCallWindowMaximumMs_.load(std::memory_order_relaxed);
const uint64_t previewCallSamples = previewCallSamples_.load(std::memory_order_relaxed); const uint64_t previewCallSamples = previewCallSamples_.load(std::memory_order_relaxed);
const double getFrameAverageMs = getFrameAverageMs_.load(std::memory_order_relaxed);
const double getFrameMaximumMs = getFrameMaximumMs_.load(std::memory_order_relaxed);
const double getFrameWindowMaximumMs = getFrameWindowMaximumMs_.load(std::memory_order_relaxed);
const uint64_t getFrameSamples = getFrameSamples_.load(std::memory_order_relaxed);
QStringList statusLines; QStringList statusLines;
statusLines << (signal.connected statusLines << (signal.connected
? QStringLiteral("Input | CH%1 | Connected | %2") ? QStringLiteral("Input | CH%1 | Connected | %2")
@@ -650,26 +646,42 @@ void MainWindow::updateSignalStatus(bool queryHardware)
.arg(backendStats.get_frame_timing_max_us, 0, 'f', 3) .arg(backendStats.get_frame_timing_max_us, 0, 'f', 3)
.arg(static_cast<qulonglong>(backendStats.get_frame_timing_samples)) .arg(static_cast<qulonglong>(backendStats.get_frame_timing_samples))
: QStringLiteral("Driver GetFrame | measuring")); : QStringLiteral("Driver GetFrame | measuring"));
statusLines << (getFrameSamples > 0
? QStringLiteral("GetFrame total | avg=%1 max300=%2 max=%3 ms/frame samples=%4")
.arg(getFrameAverageMs, 0, 'f', 3)
.arg(getFrameWindowMaximumMs, 0, 'f', 3)
.arg(getFrameMaximumMs, 0, 'f', 3)
.arg(static_cast<qulonglong>(getFrameSamples))
: QStringLiteral("GetFrame total | measuring"));
statusLines << (haveBackendStats && backendStats.event_wait_timing_samples >= 300
? QStringLiteral("Event Wait | avg=%1 max300=%2 max=%3 ms/frame samples=%4")
.arg(backendStats.event_wait_timing_average_us / 1000.0, 0, 'f', 3)
.arg(backendStats.event_wait_timing_max300_us / 1000.0, 0, 'f', 3)
.arg(backendStats.event_wait_timing_max_us / 1000.0, 0, 'f', 3)
.arg(static_cast<qulonglong>(backendStats.event_wait_timing_samples))
: QStringLiteral("Event Wait | measuring"));
statusLines << (haveBackendStats && backendStats.sdk_processing_timing_samples >= 300
? QStringLiteral("SDK Processing | avg=%1 max300=%2 max=%3 us/frame samples=%4")
.arg(backendStats.sdk_processing_timing_average_us, 0, 'f', 3)
.arg(backendStats.sdk_processing_timing_max300_us, 0, 'f', 3)
.arg(backendStats.sdk_processing_timing_max_us, 0, 'f', 3)
.arg(static_cast<qulonglong>(backendStats.sdk_processing_timing_samples))
: QStringLiteral("SDK Processing | measuring"));
#if GVFG_INTERNAL_DIAGNOSTICS #if GVFG_INTERNAL_DIAGNOSTICS
statusLines << (haveBackendStats statusLines << (haveBackendStats
? QStringLiteral("Capture| status=%1 pending_irqs=%2 dma_errors=%3 no_frame_waits=%4") ? QStringLiteral("Capture| status=%1 dma_errors=%2 no_frame_waits=%3")
.arg(captureStatusText(backendStats, signal.connected != 0)) .arg(captureStatusText(backendStats, signal.connected != 0))
.arg(backendStats.backend_pending_events)
.arg(static_cast<qulonglong>(backendStats.backend_dma_errors)) .arg(static_cast<qulonglong>(backendStats.backend_dma_errors))
.arg(static_cast<qulonglong>(backendStats.backend_wait_timeouts)) .arg(static_cast<qulonglong>(backendStats.backend_wait_timeouts))
: QStringLiteral("Capture| unavailable")); : QStringLiteral("Capture| unavailable"));
if (haveBackendStats) if (haveBackendStats)
{ {
statusLines << QStringLiteral("Flow | irqs=%1 captured=%2 app_read=%3 dropped=%4") statusLines << QStringLiteral("Flow | irqs=%1 captured=%2 app_read=%3")
.arg(static_cast<qulonglong>(backendStats.backend_interrupt_count)) .arg(static_cast<qulonglong>(backendStats.backend_interrupt_count))
.arg(valueOrDash(backendStats.backend_frames_captured)) .arg(valueOrDash(backendStats.backend_frames_captured))
.arg(valueOrDash(backendStats.backend_frames_delivered)) .arg(valueOrDash(backendStats.backend_frames_delivered));
.arg(valueOrDash(backendStats.backend_frames_dropped)); statusLines << QStringLiteral("Frame | newest/read=%1/%2")
statusLines << QStringLiteral("Buffer | app_holding=%1 slots=%2 next_write=%3 frame_id newest/read=%4/%5") .arg(valueOrDash(backendStats.backend_latest_sequence),
.arg(appHoldingText(backendStats.backend_active_delivery_slot),
valueOrDash(backendStats.backend_ring_size),
valueOrDash(backendStats.backend_next_write_slot),
valueOrDash(backendStats.backend_latest_sequence),
valueOrDash(backendStats.backend_delivered_sequence)); valueOrDash(backendStats.backend_delivered_sequence));
} }
@@ -689,7 +701,6 @@ void MainWindow::updateSignalStatus(bool queryHardware)
} }
lastDebugDmaErrors_ = backendStats.backend_dma_errors; lastDebugDmaErrors_ = backendStats.backend_dma_errors;
lastDebugDroppedFrames_ = backendStats.backend_frames_dropped;
haveDebugBaseline_ = true; haveDebugBaseline_ = true;
} }
@@ -866,6 +877,7 @@ void MainWindow::appendLog(const QString &message)
void MainWindow::captureReadLoop() void MainWindow::captureReadLoop()
{ {
constexpr uint64_t kGetFrameTimingSampleFrames = 300;
constexpr uint64_t kPreviewTimingWarmupFrames = 30; constexpr uint64_t kPreviewTimingWarmupFrames = 30;
constexpr uint64_t kPreviewTimingSampleFrames = 300; constexpr uint64_t kPreviewTimingSampleFrames = 300;
@@ -875,12 +887,39 @@ void MainWindow::captureReadLoop()
uint64_t previewTimingSampleCount = 0; uint64_t previewTimingSampleCount = 0;
double previewTimingTotalMs = 0.0; double previewTimingTotalMs = 0.0;
double previewTimingMaximumMs = 0.0; double previewTimingMaximumMs = 0.0;
uint64_t getFrameTimingSampleCount = 0;
double getFrameTimingMaximumMs = 0.0;
while (!captureStop_.load(std::memory_order_acquire)) while (!captureStop_.load(std::memory_order_acquire))
{ {
gvfg_frame_t frame{}; gvfg_frame_t frame{};
const auto getFrameStart = std::chrono::steady_clock::now();
const gvfg_status_t st = gvfg_read_channel_frame(handle_, selectedChannel_, &frame, 200); const gvfg_status_t st = gvfg_read_channel_frame(handle_, selectedChannel_, &frame, 200);
const double getFrameElapsedMs =
std::chrono::duration<double, std::milli>(
std::chrono::steady_clock::now() - getFrameStart).count();
if (st == GVFG_OK) if (st == GVFG_OK)
{ {
++getFrameTimingSampleCount;
getFrameTimingMaximumMs = (std::max)(getFrameTimingMaximumMs, getFrameElapsedMs);
const uint64_t totalSamples = getFrameSamples_.fetch_add(1, std::memory_order_relaxed) + 1;
const double previousAverage = getFrameAverageMs_.load(std::memory_order_relaxed);
getFrameAverageMs_.store(
previousAverage + (getFrameElapsedMs - previousAverage) / static_cast<double>(totalSamples),
std::memory_order_relaxed);
double observedMaximum = getFrameMaximumMs_.load(std::memory_order_relaxed);
while (getFrameElapsedMs > observedMaximum &&
!getFrameMaximumMs_.compare_exchange_weak(
observedMaximum, getFrameElapsedMs, std::memory_order_relaxed))
{
}
if (totalSamples <= kGetFrameTimingSampleFrames)
getFrameWindowMaximumMs_.store(getFrameTimingMaximumMs, std::memory_order_relaxed);
if (getFrameTimingSampleCount >= kGetFrameTimingSampleFrames)
{
getFrameWindowMaximumMs_.store(getFrameTimingMaximumMs, std::memory_order_relaxed);
getFrameTimingSampleCount = 0;
getFrameTimingMaximumMs = 0.0;
}
consecutiveTimeouts = 0; consecutiveTimeouts = 0;
if (!frameAvailable_.exchange(true, std::memory_order_acq_rel)) if (!frameAvailable_.exchange(true, std::memory_order_acq_rel))
{ {
+4 -3
View File
@@ -75,6 +75,10 @@ private:
std::atomic<bool> captureStop_{false}; std::atomic<bool> captureStop_{false};
std::atomic<bool> frameAvailable_{false}; std::atomic<bool> frameAvailable_{false};
std::thread captureThread_; std::thread captureThread_;
std::atomic<double> getFrameAverageMs_{0.0};
std::atomic<double> getFrameMaximumMs_{0.0};
std::atomic<double> getFrameWindowMaximumMs_{0.0};
std::atomic<uint64_t> getFrameSamples_{0};
uint64_t previewFailureCount_ = 0; uint64_t previewFailureCount_ = 0;
std::atomic<double> previewCallAverageMs_{0.0}; std::atomic<double> previewCallAverageMs_{0.0};
std::atomic<double> previewCallMaximumMs_{0.0}; std::atomic<double> previewCallMaximumMs_{0.0};
@@ -88,9 +92,6 @@ private:
#if GVFG_INTERNAL_DIAGNOSTICS #if GVFG_INTERNAL_DIAGNOSTICS
QString lastLoggedBackendError_; QString lastLoggedBackendError_;
uint64_t lastDebugDmaErrors_ = 0; uint64_t lastDebugDmaErrors_ = 0;
uint64_t lastDebugDroppedFrames_ = 0;
uint64_t pendingDroppedFrames_ = 0;
qint64 lastDroppedWarningMs_ = 0;
bool haveDebugBaseline_ = false; bool haveDebugBaseline_ = false;
#endif #endif
QFile logFile_; QFile logFile_;
+4 -10
View File
@@ -128,8 +128,6 @@ extern "C"
{ {
double capture_fps; /* Runtime FPS measured from frames returned by gvfg_read_channel_frame(). */ double capture_fps; /* Runtime FPS measured from frames returned by gvfg_read_channel_frame(). */
uint64_t delivered_frames; /* Number of frames returned by gvfg_read_channel_frame(). */ uint64_t delivered_frames; /* Number of frames returned by gvfg_read_channel_frame(). */
/* Frames known by the SDK to have been lost before application delivery. */
uint64_t lost_frames;
} gvfg_runtime_info_t; } gvfg_runtime_info_t;
typedef struct typedef struct
@@ -171,9 +169,7 @@ extern "C"
/* First complete frame after stream start, plug-in, or format recovery is ready. */ /* First complete frame after stream start, plug-in, or format recovery is ready. */
GVFG_EVENT_STREAM_READY = 3, GVFG_EVENT_STREAM_READY = 3,
/* Input format is changing; pause use of resources created for the old format. */ /* Input format is changing; pause use of resources created for the old format. */
GVFG_EVENT_FORMAT_CHANGE_BEGIN = 4, GVFG_EVENT_FORMAT_CHANGE_BEGIN = 4
/* One or more frames were known to be lost before application delivery. */
GVFG_EVENT_FRAME_LOSS = 5
} gvfg_event_type_t; } gvfg_event_type_t;
typedef enum typedef enum
@@ -183,16 +179,14 @@ extern "C"
GVFG_EVENT_MASK_SIGNAL_DISCONNECTED = 1u << 1, GVFG_EVENT_MASK_SIGNAL_DISCONNECTED = 1u << 1,
GVFG_EVENT_MASK_STREAM_READY = 1u << 2, GVFG_EVENT_MASK_STREAM_READY = 1u << 2,
GVFG_EVENT_MASK_FORMAT_CHANGE_BEGIN = 1u << 3, GVFG_EVENT_MASK_FORMAT_CHANGE_BEGIN = 1u << 3,
GVFG_EVENT_MASK_FRAME_LOSS = 1u << 4, GVFG_EVENT_MASK_ALL = (1u << 4) - 1u
GVFG_EVENT_MASK_ALL = (1u << 5) - 1u
} gvfg_event_mask_t; } gvfg_event_mask_t;
typedef struct typedef struct
{ {
/* Set to sizeof(gvfg_event_t) before calling gvfg_poll_channel_event(). */ /* Set to sizeof(gvfg_event_t) before calling gvfg_poll_channel_event(). */
uint32_t struct_size; uint32_t struct_size;
int32_t type; /* gvfg_event_type_t value. */ int32_t type; /* gvfg_event_type_t value. */
uint64_t count; /* Loss count for GVFG_EVENT_FRAME_LOSS; otherwise zero. */
uint64_t reserved[4]; uint64_t reserved[4];
} gvfg_event_t; } gvfg_event_t;
@@ -287,7 +281,7 @@ extern "C"
/* /*
* A handle may open both channels of the same device. The first call opens * A handle may open both channels of the same device. The first call opens
* the Windows device; the second channel shares that device connection and * the Windows device; the second channel shares that device connection and
* owns independent capture, event, frame-ring, and zero-copy state. * owns independent capture, event, frame, and zero-copy state.
*/ */
/* /*
+8 -5
View File
@@ -37,25 +37,28 @@ typedef struct
int backend_state; int backend_state;
uint64_t backend_frames_captured; uint64_t backend_frames_captured;
uint64_t backend_frames_delivered; uint64_t backend_frames_delivered;
uint64_t backend_frames_dropped;
uint64_t backend_dma_errors; uint64_t backend_dma_errors;
uint64_t backend_interrupt_count; uint64_t backend_interrupt_count;
uint64_t backend_wait_timeouts; uint64_t backend_wait_timeouts;
int backend_running; int backend_running;
int backend_capture_active; int backend_capture_active;
uint32_t backend_pending_events;
uint64_t backend_latest_sequence; uint64_t backend_latest_sequence;
uint64_t backend_delivered_sequence; uint64_t backend_delivered_sequence;
uint64_t backend_active_delivery_slot; /* UINT64_MAX when no frame is held by the caller. */
uint64_t backend_next_write_slot;
uint64_t backend_ring_size;
int get_frame_zero_copy; int get_frame_zero_copy;
uint64_t get_frame_timing_samples; uint64_t get_frame_timing_samples;
double get_frame_timing_average_us; double get_frame_timing_average_us;
double get_frame_timing_max300_us; double get_frame_timing_max300_us;
double get_frame_timing_max_us; double get_frame_timing_max_us;
uint64_t event_wait_timing_samples;
double event_wait_timing_average_us;
double event_wait_timing_max300_us;
double event_wait_timing_max_us;
uint64_t sdk_processing_timing_samples;
double sdk_processing_timing_average_us;
double sdk_processing_timing_max300_us;
double sdk_processing_timing_max_us;
} gvfg_debug_backend_stats_t; } gvfg_debug_backend_stats_t;
/* /*
@@ -63,7 +63,6 @@ typedef struct pcies2mm_stream_stats_t
pcies2mm_stream_state_t state; pcies2mm_stream_state_t state;
uint64_t frames_captured; uint64_t frames_captured;
uint64_t frames_delivered; uint64_t frames_delivered;
uint64_t frames_dropped;
uint64_t dma_errors; uint64_t dma_errors;
uint64_t interrupt_count; uint64_t interrupt_count;
} pcies2mm_stream_stats_t; } pcies2mm_stream_stats_t;
@@ -72,17 +71,21 @@ typedef struct pcies2mm_debug_state_t
{ {
int running; int running;
int capture_active; int capture_active;
uint32_t pending_events;
uint64_t latest_sequence; uint64_t latest_sequence;
uint64_t delivered_sequence; uint64_t delivered_sequence;
uint64_t active_delivery_slot;
uint64_t next_write_slot;
uint64_t ring_size;
int get_frame_zero_copy; int get_frame_zero_copy;
uint64_t get_frame_timing_samples; uint64_t get_frame_timing_samples;
double get_frame_timing_average_us; double get_frame_timing_average_us;
double get_frame_timing_max300_us; double get_frame_timing_max300_us;
double get_frame_timing_max_us; double get_frame_timing_max_us;
uint64_t event_wait_timing_samples;
double event_wait_timing_average_us;
double event_wait_timing_max300_us;
double event_wait_timing_max_us;
uint64_t sdk_processing_timing_samples;
double sdk_processing_timing_average_us;
double sdk_processing_timing_max300_us;
double sdk_processing_timing_max_us;
} pcies2mm_debug_state_t; } pcies2mm_debug_state_t;
typedef enum pcies2mm_event_type_t typedef enum pcies2mm_event_type_t
@@ -90,8 +93,7 @@ typedef enum pcies2mm_event_type_t
PCIES2MM_EVENT_PLUG_IN = 1, PCIES2MM_EVENT_PLUG_IN = 1,
PCIES2MM_EVENT_PLUG_OUT = 2, PCIES2MM_EVENT_PLUG_OUT = 2,
PCIES2MM_EVENT_STREAM_READY = 3, PCIES2MM_EVENT_STREAM_READY = 3,
PCIES2MM_EVENT_FORMAT_CHANGE_BEGIN = 4, PCIES2MM_EVENT_FORMAT_CHANGE_BEGIN = 4
PCIES2MM_EVENT_FRAME_LOSS = 5
} pcies2mm_event_type_t; } pcies2mm_event_type_t;
enum enum
@@ -100,12 +102,10 @@ enum
PCIES2MM_EVENT_MASK_PLUG_OUT = 1u << 1, PCIES2MM_EVENT_MASK_PLUG_OUT = 1u << 1,
PCIES2MM_EVENT_MASK_STREAM_READY = 1u << 2, PCIES2MM_EVENT_MASK_STREAM_READY = 1u << 2,
PCIES2MM_EVENT_MASK_FORMAT_CHANGE_BEGIN = 1u << 3, PCIES2MM_EVENT_MASK_FORMAT_CHANGE_BEGIN = 1u << 3,
PCIES2MM_EVENT_MASK_FRAME_LOSS = 1u << 4,
PCIES2MM_EVENT_MASK_DEFAULT = PCIES2MM_EVENT_MASK_PLUG_IN | PCIES2MM_EVENT_MASK_DEFAULT = PCIES2MM_EVENT_MASK_PLUG_IN |
PCIES2MM_EVENT_MASK_PLUG_OUT | PCIES2MM_EVENT_MASK_PLUG_OUT |
PCIES2MM_EVENT_MASK_STREAM_READY | PCIES2MM_EVENT_MASK_STREAM_READY |
PCIES2MM_EVENT_MASK_FORMAT_CHANGE_BEGIN | PCIES2MM_EVENT_MASK_FORMAT_CHANGE_BEGIN
PCIES2MM_EVENT_MASK_FRAME_LOSS
}; };
typedef void (*pcies2mm_event_callback_t)(pcies2mm_event_type_t event, void *user); typedef void (*pcies2mm_event_callback_t)(pcies2mm_event_type_t event, void *user);
File diff suppressed because it is too large Load Diff
@@ -6,8 +6,8 @@
#include <windows.h> #include <windows.h>
#include <atomic> #include <atomic>
#include <condition_variable>
#include <chrono> #include <chrono>
#include <condition_variable>
#include <cstddef> #include <cstddef>
#include <cstdint> #include <cstdint>
#include <mutex> #include <mutex>
@@ -78,13 +78,10 @@ namespace gvfg::internal
int get_frame(uint32_t channelIndex, uint32_t frameIndex, uint8_t *buffer, DWORD bufferSize) const; int get_frame(uint32_t channelIndex, uint32_t frameIndex, uint8_t *buffer, DWORD bufferSize) const;
void capture_thread_proc(); void capture_thread_proc();
void handle_dma_event(uint32_t channel);
void handle_format_change_event(uint32_t channel); void handle_format_change_event(uint32_t channel);
void handle_plugin_event(uint32_t channel); void handle_plugin_event(uint32_t channel);
void handle_unplug_event(uint32_t channel); void handle_unplug_event(uint32_t channel);
bool resume_capture_from_signal(uint32_t channel); bool refresh_stream_from_registers(bool resizeBuffer);
bool refresh_stream_from_registers(bool resizeRing);
void publish_frame(size_t slotIndex, size_t bytes);
void emit_event(pcies2mm_event_type_t type) const; void emit_event(pcies2mm_event_type_t type) const;
pcies2mm_status_t fail(pcies2mm_status_t status, const char *where, DWORD winerr = GetLastError()) const; pcies2mm_status_t fail(pcies2mm_status_t status, const char *where, DWORD winerr = GetLastError()) const;
@@ -96,17 +93,9 @@ namespace gvfg::internal
uint32_t video_base() const; uint32_t video_base() const;
uint32_t video_irq_mask_bit() const; uint32_t video_irq_mask_bit() const;
size_t frame_size_bytes() const; size_t frame_size_bytes() const;
void record_get_frame_timing(double elapsedUs);
struct FrameSlot void record_event_wait_timing(double elapsedUs);
{ void record_sdk_processing_timing(double elapsedUs);
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.
uint64_t sequence = 0; // Monotonic frame sequence assigned on publish.
bool ready = false; // True after the data thread publishes a frame.
bool in_use = false; // True while writing or while the caller holds the slot.
};
std::wstring base_path_; std::wstring base_path_;
std::wstring friendly_name_; std::wstring friendly_name_;
@@ -138,16 +127,11 @@ namespace gvfg::internal
std::thread capture_thread_; std::thread capture_thread_;
mutable std::mutex mutex_; mutable std::mutex mutex_;
std::condition_variable read_finished_cv_;
mutable std::mutex event_callback_mutex_; mutable std::mutex event_callback_mutex_;
std::condition_variable frame_cv_;
std::condition_variable data_cv_;
pcies2mm_event_callback_t event_callback_ = nullptr; pcies2mm_event_callback_t event_callback_ = nullptr;
void *event_callback_user_ = nullptr; void *event_callback_user_ = nullptr;
uint32_t event_mask_filter_ = PCIES2MM_EVENT_MASK_DEFAULT; uint32_t event_mask_filter_ = PCIES2MM_EVENT_MASK_DEFAULT;
uint32_t pending_events_ = 0;
std::vector<FrameSlot> frame_ring_;
size_t next_write_slot_ = 0;
size_t active_delivery_slot_ = static_cast<size_t>(-1);
uint64_t latest_sequence_ = 0; uint64_t latest_sequence_ = 0;
uint64_t delivered_sequence_ = 0; uint64_t delivered_sequence_ = 0;
uint64_t wait_timeout_count_ = 0; uint64_t wait_timeout_count_ = 0;
@@ -157,8 +141,23 @@ namespace gvfg::internal
double get_frame_timing_window_max_us_ = 0.0; double get_frame_timing_window_max_us_ = 0.0;
double get_frame_timing_last_max300_us_ = 0.0; double get_frame_timing_last_max300_us_ = 0.0;
double get_frame_timing_lifetime_max_us_ = 0.0; double get_frame_timing_lifetime_max_us_ = 0.0;
uint64_t event_wait_timing_samples_ = 0;
uint32_t event_wait_timing_window_samples_ = 0;
double event_wait_timing_total_us_ = 0.0;
double event_wait_timing_window_max_us_ = 0.0;
double event_wait_timing_last_max300_us_ = 0.0;
double event_wait_timing_lifetime_max_us_ = 0.0;
uint64_t sdk_processing_timing_samples_ = 0;
uint32_t sdk_processing_timing_window_samples_ = 0;
double sdk_processing_timing_total_us_ = 0.0;
double sdk_processing_timing_window_max_us_ = 0.0;
double sdk_processing_timing_last_max300_us_ = 0.0;
double sdk_processing_timing_lifetime_max_us_ = 0.0;
std::vector<uint8_t> copy_buffer_;
pcies2mm_frame_t held_frame_{};
bool frame_held_ = false;
bool read_in_progress_ = false;
std::chrono::steady_clock::time_point active_delivery_started_{}; std::chrono::steady_clock::time_point active_delivery_started_{};
std::chrono::steady_clock::time_point last_delivery_started_{};
bool stream_error_ = false; bool stream_error_ = false;
pcies2mm_stream_stats_t stats_{}; pcies2mm_stream_stats_t stats_{};
mutable std::string last_error_; mutable std::string last_error_;
+10 -19
View File
@@ -31,7 +31,6 @@ namespace
static_assert(GVFG_EVENT_MASK_SIGNAL_DISCONNECTED == PCIES2MM_EVENT_MASK_PLUG_OUT); static_assert(GVFG_EVENT_MASK_SIGNAL_DISCONNECTED == PCIES2MM_EVENT_MASK_PLUG_OUT);
static_assert(GVFG_EVENT_MASK_STREAM_READY == PCIES2MM_EVENT_MASK_STREAM_READY); static_assert(GVFG_EVENT_MASK_STREAM_READY == PCIES2MM_EVENT_MASK_STREAM_READY);
static_assert(GVFG_EVENT_MASK_FORMAT_CHANGE_BEGIN == PCIES2MM_EVENT_MASK_FORMAT_CHANGE_BEGIN); static_assert(GVFG_EVENT_MASK_FORMAT_CHANGE_BEGIN == PCIES2MM_EVENT_MASK_FORMAT_CHANGE_BEGIN);
static_assert(GVFG_EVENT_MASK_FRAME_LOSS == PCIES2MM_EVENT_MASK_FRAME_LOSS);
#if INTPTR_MAX == INT64_MAX #if INTPTR_MAX == INT64_MAX
static_assert(std::is_standard_layout_v<gvfg_frame_t>); static_assert(std::is_standard_layout_v<gvfg_frame_t>);
@@ -137,8 +136,6 @@ namespace
return GVFG_EVENT_STREAM_READY; return GVFG_EVENT_STREAM_READY;
case PCIES2MM_EVENT_FORMAT_CHANGE_BEGIN: case PCIES2MM_EVENT_FORMAT_CHANGE_BEGIN:
return GVFG_EVENT_FORMAT_CHANGE_BEGIN; return GVFG_EVENT_FORMAT_CHANGE_BEGIN;
case PCIES2MM_EVENT_FRAME_LOSS:
return GVFG_EVENT_FRAME_LOSS;
default: default:
return GVFG_EVENT_UNKNOWN; return GVFG_EVENT_UNKNOWN;
} }
@@ -354,7 +351,6 @@ struct gvfg_channel_session_t
pcies2mm_debug_state_t debugState{}; pcies2mm_debug_state_t debugState{};
uint64_t waitTimeouts = 0; uint64_t waitTimeouts = 0;
backend->get_debug_stats(stats, waitTimeouts, debugState); backend->get_debug_stats(stats, waitTimeouts, debugState);
out.lost_frames = stats.frames_dropped;
return GVFG_OK; return GVFG_OK;
} }
@@ -380,19 +376,11 @@ struct gvfg_channel_session_t
const gvfg_event_type_t type = map_event_type(event); const gvfg_event_type_t type = map_event_type(event);
{ {
std::lock_guard<std::mutex> lock(eventMutex); std::lock_guard<std::mutex> lock(eventMutex);
if (type == GVFG_EVENT_FRAME_LOSS && !eventQueue.empty() &&
eventQueue.back().type == GVFG_EVENT_FRAME_LOSS)
{
++eventQueue.back().count;
eventCv.notify_one();
return;
}
if (eventQueue.size() >= 64) if (eventQueue.size() >= 64)
eventQueue.pop_front(); eventQueue.pop_front();
gvfg_event_t out{}; gvfg_event_t out{};
out.struct_size = sizeof(out); out.struct_size = sizeof(out);
out.type = type; out.type = type;
out.count = type == GVFG_EVENT_FRAME_LOSS ? 1 : 0;
eventQueue.push_back(out); eventQueue.push_back(out);
} }
eventCv.notify_one(); eventCv.notify_one();
@@ -438,14 +426,14 @@ struct gvfg_channel_session_t
} }
// configure_stream() is also used to enter event-monitoring mode. Keep // configure_stream() is also used to enter event-monitoring mode. Keep
// its inactive placeholder ring minimal; the real signal descriptor // its inactive placeholder buffer minimal; the real signal descriptor
// replaces it before DMA is enabled after reconnect. // replaces it before DMA is enabled after reconnect.
pcies2mm_stream_desc_t desc{}; pcies2mm_stream_desc_t desc{};
desc.width = configureWidth; desc.width = configureWidth;
desc.height = configureHeight; desc.height = configureHeight;
desc.pixel_format = configureFormat; desc.pixel_format = configureFormat;
desc.buffer_count = 3; desc.buffer_count = 1;
const pcies2mm_status_t st = backend->configure_stream(desc); const pcies2mm_status_t st = backend->configure_stream(desc);
if (st != PCIES2MM_OK) if (st != PCIES2MM_OK)
@@ -671,23 +659,26 @@ struct gvfg_channel_session_t
out.backend_state = static_cast<int>(stats.state); out.backend_state = static_cast<int>(stats.state);
out.backend_frames_captured = stats.frames_captured; out.backend_frames_captured = stats.frames_captured;
out.backend_frames_delivered = stats.frames_delivered; out.backend_frames_delivered = stats.frames_delivered;
out.backend_frames_dropped = stats.frames_dropped;
out.backend_dma_errors = stats.dma_errors; out.backend_dma_errors = stats.dma_errors;
out.backend_interrupt_count = stats.interrupt_count; out.backend_interrupt_count = stats.interrupt_count;
out.backend_wait_timeouts = waitTimeouts; out.backend_wait_timeouts = waitTimeouts;
out.backend_running = debugState.running; out.backend_running = debugState.running;
out.backend_capture_active = debugState.capture_active; out.backend_capture_active = debugState.capture_active;
out.backend_pending_events = debugState.pending_events;
out.backend_latest_sequence = debugState.latest_sequence; out.backend_latest_sequence = debugState.latest_sequence;
out.backend_delivered_sequence = debugState.delivered_sequence; out.backend_delivered_sequence = debugState.delivered_sequence;
out.backend_active_delivery_slot = debugState.active_delivery_slot;
out.backend_next_write_slot = debugState.next_write_slot;
out.backend_ring_size = debugState.ring_size;
out.get_frame_zero_copy = debugState.get_frame_zero_copy; out.get_frame_zero_copy = debugState.get_frame_zero_copy;
out.get_frame_timing_samples = debugState.get_frame_timing_samples; out.get_frame_timing_samples = debugState.get_frame_timing_samples;
out.get_frame_timing_average_us = debugState.get_frame_timing_average_us; out.get_frame_timing_average_us = debugState.get_frame_timing_average_us;
out.get_frame_timing_max300_us = debugState.get_frame_timing_max300_us; out.get_frame_timing_max300_us = debugState.get_frame_timing_max300_us;
out.get_frame_timing_max_us = debugState.get_frame_timing_max_us; out.get_frame_timing_max_us = debugState.get_frame_timing_max_us;
out.event_wait_timing_samples = debugState.event_wait_timing_samples;
out.event_wait_timing_average_us = debugState.event_wait_timing_average_us;
out.event_wait_timing_max300_us = debugState.event_wait_timing_max300_us;
out.event_wait_timing_max_us = debugState.event_wait_timing_max_us;
out.sdk_processing_timing_samples = debugState.sdk_processing_timing_samples;
out.sdk_processing_timing_average_us = debugState.sdk_processing_timing_average_us;
out.sdk_processing_timing_max300_us = debugState.sdk_processing_timing_max300_us;
out.sdk_processing_timing_max_us = debugState.sdk_processing_timing_max_us;
} }
return GVFG_OK; return GVFG_OK;