winrt-capture.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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): %ls",
  21. err.code().value, 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): %ls",
  36. err.code().value, 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): %ls",
  237. err.code().value, 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): %ls",
  246. err.code().value, 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): %ls",
  265. err.code().value, 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): %ls",
  289. err.code().value, err.message().c_str());
  290. } catch (...) {
  291. blog(LOG_ERROR, "CreateForWindow (0x%08X)",
  292. winrt::to_hresult().value);
  293. }
  294. } else {
  295. assert(monitor);
  296. try {
  297. const HRESULT hr = interop_factory->CreateForMonitor(
  298. monitor,
  299. winrt::guid_of<ABI::Windows::Graphics::Capture::
  300. IGraphicsCaptureItem>(),
  301. reinterpret_cast<void **>(
  302. winrt::put_abi(item)));
  303. if (FAILED(hr))
  304. blog(LOG_ERROR, "CreateForMonitor (0x%08X)",
  305. hr);
  306. } catch (winrt::hresult_error &err) {
  307. blog(LOG_ERROR, "CreateForMonitor (0x%08X): %ls",
  308. err.code().value, err.message().c_str());
  309. } catch (...) {
  310. blog(LOG_ERROR, "CreateForMonitor (0x%08X)",
  311. winrt::to_hresult().value);
  312. }
  313. }
  314. return item;
  315. }
  316. static void winrt_capture_device_loss_rebuild(void *device_void, void *data)
  317. {
  318. winrt_capture *capture = static_cast<winrt_capture *>(data);
  319. auto activation_factory = winrt::get_activation_factory<
  320. winrt::Windows::Graphics::Capture::GraphicsCaptureItem>();
  321. auto interop_factory =
  322. activation_factory.as<IGraphicsCaptureItemInterop>();
  323. winrt::Windows::Graphics::Capture::GraphicsCaptureItem item =
  324. winrt_capture_create_item(interop_factory.get(),
  325. capture->window, capture->monitor);
  326. if (!item)
  327. return;
  328. ID3D11Device *const d3d_device = (ID3D11Device *)device_void;
  329. ComPtr<IDXGIDevice> dxgi_device;
  330. if (FAILED(d3d_device->QueryInterface(&dxgi_device)))
  331. blog(LOG_ERROR, "Failed to get DXGI device");
  332. winrt::com_ptr<IInspectable> inspectable;
  333. if (FAILED(CreateDirect3D11DeviceFromDXGIDevice(dxgi_device.Get(),
  334. inspectable.put())))
  335. blog(LOG_ERROR, "Failed to get WinRT device");
  336. const winrt::Windows::Graphics::DirectX::Direct3D11::IDirect3DDevice
  337. device = inspectable.as<winrt::Windows::Graphics::DirectX::
  338. Direct3D11::IDirect3DDevice>();
  339. const winrt::Windows::Graphics::Capture::Direct3D11CaptureFramePool
  340. frame_pool = winrt::Windows::Graphics::Capture::
  341. Direct3D11CaptureFramePool::Create(
  342. device,
  343. static_cast<winrt::Windows::Graphics::DirectX::
  344. DirectXPixelFormat>(
  345. capture->format),
  346. 2, capture->last_size);
  347. const winrt::Windows::Graphics::Capture::GraphicsCaptureSession session =
  348. frame_pool.CreateCaptureSession(item);
  349. if (winrt_capture_border_toggle_supported()) {
  350. winrt::Windows::Graphics::Capture::GraphicsCaptureAccess::
  351. RequestAccessAsync(
  352. winrt::Windows::Graphics::Capture::
  353. GraphicsCaptureAccessKind::Borderless)
  354. .get();
  355. session.IsBorderRequired(false);
  356. }
  357. if (winrt_capture_cursor_toggle_supported())
  358. session.IsCursorCaptureEnabled(capture->capture_cursor &&
  359. capture->cursor_visible);
  360. capture->item = item;
  361. capture->device = device;
  362. d3d_device->GetImmediateContext(&capture->context);
  363. capture->frame_pool = frame_pool;
  364. capture->session = session;
  365. capture->frame_arrived = frame_pool.FrameArrived(
  366. winrt::auto_revoke,
  367. {capture, &winrt_capture::on_frame_arrived});
  368. try {
  369. session.StartCapture();
  370. capture->active = TRUE;
  371. } catch (winrt::hresult_error &err) {
  372. blog(LOG_ERROR, "StartCapture (0x%08X): %ls", err.code().value,
  373. err.message().c_str());
  374. } catch (...) {
  375. blog(LOG_ERROR, "StartCapture (0x%08X)",
  376. winrt::to_hresult().value);
  377. }
  378. }
  379. static struct winrt_capture *
  380. winrt_capture_init_internal(BOOL cursor, HWND window, BOOL client_area,
  381. BOOL force_sdr, HMONITOR monitor)
  382. try {
  383. ID3D11Device *const d3d_device = (ID3D11Device *)gs_get_device_obj();
  384. ComPtr<IDXGIDevice> dxgi_device;
  385. HRESULT hr = d3d_device->QueryInterface(&dxgi_device);
  386. if (FAILED(hr)) {
  387. blog(LOG_ERROR, "Failed to get DXGI device");
  388. return nullptr;
  389. }
  390. winrt::com_ptr<IInspectable> inspectable;
  391. hr = CreateDirect3D11DeviceFromDXGIDevice(dxgi_device.Get(),
  392. inspectable.put());
  393. if (FAILED(hr)) {
  394. blog(LOG_ERROR, "Failed to get WinRT device");
  395. return nullptr;
  396. }
  397. auto activation_factory = winrt::get_activation_factory<
  398. winrt::Windows::Graphics::Capture::GraphicsCaptureItem>();
  399. auto interop_factory =
  400. activation_factory.as<IGraphicsCaptureItemInterop>();
  401. winrt::Windows::Graphics::Capture::GraphicsCaptureItem item =
  402. winrt_capture_create_item(interop_factory.get(), window,
  403. monitor);
  404. if (!item)
  405. return nullptr;
  406. const winrt::Windows::Graphics::DirectX::Direct3D11::IDirect3DDevice
  407. device = inspectable.as<winrt::Windows::Graphics::DirectX::
  408. Direct3D11::IDirect3DDevice>();
  409. const winrt::Windows::Graphics::SizeInt32 size = item.Size();
  410. const DXGI_FORMAT format = get_pixel_format(window, monitor, force_sdr);
  411. const winrt::Windows::Graphics::Capture::Direct3D11CaptureFramePool
  412. frame_pool = winrt::Windows::Graphics::Capture::
  413. Direct3D11CaptureFramePool::Create(
  414. device,
  415. static_cast<winrt::Windows::Graphics::DirectX::
  416. DirectXPixelFormat>(format),
  417. 2, size);
  418. const winrt::Windows::Graphics::Capture::GraphicsCaptureSession session =
  419. frame_pool.CreateCaptureSession(item);
  420. if (winrt_capture_border_toggle_supported()) {
  421. winrt::Windows::Graphics::Capture::GraphicsCaptureAccess::
  422. RequestAccessAsync(
  423. winrt::Windows::Graphics::Capture::
  424. GraphicsCaptureAccessKind::Borderless)
  425. .get();
  426. session.IsBorderRequired(false);
  427. }
  428. /* disable cursor capture if possible since ours performs better */
  429. const BOOL cursor_toggle_supported =
  430. winrt_capture_cursor_toggle_supported();
  431. if (cursor_toggle_supported)
  432. session.IsCursorCaptureEnabled(cursor);
  433. struct winrt_capture *capture = new winrt_capture{};
  434. capture->window = window;
  435. capture->client_area = client_area;
  436. capture->force_sdr = force_sdr;
  437. capture->monitor = monitor;
  438. capture->format = format;
  439. capture->capture_cursor = cursor && cursor_toggle_supported;
  440. capture->cursor_visible = cursor;
  441. capture->item = item;
  442. capture->device = device;
  443. d3d_device->GetImmediateContext(&capture->context);
  444. capture->frame_pool = frame_pool;
  445. capture->session = session;
  446. capture->last_size = size;
  447. capture->closed = item.Closed(winrt::auto_revoke,
  448. {capture, &winrt_capture::on_closed});
  449. capture->frame_arrived = frame_pool.FrameArrived(
  450. winrt::auto_revoke,
  451. {capture, &winrt_capture::on_frame_arrived});
  452. capture->next = capture_list;
  453. capture_list = capture;
  454. session.StartCapture();
  455. capture->active = TRUE;
  456. gs_device_loss callbacks;
  457. callbacks.device_loss_release = winrt_capture_device_loss_release;
  458. callbacks.device_loss_rebuild = winrt_capture_device_loss_rebuild;
  459. callbacks.data = capture;
  460. gs_register_loss_callbacks(&callbacks);
  461. return capture;
  462. } catch (const winrt::hresult_error &err) {
  463. blog(LOG_ERROR, "winrt_capture_init (0x%08X): %ls", err.code().value,
  464. err.message().c_str());
  465. return nullptr;
  466. } catch (...) {
  467. blog(LOG_ERROR, "winrt_capture_init (0x%08X)",
  468. winrt::to_hresult().value);
  469. return nullptr;
  470. }
  471. extern "C" EXPORT struct winrt_capture *
  472. winrt_capture_init_window(BOOL cursor, HWND window, BOOL client_area,
  473. BOOL force_sdr)
  474. {
  475. return winrt_capture_init_internal(cursor, window, client_area,
  476. force_sdr, NULL);
  477. }
  478. extern "C" EXPORT struct winrt_capture *
  479. winrt_capture_init_monitor(BOOL cursor, HMONITOR monitor)
  480. {
  481. return winrt_capture_init_internal(cursor, NULL, false, false, monitor);
  482. }
  483. extern "C" EXPORT void winrt_capture_free(struct winrt_capture *capture)
  484. {
  485. if (capture) {
  486. struct winrt_capture *current = capture_list;
  487. if (current == capture) {
  488. capture_list = capture->next;
  489. } else {
  490. struct winrt_capture *previous;
  491. do {
  492. previous = current;
  493. current = current->next;
  494. } while (current != capture);
  495. previous->next = current->next;
  496. }
  497. obs_enter_graphics();
  498. gs_unregister_loss_callbacks(capture);
  499. gs_texture_destroy(capture->texture);
  500. obs_leave_graphics();
  501. capture->frame_arrived.revoke();
  502. capture->closed.revoke();
  503. try {
  504. if (capture->frame_pool)
  505. capture->frame_pool.Close();
  506. } catch (winrt::hresult_error &err) {
  507. blog(LOG_ERROR,
  508. "Direct3D11CaptureFramePool::Close (0x%08X): %ls",
  509. err.code().value, err.message().c_str());
  510. } catch (...) {
  511. blog(LOG_ERROR,
  512. "Direct3D11CaptureFramePool::Close (0x%08X)",
  513. winrt::to_hresult().value);
  514. }
  515. try {
  516. if (capture->session)
  517. capture->session.Close();
  518. } catch (winrt::hresult_error &err) {
  519. blog(LOG_ERROR,
  520. "GraphicsCaptureSession::Close (0x%08X): %ls",
  521. err.code().value, err.message().c_str());
  522. } catch (...) {
  523. blog(LOG_ERROR,
  524. "GraphicsCaptureSession::Close (0x%08X)",
  525. winrt::to_hresult().value);
  526. }
  527. delete capture;
  528. }
  529. }
  530. extern "C" EXPORT BOOL winrt_capture_active(const struct winrt_capture *capture)
  531. {
  532. return capture->active;
  533. }
  534. extern "C" EXPORT BOOL winrt_capture_show_cursor(struct winrt_capture *capture,
  535. BOOL visible)
  536. {
  537. BOOL success = FALSE;
  538. try {
  539. if (capture->capture_cursor) {
  540. if (capture->cursor_visible != visible) {
  541. capture->session.IsCursorCaptureEnabled(
  542. visible);
  543. capture->cursor_visible = visible;
  544. }
  545. }
  546. success = TRUE;
  547. } catch (winrt::hresult_error &err) {
  548. blog(LOG_ERROR,
  549. "GraphicsCaptureSession::IsCursorCaptureEnabled (0x%08X): %ls",
  550. err.code().value, err.message().c_str());
  551. } catch (...) {
  552. blog(LOG_ERROR,
  553. "GraphicsCaptureSession::IsCursorCaptureEnabled (0x%08X)",
  554. winrt::to_hresult().value);
  555. }
  556. return success;
  557. }
  558. extern "C" EXPORT enum gs_color_space
  559. winrt_capture_get_color_space(const struct winrt_capture *capture)
  560. {
  561. return (capture->format == DXGI_FORMAT_R16G16B16A16_FLOAT)
  562. ? GS_CS_709_EXTENDED
  563. : GS_CS_SRGB;
  564. }
  565. extern "C" EXPORT void winrt_capture_render(struct winrt_capture *capture)
  566. {
  567. if (capture->texture_written) {
  568. const char *tech_name = "Draw";
  569. float multiplier = 1.f;
  570. const gs_color_space current_space = gs_get_color_space();
  571. if (capture->format == DXGI_FORMAT_R16G16B16A16_FLOAT) {
  572. switch (current_space) {
  573. case GS_CS_SRGB:
  574. case GS_CS_SRGB_16F:
  575. tech_name = "DrawMultiplyTonemap";
  576. multiplier =
  577. 80.f / obs_get_video_sdr_white_level();
  578. break;
  579. case GS_CS_709_EXTENDED:
  580. tech_name = "DrawMultiply";
  581. multiplier =
  582. 80.f / obs_get_video_sdr_white_level();
  583. }
  584. } else if (current_space == GS_CS_709_SCRGB) {
  585. tech_name = "DrawMultiply";
  586. multiplier = obs_get_video_sdr_white_level() / 80.f;
  587. }
  588. gs_effect_t *const effect =
  589. obs_get_base_effect(OBS_EFFECT_DEFAULT);
  590. gs_technique_t *tech =
  591. gs_effect_get_technique(effect, tech_name);
  592. const bool previous = gs_framebuffer_srgb_enabled();
  593. gs_enable_framebuffer_srgb(true);
  594. gs_blend_state_push();
  595. gs_blend_function(GS_BLEND_ONE, GS_BLEND_INVSRCALPHA);
  596. gs_texture_t *const texture = capture->texture;
  597. gs_effect_set_texture_srgb(
  598. gs_effect_get_param_by_name(effect, "image"), texture);
  599. gs_effect_set_float(gs_effect_get_param_by_name(effect,
  600. "multiplier"),
  601. multiplier);
  602. const size_t passes = gs_technique_begin(tech);
  603. for (size_t i = 0; i < passes; i++) {
  604. if (gs_technique_begin_pass(tech, i)) {
  605. gs_draw_sprite(texture, 0, 0, 0);
  606. gs_technique_end_pass(tech);
  607. }
  608. }
  609. gs_technique_end(tech);
  610. gs_blend_state_pop();
  611. gs_enable_framebuffer_srgb(previous);
  612. }
  613. }
  614. extern "C" EXPORT uint32_t
  615. winrt_capture_width(const struct winrt_capture *capture)
  616. {
  617. return capture ? capture->texture_width : 0;
  618. }
  619. extern "C" EXPORT uint32_t
  620. winrt_capture_height(const struct winrt_capture *capture)
  621. {
  622. return capture ? capture->texture_height : 0;
  623. }
  624. extern "C" EXPORT void winrt_capture_thread_start()
  625. {
  626. struct winrt_capture *capture = capture_list;
  627. void *const device = gs_get_device_obj();
  628. while (capture) {
  629. winrt_capture_device_loss_rebuild(device, capture);
  630. capture = capture->next;
  631. }
  632. }
  633. extern "C" EXPORT void winrt_capture_thread_stop()
  634. {
  635. struct winrt_capture *capture = capture_list;
  636. while (capture) {
  637. winrt_capture_device_loss_release(capture);
  638. capture = capture->next;
  639. }
  640. }