From 8a13e8b0e8413a92e0379b9d55967eedc068a709 Mon Sep 17 00:00:00 2001 From: "Mark.lin" Date: Wed, 10 Jun 2026 15:06:34 +0800 Subject: [PATCH] Use XDMA ring slots for frame delivery --- .gitignore | 1 + sdk/gvfg/include/gvfg_capture.h | 5 - .../src/backend/xdma/xdma_backend_types.h | 14 -- .../src/backend/xdma/xdma_capture_session.cpp | 230 ++++++++++++------ .../src/backend/xdma/xdma_capture_session.h | 17 +- sdk/gvfg/src/gvfg_capture.cpp | 46 +--- 6 files changed, 169 insertions(+), 144 deletions(-) diff --git a/.gitignore b/.gitignore index 520be7a..489cb7d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ build*/ out/ +demosrc/ .vs/ .vscode/ .qtcreator/ diff --git a/sdk/gvfg/include/gvfg_capture.h b/sdk/gvfg/include/gvfg_capture.h index c0c9507..cac58b3 100644 --- a/sdk/gvfg/include/gvfg_capture.h +++ b/sdk/gvfg/include/gvfg_capture.h @@ -190,15 +190,10 @@ typedef struct { const void *data; /* Native frame buffer. Valid only during the frame callback. */ uint64_t data_size; /* Total bytes available from data. */ - int stride; /* Bytes per row for plane 0. */ int width; /* Frame width in pixels. */ int height; /* Frame height in pixels. */ int pixel_format; /* gvfg_pixel_format_t value. */ int bit_depth; /* Bits per color channel of the native frame. */ - int plane_count; /* Number of valid planes. */ - uint32_t plane_offset_bytes[GVFG_MAX_PLANES]; - uint32_t plane_stride_bytes[GVFG_MAX_PLANES]; - uint64_t pts_ns; /* Presentation timestamp in nanoseconds. */ uint64_t frame_id; /* Monotonic frame identifier from the backend. */ } gvfg_frame_t; diff --git a/sdk/gvfg/src/backend/xdma/xdma_backend_types.h b/sdk/gvfg/src/backend/xdma/xdma_backend_types.h index d22afc5..ae91579 100644 --- a/sdk/gvfg/src/backend/xdma/xdma_backend_types.h +++ b/sdk/gvfg/src/backend/xdma/xdma_backend_types.h @@ -36,13 +36,6 @@ typedef enum xdma_pixel_format_t XDMA_PIXFMT_YUV444 = 8 } xdma_pixel_format_t; -typedef enum xdma_memory_kind_t -{ - XDMA_MEMORY_DRIVER_COPY = 0, - XDMA_MEMORY_SHARED_SECTION = 1, - XDMA_MEMORY_DMA_RING = 2 -} xdma_memory_kind_t; - typedef enum xdma_stream_state_t { XDMA_STREAM_STOPPED = 0, @@ -57,7 +50,6 @@ typedef struct xdma_stream_desc_t uint32_t height; xdma_pixel_format_t pixel_format; uint32_t buffer_count; - xdma_memory_kind_t memory_kind; uint32_t flags; } xdma_stream_desc_t; @@ -85,16 +77,10 @@ typedef struct xdma_frame_t const void *data; /* Valid until the next XdmaCaptureSession::wait_frame() or close(). */ size_t data_size_bytes; uint64_t frame_id; - uint64_t timestamp_ns; uint32_t width; uint32_t height; xdma_pixel_format_t pixel_format; uint32_t bit_depth; - uint32_t plane_count; - uint32_t plane_offset_bytes[XDMA_MAX_PLANES]; - uint32_t plane_stride_bytes[XDMA_MAX_PLANES]; - uint32_t driver_buffer_index; - uint32_t flags; } xdma_frame_t; typedef struct xdma_stream_stats_t diff --git a/sdk/gvfg/src/backend/xdma/xdma_capture_session.cpp b/sdk/gvfg/src/backend/xdma/xdma_capture_session.cpp index ec19e3d..cac3ef2 100644 --- a/sdk/gvfg/src/backend/xdma/xdma_capture_session.cpp +++ b/sdk/gvfg/src/backend/xdma/xdma_capture_session.cpp @@ -30,6 +30,8 @@ namespace constexpr int kPlugInScanFrames = 5; constexpr uint32_t kDefaultWidth = 1920; constexpr uint32_t kDefaultHeight = 1080; + constexpr uint32_t kDefaultRingBufferCount = 3; + constexpr uint32_t kMaxRingBufferCount = 16; constexpr DWORD kMaxBytesPerTransfer = 0x800000; // FPGA user BAR register offsets. These match the old CaptureDemo @@ -155,31 +157,6 @@ namespace } } - static uint32_t plane_count_for_pixfmt(xdma_pixel_format_t fmt) - { - return (fmt == XDMA_PIXFMT_NV12 || fmt == XDMA_PIXFMT_P010) ? 2u : 1u; - } - - static uint32_t plane_stride_for_pixfmt(uint32_t width, xdma_pixel_format_t fmt, uint32_t bitDepth) - { - switch (fmt) - { - case XDMA_PIXFMT_NV12: - return width; - case XDMA_PIXFMT_P010: - return width * 2u; - case XDMA_PIXFMT_RGB24: - case XDMA_PIXFMT_YUV444: - return bitDepth > 8u ? width * 6u : width * 3u; - case XDMA_PIXFMT_Y210: - return width * 4u; - case XDMA_PIXFMT_YUY2: - case XDMA_PIXFMT_UYVY: - default: - return width * 2u; - } - } - #if defined(GVFG_XDMA_DEBUG_LOG) static void xdma_debug_log(const char *fmt, ...) { @@ -348,8 +325,7 @@ namespace gvfg::internal stream_desc_.height = kDefaultHeight; stream_desc_.pixel_format = XDMA_PIXFMT_YUY2; stream_bit_depth_ = 8; - stream_desc_.buffer_count = 1; - stream_desc_.memory_kind = XDMA_MEMORY_DRIVER_COPY; + stream_desc_.buffer_count = kDefaultRingBufferCount; reset_stats(stats_, XDMA_STREAM_STOPPED); } @@ -577,13 +553,12 @@ namespace gvfg::internal desc.pixel_format != XDMA_PIXFMT_UNKNOWN) return fail(XDMA_ENOTSUP, "configure_stream(pixel_format)", ERROR_NOT_SUPPORTED); - XDMA_LOG("configure: request input=%u %ux%u fmt=%u buffers=%u mem=%u flags=0x%x", + XDMA_LOG("configure: request input=%u %ux%u fmt=%u buffers=%u flags=0x%x", static_cast(desc.input), desc.width, desc.height, static_cast(desc.pixel_format), desc.buffer_count, - static_cast(desc.memory_kind), desc.flags); // Normalize the requested stream. The render path may support fewer @@ -593,28 +568,29 @@ namespace gvfg::internal stream_desc_.width = desc.width ? desc.width : kDefaultWidth; stream_desc_.height = desc.height ? desc.height : kDefaultHeight; stream_desc_.pixel_format = desc.pixel_format == XDMA_PIXFMT_UNKNOWN ? XDMA_PIXFMT_YUY2 : desc.pixel_format; - uint32_t rawBitDepth = 0; stream_bit_depth_ = bit_depth_for_pixfmt(stream_desc_.pixel_format); - stream_desc_.buffer_count = desc.buffer_count ? desc.buffer_count : 1; - stream_desc_.memory_kind = XDMA_MEMORY_DRIVER_COPY; + stream_desc_.buffer_count = (std::max)(2u, (std::min)(desc.buffer_count ? desc.buffer_count : kDefaultRingBufferCount, + kMaxRingBufferCount)); configured_ = true; { std::lock_guard lock(mutex_); reset_stats(stats_, XDMA_STREAM_CONFIGURED); - latest_frame_.clear(); - delivery_frame_.clear(); + frame_ring_.clear(); + next_write_slot_ = 0; + active_delivery_slot_ = static_cast(-1); latest_sequence_ = 0; delivered_sequence_ = 0; wait_timeout_count_ = 0; } - XDMA_LOG("configure: effective input=%u path=%u %ux%u fmt=%u frame_bytes=%zu", + XDMA_LOG("configure: effective input=%u path=%u %ux%u fmt=%u ring_buffers=%u frame_bytes=%zu", static_cast(stream_desc_.input), active_input_path(), stream_desc_.width, stream_desc_.height, static_cast(stream_desc_.pixel_format), + stream_desc_.buffer_count, frame_size_bytes()); return XDMA_OK; } @@ -678,9 +654,11 @@ namespace gvfg::internal { std::lock_guard lock(mutex_); - dma_buffer_.assign(bytes, 0); - latest_frame_.clear(); - delivery_frame_.clear(); + frame_ring_.assign(stream_desc_.buffer_count, FrameSlot{}); + for (FrameSlot &slot : frame_ring_) + slot.data.assign(bytes, 0); + next_write_slot_ = 0; + active_delivery_slot_ = static_cast(-1); pending_events_ = 0; latest_sequence_ = 0; delivered_sequence_ = 0; @@ -822,7 +800,12 @@ namespace gvfg::internal const auto hasFrame = [this]() { - return latest_sequence_ > delivered_sequence_ || stream_error_ || !running_; + for (const FrameSlot &slot : frame_ring_) + { + if (slot.ready && slot.sequence > delivered_sequence_) + return true; + } + return stream_error_ || !running_; }; if (timeoutMs == 0) @@ -843,55 +826,53 @@ namespace gvfg::internal return XDMA_ETIMEOUT; } - if (!running_ && latest_sequence_ <= delivered_sequence_) + size_t readySlot = frame_ring_.size(); + uint64_t readySequence = delivered_sequence_; + for (size_t i = 0; i < frame_ring_.size(); ++i) + { + const FrameSlot &slot = frame_ring_[i]; + if (slot.ready && slot.sequence > readySequence) + { + readySlot = i; + readySequence = slot.sequence; + } + } + + if (!running_ && readySlot == frame_ring_.size()) { XDMA_LOG("wait_frame: stopped without pending frame"); return XDMA_ESTATE; } - if (stream_error_ && latest_sequence_ <= delivered_sequence_) + if (stream_error_ && readySlot == frame_ring_.size()) { XDMA_LOG("wait_frame: stream error latest=%llu delivered=%llu", static_cast(latest_sequence_), static_cast(delivered_sequence_)); return XDMA_EIO; } - if (latest_sequence_ <= delivered_sequence_) + if (readySlot == frame_ring_.size()) return XDMA_ETIMEOUT; - // Return a stable pointer to delivery_frame_. The pointer is valid - // until the next wait_frame() on this same session or close(). - delivery_frame_ = latest_frame_; - delivered_sequence_ = latest_sequence_; + FrameSlot &slot = frame_ring_[readySlot]; + slot.ready = false; + slot.in_use = true; + active_delivery_slot_ = readySlot; + delivered_sequence_ = slot.sequence; ++stats_.frames_delivered; - out.data = delivery_frame_.data(); - out.data_size_bytes = delivery_frame_.size(); + out.data = slot.data.data(); + out.data_size_bytes = slot.bytes; out.frame_id = delivered_sequence_; - out.timestamp_ns = static_cast( - std::chrono::duration_cast( - std::chrono::steady_clock::now().time_since_epoch()) - .count()); out.width = stream_desc_.width; out.height = stream_desc_.height; out.pixel_format = stream_desc_.pixel_format; out.bit_depth = stream_bit_depth_; - out.plane_count = plane_count_for_pixfmt(stream_desc_.pixel_format); - out.plane_offset_bytes[0] = 0; - out.plane_stride_bytes[0] = plane_stride_for_pixfmt(stream_desc_.width, stream_desc_.pixel_format, stream_bit_depth_); - if (out.plane_count > 1) - { - out.plane_offset_bytes[1] = out.plane_stride_bytes[0] * stream_desc_.height; - out.plane_stride_bytes[1] = out.plane_stride_bytes[0]; - } - out.driver_buffer_index = 0; - out.flags = 0; if (should_log_counter(out.frame_id)) - XDMA_LOG("wait_frame: deliver id=%llu bytes=%zu %ux%u stride=%u captured=%llu delivered=%llu", + XDMA_LOG("wait_frame: deliver id=%llu bytes=%zu %ux%u captured=%llu delivered=%llu", static_cast(out.frame_id), out.data_size_bytes, out.width, out.height, - out.plane_stride_bytes[0], static_cast(stats_.frames_captured), static_cast(stats_.frames_delivered)); return XDMA_OK; @@ -899,6 +880,13 @@ namespace gvfg::internal xdma_status_t XdmaCaptureSession::release_frame(const xdma_frame_t &) { + std::lock_guard lock(mutex_); + if (active_delivery_slot_ < frame_ring_.size()) + { + frame_ring_[active_delivery_slot_].in_use = false; + active_delivery_slot_ = static_cast(-1); + } + data_cv_.notify_all(); return XDMA_OK; } @@ -1065,32 +1053,75 @@ namespace gvfg::internal continue; const DWORD bytes = static_cast(frame_size_bytes()); - if (dma_buffer_.size() < bytes) + size_t slotIndex = frame_ring_.size(); + uint8_t *slotData = nullptr; { - std::lock_guard lock(mutex_); - if (dma_buffer_.size() < bytes) - dma_buffer_.resize(bytes); + std::unique_lock lock(mutex_); + data_cv_.wait(lock, [this]() + { + if (!running_ || data_worker_stop_) + return true; + for (const FrameSlot &slot : frame_ring_) + { + if (!slot.in_use) + return true; + } + return false; + }); + if (!running_ || data_worker_stop_) + break; + + for (size_t attempt = 0; attempt < frame_ring_.size(); ++attempt) + { + const size_t candidate = (next_write_slot_ + attempt) % frame_ring_.size(); + if (!frame_ring_[candidate].in_use) + { + slotIndex = candidate; + break; + } + } + if (slotIndex == frame_ring_.size()) + continue; + + FrameSlot &slot = frame_ring_[slotIndex]; + if (slot.ready && slot.sequence > delivered_sequence_) + ++stats_.frames_dropped; + slot.ready = false; + slot.in_use = true; + if (slot.data.size() < bytes) + slot.data.resize(bytes); + slotData = slot.data.data(); + next_write_slot_ = (slotIndex + 1) % frame_ring_.size(); } - const int ret = read_device(c2h_device_[active_input_path()], 0, bytes, dma_buffer_.data()); + const int ret = read_device(c2h_device_[active_input_path()], 0, bytes, slotData); if (!running_) break; if (!capture_active_) + { + std::lock_guard lock(mutex_); + if (slotIndex < frame_ring_.size()) + frame_ring_[slotIndex].in_use = false; + data_cv_.notify_all(); continue; + } if (ret < 0 || static_cast(ret) != bytes) { fail(XDMA_EIO, "ReadFile(c2h)"); std::lock_guard lock(mutex_); + if (slotIndex < frame_ring_.size()) + frame_ring_[slotIndex].in_use = false; stream_error_ = true; ++stats_.dma_errors; + data_cv_.notify_all(); frame_cv_.notify_all(); break; } - handle_plug_in_frame_fix(dma_buffer_.data(), static_cast(ret)); + handle_plug_in_frame_fix(slotData, static_cast(ret)); std::lock_guard lock(mutex_); - publish_frame(dma_buffer_.data(), static_cast(ret)); + publish_frame(slotIndex, static_cast(ret)); } XDMA_LOG("data_thread: exit running=%d", running_ ? 1 : 0); } @@ -1203,9 +1234,17 @@ namespace gvfg::internal uint64_t deliveredAfterPause = 0; { std::lock_guard lock(mutex_); - dma_buffer_.clear(); - latest_frame_.clear(); - delivery_frame_.clear(); + for (FrameSlot &slot : frame_ring_) + { + if (!slot.in_use) + { + slot.bytes = 0; + slot.sequence = 0; + slot.ready = false; + } + } + next_write_slot_ = 0; + active_delivery_slot_ = static_cast(-1); latest_sequence_ = delivered_sequence_; latestAfterPause = latest_sequence_; deliveredAfterPause = delivered_sequence_; @@ -1263,7 +1302,26 @@ namespace gvfg::internal { std::lock_guard lock(mutex_); const size_t bytes = frame_size_bytes(); - dma_buffer_.assign(bytes, 0); + bool anyInUse = false; + for (FrameSlot &slot : frame_ring_) + anyInUse = anyInUse || slot.in_use; + if (!anyInUse && frame_ring_.size() != stream_desc_.buffer_count) + frame_ring_.assign(stream_desc_.buffer_count, FrameSlot{}); + if (frame_ring_.empty()) + frame_ring_.assign(stream_desc_.buffer_count, FrameSlot{}); + for (FrameSlot &slot : frame_ring_) + { + if (!slot.in_use) + { + slot.data.assign(bytes, 0); + slot.bytes = 0; + slot.sequence = 0; + slot.ready = false; + } + } + next_write_slot_ = 0; + if (active_delivery_slot_ >= frame_ring_.size()) + active_delivery_slot_ = static_cast(-1); allocatedBytes = bytes; pending_events_ = 0; stream_error_ = false; @@ -1381,20 +1439,30 @@ namespace gvfg::internal statusAfterLow); } - void XdmaCaptureSession::publish_frame(const uint8_t *data, size_t bytes) + void XdmaCaptureSession::publish_frame(size_t slotIndex, size_t bytes) { - // Keep only the newest frame. This makes wait_frame() simple and avoids - // unbounded queue growth if the consumer is slower than the device. - latest_frame_.assign(data, data + bytes); + if (slotIndex >= frame_ring_.size()) + return; + + // Keep only ring-backed frames. Older ready slots may be overwritten by + // the data thread if the consumer is slower than the device. + FrameSlot &slot = frame_ring_[slotIndex]; + slot.bytes = bytes; + slot.sequence = latest_sequence_ + 1; + slot.ready = true; + slot.in_use = false; ++latest_sequence_; ++stats_.frames_captured; stats_.state = XDMA_STREAM_RUNNING; if (should_log_counter(latest_sequence_)) - XDMA_LOG("publish_frame: id=%llu bytes=%zu captured=%llu", + XDMA_LOG("publish_frame: id=%llu slot=%zu bytes=%zu captured=%llu dropped=%llu", static_cast(latest_sequence_), + slotIndex, bytes, - static_cast(stats_.frames_captured)); + static_cast(stats_.frames_captured), + static_cast(stats_.frames_dropped)); frame_cv_.notify_one(); + data_cv_.notify_all(); } int XdmaCaptureSession::read_device(HANDLE device, long address, DWORD size, uint8_t *buffer) const diff --git a/sdk/gvfg/src/backend/xdma/xdma_capture_session.h b/sdk/gvfg/src/backend/xdma/xdma_capture_session.h index 081876f..060469c 100644 --- a/sdk/gvfg/src/backend/xdma/xdma_capture_session.h +++ b/sdk/gvfg/src/backend/xdma/xdma_capture_session.h @@ -62,7 +62,7 @@ namespace gvfg::internal void data_thread_proc(); bool start_data_worker(); void stop_data_worker(); - void publish_frame(const uint8_t *data, size_t bytes); + void publish_frame(size_t slotIndex, size_t bytes); void pause_capture_for_plug_out(); void resume_capture_after_plug_in(); void handle_plug_in_frame_fix(const uint8_t *data, size_t bytes); @@ -82,6 +82,15 @@ namespace gvfg::internal long capture_enable_reg() const; size_t frame_size_bytes() const; + struct FrameSlot + { + std::vector data; // Frame byte storage for this ring slot. + 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 friendly_name_; @@ -112,9 +121,9 @@ namespace gvfg::internal void *event_callback_user_ = nullptr; uint32_t event_mask_filter_ = XDMA_EVENT_MASK_DEFAULT; uint32_t pending_events_ = 0; - std::vector dma_buffer_; - std::vector latest_frame_; - std::vector delivery_frame_; + std::vector frame_ring_; + size_t next_write_slot_ = 0; + size_t active_delivery_slot_ = static_cast(-1); uint64_t latest_sequence_ = 0; uint64_t delivered_sequence_ = 0; uint64_t wait_timeout_count_ = 0; diff --git a/sdk/gvfg/src/gvfg_capture.cpp b/sdk/gvfg/src/gvfg_capture.cpp index 2851fb1..ecb81eb 100644 --- a/sdk/gvfg/src/gvfg_capture.cpp +++ b/sdk/gvfg/src/gvfg_capture.cpp @@ -607,8 +607,7 @@ struct gvfg_handle_t desc.width = width; desc.height = height; desc.pixel_format = pixelFormat; - desc.buffer_count = 1; - desc.memory_kind = XDMA_MEMORY_DRIVER_COPY; + desc.buffer_count = 3; const xdma_status_t st = backend->configure_stream(desc); if (st != XDMA_OK) @@ -714,7 +713,7 @@ struct gvfg_handle_t break; } - updateRuntimeFps(frame.timestamp_ns ? frame.timestamp_ns : now_ns()); + updateRuntimeFps(now_ns()); renderGpuFrame(frame); emitNativeFrame(frame); backend->release_frame(frame); @@ -748,44 +747,19 @@ struct gvfg_handle_t { case XDMA_PIXFMT_YUY2: { - const int stride = frame.plane_stride_bytes[0] ? static_cast(frame.plane_stride_bytes[0]) : w * 2; - if (stride < w * 2) - return false; renderFmt = GVFG_RENDER_FMT_YUY2; - uploaded = pipeline->upload_yuy2_frame(base + frame.plane_offset_bytes[0], stride, w, h); + uploaded = pipeline->upload_yuy2_frame(base, w * 2, w, h); break; } case XDMA_PIXFMT_Y210: { - const int stride = frame.plane_stride_bytes[0] ? static_cast(frame.plane_stride_bytes[0]) : w * 4; - if (stride < w * 4) - return false; renderFmt = GVFG_RENDER_FMT_Y210; - uploaded = pipeline->upload_y210_frame(base + frame.plane_offset_bytes[0], stride, w, h); + uploaded = pipeline->upload_y210_frame(base, w * 4, w, h); break; } case XDMA_PIXFMT_NV12: - { - if (frame.plane_count < 2) - return false; - const int strideY = frame.plane_stride_bytes[0] ? static_cast(frame.plane_stride_bytes[0]) : w; - const int strideUV = frame.plane_stride_bytes[1] ? static_cast(frame.plane_stride_bytes[1]) : strideY; - renderFmt = GVFG_RENDER_FMT_NV12; - uploaded = pipeline->upload_nv12_frame(base + frame.plane_offset_bytes[0], strideY, - base + frame.plane_offset_bytes[1], strideUV, w, h); - break; - } case XDMA_PIXFMT_P010: - { - if (frame.plane_count < 2) - return false; - const int strideY = frame.plane_stride_bytes[0] ? static_cast(frame.plane_stride_bytes[0]) : w * 2; - const int strideUV = frame.plane_stride_bytes[1] ? static_cast(frame.plane_stride_bytes[1]) : strideY; - renderFmt = GVFG_RENDER_FMT_P010; - uploaded = pipeline->upload_p010_frame(base + frame.plane_offset_bytes[0], strideY, - base + frame.plane_offset_bytes[1], strideUV, w, h); - break; - } + return false; default: return false; } @@ -810,24 +784,16 @@ struct gvfg_handle_t if (!shouldEmitFrameCallback(frame.frame_id)) return; - if (frame.width == 0 || frame.height == 0 || frame.plane_count == 0) + if (frame.width == 0 || frame.height == 0) return; gvfg_frame_t out{}; out.data = frame.data; out.data_size = static_cast(frame.data_size_bytes); - out.stride = static_cast(frame.plane_stride_bytes[0]); out.width = static_cast(frame.width); out.height = static_cast(frame.height); out.pixel_format = to_gvfg_pixel_format(frame.pixel_format); out.bit_depth = static_cast(frame.bit_depth); - out.plane_count = static_cast((std::min)(frame.plane_count, static_cast(GVFG_MAX_PLANES))); - for (int i = 0; i < out.plane_count; ++i) - { - out.plane_offset_bytes[i] = frame.plane_offset_bytes[i]; - out.plane_stride_bytes[i] = frame.plane_stride_bytes[i]; - } - out.pts_ns = frame.timestamp_ns ? frame.timestamp_ns : now_ns(); out.frame_id = frame.frame_id; onFrame(&out, callbackUser); noteDeliveredFrame(out.width, out.height, out.bit_depth, out.pixel_format);