graphics-hook.c 21 KB

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