obs-display.c 5.4 KB

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