graphics-hook.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  1. #include <windows.h>
  2. #include <psapi.h>
  3. #include <inttypes.h>
  4. #include "graphics-hook.h"
  5. #ifdef OBS_LEGACY
  6. #include "../graphics-hook-ver.h"
  7. #include "../../libobs/util/windows/obfuscate.h"
  8. #else
  9. #include <graphics-hook-ver.h>
  10. #include <util/windows/obfuscate.h>
  11. #endif
  12. #define DEBUG_OUTPUT
  13. #ifdef DEBUG_OUTPUT
  14. #define DbgOut(x) OutputDebugStringA(x)
  15. #else
  16. #define DbgOut(x)
  17. #endif
  18. struct thread_data {
  19. CRITICAL_SECTION mutexes[NUM_BUFFERS];
  20. CRITICAL_SECTION data_mutex;
  21. void *volatile cur_data;
  22. uint8_t *shmem_textures[2];
  23. HANDLE copy_thread;
  24. HANDLE copy_event;
  25. HANDLE stop_event;
  26. volatile int cur_tex;
  27. unsigned int pitch;
  28. unsigned int cy;
  29. volatile bool locked_textures[NUM_BUFFERS];
  30. };
  31. ipc_pipe_client_t pipe = {0};
  32. HANDLE signal_restart = NULL;
  33. HANDLE signal_stop = NULL;
  34. HANDLE signal_ready = NULL;
  35. HANDLE signal_exit = NULL;
  36. static HANDLE signal_init = NULL;
  37. HANDLE tex_mutexes[2] = {NULL, NULL};
  38. static HANDLE filemap_hook_info = NULL;
  39. static HINSTANCE dll_inst = NULL;
  40. static volatile bool stop_loop = false;
  41. static HANDLE dup_hook_mutex = NULL;
  42. static HANDLE capture_thread = NULL;
  43. char system_path[MAX_PATH] = {0};
  44. char process_name[MAX_PATH] = {0};
  45. wchar_t keepalive_name[64] = {0};
  46. HWND dummy_window = NULL;
  47. static unsigned int shmem_id_counter = 0;
  48. static void *shmem_info = NULL;
  49. static HANDLE shmem_file_handle = 0;
  50. static struct thread_data thread_data = {0};
  51. volatile bool active = false;
  52. struct hook_info *global_hook_info = NULL;
  53. static inline void wait_for_dll_main_finish(HANDLE thread_handle)
  54. {
  55. if (thread_handle) {
  56. WaitForSingleObject(thread_handle, 100);
  57. CloseHandle(thread_handle);
  58. }
  59. }
  60. bool init_pipe(void)
  61. {
  62. char new_name[64];
  63. snprintf(new_name, sizeof(new_name), "%s%lu", PIPE_NAME, GetCurrentProcessId());
  64. const bool success = ipc_pipe_client_open(&pipe, new_name);
  65. if (!success) {
  66. DbgOut("[OBS] Failed to open pipe\n");
  67. }
  68. return success;
  69. }
  70. static HANDLE init_event(const wchar_t *name, DWORD pid)
  71. {
  72. HANDLE handle = create_event_plus_id(name, pid);
  73. if (!handle)
  74. hlog("Failed to get event '%s': %lu", name, GetLastError());
  75. return handle;
  76. }
  77. static HANDLE init_mutex(const wchar_t *name, DWORD pid)
  78. {
  79. HANDLE handle = create_mutex_plus_id(name, pid);
  80. if (!handle)
  81. hlog("Failed to open mutex '%s': %lu", name, GetLastError());
  82. return handle;
  83. }
  84. static inline bool init_signals(void)
  85. {
  86. DWORD pid = GetCurrentProcessId();
  87. signal_restart = init_event(EVENT_CAPTURE_RESTART, pid);
  88. if (!signal_restart) {
  89. return false;
  90. }
  91. signal_stop = init_event(EVENT_CAPTURE_STOP, pid);
  92. if (!signal_stop) {
  93. return false;
  94. }
  95. signal_ready = init_event(EVENT_HOOK_READY, pid);
  96. if (!signal_ready) {
  97. return false;
  98. }
  99. signal_exit = init_event(EVENT_HOOK_EXIT, pid);
  100. if (!signal_exit) {
  101. return false;
  102. }
  103. signal_init = init_event(EVENT_HOOK_INIT, pid);
  104. if (!signal_init) {
  105. return false;
  106. }
  107. return true;
  108. }
  109. static inline bool init_mutexes(void)
  110. {
  111. DWORD pid = GetCurrentProcessId();
  112. tex_mutexes[0] = init_mutex(MUTEX_TEXTURE1, pid);
  113. if (!tex_mutexes[0]) {
  114. return false;
  115. }
  116. tex_mutexes[1] = init_mutex(MUTEX_TEXTURE2, pid);
  117. if (!tex_mutexes[1]) {
  118. return false;
  119. }
  120. return true;
  121. }
  122. static inline bool init_system_path(void)
  123. {
  124. UINT ret = GetSystemDirectoryA(system_path, MAX_PATH);
  125. if (!ret) {
  126. hlog("Failed to get windows system path: %lu", GetLastError());
  127. return false;
  128. }
  129. return true;
  130. }
  131. static inline void log_current_process(void)
  132. {
  133. DWORD len = GetModuleBaseNameA(GetCurrentProcess(), NULL, process_name, MAX_PATH);
  134. if (len > 0) {
  135. process_name[len] = 0;
  136. hlog("graphics-hook.dll loaded against process: %s", process_name);
  137. } else {
  138. hlog("graphics-hook.dll loaded");
  139. }
  140. hlog("(half life scientist) everything.. seems to be in order");
  141. }
  142. static inline bool init_hook_info(void)
  143. {
  144. filemap_hook_info = create_hook_info(GetCurrentProcessId());
  145. if (!filemap_hook_info) {
  146. hlog("Failed to create hook info file mapping: %lu", GetLastError());
  147. return false;
  148. }
  149. global_hook_info = MapViewOfFile(filemap_hook_info, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(struct hook_info));
  150. if (!global_hook_info) {
  151. hlog("Failed to map the hook info file mapping: %lu", GetLastError());
  152. return false;
  153. }
  154. return true;
  155. }
  156. #define DEF_FLAGS (WS_POPUP | WS_CLIPCHILDREN | WS_CLIPSIBLINGS)
  157. static DWORD WINAPI dummy_window_thread(LPVOID *unused)
  158. {
  159. static const wchar_t dummy_window_class[] = L"temp_d3d_window_4039785";
  160. WNDCLASSW wc;
  161. MSG msg;
  162. memset(&wc, 0, sizeof(wc));
  163. wc.style = CS_OWNDC;
  164. wc.hInstance = dll_inst;
  165. wc.lpfnWndProc = (WNDPROC)DefWindowProc;
  166. wc.lpszClassName = dummy_window_class;
  167. if (!RegisterClass(&wc)) {
  168. hlog("Failed to create temp D3D window class: %lu", GetLastError());
  169. return 0;
  170. }
  171. dummy_window = CreateWindowExW(0, dummy_window_class, L"Temp Window", DEF_FLAGS, 0, 0, 1, 1, NULL, NULL,
  172. dll_inst, NULL);
  173. if (!dummy_window) {
  174. hlog("Failed to create temp D3D window: %lu", GetLastError());
  175. return 0;
  176. }
  177. while (GetMessage(&msg, NULL, 0, 0)) {
  178. TranslateMessage(&msg);
  179. DispatchMessage(&msg);
  180. }
  181. (void)unused;
  182. return 0;
  183. }
  184. static inline void init_dummy_window_thread(void)
  185. {
  186. HANDLE thread = CreateThread(NULL, 0, dummy_window_thread, NULL, 0, NULL);
  187. if (!thread) {
  188. hlog("Failed to create temp D3D window thread: %lu", GetLastError());
  189. return;
  190. }
  191. CloseHandle(thread);
  192. }
  193. static inline bool init_hook(HANDLE thread_handle)
  194. {
  195. wait_for_dll_main_finish(thread_handle);
  196. _snwprintf(keepalive_name, sizeof(keepalive_name) / sizeof(wchar_t), L"%s%lu", WINDOW_HOOK_KEEPALIVE,
  197. GetCurrentProcessId());
  198. init_dummy_window_thread();
  199. log_current_process();
  200. SetEvent(signal_restart);
  201. return true;
  202. }
  203. static inline void close_handle(HANDLE *handle)
  204. {
  205. if (*handle) {
  206. CloseHandle(*handle);
  207. *handle = NULL;
  208. }
  209. }
  210. static void free_hook(void)
  211. {
  212. if (filemap_hook_info) {
  213. CloseHandle(filemap_hook_info);
  214. filemap_hook_info = NULL;
  215. }
  216. if (global_hook_info) {
  217. UnmapViewOfFile(global_hook_info);
  218. global_hook_info = NULL;
  219. }
  220. close_handle(&tex_mutexes[1]);
  221. close_handle(&tex_mutexes[0]);
  222. close_handle(&signal_exit);
  223. close_handle(&signal_ready);
  224. close_handle(&signal_stop);
  225. close_handle(&signal_restart);
  226. close_handle(&dup_hook_mutex);
  227. ipc_pipe_client_free(&pipe);
  228. }
  229. static inline bool d3d8_hookable(void)
  230. {
  231. return !!global_hook_info->offsets.d3d8.present;
  232. }
  233. static inline bool ddraw_hookable(void)
  234. {
  235. return !!global_hook_info->offsets.ddraw.surface_create && !!global_hook_info->offsets.ddraw.surface_restore &&
  236. !!global_hook_info->offsets.ddraw.surface_release && !!global_hook_info->offsets.ddraw.surface_unlock &&
  237. !!global_hook_info->offsets.ddraw.surface_blt && !!global_hook_info->offsets.ddraw.surface_flip &&
  238. !!global_hook_info->offsets.ddraw.surface_set_palette &&
  239. !!global_hook_info->offsets.ddraw.palette_set_entries;
  240. }
  241. static inline bool d3d9_hookable(void)
  242. {
  243. return !!global_hook_info->offsets.d3d9.present && !!global_hook_info->offsets.d3d9.present_ex &&
  244. !!global_hook_info->offsets.d3d9.present_swap;
  245. }
  246. static inline bool dxgi_hookable(void)
  247. {
  248. return !!global_hook_info->offsets.dxgi.present && !!global_hook_info->offsets.dxgi.resize;
  249. }
  250. static inline bool attempt_hook(void)
  251. {
  252. //static bool ddraw_hooked = false;
  253. static bool d3d8_hooked = false;
  254. static bool d3d9_hooked = false;
  255. static bool d3d12_hooked = false;
  256. static bool dxgi_hooked = false;
  257. static bool gl_hooked = false;
  258. #ifdef COMPILE_VULKAN_HOOK
  259. static bool vulkan_hooked = false;
  260. if (!vulkan_hooked) {
  261. vulkan_hooked = hook_vulkan();
  262. if (vulkan_hooked) {
  263. return true;
  264. }
  265. }
  266. #endif //COMPILE_VULKAN_HOOK
  267. #ifdef COMPILE_D3D12_HOOK
  268. if (!d3d12_hooked) {
  269. d3d12_hooked = hook_d3d12();
  270. }
  271. #endif
  272. if (!d3d9_hooked) {
  273. if (!d3d9_hookable()) {
  274. DbgOut("[OBS] no D3D9 hook address found!\n");
  275. d3d9_hooked = true;
  276. } else {
  277. d3d9_hooked = hook_d3d9();
  278. if (d3d9_hooked) {
  279. return true;
  280. }
  281. }
  282. }
  283. if (!dxgi_hooked) {
  284. if (!dxgi_hookable()) {
  285. DbgOut("[OBS] no DXGI hook address found!\n");
  286. dxgi_hooked = true;
  287. } else {
  288. dxgi_hooked = hook_dxgi();
  289. if (dxgi_hooked) {
  290. return true;
  291. }
  292. }
  293. }
  294. if (!gl_hooked) {
  295. gl_hooked = hook_gl();
  296. if (gl_hooked) {
  297. return true;
  298. }
  299. /*} else {
  300. rehook_gl();*/
  301. }
  302. if (!d3d8_hooked) {
  303. if (!d3d8_hookable()) {
  304. d3d8_hooked = true;
  305. } else {
  306. d3d8_hooked = hook_d3d8();
  307. if (d3d8_hooked) {
  308. return true;
  309. }
  310. }
  311. }
  312. /*if (!ddraw_hooked) {
  313. if (!ddraw_hookable()) {
  314. ddraw_hooked = true;
  315. } else {
  316. ddraw_hooked = hook_ddraw();
  317. if (ddraw_hooked) {
  318. return true;
  319. }
  320. }
  321. }*/
  322. #if HOOK_VERBOSE_LOGGING
  323. DbgOut("[OBS] Attempt hook: D3D8=");
  324. DbgOut(d3d8_hooked ? "1" : "0");
  325. DbgOut(", D3D9=");
  326. DbgOut(d3d9_hooked ? "1" : "0");
  327. DbgOut(", D3D12=");
  328. DbgOut(d3d12_hooked ? "1" : "0");
  329. DbgOut(", DXGI=");
  330. DbgOut(dxgi_hooked ? "1" : "0");
  331. DbgOut(", GL=");
  332. DbgOut(gl_hooked ? "1" : "0");
  333. #if COMPILE_VULKAN_HOOK
  334. DbgOut(", VK=");
  335. DbgOut(vulkan_hooked ? "1" : "0");
  336. #endif
  337. DbgOut("\n");
  338. #endif
  339. return false;
  340. }
  341. static inline void capture_loop(void)
  342. {
  343. WaitForSingleObject(signal_init, INFINITE);
  344. while (!attempt_hook())
  345. Sleep(40);
  346. for (size_t n = 0; !stop_loop; n++) {
  347. /* this causes it to check every 4 seconds, but still with
  348. * a small sleep interval in case the thread needs to stop */
  349. if (n % 100 == 0)
  350. attempt_hook();
  351. Sleep(40);
  352. }
  353. }
  354. static DWORD WINAPI main_capture_thread(HANDLE thread_handle)
  355. {
  356. if (!init_hook(thread_handle)) {
  357. DbgOut("[OBS] Failed to init hook\n");
  358. free_hook();
  359. return 0;
  360. }
  361. capture_loop();
  362. return 0;
  363. }
  364. static inline void hlogv(const char *format, va_list args)
  365. {
  366. char message[1024] = "";
  367. int num = _vsprintf_p(message, 1024, format, args);
  368. if (num > 0) {
  369. if (!ipc_pipe_client_write(&pipe, message, (size_t)num + 1)) {
  370. ipc_pipe_client_free(&pipe);
  371. }
  372. DbgOut("[OBS] ");
  373. DbgOut(message);
  374. DbgOut("\n");
  375. }
  376. }
  377. void hlog(const char *format, ...)
  378. {
  379. va_list args;
  380. va_start(args, format);
  381. hlogv(format, args);
  382. va_end(args);
  383. }
  384. void hlog_hr(const char *text, HRESULT hr)
  385. {
  386. LPSTR buffer = NULL;
  387. FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS,
  388. NULL, hr, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), (LPSTR)&buffer, 0, NULL);
  389. if (buffer) {
  390. hlog("%s (0x%08lX): %s", text, hr, buffer);
  391. LocalFree(buffer);
  392. } else {
  393. hlog("%s (0x%08lX)", text, hr);
  394. }
  395. }
  396. static inline uint64_t get_clockfreq(void)
  397. {
  398. static bool have_clockfreq = false;
  399. static LARGE_INTEGER clock_freq;
  400. if (!have_clockfreq) {
  401. QueryPerformanceFrequency(&clock_freq);
  402. have_clockfreq = true;
  403. }
  404. return clock_freq.QuadPart;
  405. }
  406. uint64_t os_gettime_ns(void)
  407. {
  408. LARGE_INTEGER current_time;
  409. double time_val;
  410. QueryPerformanceCounter(&current_time);
  411. time_val = (double)current_time.QuadPart;
  412. time_val *= 1000000000.0;
  413. time_val /= (double)get_clockfreq();
  414. return (uint64_t)time_val;
  415. }
  416. static inline int try_lock_shmem_tex(int id)
  417. {
  418. int next = id == 0 ? 1 : 0;
  419. DWORD wait_result = WAIT_FAILED;
  420. wait_result = WaitForSingleObject(tex_mutexes[id], 0);
  421. if (wait_result == WAIT_OBJECT_0 || wait_result == WAIT_ABANDONED) {
  422. return id;
  423. }
  424. wait_result = WaitForSingleObject(tex_mutexes[next], 0);
  425. if (wait_result == WAIT_OBJECT_0 || wait_result == WAIT_ABANDONED) {
  426. return next;
  427. }
  428. return -1;
  429. }
  430. static inline void unlock_shmem_tex(int id)
  431. {
  432. if (id != -1) {
  433. ReleaseMutex(tex_mutexes[id]);
  434. }
  435. }
  436. static inline bool init_shared_info(size_t size, HWND window)
  437. {
  438. wchar_t name[64];
  439. HWND top = GetAncestor(window, GA_ROOT);
  440. swprintf(name, 64, SHMEM_TEXTURE "_%" PRIu64 "_%u", (uint64_t)(uintptr_t)top, ++shmem_id_counter);
  441. shmem_file_handle = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, (DWORD)size, name);
  442. if (!shmem_file_handle) {
  443. hlog("init_shared_info: Failed to create shared memory: %d", GetLastError());
  444. return false;
  445. }
  446. shmem_info = MapViewOfFile(shmem_file_handle, FILE_MAP_ALL_ACCESS, 0, 0, size);
  447. if (!shmem_info) {
  448. hlog("init_shared_info: Failed to map shared memory: %d", GetLastError());
  449. return false;
  450. }
  451. return true;
  452. }
  453. bool capture_init_shtex(struct shtex_data **data, HWND window, uint32_t cx, uint32_t cy, uint32_t format, bool flip,
  454. uintptr_t handle)
  455. {
  456. if (!init_shared_info(sizeof(struct shtex_data), window)) {
  457. hlog("capture_init_shtex: Failed to initialize memory");
  458. return false;
  459. }
  460. *data = shmem_info;
  461. (*data)->tex_handle = (uint32_t)handle;
  462. global_hook_info->hook_ver_major = HOOK_VER_MAJOR;
  463. global_hook_info->hook_ver_minor = HOOK_VER_MINOR;
  464. global_hook_info->window = (uint32_t)(uintptr_t)window;
  465. global_hook_info->type = CAPTURE_TYPE_TEXTURE;
  466. global_hook_info->format = format;
  467. global_hook_info->flip = flip;
  468. global_hook_info->map_id = shmem_id_counter;
  469. global_hook_info->map_size = sizeof(struct shtex_data);
  470. global_hook_info->cx = cx;
  471. global_hook_info->cy = cy;
  472. global_hook_info->UNUSED_base_cx = cx;
  473. global_hook_info->UNUSED_base_cy = cy;
  474. if (!SetEvent(signal_ready)) {
  475. hlog("capture_init_shtex: Failed to signal ready: %d", GetLastError());
  476. return false;
  477. }
  478. active = true;
  479. return true;
  480. }
  481. static DWORD CALLBACK copy_thread(LPVOID unused)
  482. {
  483. uint32_t pitch = thread_data.pitch;
  484. uint32_t cy = thread_data.cy;
  485. HANDLE events[2] = {NULL, NULL};
  486. int shmem_id = 0;
  487. if (!duplicate_handle(&events[0], thread_data.copy_event)) {
  488. hlog_hr("copy_thread: Failed to duplicate copy event: %d", GetLastError());
  489. return 0;
  490. }
  491. if (!duplicate_handle(&events[1], thread_data.stop_event)) {
  492. hlog_hr("copy_thread: Failed to duplicate stop event: %d", GetLastError());
  493. goto finish;
  494. }
  495. for (;;) {
  496. int copy_tex;
  497. void *cur_data;
  498. DWORD ret = WaitForMultipleObjects(2, events, false, INFINITE);
  499. if (ret != WAIT_OBJECT_0) {
  500. break;
  501. }
  502. EnterCriticalSection(&thread_data.data_mutex);
  503. copy_tex = thread_data.cur_tex;
  504. cur_data = thread_data.cur_data;
  505. LeaveCriticalSection(&thread_data.data_mutex);
  506. if (copy_tex < NUM_BUFFERS && !!cur_data) {
  507. EnterCriticalSection(&thread_data.mutexes[copy_tex]);
  508. int lock_id = try_lock_shmem_tex(shmem_id);
  509. if (lock_id != -1) {
  510. memcpy(thread_data.shmem_textures[lock_id], cur_data, (size_t)pitch * (size_t)cy);
  511. unlock_shmem_tex(lock_id);
  512. ((struct shmem_data *)shmem_info)->last_tex = lock_id;
  513. shmem_id = lock_id == 0 ? 1 : 0;
  514. }
  515. LeaveCriticalSection(&thread_data.mutexes[copy_tex]);
  516. }
  517. }
  518. finish:
  519. for (size_t i = 0; i < 2; i++) {
  520. if (events[i]) {
  521. CloseHandle(events[i]);
  522. }
  523. }
  524. (void)unused;
  525. return 0;
  526. }
  527. void shmem_copy_data(size_t idx, void *volatile data)
  528. {
  529. EnterCriticalSection(&thread_data.data_mutex);
  530. thread_data.cur_tex = (int)idx;
  531. thread_data.cur_data = data;
  532. thread_data.locked_textures[idx] = true;
  533. LeaveCriticalSection(&thread_data.data_mutex);
  534. SetEvent(thread_data.copy_event);
  535. }
  536. bool shmem_texture_data_lock(int idx)
  537. {
  538. bool locked;
  539. EnterCriticalSection(&thread_data.data_mutex);
  540. locked = thread_data.locked_textures[idx];
  541. LeaveCriticalSection(&thread_data.data_mutex);
  542. if (locked) {
  543. EnterCriticalSection(&thread_data.mutexes[idx]);
  544. return true;
  545. }
  546. return false;
  547. }
  548. void shmem_texture_data_unlock(int idx)
  549. {
  550. EnterCriticalSection(&thread_data.data_mutex);
  551. thread_data.locked_textures[idx] = false;
  552. LeaveCriticalSection(&thread_data.data_mutex);
  553. LeaveCriticalSection(&thread_data.mutexes[idx]);
  554. }
  555. static inline bool init_shmem_thread(uint32_t pitch, uint32_t cy)
  556. {
  557. struct shmem_data *data = shmem_info;
  558. thread_data.pitch = pitch;
  559. thread_data.cy = cy;
  560. thread_data.shmem_textures[0] = (uint8_t *)data + data->tex1_offset;
  561. thread_data.shmem_textures[1] = (uint8_t *)data + data->tex2_offset;
  562. thread_data.copy_event = CreateEvent(NULL, false, false, NULL);
  563. if (!thread_data.copy_event) {
  564. hlog("init_shmem_thread: Failed to create copy event: %d", GetLastError());
  565. return false;
  566. }
  567. thread_data.stop_event = CreateEvent(NULL, true, false, NULL);
  568. if (!thread_data.stop_event) {
  569. hlog("init_shmem_thread: Failed to create stop event: %d", GetLastError());
  570. return false;
  571. }
  572. for (size_t i = 0; i < NUM_BUFFERS; i++) {
  573. InitializeCriticalSection(&thread_data.mutexes[i]);
  574. }
  575. InitializeCriticalSection(&thread_data.data_mutex);
  576. thread_data.copy_thread = CreateThread(NULL, 0, copy_thread, NULL, 0, NULL);
  577. if (!thread_data.copy_thread) {
  578. hlog("init_shmem_thread: Failed to create thread: %d", GetLastError());
  579. return false;
  580. }
  581. return true;
  582. }
  583. #ifndef ALIGN
  584. #define ALIGN(bytes, align) (((bytes) + ((align)-1)) & ~((align)-1))
  585. #endif
  586. bool capture_init_shmem(struct shmem_data **data, HWND window, uint32_t cx, uint32_t cy, uint32_t pitch,
  587. uint32_t format, bool flip)
  588. {
  589. uint32_t tex_size = cy * pitch;
  590. uint32_t aligned_header = ALIGN(sizeof(struct shmem_data), 32);
  591. uint32_t aligned_tex = ALIGN(tex_size, 32);
  592. uint32_t total_size = aligned_header + aligned_tex * 2 + 32;
  593. uintptr_t align_pos;
  594. if (!init_shared_info(total_size, window)) {
  595. hlog("capture_init_shmem: Failed to initialize memory");
  596. return false;
  597. }
  598. *data = shmem_info;
  599. /* to ensure fast copy rate, align texture data to 256bit addresses */
  600. align_pos = (uintptr_t)shmem_info;
  601. align_pos += aligned_header;
  602. align_pos &= ~(32 - 1);
  603. align_pos -= (uintptr_t)shmem_info;
  604. if (align_pos < sizeof(struct shmem_data))
  605. align_pos += 32;
  606. (*data)->last_tex = -1;
  607. (*data)->tex1_offset = (uint32_t)align_pos;
  608. (*data)->tex2_offset = (*data)->tex1_offset + aligned_tex;
  609. global_hook_info->hook_ver_major = HOOK_VER_MAJOR;
  610. global_hook_info->hook_ver_minor = HOOK_VER_MINOR;
  611. global_hook_info->window = (uint32_t)(uintptr_t)window;
  612. global_hook_info->type = CAPTURE_TYPE_MEMORY;
  613. global_hook_info->format = format;
  614. global_hook_info->flip = flip;
  615. global_hook_info->map_id = shmem_id_counter;
  616. global_hook_info->map_size = total_size;
  617. global_hook_info->pitch = pitch;
  618. global_hook_info->cx = cx;
  619. global_hook_info->cy = cy;
  620. global_hook_info->UNUSED_base_cx = cx;
  621. global_hook_info->UNUSED_base_cy = cy;
  622. if (!init_shmem_thread(pitch, cy)) {
  623. return false;
  624. }
  625. if (!SetEvent(signal_ready)) {
  626. hlog("capture_init_shmem: Failed to signal ready: %d", GetLastError());
  627. return false;
  628. }
  629. active = true;
  630. return true;
  631. }
  632. static inline void thread_data_free(void)
  633. {
  634. if (thread_data.copy_thread) {
  635. DWORD ret;
  636. SetEvent(thread_data.stop_event);
  637. ret = WaitForSingleObject(thread_data.copy_thread, 500);
  638. if (ret != WAIT_OBJECT_0)
  639. TerminateThread(thread_data.copy_thread, (DWORD)-1);
  640. CloseHandle(thread_data.copy_thread);
  641. }
  642. if (thread_data.stop_event)
  643. CloseHandle(thread_data.stop_event);
  644. if (thread_data.copy_event)
  645. CloseHandle(thread_data.copy_event);
  646. for (size_t i = 0; i < NUM_BUFFERS; i++)
  647. DeleteCriticalSection(&thread_data.mutexes[i]);
  648. DeleteCriticalSection(&thread_data.data_mutex);
  649. memset(&thread_data, 0, sizeof(thread_data));
  650. }
  651. void capture_free(void)
  652. {
  653. thread_data_free();
  654. if (shmem_info) {
  655. UnmapViewOfFile(shmem_info);
  656. shmem_info = NULL;
  657. }
  658. close_handle(&shmem_file_handle);
  659. SetEvent(signal_restart);
  660. active = false;
  661. }
  662. #define HOOK_NAME L"graphics_hook_dup_mutex"
  663. static inline HANDLE open_mutex_plus_id(const wchar_t *name, DWORD id)
  664. {
  665. wchar_t new_name[64];
  666. _snwprintf(new_name, 64, L"%s%lu", name, id);
  667. return open_mutex(new_name);
  668. }
  669. static bool init_dll(void)
  670. {
  671. DWORD pid = GetCurrentProcessId();
  672. HANDLE h;
  673. h = open_mutex_plus_id(HOOK_NAME, pid);
  674. if (h) {
  675. CloseHandle(h);
  676. return false;
  677. }
  678. dup_hook_mutex = create_mutex_plus_id(HOOK_NAME, pid);
  679. return !!dup_hook_mutex;
  680. }
  681. BOOL WINAPI DllMain(HINSTANCE hinst, DWORD reason, LPVOID unused1)
  682. {
  683. if (reason == DLL_PROCESS_ATTACH) {
  684. wchar_t name[MAX_PATH];
  685. dll_inst = hinst;
  686. if (!init_dll()) {
  687. DbgOut("[OBS] Duplicate hook library");
  688. return false;
  689. }
  690. HANDLE cur_thread;
  691. bool success = DuplicateHandle(GetCurrentProcess(), GetCurrentThread(), GetCurrentProcess(),
  692. &cur_thread, SYNCHRONIZE, false, 0);
  693. if (!success)
  694. DbgOut("[OBS] Failed to get current thread handle");
  695. if (!init_signals()) {
  696. return false;
  697. }
  698. if (!init_system_path()) {
  699. return false;
  700. }
  701. if (!init_hook_info()) {
  702. return false;
  703. }
  704. if (!init_mutexes()) {
  705. return false;
  706. }
  707. /* this prevents the library from being automatically unloaded
  708. * by the next FreeLibrary call */
  709. GetModuleFileNameW(hinst, name, MAX_PATH);
  710. LoadLibraryW(name);
  711. capture_thread =
  712. CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)main_capture_thread, (LPVOID)cur_thread, 0, 0);
  713. if (!capture_thread) {
  714. CloseHandle(cur_thread);
  715. return false;
  716. }
  717. } else if (reason == DLL_PROCESS_DETACH) {
  718. if (!dup_hook_mutex) {
  719. return true;
  720. }
  721. if (capture_thread) {
  722. stop_loop = true;
  723. WaitForSingleObject(capture_thread, 300);
  724. CloseHandle(capture_thread);
  725. }
  726. free_hook();
  727. }
  728. (void)unused1;
  729. return true;
  730. }
  731. __declspec(dllexport) LRESULT CALLBACK dummy_debug_proc(int code, WPARAM wparam, LPARAM lparam)
  732. {
  733. static bool hooking = true;
  734. MSG *msg = (MSG *)lparam;
  735. if (hooking && msg->message == (WM_USER + 432)) {
  736. HMODULE user32 = GetModuleHandleW(L"USER32");
  737. BOOL(WINAPI * unhook_windows_hook_ex)(HHOOK) = NULL;
  738. unhook_windows_hook_ex = ms_get_obfuscated_func(user32, "VojeleY`bdgxvM`hhDz", 0x7F55F80C9EE3A213ULL);
  739. if (unhook_windows_hook_ex)
  740. unhook_windows_hook_ex((HHOOK)msg->lParam);
  741. hooking = false;
  742. }
  743. return CallNextHookEx(0, code, wparam, lparam);
  744. }