winrt-capture.cpp 21 KB

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