d3d11: Add d3d11overlay element

Similar to cairooverlay element but this element emits "draw"
signal with Direct3D11 render target view, so that an application
can render/overlay/blend on the given render target view
without any copy operation

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/4415>
This commit is contained in:
Seungha Yang 2023-04-14 02:10:35 +09:00
parent 3d6a609590
commit c883ab593c
6 changed files with 820 additions and 0 deletions

View file

@ -0,0 +1,435 @@
/*
* GStreamer
* Copyright (C) 2023 Seungha Yang <seungha@centricular.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
/**
* SECTION:element-d3d11overlay
* @title: d3d11overlay
*
* Provides Direct3D11 render target view to an application so that
* the application overlay/blend application image on the render target view
*
* Since: 1.24
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "gstd3d11overlay.h"
#include <gst/d3d11/gstd3d11-private.h>
GST_DEBUG_CATEGORY_STATIC (gst_d3d11_overlay_debug);
#define GST_CAT_DEFAULT gst_d3d11_overlay_debug
static GstStaticCaps template_caps =
GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
(GST_CAPS_FEATURE_MEMORY_D3D11_MEMORY, "{ BGRA, RGBA }"));
enum
{
SIGNAL_DRAW,
SIGNAL_CAPS_CHANGED,
SIGNAL_LAST,
};
static guint gst_d3d11_overlay_signals[SIGNAL_LAST];
struct _GstD3D11Overlay
{
GstD3D11BaseFilter parent;
GstBufferPool *fallback_pool;
};
#define gst_d3d11_overlay_parent_class parent_class
G_DEFINE_TYPE (GstD3D11Overlay, gst_d3d11_overlay, GST_TYPE_D3D11_BASE_FILTER);
static gboolean gst_d3d11_overlay_stop (GstBaseTransform * trans);
static gboolean gst_d3d11_overlay_propose_allocation (GstBaseTransform * trans,
GstQuery * decide_query, GstQuery * query);
static GstFlowReturn gst_d3d11_overlay_transform_ip (GstBaseTransform * trans,
GstBuffer * buf);
static gboolean gst_d3d11_overlay_set_info (GstD3D11BaseFilter * filter,
GstCaps * in_caps, GstVideoInfo * in_info, GstCaps * out_caps,
GstVideoInfo * out_info);
static void
gst_d3d11_overlay_class_init (GstD3D11OverlayClass * klass)
{
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
GstBaseTransformClass *trans_class = GST_BASE_TRANSFORM_CLASS (klass);
GstD3D11BaseFilterClass *filter_class = GST_D3D11_BASE_FILTER_CLASS (klass);
GstCaps *caps;
/**
* GstD3D11Overlay::draw:
* @overlay: Overlay element emitting the signal
* @device: GstD3D11Device object
* @rtv: ID3D11RenderTargetView handle
* @timestamp: Timestamp (see #GstClockTime) of the current buffer
* @duration: Duration (see #GstClockTime) of the current buffer
*
* This signal is emitted when the overlay should be drawn with
* gst_d3d11_device_lock taken.
*
* Returns: %TRUE if rendering operation happend
*
* Since: 1.24
*/
gst_d3d11_overlay_signals[SIGNAL_DRAW] = g_signal_new ("draw",
G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST, 0, nullptr, nullptr,
nullptr, G_TYPE_BOOLEAN, 4, GST_TYPE_OBJECT, G_TYPE_POINTER,
G_TYPE_UINT64, G_TYPE_UINT64);
/**
* GstD3D11Overlay::caps-changed:
* @overlay: Overlay element emitting the signal
* @caps: #GstCaps of the element
* This signal is emitted when the caps or associated GstD3D11Device
* of the element has changed
*
* Since: 1.24
*/
gst_d3d11_overlay_signals[SIGNAL_CAPS_CHANGED] = g_signal_new ("caps-changed",
G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST, 0, nullptr, nullptr,
nullptr, G_TYPE_NONE, 1, GST_TYPE_CAPS);
caps = gst_d3d11_get_updated_template_caps (&template_caps);
gst_element_class_add_pad_template (element_class,
gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, caps));
gst_element_class_add_pad_template (element_class,
gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS, caps));
gst_caps_unref (caps);
gst_element_class_set_static_metadata (element_class,
"Direct3D11 Overlay", "Filter/Video",
"Provides application renderable Direct3D11 render target view",
"Seungha Yang <seungha@centricular.com>");
trans_class->passthrough_on_same_caps = FALSE;
trans_class->stop = GST_DEBUG_FUNCPTR (gst_d3d11_overlay_stop);
trans_class->propose_allocation =
GST_DEBUG_FUNCPTR (gst_d3d11_overlay_propose_allocation);
trans_class->transform_ip =
GST_DEBUG_FUNCPTR (gst_d3d11_overlay_transform_ip);
filter_class->set_info = GST_DEBUG_FUNCPTR (gst_d3d11_overlay_set_info);
GST_DEBUG_CATEGORY_INIT (gst_d3d11_overlay_debug,
"d3d11overlay", 0, "d3d11overlay");
}
static void
gst_d3d11_overlay_init (GstD3D11Overlay * download)
{
}
static gboolean
gst_d3d11_overlay_stop (GstBaseTransform * trans)
{
GstD3D11Overlay *self = GST_D3D11_OVERLAY (trans);
if (self->fallback_pool) {
gst_buffer_pool_set_active (self->fallback_pool, FALSE);
gst_clear_object (&self->fallback_pool);
}
return GST_BASE_TRANSFORM_CLASS (parent_class)->stop (trans);
}
static gboolean
gst_d3d11_overlay_propose_allocation (GstBaseTransform * trans,
GstQuery * decide_query, GstQuery * query)
{
GstD3D11BaseFilter *filter = GST_D3D11_BASE_FILTER (trans);
GstVideoInfo info;
GstCaps *caps;
guint size;
GstBufferPool *pool = nullptr;
gboolean update_pool = FALSE;
guint min = 0;
guint max = 0;
GstStructure *config;
GstD3D11AllocationParams *params;
if (!GST_BASE_TRANSFORM_CLASS (parent_class)->propose_allocation (trans,
decide_query, query))
return FALSE;
gst_query_parse_allocation (query, &caps, nullptr);
if (!caps) {
GST_WARNING_OBJECT (filter, "Query without caps");
return FALSE;
}
if (!gst_video_info_from_caps (&info, caps)) {
GST_ERROR_OBJECT (filter, "Invalid caps %" GST_PTR_FORMAT, caps);
return FALSE;
}
size = GST_VIDEO_INFO_SIZE (&info);
if (gst_query_get_n_allocation_pools (query) > 0) {
gst_query_parse_nth_allocation_pool (query, 0, &pool, nullptr, &min, &max);
if (pool) {
if (!GST_IS_D3D11_BUFFER_POOL (pool)) {
gst_clear_object (&pool);
} else {
GstD3D11BufferPool *dpool = GST_D3D11_BUFFER_POOL (pool);
if (dpool->device != filter->device)
gst_clear_object (&pool);
}
}
update_pool = TRUE;
}
if (!pool)
pool = gst_d3d11_buffer_pool_new (filter->device);
config = gst_buffer_pool_get_config (pool);
gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
gst_buffer_pool_config_set_params (config, caps, size, 0, 0);
params = gst_buffer_pool_config_get_d3d11_allocation_params (config);
if (params) {
params->desc[0].BindFlags |=
(D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET);
} else {
params = gst_d3d11_allocation_params_new (filter->device, &info,
GST_D3D11_ALLOCATION_FLAG_DEFAULT,
D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET, 0);
}
gst_buffer_pool_config_set_d3d11_allocation_params (config, params);
gst_d3d11_allocation_params_free (params);
if (!gst_buffer_pool_set_config (pool, config)) {
GST_ERROR_OBJECT (filter, "Couldn't set pool config");
gst_object_unref (pool);
return FALSE;
}
/* d3d11 buffer pool will update buffer size based on allocated texture,
* get size from config again */
config = gst_buffer_pool_get_config (pool);
gst_buffer_pool_config_get_params (config, nullptr, &size, nullptr, nullptr);
gst_structure_free (config);
if (update_pool)
gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
else
gst_query_add_allocation_pool (query, pool, size, min, max);
gst_object_unref (pool);
gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, nullptr);
return TRUE;
}
static gboolean
gst_d3d11_overlay_copy_memory (GstD3D11Overlay * self, GstMemory * src,
GstMemory * dst)
{
GstD3D11BaseFilter *filter = GST_D3D11_BASE_FILTER (self);
GstMapInfo src_map;
GstMapInfo dst_map;
ID3D11Resource *dst_texture, *src_texture;
ID3D11DeviceContext *context;
D3D11_TEXTURE2D_DESC src_desc, dst_desc;
D3D11_BOX src_box;
context = gst_d3d11_device_get_device_context_handle (filter->device);
if (!gst_memory_map (src,
&src_map, (GstMapFlags) (GST_MAP_READ | GST_MAP_D3D11))) {
GST_ERROR_OBJECT (self, "Couldn't map src memory");
return FALSE;
}
if (!gst_memory_map (dst,
&dst_map, (GstMapFlags) (GST_MAP_WRITE | GST_MAP_D3D11))) {
GST_ERROR_OBJECT (self, "Couldn't map dst memory");
gst_memory_unmap (src, &src_map);
return FALSE;
}
gst_d3d11_memory_get_texture_desc (GST_D3D11_MEMORY_CAST (src), &src_desc);
gst_d3d11_memory_get_texture_desc (GST_D3D11_MEMORY_CAST (dst), &dst_desc);
src_texture = (ID3D11Texture2D *) src_map.data;
dst_texture = (ID3D11Texture2D *) dst_map.data;
src_box.front = 0;
src_box.back = 1;
src_box.left = 0;
src_box.top = 0;
src_box.right = MIN (src_desc.Width, dst_desc.Width);
src_box.bottom = MIN (src_desc.Height, dst_desc.Height);
context->CopySubresourceRegion (dst_texture,
0, 0, 0, 0, src_texture, 0, &src_box);
gst_memory_unmap (src, &src_map);
gst_memory_unmap (dst, &dst_map);
return TRUE;
}
static GstFlowReturn
gst_d3d11_overlay_transform_ip (GstBaseTransform * trans, GstBuffer * buf)
{
GstD3D11BaseFilter *filter = GST_D3D11_BASE_FILTER (trans);
GstD3D11Overlay *self = GST_D3D11_OVERLAY (trans);
GstMemory *mem;
GstD3D11Memory *dmem;
ID3D11RenderTargetView *rtv;
GstBuffer *fallback_buf = nullptr;
GstMemory *fallback_mem = nullptr;
GstD3D11Memory *fallback_dmem = nullptr;
GstMemory *target_mem;
GstMapInfo map;
gboolean rendered = FALSE;
GstFlowReturn ret = GST_FLOW_OK;
mem = gst_buffer_peek_memory (buf, 0);
if (!gst_is_d3d11_memory (mem)) {
GST_ERROR_OBJECT (self, "Not a d3d11 memory");
return GST_FLOW_ERROR;
}
dmem = GST_D3D11_MEMORY_CAST (mem);
rtv = gst_d3d11_memory_get_render_target_view (dmem, 0);
if (!rtv) {
GST_DEBUG_OBJECT (self, "RTV is unavailable");
gst_buffer_pool_acquire_buffer (self->fallback_pool, &fallback_buf,
nullptr);
if (!fallback_buf) {
GST_ERROR_OBJECT (self, "Couldn't allocate fallback buffer");
return GST_FLOW_ERROR;
}
}
GstD3D11DeviceLockGuard lk (filter->device);
if (fallback_buf) {
fallback_mem = gst_buffer_peek_memory (fallback_buf, 0);
if (!gst_d3d11_overlay_copy_memory (self, mem, fallback_mem)) {
GST_ERROR_OBJECT (self, "Couldn't copy input memory to fallback");
gst_buffer_unref (fallback_buf);
return GST_FLOW_ERROR;
}
fallback_dmem = GST_D3D11_MEMORY_CAST (fallback_mem);
rtv = gst_d3d11_memory_get_render_target_view (fallback_dmem, 0);
if (!rtv) {
GST_ERROR_OBJECT (self, "RTV is unavailable");
gst_buffer_unref (fallback_buf);
return GST_FLOW_ERROR;
}
target_mem = fallback_mem;
} else {
target_mem = mem;
}
if (!gst_memory_map (target_mem, &map,
(GstMapFlags) (GST_MAP_WRITE | GST_MAP_D3D11))) {
GST_ERROR_OBJECT (self, "Couldn't map render target memory");
gst_clear_buffer (&fallback_buf);
return GST_FLOW_ERROR;
}
g_signal_emit (self, gst_d3d11_overlay_signals[SIGNAL_DRAW], 0,
filter->device, rtv, GST_BUFFER_PTS (buf), GST_BUFFER_DURATION (buf),
&rendered);
GST_LOG_OBJECT (self, "Draw signal return: %d", rendered);
gst_memory_unmap (target_mem, &map);
if (fallback_buf && rendered) {
if (!gst_d3d11_overlay_copy_memory (self, fallback_mem, mem)) {
GST_ERROR_OBJECT (self, "Couldn't copy back to input memory");
ret = GST_FLOW_ERROR;
}
}
gst_clear_buffer (&fallback_buf);
return ret;
}
static gboolean
gst_d3d11_overlay_set_info (GstD3D11BaseFilter * filter, GstCaps * in_caps,
GstVideoInfo * in_info, GstCaps * out_caps, GstVideoInfo * out_info)
{
GstD3D11Overlay *self = GST_D3D11_OVERLAY (filter);
GstStructure *config;
GstD3D11AllocationParams *params;
if (self->fallback_pool) {
gst_buffer_pool_set_active (self->fallback_pool, FALSE);
gst_object_unref (self->fallback_pool);
}
self->fallback_pool = gst_d3d11_buffer_pool_new (filter->device);
config = gst_buffer_pool_get_config (self->fallback_pool);
gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
gst_buffer_pool_config_set_params (config, in_caps,
GST_VIDEO_INFO_SIZE (in_info), 0, 0);
params = gst_d3d11_allocation_params_new (filter->device, in_info,
GST_D3D11_ALLOCATION_FLAG_DEFAULT,
D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET, 0);
gst_buffer_pool_config_set_d3d11_allocation_params (config, params);
gst_d3d11_allocation_params_free (params);
if (!gst_buffer_pool_set_config (self->fallback_pool, config)) {
GST_ERROR_OBJECT (self, "Couldn't set pool config");
gst_clear_object (&self->fallback_pool);
return FALSE;
}
if (!gst_buffer_pool_set_active (self->fallback_pool, TRUE)) {
GST_ERROR_OBJECT (self, "Couldn't activate pool");
gst_clear_object (&self->fallback_pool);
return FALSE;
}
GST_DEBUG_OBJECT (self, "New caps %" GST_PTR_FORMAT, in_caps);
g_signal_emit (self, gst_d3d11_overlay_signals[SIGNAL_CAPS_CHANGED], 0,
in_caps);
return TRUE;
}

