dc-capture.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. #include "dc-capture.h"
  2. #define WIN32_MEAN_AND_LEAN
  3. #include <windows.h>
  4. static inline void init_textures(struct dc_capture *capture)
  5. {
  6. if (capture->compatibility) {
  7. capture->texture = gs_texture_create(capture->width,
  8. capture->height, GS_BGRA,
  9. 1, NULL, GS_DYNAMIC);
  10. } else {
  11. capture->texture =
  12. gs_texture_create_gdi(capture->width, capture->height);
  13. if (capture->texture) {
  14. capture->extra_texture = gs_texture_create(
  15. capture->width, capture->height, GS_BGRA, 1,
  16. NULL, 0);
  17. if (!capture->extra_texture) {
  18. blog(LOG_WARNING, "[dc_capture_init] Failed to "
  19. "create textures");
  20. gs_texture_destroy(capture->texture);
  21. capture->texture = NULL;
  22. }
  23. }
  24. }
  25. if (!capture->texture) {
  26. blog(LOG_WARNING, "[dc_capture_init] Failed to "
  27. "create textures");
  28. return;
  29. }
  30. capture->valid = true;
  31. }
  32. void dc_capture_init(struct dc_capture *capture, int x, int y, uint32_t width,
  33. uint32_t height, bool cursor, bool compatibility)
  34. {
  35. memset(capture, 0, sizeof(struct dc_capture));
  36. capture->x = x;
  37. capture->y = y;
  38. capture->width = width;
  39. capture->height = height;
  40. capture->capture_cursor = cursor;
  41. obs_enter_graphics();
  42. if (!gs_gdi_texture_available())
  43. compatibility = true;
  44. capture->compatibility = compatibility;
  45. init_textures(capture);
  46. obs_leave_graphics();
  47. if (!capture->valid)
  48. return;
  49. if (compatibility) {
  50. BITMAPINFO bi = {0};
  51. BITMAPINFOHEADER *bih = &bi.bmiHeader;
  52. bih->biSize = sizeof(BITMAPINFOHEADER);
  53. bih->biBitCount = 32;
  54. bih->biWidth = width;
  55. bih->biHeight = height;
  56. bih->biPlanes = 1;
  57. const HDC hdc = CreateCompatibleDC(NULL);
  58. if (hdc) {
  59. const HBITMAP bmp = CreateDIBSection(
  60. capture->hdc, &bi, DIB_RGB_COLORS,
  61. (void **)&capture->bits, NULL, 0);
  62. if (bmp) {
  63. capture->hdc = hdc;
  64. capture->bmp = bmp;
  65. capture->old_bmp = SelectObject(capture->hdc,
  66. capture->bmp);
  67. } else {
  68. DeleteDC(hdc);
  69. }
  70. }
  71. }
  72. }
  73. void dc_capture_free(struct dc_capture *capture)
  74. {
  75. if (capture->hdc) {
  76. SelectObject(capture->hdc, capture->old_bmp);
  77. DeleteDC(capture->hdc);
  78. DeleteObject(capture->bmp);
  79. }
  80. obs_enter_graphics();
  81. gs_texture_destroy(capture->extra_texture);
  82. gs_texture_destroy(capture->texture);
  83. obs_leave_graphics();
  84. memset(capture, 0, sizeof(struct dc_capture));
  85. }
  86. static void draw_cursor(struct dc_capture *capture, HDC hdc, HWND window)
  87. {
  88. HICON icon;
  89. ICONINFO ii;
  90. CURSORINFO *ci = &capture->ci;
  91. POINT win_pos = {capture->x, capture->y};
  92. if (!(capture->ci.flags & CURSOR_SHOWING))
  93. return;
  94. icon = CopyIcon(capture->ci.hCursor);
  95. if (!icon)
  96. return;
  97. if (GetIconInfo(icon, &ii)) {
  98. POINT pos;
  99. if (window)
  100. ClientToScreen(window, &win_pos);
  101. pos.x = ci->ptScreenPos.x - (int)ii.xHotspot - win_pos.x;
  102. pos.y = ci->ptScreenPos.y - (int)ii.yHotspot - win_pos.y;
  103. DrawIconEx(hdc, pos.x, pos.y, icon, 0, 0, 0, NULL, DI_NORMAL);
  104. DeleteObject(ii.hbmColor);
  105. DeleteObject(ii.hbmMask);
  106. }
  107. DestroyIcon(icon);
  108. }
  109. static inline HDC dc_capture_get_dc(struct dc_capture *capture)
  110. {
  111. if (!capture->valid)
  112. return NULL;
  113. if (capture->compatibility)
  114. return capture->hdc;
  115. else
  116. return gs_texture_get_dc(capture->texture);
  117. }
  118. static inline void dc_capture_release_dc(struct dc_capture *capture)
  119. {
  120. if (capture->compatibility) {
  121. gs_texture_set_image(capture->texture, capture->bits,
  122. capture->width * 4, false);
  123. } else {
  124. gs_texture_release_dc(capture->texture);
  125. }
  126. }
  127. void dc_capture_capture(struct dc_capture *capture, HWND window)
  128. {
  129. HDC hdc_target;
  130. HDC hdc;
  131. if (capture->capture_cursor) {
  132. memset(&capture->ci, 0, sizeof(CURSORINFO));
  133. capture->ci.cbSize = sizeof(CURSORINFO);
  134. capture->cursor_captured = GetCursorInfo(&capture->ci);
  135. }
  136. hdc = dc_capture_get_dc(capture);
  137. if (!hdc) {
  138. blog(LOG_WARNING, "[capture_screen] Failed to get "
  139. "texture DC");
  140. return;
  141. }
  142. hdc_target = GetDC(window);
  143. BitBlt(hdc, 0, 0, capture->width, capture->height, hdc_target,
  144. capture->x, capture->y, SRCCOPY);
  145. ReleaseDC(NULL, hdc_target);
  146. if (capture->cursor_captured && !capture->cursor_hidden)
  147. draw_cursor(capture, hdc, window);
  148. dc_capture_release_dc(capture);
  149. capture->texture_written = true;
  150. }
  151. void dc_capture_render(struct dc_capture *capture, bool texcoords_centered)
  152. {
  153. if (capture->valid && capture->texture_written) {
  154. gs_texture_t *texture = capture->texture;
  155. const bool compatibility = capture->compatibility;
  156. bool linear_sample = compatibility;
  157. if (!linear_sample && !texcoords_centered) {
  158. gs_texture_t *const extra_texture =
  159. capture->extra_texture;
  160. gs_copy_texture(extra_texture, texture);
  161. texture = extra_texture;
  162. linear_sample = true;
  163. }
  164. const char *tech_name = "Draw";
  165. float multiplier = 1.f;
  166. switch (gs_get_color_space()) {
  167. case GS_CS_SRGB_16F:
  168. case GS_CS_709_EXTENDED:
  169. if (!linear_sample)
  170. tech_name = "DrawSrgbDecompress";
  171. break;
  172. case GS_CS_709_SCRGB:
  173. if (linear_sample)
  174. tech_name = "DrawMultiply";
  175. else
  176. tech_name = "DrawSrgbDecompressMultiply";
  177. multiplier = obs_get_video_sdr_white_level() / 80.f;
  178. }
  179. gs_effect_t *effect = obs_get_base_effect(OBS_EFFECT_OPAQUE);
  180. gs_technique_t *tech =
  181. gs_effect_get_technique(effect, tech_name);
  182. gs_eparam_t *image =
  183. gs_effect_get_param_by_name(effect, "image");
  184. const bool previous = gs_framebuffer_srgb_enabled();
  185. gs_enable_framebuffer_srgb(linear_sample);
  186. gs_enable_blending(false);
  187. if (linear_sample)
  188. gs_effect_set_texture_srgb(image, texture);
  189. else
  190. gs_effect_set_texture(image, texture);
  191. gs_eparam_t *multiplier_param =
  192. gs_effect_get_param_by_name(effect, "multiplier");
  193. gs_effect_set_float(multiplier_param, multiplier);
  194. const uint32_t flip = compatibility ? GS_FLIP_V : 0;
  195. const size_t passes = gs_technique_begin(tech);
  196. for (size_t i = 0; i < passes; i++) {
  197. if (gs_technique_begin_pass(tech, i)) {
  198. gs_draw_sprite(texture, flip, 0, 0);
  199. gs_technique_end_pass(tech);
  200. }
  201. }
  202. gs_technique_end(tech);
  203. gs_enable_blending(true);
  204. gs_enable_framebuffer_srgb(previous);
  205. }
  206. }