obs-display.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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_create_swapchain(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. if (!obs_display_init(display, graphics_data)) {
  41. obs_display_destroy(display);
  42. display = NULL;
  43. }
  44. return display;
  45. }
  46. void obs_display_free(obs_display_t display)
  47. {
  48. pthread_mutex_destroy(&display->draw_callbacks_mutex);
  49. da_free(display->draw_callbacks);
  50. if (display->swap) {
  51. swapchain_destroy(display->swap);
  52. display->swap = NULL;
  53. }
  54. }
  55. void obs_display_destroy(obs_display_t display)
  56. {
  57. if (display) {
  58. pthread_mutex_lock(&obs->data.displays_mutex);
  59. da_erase_item(obs->data.displays, &display);
  60. pthread_mutex_unlock(&obs->data.displays_mutex);
  61. obs_display_free(display);
  62. bfree(display);
  63. }
  64. }
  65. void obs_display_resize(obs_display_t display, uint32_t cx, uint32_t cy)
  66. {
  67. if (!display) return;
  68. pthread_mutex_lock(&display->draw_callbacks_mutex);
  69. display->cx = cx;
  70. display->cy = cy;
  71. display->size_changed = true;
  72. pthread_mutex_unlock(&display->draw_callbacks_mutex);
  73. }
  74. void obs_display_add_draw_callback(obs_display_t display,
  75. void (*draw)(void *param, uint32_t cx, uint32_t cy),
  76. void *param)
  77. {
  78. if (!display) return;
  79. struct draw_callback data = {draw, param};
  80. pthread_mutex_lock(&display->draw_callbacks_mutex);
  81. da_push_back(display->draw_callbacks, &data);
  82. pthread_mutex_unlock(&display->draw_callbacks_mutex);
  83. }
  84. void obs_display_remove_draw_callback(obs_display_t display,
  85. void (*draw)(void *param, uint32_t cx, uint32_t cy),
  86. void *param)
  87. {
  88. if (!display) return;
  89. struct draw_callback data = {draw, param};
  90. pthread_mutex_lock(&display->draw_callbacks_mutex);
  91. da_erase_item(display->draw_callbacks, &data);
  92. pthread_mutex_unlock(&display->draw_callbacks_mutex);
  93. }
  94. static inline void render_display_begin(struct obs_display *display)
  95. {
  96. struct vec4 clear_color;
  97. gs_load_swapchain(display ? display->swap : NULL);
  98. if (display->size_changed) {
  99. gs_resize(display->cx, display->cy);
  100. display->size_changed = false;
  101. }
  102. gs_beginscene();
  103. vec4_set(&clear_color, 0.3f, 0.0f, 0.0f, 1.0f);
  104. gs_clear(GS_CLEAR_COLOR | GS_CLEAR_DEPTH | GS_CLEAR_STENCIL,
  105. &clear_color, 1.0f, 0);
  106. gs_enable_depthtest(false);
  107. /* gs_enable_blending(false); */
  108. gs_setcullmode(GS_NEITHER);
  109. gs_ortho(0.0f, (float)obs->video.base_width,
  110. 0.0f, (float)obs->video.base_height, -100.0f, 100.0f);
  111. gs_setviewport(0, 0, display->cx, display->cy);
  112. }
  113. static inline void render_display_end()
  114. {
  115. gs_endscene();
  116. gs_present();
  117. }
  118. void render_display(struct obs_display *display)
  119. {
  120. if (!display) return;
  121. render_display_begin(display);
  122. pthread_mutex_lock(&display->draw_callbacks_mutex);
  123. for (size_t i = 0; i < display->draw_callbacks.num; i++) {
  124. struct draw_callback *callback;
  125. callback = display->draw_callbacks.array+i;
  126. callback->draw(callback->param, display->cx, display->cy);
  127. }
  128. pthread_mutex_unlock(&display->draw_callbacks_mutex);
  129. render_display_end();
  130. }