graphics-hook.c 21 KB

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