File: d3d12device.cpp

package info (click to toggle)
gst-plugins-bad1.0 1.26.10-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 69,996 kB
  • sloc: ansic: 722,568; cpp: 278,154; objc: 3,556; xml: 3,351; sh: 1,095; python: 508; makefile: 175; java: 75
file content (185 lines) | stat: -rw-r--r-- 4,781 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* GStreamer
 * Copyright (C) 2024 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/check/gstcheck.h>
#include <gst/d3d12/gstd3d12.h>
#include <wrl.h>
#include <mutex>
#include <condition_variable>

/* *INDENT-OFF* */
using namespace Microsoft::WRL;
/* *INDENT-ON* */

GST_START_TEST (test_device_equal)
{
  auto device = gst_d3d12_device_new (0);
  fail_unless (GST_IS_D3D12_DEVICE (device));

  auto other_device = gst_d3d12_device_new (0);
  fail_unless (GST_IS_D3D12_DEVICE (other_device));
  fail_unless (gst_d3d12_device_is_equal (device, other_device));

  auto handle = gst_d3d12_device_get_device_handle (device);
  auto other_handle = gst_d3d12_device_get_device_handle (other_device);
  fail_unless_equals_pointer (handle, other_handle);

  gst_object_unref (device);
  gst_object_unref (other_device);
}

GST_END_TEST;

struct DeviceRemovedData
{
  std::mutex lock;
  std::condition_variable cond;
  guint removed_count = 0;
};

static void
on_device_removed (GstD3D12Device * device, GParamSpec * pspec,
    DeviceRemovedData * data)
{
  HRESULT hr = S_OK;
  g_object_get (device, "device-removed-reason", &hr, nullptr);
  fail_unless (FAILED (hr));

  std::lock_guard <std::mutex> lk (data->lock);
  data->removed_count++;
  data->cond.notify_all ();
}

GST_START_TEST (test_device_removed)
{
  auto device = gst_d3d12_device_new (0);
  fail_unless (GST_IS_D3D12_DEVICE (device));

  ComPtr<ID3D12Device5> device5;
  auto handle = gst_d3d12_device_get_device_handle (device);
  fail_unless (handle != nullptr);

  handle->QueryInterface (IID_PPV_ARGS (&device5));
  if (!device5) {
    gst_object_unref (device);
    return;
  }

  auto other_device = gst_d3d12_device_new (0);

  DeviceRemovedData data;

  g_signal_connect (device, "notify::device-removed-reason",
      G_CALLBACK (on_device_removed), &data);
  g_signal_connect (other_device, "notify::device-removed-reason",
      G_CALLBACK (on_device_removed), &data);

  /* Emulate device removed case */
  device5->RemoveDevice ();
  device5 = nullptr;

  /* Callback will be called from other thread */
  {
    std::unique_lock <std::mutex> lk (data.lock);
    while (data.removed_count != 2)
      data.cond.wait (lk);
  }

  /* This will fail since we are holding removed device */
  auto null_device = gst_d3d12_device_new (0);
  fail_if (null_device);

  gst_object_unref (device);
  gst_object_unref (other_device);

  /* After releasing all devices, create device should be successful */
  device = gst_d3d12_device_new (0);
  fail_unless (GST_IS_D3D12_DEVICE (device));

  gst_object_unref (device);
}

GST_END_TEST;

static gboolean
check_d3d12_available (void)
{
  auto device = gst_d3d12_device_new (0);
  if (!device)
    return FALSE;

  gst_object_unref (device);

  return TRUE;
}

/* ID3D12Device5::RemoveDevice requires Windows10 build 20348 or newer */
static gboolean
check_remove_device_supported (void)
{
  OSVERSIONINFOEXW osverinfo = { };
  typedef NTSTATUS (WINAPI fRtlGetVersion) (PRTL_OSVERSIONINFOEXW);
  gboolean ret = FALSE;

  memset (&osverinfo, 0, sizeof (OSVERSIONINFOEXW));
  osverinfo.dwOSVersionInfoSize = sizeof (OSVERSIONINFOEXW);

  auto hmodule = LoadLibraryW (L"ntdll.dll");
  if (!hmodule)
    return FALSE;

  auto RtlGetVersion = (fRtlGetVersion *) GetProcAddress (hmodule, "RtlGetVersion");
  if (RtlGetVersion) {
    RtlGetVersion (&osverinfo);

    if (osverinfo.dwMajorVersion > 10 ||
        (osverinfo.dwMajorVersion == 10 && osverinfo.dwBuildNumber >= 20348)) {
      ret = TRUE;
    }
  }

  FreeLibrary (hmodule);

  return ret;
}

static Suite *
d3d12device_suite (void)
{
  Suite *s = suite_create ("d3d12device");
  TCase *tc_basic = tcase_create ("general");

  suite_add_tcase (s, tc_basic);

  if (!check_d3d12_available ())
    return s;

  tcase_add_test (tc_basic, test_device_equal);
  if (check_remove_device_supported ())
    tcase_add_test (tc_basic, test_device_removed);

  return s;
}

GST_CHECK_MAIN (d3d12device);