obs-display.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. display->cx = graphics_data->cx;
  30. display->cy = graphics_data->cy;
  31. }
  32. if (pthread_mutex_init(&display->draw_callbacks_mutex, NULL) != 0) {
  33. blog(LOG_ERROR, "obs_display_init: Failed to create mutex");
  34. return false;
  35. }
  36. if (pthread_mutex_init(&display->draw_info_mutex, NULL) != 0) {
  37. blog(LOG_ERROR, "obs_display_init: Failed to create mutex");
  38. return false;
  39. }
  40. display->enabled = true;
  41. return true;
  42. }
  43. obs_display_t *obs_display_create(const struct gs_init_data *graphics_data,
  44. uint32_t background_color)
  45. {
  46. struct obs_display *display = bzalloc(sizeof(struct obs_display));
  47. gs_enter_context(obs->video.graphics);
  48. display->background_color = background_color;
  49. if (!obs_display_init(display, graphics_data)) {
  50. obs_display_destroy(display);
  51. display = NULL;
  52. } else {
  53. pthread_mutex_lock(&obs->data.displays_mutex);
  54. display->prev_next = &obs->data.first_display;
  55. display->next = obs->data.first_display;
  56. obs->data.first_display = display;
  57. if (display->next)
  58. display->next->prev_next = &display->next;
  59. pthread_mutex_unlock(&obs->data.displays_mutex);
  60. }
  61. gs_leave_context();
  62. return display;
  63. }
  64. void obs_display_free(obs_display_t *display)
  65. {
  66. pthread_mutex_destroy(&display->draw_callbacks_mutex);
  67. pthread_mutex_destroy(&display->draw_info_mutex);
  68. da_free(display->draw_callbacks);
  69. if (display->swap) {
  70. gs_swapchain_destroy(display->swap);
  71. display->swap = NULL;
  72. }
  73. }
  74. void obs_display_destroy(obs_display_t *display)
  75. {
  76. if (display) {
  77. pthread_mutex_lock(&obs->data.displays_mutex);
  78. if (display->prev_next)
  79. *display->prev_next = display->next;
  80. if (display->next)
  81. display->next->prev_next = display->prev_next;
  82. pthread_mutex_unlock(&obs->data.displays_mutex);
  83. obs_enter_graphics();
  84. obs_display_free(display);
  85. obs_leave_graphics();
  86. bfree(display);
  87. }
  88. }
  89. void obs_display_resize(obs_display_t *display, uint32_t cx, uint32_t cy)
  90. {
  91. if (!display) return;
  92. pthread_mutex_lock(&display->draw_info_mutex);
  93. display->cx = cx;
  94. display->cy = cy;
  95. display->size_changed = true;
  96. pthread_mutex_unlock(&display->draw_info_mutex);
  97. }
  98. void obs_display_add_draw_callback(obs_display_t *display,
  99. void (*draw)(void *param, uint32_t cx, uint32_t cy),
  100. void *param)
  101. {
  102. if (!display) return;
  103. struct draw_callback data = {draw, param};
  104. pthread_mutex_lock(&display->draw_callbacks_mutex);
  105. da_push_back(display->draw_callbacks, &data);
  106. pthread_mutex_unlock(&display->draw_callbacks_mutex);
  107. }
  108. void obs_display_remove_draw_callback(obs_display_t *display,
  109. void (*draw)(void *param, uint32_t cx, uint32_t cy),
  110. void *param)
  111. {
  112. if (!display) return;
  113. struct draw_callback data = {draw, param};
  114. pthread_mutex_lock(&display->draw_callbacks_mutex);
  115. da_erase_item(display->draw_callbacks, &data);
  116. pthread_mutex_unlock(&display->draw_callbacks_mutex);
  117. }
  118. static inline void render_display_begin(struct obs_display *display,
  119. uint32_t cx, uint32_t cy, bool size_changed)
  120. {
  121. struct vec4 clear_color;
  122. gs_load_swapchain(display->swap);
  123. if (size_changed)
  124. gs_resize(cx, cy);
  125. gs_begin_scene();
  126. vec4_from_rgba(&clear_color, display->background_color);
  127. clear_color.w = 1.0f;
  128. gs_clear(GS_CLEAR_COLOR | GS_CLEAR_DEPTH | GS_CLEAR_STENCIL,
  129. &clear_color, 1.0f, 0);
  130. gs_enable_depth_test(false);
  131. /* gs_enable_blending(false); */
  132. gs_set_cull_mode(GS_NEITHER);
  133. gs_ortho(0.0f, (float)cx, 0.0f, (float)cy, -100.0f, 100.0f);
  134. gs_set_viewport(0, 0, cx, cy);
  135. }
  136. static inline void render_display_end()
  137. {
  138. gs_end_scene();
  139. }
  140. void render_display(struct obs_display *display)
  141. {
  142. uint32_t cx, cy;
  143. bool size_changed;
  144. if (!display || !display->enabled) return;
  145. GS_DEBUG_MARKER_BEGIN(GS_DEBUG_COLOR_DISPLAY, "obs_display");
  146. /* -------------------------------------------- */
  147. pthread_mutex_lock(&display->draw_info_mutex);
  148. cx = display->cx;
  149. cy = display->cy;
  150. size_changed = display->size_changed;
  151. if (size_changed)
  152. display->size_changed = false;
  153. pthread_mutex_unlock(&display->draw_info_mutex);
  154. /* -------------------------------------------- */
  155. render_display_begin(display, cx, cy, size_changed);
  156. pthread_mutex_lock(&display->draw_callbacks_mutex);
  157. for (size_t i = 0; i < display->draw_callbacks.num; i++) {
  158. struct draw_callback *callback;
  159. callback = display->draw_callbacks.array+i;
  160. callback->draw(callback->param, cx, cy);
  161. }
  162. pthread_mutex_unlock(&display->draw_callbacks_mutex);
  163. render_display_end();
  164. GS_DEBUG_MARKER_END();
  165. gs_present();
  166. }
  167. void obs_display_set_enabled(obs_display_t *display, bool enable)
  168. {
  169. if (display)
  170. display->enabled = enable;
  171. }
  172. bool obs_display_enabled(obs_display_t *display)
  173. {
  174. return display ? display->enabled : false;
  175. }
  176. void obs_display_set_background_color(obs_display_t *display, uint32_t color)
  177. {
  178. if (display)
  179. display->background_color = color;
  180. }