cursor-capture.c 5.7 KB

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