graphics-hook.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #pragma once
  2. #include "graphics-hook-config.h"
  3. #ifdef _MSC_VER
  4. /* conversion from data/function pointer */
  5. #pragma warning(disable : 4152)
  6. #endif
  7. #include "../graphics-hook-info.h"
  8. #include <ipc-util/pipe.h>
  9. #include <psapi.h>
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #else
  13. #if defined(_MSC_VER) && !defined(inline)
  14. #define inline __inline
  15. #endif
  16. #endif
  17. #define NUM_BUFFERS 3
  18. extern void hlog(const char *format, ...);
  19. extern void hlog_hr(const char *text, HRESULT hr);
  20. static inline const char *get_process_name(void);
  21. static inline HMODULE get_system_module(const char *module);
  22. static inline HMODULE load_system_library(const char *module);
  23. extern uint64_t os_gettime_ns(void);
  24. static inline bool capture_active(void);
  25. static inline bool capture_ready(void);
  26. static inline bool capture_should_stop(void);
  27. static inline bool capture_should_init(void);
  28. extern void shmem_copy_data(size_t idx, void *volatile data);
  29. extern bool shmem_texture_data_lock(int idx);
  30. extern void shmem_texture_data_unlock(int idx);
  31. extern bool hook_ddraw(void);
  32. extern bool hook_d3d8(void);
  33. extern bool hook_d3d9(void);
  34. extern bool hook_dxgi(void);
  35. extern bool hook_gl(void);
  36. extern void d3d10_capture(void *swap, void *backbuffer, bool capture_overlay);
  37. extern void d3d10_free(void);
  38. extern void d3d11_capture(void *swap, void *backbuffer, bool capture_overlay);
  39. extern void d3d11_free(void);
  40. #if COMPILE_D3D12_HOOK
  41. extern void d3d12_capture(void *swap, void *backbuffer, bool capture_overlay);
  42. extern void d3d12_free(void);
  43. #endif
  44. extern uint8_t *get_d3d1x_vertex_shader(size_t *size);
  45. extern uint8_t *get_d3d1x_pixel_shader(size_t *size);
  46. extern bool rehook_gl(void);
  47. extern bool capture_init_shtex(struct shtex_data **data, HWND window,
  48. uint32_t base_cx, uint32_t base_cy, uint32_t cx,
  49. uint32_t cy, uint32_t format, bool flip,
  50. uintptr_t handle);
  51. extern bool capture_init_shmem(struct shmem_data **data, HWND window,
  52. uint32_t base_cx, uint32_t base_cy, uint32_t cx,
  53. uint32_t cy, uint32_t pitch, uint32_t format,
  54. bool flip);
  55. extern void capture_free(void);
  56. extern struct hook_info *global_hook_info;
  57. struct vertex {
  58. struct {
  59. float x, y, z, w;
  60. } pos;
  61. struct {
  62. float u, v;
  63. } tex;
  64. };
  65. static inline bool duplicate_handle(HANDLE *dst, HANDLE src)
  66. {
  67. return !!DuplicateHandle(GetCurrentProcess(), src, GetCurrentProcess(),
  68. dst, 0, false, DUPLICATE_SAME_ACCESS);
  69. }
  70. static inline void *get_offset_addr(HMODULE module, uint32_t offset)
  71. {
  72. return (void *)((uintptr_t)module + (uintptr_t)offset);
  73. }
  74. /* ------------------------------------------------------------------------- */
  75. extern ipc_pipe_client_t pipe;
  76. extern HANDLE signal_restart;
  77. extern HANDLE signal_stop;
  78. extern HANDLE signal_ready;
  79. extern HANDLE signal_exit;
  80. extern HANDLE tex_mutexes[2];
  81. extern char system_path[MAX_PATH];
  82. extern char process_name[MAX_PATH];
  83. extern wchar_t keepalive_name[64];
  84. extern HWND dummy_window;
  85. extern volatile bool active;
  86. static inline const char *get_process_name(void)
  87. {
  88. return process_name;
  89. }
  90. static inline HMODULE get_system_module(const char *module)
  91. {
  92. char base_path[MAX_PATH];
  93. strcpy(base_path, system_path);
  94. strcat(base_path, "\\");
  95. strcat(base_path, module);
  96. return GetModuleHandleA(base_path);
  97. }
  98. static inline uint32_t module_size(HMODULE module)
  99. {
  100. MODULEINFO info;
  101. bool success = !!GetModuleInformation(GetCurrentProcess(), module,
  102. &info, sizeof(info));
  103. return success ? info.SizeOfImage : 0;
  104. }
  105. static inline HMODULE load_system_library(const char *name)
  106. {
  107. char base_path[MAX_PATH];
  108. HMODULE module;
  109. strcpy(base_path, system_path);
  110. strcat(base_path, "\\");
  111. strcat(base_path, name);
  112. module = GetModuleHandleA(base_path);
  113. if (module)
  114. return module;
  115. return LoadLibraryA(base_path);
  116. }
  117. static inline bool capture_alive(void)
  118. {
  119. HANDLE handle = OpenMutexW(SYNCHRONIZE, false, keepalive_name);
  120. CloseHandle(handle);
  121. if (handle)
  122. return true;
  123. return GetLastError() != ERROR_FILE_NOT_FOUND;
  124. }
  125. static inline bool capture_active(void)
  126. {
  127. return active;
  128. }
  129. static inline bool frame_ready(uint64_t interval)
  130. {
  131. static uint64_t last_time = 0;
  132. uint64_t elapsed;
  133. uint64_t t;
  134. if (!interval) {
  135. return true;
  136. }
  137. t = os_gettime_ns();
  138. elapsed = t - last_time;
  139. if (elapsed < interval) {
  140. return false;
  141. }
  142. last_time = (elapsed > interval * 2) ? t : last_time + interval;
  143. return true;
  144. }
  145. static inline bool capture_ready(void)
  146. {
  147. return capture_active() &&
  148. frame_ready(global_hook_info->frame_interval);
  149. }
  150. static inline bool capture_stopped(void)
  151. {
  152. return WaitForSingleObject(signal_stop, 0) == WAIT_OBJECT_0;
  153. }
  154. static inline bool capture_restarted(void)
  155. {
  156. return WaitForSingleObject(signal_restart, 0) == WAIT_OBJECT_0;
  157. }
  158. static inline bool capture_should_stop(void)
  159. {
  160. bool stop_requested = false;
  161. if (capture_active()) {
  162. static uint64_t last_keepalive_check = 0;
  163. uint64_t cur_time = os_gettime_ns();
  164. bool alive = true;
  165. if (cur_time - last_keepalive_check > 5000000000) {
  166. alive = capture_alive();
  167. last_keepalive_check = cur_time;
  168. }
  169. stop_requested = capture_stopped() || !alive;
  170. }
  171. return stop_requested;
  172. }
  173. extern bool init_pipe(void);
  174. static inline bool capture_should_init(void)
  175. {
  176. if (!capture_active() && capture_restarted()) {
  177. if (capture_alive()) {
  178. if (!ipc_pipe_client_valid(&pipe)) {
  179. init_pipe();
  180. }
  181. return true;
  182. }
  183. }
  184. return false;
  185. }
  186. #ifdef __cplusplus
  187. }
  188. #endif