2019-12-24 06:54:57 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) <2019> Seungha Yang <seungha.yang@navercorp.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 "gstd3d11overlaycompositor.h"
|
2021-03-13 08:40:57 +00:00
|
|
|
#include "gstd3d11pluginutils.h"
|
|
|
|
#include <wrl.h>
|
2023-06-14 15:47:11 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <vector>
|
|
|
|
#include <algorithm>
|
2021-03-13 08:40:57 +00:00
|
|
|
|
2019-12-24 06:54:57 +00:00
|
|
|
GST_DEBUG_CATEGORY_EXTERN (gst_d3d11_overlay_compositor_debug);
|
|
|
|
#define GST_CAT_DEFAULT gst_d3d11_overlay_compositor_debug
|
|
|
|
|
2021-09-10 15:43:26 +00:00
|
|
|
/* *INDENT-OFF* */
|
|
|
|
using namespace Microsoft::WRL;
|
|
|
|
|
2019-12-24 06:54:57 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
struct {
|
|
|
|
FLOAT x;
|
|
|
|
FLOAT y;
|
|
|
|
FLOAT z;
|
|
|
|
} position;
|
|
|
|
struct {
|
2022-06-15 14:46:27 +00:00
|
|
|
FLOAT u;
|
|
|
|
FLOAT v;
|
2019-12-24 06:54:57 +00:00
|
|
|
} texture;
|
|
|
|
} VertexData;
|
|
|
|
|
2023-06-14 15:47:11 +00:00
|
|
|
struct GstD3D11CompositionOverlay
|
|
|
|
{
|
|
|
|
~GstD3D11CompositionOverlay ()
|
|
|
|
{
|
|
|
|
if (overlay_rect)
|
|
|
|
gst_video_overlay_rectangle_unref (overlay_rect);
|
2023-06-15 09:40:38 +00:00
|
|
|
if (d3d11_buffer)
|
|
|
|
gst_buffer_unref (d3d11_buffer);
|
2023-06-14 15:47:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GstVideoOverlayRectangle *overlay_rect = nullptr;
|
|
|
|
ComPtr<ID3D11Texture2D> texture;
|
|
|
|
ComPtr<ID3D11ShaderResourceView> srv;
|
|
|
|
ComPtr<ID3D11Buffer> vertex_buffer;
|
|
|
|
gboolean premul_alpha = FALSE;
|
2023-06-15 09:40:38 +00:00
|
|
|
GstBuffer *d3d11_buffer = nullptr;
|
2023-06-14 15:47:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::shared_ptr<GstD3D11CompositionOverlay> GstD3D11CompositionOverlayPtr;
|
2019-12-24 06:54:57 +00:00
|
|
|
|
2022-06-21 14:43:46 +00:00
|
|
|
struct _GstD3D11OverlayCompositorPrivate
|
2019-12-24 06:54:57 +00:00
|
|
|
{
|
2022-06-21 14:43:46 +00:00
|
|
|
GstVideoInfo info;
|
2019-12-24 06:54:57 +00:00
|
|
|
|
|
|
|
D3D11_VIEWPORT viewport;
|
|
|
|
|
2023-06-14 15:47:11 +00:00
|
|
|
ComPtr<ID3D11PixelShader> ps;
|
|
|
|
ComPtr<ID3D11PixelShader> premul_ps;
|
|
|
|
ComPtr<ID3D11VertexShader> vs;
|
|
|
|
ComPtr<ID3D11InputLayout> layout;
|
|
|
|
ComPtr<ID3D11SamplerState> sampler;
|
|
|
|
ComPtr<ID3D11BlendState> blend;
|
|
|
|
ComPtr<ID3D11Buffer> index_buffer;
|
2023-10-28 10:34:55 +00:00
|
|
|
ComPtr<ID3D11RasterizerState> rs;
|
2023-10-31 15:38:31 +00:00
|
|
|
std::vector<GstVideoOverlayRectangle *> rects_to_upload;
|
2019-12-24 06:54:57 +00:00
|
|
|
|
2023-06-14 15:47:11 +00:00
|
|
|
std::vector<GstD3D11CompositionOverlayPtr> overlays;
|
2019-12-24 06:54:57 +00:00
|
|
|
};
|
2023-06-14 15:47:11 +00:00
|
|
|
/* *INDENT-ON* */
|
2019-12-24 06:54:57 +00:00
|
|
|
|
2023-06-14 15:47:11 +00:00
|
|
|
static void gst_d3d11_overlay_compositor_finalize (GObject * object);
|
2022-06-21 14:43:46 +00:00
|
|
|
|
|
|
|
#define gst_d3d11_overlay_compositor_parent_class parent_class
|
2023-06-14 15:47:11 +00:00
|
|
|
G_DEFINE_TYPE (GstD3D11OverlayCompositor,
|
2022-06-21 14:43:46 +00:00
|
|
|
gst_d3d11_overlay_compositor, GST_TYPE_OBJECT);
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_d3d11_overlay_compositor_class_init (GstD3D11OverlayCompositorClass * klass)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
2023-06-14 15:47:11 +00:00
|
|
|
object_class->finalize = gst_d3d11_overlay_compositor_finalize;
|
2022-06-21 14:43:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_d3d11_overlay_compositor_init (GstD3D11OverlayCompositor * self)
|
|
|
|
{
|
2023-06-14 15:47:11 +00:00
|
|
|
self->priv = new GstD3D11OverlayCompositorPrivate ();
|
2022-06-21 14:43:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2023-06-14 15:47:11 +00:00
|
|
|
gst_d3d11_overlay_compositor_finalize (GObject * object)
|
2022-06-21 14:43:46 +00:00
|
|
|
{
|
|
|
|
GstD3D11OverlayCompositor *self = GST_D3D11_OVERLAY_COMPOSITOR (object);
|
|
|
|
|
2023-06-14 15:47:11 +00:00
|
|
|
delete self->priv;
|
2022-06-21 14:43:46 +00:00
|
|
|
|
|
|
|
gst_clear_object (&self->device);
|
|
|
|
|
2023-06-14 15:47:11 +00:00
|
|
|
G_OBJECT_CLASS (parent_class)->finalize (object);
|
2022-06-21 14:43:46 +00:00
|
|
|
}
|
|
|
|
|
2023-06-14 15:47:11 +00:00
|
|
|
static GstD3D11CompositionOverlayPtr
|
2019-12-24 06:54:57 +00:00
|
|
|
gst_d3d11_composition_overlay_new (GstD3D11OverlayCompositor * self,
|
|
|
|
GstVideoOverlayRectangle * overlay_rect)
|
|
|
|
{
|
2022-06-21 14:43:46 +00:00
|
|
|
GstD3D11OverlayCompositorPrivate *priv = self->priv;
|
2019-12-24 06:54:57 +00:00
|
|
|
gint x, y;
|
|
|
|
guint width, height;
|
2023-11-14 15:38:14 +00:00
|
|
|
D3D11_SUBRESOURCE_DATA subresource;
|
2021-03-13 08:40:57 +00:00
|
|
|
D3D11_TEXTURE2D_DESC texture_desc;
|
|
|
|
D3D11_SHADER_RESOURCE_VIEW_DESC srv_desc;
|
|
|
|
D3D11_BUFFER_DESC buffer_desc;
|
2023-11-14 15:38:14 +00:00
|
|
|
VertexData vertex_data[4];
|
2019-12-24 06:54:57 +00:00
|
|
|
GstBuffer *buf;
|
|
|
|
GstVideoMeta *vmeta;
|
|
|
|
GstMapInfo info;
|
|
|
|
guint8 *data;
|
|
|
|
gint stride;
|
|
|
|
HRESULT hr;
|
|
|
|
ID3D11Device *device_handle;
|
|
|
|
GstD3D11Device *device = self->device;
|
|
|
|
FLOAT x1, y1, x2, y2;
|
2020-06-05 13:39:54 +00:00
|
|
|
gdouble val;
|
2022-06-15 14:46:27 +00:00
|
|
|
ComPtr < ID3D11Texture2D > texture;
|
|
|
|
ComPtr < ID3D11ShaderResourceView > srv;
|
|
|
|
ComPtr < ID3D11Buffer > vertex_buffer;
|
2023-06-09 18:19:44 +00:00
|
|
|
GstVideoOverlayFormatFlags flags;
|
|
|
|
gboolean premul_alpha = FALSE;
|
2023-06-15 09:40:38 +00:00
|
|
|
GstMemory *mem;
|
|
|
|
gboolean is_d3d11 = FALSE;
|
2019-12-24 06:54:57 +00:00
|
|
|
|
2023-11-14 15:38:14 +00:00
|
|
|
memset (&subresource, 0, sizeof (subresource));
|
2021-03-13 08:40:57 +00:00
|
|
|
memset (&texture_desc, 0, sizeof (texture_desc));
|
|
|
|
memset (&srv_desc, 0, sizeof (srv_desc));
|
|
|
|
memset (&buffer_desc, 0, sizeof (buffer_desc));
|
|
|
|
|
2019-12-24 06:54:57 +00:00
|
|
|
device_handle = gst_d3d11_device_get_device_handle (device);
|
|
|
|
|
|
|
|
if (!gst_video_overlay_rectangle_get_render_rectangle (overlay_rect, &x, &y,
|
|
|
|
&width, &height)) {
|
2022-06-21 14:43:46 +00:00
|
|
|
GST_ERROR_OBJECT (self, "Failed to get render rectangle");
|
|
|
|
return nullptr;
|
2019-12-24 06:54:57 +00:00
|
|
|
}
|
|
|
|
|
2023-06-09 18:19:44 +00:00
|
|
|
flags = gst_video_overlay_rectangle_get_flags (overlay_rect);
|
|
|
|
if ((flags & GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA) != 0) {
|
|
|
|
premul_alpha = TRUE;
|
|
|
|
flags = GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA;
|
|
|
|
} else {
|
|
|
|
flags = GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE;
|
|
|
|
}
|
|
|
|
|
2019-12-24 06:54:57 +00:00
|
|
|
buf = gst_video_overlay_rectangle_get_pixels_unscaled_argb (overlay_rect,
|
2023-06-09 18:19:44 +00:00
|
|
|
flags);
|
2019-12-24 06:54:57 +00:00
|
|
|
if (!buf) {
|
2022-06-21 14:43:46 +00:00
|
|
|
GST_ERROR_OBJECT (self, "Failed to get overlay buffer");
|
|
|
|
return nullptr;
|
2019-12-24 06:54:57 +00:00
|
|
|
}
|
|
|
|
|
2023-06-15 09:40:38 +00:00
|
|
|
mem = gst_buffer_peek_memory (buf, 0);
|
|
|
|
if (gst_is_d3d11_memory (mem)) {
|
|
|
|
GstD3D11Memory *dmem = GST_D3D11_MEMORY_CAST (mem);
|
|
|
|
if (dmem->device == self->device) {
|
|
|
|
srv = gst_d3d11_memory_get_shader_resource_view (dmem, 0);
|
|
|
|
if (srv) {
|
|
|
|
texture = (ID3D11Texture2D *)
|
|
|
|
gst_d3d11_memory_get_resource_handle (dmem);
|
|
|
|
is_d3d11 = TRUE;
|
|
|
|
}
|
|
|
|
}
|
2019-12-24 06:54:57 +00:00
|
|
|
}
|
|
|
|
|
2023-06-15 09:40:38 +00:00
|
|
|
if (!is_d3d11) {
|
|
|
|
vmeta = gst_buffer_get_video_meta (buf);
|
|
|
|
if (!vmeta) {
|
|
|
|
GST_ERROR_OBJECT (self, "Failed to get video meta");
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-12-24 06:54:57 +00:00
|
|
|
|
2023-06-15 09:40:38 +00:00
|
|
|
if (!gst_video_meta_map (vmeta,
|
|
|
|
0, &info, (gpointer *) & data, &stride, GST_MAP_READ)) {
|
|
|
|
GST_ERROR_OBJECT (self, "Failed to map");
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-12-24 06:54:57 +00:00
|
|
|
|
2023-06-15 09:40:38 +00:00
|
|
|
/* Do create texture and upload data at once, for create immutable texture */
|
2023-11-14 15:38:14 +00:00
|
|
|
subresource.pSysMem = data;
|
|
|
|
subresource.SysMemPitch = stride;
|
2023-06-15 09:40:38 +00:00
|
|
|
|
|
|
|
texture_desc.Width = vmeta->width;
|
|
|
|
texture_desc.Height = vmeta->height;
|
|
|
|
texture_desc.MipLevels = 1;
|
|
|
|
texture_desc.ArraySize = 1;
|
|
|
|
texture_desc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
|
|
|
|
texture_desc.SampleDesc.Count = 1;
|
|
|
|
texture_desc.SampleDesc.Quality = 0;
|
|
|
|
texture_desc.Usage = D3D11_USAGE_IMMUTABLE;
|
|
|
|
texture_desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
|
|
|
|
texture_desc.CPUAccessFlags = 0;
|
|
|
|
|
2023-11-14 15:38:14 +00:00
|
|
|
hr = device_handle->CreateTexture2D (&texture_desc, &subresource, &texture);
|
2023-06-15 09:40:38 +00:00
|
|
|
gst_video_meta_unmap (vmeta, 0, &info);
|
|
|
|
|
|
|
|
if (!gst_d3d11_result (hr, device)) {
|
|
|
|
GST_ERROR_OBJECT (self, "Failed to create texture");
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-12-24 06:54:57 +00:00
|
|
|
|
2023-06-15 09:40:38 +00:00
|
|
|
srv_desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
|
|
|
|
srv_desc.Texture2D.MipLevels = 1;
|
2019-12-24 06:54:57 +00:00
|
|
|
|
2023-06-15 09:40:38 +00:00
|
|
|
hr = device_handle->CreateShaderResourceView (texture.Get (), &srv_desc,
|
|
|
|
&srv);
|
|
|
|
if (!gst_d3d11_result (hr, device) || !srv) {
|
|
|
|
GST_ERROR_OBJECT (self, "Failed to create shader resource view");
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-12-24 06:54:57 +00:00
|
|
|
}
|
|
|
|
|
2020-06-05 13:39:54 +00:00
|
|
|
/* bottom left */
|
2022-06-21 14:43:46 +00:00
|
|
|
gst_util_fraction_to_double (x, GST_VIDEO_INFO_WIDTH (&priv->info), &val);
|
2020-06-05 13:39:54 +00:00
|
|
|
x1 = (val * 2.0f) - 1.0f;
|
2019-12-24 06:54:57 +00:00
|
|
|
|
2020-06-05 13:39:54 +00:00
|
|
|
gst_util_fraction_to_double (y + height,
|
2022-06-21 14:43:46 +00:00
|
|
|
GST_VIDEO_INFO_HEIGHT (&priv->info), &val);
|
2020-06-05 13:39:54 +00:00
|
|
|
y1 = (val * -2.0f) + 1.0f;
|
2019-12-24 06:54:57 +00:00
|
|
|
|
2020-06-05 13:39:54 +00:00
|
|
|
/* top right */
|
|
|
|
gst_util_fraction_to_double (x + width,
|
2022-06-21 14:43:46 +00:00
|
|
|
GST_VIDEO_INFO_WIDTH (&priv->info), &val);
|
2020-06-05 13:39:54 +00:00
|
|
|
x2 = (val * 2.0f) - 1.0f;
|
|
|
|
|
2022-06-21 14:43:46 +00:00
|
|
|
gst_util_fraction_to_double (y, GST_VIDEO_INFO_HEIGHT (&priv->info), &val);
|
2020-06-05 13:39:54 +00:00
|
|
|
y2 = (val * -2.0f) + 1.0f;
|
|
|
|
|
|
|
|
/* bottom left */
|
2019-12-24 06:54:57 +00:00
|
|
|
vertex_data[0].position.x = x1;
|
|
|
|
vertex_data[0].position.y = y1;
|
|
|
|
vertex_data[0].position.z = 0.0f;
|
2022-06-15 14:46:27 +00:00
|
|
|
vertex_data[0].texture.u = 0.0f;
|
|
|
|
vertex_data[0].texture.v = 1.0f;
|
2019-12-24 06:54:57 +00:00
|
|
|
|
2020-06-05 13:39:54 +00:00
|
|
|
/* top left */
|
2019-12-24 06:54:57 +00:00
|
|
|
vertex_data[1].position.x = x1;
|
|
|
|
vertex_data[1].position.y = y2;
|
|
|
|
vertex_data[1].position.z = 0.0f;
|
2022-06-15 14:46:27 +00:00
|
|
|
vertex_data[1].texture.u = 0.0f;
|
|
|
|
vertex_data[1].texture.v = 0.0f;
|
2019-12-24 06:54:57 +00:00
|
|
|
|
2020-06-05 13:39:54 +00:00
|
|
|
/* top right */
|
2019-12-24 06:54:57 +00:00
|
|
|
vertex_data[2].position.x = x2;
|
|
|
|
vertex_data[2].position.y = y2;
|
|
|
|
vertex_data[2].position.z = 0.0f;
|
2022-06-15 14:46:27 +00:00
|
|
|
vertex_data[2].texture.u = 1.0f;
|
|
|
|
vertex_data[2].texture.v = 0.0f;
|
2019-12-24 06:54:57 +00:00
|
|
|
|
2020-06-05 13:39:54 +00:00
|
|
|
/* bottom right */
|
2019-12-24 06:54:57 +00:00
|
|
|
vertex_data[3].position.x = x2;
|
|
|
|
vertex_data[3].position.y = y1;
|
|
|
|
vertex_data[3].position.z = 0.0f;
|
2022-06-15 14:46:27 +00:00
|
|
|
vertex_data[3].texture.u = 1.0f;
|
|
|
|
vertex_data[3].texture.v = 1.0f;
|
2019-12-24 06:54:57 +00:00
|
|
|
|
2023-11-14 15:38:14 +00:00
|
|
|
buffer_desc.Usage = D3D11_USAGE_DYNAMIC;
|
|
|
|
buffer_desc.ByteWidth = sizeof (VertexData) * 4;
|
|
|
|
buffer_desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
|
|
|
|
buffer_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
|
|
|
|
|
|
|
subresource.pSysMem = vertex_data;
|
|
|
|
subresource.SysMemPitch = sizeof (VertexData) * 4;
|
|
|
|
|
|
|
|
hr = device_handle->CreateBuffer (&buffer_desc, &subresource, &vertex_buffer);
|
|
|
|
if (!gst_d3d11_result (hr, device)) {
|
|
|
|
GST_ERROR_OBJECT (self,
|
|
|
|
"Couldn't create vertex buffer, hr: 0x%x", (guint) hr);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-12-24 06:54:57 +00:00
|
|
|
|
2023-06-14 15:47:11 +00:00
|
|
|
auto overlay = std::make_shared < GstD3D11CompositionOverlay > ();
|
2019-12-24 06:54:57 +00:00
|
|
|
overlay->overlay_rect = gst_video_overlay_rectangle_ref (overlay_rect);
|
2023-06-14 15:47:11 +00:00
|
|
|
overlay->texture = texture;
|
|
|
|
overlay->srv = srv;
|
|
|
|
overlay->vertex_buffer = vertex_buffer;
|
2023-06-09 18:19:44 +00:00
|
|
|
overlay->premul_alpha = premul_alpha;
|
2023-06-15 09:40:38 +00:00
|
|
|
if (is_d3d11)
|
|
|
|
overlay->d3d11_buffer = gst_buffer_ref (buf);
|
2019-12-24 06:54:57 +00:00
|
|
|
|
|
|
|
return overlay;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2022-06-21 14:43:46 +00:00
|
|
|
gst_d3d11_overlay_compositor_setup_shader (GstD3D11OverlayCompositor * self)
|
2019-12-24 06:54:57 +00:00
|
|
|
{
|
2022-06-21 14:43:46 +00:00
|
|
|
GstD3D11OverlayCompositorPrivate *priv = self->priv;
|
|
|
|
GstVideoInfo *info = &priv->info;
|
|
|
|
GstD3D11Device *device = self->device;
|
2019-12-24 06:54:57 +00:00
|
|
|
HRESULT hr;
|
2021-03-13 08:40:57 +00:00
|
|
|
D3D11_BUFFER_DESC buffer_desc;
|
|
|
|
D3D11_BLEND_DESC blend_desc;
|
2023-11-14 15:38:14 +00:00
|
|
|
D3D11_SUBRESOURCE_DATA subresource;
|
|
|
|
const WORD indices[6] = { 0, 1, 2, 3, 0, 2 };
|
2019-12-24 06:54:57 +00:00
|
|
|
ID3D11Device *device_handle;
|
2022-06-15 14:46:27 +00:00
|
|
|
ComPtr < ID3D11PixelShader > ps;
|
2023-06-09 18:19:44 +00:00
|
|
|
ComPtr < ID3D11PixelShader > premul_ps;
|
2022-06-15 14:46:27 +00:00
|
|
|
ComPtr < ID3D11VertexShader > vs;
|
|
|
|
ComPtr < ID3D11InputLayout > layout;
|
|
|
|
ComPtr < ID3D11SamplerState > sampler;
|
|
|
|
ComPtr < ID3D11BlendState > blend;
|
|
|
|
ComPtr < ID3D11Buffer > index_buffer;
|
2023-10-28 10:34:55 +00:00
|
|
|
ComPtr < ID3D11RasterizerState > rs;
|
2021-03-13 08:40:57 +00:00
|
|
|
|
|
|
|
memset (&buffer_desc, 0, sizeof (buffer_desc));
|
|
|
|
memset (&blend_desc, 0, sizeof (blend_desc));
|
2023-11-14 15:38:14 +00:00
|
|
|
memset (&subresource, 0, sizeof (subresource));
|
2019-12-24 06:54:57 +00:00
|
|
|
|
|
|
|
device_handle = gst_d3d11_device_get_device_handle (device);
|
|
|
|
|
2023-10-10 11:01:57 +00:00
|
|
|
hr = gst_d3d11_device_get_sampler (device,
|
|
|
|
D3D11_FILTER_MIN_MAG_LINEAR_MIP_POINT, &sampler);
|
2019-12-24 06:54:57 +00:00
|
|
|
if (!gst_d3d11_result (hr, device)) {
|
2022-06-21 14:43:46 +00:00
|
|
|
GST_ERROR_OBJECT (self, "Couldn't create sampler state, hr: 0x%x",
|
|
|
|
(guint) hr);
|
2021-03-13 08:40:57 +00:00
|
|
|
return FALSE;
|
2019-12-24 06:54:57 +00:00
|
|
|
}
|
|
|
|
|
2023-09-30 13:21:40 +00:00
|
|
|
hr = gst_d3d11_get_pixel_shader_sample (device, &ps);
|
2022-06-15 18:57:37 +00:00
|
|
|
if (!gst_d3d11_result (hr, device)) {
|
2022-06-21 14:43:46 +00:00
|
|
|
GST_ERROR_OBJECT (self, "Couldn't create pixel shader");
|
2021-03-13 08:40:57 +00:00
|
|
|
return FALSE;
|
2019-12-24 06:54:57 +00:00
|
|
|
}
|
|
|
|
|
2023-09-30 13:21:40 +00:00
|
|
|
hr = gst_d3d11_get_pixel_shader_sample_premul (device, &premul_ps);
|
2023-06-09 18:19:44 +00:00
|
|
|
if (!gst_d3d11_result (hr, device)) {
|
|
|
|
GST_ERROR_OBJECT (self, "Couldn't create premul pixel shader");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2023-09-30 13:21:40 +00:00
|
|
|
hr = gst_d3d11_get_vertex_shader_coord (device, &vs, &layout);
|
2022-06-15 18:57:37 +00:00
|
|
|
if (!gst_d3d11_result (hr, device)) {
|
2023-09-30 13:21:40 +00:00
|
|
|
GST_ERROR_OBJECT (self, "Couldn't create vertex pixel shader");
|
2021-03-13 08:40:57 +00:00
|
|
|
return FALSE;
|
2019-12-24 06:54:57 +00:00
|
|
|
}
|
|
|
|
|
2023-10-28 10:34:55 +00:00
|
|
|
hr = gst_d3d11_device_get_rasterizer (device, &rs);
|
|
|
|
if (!gst_d3d11_result (hr, device)) {
|
|
|
|
GST_ERROR_OBJECT (self, "Couldn't get rasterizer state");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2019-12-24 06:54:57 +00:00
|
|
|
blend_desc.AlphaToCoverageEnable = FALSE;
|
|
|
|
blend_desc.IndependentBlendEnable = FALSE;
|
|
|
|
blend_desc.RenderTarget[0].BlendEnable = TRUE;
|
2023-08-24 16:40:08 +00:00
|
|
|
blend_desc.RenderTarget[0].SrcBlend = D3D11_BLEND_ONE;
|
2019-12-24 06:54:57 +00:00
|
|
|
blend_desc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
|
|
|
|
blend_desc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
|
|
|
|
blend_desc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;
|
2023-08-24 16:40:08 +00:00
|
|
|
blend_desc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA;
|
2019-12-24 06:54:57 +00:00
|
|
|
blend_desc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
|
|
|
|
blend_desc.RenderTarget[0].RenderTargetWriteMask =
|
|
|
|
D3D11_COLOR_WRITE_ENABLE_ALL;
|
|
|
|
|
2021-03-13 08:40:57 +00:00
|
|
|
hr = device_handle->CreateBlendState (&blend_desc, &blend);
|
2019-12-24 06:54:57 +00:00
|
|
|
if (!gst_d3d11_result (hr, device)) {
|
2022-06-21 14:43:46 +00:00
|
|
|
GST_ERROR_OBJECT (self,
|
|
|
|
"Couldn't create blend staten, hr: 0x%x", (guint) hr);
|
2021-03-13 08:40:57 +00:00
|
|
|
return FALSE;
|
2019-12-24 06:54:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
buffer_desc.Usage = D3D11_USAGE_DYNAMIC;
|
2022-06-15 14:46:27 +00:00
|
|
|
buffer_desc.ByteWidth = sizeof (WORD) * 6;
|
2019-12-24 06:54:57 +00:00
|
|
|
buffer_desc.BindFlags = D3D11_BIND_INDEX_BUFFER;
|
|
|
|
buffer_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
|
|
|
|
2023-11-14 15:38:14 +00:00
|
|
|
subresource.pSysMem = indices;
|
|
|
|
subresource.SysMemPitch = sizeof (WORD) * 6;
|
|
|
|
|
|
|
|
hr = device_handle->CreateBuffer (&buffer_desc, &subresource, &index_buffer);
|
2019-12-24 06:54:57 +00:00
|
|
|
if (!gst_d3d11_result (hr, device)) {
|
2022-06-21 14:43:46 +00:00
|
|
|
GST_ERROR_OBJECT (self,
|
|
|
|
"Couldn't create index buffer, hr: 0x%x", (guint) hr);
|
2021-03-13 08:40:57 +00:00
|
|
|
return FALSE;
|
2019-12-24 06:54:57 +00:00
|
|
|
}
|
|
|
|
|
2023-06-18 14:58:22 +00:00
|
|
|
priv->ps = ps;
|
|
|
|
priv->premul_ps = premul_ps;
|
|
|
|
priv->vs = vs;
|
|
|
|
priv->layout = layout;
|
|
|
|
priv->sampler = sampler;
|
|
|
|
priv->blend = blend;
|
|
|
|
priv->index_buffer = index_buffer;
|
2023-10-28 10:34:55 +00:00
|
|
|
priv->rs = rs;
|
2022-06-21 14:43:46 +00:00
|
|
|
|
|
|
|
priv->viewport.TopLeftX = 0;
|
|
|
|
priv->viewport.TopLeftY = 0;
|
|
|
|
priv->viewport.Width = GST_VIDEO_INFO_WIDTH (info);
|
|
|
|
priv->viewport.Height = GST_VIDEO_INFO_HEIGHT (info);
|
|
|
|
priv->viewport.MinDepth = 0.0f;
|
|
|
|
priv->viewport.MaxDepth = 1.0f;
|
2019-12-24 06:54:57 +00:00
|
|
|
|
2021-03-13 08:40:57 +00:00
|
|
|
return TRUE;
|
2019-12-24 06:54:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GstD3D11OverlayCompositor *
|
|
|
|
gst_d3d11_overlay_compositor_new (GstD3D11Device * device,
|
2022-06-21 14:43:46 +00:00
|
|
|
const GstVideoInfo * info)
|
2019-12-24 06:54:57 +00:00
|
|
|
{
|
2022-06-21 14:43:46 +00:00
|
|
|
GstD3D11OverlayCompositor *self = nullptr;
|
|
|
|
GstD3D11OverlayCompositorPrivate *priv;
|
2019-12-24 06:54:57 +00:00
|
|
|
|
2022-06-21 14:43:46 +00:00
|
|
|
g_return_val_if_fail (GST_IS_D3D11_DEVICE (device), nullptr);
|
|
|
|
g_return_val_if_fail (info != nullptr, nullptr);
|
2019-12-24 06:54:57 +00:00
|
|
|
|
2022-06-21 14:43:46 +00:00
|
|
|
self = (GstD3D11OverlayCompositor *)
|
|
|
|
g_object_new (GST_TYPE_D3D11_OVERLAY_COMPOSITOR, nullptr);
|
|
|
|
gst_object_ref_sink (self);
|
|
|
|
priv = self->priv;
|
2019-12-24 06:54:57 +00:00
|
|
|
|
2022-06-21 14:43:46 +00:00
|
|
|
self->device = (GstD3D11Device *) gst_object_ref (device);
|
|
|
|
priv->info = *info;
|
2019-12-24 06:54:57 +00:00
|
|
|
|
2022-06-21 14:43:46 +00:00
|
|
|
if (!gst_d3d11_overlay_compositor_setup_shader (self)) {
|
|
|
|
gst_object_unref (self);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-12-24 06:54:57 +00:00
|
|
|
|
2022-06-21 14:43:46 +00:00
|
|
|
return self;
|
2019-12-24 06:54:57 +00:00
|
|
|
}
|
|
|
|
|
2023-06-14 15:47:11 +00:00
|
|
|
static gboolean
|
|
|
|
gst_d3d11_overlay_compositor_foreach_meta (GstBuffer * buffer, GstMeta ** meta,
|
2023-10-31 15:38:31 +00:00
|
|
|
GstD3D11OverlayCompositor * self)
|
2019-12-24 06:54:57 +00:00
|
|
|
{
|
2023-10-31 15:38:31 +00:00
|
|
|
GstD3D11OverlayCompositorPrivate *priv = self->priv;
|
2023-06-14 15:47:11 +00:00
|
|
|
GstVideoOverlayCompositionMeta *cmeta;
|
|
|
|
guint num_rect;
|
2019-12-24 06:54:57 +00:00
|
|
|
|
2023-06-14 15:47:11 +00:00
|
|
|
if ((*meta)->info->api != GST_VIDEO_OVERLAY_COMPOSITION_META_API_TYPE)
|
|
|
|
return TRUE;
|
2019-12-24 06:54:57 +00:00
|
|
|
|
2023-06-14 15:47:11 +00:00
|
|
|
cmeta = (GstVideoOverlayCompositionMeta *) (*meta);
|
|
|
|
if (!cmeta->overlay)
|
|
|
|
return TRUE;
|
2019-12-24 06:54:57 +00:00
|
|
|
|
2023-06-14 15:47:11 +00:00
|
|
|
num_rect = gst_video_overlay_composition_n_rectangles (cmeta->overlay);
|
|
|
|
for (guint i = 0; i < num_rect; i++) {
|
|
|
|
auto rect = gst_video_overlay_composition_get_rectangle (cmeta->overlay, i);
|
2023-10-31 15:38:31 +00:00
|
|
|
priv->rects_to_upload.push_back (rect);
|
2019-12-24 06:54:57 +00:00
|
|
|
}
|
2023-06-14 15:47:11 +00:00
|
|
|
|
|
|
|
return TRUE;
|
2019-12-24 06:54:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_d3d11_overlay_compositor_upload (GstD3D11OverlayCompositor * compositor,
|
|
|
|
GstBuffer * buf)
|
|
|
|
{
|
2022-06-21 14:43:46 +00:00
|
|
|
GstD3D11OverlayCompositorPrivate *priv;
|
2019-12-24 06:54:57 +00:00
|
|
|
|
2022-06-21 14:43:46 +00:00
|
|
|
g_return_val_if_fail (compositor != nullptr, FALSE);
|
2019-12-24 06:54:57 +00:00
|
|
|
g_return_val_if_fail (GST_IS_BUFFER (buf), FALSE);
|
|
|
|
|
2022-06-21 14:43:46 +00:00
|
|
|
priv = compositor->priv;
|
2023-10-31 15:38:31 +00:00
|
|
|
priv->rects_to_upload.clear ();
|
2019-12-24 06:54:57 +00:00
|
|
|
|
2023-06-14 15:47:11 +00:00
|
|
|
gst_buffer_foreach_meta (buf,
|
|
|
|
(GstBufferForeachMetaFunc) gst_d3d11_overlay_compositor_foreach_meta,
|
2023-10-31 15:38:31 +00:00
|
|
|
compositor);
|
2019-12-24 06:54:57 +00:00
|
|
|
|
2023-10-31 15:38:31 +00:00
|
|
|
if (priv->rects_to_upload.empty ()) {
|
2023-06-14 15:47:11 +00:00
|
|
|
priv->overlays.clear ();
|
2019-12-24 06:54:57 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2023-06-14 15:47:11 +00:00
|
|
|
GST_LOG_OBJECT (compositor, "Found %" G_GSIZE_FORMAT
|
|
|
|
" overlay rectangles, %" G_GSIZE_FORMAT " in current queue",
|
2023-10-31 15:38:31 +00:00
|
|
|
priv->rects_to_upload.size (), priv->overlays.size ());
|
2023-06-14 15:47:11 +00:00
|
|
|
|
|
|
|
/* *INDENT-OFF* */
|
2023-10-31 15:38:31 +00:00
|
|
|
for (auto it : priv->rects_to_upload) {
|
2023-06-14 15:47:11 +00:00
|
|
|
if (std::find_if (priv->overlays.begin (), priv->overlays.end (),
|
|
|
|
[&] (const auto & overlay) -> bool {
|
|
|
|
return overlay->overlay_rect == it;
|
|
|
|
}) == priv->overlays.end ()) {
|
|
|
|
auto new_overlay = gst_d3d11_composition_overlay_new (compositor, it);
|
|
|
|
if (!new_overlay)
|
2019-12-24 06:54:57 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2023-06-14 15:47:11 +00:00
|
|
|
priv->overlays.push_back (new_overlay);
|
2019-12-24 06:54:57 +00:00
|
|
|
}
|
|
|
|
}
|
2023-06-14 15:47:11 +00:00
|
|
|
/* *INDENT-ON* */
|
|
|
|
|
|
|
|
GST_LOG_OBJECT (compositor, "Overlay rectangles in queue after uploaded %"
|
|
|
|
G_GSIZE_FORMAT, priv->overlays.size ());
|
2019-12-24 06:54:57 +00:00
|
|
|
|
|
|
|
/* Remove old overlay */
|
2023-06-14 15:47:11 +00:00
|
|
|
/* *INDENT-OFF* */
|
|
|
|
auto it = priv->overlays.begin ();
|
|
|
|
while (it != priv->overlays.end ()) {
|
|
|
|
auto old_overlay = *it;
|
2023-10-31 15:38:31 +00:00
|
|
|
if (std::find_if (priv->rects_to_upload.begin (),
|
|
|
|
priv->rects_to_upload.end (), [&] (const auto & overlay) -> bool {
|
2023-06-14 15:47:11 +00:00
|
|
|
return overlay == old_overlay->overlay_rect;
|
2023-10-31 15:38:31 +00:00
|
|
|
}) == priv->rects_to_upload.end ()) {
|
2023-06-14 15:47:11 +00:00
|
|
|
GST_LOG_OBJECT (compositor, "Removing %p from queue",
|
|
|
|
old_overlay->overlay_rect);
|
|
|
|
it = priv->overlays.erase (it);
|
|
|
|
} else {
|
|
|
|
it++;
|
2019-12-24 06:54:57 +00:00
|
|
|
}
|
|
|
|
}
|
2023-06-14 15:47:11 +00:00
|
|
|
/* *INDENT-ON* */
|
|
|
|
|
|
|
|
GST_LOG_OBJECT (compositor, "Final queue size %" G_GSIZE_FORMAT,
|
|
|
|
priv->overlays.size ());
|
2019-12-24 06:54:57 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
2020-06-02 15:46:13 +00:00
|
|
|
gst_d3d11_overlay_compositor_update_viewport (GstD3D11OverlayCompositor *
|
|
|
|
compositor, D3D11_VIEWPORT * viewport)
|
2019-12-24 06:54:57 +00:00
|
|
|
{
|
2022-06-21 14:43:46 +00:00
|
|
|
g_return_val_if_fail (GST_IS_D3D11_OVERLAY_COMPOSITOR (compositor), FALSE);
|
|
|
|
g_return_val_if_fail (viewport != nullptr, FALSE);
|
2019-12-24 06:54:57 +00:00
|
|
|
|
2022-06-21 14:43:46 +00:00
|
|
|
compositor->priv->viewport = *viewport;
|
2019-12-24 06:54:57 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_d3d11_overlay_compositor_draw (GstD3D11OverlayCompositor * compositor,
|
|
|
|
ID3D11RenderTargetView * rtv[GST_VIDEO_MAX_PLANES])
|
|
|
|
{
|
2022-06-21 14:43:46 +00:00
|
|
|
g_return_val_if_fail (compositor != nullptr, FALSE);
|
|
|
|
g_return_val_if_fail (rtv != nullptr, FALSE);
|
2019-12-24 06:54:57 +00:00
|
|
|
|
2022-07-07 17:24:56 +00:00
|
|
|
GstD3D11DeviceLockGuard lk (compositor->device);
|
|
|
|
return gst_d3d11_overlay_compositor_draw_unlocked (compositor, rtv);
|
2019-12-24 06:54:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_d3d11_overlay_compositor_draw_unlocked (GstD3D11OverlayCompositor *
|
|
|
|
compositor, ID3D11RenderTargetView * rtv[GST_VIDEO_MAX_PLANES])
|
|
|
|
{
|
2022-06-21 14:43:46 +00:00
|
|
|
GstD3D11OverlayCompositorPrivate *priv;
|
2022-06-15 14:46:27 +00:00
|
|
|
ID3D11DeviceContext *context;
|
|
|
|
ID3D11ShaderResourceView *clear_view[GST_VIDEO_MAX_PLANES] = { nullptr, };
|
|
|
|
UINT strides = sizeof (VertexData);
|
|
|
|
UINT offsets = 0;
|
2023-06-14 15:47:11 +00:00
|
|
|
ID3D11SamplerState *samplers[1];
|
2019-12-24 06:54:57 +00:00
|
|
|
|
2022-06-21 14:43:46 +00:00
|
|
|
g_return_val_if_fail (compositor != nullptr, FALSE);
|
|
|
|
g_return_val_if_fail (rtv != nullptr, FALSE);
|
|
|
|
|
|
|
|
priv = compositor->priv;
|
|
|
|
|
2023-06-14 15:47:11 +00:00
|
|
|
if (priv->overlays.empty ())
|
2022-06-21 14:43:46 +00:00
|
|
|
return TRUE;
|
2019-12-24 06:54:57 +00:00
|
|
|
|
2023-06-14 15:47:11 +00:00
|
|
|
samplers[0] = priv->sampler.Get ();
|
|
|
|
|
2022-06-15 14:46:27 +00:00
|
|
|
context = gst_d3d11_device_get_device_context_handle (compositor->device);
|
|
|
|
context->IASetPrimitiveTopology (D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
|
2023-06-14 15:47:11 +00:00
|
|
|
context->IASetInputLayout (priv->layout.Get ());
|
|
|
|
context->IASetIndexBuffer (priv->index_buffer.Get (),
|
|
|
|
DXGI_FORMAT_R16_UINT, 0);
|
|
|
|
context->PSSetSamplers (0, 1, samplers);
|
|
|
|
context->VSSetShader (priv->vs.Get (), nullptr, 0);
|
2022-06-21 14:43:46 +00:00
|
|
|
context->RSSetViewports (1, &priv->viewport);
|
2023-10-28 10:34:55 +00:00
|
|
|
context->RSSetState (priv->rs.Get ());
|
2022-06-15 14:46:27 +00:00
|
|
|
context->OMSetRenderTargets (1, rtv, nullptr);
|
2023-06-14 15:47:11 +00:00
|
|
|
context->OMSetBlendState (priv->blend.Get (), nullptr, 0xffffffff);
|
2022-06-15 14:46:27 +00:00
|
|
|
|
2023-06-14 15:47:11 +00:00
|
|
|
/* *INDENT-OFF* */
|
|
|
|
for (auto overlay : priv->overlays) {
|
|
|
|
ID3D11ShaderResourceView *srv[] = { overlay->srv.Get () };
|
|
|
|
ID3D11Buffer *vertex_buf[] = { overlay->vertex_buffer.Get () };
|
2023-06-15 09:40:38 +00:00
|
|
|
GstMapInfo info;
|
|
|
|
GstMemory *mem = nullptr;
|
|
|
|
|
|
|
|
if (overlay->d3d11_buffer) {
|
|
|
|
mem = gst_buffer_peek_memory (overlay->d3d11_buffer, 0);
|
|
|
|
gst_memory_map (mem, &info, (GstMapFlags) (GST_MAP_D3D11 | GST_MAP_READ));
|
|
|
|
}
|
2019-12-24 06:54:57 +00:00
|
|
|
|
2023-08-24 16:40:08 +00:00
|
|
|
if (!overlay->premul_alpha)
|
2023-06-14 15:47:11 +00:00
|
|
|
context->PSSetShader (priv->premul_ps.Get (), nullptr, 0);
|
2023-06-09 18:19:44 +00:00
|
|
|
else
|
2023-06-14 15:47:11 +00:00
|
|
|
context->PSSetShader (priv->ps.Get (), nullptr, 0);
|
2023-06-09 18:19:44 +00:00
|
|
|
|
2023-06-14 15:47:11 +00:00
|
|
|
context->PSSetShaderResources (0, 1, srv);
|
|
|
|
context->IASetVertexBuffers (0, 1, vertex_buf, &strides, &offsets);
|
2019-12-24 06:54:57 +00:00
|
|
|
|
2022-06-15 14:46:27 +00:00
|
|
|
context->DrawIndexed (6, 0, 0);
|
2023-06-15 09:40:38 +00:00
|
|
|
|
|
|
|
if (mem)
|
|
|
|
gst_memory_unmap (mem, &info);
|
2019-12-24 06:54:57 +00:00
|
|
|
}
|
2023-06-14 15:47:11 +00:00
|
|
|
/* *INDENT-ON* */
|
2019-12-24 06:54:57 +00:00
|
|
|
|
2022-06-15 14:46:27 +00:00
|
|
|
context->PSSetShaderResources (0, 1, clear_view);
|
|
|
|
context->OMSetRenderTargets (0, nullptr, nullptr);
|
|
|
|
|
|
|
|
return TRUE;
|
2019-12-24 06:54:57 +00:00
|
|
|
}
|