graphics-hook.h 4.8 KB

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