obs-display.c 6.1 KB

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