winrt-capture.cpp 19 KB

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