window-capture.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. #include <stdlib.h>
  2. #include <util/dstr.h>
  3. #include <util/threading.h>
  4. #include <util/windows/window-helpers.h>
  5. #include "dc-capture.h"
  6. #include "compat-helpers.h"
  7. #include "../../libobs/util/platform.h"
  8. #include "../../libobs-winrt/winrt-capture.h"
  9. /* clang-format off */
  10. #define TEXT_WINDOW_CAPTURE obs_module_text("WindowCapture")
  11. #define TEXT_WINDOW obs_module_text("WindowCapture.Window")
  12. #define TEXT_METHOD obs_module_text("WindowCapture.Method")
  13. #define TEXT_METHOD_AUTO obs_module_text("WindowCapture.Method.Auto")
  14. #define TEXT_METHOD_BITBLT obs_module_text("WindowCapture.Method.BitBlt")
  15. #define TEXT_METHOD_WGC obs_module_text("WindowCapture.Method.WindowsGraphicsCapture")
  16. #define TEXT_MATCH_PRIORITY obs_module_text("WindowCapture.Priority")
  17. #define TEXT_MATCH_TITLE obs_module_text("WindowCapture.Priority.Title")
  18. #define TEXT_MATCH_CLASS obs_module_text("WindowCapture.Priority.Class")
  19. #define TEXT_MATCH_EXE obs_module_text("WindowCapture.Priority.Exe")
  20. #define TEXT_CAPTURE_CURSOR obs_module_text("CaptureCursor")
  21. #define TEXT_COMPATIBILITY obs_module_text("Compatibility")
  22. #define TEXT_CLIENT_AREA obs_module_text("ClientArea")
  23. #define TEXT_FORCE_SDR obs_module_text("ForceSdr")
  24. /* clang-format on */
  25. #define WC_CHECK_TIMER 1.0f
  26. #define RESIZE_CHECK_TIME 0.2f
  27. #define CURSOR_CHECK_TIME 0.2f
  28. typedef BOOL (*PFN_winrt_capture_supported)();
  29. typedef BOOL (*PFN_winrt_capture_cursor_toggle_supported)();
  30. typedef struct winrt_capture *(*PFN_winrt_capture_init_window)(BOOL cursor,
  31. HWND window,
  32. BOOL client_area,
  33. BOOL force_sdr);
  34. typedef void (*PFN_winrt_capture_free)(struct winrt_capture *capture);
  35. typedef BOOL (*PFN_winrt_capture_active)(const struct winrt_capture *capture);
  36. typedef BOOL (*PFN_winrt_capture_show_cursor)(struct winrt_capture *capture,
  37. BOOL visible);
  38. typedef enum gs_color_space (*PFN_winrt_capture_get_color_space)(
  39. const struct winrt_capture *capture);
  40. typedef void (*PFN_winrt_capture_render)(struct winrt_capture *capture);
  41. typedef uint32_t (*PFN_winrt_capture_width)(const struct winrt_capture *capture);
  42. typedef uint32_t (*PFN_winrt_capture_height)(
  43. const struct winrt_capture *capture);
  44. struct winrt_exports {
  45. PFN_winrt_capture_supported winrt_capture_supported;
  46. PFN_winrt_capture_cursor_toggle_supported
  47. winrt_capture_cursor_toggle_supported;
  48. PFN_winrt_capture_init_window winrt_capture_init_window;
  49. PFN_winrt_capture_free winrt_capture_free;
  50. PFN_winrt_capture_active winrt_capture_active;
  51. PFN_winrt_capture_show_cursor winrt_capture_show_cursor;
  52. PFN_winrt_capture_get_color_space winrt_capture_get_color_space;
  53. PFN_winrt_capture_render winrt_capture_render;
  54. PFN_winrt_capture_width winrt_capture_width;
  55. PFN_winrt_capture_height winrt_capture_height;
  56. };
  57. enum window_capture_method {
  58. METHOD_AUTO,
  59. METHOD_BITBLT,
  60. METHOD_WGC,
  61. };
  62. typedef DPI_AWARENESS_CONTEXT(WINAPI *PFN_SetThreadDpiAwarenessContext)(
  63. DPI_AWARENESS_CONTEXT);
  64. typedef DPI_AWARENESS_CONTEXT(WINAPI *PFN_GetThreadDpiAwarenessContext)(VOID);
  65. typedef DPI_AWARENESS_CONTEXT(WINAPI *PFN_GetWindowDpiAwarenessContext)(HWND);
  66. struct window_capture {
  67. obs_source_t *source;
  68. pthread_mutex_t update_mutex;
  69. char *title;
  70. char *class;
  71. char *executable;
  72. enum window_capture_method method;
  73. enum window_priority priority;
  74. bool cursor;
  75. bool compatibility;
  76. bool client_area;
  77. bool force_sdr;
  78. struct dc_capture capture;
  79. bool previously_failed;
  80. void *winrt_module;
  81. struct winrt_exports exports;
  82. struct winrt_capture *capture_winrt;
  83. float resize_timer;
  84. float check_window_timer;
  85. float cursor_check_time;
  86. HWND window;
  87. RECT last_rect;
  88. PFN_SetThreadDpiAwarenessContext set_thread_dpi_awareness_context;
  89. PFN_GetThreadDpiAwarenessContext get_thread_dpi_awareness_context;
  90. PFN_GetWindowDpiAwarenessContext get_window_dpi_awareness_context;
  91. };
  92. static const char *wgc_partial_match_classes[] = {
  93. "Chrome",
  94. "Mozilla",
  95. NULL,
  96. };
  97. static const char *wgc_whole_match_classes[] = {
  98. "ApplicationFrameWindow",
  99. "Windows.UI.Core.CoreWindow",
  100. "XLMAIN", /* excel*/
  101. "PPTFrameClass", /* powerpoint */
  102. "OpusApp", /* word */
  103. NULL,
  104. };
  105. static enum window_capture_method
  106. choose_method(enum window_capture_method method, bool wgc_supported,
  107. const char *current_class)
  108. {
  109. if (!wgc_supported)
  110. return METHOD_BITBLT;
  111. if (method != METHOD_AUTO)
  112. return method;
  113. if (!current_class)
  114. return METHOD_BITBLT;
  115. const char **class = wgc_partial_match_classes;
  116. while (*class) {
  117. if (astrstri(current_class, *class) != NULL) {
  118. return METHOD_WGC;
  119. }
  120. class ++;
  121. }
  122. class = wgc_whole_match_classes;
  123. while (*class) {
  124. if (astrcmpi(current_class, *class) == 0) {
  125. return METHOD_WGC;
  126. }
  127. class ++;
  128. }
  129. return METHOD_BITBLT;
  130. }
  131. static const char *get_method_name(int method)
  132. {
  133. const char *method_name = "";
  134. switch (method) {
  135. case METHOD_AUTO:
  136. method_name = "Automatic";
  137. break;
  138. case METHOD_BITBLT:
  139. method_name = "BitBlt";
  140. break;
  141. case METHOD_WGC:
  142. method_name = "WGC";
  143. break;
  144. }
  145. return method_name;
  146. }
  147. static void log_settings(struct window_capture *wc, obs_data_t *s)
  148. {
  149. int method = (int)obs_data_get_int(s, "method");
  150. if (wc->title != NULL) {
  151. blog(LOG_INFO,
  152. "[window-capture: '%s'] update settings:\n"
  153. "\texecutable: %s\n"
  154. "\tmethod selected: %s\n"
  155. "\tmethod chosen: %s\n",
  156. obs_source_get_name(wc->source), wc->executable,
  157. get_method_name(method), get_method_name(wc->method));
  158. blog(LOG_DEBUG, "\tclass: %s", wc->class);
  159. }
  160. }
  161. extern bool wgc_supported;
  162. static void update_settings(struct window_capture *wc, obs_data_t *s)
  163. {
  164. pthread_mutex_lock(&wc->update_mutex);
  165. int method = (int)obs_data_get_int(s, "method");
  166. const char *window = obs_data_get_string(s, "window");
  167. int priority = (int)obs_data_get_int(s, "priority");
  168. bfree(wc->title);
  169. bfree(wc->class);
  170. bfree(wc->executable);
  171. ms_build_window_strings(window, &wc->class, &wc->title,
  172. &wc->executable);
  173. wc->method = choose_method(method, wgc_supported, wc->class);
  174. wc->priority = (enum window_priority)priority;
  175. wc->cursor = obs_data_get_bool(s, "cursor");
  176. wc->force_sdr = obs_data_get_bool(s, "force_sdr");
  177. wc->compatibility = obs_data_get_bool(s, "compatibility");
  178. wc->client_area = obs_data_get_bool(s, "client_area");
  179. pthread_mutex_unlock(&wc->update_mutex);
  180. }
  181. /* ------------------------------------------------------------------------- */
  182. static const char *wc_getname(void *unused)
  183. {
  184. UNUSED_PARAMETER(unused);
  185. return TEXT_WINDOW_CAPTURE;
  186. }
  187. #define WINRT_IMPORT(func) \
  188. do { \
  189. exports->func = (PFN_##func)os_dlsym(module, #func); \
  190. if (!exports->func) { \
  191. success = false; \
  192. blog(LOG_ERROR, \
  193. "Could not load function '%s' from " \
  194. "module '%s'", \
  195. #func, module_name); \
  196. } \
  197. } while (false)
  198. static bool load_winrt_imports(struct winrt_exports *exports, void *module,
  199. const char *module_name)
  200. {
  201. bool success = true;
  202. WINRT_IMPORT(winrt_capture_supported);
  203. WINRT_IMPORT(winrt_capture_cursor_toggle_supported);
  204. WINRT_IMPORT(winrt_capture_init_window);
  205. WINRT_IMPORT(winrt_capture_free);
  206. WINRT_IMPORT(winrt_capture_active);
  207. WINRT_IMPORT(winrt_capture_show_cursor);
  208. WINRT_IMPORT(winrt_capture_get_color_space);
  209. WINRT_IMPORT(winrt_capture_render);
  210. WINRT_IMPORT(winrt_capture_width);
  211. WINRT_IMPORT(winrt_capture_height);
  212. return success;
  213. }
  214. extern bool graphics_uses_d3d11;
  215. static void *wc_create(obs_data_t *settings, obs_source_t *source)
  216. {
  217. struct window_capture *wc = bzalloc(sizeof(struct window_capture));
  218. wc->source = source;
  219. pthread_mutex_init(&wc->update_mutex, NULL);
  220. if (graphics_uses_d3d11) {
  221. static const char *const module = "libobs-winrt";
  222. wc->winrt_module = os_dlopen(module);
  223. if (wc->winrt_module) {
  224. load_winrt_imports(&wc->exports, wc->winrt_module,
  225. module);
  226. }
  227. }
  228. const HMODULE hModuleUser32 = GetModuleHandle(L"User32.dll");
  229. if (hModuleUser32) {
  230. PFN_SetThreadDpiAwarenessContext
  231. set_thread_dpi_awareness_context =
  232. (PFN_SetThreadDpiAwarenessContext)GetProcAddress(
  233. hModuleUser32,
  234. "SetThreadDpiAwarenessContext");
  235. PFN_GetThreadDpiAwarenessContext
  236. get_thread_dpi_awareness_context =
  237. (PFN_GetThreadDpiAwarenessContext)GetProcAddress(
  238. hModuleUser32,
  239. "GetThreadDpiAwarenessContext");
  240. PFN_GetWindowDpiAwarenessContext
  241. get_window_dpi_awareness_context =
  242. (PFN_GetWindowDpiAwarenessContext)GetProcAddress(
  243. hModuleUser32,
  244. "GetWindowDpiAwarenessContext");
  245. if (set_thread_dpi_awareness_context &&
  246. get_thread_dpi_awareness_context &&
  247. get_window_dpi_awareness_context) {
  248. wc->set_thread_dpi_awareness_context =
  249. set_thread_dpi_awareness_context;
  250. wc->get_thread_dpi_awareness_context =
  251. get_thread_dpi_awareness_context;
  252. wc->get_window_dpi_awareness_context =
  253. get_window_dpi_awareness_context;
  254. }
  255. }
  256. update_settings(wc, settings);
  257. log_settings(wc, settings);
  258. return wc;
  259. }
  260. static void wc_actual_destroy(void *data)
  261. {
  262. struct window_capture *wc = data;
  263. if (wc->capture_winrt) {
  264. wc->exports.winrt_capture_free(wc->capture_winrt);
  265. }
  266. obs_enter_graphics();
  267. dc_capture_free(&wc->capture);
  268. obs_leave_graphics();
  269. bfree(wc->title);
  270. bfree(wc->class);
  271. bfree(wc->executable);
  272. if (wc->winrt_module)
  273. os_dlclose(wc->winrt_module);
  274. pthread_mutex_destroy(&wc->update_mutex);
  275. bfree(wc);
  276. }
  277. static void wc_destroy(void *data)
  278. {
  279. obs_queue_task(OBS_TASK_GRAPHICS, wc_actual_destroy, data, false);
  280. }
  281. static void force_reset(struct window_capture *wc)
  282. {
  283. wc->window = NULL;
  284. wc->resize_timer = RESIZE_CHECK_TIME;
  285. wc->check_window_timer = WC_CHECK_TIMER;
  286. wc->cursor_check_time = CURSOR_CHECK_TIME;
  287. wc->previously_failed = false;
  288. }
  289. static void wc_update(void *data, obs_data_t *settings)
  290. {
  291. struct window_capture *wc = data;
  292. update_settings(wc, settings);
  293. log_settings(wc, settings);
  294. force_reset(wc);
  295. }
  296. static uint32_t wc_width(void *data)
  297. {
  298. struct window_capture *wc = data;
  299. return (wc->method == METHOD_WGC)
  300. ? wc->exports.winrt_capture_width(wc->capture_winrt)
  301. : wc->capture.width;
  302. }
  303. static uint32_t wc_height(void *data)
  304. {
  305. struct window_capture *wc = data;
  306. return (wc->method == METHOD_WGC)
  307. ? wc->exports.winrt_capture_height(wc->capture_winrt)
  308. : wc->capture.height;
  309. }
  310. static void wc_defaults(obs_data_t *defaults)
  311. {
  312. obs_data_set_default_int(defaults, "method", METHOD_AUTO);
  313. obs_data_set_default_bool(defaults, "cursor", true);
  314. obs_data_set_default_bool(defaults, "force_sdr", false);
  315. obs_data_set_default_bool(defaults, "compatibility", false);
  316. obs_data_set_default_bool(defaults, "client_area", true);
  317. }
  318. static void update_settings_visibility(obs_properties_t *props,
  319. struct window_capture *wc)
  320. {
  321. pthread_mutex_lock(&wc->update_mutex);
  322. const enum window_capture_method method = wc->method;
  323. const bool bitblt_options = method == METHOD_BITBLT;
  324. const bool wgc_options = method == METHOD_WGC;
  325. const bool wgc_cursor_toggle =
  326. wgc_options &&
  327. wc->exports.winrt_capture_cursor_toggle_supported();
  328. obs_property_t *p = obs_properties_get(props, "cursor");
  329. obs_property_set_visible(p, bitblt_options || wgc_cursor_toggle);
  330. p = obs_properties_get(props, "compatibility");
  331. obs_property_set_visible(p, bitblt_options);
  332. p = obs_properties_get(props, "client_area");
  333. obs_property_set_visible(p, wgc_options);
  334. p = obs_properties_get(props, "force_sdr");
  335. obs_property_set_visible(p, wgc_options);
  336. pthread_mutex_unlock(&wc->update_mutex);
  337. }
  338. static void wc_check_compatibility(struct window_capture *wc,
  339. obs_properties_t *props)
  340. {
  341. obs_property_t *p_warn = obs_properties_get(props, "compat_info");
  342. struct compat_result *compat =
  343. check_compatibility(wc->title, wc->class, wc->executable,
  344. (enum source_type)wc->method);
  345. if (!compat) {
  346. obs_property_set_visible(p_warn, false);
  347. return;
  348. }
  349. obs_property_set_long_description(p_warn, compat->message);
  350. obs_property_text_set_info_type(p_warn, compat->severity);
  351. obs_property_set_visible(p_warn, true);
  352. compat_result_free(compat);
  353. }
  354. static bool wc_capture_method_changed(obs_properties_t *props,
  355. obs_property_t *p, obs_data_t *settings)
  356. {
  357. UNUSED_PARAMETER(p);
  358. struct window_capture *wc = obs_properties_get_param(props);
  359. if (!wc)
  360. return false;
  361. update_settings(wc, settings);
  362. update_settings_visibility(props, wc);
  363. wc_check_compatibility(wc, props);
  364. return true;
  365. }
  366. static bool wc_window_changed(obs_properties_t *props, obs_property_t *p,
  367. obs_data_t *settings)
  368. {
  369. struct window_capture *wc = obs_properties_get_param(props);
  370. if (!wc)
  371. return false;
  372. update_settings(wc, settings);
  373. update_settings_visibility(props, wc);
  374. ms_check_window_property_setting(props, p, settings, "window", 0);
  375. wc_check_compatibility(wc, props);
  376. return true;
  377. }
  378. static obs_properties_t *wc_properties(void *data)
  379. {
  380. struct window_capture *wc = data;
  381. obs_properties_t *ppts = obs_properties_create();
  382. obs_properties_set_param(ppts, wc, NULL);
  383. obs_property_t *p;
  384. p = obs_properties_add_list(ppts, "window", TEXT_WINDOW,
  385. OBS_COMBO_TYPE_LIST,
  386. OBS_COMBO_FORMAT_STRING);
  387. ms_fill_window_list(p, EXCLUDE_MINIMIZED, NULL);
  388. obs_property_set_modified_callback(p, wc_window_changed);
  389. p = obs_properties_add_list(ppts, "method", TEXT_METHOD,
  390. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  391. obs_property_list_add_int(p, TEXT_METHOD_AUTO, METHOD_AUTO);
  392. obs_property_list_add_int(p, TEXT_METHOD_BITBLT, METHOD_BITBLT);
  393. obs_property_list_add_int(p, TEXT_METHOD_WGC, METHOD_WGC);
  394. obs_property_list_item_disable(p, 2, !wgc_supported);
  395. obs_property_set_modified_callback(p, wc_capture_method_changed);
  396. p = obs_properties_add_list(ppts, "priority", TEXT_MATCH_PRIORITY,
  397. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  398. obs_property_list_add_int(p, TEXT_MATCH_TITLE, WINDOW_PRIORITY_TITLE);
  399. obs_property_list_add_int(p, TEXT_MATCH_CLASS, WINDOW_PRIORITY_CLASS);
  400. obs_property_list_add_int(p, TEXT_MATCH_EXE, WINDOW_PRIORITY_EXE);
  401. p = obs_properties_add_text(ppts, "compat_info", NULL, OBS_TEXT_INFO);
  402. obs_property_set_enabled(p, false);
  403. obs_properties_add_bool(ppts, "cursor", TEXT_CAPTURE_CURSOR);
  404. obs_properties_add_bool(ppts, "compatibility", TEXT_COMPATIBILITY);
  405. obs_properties_add_bool(ppts, "client_area", TEXT_CLIENT_AREA);
  406. obs_properties_add_bool(ppts, "force_sdr", TEXT_FORCE_SDR);
  407. return ppts;
  408. }
  409. static void wc_hide(void *data)
  410. {
  411. struct window_capture *wc = data;
  412. if (wc->capture_winrt) {
  413. wc->exports.winrt_capture_free(wc->capture_winrt);
  414. wc->capture_winrt = NULL;
  415. }
  416. memset(&wc->last_rect, 0, sizeof(wc->last_rect));
  417. }
  418. static void wc_tick(void *data, float seconds)
  419. {
  420. struct window_capture *wc = data;
  421. RECT rect;
  422. bool reset_capture = false;
  423. if (!obs_source_showing(wc->source))
  424. return;
  425. if (!wc->window || !IsWindow(wc->window)) {
  426. if (!wc->title && !wc->class) {
  427. if (wc->capture.valid)
  428. dc_capture_free(&wc->capture);
  429. return;
  430. }
  431. wc->check_window_timer += seconds;
  432. if (wc->check_window_timer < WC_CHECK_TIMER) {
  433. if (wc->capture.valid)
  434. dc_capture_free(&wc->capture);
  435. return;
  436. }
  437. if (wc->capture_winrt) {
  438. wc->exports.winrt_capture_free(wc->capture_winrt);
  439. wc->capture_winrt = NULL;
  440. }
  441. wc->check_window_timer = 0.0f;
  442. wc->window =
  443. (wc->method == METHOD_WGC)
  444. ? ms_find_window_top_level(INCLUDE_MINIMIZED,
  445. wc->priority,
  446. wc->class, wc->title,
  447. wc->executable)
  448. : ms_find_window(INCLUDE_MINIMIZED,
  449. wc->priority, wc->class,
  450. wc->title, wc->executable);
  451. if (!wc->window) {
  452. if (wc->capture.valid)
  453. dc_capture_free(&wc->capture);
  454. return;
  455. }
  456. wc->previously_failed = false;
  457. reset_capture = true;
  458. } else if (IsIconic(wc->window) || !IsWindowVisible(wc->window)) {
  459. return; /* If HWND is invisible, WGC module can't be initialized successfully */
  460. }
  461. wc->cursor_check_time += seconds;
  462. if (wc->cursor_check_time >= CURSOR_CHECK_TIME) {
  463. DWORD foreground_pid, target_pid;
  464. // Can't just compare the window handle in case of app with child windows
  465. if (!GetWindowThreadProcessId(GetForegroundWindow(),
  466. &foreground_pid))
  467. foreground_pid = 0;
  468. if (!GetWindowThreadProcessId(wc->window, &target_pid))
  469. target_pid = 0;
  470. const bool cursor_hidden = foreground_pid && target_pid &&
  471. foreground_pid != target_pid;
  472. wc->capture.cursor_hidden = cursor_hidden;
  473. if (wc->capture_winrt &&
  474. !wc->exports.winrt_capture_show_cursor(wc->capture_winrt,
  475. !cursor_hidden)) {
  476. force_reset(wc);
  477. return;
  478. }
  479. wc->cursor_check_time = 0.0f;
  480. }
  481. obs_enter_graphics();
  482. if (wc->method == METHOD_BITBLT) {
  483. DPI_AWARENESS_CONTEXT previous = NULL;
  484. if (wc->get_window_dpi_awareness_context != NULL) {
  485. const DPI_AWARENESS_CONTEXT context =
  486. wc->get_window_dpi_awareness_context(
  487. wc->window);
  488. previous =
  489. wc->set_thread_dpi_awareness_context(context);
  490. }
  491. GetClientRect(wc->window, &rect);
  492. if (!reset_capture) {
  493. wc->resize_timer += seconds;
  494. if (wc->resize_timer >= RESIZE_CHECK_TIME) {
  495. if ((rect.bottom - rect.top) !=
  496. (wc->last_rect.bottom -
  497. wc->last_rect.top) ||
  498. (rect.right - rect.left) !=
  499. (wc->last_rect.right -
  500. wc->last_rect.left))
  501. reset_capture = true;
  502. wc->resize_timer = 0.0f;
  503. }
  504. }
  505. if (reset_capture) {
  506. wc->resize_timer = 0.0f;
  507. wc->last_rect = rect;
  508. dc_capture_free(&wc->capture);
  509. dc_capture_init(&wc->capture, 0, 0,
  510. rect.right - rect.left,
  511. rect.bottom - rect.top, wc->cursor,
  512. wc->compatibility);
  513. }
  514. dc_capture_capture(&wc->capture, wc->window);
  515. if (previous)
  516. wc->set_thread_dpi_awareness_context(previous);
  517. } else if (wc->method == METHOD_WGC) {
  518. if (wc->window && (wc->capture_winrt == NULL)) {
  519. if (!wc->previously_failed) {
  520. wc->capture_winrt =
  521. wc->exports.winrt_capture_init_window(
  522. wc->cursor, wc->window,
  523. wc->client_area, wc->force_sdr);
  524. if (!wc->capture_winrt) {
  525. wc->previously_failed = true;
  526. }
  527. }
  528. }
  529. }
  530. obs_leave_graphics();
  531. }
  532. static void wc_render(void *data, gs_effect_t *effect)
  533. {
  534. struct window_capture *wc = data;
  535. if (wc->method == METHOD_WGC) {
  536. if (wc->capture_winrt) {
  537. if (wc->exports.winrt_capture_active(
  538. wc->capture_winrt)) {
  539. wc->exports.winrt_capture_render(
  540. wc->capture_winrt);
  541. } else {
  542. wc->exports.winrt_capture_free(
  543. wc->capture_winrt);
  544. wc->capture_winrt = NULL;
  545. }
  546. }
  547. } else {
  548. dc_capture_render(
  549. &wc->capture,
  550. obs_source_get_texcoords_centered(wc->source));
  551. }
  552. UNUSED_PARAMETER(effect);
  553. }
  554. enum gs_color_space
  555. wc_get_color_space(void *data, size_t count,
  556. const enum gs_color_space *preferred_spaces)
  557. {
  558. struct window_capture *wc = data;
  559. enum gs_color_space capture_space = GS_CS_SRGB;
  560. if ((wc->method == METHOD_WGC) && wc->capture_winrt) {
  561. capture_space = wc->exports.winrt_capture_get_color_space(
  562. wc->capture_winrt);
  563. }
  564. enum gs_color_space space = capture_space;
  565. for (size_t i = 0; i < count; ++i) {
  566. const enum gs_color_space preferred_space = preferred_spaces[i];
  567. space = preferred_space;
  568. if (preferred_space == capture_space)
  569. break;
  570. }
  571. return space;
  572. }
  573. struct obs_source_info window_capture_info = {
  574. .id = "window_capture",
  575. .type = OBS_SOURCE_TYPE_INPUT,
  576. .output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW |
  577. OBS_SOURCE_SRGB,
  578. .get_name = wc_getname,
  579. .create = wc_create,
  580. .destroy = wc_destroy,
  581. .update = wc_update,
  582. .video_render = wc_render,
  583. .hide = wc_hide,
  584. .video_tick = wc_tick,
  585. .get_width = wc_width,
  586. .get_height = wc_height,
  587. .get_defaults = wc_defaults,
  588. .get_properties = wc_properties,
  589. .icon_type = OBS_ICON_TYPE_WINDOW_CAPTURE,
  590. .video_get_color_space = wc_get_color_space,
  591. };