diff --git a/CMakeLists.txt b/CMakeLists.txt index 3129a2d..06c6fcd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,6 +2,8 @@ cmake_minimum_required(VERSION 3.16) project(gvfg_standalone VERSION 0.1.0 LANGUAGES CXX) +include(GNUInstallDirs) + set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) @@ -16,6 +18,13 @@ set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib") option(BUILD_GVFG_SAMPLES "Build GVFG customer samples" ON) +option(GVFG_ENABLE_INTERNAL_DEBUG_API "Export internal GVFG diagnostic APIs" OFF) +option(BUILD_GVFG_INTERNAL_TOOLS "Build internal driver/FPGA diagnostic tools" OFF) + +if (BUILD_GVFG_INTERNAL_TOOLS AND NOT GVFG_ENABLE_INTERNAL_DEBUG_API) + message(FATAL_ERROR + "BUILD_GVFG_INTERNAL_TOOLS requires GVFG_ENABLE_INTERNAL_DEBUG_API=ON") +endif() if (NOT WIN32) message(FATAL_ERROR "GVFG currently supports Windows only.") @@ -25,6 +34,19 @@ add_subdirectory(sdk/gvfg) add_subdirectory(helpers/gvfg_preview) add_subdirectory(helpers/gvfg_convert) -if (BUILD_GVFG_SAMPLES) +if (BUILD_GVFG_SAMPLES OR BUILD_GVFG_INTERNAL_TOOLS) add_subdirectory(samples/gvfg_qt_preview) endif() + +install(FILES + docs/GVFG_CUSTOMER_API.md + DESTINATION ${CMAKE_INSTALL_DOCDIR} +) + +if (GVFG_ENABLE_INTERNAL_DEBUG_API) + install(FILES + README.md + docs/GVFG_INTERNAL_NOTES.md + DESTINATION ${CMAKE_INSTALL_DOCDIR} + ) +endif() diff --git a/README.md b/README.md index 9f19438..4fd388e 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,8 @@ import library、runtime DLL 來使用 GVFG,不要直接把 GVFG source 編進 - `sdk/gvfg`:GVFG customer C API、internal debug API、PCIES2MM backend。 - `helpers/gvfg_preview`:可選的 preview helper DLL,在 `gvfg_read_frame()` 後使用。 - `helpers/gvfg_convert`:可選的 snapshot/export conversion helper DLL。 -- `samples/gvfg_qt_preview`:內部 debug Qt preview tool。 +- `samples/gvfg_qt_preview`:customer-facing Qt preview sample;internal build + 會另外產生 `gvfg_qt_diagnostic`。 - `docs`:API 與整合說明。 客戶端 API 細節在 `docs/GVFG_CUSTOMER_API.md`。內部架構、package @@ -32,9 +33,25 @@ cmake --build build --target gvfg_qt_preview --config Release ```text BUILD_GVFG_SAMPLES=ON +GVFG_ENABLE_INTERNAL_DEBUG_API=OFF +BUILD_GVFG_INTERNAL_TOOLS=OFF GVFG_PCIES2MM_DEBUG_LOG=OFF ``` +Customer build 必須保持 `GVFG_ENABLE_INTERNAL_DEBUG_API=OFF`。這會移除 +`gvfg_debug_*` DLL exports,install tree 也不會包含 `gvfg_debug.h`。 + +公司內部 diagnostic build 使用: + +```bat +cmake -S . -B build_internal ^ + -DBUILD_GVFG_SAMPLES=OFF ^ + -DGVFG_ENABLE_INTERNAL_DEBUG_API=ON ^ + -DBUILD_GVFG_INTERNAL_TOOLS=ON ^ + -DCMAKE_PREFIX_PATH=C:\Qt\6.10.2\msvc2022_64 +cmake --build build_internal --target gvfg_qt_diagnostic --config Release +``` + Build 產物: ```text @@ -47,6 +64,9 @@ build/.../lib/gvfg_convert.lib build/.../bin/gvfg_qt_preview.exe ``` +`gvfg_qt_preview.exe` 只使用 public API,主畫面只顯示 input 與 Preview +狀態;IRQ、DMA、ring slot 等資訊只存在 internal diagnostic tool。 + ## Consumer Layout 一般 application 使用 core SDK 只需要: diff --git a/docs/GVFG_CUSTOMER_API.md b/docs/GVFG_CUSTOMER_API.md index 88cb101..2b7d4a4 100644 --- a/docs/GVFG_CUSTOMER_API.md +++ b/docs/GVFG_CUSTOMER_API.md @@ -7,6 +7,10 @@ Customer SDK 的 core API 只負責 capture。它不 expose SDK-managed preview driver registers、DMA internals 或 FPGA debug controls。Preview rendering、 snapshot conversion 這類功能放在 helper DLL,使用者需要時再另外 link。 +Customer 交付內容只包含 public headers 與 public DLL functions。Customer +sample 只記錄 signal events、API failures、capture stall/recovery 與 +preview failures,不顯示硬體或 backend implementation details。 + ## 基本流程 ```text diff --git a/docs/GVFG_INTERNAL_NOTES.md b/docs/GVFG_INTERNAL_NOTES.md index 93b13aa..f89571b 100644 --- a/docs/GVFG_INTERNAL_NOTES.md +++ b/docs/GVFG_INTERNAL_NOTES.md @@ -21,8 +21,8 @@ helpers/gvfg_convert/ optional snapshot/export helper; converts native capture frames on request samples/gvfg_qt_preview/ - internal debug Qt tool - uses gvfg.dll + gvfg_preview.dll + gvfg_debug.h + gvfg_qt_preview: customer-facing, uses public APIs only + gvfg_qt_diagnostic: internal build, additionally uses gvfg_debug.h ``` 從 customer 角度看,core SDK 必須維持 driver-neutral。PCIES2MM、IRQ、DMA counters、 @@ -255,7 +255,7 @@ lib/gvfg_convert.lib bin/gvfg.dll bin/gvfg_preview.dll bin/gvfg_convert.dll -bin/gvfg_qt_preview.exe +bin/gvfg_qt_diagnostic.exe PDB symbols internal debug notes ``` @@ -272,9 +272,27 @@ Top-level CMake 會 build core SDK、helpers 和 optional sample: ```text BUILD_GVFG_SAMPLES=ON +GVFG_ENABLE_INTERNAL_DEBUG_API=OFF +BUILD_GVFG_INTERNAL_TOOLS=OFF GVFG_PCIES2MM_DEBUG_LOG=OFF ``` +Customer build 使用 `GVFG_ENABLE_INTERNAL_DEBUG_API=OFF`。此時 `gvfg.dll` +不 export `gvfg_debug_get_backend_stats()` 或 +`gvfg_debug_get_last_error_detail()`,install tree 也不包含 +`gvfg_debug.h`。 + +Internal diagnostic build 使用: + +```text +BUILD_GVFG_SAMPLES=OFF +GVFG_ENABLE_INTERNAL_DEBUG_API=ON +BUILD_GVFG_INTERNAL_TOOLS=ON +``` + +只有這個 build 會產生 `gvfg_qt_diagnostic.exe` 並安裝 +`gvfg_debug.h`。 + 輸出產物: ```text diff --git a/helpers/gvfg_convert/CMakeLists.txt b/helpers/gvfg_convert/CMakeLists.txt index 44361b6..75ba43f 100644 --- a/helpers/gvfg_convert/CMakeLists.txt +++ b/helpers/gvfg_convert/CMakeLists.txt @@ -29,3 +29,12 @@ target_link_libraries(gvfg_convert PUBLIC if (MSVC) target_compile_options(gvfg_convert PRIVATE /utf-8 /EHsc) endif() + +install(TARGETS gvfg_convert + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} +) + +install(FILES include/gvfg_convert.h + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} +) diff --git a/helpers/gvfg_preview/CMakeLists.txt b/helpers/gvfg_preview/CMakeLists.txt index c092d25..41617d8 100644 --- a/helpers/gvfg_preview/CMakeLists.txt +++ b/helpers/gvfg_preview/CMakeLists.txt @@ -35,3 +35,12 @@ target_link_libraries(gvfg_preview if (MSVC) target_compile_options(gvfg_preview PRIVATE /utf-8 /EHsc) endif() + +install(TARGETS gvfg_preview + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} +) + +install(FILES include/gvfg_preview.h + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} +) diff --git a/samples/gvfg_qt_preview/CMakeLists.txt b/samples/gvfg_qt_preview/CMakeLists.txt index 398a5e1..cb4f9cc 100644 --- a/samples/gvfg_qt_preview/CMakeLists.txt +++ b/samples/gvfg_qt_preview/CMakeLists.txt @@ -8,44 +8,58 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets) find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets) -if (QT_VERSION_MAJOR GREATER_EQUAL 6) - qt_add_executable(gvfg_qt_preview - main.cpp - mainwindow.cpp - mainwindow.h - mainwindow.ui - previewwindow.cpp - previewwindow.h - previewwindow.ui +set(GVFG_QT_PREVIEW_SOURCES + main.cpp + mainwindow.cpp + mainwindow.h + mainwindow.ui + previewwindow.cpp + previewwindow.h + previewwindow.ui +) + +function(add_gvfg_qt_preview_target target_name internal_diagnostics) + if (QT_VERSION_MAJOR GREATER_EQUAL 6) + qt_add_executable(${target_name} ${GVFG_QT_PREVIEW_SOURCES}) + else() + add_executable(${target_name} ${GVFG_QT_PREVIEW_SOURCES}) + endif() + + target_link_libraries(${target_name} PRIVATE + Qt${QT_VERSION_MAJOR}::Widgets + gvfg + gvfg_preview ) -else() - add_executable(gvfg_qt_preview - main.cpp - mainwindow.cpp - mainwindow.h - mainwindow.ui - previewwindow.cpp - previewwindow.h - previewwindow.ui + + target_compile_definitions(${target_name} PRIVATE + GVFG_INTERNAL_DIAGNOSTICS=${internal_diagnostics} + ) + + if (MSVC) + target_compile_options(${target_name} PRIVATE /utf-8) + endif() + + add_custom_command(TARGET ${target_name} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_if_different + $ + $ + ) + + set_target_properties(${target_name} PROPERTIES + WIN32_EXECUTABLE TRUE + ) +endfunction() + +if (BUILD_GVFG_SAMPLES) + add_gvfg_qt_preview_target(gvfg_qt_preview 0) + install(TARGETS gvfg_qt_preview + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} ) endif() -target_link_libraries(gvfg_qt_preview PRIVATE - Qt${QT_VERSION_MAJOR}::Widgets - gvfg - gvfg_preview -) - -if (MSVC) - target_compile_options(gvfg_qt_preview PRIVATE /utf-8) +if (BUILD_GVFG_INTERNAL_TOOLS) + add_gvfg_qt_preview_target(gvfg_qt_diagnostic 1) + install(TARGETS gvfg_qt_diagnostic + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + ) endif() - -add_custom_command(TARGET gvfg_qt_preview POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy_if_different - $ - $ -) - -set_target_properties(gvfg_qt_preview PROPERTIES - WIN32_EXECUTABLE TRUE -) diff --git a/samples/gvfg_qt_preview/mainwindow.cpp b/samples/gvfg_qt_preview/mainwindow.cpp index f576384..9b3c98c 100644 --- a/samples/gvfg_qt_preview/mainwindow.cpp +++ b/samples/gvfg_qt_preview/mainwindow.cpp @@ -2,7 +2,9 @@ #include "previewwindow.h" #include "ui_mainwindow.h" +#if GVFG_INTERNAL_DIAGNOSTICS #include +#endif #include #include @@ -17,6 +19,15 @@ namespace { + QString logFilePrefix() + { +#if GVFG_INTERNAL_DIAGNOSTICS + return QStringLiteral("gvfg_qt_diagnostic"); +#else + return QStringLiteral("gvfg_qt_preview"); +#endif + } + class LogHighlighter final : public QSyntaxHighlighter { public: @@ -27,6 +38,8 @@ namespace errorFormat_.setFontWeight(QFont::Bold); recoveryFormat_.setForeground(QColor(0, 128, 0)); recoveryFormat_.setFontWeight(QFont::Bold); + warningFormat_.setForeground(QColor(190, 110, 0)); + warningFormat_.setFontWeight(QFont::Bold); } protected: @@ -41,6 +54,12 @@ namespace return; } + if (text.contains(QStringLiteral("warning"), Qt::CaseInsensitive)) + { + setFormat(0, text.size(), warningFormat_); + return; + } + if (text.contains(QStringLiteral("SIGNAL_CONNECTED")) || text.contains(QStringLiteral("CAPTURE_RESUMED")) || text.contains(QStringLiteral("recovered"), Qt::CaseInsensitive)) @@ -50,8 +69,10 @@ namespace private: QTextCharFormat errorFormat_; QTextCharFormat recoveryFormat_; + QTextCharFormat warningFormat_; }; +#if GVFG_INTERNAL_DIAGNOSTICS QString boolText(int value) { return value ? QStringLiteral("yes") : QStringLiteral("no"); @@ -77,6 +98,7 @@ namespace { return value == UINT64_MAX ? QStringLiteral("none") : QStringLiteral("slot %1").arg(valueOrDash(value)); } +#endif QString frameText(bool valid, int width, int height, const char *pixelFormat, int bitDepth) { @@ -92,6 +114,9 @@ namespace QString signalFrameText(const gvfg_signal_status_t &signal) { + if (!signal.connected) + return QStringLiteral("No signal"); + const QString resolution = (signal.width > 0 && signal.height > 0) ? QStringLiteral("%1x%2").arg(signal.width).arg(signal.height) : QStringLiteral("--"); @@ -102,6 +127,7 @@ namespace return QStringLiteral("%1 %2 %3-bit").arg(resolution, format, bit); } +#if GVFG_INTERNAL_DIAGNOSTICS QString backendLastError(gvfg_handle handle) { char message[512] = {}; @@ -109,6 +135,7 @@ namespace return {}; return QString::fromUtf8(message); } +#endif QString eventTypeText(gvfg_event_type_t type) { @@ -133,13 +160,17 @@ MainWindow::MainWindow(QWidget *parent) { ui_->setupUi(this); +#if GVFG_INTERNAL_DIAGNOSTICS + setWindowTitle(QStringLiteral("GVFG Internal Diagnostic")); +#else + setWindowTitle(QStringLiteral("GVFG Preview Sample")); +#endif previewWindow_ = new PreviewWindow(); ui_->logEdit->setMaximumBlockCount(300); new LogHighlighter(ui_->logEdit->document()); ui_->statusLabel->setWordWrap(true); signalStatusTimer_ = new QTimer(this); signalStatusTimer_->setInterval(1000); - diagnosticSnapshotTimer_.start(); openLogFile(); connect(ui_->refreshButton, &QPushButton::clicked, this, [this]() @@ -218,7 +249,7 @@ void MainWindow::refreshDevices() } else { - appendLog(QStringLiteral("Found %1 PCIES2MM device(s)").arg(deviceCount_)); + appendLog(QStringLiteral("Found %1 GVFG capture device(s)").arg(deviceCount_)); } } @@ -273,7 +304,7 @@ bool MainWindow::openDevice() lastSignalStatusText_.clear(); appendLog(QStringLiteral("Opened device index %1 CH%2").arg(deviceIndex).arg(channelIndex)); - appendLog(QStringLiteral("FPGA signal monitor active")); + appendLog(QStringLiteral("Signal monitoring active")); updateSignalStatus(); signalStatusTimer_->start(); updateUiState(); @@ -317,7 +348,9 @@ void MainWindow::startCapture() if (!applyPreview()) return; +#if GVFG_INTERNAL_DIAGNOSTICS appendLog(QStringLiteral("FPGA signal before stream start")); +#endif updateSignalStatus(); const gvfg_status_t st = gvfg_start(handle_); @@ -330,6 +363,13 @@ void MainWindow::startCapture() } previewFailureCount_ = 0; +#if GVFG_INTERNAL_DIAGNOSTICS + haveDebugBaseline_ = false; + lastDebugDmaErrors_ = 0; + lastDebugDroppedFrames_ = 0; + pendingDroppedFrames_ = 0; + lastDroppedWarningMs_ = 0; +#endif captureStop_.store(false, std::memory_order_release); captureRunning_ = true; captureThread_ = std::thread([this]() @@ -422,10 +462,11 @@ void MainWindow::updateSignalStatus() if (gvfg_get_signal_status(handle_, &signal) != GVFG_OK) return; - const auto &readFrame = info.last_frame; +#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); @@ -447,21 +488,22 @@ void MainWindow::updateSignalStatus() previewInfo.pixel_format, previewInfo.bit_depth) : QStringLiteral("--"); - const QString lastFrame = frameText(readFrame.valid != 0, - readFrame.width, - readFrame.height, - gvfg_pixel_format_name(readFrame.pixel_format), - readFrame.bit_depth); - QStringList statusLines; - statusLines << QStringLiteral("Input | CH%1 connected=%2 signal=%3") - .arg(signal.channel) - .arg(boolText(signal.connected), signalFrameText(signal)); - statusLines << QStringLiteral("Preview | fps=%1 last_frame=%2 active=%3 output=%4") - .arg(previewFps) - .arg(lastFrame) - .arg(boolText(previewInfoOk ? 1 : 0)) - .arg(previewFrame); + statusLines << (signal.connected + ? QStringLiteral("Input | CH%1 | Connected | %2") + .arg(signal.channel) + .arg(signalFrameText(signal)) + : QStringLiteral("Input | CH%1 | No signal") + .arg(signal.channel)); + statusLines << (previewInfoOk + ? QStringLiteral("Preview | %1 | %2") + .arg(previewFps == QStringLiteral("--") + ? QStringLiteral("Measuring") + : QStringLiteral("%1 FPS").arg(previewFps), + previewFrame) + : QStringLiteral("Preview | Inactive")); + +#if GVFG_INTERNAL_DIAGNOSTICS statusLines << (haveBackendStats ? QStringLiteral("Capture| status=%1 pending_irqs=%2 dma_errors=%3 no_frame_waits=%4") .arg(captureStatusText(backendStats, signal.connected != 0)) @@ -483,6 +525,44 @@ void MainWindow::updateSignalStatus() valueOrDash(backendStats.backend_latest_sequence), valueOrDash(backendStats.backend_delivered_sequence)); } + + bool diagnosticProblemDetected = false; + if (haveBackendStats) + { + if (haveDebugBaseline_) + { + if (backendStats.backend_dma_errors > lastDebugDmaErrors_) + { + const uint64_t delta = backendStats.backend_dma_errors - lastDebugDmaErrors_; + appendLog(QStringLiteral("ERROR DMA failures +%1, total=%2") + .arg(valueOrDash(delta), + valueOrDash(backendStats.backend_dma_errors))); + diagnosticProblemDetected = true; + } + if (backendStats.backend_frames_dropped > lastDebugDroppedFrames_) + { + const uint64_t delta = backendStats.backend_frames_dropped - lastDebugDroppedFrames_; + pendingDroppedFrames_ += delta; + } + } + + const qint64 nowMs = QDateTime::currentMSecsSinceEpoch(); + if (pendingDroppedFrames_ > 0 && + (lastDroppedWarningMs_ == 0 || nowMs - lastDroppedWarningMs_ >= 10000)) + { + appendLog(QStringLiteral("WARNING dropped frames +%1, total=%2") + .arg(valueOrDash(pendingDroppedFrames_), + valueOrDash(backendStats.backend_frames_dropped))); + pendingDroppedFrames_ = 0; + lastDroppedWarningMs_ = nowMs; + diagnosticProblemDetected = true; + } + + lastDebugDmaErrors_ = backendStats.backend_dma_errors; + lastDebugDroppedFrames_ = backendStats.backend_frames_dropped; + haveDebugBaseline_ = true; + } + const QString lastError = backendLastError(handle_); if (!lastError.isEmpty()) statusLines << QStringLiteral("Error | %1").arg(lastError); @@ -491,11 +571,13 @@ void MainWindow::updateSignalStatus() { lastLoggedBackendError_ = lastError; appendLog(QStringLiteral("Backend error | %1").arg(lastError)); + diagnosticProblemDetected = true; } else if (lastError.isEmpty()) { lastLoggedBackendError_.clear(); } +#endif const QString statusText = statusLines.join(QLatin1Char('\n')); const bool changed = lastSignalStatusText_ != statusText; @@ -505,11 +587,10 @@ void MainWindow::updateSignalStatus() lastSignalStatusText_ = statusText; } - if (diagnosticSnapshotTimer_.elapsed() >= 10000) - { +#if GVFG_INTERNAL_DIAGNOSTICS + if (diagnosticProblemDetected) writeDiagnosticSnapshot(statusText); - diagnosticSnapshotTimer_.restart(); - } +#endif } void MainWindow::updateUiState() @@ -549,15 +630,16 @@ bool MainWindow::openLogFilePart() if (logFile_.isOpen()) logFile_.close(); - logFilePath_ = QStringLiteral("%1/gvfg_qt_preview_%2_part%3.log") - .arg(logDirPath_, logSessionStamp_) + logFilePath_ = QStringLiteral("%1/%2_%3_part%4.log") + .arg(logDirPath_, logFilePrefix(), logSessionStamp_) .arg(logPartIndex_, 2, 10, QLatin1Char('0')); logFile_.setFileName(logFilePath_); if (!logFile_.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text)) return false; - const QString header = QStringLiteral("\n==== gvfg_qt_preview session %1 part %2 ====\n") - .arg(QDateTime::currentDateTime().toString(QStringLiteral("yyyy-MM-dd HH:mm:ss.zzz"))) + const QString header = QStringLiteral("\n==== %1 session %2 part %3 ====\n") + .arg(logFilePrefix(), + QDateTime::currentDateTime().toString(QStringLiteral("yyyy-MM-dd HH:mm:ss.zzz"))) .arg(logPartIndex_); logFile_.write(header.toUtf8()); logFile_.flush(); @@ -592,14 +674,15 @@ void MainWindow::writeLogFileLine(const QString &line) ++logPartIndex_; if (logFile_.isOpen()) logFile_.close(); - logFilePath_ = QStringLiteral("%1/gvfg_qt_preview_%2_part%3.log") - .arg(logDirPath_, logSessionStamp_) + logFilePath_ = QStringLiteral("%1/%2_%3_part%4.log") + .arg(logDirPath_, logFilePrefix(), logSessionStamp_) .arg(logPartIndex_, 2, 10, QLatin1Char('0')); logFile_.setFileName(logFilePath_); if (logFile_.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text)) { - const QString header = QStringLiteral("\n==== gvfg_qt_preview session %1 part %2 ====\n") - .arg(QDateTime::currentDateTime().toString(QStringLiteral("yyyy-MM-dd HH:mm:ss.zzz"))) + const QString header = QStringLiteral("\n==== %1 session %2 part %3 ====\n") + .arg(logFilePrefix(), + QDateTime::currentDateTime().toString(QStringLiteral("yyyy-MM-dd HH:mm:ss.zzz"))) .arg(logPartIndex_); logFile_.write(header.toUtf8()); } @@ -613,6 +696,7 @@ void MainWindow::writeLogFileLine(const QString &line) logFile_.flush(); } +#if GVFG_INTERNAL_DIAGNOSTICS void MainWindow::writeDiagnosticSnapshot(const QString &statusText) { const QString timestamp = QDateTime::currentDateTime().toString(QStringLiteral("HH:mm:ss.zzz")); @@ -626,6 +710,7 @@ void MainWindow::writeDiagnosticSnapshot(const QString &statusText) writeLogFileLine(line); } } +#endif void MainWindow::appendLog(const QString &message) { @@ -644,23 +729,41 @@ void MainWindow::appendLog(const QString &message) void MainWindow::captureReadLoop() { + uint32_t consecutiveTimeouts = 0; + bool captureStalledLogged = false; + while (!captureStop_.load(std::memory_order_acquire)) { gvfg_event_t event{}; 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 + QMetaObject::invokeMethod(this, [this, type]() + { appendLog(QStringLiteral("EVENT %1").arg(type)); }, + Qt::QueuedConnection); +#endif } gvfg_frame_t frame{}; const gvfg_status_t st = gvfg_read_frame(handle_, &frame, 200); if (st == GVFG_OK) { + consecutiveTimeouts = 0; + if (captureStalledLogged) + { + captureStalledLogged = false; + QMetaObject::invokeMethod(this, [this]() + { appendLog(QStringLiteral("RECOVERED capture resumed")); }, + Qt::QueuedConnection); + } + if (previewHandle_) { gvfg_preview_frame_t previewFrame{}; @@ -703,7 +806,7 @@ void MainWindow::captureReadLoop() if (previewStatus != GVFG_PREVIEW_OK) { const uint64_t failures = ++previewFailureCount_; - if (failures <= 5 || (failures % 60) == 0) + if (failures == 1) { QMetaObject::invokeMethod(this, [this, failures, previewStatus]() { appendLog(QStringLiteral("preview render failed #%1: %2") @@ -726,7 +829,24 @@ void MainWindow::captureReadLoop() } if (st == GVFG_ETIMEOUT) + { + ++consecutiveTimeouts; + if (!captureStalledLogged && + consecutiveTimeouts >= 10 && + (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; + QMetaObject::invokeMethod(this, [this]() + { appendLog(QStringLiteral("ERROR capture stalled: no frame for at least 2 seconds")); }, + Qt::QueuedConnection); + } + } continue; + } if (captureStop_.load(std::memory_order_acquire) || st == GVFG_ESTATE) break; diff --git a/samples/gvfg_qt_preview/mainwindow.h b/samples/gvfg_qt_preview/mainwindow.h index dfa4ca4..5bcc612 100644 --- a/samples/gvfg_qt_preview/mainwindow.h +++ b/samples/gvfg_qt_preview/mainwindow.h @@ -4,7 +4,6 @@ #include #include -#include #include #include @@ -55,7 +54,9 @@ private: bool openLogFilePart(); void rotateLogFileIfNeeded(); void writeLogFileLine(const QString &line); +#if GVFG_INTERNAL_DIAGNOSTICS void writeDiagnosticSnapshot(const QString &statusText); +#endif void appendLog(const QString &message); void captureReadLoop(); void joinCaptureThread(); @@ -74,8 +75,14 @@ private: uint64_t previewFailureCount_ = 0; QTimer *signalStatusTimer_ = nullptr; QString lastSignalStatusText_; +#if GVFG_INTERNAL_DIAGNOSTICS QString lastLoggedBackendError_; - QElapsedTimer diagnosticSnapshotTimer_; + uint64_t lastDebugDmaErrors_ = 0; + uint64_t lastDebugDroppedFrames_ = 0; + uint64_t pendingDroppedFrames_ = 0; + qint64 lastDroppedWarningMs_ = 0; + bool haveDebugBaseline_ = false; +#endif QFile logFile_; std::mutex logFileMutex_; QString logDirPath_; diff --git a/sdk/gvfg/CMakeLists.txt b/sdk/gvfg/CMakeLists.txt index ee8238b..cf91b29 100644 --- a/sdk/gvfg/CMakeLists.txt +++ b/sdk/gvfg/CMakeLists.txt @@ -8,7 +8,6 @@ project(gvfg VERSION ${GVFG_SDK_VERSION} LANGUAGES CXX) add_library(gvfg SHARED include/gvfg_capture.h - include/gvfg_debug.h src/backend/pcies2mm/pcies2mm_backend_types.h src/backend/pcies2mm/pcies2mm_capture_session.cpp src/backend/pcies2mm/pcies2mm_capture_session.h @@ -21,6 +20,10 @@ add_library(gvfg SHARED src/gvfg_capture.cpp ) +if (GVFG_ENABLE_INTERNAL_DEBUG_API) + target_sources(gvfg PRIVATE include/gvfg_debug.h) +endif() + set_target_properties(gvfg PROPERTIES AUTOMOC OFF AUTOUIC OFF @@ -37,6 +40,7 @@ target_include_directories(gvfg target_compile_definitions(gvfg PRIVATE GVFG_BUILD GVFG_SDK_VERSION="${GVFG_SDK_VERSION}" + GVFG_ENABLE_INTERNAL_DEBUG_API=$ ) if (GVFG_PCIES2MM_DEBUG_LOG) @@ -53,4 +57,18 @@ if (MSVC) target_compile_options(gvfg PRIVATE /utf-8 /EHsc) endif() +install(TARGETS gvfg + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} +) + +install(FILES include/gvfg_capture.h + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} +) + +if (GVFG_ENABLE_INTERNAL_DEBUG_API) + install(FILES include/gvfg_debug.h + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} + ) +endif() diff --git a/sdk/gvfg/src/gvfg_capture.cpp b/sdk/gvfg/src/gvfg_capture.cpp index 7001bc6..f35c2d4 100644 --- a/sdk/gvfg/src/gvfg_capture.cpp +++ b/sdk/gvfg/src/gvfg_capture.cpp @@ -1,6 +1,8 @@ #include "gvfg_capture.h" +#if GVFG_ENABLE_INTERNAL_DEBUG_API #include "gvfg_debug.h" +#endif #include "pcies2mm_capture_session.h" #include @@ -554,6 +556,7 @@ struct gvfg_handle_t return GVFG_OK; } +#if GVFG_ENABLE_INTERNAL_DEBUG_API gvfg_status_t getDebugBackendStats(gvfg_debug_backend_stats_t &out) { std::memset(&out, 0, sizeof(out)); @@ -611,6 +614,7 @@ struct gvfg_handle_t copy_cstr(outMessage, outMessageSize, backend ? backend->last_error() : ""); return GVFG_OK; } +#endif void recordError(const char *msg) { @@ -866,6 +870,7 @@ extern "C" } } +#if GVFG_ENABLE_INTERNAL_DEBUG_API gvfg_status_t gvfg_debug_get_backend_stats(gvfg_handle handle, gvfg_debug_backend_stats_t *out_stats) { @@ -895,6 +900,7 @@ extern "C" return GVFG_EINVAL; return handle->getLastErrorDetail(out_message, out_message_size); } +#endif }