obs-display.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. 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(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 (!graphics_data->num_backbuffers)
  42. graphics_data->num_backbuffers = 1;
  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. *display->prev_next = display->next;
  72. if (display->next)
  73. display->next->prev_next = display->prev_next;
  74. pthread_mutex_unlock(&obs->data.displays_mutex);
  75. obs_enter_graphics();
  76. obs_display_free(display);
  77. obs_leave_graphics();
  78. bfree(display);
  79. }
  80. }
  81. void obs_display_resize(obs_display_t display, uint32_t cx, uint32_t cy)
  82. {
  83. if (!display) return;
  84. pthread_mutex_lock(&display->draw_callbacks_mutex);
  85. display->cx = cx;
  86. display->cy = cy;
  87. display->size_changed = true;
  88. pthread_mutex_unlock(&display->draw_callbacks_mutex);
  89. }
  90. void obs_display_add_draw_callback(obs_display_t display,
  91. void (*draw)(void *param, uint32_t cx, uint32_t cy),
  92. void *param)
  93. {
  94. if (!display) return;
  95. struct draw_callback data = {draw, param};
  96. pthread_mutex_lock(&display->draw_callbacks_mutex);
  97. da_push_back(display->draw_callbacks, &data);
  98. pthread_mutex_unlock(&display->draw_callbacks_mutex);
  99. }
  100. void obs_display_remove_draw_callback(obs_display_t display,
  101. void (*draw)(void *param, uint32_t cx, uint32_t cy),
  102. void *param)
  103. {
  104. if (!display) return;
  105. struct draw_callback data = {draw, param};
  106. pthread_mutex_lock(&display->draw_callbacks_mutex);
  107. da_erase_item(display->draw_callbacks, &data);
  108. pthread_mutex_unlock(&display->draw_callbacks_mutex);
  109. }
  110. static inline void render_display_begin(struct obs_display *display)
  111. {
  112. struct vec4 clear_color;
  113. gs_load_swapchain(display ? display->swap : NULL);
  114. if (display->size_changed) {
  115. gs_resize(display->cx, display->cy);
  116. display->size_changed = false;
  117. }
  118. gs_begin_scene();
  119. vec4_set(&clear_color, 0.3f, 0.3f, 0.3f, 1.0f);
  120. gs_clear(GS_CLEAR_COLOR | GS_CLEAR_DEPTH | GS_CLEAR_STENCIL,
  121. &clear_color, 1.0f, 0);
  122. gs_enable_depth_test(false);
  123. /* gs_enable_blending(false); */
  124. gs_set_cull_mode(GS_NEITHER);
  125. gs_ortho(0.0f, (float)display->cx,
  126. 0.0f, (float)display->cy, -100.0f, 100.0f);
  127. gs_set_viewport(0, 0, display->cx, display->cy);
  128. }
  129. static inline void render_display_end()
  130. {
  131. gs_end_scene();
  132. gs_present();
  133. }
  134. void render_display(struct obs_display *display)
  135. {
  136. if (!display) return;
  137. render_display_begin(display);
  138. pthread_mutex_lock(&display->draw_callbacks_mutex);
  139. for (size_t i = 0; i < display->draw_callbacks.num; i++) {
  140. struct draw_callback *callback;
  141. callback = display->draw_callbacks.array+i;
  142. callback->draw(callback->param, display->cx, display->cy);
  143. }
  144. pthread_mutex_unlock(&display->draw_callbacks_mutex);
  145. render_display_end();
  146. }