1
0

obs-display.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. return true;
  36. }
  37. obs_display_t *obs_display_create(const struct gs_init_data *graphics_data)
  38. {
  39. struct obs_display *display = bzalloc(sizeof(struct obs_display));
  40. gs_enter_context(obs->video.graphics);
  41. if (!obs_display_init(display, graphics_data)) {
  42. obs_display_destroy(display);
  43. display = NULL;
  44. } else {
  45. pthread_mutex_lock(&obs->data.displays_mutex);
  46. display->prev_next = &obs->data.first_display;
  47. display->next = obs->data.first_display;
  48. obs->data.first_display = display;
  49. if (display->next)
  50. display->next->prev_next = &display->next;
  51. pthread_mutex_unlock(&obs->data.displays_mutex);
  52. }
  53. gs_leave_context();
  54. return display;
  55. }
  56. void obs_display_free(obs_display_t *display)
  57. {
  58. pthread_mutex_destroy(&display->draw_callbacks_mutex);
  59. da_free(display->draw_callbacks);
  60. if (display->swap) {
  61. gs_swapchain_destroy(display->swap);
  62. display->swap = NULL;
  63. }
  64. }
  65. void obs_display_destroy(obs_display_t *display)
  66. {
  67. if (display) {
  68. pthread_mutex_lock(&obs->data.displays_mutex);
  69. if (display->prev_next)
  70. *display->prev_next = display->next;
  71. if (display->next)
  72. display->next->prev_next = display->prev_next;
  73. pthread_mutex_unlock(&obs->data.displays_mutex);
  74. obs_enter_graphics();
  75. obs_display_free(display);
  76. obs_leave_graphics();
  77. bfree(display);
  78. }
  79. }
  80. void obs_display_resize(obs_display_t *display, uint32_t cx, uint32_t cy)
  81. {
  82. if (!display) return;
  83. pthread_mutex_lock(&display->draw_callbacks_mutex);
  84. display->cx = cx;
  85. display->cy = cy;
  86. display->size_changed = true;
  87. pthread_mutex_unlock(&display->draw_callbacks_mutex);
  88. }
  89. void obs_display_add_draw_callback(obs_display_t *display,
  90. void (*draw)(void *param, uint32_t cx, uint32_t cy),
  91. void *param)
  92. {
  93. if (!display) return;
  94. struct draw_callback data = {draw, param};
  95. pthread_mutex_lock(&display->draw_callbacks_mutex);
  96. da_push_back(display->draw_callbacks, &data);
  97. pthread_mutex_unlock(&display->draw_callbacks_mutex);
  98. }
  99. void obs_display_remove_draw_callback(obs_display_t *display,
  100. void (*draw)(void *param, uint32_t cx, uint32_t cy),
  101. void *param)
  102. {
  103. if (!display) return;
  104. struct draw_callback data = {draw, param};
  105. pthread_mutex_lock(&display->draw_callbacks_mutex);
  106. da_erase_item(display->draw_callbacks, &data);
  107. pthread_mutex_unlock(&display->draw_callbacks_mutex);
  108. }
  109. static inline void render_display_begin(struct obs_display *display)
  110. {
  111. struct vec4 clear_color;
  112. gs_load_swapchain(display ? display->swap : NULL);
  113. if (display->size_changed) {
  114. gs_resize(display->cx, display->cy);
  115. display->size_changed = false;
  116. }
  117. gs_begin_scene();
  118. vec4_set(&clear_color, 0.3f, 0.3f, 0.3f, 1.0f);
  119. gs_clear(GS_CLEAR_COLOR | GS_CLEAR_DEPTH | GS_CLEAR_STENCIL,
  120. &clear_color, 1.0f, 0);
  121. gs_enable_depth_test(false);
  122. /* gs_enable_blending(false); */
  123. gs_set_cull_mode(GS_NEITHER);
  124. gs_ortho(0.0f, (float)display->cx,
  125. 0.0f, (float)display->cy, -100.0f, 100.0f);
  126. gs_set_viewport(0, 0, display->cx, display->cy);
  127. }
  128. static inline void render_display_end()
  129. {
  130. gs_end_scene();
  131. gs_present();
  132. }
  133. void render_display(struct obs_display *display)
  134. {
  135. if (!display) return;
  136. render_display_begin(display);
  137. pthread_mutex_lock(&display->draw_callbacks_mutex);
  138. for (size_t i = 0; i < display->draw_callbacks.num; i++) {
  139. struct draw_callback *callback;
  140. callback = display->draw_callbacks.array+i;
  141. callback->draw(callback->param, display->cx, display->cy);
  142. }
  143. pthread_mutex_unlock(&display->draw_callbacks_mutex);
  144. render_display_end();
  145. }