Refactor SDK to pure pull-based capture API

- Remove customer-facing SDK preview APIs and preview structs
- Strip D3D preview/render pipeline from gvfg.dll
- Keep gvfg.dll focused on capture, frame ownership, runtime status, and events
- Move Qt preview rendering into sample-owned private renderer
- Update Qt preview sample to render frames after gvfg_read_frame()
- Refresh customer API docs around read_frame/release_frame/poll_event flow
- Update architecture notes to mark preview as app/internal-tool code
This commit is contained in:
Mark.lin
2026-06-15 14:03:21 +08:00
parent c21f6981ea
commit 97971e6242
14 changed files with 947 additions and 1286 deletions
+14
View File
@@ -17,6 +17,10 @@ if (QT_VERSION_MAJOR GREATER_EQUAL 6)
previewwindow.cpp
previewwindow.h
previewwindow.ui
sample_preview_renderer.cpp
sample_preview_renderer.h
${CMAKE_SOURCE_DIR}/sdk/gvfg/src/gvfg_log_bridge.cpp
${CMAKE_SOURCE_DIR}/sdk/gvfg/src/render/shared_scene_pipeline.cpp
)
else()
add_executable(gvfg_qt_preview
@@ -27,16 +31,26 @@ else()
previewwindow.cpp
previewwindow.h
previewwindow.ui
sample_preview_renderer.cpp
sample_preview_renderer.h
${CMAKE_SOURCE_DIR}/sdk/gvfg/src/gvfg_log_bridge.cpp
${CMAKE_SOURCE_DIR}/sdk/gvfg/src/render/shared_scene_pipeline.cpp
)
endif()
target_link_libraries(gvfg_qt_preview PRIVATE
Qt${QT_VERSION_MAJOR}::Widgets
gvfg
user32
dxgi
d3d11
D3DCompiler
)
target_include_directories(gvfg_qt_preview PRIVATE
${CMAKE_SOURCE_DIR}/sdk/gvfg/include
${CMAKE_SOURCE_DIR}/sdk/gvfg/src/internal
${CMAKE_SOURCE_DIR}/sdk/gvfg/src/render
)
if (MSVC)
+86 -89
View File
@@ -145,9 +145,6 @@ bool MainWindow::openDevice()
return false;
}
gvfg_set_callbacks(handle_, &MainWindow::onFrame, &MainWindow::onError, this);
gvfg_set_event_callback(handle_, &MainWindow::onEvent, this, GVFG_EVENT_MASK_DEFAULT);
const int deviceIndex = ui_->deviceCombo->currentData().toInt();
st = gvfg_open(handle_, deviceIndex);
if (st != GVFG_OK)
@@ -169,6 +166,7 @@ bool MainWindow::openDevice()
void MainWindow::closeDevice()
{
stopCapture();
previewRenderer_.shutdown();
if (signalStatusTimer_)
signalStatusTimer_->stop();
@@ -211,7 +209,10 @@ void MainWindow::startCapture()
}
frameCount_ = 0;
captureStop_.store(false, std::memory_order_release);
captureRunning_ = true;
captureThread_ = std::thread([this]()
{ captureReadLoop(); });
updateUiState();
appendLog(QStringLiteral("Started capture"));
updateSignalStatus(true);
@@ -221,6 +222,8 @@ void MainWindow::stopCapture()
{
if (handle_ && captureRunning_)
{
captureStop_.store(true, std::memory_order_release);
joinCaptureThread();
gvfg_stop(handle_);
captureRunning_ = false;
appendLog(QStringLiteral("Stopped capture"));
@@ -232,18 +235,9 @@ void MainWindow::stopCapture()
bool MainWindow::applyPreview()
{
if (!handle_)
return false;
gvfg_preview_desc_t preview{};
preview.hwnd = previewWindow_->nativePreviewHandle();
preview.enable_preview = 1;
preview.swapchain_bitdepth = GVFG_PREVIEW_BITDEPTH_AUTO;
const gvfg_status_t st = gvfg_set_preview(handle_, &preview);
if (st != GVFG_OK)
if (!previewRenderer_.configure(previewWindow_->nativePreviewHandle()))
{
showError(QStringLiteral("gvfg_set_preview"), st);
appendLog(QStringLiteral("Preview setup failed: invalid preview window"));
return false;
}
return true;
@@ -252,7 +246,7 @@ bool MainWindow::applyPreview()
void MainWindow::updatePreviewSourceSize(const gvfg_runtime_info_t &info)
{
const auto &signal = info.input_signal;
const auto &callback = info.callback_frame;
const auto &readFrame = info.callback_frame;
if (signal.width > 0 && signal.height > 0)
{
@@ -260,8 +254,8 @@ void MainWindow::updatePreviewSourceSize(const gvfg_runtime_info_t &info)
return;
}
if (callback.valid && callback.width > 0 && callback.height > 0)
previewWindow_->setSourceSize(callback.width, callback.height);
if (readFrame.valid && readFrame.width > 0 && readFrame.height > 0)
previewWindow_->setSourceSize(readFrame.width, readFrame.height);
}
void MainWindow::updatePreviewSourceSize()
@@ -284,8 +278,7 @@ void MainWindow::updateSignalStatus(bool writeLog)
return;
const auto &signal = info.input_signal;
const auto &preview = info.preview_output;
const auto &callback = info.callback_frame;
const auto &readFrame = info.callback_frame;
if (previewWindow_->isVisible())
updatePreviewSourceSize(info);
@@ -320,21 +313,20 @@ void MainWindow::updateSignalStatus(bool writeLog)
.arg(hex32(signal.raw.frame_rate))
.arg(signal.raw.bit_depth)
.arg(hex32(signal.raw.status));
const QString line3 = preview.active
const QString line3 = previewRenderer_.active()
? QStringLiteral("Preview output | frame=%1x%2 format=%3 bitdepth=%4")
.arg(preview.width)
.arg(preview.height)
.arg(QString::fromUtf8(preview.pixel_format))
.arg(preview.bit_depth)
: (preview.enabled ? QStringLiteral("Preview output | configured, inactive")
: QStringLiteral("Preview output | disabled"));
const QString line4 = callback.valid
? QStringLiteral("Callback frame | frame=%1x%2 format=%3 bitdepth=%4")
.arg(callback.width)
.arg(callback.height)
.arg(QString::fromUtf8(callback.pixel_format))
.arg(callback.bit_depth)
: QStringLiteral("Callback frame | --");
.arg(previewRenderer_.width())
.arg(previewRenderer_.height())
.arg(QString::fromUtf8(previewRenderer_.pixelFormat()))
.arg(previewRenderer_.bitDepth())
: QStringLiteral("Preview output | app renderer inactive");
const QString line4 = readFrame.valid
? QStringLiteral("Read frame | frame=%1x%2 format=%3 bitdepth=%4")
.arg(readFrame.width)
.arg(readFrame.height)
.arg(QString::fromUtf8(readFrame.pixel_format))
.arg(readFrame.bit_depth)
: QStringLiteral("Read frame | --");
const QString line5 = QStringLiteral("App runtime | capture=%1 fps delivered=%2")
.arg(info.capture_fps > 0.0 ? QString::number(info.capture_fps, 'f', 2)
: QStringLiteral("--"))
@@ -377,69 +369,74 @@ void MainWindow::appendLog(const QString &message)
ui_->logEdit->appendPlainText(line);
}
void MainWindow::onFrame(const gvfg_frame_t *frame, void *user)
void MainWindow::captureReadLoop()
{
MainWindow *self = static_cast<MainWindow *>(user);
if (!self || !frame)
return;
const uint64_t count = ++self->frameCount_;
if (count <= 5 || (count % 60) == 0)
while (!captureStop_.load(std::memory_order_acquire))
{
const int width = frame->width;
const int height = frame->height;
const uint64_t frameId = frame->frame_id;
QMetaObject::invokeMethod(self,
[self, count, frameId, width, height]()
gvfg_event_t event{};
while (gvfg_poll_event(handle_, &event, 0) == GVFG_OK)
{
const QString type = eventTypeText(event.type);
const uint32_t irqBit = event.irq_bit;
const uint32_t irqMask = event.irq_mask;
const uint64_t timestampNs = event.timestamp_ns;
QMetaObject::invokeMethod(this,
[this, type, irqBit, irqMask, timestampNs]()
{
appendLog(QStringLiteral("event %1 irq=%2 mask=%3 ts=%4")
.arg(type)
.arg(irqBit)
.arg(hex32(irqMask))
.arg(static_cast<qulonglong>(timestampNs)));
},
Qt::QueuedConnection);
}
gvfg_frame_t frame{};
const gvfg_status_t st = gvfg_read_frame(handle_, &frame, 200);
if (st == GVFG_OK)
{
const uint64_t count = ++frameCount_;
const int width = frame.width;
const int height = frame.height;
const uint64_t frameId = frame.frame_id;
previewRenderer_.render(frame);
gvfg_release_frame(handle_, &frame);
if (count <= 5 || (count % 60) == 0)
{
QMetaObject::invokeMethod(this,
[this, count, frameId, width, height]()
{
appendLog(QStringLiteral("read_frame #%1 source id=%2 %3x%4")
.arg(static_cast<qulonglong>(count))
.arg(static_cast<qulonglong>(frameId))
.arg(width)
.arg(height));
},
Qt::QueuedConnection);
}
continue;
}
if (st == GVFG_ETIMEOUT)
continue;
if (captureStop_.load(std::memory_order_acquire) || st == GVFG_ESTATE)
break;
QMetaObject::invokeMethod(this,
[this, st]()
{
self->appendLog(QStringLiteral("callback #%1 source id=%2 %3x%4")
.arg(static_cast<qulonglong>(count))
.arg(static_cast<qulonglong>(frameId))
.arg(width)
.arg(height));
appendLog(QStringLiteral("gvfg_read_frame failed: %1")
.arg(QString::fromUtf8(gvfg_strerror(st))));
},
Qt::QueuedConnection);
break;
}
}
void MainWindow::onEvent(const gvfg_event_t *event, void *user)
void MainWindow::joinCaptureThread()
{
MainWindow *self = static_cast<MainWindow *>(user);
if (!self || !event)
return;
const QString type = eventTypeText(event->type);
const uint32_t irqBit = event->irq_bit;
const uint32_t irqMask = event->irq_mask;
const uint64_t timestampNs = event->timestamp_ns;
QMetaObject::invokeMethod(self,
[self, type, irqBit, irqMask, timestampNs]()
{
self->appendLog(QStringLiteral("event %1 irq=%2 mask=%3 ts=%4")
.arg(type)
.arg(irqBit)
.arg(hex32(irqMask))
.arg(static_cast<qulonglong>(timestampNs)));
},
Qt::QueuedConnection);
}
void MainWindow::onError(gvfg_status_t status, const char *message, void *user)
{
MainWindow *self = static_cast<MainWindow *>(user);
if (!self)
return;
const QString text = message && message[0]
? QString::fromUtf8(message)
: QString::fromUtf8(gvfg_strerror(status));
QMetaObject::invokeMethod(self,
[self, status, text]()
{
self->appendLog(QStringLiteral("message %1: %2")
.arg(static_cast<int>(status))
.arg(text));
},
Qt::QueuedConnection);
if (captureThread_.joinable())
captureThread_.join();
}
+8 -4
View File
@@ -2,12 +2,15 @@
#include <gvfg_capture.h>
#include "sample_preview_renderer.h"
#include <QString>
#include <QWidget>
#include <array>
#include <atomic>
#include <cstdint>
#include <thread>
class QCloseEvent;
class PreviewWindow;
@@ -46,17 +49,18 @@ private:
void updateUiState();
void showError(const QString &apiName, gvfg_status_t status);
void appendLog(const QString &message);
static void onFrame(const gvfg_frame_t *frame, void *user);
static void onEvent(const gvfg_event_t *event, void *user);
static void onError(gvfg_status_t status, const char *message, void *user);
void captureReadLoop();
void joinCaptureThread();
Ui::MainWindow *ui_ = nullptr;
PreviewWindow *previewWindow_ = nullptr;
std::array<gvfg_device_info_t, GVFG_MAX_DEVICES> devices_{};
int deviceCount_ = 0;
gvfg_handle handle_ = nullptr;
SamplePreviewRenderer previewRenderer_;
bool captureRunning_ = false;
std::atomic<bool> captureStop_{false};
std::thread captureThread_;
std::atomic<uint64_t> frameCount_{0};
QTimer *signalStatusTimer_ = nullptr;
QString lastSignalStatusText_;
@@ -0,0 +1,198 @@
#include "sample_preview_renderer.h"
#include "gvfg_render_types.h"
#include "shared_scene_pipeline.h"
#include <d3d11_4.h>
#include <wrl/client.h>
using Microsoft::WRL::ComPtr;
struct SamplePreviewRenderer::D3DState
{
ComPtr<ID3D11Device> device;
ComPtr<ID3D11DeviceContext> context;
};
SamplePreviewRenderer::SamplePreviewRenderer() = default;
SamplePreviewRenderer::~SamplePreviewRenderer()
{
shutdown();
}
bool SamplePreviewRenderer::configure(void *hwnd)
{
std::lock_guard<std::mutex> lock(mutex_);
hwnd_ = hwnd;
configured_ = hwnd_ != nullptr;
if (pipeline_)
{
gvfg::internal::gvfg_render_preview_desc_t desc{};
desc.hwnd = hwnd_;
desc.enable_preview = configured_ ? 1 : 0;
desc.use_fp16_pipeline = 1;
desc.swapchain_10bit = gvfg::internal::GVFG_RENDER_PREVIEW_BITDEPTH_AUTO;
pipeline_->configurePreview(desc);
}
return configured_;
}
bool SamplePreviewRenderer::render(const gvfg_frame_t &frame)
{
std::lock_guard<std::mutex> lock(mutex_);
if (!configured_ || !hwnd_ || !frame.data || frame.width <= 0 || frame.height <= 0)
return false;
if (!ensureDevice() || !ensurePipeline(frame.width, frame.height, frame.bit_depth > 0 ? frame.bit_depth : 8))
return false;
const auto *base = static_cast<const uint8_t *>(frame.data);
gvfg::internal::gvfg_render_pixfmt_t renderFmt = gvfg::internal::GVFG_RENDER_FMT_YUY2;
bool uploaded = false;
switch (frame.pixel_format)
{
case GVFG_PIXFMT_YUY2:
renderFmt = gvfg::internal::GVFG_RENDER_FMT_YUY2;
uploaded = pipeline_->upload_yuy2_frame(base, frame.width * 2, frame.width, frame.height);
break;
case GVFG_PIXFMT_Y210:
renderFmt = gvfg::internal::GVFG_RENDER_FMT_Y210;
uploaded = pipeline_->upload_y210_frame(base, frame.width * 4, frame.width, frame.height);
break;
default:
return false;
}
if (!uploaded ||
!pipeline_->render_uploaded_yuv_to_fp16(renderFmt, frame.width, frame.height) ||
!pipeline_->copy_fp16_to_scene())
return false;
bool ok = true;
if (!pipeline_->preview_swapchain_10bit())
ok = pipeline_->blit_fp16_to_rgba8(frame.width, frame.height);
if (!ok)
return false;
pipeline_->present_preview(frame.width, frame.height);
width_ = pipeline_->preview_w_;
height_ = pipeline_->preview_h_;
bitDepth_ = pipeline_->preview_swapchain_10bit() ? 10 : 8;
swapchain10Bit_ = pipeline_->preview_swapchain_10bit();
return true;
}
void SamplePreviewRenderer::shutdown()
{
std::lock_guard<std::mutex> lock(mutex_);
if (pipeline_)
pipeline_->release_preview_swapchain();
pipeline_.reset();
d3d_.reset();
configured_ = false;
hwnd_ = nullptr;
width_ = 0;
height_ = 0;
bitDepth_ = 0;
swapchain10Bit_ = false;
}
bool SamplePreviewRenderer::active() const
{
std::lock_guard<std::mutex> lock(mutex_);
return configured_ && pipeline_ && width_ > 0 && height_ > 0;
}
int SamplePreviewRenderer::width() const
{
std::lock_guard<std::mutex> lock(mutex_);
return width_;
}
int SamplePreviewRenderer::height() const
{
std::lock_guard<std::mutex> lock(mutex_);
return height_;
}
int SamplePreviewRenderer::bitDepth() const
{
std::lock_guard<std::mutex> lock(mutex_);
return bitDepth_;
}
const char *SamplePreviewRenderer::pixelFormat() const
{
std::lock_guard<std::mutex> lock(mutex_);
return swapchain10Bit_ ? "RGB10A2" : "BGRA8";
}
bool SamplePreviewRenderer::ensureDevice()
{
if (d3d_ && d3d_->device && d3d_->context)
return true;
auto state = std::make_unique<D3DState>();
UINT flags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
#ifdef _DEBUG
flags |= D3D11_CREATE_DEVICE_DEBUG;
#endif
D3D_FEATURE_LEVEL levels[] = {D3D_FEATURE_LEVEL_11_1, D3D_FEATURE_LEVEL_11_0};
D3D_FEATURE_LEVEL got{};
HRESULT hr = D3D11CreateDevice(nullptr,
D3D_DRIVER_TYPE_HARDWARE,
nullptr,
flags,
levels,
_countof(levels),
D3D11_SDK_VERSION,
&state->device,
&got,
&state->context);
#ifdef _DEBUG
if (FAILED(hr))
{
flags &= ~D3D11_CREATE_DEVICE_DEBUG;
hr = D3D11CreateDevice(nullptr,
D3D_DRIVER_TYPE_HARDWARE,
nullptr,
flags,
levels,
_countof(levels),
D3D11_SDK_VERSION,
&state->device,
&got,
&state->context);
}
#endif
if (FAILED(hr) || !state->device || !state->context)
return false;
ComPtr<ID3D11Multithread> mt;
if (SUCCEEDED(state->device.As(&mt)) && mt)
mt->SetMultithreadProtected(TRUE);
d3d_ = std::move(state);
return true;
}
bool SamplePreviewRenderer::ensurePipeline(int width, int height, int sourceBitDepth)
{
if (!pipeline_)
pipeline_ = std::make_unique<gvfg::internal::SharedScenePipeline>();
if (!pipeline_->initialize(d3d_->device.Get(), d3d_->context.Get()))
return false;
gvfg::internal::gvfg_render_preview_desc_t desc{};
desc.hwnd = hwnd_;
desc.enable_preview = configured_ ? 1 : 0;
desc.use_fp16_pipeline = 1;
desc.swapchain_10bit = gvfg::internal::GVFG_RENDER_PREVIEW_BITDEPTH_AUTO;
pipeline_->configurePreview(desc);
pipeline_->set_source_bit_depth(sourceBitDepth > 0 ? sourceBitDepth : 8);
return pipeline_->ensure_rt_and_pipeline(width, height) &&
pipeline_->ensure_preview_swapchain(width, height);
}
@@ -0,0 +1,44 @@
#pragma once
#include <gvfg_capture.h>
#include <memory>
#include <mutex>
namespace gvfg::internal
{
class SharedScenePipeline;
}
class SamplePreviewRenderer
{
public:
SamplePreviewRenderer();
~SamplePreviewRenderer();
bool configure(void *hwnd);
bool render(const gvfg_frame_t &frame);
void shutdown();
bool active() const;
int width() const;
int height() const;
int bitDepth() const;
const char *pixelFormat() const;
private:
bool ensureDevice();
bool ensurePipeline(int width, int height, int sourceBitDepth);
mutable std::mutex mutex_;
void *hwnd_ = nullptr;
bool configured_ = false;
int width_ = 0;
int height_ = 0;
int bitDepth_ = 0;
bool swapchain10Bit_ = false;
struct D3DState;
std::unique_ptr<D3DState> d3d_;
std::unique_ptr<gvfg::internal::SharedScenePipeline> pipeline_;
};