window-capture.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  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. "\tforce SDR: %s",
  157. obs_source_get_name(wc->source), wc->executable,
  158. get_method_name(method), get_method_name(wc->method),
  159. (wc->force_sdr && (wc->method == METHOD_WGC)) ? "true"
  160. : "false");
  161. blog(LOG_DEBUG, "\tclass: %s", wc->class);
  162. }
  163. }
  164. extern bool wgc_supported;
  165. static void update_settings(struct window_capture *wc, obs_data_t *s)
  166. {
  167. pthread_mutex_lock(&wc->update_mutex);
  168. int method = (int)obs_data_get_int(s, "method");
  169. const char *window = obs_data_get_string(s, "window");
  170. int priority = (int)obs_data_get_int(s, "priority");
  171. bfree(wc->title);
  172. bfree(wc->class);
  173. bfree(wc->executable);
  174. ms_build_window_strings(window, &wc->class, &wc->title,
  175. &wc->executable);
  176. wc->method = choose_method(method, wgc_supported, wc->class);
  177. wc->priority = (enum window_priority)priority;
  178. wc->cursor = obs_data_get_bool(s, "cursor");
  179. wc->force_sdr = obs_data_get_bool(s, "force_sdr");
  180. wc->compatibility = obs_data_get_bool(s, "compatibility");
  181. wc->client_area = obs_data_get_bool(s, "client_area");
  182. pthread_mutex_unlock(&wc->update_mutex);
  183. }
  184. /* ------------------------------------------------------------------------- */
  185. static const char *wc_getname(void *unused)
  186. {
  187. UNUSED_PARAMETER(unused);
  188. return TEXT_WINDOW_CAPTURE;
  189. }
  190. #define WINRT_IMPORT(func) \
  191. do { \
  192. exports->func = (PFN_##func)os_dlsym(module, #func); \
  193. if (!exports->func) { \
  194. success = false; \
  195. blog(LOG_ERROR, \
  196. "Could not load function '%s' from " \
  197. "module '%s'", \
  198. #func, module_name); \
  199. } \
  200. } while (false)
  201. static bool load_winrt_imports(struct winrt_exports *exports, void *module,
  202. const char *module_name)
  203. {
  204. bool success = true;
  205. WINRT_IMPORT(winrt_capture_supported);
  206. WINRT_IMPORT(winrt_capture_cursor_toggle_supported);
  207. WINRT_IMPORT(winrt_capture_init_window);
  208. WINRT_IMPORT(winrt_capture_free);
  209. WINRT_IMPORT(winrt_capture_active);
  210. WINRT_IMPORT(winrt_capture_show_cursor);
  211. WINRT_IMPORT(winrt_capture_get_color_space);
  212. WINRT_IMPORT(winrt_capture_render);
  213. WINRT_IMPORT(winrt_capture_width);
  214. WINRT_IMPORT(winrt_capture_height);
  215. return success;
  216. }
  217. extern bool graphics_uses_d3d11;
  218. static void *wc_create(obs_data_t *settings, obs_source_t *source)
  219. {
  220. struct window_capture *wc = bzalloc(sizeof(struct window_capture));
  221. wc->source = source;
  222. pthread_mutex_init(&wc->update_mutex, NULL);
  223. if (graphics_uses_d3d11) {
  224. static const char *const module = "libobs-winrt";
  225. wc->winrt_module = os_dlopen(module);
  226. if (wc->winrt_module) {
  227. load_winrt_imports(&wc->exports, wc->winrt_module,
  228. module);
  229. }
  230. }
  231. const HMODULE hModuleUser32 = GetModuleHandle(L"User32.dll");
  232. if (hModuleUser32) {
  233. PFN_SetThreadDpiAwarenessContext
  234. set_thread_dpi_awareness_context =
  235. (PFN_SetThreadDpiAwarenessContext)GetProcAddress(
  236. hModuleUser32,
  237. "SetThreadDpiAwarenessContext");
  238. PFN_GetThreadDpiAwarenessContext
  239. get_thread_dpi_awareness_context =
  240. (PFN_GetThreadDpiAwarenessContext)GetProcAddress(
  241. hModuleUser32,
  242. "GetThreadDpiAwarenessContext");
  243. PFN_GetWindowDpiAwarenessContext
  244. get_window_dpi_awareness_context =
  245. (PFN_GetWindowDpiAwarenessContext)GetProcAddress(
  246. hModuleUser32,
  247. "GetWindowDpiAwarenessContext");
  248. if (set_thread_dpi_awareness_context &&
  249. get_thread_dpi_awareness_context &&
  250. get_window_dpi_awareness_context) {
  251. wc->set_thread_dpi_awareness_context =
  252. set_thread_dpi_awareness_context;
  253. wc->get_thread_dpi_awareness_context =
  254. get_thread_dpi_awareness_context;
  255. wc->get_window_dpi_awareness_context =
  256. get_window_dpi_awareness_context;
  257. }
  258. }
  259. update_settings(wc, settings);
  260. log_settings(wc, settings);
  261. return wc;
  262. }
  263. static void wc_actual_destroy(void *data)
  264. {
  265. struct window_capture *wc = data;
  266. if (wc->capture_winrt) {
  267. wc->exports.winrt_capture_free(wc->capture_winrt);
  268. }
  269. obs_enter_graphics();
  270. dc_capture_free(&wc->capture);
  271. obs_leave_graphics();
  272. bfree(wc->title);
  273. bfree(wc->class);
  274. bfree(wc->executable);
  275. if (wc->winrt_module)
  276. os_dlclose(wc->winrt_module);
  277. pthread_mutex_destroy(&wc->update_mutex);
  278. bfree(wc);
  279. }
  280. static void wc_destroy(void *data)
  281. {
  282. obs_queue_task(OBS_TASK_GRAPHICS, wc_actual_destroy, data, false);
  283. }
  284. static void force_reset(struct window_capture *wc)
  285. {
  286. wc->window = NULL;
  287. wc->resize_timer = RESIZE_CHECK_TIME;
  288. wc->check_window_timer = WC_CHECK_TIMER;
  289. wc->cursor_check_time = CURSOR_CHECK_TIME;
  290. wc->previously_failed = false;
  291. }
  292. static void wc_update(void *data, obs_data_t *settings)
  293. {
  294. struct window_capture *wc = data;
  295. update_settings(wc, settings);
  296. log_settings(wc, settings);
  297. force_reset(wc);
  298. }
  299. static uint32_t wc_width(void *data)
  300. {
  301. struct window_capture *wc = data;
  302. return (wc->method == METHOD_WGC)
  303. ? wc->exports.winrt_capture_width(wc->capture_winrt)
  304. : wc->capture.width;
  305. }
  306. static uint32_t wc_height(void *data)
  307. {
  308. struct window_capture *wc = data;
  309. return (wc->method == METHOD_WGC)
  310. ? wc->exports.winrt_capture_height(wc->capture_winrt)
  311. : wc->capture.height;
  312. }
  313. static void wc_defaults(obs_data_t *defaults)
  314. {
  315. obs_data_set_default_int(defaults, "method", METHOD_AUTO);
  316. obs_data_set_default_bool(defaults, "cursor", true);
  317. obs_data_set_default_bool(defaults, "force_sdr", false);
  318. obs_data_set_default_bool(defaults, "compatibility", false);
  319. obs_data_set_default_bool(defaults, "client_area", true);
  320. }
  321. static void update_settings_visibility(obs_properties_t *props,
  322. struct window_capture *wc)
  323. {
  324. pthread_mutex_lock(&wc->update_mutex);
  325. const enum window_capture_method method = wc->method;
  326. const bool bitblt_options = method == METHOD_BITBLT;
  327. const bool wgc_options = method == METHOD_WGC;
  328. const bool wgc_cursor_toggle =
  329. wgc_options &&
  330. wc->exports.winrt_capture_cursor_toggle_supported();
  331. obs_property_t *p = obs_properties_get(props, "cursor");
  332. obs_property_set_visible(p, bitblt_options || wgc_cursor_toggle);
  333. p = obs_properties_get(props, "compatibility");
  334. obs_property_set_visible(p, bitblt_options);
  335. p = obs_properties_get(props, "client_area");
  336. obs_property_set_visible(p, wgc_options);
  337. p = obs_properties_get(props, "force_sdr");
  338. obs_property_set_visible(p, wgc_options);
  339. pthread_mutex_unlock(&wc->update_mutex);
  340. }
  341. static void wc_check_compatibility(struct window_capture *wc,
  342. obs_properties_t *props)
  343. {
  344. obs_property_t *p_warn = obs_properties_get(props, "compat_info");
  345. struct compat_result *compat =
  346. check_compatibility(wc->title, wc->class, wc->executable,
  347. (enum source_type)wc->method);
  348. if (!compat) {
  349. obs_property_set_visible(p_warn, false);
  350. return;
  351. }
  352. obs_property_set_long_description(p_warn, compat->message);
  353. obs_property_text_set_info_type(p_warn, compat->severity);
  354. obs_property_set_visible(p_warn, true);
  355. compat_result_free(compat);
  356. }
  357. static bool wc_capture_method_changed(obs_properties_t *props,
  358. obs_property_t *p, obs_data_t *settings)
  359. {
  360. UNUSED_PARAMETER(p);
  361. struct window_capture *wc = obs_properties_get_param(props);
  362. if (!wc)
  363. return false;
  364. update_settings(wc, settings);
  365. update_settings_visibility(props, wc);
  366. wc_check_compatibility(wc, props);
  367. return true;
  368. }
  369. static bool wc_window_changed(obs_properties_t *props, obs_property_t *p,
  370. obs_data_t *settings)
  371. {
  372. struct window_capture *wc = obs_properties_get_param(props);
  373. if (!wc)
  374. return false;
  375. update_settings(wc, settings);
  376. update_settings_visibility(props, wc);
  377. ms_check_window_property_setting(props, p, settings, "window", 0);
  378. wc_check_compatibility(wc, props);
  379. return true;
  380. }
  381. static obs_properties_t *wc_properties(void *data)
  382. {
  383. struct window_capture *wc = data;
  384. obs_properties_t *ppts = obs_properties_create();
  385. obs_properties_set_param(ppts, wc, NULL);
  386. obs_property_t *p;
  387. p = obs_properties_add_list(ppts, "window", TEXT_WINDOW,
  388. OBS_COMBO_TYPE_LIST,
  389. OBS_COMBO_FORMAT_STRING);
  390. ms_fill_window_list(p, EXCLUDE_MINIMIZED, NULL);
  391. obs_property_set_modified_callback(p, wc_window_changed);
  392. p = obs_properties_add_list(ppts, "method", TEXT_METHOD,
  393. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  394. obs_property_list_add_int(p, TEXT_METHOD_AUTO, METHOD_AUTO);
  395. obs_property_list_add_int(p, TEXT_METHOD_BITBLT, METHOD_BITBLT);
  396. obs_property_list_add_int(p, TEXT_METHOD_WGC, METHOD_WGC);
  397. obs_property_list_item_disable(p, 2, !wgc_supported);
  398. obs_property_set_modified_callback(p, wc_capture_method_changed);
  399. p = obs_properties_add_list(ppts, "priority", TEXT_MATCH_PRIORITY,
  400. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  401. obs_property_list_add_int(p, TEXT_MATCH_TITLE, WINDOW_PRIORITY_TITLE);
  402. obs_property_list_add_int(p, TEXT_MATCH_CLASS, WINDOW_PRIORITY_CLASS);
  403. obs_property_list_add_int(p, TEXT_MATCH_EXE, WINDOW_PRIORITY_EXE);
  404. p = obs_properties_add_text(ppts, "compat_info", NULL, OBS_TEXT_INFO);
  405. obs_property_set_enabled(p, false);
  406. obs_properties_add_bool(ppts, "cursor", TEXT_CAPTURE_CURSOR);
  407. obs_properties_add_bool(ppts, "compatibility", TEXT_COMPATIBILITY);
  408. obs_properties_add_bool(ppts, "client_area", TEXT_CLIENT_AREA);
  409. obs_properties_add_bool(ppts, "force_sdr", TEXT_FORCE_SDR);
  410. return ppts;
  411. }
  412. static void wc_hide(void *data)
  413. {
  414. struct window_capture *wc = data;
  415. if (wc->capture_winrt) {
  416. wc->exports.winrt_capture_free(wc->capture_winrt);
  417. wc->capture_winrt = NULL;
  418. }
  419. memset(&wc->last_rect, 0, sizeof(wc->last_rect));
  420. }
  421. static void wc_tick(void *data, float seconds)
  422. {
  423. struct window_capture *wc = data;
  424. RECT rect;
  425. bool reset_capture = false;
  426. if (!obs_source_showing(wc->source))
  427. return;
  428. if (!wc->window || !IsWindow(wc->window)) {
  429. if (!wc->title && !wc->class) {
  430. if (wc->capture.valid)
  431. dc_capture_free(&wc->capture);
  432. return;
  433. }
  434. wc->check_window_timer += seconds;
  435. if (wc->check_window_timer < WC_CHECK_TIMER) {
  436. if (wc->capture.valid)
  437. dc_capture_free(&wc->capture);
  438. return;
  439. }
  440. if (wc->capture_winrt) {
  441. wc->exports.winrt_capture_free(wc->capture_winrt);
  442. wc->capture_winrt = NULL;
  443. }
  444. wc->check_window_timer = 0.0f;
  445. wc->window =
  446. (wc->method == METHOD_WGC)
  447. ? ms_find_window_top_level(INCLUDE_MINIMIZED,
  448. wc->priority,
  449. wc->class, wc->title,
  450. wc->executable)
  451. : ms_find_window(INCLUDE_MINIMIZED,
  452. wc->priority, wc->class,
  453. wc->title, wc->executable);
  454. if (!wc->window) {
  455. if (wc->capture.valid)
  456. dc_capture_free(&wc->capture);
  457. return;
  458. }
  459. wc->previously_failed = false;
  460. reset_capture = true;
  461. } else if (IsIconic(wc->window) || !IsWindowVisible(wc->window)) {
  462. return; /* If HWND is invisible, WGC module can't be initialized successfully */
  463. }
  464. wc->cursor_check_time += seconds;
  465. if (wc->cursor_check_time >= CURSOR_CHECK_TIME) {
  466. DWORD foreground_pid, target_pid;
  467. // Can't just compare the window handle in case of app with child windows
  468. if (!GetWindowThreadProcessId(GetForegroundWindow(),
  469. &foreground_pid))
  470. foreground_pid = 0;
  471. if (!GetWindowThreadProcessId(wc->window, &target_pid))
  472. target_pid = 0;
  473. const bool cursor_hidden = foreground_pid && target_pid &&
  474. foreground_pid != target_pid;
  475. wc->capture.cursor_hidden = cursor_hidden;
  476. if (wc->capture_winrt &&
  477. !wc->exports.winrt_capture_show_cursor(wc->capture_winrt,
  478. !cursor_hidden)) {
  479. force_reset(wc);
  480. return;
  481. }
  482. wc->cursor_check_time = 0.0f;
  483. }
  484. obs_enter_graphics();
  485. if (wc->method == METHOD_BITBLT) {
  486. DPI_AWARENESS_CONTEXT previous = NULL;
  487. if (wc->get_window_dpi_awareness_context != NULL) {
  488. const DPI_AWARENESS_CONTEXT context =
  489. wc->get_window_dpi_awareness_context(
  490. wc->window);
  491. previous =
  492. wc->set_thread_dpi_awareness_context(context);
  493. }
  494. GetClientRect(wc->window, &rect);
  495. if (!reset_capture) {
  496. wc->resize_timer += seconds;
  497. if (wc->resize_timer >= RESIZE_CHECK_TIME) {
  498. if ((rect.bottom - rect.top) !=
  499. (wc->last_rect.bottom -
  500. wc->last_rect.top) ||
  501. (rect.right - rect.left) !=
  502. (wc->last_rect.right -
  503. wc->last_rect.left))
  504. reset_capture = true;
  505. wc->resize_timer = 0.0f;
  506. }
  507. }
  508. if (reset_capture) {
  509. wc->resize_timer = 0.0f;
  510. wc->last_rect = rect;
  511. dc_capture_free(&wc->capture);
  512. dc_capture_init(&wc->capture, 0, 0,
  513. rect.right - rect.left,
  514. rect.bottom - rect.top, wc->cursor,
  515. wc->compatibility);
  516. }
  517. dc_capture_capture(&wc->capture, wc->window);
  518. if (previous)
  519. wc->set_thread_dpi_awareness_context(previous);
  520. } else if (wc->method == METHOD_WGC) {
  521. if (wc->window && (wc->capture_winrt == NULL)) {
  522. if (!wc->previously_failed) {
  523. wc->capture_winrt =
  524. wc->exports.winrt_capture_init_window(
  525. wc->cursor, wc->window,
  526. wc->client_area, wc->force_sdr);
  527. if (!wc->capture_winrt) {
  528. wc->previously_failed = true;
  529. }
  530. }
  531. }
  532. }
  533. obs_leave_graphics();
  534. }
  535. static void wc_render(void *data, gs_effect_t *effect)
  536. {
  537. struct window_capture *wc = data;
  538. if (wc->method == METHOD_WGC) {
  539. if (wc->capture_winrt) {
  540. if (wc->exports.winrt_capture_active(
  541. wc->capture_winrt)) {
  542. wc->exports.winrt_capture_render(
  543. wc->capture_winrt);
  544. } else {
  545. wc->exports.winrt_capture_free(
  546. wc->capture_winrt);
  547. wc->capture_winrt = NULL;
  548. }
  549. }
  550. } else {
  551. dc_capture_render(
  552. &wc->capture,
  553. obs_source_get_texcoords_centered(wc->source));
  554. }
  555. UNUSED_PARAMETER(effect);
  556. }
  557. enum gs_color_space
  558. wc_get_color_space(void *data, size_t count,
  559. const enum gs_color_space *preferred_spaces)
  560. {
  561. struct window_capture *wc = data;
  562. enum gs_color_space capture_space = GS_CS_SRGB;
  563. if ((wc->method == METHOD_WGC) && wc->capture_winrt) {
  564. capture_space = wc->exports.winrt_capture_get_color_space(
  565. wc->capture_winrt);
  566. }
  567. enum gs_color_space space = capture_space;
  568. for (size_t i = 0; i < count; ++i) {
  569. const enum gs_color_space preferred_space = preferred_spaces[i];
  570. space = preferred_space;
  571. if (preferred_space == capture_space)
  572. break;
  573. }
  574. return space;
  575. }
  576. struct obs_source_info window_capture_info = {
  577. .id = "window_capture",
  578. .type = OBS_SOURCE_TYPE_INPUT,
  579. .output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW |
  580. OBS_SOURCE_SRGB,
  581. .get_name = wc_getname,
  582. .create = wc_create,
  583. .destroy = wc_destroy,
  584. .update = wc_update,
  585. .video_render = wc_render,
  586. .hide = wc_hide,
  587. .video_tick = wc_tick,
  588. .get_width = wc_width,
  589. .get_height = wc_height,
  590. .get_defaults = wc_defaults,
  591. .get_properties = wc_properties,
  592. .icon_type = OBS_ICON_TYPE_WINDOW_CAPTURE,
  593. .video_get_color_space = wc_get_color_space,
  594. };