Sunshine latest
Self-hosted game stream host for Moonlight.
display.h
Go to the documentation of this file.
1
5#pragma once
6
7// platform includes
8#include <d3d11.h>
9#include <d3d11_4.h>
10#include <d3dcommon.h>
11#include <dwmapi.h>
12#include <dxgi.h>
13#include <dxgi1_6.h>
14#include <Unknwn.h>
15#include <winrt/Windows.Graphics.Capture.h>
16
17// local includes
18#include "src/platform/common.h"
19#include "src/utility.h"
20#include "src/video.h"
21
22namespace platf::dxgi {
23 extern const char *format_str[];
24
25 // Add D3D11_CREATE_DEVICE_DEBUG here to enable the D3D11 debug runtime.
26 // You should have a debugger like WinDbg attached to receive debug messages.
27 auto constexpr D3D11_CREATE_DEVICE_FLAGS = 0;
28
29 template<class T>
30 void Release(T *dxgi) {
31 dxgi->Release();
32 }
33
64
65 namespace video {
72 } // namespace video
73
74 class hwdevice_t;
75
76 struct cursor_t {
77 std::vector<std::uint8_t> img_data;
78
79 DXGI_OUTDUPL_POINTER_SHAPE_INFO shape_info;
80 int x, y;
81 bool visible;
82 };
83
85 public:
87 cursor_view {0, 0, 0, 0, 0.0f, 1.0f} {};
88
89 void set_pos(LONG topleft_x, LONG topleft_y, LONG display_width, LONG display_height, DXGI_MODE_ROTATION display_rotation, bool visible) {
90 this->topleft_x = topleft_x;
91 this->topleft_y = topleft_y;
92 this->display_width = display_width;
93 this->display_height = display_height;
94 this->display_rotation = display_rotation;
95 this->visible = visible;
96 update_viewport();
97 }
98
99 void set_texture(LONG texture_width, LONG texture_height, texture2d_t &&texture) {
100 this->texture = std::move(texture);
101 this->texture_width = texture_width;
102 this->texture_height = texture_height;
103 update_viewport();
104 }
105
106 void update_viewport() {
107 switch (display_rotation) {
108 case DXGI_MODE_ROTATION_UNSPECIFIED:
109 case DXGI_MODE_ROTATION_IDENTITY:
110 cursor_view.TopLeftX = topleft_x;
111 cursor_view.TopLeftY = topleft_y;
112 cursor_view.Width = texture_width;
113 cursor_view.Height = texture_height;
114 break;
115
116 case DXGI_MODE_ROTATION_ROTATE90:
117 cursor_view.TopLeftX = topleft_y;
118 cursor_view.TopLeftY = display_width - texture_width - topleft_x;
119 cursor_view.Width = texture_height;
120 cursor_view.Height = texture_width;
121 break;
122
123 case DXGI_MODE_ROTATION_ROTATE180:
124 cursor_view.TopLeftX = display_width - texture_width - topleft_x;
125 cursor_view.TopLeftY = display_height - texture_height - topleft_y;
126 cursor_view.Width = texture_width;
127 cursor_view.Height = texture_height;
128 break;
129
130 case DXGI_MODE_ROTATION_ROTATE270:
131 cursor_view.TopLeftX = display_height - texture_height - topleft_y;
132 cursor_view.TopLeftY = topleft_x;
133 cursor_view.Width = texture_height;
134 cursor_view.Height = texture_width;
135 break;
136 }
137 }
138
139 texture2d_t texture;
140 LONG texture_width;
141 LONG texture_height;
142
143 LONG topleft_x;
144 LONG topleft_y;
145
146 LONG display_width;
147 LONG display_height;
148 DXGI_MODE_ROTATION display_rotation;
149
150 shader_res_t input_res;
151
152 D3D11_VIEWPORT cursor_view;
153
154 bool visible;
155 };
156
157 class display_base_t: public display_t {
158 public:
159 int init(const ::video::config_t &config, const std::string &display_name);
160
161 capture_e capture(const push_captured_image_cb_t &push_captured_image_cb, const pull_free_image_cb_t &pull_free_image_cb, bool *cursor) override;
162
163 factory1_t factory;
164 adapter_t adapter;
165 output_t output;
166 device_t device;
167 device_ctx_t device_ctx;
168 DXGI_RATIONAL display_refresh_rate;
169 int display_refresh_rate_rounded;
170
171 DXGI_MODE_ROTATION display_rotation = DXGI_MODE_ROTATION_UNSPECIFIED;
172 int width_before_rotation;
173 int height_before_rotation;
174
175 int client_frame_rate;
176
177 DXGI_FORMAT capture_format;
178 D3D_FEATURE_LEVEL feature_level;
179
180 std::unique_ptr<high_precision_timer> timer = create_high_precision_timer();
181
190
191 typedef UINT D3DKMT_HANDLE;
192
194 LUID AdapterLuid;
195 D3DKMT_HANDLE hAdapter;
197
198 typedef struct _D3DKMT_WDDM_2_7_CAPS {
199 union {
200 struct
201 {
202 UINT HwSchSupported : 1;
203 UINT HwSchEnabled : 1;
204 UINT HwSchEnabledByDefault : 1;
205 UINT IndependentVidPnVSyncControl : 1;
206 UINT Reserved : 28;
207 };
208
209 UINT Value;
210 };
212
214 D3DKMT_HANDLE hAdapter;
215 UINT Type;
216 VOID *pPrivateDriverData;
217 UINT PrivateDriverDataSize;
219
220 const UINT KMTQAITYPE_WDDM_2_7_CAPS = 70;
221
222 typedef struct _D3DKMT_CLOSEADAPTER {
223 D3DKMT_HANDLE hAdapter;
225
226 typedef NTSTATUS(WINAPI *PD3DKMTSetProcessSchedulingPriorityClass)(HANDLE, D3DKMT_SCHEDULINGPRIORITYCLASS);
227 typedef NTSTATUS(WINAPI *PD3DKMTOpenAdapterFromLuid)(D3DKMT_OPENADAPTERFROMLUID *);
228 typedef NTSTATUS(WINAPI *PD3DKMTQueryAdapterInfo)(D3DKMT_QUERYADAPTERINFO *);
229 typedef NTSTATUS(WINAPI *PD3DKMTCloseAdapter)(D3DKMT_CLOSEADAPTER *);
230
231 virtual bool is_hdr() override;
232 virtual bool get_hdr_metadata(SS_HDR_METADATA &metadata) override;
233
234 const char *dxgi_format_to_string(DXGI_FORMAT format);
235 const char *colorspace_to_string(DXGI_COLOR_SPACE_TYPE type);
236 virtual std::vector<DXGI_FORMAT> get_supported_capture_formats() = 0;
237
238 protected:
239 int get_pixel_pitch() {
240 return (capture_format == DXGI_FORMAT_R16G16B16A16_FLOAT) ? 8 : 4;
241 }
242
243 virtual capture_e snapshot(const pull_free_image_cb_t &pull_free_image_cb, std::shared_ptr<platf::img_t> &img_out, std::chrono::milliseconds timeout, bool cursor_visible) = 0;
244 virtual capture_e release_snapshot() = 0;
245 virtual int complete_img(img_t *img, bool dummy) = 0;
246 };
247
252 public:
253 std::shared_ptr<img_t> alloc_img() override;
254 int dummy_img(img_t *img) override;
255 int complete_img(img_t *img, bool dummy) override;
256 std::vector<DXGI_FORMAT> get_supported_capture_formats() override;
257
258 std::unique_ptr<avcodec_encode_device_t> make_avcodec_encode_device(pix_fmt_e pix_fmt) override;
259
260 D3D11_MAPPED_SUBRESOURCE img_info;
261 texture2d_t texture;
262 };
263
267 class display_vram_t: public display_base_t, public std::enable_shared_from_this<display_vram_t> {
268 public:
269 std::shared_ptr<img_t> alloc_img() override;
270 int dummy_img(img_t *img_base) override;
271 int complete_img(img_t *img_base, bool dummy) override;
272 std::vector<DXGI_FORMAT> get_supported_capture_formats() override;
273
274 bool is_codec_supported(std::string_view name, const ::video::config_t &config) override;
275
276 std::unique_ptr<avcodec_encode_device_t> make_avcodec_encode_device(pix_fmt_e pix_fmt) override;
277
278 std::unique_ptr<nvenc_encode_device_t> make_nvenc_encode_device(pix_fmt_e pix_fmt) override;
279
280 std::atomic<uint32_t> next_image_id;
281 };
282
287 public:
288 dup_t dup;
289 bool has_frame {};
290 std::chrono::steady_clock::time_point last_protected_content_warning_time {};
291
292 int init(display_base_t *display, const ::video::config_t &config);
293 capture_e next_frame(DXGI_OUTDUPL_FRAME_INFO &frame_info, std::chrono::milliseconds timeout, resource_t::pointer *res_p);
294 capture_e reset(dup_t::pointer dup_p = dup_t::pointer());
295 capture_e release_frame();
296
298 };
299
304 public:
305 int init(const ::video::config_t &config, const std::string &display_name);
306 capture_e snapshot(const pull_free_image_cb_t &pull_free_image_cb, std::shared_ptr<platf::img_t> &img_out, std::chrono::milliseconds timeout, bool cursor_visible) override;
307 capture_e release_snapshot() override;
308
309 duplication_t dup;
310 cursor_t cursor;
311 };
312
317 public:
318 int init(const ::video::config_t &config, const std::string &display_name);
319 capture_e snapshot(const pull_free_image_cb_t &pull_free_image_cb, std::shared_ptr<platf::img_t> &img_out, std::chrono::milliseconds timeout, bool cursor_visible) override;
320 capture_e release_snapshot() override;
321
322 duplication_t dup;
323 sampler_state_t sampler_linear;
324
325 blend_t blend_alpha;
326 blend_t blend_invert;
327 blend_t blend_disable;
328
329 ps_t cursor_ps;
330 vs_t cursor_vs;
331
332 gpu_cursor_t cursor_alpha;
333 gpu_cursor_t cursor_xor;
334
335 texture2d_t old_surface_delayed_destruction;
336 std::chrono::steady_clock::time_point old_surface_timestamp;
337 std::variant<std::monostate, texture2d_t, std::shared_ptr<platf::img_t>> last_frame_variant;
338 };
339
344 winrt::Windows::Graphics::DirectX::Direct3D11::IDirect3DDevice uwp_device {nullptr};
345 winrt::Windows::Graphics::Capture::GraphicsCaptureItem item {nullptr};
346 winrt::Windows::Graphics::Capture::Direct3D11CaptureFramePool frame_pool {nullptr};
347 winrt::Windows::Graphics::Capture::GraphicsCaptureSession capture_session {nullptr};
348 winrt::Windows::Graphics::Capture::Direct3D11CaptureFrame produced_frame {nullptr}, consumed_frame {nullptr};
349 SRWLOCK frame_lock = SRWLOCK_INIT;
350 CONDITION_VARIABLE frame_present_cv;
351
352 void on_frame_arrived(winrt::Windows::Graphics::Capture::Direct3D11CaptureFramePool const &sender, winrt::Windows::Foundation::IInspectable const &);
353
354 public:
357
358 int init(display_base_t *display, const ::video::config_t &config);
359 capture_e next_frame(std::chrono::milliseconds timeout, ID3D11Texture2D **out, uint64_t &out_time);
360 capture_e release_frame();
361 int set_cursor_visible(bool);
362 };
363
368 wgc_capture_t dup;
369
370 public:
371 int init(const ::video::config_t &config, const std::string &display_name);
372 capture_e snapshot(const pull_free_image_cb_t &pull_free_image_cb, std::shared_ptr<platf::img_t> &img_out, std::chrono::milliseconds timeout, bool cursor_visible) override;
373 capture_e release_snapshot() override;
374 };
375
380 wgc_capture_t dup;
381
382 public:
383 int init(const ::video::config_t &config, const std::string &display_name);
384 capture_e snapshot(const pull_free_image_cb_t &pull_free_image_cb, std::shared_ptr<platf::img_t> &img_out, std::chrono::milliseconds timeout, bool cursor_visible) override;
385 capture_e release_snapshot() override;
386 };
387} // namespace platf::dxgi
Definition common.h:445
std::function< bool(std::shared_ptr< img_t > &&img, bool frame_captured)> push_captured_image_cb_t
Callback for when a new image is ready. When display has a new image ready or a timeout occurs,...
Definition common.h:454
std::function< bool(std::shared_ptr< img_t > &img_out)> pull_free_image_cb_t
Get free image from pool. Calls must be synchronized. Blocks until there is free image in the pool or...
Definition common.h:463
Definition display.h:157
capture_e capture(const push_captured_image_cb_t &push_captured_image_cb, const pull_free_image_cb_t &pull_free_image_cb, bool *cursor) override
Capture a frame.
Definition display_base.cpp:197
_D3DKMT_SCHEDULINGPRIORITYCLASS
Definition display.h:182
@ D3DKMT_SCHEDULINGPRIORITYCLASS_NORMAL
Normal priority class.
Definition display.h:185
@ D3DKMT_SCHEDULINGPRIORITYCLASS_BELOW_NORMAL
Below normal priority class.
Definition display.h:184
@ D3DKMT_SCHEDULINGPRIORITYCLASS_HIGH
High priority class.
Definition display.h:187
@ D3DKMT_SCHEDULINGPRIORITYCLASS_IDLE
Idle priority class.
Definition display.h:183
@ D3DKMT_SCHEDULINGPRIORITYCLASS_REALTIME
Realtime priority class.
Definition display.h:188
@ D3DKMT_SCHEDULINGPRIORITYCLASS_ABOVE_NORMAL
Above normal priority class.
Definition display.h:186
Definition display.h:303
Definition display.h:316
Definition display.h:251
Definition display.h:267
bool is_codec_supported(std::string_view name, const ::video::config_t &config) override
Check that a given codec is supported by the display device.
Definition display_vram.cpp:1838
Definition display.h:367
capture_e snapshot(const pull_free_image_cb_t &pull_free_image_cb, std::shared_ptr< platf::img_t > &img_out, std::chrono::milliseconds timeout, bool cursor_visible) override
Get the next frame from the Windows.Graphics.Capture API and copy it into a new snapshot texture.
Definition display_wgc.cpp:247
Definition display.h:379
capture_e snapshot(const pull_free_image_cb_t &pull_free_image_cb, std::shared_ptr< platf::img_t > &img_out, std::chrono::milliseconds timeout, bool cursor_visible) override
Definition display_vram.cpp:1643
Definition display.h:286
Definition display.h:84
Definition display.h:343
capture_e next_frame(std::chrono::milliseconds timeout, ID3D11Texture2D **out, uint64_t &out_time)
Get the next frame from the producer thread. If not available, the capture thread blocks until one is...
Definition display_wgc.cpp:181
Definition utility.h:530
Declarations for common platform specific utilities.
std::shared_ptr< display_t > display(mem_type_e hwdevice_type, const std::string &display_name, const video::config_t &config)
Get the display_t instance for the given hwdevice_type. If display_name is empty, use the first monit...
Definition misc.cpp:864
pix_fmt_e
Definition common.h:220
std::unique_ptr< high_precision_timer > create_high_precision_timer()
Create platform-specific timer capable of high-precision sleep.
Definition misc.cpp:971
capture_e
Definition common.h:437
@ timeout
Timeout.
Definition display.h:76
Definition display_ram.cpp:15
Declarations for utility functions.
Declarations for video.