cursor-capture.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #include <windows.h>
  2. #include <obs.h>
  3. #include "cursor-capture.h"
  4. static uint8_t *get_bitmap_data(HBITMAP hbmp, BITMAP *bmp)
  5. {
  6. if (GetObject(hbmp, sizeof(*bmp), bmp) != 0) {
  7. uint8_t *output;
  8. unsigned int size = bmp->bmHeight * bmp->bmWidthBytes;
  9. output = bmalloc(size);
  10. GetBitmapBits(hbmp, size, output);
  11. return output;
  12. }
  13. return NULL;
  14. }
  15. static inline uint8_t bit_to_alpha(uint8_t *data, long pixel, bool invert)
  16. {
  17. uint8_t pix_byte = data[pixel / 8];
  18. bool alpha = (pix_byte >> (7 - pixel % 8) & 1) != 0;
  19. if (invert) {
  20. return alpha ? 0xFF : 0;
  21. } else {
  22. return alpha ? 0 : 0xFF;
  23. }
  24. }
  25. static inline bool bitmap_has_alpha(uint8_t *data, long num_pixels)
  26. {
  27. for (long i = 0; i < num_pixels; i++) {
  28. if (data[i * 4 + 3] != 0) {
  29. return true;
  30. }
  31. }
  32. return false;
  33. }
  34. static inline void apply_mask(uint8_t *color, uint8_t *mask, BITMAP *bmp_mask)
  35. {
  36. long mask_pix_offs;
  37. for (long y = 0; y < bmp_mask->bmHeight; y++) {
  38. for (long x = 0; x < bmp_mask->bmWidth; x++) {
  39. mask_pix_offs = y * (bmp_mask->bmWidthBytes * 8) + x;
  40. color[(y * bmp_mask->bmWidth + x) * 4 + 3] = bit_to_alpha(mask, mask_pix_offs, false);
  41. }
  42. }
  43. }
  44. static inline uint8_t *copy_from_color(ICONINFO *ii, uint32_t *width, uint32_t *height)
  45. {
  46. BITMAP bmp_color;
  47. BITMAP bmp_mask;
  48. uint8_t *color;
  49. uint8_t *mask;
  50. color = get_bitmap_data(ii->hbmColor, &bmp_color);
  51. if (!color) {
  52. return NULL;
  53. }
  54. if (bmp_color.bmBitsPixel < 32) {
  55. bfree(color);
  56. return NULL;
  57. }
  58. mask = get_bitmap_data(ii->hbmMask, &bmp_mask);
  59. if (mask) {
  60. long pixels = bmp_color.bmHeight * bmp_color.bmWidth;
  61. if (!bitmap_has_alpha(color, pixels))
  62. apply_mask(color, mask, &bmp_mask);
  63. bfree(mask);
  64. }
  65. *width = bmp_color.bmWidth;
  66. *height = bmp_color.bmHeight;
  67. return color;
  68. }
  69. static inline uint8_t *copy_from_mask(ICONINFO *ii, uint32_t *width, uint32_t *height)
  70. {
  71. uint8_t *output;
  72. uint8_t *mask;
  73. long pixels;
  74. long bottom;
  75. BITMAP bmp;
  76. mask = get_bitmap_data(ii->hbmMask, &bmp);
  77. if (!mask) {
  78. return NULL;
  79. }
  80. bmp.bmHeight /= 2;
  81. pixels = bmp.bmHeight * bmp.bmWidth;
  82. output = bzalloc(pixels * 4);
  83. bottom = bmp.bmWidthBytes * bmp.bmHeight;
  84. for (long i = 0; i < pixels; i++) {
  85. uint8_t andMask = bit_to_alpha(mask, i, true);
  86. uint8_t xorMask = bit_to_alpha(mask + bottom, i, true);
  87. if (!andMask) {
  88. // black in the AND mask
  89. *(uint32_t *)&output[i * 4] = !!xorMask ? 0x00FFFFFF /*always white*/
  90. : 0xFF000000 /*always black*/;
  91. } else {
  92. // white in the AND mask
  93. *(uint32_t *)&output[i * 4] = !!xorMask ? 0xFFFFFFFF /*source inverted*/
  94. : 0 /*transparent*/;
  95. }
  96. }
  97. bfree(mask);
  98. *width = bmp.bmWidth;
  99. *height = bmp.bmHeight;
  100. return output;
  101. }
  102. static inline uint8_t *cursor_capture_icon_bitmap(ICONINFO *ii, uint32_t *width, uint32_t *height, bool *monochrome)
  103. {
  104. uint8_t *output;
  105. *monochrome = false;
  106. output = copy_from_color(ii, width, height);
  107. if (!output) {
  108. *monochrome = true;
  109. output = copy_from_mask(ii, width, height);
  110. }
  111. return output;
  112. }
  113. static gs_texture_t *get_cached_texture(struct cursor_data *data, uint32_t cx, uint32_t cy)
  114. {
  115. struct cached_cursor cc;
  116. for (size_t i = 0; i < data->cached_textures.num; i++) {
  117. struct cached_cursor *pcc = &data->cached_textures.array[i];
  118. if (pcc->cx == cx && pcc->cy == cy)
  119. return pcc->texture;
  120. }
  121. cc.texture = gs_texture_create(cx, cy, GS_BGRA, 1, NULL, GS_DYNAMIC);
  122. cc.cx = cx;
  123. cc.cy = cy;
  124. da_push_back(data->cached_textures, &cc);
  125. return cc.texture;
  126. }
  127. static inline bool cursor_capture_icon(struct cursor_data *data, HICON icon)
  128. {
  129. uint8_t *bitmap;
  130. uint32_t height;
  131. uint32_t width;
  132. ICONINFO ii;
  133. if (!icon) {
  134. return false;
  135. }
  136. if (!GetIconInfo(icon, &ii)) {
  137. return false;
  138. }
  139. bitmap = cursor_capture_icon_bitmap(&ii, &width, &height, &data->monochrome);
  140. if (bitmap) {
  141. if (data->last_cx != width || data->last_cy != height) {
  142. data->texture = get_cached_texture(data, width, height);
  143. data->last_cx = width;
  144. data->last_cy = height;
  145. }
  146. gs_texture_set_image(data->texture, bitmap, width * 4, false);
  147. bfree(bitmap);
  148. data->x_hotspot = ii.xHotspot;
  149. data->y_hotspot = ii.yHotspot;
  150. }
  151. DeleteObject(ii.hbmColor);
  152. DeleteObject(ii.hbmMask);
  153. return !!data->texture;
  154. }
  155. void cursor_capture(struct cursor_data *data)
  156. {
  157. CURSORINFO ci = {0};
  158. HICON icon;
  159. ci.cbSize = sizeof(ci);
  160. if (!GetCursorInfo(&ci)) {
  161. data->visible = false;
  162. return;
  163. }
  164. memcpy(&data->cursor_pos, &ci.ptScreenPos, sizeof(data->cursor_pos));
  165. if (data->current_cursor == ci.hCursor) {
  166. return;
  167. }
  168. icon = CopyIcon(ci.hCursor);
  169. data->visible = cursor_capture_icon(data, icon);
  170. data->current_cursor = ci.hCursor;
  171. if ((ci.flags & CURSOR_SHOWING) == 0)
  172. data->visible = false;
  173. DestroyIcon(icon);
  174. }
  175. void cursor_draw(struct cursor_data *data, long x_offset, long y_offset, long width, long height)
  176. {
  177. long x = data->cursor_pos.x + x_offset;
  178. long y = data->cursor_pos.y + y_offset;
  179. long x_draw = x - data->x_hotspot;
  180. long y_draw = y - data->y_hotspot;
  181. if (x < 0 || x > width || y < 0 || y > height)
  182. return;
  183. if (data->visible && !!data->texture) {
  184. gs_blend_state_push();
  185. enum gs_blend_type blendMode = data->monochrome ? GS_BLEND_INVDSTCOLOR : GS_BLEND_SRCALPHA;
  186. gs_blend_function_separate(blendMode, /*src_color*/
  187. GS_BLEND_INVSRCALPHA /*dest_color*/, GS_BLEND_ONE /*src_alpha*/,
  188. GS_BLEND_INVSRCALPHA /*dest_alpha*/);
  189. gs_matrix_push();
  190. obs_source_draw(data->texture, x_draw, y_draw, 0, 0, false);
  191. gs_matrix_pop();
  192. gs_blend_state_pop();
  193. }
  194. }
  195. void cursor_data_free(struct cursor_data *data)
  196. {
  197. for (size_t i = 0; i < data->cached_textures.num; i++) {
  198. struct cached_cursor *pcc = &data->cached_textures.array[i];
  199. gs_texture_destroy(pcc->texture);
  200. }
  201. da_free(data->cached_textures);
  202. memset(data, 0, sizeof(*data));
  203. }