obs-display.c 6.3 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. 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)
  92. return;
  93. pthread_mutex_lock(&display->draw_info_mutex);
  94. display->cx = cx;
  95. display->cy = cy;
  96. display->size_changed = true;
  97. pthread_mutex_unlock(&display->draw_info_mutex);
  98. }
  99. void obs_display_add_draw_callback(obs_display_t *display,
  100. void (*draw)(void *param, uint32_t cx,
  101. uint32_t cy),
  102. void *param)
  103. {
  104. if (!display)
  105. 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,
  113. uint32_t cy),
  114. void *param)
  115. {
  116. if (!display)
  117. return;
  118. struct draw_callback data = {draw, param};
  119. pthread_mutex_lock(&display->draw_callbacks_mutex);
  120. da_erase_item(display->draw_callbacks, &data);
  121. pthread_mutex_unlock(&display->draw_callbacks_mutex);
  122. }
  123. static inline void render_display_begin(struct obs_display *display,
  124. uint32_t cx, uint32_t cy,
  125. bool size_changed)
  126. {
  127. struct vec4 clear_color;
  128. gs_load_swapchain(display->swap);
  129. if (size_changed)
  130. gs_resize(cx, cy);
  131. gs_begin_scene();
  132. vec4_from_rgba(&clear_color, display->background_color);
  133. clear_color.w = 1.0f;
  134. gs_clear(GS_CLEAR_COLOR | GS_CLEAR_DEPTH | GS_CLEAR_STENCIL,
  135. &clear_color, 1.0f, 0);
  136. gs_enable_depth_test(false);
  137. /* gs_enable_blending(false); */
  138. gs_set_cull_mode(GS_NEITHER);
  139. gs_ortho(0.0f, (float)cx, 0.0f, (float)cy, -100.0f, 100.0f);
  140. gs_set_viewport(0, 0, cx, cy);
  141. }
  142. static inline void render_display_end()
  143. {
  144. gs_end_scene();
  145. }
  146. void render_display(struct obs_display *display)
  147. {
  148. uint32_t cx, cy;
  149. bool size_changed;
  150. if (!display || !display->enabled)
  151. return;
  152. GS_DEBUG_MARKER_BEGIN(GS_DEBUG_COLOR_DISPLAY, "obs_display");
  153. /* -------------------------------------------- */
  154. pthread_mutex_lock(&display->draw_info_mutex);
  155. cx = display->cx;
  156. cy = display->cy;
  157. size_changed = display->size_changed;
  158. if (size_changed)
  159. display->size_changed = false;
  160. pthread_mutex_unlock(&display->draw_info_mutex);
  161. /* -------------------------------------------- */
  162. render_display_begin(display, cx, cy, size_changed);
  163. pthread_mutex_lock(&display->draw_callbacks_mutex);
  164. for (size_t i = 0; i < display->draw_callbacks.num; i++) {
  165. struct draw_callback *callback;
  166. callback = display->draw_callbacks.array + i;
  167. callback->draw(callback->param, cx, cy);
  168. }
  169. pthread_mutex_unlock(&display->draw_callbacks_mutex);
  170. render_display_end();
  171. GS_DEBUG_MARKER_END();
  172. gs_present();
  173. }
  174. void obs_display_set_enabled(obs_display_t *display, bool enable)
  175. {
  176. if (display)
  177. display->enabled = enable;
  178. }
  179. bool obs_display_enabled(obs_display_t *display)
  180. {
  181. return display ? display->enabled : false;
  182. }
  183. void obs_display_set_background_color(obs_display_t *display, uint32_t color)
  184. {
  185. if (display)
  186. display->background_color = color;
  187. }
  188. void obs_display_size(obs_display_t *display, uint32_t *width, uint32_t *height)
  189. {
  190. *width = 0;
  191. *height = 0;
  192. if (display) {
  193. pthread_mutex_lock(&display->draw_info_mutex);
  194. *width = display->cx;
  195. *height = display->cy;
  196. pthread_mutex_unlock(&display->draw_info_mutex);
  197. }
  198. }