graphics-hook.h 4.5 KB

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