graphics-hook.h 5.5 KB

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