winrt-capture.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. {
  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. }
  19. template<typename T>
  20. static winrt::com_ptr<T> GetDXGIInterfaceFromObject(
  21. winrt::Windows::Foundation::IInspectable const &object)
  22. {
  23. auto access = object.as<IDirect3DDxgiInterfaceAccess>();
  24. winrt::com_ptr<T> result;
  25. winrt::check_hresult(
  26. access->GetInterface(winrt::guid_of<T>(), result.put_void()));
  27. return result;
  28. }
  29. static bool get_client_box(HWND window, uint32_t width, uint32_t height,
  30. D3D11_BOX *client_box)
  31. {
  32. RECT client_rect, window_rect{};
  33. POINT upper_left{};
  34. const bool client_box_available =
  35. GetClientRect(window, &client_rect) &&
  36. (DwmGetWindowAttribute(window, DWMWA_EXTENDED_FRAME_BOUNDS,
  37. &window_rect,
  38. sizeof(window_rect)) == S_OK) &&
  39. ClientToScreen(window, &upper_left);
  40. if (client_box_available) {
  41. const uint32_t left =
  42. (upper_left.x > window_rect.left)
  43. ? (upper_left.x - window_rect.left)
  44. : 0;
  45. client_box->left = left;
  46. const uint32_t top = (upper_left.y > window_rect.top)
  47. ? (upper_left.y - window_rect.top)
  48. : 0;
  49. client_box->top = top;
  50. uint32_t texture_width = 1;
  51. if (width > left) {
  52. texture_width =
  53. min(width - left, (uint32_t)client_rect.right);
  54. }
  55. uint32_t texture_height = 1;
  56. if (height > top) {
  57. texture_height =
  58. min(height - top, (uint32_t)client_rect.bottom);
  59. }
  60. client_box->right = left + texture_width;
  61. client_box->bottom = top + texture_height;
  62. client_box->front = 0;
  63. client_box->back = 1;
  64. }
  65. return client_box_available;
  66. }
  67. struct winrt_capture {
  68. bool capture_cursor;
  69. HWND window;
  70. bool client_area;
  71. gs_texture_t *texture;
  72. bool texture_written;
  73. winrt::Windows::Graphics::Capture::GraphicsCaptureItem item{nullptr};
  74. winrt::Windows::Graphics::DirectX::Direct3D11::IDirect3DDevice device{
  75. nullptr};
  76. ComPtr<ID3D11DeviceContext> context;
  77. winrt::Windows::Graphics::Capture::Direct3D11CaptureFramePool frame_pool{
  78. nullptr};
  79. winrt::Windows::Graphics::Capture::GraphicsCaptureSession session{
  80. nullptr};
  81. winrt::Windows::Graphics::SizeInt32 last_size;
  82. winrt::Windows::Graphics::Capture::Direct3D11CaptureFramePool::
  83. FrameArrived_revoker frame_arrived;
  84. uint32_t texture_width;
  85. uint32_t texture_height;
  86. D3D11_BOX client_box;
  87. bool client_box_available;
  88. bool thread_changed;
  89. struct winrt_capture *next;
  90. void on_frame_arrived(winrt::Windows::Graphics::Capture::
  91. Direct3D11CaptureFramePool const &sender,
  92. winrt::Windows::Foundation::IInspectable const &)
  93. {
  94. obs_enter_graphics();
  95. const winrt::Windows::Graphics::Capture::Direct3D11CaptureFrame
  96. frame = sender.TryGetNextFrame();
  97. const winrt::Windows::Graphics::SizeInt32 frame_content_size =
  98. frame.ContentSize();
  99. winrt::com_ptr<ID3D11Texture2D> frame_surface =
  100. GetDXGIInterfaceFromObject<ID3D11Texture2D>(
  101. frame.Surface());
  102. /* need GetDesc because ContentSize is not reliable */
  103. D3D11_TEXTURE2D_DESC desc;
  104. frame_surface->GetDesc(&desc);
  105. client_box_available = false;
  106. if (client_area) {
  107. client_box_available = get_client_box(
  108. window, desc.Width, desc.Height, &client_box);
  109. }
  110. if (client_box_available) {
  111. texture_width = client_box.right - client_box.left;
  112. texture_height = client_box.bottom - client_box.top;
  113. } else {
  114. texture_width = desc.Width;
  115. texture_height = desc.Height;
  116. }
  117. if (texture) {
  118. if (texture_width != gs_texture_get_width(texture) ||
  119. texture_height != gs_texture_get_height(texture)) {
  120. gs_texture_destroy(texture);
  121. texture = nullptr;
  122. }
  123. }
  124. if (!texture) {
  125. texture = gs_texture_create(texture_width,
  126. texture_height, GS_BGRA, 1,
  127. nullptr, 0);
  128. }
  129. if (client_box_available) {
  130. context->CopySubresourceRegion(
  131. (ID3D11Texture2D *)gs_texture_get_obj(texture),
  132. 0, 0, 0, 0, frame_surface.get(), 0,
  133. &client_box);
  134. } else {
  135. /* if they gave an SRV, we could avoid this copy */
  136. context->CopyResource(
  137. (ID3D11Texture2D *)gs_texture_get_obj(texture),
  138. frame_surface.get());
  139. }
  140. texture_written = true;
  141. if (frame_content_size.Width != last_size.Width ||
  142. frame_content_size.Height != last_size.Height) {
  143. frame_pool.Recreate(
  144. device,
  145. winrt::Windows::Graphics::DirectX::
  146. DirectXPixelFormat::B8G8R8A8UIntNormalized,
  147. 2, frame_content_size);
  148. last_size = frame_content_size;
  149. }
  150. obs_leave_graphics();
  151. }
  152. };
  153. struct winrt_capture *capture_list;
  154. static void winrt_capture_device_loss_release(void *data)
  155. {
  156. winrt_capture *capture = static_cast<winrt_capture *>(data);
  157. capture->frame_arrived.revoke();
  158. capture->frame_pool.Close();
  159. capture->session.Close();
  160. capture->session = nullptr;
  161. capture->frame_pool = nullptr;
  162. capture->context = nullptr;
  163. capture->device = nullptr;
  164. }
  165. static void winrt_capture_device_loss_rebuild(void *device_void, void *data)
  166. {
  167. winrt_capture *capture = static_cast<winrt_capture *>(data);
  168. ID3D11Device *const d3d_device = (ID3D11Device *)device_void;
  169. ComPtr<IDXGIDevice> dxgi_device;
  170. if (FAILED(d3d_device->QueryInterface(&dxgi_device)))
  171. blog(LOG_ERROR, "Failed to get DXGI device");
  172. winrt::com_ptr<IInspectable> inspectable;
  173. if (FAILED(CreateDirect3D11DeviceFromDXGIDevice(dxgi_device.Get(),
  174. inspectable.put())))
  175. blog(LOG_ERROR, "Failed to get WinRT device");
  176. const winrt::Windows::Graphics::DirectX::Direct3D11::IDirect3DDevice
  177. device = inspectable.as<winrt::Windows::Graphics::DirectX::
  178. Direct3D11::IDirect3DDevice>();
  179. const winrt::Windows::Graphics::Capture::Direct3D11CaptureFramePool
  180. frame_pool = winrt::Windows::Graphics::Capture::
  181. Direct3D11CaptureFramePool::Create(
  182. device,
  183. winrt::Windows::Graphics::DirectX::
  184. DirectXPixelFormat::B8G8R8A8UIntNormalized,
  185. 2, capture->last_size);
  186. const winrt::Windows::Graphics::Capture::GraphicsCaptureSession session =
  187. frame_pool.CreateCaptureSession(capture->item);
  188. capture->device = device;
  189. d3d_device->GetImmediateContext(&capture->context);
  190. capture->frame_pool = frame_pool;
  191. capture->session = session;
  192. capture->frame_arrived = frame_pool.FrameArrived(
  193. winrt::auto_revoke,
  194. {capture, &winrt_capture::on_frame_arrived});
  195. session.StartCapture();
  196. }
  197. thread_local bool initialized_tls;
  198. extern "C" EXPORT struct winrt_capture *
  199. winrt_capture_init(bool cursor, HWND window, bool client_area)
  200. {
  201. ID3D11Device *const d3d_device = (ID3D11Device *)gs_get_device_obj();
  202. ComPtr<IDXGIDevice> dxgi_device;
  203. if (FAILED(d3d_device->QueryInterface(&dxgi_device))) {
  204. blog(LOG_WARNING, "[winrt_capture_init] Failed to "
  205. "get DXGI device");
  206. return nullptr;
  207. }
  208. winrt::com_ptr<IInspectable> inspectable;
  209. HRESULT hr = CreateDirect3D11DeviceFromDXGIDevice(dxgi_device.Get(),
  210. inspectable.put());
  211. if (FAILED(hr)) {
  212. blog(LOG_WARNING, "[winrt_capture_init] Failed to "
  213. "get WinRT device");
  214. return nullptr;
  215. }
  216. auto activation_factory = winrt::get_activation_factory<
  217. winrt::Windows::Graphics::Capture::GraphicsCaptureItem>();
  218. auto interop_factory =
  219. activation_factory.as<IGraphicsCaptureItemInterop>();
  220. winrt::Windows::Graphics::Capture::GraphicsCaptureItem item = {nullptr};
  221. try {
  222. interop_factory->CreateForWindow(
  223. window,
  224. winrt::guid_of<ABI::Windows::Graphics::Capture::
  225. IGraphicsCaptureItem>(),
  226. reinterpret_cast<void **>(winrt::put_abi(item)));
  227. } catch (winrt::hresult_invalid_argument &) {
  228. /* too spammy */
  229. //blog(LOG_WARNING, "[winrt_capture_init] Failed to "
  230. // "create GraphicsCaptureItem");
  231. return nullptr;
  232. }
  233. const winrt::Windows::Graphics::DirectX::Direct3D11::IDirect3DDevice
  234. device = inspectable.as<winrt::Windows::Graphics::DirectX::
  235. Direct3D11::IDirect3DDevice>();
  236. const winrt::Windows::Graphics::SizeInt32 size = item.Size();
  237. const winrt::Windows::Graphics::Capture::Direct3D11CaptureFramePool
  238. frame_pool = winrt::Windows::Graphics::Capture::
  239. Direct3D11CaptureFramePool::Create(
  240. device,
  241. winrt::Windows::Graphics::DirectX::
  242. DirectXPixelFormat::B8G8R8A8UIntNormalized,
  243. 2, size);
  244. const winrt::Windows::Graphics::Capture::GraphicsCaptureSession session =
  245. frame_pool.CreateCaptureSession(item);
  246. if (capture_list == nullptr)
  247. initialized_tls = true;
  248. struct winrt_capture *capture = new winrt_capture{};
  249. capture->capture_cursor = cursor;
  250. capture->window = window;
  251. capture->client_area = client_area;
  252. capture->item = item;
  253. capture->device = device;
  254. d3d_device->GetImmediateContext(&capture->context);
  255. capture->frame_pool = frame_pool;
  256. capture->session = session;
  257. capture->last_size = size;
  258. capture->frame_arrived = frame_pool.FrameArrived(
  259. winrt::auto_revoke,
  260. {capture, &winrt_capture::on_frame_arrived});
  261. capture->next = capture_list;
  262. capture_list = capture;
  263. session.StartCapture();
  264. gs_device_loss callbacks;
  265. callbacks.device_loss_release = winrt_capture_device_loss_release;
  266. callbacks.device_loss_rebuild = winrt_capture_device_loss_rebuild;
  267. callbacks.data = capture;
  268. gs_register_loss_callbacks(&callbacks);
  269. return capture;
  270. }
  271. extern "C" EXPORT void winrt_capture_free(struct winrt_capture *capture)
  272. {
  273. if (capture) {
  274. struct winrt_capture *current = capture_list;
  275. if (current == capture) {
  276. capture_list = capture->next;
  277. } else {
  278. struct winrt_capture *previous;
  279. do {
  280. previous = current;
  281. current = current->next;
  282. } while (current != capture);
  283. previous->next = current->next;
  284. }
  285. obs_enter_graphics();
  286. gs_unregister_loss_callbacks(capture);
  287. gs_texture_destroy(capture->texture);
  288. obs_leave_graphics();
  289. capture->frame_arrived.revoke();
  290. capture->frame_pool.Close();
  291. capture->session.Close();
  292. delete capture;
  293. }
  294. }
  295. static void draw_texture(struct winrt_capture *capture, gs_effect_t *effect)
  296. {
  297. gs_texture_t *const texture = capture->texture;
  298. gs_technique_t *tech = gs_effect_get_technique(effect, "Draw");
  299. gs_eparam_t *image = gs_effect_get_param_by_name(effect, "image");
  300. size_t passes;
  301. gs_effect_set_texture(image, texture);
  302. passes = gs_technique_begin(tech);
  303. for (size_t i = 0; i < passes; i++) {
  304. if (gs_technique_begin_pass(tech, i)) {
  305. gs_draw_sprite(texture, 0, 0, 0);
  306. gs_technique_end_pass(tech);
  307. }
  308. }
  309. gs_technique_end(tech);
  310. }
  311. extern "C" EXPORT void winrt_capture_render(struct winrt_capture *capture,
  312. gs_effect_t *effect)
  313. {
  314. if (capture && capture->texture_written) {
  315. if (!initialized_tls) {
  316. struct winrt_capture *current = capture_list;
  317. while (current) {
  318. current->thread_changed = true;
  319. current = current->next;
  320. }
  321. initialized_tls = true;
  322. }
  323. if (capture->thread_changed) {
  324. /* new graphics thread. treat like device loss. */
  325. winrt_capture_device_loss_release(capture);
  326. winrt_capture_device_loss_rebuild(gs_get_device_obj(),
  327. capture);
  328. capture->thread_changed = false;
  329. }
  330. draw_texture(capture, effect);
  331. }
  332. }
  333. extern "C" EXPORT uint32_t
  334. winrt_capture_width(const struct winrt_capture *capture)
  335. {
  336. return capture ? capture->texture_width : 0;
  337. }
  338. extern "C" EXPORT uint32_t
  339. winrt_capture_height(const struct winrt_capture *capture)
  340. {
  341. return capture ? capture->texture_height : 0;
  342. }