From 2b5e75ade9864725758d712f0f2d48c16fb9e8e5 Mon Sep 17 00:00:00 2001 From: "Mark.lin" Date: Thu, 30 Jul 2026 16:43:30 +0800 Subject: [PATCH] Simplify GVFG API and frame diagnostics --- CaptureDemo/CaptureDemo/mainwindow.cpp | 1 - .../mainwindow.cpp | 1 - .../gvfg_preview/include/gvfg_preview.h | 1 - docs/GVFG_ARCHITECTURE_OVERVIEW.drawio | 10 +- docs/GVFG_CUSTOMER_API.md | 29 ++--- docs/GVFG_DMA_RING_DATA_FLOW.drawio | 28 ++--- docs/GVFG_INTERNAL_NOTES.md | 103 +++++++++++++++- helpers/gvfg_convert/include/gvfg_convert.h | 8 +- helpers/gvfg_convert/src/gvfg_convert.cpp | 19 +-- helpers/gvfg_preview/include/gvfg_preview.h | 2 - helpers/gvfg_preview/src/gvfg_preview.cpp | 13 +- samples/gvfg_qt_preview/mainwindow.cpp | 58 +++------ samples/gvfg_qt_preview/mainwindow.h | 3 +- samples/gvfg_qt_preview/previewwindow.cpp | 2 + sdk/gvfg/include/gvfg_capture.h | 50 +++----- sdk/gvfg/include/gvfg_debug.h | 15 ++- .../backend/pcies2mm/pcies2mm_backend_types.h | 18 +-- .../pcies2mm/pcies2mm_capture_session.cpp | 27 +++-- .../pcies2mm/pcies2mm_capture_session.h | 1 + sdk/gvfg/src/gvfg_capture.cpp | 113 +++++++----------- 20 files changed, 245 insertions(+), 257 deletions(-) diff --git a/CaptureDemo/CaptureDemo/mainwindow.cpp b/CaptureDemo/CaptureDemo/mainwindow.cpp index 3f47939..f700fcb 100644 --- a/CaptureDemo/CaptureDemo/mainwindow.cpp +++ b/CaptureDemo/CaptureDemo/mainwindow.cpp @@ -461,7 +461,6 @@ void MainWindow::init() const uint64_t frameSize = (uint64_t)video_width * 4u * (uint64_t)video_height; gvfg_preview_frame_t frame{}; - frame.struct_size = sizeof(frame); frame.data = data; frame.data_size = frameSize; frame.width = video_width; diff --git a/CaptureDemo/CaptureDemo_Source_Helper_MSVC2022/mainwindow.cpp b/CaptureDemo/CaptureDemo_Source_Helper_MSVC2022/mainwindow.cpp index 3f47939..f700fcb 100644 --- a/CaptureDemo/CaptureDemo_Source_Helper_MSVC2022/mainwindow.cpp +++ b/CaptureDemo/CaptureDemo_Source_Helper_MSVC2022/mainwindow.cpp @@ -461,7 +461,6 @@ void MainWindow::init() const uint64_t frameSize = (uint64_t)video_width * 4u * (uint64_t)video_height; gvfg_preview_frame_t frame{}; - frame.struct_size = sizeof(frame); frame.data = data; frame.data_size = frameSize; frame.width = video_width; diff --git a/CaptureDemo/CaptureDemo_Source_Helper_MSVC2022/prebuilt/gvfg_preview/include/gvfg_preview.h b/CaptureDemo/CaptureDemo_Source_Helper_MSVC2022/prebuilt/gvfg_preview/include/gvfg_preview.h index f664580..841c517 100644 --- a/CaptureDemo/CaptureDemo_Source_Helper_MSVC2022/prebuilt/gvfg_preview/include/gvfg_preview.h +++ b/CaptureDemo/CaptureDemo_Source_Helper_MSVC2022/prebuilt/gvfg_preview/include/gvfg_preview.h @@ -37,7 +37,6 @@ extern "C" typedef struct { - uint32_t struct_size; /* Set to sizeof(gvfg_preview_frame_t). */ const void *data; uint64_t data_size; int width; diff --git a/docs/GVFG_ARCHITECTURE_OVERVIEW.drawio b/docs/GVFG_ARCHITECTURE_OVERVIEW.drawio index b89074d..45ac5cb 100644 --- a/docs/GVFG_ARCHITECTURE_OVERVIEW.drawio +++ b/docs/GVFG_ARCHITECTURE_OVERVIEW.drawio @@ -1,4 +1,4 @@ - + @@ -30,7 +30,7 @@ - + @@ -56,6 +56,9 @@ + + + @@ -74,6 +77,9 @@ + + + diff --git a/docs/GVFG_CUSTOMER_API.md b/docs/GVFG_CUSTOMER_API.md index 20a8c63..aa3fb3f 100644 --- a/docs/GVFG_CUSTOMER_API.md +++ b/docs/GVFG_CUSTOMER_API.md @@ -108,12 +108,10 @@ Customer-readable input signal status: 新 driver 未提供 frame-rate、SDI/HDMI lock、DDR status 或舊 FPGA validity register,因此 SDK 不再合成或公開這些欄位。 -這是可擴充的 query output,呼叫前必須設定 `struct_size`。沒有 input signal 是正常 -狀態:API 回傳 `GVFG_OK`,同時 `connected == 0`。 +沒有 input signal 是正常狀態:API 回傳 `GVFG_OK`,同時 `connected == 0`。 ```c gvfg_signal_status_t signal = {}; -signal.struct_size = sizeof(signal); gvfg_get_signal_status(h, &signal); ``` @@ -145,7 +143,6 @@ Memory layout、color metadata 與 timestamp metadata 不會繼續塞進這個 s ```c typedef struct { - uint32_t struct_size; int plane_count; const void *plane_data[GVFG_MAX_PLANES]; int plane_stride[GVFG_MAX_PLANES]; @@ -161,7 +158,7 @@ typedef struct - `gvfg_handle` 永遠保持 opaque。 - `gvfg_frame_t` 保持極小,stable release 後凍結。 -- 真正可擴充的 query output 使用 `struct_size`。 +- 第一版 API 不做跨版本 struct 相容;DLL、header 與 application 必須整套更新。 - Layout 使用 `gvfg_get_frame_layout()`。 - 未來 color metadata 使用獨立的 `gvfg_get_frame_color_info()`。 - 未來 timestamp metadata 使用獨立的 `gvfg_get_frame_timestamp_info()`。 @@ -184,15 +181,11 @@ typedef struct gvfg_convert_frame_t *gvfg_convert_frame; typedef struct { - uint32_t struct_size; int width; int height; int pixel_format; int row_bytes; - uint32_t flags; - uint32_t reserved0; uint64_t data_size; - uint64_t reserved[8]; } gvfg_convert_frame_desc_t; ``` @@ -217,9 +210,9 @@ captured source frame 自動決定大小。 Conversion 使用 BT.709 limited-range YUV to RGB,和 preview path 對齊。其他 source 或 destination format 會回傳 `GVFG_ENOTSUP`。 -### `gvfg_event_t` +### `gvfg_event_type_t` -`gvfg_poll_event()` 回傳的 capture event。 +`gvfg_poll_event()` 直接回傳 capture event 類型。 Customer event 保持 driver-neutral: @@ -302,11 +295,8 @@ gvfg_status_t gvfg_get_frame_layout(const gvfg_frame_t *frame, 回傳 `gvfg_read_frame()` 取得 frame 的 per-plane layout。 -呼叫前先初始化 `out_layout->struct_size`: - ```c gvfg_frame_layout_t layout = {}; -layout.struct_size = sizeof(layout); gvfg_get_frame_layout(&frame, &layout); ``` @@ -343,7 +333,6 @@ Snapshot buffer flow 範例: gvfg_frame_t frame = {}; if (gvfg_read_frame(h, &frame, 1000) == GVFG_OK) { gvfg_convert_frame_desc_t desc = {}; - desc.struct_size = sizeof(desc); desc.pixel_format = GVFG_CONVERT_FMT_RGB48; gvfg_convert_frame image = NULL; @@ -388,7 +377,7 @@ Release `gvfg_read_frame()` 回傳的 frame。 ```c gvfg_status_t gvfg_poll_event(gvfg_handle handle, - gvfg_event_t *out_event, + gvfg_event_type_t *out_event, uint32_t timeout_ms); ``` @@ -408,7 +397,6 @@ gvfg_status_t gvfg_get_signal_status(gvfg_handle handle, ```c gvfg_signal_status_t status = {}; -status.struct_size = sizeof(status); gvfg_get_signal_status(h, &status); ``` @@ -421,13 +409,12 @@ gvfg_status_t gvfg_get_runtime_info(gvfg_handle handle, gvfg_runtime_info_t *out_info); ``` -查詢 last read frame format、FPS、delivered frame count。Input signal metadata 請另外 -呼叫 `gvfg_get_signal_status()`;不要把可擴充的 signal struct 內嵌進 runtime struct, -避免未來欄位位移破壞 ABI。 +查詢 FPS 與 delivered frame count。Input signal metadata 請另外呼叫 +`gvfg_get_signal_status()`;每張實際交付的 frame metadata 由 `gvfg_frame_t` +提供。Last-delivered frame metadata 僅保留於 internal debug API。 ```c gvfg_runtime_info_t info = {}; -info.struct_size = sizeof(info); gvfg_get_runtime_info(h, &info); ``` diff --git a/docs/GVFG_DMA_RING_DATA_FLOW.drawio b/docs/GVFG_DMA_RING_DATA_FLOW.drawio index e809155..2798095 100644 --- a/docs/GVFG_DMA_RING_DATA_FLOW.drawio +++ b/docs/GVFG_DMA_RING_DATA_FLOW.drawio @@ -1,42 +1,42 @@ - + - + - + - + - + - + - + - + - + - + - + @@ -84,9 +84,9 @@ - - - + + + diff --git a/docs/GVFG_INTERNAL_NOTES.md b/docs/GVFG_INTERNAL_NOTES.md index 353ac90..daf5eb9 100644 --- a/docs/GVFG_INTERNAL_NOTES.md +++ b/docs/GVFG_INTERNAL_NOTES.md @@ -84,7 +84,7 @@ flowchart TD subgraph Backend["Internal Backend"] PcieS2mm["sdk/gvfg/src/backend/pcies2mm"] Ring["frame_ring_"] - Workers["single driver event + DMA worker"] + Workers["single capture thread: DMA / format / plug events"] end CustomerApp --> CaptureApi @@ -125,14 +125,77 @@ Pull mode 下,`gvfg.dll` 不擁有 application 的 public read thread。UI app 自己建立 worker thread,並在那個 thread 呼叫 `gvfg_read_frame()`。 `gvfg_start()` 在尚未確認 input signal 時仍會註冊 driver events 並啟動單一 backend -wait thread。`gvfg_read_frame()` 此時 timeout;收到 signal-connected event 後,backend -在相同 thread 重讀 width/height/payload format、resize ring,然後 enable DMA 並送出 -capture-resumed event。 +capture thread。`gvfg_read_frame()` 此時 timeout;收到 signal-connected event 後, +backend 在相同 thread 重讀 width/height/payload format、resize ring,然後 enable DMA。 +第一張完整 frame publish 後,public event queue 收到 `GVFG_EVENT_STREAM_READY`。 FPGA H/V/format registers 可能在拔除來源後保留 last-known values,因此它們只能用來 準備 DMA probe layout,不能當作 signal-present 判斷。啟動時 backend 可以在內部 enable probe DMA,以涵蓋來源早於 event registration 就已接上的情況;只有收到 driver plug-in -event 或成功取得第一張完整 frame 後,public signal status 才能回報 connected。 +event 或成功取得第一張完整 frame 後,public signal status 才能回報 connected。DMA +probe 成功不偽造 plug-in event,而是送出 `GVFG_EVENT_STREAM_READY`。 + +## Internal DMA / Ring Data Flow + +Driver DMA buffer 與 SDK `frame_ring_` 是兩層不同的 storage。Driver buffer 負責接收 +硬體 DMA;SDK ring 保存一份 user-mode copy,讓 application 能持有 frame 到 +`gvfg_release_frame()`。三個 SDK ring slots 只增加緩衝空間,每一張 frame 仍只有一次 +driver-to-SDK copy。 + +目前完整路徑: + +```text +video source produces one frame +-> hardware writes driver DMA buffer N +-> DMA complete interrupt +-> driver SetEvent(dma_event_) +-> capture_thread_proc() wakes from WaitForMultipleObjects() +-> handle_dma_event() +-> IOCTL_GET_VIDEO_DONE_INDEX +-> frameIndex = doneIndex % kDmaBufferCount (currently 16) +-> search the 3-slot SDK frame_ring_ for slot.in_use == false +-> mark slot: ready=false, in_use=true +-> IOCTL_GET_FRAME +-> one copy: driver DMA buffer N -> slot.data +-> validate bytesReturned == expected frame size +-> publish_frame() + - sequence = ++latest_sequence_ + - ready = true + - in_use = false + - frame_cv_.notify_one() +-> gvfg_read_frame() / wait_frame() wakes +-> select the newest ready sequence +-> mark delivered slot: ready=false, in_use=true +-> return gvfg_frame_t pointing at slot.data +-> preview / conversion / customer processing +-> gvfg_release_frame() +-> mark slot.in_use=false +-> data_cv_.notify_all() +-> slot becomes reusable +``` + +FrameSlot 的主要狀態: + +```text +Free : ready=false, in_use=false +Writing : ready=false, in_use=true +Ready : ready=true, in_use=false +Delivered : ready=false, in_use=true +Released : ready=false, in_use=false +``` + +同步責任: + +- `mutex_` 保護 `frame_ring_`、slot flags、sequence 與 active delivery slot。 +- `frame_cv_` 喚醒等待 ready frame 的 `wait_frame()`。 +- `data_cv_` 喚醒等待 free slot 的 capture thread。 +- `eventMutex`、`eventCv` 與 `eventQueue` 是 public capture event 的另一套同步機制, + 不保存 frame。 + +如果三個 SDK slots 全部 `in_use`,capture thread 目前會在 `data_cv_` 等待,不會主動 +從 SDK ring 丟掉 application 正在持有的 frame。硬體與 driver DMA 仍可能繼續前進, +因此等待期間的中間 frame 可能在 driver 層被覆寫或 event 被合併,形成隱性掉幀。 +這是 backpressure policy,不是 lossless queue 保證。 ## Frame 所有權 @@ -144,7 +207,7 @@ event 或成功取得第一張完整 frame 後,public signal status 才能回 - `gvfg_get_frame_layout()` 回傳 SDK-filled layout metadata:`plane_data`、 `plane_stride`、`plane_size`。目前 PCIES2MM backend 使用 width/height/format 推導 tightly packed layout。 -- 未來 color/timestamp metadata 應新增各自帶 `struct_size` 的 query output,例如 +- 未來 color/timestamp metadata 應新增獨立的 query output,例如 `gvfg_get_frame_color_info()` 與 `gvfg_get_frame_timestamp_info()`。不要預先在每個 struct 放大型 reserved array;等 metadata 類型真的很多再考慮 side data。 - `gvfg_preview.dll` 應優先吃 `gvfg_get_frame_layout()`;layout query 不可用時才 @@ -174,8 +237,36 @@ Customer event 保持 driver-neutral: ```text GVFG_EVENT_SIGNAL_CONNECTED GVFG_EVENT_SIGNAL_DISCONNECTED +GVFG_EVENT_STREAM_READY +GVFG_EVENT_FORMAT_CHANGE_BEGIN ``` +事件語意: + +- `GVFG_EVENT_SIGNAL_CONNECTED`:backend 收到真正的 driver plug-in event。 +- `GVFG_EVENT_SIGNAL_DISCONNECTED`:backend 收到真正的 driver plug-out event。 +- `GVFG_EVENT_FORMAT_CHANGE_BEGIN`:收到 driver format-change event,舊格式資源即將 + 失效;backend 自動停止舊 DMA、重讀格式並恢復 capture。 +- `GVFG_EVENT_STREAM_READY`:start、plug-in recovery 或 format recovery 後,第一張 + 完整 frame 已經 publish。Application 可讀取新 `gvfg_frame_t`,依其中的 + width/height/pixel_format/bit_depth 重建 render resources。 + +Format change 是兩階段通知: + +```text +driver format-change event +-> GVFG_EVENT_FORMAT_CHANGE_BEGIN +-> backend disables old DMA/video +-> backend refreshes width/height/payload format and resizes ring +-> backend enables capture +-> first complete new-format frame is published +-> GVFG_EVENT_STREAM_READY +``` + +Application 可在 `FORMAT_CHANGE_BEGIN` 暫停 render 或清理舊格式資源,但不應自行對同一 +session 執行 Stop/Start;backend 負責恢復。在 `STREAM_READY` 後,以新 frame metadata +重建並恢復 render。 + Video IRQ handling 是 `sdk/gvfg/src/backend/pcies2mm` 內部細節。IRQ bit numbers、 IRQ masks 與 DMA counters 不應出現在 `gvfg_capture.h`。 diff --git a/helpers/gvfg_convert/include/gvfg_convert.h b/helpers/gvfg_convert/include/gvfg_convert.h index 5a57b94..6da2cd7 100644 --- a/helpers/gvfg_convert/include/gvfg_convert.h +++ b/helpers/gvfg_convert/include/gvfg_convert.h @@ -35,15 +35,11 @@ typedef struct gvfg_convert_frame_t *gvfg_convert_frame; typedef struct { - uint32_t struct_size; /* Set to sizeof(gvfg_convert_frame_desc_t) before calling. */ int width; /* Frame width in pixels. Use 0 to size from source during conversion. */ int height; /* Frame height in pixels. Use 0 to size from source during conversion. */ int pixel_format; /* gvfg_convert_format_t destination format. */ int row_bytes; /* Destination row stride in bytes. Use 0 for helper default. */ - uint32_t flags; /* Reserved for future conversion options. Must be 0. */ - uint32_t reserved0; /* Reserved. Must be 0. */ uint64_t data_size; /* Output buffer size in bytes, filled after create/convert. */ - uint64_t reserved[8]; /* Reserved for future helper metadata. Must be ignored. */ } gvfg_convert_frame_desc_t; GVFG_CONVERT_API gvfg_status_t gvfg_convert_create_frame( @@ -59,7 +55,7 @@ GVFG_CONVERT_API gvfg_status_t gvfg_convert_frame_from_capture( GVFG_CONVERT_API gvfg_status_t gvfg_convert_get_frame_desc( _In_ gvfg_convert_frame frame, - _Inout_ gvfg_convert_frame_desc_t *out_desc); + _Out_ gvfg_convert_frame_desc_t *out_desc); GVFG_CONVERT_API gvfg_status_t gvfg_convert_get_buffer( _In_ gvfg_convert_frame frame, @@ -68,7 +64,7 @@ GVFG_CONVERT_API gvfg_status_t gvfg_convert_get_buffer( GVFG_CONVERT_API gvfg_status_t gvfg_convert_get_layout( _In_ gvfg_convert_frame frame, - _Inout_ gvfg_frame_layout_t *out_layout); + _Out_ gvfg_frame_layout_t *out_layout); GVFG_CONVERT_API const char *gvfg_convert_strerror( _In_ gvfg_status_t status); diff --git a/helpers/gvfg_convert/src/gvfg_convert.cpp b/helpers/gvfg_convert/src/gvfg_convert.cpp index cdfd646..3a6ff62 100644 --- a/helpers/gvfg_convert/src/gvfg_convert.cpp +++ b/helpers/gvfg_convert/src/gvfg_convert.cpp @@ -160,7 +160,6 @@ struct gvfg_convert_frame_t } layout = {}; - layout.struct_size = sizeof(layout); layout.plane_count = 1; layout.plane_data[0] = buffer.data(); layout.plane_stride[0] = actualStride; @@ -323,17 +322,13 @@ extern "C" return GVFG_EINVAL; *out_frame = nullptr; - if (desc->struct_size < sizeof(gvfg_convert_frame_desc_t) || - desc->flags != 0 || - desc->reserved0 != 0 || - convert_format_bytes_per_pixel(desc->pixel_format) <= 0) + if (convert_format_bytes_per_pixel(desc->pixel_format) <= 0) return GVFG_EINVAL; if (desc->width < 0 || desc->height < 0 || desc->row_bytes < 0) return GVFG_EINVAL; auto frame = std::make_unique(); frame->request = {}; - frame->request.struct_size = sizeof(gvfg_convert_frame_desc_t); frame->request.width = desc->width; frame->request.height = desc->height; frame->request.pixel_format = desc->pixel_format; @@ -374,7 +369,6 @@ extern "C" return cfg; gvfg_frame_layout_t srcLayout{}; - srcLayout.struct_size = sizeof(srcLayout); const gvfg_status_t layoutStatus = gvfg_get_frame_layout(src, &srcLayout); if (layoutStatus != GVFG_OK) return layoutStatus; @@ -395,13 +389,7 @@ extern "C" { if (!frame || !out_desc) return GVFG_EINVAL; - if (out_desc->struct_size < sizeof(gvfg_convert_frame_desc_t)) - return GVFG_EINVAL; - - const uint32_t callerSize = out_desc->struct_size; - std::memset(out_desc, 0, sizeof(*out_desc)); *out_desc = frame->desc; - out_desc->struct_size = callerSize; return GVFG_OK; } @@ -421,15 +409,10 @@ extern "C" { if (!frame || !out_layout) return GVFG_EINVAL; - if (out_layout->struct_size < sizeof(gvfg_frame_layout_t)) - return GVFG_EINVAL; if (frame->buffer.empty() || frame->layout.plane_count <= 0) return GVFG_ESTATE; - const uint32_t callerSize = out_layout->struct_size; - std::memset(out_layout, 0, sizeof(*out_layout)); *out_layout = frame->layout; - out_layout->struct_size = callerSize; return GVFG_OK; } diff --git a/helpers/gvfg_preview/include/gvfg_preview.h b/helpers/gvfg_preview/include/gvfg_preview.h index 2f96ab0..df077e7 100644 --- a/helpers/gvfg_preview/include/gvfg_preview.h +++ b/helpers/gvfg_preview/include/gvfg_preview.h @@ -43,7 +43,6 @@ typedef enum typedef struct { - uint32_t struct_size; /* Set to sizeof(gvfg_preview_frame_t). */ const void *data; uint64_t data_size; int width; @@ -74,7 +73,6 @@ typedef struct */ typedef struct { - uint32_t struct_size; /* Set to sizeof(gvfg_preview_stats_t). */ double present_fps; uint64_t presented_frames; uint64_t skipped_presents; diff --git a/helpers/gvfg_preview/src/gvfg_preview.cpp b/helpers/gvfg_preview/src/gvfg_preview.cpp index 8a1ebb7..a2ee21f 100644 --- a/helpers/gvfg_preview/src/gvfg_preview.cpp +++ b/helpers/gvfg_preview/src/gvfg_preview.cpp @@ -429,8 +429,7 @@ extern "C" { if (!handle || !frame) return GVFG_PREVIEW_EINVAL; - if (frame->struct_size < sizeof(gvfg_preview_frame_t) || - !frame->data || + if (!frame->data || frame->width <= 0 || frame->height <= 0 || frame->row_bytes <= 0) @@ -481,18 +480,12 @@ extern "C" gvfg_preview_status_t gvfg_preview_get_stats(gvfg_preview_handle handle, gvfg_preview_stats_t *out_stats) { - constexpr size_t kMinimumSize = - offsetof(gvfg_preview_stats_t, skipped_presents) + - sizeof(uint64_t); - if (!handle || !out_stats || out_stats->struct_size < kMinimumSize) + if (!handle || !out_stats) return GVFG_PREVIEW_EINVAL; - const uint32_t callerSize = out_stats->struct_size; gvfg_preview_stats_t stats{}; - stats.struct_size = sizeof(stats); handle->renderer.getStats(stats); - std::memcpy(out_stats, &stats, (callerSize < sizeof(stats)) ? callerSize : sizeof(stats)); - out_stats->struct_size = callerSize; + *out_stats = stats; return GVFG_PREVIEW_OK; } diff --git a/samples/gvfg_qt_preview/mainwindow.cpp b/samples/gvfg_qt_preview/mainwindow.cpp index cfbca2e..dd0a986 100644 --- a/samples/gvfg_qt_preview/mainwindow.cpp +++ b/samples/gvfg_qt_preview/mainwindow.cpp @@ -143,6 +143,10 @@ namespace return QStringLiteral("SIGNAL_CONNECTED"); case GVFG_EVENT_SIGNAL_DISCONNECTED: return QStringLiteral("SIGNAL_DISCONNECTED"); + case GVFG_EVENT_STREAM_READY: + return QStringLiteral("STREAM_READY"); + case GVFG_EVENT_FORMAT_CHANGE_BEGIN: + return QStringLiteral("FORMAT_CHANGE_BEGIN"); default: return QStringLiteral("UNKNOWN"); } @@ -412,19 +416,10 @@ bool MainWindow::applyPreview() return true; } -void MainWindow::updatePreviewSourceSize(const gvfg_runtime_info_t &info, - const gvfg_signal_status_t &signal) +void MainWindow::updatePreviewSourceSize(const gvfg_signal_status_t &signal) { - const auto &readFrame = info.last_frame; - if (signal.width > 0 && signal.height > 0) - { previewWindow_->setSourceSize(signal.width, signal.height); - return; - } - - if (readFrame.valid && readFrame.width > 0 && readFrame.height > 0) - previewWindow_->setSourceSize(readFrame.width, readFrame.height); } void MainWindow::updatePreviewSourceSize() @@ -432,13 +427,9 @@ void MainWindow::updatePreviewSourceSize() if (!handle_) return; - gvfg_runtime_info_t info{}; - info.struct_size = sizeof(info); gvfg_signal_status_t signal{}; - signal.struct_size = sizeof(signal); - if (gvfg_get_runtime_info(handle_, &info) == GVFG_OK && - gvfg_get_signal_status(handle_, &signal) == GVFG_OK) - updatePreviewSourceSize(info, signal); + if (gvfg_get_signal_status(handle_, &signal) == GVFG_OK) + updatePreviewSourceSize(signal); } void MainWindow::updateSignalStatus() @@ -446,30 +437,22 @@ void MainWindow::updateSignalStatus() if (!handle_) return; - gvfg_runtime_info_t info{}; - info.struct_size = sizeof(info); - if (gvfg_get_runtime_info(handle_, &info) != GVFG_OK) - return; - gvfg_signal_status_t signal{}; - signal.struct_size = sizeof(signal); if (gvfg_get_signal_status(handle_, &signal) != GVFG_OK) return; #if GVFG_INTERNAL_DIAGNOSTICS gvfg_debug_backend_stats_t backendStats{}; - backendStats.struct_size = sizeof(backendStats); const bool haveBackendStats = gvfg_debug_get_backend_stats(handle_, &backendStats) == GVFG_OK; #endif if (previewWindow_->isVisible()) - updatePreviewSourceSize(info, signal); + updatePreviewSourceSize(signal); gvfg_preview_info_t previewInfo{}; const bool previewInfoOk = previewHandle_ && gvfg_preview_get_info(previewHandle_, &previewInfo) == GVFG_PREVIEW_OK && previewInfo.active; gvfg_preview_stats_t previewStats{}; - previewStats.struct_size = sizeof(previewStats); const bool previewStatsOk = previewHandle_ && gvfg_preview_get_stats(previewHandle_, &previewStats) == GVFG_PREVIEW_OK; const QString previewFps = previewStatsOk && previewStats.present_fps > 0.0 @@ -482,6 +465,11 @@ void MainWindow::updateSignalStatus() previewInfo.pixel_format, previewInfo.bit_depth) : QStringLiteral("--"); + const QString previewState = !captureRunning_ + ? QStringLiteral("Stopped") + : previewFps == QStringLiteral("--") + ? QStringLiteral("Measuring") + : QStringLiteral("%1 FPS").arg(previewFps); QStringList statusLines; statusLines << (signal.connected ? QStringLiteral("Input | CH%1 | Connected | %2") @@ -491,10 +479,7 @@ void MainWindow::updateSignalStatus() .arg(signal.channel)); statusLines << (previewInfoOk ? QStringLiteral("Preview | %1 | %2") - .arg(previewFps == QStringLiteral("--") - ? QStringLiteral("Measuring") - : QStringLiteral("%1 FPS").arg(previewFps), - previewFrame) + .arg(previewState, previewFrame) : QStringLiteral("Preview | Inactive")); #if GVFG_INTERNAL_DIAGNOSTICS @@ -728,21 +713,13 @@ void MainWindow::captureReadLoop() while (!captureStop_.load(std::memory_order_acquire)) { - gvfg_event_t event{}; + gvfg_event_type_t event = GVFG_EVENT_UNKNOWN; while (gvfg_poll_event(handle_, &event, 0) == GVFG_OK) { - const QString type = eventTypeText(event.type); -#if GVFG_INTERNAL_DIAGNOSTICS - const uint64_t timestampNs = event.timestamp_ns; - QMetaObject::invokeMethod(this, [this, type, timestampNs]() - { appendLog(QStringLiteral("event %1 ts=%2") - .arg(type) - .arg(static_cast(timestampNs))); }, Qt::QueuedConnection); -#else + const QString type = eventTypeText(event); QMetaObject::invokeMethod(this, [this, type]() { appendLog(QStringLiteral("EVENT %1").arg(type)); }, Qt::QueuedConnection); -#endif } gvfg_frame_t frame{}; @@ -761,7 +738,6 @@ void MainWindow::captureReadLoop() if (previewHandle_) { gvfg_preview_frame_t previewFrame{}; - previewFrame.struct_size = sizeof(previewFrame); previewFrame.data = frame.data; previewFrame.data_size = frame.data_size; previewFrame.width = frame.width; @@ -785,7 +761,6 @@ void MainWindow::captureReadLoop() } gvfg_frame_layout_t layout{}; - layout.struct_size = sizeof(layout); if (gvfg_get_frame_layout(&frame, &layout) == GVFG_OK && layout.plane_count > 0 && layout.plane_data[0] && @@ -834,7 +809,6 @@ void MainWindow::captureReadLoop() (consecutiveTimeouts % 5) == 0) { gvfg_signal_status_t signal{}; - signal.struct_size = sizeof(signal); if (gvfg_get_signal_status(handle_, &signal) == GVFG_OK && signal.connected) { captureStalledLogged = true; diff --git a/samples/gvfg_qt_preview/mainwindow.h b/samples/gvfg_qt_preview/mainwindow.h index 5bcc612..87c73e0 100644 --- a/samples/gvfg_qt_preview/mainwindow.h +++ b/samples/gvfg_qt_preview/mainwindow.h @@ -44,8 +44,7 @@ private: void startCapture(); void stopCapture(); bool applyPreview(); - void updatePreviewSourceSize(const gvfg_runtime_info_t &info, - const gvfg_signal_status_t &signal); + void updatePreviewSourceSize(const gvfg_signal_status_t &signal); void updatePreviewSourceSize(); void updateSignalStatus(); void updateUiState(); diff --git a/samples/gvfg_qt_preview/previewwindow.cpp b/samples/gvfg_qt_preview/previewwindow.cpp index 1f04dc0..19e90bc 100644 --- a/samples/gvfg_qt_preview/previewwindow.cpp +++ b/samples/gvfg_qt_preview/previewwindow.cpp @@ -13,6 +13,8 @@ PreviewWindow::PreviewWindow(QWidget *parent) : QWidget(parent), ui_(new Ui::PreviewWindow) { ui_->setupUi(this); + ui_->verticalLayout->setContentsMargins(0, 0, 0, 0); + ui_->verticalLayout->setSpacing(0); ui_->previewHost->setAttribute(Qt::WA_NativeWindow, true); ui_->previewHost->setStyleSheet(QStringLiteral("background: black;")); } diff --git a/sdk/gvfg/include/gvfg_capture.h b/sdk/gvfg/include/gvfg_capture.h index 3d88ed5..2bf4f74 100644 --- a/sdk/gvfg/include/gvfg_capture.h +++ b/sdk/gvfg/include/gvfg_capture.h @@ -118,7 +118,6 @@ extern "C" typedef struct { - uint32_t struct_size; /* Set to sizeof(gvfg_signal_status_t) before calling. */ int connected; /* Non-zero while the selected channel has a valid input signal. */ int channel; /* gvfg_channel_t selected when the device was opened. */ int width; /* Signal width in pixels when connected. */ @@ -129,19 +128,8 @@ extern "C" typedef struct { - int width; /* Width of the most recent frame returned by gvfg_read_frame(). */ - int height; /* Height of the most recent frame returned by gvfg_read_frame(). */ - int bit_depth; /* Bits per color channel of the frame buffer. */ - int pixel_format; /* gvfg_pixel_format_t value: YUY2 or Y210. */ - int valid; /* Non-zero while capture is running after at least one frame read. */ - } gvfg_last_frame_info_t; - - typedef struct - { - uint32_t struct_size; /* Set to sizeof(gvfg_runtime_info_t) before calling. */ - gvfg_last_frame_info_t last_frame; /* Last frame returned by gvfg_read_frame(). */ - double capture_fps; /* Runtime FPS measured from frames returned by gvfg_read_frame(). */ - uint64_t delivered_frames; /* Number of frames returned by gvfg_read_frame(). */ + double capture_fps; /* Runtime FPS measured from frames returned by gvfg_read_frame(). */ + uint64_t delivered_frames; /* Number of frames returned by gvfg_read_frame(). */ } gvfg_runtime_info_t; typedef struct @@ -157,7 +145,6 @@ extern "C" typedef struct { - uint32_t struct_size; /* Set to sizeof(gvfg_frame_layout_t) before calling. */ int plane_count; /* Number of valid entries in plane_data/plane_stride/plane_size. */ const void *plane_data[GVFG_MAX_PLANES]; /* Plane pointers inside frame.data, valid until gvfg_release_frame(). */ int plane_stride[GVFG_MAX_PLANES]; /* Bytes from one row to the next for each plane. */ @@ -168,15 +155,13 @@ extern "C" { GVFG_EVENT_UNKNOWN = 0, GVFG_EVENT_SIGNAL_CONNECTED = 1, - GVFG_EVENT_SIGNAL_DISCONNECTED = 2 + GVFG_EVENT_SIGNAL_DISCONNECTED = 2, + /* First complete frame after stream start, plug-in, or format recovery is ready. */ + GVFG_EVENT_STREAM_READY = 3, + /* Input format is changing; pause use of resources created for the old format. */ + GVFG_EVENT_FORMAT_CHANGE_BEGIN = 4 } gvfg_event_type_t; - typedef struct - { - gvfg_event_type_t type; - uint64_t timestamp_ns; /* Monotonic timestamp for ordering; not Unix/wall-clock time. */ - } gvfg_event_t; - /* Opaque session handle created by gvfg_create() and released by gvfg_destroy(). */ typedef struct gvfg_handle_t *gvfg_handle; @@ -299,13 +284,11 @@ extern "C" * * Parameters: * - frame: Frame descriptor returned by gvfg_read_frame(). Must not be NULL. - * - out_layout: Receives plane pointers, strides, and sizes. Must not - * be NULL. Set out_layout->struct_size to sizeof(gvfg_frame_layout_t) before - * calling so future SDKs can safely extend this struct. + * - out_layout: Receives plane pointers, strides, and sizes. Must not be NULL. * * Returns: * - GVFG_OK on success. - * - GVFG_EINVAL if frame/out_layout is NULL or struct_size is too small. + * - GVFG_EINVAL if frame or out_layout is NULL. * - GVFG_ENOTSUP if the SDK cannot describe the frame layout. * * The returned plane_data pointers are owned by the SDK and remain valid only @@ -313,7 +296,7 @@ extern "C" */ GVFG_API gvfg_status_t gvfg_get_frame_layout( _In_ const gvfg_frame_t *frame, - _Inout_ gvfg_frame_layout_t *out_layout); + _Out_ gvfg_frame_layout_t *out_layout); /* * Release a frame returned by gvfg_read_frame(). @@ -349,7 +332,7 @@ extern "C" */ GVFG_API gvfg_status_t gvfg_poll_event( _In_ gvfg_handle handle, - _Out_ gvfg_event_t *out_event, + _Out_ gvfg_event_type_t *out_event, _In_ uint32_t timeout_ms); /* @@ -377,15 +360,14 @@ extern "C" * Returns: * - GVFG_OK on success. * - GVFG_EINVAL if handle or out_status is NULL. - * - GVFG_EINVAL if out_status->struct_size is too small. * - GVFG_ESTATE if no capture device is open. * * No input signal is a normal state: the function returns GVFG_OK with - * out_status->connected set to 0. Set out_status->struct_size before calling. + * out_status->connected set to 0. */ GVFG_API gvfg_status_t gvfg_get_signal_status( _In_ gvfg_handle handle, - _Inout_ gvfg_signal_status_t *out_status); + _Out_ gvfg_signal_status_t *out_status); /* * Query runtime frame-delivery diagnostics. @@ -397,16 +379,14 @@ extern "C" * Returns: * - GVFG_OK on success. * - GVFG_EINVAL if handle or out_info is NULL. - * - GVFG_EINVAL if out_info->struct_size is too small. * * The result includes the latest delivered frame, SDK-measured capture FPS, * and the number of frames delivered by the SDK. Query current input signal - * metadata separately with gvfg_get_signal_status(). Set out_info->struct_size - * before calling. + * metadata separately with gvfg_get_signal_status(). */ GVFG_API gvfg_status_t gvfg_get_runtime_info( _In_ gvfg_handle handle, - _Inout_ gvfg_runtime_info_t *out_info); + _Out_ gvfg_runtime_info_t *out_info); /* Convert a gvfg_pixel_format_t value to a static English format name. */ GVFG_API const char *gvfg_pixel_format_name( diff --git a/sdk/gvfg/include/gvfg_debug.h b/sdk/gvfg/include/gvfg_debug.h index ce78783..03f7935 100644 --- a/sdk/gvfg/include/gvfg_debug.h +++ b/sdk/gvfg/include/gvfg_debug.h @@ -17,17 +17,22 @@ extern "C" { typedef struct { - uint32_t struct_size; /* Set to sizeof(gvfg_debug_backend_stats_t) before calling. */ + int width; /* Width of the most recent frame returned by gvfg_read_frame(). */ + int height; /* Height of the most recent frame returned by gvfg_read_frame(). */ + int bit_depth; /* Bits per color channel of the frame buffer. */ + int pixel_format; /* gvfg_pixel_format_t value: YUY2 or Y210. */ + int valid; /* Non-zero after a frame is returned during the current running session. */ +} gvfg_debug_last_frame_info_t; + +typedef struct +{ int sdk_running; int frame_held; uint32_t event_queue_depth; double runtime_fps; uint64_t frames_returned; - int last_frame_width; - int last_frame_height; - int last_frame_pixel_format; - int last_frame_bit_depth; + gvfg_debug_last_frame_info_t last_frame; int backend_state; uint64_t backend_frames_captured; diff --git a/sdk/gvfg/src/backend/pcies2mm/pcies2mm_backend_types.h b/sdk/gvfg/src/backend/pcies2mm/pcies2mm_backend_types.h index fe2ecd2..4592438 100644 --- a/sdk/gvfg/src/backend/pcies2mm/pcies2mm_backend_types.h +++ b/sdk/gvfg/src/backend/pcies2mm/pcies2mm_backend_types.h @@ -84,21 +84,21 @@ typedef struct pcies2mm_debug_state_t typedef enum pcies2mm_event_type_t { PCIES2MM_EVENT_PLUG_IN = 1, - PCIES2MM_EVENT_PLUG_OUT = 2 + PCIES2MM_EVENT_PLUG_OUT = 2, + PCIES2MM_EVENT_STREAM_READY = 3, + PCIES2MM_EVENT_FORMAT_CHANGE_BEGIN = 4 } pcies2mm_event_type_t; enum { PCIES2MM_EVENT_MASK_PLUG_IN = 1u << 0, PCIES2MM_EVENT_MASK_PLUG_OUT = 1u << 1, + PCIES2MM_EVENT_MASK_STREAM_READY = 1u << 2, + PCIES2MM_EVENT_MASK_FORMAT_CHANGE_BEGIN = 1u << 3, 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_FORMAT_CHANGE_BEGIN }; -typedef struct pcies2mm_event_t -{ - pcies2mm_event_type_t type; - uint64_t timestamp_ns; -} pcies2mm_event_t; - -typedef void (*pcies2mm_event_callback_t)(const pcies2mm_event_t *event, void *user); +typedef void (*pcies2mm_event_callback_t)(pcies2mm_event_type_t event, void *user); diff --git a/sdk/gvfg/src/backend/pcies2mm/pcies2mm_capture_session.cpp b/sdk/gvfg/src/backend/pcies2mm/pcies2mm_capture_session.cpp index 697f6f8..8eb6058 100644 --- a/sdk/gvfg/src/backend/pcies2mm/pcies2mm_capture_session.cpp +++ b/sdk/gvfg/src/backend/pcies2mm/pcies2mm_capture_session.cpp @@ -20,12 +20,6 @@ namespace constexpr uint32_t kDefaultRingBufferCount = 3; constexpr uint32_t kMaxRingBufferCount = 16; - static uint64_t steady_now_ns() - { - const auto now = std::chrono::steady_clock::now().time_since_epoch(); - return static_cast(std::chrono::duration_cast(now).count()); - } - static bool should_log_counter(uint64_t count) { return count <= 5 || (count % 60) == 0; @@ -109,6 +103,10 @@ namespace return PCIES2MM_EVENT_MASK_PLUG_IN; case PCIES2MM_EVENT_PLUG_OUT: return PCIES2MM_EVENT_MASK_PLUG_OUT; + case PCIES2MM_EVENT_STREAM_READY: + return PCIES2MM_EVENT_MASK_STREAM_READY; + case PCIES2MM_EVENT_FORMAT_CHANGE_BEGIN: + return PCIES2MM_EVENT_MASK_FORMAT_CHANGE_BEGIN; default: return 0; } @@ -354,6 +352,7 @@ namespace gvfg::internal running_ = true; capture_active_ = startProbeNow; signal_probe_active_ = startProbeNow; + stream_ready_pending_ = startProbeNow; try { capture_thread_ = std::thread(&PcieS2mmCaptureSession::capture_thread_proc, this); @@ -363,6 +362,7 @@ namespace gvfg::internal running_ = false; capture_active_ = false; signal_probe_active_ = false; + stream_ready_pending_ = false; if (startProbeNow) { write_reg(video_base() + VIDEO_DMA_EN_OFFSET, 0); @@ -390,6 +390,7 @@ namespace gvfg::internal running_ = false; capture_active_ = false; signal_probe_active_ = false; + stream_ready_pending_ = false; if (device_ != INVALID_HANDLE_VALUE) { @@ -731,6 +732,7 @@ namespace gvfg::internal if (dmaEnableOk && videoEnableOk) { signal_probe_active_.store(true, std::memory_order_release); + stream_ready_pending_.store(true, std::memory_order_release); capture_active_.store(true, std::memory_order_release); } else @@ -861,7 +863,6 @@ namespace gvfg::internal { signal_present_.store(true, std::memory_order_release); signal_presence_known_.store(true, std::memory_order_release); - emit_event(PCIES2MM_EVENT_PLUG_IN); } { @@ -870,10 +871,14 @@ namespace gvfg::internal --pending_events_; publish_frame(slotIndex, static_cast(ret)); } + + if (stream_ready_pending_.exchange(false, std::memory_order_acq_rel)) + emit_event(PCIES2MM_EVENT_STREAM_READY); } void PcieS2mmCaptureSession::handle_format_change_event(uint32_t channel) { + emit_event(PCIES2MM_EVENT_FORMAT_CHANGE_BEGIN); write_reg(video_base() + VIDEO_DMA_EN_OFFSET, 0); write_reg(video_base() + VIDEO_EN_OFFSET, 0); capture_active_ = false; @@ -906,6 +911,7 @@ namespace gvfg::internal void PcieS2mmCaptureSession::handle_unplug_event(uint32_t channel) { signal_probe_active_.store(false, std::memory_order_release); + stream_ready_pending_.store(false, std::memory_order_release); signal_present_.store(false, std::memory_order_release); signal_presence_known_.store(true, std::memory_order_release); write_reg(video_base() + VIDEO_DMA_EN_OFFSET, 0); @@ -942,6 +948,7 @@ namespace gvfg::internal write_reg(video_base() + VIDEO_DMA_EN_OFFSET, 0); write_reg(video_base() + VIDEO_EN_OFFSET, 0); capture_active_.store(false, std::memory_order_release); + stream_ready_pending_.store(false, std::memory_order_release); { std::lock_guard lock(mutex_); stream_error_ = false; @@ -956,6 +963,7 @@ namespace gvfg::internal capture_active_.store(true, std::memory_order_release); signal_probe_active_.store(false, std::memory_order_release); + stream_ready_pending_.store(true, std::memory_order_release); { std::lock_guard lock(mutex_); stream_error_ = false; @@ -1055,10 +1063,7 @@ namespace gvfg::internal user = event_callback_user_; } - pcies2mm_event_t event{}; - event.type = type; - event.timestamp_ns = steady_now_ns(); - callback(&event, user); + callback(type, user); } pcies2mm_status_t PcieS2mmCaptureSession::fail(pcies2mm_status_t status, const char *where, DWORD winerr) const diff --git a/sdk/gvfg/src/backend/pcies2mm/pcies2mm_capture_session.h b/sdk/gvfg/src/backend/pcies2mm/pcies2mm_capture_session.h index 898d01b..6057030 100644 --- a/sdk/gvfg/src/backend/pcies2mm/pcies2mm_capture_session.h +++ b/sdk/gvfg/src/backend/pcies2mm/pcies2mm_capture_session.h @@ -103,6 +103,7 @@ namespace gvfg::internal std::atomic running_{false}; std::atomic capture_active_{false}; std::atomic signal_probe_active_{false}; + std::atomic stream_ready_pending_{false}; mutable std::atomic signal_presence_known_{false}; mutable std::atomic signal_present_{false}; std::thread capture_thread_; diff --git a/sdk/gvfg/src/gvfg_capture.cpp b/sdk/gvfg/src/gvfg_capture.cpp index ee410a8..b67b043 100644 --- a/sdk/gvfg/src/gvfg_capture.cpp +++ b/sdk/gvfg/src/gvfg_capture.cpp @@ -36,9 +36,8 @@ namespace static_assert(offsetof(gvfg_frame_t, frame_id) == 32); static_assert(std::is_standard_layout_v); - static_assert(sizeof(gvfg_frame_layout_t) == 32, "gvfg_frame_layout_t x64 ABI must remain stable"); - static_assert(offsetof(gvfg_frame_layout_t, struct_size) == 0); - static_assert(offsetof(gvfg_frame_layout_t, plane_count) == 4); + static_assert(sizeof(gvfg_frame_layout_t) == 32, "gvfg_frame_layout_t x64 ABI must remain frozen"); + static_assert(offsetof(gvfg_frame_layout_t, plane_count) == 0); static_assert(offsetof(gvfg_frame_layout_t, plane_data) == 8); static_assert(offsetof(gvfg_frame_layout_t, plane_stride) == 16); static_assert(offsetof(gvfg_frame_layout_t, plane_size) == 24); @@ -131,6 +130,10 @@ namespace return GVFG_EVENT_SIGNAL_CONNECTED; case PCIES2MM_EVENT_PLUG_OUT: return GVFG_EVENT_SIGNAL_DISCONNECTED; + case PCIES2MM_EVENT_STREAM_READY: + return GVFG_EVENT_STREAM_READY; + case PCIES2MM_EVENT_FORMAT_CHANGE_BEGIN: + return GVFG_EVENT_FORMAT_CHANGE_BEGIN; default: return GVFG_EVENT_UNKNOWN; } @@ -297,7 +300,6 @@ struct gvfg_handle_t gvfg_status_t getSignalStatus(gvfg_signal_status_t &out) { std::memset(&out, 0, sizeof(out)); - out.struct_size = sizeof(out); const pcies2mm_status_t status = querySignal(); { std::lock_guard lock(stateMutex); @@ -319,20 +321,8 @@ struct gvfg_handle_t return GVFG_ESTATE; std::memset(&out, 0, sizeof(out)); - out.struct_size = sizeof(out); - const uint64_t frames = deliveredFrames.load(std::memory_order_relaxed); - const bool deliveredValid = running.load(std::memory_order_relaxed) && frames > 0; - - out.last_frame.valid = deliveredValid ? 1 : 0; - if (deliveredValid) - { - out.last_frame.width = static_cast(deliveredWidth.load(std::memory_order_relaxed)); - out.last_frame.height = static_cast(deliveredHeight.load(std::memory_order_relaxed)); - out.last_frame.bit_depth = static_cast(deliveredBitDepth.load(std::memory_order_relaxed)); - out.last_frame.pixel_format = deliveredPixelFormat.load(std::memory_order_relaxed); - } out.capture_fps = runtimeFps.load(std::memory_order_relaxed); - out.delivered_frames = frames; + out.delivered_frames = deliveredFrames.load(std::memory_order_relaxed); return GVFG_OK; } @@ -345,19 +335,17 @@ struct gvfg_handle_t PCIES2MM_EVENT_MASK_DEFAULT); } - static void onBackendEvent(const pcies2mm_event_t *event, void *user) + static void onBackendEvent(pcies2mm_event_type_t event, void *user) { auto *self = static_cast(user); - if (!self || !event) + if (!self) return; - self->emitEvent(*event); + self->emitEvent(event); } - void emitEvent(const pcies2mm_event_t &event) + void emitEvent(pcies2mm_event_type_t event) { - gvfg_event_t out{}; - out.type = map_event_type(event.type); - out.timestamp_ns = event.timestamp_ns; + const gvfg_event_type_t out = map_event_type(event); { std::lock_guard lock(eventMutex); if (eventQueue.size() >= 64) @@ -541,31 +529,40 @@ struct gvfg_handle_t backend->release_frame(frame); } - gvfg_status_t pollEvent(gvfg_event_t &out, uint32_t timeoutMs) + gvfg_status_t pollEvent(gvfg_event_type_t &out, uint32_t timeoutMs) { if (!backend || !running.load(std::memory_order_acquire)) return GVFG_ESTATE; std::unique_lock lock(eventMutex); - const auto hasEvent = [this]() + const auto eventAvailableOrStopped = [this]() { return !eventQueue.empty() || !running.load(std::memory_order_acquire); }; + bool waitConditionMet = false; if (timeoutMs == 0) { - if (!hasEvent()) - return GVFG_ETIMEOUT; + // Non-blocking: inspect the current state without sleeping. + waitConditionMet = eventAvailableOrStopped(); } else if (timeoutMs == GVFG_TIMEOUT_INFINITE) { - eventCv.wait(lock, hasEvent); + // Wake when an event arrives or capture is stopped. + eventCv.wait(lock, eventAvailableOrStopped); + waitConditionMet = true; } - else if (!eventCv.wait_for(lock, std::chrono::milliseconds(timeoutMs), hasEvent)) + else { - return GVFG_ETIMEOUT; + waitConditionMet = eventCv.wait_for(lock, + std::chrono::milliseconds(timeoutMs), + eventAvailableOrStopped); } + if (!waitConditionMet) + return GVFG_ETIMEOUT; + + // A wake-up without an event means capture was stopped while waiting. if (eventQueue.empty()) return GVFG_ESTATE; @@ -581,10 +578,14 @@ struct gvfg_handle_t out.sdk_running = running.load(std::memory_order_relaxed) ? 1 : 0; out.runtime_fps = runtimeFps.load(std::memory_order_relaxed); out.frames_returned = deliveredFrames.load(std::memory_order_relaxed); - out.last_frame_width = static_cast(deliveredWidth.load(std::memory_order_relaxed)); - out.last_frame_height = static_cast(deliveredHeight.load(std::memory_order_relaxed)); - out.last_frame_pixel_format = deliveredPixelFormat.load(std::memory_order_relaxed); - out.last_frame_bit_depth = static_cast(deliveredBitDepth.load(std::memory_order_relaxed)); + out.last_frame.valid = out.sdk_running && out.frames_returned > 0 ? 1 : 0; + if (out.last_frame.valid) + { + out.last_frame.width = static_cast(deliveredWidth.load(std::memory_order_relaxed)); + out.last_frame.height = static_cast(deliveredHeight.load(std::memory_order_relaxed)); + out.last_frame.bit_depth = static_cast(deliveredBitDepth.load(std::memory_order_relaxed)); + out.last_frame.pixel_format = deliveredPixelFormat.load(std::memory_order_relaxed); + } { std::lock_guard lock(frameMutex); @@ -701,7 +702,7 @@ struct gvfg_handle_t pcies2mm_frame_t heldBackendFrame{}; std::mutex eventMutex; std::condition_variable eventCv; - std::deque eventQueue; + std::deque eventQueue; }; extern "C" @@ -776,20 +777,12 @@ extern "C" { if (!frame || !out_layout) return GVFG_EINVAL; - constexpr uint32_t kFrameLayoutV1Size = - static_cast(offsetof(gvfg_frame_layout_t, plane_size) + sizeof(out_layout->plane_size)); - const uint32_t callerSize = out_layout->struct_size; - if (callerSize < kFrameLayoutV1Size) - return GVFG_EINVAL; - gvfg_frame_layout_t layout{}; - layout.struct_size = sizeof(layout); const gvfg_status_t status = populate_frame_layout(*frame, layout); if (status != GVFG_OK) return status; - std::memcpy(out_layout, &layout, std::min(callerSize, sizeof(layout))); - out_layout->struct_size = callerSize; + *out_layout = layout; return GVFG_OK; } @@ -800,7 +793,7 @@ extern "C" return handle->releaseFrame(*frame); } - gvfg_status_t gvfg_poll_event(gvfg_handle handle, gvfg_event_t *out_event, uint32_t timeout_ms) + gvfg_status_t gvfg_poll_event(gvfg_handle handle, gvfg_event_type_t *out_event, uint32_t timeout_ms) { if (!handle || !out_event) return GVFG_EINVAL; @@ -818,20 +811,12 @@ extern "C" { if (!handle || !out_status) return GVFG_EINVAL; - constexpr uint32_t kSignalStatusV1Size = - static_cast(offsetof(gvfg_signal_status_t, bit_depth) + sizeof(out_status->bit_depth)); - const uint32_t callerSize = out_status->struct_size; - if (callerSize < kSignalStatusV1Size) - return GVFG_EINVAL; - gvfg_signal_status_t statusInfo{}; - statusInfo.struct_size = sizeof(statusInfo); const gvfg_status_t status = handle->getSignalStatus(statusInfo); if (status != GVFG_OK) return status; - std::memcpy(out_status, &statusInfo, std::min(callerSize, sizeof(statusInfo))); - out_status->struct_size = callerSize; + *out_status = statusInfo; return GVFG_OK; } @@ -839,20 +824,12 @@ extern "C" { if (!handle || !out_info) return GVFG_EINVAL; - constexpr uint32_t kRuntimeInfoV1Size = - static_cast(offsetof(gvfg_runtime_info_t, delivered_frames) + sizeof(out_info->delivered_frames)); - const uint32_t callerSize = out_info->struct_size; - if (callerSize < kRuntimeInfoV1Size) - return GVFG_EINVAL; - gvfg_runtime_info_t runtimeInfo{}; - runtimeInfo.struct_size = sizeof(runtimeInfo); const gvfg_status_t status = handle->getRuntimeInfo(runtimeInfo); if (status != GVFG_OK) return status; - std::memcpy(out_info, &runtimeInfo, std::min(callerSize, sizeof(runtimeInfo))); - out_info->struct_size = callerSize; + *out_info = runtimeInfo; return GVFG_OK; } @@ -891,18 +868,12 @@ extern "C" if (!handle || !out_stats) return GVFG_EINVAL; - const uint32_t callerSize = out_stats->struct_size; - if (callerSize < sizeof(uint32_t) || callerSize > sizeof(gvfg_debug_backend_stats_t)) - return GVFG_EINVAL; - gvfg_debug_backend_stats_t stats{}; const gvfg_status_t status = handle->getDebugBackendStats(stats); if (status != GVFG_OK) return status; - stats.struct_size = sizeof(stats); - std::memcpy(out_stats, &stats, callerSize); - out_stats->struct_size = callerSize; + *out_stats = stats; return GVFG_OK; }