dc-capture.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. for (int i = 0; i < capture->num_textures; i++) {
  7. if (capture->compatibility)
  8. capture->textures[i] = gs_create_texture(
  9. capture->width, capture->height,
  10. GS_BGRA, 1, NULL, GS_DYNAMIC);
  11. else
  12. capture->textures[i] = gs_create_gdi_texture(
  13. capture->width, capture->height);
  14. if (!capture->textures[i]) {
  15. blog(LOG_WARNING, "[dc_capture_init] Failed to "
  16. "create textures");
  17. return;
  18. }
  19. }
  20. capture->valid = true;
  21. }
  22. void dc_capture_init(struct dc_capture *capture, int x, int y,
  23. uint32_t width, uint32_t height, bool cursor,
  24. bool compatibility)
  25. {
  26. memset(capture, 0, sizeof(struct dc_capture));
  27. capture->x = x;
  28. capture->y = y;
  29. capture->width = width;
  30. capture->height = height;
  31. capture->capture_cursor = cursor;
  32. gs_entercontext(obs_graphics());
  33. if (!gs_gdi_texture_available())
  34. compatibility = true;
  35. capture->compatibility = compatibility;
  36. capture->num_textures = compatibility ? 1 : 2;
  37. init_textures(capture);
  38. gs_leavecontext();
  39. if (!capture->valid)
  40. return;
  41. if (compatibility) {
  42. BITMAPINFO bi = {0};
  43. BITMAPINFOHEADER *bih = &bi.bmiHeader;
  44. bih->biSize = sizeof(BITMAPINFOHEADER);
  45. bih->biBitCount = 32;
  46. bih->biWidth = width;
  47. bih->biHeight = height;
  48. bih->biPlanes = 1;
  49. capture->hdc = CreateCompatibleDC(NULL);
  50. capture->bmp = CreateDIBSection(capture->hdc, &bi,
  51. DIB_RGB_COLORS, (void**)&capture->bits,
  52. NULL, 0);
  53. capture->old_bmp = SelectObject(capture->hdc, capture->bmp);
  54. }
  55. }
  56. void dc_capture_free(struct dc_capture *capture)
  57. {
  58. if (capture->hdc) {
  59. SelectObject(capture->hdc, capture->old_bmp);
  60. DeleteDC(capture->hdc);
  61. DeleteObject(capture->bmp);
  62. }
  63. gs_entercontext(obs_graphics());
  64. for (int i = 0; i < capture->num_textures; i++)
  65. texture_destroy(capture->textures[i]);
  66. gs_leavecontext();
  67. memset(capture, 0, sizeof(struct dc_capture));
  68. }
  69. static void draw_cursor(struct dc_capture *capture, HDC hdc, HWND window)
  70. {
  71. HICON icon;
  72. ICONINFO ii;
  73. CURSORINFO *ci = &capture->ci;
  74. POINT win_pos = {capture->x, capture->y};
  75. if (!(capture->ci.flags & CURSOR_SHOWING))
  76. return;
  77. icon = CopyIcon(capture->ci.hCursor);
  78. if (!icon)
  79. return;
  80. if (GetIconInfo(icon, &ii)) {
  81. POINT pos;
  82. if (window)
  83. ClientToScreen(window, &win_pos);
  84. pos.x = ci->ptScreenPos.x - (int)ii.xHotspot - win_pos.x;
  85. pos.y = ci->ptScreenPos.y - (int)ii.yHotspot - win_pos.y;
  86. DrawIcon(hdc, pos.x, pos.y, icon);
  87. DeleteObject(ii.hbmColor);
  88. DeleteObject(ii.hbmMask);
  89. }
  90. DestroyIcon(icon);
  91. }
  92. static inline HDC dc_capture_get_dc(struct dc_capture *capture)
  93. {
  94. if (!capture->valid)
  95. return NULL;
  96. if (capture->compatibility)
  97. return capture->hdc;
  98. else
  99. return texture_get_dc(capture->textures[capture->cur_tex]);
  100. }
  101. static inline void dc_capture_release_dc(struct dc_capture *capture)
  102. {
  103. if (capture->compatibility) {
  104. texture_setimage(capture->textures[capture->cur_tex],
  105. capture->bits, capture->width*4, false);
  106. } else {
  107. texture_release_dc(capture->textures[capture->cur_tex]);
  108. }
  109. }
  110. void dc_capture_capture(struct dc_capture *capture, HWND window)
  111. {
  112. HDC hdc_target;
  113. HDC hdc;
  114. if (capture->capture_cursor) {
  115. memset(&capture->ci, 0, sizeof(CURSORINFO));
  116. capture->ci.cbSize = sizeof(CURSORINFO);
  117. capture->cursor_captured = GetCursorInfo(&capture->ci);
  118. }
  119. if (++capture->cur_tex == capture->num_textures)
  120. capture->cur_tex = 0;
  121. hdc = dc_capture_get_dc(capture);
  122. if (!hdc) {
  123. blog(LOG_WARNING, "[capture_screen] Failed to get "
  124. "texture DC");
  125. return;
  126. }
  127. hdc_target = GetDC(window);
  128. BitBlt(hdc, 0, 0, capture->width, capture->height,
  129. hdc_target, capture->x, capture->y, SRCCOPY);
  130. ReleaseDC(NULL, hdc_target);
  131. if (capture->cursor_captured)
  132. draw_cursor(capture, hdc, window);
  133. dc_capture_release_dc(capture);
  134. capture->textures_written[capture->cur_tex] = true;
  135. }
  136. static void draw_texture(struct dc_capture *capture, int id, effect_t effect)
  137. {
  138. texture_t texture = capture->textures[id];
  139. technique_t tech = effect_gettechnique(effect, "Draw");
  140. eparam_t image = effect_getparambyname(effect, "image");
  141. size_t passes;
  142. effect_settexture(image, texture);
  143. passes = technique_begin(tech);
  144. for (size_t i = 0; i < passes; i++) {
  145. if (technique_beginpass(tech, i)) {
  146. if (capture->compatibility)
  147. gs_draw_sprite(texture, GS_FLIP_V, 0, 0);
  148. else
  149. gs_draw_sprite(texture, 0, 0, 0);
  150. technique_endpass(tech);
  151. }
  152. }
  153. technique_end(tech);
  154. }
  155. void dc_capture_render(struct dc_capture *capture, effect_t effect)
  156. {
  157. int last_tex = (capture->cur_tex > 0) ?
  158. capture->cur_tex-1 : capture->num_textures-1;
  159. if (!capture->valid)
  160. return;
  161. if (capture->textures_written[last_tex])
  162. draw_texture(capture, last_tex, effect);
  163. }
  164. effect_t create_opaque_effect(void)
  165. {
  166. effect_t opaque_effect;
  167. char *effect_file;
  168. char *error_string = NULL;
  169. effect_file = obs_module_file("opaque.effect");
  170. if (!effect_file) {
  171. blog(LOG_ERROR, "[create_opaque_effect] Could not find "
  172. "opaque effect file");
  173. return false;
  174. }
  175. gs_entercontext(obs_graphics());
  176. opaque_effect = gs_create_effect_from_file(effect_file, &error_string);
  177. if (!opaque_effect) {
  178. if (error_string)
  179. blog(LOG_ERROR, "[create_opaque_effect] Failed to "
  180. "create opaque effect:\n%s",
  181. error_string);
  182. else
  183. blog(LOG_ERROR, "[create_opaque_effect] Failed to "
  184. "create opaque effect");
  185. }
  186. bfree(effect_file);
  187. bfree(error_string);
  188. gs_leavecontext();
  189. return opaque_effect;
  190. }