window-capture.c 20 KB

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