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