View file

@ -0,0 +1,32 @@
/*
* GStreamer
* Copyright (C) 2023 Seungha Yang <seungha@centricular.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#pragma once
#include "gstd3d11basefilter.h"
G_BEGIN_DECLS
#define GST_TYPE_D3D11_OVERLAY (gst_d3d11_overlay_get_type())
G_DECLARE_FINAL_TYPE (GstD3D11Overlay,
gst_d3d11_overlay, GST, D3D11_OVERLAY, GstD3D11BaseFilter);
G_END_DECLS

View file

@ -9,6 +9,7 @@ d3d11_sources = [
'gstd3d11h264dec.cpp',
'gstd3d11h265dec.cpp',
'gstd3d11mpeg2dec.cpp',
'gstd3d11overlay.cpp',
'gstd3d11overlaycompositor.cpp',
'gstd3d11pluginutils.cpp',
'gstd3d11testsrc.cpp',

View file

@ -75,6 +75,7 @@
#include "gstd3d11av1dec.h"
#include "gstd3d11deinterlace.h"
#include "gstd3d11testsrc.h"
#include "gstd3d11overlay.h"
#if !GST_D3D11_WINAPI_ONLY_APP
#include "gstd3d11screencapturesrc.h"
@ -235,6 +236,8 @@ plugin_init (GstPlugin * plugin)
"d3d11compositor", GST_RANK_SECONDARY, GST_TYPE_D3D11_COMPOSITOR);
gst_element_register (plugin,
"d3d11testsrc", GST_RANK_NONE, GST_TYPE_D3D11_TEST_SRC);
gst_element_register (plugin,
"d3d11overlay", GST_RANK_NONE, GST_TYPE_D3D11_OVERLAY);
#if !GST_D3D11_WINAPI_ONLY_APP
if (gst_d3d11_is_windows_8_or_greater ()) {

View file

@ -0,0 +1,341 @@
/*
* GStreamer
* Copyright (C) 2023 Seungha Yang <seungha@centricular.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <gst/gst.h>
#include <gst/video/video.h>
#include <gst/d3d11/gstd3d11.h>
#include <d2d1.h>
#include <dwrite.h>
#include <wrl.h>
#include <string>
#include <locale>
#include <codecvt>
/* *INDENT-OFF* */
using namespace Microsoft::WRL;
struct OverlayContext
{
GstElement *pipeline = nullptr;
ID2D1Factory *d2d_factory = nullptr;
IDWriteFactory *dwrite_factory = nullptr;
IDWriteTextFormat *format = nullptr;
IDWriteTextLayout *layout = nullptr;
std::wstring text;
FLOAT width;
FLOAT height;
FLOAT origin_y;
gint text_width;
gint last_position = 0;
GMainLoop *loop = nullptr;
};
/* *INDENT-ON* */
static gboolean
bus_msg (GstBus * bus, GstMessage * msg, OverlayContext * context)
{
switch (GST_MESSAGE_TYPE (msg)) {
case GST_MESSAGE_ERROR:{
GError *err;
gchar *dbg;
gst_message_parse_error (msg, &err, &dbg);
gst_printerrln ("ERROR %s", err->message);
if (dbg)
gst_printerrln ("ERROR debug information: %s", dbg);
g_clear_error (&err);
g_free (dbg);
g_main_loop_quit (context->loop);
break;
}
case GST_MESSAGE_EOS:
gst_println ("Got EOS");
g_main_loop_quit (context->loop);
break;
default:
break;
}
return TRUE;
}
/* This callback will be called with gst_d3d11_device_lock() taken by
* d3d11overlay. We can perform GPU operation here safely */
static gboolean
on_draw (GstElement * overlay, GstD3D11Device * device,
ID3D11RenderTargetView * rtv, GstClockTime pts, GstClockTime duration,
OverlayContext * context)
{
ComPtr < ID3D11Resource > resource;
ComPtr < IDXGISurface > surface;
ComPtr < ID2D1RenderTarget > d2d_target;
ComPtr < ID2D1SolidColorBrush > text_brush;
ComPtr < ID2D1SolidColorBrush > bg_brush;
HRESULT hr;
ID2D1Factory *d2d_factory;
FLOAT position;
rtv->GetResource (&resource);
hr = resource.As (&surface);
g_assert (SUCCEEDED (hr));
d2d_factory = context->d2d_factory;
D2D1_RENDER_TARGET_PROPERTIES props;
props.type = D2D1_RENDER_TARGET_TYPE_DEFAULT;
props.pixelFormat.format = DXGI_FORMAT_B8G8R8A8_UNORM;
props.pixelFormat.alphaMode = D2D1_ALPHA_MODE_PREMULTIPLIED;
/* default DPI */
props.dpiX = 0;
props.dpiY = 0;
props.usage = D2D1_RENDER_TARGET_USAGE_NONE;
props.minLevel = D2D1_FEATURE_LEVEL_DEFAULT;
/* Creates D2D render target using swapchin's backbuffer */
hr = d2d_factory->CreateDxgiSurfaceRenderTarget (surface.Get (), props,
&d2d_target);
g_assert (SUCCEEDED (hr));
hr = d2d_target->CreateSolidColorBrush (D2D1::ColorF (D2D1::ColorF::Black),
&bg_brush);
g_assert (SUCCEEDED (hr));
hr = d2d_target->CreateSolidColorBrush (D2D1::ColorF (D2D1::ColorF::White),
&text_brush);
g_assert (SUCCEEDED (hr));
d2d_target->BeginDraw ();
/* Draw background */
d2d_target->FillRectangle (D2D1::RectF (0, context->origin_y, context->width,
context->height), bg_brush.Get ());
/* Draw text */
position = -context->last_position;
do {
d2d_target->DrawTextLayout (D2D1::Point2F (position, context->origin_y),
context->layout, text_brush.Get (),
D2D1_DRAW_TEXT_OPTIONS_CLIP | D2D1_DRAW_TEXT_OPTIONS_ENABLE_COLOR_FONT);
position = position + context->text_width;
} while (position < context->width);
d2d_target->EndDraw ();
context->last_position += 2;
if (context->last_position >= context->text_width) {
context->last_position %= context->text_width;
}
return TRUE;
}
gint
main (gint argc, gchar ** argv)
{
GOptionContext *option_ctx;
GError *error = nullptr;
gboolean ret;
gchar *text = nullptr;
gint width = 1280;
gint height = 720;
GOptionEntry options[] = {
{"text", 0, 0, G_OPTION_ARG_STRING, &text, "Text to render", nullptr},
{"width", 0, 0, G_OPTION_ARG_INT, &width, "Width of video stream", nullptr},
{"height", 0, 0, G_OPTION_ARG_INT, &height, "Height of video stream",
nullptr},
{nullptr}
};
OverlayContext context;
GstStateChangeReturn sret;
HRESULT hr;
std::wstring text_wide;
std::string pipeline_str;
GstElement *overlay;
DWRITE_TEXT_METRICS metrics;
FLOAT font_size;
bool was_decreased = false;
DWRITE_TEXT_RANGE range;
FLOAT text_height;
gchar **win32_argv;
ComPtr < IDWriteTextFormat > text_format;
ComPtr < IDWriteTextLayout > text_layout;
win32_argv = g_win32_get_command_line ();
option_ctx = g_option_context_new ("d3d11overlay example");
g_option_context_add_main_entries (option_ctx, options, nullptr);
g_option_context_add_group (option_ctx, gst_init_get_option_group ());
ret = g_option_context_parse_strv (option_ctx, &win32_argv, &error);
g_option_context_free (option_ctx);
g_strfreev (win32_argv);
if (!ret) {
gst_printerrln ("option parsing failed: %s", error->message);
g_clear_error (&error);
return 1;
}
/* Prepare device independent D2D objects */
hr = D2D1CreateFactory (D2D1_FACTORY_TYPE_MULTI_THREADED,
IID_PPV_ARGS (&context.d2d_factory));
if (FAILED (hr)) {
gst_printerrln ("Couldn't create D2D factory");
return 1;
}
hr = DWriteCreateFactory (DWRITE_FACTORY_TYPE_SHARED,
__uuidof (context.dwrite_factory),
reinterpret_cast < IUnknown ** >(&context.dwrite_factory));
if (FAILED (hr)) {
gst_printerrln ("Couldn't create DirectWrite factory");
return 1;
}
hr = context.dwrite_factory->CreateTextFormat (L"Arial", nullptr,
DWRITE_FONT_WEIGHT_BOLD, DWRITE_FONT_STYLE_NORMAL,
DWRITE_FONT_STRETCH_NORMAL, 12.0f, L"en-us", &text_format);
if (FAILED (hr)) {
gst_printerrln ("Couldn't create IDWriteTextFormat");
return 1;
}
text_format->SetTextAlignment (DWRITE_TEXT_ALIGNMENT_LEADING);
text_format->SetParagraphAlignment (DWRITE_PARAGRAPH_ALIGNMENT_CENTER);
text_format->SetWordWrapping (DWRITE_WORD_WRAPPING_NO_WRAP);
if (text && text[0] != '\0') {
std::wstring_convert < std::codecvt_utf8_utf16 < wchar_t >>converter;
text_wide = converter.from_bytes (text);
} else {
/* *INDENT-OFF* */
text_wide =
std::wstring (L"Hello GStreamer! 😊 안녕하세요 GStreamer! 😉 ") +
std::wstring (L"नमस्ते GStreamer! ❤️ Bonjour GStreamer! 😁 ") +
std::wstring (L"Hallo GStreamer! 😎 Hola GStreamer! 😍 ") +
std::wstring (L"こんにちは GStreamer! ✌️ 你好 GStreamer! 👍");
/* *INDENT-ON* */
}
text_height = (FLOAT) height / 10.0f;
hr = context.dwrite_factory->CreateTextLayout (text_wide.c_str (),
text_wide.length (), text_format.Get (), G_MAXFLOAT, G_MAXFLOAT,
&text_layout);
if (FAILED (hr)) {
gst_printerrln ("Couldn't create IDWriteTextLayout");
return 1;
}
/* Calculate best font size */
range.startPosition = 0;
range.length = text_wide.length ();
do {
hr = text_layout->GetMetrics (&metrics);
g_assert (SUCCEEDED (hr));
text_layout->GetFontSize (0, &font_size);
if (metrics.height >= (FLOAT) text_height) {
if (font_size > 1.0f) {
font_size -= 0.5f;
was_decreased = true;
hr = text_layout->SetFontSize (font_size, range);
g_assert (SUCCEEDED (hr));
continue;
}
break;
}
if (was_decreased)
break;
if (metrics.height < text_height) {
if (metrics.height >= text_height * 0.9)
break;
font_size += 0.5f;
hr = text_layout->SetFontSize (font_size, range);
g_assert (SUCCEEDED (hr));
continue;
}
} while (true);
gst_println ("Calculated font size %lf", font_size);
/* 10 pixels padding per loop */
text_layout->SetMaxWidth (metrics.widthIncludingTrailingWhitespace + 10);
text_layout->SetMaxHeight (metrics.height);
context.text = text_wide;
context.origin_y = (FLOAT) height - text_height;
context.width = width;
context.height = height;
context.text_width = metrics.widthIncludingTrailingWhitespace + 10;
context.layout = text_layout.Detach ();
context.loop = g_main_loop_new (nullptr, FALSE);
pipeline_str =
"d3d11testsrc ! video/x-raw(memory:D3D11Memory),format=BGRA,width=" +
std::to_string (width) + ",height=" + std::to_string (height) +
",framerate=60/1 ! d3d11overlay name=overlay ! queue ! d3d11videosink";
context.pipeline = gst_parse_launch (pipeline_str.c_str (), nullptr);
g_assert (context.pipeline);
overlay = gst_bin_get_by_name (GST_BIN (context.pipeline), "overlay");
g_assert (overlay);
g_signal_connect (overlay, "draw", G_CALLBACK (on_draw), &context);
gst_object_unref (overlay);
gst_bus_add_watch (GST_ELEMENT_BUS (context.pipeline),
(GstBusFunc) bus_msg, &context);
sret = gst_element_set_state (context.pipeline, GST_STATE_PLAYING);
g_assert (sret != GST_STATE_CHANGE_FAILURE);
g_main_loop_run (context.loop);
gst_element_set_state (context.pipeline, GST_STATE_NULL);
gst_bus_remove_watch (GST_ELEMENT_BUS (context.pipeline));
gst_object_unref (context.pipeline);
context.format->Release ();
context.layout->Release ();
context.d2d_factory->Release ();
context.dwrite_factory->Release ();
g_main_loop_unref (context.loop);
g_free (text);
return 0;
}

View file

@ -78,6 +78,14 @@ if d3d11_lib.found() and dxgi_lib.found() and d3dcompiler_lib.found() and have_d
dependencies: [gst_dep, gstbase_dep, gstvideo_dep, d3d11_lib, dxgi_lib, gstd3d11_dep, d2d_dep, dwrite_dep],
install: false,
)
executable('d3d11overlay', ['d3d11overlay.cpp'],
c_args : gst_plugins_bad_args + ['-DGST_USE_UNSTABLE_API'],
cpp_args : gst_plugins_bad_args + ['-DGST_USE_UNSTABLE_API'],
include_directories : [configinc, libsinc],
dependencies: [gst_dep, gstbase_dep, gstvideo_dep, d3d11_lib, dxgi_lib, gstd3d11_dep, d2d_dep, dwrite_dep],
install: false,
)
endif
endif