winrt-capture.cpp 21 KB

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