1
0

winrt-capture.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. #include "winrt-capture.h"
  2. extern "C" {
  3. HRESULT __stdcall CreateDirect3D11DeviceFromDXGIDevice(
  4. ::IDXGIDevice *dxgiDevice, ::IInspectable **graphicsDevice);
  5. HRESULT __stdcall CreateDirect3D11SurfaceFromDXGISurface(
  6. ::IDXGISurface *dgxiSurface, ::IInspectable **graphicsSurface);
  7. }
  8. struct __declspec(uuid("A9B3D012-3DF2-4EE3-B8D1-8695F457D3C1"))
  9. IDirect3DDxgiInterfaceAccess : ::IUnknown {
  10. virtual HRESULT __stdcall GetInterface(GUID const &id,
  11. void **object) = 0;
  12. };
  13. extern "C" EXPORT BOOL winrt_capture_supported()
  14. try {
  15. /* no contract for IGraphicsCaptureItemInterop, verify 10.0.18362.0 */
  16. return winrt::Windows::Foundation::Metadata::ApiInformation::
  17. IsApiContractPresent(L"Windows.Foundation.UniversalApiContract",
  18. 8);
  19. } catch (const winrt::hresult_error &err) {
  20. blog(LOG_ERROR, "winrt_capture_supported (0x%08X): %s",
  21. err.code().value, winrt::to_string(err.message()).c_str());
  22. return false;
  23. } catch (...) {
  24. blog(LOG_ERROR, "winrt_capture_supported (0x%08X)",
  25. winrt::to_hresult().value);
  26. return false;
  27. }
  28. extern "C" EXPORT BOOL winrt_capture_cursor_toggle_supported()
  29. try {
  30. return winrt::Windows::Foundation::Metadata::ApiInformation::
  31. IsPropertyPresent(
  32. L"Windows.Graphics.Capture.GraphicsCaptureSession",
  33. L"IsCursorCaptureEnabled");
  34. } catch (const winrt::hresult_error &err) {
  35. blog(LOG_ERROR, "winrt_capture_cursor_toggle_supported (0x%08X): %s",
  36. err.code().value, winrt::to_string(err.message()).c_str());
  37. return false;
  38. } catch (...) {
  39. blog(LOG_ERROR, "winrt_capture_cursor_toggle_supported (0x%08X)",
  40. winrt::to_hresult().value);
  41. return false;
  42. }
  43. template<typename T>
  44. static winrt::com_ptr<T> GetDXGIInterfaceFromObject(
  45. winrt::Windows::Foundation::IInspectable const &object)
  46. {
  47. auto access = object.as<IDirect3DDxgiInterfaceAccess>();
  48. winrt::com_ptr<T> result;
  49. winrt::check_hresult(
  50. access->GetInterface(winrt::guid_of<T>(), result.put_void()));
  51. return result;
  52. }
  53. static bool get_client_box(HWND window, uint32_t width, uint32_t height,
  54. D3D11_BOX *client_box)
  55. {
  56. RECT client_rect{}, window_rect{};
  57. POINT upper_left{};
  58. /* check iconic (minimized) twice, ABA is very unlikely */
  59. bool client_box_available =
  60. !IsIconic(window) && GetClientRect(window, &client_rect) &&
  61. !IsIconic(window) && (client_rect.right > 0) &&
  62. (client_rect.bottom > 0) &&
  63. (DwmGetWindowAttribute(window, DWMWA_EXTENDED_FRAME_BOUNDS,
  64. &window_rect,
  65. sizeof(window_rect)) == S_OK) &&
  66. ClientToScreen(window, &upper_left);
  67. if (client_box_available) {
  68. const uint32_t left =
  69. (upper_left.x > window_rect.left)
  70. ? (upper_left.x - window_rect.left)
  71. : 0;
  72. client_box->left = left;
  73. const uint32_t top = (upper_left.y > window_rect.top)
  74. ? (upper_left.y - window_rect.top)
  75. : 0;
  76. client_box->top = top;
  77. uint32_t texture_width = 1;
  78. if (width > left) {
  79. texture_width =
  80. min(width - left, (uint32_t)client_rect.right);
  81. }
  82. uint32_t texture_height = 1;
  83. if (height > top) {
  84. texture_height =
  85. min(height - top, (uint32_t)client_rect.bottom);
  86. }
  87. client_box->right = left + texture_width;
  88. client_box->bottom = top + texture_height;
  89. client_box->front = 0;
  90. client_box->back = 1;
  91. client_box_available = (client_box->right <= width) &&
  92. (client_box->bottom <= height);
  93. }
  94. return client_box_available;
  95. }
  96. static DXGI_FORMAT get_pixel_format(HWND window, HMONITOR monitor,
  97. BOOL force_sdr)
  98. {
  99. static constexpr DXGI_FORMAT sdr_format = DXGI_FORMAT_B8G8R8A8_UNORM;
  100. if (force_sdr)
  101. return sdr_format;
  102. if (window)
  103. monitor = MonitorFromWindow(window, MONITOR_DEFAULTTONEAREST);
  104. return (monitor && gs_is_monitor_hdr(monitor))
  105. ? DXGI_FORMAT_R16G16B16A16_FLOAT
  106. : sdr_format;
  107. }
  108. struct winrt_capture {
  109. HWND window;
  110. BOOL client_area;
  111. BOOL force_sdr;
  112. HMONITOR monitor;
  113. DXGI_FORMAT format;
  114. bool capture_cursor;
  115. BOOL cursor_visible;
  116. gs_texture_t *texture;
  117. bool texture_written;
  118. winrt::Windows::Graphics::Capture::GraphicsCaptureItem item{nullptr};
  119. winrt::Windows::Graphics::DirectX::Direct3D11::IDirect3DDevice device{
  120. nullptr};
  121. ComPtr<ID3D11DeviceContext> context;
  122. winrt::Windows::Graphics::Capture::Direct3D11CaptureFramePool frame_pool{
  123. nullptr};
  124. winrt::Windows::Graphics::Capture::GraphicsCaptureSession session{
  125. nullptr};
  126. winrt::Windows::Graphics::SizeInt32 last_size;
  127. winrt::Windows::Graphics::Capture::GraphicsCaptureItem::Closed_revoker
  128. closed;
  129. winrt::Windows::Graphics::Capture::Direct3D11CaptureFramePool::
  130. FrameArrived_revoker frame_arrived;
  131. uint32_t texture_width;
  132. uint32_t texture_height;
  133. D3D11_BOX client_box;
  134. BOOL active;
  135. struct winrt_capture *next;
  136. void on_closed(
  137. winrt::Windows::Graphics::Capture::GraphicsCaptureItem const &,
  138. winrt::Windows::Foundation::IInspectable const &)
  139. {
  140. active = FALSE;
  141. }
  142. void on_frame_arrived(winrt::Windows::Graphics::Capture::
  143. Direct3D11CaptureFramePool const &sender,
  144. winrt::Windows::Foundation::IInspectable const &)
  145. {
  146. const winrt::Windows::Graphics::Capture::Direct3D11CaptureFrame
  147. frame = sender.TryGetNextFrame();
  148. const winrt::Windows::Graphics::SizeInt32 frame_content_size =
  149. frame.ContentSize();
  150. winrt::com_ptr<ID3D11Texture2D> frame_surface =
  151. GetDXGIInterfaceFromObject<ID3D11Texture2D>(
  152. frame.Surface());
  153. /* need GetDesc because ContentSize is not reliable */
  154. D3D11_TEXTURE2D_DESC desc;
  155. frame_surface->GetDesc(&desc);
  156. obs_enter_graphics();
  157. if (desc.Format ==
  158. get_pixel_format(window, monitor, force_sdr)) {
  159. if (!client_area ||
  160. get_client_box(window, desc.Width, desc.Height,
  161. &client_box)) {
  162. if (client_area) {
  163. texture_width = client_box.right -
  164. client_box.left;
  165. texture_height = client_box.bottom -
  166. client_box.top;
  167. } else {
  168. texture_width = desc.Width;
  169. texture_height = desc.Height;
  170. }
  171. if (texture) {
  172. if (texture_width !=
  173. gs_texture_get_width(
  174. texture) ||
  175. texture_height !=
  176. gs_texture_get_height(
  177. texture)) {
  178. gs_texture_destroy(texture);
  179. texture = nullptr;
  180. }
  181. }
  182. if (!texture) {
  183. const gs_color_format color_format =
  184. desc.Format == DXGI_FORMAT_R16G16B16A16_FLOAT
  185. ? GS_RGBA16F
  186. : GS_BGRA;
  187. texture = gs_texture_create(
  188. texture_width, texture_height,
  189. color_format, 1, NULL, 0);
  190. }
  191. if (client_area) {
  192. context->CopySubresourceRegion(
  193. (ID3D11Texture2D *)
  194. gs_texture_get_obj(
  195. texture),
  196. 0, 0, 0, 0, frame_surface.get(),
  197. 0, &client_box);
  198. } else {
  199. /* if they gave an SRV, we could avoid this copy */
  200. context->CopyResource(
  201. (ID3D11Texture2D *)
  202. gs_texture_get_obj(
  203. texture),
  204. frame_surface.get());
  205. }
  206. texture_written = true;
  207. }
  208. if (frame_content_size.Width != last_size.Width ||
  209. frame_content_size.Height != last_size.Height) {
  210. format = desc.Format;
  211. frame_pool.Recreate(
  212. device,
  213. static_cast<
  214. winrt::Windows::Graphics::DirectX::
  215. DirectXPixelFormat>(
  216. format),
  217. 2, frame_content_size);
  218. last_size = frame_content_size;
  219. }
  220. } else {
  221. active = FALSE;
  222. }
  223. obs_leave_graphics();
  224. }
  225. };
  226. static struct winrt_capture *capture_list;
  227. static void winrt_capture_device_loss_release(void *data)
  228. {
  229. winrt_capture *capture = static_cast<winrt_capture *>(data);
  230. capture->active = FALSE;
  231. capture->frame_arrived.revoke();
  232. try {
  233. capture->frame_pool.Close();
  234. } catch (winrt::hresult_error &err) {
  235. blog(LOG_ERROR,
  236. "Direct3D11CaptureFramePool::Close (0x%08X): %s",
  237. err.code().value, winrt::to_string(err.message()).c_str());
  238. } catch (...) {
  239. blog(LOG_ERROR, "Direct3D11CaptureFramePool::Close (0x%08X)",
  240. winrt::to_hresult().value);
  241. }
  242. try {
  243. capture->session.Close();
  244. } catch (winrt::hresult_error &err) {
  245. blog(LOG_ERROR, "GraphicsCaptureSession::Close (0x%08X): %s",
  246. err.code().value, winrt::to_string(err.message()).c_str());
  247. } catch (...) {
  248. blog(LOG_ERROR, "GraphicsCaptureSession::Close (0x%08X)",
  249. winrt::to_hresult().value);
  250. }
  251. capture->session = nullptr;
  252. capture->frame_pool = nullptr;
  253. capture->context = nullptr;
  254. capture->device = nullptr;
  255. capture->item = nullptr;
  256. }
  257. static bool winrt_capture_border_toggle_supported()
  258. try {
  259. return winrt::Windows::Foundation::Metadata::ApiInformation::
  260. IsPropertyPresent(
  261. L"Windows.Graphics.Capture.GraphicsCaptureSession",
  262. L"IsBorderRequired");
  263. } catch (const winrt::hresult_error &err) {
  264. blog(LOG_ERROR, "winrt_capture_border_toggle_supported (0x%08X): %s",
  265. err.code().value, winrt::to_string(err.message()).c_str());
  266. return false;
  267. } catch (...) {
  268. blog(LOG_ERROR, "winrt_capture_border_toggle_supported (0x%08X)",
  269. winrt::to_hresult().value);
  270. return false;
  271. }
  272. static winrt::Windows::Graphics::Capture::GraphicsCaptureItem
  273. winrt_capture_create_item(IGraphicsCaptureItemInterop *const interop_factory,
  274. HWND window, HMONITOR monitor)
  275. {
  276. winrt::Windows::Graphics::Capture::GraphicsCaptureItem item = {nullptr};
  277. if (window) {
  278. try {
  279. const HRESULT hr = interop_factory->CreateForWindow(
  280. window,
  281. winrt::guid_of<ABI::Windows::Graphics::Capture::
  282. IGraphicsCaptureItem>(),
  283. reinterpret_cast<void **>(
  284. winrt::put_abi(item)));
  285. if (FAILED(hr))
  286. blog(LOG_ERROR, "CreateForWindow (0x%08X)", hr);
  287. } catch (winrt::hresult_error &err) {
  288. blog(LOG_ERROR, "CreateForWindow (0x%08X): %s",
  289. err.code().value,
  290. winrt::to_string(err.message()).c_str());
  291. } catch (...) {
  292. blog(LOG_ERROR, "CreateForWindow (0x%08X)",
  293. winrt::to_hresult().value);
  294. }
  295. } else {
  296. assert(monitor);
  297. try {
  298. const HRESULT hr = interop_factory->CreateForMonitor(
  299. monitor,
  300. winrt::guid_of<ABI::Windows::Graphics::Capture::
  301. IGraphicsCaptureItem>(),
  302. reinterpret_cast<void **>(
  303. winrt::put_abi(item)));
  304. if (FAILED(hr))
  305. blog(LOG_ERROR, "CreateForMonitor (0x%08X)",
  306. hr);
  307. } catch (winrt::hresult_error &err) {
  308. blog(LOG_ERROR, "CreateForMonitor (0x%08X): %s",
  309. err.code().value,
  310. winrt::to_string(err.message()).c_str());
  311. } catch (...) {
  312. blog(LOG_ERROR, "CreateForMonitor (0x%08X)",
  313. winrt::to_hresult().value);
  314. }
  315. }
  316. return item;
  317. }
  318. static void winrt_capture_device_loss_rebuild(void *device_void, void *data)
  319. {
  320. winrt_capture *capture = static_cast<winrt_capture *>(data);
  321. auto activation_factory = winrt::get_activation_factory<
  322. winrt::Windows::Graphics::Capture::GraphicsCaptureItem>();
  323. auto interop_factory =
  324. activation_factory.as<IGraphicsCaptureItemInterop>();
  325. winrt::Windows::Graphics::Capture::GraphicsCaptureItem item =
  326. winrt_capture_create_item(interop_factory.get(),
  327. capture->window, capture->monitor);
  328. if (!item)
  329. return;
  330. ID3D11Device *const d3d_device = (ID3D11Device *)device_void;
  331. ComPtr<IDXGIDevice> dxgi_device;
  332. if (FAILED(d3d_device->QueryInterface(&dxgi_device)))
  333. blog(LOG_ERROR, "Failed to get DXGI device");
  334. winrt::com_ptr<IInspectable> inspectable;
  335. if (FAILED(CreateDirect3D11DeviceFromDXGIDevice(dxgi_device.Get(),
  336. inspectable.put())))
  337. blog(LOG_ERROR, "Failed to get WinRT device");
  338. const winrt::Windows::Graphics::DirectX::Direct3D11::IDirect3DDevice
  339. device = inspectable.as<winrt::Windows::Graphics::DirectX::
  340. Direct3D11::IDirect3DDevice>();
  341. const winrt::Windows::Graphics::Capture::Direct3D11CaptureFramePool
  342. frame_pool = winrt::Windows::Graphics::Capture::
  343. Direct3D11CaptureFramePool::Create(
  344. device,
  345. static_cast<winrt::Windows::Graphics::DirectX::
  346. DirectXPixelFormat>(
  347. capture->format),
  348. 2, capture->last_size);
  349. const winrt::Windows::Graphics::Capture::GraphicsCaptureSession session =
  350. frame_pool.CreateCaptureSession(item);
  351. if (winrt_capture_border_toggle_supported()) {
  352. winrt::Windows::Graphics::Capture::GraphicsCaptureAccess::
  353. RequestAccessAsync(
  354. winrt::Windows::Graphics::Capture::
  355. GraphicsCaptureAccessKind::Borderless)
  356. .get();
  357. session.IsBorderRequired(false);
  358. }
  359. if (winrt_capture_cursor_toggle_supported())
  360. session.IsCursorCaptureEnabled(capture->capture_cursor &&
  361. capture->cursor_visible);
  362. capture->item = item;
  363. capture->device = device;
  364. d3d_device->GetImmediateContext(&capture->context);
  365. capture->frame_pool = frame_pool;
  366. capture->session = session;
  367. capture->frame_arrived = frame_pool.FrameArrived(
  368. winrt::auto_revoke,
  369. {capture, &winrt_capture::on_frame_arrived});
  370. try {
  371. session.StartCapture();
  372. capture->active = TRUE;
  373. } catch (winrt::hresult_error &err) {
  374. blog(LOG_ERROR, "StartCapture (0x%08X): %s", err.code().value,
  375. winrt::to_string(err.message()).c_str());
  376. } catch (...) {
  377. blog(LOG_ERROR, "StartCapture (0x%08X)",
  378. winrt::to_hresult().value);
  379. }
  380. }
  381. static struct winrt_capture *
  382. winrt_capture_init_internal(BOOL cursor, HWND window, BOOL client_area,
  383. BOOL force_sdr, HMONITOR monitor)
  384. try {
  385. ID3D11Device *const d3d_device = (ID3D11Device *)gs_get_device_obj();
  386. ComPtr<IDXGIDevice> dxgi_device;
  387. HRESULT hr = d3d_device->QueryInterface(&dxgi_device);
  388. if (FAILED(hr)) {
  389. blog(LOG_ERROR, "Failed to get DXGI device");
  390. return nullptr;
  391. }
  392. winrt::com_ptr<IInspectable> inspectable;
  393. hr = CreateDirect3D11DeviceFromDXGIDevice(dxgi_device.Get(),
  394. inspectable.put());
  395. if (FAILED(hr)) {
  396. blog(LOG_ERROR, "Failed to get WinRT device");
  397. return nullptr;
  398. }
  399. auto activation_factory = winrt::get_activation_factory<
  400. winrt::Windows::Graphics::Capture::GraphicsCaptureItem>();
  401. auto interop_factory =
  402. activation_factory.as<IGraphicsCaptureItemInterop>();
  403. winrt::Windows::Graphics::Capture::GraphicsCaptureItem item =
  404. winrt_capture_create_item(interop_factory.get(), window,
  405. monitor);
  406. if (!item)
  407. return nullptr;
  408. const winrt::Windows::Graphics::DirectX::Direct3D11::IDirect3DDevice
  409. device = inspectable.as<winrt::Windows::Graphics::DirectX::
  410. Direct3D11::IDirect3DDevice>();
  411. const winrt::Windows::Graphics::SizeInt32 size = item.Size();
  412. const DXGI_FORMAT format = get_pixel_format(window, monitor, force_sdr);
  413. const winrt::Windows::Graphics::Capture::Direct3D11CaptureFramePool
  414. frame_pool = winrt::Windows::Graphics::Capture::
  415. Direct3D11CaptureFramePool::Create(
  416. device,
  417. static_cast<winrt::Windows::Graphics::DirectX::
  418. DirectXPixelFormat>(format),
  419. 2, size);
  420. const winrt::Windows::Graphics::Capture::GraphicsCaptureSession session =
  421. frame_pool.CreateCaptureSession(item);
  422. if (winrt_capture_border_toggle_supported()) {
  423. winrt::Windows::Graphics::Capture::GraphicsCaptureAccess::
  424. RequestAccessAsync(
  425. winrt::Windows::Graphics::Capture::
  426. GraphicsCaptureAccessKind::Borderless)
  427. .get();
  428. session.IsBorderRequired(false);
  429. }
  430. /* disable cursor capture if possible since ours performs better */
  431. const BOOL cursor_toggle_supported =
  432. winrt_capture_cursor_toggle_supported();
  433. if (cursor_toggle_supported)
  434. session.IsCursorCaptureEnabled(cursor);
  435. struct winrt_capture *capture = new winrt_capture{};
  436. capture->window = window;
  437. capture->client_area = client_area;
  438. capture->force_sdr = force_sdr;
  439. capture->monitor = monitor;
  440. capture->format = format;
  441. capture->capture_cursor = cursor && cursor_toggle_supported;
  442. capture->cursor_visible = cursor;
  443. capture->item = item;
  444. capture->device = device;
  445. d3d_device->GetImmediateContext(&capture->context);
  446. capture->frame_pool = frame_pool;
  447. capture->session = session;
  448. capture->last_size = size;
  449. capture->closed = item.Closed(winrt::auto_revoke,
  450. {capture, &winrt_capture::on_closed});
  451. capture->frame_arrived = frame_pool.FrameArrived(
  452. winrt::auto_revoke,
  453. {capture, &winrt_capture::on_frame_arrived});
  454. capture->next = capture_list;
  455. capture_list = capture;
  456. session.StartCapture();
  457. capture->active = TRUE;
  458. gs_device_loss callbacks;
  459. callbacks.device_loss_release = winrt_capture_device_loss_release;
  460. callbacks.device_loss_rebuild = winrt_capture_device_loss_rebuild;
  461. callbacks.data = capture;
  462. gs_register_loss_callbacks(&callbacks);
  463. return capture;
  464. } catch (const winrt::hresult_error &err) {
  465. blog(LOG_ERROR, "winrt_capture_init (0x%08X): %s", err.code().value,
  466. winrt::to_string(err.message()).c_str());
  467. return nullptr;
  468. } catch (...) {
  469. blog(LOG_ERROR, "winrt_capture_init (0x%08X)",
  470. winrt::to_hresult().value);
  471. return nullptr;
  472. }
  473. extern "C" EXPORT struct winrt_capture *
  474. winrt_capture_init_window(BOOL cursor, HWND window, BOOL client_area,
  475. BOOL force_sdr)
  476. {
  477. return winrt_capture_init_internal(cursor, window, client_area,
  478. force_sdr, NULL);
  479. }
  480. extern "C" EXPORT struct winrt_capture *
  481. winrt_capture_init_monitor(BOOL cursor, HMONITOR monitor, BOOL force_sdr)
  482. {
  483. return winrt_capture_init_internal(cursor, NULL, false, force_sdr,
  484. monitor);
  485. }
  486. extern "C" EXPORT void winrt_capture_free(struct winrt_capture *capture)
  487. {
  488. if (capture) {
  489. struct winrt_capture *current = capture_list;
  490. if (current == capture) {
  491. capture_list = capture->next;
  492. } else {
  493. struct winrt_capture *previous;
  494. do {
  495. previous = current;
  496. current = current->next;
  497. } while (current != capture);
  498. previous->next = current->next;
  499. }
  500. obs_enter_graphics();
  501. gs_unregister_loss_callbacks(capture);
  502. gs_texture_destroy(capture->texture);
  503. obs_leave_graphics();
  504. capture->frame_arrived.revoke();
  505. capture->closed.revoke();
  506. try {
  507. if (capture->frame_pool)
  508. capture->frame_pool.Close();
  509. } catch (winrt::hresult_error &err) {
  510. blog(LOG_ERROR,
  511. "Direct3D11CaptureFramePool::Close (0x%08X): %s",
  512. err.code().value,
  513. winrt::to_string(err.message()).c_str());
  514. } catch (...) {
  515. blog(LOG_ERROR,
  516. "Direct3D11CaptureFramePool::Close (0x%08X)",
  517. winrt::to_hresult().value);
  518. }
  519. try {
  520. if (capture->session)
  521. capture->session.Close();
  522. } catch (winrt::hresult_error &err) {
  523. blog(LOG_ERROR,
  524. "GraphicsCaptureSession::Close (0x%08X): %s",
  525. err.code().value,
  526. winrt::to_string(err.message()).c_str());
  527. } catch (...) {
  528. blog(LOG_ERROR,
  529. "GraphicsCaptureSession::Close (0x%08X)",
  530. winrt::to_hresult().value);
  531. }
  532. delete capture;
  533. }
  534. }
  535. extern "C" EXPORT BOOL winrt_capture_active(const struct winrt_capture *capture)
  536. {
  537. return capture->active;
  538. }
  539. extern "C" EXPORT BOOL winrt_capture_show_cursor(struct winrt_capture *capture,
  540. BOOL visible)
  541. {
  542. BOOL success = FALSE;
  543. try {
  544. if (capture->capture_cursor) {
  545. if (capture->cursor_visible != visible) {
  546. capture->session.IsCursorCaptureEnabled(
  547. visible);
  548. capture->cursor_visible = visible;
  549. }
  550. }
  551. success = TRUE;
  552. } catch (winrt::hresult_error &err) {
  553. blog(LOG_ERROR,
  554. "GraphicsCaptureSession::IsCursorCaptureEnabled (0x%08X): %s",
  555. err.code().value, winrt::to_string(err.message()).c_str());
  556. } catch (...) {
  557. blog(LOG_ERROR,
  558. "GraphicsCaptureSession::IsCursorCaptureEnabled (0x%08X)",
  559. winrt::to_hresult().value);
  560. }
  561. return success;
  562. }
  563. extern "C" EXPORT enum gs_color_space
  564. winrt_capture_get_color_space(const struct winrt_capture *capture)
  565. {
  566. return (capture->format == DXGI_FORMAT_R16G16B16A16_FLOAT)
  567. ? GS_CS_709_EXTENDED
  568. : GS_CS_SRGB;
  569. }
  570. extern "C" EXPORT void winrt_capture_render(struct winrt_capture *capture)
  571. {
  572. if (capture->texture_written) {
  573. const char *tech_name = "Draw";
  574. float multiplier = 1.f;
  575. const gs_color_space current_space = gs_get_color_space();
  576. if (capture->format == DXGI_FORMAT_R16G16B16A16_FLOAT) {
  577. switch (current_space) {
  578. case GS_CS_SRGB:
  579. case GS_CS_SRGB_16F:
  580. tech_name = "DrawMultiplyTonemap";
  581. multiplier =
  582. 80.f / obs_get_video_sdr_white_level();
  583. break;
  584. case GS_CS_709_EXTENDED:
  585. tech_name = "DrawMultiply";
  586. multiplier =
  587. 80.f / obs_get_video_sdr_white_level();
  588. }
  589. } else if (current_space == GS_CS_709_SCRGB) {
  590. tech_name = "DrawMultiply";
  591. multiplier = obs_get_video_sdr_white_level() / 80.f;
  592. }
  593. gs_effect_t *const effect =
  594. obs_get_base_effect(OBS_EFFECT_DEFAULT);
  595. gs_technique_t *tech =
  596. gs_effect_get_technique(effect, tech_name);
  597. const bool previous = gs_framebuffer_srgb_enabled();
  598. gs_enable_framebuffer_srgb(true);
  599. gs_blend_state_push();
  600. gs_blend_function(GS_BLEND_ONE, GS_BLEND_INVSRCALPHA);
  601. gs_texture_t *const texture = capture->texture;
  602. gs_effect_set_texture_srgb(
  603. gs_effect_get_param_by_name(effect, "image"), texture);
  604. gs_effect_set_float(gs_effect_get_param_by_name(effect,
  605. "multiplier"),
  606. multiplier);
  607. const size_t passes = gs_technique_begin(tech);
  608. for (size_t i = 0; i < passes; i++) {
  609. if (gs_technique_begin_pass(tech, i)) {
  610. gs_draw_sprite(texture, 0, 0, 0);
  611. gs_technique_end_pass(tech);
  612. }
  613. }
  614. gs_technique_end(tech);
  615. gs_blend_state_pop();
  616. gs_enable_framebuffer_srgb(previous);
  617. }
  618. }
  619. extern "C" EXPORT uint32_t
  620. winrt_capture_width(const struct winrt_capture *capture)
  621. {
  622. return capture ? capture->texture_width : 0;
  623. }
  624. extern "C" EXPORT uint32_t
  625. winrt_capture_height(const struct winrt_capture *capture)
  626. {
  627. return capture ? capture->texture_height : 0;
  628. }
  629. extern "C" EXPORT void winrt_capture_thread_start()
  630. {
  631. struct winrt_capture *capture = capture_list;
  632. void *const device = gs_get_device_obj();
  633. while (capture) {
  634. winrt_capture_device_loss_rebuild(device, capture);
  635. capture = capture->next;
  636. }
  637. }
  638. extern "C" EXPORT void winrt_capture_thread_stop()
  639. {
  640. struct winrt_capture *capture = capture_list;
  641. while (capture) {
  642. winrt_capture_device_loss_release(capture);
  643. capture = capture->next;
  644. }
  645. }