Add dual-slot font-selectable OSD and privatize playback diagnostics for SDK 3.0.0

- replace the single-overlay API with two independently configured OSD slots per channel, simplify NUL-terminated text/font settings, and add explicit visibility controls that preserve cached glyphs separately from Clear

- enumerate installed Windows font families as an SDK-owned UTF-8 list without callbacks or caller-side free, refresh installed fonts on demand, and use the selected family consistently for measurement and rendering with invalid/missing-font rejection

- retain per-slot YUV and A8 mask caches, blend slot 1 over slot 0 in one GPU dispatch/readback, preserve hidden surfaces, and skip oversized slots independently while capture and the other slot continue

- move detailed playback core statistics to a private Debug-only header/export, remove the public legacy statistics query and compatibility layouts, and retain public preview/delivery statistics

- bump the SDK major version to 3.0.0 for the breaking OSD and playback API changes and document matching header, import-library and runtime deployment requirements

- refresh the capture/playback guides, internal build notes and API reference, document all 51 public Capture/Playback functions, add checked OSD usage examples, and regenerate the branded 23-page PDF

Validation: git diff --cached --check and public API documentation coverage checks passed. The regenerated PDF passed text/content checks and visual review of its rendered pages, including the OSD types, API contracts and examples. Earlier session SDK Release compilation and synthetic YUY2/Y210 blending checks passed; no new build, hardware capture, FPGA upload or long-duration runtime validation was performed during this commit pass.
This commit is contained in:
Mark.lin
2026-10-29 09:02:54 +08:00
parent bfa9397f6b
commit c8b2d0608a
24 changed files with 796 additions and 482 deletions
+2
View File
@@ -8,6 +8,8 @@ EXPORTS
gvfg_set_channel_zero_copy_enabled
gvfg_get_channel_zero_copy_enabled
gvfg_set_channel_video_format
gvfg_get_osd_fonts
gvfg_set_channel_osd_visible
gvfg_measure_osd_text
gvfg_set_channel_osd
gvfg_clear_channel_osd
+45 -33
View File
@@ -207,21 +207,24 @@ extern "C"
uint64_t timestamp_ns; /* Monotonic SDK delivery time; same clock domain as audio. */
} gvfg_frame_t;
/*
* Text overlay applied by the SDK to subsequently delivered video frames.
* The text is copied by gvfg_set_channel_osd(); the caller does not need to
* keep the pointer alive after that function returns.
*/
typedef struct
/* Strings are copied synchronously by gvfg_set_channel_osd(). */
typedef struct gvfg_channel_osd_config_t
{
const char *text; /* Printable English ASCII text (0x20-0x7e). */
uint32_t text_length; /* 1 through GVFG_OSD_MAX_TEXT_CHARACTERS. */
int32_t x; /* Measured text-box left in frame pixels; clamped to fit. */
int32_t y; /* Measured text-box top in frame pixels; clamped to fit. */
uint32_t font_size_px; /* Font size in pixels; must be non-zero. */
uint32_t color_rgb; /* 0x00RRGGBB. */
const char *text; /* NUL-terminated printable ASCII, 1..32 characters. */
const char *font_family; /* NUL-terminated UTF-8 installed font family name. */
uint32_t font_size_px; /* Non-zero em size; 96-DPI rendering, not ink height. */
uint32_t color_rgb; /* 0x00RRGGBB. */
int32_t x; /* Text-box left in frame pixels; clamped to fit. */
int32_t y; /* Text-box top in frame pixels; clamped to fit. */
} gvfg_channel_osd_config_t;
typedef struct gvfg_osd_font_list_t
{
uint32_t count;
const char *const *names; /* SDK-owned UTF-8 family names; do not modify/free. */
} gvfg_osd_font_list_t;
#define GVFG_OSD_SLOT_COUNT 2u
#define GVFG_OSD_MAX_TEXT_CHARACTERS 32u
typedef struct
@@ -368,40 +371,49 @@ extern "C"
GVFG_PARAM_IN int channel_index,
GVFG_PARAM_IN gvfg_pixel_format_t format);
/*
* Measure printable ASCII text as a single non-wrapping line, using the
* same font/layout as SDK OSD. No capture handle or open device is needed.
* Returns a conservative whole-text box in pixels, including glyph
* overhang and a one-pixel guard per edge; these are not pixel-tight bounds.
* Both outputs must be non-NULL and are zeroed on failure.
* Returns GVFG_EINVAL for invalid input or unrepresentable dimensions,
* and GVFG_EIO on measurement failure.
/* Refresh installed fonts and return an SDK-owned snapshot. No device needed.
* Pointers expire at the next call (including a failed call), or DLL unload.
* Copy names before then. Serialize calls and use of this borrowed list.
* Returns an empty list on failure; no callback or caller-side free is needed.
*/
GVFG_API gvfg_status_t gvfg_get_osd_fonts(
GVFG_PARAM_OUT gvfg_osd_font_list_t *out_list);
/* Measure the same conservative text box used for rendering, including
* overhang and a one-pixel guard per edge. Ignores x/y/color; no device needed.
* Outputs are zeroed on failure. Missing font/invalid input: GVFG_EINVAL.
*/
GVFG_API gvfg_status_t gvfg_measure_osd_text(
GVFG_PARAM_IN const char *text,
GVFG_PARAM_IN uint32_t text_length,
GVFG_PARAM_IN uint32_t font_size_px,
GVFG_PARAM_IN const gvfg_channel_osd_config_t *config,
GVFG_PARAM_OUT uint32_t *out_width,
GVFG_PARAM_OUT uint32_t *out_height);
/*
* Enable or update SDK-side OSD for an opened channel. The configuration
* is copied synchronously and applies to subsequent YUY2 or Y210 frames.
* Text is rendered as BT.709 limited-range video and blended by the GPU.
* The single-line measured text box is positioned at x/y, clamped inside
* each frame. If the whole box is larger than the frame, OSD is temporarily
* skipped while capture continues. It resumes when settings or frame size
* change so that the text fits; text is not wrapped, shrunk, or truncated.
/* Set and show slot 0 or 1 on an opened channel. Strings are copied before
* return; slot 1 is blended over slot 0. Applies to subsequent YUY2/Y210
* frames using BT.709 limited range. X/Y are clamped to fit the text box.
* An oversized slot is skipped until settings/frame size allow it to fit;
* capture and the other slot continue. No wrapping/shrinking/truncation.
*/
GVFG_API gvfg_status_t gvfg_set_channel_osd(
GVFG_PARAM_IN gvfg_handle handle,
GVFG_PARAM_IN int channel_index,
GVFG_PARAM_IN uint32_t osd_index,
GVFG_PARAM_IN const gvfg_channel_osd_config_t *config);
/* Disable SDK-side OSD for subsequently delivered frames. */
/* visible must be 0 or 1. Retains settings and cached glyph surfaces.
* Showing an unconfigured/cleared slot returns GVFG_ESTATE.
*/
GVFG_API gvfg_status_t gvfg_set_channel_osd_visible(
GVFG_PARAM_IN gvfg_handle handle,
GVFG_PARAM_IN int channel_index,
GVFG_PARAM_IN uint32_t osd_index,
GVFG_PARAM_IN int visible);
/* Disable one slot and discard its settings and cached glyph surfaces. */
GVFG_API gvfg_status_t gvfg_clear_channel_osd(
GVFG_PARAM_IN gvfg_handle handle,
GVFG_PARAM_IN int channel_index);
GVFG_PARAM_IN int channel_index,
GVFG_PARAM_IN uint32_t osd_index);
/*
* Enable or disable audio for the next channel start. Video remains
+224 -202
View File
@@ -7,6 +7,7 @@
#include <wrl/client.h>
#include <algorithm>
#include <array>
#include <chrono>
#include <cstdint>
#include <cstring>
@@ -75,12 +76,15 @@ constexpr char kOsdBlendShader[] = R"(
Texture2D<uint4> sourceTexture : register(t0);
Texture2D<uint4> osdTexture : register(t1);
Texture2D<uint2> maskTexture : register(t2);
Texture2D<uint4> osdTexture1 : register(t3);
Texture2D<uint2> maskTexture1 : register(t4);
RWTexture2D<uint4> outputTexture : register(u0);
cbuffer BlendParameters : register(b0)
{
uint outputValueMask;
uint3 padding;
uint visibleSlots;
uint2 padding;
};
uint blendComponent(uint sourceValue, uint osdValue, uint alpha)
@@ -89,6 +93,17 @@ uint blendComponent(uint sourceValue, uint osdValue, uint alpha)
outputValueMask;
}
uint4 blendLayer(uint4 sourceValue, uint4 osdValue, uint2 maskValue)
{
uint pairAlpha = max(maskValue.x, maskValue.y);
uint4 result;
result.x = blendComponent(sourceValue.x, osdValue.x, maskValue.x);
result.y = blendComponent(sourceValue.y, osdValue.y, pairAlpha);
result.z = blendComponent(sourceValue.z, osdValue.z, maskValue.y);
result.w = blendComponent(sourceValue.w, osdValue.w, pairAlpha);
return result;
}
[numthreads(16, 16, 1)]
void main(uint3 id : SV_DispatchThreadID)
{
@@ -97,24 +112,13 @@ void main(uint3 id : SV_DispatchThreadID)
outputTexture.GetDimensions(width, height);
if (id.x >= width || id.y >= height)
return;
const uint2 location = id.xy;
const uint4 sourceValue = sourceTexture.Load(uint3(location, 0));
const uint2 maskValue = maskTexture.Load(uint3(location, 0));
const uint pairAlpha = max(maskValue.x, maskValue.y);
if (pairAlpha == 0u)
{
outputTexture[location] = sourceValue;
return;
}
const uint4 osdValue = osdTexture.Load(uint3(location, 0));
uint4 result;
result.x = blendComponent(sourceValue.x, osdValue.x, maskValue.x);
result.y = blendComponent(sourceValue.y, osdValue.y, pairAlpha);
result.z = blendComponent(sourceValue.z, osdValue.z, maskValue.y);
result.w = blendComponent(sourceValue.w, osdValue.w, pairAlpha);
outputTexture[location] = result;
const uint3 location = uint3(id.xy, 0);
uint4 result = sourceTexture.Load(location);
if ((visibleSlots & 1u) != 0u)
result = blendLayer(result, osdTexture.Load(location), maskTexture.Load(location));
if ((visibleSlots & 2u) != 0u)
result = blendLayer(result, osdTexture1.Load(location), maskTexture1.Load(location));
outputTexture[id.xy] = result;
}
)";
@@ -195,36 +199,70 @@ void copyFrameRows(const gvfg_frame_t &source, uint8_t *destination,
struct GpuOsdProcessor::Impl
{
gvfg_status_t configure(const gvfg_channel_osd_config_t &value)
struct Slot
{
if (!value.text || value.text_length == 0 ||
value.text_length > GVFG_OSD_MAX_TEXT_CHARACTERS ||
value.font_size_px == 0)
return GVFG_EINVAL;
for (uint32_t i = 0; i < value.text_length; ++i)
{
const unsigned char c = static_cast<unsigned char>(value.text[i]);
if (c < 0x20 || c > 0x7e)
return GVFG_EINVAL;
}
bool configured = false;
bool visible = false;
bool dirty = true;
std::string text;
std::string fontFamily;
int32_t x = 0, y = 0;
uint32_t fontSize = 0, colorRgb = 0xffffffu;
uint32_t width = 0, height = 0;
int format = GVFG_PIXFMT_UNKNOWN;
OsdRegion region;
std::vector<uint8_t> yuv, mask;
ComPtr<ID3D11Texture2D> texture, maskTexture;
ComPtr<ID3D11ShaderResourceView> srv, maskSrv;
};
gvfg_status_t configure(uint32_t index, const gvfg_channel_osd_config_t &value)
{
if (index >= slots.size())
return GVFG_EINVAL;
uint32_t width = 0, height = 0;
const auto status = gvfg_measure_osd_text(&value, &width, &height);
if (status != GVFG_OK)
return status;
// Allocate copies before changing state so failures retain the old slot.
std::string text(value.text), family(value.font_family);
std::lock_guard<std::mutex> lock(mutex);
text.assign(value.text, value.text + value.text_length);
x = value.x;
y = value.y;
fontSize = value.font_size_px;
colorRgb = value.color_rgb & 0x00ffffffu;
active = true;
auto &slot = slots[index];
const bool changed = !slot.configured || slot.text != text || slot.fontFamily != family ||
slot.x != value.x || slot.y != value.y || slot.fontSize != value.font_size_px ||
slot.colorRgb != (value.color_rgb & 0x00ffffffu);
slot.text.swap(text);
slot.fontFamily.swap(family);
slot.x = value.x;
slot.y = value.y;
slot.fontSize = value.font_size_px;
slot.colorRgb = value.color_rgb & 0x00ffffffu;
slot.configured = slot.visible = true;
slot.dirty = slot.dirty || changed;
surfaceDirty = surfaceDirty || changed;
return GVFG_OK;
}
gvfg_status_t clear(uint32_t index)
{
if (index >= slots.size())
return GVFG_EINVAL;
std::lock_guard<std::mutex> lock(mutex);
slots[index] = Slot{};
surfaceDirty = true;
return GVFG_OK;
}
void clear()
gvfg_status_t setVisible(uint32_t index, int visible)
{
if (index >= slots.size() || (visible != 0 && visible != 1))
return GVFG_EINVAL;
std::lock_guard<std::mutex> lock(mutex);
active = false;
text.clear();
surfaceDirty = true;
if (!slots[index].configured && visible)
return GVFG_ESTATE;
slots[index].visible = visible != 0;
// Keep rasterized surfaces and GPU textures intact.
return GVFG_OK;
}
bool ensureDevice()
@@ -305,169 +343,152 @@ struct GpuOsdProcessor::Impl
bool ensureResources(uint32_t regionWidth, uint32_t regionHeight, int pixelFormat)
{
if ((regionWidth & 1u) != 0 || regionWidth == 0 || regionHeight == 0)
return false;
const DXGI_FORMAT packedFormat = pixelFormat == GVFG_PIXFMT_YUY2
? DXGI_FORMAT_R8G8B8A8_UINT
: pixelFormat == GVFG_PIXFMT_Y210
? DXGI_FORMAT_R16G16B16A16_UINT
: DXGI_FORMAT_UNKNOWN;
if (packedFormat == DXGI_FORMAT_UNKNOWN)
return false;
if (resourceWidth != regionWidth || resourceHeight != regionHeight ||
resourceFormat != pixelFormat || !sourceSrv || !osdSrv || !maskSrv || !outputUav)
? DXGI_FORMAT_R8G8B8A8_UINT : DXGI_FORMAT_R16G16B16A16_UINT;
const bool changed = resourceWidth != regionWidth || resourceHeight != regionHeight ||
resourceFormat != pixelFormat;
if (changed)
{
// Failed partial recreation must never leave views cached against
// old textures, including when the next request returns to an old size.
sourceSrv.Reset();
osdSrv.Reset();
maskSrv.Reset();
outputUav.Reset();
resourceWidth = 0;
resourceHeight = 0;
for (auto &slot : slots) { slot.srv.Reset(); slot.maskSrv.Reset(); }
resourceWidth = resourceHeight = 0;
resourceFormat = GVFG_PIXFMT_UNKNOWN;
}
const uint32_t pairWidth = regionWidth / 2u;
if (!ensureTexture(sourceTexture, pairWidth, regionHeight, packedFormat,
D3D11_USAGE_DYNAMIC, D3D11_BIND_SHADER_RESOURCE,
D3D11_CPU_ACCESS_WRITE) ||
!ensureTexture(osdTexture, pairWidth, regionHeight, packedFormat,
D3D11_USAGE_DYNAMIC, D3D11_BIND_SHADER_RESOURCE,
D3D11_CPU_ACCESS_WRITE) ||
!ensureTexture(maskTexture, pairWidth, regionHeight, DXGI_FORMAT_R8G8_UINT,
D3D11_USAGE_DYNAMIC, D3D11_BIND_SHADER_RESOURCE,
D3D11_CPU_ACCESS_WRITE) ||
D3D11_USAGE_DYNAMIC, D3D11_BIND_SHADER_RESOURCE, D3D11_CPU_ACCESS_WRITE) ||
!ensureTexture(outputTexture, pairWidth, regionHeight, packedFormat,
D3D11_USAGE_DEFAULT, D3D11_BIND_UNORDERED_ACCESS, 0) ||
D3D11_USAGE_DEFAULT, D3D11_BIND_UNORDERED_ACCESS, 0) ||
!ensureTexture(readbackTexture, pairWidth, regionHeight, packedFormat,
D3D11_USAGE_STAGING, 0, D3D11_CPU_ACCESS_READ))
D3D11_USAGE_STAGING, 0, D3D11_CPU_ACCESS_READ))
return false;
if (!sourceSrv || resourceWidth != regionWidth || resourceHeight != regionHeight ||
resourceFormat != pixelFormat)
for (auto &slot : slots)
{
sourceSrv.Reset();
osdSrv.Reset();
maskSrv.Reset();
outputUav.Reset();
if (FAILED(device->CreateShaderResourceView(sourceTexture.Get(), nullptr, &sourceSrv)) ||
FAILED(device->CreateShaderResourceView(osdTexture.Get(), nullptr, &osdSrv)) ||
FAILED(device->CreateShaderResourceView(maskTexture.Get(), nullptr, &maskSrv)) ||
FAILED(device->CreateUnorderedAccessView(outputTexture.Get(), nullptr, &outputUav)))
if (!ensureTexture(slot.texture, pairWidth, regionHeight, packedFormat,
D3D11_USAGE_DYNAMIC, D3D11_BIND_SHADER_RESOURCE, D3D11_CPU_ACCESS_WRITE) ||
!ensureTexture(slot.maskTexture, pairWidth, regionHeight, DXGI_FORMAT_R8G8_UINT,
D3D11_USAGE_DYNAMIC, D3D11_BIND_SHADER_RESOURCE, D3D11_CPU_ACCESS_WRITE))
return false;
if ((!slot.srv && FAILED(device->CreateShaderResourceView(slot.texture.Get(), nullptr, &slot.srv))) ||
(!slot.maskSrv && FAILED(device->CreateShaderResourceView(slot.maskTexture.Get(), nullptr, &slot.maskSrv))))
return false;
resourceWidth = regionWidth;
resourceHeight = regionHeight;
resourceFormat = pixelFormat;
}
if ((!sourceSrv && FAILED(device->CreateShaderResourceView(sourceTexture.Get(), nullptr, &sourceSrv))) ||
(!outputUav && FAILED(device->CreateUnorderedAccessView(outputTexture.Get(), nullptr, &outputUav))))
return false;
resourceWidth = regionWidth;
resourceHeight = regionHeight;
resourceFormat = pixelFormat;
return true;
}
bool rebuildSurface(uint32_t frameWidth, uint32_t frameHeight, int pixelFormat)
bool rebuildSlot(Slot &slot, uint32_t width, uint32_t height, int format)
{
// Any failed rebuild must be retried, even if input dimensions then
// return to the previously cached frame size.
surfaceDirty = true;
const uint32_t yuvStride = frameWidth * (pixelFormat == GVFG_PIXFMT_YUY2 ? 2u : 4u);
const uint32_t maskStride = frameWidth;
gvfg_osd_handle rawHandle = nullptr;
if (gvfg_osd_create(&rawHandle) != GVFG_OSD_OK)
return false;
std::unique_ptr<void, decltype(&gvfg_osd_destroy)> handle(rawHandle, &gvfg_osd_destroy);
uint32_t textWidth = 0;
uint32_t textHeight = 0;
const gvfg_osd_status_t measureStatus = gvfg_osd_measure(
handle.get(), text.c_str(), static_cast<uint32_t>(text.size()),
fontSize, &textWidth, &textHeight);
// Stored input already passed configure() validation. EINVAL here
// means the measured box cannot be represented by uint32_t either.
if (measureStatus != GVFG_OSD_OK && measureStatus != GVFG_OSD_EINVAL)
return false;
if (measureStatus == GVFG_OSD_EINVAL ||
textWidth > frameWidth || textHeight > frameHeight)
{
// Retain the configuration and retry only after settings or the
// input dimensions/format change. Capture must continue normally.
region = {};
surfaceWidth = frameWidth;
surfaceHeight = frameHeight;
surfaceFormat = pixelFormat;
surfaceDirty = false;
if (!slot.dirty && slot.width == width && slot.height == height && slot.format == format)
return true;
}
yuvSurface.resize(static_cast<size_t>(yuvStride) * frameHeight);
maskSurface.resize(static_cast<size_t>(maskStride) * frameHeight);
gvfg_osd_config_t config{};
config.struct_size = sizeof(config);
config.width = frameWidth;
config.height = frameHeight;
config.pixel_format = pixelFormat == GVFG_PIXFMT_YUY2
? GVFG_OSD_PIXFMT_YUY2
: GVFG_OSD_PIXFMT_Y210;
config.text = text.c_str();
config.text_length = static_cast<uint32_t>(text.size());
config.x = x;
config.y = y;
config.font_size_px = fontSize;
config.color_rgb = colorRgb;
gvfg_osd_output_t output{};
output.struct_size = sizeof(output);
output.yuv_data = yuvSurface.data();
output.yuv_size = yuvSurface.size();
output.yuv_stride = yuvStride;
output.mask_data = maskSurface.data();
output.mask_size = maskSurface.size();
output.mask_stride = maskStride;
const gvfg_osd_status_t status = gvfg_osd_render(handle.get(), &config, &output);
handle.reset();
if (status != GVFG_OSD_OK)
return false;
region = findCoveredRegion(maskSurface, frameWidth, frameHeight);
if (region.empty())
const uint32_t stride = width * (format == GVFG_PIXFMT_YUY2 ? 2u : 4u);
slot.yuv.assign(static_cast<size_t>(stride) * height, 0);
slot.mask.assign(static_cast<size_t>(width) * height, 0);
slot.region = {};
if (slot.configured)
{
surfaceWidth = frameWidth;
surfaceHeight = frameHeight;
surfaceFormat = pixelFormat;
surfaceDirty = false;
return true;
gvfg_osd_handle raw = nullptr;
if (gvfg_osd_create(&raw) != GVFG_OSD_OK)
return false;
std::unique_ptr<void, decltype(&gvfg_osd_destroy)> helper(raw, &gvfg_osd_destroy);
uint32_t textWidth = 0, textHeight = 0;
const auto measured = gvfg_osd_measure(raw, slot.text.c_str(),
static_cast<uint32_t>(slot.text.size()), slot.fontSize, slot.fontFamily.c_str(),
&textWidth, &textHeight);
if (measured != GVFG_OSD_OK)
return false;
// Oversized text skips only this slot, preserving capture and the other slot.
if (textWidth <= width && textHeight <= height)
{
gvfg_osd_config_t config{};
config.struct_size = sizeof(config);
config.width = width;
config.height = height;
config.pixel_format = format == GVFG_PIXFMT_YUY2 ? GVFG_OSD_PIXFMT_YUY2 : GVFG_OSD_PIXFMT_Y210;
config.text = slot.text.c_str();
config.text_length = static_cast<uint32_t>(slot.text.size());
config.font_family = slot.fontFamily.c_str();
config.font_size_px = slot.fontSize;
config.color_rgb = slot.colorRgb;
config.x = slot.x;
config.y = slot.y;
gvfg_osd_output_t output{};
output.struct_size = sizeof(output);
output.yuv_data = slot.yuv.data();
output.yuv_size = slot.yuv.size();
output.yuv_stride = stride;
output.mask_data = slot.mask.data();
output.mask_size = slot.mask.size();
output.mask_stride = width;
if (gvfg_osd_render(raw, &config, &output) != GVFG_OSD_OK)
return false;
slot.region = findCoveredRegion(slot.mask, width, height);
}
}
if (!ensureDevice() || !ensureResources(region.width, region.height, pixelFormat))
return false;
slot.width = width;
slot.height = height;
slot.format = format;
slot.dirty = false;
return true;
}
const uint32_t bytesPerPixel = pixelFormat == GVFG_PIXFMT_YUY2 ? 2u : 4u;
const uint32_t regionRowBytes = region.width * bytesPerPixel;
const size_t yuvOffset = static_cast<size_t>(region.top) * yuvStride +
static_cast<size_t>(region.left) * bytesPerPixel;
const size_t maskOffset = static_cast<size_t>(region.top) * maskStride + region.left;
D3D11_MAPPED_SUBRESOURCE mapped{};
if (FAILED(context->Map(osdTexture.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped)))
return false;
const bool copiedYuv = copyRowsToMapped(
yuvSurface.data() + yuvOffset, static_cast<int>(yuvStride),
regionRowBytes, region.height, mapped);
context->Unmap(osdTexture.Get(), 0);
if (!copiedYuv)
return false;
if (FAILED(context->Map(maskTexture.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped)))
return false;
const bool copiedMask = copyRowsToMapped(
maskSurface.data() + maskOffset, static_cast<int>(maskStride),
region.width, region.height, mapped);
context->Unmap(maskTexture.Get(), 0);
if (!copiedMask)
return false;
const uint32_t blendValues[4] = {
pixelFormat == GVFG_PIXFMT_Y210 ? 0xffc0u : 0x00ffu,
0u, 0u, 0u};
context->UpdateSubresource(blendParameters.Get(), 0, nullptr, blendValues, 0, 0);
surfaceWidth = frameWidth;
surfaceHeight = frameHeight;
surfaceFormat = pixelFormat;
bool rebuildSurface(uint32_t width, uint32_t height, int format)
{
surfaceDirty = true; // Retry partial failures, including upload failures.
region = {};
for (auto &slot : slots)
{
if (!rebuildSlot(slot, width, height, format))
return false;
if (slot.region.empty())
continue;
if (region.empty())
region = slot.region;
else
{
const uint32_t right = (std::max)(region.left + region.width, slot.region.left + slot.region.width);
const uint32_t bottom = (std::max)(region.top + region.height, slot.region.top + slot.region.height);
region.left = (std::min)(region.left, slot.region.left);
region.top = (std::min)(region.top, slot.region.top);
region.width = right - region.left;
region.height = bottom - region.top;
}
}
// Include hidden slots in the shared ROI so show/hide never rerasterizes
// glyphs or recreates textures. Visibility is a shader constant.
if (!region.empty())
{
if (!ensureDevice() || !ensureResources(region.width, region.height, format))
return false;
const uint32_t bpp = format == GVFG_PIXFMT_YUY2 ? 2u : 4u;
const size_t yuvOffset = (static_cast<size_t>(region.top) * width + region.left) * bpp;
const size_t maskOffset = static_cast<size_t>(region.top) * width + region.left;
for (auto &slot : slots)
{
D3D11_MAPPED_SUBRESOURCE mapped{};
if (FAILED(context->Map(slot.texture.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped)))
return false;
const bool yuvOk = copyRowsToMapped(slot.yuv.data() + yuvOffset,
static_cast<int>(width * bpp), region.width * bpp, region.height, mapped);
context->Unmap(slot.texture.Get(), 0);
if (!yuvOk || FAILED(context->Map(slot.maskTexture.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped)))
return false;
const bool maskOk = copyRowsToMapped(slot.mask.data() + maskOffset,
static_cast<int>(width), region.width, region.height, mapped);
context->Unmap(slot.maskTexture.Get(), 0);
if (!maskOk)
return false;
}
}
surfaceWidth = width;
surfaceHeight = height;
surfaceFormat = format;
surfaceDirty = false;
return true;
}
@@ -505,7 +526,11 @@ struct GpuOsdProcessor::Impl
std::lock_guard<std::mutex> lock(mutex);
finishStage(GVFG_DEBUG_OSD_LOCK_WAIT);
applied = false;
if (!active)
uint32_t visibleSlots = 0;
for (uint32_t i = 0; i < slots.size(); ++i)
if (slots[i].configured && slots[i].visible)
visibleSlots |= 1u << i;
if (!visibleSlots)
return GVFG_OK;
if (!source.data || source.width <= 0 || source.height <= 0 ||
(source.width & 1) != 0 || source.row_stride_bytes <= 0 ||
@@ -537,7 +562,10 @@ struct GpuOsdProcessor::Impl
return GVFG_EIO;
finishStage(GVFG_DEBUG_OSD_SURFACE);
// Invisible text needs no processed buffer or GPU work.
if (region.empty())
bool hasVisibleInk = false;
for (const auto &slot : slots)
hasVisibleInk = hasVisibleInk || (slot.visible && !slot.region.empty());
if (region.empty() || !hasVisibleInk)
return GVFG_OK;
const uint32_t rowBytes = static_cast<uint32_t>(rowBytes64);
@@ -582,18 +610,22 @@ struct GpuOsdProcessor::Impl
return GVFG_EIO;
finishStage(GVFG_DEBUG_OSD_UPLOAD);
ID3D11ShaderResourceView *srvs[] = {sourceSrv.Get(), osdSrv.Get(), maskSrv.Get()};
const uint32_t blendValues[4] = {
source.pixel_format == GVFG_PIXFMT_Y210 ? 0xffc0u : 0x00ffu, visibleSlots, 0u, 0u};
context->UpdateSubresource(blendParameters.Get(), 0, nullptr, blendValues, 0, 0);
ID3D11ShaderResourceView *srvs[] = {sourceSrv.Get(), slots[0].srv.Get(), slots[0].maskSrv.Get(),
slots[1].srv.Get(), slots[1].maskSrv.Get()};
ID3D11UnorderedAccessView *uavs[] = {outputUav.Get()};
context->CSSetShader(shader.Get(), nullptr, 0);
ID3D11Buffer *constantBuffers[] = {blendParameters.Get()};
context->CSSetConstantBuffers(0, 1, constantBuffers);
context->CSSetShaderResources(0, 3, srvs);
context->CSSetShaderResources(0, 5, srvs);
context->CSSetUnorderedAccessViews(0, 1, uavs, nullptr);
context->Dispatch((region.width / 2u + 15u) / 16u,
(region.height + 15u) / 16u, 1);
ID3D11ShaderResourceView *nullSrvs[] = {nullptr, nullptr, nullptr};
ID3D11ShaderResourceView *nullSrvs[] = {nullptr, nullptr, nullptr, nullptr, nullptr};
ID3D11UnorderedAccessView *nullUav = nullptr;
context->CSSetShaderResources(0, 3, nullSrvs);
context->CSSetShaderResources(0, 5, nullSrvs);
context->CSSetUnorderedAccessViews(0, 1, &nullUav, nullptr);
ID3D11Buffer *nullBuffer = nullptr;
context->CSSetConstantBuffers(0, 1, &nullBuffer);
@@ -650,13 +682,8 @@ struct GpuOsdProcessor::Impl
}
mutable std::mutex mutex;
bool active = false;
std::array<Slot, GVFG_OSD_SLOT_COUNT> slots;
bool surfaceDirty = true;
std::string text;
int32_t x = 0;
int32_t y = 0;
uint32_t fontSize = 0;
uint32_t colorRgb = 0xffffffu;
OsdRegion region;
uint32_t surfaceWidth = 0;
uint32_t surfaceHeight = 0;
@@ -664,37 +691,32 @@ struct GpuOsdProcessor::Impl
uint32_t resourceWidth = 0;
uint32_t resourceHeight = 0;
int resourceFormat = GVFG_PIXFMT_UNKNOWN;
std::vector<uint8_t> yuvSurface;
std::vector<uint8_t> maskSurface;
ComPtr<ID3D11Device> device;
ComPtr<ID3D11DeviceContext> context;
ComPtr<ID3D11ComputeShader> shader;
ComPtr<ID3D11Buffer> blendParameters;
ComPtr<ID3D11Texture2D> sourceTexture;
ComPtr<ID3D11Texture2D> osdTexture;
ComPtr<ID3D11Texture2D> maskTexture;
ComPtr<ID3D11Texture2D> outputTexture;
ComPtr<ID3D11Texture2D> readbackTexture;
ComPtr<ID3D11ShaderResourceView> sourceSrv;
ComPtr<ID3D11ShaderResourceView> osdSrv;
ComPtr<ID3D11ShaderResourceView> maskSrv;
ComPtr<ID3D11UnorderedAccessView> outputUav;
};
GpuOsdProcessor::GpuOsdProcessor() : impl_(std::make_unique<Impl>()) {}
GpuOsdProcessor::~GpuOsdProcessor() = default;
gvfg_status_t GpuOsdProcessor::configure(const gvfg_channel_osd_config_t &config)
gvfg_status_t GpuOsdProcessor::configure(uint32_t index, const gvfg_channel_osd_config_t &config)
{
try
{
return impl_->configure(config);
return impl_->configure(index, config);
}
catch (...)
{
return GVFG_EIO;
}
}
void GpuOsdProcessor::clear() { impl_->clear(); }
gvfg_status_t GpuOsdProcessor::clear(uint32_t index) { return impl_->clear(index); }
gvfg_status_t GpuOsdProcessor::setVisible(uint32_t index, int visible) { return impl_->setVisible(index, visible); }
gvfg_status_t GpuOsdProcessor::process(const gvfg_frame_t &source,
std::vector<uint8_t> &destination,
int &destinationStride,
+3 -2
View File
@@ -15,8 +15,9 @@ public:
GpuOsdProcessor();
~GpuOsdProcessor();
gvfg_status_t configure(const gvfg_channel_osd_config_t &config);
void clear();
gvfg_status_t configure(uint32_t index, const gvfg_channel_osd_config_t &config);
gvfg_status_t clear(uint32_t index);
gvfg_status_t setVisible(uint32_t index, int visible);
// writableSource must equal source.data. Only the backend can grant this
// writable SDK-owned storage, held until the caller releases the frame.
// The default-OFF experimental override admits driver storage only after
+60 -48
View File
@@ -682,11 +682,11 @@ struct gvfg_channel_session_t
return session->set_video_format(format);
}
gvfg_status_t setOsd(const gvfg_channel_osd_config_t &config)
gvfg_status_t setOsd(uint32_t index, const gvfg_channel_osd_config_t &config)
{
if (!session)
return reject(GVFG_ESTATE, "gvfg_set_channel_osd rejected: channel is not open");
const gvfg_status_t status = osdProcessor.configure(config);
const gvfg_status_t status = osdProcessor.configure(index, config);
return status == GVFG_OK
? GVFG_OK
: reject(status, status == GVFG_EINVAL
@@ -694,12 +694,19 @@ struct gvfg_channel_session_t
: "gvfg_set_channel_osd failed to store configuration");
}
gvfg_status_t clearOsd()
gvfg_status_t clearOsd(uint32_t index)
{
if (!session)
return reject(GVFG_ESTATE, "gvfg_clear_channel_osd rejected: channel is not open");
osdProcessor.clear();
return GVFG_OK;
return osdProcessor.clear(index);
}
gvfg_status_t setOsdVisible(uint32_t index, int visible)
{
if (!session)
return reject(GVFG_ESTATE, "OSD visibility rejected: channel is not open");
const gvfg_status_t status = osdProcessor.setVisible(index, visible);
return status == GVFG_OK ? status : reject(status, "OSD visibility rejected: invalid slot, visibility, or no saved configuration");
}
gvfg_status_t setAudioEnabled(bool enabled)
@@ -1173,68 +1180,73 @@ extern "C"
return handle->setChannelVideoFormat(channel_index, format);
}
gvfg_status_t gvfg_measure_osd_text(const char *text, uint32_t text_length,
uint32_t font_size_px,
uint32_t *out_width, uint32_t *out_height)
gvfg_status_t gvfg_get_osd_fonts(gvfg_osd_font_list_t *out_list)
{
if (out_width)
*out_width = 0;
if (out_height)
*out_height = 0;
if (!text || text_length == 0 || text_length > GVFG_OSD_MAX_TEXT_CHARACTERS ||
font_size_px == 0 || !out_width || !out_height)
if (!out_list)
return GVFG_EINVAL;
for (uint32_t i = 0; i < text_length; ++i)
{
const unsigned char c = static_cast<unsigned char>(text[i]);
if (c < 0x20 || c > 0x7e)
return GVFG_EINVAL;
}
*out_list = {};
const auto status = gvfg_osd_get_fonts(&out_list->count, &out_list->names);
return status == GVFG_OSD_OK ? GVFG_OK : GVFG_EIO;
}
gvfg_status_t gvfg_measure_osd_text(const gvfg_channel_osd_config_t *config,
uint32_t *out_width, uint32_t *out_height)
{
if (out_width) *out_width = 0;
if (out_height) *out_height = 0;
if (!config || !config->text || !out_width || !out_height)
return GVFG_EINVAL;
const size_t length = strnlen_s(config->text, GVFG_OSD_MAX_TEXT_CHARACTERS + 1u);
if (length == 0 || length > GVFG_OSD_MAX_TEXT_CHARACTERS)
return GVFG_EINVAL;
try
{
gvfg_osd_handle rawHandle = nullptr;
if (gvfg_osd_create(&rawHandle) != GVFG_OSD_OK)
return GVFG_EIO;
const std::unique_ptr<void, decltype(&gvfg_osd_destroy)> helper(
rawHandle, &gvfg_osd_destroy);
const gvfg_osd_status_t status = gvfg_osd_measure(
helper.get(), text, text_length, font_size_px, out_width, out_height);
if (status == GVFG_OSD_OK)
return GVFG_OK;
return status == GVFG_OSD_EINVAL ? GVFG_EINVAL : GVFG_EIO;
}
catch (...)
{
*out_width = 0;
*out_height = 0;
return GVFG_EIO;
const std::unique_ptr<void, decltype(&gvfg_osd_destroy)> helper(rawHandle, &gvfg_osd_destroy);
const auto status = gvfg_osd_measure(helper.get(), config->text,
static_cast<uint32_t>(length), config->font_size_px, config->font_family,
out_width, out_height);
return status == GVFG_OSD_OK ? GVFG_OK :
status == GVFG_OSD_EINVAL ? GVFG_EINVAL : GVFG_EIO;
}
catch (...) { return GVFG_EIO; }
}
gvfg_status_t gvfg_set_channel_osd(gvfg_handle handle,
int channel_index,
const gvfg_channel_osd_config_t *config)
gvfg_status_t gvfg_set_channel_osd(gvfg_handle handle, int channel_index,
uint32_t osd_index, const gvfg_channel_osd_config_t *config)
{
if (!handle)
return GVFG_EINVAL;
if (!config)
return handle->rejectChannel(channel_index, GVFG_EINVAL,
"gvfg_set_channel_osd rejected: config is null");
gvfg_channel_session_t *channel = handle->findChannel(channel_index);
return channel ? channel->setOsd(*config)
: handle->rejectChannel(channel_index, GVFG_ESTATE,
"gvfg_set_channel_osd rejected: channel is not open");
if (!config || osd_index >= GVFG_OSD_SLOT_COUNT)
return handle->rejectChannel(channel_index, GVFG_EINVAL, "OSD config or index is invalid");
auto *channel = handle->findChannel(channel_index);
return channel ? channel->setOsd(osd_index, *config)
: handle->rejectChannel(channel_index, GVFG_ESTATE, "OSD channel is not open");
}
gvfg_status_t gvfg_clear_channel_osd(gvfg_handle handle, int channel_index)
gvfg_status_t gvfg_set_channel_osd_visible(gvfg_handle handle, int channel_index,
uint32_t osd_index, int visible)
{
if (!handle)
return GVFG_EINVAL;
gvfg_channel_session_t *channel = handle->findChannel(channel_index);
return channel ? channel->clearOsd()
: handle->rejectChannel(channel_index, GVFG_ESTATE,
"gvfg_clear_channel_osd rejected: channel is not open");
if (osd_index >= GVFG_OSD_SLOT_COUNT || (visible != 0 && visible != 1))
return handle->rejectChannel(channel_index, GVFG_EINVAL, "OSD index or visibility is invalid");
auto *channel = handle->findChannel(channel_index);
return channel ? channel->setOsdVisible(osd_index, visible)
: handle->rejectChannel(channel_index, GVFG_ESTATE, "OSD channel is not open");
}
gvfg_status_t gvfg_clear_channel_osd(gvfg_handle handle, int channel_index, uint32_t osd_index)
{
if (!handle)
return GVFG_EINVAL;
if (osd_index >= GVFG_OSD_SLOT_COUNT)
return handle->rejectChannel(channel_index, GVFG_EINVAL, "OSD index is invalid");
auto *channel = handle->findChannel(channel_index);
return channel ? channel->clearOsd(osd_index)
: handle->rejectChannel(channel_index, GVFG_ESTATE, "OSD channel is not open");
}
gvfg_status_t gvfg_set_channel_audio_enabled(gvfg_handle handle,