obs-display.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /******************************************************************************
  2. Copyright (C) 2013 by Hugh Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include "graphics/vec4.h"
  15. #include "obs.h"
  16. #include "obs-internal.h"
  17. bool obs_display_init(struct obs_display *display,
  18. const struct gs_init_data *graphics_data)
  19. {
  20. pthread_mutex_init_value(&display->draw_callbacks_mutex);
  21. pthread_mutex_init_value(&display->draw_info_mutex);
  22. if (graphics_data) {
  23. display->swap = gs_swapchain_create(graphics_data);
  24. if (!display->swap) {
  25. blog(LOG_ERROR, "obs_display_init: Failed to "
  26. "create swap chain");
  27. return false;
  28. }
  29. const uint32_t cx = graphics_data->cx;
  30. const uint32_t cy = graphics_data->cy;
  31. display->cx = cx;
  32. display->cy = cy;
  33. display->next_cx = cx;
  34. display->next_cy = cy;
  35. }
  36. if (pthread_mutex_init(&display->draw_callbacks_mutex, NULL) != 0) {
  37. blog(LOG_ERROR, "obs_display_init: Failed to create mutex");
  38. return false;
  39. }
  40. if (pthread_mutex_init(&display->draw_info_mutex, NULL) != 0) {
  41. blog(LOG_ERROR, "obs_display_init: Failed to create mutex");
  42. return false;
  43. }
  44. display->enabled = true;
  45. return true;
  46. }
  47. obs_display_t *obs_display_create(const struct gs_init_data *graphics_data,
  48. uint32_t background_color)
  49. {
  50. struct obs_display *display = bzalloc(sizeof(struct obs_display));
  51. gs_enter_context(obs->video.graphics);
  52. display->background_color = background_color;
  53. if (!obs_display_init(display, graphics_data)) {
  54. obs_display_destroy(display);
  55. display = NULL;
  56. } else {
  57. pthread_mutex_lock(&obs->data.displays_mutex);
  58. display->prev_next = &obs->data.first_display;
  59. display->next = obs->data.first_display;
  60. obs->data.first_display = display;
  61. if (display->next)
  62. display->next->prev_next = &display->next;
  63. pthread_mutex_unlock(&obs->data.displays_mutex);
  64. }
  65. gs_leave_context();
  66. return display;
  67. }
  68. void obs_display_free(obs_display_t *display)
  69. {
  70. pthread_mutex_destroy(&display->draw_callbacks_mutex);
  71. pthread_mutex_destroy(&display->draw_info_mutex);
  72. da_free(display->draw_callbacks);
  73. if (display->swap) {
  74. gs_swapchain_destroy(display->swap);
  75. display->swap = NULL;
  76. }
  77. }
  78. void obs_display_destroy(obs_display_t *display)
  79. {
  80. if (display) {
  81. pthread_mutex_lock(&obs->data.displays_mutex);
  82. if (display->prev_next)
  83. *display->prev_next = display->next;
  84. if (display->next)
  85. display->next->prev_next = display->prev_next;
  86. pthread_mutex_unlock(&obs->data.displays_mutex);
  87. obs_enter_graphics();
  88. obs_display_free(display);
  89. obs_leave_graphics();
  90. bfree(display);
  91. }
  92. }
  93. void obs_display_resize(obs_display_t *display, uint32_t cx, uint32_t cy)
  94. {
  95. if (!display)
  96. return;
  97. pthread_mutex_lock(&display->draw_info_mutex);
  98. display->next_cx = cx;
  99. display->next_cy = cy;
  100. pthread_mutex_unlock(&display->draw_info_mutex);
  101. }
  102. void obs_display_add_draw_callback(obs_display_t *display,
  103. void (*draw)(void *param, uint32_t cx,
  104. uint32_t cy),
  105. void *param)
  106. {
  107. if (!display)
  108. return;
  109. struct draw_callback data = {draw, param};
  110. pthread_mutex_lock(&display->draw_callbacks_mutex);
  111. da_push_back(display->draw_callbacks, &data);
  112. pthread_mutex_unlock(&display->draw_callbacks_mutex);
  113. }
  114. void obs_display_remove_draw_callback(obs_display_t *display,
  115. void (*draw)(void *param, uint32_t cx,
  116. uint32_t cy),
  117. void *param)
  118. {
  119. if (!display)
  120. return;
  121. struct draw_callback data = {draw, param};
  122. pthread_mutex_lock(&display->draw_callbacks_mutex);
  123. da_erase_item(display->draw_callbacks, &data);
  124. pthread_mutex_unlock(&display->draw_callbacks_mutex);
  125. }
  126. static inline void render_display_begin(struct obs_display *display,
  127. uint32_t cx, uint32_t cy)
  128. {
  129. struct vec4 clear_color;
  130. gs_load_swapchain(display->swap);
  131. if ((display->cx != cx) || (display->cy != cy)) {
  132. gs_resize(cx, cy);
  133. display->cx = cx;
  134. display->cy = cy;
  135. }
  136. gs_begin_scene();
  137. vec4_from_rgba(&clear_color, display->background_color);
  138. clear_color.w = 1.0f;
  139. gs_clear(GS_CLEAR_COLOR | GS_CLEAR_DEPTH | GS_CLEAR_STENCIL,
  140. &clear_color, 1.0f, 0);
  141. gs_enable_depth_test(false);
  142. /* gs_enable_blending(false); */
  143. gs_set_cull_mode(GS_NEITHER);
  144. gs_ortho(0.0f, (float)cx, 0.0f, (float)cy, -100.0f, 100.0f);
  145. gs_set_viewport(0, 0, cx, cy);
  146. }
  147. static inline void render_display_end()
  148. {
  149. gs_end_scene();
  150. }
  151. void render_display(struct obs_display *display)
  152. {
  153. uint32_t cx, cy;
  154. if (!display || !display->enabled)
  155. return;
  156. GS_DEBUG_MARKER_BEGIN(GS_DEBUG_COLOR_DISPLAY, "obs_display");
  157. /* -------------------------------------------- */
  158. pthread_mutex_lock(&display->draw_info_mutex);
  159. cx = display->next_cx;
  160. cy = display->next_cy;
  161. pthread_mutex_unlock(&display->draw_info_mutex);
  162. /* -------------------------------------------- */
  163. render_display_begin(display, cx, cy);
  164. pthread_mutex_lock(&display->draw_callbacks_mutex);
  165. for (size_t i = 0; i < display->draw_callbacks.num; i++) {
  166. struct draw_callback *callback;
  167. callback = display->draw_callbacks.array + i;
  168. callback->draw(callback->param, cx, cy);
  169. }
  170. pthread_mutex_unlock(&display->draw_callbacks_mutex);
  171. render_display_end();
  172. GS_DEBUG_MARKER_END();
  173. gs_present();
  174. }
  175. void obs_display_set_enabled(obs_display_t *display, bool enable)
  176. {
  177. if (display)
  178. display->enabled = enable;
  179. }
  180. bool obs_display_enabled(obs_display_t *display)
  181. {
  182. return display ? display->enabled : false;
  183. }
  184. void obs_display_set_background_color(obs_display_t *display, uint32_t color)
  185. {
  186. if (display)
  187. display->background_color = color;
  188. }
  189. void obs_display_size(obs_display_t *display, uint32_t *width, uint32_t *height)
  190. {
  191. *width = 0;
  192. *height = 0;
  193. if (display) {
  194. pthread_mutex_lock(&display->draw_info_mutex);
  195. *width = display->cx;
  196. *height = display->cy;
  197. pthread_mutex_unlock(&display->draw_info_mutex);
  198. }
  199. }